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/drm_hdcp.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/drm_atomic.h>
77 #include <drm/drm_atomic_uapi.h>
78 #include <drm/drm_atomic_helper.h>
79 #include <drm/dp/drm_dp_mst_helper.h>
80 #include <drm/drm_fb_helper.h>
81 #include <drm/drm_fourcc.h>
82 #include <drm/drm_edid.h>
83 #include <drm/drm_vblank.h>
84 #include <drm/drm_audio_component.h>
86 #if defined(CONFIG_DRM_AMD_DC_DCN)
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"
97 #include "modules/inc/mod_freesync.h"
98 #include "modules/power/power_helpers.h"
99 #include "modules/inc/mod_info_packet.h"
101 #define FIRMWARE_RENOIR_DMUB "amdgpu/renoir_dmcub.bin"
102 MODULE_FIRMWARE(FIRMWARE_RENOIR_DMUB);
103 #define FIRMWARE_SIENNA_CICHLID_DMUB "amdgpu/sienna_cichlid_dmcub.bin"
104 MODULE_FIRMWARE(FIRMWARE_SIENNA_CICHLID_DMUB);
105 #define FIRMWARE_NAVY_FLOUNDER_DMUB "amdgpu/navy_flounder_dmcub.bin"
106 MODULE_FIRMWARE(FIRMWARE_NAVY_FLOUNDER_DMUB);
107 #define FIRMWARE_GREEN_SARDINE_DMUB "amdgpu/green_sardine_dmcub.bin"
108 MODULE_FIRMWARE(FIRMWARE_GREEN_SARDINE_DMUB);
109 #define FIRMWARE_VANGOGH_DMUB "amdgpu/vangogh_dmcub.bin"
110 MODULE_FIRMWARE(FIRMWARE_VANGOGH_DMUB);
111 #define FIRMWARE_DIMGREY_CAVEFISH_DMUB "amdgpu/dimgrey_cavefish_dmcub.bin"
112 MODULE_FIRMWARE(FIRMWARE_DIMGREY_CAVEFISH_DMUB);
113 #define FIRMWARE_BEIGE_GOBY_DMUB "amdgpu/beige_goby_dmcub.bin"
114 MODULE_FIRMWARE(FIRMWARE_BEIGE_GOBY_DMUB);
115 #define FIRMWARE_YELLOW_CARP_DMUB "amdgpu/yellow_carp_dmcub.bin"
116 MODULE_FIRMWARE(FIRMWARE_YELLOW_CARP_DMUB);
118 #define FIRMWARE_RAVEN_DMCU "amdgpu/raven_dmcu.bin"
119 MODULE_FIRMWARE(FIRMWARE_RAVEN_DMCU);
121 #define FIRMWARE_NAVI12_DMCU "amdgpu/navi12_dmcu.bin"
122 MODULE_FIRMWARE(FIRMWARE_NAVI12_DMCU);
124 /* Number of bytes in PSP header for firmware. */
125 #define PSP_HEADER_BYTES 0x100
127 /* Number of bytes in PSP footer for firmware. */
128 #define PSP_FOOTER_BYTES 0x100
133 * The AMDgpu display manager, **amdgpu_dm** (or even simpler,
134 * **dm**) sits between DRM and DC. It acts as a liaison, converting DRM
135 * requests into DC requests, and DC responses into DRM responses.
137 * The root control structure is &struct amdgpu_display_manager.
140 /* basic init/fini API */
141 static int amdgpu_dm_init(struct amdgpu_device *adev);
142 static void amdgpu_dm_fini(struct amdgpu_device *adev);
143 static bool is_freesync_video_mode(const struct drm_display_mode *mode, struct amdgpu_dm_connector *aconnector);
145 static enum drm_mode_subconnector get_subconnector_type(struct dc_link *link)
147 switch (link->dpcd_caps.dongle_type) {
148 case DISPLAY_DONGLE_NONE:
149 return DRM_MODE_SUBCONNECTOR_Native;
150 case DISPLAY_DONGLE_DP_VGA_CONVERTER:
151 return DRM_MODE_SUBCONNECTOR_VGA;
152 case DISPLAY_DONGLE_DP_DVI_CONVERTER:
153 case DISPLAY_DONGLE_DP_DVI_DONGLE:
154 return DRM_MODE_SUBCONNECTOR_DVID;
155 case DISPLAY_DONGLE_DP_HDMI_CONVERTER:
156 case DISPLAY_DONGLE_DP_HDMI_DONGLE:
157 return DRM_MODE_SUBCONNECTOR_HDMIA;
158 case DISPLAY_DONGLE_DP_HDMI_MISMATCHED_DONGLE:
160 return DRM_MODE_SUBCONNECTOR_Unknown;
164 static void update_subconnector_property(struct amdgpu_dm_connector *aconnector)
166 struct dc_link *link = aconnector->dc_link;
167 struct drm_connector *connector = &aconnector->base;
168 enum drm_mode_subconnector subconnector = DRM_MODE_SUBCONNECTOR_Unknown;
170 if (connector->connector_type != DRM_MODE_CONNECTOR_DisplayPort)
173 if (aconnector->dc_sink)
174 subconnector = get_subconnector_type(link);
176 drm_object_property_set_value(&connector->base,
177 connector->dev->mode_config.dp_subconnector_property,
182 * initializes drm_device display related structures, based on the information
183 * provided by DAL. The drm strcutures are: drm_crtc, drm_connector,
184 * drm_encoder, drm_mode_config
186 * Returns 0 on success
188 static int amdgpu_dm_initialize_drm_device(struct amdgpu_device *adev);
189 /* removes and deallocates the drm structures, created by the above function */
190 static void amdgpu_dm_destroy_drm_device(struct amdgpu_display_manager *dm);
192 static int amdgpu_dm_plane_init(struct amdgpu_display_manager *dm,
193 struct drm_plane *plane,
194 unsigned long possible_crtcs,
195 const struct dc_plane_cap *plane_cap);
196 static int amdgpu_dm_crtc_init(struct amdgpu_display_manager *dm,
197 struct drm_plane *plane,
198 uint32_t link_index);
199 static int amdgpu_dm_connector_init(struct amdgpu_display_manager *dm,
200 struct amdgpu_dm_connector *amdgpu_dm_connector,
202 struct amdgpu_encoder *amdgpu_encoder);
203 static int amdgpu_dm_encoder_init(struct drm_device *dev,
204 struct amdgpu_encoder *aencoder,
205 uint32_t link_index);
207 static int amdgpu_dm_connector_get_modes(struct drm_connector *connector);
209 static void amdgpu_dm_atomic_commit_tail(struct drm_atomic_state *state);
211 static int amdgpu_dm_atomic_check(struct drm_device *dev,
212 struct drm_atomic_state *state);
214 static void handle_cursor_update(struct drm_plane *plane,
215 struct drm_plane_state *old_plane_state);
217 static const struct drm_format_info *
218 amd_get_format_info(const struct drm_mode_fb_cmd2 *cmd);
220 static void handle_hpd_irq_helper(struct amdgpu_dm_connector *aconnector);
221 static void handle_hpd_rx_irq(void *param);
224 is_timing_unchanged_for_freesync(struct drm_crtc_state *old_crtc_state,
225 struct drm_crtc_state *new_crtc_state);
227 * dm_vblank_get_counter
230 * Get counter for number of vertical blanks
233 * struct amdgpu_device *adev - [in] desired amdgpu device
234 * int disp_idx - [in] which CRTC to get the counter from
237 * Counter for vertical blanks
239 static u32 dm_vblank_get_counter(struct amdgpu_device *adev, int crtc)
241 if (crtc >= adev->mode_info.num_crtc)
244 struct amdgpu_crtc *acrtc = adev->mode_info.crtcs[crtc];
246 if (acrtc->dm_irq_params.stream == NULL) {
247 DRM_ERROR("dc_stream_state is NULL for crtc '%d'!\n",
252 return dc_stream_get_vblank_counter(acrtc->dm_irq_params.stream);
256 static int dm_crtc_get_scanoutpos(struct amdgpu_device *adev, int crtc,
257 u32 *vbl, u32 *position)
259 uint32_t v_blank_start, v_blank_end, h_position, v_position;
261 if ((crtc < 0) || (crtc >= adev->mode_info.num_crtc))
264 struct amdgpu_crtc *acrtc = adev->mode_info.crtcs[crtc];
266 if (acrtc->dm_irq_params.stream == NULL) {
267 DRM_ERROR("dc_stream_state is NULL for crtc '%d'!\n",
273 * TODO rework base driver to use values directly.
274 * for now parse it back into reg-format
276 dc_stream_get_scanoutpos(acrtc->dm_irq_params.stream,
282 *position = v_position | (h_position << 16);
283 *vbl = v_blank_start | (v_blank_end << 16);
289 static bool dm_is_idle(void *handle)
295 static int dm_wait_for_idle(void *handle)
301 static bool dm_check_soft_reset(void *handle)
306 static int dm_soft_reset(void *handle)
312 static struct amdgpu_crtc *
313 get_crtc_by_otg_inst(struct amdgpu_device *adev,
316 struct drm_device *dev = adev_to_drm(adev);
317 struct drm_crtc *crtc;
318 struct amdgpu_crtc *amdgpu_crtc;
320 if (WARN_ON(otg_inst == -1))
321 return adev->mode_info.crtcs[0];
323 list_for_each_entry(crtc, &dev->mode_config.crtc_list, head) {
324 amdgpu_crtc = to_amdgpu_crtc(crtc);
326 if (amdgpu_crtc->otg_inst == otg_inst)
333 static inline bool amdgpu_dm_vrr_active_irq(struct amdgpu_crtc *acrtc)
335 return acrtc->dm_irq_params.freesync_config.state ==
336 VRR_STATE_ACTIVE_VARIABLE ||
337 acrtc->dm_irq_params.freesync_config.state ==
338 VRR_STATE_ACTIVE_FIXED;
341 static inline bool amdgpu_dm_vrr_active(struct dm_crtc_state *dm_state)
343 return dm_state->freesync_config.state == VRR_STATE_ACTIVE_VARIABLE ||
344 dm_state->freesync_config.state == VRR_STATE_ACTIVE_FIXED;
347 static inline bool is_dc_timing_adjust_needed(struct dm_crtc_state *old_state,
348 struct dm_crtc_state *new_state)
350 if (new_state->freesync_config.state == VRR_STATE_ACTIVE_FIXED)
352 else if (amdgpu_dm_vrr_active(old_state) != amdgpu_dm_vrr_active(new_state))
359 * dm_pflip_high_irq() - Handle pageflip interrupt
360 * @interrupt_params: ignored
362 * Handles the pageflip interrupt by notifying all interested parties
363 * that the pageflip has been completed.
365 static void dm_pflip_high_irq(void *interrupt_params)
367 struct amdgpu_crtc *amdgpu_crtc;
368 struct common_irq_params *irq_params = interrupt_params;
369 struct amdgpu_device *adev = irq_params->adev;
371 struct drm_pending_vblank_event *e;
372 uint32_t vpos, hpos, v_blank_start, v_blank_end;
375 amdgpu_crtc = get_crtc_by_otg_inst(adev, irq_params->irq_src - IRQ_TYPE_PFLIP);
377 /* IRQ could occur when in initial stage */
378 /* TODO work and BO cleanup */
379 if (amdgpu_crtc == NULL) {
380 DC_LOG_PFLIP("CRTC is null, returning.\n");
384 spin_lock_irqsave(&adev_to_drm(adev)->event_lock, flags);
386 if (amdgpu_crtc->pflip_status != AMDGPU_FLIP_SUBMITTED){
387 DC_LOG_PFLIP("amdgpu_crtc->pflip_status = %d !=AMDGPU_FLIP_SUBMITTED(%d) on crtc:%d[%p] \n",
388 amdgpu_crtc->pflip_status,
389 AMDGPU_FLIP_SUBMITTED,
390 amdgpu_crtc->crtc_id,
392 spin_unlock_irqrestore(&adev_to_drm(adev)->event_lock, flags);
396 /* page flip completed. */
397 e = amdgpu_crtc->event;
398 amdgpu_crtc->event = NULL;
402 vrr_active = amdgpu_dm_vrr_active_irq(amdgpu_crtc);
404 /* Fixed refresh rate, or VRR scanout position outside front-porch? */
406 !dc_stream_get_scanoutpos(amdgpu_crtc->dm_irq_params.stream, &v_blank_start,
407 &v_blank_end, &hpos, &vpos) ||
408 (vpos < v_blank_start)) {
409 /* Update to correct count and vblank timestamp if racing with
410 * vblank irq. This also updates to the correct vblank timestamp
411 * even in VRR mode, as scanout is past the front-porch atm.
413 drm_crtc_accurate_vblank_count(&amdgpu_crtc->base);
415 /* Wake up userspace by sending the pageflip event with proper
416 * count and timestamp of vblank of flip completion.
419 drm_crtc_send_vblank_event(&amdgpu_crtc->base, e);
421 /* Event sent, so done with vblank for this flip */
422 drm_crtc_vblank_put(&amdgpu_crtc->base);
425 /* VRR active and inside front-porch: vblank count and
426 * timestamp for pageflip event will only be up to date after
427 * drm_crtc_handle_vblank() has been executed from late vblank
428 * irq handler after start of back-porch (vline 0). We queue the
429 * pageflip event for send-out by drm_crtc_handle_vblank() with
430 * updated timestamp and count, once it runs after us.
432 * We need to open-code this instead of using the helper
433 * drm_crtc_arm_vblank_event(), as that helper would
434 * call drm_crtc_accurate_vblank_count(), which we must
435 * not call in VRR mode while we are in front-porch!
438 /* sequence will be replaced by real count during send-out. */
439 e->sequence = drm_crtc_vblank_count(&amdgpu_crtc->base);
440 e->pipe = amdgpu_crtc->crtc_id;
442 list_add_tail(&e->base.link, &adev_to_drm(adev)->vblank_event_list);
446 /* Keep track of vblank of this flip for flip throttling. We use the
447 * cooked hw counter, as that one incremented at start of this vblank
448 * of pageflip completion, so last_flip_vblank is the forbidden count
449 * for queueing new pageflips if vsync + VRR is enabled.
451 amdgpu_crtc->dm_irq_params.last_flip_vblank =
452 amdgpu_get_vblank_counter_kms(&amdgpu_crtc->base);
454 amdgpu_crtc->pflip_status = AMDGPU_FLIP_NONE;
455 spin_unlock_irqrestore(&adev_to_drm(adev)->event_lock, flags);
457 DC_LOG_PFLIP("crtc:%d[%p], pflip_stat:AMDGPU_FLIP_NONE, vrr[%d]-fp %d\n",
458 amdgpu_crtc->crtc_id, amdgpu_crtc,
459 vrr_active, (int) !e);
462 static void dm_vupdate_high_irq(void *interrupt_params)
464 struct common_irq_params *irq_params = interrupt_params;
465 struct amdgpu_device *adev = irq_params->adev;
466 struct amdgpu_crtc *acrtc;
467 struct drm_device *drm_dev;
468 struct drm_vblank_crtc *vblank;
469 ktime_t frame_duration_ns, previous_timestamp;
473 acrtc = get_crtc_by_otg_inst(adev, irq_params->irq_src - IRQ_TYPE_VUPDATE);
476 vrr_active = amdgpu_dm_vrr_active_irq(acrtc);
477 drm_dev = acrtc->base.dev;
478 vblank = &drm_dev->vblank[acrtc->base.index];
479 previous_timestamp = atomic64_read(&irq_params->previous_timestamp);
480 frame_duration_ns = vblank->time - previous_timestamp;
482 if (frame_duration_ns > 0) {
483 trace_amdgpu_refresh_rate_track(acrtc->base.index,
485 ktime_divns(NSEC_PER_SEC, frame_duration_ns));
486 atomic64_set(&irq_params->previous_timestamp, vblank->time);
489 DC_LOG_VBLANK("crtc:%d, vupdate-vrr:%d\n",
493 /* Core vblank handling is done here after end of front-porch in
494 * vrr mode, as vblank timestamping will give valid results
495 * while now done after front-porch. This will also deliver
496 * page-flip completion events that have been queued to us
497 * if a pageflip happened inside front-porch.
500 drm_crtc_handle_vblank(&acrtc->base);
502 /* BTR processing for pre-DCE12 ASICs */
503 if (acrtc->dm_irq_params.stream &&
504 adev->family < AMDGPU_FAMILY_AI) {
505 spin_lock_irqsave(&adev_to_drm(adev)->event_lock, flags);
506 mod_freesync_handle_v_update(
507 adev->dm.freesync_module,
508 acrtc->dm_irq_params.stream,
509 &acrtc->dm_irq_params.vrr_params);
511 dc_stream_adjust_vmin_vmax(
513 acrtc->dm_irq_params.stream,
514 &acrtc->dm_irq_params.vrr_params.adjust);
515 spin_unlock_irqrestore(&adev_to_drm(adev)->event_lock, flags);
522 * dm_crtc_high_irq() - Handles CRTC interrupt
523 * @interrupt_params: used for determining the CRTC instance
525 * Handles the CRTC/VSYNC interrupt by notfying DRM's VBLANK
528 static void dm_crtc_high_irq(void *interrupt_params)
530 struct common_irq_params *irq_params = interrupt_params;
531 struct amdgpu_device *adev = irq_params->adev;
532 struct amdgpu_crtc *acrtc;
536 acrtc = get_crtc_by_otg_inst(adev, irq_params->irq_src - IRQ_TYPE_VBLANK);
540 vrr_active = amdgpu_dm_vrr_active_irq(acrtc);
542 DC_LOG_VBLANK("crtc:%d, vupdate-vrr:%d, planes:%d\n", acrtc->crtc_id,
543 vrr_active, acrtc->dm_irq_params.active_planes);
546 * Core vblank handling at start of front-porch is only possible
547 * in non-vrr mode, as only there vblank timestamping will give
548 * valid results while done in front-porch. Otherwise defer it
549 * to dm_vupdate_high_irq after end of front-porch.
552 drm_crtc_handle_vblank(&acrtc->base);
555 * Following stuff must happen at start of vblank, for crc
556 * computation and below-the-range btr support in vrr mode.
558 amdgpu_dm_crtc_handle_crc_irq(&acrtc->base);
560 /* BTR updates need to happen before VUPDATE on Vega and above. */
561 if (adev->family < AMDGPU_FAMILY_AI)
564 spin_lock_irqsave(&adev_to_drm(adev)->event_lock, flags);
566 if (acrtc->dm_irq_params.stream &&
567 acrtc->dm_irq_params.vrr_params.supported &&
568 acrtc->dm_irq_params.freesync_config.state ==
569 VRR_STATE_ACTIVE_VARIABLE) {
570 mod_freesync_handle_v_update(adev->dm.freesync_module,
571 acrtc->dm_irq_params.stream,
572 &acrtc->dm_irq_params.vrr_params);
574 dc_stream_adjust_vmin_vmax(adev->dm.dc, acrtc->dm_irq_params.stream,
575 &acrtc->dm_irq_params.vrr_params.adjust);
579 * If there aren't any active_planes then DCH HUBP may be clock-gated.
580 * In that case, pageflip completion interrupts won't fire and pageflip
581 * completion events won't get delivered. Prevent this by sending
582 * pending pageflip events from here if a flip is still pending.
584 * If any planes are enabled, use dm_pflip_high_irq() instead, to
585 * avoid race conditions between flip programming and completion,
586 * which could cause too early flip completion events.
588 if (adev->family >= AMDGPU_FAMILY_RV &&
589 acrtc->pflip_status == AMDGPU_FLIP_SUBMITTED &&
590 acrtc->dm_irq_params.active_planes == 0) {
592 drm_crtc_send_vblank_event(&acrtc->base, acrtc->event);
594 drm_crtc_vblank_put(&acrtc->base);
596 acrtc->pflip_status = AMDGPU_FLIP_NONE;
599 spin_unlock_irqrestore(&adev_to_drm(adev)->event_lock, flags);
602 #if defined(CONFIG_DRM_AMD_DC_DCN)
603 #if defined(CONFIG_DRM_AMD_SECURE_DISPLAY)
605 * dm_dcn_vertical_interrupt0_high_irq() - Handles OTG Vertical interrupt0 for
606 * DCN generation ASICs
607 * @interrupt_params: interrupt parameters
609 * Used to set crc window/read out crc value at vertical line 0 position
611 static void dm_dcn_vertical_interrupt0_high_irq(void *interrupt_params)
613 struct common_irq_params *irq_params = interrupt_params;
614 struct amdgpu_device *adev = irq_params->adev;
615 struct amdgpu_crtc *acrtc;
617 acrtc = get_crtc_by_otg_inst(adev, irq_params->irq_src - IRQ_TYPE_VLINE0);
622 amdgpu_dm_crtc_handle_crc_window_irq(&acrtc->base);
624 #endif /* CONFIG_DRM_AMD_SECURE_DISPLAY */
627 * dmub_aux_setconfig_callback - Callback for AUX or SET_CONFIG command.
628 * @adev: amdgpu_device pointer
629 * @notify: dmub notification structure
631 * Dmub AUX or SET_CONFIG command completion processing callback
632 * Copies dmub notification to DM which is to be read by AUX command.
633 * issuing thread and also signals the event to wake up the thread.
635 static void dmub_aux_setconfig_callback(struct amdgpu_device *adev,
636 struct dmub_notification *notify)
638 if (adev->dm.dmub_notify)
639 memcpy(adev->dm.dmub_notify, notify, sizeof(struct dmub_notification));
640 if (notify->type == DMUB_NOTIFICATION_AUX_REPLY)
641 complete(&adev->dm.dmub_aux_transfer_done);
645 * dmub_hpd_callback - DMUB HPD interrupt processing callback.
646 * @adev: amdgpu_device pointer
647 * @notify: dmub notification structure
649 * Dmub Hpd interrupt processing callback. Gets displayindex through the
650 * ink index and calls helper to do the processing.
652 static void dmub_hpd_callback(struct amdgpu_device *adev,
653 struct dmub_notification *notify)
655 struct amdgpu_dm_connector *aconnector;
656 struct amdgpu_dm_connector *hpd_aconnector = NULL;
657 struct drm_connector *connector;
658 struct drm_connector_list_iter iter;
659 struct dc_link *link;
660 uint8_t link_index = 0;
661 struct drm_device *dev;
666 if (notify == NULL) {
667 DRM_ERROR("DMUB HPD callback notification was NULL");
671 if (notify->link_index > adev->dm.dc->link_count) {
672 DRM_ERROR("DMUB HPD index (%u)is abnormal", notify->link_index);
676 link_index = notify->link_index;
677 link = adev->dm.dc->links[link_index];
680 drm_connector_list_iter_begin(dev, &iter);
681 drm_for_each_connector_iter(connector, &iter) {
682 aconnector = to_amdgpu_dm_connector(connector);
683 if (link && aconnector->dc_link == link) {
684 DRM_INFO("DMUB HPD callback: link_index=%u\n", link_index);
685 hpd_aconnector = aconnector;
689 drm_connector_list_iter_end(&iter);
691 if (hpd_aconnector) {
692 if (notify->type == DMUB_NOTIFICATION_HPD)
693 handle_hpd_irq_helper(hpd_aconnector);
694 else if (notify->type == DMUB_NOTIFICATION_HPD_IRQ)
695 handle_hpd_rx_irq(hpd_aconnector);
700 * register_dmub_notify_callback - Sets callback for DMUB notify
701 * @adev: amdgpu_device pointer
702 * @type: Type of dmub notification
703 * @callback: Dmub interrupt callback function
704 * @dmub_int_thread_offload: offload indicator
706 * API to register a dmub callback handler for a dmub notification
707 * Also sets indicator whether callback processing to be offloaded.
708 * to dmub interrupt handling thread
709 * Return: true if successfully registered, false if there is existing registration
711 static bool register_dmub_notify_callback(struct amdgpu_device *adev,
712 enum dmub_notification_type type,
713 dmub_notify_interrupt_callback_t callback,
714 bool dmub_int_thread_offload)
716 if (callback != NULL && type < ARRAY_SIZE(adev->dm.dmub_thread_offload)) {
717 adev->dm.dmub_callback[type] = callback;
718 adev->dm.dmub_thread_offload[type] = dmub_int_thread_offload;
725 static void dm_handle_hpd_work(struct work_struct *work)
727 struct dmub_hpd_work *dmub_hpd_wrk;
729 dmub_hpd_wrk = container_of(work, struct dmub_hpd_work, handle_hpd_work);
731 if (!dmub_hpd_wrk->dmub_notify) {
732 DRM_ERROR("dmub_hpd_wrk dmub_notify is NULL");
736 if (dmub_hpd_wrk->dmub_notify->type < ARRAY_SIZE(dmub_hpd_wrk->adev->dm.dmub_callback)) {
737 dmub_hpd_wrk->adev->dm.dmub_callback[dmub_hpd_wrk->dmub_notify->type](dmub_hpd_wrk->adev,
738 dmub_hpd_wrk->dmub_notify);
741 kfree(dmub_hpd_wrk->dmub_notify);
746 #define DMUB_TRACE_MAX_READ 64
748 * dm_dmub_outbox1_low_irq() - Handles Outbox interrupt
749 * @interrupt_params: used for determining the Outbox instance
751 * Handles the Outbox Interrupt
754 static void dm_dmub_outbox1_low_irq(void *interrupt_params)
756 struct dmub_notification notify;
757 struct common_irq_params *irq_params = interrupt_params;
758 struct amdgpu_device *adev = irq_params->adev;
759 struct amdgpu_display_manager *dm = &adev->dm;
760 struct dmcub_trace_buf_entry entry = { 0 };
762 struct dmub_hpd_work *dmub_hpd_wrk;
763 struct dc_link *plink = NULL;
765 if (dc_enable_dmub_notifications(adev->dm.dc) &&
766 irq_params->irq_src == DC_IRQ_SOURCE_DMCUB_OUTBOX) {
769 dc_stat_get_dmub_notification(adev->dm.dc, ¬ify);
770 if (notify.type > ARRAY_SIZE(dm->dmub_thread_offload)) {
771 DRM_ERROR("DM: notify type %d invalid!", notify.type);
774 if (!dm->dmub_callback[notify.type]) {
775 DRM_DEBUG_DRIVER("DMUB notification skipped, no handler: type=%d\n", notify.type);
778 if (dm->dmub_thread_offload[notify.type] == true) {
779 dmub_hpd_wrk = kzalloc(sizeof(*dmub_hpd_wrk), GFP_ATOMIC);
781 DRM_ERROR("Failed to allocate dmub_hpd_wrk");
784 dmub_hpd_wrk->dmub_notify = kzalloc(sizeof(struct dmub_notification), GFP_ATOMIC);
785 if (!dmub_hpd_wrk->dmub_notify) {
787 DRM_ERROR("Failed to allocate dmub_hpd_wrk->dmub_notify");
790 INIT_WORK(&dmub_hpd_wrk->handle_hpd_work, dm_handle_hpd_work);
791 if (dmub_hpd_wrk->dmub_notify)
792 memcpy(dmub_hpd_wrk->dmub_notify, ¬ify, sizeof(struct dmub_notification));
793 dmub_hpd_wrk->adev = adev;
794 if (notify.type == DMUB_NOTIFICATION_HPD) {
795 plink = adev->dm.dc->links[notify.link_index];
798 notify.hpd_status == DP_HPD_PLUG;
801 queue_work(adev->dm.delayed_hpd_wq, &dmub_hpd_wrk->handle_hpd_work);
803 dm->dmub_callback[notify.type](adev, ¬ify);
805 } while (notify.pending_notification);
810 if (dc_dmub_srv_get_dmub_outbox0_msg(dm->dc, &entry)) {
811 trace_amdgpu_dmub_trace_high_irq(entry.trace_code, entry.tick_count,
812 entry.param0, entry.param1);
814 DRM_DEBUG_DRIVER("trace_code:%u, tick_count:%u, param0:%u, param1:%u\n",
815 entry.trace_code, entry.tick_count, entry.param0, entry.param1);
821 } while (count <= DMUB_TRACE_MAX_READ);
823 if (count > DMUB_TRACE_MAX_READ)
824 DRM_DEBUG_DRIVER("Warning : count > DMUB_TRACE_MAX_READ");
826 #endif /* CONFIG_DRM_AMD_DC_DCN */
828 static int dm_set_clockgating_state(void *handle,
829 enum amd_clockgating_state state)
834 static int dm_set_powergating_state(void *handle,
835 enum amd_powergating_state state)
840 /* Prototypes of private functions */
841 static int dm_early_init(void* handle);
843 /* Allocate memory for FBC compressed data */
844 static void amdgpu_dm_fbc_init(struct drm_connector *connector)
846 struct drm_device *dev = connector->dev;
847 struct amdgpu_device *adev = drm_to_adev(dev);
848 struct dm_compressor_info *compressor = &adev->dm.compressor;
849 struct amdgpu_dm_connector *aconn = to_amdgpu_dm_connector(connector);
850 struct drm_display_mode *mode;
851 unsigned long max_size = 0;
853 if (adev->dm.dc->fbc_compressor == NULL)
856 if (aconn->dc_link->connector_signal != SIGNAL_TYPE_EDP)
859 if (compressor->bo_ptr)
863 list_for_each_entry(mode, &connector->modes, head) {
864 if (max_size < mode->htotal * mode->vtotal)
865 max_size = mode->htotal * mode->vtotal;
869 int r = amdgpu_bo_create_kernel(adev, max_size * 4, PAGE_SIZE,
870 AMDGPU_GEM_DOMAIN_GTT, &compressor->bo_ptr,
871 &compressor->gpu_addr, &compressor->cpu_addr);
874 DRM_ERROR("DM: Failed to initialize FBC\n");
876 adev->dm.dc->ctx->fbc_gpu_addr = compressor->gpu_addr;
877 DRM_INFO("DM: FBC alloc %lu\n", max_size*4);
884 static int amdgpu_dm_audio_component_get_eld(struct device *kdev, int port,
885 int pipe, bool *enabled,
886 unsigned char *buf, int max_bytes)
888 struct drm_device *dev = dev_get_drvdata(kdev);
889 struct amdgpu_device *adev = drm_to_adev(dev);
890 struct drm_connector *connector;
891 struct drm_connector_list_iter conn_iter;
892 struct amdgpu_dm_connector *aconnector;
897 mutex_lock(&adev->dm.audio_lock);
899 drm_connector_list_iter_begin(dev, &conn_iter);
900 drm_for_each_connector_iter(connector, &conn_iter) {
901 aconnector = to_amdgpu_dm_connector(connector);
902 if (aconnector->audio_inst != port)
906 ret = drm_eld_size(connector->eld);
907 memcpy(buf, connector->eld, min(max_bytes, ret));
911 drm_connector_list_iter_end(&conn_iter);
913 mutex_unlock(&adev->dm.audio_lock);
915 DRM_DEBUG_KMS("Get ELD : idx=%d ret=%d en=%d\n", port, ret, *enabled);
920 static const struct drm_audio_component_ops amdgpu_dm_audio_component_ops = {
921 .get_eld = amdgpu_dm_audio_component_get_eld,
924 static int amdgpu_dm_audio_component_bind(struct device *kdev,
925 struct device *hda_kdev, void *data)
927 struct drm_device *dev = dev_get_drvdata(kdev);
928 struct amdgpu_device *adev = drm_to_adev(dev);
929 struct drm_audio_component *acomp = data;
931 acomp->ops = &amdgpu_dm_audio_component_ops;
933 adev->dm.audio_component = acomp;
938 static void amdgpu_dm_audio_component_unbind(struct device *kdev,
939 struct device *hda_kdev, void *data)
941 struct drm_device *dev = dev_get_drvdata(kdev);
942 struct amdgpu_device *adev = drm_to_adev(dev);
943 struct drm_audio_component *acomp = data;
947 adev->dm.audio_component = NULL;
950 static const struct component_ops amdgpu_dm_audio_component_bind_ops = {
951 .bind = amdgpu_dm_audio_component_bind,
952 .unbind = amdgpu_dm_audio_component_unbind,
955 static int amdgpu_dm_audio_init(struct amdgpu_device *adev)
962 adev->mode_info.audio.enabled = true;
964 adev->mode_info.audio.num_pins = adev->dm.dc->res_pool->audio_count;
966 for (i = 0; i < adev->mode_info.audio.num_pins; i++) {
967 adev->mode_info.audio.pin[i].channels = -1;
968 adev->mode_info.audio.pin[i].rate = -1;
969 adev->mode_info.audio.pin[i].bits_per_sample = -1;
970 adev->mode_info.audio.pin[i].status_bits = 0;
971 adev->mode_info.audio.pin[i].category_code = 0;
972 adev->mode_info.audio.pin[i].connected = false;
973 adev->mode_info.audio.pin[i].id =
974 adev->dm.dc->res_pool->audios[i]->inst;
975 adev->mode_info.audio.pin[i].offset = 0;
978 ret = component_add(adev->dev, &amdgpu_dm_audio_component_bind_ops);
982 adev->dm.audio_registered = true;
987 static void amdgpu_dm_audio_fini(struct amdgpu_device *adev)
992 if (!adev->mode_info.audio.enabled)
995 if (adev->dm.audio_registered) {
996 component_del(adev->dev, &amdgpu_dm_audio_component_bind_ops);
997 adev->dm.audio_registered = false;
1000 /* TODO: Disable audio? */
1002 adev->mode_info.audio.enabled = false;
1005 static void amdgpu_dm_audio_eld_notify(struct amdgpu_device *adev, int pin)
1007 struct drm_audio_component *acomp = adev->dm.audio_component;
1009 if (acomp && acomp->audio_ops && acomp->audio_ops->pin_eld_notify) {
1010 DRM_DEBUG_KMS("Notify ELD: %d\n", pin);
1012 acomp->audio_ops->pin_eld_notify(acomp->audio_ops->audio_ptr,
1017 static int dm_dmub_hw_init(struct amdgpu_device *adev)
1019 const struct dmcub_firmware_header_v1_0 *hdr;
1020 struct dmub_srv *dmub_srv = adev->dm.dmub_srv;
1021 struct dmub_srv_fb_info *fb_info = adev->dm.dmub_fb_info;
1022 const struct firmware *dmub_fw = adev->dm.dmub_fw;
1023 struct dmcu *dmcu = adev->dm.dc->res_pool->dmcu;
1024 struct abm *abm = adev->dm.dc->res_pool->abm;
1025 struct dmub_srv_hw_params hw_params;
1026 enum dmub_status status;
1027 const unsigned char *fw_inst_const, *fw_bss_data;
1028 uint32_t i, fw_inst_const_size, fw_bss_data_size;
1029 bool has_hw_support;
1032 /* DMUB isn't supported on the ASIC. */
1036 DRM_ERROR("No framebuffer info for DMUB service.\n");
1041 /* Firmware required for DMUB support. */
1042 DRM_ERROR("No firmware provided for DMUB.\n");
1046 status = dmub_srv_has_hw_support(dmub_srv, &has_hw_support);
1047 if (status != DMUB_STATUS_OK) {
1048 DRM_ERROR("Error checking HW support for DMUB: %d\n", status);
1052 if (!has_hw_support) {
1053 DRM_INFO("DMUB unsupported on ASIC\n");
1057 /* Reset DMCUB if it was previously running - before we overwrite its memory. */
1058 status = dmub_srv_hw_reset(dmub_srv);
1059 if (status != DMUB_STATUS_OK)
1060 DRM_WARN("Error resetting DMUB HW: %d\n", status);
1062 hdr = (const struct dmcub_firmware_header_v1_0 *)dmub_fw->data;
1064 fw_inst_const = dmub_fw->data +
1065 le32_to_cpu(hdr->header.ucode_array_offset_bytes) +
1068 fw_bss_data = dmub_fw->data +
1069 le32_to_cpu(hdr->header.ucode_array_offset_bytes) +
1070 le32_to_cpu(hdr->inst_const_bytes);
1072 /* Copy firmware and bios info into FB memory. */
1073 fw_inst_const_size = le32_to_cpu(hdr->inst_const_bytes) -
1074 PSP_HEADER_BYTES - PSP_FOOTER_BYTES;
1076 fw_bss_data_size = le32_to_cpu(hdr->bss_data_bytes);
1078 /* if adev->firmware.load_type == AMDGPU_FW_LOAD_PSP,
1079 * amdgpu_ucode_init_single_fw will load dmub firmware
1080 * fw_inst_const part to cw0; otherwise, the firmware back door load
1081 * will be done by dm_dmub_hw_init
1083 if (adev->firmware.load_type != AMDGPU_FW_LOAD_PSP) {
1084 memcpy(fb_info->fb[DMUB_WINDOW_0_INST_CONST].cpu_addr, fw_inst_const,
1085 fw_inst_const_size);
1088 if (fw_bss_data_size)
1089 memcpy(fb_info->fb[DMUB_WINDOW_2_BSS_DATA].cpu_addr,
1090 fw_bss_data, fw_bss_data_size);
1092 /* Copy firmware bios info into FB memory. */
1093 memcpy(fb_info->fb[DMUB_WINDOW_3_VBIOS].cpu_addr, adev->bios,
1096 /* Reset regions that need to be reset. */
1097 memset(fb_info->fb[DMUB_WINDOW_4_MAILBOX].cpu_addr, 0,
1098 fb_info->fb[DMUB_WINDOW_4_MAILBOX].size);
1100 memset(fb_info->fb[DMUB_WINDOW_5_TRACEBUFF].cpu_addr, 0,
1101 fb_info->fb[DMUB_WINDOW_5_TRACEBUFF].size);
1103 memset(fb_info->fb[DMUB_WINDOW_6_FW_STATE].cpu_addr, 0,
1104 fb_info->fb[DMUB_WINDOW_6_FW_STATE].size);
1106 /* Initialize hardware. */
1107 memset(&hw_params, 0, sizeof(hw_params));
1108 hw_params.fb_base = adev->gmc.fb_start;
1109 hw_params.fb_offset = adev->gmc.aper_base;
1111 /* backdoor load firmware and trigger dmub running */
1112 if (adev->firmware.load_type != AMDGPU_FW_LOAD_PSP)
1113 hw_params.load_inst_const = true;
1116 hw_params.psp_version = dmcu->psp_version;
1118 for (i = 0; i < fb_info->num_fb; ++i)
1119 hw_params.fb[i] = &fb_info->fb[i];
1121 switch (adev->ip_versions[DCE_HWIP][0]) {
1122 case IP_VERSION(3, 1, 3): /* Only for this asic hw internal rev B0 */
1123 hw_params.dpia_supported = true;
1124 #if defined(CONFIG_DRM_AMD_DC_DCN)
1125 hw_params.disable_dpia = adev->dm.dc->debug.dpia_debug.bits.disable_dpia;
1132 status = dmub_srv_hw_init(dmub_srv, &hw_params);
1133 if (status != DMUB_STATUS_OK) {
1134 DRM_ERROR("Error initializing DMUB HW: %d\n", status);
1138 /* Wait for firmware load to finish. */
1139 status = dmub_srv_wait_for_auto_load(dmub_srv, 100000);
1140 if (status != DMUB_STATUS_OK)
1141 DRM_WARN("Wait for DMUB auto-load failed: %d\n", status);
1143 /* Init DMCU and ABM if available. */
1145 dmcu->funcs->dmcu_init(dmcu);
1146 abm->dmcu_is_running = dmcu->funcs->is_dmcu_initialized(dmcu);
1149 if (!adev->dm.dc->ctx->dmub_srv)
1150 adev->dm.dc->ctx->dmub_srv = dc_dmub_srv_create(adev->dm.dc, dmub_srv);
1151 if (!adev->dm.dc->ctx->dmub_srv) {
1152 DRM_ERROR("Couldn't allocate DC DMUB server!\n");
1156 DRM_INFO("DMUB hardware initialized: version=0x%08X\n",
1157 adev->dm.dmcub_fw_version);
1162 static void dm_dmub_hw_resume(struct amdgpu_device *adev)
1164 struct dmub_srv *dmub_srv = adev->dm.dmub_srv;
1165 enum dmub_status status;
1169 /* DMUB isn't supported on the ASIC. */
1173 status = dmub_srv_is_hw_init(dmub_srv, &init);
1174 if (status != DMUB_STATUS_OK)
1175 DRM_WARN("DMUB hardware init check failed: %d\n", status);
1177 if (status == DMUB_STATUS_OK && init) {
1178 /* Wait for firmware load to finish. */
1179 status = dmub_srv_wait_for_auto_load(dmub_srv, 100000);
1180 if (status != DMUB_STATUS_OK)
1181 DRM_WARN("Wait for DMUB auto-load failed: %d\n", status);
1183 /* Perform the full hardware initialization. */
1184 dm_dmub_hw_init(adev);
1188 #if defined(CONFIG_DRM_AMD_DC_DCN)
1189 static void mmhub_read_system_context(struct amdgpu_device *adev, struct dc_phy_addr_space_config *pa_config)
1192 uint32_t logical_addr_low;
1193 uint32_t logical_addr_high;
1194 uint32_t agp_base, agp_bot, agp_top;
1195 PHYSICAL_ADDRESS_LOC page_table_start, page_table_end, page_table_base;
1197 memset(pa_config, 0, sizeof(*pa_config));
1199 logical_addr_low = min(adev->gmc.fb_start, adev->gmc.agp_start) >> 18;
1200 pt_base = amdgpu_gmc_pd_addr(adev->gart.bo);
1202 if (adev->apu_flags & AMD_APU_IS_RAVEN2)
1204 * Raven2 has a HW issue that it is unable to use the vram which
1205 * is out of MC_VM_SYSTEM_APERTURE_HIGH_ADDR. So here is the
1206 * workaround that increase system aperture high address (add 1)
1207 * to get rid of the VM fault and hardware hang.
1209 logical_addr_high = max((adev->gmc.fb_end >> 18) + 0x1, adev->gmc.agp_end >> 18);
1211 logical_addr_high = max(adev->gmc.fb_end, adev->gmc.agp_end) >> 18;
1214 agp_bot = adev->gmc.agp_start >> 24;
1215 agp_top = adev->gmc.agp_end >> 24;
1218 page_table_start.high_part = (u32)(adev->gmc.gart_start >> 44) & 0xF;
1219 page_table_start.low_part = (u32)(adev->gmc.gart_start >> 12);
1220 page_table_end.high_part = (u32)(adev->gmc.gart_end >> 44) & 0xF;
1221 page_table_end.low_part = (u32)(adev->gmc.gart_end >> 12);
1222 page_table_base.high_part = upper_32_bits(pt_base) & 0xF;
1223 page_table_base.low_part = lower_32_bits(pt_base);
1225 pa_config->system_aperture.start_addr = (uint64_t)logical_addr_low << 18;
1226 pa_config->system_aperture.end_addr = (uint64_t)logical_addr_high << 18;
1228 pa_config->system_aperture.agp_base = (uint64_t)agp_base << 24 ;
1229 pa_config->system_aperture.agp_bot = (uint64_t)agp_bot << 24;
1230 pa_config->system_aperture.agp_top = (uint64_t)agp_top << 24;
1232 pa_config->system_aperture.fb_base = adev->gmc.fb_start;
1233 pa_config->system_aperture.fb_offset = adev->gmc.aper_base;
1234 pa_config->system_aperture.fb_top = adev->gmc.fb_end;
1236 pa_config->gart_config.page_table_start_addr = page_table_start.quad_part << 12;
1237 pa_config->gart_config.page_table_end_addr = page_table_end.quad_part << 12;
1238 pa_config->gart_config.page_table_base_addr = page_table_base.quad_part;
1240 pa_config->is_hvm_enabled = 0;
1244 #if defined(CONFIG_DRM_AMD_DC_DCN)
1245 static void vblank_control_worker(struct work_struct *work)
1247 struct vblank_control_work *vblank_work =
1248 container_of(work, struct vblank_control_work, work);
1249 struct amdgpu_display_manager *dm = vblank_work->dm;
1251 mutex_lock(&dm->dc_lock);
1253 if (vblank_work->enable)
1254 dm->active_vblank_irq_count++;
1255 else if(dm->active_vblank_irq_count)
1256 dm->active_vblank_irq_count--;
1258 dc_allow_idle_optimizations(dm->dc, dm->active_vblank_irq_count == 0);
1260 DRM_DEBUG_KMS("Allow idle optimizations (MALL): %d\n", dm->active_vblank_irq_count == 0);
1262 /* Control PSR based on vblank requirements from OS */
1263 if (vblank_work->stream && vblank_work->stream->link) {
1264 if (vblank_work->enable) {
1265 if (vblank_work->stream->link->psr_settings.psr_allow_active)
1266 amdgpu_dm_psr_disable(vblank_work->stream);
1267 } else if (vblank_work->stream->link->psr_settings.psr_feature_enabled &&
1268 !vblank_work->stream->link->psr_settings.psr_allow_active &&
1269 vblank_work->acrtc->dm_irq_params.allow_psr_entry) {
1270 amdgpu_dm_psr_enable(vblank_work->stream);
1274 mutex_unlock(&dm->dc_lock);
1276 dc_stream_release(vblank_work->stream);
1283 static void dm_handle_hpd_rx_offload_work(struct work_struct *work)
1285 struct hpd_rx_irq_offload_work *offload_work;
1286 struct amdgpu_dm_connector *aconnector;
1287 struct dc_link *dc_link;
1288 struct amdgpu_device *adev;
1289 enum dc_connection_type new_connection_type = dc_connection_none;
1290 unsigned long flags;
1292 offload_work = container_of(work, struct hpd_rx_irq_offload_work, work);
1293 aconnector = offload_work->offload_wq->aconnector;
1296 DRM_ERROR("Can't retrieve aconnector in hpd_rx_irq_offload_work");
1300 adev = drm_to_adev(aconnector->base.dev);
1301 dc_link = aconnector->dc_link;
1303 mutex_lock(&aconnector->hpd_lock);
1304 if (!dc_link_detect_sink(dc_link, &new_connection_type))
1305 DRM_ERROR("KMS: Failed to detect connector\n");
1306 mutex_unlock(&aconnector->hpd_lock);
1308 if (new_connection_type == dc_connection_none)
1311 if (amdgpu_in_reset(adev))
1314 mutex_lock(&adev->dm.dc_lock);
1315 if (offload_work->data.bytes.device_service_irq.bits.AUTOMATED_TEST)
1316 dc_link_dp_handle_automated_test(dc_link);
1317 else if ((dc_link->connector_signal != SIGNAL_TYPE_EDP) &&
1318 hpd_rx_irq_check_link_loss_status(dc_link, &offload_work->data) &&
1319 dc_link_dp_allow_hpd_rx_irq(dc_link)) {
1320 dc_link_dp_handle_link_loss(dc_link);
1321 spin_lock_irqsave(&offload_work->offload_wq->offload_lock, flags);
1322 offload_work->offload_wq->is_handling_link_loss = false;
1323 spin_unlock_irqrestore(&offload_work->offload_wq->offload_lock, flags);
1325 mutex_unlock(&adev->dm.dc_lock);
1328 kfree(offload_work);
1332 static struct hpd_rx_irq_offload_work_queue *hpd_rx_irq_create_workqueue(struct dc *dc)
1334 int max_caps = dc->caps.max_links;
1336 struct hpd_rx_irq_offload_work_queue *hpd_rx_offload_wq = NULL;
1338 hpd_rx_offload_wq = kcalloc(max_caps, sizeof(*hpd_rx_offload_wq), GFP_KERNEL);
1340 if (!hpd_rx_offload_wq)
1344 for (i = 0; i < max_caps; i++) {
1345 hpd_rx_offload_wq[i].wq =
1346 create_singlethread_workqueue("amdgpu_dm_hpd_rx_offload_wq");
1348 if (hpd_rx_offload_wq[i].wq == NULL) {
1349 DRM_ERROR("create amdgpu_dm_hpd_rx_offload_wq fail!");
1353 spin_lock_init(&hpd_rx_offload_wq[i].offload_lock);
1356 return hpd_rx_offload_wq;
1359 struct amdgpu_stutter_quirk {
1367 static const struct amdgpu_stutter_quirk amdgpu_stutter_quirk_list[] = {
1368 /* https://bugzilla.kernel.org/show_bug.cgi?id=214417 */
1369 { 0x1002, 0x15dd, 0x1002, 0x15dd, 0xc8 },
1373 static bool dm_should_disable_stutter(struct pci_dev *pdev)
1375 const struct amdgpu_stutter_quirk *p = amdgpu_stutter_quirk_list;
1377 while (p && p->chip_device != 0) {
1378 if (pdev->vendor == p->chip_vendor &&
1379 pdev->device == p->chip_device &&
1380 pdev->subsystem_vendor == p->subsys_vendor &&
1381 pdev->subsystem_device == p->subsys_device &&
1382 pdev->revision == p->revision) {
1390 static int amdgpu_dm_init(struct amdgpu_device *adev)
1392 struct dc_init_data init_data;
1393 #ifdef CONFIG_DRM_AMD_DC_HDCP
1394 struct dc_callback_init init_params;
1398 adev->dm.ddev = adev_to_drm(adev);
1399 adev->dm.adev = adev;
1401 /* Zero all the fields */
1402 memset(&init_data, 0, sizeof(init_data));
1403 #ifdef CONFIG_DRM_AMD_DC_HDCP
1404 memset(&init_params, 0, sizeof(init_params));
1407 mutex_init(&adev->dm.dc_lock);
1408 mutex_init(&adev->dm.audio_lock);
1409 #if defined(CONFIG_DRM_AMD_DC_DCN)
1410 spin_lock_init(&adev->dm.vblank_lock);
1413 if(amdgpu_dm_irq_init(adev)) {
1414 DRM_ERROR("amdgpu: failed to initialize DM IRQ support.\n");
1418 init_data.asic_id.chip_family = adev->family;
1420 init_data.asic_id.pci_revision_id = adev->pdev->revision;
1421 init_data.asic_id.hw_internal_rev = adev->external_rev_id;
1422 init_data.asic_id.chip_id = adev->pdev->device;
1424 init_data.asic_id.vram_width = adev->gmc.vram_width;
1425 /* TODO: initialize init_data.asic_id.vram_type here!!!! */
1426 init_data.asic_id.atombios_base_address =
1427 adev->mode_info.atom_context->bios;
1429 init_data.driver = adev;
1431 adev->dm.cgs_device = amdgpu_cgs_create_device(adev);
1433 if (!adev->dm.cgs_device) {
1434 DRM_ERROR("amdgpu: failed to create cgs device.\n");
1438 init_data.cgs_device = adev->dm.cgs_device;
1440 init_data.dce_environment = DCE_ENV_PRODUCTION_DRV;
1442 switch (adev->asic_type) {
1445 init_data.flags.gpu_vm_support = true;
1448 switch (adev->ip_versions[DCE_HWIP][0]) {
1449 case IP_VERSION(2, 1, 0):
1450 init_data.flags.gpu_vm_support = true;
1451 switch (adev->dm.dmcub_fw_version) {
1452 case 0: /* development */
1453 case 0x1: /* linux-firmware.git hash 6d9f399 */
1454 case 0x01000000: /* linux-firmware.git hash 9a0b0f4 */
1455 init_data.flags.disable_dmcu = false;
1458 init_data.flags.disable_dmcu = true;
1461 case IP_VERSION(1, 0, 0):
1462 case IP_VERSION(1, 0, 1):
1463 case IP_VERSION(3, 0, 1):
1464 case IP_VERSION(3, 1, 2):
1465 case IP_VERSION(3, 1, 3):
1466 init_data.flags.gpu_vm_support = true;
1468 case IP_VERSION(2, 0, 3):
1469 init_data.flags.disable_dmcu = true;
1477 if (amdgpu_dc_feature_mask & DC_FBC_MASK)
1478 init_data.flags.fbc_support = true;
1480 if (amdgpu_dc_feature_mask & DC_MULTI_MON_PP_MCLK_SWITCH_MASK)
1481 init_data.flags.multi_mon_pp_mclk_switch = true;
1483 if (amdgpu_dc_feature_mask & DC_DISABLE_FRACTIONAL_PWM_MASK)
1484 init_data.flags.disable_fractional_pwm = true;
1486 if (amdgpu_dc_feature_mask & DC_EDP_NO_POWER_SEQUENCING)
1487 init_data.flags.edp_no_power_sequencing = true;
1489 #ifdef CONFIG_DRM_AMD_DC_DCN
1490 if (amdgpu_dc_feature_mask & DC_DISABLE_LTTPR_DP1_4A)
1491 init_data.flags.allow_lttpr_non_transparent_mode.bits.DP1_4A = true;
1492 if (amdgpu_dc_feature_mask & DC_DISABLE_LTTPR_DP2_0)
1493 init_data.flags.allow_lttpr_non_transparent_mode.bits.DP2_0 = true;
1496 init_data.flags.seamless_boot_edp_requested = false;
1498 if (check_seamless_boot_capability(adev)) {
1499 init_data.flags.seamless_boot_edp_requested = true;
1500 init_data.flags.allow_seamless_boot_optimization = true;
1501 DRM_INFO("Seamless boot condition check passed\n");
1504 INIT_LIST_HEAD(&adev->dm.da_list);
1505 /* Display Core create. */
1506 adev->dm.dc = dc_create(&init_data);
1509 DRM_INFO("Display Core initialized with v%s!\n", DC_VER);
1511 DRM_INFO("Display Core failed to initialize with v%s!\n", DC_VER);
1515 if (amdgpu_dc_debug_mask & DC_DISABLE_PIPE_SPLIT) {
1516 adev->dm.dc->debug.force_single_disp_pipe_split = false;
1517 adev->dm.dc->debug.pipe_split_policy = MPC_SPLIT_AVOID;
1520 if (adev->asic_type != CHIP_CARRIZO && adev->asic_type != CHIP_STONEY)
1521 adev->dm.dc->debug.disable_stutter = amdgpu_pp_feature_mask & PP_STUTTER_MODE ? false : true;
1522 if (dm_should_disable_stutter(adev->pdev))
1523 adev->dm.dc->debug.disable_stutter = true;
1525 if (amdgpu_dc_debug_mask & DC_DISABLE_STUTTER)
1526 adev->dm.dc->debug.disable_stutter = true;
1528 if (amdgpu_dc_debug_mask & DC_DISABLE_DSC) {
1529 adev->dm.dc->debug.disable_dsc = true;
1530 adev->dm.dc->debug.disable_dsc_edp = true;
1533 if (amdgpu_dc_debug_mask & DC_DISABLE_CLOCK_GATING)
1534 adev->dm.dc->debug.disable_clock_gate = true;
1536 r = dm_dmub_hw_init(adev);
1538 DRM_ERROR("DMUB interface failed to initialize: status=%d\n", r);
1542 dc_hardware_init(adev->dm.dc);
1544 adev->dm.hpd_rx_offload_wq = hpd_rx_irq_create_workqueue(adev->dm.dc);
1545 if (!adev->dm.hpd_rx_offload_wq) {
1546 DRM_ERROR("amdgpu: failed to create hpd rx offload workqueue.\n");
1550 #if defined(CONFIG_DRM_AMD_DC_DCN)
1551 if ((adev->flags & AMD_IS_APU) && (adev->asic_type >= CHIP_CARRIZO)) {
1552 struct dc_phy_addr_space_config pa_config;
1554 mmhub_read_system_context(adev, &pa_config);
1556 // Call the DC init_memory func
1557 dc_setup_system_context(adev->dm.dc, &pa_config);
1561 adev->dm.freesync_module = mod_freesync_create(adev->dm.dc);
1562 if (!adev->dm.freesync_module) {
1564 "amdgpu: failed to initialize freesync_module.\n");
1566 DRM_DEBUG_DRIVER("amdgpu: freesync_module init done %p.\n",
1567 adev->dm.freesync_module);
1569 amdgpu_dm_init_color_mod();
1571 #if defined(CONFIG_DRM_AMD_DC_DCN)
1572 if (adev->dm.dc->caps.max_links > 0) {
1573 adev->dm.vblank_control_workqueue =
1574 create_singlethread_workqueue("dm_vblank_control_workqueue");
1575 if (!adev->dm.vblank_control_workqueue)
1576 DRM_ERROR("amdgpu: failed to initialize vblank_workqueue.\n");
1580 #ifdef CONFIG_DRM_AMD_DC_HDCP
1581 if (adev->dm.dc->caps.max_links > 0 && adev->family >= AMDGPU_FAMILY_RV) {
1582 adev->dm.hdcp_workqueue = hdcp_create_workqueue(adev, &init_params.cp_psp, adev->dm.dc);
1584 if (!adev->dm.hdcp_workqueue)
1585 DRM_ERROR("amdgpu: failed to initialize hdcp_workqueue.\n");
1587 DRM_DEBUG_DRIVER("amdgpu: hdcp_workqueue init done %p.\n", adev->dm.hdcp_workqueue);
1589 dc_init_callbacks(adev->dm.dc, &init_params);
1592 #if defined(CONFIG_DRM_AMD_SECURE_DISPLAY)
1593 adev->dm.crc_rd_wrk = amdgpu_dm_crtc_secure_display_create_work();
1595 if (dc_enable_dmub_notifications(adev->dm.dc)) {
1596 init_completion(&adev->dm.dmub_aux_transfer_done);
1597 adev->dm.dmub_notify = kzalloc(sizeof(struct dmub_notification), GFP_KERNEL);
1598 if (!adev->dm.dmub_notify) {
1599 DRM_INFO("amdgpu: fail to allocate adev->dm.dmub_notify");
1603 adev->dm.delayed_hpd_wq = create_singlethread_workqueue("amdgpu_dm_hpd_wq");
1604 if (!adev->dm.delayed_hpd_wq) {
1605 DRM_ERROR("amdgpu: failed to create hpd offload workqueue.\n");
1609 amdgpu_dm_outbox_init(adev);
1610 #if defined(CONFIG_DRM_AMD_DC_DCN)
1611 if (!register_dmub_notify_callback(adev, DMUB_NOTIFICATION_AUX_REPLY,
1612 dmub_aux_setconfig_callback, false)) {
1613 DRM_ERROR("amdgpu: fail to register dmub aux callback");
1616 if (!register_dmub_notify_callback(adev, DMUB_NOTIFICATION_HPD, dmub_hpd_callback, true)) {
1617 DRM_ERROR("amdgpu: fail to register dmub hpd callback");
1620 if (!register_dmub_notify_callback(adev, DMUB_NOTIFICATION_HPD_IRQ, dmub_hpd_callback, true)) {
1621 DRM_ERROR("amdgpu: fail to register dmub hpd callback");
1624 #endif /* CONFIG_DRM_AMD_DC_DCN */
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 defined(CONFIG_DRM_AMD_DC_DCN)
1672 if (adev->dm.vblank_control_workqueue) {
1673 destroy_workqueue(adev->dm.vblank_control_workqueue);
1674 adev->dm.vblank_control_workqueue = NULL;
1678 for (i = 0; i < adev->dm.display_indexes_num; i++) {
1679 drm_encoder_cleanup(&adev->dm.mst_encoders[i].base);
1682 amdgpu_dm_destroy_drm_device(&adev->dm);
1684 #if defined(CONFIG_DRM_AMD_SECURE_DISPLAY)
1685 if (adev->dm.crc_rd_wrk) {
1686 flush_work(&adev->dm.crc_rd_wrk->notify_ta_work);
1687 kfree(adev->dm.crc_rd_wrk);
1688 adev->dm.crc_rd_wrk = NULL;
1691 #ifdef CONFIG_DRM_AMD_DC_HDCP
1692 if (adev->dm.hdcp_workqueue) {
1693 hdcp_destroy(&adev->dev->kobj, adev->dm.hdcp_workqueue);
1694 adev->dm.hdcp_workqueue = NULL;
1698 dc_deinit_callbacks(adev->dm.dc);
1701 dc_dmub_srv_destroy(&adev->dm.dc->ctx->dmub_srv);
1703 if (dc_enable_dmub_notifications(adev->dm.dc)) {
1704 kfree(adev->dm.dmub_notify);
1705 adev->dm.dmub_notify = NULL;
1706 destroy_workqueue(adev->dm.delayed_hpd_wq);
1707 adev->dm.delayed_hpd_wq = NULL;
1710 if (adev->dm.dmub_bo)
1711 amdgpu_bo_free_kernel(&adev->dm.dmub_bo,
1712 &adev->dm.dmub_bo_gpu_addr,
1713 &adev->dm.dmub_bo_cpu_addr);
1715 if (adev->dm.hpd_rx_offload_wq) {
1716 for (i = 0; i < adev->dm.dc->caps.max_links; i++) {
1717 if (adev->dm.hpd_rx_offload_wq[i].wq) {
1718 destroy_workqueue(adev->dm.hpd_rx_offload_wq[i].wq);
1719 adev->dm.hpd_rx_offload_wq[i].wq = NULL;
1723 kfree(adev->dm.hpd_rx_offload_wq);
1724 adev->dm.hpd_rx_offload_wq = NULL;
1727 /* DC Destroy TODO: Replace destroy DAL */
1729 dc_destroy(&adev->dm.dc);
1731 * TODO: pageflip, vlank interrupt
1733 * amdgpu_dm_irq_fini(adev);
1736 if (adev->dm.cgs_device) {
1737 amdgpu_cgs_destroy_device(adev->dm.cgs_device);
1738 adev->dm.cgs_device = NULL;
1740 if (adev->dm.freesync_module) {
1741 mod_freesync_destroy(adev->dm.freesync_module);
1742 adev->dm.freesync_module = NULL;
1745 mutex_destroy(&adev->dm.audio_lock);
1746 mutex_destroy(&adev->dm.dc_lock);
1751 static int load_dmcu_fw(struct amdgpu_device *adev)
1753 const char *fw_name_dmcu = NULL;
1755 const struct dmcu_firmware_header_v1_0 *hdr;
1757 switch(adev->asic_type) {
1758 #if defined(CONFIG_DRM_AMD_DC_SI)
1773 case CHIP_POLARIS11:
1774 case CHIP_POLARIS10:
1775 case CHIP_POLARIS12:
1782 fw_name_dmcu = FIRMWARE_NAVI12_DMCU;
1785 if (ASICREV_IS_PICASSO(adev->external_rev_id))
1786 fw_name_dmcu = FIRMWARE_RAVEN_DMCU;
1787 else if (ASICREV_IS_RAVEN2(adev->external_rev_id))
1788 fw_name_dmcu = FIRMWARE_RAVEN_DMCU;
1793 switch (adev->ip_versions[DCE_HWIP][0]) {
1794 case IP_VERSION(2, 0, 2):
1795 case IP_VERSION(2, 0, 3):
1796 case IP_VERSION(2, 0, 0):
1797 case IP_VERSION(2, 1, 0):
1798 case IP_VERSION(3, 0, 0):
1799 case IP_VERSION(3, 0, 2):
1800 case IP_VERSION(3, 0, 3):
1801 case IP_VERSION(3, 0, 1):
1802 case IP_VERSION(3, 1, 2):
1803 case IP_VERSION(3, 1, 3):
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;
1921 /* ASIC doesn't support DMUB. */
1925 r = request_firmware_direct(&adev->dm.dmub_fw, fw_name_dmub, adev->dev);
1927 DRM_ERROR("DMUB firmware loading failed: %d\n", r);
1931 r = amdgpu_ucode_validate(adev->dm.dmub_fw);
1933 DRM_ERROR("Couldn't validate DMUB firmware: %d\n", r);
1937 hdr = (const struct dmcub_firmware_header_v1_0 *)adev->dm.dmub_fw->data;
1938 adev->dm.dmcub_fw_version = le32_to_cpu(hdr->header.ucode_version);
1940 if (adev->firmware.load_type == AMDGPU_FW_LOAD_PSP) {
1941 adev->firmware.ucode[AMDGPU_UCODE_ID_DMCUB].ucode_id =
1942 AMDGPU_UCODE_ID_DMCUB;
1943 adev->firmware.ucode[AMDGPU_UCODE_ID_DMCUB].fw =
1945 adev->firmware.fw_size +=
1946 ALIGN(le32_to_cpu(hdr->inst_const_bytes), PAGE_SIZE);
1948 DRM_INFO("Loading DMUB firmware via PSP: version=0x%08X\n",
1949 adev->dm.dmcub_fw_version);
1953 adev->dm.dmub_srv = kzalloc(sizeof(*adev->dm.dmub_srv), GFP_KERNEL);
1954 dmub_srv = adev->dm.dmub_srv;
1957 DRM_ERROR("Failed to allocate DMUB service!\n");
1961 memset(&create_params, 0, sizeof(create_params));
1962 create_params.user_ctx = adev;
1963 create_params.funcs.reg_read = amdgpu_dm_dmub_reg_read;
1964 create_params.funcs.reg_write = amdgpu_dm_dmub_reg_write;
1965 create_params.asic = dmub_asic;
1967 /* Create the DMUB service. */
1968 status = dmub_srv_create(dmub_srv, &create_params);
1969 if (status != DMUB_STATUS_OK) {
1970 DRM_ERROR("Error creating DMUB service: %d\n", status);
1974 /* Calculate the size of all the regions for the DMUB service. */
1975 memset(®ion_params, 0, sizeof(region_params));
1977 region_params.inst_const_size = le32_to_cpu(hdr->inst_const_bytes) -
1978 PSP_HEADER_BYTES - PSP_FOOTER_BYTES;
1979 region_params.bss_data_size = le32_to_cpu(hdr->bss_data_bytes);
1980 region_params.vbios_size = adev->bios_size;
1981 region_params.fw_bss_data = region_params.bss_data_size ?
1982 adev->dm.dmub_fw->data +
1983 le32_to_cpu(hdr->header.ucode_array_offset_bytes) +
1984 le32_to_cpu(hdr->inst_const_bytes) : NULL;
1985 region_params.fw_inst_const =
1986 adev->dm.dmub_fw->data +
1987 le32_to_cpu(hdr->header.ucode_array_offset_bytes) +
1990 status = dmub_srv_calc_region_info(dmub_srv, ®ion_params,
1993 if (status != DMUB_STATUS_OK) {
1994 DRM_ERROR("Error calculating DMUB region info: %d\n", status);
1999 * Allocate a framebuffer based on the total size of all the regions.
2000 * TODO: Move this into GART.
2002 r = amdgpu_bo_create_kernel(adev, region_info.fb_size, PAGE_SIZE,
2003 AMDGPU_GEM_DOMAIN_VRAM, &adev->dm.dmub_bo,
2004 &adev->dm.dmub_bo_gpu_addr,
2005 &adev->dm.dmub_bo_cpu_addr);
2009 /* Rebase the regions on the framebuffer address. */
2010 memset(&fb_params, 0, sizeof(fb_params));
2011 fb_params.cpu_addr = adev->dm.dmub_bo_cpu_addr;
2012 fb_params.gpu_addr = adev->dm.dmub_bo_gpu_addr;
2013 fb_params.region_info = ®ion_info;
2015 adev->dm.dmub_fb_info =
2016 kzalloc(sizeof(*adev->dm.dmub_fb_info), GFP_KERNEL);
2017 fb_info = adev->dm.dmub_fb_info;
2021 "Failed to allocate framebuffer info for DMUB service!\n");
2025 status = dmub_srv_calc_fb_info(dmub_srv, &fb_params, fb_info);
2026 if (status != DMUB_STATUS_OK) {
2027 DRM_ERROR("Error calculating DMUB FB info: %d\n", status);
2034 static int dm_sw_init(void *handle)
2036 struct amdgpu_device *adev = (struct amdgpu_device *)handle;
2039 r = dm_dmub_sw_init(adev);
2043 return load_dmcu_fw(adev);
2046 static int dm_sw_fini(void *handle)
2048 struct amdgpu_device *adev = (struct amdgpu_device *)handle;
2050 kfree(adev->dm.dmub_fb_info);
2051 adev->dm.dmub_fb_info = NULL;
2053 if (adev->dm.dmub_srv) {
2054 dmub_srv_destroy(adev->dm.dmub_srv);
2055 adev->dm.dmub_srv = NULL;
2058 release_firmware(adev->dm.dmub_fw);
2059 adev->dm.dmub_fw = NULL;
2061 release_firmware(adev->dm.fw_dmcu);
2062 adev->dm.fw_dmcu = NULL;
2067 static int detect_mst_link_for_all_connectors(struct drm_device *dev)
2069 struct amdgpu_dm_connector *aconnector;
2070 struct drm_connector *connector;
2071 struct drm_connector_list_iter iter;
2074 drm_connector_list_iter_begin(dev, &iter);
2075 drm_for_each_connector_iter(connector, &iter) {
2076 aconnector = to_amdgpu_dm_connector(connector);
2077 if (aconnector->dc_link->type == dc_connection_mst_branch &&
2078 aconnector->mst_mgr.aux) {
2079 DRM_DEBUG_DRIVER("DM_MST: starting TM on aconnector: %p [id: %d]\n",
2081 aconnector->base.base.id);
2083 ret = drm_dp_mst_topology_mgr_set_mst(&aconnector->mst_mgr, true);
2085 DRM_ERROR("DM_MST: Failed to start MST\n");
2086 aconnector->dc_link->type =
2087 dc_connection_single;
2092 drm_connector_list_iter_end(&iter);
2097 static int dm_late_init(void *handle)
2099 struct amdgpu_device *adev = (struct amdgpu_device *)handle;
2101 struct dmcu_iram_parameters params;
2102 unsigned int linear_lut[16];
2104 struct dmcu *dmcu = NULL;
2106 dmcu = adev->dm.dc->res_pool->dmcu;
2108 for (i = 0; i < 16; i++)
2109 linear_lut[i] = 0xFFFF * i / 15;
2112 params.backlight_ramping_override = false;
2113 params.backlight_ramping_start = 0xCCCC;
2114 params.backlight_ramping_reduction = 0xCCCCCCCC;
2115 params.backlight_lut_array_size = 16;
2116 params.backlight_lut_array = linear_lut;
2118 /* Min backlight level after ABM reduction, Don't allow below 1%
2119 * 0xFFFF x 0.01 = 0x28F
2121 params.min_abm_backlight = 0x28F;
2122 /* In the case where abm is implemented on dmcub,
2123 * dmcu object will be null.
2124 * ABM 2.4 and up are implemented on dmcub.
2127 if (!dmcu_load_iram(dmcu, params))
2129 } else if (adev->dm.dc->ctx->dmub_srv) {
2130 struct dc_link *edp_links[MAX_NUM_EDP];
2133 get_edp_links(adev->dm.dc, edp_links, &edp_num);
2134 for (i = 0; i < edp_num; i++) {
2135 if (!dmub_init_abm_config(adev->dm.dc->res_pool, params, i))
2140 return detect_mst_link_for_all_connectors(adev_to_drm(adev));
2143 static void s3_handle_mst(struct drm_device *dev, bool suspend)
2145 struct amdgpu_dm_connector *aconnector;
2146 struct drm_connector *connector;
2147 struct drm_connector_list_iter iter;
2148 struct drm_dp_mst_topology_mgr *mgr;
2150 bool need_hotplug = false;
2152 drm_connector_list_iter_begin(dev, &iter);
2153 drm_for_each_connector_iter(connector, &iter) {
2154 aconnector = to_amdgpu_dm_connector(connector);
2155 if (aconnector->dc_link->type != dc_connection_mst_branch ||
2156 aconnector->mst_port)
2159 mgr = &aconnector->mst_mgr;
2162 drm_dp_mst_topology_mgr_suspend(mgr);
2164 ret = drm_dp_mst_topology_mgr_resume(mgr, true);
2166 drm_dp_mst_topology_mgr_set_mst(mgr, false);
2167 need_hotplug = true;
2171 drm_connector_list_iter_end(&iter);
2174 drm_kms_helper_hotplug_event(dev);
2177 static int amdgpu_dm_smu_write_watermarks_table(struct amdgpu_device *adev)
2181 /* This interface is for dGPU Navi1x.Linux dc-pplib interface depends
2182 * on window driver dc implementation.
2183 * For Navi1x, clock settings of dcn watermarks are fixed. the settings
2184 * should be passed to smu during boot up and resume from s3.
2185 * boot up: dc calculate dcn watermark clock settings within dc_create,
2186 * dcn20_resource_construct
2187 * then call pplib functions below to pass the settings to smu:
2188 * smu_set_watermarks_for_clock_ranges
2189 * smu_set_watermarks_table
2190 * navi10_set_watermarks_table
2191 * smu_write_watermarks_table
2193 * For Renoir, clock settings of dcn watermark are also fixed values.
2194 * dc has implemented different flow for window driver:
2195 * dc_hardware_init / dc_set_power_state
2200 * smu_set_watermarks_for_clock_ranges
2201 * renoir_set_watermarks_table
2202 * smu_write_watermarks_table
2205 * dc_hardware_init -> amdgpu_dm_init
2206 * dc_set_power_state --> dm_resume
2208 * therefore, this function apply to navi10/12/14 but not Renoir
2211 switch (adev->ip_versions[DCE_HWIP][0]) {
2212 case IP_VERSION(2, 0, 2):
2213 case IP_VERSION(2, 0, 0):
2219 ret = amdgpu_dpm_write_watermarks_table(adev);
2221 DRM_ERROR("Failed to update WMTABLE!\n");
2229 * dm_hw_init() - Initialize DC device
2230 * @handle: The base driver device containing the amdgpu_dm device.
2232 * Initialize the &struct amdgpu_display_manager device. This involves calling
2233 * the initializers of each DM component, then populating the struct with them.
2235 * Although the function implies hardware initialization, both hardware and
2236 * software are initialized here. Splitting them out to their relevant init
2237 * hooks is a future TODO item.
2239 * Some notable things that are initialized here:
2241 * - Display Core, both software and hardware
2242 * - DC modules that we need (freesync and color management)
2243 * - DRM software states
2244 * - Interrupt sources and handlers
2246 * - Debug FS entries, if enabled
2248 static int dm_hw_init(void *handle)
2250 struct amdgpu_device *adev = (struct amdgpu_device *)handle;
2251 /* Create DAL display manager */
2252 amdgpu_dm_init(adev);
2253 amdgpu_dm_hpd_init(adev);
2259 * dm_hw_fini() - Teardown DC device
2260 * @handle: The base driver device containing the amdgpu_dm device.
2262 * Teardown components within &struct amdgpu_display_manager that require
2263 * cleanup. This involves cleaning up the DRM device, DC, and any modules that
2264 * were loaded. Also flush IRQ workqueues and disable them.
2266 static int dm_hw_fini(void *handle)
2268 struct amdgpu_device *adev = (struct amdgpu_device *)handle;
2270 amdgpu_dm_hpd_fini(adev);
2272 amdgpu_dm_irq_fini(adev);
2273 amdgpu_dm_fini(adev);
2278 static int dm_enable_vblank(struct drm_crtc *crtc);
2279 static void dm_disable_vblank(struct drm_crtc *crtc);
2281 static void dm_gpureset_toggle_interrupts(struct amdgpu_device *adev,
2282 struct dc_state *state, bool enable)
2284 enum dc_irq_source irq_source;
2285 struct amdgpu_crtc *acrtc;
2289 for (i = 0; i < state->stream_count; i++) {
2290 acrtc = get_crtc_by_otg_inst(
2291 adev, state->stream_status[i].primary_otg_inst);
2293 if (acrtc && state->stream_status[i].plane_count != 0) {
2294 irq_source = IRQ_TYPE_PFLIP + acrtc->otg_inst;
2295 rc = dc_interrupt_set(adev->dm.dc, irq_source, enable) ? 0 : -EBUSY;
2296 DRM_DEBUG_VBL("crtc %d - vupdate irq %sabling: r=%d\n",
2297 acrtc->crtc_id, enable ? "en" : "dis", rc);
2299 DRM_WARN("Failed to %s pflip interrupts\n",
2300 enable ? "enable" : "disable");
2303 rc = dm_enable_vblank(&acrtc->base);
2305 DRM_WARN("Failed to enable vblank interrupts\n");
2307 dm_disable_vblank(&acrtc->base);
2315 static enum dc_status amdgpu_dm_commit_zero_streams(struct dc *dc)
2317 struct dc_state *context = NULL;
2318 enum dc_status res = DC_ERROR_UNEXPECTED;
2320 struct dc_stream_state *del_streams[MAX_PIPES];
2321 int del_streams_count = 0;
2323 memset(del_streams, 0, sizeof(del_streams));
2325 context = dc_create_state(dc);
2326 if (context == NULL)
2327 goto context_alloc_fail;
2329 dc_resource_state_copy_construct_current(dc, context);
2331 /* First remove from context all streams */
2332 for (i = 0; i < context->stream_count; i++) {
2333 struct dc_stream_state *stream = context->streams[i];
2335 del_streams[del_streams_count++] = stream;
2338 /* Remove all planes for removed streams and then remove the streams */
2339 for (i = 0; i < del_streams_count; i++) {
2340 if (!dc_rem_all_planes_for_stream(dc, del_streams[i], context)) {
2341 res = DC_FAIL_DETACH_SURFACES;
2345 res = dc_remove_stream_from_ctx(dc, context, del_streams[i]);
2350 res = dc_commit_state(dc, context);
2353 dc_release_state(context);
2359 static void hpd_rx_irq_work_suspend(struct amdgpu_display_manager *dm)
2363 if (dm->hpd_rx_offload_wq) {
2364 for (i = 0; i < dm->dc->caps.max_links; i++)
2365 flush_workqueue(dm->hpd_rx_offload_wq[i].wq);
2369 static int dm_suspend(void *handle)
2371 struct amdgpu_device *adev = handle;
2372 struct amdgpu_display_manager *dm = &adev->dm;
2375 if (amdgpu_in_reset(adev)) {
2376 mutex_lock(&dm->dc_lock);
2378 #if defined(CONFIG_DRM_AMD_DC_DCN)
2379 dc_allow_idle_optimizations(adev->dm.dc, false);
2382 dm->cached_dc_state = dc_copy_state(dm->dc->current_state);
2384 dm_gpureset_toggle_interrupts(adev, dm->cached_dc_state, false);
2386 amdgpu_dm_commit_zero_streams(dm->dc);
2388 amdgpu_dm_irq_suspend(adev);
2390 hpd_rx_irq_work_suspend(dm);
2395 WARN_ON(adev->dm.cached_state);
2396 adev->dm.cached_state = drm_atomic_helper_suspend(adev_to_drm(adev));
2398 s3_handle_mst(adev_to_drm(adev), true);
2400 amdgpu_dm_irq_suspend(adev);
2402 hpd_rx_irq_work_suspend(dm);
2404 dc_set_power_state(dm->dc, DC_ACPI_CM_POWER_STATE_D3);
2409 static struct amdgpu_dm_connector *
2410 amdgpu_dm_find_first_crtc_matching_connector(struct drm_atomic_state *state,
2411 struct drm_crtc *crtc)
2414 struct drm_connector_state *new_con_state;
2415 struct drm_connector *connector;
2416 struct drm_crtc *crtc_from_state;
2418 for_each_new_connector_in_state(state, connector, new_con_state, i) {
2419 crtc_from_state = new_con_state->crtc;
2421 if (crtc_from_state == crtc)
2422 return to_amdgpu_dm_connector(connector);
2428 static void emulated_link_detect(struct dc_link *link)
2430 struct dc_sink_init_data sink_init_data = { 0 };
2431 struct display_sink_capability sink_caps = { 0 };
2432 enum dc_edid_status edid_status;
2433 struct dc_context *dc_ctx = link->ctx;
2434 struct dc_sink *sink = NULL;
2435 struct dc_sink *prev_sink = NULL;
2437 link->type = dc_connection_none;
2438 prev_sink = link->local_sink;
2441 dc_sink_release(prev_sink);
2443 switch (link->connector_signal) {
2444 case SIGNAL_TYPE_HDMI_TYPE_A: {
2445 sink_caps.transaction_type = DDC_TRANSACTION_TYPE_I2C;
2446 sink_caps.signal = SIGNAL_TYPE_HDMI_TYPE_A;
2450 case SIGNAL_TYPE_DVI_SINGLE_LINK: {
2451 sink_caps.transaction_type = DDC_TRANSACTION_TYPE_I2C;
2452 sink_caps.signal = SIGNAL_TYPE_DVI_SINGLE_LINK;
2456 case SIGNAL_TYPE_DVI_DUAL_LINK: {
2457 sink_caps.transaction_type = DDC_TRANSACTION_TYPE_I2C;
2458 sink_caps.signal = SIGNAL_TYPE_DVI_DUAL_LINK;
2462 case SIGNAL_TYPE_LVDS: {
2463 sink_caps.transaction_type = DDC_TRANSACTION_TYPE_I2C;
2464 sink_caps.signal = SIGNAL_TYPE_LVDS;
2468 case SIGNAL_TYPE_EDP: {
2469 sink_caps.transaction_type =
2470 DDC_TRANSACTION_TYPE_I2C_OVER_AUX;
2471 sink_caps.signal = SIGNAL_TYPE_EDP;
2475 case SIGNAL_TYPE_DISPLAY_PORT: {
2476 sink_caps.transaction_type =
2477 DDC_TRANSACTION_TYPE_I2C_OVER_AUX;
2478 sink_caps.signal = SIGNAL_TYPE_VIRTUAL;
2483 DC_ERROR("Invalid connector type! signal:%d\n",
2484 link->connector_signal);
2488 sink_init_data.link = link;
2489 sink_init_data.sink_signal = sink_caps.signal;
2491 sink = dc_sink_create(&sink_init_data);
2493 DC_ERROR("Failed to create sink!\n");
2497 /* dc_sink_create returns a new reference */
2498 link->local_sink = sink;
2500 edid_status = dm_helpers_read_local_edid(
2505 if (edid_status != EDID_OK)
2506 DC_ERROR("Failed to read EDID");
2510 static void dm_gpureset_commit_state(struct dc_state *dc_state,
2511 struct amdgpu_display_manager *dm)
2514 struct dc_surface_update surface_updates[MAX_SURFACES];
2515 struct dc_plane_info plane_infos[MAX_SURFACES];
2516 struct dc_scaling_info scaling_infos[MAX_SURFACES];
2517 struct dc_flip_addrs flip_addrs[MAX_SURFACES];
2518 struct dc_stream_update stream_update;
2522 bundle = kzalloc(sizeof(*bundle), GFP_KERNEL);
2525 dm_error("Failed to allocate update bundle\n");
2529 for (k = 0; k < dc_state->stream_count; k++) {
2530 bundle->stream_update.stream = dc_state->streams[k];
2532 for (m = 0; m < dc_state->stream_status->plane_count; m++) {
2533 bundle->surface_updates[m].surface =
2534 dc_state->stream_status->plane_states[m];
2535 bundle->surface_updates[m].surface->force_full_update =
2538 dc_commit_updates_for_stream(
2539 dm->dc, bundle->surface_updates,
2540 dc_state->stream_status->plane_count,
2541 dc_state->streams[k], &bundle->stream_update, dc_state);
2550 static void dm_set_dpms_off(struct dc_link *link, struct dm_crtc_state *acrtc_state)
2552 struct dc_stream_state *stream_state;
2553 struct amdgpu_dm_connector *aconnector = link->priv;
2554 struct amdgpu_device *adev = drm_to_adev(aconnector->base.dev);
2555 struct dc_stream_update stream_update;
2556 bool dpms_off = true;
2558 memset(&stream_update, 0, sizeof(stream_update));
2559 stream_update.dpms_off = &dpms_off;
2561 mutex_lock(&adev->dm.dc_lock);
2562 stream_state = dc_stream_find_from_link(link);
2564 if (stream_state == NULL) {
2565 DRM_DEBUG_DRIVER("Error finding stream state associated with link!\n");
2566 mutex_unlock(&adev->dm.dc_lock);
2570 stream_update.stream = stream_state;
2571 acrtc_state->force_dpms_off = true;
2572 dc_commit_updates_for_stream(stream_state->ctx->dc, NULL, 0,
2573 stream_state, &stream_update,
2574 stream_state->ctx->dc->current_state);
2575 mutex_unlock(&adev->dm.dc_lock);
2578 static int dm_resume(void *handle)
2580 struct amdgpu_device *adev = handle;
2581 struct drm_device *ddev = adev_to_drm(adev);
2582 struct amdgpu_display_manager *dm = &adev->dm;
2583 struct amdgpu_dm_connector *aconnector;
2584 struct drm_connector *connector;
2585 struct drm_connector_list_iter iter;
2586 struct drm_crtc *crtc;
2587 struct drm_crtc_state *new_crtc_state;
2588 struct dm_crtc_state *dm_new_crtc_state;
2589 struct drm_plane *plane;
2590 struct drm_plane_state *new_plane_state;
2591 struct dm_plane_state *dm_new_plane_state;
2592 struct dm_atomic_state *dm_state = to_dm_atomic_state(dm->atomic_obj.state);
2593 enum dc_connection_type new_connection_type = dc_connection_none;
2594 struct dc_state *dc_state;
2597 if (amdgpu_in_reset(adev)) {
2598 dc_state = dm->cached_dc_state;
2601 * The dc->current_state is backed up into dm->cached_dc_state
2602 * before we commit 0 streams.
2604 * DC will clear link encoder assignments on the real state
2605 * but the changes won't propagate over to the copy we made
2606 * before the 0 streams commit.
2608 * DC expects that link encoder assignments are *not* valid
2609 * when committing a state, so as a workaround it needs to be
2612 link_enc_cfg_init(dm->dc, dc_state);
2614 if (dc_enable_dmub_notifications(adev->dm.dc))
2615 amdgpu_dm_outbox_init(adev);
2617 r = dm_dmub_hw_init(adev);
2619 DRM_ERROR("DMUB interface failed to initialize: status=%d\n", r);
2621 dc_set_power_state(dm->dc, DC_ACPI_CM_POWER_STATE_D0);
2624 amdgpu_dm_irq_resume_early(adev);
2626 for (i = 0; i < dc_state->stream_count; i++) {
2627 dc_state->streams[i]->mode_changed = true;
2628 for (j = 0; j < dc_state->stream_status[i].plane_count; j++) {
2629 dc_state->stream_status[i].plane_states[j]->update_flags.raw
2634 WARN_ON(!dc_commit_state(dm->dc, dc_state));
2636 dm_gpureset_commit_state(dm->cached_dc_state, dm);
2638 dm_gpureset_toggle_interrupts(adev, dm->cached_dc_state, true);
2640 dc_release_state(dm->cached_dc_state);
2641 dm->cached_dc_state = NULL;
2643 amdgpu_dm_irq_resume_late(adev);
2645 mutex_unlock(&dm->dc_lock);
2649 /* Recreate dc_state - DC invalidates it when setting power state to S3. */
2650 dc_release_state(dm_state->context);
2651 dm_state->context = dc_create_state(dm->dc);
2652 /* TODO: Remove dc_state->dccg, use dc->dccg directly. */
2653 dc_resource_state_construct(dm->dc, dm_state->context);
2655 /* Re-enable outbox interrupts for DPIA. */
2656 if (dc_enable_dmub_notifications(adev->dm.dc))
2657 amdgpu_dm_outbox_init(adev);
2659 /* Before powering on DC we need to re-initialize DMUB. */
2660 dm_dmub_hw_resume(adev);
2662 /* power on hardware */
2663 dc_set_power_state(dm->dc, DC_ACPI_CM_POWER_STATE_D0);
2665 /* program HPD filter */
2669 * early enable HPD Rx IRQ, should be done before set mode as short
2670 * pulse interrupts are used for MST
2672 amdgpu_dm_irq_resume_early(adev);
2674 /* On resume we need to rewrite the MSTM control bits to enable MST*/
2675 s3_handle_mst(ddev, false);
2678 drm_connector_list_iter_begin(ddev, &iter);
2679 drm_for_each_connector_iter(connector, &iter) {
2680 aconnector = to_amdgpu_dm_connector(connector);
2683 * this is the case when traversing through already created
2684 * MST connectors, should be skipped
2686 if (aconnector->mst_port)
2689 mutex_lock(&aconnector->hpd_lock);
2690 if (!dc_link_detect_sink(aconnector->dc_link, &new_connection_type))
2691 DRM_ERROR("KMS: Failed to detect connector\n");
2693 if (aconnector->base.force && new_connection_type == dc_connection_none)
2694 emulated_link_detect(aconnector->dc_link);
2696 dc_link_detect(aconnector->dc_link, DETECT_REASON_HPD);
2698 if (aconnector->fake_enable && aconnector->dc_link->local_sink)
2699 aconnector->fake_enable = false;
2701 if (aconnector->dc_sink)
2702 dc_sink_release(aconnector->dc_sink);
2703 aconnector->dc_sink = NULL;
2704 amdgpu_dm_update_connector_after_detect(aconnector);
2705 mutex_unlock(&aconnector->hpd_lock);
2707 drm_connector_list_iter_end(&iter);
2709 /* Force mode set in atomic commit */
2710 for_each_new_crtc_in_state(dm->cached_state, crtc, new_crtc_state, i)
2711 new_crtc_state->active_changed = true;
2714 * atomic_check is expected to create the dc states. We need to release
2715 * them here, since they were duplicated as part of the suspend
2718 for_each_new_crtc_in_state(dm->cached_state, crtc, new_crtc_state, i) {
2719 dm_new_crtc_state = to_dm_crtc_state(new_crtc_state);
2720 if (dm_new_crtc_state->stream) {
2721 WARN_ON(kref_read(&dm_new_crtc_state->stream->refcount) > 1);
2722 dc_stream_release(dm_new_crtc_state->stream);
2723 dm_new_crtc_state->stream = NULL;
2727 for_each_new_plane_in_state(dm->cached_state, plane, new_plane_state, i) {
2728 dm_new_plane_state = to_dm_plane_state(new_plane_state);
2729 if (dm_new_plane_state->dc_state) {
2730 WARN_ON(kref_read(&dm_new_plane_state->dc_state->refcount) > 1);
2731 dc_plane_state_release(dm_new_plane_state->dc_state);
2732 dm_new_plane_state->dc_state = NULL;
2736 drm_atomic_helper_resume(ddev, dm->cached_state);
2738 dm->cached_state = NULL;
2740 amdgpu_dm_irq_resume_late(adev);
2742 amdgpu_dm_smu_write_watermarks_table(adev);
2750 * DM (and consequently DC) is registered in the amdgpu base driver as a IP
2751 * block. When CONFIG_DRM_AMD_DC is enabled, the DM device IP block is added to
2752 * the base driver's device list to be initialized and torn down accordingly.
2754 * The functions to do so are provided as hooks in &struct amd_ip_funcs.
2757 static const struct amd_ip_funcs amdgpu_dm_funcs = {
2759 .early_init = dm_early_init,
2760 .late_init = dm_late_init,
2761 .sw_init = dm_sw_init,
2762 .sw_fini = dm_sw_fini,
2763 .early_fini = amdgpu_dm_early_fini,
2764 .hw_init = dm_hw_init,
2765 .hw_fini = dm_hw_fini,
2766 .suspend = dm_suspend,
2767 .resume = dm_resume,
2768 .is_idle = dm_is_idle,
2769 .wait_for_idle = dm_wait_for_idle,
2770 .check_soft_reset = dm_check_soft_reset,
2771 .soft_reset = dm_soft_reset,
2772 .set_clockgating_state = dm_set_clockgating_state,
2773 .set_powergating_state = dm_set_powergating_state,
2776 const struct amdgpu_ip_block_version dm_ip_block =
2778 .type = AMD_IP_BLOCK_TYPE_DCE,
2782 .funcs = &amdgpu_dm_funcs,
2792 static const struct drm_mode_config_funcs amdgpu_dm_mode_funcs = {
2793 .fb_create = amdgpu_display_user_framebuffer_create,
2794 .get_format_info = amd_get_format_info,
2795 .output_poll_changed = drm_fb_helper_output_poll_changed,
2796 .atomic_check = amdgpu_dm_atomic_check,
2797 .atomic_commit = drm_atomic_helper_commit,
2800 static struct drm_mode_config_helper_funcs amdgpu_dm_mode_config_helperfuncs = {
2801 .atomic_commit_tail = amdgpu_dm_atomic_commit_tail
2804 static void update_connector_ext_caps(struct amdgpu_dm_connector *aconnector)
2806 u32 max_cll, min_cll, max, min, q, r;
2807 struct amdgpu_dm_backlight_caps *caps;
2808 struct amdgpu_display_manager *dm;
2809 struct drm_connector *conn_base;
2810 struct amdgpu_device *adev;
2811 struct dc_link *link = NULL;
2812 static const u8 pre_computed_values[] = {
2813 50, 51, 52, 53, 55, 56, 57, 58, 59, 61, 62, 63, 65, 66, 68, 69,
2814 71, 72, 74, 75, 77, 79, 81, 82, 84, 86, 88, 90, 92, 94, 96, 98};
2817 if (!aconnector || !aconnector->dc_link)
2820 link = aconnector->dc_link;
2821 if (link->connector_signal != SIGNAL_TYPE_EDP)
2824 conn_base = &aconnector->base;
2825 adev = drm_to_adev(conn_base->dev);
2827 for (i = 0; i < dm->num_of_edps; i++) {
2828 if (link == dm->backlight_link[i])
2831 if (i >= dm->num_of_edps)
2833 caps = &dm->backlight_caps[i];
2834 caps->ext_caps = &aconnector->dc_link->dpcd_sink_ext_caps;
2835 caps->aux_support = false;
2836 max_cll = conn_base->hdr_sink_metadata.hdmi_type1.max_cll;
2837 min_cll = conn_base->hdr_sink_metadata.hdmi_type1.min_cll;
2839 if (caps->ext_caps->bits.oled == 1 /*||
2840 caps->ext_caps->bits.sdr_aux_backlight_control == 1 ||
2841 caps->ext_caps->bits.hdr_aux_backlight_control == 1*/)
2842 caps->aux_support = true;
2844 if (amdgpu_backlight == 0)
2845 caps->aux_support = false;
2846 else if (amdgpu_backlight == 1)
2847 caps->aux_support = true;
2849 /* From the specification (CTA-861-G), for calculating the maximum
2850 * luminance we need to use:
2851 * Luminance = 50*2**(CV/32)
2852 * Where CV is a one-byte value.
2853 * For calculating this expression we may need float point precision;
2854 * to avoid this complexity level, we take advantage that CV is divided
2855 * by a constant. From the Euclids division algorithm, we know that CV
2856 * can be written as: CV = 32*q + r. Next, we replace CV in the
2857 * Luminance expression and get 50*(2**q)*(2**(r/32)), hence we just
2858 * need to pre-compute the value of r/32. For pre-computing the values
2859 * We just used the following Ruby line:
2860 * (0...32).each {|cv| puts (50*2**(cv/32.0)).round}
2861 * The results of the above expressions can be verified at
2862 * pre_computed_values.
2866 max = (1 << q) * pre_computed_values[r];
2868 // min luminance: maxLum * (CV/255)^2 / 100
2869 q = DIV_ROUND_CLOSEST(min_cll, 255);
2870 min = max * DIV_ROUND_CLOSEST((q * q), 100);
2872 caps->aux_max_input_signal = max;
2873 caps->aux_min_input_signal = min;
2876 void amdgpu_dm_update_connector_after_detect(
2877 struct amdgpu_dm_connector *aconnector)
2879 struct drm_connector *connector = &aconnector->base;
2880 struct drm_device *dev = connector->dev;
2881 struct dc_sink *sink;
2883 /* MST handled by drm_mst framework */
2884 if (aconnector->mst_mgr.mst_state == true)
2887 sink = aconnector->dc_link->local_sink;
2889 dc_sink_retain(sink);
2892 * Edid mgmt connector gets first update only in mode_valid hook and then
2893 * the connector sink is set to either fake or physical sink depends on link status.
2894 * Skip if already done during boot.
2896 if (aconnector->base.force != DRM_FORCE_UNSPECIFIED
2897 && aconnector->dc_em_sink) {
2900 * For S3 resume with headless use eml_sink to fake stream
2901 * because on resume connector->sink is set to NULL
2903 mutex_lock(&dev->mode_config.mutex);
2906 if (aconnector->dc_sink) {
2907 amdgpu_dm_update_freesync_caps(connector, NULL);
2909 * retain and release below are used to
2910 * bump up refcount for sink because the link doesn't point
2911 * to it anymore after disconnect, so on next crtc to connector
2912 * reshuffle by UMD we will get into unwanted dc_sink release
2914 dc_sink_release(aconnector->dc_sink);
2916 aconnector->dc_sink = sink;
2917 dc_sink_retain(aconnector->dc_sink);
2918 amdgpu_dm_update_freesync_caps(connector,
2921 amdgpu_dm_update_freesync_caps(connector, NULL);
2922 if (!aconnector->dc_sink) {
2923 aconnector->dc_sink = aconnector->dc_em_sink;
2924 dc_sink_retain(aconnector->dc_sink);
2928 mutex_unlock(&dev->mode_config.mutex);
2931 dc_sink_release(sink);
2936 * TODO: temporary guard to look for proper fix
2937 * if this sink is MST sink, we should not do anything
2939 if (sink && sink->sink_signal == SIGNAL_TYPE_DISPLAY_PORT_MST) {
2940 dc_sink_release(sink);
2944 if (aconnector->dc_sink == sink) {
2946 * We got a DP short pulse (Link Loss, DP CTS, etc...).
2949 DRM_DEBUG_DRIVER("DCHPD: connector_id=%d: dc_sink didn't change.\n",
2950 aconnector->connector_id);
2952 dc_sink_release(sink);
2956 DRM_DEBUG_DRIVER("DCHPD: connector_id=%d: Old sink=%p New sink=%p\n",
2957 aconnector->connector_id, aconnector->dc_sink, sink);
2959 mutex_lock(&dev->mode_config.mutex);
2962 * 1. Update status of the drm connector
2963 * 2. Send an event and let userspace tell us what to do
2967 * TODO: check if we still need the S3 mode update workaround.
2968 * If yes, put it here.
2970 if (aconnector->dc_sink) {
2971 amdgpu_dm_update_freesync_caps(connector, NULL);
2972 dc_sink_release(aconnector->dc_sink);
2975 aconnector->dc_sink = sink;
2976 dc_sink_retain(aconnector->dc_sink);
2977 if (sink->dc_edid.length == 0) {
2978 aconnector->edid = NULL;
2979 if (aconnector->dc_link->aux_mode) {
2980 drm_dp_cec_unset_edid(
2981 &aconnector->dm_dp_aux.aux);
2985 (struct edid *)sink->dc_edid.raw_edid;
2987 if (aconnector->dc_link->aux_mode)
2988 drm_dp_cec_set_edid(&aconnector->dm_dp_aux.aux,
2992 drm_connector_update_edid_property(connector, aconnector->edid);
2993 amdgpu_dm_update_freesync_caps(connector, aconnector->edid);
2994 update_connector_ext_caps(aconnector);
2996 drm_dp_cec_unset_edid(&aconnector->dm_dp_aux.aux);
2997 amdgpu_dm_update_freesync_caps(connector, NULL);
2998 drm_connector_update_edid_property(connector, NULL);
2999 aconnector->num_modes = 0;
3000 dc_sink_release(aconnector->dc_sink);
3001 aconnector->dc_sink = NULL;
3002 aconnector->edid = NULL;
3003 #ifdef CONFIG_DRM_AMD_DC_HDCP
3004 /* Set CP to DESIRED if it was ENABLED, so we can re-enable it again on hotplug */
3005 if (connector->state->content_protection == DRM_MODE_CONTENT_PROTECTION_ENABLED)
3006 connector->state->content_protection = DRM_MODE_CONTENT_PROTECTION_DESIRED;
3010 mutex_unlock(&dev->mode_config.mutex);
3012 update_subconnector_property(aconnector);
3015 dc_sink_release(sink);
3018 static void handle_hpd_irq_helper(struct amdgpu_dm_connector *aconnector)
3020 struct drm_connector *connector = &aconnector->base;
3021 struct drm_device *dev = connector->dev;
3022 enum dc_connection_type new_connection_type = dc_connection_none;
3023 struct amdgpu_device *adev = drm_to_adev(dev);
3024 struct dm_connector_state *dm_con_state = to_dm_connector_state(connector->state);
3025 struct dm_crtc_state *dm_crtc_state = NULL;
3027 if (adev->dm.disable_hpd_irq)
3030 if (dm_con_state->base.state && dm_con_state->base.crtc)
3031 dm_crtc_state = to_dm_crtc_state(drm_atomic_get_crtc_state(
3032 dm_con_state->base.state,
3033 dm_con_state->base.crtc));
3035 * In case of failure or MST no need to update connector status or notify the OS
3036 * since (for MST case) MST does this in its own context.
3038 mutex_lock(&aconnector->hpd_lock);
3040 #ifdef CONFIG_DRM_AMD_DC_HDCP
3041 if (adev->dm.hdcp_workqueue) {
3042 hdcp_reset_display(adev->dm.hdcp_workqueue, aconnector->dc_link->link_index);
3043 dm_con_state->update_hdcp = true;
3046 if (aconnector->fake_enable)
3047 aconnector->fake_enable = false;
3049 if (!dc_link_detect_sink(aconnector->dc_link, &new_connection_type))
3050 DRM_ERROR("KMS: Failed to detect connector\n");
3052 if (aconnector->base.force && new_connection_type == dc_connection_none) {
3053 emulated_link_detect(aconnector->dc_link);
3055 drm_modeset_lock_all(dev);
3056 dm_restore_drm_connector_state(dev, connector);
3057 drm_modeset_unlock_all(dev);
3059 if (aconnector->base.force == DRM_FORCE_UNSPECIFIED)
3060 drm_kms_helper_connector_hotplug_event(connector);
3062 } else if (dc_link_detect(aconnector->dc_link, DETECT_REASON_HPD)) {
3063 if (new_connection_type == dc_connection_none &&
3064 aconnector->dc_link->type == dc_connection_none &&
3066 dm_set_dpms_off(aconnector->dc_link, dm_crtc_state);
3068 amdgpu_dm_update_connector_after_detect(aconnector);
3070 drm_modeset_lock_all(dev);
3071 dm_restore_drm_connector_state(dev, connector);
3072 drm_modeset_unlock_all(dev);
3074 if (aconnector->base.force == DRM_FORCE_UNSPECIFIED)
3075 drm_kms_helper_connector_hotplug_event(connector);
3077 mutex_unlock(&aconnector->hpd_lock);
3081 static void handle_hpd_irq(void *param)
3083 struct amdgpu_dm_connector *aconnector = (struct amdgpu_dm_connector *)param;
3085 handle_hpd_irq_helper(aconnector);
3089 static void dm_handle_mst_sideband_msg(struct amdgpu_dm_connector *aconnector)
3091 uint8_t esi[DP_PSR_ERROR_STATUS - DP_SINK_COUNT_ESI] = { 0 };
3093 bool new_irq_handled = false;
3095 int dpcd_bytes_to_read;
3097 const int max_process_count = 30;
3098 int process_count = 0;
3100 const struct dc_link_status *link_status = dc_link_get_status(aconnector->dc_link);
3102 if (link_status->dpcd_caps->dpcd_rev.raw < 0x12) {
3103 dpcd_bytes_to_read = DP_LANE0_1_STATUS - DP_SINK_COUNT;
3104 /* DPCD 0x200 - 0x201 for downstream IRQ */
3105 dpcd_addr = DP_SINK_COUNT;
3107 dpcd_bytes_to_read = DP_PSR_ERROR_STATUS - DP_SINK_COUNT_ESI;
3108 /* DPCD 0x2002 - 0x2005 for downstream IRQ */
3109 dpcd_addr = DP_SINK_COUNT_ESI;
3112 dret = drm_dp_dpcd_read(
3113 &aconnector->dm_dp_aux.aux,
3116 dpcd_bytes_to_read);
3118 while (dret == dpcd_bytes_to_read &&
3119 process_count < max_process_count) {
3125 DRM_DEBUG_DRIVER("ESI %02x %02x %02x\n", esi[0], esi[1], esi[2]);
3126 /* handle HPD short pulse irq */
3127 if (aconnector->mst_mgr.mst_state)
3129 &aconnector->mst_mgr,
3133 if (new_irq_handled) {
3134 /* ACK at DPCD to notify down stream */
3135 const int ack_dpcd_bytes_to_write =
3136 dpcd_bytes_to_read - 1;
3138 for (retry = 0; retry < 3; retry++) {
3141 wret = drm_dp_dpcd_write(
3142 &aconnector->dm_dp_aux.aux,
3145 ack_dpcd_bytes_to_write);
3146 if (wret == ack_dpcd_bytes_to_write)
3150 /* check if there is new irq to be handled */
3151 dret = drm_dp_dpcd_read(
3152 &aconnector->dm_dp_aux.aux,
3155 dpcd_bytes_to_read);
3157 new_irq_handled = false;
3163 if (process_count == max_process_count)
3164 DRM_DEBUG_DRIVER("Loop exceeded max iterations\n");
3167 static void schedule_hpd_rx_offload_work(struct hpd_rx_irq_offload_work_queue *offload_wq,
3168 union hpd_irq_data hpd_irq_data)
3170 struct hpd_rx_irq_offload_work *offload_work =
3171 kzalloc(sizeof(*offload_work), GFP_KERNEL);
3173 if (!offload_work) {
3174 DRM_ERROR("Failed to allocate hpd_rx_irq_offload_work.\n");
3178 INIT_WORK(&offload_work->work, dm_handle_hpd_rx_offload_work);
3179 offload_work->data = hpd_irq_data;
3180 offload_work->offload_wq = offload_wq;
3182 queue_work(offload_wq->wq, &offload_work->work);
3183 DRM_DEBUG_KMS("queue work to handle hpd_rx offload work");
3186 static void handle_hpd_rx_irq(void *param)
3188 struct amdgpu_dm_connector *aconnector = (struct amdgpu_dm_connector *)param;
3189 struct drm_connector *connector = &aconnector->base;
3190 struct drm_device *dev = connector->dev;
3191 struct dc_link *dc_link = aconnector->dc_link;
3192 bool is_mst_root_connector = aconnector->mst_mgr.mst_state;
3193 bool result = false;
3194 enum dc_connection_type new_connection_type = dc_connection_none;
3195 struct amdgpu_device *adev = drm_to_adev(dev);
3196 union hpd_irq_data hpd_irq_data;
3197 bool link_loss = false;
3198 bool has_left_work = false;
3199 int idx = aconnector->base.index;
3200 struct hpd_rx_irq_offload_work_queue *offload_wq = &adev->dm.hpd_rx_offload_wq[idx];
3202 memset(&hpd_irq_data, 0, sizeof(hpd_irq_data));
3204 if (adev->dm.disable_hpd_irq)
3208 * TODO:Temporary add mutex to protect hpd interrupt not have a gpio
3209 * conflict, after implement i2c helper, this mutex should be
3212 mutex_lock(&aconnector->hpd_lock);
3214 result = dc_link_handle_hpd_rx_irq(dc_link, &hpd_irq_data,
3215 &link_loss, true, &has_left_work);
3220 if (hpd_irq_data.bytes.device_service_irq.bits.AUTOMATED_TEST) {
3221 schedule_hpd_rx_offload_work(offload_wq, hpd_irq_data);
3225 if (dc_link_dp_allow_hpd_rx_irq(dc_link)) {
3226 if (hpd_irq_data.bytes.device_service_irq.bits.UP_REQ_MSG_RDY ||
3227 hpd_irq_data.bytes.device_service_irq.bits.DOWN_REP_MSG_RDY) {
3228 dm_handle_mst_sideband_msg(aconnector);
3235 spin_lock(&offload_wq->offload_lock);
3236 skip = offload_wq->is_handling_link_loss;
3239 offload_wq->is_handling_link_loss = true;
3241 spin_unlock(&offload_wq->offload_lock);
3244 schedule_hpd_rx_offload_work(offload_wq, hpd_irq_data);
3251 if (result && !is_mst_root_connector) {
3252 /* Downstream Port status changed. */
3253 if (!dc_link_detect_sink(dc_link, &new_connection_type))
3254 DRM_ERROR("KMS: Failed to detect connector\n");
3256 if (aconnector->base.force && new_connection_type == dc_connection_none) {
3257 emulated_link_detect(dc_link);
3259 if (aconnector->fake_enable)
3260 aconnector->fake_enable = false;
3262 amdgpu_dm_update_connector_after_detect(aconnector);
3265 drm_modeset_lock_all(dev);
3266 dm_restore_drm_connector_state(dev, connector);
3267 drm_modeset_unlock_all(dev);
3269 drm_kms_helper_connector_hotplug_event(connector);
3270 } else if (dc_link_detect(dc_link, DETECT_REASON_HPDRX)) {
3272 if (aconnector->fake_enable)
3273 aconnector->fake_enable = false;
3275 amdgpu_dm_update_connector_after_detect(aconnector);
3278 drm_modeset_lock_all(dev);
3279 dm_restore_drm_connector_state(dev, connector);
3280 drm_modeset_unlock_all(dev);
3282 drm_kms_helper_connector_hotplug_event(connector);
3285 #ifdef CONFIG_DRM_AMD_DC_HDCP
3286 if (hpd_irq_data.bytes.device_service_irq.bits.CP_IRQ) {
3287 if (adev->dm.hdcp_workqueue)
3288 hdcp_handle_cpirq(adev->dm.hdcp_workqueue, aconnector->base.index);
3292 if (dc_link->type != dc_connection_mst_branch)
3293 drm_dp_cec_irq(&aconnector->dm_dp_aux.aux);
3295 mutex_unlock(&aconnector->hpd_lock);
3298 static void register_hpd_handlers(struct amdgpu_device *adev)
3300 struct drm_device *dev = adev_to_drm(adev);
3301 struct drm_connector *connector;
3302 struct amdgpu_dm_connector *aconnector;
3303 const struct dc_link *dc_link;
3304 struct dc_interrupt_params int_params = {0};
3306 int_params.requested_polarity = INTERRUPT_POLARITY_DEFAULT;
3307 int_params.current_polarity = INTERRUPT_POLARITY_DEFAULT;
3309 list_for_each_entry(connector,
3310 &dev->mode_config.connector_list, head) {
3312 aconnector = to_amdgpu_dm_connector(connector);
3313 dc_link = aconnector->dc_link;
3315 if (DC_IRQ_SOURCE_INVALID != dc_link->irq_source_hpd) {
3316 int_params.int_context = INTERRUPT_LOW_IRQ_CONTEXT;
3317 int_params.irq_source = dc_link->irq_source_hpd;
3319 amdgpu_dm_irq_register_interrupt(adev, &int_params,
3321 (void *) aconnector);
3324 if (DC_IRQ_SOURCE_INVALID != dc_link->irq_source_hpd_rx) {
3326 /* Also register for DP short pulse (hpd_rx). */
3327 int_params.int_context = INTERRUPT_LOW_IRQ_CONTEXT;
3328 int_params.irq_source = dc_link->irq_source_hpd_rx;
3330 amdgpu_dm_irq_register_interrupt(adev, &int_params,
3332 (void *) aconnector);
3334 if (adev->dm.hpd_rx_offload_wq)
3335 adev->dm.hpd_rx_offload_wq[connector->index].aconnector =
3341 #if defined(CONFIG_DRM_AMD_DC_SI)
3342 /* Register IRQ sources and initialize IRQ callbacks */
3343 static int dce60_register_irq_handlers(struct amdgpu_device *adev)
3345 struct dc *dc = adev->dm.dc;
3346 struct common_irq_params *c_irq_params;
3347 struct dc_interrupt_params int_params = {0};
3350 unsigned client_id = AMDGPU_IRQ_CLIENTID_LEGACY;
3352 int_params.requested_polarity = INTERRUPT_POLARITY_DEFAULT;
3353 int_params.current_polarity = INTERRUPT_POLARITY_DEFAULT;
3356 * Actions of amdgpu_irq_add_id():
3357 * 1. Register a set() function with base driver.
3358 * Base driver will call set() function to enable/disable an
3359 * interrupt in DC hardware.
3360 * 2. Register amdgpu_dm_irq_handler().
3361 * Base driver will call amdgpu_dm_irq_handler() for ALL interrupts
3362 * coming from DC hardware.
3363 * amdgpu_dm_irq_handler() will re-direct the interrupt to DC
3364 * for acknowledging and handling. */
3366 /* Use VBLANK interrupt */
3367 for (i = 0; i < adev->mode_info.num_crtc; i++) {
3368 r = amdgpu_irq_add_id(adev, client_id, i+1 , &adev->crtc_irq);
3370 DRM_ERROR("Failed to add crtc irq id!\n");
3374 int_params.int_context = INTERRUPT_HIGH_IRQ_CONTEXT;
3375 int_params.irq_source =
3376 dc_interrupt_to_irq_source(dc, i+1 , 0);
3378 c_irq_params = &adev->dm.vblank_params[int_params.irq_source - DC_IRQ_SOURCE_VBLANK1];
3380 c_irq_params->adev = adev;
3381 c_irq_params->irq_src = int_params.irq_source;
3383 amdgpu_dm_irq_register_interrupt(adev, &int_params,
3384 dm_crtc_high_irq, c_irq_params);
3387 /* Use GRPH_PFLIP interrupt */
3388 for (i = VISLANDS30_IV_SRCID_D1_GRPH_PFLIP;
3389 i <= VISLANDS30_IV_SRCID_D6_GRPH_PFLIP; i += 2) {
3390 r = amdgpu_irq_add_id(adev, client_id, i, &adev->pageflip_irq);
3392 DRM_ERROR("Failed to add page flip irq id!\n");
3396 int_params.int_context = INTERRUPT_HIGH_IRQ_CONTEXT;
3397 int_params.irq_source =
3398 dc_interrupt_to_irq_source(dc, i, 0);
3400 c_irq_params = &adev->dm.pflip_params[int_params.irq_source - DC_IRQ_SOURCE_PFLIP_FIRST];
3402 c_irq_params->adev = adev;
3403 c_irq_params->irq_src = int_params.irq_source;
3405 amdgpu_dm_irq_register_interrupt(adev, &int_params,
3406 dm_pflip_high_irq, c_irq_params);
3411 r = amdgpu_irq_add_id(adev, client_id,
3412 VISLANDS30_IV_SRCID_HOTPLUG_DETECT_A, &adev->hpd_irq);
3414 DRM_ERROR("Failed to add hpd irq id!\n");
3418 register_hpd_handlers(adev);
3424 /* Register IRQ sources and initialize IRQ callbacks */
3425 static int dce110_register_irq_handlers(struct amdgpu_device *adev)
3427 struct dc *dc = adev->dm.dc;
3428 struct common_irq_params *c_irq_params;
3429 struct dc_interrupt_params int_params = {0};
3432 unsigned client_id = AMDGPU_IRQ_CLIENTID_LEGACY;
3434 if (adev->family >= AMDGPU_FAMILY_AI)
3435 client_id = SOC15_IH_CLIENTID_DCE;
3437 int_params.requested_polarity = INTERRUPT_POLARITY_DEFAULT;
3438 int_params.current_polarity = INTERRUPT_POLARITY_DEFAULT;
3441 * Actions of amdgpu_irq_add_id():
3442 * 1. Register a set() function with base driver.
3443 * Base driver will call set() function to enable/disable an
3444 * interrupt in DC hardware.
3445 * 2. Register amdgpu_dm_irq_handler().
3446 * Base driver will call amdgpu_dm_irq_handler() for ALL interrupts
3447 * coming from DC hardware.
3448 * amdgpu_dm_irq_handler() will re-direct the interrupt to DC
3449 * for acknowledging and handling. */
3451 /* Use VBLANK interrupt */
3452 for (i = VISLANDS30_IV_SRCID_D1_VERTICAL_INTERRUPT0; i <= VISLANDS30_IV_SRCID_D6_VERTICAL_INTERRUPT0; i++) {
3453 r = amdgpu_irq_add_id(adev, client_id, i, &adev->crtc_irq);
3455 DRM_ERROR("Failed to add crtc irq id!\n");
3459 int_params.int_context = INTERRUPT_HIGH_IRQ_CONTEXT;
3460 int_params.irq_source =
3461 dc_interrupt_to_irq_source(dc, i, 0);
3463 c_irq_params = &adev->dm.vblank_params[int_params.irq_source - DC_IRQ_SOURCE_VBLANK1];
3465 c_irq_params->adev = adev;
3466 c_irq_params->irq_src = int_params.irq_source;
3468 amdgpu_dm_irq_register_interrupt(adev, &int_params,
3469 dm_crtc_high_irq, c_irq_params);
3472 /* Use VUPDATE interrupt */
3473 for (i = VISLANDS30_IV_SRCID_D1_V_UPDATE_INT; i <= VISLANDS30_IV_SRCID_D6_V_UPDATE_INT; i += 2) {
3474 r = amdgpu_irq_add_id(adev, client_id, i, &adev->vupdate_irq);
3476 DRM_ERROR("Failed to add vupdate irq id!\n");
3480 int_params.int_context = INTERRUPT_HIGH_IRQ_CONTEXT;
3481 int_params.irq_source =
3482 dc_interrupt_to_irq_source(dc, i, 0);
3484 c_irq_params = &adev->dm.vupdate_params[int_params.irq_source - DC_IRQ_SOURCE_VUPDATE1];
3486 c_irq_params->adev = adev;
3487 c_irq_params->irq_src = int_params.irq_source;
3489 amdgpu_dm_irq_register_interrupt(adev, &int_params,
3490 dm_vupdate_high_irq, c_irq_params);
3493 /* Use GRPH_PFLIP interrupt */
3494 for (i = VISLANDS30_IV_SRCID_D1_GRPH_PFLIP;
3495 i <= VISLANDS30_IV_SRCID_D6_GRPH_PFLIP; i += 2) {
3496 r = amdgpu_irq_add_id(adev, client_id, i, &adev->pageflip_irq);
3498 DRM_ERROR("Failed to add page flip irq id!\n");
3502 int_params.int_context = INTERRUPT_HIGH_IRQ_CONTEXT;
3503 int_params.irq_source =
3504 dc_interrupt_to_irq_source(dc, i, 0);
3506 c_irq_params = &adev->dm.pflip_params[int_params.irq_source - DC_IRQ_SOURCE_PFLIP_FIRST];
3508 c_irq_params->adev = adev;
3509 c_irq_params->irq_src = int_params.irq_source;
3511 amdgpu_dm_irq_register_interrupt(adev, &int_params,
3512 dm_pflip_high_irq, c_irq_params);
3517 r = amdgpu_irq_add_id(adev, client_id,
3518 VISLANDS30_IV_SRCID_HOTPLUG_DETECT_A, &adev->hpd_irq);
3520 DRM_ERROR("Failed to add hpd irq id!\n");
3524 register_hpd_handlers(adev);
3529 #if defined(CONFIG_DRM_AMD_DC_DCN)
3530 /* Register IRQ sources and initialize IRQ callbacks */
3531 static int dcn10_register_irq_handlers(struct amdgpu_device *adev)
3533 struct dc *dc = adev->dm.dc;
3534 struct common_irq_params *c_irq_params;
3535 struct dc_interrupt_params int_params = {0};
3538 #if defined(CONFIG_DRM_AMD_SECURE_DISPLAY)
3539 static const unsigned int vrtl_int_srcid[] = {
3540 DCN_1_0__SRCID__OTG1_VERTICAL_INTERRUPT0_CONTROL,
3541 DCN_1_0__SRCID__OTG2_VERTICAL_INTERRUPT0_CONTROL,
3542 DCN_1_0__SRCID__OTG3_VERTICAL_INTERRUPT0_CONTROL,
3543 DCN_1_0__SRCID__OTG4_VERTICAL_INTERRUPT0_CONTROL,
3544 DCN_1_0__SRCID__OTG5_VERTICAL_INTERRUPT0_CONTROL,
3545 DCN_1_0__SRCID__OTG6_VERTICAL_INTERRUPT0_CONTROL
3549 int_params.requested_polarity = INTERRUPT_POLARITY_DEFAULT;
3550 int_params.current_polarity = INTERRUPT_POLARITY_DEFAULT;
3553 * Actions of amdgpu_irq_add_id():
3554 * 1. Register a set() function with base driver.
3555 * Base driver will call set() function to enable/disable an
3556 * interrupt in DC hardware.
3557 * 2. Register amdgpu_dm_irq_handler().
3558 * Base driver will call amdgpu_dm_irq_handler() for ALL interrupts
3559 * coming from DC hardware.
3560 * amdgpu_dm_irq_handler() will re-direct the interrupt to DC
3561 * for acknowledging and handling.
3564 /* Use VSTARTUP interrupt */
3565 for (i = DCN_1_0__SRCID__DC_D1_OTG_VSTARTUP;
3566 i <= DCN_1_0__SRCID__DC_D1_OTG_VSTARTUP + adev->mode_info.num_crtc - 1;
3568 r = amdgpu_irq_add_id(adev, SOC15_IH_CLIENTID_DCE, i, &adev->crtc_irq);
3571 DRM_ERROR("Failed to add crtc irq id!\n");
3575 int_params.int_context = INTERRUPT_HIGH_IRQ_CONTEXT;
3576 int_params.irq_source =
3577 dc_interrupt_to_irq_source(dc, i, 0);
3579 c_irq_params = &adev->dm.vblank_params[int_params.irq_source - DC_IRQ_SOURCE_VBLANK1];
3581 c_irq_params->adev = adev;
3582 c_irq_params->irq_src = int_params.irq_source;
3584 amdgpu_dm_irq_register_interrupt(
3585 adev, &int_params, dm_crtc_high_irq, c_irq_params);
3588 /* Use otg vertical line interrupt */
3589 #if defined(CONFIG_DRM_AMD_SECURE_DISPLAY)
3590 for (i = 0; i <= adev->mode_info.num_crtc - 1; i++) {
3591 r = amdgpu_irq_add_id(adev, SOC15_IH_CLIENTID_DCE,
3592 vrtl_int_srcid[i], &adev->vline0_irq);
3595 DRM_ERROR("Failed to add vline0 irq id!\n");
3599 int_params.int_context = INTERRUPT_HIGH_IRQ_CONTEXT;
3600 int_params.irq_source =
3601 dc_interrupt_to_irq_source(dc, vrtl_int_srcid[i], 0);
3603 if (int_params.irq_source == DC_IRQ_SOURCE_INVALID) {
3604 DRM_ERROR("Failed to register vline0 irq %d!\n", vrtl_int_srcid[i]);
3608 c_irq_params = &adev->dm.vline0_params[int_params.irq_source
3609 - DC_IRQ_SOURCE_DC1_VLINE0];
3611 c_irq_params->adev = adev;
3612 c_irq_params->irq_src = int_params.irq_source;
3614 amdgpu_dm_irq_register_interrupt(adev, &int_params,
3615 dm_dcn_vertical_interrupt0_high_irq, c_irq_params);
3619 /* Use VUPDATE_NO_LOCK interrupt on DCN, which seems to correspond to
3620 * the regular VUPDATE interrupt on DCE. We want DC_IRQ_SOURCE_VUPDATEx
3621 * to trigger at end of each vblank, regardless of state of the lock,
3622 * matching DCE behaviour.
3624 for (i = DCN_1_0__SRCID__OTG0_IHC_V_UPDATE_NO_LOCK_INTERRUPT;
3625 i <= DCN_1_0__SRCID__OTG0_IHC_V_UPDATE_NO_LOCK_INTERRUPT + adev->mode_info.num_crtc - 1;
3627 r = amdgpu_irq_add_id(adev, SOC15_IH_CLIENTID_DCE, i, &adev->vupdate_irq);
3630 DRM_ERROR("Failed to add vupdate irq id!\n");
3634 int_params.int_context = INTERRUPT_HIGH_IRQ_CONTEXT;
3635 int_params.irq_source =
3636 dc_interrupt_to_irq_source(dc, i, 0);
3638 c_irq_params = &adev->dm.vupdate_params[int_params.irq_source - DC_IRQ_SOURCE_VUPDATE1];
3640 c_irq_params->adev = adev;
3641 c_irq_params->irq_src = int_params.irq_source;
3643 amdgpu_dm_irq_register_interrupt(adev, &int_params,
3644 dm_vupdate_high_irq, c_irq_params);
3647 /* Use GRPH_PFLIP interrupt */
3648 for (i = DCN_1_0__SRCID__HUBP0_FLIP_INTERRUPT;
3649 i <= DCN_1_0__SRCID__HUBP0_FLIP_INTERRUPT + dc->caps.max_otg_num - 1;
3651 r = amdgpu_irq_add_id(adev, SOC15_IH_CLIENTID_DCE, i, &adev->pageflip_irq);
3653 DRM_ERROR("Failed to add page flip irq id!\n");
3657 int_params.int_context = INTERRUPT_HIGH_IRQ_CONTEXT;
3658 int_params.irq_source =
3659 dc_interrupt_to_irq_source(dc, i, 0);
3661 c_irq_params = &adev->dm.pflip_params[int_params.irq_source - DC_IRQ_SOURCE_PFLIP_FIRST];
3663 c_irq_params->adev = adev;
3664 c_irq_params->irq_src = int_params.irq_source;
3666 amdgpu_dm_irq_register_interrupt(adev, &int_params,
3667 dm_pflip_high_irq, c_irq_params);
3672 r = amdgpu_irq_add_id(adev, SOC15_IH_CLIENTID_DCE, DCN_1_0__SRCID__DC_HPD1_INT,
3675 DRM_ERROR("Failed to add hpd irq id!\n");
3679 register_hpd_handlers(adev);
3683 /* Register Outbox IRQ sources and initialize IRQ callbacks */
3684 static int register_outbox_irq_handlers(struct amdgpu_device *adev)
3686 struct dc *dc = adev->dm.dc;
3687 struct common_irq_params *c_irq_params;
3688 struct dc_interrupt_params int_params = {0};
3691 int_params.requested_polarity = INTERRUPT_POLARITY_DEFAULT;
3692 int_params.current_polarity = INTERRUPT_POLARITY_DEFAULT;
3694 r = amdgpu_irq_add_id(adev, SOC15_IH_CLIENTID_DCE, DCN_1_0__SRCID__DMCUB_OUTBOX_LOW_PRIORITY_READY_INT,
3695 &adev->dmub_outbox_irq);
3697 DRM_ERROR("Failed to add outbox irq id!\n");
3701 if (dc->ctx->dmub_srv) {
3702 i = DCN_1_0__SRCID__DMCUB_OUTBOX_LOW_PRIORITY_READY_INT;
3703 int_params.int_context = INTERRUPT_LOW_IRQ_CONTEXT;
3704 int_params.irq_source =
3705 dc_interrupt_to_irq_source(dc, i, 0);
3707 c_irq_params = &adev->dm.dmub_outbox_params[0];
3709 c_irq_params->adev = adev;
3710 c_irq_params->irq_src = int_params.irq_source;
3712 amdgpu_dm_irq_register_interrupt(adev, &int_params,
3713 dm_dmub_outbox1_low_irq, c_irq_params);
3721 * Acquires the lock for the atomic state object and returns
3722 * the new atomic state.
3724 * This should only be called during atomic check.
3726 static int dm_atomic_get_state(struct drm_atomic_state *state,
3727 struct dm_atomic_state **dm_state)
3729 struct drm_device *dev = state->dev;
3730 struct amdgpu_device *adev = drm_to_adev(dev);
3731 struct amdgpu_display_manager *dm = &adev->dm;
3732 struct drm_private_state *priv_state;
3737 priv_state = drm_atomic_get_private_obj_state(state, &dm->atomic_obj);
3738 if (IS_ERR(priv_state))
3739 return PTR_ERR(priv_state);
3741 *dm_state = to_dm_atomic_state(priv_state);
3746 static struct dm_atomic_state *
3747 dm_atomic_get_new_state(struct drm_atomic_state *state)
3749 struct drm_device *dev = state->dev;
3750 struct amdgpu_device *adev = drm_to_adev(dev);
3751 struct amdgpu_display_manager *dm = &adev->dm;
3752 struct drm_private_obj *obj;
3753 struct drm_private_state *new_obj_state;
3756 for_each_new_private_obj_in_state(state, obj, new_obj_state, i) {
3757 if (obj->funcs == dm->atomic_obj.funcs)
3758 return to_dm_atomic_state(new_obj_state);
3764 static struct drm_private_state *
3765 dm_atomic_duplicate_state(struct drm_private_obj *obj)
3767 struct dm_atomic_state *old_state, *new_state;
3769 new_state = kzalloc(sizeof(*new_state), GFP_KERNEL);
3773 __drm_atomic_helper_private_obj_duplicate_state(obj, &new_state->base);
3775 old_state = to_dm_atomic_state(obj->state);
3777 if (old_state && old_state->context)
3778 new_state->context = dc_copy_state(old_state->context);
3780 if (!new_state->context) {
3785 return &new_state->base;
3788 static void dm_atomic_destroy_state(struct drm_private_obj *obj,
3789 struct drm_private_state *state)
3791 struct dm_atomic_state *dm_state = to_dm_atomic_state(state);
3793 if (dm_state && dm_state->context)
3794 dc_release_state(dm_state->context);
3799 static struct drm_private_state_funcs dm_atomic_state_funcs = {
3800 .atomic_duplicate_state = dm_atomic_duplicate_state,
3801 .atomic_destroy_state = dm_atomic_destroy_state,
3804 static int amdgpu_dm_mode_config_init(struct amdgpu_device *adev)
3806 struct dm_atomic_state *state;
3809 adev->mode_info.mode_config_initialized = true;
3811 adev_to_drm(adev)->mode_config.funcs = (void *)&amdgpu_dm_mode_funcs;
3812 adev_to_drm(adev)->mode_config.helper_private = &amdgpu_dm_mode_config_helperfuncs;
3814 adev_to_drm(adev)->mode_config.max_width = 16384;
3815 adev_to_drm(adev)->mode_config.max_height = 16384;
3817 adev_to_drm(adev)->mode_config.preferred_depth = 24;
3818 adev_to_drm(adev)->mode_config.prefer_shadow = 1;
3819 /* indicates support for immediate flip */
3820 adev_to_drm(adev)->mode_config.async_page_flip = true;
3822 adev_to_drm(adev)->mode_config.fb_base = adev->gmc.aper_base;
3824 state = kzalloc(sizeof(*state), GFP_KERNEL);
3828 state->context = dc_create_state(adev->dm.dc);
3829 if (!state->context) {
3834 dc_resource_state_copy_construct_current(adev->dm.dc, state->context);
3836 drm_atomic_private_obj_init(adev_to_drm(adev),
3837 &adev->dm.atomic_obj,
3839 &dm_atomic_state_funcs);
3841 r = amdgpu_display_modeset_create_props(adev);
3843 dc_release_state(state->context);
3848 r = amdgpu_dm_audio_init(adev);
3850 dc_release_state(state->context);
3858 #define AMDGPU_DM_DEFAULT_MIN_BACKLIGHT 12
3859 #define AMDGPU_DM_DEFAULT_MAX_BACKLIGHT 255
3860 #define AUX_BL_DEFAULT_TRANSITION_TIME_MS 50
3862 #if defined(CONFIG_BACKLIGHT_CLASS_DEVICE) ||\
3863 defined(CONFIG_BACKLIGHT_CLASS_DEVICE_MODULE)
3865 static void amdgpu_dm_update_backlight_caps(struct amdgpu_display_manager *dm,
3868 #if defined(CONFIG_ACPI)
3869 struct amdgpu_dm_backlight_caps caps;
3871 memset(&caps, 0, sizeof(caps));
3873 if (dm->backlight_caps[bl_idx].caps_valid)
3876 amdgpu_acpi_get_backlight_caps(&caps);
3877 if (caps.caps_valid) {
3878 dm->backlight_caps[bl_idx].caps_valid = true;
3879 if (caps.aux_support)
3881 dm->backlight_caps[bl_idx].min_input_signal = caps.min_input_signal;
3882 dm->backlight_caps[bl_idx].max_input_signal = caps.max_input_signal;
3884 dm->backlight_caps[bl_idx].min_input_signal =
3885 AMDGPU_DM_DEFAULT_MIN_BACKLIGHT;
3886 dm->backlight_caps[bl_idx].max_input_signal =
3887 AMDGPU_DM_DEFAULT_MAX_BACKLIGHT;
3890 if (dm->backlight_caps[bl_idx].aux_support)
3893 dm->backlight_caps[bl_idx].min_input_signal = AMDGPU_DM_DEFAULT_MIN_BACKLIGHT;
3894 dm->backlight_caps[bl_idx].max_input_signal = AMDGPU_DM_DEFAULT_MAX_BACKLIGHT;
3898 static int get_brightness_range(const struct amdgpu_dm_backlight_caps *caps,
3899 unsigned *min, unsigned *max)
3904 if (caps->aux_support) {
3905 // Firmware limits are in nits, DC API wants millinits.
3906 *max = 1000 * caps->aux_max_input_signal;
3907 *min = 1000 * caps->aux_min_input_signal;
3909 // Firmware limits are 8-bit, PWM control is 16-bit.
3910 *max = 0x101 * caps->max_input_signal;
3911 *min = 0x101 * caps->min_input_signal;
3916 static u32 convert_brightness_from_user(const struct amdgpu_dm_backlight_caps *caps,
3917 uint32_t brightness)
3921 if (!get_brightness_range(caps, &min, &max))
3924 // Rescale 0..255 to min..max
3925 return min + DIV_ROUND_CLOSEST((max - min) * brightness,
3926 AMDGPU_MAX_BL_LEVEL);
3929 static u32 convert_brightness_to_user(const struct amdgpu_dm_backlight_caps *caps,
3930 uint32_t brightness)
3934 if (!get_brightness_range(caps, &min, &max))
3937 if (brightness < min)
3939 // Rescale min..max to 0..255
3940 return DIV_ROUND_CLOSEST(AMDGPU_MAX_BL_LEVEL * (brightness - min),
3944 static int amdgpu_dm_backlight_set_level(struct amdgpu_display_manager *dm,
3946 u32 user_brightness)
3948 struct amdgpu_dm_backlight_caps caps;
3949 struct dc_link *link;
3953 amdgpu_dm_update_backlight_caps(dm, bl_idx);
3954 caps = dm->backlight_caps[bl_idx];
3956 dm->brightness[bl_idx] = user_brightness;
3957 /* update scratch register */
3959 amdgpu_atombios_scratch_regs_set_backlight_level(dm->adev, dm->brightness[bl_idx]);
3960 brightness = convert_brightness_from_user(&caps, dm->brightness[bl_idx]);
3961 link = (struct dc_link *)dm->backlight_link[bl_idx];
3963 /* Change brightness based on AUX property */
3964 if (caps.aux_support) {
3965 rc = dc_link_set_backlight_level_nits(link, true, brightness,
3966 AUX_BL_DEFAULT_TRANSITION_TIME_MS);
3968 DRM_DEBUG("DM: Failed to update backlight via AUX on eDP[%d]\n", bl_idx);
3970 rc = dc_link_set_backlight_level(link, brightness, 0);
3972 DRM_DEBUG("DM: Failed to update backlight on eDP[%d]\n", bl_idx);
3978 static int amdgpu_dm_backlight_update_status(struct backlight_device *bd)
3980 struct amdgpu_display_manager *dm = bl_get_data(bd);
3983 for (i = 0; i < dm->num_of_edps; i++) {
3984 if (bd == dm->backlight_dev[i])
3987 if (i >= AMDGPU_DM_MAX_NUM_EDP)
3989 amdgpu_dm_backlight_set_level(dm, i, bd->props.brightness);
3994 static u32 amdgpu_dm_backlight_get_level(struct amdgpu_display_manager *dm,
3997 struct amdgpu_dm_backlight_caps caps;
3998 struct dc_link *link = (struct dc_link *)dm->backlight_link[bl_idx];
4000 amdgpu_dm_update_backlight_caps(dm, bl_idx);
4001 caps = dm->backlight_caps[bl_idx];
4003 if (caps.aux_support) {
4007 rc = dc_link_get_backlight_level_nits(link, &avg, &peak);
4009 return dm->brightness[bl_idx];
4010 return convert_brightness_to_user(&caps, avg);
4012 int ret = dc_link_get_backlight_level(link);
4014 if (ret == DC_ERROR_UNEXPECTED)
4015 return dm->brightness[bl_idx];
4016 return convert_brightness_to_user(&caps, ret);
4020 static int amdgpu_dm_backlight_get_brightness(struct backlight_device *bd)
4022 struct amdgpu_display_manager *dm = bl_get_data(bd);
4025 for (i = 0; i < dm->num_of_edps; i++) {
4026 if (bd == dm->backlight_dev[i])
4029 if (i >= AMDGPU_DM_MAX_NUM_EDP)
4031 return amdgpu_dm_backlight_get_level(dm, i);
4034 static const struct backlight_ops amdgpu_dm_backlight_ops = {
4035 .options = BL_CORE_SUSPENDRESUME,
4036 .get_brightness = amdgpu_dm_backlight_get_brightness,
4037 .update_status = amdgpu_dm_backlight_update_status,
4041 amdgpu_dm_register_backlight_device(struct amdgpu_display_manager *dm)
4044 struct backlight_properties props = { 0 };
4046 amdgpu_dm_update_backlight_caps(dm, dm->num_of_edps);
4047 dm->brightness[dm->num_of_edps] = AMDGPU_MAX_BL_LEVEL;
4049 props.max_brightness = AMDGPU_MAX_BL_LEVEL;
4050 props.brightness = AMDGPU_MAX_BL_LEVEL;
4051 props.type = BACKLIGHT_RAW;
4053 snprintf(bl_name, sizeof(bl_name), "amdgpu_bl%d",
4054 adev_to_drm(dm->adev)->primary->index + dm->num_of_edps);
4056 dm->backlight_dev[dm->num_of_edps] = backlight_device_register(bl_name,
4057 adev_to_drm(dm->adev)->dev,
4059 &amdgpu_dm_backlight_ops,
4062 if (IS_ERR(dm->backlight_dev[dm->num_of_edps]))
4063 DRM_ERROR("DM: Backlight registration failed!\n");
4065 DRM_DEBUG_DRIVER("DM: Registered Backlight device: %s\n", bl_name);
4069 static int initialize_plane(struct amdgpu_display_manager *dm,
4070 struct amdgpu_mode_info *mode_info, int plane_id,
4071 enum drm_plane_type plane_type,
4072 const struct dc_plane_cap *plane_cap)
4074 struct drm_plane *plane;
4075 unsigned long possible_crtcs;
4078 plane = kzalloc(sizeof(struct drm_plane), GFP_KERNEL);
4080 DRM_ERROR("KMS: Failed to allocate plane\n");
4083 plane->type = plane_type;
4086 * HACK: IGT tests expect that the primary plane for a CRTC
4087 * can only have one possible CRTC. Only expose support for
4088 * any CRTC if they're not going to be used as a primary plane
4089 * for a CRTC - like overlay or underlay planes.
4091 possible_crtcs = 1 << plane_id;
4092 if (plane_id >= dm->dc->caps.max_streams)
4093 possible_crtcs = 0xff;
4095 ret = amdgpu_dm_plane_init(dm, plane, possible_crtcs, plane_cap);
4098 DRM_ERROR("KMS: Failed to initialize plane\n");
4104 mode_info->planes[plane_id] = plane;
4110 static void register_backlight_device(struct amdgpu_display_manager *dm,
4111 struct dc_link *link)
4113 #if defined(CONFIG_BACKLIGHT_CLASS_DEVICE) ||\
4114 defined(CONFIG_BACKLIGHT_CLASS_DEVICE_MODULE)
4116 if ((link->connector_signal & (SIGNAL_TYPE_EDP | SIGNAL_TYPE_LVDS)) &&
4117 link->type != dc_connection_none) {
4119 * Event if registration failed, we should continue with
4120 * DM initialization because not having a backlight control
4121 * is better then a black screen.
4123 if (!dm->backlight_dev[dm->num_of_edps])
4124 amdgpu_dm_register_backlight_device(dm);
4126 if (dm->backlight_dev[dm->num_of_edps]) {
4127 dm->backlight_link[dm->num_of_edps] = link;
4136 * In this architecture, the association
4137 * connector -> encoder -> crtc
4138 * id not really requried. The crtc and connector will hold the
4139 * display_index as an abstraction to use with DAL component
4141 * Returns 0 on success
4143 static int amdgpu_dm_initialize_drm_device(struct amdgpu_device *adev)
4145 struct amdgpu_display_manager *dm = &adev->dm;
4147 struct amdgpu_dm_connector *aconnector = NULL;
4148 struct amdgpu_encoder *aencoder = NULL;
4149 struct amdgpu_mode_info *mode_info = &adev->mode_info;
4151 int32_t primary_planes;
4152 enum dc_connection_type new_connection_type = dc_connection_none;
4153 const struct dc_plane_cap *plane;
4154 bool psr_feature_enabled = false;
4156 dm->display_indexes_num = dm->dc->caps.max_streams;
4157 /* Update the actual used number of crtc */
4158 adev->mode_info.num_crtc = adev->dm.display_indexes_num;
4160 link_cnt = dm->dc->caps.max_links;
4161 if (amdgpu_dm_mode_config_init(dm->adev)) {
4162 DRM_ERROR("DM: Failed to initialize mode config\n");
4166 /* There is one primary plane per CRTC */
4167 primary_planes = dm->dc->caps.max_streams;
4168 ASSERT(primary_planes <= AMDGPU_MAX_PLANES);
4171 * Initialize primary planes, implicit planes for legacy IOCTLS.
4172 * Order is reversed to match iteration order in atomic check.
4174 for (i = (primary_planes - 1); i >= 0; i--) {
4175 plane = &dm->dc->caps.planes[i];
4177 if (initialize_plane(dm, mode_info, i,
4178 DRM_PLANE_TYPE_PRIMARY, plane)) {
4179 DRM_ERROR("KMS: Failed to initialize primary plane\n");
4185 * Initialize overlay planes, index starting after primary planes.
4186 * These planes have a higher DRM index than the primary planes since
4187 * they should be considered as having a higher z-order.
4188 * Order is reversed to match iteration order in atomic check.
4190 * Only support DCN for now, and only expose one so we don't encourage
4191 * userspace to use up all the pipes.
4193 for (i = 0; i < dm->dc->caps.max_planes; ++i) {
4194 struct dc_plane_cap *plane = &dm->dc->caps.planes[i];
4196 if (plane->type != DC_PLANE_TYPE_DCN_UNIVERSAL)
4199 if (!plane->blends_with_above || !plane->blends_with_below)
4202 if (!plane->pixel_format_support.argb8888)
4205 if (initialize_plane(dm, NULL, primary_planes + i,
4206 DRM_PLANE_TYPE_OVERLAY, plane)) {
4207 DRM_ERROR("KMS: Failed to initialize overlay plane\n");
4211 /* Only create one overlay plane. */
4215 for (i = 0; i < dm->dc->caps.max_streams; i++)
4216 if (amdgpu_dm_crtc_init(dm, mode_info->planes[i], i)) {
4217 DRM_ERROR("KMS: Failed to initialize crtc\n");
4221 #if defined(CONFIG_DRM_AMD_DC_DCN)
4222 /* Use Outbox interrupt */
4223 switch (adev->ip_versions[DCE_HWIP][0]) {
4224 case IP_VERSION(3, 0, 0):
4225 case IP_VERSION(3, 1, 2):
4226 case IP_VERSION(3, 1, 3):
4227 case IP_VERSION(2, 1, 0):
4228 if (register_outbox_irq_handlers(dm->adev)) {
4229 DRM_ERROR("DM: Failed to initialize IRQ\n");
4234 DRM_DEBUG_KMS("Unsupported DCN IP version for outbox: 0x%X\n",
4235 adev->ip_versions[DCE_HWIP][0]);
4238 /* Determine whether to enable PSR support by default. */
4239 if (!(amdgpu_dc_debug_mask & DC_DISABLE_PSR)) {
4240 switch (adev->ip_versions[DCE_HWIP][0]) {
4241 case IP_VERSION(3, 1, 2):
4242 case IP_VERSION(3, 1, 3):
4243 psr_feature_enabled = true;
4246 psr_feature_enabled = amdgpu_dc_feature_mask & DC_PSR_MASK;
4252 /* loops over all connectors on the board */
4253 for (i = 0; i < link_cnt; i++) {
4254 struct dc_link *link = NULL;
4256 if (i > AMDGPU_DM_MAX_DISPLAY_INDEX) {
4258 "KMS: Cannot support more than %d display indexes\n",
4259 AMDGPU_DM_MAX_DISPLAY_INDEX);
4263 aconnector = kzalloc(sizeof(*aconnector), GFP_KERNEL);
4267 aencoder = kzalloc(sizeof(*aencoder), GFP_KERNEL);
4271 if (amdgpu_dm_encoder_init(dm->ddev, aencoder, i)) {
4272 DRM_ERROR("KMS: Failed to initialize encoder\n");
4276 if (amdgpu_dm_connector_init(dm, aconnector, i, aencoder)) {
4277 DRM_ERROR("KMS: Failed to initialize connector\n");
4281 link = dc_get_link_at_index(dm->dc, i);
4283 if (!dc_link_detect_sink(link, &new_connection_type))
4284 DRM_ERROR("KMS: Failed to detect connector\n");
4286 if (aconnector->base.force && new_connection_type == dc_connection_none) {
4287 emulated_link_detect(link);
4288 amdgpu_dm_update_connector_after_detect(aconnector);
4290 } else if (dc_link_detect(link, DETECT_REASON_BOOT)) {
4291 amdgpu_dm_update_connector_after_detect(aconnector);
4292 register_backlight_device(dm, link);
4293 if (dm->num_of_edps)
4294 update_connector_ext_caps(aconnector);
4295 if (psr_feature_enabled)
4296 amdgpu_dm_set_psr_caps(link);
4303 * Disable vblank IRQs aggressively for power-saving.
4305 * TODO: Fix vblank control helpers to delay PSR entry to allow this when PSR
4306 * is also supported.
4308 adev_to_drm(adev)->vblank_disable_immediate = !psr_feature_enabled;
4310 /* Software is initialized. Now we can register interrupt handlers. */
4311 switch (adev->asic_type) {
4312 #if defined(CONFIG_DRM_AMD_DC_SI)
4317 if (dce60_register_irq_handlers(dm->adev)) {
4318 DRM_ERROR("DM: Failed to initialize IRQ\n");
4332 case CHIP_POLARIS11:
4333 case CHIP_POLARIS10:
4334 case CHIP_POLARIS12:
4339 if (dce110_register_irq_handlers(dm->adev)) {
4340 DRM_ERROR("DM: Failed to initialize IRQ\n");
4345 #if defined(CONFIG_DRM_AMD_DC_DCN)
4346 switch (adev->ip_versions[DCE_HWIP][0]) {
4347 case IP_VERSION(1, 0, 0):
4348 case IP_VERSION(1, 0, 1):
4349 case IP_VERSION(2, 0, 2):
4350 case IP_VERSION(2, 0, 3):
4351 case IP_VERSION(2, 0, 0):
4352 case IP_VERSION(2, 1, 0):
4353 case IP_VERSION(3, 0, 0):
4354 case IP_VERSION(3, 0, 2):
4355 case IP_VERSION(3, 0, 3):
4356 case IP_VERSION(3, 0, 1):
4357 case IP_VERSION(3, 1, 2):
4358 case IP_VERSION(3, 1, 3):
4359 if (dcn10_register_irq_handlers(dm->adev)) {
4360 DRM_ERROR("DM: Failed to initialize IRQ\n");
4365 DRM_ERROR("Unsupported DCE IP versions: 0x%X\n",
4366 adev->ip_versions[DCE_HWIP][0]);
4381 static void amdgpu_dm_destroy_drm_device(struct amdgpu_display_manager *dm)
4383 drm_atomic_private_obj_fini(&dm->atomic_obj);
4387 /******************************************************************************
4388 * amdgpu_display_funcs functions
4389 *****************************************************************************/
4392 * dm_bandwidth_update - program display watermarks
4394 * @adev: amdgpu_device pointer
4396 * Calculate and program the display watermarks and line buffer allocation.
4398 static void dm_bandwidth_update(struct amdgpu_device *adev)
4400 /* TODO: implement later */
4403 static const struct amdgpu_display_funcs dm_display_funcs = {
4404 .bandwidth_update = dm_bandwidth_update, /* called unconditionally */
4405 .vblank_get_counter = dm_vblank_get_counter,/* called unconditionally */
4406 .backlight_set_level = NULL, /* never called for DC */
4407 .backlight_get_level = NULL, /* never called for DC */
4408 .hpd_sense = NULL,/* called unconditionally */
4409 .hpd_set_polarity = NULL, /* called unconditionally */
4410 .hpd_get_gpio_reg = NULL, /* VBIOS parsing. DAL does it. */
4411 .page_flip_get_scanoutpos =
4412 dm_crtc_get_scanoutpos,/* called unconditionally */
4413 .add_encoder = NULL, /* VBIOS parsing. DAL does it. */
4414 .add_connector = NULL, /* VBIOS parsing. DAL does it. */
4417 #if defined(CONFIG_DEBUG_KERNEL_DC)
4419 static ssize_t s3_debug_store(struct device *device,
4420 struct device_attribute *attr,
4426 struct drm_device *drm_dev = dev_get_drvdata(device);
4427 struct amdgpu_device *adev = drm_to_adev(drm_dev);
4429 ret = kstrtoint(buf, 0, &s3_state);
4434 drm_kms_helper_hotplug_event(adev_to_drm(adev));
4439 return ret == 0 ? count : 0;
4442 DEVICE_ATTR_WO(s3_debug);
4446 static int dm_early_init(void *handle)
4448 struct amdgpu_device *adev = (struct amdgpu_device *)handle;
4450 switch (adev->asic_type) {
4451 #if defined(CONFIG_DRM_AMD_DC_SI)
4455 adev->mode_info.num_crtc = 6;
4456 adev->mode_info.num_hpd = 6;
4457 adev->mode_info.num_dig = 6;
4460 adev->mode_info.num_crtc = 2;
4461 adev->mode_info.num_hpd = 2;
4462 adev->mode_info.num_dig = 2;
4467 adev->mode_info.num_crtc = 6;
4468 adev->mode_info.num_hpd = 6;
4469 adev->mode_info.num_dig = 6;
4472 adev->mode_info.num_crtc = 4;
4473 adev->mode_info.num_hpd = 6;
4474 adev->mode_info.num_dig = 7;
4478 adev->mode_info.num_crtc = 2;
4479 adev->mode_info.num_hpd = 6;
4480 adev->mode_info.num_dig = 6;
4484 adev->mode_info.num_crtc = 6;
4485 adev->mode_info.num_hpd = 6;
4486 adev->mode_info.num_dig = 7;
4489 adev->mode_info.num_crtc = 3;
4490 adev->mode_info.num_hpd = 6;
4491 adev->mode_info.num_dig = 9;
4494 adev->mode_info.num_crtc = 2;
4495 adev->mode_info.num_hpd = 6;
4496 adev->mode_info.num_dig = 9;
4498 case CHIP_POLARIS11:
4499 case CHIP_POLARIS12:
4500 adev->mode_info.num_crtc = 5;
4501 adev->mode_info.num_hpd = 5;
4502 adev->mode_info.num_dig = 5;
4504 case CHIP_POLARIS10:
4506 adev->mode_info.num_crtc = 6;
4507 adev->mode_info.num_hpd = 6;
4508 adev->mode_info.num_dig = 6;
4513 adev->mode_info.num_crtc = 6;
4514 adev->mode_info.num_hpd = 6;
4515 adev->mode_info.num_dig = 6;
4518 #if defined(CONFIG_DRM_AMD_DC_DCN)
4519 switch (adev->ip_versions[DCE_HWIP][0]) {
4520 case IP_VERSION(2, 0, 2):
4521 case IP_VERSION(3, 0, 0):
4522 adev->mode_info.num_crtc = 6;
4523 adev->mode_info.num_hpd = 6;
4524 adev->mode_info.num_dig = 6;
4526 case IP_VERSION(2, 0, 0):
4527 case IP_VERSION(3, 0, 2):
4528 adev->mode_info.num_crtc = 5;
4529 adev->mode_info.num_hpd = 5;
4530 adev->mode_info.num_dig = 5;
4532 case IP_VERSION(2, 0, 3):
4533 case IP_VERSION(3, 0, 3):
4534 adev->mode_info.num_crtc = 2;
4535 adev->mode_info.num_hpd = 2;
4536 adev->mode_info.num_dig = 2;
4538 case IP_VERSION(1, 0, 0):
4539 case IP_VERSION(1, 0, 1):
4540 case IP_VERSION(3, 0, 1):
4541 case IP_VERSION(2, 1, 0):
4542 case IP_VERSION(3, 1, 2):
4543 case IP_VERSION(3, 1, 3):
4544 adev->mode_info.num_crtc = 4;
4545 adev->mode_info.num_hpd = 4;
4546 adev->mode_info.num_dig = 4;
4549 DRM_ERROR("Unsupported DCE IP versions: 0x%x\n",
4550 adev->ip_versions[DCE_HWIP][0]);
4557 amdgpu_dm_set_irq_funcs(adev);
4559 if (adev->mode_info.funcs == NULL)
4560 adev->mode_info.funcs = &dm_display_funcs;
4563 * Note: Do NOT change adev->audio_endpt_rreg and
4564 * adev->audio_endpt_wreg because they are initialised in
4565 * amdgpu_device_init()
4567 #if defined(CONFIG_DEBUG_KERNEL_DC)
4569 adev_to_drm(adev)->dev,
4570 &dev_attr_s3_debug);
4576 static bool modeset_required(struct drm_crtc_state *crtc_state,
4577 struct dc_stream_state *new_stream,
4578 struct dc_stream_state *old_stream)
4580 return crtc_state->active && drm_atomic_crtc_needs_modeset(crtc_state);
4583 static bool modereset_required(struct drm_crtc_state *crtc_state)
4585 return !crtc_state->active && drm_atomic_crtc_needs_modeset(crtc_state);
4588 static void amdgpu_dm_encoder_destroy(struct drm_encoder *encoder)
4590 drm_encoder_cleanup(encoder);
4594 static const struct drm_encoder_funcs amdgpu_dm_encoder_funcs = {
4595 .destroy = amdgpu_dm_encoder_destroy,
4599 static void get_min_max_dc_plane_scaling(struct drm_device *dev,
4600 struct drm_framebuffer *fb,
4601 int *min_downscale, int *max_upscale)
4603 struct amdgpu_device *adev = drm_to_adev(dev);
4604 struct dc *dc = adev->dm.dc;
4605 /* Caps for all supported planes are the same on DCE and DCN 1 - 3 */
4606 struct dc_plane_cap *plane_cap = &dc->caps.planes[0];
4608 switch (fb->format->format) {
4609 case DRM_FORMAT_P010:
4610 case DRM_FORMAT_NV12:
4611 case DRM_FORMAT_NV21:
4612 *max_upscale = plane_cap->max_upscale_factor.nv12;
4613 *min_downscale = plane_cap->max_downscale_factor.nv12;
4616 case DRM_FORMAT_XRGB16161616F:
4617 case DRM_FORMAT_ARGB16161616F:
4618 case DRM_FORMAT_XBGR16161616F:
4619 case DRM_FORMAT_ABGR16161616F:
4620 *max_upscale = plane_cap->max_upscale_factor.fp16;
4621 *min_downscale = plane_cap->max_downscale_factor.fp16;
4625 *max_upscale = plane_cap->max_upscale_factor.argb8888;
4626 *min_downscale = plane_cap->max_downscale_factor.argb8888;
4631 * A factor of 1 in the plane_cap means to not allow scaling, ie. use a
4632 * scaling factor of 1.0 == 1000 units.
4634 if (*max_upscale == 1)
4635 *max_upscale = 1000;
4637 if (*min_downscale == 1)
4638 *min_downscale = 1000;
4642 static int fill_dc_scaling_info(struct amdgpu_device *adev,
4643 const struct drm_plane_state *state,
4644 struct dc_scaling_info *scaling_info)
4646 int scale_w, scale_h, min_downscale, max_upscale;
4648 memset(scaling_info, 0, sizeof(*scaling_info));
4650 /* Source is fixed 16.16 but we ignore mantissa for now... */
4651 scaling_info->src_rect.x = state->src_x >> 16;
4652 scaling_info->src_rect.y = state->src_y >> 16;
4655 * For reasons we don't (yet) fully understand a non-zero
4656 * src_y coordinate into an NV12 buffer can cause a
4657 * system hang on DCN1x.
4658 * To avoid hangs (and maybe be overly cautious)
4659 * let's reject both non-zero src_x and src_y.
4661 * We currently know of only one use-case to reproduce a
4662 * scenario with non-zero src_x and src_y for NV12, which
4663 * is to gesture the YouTube Android app into full screen
4666 if (((adev->ip_versions[DCE_HWIP][0] == IP_VERSION(1, 0, 0)) ||
4667 (adev->ip_versions[DCE_HWIP][0] == IP_VERSION(1, 0, 1))) &&
4668 (state->fb && state->fb->format->format == DRM_FORMAT_NV12 &&
4669 (scaling_info->src_rect.x != 0 || scaling_info->src_rect.y != 0)))
4672 scaling_info->src_rect.width = state->src_w >> 16;
4673 if (scaling_info->src_rect.width == 0)
4676 scaling_info->src_rect.height = state->src_h >> 16;
4677 if (scaling_info->src_rect.height == 0)
4680 scaling_info->dst_rect.x = state->crtc_x;
4681 scaling_info->dst_rect.y = state->crtc_y;
4683 if (state->crtc_w == 0)
4686 scaling_info->dst_rect.width = state->crtc_w;
4688 if (state->crtc_h == 0)
4691 scaling_info->dst_rect.height = state->crtc_h;
4693 /* DRM doesn't specify clipping on destination output. */
4694 scaling_info->clip_rect = scaling_info->dst_rect;
4696 /* Validate scaling per-format with DC plane caps */
4697 if (state->plane && state->plane->dev && state->fb) {
4698 get_min_max_dc_plane_scaling(state->plane->dev, state->fb,
4699 &min_downscale, &max_upscale);
4701 min_downscale = 250;
4702 max_upscale = 16000;
4705 scale_w = scaling_info->dst_rect.width * 1000 /
4706 scaling_info->src_rect.width;
4708 if (scale_w < min_downscale || scale_w > max_upscale)
4711 scale_h = scaling_info->dst_rect.height * 1000 /
4712 scaling_info->src_rect.height;
4714 if (scale_h < min_downscale || scale_h > max_upscale)
4718 * The "scaling_quality" can be ignored for now, quality = 0 has DC
4719 * assume reasonable defaults based on the format.
4726 fill_gfx8_tiling_info_from_flags(union dc_tiling_info *tiling_info,
4727 uint64_t tiling_flags)
4729 /* Fill GFX8 params */
4730 if (AMDGPU_TILING_GET(tiling_flags, ARRAY_MODE) == DC_ARRAY_2D_TILED_THIN1) {
4731 unsigned int bankw, bankh, mtaspect, tile_split, num_banks;
4733 bankw = AMDGPU_TILING_GET(tiling_flags, BANK_WIDTH);
4734 bankh = AMDGPU_TILING_GET(tiling_flags, BANK_HEIGHT);
4735 mtaspect = AMDGPU_TILING_GET(tiling_flags, MACRO_TILE_ASPECT);
4736 tile_split = AMDGPU_TILING_GET(tiling_flags, TILE_SPLIT);
4737 num_banks = AMDGPU_TILING_GET(tiling_flags, NUM_BANKS);
4739 /* XXX fix me for VI */
4740 tiling_info->gfx8.num_banks = num_banks;
4741 tiling_info->gfx8.array_mode =
4742 DC_ARRAY_2D_TILED_THIN1;
4743 tiling_info->gfx8.tile_split = tile_split;
4744 tiling_info->gfx8.bank_width = bankw;
4745 tiling_info->gfx8.bank_height = bankh;
4746 tiling_info->gfx8.tile_aspect = mtaspect;
4747 tiling_info->gfx8.tile_mode =
4748 DC_ADDR_SURF_MICRO_TILING_DISPLAY;
4749 } else if (AMDGPU_TILING_GET(tiling_flags, ARRAY_MODE)
4750 == DC_ARRAY_1D_TILED_THIN1) {
4751 tiling_info->gfx8.array_mode = DC_ARRAY_1D_TILED_THIN1;
4754 tiling_info->gfx8.pipe_config =
4755 AMDGPU_TILING_GET(tiling_flags, PIPE_CONFIG);
4759 fill_gfx9_tiling_info_from_device(const struct amdgpu_device *adev,
4760 union dc_tiling_info *tiling_info)
4762 tiling_info->gfx9.num_pipes =
4763 adev->gfx.config.gb_addr_config_fields.num_pipes;
4764 tiling_info->gfx9.num_banks =
4765 adev->gfx.config.gb_addr_config_fields.num_banks;
4766 tiling_info->gfx9.pipe_interleave =
4767 adev->gfx.config.gb_addr_config_fields.pipe_interleave_size;
4768 tiling_info->gfx9.num_shader_engines =
4769 adev->gfx.config.gb_addr_config_fields.num_se;
4770 tiling_info->gfx9.max_compressed_frags =
4771 adev->gfx.config.gb_addr_config_fields.max_compress_frags;
4772 tiling_info->gfx9.num_rb_per_se =
4773 adev->gfx.config.gb_addr_config_fields.num_rb_per_se;
4774 tiling_info->gfx9.shaderEnable = 1;
4775 if (adev->ip_versions[GC_HWIP][0] >= IP_VERSION(10, 3, 0))
4776 tiling_info->gfx9.num_pkrs = adev->gfx.config.gb_addr_config_fields.num_pkrs;
4780 validate_dcc(struct amdgpu_device *adev,
4781 const enum surface_pixel_format format,
4782 const enum dc_rotation_angle rotation,
4783 const union dc_tiling_info *tiling_info,
4784 const struct dc_plane_dcc_param *dcc,
4785 const struct dc_plane_address *address,
4786 const struct plane_size *plane_size)
4788 struct dc *dc = adev->dm.dc;
4789 struct dc_dcc_surface_param input;
4790 struct dc_surface_dcc_cap output;
4792 memset(&input, 0, sizeof(input));
4793 memset(&output, 0, sizeof(output));
4798 if (format >= SURFACE_PIXEL_FORMAT_VIDEO_BEGIN ||
4799 !dc->cap_funcs.get_dcc_compression_cap)
4802 input.format = format;
4803 input.surface_size.width = plane_size->surface_size.width;
4804 input.surface_size.height = plane_size->surface_size.height;
4805 input.swizzle_mode = tiling_info->gfx9.swizzle;
4807 if (rotation == ROTATION_ANGLE_0 || rotation == ROTATION_ANGLE_180)
4808 input.scan = SCAN_DIRECTION_HORIZONTAL;
4809 else if (rotation == ROTATION_ANGLE_90 || rotation == ROTATION_ANGLE_270)
4810 input.scan = SCAN_DIRECTION_VERTICAL;
4812 if (!dc->cap_funcs.get_dcc_compression_cap(dc, &input, &output))
4815 if (!output.capable)
4818 if (dcc->independent_64b_blks == 0 &&
4819 output.grph.rgb.independent_64b_blks != 0)
4826 modifier_has_dcc(uint64_t modifier)
4828 return IS_AMD_FMT_MOD(modifier) && AMD_FMT_MOD_GET(DCC, modifier);
4832 modifier_gfx9_swizzle_mode(uint64_t modifier)
4834 if (modifier == DRM_FORMAT_MOD_LINEAR)
4837 return AMD_FMT_MOD_GET(TILE, modifier);
4840 static const struct drm_format_info *
4841 amd_get_format_info(const struct drm_mode_fb_cmd2 *cmd)
4843 return amdgpu_lookup_format_info(cmd->pixel_format, cmd->modifier[0]);
4847 fill_gfx9_tiling_info_from_modifier(const struct amdgpu_device *adev,
4848 union dc_tiling_info *tiling_info,
4851 unsigned int mod_bank_xor_bits = AMD_FMT_MOD_GET(BANK_XOR_BITS, modifier);
4852 unsigned int mod_pipe_xor_bits = AMD_FMT_MOD_GET(PIPE_XOR_BITS, modifier);
4853 unsigned int pkrs_log2 = AMD_FMT_MOD_GET(PACKERS, modifier);
4854 unsigned int pipes_log2 = min(4u, mod_pipe_xor_bits);
4856 fill_gfx9_tiling_info_from_device(adev, tiling_info);
4858 if (!IS_AMD_FMT_MOD(modifier))
4861 tiling_info->gfx9.num_pipes = 1u << pipes_log2;
4862 tiling_info->gfx9.num_shader_engines = 1u << (mod_pipe_xor_bits - pipes_log2);
4864 if (adev->family >= AMDGPU_FAMILY_NV) {
4865 tiling_info->gfx9.num_pkrs = 1u << pkrs_log2;
4867 tiling_info->gfx9.num_banks = 1u << mod_bank_xor_bits;
4869 /* for DCC we know it isn't rb aligned, so rb_per_se doesn't matter. */
4873 enum dm_micro_swizzle {
4874 MICRO_SWIZZLE_Z = 0,
4875 MICRO_SWIZZLE_S = 1,
4876 MICRO_SWIZZLE_D = 2,
4880 static bool dm_plane_format_mod_supported(struct drm_plane *plane,
4884 struct amdgpu_device *adev = drm_to_adev(plane->dev);
4885 const struct drm_format_info *info = drm_format_info(format);
4888 enum dm_micro_swizzle microtile = modifier_gfx9_swizzle_mode(modifier) & 3;
4894 * We always have to allow these modifiers:
4895 * 1. Core DRM checks for LINEAR support if userspace does not provide modifiers.
4896 * 2. Not passing any modifiers is the same as explicitly passing INVALID.
4898 if (modifier == DRM_FORMAT_MOD_LINEAR ||
4899 modifier == DRM_FORMAT_MOD_INVALID) {
4903 /* Check that the modifier is on the list of the plane's supported modifiers. */
4904 for (i = 0; i < plane->modifier_count; i++) {
4905 if (modifier == plane->modifiers[i])
4908 if (i == plane->modifier_count)
4912 * For D swizzle the canonical modifier depends on the bpp, so check
4915 if (AMD_FMT_MOD_GET(TILE_VERSION, modifier) == AMD_FMT_MOD_TILE_VER_GFX9 &&
4916 adev->family >= AMDGPU_FAMILY_NV) {
4917 if (microtile == MICRO_SWIZZLE_D && info->cpp[0] == 4)
4921 if (adev->family >= AMDGPU_FAMILY_RV && microtile == MICRO_SWIZZLE_D &&
4925 if (modifier_has_dcc(modifier)) {
4926 /* Per radeonsi comments 16/64 bpp are more complicated. */
4927 if (info->cpp[0] != 4)
4929 /* We support multi-planar formats, but not when combined with
4930 * additional DCC metadata planes. */
4931 if (info->num_planes > 1)
4939 add_modifier(uint64_t **mods, uint64_t *size, uint64_t *cap, uint64_t mod)
4944 if (*cap - *size < 1) {
4945 uint64_t new_cap = *cap * 2;
4946 uint64_t *new_mods = kmalloc(new_cap * sizeof(uint64_t), GFP_KERNEL);
4954 memcpy(new_mods, *mods, sizeof(uint64_t) * *size);
4960 (*mods)[*size] = mod;
4965 add_gfx9_modifiers(const struct amdgpu_device *adev,
4966 uint64_t **mods, uint64_t *size, uint64_t *capacity)
4968 int pipes = ilog2(adev->gfx.config.gb_addr_config_fields.num_pipes);
4969 int pipe_xor_bits = min(8, pipes +
4970 ilog2(adev->gfx.config.gb_addr_config_fields.num_se));
4971 int bank_xor_bits = min(8 - pipe_xor_bits,
4972 ilog2(adev->gfx.config.gb_addr_config_fields.num_banks));
4973 int rb = ilog2(adev->gfx.config.gb_addr_config_fields.num_se) +
4974 ilog2(adev->gfx.config.gb_addr_config_fields.num_rb_per_se);
4977 if (adev->family == AMDGPU_FAMILY_RV) {
4978 /* Raven2 and later */
4979 bool has_constant_encode = adev->asic_type > CHIP_RAVEN || adev->external_rev_id >= 0x81;
4982 * No _D DCC swizzles yet because we only allow 32bpp, which
4983 * doesn't support _D on DCN
4986 if (has_constant_encode) {
4987 add_modifier(mods, size, capacity, AMD_FMT_MOD |
4988 AMD_FMT_MOD_SET(TILE, AMD_FMT_MOD_TILE_GFX9_64K_S_X) |
4989 AMD_FMT_MOD_SET(TILE_VERSION, AMD_FMT_MOD_TILE_VER_GFX9) |
4990 AMD_FMT_MOD_SET(PIPE_XOR_BITS, pipe_xor_bits) |
4991 AMD_FMT_MOD_SET(BANK_XOR_BITS, bank_xor_bits) |
4992 AMD_FMT_MOD_SET(DCC, 1) |
4993 AMD_FMT_MOD_SET(DCC_INDEPENDENT_64B, 1) |
4994 AMD_FMT_MOD_SET(DCC_MAX_COMPRESSED_BLOCK, AMD_FMT_MOD_DCC_BLOCK_64B) |
4995 AMD_FMT_MOD_SET(DCC_CONSTANT_ENCODE, 1));
4998 add_modifier(mods, size, capacity, AMD_FMT_MOD |
4999 AMD_FMT_MOD_SET(TILE, AMD_FMT_MOD_TILE_GFX9_64K_S_X) |
5000 AMD_FMT_MOD_SET(TILE_VERSION, AMD_FMT_MOD_TILE_VER_GFX9) |
5001 AMD_FMT_MOD_SET(PIPE_XOR_BITS, pipe_xor_bits) |
5002 AMD_FMT_MOD_SET(BANK_XOR_BITS, bank_xor_bits) |
5003 AMD_FMT_MOD_SET(DCC, 1) |
5004 AMD_FMT_MOD_SET(DCC_INDEPENDENT_64B, 1) |
5005 AMD_FMT_MOD_SET(DCC_MAX_COMPRESSED_BLOCK, AMD_FMT_MOD_DCC_BLOCK_64B) |
5006 AMD_FMT_MOD_SET(DCC_CONSTANT_ENCODE, 0));
5008 if (has_constant_encode) {
5009 add_modifier(mods, size, capacity, AMD_FMT_MOD |
5010 AMD_FMT_MOD_SET(TILE, AMD_FMT_MOD_TILE_GFX9_64K_S_X) |
5011 AMD_FMT_MOD_SET(TILE_VERSION, AMD_FMT_MOD_TILE_VER_GFX9) |
5012 AMD_FMT_MOD_SET(PIPE_XOR_BITS, pipe_xor_bits) |
5013 AMD_FMT_MOD_SET(BANK_XOR_BITS, bank_xor_bits) |
5014 AMD_FMT_MOD_SET(DCC, 1) |
5015 AMD_FMT_MOD_SET(DCC_RETILE, 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) |
5019 AMD_FMT_MOD_SET(DCC_CONSTANT_ENCODE, 1) |
5020 AMD_FMT_MOD_SET(RB, rb) |
5021 AMD_FMT_MOD_SET(PIPE, pipes));
5024 add_modifier(mods, size, capacity, AMD_FMT_MOD |
5025 AMD_FMT_MOD_SET(TILE, AMD_FMT_MOD_TILE_GFX9_64K_S_X) |
5026 AMD_FMT_MOD_SET(TILE_VERSION, AMD_FMT_MOD_TILE_VER_GFX9) |
5027 AMD_FMT_MOD_SET(PIPE_XOR_BITS, pipe_xor_bits) |
5028 AMD_FMT_MOD_SET(BANK_XOR_BITS, bank_xor_bits) |
5029 AMD_FMT_MOD_SET(DCC, 1) |
5030 AMD_FMT_MOD_SET(DCC_RETILE, 1) |
5031 AMD_FMT_MOD_SET(DCC_INDEPENDENT_64B, 1) |
5032 AMD_FMT_MOD_SET(DCC_MAX_COMPRESSED_BLOCK, AMD_FMT_MOD_DCC_BLOCK_64B) |
5033 AMD_FMT_MOD_SET(DCC_CONSTANT_ENCODE, 0) |
5034 AMD_FMT_MOD_SET(RB, rb) |
5035 AMD_FMT_MOD_SET(PIPE, pipes));
5039 * Only supported for 64bpp on Raven, will be filtered on format in
5040 * dm_plane_format_mod_supported.
5042 add_modifier(mods, size, capacity, AMD_FMT_MOD |
5043 AMD_FMT_MOD_SET(TILE, AMD_FMT_MOD_TILE_GFX9_64K_D_X) |
5044 AMD_FMT_MOD_SET(TILE_VERSION, AMD_FMT_MOD_TILE_VER_GFX9) |
5045 AMD_FMT_MOD_SET(PIPE_XOR_BITS, pipe_xor_bits) |
5046 AMD_FMT_MOD_SET(BANK_XOR_BITS, bank_xor_bits));
5048 if (adev->family == AMDGPU_FAMILY_RV) {
5049 add_modifier(mods, size, capacity, AMD_FMT_MOD |
5050 AMD_FMT_MOD_SET(TILE, AMD_FMT_MOD_TILE_GFX9_64K_S_X) |
5051 AMD_FMT_MOD_SET(TILE_VERSION, AMD_FMT_MOD_TILE_VER_GFX9) |
5052 AMD_FMT_MOD_SET(PIPE_XOR_BITS, pipe_xor_bits) |
5053 AMD_FMT_MOD_SET(BANK_XOR_BITS, bank_xor_bits));
5057 * Only supported for 64bpp on Raven, will be filtered on format in
5058 * dm_plane_format_mod_supported.
5060 add_modifier(mods, size, capacity, AMD_FMT_MOD |
5061 AMD_FMT_MOD_SET(TILE, AMD_FMT_MOD_TILE_GFX9_64K_D) |
5062 AMD_FMT_MOD_SET(TILE_VERSION, AMD_FMT_MOD_TILE_VER_GFX9));
5064 if (adev->family == AMDGPU_FAMILY_RV) {
5065 add_modifier(mods, size, capacity, AMD_FMT_MOD |
5066 AMD_FMT_MOD_SET(TILE, AMD_FMT_MOD_TILE_GFX9_64K_S) |
5067 AMD_FMT_MOD_SET(TILE_VERSION, AMD_FMT_MOD_TILE_VER_GFX9));
5072 add_gfx10_1_modifiers(const struct amdgpu_device *adev,
5073 uint64_t **mods, uint64_t *size, uint64_t *capacity)
5075 int pipe_xor_bits = ilog2(adev->gfx.config.gb_addr_config_fields.num_pipes);
5077 add_modifier(mods, size, capacity, AMD_FMT_MOD |
5078 AMD_FMT_MOD_SET(TILE, AMD_FMT_MOD_TILE_GFX9_64K_R_X) |
5079 AMD_FMT_MOD_SET(TILE_VERSION, AMD_FMT_MOD_TILE_VER_GFX10) |
5080 AMD_FMT_MOD_SET(PIPE_XOR_BITS, pipe_xor_bits) |
5081 AMD_FMT_MOD_SET(DCC, 1) |
5082 AMD_FMT_MOD_SET(DCC_CONSTANT_ENCODE, 1) |
5083 AMD_FMT_MOD_SET(DCC_INDEPENDENT_64B, 1) |
5084 AMD_FMT_MOD_SET(DCC_MAX_COMPRESSED_BLOCK, AMD_FMT_MOD_DCC_BLOCK_64B));
5086 add_modifier(mods, size, capacity, AMD_FMT_MOD |
5087 AMD_FMT_MOD_SET(TILE, AMD_FMT_MOD_TILE_GFX9_64K_R_X) |
5088 AMD_FMT_MOD_SET(TILE_VERSION, AMD_FMT_MOD_TILE_VER_GFX10) |
5089 AMD_FMT_MOD_SET(PIPE_XOR_BITS, pipe_xor_bits) |
5090 AMD_FMT_MOD_SET(DCC, 1) |
5091 AMD_FMT_MOD_SET(DCC_RETILE, 1) |
5092 AMD_FMT_MOD_SET(DCC_CONSTANT_ENCODE, 1) |
5093 AMD_FMT_MOD_SET(DCC_INDEPENDENT_64B, 1) |
5094 AMD_FMT_MOD_SET(DCC_MAX_COMPRESSED_BLOCK, AMD_FMT_MOD_DCC_BLOCK_64B));
5096 add_modifier(mods, size, capacity, AMD_FMT_MOD |
5097 AMD_FMT_MOD_SET(TILE, AMD_FMT_MOD_TILE_GFX9_64K_R_X) |
5098 AMD_FMT_MOD_SET(TILE_VERSION, AMD_FMT_MOD_TILE_VER_GFX10) |
5099 AMD_FMT_MOD_SET(PIPE_XOR_BITS, pipe_xor_bits));
5101 add_modifier(mods, size, capacity, AMD_FMT_MOD |
5102 AMD_FMT_MOD_SET(TILE, AMD_FMT_MOD_TILE_GFX9_64K_S_X) |
5103 AMD_FMT_MOD_SET(TILE_VERSION, AMD_FMT_MOD_TILE_VER_GFX10) |
5104 AMD_FMT_MOD_SET(PIPE_XOR_BITS, pipe_xor_bits));
5107 /* Only supported for 64bpp, will be filtered in dm_plane_format_mod_supported */
5108 add_modifier(mods, size, capacity, AMD_FMT_MOD |
5109 AMD_FMT_MOD_SET(TILE, AMD_FMT_MOD_TILE_GFX9_64K_D) |
5110 AMD_FMT_MOD_SET(TILE_VERSION, AMD_FMT_MOD_TILE_VER_GFX9));
5112 add_modifier(mods, size, capacity, AMD_FMT_MOD |
5113 AMD_FMT_MOD_SET(TILE, AMD_FMT_MOD_TILE_GFX9_64K_S) |
5114 AMD_FMT_MOD_SET(TILE_VERSION, AMD_FMT_MOD_TILE_VER_GFX9));
5118 add_gfx10_3_modifiers(const struct amdgpu_device *adev,
5119 uint64_t **mods, uint64_t *size, uint64_t *capacity)
5121 int pipe_xor_bits = ilog2(adev->gfx.config.gb_addr_config_fields.num_pipes);
5122 int pkrs = ilog2(adev->gfx.config.gb_addr_config_fields.num_pkrs);
5124 add_modifier(mods, size, capacity, AMD_FMT_MOD |
5125 AMD_FMT_MOD_SET(TILE, AMD_FMT_MOD_TILE_GFX9_64K_R_X) |
5126 AMD_FMT_MOD_SET(TILE_VERSION, AMD_FMT_MOD_TILE_VER_GFX10_RBPLUS) |
5127 AMD_FMT_MOD_SET(PIPE_XOR_BITS, pipe_xor_bits) |
5128 AMD_FMT_MOD_SET(PACKERS, pkrs) |
5129 AMD_FMT_MOD_SET(DCC, 1) |
5130 AMD_FMT_MOD_SET(DCC_CONSTANT_ENCODE, 1) |
5131 AMD_FMT_MOD_SET(DCC_INDEPENDENT_64B, 1) |
5132 AMD_FMT_MOD_SET(DCC_INDEPENDENT_128B, 1) |
5133 AMD_FMT_MOD_SET(DCC_MAX_COMPRESSED_BLOCK, AMD_FMT_MOD_DCC_BLOCK_64B));
5135 add_modifier(mods, size, capacity, AMD_FMT_MOD |
5136 AMD_FMT_MOD_SET(TILE, AMD_FMT_MOD_TILE_GFX9_64K_R_X) |
5137 AMD_FMT_MOD_SET(TILE_VERSION, AMD_FMT_MOD_TILE_VER_GFX10_RBPLUS) |
5138 AMD_FMT_MOD_SET(PIPE_XOR_BITS, pipe_xor_bits) |
5139 AMD_FMT_MOD_SET(PACKERS, pkrs) |
5140 AMD_FMT_MOD_SET(DCC, 1) |
5141 AMD_FMT_MOD_SET(DCC_CONSTANT_ENCODE, 1) |
5142 AMD_FMT_MOD_SET(DCC_INDEPENDENT_128B, 1) |
5143 AMD_FMT_MOD_SET(DCC_MAX_COMPRESSED_BLOCK, AMD_FMT_MOD_DCC_BLOCK_128B));
5145 add_modifier(mods, size, capacity, AMD_FMT_MOD |
5146 AMD_FMT_MOD_SET(TILE, AMD_FMT_MOD_TILE_GFX9_64K_R_X) |
5147 AMD_FMT_MOD_SET(TILE_VERSION, AMD_FMT_MOD_TILE_VER_GFX10_RBPLUS) |
5148 AMD_FMT_MOD_SET(PIPE_XOR_BITS, pipe_xor_bits) |
5149 AMD_FMT_MOD_SET(PACKERS, pkrs) |
5150 AMD_FMT_MOD_SET(DCC, 1) |
5151 AMD_FMT_MOD_SET(DCC_RETILE, 1) |
5152 AMD_FMT_MOD_SET(DCC_CONSTANT_ENCODE, 1) |
5153 AMD_FMT_MOD_SET(DCC_INDEPENDENT_64B, 1) |
5154 AMD_FMT_MOD_SET(DCC_INDEPENDENT_128B, 1) |
5155 AMD_FMT_MOD_SET(DCC_MAX_COMPRESSED_BLOCK, AMD_FMT_MOD_DCC_BLOCK_64B));
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_128B, 1) |
5166 AMD_FMT_MOD_SET(DCC_MAX_COMPRESSED_BLOCK, AMD_FMT_MOD_DCC_BLOCK_128B));
5168 add_modifier(mods, size, capacity, AMD_FMT_MOD |
5169 AMD_FMT_MOD_SET(TILE, AMD_FMT_MOD_TILE_GFX9_64K_R_X) |
5170 AMD_FMT_MOD_SET(TILE_VERSION, AMD_FMT_MOD_TILE_VER_GFX10_RBPLUS) |
5171 AMD_FMT_MOD_SET(PIPE_XOR_BITS, pipe_xor_bits) |
5172 AMD_FMT_MOD_SET(PACKERS, pkrs));
5174 add_modifier(mods, size, capacity, AMD_FMT_MOD |
5175 AMD_FMT_MOD_SET(TILE, AMD_FMT_MOD_TILE_GFX9_64K_S_X) |
5176 AMD_FMT_MOD_SET(TILE_VERSION, AMD_FMT_MOD_TILE_VER_GFX10_RBPLUS) |
5177 AMD_FMT_MOD_SET(PIPE_XOR_BITS, pipe_xor_bits) |
5178 AMD_FMT_MOD_SET(PACKERS, pkrs));
5180 /* Only supported for 64bpp, will be filtered in dm_plane_format_mod_supported */
5181 add_modifier(mods, size, capacity, AMD_FMT_MOD |
5182 AMD_FMT_MOD_SET(TILE, AMD_FMT_MOD_TILE_GFX9_64K_D) |
5183 AMD_FMT_MOD_SET(TILE_VERSION, AMD_FMT_MOD_TILE_VER_GFX9));
5185 add_modifier(mods, size, capacity, AMD_FMT_MOD |
5186 AMD_FMT_MOD_SET(TILE, AMD_FMT_MOD_TILE_GFX9_64K_S) |
5187 AMD_FMT_MOD_SET(TILE_VERSION, AMD_FMT_MOD_TILE_VER_GFX9));
5191 get_plane_modifiers(const struct amdgpu_device *adev, unsigned int plane_type, uint64_t **mods)
5193 uint64_t size = 0, capacity = 128;
5196 /* We have not hooked up any pre-GFX9 modifiers. */
5197 if (adev->family < AMDGPU_FAMILY_AI)
5200 *mods = kmalloc(capacity * sizeof(uint64_t), GFP_KERNEL);
5202 if (plane_type == DRM_PLANE_TYPE_CURSOR) {
5203 add_modifier(mods, &size, &capacity, DRM_FORMAT_MOD_LINEAR);
5204 add_modifier(mods, &size, &capacity, DRM_FORMAT_MOD_INVALID);
5205 return *mods ? 0 : -ENOMEM;
5208 switch (adev->family) {
5209 case AMDGPU_FAMILY_AI:
5210 case AMDGPU_FAMILY_RV:
5211 add_gfx9_modifiers(adev, mods, &size, &capacity);
5213 case AMDGPU_FAMILY_NV:
5214 case AMDGPU_FAMILY_VGH:
5215 case AMDGPU_FAMILY_YC:
5216 if (adev->ip_versions[GC_HWIP][0] >= IP_VERSION(10, 3, 0))
5217 add_gfx10_3_modifiers(adev, mods, &size, &capacity);
5219 add_gfx10_1_modifiers(adev, mods, &size, &capacity);
5223 add_modifier(mods, &size, &capacity, DRM_FORMAT_MOD_LINEAR);
5225 /* INVALID marks the end of the list. */
5226 add_modifier(mods, &size, &capacity, DRM_FORMAT_MOD_INVALID);
5235 fill_gfx9_plane_attributes_from_modifiers(struct amdgpu_device *adev,
5236 const struct amdgpu_framebuffer *afb,
5237 const enum surface_pixel_format format,
5238 const enum dc_rotation_angle rotation,
5239 const struct plane_size *plane_size,
5240 union dc_tiling_info *tiling_info,
5241 struct dc_plane_dcc_param *dcc,
5242 struct dc_plane_address *address,
5243 const bool force_disable_dcc)
5245 const uint64_t modifier = afb->base.modifier;
5248 fill_gfx9_tiling_info_from_modifier(adev, tiling_info, modifier);
5249 tiling_info->gfx9.swizzle = modifier_gfx9_swizzle_mode(modifier);
5251 if (modifier_has_dcc(modifier) && !force_disable_dcc) {
5252 uint64_t dcc_address = afb->address + afb->base.offsets[1];
5253 bool independent_64b_blks = AMD_FMT_MOD_GET(DCC_INDEPENDENT_64B, modifier);
5254 bool independent_128b_blks = AMD_FMT_MOD_GET(DCC_INDEPENDENT_128B, modifier);
5257 dcc->meta_pitch = afb->base.pitches[1];
5258 dcc->independent_64b_blks = independent_64b_blks;
5259 if (AMD_FMT_MOD_GET(TILE_VERSION, modifier) == AMD_FMT_MOD_TILE_VER_GFX10_RBPLUS) {
5260 if (independent_64b_blks && independent_128b_blks)
5261 dcc->dcc_ind_blk = hubp_ind_block_64b_no_128bcl;
5262 else if (independent_128b_blks)
5263 dcc->dcc_ind_blk = hubp_ind_block_128b;
5264 else if (independent_64b_blks && !independent_128b_blks)
5265 dcc->dcc_ind_blk = hubp_ind_block_64b;
5267 dcc->dcc_ind_blk = hubp_ind_block_unconstrained;
5269 if (independent_64b_blks)
5270 dcc->dcc_ind_blk = hubp_ind_block_64b;
5272 dcc->dcc_ind_blk = hubp_ind_block_unconstrained;
5275 address->grph.meta_addr.low_part = lower_32_bits(dcc_address);
5276 address->grph.meta_addr.high_part = upper_32_bits(dcc_address);
5279 ret = validate_dcc(adev, format, rotation, tiling_info, dcc, address, plane_size);
5281 drm_dbg_kms(adev_to_drm(adev), "validate_dcc: returned error: %d\n", ret);
5287 fill_plane_buffer_attributes(struct amdgpu_device *adev,
5288 const struct amdgpu_framebuffer *afb,
5289 const enum surface_pixel_format format,
5290 const enum dc_rotation_angle rotation,
5291 const uint64_t tiling_flags,
5292 union dc_tiling_info *tiling_info,
5293 struct plane_size *plane_size,
5294 struct dc_plane_dcc_param *dcc,
5295 struct dc_plane_address *address,
5297 bool force_disable_dcc)
5299 const struct drm_framebuffer *fb = &afb->base;
5302 memset(tiling_info, 0, sizeof(*tiling_info));
5303 memset(plane_size, 0, sizeof(*plane_size));
5304 memset(dcc, 0, sizeof(*dcc));
5305 memset(address, 0, sizeof(*address));
5307 address->tmz_surface = tmz_surface;
5309 if (format < SURFACE_PIXEL_FORMAT_VIDEO_BEGIN) {
5310 uint64_t addr = afb->address + fb->offsets[0];
5312 plane_size->surface_size.x = 0;
5313 plane_size->surface_size.y = 0;
5314 plane_size->surface_size.width = fb->width;
5315 plane_size->surface_size.height = fb->height;
5316 plane_size->surface_pitch =
5317 fb->pitches[0] / fb->format->cpp[0];
5319 address->type = PLN_ADDR_TYPE_GRAPHICS;
5320 address->grph.addr.low_part = lower_32_bits(addr);
5321 address->grph.addr.high_part = upper_32_bits(addr);
5322 } else if (format < SURFACE_PIXEL_FORMAT_INVALID) {
5323 uint64_t luma_addr = afb->address + fb->offsets[0];
5324 uint64_t chroma_addr = afb->address + fb->offsets[1];
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 plane_size->chroma_size.x = 0;
5334 plane_size->chroma_size.y = 0;
5335 /* TODO: set these based on surface format */
5336 plane_size->chroma_size.width = fb->width / 2;
5337 plane_size->chroma_size.height = fb->height / 2;
5339 plane_size->chroma_pitch =
5340 fb->pitches[1] / fb->format->cpp[1];
5342 address->type = PLN_ADDR_TYPE_VIDEO_PROGRESSIVE;
5343 address->video_progressive.luma_addr.low_part =
5344 lower_32_bits(luma_addr);
5345 address->video_progressive.luma_addr.high_part =
5346 upper_32_bits(luma_addr);
5347 address->video_progressive.chroma_addr.low_part =
5348 lower_32_bits(chroma_addr);
5349 address->video_progressive.chroma_addr.high_part =
5350 upper_32_bits(chroma_addr);
5353 if (adev->family >= AMDGPU_FAMILY_AI) {
5354 ret = fill_gfx9_plane_attributes_from_modifiers(adev, afb, format,
5355 rotation, plane_size,
5362 fill_gfx8_tiling_info_from_flags(tiling_info, tiling_flags);
5369 fill_blending_from_plane_state(const struct drm_plane_state *plane_state,
5370 bool *per_pixel_alpha, bool *global_alpha,
5371 int *global_alpha_value)
5373 *per_pixel_alpha = false;
5374 *global_alpha = false;
5375 *global_alpha_value = 0xff;
5377 if (plane_state->plane->type != DRM_PLANE_TYPE_OVERLAY)
5380 if (plane_state->pixel_blend_mode == DRM_MODE_BLEND_PREMULTI) {
5381 static const uint32_t alpha_formats[] = {
5382 DRM_FORMAT_ARGB8888,
5383 DRM_FORMAT_RGBA8888,
5384 DRM_FORMAT_ABGR8888,
5386 uint32_t format = plane_state->fb->format->format;
5389 for (i = 0; i < ARRAY_SIZE(alpha_formats); ++i) {
5390 if (format == alpha_formats[i]) {
5391 *per_pixel_alpha = true;
5397 if (plane_state->alpha < 0xffff) {
5398 *global_alpha = true;
5399 *global_alpha_value = plane_state->alpha >> 8;
5404 fill_plane_color_attributes(const struct drm_plane_state *plane_state,
5405 const enum surface_pixel_format format,
5406 enum dc_color_space *color_space)
5410 *color_space = COLOR_SPACE_SRGB;
5412 /* DRM color properties only affect non-RGB formats. */
5413 if (format < SURFACE_PIXEL_FORMAT_VIDEO_BEGIN)
5416 full_range = (plane_state->color_range == DRM_COLOR_YCBCR_FULL_RANGE);
5418 switch (plane_state->color_encoding) {
5419 case DRM_COLOR_YCBCR_BT601:
5421 *color_space = COLOR_SPACE_YCBCR601;
5423 *color_space = COLOR_SPACE_YCBCR601_LIMITED;
5426 case DRM_COLOR_YCBCR_BT709:
5428 *color_space = COLOR_SPACE_YCBCR709;
5430 *color_space = COLOR_SPACE_YCBCR709_LIMITED;
5433 case DRM_COLOR_YCBCR_BT2020:
5435 *color_space = COLOR_SPACE_2020_YCBCR;
5448 fill_dc_plane_info_and_addr(struct amdgpu_device *adev,
5449 const struct drm_plane_state *plane_state,
5450 const uint64_t tiling_flags,
5451 struct dc_plane_info *plane_info,
5452 struct dc_plane_address *address,
5454 bool force_disable_dcc)
5456 const struct drm_framebuffer *fb = plane_state->fb;
5457 const struct amdgpu_framebuffer *afb =
5458 to_amdgpu_framebuffer(plane_state->fb);
5461 memset(plane_info, 0, sizeof(*plane_info));
5463 switch (fb->format->format) {
5465 plane_info->format =
5466 SURFACE_PIXEL_FORMAT_GRPH_PALETA_256_COLORS;
5468 case DRM_FORMAT_RGB565:
5469 plane_info->format = SURFACE_PIXEL_FORMAT_GRPH_RGB565;
5471 case DRM_FORMAT_XRGB8888:
5472 case DRM_FORMAT_ARGB8888:
5473 plane_info->format = SURFACE_PIXEL_FORMAT_GRPH_ARGB8888;
5475 case DRM_FORMAT_XRGB2101010:
5476 case DRM_FORMAT_ARGB2101010:
5477 plane_info->format = SURFACE_PIXEL_FORMAT_GRPH_ARGB2101010;
5479 case DRM_FORMAT_XBGR2101010:
5480 case DRM_FORMAT_ABGR2101010:
5481 plane_info->format = SURFACE_PIXEL_FORMAT_GRPH_ABGR2101010;
5483 case DRM_FORMAT_XBGR8888:
5484 case DRM_FORMAT_ABGR8888:
5485 plane_info->format = SURFACE_PIXEL_FORMAT_GRPH_ABGR8888;
5487 case DRM_FORMAT_NV21:
5488 plane_info->format = SURFACE_PIXEL_FORMAT_VIDEO_420_YCbCr;
5490 case DRM_FORMAT_NV12:
5491 plane_info->format = SURFACE_PIXEL_FORMAT_VIDEO_420_YCrCb;
5493 case DRM_FORMAT_P010:
5494 plane_info->format = SURFACE_PIXEL_FORMAT_VIDEO_420_10bpc_YCrCb;
5496 case DRM_FORMAT_XRGB16161616F:
5497 case DRM_FORMAT_ARGB16161616F:
5498 plane_info->format = SURFACE_PIXEL_FORMAT_GRPH_ARGB16161616F;
5500 case DRM_FORMAT_XBGR16161616F:
5501 case DRM_FORMAT_ABGR16161616F:
5502 plane_info->format = SURFACE_PIXEL_FORMAT_GRPH_ABGR16161616F;
5504 case DRM_FORMAT_XRGB16161616:
5505 case DRM_FORMAT_ARGB16161616:
5506 plane_info->format = SURFACE_PIXEL_FORMAT_GRPH_ARGB16161616;
5508 case DRM_FORMAT_XBGR16161616:
5509 case DRM_FORMAT_ABGR16161616:
5510 plane_info->format = SURFACE_PIXEL_FORMAT_GRPH_ABGR16161616;
5514 "Unsupported screen format %p4cc\n",
5515 &fb->format->format);
5519 switch (plane_state->rotation & DRM_MODE_ROTATE_MASK) {
5520 case DRM_MODE_ROTATE_0:
5521 plane_info->rotation = ROTATION_ANGLE_0;
5523 case DRM_MODE_ROTATE_90:
5524 plane_info->rotation = ROTATION_ANGLE_90;
5526 case DRM_MODE_ROTATE_180:
5527 plane_info->rotation = ROTATION_ANGLE_180;
5529 case DRM_MODE_ROTATE_270:
5530 plane_info->rotation = ROTATION_ANGLE_270;
5533 plane_info->rotation = ROTATION_ANGLE_0;
5537 plane_info->visible = true;
5538 plane_info->stereo_format = PLANE_STEREO_FORMAT_NONE;
5540 plane_info->layer_index = 0;
5542 ret = fill_plane_color_attributes(plane_state, plane_info->format,
5543 &plane_info->color_space);
5547 ret = fill_plane_buffer_attributes(adev, afb, plane_info->format,
5548 plane_info->rotation, tiling_flags,
5549 &plane_info->tiling_info,
5550 &plane_info->plane_size,
5551 &plane_info->dcc, address, tmz_surface,
5556 fill_blending_from_plane_state(
5557 plane_state, &plane_info->per_pixel_alpha,
5558 &plane_info->global_alpha, &plane_info->global_alpha_value);
5563 static int fill_dc_plane_attributes(struct amdgpu_device *adev,
5564 struct dc_plane_state *dc_plane_state,
5565 struct drm_plane_state *plane_state,
5566 struct drm_crtc_state *crtc_state)
5568 struct dm_crtc_state *dm_crtc_state = to_dm_crtc_state(crtc_state);
5569 struct amdgpu_framebuffer *afb = (struct amdgpu_framebuffer *)plane_state->fb;
5570 struct dc_scaling_info scaling_info;
5571 struct dc_plane_info plane_info;
5573 bool force_disable_dcc = false;
5575 ret = fill_dc_scaling_info(adev, plane_state, &scaling_info);
5579 dc_plane_state->src_rect = scaling_info.src_rect;
5580 dc_plane_state->dst_rect = scaling_info.dst_rect;
5581 dc_plane_state->clip_rect = scaling_info.clip_rect;
5582 dc_plane_state->scaling_quality = scaling_info.scaling_quality;
5584 force_disable_dcc = adev->asic_type == CHIP_RAVEN && adev->in_suspend;
5585 ret = fill_dc_plane_info_and_addr(adev, plane_state,
5588 &dc_plane_state->address,
5594 dc_plane_state->format = plane_info.format;
5595 dc_plane_state->color_space = plane_info.color_space;
5596 dc_plane_state->format = plane_info.format;
5597 dc_plane_state->plane_size = plane_info.plane_size;
5598 dc_plane_state->rotation = plane_info.rotation;
5599 dc_plane_state->horizontal_mirror = plane_info.horizontal_mirror;
5600 dc_plane_state->stereo_format = plane_info.stereo_format;
5601 dc_plane_state->tiling_info = plane_info.tiling_info;
5602 dc_plane_state->visible = plane_info.visible;
5603 dc_plane_state->per_pixel_alpha = plane_info.per_pixel_alpha;
5604 dc_plane_state->global_alpha = plane_info.global_alpha;
5605 dc_plane_state->global_alpha_value = plane_info.global_alpha_value;
5606 dc_plane_state->dcc = plane_info.dcc;
5607 dc_plane_state->layer_index = plane_info.layer_index; // Always returns 0
5608 dc_plane_state->flip_int_enabled = true;
5611 * Always set input transfer function, since plane state is refreshed
5614 ret = amdgpu_dm_update_plane_color_mgmt(dm_crtc_state, dc_plane_state);
5621 static void update_stream_scaling_settings(const struct drm_display_mode *mode,
5622 const struct dm_connector_state *dm_state,
5623 struct dc_stream_state *stream)
5625 enum amdgpu_rmx_type rmx_type;
5627 struct rect src = { 0 }; /* viewport in composition space*/
5628 struct rect dst = { 0 }; /* stream addressable area */
5630 /* no mode. nothing to be done */
5634 /* Full screen scaling by default */
5635 src.width = mode->hdisplay;
5636 src.height = mode->vdisplay;
5637 dst.width = stream->timing.h_addressable;
5638 dst.height = stream->timing.v_addressable;
5641 rmx_type = dm_state->scaling;
5642 if (rmx_type == RMX_ASPECT || rmx_type == RMX_OFF) {
5643 if (src.width * dst.height <
5644 src.height * dst.width) {
5645 /* height needs less upscaling/more downscaling */
5646 dst.width = src.width *
5647 dst.height / src.height;
5649 /* width needs less upscaling/more downscaling */
5650 dst.height = src.height *
5651 dst.width / src.width;
5653 } else if (rmx_type == RMX_CENTER) {
5657 dst.x = (stream->timing.h_addressable - dst.width) / 2;
5658 dst.y = (stream->timing.v_addressable - dst.height) / 2;
5660 if (dm_state->underscan_enable) {
5661 dst.x += dm_state->underscan_hborder / 2;
5662 dst.y += dm_state->underscan_vborder / 2;
5663 dst.width -= dm_state->underscan_hborder;
5664 dst.height -= dm_state->underscan_vborder;
5671 DRM_DEBUG_KMS("Destination Rectangle x:%d y:%d width:%d height:%d\n",
5672 dst.x, dst.y, dst.width, dst.height);
5676 static enum dc_color_depth
5677 convert_color_depth_from_display_info(const struct drm_connector *connector,
5678 bool is_y420, int requested_bpc)
5685 /* Cap display bpc based on HDMI 2.0 HF-VSDB */
5686 if (connector->display_info.hdmi.y420_dc_modes & DRM_EDID_YCBCR420_DC_48)
5688 else if (connector->display_info.hdmi.y420_dc_modes & DRM_EDID_YCBCR420_DC_36)
5690 else if (connector->display_info.hdmi.y420_dc_modes & DRM_EDID_YCBCR420_DC_30)
5693 bpc = (uint8_t)connector->display_info.bpc;
5694 /* Assume 8 bpc by default if no bpc is specified. */
5695 bpc = bpc ? bpc : 8;
5698 if (requested_bpc > 0) {
5700 * Cap display bpc based on the user requested value.
5702 * The value for state->max_bpc may not correctly updated
5703 * depending on when the connector gets added to the state
5704 * or if this was called outside of atomic check, so it
5705 * can't be used directly.
5707 bpc = min_t(u8, bpc, requested_bpc);
5709 /* Round down to the nearest even number. */
5710 bpc = bpc - (bpc & 1);
5716 * Temporary Work around, DRM doesn't parse color depth for
5717 * EDID revision before 1.4
5718 * TODO: Fix edid parsing
5720 return COLOR_DEPTH_888;
5722 return COLOR_DEPTH_666;
5724 return COLOR_DEPTH_888;
5726 return COLOR_DEPTH_101010;
5728 return COLOR_DEPTH_121212;
5730 return COLOR_DEPTH_141414;
5732 return COLOR_DEPTH_161616;
5734 return COLOR_DEPTH_UNDEFINED;
5738 static enum dc_aspect_ratio
5739 get_aspect_ratio(const struct drm_display_mode *mode_in)
5741 /* 1-1 mapping, since both enums follow the HDMI spec. */
5742 return (enum dc_aspect_ratio) mode_in->picture_aspect_ratio;
5745 static enum dc_color_space
5746 get_output_color_space(const struct dc_crtc_timing *dc_crtc_timing)
5748 enum dc_color_space color_space = COLOR_SPACE_SRGB;
5750 switch (dc_crtc_timing->pixel_encoding) {
5751 case PIXEL_ENCODING_YCBCR422:
5752 case PIXEL_ENCODING_YCBCR444:
5753 case PIXEL_ENCODING_YCBCR420:
5756 * 27030khz is the separation point between HDTV and SDTV
5757 * according to HDMI spec, we use YCbCr709 and YCbCr601
5760 if (dc_crtc_timing->pix_clk_100hz > 270300) {
5761 if (dc_crtc_timing->flags.Y_ONLY)
5763 COLOR_SPACE_YCBCR709_LIMITED;
5765 color_space = COLOR_SPACE_YCBCR709;
5767 if (dc_crtc_timing->flags.Y_ONLY)
5769 COLOR_SPACE_YCBCR601_LIMITED;
5771 color_space = COLOR_SPACE_YCBCR601;
5776 case PIXEL_ENCODING_RGB:
5777 color_space = COLOR_SPACE_SRGB;
5788 static bool adjust_colour_depth_from_display_info(
5789 struct dc_crtc_timing *timing_out,
5790 const struct drm_display_info *info)
5792 enum dc_color_depth depth = timing_out->display_color_depth;
5795 normalized_clk = timing_out->pix_clk_100hz / 10;
5796 /* YCbCr 4:2:0 requires additional adjustment of 1/2 */
5797 if (timing_out->pixel_encoding == PIXEL_ENCODING_YCBCR420)
5798 normalized_clk /= 2;
5799 /* Adjusting pix clock following on HDMI spec based on colour depth */
5801 case COLOR_DEPTH_888:
5803 case COLOR_DEPTH_101010:
5804 normalized_clk = (normalized_clk * 30) / 24;
5806 case COLOR_DEPTH_121212:
5807 normalized_clk = (normalized_clk * 36) / 24;
5809 case COLOR_DEPTH_161616:
5810 normalized_clk = (normalized_clk * 48) / 24;
5813 /* The above depths are the only ones valid for HDMI. */
5816 if (normalized_clk <= info->max_tmds_clock) {
5817 timing_out->display_color_depth = depth;
5820 } while (--depth > COLOR_DEPTH_666);
5824 static void fill_stream_properties_from_drm_display_mode(
5825 struct dc_stream_state *stream,
5826 const struct drm_display_mode *mode_in,
5827 const struct drm_connector *connector,
5828 const struct drm_connector_state *connector_state,
5829 const struct dc_stream_state *old_stream,
5832 struct dc_crtc_timing *timing_out = &stream->timing;
5833 const struct drm_display_info *info = &connector->display_info;
5834 struct amdgpu_dm_connector *aconnector = to_amdgpu_dm_connector(connector);
5835 struct hdmi_vendor_infoframe hv_frame;
5836 struct hdmi_avi_infoframe avi_frame;
5838 memset(&hv_frame, 0, sizeof(hv_frame));
5839 memset(&avi_frame, 0, sizeof(avi_frame));
5841 timing_out->h_border_left = 0;
5842 timing_out->h_border_right = 0;
5843 timing_out->v_border_top = 0;
5844 timing_out->v_border_bottom = 0;
5845 /* TODO: un-hardcode */
5846 if (drm_mode_is_420_only(info, mode_in)
5847 && stream->signal == SIGNAL_TYPE_HDMI_TYPE_A)
5848 timing_out->pixel_encoding = PIXEL_ENCODING_YCBCR420;
5849 else if (drm_mode_is_420_also(info, mode_in)
5850 && aconnector->force_yuv420_output)
5851 timing_out->pixel_encoding = PIXEL_ENCODING_YCBCR420;
5852 else if ((connector->display_info.color_formats & DRM_COLOR_FORMAT_YCBCR444)
5853 && stream->signal == SIGNAL_TYPE_HDMI_TYPE_A)
5854 timing_out->pixel_encoding = PIXEL_ENCODING_YCBCR444;
5856 timing_out->pixel_encoding = PIXEL_ENCODING_RGB;
5858 timing_out->timing_3d_format = TIMING_3D_FORMAT_NONE;
5859 timing_out->display_color_depth = convert_color_depth_from_display_info(
5861 (timing_out->pixel_encoding == PIXEL_ENCODING_YCBCR420),
5863 timing_out->scan_type = SCANNING_TYPE_NODATA;
5864 timing_out->hdmi_vic = 0;
5867 timing_out->vic = old_stream->timing.vic;
5868 timing_out->flags.HSYNC_POSITIVE_POLARITY = old_stream->timing.flags.HSYNC_POSITIVE_POLARITY;
5869 timing_out->flags.VSYNC_POSITIVE_POLARITY = old_stream->timing.flags.VSYNC_POSITIVE_POLARITY;
5871 timing_out->vic = drm_match_cea_mode(mode_in);
5872 if (mode_in->flags & DRM_MODE_FLAG_PHSYNC)
5873 timing_out->flags.HSYNC_POSITIVE_POLARITY = 1;
5874 if (mode_in->flags & DRM_MODE_FLAG_PVSYNC)
5875 timing_out->flags.VSYNC_POSITIVE_POLARITY = 1;
5878 if (stream->signal == SIGNAL_TYPE_HDMI_TYPE_A) {
5879 drm_hdmi_avi_infoframe_from_display_mode(&avi_frame, (struct drm_connector *)connector, mode_in);
5880 timing_out->vic = avi_frame.video_code;
5881 drm_hdmi_vendor_infoframe_from_display_mode(&hv_frame, (struct drm_connector *)connector, mode_in);
5882 timing_out->hdmi_vic = hv_frame.vic;
5885 if (is_freesync_video_mode(mode_in, aconnector)) {
5886 timing_out->h_addressable = mode_in->hdisplay;
5887 timing_out->h_total = mode_in->htotal;
5888 timing_out->h_sync_width = mode_in->hsync_end - mode_in->hsync_start;
5889 timing_out->h_front_porch = mode_in->hsync_start - mode_in->hdisplay;
5890 timing_out->v_total = mode_in->vtotal;
5891 timing_out->v_addressable = mode_in->vdisplay;
5892 timing_out->v_front_porch = mode_in->vsync_start - mode_in->vdisplay;
5893 timing_out->v_sync_width = mode_in->vsync_end - mode_in->vsync_start;
5894 timing_out->pix_clk_100hz = mode_in->clock * 10;
5896 timing_out->h_addressable = mode_in->crtc_hdisplay;
5897 timing_out->h_total = mode_in->crtc_htotal;
5898 timing_out->h_sync_width = mode_in->crtc_hsync_end - mode_in->crtc_hsync_start;
5899 timing_out->h_front_porch = mode_in->crtc_hsync_start - mode_in->crtc_hdisplay;
5900 timing_out->v_total = mode_in->crtc_vtotal;
5901 timing_out->v_addressable = mode_in->crtc_vdisplay;
5902 timing_out->v_front_porch = mode_in->crtc_vsync_start - mode_in->crtc_vdisplay;
5903 timing_out->v_sync_width = mode_in->crtc_vsync_end - mode_in->crtc_vsync_start;
5904 timing_out->pix_clk_100hz = mode_in->crtc_clock * 10;
5907 timing_out->aspect_ratio = get_aspect_ratio(mode_in);
5909 stream->output_color_space = get_output_color_space(timing_out);
5911 stream->out_transfer_func->type = TF_TYPE_PREDEFINED;
5912 stream->out_transfer_func->tf = TRANSFER_FUNCTION_SRGB;
5913 if (stream->signal == SIGNAL_TYPE_HDMI_TYPE_A) {
5914 if (!adjust_colour_depth_from_display_info(timing_out, info) &&
5915 drm_mode_is_420_also(info, mode_in) &&
5916 timing_out->pixel_encoding != PIXEL_ENCODING_YCBCR420) {
5917 timing_out->pixel_encoding = PIXEL_ENCODING_YCBCR420;
5918 adjust_colour_depth_from_display_info(timing_out, info);
5923 static void fill_audio_info(struct audio_info *audio_info,
5924 const struct drm_connector *drm_connector,
5925 const struct dc_sink *dc_sink)
5928 int cea_revision = 0;
5929 const struct dc_edid_caps *edid_caps = &dc_sink->edid_caps;
5931 audio_info->manufacture_id = edid_caps->manufacturer_id;
5932 audio_info->product_id = edid_caps->product_id;
5934 cea_revision = drm_connector->display_info.cea_rev;
5936 strscpy(audio_info->display_name,
5937 edid_caps->display_name,
5938 AUDIO_INFO_DISPLAY_NAME_SIZE_IN_CHARS);
5940 if (cea_revision >= 3) {
5941 audio_info->mode_count = edid_caps->audio_mode_count;
5943 for (i = 0; i < audio_info->mode_count; ++i) {
5944 audio_info->modes[i].format_code =
5945 (enum audio_format_code)
5946 (edid_caps->audio_modes[i].format_code);
5947 audio_info->modes[i].channel_count =
5948 edid_caps->audio_modes[i].channel_count;
5949 audio_info->modes[i].sample_rates.all =
5950 edid_caps->audio_modes[i].sample_rate;
5951 audio_info->modes[i].sample_size =
5952 edid_caps->audio_modes[i].sample_size;
5956 audio_info->flags.all = edid_caps->speaker_flags;
5958 /* TODO: We only check for the progressive mode, check for interlace mode too */
5959 if (drm_connector->latency_present[0]) {
5960 audio_info->video_latency = drm_connector->video_latency[0];
5961 audio_info->audio_latency = drm_connector->audio_latency[0];
5964 /* TODO: For DP, video and audio latency should be calculated from DPCD caps */
5969 copy_crtc_timing_for_drm_display_mode(const struct drm_display_mode *src_mode,
5970 struct drm_display_mode *dst_mode)
5972 dst_mode->crtc_hdisplay = src_mode->crtc_hdisplay;
5973 dst_mode->crtc_vdisplay = src_mode->crtc_vdisplay;
5974 dst_mode->crtc_clock = src_mode->crtc_clock;
5975 dst_mode->crtc_hblank_start = src_mode->crtc_hblank_start;
5976 dst_mode->crtc_hblank_end = src_mode->crtc_hblank_end;
5977 dst_mode->crtc_hsync_start = src_mode->crtc_hsync_start;
5978 dst_mode->crtc_hsync_end = src_mode->crtc_hsync_end;
5979 dst_mode->crtc_htotal = src_mode->crtc_htotal;
5980 dst_mode->crtc_hskew = src_mode->crtc_hskew;
5981 dst_mode->crtc_vblank_start = src_mode->crtc_vblank_start;
5982 dst_mode->crtc_vblank_end = src_mode->crtc_vblank_end;
5983 dst_mode->crtc_vsync_start = src_mode->crtc_vsync_start;
5984 dst_mode->crtc_vsync_end = src_mode->crtc_vsync_end;
5985 dst_mode->crtc_vtotal = src_mode->crtc_vtotal;
5989 decide_crtc_timing_for_drm_display_mode(struct drm_display_mode *drm_mode,
5990 const struct drm_display_mode *native_mode,
5993 if (scale_enabled) {
5994 copy_crtc_timing_for_drm_display_mode(native_mode, drm_mode);
5995 } else if (native_mode->clock == drm_mode->clock &&
5996 native_mode->htotal == drm_mode->htotal &&
5997 native_mode->vtotal == drm_mode->vtotal) {
5998 copy_crtc_timing_for_drm_display_mode(native_mode, drm_mode);
6000 /* no scaling nor amdgpu inserted, no need to patch */
6004 static struct dc_sink *
6005 create_fake_sink(struct amdgpu_dm_connector *aconnector)
6007 struct dc_sink_init_data sink_init_data = { 0 };
6008 struct dc_sink *sink = NULL;
6009 sink_init_data.link = aconnector->dc_link;
6010 sink_init_data.sink_signal = aconnector->dc_link->connector_signal;
6012 sink = dc_sink_create(&sink_init_data);
6014 DRM_ERROR("Failed to create sink!\n");
6017 sink->sink_signal = SIGNAL_TYPE_VIRTUAL;
6022 static void set_multisync_trigger_params(
6023 struct dc_stream_state *stream)
6025 struct dc_stream_state *master = NULL;
6027 if (stream->triggered_crtc_reset.enabled) {
6028 master = stream->triggered_crtc_reset.event_source;
6029 stream->triggered_crtc_reset.event =
6030 master->timing.flags.VSYNC_POSITIVE_POLARITY ?
6031 CRTC_EVENT_VSYNC_RISING : CRTC_EVENT_VSYNC_FALLING;
6032 stream->triggered_crtc_reset.delay = TRIGGER_DELAY_NEXT_PIXEL;
6036 static void set_master_stream(struct dc_stream_state *stream_set[],
6039 int j, highest_rfr = 0, master_stream = 0;
6041 for (j = 0; j < stream_count; j++) {
6042 if (stream_set[j] && stream_set[j]->triggered_crtc_reset.enabled) {
6043 int refresh_rate = 0;
6045 refresh_rate = (stream_set[j]->timing.pix_clk_100hz*100)/
6046 (stream_set[j]->timing.h_total*stream_set[j]->timing.v_total);
6047 if (refresh_rate > highest_rfr) {
6048 highest_rfr = refresh_rate;
6053 for (j = 0; j < stream_count; j++) {
6055 stream_set[j]->triggered_crtc_reset.event_source = stream_set[master_stream];
6059 static void dm_enable_per_frame_crtc_master_sync(struct dc_state *context)
6062 struct dc_stream_state *stream;
6064 if (context->stream_count < 2)
6066 for (i = 0; i < context->stream_count ; i++) {
6067 if (!context->streams[i])
6070 * TODO: add a function to read AMD VSDB bits and set
6071 * crtc_sync_master.multi_sync_enabled flag
6072 * For now it's set to false
6076 set_master_stream(context->streams, context->stream_count);
6078 for (i = 0; i < context->stream_count ; i++) {
6079 stream = context->streams[i];
6084 set_multisync_trigger_params(stream);
6088 #if defined(CONFIG_DRM_AMD_DC_DCN)
6089 static void update_dsc_caps(struct amdgpu_dm_connector *aconnector,
6090 struct dc_sink *sink, struct dc_stream_state *stream,
6091 struct dsc_dec_dpcd_caps *dsc_caps)
6093 stream->timing.flags.DSC = 0;
6094 dsc_caps->is_dsc_supported = false;
6096 if (aconnector->dc_link && (sink->sink_signal == SIGNAL_TYPE_DISPLAY_PORT ||
6097 sink->sink_signal == SIGNAL_TYPE_EDP)) {
6098 if (sink->link->dpcd_caps.dongle_type == DISPLAY_DONGLE_NONE ||
6099 sink->link->dpcd_caps.dongle_type == DISPLAY_DONGLE_DP_HDMI_CONVERTER)
6100 dc_dsc_parse_dsc_dpcd(aconnector->dc_link->ctx->dc,
6101 aconnector->dc_link->dpcd_caps.dsc_caps.dsc_basic_caps.raw,
6102 aconnector->dc_link->dpcd_caps.dsc_caps.dsc_branch_decoder_caps.raw,
6107 static void apply_dsc_policy_for_edp(struct amdgpu_dm_connector *aconnector,
6108 struct dc_sink *sink, struct dc_stream_state *stream,
6109 struct dsc_dec_dpcd_caps *dsc_caps,
6110 uint32_t max_dsc_target_bpp_limit_override)
6112 const struct dc_link_settings *verified_link_cap = NULL;
6113 uint32_t link_bw_in_kbps;
6114 uint32_t edp_min_bpp_x16, edp_max_bpp_x16;
6115 struct dc *dc = sink->ctx->dc;
6116 struct dc_dsc_bw_range bw_range = {0};
6117 struct dc_dsc_config dsc_cfg = {0};
6119 verified_link_cap = dc_link_get_link_cap(stream->link);
6120 link_bw_in_kbps = dc_link_bandwidth_kbps(stream->link, verified_link_cap);
6121 edp_min_bpp_x16 = 8 * 16;
6122 edp_max_bpp_x16 = 8 * 16;
6124 if (edp_max_bpp_x16 > dsc_caps->edp_max_bits_per_pixel)
6125 edp_max_bpp_x16 = dsc_caps->edp_max_bits_per_pixel;
6127 if (edp_max_bpp_x16 < edp_min_bpp_x16)
6128 edp_min_bpp_x16 = edp_max_bpp_x16;
6130 if (dc_dsc_compute_bandwidth_range(dc->res_pool->dscs[0],
6131 dc->debug.dsc_min_slice_height_override,
6132 edp_min_bpp_x16, edp_max_bpp_x16,
6137 if (bw_range.max_kbps < link_bw_in_kbps) {
6138 if (dc_dsc_compute_config(dc->res_pool->dscs[0],
6140 dc->debug.dsc_min_slice_height_override,
6141 max_dsc_target_bpp_limit_override,
6145 stream->timing.dsc_cfg = dsc_cfg;
6146 stream->timing.flags.DSC = 1;
6147 stream->timing.dsc_cfg.bits_per_pixel = edp_max_bpp_x16;
6153 if (dc_dsc_compute_config(dc->res_pool->dscs[0],
6155 dc->debug.dsc_min_slice_height_override,
6156 max_dsc_target_bpp_limit_override,
6160 stream->timing.dsc_cfg = dsc_cfg;
6161 stream->timing.flags.DSC = 1;
6165 static void apply_dsc_policy_for_stream(struct amdgpu_dm_connector *aconnector,
6166 struct dc_sink *sink, struct dc_stream_state *stream,
6167 struct dsc_dec_dpcd_caps *dsc_caps)
6169 struct drm_connector *drm_connector = &aconnector->base;
6170 uint32_t link_bandwidth_kbps;
6171 uint32_t max_dsc_target_bpp_limit_override = 0;
6172 struct dc *dc = sink->ctx->dc;
6173 uint32_t max_supported_bw_in_kbps, timing_bw_in_kbps;
6174 uint32_t dsc_max_supported_bw_in_kbps;
6176 link_bandwidth_kbps = dc_link_bandwidth_kbps(aconnector->dc_link,
6177 dc_link_get_link_cap(aconnector->dc_link));
6179 if (stream->link && stream->link->local_sink)
6180 max_dsc_target_bpp_limit_override =
6181 stream->link->local_sink->edid_caps.panel_patch.max_dsc_target_bpp_limit;
6183 /* Set DSC policy according to dsc_clock_en */
6184 dc_dsc_policy_set_enable_dsc_when_not_needed(
6185 aconnector->dsc_settings.dsc_force_enable == DSC_CLK_FORCE_ENABLE);
6187 if (aconnector->dc_link && sink->sink_signal == SIGNAL_TYPE_EDP && !dc->debug.disable_dsc_edp &&
6188 dc->caps.edp_dsc_support && aconnector->dsc_settings.dsc_force_enable != DSC_CLK_FORCE_DISABLE) {
6190 apply_dsc_policy_for_edp(aconnector, sink, stream, dsc_caps, max_dsc_target_bpp_limit_override);
6192 } else if (aconnector->dc_link && sink->sink_signal == SIGNAL_TYPE_DISPLAY_PORT) {
6193 if (sink->link->dpcd_caps.dongle_type == DISPLAY_DONGLE_NONE) {
6194 if (dc_dsc_compute_config(aconnector->dc_link->ctx->dc->res_pool->dscs[0],
6196 aconnector->dc_link->ctx->dc->debug.dsc_min_slice_height_override,
6197 max_dsc_target_bpp_limit_override,
6198 link_bandwidth_kbps,
6200 &stream->timing.dsc_cfg)) {
6201 stream->timing.flags.DSC = 1;
6202 DRM_DEBUG_DRIVER("%s: [%s] DSC is selected from SST RX\n",
6203 __func__, drm_connector->name);
6205 } else if (sink->link->dpcd_caps.dongle_type == DISPLAY_DONGLE_DP_HDMI_CONVERTER) {
6206 timing_bw_in_kbps = dc_bandwidth_in_kbps_from_timing(&stream->timing);
6207 max_supported_bw_in_kbps = link_bandwidth_kbps;
6208 dsc_max_supported_bw_in_kbps = link_bandwidth_kbps;
6210 if (timing_bw_in_kbps > max_supported_bw_in_kbps &&
6211 max_supported_bw_in_kbps > 0 &&
6212 dsc_max_supported_bw_in_kbps > 0)
6213 if (dc_dsc_compute_config(aconnector->dc_link->ctx->dc->res_pool->dscs[0],
6215 aconnector->dc_link->ctx->dc->debug.dsc_min_slice_height_override,
6216 max_dsc_target_bpp_limit_override,
6217 dsc_max_supported_bw_in_kbps,
6219 &stream->timing.dsc_cfg)) {
6220 stream->timing.flags.DSC = 1;
6221 DRM_DEBUG_DRIVER("%s: [%s] DSC is selected from DP-HDMI PCON\n",
6222 __func__, drm_connector->name);
6227 /* Overwrite the stream flag if DSC is enabled through debugfs */
6228 if (aconnector->dsc_settings.dsc_force_enable == DSC_CLK_FORCE_ENABLE)
6229 stream->timing.flags.DSC = 1;
6231 if (stream->timing.flags.DSC && aconnector->dsc_settings.dsc_num_slices_h)
6232 stream->timing.dsc_cfg.num_slices_h = aconnector->dsc_settings.dsc_num_slices_h;
6234 if (stream->timing.flags.DSC && aconnector->dsc_settings.dsc_num_slices_v)
6235 stream->timing.dsc_cfg.num_slices_v = aconnector->dsc_settings.dsc_num_slices_v;
6237 if (stream->timing.flags.DSC && aconnector->dsc_settings.dsc_bits_per_pixel)
6238 stream->timing.dsc_cfg.bits_per_pixel = aconnector->dsc_settings.dsc_bits_per_pixel;
6240 #endif /* CONFIG_DRM_AMD_DC_DCN */
6243 * DOC: FreeSync Video
6245 * When a userspace application wants to play a video, the content follows a
6246 * standard format definition that usually specifies the FPS for that format.
6247 * The below list illustrates some video format and the expected FPS,
6250 * - TV/NTSC (23.976 FPS)
6253 * - TV/NTSC (29.97 FPS)
6254 * - TV/NTSC (30 FPS)
6255 * - Cinema HFR (48 FPS)
6257 * - Commonly used (60 FPS)
6258 * - Multiples of 24 (48,72,96,120 FPS)
6260 * The list of standards video format is not huge and can be added to the
6261 * connector modeset list beforehand. With that, userspace can leverage
6262 * FreeSync to extends the front porch in order to attain the target refresh
6263 * rate. Such a switch will happen seamlessly, without screen blanking or
6264 * reprogramming of the output in any other way. If the userspace requests a
6265 * modesetting change compatible with FreeSync modes that only differ in the
6266 * refresh rate, DC will skip the full update and avoid blink during the
6267 * transition. For example, the video player can change the modesetting from
6268 * 60Hz to 30Hz for playing TV/NTSC content when it goes full screen without
6269 * causing any display blink. This same concept can be applied to a mode
6272 static struct drm_display_mode *
6273 get_highest_refresh_rate_mode(struct amdgpu_dm_connector *aconnector,
6274 bool use_probed_modes)
6276 struct drm_display_mode *m, *m_pref = NULL;
6277 u16 current_refresh, highest_refresh;
6278 struct list_head *list_head = use_probed_modes ?
6279 &aconnector->base.probed_modes :
6280 &aconnector->base.modes;
6282 if (aconnector->freesync_vid_base.clock != 0)
6283 return &aconnector->freesync_vid_base;
6285 /* Find the preferred mode */
6286 list_for_each_entry (m, list_head, head) {
6287 if (m->type & DRM_MODE_TYPE_PREFERRED) {
6294 /* Probably an EDID with no preferred mode. Fallback to first entry */
6295 m_pref = list_first_entry_or_null(
6296 &aconnector->base.modes, struct drm_display_mode, head);
6298 DRM_DEBUG_DRIVER("No preferred mode found in EDID\n");
6303 highest_refresh = drm_mode_vrefresh(m_pref);
6306 * Find the mode with highest refresh rate with same resolution.
6307 * For some monitors, preferred mode is not the mode with highest
6308 * supported refresh rate.
6310 list_for_each_entry (m, list_head, head) {
6311 current_refresh = drm_mode_vrefresh(m);
6313 if (m->hdisplay == m_pref->hdisplay &&
6314 m->vdisplay == m_pref->vdisplay &&
6315 highest_refresh < current_refresh) {
6316 highest_refresh = current_refresh;
6321 aconnector->freesync_vid_base = *m_pref;
6325 static bool is_freesync_video_mode(const struct drm_display_mode *mode,
6326 struct amdgpu_dm_connector *aconnector)
6328 struct drm_display_mode *high_mode;
6331 high_mode = get_highest_refresh_rate_mode(aconnector, false);
6332 if (!high_mode || !mode)
6335 timing_diff = high_mode->vtotal - mode->vtotal;
6337 if (high_mode->clock == 0 || high_mode->clock != mode->clock ||
6338 high_mode->hdisplay != mode->hdisplay ||
6339 high_mode->vdisplay != mode->vdisplay ||
6340 high_mode->hsync_start != mode->hsync_start ||
6341 high_mode->hsync_end != mode->hsync_end ||
6342 high_mode->htotal != mode->htotal ||
6343 high_mode->hskew != mode->hskew ||
6344 high_mode->vscan != mode->vscan ||
6345 high_mode->vsync_start - mode->vsync_start != timing_diff ||
6346 high_mode->vsync_end - mode->vsync_end != timing_diff)
6352 static struct dc_stream_state *
6353 create_stream_for_sink(struct amdgpu_dm_connector *aconnector,
6354 const struct drm_display_mode *drm_mode,
6355 const struct dm_connector_state *dm_state,
6356 const struct dc_stream_state *old_stream,
6359 struct drm_display_mode *preferred_mode = NULL;
6360 struct drm_connector *drm_connector;
6361 const struct drm_connector_state *con_state =
6362 dm_state ? &dm_state->base : NULL;
6363 struct dc_stream_state *stream = NULL;
6364 struct drm_display_mode mode = *drm_mode;
6365 struct drm_display_mode saved_mode;
6366 struct drm_display_mode *freesync_mode = NULL;
6367 bool native_mode_found = false;
6368 bool recalculate_timing = false;
6369 bool scale = dm_state ? (dm_state->scaling != RMX_OFF) : false;
6371 int preferred_refresh = 0;
6372 #if defined(CONFIG_DRM_AMD_DC_DCN)
6373 struct dsc_dec_dpcd_caps dsc_caps;
6375 struct dc_sink *sink = NULL;
6377 memset(&saved_mode, 0, sizeof(saved_mode));
6379 if (aconnector == NULL) {
6380 DRM_ERROR("aconnector is NULL!\n");
6384 drm_connector = &aconnector->base;
6386 if (!aconnector->dc_sink) {
6387 sink = create_fake_sink(aconnector);
6391 sink = aconnector->dc_sink;
6392 dc_sink_retain(sink);
6395 stream = dc_create_stream_for_sink(sink);
6397 if (stream == NULL) {
6398 DRM_ERROR("Failed to create stream for sink!\n");
6402 stream->dm_stream_context = aconnector;
6404 stream->timing.flags.LTE_340MCSC_SCRAMBLE =
6405 drm_connector->display_info.hdmi.scdc.scrambling.low_rates;
6407 list_for_each_entry(preferred_mode, &aconnector->base.modes, head) {
6408 /* Search for preferred mode */
6409 if (preferred_mode->type & DRM_MODE_TYPE_PREFERRED) {
6410 native_mode_found = true;
6414 if (!native_mode_found)
6415 preferred_mode = list_first_entry_or_null(
6416 &aconnector->base.modes,
6417 struct drm_display_mode,
6420 mode_refresh = drm_mode_vrefresh(&mode);
6422 if (preferred_mode == NULL) {
6424 * This may not be an error, the use case is when we have no
6425 * usermode calls to reset and set mode upon hotplug. In this
6426 * case, we call set mode ourselves to restore the previous mode
6427 * and the modelist may not be filled in in time.
6429 DRM_DEBUG_DRIVER("No preferred mode found\n");
6431 recalculate_timing = is_freesync_video_mode(&mode, aconnector);
6432 if (recalculate_timing) {
6433 freesync_mode = get_highest_refresh_rate_mode(aconnector, false);
6435 mode = *freesync_mode;
6437 decide_crtc_timing_for_drm_display_mode(
6438 &mode, preferred_mode, scale);
6440 preferred_refresh = drm_mode_vrefresh(preferred_mode);
6444 if (recalculate_timing)
6445 drm_mode_set_crtcinfo(&saved_mode, 0);
6447 drm_mode_set_crtcinfo(&mode, 0);
6450 * If scaling is enabled and refresh rate didn't change
6451 * we copy the vic and polarities of the old timings
6453 if (!scale || mode_refresh != preferred_refresh)
6454 fill_stream_properties_from_drm_display_mode(
6455 stream, &mode, &aconnector->base, con_state, NULL,
6458 fill_stream_properties_from_drm_display_mode(
6459 stream, &mode, &aconnector->base, con_state, old_stream,
6462 #if defined(CONFIG_DRM_AMD_DC_DCN)
6463 /* SST DSC determination policy */
6464 update_dsc_caps(aconnector, sink, stream, &dsc_caps);
6465 if (aconnector->dsc_settings.dsc_force_enable != DSC_CLK_FORCE_DISABLE && dsc_caps.is_dsc_supported)
6466 apply_dsc_policy_for_stream(aconnector, sink, stream, &dsc_caps);
6469 update_stream_scaling_settings(&mode, dm_state, stream);
6472 &stream->audio_info,
6476 update_stream_signal(stream, sink);
6478 if (stream->signal == SIGNAL_TYPE_HDMI_TYPE_A)
6479 mod_build_hf_vsif_infopacket(stream, &stream->vsp_infopacket);
6481 if (stream->link->psr_settings.psr_feature_enabled) {
6483 // should decide stream support vsc sdp colorimetry capability
6484 // before building vsc info packet
6486 stream->use_vsc_sdp_for_colorimetry = false;
6487 if (aconnector->dc_sink->sink_signal == SIGNAL_TYPE_DISPLAY_PORT_MST) {
6488 stream->use_vsc_sdp_for_colorimetry =
6489 aconnector->dc_sink->is_vsc_sdp_colorimetry_supported;
6491 if (stream->link->dpcd_caps.dprx_feature.bits.VSC_SDP_COLORIMETRY_SUPPORTED)
6492 stream->use_vsc_sdp_for_colorimetry = true;
6494 mod_build_vsc_infopacket(stream, &stream->vsc_infopacket, stream->output_color_space);
6495 aconnector->psr_skip_count = AMDGPU_DM_PSR_ENTRY_DELAY;
6499 dc_sink_release(sink);
6504 static void amdgpu_dm_crtc_destroy(struct drm_crtc *crtc)
6506 drm_crtc_cleanup(crtc);
6510 static void dm_crtc_destroy_state(struct drm_crtc *crtc,
6511 struct drm_crtc_state *state)
6513 struct dm_crtc_state *cur = to_dm_crtc_state(state);
6515 /* TODO Destroy dc_stream objects are stream object is flattened */
6517 dc_stream_release(cur->stream);
6520 __drm_atomic_helper_crtc_destroy_state(state);
6526 static void dm_crtc_reset_state(struct drm_crtc *crtc)
6528 struct dm_crtc_state *state;
6531 dm_crtc_destroy_state(crtc, crtc->state);
6533 state = kzalloc(sizeof(*state), GFP_KERNEL);
6534 if (WARN_ON(!state))
6537 __drm_atomic_helper_crtc_reset(crtc, &state->base);
6540 static struct drm_crtc_state *
6541 dm_crtc_duplicate_state(struct drm_crtc *crtc)
6543 struct dm_crtc_state *state, *cur;
6545 cur = to_dm_crtc_state(crtc->state);
6547 if (WARN_ON(!crtc->state))
6550 state = kzalloc(sizeof(*state), GFP_KERNEL);
6554 __drm_atomic_helper_crtc_duplicate_state(crtc, &state->base);
6557 state->stream = cur->stream;
6558 dc_stream_retain(state->stream);
6561 state->active_planes = cur->active_planes;
6562 state->vrr_infopacket = cur->vrr_infopacket;
6563 state->abm_level = cur->abm_level;
6564 state->vrr_supported = cur->vrr_supported;
6565 state->freesync_config = cur->freesync_config;
6566 state->cm_has_degamma = cur->cm_has_degamma;
6567 state->cm_is_degamma_srgb = cur->cm_is_degamma_srgb;
6568 state->force_dpms_off = cur->force_dpms_off;
6569 /* TODO Duplicate dc_stream after objects are stream object is flattened */
6571 return &state->base;
6574 #ifdef CONFIG_DRM_AMD_SECURE_DISPLAY
6575 static int amdgpu_dm_crtc_late_register(struct drm_crtc *crtc)
6577 crtc_debugfs_init(crtc);
6583 static inline int dm_set_vupdate_irq(struct drm_crtc *crtc, bool enable)
6585 enum dc_irq_source irq_source;
6586 struct amdgpu_crtc *acrtc = to_amdgpu_crtc(crtc);
6587 struct amdgpu_device *adev = drm_to_adev(crtc->dev);
6590 irq_source = IRQ_TYPE_VUPDATE + acrtc->otg_inst;
6592 rc = dc_interrupt_set(adev->dm.dc, irq_source, enable) ? 0 : -EBUSY;
6594 DRM_DEBUG_VBL("crtc %d - vupdate irq %sabling: r=%d\n",
6595 acrtc->crtc_id, enable ? "en" : "dis", rc);
6599 static inline int dm_set_vblank(struct drm_crtc *crtc, bool enable)
6601 enum dc_irq_source irq_source;
6602 struct amdgpu_crtc *acrtc = to_amdgpu_crtc(crtc);
6603 struct amdgpu_device *adev = drm_to_adev(crtc->dev);
6604 struct dm_crtc_state *acrtc_state = to_dm_crtc_state(crtc->state);
6605 #if defined(CONFIG_DRM_AMD_DC_DCN)
6606 struct amdgpu_display_manager *dm = &adev->dm;
6607 struct vblank_control_work *work;
6612 /* vblank irq on -> Only need vupdate irq in vrr mode */
6613 if (amdgpu_dm_vrr_active(acrtc_state))
6614 rc = dm_set_vupdate_irq(crtc, true);
6616 /* vblank irq off -> vupdate irq off */
6617 rc = dm_set_vupdate_irq(crtc, false);
6623 irq_source = IRQ_TYPE_VBLANK + acrtc->otg_inst;
6625 if (!dc_interrupt_set(adev->dm.dc, irq_source, enable))
6628 if (amdgpu_in_reset(adev))
6631 #if defined(CONFIG_DRM_AMD_DC_DCN)
6632 if (dm->vblank_control_workqueue) {
6633 work = kzalloc(sizeof(*work), GFP_ATOMIC);
6637 INIT_WORK(&work->work, vblank_control_worker);
6639 work->acrtc = acrtc;
6640 work->enable = enable;
6642 if (acrtc_state->stream) {
6643 dc_stream_retain(acrtc_state->stream);
6644 work->stream = acrtc_state->stream;
6647 queue_work(dm->vblank_control_workqueue, &work->work);
6654 static int dm_enable_vblank(struct drm_crtc *crtc)
6656 return dm_set_vblank(crtc, true);
6659 static void dm_disable_vblank(struct drm_crtc *crtc)
6661 dm_set_vblank(crtc, false);
6664 /* Implemented only the options currently availible for the driver */
6665 static const struct drm_crtc_funcs amdgpu_dm_crtc_funcs = {
6666 .reset = dm_crtc_reset_state,
6667 .destroy = amdgpu_dm_crtc_destroy,
6668 .set_config = drm_atomic_helper_set_config,
6669 .page_flip = drm_atomic_helper_page_flip,
6670 .atomic_duplicate_state = dm_crtc_duplicate_state,
6671 .atomic_destroy_state = dm_crtc_destroy_state,
6672 .set_crc_source = amdgpu_dm_crtc_set_crc_source,
6673 .verify_crc_source = amdgpu_dm_crtc_verify_crc_source,
6674 .get_crc_sources = amdgpu_dm_crtc_get_crc_sources,
6675 .get_vblank_counter = amdgpu_get_vblank_counter_kms,
6676 .enable_vblank = dm_enable_vblank,
6677 .disable_vblank = dm_disable_vblank,
6678 .get_vblank_timestamp = drm_crtc_vblank_helper_get_vblank_timestamp,
6679 #if defined(CONFIG_DRM_AMD_SECURE_DISPLAY)
6680 .late_register = amdgpu_dm_crtc_late_register,
6684 static enum drm_connector_status
6685 amdgpu_dm_connector_detect(struct drm_connector *connector, bool force)
6688 struct amdgpu_dm_connector *aconnector = to_amdgpu_dm_connector(connector);
6692 * 1. This interface is NOT called in context of HPD irq.
6693 * 2. This interface *is called* in context of user-mode ioctl. Which
6694 * makes it a bad place for *any* MST-related activity.
6697 if (aconnector->base.force == DRM_FORCE_UNSPECIFIED &&
6698 !aconnector->fake_enable)
6699 connected = (aconnector->dc_sink != NULL);
6701 connected = (aconnector->base.force == DRM_FORCE_ON);
6703 update_subconnector_property(aconnector);
6705 return (connected ? connector_status_connected :
6706 connector_status_disconnected);
6709 int amdgpu_dm_connector_atomic_set_property(struct drm_connector *connector,
6710 struct drm_connector_state *connector_state,
6711 struct drm_property *property,
6714 struct drm_device *dev = connector->dev;
6715 struct amdgpu_device *adev = drm_to_adev(dev);
6716 struct dm_connector_state *dm_old_state =
6717 to_dm_connector_state(connector->state);
6718 struct dm_connector_state *dm_new_state =
6719 to_dm_connector_state(connector_state);
6723 if (property == dev->mode_config.scaling_mode_property) {
6724 enum amdgpu_rmx_type rmx_type;
6727 case DRM_MODE_SCALE_CENTER:
6728 rmx_type = RMX_CENTER;
6730 case DRM_MODE_SCALE_ASPECT:
6731 rmx_type = RMX_ASPECT;
6733 case DRM_MODE_SCALE_FULLSCREEN:
6734 rmx_type = RMX_FULL;
6736 case DRM_MODE_SCALE_NONE:
6742 if (dm_old_state->scaling == rmx_type)
6745 dm_new_state->scaling = rmx_type;
6747 } else if (property == adev->mode_info.underscan_hborder_property) {
6748 dm_new_state->underscan_hborder = val;
6750 } else if (property == adev->mode_info.underscan_vborder_property) {
6751 dm_new_state->underscan_vborder = val;
6753 } else if (property == adev->mode_info.underscan_property) {
6754 dm_new_state->underscan_enable = val;
6756 } else if (property == adev->mode_info.abm_level_property) {
6757 dm_new_state->abm_level = val;
6764 int amdgpu_dm_connector_atomic_get_property(struct drm_connector *connector,
6765 const struct drm_connector_state *state,
6766 struct drm_property *property,
6769 struct drm_device *dev = connector->dev;
6770 struct amdgpu_device *adev = drm_to_adev(dev);
6771 struct dm_connector_state *dm_state =
6772 to_dm_connector_state(state);
6775 if (property == dev->mode_config.scaling_mode_property) {
6776 switch (dm_state->scaling) {
6778 *val = DRM_MODE_SCALE_CENTER;
6781 *val = DRM_MODE_SCALE_ASPECT;
6784 *val = DRM_MODE_SCALE_FULLSCREEN;
6788 *val = DRM_MODE_SCALE_NONE;
6792 } else if (property == adev->mode_info.underscan_hborder_property) {
6793 *val = dm_state->underscan_hborder;
6795 } else if (property == adev->mode_info.underscan_vborder_property) {
6796 *val = dm_state->underscan_vborder;
6798 } else if (property == adev->mode_info.underscan_property) {
6799 *val = dm_state->underscan_enable;
6801 } else if (property == adev->mode_info.abm_level_property) {
6802 *val = dm_state->abm_level;
6809 static void amdgpu_dm_connector_unregister(struct drm_connector *connector)
6811 struct amdgpu_dm_connector *amdgpu_dm_connector = to_amdgpu_dm_connector(connector);
6813 drm_dp_aux_unregister(&amdgpu_dm_connector->dm_dp_aux.aux);
6816 static void amdgpu_dm_connector_destroy(struct drm_connector *connector)
6818 struct amdgpu_dm_connector *aconnector = to_amdgpu_dm_connector(connector);
6819 const struct dc_link *link = aconnector->dc_link;
6820 struct amdgpu_device *adev = drm_to_adev(connector->dev);
6821 struct amdgpu_display_manager *dm = &adev->dm;
6825 * Call only if mst_mgr was iniitalized before since it's not done
6826 * for all connector types.
6828 if (aconnector->mst_mgr.dev)
6829 drm_dp_mst_topology_mgr_destroy(&aconnector->mst_mgr);
6831 #if defined(CONFIG_BACKLIGHT_CLASS_DEVICE) ||\
6832 defined(CONFIG_BACKLIGHT_CLASS_DEVICE_MODULE)
6833 for (i = 0; i < dm->num_of_edps; i++) {
6834 if ((link == dm->backlight_link[i]) && dm->backlight_dev[i]) {
6835 backlight_device_unregister(dm->backlight_dev[i]);
6836 dm->backlight_dev[i] = NULL;
6841 if (aconnector->dc_em_sink)
6842 dc_sink_release(aconnector->dc_em_sink);
6843 aconnector->dc_em_sink = NULL;
6844 if (aconnector->dc_sink)
6845 dc_sink_release(aconnector->dc_sink);
6846 aconnector->dc_sink = NULL;
6848 drm_dp_cec_unregister_connector(&aconnector->dm_dp_aux.aux);
6849 drm_connector_unregister(connector);
6850 drm_connector_cleanup(connector);
6851 if (aconnector->i2c) {
6852 i2c_del_adapter(&aconnector->i2c->base);
6853 kfree(aconnector->i2c);
6855 kfree(aconnector->dm_dp_aux.aux.name);
6860 void amdgpu_dm_connector_funcs_reset(struct drm_connector *connector)
6862 struct dm_connector_state *state =
6863 to_dm_connector_state(connector->state);
6865 if (connector->state)
6866 __drm_atomic_helper_connector_destroy_state(connector->state);
6870 state = kzalloc(sizeof(*state), GFP_KERNEL);
6873 state->scaling = RMX_OFF;
6874 state->underscan_enable = false;
6875 state->underscan_hborder = 0;
6876 state->underscan_vborder = 0;
6877 state->base.max_requested_bpc = 8;
6878 state->vcpi_slots = 0;
6880 if (connector->connector_type == DRM_MODE_CONNECTOR_eDP)
6881 state->abm_level = amdgpu_dm_abm_level;
6883 __drm_atomic_helper_connector_reset(connector, &state->base);
6887 struct drm_connector_state *
6888 amdgpu_dm_connector_atomic_duplicate_state(struct drm_connector *connector)
6890 struct dm_connector_state *state =
6891 to_dm_connector_state(connector->state);
6893 struct dm_connector_state *new_state =
6894 kmemdup(state, sizeof(*state), GFP_KERNEL);
6899 __drm_atomic_helper_connector_duplicate_state(connector, &new_state->base);
6901 new_state->freesync_capable = state->freesync_capable;
6902 new_state->abm_level = state->abm_level;
6903 new_state->scaling = state->scaling;
6904 new_state->underscan_enable = state->underscan_enable;
6905 new_state->underscan_hborder = state->underscan_hborder;
6906 new_state->underscan_vborder = state->underscan_vborder;
6907 new_state->vcpi_slots = state->vcpi_slots;
6908 new_state->pbn = state->pbn;
6909 return &new_state->base;
6913 amdgpu_dm_connector_late_register(struct drm_connector *connector)
6915 struct amdgpu_dm_connector *amdgpu_dm_connector =
6916 to_amdgpu_dm_connector(connector);
6919 if ((connector->connector_type == DRM_MODE_CONNECTOR_DisplayPort) ||
6920 (connector->connector_type == DRM_MODE_CONNECTOR_eDP)) {
6921 amdgpu_dm_connector->dm_dp_aux.aux.dev = connector->kdev;
6922 r = drm_dp_aux_register(&amdgpu_dm_connector->dm_dp_aux.aux);
6927 #if defined(CONFIG_DEBUG_FS)
6928 connector_debugfs_init(amdgpu_dm_connector);
6934 static const struct drm_connector_funcs amdgpu_dm_connector_funcs = {
6935 .reset = amdgpu_dm_connector_funcs_reset,
6936 .detect = amdgpu_dm_connector_detect,
6937 .fill_modes = drm_helper_probe_single_connector_modes,
6938 .destroy = amdgpu_dm_connector_destroy,
6939 .atomic_duplicate_state = amdgpu_dm_connector_atomic_duplicate_state,
6940 .atomic_destroy_state = drm_atomic_helper_connector_destroy_state,
6941 .atomic_set_property = amdgpu_dm_connector_atomic_set_property,
6942 .atomic_get_property = amdgpu_dm_connector_atomic_get_property,
6943 .late_register = amdgpu_dm_connector_late_register,
6944 .early_unregister = amdgpu_dm_connector_unregister
6947 static int get_modes(struct drm_connector *connector)
6949 return amdgpu_dm_connector_get_modes(connector);
6952 static void create_eml_sink(struct amdgpu_dm_connector *aconnector)
6954 struct dc_sink_init_data init_params = {
6955 .link = aconnector->dc_link,
6956 .sink_signal = SIGNAL_TYPE_VIRTUAL
6960 if (!aconnector->base.edid_blob_ptr) {
6961 DRM_ERROR("No EDID firmware found on connector: %s ,forcing to OFF!\n",
6962 aconnector->base.name);
6964 aconnector->base.force = DRM_FORCE_OFF;
6965 aconnector->base.override_edid = false;
6969 edid = (struct edid *) aconnector->base.edid_blob_ptr->data;
6971 aconnector->edid = edid;
6973 aconnector->dc_em_sink = dc_link_add_remote_sink(
6974 aconnector->dc_link,
6976 (edid->extensions + 1) * EDID_LENGTH,
6979 if (aconnector->base.force == DRM_FORCE_ON) {
6980 aconnector->dc_sink = aconnector->dc_link->local_sink ?
6981 aconnector->dc_link->local_sink :
6982 aconnector->dc_em_sink;
6983 dc_sink_retain(aconnector->dc_sink);
6987 static void handle_edid_mgmt(struct amdgpu_dm_connector *aconnector)
6989 struct dc_link *link = (struct dc_link *)aconnector->dc_link;
6992 * In case of headless boot with force on for DP managed connector
6993 * Those settings have to be != 0 to get initial modeset
6995 if (link->connector_signal == SIGNAL_TYPE_DISPLAY_PORT) {
6996 link->verified_link_cap.lane_count = LANE_COUNT_FOUR;
6997 link->verified_link_cap.link_rate = LINK_RATE_HIGH2;
7001 aconnector->base.override_edid = true;
7002 create_eml_sink(aconnector);
7005 static struct dc_stream_state *
7006 create_validate_stream_for_sink(struct amdgpu_dm_connector *aconnector,
7007 const struct drm_display_mode *drm_mode,
7008 const struct dm_connector_state *dm_state,
7009 const struct dc_stream_state *old_stream)
7011 struct drm_connector *connector = &aconnector->base;
7012 struct amdgpu_device *adev = drm_to_adev(connector->dev);
7013 struct dc_stream_state *stream;
7014 const struct drm_connector_state *drm_state = dm_state ? &dm_state->base : NULL;
7015 int requested_bpc = drm_state ? drm_state->max_requested_bpc : 8;
7016 enum dc_status dc_result = DC_OK;
7019 stream = create_stream_for_sink(aconnector, drm_mode,
7020 dm_state, old_stream,
7022 if (stream == NULL) {
7023 DRM_ERROR("Failed to create stream for sink!\n");
7027 dc_result = dc_validate_stream(adev->dm.dc, stream);
7029 if (dc_result != DC_OK) {
7030 DRM_DEBUG_KMS("Mode %dx%d (clk %d) failed DC validation with error %d (%s)\n",
7035 dc_status_to_str(dc_result));
7037 dc_stream_release(stream);
7039 requested_bpc -= 2; /* lower bpc to retry validation */
7042 } while (stream == NULL && requested_bpc >= 6);
7044 if (dc_result == DC_FAIL_ENC_VALIDATE && !aconnector->force_yuv420_output) {
7045 DRM_DEBUG_KMS("Retry forcing YCbCr420 encoding\n");
7047 aconnector->force_yuv420_output = true;
7048 stream = create_validate_stream_for_sink(aconnector, drm_mode,
7049 dm_state, old_stream);
7050 aconnector->force_yuv420_output = false;
7056 enum drm_mode_status amdgpu_dm_connector_mode_valid(struct drm_connector *connector,
7057 struct drm_display_mode *mode)
7059 int result = MODE_ERROR;
7060 struct dc_sink *dc_sink;
7061 /* TODO: Unhardcode stream count */
7062 struct dc_stream_state *stream;
7063 struct amdgpu_dm_connector *aconnector = to_amdgpu_dm_connector(connector);
7065 if ((mode->flags & DRM_MODE_FLAG_INTERLACE) ||
7066 (mode->flags & DRM_MODE_FLAG_DBLSCAN))
7070 * Only run this the first time mode_valid is called to initilialize
7073 if (aconnector->base.force != DRM_FORCE_UNSPECIFIED &&
7074 !aconnector->dc_em_sink)
7075 handle_edid_mgmt(aconnector);
7077 dc_sink = to_amdgpu_dm_connector(connector)->dc_sink;
7079 if (dc_sink == NULL && aconnector->base.force != DRM_FORCE_ON_DIGITAL &&
7080 aconnector->base.force != DRM_FORCE_ON) {
7081 DRM_ERROR("dc_sink is NULL!\n");
7085 stream = create_validate_stream_for_sink(aconnector, mode, NULL, NULL);
7087 dc_stream_release(stream);
7092 /* TODO: error handling*/
7096 static int fill_hdr_info_packet(const struct drm_connector_state *state,
7097 struct dc_info_packet *out)
7099 struct hdmi_drm_infoframe frame;
7100 unsigned char buf[30]; /* 26 + 4 */
7104 memset(out, 0, sizeof(*out));
7106 if (!state->hdr_output_metadata)
7109 ret = drm_hdmi_infoframe_set_hdr_metadata(&frame, state);
7113 len = hdmi_drm_infoframe_pack_only(&frame, buf, sizeof(buf));
7117 /* Static metadata is a fixed 26 bytes + 4 byte header. */
7121 /* Prepare the infopacket for DC. */
7122 switch (state->connector->connector_type) {
7123 case DRM_MODE_CONNECTOR_HDMIA:
7124 out->hb0 = 0x87; /* type */
7125 out->hb1 = 0x01; /* version */
7126 out->hb2 = 0x1A; /* length */
7127 out->sb[0] = buf[3]; /* checksum */
7131 case DRM_MODE_CONNECTOR_DisplayPort:
7132 case DRM_MODE_CONNECTOR_eDP:
7133 out->hb0 = 0x00; /* sdp id, zero */
7134 out->hb1 = 0x87; /* type */
7135 out->hb2 = 0x1D; /* payload len - 1 */
7136 out->hb3 = (0x13 << 2); /* sdp version */
7137 out->sb[0] = 0x01; /* version */
7138 out->sb[1] = 0x1A; /* length */
7146 memcpy(&out->sb[i], &buf[4], 26);
7149 print_hex_dump(KERN_DEBUG, "HDR SB:", DUMP_PREFIX_NONE, 16, 1, out->sb,
7150 sizeof(out->sb), false);
7156 amdgpu_dm_connector_atomic_check(struct drm_connector *conn,
7157 struct drm_atomic_state *state)
7159 struct drm_connector_state *new_con_state =
7160 drm_atomic_get_new_connector_state(state, conn);
7161 struct drm_connector_state *old_con_state =
7162 drm_atomic_get_old_connector_state(state, conn);
7163 struct drm_crtc *crtc = new_con_state->crtc;
7164 struct drm_crtc_state *new_crtc_state;
7167 trace_amdgpu_dm_connector_atomic_check(new_con_state);
7172 if (!drm_connector_atomic_hdr_metadata_equal(old_con_state, new_con_state)) {
7173 struct dc_info_packet hdr_infopacket;
7175 ret = fill_hdr_info_packet(new_con_state, &hdr_infopacket);
7179 new_crtc_state = drm_atomic_get_crtc_state(state, crtc);
7180 if (IS_ERR(new_crtc_state))
7181 return PTR_ERR(new_crtc_state);
7184 * DC considers the stream backends changed if the
7185 * static metadata changes. Forcing the modeset also
7186 * gives a simple way for userspace to switch from
7187 * 8bpc to 10bpc when setting the metadata to enter
7190 * Changing the static metadata after it's been
7191 * set is permissible, however. So only force a
7192 * modeset if we're entering or exiting HDR.
7194 new_crtc_state->mode_changed =
7195 !old_con_state->hdr_output_metadata ||
7196 !new_con_state->hdr_output_metadata;
7202 static const struct drm_connector_helper_funcs
7203 amdgpu_dm_connector_helper_funcs = {
7205 * If hotplugging a second bigger display in FB Con mode, bigger resolution
7206 * modes will be filtered by drm_mode_validate_size(), and those modes
7207 * are missing after user start lightdm. So we need to renew modes list.
7208 * in get_modes call back, not just return the modes count
7210 .get_modes = get_modes,
7211 .mode_valid = amdgpu_dm_connector_mode_valid,
7212 .atomic_check = amdgpu_dm_connector_atomic_check,
7215 static void dm_crtc_helper_disable(struct drm_crtc *crtc)
7219 static int count_crtc_active_planes(struct drm_crtc_state *new_crtc_state)
7221 struct drm_atomic_state *state = new_crtc_state->state;
7222 struct drm_plane *plane;
7225 drm_for_each_plane_mask(plane, state->dev, new_crtc_state->plane_mask) {
7226 struct drm_plane_state *new_plane_state;
7228 /* Cursor planes are "fake". */
7229 if (plane->type == DRM_PLANE_TYPE_CURSOR)
7232 new_plane_state = drm_atomic_get_new_plane_state(state, plane);
7234 if (!new_plane_state) {
7236 * The plane is enable on the CRTC and hasn't changed
7237 * state. This means that it previously passed
7238 * validation and is therefore enabled.
7244 /* We need a framebuffer to be considered enabled. */
7245 num_active += (new_plane_state->fb != NULL);
7251 static void dm_update_crtc_active_planes(struct drm_crtc *crtc,
7252 struct drm_crtc_state *new_crtc_state)
7254 struct dm_crtc_state *dm_new_crtc_state =
7255 to_dm_crtc_state(new_crtc_state);
7257 dm_new_crtc_state->active_planes = 0;
7259 if (!dm_new_crtc_state->stream)
7262 dm_new_crtc_state->active_planes =
7263 count_crtc_active_planes(new_crtc_state);
7266 static int dm_crtc_helper_atomic_check(struct drm_crtc *crtc,
7267 struct drm_atomic_state *state)
7269 struct drm_crtc_state *crtc_state = drm_atomic_get_new_crtc_state(state,
7271 struct amdgpu_device *adev = drm_to_adev(crtc->dev);
7272 struct dc *dc = adev->dm.dc;
7273 struct dm_crtc_state *dm_crtc_state = to_dm_crtc_state(crtc_state);
7276 trace_amdgpu_dm_crtc_atomic_check(crtc_state);
7278 dm_update_crtc_active_planes(crtc, crtc_state);
7280 if (WARN_ON(unlikely(!dm_crtc_state->stream &&
7281 modeset_required(crtc_state, NULL, dm_crtc_state->stream)))) {
7286 * We require the primary plane to be enabled whenever the CRTC is, otherwise
7287 * drm_mode_cursor_universal may end up trying to enable the cursor plane while all other
7288 * planes are disabled, which is not supported by the hardware. And there is legacy
7289 * userspace which stops using the HW cursor altogether in response to the resulting EINVAL.
7291 if (crtc_state->enable &&
7292 !(crtc_state->plane_mask & drm_plane_mask(crtc->primary))) {
7293 DRM_DEBUG_ATOMIC("Can't enable a CRTC without enabling the primary plane\n");
7297 /* In some use cases, like reset, no stream is attached */
7298 if (!dm_crtc_state->stream)
7301 if (dc_validate_stream(dc, dm_crtc_state->stream) == DC_OK)
7304 DRM_DEBUG_ATOMIC("Failed DC stream validation\n");
7308 static bool dm_crtc_helper_mode_fixup(struct drm_crtc *crtc,
7309 const struct drm_display_mode *mode,
7310 struct drm_display_mode *adjusted_mode)
7315 static const struct drm_crtc_helper_funcs amdgpu_dm_crtc_helper_funcs = {
7316 .disable = dm_crtc_helper_disable,
7317 .atomic_check = dm_crtc_helper_atomic_check,
7318 .mode_fixup = dm_crtc_helper_mode_fixup,
7319 .get_scanout_position = amdgpu_crtc_get_scanout_position,
7322 static void dm_encoder_helper_disable(struct drm_encoder *encoder)
7327 static int convert_dc_color_depth_into_bpc (enum dc_color_depth display_color_depth)
7329 switch (display_color_depth) {
7330 case COLOR_DEPTH_666:
7332 case COLOR_DEPTH_888:
7334 case COLOR_DEPTH_101010:
7336 case COLOR_DEPTH_121212:
7338 case COLOR_DEPTH_141414:
7340 case COLOR_DEPTH_161616:
7348 static int dm_encoder_helper_atomic_check(struct drm_encoder *encoder,
7349 struct drm_crtc_state *crtc_state,
7350 struct drm_connector_state *conn_state)
7352 struct drm_atomic_state *state = crtc_state->state;
7353 struct drm_connector *connector = conn_state->connector;
7354 struct amdgpu_dm_connector *aconnector = to_amdgpu_dm_connector(connector);
7355 struct dm_connector_state *dm_new_connector_state = to_dm_connector_state(conn_state);
7356 const struct drm_display_mode *adjusted_mode = &crtc_state->adjusted_mode;
7357 struct drm_dp_mst_topology_mgr *mst_mgr;
7358 struct drm_dp_mst_port *mst_port;
7359 enum dc_color_depth color_depth;
7361 bool is_y420 = false;
7363 if (!aconnector->port || !aconnector->dc_sink)
7366 mst_port = aconnector->port;
7367 mst_mgr = &aconnector->mst_port->mst_mgr;
7369 if (!crtc_state->connectors_changed && !crtc_state->mode_changed)
7372 if (!state->duplicated) {
7373 int max_bpc = conn_state->max_requested_bpc;
7374 is_y420 = drm_mode_is_420_also(&connector->display_info, adjusted_mode) &&
7375 aconnector->force_yuv420_output;
7376 color_depth = convert_color_depth_from_display_info(connector,
7379 bpp = convert_dc_color_depth_into_bpc(color_depth) * 3;
7380 clock = adjusted_mode->clock;
7381 dm_new_connector_state->pbn = drm_dp_calc_pbn_mode(clock, bpp, false);
7383 dm_new_connector_state->vcpi_slots = drm_dp_atomic_find_vcpi_slots(state,
7386 dm_new_connector_state->pbn,
7387 dm_mst_get_pbn_divider(aconnector->dc_link));
7388 if (dm_new_connector_state->vcpi_slots < 0) {
7389 DRM_DEBUG_ATOMIC("failed finding vcpi slots: %d\n", (int)dm_new_connector_state->vcpi_slots);
7390 return dm_new_connector_state->vcpi_slots;
7395 const struct drm_encoder_helper_funcs amdgpu_dm_encoder_helper_funcs = {
7396 .disable = dm_encoder_helper_disable,
7397 .atomic_check = dm_encoder_helper_atomic_check
7400 #if defined(CONFIG_DRM_AMD_DC_DCN)
7401 static int dm_update_mst_vcpi_slots_for_dsc(struct drm_atomic_state *state,
7402 struct dc_state *dc_state,
7403 struct dsc_mst_fairness_vars *vars)
7405 struct dc_stream_state *stream = NULL;
7406 struct drm_connector *connector;
7407 struct drm_connector_state *new_con_state;
7408 struct amdgpu_dm_connector *aconnector;
7409 struct dm_connector_state *dm_conn_state;
7411 int vcpi, pbn_div, pbn, slot_num = 0;
7413 for_each_new_connector_in_state(state, connector, new_con_state, i) {
7415 aconnector = to_amdgpu_dm_connector(connector);
7417 if (!aconnector->port)
7420 if (!new_con_state || !new_con_state->crtc)
7423 dm_conn_state = to_dm_connector_state(new_con_state);
7425 for (j = 0; j < dc_state->stream_count; j++) {
7426 stream = dc_state->streams[j];
7430 if ((struct amdgpu_dm_connector*)stream->dm_stream_context == aconnector)
7439 pbn_div = dm_mst_get_pbn_divider(stream->link);
7440 /* pbn is calculated by compute_mst_dsc_configs_for_state*/
7441 for (j = 0; j < dc_state->stream_count; j++) {
7442 if (vars[j].aconnector == aconnector) {
7448 if (j == dc_state->stream_count)
7451 slot_num = DIV_ROUND_UP(pbn, pbn_div);
7453 if (stream->timing.flags.DSC != 1) {
7454 dm_conn_state->pbn = pbn;
7455 dm_conn_state->vcpi_slots = slot_num;
7457 drm_dp_mst_atomic_enable_dsc(state,
7465 vcpi = drm_dp_mst_atomic_enable_dsc(state,
7472 dm_conn_state->pbn = pbn;
7473 dm_conn_state->vcpi_slots = vcpi;
7479 static void dm_drm_plane_reset(struct drm_plane *plane)
7481 struct dm_plane_state *amdgpu_state = NULL;
7484 plane->funcs->atomic_destroy_state(plane, plane->state);
7486 amdgpu_state = kzalloc(sizeof(*amdgpu_state), GFP_KERNEL);
7487 WARN_ON(amdgpu_state == NULL);
7490 __drm_atomic_helper_plane_reset(plane, &amdgpu_state->base);
7493 static struct drm_plane_state *
7494 dm_drm_plane_duplicate_state(struct drm_plane *plane)
7496 struct dm_plane_state *dm_plane_state, *old_dm_plane_state;
7498 old_dm_plane_state = to_dm_plane_state(plane->state);
7499 dm_plane_state = kzalloc(sizeof(*dm_plane_state), GFP_KERNEL);
7500 if (!dm_plane_state)
7503 __drm_atomic_helper_plane_duplicate_state(plane, &dm_plane_state->base);
7505 if (old_dm_plane_state->dc_state) {
7506 dm_plane_state->dc_state = old_dm_plane_state->dc_state;
7507 dc_plane_state_retain(dm_plane_state->dc_state);
7510 return &dm_plane_state->base;
7513 static void dm_drm_plane_destroy_state(struct drm_plane *plane,
7514 struct drm_plane_state *state)
7516 struct dm_plane_state *dm_plane_state = to_dm_plane_state(state);
7518 if (dm_plane_state->dc_state)
7519 dc_plane_state_release(dm_plane_state->dc_state);
7521 drm_atomic_helper_plane_destroy_state(plane, state);
7524 static const struct drm_plane_funcs dm_plane_funcs = {
7525 .update_plane = drm_atomic_helper_update_plane,
7526 .disable_plane = drm_atomic_helper_disable_plane,
7527 .destroy = drm_primary_helper_destroy,
7528 .reset = dm_drm_plane_reset,
7529 .atomic_duplicate_state = dm_drm_plane_duplicate_state,
7530 .atomic_destroy_state = dm_drm_plane_destroy_state,
7531 .format_mod_supported = dm_plane_format_mod_supported,
7534 static int dm_plane_helper_prepare_fb(struct drm_plane *plane,
7535 struct drm_plane_state *new_state)
7537 struct amdgpu_framebuffer *afb;
7538 struct drm_gem_object *obj;
7539 struct amdgpu_device *adev;
7540 struct amdgpu_bo *rbo;
7541 struct dm_plane_state *dm_plane_state_new, *dm_plane_state_old;
7542 struct list_head list;
7543 struct ttm_validate_buffer tv;
7544 struct ww_acquire_ctx ticket;
7548 if (!new_state->fb) {
7549 DRM_DEBUG_KMS("No FB bound\n");
7553 afb = to_amdgpu_framebuffer(new_state->fb);
7554 obj = new_state->fb->obj[0];
7555 rbo = gem_to_amdgpu_bo(obj);
7556 adev = amdgpu_ttm_adev(rbo->tbo.bdev);
7557 INIT_LIST_HEAD(&list);
7561 list_add(&tv.head, &list);
7563 r = ttm_eu_reserve_buffers(&ticket, &list, false, NULL);
7565 dev_err(adev->dev, "fail to reserve bo (%d)\n", r);
7569 if (plane->type != DRM_PLANE_TYPE_CURSOR)
7570 domain = amdgpu_display_supported_domains(adev, rbo->flags);
7572 domain = AMDGPU_GEM_DOMAIN_VRAM;
7574 r = amdgpu_bo_pin(rbo, domain);
7575 if (unlikely(r != 0)) {
7576 if (r != -ERESTARTSYS)
7577 DRM_ERROR("Failed to pin framebuffer with error %d\n", r);
7578 ttm_eu_backoff_reservation(&ticket, &list);
7582 r = amdgpu_ttm_alloc_gart(&rbo->tbo);
7583 if (unlikely(r != 0)) {
7584 amdgpu_bo_unpin(rbo);
7585 ttm_eu_backoff_reservation(&ticket, &list);
7586 DRM_ERROR("%p bind failed\n", rbo);
7590 ttm_eu_backoff_reservation(&ticket, &list);
7592 afb->address = amdgpu_bo_gpu_offset(rbo);
7597 * We don't do surface updates on planes that have been newly created,
7598 * but we also don't have the afb->address during atomic check.
7600 * Fill in buffer attributes depending on the address here, but only on
7601 * newly created planes since they're not being used by DC yet and this
7602 * won't modify global state.
7604 dm_plane_state_old = to_dm_plane_state(plane->state);
7605 dm_plane_state_new = to_dm_plane_state(new_state);
7607 if (dm_plane_state_new->dc_state &&
7608 dm_plane_state_old->dc_state != dm_plane_state_new->dc_state) {
7609 struct dc_plane_state *plane_state =
7610 dm_plane_state_new->dc_state;
7611 bool force_disable_dcc = !plane_state->dcc.enable;
7613 fill_plane_buffer_attributes(
7614 adev, afb, plane_state->format, plane_state->rotation,
7616 &plane_state->tiling_info, &plane_state->plane_size,
7617 &plane_state->dcc, &plane_state->address,
7618 afb->tmz_surface, force_disable_dcc);
7624 static void dm_plane_helper_cleanup_fb(struct drm_plane *plane,
7625 struct drm_plane_state *old_state)
7627 struct amdgpu_bo *rbo;
7633 rbo = gem_to_amdgpu_bo(old_state->fb->obj[0]);
7634 r = amdgpu_bo_reserve(rbo, false);
7636 DRM_ERROR("failed to reserve rbo before unpin\n");
7640 amdgpu_bo_unpin(rbo);
7641 amdgpu_bo_unreserve(rbo);
7642 amdgpu_bo_unref(&rbo);
7645 static int dm_plane_helper_check_state(struct drm_plane_state *state,
7646 struct drm_crtc_state *new_crtc_state)
7648 struct drm_framebuffer *fb = state->fb;
7649 int min_downscale, max_upscale;
7651 int max_scale = INT_MAX;
7653 /* Plane enabled? Validate viewport and get scaling factors from plane caps. */
7654 if (fb && state->crtc) {
7655 /* Validate viewport to cover the case when only the position changes */
7656 if (state->plane->type != DRM_PLANE_TYPE_CURSOR) {
7657 int viewport_width = state->crtc_w;
7658 int viewport_height = state->crtc_h;
7660 if (state->crtc_x < 0)
7661 viewport_width += state->crtc_x;
7662 else if (state->crtc_x + state->crtc_w > new_crtc_state->mode.crtc_hdisplay)
7663 viewport_width = new_crtc_state->mode.crtc_hdisplay - state->crtc_x;
7665 if (state->crtc_y < 0)
7666 viewport_height += state->crtc_y;
7667 else if (state->crtc_y + state->crtc_h > new_crtc_state->mode.crtc_vdisplay)
7668 viewport_height = new_crtc_state->mode.crtc_vdisplay - state->crtc_y;
7670 if (viewport_width < 0 || viewport_height < 0) {
7671 DRM_DEBUG_ATOMIC("Plane completely outside of screen\n");
7673 } else if (viewport_width < MIN_VIEWPORT_SIZE*2) { /* x2 for width is because of pipe-split. */
7674 DRM_DEBUG_ATOMIC("Viewport width %d smaller than %d\n", viewport_width, MIN_VIEWPORT_SIZE*2);
7676 } else if (viewport_height < MIN_VIEWPORT_SIZE) {
7677 DRM_DEBUG_ATOMIC("Viewport height %d smaller than %d\n", viewport_height, MIN_VIEWPORT_SIZE);
7683 /* Get min/max allowed scaling factors from plane caps. */
7684 get_min_max_dc_plane_scaling(state->crtc->dev, fb,
7685 &min_downscale, &max_upscale);
7687 * Convert to drm convention: 16.16 fixed point, instead of dc's
7688 * 1.0 == 1000. Also drm scaling is src/dst instead of dc's
7689 * dst/src, so min_scale = 1.0 / max_upscale, etc.
7691 min_scale = (1000 << 16) / max_upscale;
7692 max_scale = (1000 << 16) / min_downscale;
7695 return drm_atomic_helper_check_plane_state(
7696 state, new_crtc_state, min_scale, max_scale, true, true);
7699 static int dm_plane_atomic_check(struct drm_plane *plane,
7700 struct drm_atomic_state *state)
7702 struct drm_plane_state *new_plane_state = drm_atomic_get_new_plane_state(state,
7704 struct amdgpu_device *adev = drm_to_adev(plane->dev);
7705 struct dc *dc = adev->dm.dc;
7706 struct dm_plane_state *dm_plane_state;
7707 struct dc_scaling_info scaling_info;
7708 struct drm_crtc_state *new_crtc_state;
7711 trace_amdgpu_dm_plane_atomic_check(new_plane_state);
7713 dm_plane_state = to_dm_plane_state(new_plane_state);
7715 if (!dm_plane_state->dc_state)
7719 drm_atomic_get_new_crtc_state(state,
7720 new_plane_state->crtc);
7721 if (!new_crtc_state)
7724 ret = dm_plane_helper_check_state(new_plane_state, new_crtc_state);
7728 ret = fill_dc_scaling_info(adev, new_plane_state, &scaling_info);
7732 if (dc_validate_plane(dc, dm_plane_state->dc_state) == DC_OK)
7738 static int dm_plane_atomic_async_check(struct drm_plane *plane,
7739 struct drm_atomic_state *state)
7741 /* Only support async updates on cursor planes. */
7742 if (plane->type != DRM_PLANE_TYPE_CURSOR)
7748 static void dm_plane_atomic_async_update(struct drm_plane *plane,
7749 struct drm_atomic_state *state)
7751 struct drm_plane_state *new_state = drm_atomic_get_new_plane_state(state,
7753 struct drm_plane_state *old_state =
7754 drm_atomic_get_old_plane_state(state, plane);
7756 trace_amdgpu_dm_atomic_update_cursor(new_state);
7758 swap(plane->state->fb, new_state->fb);
7760 plane->state->src_x = new_state->src_x;
7761 plane->state->src_y = new_state->src_y;
7762 plane->state->src_w = new_state->src_w;
7763 plane->state->src_h = new_state->src_h;
7764 plane->state->crtc_x = new_state->crtc_x;
7765 plane->state->crtc_y = new_state->crtc_y;
7766 plane->state->crtc_w = new_state->crtc_w;
7767 plane->state->crtc_h = new_state->crtc_h;
7769 handle_cursor_update(plane, old_state);
7772 static const struct drm_plane_helper_funcs dm_plane_helper_funcs = {
7773 .prepare_fb = dm_plane_helper_prepare_fb,
7774 .cleanup_fb = dm_plane_helper_cleanup_fb,
7775 .atomic_check = dm_plane_atomic_check,
7776 .atomic_async_check = dm_plane_atomic_async_check,
7777 .atomic_async_update = dm_plane_atomic_async_update
7781 * TODO: these are currently initialized to rgb formats only.
7782 * For future use cases we should either initialize them dynamically based on
7783 * plane capabilities, or initialize this array to all formats, so internal drm
7784 * check will succeed, and let DC implement proper check
7786 static const uint32_t rgb_formats[] = {
7787 DRM_FORMAT_XRGB8888,
7788 DRM_FORMAT_ARGB8888,
7789 DRM_FORMAT_RGBA8888,
7790 DRM_FORMAT_XRGB2101010,
7791 DRM_FORMAT_XBGR2101010,
7792 DRM_FORMAT_ARGB2101010,
7793 DRM_FORMAT_ABGR2101010,
7794 DRM_FORMAT_XRGB16161616,
7795 DRM_FORMAT_XBGR16161616,
7796 DRM_FORMAT_ARGB16161616,
7797 DRM_FORMAT_ABGR16161616,
7798 DRM_FORMAT_XBGR8888,
7799 DRM_FORMAT_ABGR8888,
7803 static const uint32_t overlay_formats[] = {
7804 DRM_FORMAT_XRGB8888,
7805 DRM_FORMAT_ARGB8888,
7806 DRM_FORMAT_RGBA8888,
7807 DRM_FORMAT_XBGR8888,
7808 DRM_FORMAT_ABGR8888,
7812 static const u32 cursor_formats[] = {
7816 static int get_plane_formats(const struct drm_plane *plane,
7817 const struct dc_plane_cap *plane_cap,
7818 uint32_t *formats, int max_formats)
7820 int i, num_formats = 0;
7823 * TODO: Query support for each group of formats directly from
7824 * DC plane caps. This will require adding more formats to the
7828 switch (plane->type) {
7829 case DRM_PLANE_TYPE_PRIMARY:
7830 for (i = 0; i < ARRAY_SIZE(rgb_formats); ++i) {
7831 if (num_formats >= max_formats)
7834 formats[num_formats++] = rgb_formats[i];
7837 if (plane_cap && plane_cap->pixel_format_support.nv12)
7838 formats[num_formats++] = DRM_FORMAT_NV12;
7839 if (plane_cap && plane_cap->pixel_format_support.p010)
7840 formats[num_formats++] = DRM_FORMAT_P010;
7841 if (plane_cap && plane_cap->pixel_format_support.fp16) {
7842 formats[num_formats++] = DRM_FORMAT_XRGB16161616F;
7843 formats[num_formats++] = DRM_FORMAT_ARGB16161616F;
7844 formats[num_formats++] = DRM_FORMAT_XBGR16161616F;
7845 formats[num_formats++] = DRM_FORMAT_ABGR16161616F;
7849 case DRM_PLANE_TYPE_OVERLAY:
7850 for (i = 0; i < ARRAY_SIZE(overlay_formats); ++i) {
7851 if (num_formats >= max_formats)
7854 formats[num_formats++] = overlay_formats[i];
7858 case DRM_PLANE_TYPE_CURSOR:
7859 for (i = 0; i < ARRAY_SIZE(cursor_formats); ++i) {
7860 if (num_formats >= max_formats)
7863 formats[num_formats++] = cursor_formats[i];
7871 static int amdgpu_dm_plane_init(struct amdgpu_display_manager *dm,
7872 struct drm_plane *plane,
7873 unsigned long possible_crtcs,
7874 const struct dc_plane_cap *plane_cap)
7876 uint32_t formats[32];
7879 unsigned int supported_rotations;
7880 uint64_t *modifiers = NULL;
7882 num_formats = get_plane_formats(plane, plane_cap, formats,
7883 ARRAY_SIZE(formats));
7885 res = get_plane_modifiers(dm->adev, plane->type, &modifiers);
7889 res = drm_universal_plane_init(adev_to_drm(dm->adev), plane, possible_crtcs,
7890 &dm_plane_funcs, formats, num_formats,
7891 modifiers, plane->type, NULL);
7896 if (plane->type == DRM_PLANE_TYPE_OVERLAY &&
7897 plane_cap && plane_cap->per_pixel_alpha) {
7898 unsigned int blend_caps = BIT(DRM_MODE_BLEND_PIXEL_NONE) |
7899 BIT(DRM_MODE_BLEND_PREMULTI);
7901 drm_plane_create_alpha_property(plane);
7902 drm_plane_create_blend_mode_property(plane, blend_caps);
7905 if (plane->type == DRM_PLANE_TYPE_PRIMARY &&
7907 (plane_cap->pixel_format_support.nv12 ||
7908 plane_cap->pixel_format_support.p010)) {
7909 /* This only affects YUV formats. */
7910 drm_plane_create_color_properties(
7912 BIT(DRM_COLOR_YCBCR_BT601) |
7913 BIT(DRM_COLOR_YCBCR_BT709) |
7914 BIT(DRM_COLOR_YCBCR_BT2020),
7915 BIT(DRM_COLOR_YCBCR_LIMITED_RANGE) |
7916 BIT(DRM_COLOR_YCBCR_FULL_RANGE),
7917 DRM_COLOR_YCBCR_BT709, DRM_COLOR_YCBCR_LIMITED_RANGE);
7920 supported_rotations =
7921 DRM_MODE_ROTATE_0 | DRM_MODE_ROTATE_90 |
7922 DRM_MODE_ROTATE_180 | DRM_MODE_ROTATE_270;
7924 if (dm->adev->asic_type >= CHIP_BONAIRE &&
7925 plane->type != DRM_PLANE_TYPE_CURSOR)
7926 drm_plane_create_rotation_property(plane, DRM_MODE_ROTATE_0,
7927 supported_rotations);
7929 drm_plane_helper_add(plane, &dm_plane_helper_funcs);
7931 /* Create (reset) the plane state */
7932 if (plane->funcs->reset)
7933 plane->funcs->reset(plane);
7938 static int amdgpu_dm_crtc_init(struct amdgpu_display_manager *dm,
7939 struct drm_plane *plane,
7940 uint32_t crtc_index)
7942 struct amdgpu_crtc *acrtc = NULL;
7943 struct drm_plane *cursor_plane;
7947 cursor_plane = kzalloc(sizeof(*cursor_plane), GFP_KERNEL);
7951 cursor_plane->type = DRM_PLANE_TYPE_CURSOR;
7952 res = amdgpu_dm_plane_init(dm, cursor_plane, 0, NULL);
7954 acrtc = kzalloc(sizeof(struct amdgpu_crtc), GFP_KERNEL);
7958 res = drm_crtc_init_with_planes(
7963 &amdgpu_dm_crtc_funcs, NULL);
7968 drm_crtc_helper_add(&acrtc->base, &amdgpu_dm_crtc_helper_funcs);
7970 /* Create (reset) the plane state */
7971 if (acrtc->base.funcs->reset)
7972 acrtc->base.funcs->reset(&acrtc->base);
7974 acrtc->max_cursor_width = dm->adev->dm.dc->caps.max_cursor_size;
7975 acrtc->max_cursor_height = dm->adev->dm.dc->caps.max_cursor_size;
7977 acrtc->crtc_id = crtc_index;
7978 acrtc->base.enabled = false;
7979 acrtc->otg_inst = -1;
7981 dm->adev->mode_info.crtcs[crtc_index] = acrtc;
7982 drm_crtc_enable_color_mgmt(&acrtc->base, MAX_COLOR_LUT_ENTRIES,
7983 true, MAX_COLOR_LUT_ENTRIES);
7984 drm_mode_crtc_set_gamma_size(&acrtc->base, MAX_COLOR_LEGACY_LUT_ENTRIES);
7990 kfree(cursor_plane);
7995 static int to_drm_connector_type(enum signal_type st)
7998 case SIGNAL_TYPE_HDMI_TYPE_A:
7999 return DRM_MODE_CONNECTOR_HDMIA;
8000 case SIGNAL_TYPE_EDP:
8001 return DRM_MODE_CONNECTOR_eDP;
8002 case SIGNAL_TYPE_LVDS:
8003 return DRM_MODE_CONNECTOR_LVDS;
8004 case SIGNAL_TYPE_RGB:
8005 return DRM_MODE_CONNECTOR_VGA;
8006 case SIGNAL_TYPE_DISPLAY_PORT:
8007 case SIGNAL_TYPE_DISPLAY_PORT_MST:
8008 return DRM_MODE_CONNECTOR_DisplayPort;
8009 case SIGNAL_TYPE_DVI_DUAL_LINK:
8010 case SIGNAL_TYPE_DVI_SINGLE_LINK:
8011 return DRM_MODE_CONNECTOR_DVID;
8012 case SIGNAL_TYPE_VIRTUAL:
8013 return DRM_MODE_CONNECTOR_VIRTUAL;
8016 return DRM_MODE_CONNECTOR_Unknown;
8020 static struct drm_encoder *amdgpu_dm_connector_to_encoder(struct drm_connector *connector)
8022 struct drm_encoder *encoder;
8024 /* There is only one encoder per connector */
8025 drm_connector_for_each_possible_encoder(connector, encoder)
8031 static void amdgpu_dm_get_native_mode(struct drm_connector *connector)
8033 struct drm_encoder *encoder;
8034 struct amdgpu_encoder *amdgpu_encoder;
8036 encoder = amdgpu_dm_connector_to_encoder(connector);
8038 if (encoder == NULL)
8041 amdgpu_encoder = to_amdgpu_encoder(encoder);
8043 amdgpu_encoder->native_mode.clock = 0;
8045 if (!list_empty(&connector->probed_modes)) {
8046 struct drm_display_mode *preferred_mode = NULL;
8048 list_for_each_entry(preferred_mode,
8049 &connector->probed_modes,
8051 if (preferred_mode->type & DRM_MODE_TYPE_PREFERRED)
8052 amdgpu_encoder->native_mode = *preferred_mode;
8060 static struct drm_display_mode *
8061 amdgpu_dm_create_common_mode(struct drm_encoder *encoder,
8063 int hdisplay, int vdisplay)
8065 struct drm_device *dev = encoder->dev;
8066 struct amdgpu_encoder *amdgpu_encoder = to_amdgpu_encoder(encoder);
8067 struct drm_display_mode *mode = NULL;
8068 struct drm_display_mode *native_mode = &amdgpu_encoder->native_mode;
8070 mode = drm_mode_duplicate(dev, native_mode);
8075 mode->hdisplay = hdisplay;
8076 mode->vdisplay = vdisplay;
8077 mode->type &= ~DRM_MODE_TYPE_PREFERRED;
8078 strscpy(mode->name, name, DRM_DISPLAY_MODE_LEN);
8084 static void amdgpu_dm_connector_add_common_modes(struct drm_encoder *encoder,
8085 struct drm_connector *connector)
8087 struct amdgpu_encoder *amdgpu_encoder = to_amdgpu_encoder(encoder);
8088 struct drm_display_mode *mode = NULL;
8089 struct drm_display_mode *native_mode = &amdgpu_encoder->native_mode;
8090 struct amdgpu_dm_connector *amdgpu_dm_connector =
8091 to_amdgpu_dm_connector(connector);
8095 char name[DRM_DISPLAY_MODE_LEN];
8098 } common_modes[] = {
8099 { "640x480", 640, 480},
8100 { "800x600", 800, 600},
8101 { "1024x768", 1024, 768},
8102 { "1280x720", 1280, 720},
8103 { "1280x800", 1280, 800},
8104 {"1280x1024", 1280, 1024},
8105 { "1440x900", 1440, 900},
8106 {"1680x1050", 1680, 1050},
8107 {"1600x1200", 1600, 1200},
8108 {"1920x1080", 1920, 1080},
8109 {"1920x1200", 1920, 1200}
8112 n = ARRAY_SIZE(common_modes);
8114 for (i = 0; i < n; i++) {
8115 struct drm_display_mode *curmode = NULL;
8116 bool mode_existed = false;
8118 if (common_modes[i].w > native_mode->hdisplay ||
8119 common_modes[i].h > native_mode->vdisplay ||
8120 (common_modes[i].w == native_mode->hdisplay &&
8121 common_modes[i].h == native_mode->vdisplay))
8124 list_for_each_entry(curmode, &connector->probed_modes, head) {
8125 if (common_modes[i].w == curmode->hdisplay &&
8126 common_modes[i].h == curmode->vdisplay) {
8127 mode_existed = true;
8135 mode = amdgpu_dm_create_common_mode(encoder,
8136 common_modes[i].name, common_modes[i].w,
8141 drm_mode_probed_add(connector, mode);
8142 amdgpu_dm_connector->num_modes++;
8146 static void amdgpu_set_panel_orientation(struct drm_connector *connector)
8148 struct drm_encoder *encoder;
8149 struct amdgpu_encoder *amdgpu_encoder;
8150 const struct drm_display_mode *native_mode;
8152 if (connector->connector_type != DRM_MODE_CONNECTOR_eDP &&
8153 connector->connector_type != DRM_MODE_CONNECTOR_LVDS)
8156 encoder = amdgpu_dm_connector_to_encoder(connector);
8160 amdgpu_encoder = to_amdgpu_encoder(encoder);
8162 native_mode = &amdgpu_encoder->native_mode;
8163 if (native_mode->hdisplay == 0 || native_mode->vdisplay == 0)
8166 drm_connector_set_panel_orientation_with_quirk(connector,
8167 DRM_MODE_PANEL_ORIENTATION_UNKNOWN,
8168 native_mode->hdisplay,
8169 native_mode->vdisplay);
8172 static void amdgpu_dm_connector_ddc_get_modes(struct drm_connector *connector,
8175 struct amdgpu_dm_connector *amdgpu_dm_connector =
8176 to_amdgpu_dm_connector(connector);
8179 /* empty probed_modes */
8180 INIT_LIST_HEAD(&connector->probed_modes);
8181 amdgpu_dm_connector->num_modes =
8182 drm_add_edid_modes(connector, edid);
8184 /* sorting the probed modes before calling function
8185 * amdgpu_dm_get_native_mode() since EDID can have
8186 * more than one preferred mode. The modes that are
8187 * later in the probed mode list could be of higher
8188 * and preferred resolution. For example, 3840x2160
8189 * resolution in base EDID preferred timing and 4096x2160
8190 * preferred resolution in DID extension block later.
8192 drm_mode_sort(&connector->probed_modes);
8193 amdgpu_dm_get_native_mode(connector);
8195 /* Freesync capabilities are reset by calling
8196 * drm_add_edid_modes() and need to be
8199 amdgpu_dm_update_freesync_caps(connector, edid);
8201 amdgpu_set_panel_orientation(connector);
8203 amdgpu_dm_connector->num_modes = 0;
8207 static bool is_duplicate_mode(struct amdgpu_dm_connector *aconnector,
8208 struct drm_display_mode *mode)
8210 struct drm_display_mode *m;
8212 list_for_each_entry (m, &aconnector->base.probed_modes, head) {
8213 if (drm_mode_equal(m, mode))
8220 static uint add_fs_modes(struct amdgpu_dm_connector *aconnector)
8222 const struct drm_display_mode *m;
8223 struct drm_display_mode *new_mode;
8225 uint32_t new_modes_count = 0;
8227 /* Standard FPS values
8236 * 60 - Commonly used
8237 * 48,72,96,120 - Multiples of 24
8239 static const uint32_t common_rates[] = {
8240 23976, 24000, 25000, 29970, 30000,
8241 48000, 50000, 60000, 72000, 96000, 120000
8245 * Find mode with highest refresh rate with the same resolution
8246 * as the preferred mode. Some monitors report a preferred mode
8247 * with lower resolution than the highest refresh rate supported.
8250 m = get_highest_refresh_rate_mode(aconnector, true);
8254 for (i = 0; i < ARRAY_SIZE(common_rates); i++) {
8255 uint64_t target_vtotal, target_vtotal_diff;
8258 if (drm_mode_vrefresh(m) * 1000 < common_rates[i])
8261 if (common_rates[i] < aconnector->min_vfreq * 1000 ||
8262 common_rates[i] > aconnector->max_vfreq * 1000)
8265 num = (unsigned long long)m->clock * 1000 * 1000;
8266 den = common_rates[i] * (unsigned long long)m->htotal;
8267 target_vtotal = div_u64(num, den);
8268 target_vtotal_diff = target_vtotal - m->vtotal;
8270 /* Check for illegal modes */
8271 if (m->vsync_start + target_vtotal_diff < m->vdisplay ||
8272 m->vsync_end + target_vtotal_diff < m->vsync_start ||
8273 m->vtotal + target_vtotal_diff < m->vsync_end)
8276 new_mode = drm_mode_duplicate(aconnector->base.dev, m);
8280 new_mode->vtotal += (u16)target_vtotal_diff;
8281 new_mode->vsync_start += (u16)target_vtotal_diff;
8282 new_mode->vsync_end += (u16)target_vtotal_diff;
8283 new_mode->type &= ~DRM_MODE_TYPE_PREFERRED;
8284 new_mode->type |= DRM_MODE_TYPE_DRIVER;
8286 if (!is_duplicate_mode(aconnector, new_mode)) {
8287 drm_mode_probed_add(&aconnector->base, new_mode);
8288 new_modes_count += 1;
8290 drm_mode_destroy(aconnector->base.dev, new_mode);
8293 return new_modes_count;
8296 static void amdgpu_dm_connector_add_freesync_modes(struct drm_connector *connector,
8299 struct amdgpu_dm_connector *amdgpu_dm_connector =
8300 to_amdgpu_dm_connector(connector);
8305 if (amdgpu_dm_connector->max_vfreq - amdgpu_dm_connector->min_vfreq > 10)
8306 amdgpu_dm_connector->num_modes +=
8307 add_fs_modes(amdgpu_dm_connector);
8310 static int amdgpu_dm_connector_get_modes(struct drm_connector *connector)
8312 struct amdgpu_dm_connector *amdgpu_dm_connector =
8313 to_amdgpu_dm_connector(connector);
8314 struct drm_encoder *encoder;
8315 struct edid *edid = amdgpu_dm_connector->edid;
8317 encoder = amdgpu_dm_connector_to_encoder(connector);
8319 if (!drm_edid_is_valid(edid)) {
8320 amdgpu_dm_connector->num_modes =
8321 drm_add_modes_noedid(connector, 640, 480);
8323 amdgpu_dm_connector_ddc_get_modes(connector, edid);
8324 amdgpu_dm_connector_add_common_modes(encoder, connector);
8325 amdgpu_dm_connector_add_freesync_modes(connector, edid);
8327 amdgpu_dm_fbc_init(connector);
8329 return amdgpu_dm_connector->num_modes;
8332 void amdgpu_dm_connector_init_helper(struct amdgpu_display_manager *dm,
8333 struct amdgpu_dm_connector *aconnector,
8335 struct dc_link *link,
8338 struct amdgpu_device *adev = drm_to_adev(dm->ddev);
8341 * Some of the properties below require access to state, like bpc.
8342 * Allocate some default initial connector state with our reset helper.
8344 if (aconnector->base.funcs->reset)
8345 aconnector->base.funcs->reset(&aconnector->base);
8347 aconnector->connector_id = link_index;
8348 aconnector->dc_link = link;
8349 aconnector->base.interlace_allowed = false;
8350 aconnector->base.doublescan_allowed = false;
8351 aconnector->base.stereo_allowed = false;
8352 aconnector->base.dpms = DRM_MODE_DPMS_OFF;
8353 aconnector->hpd.hpd = AMDGPU_HPD_NONE; /* not used */
8354 aconnector->audio_inst = -1;
8355 mutex_init(&aconnector->hpd_lock);
8358 * configure support HPD hot plug connector_>polled default value is 0
8359 * which means HPD hot plug not supported
8361 switch (connector_type) {
8362 case DRM_MODE_CONNECTOR_HDMIA:
8363 aconnector->base.polled = DRM_CONNECTOR_POLL_HPD;
8364 aconnector->base.ycbcr_420_allowed =
8365 link->link_enc->features.hdmi_ycbcr420_supported ? true : false;
8367 case DRM_MODE_CONNECTOR_DisplayPort:
8368 aconnector->base.polled = DRM_CONNECTOR_POLL_HPD;
8369 link->link_enc = link_enc_cfg_get_link_enc(link);
8370 ASSERT(link->link_enc);
8372 aconnector->base.ycbcr_420_allowed =
8373 link->link_enc->features.dp_ycbcr420_supported ? true : false;
8375 case DRM_MODE_CONNECTOR_DVID:
8376 aconnector->base.polled = DRM_CONNECTOR_POLL_HPD;
8382 drm_object_attach_property(&aconnector->base.base,
8383 dm->ddev->mode_config.scaling_mode_property,
8384 DRM_MODE_SCALE_NONE);
8386 drm_object_attach_property(&aconnector->base.base,
8387 adev->mode_info.underscan_property,
8389 drm_object_attach_property(&aconnector->base.base,
8390 adev->mode_info.underscan_hborder_property,
8392 drm_object_attach_property(&aconnector->base.base,
8393 adev->mode_info.underscan_vborder_property,
8396 if (!aconnector->mst_port)
8397 drm_connector_attach_max_bpc_property(&aconnector->base, 8, 16);
8399 /* This defaults to the max in the range, but we want 8bpc for non-edp. */
8400 aconnector->base.state->max_bpc = (connector_type == DRM_MODE_CONNECTOR_eDP) ? 16 : 8;
8401 aconnector->base.state->max_requested_bpc = aconnector->base.state->max_bpc;
8403 if (connector_type == DRM_MODE_CONNECTOR_eDP &&
8404 (dc_is_dmcu_initialized(adev->dm.dc) || adev->dm.dc->ctx->dmub_srv)) {
8405 drm_object_attach_property(&aconnector->base.base,
8406 adev->mode_info.abm_level_property, 0);
8409 if (connector_type == DRM_MODE_CONNECTOR_HDMIA ||
8410 connector_type == DRM_MODE_CONNECTOR_DisplayPort ||
8411 connector_type == DRM_MODE_CONNECTOR_eDP) {
8412 drm_connector_attach_hdr_output_metadata_property(&aconnector->base);
8414 if (!aconnector->mst_port)
8415 drm_connector_attach_vrr_capable_property(&aconnector->base);
8417 #ifdef CONFIG_DRM_AMD_DC_HDCP
8418 if (adev->dm.hdcp_workqueue)
8419 drm_connector_attach_content_protection_property(&aconnector->base, true);
8424 static int amdgpu_dm_i2c_xfer(struct i2c_adapter *i2c_adap,
8425 struct i2c_msg *msgs, int num)
8427 struct amdgpu_i2c_adapter *i2c = i2c_get_adapdata(i2c_adap);
8428 struct ddc_service *ddc_service = i2c->ddc_service;
8429 struct i2c_command cmd;
8433 cmd.payloads = kcalloc(num, sizeof(struct i2c_payload), GFP_KERNEL);
8438 cmd.number_of_payloads = num;
8439 cmd.engine = I2C_COMMAND_ENGINE_DEFAULT;
8442 for (i = 0; i < num; i++) {
8443 cmd.payloads[i].write = !(msgs[i].flags & I2C_M_RD);
8444 cmd.payloads[i].address = msgs[i].addr;
8445 cmd.payloads[i].length = msgs[i].len;
8446 cmd.payloads[i].data = msgs[i].buf;
8450 ddc_service->ctx->dc,
8451 ddc_service->ddc_pin->hw_info.ddc_channel,
8455 kfree(cmd.payloads);
8459 static u32 amdgpu_dm_i2c_func(struct i2c_adapter *adap)
8461 return I2C_FUNC_I2C | I2C_FUNC_SMBUS_EMUL;
8464 static const struct i2c_algorithm amdgpu_dm_i2c_algo = {
8465 .master_xfer = amdgpu_dm_i2c_xfer,
8466 .functionality = amdgpu_dm_i2c_func,
8469 static struct amdgpu_i2c_adapter *
8470 create_i2c(struct ddc_service *ddc_service,
8474 struct amdgpu_device *adev = ddc_service->ctx->driver_context;
8475 struct amdgpu_i2c_adapter *i2c;
8477 i2c = kzalloc(sizeof(struct amdgpu_i2c_adapter), GFP_KERNEL);
8480 i2c->base.owner = THIS_MODULE;
8481 i2c->base.class = I2C_CLASS_DDC;
8482 i2c->base.dev.parent = &adev->pdev->dev;
8483 i2c->base.algo = &amdgpu_dm_i2c_algo;
8484 snprintf(i2c->base.name, sizeof(i2c->base.name), "AMDGPU DM i2c hw bus %d", link_index);
8485 i2c_set_adapdata(&i2c->base, i2c);
8486 i2c->ddc_service = ddc_service;
8487 if (i2c->ddc_service->ddc_pin)
8488 i2c->ddc_service->ddc_pin->hw_info.ddc_channel = link_index;
8495 * Note: this function assumes that dc_link_detect() was called for the
8496 * dc_link which will be represented by this aconnector.
8498 static int amdgpu_dm_connector_init(struct amdgpu_display_manager *dm,
8499 struct amdgpu_dm_connector *aconnector,
8500 uint32_t link_index,
8501 struct amdgpu_encoder *aencoder)
8505 struct dc *dc = dm->dc;
8506 struct dc_link *link = dc_get_link_at_index(dc, link_index);
8507 struct amdgpu_i2c_adapter *i2c;
8509 link->priv = aconnector;
8511 DRM_DEBUG_DRIVER("%s()\n", __func__);
8513 i2c = create_i2c(link->ddc, link->link_index, &res);
8515 DRM_ERROR("Failed to create i2c adapter data\n");
8519 aconnector->i2c = i2c;
8520 res = i2c_add_adapter(&i2c->base);
8523 DRM_ERROR("Failed to register hw i2c %d\n", link->link_index);
8527 connector_type = to_drm_connector_type(link->connector_signal);
8529 res = drm_connector_init_with_ddc(
8532 &amdgpu_dm_connector_funcs,
8537 DRM_ERROR("connector_init failed\n");
8538 aconnector->connector_id = -1;
8542 drm_connector_helper_add(
8544 &amdgpu_dm_connector_helper_funcs);
8546 amdgpu_dm_connector_init_helper(
8553 drm_connector_attach_encoder(
8554 &aconnector->base, &aencoder->base);
8556 if (connector_type == DRM_MODE_CONNECTOR_DisplayPort
8557 || connector_type == DRM_MODE_CONNECTOR_eDP)
8558 amdgpu_dm_initialize_dp_connector(dm, aconnector, link->link_index);
8563 aconnector->i2c = NULL;
8568 int amdgpu_dm_get_encoder_crtc_mask(struct amdgpu_device *adev)
8570 switch (adev->mode_info.num_crtc) {
8587 static int amdgpu_dm_encoder_init(struct drm_device *dev,
8588 struct amdgpu_encoder *aencoder,
8589 uint32_t link_index)
8591 struct amdgpu_device *adev = drm_to_adev(dev);
8593 int res = drm_encoder_init(dev,
8595 &amdgpu_dm_encoder_funcs,
8596 DRM_MODE_ENCODER_TMDS,
8599 aencoder->base.possible_crtcs = amdgpu_dm_get_encoder_crtc_mask(adev);
8602 aencoder->encoder_id = link_index;
8604 aencoder->encoder_id = -1;
8606 drm_encoder_helper_add(&aencoder->base, &amdgpu_dm_encoder_helper_funcs);
8611 static void manage_dm_interrupts(struct amdgpu_device *adev,
8612 struct amdgpu_crtc *acrtc,
8616 * We have no guarantee that the frontend index maps to the same
8617 * backend index - some even map to more than one.
8619 * TODO: Use a different interrupt or check DC itself for the mapping.
8622 amdgpu_display_crtc_idx_to_irq_type(
8627 drm_crtc_vblank_on(&acrtc->base);
8630 &adev->pageflip_irq,
8632 #if defined(CONFIG_DRM_AMD_SECURE_DISPLAY)
8639 #if defined(CONFIG_DRM_AMD_SECURE_DISPLAY)
8647 &adev->pageflip_irq,
8649 drm_crtc_vblank_off(&acrtc->base);
8653 static void dm_update_pflip_irq_state(struct amdgpu_device *adev,
8654 struct amdgpu_crtc *acrtc)
8657 amdgpu_display_crtc_idx_to_irq_type(adev, acrtc->crtc_id);
8660 * This reads the current state for the IRQ and force reapplies
8661 * the setting to hardware.
8663 amdgpu_irq_update(adev, &adev->pageflip_irq, irq_type);
8667 is_scaling_state_different(const struct dm_connector_state *dm_state,
8668 const struct dm_connector_state *old_dm_state)
8670 if (dm_state->scaling != old_dm_state->scaling)
8672 if (!dm_state->underscan_enable && old_dm_state->underscan_enable) {
8673 if (old_dm_state->underscan_hborder != 0 && old_dm_state->underscan_vborder != 0)
8675 } else if (dm_state->underscan_enable && !old_dm_state->underscan_enable) {
8676 if (dm_state->underscan_hborder != 0 && dm_state->underscan_vborder != 0)
8678 } else if (dm_state->underscan_hborder != old_dm_state->underscan_hborder ||
8679 dm_state->underscan_vborder != old_dm_state->underscan_vborder)
8684 #ifdef CONFIG_DRM_AMD_DC_HDCP
8685 static bool is_content_protection_different(struct drm_connector_state *state,
8686 const struct drm_connector_state *old_state,
8687 const struct drm_connector *connector, struct hdcp_workqueue *hdcp_w)
8689 struct amdgpu_dm_connector *aconnector = to_amdgpu_dm_connector(connector);
8690 struct dm_connector_state *dm_con_state = to_dm_connector_state(connector->state);
8692 /* Handle: Type0/1 change */
8693 if (old_state->hdcp_content_type != state->hdcp_content_type &&
8694 state->content_protection != DRM_MODE_CONTENT_PROTECTION_UNDESIRED) {
8695 state->content_protection = DRM_MODE_CONTENT_PROTECTION_DESIRED;
8699 /* CP is being re enabled, ignore this
8701 * Handles: ENABLED -> DESIRED
8703 if (old_state->content_protection == DRM_MODE_CONTENT_PROTECTION_ENABLED &&
8704 state->content_protection == DRM_MODE_CONTENT_PROTECTION_DESIRED) {
8705 state->content_protection = DRM_MODE_CONTENT_PROTECTION_ENABLED;
8709 /* S3 resume case, since old state will always be 0 (UNDESIRED) and the restored state will be ENABLED
8711 * Handles: UNDESIRED -> ENABLED
8713 if (old_state->content_protection == DRM_MODE_CONTENT_PROTECTION_UNDESIRED &&
8714 state->content_protection == DRM_MODE_CONTENT_PROTECTION_ENABLED)
8715 state->content_protection = DRM_MODE_CONTENT_PROTECTION_DESIRED;
8717 /* Stream removed and re-enabled
8719 * Can sometimes overlap with the HPD case,
8720 * thus set update_hdcp to false to avoid
8721 * setting HDCP multiple times.
8723 * Handles: DESIRED -> DESIRED (Special case)
8725 if (!(old_state->crtc && old_state->crtc->enabled) &&
8726 state->crtc && state->crtc->enabled &&
8727 connector->state->content_protection == DRM_MODE_CONTENT_PROTECTION_DESIRED) {
8728 dm_con_state->update_hdcp = false;
8732 /* Hot-plug, headless s3, dpms
8734 * Only start HDCP if the display is connected/enabled.
8735 * update_hdcp flag will be set to false until the next
8738 * Handles: DESIRED -> DESIRED (Special case)
8740 if (dm_con_state->update_hdcp && state->content_protection == DRM_MODE_CONTENT_PROTECTION_DESIRED &&
8741 connector->dpms == DRM_MODE_DPMS_ON && aconnector->dc_sink != NULL) {
8742 dm_con_state->update_hdcp = false;
8747 * Handles: UNDESIRED -> UNDESIRED
8748 * DESIRED -> DESIRED
8749 * ENABLED -> ENABLED
8751 if (old_state->content_protection == state->content_protection)
8755 * Handles: UNDESIRED -> DESIRED
8756 * DESIRED -> UNDESIRED
8757 * ENABLED -> UNDESIRED
8759 if (state->content_protection != DRM_MODE_CONTENT_PROTECTION_ENABLED)
8763 * Handles: DESIRED -> ENABLED
8769 static void remove_stream(struct amdgpu_device *adev,
8770 struct amdgpu_crtc *acrtc,
8771 struct dc_stream_state *stream)
8773 /* this is the update mode case */
8775 acrtc->otg_inst = -1;
8776 acrtc->enabled = false;
8779 static int get_cursor_position(struct drm_plane *plane, struct drm_crtc *crtc,
8780 struct dc_cursor_position *position)
8782 struct amdgpu_crtc *amdgpu_crtc = to_amdgpu_crtc(crtc);
8784 int xorigin = 0, yorigin = 0;
8786 if (!crtc || !plane->state->fb)
8789 if ((plane->state->crtc_w > amdgpu_crtc->max_cursor_width) ||
8790 (plane->state->crtc_h > amdgpu_crtc->max_cursor_height)) {
8791 DRM_ERROR("%s: bad cursor width or height %d x %d\n",
8793 plane->state->crtc_w,
8794 plane->state->crtc_h);
8798 x = plane->state->crtc_x;
8799 y = plane->state->crtc_y;
8801 if (x <= -amdgpu_crtc->max_cursor_width ||
8802 y <= -amdgpu_crtc->max_cursor_height)
8806 xorigin = min(-x, amdgpu_crtc->max_cursor_width - 1);
8810 yorigin = min(-y, amdgpu_crtc->max_cursor_height - 1);
8813 position->enable = true;
8814 position->translate_by_source = true;
8817 position->x_hotspot = xorigin;
8818 position->y_hotspot = yorigin;
8823 static void handle_cursor_update(struct drm_plane *plane,
8824 struct drm_plane_state *old_plane_state)
8826 struct amdgpu_device *adev = drm_to_adev(plane->dev);
8827 struct amdgpu_framebuffer *afb = to_amdgpu_framebuffer(plane->state->fb);
8828 struct drm_crtc *crtc = afb ? plane->state->crtc : old_plane_state->crtc;
8829 struct dm_crtc_state *crtc_state = crtc ? to_dm_crtc_state(crtc->state) : NULL;
8830 struct amdgpu_crtc *amdgpu_crtc = to_amdgpu_crtc(crtc);
8831 uint64_t address = afb ? afb->address : 0;
8832 struct dc_cursor_position position = {0};
8833 struct dc_cursor_attributes attributes;
8836 if (!plane->state->fb && !old_plane_state->fb)
8839 DC_LOG_CURSOR("%s: crtc_id=%d with size %d to %d\n",
8841 amdgpu_crtc->crtc_id,
8842 plane->state->crtc_w,
8843 plane->state->crtc_h);
8845 ret = get_cursor_position(plane, crtc, &position);
8849 if (!position.enable) {
8850 /* turn off cursor */
8851 if (crtc_state && crtc_state->stream) {
8852 mutex_lock(&adev->dm.dc_lock);
8853 dc_stream_set_cursor_position(crtc_state->stream,
8855 mutex_unlock(&adev->dm.dc_lock);
8860 amdgpu_crtc->cursor_width = plane->state->crtc_w;
8861 amdgpu_crtc->cursor_height = plane->state->crtc_h;
8863 memset(&attributes, 0, sizeof(attributes));
8864 attributes.address.high_part = upper_32_bits(address);
8865 attributes.address.low_part = lower_32_bits(address);
8866 attributes.width = plane->state->crtc_w;
8867 attributes.height = plane->state->crtc_h;
8868 attributes.color_format = CURSOR_MODE_COLOR_PRE_MULTIPLIED_ALPHA;
8869 attributes.rotation_angle = 0;
8870 attributes.attribute_flags.value = 0;
8872 attributes.pitch = afb->base.pitches[0] / afb->base.format->cpp[0];
8874 if (crtc_state->stream) {
8875 mutex_lock(&adev->dm.dc_lock);
8876 if (!dc_stream_set_cursor_attributes(crtc_state->stream,
8878 DRM_ERROR("DC failed to set cursor attributes\n");
8880 if (!dc_stream_set_cursor_position(crtc_state->stream,
8882 DRM_ERROR("DC failed to set cursor position\n");
8883 mutex_unlock(&adev->dm.dc_lock);
8887 static void prepare_flip_isr(struct amdgpu_crtc *acrtc)
8890 assert_spin_locked(&acrtc->base.dev->event_lock);
8891 WARN_ON(acrtc->event);
8893 acrtc->event = acrtc->base.state->event;
8895 /* Set the flip status */
8896 acrtc->pflip_status = AMDGPU_FLIP_SUBMITTED;
8898 /* Mark this event as consumed */
8899 acrtc->base.state->event = NULL;
8901 DC_LOG_PFLIP("crtc:%d, pflip_stat:AMDGPU_FLIP_SUBMITTED\n",
8905 static void update_freesync_state_on_stream(
8906 struct amdgpu_display_manager *dm,
8907 struct dm_crtc_state *new_crtc_state,
8908 struct dc_stream_state *new_stream,
8909 struct dc_plane_state *surface,
8910 u32 flip_timestamp_in_us)
8912 struct mod_vrr_params vrr_params;
8913 struct dc_info_packet vrr_infopacket = {0};
8914 struct amdgpu_device *adev = dm->adev;
8915 struct amdgpu_crtc *acrtc = to_amdgpu_crtc(new_crtc_state->base.crtc);
8916 unsigned long flags;
8917 bool pack_sdp_v1_3 = false;
8923 * TODO: Determine why min/max totals and vrefresh can be 0 here.
8924 * For now it's sufficient to just guard against these conditions.
8927 if (!new_stream->timing.h_total || !new_stream->timing.v_total)
8930 spin_lock_irqsave(&adev_to_drm(adev)->event_lock, flags);
8931 vrr_params = acrtc->dm_irq_params.vrr_params;
8934 mod_freesync_handle_preflip(
8935 dm->freesync_module,
8938 flip_timestamp_in_us,
8941 if (adev->family < AMDGPU_FAMILY_AI &&
8942 amdgpu_dm_vrr_active(new_crtc_state)) {
8943 mod_freesync_handle_v_update(dm->freesync_module,
8944 new_stream, &vrr_params);
8946 /* Need to call this before the frame ends. */
8947 dc_stream_adjust_vmin_vmax(dm->dc,
8948 new_crtc_state->stream,
8949 &vrr_params.adjust);
8953 mod_freesync_build_vrr_infopacket(
8954 dm->freesync_module,
8958 TRANSFER_FUNC_UNKNOWN,
8962 new_crtc_state->freesync_timing_changed |=
8963 (memcmp(&acrtc->dm_irq_params.vrr_params.adjust,
8965 sizeof(vrr_params.adjust)) != 0);
8967 new_crtc_state->freesync_vrr_info_changed |=
8968 (memcmp(&new_crtc_state->vrr_infopacket,
8970 sizeof(vrr_infopacket)) != 0);
8972 acrtc->dm_irq_params.vrr_params = vrr_params;
8973 new_crtc_state->vrr_infopacket = vrr_infopacket;
8975 new_stream->adjust = acrtc->dm_irq_params.vrr_params.adjust;
8976 new_stream->vrr_infopacket = vrr_infopacket;
8978 if (new_crtc_state->freesync_vrr_info_changed)
8979 DRM_DEBUG_KMS("VRR packet update: crtc=%u enabled=%d state=%d",
8980 new_crtc_state->base.crtc->base.id,
8981 (int)new_crtc_state->base.vrr_enabled,
8982 (int)vrr_params.state);
8984 spin_unlock_irqrestore(&adev_to_drm(adev)->event_lock, flags);
8987 static void update_stream_irq_parameters(
8988 struct amdgpu_display_manager *dm,
8989 struct dm_crtc_state *new_crtc_state)
8991 struct dc_stream_state *new_stream = new_crtc_state->stream;
8992 struct mod_vrr_params vrr_params;
8993 struct mod_freesync_config config = new_crtc_state->freesync_config;
8994 struct amdgpu_device *adev = dm->adev;
8995 struct amdgpu_crtc *acrtc = to_amdgpu_crtc(new_crtc_state->base.crtc);
8996 unsigned long flags;
9002 * TODO: Determine why min/max totals and vrefresh can be 0 here.
9003 * For now it's sufficient to just guard against these conditions.
9005 if (!new_stream->timing.h_total || !new_stream->timing.v_total)
9008 spin_lock_irqsave(&adev_to_drm(adev)->event_lock, flags);
9009 vrr_params = acrtc->dm_irq_params.vrr_params;
9011 if (new_crtc_state->vrr_supported &&
9012 config.min_refresh_in_uhz &&
9013 config.max_refresh_in_uhz) {
9015 * if freesync compatible mode was set, config.state will be set
9018 if (config.state == VRR_STATE_ACTIVE_FIXED && config.fixed_refresh_in_uhz &&
9019 (!drm_atomic_crtc_needs_modeset(&new_crtc_state->base) ||
9020 new_crtc_state->freesync_config.state == VRR_STATE_ACTIVE_FIXED)) {
9021 vrr_params.max_refresh_in_uhz = config.max_refresh_in_uhz;
9022 vrr_params.min_refresh_in_uhz = config.min_refresh_in_uhz;
9023 vrr_params.fixed_refresh_in_uhz = config.fixed_refresh_in_uhz;
9024 vrr_params.state = VRR_STATE_ACTIVE_FIXED;
9026 config.state = new_crtc_state->base.vrr_enabled ?
9027 VRR_STATE_ACTIVE_VARIABLE :
9031 config.state = VRR_STATE_UNSUPPORTED;
9034 mod_freesync_build_vrr_params(dm->freesync_module,
9036 &config, &vrr_params);
9038 new_crtc_state->freesync_timing_changed |=
9039 (memcmp(&acrtc->dm_irq_params.vrr_params.adjust,
9040 &vrr_params.adjust, sizeof(vrr_params.adjust)) != 0);
9042 new_crtc_state->freesync_config = config;
9043 /* Copy state for access from DM IRQ handler */
9044 acrtc->dm_irq_params.freesync_config = config;
9045 acrtc->dm_irq_params.active_planes = new_crtc_state->active_planes;
9046 acrtc->dm_irq_params.vrr_params = vrr_params;
9047 spin_unlock_irqrestore(&adev_to_drm(adev)->event_lock, flags);
9050 static void amdgpu_dm_handle_vrr_transition(struct dm_crtc_state *old_state,
9051 struct dm_crtc_state *new_state)
9053 bool old_vrr_active = amdgpu_dm_vrr_active(old_state);
9054 bool new_vrr_active = amdgpu_dm_vrr_active(new_state);
9056 if (!old_vrr_active && new_vrr_active) {
9057 /* Transition VRR inactive -> active:
9058 * While VRR is active, we must not disable vblank irq, as a
9059 * reenable after disable would compute bogus vblank/pflip
9060 * timestamps if it likely happened inside display front-porch.
9062 * We also need vupdate irq for the actual core vblank handling
9065 dm_set_vupdate_irq(new_state->base.crtc, true);
9066 drm_crtc_vblank_get(new_state->base.crtc);
9067 DRM_DEBUG_DRIVER("%s: crtc=%u VRR off->on: Get vblank ref\n",
9068 __func__, new_state->base.crtc->base.id);
9069 } else if (old_vrr_active && !new_vrr_active) {
9070 /* Transition VRR active -> inactive:
9071 * Allow vblank irq disable again for fixed refresh rate.
9073 dm_set_vupdate_irq(new_state->base.crtc, false);
9074 drm_crtc_vblank_put(new_state->base.crtc);
9075 DRM_DEBUG_DRIVER("%s: crtc=%u VRR on->off: Drop vblank ref\n",
9076 __func__, new_state->base.crtc->base.id);
9080 static void amdgpu_dm_commit_cursors(struct drm_atomic_state *state)
9082 struct drm_plane *plane;
9083 struct drm_plane_state *old_plane_state;
9087 * TODO: Make this per-stream so we don't issue redundant updates for
9088 * commits with multiple streams.
9090 for_each_old_plane_in_state(state, plane, old_plane_state, i)
9091 if (plane->type == DRM_PLANE_TYPE_CURSOR)
9092 handle_cursor_update(plane, old_plane_state);
9095 static void amdgpu_dm_commit_planes(struct drm_atomic_state *state,
9096 struct dc_state *dc_state,
9097 struct drm_device *dev,
9098 struct amdgpu_display_manager *dm,
9099 struct drm_crtc *pcrtc,
9100 bool wait_for_vblank)
9103 uint64_t timestamp_ns;
9104 struct drm_plane *plane;
9105 struct drm_plane_state *old_plane_state, *new_plane_state;
9106 struct amdgpu_crtc *acrtc_attach = to_amdgpu_crtc(pcrtc);
9107 struct drm_crtc_state *new_pcrtc_state =
9108 drm_atomic_get_new_crtc_state(state, pcrtc);
9109 struct dm_crtc_state *acrtc_state = to_dm_crtc_state(new_pcrtc_state);
9110 struct dm_crtc_state *dm_old_crtc_state =
9111 to_dm_crtc_state(drm_atomic_get_old_crtc_state(state, pcrtc));
9112 int planes_count = 0, vpos, hpos;
9114 unsigned long flags;
9115 struct amdgpu_bo *abo;
9116 uint32_t target_vblank, last_flip_vblank;
9117 bool vrr_active = amdgpu_dm_vrr_active(acrtc_state);
9118 bool pflip_present = false;
9120 struct dc_surface_update surface_updates[MAX_SURFACES];
9121 struct dc_plane_info plane_infos[MAX_SURFACES];
9122 struct dc_scaling_info scaling_infos[MAX_SURFACES];
9123 struct dc_flip_addrs flip_addrs[MAX_SURFACES];
9124 struct dc_stream_update stream_update;
9127 bundle = kzalloc(sizeof(*bundle), GFP_KERNEL);
9130 dm_error("Failed to allocate update bundle\n");
9135 * Disable the cursor first if we're disabling all the planes.
9136 * It'll remain on the screen after the planes are re-enabled
9139 if (acrtc_state->active_planes == 0)
9140 amdgpu_dm_commit_cursors(state);
9142 /* update planes when needed */
9143 for_each_oldnew_plane_in_state(state, plane, old_plane_state, new_plane_state, i) {
9144 struct drm_crtc *crtc = new_plane_state->crtc;
9145 struct drm_crtc_state *new_crtc_state;
9146 struct drm_framebuffer *fb = new_plane_state->fb;
9147 struct amdgpu_framebuffer *afb = (struct amdgpu_framebuffer *)fb;
9148 bool plane_needs_flip;
9149 struct dc_plane_state *dc_plane;
9150 struct dm_plane_state *dm_new_plane_state = to_dm_plane_state(new_plane_state);
9152 /* Cursor plane is handled after stream updates */
9153 if (plane->type == DRM_PLANE_TYPE_CURSOR)
9156 if (!fb || !crtc || pcrtc != crtc)
9159 new_crtc_state = drm_atomic_get_new_crtc_state(state, crtc);
9160 if (!new_crtc_state->active)
9163 dc_plane = dm_new_plane_state->dc_state;
9165 bundle->surface_updates[planes_count].surface = dc_plane;
9166 if (new_pcrtc_state->color_mgmt_changed) {
9167 bundle->surface_updates[planes_count].gamma = dc_plane->gamma_correction;
9168 bundle->surface_updates[planes_count].in_transfer_func = dc_plane->in_transfer_func;
9169 bundle->surface_updates[planes_count].gamut_remap_matrix = &dc_plane->gamut_remap_matrix;
9172 fill_dc_scaling_info(dm->adev, new_plane_state,
9173 &bundle->scaling_infos[planes_count]);
9175 bundle->surface_updates[planes_count].scaling_info =
9176 &bundle->scaling_infos[planes_count];
9178 plane_needs_flip = old_plane_state->fb && new_plane_state->fb;
9180 pflip_present = pflip_present || plane_needs_flip;
9182 if (!plane_needs_flip) {
9187 abo = gem_to_amdgpu_bo(fb->obj[0]);
9190 * Wait for all fences on this FB. Do limited wait to avoid
9191 * deadlock during GPU reset when this fence will not signal
9192 * but we hold reservation lock for the BO.
9194 r = dma_resv_wait_timeout(abo->tbo.base.resv, true, false,
9195 msecs_to_jiffies(5000));
9196 if (unlikely(r <= 0))
9197 DRM_ERROR("Waiting for fences timed out!");
9199 fill_dc_plane_info_and_addr(
9200 dm->adev, new_plane_state,
9202 &bundle->plane_infos[planes_count],
9203 &bundle->flip_addrs[planes_count].address,
9204 afb->tmz_surface, false);
9206 DRM_DEBUG_ATOMIC("plane: id=%d dcc_en=%d\n",
9207 new_plane_state->plane->index,
9208 bundle->plane_infos[planes_count].dcc.enable);
9210 bundle->surface_updates[planes_count].plane_info =
9211 &bundle->plane_infos[planes_count];
9214 * Only allow immediate flips for fast updates that don't
9215 * change FB pitch, DCC state, rotation or mirroing.
9217 bundle->flip_addrs[planes_count].flip_immediate =
9218 crtc->state->async_flip &&
9219 acrtc_state->update_type == UPDATE_TYPE_FAST;
9221 timestamp_ns = ktime_get_ns();
9222 bundle->flip_addrs[planes_count].flip_timestamp_in_us = div_u64(timestamp_ns, 1000);
9223 bundle->surface_updates[planes_count].flip_addr = &bundle->flip_addrs[planes_count];
9224 bundle->surface_updates[planes_count].surface = dc_plane;
9226 if (!bundle->surface_updates[planes_count].surface) {
9227 DRM_ERROR("No surface for CRTC: id=%d\n",
9228 acrtc_attach->crtc_id);
9232 if (plane == pcrtc->primary)
9233 update_freesync_state_on_stream(
9236 acrtc_state->stream,
9238 bundle->flip_addrs[planes_count].flip_timestamp_in_us);
9240 DRM_DEBUG_ATOMIC("%s Flipping to hi: 0x%x, low: 0x%x\n",
9242 bundle->flip_addrs[planes_count].address.grph.addr.high_part,
9243 bundle->flip_addrs[planes_count].address.grph.addr.low_part);
9249 if (pflip_present) {
9251 /* Use old throttling in non-vrr fixed refresh rate mode
9252 * to keep flip scheduling based on target vblank counts
9253 * working in a backwards compatible way, e.g., for
9254 * clients using the GLX_OML_sync_control extension or
9255 * DRI3/Present extension with defined target_msc.
9257 last_flip_vblank = amdgpu_get_vblank_counter_kms(pcrtc);
9260 /* For variable refresh rate mode only:
9261 * Get vblank of last completed flip to avoid > 1 vrr
9262 * flips per video frame by use of throttling, but allow
9263 * flip programming anywhere in the possibly large
9264 * variable vrr vblank interval for fine-grained flip
9265 * timing control and more opportunity to avoid stutter
9266 * on late submission of flips.
9268 spin_lock_irqsave(&pcrtc->dev->event_lock, flags);
9269 last_flip_vblank = acrtc_attach->dm_irq_params.last_flip_vblank;
9270 spin_unlock_irqrestore(&pcrtc->dev->event_lock, flags);
9273 target_vblank = last_flip_vblank + wait_for_vblank;
9276 * Wait until we're out of the vertical blank period before the one
9277 * targeted by the flip
9279 while ((acrtc_attach->enabled &&
9280 (amdgpu_display_get_crtc_scanoutpos(dm->ddev, acrtc_attach->crtc_id,
9281 0, &vpos, &hpos, NULL,
9282 NULL, &pcrtc->hwmode)
9283 & (DRM_SCANOUTPOS_VALID | DRM_SCANOUTPOS_IN_VBLANK)) ==
9284 (DRM_SCANOUTPOS_VALID | DRM_SCANOUTPOS_IN_VBLANK) &&
9285 (int)(target_vblank -
9286 amdgpu_get_vblank_counter_kms(pcrtc)) > 0)) {
9287 usleep_range(1000, 1100);
9291 * Prepare the flip event for the pageflip interrupt to handle.
9293 * This only works in the case where we've already turned on the
9294 * appropriate hardware blocks (eg. HUBP) so in the transition case
9295 * from 0 -> n planes we have to skip a hardware generated event
9296 * and rely on sending it from software.
9298 if (acrtc_attach->base.state->event &&
9299 acrtc_state->active_planes > 0 &&
9300 !acrtc_state->force_dpms_off) {
9301 drm_crtc_vblank_get(pcrtc);
9303 spin_lock_irqsave(&pcrtc->dev->event_lock, flags);
9305 WARN_ON(acrtc_attach->pflip_status != AMDGPU_FLIP_NONE);
9306 prepare_flip_isr(acrtc_attach);
9308 spin_unlock_irqrestore(&pcrtc->dev->event_lock, flags);
9311 if (acrtc_state->stream) {
9312 if (acrtc_state->freesync_vrr_info_changed)
9313 bundle->stream_update.vrr_infopacket =
9314 &acrtc_state->stream->vrr_infopacket;
9318 /* Update the planes if changed or disable if we don't have any. */
9319 if ((planes_count || acrtc_state->active_planes == 0) &&
9320 acrtc_state->stream) {
9321 #if defined(CONFIG_DRM_AMD_DC_DCN)
9323 * If PSR or idle optimizations are enabled then flush out
9324 * any pending work before hardware programming.
9326 if (dm->vblank_control_workqueue)
9327 flush_workqueue(dm->vblank_control_workqueue);
9330 bundle->stream_update.stream = acrtc_state->stream;
9331 if (new_pcrtc_state->mode_changed) {
9332 bundle->stream_update.src = acrtc_state->stream->src;
9333 bundle->stream_update.dst = acrtc_state->stream->dst;
9336 if (new_pcrtc_state->color_mgmt_changed) {
9338 * TODO: This isn't fully correct since we've actually
9339 * already modified the stream in place.
9341 bundle->stream_update.gamut_remap =
9342 &acrtc_state->stream->gamut_remap_matrix;
9343 bundle->stream_update.output_csc_transform =
9344 &acrtc_state->stream->csc_color_matrix;
9345 bundle->stream_update.out_transfer_func =
9346 acrtc_state->stream->out_transfer_func;
9349 acrtc_state->stream->abm_level = acrtc_state->abm_level;
9350 if (acrtc_state->abm_level != dm_old_crtc_state->abm_level)
9351 bundle->stream_update.abm_level = &acrtc_state->abm_level;
9354 * If FreeSync state on the stream has changed then we need to
9355 * re-adjust the min/max bounds now that DC doesn't handle this
9356 * as part of commit.
9358 if (is_dc_timing_adjust_needed(dm_old_crtc_state, acrtc_state)) {
9359 spin_lock_irqsave(&pcrtc->dev->event_lock, flags);
9360 dc_stream_adjust_vmin_vmax(
9361 dm->dc, acrtc_state->stream,
9362 &acrtc_attach->dm_irq_params.vrr_params.adjust);
9363 spin_unlock_irqrestore(&pcrtc->dev->event_lock, flags);
9365 mutex_lock(&dm->dc_lock);
9366 if ((acrtc_state->update_type > UPDATE_TYPE_FAST) &&
9367 acrtc_state->stream->link->psr_settings.psr_allow_active)
9368 amdgpu_dm_psr_disable(acrtc_state->stream);
9370 dc_commit_updates_for_stream(dm->dc,
9371 bundle->surface_updates,
9373 acrtc_state->stream,
9374 &bundle->stream_update,
9378 * Enable or disable the interrupts on the backend.
9380 * Most pipes are put into power gating when unused.
9382 * When power gating is enabled on a pipe we lose the
9383 * interrupt enablement state when power gating is disabled.
9385 * So we need to update the IRQ control state in hardware
9386 * whenever the pipe turns on (since it could be previously
9387 * power gated) or off (since some pipes can't be power gated
9390 if (dm_old_crtc_state->active_planes != acrtc_state->active_planes)
9391 dm_update_pflip_irq_state(drm_to_adev(dev),
9394 if ((acrtc_state->update_type > UPDATE_TYPE_FAST) &&
9395 acrtc_state->stream->link->psr_settings.psr_version != DC_PSR_VERSION_UNSUPPORTED &&
9396 !acrtc_state->stream->link->psr_settings.psr_feature_enabled)
9397 amdgpu_dm_link_setup_psr(acrtc_state->stream);
9399 /* Decrement skip count when PSR is enabled and we're doing fast updates. */
9400 if (acrtc_state->update_type == UPDATE_TYPE_FAST &&
9401 acrtc_state->stream->link->psr_settings.psr_feature_enabled) {
9402 struct amdgpu_dm_connector *aconn =
9403 (struct amdgpu_dm_connector *)acrtc_state->stream->dm_stream_context;
9405 if (aconn->psr_skip_count > 0)
9406 aconn->psr_skip_count--;
9408 /* Allow PSR when skip count is 0. */
9409 acrtc_attach->dm_irq_params.allow_psr_entry = !aconn->psr_skip_count;
9411 acrtc_attach->dm_irq_params.allow_psr_entry = false;
9414 mutex_unlock(&dm->dc_lock);
9418 * Update cursor state *after* programming all the planes.
9419 * This avoids redundant programming in the case where we're going
9420 * to be disabling a single plane - those pipes are being disabled.
9422 if (acrtc_state->active_planes)
9423 amdgpu_dm_commit_cursors(state);
9429 static void amdgpu_dm_commit_audio(struct drm_device *dev,
9430 struct drm_atomic_state *state)
9432 struct amdgpu_device *adev = drm_to_adev(dev);
9433 struct amdgpu_dm_connector *aconnector;
9434 struct drm_connector *connector;
9435 struct drm_connector_state *old_con_state, *new_con_state;
9436 struct drm_crtc_state *new_crtc_state;
9437 struct dm_crtc_state *new_dm_crtc_state;
9438 const struct dc_stream_status *status;
9441 /* Notify device removals. */
9442 for_each_oldnew_connector_in_state(state, connector, old_con_state, new_con_state, i) {
9443 if (old_con_state->crtc != new_con_state->crtc) {
9444 /* CRTC changes require notification. */
9448 if (!new_con_state->crtc)
9451 new_crtc_state = drm_atomic_get_new_crtc_state(
9452 state, new_con_state->crtc);
9454 if (!new_crtc_state)
9457 if (!drm_atomic_crtc_needs_modeset(new_crtc_state))
9461 aconnector = to_amdgpu_dm_connector(connector);
9463 mutex_lock(&adev->dm.audio_lock);
9464 inst = aconnector->audio_inst;
9465 aconnector->audio_inst = -1;
9466 mutex_unlock(&adev->dm.audio_lock);
9468 amdgpu_dm_audio_eld_notify(adev, inst);
9471 /* Notify audio device additions. */
9472 for_each_new_connector_in_state(state, connector, new_con_state, i) {
9473 if (!new_con_state->crtc)
9476 new_crtc_state = drm_atomic_get_new_crtc_state(
9477 state, new_con_state->crtc);
9479 if (!new_crtc_state)
9482 if (!drm_atomic_crtc_needs_modeset(new_crtc_state))
9485 new_dm_crtc_state = to_dm_crtc_state(new_crtc_state);
9486 if (!new_dm_crtc_state->stream)
9489 status = dc_stream_get_status(new_dm_crtc_state->stream);
9493 aconnector = to_amdgpu_dm_connector(connector);
9495 mutex_lock(&adev->dm.audio_lock);
9496 inst = status->audio_inst;
9497 aconnector->audio_inst = inst;
9498 mutex_unlock(&adev->dm.audio_lock);
9500 amdgpu_dm_audio_eld_notify(adev, inst);
9505 * amdgpu_dm_crtc_copy_transient_flags - copy mirrored flags from DRM to DC
9506 * @crtc_state: the DRM CRTC state
9507 * @stream_state: the DC stream state.
9509 * Copy the mirrored transient state flags from DRM, to DC. It is used to bring
9510 * a dc_stream_state's flags in sync with a drm_crtc_state's flags.
9512 static void amdgpu_dm_crtc_copy_transient_flags(struct drm_crtc_state *crtc_state,
9513 struct dc_stream_state *stream_state)
9515 stream_state->mode_changed = drm_atomic_crtc_needs_modeset(crtc_state);
9519 * amdgpu_dm_atomic_commit_tail() - AMDgpu DM's commit tail implementation.
9520 * @state: The atomic state to commit
9522 * This will tell DC to commit the constructed DC state from atomic_check,
9523 * programming the hardware. Any failures here implies a hardware failure, since
9524 * atomic check should have filtered anything non-kosher.
9526 static void amdgpu_dm_atomic_commit_tail(struct drm_atomic_state *state)
9528 struct drm_device *dev = state->dev;
9529 struct amdgpu_device *adev = drm_to_adev(dev);
9530 struct amdgpu_display_manager *dm = &adev->dm;
9531 struct dm_atomic_state *dm_state;
9532 struct dc_state *dc_state = NULL, *dc_state_temp = NULL;
9534 struct drm_crtc *crtc;
9535 struct drm_crtc_state *old_crtc_state, *new_crtc_state;
9536 unsigned long flags;
9537 bool wait_for_vblank = true;
9538 struct drm_connector *connector;
9539 struct drm_connector_state *old_con_state, *new_con_state;
9540 struct dm_crtc_state *dm_old_crtc_state, *dm_new_crtc_state;
9541 int crtc_disable_count = 0;
9542 bool mode_set_reset_required = false;
9544 trace_amdgpu_dm_atomic_commit_tail_begin(state);
9546 drm_atomic_helper_update_legacy_modeset_state(dev, state);
9548 dm_state = dm_atomic_get_new_state(state);
9549 if (dm_state && dm_state->context) {
9550 dc_state = dm_state->context;
9552 /* No state changes, retain current state. */
9553 dc_state_temp = dc_create_state(dm->dc);
9554 ASSERT(dc_state_temp);
9555 dc_state = dc_state_temp;
9556 dc_resource_state_copy_construct_current(dm->dc, dc_state);
9559 for_each_oldnew_crtc_in_state (state, crtc, old_crtc_state,
9560 new_crtc_state, i) {
9561 struct amdgpu_crtc *acrtc = to_amdgpu_crtc(crtc);
9563 dm_old_crtc_state = to_dm_crtc_state(old_crtc_state);
9565 if (old_crtc_state->active &&
9566 (!new_crtc_state->active ||
9567 drm_atomic_crtc_needs_modeset(new_crtc_state))) {
9568 manage_dm_interrupts(adev, acrtc, false);
9569 dc_stream_release(dm_old_crtc_state->stream);
9573 drm_atomic_helper_calc_timestamping_constants(state);
9575 /* update changed items */
9576 for_each_oldnew_crtc_in_state(state, crtc, old_crtc_state, new_crtc_state, i) {
9577 struct amdgpu_crtc *acrtc = to_amdgpu_crtc(crtc);
9579 dm_new_crtc_state = to_dm_crtc_state(new_crtc_state);
9580 dm_old_crtc_state = to_dm_crtc_state(old_crtc_state);
9583 "amdgpu_crtc id:%d crtc_state_flags: enable:%d, active:%d, "
9584 "planes_changed:%d, mode_changed:%d,active_changed:%d,"
9585 "connectors_changed:%d\n",
9587 new_crtc_state->enable,
9588 new_crtc_state->active,
9589 new_crtc_state->planes_changed,
9590 new_crtc_state->mode_changed,
9591 new_crtc_state->active_changed,
9592 new_crtc_state->connectors_changed);
9594 /* Disable cursor if disabling crtc */
9595 if (old_crtc_state->active && !new_crtc_state->active) {
9596 struct dc_cursor_position position;
9598 memset(&position, 0, sizeof(position));
9599 mutex_lock(&dm->dc_lock);
9600 dc_stream_set_cursor_position(dm_old_crtc_state->stream, &position);
9601 mutex_unlock(&dm->dc_lock);
9604 /* Copy all transient state flags into dc state */
9605 if (dm_new_crtc_state->stream) {
9606 amdgpu_dm_crtc_copy_transient_flags(&dm_new_crtc_state->base,
9607 dm_new_crtc_state->stream);
9610 /* handles headless hotplug case, updating new_state and
9611 * aconnector as needed
9614 if (modeset_required(new_crtc_state, dm_new_crtc_state->stream, dm_old_crtc_state->stream)) {
9616 DRM_DEBUG_ATOMIC("Atomic commit: SET crtc id %d: [%p]\n", acrtc->crtc_id, acrtc);
9618 if (!dm_new_crtc_state->stream) {
9620 * this could happen because of issues with
9621 * userspace notifications delivery.
9622 * In this case userspace tries to set mode on
9623 * display which is disconnected in fact.
9624 * dc_sink is NULL in this case on aconnector.
9625 * We expect reset mode will come soon.
9627 * This can also happen when unplug is done
9628 * during resume sequence ended
9630 * In this case, we want to pretend we still
9631 * have a sink to keep the pipe running so that
9632 * hw state is consistent with the sw state
9634 DRM_DEBUG_DRIVER("%s: Failed to create new stream for crtc %d\n",
9635 __func__, acrtc->base.base.id);
9639 if (dm_old_crtc_state->stream)
9640 remove_stream(adev, acrtc, dm_old_crtc_state->stream);
9642 pm_runtime_get_noresume(dev->dev);
9644 acrtc->enabled = true;
9645 acrtc->hw_mode = new_crtc_state->mode;
9646 crtc->hwmode = new_crtc_state->mode;
9647 mode_set_reset_required = true;
9648 } else if (modereset_required(new_crtc_state)) {
9649 DRM_DEBUG_ATOMIC("Atomic commit: RESET. crtc id %d:[%p]\n", acrtc->crtc_id, acrtc);
9650 /* i.e. reset mode */
9651 if (dm_old_crtc_state->stream)
9652 remove_stream(adev, acrtc, dm_old_crtc_state->stream);
9654 mode_set_reset_required = true;
9656 } /* for_each_crtc_in_state() */
9659 /* if there mode set or reset, disable eDP PSR */
9660 if (mode_set_reset_required) {
9661 #if defined(CONFIG_DRM_AMD_DC_DCN)
9662 if (dm->vblank_control_workqueue)
9663 flush_workqueue(dm->vblank_control_workqueue);
9665 amdgpu_dm_psr_disable_all(dm);
9668 dm_enable_per_frame_crtc_master_sync(dc_state);
9669 mutex_lock(&dm->dc_lock);
9670 WARN_ON(!dc_commit_state(dm->dc, dc_state));
9671 #if defined(CONFIG_DRM_AMD_DC_DCN)
9672 /* Allow idle optimization when vblank count is 0 for display off */
9673 if (dm->active_vblank_irq_count == 0)
9674 dc_allow_idle_optimizations(dm->dc,true);
9676 mutex_unlock(&dm->dc_lock);
9679 for_each_new_crtc_in_state(state, crtc, new_crtc_state, i) {
9680 struct amdgpu_crtc *acrtc = to_amdgpu_crtc(crtc);
9682 dm_new_crtc_state = to_dm_crtc_state(new_crtc_state);
9684 if (dm_new_crtc_state->stream != NULL) {
9685 const struct dc_stream_status *status =
9686 dc_stream_get_status(dm_new_crtc_state->stream);
9689 status = dc_stream_get_status_from_state(dc_state,
9690 dm_new_crtc_state->stream);
9692 DC_ERR("got no status for stream %p on acrtc%p\n", dm_new_crtc_state->stream, acrtc);
9694 acrtc->otg_inst = status->primary_otg_inst;
9697 #ifdef CONFIG_DRM_AMD_DC_HDCP
9698 for_each_oldnew_connector_in_state(state, connector, old_con_state, new_con_state, i) {
9699 struct dm_connector_state *dm_new_con_state = to_dm_connector_state(new_con_state);
9700 struct amdgpu_crtc *acrtc = to_amdgpu_crtc(dm_new_con_state->base.crtc);
9701 struct amdgpu_dm_connector *aconnector = to_amdgpu_dm_connector(connector);
9703 new_crtc_state = NULL;
9706 new_crtc_state = drm_atomic_get_new_crtc_state(state, &acrtc->base);
9708 dm_new_crtc_state = to_dm_crtc_state(new_crtc_state);
9710 if (dm_new_crtc_state && dm_new_crtc_state->stream == NULL &&
9711 connector->state->content_protection == DRM_MODE_CONTENT_PROTECTION_ENABLED) {
9712 hdcp_reset_display(adev->dm.hdcp_workqueue, aconnector->dc_link->link_index);
9713 new_con_state->content_protection = DRM_MODE_CONTENT_PROTECTION_DESIRED;
9714 dm_new_con_state->update_hdcp = true;
9718 if (is_content_protection_different(new_con_state, old_con_state, connector, adev->dm.hdcp_workqueue))
9719 hdcp_update_display(
9720 adev->dm.hdcp_workqueue, aconnector->dc_link->link_index, aconnector,
9721 new_con_state->hdcp_content_type,
9722 new_con_state->content_protection == DRM_MODE_CONTENT_PROTECTION_DESIRED);
9726 /* Handle connector state changes */
9727 for_each_oldnew_connector_in_state(state, connector, old_con_state, new_con_state, i) {
9728 struct dm_connector_state *dm_new_con_state = to_dm_connector_state(new_con_state);
9729 struct dm_connector_state *dm_old_con_state = to_dm_connector_state(old_con_state);
9730 struct amdgpu_crtc *acrtc = to_amdgpu_crtc(dm_new_con_state->base.crtc);
9731 struct dc_surface_update dummy_updates[MAX_SURFACES];
9732 struct dc_stream_update stream_update;
9733 struct dc_info_packet hdr_packet;
9734 struct dc_stream_status *status = NULL;
9735 bool abm_changed, hdr_changed, scaling_changed;
9737 memset(&dummy_updates, 0, sizeof(dummy_updates));
9738 memset(&stream_update, 0, sizeof(stream_update));
9741 new_crtc_state = drm_atomic_get_new_crtc_state(state, &acrtc->base);
9742 old_crtc_state = drm_atomic_get_old_crtc_state(state, &acrtc->base);
9745 /* Skip any modesets/resets */
9746 if (!acrtc || drm_atomic_crtc_needs_modeset(new_crtc_state))
9749 dm_new_crtc_state = to_dm_crtc_state(new_crtc_state);
9750 dm_old_crtc_state = to_dm_crtc_state(old_crtc_state);
9752 scaling_changed = is_scaling_state_different(dm_new_con_state,
9755 abm_changed = dm_new_crtc_state->abm_level !=
9756 dm_old_crtc_state->abm_level;
9759 !drm_connector_atomic_hdr_metadata_equal(old_con_state, new_con_state);
9761 if (!scaling_changed && !abm_changed && !hdr_changed)
9764 stream_update.stream = dm_new_crtc_state->stream;
9765 if (scaling_changed) {
9766 update_stream_scaling_settings(&dm_new_con_state->base.crtc->mode,
9767 dm_new_con_state, dm_new_crtc_state->stream);
9769 stream_update.src = dm_new_crtc_state->stream->src;
9770 stream_update.dst = dm_new_crtc_state->stream->dst;
9774 dm_new_crtc_state->stream->abm_level = dm_new_crtc_state->abm_level;
9776 stream_update.abm_level = &dm_new_crtc_state->abm_level;
9780 fill_hdr_info_packet(new_con_state, &hdr_packet);
9781 stream_update.hdr_static_metadata = &hdr_packet;
9784 status = dc_stream_get_status(dm_new_crtc_state->stream);
9786 if (WARN_ON(!status))
9789 WARN_ON(!status->plane_count);
9792 * TODO: DC refuses to perform stream updates without a dc_surface_update.
9793 * Here we create an empty update on each plane.
9794 * To fix this, DC should permit updating only stream properties.
9796 for (j = 0; j < status->plane_count; j++)
9797 dummy_updates[j].surface = status->plane_states[0];
9800 mutex_lock(&dm->dc_lock);
9801 dc_commit_updates_for_stream(dm->dc,
9803 status->plane_count,
9804 dm_new_crtc_state->stream,
9807 mutex_unlock(&dm->dc_lock);
9810 /* Count number of newly disabled CRTCs for dropping PM refs later. */
9811 for_each_oldnew_crtc_in_state(state, crtc, old_crtc_state,
9812 new_crtc_state, i) {
9813 if (old_crtc_state->active && !new_crtc_state->active)
9814 crtc_disable_count++;
9816 dm_new_crtc_state = to_dm_crtc_state(new_crtc_state);
9817 dm_old_crtc_state = to_dm_crtc_state(old_crtc_state);
9819 /* For freesync config update on crtc state and params for irq */
9820 update_stream_irq_parameters(dm, dm_new_crtc_state);
9822 /* Handle vrr on->off / off->on transitions */
9823 amdgpu_dm_handle_vrr_transition(dm_old_crtc_state,
9828 * Enable interrupts for CRTCs that are newly enabled or went through
9829 * a modeset. It was intentionally deferred until after the front end
9830 * state was modified to wait until the OTG was on and so the IRQ
9831 * handlers didn't access stale or invalid state.
9833 for_each_oldnew_crtc_in_state(state, crtc, old_crtc_state, new_crtc_state, i) {
9834 struct amdgpu_crtc *acrtc = to_amdgpu_crtc(crtc);
9835 #ifdef CONFIG_DEBUG_FS
9836 bool configure_crc = false;
9837 enum amdgpu_dm_pipe_crc_source cur_crc_src;
9838 #if defined(CONFIG_DRM_AMD_SECURE_DISPLAY)
9839 struct crc_rd_work *crc_rd_wrk = dm->crc_rd_wrk;
9841 spin_lock_irqsave(&adev_to_drm(adev)->event_lock, flags);
9842 cur_crc_src = acrtc->dm_irq_params.crc_src;
9843 spin_unlock_irqrestore(&adev_to_drm(adev)->event_lock, flags);
9845 dm_new_crtc_state = to_dm_crtc_state(new_crtc_state);
9847 if (new_crtc_state->active &&
9848 (!old_crtc_state->active ||
9849 drm_atomic_crtc_needs_modeset(new_crtc_state))) {
9850 dc_stream_retain(dm_new_crtc_state->stream);
9851 acrtc->dm_irq_params.stream = dm_new_crtc_state->stream;
9852 manage_dm_interrupts(adev, acrtc, true);
9854 #ifdef CONFIG_DEBUG_FS
9856 * Frontend may have changed so reapply the CRC capture
9857 * settings for the stream.
9859 dm_new_crtc_state = to_dm_crtc_state(new_crtc_state);
9861 if (amdgpu_dm_is_valid_crc_source(cur_crc_src)) {
9862 configure_crc = true;
9863 #if defined(CONFIG_DRM_AMD_SECURE_DISPLAY)
9864 if (amdgpu_dm_crc_window_is_activated(crtc)) {
9865 spin_lock_irqsave(&adev_to_drm(adev)->event_lock, flags);
9866 acrtc->dm_irq_params.crc_window.update_win = true;
9867 acrtc->dm_irq_params.crc_window.skip_frame_cnt = 2;
9868 spin_lock_irq(&crc_rd_wrk->crc_rd_work_lock);
9869 crc_rd_wrk->crtc = crtc;
9870 spin_unlock_irq(&crc_rd_wrk->crc_rd_work_lock);
9871 spin_unlock_irqrestore(&adev_to_drm(adev)->event_lock, flags);
9877 if (amdgpu_dm_crtc_configure_crc_source(
9878 crtc, dm_new_crtc_state, cur_crc_src))
9879 DRM_DEBUG_DRIVER("Failed to configure crc source");
9884 for_each_new_crtc_in_state(state, crtc, new_crtc_state, j)
9885 if (new_crtc_state->async_flip)
9886 wait_for_vblank = false;
9888 /* update planes when needed per crtc*/
9889 for_each_new_crtc_in_state(state, crtc, new_crtc_state, j) {
9890 dm_new_crtc_state = to_dm_crtc_state(new_crtc_state);
9892 if (dm_new_crtc_state->stream)
9893 amdgpu_dm_commit_planes(state, dc_state, dev,
9894 dm, crtc, wait_for_vblank);
9897 /* Update audio instances for each connector. */
9898 amdgpu_dm_commit_audio(dev, state);
9900 #if defined(CONFIG_BACKLIGHT_CLASS_DEVICE) || \
9901 defined(CONFIG_BACKLIGHT_CLASS_DEVICE_MODULE)
9902 /* restore the backlight level */
9903 for (i = 0; i < dm->num_of_edps; i++) {
9904 if (dm->backlight_dev[i] &&
9905 (amdgpu_dm_backlight_get_level(dm, i) != dm->brightness[i]))
9906 amdgpu_dm_backlight_set_level(dm, i, dm->brightness[i]);
9910 * send vblank event on all events not handled in flip and
9911 * mark consumed event for drm_atomic_helper_commit_hw_done
9913 spin_lock_irqsave(&adev_to_drm(adev)->event_lock, flags);
9914 for_each_new_crtc_in_state(state, crtc, new_crtc_state, i) {
9916 if (new_crtc_state->event)
9917 drm_send_event_locked(dev, &new_crtc_state->event->base);
9919 new_crtc_state->event = NULL;
9921 spin_unlock_irqrestore(&adev_to_drm(adev)->event_lock, flags);
9923 /* Signal HW programming completion */
9924 drm_atomic_helper_commit_hw_done(state);
9926 if (wait_for_vblank)
9927 drm_atomic_helper_wait_for_flip_done(dev, state);
9929 drm_atomic_helper_cleanup_planes(dev, state);
9931 /* return the stolen vga memory back to VRAM */
9932 if (!adev->mman.keep_stolen_vga_memory)
9933 amdgpu_bo_free_kernel(&adev->mman.stolen_vga_memory, NULL, NULL);
9934 amdgpu_bo_free_kernel(&adev->mman.stolen_extended_memory, NULL, NULL);
9937 * Finally, drop a runtime PM reference for each newly disabled CRTC,
9938 * so we can put the GPU into runtime suspend if we're not driving any
9941 for (i = 0; i < crtc_disable_count; i++)
9942 pm_runtime_put_autosuspend(dev->dev);
9943 pm_runtime_mark_last_busy(dev->dev);
9946 dc_release_state(dc_state_temp);
9950 static int dm_force_atomic_commit(struct drm_connector *connector)
9953 struct drm_device *ddev = connector->dev;
9954 struct drm_atomic_state *state = drm_atomic_state_alloc(ddev);
9955 struct amdgpu_crtc *disconnected_acrtc = to_amdgpu_crtc(connector->encoder->crtc);
9956 struct drm_plane *plane = disconnected_acrtc->base.primary;
9957 struct drm_connector_state *conn_state;
9958 struct drm_crtc_state *crtc_state;
9959 struct drm_plane_state *plane_state;
9964 state->acquire_ctx = ddev->mode_config.acquire_ctx;
9966 /* Construct an atomic state to restore previous display setting */
9969 * Attach connectors to drm_atomic_state
9971 conn_state = drm_atomic_get_connector_state(state, connector);
9973 ret = PTR_ERR_OR_ZERO(conn_state);
9977 /* Attach crtc to drm_atomic_state*/
9978 crtc_state = drm_atomic_get_crtc_state(state, &disconnected_acrtc->base);
9980 ret = PTR_ERR_OR_ZERO(crtc_state);
9984 /* force a restore */
9985 crtc_state->mode_changed = true;
9987 /* Attach plane to drm_atomic_state */
9988 plane_state = drm_atomic_get_plane_state(state, plane);
9990 ret = PTR_ERR_OR_ZERO(plane_state);
9994 /* Call commit internally with the state we just constructed */
9995 ret = drm_atomic_commit(state);
9998 drm_atomic_state_put(state);
10000 DRM_ERROR("Restoring old state failed with %i\n", ret);
10006 * This function handles all cases when set mode does not come upon hotplug.
10007 * This includes when a display is unplugged then plugged back into the
10008 * same port and when running without usermode desktop manager supprot
10010 void dm_restore_drm_connector_state(struct drm_device *dev,
10011 struct drm_connector *connector)
10013 struct amdgpu_dm_connector *aconnector = to_amdgpu_dm_connector(connector);
10014 struct amdgpu_crtc *disconnected_acrtc;
10015 struct dm_crtc_state *acrtc_state;
10017 if (!aconnector->dc_sink || !connector->state || !connector->encoder)
10020 disconnected_acrtc = to_amdgpu_crtc(connector->encoder->crtc);
10021 if (!disconnected_acrtc)
10024 acrtc_state = to_dm_crtc_state(disconnected_acrtc->base.state);
10025 if (!acrtc_state->stream)
10029 * If the previous sink is not released and different from the current,
10030 * we deduce we are in a state where we can not rely on usermode call
10031 * to turn on the display, so we do it here
10033 if (acrtc_state->stream->sink != aconnector->dc_sink)
10034 dm_force_atomic_commit(&aconnector->base);
10038 * Grabs all modesetting locks to serialize against any blocking commits,
10039 * Waits for completion of all non blocking commits.
10041 static int do_aquire_global_lock(struct drm_device *dev,
10042 struct drm_atomic_state *state)
10044 struct drm_crtc *crtc;
10045 struct drm_crtc_commit *commit;
10049 * Adding all modeset locks to aquire_ctx will
10050 * ensure that when the framework release it the
10051 * extra locks we are locking here will get released to
10053 ret = drm_modeset_lock_all_ctx(dev, state->acquire_ctx);
10057 list_for_each_entry(crtc, &dev->mode_config.crtc_list, head) {
10058 spin_lock(&crtc->commit_lock);
10059 commit = list_first_entry_or_null(&crtc->commit_list,
10060 struct drm_crtc_commit, commit_entry);
10062 drm_crtc_commit_get(commit);
10063 spin_unlock(&crtc->commit_lock);
10069 * Make sure all pending HW programming completed and
10072 ret = wait_for_completion_interruptible_timeout(&commit->hw_done, 10*HZ);
10075 ret = wait_for_completion_interruptible_timeout(
10076 &commit->flip_done, 10*HZ);
10079 DRM_ERROR("[CRTC:%d:%s] hw_done or flip_done "
10080 "timed out\n", crtc->base.id, crtc->name);
10082 drm_crtc_commit_put(commit);
10085 return ret < 0 ? ret : 0;
10088 static void get_freesync_config_for_crtc(
10089 struct dm_crtc_state *new_crtc_state,
10090 struct dm_connector_state *new_con_state)
10092 struct mod_freesync_config config = {0};
10093 struct amdgpu_dm_connector *aconnector =
10094 to_amdgpu_dm_connector(new_con_state->base.connector);
10095 struct drm_display_mode *mode = &new_crtc_state->base.mode;
10096 int vrefresh = drm_mode_vrefresh(mode);
10097 bool fs_vid_mode = false;
10099 new_crtc_state->vrr_supported = new_con_state->freesync_capable &&
10100 vrefresh >= aconnector->min_vfreq &&
10101 vrefresh <= aconnector->max_vfreq;
10103 if (new_crtc_state->vrr_supported) {
10104 new_crtc_state->stream->ignore_msa_timing_param = true;
10105 fs_vid_mode = new_crtc_state->freesync_config.state == VRR_STATE_ACTIVE_FIXED;
10107 config.min_refresh_in_uhz = aconnector->min_vfreq * 1000000;
10108 config.max_refresh_in_uhz = aconnector->max_vfreq * 1000000;
10109 config.vsif_supported = true;
10113 config.state = VRR_STATE_ACTIVE_FIXED;
10114 config.fixed_refresh_in_uhz = new_crtc_state->freesync_config.fixed_refresh_in_uhz;
10116 } else if (new_crtc_state->base.vrr_enabled) {
10117 config.state = VRR_STATE_ACTIVE_VARIABLE;
10119 config.state = VRR_STATE_INACTIVE;
10123 new_crtc_state->freesync_config = config;
10126 static void reset_freesync_config_for_crtc(
10127 struct dm_crtc_state *new_crtc_state)
10129 new_crtc_state->vrr_supported = false;
10131 memset(&new_crtc_state->vrr_infopacket, 0,
10132 sizeof(new_crtc_state->vrr_infopacket));
10136 is_timing_unchanged_for_freesync(struct drm_crtc_state *old_crtc_state,
10137 struct drm_crtc_state *new_crtc_state)
10139 struct drm_display_mode old_mode, new_mode;
10141 if (!old_crtc_state || !new_crtc_state)
10144 old_mode = old_crtc_state->mode;
10145 new_mode = new_crtc_state->mode;
10147 if (old_mode.clock == new_mode.clock &&
10148 old_mode.hdisplay == new_mode.hdisplay &&
10149 old_mode.vdisplay == new_mode.vdisplay &&
10150 old_mode.htotal == new_mode.htotal &&
10151 old_mode.vtotal != new_mode.vtotal &&
10152 old_mode.hsync_start == new_mode.hsync_start &&
10153 old_mode.vsync_start != new_mode.vsync_start &&
10154 old_mode.hsync_end == new_mode.hsync_end &&
10155 old_mode.vsync_end != new_mode.vsync_end &&
10156 old_mode.hskew == new_mode.hskew &&
10157 old_mode.vscan == new_mode.vscan &&
10158 (old_mode.vsync_end - old_mode.vsync_start) ==
10159 (new_mode.vsync_end - new_mode.vsync_start))
10165 static void set_freesync_fixed_config(struct dm_crtc_state *dm_new_crtc_state) {
10166 uint64_t num, den, res;
10167 struct drm_crtc_state *new_crtc_state = &dm_new_crtc_state->base;
10169 dm_new_crtc_state->freesync_config.state = VRR_STATE_ACTIVE_FIXED;
10171 num = (unsigned long long)new_crtc_state->mode.clock * 1000 * 1000000;
10172 den = (unsigned long long)new_crtc_state->mode.htotal *
10173 (unsigned long long)new_crtc_state->mode.vtotal;
10175 res = div_u64(num, den);
10176 dm_new_crtc_state->freesync_config.fixed_refresh_in_uhz = res;
10179 static int dm_update_crtc_state(struct amdgpu_display_manager *dm,
10180 struct drm_atomic_state *state,
10181 struct drm_crtc *crtc,
10182 struct drm_crtc_state *old_crtc_state,
10183 struct drm_crtc_state *new_crtc_state,
10185 bool *lock_and_validation_needed)
10187 struct dm_atomic_state *dm_state = NULL;
10188 struct dm_crtc_state *dm_old_crtc_state, *dm_new_crtc_state;
10189 struct dc_stream_state *new_stream;
10193 * TODO Move this code into dm_crtc_atomic_check once we get rid of dc_validation_set
10194 * update changed items
10196 struct amdgpu_crtc *acrtc = NULL;
10197 struct amdgpu_dm_connector *aconnector = NULL;
10198 struct drm_connector_state *drm_new_conn_state = NULL, *drm_old_conn_state = NULL;
10199 struct dm_connector_state *dm_new_conn_state = NULL, *dm_old_conn_state = NULL;
10203 dm_old_crtc_state = to_dm_crtc_state(old_crtc_state);
10204 dm_new_crtc_state = to_dm_crtc_state(new_crtc_state);
10205 acrtc = to_amdgpu_crtc(crtc);
10206 aconnector = amdgpu_dm_find_first_crtc_matching_connector(state, crtc);
10208 /* TODO This hack should go away */
10209 if (aconnector && enable) {
10210 /* Make sure fake sink is created in plug-in scenario */
10211 drm_new_conn_state = drm_atomic_get_new_connector_state(state,
10212 &aconnector->base);
10213 drm_old_conn_state = drm_atomic_get_old_connector_state(state,
10214 &aconnector->base);
10216 if (IS_ERR(drm_new_conn_state)) {
10217 ret = PTR_ERR_OR_ZERO(drm_new_conn_state);
10221 dm_new_conn_state = to_dm_connector_state(drm_new_conn_state);
10222 dm_old_conn_state = to_dm_connector_state(drm_old_conn_state);
10224 if (!drm_atomic_crtc_needs_modeset(new_crtc_state))
10227 new_stream = create_validate_stream_for_sink(aconnector,
10228 &new_crtc_state->mode,
10230 dm_old_crtc_state->stream);
10233 * we can have no stream on ACTION_SET if a display
10234 * was disconnected during S3, in this case it is not an
10235 * error, the OS will be updated after detection, and
10236 * will do the right thing on next atomic commit
10240 DRM_DEBUG_DRIVER("%s: Failed to create new stream for crtc %d\n",
10241 __func__, acrtc->base.base.id);
10247 * TODO: Check VSDB bits to decide whether this should
10248 * be enabled or not.
10250 new_stream->triggered_crtc_reset.enabled =
10251 dm->force_timing_sync;
10253 dm_new_crtc_state->abm_level = dm_new_conn_state->abm_level;
10255 ret = fill_hdr_info_packet(drm_new_conn_state,
10256 &new_stream->hdr_static_metadata);
10261 * If we already removed the old stream from the context
10262 * (and set the new stream to NULL) then we can't reuse
10263 * the old stream even if the stream and scaling are unchanged.
10264 * We'll hit the BUG_ON and black screen.
10266 * TODO: Refactor this function to allow this check to work
10267 * in all conditions.
10269 if (dm_new_crtc_state->stream &&
10270 is_timing_unchanged_for_freesync(new_crtc_state, old_crtc_state))
10273 if (dm_new_crtc_state->stream &&
10274 dc_is_stream_unchanged(new_stream, dm_old_crtc_state->stream) &&
10275 dc_is_stream_scaling_unchanged(new_stream, dm_old_crtc_state->stream)) {
10276 new_crtc_state->mode_changed = false;
10277 DRM_DEBUG_DRIVER("Mode change not required, setting mode_changed to %d",
10278 new_crtc_state->mode_changed);
10282 /* mode_changed flag may get updated above, need to check again */
10283 if (!drm_atomic_crtc_needs_modeset(new_crtc_state))
10287 "amdgpu_crtc id:%d crtc_state_flags: enable:%d, active:%d, "
10288 "planes_changed:%d, mode_changed:%d,active_changed:%d,"
10289 "connectors_changed:%d\n",
10291 new_crtc_state->enable,
10292 new_crtc_state->active,
10293 new_crtc_state->planes_changed,
10294 new_crtc_state->mode_changed,
10295 new_crtc_state->active_changed,
10296 new_crtc_state->connectors_changed);
10298 /* Remove stream for any changed/disabled CRTC */
10301 if (!dm_old_crtc_state->stream)
10304 if (dm_new_crtc_state->stream &&
10305 is_timing_unchanged_for_freesync(new_crtc_state,
10307 new_crtc_state->mode_changed = false;
10309 "Mode change not required for front porch change, "
10310 "setting mode_changed to %d",
10311 new_crtc_state->mode_changed);
10313 set_freesync_fixed_config(dm_new_crtc_state);
10316 } else if (aconnector &&
10317 is_freesync_video_mode(&new_crtc_state->mode,
10319 struct drm_display_mode *high_mode;
10321 high_mode = get_highest_refresh_rate_mode(aconnector, false);
10322 if (!drm_mode_equal(&new_crtc_state->mode, high_mode)) {
10323 set_freesync_fixed_config(dm_new_crtc_state);
10327 ret = dm_atomic_get_state(state, &dm_state);
10331 DRM_DEBUG_DRIVER("Disabling DRM crtc: %d\n",
10334 /* i.e. reset mode */
10335 if (dc_remove_stream_from_ctx(
10338 dm_old_crtc_state->stream) != DC_OK) {
10343 dc_stream_release(dm_old_crtc_state->stream);
10344 dm_new_crtc_state->stream = NULL;
10346 reset_freesync_config_for_crtc(dm_new_crtc_state);
10348 *lock_and_validation_needed = true;
10350 } else {/* Add stream for any updated/enabled CRTC */
10352 * Quick fix to prevent NULL pointer on new_stream when
10353 * added MST connectors not found in existing crtc_state in the chained mode
10354 * TODO: need to dig out the root cause of that
10356 if (!aconnector || (!aconnector->dc_sink && aconnector->mst_port))
10359 if (modereset_required(new_crtc_state))
10362 if (modeset_required(new_crtc_state, new_stream,
10363 dm_old_crtc_state->stream)) {
10365 WARN_ON(dm_new_crtc_state->stream);
10367 ret = dm_atomic_get_state(state, &dm_state);
10371 dm_new_crtc_state->stream = new_stream;
10373 dc_stream_retain(new_stream);
10375 DRM_DEBUG_ATOMIC("Enabling DRM crtc: %d\n",
10378 if (dc_add_stream_to_ctx(
10381 dm_new_crtc_state->stream) != DC_OK) {
10386 *lock_and_validation_needed = true;
10391 /* Release extra reference */
10393 dc_stream_release(new_stream);
10396 * We want to do dc stream updates that do not require a
10397 * full modeset below.
10399 if (!(enable && aconnector && new_crtc_state->active))
10402 * Given above conditions, the dc state cannot be NULL because:
10403 * 1. We're in the process of enabling CRTCs (just been added
10404 * to the dc context, or already is on the context)
10405 * 2. Has a valid connector attached, and
10406 * 3. Is currently active and enabled.
10407 * => The dc stream state currently exists.
10409 BUG_ON(dm_new_crtc_state->stream == NULL);
10411 /* Scaling or underscan settings */
10412 if (is_scaling_state_different(dm_old_conn_state, dm_new_conn_state) ||
10413 drm_atomic_crtc_needs_modeset(new_crtc_state))
10414 update_stream_scaling_settings(
10415 &new_crtc_state->mode, dm_new_conn_state, dm_new_crtc_state->stream);
10418 dm_new_crtc_state->abm_level = dm_new_conn_state->abm_level;
10421 * Color management settings. We also update color properties
10422 * when a modeset is needed, to ensure it gets reprogrammed.
10424 if (dm_new_crtc_state->base.color_mgmt_changed ||
10425 drm_atomic_crtc_needs_modeset(new_crtc_state)) {
10426 ret = amdgpu_dm_update_crtc_color_mgmt(dm_new_crtc_state);
10431 /* Update Freesync settings. */
10432 get_freesync_config_for_crtc(dm_new_crtc_state,
10433 dm_new_conn_state);
10439 dc_stream_release(new_stream);
10443 static bool should_reset_plane(struct drm_atomic_state *state,
10444 struct drm_plane *plane,
10445 struct drm_plane_state *old_plane_state,
10446 struct drm_plane_state *new_plane_state)
10448 struct drm_plane *other;
10449 struct drm_plane_state *old_other_state, *new_other_state;
10450 struct drm_crtc_state *new_crtc_state;
10454 * TODO: Remove this hack once the checks below are sufficient
10455 * enough to determine when we need to reset all the planes on
10458 if (state->allow_modeset)
10461 /* Exit early if we know that we're adding or removing the plane. */
10462 if (old_plane_state->crtc != new_plane_state->crtc)
10465 /* old crtc == new_crtc == NULL, plane not in context. */
10466 if (!new_plane_state->crtc)
10470 drm_atomic_get_new_crtc_state(state, new_plane_state->crtc);
10472 if (!new_crtc_state)
10475 /* CRTC Degamma changes currently require us to recreate planes. */
10476 if (new_crtc_state->color_mgmt_changed)
10479 if (drm_atomic_crtc_needs_modeset(new_crtc_state))
10483 * If there are any new primary or overlay planes being added or
10484 * removed then the z-order can potentially change. To ensure
10485 * correct z-order and pipe acquisition the current DC architecture
10486 * requires us to remove and recreate all existing planes.
10488 * TODO: Come up with a more elegant solution for this.
10490 for_each_oldnew_plane_in_state(state, other, old_other_state, new_other_state, i) {
10491 struct amdgpu_framebuffer *old_afb, *new_afb;
10492 if (other->type == DRM_PLANE_TYPE_CURSOR)
10495 if (old_other_state->crtc != new_plane_state->crtc &&
10496 new_other_state->crtc != new_plane_state->crtc)
10499 if (old_other_state->crtc != new_other_state->crtc)
10502 /* Src/dst size and scaling updates. */
10503 if (old_other_state->src_w != new_other_state->src_w ||
10504 old_other_state->src_h != new_other_state->src_h ||
10505 old_other_state->crtc_w != new_other_state->crtc_w ||
10506 old_other_state->crtc_h != new_other_state->crtc_h)
10509 /* Rotation / mirroring updates. */
10510 if (old_other_state->rotation != new_other_state->rotation)
10513 /* Blending updates. */
10514 if (old_other_state->pixel_blend_mode !=
10515 new_other_state->pixel_blend_mode)
10518 /* Alpha updates. */
10519 if (old_other_state->alpha != new_other_state->alpha)
10522 /* Colorspace changes. */
10523 if (old_other_state->color_range != new_other_state->color_range ||
10524 old_other_state->color_encoding != new_other_state->color_encoding)
10527 /* Framebuffer checks fall at the end. */
10528 if (!old_other_state->fb || !new_other_state->fb)
10531 /* Pixel format changes can require bandwidth updates. */
10532 if (old_other_state->fb->format != new_other_state->fb->format)
10535 old_afb = (struct amdgpu_framebuffer *)old_other_state->fb;
10536 new_afb = (struct amdgpu_framebuffer *)new_other_state->fb;
10538 /* Tiling and DCC changes also require bandwidth updates. */
10539 if (old_afb->tiling_flags != new_afb->tiling_flags ||
10540 old_afb->base.modifier != new_afb->base.modifier)
10547 static int dm_check_cursor_fb(struct amdgpu_crtc *new_acrtc,
10548 struct drm_plane_state *new_plane_state,
10549 struct drm_framebuffer *fb)
10551 struct amdgpu_device *adev = drm_to_adev(new_acrtc->base.dev);
10552 struct amdgpu_framebuffer *afb = to_amdgpu_framebuffer(fb);
10553 unsigned int pitch;
10556 if (fb->width > new_acrtc->max_cursor_width ||
10557 fb->height > new_acrtc->max_cursor_height) {
10558 DRM_DEBUG_ATOMIC("Bad cursor FB size %dx%d\n",
10559 new_plane_state->fb->width,
10560 new_plane_state->fb->height);
10563 if (new_plane_state->src_w != fb->width << 16 ||
10564 new_plane_state->src_h != fb->height << 16) {
10565 DRM_DEBUG_ATOMIC("Cropping not supported for cursor plane\n");
10569 /* Pitch in pixels */
10570 pitch = fb->pitches[0] / fb->format->cpp[0];
10572 if (fb->width != pitch) {
10573 DRM_DEBUG_ATOMIC("Cursor FB width %d doesn't match pitch %d",
10582 /* FB pitch is supported by cursor plane */
10585 DRM_DEBUG_ATOMIC("Bad cursor FB pitch %d px\n", pitch);
10589 /* Core DRM takes care of checking FB modifiers, so we only need to
10590 * check tiling flags when the FB doesn't have a modifier. */
10591 if (!(fb->flags & DRM_MODE_FB_MODIFIERS)) {
10592 if (adev->family < AMDGPU_FAMILY_AI) {
10593 linear = AMDGPU_TILING_GET(afb->tiling_flags, ARRAY_MODE) != DC_ARRAY_2D_TILED_THIN1 &&
10594 AMDGPU_TILING_GET(afb->tiling_flags, ARRAY_MODE) != DC_ARRAY_1D_TILED_THIN1 &&
10595 AMDGPU_TILING_GET(afb->tiling_flags, MICRO_TILE_MODE) == 0;
10597 linear = AMDGPU_TILING_GET(afb->tiling_flags, SWIZZLE_MODE) == 0;
10600 DRM_DEBUG_ATOMIC("Cursor FB not linear");
10608 static int dm_update_plane_state(struct dc *dc,
10609 struct drm_atomic_state *state,
10610 struct drm_plane *plane,
10611 struct drm_plane_state *old_plane_state,
10612 struct drm_plane_state *new_plane_state,
10614 bool *lock_and_validation_needed)
10617 struct dm_atomic_state *dm_state = NULL;
10618 struct drm_crtc *new_plane_crtc, *old_plane_crtc;
10619 struct drm_crtc_state *old_crtc_state, *new_crtc_state;
10620 struct dm_crtc_state *dm_new_crtc_state, *dm_old_crtc_state;
10621 struct dm_plane_state *dm_new_plane_state, *dm_old_plane_state;
10622 struct amdgpu_crtc *new_acrtc;
10627 new_plane_crtc = new_plane_state->crtc;
10628 old_plane_crtc = old_plane_state->crtc;
10629 dm_new_plane_state = to_dm_plane_state(new_plane_state);
10630 dm_old_plane_state = to_dm_plane_state(old_plane_state);
10632 if (plane->type == DRM_PLANE_TYPE_CURSOR) {
10633 if (!enable || !new_plane_crtc ||
10634 drm_atomic_plane_disabling(plane->state, new_plane_state))
10637 new_acrtc = to_amdgpu_crtc(new_plane_crtc);
10639 if (new_plane_state->src_x != 0 || new_plane_state->src_y != 0) {
10640 DRM_DEBUG_ATOMIC("Cropping not supported for cursor plane\n");
10644 if (new_plane_state->fb) {
10645 ret = dm_check_cursor_fb(new_acrtc, new_plane_state,
10646 new_plane_state->fb);
10654 needs_reset = should_reset_plane(state, plane, old_plane_state,
10657 /* Remove any changed/removed planes */
10662 if (!old_plane_crtc)
10665 old_crtc_state = drm_atomic_get_old_crtc_state(
10666 state, old_plane_crtc);
10667 dm_old_crtc_state = to_dm_crtc_state(old_crtc_state);
10669 if (!dm_old_crtc_state->stream)
10672 DRM_DEBUG_ATOMIC("Disabling DRM plane: %d on DRM crtc %d\n",
10673 plane->base.id, old_plane_crtc->base.id);
10675 ret = dm_atomic_get_state(state, &dm_state);
10679 if (!dc_remove_plane_from_context(
10681 dm_old_crtc_state->stream,
10682 dm_old_plane_state->dc_state,
10683 dm_state->context)) {
10689 dc_plane_state_release(dm_old_plane_state->dc_state);
10690 dm_new_plane_state->dc_state = NULL;
10692 *lock_and_validation_needed = true;
10694 } else { /* Add new planes */
10695 struct dc_plane_state *dc_new_plane_state;
10697 if (drm_atomic_plane_disabling(plane->state, new_plane_state))
10700 if (!new_plane_crtc)
10703 new_crtc_state = drm_atomic_get_new_crtc_state(state, new_plane_crtc);
10704 dm_new_crtc_state = to_dm_crtc_state(new_crtc_state);
10706 if (!dm_new_crtc_state->stream)
10712 ret = dm_plane_helper_check_state(new_plane_state, new_crtc_state);
10716 WARN_ON(dm_new_plane_state->dc_state);
10718 dc_new_plane_state = dc_create_plane_state(dc);
10719 if (!dc_new_plane_state)
10722 DRM_DEBUG_ATOMIC("Enabling DRM plane: %d on DRM crtc %d\n",
10723 plane->base.id, new_plane_crtc->base.id);
10725 ret = fill_dc_plane_attributes(
10726 drm_to_adev(new_plane_crtc->dev),
10727 dc_new_plane_state,
10731 dc_plane_state_release(dc_new_plane_state);
10735 ret = dm_atomic_get_state(state, &dm_state);
10737 dc_plane_state_release(dc_new_plane_state);
10742 * Any atomic check errors that occur after this will
10743 * not need a release. The plane state will be attached
10744 * to the stream, and therefore part of the atomic
10745 * state. It'll be released when the atomic state is
10748 if (!dc_add_plane_to_context(
10750 dm_new_crtc_state->stream,
10751 dc_new_plane_state,
10752 dm_state->context)) {
10754 dc_plane_state_release(dc_new_plane_state);
10758 dm_new_plane_state->dc_state = dc_new_plane_state;
10760 dm_new_crtc_state->mpo_requested |= (plane->type == DRM_PLANE_TYPE_OVERLAY);
10762 /* Tell DC to do a full surface update every time there
10763 * is a plane change. Inefficient, but works for now.
10765 dm_new_plane_state->dc_state->update_flags.bits.full_update = 1;
10767 *lock_and_validation_needed = true;
10774 static void dm_get_oriented_plane_size(struct drm_plane_state *plane_state,
10775 int *src_w, int *src_h)
10777 switch (plane_state->rotation & DRM_MODE_ROTATE_MASK) {
10778 case DRM_MODE_ROTATE_90:
10779 case DRM_MODE_ROTATE_270:
10780 *src_w = plane_state->src_h >> 16;
10781 *src_h = plane_state->src_w >> 16;
10783 case DRM_MODE_ROTATE_0:
10784 case DRM_MODE_ROTATE_180:
10786 *src_w = plane_state->src_w >> 16;
10787 *src_h = plane_state->src_h >> 16;
10792 static int dm_check_crtc_cursor(struct drm_atomic_state *state,
10793 struct drm_crtc *crtc,
10794 struct drm_crtc_state *new_crtc_state)
10796 struct drm_plane *cursor = crtc->cursor, *underlying;
10797 struct drm_plane_state *new_cursor_state, *new_underlying_state;
10799 int cursor_scale_w, cursor_scale_h, underlying_scale_w, underlying_scale_h;
10800 int cursor_src_w, cursor_src_h;
10801 int underlying_src_w, underlying_src_h;
10803 /* On DCE and DCN there is no dedicated hardware cursor plane. We get a
10804 * cursor per pipe but it's going to inherit the scaling and
10805 * positioning from the underlying pipe. Check the cursor plane's
10806 * blending properties match the underlying planes'. */
10808 new_cursor_state = drm_atomic_get_new_plane_state(state, cursor);
10809 if (!new_cursor_state || !new_cursor_state->fb) {
10813 dm_get_oriented_plane_size(new_cursor_state, &cursor_src_w, &cursor_src_h);
10814 cursor_scale_w = new_cursor_state->crtc_w * 1000 / cursor_src_w;
10815 cursor_scale_h = new_cursor_state->crtc_h * 1000 / cursor_src_h;
10817 for_each_new_plane_in_state_reverse(state, underlying, new_underlying_state, i) {
10818 /* Narrow down to non-cursor planes on the same CRTC as the cursor */
10819 if (new_underlying_state->crtc != crtc || underlying == crtc->cursor)
10822 /* Ignore disabled planes */
10823 if (!new_underlying_state->fb)
10826 dm_get_oriented_plane_size(new_underlying_state,
10827 &underlying_src_w, &underlying_src_h);
10828 underlying_scale_w = new_underlying_state->crtc_w * 1000 / underlying_src_w;
10829 underlying_scale_h = new_underlying_state->crtc_h * 1000 / underlying_src_h;
10831 if (cursor_scale_w != underlying_scale_w ||
10832 cursor_scale_h != underlying_scale_h) {
10833 drm_dbg_atomic(crtc->dev,
10834 "Cursor [PLANE:%d:%s] scaling doesn't match underlying [PLANE:%d:%s]\n",
10835 cursor->base.id, cursor->name, underlying->base.id, underlying->name);
10839 /* If this plane covers the whole CRTC, no need to check planes underneath */
10840 if (new_underlying_state->crtc_x <= 0 &&
10841 new_underlying_state->crtc_y <= 0 &&
10842 new_underlying_state->crtc_x + new_underlying_state->crtc_w >= new_crtc_state->mode.hdisplay &&
10843 new_underlying_state->crtc_y + new_underlying_state->crtc_h >= new_crtc_state->mode.vdisplay)
10850 #if defined(CONFIG_DRM_AMD_DC_DCN)
10851 static int add_affected_mst_dsc_crtcs(struct drm_atomic_state *state, struct drm_crtc *crtc)
10853 struct drm_connector *connector;
10854 struct drm_connector_state *conn_state;
10855 struct amdgpu_dm_connector *aconnector = NULL;
10857 for_each_new_connector_in_state(state, connector, conn_state, i) {
10858 if (conn_state->crtc != crtc)
10861 aconnector = to_amdgpu_dm_connector(connector);
10862 if (!aconnector->port || !aconnector->mst_port)
10871 return drm_dp_mst_add_affected_dsc_crtcs(state, &aconnector->mst_port->mst_mgr);
10876 * amdgpu_dm_atomic_check() - Atomic check implementation for AMDgpu DM.
10877 * @dev: The DRM device
10878 * @state: The atomic state to commit
10880 * Validate that the given atomic state is programmable by DC into hardware.
10881 * This involves constructing a &struct dc_state reflecting the new hardware
10882 * state we wish to commit, then querying DC to see if it is programmable. It's
10883 * important not to modify the existing DC state. Otherwise, atomic_check
10884 * may unexpectedly commit hardware changes.
10886 * When validating the DC state, it's important that the right locks are
10887 * acquired. For full updates case which removes/adds/updates streams on one
10888 * CRTC while flipping on another CRTC, acquiring global lock will guarantee
10889 * that any such full update commit will wait for completion of any outstanding
10890 * flip using DRMs synchronization events.
10892 * Note that DM adds the affected connectors for all CRTCs in state, when that
10893 * might not seem necessary. This is because DC stream creation requires the
10894 * DC sink, which is tied to the DRM connector state. Cleaning this up should
10895 * be possible but non-trivial - a possible TODO item.
10897 * Return: -Error code if validation failed.
10899 static int amdgpu_dm_atomic_check(struct drm_device *dev,
10900 struct drm_atomic_state *state)
10902 struct amdgpu_device *adev = drm_to_adev(dev);
10903 struct dm_atomic_state *dm_state = NULL;
10904 struct dc *dc = adev->dm.dc;
10905 struct drm_connector *connector;
10906 struct drm_connector_state *old_con_state, *new_con_state;
10907 struct drm_crtc *crtc;
10908 struct drm_crtc_state *old_crtc_state, *new_crtc_state;
10909 struct drm_plane *plane;
10910 struct drm_plane_state *old_plane_state, *new_plane_state;
10911 enum dc_status status;
10913 bool lock_and_validation_needed = false;
10914 struct dm_crtc_state *dm_old_crtc_state, *dm_new_crtc_state;
10915 #if defined(CONFIG_DRM_AMD_DC_DCN)
10916 struct dsc_mst_fairness_vars vars[MAX_PIPES];
10917 struct drm_dp_mst_topology_state *mst_state;
10918 struct drm_dp_mst_topology_mgr *mgr;
10921 trace_amdgpu_dm_atomic_check_begin(state);
10923 ret = drm_atomic_helper_check_modeset(dev, state);
10925 DRM_DEBUG_DRIVER("drm_atomic_helper_check_modeset() failed\n");
10929 /* Check connector changes */
10930 for_each_oldnew_connector_in_state(state, connector, old_con_state, new_con_state, i) {
10931 struct dm_connector_state *dm_old_con_state = to_dm_connector_state(old_con_state);
10932 struct dm_connector_state *dm_new_con_state = to_dm_connector_state(new_con_state);
10934 /* Skip connectors that are disabled or part of modeset already. */
10935 if (!old_con_state->crtc && !new_con_state->crtc)
10938 if (!new_con_state->crtc)
10941 new_crtc_state = drm_atomic_get_crtc_state(state, new_con_state->crtc);
10942 if (IS_ERR(new_crtc_state)) {
10943 DRM_DEBUG_DRIVER("drm_atomic_get_crtc_state() failed\n");
10944 ret = PTR_ERR(new_crtc_state);
10948 if (dm_old_con_state->abm_level !=
10949 dm_new_con_state->abm_level)
10950 new_crtc_state->connectors_changed = true;
10953 #if defined(CONFIG_DRM_AMD_DC_DCN)
10954 if (dc_resource_is_dsc_encoding_supported(dc)) {
10955 for_each_oldnew_crtc_in_state(state, crtc, old_crtc_state, new_crtc_state, i) {
10956 if (drm_atomic_crtc_needs_modeset(new_crtc_state)) {
10957 ret = add_affected_mst_dsc_crtcs(state, crtc);
10959 DRM_DEBUG_DRIVER("add_affected_mst_dsc_crtcs() failed\n");
10966 for_each_oldnew_crtc_in_state(state, crtc, old_crtc_state, new_crtc_state, i) {
10967 dm_old_crtc_state = to_dm_crtc_state(old_crtc_state);
10969 if (!drm_atomic_crtc_needs_modeset(new_crtc_state) &&
10970 !new_crtc_state->color_mgmt_changed &&
10971 old_crtc_state->vrr_enabled == new_crtc_state->vrr_enabled &&
10972 dm_old_crtc_state->dsc_force_changed == false)
10975 ret = amdgpu_dm_verify_lut_sizes(new_crtc_state);
10977 DRM_DEBUG_DRIVER("amdgpu_dm_verify_lut_sizes() failed\n");
10981 if (!new_crtc_state->enable)
10984 ret = drm_atomic_add_affected_connectors(state, crtc);
10986 DRM_DEBUG_DRIVER("drm_atomic_add_affected_connectors() failed\n");
10990 ret = drm_atomic_add_affected_planes(state, crtc);
10992 DRM_DEBUG_DRIVER("drm_atomic_add_affected_planes() failed\n");
10996 if (dm_old_crtc_state->dsc_force_changed)
10997 new_crtc_state->mode_changed = true;
11001 * Add all primary and overlay planes on the CRTC to the state
11002 * whenever a plane is enabled to maintain correct z-ordering
11003 * and to enable fast surface updates.
11005 drm_for_each_crtc(crtc, dev) {
11006 bool modified = false;
11008 for_each_oldnew_plane_in_state(state, plane, old_plane_state, new_plane_state, i) {
11009 if (plane->type == DRM_PLANE_TYPE_CURSOR)
11012 if (new_plane_state->crtc == crtc ||
11013 old_plane_state->crtc == crtc) {
11022 drm_for_each_plane_mask(plane, state->dev, crtc->state->plane_mask) {
11023 if (plane->type == DRM_PLANE_TYPE_CURSOR)
11027 drm_atomic_get_plane_state(state, plane);
11029 if (IS_ERR(new_plane_state)) {
11030 ret = PTR_ERR(new_plane_state);
11031 DRM_DEBUG_DRIVER("new_plane_state is BAD\n");
11037 /* Remove exiting planes if they are modified */
11038 for_each_oldnew_plane_in_state_reverse(state, plane, old_plane_state, new_plane_state, i) {
11039 ret = dm_update_plane_state(dc, state, plane,
11043 &lock_and_validation_needed);
11045 DRM_DEBUG_DRIVER("dm_update_plane_state() failed\n");
11050 /* Disable all crtcs which require disable */
11051 for_each_oldnew_crtc_in_state(state, crtc, old_crtc_state, new_crtc_state, i) {
11052 ret = dm_update_crtc_state(&adev->dm, state, crtc,
11056 &lock_and_validation_needed);
11058 DRM_DEBUG_DRIVER("DISABLE: dm_update_crtc_state() failed\n");
11063 /* Enable all crtcs which require enable */
11064 for_each_oldnew_crtc_in_state(state, crtc, old_crtc_state, new_crtc_state, i) {
11065 ret = dm_update_crtc_state(&adev->dm, state, crtc,
11069 &lock_and_validation_needed);
11071 DRM_DEBUG_DRIVER("ENABLE: dm_update_crtc_state() failed\n");
11076 /* Add new/modified planes */
11077 for_each_oldnew_plane_in_state_reverse(state, plane, old_plane_state, new_plane_state, i) {
11078 ret = dm_update_plane_state(dc, state, plane,
11082 &lock_and_validation_needed);
11084 DRM_DEBUG_DRIVER("dm_update_plane_state() failed\n");
11089 /* Run this here since we want to validate the streams we created */
11090 ret = drm_atomic_helper_check_planes(dev, state);
11092 DRM_DEBUG_DRIVER("drm_atomic_helper_check_planes() failed\n");
11096 for_each_new_crtc_in_state(state, crtc, new_crtc_state, i) {
11097 dm_new_crtc_state = to_dm_crtc_state(new_crtc_state);
11098 if (dm_new_crtc_state->mpo_requested)
11099 DRM_DEBUG_DRIVER("MPO enablement requested on crtc:[%p]\n", crtc);
11102 /* Check cursor planes scaling */
11103 for_each_new_crtc_in_state(state, crtc, new_crtc_state, i) {
11104 ret = dm_check_crtc_cursor(state, crtc, new_crtc_state);
11106 DRM_DEBUG_DRIVER("dm_check_crtc_cursor() failed\n");
11111 if (state->legacy_cursor_update) {
11113 * This is a fast cursor update coming from the plane update
11114 * helper, check if it can be done asynchronously for better
11117 state->async_update =
11118 !drm_atomic_helper_async_check(dev, state);
11121 * Skip the remaining global validation if this is an async
11122 * update. Cursor updates can be done without affecting
11123 * state or bandwidth calcs and this avoids the performance
11124 * penalty of locking the private state object and
11125 * allocating a new dc_state.
11127 if (state->async_update)
11131 /* Check scaling and underscan changes*/
11132 /* TODO Removed scaling changes validation due to inability to commit
11133 * new stream into context w\o causing full reset. Need to
11134 * decide how to handle.
11136 for_each_oldnew_connector_in_state(state, connector, old_con_state, new_con_state, i) {
11137 struct dm_connector_state *dm_old_con_state = to_dm_connector_state(old_con_state);
11138 struct dm_connector_state *dm_new_con_state = to_dm_connector_state(new_con_state);
11139 struct amdgpu_crtc *acrtc = to_amdgpu_crtc(dm_new_con_state->base.crtc);
11141 /* Skip any modesets/resets */
11142 if (!acrtc || drm_atomic_crtc_needs_modeset(
11143 drm_atomic_get_new_crtc_state(state, &acrtc->base)))
11146 /* Skip any thing not scale or underscan changes */
11147 if (!is_scaling_state_different(dm_new_con_state, dm_old_con_state))
11150 lock_and_validation_needed = true;
11153 #if defined(CONFIG_DRM_AMD_DC_DCN)
11154 /* set the slot info for each mst_state based on the link encoding format */
11155 for_each_new_mst_mgr_in_state(state, mgr, mst_state, i) {
11156 struct amdgpu_dm_connector *aconnector;
11157 struct drm_connector *connector;
11158 struct drm_connector_list_iter iter;
11159 u8 link_coding_cap;
11161 if (!mgr->mst_state )
11164 drm_connector_list_iter_begin(dev, &iter);
11165 drm_for_each_connector_iter(connector, &iter) {
11166 int id = connector->index;
11168 if (id == mst_state->mgr->conn_base_id) {
11169 aconnector = to_amdgpu_dm_connector(connector);
11170 link_coding_cap = dc_link_dp_mst_decide_link_encoding_format(aconnector->dc_link);
11171 drm_dp_mst_update_slots(mst_state, link_coding_cap);
11176 drm_connector_list_iter_end(&iter);
11181 * Streams and planes are reset when there are changes that affect
11182 * bandwidth. Anything that affects bandwidth needs to go through
11183 * DC global validation to ensure that the configuration can be applied
11186 * We have to currently stall out here in atomic_check for outstanding
11187 * commits to finish in this case because our IRQ handlers reference
11188 * DRM state directly - we can end up disabling interrupts too early
11191 * TODO: Remove this stall and drop DM state private objects.
11193 if (lock_and_validation_needed) {
11194 ret = dm_atomic_get_state(state, &dm_state);
11196 DRM_DEBUG_DRIVER("dm_atomic_get_state() failed\n");
11200 ret = do_aquire_global_lock(dev, state);
11202 DRM_DEBUG_DRIVER("do_aquire_global_lock() failed\n");
11206 #if defined(CONFIG_DRM_AMD_DC_DCN)
11207 if (!compute_mst_dsc_configs_for_state(state, dm_state->context, vars)) {
11208 DRM_DEBUG_DRIVER("compute_mst_dsc_configs_for_state() failed\n");
11212 ret = dm_update_mst_vcpi_slots_for_dsc(state, dm_state->context, vars);
11214 DRM_DEBUG_DRIVER("dm_update_mst_vcpi_slots_for_dsc() failed\n");
11220 * Perform validation of MST topology in the state:
11221 * We need to perform MST atomic check before calling
11222 * dc_validate_global_state(), or there is a chance
11223 * to get stuck in an infinite loop and hang eventually.
11225 ret = drm_dp_mst_atomic_check(state);
11227 DRM_DEBUG_DRIVER("drm_dp_mst_atomic_check() failed\n");
11230 status = dc_validate_global_state(dc, dm_state->context, true);
11231 if (status != DC_OK) {
11232 DRM_DEBUG_DRIVER("DC global validation failure: %s (%d)",
11233 dc_status_to_str(status), status);
11239 * The commit is a fast update. Fast updates shouldn't change
11240 * the DC context, affect global validation, and can have their
11241 * commit work done in parallel with other commits not touching
11242 * the same resource. If we have a new DC context as part of
11243 * the DM atomic state from validation we need to free it and
11244 * retain the existing one instead.
11246 * Furthermore, since the DM atomic state only contains the DC
11247 * context and can safely be annulled, we can free the state
11248 * and clear the associated private object now to free
11249 * some memory and avoid a possible use-after-free later.
11252 for (i = 0; i < state->num_private_objs; i++) {
11253 struct drm_private_obj *obj = state->private_objs[i].ptr;
11255 if (obj->funcs == adev->dm.atomic_obj.funcs) {
11256 int j = state->num_private_objs-1;
11258 dm_atomic_destroy_state(obj,
11259 state->private_objs[i].state);
11261 /* If i is not at the end of the array then the
11262 * last element needs to be moved to where i was
11263 * before the array can safely be truncated.
11266 state->private_objs[i] =
11267 state->private_objs[j];
11269 state->private_objs[j].ptr = NULL;
11270 state->private_objs[j].state = NULL;
11271 state->private_objs[j].old_state = NULL;
11272 state->private_objs[j].new_state = NULL;
11274 state->num_private_objs = j;
11280 /* Store the overall update type for use later in atomic check. */
11281 for_each_new_crtc_in_state (state, crtc, new_crtc_state, i) {
11282 struct dm_crtc_state *dm_new_crtc_state =
11283 to_dm_crtc_state(new_crtc_state);
11285 dm_new_crtc_state->update_type = lock_and_validation_needed ?
11290 /* Must be success */
11293 trace_amdgpu_dm_atomic_check_finish(state, ret);
11298 if (ret == -EDEADLK)
11299 DRM_DEBUG_DRIVER("Atomic check stopped to avoid deadlock.\n");
11300 else if (ret == -EINTR || ret == -EAGAIN || ret == -ERESTARTSYS)
11301 DRM_DEBUG_DRIVER("Atomic check stopped due to signal.\n");
11303 DRM_DEBUG_DRIVER("Atomic check failed with err: %d \n", ret);
11305 trace_amdgpu_dm_atomic_check_finish(state, ret);
11310 static bool is_dp_capable_without_timing_msa(struct dc *dc,
11311 struct amdgpu_dm_connector *amdgpu_dm_connector)
11314 bool capable = false;
11316 if (amdgpu_dm_connector->dc_link &&
11317 dm_helpers_dp_read_dpcd(
11319 amdgpu_dm_connector->dc_link,
11320 DP_DOWN_STREAM_PORT_COUNT,
11322 sizeof(dpcd_data))) {
11323 capable = (dpcd_data & DP_MSA_TIMING_PAR_IGNORED) ? true:false;
11329 static bool dm_edid_parser_send_cea(struct amdgpu_display_manager *dm,
11330 unsigned int offset,
11331 unsigned int total_length,
11333 unsigned int length,
11334 struct amdgpu_hdmi_vsdb_info *vsdb)
11337 union dmub_rb_cmd cmd;
11338 struct dmub_cmd_send_edid_cea *input;
11339 struct dmub_cmd_edid_cea_output *output;
11341 if (length > DMUB_EDID_CEA_DATA_CHUNK_BYTES)
11344 memset(&cmd, 0, sizeof(cmd));
11346 input = &cmd.edid_cea.data.input;
11348 cmd.edid_cea.header.type = DMUB_CMD__EDID_CEA;
11349 cmd.edid_cea.header.sub_type = 0;
11350 cmd.edid_cea.header.payload_bytes =
11351 sizeof(cmd.edid_cea) - sizeof(cmd.edid_cea.header);
11352 input->offset = offset;
11353 input->length = length;
11354 input->cea_total_length = total_length;
11355 memcpy(input->payload, data, length);
11357 res = dc_dmub_srv_cmd_with_reply_data(dm->dc->ctx->dmub_srv, &cmd);
11359 DRM_ERROR("EDID CEA parser failed\n");
11363 output = &cmd.edid_cea.data.output;
11365 if (output->type == DMUB_CMD__EDID_CEA_ACK) {
11366 if (!output->ack.success) {
11367 DRM_ERROR("EDID CEA ack failed at offset %d\n",
11368 output->ack.offset);
11370 } else if (output->type == DMUB_CMD__EDID_CEA_AMD_VSDB) {
11371 if (!output->amd_vsdb.vsdb_found)
11374 vsdb->freesync_supported = output->amd_vsdb.freesync_supported;
11375 vsdb->amd_vsdb_version = output->amd_vsdb.amd_vsdb_version;
11376 vsdb->min_refresh_rate_hz = output->amd_vsdb.min_frame_rate;
11377 vsdb->max_refresh_rate_hz = output->amd_vsdb.max_frame_rate;
11379 DRM_WARN("Unknown EDID CEA parser results\n");
11386 static bool parse_edid_cea_dmcu(struct amdgpu_display_manager *dm,
11387 uint8_t *edid_ext, int len,
11388 struct amdgpu_hdmi_vsdb_info *vsdb_info)
11392 /* send extension block to DMCU for parsing */
11393 for (i = 0; i < len; i += 8) {
11397 /* send 8 bytes a time */
11398 if (!dc_edid_parser_send_cea(dm->dc, i, len, &edid_ext[i], 8))
11402 /* EDID block sent completed, expect result */
11403 int version, min_rate, max_rate;
11405 res = dc_edid_parser_recv_amd_vsdb(dm->dc, &version, &min_rate, &max_rate);
11407 /* amd vsdb found */
11408 vsdb_info->freesync_supported = 1;
11409 vsdb_info->amd_vsdb_version = version;
11410 vsdb_info->min_refresh_rate_hz = min_rate;
11411 vsdb_info->max_refresh_rate_hz = max_rate;
11419 res = dc_edid_parser_recv_cea_ack(dm->dc, &offset);
11427 static bool parse_edid_cea_dmub(struct amdgpu_display_manager *dm,
11428 uint8_t *edid_ext, int len,
11429 struct amdgpu_hdmi_vsdb_info *vsdb_info)
11433 /* send extension block to DMCU for parsing */
11434 for (i = 0; i < len; i += 8) {
11435 /* send 8 bytes a time */
11436 if (!dm_edid_parser_send_cea(dm, i, len, &edid_ext[i], 8, vsdb_info))
11440 return vsdb_info->freesync_supported;
11443 static bool parse_edid_cea(struct amdgpu_dm_connector *aconnector,
11444 uint8_t *edid_ext, int len,
11445 struct amdgpu_hdmi_vsdb_info *vsdb_info)
11447 struct amdgpu_device *adev = drm_to_adev(aconnector->base.dev);
11449 if (adev->dm.dmub_srv)
11450 return parse_edid_cea_dmub(&adev->dm, edid_ext, len, vsdb_info);
11452 return parse_edid_cea_dmcu(&adev->dm, edid_ext, len, vsdb_info);
11455 static int parse_hdmi_amd_vsdb(struct amdgpu_dm_connector *aconnector,
11456 struct edid *edid, struct amdgpu_hdmi_vsdb_info *vsdb_info)
11458 uint8_t *edid_ext = NULL;
11460 bool valid_vsdb_found = false;
11462 /*----- drm_find_cea_extension() -----*/
11463 /* No EDID or EDID extensions */
11464 if (edid == NULL || edid->extensions == 0)
11467 /* Find CEA extension */
11468 for (i = 0; i < edid->extensions; i++) {
11469 edid_ext = (uint8_t *)edid + EDID_LENGTH * (i + 1);
11470 if (edid_ext[0] == CEA_EXT)
11474 if (i == edid->extensions)
11477 /*----- cea_db_offsets() -----*/
11478 if (edid_ext[0] != CEA_EXT)
11481 valid_vsdb_found = parse_edid_cea(aconnector, edid_ext, EDID_LENGTH, vsdb_info);
11483 return valid_vsdb_found ? i : -ENODEV;
11486 void amdgpu_dm_update_freesync_caps(struct drm_connector *connector,
11490 struct detailed_timing *timing;
11491 struct detailed_non_pixel *data;
11492 struct detailed_data_monitor_range *range;
11493 struct amdgpu_dm_connector *amdgpu_dm_connector =
11494 to_amdgpu_dm_connector(connector);
11495 struct dm_connector_state *dm_con_state = NULL;
11496 struct dc_sink *sink;
11498 struct drm_device *dev = connector->dev;
11499 struct amdgpu_device *adev = drm_to_adev(dev);
11500 bool freesync_capable = false;
11501 struct amdgpu_hdmi_vsdb_info vsdb_info = {0};
11503 if (!connector->state) {
11504 DRM_ERROR("%s - Connector has no state", __func__);
11508 sink = amdgpu_dm_connector->dc_sink ?
11509 amdgpu_dm_connector->dc_sink :
11510 amdgpu_dm_connector->dc_em_sink;
11512 if (!edid || !sink) {
11513 dm_con_state = to_dm_connector_state(connector->state);
11515 amdgpu_dm_connector->min_vfreq = 0;
11516 amdgpu_dm_connector->max_vfreq = 0;
11517 amdgpu_dm_connector->pixel_clock_mhz = 0;
11518 connector->display_info.monitor_range.min_vfreq = 0;
11519 connector->display_info.monitor_range.max_vfreq = 0;
11520 freesync_capable = false;
11525 dm_con_state = to_dm_connector_state(connector->state);
11527 if (!adev->dm.freesync_module)
11531 if (sink->sink_signal == SIGNAL_TYPE_DISPLAY_PORT
11532 || sink->sink_signal == SIGNAL_TYPE_EDP) {
11533 bool edid_check_required = false;
11536 edid_check_required = is_dp_capable_without_timing_msa(
11538 amdgpu_dm_connector);
11541 if (edid_check_required == true && (edid->version > 1 ||
11542 (edid->version == 1 && edid->revision > 1))) {
11543 for (i = 0; i < 4; i++) {
11545 timing = &edid->detailed_timings[i];
11546 data = &timing->data.other_data;
11547 range = &data->data.range;
11549 * Check if monitor has continuous frequency mode
11551 if (data->type != EDID_DETAIL_MONITOR_RANGE)
11554 * Check for flag range limits only. If flag == 1 then
11555 * no additional timing information provided.
11556 * Default GTF, GTF Secondary curve and CVT are not
11559 if (range->flags != 1)
11562 amdgpu_dm_connector->min_vfreq = range->min_vfreq;
11563 amdgpu_dm_connector->max_vfreq = range->max_vfreq;
11564 amdgpu_dm_connector->pixel_clock_mhz =
11565 range->pixel_clock_mhz * 10;
11567 connector->display_info.monitor_range.min_vfreq = range->min_vfreq;
11568 connector->display_info.monitor_range.max_vfreq = range->max_vfreq;
11573 if (amdgpu_dm_connector->max_vfreq -
11574 amdgpu_dm_connector->min_vfreq > 10) {
11576 freesync_capable = true;
11579 } else if (edid && sink->sink_signal == SIGNAL_TYPE_HDMI_TYPE_A) {
11580 i = parse_hdmi_amd_vsdb(amdgpu_dm_connector, edid, &vsdb_info);
11581 if (i >= 0 && vsdb_info.freesync_supported) {
11582 timing = &edid->detailed_timings[i];
11583 data = &timing->data.other_data;
11585 amdgpu_dm_connector->min_vfreq = vsdb_info.min_refresh_rate_hz;
11586 amdgpu_dm_connector->max_vfreq = vsdb_info.max_refresh_rate_hz;
11587 if (amdgpu_dm_connector->max_vfreq - amdgpu_dm_connector->min_vfreq > 10)
11588 freesync_capable = true;
11590 connector->display_info.monitor_range.min_vfreq = vsdb_info.min_refresh_rate_hz;
11591 connector->display_info.monitor_range.max_vfreq = vsdb_info.max_refresh_rate_hz;
11597 dm_con_state->freesync_capable = freesync_capable;
11599 if (connector->vrr_capable_property)
11600 drm_connector_set_vrr_capable_property(connector,
11604 void amdgpu_dm_trigger_timing_sync(struct drm_device *dev)
11606 struct amdgpu_device *adev = drm_to_adev(dev);
11607 struct dc *dc = adev->dm.dc;
11610 mutex_lock(&adev->dm.dc_lock);
11611 if (dc->current_state) {
11612 for (i = 0; i < dc->current_state->stream_count; ++i)
11613 dc->current_state->streams[i]
11614 ->triggered_crtc_reset.enabled =
11615 adev->dm.force_timing_sync;
11617 dm_enable_per_frame_crtc_master_sync(dc->current_state);
11618 dc_trigger_sync(dc, dc->current_state);
11620 mutex_unlock(&adev->dm.dc_lock);
11623 void dm_write_reg_func(const struct dc_context *ctx, uint32_t address,
11624 uint32_t value, const char *func_name)
11626 #ifdef DM_CHECK_ADDR_0
11627 if (address == 0) {
11628 DC_ERR("invalid register write. address = 0");
11632 cgs_write_register(ctx->cgs_device, address, value);
11633 trace_amdgpu_dc_wreg(&ctx->perf_trace->write_count, address, value);
11636 uint32_t dm_read_reg_func(const struct dc_context *ctx, uint32_t address,
11637 const char *func_name)
11640 #ifdef DM_CHECK_ADDR_0
11641 if (address == 0) {
11642 DC_ERR("invalid register read; address = 0\n");
11647 if (ctx->dmub_srv &&
11648 ctx->dmub_srv->reg_helper_offload.gather_in_progress &&
11649 !ctx->dmub_srv->reg_helper_offload.should_burst_write) {
11654 value = cgs_read_register(ctx->cgs_device, address);
11656 trace_amdgpu_dc_rreg(&ctx->perf_trace->read_count, address, value);
11661 static int amdgpu_dm_set_dmub_async_sync_status(bool is_cmd_aux,
11662 struct dc_context *ctx,
11663 uint8_t status_type,
11664 uint32_t *operation_result)
11666 struct amdgpu_device *adev = ctx->driver_context;
11667 int return_status = -1;
11668 struct dmub_notification *p_notify = adev->dm.dmub_notify;
11671 if (status_type == DMUB_ASYNC_TO_SYNC_ACCESS_SUCCESS) {
11672 return_status = p_notify->aux_reply.length;
11673 *operation_result = p_notify->result;
11674 } else if (status_type == DMUB_ASYNC_TO_SYNC_ACCESS_TIMEOUT) {
11675 *operation_result = AUX_RET_ERROR_TIMEOUT;
11676 } else if (status_type == DMUB_ASYNC_TO_SYNC_ACCESS_FAIL) {
11677 *operation_result = AUX_RET_ERROR_ENGINE_ACQUIRE;
11679 *operation_result = AUX_RET_ERROR_UNKNOWN;
11682 if (status_type == DMUB_ASYNC_TO_SYNC_ACCESS_SUCCESS) {
11684 *operation_result = p_notify->sc_status;
11686 *operation_result = SET_CONFIG_UNKNOWN_ERROR;
11690 return return_status;
11693 int amdgpu_dm_process_dmub_aux_transfer_sync(bool is_cmd_aux, struct dc_context *ctx,
11694 unsigned int link_index, void *cmd_payload, void *operation_result)
11696 struct amdgpu_device *adev = ctx->driver_context;
11700 dc_process_dmub_aux_transfer_async(ctx->dc,
11701 link_index, (struct aux_payload *)cmd_payload);
11702 } else if (dc_process_dmub_set_config_async(ctx->dc, link_index,
11703 (struct set_config_cmd_payload *)cmd_payload,
11704 adev->dm.dmub_notify)) {
11705 return amdgpu_dm_set_dmub_async_sync_status(is_cmd_aux,
11706 ctx, DMUB_ASYNC_TO_SYNC_ACCESS_SUCCESS,
11707 (uint32_t *)operation_result);
11710 ret = wait_for_completion_timeout(&adev->dm.dmub_aux_transfer_done, 10 * HZ);
11712 DRM_ERROR("wait_for_completion_timeout timeout!");
11713 return amdgpu_dm_set_dmub_async_sync_status(is_cmd_aux,
11714 ctx, DMUB_ASYNC_TO_SYNC_ACCESS_TIMEOUT,
11715 (uint32_t *)operation_result);
11719 if (adev->dm.dmub_notify->result == AUX_RET_SUCCESS) {
11720 struct aux_payload *payload = (struct aux_payload *)cmd_payload;
11722 payload->reply[0] = adev->dm.dmub_notify->aux_reply.command;
11723 if (!payload->write && adev->dm.dmub_notify->aux_reply.length &&
11724 payload->reply[0] == AUX_TRANSACTION_REPLY_AUX_ACK) {
11725 memcpy(payload->data, adev->dm.dmub_notify->aux_reply.data,
11726 adev->dm.dmub_notify->aux_reply.length);
11731 return amdgpu_dm_set_dmub_async_sync_status(is_cmd_aux,
11732 ctx, DMUB_ASYNC_TO_SYNC_ACCESS_SUCCESS,
11733 (uint32_t *)operation_result);
11737 * Check whether seamless boot is supported.
11739 * So far we only support seamless boot on CHIP_VANGOGH.
11740 * If everything goes well, we may consider expanding
11741 * seamless boot to other ASICs.
11743 bool check_seamless_boot_capability(struct amdgpu_device *adev)
11745 switch (adev->asic_type) {
11747 if (!adev->mman.keep_stolen_vga_memory)