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 "link_enc_cfg.h"
32 #include "dc/inc/core_types.h"
33 #include "dal_asic_id.h"
34 #include "dmub/dmub_srv.h"
35 #include "dc/inc/hw/dmcu.h"
36 #include "dc/inc/hw/abm.h"
37 #include "dc/dc_dmub_srv.h"
38 #include "dc/dc_edid_parser.h"
39 #include "dc/dc_stat.h"
40 #include "amdgpu_dm_trace.h"
41 #include "dpcd_defs.h"
42 #include "link/protocols/link_dpcd.h"
43 #include "link_service_types.h"
44 #include "link/protocols/link_dp_capability.h"
45 #include "link/protocols/link_ddc.h"
49 #include "amdgpu_display.h"
50 #include "amdgpu_ucode.h"
52 #include "amdgpu_dm.h"
53 #include "amdgpu_dm_plane.h"
54 #include "amdgpu_dm_crtc.h"
55 #include "amdgpu_dm_hdcp.h"
56 #include <drm/display/drm_hdcp_helper.h>
57 #include "amdgpu_pm.h"
58 #include "amdgpu_atombios.h"
60 #include "amd_shared.h"
61 #include "amdgpu_dm_irq.h"
62 #include "dm_helpers.h"
63 #include "amdgpu_dm_mst_types.h"
64 #if defined(CONFIG_DEBUG_FS)
65 #include "amdgpu_dm_debugfs.h"
67 #include "amdgpu_dm_psr.h"
69 #include "ivsrcid/ivsrcid_vislands30.h"
71 #include <linux/backlight.h>
72 #include <linux/module.h>
73 #include <linux/moduleparam.h>
74 #include <linux/types.h>
75 #include <linux/pm_runtime.h>
76 #include <linux/pci.h>
77 #include <linux/firmware.h>
78 #include <linux/component.h>
79 #include <linux/dmi.h>
81 #include <drm/display/drm_dp_mst_helper.h>
82 #include <drm/display/drm_hdmi_helper.h>
83 #include <drm/drm_atomic.h>
84 #include <drm/drm_atomic_uapi.h>
85 #include <drm/drm_atomic_helper.h>
86 #include <drm/drm_blend.h>
87 #include <drm/drm_fourcc.h>
88 #include <drm/drm_edid.h>
89 #include <drm/drm_vblank.h>
90 #include <drm/drm_audio_component.h>
91 #include <drm/drm_gem_atomic_helper.h>
92 #include <drm/drm_plane_helper.h>
94 #include <acpi/video.h>
96 #include "ivsrcid/dcn/irqsrcs_dcn_1_0.h"
98 #include "dcn/dcn_1_0_offset.h"
99 #include "dcn/dcn_1_0_sh_mask.h"
100 #include "soc15_hw_ip.h"
101 #include "soc15_common.h"
102 #include "vega10_ip_offset.h"
104 #include "gc/gc_11_0_0_offset.h"
105 #include "gc/gc_11_0_0_sh_mask.h"
107 #include "modules/inc/mod_freesync.h"
108 #include "modules/power/power_helpers.h"
110 #define FIRMWARE_RENOIR_DMUB "amdgpu/renoir_dmcub.bin"
111 MODULE_FIRMWARE(FIRMWARE_RENOIR_DMUB);
112 #define FIRMWARE_SIENNA_CICHLID_DMUB "amdgpu/sienna_cichlid_dmcub.bin"
113 MODULE_FIRMWARE(FIRMWARE_SIENNA_CICHLID_DMUB);
114 #define FIRMWARE_NAVY_FLOUNDER_DMUB "amdgpu/navy_flounder_dmcub.bin"
115 MODULE_FIRMWARE(FIRMWARE_NAVY_FLOUNDER_DMUB);
116 #define FIRMWARE_GREEN_SARDINE_DMUB "amdgpu/green_sardine_dmcub.bin"
117 MODULE_FIRMWARE(FIRMWARE_GREEN_SARDINE_DMUB);
118 #define FIRMWARE_VANGOGH_DMUB "amdgpu/vangogh_dmcub.bin"
119 MODULE_FIRMWARE(FIRMWARE_VANGOGH_DMUB);
120 #define FIRMWARE_DIMGREY_CAVEFISH_DMUB "amdgpu/dimgrey_cavefish_dmcub.bin"
121 MODULE_FIRMWARE(FIRMWARE_DIMGREY_CAVEFISH_DMUB);
122 #define FIRMWARE_BEIGE_GOBY_DMUB "amdgpu/beige_goby_dmcub.bin"
123 MODULE_FIRMWARE(FIRMWARE_BEIGE_GOBY_DMUB);
124 #define FIRMWARE_YELLOW_CARP_DMUB "amdgpu/yellow_carp_dmcub.bin"
125 MODULE_FIRMWARE(FIRMWARE_YELLOW_CARP_DMUB);
126 #define FIRMWARE_DCN_314_DMUB "amdgpu/dcn_3_1_4_dmcub.bin"
127 MODULE_FIRMWARE(FIRMWARE_DCN_314_DMUB);
128 #define FIRMWARE_DCN_315_DMUB "amdgpu/dcn_3_1_5_dmcub.bin"
129 MODULE_FIRMWARE(FIRMWARE_DCN_315_DMUB);
130 #define FIRMWARE_DCN316_DMUB "amdgpu/dcn_3_1_6_dmcub.bin"
131 MODULE_FIRMWARE(FIRMWARE_DCN316_DMUB);
133 #define FIRMWARE_DCN_V3_2_0_DMCUB "amdgpu/dcn_3_2_0_dmcub.bin"
134 MODULE_FIRMWARE(FIRMWARE_DCN_V3_2_0_DMCUB);
135 #define FIRMWARE_DCN_V3_2_1_DMCUB "amdgpu/dcn_3_2_1_dmcub.bin"
136 MODULE_FIRMWARE(FIRMWARE_DCN_V3_2_1_DMCUB);
138 #define FIRMWARE_RAVEN_DMCU "amdgpu/raven_dmcu.bin"
139 MODULE_FIRMWARE(FIRMWARE_RAVEN_DMCU);
141 #define FIRMWARE_NAVI12_DMCU "amdgpu/navi12_dmcu.bin"
142 MODULE_FIRMWARE(FIRMWARE_NAVI12_DMCU);
144 /* Number of bytes in PSP header for firmware. */
145 #define PSP_HEADER_BYTES 0x100
147 /* Number of bytes in PSP footer for firmware. */
148 #define PSP_FOOTER_BYTES 0x100
153 * The AMDgpu display manager, **amdgpu_dm** (or even simpler,
154 * **dm**) sits between DRM and DC. It acts as a liaison, converting DRM
155 * requests into DC requests, and DC responses into DRM responses.
157 * The root control structure is &struct amdgpu_display_manager.
160 /* basic init/fini API */
161 static int amdgpu_dm_init(struct amdgpu_device *adev);
162 static void amdgpu_dm_fini(struct amdgpu_device *adev);
163 static bool is_freesync_video_mode(const struct drm_display_mode *mode, struct amdgpu_dm_connector *aconnector);
165 static enum drm_mode_subconnector get_subconnector_type(struct dc_link *link)
167 switch (link->dpcd_caps.dongle_type) {
168 case DISPLAY_DONGLE_NONE:
169 return DRM_MODE_SUBCONNECTOR_Native;
170 case DISPLAY_DONGLE_DP_VGA_CONVERTER:
171 return DRM_MODE_SUBCONNECTOR_VGA;
172 case DISPLAY_DONGLE_DP_DVI_CONVERTER:
173 case DISPLAY_DONGLE_DP_DVI_DONGLE:
174 return DRM_MODE_SUBCONNECTOR_DVID;
175 case DISPLAY_DONGLE_DP_HDMI_CONVERTER:
176 case DISPLAY_DONGLE_DP_HDMI_DONGLE:
177 return DRM_MODE_SUBCONNECTOR_HDMIA;
178 case DISPLAY_DONGLE_DP_HDMI_MISMATCHED_DONGLE:
180 return DRM_MODE_SUBCONNECTOR_Unknown;
184 static void update_subconnector_property(struct amdgpu_dm_connector *aconnector)
186 struct dc_link *link = aconnector->dc_link;
187 struct drm_connector *connector = &aconnector->base;
188 enum drm_mode_subconnector subconnector = DRM_MODE_SUBCONNECTOR_Unknown;
190 if (connector->connector_type != DRM_MODE_CONNECTOR_DisplayPort)
193 if (aconnector->dc_sink)
194 subconnector = get_subconnector_type(link);
196 drm_object_property_set_value(&connector->base,
197 connector->dev->mode_config.dp_subconnector_property,
202 * initializes drm_device display related structures, based on the information
203 * provided by DAL. The drm strcutures are: drm_crtc, drm_connector,
204 * drm_encoder, drm_mode_config
206 * Returns 0 on success
208 static int amdgpu_dm_initialize_drm_device(struct amdgpu_device *adev);
209 /* removes and deallocates the drm structures, created by the above function */
210 static void amdgpu_dm_destroy_drm_device(struct amdgpu_display_manager *dm);
212 static int amdgpu_dm_connector_init(struct amdgpu_display_manager *dm,
213 struct amdgpu_dm_connector *amdgpu_dm_connector,
215 struct amdgpu_encoder *amdgpu_encoder);
216 static int amdgpu_dm_encoder_init(struct drm_device *dev,
217 struct amdgpu_encoder *aencoder,
218 uint32_t link_index);
220 static int amdgpu_dm_connector_get_modes(struct drm_connector *connector);
222 static void amdgpu_dm_atomic_commit_tail(struct drm_atomic_state *state);
224 static int amdgpu_dm_atomic_check(struct drm_device *dev,
225 struct drm_atomic_state *state);
227 static void handle_hpd_irq_helper(struct amdgpu_dm_connector *aconnector);
228 static void handle_hpd_rx_irq(void *param);
231 is_timing_unchanged_for_freesync(struct drm_crtc_state *old_crtc_state,
232 struct drm_crtc_state *new_crtc_state);
234 * dm_vblank_get_counter
237 * Get counter for number of vertical blanks
240 * struct amdgpu_device *adev - [in] desired amdgpu device
241 * int disp_idx - [in] which CRTC to get the counter from
244 * Counter for vertical blanks
246 static u32 dm_vblank_get_counter(struct amdgpu_device *adev, int crtc)
248 struct amdgpu_crtc *acrtc = NULL;
250 if (crtc >= adev->mode_info.num_crtc)
253 acrtc = adev->mode_info.crtcs[crtc];
255 if (!acrtc->dm_irq_params.stream) {
256 DRM_ERROR("dc_stream_state is NULL for crtc '%d'!\n",
261 return dc_stream_get_vblank_counter(acrtc->dm_irq_params.stream);
264 static int dm_crtc_get_scanoutpos(struct amdgpu_device *adev, int crtc,
265 u32 *vbl, u32 *position)
267 u32 v_blank_start, v_blank_end, h_position, v_position;
268 struct amdgpu_crtc *acrtc = NULL;
270 if ((crtc < 0) || (crtc >= adev->mode_info.num_crtc))
273 acrtc = adev->mode_info.crtcs[crtc];
275 if (!acrtc->dm_irq_params.stream) {
276 DRM_ERROR("dc_stream_state is NULL for crtc '%d'!\n",
282 * TODO rework base driver to use values directly.
283 * for now parse it back into reg-format
285 dc_stream_get_scanoutpos(acrtc->dm_irq_params.stream,
291 *position = v_position | (h_position << 16);
292 *vbl = v_blank_start | (v_blank_end << 16);
297 static bool dm_is_idle(void *handle)
303 static int dm_wait_for_idle(void *handle)
309 static bool dm_check_soft_reset(void *handle)
314 static int dm_soft_reset(void *handle)
320 static struct amdgpu_crtc *
321 get_crtc_by_otg_inst(struct amdgpu_device *adev,
324 struct drm_device *dev = adev_to_drm(adev);
325 struct drm_crtc *crtc;
326 struct amdgpu_crtc *amdgpu_crtc;
328 if (WARN_ON(otg_inst == -1))
329 return adev->mode_info.crtcs[0];
331 list_for_each_entry(crtc, &dev->mode_config.crtc_list, head) {
332 amdgpu_crtc = to_amdgpu_crtc(crtc);
334 if (amdgpu_crtc->otg_inst == otg_inst)
341 static inline bool is_dc_timing_adjust_needed(struct dm_crtc_state *old_state,
342 struct dm_crtc_state *new_state)
344 if (new_state->freesync_config.state == VRR_STATE_ACTIVE_FIXED)
346 else if (amdgpu_dm_crtc_vrr_active(old_state) != amdgpu_dm_crtc_vrr_active(new_state))
352 static inline void reverse_planes_order(struct dc_surface_update *array_of_surface_update,
357 for (i = 0, j = planes_count - 1; i < j; i++, j--)
358 swap(array_of_surface_update[i], array_of_surface_update[j]);
362 * update_planes_and_stream_adapter() - Send planes to be updated in DC
364 * DC has a generic way to update planes and stream via
365 * dc_update_planes_and_stream function; however, DM might need some
366 * adjustments and preparation before calling it. This function is a wrapper
367 * for the dc_update_planes_and_stream that does any required configuration
368 * before passing control to DC.
370 * @dc: Display Core control structure
371 * @update_type: specify whether it is FULL/MEDIUM/FAST update
372 * @planes_count: planes count to update
373 * @stream: stream state
374 * @stream_update: stream update
375 * @array_of_surface_update: dc surface update pointer
378 static inline bool update_planes_and_stream_adapter(struct dc *dc,
381 struct dc_stream_state *stream,
382 struct dc_stream_update *stream_update,
383 struct dc_surface_update *array_of_surface_update)
385 reverse_planes_order(array_of_surface_update, planes_count);
388 * Previous frame finished and HW is ready for optimization.
390 if (update_type == UPDATE_TYPE_FAST)
391 dc_post_update_surfaces_to_stream(dc);
393 return dc_update_planes_and_stream(dc,
394 array_of_surface_update,
401 * dm_pflip_high_irq() - Handle pageflip interrupt
402 * @interrupt_params: ignored
404 * Handles the pageflip interrupt by notifying all interested parties
405 * that the pageflip has been completed.
407 static void dm_pflip_high_irq(void *interrupt_params)
409 struct amdgpu_crtc *amdgpu_crtc;
410 struct common_irq_params *irq_params = interrupt_params;
411 struct amdgpu_device *adev = irq_params->adev;
413 struct drm_pending_vblank_event *e;
414 u32 vpos, hpos, v_blank_start, v_blank_end;
417 amdgpu_crtc = get_crtc_by_otg_inst(adev, irq_params->irq_src - IRQ_TYPE_PFLIP);
419 /* IRQ could occur when in initial stage */
420 /* TODO work and BO cleanup */
421 if (amdgpu_crtc == NULL) {
422 DC_LOG_PFLIP("CRTC is null, returning.\n");
426 spin_lock_irqsave(&adev_to_drm(adev)->event_lock, flags);
428 if (amdgpu_crtc->pflip_status != AMDGPU_FLIP_SUBMITTED) {
429 DC_LOG_PFLIP("amdgpu_crtc->pflip_status = %d !=AMDGPU_FLIP_SUBMITTED(%d) on crtc:%d[%p]\n",
430 amdgpu_crtc->pflip_status,
431 AMDGPU_FLIP_SUBMITTED,
432 amdgpu_crtc->crtc_id,
434 spin_unlock_irqrestore(&adev_to_drm(adev)->event_lock, flags);
438 /* page flip completed. */
439 e = amdgpu_crtc->event;
440 amdgpu_crtc->event = NULL;
444 vrr_active = amdgpu_dm_crtc_vrr_active_irq(amdgpu_crtc);
446 /* Fixed refresh rate, or VRR scanout position outside front-porch? */
448 !dc_stream_get_scanoutpos(amdgpu_crtc->dm_irq_params.stream, &v_blank_start,
449 &v_blank_end, &hpos, &vpos) ||
450 (vpos < v_blank_start)) {
451 /* Update to correct count and vblank timestamp if racing with
452 * vblank irq. This also updates to the correct vblank timestamp
453 * even in VRR mode, as scanout is past the front-porch atm.
455 drm_crtc_accurate_vblank_count(&amdgpu_crtc->base);
457 /* Wake up userspace by sending the pageflip event with proper
458 * count and timestamp of vblank of flip completion.
461 drm_crtc_send_vblank_event(&amdgpu_crtc->base, e);
463 /* Event sent, so done with vblank for this flip */
464 drm_crtc_vblank_put(&amdgpu_crtc->base);
467 /* VRR active and inside front-porch: vblank count and
468 * timestamp for pageflip event will only be up to date after
469 * drm_crtc_handle_vblank() has been executed from late vblank
470 * irq handler after start of back-porch (vline 0). We queue the
471 * pageflip event for send-out by drm_crtc_handle_vblank() with
472 * updated timestamp and count, once it runs after us.
474 * We need to open-code this instead of using the helper
475 * drm_crtc_arm_vblank_event(), as that helper would
476 * call drm_crtc_accurate_vblank_count(), which we must
477 * not call in VRR mode while we are in front-porch!
480 /* sequence will be replaced by real count during send-out. */
481 e->sequence = drm_crtc_vblank_count(&amdgpu_crtc->base);
482 e->pipe = amdgpu_crtc->crtc_id;
484 list_add_tail(&e->base.link, &adev_to_drm(adev)->vblank_event_list);
488 /* Keep track of vblank of this flip for flip throttling. We use the
489 * cooked hw counter, as that one incremented at start of this vblank
490 * of pageflip completion, so last_flip_vblank is the forbidden count
491 * for queueing new pageflips if vsync + VRR is enabled.
493 amdgpu_crtc->dm_irq_params.last_flip_vblank =
494 amdgpu_get_vblank_counter_kms(&amdgpu_crtc->base);
496 amdgpu_crtc->pflip_status = AMDGPU_FLIP_NONE;
497 spin_unlock_irqrestore(&adev_to_drm(adev)->event_lock, flags);
499 DC_LOG_PFLIP("crtc:%d[%p], pflip_stat:AMDGPU_FLIP_NONE, vrr[%d]-fp %d\n",
500 amdgpu_crtc->crtc_id, amdgpu_crtc,
501 vrr_active, (int) !e);
504 static void dm_vupdate_high_irq(void *interrupt_params)
506 struct common_irq_params *irq_params = interrupt_params;
507 struct amdgpu_device *adev = irq_params->adev;
508 struct amdgpu_crtc *acrtc;
509 struct drm_device *drm_dev;
510 struct drm_vblank_crtc *vblank;
511 ktime_t frame_duration_ns, previous_timestamp;
515 acrtc = get_crtc_by_otg_inst(adev, irq_params->irq_src - IRQ_TYPE_VUPDATE);
518 vrr_active = amdgpu_dm_crtc_vrr_active_irq(acrtc);
519 drm_dev = acrtc->base.dev;
520 vblank = &drm_dev->vblank[acrtc->base.index];
521 previous_timestamp = atomic64_read(&irq_params->previous_timestamp);
522 frame_duration_ns = vblank->time - previous_timestamp;
524 if (frame_duration_ns > 0) {
525 trace_amdgpu_refresh_rate_track(acrtc->base.index,
527 ktime_divns(NSEC_PER_SEC, frame_duration_ns));
528 atomic64_set(&irq_params->previous_timestamp, vblank->time);
531 DC_LOG_VBLANK("crtc:%d, vupdate-vrr:%d\n",
535 /* Core vblank handling is done here after end of front-porch in
536 * vrr mode, as vblank timestamping will give valid results
537 * while now done after front-porch. This will also deliver
538 * page-flip completion events that have been queued to us
539 * if a pageflip happened inside front-porch.
542 amdgpu_dm_crtc_handle_vblank(acrtc);
544 /* BTR processing for pre-DCE12 ASICs */
545 if (acrtc->dm_irq_params.stream &&
546 adev->family < AMDGPU_FAMILY_AI) {
547 spin_lock_irqsave(&adev_to_drm(adev)->event_lock, flags);
548 mod_freesync_handle_v_update(
549 adev->dm.freesync_module,
550 acrtc->dm_irq_params.stream,
551 &acrtc->dm_irq_params.vrr_params);
553 dc_stream_adjust_vmin_vmax(
555 acrtc->dm_irq_params.stream,
556 &acrtc->dm_irq_params.vrr_params.adjust);
557 spin_unlock_irqrestore(&adev_to_drm(adev)->event_lock, flags);
564 * dm_crtc_high_irq() - Handles CRTC interrupt
565 * @interrupt_params: used for determining the CRTC instance
567 * Handles the CRTC/VSYNC interrupt by notfying DRM's VBLANK
570 static void dm_crtc_high_irq(void *interrupt_params)
572 struct common_irq_params *irq_params = interrupt_params;
573 struct amdgpu_device *adev = irq_params->adev;
574 struct amdgpu_crtc *acrtc;
578 acrtc = get_crtc_by_otg_inst(adev, irq_params->irq_src - IRQ_TYPE_VBLANK);
582 vrr_active = amdgpu_dm_crtc_vrr_active_irq(acrtc);
584 DC_LOG_VBLANK("crtc:%d, vupdate-vrr:%d, planes:%d\n", acrtc->crtc_id,
585 vrr_active, acrtc->dm_irq_params.active_planes);
588 * Core vblank handling at start of front-porch is only possible
589 * in non-vrr mode, as only there vblank timestamping will give
590 * valid results while done in front-porch. Otherwise defer it
591 * to dm_vupdate_high_irq after end of front-porch.
594 amdgpu_dm_crtc_handle_vblank(acrtc);
597 * Following stuff must happen at start of vblank, for crc
598 * computation and below-the-range btr support in vrr mode.
600 amdgpu_dm_crtc_handle_crc_irq(&acrtc->base);
602 /* BTR updates need to happen before VUPDATE on Vega and above. */
603 if (adev->family < AMDGPU_FAMILY_AI)
606 spin_lock_irqsave(&adev_to_drm(adev)->event_lock, flags);
608 if (acrtc->dm_irq_params.stream &&
609 acrtc->dm_irq_params.vrr_params.supported &&
610 acrtc->dm_irq_params.freesync_config.state ==
611 VRR_STATE_ACTIVE_VARIABLE) {
612 mod_freesync_handle_v_update(adev->dm.freesync_module,
613 acrtc->dm_irq_params.stream,
614 &acrtc->dm_irq_params.vrr_params);
616 dc_stream_adjust_vmin_vmax(adev->dm.dc, acrtc->dm_irq_params.stream,
617 &acrtc->dm_irq_params.vrr_params.adjust);
621 * If there aren't any active_planes then DCH HUBP may be clock-gated.
622 * In that case, pageflip completion interrupts won't fire and pageflip
623 * completion events won't get delivered. Prevent this by sending
624 * pending pageflip events from here if a flip is still pending.
626 * If any planes are enabled, use dm_pflip_high_irq() instead, to
627 * avoid race conditions between flip programming and completion,
628 * which could cause too early flip completion events.
630 if (adev->family >= AMDGPU_FAMILY_RV &&
631 acrtc->pflip_status == AMDGPU_FLIP_SUBMITTED &&
632 acrtc->dm_irq_params.active_planes == 0) {
634 drm_crtc_send_vblank_event(&acrtc->base, acrtc->event);
636 drm_crtc_vblank_put(&acrtc->base);
638 acrtc->pflip_status = AMDGPU_FLIP_NONE;
641 spin_unlock_irqrestore(&adev_to_drm(adev)->event_lock, flags);
644 #if defined(CONFIG_DRM_AMD_SECURE_DISPLAY)
646 * dm_dcn_vertical_interrupt0_high_irq() - Handles OTG Vertical interrupt0 for
647 * DCN generation ASICs
648 * @interrupt_params: interrupt parameters
650 * Used to set crc window/read out crc value at vertical line 0 position
652 static void dm_dcn_vertical_interrupt0_high_irq(void *interrupt_params)
654 struct common_irq_params *irq_params = interrupt_params;
655 struct amdgpu_device *adev = irq_params->adev;
656 struct amdgpu_crtc *acrtc;
658 acrtc = get_crtc_by_otg_inst(adev, irq_params->irq_src - IRQ_TYPE_VLINE0);
663 amdgpu_dm_crtc_handle_crc_window_irq(&acrtc->base);
665 #endif /* CONFIG_DRM_AMD_SECURE_DISPLAY */
668 * dmub_aux_setconfig_callback - Callback for AUX or SET_CONFIG command.
669 * @adev: amdgpu_device pointer
670 * @notify: dmub notification structure
672 * Dmub AUX or SET_CONFIG command completion processing callback
673 * Copies dmub notification to DM which is to be read by AUX command.
674 * issuing thread and also signals the event to wake up the thread.
676 static void dmub_aux_setconfig_callback(struct amdgpu_device *adev,
677 struct dmub_notification *notify)
679 if (adev->dm.dmub_notify)
680 memcpy(adev->dm.dmub_notify, notify, sizeof(struct dmub_notification));
681 if (notify->type == DMUB_NOTIFICATION_AUX_REPLY)
682 complete(&adev->dm.dmub_aux_transfer_done);
686 * dmub_hpd_callback - DMUB HPD interrupt processing callback.
687 * @adev: amdgpu_device pointer
688 * @notify: dmub notification structure
690 * Dmub Hpd interrupt processing callback. Gets displayindex through the
691 * ink index and calls helper to do the processing.
693 static void dmub_hpd_callback(struct amdgpu_device *adev,
694 struct dmub_notification *notify)
696 struct amdgpu_dm_connector *aconnector;
697 struct amdgpu_dm_connector *hpd_aconnector = NULL;
698 struct drm_connector *connector;
699 struct drm_connector_list_iter iter;
700 struct dc_link *link;
702 struct drm_device *dev;
707 if (notify == NULL) {
708 DRM_ERROR("DMUB HPD callback notification was NULL");
712 if (notify->link_index > adev->dm.dc->link_count) {
713 DRM_ERROR("DMUB HPD index (%u)is abnormal", notify->link_index);
717 link_index = notify->link_index;
718 link = adev->dm.dc->links[link_index];
721 drm_connector_list_iter_begin(dev, &iter);
722 drm_for_each_connector_iter(connector, &iter) {
723 aconnector = to_amdgpu_dm_connector(connector);
724 if (link && aconnector->dc_link == link) {
725 if (notify->type == DMUB_NOTIFICATION_HPD)
726 DRM_INFO("DMUB HPD callback: link_index=%u\n", link_index);
727 else if (notify->type == DMUB_NOTIFICATION_HPD_IRQ)
728 DRM_INFO("DMUB HPD IRQ callback: link_index=%u\n", link_index);
730 DRM_WARN("DMUB Unknown HPD callback type %d, link_index=%u\n",
731 notify->type, link_index);
733 hpd_aconnector = aconnector;
737 drm_connector_list_iter_end(&iter);
739 if (hpd_aconnector) {
740 if (notify->type == DMUB_NOTIFICATION_HPD)
741 handle_hpd_irq_helper(hpd_aconnector);
742 else if (notify->type == DMUB_NOTIFICATION_HPD_IRQ)
743 handle_hpd_rx_irq(hpd_aconnector);
748 * register_dmub_notify_callback - Sets callback for DMUB notify
749 * @adev: amdgpu_device pointer
750 * @type: Type of dmub notification
751 * @callback: Dmub interrupt callback function
752 * @dmub_int_thread_offload: offload indicator
754 * API to register a dmub callback handler for a dmub notification
755 * Also sets indicator whether callback processing to be offloaded.
756 * to dmub interrupt handling thread
757 * Return: true if successfully registered, false if there is existing registration
759 static bool register_dmub_notify_callback(struct amdgpu_device *adev,
760 enum dmub_notification_type type,
761 dmub_notify_interrupt_callback_t callback,
762 bool dmub_int_thread_offload)
764 if (callback != NULL && type < ARRAY_SIZE(adev->dm.dmub_thread_offload)) {
765 adev->dm.dmub_callback[type] = callback;
766 adev->dm.dmub_thread_offload[type] = dmub_int_thread_offload;
773 static void dm_handle_hpd_work(struct work_struct *work)
775 struct dmub_hpd_work *dmub_hpd_wrk;
777 dmub_hpd_wrk = container_of(work, struct dmub_hpd_work, handle_hpd_work);
779 if (!dmub_hpd_wrk->dmub_notify) {
780 DRM_ERROR("dmub_hpd_wrk dmub_notify is NULL");
784 if (dmub_hpd_wrk->dmub_notify->type < ARRAY_SIZE(dmub_hpd_wrk->adev->dm.dmub_callback)) {
785 dmub_hpd_wrk->adev->dm.dmub_callback[dmub_hpd_wrk->dmub_notify->type](dmub_hpd_wrk->adev,
786 dmub_hpd_wrk->dmub_notify);
789 kfree(dmub_hpd_wrk->dmub_notify);
794 #define DMUB_TRACE_MAX_READ 64
796 * dm_dmub_outbox1_low_irq() - Handles Outbox interrupt
797 * @interrupt_params: used for determining the Outbox instance
799 * Handles the Outbox Interrupt
802 static void dm_dmub_outbox1_low_irq(void *interrupt_params)
804 struct dmub_notification notify;
805 struct common_irq_params *irq_params = interrupt_params;
806 struct amdgpu_device *adev = irq_params->adev;
807 struct amdgpu_display_manager *dm = &adev->dm;
808 struct dmcub_trace_buf_entry entry = { 0 };
810 struct dmub_hpd_work *dmub_hpd_wrk;
811 struct dc_link *plink = NULL;
813 if (dc_enable_dmub_notifications(adev->dm.dc) &&
814 irq_params->irq_src == DC_IRQ_SOURCE_DMCUB_OUTBOX) {
817 dc_stat_get_dmub_notification(adev->dm.dc, ¬ify);
818 if (notify.type >= ARRAY_SIZE(dm->dmub_thread_offload)) {
819 DRM_ERROR("DM: notify type %d invalid!", notify.type);
822 if (!dm->dmub_callback[notify.type]) {
823 DRM_DEBUG_DRIVER("DMUB notification skipped, no handler: type=%d\n", notify.type);
826 if (dm->dmub_thread_offload[notify.type] == true) {
827 dmub_hpd_wrk = kzalloc(sizeof(*dmub_hpd_wrk), GFP_ATOMIC);
829 DRM_ERROR("Failed to allocate dmub_hpd_wrk");
832 dmub_hpd_wrk->dmub_notify = kmemdup(¬ify, sizeof(struct dmub_notification),
834 if (!dmub_hpd_wrk->dmub_notify) {
836 DRM_ERROR("Failed to allocate dmub_hpd_wrk->dmub_notify");
839 INIT_WORK(&dmub_hpd_wrk->handle_hpd_work, dm_handle_hpd_work);
840 dmub_hpd_wrk->adev = adev;
841 if (notify.type == DMUB_NOTIFICATION_HPD) {
842 plink = adev->dm.dc->links[notify.link_index];
845 notify.hpd_status == DP_HPD_PLUG;
848 queue_work(adev->dm.delayed_hpd_wq, &dmub_hpd_wrk->handle_hpd_work);
850 dm->dmub_callback[notify.type](adev, ¬ify);
852 } while (notify.pending_notification);
857 if (dc_dmub_srv_get_dmub_outbox0_msg(dm->dc, &entry)) {
858 trace_amdgpu_dmub_trace_high_irq(entry.trace_code, entry.tick_count,
859 entry.param0, entry.param1);
861 DRM_DEBUG_DRIVER("trace_code:%u, tick_count:%u, param0:%u, param1:%u\n",
862 entry.trace_code, entry.tick_count, entry.param0, entry.param1);
868 } while (count <= DMUB_TRACE_MAX_READ);
870 if (count > DMUB_TRACE_MAX_READ)
871 DRM_DEBUG_DRIVER("Warning : count > DMUB_TRACE_MAX_READ");
874 static int dm_set_clockgating_state(void *handle,
875 enum amd_clockgating_state state)
880 static int dm_set_powergating_state(void *handle,
881 enum amd_powergating_state state)
886 /* Prototypes of private functions */
887 static int dm_early_init(void *handle);
889 /* Allocate memory for FBC compressed data */
890 static void amdgpu_dm_fbc_init(struct drm_connector *connector)
892 struct drm_device *dev = connector->dev;
893 struct amdgpu_device *adev = drm_to_adev(dev);
894 struct dm_compressor_info *compressor = &adev->dm.compressor;
895 struct amdgpu_dm_connector *aconn = to_amdgpu_dm_connector(connector);
896 struct drm_display_mode *mode;
897 unsigned long max_size = 0;
899 if (adev->dm.dc->fbc_compressor == NULL)
902 if (aconn->dc_link->connector_signal != SIGNAL_TYPE_EDP)
905 if (compressor->bo_ptr)
909 list_for_each_entry(mode, &connector->modes, head) {
910 if (max_size < mode->htotal * mode->vtotal)
911 max_size = mode->htotal * mode->vtotal;
915 int r = amdgpu_bo_create_kernel(adev, max_size * 4, PAGE_SIZE,
916 AMDGPU_GEM_DOMAIN_GTT, &compressor->bo_ptr,
917 &compressor->gpu_addr, &compressor->cpu_addr);
920 DRM_ERROR("DM: Failed to initialize FBC\n");
922 adev->dm.dc->ctx->fbc_gpu_addr = compressor->gpu_addr;
923 DRM_INFO("DM: FBC alloc %lu\n", max_size*4);
930 static int amdgpu_dm_audio_component_get_eld(struct device *kdev, int port,
931 int pipe, bool *enabled,
932 unsigned char *buf, int max_bytes)
934 struct drm_device *dev = dev_get_drvdata(kdev);
935 struct amdgpu_device *adev = drm_to_adev(dev);
936 struct drm_connector *connector;
937 struct drm_connector_list_iter conn_iter;
938 struct amdgpu_dm_connector *aconnector;
943 mutex_lock(&adev->dm.audio_lock);
945 drm_connector_list_iter_begin(dev, &conn_iter);
946 drm_for_each_connector_iter(connector, &conn_iter) {
947 aconnector = to_amdgpu_dm_connector(connector);
948 if (aconnector->audio_inst != port)
952 ret = drm_eld_size(connector->eld);
953 memcpy(buf, connector->eld, min(max_bytes, ret));
957 drm_connector_list_iter_end(&conn_iter);
959 mutex_unlock(&adev->dm.audio_lock);
961 DRM_DEBUG_KMS("Get ELD : idx=%d ret=%d en=%d\n", port, ret, *enabled);
966 static const struct drm_audio_component_ops amdgpu_dm_audio_component_ops = {
967 .get_eld = amdgpu_dm_audio_component_get_eld,
970 static int amdgpu_dm_audio_component_bind(struct device *kdev,
971 struct device *hda_kdev, void *data)
973 struct drm_device *dev = dev_get_drvdata(kdev);
974 struct amdgpu_device *adev = drm_to_adev(dev);
975 struct drm_audio_component *acomp = data;
977 acomp->ops = &amdgpu_dm_audio_component_ops;
979 adev->dm.audio_component = acomp;
984 static void amdgpu_dm_audio_component_unbind(struct device *kdev,
985 struct device *hda_kdev, void *data)
987 struct drm_device *dev = dev_get_drvdata(kdev);
988 struct amdgpu_device *adev = drm_to_adev(dev);
989 struct drm_audio_component *acomp = data;
993 adev->dm.audio_component = NULL;
996 static const struct component_ops amdgpu_dm_audio_component_bind_ops = {
997 .bind = amdgpu_dm_audio_component_bind,
998 .unbind = amdgpu_dm_audio_component_unbind,
1001 static int amdgpu_dm_audio_init(struct amdgpu_device *adev)
1008 adev->mode_info.audio.enabled = true;
1010 adev->mode_info.audio.num_pins = adev->dm.dc->res_pool->audio_count;
1012 for (i = 0; i < adev->mode_info.audio.num_pins; i++) {
1013 adev->mode_info.audio.pin[i].channels = -1;
1014 adev->mode_info.audio.pin[i].rate = -1;
1015 adev->mode_info.audio.pin[i].bits_per_sample = -1;
1016 adev->mode_info.audio.pin[i].status_bits = 0;
1017 adev->mode_info.audio.pin[i].category_code = 0;
1018 adev->mode_info.audio.pin[i].connected = false;
1019 adev->mode_info.audio.pin[i].id =
1020 adev->dm.dc->res_pool->audios[i]->inst;
1021 adev->mode_info.audio.pin[i].offset = 0;
1024 ret = component_add(adev->dev, &amdgpu_dm_audio_component_bind_ops);
1028 adev->dm.audio_registered = true;
1033 static void amdgpu_dm_audio_fini(struct amdgpu_device *adev)
1038 if (!adev->mode_info.audio.enabled)
1041 if (adev->dm.audio_registered) {
1042 component_del(adev->dev, &amdgpu_dm_audio_component_bind_ops);
1043 adev->dm.audio_registered = false;
1046 /* TODO: Disable audio? */
1048 adev->mode_info.audio.enabled = false;
1051 static void amdgpu_dm_audio_eld_notify(struct amdgpu_device *adev, int pin)
1053 struct drm_audio_component *acomp = adev->dm.audio_component;
1055 if (acomp && acomp->audio_ops && acomp->audio_ops->pin_eld_notify) {
1056 DRM_DEBUG_KMS("Notify ELD: %d\n", pin);
1058 acomp->audio_ops->pin_eld_notify(acomp->audio_ops->audio_ptr,
1063 static int dm_dmub_hw_init(struct amdgpu_device *adev)
1065 const struct dmcub_firmware_header_v1_0 *hdr;
1066 struct dmub_srv *dmub_srv = adev->dm.dmub_srv;
1067 struct dmub_srv_fb_info *fb_info = adev->dm.dmub_fb_info;
1068 const struct firmware *dmub_fw = adev->dm.dmub_fw;
1069 struct dmcu *dmcu = adev->dm.dc->res_pool->dmcu;
1070 struct abm *abm = adev->dm.dc->res_pool->abm;
1071 struct dmub_srv_hw_params hw_params;
1072 enum dmub_status status;
1073 const unsigned char *fw_inst_const, *fw_bss_data;
1074 u32 i, fw_inst_const_size, fw_bss_data_size;
1075 bool has_hw_support;
1078 /* DMUB isn't supported on the ASIC. */
1082 DRM_ERROR("No framebuffer info for DMUB service.\n");
1087 /* Firmware required for DMUB support. */
1088 DRM_ERROR("No firmware provided for DMUB.\n");
1092 status = dmub_srv_has_hw_support(dmub_srv, &has_hw_support);
1093 if (status != DMUB_STATUS_OK) {
1094 DRM_ERROR("Error checking HW support for DMUB: %d\n", status);
1098 if (!has_hw_support) {
1099 DRM_INFO("DMUB unsupported on ASIC\n");
1103 /* Reset DMCUB if it was previously running - before we overwrite its memory. */
1104 status = dmub_srv_hw_reset(dmub_srv);
1105 if (status != DMUB_STATUS_OK)
1106 DRM_WARN("Error resetting DMUB HW: %d\n", status);
1108 hdr = (const struct dmcub_firmware_header_v1_0 *)dmub_fw->data;
1110 fw_inst_const = dmub_fw->data +
1111 le32_to_cpu(hdr->header.ucode_array_offset_bytes) +
1114 fw_bss_data = dmub_fw->data +
1115 le32_to_cpu(hdr->header.ucode_array_offset_bytes) +
1116 le32_to_cpu(hdr->inst_const_bytes);
1118 /* Copy firmware and bios info into FB memory. */
1119 fw_inst_const_size = le32_to_cpu(hdr->inst_const_bytes) -
1120 PSP_HEADER_BYTES - PSP_FOOTER_BYTES;
1122 fw_bss_data_size = le32_to_cpu(hdr->bss_data_bytes);
1124 /* if adev->firmware.load_type == AMDGPU_FW_LOAD_PSP,
1125 * amdgpu_ucode_init_single_fw will load dmub firmware
1126 * fw_inst_const part to cw0; otherwise, the firmware back door load
1127 * will be done by dm_dmub_hw_init
1129 if (adev->firmware.load_type != AMDGPU_FW_LOAD_PSP) {
1130 memcpy(fb_info->fb[DMUB_WINDOW_0_INST_CONST].cpu_addr, fw_inst_const,
1131 fw_inst_const_size);
1134 if (fw_bss_data_size)
1135 memcpy(fb_info->fb[DMUB_WINDOW_2_BSS_DATA].cpu_addr,
1136 fw_bss_data, fw_bss_data_size);
1138 /* Copy firmware bios info into FB memory. */
1139 memcpy(fb_info->fb[DMUB_WINDOW_3_VBIOS].cpu_addr, adev->bios,
1142 /* Reset regions that need to be reset. */
1143 memset(fb_info->fb[DMUB_WINDOW_4_MAILBOX].cpu_addr, 0,
1144 fb_info->fb[DMUB_WINDOW_4_MAILBOX].size);
1146 memset(fb_info->fb[DMUB_WINDOW_5_TRACEBUFF].cpu_addr, 0,
1147 fb_info->fb[DMUB_WINDOW_5_TRACEBUFF].size);
1149 memset(fb_info->fb[DMUB_WINDOW_6_FW_STATE].cpu_addr, 0,
1150 fb_info->fb[DMUB_WINDOW_6_FW_STATE].size);
1152 /* Initialize hardware. */
1153 memset(&hw_params, 0, sizeof(hw_params));
1154 hw_params.fb_base = adev->gmc.fb_start;
1155 hw_params.fb_offset = adev->vm_manager.vram_base_offset;
1157 /* backdoor load firmware and trigger dmub running */
1158 if (adev->firmware.load_type != AMDGPU_FW_LOAD_PSP)
1159 hw_params.load_inst_const = true;
1162 hw_params.psp_version = dmcu->psp_version;
1164 for (i = 0; i < fb_info->num_fb; ++i)
1165 hw_params.fb[i] = &fb_info->fb[i];
1167 switch (adev->ip_versions[DCE_HWIP][0]) {
1168 case IP_VERSION(3, 1, 3):
1169 case IP_VERSION(3, 1, 4):
1170 hw_params.dpia_supported = true;
1171 hw_params.disable_dpia = adev->dm.dc->debug.dpia_debug.bits.disable_dpia;
1177 status = dmub_srv_hw_init(dmub_srv, &hw_params);
1178 if (status != DMUB_STATUS_OK) {
1179 DRM_ERROR("Error initializing DMUB HW: %d\n", status);
1183 /* Wait for firmware load to finish. */
1184 status = dmub_srv_wait_for_auto_load(dmub_srv, 100000);
1185 if (status != DMUB_STATUS_OK)
1186 DRM_WARN("Wait for DMUB auto-load failed: %d\n", status);
1188 /* Init DMCU and ABM if available. */
1190 dmcu->funcs->dmcu_init(dmcu);
1191 abm->dmcu_is_running = dmcu->funcs->is_dmcu_initialized(dmcu);
1194 if (!adev->dm.dc->ctx->dmub_srv)
1195 adev->dm.dc->ctx->dmub_srv = dc_dmub_srv_create(adev->dm.dc, dmub_srv);
1196 if (!adev->dm.dc->ctx->dmub_srv) {
1197 DRM_ERROR("Couldn't allocate DC DMUB server!\n");
1201 DRM_INFO("DMUB hardware initialized: version=0x%08X\n",
1202 adev->dm.dmcub_fw_version);
1207 static void dm_dmub_hw_resume(struct amdgpu_device *adev)
1209 struct dmub_srv *dmub_srv = adev->dm.dmub_srv;
1210 enum dmub_status status;
1214 /* DMUB isn't supported on the ASIC. */
1218 status = dmub_srv_is_hw_init(dmub_srv, &init);
1219 if (status != DMUB_STATUS_OK)
1220 DRM_WARN("DMUB hardware init check failed: %d\n", status);
1222 if (status == DMUB_STATUS_OK && init) {
1223 /* Wait for firmware load to finish. */
1224 status = dmub_srv_wait_for_auto_load(dmub_srv, 100000);
1225 if (status != DMUB_STATUS_OK)
1226 DRM_WARN("Wait for DMUB auto-load failed: %d\n", status);
1228 /* Perform the full hardware initialization. */
1229 dm_dmub_hw_init(adev);
1233 static void mmhub_read_system_context(struct amdgpu_device *adev, struct dc_phy_addr_space_config *pa_config)
1236 u32 logical_addr_low;
1237 u32 logical_addr_high;
1238 u32 agp_base, agp_bot, agp_top;
1239 PHYSICAL_ADDRESS_LOC page_table_start, page_table_end, page_table_base;
1241 memset(pa_config, 0, sizeof(*pa_config));
1244 agp_bot = adev->gmc.agp_start >> 24;
1245 agp_top = adev->gmc.agp_end >> 24;
1247 /* AGP aperture is disabled */
1248 if (agp_bot == agp_top) {
1249 logical_addr_low = adev->gmc.fb_start >> 18;
1250 if (adev->apu_flags & AMD_APU_IS_RAVEN2)
1252 * Raven2 has a HW issue that it is unable to use the vram which
1253 * is out of MC_VM_SYSTEM_APERTURE_HIGH_ADDR. So here is the
1254 * workaround that increase system aperture high address (add 1)
1255 * to get rid of the VM fault and hardware hang.
1257 logical_addr_high = (adev->gmc.fb_end >> 18) + 0x1;
1259 logical_addr_high = adev->gmc.fb_end >> 18;
1261 logical_addr_low = min(adev->gmc.fb_start, adev->gmc.agp_start) >> 18;
1262 if (adev->apu_flags & AMD_APU_IS_RAVEN2)
1264 * Raven2 has a HW issue that it is unable to use the vram which
1265 * is out of MC_VM_SYSTEM_APERTURE_HIGH_ADDR. So here is the
1266 * workaround that increase system aperture high address (add 1)
1267 * to get rid of the VM fault and hardware hang.
1269 logical_addr_high = max((adev->gmc.fb_end >> 18) + 0x1, adev->gmc.agp_end >> 18);
1271 logical_addr_high = max(adev->gmc.fb_end, adev->gmc.agp_end) >> 18;
1274 pt_base = amdgpu_gmc_pd_addr(adev->gart.bo);
1276 page_table_start.high_part = (u32)(adev->gmc.gart_start >> 44) & 0xF;
1277 page_table_start.low_part = (u32)(adev->gmc.gart_start >> 12);
1278 page_table_end.high_part = (u32)(adev->gmc.gart_end >> 44) & 0xF;
1279 page_table_end.low_part = (u32)(adev->gmc.gart_end >> 12);
1280 page_table_base.high_part = upper_32_bits(pt_base) & 0xF;
1281 page_table_base.low_part = lower_32_bits(pt_base);
1283 pa_config->system_aperture.start_addr = (uint64_t)logical_addr_low << 18;
1284 pa_config->system_aperture.end_addr = (uint64_t)logical_addr_high << 18;
1286 pa_config->system_aperture.agp_base = (uint64_t)agp_base << 24;
1287 pa_config->system_aperture.agp_bot = (uint64_t)agp_bot << 24;
1288 pa_config->system_aperture.agp_top = (uint64_t)agp_top << 24;
1290 pa_config->system_aperture.fb_base = adev->gmc.fb_start;
1291 pa_config->system_aperture.fb_offset = adev->vm_manager.vram_base_offset;
1292 pa_config->system_aperture.fb_top = adev->gmc.fb_end;
1294 pa_config->gart_config.page_table_start_addr = page_table_start.quad_part << 12;
1295 pa_config->gart_config.page_table_end_addr = page_table_end.quad_part << 12;
1296 pa_config->gart_config.page_table_base_addr = page_table_base.quad_part;
1298 pa_config->is_hvm_enabled = adev->mode_info.gpu_vm_support;
1302 static void force_connector_state(
1303 struct amdgpu_dm_connector *aconnector,
1304 enum drm_connector_force force_state)
1306 struct drm_connector *connector = &aconnector->base;
1308 mutex_lock(&connector->dev->mode_config.mutex);
1309 aconnector->base.force = force_state;
1310 mutex_unlock(&connector->dev->mode_config.mutex);
1312 mutex_lock(&aconnector->hpd_lock);
1313 drm_kms_helper_connector_hotplug_event(connector);
1314 mutex_unlock(&aconnector->hpd_lock);
1317 static void dm_handle_hpd_rx_offload_work(struct work_struct *work)
1319 struct hpd_rx_irq_offload_work *offload_work;
1320 struct amdgpu_dm_connector *aconnector;
1321 struct dc_link *dc_link;
1322 struct amdgpu_device *adev;
1323 enum dc_connection_type new_connection_type = dc_connection_none;
1324 unsigned long flags;
1325 union test_response test_response;
1327 memset(&test_response, 0, sizeof(test_response));
1329 offload_work = container_of(work, struct hpd_rx_irq_offload_work, work);
1330 aconnector = offload_work->offload_wq->aconnector;
1333 DRM_ERROR("Can't retrieve aconnector in hpd_rx_irq_offload_work");
1337 adev = drm_to_adev(aconnector->base.dev);
1338 dc_link = aconnector->dc_link;
1340 mutex_lock(&aconnector->hpd_lock);
1341 if (!dc_link_detect_connection_type(dc_link, &new_connection_type))
1342 DRM_ERROR("KMS: Failed to detect connector\n");
1343 mutex_unlock(&aconnector->hpd_lock);
1345 if (new_connection_type == dc_connection_none)
1348 if (amdgpu_in_reset(adev))
1351 if (offload_work->data.bytes.device_service_irq.bits.UP_REQ_MSG_RDY ||
1352 offload_work->data.bytes.device_service_irq.bits.DOWN_REP_MSG_RDY) {
1353 dm_handle_mst_sideband_msg_ready_event(&aconnector->mst_mgr, DOWN_OR_UP_MSG_RDY_EVENT);
1354 spin_lock_irqsave(&offload_work->offload_wq->offload_lock, flags);
1355 offload_work->offload_wq->is_handling_mst_msg_rdy_event = false;
1356 spin_unlock_irqrestore(&offload_work->offload_wq->offload_lock, flags);
1360 mutex_lock(&adev->dm.dc_lock);
1361 if (offload_work->data.bytes.device_service_irq.bits.AUTOMATED_TEST) {
1362 dc_link_dp_handle_automated_test(dc_link);
1364 if (aconnector->timing_changed) {
1365 /* force connector disconnect and reconnect */
1366 force_connector_state(aconnector, DRM_FORCE_OFF);
1368 force_connector_state(aconnector, DRM_FORCE_UNSPECIFIED);
1371 test_response.bits.ACK = 1;
1373 core_link_write_dpcd(
1377 sizeof(test_response));
1378 } else if ((dc_link->connector_signal != SIGNAL_TYPE_EDP) &&
1379 dc_link_check_link_loss_status(dc_link, &offload_work->data) &&
1380 dc_link_dp_allow_hpd_rx_irq(dc_link)) {
1381 /* offload_work->data is from handle_hpd_rx_irq->
1382 * schedule_hpd_rx_offload_work.this is defer handle
1383 * for hpd short pulse. upon here, link status may be
1384 * changed, need get latest link status from dpcd
1385 * registers. if link status is good, skip run link
1388 union hpd_irq_data irq_data;
1390 memset(&irq_data, 0, sizeof(irq_data));
1392 /* before dc_link_dp_handle_link_loss, allow new link lost handle
1393 * request be added to work queue if link lost at end of dc_link_
1394 * dp_handle_link_loss
1396 spin_lock_irqsave(&offload_work->offload_wq->offload_lock, flags);
1397 offload_work->offload_wq->is_handling_link_loss = false;
1398 spin_unlock_irqrestore(&offload_work->offload_wq->offload_lock, flags);
1400 if ((dc_link_dp_read_hpd_rx_irq_data(dc_link, &irq_data) == DC_OK) &&
1401 dc_link_check_link_loss_status(dc_link, &irq_data))
1402 dc_link_dp_handle_link_loss(dc_link);
1404 mutex_unlock(&adev->dm.dc_lock);
1407 kfree(offload_work);
1411 static struct hpd_rx_irq_offload_work_queue *hpd_rx_irq_create_workqueue(struct dc *dc)
1413 int max_caps = dc->caps.max_links;
1415 struct hpd_rx_irq_offload_work_queue *hpd_rx_offload_wq = NULL;
1417 hpd_rx_offload_wq = kcalloc(max_caps, sizeof(*hpd_rx_offload_wq), GFP_KERNEL);
1419 if (!hpd_rx_offload_wq)
1423 for (i = 0; i < max_caps; i++) {
1424 hpd_rx_offload_wq[i].wq =
1425 create_singlethread_workqueue("amdgpu_dm_hpd_rx_offload_wq");
1427 if (hpd_rx_offload_wq[i].wq == NULL) {
1428 DRM_ERROR("create amdgpu_dm_hpd_rx_offload_wq fail!");
1432 spin_lock_init(&hpd_rx_offload_wq[i].offload_lock);
1435 return hpd_rx_offload_wq;
1438 for (i = 0; i < max_caps; i++) {
1439 if (hpd_rx_offload_wq[i].wq)
1440 destroy_workqueue(hpd_rx_offload_wq[i].wq);
1442 kfree(hpd_rx_offload_wq);
1446 struct amdgpu_stutter_quirk {
1454 static const struct amdgpu_stutter_quirk amdgpu_stutter_quirk_list[] = {
1455 /* https://bugzilla.kernel.org/show_bug.cgi?id=214417 */
1456 { 0x1002, 0x15dd, 0x1002, 0x15dd, 0xc8 },
1460 static bool dm_should_disable_stutter(struct pci_dev *pdev)
1462 const struct amdgpu_stutter_quirk *p = amdgpu_stutter_quirk_list;
1464 while (p && p->chip_device != 0) {
1465 if (pdev->vendor == p->chip_vendor &&
1466 pdev->device == p->chip_device &&
1467 pdev->subsystem_vendor == p->subsys_vendor &&
1468 pdev->subsystem_device == p->subsys_device &&
1469 pdev->revision == p->revision) {
1477 static const struct dmi_system_id hpd_disconnect_quirk_table[] = {
1480 DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
1481 DMI_MATCH(DMI_PRODUCT_NAME, "Precision 3660"),
1486 DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
1487 DMI_MATCH(DMI_PRODUCT_NAME, "Precision 3260"),
1492 DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
1493 DMI_MATCH(DMI_PRODUCT_NAME, "Precision 3460"),
1498 DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
1499 DMI_MATCH(DMI_PRODUCT_NAME, "OptiPlex Tower Plus 7010"),
1504 DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
1505 DMI_MATCH(DMI_PRODUCT_NAME, "OptiPlex Tower 7010"),
1510 DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
1511 DMI_MATCH(DMI_PRODUCT_NAME, "OptiPlex SFF Plus 7010"),
1516 DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
1517 DMI_MATCH(DMI_PRODUCT_NAME, "OptiPlex SFF 7010"),
1522 DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
1523 DMI_MATCH(DMI_PRODUCT_NAME, "OptiPlex Micro Plus 7010"),
1528 DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
1529 DMI_MATCH(DMI_PRODUCT_NAME, "OptiPlex Micro 7010"),
1533 /* TODO: refactor this from a fixed table to a dynamic option */
1536 static void retrieve_dmi_info(struct amdgpu_display_manager *dm)
1538 const struct dmi_system_id *dmi_id;
1540 dm->aux_hpd_discon_quirk = false;
1542 dmi_id = dmi_first_match(hpd_disconnect_quirk_table);
1544 dm->aux_hpd_discon_quirk = true;
1545 DRM_INFO("aux_hpd_discon_quirk attached\n");
1549 static int amdgpu_dm_init(struct amdgpu_device *adev)
1551 struct dc_init_data init_data;
1552 struct dc_callback_init init_params;
1555 adev->dm.ddev = adev_to_drm(adev);
1556 adev->dm.adev = adev;
1558 /* Zero all the fields */
1559 memset(&init_data, 0, sizeof(init_data));
1560 memset(&init_params, 0, sizeof(init_params));
1562 mutex_init(&adev->dm.dpia_aux_lock);
1563 mutex_init(&adev->dm.dc_lock);
1564 mutex_init(&adev->dm.audio_lock);
1566 if (amdgpu_dm_irq_init(adev)) {
1567 DRM_ERROR("amdgpu: failed to initialize DM IRQ support.\n");
1571 init_data.asic_id.chip_family = adev->family;
1573 init_data.asic_id.pci_revision_id = adev->pdev->revision;
1574 init_data.asic_id.hw_internal_rev = adev->external_rev_id;
1575 init_data.asic_id.chip_id = adev->pdev->device;
1577 init_data.asic_id.vram_width = adev->gmc.vram_width;
1578 /* TODO: initialize init_data.asic_id.vram_type here!!!! */
1579 init_data.asic_id.atombios_base_address =
1580 adev->mode_info.atom_context->bios;
1582 init_data.driver = adev;
1584 adev->dm.cgs_device = amdgpu_cgs_create_device(adev);
1586 if (!adev->dm.cgs_device) {
1587 DRM_ERROR("amdgpu: failed to create cgs device.\n");
1591 init_data.cgs_device = adev->dm.cgs_device;
1593 init_data.dce_environment = DCE_ENV_PRODUCTION_DRV;
1595 switch (adev->ip_versions[DCE_HWIP][0]) {
1596 case IP_VERSION(2, 1, 0):
1597 switch (adev->dm.dmcub_fw_version) {
1598 case 0: /* development */
1599 case 0x1: /* linux-firmware.git hash 6d9f399 */
1600 case 0x01000000: /* linux-firmware.git hash 9a0b0f4 */
1601 init_data.flags.disable_dmcu = false;
1604 init_data.flags.disable_dmcu = true;
1607 case IP_VERSION(2, 0, 3):
1608 init_data.flags.disable_dmcu = true;
1614 switch (adev->asic_type) {
1617 init_data.flags.gpu_vm_support = true;
1620 switch (adev->ip_versions[DCE_HWIP][0]) {
1621 case IP_VERSION(1, 0, 0):
1622 case IP_VERSION(1, 0, 1):
1623 /* enable S/G on PCO and RV2 */
1624 if ((adev->apu_flags & AMD_APU_IS_RAVEN2) ||
1625 (adev->apu_flags & AMD_APU_IS_PICASSO))
1626 init_data.flags.gpu_vm_support = true;
1628 case IP_VERSION(2, 1, 0):
1629 case IP_VERSION(3, 0, 1):
1630 case IP_VERSION(3, 1, 2):
1631 case IP_VERSION(3, 1, 3):
1632 case IP_VERSION(3, 1, 4):
1633 case IP_VERSION(3, 1, 5):
1634 case IP_VERSION(3, 1, 6):
1635 init_data.flags.gpu_vm_support = true;
1642 if (init_data.flags.gpu_vm_support)
1643 init_data.flags.gpu_vm_support = amdgpu_sg_display_supported(adev);
1645 if (init_data.flags.gpu_vm_support)
1646 adev->mode_info.gpu_vm_support = true;
1648 if (amdgpu_dc_feature_mask & DC_FBC_MASK)
1649 init_data.flags.fbc_support = true;
1651 if (amdgpu_dc_feature_mask & DC_MULTI_MON_PP_MCLK_SWITCH_MASK)
1652 init_data.flags.multi_mon_pp_mclk_switch = true;
1654 if (amdgpu_dc_feature_mask & DC_DISABLE_FRACTIONAL_PWM_MASK)
1655 init_data.flags.disable_fractional_pwm = true;
1657 if (amdgpu_dc_feature_mask & DC_EDP_NO_POWER_SEQUENCING)
1658 init_data.flags.edp_no_power_sequencing = true;
1660 if (amdgpu_dc_feature_mask & DC_DISABLE_LTTPR_DP1_4A)
1661 init_data.flags.allow_lttpr_non_transparent_mode.bits.DP1_4A = true;
1662 if (amdgpu_dc_feature_mask & DC_DISABLE_LTTPR_DP2_0)
1663 init_data.flags.allow_lttpr_non_transparent_mode.bits.DP2_0 = true;
1665 init_data.flags.seamless_boot_edp_requested = false;
1667 if (check_seamless_boot_capability(adev)) {
1668 init_data.flags.seamless_boot_edp_requested = true;
1669 init_data.flags.allow_seamless_boot_optimization = true;
1670 DRM_INFO("Seamless boot condition check passed\n");
1673 init_data.flags.enable_mipi_converter_optimization = true;
1675 init_data.dcn_reg_offsets = adev->reg_offset[DCE_HWIP][0];
1676 init_data.nbio_reg_offsets = adev->reg_offset[NBIO_HWIP][0];
1678 INIT_LIST_HEAD(&adev->dm.da_list);
1680 retrieve_dmi_info(&adev->dm);
1682 /* Display Core create. */
1683 adev->dm.dc = dc_create(&init_data);
1686 DRM_INFO("Display Core v%s initialized on %s\n", DC_VER,
1687 dce_version_to_string(adev->dm.dc->ctx->dce_version));
1689 DRM_INFO("Display Core v%s failed to initialize on %s\n", DC_VER,
1690 dce_version_to_string(adev->dm.dc->ctx->dce_version));
1694 if (amdgpu_dc_debug_mask & DC_DISABLE_PIPE_SPLIT) {
1695 adev->dm.dc->debug.force_single_disp_pipe_split = false;
1696 adev->dm.dc->debug.pipe_split_policy = MPC_SPLIT_AVOID;
1699 if (adev->asic_type != CHIP_CARRIZO && adev->asic_type != CHIP_STONEY)
1700 adev->dm.dc->debug.disable_stutter = amdgpu_pp_feature_mask & PP_STUTTER_MODE ? false : true;
1701 if (dm_should_disable_stutter(adev->pdev))
1702 adev->dm.dc->debug.disable_stutter = true;
1704 if (amdgpu_dc_debug_mask & DC_DISABLE_STUTTER)
1705 adev->dm.dc->debug.disable_stutter = true;
1707 if (amdgpu_dc_debug_mask & DC_DISABLE_DSC)
1708 adev->dm.dc->debug.disable_dsc = true;
1710 if (amdgpu_dc_debug_mask & DC_DISABLE_CLOCK_GATING)
1711 adev->dm.dc->debug.disable_clock_gate = true;
1713 if (amdgpu_dc_debug_mask & DC_FORCE_SUBVP_MCLK_SWITCH)
1714 adev->dm.dc->debug.force_subvp_mclk_switch = true;
1716 adev->dm.dc->debug.visual_confirm = amdgpu_dc_visual_confirm;
1718 /* TODO: Remove after DP2 receiver gets proper support of Cable ID feature */
1719 adev->dm.dc->debug.ignore_cable_id = true;
1721 /* TODO: There is a new drm mst change where the freedom of
1722 * vc_next_start_slot update is revoked/moved into drm, instead of in
1723 * driver. This forces us to make sure to get vc_next_start_slot updated
1724 * in drm function each time without considering if mst_state is active
1725 * or not. Otherwise, next time hotplug will give wrong start_slot
1726 * number. We are implementing a temporary solution to even notify drm
1727 * mst deallocation when link is no longer of MST type when uncommitting
1728 * the stream so we will have more time to work on a proper solution.
1729 * Ideally when dm_helpers_dp_mst_stop_top_mgr message is triggered, we
1730 * should notify drm to do a complete "reset" of its states and stop
1731 * calling further drm mst functions when link is no longer of an MST
1732 * type. This could happen when we unplug an MST hubs/displays. When
1733 * uncommit stream comes later after unplug, we should just reset
1734 * hardware states only.
1736 adev->dm.dc->debug.temp_mst_deallocation_sequence = true;
1738 if (adev->dm.dc->caps.dp_hdmi21_pcon_support)
1739 DRM_INFO("DP-HDMI FRL PCON supported\n");
1741 r = dm_dmub_hw_init(adev);
1743 DRM_ERROR("DMUB interface failed to initialize: status=%d\n", r);
1747 dc_hardware_init(adev->dm.dc);
1749 adev->dm.hpd_rx_offload_wq = hpd_rx_irq_create_workqueue(adev->dm.dc);
1750 if (!adev->dm.hpd_rx_offload_wq) {
1751 DRM_ERROR("amdgpu: failed to create hpd rx offload workqueue.\n");
1755 if ((adev->flags & AMD_IS_APU) && (adev->asic_type >= CHIP_CARRIZO)) {
1756 struct dc_phy_addr_space_config pa_config;
1758 mmhub_read_system_context(adev, &pa_config);
1760 // Call the DC init_memory func
1761 dc_setup_system_context(adev->dm.dc, &pa_config);
1764 adev->dm.freesync_module = mod_freesync_create(adev->dm.dc);
1765 if (!adev->dm.freesync_module) {
1767 "amdgpu: failed to initialize freesync_module.\n");
1769 DRM_DEBUG_DRIVER("amdgpu: freesync_module init done %p.\n",
1770 adev->dm.freesync_module);
1772 amdgpu_dm_init_color_mod();
1774 if (adev->dm.dc->caps.max_links > 0) {
1775 adev->dm.vblank_control_workqueue =
1776 create_singlethread_workqueue("dm_vblank_control_workqueue");
1777 if (!adev->dm.vblank_control_workqueue)
1778 DRM_ERROR("amdgpu: failed to initialize vblank_workqueue.\n");
1781 if (adev->dm.dc->caps.max_links > 0 && adev->family >= AMDGPU_FAMILY_RV) {
1782 adev->dm.hdcp_workqueue = hdcp_create_workqueue(adev, &init_params.cp_psp, adev->dm.dc);
1784 if (!adev->dm.hdcp_workqueue)
1785 DRM_ERROR("amdgpu: failed to initialize hdcp_workqueue.\n");
1787 DRM_DEBUG_DRIVER("amdgpu: hdcp_workqueue init done %p.\n", adev->dm.hdcp_workqueue);
1789 dc_init_callbacks(adev->dm.dc, &init_params);
1791 if (dc_is_dmub_outbox_supported(adev->dm.dc)) {
1792 init_completion(&adev->dm.dmub_aux_transfer_done);
1793 adev->dm.dmub_notify = kzalloc(sizeof(struct dmub_notification), GFP_KERNEL);
1794 if (!adev->dm.dmub_notify) {
1795 DRM_INFO("amdgpu: fail to allocate adev->dm.dmub_notify");
1799 adev->dm.delayed_hpd_wq = create_singlethread_workqueue("amdgpu_dm_hpd_wq");
1800 if (!adev->dm.delayed_hpd_wq) {
1801 DRM_ERROR("amdgpu: failed to create hpd offload workqueue.\n");
1805 amdgpu_dm_outbox_init(adev);
1806 if (!register_dmub_notify_callback(adev, DMUB_NOTIFICATION_AUX_REPLY,
1807 dmub_aux_setconfig_callback, false)) {
1808 DRM_ERROR("amdgpu: fail to register dmub aux callback");
1811 if (!register_dmub_notify_callback(adev, DMUB_NOTIFICATION_HPD, dmub_hpd_callback, true)) {
1812 DRM_ERROR("amdgpu: fail to register dmub hpd callback");
1815 if (!register_dmub_notify_callback(adev, DMUB_NOTIFICATION_HPD_IRQ, dmub_hpd_callback, true)) {
1816 DRM_ERROR("amdgpu: fail to register dmub hpd callback");
1821 /* Enable outbox notification only after IRQ handlers are registered and DMUB is alive.
1822 * It is expected that DMUB will resend any pending notifications at this point, for
1823 * example HPD from DPIA.
1825 if (dc_is_dmub_outbox_supported(adev->dm.dc)) {
1826 dc_enable_dmub_outbox(adev->dm.dc);
1828 /* DPIA trace goes to dmesg logs only if outbox is enabled */
1829 if (amdgpu_dc_debug_mask & DC_ENABLE_DPIA_TRACE)
1830 dc_dmub_srv_enable_dpia_trace(adev->dm.dc);
1833 if (amdgpu_dm_initialize_drm_device(adev)) {
1835 "amdgpu: failed to initialize sw for display support.\n");
1839 /* create fake encoders for MST */
1840 dm_dp_create_fake_mst_encoders(adev);
1842 /* TODO: Add_display_info? */
1844 /* TODO use dynamic cursor width */
1845 adev_to_drm(adev)->mode_config.cursor_width = adev->dm.dc->caps.max_cursor_size;
1846 adev_to_drm(adev)->mode_config.cursor_height = adev->dm.dc->caps.max_cursor_size;
1848 if (drm_vblank_init(adev_to_drm(adev), adev->dm.display_indexes_num)) {
1850 "amdgpu: failed to initialize sw for display support.\n");
1854 #if defined(CONFIG_DRM_AMD_SECURE_DISPLAY)
1855 adev->dm.secure_display_ctxs = amdgpu_dm_crtc_secure_display_create_contexts(adev);
1856 if (!adev->dm.secure_display_ctxs)
1857 DRM_ERROR("amdgpu: failed to initialize secure display contexts.\n");
1860 DRM_DEBUG_DRIVER("KMS initialized.\n");
1864 amdgpu_dm_fini(adev);
1869 static int amdgpu_dm_early_fini(void *handle)
1871 struct amdgpu_device *adev = (struct amdgpu_device *)handle;
1873 amdgpu_dm_audio_fini(adev);
1878 static void amdgpu_dm_fini(struct amdgpu_device *adev)
1882 if (adev->dm.vblank_control_workqueue) {
1883 destroy_workqueue(adev->dm.vblank_control_workqueue);
1884 adev->dm.vblank_control_workqueue = NULL;
1887 amdgpu_dm_destroy_drm_device(&adev->dm);
1889 #if defined(CONFIG_DRM_AMD_SECURE_DISPLAY)
1890 if (adev->dm.secure_display_ctxs) {
1891 for (i = 0; i < adev->mode_info.num_crtc; i++) {
1892 if (adev->dm.secure_display_ctxs[i].crtc) {
1893 flush_work(&adev->dm.secure_display_ctxs[i].notify_ta_work);
1894 flush_work(&adev->dm.secure_display_ctxs[i].forward_roi_work);
1897 kfree(adev->dm.secure_display_ctxs);
1898 adev->dm.secure_display_ctxs = NULL;
1901 if (adev->dm.hdcp_workqueue) {
1902 hdcp_destroy(&adev->dev->kobj, adev->dm.hdcp_workqueue);
1903 adev->dm.hdcp_workqueue = NULL;
1907 dc_deinit_callbacks(adev->dm.dc);
1910 dc_dmub_srv_destroy(&adev->dm.dc->ctx->dmub_srv);
1912 if (dc_enable_dmub_notifications(adev->dm.dc)) {
1913 kfree(adev->dm.dmub_notify);
1914 adev->dm.dmub_notify = NULL;
1915 destroy_workqueue(adev->dm.delayed_hpd_wq);
1916 adev->dm.delayed_hpd_wq = NULL;
1919 if (adev->dm.dmub_bo)
1920 amdgpu_bo_free_kernel(&adev->dm.dmub_bo,
1921 &adev->dm.dmub_bo_gpu_addr,
1922 &adev->dm.dmub_bo_cpu_addr);
1924 if (adev->dm.hpd_rx_offload_wq) {
1925 for (i = 0; i < adev->dm.dc->caps.max_links; i++) {
1926 if (adev->dm.hpd_rx_offload_wq[i].wq) {
1927 destroy_workqueue(adev->dm.hpd_rx_offload_wq[i].wq);
1928 adev->dm.hpd_rx_offload_wq[i].wq = NULL;
1932 kfree(adev->dm.hpd_rx_offload_wq);
1933 adev->dm.hpd_rx_offload_wq = NULL;
1936 /* DC Destroy TODO: Replace destroy DAL */
1938 dc_destroy(&adev->dm.dc);
1940 * TODO: pageflip, vlank interrupt
1942 * amdgpu_dm_irq_fini(adev);
1945 if (adev->dm.cgs_device) {
1946 amdgpu_cgs_destroy_device(adev->dm.cgs_device);
1947 adev->dm.cgs_device = NULL;
1949 if (adev->dm.freesync_module) {
1950 mod_freesync_destroy(adev->dm.freesync_module);
1951 adev->dm.freesync_module = NULL;
1954 mutex_destroy(&adev->dm.audio_lock);
1955 mutex_destroy(&adev->dm.dc_lock);
1956 mutex_destroy(&adev->dm.dpia_aux_lock);
1959 static int load_dmcu_fw(struct amdgpu_device *adev)
1961 const char *fw_name_dmcu = NULL;
1963 const struct dmcu_firmware_header_v1_0 *hdr;
1965 switch (adev->asic_type) {
1966 #if defined(CONFIG_DRM_AMD_DC_SI)
1981 case CHIP_POLARIS11:
1982 case CHIP_POLARIS10:
1983 case CHIP_POLARIS12:
1990 fw_name_dmcu = FIRMWARE_NAVI12_DMCU;
1993 if (ASICREV_IS_PICASSO(adev->external_rev_id))
1994 fw_name_dmcu = FIRMWARE_RAVEN_DMCU;
1995 else if (ASICREV_IS_RAVEN2(adev->external_rev_id))
1996 fw_name_dmcu = FIRMWARE_RAVEN_DMCU;
2001 switch (adev->ip_versions[DCE_HWIP][0]) {
2002 case IP_VERSION(2, 0, 2):
2003 case IP_VERSION(2, 0, 3):
2004 case IP_VERSION(2, 0, 0):
2005 case IP_VERSION(2, 1, 0):
2006 case IP_VERSION(3, 0, 0):
2007 case IP_VERSION(3, 0, 2):
2008 case IP_VERSION(3, 0, 3):
2009 case IP_VERSION(3, 0, 1):
2010 case IP_VERSION(3, 1, 2):
2011 case IP_VERSION(3, 1, 3):
2012 case IP_VERSION(3, 1, 4):
2013 case IP_VERSION(3, 1, 5):
2014 case IP_VERSION(3, 1, 6):
2015 case IP_VERSION(3, 2, 0):
2016 case IP_VERSION(3, 2, 1):
2021 DRM_ERROR("Unsupported ASIC type: 0x%X\n", adev->asic_type);
2025 if (adev->firmware.load_type != AMDGPU_FW_LOAD_PSP) {
2026 DRM_DEBUG_KMS("dm: DMCU firmware not supported on direct or SMU loading\n");
2030 r = amdgpu_ucode_request(adev, &adev->dm.fw_dmcu, fw_name_dmcu);
2032 /* DMCU firmware is not necessary, so don't raise a fuss if it's missing */
2033 DRM_DEBUG_KMS("dm: DMCU firmware not found\n");
2034 adev->dm.fw_dmcu = NULL;
2038 dev_err(adev->dev, "amdgpu_dm: Can't validate firmware \"%s\"\n",
2040 amdgpu_ucode_release(&adev->dm.fw_dmcu);
2044 hdr = (const struct dmcu_firmware_header_v1_0 *)adev->dm.fw_dmcu->data;
2045 adev->firmware.ucode[AMDGPU_UCODE_ID_DMCU_ERAM].ucode_id = AMDGPU_UCODE_ID_DMCU_ERAM;
2046 adev->firmware.ucode[AMDGPU_UCODE_ID_DMCU_ERAM].fw = adev->dm.fw_dmcu;
2047 adev->firmware.fw_size +=
2048 ALIGN(le32_to_cpu(hdr->header.ucode_size_bytes) - le32_to_cpu(hdr->intv_size_bytes), PAGE_SIZE);
2050 adev->firmware.ucode[AMDGPU_UCODE_ID_DMCU_INTV].ucode_id = AMDGPU_UCODE_ID_DMCU_INTV;
2051 adev->firmware.ucode[AMDGPU_UCODE_ID_DMCU_INTV].fw = adev->dm.fw_dmcu;
2052 adev->firmware.fw_size +=
2053 ALIGN(le32_to_cpu(hdr->intv_size_bytes), PAGE_SIZE);
2055 adev->dm.dmcu_fw_version = le32_to_cpu(hdr->header.ucode_version);
2057 DRM_DEBUG_KMS("PSP loading DMCU firmware\n");
2062 static uint32_t amdgpu_dm_dmub_reg_read(void *ctx, uint32_t address)
2064 struct amdgpu_device *adev = ctx;
2066 return dm_read_reg(adev->dm.dc->ctx, address);
2069 static void amdgpu_dm_dmub_reg_write(void *ctx, uint32_t address,
2072 struct amdgpu_device *adev = ctx;
2074 return dm_write_reg(adev->dm.dc->ctx, address, value);
2077 static int dm_dmub_sw_init(struct amdgpu_device *adev)
2079 struct dmub_srv_create_params create_params;
2080 struct dmub_srv_region_params region_params;
2081 struct dmub_srv_region_info region_info;
2082 struct dmub_srv_fb_params fb_params;
2083 struct dmub_srv_fb_info *fb_info;
2084 struct dmub_srv *dmub_srv;
2085 const struct dmcub_firmware_header_v1_0 *hdr;
2086 enum dmub_asic dmub_asic;
2087 enum dmub_status status;
2090 switch (adev->ip_versions[DCE_HWIP][0]) {
2091 case IP_VERSION(2, 1, 0):
2092 dmub_asic = DMUB_ASIC_DCN21;
2094 case IP_VERSION(3, 0, 0):
2095 dmub_asic = DMUB_ASIC_DCN30;
2097 case IP_VERSION(3, 0, 1):
2098 dmub_asic = DMUB_ASIC_DCN301;
2100 case IP_VERSION(3, 0, 2):
2101 dmub_asic = DMUB_ASIC_DCN302;
2103 case IP_VERSION(3, 0, 3):
2104 dmub_asic = DMUB_ASIC_DCN303;
2106 case IP_VERSION(3, 1, 2):
2107 case IP_VERSION(3, 1, 3):
2108 dmub_asic = (adev->external_rev_id == YELLOW_CARP_B0) ? DMUB_ASIC_DCN31B : DMUB_ASIC_DCN31;
2110 case IP_VERSION(3, 1, 4):
2111 dmub_asic = DMUB_ASIC_DCN314;
2113 case IP_VERSION(3, 1, 5):
2114 dmub_asic = DMUB_ASIC_DCN315;
2116 case IP_VERSION(3, 1, 6):
2117 dmub_asic = DMUB_ASIC_DCN316;
2119 case IP_VERSION(3, 2, 0):
2120 dmub_asic = DMUB_ASIC_DCN32;
2122 case IP_VERSION(3, 2, 1):
2123 dmub_asic = DMUB_ASIC_DCN321;
2126 /* ASIC doesn't support DMUB. */
2130 hdr = (const struct dmcub_firmware_header_v1_0 *)adev->dm.dmub_fw->data;
2131 adev->dm.dmcub_fw_version = le32_to_cpu(hdr->header.ucode_version);
2133 if (adev->firmware.load_type == AMDGPU_FW_LOAD_PSP) {
2134 adev->firmware.ucode[AMDGPU_UCODE_ID_DMCUB].ucode_id =
2135 AMDGPU_UCODE_ID_DMCUB;
2136 adev->firmware.ucode[AMDGPU_UCODE_ID_DMCUB].fw =
2138 adev->firmware.fw_size +=
2139 ALIGN(le32_to_cpu(hdr->inst_const_bytes), PAGE_SIZE);
2141 DRM_INFO("Loading DMUB firmware via PSP: version=0x%08X\n",
2142 adev->dm.dmcub_fw_version);
2146 adev->dm.dmub_srv = kzalloc(sizeof(*adev->dm.dmub_srv), GFP_KERNEL);
2147 dmub_srv = adev->dm.dmub_srv;
2150 DRM_ERROR("Failed to allocate DMUB service!\n");
2154 memset(&create_params, 0, sizeof(create_params));
2155 create_params.user_ctx = adev;
2156 create_params.funcs.reg_read = amdgpu_dm_dmub_reg_read;
2157 create_params.funcs.reg_write = amdgpu_dm_dmub_reg_write;
2158 create_params.asic = dmub_asic;
2160 /* Create the DMUB service. */
2161 status = dmub_srv_create(dmub_srv, &create_params);
2162 if (status != DMUB_STATUS_OK) {
2163 DRM_ERROR("Error creating DMUB service: %d\n", status);
2167 /* Calculate the size of all the regions for the DMUB service. */
2168 memset(®ion_params, 0, sizeof(region_params));
2170 region_params.inst_const_size = le32_to_cpu(hdr->inst_const_bytes) -
2171 PSP_HEADER_BYTES - PSP_FOOTER_BYTES;
2172 region_params.bss_data_size = le32_to_cpu(hdr->bss_data_bytes);
2173 region_params.vbios_size = adev->bios_size;
2174 region_params.fw_bss_data = region_params.bss_data_size ?
2175 adev->dm.dmub_fw->data +
2176 le32_to_cpu(hdr->header.ucode_array_offset_bytes) +
2177 le32_to_cpu(hdr->inst_const_bytes) : NULL;
2178 region_params.fw_inst_const =
2179 adev->dm.dmub_fw->data +
2180 le32_to_cpu(hdr->header.ucode_array_offset_bytes) +
2183 status = dmub_srv_calc_region_info(dmub_srv, ®ion_params,
2186 if (status != DMUB_STATUS_OK) {
2187 DRM_ERROR("Error calculating DMUB region info: %d\n", status);
2192 * Allocate a framebuffer based on the total size of all the regions.
2193 * TODO: Move this into GART.
2195 r = amdgpu_bo_create_kernel(adev, region_info.fb_size, PAGE_SIZE,
2196 AMDGPU_GEM_DOMAIN_VRAM |
2197 AMDGPU_GEM_DOMAIN_GTT,
2199 &adev->dm.dmub_bo_gpu_addr,
2200 &adev->dm.dmub_bo_cpu_addr);
2204 /* Rebase the regions on the framebuffer address. */
2205 memset(&fb_params, 0, sizeof(fb_params));
2206 fb_params.cpu_addr = adev->dm.dmub_bo_cpu_addr;
2207 fb_params.gpu_addr = adev->dm.dmub_bo_gpu_addr;
2208 fb_params.region_info = ®ion_info;
2210 adev->dm.dmub_fb_info =
2211 kzalloc(sizeof(*adev->dm.dmub_fb_info), GFP_KERNEL);
2212 fb_info = adev->dm.dmub_fb_info;
2216 "Failed to allocate framebuffer info for DMUB service!\n");
2220 status = dmub_srv_calc_fb_info(dmub_srv, &fb_params, fb_info);
2221 if (status != DMUB_STATUS_OK) {
2222 DRM_ERROR("Error calculating DMUB FB info: %d\n", status);
2229 static int dm_sw_init(void *handle)
2231 struct amdgpu_device *adev = (struct amdgpu_device *)handle;
2234 r = dm_dmub_sw_init(adev);
2238 return load_dmcu_fw(adev);
2241 static int dm_sw_fini(void *handle)
2243 struct amdgpu_device *adev = (struct amdgpu_device *)handle;
2245 kfree(adev->dm.dmub_fb_info);
2246 adev->dm.dmub_fb_info = NULL;
2248 if (adev->dm.dmub_srv) {
2249 dmub_srv_destroy(adev->dm.dmub_srv);
2250 adev->dm.dmub_srv = NULL;
2253 amdgpu_ucode_release(&adev->dm.dmub_fw);
2254 amdgpu_ucode_release(&adev->dm.fw_dmcu);
2259 static int detect_mst_link_for_all_connectors(struct drm_device *dev)
2261 struct amdgpu_dm_connector *aconnector;
2262 struct drm_connector *connector;
2263 struct drm_connector_list_iter iter;
2266 drm_connector_list_iter_begin(dev, &iter);
2267 drm_for_each_connector_iter(connector, &iter) {
2268 aconnector = to_amdgpu_dm_connector(connector);
2269 if (aconnector->dc_link->type == dc_connection_mst_branch &&
2270 aconnector->mst_mgr.aux) {
2271 DRM_DEBUG_DRIVER("DM_MST: starting TM on aconnector: %p [id: %d]\n",
2273 aconnector->base.base.id);
2275 ret = drm_dp_mst_topology_mgr_set_mst(&aconnector->mst_mgr, true);
2277 DRM_ERROR("DM_MST: Failed to start MST\n");
2278 aconnector->dc_link->type =
2279 dc_connection_single;
2280 ret = dm_helpers_dp_mst_stop_top_mgr(aconnector->dc_link->ctx,
2281 aconnector->dc_link);
2286 drm_connector_list_iter_end(&iter);
2291 static int dm_late_init(void *handle)
2293 struct amdgpu_device *adev = (struct amdgpu_device *)handle;
2295 struct dmcu_iram_parameters params;
2296 unsigned int linear_lut[16];
2298 struct dmcu *dmcu = NULL;
2300 dmcu = adev->dm.dc->res_pool->dmcu;
2302 for (i = 0; i < 16; i++)
2303 linear_lut[i] = 0xFFFF * i / 15;
2306 params.backlight_ramping_override = false;
2307 params.backlight_ramping_start = 0xCCCC;
2308 params.backlight_ramping_reduction = 0xCCCCCCCC;
2309 params.backlight_lut_array_size = 16;
2310 params.backlight_lut_array = linear_lut;
2312 /* Min backlight level after ABM reduction, Don't allow below 1%
2313 * 0xFFFF x 0.01 = 0x28F
2315 params.min_abm_backlight = 0x28F;
2316 /* In the case where abm is implemented on dmcub,
2317 * dmcu object will be null.
2318 * ABM 2.4 and up are implemented on dmcub.
2321 if (!dmcu_load_iram(dmcu, params))
2323 } else if (adev->dm.dc->ctx->dmub_srv) {
2324 struct dc_link *edp_links[MAX_NUM_EDP];
2327 dc_get_edp_links(adev->dm.dc, edp_links, &edp_num);
2328 for (i = 0; i < edp_num; i++) {
2329 if (!dmub_init_abm_config(adev->dm.dc->res_pool, params, i))
2334 return detect_mst_link_for_all_connectors(adev_to_drm(adev));
2337 static void s3_handle_mst(struct drm_device *dev, bool suspend)
2339 struct amdgpu_dm_connector *aconnector;
2340 struct drm_connector *connector;
2341 struct drm_connector_list_iter iter;
2342 struct drm_dp_mst_topology_mgr *mgr;
2344 bool need_hotplug = false;
2346 drm_connector_list_iter_begin(dev, &iter);
2347 drm_for_each_connector_iter(connector, &iter) {
2348 aconnector = to_amdgpu_dm_connector(connector);
2349 if (aconnector->dc_link->type != dc_connection_mst_branch ||
2350 aconnector->mst_root)
2353 mgr = &aconnector->mst_mgr;
2356 drm_dp_mst_topology_mgr_suspend(mgr);
2358 /* if extended timeout is supported in hardware,
2359 * default to LTTPR timeout (3.2ms) first as a W/A for DP link layer
2360 * CTS 4.2.1.1 regression introduced by CTS specs requirement update.
2362 try_to_configure_aux_timeout(aconnector->dc_link->ddc, LINK_AUX_DEFAULT_LTTPR_TIMEOUT_PERIOD);
2363 if (!dp_is_lttpr_present(aconnector->dc_link))
2364 try_to_configure_aux_timeout(aconnector->dc_link->ddc, LINK_AUX_DEFAULT_TIMEOUT_PERIOD);
2366 ret = drm_dp_mst_topology_mgr_resume(mgr, true);
2368 dm_helpers_dp_mst_stop_top_mgr(aconnector->dc_link->ctx,
2369 aconnector->dc_link);
2370 need_hotplug = true;
2374 drm_connector_list_iter_end(&iter);
2377 drm_kms_helper_hotplug_event(dev);
2380 static int amdgpu_dm_smu_write_watermarks_table(struct amdgpu_device *adev)
2384 /* This interface is for dGPU Navi1x.Linux dc-pplib interface depends
2385 * on window driver dc implementation.
2386 * For Navi1x, clock settings of dcn watermarks are fixed. the settings
2387 * should be passed to smu during boot up and resume from s3.
2388 * boot up: dc calculate dcn watermark clock settings within dc_create,
2389 * dcn20_resource_construct
2390 * then call pplib functions below to pass the settings to smu:
2391 * smu_set_watermarks_for_clock_ranges
2392 * smu_set_watermarks_table
2393 * navi10_set_watermarks_table
2394 * smu_write_watermarks_table
2396 * For Renoir, clock settings of dcn watermark are also fixed values.
2397 * dc has implemented different flow for window driver:
2398 * dc_hardware_init / dc_set_power_state
2403 * smu_set_watermarks_for_clock_ranges
2404 * renoir_set_watermarks_table
2405 * smu_write_watermarks_table
2408 * dc_hardware_init -> amdgpu_dm_init
2409 * dc_set_power_state --> dm_resume
2411 * therefore, this function apply to navi10/12/14 but not Renoir
2414 switch (adev->ip_versions[DCE_HWIP][0]) {
2415 case IP_VERSION(2, 0, 2):
2416 case IP_VERSION(2, 0, 0):
2422 ret = amdgpu_dpm_write_watermarks_table(adev);
2424 DRM_ERROR("Failed to update WMTABLE!\n");
2432 * dm_hw_init() - Initialize DC device
2433 * @handle: The base driver device containing the amdgpu_dm device.
2435 * Initialize the &struct amdgpu_display_manager device. This involves calling
2436 * the initializers of each DM component, then populating the struct with them.
2438 * Although the function implies hardware initialization, both hardware and
2439 * software are initialized here. Splitting them out to their relevant init
2440 * hooks is a future TODO item.
2442 * Some notable things that are initialized here:
2444 * - Display Core, both software and hardware
2445 * - DC modules that we need (freesync and color management)
2446 * - DRM software states
2447 * - Interrupt sources and handlers
2449 * - Debug FS entries, if enabled
2451 static int dm_hw_init(void *handle)
2453 struct amdgpu_device *adev = (struct amdgpu_device *)handle;
2454 /* Create DAL display manager */
2455 amdgpu_dm_init(adev);
2456 amdgpu_dm_hpd_init(adev);
2462 * dm_hw_fini() - Teardown DC device
2463 * @handle: The base driver device containing the amdgpu_dm device.
2465 * Teardown components within &struct amdgpu_display_manager that require
2466 * cleanup. This involves cleaning up the DRM device, DC, and any modules that
2467 * were loaded. Also flush IRQ workqueues and disable them.
2469 static int dm_hw_fini(void *handle)
2471 struct amdgpu_device *adev = (struct amdgpu_device *)handle;
2473 amdgpu_dm_hpd_fini(adev);
2475 amdgpu_dm_irq_fini(adev);
2476 amdgpu_dm_fini(adev);
2481 static void dm_gpureset_toggle_interrupts(struct amdgpu_device *adev,
2482 struct dc_state *state, bool enable)
2484 enum dc_irq_source irq_source;
2485 struct amdgpu_crtc *acrtc;
2489 for (i = 0; i < state->stream_count; i++) {
2490 acrtc = get_crtc_by_otg_inst(
2491 adev, state->stream_status[i].primary_otg_inst);
2493 if (acrtc && state->stream_status[i].plane_count != 0) {
2494 irq_source = IRQ_TYPE_PFLIP + acrtc->otg_inst;
2495 rc = dc_interrupt_set(adev->dm.dc, irq_source, enable) ? 0 : -EBUSY;
2497 DRM_WARN("Failed to %s pflip interrupts\n",
2498 enable ? "enable" : "disable");
2501 if (amdgpu_dm_crtc_vrr_active(to_dm_crtc_state(acrtc->base.state)))
2502 rc = amdgpu_dm_crtc_set_vupdate_irq(&acrtc->base, true);
2504 rc = amdgpu_dm_crtc_set_vupdate_irq(&acrtc->base, false);
2507 DRM_WARN("Failed to %sable vupdate interrupt\n", enable ? "en" : "dis");
2509 irq_source = IRQ_TYPE_VBLANK + acrtc->otg_inst;
2510 /* During gpu-reset we disable and then enable vblank irq, so
2511 * don't use amdgpu_irq_get/put() to avoid refcount change.
2513 if (!dc_interrupt_set(adev->dm.dc, irq_source, enable))
2514 DRM_WARN("Failed to %sable vblank interrupt\n", enable ? "en" : "dis");
2520 static enum dc_status amdgpu_dm_commit_zero_streams(struct dc *dc)
2522 struct dc_state *context = NULL;
2523 enum dc_status res = DC_ERROR_UNEXPECTED;
2525 struct dc_stream_state *del_streams[MAX_PIPES];
2526 int del_streams_count = 0;
2528 memset(del_streams, 0, sizeof(del_streams));
2530 context = dc_create_state(dc);
2531 if (context == NULL)
2532 goto context_alloc_fail;
2534 dc_resource_state_copy_construct_current(dc, context);
2536 /* First remove from context all streams */
2537 for (i = 0; i < context->stream_count; i++) {
2538 struct dc_stream_state *stream = context->streams[i];
2540 del_streams[del_streams_count++] = stream;
2543 /* Remove all planes for removed streams and then remove the streams */
2544 for (i = 0; i < del_streams_count; i++) {
2545 if (!dc_rem_all_planes_for_stream(dc, del_streams[i], context)) {
2546 res = DC_FAIL_DETACH_SURFACES;
2550 res = dc_remove_stream_from_ctx(dc, context, del_streams[i]);
2555 res = dc_commit_streams(dc, context->streams, context->stream_count);
2558 dc_release_state(context);
2564 static void hpd_rx_irq_work_suspend(struct amdgpu_display_manager *dm)
2568 if (dm->hpd_rx_offload_wq) {
2569 for (i = 0; i < dm->dc->caps.max_links; i++)
2570 flush_workqueue(dm->hpd_rx_offload_wq[i].wq);
2574 static int dm_suspend(void *handle)
2576 struct amdgpu_device *adev = handle;
2577 struct amdgpu_display_manager *dm = &adev->dm;
2580 if (amdgpu_in_reset(adev)) {
2581 mutex_lock(&dm->dc_lock);
2583 dc_allow_idle_optimizations(adev->dm.dc, false);
2585 dm->cached_dc_state = dc_copy_state(dm->dc->current_state);
2587 dm_gpureset_toggle_interrupts(adev, dm->cached_dc_state, false);
2589 amdgpu_dm_commit_zero_streams(dm->dc);
2591 amdgpu_dm_irq_suspend(adev);
2593 hpd_rx_irq_work_suspend(dm);
2598 WARN_ON(adev->dm.cached_state);
2599 adev->dm.cached_state = drm_atomic_helper_suspend(adev_to_drm(adev));
2601 s3_handle_mst(adev_to_drm(adev), true);
2603 amdgpu_dm_irq_suspend(adev);
2605 hpd_rx_irq_work_suspend(dm);
2607 dc_set_power_state(dm->dc, DC_ACPI_CM_POWER_STATE_D3);
2612 struct amdgpu_dm_connector *
2613 amdgpu_dm_find_first_crtc_matching_connector(struct drm_atomic_state *state,
2614 struct drm_crtc *crtc)
2617 struct drm_connector_state *new_con_state;
2618 struct drm_connector *connector;
2619 struct drm_crtc *crtc_from_state;
2621 for_each_new_connector_in_state(state, connector, new_con_state, i) {
2622 crtc_from_state = new_con_state->crtc;
2624 if (crtc_from_state == crtc)
2625 return to_amdgpu_dm_connector(connector);
2631 static void emulated_link_detect(struct dc_link *link)
2633 struct dc_sink_init_data sink_init_data = { 0 };
2634 struct display_sink_capability sink_caps = { 0 };
2635 enum dc_edid_status edid_status;
2636 struct dc_context *dc_ctx = link->ctx;
2637 struct dc_sink *sink = NULL;
2638 struct dc_sink *prev_sink = NULL;
2640 link->type = dc_connection_none;
2641 prev_sink = link->local_sink;
2644 dc_sink_release(prev_sink);
2646 switch (link->connector_signal) {
2647 case SIGNAL_TYPE_HDMI_TYPE_A: {
2648 sink_caps.transaction_type = DDC_TRANSACTION_TYPE_I2C;
2649 sink_caps.signal = SIGNAL_TYPE_HDMI_TYPE_A;
2653 case SIGNAL_TYPE_DVI_SINGLE_LINK: {
2654 sink_caps.transaction_type = DDC_TRANSACTION_TYPE_I2C;
2655 sink_caps.signal = SIGNAL_TYPE_DVI_SINGLE_LINK;
2659 case SIGNAL_TYPE_DVI_DUAL_LINK: {
2660 sink_caps.transaction_type = DDC_TRANSACTION_TYPE_I2C;
2661 sink_caps.signal = SIGNAL_TYPE_DVI_DUAL_LINK;
2665 case SIGNAL_TYPE_LVDS: {
2666 sink_caps.transaction_type = DDC_TRANSACTION_TYPE_I2C;
2667 sink_caps.signal = SIGNAL_TYPE_LVDS;
2671 case SIGNAL_TYPE_EDP: {
2672 sink_caps.transaction_type =
2673 DDC_TRANSACTION_TYPE_I2C_OVER_AUX;
2674 sink_caps.signal = SIGNAL_TYPE_EDP;
2678 case SIGNAL_TYPE_DISPLAY_PORT: {
2679 sink_caps.transaction_type =
2680 DDC_TRANSACTION_TYPE_I2C_OVER_AUX;
2681 sink_caps.signal = SIGNAL_TYPE_VIRTUAL;
2686 DC_ERROR("Invalid connector type! signal:%d\n",
2687 link->connector_signal);
2691 sink_init_data.link = link;
2692 sink_init_data.sink_signal = sink_caps.signal;
2694 sink = dc_sink_create(&sink_init_data);
2696 DC_ERROR("Failed to create sink!\n");
2700 /* dc_sink_create returns a new reference */
2701 link->local_sink = sink;
2703 edid_status = dm_helpers_read_local_edid(
2708 if (edid_status != EDID_OK)
2709 DC_ERROR("Failed to read EDID");
2713 static void dm_gpureset_commit_state(struct dc_state *dc_state,
2714 struct amdgpu_display_manager *dm)
2717 struct dc_surface_update surface_updates[MAX_SURFACES];
2718 struct dc_plane_info plane_infos[MAX_SURFACES];
2719 struct dc_scaling_info scaling_infos[MAX_SURFACES];
2720 struct dc_flip_addrs flip_addrs[MAX_SURFACES];
2721 struct dc_stream_update stream_update;
2725 bundle = kzalloc(sizeof(*bundle), GFP_KERNEL);
2728 dm_error("Failed to allocate update bundle\n");
2732 for (k = 0; k < dc_state->stream_count; k++) {
2733 bundle->stream_update.stream = dc_state->streams[k];
2735 for (m = 0; m < dc_state->stream_status->plane_count; m++) {
2736 bundle->surface_updates[m].surface =
2737 dc_state->stream_status->plane_states[m];
2738 bundle->surface_updates[m].surface->force_full_update =
2742 update_planes_and_stream_adapter(dm->dc,
2744 dc_state->stream_status->plane_count,
2745 dc_state->streams[k],
2746 &bundle->stream_update,
2747 bundle->surface_updates);
2754 static int dm_resume(void *handle)
2756 struct amdgpu_device *adev = handle;
2757 struct drm_device *ddev = adev_to_drm(adev);
2758 struct amdgpu_display_manager *dm = &adev->dm;
2759 struct amdgpu_dm_connector *aconnector;
2760 struct drm_connector *connector;
2761 struct drm_connector_list_iter iter;
2762 struct drm_crtc *crtc;
2763 struct drm_crtc_state *new_crtc_state;
2764 struct dm_crtc_state *dm_new_crtc_state;
2765 struct drm_plane *plane;
2766 struct drm_plane_state *new_plane_state;
2767 struct dm_plane_state *dm_new_plane_state;
2768 struct dm_atomic_state *dm_state = to_dm_atomic_state(dm->atomic_obj.state);
2769 enum dc_connection_type new_connection_type = dc_connection_none;
2770 struct dc_state *dc_state;
2773 if (amdgpu_in_reset(adev)) {
2774 dc_state = dm->cached_dc_state;
2777 * The dc->current_state is backed up into dm->cached_dc_state
2778 * before we commit 0 streams.
2780 * DC will clear link encoder assignments on the real state
2781 * but the changes won't propagate over to the copy we made
2782 * before the 0 streams commit.
2784 * DC expects that link encoder assignments are *not* valid
2785 * when committing a state, so as a workaround we can copy
2786 * off of the current state.
2788 * We lose the previous assignments, but we had already
2789 * commit 0 streams anyway.
2791 link_enc_cfg_copy(adev->dm.dc->current_state, dc_state);
2793 r = dm_dmub_hw_init(adev);
2795 DRM_ERROR("DMUB interface failed to initialize: status=%d\n", r);
2797 dc_set_power_state(dm->dc, DC_ACPI_CM_POWER_STATE_D0);
2800 amdgpu_dm_irq_resume_early(adev);
2802 for (i = 0; i < dc_state->stream_count; i++) {
2803 dc_state->streams[i]->mode_changed = true;
2804 for (j = 0; j < dc_state->stream_status[i].plane_count; j++) {
2805 dc_state->stream_status[i].plane_states[j]->update_flags.raw
2810 if (dc_is_dmub_outbox_supported(adev->dm.dc)) {
2811 amdgpu_dm_outbox_init(adev);
2812 dc_enable_dmub_outbox(adev->dm.dc);
2815 WARN_ON(!dc_commit_streams(dm->dc, dc_state->streams, dc_state->stream_count));
2817 dm_gpureset_commit_state(dm->cached_dc_state, dm);
2819 dm_gpureset_toggle_interrupts(adev, dm->cached_dc_state, true);
2821 dc_release_state(dm->cached_dc_state);
2822 dm->cached_dc_state = NULL;
2824 amdgpu_dm_irq_resume_late(adev);
2826 mutex_unlock(&dm->dc_lock);
2830 /* Recreate dc_state - DC invalidates it when setting power state to S3. */
2831 dc_release_state(dm_state->context);
2832 dm_state->context = dc_create_state(dm->dc);
2833 /* TODO: Remove dc_state->dccg, use dc->dccg directly. */
2834 dc_resource_state_construct(dm->dc, dm_state->context);
2836 /* Before powering on DC we need to re-initialize DMUB. */
2837 dm_dmub_hw_resume(adev);
2839 /* Re-enable outbox interrupts for DPIA. */
2840 if (dc_is_dmub_outbox_supported(adev->dm.dc)) {
2841 amdgpu_dm_outbox_init(adev);
2842 dc_enable_dmub_outbox(adev->dm.dc);
2845 /* power on hardware */
2846 dc_set_power_state(dm->dc, DC_ACPI_CM_POWER_STATE_D0);
2848 /* program HPD filter */
2852 * early enable HPD Rx IRQ, should be done before set mode as short
2853 * pulse interrupts are used for MST
2855 amdgpu_dm_irq_resume_early(adev);
2857 /* On resume we need to rewrite the MSTM control bits to enable MST*/
2858 s3_handle_mst(ddev, false);
2861 drm_connector_list_iter_begin(ddev, &iter);
2862 drm_for_each_connector_iter(connector, &iter) {
2863 aconnector = to_amdgpu_dm_connector(connector);
2865 if (!aconnector->dc_link)
2869 * this is the case when traversing through already created
2870 * MST connectors, should be skipped
2872 if (aconnector && aconnector->mst_root)
2875 mutex_lock(&aconnector->hpd_lock);
2876 if (!dc_link_detect_connection_type(aconnector->dc_link, &new_connection_type))
2877 DRM_ERROR("KMS: Failed to detect connector\n");
2879 if (aconnector->base.force && new_connection_type == dc_connection_none) {
2880 emulated_link_detect(aconnector->dc_link);
2882 mutex_lock(&dm->dc_lock);
2883 dc_link_detect(aconnector->dc_link, DETECT_REASON_HPD);
2884 mutex_unlock(&dm->dc_lock);
2887 if (aconnector->fake_enable && aconnector->dc_link->local_sink)
2888 aconnector->fake_enable = false;
2890 if (aconnector->dc_sink)
2891 dc_sink_release(aconnector->dc_sink);
2892 aconnector->dc_sink = NULL;
2893 amdgpu_dm_update_connector_after_detect(aconnector);
2894 mutex_unlock(&aconnector->hpd_lock);
2896 drm_connector_list_iter_end(&iter);
2898 /* Force mode set in atomic commit */
2899 for_each_new_crtc_in_state(dm->cached_state, crtc, new_crtc_state, i)
2900 new_crtc_state->active_changed = true;
2903 * atomic_check is expected to create the dc states. We need to release
2904 * them here, since they were duplicated as part of the suspend
2907 for_each_new_crtc_in_state(dm->cached_state, crtc, new_crtc_state, i) {
2908 dm_new_crtc_state = to_dm_crtc_state(new_crtc_state);
2909 if (dm_new_crtc_state->stream) {
2910 WARN_ON(kref_read(&dm_new_crtc_state->stream->refcount) > 1);
2911 dc_stream_release(dm_new_crtc_state->stream);
2912 dm_new_crtc_state->stream = NULL;
2916 for_each_new_plane_in_state(dm->cached_state, plane, new_plane_state, i) {
2917 dm_new_plane_state = to_dm_plane_state(new_plane_state);
2918 if (dm_new_plane_state->dc_state) {
2919 WARN_ON(kref_read(&dm_new_plane_state->dc_state->refcount) > 1);
2920 dc_plane_state_release(dm_new_plane_state->dc_state);
2921 dm_new_plane_state->dc_state = NULL;
2925 drm_atomic_helper_resume(ddev, dm->cached_state);
2927 dm->cached_state = NULL;
2929 amdgpu_dm_irq_resume_late(adev);
2931 amdgpu_dm_smu_write_watermarks_table(adev);
2939 * DM (and consequently DC) is registered in the amdgpu base driver as a IP
2940 * block. When CONFIG_DRM_AMD_DC is enabled, the DM device IP block is added to
2941 * the base driver's device list to be initialized and torn down accordingly.
2943 * The functions to do so are provided as hooks in &struct amd_ip_funcs.
2946 static const struct amd_ip_funcs amdgpu_dm_funcs = {
2948 .early_init = dm_early_init,
2949 .late_init = dm_late_init,
2950 .sw_init = dm_sw_init,
2951 .sw_fini = dm_sw_fini,
2952 .early_fini = amdgpu_dm_early_fini,
2953 .hw_init = dm_hw_init,
2954 .hw_fini = dm_hw_fini,
2955 .suspend = dm_suspend,
2956 .resume = dm_resume,
2957 .is_idle = dm_is_idle,
2958 .wait_for_idle = dm_wait_for_idle,
2959 .check_soft_reset = dm_check_soft_reset,
2960 .soft_reset = dm_soft_reset,
2961 .set_clockgating_state = dm_set_clockgating_state,
2962 .set_powergating_state = dm_set_powergating_state,
2965 const struct amdgpu_ip_block_version dm_ip_block = {
2966 .type = AMD_IP_BLOCK_TYPE_DCE,
2970 .funcs = &amdgpu_dm_funcs,
2980 static const struct drm_mode_config_funcs amdgpu_dm_mode_funcs = {
2981 .fb_create = amdgpu_display_user_framebuffer_create,
2982 .get_format_info = amdgpu_dm_plane_get_format_info,
2983 .atomic_check = amdgpu_dm_atomic_check,
2984 .atomic_commit = drm_atomic_helper_commit,
2987 static struct drm_mode_config_helper_funcs amdgpu_dm_mode_config_helperfuncs = {
2988 .atomic_commit_tail = amdgpu_dm_atomic_commit_tail,
2989 .atomic_commit_setup = drm_dp_mst_atomic_setup_commit,
2992 static void update_connector_ext_caps(struct amdgpu_dm_connector *aconnector)
2994 struct amdgpu_dm_backlight_caps *caps;
2995 struct drm_connector *conn_base;
2996 struct amdgpu_device *adev;
2997 struct drm_luminance_range_info *luminance_range;
2999 if (aconnector->bl_idx == -1 ||
3000 aconnector->dc_link->connector_signal != SIGNAL_TYPE_EDP)
3003 conn_base = &aconnector->base;
3004 adev = drm_to_adev(conn_base->dev);
3006 caps = &adev->dm.backlight_caps[aconnector->bl_idx];
3007 caps->ext_caps = &aconnector->dc_link->dpcd_sink_ext_caps;
3008 caps->aux_support = false;
3010 if (caps->ext_caps->bits.oled == 1
3013 * caps->ext_caps->bits.sdr_aux_backlight_control == 1 ||
3014 * caps->ext_caps->bits.hdr_aux_backlight_control == 1
3016 caps->aux_support = true;
3018 if (amdgpu_backlight == 0)
3019 caps->aux_support = false;
3020 else if (amdgpu_backlight == 1)
3021 caps->aux_support = true;
3023 luminance_range = &conn_base->display_info.luminance_range;
3025 if (luminance_range->max_luminance) {
3026 caps->aux_min_input_signal = luminance_range->min_luminance;
3027 caps->aux_max_input_signal = luminance_range->max_luminance;
3029 caps->aux_min_input_signal = 0;
3030 caps->aux_max_input_signal = 512;
3034 void amdgpu_dm_update_connector_after_detect(
3035 struct amdgpu_dm_connector *aconnector)
3037 struct drm_connector *connector = &aconnector->base;
3038 struct drm_device *dev = connector->dev;
3039 struct dc_sink *sink;
3041 /* MST handled by drm_mst framework */
3042 if (aconnector->mst_mgr.mst_state == true)
3045 sink = aconnector->dc_link->local_sink;
3047 dc_sink_retain(sink);
3050 * Edid mgmt connector gets first update only in mode_valid hook and then
3051 * the connector sink is set to either fake or physical sink depends on link status.
3052 * Skip if already done during boot.
3054 if (aconnector->base.force != DRM_FORCE_UNSPECIFIED
3055 && aconnector->dc_em_sink) {
3058 * For S3 resume with headless use eml_sink to fake stream
3059 * because on resume connector->sink is set to NULL
3061 mutex_lock(&dev->mode_config.mutex);
3064 if (aconnector->dc_sink) {
3065 amdgpu_dm_update_freesync_caps(connector, NULL);
3067 * retain and release below are used to
3068 * bump up refcount for sink because the link doesn't point
3069 * to it anymore after disconnect, so on next crtc to connector
3070 * reshuffle by UMD we will get into unwanted dc_sink release
3072 dc_sink_release(aconnector->dc_sink);
3074 aconnector->dc_sink = sink;
3075 dc_sink_retain(aconnector->dc_sink);
3076 amdgpu_dm_update_freesync_caps(connector,
3079 amdgpu_dm_update_freesync_caps(connector, NULL);
3080 if (!aconnector->dc_sink) {
3081 aconnector->dc_sink = aconnector->dc_em_sink;
3082 dc_sink_retain(aconnector->dc_sink);
3086 mutex_unlock(&dev->mode_config.mutex);
3089 dc_sink_release(sink);
3094 * TODO: temporary guard to look for proper fix
3095 * if this sink is MST sink, we should not do anything
3097 if (sink && sink->sink_signal == SIGNAL_TYPE_DISPLAY_PORT_MST) {
3098 dc_sink_release(sink);
3102 if (aconnector->dc_sink == sink) {
3104 * We got a DP short pulse (Link Loss, DP CTS, etc...).
3107 DRM_DEBUG_DRIVER("DCHPD: connector_id=%d: dc_sink didn't change.\n",
3108 aconnector->connector_id);
3110 dc_sink_release(sink);
3114 DRM_DEBUG_DRIVER("DCHPD: connector_id=%d: Old sink=%p New sink=%p\n",
3115 aconnector->connector_id, aconnector->dc_sink, sink);
3117 mutex_lock(&dev->mode_config.mutex);
3120 * 1. Update status of the drm connector
3121 * 2. Send an event and let userspace tell us what to do
3125 * TODO: check if we still need the S3 mode update workaround.
3126 * If yes, put it here.
3128 if (aconnector->dc_sink) {
3129 amdgpu_dm_update_freesync_caps(connector, NULL);
3130 dc_sink_release(aconnector->dc_sink);
3133 aconnector->dc_sink = sink;
3134 dc_sink_retain(aconnector->dc_sink);
3135 if (sink->dc_edid.length == 0) {
3136 aconnector->edid = NULL;
3137 if (aconnector->dc_link->aux_mode) {
3138 drm_dp_cec_unset_edid(
3139 &aconnector->dm_dp_aux.aux);
3143 (struct edid *)sink->dc_edid.raw_edid;
3145 if (aconnector->dc_link->aux_mode)
3146 drm_dp_cec_set_edid(&aconnector->dm_dp_aux.aux,
3150 if (!aconnector->timing_requested) {
3151 aconnector->timing_requested =
3152 kzalloc(sizeof(struct dc_crtc_timing), GFP_KERNEL);
3153 if (!aconnector->timing_requested)
3154 dm_error("failed to create aconnector->requested_timing\n");
3157 drm_connector_update_edid_property(connector, aconnector->edid);
3158 amdgpu_dm_update_freesync_caps(connector, aconnector->edid);
3159 update_connector_ext_caps(aconnector);
3161 drm_dp_cec_unset_edid(&aconnector->dm_dp_aux.aux);
3162 amdgpu_dm_update_freesync_caps(connector, NULL);
3163 drm_connector_update_edid_property(connector, NULL);
3164 aconnector->num_modes = 0;
3165 dc_sink_release(aconnector->dc_sink);
3166 aconnector->dc_sink = NULL;
3167 aconnector->edid = NULL;
3168 kfree(aconnector->timing_requested);
3169 aconnector->timing_requested = NULL;
3170 /* Set CP to DESIRED if it was ENABLED, so we can re-enable it again on hotplug */
3171 if (connector->state->content_protection == DRM_MODE_CONTENT_PROTECTION_ENABLED)
3172 connector->state->content_protection = DRM_MODE_CONTENT_PROTECTION_DESIRED;
3175 mutex_unlock(&dev->mode_config.mutex);
3177 update_subconnector_property(aconnector);
3180 dc_sink_release(sink);
3183 static void handle_hpd_irq_helper(struct amdgpu_dm_connector *aconnector)
3185 struct drm_connector *connector = &aconnector->base;
3186 struct drm_device *dev = connector->dev;
3187 enum dc_connection_type new_connection_type = dc_connection_none;
3188 struct amdgpu_device *adev = drm_to_adev(dev);
3189 struct dm_connector_state *dm_con_state = to_dm_connector_state(connector->state);
3192 if (adev->dm.disable_hpd_irq)
3196 * In case of failure or MST no need to update connector status or notify the OS
3197 * since (for MST case) MST does this in its own context.
3199 mutex_lock(&aconnector->hpd_lock);
3201 if (adev->dm.hdcp_workqueue) {
3202 hdcp_reset_display(adev->dm.hdcp_workqueue, aconnector->dc_link->link_index);
3203 dm_con_state->update_hdcp = true;
3205 if (aconnector->fake_enable)
3206 aconnector->fake_enable = false;
3208 aconnector->timing_changed = false;
3210 if (!dc_link_detect_connection_type(aconnector->dc_link, &new_connection_type))
3211 DRM_ERROR("KMS: Failed to detect connector\n");
3213 if (aconnector->base.force && new_connection_type == dc_connection_none) {
3214 emulated_link_detect(aconnector->dc_link);
3216 drm_modeset_lock_all(dev);
3217 dm_restore_drm_connector_state(dev, connector);
3218 drm_modeset_unlock_all(dev);
3220 if (aconnector->base.force == DRM_FORCE_UNSPECIFIED)
3221 drm_kms_helper_connector_hotplug_event(connector);
3223 mutex_lock(&adev->dm.dc_lock);
3224 ret = dc_link_detect(aconnector->dc_link, DETECT_REASON_HPD);
3225 mutex_unlock(&adev->dm.dc_lock);
3227 amdgpu_dm_update_connector_after_detect(aconnector);
3229 drm_modeset_lock_all(dev);
3230 dm_restore_drm_connector_state(dev, connector);
3231 drm_modeset_unlock_all(dev);
3233 if (aconnector->base.force == DRM_FORCE_UNSPECIFIED)
3234 drm_kms_helper_connector_hotplug_event(connector);
3237 mutex_unlock(&aconnector->hpd_lock);
3241 static void handle_hpd_irq(void *param)
3243 struct amdgpu_dm_connector *aconnector = (struct amdgpu_dm_connector *)param;
3245 handle_hpd_irq_helper(aconnector);
3249 static void schedule_hpd_rx_offload_work(struct hpd_rx_irq_offload_work_queue *offload_wq,
3250 union hpd_irq_data hpd_irq_data)
3252 struct hpd_rx_irq_offload_work *offload_work =
3253 kzalloc(sizeof(*offload_work), GFP_KERNEL);
3255 if (!offload_work) {
3256 DRM_ERROR("Failed to allocate hpd_rx_irq_offload_work.\n");
3260 INIT_WORK(&offload_work->work, dm_handle_hpd_rx_offload_work);
3261 offload_work->data = hpd_irq_data;
3262 offload_work->offload_wq = offload_wq;
3264 queue_work(offload_wq->wq, &offload_work->work);
3265 DRM_DEBUG_KMS("queue work to handle hpd_rx offload work");
3268 static void handle_hpd_rx_irq(void *param)
3270 struct amdgpu_dm_connector *aconnector = (struct amdgpu_dm_connector *)param;
3271 struct drm_connector *connector = &aconnector->base;
3272 struct drm_device *dev = connector->dev;
3273 struct dc_link *dc_link = aconnector->dc_link;
3274 bool is_mst_root_connector = aconnector->mst_mgr.mst_state;
3275 bool result = false;
3276 enum dc_connection_type new_connection_type = dc_connection_none;
3277 struct amdgpu_device *adev = drm_to_adev(dev);
3278 union hpd_irq_data hpd_irq_data;
3279 bool link_loss = false;
3280 bool has_left_work = false;
3281 int idx = dc_link->link_index;
3282 struct hpd_rx_irq_offload_work_queue *offload_wq = &adev->dm.hpd_rx_offload_wq[idx];
3284 memset(&hpd_irq_data, 0, sizeof(hpd_irq_data));
3286 if (adev->dm.disable_hpd_irq)
3290 * TODO:Temporary add mutex to protect hpd interrupt not have a gpio
3291 * conflict, after implement i2c helper, this mutex should be
3294 mutex_lock(&aconnector->hpd_lock);
3296 result = dc_link_handle_hpd_rx_irq(dc_link, &hpd_irq_data,
3297 &link_loss, true, &has_left_work);
3302 if (hpd_irq_data.bytes.device_service_irq.bits.AUTOMATED_TEST) {
3303 schedule_hpd_rx_offload_work(offload_wq, hpd_irq_data);
3307 if (dc_link_dp_allow_hpd_rx_irq(dc_link)) {
3308 if (hpd_irq_data.bytes.device_service_irq.bits.UP_REQ_MSG_RDY ||
3309 hpd_irq_data.bytes.device_service_irq.bits.DOWN_REP_MSG_RDY) {
3313 * DOWN_REP_MSG_RDY is also handled by polling method
3314 * mgr->cbs->poll_hpd_irq()
3316 spin_lock(&offload_wq->offload_lock);
3317 skip = offload_wq->is_handling_mst_msg_rdy_event;
3320 offload_wq->is_handling_mst_msg_rdy_event = true;
3322 spin_unlock(&offload_wq->offload_lock);
3325 schedule_hpd_rx_offload_work(offload_wq, hpd_irq_data);
3333 spin_lock(&offload_wq->offload_lock);
3334 skip = offload_wq->is_handling_link_loss;
3337 offload_wq->is_handling_link_loss = true;
3339 spin_unlock(&offload_wq->offload_lock);
3342 schedule_hpd_rx_offload_work(offload_wq, hpd_irq_data);
3349 if (result && !is_mst_root_connector) {
3350 /* Downstream Port status changed. */
3351 if (!dc_link_detect_connection_type(dc_link, &new_connection_type))
3352 DRM_ERROR("KMS: Failed to detect connector\n");
3354 if (aconnector->base.force && new_connection_type == dc_connection_none) {
3355 emulated_link_detect(dc_link);
3357 if (aconnector->fake_enable)
3358 aconnector->fake_enable = false;
3360 amdgpu_dm_update_connector_after_detect(aconnector);
3363 drm_modeset_lock_all(dev);
3364 dm_restore_drm_connector_state(dev, connector);
3365 drm_modeset_unlock_all(dev);
3367 drm_kms_helper_connector_hotplug_event(connector);
3371 mutex_lock(&adev->dm.dc_lock);
3372 ret = dc_link_detect(dc_link, DETECT_REASON_HPDRX);
3373 mutex_unlock(&adev->dm.dc_lock);
3376 if (aconnector->fake_enable)
3377 aconnector->fake_enable = false;
3379 amdgpu_dm_update_connector_after_detect(aconnector);
3381 drm_modeset_lock_all(dev);
3382 dm_restore_drm_connector_state(dev, connector);
3383 drm_modeset_unlock_all(dev);
3385 drm_kms_helper_connector_hotplug_event(connector);
3389 if (hpd_irq_data.bytes.device_service_irq.bits.CP_IRQ) {
3390 if (adev->dm.hdcp_workqueue)
3391 hdcp_handle_cpirq(adev->dm.hdcp_workqueue, aconnector->base.index);
3394 if (dc_link->type != dc_connection_mst_branch)
3395 drm_dp_cec_irq(&aconnector->dm_dp_aux.aux);
3397 mutex_unlock(&aconnector->hpd_lock);
3400 static void register_hpd_handlers(struct amdgpu_device *adev)
3402 struct drm_device *dev = adev_to_drm(adev);
3403 struct drm_connector *connector;
3404 struct amdgpu_dm_connector *aconnector;
3405 const struct dc_link *dc_link;
3406 struct dc_interrupt_params int_params = {0};
3408 int_params.requested_polarity = INTERRUPT_POLARITY_DEFAULT;
3409 int_params.current_polarity = INTERRUPT_POLARITY_DEFAULT;
3411 list_for_each_entry(connector,
3412 &dev->mode_config.connector_list, head) {
3414 aconnector = to_amdgpu_dm_connector(connector);
3415 dc_link = aconnector->dc_link;
3417 if (dc_link->irq_source_hpd != DC_IRQ_SOURCE_INVALID) {
3418 int_params.int_context = INTERRUPT_LOW_IRQ_CONTEXT;
3419 int_params.irq_source = dc_link->irq_source_hpd;
3421 amdgpu_dm_irq_register_interrupt(adev, &int_params,
3423 (void *) aconnector);
3426 if (dc_link->irq_source_hpd_rx != DC_IRQ_SOURCE_INVALID) {
3428 /* Also register for DP short pulse (hpd_rx). */
3429 int_params.int_context = INTERRUPT_LOW_IRQ_CONTEXT;
3430 int_params.irq_source = dc_link->irq_source_hpd_rx;
3432 amdgpu_dm_irq_register_interrupt(adev, &int_params,
3434 (void *) aconnector);
3437 if (adev->dm.hpd_rx_offload_wq)
3438 adev->dm.hpd_rx_offload_wq[connector->index].aconnector =
3443 #if defined(CONFIG_DRM_AMD_DC_SI)
3444 /* Register IRQ sources and initialize IRQ callbacks */
3445 static int dce60_register_irq_handlers(struct amdgpu_device *adev)
3447 struct dc *dc = adev->dm.dc;
3448 struct common_irq_params *c_irq_params;
3449 struct dc_interrupt_params int_params = {0};
3452 unsigned int client_id = AMDGPU_IRQ_CLIENTID_LEGACY;
3454 int_params.requested_polarity = INTERRUPT_POLARITY_DEFAULT;
3455 int_params.current_polarity = INTERRUPT_POLARITY_DEFAULT;
3458 * Actions of amdgpu_irq_add_id():
3459 * 1. Register a set() function with base driver.
3460 * Base driver will call set() function to enable/disable an
3461 * interrupt in DC hardware.
3462 * 2. Register amdgpu_dm_irq_handler().
3463 * Base driver will call amdgpu_dm_irq_handler() for ALL interrupts
3464 * coming from DC hardware.
3465 * amdgpu_dm_irq_handler() will re-direct the interrupt to DC
3466 * for acknowledging and handling.
3469 /* Use VBLANK interrupt */
3470 for (i = 0; i < adev->mode_info.num_crtc; i++) {
3471 r = amdgpu_irq_add_id(adev, client_id, i + 1, &adev->crtc_irq);
3473 DRM_ERROR("Failed to add crtc irq id!\n");
3477 int_params.int_context = INTERRUPT_HIGH_IRQ_CONTEXT;
3478 int_params.irq_source =
3479 dc_interrupt_to_irq_source(dc, i + 1, 0);
3481 c_irq_params = &adev->dm.vblank_params[int_params.irq_source - DC_IRQ_SOURCE_VBLANK1];
3483 c_irq_params->adev = adev;
3484 c_irq_params->irq_src = int_params.irq_source;
3486 amdgpu_dm_irq_register_interrupt(adev, &int_params,
3487 dm_crtc_high_irq, c_irq_params);
3490 /* Use GRPH_PFLIP interrupt */
3491 for (i = VISLANDS30_IV_SRCID_D1_GRPH_PFLIP;
3492 i <= VISLANDS30_IV_SRCID_D6_GRPH_PFLIP; i += 2) {
3493 r = amdgpu_irq_add_id(adev, client_id, i, &adev->pageflip_irq);
3495 DRM_ERROR("Failed to add page flip irq id!\n");
3499 int_params.int_context = INTERRUPT_HIGH_IRQ_CONTEXT;
3500 int_params.irq_source =
3501 dc_interrupt_to_irq_source(dc, i, 0);
3503 c_irq_params = &adev->dm.pflip_params[int_params.irq_source - DC_IRQ_SOURCE_PFLIP_FIRST];
3505 c_irq_params->adev = adev;
3506 c_irq_params->irq_src = int_params.irq_source;
3508 amdgpu_dm_irq_register_interrupt(adev, &int_params,
3509 dm_pflip_high_irq, c_irq_params);
3514 r = amdgpu_irq_add_id(adev, client_id,
3515 VISLANDS30_IV_SRCID_HOTPLUG_DETECT_A, &adev->hpd_irq);
3517 DRM_ERROR("Failed to add hpd irq id!\n");
3521 register_hpd_handlers(adev);
3527 /* Register IRQ sources and initialize IRQ callbacks */
3528 static int dce110_register_irq_handlers(struct amdgpu_device *adev)
3530 struct dc *dc = adev->dm.dc;
3531 struct common_irq_params *c_irq_params;
3532 struct dc_interrupt_params int_params = {0};
3535 unsigned int client_id = AMDGPU_IRQ_CLIENTID_LEGACY;
3537 if (adev->family >= AMDGPU_FAMILY_AI)
3538 client_id = SOC15_IH_CLIENTID_DCE;
3540 int_params.requested_polarity = INTERRUPT_POLARITY_DEFAULT;
3541 int_params.current_polarity = INTERRUPT_POLARITY_DEFAULT;
3544 * Actions of amdgpu_irq_add_id():
3545 * 1. Register a set() function with base driver.
3546 * Base driver will call set() function to enable/disable an
3547 * interrupt in DC hardware.
3548 * 2. Register amdgpu_dm_irq_handler().
3549 * Base driver will call amdgpu_dm_irq_handler() for ALL interrupts
3550 * coming from DC hardware.
3551 * amdgpu_dm_irq_handler() will re-direct the interrupt to DC
3552 * for acknowledging and handling.
3555 /* Use VBLANK interrupt */
3556 for (i = VISLANDS30_IV_SRCID_D1_VERTICAL_INTERRUPT0; i <= VISLANDS30_IV_SRCID_D6_VERTICAL_INTERRUPT0; i++) {
3557 r = amdgpu_irq_add_id(adev, client_id, i, &adev->crtc_irq);
3559 DRM_ERROR("Failed to add crtc irq id!\n");
3563 int_params.int_context = INTERRUPT_HIGH_IRQ_CONTEXT;
3564 int_params.irq_source =
3565 dc_interrupt_to_irq_source(dc, i, 0);
3567 c_irq_params = &adev->dm.vblank_params[int_params.irq_source - DC_IRQ_SOURCE_VBLANK1];
3569 c_irq_params->adev = adev;
3570 c_irq_params->irq_src = int_params.irq_source;
3572 amdgpu_dm_irq_register_interrupt(adev, &int_params,
3573 dm_crtc_high_irq, c_irq_params);
3576 /* Use VUPDATE interrupt */
3577 for (i = VISLANDS30_IV_SRCID_D1_V_UPDATE_INT; i <= VISLANDS30_IV_SRCID_D6_V_UPDATE_INT; i += 2) {
3578 r = amdgpu_irq_add_id(adev, client_id, i, &adev->vupdate_irq);
3580 DRM_ERROR("Failed to add vupdate irq id!\n");
3584 int_params.int_context = INTERRUPT_HIGH_IRQ_CONTEXT;
3585 int_params.irq_source =
3586 dc_interrupt_to_irq_source(dc, i, 0);
3588 c_irq_params = &adev->dm.vupdate_params[int_params.irq_source - DC_IRQ_SOURCE_VUPDATE1];
3590 c_irq_params->adev = adev;
3591 c_irq_params->irq_src = int_params.irq_source;
3593 amdgpu_dm_irq_register_interrupt(adev, &int_params,
3594 dm_vupdate_high_irq, c_irq_params);
3597 /* Use GRPH_PFLIP interrupt */
3598 for (i = VISLANDS30_IV_SRCID_D1_GRPH_PFLIP;
3599 i <= VISLANDS30_IV_SRCID_D6_GRPH_PFLIP; i += 2) {
3600 r = amdgpu_irq_add_id(adev, client_id, i, &adev->pageflip_irq);
3602 DRM_ERROR("Failed to add page flip irq id!\n");
3606 int_params.int_context = INTERRUPT_HIGH_IRQ_CONTEXT;
3607 int_params.irq_source =
3608 dc_interrupt_to_irq_source(dc, i, 0);
3610 c_irq_params = &adev->dm.pflip_params[int_params.irq_source - DC_IRQ_SOURCE_PFLIP_FIRST];
3612 c_irq_params->adev = adev;
3613 c_irq_params->irq_src = int_params.irq_source;
3615 amdgpu_dm_irq_register_interrupt(adev, &int_params,
3616 dm_pflip_high_irq, c_irq_params);
3621 r = amdgpu_irq_add_id(adev, client_id,
3622 VISLANDS30_IV_SRCID_HOTPLUG_DETECT_A, &adev->hpd_irq);
3624 DRM_ERROR("Failed to add hpd irq id!\n");
3628 register_hpd_handlers(adev);
3633 /* Register IRQ sources and initialize IRQ callbacks */
3634 static int dcn10_register_irq_handlers(struct amdgpu_device *adev)
3636 struct dc *dc = adev->dm.dc;
3637 struct common_irq_params *c_irq_params;
3638 struct dc_interrupt_params int_params = {0};
3641 #if defined(CONFIG_DRM_AMD_SECURE_DISPLAY)
3642 static const unsigned int vrtl_int_srcid[] = {
3643 DCN_1_0__SRCID__OTG1_VERTICAL_INTERRUPT0_CONTROL,
3644 DCN_1_0__SRCID__OTG2_VERTICAL_INTERRUPT0_CONTROL,
3645 DCN_1_0__SRCID__OTG3_VERTICAL_INTERRUPT0_CONTROL,
3646 DCN_1_0__SRCID__OTG4_VERTICAL_INTERRUPT0_CONTROL,
3647 DCN_1_0__SRCID__OTG5_VERTICAL_INTERRUPT0_CONTROL,
3648 DCN_1_0__SRCID__OTG6_VERTICAL_INTERRUPT0_CONTROL
3652 int_params.requested_polarity = INTERRUPT_POLARITY_DEFAULT;
3653 int_params.current_polarity = INTERRUPT_POLARITY_DEFAULT;
3656 * Actions of amdgpu_irq_add_id():
3657 * 1. Register a set() function with base driver.
3658 * Base driver will call set() function to enable/disable an
3659 * interrupt in DC hardware.
3660 * 2. Register amdgpu_dm_irq_handler().
3661 * Base driver will call amdgpu_dm_irq_handler() for ALL interrupts
3662 * coming from DC hardware.
3663 * amdgpu_dm_irq_handler() will re-direct the interrupt to DC
3664 * for acknowledging and handling.
3667 /* Use VSTARTUP interrupt */
3668 for (i = DCN_1_0__SRCID__DC_D1_OTG_VSTARTUP;
3669 i <= DCN_1_0__SRCID__DC_D1_OTG_VSTARTUP + adev->mode_info.num_crtc - 1;
3671 r = amdgpu_irq_add_id(adev, SOC15_IH_CLIENTID_DCE, i, &adev->crtc_irq);
3674 DRM_ERROR("Failed to add crtc irq id!\n");
3678 int_params.int_context = INTERRUPT_HIGH_IRQ_CONTEXT;
3679 int_params.irq_source =
3680 dc_interrupt_to_irq_source(dc, i, 0);
3682 c_irq_params = &adev->dm.vblank_params[int_params.irq_source - DC_IRQ_SOURCE_VBLANK1];
3684 c_irq_params->adev = adev;
3685 c_irq_params->irq_src = int_params.irq_source;
3687 amdgpu_dm_irq_register_interrupt(
3688 adev, &int_params, dm_crtc_high_irq, c_irq_params);
3691 /* Use otg vertical line interrupt */
3692 #if defined(CONFIG_DRM_AMD_SECURE_DISPLAY)
3693 for (i = 0; i <= adev->mode_info.num_crtc - 1; i++) {
3694 r = amdgpu_irq_add_id(adev, SOC15_IH_CLIENTID_DCE,
3695 vrtl_int_srcid[i], &adev->vline0_irq);
3698 DRM_ERROR("Failed to add vline0 irq id!\n");
3702 int_params.int_context = INTERRUPT_HIGH_IRQ_CONTEXT;
3703 int_params.irq_source =
3704 dc_interrupt_to_irq_source(dc, vrtl_int_srcid[i], 0);
3706 if (int_params.irq_source == DC_IRQ_SOURCE_INVALID) {
3707 DRM_ERROR("Failed to register vline0 irq %d!\n", vrtl_int_srcid[i]);
3711 c_irq_params = &adev->dm.vline0_params[int_params.irq_source
3712 - DC_IRQ_SOURCE_DC1_VLINE0];
3714 c_irq_params->adev = adev;
3715 c_irq_params->irq_src = int_params.irq_source;
3717 amdgpu_dm_irq_register_interrupt(adev, &int_params,
3718 dm_dcn_vertical_interrupt0_high_irq, c_irq_params);
3722 /* Use VUPDATE_NO_LOCK interrupt on DCN, which seems to correspond to
3723 * the regular VUPDATE interrupt on DCE. We want DC_IRQ_SOURCE_VUPDATEx
3724 * to trigger at end of each vblank, regardless of state of the lock,
3725 * matching DCE behaviour.
3727 for (i = DCN_1_0__SRCID__OTG0_IHC_V_UPDATE_NO_LOCK_INTERRUPT;
3728 i <= DCN_1_0__SRCID__OTG0_IHC_V_UPDATE_NO_LOCK_INTERRUPT + adev->mode_info.num_crtc - 1;
3730 r = amdgpu_irq_add_id(adev, SOC15_IH_CLIENTID_DCE, i, &adev->vupdate_irq);
3733 DRM_ERROR("Failed to add vupdate irq id!\n");
3737 int_params.int_context = INTERRUPT_HIGH_IRQ_CONTEXT;
3738 int_params.irq_source =
3739 dc_interrupt_to_irq_source(dc, i, 0);
3741 c_irq_params = &adev->dm.vupdate_params[int_params.irq_source - DC_IRQ_SOURCE_VUPDATE1];
3743 c_irq_params->adev = adev;
3744 c_irq_params->irq_src = int_params.irq_source;
3746 amdgpu_dm_irq_register_interrupt(adev, &int_params,
3747 dm_vupdate_high_irq, c_irq_params);
3750 /* Use GRPH_PFLIP interrupt */
3751 for (i = DCN_1_0__SRCID__HUBP0_FLIP_INTERRUPT;
3752 i <= DCN_1_0__SRCID__HUBP0_FLIP_INTERRUPT + dc->caps.max_otg_num - 1;
3754 r = amdgpu_irq_add_id(adev, SOC15_IH_CLIENTID_DCE, i, &adev->pageflip_irq);
3756 DRM_ERROR("Failed to add page flip irq id!\n");
3760 int_params.int_context = INTERRUPT_HIGH_IRQ_CONTEXT;
3761 int_params.irq_source =
3762 dc_interrupt_to_irq_source(dc, i, 0);
3764 c_irq_params = &adev->dm.pflip_params[int_params.irq_source - DC_IRQ_SOURCE_PFLIP_FIRST];
3766 c_irq_params->adev = adev;
3767 c_irq_params->irq_src = int_params.irq_source;
3769 amdgpu_dm_irq_register_interrupt(adev, &int_params,
3770 dm_pflip_high_irq, c_irq_params);
3775 r = amdgpu_irq_add_id(adev, SOC15_IH_CLIENTID_DCE, DCN_1_0__SRCID__DC_HPD1_INT,
3778 DRM_ERROR("Failed to add hpd irq id!\n");
3782 register_hpd_handlers(adev);
3786 /* Register Outbox IRQ sources and initialize IRQ callbacks */
3787 static int register_outbox_irq_handlers(struct amdgpu_device *adev)
3789 struct dc *dc = adev->dm.dc;
3790 struct common_irq_params *c_irq_params;
3791 struct dc_interrupt_params int_params = {0};
3794 int_params.requested_polarity = INTERRUPT_POLARITY_DEFAULT;
3795 int_params.current_polarity = INTERRUPT_POLARITY_DEFAULT;
3797 r = amdgpu_irq_add_id(adev, SOC15_IH_CLIENTID_DCE, DCN_1_0__SRCID__DMCUB_OUTBOX_LOW_PRIORITY_READY_INT,
3798 &adev->dmub_outbox_irq);
3800 DRM_ERROR("Failed to add outbox irq id!\n");
3804 if (dc->ctx->dmub_srv) {
3805 i = DCN_1_0__SRCID__DMCUB_OUTBOX_LOW_PRIORITY_READY_INT;
3806 int_params.int_context = INTERRUPT_LOW_IRQ_CONTEXT;
3807 int_params.irq_source =
3808 dc_interrupt_to_irq_source(dc, i, 0);
3810 c_irq_params = &adev->dm.dmub_outbox_params[0];
3812 c_irq_params->adev = adev;
3813 c_irq_params->irq_src = int_params.irq_source;
3815 amdgpu_dm_irq_register_interrupt(adev, &int_params,
3816 dm_dmub_outbox1_low_irq, c_irq_params);
3823 * Acquires the lock for the atomic state object and returns
3824 * the new atomic state.
3826 * This should only be called during atomic check.
3828 int dm_atomic_get_state(struct drm_atomic_state *state,
3829 struct dm_atomic_state **dm_state)
3831 struct drm_device *dev = state->dev;
3832 struct amdgpu_device *adev = drm_to_adev(dev);
3833 struct amdgpu_display_manager *dm = &adev->dm;
3834 struct drm_private_state *priv_state;
3839 priv_state = drm_atomic_get_private_obj_state(state, &dm->atomic_obj);
3840 if (IS_ERR(priv_state))
3841 return PTR_ERR(priv_state);
3843 *dm_state = to_dm_atomic_state(priv_state);
3848 static struct dm_atomic_state *
3849 dm_atomic_get_new_state(struct drm_atomic_state *state)
3851 struct drm_device *dev = state->dev;
3852 struct amdgpu_device *adev = drm_to_adev(dev);
3853 struct amdgpu_display_manager *dm = &adev->dm;
3854 struct drm_private_obj *obj;
3855 struct drm_private_state *new_obj_state;
3858 for_each_new_private_obj_in_state(state, obj, new_obj_state, i) {
3859 if (obj->funcs == dm->atomic_obj.funcs)
3860 return to_dm_atomic_state(new_obj_state);
3866 static struct drm_private_state *
3867 dm_atomic_duplicate_state(struct drm_private_obj *obj)
3869 struct dm_atomic_state *old_state, *new_state;
3871 new_state = kzalloc(sizeof(*new_state), GFP_KERNEL);
3875 __drm_atomic_helper_private_obj_duplicate_state(obj, &new_state->base);
3877 old_state = to_dm_atomic_state(obj->state);
3879 if (old_state && old_state->context)
3880 new_state->context = dc_copy_state(old_state->context);
3882 if (!new_state->context) {
3887 return &new_state->base;
3890 static void dm_atomic_destroy_state(struct drm_private_obj *obj,
3891 struct drm_private_state *state)
3893 struct dm_atomic_state *dm_state = to_dm_atomic_state(state);
3895 if (dm_state && dm_state->context)
3896 dc_release_state(dm_state->context);
3901 static struct drm_private_state_funcs dm_atomic_state_funcs = {
3902 .atomic_duplicate_state = dm_atomic_duplicate_state,
3903 .atomic_destroy_state = dm_atomic_destroy_state,
3906 static int amdgpu_dm_mode_config_init(struct amdgpu_device *adev)
3908 struct dm_atomic_state *state;
3911 adev->mode_info.mode_config_initialized = true;
3913 adev_to_drm(adev)->mode_config.funcs = (void *)&amdgpu_dm_mode_funcs;
3914 adev_to_drm(adev)->mode_config.helper_private = &amdgpu_dm_mode_config_helperfuncs;
3916 adev_to_drm(adev)->mode_config.max_width = 16384;
3917 adev_to_drm(adev)->mode_config.max_height = 16384;
3919 adev_to_drm(adev)->mode_config.preferred_depth = 24;
3920 if (adev->asic_type == CHIP_HAWAII)
3921 /* disable prefer shadow for now due to hibernation issues */
3922 adev_to_drm(adev)->mode_config.prefer_shadow = 0;
3924 adev_to_drm(adev)->mode_config.prefer_shadow = 1;
3925 /* indicates support for immediate flip */
3926 adev_to_drm(adev)->mode_config.async_page_flip = true;
3928 state = kzalloc(sizeof(*state), GFP_KERNEL);
3932 state->context = dc_create_state(adev->dm.dc);
3933 if (!state->context) {
3938 dc_resource_state_copy_construct_current(adev->dm.dc, state->context);
3940 drm_atomic_private_obj_init(adev_to_drm(adev),
3941 &adev->dm.atomic_obj,
3943 &dm_atomic_state_funcs);
3945 r = amdgpu_display_modeset_create_props(adev);
3947 dc_release_state(state->context);
3952 r = amdgpu_dm_audio_init(adev);
3954 dc_release_state(state->context);
3962 #define AMDGPU_DM_DEFAULT_MIN_BACKLIGHT 12
3963 #define AMDGPU_DM_DEFAULT_MAX_BACKLIGHT 255
3964 #define AUX_BL_DEFAULT_TRANSITION_TIME_MS 50
3966 static void amdgpu_dm_update_backlight_caps(struct amdgpu_display_manager *dm,
3969 #if defined(CONFIG_ACPI)
3970 struct amdgpu_dm_backlight_caps caps;
3972 memset(&caps, 0, sizeof(caps));
3974 if (dm->backlight_caps[bl_idx].caps_valid)
3977 amdgpu_acpi_get_backlight_caps(&caps);
3978 if (caps.caps_valid) {
3979 dm->backlight_caps[bl_idx].caps_valid = true;
3980 if (caps.aux_support)
3982 dm->backlight_caps[bl_idx].min_input_signal = caps.min_input_signal;
3983 dm->backlight_caps[bl_idx].max_input_signal = caps.max_input_signal;
3985 dm->backlight_caps[bl_idx].min_input_signal =
3986 AMDGPU_DM_DEFAULT_MIN_BACKLIGHT;
3987 dm->backlight_caps[bl_idx].max_input_signal =
3988 AMDGPU_DM_DEFAULT_MAX_BACKLIGHT;
3991 if (dm->backlight_caps[bl_idx].aux_support)
3994 dm->backlight_caps[bl_idx].min_input_signal = AMDGPU_DM_DEFAULT_MIN_BACKLIGHT;
3995 dm->backlight_caps[bl_idx].max_input_signal = AMDGPU_DM_DEFAULT_MAX_BACKLIGHT;
3999 static int get_brightness_range(const struct amdgpu_dm_backlight_caps *caps,
4000 unsigned int *min, unsigned int *max)
4005 if (caps->aux_support) {
4006 // Firmware limits are in nits, DC API wants millinits.
4007 *max = 1000 * caps->aux_max_input_signal;
4008 *min = 1000 * caps->aux_min_input_signal;
4010 // Firmware limits are 8-bit, PWM control is 16-bit.
4011 *max = 0x101 * caps->max_input_signal;
4012 *min = 0x101 * caps->min_input_signal;
4017 static u32 convert_brightness_from_user(const struct amdgpu_dm_backlight_caps *caps,
4018 uint32_t brightness)
4020 unsigned int min, max;
4022 if (!get_brightness_range(caps, &min, &max))
4025 // Rescale 0..255 to min..max
4026 return min + DIV_ROUND_CLOSEST((max - min) * brightness,
4027 AMDGPU_MAX_BL_LEVEL);
4030 static u32 convert_brightness_to_user(const struct amdgpu_dm_backlight_caps *caps,
4031 uint32_t brightness)
4033 unsigned int min, max;
4035 if (!get_brightness_range(caps, &min, &max))
4038 if (brightness < min)
4040 // Rescale min..max to 0..255
4041 return DIV_ROUND_CLOSEST(AMDGPU_MAX_BL_LEVEL * (brightness - min),
4045 static void amdgpu_dm_backlight_set_level(struct amdgpu_display_manager *dm,
4047 u32 user_brightness)
4049 struct amdgpu_dm_backlight_caps caps;
4050 struct dc_link *link;
4054 amdgpu_dm_update_backlight_caps(dm, bl_idx);
4055 caps = dm->backlight_caps[bl_idx];
4057 dm->brightness[bl_idx] = user_brightness;
4058 /* update scratch register */
4060 amdgpu_atombios_scratch_regs_set_backlight_level(dm->adev, dm->brightness[bl_idx]);
4061 brightness = convert_brightness_from_user(&caps, dm->brightness[bl_idx]);
4062 link = (struct dc_link *)dm->backlight_link[bl_idx];
4064 /* Change brightness based on AUX property */
4065 if (caps.aux_support) {
4066 rc = dc_link_set_backlight_level_nits(link, true, brightness,
4067 AUX_BL_DEFAULT_TRANSITION_TIME_MS);
4069 DRM_DEBUG("DM: Failed to update backlight via AUX on eDP[%d]\n", bl_idx);
4071 rc = dc_link_set_backlight_level(link, brightness, 0);
4073 DRM_DEBUG("DM: Failed to update backlight on eDP[%d]\n", bl_idx);
4077 dm->actual_brightness[bl_idx] = user_brightness;
4080 static int amdgpu_dm_backlight_update_status(struct backlight_device *bd)
4082 struct amdgpu_display_manager *dm = bl_get_data(bd);
4085 for (i = 0; i < dm->num_of_edps; i++) {
4086 if (bd == dm->backlight_dev[i])
4089 if (i >= AMDGPU_DM_MAX_NUM_EDP)
4091 amdgpu_dm_backlight_set_level(dm, i, bd->props.brightness);
4096 static u32 amdgpu_dm_backlight_get_level(struct amdgpu_display_manager *dm,
4100 struct amdgpu_dm_backlight_caps caps;
4101 struct dc_link *link = (struct dc_link *)dm->backlight_link[bl_idx];
4103 amdgpu_dm_update_backlight_caps(dm, bl_idx);
4104 caps = dm->backlight_caps[bl_idx];
4106 if (caps.aux_support) {
4110 rc = dc_link_get_backlight_level_nits(link, &avg, &peak);
4112 return dm->brightness[bl_idx];
4113 return convert_brightness_to_user(&caps, avg);
4116 ret = dc_link_get_backlight_level(link);
4118 if (ret == DC_ERROR_UNEXPECTED)
4119 return dm->brightness[bl_idx];
4121 return convert_brightness_to_user(&caps, ret);
4124 static int amdgpu_dm_backlight_get_brightness(struct backlight_device *bd)
4126 struct amdgpu_display_manager *dm = bl_get_data(bd);
4129 for (i = 0; i < dm->num_of_edps; i++) {
4130 if (bd == dm->backlight_dev[i])
4133 if (i >= AMDGPU_DM_MAX_NUM_EDP)
4135 return amdgpu_dm_backlight_get_level(dm, i);
4138 static const struct backlight_ops amdgpu_dm_backlight_ops = {
4139 .options = BL_CORE_SUSPENDRESUME,
4140 .get_brightness = amdgpu_dm_backlight_get_brightness,
4141 .update_status = amdgpu_dm_backlight_update_status,
4145 amdgpu_dm_register_backlight_device(struct amdgpu_dm_connector *aconnector)
4147 struct drm_device *drm = aconnector->base.dev;
4148 struct amdgpu_display_manager *dm = &drm_to_adev(drm)->dm;
4149 struct backlight_properties props = { 0 };
4152 if (aconnector->bl_idx == -1)
4155 if (!acpi_video_backlight_use_native()) {
4156 drm_info(drm, "Skipping amdgpu DM backlight registration\n");
4157 /* Try registering an ACPI video backlight device instead. */
4158 acpi_video_register_backlight();
4162 props.max_brightness = AMDGPU_MAX_BL_LEVEL;
4163 props.brightness = AMDGPU_MAX_BL_LEVEL;
4164 props.type = BACKLIGHT_RAW;
4166 snprintf(bl_name, sizeof(bl_name), "amdgpu_bl%d",
4167 drm->primary->index + aconnector->bl_idx);
4169 dm->backlight_dev[aconnector->bl_idx] =
4170 backlight_device_register(bl_name, aconnector->base.kdev, dm,
4171 &amdgpu_dm_backlight_ops, &props);
4173 if (IS_ERR(dm->backlight_dev[aconnector->bl_idx])) {
4174 DRM_ERROR("DM: Backlight registration failed!\n");
4175 dm->backlight_dev[aconnector->bl_idx] = NULL;
4177 DRM_DEBUG_DRIVER("DM: Registered Backlight device: %s\n", bl_name);
4180 static int initialize_plane(struct amdgpu_display_manager *dm,
4181 struct amdgpu_mode_info *mode_info, int plane_id,
4182 enum drm_plane_type plane_type,
4183 const struct dc_plane_cap *plane_cap)
4185 struct drm_plane *plane;
4186 unsigned long possible_crtcs;
4189 plane = kzalloc(sizeof(struct drm_plane), GFP_KERNEL);
4191 DRM_ERROR("KMS: Failed to allocate plane\n");
4194 plane->type = plane_type;
4197 * HACK: IGT tests expect that the primary plane for a CRTC
4198 * can only have one possible CRTC. Only expose support for
4199 * any CRTC if they're not going to be used as a primary plane
4200 * for a CRTC - like overlay or underlay planes.
4202 possible_crtcs = 1 << plane_id;
4203 if (plane_id >= dm->dc->caps.max_streams)
4204 possible_crtcs = 0xff;
4206 ret = amdgpu_dm_plane_init(dm, plane, possible_crtcs, plane_cap);
4209 DRM_ERROR("KMS: Failed to initialize plane\n");
4215 mode_info->planes[plane_id] = plane;
4221 static void setup_backlight_device(struct amdgpu_display_manager *dm,
4222 struct amdgpu_dm_connector *aconnector)
4224 struct dc_link *link = aconnector->dc_link;
4225 int bl_idx = dm->num_of_edps;
4227 if (!(link->connector_signal & (SIGNAL_TYPE_EDP | SIGNAL_TYPE_LVDS)) ||
4228 link->type == dc_connection_none)
4231 if (dm->num_of_edps >= AMDGPU_DM_MAX_NUM_EDP) {
4232 drm_warn(adev_to_drm(dm->adev), "Too much eDP connections, skipping backlight setup for additional eDPs\n");
4236 aconnector->bl_idx = bl_idx;
4238 amdgpu_dm_update_backlight_caps(dm, bl_idx);
4239 dm->brightness[bl_idx] = AMDGPU_MAX_BL_LEVEL;
4240 dm->backlight_link[bl_idx] = link;
4243 update_connector_ext_caps(aconnector);
4246 static void amdgpu_set_panel_orientation(struct drm_connector *connector);
4249 * In this architecture, the association
4250 * connector -> encoder -> crtc
4251 * id not really requried. The crtc and connector will hold the
4252 * display_index as an abstraction to use with DAL component
4254 * Returns 0 on success
4256 static int amdgpu_dm_initialize_drm_device(struct amdgpu_device *adev)
4258 struct amdgpu_display_manager *dm = &adev->dm;
4260 struct amdgpu_dm_connector *aconnector = NULL;
4261 struct amdgpu_encoder *aencoder = NULL;
4262 struct amdgpu_mode_info *mode_info = &adev->mode_info;
4265 enum dc_connection_type new_connection_type = dc_connection_none;
4266 const struct dc_plane_cap *plane;
4267 bool psr_feature_enabled = false;
4268 int max_overlay = dm->dc->caps.max_slave_planes;
4270 dm->display_indexes_num = dm->dc->caps.max_streams;
4271 /* Update the actual used number of crtc */
4272 adev->mode_info.num_crtc = adev->dm.display_indexes_num;
4274 amdgpu_dm_set_irq_funcs(adev);
4276 link_cnt = dm->dc->caps.max_links;
4277 if (amdgpu_dm_mode_config_init(dm->adev)) {
4278 DRM_ERROR("DM: Failed to initialize mode config\n");
4282 /* There is one primary plane per CRTC */
4283 primary_planes = dm->dc->caps.max_streams;
4284 ASSERT(primary_planes <= AMDGPU_MAX_PLANES);
4287 * Initialize primary planes, implicit planes for legacy IOCTLS.
4288 * Order is reversed to match iteration order in atomic check.
4290 for (i = (primary_planes - 1); i >= 0; i--) {
4291 plane = &dm->dc->caps.planes[i];
4293 if (initialize_plane(dm, mode_info, i,
4294 DRM_PLANE_TYPE_PRIMARY, plane)) {
4295 DRM_ERROR("KMS: Failed to initialize primary plane\n");
4301 * Initialize overlay planes, index starting after primary planes.
4302 * These planes have a higher DRM index than the primary planes since
4303 * they should be considered as having a higher z-order.
4304 * Order is reversed to match iteration order in atomic check.
4306 * Only support DCN for now, and only expose one so we don't encourage
4307 * userspace to use up all the pipes.
4309 for (i = 0; i < dm->dc->caps.max_planes; ++i) {
4310 struct dc_plane_cap *plane = &dm->dc->caps.planes[i];
4312 /* Do not create overlay if MPO disabled */
4313 if (amdgpu_dc_debug_mask & DC_DISABLE_MPO)
4316 if (plane->type != DC_PLANE_TYPE_DCN_UNIVERSAL)
4319 if (!plane->pixel_format_support.argb8888)
4322 if (max_overlay-- == 0)
4325 if (initialize_plane(dm, NULL, primary_planes + i,
4326 DRM_PLANE_TYPE_OVERLAY, plane)) {
4327 DRM_ERROR("KMS: Failed to initialize overlay plane\n");
4332 for (i = 0; i < dm->dc->caps.max_streams; i++)
4333 if (amdgpu_dm_crtc_init(dm, mode_info->planes[i], i)) {
4334 DRM_ERROR("KMS: Failed to initialize crtc\n");
4338 /* Use Outbox interrupt */
4339 switch (adev->ip_versions[DCE_HWIP][0]) {
4340 case IP_VERSION(3, 0, 0):
4341 case IP_VERSION(3, 1, 2):
4342 case IP_VERSION(3, 1, 3):
4343 case IP_VERSION(3, 1, 4):
4344 case IP_VERSION(3, 1, 5):
4345 case IP_VERSION(3, 1, 6):
4346 case IP_VERSION(3, 2, 0):
4347 case IP_VERSION(3, 2, 1):
4348 case IP_VERSION(2, 1, 0):
4349 if (register_outbox_irq_handlers(dm->adev)) {
4350 DRM_ERROR("DM: Failed to initialize IRQ\n");
4355 DRM_DEBUG_KMS("Unsupported DCN IP version for outbox: 0x%X\n",
4356 adev->ip_versions[DCE_HWIP][0]);
4359 /* Determine whether to enable PSR support by default. */
4360 if (!(amdgpu_dc_debug_mask & DC_DISABLE_PSR)) {
4361 switch (adev->ip_versions[DCE_HWIP][0]) {
4362 case IP_VERSION(3, 1, 2):
4363 case IP_VERSION(3, 1, 3):
4364 case IP_VERSION(3, 1, 4):
4365 case IP_VERSION(3, 1, 5):
4366 case IP_VERSION(3, 1, 6):
4367 case IP_VERSION(3, 2, 0):
4368 case IP_VERSION(3, 2, 1):
4369 psr_feature_enabled = true;
4372 psr_feature_enabled = amdgpu_dc_feature_mask & DC_PSR_MASK;
4377 /* loops over all connectors on the board */
4378 for (i = 0; i < link_cnt; i++) {
4379 struct dc_link *link = NULL;
4381 if (i > AMDGPU_DM_MAX_DISPLAY_INDEX) {
4383 "KMS: Cannot support more than %d display indexes\n",
4384 AMDGPU_DM_MAX_DISPLAY_INDEX);
4388 aconnector = kzalloc(sizeof(*aconnector), GFP_KERNEL);
4392 aencoder = kzalloc(sizeof(*aencoder), GFP_KERNEL);
4396 if (amdgpu_dm_encoder_init(dm->ddev, aencoder, i)) {
4397 DRM_ERROR("KMS: Failed to initialize encoder\n");
4401 if (amdgpu_dm_connector_init(dm, aconnector, i, aencoder)) {
4402 DRM_ERROR("KMS: Failed to initialize connector\n");
4406 link = dc_get_link_at_index(dm->dc, i);
4408 if (!dc_link_detect_connection_type(link, &new_connection_type))
4409 DRM_ERROR("KMS: Failed to detect connector\n");
4411 if (aconnector->base.force && new_connection_type == dc_connection_none) {
4412 emulated_link_detect(link);
4413 amdgpu_dm_update_connector_after_detect(aconnector);
4417 mutex_lock(&dm->dc_lock);
4418 ret = dc_link_detect(link, DETECT_REASON_BOOT);
4419 mutex_unlock(&dm->dc_lock);
4422 amdgpu_dm_update_connector_after_detect(aconnector);
4423 setup_backlight_device(dm, aconnector);
4425 if (psr_feature_enabled)
4426 amdgpu_dm_set_psr_caps(link);
4428 /* TODO: Fix vblank control helpers to delay PSR entry to allow this when
4429 * PSR is also supported.
4431 if (link->psr_settings.psr_feature_enabled)
4432 adev_to_drm(adev)->vblank_disable_immediate = false;
4435 amdgpu_set_panel_orientation(&aconnector->base);
4438 /* Software is initialized. Now we can register interrupt handlers. */
4439 switch (adev->asic_type) {
4440 #if defined(CONFIG_DRM_AMD_DC_SI)
4445 if (dce60_register_irq_handlers(dm->adev)) {
4446 DRM_ERROR("DM: Failed to initialize IRQ\n");
4460 case CHIP_POLARIS11:
4461 case CHIP_POLARIS10:
4462 case CHIP_POLARIS12:
4467 if (dce110_register_irq_handlers(dm->adev)) {
4468 DRM_ERROR("DM: Failed to initialize IRQ\n");
4473 switch (adev->ip_versions[DCE_HWIP][0]) {
4474 case IP_VERSION(1, 0, 0):
4475 case IP_VERSION(1, 0, 1):
4476 case IP_VERSION(2, 0, 2):
4477 case IP_VERSION(2, 0, 3):
4478 case IP_VERSION(2, 0, 0):
4479 case IP_VERSION(2, 1, 0):
4480 case IP_VERSION(3, 0, 0):
4481 case IP_VERSION(3, 0, 2):
4482 case IP_VERSION(3, 0, 3):
4483 case IP_VERSION(3, 0, 1):
4484 case IP_VERSION(3, 1, 2):
4485 case IP_VERSION(3, 1, 3):
4486 case IP_VERSION(3, 1, 4):
4487 case IP_VERSION(3, 1, 5):
4488 case IP_VERSION(3, 1, 6):
4489 case IP_VERSION(3, 2, 0):
4490 case IP_VERSION(3, 2, 1):
4491 if (dcn10_register_irq_handlers(dm->adev)) {
4492 DRM_ERROR("DM: Failed to initialize IRQ\n");
4497 DRM_ERROR("Unsupported DCE IP versions: 0x%X\n",
4498 adev->ip_versions[DCE_HWIP][0]);
4512 static void amdgpu_dm_destroy_drm_device(struct amdgpu_display_manager *dm)
4514 drm_atomic_private_obj_fini(&dm->atomic_obj);
4517 /******************************************************************************
4518 * amdgpu_display_funcs functions
4519 *****************************************************************************/
4522 * dm_bandwidth_update - program display watermarks
4524 * @adev: amdgpu_device pointer
4526 * Calculate and program the display watermarks and line buffer allocation.
4528 static void dm_bandwidth_update(struct amdgpu_device *adev)
4530 /* TODO: implement later */
4533 static const struct amdgpu_display_funcs dm_display_funcs = {
4534 .bandwidth_update = dm_bandwidth_update, /* called unconditionally */
4535 .vblank_get_counter = dm_vblank_get_counter,/* called unconditionally */
4536 .backlight_set_level = NULL, /* never called for DC */
4537 .backlight_get_level = NULL, /* never called for DC */
4538 .hpd_sense = NULL,/* called unconditionally */
4539 .hpd_set_polarity = NULL, /* called unconditionally */
4540 .hpd_get_gpio_reg = NULL, /* VBIOS parsing. DAL does it. */
4541 .page_flip_get_scanoutpos =
4542 dm_crtc_get_scanoutpos,/* called unconditionally */
4543 .add_encoder = NULL, /* VBIOS parsing. DAL does it. */
4544 .add_connector = NULL, /* VBIOS parsing. DAL does it. */
4547 #if defined(CONFIG_DEBUG_KERNEL_DC)
4549 static ssize_t s3_debug_store(struct device *device,
4550 struct device_attribute *attr,
4556 struct drm_device *drm_dev = dev_get_drvdata(device);
4557 struct amdgpu_device *adev = drm_to_adev(drm_dev);
4559 ret = kstrtoint(buf, 0, &s3_state);
4564 drm_kms_helper_hotplug_event(adev_to_drm(adev));
4569 return ret == 0 ? count : 0;
4572 DEVICE_ATTR_WO(s3_debug);
4576 static int dm_init_microcode(struct amdgpu_device *adev)
4581 switch (adev->ip_versions[DCE_HWIP][0]) {
4582 case IP_VERSION(2, 1, 0):
4583 fw_name_dmub = FIRMWARE_RENOIR_DMUB;
4584 if (ASICREV_IS_GREEN_SARDINE(adev->external_rev_id))
4585 fw_name_dmub = FIRMWARE_GREEN_SARDINE_DMUB;
4587 case IP_VERSION(3, 0, 0):
4588 if (adev->ip_versions[GC_HWIP][0] == IP_VERSION(10, 3, 0))
4589 fw_name_dmub = FIRMWARE_SIENNA_CICHLID_DMUB;
4591 fw_name_dmub = FIRMWARE_NAVY_FLOUNDER_DMUB;
4593 case IP_VERSION(3, 0, 1):
4594 fw_name_dmub = FIRMWARE_VANGOGH_DMUB;
4596 case IP_VERSION(3, 0, 2):
4597 fw_name_dmub = FIRMWARE_DIMGREY_CAVEFISH_DMUB;
4599 case IP_VERSION(3, 0, 3):
4600 fw_name_dmub = FIRMWARE_BEIGE_GOBY_DMUB;
4602 case IP_VERSION(3, 1, 2):
4603 case IP_VERSION(3, 1, 3):
4604 fw_name_dmub = FIRMWARE_YELLOW_CARP_DMUB;
4606 case IP_VERSION(3, 1, 4):
4607 fw_name_dmub = FIRMWARE_DCN_314_DMUB;
4609 case IP_VERSION(3, 1, 5):
4610 fw_name_dmub = FIRMWARE_DCN_315_DMUB;
4612 case IP_VERSION(3, 1, 6):
4613 fw_name_dmub = FIRMWARE_DCN316_DMUB;
4615 case IP_VERSION(3, 2, 0):
4616 fw_name_dmub = FIRMWARE_DCN_V3_2_0_DMCUB;
4618 case IP_VERSION(3, 2, 1):
4619 fw_name_dmub = FIRMWARE_DCN_V3_2_1_DMCUB;
4622 /* ASIC doesn't support DMUB. */
4625 r = amdgpu_ucode_request(adev, &adev->dm.dmub_fw, fw_name_dmub);
4627 DRM_ERROR("DMUB firmware loading failed: %d\n", r);
4631 static int dm_early_init(void *handle)
4633 struct amdgpu_device *adev = (struct amdgpu_device *)handle;
4634 struct amdgpu_mode_info *mode_info = &adev->mode_info;
4635 struct atom_context *ctx = mode_info->atom_context;
4636 int index = GetIndexIntoMasterTable(DATA, Object_Header);
4639 /* if there is no object header, skip DM */
4640 if (!amdgpu_atom_parse_data_header(ctx, index, NULL, NULL, NULL, &data_offset)) {
4641 adev->harvest_ip_mask |= AMD_HARVEST_IP_DMU_MASK;
4642 dev_info(adev->dev, "No object header, skipping DM\n");
4646 switch (adev->asic_type) {
4647 #if defined(CONFIG_DRM_AMD_DC_SI)
4651 adev->mode_info.num_crtc = 6;
4652 adev->mode_info.num_hpd = 6;
4653 adev->mode_info.num_dig = 6;
4656 adev->mode_info.num_crtc = 2;
4657 adev->mode_info.num_hpd = 2;
4658 adev->mode_info.num_dig = 2;
4663 adev->mode_info.num_crtc = 6;
4664 adev->mode_info.num_hpd = 6;
4665 adev->mode_info.num_dig = 6;
4668 adev->mode_info.num_crtc = 4;
4669 adev->mode_info.num_hpd = 6;
4670 adev->mode_info.num_dig = 7;
4674 adev->mode_info.num_crtc = 2;
4675 adev->mode_info.num_hpd = 6;
4676 adev->mode_info.num_dig = 6;
4680 adev->mode_info.num_crtc = 6;
4681 adev->mode_info.num_hpd = 6;
4682 adev->mode_info.num_dig = 7;
4685 adev->mode_info.num_crtc = 3;
4686 adev->mode_info.num_hpd = 6;
4687 adev->mode_info.num_dig = 9;
4690 adev->mode_info.num_crtc = 2;
4691 adev->mode_info.num_hpd = 6;
4692 adev->mode_info.num_dig = 9;
4694 case CHIP_POLARIS11:
4695 case CHIP_POLARIS12:
4696 adev->mode_info.num_crtc = 5;
4697 adev->mode_info.num_hpd = 5;
4698 adev->mode_info.num_dig = 5;
4700 case CHIP_POLARIS10:
4702 adev->mode_info.num_crtc = 6;
4703 adev->mode_info.num_hpd = 6;
4704 adev->mode_info.num_dig = 6;
4709 adev->mode_info.num_crtc = 6;
4710 adev->mode_info.num_hpd = 6;
4711 adev->mode_info.num_dig = 6;
4715 switch (adev->ip_versions[DCE_HWIP][0]) {
4716 case IP_VERSION(2, 0, 2):
4717 case IP_VERSION(3, 0, 0):
4718 adev->mode_info.num_crtc = 6;
4719 adev->mode_info.num_hpd = 6;
4720 adev->mode_info.num_dig = 6;
4722 case IP_VERSION(2, 0, 0):
4723 case IP_VERSION(3, 0, 2):
4724 adev->mode_info.num_crtc = 5;
4725 adev->mode_info.num_hpd = 5;
4726 adev->mode_info.num_dig = 5;
4728 case IP_VERSION(2, 0, 3):
4729 case IP_VERSION(3, 0, 3):
4730 adev->mode_info.num_crtc = 2;
4731 adev->mode_info.num_hpd = 2;
4732 adev->mode_info.num_dig = 2;
4734 case IP_VERSION(1, 0, 0):
4735 case IP_VERSION(1, 0, 1):
4736 case IP_VERSION(3, 0, 1):
4737 case IP_VERSION(2, 1, 0):
4738 case IP_VERSION(3, 1, 2):
4739 case IP_VERSION(3, 1, 3):
4740 case IP_VERSION(3, 1, 4):
4741 case IP_VERSION(3, 1, 5):
4742 case IP_VERSION(3, 1, 6):
4743 case IP_VERSION(3, 2, 0):
4744 case IP_VERSION(3, 2, 1):
4745 adev->mode_info.num_crtc = 4;
4746 adev->mode_info.num_hpd = 4;
4747 adev->mode_info.num_dig = 4;
4750 DRM_ERROR("Unsupported DCE IP versions: 0x%x\n",
4751 adev->ip_versions[DCE_HWIP][0]);
4757 if (adev->mode_info.funcs == NULL)
4758 adev->mode_info.funcs = &dm_display_funcs;
4761 * Note: Do NOT change adev->audio_endpt_rreg and
4762 * adev->audio_endpt_wreg because they are initialised in
4763 * amdgpu_device_init()
4765 #if defined(CONFIG_DEBUG_KERNEL_DC)
4767 adev_to_drm(adev)->dev,
4768 &dev_attr_s3_debug);
4770 adev->dc_enabled = true;
4772 return dm_init_microcode(adev);
4775 static bool modereset_required(struct drm_crtc_state *crtc_state)
4777 return !crtc_state->active && drm_atomic_crtc_needs_modeset(crtc_state);
4780 static void amdgpu_dm_encoder_destroy(struct drm_encoder *encoder)
4782 drm_encoder_cleanup(encoder);
4786 static const struct drm_encoder_funcs amdgpu_dm_encoder_funcs = {
4787 .destroy = amdgpu_dm_encoder_destroy,
4791 fill_plane_color_attributes(const struct drm_plane_state *plane_state,
4792 const enum surface_pixel_format format,
4793 enum dc_color_space *color_space)
4797 *color_space = COLOR_SPACE_SRGB;
4799 /* DRM color properties only affect non-RGB formats. */
4800 if (format < SURFACE_PIXEL_FORMAT_VIDEO_BEGIN)
4803 full_range = (plane_state->color_range == DRM_COLOR_YCBCR_FULL_RANGE);
4805 switch (plane_state->color_encoding) {
4806 case DRM_COLOR_YCBCR_BT601:
4808 *color_space = COLOR_SPACE_YCBCR601;
4810 *color_space = COLOR_SPACE_YCBCR601_LIMITED;
4813 case DRM_COLOR_YCBCR_BT709:
4815 *color_space = COLOR_SPACE_YCBCR709;
4817 *color_space = COLOR_SPACE_YCBCR709_LIMITED;
4820 case DRM_COLOR_YCBCR_BT2020:
4822 *color_space = COLOR_SPACE_2020_YCBCR;
4835 fill_dc_plane_info_and_addr(struct amdgpu_device *adev,
4836 const struct drm_plane_state *plane_state,
4837 const u64 tiling_flags,
4838 struct dc_plane_info *plane_info,
4839 struct dc_plane_address *address,
4841 bool force_disable_dcc)
4843 const struct drm_framebuffer *fb = plane_state->fb;
4844 const struct amdgpu_framebuffer *afb =
4845 to_amdgpu_framebuffer(plane_state->fb);
4848 memset(plane_info, 0, sizeof(*plane_info));
4850 switch (fb->format->format) {
4852 plane_info->format =
4853 SURFACE_PIXEL_FORMAT_GRPH_PALETA_256_COLORS;
4855 case DRM_FORMAT_RGB565:
4856 plane_info->format = SURFACE_PIXEL_FORMAT_GRPH_RGB565;
4858 case DRM_FORMAT_XRGB8888:
4859 case DRM_FORMAT_ARGB8888:
4860 plane_info->format = SURFACE_PIXEL_FORMAT_GRPH_ARGB8888;
4862 case DRM_FORMAT_XRGB2101010:
4863 case DRM_FORMAT_ARGB2101010:
4864 plane_info->format = SURFACE_PIXEL_FORMAT_GRPH_ARGB2101010;
4866 case DRM_FORMAT_XBGR2101010:
4867 case DRM_FORMAT_ABGR2101010:
4868 plane_info->format = SURFACE_PIXEL_FORMAT_GRPH_ABGR2101010;
4870 case DRM_FORMAT_XBGR8888:
4871 case DRM_FORMAT_ABGR8888:
4872 plane_info->format = SURFACE_PIXEL_FORMAT_GRPH_ABGR8888;
4874 case DRM_FORMAT_NV21:
4875 plane_info->format = SURFACE_PIXEL_FORMAT_VIDEO_420_YCbCr;
4877 case DRM_FORMAT_NV12:
4878 plane_info->format = SURFACE_PIXEL_FORMAT_VIDEO_420_YCrCb;
4880 case DRM_FORMAT_P010:
4881 plane_info->format = SURFACE_PIXEL_FORMAT_VIDEO_420_10bpc_YCrCb;
4883 case DRM_FORMAT_XRGB16161616F:
4884 case DRM_FORMAT_ARGB16161616F:
4885 plane_info->format = SURFACE_PIXEL_FORMAT_GRPH_ARGB16161616F;
4887 case DRM_FORMAT_XBGR16161616F:
4888 case DRM_FORMAT_ABGR16161616F:
4889 plane_info->format = SURFACE_PIXEL_FORMAT_GRPH_ABGR16161616F;
4891 case DRM_FORMAT_XRGB16161616:
4892 case DRM_FORMAT_ARGB16161616:
4893 plane_info->format = SURFACE_PIXEL_FORMAT_GRPH_ARGB16161616;
4895 case DRM_FORMAT_XBGR16161616:
4896 case DRM_FORMAT_ABGR16161616:
4897 plane_info->format = SURFACE_PIXEL_FORMAT_GRPH_ABGR16161616;
4901 "Unsupported screen format %p4cc\n",
4902 &fb->format->format);
4906 switch (plane_state->rotation & DRM_MODE_ROTATE_MASK) {
4907 case DRM_MODE_ROTATE_0:
4908 plane_info->rotation = ROTATION_ANGLE_0;
4910 case DRM_MODE_ROTATE_90:
4911 plane_info->rotation = ROTATION_ANGLE_90;
4913 case DRM_MODE_ROTATE_180:
4914 plane_info->rotation = ROTATION_ANGLE_180;
4916 case DRM_MODE_ROTATE_270:
4917 plane_info->rotation = ROTATION_ANGLE_270;
4920 plane_info->rotation = ROTATION_ANGLE_0;
4925 plane_info->visible = true;
4926 plane_info->stereo_format = PLANE_STEREO_FORMAT_NONE;
4928 plane_info->layer_index = plane_state->normalized_zpos;
4930 ret = fill_plane_color_attributes(plane_state, plane_info->format,
4931 &plane_info->color_space);
4935 ret = amdgpu_dm_plane_fill_plane_buffer_attributes(adev, afb, plane_info->format,
4936 plane_info->rotation, tiling_flags,
4937 &plane_info->tiling_info,
4938 &plane_info->plane_size,
4939 &plane_info->dcc, address,
4940 tmz_surface, force_disable_dcc);
4944 amdgpu_dm_plane_fill_blending_from_plane_state(
4945 plane_state, &plane_info->per_pixel_alpha, &plane_info->pre_multiplied_alpha,
4946 &plane_info->global_alpha, &plane_info->global_alpha_value);
4951 static int fill_dc_plane_attributes(struct amdgpu_device *adev,
4952 struct dc_plane_state *dc_plane_state,
4953 struct drm_plane_state *plane_state,
4954 struct drm_crtc_state *crtc_state)
4956 struct dm_crtc_state *dm_crtc_state = to_dm_crtc_state(crtc_state);
4957 struct amdgpu_framebuffer *afb = (struct amdgpu_framebuffer *)plane_state->fb;
4958 struct dc_scaling_info scaling_info;
4959 struct dc_plane_info plane_info;
4961 bool force_disable_dcc = false;
4963 ret = amdgpu_dm_plane_fill_dc_scaling_info(adev, plane_state, &scaling_info);
4967 dc_plane_state->src_rect = scaling_info.src_rect;
4968 dc_plane_state->dst_rect = scaling_info.dst_rect;
4969 dc_plane_state->clip_rect = scaling_info.clip_rect;
4970 dc_plane_state->scaling_quality = scaling_info.scaling_quality;
4972 force_disable_dcc = adev->asic_type == CHIP_RAVEN && adev->in_suspend;
4973 ret = fill_dc_plane_info_and_addr(adev, plane_state,
4976 &dc_plane_state->address,
4982 dc_plane_state->format = plane_info.format;
4983 dc_plane_state->color_space = plane_info.color_space;
4984 dc_plane_state->format = plane_info.format;
4985 dc_plane_state->plane_size = plane_info.plane_size;
4986 dc_plane_state->rotation = plane_info.rotation;
4987 dc_plane_state->horizontal_mirror = plane_info.horizontal_mirror;
4988 dc_plane_state->stereo_format = plane_info.stereo_format;
4989 dc_plane_state->tiling_info = plane_info.tiling_info;
4990 dc_plane_state->visible = plane_info.visible;
4991 dc_plane_state->per_pixel_alpha = plane_info.per_pixel_alpha;
4992 dc_plane_state->pre_multiplied_alpha = plane_info.pre_multiplied_alpha;
4993 dc_plane_state->global_alpha = plane_info.global_alpha;
4994 dc_plane_state->global_alpha_value = plane_info.global_alpha_value;
4995 dc_plane_state->dcc = plane_info.dcc;
4996 dc_plane_state->layer_index = plane_info.layer_index;
4997 dc_plane_state->flip_int_enabled = true;
5000 * Always set input transfer function, since plane state is refreshed
5003 ret = amdgpu_dm_update_plane_color_mgmt(dm_crtc_state, dc_plane_state);
5010 static inline void fill_dc_dirty_rect(struct drm_plane *plane,
5011 struct rect *dirty_rect, int32_t x,
5012 s32 y, s32 width, s32 height,
5015 WARN_ON(*i >= DC_MAX_DIRTY_RECTS);
5019 dirty_rect->width = width;
5020 dirty_rect->height = height;
5024 "[PLANE:%d] PSR FFU dirty rect size (%d, %d)\n",
5025 plane->base.id, width, height);
5028 "[PLANE:%d] PSR SU dirty rect at (%d, %d) size (%d, %d)",
5029 plane->base.id, x, y, width, height);
5035 * fill_dc_dirty_rects() - Fill DC dirty regions for PSR selective updates
5037 * @plane: DRM plane containing dirty regions that need to be flushed to the eDP
5039 * @old_plane_state: Old state of @plane
5040 * @new_plane_state: New state of @plane
5041 * @crtc_state: New state of CRTC connected to the @plane
5042 * @flip_addrs: DC flip tracking struct, which also tracts dirty rects
5043 * @dirty_regions_changed: dirty regions changed
5045 * For PSR SU, DC informs the DMUB uController of dirty rectangle regions
5046 * (referred to as "damage clips" in DRM nomenclature) that require updating on
5047 * the eDP remote buffer. The responsibility of specifying the dirty regions is
5050 * A damage-aware DRM client should fill the FB_DAMAGE_CLIPS property on the
5051 * plane with regions that require flushing to the eDP remote buffer. In
5052 * addition, certain use cases - such as cursor and multi-plane overlay (MPO) -
5053 * implicitly provide damage clips without any client support via the plane
5056 static void fill_dc_dirty_rects(struct drm_plane *plane,
5057 struct drm_plane_state *old_plane_state,
5058 struct drm_plane_state *new_plane_state,
5059 struct drm_crtc_state *crtc_state,
5060 struct dc_flip_addrs *flip_addrs,
5061 bool *dirty_regions_changed)
5063 struct dm_crtc_state *dm_crtc_state = to_dm_crtc_state(crtc_state);
5064 struct rect *dirty_rects = flip_addrs->dirty_rects;
5066 struct drm_mode_rect *clips;
5070 *dirty_regions_changed = false;
5073 * Cursor plane has it's own dirty rect update interface. See
5074 * dcn10_dmub_update_cursor_data and dmub_cmd_update_cursor_info_data
5076 if (plane->type == DRM_PLANE_TYPE_CURSOR)
5079 num_clips = drm_plane_get_damage_clips_count(new_plane_state);
5080 clips = drm_plane_get_damage_clips(new_plane_state);
5082 if (!dm_crtc_state->mpo_requested) {
5083 if (!num_clips || num_clips > DC_MAX_DIRTY_RECTS)
5086 for (; flip_addrs->dirty_rect_count < num_clips; clips++)
5087 fill_dc_dirty_rect(new_plane_state->plane,
5088 &dirty_rects[flip_addrs->dirty_rect_count],
5089 clips->x1, clips->y1,
5090 clips->x2 - clips->x1, clips->y2 - clips->y1,
5091 &flip_addrs->dirty_rect_count,
5097 * MPO is requested. Add entire plane bounding box to dirty rects if
5098 * flipped to or damaged.
5100 * If plane is moved or resized, also add old bounding box to dirty
5103 fb_changed = old_plane_state->fb->base.id !=
5104 new_plane_state->fb->base.id;
5105 bb_changed = (old_plane_state->crtc_x != new_plane_state->crtc_x ||
5106 old_plane_state->crtc_y != new_plane_state->crtc_y ||
5107 old_plane_state->crtc_w != new_plane_state->crtc_w ||
5108 old_plane_state->crtc_h != new_plane_state->crtc_h);
5111 "[PLANE:%d] PSR bb_changed:%d fb_changed:%d num_clips:%d\n",
5112 new_plane_state->plane->base.id,
5113 bb_changed, fb_changed, num_clips);
5115 *dirty_regions_changed = bb_changed;
5117 if ((num_clips + (bb_changed ? 2 : 0)) > DC_MAX_DIRTY_RECTS)
5121 fill_dc_dirty_rect(new_plane_state->plane, &dirty_rects[i],
5122 new_plane_state->crtc_x,
5123 new_plane_state->crtc_y,
5124 new_plane_state->crtc_w,
5125 new_plane_state->crtc_h, &i, false);
5127 /* Add old plane bounding-box if plane is moved or resized */
5128 fill_dc_dirty_rect(new_plane_state->plane, &dirty_rects[i],
5129 old_plane_state->crtc_x,
5130 old_plane_state->crtc_y,
5131 old_plane_state->crtc_w,
5132 old_plane_state->crtc_h, &i, false);
5136 for (; i < num_clips; clips++)
5137 fill_dc_dirty_rect(new_plane_state->plane,
5138 &dirty_rects[i], clips->x1,
5139 clips->y1, clips->x2 - clips->x1,
5140 clips->y2 - clips->y1, &i, false);
5141 } else if (fb_changed && !bb_changed) {
5142 fill_dc_dirty_rect(new_plane_state->plane, &dirty_rects[i],
5143 new_plane_state->crtc_x,
5144 new_plane_state->crtc_y,
5145 new_plane_state->crtc_w,
5146 new_plane_state->crtc_h, &i, false);
5149 flip_addrs->dirty_rect_count = i;
5153 fill_dc_dirty_rect(new_plane_state->plane, &dirty_rects[0], 0, 0,
5154 dm_crtc_state->base.mode.crtc_hdisplay,
5155 dm_crtc_state->base.mode.crtc_vdisplay,
5156 &flip_addrs->dirty_rect_count, true);
5159 static void update_stream_scaling_settings(const struct drm_display_mode *mode,
5160 const struct dm_connector_state *dm_state,
5161 struct dc_stream_state *stream)
5163 enum amdgpu_rmx_type rmx_type;
5165 struct rect src = { 0 }; /* viewport in composition space*/
5166 struct rect dst = { 0 }; /* stream addressable area */
5168 /* no mode. nothing to be done */
5172 /* Full screen scaling by default */
5173 src.width = mode->hdisplay;
5174 src.height = mode->vdisplay;
5175 dst.width = stream->timing.h_addressable;
5176 dst.height = stream->timing.v_addressable;
5179 rmx_type = dm_state->scaling;
5180 if (rmx_type == RMX_ASPECT || rmx_type == RMX_OFF) {
5181 if (src.width * dst.height <
5182 src.height * dst.width) {
5183 /* height needs less upscaling/more downscaling */
5184 dst.width = src.width *
5185 dst.height / src.height;
5187 /* width needs less upscaling/more downscaling */
5188 dst.height = src.height *
5189 dst.width / src.width;
5191 } else if (rmx_type == RMX_CENTER) {
5195 dst.x = (stream->timing.h_addressable - dst.width) / 2;
5196 dst.y = (stream->timing.v_addressable - dst.height) / 2;
5198 if (dm_state->underscan_enable) {
5199 dst.x += dm_state->underscan_hborder / 2;
5200 dst.y += dm_state->underscan_vborder / 2;
5201 dst.width -= dm_state->underscan_hborder;
5202 dst.height -= dm_state->underscan_vborder;
5209 DRM_DEBUG_KMS("Destination Rectangle x:%d y:%d width:%d height:%d\n",
5210 dst.x, dst.y, dst.width, dst.height);
5214 static enum dc_color_depth
5215 convert_color_depth_from_display_info(const struct drm_connector *connector,
5216 bool is_y420, int requested_bpc)
5223 /* Cap display bpc based on HDMI 2.0 HF-VSDB */
5224 if (connector->display_info.hdmi.y420_dc_modes & DRM_EDID_YCBCR420_DC_48)
5226 else if (connector->display_info.hdmi.y420_dc_modes & DRM_EDID_YCBCR420_DC_36)
5228 else if (connector->display_info.hdmi.y420_dc_modes & DRM_EDID_YCBCR420_DC_30)
5231 bpc = (uint8_t)connector->display_info.bpc;
5232 /* Assume 8 bpc by default if no bpc is specified. */
5233 bpc = bpc ? bpc : 8;
5236 if (requested_bpc > 0) {
5238 * Cap display bpc based on the user requested value.
5240 * The value for state->max_bpc may not correctly updated
5241 * depending on when the connector gets added to the state
5242 * or if this was called outside of atomic check, so it
5243 * can't be used directly.
5245 bpc = min_t(u8, bpc, requested_bpc);
5247 /* Round down to the nearest even number. */
5248 bpc = bpc - (bpc & 1);
5254 * Temporary Work around, DRM doesn't parse color depth for
5255 * EDID revision before 1.4
5256 * TODO: Fix edid parsing
5258 return COLOR_DEPTH_888;
5260 return COLOR_DEPTH_666;
5262 return COLOR_DEPTH_888;
5264 return COLOR_DEPTH_101010;
5266 return COLOR_DEPTH_121212;
5268 return COLOR_DEPTH_141414;
5270 return COLOR_DEPTH_161616;
5272 return COLOR_DEPTH_UNDEFINED;
5276 static enum dc_aspect_ratio
5277 get_aspect_ratio(const struct drm_display_mode *mode_in)
5279 /* 1-1 mapping, since both enums follow the HDMI spec. */
5280 return (enum dc_aspect_ratio) mode_in->picture_aspect_ratio;
5283 static enum dc_color_space
5284 get_output_color_space(const struct dc_crtc_timing *dc_crtc_timing,
5285 const struct drm_connector_state *connector_state)
5287 enum dc_color_space color_space = COLOR_SPACE_SRGB;
5289 switch (connector_state->colorspace) {
5290 case DRM_MODE_COLORIMETRY_BT601_YCC:
5291 if (dc_crtc_timing->flags.Y_ONLY)
5292 color_space = COLOR_SPACE_YCBCR601_LIMITED;
5294 color_space = COLOR_SPACE_YCBCR601;
5296 case DRM_MODE_COLORIMETRY_BT709_YCC:
5297 if (dc_crtc_timing->flags.Y_ONLY)
5298 color_space = COLOR_SPACE_YCBCR709_LIMITED;
5300 color_space = COLOR_SPACE_YCBCR709;
5302 case DRM_MODE_COLORIMETRY_OPRGB:
5303 color_space = COLOR_SPACE_ADOBERGB;
5305 case DRM_MODE_COLORIMETRY_BT2020_RGB:
5306 case DRM_MODE_COLORIMETRY_BT2020_YCC:
5307 if (dc_crtc_timing->pixel_encoding == PIXEL_ENCODING_RGB)
5308 color_space = COLOR_SPACE_2020_RGB_FULLRANGE;
5310 color_space = COLOR_SPACE_2020_YCBCR;
5312 case DRM_MODE_COLORIMETRY_DEFAULT: // ITU601
5314 if (dc_crtc_timing->pixel_encoding == PIXEL_ENCODING_RGB) {
5315 color_space = COLOR_SPACE_SRGB;
5317 * 27030khz is the separation point between HDTV and SDTV
5318 * according to HDMI spec, we use YCbCr709 and YCbCr601
5321 } else if (dc_crtc_timing->pix_clk_100hz > 270300) {
5322 if (dc_crtc_timing->flags.Y_ONLY)
5324 COLOR_SPACE_YCBCR709_LIMITED;
5326 color_space = COLOR_SPACE_YCBCR709;
5328 if (dc_crtc_timing->flags.Y_ONLY)
5330 COLOR_SPACE_YCBCR601_LIMITED;
5332 color_space = COLOR_SPACE_YCBCR601;
5340 static bool adjust_colour_depth_from_display_info(
5341 struct dc_crtc_timing *timing_out,
5342 const struct drm_display_info *info)
5344 enum dc_color_depth depth = timing_out->display_color_depth;
5348 normalized_clk = timing_out->pix_clk_100hz / 10;
5349 /* YCbCr 4:2:0 requires additional adjustment of 1/2 */
5350 if (timing_out->pixel_encoding == PIXEL_ENCODING_YCBCR420)
5351 normalized_clk /= 2;
5352 /* Adjusting pix clock following on HDMI spec based on colour depth */
5354 case COLOR_DEPTH_888:
5356 case COLOR_DEPTH_101010:
5357 normalized_clk = (normalized_clk * 30) / 24;
5359 case COLOR_DEPTH_121212:
5360 normalized_clk = (normalized_clk * 36) / 24;
5362 case COLOR_DEPTH_161616:
5363 normalized_clk = (normalized_clk * 48) / 24;
5366 /* The above depths are the only ones valid for HDMI. */
5369 if (normalized_clk <= info->max_tmds_clock) {
5370 timing_out->display_color_depth = depth;
5373 } while (--depth > COLOR_DEPTH_666);
5377 static void fill_stream_properties_from_drm_display_mode(
5378 struct dc_stream_state *stream,
5379 const struct drm_display_mode *mode_in,
5380 const struct drm_connector *connector,
5381 const struct drm_connector_state *connector_state,
5382 const struct dc_stream_state *old_stream,
5385 struct dc_crtc_timing *timing_out = &stream->timing;
5386 const struct drm_display_info *info = &connector->display_info;
5387 struct amdgpu_dm_connector *aconnector = to_amdgpu_dm_connector(connector);
5388 struct hdmi_vendor_infoframe hv_frame;
5389 struct hdmi_avi_infoframe avi_frame;
5391 memset(&hv_frame, 0, sizeof(hv_frame));
5392 memset(&avi_frame, 0, sizeof(avi_frame));
5394 timing_out->h_border_left = 0;
5395 timing_out->h_border_right = 0;
5396 timing_out->v_border_top = 0;
5397 timing_out->v_border_bottom = 0;
5398 /* TODO: un-hardcode */
5399 if (drm_mode_is_420_only(info, mode_in)
5400 && stream->signal == SIGNAL_TYPE_HDMI_TYPE_A)
5401 timing_out->pixel_encoding = PIXEL_ENCODING_YCBCR420;
5402 else if (drm_mode_is_420_also(info, mode_in)
5403 && aconnector->force_yuv420_output)
5404 timing_out->pixel_encoding = PIXEL_ENCODING_YCBCR420;
5405 else if ((connector->display_info.color_formats & DRM_COLOR_FORMAT_YCBCR444)
5406 && stream->signal == SIGNAL_TYPE_HDMI_TYPE_A)
5407 timing_out->pixel_encoding = PIXEL_ENCODING_YCBCR444;
5409 timing_out->pixel_encoding = PIXEL_ENCODING_RGB;
5411 timing_out->timing_3d_format = TIMING_3D_FORMAT_NONE;
5412 timing_out->display_color_depth = convert_color_depth_from_display_info(
5414 (timing_out->pixel_encoding == PIXEL_ENCODING_YCBCR420),
5416 timing_out->scan_type = SCANNING_TYPE_NODATA;
5417 timing_out->hdmi_vic = 0;
5420 timing_out->vic = old_stream->timing.vic;
5421 timing_out->flags.HSYNC_POSITIVE_POLARITY = old_stream->timing.flags.HSYNC_POSITIVE_POLARITY;
5422 timing_out->flags.VSYNC_POSITIVE_POLARITY = old_stream->timing.flags.VSYNC_POSITIVE_POLARITY;
5424 timing_out->vic = drm_match_cea_mode(mode_in);
5425 if (mode_in->flags & DRM_MODE_FLAG_PHSYNC)
5426 timing_out->flags.HSYNC_POSITIVE_POLARITY = 1;
5427 if (mode_in->flags & DRM_MODE_FLAG_PVSYNC)
5428 timing_out->flags.VSYNC_POSITIVE_POLARITY = 1;
5431 if (stream->signal == SIGNAL_TYPE_HDMI_TYPE_A) {
5432 drm_hdmi_avi_infoframe_from_display_mode(&avi_frame, (struct drm_connector *)connector, mode_in);
5433 timing_out->vic = avi_frame.video_code;
5434 drm_hdmi_vendor_infoframe_from_display_mode(&hv_frame, (struct drm_connector *)connector, mode_in);
5435 timing_out->hdmi_vic = hv_frame.vic;
5438 if (is_freesync_video_mode(mode_in, aconnector)) {
5439 timing_out->h_addressable = mode_in->hdisplay;
5440 timing_out->h_total = mode_in->htotal;
5441 timing_out->h_sync_width = mode_in->hsync_end - mode_in->hsync_start;
5442 timing_out->h_front_porch = mode_in->hsync_start - mode_in->hdisplay;
5443 timing_out->v_total = mode_in->vtotal;
5444 timing_out->v_addressable = mode_in->vdisplay;
5445 timing_out->v_front_porch = mode_in->vsync_start - mode_in->vdisplay;
5446 timing_out->v_sync_width = mode_in->vsync_end - mode_in->vsync_start;
5447 timing_out->pix_clk_100hz = mode_in->clock * 10;
5449 timing_out->h_addressable = mode_in->crtc_hdisplay;
5450 timing_out->h_total = mode_in->crtc_htotal;
5451 timing_out->h_sync_width = mode_in->crtc_hsync_end - mode_in->crtc_hsync_start;
5452 timing_out->h_front_porch = mode_in->crtc_hsync_start - mode_in->crtc_hdisplay;
5453 timing_out->v_total = mode_in->crtc_vtotal;
5454 timing_out->v_addressable = mode_in->crtc_vdisplay;
5455 timing_out->v_front_porch = mode_in->crtc_vsync_start - mode_in->crtc_vdisplay;
5456 timing_out->v_sync_width = mode_in->crtc_vsync_end - mode_in->crtc_vsync_start;
5457 timing_out->pix_clk_100hz = mode_in->crtc_clock * 10;
5460 timing_out->aspect_ratio = get_aspect_ratio(mode_in);
5462 stream->out_transfer_func->type = TF_TYPE_PREDEFINED;
5463 stream->out_transfer_func->tf = TRANSFER_FUNCTION_SRGB;
5464 if (stream->signal == SIGNAL_TYPE_HDMI_TYPE_A) {
5465 if (!adjust_colour_depth_from_display_info(timing_out, info) &&
5466 drm_mode_is_420_also(info, mode_in) &&
5467 timing_out->pixel_encoding != PIXEL_ENCODING_YCBCR420) {
5468 timing_out->pixel_encoding = PIXEL_ENCODING_YCBCR420;
5469 adjust_colour_depth_from_display_info(timing_out, info);
5473 stream->output_color_space = get_output_color_space(timing_out, connector_state);
5476 static void fill_audio_info(struct audio_info *audio_info,
5477 const struct drm_connector *drm_connector,
5478 const struct dc_sink *dc_sink)
5481 int cea_revision = 0;
5482 const struct dc_edid_caps *edid_caps = &dc_sink->edid_caps;
5484 audio_info->manufacture_id = edid_caps->manufacturer_id;
5485 audio_info->product_id = edid_caps->product_id;
5487 cea_revision = drm_connector->display_info.cea_rev;
5489 strscpy(audio_info->display_name,
5490 edid_caps->display_name,
5491 AUDIO_INFO_DISPLAY_NAME_SIZE_IN_CHARS);
5493 if (cea_revision >= 3) {
5494 audio_info->mode_count = edid_caps->audio_mode_count;
5496 for (i = 0; i < audio_info->mode_count; ++i) {
5497 audio_info->modes[i].format_code =
5498 (enum audio_format_code)
5499 (edid_caps->audio_modes[i].format_code);
5500 audio_info->modes[i].channel_count =
5501 edid_caps->audio_modes[i].channel_count;
5502 audio_info->modes[i].sample_rates.all =
5503 edid_caps->audio_modes[i].sample_rate;
5504 audio_info->modes[i].sample_size =
5505 edid_caps->audio_modes[i].sample_size;
5509 audio_info->flags.all = edid_caps->speaker_flags;
5511 /* TODO: We only check for the progressive mode, check for interlace mode too */
5512 if (drm_connector->latency_present[0]) {
5513 audio_info->video_latency = drm_connector->video_latency[0];
5514 audio_info->audio_latency = drm_connector->audio_latency[0];
5517 /* TODO: For DP, video and audio latency should be calculated from DPCD caps */
5522 copy_crtc_timing_for_drm_display_mode(const struct drm_display_mode *src_mode,
5523 struct drm_display_mode *dst_mode)
5525 dst_mode->crtc_hdisplay = src_mode->crtc_hdisplay;
5526 dst_mode->crtc_vdisplay = src_mode->crtc_vdisplay;
5527 dst_mode->crtc_clock = src_mode->crtc_clock;
5528 dst_mode->crtc_hblank_start = src_mode->crtc_hblank_start;
5529 dst_mode->crtc_hblank_end = src_mode->crtc_hblank_end;
5530 dst_mode->crtc_hsync_start = src_mode->crtc_hsync_start;
5531 dst_mode->crtc_hsync_end = src_mode->crtc_hsync_end;
5532 dst_mode->crtc_htotal = src_mode->crtc_htotal;
5533 dst_mode->crtc_hskew = src_mode->crtc_hskew;
5534 dst_mode->crtc_vblank_start = src_mode->crtc_vblank_start;
5535 dst_mode->crtc_vblank_end = src_mode->crtc_vblank_end;
5536 dst_mode->crtc_vsync_start = src_mode->crtc_vsync_start;
5537 dst_mode->crtc_vsync_end = src_mode->crtc_vsync_end;
5538 dst_mode->crtc_vtotal = src_mode->crtc_vtotal;
5542 decide_crtc_timing_for_drm_display_mode(struct drm_display_mode *drm_mode,
5543 const struct drm_display_mode *native_mode,
5546 if (scale_enabled) {
5547 copy_crtc_timing_for_drm_display_mode(native_mode, drm_mode);
5548 } else if (native_mode->clock == drm_mode->clock &&
5549 native_mode->htotal == drm_mode->htotal &&
5550 native_mode->vtotal == drm_mode->vtotal) {
5551 copy_crtc_timing_for_drm_display_mode(native_mode, drm_mode);
5553 /* no scaling nor amdgpu inserted, no need to patch */
5557 static struct dc_sink *
5558 create_fake_sink(struct amdgpu_dm_connector *aconnector)
5560 struct dc_sink_init_data sink_init_data = { 0 };
5561 struct dc_sink *sink = NULL;
5563 sink_init_data.link = aconnector->dc_link;
5564 sink_init_data.sink_signal = aconnector->dc_link->connector_signal;
5566 sink = dc_sink_create(&sink_init_data);
5568 DRM_ERROR("Failed to create sink!\n");
5571 sink->sink_signal = SIGNAL_TYPE_VIRTUAL;
5576 static void set_multisync_trigger_params(
5577 struct dc_stream_state *stream)
5579 struct dc_stream_state *master = NULL;
5581 if (stream->triggered_crtc_reset.enabled) {
5582 master = stream->triggered_crtc_reset.event_source;
5583 stream->triggered_crtc_reset.event =
5584 master->timing.flags.VSYNC_POSITIVE_POLARITY ?
5585 CRTC_EVENT_VSYNC_RISING : CRTC_EVENT_VSYNC_FALLING;
5586 stream->triggered_crtc_reset.delay = TRIGGER_DELAY_NEXT_PIXEL;
5590 static void set_master_stream(struct dc_stream_state *stream_set[],
5593 int j, highest_rfr = 0, master_stream = 0;
5595 for (j = 0; j < stream_count; j++) {
5596 if (stream_set[j] && stream_set[j]->triggered_crtc_reset.enabled) {
5597 int refresh_rate = 0;
5599 refresh_rate = (stream_set[j]->timing.pix_clk_100hz*100)/
5600 (stream_set[j]->timing.h_total*stream_set[j]->timing.v_total);
5601 if (refresh_rate > highest_rfr) {
5602 highest_rfr = refresh_rate;
5607 for (j = 0; j < stream_count; j++) {
5609 stream_set[j]->triggered_crtc_reset.event_source = stream_set[master_stream];
5613 static void dm_enable_per_frame_crtc_master_sync(struct dc_state *context)
5616 struct dc_stream_state *stream;
5618 if (context->stream_count < 2)
5620 for (i = 0; i < context->stream_count ; i++) {
5621 if (!context->streams[i])
5624 * TODO: add a function to read AMD VSDB bits and set
5625 * crtc_sync_master.multi_sync_enabled flag
5626 * For now it's set to false
5630 set_master_stream(context->streams, context->stream_count);
5632 for (i = 0; i < context->stream_count ; i++) {
5633 stream = context->streams[i];
5638 set_multisync_trigger_params(stream);
5643 * DOC: FreeSync Video
5645 * When a userspace application wants to play a video, the content follows a
5646 * standard format definition that usually specifies the FPS for that format.
5647 * The below list illustrates some video format and the expected FPS,
5650 * - TV/NTSC (23.976 FPS)
5653 * - TV/NTSC (29.97 FPS)
5654 * - TV/NTSC (30 FPS)
5655 * - Cinema HFR (48 FPS)
5657 * - Commonly used (60 FPS)
5658 * - Multiples of 24 (48,72,96 FPS)
5660 * The list of standards video format is not huge and can be added to the
5661 * connector modeset list beforehand. With that, userspace can leverage
5662 * FreeSync to extends the front porch in order to attain the target refresh
5663 * rate. Such a switch will happen seamlessly, without screen blanking or
5664 * reprogramming of the output in any other way. If the userspace requests a
5665 * modesetting change compatible with FreeSync modes that only differ in the
5666 * refresh rate, DC will skip the full update and avoid blink during the
5667 * transition. For example, the video player can change the modesetting from
5668 * 60Hz to 30Hz for playing TV/NTSC content when it goes full screen without
5669 * causing any display blink. This same concept can be applied to a mode
5672 static struct drm_display_mode *
5673 get_highest_refresh_rate_mode(struct amdgpu_dm_connector *aconnector,
5674 bool use_probed_modes)
5676 struct drm_display_mode *m, *m_pref = NULL;
5677 u16 current_refresh, highest_refresh;
5678 struct list_head *list_head = use_probed_modes ?
5679 &aconnector->base.probed_modes :
5680 &aconnector->base.modes;
5682 if (aconnector->freesync_vid_base.clock != 0)
5683 return &aconnector->freesync_vid_base;
5685 /* Find the preferred mode */
5686 list_for_each_entry(m, list_head, head) {
5687 if (m->type & DRM_MODE_TYPE_PREFERRED) {
5694 /* Probably an EDID with no preferred mode. Fallback to first entry */
5695 m_pref = list_first_entry_or_null(
5696 &aconnector->base.modes, struct drm_display_mode, head);
5698 DRM_DEBUG_DRIVER("No preferred mode found in EDID\n");
5703 highest_refresh = drm_mode_vrefresh(m_pref);
5706 * Find the mode with highest refresh rate with same resolution.
5707 * For some monitors, preferred mode is not the mode with highest
5708 * supported refresh rate.
5710 list_for_each_entry(m, list_head, head) {
5711 current_refresh = drm_mode_vrefresh(m);
5713 if (m->hdisplay == m_pref->hdisplay &&
5714 m->vdisplay == m_pref->vdisplay &&
5715 highest_refresh < current_refresh) {
5716 highest_refresh = current_refresh;
5721 drm_mode_copy(&aconnector->freesync_vid_base, m_pref);
5725 static bool is_freesync_video_mode(const struct drm_display_mode *mode,
5726 struct amdgpu_dm_connector *aconnector)
5728 struct drm_display_mode *high_mode;
5731 high_mode = get_highest_refresh_rate_mode(aconnector, false);
5732 if (!high_mode || !mode)
5735 timing_diff = high_mode->vtotal - mode->vtotal;
5737 if (high_mode->clock == 0 || high_mode->clock != mode->clock ||
5738 high_mode->hdisplay != mode->hdisplay ||
5739 high_mode->vdisplay != mode->vdisplay ||
5740 high_mode->hsync_start != mode->hsync_start ||
5741 high_mode->hsync_end != mode->hsync_end ||
5742 high_mode->htotal != mode->htotal ||
5743 high_mode->hskew != mode->hskew ||
5744 high_mode->vscan != mode->vscan ||
5745 high_mode->vsync_start - mode->vsync_start != timing_diff ||
5746 high_mode->vsync_end - mode->vsync_end != timing_diff)
5752 static void update_dsc_caps(struct amdgpu_dm_connector *aconnector,
5753 struct dc_sink *sink, struct dc_stream_state *stream,
5754 struct dsc_dec_dpcd_caps *dsc_caps)
5756 stream->timing.flags.DSC = 0;
5757 dsc_caps->is_dsc_supported = false;
5759 if (aconnector->dc_link && (sink->sink_signal == SIGNAL_TYPE_DISPLAY_PORT ||
5760 sink->sink_signal == SIGNAL_TYPE_EDP)) {
5761 if (sink->link->dpcd_caps.dongle_type == DISPLAY_DONGLE_NONE ||
5762 sink->link->dpcd_caps.dongle_type == DISPLAY_DONGLE_DP_HDMI_CONVERTER)
5763 dc_dsc_parse_dsc_dpcd(aconnector->dc_link->ctx->dc,
5764 aconnector->dc_link->dpcd_caps.dsc_caps.dsc_basic_caps.raw,
5765 aconnector->dc_link->dpcd_caps.dsc_caps.dsc_branch_decoder_caps.raw,
5771 static void apply_dsc_policy_for_edp(struct amdgpu_dm_connector *aconnector,
5772 struct dc_sink *sink, struct dc_stream_state *stream,
5773 struct dsc_dec_dpcd_caps *dsc_caps,
5774 uint32_t max_dsc_target_bpp_limit_override)
5776 const struct dc_link_settings *verified_link_cap = NULL;
5777 u32 link_bw_in_kbps;
5778 u32 edp_min_bpp_x16, edp_max_bpp_x16;
5779 struct dc *dc = sink->ctx->dc;
5780 struct dc_dsc_bw_range bw_range = {0};
5781 struct dc_dsc_config dsc_cfg = {0};
5782 struct dc_dsc_config_options dsc_options = {0};
5784 dc_dsc_get_default_config_option(dc, &dsc_options);
5785 dsc_options.max_target_bpp_limit_override_x16 = max_dsc_target_bpp_limit_override * 16;
5787 verified_link_cap = dc_link_get_link_cap(stream->link);
5788 link_bw_in_kbps = dc_link_bandwidth_kbps(stream->link, verified_link_cap);
5789 edp_min_bpp_x16 = 8 * 16;
5790 edp_max_bpp_x16 = 8 * 16;
5792 if (edp_max_bpp_x16 > dsc_caps->edp_max_bits_per_pixel)
5793 edp_max_bpp_x16 = dsc_caps->edp_max_bits_per_pixel;
5795 if (edp_max_bpp_x16 < edp_min_bpp_x16)
5796 edp_min_bpp_x16 = edp_max_bpp_x16;
5798 if (dc_dsc_compute_bandwidth_range(dc->res_pool->dscs[0],
5799 dc->debug.dsc_min_slice_height_override,
5800 edp_min_bpp_x16, edp_max_bpp_x16,
5803 dc_link_get_highest_encoding_format(aconnector->dc_link),
5806 if (bw_range.max_kbps < link_bw_in_kbps) {
5807 if (dc_dsc_compute_config(dc->res_pool->dscs[0],
5812 dc_link_get_highest_encoding_format(aconnector->dc_link),
5814 stream->timing.dsc_cfg = dsc_cfg;
5815 stream->timing.flags.DSC = 1;
5816 stream->timing.dsc_cfg.bits_per_pixel = edp_max_bpp_x16;
5822 if (dc_dsc_compute_config(dc->res_pool->dscs[0],
5827 dc_link_get_highest_encoding_format(aconnector->dc_link),
5829 stream->timing.dsc_cfg = dsc_cfg;
5830 stream->timing.flags.DSC = 1;
5835 static void apply_dsc_policy_for_stream(struct amdgpu_dm_connector *aconnector,
5836 struct dc_sink *sink, struct dc_stream_state *stream,
5837 struct dsc_dec_dpcd_caps *dsc_caps)
5839 struct drm_connector *drm_connector = &aconnector->base;
5840 u32 link_bandwidth_kbps;
5841 struct dc *dc = sink->ctx->dc;
5842 u32 max_supported_bw_in_kbps, timing_bw_in_kbps;
5843 u32 dsc_max_supported_bw_in_kbps;
5844 u32 max_dsc_target_bpp_limit_override =
5845 drm_connector->display_info.max_dsc_bpp;
5846 struct dc_dsc_config_options dsc_options = {0};
5848 dc_dsc_get_default_config_option(dc, &dsc_options);
5849 dsc_options.max_target_bpp_limit_override_x16 = max_dsc_target_bpp_limit_override * 16;
5851 link_bandwidth_kbps = dc_link_bandwidth_kbps(aconnector->dc_link,
5852 dc_link_get_link_cap(aconnector->dc_link));
5854 /* Set DSC policy according to dsc_clock_en */
5855 dc_dsc_policy_set_enable_dsc_when_not_needed(
5856 aconnector->dsc_settings.dsc_force_enable == DSC_CLK_FORCE_ENABLE);
5858 if (aconnector->dc_link && sink->sink_signal == SIGNAL_TYPE_EDP &&
5859 !aconnector->dc_link->panel_config.dsc.disable_dsc_edp &&
5860 dc->caps.edp_dsc_support && aconnector->dsc_settings.dsc_force_enable != DSC_CLK_FORCE_DISABLE) {
5862 apply_dsc_policy_for_edp(aconnector, sink, stream, dsc_caps, max_dsc_target_bpp_limit_override);
5864 } else if (aconnector->dc_link && sink->sink_signal == SIGNAL_TYPE_DISPLAY_PORT) {
5865 if (sink->link->dpcd_caps.dongle_type == DISPLAY_DONGLE_NONE) {
5866 if (dc_dsc_compute_config(aconnector->dc_link->ctx->dc->res_pool->dscs[0],
5869 link_bandwidth_kbps,
5871 dc_link_get_highest_encoding_format(aconnector->dc_link),
5872 &stream->timing.dsc_cfg)) {
5873 stream->timing.flags.DSC = 1;
5874 DRM_DEBUG_DRIVER("%s: [%s] DSC is selected from SST RX\n", __func__, drm_connector->name);
5876 } else if (sink->link->dpcd_caps.dongle_type == DISPLAY_DONGLE_DP_HDMI_CONVERTER) {
5877 timing_bw_in_kbps = dc_bandwidth_in_kbps_from_timing(&stream->timing,
5878 dc_link_get_highest_encoding_format(aconnector->dc_link));
5879 max_supported_bw_in_kbps = link_bandwidth_kbps;
5880 dsc_max_supported_bw_in_kbps = link_bandwidth_kbps;
5882 if (timing_bw_in_kbps > max_supported_bw_in_kbps &&
5883 max_supported_bw_in_kbps > 0 &&
5884 dsc_max_supported_bw_in_kbps > 0)
5885 if (dc_dsc_compute_config(aconnector->dc_link->ctx->dc->res_pool->dscs[0],
5888 dsc_max_supported_bw_in_kbps,
5890 dc_link_get_highest_encoding_format(aconnector->dc_link),
5891 &stream->timing.dsc_cfg)) {
5892 stream->timing.flags.DSC = 1;
5893 DRM_DEBUG_DRIVER("%s: [%s] DSC is selected from DP-HDMI PCON\n",
5894 __func__, drm_connector->name);
5899 /* Overwrite the stream flag if DSC is enabled through debugfs */
5900 if (aconnector->dsc_settings.dsc_force_enable == DSC_CLK_FORCE_ENABLE)
5901 stream->timing.flags.DSC = 1;
5903 if (stream->timing.flags.DSC && aconnector->dsc_settings.dsc_num_slices_h)
5904 stream->timing.dsc_cfg.num_slices_h = aconnector->dsc_settings.dsc_num_slices_h;
5906 if (stream->timing.flags.DSC && aconnector->dsc_settings.dsc_num_slices_v)
5907 stream->timing.dsc_cfg.num_slices_v = aconnector->dsc_settings.dsc_num_slices_v;
5909 if (stream->timing.flags.DSC && aconnector->dsc_settings.dsc_bits_per_pixel)
5910 stream->timing.dsc_cfg.bits_per_pixel = aconnector->dsc_settings.dsc_bits_per_pixel;
5913 static struct dc_stream_state *
5914 create_stream_for_sink(struct amdgpu_dm_connector *aconnector,
5915 const struct drm_display_mode *drm_mode,
5916 const struct dm_connector_state *dm_state,
5917 const struct dc_stream_state *old_stream,
5920 struct drm_display_mode *preferred_mode = NULL;
5921 struct drm_connector *drm_connector;
5922 const struct drm_connector_state *con_state = &dm_state->base;
5923 struct dc_stream_state *stream = NULL;
5924 struct drm_display_mode mode;
5925 struct drm_display_mode saved_mode;
5926 struct drm_display_mode *freesync_mode = NULL;
5927 bool native_mode_found = false;
5928 bool recalculate_timing = false;
5929 bool scale = dm_state->scaling != RMX_OFF;
5931 int preferred_refresh = 0;
5932 enum color_transfer_func tf = TRANSFER_FUNC_UNKNOWN;
5933 struct dsc_dec_dpcd_caps dsc_caps;
5935 struct dc_sink *sink = NULL;
5937 drm_mode_init(&mode, drm_mode);
5938 memset(&saved_mode, 0, sizeof(saved_mode));
5940 if (aconnector == NULL) {
5941 DRM_ERROR("aconnector is NULL!\n");
5945 drm_connector = &aconnector->base;
5947 if (!aconnector->dc_sink) {
5948 sink = create_fake_sink(aconnector);
5952 sink = aconnector->dc_sink;
5953 dc_sink_retain(sink);
5956 stream = dc_create_stream_for_sink(sink);
5958 if (stream == NULL) {
5959 DRM_ERROR("Failed to create stream for sink!\n");
5963 stream->dm_stream_context = aconnector;
5965 stream->timing.flags.LTE_340MCSC_SCRAMBLE =
5966 drm_connector->display_info.hdmi.scdc.scrambling.low_rates;
5968 list_for_each_entry(preferred_mode, &aconnector->base.modes, head) {
5969 /* Search for preferred mode */
5970 if (preferred_mode->type & DRM_MODE_TYPE_PREFERRED) {
5971 native_mode_found = true;
5975 if (!native_mode_found)
5976 preferred_mode = list_first_entry_or_null(
5977 &aconnector->base.modes,
5978 struct drm_display_mode,
5981 mode_refresh = drm_mode_vrefresh(&mode);
5983 if (preferred_mode == NULL) {
5985 * This may not be an error, the use case is when we have no
5986 * usermode calls to reset and set mode upon hotplug. In this
5987 * case, we call set mode ourselves to restore the previous mode
5988 * and the modelist may not be filled in time.
5990 DRM_DEBUG_DRIVER("No preferred mode found\n");
5992 recalculate_timing = is_freesync_video_mode(&mode, aconnector);
5993 if (recalculate_timing) {
5994 freesync_mode = get_highest_refresh_rate_mode(aconnector, false);
5995 drm_mode_copy(&saved_mode, &mode);
5996 drm_mode_copy(&mode, freesync_mode);
5998 decide_crtc_timing_for_drm_display_mode(
5999 &mode, preferred_mode, scale);
6001 preferred_refresh = drm_mode_vrefresh(preferred_mode);
6005 if (recalculate_timing)
6006 drm_mode_set_crtcinfo(&saved_mode, 0);
6008 drm_mode_set_crtcinfo(&mode, 0);
6011 * If scaling is enabled and refresh rate didn't change
6012 * we copy the vic and polarities of the old timings
6014 if (!scale || mode_refresh != preferred_refresh)
6015 fill_stream_properties_from_drm_display_mode(
6016 stream, &mode, &aconnector->base, con_state, NULL,
6019 fill_stream_properties_from_drm_display_mode(
6020 stream, &mode, &aconnector->base, con_state, old_stream,
6023 if (aconnector->timing_changed) {
6024 DC_LOG_DEBUG("%s: overriding timing for automated test, bpc %d, changing to %d\n",
6026 stream->timing.display_color_depth,
6027 aconnector->timing_requested->display_color_depth);
6028 stream->timing = *aconnector->timing_requested;
6031 /* SST DSC determination policy */
6032 update_dsc_caps(aconnector, sink, stream, &dsc_caps);
6033 if (aconnector->dsc_settings.dsc_force_enable != DSC_CLK_FORCE_DISABLE && dsc_caps.is_dsc_supported)
6034 apply_dsc_policy_for_stream(aconnector, sink, stream, &dsc_caps);
6036 update_stream_scaling_settings(&mode, dm_state, stream);
6039 &stream->audio_info,
6043 update_stream_signal(stream, sink);
6045 if (stream->signal == SIGNAL_TYPE_HDMI_TYPE_A)
6046 mod_build_hf_vsif_infopacket(stream, &stream->vsp_infopacket);
6048 if (stream->link->psr_settings.psr_feature_enabled || stream->link->replay_settings.replay_feature_enabled) {
6050 // should decide stream support vsc sdp colorimetry capability
6051 // before building vsc info packet
6053 stream->use_vsc_sdp_for_colorimetry = false;
6054 if (aconnector->dc_sink->sink_signal == SIGNAL_TYPE_DISPLAY_PORT_MST) {
6055 stream->use_vsc_sdp_for_colorimetry =
6056 aconnector->dc_sink->is_vsc_sdp_colorimetry_supported;
6058 if (stream->link->dpcd_caps.dprx_feature.bits.VSC_SDP_COLORIMETRY_SUPPORTED)
6059 stream->use_vsc_sdp_for_colorimetry = true;
6061 if (stream->out_transfer_func->tf == TRANSFER_FUNCTION_GAMMA22)
6062 tf = TRANSFER_FUNC_GAMMA_22;
6063 mod_build_vsc_infopacket(stream, &stream->vsc_infopacket, stream->output_color_space, tf);
6064 aconnector->psr_skip_count = AMDGPU_DM_PSR_ENTRY_DELAY;
6068 dc_sink_release(sink);
6073 static enum drm_connector_status
6074 amdgpu_dm_connector_detect(struct drm_connector *connector, bool force)
6077 struct amdgpu_dm_connector *aconnector = to_amdgpu_dm_connector(connector);
6081 * 1. This interface is NOT called in context of HPD irq.
6082 * 2. This interface *is called* in context of user-mode ioctl. Which
6083 * makes it a bad place for *any* MST-related activity.
6086 if (aconnector->base.force == DRM_FORCE_UNSPECIFIED &&
6087 !aconnector->fake_enable)
6088 connected = (aconnector->dc_sink != NULL);
6090 connected = (aconnector->base.force == DRM_FORCE_ON ||
6091 aconnector->base.force == DRM_FORCE_ON_DIGITAL);
6093 update_subconnector_property(aconnector);
6095 return (connected ? connector_status_connected :
6096 connector_status_disconnected);
6099 int amdgpu_dm_connector_atomic_set_property(struct drm_connector *connector,
6100 struct drm_connector_state *connector_state,
6101 struct drm_property *property,
6104 struct drm_device *dev = connector->dev;
6105 struct amdgpu_device *adev = drm_to_adev(dev);
6106 struct dm_connector_state *dm_old_state =
6107 to_dm_connector_state(connector->state);
6108 struct dm_connector_state *dm_new_state =
6109 to_dm_connector_state(connector_state);
6113 if (property == dev->mode_config.scaling_mode_property) {
6114 enum amdgpu_rmx_type rmx_type;
6117 case DRM_MODE_SCALE_CENTER:
6118 rmx_type = RMX_CENTER;
6120 case DRM_MODE_SCALE_ASPECT:
6121 rmx_type = RMX_ASPECT;
6123 case DRM_MODE_SCALE_FULLSCREEN:
6124 rmx_type = RMX_FULL;
6126 case DRM_MODE_SCALE_NONE:
6132 if (dm_old_state->scaling == rmx_type)
6135 dm_new_state->scaling = rmx_type;
6137 } else if (property == adev->mode_info.underscan_hborder_property) {
6138 dm_new_state->underscan_hborder = val;
6140 } else if (property == adev->mode_info.underscan_vborder_property) {
6141 dm_new_state->underscan_vborder = val;
6143 } else if (property == adev->mode_info.underscan_property) {
6144 dm_new_state->underscan_enable = val;
6146 } else if (property == adev->mode_info.abm_level_property) {
6147 dm_new_state->abm_level = val;
6154 int amdgpu_dm_connector_atomic_get_property(struct drm_connector *connector,
6155 const struct drm_connector_state *state,
6156 struct drm_property *property,
6159 struct drm_device *dev = connector->dev;
6160 struct amdgpu_device *adev = drm_to_adev(dev);
6161 struct dm_connector_state *dm_state =
6162 to_dm_connector_state(state);
6165 if (property == dev->mode_config.scaling_mode_property) {
6166 switch (dm_state->scaling) {
6168 *val = DRM_MODE_SCALE_CENTER;
6171 *val = DRM_MODE_SCALE_ASPECT;
6174 *val = DRM_MODE_SCALE_FULLSCREEN;
6178 *val = DRM_MODE_SCALE_NONE;
6182 } else if (property == adev->mode_info.underscan_hborder_property) {
6183 *val = dm_state->underscan_hborder;
6185 } else if (property == adev->mode_info.underscan_vborder_property) {
6186 *val = dm_state->underscan_vborder;
6188 } else if (property == adev->mode_info.underscan_property) {
6189 *val = dm_state->underscan_enable;
6191 } else if (property == adev->mode_info.abm_level_property) {
6192 *val = dm_state->abm_level;
6199 static void amdgpu_dm_connector_unregister(struct drm_connector *connector)
6201 struct amdgpu_dm_connector *amdgpu_dm_connector = to_amdgpu_dm_connector(connector);
6203 drm_dp_aux_unregister(&amdgpu_dm_connector->dm_dp_aux.aux);
6206 static void amdgpu_dm_connector_destroy(struct drm_connector *connector)
6208 struct amdgpu_dm_connector *aconnector = to_amdgpu_dm_connector(connector);
6209 struct amdgpu_device *adev = drm_to_adev(connector->dev);
6210 struct amdgpu_display_manager *dm = &adev->dm;
6213 * Call only if mst_mgr was initialized before since it's not done
6214 * for all connector types.
6216 if (aconnector->mst_mgr.dev)
6217 drm_dp_mst_topology_mgr_destroy(&aconnector->mst_mgr);
6219 if (aconnector->bl_idx != -1) {
6220 backlight_device_unregister(dm->backlight_dev[aconnector->bl_idx]);
6221 dm->backlight_dev[aconnector->bl_idx] = NULL;
6224 if (aconnector->dc_em_sink)
6225 dc_sink_release(aconnector->dc_em_sink);
6226 aconnector->dc_em_sink = NULL;
6227 if (aconnector->dc_sink)
6228 dc_sink_release(aconnector->dc_sink);
6229 aconnector->dc_sink = NULL;
6231 drm_dp_cec_unregister_connector(&aconnector->dm_dp_aux.aux);
6232 drm_connector_unregister(connector);
6233 drm_connector_cleanup(connector);
6234 if (aconnector->i2c) {
6235 i2c_del_adapter(&aconnector->i2c->base);
6236 kfree(aconnector->i2c);
6238 kfree(aconnector->dm_dp_aux.aux.name);
6243 void amdgpu_dm_connector_funcs_reset(struct drm_connector *connector)
6245 struct dm_connector_state *state =
6246 to_dm_connector_state(connector->state);
6248 if (connector->state)
6249 __drm_atomic_helper_connector_destroy_state(connector->state);
6253 state = kzalloc(sizeof(*state), GFP_KERNEL);
6256 state->scaling = RMX_OFF;
6257 state->underscan_enable = false;
6258 state->underscan_hborder = 0;
6259 state->underscan_vborder = 0;
6260 state->base.max_requested_bpc = 8;
6261 state->vcpi_slots = 0;
6264 if (connector->connector_type == DRM_MODE_CONNECTOR_eDP)
6265 state->abm_level = amdgpu_dm_abm_level;
6267 __drm_atomic_helper_connector_reset(connector, &state->base);
6271 struct drm_connector_state *
6272 amdgpu_dm_connector_atomic_duplicate_state(struct drm_connector *connector)
6274 struct dm_connector_state *state =
6275 to_dm_connector_state(connector->state);
6277 struct dm_connector_state *new_state =
6278 kmemdup(state, sizeof(*state), GFP_KERNEL);
6283 __drm_atomic_helper_connector_duplicate_state(connector, &new_state->base);
6285 new_state->freesync_capable = state->freesync_capable;
6286 new_state->abm_level = state->abm_level;
6287 new_state->scaling = state->scaling;
6288 new_state->underscan_enable = state->underscan_enable;
6289 new_state->underscan_hborder = state->underscan_hborder;
6290 new_state->underscan_vborder = state->underscan_vborder;
6291 new_state->vcpi_slots = state->vcpi_slots;
6292 new_state->pbn = state->pbn;
6293 return &new_state->base;
6297 amdgpu_dm_connector_late_register(struct drm_connector *connector)
6299 struct amdgpu_dm_connector *amdgpu_dm_connector =
6300 to_amdgpu_dm_connector(connector);
6303 amdgpu_dm_register_backlight_device(amdgpu_dm_connector);
6305 if ((connector->connector_type == DRM_MODE_CONNECTOR_DisplayPort) ||
6306 (connector->connector_type == DRM_MODE_CONNECTOR_eDP)) {
6307 amdgpu_dm_connector->dm_dp_aux.aux.dev = connector->kdev;
6308 r = drm_dp_aux_register(&amdgpu_dm_connector->dm_dp_aux.aux);
6313 #if defined(CONFIG_DEBUG_FS)
6314 connector_debugfs_init(amdgpu_dm_connector);
6320 static void amdgpu_dm_connector_funcs_force(struct drm_connector *connector)
6322 struct amdgpu_dm_connector *aconnector = to_amdgpu_dm_connector(connector);
6323 struct dc_link *dc_link = aconnector->dc_link;
6324 struct dc_sink *dc_em_sink = aconnector->dc_em_sink;
6327 if (!connector->edid_override)
6330 drm_edid_override_connector_update(&aconnector->base);
6331 edid = aconnector->base.edid_blob_ptr->data;
6332 aconnector->edid = edid;
6334 /* Update emulated (virtual) sink's EDID */
6335 if (dc_em_sink && dc_link) {
6336 memset(&dc_em_sink->edid_caps, 0, sizeof(struct dc_edid_caps));
6337 memmove(dc_em_sink->dc_edid.raw_edid, edid, (edid->extensions + 1) * EDID_LENGTH);
6338 dm_helpers_parse_edid_caps(
6340 &dc_em_sink->dc_edid,
6341 &dc_em_sink->edid_caps);
6345 static const struct drm_connector_funcs amdgpu_dm_connector_funcs = {
6346 .reset = amdgpu_dm_connector_funcs_reset,
6347 .detect = amdgpu_dm_connector_detect,
6348 .fill_modes = drm_helper_probe_single_connector_modes,
6349 .destroy = amdgpu_dm_connector_destroy,
6350 .atomic_duplicate_state = amdgpu_dm_connector_atomic_duplicate_state,
6351 .atomic_destroy_state = drm_atomic_helper_connector_destroy_state,
6352 .atomic_set_property = amdgpu_dm_connector_atomic_set_property,
6353 .atomic_get_property = amdgpu_dm_connector_atomic_get_property,
6354 .late_register = amdgpu_dm_connector_late_register,
6355 .early_unregister = amdgpu_dm_connector_unregister,
6356 .force = amdgpu_dm_connector_funcs_force
6359 static int get_modes(struct drm_connector *connector)
6361 return amdgpu_dm_connector_get_modes(connector);
6364 static void create_eml_sink(struct amdgpu_dm_connector *aconnector)
6366 struct dc_sink_init_data init_params = {
6367 .link = aconnector->dc_link,
6368 .sink_signal = SIGNAL_TYPE_VIRTUAL
6372 if (!aconnector->base.edid_blob_ptr) {
6373 /* if connector->edid_override valid, pass
6374 * it to edid_override to edid_blob_ptr
6377 drm_edid_override_connector_update(&aconnector->base);
6379 if (!aconnector->base.edid_blob_ptr) {
6380 DRM_ERROR("No EDID firmware found on connector: %s ,forcing to OFF!\n",
6381 aconnector->base.name);
6383 aconnector->base.force = DRM_FORCE_OFF;
6388 edid = (struct edid *) aconnector->base.edid_blob_ptr->data;
6390 aconnector->edid = edid;
6392 aconnector->dc_em_sink = dc_link_add_remote_sink(
6393 aconnector->dc_link,
6395 (edid->extensions + 1) * EDID_LENGTH,
6398 if (aconnector->base.force == DRM_FORCE_ON) {
6399 aconnector->dc_sink = aconnector->dc_link->local_sink ?
6400 aconnector->dc_link->local_sink :
6401 aconnector->dc_em_sink;
6402 dc_sink_retain(aconnector->dc_sink);
6406 static void handle_edid_mgmt(struct amdgpu_dm_connector *aconnector)
6408 struct dc_link *link = (struct dc_link *)aconnector->dc_link;
6411 * In case of headless boot with force on for DP managed connector
6412 * Those settings have to be != 0 to get initial modeset
6414 if (link->connector_signal == SIGNAL_TYPE_DISPLAY_PORT) {
6415 link->verified_link_cap.lane_count = LANE_COUNT_FOUR;
6416 link->verified_link_cap.link_rate = LINK_RATE_HIGH2;
6419 create_eml_sink(aconnector);
6422 static enum dc_status dm_validate_stream_and_context(struct dc *dc,
6423 struct dc_stream_state *stream)
6425 enum dc_status dc_result = DC_ERROR_UNEXPECTED;
6426 struct dc_plane_state *dc_plane_state = NULL;
6427 struct dc_state *dc_state = NULL;
6432 dc_plane_state = dc_create_plane_state(dc);
6433 if (!dc_plane_state)
6436 dc_state = dc_create_state(dc);
6440 /* populate stream to plane */
6441 dc_plane_state->src_rect.height = stream->src.height;
6442 dc_plane_state->src_rect.width = stream->src.width;
6443 dc_plane_state->dst_rect.height = stream->src.height;
6444 dc_plane_state->dst_rect.width = stream->src.width;
6445 dc_plane_state->clip_rect.height = stream->src.height;
6446 dc_plane_state->clip_rect.width = stream->src.width;
6447 dc_plane_state->plane_size.surface_pitch = ((stream->src.width + 255) / 256) * 256;
6448 dc_plane_state->plane_size.surface_size.height = stream->src.height;
6449 dc_plane_state->plane_size.surface_size.width = stream->src.width;
6450 dc_plane_state->plane_size.chroma_size.height = stream->src.height;
6451 dc_plane_state->plane_size.chroma_size.width = stream->src.width;
6452 dc_plane_state->format = SURFACE_PIXEL_FORMAT_GRPH_ARGB8888;
6453 dc_plane_state->tiling_info.gfx9.swizzle = DC_SW_UNKNOWN;
6454 dc_plane_state->rotation = ROTATION_ANGLE_0;
6455 dc_plane_state->is_tiling_rotated = false;
6456 dc_plane_state->tiling_info.gfx8.array_mode = DC_ARRAY_LINEAR_GENERAL;
6458 dc_result = dc_validate_stream(dc, stream);
6459 if (dc_result == DC_OK)
6460 dc_result = dc_validate_plane(dc, dc_plane_state);
6462 if (dc_result == DC_OK)
6463 dc_result = dc_add_stream_to_ctx(dc, dc_state, stream);
6465 if (dc_result == DC_OK && !dc_add_plane_to_context(
6470 dc_result = DC_FAIL_ATTACH_SURFACES;
6472 if (dc_result == DC_OK)
6473 dc_result = dc_validate_global_state(dc, dc_state, true);
6477 dc_release_state(dc_state);
6480 dc_plane_state_release(dc_plane_state);
6485 struct dc_stream_state *
6486 create_validate_stream_for_sink(struct amdgpu_dm_connector *aconnector,
6487 const struct drm_display_mode *drm_mode,
6488 const struct dm_connector_state *dm_state,
6489 const struct dc_stream_state *old_stream)
6491 struct drm_connector *connector = &aconnector->base;
6492 struct amdgpu_device *adev = drm_to_adev(connector->dev);
6493 struct dc_stream_state *stream;
6494 const struct drm_connector_state *drm_state = dm_state ? &dm_state->base : NULL;
6495 int requested_bpc = drm_state ? drm_state->max_requested_bpc : 8;
6496 enum dc_status dc_result = DC_OK;
6499 stream = create_stream_for_sink(aconnector, drm_mode,
6500 dm_state, old_stream,
6502 if (stream == NULL) {
6503 DRM_ERROR("Failed to create stream for sink!\n");
6507 dc_result = dc_validate_stream(adev->dm.dc, stream);
6508 if (dc_result == DC_OK && stream->signal == SIGNAL_TYPE_DISPLAY_PORT_MST)
6509 dc_result = dm_dp_mst_is_port_support_mode(aconnector, stream);
6511 if (dc_result == DC_OK)
6512 dc_result = dm_validate_stream_and_context(adev->dm.dc, stream);
6514 if (dc_result != DC_OK) {
6515 DRM_DEBUG_KMS("Mode %dx%d (clk %d) failed DC validation with error %d (%s)\n",
6520 dc_status_to_str(dc_result));
6522 dc_stream_release(stream);
6524 requested_bpc -= 2; /* lower bpc to retry validation */
6527 } while (stream == NULL && requested_bpc >= 6);
6529 if (dc_result == DC_FAIL_ENC_VALIDATE && !aconnector->force_yuv420_output) {
6530 DRM_DEBUG_KMS("Retry forcing YCbCr420 encoding\n");
6532 aconnector->force_yuv420_output = true;
6533 stream = create_validate_stream_for_sink(aconnector, drm_mode,
6534 dm_state, old_stream);
6535 aconnector->force_yuv420_output = false;
6541 enum drm_mode_status amdgpu_dm_connector_mode_valid(struct drm_connector *connector,
6542 struct drm_display_mode *mode)
6544 int result = MODE_ERROR;
6545 struct dc_sink *dc_sink;
6546 /* TODO: Unhardcode stream count */
6547 struct dc_stream_state *stream;
6548 struct amdgpu_dm_connector *aconnector = to_amdgpu_dm_connector(connector);
6550 if ((mode->flags & DRM_MODE_FLAG_INTERLACE) ||
6551 (mode->flags & DRM_MODE_FLAG_DBLSCAN))
6555 * Only run this the first time mode_valid is called to initilialize
6558 if (aconnector->base.force != DRM_FORCE_UNSPECIFIED &&
6559 !aconnector->dc_em_sink)
6560 handle_edid_mgmt(aconnector);
6562 dc_sink = to_amdgpu_dm_connector(connector)->dc_sink;
6564 if (dc_sink == NULL && aconnector->base.force != DRM_FORCE_ON_DIGITAL &&
6565 aconnector->base.force != DRM_FORCE_ON) {
6566 DRM_ERROR("dc_sink is NULL!\n");
6570 stream = create_validate_stream_for_sink(aconnector, mode,
6571 to_dm_connector_state(connector->state),
6574 dc_stream_release(stream);
6579 /* TODO: error handling*/
6583 static int fill_hdr_info_packet(const struct drm_connector_state *state,
6584 struct dc_info_packet *out)
6586 struct hdmi_drm_infoframe frame;
6587 unsigned char buf[30]; /* 26 + 4 */
6591 memset(out, 0, sizeof(*out));
6593 if (!state->hdr_output_metadata)
6596 ret = drm_hdmi_infoframe_set_hdr_metadata(&frame, state);
6600 len = hdmi_drm_infoframe_pack_only(&frame, buf, sizeof(buf));
6604 /* Static metadata is a fixed 26 bytes + 4 byte header. */
6608 /* Prepare the infopacket for DC. */
6609 switch (state->connector->connector_type) {
6610 case DRM_MODE_CONNECTOR_HDMIA:
6611 out->hb0 = 0x87; /* type */
6612 out->hb1 = 0x01; /* version */
6613 out->hb2 = 0x1A; /* length */
6614 out->sb[0] = buf[3]; /* checksum */
6618 case DRM_MODE_CONNECTOR_DisplayPort:
6619 case DRM_MODE_CONNECTOR_eDP:
6620 out->hb0 = 0x00; /* sdp id, zero */
6621 out->hb1 = 0x87; /* type */
6622 out->hb2 = 0x1D; /* payload len - 1 */
6623 out->hb3 = (0x13 << 2); /* sdp version */
6624 out->sb[0] = 0x01; /* version */
6625 out->sb[1] = 0x1A; /* length */
6633 memcpy(&out->sb[i], &buf[4], 26);
6636 print_hex_dump(KERN_DEBUG, "HDR SB:", DUMP_PREFIX_NONE, 16, 1, out->sb,
6637 sizeof(out->sb), false);
6643 amdgpu_dm_connector_atomic_check(struct drm_connector *conn,
6644 struct drm_atomic_state *state)
6646 struct drm_connector_state *new_con_state =
6647 drm_atomic_get_new_connector_state(state, conn);
6648 struct drm_connector_state *old_con_state =
6649 drm_atomic_get_old_connector_state(state, conn);
6650 struct drm_crtc *crtc = new_con_state->crtc;
6651 struct drm_crtc_state *new_crtc_state;
6652 struct amdgpu_dm_connector *aconn = to_amdgpu_dm_connector(conn);
6655 trace_amdgpu_dm_connector_atomic_check(new_con_state);
6657 if (conn->connector_type == DRM_MODE_CONNECTOR_DisplayPort) {
6658 ret = drm_dp_mst_root_conn_atomic_check(new_con_state, &aconn->mst_mgr);
6666 if (new_con_state->colorspace != old_con_state->colorspace) {
6667 new_crtc_state = drm_atomic_get_crtc_state(state, crtc);
6668 if (IS_ERR(new_crtc_state))
6669 return PTR_ERR(new_crtc_state);
6671 new_crtc_state->mode_changed = true;
6674 if (!drm_connector_atomic_hdr_metadata_equal(old_con_state, new_con_state)) {
6675 struct dc_info_packet hdr_infopacket;
6677 ret = fill_hdr_info_packet(new_con_state, &hdr_infopacket);
6681 new_crtc_state = drm_atomic_get_crtc_state(state, crtc);
6682 if (IS_ERR(new_crtc_state))
6683 return PTR_ERR(new_crtc_state);
6686 * DC considers the stream backends changed if the
6687 * static metadata changes. Forcing the modeset also
6688 * gives a simple way for userspace to switch from
6689 * 8bpc to 10bpc when setting the metadata to enter
6692 * Changing the static metadata after it's been
6693 * set is permissible, however. So only force a
6694 * modeset if we're entering or exiting HDR.
6696 new_crtc_state->mode_changed = new_crtc_state->mode_changed ||
6697 !old_con_state->hdr_output_metadata ||
6698 !new_con_state->hdr_output_metadata;
6704 static const struct drm_connector_helper_funcs
6705 amdgpu_dm_connector_helper_funcs = {
6707 * If hotplugging a second bigger display in FB Con mode, bigger resolution
6708 * modes will be filtered by drm_mode_validate_size(), and those modes
6709 * are missing after user start lightdm. So we need to renew modes list.
6710 * in get_modes call back, not just return the modes count
6712 .get_modes = get_modes,
6713 .mode_valid = amdgpu_dm_connector_mode_valid,
6714 .atomic_check = amdgpu_dm_connector_atomic_check,
6717 static void dm_encoder_helper_disable(struct drm_encoder *encoder)
6722 int convert_dc_color_depth_into_bpc(enum dc_color_depth display_color_depth)
6724 switch (display_color_depth) {
6725 case COLOR_DEPTH_666:
6727 case COLOR_DEPTH_888:
6729 case COLOR_DEPTH_101010:
6731 case COLOR_DEPTH_121212:
6733 case COLOR_DEPTH_141414:
6735 case COLOR_DEPTH_161616:
6743 static int dm_encoder_helper_atomic_check(struct drm_encoder *encoder,
6744 struct drm_crtc_state *crtc_state,
6745 struct drm_connector_state *conn_state)
6747 struct drm_atomic_state *state = crtc_state->state;
6748 struct drm_connector *connector = conn_state->connector;
6749 struct amdgpu_dm_connector *aconnector = to_amdgpu_dm_connector(connector);
6750 struct dm_connector_state *dm_new_connector_state = to_dm_connector_state(conn_state);
6751 const struct drm_display_mode *adjusted_mode = &crtc_state->adjusted_mode;
6752 struct drm_dp_mst_topology_mgr *mst_mgr;
6753 struct drm_dp_mst_port *mst_port;
6754 struct drm_dp_mst_topology_state *mst_state;
6755 enum dc_color_depth color_depth;
6757 bool is_y420 = false;
6759 if (!aconnector->mst_output_port)
6762 mst_port = aconnector->mst_output_port;
6763 mst_mgr = &aconnector->mst_root->mst_mgr;
6765 if (!crtc_state->connectors_changed && !crtc_state->mode_changed)
6768 mst_state = drm_atomic_get_mst_topology_state(state, mst_mgr);
6769 if (IS_ERR(mst_state))
6770 return PTR_ERR(mst_state);
6772 if (!mst_state->pbn_div)
6773 mst_state->pbn_div = dm_mst_get_pbn_divider(aconnector->mst_root->dc_link);
6775 if (!state->duplicated) {
6776 int max_bpc = conn_state->max_requested_bpc;
6778 is_y420 = drm_mode_is_420_also(&connector->display_info, adjusted_mode) &&
6779 aconnector->force_yuv420_output;
6780 color_depth = convert_color_depth_from_display_info(connector,
6783 bpp = convert_dc_color_depth_into_bpc(color_depth) * 3;
6784 clock = adjusted_mode->clock;
6785 dm_new_connector_state->pbn = drm_dp_calc_pbn_mode(clock, bpp, false);
6788 dm_new_connector_state->vcpi_slots =
6789 drm_dp_atomic_find_time_slots(state, mst_mgr, mst_port,
6790 dm_new_connector_state->pbn);
6791 if (dm_new_connector_state->vcpi_slots < 0) {
6792 DRM_DEBUG_ATOMIC("failed finding vcpi slots: %d\n", (int)dm_new_connector_state->vcpi_slots);
6793 return dm_new_connector_state->vcpi_slots;
6798 const struct drm_encoder_helper_funcs amdgpu_dm_encoder_helper_funcs = {
6799 .disable = dm_encoder_helper_disable,
6800 .atomic_check = dm_encoder_helper_atomic_check
6803 static int dm_update_mst_vcpi_slots_for_dsc(struct drm_atomic_state *state,
6804 struct dc_state *dc_state,
6805 struct dsc_mst_fairness_vars *vars)
6807 struct dc_stream_state *stream = NULL;
6808 struct drm_connector *connector;
6809 struct drm_connector_state *new_con_state;
6810 struct amdgpu_dm_connector *aconnector;
6811 struct dm_connector_state *dm_conn_state;
6813 int vcpi, pbn_div, pbn, slot_num = 0;
6815 for_each_new_connector_in_state(state, connector, new_con_state, i) {
6817 aconnector = to_amdgpu_dm_connector(connector);
6819 if (!aconnector->mst_output_port)
6822 if (!new_con_state || !new_con_state->crtc)
6825 dm_conn_state = to_dm_connector_state(new_con_state);
6827 for (j = 0; j < dc_state->stream_count; j++) {
6828 stream = dc_state->streams[j];
6832 if ((struct amdgpu_dm_connector *)stream->dm_stream_context == aconnector)
6841 pbn_div = dm_mst_get_pbn_divider(stream->link);
6842 /* pbn is calculated by compute_mst_dsc_configs_for_state*/
6843 for (j = 0; j < dc_state->stream_count; j++) {
6844 if (vars[j].aconnector == aconnector) {
6850 if (j == dc_state->stream_count)
6853 slot_num = DIV_ROUND_UP(pbn, pbn_div);
6855 if (stream->timing.flags.DSC != 1) {
6856 dm_conn_state->pbn = pbn;
6857 dm_conn_state->vcpi_slots = slot_num;
6859 ret = drm_dp_mst_atomic_enable_dsc(state, aconnector->mst_output_port,
6860 dm_conn_state->pbn, false);
6867 vcpi = drm_dp_mst_atomic_enable_dsc(state, aconnector->mst_output_port, pbn, true);
6871 dm_conn_state->pbn = pbn;
6872 dm_conn_state->vcpi_slots = vcpi;
6877 static int to_drm_connector_type(enum signal_type st)
6880 case SIGNAL_TYPE_HDMI_TYPE_A:
6881 return DRM_MODE_CONNECTOR_HDMIA;
6882 case SIGNAL_TYPE_EDP:
6883 return DRM_MODE_CONNECTOR_eDP;
6884 case SIGNAL_TYPE_LVDS:
6885 return DRM_MODE_CONNECTOR_LVDS;
6886 case SIGNAL_TYPE_RGB:
6887 return DRM_MODE_CONNECTOR_VGA;
6888 case SIGNAL_TYPE_DISPLAY_PORT:
6889 case SIGNAL_TYPE_DISPLAY_PORT_MST:
6890 return DRM_MODE_CONNECTOR_DisplayPort;
6891 case SIGNAL_TYPE_DVI_DUAL_LINK:
6892 case SIGNAL_TYPE_DVI_SINGLE_LINK:
6893 return DRM_MODE_CONNECTOR_DVID;
6894 case SIGNAL_TYPE_VIRTUAL:
6895 return DRM_MODE_CONNECTOR_VIRTUAL;
6898 return DRM_MODE_CONNECTOR_Unknown;
6902 static struct drm_encoder *amdgpu_dm_connector_to_encoder(struct drm_connector *connector)
6904 struct drm_encoder *encoder;
6906 /* There is only one encoder per connector */
6907 drm_connector_for_each_possible_encoder(connector, encoder)
6913 static void amdgpu_dm_get_native_mode(struct drm_connector *connector)
6915 struct drm_encoder *encoder;
6916 struct amdgpu_encoder *amdgpu_encoder;
6918 encoder = amdgpu_dm_connector_to_encoder(connector);
6920 if (encoder == NULL)
6923 amdgpu_encoder = to_amdgpu_encoder(encoder);
6925 amdgpu_encoder->native_mode.clock = 0;
6927 if (!list_empty(&connector->probed_modes)) {
6928 struct drm_display_mode *preferred_mode = NULL;
6930 list_for_each_entry(preferred_mode,
6931 &connector->probed_modes,
6933 if (preferred_mode->type & DRM_MODE_TYPE_PREFERRED)
6934 amdgpu_encoder->native_mode = *preferred_mode;
6942 static struct drm_display_mode *
6943 amdgpu_dm_create_common_mode(struct drm_encoder *encoder,
6945 int hdisplay, int vdisplay)
6947 struct drm_device *dev = encoder->dev;
6948 struct amdgpu_encoder *amdgpu_encoder = to_amdgpu_encoder(encoder);
6949 struct drm_display_mode *mode = NULL;
6950 struct drm_display_mode *native_mode = &amdgpu_encoder->native_mode;
6952 mode = drm_mode_duplicate(dev, native_mode);
6957 mode->hdisplay = hdisplay;
6958 mode->vdisplay = vdisplay;
6959 mode->type &= ~DRM_MODE_TYPE_PREFERRED;
6960 strscpy(mode->name, name, DRM_DISPLAY_MODE_LEN);
6966 static void amdgpu_dm_connector_add_common_modes(struct drm_encoder *encoder,
6967 struct drm_connector *connector)
6969 struct amdgpu_encoder *amdgpu_encoder = to_amdgpu_encoder(encoder);
6970 struct drm_display_mode *mode = NULL;
6971 struct drm_display_mode *native_mode = &amdgpu_encoder->native_mode;
6972 struct amdgpu_dm_connector *amdgpu_dm_connector =
6973 to_amdgpu_dm_connector(connector);
6977 char name[DRM_DISPLAY_MODE_LEN];
6980 } common_modes[] = {
6981 { "640x480", 640, 480},
6982 { "800x600", 800, 600},
6983 { "1024x768", 1024, 768},
6984 { "1280x720", 1280, 720},
6985 { "1280x800", 1280, 800},
6986 {"1280x1024", 1280, 1024},
6987 { "1440x900", 1440, 900},
6988 {"1680x1050", 1680, 1050},
6989 {"1600x1200", 1600, 1200},
6990 {"1920x1080", 1920, 1080},
6991 {"1920x1200", 1920, 1200}
6994 n = ARRAY_SIZE(common_modes);
6996 for (i = 0; i < n; i++) {
6997 struct drm_display_mode *curmode = NULL;
6998 bool mode_existed = false;
7000 if (common_modes[i].w > native_mode->hdisplay ||
7001 common_modes[i].h > native_mode->vdisplay ||
7002 (common_modes[i].w == native_mode->hdisplay &&
7003 common_modes[i].h == native_mode->vdisplay))
7006 list_for_each_entry(curmode, &connector->probed_modes, head) {
7007 if (common_modes[i].w == curmode->hdisplay &&
7008 common_modes[i].h == curmode->vdisplay) {
7009 mode_existed = true;
7017 mode = amdgpu_dm_create_common_mode(encoder,
7018 common_modes[i].name, common_modes[i].w,
7023 drm_mode_probed_add(connector, mode);
7024 amdgpu_dm_connector->num_modes++;
7028 static void amdgpu_set_panel_orientation(struct drm_connector *connector)
7030 struct drm_encoder *encoder;
7031 struct amdgpu_encoder *amdgpu_encoder;
7032 const struct drm_display_mode *native_mode;
7034 if (connector->connector_type != DRM_MODE_CONNECTOR_eDP &&
7035 connector->connector_type != DRM_MODE_CONNECTOR_LVDS)
7038 mutex_lock(&connector->dev->mode_config.mutex);
7039 amdgpu_dm_connector_get_modes(connector);
7040 mutex_unlock(&connector->dev->mode_config.mutex);
7042 encoder = amdgpu_dm_connector_to_encoder(connector);
7046 amdgpu_encoder = to_amdgpu_encoder(encoder);
7048 native_mode = &amdgpu_encoder->native_mode;
7049 if (native_mode->hdisplay == 0 || native_mode->vdisplay == 0)
7052 drm_connector_set_panel_orientation_with_quirk(connector,
7053 DRM_MODE_PANEL_ORIENTATION_UNKNOWN,
7054 native_mode->hdisplay,
7055 native_mode->vdisplay);
7058 static void amdgpu_dm_connector_ddc_get_modes(struct drm_connector *connector,
7061 struct amdgpu_dm_connector *amdgpu_dm_connector =
7062 to_amdgpu_dm_connector(connector);
7065 /* empty probed_modes */
7066 INIT_LIST_HEAD(&connector->probed_modes);
7067 amdgpu_dm_connector->num_modes =
7068 drm_add_edid_modes(connector, edid);
7070 /* sorting the probed modes before calling function
7071 * amdgpu_dm_get_native_mode() since EDID can have
7072 * more than one preferred mode. The modes that are
7073 * later in the probed mode list could be of higher
7074 * and preferred resolution. For example, 3840x2160
7075 * resolution in base EDID preferred timing and 4096x2160
7076 * preferred resolution in DID extension block later.
7078 drm_mode_sort(&connector->probed_modes);
7079 amdgpu_dm_get_native_mode(connector);
7081 /* Freesync capabilities are reset by calling
7082 * drm_add_edid_modes() and need to be
7085 amdgpu_dm_update_freesync_caps(connector, edid);
7087 amdgpu_dm_connector->num_modes = 0;
7091 static bool is_duplicate_mode(struct amdgpu_dm_connector *aconnector,
7092 struct drm_display_mode *mode)
7094 struct drm_display_mode *m;
7096 list_for_each_entry(m, &aconnector->base.probed_modes, head) {
7097 if (drm_mode_equal(m, mode))
7104 static uint add_fs_modes(struct amdgpu_dm_connector *aconnector)
7106 const struct drm_display_mode *m;
7107 struct drm_display_mode *new_mode;
7109 u32 new_modes_count = 0;
7111 /* Standard FPS values
7120 * 60 - Commonly used
7121 * 48,72,96,120 - Multiples of 24
7123 static const u32 common_rates[] = {
7124 23976, 24000, 25000, 29970, 30000,
7125 48000, 50000, 60000, 72000, 96000, 120000
7129 * Find mode with highest refresh rate with the same resolution
7130 * as the preferred mode. Some monitors report a preferred mode
7131 * with lower resolution than the highest refresh rate supported.
7134 m = get_highest_refresh_rate_mode(aconnector, true);
7138 for (i = 0; i < ARRAY_SIZE(common_rates); i++) {
7139 u64 target_vtotal, target_vtotal_diff;
7142 if (drm_mode_vrefresh(m) * 1000 < common_rates[i])
7145 if (common_rates[i] < aconnector->min_vfreq * 1000 ||
7146 common_rates[i] > aconnector->max_vfreq * 1000)
7149 num = (unsigned long long)m->clock * 1000 * 1000;
7150 den = common_rates[i] * (unsigned long long)m->htotal;
7151 target_vtotal = div_u64(num, den);
7152 target_vtotal_diff = target_vtotal - m->vtotal;
7154 /* Check for illegal modes */
7155 if (m->vsync_start + target_vtotal_diff < m->vdisplay ||
7156 m->vsync_end + target_vtotal_diff < m->vsync_start ||
7157 m->vtotal + target_vtotal_diff < m->vsync_end)
7160 new_mode = drm_mode_duplicate(aconnector->base.dev, m);
7164 new_mode->vtotal += (u16)target_vtotal_diff;
7165 new_mode->vsync_start += (u16)target_vtotal_diff;
7166 new_mode->vsync_end += (u16)target_vtotal_diff;
7167 new_mode->type &= ~DRM_MODE_TYPE_PREFERRED;
7168 new_mode->type |= DRM_MODE_TYPE_DRIVER;
7170 if (!is_duplicate_mode(aconnector, new_mode)) {
7171 drm_mode_probed_add(&aconnector->base, new_mode);
7172 new_modes_count += 1;
7174 drm_mode_destroy(aconnector->base.dev, new_mode);
7177 return new_modes_count;
7180 static void amdgpu_dm_connector_add_freesync_modes(struct drm_connector *connector,
7183 struct amdgpu_dm_connector *amdgpu_dm_connector =
7184 to_amdgpu_dm_connector(connector);
7189 if (amdgpu_dm_connector->max_vfreq - amdgpu_dm_connector->min_vfreq > 10)
7190 amdgpu_dm_connector->num_modes +=
7191 add_fs_modes(amdgpu_dm_connector);
7194 static int amdgpu_dm_connector_get_modes(struct drm_connector *connector)
7196 struct amdgpu_dm_connector *amdgpu_dm_connector =
7197 to_amdgpu_dm_connector(connector);
7198 struct drm_encoder *encoder;
7199 struct edid *edid = amdgpu_dm_connector->edid;
7200 struct dc_link_settings *verified_link_cap =
7201 &amdgpu_dm_connector->dc_link->verified_link_cap;
7202 const struct dc *dc = amdgpu_dm_connector->dc_link->dc;
7204 encoder = amdgpu_dm_connector_to_encoder(connector);
7206 if (!drm_edid_is_valid(edid)) {
7207 amdgpu_dm_connector->num_modes =
7208 drm_add_modes_noedid(connector, 640, 480);
7209 if (dc->link_srv->dp_get_encoding_format(verified_link_cap) == DP_128b_132b_ENCODING)
7210 amdgpu_dm_connector->num_modes +=
7211 drm_add_modes_noedid(connector, 1920, 1080);
7213 amdgpu_dm_connector_ddc_get_modes(connector, edid);
7214 amdgpu_dm_connector_add_common_modes(encoder, connector);
7215 amdgpu_dm_connector_add_freesync_modes(connector, edid);
7217 amdgpu_dm_fbc_init(connector);
7219 return amdgpu_dm_connector->num_modes;
7222 static const u32 supported_colorspaces =
7223 BIT(DRM_MODE_COLORIMETRY_BT709_YCC) |
7224 BIT(DRM_MODE_COLORIMETRY_OPRGB) |
7225 BIT(DRM_MODE_COLORIMETRY_BT2020_RGB) |
7226 BIT(DRM_MODE_COLORIMETRY_BT2020_YCC);
7228 void amdgpu_dm_connector_init_helper(struct amdgpu_display_manager *dm,
7229 struct amdgpu_dm_connector *aconnector,
7231 struct dc_link *link,
7234 struct amdgpu_device *adev = drm_to_adev(dm->ddev);
7237 * Some of the properties below require access to state, like bpc.
7238 * Allocate some default initial connector state with our reset helper.
7240 if (aconnector->base.funcs->reset)
7241 aconnector->base.funcs->reset(&aconnector->base);
7243 aconnector->connector_id = link_index;
7244 aconnector->bl_idx = -1;
7245 aconnector->dc_link = link;
7246 aconnector->base.interlace_allowed = false;
7247 aconnector->base.doublescan_allowed = false;
7248 aconnector->base.stereo_allowed = false;
7249 aconnector->base.dpms = DRM_MODE_DPMS_OFF;
7250 aconnector->hpd.hpd = AMDGPU_HPD_NONE; /* not used */
7251 aconnector->audio_inst = -1;
7252 aconnector->pack_sdp_v1_3 = false;
7253 aconnector->as_type = ADAPTIVE_SYNC_TYPE_NONE;
7254 memset(&aconnector->vsdb_info, 0, sizeof(aconnector->vsdb_info));
7255 mutex_init(&aconnector->hpd_lock);
7256 mutex_init(&aconnector->handle_mst_msg_ready);
7259 * configure support HPD hot plug connector_>polled default value is 0
7260 * which means HPD hot plug not supported
7262 switch (connector_type) {
7263 case DRM_MODE_CONNECTOR_HDMIA:
7264 aconnector->base.polled = DRM_CONNECTOR_POLL_HPD;
7265 aconnector->base.ycbcr_420_allowed =
7266 link->link_enc->features.hdmi_ycbcr420_supported ? true : false;
7268 case DRM_MODE_CONNECTOR_DisplayPort:
7269 aconnector->base.polled = DRM_CONNECTOR_POLL_HPD;
7270 link->link_enc = link_enc_cfg_get_link_enc(link);
7271 ASSERT(link->link_enc);
7273 aconnector->base.ycbcr_420_allowed =
7274 link->link_enc->features.dp_ycbcr420_supported ? true : false;
7276 case DRM_MODE_CONNECTOR_DVID:
7277 aconnector->base.polled = DRM_CONNECTOR_POLL_HPD;
7283 drm_object_attach_property(&aconnector->base.base,
7284 dm->ddev->mode_config.scaling_mode_property,
7285 DRM_MODE_SCALE_NONE);
7287 drm_object_attach_property(&aconnector->base.base,
7288 adev->mode_info.underscan_property,
7290 drm_object_attach_property(&aconnector->base.base,
7291 adev->mode_info.underscan_hborder_property,
7293 drm_object_attach_property(&aconnector->base.base,
7294 adev->mode_info.underscan_vborder_property,
7297 if (!aconnector->mst_root)
7298 drm_connector_attach_max_bpc_property(&aconnector->base, 8, 16);
7300 aconnector->base.state->max_bpc = 16;
7301 aconnector->base.state->max_requested_bpc = aconnector->base.state->max_bpc;
7303 if (connector_type == DRM_MODE_CONNECTOR_eDP &&
7304 (dc_is_dmcu_initialized(adev->dm.dc) || adev->dm.dc->ctx->dmub_srv)) {
7305 drm_object_attach_property(&aconnector->base.base,
7306 adev->mode_info.abm_level_property, 0);
7309 if (connector_type == DRM_MODE_CONNECTOR_HDMIA) {
7310 if (!drm_mode_create_hdmi_colorspace_property(&aconnector->base, supported_colorspaces))
7311 drm_connector_attach_colorspace_property(&aconnector->base);
7312 } else if ((connector_type == DRM_MODE_CONNECTOR_DisplayPort && !aconnector->mst_root) ||
7313 connector_type == DRM_MODE_CONNECTOR_eDP) {
7314 if (!drm_mode_create_dp_colorspace_property(&aconnector->base, supported_colorspaces))
7315 drm_connector_attach_colorspace_property(&aconnector->base);
7318 if (connector_type == DRM_MODE_CONNECTOR_HDMIA ||
7319 connector_type == DRM_MODE_CONNECTOR_DisplayPort ||
7320 connector_type == DRM_MODE_CONNECTOR_eDP) {
7321 drm_connector_attach_hdr_output_metadata_property(&aconnector->base);
7323 if (!aconnector->mst_root)
7324 drm_connector_attach_vrr_capable_property(&aconnector->base);
7326 if (adev->dm.hdcp_workqueue)
7327 drm_connector_attach_content_protection_property(&aconnector->base, true);
7331 static int amdgpu_dm_i2c_xfer(struct i2c_adapter *i2c_adap,
7332 struct i2c_msg *msgs, int num)
7334 struct amdgpu_i2c_adapter *i2c = i2c_get_adapdata(i2c_adap);
7335 struct ddc_service *ddc_service = i2c->ddc_service;
7336 struct i2c_command cmd;
7340 cmd.payloads = kcalloc(num, sizeof(struct i2c_payload), GFP_KERNEL);
7345 cmd.number_of_payloads = num;
7346 cmd.engine = I2C_COMMAND_ENGINE_DEFAULT;
7349 for (i = 0; i < num; i++) {
7350 cmd.payloads[i].write = !(msgs[i].flags & I2C_M_RD);
7351 cmd.payloads[i].address = msgs[i].addr;
7352 cmd.payloads[i].length = msgs[i].len;
7353 cmd.payloads[i].data = msgs[i].buf;
7357 ddc_service->ctx->dc,
7358 ddc_service->link->link_index,
7362 kfree(cmd.payloads);
7366 static u32 amdgpu_dm_i2c_func(struct i2c_adapter *adap)
7368 return I2C_FUNC_I2C | I2C_FUNC_SMBUS_EMUL;
7371 static const struct i2c_algorithm amdgpu_dm_i2c_algo = {
7372 .master_xfer = amdgpu_dm_i2c_xfer,
7373 .functionality = amdgpu_dm_i2c_func,
7376 static struct amdgpu_i2c_adapter *
7377 create_i2c(struct ddc_service *ddc_service,
7381 struct amdgpu_device *adev = ddc_service->ctx->driver_context;
7382 struct amdgpu_i2c_adapter *i2c;
7384 i2c = kzalloc(sizeof(struct amdgpu_i2c_adapter), GFP_KERNEL);
7387 i2c->base.owner = THIS_MODULE;
7388 i2c->base.class = I2C_CLASS_DDC;
7389 i2c->base.dev.parent = &adev->pdev->dev;
7390 i2c->base.algo = &amdgpu_dm_i2c_algo;
7391 snprintf(i2c->base.name, sizeof(i2c->base.name), "AMDGPU DM i2c hw bus %d", link_index);
7392 i2c_set_adapdata(&i2c->base, i2c);
7393 i2c->ddc_service = ddc_service;
7400 * Note: this function assumes that dc_link_detect() was called for the
7401 * dc_link which will be represented by this aconnector.
7403 static int amdgpu_dm_connector_init(struct amdgpu_display_manager *dm,
7404 struct amdgpu_dm_connector *aconnector,
7406 struct amdgpu_encoder *aencoder)
7410 struct dc *dc = dm->dc;
7411 struct dc_link *link = dc_get_link_at_index(dc, link_index);
7412 struct amdgpu_i2c_adapter *i2c;
7414 link->priv = aconnector;
7417 i2c = create_i2c(link->ddc, link->link_index, &res);
7419 DRM_ERROR("Failed to create i2c adapter data\n");
7423 aconnector->i2c = i2c;
7424 res = i2c_add_adapter(&i2c->base);
7427 DRM_ERROR("Failed to register hw i2c %d\n", link->link_index);
7431 connector_type = to_drm_connector_type(link->connector_signal);
7433 res = drm_connector_init_with_ddc(
7436 &amdgpu_dm_connector_funcs,
7441 DRM_ERROR("connector_init failed\n");
7442 aconnector->connector_id = -1;
7446 drm_connector_helper_add(
7448 &amdgpu_dm_connector_helper_funcs);
7450 amdgpu_dm_connector_init_helper(
7457 drm_connector_attach_encoder(
7458 &aconnector->base, &aencoder->base);
7460 if (connector_type == DRM_MODE_CONNECTOR_DisplayPort
7461 || connector_type == DRM_MODE_CONNECTOR_eDP)
7462 amdgpu_dm_initialize_dp_connector(dm, aconnector, link->link_index);
7467 aconnector->i2c = NULL;
7472 int amdgpu_dm_get_encoder_crtc_mask(struct amdgpu_device *adev)
7474 switch (adev->mode_info.num_crtc) {
7491 static int amdgpu_dm_encoder_init(struct drm_device *dev,
7492 struct amdgpu_encoder *aencoder,
7493 uint32_t link_index)
7495 struct amdgpu_device *adev = drm_to_adev(dev);
7497 int res = drm_encoder_init(dev,
7499 &amdgpu_dm_encoder_funcs,
7500 DRM_MODE_ENCODER_TMDS,
7503 aencoder->base.possible_crtcs = amdgpu_dm_get_encoder_crtc_mask(adev);
7506 aencoder->encoder_id = link_index;
7508 aencoder->encoder_id = -1;
7510 drm_encoder_helper_add(&aencoder->base, &amdgpu_dm_encoder_helper_funcs);
7515 static void manage_dm_interrupts(struct amdgpu_device *adev,
7516 struct amdgpu_crtc *acrtc,
7520 * We have no guarantee that the frontend index maps to the same
7521 * backend index - some even map to more than one.
7523 * TODO: Use a different interrupt or check DC itself for the mapping.
7526 amdgpu_display_crtc_idx_to_irq_type(
7531 drm_crtc_vblank_on(&acrtc->base);
7534 &adev->pageflip_irq,
7536 #if defined(CONFIG_DRM_AMD_SECURE_DISPLAY)
7543 #if defined(CONFIG_DRM_AMD_SECURE_DISPLAY)
7551 &adev->pageflip_irq,
7553 drm_crtc_vblank_off(&acrtc->base);
7557 static void dm_update_pflip_irq_state(struct amdgpu_device *adev,
7558 struct amdgpu_crtc *acrtc)
7561 amdgpu_display_crtc_idx_to_irq_type(adev, acrtc->crtc_id);
7564 * This reads the current state for the IRQ and force reapplies
7565 * the setting to hardware.
7567 amdgpu_irq_update(adev, &adev->pageflip_irq, irq_type);
7571 is_scaling_state_different(const struct dm_connector_state *dm_state,
7572 const struct dm_connector_state *old_dm_state)
7574 if (dm_state->scaling != old_dm_state->scaling)
7576 if (!dm_state->underscan_enable && old_dm_state->underscan_enable) {
7577 if (old_dm_state->underscan_hborder != 0 && old_dm_state->underscan_vborder != 0)
7579 } else if (dm_state->underscan_enable && !old_dm_state->underscan_enable) {
7580 if (dm_state->underscan_hborder != 0 && dm_state->underscan_vborder != 0)
7582 } else if (dm_state->underscan_hborder != old_dm_state->underscan_hborder ||
7583 dm_state->underscan_vborder != old_dm_state->underscan_vborder)
7588 static bool is_content_protection_different(struct drm_crtc_state *new_crtc_state,
7589 struct drm_crtc_state *old_crtc_state,
7590 struct drm_connector_state *new_conn_state,
7591 struct drm_connector_state *old_conn_state,
7592 const struct drm_connector *connector,
7593 struct hdcp_workqueue *hdcp_w)
7595 struct amdgpu_dm_connector *aconnector = to_amdgpu_dm_connector(connector);
7596 struct dm_connector_state *dm_con_state = to_dm_connector_state(connector->state);
7598 pr_debug("[HDCP_DM] connector->index: %x connect_status: %x dpms: %x\n",
7599 connector->index, connector->status, connector->dpms);
7600 pr_debug("[HDCP_DM] state protection old: %x new: %x\n",
7601 old_conn_state->content_protection, new_conn_state->content_protection);
7604 pr_debug("[HDCP_DM] old crtc en: %x a: %x m: %x a-chg: %x c-chg: %x\n",
7605 old_crtc_state->enable,
7606 old_crtc_state->active,
7607 old_crtc_state->mode_changed,
7608 old_crtc_state->active_changed,
7609 old_crtc_state->connectors_changed);
7612 pr_debug("[HDCP_DM] NEW crtc en: %x a: %x m: %x a-chg: %x c-chg: %x\n",
7613 new_crtc_state->enable,
7614 new_crtc_state->active,
7615 new_crtc_state->mode_changed,
7616 new_crtc_state->active_changed,
7617 new_crtc_state->connectors_changed);
7619 /* hdcp content type change */
7620 if (old_conn_state->hdcp_content_type != new_conn_state->hdcp_content_type &&
7621 new_conn_state->content_protection != DRM_MODE_CONTENT_PROTECTION_UNDESIRED) {
7622 new_conn_state->content_protection = DRM_MODE_CONTENT_PROTECTION_DESIRED;
7623 pr_debug("[HDCP_DM] Type0/1 change %s :true\n", __func__);
7627 /* CP is being re enabled, ignore this */
7628 if (old_conn_state->content_protection == DRM_MODE_CONTENT_PROTECTION_ENABLED &&
7629 new_conn_state->content_protection == DRM_MODE_CONTENT_PROTECTION_DESIRED) {
7630 if (new_crtc_state && new_crtc_state->mode_changed) {
7631 new_conn_state->content_protection = DRM_MODE_CONTENT_PROTECTION_DESIRED;
7632 pr_debug("[HDCP_DM] ENABLED->DESIRED & mode_changed %s :true\n", __func__);
7635 new_conn_state->content_protection = DRM_MODE_CONTENT_PROTECTION_ENABLED;
7636 pr_debug("[HDCP_DM] ENABLED -> DESIRED %s :false\n", __func__);
7640 /* S3 resume case, since old state will always be 0 (UNDESIRED) and the restored state will be ENABLED
7642 * Handles: UNDESIRED -> ENABLED
7644 if (old_conn_state->content_protection == DRM_MODE_CONTENT_PROTECTION_UNDESIRED &&
7645 new_conn_state->content_protection == DRM_MODE_CONTENT_PROTECTION_ENABLED)
7646 new_conn_state->content_protection = DRM_MODE_CONTENT_PROTECTION_DESIRED;
7648 /* Stream removed and re-enabled
7650 * Can sometimes overlap with the HPD case,
7651 * thus set update_hdcp to false to avoid
7652 * setting HDCP multiple times.
7654 * Handles: DESIRED -> DESIRED (Special case)
7656 if (!(old_conn_state->crtc && old_conn_state->crtc->enabled) &&
7657 new_conn_state->crtc && new_conn_state->crtc->enabled &&
7658 connector->state->content_protection == DRM_MODE_CONTENT_PROTECTION_DESIRED) {
7659 dm_con_state->update_hdcp = false;
7660 pr_debug("[HDCP_DM] DESIRED->DESIRED (Stream removed and re-enabled) %s :true\n",
7665 /* Hot-plug, headless s3, dpms
7667 * Only start HDCP if the display is connected/enabled.
7668 * update_hdcp flag will be set to false until the next
7671 * Handles: DESIRED -> DESIRED (Special case)
7673 if (dm_con_state->update_hdcp &&
7674 new_conn_state->content_protection == DRM_MODE_CONTENT_PROTECTION_DESIRED &&
7675 connector->dpms == DRM_MODE_DPMS_ON && aconnector->dc_sink != NULL) {
7676 dm_con_state->update_hdcp = false;
7677 pr_debug("[HDCP_DM] DESIRED->DESIRED (Hot-plug, headless s3, dpms) %s :true\n",
7682 if (old_conn_state->content_protection == new_conn_state->content_protection) {
7683 if (new_conn_state->content_protection >= DRM_MODE_CONTENT_PROTECTION_DESIRED) {
7684 if (new_crtc_state && new_crtc_state->mode_changed) {
7685 pr_debug("[HDCP_DM] DESIRED->DESIRED or ENABLE->ENABLE mode_change %s :true\n",
7689 pr_debug("[HDCP_DM] DESIRED->DESIRED & ENABLE->ENABLE %s :false\n",
7694 pr_debug("[HDCP_DM] UNDESIRED->UNDESIRED %s :false\n", __func__);
7698 if (new_conn_state->content_protection != DRM_MODE_CONTENT_PROTECTION_ENABLED) {
7699 pr_debug("[HDCP_DM] UNDESIRED->DESIRED or DESIRED->UNDESIRED or ENABLED->UNDESIRED %s :true\n",
7704 pr_debug("[HDCP_DM] DESIRED->ENABLED %s :false\n", __func__);
7708 static void remove_stream(struct amdgpu_device *adev,
7709 struct amdgpu_crtc *acrtc,
7710 struct dc_stream_state *stream)
7712 /* this is the update mode case */
7714 acrtc->otg_inst = -1;
7715 acrtc->enabled = false;
7718 static void prepare_flip_isr(struct amdgpu_crtc *acrtc)
7721 assert_spin_locked(&acrtc->base.dev->event_lock);
7722 WARN_ON(acrtc->event);
7724 acrtc->event = acrtc->base.state->event;
7726 /* Set the flip status */
7727 acrtc->pflip_status = AMDGPU_FLIP_SUBMITTED;
7729 /* Mark this event as consumed */
7730 acrtc->base.state->event = NULL;
7732 DC_LOG_PFLIP("crtc:%d, pflip_stat:AMDGPU_FLIP_SUBMITTED\n",
7736 static void update_freesync_state_on_stream(
7737 struct amdgpu_display_manager *dm,
7738 struct dm_crtc_state *new_crtc_state,
7739 struct dc_stream_state *new_stream,
7740 struct dc_plane_state *surface,
7741 u32 flip_timestamp_in_us)
7743 struct mod_vrr_params vrr_params;
7744 struct dc_info_packet vrr_infopacket = {0};
7745 struct amdgpu_device *adev = dm->adev;
7746 struct amdgpu_crtc *acrtc = to_amdgpu_crtc(new_crtc_state->base.crtc);
7747 unsigned long flags;
7748 bool pack_sdp_v1_3 = false;
7749 struct amdgpu_dm_connector *aconn;
7750 enum vrr_packet_type packet_type = PACKET_TYPE_VRR;
7756 * TODO: Determine why min/max totals and vrefresh can be 0 here.
7757 * For now it's sufficient to just guard against these conditions.
7760 if (!new_stream->timing.h_total || !new_stream->timing.v_total)
7763 spin_lock_irqsave(&adev_to_drm(adev)->event_lock, flags);
7764 vrr_params = acrtc->dm_irq_params.vrr_params;
7767 mod_freesync_handle_preflip(
7768 dm->freesync_module,
7771 flip_timestamp_in_us,
7774 if (adev->family < AMDGPU_FAMILY_AI &&
7775 amdgpu_dm_crtc_vrr_active(new_crtc_state)) {
7776 mod_freesync_handle_v_update(dm->freesync_module,
7777 new_stream, &vrr_params);
7779 /* Need to call this before the frame ends. */
7780 dc_stream_adjust_vmin_vmax(dm->dc,
7781 new_crtc_state->stream,
7782 &vrr_params.adjust);
7786 aconn = (struct amdgpu_dm_connector *)new_stream->dm_stream_context;
7788 if (aconn && (aconn->as_type == FREESYNC_TYPE_PCON_IN_WHITELIST || aconn->vsdb_info.replay_mode)) {
7789 pack_sdp_v1_3 = aconn->pack_sdp_v1_3;
7791 if (aconn->vsdb_info.amd_vsdb_version == 1)
7792 packet_type = PACKET_TYPE_FS_V1;
7793 else if (aconn->vsdb_info.amd_vsdb_version == 2)
7794 packet_type = PACKET_TYPE_FS_V2;
7795 else if (aconn->vsdb_info.amd_vsdb_version == 3)
7796 packet_type = PACKET_TYPE_FS_V3;
7798 mod_build_adaptive_sync_infopacket(new_stream, aconn->as_type, NULL,
7799 &new_stream->adaptive_sync_infopacket);
7802 mod_freesync_build_vrr_infopacket(
7803 dm->freesync_module,
7807 TRANSFER_FUNC_UNKNOWN,
7811 new_crtc_state->freesync_vrr_info_changed |=
7812 (memcmp(&new_crtc_state->vrr_infopacket,
7814 sizeof(vrr_infopacket)) != 0);
7816 acrtc->dm_irq_params.vrr_params = vrr_params;
7817 new_crtc_state->vrr_infopacket = vrr_infopacket;
7819 new_stream->vrr_infopacket = vrr_infopacket;
7820 new_stream->allow_freesync = mod_freesync_get_freesync_enabled(&vrr_params);
7822 if (new_crtc_state->freesync_vrr_info_changed)
7823 DRM_DEBUG_KMS("VRR packet update: crtc=%u enabled=%d state=%d",
7824 new_crtc_state->base.crtc->base.id,
7825 (int)new_crtc_state->base.vrr_enabled,
7826 (int)vrr_params.state);
7828 spin_unlock_irqrestore(&adev_to_drm(adev)->event_lock, flags);
7831 static void update_stream_irq_parameters(
7832 struct amdgpu_display_manager *dm,
7833 struct dm_crtc_state *new_crtc_state)
7835 struct dc_stream_state *new_stream = new_crtc_state->stream;
7836 struct mod_vrr_params vrr_params;
7837 struct mod_freesync_config config = new_crtc_state->freesync_config;
7838 struct amdgpu_device *adev = dm->adev;
7839 struct amdgpu_crtc *acrtc = to_amdgpu_crtc(new_crtc_state->base.crtc);
7840 unsigned long flags;
7846 * TODO: Determine why min/max totals and vrefresh can be 0 here.
7847 * For now it's sufficient to just guard against these conditions.
7849 if (!new_stream->timing.h_total || !new_stream->timing.v_total)
7852 spin_lock_irqsave(&adev_to_drm(adev)->event_lock, flags);
7853 vrr_params = acrtc->dm_irq_params.vrr_params;
7855 if (new_crtc_state->vrr_supported &&
7856 config.min_refresh_in_uhz &&
7857 config.max_refresh_in_uhz) {
7859 * if freesync compatible mode was set, config.state will be set
7862 if (config.state == VRR_STATE_ACTIVE_FIXED && config.fixed_refresh_in_uhz &&
7863 (!drm_atomic_crtc_needs_modeset(&new_crtc_state->base) ||
7864 new_crtc_state->freesync_config.state == VRR_STATE_ACTIVE_FIXED)) {
7865 vrr_params.max_refresh_in_uhz = config.max_refresh_in_uhz;
7866 vrr_params.min_refresh_in_uhz = config.min_refresh_in_uhz;
7867 vrr_params.fixed_refresh_in_uhz = config.fixed_refresh_in_uhz;
7868 vrr_params.state = VRR_STATE_ACTIVE_FIXED;
7870 config.state = new_crtc_state->base.vrr_enabled ?
7871 VRR_STATE_ACTIVE_VARIABLE :
7875 config.state = VRR_STATE_UNSUPPORTED;
7878 mod_freesync_build_vrr_params(dm->freesync_module,
7880 &config, &vrr_params);
7882 new_crtc_state->freesync_config = config;
7883 /* Copy state for access from DM IRQ handler */
7884 acrtc->dm_irq_params.freesync_config = config;
7885 acrtc->dm_irq_params.active_planes = new_crtc_state->active_planes;
7886 acrtc->dm_irq_params.vrr_params = vrr_params;
7887 spin_unlock_irqrestore(&adev_to_drm(adev)->event_lock, flags);
7890 static void amdgpu_dm_handle_vrr_transition(struct dm_crtc_state *old_state,
7891 struct dm_crtc_state *new_state)
7893 bool old_vrr_active = amdgpu_dm_crtc_vrr_active(old_state);
7894 bool new_vrr_active = amdgpu_dm_crtc_vrr_active(new_state);
7896 if (!old_vrr_active && new_vrr_active) {
7897 /* Transition VRR inactive -> active:
7898 * While VRR is active, we must not disable vblank irq, as a
7899 * reenable after disable would compute bogus vblank/pflip
7900 * timestamps if it likely happened inside display front-porch.
7902 * We also need vupdate irq for the actual core vblank handling
7905 WARN_ON(amdgpu_dm_crtc_set_vupdate_irq(new_state->base.crtc, true) != 0);
7906 WARN_ON(drm_crtc_vblank_get(new_state->base.crtc) != 0);
7907 DRM_DEBUG_DRIVER("%s: crtc=%u VRR off->on: Get vblank ref\n",
7908 __func__, new_state->base.crtc->base.id);
7909 } else if (old_vrr_active && !new_vrr_active) {
7910 /* Transition VRR active -> inactive:
7911 * Allow vblank irq disable again for fixed refresh rate.
7913 WARN_ON(amdgpu_dm_crtc_set_vupdate_irq(new_state->base.crtc, false) != 0);
7914 drm_crtc_vblank_put(new_state->base.crtc);
7915 DRM_DEBUG_DRIVER("%s: crtc=%u VRR on->off: Drop vblank ref\n",
7916 __func__, new_state->base.crtc->base.id);
7920 static void amdgpu_dm_commit_cursors(struct drm_atomic_state *state)
7922 struct drm_plane *plane;
7923 struct drm_plane_state *old_plane_state;
7927 * TODO: Make this per-stream so we don't issue redundant updates for
7928 * commits with multiple streams.
7930 for_each_old_plane_in_state(state, plane, old_plane_state, i)
7931 if (plane->type == DRM_PLANE_TYPE_CURSOR)
7932 amdgpu_dm_plane_handle_cursor_update(plane, old_plane_state);
7935 static inline uint32_t get_mem_type(struct drm_framebuffer *fb)
7937 struct amdgpu_bo *abo = gem_to_amdgpu_bo(fb->obj[0]);
7939 return abo->tbo.resource ? abo->tbo.resource->mem_type : 0;
7942 static void amdgpu_dm_commit_planes(struct drm_atomic_state *state,
7943 struct drm_device *dev,
7944 struct amdgpu_display_manager *dm,
7945 struct drm_crtc *pcrtc,
7946 bool wait_for_vblank)
7949 u64 timestamp_ns = ktime_get_ns();
7950 struct drm_plane *plane;
7951 struct drm_plane_state *old_plane_state, *new_plane_state;
7952 struct amdgpu_crtc *acrtc_attach = to_amdgpu_crtc(pcrtc);
7953 struct drm_crtc_state *new_pcrtc_state =
7954 drm_atomic_get_new_crtc_state(state, pcrtc);
7955 struct dm_crtc_state *acrtc_state = to_dm_crtc_state(new_pcrtc_state);
7956 struct dm_crtc_state *dm_old_crtc_state =
7957 to_dm_crtc_state(drm_atomic_get_old_crtc_state(state, pcrtc));
7958 int planes_count = 0, vpos, hpos;
7959 unsigned long flags;
7960 u32 target_vblank, last_flip_vblank;
7961 bool vrr_active = amdgpu_dm_crtc_vrr_active(acrtc_state);
7962 bool cursor_update = false;
7963 bool pflip_present = false;
7964 bool dirty_rects_changed = false;
7966 struct dc_surface_update surface_updates[MAX_SURFACES];
7967 struct dc_plane_info plane_infos[MAX_SURFACES];
7968 struct dc_scaling_info scaling_infos[MAX_SURFACES];
7969 struct dc_flip_addrs flip_addrs[MAX_SURFACES];
7970 struct dc_stream_update stream_update;
7973 bundle = kzalloc(sizeof(*bundle), GFP_KERNEL);
7976 dm_error("Failed to allocate update bundle\n");
7981 * Disable the cursor first if we're disabling all the planes.
7982 * It'll remain on the screen after the planes are re-enabled
7985 if (acrtc_state->active_planes == 0)
7986 amdgpu_dm_commit_cursors(state);
7988 /* update planes when needed */
7989 for_each_oldnew_plane_in_state(state, plane, old_plane_state, new_plane_state, i) {
7990 struct drm_crtc *crtc = new_plane_state->crtc;
7991 struct drm_crtc_state *new_crtc_state;
7992 struct drm_framebuffer *fb = new_plane_state->fb;
7993 struct amdgpu_framebuffer *afb = (struct amdgpu_framebuffer *)fb;
7994 bool plane_needs_flip;
7995 struct dc_plane_state *dc_plane;
7996 struct dm_plane_state *dm_new_plane_state = to_dm_plane_state(new_plane_state);
7998 /* Cursor plane is handled after stream updates */
7999 if (plane->type == DRM_PLANE_TYPE_CURSOR) {
8000 if ((fb && crtc == pcrtc) ||
8001 (old_plane_state->fb && old_plane_state->crtc == pcrtc))
8002 cursor_update = true;
8007 if (!fb || !crtc || pcrtc != crtc)
8010 new_crtc_state = drm_atomic_get_new_crtc_state(state, crtc);
8011 if (!new_crtc_state->active)
8014 dc_plane = dm_new_plane_state->dc_state;
8018 bundle->surface_updates[planes_count].surface = dc_plane;
8019 if (new_pcrtc_state->color_mgmt_changed) {
8020 bundle->surface_updates[planes_count].gamma = dc_plane->gamma_correction;
8021 bundle->surface_updates[planes_count].in_transfer_func = dc_plane->in_transfer_func;
8022 bundle->surface_updates[planes_count].gamut_remap_matrix = &dc_plane->gamut_remap_matrix;
8025 amdgpu_dm_plane_fill_dc_scaling_info(dm->adev, new_plane_state,
8026 &bundle->scaling_infos[planes_count]);
8028 bundle->surface_updates[planes_count].scaling_info =
8029 &bundle->scaling_infos[planes_count];
8031 plane_needs_flip = old_plane_state->fb && new_plane_state->fb;
8033 pflip_present = pflip_present || plane_needs_flip;
8035 if (!plane_needs_flip) {
8040 fill_dc_plane_info_and_addr(
8041 dm->adev, new_plane_state,
8043 &bundle->plane_infos[planes_count],
8044 &bundle->flip_addrs[planes_count].address,
8045 afb->tmz_surface, false);
8047 drm_dbg_state(state->dev, "plane: id=%d dcc_en=%d\n",
8048 new_plane_state->plane->index,
8049 bundle->plane_infos[planes_count].dcc.enable);
8051 bundle->surface_updates[planes_count].plane_info =
8052 &bundle->plane_infos[planes_count];
8054 if (acrtc_state->stream->link->psr_settings.psr_feature_enabled) {
8055 fill_dc_dirty_rects(plane, old_plane_state,
8056 new_plane_state, new_crtc_state,
8057 &bundle->flip_addrs[planes_count],
8058 &dirty_rects_changed);
8061 * If the dirty regions changed, PSR-SU need to be disabled temporarily
8062 * and enabled it again after dirty regions are stable to avoid video glitch.
8063 * PSR-SU will be enabled in vblank_control_worker() if user pause the video
8064 * during the PSR-SU was disabled.
8066 if (acrtc_state->stream->link->psr_settings.psr_version >= DC_PSR_VERSION_SU_1 &&
8067 acrtc_attach->dm_irq_params.allow_psr_entry &&
8068 #ifdef CONFIG_DRM_AMD_SECURE_DISPLAY
8069 !amdgpu_dm_crc_window_is_activated(acrtc_state->base.crtc) &&
8071 dirty_rects_changed) {
8072 mutex_lock(&dm->dc_lock);
8073 acrtc_state->stream->link->psr_settings.psr_dirty_rects_change_timestamp_ns =
8075 if (acrtc_state->stream->link->psr_settings.psr_allow_active)
8076 amdgpu_dm_psr_disable(acrtc_state->stream);
8077 mutex_unlock(&dm->dc_lock);
8082 * Only allow immediate flips for fast updates that don't
8083 * change memory domain, FB pitch, DCC state, rotation or
8086 * dm_crtc_helper_atomic_check() only accepts async flips with
8089 if (crtc->state->async_flip &&
8090 (acrtc_state->update_type != UPDATE_TYPE_FAST ||
8091 get_mem_type(old_plane_state->fb) != get_mem_type(fb)))
8092 drm_warn_once(state->dev,
8093 "[PLANE:%d:%s] async flip with non-fast update\n",
8094 plane->base.id, plane->name);
8096 bundle->flip_addrs[planes_count].flip_immediate =
8097 crtc->state->async_flip &&
8098 acrtc_state->update_type == UPDATE_TYPE_FAST &&
8099 get_mem_type(old_plane_state->fb) == get_mem_type(fb);
8101 timestamp_ns = ktime_get_ns();
8102 bundle->flip_addrs[planes_count].flip_timestamp_in_us = div_u64(timestamp_ns, 1000);
8103 bundle->surface_updates[planes_count].flip_addr = &bundle->flip_addrs[planes_count];
8104 bundle->surface_updates[planes_count].surface = dc_plane;
8106 if (!bundle->surface_updates[planes_count].surface) {
8107 DRM_ERROR("No surface for CRTC: id=%d\n",
8108 acrtc_attach->crtc_id);
8112 if (plane == pcrtc->primary)
8113 update_freesync_state_on_stream(
8116 acrtc_state->stream,
8118 bundle->flip_addrs[planes_count].flip_timestamp_in_us);
8120 drm_dbg_state(state->dev, "%s Flipping to hi: 0x%x, low: 0x%x\n",
8122 bundle->flip_addrs[planes_count].address.grph.addr.high_part,
8123 bundle->flip_addrs[planes_count].address.grph.addr.low_part);
8129 if (pflip_present) {
8131 /* Use old throttling in non-vrr fixed refresh rate mode
8132 * to keep flip scheduling based on target vblank counts
8133 * working in a backwards compatible way, e.g., for
8134 * clients using the GLX_OML_sync_control extension or
8135 * DRI3/Present extension with defined target_msc.
8137 last_flip_vblank = amdgpu_get_vblank_counter_kms(pcrtc);
8139 /* For variable refresh rate mode only:
8140 * Get vblank of last completed flip to avoid > 1 vrr
8141 * flips per video frame by use of throttling, but allow
8142 * flip programming anywhere in the possibly large
8143 * variable vrr vblank interval for fine-grained flip
8144 * timing control and more opportunity to avoid stutter
8145 * on late submission of flips.
8147 spin_lock_irqsave(&pcrtc->dev->event_lock, flags);
8148 last_flip_vblank = acrtc_attach->dm_irq_params.last_flip_vblank;
8149 spin_unlock_irqrestore(&pcrtc->dev->event_lock, flags);
8152 target_vblank = last_flip_vblank + wait_for_vblank;
8155 * Wait until we're out of the vertical blank period before the one
8156 * targeted by the flip
8158 while ((acrtc_attach->enabled &&
8159 (amdgpu_display_get_crtc_scanoutpos(dm->ddev, acrtc_attach->crtc_id,
8160 0, &vpos, &hpos, NULL,
8161 NULL, &pcrtc->hwmode)
8162 & (DRM_SCANOUTPOS_VALID | DRM_SCANOUTPOS_IN_VBLANK)) ==
8163 (DRM_SCANOUTPOS_VALID | DRM_SCANOUTPOS_IN_VBLANK) &&
8164 (int)(target_vblank -
8165 amdgpu_get_vblank_counter_kms(pcrtc)) > 0)) {
8166 usleep_range(1000, 1100);
8170 * Prepare the flip event for the pageflip interrupt to handle.
8172 * This only works in the case where we've already turned on the
8173 * appropriate hardware blocks (eg. HUBP) so in the transition case
8174 * from 0 -> n planes we have to skip a hardware generated event
8175 * and rely on sending it from software.
8177 if (acrtc_attach->base.state->event &&
8178 acrtc_state->active_planes > 0) {
8179 drm_crtc_vblank_get(pcrtc);
8181 spin_lock_irqsave(&pcrtc->dev->event_lock, flags);
8183 WARN_ON(acrtc_attach->pflip_status != AMDGPU_FLIP_NONE);
8184 prepare_flip_isr(acrtc_attach);
8186 spin_unlock_irqrestore(&pcrtc->dev->event_lock, flags);
8189 if (acrtc_state->stream) {
8190 if (acrtc_state->freesync_vrr_info_changed)
8191 bundle->stream_update.vrr_infopacket =
8192 &acrtc_state->stream->vrr_infopacket;
8194 } else if (cursor_update && acrtc_state->active_planes > 0 &&
8195 acrtc_attach->base.state->event) {
8196 drm_crtc_vblank_get(pcrtc);
8198 spin_lock_irqsave(&pcrtc->dev->event_lock, flags);
8200 acrtc_attach->event = acrtc_attach->base.state->event;
8201 acrtc_attach->base.state->event = NULL;
8203 spin_unlock_irqrestore(&pcrtc->dev->event_lock, flags);
8206 /* Update the planes if changed or disable if we don't have any. */
8207 if ((planes_count || acrtc_state->active_planes == 0) &&
8208 acrtc_state->stream) {
8210 * If PSR or idle optimizations are enabled then flush out
8211 * any pending work before hardware programming.
8213 if (dm->vblank_control_workqueue)
8214 flush_workqueue(dm->vblank_control_workqueue);
8216 bundle->stream_update.stream = acrtc_state->stream;
8217 if (new_pcrtc_state->mode_changed) {
8218 bundle->stream_update.src = acrtc_state->stream->src;
8219 bundle->stream_update.dst = acrtc_state->stream->dst;
8222 if (new_pcrtc_state->color_mgmt_changed) {
8224 * TODO: This isn't fully correct since we've actually
8225 * already modified the stream in place.
8227 bundle->stream_update.gamut_remap =
8228 &acrtc_state->stream->gamut_remap_matrix;
8229 bundle->stream_update.output_csc_transform =
8230 &acrtc_state->stream->csc_color_matrix;
8231 bundle->stream_update.out_transfer_func =
8232 acrtc_state->stream->out_transfer_func;
8235 acrtc_state->stream->abm_level = acrtc_state->abm_level;
8236 if (acrtc_state->abm_level != dm_old_crtc_state->abm_level)
8237 bundle->stream_update.abm_level = &acrtc_state->abm_level;
8239 mutex_lock(&dm->dc_lock);
8240 if ((acrtc_state->update_type > UPDATE_TYPE_FAST) &&
8241 acrtc_state->stream->link->psr_settings.psr_allow_active)
8242 amdgpu_dm_psr_disable(acrtc_state->stream);
8243 mutex_unlock(&dm->dc_lock);
8246 * If FreeSync state on the stream has changed then we need to
8247 * re-adjust the min/max bounds now that DC doesn't handle this
8248 * as part of commit.
8250 if (is_dc_timing_adjust_needed(dm_old_crtc_state, acrtc_state)) {
8251 spin_lock_irqsave(&pcrtc->dev->event_lock, flags);
8252 dc_stream_adjust_vmin_vmax(
8253 dm->dc, acrtc_state->stream,
8254 &acrtc_attach->dm_irq_params.vrr_params.adjust);
8255 spin_unlock_irqrestore(&pcrtc->dev->event_lock, flags);
8257 mutex_lock(&dm->dc_lock);
8258 update_planes_and_stream_adapter(dm->dc,
8259 acrtc_state->update_type,
8261 acrtc_state->stream,
8262 &bundle->stream_update,
8263 bundle->surface_updates);
8266 * Enable or disable the interrupts on the backend.
8268 * Most pipes are put into power gating when unused.
8270 * When power gating is enabled on a pipe we lose the
8271 * interrupt enablement state when power gating is disabled.
8273 * So we need to update the IRQ control state in hardware
8274 * whenever the pipe turns on (since it could be previously
8275 * power gated) or off (since some pipes can't be power gated
8278 if (dm_old_crtc_state->active_planes != acrtc_state->active_planes)
8279 dm_update_pflip_irq_state(drm_to_adev(dev),
8282 if ((acrtc_state->update_type > UPDATE_TYPE_FAST) &&
8283 acrtc_state->stream->link->psr_settings.psr_version != DC_PSR_VERSION_UNSUPPORTED &&
8284 !acrtc_state->stream->link->psr_settings.psr_feature_enabled)
8285 amdgpu_dm_link_setup_psr(acrtc_state->stream);
8287 /* Decrement skip count when PSR is enabled and we're doing fast updates. */
8288 if (acrtc_state->update_type == UPDATE_TYPE_FAST &&
8289 acrtc_state->stream->link->psr_settings.psr_feature_enabled) {
8290 struct amdgpu_dm_connector *aconn =
8291 (struct amdgpu_dm_connector *)acrtc_state->stream->dm_stream_context;
8293 if (aconn->psr_skip_count > 0)
8294 aconn->psr_skip_count--;
8296 /* Allow PSR when skip count is 0. */
8297 acrtc_attach->dm_irq_params.allow_psr_entry = !aconn->psr_skip_count;
8300 * If sink supports PSR SU, there is no need to rely on
8301 * a vblank event disable request to enable PSR. PSR SU
8302 * can be enabled immediately once OS demonstrates an
8303 * adequate number of fast atomic commits to notify KMD
8304 * of update events. See `vblank_control_worker()`.
8306 if (acrtc_state->stream->link->psr_settings.psr_version >= DC_PSR_VERSION_SU_1 &&
8307 acrtc_attach->dm_irq_params.allow_psr_entry &&
8308 #ifdef CONFIG_DRM_AMD_SECURE_DISPLAY
8309 !amdgpu_dm_crc_window_is_activated(acrtc_state->base.crtc) &&
8311 !acrtc_state->stream->link->psr_settings.psr_allow_active &&
8313 acrtc_state->stream->link->psr_settings.psr_dirty_rects_change_timestamp_ns) >
8315 amdgpu_dm_psr_enable(acrtc_state->stream);
8317 acrtc_attach->dm_irq_params.allow_psr_entry = false;
8320 mutex_unlock(&dm->dc_lock);
8324 * Update cursor state *after* programming all the planes.
8325 * This avoids redundant programming in the case where we're going
8326 * to be disabling a single plane - those pipes are being disabled.
8328 if (acrtc_state->active_planes)
8329 amdgpu_dm_commit_cursors(state);
8335 static void amdgpu_dm_commit_audio(struct drm_device *dev,
8336 struct drm_atomic_state *state)
8338 struct amdgpu_device *adev = drm_to_adev(dev);
8339 struct amdgpu_dm_connector *aconnector;
8340 struct drm_connector *connector;
8341 struct drm_connector_state *old_con_state, *new_con_state;
8342 struct drm_crtc_state *new_crtc_state;
8343 struct dm_crtc_state *new_dm_crtc_state;
8344 const struct dc_stream_status *status;
8347 /* Notify device removals. */
8348 for_each_oldnew_connector_in_state(state, connector, old_con_state, new_con_state, i) {
8349 if (old_con_state->crtc != new_con_state->crtc) {
8350 /* CRTC changes require notification. */
8354 if (!new_con_state->crtc)
8357 new_crtc_state = drm_atomic_get_new_crtc_state(
8358 state, new_con_state->crtc);
8360 if (!new_crtc_state)
8363 if (!drm_atomic_crtc_needs_modeset(new_crtc_state))
8367 aconnector = to_amdgpu_dm_connector(connector);
8369 mutex_lock(&adev->dm.audio_lock);
8370 inst = aconnector->audio_inst;
8371 aconnector->audio_inst = -1;
8372 mutex_unlock(&adev->dm.audio_lock);
8374 amdgpu_dm_audio_eld_notify(adev, inst);
8377 /* Notify audio device additions. */
8378 for_each_new_connector_in_state(state, connector, new_con_state, i) {
8379 if (!new_con_state->crtc)
8382 new_crtc_state = drm_atomic_get_new_crtc_state(
8383 state, new_con_state->crtc);
8385 if (!new_crtc_state)
8388 if (!drm_atomic_crtc_needs_modeset(new_crtc_state))
8391 new_dm_crtc_state = to_dm_crtc_state(new_crtc_state);
8392 if (!new_dm_crtc_state->stream)
8395 status = dc_stream_get_status(new_dm_crtc_state->stream);
8399 aconnector = to_amdgpu_dm_connector(connector);
8401 mutex_lock(&adev->dm.audio_lock);
8402 inst = status->audio_inst;
8403 aconnector->audio_inst = inst;
8404 mutex_unlock(&adev->dm.audio_lock);
8406 amdgpu_dm_audio_eld_notify(adev, inst);
8411 * amdgpu_dm_crtc_copy_transient_flags - copy mirrored flags from DRM to DC
8412 * @crtc_state: the DRM CRTC state
8413 * @stream_state: the DC stream state.
8415 * Copy the mirrored transient state flags from DRM, to DC. It is used to bring
8416 * a dc_stream_state's flags in sync with a drm_crtc_state's flags.
8418 static void amdgpu_dm_crtc_copy_transient_flags(struct drm_crtc_state *crtc_state,
8419 struct dc_stream_state *stream_state)
8421 stream_state->mode_changed = drm_atomic_crtc_needs_modeset(crtc_state);
8424 static void amdgpu_dm_commit_streams(struct drm_atomic_state *state,
8425 struct dc_state *dc_state)
8427 struct drm_device *dev = state->dev;
8428 struct amdgpu_device *adev = drm_to_adev(dev);
8429 struct amdgpu_display_manager *dm = &adev->dm;
8430 struct drm_crtc *crtc;
8431 struct drm_crtc_state *old_crtc_state, *new_crtc_state;
8432 struct dm_crtc_state *dm_old_crtc_state, *dm_new_crtc_state;
8433 bool mode_set_reset_required = false;
8436 for_each_oldnew_crtc_in_state(state, crtc, old_crtc_state,
8437 new_crtc_state, i) {
8438 struct amdgpu_crtc *acrtc = to_amdgpu_crtc(crtc);
8440 dm_old_crtc_state = to_dm_crtc_state(old_crtc_state);
8442 if (old_crtc_state->active &&
8443 (!new_crtc_state->active ||
8444 drm_atomic_crtc_needs_modeset(new_crtc_state))) {
8445 manage_dm_interrupts(adev, acrtc, false);
8446 dc_stream_release(dm_old_crtc_state->stream);
8450 drm_atomic_helper_calc_timestamping_constants(state);
8452 /* update changed items */
8453 for_each_oldnew_crtc_in_state(state, crtc, old_crtc_state, new_crtc_state, i) {
8454 struct amdgpu_crtc *acrtc = to_amdgpu_crtc(crtc);
8456 dm_new_crtc_state = to_dm_crtc_state(new_crtc_state);
8457 dm_old_crtc_state = to_dm_crtc_state(old_crtc_state);
8459 drm_dbg_state(state->dev,
8460 "amdgpu_crtc id:%d crtc_state_flags: enable:%d, active:%d, planes_changed:%d, mode_changed:%d,active_changed:%d,connectors_changed:%d\n",
8462 new_crtc_state->enable,
8463 new_crtc_state->active,
8464 new_crtc_state->planes_changed,
8465 new_crtc_state->mode_changed,
8466 new_crtc_state->active_changed,
8467 new_crtc_state->connectors_changed);
8469 /* Disable cursor if disabling crtc */
8470 if (old_crtc_state->active && !new_crtc_state->active) {
8471 struct dc_cursor_position position;
8473 memset(&position, 0, sizeof(position));
8474 mutex_lock(&dm->dc_lock);
8475 dc_stream_set_cursor_position(dm_old_crtc_state->stream, &position);
8476 mutex_unlock(&dm->dc_lock);
8479 /* Copy all transient state flags into dc state */
8480 if (dm_new_crtc_state->stream) {
8481 amdgpu_dm_crtc_copy_transient_flags(&dm_new_crtc_state->base,
8482 dm_new_crtc_state->stream);
8485 /* handles headless hotplug case, updating new_state and
8486 * aconnector as needed
8489 if (amdgpu_dm_crtc_modeset_required(new_crtc_state, dm_new_crtc_state->stream, dm_old_crtc_state->stream)) {
8491 DRM_DEBUG_ATOMIC("Atomic commit: SET crtc id %d: [%p]\n", acrtc->crtc_id, acrtc);
8493 if (!dm_new_crtc_state->stream) {
8495 * this could happen because of issues with
8496 * userspace notifications delivery.
8497 * In this case userspace tries to set mode on
8498 * display which is disconnected in fact.
8499 * dc_sink is NULL in this case on aconnector.
8500 * We expect reset mode will come soon.
8502 * This can also happen when unplug is done
8503 * during resume sequence ended
8505 * In this case, we want to pretend we still
8506 * have a sink to keep the pipe running so that
8507 * hw state is consistent with the sw state
8509 DRM_DEBUG_DRIVER("%s: Failed to create new stream for crtc %d\n",
8510 __func__, acrtc->base.base.id);
8514 if (dm_old_crtc_state->stream)
8515 remove_stream(adev, acrtc, dm_old_crtc_state->stream);
8517 pm_runtime_get_noresume(dev->dev);
8519 acrtc->enabled = true;
8520 acrtc->hw_mode = new_crtc_state->mode;
8521 crtc->hwmode = new_crtc_state->mode;
8522 mode_set_reset_required = true;
8523 } else if (modereset_required(new_crtc_state)) {
8524 DRM_DEBUG_ATOMIC("Atomic commit: RESET. crtc id %d:[%p]\n", acrtc->crtc_id, acrtc);
8525 /* i.e. reset mode */
8526 if (dm_old_crtc_state->stream)
8527 remove_stream(adev, acrtc, dm_old_crtc_state->stream);
8529 mode_set_reset_required = true;
8531 } /* for_each_crtc_in_state() */
8533 /* if there mode set or reset, disable eDP PSR */
8534 if (mode_set_reset_required) {
8535 if (dm->vblank_control_workqueue)
8536 flush_workqueue(dm->vblank_control_workqueue);
8538 amdgpu_dm_psr_disable_all(dm);
8541 dm_enable_per_frame_crtc_master_sync(dc_state);
8542 mutex_lock(&dm->dc_lock);
8543 WARN_ON(!dc_commit_streams(dm->dc, dc_state->streams, dc_state->stream_count));
8545 /* Allow idle optimization when vblank count is 0 for display off */
8546 if (dm->active_vblank_irq_count == 0)
8547 dc_allow_idle_optimizations(dm->dc, true);
8548 mutex_unlock(&dm->dc_lock);
8550 for_each_new_crtc_in_state(state, crtc, new_crtc_state, i) {
8551 struct amdgpu_crtc *acrtc = to_amdgpu_crtc(crtc);
8553 dm_new_crtc_state = to_dm_crtc_state(new_crtc_state);
8555 if (dm_new_crtc_state->stream != NULL) {
8556 const struct dc_stream_status *status =
8557 dc_stream_get_status(dm_new_crtc_state->stream);
8560 status = dc_stream_get_status_from_state(dc_state,
8561 dm_new_crtc_state->stream);
8563 DC_ERR("got no status for stream %p on acrtc%p\n", dm_new_crtc_state->stream, acrtc);
8565 acrtc->otg_inst = status->primary_otg_inst;
8571 * amdgpu_dm_atomic_commit_tail() - AMDgpu DM's commit tail implementation.
8572 * @state: The atomic state to commit
8574 * This will tell DC to commit the constructed DC state from atomic_check,
8575 * programming the hardware. Any failures here implies a hardware failure, since
8576 * atomic check should have filtered anything non-kosher.
8578 static void amdgpu_dm_atomic_commit_tail(struct drm_atomic_state *state)
8580 struct drm_device *dev = state->dev;
8581 struct amdgpu_device *adev = drm_to_adev(dev);
8582 struct amdgpu_display_manager *dm = &adev->dm;
8583 struct dm_atomic_state *dm_state;
8584 struct dc_state *dc_state = NULL;
8586 struct drm_crtc *crtc;
8587 struct drm_crtc_state *old_crtc_state, *new_crtc_state;
8588 unsigned long flags;
8589 bool wait_for_vblank = true;
8590 struct drm_connector *connector;
8591 struct drm_connector_state *old_con_state, *new_con_state;
8592 struct dm_crtc_state *dm_old_crtc_state, *dm_new_crtc_state;
8593 int crtc_disable_count = 0;
8595 trace_amdgpu_dm_atomic_commit_tail_begin(state);
8597 drm_atomic_helper_update_legacy_modeset_state(dev, state);
8598 drm_dp_mst_atomic_wait_for_dependencies(state);
8600 dm_state = dm_atomic_get_new_state(state);
8601 if (dm_state && dm_state->context) {
8602 dc_state = dm_state->context;
8603 amdgpu_dm_commit_streams(state, dc_state);
8606 for_each_oldnew_connector_in_state(state, connector, old_con_state, new_con_state, i) {
8607 struct dm_connector_state *dm_new_con_state = to_dm_connector_state(new_con_state);
8608 struct amdgpu_crtc *acrtc = to_amdgpu_crtc(dm_new_con_state->base.crtc);
8609 struct amdgpu_dm_connector *aconnector = to_amdgpu_dm_connector(connector);
8611 if (!adev->dm.hdcp_workqueue)
8614 pr_debug("[HDCP_DM] -------------- i : %x ----------\n", i);
8619 pr_debug("[HDCP_DM] connector->index: %x connect_status: %x dpms: %x\n",
8620 connector->index, connector->status, connector->dpms);
8621 pr_debug("[HDCP_DM] state protection old: %x new: %x\n",
8622 old_con_state->content_protection, new_con_state->content_protection);
8624 if (aconnector->dc_sink) {
8625 if (aconnector->dc_sink->sink_signal != SIGNAL_TYPE_VIRTUAL &&
8626 aconnector->dc_sink->sink_signal != SIGNAL_TYPE_NONE) {
8627 pr_debug("[HDCP_DM] pipe_ctx dispname=%s\n",
8628 aconnector->dc_sink->edid_caps.display_name);
8632 new_crtc_state = NULL;
8633 old_crtc_state = NULL;
8636 new_crtc_state = drm_atomic_get_new_crtc_state(state, &acrtc->base);
8637 old_crtc_state = drm_atomic_get_old_crtc_state(state, &acrtc->base);
8641 pr_debug("old crtc en: %x a: %x m: %x a-chg: %x c-chg: %x\n",
8642 old_crtc_state->enable,
8643 old_crtc_state->active,
8644 old_crtc_state->mode_changed,
8645 old_crtc_state->active_changed,
8646 old_crtc_state->connectors_changed);
8649 pr_debug("NEW crtc en: %x a: %x m: %x a-chg: %x c-chg: %x\n",
8650 new_crtc_state->enable,
8651 new_crtc_state->active,
8652 new_crtc_state->mode_changed,
8653 new_crtc_state->active_changed,
8654 new_crtc_state->connectors_changed);
8657 for_each_oldnew_connector_in_state(state, connector, old_con_state, new_con_state, i) {
8658 struct dm_connector_state *dm_new_con_state = to_dm_connector_state(new_con_state);
8659 struct amdgpu_crtc *acrtc = to_amdgpu_crtc(dm_new_con_state->base.crtc);
8660 struct amdgpu_dm_connector *aconnector = to_amdgpu_dm_connector(connector);
8662 if (!adev->dm.hdcp_workqueue)
8665 new_crtc_state = NULL;
8666 old_crtc_state = NULL;
8669 new_crtc_state = drm_atomic_get_new_crtc_state(state, &acrtc->base);
8670 old_crtc_state = drm_atomic_get_old_crtc_state(state, &acrtc->base);
8673 dm_new_crtc_state = to_dm_crtc_state(new_crtc_state);
8675 if (dm_new_crtc_state && dm_new_crtc_state->stream == NULL &&
8676 connector->state->content_protection == DRM_MODE_CONTENT_PROTECTION_ENABLED) {
8677 hdcp_reset_display(adev->dm.hdcp_workqueue, aconnector->dc_link->link_index);
8678 new_con_state->content_protection = DRM_MODE_CONTENT_PROTECTION_DESIRED;
8679 dm_new_con_state->update_hdcp = true;
8683 if (is_content_protection_different(new_crtc_state, old_crtc_state, new_con_state,
8684 old_con_state, connector, adev->dm.hdcp_workqueue)) {
8685 /* when display is unplugged from mst hub, connctor will
8686 * be destroyed within dm_dp_mst_connector_destroy. connector
8687 * hdcp perperties, like type, undesired, desired, enabled,
8688 * will be lost. So, save hdcp properties into hdcp_work within
8689 * amdgpu_dm_atomic_commit_tail. if the same display is
8690 * plugged back with same display index, its hdcp properties
8691 * will be retrieved from hdcp_work within dm_dp_mst_get_modes
8694 bool enable_encryption = false;
8696 if (new_con_state->content_protection == DRM_MODE_CONTENT_PROTECTION_DESIRED)
8697 enable_encryption = true;
8699 if (aconnector->dc_link && aconnector->dc_sink &&
8700 aconnector->dc_link->type == dc_connection_mst_branch) {
8701 struct hdcp_workqueue *hdcp_work = adev->dm.hdcp_workqueue;
8702 struct hdcp_workqueue *hdcp_w =
8703 &hdcp_work[aconnector->dc_link->link_index];
8705 hdcp_w->hdcp_content_type[connector->index] =
8706 new_con_state->hdcp_content_type;
8707 hdcp_w->content_protection[connector->index] =
8708 new_con_state->content_protection;
8711 if (new_crtc_state && new_crtc_state->mode_changed &&
8712 new_con_state->content_protection >= DRM_MODE_CONTENT_PROTECTION_DESIRED)
8713 enable_encryption = true;
8715 DRM_INFO("[HDCP_DM] hdcp_update_display enable_encryption = %x\n", enable_encryption);
8717 hdcp_update_display(
8718 adev->dm.hdcp_workqueue, aconnector->dc_link->link_index, aconnector,
8719 new_con_state->hdcp_content_type, enable_encryption);
8723 /* Handle connector state changes */
8724 for_each_oldnew_connector_in_state(state, connector, old_con_state, new_con_state, i) {
8725 struct dm_connector_state *dm_new_con_state = to_dm_connector_state(new_con_state);
8726 struct dm_connector_state *dm_old_con_state = to_dm_connector_state(old_con_state);
8727 struct amdgpu_crtc *acrtc = to_amdgpu_crtc(dm_new_con_state->base.crtc);
8728 struct dc_surface_update *dummy_updates;
8729 struct dc_stream_update stream_update;
8730 struct dc_info_packet hdr_packet;
8731 struct dc_stream_status *status = NULL;
8732 bool abm_changed, hdr_changed, scaling_changed;
8734 memset(&stream_update, 0, sizeof(stream_update));
8737 new_crtc_state = drm_atomic_get_new_crtc_state(state, &acrtc->base);
8738 old_crtc_state = drm_atomic_get_old_crtc_state(state, &acrtc->base);
8741 /* Skip any modesets/resets */
8742 if (!acrtc || drm_atomic_crtc_needs_modeset(new_crtc_state))
8745 dm_new_crtc_state = to_dm_crtc_state(new_crtc_state);
8746 dm_old_crtc_state = to_dm_crtc_state(old_crtc_state);
8748 scaling_changed = is_scaling_state_different(dm_new_con_state,
8751 abm_changed = dm_new_crtc_state->abm_level !=
8752 dm_old_crtc_state->abm_level;
8755 !drm_connector_atomic_hdr_metadata_equal(old_con_state, new_con_state);
8757 if (!scaling_changed && !abm_changed && !hdr_changed)
8760 stream_update.stream = dm_new_crtc_state->stream;
8761 if (scaling_changed) {
8762 update_stream_scaling_settings(&dm_new_con_state->base.crtc->mode,
8763 dm_new_con_state, dm_new_crtc_state->stream);
8765 stream_update.src = dm_new_crtc_state->stream->src;
8766 stream_update.dst = dm_new_crtc_state->stream->dst;
8770 dm_new_crtc_state->stream->abm_level = dm_new_crtc_state->abm_level;
8772 stream_update.abm_level = &dm_new_crtc_state->abm_level;
8776 fill_hdr_info_packet(new_con_state, &hdr_packet);
8777 stream_update.hdr_static_metadata = &hdr_packet;
8780 status = dc_stream_get_status(dm_new_crtc_state->stream);
8782 if (WARN_ON(!status))
8785 WARN_ON(!status->plane_count);
8788 * TODO: DC refuses to perform stream updates without a dc_surface_update.
8789 * Here we create an empty update on each plane.
8790 * To fix this, DC should permit updating only stream properties.
8792 dummy_updates = kzalloc(sizeof(struct dc_surface_update) * MAX_SURFACES, GFP_ATOMIC);
8793 for (j = 0; j < status->plane_count; j++)
8794 dummy_updates[j].surface = status->plane_states[0];
8797 mutex_lock(&dm->dc_lock);
8798 dc_update_planes_and_stream(dm->dc,
8800 status->plane_count,
8801 dm_new_crtc_state->stream,
8803 mutex_unlock(&dm->dc_lock);
8804 kfree(dummy_updates);
8808 * Enable interrupts for CRTCs that are newly enabled or went through
8809 * a modeset. It was intentionally deferred until after the front end
8810 * state was modified to wait until the OTG was on and so the IRQ
8811 * handlers didn't access stale or invalid state.
8813 for_each_oldnew_crtc_in_state(state, crtc, old_crtc_state, new_crtc_state, i) {
8814 struct amdgpu_crtc *acrtc = to_amdgpu_crtc(crtc);
8815 #ifdef CONFIG_DEBUG_FS
8816 enum amdgpu_dm_pipe_crc_source cur_crc_src;
8818 /* Count number of newly disabled CRTCs for dropping PM refs later. */
8819 if (old_crtc_state->active && !new_crtc_state->active)
8820 crtc_disable_count++;
8822 dm_new_crtc_state = to_dm_crtc_state(new_crtc_state);
8823 dm_old_crtc_state = to_dm_crtc_state(old_crtc_state);
8825 /* For freesync config update on crtc state and params for irq */
8826 update_stream_irq_parameters(dm, dm_new_crtc_state);
8828 #ifdef CONFIG_DEBUG_FS
8829 spin_lock_irqsave(&adev_to_drm(adev)->event_lock, flags);
8830 cur_crc_src = acrtc->dm_irq_params.crc_src;
8831 spin_unlock_irqrestore(&adev_to_drm(adev)->event_lock, flags);
8834 if (new_crtc_state->active &&
8835 (!old_crtc_state->active ||
8836 drm_atomic_crtc_needs_modeset(new_crtc_state))) {
8837 dc_stream_retain(dm_new_crtc_state->stream);
8838 acrtc->dm_irq_params.stream = dm_new_crtc_state->stream;
8839 manage_dm_interrupts(adev, acrtc, true);
8841 /* Handle vrr on->off / off->on transitions */
8842 amdgpu_dm_handle_vrr_transition(dm_old_crtc_state, dm_new_crtc_state);
8844 #ifdef CONFIG_DEBUG_FS
8845 if (new_crtc_state->active &&
8846 (!old_crtc_state->active ||
8847 drm_atomic_crtc_needs_modeset(new_crtc_state))) {
8849 * Frontend may have changed so reapply the CRC capture
8850 * settings for the stream.
8852 if (amdgpu_dm_is_valid_crc_source(cur_crc_src)) {
8853 #if defined(CONFIG_DRM_AMD_SECURE_DISPLAY)
8854 if (amdgpu_dm_crc_window_is_activated(crtc)) {
8855 spin_lock_irqsave(&adev_to_drm(adev)->event_lock, flags);
8856 acrtc->dm_irq_params.window_param.update_win = true;
8859 * It takes 2 frames for HW to stably generate CRC when
8860 * resuming from suspend, so we set skip_frame_cnt 2.
8862 acrtc->dm_irq_params.window_param.skip_frame_cnt = 2;
8863 spin_unlock_irqrestore(&adev_to_drm(adev)->event_lock, flags);
8866 if (amdgpu_dm_crtc_configure_crc_source(
8867 crtc, dm_new_crtc_state, cur_crc_src))
8868 DRM_DEBUG_DRIVER("Failed to configure crc source");
8874 for_each_new_crtc_in_state(state, crtc, new_crtc_state, j)
8875 if (new_crtc_state->async_flip)
8876 wait_for_vblank = false;
8878 /* update planes when needed per crtc*/
8879 for_each_new_crtc_in_state(state, crtc, new_crtc_state, j) {
8880 dm_new_crtc_state = to_dm_crtc_state(new_crtc_state);
8882 if (dm_new_crtc_state->stream)
8883 amdgpu_dm_commit_planes(state, dev, dm, crtc, wait_for_vblank);
8886 /* Update audio instances for each connector. */
8887 amdgpu_dm_commit_audio(dev, state);
8889 /* restore the backlight level */
8890 for (i = 0; i < dm->num_of_edps; i++) {
8891 if (dm->backlight_dev[i] &&
8892 (dm->actual_brightness[i] != dm->brightness[i]))
8893 amdgpu_dm_backlight_set_level(dm, i, dm->brightness[i]);
8897 * send vblank event on all events not handled in flip and
8898 * mark consumed event for drm_atomic_helper_commit_hw_done
8900 spin_lock_irqsave(&adev_to_drm(adev)->event_lock, flags);
8901 for_each_new_crtc_in_state(state, crtc, new_crtc_state, i) {
8903 if (new_crtc_state->event)
8904 drm_send_event_locked(dev, &new_crtc_state->event->base);
8906 new_crtc_state->event = NULL;
8908 spin_unlock_irqrestore(&adev_to_drm(adev)->event_lock, flags);
8910 /* Signal HW programming completion */
8911 drm_atomic_helper_commit_hw_done(state);
8913 if (wait_for_vblank)
8914 drm_atomic_helper_wait_for_flip_done(dev, state);
8916 drm_atomic_helper_cleanup_planes(dev, state);
8918 /* Don't free the memory if we are hitting this as part of suspend.
8919 * This way we don't free any memory during suspend; see
8920 * amdgpu_bo_free_kernel(). The memory will be freed in the first
8921 * non-suspend modeset or when the driver is torn down.
8923 if (!adev->in_suspend) {
8924 /* return the stolen vga memory back to VRAM */
8925 if (!adev->mman.keep_stolen_vga_memory)
8926 amdgpu_bo_free_kernel(&adev->mman.stolen_vga_memory, NULL, NULL);
8927 amdgpu_bo_free_kernel(&adev->mman.stolen_extended_memory, NULL, NULL);
8931 * Finally, drop a runtime PM reference for each newly disabled CRTC,
8932 * so we can put the GPU into runtime suspend if we're not driving any
8935 for (i = 0; i < crtc_disable_count; i++)
8936 pm_runtime_put_autosuspend(dev->dev);
8937 pm_runtime_mark_last_busy(dev->dev);
8940 static int dm_force_atomic_commit(struct drm_connector *connector)
8943 struct drm_device *ddev = connector->dev;
8944 struct drm_atomic_state *state = drm_atomic_state_alloc(ddev);
8945 struct amdgpu_crtc *disconnected_acrtc = to_amdgpu_crtc(connector->encoder->crtc);
8946 struct drm_plane *plane = disconnected_acrtc->base.primary;
8947 struct drm_connector_state *conn_state;
8948 struct drm_crtc_state *crtc_state;
8949 struct drm_plane_state *plane_state;
8954 state->acquire_ctx = ddev->mode_config.acquire_ctx;
8956 /* Construct an atomic state to restore previous display setting */
8959 * Attach connectors to drm_atomic_state
8961 conn_state = drm_atomic_get_connector_state(state, connector);
8963 ret = PTR_ERR_OR_ZERO(conn_state);
8967 /* Attach crtc to drm_atomic_state*/
8968 crtc_state = drm_atomic_get_crtc_state(state, &disconnected_acrtc->base);
8970 ret = PTR_ERR_OR_ZERO(crtc_state);
8974 /* force a restore */
8975 crtc_state->mode_changed = true;
8977 /* Attach plane to drm_atomic_state */
8978 plane_state = drm_atomic_get_plane_state(state, plane);
8980 ret = PTR_ERR_OR_ZERO(plane_state);
8984 /* Call commit internally with the state we just constructed */
8985 ret = drm_atomic_commit(state);
8988 drm_atomic_state_put(state);
8990 DRM_ERROR("Restoring old state failed with %i\n", ret);
8996 * This function handles all cases when set mode does not come upon hotplug.
8997 * This includes when a display is unplugged then plugged back into the
8998 * same port and when running without usermode desktop manager supprot
9000 void dm_restore_drm_connector_state(struct drm_device *dev,
9001 struct drm_connector *connector)
9003 struct amdgpu_dm_connector *aconnector = to_amdgpu_dm_connector(connector);
9004 struct amdgpu_crtc *disconnected_acrtc;
9005 struct dm_crtc_state *acrtc_state;
9007 if (!aconnector->dc_sink || !connector->state || !connector->encoder)
9010 disconnected_acrtc = to_amdgpu_crtc(connector->encoder->crtc);
9011 if (!disconnected_acrtc)
9014 acrtc_state = to_dm_crtc_state(disconnected_acrtc->base.state);
9015 if (!acrtc_state->stream)
9019 * If the previous sink is not released and different from the current,
9020 * we deduce we are in a state where we can not rely on usermode call
9021 * to turn on the display, so we do it here
9023 if (acrtc_state->stream->sink != aconnector->dc_sink)
9024 dm_force_atomic_commit(&aconnector->base);
9028 * Grabs all modesetting locks to serialize against any blocking commits,
9029 * Waits for completion of all non blocking commits.
9031 static int do_aquire_global_lock(struct drm_device *dev,
9032 struct drm_atomic_state *state)
9034 struct drm_crtc *crtc;
9035 struct drm_crtc_commit *commit;
9039 * Adding all modeset locks to aquire_ctx will
9040 * ensure that when the framework release it the
9041 * extra locks we are locking here will get released to
9043 ret = drm_modeset_lock_all_ctx(dev, state->acquire_ctx);
9047 list_for_each_entry(crtc, &dev->mode_config.crtc_list, head) {
9048 spin_lock(&crtc->commit_lock);
9049 commit = list_first_entry_or_null(&crtc->commit_list,
9050 struct drm_crtc_commit, commit_entry);
9052 drm_crtc_commit_get(commit);
9053 spin_unlock(&crtc->commit_lock);
9059 * Make sure all pending HW programming completed and
9062 ret = wait_for_completion_interruptible_timeout(&commit->hw_done, 10*HZ);
9065 ret = wait_for_completion_interruptible_timeout(
9066 &commit->flip_done, 10*HZ);
9069 DRM_ERROR("[CRTC:%d:%s] hw_done or flip_done timed out\n",
9070 crtc->base.id, crtc->name);
9072 drm_crtc_commit_put(commit);
9075 return ret < 0 ? ret : 0;
9078 static void get_freesync_config_for_crtc(
9079 struct dm_crtc_state *new_crtc_state,
9080 struct dm_connector_state *new_con_state)
9082 struct mod_freesync_config config = {0};
9083 struct amdgpu_dm_connector *aconnector =
9084 to_amdgpu_dm_connector(new_con_state->base.connector);
9085 struct drm_display_mode *mode = &new_crtc_state->base.mode;
9086 int vrefresh = drm_mode_vrefresh(mode);
9087 bool fs_vid_mode = false;
9089 new_crtc_state->vrr_supported = new_con_state->freesync_capable &&
9090 vrefresh >= aconnector->min_vfreq &&
9091 vrefresh <= aconnector->max_vfreq;
9093 if (new_crtc_state->vrr_supported) {
9094 new_crtc_state->stream->ignore_msa_timing_param = true;
9095 fs_vid_mode = new_crtc_state->freesync_config.state == VRR_STATE_ACTIVE_FIXED;
9097 config.min_refresh_in_uhz = aconnector->min_vfreq * 1000000;
9098 config.max_refresh_in_uhz = aconnector->max_vfreq * 1000000;
9099 config.vsif_supported = true;
9103 config.state = VRR_STATE_ACTIVE_FIXED;
9104 config.fixed_refresh_in_uhz = new_crtc_state->freesync_config.fixed_refresh_in_uhz;
9106 } else if (new_crtc_state->base.vrr_enabled) {
9107 config.state = VRR_STATE_ACTIVE_VARIABLE;
9109 config.state = VRR_STATE_INACTIVE;
9113 new_crtc_state->freesync_config = config;
9116 static void reset_freesync_config_for_crtc(
9117 struct dm_crtc_state *new_crtc_state)
9119 new_crtc_state->vrr_supported = false;
9121 memset(&new_crtc_state->vrr_infopacket, 0,
9122 sizeof(new_crtc_state->vrr_infopacket));
9126 is_timing_unchanged_for_freesync(struct drm_crtc_state *old_crtc_state,
9127 struct drm_crtc_state *new_crtc_state)
9129 const struct drm_display_mode *old_mode, *new_mode;
9131 if (!old_crtc_state || !new_crtc_state)
9134 old_mode = &old_crtc_state->mode;
9135 new_mode = &new_crtc_state->mode;
9137 if (old_mode->clock == new_mode->clock &&
9138 old_mode->hdisplay == new_mode->hdisplay &&
9139 old_mode->vdisplay == new_mode->vdisplay &&
9140 old_mode->htotal == new_mode->htotal &&
9141 old_mode->vtotal != new_mode->vtotal &&
9142 old_mode->hsync_start == new_mode->hsync_start &&
9143 old_mode->vsync_start != new_mode->vsync_start &&
9144 old_mode->hsync_end == new_mode->hsync_end &&
9145 old_mode->vsync_end != new_mode->vsync_end &&
9146 old_mode->hskew == new_mode->hskew &&
9147 old_mode->vscan == new_mode->vscan &&
9148 (old_mode->vsync_end - old_mode->vsync_start) ==
9149 (new_mode->vsync_end - new_mode->vsync_start))
9155 static void set_freesync_fixed_config(struct dm_crtc_state *dm_new_crtc_state)
9158 struct drm_crtc_state *new_crtc_state = &dm_new_crtc_state->base;
9160 dm_new_crtc_state->freesync_config.state = VRR_STATE_ACTIVE_FIXED;
9162 num = (unsigned long long)new_crtc_state->mode.clock * 1000 * 1000000;
9163 den = (unsigned long long)new_crtc_state->mode.htotal *
9164 (unsigned long long)new_crtc_state->mode.vtotal;
9166 res = div_u64(num, den);
9167 dm_new_crtc_state->freesync_config.fixed_refresh_in_uhz = res;
9170 static int dm_update_crtc_state(struct amdgpu_display_manager *dm,
9171 struct drm_atomic_state *state,
9172 struct drm_crtc *crtc,
9173 struct drm_crtc_state *old_crtc_state,
9174 struct drm_crtc_state *new_crtc_state,
9176 bool *lock_and_validation_needed)
9178 struct dm_atomic_state *dm_state = NULL;
9179 struct dm_crtc_state *dm_old_crtc_state, *dm_new_crtc_state;
9180 struct dc_stream_state *new_stream;
9184 * TODO Move this code into dm_crtc_atomic_check once we get rid of dc_validation_set
9185 * update changed items
9187 struct amdgpu_crtc *acrtc = NULL;
9188 struct amdgpu_dm_connector *aconnector = NULL;
9189 struct drm_connector_state *drm_new_conn_state = NULL, *drm_old_conn_state = NULL;
9190 struct dm_connector_state *dm_new_conn_state = NULL, *dm_old_conn_state = NULL;
9194 dm_old_crtc_state = to_dm_crtc_state(old_crtc_state);
9195 dm_new_crtc_state = to_dm_crtc_state(new_crtc_state);
9196 acrtc = to_amdgpu_crtc(crtc);
9197 aconnector = amdgpu_dm_find_first_crtc_matching_connector(state, crtc);
9199 /* TODO This hack should go away */
9200 if (aconnector && enable) {
9201 /* Make sure fake sink is created in plug-in scenario */
9202 drm_new_conn_state = drm_atomic_get_new_connector_state(state,
9204 drm_old_conn_state = drm_atomic_get_old_connector_state(state,
9207 if (IS_ERR(drm_new_conn_state)) {
9208 ret = PTR_ERR_OR_ZERO(drm_new_conn_state);
9212 dm_new_conn_state = to_dm_connector_state(drm_new_conn_state);
9213 dm_old_conn_state = to_dm_connector_state(drm_old_conn_state);
9215 if (!drm_atomic_crtc_needs_modeset(new_crtc_state))
9218 new_stream = create_validate_stream_for_sink(aconnector,
9219 &new_crtc_state->mode,
9221 dm_old_crtc_state->stream);
9224 * we can have no stream on ACTION_SET if a display
9225 * was disconnected during S3, in this case it is not an
9226 * error, the OS will be updated after detection, and
9227 * will do the right thing on next atomic commit
9231 DRM_DEBUG_DRIVER("%s: Failed to create new stream for crtc %d\n",
9232 __func__, acrtc->base.base.id);
9238 * TODO: Check VSDB bits to decide whether this should
9239 * be enabled or not.
9241 new_stream->triggered_crtc_reset.enabled =
9242 dm->force_timing_sync;
9244 dm_new_crtc_state->abm_level = dm_new_conn_state->abm_level;
9246 ret = fill_hdr_info_packet(drm_new_conn_state,
9247 &new_stream->hdr_static_metadata);
9252 * If we already removed the old stream from the context
9253 * (and set the new stream to NULL) then we can't reuse
9254 * the old stream even if the stream and scaling are unchanged.
9255 * We'll hit the BUG_ON and black screen.
9257 * TODO: Refactor this function to allow this check to work
9258 * in all conditions.
9260 if (dm_new_crtc_state->stream &&
9261 is_timing_unchanged_for_freesync(new_crtc_state, old_crtc_state))
9264 if (dm_new_crtc_state->stream &&
9265 dc_is_stream_unchanged(new_stream, dm_old_crtc_state->stream) &&
9266 dc_is_stream_scaling_unchanged(new_stream, dm_old_crtc_state->stream)) {
9267 new_crtc_state->mode_changed = false;
9268 DRM_DEBUG_DRIVER("Mode change not required, setting mode_changed to %d",
9269 new_crtc_state->mode_changed);
9273 /* mode_changed flag may get updated above, need to check again */
9274 if (!drm_atomic_crtc_needs_modeset(new_crtc_state))
9277 drm_dbg_state(state->dev,
9278 "amdgpu_crtc id:%d crtc_state_flags: enable:%d, active:%d, planes_changed:%d, mode_changed:%d,active_changed:%d,connectors_changed:%d\n",
9280 new_crtc_state->enable,
9281 new_crtc_state->active,
9282 new_crtc_state->planes_changed,
9283 new_crtc_state->mode_changed,
9284 new_crtc_state->active_changed,
9285 new_crtc_state->connectors_changed);
9287 /* Remove stream for any changed/disabled CRTC */
9290 if (!dm_old_crtc_state->stream)
9293 /* Unset freesync video if it was active before */
9294 if (dm_old_crtc_state->freesync_config.state == VRR_STATE_ACTIVE_FIXED) {
9295 dm_new_crtc_state->freesync_config.state = VRR_STATE_INACTIVE;
9296 dm_new_crtc_state->freesync_config.fixed_refresh_in_uhz = 0;
9299 /* Now check if we should set freesync video mode */
9300 if (dm_new_crtc_state->stream &&
9301 dc_is_stream_unchanged(new_stream, dm_old_crtc_state->stream) &&
9302 dc_is_stream_scaling_unchanged(new_stream, dm_old_crtc_state->stream) &&
9303 is_timing_unchanged_for_freesync(new_crtc_state,
9305 new_crtc_state->mode_changed = false;
9307 "Mode change not required for front porch change, setting mode_changed to %d",
9308 new_crtc_state->mode_changed);
9310 set_freesync_fixed_config(dm_new_crtc_state);
9313 } else if (aconnector &&
9314 is_freesync_video_mode(&new_crtc_state->mode,
9316 struct drm_display_mode *high_mode;
9318 high_mode = get_highest_refresh_rate_mode(aconnector, false);
9319 if (!drm_mode_equal(&new_crtc_state->mode, high_mode))
9320 set_freesync_fixed_config(dm_new_crtc_state);
9323 ret = dm_atomic_get_state(state, &dm_state);
9327 DRM_DEBUG_DRIVER("Disabling DRM crtc: %d\n",
9330 /* i.e. reset mode */
9331 if (dc_remove_stream_from_ctx(
9334 dm_old_crtc_state->stream) != DC_OK) {
9339 dc_stream_release(dm_old_crtc_state->stream);
9340 dm_new_crtc_state->stream = NULL;
9342 reset_freesync_config_for_crtc(dm_new_crtc_state);
9344 *lock_and_validation_needed = true;
9346 } else {/* Add stream for any updated/enabled CRTC */
9348 * Quick fix to prevent NULL pointer on new_stream when
9349 * added MST connectors not found in existing crtc_state in the chained mode
9350 * TODO: need to dig out the root cause of that
9355 if (modereset_required(new_crtc_state))
9358 if (amdgpu_dm_crtc_modeset_required(new_crtc_state, new_stream,
9359 dm_old_crtc_state->stream)) {
9361 WARN_ON(dm_new_crtc_state->stream);
9363 ret = dm_atomic_get_state(state, &dm_state);
9367 dm_new_crtc_state->stream = new_stream;
9369 dc_stream_retain(new_stream);
9371 DRM_DEBUG_ATOMIC("Enabling DRM crtc: %d\n",
9374 if (dc_add_stream_to_ctx(
9377 dm_new_crtc_state->stream) != DC_OK) {
9382 *lock_and_validation_needed = true;
9387 /* Release extra reference */
9389 dc_stream_release(new_stream);
9392 * We want to do dc stream updates that do not require a
9393 * full modeset below.
9395 if (!(enable && aconnector && new_crtc_state->active))
9398 * Given above conditions, the dc state cannot be NULL because:
9399 * 1. We're in the process of enabling CRTCs (just been added
9400 * to the dc context, or already is on the context)
9401 * 2. Has a valid connector attached, and
9402 * 3. Is currently active and enabled.
9403 * => The dc stream state currently exists.
9405 BUG_ON(dm_new_crtc_state->stream == NULL);
9407 /* Scaling or underscan settings */
9408 if (is_scaling_state_different(dm_old_conn_state, dm_new_conn_state) ||
9409 drm_atomic_crtc_needs_modeset(new_crtc_state))
9410 update_stream_scaling_settings(
9411 &new_crtc_state->mode, dm_new_conn_state, dm_new_crtc_state->stream);
9414 dm_new_crtc_state->abm_level = dm_new_conn_state->abm_level;
9417 * Color management settings. We also update color properties
9418 * when a modeset is needed, to ensure it gets reprogrammed.
9420 if (dm_new_crtc_state->base.color_mgmt_changed ||
9421 drm_atomic_crtc_needs_modeset(new_crtc_state)) {
9422 ret = amdgpu_dm_update_crtc_color_mgmt(dm_new_crtc_state);
9427 /* Update Freesync settings. */
9428 get_freesync_config_for_crtc(dm_new_crtc_state,
9435 dc_stream_release(new_stream);
9439 static bool should_reset_plane(struct drm_atomic_state *state,
9440 struct drm_plane *plane,
9441 struct drm_plane_state *old_plane_state,
9442 struct drm_plane_state *new_plane_state)
9444 struct drm_plane *other;
9445 struct drm_plane_state *old_other_state, *new_other_state;
9446 struct drm_crtc_state *new_crtc_state;
9450 * TODO: Remove this hack once the checks below are sufficient
9451 * enough to determine when we need to reset all the planes on
9454 if (state->allow_modeset)
9457 /* Exit early if we know that we're adding or removing the plane. */
9458 if (old_plane_state->crtc != new_plane_state->crtc)
9461 /* old crtc == new_crtc == NULL, plane not in context. */
9462 if (!new_plane_state->crtc)
9466 drm_atomic_get_new_crtc_state(state, new_plane_state->crtc);
9468 if (!new_crtc_state)
9471 /* CRTC Degamma changes currently require us to recreate planes. */
9472 if (new_crtc_state->color_mgmt_changed)
9475 if (drm_atomic_crtc_needs_modeset(new_crtc_state))
9479 * If there are any new primary or overlay planes being added or
9480 * removed then the z-order can potentially change. To ensure
9481 * correct z-order and pipe acquisition the current DC architecture
9482 * requires us to remove and recreate all existing planes.
9484 * TODO: Come up with a more elegant solution for this.
9486 for_each_oldnew_plane_in_state(state, other, old_other_state, new_other_state, i) {
9487 struct amdgpu_framebuffer *old_afb, *new_afb;
9489 if (other->type == DRM_PLANE_TYPE_CURSOR)
9492 if (old_other_state->crtc != new_plane_state->crtc &&
9493 new_other_state->crtc != new_plane_state->crtc)
9496 if (old_other_state->crtc != new_other_state->crtc)
9499 /* Src/dst size and scaling updates. */
9500 if (old_other_state->src_w != new_other_state->src_w ||
9501 old_other_state->src_h != new_other_state->src_h ||
9502 old_other_state->crtc_w != new_other_state->crtc_w ||
9503 old_other_state->crtc_h != new_other_state->crtc_h)
9506 /* Rotation / mirroring updates. */
9507 if (old_other_state->rotation != new_other_state->rotation)
9510 /* Blending updates. */
9511 if (old_other_state->pixel_blend_mode !=
9512 new_other_state->pixel_blend_mode)
9515 /* Alpha updates. */
9516 if (old_other_state->alpha != new_other_state->alpha)
9519 /* Colorspace changes. */
9520 if (old_other_state->color_range != new_other_state->color_range ||
9521 old_other_state->color_encoding != new_other_state->color_encoding)
9524 /* Framebuffer checks fall at the end. */
9525 if (!old_other_state->fb || !new_other_state->fb)
9528 /* Pixel format changes can require bandwidth updates. */
9529 if (old_other_state->fb->format != new_other_state->fb->format)
9532 old_afb = (struct amdgpu_framebuffer *)old_other_state->fb;
9533 new_afb = (struct amdgpu_framebuffer *)new_other_state->fb;
9535 /* Tiling and DCC changes also require bandwidth updates. */
9536 if (old_afb->tiling_flags != new_afb->tiling_flags ||
9537 old_afb->base.modifier != new_afb->base.modifier)
9544 static int dm_check_cursor_fb(struct amdgpu_crtc *new_acrtc,
9545 struct drm_plane_state *new_plane_state,
9546 struct drm_framebuffer *fb)
9548 struct amdgpu_device *adev = drm_to_adev(new_acrtc->base.dev);
9549 struct amdgpu_framebuffer *afb = to_amdgpu_framebuffer(fb);
9553 if (fb->width > new_acrtc->max_cursor_width ||
9554 fb->height > new_acrtc->max_cursor_height) {
9555 DRM_DEBUG_ATOMIC("Bad cursor FB size %dx%d\n",
9556 new_plane_state->fb->width,
9557 new_plane_state->fb->height);
9560 if (new_plane_state->src_w != fb->width << 16 ||
9561 new_plane_state->src_h != fb->height << 16) {
9562 DRM_DEBUG_ATOMIC("Cropping not supported for cursor plane\n");
9566 /* Pitch in pixels */
9567 pitch = fb->pitches[0] / fb->format->cpp[0];
9569 if (fb->width != pitch) {
9570 DRM_DEBUG_ATOMIC("Cursor FB width %d doesn't match pitch %d",
9579 /* FB pitch is supported by cursor plane */
9582 DRM_DEBUG_ATOMIC("Bad cursor FB pitch %d px\n", pitch);
9586 /* Core DRM takes care of checking FB modifiers, so we only need to
9587 * check tiling flags when the FB doesn't have a modifier.
9589 if (!(fb->flags & DRM_MODE_FB_MODIFIERS)) {
9590 if (adev->family < AMDGPU_FAMILY_AI) {
9591 linear = AMDGPU_TILING_GET(afb->tiling_flags, ARRAY_MODE) != DC_ARRAY_2D_TILED_THIN1 &&
9592 AMDGPU_TILING_GET(afb->tiling_flags, ARRAY_MODE) != DC_ARRAY_1D_TILED_THIN1 &&
9593 AMDGPU_TILING_GET(afb->tiling_flags, MICRO_TILE_MODE) == 0;
9595 linear = AMDGPU_TILING_GET(afb->tiling_flags, SWIZZLE_MODE) == 0;
9598 DRM_DEBUG_ATOMIC("Cursor FB not linear");
9606 static int dm_update_plane_state(struct dc *dc,
9607 struct drm_atomic_state *state,
9608 struct drm_plane *plane,
9609 struct drm_plane_state *old_plane_state,
9610 struct drm_plane_state *new_plane_state,
9612 bool *lock_and_validation_needed,
9613 bool *is_top_most_overlay)
9616 struct dm_atomic_state *dm_state = NULL;
9617 struct drm_crtc *new_plane_crtc, *old_plane_crtc;
9618 struct drm_crtc_state *old_crtc_state, *new_crtc_state;
9619 struct dm_crtc_state *dm_new_crtc_state, *dm_old_crtc_state;
9620 struct dm_plane_state *dm_new_plane_state, *dm_old_plane_state;
9621 struct amdgpu_crtc *new_acrtc;
9626 new_plane_crtc = new_plane_state->crtc;
9627 old_plane_crtc = old_plane_state->crtc;
9628 dm_new_plane_state = to_dm_plane_state(new_plane_state);
9629 dm_old_plane_state = to_dm_plane_state(old_plane_state);
9631 if (plane->type == DRM_PLANE_TYPE_CURSOR) {
9632 if (!enable || !new_plane_crtc ||
9633 drm_atomic_plane_disabling(plane->state, new_plane_state))
9636 new_acrtc = to_amdgpu_crtc(new_plane_crtc);
9638 if (new_plane_state->src_x != 0 || new_plane_state->src_y != 0) {
9639 DRM_DEBUG_ATOMIC("Cropping not supported for cursor plane\n");
9643 if (new_plane_state->fb) {
9644 ret = dm_check_cursor_fb(new_acrtc, new_plane_state,
9645 new_plane_state->fb);
9653 needs_reset = should_reset_plane(state, plane, old_plane_state,
9656 /* Remove any changed/removed planes */
9661 if (!old_plane_crtc)
9664 old_crtc_state = drm_atomic_get_old_crtc_state(
9665 state, old_plane_crtc);
9666 dm_old_crtc_state = to_dm_crtc_state(old_crtc_state);
9668 if (!dm_old_crtc_state->stream)
9671 DRM_DEBUG_ATOMIC("Disabling DRM plane: %d on DRM crtc %d\n",
9672 plane->base.id, old_plane_crtc->base.id);
9674 ret = dm_atomic_get_state(state, &dm_state);
9678 if (!dc_remove_plane_from_context(
9680 dm_old_crtc_state->stream,
9681 dm_old_plane_state->dc_state,
9682 dm_state->context)) {
9687 if (dm_old_plane_state->dc_state)
9688 dc_plane_state_release(dm_old_plane_state->dc_state);
9690 dm_new_plane_state->dc_state = NULL;
9692 *lock_and_validation_needed = true;
9694 } else { /* Add new planes */
9695 struct dc_plane_state *dc_new_plane_state;
9697 if (drm_atomic_plane_disabling(plane->state, new_plane_state))
9700 if (!new_plane_crtc)
9703 new_crtc_state = drm_atomic_get_new_crtc_state(state, new_plane_crtc);
9704 dm_new_crtc_state = to_dm_crtc_state(new_crtc_state);
9706 if (!dm_new_crtc_state->stream)
9712 ret = amdgpu_dm_plane_helper_check_state(new_plane_state, new_crtc_state);
9716 WARN_ON(dm_new_plane_state->dc_state);
9718 dc_new_plane_state = dc_create_plane_state(dc);
9719 if (!dc_new_plane_state)
9722 /* Block top most plane from being a video plane */
9723 if (plane->type == DRM_PLANE_TYPE_OVERLAY) {
9724 if (is_video_format(new_plane_state->fb->format->format) && *is_top_most_overlay)
9727 *is_top_most_overlay = false;
9730 DRM_DEBUG_ATOMIC("Enabling DRM plane: %d on DRM crtc %d\n",
9731 plane->base.id, new_plane_crtc->base.id);
9733 ret = fill_dc_plane_attributes(
9734 drm_to_adev(new_plane_crtc->dev),
9739 dc_plane_state_release(dc_new_plane_state);
9743 ret = dm_atomic_get_state(state, &dm_state);
9745 dc_plane_state_release(dc_new_plane_state);
9750 * Any atomic check errors that occur after this will
9751 * not need a release. The plane state will be attached
9752 * to the stream, and therefore part of the atomic
9753 * state. It'll be released when the atomic state is
9756 if (!dc_add_plane_to_context(
9758 dm_new_crtc_state->stream,
9760 dm_state->context)) {
9762 dc_plane_state_release(dc_new_plane_state);
9766 dm_new_plane_state->dc_state = dc_new_plane_state;
9768 dm_new_crtc_state->mpo_requested |= (plane->type == DRM_PLANE_TYPE_OVERLAY);
9770 /* Tell DC to do a full surface update every time there
9771 * is a plane change. Inefficient, but works for now.
9773 dm_new_plane_state->dc_state->update_flags.bits.full_update = 1;
9775 *lock_and_validation_needed = true;
9782 static void dm_get_oriented_plane_size(struct drm_plane_state *plane_state,
9783 int *src_w, int *src_h)
9785 switch (plane_state->rotation & DRM_MODE_ROTATE_MASK) {
9786 case DRM_MODE_ROTATE_90:
9787 case DRM_MODE_ROTATE_270:
9788 *src_w = plane_state->src_h >> 16;
9789 *src_h = plane_state->src_w >> 16;
9791 case DRM_MODE_ROTATE_0:
9792 case DRM_MODE_ROTATE_180:
9794 *src_w = plane_state->src_w >> 16;
9795 *src_h = plane_state->src_h >> 16;
9800 static int dm_check_crtc_cursor(struct drm_atomic_state *state,
9801 struct drm_crtc *crtc,
9802 struct drm_crtc_state *new_crtc_state)
9804 struct drm_plane *cursor = crtc->cursor, *underlying;
9805 struct drm_plane_state *new_cursor_state, *new_underlying_state;
9807 int cursor_scale_w, cursor_scale_h, underlying_scale_w, underlying_scale_h;
9808 int cursor_src_w, cursor_src_h;
9809 int underlying_src_w, underlying_src_h;
9811 /* On DCE and DCN there is no dedicated hardware cursor plane. We get a
9812 * cursor per pipe but it's going to inherit the scaling and
9813 * positioning from the underlying pipe. Check the cursor plane's
9814 * blending properties match the underlying planes'.
9817 new_cursor_state = drm_atomic_get_new_plane_state(state, cursor);
9818 if (!new_cursor_state || !new_cursor_state->fb)
9821 dm_get_oriented_plane_size(new_cursor_state, &cursor_src_w, &cursor_src_h);
9822 cursor_scale_w = new_cursor_state->crtc_w * 1000 / cursor_src_w;
9823 cursor_scale_h = new_cursor_state->crtc_h * 1000 / cursor_src_h;
9825 for_each_new_plane_in_state_reverse(state, underlying, new_underlying_state, i) {
9826 /* Narrow down to non-cursor planes on the same CRTC as the cursor */
9827 if (new_underlying_state->crtc != crtc || underlying == crtc->cursor)
9830 /* Ignore disabled planes */
9831 if (!new_underlying_state->fb)
9834 dm_get_oriented_plane_size(new_underlying_state,
9835 &underlying_src_w, &underlying_src_h);
9836 underlying_scale_w = new_underlying_state->crtc_w * 1000 / underlying_src_w;
9837 underlying_scale_h = new_underlying_state->crtc_h * 1000 / underlying_src_h;
9839 if (cursor_scale_w != underlying_scale_w ||
9840 cursor_scale_h != underlying_scale_h) {
9841 drm_dbg_atomic(crtc->dev,
9842 "Cursor [PLANE:%d:%s] scaling doesn't match underlying [PLANE:%d:%s]\n",
9843 cursor->base.id, cursor->name, underlying->base.id, underlying->name);
9847 /* If this plane covers the whole CRTC, no need to check planes underneath */
9848 if (new_underlying_state->crtc_x <= 0 &&
9849 new_underlying_state->crtc_y <= 0 &&
9850 new_underlying_state->crtc_x + new_underlying_state->crtc_w >= new_crtc_state->mode.hdisplay &&
9851 new_underlying_state->crtc_y + new_underlying_state->crtc_h >= new_crtc_state->mode.vdisplay)
9858 static int add_affected_mst_dsc_crtcs(struct drm_atomic_state *state, struct drm_crtc *crtc)
9860 struct drm_connector *connector;
9861 struct drm_connector_state *conn_state, *old_conn_state;
9862 struct amdgpu_dm_connector *aconnector = NULL;
9865 for_each_oldnew_connector_in_state(state, connector, old_conn_state, conn_state, i) {
9866 if (!conn_state->crtc)
9867 conn_state = old_conn_state;
9869 if (conn_state->crtc != crtc)
9872 aconnector = to_amdgpu_dm_connector(connector);
9873 if (!aconnector->mst_output_port || !aconnector->mst_root)
9882 return drm_dp_mst_add_affected_dsc_crtcs(state, &aconnector->mst_root->mst_mgr);
9886 * amdgpu_dm_atomic_check() - Atomic check implementation for AMDgpu DM.
9888 * @dev: The DRM device
9889 * @state: The atomic state to commit
9891 * Validate that the given atomic state is programmable by DC into hardware.
9892 * This involves constructing a &struct dc_state reflecting the new hardware
9893 * state we wish to commit, then querying DC to see if it is programmable. It's
9894 * important not to modify the existing DC state. Otherwise, atomic_check
9895 * may unexpectedly commit hardware changes.
9897 * When validating the DC state, it's important that the right locks are
9898 * acquired. For full updates case which removes/adds/updates streams on one
9899 * CRTC while flipping on another CRTC, acquiring global lock will guarantee
9900 * that any such full update commit will wait for completion of any outstanding
9901 * flip using DRMs synchronization events.
9903 * Note that DM adds the affected connectors for all CRTCs in state, when that
9904 * might not seem necessary. This is because DC stream creation requires the
9905 * DC sink, which is tied to the DRM connector state. Cleaning this up should
9906 * be possible but non-trivial - a possible TODO item.
9908 * Return: -Error code if validation failed.
9910 static int amdgpu_dm_atomic_check(struct drm_device *dev,
9911 struct drm_atomic_state *state)
9913 struct amdgpu_device *adev = drm_to_adev(dev);
9914 struct dm_atomic_state *dm_state = NULL;
9915 struct dc *dc = adev->dm.dc;
9916 struct drm_connector *connector;
9917 struct drm_connector_state *old_con_state, *new_con_state;
9918 struct drm_crtc *crtc;
9919 struct drm_crtc_state *old_crtc_state, *new_crtc_state;
9920 struct drm_plane *plane;
9921 struct drm_plane_state *old_plane_state, *new_plane_state;
9922 enum dc_status status;
9924 bool lock_and_validation_needed = false;
9925 bool is_top_most_overlay = true;
9926 struct dm_crtc_state *dm_old_crtc_state, *dm_new_crtc_state;
9927 struct drm_dp_mst_topology_mgr *mgr;
9928 struct drm_dp_mst_topology_state *mst_state;
9929 struct dsc_mst_fairness_vars vars[MAX_PIPES];
9931 trace_amdgpu_dm_atomic_check_begin(state);
9933 ret = drm_atomic_helper_check_modeset(dev, state);
9935 DRM_DEBUG_DRIVER("drm_atomic_helper_check_modeset() failed\n");
9939 /* Check connector changes */
9940 for_each_oldnew_connector_in_state(state, connector, old_con_state, new_con_state, i) {
9941 struct dm_connector_state *dm_old_con_state = to_dm_connector_state(old_con_state);
9942 struct dm_connector_state *dm_new_con_state = to_dm_connector_state(new_con_state);
9944 /* Skip connectors that are disabled or part of modeset already. */
9945 if (!new_con_state->crtc)
9948 new_crtc_state = drm_atomic_get_crtc_state(state, new_con_state->crtc);
9949 if (IS_ERR(new_crtc_state)) {
9950 DRM_DEBUG_DRIVER("drm_atomic_get_crtc_state() failed\n");
9951 ret = PTR_ERR(new_crtc_state);
9955 if (dm_old_con_state->abm_level != dm_new_con_state->abm_level ||
9956 dm_old_con_state->scaling != dm_new_con_state->scaling)
9957 new_crtc_state->connectors_changed = true;
9960 if (dc_resource_is_dsc_encoding_supported(dc)) {
9961 for_each_oldnew_crtc_in_state(state, crtc, old_crtc_state, new_crtc_state, i) {
9962 if (drm_atomic_crtc_needs_modeset(new_crtc_state)) {
9963 ret = add_affected_mst_dsc_crtcs(state, crtc);
9965 DRM_DEBUG_DRIVER("add_affected_mst_dsc_crtcs() failed\n");
9971 for_each_oldnew_crtc_in_state(state, crtc, old_crtc_state, new_crtc_state, i) {
9972 dm_old_crtc_state = to_dm_crtc_state(old_crtc_state);
9974 if (!drm_atomic_crtc_needs_modeset(new_crtc_state) &&
9975 !new_crtc_state->color_mgmt_changed &&
9976 old_crtc_state->vrr_enabled == new_crtc_state->vrr_enabled &&
9977 dm_old_crtc_state->dsc_force_changed == false)
9980 ret = amdgpu_dm_verify_lut_sizes(new_crtc_state);
9982 DRM_DEBUG_DRIVER("amdgpu_dm_verify_lut_sizes() failed\n");
9986 if (!new_crtc_state->enable)
9989 ret = drm_atomic_add_affected_connectors(state, crtc);
9991 DRM_DEBUG_DRIVER("drm_atomic_add_affected_connectors() failed\n");
9995 ret = drm_atomic_add_affected_planes(state, crtc);
9997 DRM_DEBUG_DRIVER("drm_atomic_add_affected_planes() failed\n");
10001 if (dm_old_crtc_state->dsc_force_changed)
10002 new_crtc_state->mode_changed = true;
10006 * Add all primary and overlay planes on the CRTC to the state
10007 * whenever a plane is enabled to maintain correct z-ordering
10008 * and to enable fast surface updates.
10010 drm_for_each_crtc(crtc, dev) {
10011 bool modified = false;
10013 for_each_oldnew_plane_in_state(state, plane, old_plane_state, new_plane_state, i) {
10014 if (plane->type == DRM_PLANE_TYPE_CURSOR)
10017 if (new_plane_state->crtc == crtc ||
10018 old_plane_state->crtc == crtc) {
10027 drm_for_each_plane_mask(plane, state->dev, crtc->state->plane_mask) {
10028 if (plane->type == DRM_PLANE_TYPE_CURSOR)
10032 drm_atomic_get_plane_state(state, plane);
10034 if (IS_ERR(new_plane_state)) {
10035 ret = PTR_ERR(new_plane_state);
10036 DRM_DEBUG_DRIVER("new_plane_state is BAD\n");
10043 * DC consults the zpos (layer_index in DC terminology) to determine the
10044 * hw plane on which to enable the hw cursor (see
10045 * `dcn10_can_pipe_disable_cursor`). By now, all modified planes are in
10046 * atomic state, so call drm helper to normalize zpos.
10048 ret = drm_atomic_normalize_zpos(dev, state);
10050 drm_dbg(dev, "drm_atomic_normalize_zpos() failed\n");
10054 /* Remove exiting planes if they are modified */
10055 for_each_oldnew_plane_in_state_reverse(state, plane, old_plane_state, new_plane_state, i) {
10056 if (old_plane_state->fb && new_plane_state->fb &&
10057 get_mem_type(old_plane_state->fb) !=
10058 get_mem_type(new_plane_state->fb))
10059 lock_and_validation_needed = true;
10061 ret = dm_update_plane_state(dc, state, plane,
10065 &lock_and_validation_needed,
10066 &is_top_most_overlay);
10068 DRM_DEBUG_DRIVER("dm_update_plane_state() failed\n");
10073 /* Disable all crtcs which require disable */
10074 for_each_oldnew_crtc_in_state(state, crtc, old_crtc_state, new_crtc_state, i) {
10075 ret = dm_update_crtc_state(&adev->dm, state, crtc,
10079 &lock_and_validation_needed);
10081 DRM_DEBUG_DRIVER("DISABLE: dm_update_crtc_state() failed\n");
10086 /* Enable all crtcs which require enable */
10087 for_each_oldnew_crtc_in_state(state, crtc, old_crtc_state, new_crtc_state, i) {
10088 ret = dm_update_crtc_state(&adev->dm, state, crtc,
10092 &lock_and_validation_needed);
10094 DRM_DEBUG_DRIVER("ENABLE: dm_update_crtc_state() failed\n");
10099 /* Add new/modified planes */
10100 for_each_oldnew_plane_in_state_reverse(state, plane, old_plane_state, new_plane_state, i) {
10101 ret = dm_update_plane_state(dc, state, plane,
10105 &lock_and_validation_needed,
10106 &is_top_most_overlay);
10108 DRM_DEBUG_DRIVER("dm_update_plane_state() failed\n");
10113 if (dc_resource_is_dsc_encoding_supported(dc)) {
10114 ret = pre_validate_dsc(state, &dm_state, vars);
10119 /* Run this here since we want to validate the streams we created */
10120 ret = drm_atomic_helper_check_planes(dev, state);
10122 DRM_DEBUG_DRIVER("drm_atomic_helper_check_planes() failed\n");
10126 for_each_new_crtc_in_state(state, crtc, new_crtc_state, i) {
10127 dm_new_crtc_state = to_dm_crtc_state(new_crtc_state);
10128 if (dm_new_crtc_state->mpo_requested)
10129 DRM_DEBUG_DRIVER("MPO enablement requested on crtc:[%p]\n", crtc);
10132 /* Check cursor planes scaling */
10133 for_each_new_crtc_in_state(state, crtc, new_crtc_state, i) {
10134 ret = dm_check_crtc_cursor(state, crtc, new_crtc_state);
10136 DRM_DEBUG_DRIVER("dm_check_crtc_cursor() failed\n");
10141 if (state->legacy_cursor_update) {
10143 * This is a fast cursor update coming from the plane update
10144 * helper, check if it can be done asynchronously for better
10147 state->async_update =
10148 !drm_atomic_helper_async_check(dev, state);
10151 * Skip the remaining global validation if this is an async
10152 * update. Cursor updates can be done without affecting
10153 * state or bandwidth calcs and this avoids the performance
10154 * penalty of locking the private state object and
10155 * allocating a new dc_state.
10157 if (state->async_update)
10161 /* Check scaling and underscan changes*/
10162 /* TODO Removed scaling changes validation due to inability to commit
10163 * new stream into context w\o causing full reset. Need to
10164 * decide how to handle.
10166 for_each_oldnew_connector_in_state(state, connector, old_con_state, new_con_state, i) {
10167 struct dm_connector_state *dm_old_con_state = to_dm_connector_state(old_con_state);
10168 struct dm_connector_state *dm_new_con_state = to_dm_connector_state(new_con_state);
10169 struct amdgpu_crtc *acrtc = to_amdgpu_crtc(dm_new_con_state->base.crtc);
10171 /* Skip any modesets/resets */
10172 if (!acrtc || drm_atomic_crtc_needs_modeset(
10173 drm_atomic_get_new_crtc_state(state, &acrtc->base)))
10176 /* Skip any thing not scale or underscan changes */
10177 if (!is_scaling_state_different(dm_new_con_state, dm_old_con_state))
10180 lock_and_validation_needed = true;
10183 /* set the slot info for each mst_state based on the link encoding format */
10184 for_each_new_mst_mgr_in_state(state, mgr, mst_state, i) {
10185 struct amdgpu_dm_connector *aconnector;
10186 struct drm_connector *connector;
10187 struct drm_connector_list_iter iter;
10188 u8 link_coding_cap;
10190 drm_connector_list_iter_begin(dev, &iter);
10191 drm_for_each_connector_iter(connector, &iter) {
10192 if (connector->index == mst_state->mgr->conn_base_id) {
10193 aconnector = to_amdgpu_dm_connector(connector);
10194 link_coding_cap = dc_link_dp_mst_decide_link_encoding_format(aconnector->dc_link);
10195 drm_dp_mst_update_slots(mst_state, link_coding_cap);
10200 drm_connector_list_iter_end(&iter);
10204 * Streams and planes are reset when there are changes that affect
10205 * bandwidth. Anything that affects bandwidth needs to go through
10206 * DC global validation to ensure that the configuration can be applied
10209 * We have to currently stall out here in atomic_check for outstanding
10210 * commits to finish in this case because our IRQ handlers reference
10211 * DRM state directly - we can end up disabling interrupts too early
10214 * TODO: Remove this stall and drop DM state private objects.
10216 if (lock_and_validation_needed) {
10217 ret = dm_atomic_get_state(state, &dm_state);
10219 DRM_DEBUG_DRIVER("dm_atomic_get_state() failed\n");
10223 ret = do_aquire_global_lock(dev, state);
10225 DRM_DEBUG_DRIVER("do_aquire_global_lock() failed\n");
10229 ret = compute_mst_dsc_configs_for_state(state, dm_state->context, vars);
10231 DRM_DEBUG_DRIVER("compute_mst_dsc_configs_for_state() failed\n");
10236 ret = dm_update_mst_vcpi_slots_for_dsc(state, dm_state->context, vars);
10238 DRM_DEBUG_DRIVER("dm_update_mst_vcpi_slots_for_dsc() failed\n");
10243 * Perform validation of MST topology in the state:
10244 * We need to perform MST atomic check before calling
10245 * dc_validate_global_state(), or there is a chance
10246 * to get stuck in an infinite loop and hang eventually.
10248 ret = drm_dp_mst_atomic_check(state);
10250 DRM_DEBUG_DRIVER("drm_dp_mst_atomic_check() failed\n");
10253 status = dc_validate_global_state(dc, dm_state->context, true);
10254 if (status != DC_OK) {
10255 DRM_DEBUG_DRIVER("DC global validation failure: %s (%d)",
10256 dc_status_to_str(status), status);
10262 * The commit is a fast update. Fast updates shouldn't change
10263 * the DC context, affect global validation, and can have their
10264 * commit work done in parallel with other commits not touching
10265 * the same resource. If we have a new DC context as part of
10266 * the DM atomic state from validation we need to free it and
10267 * retain the existing one instead.
10269 * Furthermore, since the DM atomic state only contains the DC
10270 * context and can safely be annulled, we can free the state
10271 * and clear the associated private object now to free
10272 * some memory and avoid a possible use-after-free later.
10275 for (i = 0; i < state->num_private_objs; i++) {
10276 struct drm_private_obj *obj = state->private_objs[i].ptr;
10278 if (obj->funcs == adev->dm.atomic_obj.funcs) {
10279 int j = state->num_private_objs-1;
10281 dm_atomic_destroy_state(obj,
10282 state->private_objs[i].state);
10284 /* If i is not at the end of the array then the
10285 * last element needs to be moved to where i was
10286 * before the array can safely be truncated.
10289 state->private_objs[i] =
10290 state->private_objs[j];
10292 state->private_objs[j].ptr = NULL;
10293 state->private_objs[j].state = NULL;
10294 state->private_objs[j].old_state = NULL;
10295 state->private_objs[j].new_state = NULL;
10297 state->num_private_objs = j;
10303 /* Store the overall update type for use later in atomic check. */
10304 for_each_new_crtc_in_state(state, crtc, new_crtc_state, i) {
10305 struct dm_crtc_state *dm_new_crtc_state =
10306 to_dm_crtc_state(new_crtc_state);
10309 * Only allow async flips for fast updates that don't change
10310 * the FB pitch, the DCC state, rotation, etc.
10312 if (new_crtc_state->async_flip && lock_and_validation_needed) {
10313 drm_dbg_atomic(crtc->dev,
10314 "[CRTC:%d:%s] async flips are only supported for fast updates\n",
10315 crtc->base.id, crtc->name);
10320 dm_new_crtc_state->update_type = lock_and_validation_needed ?
10321 UPDATE_TYPE_FULL : UPDATE_TYPE_FAST;
10324 /* Must be success */
10327 trace_amdgpu_dm_atomic_check_finish(state, ret);
10332 if (ret == -EDEADLK)
10333 DRM_DEBUG_DRIVER("Atomic check stopped to avoid deadlock.\n");
10334 else if (ret == -EINTR || ret == -EAGAIN || ret == -ERESTARTSYS)
10335 DRM_DEBUG_DRIVER("Atomic check stopped due to signal.\n");
10337 DRM_DEBUG_DRIVER("Atomic check failed with err: %d\n", ret);
10339 trace_amdgpu_dm_atomic_check_finish(state, ret);
10344 static bool is_dp_capable_without_timing_msa(struct dc *dc,
10345 struct amdgpu_dm_connector *amdgpu_dm_connector)
10348 bool capable = false;
10350 if (amdgpu_dm_connector->dc_link &&
10351 dm_helpers_dp_read_dpcd(
10353 amdgpu_dm_connector->dc_link,
10354 DP_DOWN_STREAM_PORT_COUNT,
10356 sizeof(dpcd_data))) {
10357 capable = (dpcd_data & DP_MSA_TIMING_PAR_IGNORED) ? true:false;
10363 static bool dm_edid_parser_send_cea(struct amdgpu_display_manager *dm,
10364 unsigned int offset,
10365 unsigned int total_length,
10367 unsigned int length,
10368 struct amdgpu_hdmi_vsdb_info *vsdb)
10371 union dmub_rb_cmd cmd;
10372 struct dmub_cmd_send_edid_cea *input;
10373 struct dmub_cmd_edid_cea_output *output;
10375 if (length > DMUB_EDID_CEA_DATA_CHUNK_BYTES)
10378 memset(&cmd, 0, sizeof(cmd));
10380 input = &cmd.edid_cea.data.input;
10382 cmd.edid_cea.header.type = DMUB_CMD__EDID_CEA;
10383 cmd.edid_cea.header.sub_type = 0;
10384 cmd.edid_cea.header.payload_bytes =
10385 sizeof(cmd.edid_cea) - sizeof(cmd.edid_cea.header);
10386 input->offset = offset;
10387 input->length = length;
10388 input->cea_total_length = total_length;
10389 memcpy(input->payload, data, length);
10391 res = dm_execute_dmub_cmd(dm->dc->ctx, &cmd, DM_DMUB_WAIT_TYPE_WAIT_WITH_REPLY);
10393 DRM_ERROR("EDID CEA parser failed\n");
10397 output = &cmd.edid_cea.data.output;
10399 if (output->type == DMUB_CMD__EDID_CEA_ACK) {
10400 if (!output->ack.success) {
10401 DRM_ERROR("EDID CEA ack failed at offset %d\n",
10402 output->ack.offset);
10404 } else if (output->type == DMUB_CMD__EDID_CEA_AMD_VSDB) {
10405 if (!output->amd_vsdb.vsdb_found)
10408 vsdb->freesync_supported = output->amd_vsdb.freesync_supported;
10409 vsdb->amd_vsdb_version = output->amd_vsdb.amd_vsdb_version;
10410 vsdb->min_refresh_rate_hz = output->amd_vsdb.min_frame_rate;
10411 vsdb->max_refresh_rate_hz = output->amd_vsdb.max_frame_rate;
10413 DRM_WARN("Unknown EDID CEA parser results\n");
10420 static bool parse_edid_cea_dmcu(struct amdgpu_display_manager *dm,
10421 u8 *edid_ext, int len,
10422 struct amdgpu_hdmi_vsdb_info *vsdb_info)
10426 /* send extension block to DMCU for parsing */
10427 for (i = 0; i < len; i += 8) {
10431 /* send 8 bytes a time */
10432 if (!dc_edid_parser_send_cea(dm->dc, i, len, &edid_ext[i], 8))
10436 /* EDID block sent completed, expect result */
10437 int version, min_rate, max_rate;
10439 res = dc_edid_parser_recv_amd_vsdb(dm->dc, &version, &min_rate, &max_rate);
10441 /* amd vsdb found */
10442 vsdb_info->freesync_supported = 1;
10443 vsdb_info->amd_vsdb_version = version;
10444 vsdb_info->min_refresh_rate_hz = min_rate;
10445 vsdb_info->max_refresh_rate_hz = max_rate;
10453 res = dc_edid_parser_recv_cea_ack(dm->dc, &offset);
10461 static bool parse_edid_cea_dmub(struct amdgpu_display_manager *dm,
10462 u8 *edid_ext, int len,
10463 struct amdgpu_hdmi_vsdb_info *vsdb_info)
10467 /* send extension block to DMCU for parsing */
10468 for (i = 0; i < len; i += 8) {
10469 /* send 8 bytes a time */
10470 if (!dm_edid_parser_send_cea(dm, i, len, &edid_ext[i], 8, vsdb_info))
10474 return vsdb_info->freesync_supported;
10477 static bool parse_edid_cea(struct amdgpu_dm_connector *aconnector,
10478 u8 *edid_ext, int len,
10479 struct amdgpu_hdmi_vsdb_info *vsdb_info)
10481 struct amdgpu_device *adev = drm_to_adev(aconnector->base.dev);
10484 mutex_lock(&adev->dm.dc_lock);
10485 if (adev->dm.dmub_srv)
10486 ret = parse_edid_cea_dmub(&adev->dm, edid_ext, len, vsdb_info);
10488 ret = parse_edid_cea_dmcu(&adev->dm, edid_ext, len, vsdb_info);
10489 mutex_unlock(&adev->dm.dc_lock);
10493 static int parse_amd_vsdb(struct amdgpu_dm_connector *aconnector,
10494 struct edid *edid, struct amdgpu_hdmi_vsdb_info *vsdb_info)
10496 u8 *edid_ext = NULL;
10500 if (edid == NULL || edid->extensions == 0)
10503 /* Find DisplayID extension */
10504 for (i = 0; i < edid->extensions; i++) {
10505 edid_ext = (void *)(edid + (i + 1));
10506 if (edid_ext[0] == DISPLAYID_EXT)
10510 while (j < EDID_LENGTH) {
10511 struct amd_vsdb_block *amd_vsdb = (struct amd_vsdb_block *)&edid_ext[j];
10512 unsigned int ieeeId = (amd_vsdb->ieee_id[2] << 16) | (amd_vsdb->ieee_id[1] << 8) | (amd_vsdb->ieee_id[0]);
10514 if (ieeeId == HDMI_AMD_VENDOR_SPECIFIC_DATA_BLOCK_IEEE_REGISTRATION_ID &&
10515 amd_vsdb->version == HDMI_AMD_VENDOR_SPECIFIC_DATA_BLOCK_VERSION_3) {
10516 vsdb_info->replay_mode = (amd_vsdb->feature_caps & AMD_VSDB_VERSION_3_FEATURECAP_REPLAYMODE) ? true : false;
10517 vsdb_info->amd_vsdb_version = HDMI_AMD_VENDOR_SPECIFIC_DATA_BLOCK_VERSION_3;
10518 DRM_DEBUG_KMS("Panel supports Replay Mode: %d\n", vsdb_info->replay_mode);
10528 static int parse_hdmi_amd_vsdb(struct amdgpu_dm_connector *aconnector,
10529 struct edid *edid, struct amdgpu_hdmi_vsdb_info *vsdb_info)
10531 u8 *edid_ext = NULL;
10533 bool valid_vsdb_found = false;
10535 /*----- drm_find_cea_extension() -----*/
10536 /* No EDID or EDID extensions */
10537 if (edid == NULL || edid->extensions == 0)
10540 /* Find CEA extension */
10541 for (i = 0; i < edid->extensions; i++) {
10542 edid_ext = (uint8_t *)edid + EDID_LENGTH * (i + 1);
10543 if (edid_ext[0] == CEA_EXT)
10547 if (i == edid->extensions)
10550 /*----- cea_db_offsets() -----*/
10551 if (edid_ext[0] != CEA_EXT)
10554 valid_vsdb_found = parse_edid_cea(aconnector, edid_ext, EDID_LENGTH, vsdb_info);
10556 return valid_vsdb_found ? i : -ENODEV;
10560 * amdgpu_dm_update_freesync_caps - Update Freesync capabilities
10562 * @connector: Connector to query.
10563 * @edid: EDID from monitor
10565 * Amdgpu supports Freesync in DP and HDMI displays, and it is required to keep
10566 * track of some of the display information in the internal data struct used by
10567 * amdgpu_dm. This function checks which type of connector we need to set the
10568 * FreeSync parameters.
10570 void amdgpu_dm_update_freesync_caps(struct drm_connector *connector,
10574 struct detailed_timing *timing;
10575 struct detailed_non_pixel *data;
10576 struct detailed_data_monitor_range *range;
10577 struct amdgpu_dm_connector *amdgpu_dm_connector =
10578 to_amdgpu_dm_connector(connector);
10579 struct dm_connector_state *dm_con_state = NULL;
10580 struct dc_sink *sink;
10582 struct drm_device *dev = connector->dev;
10583 struct amdgpu_device *adev = drm_to_adev(dev);
10584 struct amdgpu_hdmi_vsdb_info vsdb_info = {0};
10585 bool freesync_capable = false;
10586 enum adaptive_sync_type as_type = ADAPTIVE_SYNC_TYPE_NONE;
10588 if (!connector->state) {
10589 DRM_ERROR("%s - Connector has no state", __func__);
10593 sink = amdgpu_dm_connector->dc_sink ?
10594 amdgpu_dm_connector->dc_sink :
10595 amdgpu_dm_connector->dc_em_sink;
10597 if (!edid || !sink) {
10598 dm_con_state = to_dm_connector_state(connector->state);
10600 amdgpu_dm_connector->min_vfreq = 0;
10601 amdgpu_dm_connector->max_vfreq = 0;
10602 amdgpu_dm_connector->pixel_clock_mhz = 0;
10603 connector->display_info.monitor_range.min_vfreq = 0;
10604 connector->display_info.monitor_range.max_vfreq = 0;
10605 freesync_capable = false;
10610 dm_con_state = to_dm_connector_state(connector->state);
10612 if (!adev->dm.freesync_module)
10615 if (sink->sink_signal == SIGNAL_TYPE_DISPLAY_PORT
10616 || sink->sink_signal == SIGNAL_TYPE_EDP) {
10617 bool edid_check_required = false;
10620 edid_check_required = is_dp_capable_without_timing_msa(
10622 amdgpu_dm_connector);
10625 if (edid_check_required == true && (edid->version > 1 ||
10626 (edid->version == 1 && edid->revision > 1))) {
10627 for (i = 0; i < 4; i++) {
10629 timing = &edid->detailed_timings[i];
10630 data = &timing->data.other_data;
10631 range = &data->data.range;
10633 * Check if monitor has continuous frequency mode
10635 if (data->type != EDID_DETAIL_MONITOR_RANGE)
10638 * Check for flag range limits only. If flag == 1 then
10639 * no additional timing information provided.
10640 * Default GTF, GTF Secondary curve and CVT are not
10643 if (range->flags != 1)
10646 amdgpu_dm_connector->min_vfreq = range->min_vfreq;
10647 amdgpu_dm_connector->max_vfreq = range->max_vfreq;
10648 amdgpu_dm_connector->pixel_clock_mhz =
10649 range->pixel_clock_mhz * 10;
10651 connector->display_info.monitor_range.min_vfreq = range->min_vfreq;
10652 connector->display_info.monitor_range.max_vfreq = range->max_vfreq;
10657 if (amdgpu_dm_connector->max_vfreq -
10658 amdgpu_dm_connector->min_vfreq > 10) {
10660 freesync_capable = true;
10663 parse_amd_vsdb(amdgpu_dm_connector, edid, &vsdb_info);
10665 if (vsdb_info.replay_mode) {
10666 amdgpu_dm_connector->vsdb_info.replay_mode = vsdb_info.replay_mode;
10667 amdgpu_dm_connector->vsdb_info.amd_vsdb_version = vsdb_info.amd_vsdb_version;
10668 amdgpu_dm_connector->as_type = ADAPTIVE_SYNC_TYPE_EDP;
10671 } else if (edid && sink->sink_signal == SIGNAL_TYPE_HDMI_TYPE_A) {
10672 i = parse_hdmi_amd_vsdb(amdgpu_dm_connector, edid, &vsdb_info);
10673 if (i >= 0 && vsdb_info.freesync_supported) {
10674 timing = &edid->detailed_timings[i];
10675 data = &timing->data.other_data;
10677 amdgpu_dm_connector->min_vfreq = vsdb_info.min_refresh_rate_hz;
10678 amdgpu_dm_connector->max_vfreq = vsdb_info.max_refresh_rate_hz;
10679 if (amdgpu_dm_connector->max_vfreq - amdgpu_dm_connector->min_vfreq > 10)
10680 freesync_capable = true;
10682 connector->display_info.monitor_range.min_vfreq = vsdb_info.min_refresh_rate_hz;
10683 connector->display_info.monitor_range.max_vfreq = vsdb_info.max_refresh_rate_hz;
10687 as_type = dm_get_adaptive_sync_support_type(amdgpu_dm_connector->dc_link);
10689 if (as_type == FREESYNC_TYPE_PCON_IN_WHITELIST) {
10690 i = parse_hdmi_amd_vsdb(amdgpu_dm_connector, edid, &vsdb_info);
10691 if (i >= 0 && vsdb_info.freesync_supported && vsdb_info.amd_vsdb_version > 0) {
10693 amdgpu_dm_connector->pack_sdp_v1_3 = true;
10694 amdgpu_dm_connector->as_type = as_type;
10695 amdgpu_dm_connector->vsdb_info = vsdb_info;
10697 amdgpu_dm_connector->min_vfreq = vsdb_info.min_refresh_rate_hz;
10698 amdgpu_dm_connector->max_vfreq = vsdb_info.max_refresh_rate_hz;
10699 if (amdgpu_dm_connector->max_vfreq - amdgpu_dm_connector->min_vfreq > 10)
10700 freesync_capable = true;
10702 connector->display_info.monitor_range.min_vfreq = vsdb_info.min_refresh_rate_hz;
10703 connector->display_info.monitor_range.max_vfreq = vsdb_info.max_refresh_rate_hz;
10709 dm_con_state->freesync_capable = freesync_capable;
10711 if (connector->vrr_capable_property)
10712 drm_connector_set_vrr_capable_property(connector,
10716 void amdgpu_dm_trigger_timing_sync(struct drm_device *dev)
10718 struct amdgpu_device *adev = drm_to_adev(dev);
10719 struct dc *dc = adev->dm.dc;
10722 mutex_lock(&adev->dm.dc_lock);
10723 if (dc->current_state) {
10724 for (i = 0; i < dc->current_state->stream_count; ++i)
10725 dc->current_state->streams[i]
10726 ->triggered_crtc_reset.enabled =
10727 adev->dm.force_timing_sync;
10729 dm_enable_per_frame_crtc_master_sync(dc->current_state);
10730 dc_trigger_sync(dc, dc->current_state);
10732 mutex_unlock(&adev->dm.dc_lock);
10735 void dm_write_reg_func(const struct dc_context *ctx, uint32_t address,
10736 u32 value, const char *func_name)
10738 #ifdef DM_CHECK_ADDR_0
10739 if (address == 0) {
10740 DC_ERR("invalid register write. address = 0");
10744 cgs_write_register(ctx->cgs_device, address, value);
10745 trace_amdgpu_dc_wreg(&ctx->perf_trace->write_count, address, value);
10748 uint32_t dm_read_reg_func(const struct dc_context *ctx, uint32_t address,
10749 const char *func_name)
10752 #ifdef DM_CHECK_ADDR_0
10753 if (address == 0) {
10754 DC_ERR("invalid register read; address = 0\n");
10759 if (ctx->dmub_srv &&
10760 ctx->dmub_srv->reg_helper_offload.gather_in_progress &&
10761 !ctx->dmub_srv->reg_helper_offload.should_burst_write) {
10766 value = cgs_read_register(ctx->cgs_device, address);
10768 trace_amdgpu_dc_rreg(&ctx->perf_trace->read_count, address, value);
10773 int amdgpu_dm_process_dmub_aux_transfer_sync(
10774 struct dc_context *ctx,
10775 unsigned int link_index,
10776 struct aux_payload *payload,
10777 enum aux_return_code_type *operation_result)
10779 struct amdgpu_device *adev = ctx->driver_context;
10780 struct dmub_notification *p_notify = adev->dm.dmub_notify;
10783 mutex_lock(&adev->dm.dpia_aux_lock);
10784 if (!dc_process_dmub_aux_transfer_async(ctx->dc, link_index, payload)) {
10785 *operation_result = AUX_RET_ERROR_ENGINE_ACQUIRE;
10789 if (!wait_for_completion_timeout(&adev->dm.dmub_aux_transfer_done, 10 * HZ)) {
10790 DRM_ERROR("wait_for_completion_timeout timeout!");
10791 *operation_result = AUX_RET_ERROR_TIMEOUT;
10795 if (p_notify->result != AUX_RET_SUCCESS) {
10797 * Transient states before tunneling is enabled could
10798 * lead to this error. We can ignore this for now.
10800 if (p_notify->result != AUX_RET_ERROR_PROTOCOL_ERROR) {
10801 DRM_WARN("DPIA AUX failed on 0x%x(%d), error %d\n",
10802 payload->address, payload->length,
10805 *operation_result = AUX_RET_ERROR_INVALID_REPLY;
10810 payload->reply[0] = adev->dm.dmub_notify->aux_reply.command;
10811 if (!payload->write && p_notify->aux_reply.length &&
10812 (payload->reply[0] == AUX_TRANSACTION_REPLY_AUX_ACK)) {
10814 if (payload->length != p_notify->aux_reply.length) {
10815 DRM_WARN("invalid read length %d from DPIA AUX 0x%x(%d)!\n",
10816 p_notify->aux_reply.length,
10817 payload->address, payload->length);
10818 *operation_result = AUX_RET_ERROR_INVALID_REPLY;
10822 memcpy(payload->data, p_notify->aux_reply.data,
10823 p_notify->aux_reply.length);
10827 ret = p_notify->aux_reply.length;
10828 *operation_result = p_notify->result;
10830 reinit_completion(&adev->dm.dmub_aux_transfer_done);
10831 mutex_unlock(&adev->dm.dpia_aux_lock);
10835 int amdgpu_dm_process_dmub_set_config_sync(
10836 struct dc_context *ctx,
10837 unsigned int link_index,
10838 struct set_config_cmd_payload *payload,
10839 enum set_config_status *operation_result)
10841 struct amdgpu_device *adev = ctx->driver_context;
10842 bool is_cmd_complete;
10845 mutex_lock(&adev->dm.dpia_aux_lock);
10846 is_cmd_complete = dc_process_dmub_set_config_async(ctx->dc,
10847 link_index, payload, adev->dm.dmub_notify);
10849 if (is_cmd_complete || wait_for_completion_timeout(&adev->dm.dmub_aux_transfer_done, 10 * HZ)) {
10851 *operation_result = adev->dm.dmub_notify->sc_status;
10853 DRM_ERROR("wait_for_completion_timeout timeout!");
10855 *operation_result = SET_CONFIG_UNKNOWN_ERROR;
10858 if (!is_cmd_complete)
10859 reinit_completion(&adev->dm.dmub_aux_transfer_done);
10860 mutex_unlock(&adev->dm.dpia_aux_lock);
10865 * Check whether seamless boot is supported.
10867 * So far we only support seamless boot on CHIP_VANGOGH.
10868 * If everything goes well, we may consider expanding
10869 * seamless boot to other ASICs.
10871 bool check_seamless_boot_capability(struct amdgpu_device *adev)
10873 switch (adev->ip_versions[DCE_HWIP][0]) {
10874 case IP_VERSION(3, 0, 1):
10875 if (!adev->mman.keep_stolen_vga_memory)
10885 bool dm_execute_dmub_cmd(const struct dc_context *ctx, union dmub_rb_cmd *cmd, enum dm_dmub_wait_type wait_type)
10887 return dc_dmub_srv_cmd_run(ctx->dmub_srv, cmd, wait_type);
10890 bool dm_execute_dmub_cmd_list(const struct dc_context *ctx, unsigned int count, union dmub_rb_cmd *cmd, enum dm_dmub_wait_type wait_type)
10892 return dc_dmub_srv_cmd_run_list(ctx->dmub_srv, count, cmd, wait_type);