drm/amdgpu: skip the invalid workload type
[linux-2.6-block.git] / drivers / gpu / drm / amd / display / amdgpu_dm / amdgpu_dm.c
CommitLineData
4562236b
HW
1/*
2 * Copyright 2015 Advanced Micro Devices, Inc.
3 *
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:
10 *
11 * The above copyright notice and this permission notice shall be included in
12 * all copies or substantial portions of the Software.
13 *
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.
21 *
22 * Authors: AMD
23 *
24 */
25
0cf5eb76
DF
26/* The caprices of the preprocessor require that this be declared right here */
27#define CREATE_TRACE_POINTS
28
4562236b
HW
29#include "dm_services_types.h"
30#include "dc.h"
f6e03f80 31#include "link_enc_cfg.h"
1dc90497 32#include "dc/inc/core_types.h"
a7669aff 33#include "dal_asic_id.h"
cdca3f21 34#include "dmub/dmub_srv.h"
743b9786
NK
35#include "dc/inc/hw/dmcu.h"
36#include "dc/inc/hw/abm.h"
9a71c7d3 37#include "dc/dc_dmub_srv.h"
f9b4f20c 38#include "dc/dc_edid_parser.h"
81927e28 39#include "dc/dc_stat.h"
9d83722d 40#include "amdgpu_dm_trace.h"
028c4ccf 41#include "dpcd_defs.h"
bc33f5e5 42#include "link/protocols/link_dpcd.h"
028c4ccf 43#include "link_service_types.h"
1e5d4d8e
RL
44#include "link/protocols/link_dp_capability.h"
45#include "link/protocols/link_ddc.h"
4562236b
HW
46
47#include "vid.h"
48#include "amdgpu.h"
a49dcb88 49#include "amdgpu_display.h"
a94d5569 50#include "amdgpu_ucode.h"
4562236b
HW
51#include "atom.h"
52#include "amdgpu_dm.h"
5d945cbc 53#include "amdgpu_dm_plane.h"
473683a0 54#include "amdgpu_dm_crtc.h"
52704fca
BL
55#ifdef CONFIG_DRM_AMD_DC_HDCP
56#include "amdgpu_dm_hdcp.h"
6a99099f 57#include <drm/display/drm_hdcp_helper.h>
52704fca 58#endif
e7b07cee 59#include "amdgpu_pm.h"
1f579254 60#include "amdgpu_atombios.h"
4562236b
HW
61
62#include "amd_shared.h"
63#include "amdgpu_dm_irq.h"
64#include "dm_helpers.h"
e7b07cee 65#include "amdgpu_dm_mst_types.h"
dc38fd9d
DF
66#if defined(CONFIG_DEBUG_FS)
67#include "amdgpu_dm_debugfs.h"
68#endif
f4594cd1 69#include "amdgpu_dm_psr.h"
4562236b
HW
70
71#include "ivsrcid/ivsrcid_vislands30.h"
72
a6276e92 73#include <linux/backlight.h>
4562236b
HW
74#include <linux/module.h>
75#include <linux/moduleparam.h>
e7b07cee 76#include <linux/types.h>
97028037 77#include <linux/pm_runtime.h>
09d21852 78#include <linux/pci.h>
a94d5569 79#include <linux/firmware.h>
6ce8f316 80#include <linux/component.h>
57b9f338 81#include <linux/dmi.h>
4562236b 82
da68386d 83#include <drm/display/drm_dp_mst_helper.h>
4fc8cb47 84#include <drm/display/drm_hdmi_helper.h>
4562236b 85#include <drm/drm_atomic.h>
674e78ac 86#include <drm/drm_atomic_uapi.h>
4562236b 87#include <drm/drm_atomic_helper.h>
90bb087f 88#include <drm/drm_blend.h>
09d21852 89#include <drm/drm_fourcc.h>
e7b07cee 90#include <drm/drm_edid.h>
09d21852 91#include <drm/drm_vblank.h>
6ce8f316 92#include <drm/drm_audio_component.h>
047de3f1 93#include <drm/drm_gem_atomic_helper.h>
30c63715 94#include <drm/drm_plane_helper.h>
4562236b 95
da11ef83
HG
96#include <acpi/video.h>
97
5527cd06 98#include "ivsrcid/dcn/irqsrcs_dcn_1_0.h"
ff5ef992 99
ad941f7a
FX
100#include "dcn/dcn_1_0_offset.h"
101#include "dcn/dcn_1_0_sh_mask.h"
407e7517 102#include "soc15_hw_ip.h"
543036a2 103#include "soc15_common.h"
407e7517 104#include "vega10_ip_offset.h"
ff5ef992 105
543036a2
AP
106#include "gc/gc_11_0_0_offset.h"
107#include "gc/gc_11_0_0_sh_mask.h"
108
e7b07cee 109#include "modules/inc/mod_freesync.h"
bbf854dc 110#include "modules/power/power_helpers.h"
e7b07cee 111
743b9786
NK
112#define FIRMWARE_RENOIR_DMUB "amdgpu/renoir_dmcub.bin"
113MODULE_FIRMWARE(FIRMWARE_RENOIR_DMUB);
79037324
BL
114#define FIRMWARE_SIENNA_CICHLID_DMUB "amdgpu/sienna_cichlid_dmcub.bin"
115MODULE_FIRMWARE(FIRMWARE_SIENNA_CICHLID_DMUB);
5ce868fc
BL
116#define FIRMWARE_NAVY_FLOUNDER_DMUB "amdgpu/navy_flounder_dmcub.bin"
117MODULE_FIRMWARE(FIRMWARE_NAVY_FLOUNDER_DMUB);
71c0fd92
RL
118#define FIRMWARE_GREEN_SARDINE_DMUB "amdgpu/green_sardine_dmcub.bin"
119MODULE_FIRMWARE(FIRMWARE_GREEN_SARDINE_DMUB);
469989ca
RL
120#define FIRMWARE_VANGOGH_DMUB "amdgpu/vangogh_dmcub.bin"
121MODULE_FIRMWARE(FIRMWARE_VANGOGH_DMUB);
2a411205
BL
122#define FIRMWARE_DIMGREY_CAVEFISH_DMUB "amdgpu/dimgrey_cavefish_dmcub.bin"
123MODULE_FIRMWARE(FIRMWARE_DIMGREY_CAVEFISH_DMUB);
656fe9b6
AP
124#define FIRMWARE_BEIGE_GOBY_DMUB "amdgpu/beige_goby_dmcub.bin"
125MODULE_FIRMWARE(FIRMWARE_BEIGE_GOBY_DMUB);
1ebcaebd
NK
126#define FIRMWARE_YELLOW_CARP_DMUB "amdgpu/yellow_carp_dmcub.bin"
127MODULE_FIRMWARE(FIRMWARE_YELLOW_CARP_DMUB);
e850f6b1
RL
128#define FIRMWARE_DCN_314_DMUB "amdgpu/dcn_3_1_4_dmcub.bin"
129MODULE_FIRMWARE(FIRMWARE_DCN_314_DMUB);
b5b8ed44
QZ
130#define FIRMWARE_DCN_315_DMUB "amdgpu/dcn_3_1_5_dmcub.bin"
131MODULE_FIRMWARE(FIRMWARE_DCN_315_DMUB);
de7cc1b4
PL
132#define FIRMWARE_DCN316_DMUB "amdgpu/dcn_3_1_6_dmcub.bin"
133MODULE_FIRMWARE(FIRMWARE_DCN316_DMUB);
2200eb9e 134
577359ca
AP
135#define FIRMWARE_DCN_V3_2_0_DMCUB "amdgpu/dcn_3_2_0_dmcub.bin"
136MODULE_FIRMWARE(FIRMWARE_DCN_V3_2_0_DMCUB);
137#define FIRMWARE_DCN_V3_2_1_DMCUB "amdgpu/dcn_3_2_1_dmcub.bin"
138MODULE_FIRMWARE(FIRMWARE_DCN_V3_2_1_DMCUB);
139
a94d5569
DF
140#define FIRMWARE_RAVEN_DMCU "amdgpu/raven_dmcu.bin"
141MODULE_FIRMWARE(FIRMWARE_RAVEN_DMCU);
e7b07cee 142
5ea23931
RL
143#define FIRMWARE_NAVI12_DMCU "amdgpu/navi12_dmcu.bin"
144MODULE_FIRMWARE(FIRMWARE_NAVI12_DMCU);
145
8c7aea40
NK
146/* Number of bytes in PSP header for firmware. */
147#define PSP_HEADER_BYTES 0x100
148
149/* Number of bytes in PSP footer for firmware. */
150#define PSP_FOOTER_BYTES 0x100
151
b8592b48
LL
152/**
153 * DOC: overview
154 *
155 * The AMDgpu display manager, **amdgpu_dm** (or even simpler,
ec5c0ffa 156 * **dm**) sits between DRM and DC. It acts as a liaison, converting DRM
b8592b48
LL
157 * requests into DC requests, and DC responses into DRM responses.
158 *
159 * The root control structure is &struct amdgpu_display_manager.
160 */
161
7578ecda
AD
162/* basic init/fini API */
163static int amdgpu_dm_init(struct amdgpu_device *adev);
164static void amdgpu_dm_fini(struct amdgpu_device *adev);
fe8858bb 165static bool is_freesync_video_mode(const struct drm_display_mode *mode, struct amdgpu_dm_connector *aconnector);
7578ecda 166
0f877894
OV
167static enum drm_mode_subconnector get_subconnector_type(struct dc_link *link)
168{
169 switch (link->dpcd_caps.dongle_type) {
170 case DISPLAY_DONGLE_NONE:
171 return DRM_MODE_SUBCONNECTOR_Native;
172 case DISPLAY_DONGLE_DP_VGA_CONVERTER:
173 return DRM_MODE_SUBCONNECTOR_VGA;
174 case DISPLAY_DONGLE_DP_DVI_CONVERTER:
175 case DISPLAY_DONGLE_DP_DVI_DONGLE:
176 return DRM_MODE_SUBCONNECTOR_DVID;
177 case DISPLAY_DONGLE_DP_HDMI_CONVERTER:
178 case DISPLAY_DONGLE_DP_HDMI_DONGLE:
179 return DRM_MODE_SUBCONNECTOR_HDMIA;
180 case DISPLAY_DONGLE_DP_HDMI_MISMATCHED_DONGLE:
181 default:
182 return DRM_MODE_SUBCONNECTOR_Unknown;
183 }
184}
185
186static void update_subconnector_property(struct amdgpu_dm_connector *aconnector)
187{
188 struct dc_link *link = aconnector->dc_link;
189 struct drm_connector *connector = &aconnector->base;
190 enum drm_mode_subconnector subconnector = DRM_MODE_SUBCONNECTOR_Unknown;
191
192 if (connector->connector_type != DRM_MODE_CONNECTOR_DisplayPort)
193 return;
194
195 if (aconnector->dc_sink)
196 subconnector = get_subconnector_type(link);
197
198 drm_object_property_set_value(&connector->base,
199 connector->dev->mode_config.dp_subconnector_property,
200 subconnector);
201}
202
1f6010a9
DF
203/*
204 * initializes drm_device display related structures, based on the information
7578ecda
AD
205 * provided by DAL. The drm strcutures are: drm_crtc, drm_connector,
206 * drm_encoder, drm_mode_config
207 *
208 * Returns 0 on success
209 */
210static int amdgpu_dm_initialize_drm_device(struct amdgpu_device *adev);
211/* removes and deallocates the drm structures, created by the above function */
212static void amdgpu_dm_destroy_drm_device(struct amdgpu_display_manager *dm);
213
7578ecda
AD
214static int amdgpu_dm_connector_init(struct amdgpu_display_manager *dm,
215 struct amdgpu_dm_connector *amdgpu_dm_connector,
ae67558b 216 u32 link_index,
7578ecda
AD
217 struct amdgpu_encoder *amdgpu_encoder);
218static int amdgpu_dm_encoder_init(struct drm_device *dev,
219 struct amdgpu_encoder *aencoder,
220 uint32_t link_index);
221
222static int amdgpu_dm_connector_get_modes(struct drm_connector *connector);
223
7578ecda
AD
224static void amdgpu_dm_atomic_commit_tail(struct drm_atomic_state *state);
225
226static int amdgpu_dm_atomic_check(struct drm_device *dev,
227 struct drm_atomic_state *state);
228
e27c41d5 229static void handle_hpd_irq_helper(struct amdgpu_dm_connector *aconnector);
c40a09e5 230static void handle_hpd_rx_irq(void *param);
e27c41d5 231
a85ba005
NC
232static bool
233is_timing_unchanged_for_freesync(struct drm_crtc_state *old_crtc_state,
234 struct drm_crtc_state *new_crtc_state);
4562236b
HW
235/*
236 * dm_vblank_get_counter
237 *
238 * @brief
239 * Get counter for number of vertical blanks
240 *
241 * @param
242 * struct amdgpu_device *adev - [in] desired amdgpu device
243 * int disp_idx - [in] which CRTC to get the counter from
244 *
245 * @return
246 * Counter for vertical blanks
247 */
248static u32 dm_vblank_get_counter(struct amdgpu_device *adev, int crtc)
249{
250 if (crtc >= adev->mode_info.num_crtc)
251 return 0;
252 else {
253 struct amdgpu_crtc *acrtc = adev->mode_info.crtcs[crtc];
254
585d450c 255 if (acrtc->dm_irq_params.stream == NULL) {
0971c40e
HW
256 DRM_ERROR("dc_stream_state is NULL for crtc '%d'!\n",
257 crtc);
4562236b
HW
258 return 0;
259 }
260
585d450c 261 return dc_stream_get_vblank_counter(acrtc->dm_irq_params.stream);
4562236b
HW
262 }
263}
264
265static int dm_crtc_get_scanoutpos(struct amdgpu_device *adev, int crtc,
3ee6b26b 266 u32 *vbl, u32 *position)
4562236b 267{
ae67558b 268 u32 v_blank_start, v_blank_end, h_position, v_position;
81c50963 269
4562236b
HW
270 if ((crtc < 0) || (crtc >= adev->mode_info.num_crtc))
271 return -EINVAL;
272 else {
273 struct amdgpu_crtc *acrtc = adev->mode_info.crtcs[crtc];
274
585d450c 275 if (acrtc->dm_irq_params.stream == NULL) {
0971c40e
HW
276 DRM_ERROR("dc_stream_state is NULL for crtc '%d'!\n",
277 crtc);
4562236b
HW
278 return 0;
279 }
280
81c50963
ST
281 /*
282 * TODO rework base driver to use values directly.
283 * for now parse it back into reg-format
284 */
585d450c 285 dc_stream_get_scanoutpos(acrtc->dm_irq_params.stream,
81c50963
ST
286 &v_blank_start,
287 &v_blank_end,
288 &h_position,
289 &v_position);
290
e806208d
AG
291 *position = v_position | (h_position << 16);
292 *vbl = v_blank_start | (v_blank_end << 16);
4562236b
HW
293 }
294
295 return 0;
296}
297
298static bool dm_is_idle(void *handle)
299{
300 /* XXX todo */
301 return true;
302}
303
304static int dm_wait_for_idle(void *handle)
305{
306 /* XXX todo */
307 return 0;
308}
309
310static bool dm_check_soft_reset(void *handle)
311{
312 return false;
313}
314
315static int dm_soft_reset(void *handle)
316{
317 /* XXX todo */
318 return 0;
319}
320
3ee6b26b
AD
321static struct amdgpu_crtc *
322get_crtc_by_otg_inst(struct amdgpu_device *adev,
323 int otg_inst)
4562236b 324{
4a580877 325 struct drm_device *dev = adev_to_drm(adev);
4562236b
HW
326 struct drm_crtc *crtc;
327 struct amdgpu_crtc *amdgpu_crtc;
328
bcd74374 329 if (WARN_ON(otg_inst == -1))
4562236b 330 return adev->mode_info.crtcs[0];
4562236b
HW
331
332 list_for_each_entry(crtc, &dev->mode_config.crtc_list, head) {
333 amdgpu_crtc = to_amdgpu_crtc(crtc);
334
335 if (amdgpu_crtc->otg_inst == otg_inst)
336 return amdgpu_crtc;
337 }
338
339 return NULL;
340}
341
a85ba005
NC
342static inline bool is_dc_timing_adjust_needed(struct dm_crtc_state *old_state,
343 struct dm_crtc_state *new_state)
344{
345 if (new_state->freesync_config.state == VRR_STATE_ACTIVE_FIXED)
346 return true;
347 else if (amdgpu_dm_vrr_active(old_state) != amdgpu_dm_vrr_active(new_state))
348 return true;
349 else
350 return false;
351}
352
b8e8c934
HW
353/**
354 * dm_pflip_high_irq() - Handle pageflip interrupt
355 * @interrupt_params: ignored
356 *
357 * Handles the pageflip interrupt by notifying all interested parties
358 * that the pageflip has been completed.
359 */
4562236b
HW
360static void dm_pflip_high_irq(void *interrupt_params)
361{
4562236b
HW
362 struct amdgpu_crtc *amdgpu_crtc;
363 struct common_irq_params *irq_params = interrupt_params;
364 struct amdgpu_device *adev = irq_params->adev;
365 unsigned long flags;
71bbe51a 366 struct drm_pending_vblank_event *e;
ae67558b 367 u32 vpos, hpos, v_blank_start, v_blank_end;
71bbe51a 368 bool vrr_active;
4562236b
HW
369
370 amdgpu_crtc = get_crtc_by_otg_inst(adev, irq_params->irq_src - IRQ_TYPE_PFLIP);
371
372 /* IRQ could occur when in initial stage */
1f6010a9 373 /* TODO work and BO cleanup */
4562236b 374 if (amdgpu_crtc == NULL) {
cb2318b7 375 DC_LOG_PFLIP("CRTC is null, returning.\n");
4562236b
HW
376 return;
377 }
378
4a580877 379 spin_lock_irqsave(&adev_to_drm(adev)->event_lock, flags);
4562236b
HW
380
381 if (amdgpu_crtc->pflip_status != AMDGPU_FLIP_SUBMITTED){
cb2318b7 382 DC_LOG_PFLIP("amdgpu_crtc->pflip_status = %d !=AMDGPU_FLIP_SUBMITTED(%d) on crtc:%d[%p] \n",
4562236b
HW
383 amdgpu_crtc->pflip_status,
384 AMDGPU_FLIP_SUBMITTED,
385 amdgpu_crtc->crtc_id,
386 amdgpu_crtc);
4a580877 387 spin_unlock_irqrestore(&adev_to_drm(adev)->event_lock, flags);
4562236b
HW
388 return;
389 }
390
71bbe51a
MK
391 /* page flip completed. */
392 e = amdgpu_crtc->event;
393 amdgpu_crtc->event = NULL;
4562236b 394
bcd74374 395 WARN_ON(!e);
1159898a 396
585d450c 397 vrr_active = amdgpu_dm_vrr_active_irq(amdgpu_crtc);
71bbe51a
MK
398
399 /* Fixed refresh rate, or VRR scanout position outside front-porch? */
400 if (!vrr_active ||
585d450c 401 !dc_stream_get_scanoutpos(amdgpu_crtc->dm_irq_params.stream, &v_blank_start,
71bbe51a
MK
402 &v_blank_end, &hpos, &vpos) ||
403 (vpos < v_blank_start)) {
404 /* Update to correct count and vblank timestamp if racing with
405 * vblank irq. This also updates to the correct vblank timestamp
406 * even in VRR mode, as scanout is past the front-porch atm.
407 */
408 drm_crtc_accurate_vblank_count(&amdgpu_crtc->base);
1159898a 409
71bbe51a
MK
410 /* Wake up userspace by sending the pageflip event with proper
411 * count and timestamp of vblank of flip completion.
412 */
413 if (e) {
414 drm_crtc_send_vblank_event(&amdgpu_crtc->base, e);
415
416 /* Event sent, so done with vblank for this flip */
417 drm_crtc_vblank_put(&amdgpu_crtc->base);
418 }
419 } else if (e) {
420 /* VRR active and inside front-porch: vblank count and
421 * timestamp for pageflip event will only be up to date after
422 * drm_crtc_handle_vblank() has been executed from late vblank
423 * irq handler after start of back-porch (vline 0). We queue the
424 * pageflip event for send-out by drm_crtc_handle_vblank() with
425 * updated timestamp and count, once it runs after us.
426 *
427 * We need to open-code this instead of using the helper
428 * drm_crtc_arm_vblank_event(), as that helper would
429 * call drm_crtc_accurate_vblank_count(), which we must
430 * not call in VRR mode while we are in front-porch!
431 */
432
433 /* sequence will be replaced by real count during send-out. */
434 e->sequence = drm_crtc_vblank_count(&amdgpu_crtc->base);
435 e->pipe = amdgpu_crtc->crtc_id;
436
4a580877 437 list_add_tail(&e->base.link, &adev_to_drm(adev)->vblank_event_list);
71bbe51a
MK
438 e = NULL;
439 }
4562236b 440
fdd1fe57
MK
441 /* Keep track of vblank of this flip for flip throttling. We use the
442 * cooked hw counter, as that one incremented at start of this vblank
443 * of pageflip completion, so last_flip_vblank is the forbidden count
444 * for queueing new pageflips if vsync + VRR is enabled.
445 */
5d1c59c4 446 amdgpu_crtc->dm_irq_params.last_flip_vblank =
e3eff4b5 447 amdgpu_get_vblank_counter_kms(&amdgpu_crtc->base);
fdd1fe57 448
54f5499a 449 amdgpu_crtc->pflip_status = AMDGPU_FLIP_NONE;
4a580877 450 spin_unlock_irqrestore(&adev_to_drm(adev)->event_lock, flags);
4562236b 451
cb2318b7
VL
452 DC_LOG_PFLIP("crtc:%d[%p], pflip_stat:AMDGPU_FLIP_NONE, vrr[%d]-fp %d\n",
453 amdgpu_crtc->crtc_id, amdgpu_crtc,
454 vrr_active, (int) !e);
4562236b
HW
455}
456
d2574c33
MK
457static void dm_vupdate_high_irq(void *interrupt_params)
458{
459 struct common_irq_params *irq_params = interrupt_params;
460 struct amdgpu_device *adev = irq_params->adev;
461 struct amdgpu_crtc *acrtc;
47588233
RS
462 struct drm_device *drm_dev;
463 struct drm_vblank_crtc *vblank;
464 ktime_t frame_duration_ns, previous_timestamp;
09aef2c4 465 unsigned long flags;
585d450c 466 int vrr_active;
d2574c33
MK
467
468 acrtc = get_crtc_by_otg_inst(adev, irq_params->irq_src - IRQ_TYPE_VUPDATE);
469
470 if (acrtc) {
585d450c 471 vrr_active = amdgpu_dm_vrr_active_irq(acrtc);
47588233
RS
472 drm_dev = acrtc->base.dev;
473 vblank = &drm_dev->vblank[acrtc->base.index];
474 previous_timestamp = atomic64_read(&irq_params->previous_timestamp);
475 frame_duration_ns = vblank->time - previous_timestamp;
476
477 if (frame_duration_ns > 0) {
478 trace_amdgpu_refresh_rate_track(acrtc->base.index,
479 frame_duration_ns,
480 ktime_divns(NSEC_PER_SEC, frame_duration_ns));
481 atomic64_set(&irq_params->previous_timestamp, vblank->time);
482 }
d2574c33 483
cb2318b7 484 DC_LOG_VBLANK("crtc:%d, vupdate-vrr:%d\n",
7f2be468 485 acrtc->crtc_id,
585d450c 486 vrr_active);
d2574c33
MK
487
488 /* Core vblank handling is done here after end of front-porch in
489 * vrr mode, as vblank timestamping will give valid results
490 * while now done after front-porch. This will also deliver
491 * page-flip completion events that have been queued to us
492 * if a pageflip happened inside front-porch.
493 */
585d450c 494 if (vrr_active) {
cc79950b 495 dm_crtc_handle_vblank(acrtc);
09aef2c4
MK
496
497 /* BTR processing for pre-DCE12 ASICs */
585d450c 498 if (acrtc->dm_irq_params.stream &&
09aef2c4 499 adev->family < AMDGPU_FAMILY_AI) {
4a580877 500 spin_lock_irqsave(&adev_to_drm(adev)->event_lock, flags);
09aef2c4
MK
501 mod_freesync_handle_v_update(
502 adev->dm.freesync_module,
585d450c
AP
503 acrtc->dm_irq_params.stream,
504 &acrtc->dm_irq_params.vrr_params);
09aef2c4
MK
505
506 dc_stream_adjust_vmin_vmax(
507 adev->dm.dc,
585d450c
AP
508 acrtc->dm_irq_params.stream,
509 &acrtc->dm_irq_params.vrr_params.adjust);
4a580877 510 spin_unlock_irqrestore(&adev_to_drm(adev)->event_lock, flags);
09aef2c4
MK
511 }
512 }
d2574c33
MK
513 }
514}
515
b8e8c934
HW
516/**
517 * dm_crtc_high_irq() - Handles CRTC interrupt
2346ef47 518 * @interrupt_params: used for determining the CRTC instance
b8e8c934
HW
519 *
520 * Handles the CRTC/VSYNC interrupt by notfying DRM's VBLANK
521 * event handler.
522 */
4562236b
HW
523static void dm_crtc_high_irq(void *interrupt_params)
524{
525 struct common_irq_params *irq_params = interrupt_params;
526 struct amdgpu_device *adev = irq_params->adev;
4562236b 527 struct amdgpu_crtc *acrtc;
09aef2c4 528 unsigned long flags;
585d450c 529 int vrr_active;
4562236b 530
b57de80a 531 acrtc = get_crtc_by_otg_inst(adev, irq_params->irq_src - IRQ_TYPE_VBLANK);
16f17eda
LL
532 if (!acrtc)
533 return;
534
585d450c 535 vrr_active = amdgpu_dm_vrr_active_irq(acrtc);
16f17eda 536
cb2318b7 537 DC_LOG_VBLANK("crtc:%d, vupdate-vrr:%d, planes:%d\n", acrtc->crtc_id,
585d450c 538 vrr_active, acrtc->dm_irq_params.active_planes);
16f17eda 539
2346ef47
NK
540 /**
541 * Core vblank handling at start of front-porch is only possible
542 * in non-vrr mode, as only there vblank timestamping will give
543 * valid results while done in front-porch. Otherwise defer it
544 * to dm_vupdate_high_irq after end of front-porch.
545 */
585d450c 546 if (!vrr_active)
cc79950b 547 dm_crtc_handle_vblank(acrtc);
2346ef47
NK
548
549 /**
550 * Following stuff must happen at start of vblank, for crc
551 * computation and below-the-range btr support in vrr mode.
552 */
16f17eda 553 amdgpu_dm_crtc_handle_crc_irq(&acrtc->base);
2346ef47
NK
554
555 /* BTR updates need to happen before VUPDATE on Vega and above. */
556 if (adev->family < AMDGPU_FAMILY_AI)
557 return;
16f17eda 558
4a580877 559 spin_lock_irqsave(&adev_to_drm(adev)->event_lock, flags);
16f17eda 560
585d450c
AP
561 if (acrtc->dm_irq_params.stream &&
562 acrtc->dm_irq_params.vrr_params.supported &&
563 acrtc->dm_irq_params.freesync_config.state ==
564 VRR_STATE_ACTIVE_VARIABLE) {
2346ef47 565 mod_freesync_handle_v_update(adev->dm.freesync_module,
585d450c
AP
566 acrtc->dm_irq_params.stream,
567 &acrtc->dm_irq_params.vrr_params);
16f17eda 568
585d450c
AP
569 dc_stream_adjust_vmin_vmax(adev->dm.dc, acrtc->dm_irq_params.stream,
570 &acrtc->dm_irq_params.vrr_params.adjust);
16f17eda
LL
571 }
572
2b5aed9a
MK
573 /*
574 * If there aren't any active_planes then DCH HUBP may be clock-gated.
575 * In that case, pageflip completion interrupts won't fire and pageflip
576 * completion events won't get delivered. Prevent this by sending
577 * pending pageflip events from here if a flip is still pending.
578 *
579 * If any planes are enabled, use dm_pflip_high_irq() instead, to
580 * avoid race conditions between flip programming and completion,
581 * which could cause too early flip completion events.
582 */
2346ef47
NK
583 if (adev->family >= AMDGPU_FAMILY_RV &&
584 acrtc->pflip_status == AMDGPU_FLIP_SUBMITTED &&
585d450c 585 acrtc->dm_irq_params.active_planes == 0) {
16f17eda
LL
586 if (acrtc->event) {
587 drm_crtc_send_vblank_event(&acrtc->base, acrtc->event);
588 acrtc->event = NULL;
589 drm_crtc_vblank_put(&acrtc->base);
590 }
591 acrtc->pflip_status = AMDGPU_FLIP_NONE;
592 }
593
4a580877 594 spin_unlock_irqrestore(&adev_to_drm(adev)->event_lock, flags);
16f17eda
LL
595}
596
9e1178ef 597#if defined(CONFIG_DRM_AMD_SECURE_DISPLAY)
86bc2219
WL
598/**
599 * dm_dcn_vertical_interrupt0_high_irq() - Handles OTG Vertical interrupt0 for
600 * DCN generation ASICs
48e01bf4 601 * @interrupt_params: interrupt parameters
86bc2219
WL
602 *
603 * Used to set crc window/read out crc value at vertical line 0 position
604 */
86bc2219
WL
605static void dm_dcn_vertical_interrupt0_high_irq(void *interrupt_params)
606{
607 struct common_irq_params *irq_params = interrupt_params;
608 struct amdgpu_device *adev = irq_params->adev;
609 struct amdgpu_crtc *acrtc;
610
611 acrtc = get_crtc_by_otg_inst(adev, irq_params->irq_src - IRQ_TYPE_VLINE0);
612
613 if (!acrtc)
614 return;
615
616 amdgpu_dm_crtc_handle_crc_window_irq(&acrtc->base);
617}
433e5dec 618#endif /* CONFIG_DRM_AMD_SECURE_DISPLAY */
86bc2219 619
e27c41d5 620/**
03f2abb0 621 * dmub_aux_setconfig_callback - Callback for AUX or SET_CONFIG command.
e27c41d5
JS
622 * @adev: amdgpu_device pointer
623 * @notify: dmub notification structure
624 *
625 * Dmub AUX or SET_CONFIG command completion processing callback
626 * Copies dmub notification to DM which is to be read by AUX command.
627 * issuing thread and also signals the event to wake up the thread.
628 */
240e6d25
IB
629static void dmub_aux_setconfig_callback(struct amdgpu_device *adev,
630 struct dmub_notification *notify)
e27c41d5
JS
631{
632 if (adev->dm.dmub_notify)
633 memcpy(adev->dm.dmub_notify, notify, sizeof(struct dmub_notification));
634 if (notify->type == DMUB_NOTIFICATION_AUX_REPLY)
635 complete(&adev->dm.dmub_aux_transfer_done);
636}
637
638/**
639 * dmub_hpd_callback - DMUB HPD interrupt processing callback.
640 * @adev: amdgpu_device pointer
641 * @notify: dmub notification structure
642 *
643 * Dmub Hpd interrupt processing callback. Gets displayindex through the
644 * ink index and calls helper to do the processing.
645 */
240e6d25
IB
646static void dmub_hpd_callback(struct amdgpu_device *adev,
647 struct dmub_notification *notify)
e27c41d5
JS
648{
649 struct amdgpu_dm_connector *aconnector;
f6e03f80 650 struct amdgpu_dm_connector *hpd_aconnector = NULL;
e27c41d5
JS
651 struct drm_connector *connector;
652 struct drm_connector_list_iter iter;
653 struct dc_link *link;
ae67558b 654 u8 link_index = 0;
978ffac8 655 struct drm_device *dev;
e27c41d5
JS
656
657 if (adev == NULL)
658 return;
659
660 if (notify == NULL) {
661 DRM_ERROR("DMUB HPD callback notification was NULL");
662 return;
663 }
664
665 if (notify->link_index > adev->dm.dc->link_count) {
666 DRM_ERROR("DMUB HPD index (%u)is abnormal", notify->link_index);
667 return;
668 }
669
e27c41d5 670 link_index = notify->link_index;
e27c41d5 671 link = adev->dm.dc->links[link_index];
978ffac8 672 dev = adev->dm.ddev;
e27c41d5
JS
673
674 drm_connector_list_iter_begin(dev, &iter);
675 drm_for_each_connector_iter(connector, &iter) {
676 aconnector = to_amdgpu_dm_connector(connector);
677 if (link && aconnector->dc_link == link) {
678 DRM_INFO("DMUB HPD callback: link_index=%u\n", link_index);
f6e03f80 679 hpd_aconnector = aconnector;
e27c41d5
JS
680 break;
681 }
682 }
683 drm_connector_list_iter_end(&iter);
e27c41d5 684
c40a09e5
NK
685 if (hpd_aconnector) {
686 if (notify->type == DMUB_NOTIFICATION_HPD)
687 handle_hpd_irq_helper(hpd_aconnector);
688 else if (notify->type == DMUB_NOTIFICATION_HPD_IRQ)
689 handle_hpd_rx_irq(hpd_aconnector);
690 }
e27c41d5
JS
691}
692
693/**
694 * register_dmub_notify_callback - Sets callback for DMUB notify
695 * @adev: amdgpu_device pointer
696 * @type: Type of dmub notification
697 * @callback: Dmub interrupt callback function
698 * @dmub_int_thread_offload: offload indicator
699 *
700 * API to register a dmub callback handler for a dmub notification
701 * Also sets indicator whether callback processing to be offloaded.
702 * to dmub interrupt handling thread
703 * Return: true if successfully registered, false if there is existing registration
704 */
240e6d25
IB
705static bool register_dmub_notify_callback(struct amdgpu_device *adev,
706 enum dmub_notification_type type,
707 dmub_notify_interrupt_callback_t callback,
708 bool dmub_int_thread_offload)
e27c41d5
JS
709{
710 if (callback != NULL && type < ARRAY_SIZE(adev->dm.dmub_thread_offload)) {
711 adev->dm.dmub_callback[type] = callback;
712 adev->dm.dmub_thread_offload[type] = dmub_int_thread_offload;
713 } else
714 return false;
715
716 return true;
717}
718
719static void dm_handle_hpd_work(struct work_struct *work)
720{
721 struct dmub_hpd_work *dmub_hpd_wrk;
722
723 dmub_hpd_wrk = container_of(work, struct dmub_hpd_work, handle_hpd_work);
724
725 if (!dmub_hpd_wrk->dmub_notify) {
726 DRM_ERROR("dmub_hpd_wrk dmub_notify is NULL");
727 return;
728 }
729
730 if (dmub_hpd_wrk->dmub_notify->type < ARRAY_SIZE(dmub_hpd_wrk->adev->dm.dmub_callback)) {
731 dmub_hpd_wrk->adev->dm.dmub_callback[dmub_hpd_wrk->dmub_notify->type](dmub_hpd_wrk->adev,
732 dmub_hpd_wrk->dmub_notify);
733 }
094b21c1
JS
734
735 kfree(dmub_hpd_wrk->dmub_notify);
e27c41d5
JS
736 kfree(dmub_hpd_wrk);
737
738}
739
e25515e2 740#define DMUB_TRACE_MAX_READ 64
81927e28
JS
741/**
742 * dm_dmub_outbox1_low_irq() - Handles Outbox interrupt
743 * @interrupt_params: used for determining the Outbox instance
744 *
745 * Handles the Outbox Interrupt
746 * event handler.
747 */
81927e28
JS
748static void dm_dmub_outbox1_low_irq(void *interrupt_params)
749{
750 struct dmub_notification notify;
751 struct common_irq_params *irq_params = interrupt_params;
752 struct amdgpu_device *adev = irq_params->adev;
753 struct amdgpu_display_manager *dm = &adev->dm;
754 struct dmcub_trace_buf_entry entry = { 0 };
ae67558b 755 u32 count = 0;
e27c41d5 756 struct dmub_hpd_work *dmub_hpd_wrk;
f6e03f80 757 struct dc_link *plink = NULL;
81927e28 758
f6e03f80
JS
759 if (dc_enable_dmub_notifications(adev->dm.dc) &&
760 irq_params->irq_src == DC_IRQ_SOURCE_DMCUB_OUTBOX) {
e27c41d5 761
f6e03f80
JS
762 do {
763 dc_stat_get_dmub_notification(adev->dm.dc, &notify);
a35faec3 764 if (notify.type >= ARRAY_SIZE(dm->dmub_thread_offload)) {
f6e03f80
JS
765 DRM_ERROR("DM: notify type %d invalid!", notify.type);
766 continue;
767 }
c40a09e5
NK
768 if (!dm->dmub_callback[notify.type]) {
769 DRM_DEBUG_DRIVER("DMUB notification skipped, no handler: type=%d\n", notify.type);
770 continue;
771 }
f6e03f80 772 if (dm->dmub_thread_offload[notify.type] == true) {
094b21c1
JS
773 dmub_hpd_wrk = kzalloc(sizeof(*dmub_hpd_wrk), GFP_ATOMIC);
774 if (!dmub_hpd_wrk) {
775 DRM_ERROR("Failed to allocate dmub_hpd_wrk");
776 return;
777 }
778 dmub_hpd_wrk->dmub_notify = kzalloc(sizeof(struct dmub_notification), GFP_ATOMIC);
779 if (!dmub_hpd_wrk->dmub_notify) {
780 kfree(dmub_hpd_wrk);
781 DRM_ERROR("Failed to allocate dmub_hpd_wrk->dmub_notify");
782 return;
783 }
784 INIT_WORK(&dmub_hpd_wrk->handle_hpd_work, dm_handle_hpd_work);
785 if (dmub_hpd_wrk->dmub_notify)
786 memcpy(dmub_hpd_wrk->dmub_notify, &notify, sizeof(struct dmub_notification));
f6e03f80
JS
787 dmub_hpd_wrk->adev = adev;
788 if (notify.type == DMUB_NOTIFICATION_HPD) {
789 plink = adev->dm.dc->links[notify.link_index];
790 if (plink) {
791 plink->hpd_status =
b97788e5 792 notify.hpd_status == DP_HPD_PLUG;
f6e03f80 793 }
e27c41d5 794 }
f6e03f80
JS
795 queue_work(adev->dm.delayed_hpd_wq, &dmub_hpd_wrk->handle_hpd_work);
796 } else {
797 dm->dmub_callback[notify.type](adev, &notify);
798 }
799 } while (notify.pending_notification);
81927e28
JS
800 }
801
802
803 do {
804 if (dc_dmub_srv_get_dmub_outbox0_msg(dm->dc, &entry)) {
805 trace_amdgpu_dmub_trace_high_irq(entry.trace_code, entry.tick_count,
806 entry.param0, entry.param1);
807
808 DRM_DEBUG_DRIVER("trace_code:%u, tick_count:%u, param0:%u, param1:%u\n",
809 entry.trace_code, entry.tick_count, entry.param0, entry.param1);
810 } else
811 break;
812
813 count++;
814
815 } while (count <= DMUB_TRACE_MAX_READ);
816
f6e03f80
JS
817 if (count > DMUB_TRACE_MAX_READ)
818 DRM_DEBUG_DRIVER("Warning : count > DMUB_TRACE_MAX_READ");
81927e28 819}
86bc2219 820
4562236b
HW
821static int dm_set_clockgating_state(void *handle,
822 enum amd_clockgating_state state)
823{
824 return 0;
825}
826
827static int dm_set_powergating_state(void *handle,
828 enum amd_powergating_state state)
829{
830 return 0;
831}
832
833/* Prototypes of private functions */
834static int dm_early_init(void* handle);
835
a32e24b4 836/* Allocate memory for FBC compressed data */
3e332d3a 837static void amdgpu_dm_fbc_init(struct drm_connector *connector)
a32e24b4 838{
3e332d3a 839 struct drm_device *dev = connector->dev;
1348969a 840 struct amdgpu_device *adev = drm_to_adev(dev);
4d154b85 841 struct dm_compressor_info *compressor = &adev->dm.compressor;
3e332d3a
RL
842 struct amdgpu_dm_connector *aconn = to_amdgpu_dm_connector(connector);
843 struct drm_display_mode *mode;
42e67c3b
RL
844 unsigned long max_size = 0;
845
846 if (adev->dm.dc->fbc_compressor == NULL)
847 return;
a32e24b4 848
3e332d3a 849 if (aconn->dc_link->connector_signal != SIGNAL_TYPE_EDP)
42e67c3b
RL
850 return;
851
3e332d3a
RL
852 if (compressor->bo_ptr)
853 return;
42e67c3b 854
42e67c3b 855
3e332d3a
RL
856 list_for_each_entry(mode, &connector->modes, head) {
857 if (max_size < mode->htotal * mode->vtotal)
858 max_size = mode->htotal * mode->vtotal;
42e67c3b
RL
859 }
860
861 if (max_size) {
862 int r = amdgpu_bo_create_kernel(adev, max_size * 4, PAGE_SIZE,
0e5916ff 863 AMDGPU_GEM_DOMAIN_GTT, &compressor->bo_ptr,
42e67c3b 864 &compressor->gpu_addr, &compressor->cpu_addr);
a32e24b4
RL
865
866 if (r)
42e67c3b
RL
867 DRM_ERROR("DM: Failed to initialize FBC\n");
868 else {
869 adev->dm.dc->ctx->fbc_gpu_addr = compressor->gpu_addr;
870 DRM_INFO("DM: FBC alloc %lu\n", max_size*4);
871 }
872
a32e24b4
RL
873 }
874
875}
a32e24b4 876
6ce8f316
NK
877static int amdgpu_dm_audio_component_get_eld(struct device *kdev, int port,
878 int pipe, bool *enabled,
879 unsigned char *buf, int max_bytes)
880{
881 struct drm_device *dev = dev_get_drvdata(kdev);
1348969a 882 struct amdgpu_device *adev = drm_to_adev(dev);
6ce8f316
NK
883 struct drm_connector *connector;
884 struct drm_connector_list_iter conn_iter;
885 struct amdgpu_dm_connector *aconnector;
886 int ret = 0;
887
888 *enabled = false;
889
890 mutex_lock(&adev->dm.audio_lock);
891
892 drm_connector_list_iter_begin(dev, &conn_iter);
893 drm_for_each_connector_iter(connector, &conn_iter) {
894 aconnector = to_amdgpu_dm_connector(connector);
895 if (aconnector->audio_inst != port)
896 continue;
897
898 *enabled = true;
899 ret = drm_eld_size(connector->eld);
900 memcpy(buf, connector->eld, min(max_bytes, ret));
901
902 break;
903 }
904 drm_connector_list_iter_end(&conn_iter);
905
906 mutex_unlock(&adev->dm.audio_lock);
907
908 DRM_DEBUG_KMS("Get ELD : idx=%d ret=%d en=%d\n", port, ret, *enabled);
909
910 return ret;
911}
912
913static const struct drm_audio_component_ops amdgpu_dm_audio_component_ops = {
914 .get_eld = amdgpu_dm_audio_component_get_eld,
915};
916
917static int amdgpu_dm_audio_component_bind(struct device *kdev,
918 struct device *hda_kdev, void *data)
919{
920 struct drm_device *dev = dev_get_drvdata(kdev);
1348969a 921 struct amdgpu_device *adev = drm_to_adev(dev);
6ce8f316
NK
922 struct drm_audio_component *acomp = data;
923
924 acomp->ops = &amdgpu_dm_audio_component_ops;
925 acomp->dev = kdev;
926 adev->dm.audio_component = acomp;
927
928 return 0;
929}
930
931static void amdgpu_dm_audio_component_unbind(struct device *kdev,
932 struct device *hda_kdev, void *data)
933{
934 struct drm_device *dev = dev_get_drvdata(kdev);
1348969a 935 struct amdgpu_device *adev = drm_to_adev(dev);
6ce8f316
NK
936 struct drm_audio_component *acomp = data;
937
938 acomp->ops = NULL;
939 acomp->dev = NULL;
940 adev->dm.audio_component = NULL;
941}
942
943static const struct component_ops amdgpu_dm_audio_component_bind_ops = {
944 .bind = amdgpu_dm_audio_component_bind,
945 .unbind = amdgpu_dm_audio_component_unbind,
946};
947
948static int amdgpu_dm_audio_init(struct amdgpu_device *adev)
949{
950 int i, ret;
951
952 if (!amdgpu_audio)
953 return 0;
954
955 adev->mode_info.audio.enabled = true;
956
957 adev->mode_info.audio.num_pins = adev->dm.dc->res_pool->audio_count;
958
959 for (i = 0; i < adev->mode_info.audio.num_pins; i++) {
960 adev->mode_info.audio.pin[i].channels = -1;
961 adev->mode_info.audio.pin[i].rate = -1;
962 adev->mode_info.audio.pin[i].bits_per_sample = -1;
963 adev->mode_info.audio.pin[i].status_bits = 0;
964 adev->mode_info.audio.pin[i].category_code = 0;
965 adev->mode_info.audio.pin[i].connected = false;
966 adev->mode_info.audio.pin[i].id =
967 adev->dm.dc->res_pool->audios[i]->inst;
968 adev->mode_info.audio.pin[i].offset = 0;
969 }
970
971 ret = component_add(adev->dev, &amdgpu_dm_audio_component_bind_ops);
972 if (ret < 0)
973 return ret;
974
975 adev->dm.audio_registered = true;
976
977 return 0;
978}
979
980static void amdgpu_dm_audio_fini(struct amdgpu_device *adev)
981{
982 if (!amdgpu_audio)
983 return;
984
985 if (!adev->mode_info.audio.enabled)
986 return;
987
988 if (adev->dm.audio_registered) {
989 component_del(adev->dev, &amdgpu_dm_audio_component_bind_ops);
990 adev->dm.audio_registered = false;
991 }
992
993 /* TODO: Disable audio? */
994
995 adev->mode_info.audio.enabled = false;
996}
997
dfd84d90 998static void amdgpu_dm_audio_eld_notify(struct amdgpu_device *adev, int pin)
6ce8f316
NK
999{
1000 struct drm_audio_component *acomp = adev->dm.audio_component;
1001
1002 if (acomp && acomp->audio_ops && acomp->audio_ops->pin_eld_notify) {
1003 DRM_DEBUG_KMS("Notify ELD: %d\n", pin);
1004
1005 acomp->audio_ops->pin_eld_notify(acomp->audio_ops->audio_ptr,
1006 pin, -1);
1007 }
1008}
1009
743b9786
NK
1010static int dm_dmub_hw_init(struct amdgpu_device *adev)
1011{
743b9786
NK
1012 const struct dmcub_firmware_header_v1_0 *hdr;
1013 struct dmub_srv *dmub_srv = adev->dm.dmub_srv;
8c7aea40 1014 struct dmub_srv_fb_info *fb_info = adev->dm.dmub_fb_info;
743b9786
NK
1015 const struct firmware *dmub_fw = adev->dm.dmub_fw;
1016 struct dmcu *dmcu = adev->dm.dc->res_pool->dmcu;
1017 struct abm *abm = adev->dm.dc->res_pool->abm;
743b9786
NK
1018 struct dmub_srv_hw_params hw_params;
1019 enum dmub_status status;
1020 const unsigned char *fw_inst_const, *fw_bss_data;
ae67558b 1021 u32 i, fw_inst_const_size, fw_bss_data_size;
743b9786
NK
1022 bool has_hw_support;
1023
1024 if (!dmub_srv)
1025 /* DMUB isn't supported on the ASIC. */
1026 return 0;
1027
8c7aea40
NK
1028 if (!fb_info) {
1029 DRM_ERROR("No framebuffer info for DMUB service.\n");
1030 return -EINVAL;
1031 }
1032
743b9786
NK
1033 if (!dmub_fw) {
1034 /* Firmware required for DMUB support. */
1035 DRM_ERROR("No firmware provided for DMUB.\n");
1036 return -EINVAL;
1037 }
1038
1039 status = dmub_srv_has_hw_support(dmub_srv, &has_hw_support);
1040 if (status != DMUB_STATUS_OK) {
1041 DRM_ERROR("Error checking HW support for DMUB: %d\n", status);
1042 return -EINVAL;
1043 }
1044
1045 if (!has_hw_support) {
1046 DRM_INFO("DMUB unsupported on ASIC\n");
1047 return 0;
1048 }
1049
47e62dbd
NK
1050 /* Reset DMCUB if it was previously running - before we overwrite its memory. */
1051 status = dmub_srv_hw_reset(dmub_srv);
1052 if (status != DMUB_STATUS_OK)
1053 DRM_WARN("Error resetting DMUB HW: %d\n", status);
1054
743b9786
NK
1055 hdr = (const struct dmcub_firmware_header_v1_0 *)dmub_fw->data;
1056
743b9786
NK
1057 fw_inst_const = dmub_fw->data +
1058 le32_to_cpu(hdr->header.ucode_array_offset_bytes) +
8c7aea40 1059 PSP_HEADER_BYTES;
743b9786
NK
1060
1061 fw_bss_data = dmub_fw->data +
1062 le32_to_cpu(hdr->header.ucode_array_offset_bytes) +
1063 le32_to_cpu(hdr->inst_const_bytes);
1064
1065 /* Copy firmware and bios info into FB memory. */
8c7aea40
NK
1066 fw_inst_const_size = le32_to_cpu(hdr->inst_const_bytes) -
1067 PSP_HEADER_BYTES - PSP_FOOTER_BYTES;
1068
1069 fw_bss_data_size = le32_to_cpu(hdr->bss_data_bytes);
1070
ddde28a5
HW
1071 /* if adev->firmware.load_type == AMDGPU_FW_LOAD_PSP,
1072 * amdgpu_ucode_init_single_fw will load dmub firmware
1073 * fw_inst_const part to cw0; otherwise, the firmware back door load
1074 * will be done by dm_dmub_hw_init
1075 */
1076 if (adev->firmware.load_type != AMDGPU_FW_LOAD_PSP) {
1077 memcpy(fb_info->fb[DMUB_WINDOW_0_INST_CONST].cpu_addr, fw_inst_const,
1078 fw_inst_const_size);
1079 }
1080
a576b345
NK
1081 if (fw_bss_data_size)
1082 memcpy(fb_info->fb[DMUB_WINDOW_2_BSS_DATA].cpu_addr,
1083 fw_bss_data, fw_bss_data_size);
ddde28a5
HW
1084
1085 /* Copy firmware bios info into FB memory. */
8c7aea40
NK
1086 memcpy(fb_info->fb[DMUB_WINDOW_3_VBIOS].cpu_addr, adev->bios,
1087 adev->bios_size);
1088
1089 /* Reset regions that need to be reset. */
1090 memset(fb_info->fb[DMUB_WINDOW_4_MAILBOX].cpu_addr, 0,
1091 fb_info->fb[DMUB_WINDOW_4_MAILBOX].size);
1092
1093 memset(fb_info->fb[DMUB_WINDOW_5_TRACEBUFF].cpu_addr, 0,
1094 fb_info->fb[DMUB_WINDOW_5_TRACEBUFF].size);
1095
1096 memset(fb_info->fb[DMUB_WINDOW_6_FW_STATE].cpu_addr, 0,
1097 fb_info->fb[DMUB_WINDOW_6_FW_STATE].size);
743b9786
NK
1098
1099 /* Initialize hardware. */
1100 memset(&hw_params, 0, sizeof(hw_params));
1101 hw_params.fb_base = adev->gmc.fb_start;
949933b0 1102 hw_params.fb_offset = adev->vm_manager.vram_base_offset;
743b9786 1103
31a7f4bb
HW
1104 /* backdoor load firmware and trigger dmub running */
1105 if (adev->firmware.load_type != AMDGPU_FW_LOAD_PSP)
1106 hw_params.load_inst_const = true;
1107
743b9786
NK
1108 if (dmcu)
1109 hw_params.psp_version = dmcu->psp_version;
1110
8c7aea40
NK
1111 for (i = 0; i < fb_info->num_fb; ++i)
1112 hw_params.fb[i] = &fb_info->fb[i];
743b9786 1113
3b36f50d 1114 switch (adev->ip_versions[DCE_HWIP][0]) {
f6aa84b8
RL
1115 case IP_VERSION(3, 1, 3):
1116 case IP_VERSION(3, 1, 4):
3b36f50d 1117 hw_params.dpia_supported = true;
7367540b 1118 hw_params.disable_dpia = adev->dm.dc->debug.dpia_debug.bits.disable_dpia;
5b109397
JS
1119 break;
1120 default:
1121 break;
1122 }
1123
743b9786
NK
1124 status = dmub_srv_hw_init(dmub_srv, &hw_params);
1125 if (status != DMUB_STATUS_OK) {
1126 DRM_ERROR("Error initializing DMUB HW: %d\n", status);
1127 return -EINVAL;
1128 }
1129
1130 /* Wait for firmware load to finish. */
1131 status = dmub_srv_wait_for_auto_load(dmub_srv, 100000);
1132 if (status != DMUB_STATUS_OK)
1133 DRM_WARN("Wait for DMUB auto-load failed: %d\n", status);
1134
1135 /* Init DMCU and ABM if available. */
1136 if (dmcu && abm) {
1137 dmcu->funcs->dmcu_init(dmcu);
1138 abm->dmcu_is_running = dmcu->funcs->is_dmcu_initialized(dmcu);
1139 }
1140
051b7887
RL
1141 if (!adev->dm.dc->ctx->dmub_srv)
1142 adev->dm.dc->ctx->dmub_srv = dc_dmub_srv_create(adev->dm.dc, dmub_srv);
9a71c7d3
NK
1143 if (!adev->dm.dc->ctx->dmub_srv) {
1144 DRM_ERROR("Couldn't allocate DC DMUB server!\n");
1145 return -ENOMEM;
1146 }
1147
743b9786
NK
1148 DRM_INFO("DMUB hardware initialized: version=0x%08X\n",
1149 adev->dm.dmcub_fw_version);
1150
1151 return 0;
1152}
1153
79d6b935
NK
1154static void dm_dmub_hw_resume(struct amdgpu_device *adev)
1155{
1156 struct dmub_srv *dmub_srv = adev->dm.dmub_srv;
1157 enum dmub_status status;
1158 bool init;
1159
1160 if (!dmub_srv) {
1161 /* DMUB isn't supported on the ASIC. */
1162 return;
1163 }
1164
1165 status = dmub_srv_is_hw_init(dmub_srv, &init);
1166 if (status != DMUB_STATUS_OK)
1167 DRM_WARN("DMUB hardware init check failed: %d\n", status);
1168
1169 if (status == DMUB_STATUS_OK && init) {
1170 /* Wait for firmware load to finish. */
1171 status = dmub_srv_wait_for_auto_load(dmub_srv, 100000);
1172 if (status != DMUB_STATUS_OK)
1173 DRM_WARN("Wait for DMUB auto-load failed: %d\n", status);
1174 } else {
1175 /* Perform the full hardware initialization. */
1176 dm_dmub_hw_init(adev);
1177 }
1178}
1179
c0fb85ae 1180static void mmhub_read_system_context(struct amdgpu_device *adev, struct dc_phy_addr_space_config *pa_config)
c44a22b3 1181{
ae67558b
SS
1182 u64 pt_base;
1183 u32 logical_addr_low;
1184 u32 logical_addr_high;
1185 u32 agp_base, agp_bot, agp_top;
c0fb85ae 1186 PHYSICAL_ADDRESS_LOC page_table_start, page_table_end, page_table_base;
c44a22b3 1187
a0f884f5
NK
1188 memset(pa_config, 0, sizeof(*pa_config));
1189
c0fb85ae
YZ
1190 agp_base = 0;
1191 agp_bot = adev->gmc.agp_start >> 24;
1192 agp_top = adev->gmc.agp_end >> 24;
c44a22b3 1193
0294868f
AD
1194 /* AGP aperture is disabled */
1195 if (agp_bot == agp_top) {
4d2c6e89 1196 logical_addr_low = adev->gmc.fb_start >> 18;
0294868f
AD
1197 if (adev->apu_flags & AMD_APU_IS_RAVEN2)
1198 /*
1199 * Raven2 has a HW issue that it is unable to use the vram which
1200 * is out of MC_VM_SYSTEM_APERTURE_HIGH_ADDR. So here is the
1201 * workaround that increase system aperture high address (add 1)
1202 * to get rid of the VM fault and hardware hang.
1203 */
1204 logical_addr_high = (adev->gmc.fb_end >> 18) + 0x1;
1205 else
4d2c6e89 1206 logical_addr_high = adev->gmc.fb_end >> 18;
0294868f 1207 } else {
4d2c6e89 1208 logical_addr_low = min(adev->gmc.fb_start, adev->gmc.agp_start) >> 18;
0294868f
AD
1209 if (adev->apu_flags & AMD_APU_IS_RAVEN2)
1210 /*
1211 * Raven2 has a HW issue that it is unable to use the vram which
1212 * is out of MC_VM_SYSTEM_APERTURE_HIGH_ADDR. So here is the
1213 * workaround that increase system aperture high address (add 1)
1214 * to get rid of the VM fault and hardware hang.
1215 */
1216 logical_addr_high = max((adev->gmc.fb_end >> 18) + 0x1, adev->gmc.agp_end >> 18);
1217 else
1218 logical_addr_high = max(adev->gmc.fb_end, adev->gmc.agp_end) >> 18;
1219 }
1220
1221 pt_base = amdgpu_gmc_pd_addr(adev->gart.bo);
c44a22b3 1222
c0fb85ae
YZ
1223 page_table_start.high_part = (u32)(adev->gmc.gart_start >> 44) & 0xF;
1224 page_table_start.low_part = (u32)(adev->gmc.gart_start >> 12);
1225 page_table_end.high_part = (u32)(adev->gmc.gart_end >> 44) & 0xF;
1226 page_table_end.low_part = (u32)(adev->gmc.gart_end >> 12);
1227 page_table_base.high_part = upper_32_bits(pt_base) & 0xF;
1228 page_table_base.low_part = lower_32_bits(pt_base);
c44a22b3 1229
c0fb85ae
YZ
1230 pa_config->system_aperture.start_addr = (uint64_t)logical_addr_low << 18;
1231 pa_config->system_aperture.end_addr = (uint64_t)logical_addr_high << 18;
1232
1233 pa_config->system_aperture.agp_base = (uint64_t)agp_base << 24 ;
1234 pa_config->system_aperture.agp_bot = (uint64_t)agp_bot << 24;
1235 pa_config->system_aperture.agp_top = (uint64_t)agp_top << 24;
1236
1237 pa_config->system_aperture.fb_base = adev->gmc.fb_start;
949933b0 1238 pa_config->system_aperture.fb_offset = adev->vm_manager.vram_base_offset;
c0fb85ae
YZ
1239 pa_config->system_aperture.fb_top = adev->gmc.fb_end;
1240
1241 pa_config->gart_config.page_table_start_addr = page_table_start.quad_part << 12;
1242 pa_config->gart_config.page_table_end_addr = page_table_end.quad_part << 12;
1243 pa_config->gart_config.page_table_base_addr = page_table_base.quad_part;
1244
40e9f3f0 1245 pa_config->is_hvm_enabled = adev->mode_info.gpu_vm_support;
c44a22b3 1246
c44a22b3 1247}
cae5c1ab 1248
028c4ccf
QZ
1249static void force_connector_state(
1250 struct amdgpu_dm_connector *aconnector,
1251 enum drm_connector_force force_state)
1252{
1253 struct drm_connector *connector = &aconnector->base;
1254
1255 mutex_lock(&connector->dev->mode_config.mutex);
1256 aconnector->base.force = force_state;
1257 mutex_unlock(&connector->dev->mode_config.mutex);
1258
1259 mutex_lock(&aconnector->hpd_lock);
1260 drm_kms_helper_connector_hotplug_event(connector);
1261 mutex_unlock(&aconnector->hpd_lock);
1262}
1263
8e794421
WL
1264static void dm_handle_hpd_rx_offload_work(struct work_struct *work)
1265{
1266 struct hpd_rx_irq_offload_work *offload_work;
1267 struct amdgpu_dm_connector *aconnector;
1268 struct dc_link *dc_link;
1269 struct amdgpu_device *adev;
1270 enum dc_connection_type new_connection_type = dc_connection_none;
1271 unsigned long flags;
028c4ccf
QZ
1272 union test_response test_response;
1273
1274 memset(&test_response, 0, sizeof(test_response));
8e794421
WL
1275
1276 offload_work = container_of(work, struct hpd_rx_irq_offload_work, work);
1277 aconnector = offload_work->offload_wq->aconnector;
1278
1279 if (!aconnector) {
1280 DRM_ERROR("Can't retrieve aconnector in hpd_rx_irq_offload_work");
1281 goto skip;
1282 }
1283
1284 adev = drm_to_adev(aconnector->base.dev);
1285 dc_link = aconnector->dc_link;
1286
1287 mutex_lock(&aconnector->hpd_lock);
54618888 1288 if (!dc_link_detect_connection_type(dc_link, &new_connection_type))
8e794421
WL
1289 DRM_ERROR("KMS: Failed to detect connector\n");
1290 mutex_unlock(&aconnector->hpd_lock);
1291
1292 if (new_connection_type == dc_connection_none)
1293 goto skip;
1294
1295 if (amdgpu_in_reset(adev))
1296 goto skip;
1297
1298 mutex_lock(&adev->dm.dc_lock);
028c4ccf 1299 if (offload_work->data.bytes.device_service_irq.bits.AUTOMATED_TEST) {
8e794421 1300 dc_link_dp_handle_automated_test(dc_link);
028c4ccf
QZ
1301
1302 if (aconnector->timing_changed) {
1303 /* force connector disconnect and reconnect */
1304 force_connector_state(aconnector, DRM_FORCE_OFF);
1305 msleep(100);
1306 force_connector_state(aconnector, DRM_FORCE_UNSPECIFIED);
1307 }
1308
1309 test_response.bits.ACK = 1;
1310
1311 core_link_write_dpcd(
1312 dc_link,
1313 DP_TEST_RESPONSE,
1314 &test_response.raw,
1315 sizeof(test_response));
1316 }
8e794421 1317 else if ((dc_link->connector_signal != SIGNAL_TYPE_EDP) &&
c5a31f17 1318 dc_link_check_link_loss_status(dc_link, &offload_work->data) &&
8e794421 1319 dc_link_dp_allow_hpd_rx_irq(dc_link)) {
e322843e
HW
1320 /* offload_work->data is from handle_hpd_rx_irq->
1321 * schedule_hpd_rx_offload_work.this is defer handle
1322 * for hpd short pulse. upon here, link status may be
1323 * changed, need get latest link status from dpcd
1324 * registers. if link status is good, skip run link
1325 * training again.
1326 */
1327 union hpd_irq_data irq_data;
1328
1329 memset(&irq_data, 0, sizeof(irq_data));
1330
1331 /* before dc_link_dp_handle_link_loss, allow new link lost handle
1332 * request be added to work queue if link lost at end of dc_link_
1333 * dp_handle_link_loss
1334 */
8e794421
WL
1335 spin_lock_irqsave(&offload_work->offload_wq->offload_lock, flags);
1336 offload_work->offload_wq->is_handling_link_loss = false;
1337 spin_unlock_irqrestore(&offload_work->offload_wq->offload_lock, flags);
e322843e 1338
54618888 1339 if ((dc_link_dp_read_hpd_rx_irq_data(dc_link, &irq_data) == DC_OK) &&
e322843e
HW
1340 dc_link_check_link_loss_status(dc_link, &irq_data))
1341 dc_link_dp_handle_link_loss(dc_link);
8e794421
WL
1342 }
1343 mutex_unlock(&adev->dm.dc_lock);
1344
1345skip:
1346 kfree(offload_work);
1347
1348}
1349
1350static struct hpd_rx_irq_offload_work_queue *hpd_rx_irq_create_workqueue(struct dc *dc)
1351{
1352 int max_caps = dc->caps.max_links;
1353 int i = 0;
1354 struct hpd_rx_irq_offload_work_queue *hpd_rx_offload_wq = NULL;
1355
1356 hpd_rx_offload_wq = kcalloc(max_caps, sizeof(*hpd_rx_offload_wq), GFP_KERNEL);
1357
1358 if (!hpd_rx_offload_wq)
1359 return NULL;
1360
1361
1362 for (i = 0; i < max_caps; i++) {
1363 hpd_rx_offload_wq[i].wq =
1364 create_singlethread_workqueue("amdgpu_dm_hpd_rx_offload_wq");
1365
1366 if (hpd_rx_offload_wq[i].wq == NULL) {
1367 DRM_ERROR("create amdgpu_dm_hpd_rx_offload_wq fail!");
7136f956 1368 goto out_err;
8e794421
WL
1369 }
1370
1371 spin_lock_init(&hpd_rx_offload_wq[i].offload_lock);
1372 }
1373
1374 return hpd_rx_offload_wq;
7136f956
RM
1375
1376out_err:
1377 for (i = 0; i < max_caps; i++) {
1378 if (hpd_rx_offload_wq[i].wq)
1379 destroy_workqueue(hpd_rx_offload_wq[i].wq);
1380 }
1381 kfree(hpd_rx_offload_wq);
1382 return NULL;
8e794421
WL
1383}
1384
3ce51649
AD
1385struct amdgpu_stutter_quirk {
1386 u16 chip_vendor;
1387 u16 chip_device;
1388 u16 subsys_vendor;
1389 u16 subsys_device;
1390 u8 revision;
1391};
1392
1393static const struct amdgpu_stutter_quirk amdgpu_stutter_quirk_list[] = {
1394 /* https://bugzilla.kernel.org/show_bug.cgi?id=214417 */
1395 { 0x1002, 0x15dd, 0x1002, 0x15dd, 0xc8 },
1396 { 0, 0, 0, 0, 0 },
1397};
1398
1399static bool dm_should_disable_stutter(struct pci_dev *pdev)
1400{
1401 const struct amdgpu_stutter_quirk *p = amdgpu_stutter_quirk_list;
1402
1403 while (p && p->chip_device != 0) {
1404 if (pdev->vendor == p->chip_vendor &&
1405 pdev->device == p->chip_device &&
1406 pdev->subsystem_vendor == p->subsys_vendor &&
1407 pdev->subsystem_device == p->subsys_device &&
1408 pdev->revision == p->revision) {
1409 return true;
1410 }
1411 ++p;
1412 }
1413 return false;
1414}
1415
57b9f338
FZ
1416static const struct dmi_system_id hpd_disconnect_quirk_table[] = {
1417 {
1418 .matches = {
1419 DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
1420 DMI_MATCH(DMI_PRODUCT_NAME, "Precision 3660"),
1421 },
1422 },
1423 {
1424 .matches = {
1425 DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
1426 DMI_MATCH(DMI_PRODUCT_NAME, "Precision 3260"),
1427 },
1428 },
1429 {
1430 .matches = {
1431 DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
1432 DMI_MATCH(DMI_PRODUCT_NAME, "Precision 3460"),
1433 },
1434 },
503dc81c
TL
1435 {
1436 .matches = {
1437 DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
1438 DMI_MATCH(DMI_PRODUCT_NAME, "OptiPlex Tower Plus 7010"),
1439 },
1440 },
1441 {
1442 .matches = {
1443 DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
1444 DMI_MATCH(DMI_PRODUCT_NAME, "OptiPlex Tower 7010"),
1445 },
1446 },
1447 {
1448 .matches = {
1449 DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
1450 DMI_MATCH(DMI_PRODUCT_NAME, "OptiPlex SFF Plus 7010"),
1451 },
1452 },
1453 {
1454 .matches = {
1455 DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
1456 DMI_MATCH(DMI_PRODUCT_NAME, "OptiPlex SFF 7010"),
1457 },
1458 },
1459 {
1460 .matches = {
1461 DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
1462 DMI_MATCH(DMI_PRODUCT_NAME, "OptiPlex Micro Plus 7010"),
1463 },
1464 },
1465 {
1466 .matches = {
1467 DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
1468 DMI_MATCH(DMI_PRODUCT_NAME, "OptiPlex Micro 7010"),
1469 },
1470 },
57b9f338 1471 {}
503dc81c 1472 /* TODO: refactor this from a fixed table to a dynamic option */
57b9f338
FZ
1473};
1474
1475static void retrieve_dmi_info(struct amdgpu_display_manager *dm)
1476{
1477 const struct dmi_system_id *dmi_id;
1478
1479 dm->aux_hpd_discon_quirk = false;
1480
1481 dmi_id = dmi_first_match(hpd_disconnect_quirk_table);
1482 if (dmi_id) {
1483 dm->aux_hpd_discon_quirk = true;
1484 DRM_INFO("aux_hpd_discon_quirk attached\n");
1485 }
1486}
1487
7578ecda 1488static int amdgpu_dm_init(struct amdgpu_device *adev)
4562236b
HW
1489{
1490 struct dc_init_data init_data;
52704fca
BL
1491#ifdef CONFIG_DRM_AMD_DC_HDCP
1492 struct dc_callback_init init_params;
1493#endif
743b9786 1494 int r;
52704fca 1495
4a580877 1496 adev->dm.ddev = adev_to_drm(adev);
4562236b
HW
1497 adev->dm.adev = adev;
1498
4562236b
HW
1499 /* Zero all the fields */
1500 memset(&init_data, 0, sizeof(init_data));
52704fca
BL
1501#ifdef CONFIG_DRM_AMD_DC_HDCP
1502 memset(&init_params, 0, sizeof(init_params));
1503#endif
4562236b 1504
ead08b95 1505 mutex_init(&adev->dm.dpia_aux_lock);
674e78ac 1506 mutex_init(&adev->dm.dc_lock);
6ce8f316 1507 mutex_init(&adev->dm.audio_lock);
674e78ac 1508
4562236b
HW
1509 if(amdgpu_dm_irq_init(adev)) {
1510 DRM_ERROR("amdgpu: failed to initialize DM IRQ support.\n");
1511 goto error;
1512 }
1513
1514 init_data.asic_id.chip_family = adev->family;
1515
2dc31ca1 1516 init_data.asic_id.pci_revision_id = adev->pdev->revision;
4562236b 1517 init_data.asic_id.hw_internal_rev = adev->external_rev_id;
dae66a04 1518 init_data.asic_id.chip_id = adev->pdev->device;
4562236b 1519
770d13b1 1520 init_data.asic_id.vram_width = adev->gmc.vram_width;
4562236b
HW
1521 /* TODO: initialize init_data.asic_id.vram_type here!!!! */
1522 init_data.asic_id.atombios_base_address =
1523 adev->mode_info.atom_context->bios;
1524
1525 init_data.driver = adev;
1526
1527 adev->dm.cgs_device = amdgpu_cgs_create_device(adev);
1528
1529 if (!adev->dm.cgs_device) {
1530 DRM_ERROR("amdgpu: failed to create cgs device.\n");
1531 goto error;
1532 }
1533
1534 init_data.cgs_device = adev->dm.cgs_device;
1535
4562236b
HW
1536 init_data.dce_environment = DCE_ENV_PRODUCTION_DRV;
1537
fd546bc5
AD
1538 switch (adev->ip_versions[DCE_HWIP][0]) {
1539 case IP_VERSION(2, 1, 0):
1540 switch (adev->dm.dmcub_fw_version) {
1541 case 0: /* development */
1542 case 0x1: /* linux-firmware.git hash 6d9f399 */
1543 case 0x01000000: /* linux-firmware.git hash 9a0b0f4 */
1544 init_data.flags.disable_dmcu = false;
1545 break;
1546 default:
1547 init_data.flags.disable_dmcu = true;
1548 }
1549 break;
1550 case IP_VERSION(2, 0, 3):
1551 init_data.flags.disable_dmcu = true;
1552 break;
1553 default:
1554 break;
1555 }
1556
60fb100b
AD
1557 switch (adev->asic_type) {
1558 case CHIP_CARRIZO:
1559 case CHIP_STONEY:
1ebcaebd
NK
1560 init_data.flags.gpu_vm_support = true;
1561 break;
60fb100b 1562 default:
1d789535 1563 switch (adev->ip_versions[DCE_HWIP][0]) {
559f591d
AD
1564 case IP_VERSION(1, 0, 0):
1565 case IP_VERSION(1, 0, 1):
a7f520bf
AD
1566 /* enable S/G on PCO and RV2 */
1567 if ((adev->apu_flags & AMD_APU_IS_RAVEN2) ||
1568 (adev->apu_flags & AMD_APU_IS_PICASSO))
1569 init_data.flags.gpu_vm_support = true;
1570 break;
c4029779 1571 case IP_VERSION(2, 1, 0):
c08182f2 1572 case IP_VERSION(3, 0, 1):
8f56a0fe
AD
1573 case IP_VERSION(3, 1, 2):
1574 case IP_VERSION(3, 1, 3):
69ed0c5d 1575 case IP_VERSION(3, 1, 4):
512e8475 1576 case IP_VERSION(3, 1, 5):
0fe382fb 1577 case IP_VERSION(3, 1, 6):
c08182f2
AD
1578 init_data.flags.gpu_vm_support = true;
1579 break;
c08182f2
AD
1580 default:
1581 break;
1582 }
60fb100b
AD
1583 break;
1584 }
bf0207e1
AD
1585 if (init_data.flags.gpu_vm_support &&
1586 (amdgpu_sg_display == 0))
1587 init_data.flags.gpu_vm_support = false;
6e227308 1588
a7f520bf
AD
1589 if (init_data.flags.gpu_vm_support)
1590 adev->mode_info.gpu_vm_support = true;
1591
04b94af4
AD
1592 if (amdgpu_dc_feature_mask & DC_FBC_MASK)
1593 init_data.flags.fbc_support = true;
1594
d99f38ae
AD
1595 if (amdgpu_dc_feature_mask & DC_MULTI_MON_PP_MCLK_SWITCH_MASK)
1596 init_data.flags.multi_mon_pp_mclk_switch = true;
1597
eaf56410
LL
1598 if (amdgpu_dc_feature_mask & DC_DISABLE_FRACTIONAL_PWM_MASK)
1599 init_data.flags.disable_fractional_pwm = true;
a5148245
ZL
1600
1601 if (amdgpu_dc_feature_mask & DC_EDP_NO_POWER_SEQUENCING)
1602 init_data.flags.edp_no_power_sequencing = true;
eaf56410 1603
12320274
AP
1604 if (amdgpu_dc_feature_mask & DC_DISABLE_LTTPR_DP1_4A)
1605 init_data.flags.allow_lttpr_non_transparent_mode.bits.DP1_4A = true;
1606 if (amdgpu_dc_feature_mask & DC_DISABLE_LTTPR_DP2_0)
1607 init_data.flags.allow_lttpr_non_transparent_mode.bits.DP2_0 = true;
12320274 1608
80c6d680
AP
1609 /* Disable SubVP + DRR config by default */
1610 init_data.flags.disable_subvp_drr = true;
1611 if (amdgpu_dc_feature_mask & DC_ENABLE_SUBVP_DRR)
1612 init_data.flags.disable_subvp_drr = false;
1613
7aba117a 1614 init_data.flags.seamless_boot_edp_requested = false;
78ad75f8 1615
1edf5ae1 1616 if (check_seamless_boot_capability(adev)) {
7aba117a 1617 init_data.flags.seamless_boot_edp_requested = true;
1edf5ae1
ZL
1618 init_data.flags.allow_seamless_boot_optimization = true;
1619 DRM_INFO("Seamless boot condition check passed\n");
1620 }
1621
a8201902
LM
1622 init_data.flags.enable_mipi_converter_optimization = true;
1623
e5028e9f 1624 init_data.dcn_reg_offsets = adev->reg_offset[DCE_HWIP][0];
2a93292f 1625 init_data.nbio_reg_offsets = adev->reg_offset[NBIO_HWIP][0];
e5028e9f 1626
0dd79532 1627 INIT_LIST_HEAD(&adev->dm.da_list);
57b9f338
FZ
1628
1629 retrieve_dmi_info(&adev->dm);
1630
4562236b
HW
1631 /* Display Core create. */
1632 adev->dm.dc = dc_create(&init_data);
1633
423788c7 1634 if (adev->dm.dc) {
76121231 1635 DRM_INFO("Display Core initialized with v%s!\n", DC_VER);
423788c7 1636 } else {
76121231 1637 DRM_INFO("Display Core failed to initialize with v%s!\n", DC_VER);
423788c7
ES
1638 goto error;
1639 }
4562236b 1640
8a791dab
HW
1641 if (amdgpu_dc_debug_mask & DC_DISABLE_PIPE_SPLIT) {
1642 adev->dm.dc->debug.force_single_disp_pipe_split = false;
1643 adev->dm.dc->debug.pipe_split_policy = MPC_SPLIT_AVOID;
1644 }
1645
f99d8762
HW
1646 if (adev->asic_type != CHIP_CARRIZO && adev->asic_type != CHIP_STONEY)
1647 adev->dm.dc->debug.disable_stutter = amdgpu_pp_feature_mask & PP_STUTTER_MODE ? false : true;
3ce51649
AD
1648 if (dm_should_disable_stutter(adev->pdev))
1649 adev->dm.dc->debug.disable_stutter = true;
f99d8762 1650
8a791dab
HW
1651 if (amdgpu_dc_debug_mask & DC_DISABLE_STUTTER)
1652 adev->dm.dc->debug.disable_stutter = true;
1653
2665f63a 1654 if (amdgpu_dc_debug_mask & DC_DISABLE_DSC) {
8a791dab 1655 adev->dm.dc->debug.disable_dsc = true;
2665f63a 1656 }
8a791dab
HW
1657
1658 if (amdgpu_dc_debug_mask & DC_DISABLE_CLOCK_GATING)
1659 adev->dm.dc->debug.disable_clock_gate = true;
1660
cfb979f7
AP
1661 if (amdgpu_dc_debug_mask & DC_FORCE_SUBVP_MCLK_SWITCH)
1662 adev->dm.dc->debug.force_subvp_mclk_switch = true;
1663
792a0cdd
LL
1664 adev->dm.dc->debug.visual_confirm = amdgpu_dc_visual_confirm;
1665
d1bc26cb
FZ
1666 /* TODO: Remove after DP2 receiver gets proper support of Cable ID feature */
1667 adev->dm.dc->debug.ignore_cable_id = true;
1668
3d8fcc67
WL
1669 /* TODO: There is a new drm mst change where the freedom of
1670 * vc_next_start_slot update is revoked/moved into drm, instead of in
1671 * driver. This forces us to make sure to get vc_next_start_slot updated
1672 * in drm function each time without considering if mst_state is active
1673 * or not. Otherwise, next time hotplug will give wrong start_slot
1674 * number. We are implementing a temporary solution to even notify drm
1675 * mst deallocation when link is no longer of MST type when uncommitting
1676 * the stream so we will have more time to work on a proper solution.
1677 * Ideally when dm_helpers_dp_mst_stop_top_mgr message is triggered, we
1678 * should notify drm to do a complete "reset" of its states and stop
1679 * calling further drm mst functions when link is no longer of an MST
1680 * type. This could happen when we unplug an MST hubs/displays. When
1681 * uncommit stream comes later after unplug, we should just reset
1682 * hardware states only.
1683 */
1684 adev->dm.dc->debug.temp_mst_deallocation_sequence = true;
1685
e3834491
FZ
1686 if (adev->dm.dc->caps.dp_hdmi21_pcon_support)
1687 DRM_INFO("DP-HDMI FRL PCON supported\n");
1688
743b9786
NK
1689 r = dm_dmub_hw_init(adev);
1690 if (r) {
1691 DRM_ERROR("DMUB interface failed to initialize: status=%d\n", r);
1692 goto error;
1693 }
1694
bb6785c1
NK
1695 dc_hardware_init(adev->dm.dc);
1696
8e794421
WL
1697 adev->dm.hpd_rx_offload_wq = hpd_rx_irq_create_workqueue(adev->dm.dc);
1698 if (!adev->dm.hpd_rx_offload_wq) {
1699 DRM_ERROR("amdgpu: failed to create hpd rx offload workqueue.\n");
1700 goto error;
1701 }
1702
3ca001af 1703 if ((adev->flags & AMD_IS_APU) && (adev->asic_type >= CHIP_CARRIZO)) {
e6cd859d
AD
1704 struct dc_phy_addr_space_config pa_config;
1705
0b08c54b 1706 mmhub_read_system_context(adev, &pa_config);
c0fb85ae 1707
0b08c54b
YZ
1708 // Call the DC init_memory func
1709 dc_setup_system_context(adev->dm.dc, &pa_config);
1710 }
c0fb85ae 1711
4562236b
HW
1712 adev->dm.freesync_module = mod_freesync_create(adev->dm.dc);
1713 if (!adev->dm.freesync_module) {
1714 DRM_ERROR(
1715 "amdgpu: failed to initialize freesync_module.\n");
1716 } else
f1ad2f5e 1717 DRM_DEBUG_DRIVER("amdgpu: freesync_module init done %p.\n",
4562236b
HW
1718 adev->dm.freesync_module);
1719
e277adc5
LSL
1720 amdgpu_dm_init_color_mod();
1721
ea3b4242 1722 if (adev->dm.dc->caps.max_links > 0) {
09a5df6c
NK
1723 adev->dm.vblank_control_workqueue =
1724 create_singlethread_workqueue("dm_vblank_control_workqueue");
1725 if (!adev->dm.vblank_control_workqueue)
ea3b4242 1726 DRM_ERROR("amdgpu: failed to initialize vblank_workqueue.\n");
ea3b4242 1727 }
ea3b4242 1728
52704fca 1729#ifdef CONFIG_DRM_AMD_DC_HDCP
c08182f2 1730 if (adev->dm.dc->caps.max_links > 0 && adev->family >= AMDGPU_FAMILY_RV) {
e50dc171 1731 adev->dm.hdcp_workqueue = hdcp_create_workqueue(adev, &init_params.cp_psp, adev->dm.dc);
52704fca 1732
96a3b32e
BL
1733 if (!adev->dm.hdcp_workqueue)
1734 DRM_ERROR("amdgpu: failed to initialize hdcp_workqueue.\n");
1735 else
1736 DRM_DEBUG_DRIVER("amdgpu: hdcp_workqueue init done %p.\n", adev->dm.hdcp_workqueue);
52704fca 1737
96a3b32e
BL
1738 dc_init_callbacks(adev->dm.dc, &init_params);
1739 }
9a65df19
WL
1740#endif
1741#if defined(CONFIG_DRM_AMD_SECURE_DISPLAY)
b8ff7e08 1742 adev->dm.secure_display_ctxs = amdgpu_dm_crtc_secure_display_create_contexts(adev);
cbd8f20b
AL
1743 if (!adev->dm.secure_display_ctxs) {
1744 DRM_ERROR("amdgpu: failed to initialize secure_display_ctxs.\n");
1745 }
52704fca 1746#endif
11d526f1 1747 if (dc_is_dmub_outbox_supported(adev->dm.dc)) {
81927e28
JS
1748 init_completion(&adev->dm.dmub_aux_transfer_done);
1749 adev->dm.dmub_notify = kzalloc(sizeof(struct dmub_notification), GFP_KERNEL);
1750 if (!adev->dm.dmub_notify) {
1751 DRM_INFO("amdgpu: fail to allocate adev->dm.dmub_notify");
1752 goto error;
1753 }
e27c41d5
JS
1754
1755 adev->dm.delayed_hpd_wq = create_singlethread_workqueue("amdgpu_dm_hpd_wq");
1756 if (!adev->dm.delayed_hpd_wq) {
1757 DRM_ERROR("amdgpu: failed to create hpd offload workqueue.\n");
1758 goto error;
1759 }
1760
81927e28 1761 amdgpu_dm_outbox_init(adev);
e27c41d5
JS
1762 if (!register_dmub_notify_callback(adev, DMUB_NOTIFICATION_AUX_REPLY,
1763 dmub_aux_setconfig_callback, false)) {
1764 DRM_ERROR("amdgpu: fail to register dmub aux callback");
1765 goto error;
1766 }
1767 if (!register_dmub_notify_callback(adev, DMUB_NOTIFICATION_HPD, dmub_hpd_callback, true)) {
1768 DRM_ERROR("amdgpu: fail to register dmub hpd callback");
1769 goto error;
1770 }
c40a09e5
NK
1771 if (!register_dmub_notify_callback(adev, DMUB_NOTIFICATION_HPD_IRQ, dmub_hpd_callback, true)) {
1772 DRM_ERROR("amdgpu: fail to register dmub hpd callback");
1773 goto error;
1774 }
81927e28
JS
1775 }
1776
11d526f1
SW
1777 /* Enable outbox notification only after IRQ handlers are registered and DMUB is alive.
1778 * It is expected that DMUB will resend any pending notifications at this point, for
1779 * example HPD from DPIA.
1780 */
1781 if (dc_is_dmub_outbox_supported(adev->dm.dc))
1782 dc_enable_dmub_outbox(adev->dm.dc);
1783
1c43a48b
SW
1784 if (amdgpu_dm_initialize_drm_device(adev)) {
1785 DRM_ERROR(
1786 "amdgpu: failed to initialize sw for display support.\n");
1787 goto error;
1788 }
1789
f74367e4
AD
1790 /* create fake encoders for MST */
1791 dm_dp_create_fake_mst_encoders(adev);
1792
4562236b
HW
1793 /* TODO: Add_display_info? */
1794
1795 /* TODO use dynamic cursor width */
4a580877
LT
1796 adev_to_drm(adev)->mode_config.cursor_width = adev->dm.dc->caps.max_cursor_size;
1797 adev_to_drm(adev)->mode_config.cursor_height = adev->dm.dc->caps.max_cursor_size;
4562236b 1798
4a580877 1799 if (drm_vblank_init(adev_to_drm(adev), adev->dm.display_indexes_num)) {
4562236b
HW
1800 DRM_ERROR(
1801 "amdgpu: failed to initialize sw for display support.\n");
1802 goto error;
1803 }
1804
c0fb85ae 1805
f1ad2f5e 1806 DRM_DEBUG_DRIVER("KMS initialized.\n");
4562236b
HW
1807
1808 return 0;
1809error:
1810 amdgpu_dm_fini(adev);
1811
59d0f396 1812 return -EINVAL;
4562236b
HW
1813}
1814
e9669fb7
AG
1815static int amdgpu_dm_early_fini(void *handle)
1816{
1817 struct amdgpu_device *adev = (struct amdgpu_device *)handle;
1818
1819 amdgpu_dm_audio_fini(adev);
1820
1821 return 0;
1822}
1823
7578ecda 1824static void amdgpu_dm_fini(struct amdgpu_device *adev)
4562236b 1825{
f74367e4
AD
1826 int i;
1827
09a5df6c
NK
1828 if (adev->dm.vblank_control_workqueue) {
1829 destroy_workqueue(adev->dm.vblank_control_workqueue);
1830 adev->dm.vblank_control_workqueue = NULL;
1831 }
09a5df6c 1832
4562236b 1833 amdgpu_dm_destroy_drm_device(&adev->dm);
c8bdf2b6 1834
9a65df19 1835#if defined(CONFIG_DRM_AMD_SECURE_DISPLAY)
1b11ff76 1836 if (adev->dm.secure_display_ctxs) {
c3d74960 1837 for (i = 0; i < adev->mode_info.num_crtc; i++) {
1b11ff76
AL
1838 if (adev->dm.secure_display_ctxs[i].crtc) {
1839 flush_work(&adev->dm.secure_display_ctxs[i].notify_ta_work);
1840 flush_work(&adev->dm.secure_display_ctxs[i].forward_roi_work);
1841 }
1842 }
1843 kfree(adev->dm.secure_display_ctxs);
1844 adev->dm.secure_display_ctxs = NULL;
9a65df19
WL
1845 }
1846#endif
52704fca
BL
1847#ifdef CONFIG_DRM_AMD_DC_HDCP
1848 if (adev->dm.hdcp_workqueue) {
e96b1b29 1849 hdcp_destroy(&adev->dev->kobj, adev->dm.hdcp_workqueue);
52704fca
BL
1850 adev->dm.hdcp_workqueue = NULL;
1851 }
1852
1853 if (adev->dm.dc)
1854 dc_deinit_callbacks(adev->dm.dc);
1855#endif
51ba6912 1856
3beac533 1857 dc_dmub_srv_destroy(&adev->dm.dc->ctx->dmub_srv);
9a71c7d3 1858
81927e28
JS
1859 if (dc_enable_dmub_notifications(adev->dm.dc)) {
1860 kfree(adev->dm.dmub_notify);
1861 adev->dm.dmub_notify = NULL;
e27c41d5
JS
1862 destroy_workqueue(adev->dm.delayed_hpd_wq);
1863 adev->dm.delayed_hpd_wq = NULL;
81927e28
JS
1864 }
1865
743b9786
NK
1866 if (adev->dm.dmub_bo)
1867 amdgpu_bo_free_kernel(&adev->dm.dmub_bo,
1868 &adev->dm.dmub_bo_gpu_addr,
1869 &adev->dm.dmub_bo_cpu_addr);
52704fca 1870
006c26a0
AG
1871 if (adev->dm.hpd_rx_offload_wq) {
1872 for (i = 0; i < adev->dm.dc->caps.max_links; i++) {
1873 if (adev->dm.hpd_rx_offload_wq[i].wq) {
1874 destroy_workqueue(adev->dm.hpd_rx_offload_wq[i].wq);
1875 adev->dm.hpd_rx_offload_wq[i].wq = NULL;
1876 }
1877 }
1878
1879 kfree(adev->dm.hpd_rx_offload_wq);
1880 adev->dm.hpd_rx_offload_wq = NULL;
1881 }
1882
c8bdf2b6
ED
1883 /* DC Destroy TODO: Replace destroy DAL */
1884 if (adev->dm.dc)
1885 dc_destroy(&adev->dm.dc);
4562236b
HW
1886 /*
1887 * TODO: pageflip, vlank interrupt
1888 *
1889 * amdgpu_dm_irq_fini(adev);
1890 */
1891
1892 if (adev->dm.cgs_device) {
1893 amdgpu_cgs_destroy_device(adev->dm.cgs_device);
1894 adev->dm.cgs_device = NULL;
1895 }
1896 if (adev->dm.freesync_module) {
1897 mod_freesync_destroy(adev->dm.freesync_module);
1898 adev->dm.freesync_module = NULL;
1899 }
674e78ac 1900
6ce8f316 1901 mutex_destroy(&adev->dm.audio_lock);
674e78ac 1902 mutex_destroy(&adev->dm.dc_lock);
ead08b95 1903 mutex_destroy(&adev->dm.dpia_aux_lock);
674e78ac 1904
4562236b
HW
1905 return;
1906}
1907
a94d5569 1908static int load_dmcu_fw(struct amdgpu_device *adev)
4562236b 1909{
a7669aff 1910 const char *fw_name_dmcu = NULL;
a94d5569
DF
1911 int r;
1912 const struct dmcu_firmware_header_v1_0 *hdr;
1913
1914 switch(adev->asic_type) {
55e56389
MR
1915#if defined(CONFIG_DRM_AMD_DC_SI)
1916 case CHIP_TAHITI:
1917 case CHIP_PITCAIRN:
1918 case CHIP_VERDE:
1919 case CHIP_OLAND:
1920#endif
a94d5569
DF
1921 case CHIP_BONAIRE:
1922 case CHIP_HAWAII:
1923 case CHIP_KAVERI:
1924 case CHIP_KABINI:
1925 case CHIP_MULLINS:
1926 case CHIP_TONGA:
1927 case CHIP_FIJI:
1928 case CHIP_CARRIZO:
1929 case CHIP_STONEY:
1930 case CHIP_POLARIS11:
1931 case CHIP_POLARIS10:
1932 case CHIP_POLARIS12:
1933 case CHIP_VEGAM:
1934 case CHIP_VEGA10:
1935 case CHIP_VEGA12:
1936 case CHIP_VEGA20:
1937 return 0;
5ea23931
RL
1938 case CHIP_NAVI12:
1939 fw_name_dmcu = FIRMWARE_NAVI12_DMCU;
1940 break;
a94d5569 1941 case CHIP_RAVEN:
a7669aff
HW
1942 if (ASICREV_IS_PICASSO(adev->external_rev_id))
1943 fw_name_dmcu = FIRMWARE_RAVEN_DMCU;
1944 else if (ASICREV_IS_RAVEN2(adev->external_rev_id))
1945 fw_name_dmcu = FIRMWARE_RAVEN_DMCU;
1946 else
a7669aff 1947 return 0;
a94d5569
DF
1948 break;
1949 default:
1d789535 1950 switch (adev->ip_versions[DCE_HWIP][0]) {
c08182f2
AD
1951 case IP_VERSION(2, 0, 2):
1952 case IP_VERSION(2, 0, 3):
1953 case IP_VERSION(2, 0, 0):
1954 case IP_VERSION(2, 1, 0):
1955 case IP_VERSION(3, 0, 0):
1956 case IP_VERSION(3, 0, 2):
1957 case IP_VERSION(3, 0, 3):
1958 case IP_VERSION(3, 0, 1):
1959 case IP_VERSION(3, 1, 2):
1960 case IP_VERSION(3, 1, 3):
f3cd57e4 1961 case IP_VERSION(3, 1, 4):
b5b8ed44 1962 case IP_VERSION(3, 1, 5):
de7cc1b4 1963 case IP_VERSION(3, 1, 6):
577359ca
AP
1964 case IP_VERSION(3, 2, 0):
1965 case IP_VERSION(3, 2, 1):
c08182f2
AD
1966 return 0;
1967 default:
1968 break;
1969 }
a94d5569 1970 DRM_ERROR("Unsupported ASIC type: 0x%X\n", adev->asic_type);
59d0f396 1971 return -EINVAL;
a94d5569
DF
1972 }
1973
1974 if (adev->firmware.load_type != AMDGPU_FW_LOAD_PSP) {
1975 DRM_DEBUG_KMS("dm: DMCU firmware not supported on direct or SMU loading\n");
1976 return 0;
1977 }
1978
46fa9075
ML
1979 r = amdgpu_ucode_request(adev, &adev->dm.fw_dmcu, fw_name_dmcu);
1980 if (r == -ENODEV) {
a94d5569
DF
1981 /* DMCU firmware is not necessary, so don't raise a fuss if it's missing */
1982 DRM_DEBUG_KMS("dm: DMCU firmware not found\n");
1983 adev->dm.fw_dmcu = NULL;
1984 return 0;
1985 }
a94d5569
DF
1986 if (r) {
1987 dev_err(adev->dev, "amdgpu_dm: Can't validate firmware \"%s\"\n",
1988 fw_name_dmcu);
51526637 1989 amdgpu_ucode_release(&adev->dm.fw_dmcu);
a94d5569
DF
1990 return r;
1991 }
1992
1993 hdr = (const struct dmcu_firmware_header_v1_0 *)adev->dm.fw_dmcu->data;
1994 adev->firmware.ucode[AMDGPU_UCODE_ID_DMCU_ERAM].ucode_id = AMDGPU_UCODE_ID_DMCU_ERAM;
1995 adev->firmware.ucode[AMDGPU_UCODE_ID_DMCU_ERAM].fw = adev->dm.fw_dmcu;
1996 adev->firmware.fw_size +=
1997 ALIGN(le32_to_cpu(hdr->header.ucode_size_bytes) - le32_to_cpu(hdr->intv_size_bytes), PAGE_SIZE);
1998
1999 adev->firmware.ucode[AMDGPU_UCODE_ID_DMCU_INTV].ucode_id = AMDGPU_UCODE_ID_DMCU_INTV;
2000 adev->firmware.ucode[AMDGPU_UCODE_ID_DMCU_INTV].fw = adev->dm.fw_dmcu;
2001 adev->firmware.fw_size +=
2002 ALIGN(le32_to_cpu(hdr->intv_size_bytes), PAGE_SIZE);
2003
ee6e89c0
DF
2004 adev->dm.dmcu_fw_version = le32_to_cpu(hdr->header.ucode_version);
2005
a94d5569
DF
2006 DRM_DEBUG_KMS("PSP loading DMCU firmware\n");
2007
4562236b
HW
2008 return 0;
2009}
2010
743b9786
NK
2011static uint32_t amdgpu_dm_dmub_reg_read(void *ctx, uint32_t address)
2012{
2013 struct amdgpu_device *adev = ctx;
2014
2015 return dm_read_reg(adev->dm.dc->ctx, address);
2016}
2017
2018static void amdgpu_dm_dmub_reg_write(void *ctx, uint32_t address,
2019 uint32_t value)
2020{
2021 struct amdgpu_device *adev = ctx;
2022
2023 return dm_write_reg(adev->dm.dc->ctx, address, value);
2024}
2025
2026static int dm_dmub_sw_init(struct amdgpu_device *adev)
2027{
2028 struct dmub_srv_create_params create_params;
8c7aea40
NK
2029 struct dmub_srv_region_params region_params;
2030 struct dmub_srv_region_info region_info;
2031 struct dmub_srv_fb_params fb_params;
2032 struct dmub_srv_fb_info *fb_info;
2033 struct dmub_srv *dmub_srv;
743b9786 2034 const struct dmcub_firmware_header_v1_0 *hdr;
743b9786
NK
2035 enum dmub_asic dmub_asic;
2036 enum dmub_status status;
2037 int r;
2038
1d789535 2039 switch (adev->ip_versions[DCE_HWIP][0]) {
c08182f2 2040 case IP_VERSION(2, 1, 0):
743b9786 2041 dmub_asic = DMUB_ASIC_DCN21;
743b9786 2042 break;
c08182f2 2043 case IP_VERSION(3, 0, 0):
35a45d63 2044 dmub_asic = DMUB_ASIC_DCN30;
79037324 2045 break;
c08182f2 2046 case IP_VERSION(3, 0, 1):
469989ca 2047 dmub_asic = DMUB_ASIC_DCN301;
469989ca 2048 break;
c08182f2 2049 case IP_VERSION(3, 0, 2):
2a411205 2050 dmub_asic = DMUB_ASIC_DCN302;
2a411205 2051 break;
c08182f2 2052 case IP_VERSION(3, 0, 3):
656fe9b6 2053 dmub_asic = DMUB_ASIC_DCN303;
656fe9b6 2054 break;
c08182f2
AD
2055 case IP_VERSION(3, 1, 2):
2056 case IP_VERSION(3, 1, 3):
3137f792 2057 dmub_asic = (adev->external_rev_id == YELLOW_CARP_B0) ? DMUB_ASIC_DCN31B : DMUB_ASIC_DCN31;
1ebcaebd 2058 break;
e850f6b1
RL
2059 case IP_VERSION(3, 1, 4):
2060 dmub_asic = DMUB_ASIC_DCN314;
e850f6b1 2061 break;
b5b8ed44
QZ
2062 case IP_VERSION(3, 1, 5):
2063 dmub_asic = DMUB_ASIC_DCN315;
b5b8ed44 2064 break;
de7cc1b4 2065 case IP_VERSION(3, 1, 6):
868f4357 2066 dmub_asic = DMUB_ASIC_DCN316;
de7cc1b4 2067 break;
577359ca
AP
2068 case IP_VERSION(3, 2, 0):
2069 dmub_asic = DMUB_ASIC_DCN32;
577359ca
AP
2070 break;
2071 case IP_VERSION(3, 2, 1):
2072 dmub_asic = DMUB_ASIC_DCN321;
577359ca 2073 break;
743b9786
NK
2074 default:
2075 /* ASIC doesn't support DMUB. */
2076 return 0;
2077 }
2078
743b9786 2079 hdr = (const struct dmcub_firmware_header_v1_0 *)adev->dm.dmub_fw->data;
72a74a18 2080 adev->dm.dmcub_fw_version = le32_to_cpu(hdr->header.ucode_version);
743b9786 2081
9a6ed547
NK
2082 if (adev->firmware.load_type == AMDGPU_FW_LOAD_PSP) {
2083 adev->firmware.ucode[AMDGPU_UCODE_ID_DMCUB].ucode_id =
2084 AMDGPU_UCODE_ID_DMCUB;
2085 adev->firmware.ucode[AMDGPU_UCODE_ID_DMCUB].fw =
2086 adev->dm.dmub_fw;
2087 adev->firmware.fw_size +=
2088 ALIGN(le32_to_cpu(hdr->inst_const_bytes), PAGE_SIZE);
743b9786 2089
9a6ed547
NK
2090 DRM_INFO("Loading DMUB firmware via PSP: version=0x%08X\n",
2091 adev->dm.dmcub_fw_version);
2092 }
2093
743b9786 2094
8c7aea40
NK
2095 adev->dm.dmub_srv = kzalloc(sizeof(*adev->dm.dmub_srv), GFP_KERNEL);
2096 dmub_srv = adev->dm.dmub_srv;
2097
2098 if (!dmub_srv) {
2099 DRM_ERROR("Failed to allocate DMUB service!\n");
2100 return -ENOMEM;
2101 }
2102
2103 memset(&create_params, 0, sizeof(create_params));
2104 create_params.user_ctx = adev;
2105 create_params.funcs.reg_read = amdgpu_dm_dmub_reg_read;
2106 create_params.funcs.reg_write = amdgpu_dm_dmub_reg_write;
2107 create_params.asic = dmub_asic;
2108
2109 /* Create the DMUB service. */
2110 status = dmub_srv_create(dmub_srv, &create_params);
2111 if (status != DMUB_STATUS_OK) {
2112 DRM_ERROR("Error creating DMUB service: %d\n", status);
2113 return -EINVAL;
2114 }
2115
2116 /* Calculate the size of all the regions for the DMUB service. */
2117 memset(&region_params, 0, sizeof(region_params));
2118
2119 region_params.inst_const_size = le32_to_cpu(hdr->inst_const_bytes) -
2120 PSP_HEADER_BYTES - PSP_FOOTER_BYTES;
2121 region_params.bss_data_size = le32_to_cpu(hdr->bss_data_bytes);
2122 region_params.vbios_size = adev->bios_size;
0922b899 2123 region_params.fw_bss_data = region_params.bss_data_size ?
1f0674fd
NK
2124 adev->dm.dmub_fw->data +
2125 le32_to_cpu(hdr->header.ucode_array_offset_bytes) +
0922b899 2126 le32_to_cpu(hdr->inst_const_bytes) : NULL;
a576b345
NK
2127 region_params.fw_inst_const =
2128 adev->dm.dmub_fw->data +
2129 le32_to_cpu(hdr->header.ucode_array_offset_bytes) +
2130 PSP_HEADER_BYTES;
8c7aea40
NK
2131
2132 status = dmub_srv_calc_region_info(dmub_srv, &region_params,
2133 &region_info);
2134
2135 if (status != DMUB_STATUS_OK) {
2136 DRM_ERROR("Error calculating DMUB region info: %d\n", status);
2137 return -EINVAL;
2138 }
2139
2140 /*
2141 * Allocate a framebuffer based on the total size of all the regions.
2142 * TODO: Move this into GART.
2143 */
2144 r = amdgpu_bo_create_kernel(adev, region_info.fb_size, PAGE_SIZE,
58ab2c08
CK
2145 AMDGPU_GEM_DOMAIN_VRAM |
2146 AMDGPU_GEM_DOMAIN_GTT,
2147 &adev->dm.dmub_bo,
8c7aea40
NK
2148 &adev->dm.dmub_bo_gpu_addr,
2149 &adev->dm.dmub_bo_cpu_addr);
2150 if (r)
2151 return r;
2152
2153 /* Rebase the regions on the framebuffer address. */
2154 memset(&fb_params, 0, sizeof(fb_params));
2155 fb_params.cpu_addr = adev->dm.dmub_bo_cpu_addr;
2156 fb_params.gpu_addr = adev->dm.dmub_bo_gpu_addr;
2157 fb_params.region_info = &region_info;
2158
2159 adev->dm.dmub_fb_info =
2160 kzalloc(sizeof(*adev->dm.dmub_fb_info), GFP_KERNEL);
2161 fb_info = adev->dm.dmub_fb_info;
2162
2163 if (!fb_info) {
2164 DRM_ERROR(
2165 "Failed to allocate framebuffer info for DMUB service!\n");
2166 return -ENOMEM;
2167 }
2168
2169 status = dmub_srv_calc_fb_info(dmub_srv, &fb_params, fb_info);
2170 if (status != DMUB_STATUS_OK) {
2171 DRM_ERROR("Error calculating DMUB FB info: %d\n", status);
2172 return -EINVAL;
2173 }
2174
743b9786
NK
2175 return 0;
2176}
2177
a94d5569
DF
2178static int dm_sw_init(void *handle)
2179{
2180 struct amdgpu_device *adev = (struct amdgpu_device *)handle;
743b9786
NK
2181 int r;
2182
2183 r = dm_dmub_sw_init(adev);
2184 if (r)
2185 return r;
a94d5569
DF
2186
2187 return load_dmcu_fw(adev);
2188}
2189
4562236b
HW
2190static int dm_sw_fini(void *handle)
2191{
a94d5569
DF
2192 struct amdgpu_device *adev = (struct amdgpu_device *)handle;
2193
8c7aea40
NK
2194 kfree(adev->dm.dmub_fb_info);
2195 adev->dm.dmub_fb_info = NULL;
2196
743b9786
NK
2197 if (adev->dm.dmub_srv) {
2198 dmub_srv_destroy(adev->dm.dmub_srv);
2199 adev->dm.dmub_srv = NULL;
2200 }
2201
51526637
ML
2202 amdgpu_ucode_release(&adev->dm.dmub_fw);
2203 amdgpu_ucode_release(&adev->dm.fw_dmcu);
a94d5569 2204
4562236b
HW
2205 return 0;
2206}
2207
7abcf6b5 2208static int detect_mst_link_for_all_connectors(struct drm_device *dev)
4562236b 2209{
c84dec2f 2210 struct amdgpu_dm_connector *aconnector;
4562236b 2211 struct drm_connector *connector;
f8d2d39e 2212 struct drm_connector_list_iter iter;
7abcf6b5 2213 int ret = 0;
4562236b 2214
f8d2d39e
LP
2215 drm_connector_list_iter_begin(dev, &iter);
2216 drm_for_each_connector_iter(connector, &iter) {
b349f76e 2217 aconnector = to_amdgpu_dm_connector(connector);
30ec2b97
JFZ
2218 if (aconnector->dc_link->type == dc_connection_mst_branch &&
2219 aconnector->mst_mgr.aux) {
f1ad2f5e 2220 DRM_DEBUG_DRIVER("DM_MST: starting TM on aconnector: %p [id: %d]\n",
f8d2d39e
LP
2221 aconnector,
2222 aconnector->base.base.id);
7abcf6b5
AG
2223
2224 ret = drm_dp_mst_topology_mgr_set_mst(&aconnector->mst_mgr, true);
2225 if (ret < 0) {
2226 DRM_ERROR("DM_MST: Failed to start MST\n");
f8d2d39e
LP
2227 aconnector->dc_link->type =
2228 dc_connection_single;
3f6752b4
RL
2229 ret = dm_helpers_dp_mst_stop_top_mgr(aconnector->dc_link->ctx,
2230 aconnector->dc_link);
f8d2d39e 2231 break;
7abcf6b5 2232 }
f8d2d39e 2233 }
4562236b 2234 }
f8d2d39e 2235 drm_connector_list_iter_end(&iter);
4562236b 2236
7abcf6b5
AG
2237 return ret;
2238}
2239
2240static int dm_late_init(void *handle)
2241{
42e67c3b 2242 struct amdgpu_device *adev = (struct amdgpu_device *)handle;
7abcf6b5 2243
bbf854dc
DF
2244 struct dmcu_iram_parameters params;
2245 unsigned int linear_lut[16];
2246 int i;
17bdb4a8 2247 struct dmcu *dmcu = NULL;
bbf854dc 2248
17bdb4a8
JFZ
2249 dmcu = adev->dm.dc->res_pool->dmcu;
2250
bbf854dc
DF
2251 for (i = 0; i < 16; i++)
2252 linear_lut[i] = 0xFFFF * i / 15;
2253
2254 params.set = 0;
75068994 2255 params.backlight_ramping_override = false;
bbf854dc
DF
2256 params.backlight_ramping_start = 0xCCCC;
2257 params.backlight_ramping_reduction = 0xCCCCCCCC;
2258 params.backlight_lut_array_size = 16;
2259 params.backlight_lut_array = linear_lut;
2260
2ad0cdf9
AK
2261 /* Min backlight level after ABM reduction, Don't allow below 1%
2262 * 0xFFFF x 0.01 = 0x28F
2263 */
2264 params.min_abm_backlight = 0x28F;
5cb32419 2265 /* In the case where abm is implemented on dmcub,
6e568e43
JW
2266 * dmcu object will be null.
2267 * ABM 2.4 and up are implemented on dmcub.
2268 */
2269 if (dmcu) {
2270 if (!dmcu_load_iram(dmcu, params))
2271 return -EINVAL;
2272 } else if (adev->dm.dc->ctx->dmub_srv) {
2273 struct dc_link *edp_links[MAX_NUM_EDP];
2274 int edp_num;
bbf854dc 2275
7ae1dbe6 2276 dc_get_edp_links(adev->dm.dc, edp_links, &edp_num);
6e568e43
JW
2277 for (i = 0; i < edp_num; i++) {
2278 if (!dmub_init_abm_config(adev->dm.dc->res_pool, params, i))
2279 return -EINVAL;
2280 }
2281 }
bbf854dc 2282
4a580877 2283 return detect_mst_link_for_all_connectors(adev_to_drm(adev));
4562236b
HW
2284}
2285
2286static void s3_handle_mst(struct drm_device *dev, bool suspend)
2287{
c84dec2f 2288 struct amdgpu_dm_connector *aconnector;
4562236b 2289 struct drm_connector *connector;
f8d2d39e 2290 struct drm_connector_list_iter iter;
fe7553be
LP
2291 struct drm_dp_mst_topology_mgr *mgr;
2292 int ret;
2293 bool need_hotplug = false;
4562236b 2294
f8d2d39e
LP
2295 drm_connector_list_iter_begin(dev, &iter);
2296 drm_for_each_connector_iter(connector, &iter) {
fe7553be
LP
2297 aconnector = to_amdgpu_dm_connector(connector);
2298 if (aconnector->dc_link->type != dc_connection_mst_branch ||
f0127cb1 2299 aconnector->mst_root)
fe7553be
LP
2300 continue;
2301
2302 mgr = &aconnector->mst_mgr;
2303
2304 if (suspend) {
2305 drm_dp_mst_topology_mgr_suspend(mgr);
2306 } else {
1e5d4d8e
RL
2307 /* if extended timeout is supported in hardware,
2308 * default to LTTPR timeout (3.2ms) first as a W/A for DP link layer
2309 * CTS 4.2.1.1 regression introduced by CTS specs requirement update.
2310 */
2311 try_to_configure_aux_timeout(aconnector->dc_link->ddc, LINK_AUX_DEFAULT_LTTPR_TIMEOUT_PERIOD);
2312 if (!dp_is_lttpr_present(aconnector->dc_link))
2313 try_to_configure_aux_timeout(aconnector->dc_link->ddc, LINK_AUX_DEFAULT_TIMEOUT_PERIOD);
2314
6f85f738 2315 ret = drm_dp_mst_topology_mgr_resume(mgr, true);
fe7553be 2316 if (ret < 0) {
84a8b390
WL
2317 dm_helpers_dp_mst_stop_top_mgr(aconnector->dc_link->ctx,
2318 aconnector->dc_link);
fe7553be
LP
2319 need_hotplug = true;
2320 }
2321 }
4562236b 2322 }
f8d2d39e 2323 drm_connector_list_iter_end(&iter);
fe7553be
LP
2324
2325 if (need_hotplug)
2326 drm_kms_helper_hotplug_event(dev);
4562236b
HW
2327}
2328
9340dfd3
HW
2329static int amdgpu_dm_smu_write_watermarks_table(struct amdgpu_device *adev)
2330{
9340dfd3
HW
2331 int ret = 0;
2332
9340dfd3
HW
2333 /* This interface is for dGPU Navi1x.Linux dc-pplib interface depends
2334 * on window driver dc implementation.
2335 * For Navi1x, clock settings of dcn watermarks are fixed. the settings
2336 * should be passed to smu during boot up and resume from s3.
2337 * boot up: dc calculate dcn watermark clock settings within dc_create,
2338 * dcn20_resource_construct
2339 * then call pplib functions below to pass the settings to smu:
2340 * smu_set_watermarks_for_clock_ranges
2341 * smu_set_watermarks_table
2342 * navi10_set_watermarks_table
2343 * smu_write_watermarks_table
2344 *
2345 * For Renoir, clock settings of dcn watermark are also fixed values.
2346 * dc has implemented different flow for window driver:
2347 * dc_hardware_init / dc_set_power_state
2348 * dcn10_init_hw
2349 * notify_wm_ranges
2350 * set_wm_ranges
2351 * -- Linux
2352 * smu_set_watermarks_for_clock_ranges
2353 * renoir_set_watermarks_table
2354 * smu_write_watermarks_table
2355 *
2356 * For Linux,
2357 * dc_hardware_init -> amdgpu_dm_init
2358 * dc_set_power_state --> dm_resume
2359 *
2360 * therefore, this function apply to navi10/12/14 but not Renoir
2361 * *
2362 */
1d789535 2363 switch (adev->ip_versions[DCE_HWIP][0]) {
c08182f2
AD
2364 case IP_VERSION(2, 0, 2):
2365 case IP_VERSION(2, 0, 0):
9340dfd3
HW
2366 break;
2367 default:
2368 return 0;
2369 }
2370
13f5dbd6 2371 ret = amdgpu_dpm_write_watermarks_table(adev);
e7a95eea
EQ
2372 if (ret) {
2373 DRM_ERROR("Failed to update WMTABLE!\n");
2374 return ret;
9340dfd3
HW
2375 }
2376
9340dfd3
HW
2377 return 0;
2378}
2379
b8592b48
LL
2380/**
2381 * dm_hw_init() - Initialize DC device
28d687ea 2382 * @handle: The base driver device containing the amdgpu_dm device.
b8592b48
LL
2383 *
2384 * Initialize the &struct amdgpu_display_manager device. This involves calling
2385 * the initializers of each DM component, then populating the struct with them.
2386 *
2387 * Although the function implies hardware initialization, both hardware and
2388 * software are initialized here. Splitting them out to their relevant init
2389 * hooks is a future TODO item.
2390 *
2391 * Some notable things that are initialized here:
2392 *
2393 * - Display Core, both software and hardware
2394 * - DC modules that we need (freesync and color management)
2395 * - DRM software states
2396 * - Interrupt sources and handlers
2397 * - Vblank support
2398 * - Debug FS entries, if enabled
2399 */
4562236b
HW
2400static int dm_hw_init(void *handle)
2401{
2402 struct amdgpu_device *adev = (struct amdgpu_device *)handle;
2403 /* Create DAL display manager */
2404 amdgpu_dm_init(adev);
4562236b
HW
2405 amdgpu_dm_hpd_init(adev);
2406
4562236b
HW
2407 return 0;
2408}
2409
b8592b48
LL
2410/**
2411 * dm_hw_fini() - Teardown DC device
28d687ea 2412 * @handle: The base driver device containing the amdgpu_dm device.
b8592b48
LL
2413 *
2414 * Teardown components within &struct amdgpu_display_manager that require
2415 * cleanup. This involves cleaning up the DRM device, DC, and any modules that
2416 * were loaded. Also flush IRQ workqueues and disable them.
2417 */
4562236b
HW
2418static int dm_hw_fini(void *handle)
2419{
2420 struct amdgpu_device *adev = (struct amdgpu_device *)handle;
2421
2422 amdgpu_dm_hpd_fini(adev);
2423
2424 amdgpu_dm_irq_fini(adev);
21de3396 2425 amdgpu_dm_fini(adev);
4562236b
HW
2426 return 0;
2427}
2428
cdaae837 2429
cdaae837
BL
2430static void dm_gpureset_toggle_interrupts(struct amdgpu_device *adev,
2431 struct dc_state *state, bool enable)
2432{
2433 enum dc_irq_source irq_source;
2434 struct amdgpu_crtc *acrtc;
2435 int rc = -EBUSY;
2436 int i = 0;
2437
2438 for (i = 0; i < state->stream_count; i++) {
2439 acrtc = get_crtc_by_otg_inst(
2440 adev, state->stream_status[i].primary_otg_inst);
2441
2442 if (acrtc && state->stream_status[i].plane_count != 0) {
2443 irq_source = IRQ_TYPE_PFLIP + acrtc->otg_inst;
2444 rc = dc_interrupt_set(adev->dm.dc, irq_source, enable) ? 0 : -EBUSY;
4711c033
LT
2445 DRM_DEBUG_VBL("crtc %d - vupdate irq %sabling: r=%d\n",
2446 acrtc->crtc_id, enable ? "en" : "dis", rc);
cdaae837
BL
2447 if (rc)
2448 DRM_WARN("Failed to %s pflip interrupts\n",
2449 enable ? "enable" : "disable");
2450
2451 if (enable) {
2452 rc = dm_enable_vblank(&acrtc->base);
2453 if (rc)
2454 DRM_WARN("Failed to enable vblank interrupts\n");
2455 } else {
2456 dm_disable_vblank(&acrtc->base);
2457 }
2458
2459 }
2460 }
2461
2462}
2463
dfd84d90 2464static enum dc_status amdgpu_dm_commit_zero_streams(struct dc *dc)
cdaae837
BL
2465{
2466 struct dc_state *context = NULL;
2467 enum dc_status res = DC_ERROR_UNEXPECTED;
2468 int i;
2469 struct dc_stream_state *del_streams[MAX_PIPES];
2470 int del_streams_count = 0;
2471
2472 memset(del_streams, 0, sizeof(del_streams));
2473
2474 context = dc_create_state(dc);
2475 if (context == NULL)
2476 goto context_alloc_fail;
2477
2478 dc_resource_state_copy_construct_current(dc, context);
2479
2480 /* First remove from context all streams */
2481 for (i = 0; i < context->stream_count; i++) {
2482 struct dc_stream_state *stream = context->streams[i];
2483
2484 del_streams[del_streams_count++] = stream;
2485 }
2486
2487 /* Remove all planes for removed streams and then remove the streams */
2488 for (i = 0; i < del_streams_count; i++) {
2489 if (!dc_rem_all_planes_for_stream(dc, del_streams[i], context)) {
2490 res = DC_FAIL_DETACH_SURFACES;
2491 goto fail;
2492 }
2493
2494 res = dc_remove_stream_from_ctx(dc, context, del_streams[i]);
2495 if (res != DC_OK)
2496 goto fail;
2497 }
2498
cdaae837
BL
2499 res = dc_commit_state(dc, context);
2500
2501fail:
2502 dc_release_state(context);
2503
2504context_alloc_fail:
2505 return res;
2506}
2507
8e794421
WL
2508static void hpd_rx_irq_work_suspend(struct amdgpu_display_manager *dm)
2509{
2510 int i;
2511
2512 if (dm->hpd_rx_offload_wq) {
2513 for (i = 0; i < dm->dc->caps.max_links; i++)
2514 flush_workqueue(dm->hpd_rx_offload_wq[i].wq);
2515 }
2516}
2517
4562236b
HW
2518static int dm_suspend(void *handle)
2519{
2520 struct amdgpu_device *adev = handle;
2521 struct amdgpu_display_manager *dm = &adev->dm;
2522 int ret = 0;
4562236b 2523
53b3f8f4 2524 if (amdgpu_in_reset(adev)) {
cdaae837 2525 mutex_lock(&dm->dc_lock);
98ab5f35 2526
98ab5f35 2527 dc_allow_idle_optimizations(adev->dm.dc, false);
98ab5f35 2528
cdaae837
BL
2529 dm->cached_dc_state = dc_copy_state(dm->dc->current_state);
2530
2531 dm_gpureset_toggle_interrupts(adev, dm->cached_dc_state, false);
2532
2533 amdgpu_dm_commit_zero_streams(dm->dc);
2534
2535 amdgpu_dm_irq_suspend(adev);
2536
8e794421
WL
2537 hpd_rx_irq_work_suspend(dm);
2538
cdaae837
BL
2539 return ret;
2540 }
4562236b 2541
d2f0b53b 2542 WARN_ON(adev->dm.cached_state);
4a580877 2543 adev->dm.cached_state = drm_atomic_helper_suspend(adev_to_drm(adev));
d2f0b53b 2544
4a580877 2545 s3_handle_mst(adev_to_drm(adev), true);
4562236b 2546
4562236b
HW
2547 amdgpu_dm_irq_suspend(adev);
2548
8e794421
WL
2549 hpd_rx_irq_work_suspend(dm);
2550
32f5062d 2551 dc_set_power_state(dm->dc, DC_ACPI_CM_POWER_STATE_D3);
4562236b 2552
1c2075d4 2553 return 0;
4562236b
HW
2554}
2555
17ce8a69 2556struct amdgpu_dm_connector *
1daf8c63
AD
2557amdgpu_dm_find_first_crtc_matching_connector(struct drm_atomic_state *state,
2558 struct drm_crtc *crtc)
4562236b 2559{
ae67558b 2560 u32 i;
c2cea706 2561 struct drm_connector_state *new_con_state;
4562236b
HW
2562 struct drm_connector *connector;
2563 struct drm_crtc *crtc_from_state;
2564
c2cea706
LSL
2565 for_each_new_connector_in_state(state, connector, new_con_state, i) {
2566 crtc_from_state = new_con_state->crtc;
4562236b
HW
2567
2568 if (crtc_from_state == crtc)
c84dec2f 2569 return to_amdgpu_dm_connector(connector);
4562236b
HW
2570 }
2571
2572 return NULL;
2573}
2574
fbbdadf2
BL
2575static void emulated_link_detect(struct dc_link *link)
2576{
2577 struct dc_sink_init_data sink_init_data = { 0 };
2578 struct display_sink_capability sink_caps = { 0 };
2579 enum dc_edid_status edid_status;
2580 struct dc_context *dc_ctx = link->ctx;
2581 struct dc_sink *sink = NULL;
2582 struct dc_sink *prev_sink = NULL;
2583
2584 link->type = dc_connection_none;
2585 prev_sink = link->local_sink;
2586
30164a16
VL
2587 if (prev_sink)
2588 dc_sink_release(prev_sink);
fbbdadf2
BL
2589
2590 switch (link->connector_signal) {
2591 case SIGNAL_TYPE_HDMI_TYPE_A: {
2592 sink_caps.transaction_type = DDC_TRANSACTION_TYPE_I2C;
2593 sink_caps.signal = SIGNAL_TYPE_HDMI_TYPE_A;
2594 break;
2595 }
2596
2597 case SIGNAL_TYPE_DVI_SINGLE_LINK: {
2598 sink_caps.transaction_type = DDC_TRANSACTION_TYPE_I2C;
2599 sink_caps.signal = SIGNAL_TYPE_DVI_SINGLE_LINK;
2600 break;
2601 }
2602
2603 case SIGNAL_TYPE_DVI_DUAL_LINK: {
2604 sink_caps.transaction_type = DDC_TRANSACTION_TYPE_I2C;
2605 sink_caps.signal = SIGNAL_TYPE_DVI_DUAL_LINK;
2606 break;
2607 }
2608
2609 case SIGNAL_TYPE_LVDS: {
2610 sink_caps.transaction_type = DDC_TRANSACTION_TYPE_I2C;
2611 sink_caps.signal = SIGNAL_TYPE_LVDS;
2612 break;
2613 }
2614
2615 case SIGNAL_TYPE_EDP: {
2616 sink_caps.transaction_type =
2617 DDC_TRANSACTION_TYPE_I2C_OVER_AUX;
2618 sink_caps.signal = SIGNAL_TYPE_EDP;
2619 break;
2620 }
2621
2622 case SIGNAL_TYPE_DISPLAY_PORT: {
2623 sink_caps.transaction_type =
2624 DDC_TRANSACTION_TYPE_I2C_OVER_AUX;
2625 sink_caps.signal = SIGNAL_TYPE_VIRTUAL;
2626 break;
2627 }
2628
2629 default:
2630 DC_ERROR("Invalid connector type! signal:%d\n",
2631 link->connector_signal);
2632 return;
2633 }
2634
2635 sink_init_data.link = link;
2636 sink_init_data.sink_signal = sink_caps.signal;
2637
2638 sink = dc_sink_create(&sink_init_data);
2639 if (!sink) {
2640 DC_ERROR("Failed to create sink!\n");
2641 return;
2642 }
2643
dcd5fb82 2644 /* dc_sink_create returns a new reference */
fbbdadf2
BL
2645 link->local_sink = sink;
2646
2647 edid_status = dm_helpers_read_local_edid(
2648 link->ctx,
2649 link,
2650 sink);
2651
2652 if (edid_status != EDID_OK)
2653 DC_ERROR("Failed to read EDID");
2654
2655}
2656
cdaae837
BL
2657static void dm_gpureset_commit_state(struct dc_state *dc_state,
2658 struct amdgpu_display_manager *dm)
2659{
2660 struct {
2661 struct dc_surface_update surface_updates[MAX_SURFACES];
2662 struct dc_plane_info plane_infos[MAX_SURFACES];
2663 struct dc_scaling_info scaling_infos[MAX_SURFACES];
2664 struct dc_flip_addrs flip_addrs[MAX_SURFACES];
2665 struct dc_stream_update stream_update;
2666 } * bundle;
2667 int k, m;
2668
2669 bundle = kzalloc(sizeof(*bundle), GFP_KERNEL);
2670
2671 if (!bundle) {
2672 dm_error("Failed to allocate update bundle\n");
2673 goto cleanup;
2674 }
2675
2676 for (k = 0; k < dc_state->stream_count; k++) {
2677 bundle->stream_update.stream = dc_state->streams[k];
2678
2679 for (m = 0; m < dc_state->stream_status->plane_count; m++) {
2680 bundle->surface_updates[m].surface =
2681 dc_state->stream_status->plane_states[m];
2682 bundle->surface_updates[m].surface->force_full_update =
2683 true;
2684 }
2685 dc_commit_updates_for_stream(
2686 dm->dc, bundle->surface_updates,
2687 dc_state->stream_status->plane_count,
efc8278e 2688 dc_state->streams[k], &bundle->stream_update, dc_state);
cdaae837
BL
2689 }
2690
2691cleanup:
2692 kfree(bundle);
2693
2694 return;
2695}
2696
4562236b
HW
2697static int dm_resume(void *handle)
2698{
2699 struct amdgpu_device *adev = handle;
4a580877 2700 struct drm_device *ddev = adev_to_drm(adev);
4562236b 2701 struct amdgpu_display_manager *dm = &adev->dm;
c84dec2f 2702 struct amdgpu_dm_connector *aconnector;
4562236b 2703 struct drm_connector *connector;
f8d2d39e 2704 struct drm_connector_list_iter iter;
4562236b 2705 struct drm_crtc *crtc;
c2cea706 2706 struct drm_crtc_state *new_crtc_state;
fcb4019e
LSL
2707 struct dm_crtc_state *dm_new_crtc_state;
2708 struct drm_plane *plane;
2709 struct drm_plane_state *new_plane_state;
2710 struct dm_plane_state *dm_new_plane_state;
113b7a01 2711 struct dm_atomic_state *dm_state = to_dm_atomic_state(dm->atomic_obj.state);
fbbdadf2 2712 enum dc_connection_type new_connection_type = dc_connection_none;
cdaae837
BL
2713 struct dc_state *dc_state;
2714 int i, r, j;
4562236b 2715
53b3f8f4 2716 if (amdgpu_in_reset(adev)) {
cdaae837
BL
2717 dc_state = dm->cached_dc_state;
2718
6d63fcc2
NK
2719 /*
2720 * The dc->current_state is backed up into dm->cached_dc_state
2721 * before we commit 0 streams.
2722 *
2723 * DC will clear link encoder assignments on the real state
2724 * but the changes won't propagate over to the copy we made
2725 * before the 0 streams commit.
2726 *
2727 * DC expects that link encoder assignments are *not* valid
32685b32
NK
2728 * when committing a state, so as a workaround we can copy
2729 * off of the current state.
2730 *
2731 * We lose the previous assignments, but we had already
2732 * commit 0 streams anyway.
6d63fcc2 2733 */
32685b32 2734 link_enc_cfg_copy(adev->dm.dc->current_state, dc_state);
6d63fcc2 2735
cdaae837
BL
2736 r = dm_dmub_hw_init(adev);
2737 if (r)
2738 DRM_ERROR("DMUB interface failed to initialize: status=%d\n", r);
2739
2740 dc_set_power_state(dm->dc, DC_ACPI_CM_POWER_STATE_D0);
2741 dc_resume(dm->dc);
2742
2743 amdgpu_dm_irq_resume_early(adev);
2744
2745 for (i = 0; i < dc_state->stream_count; i++) {
2746 dc_state->streams[i]->mode_changed = true;
6984fa41
NK
2747 for (j = 0; j < dc_state->stream_status[i].plane_count; j++) {
2748 dc_state->stream_status[i].plane_states[j]->update_flags.raw
cdaae837
BL
2749 = 0xffffffff;
2750 }
2751 }
2752
11d526f1
SW
2753 if (dc_is_dmub_outbox_supported(adev->dm.dc)) {
2754 amdgpu_dm_outbox_init(adev);
2755 dc_enable_dmub_outbox(adev->dm.dc);
2756 }
2757
cdaae837 2758 WARN_ON(!dc_commit_state(dm->dc, dc_state));
4562236b 2759
cdaae837
BL
2760 dm_gpureset_commit_state(dm->cached_dc_state, dm);
2761
2762 dm_gpureset_toggle_interrupts(adev, dm->cached_dc_state, true);
2763
2764 dc_release_state(dm->cached_dc_state);
2765 dm->cached_dc_state = NULL;
2766
2767 amdgpu_dm_irq_resume_late(adev);
2768
2769 mutex_unlock(&dm->dc_lock);
2770
2771 return 0;
2772 }
113b7a01
LL
2773 /* Recreate dc_state - DC invalidates it when setting power state to S3. */
2774 dc_release_state(dm_state->context);
2775 dm_state->context = dc_create_state(dm->dc);
2776 /* TODO: Remove dc_state->dccg, use dc->dccg directly. */
2777 dc_resource_state_construct(dm->dc, dm_state->context);
2778
8c7aea40 2779 /* Before powering on DC we need to re-initialize DMUB. */
79d6b935 2780 dm_dmub_hw_resume(adev);
8c7aea40 2781
11d526f1
SW
2782 /* Re-enable outbox interrupts for DPIA. */
2783 if (dc_is_dmub_outbox_supported(adev->dm.dc)) {
2784 amdgpu_dm_outbox_init(adev);
2785 dc_enable_dmub_outbox(adev->dm.dc);
2786 }
2787
a80aa93d
ML
2788 /* power on hardware */
2789 dc_set_power_state(dm->dc, DC_ACPI_CM_POWER_STATE_D0);
2790
4562236b
HW
2791 /* program HPD filter */
2792 dc_resume(dm->dc);
2793
4562236b
HW
2794 /*
2795 * early enable HPD Rx IRQ, should be done before set mode as short
2796 * pulse interrupts are used for MST
2797 */
2798 amdgpu_dm_irq_resume_early(adev);
2799
d20ebea8 2800 /* On resume we need to rewrite the MSTM control bits to enable MST*/
684cd480
LP
2801 s3_handle_mst(ddev, false);
2802
4562236b 2803 /* Do detection*/
f8d2d39e
LP
2804 drm_connector_list_iter_begin(ddev, &iter);
2805 drm_for_each_connector_iter(connector, &iter) {
c84dec2f 2806 aconnector = to_amdgpu_dm_connector(connector);
4562236b 2807
7a7175a2
RL
2808 if (!aconnector->dc_link)
2809 continue;
2810
4562236b
HW
2811 /*
2812 * this is the case when traversing through already created
2813 * MST connectors, should be skipped
2814 */
7a7175a2 2815 if (aconnector->dc_link->type == dc_connection_mst_branch)
4562236b
HW
2816 continue;
2817
03ea364c 2818 mutex_lock(&aconnector->hpd_lock);
54618888 2819 if (!dc_link_detect_connection_type(aconnector->dc_link, &new_connection_type))
fbbdadf2
BL
2820 DRM_ERROR("KMS: Failed to detect connector\n");
2821
15c735e7 2822 if (aconnector->base.force && new_connection_type == dc_connection_none) {
fbbdadf2 2823 emulated_link_detect(aconnector->dc_link);
15c735e7
WL
2824 } else {
2825 mutex_lock(&dm->dc_lock);
fbbdadf2 2826 dc_link_detect(aconnector->dc_link, DETECT_REASON_HPD);
15c735e7
WL
2827 mutex_unlock(&dm->dc_lock);
2828 }
3eb4eba4
RL
2829
2830 if (aconnector->fake_enable && aconnector->dc_link->local_sink)
2831 aconnector->fake_enable = false;
2832
dcd5fb82
MF
2833 if (aconnector->dc_sink)
2834 dc_sink_release(aconnector->dc_sink);
4562236b
HW
2835 aconnector->dc_sink = NULL;
2836 amdgpu_dm_update_connector_after_detect(aconnector);
03ea364c 2837 mutex_unlock(&aconnector->hpd_lock);
4562236b 2838 }
f8d2d39e 2839 drm_connector_list_iter_end(&iter);
4562236b 2840
1f6010a9 2841 /* Force mode set in atomic commit */
a80aa93d 2842 for_each_new_crtc_in_state(dm->cached_state, crtc, new_crtc_state, i)
c2cea706 2843 new_crtc_state->active_changed = true;
4f346e65 2844
fcb4019e
LSL
2845 /*
2846 * atomic_check is expected to create the dc states. We need to release
2847 * them here, since they were duplicated as part of the suspend
2848 * procedure.
2849 */
a80aa93d 2850 for_each_new_crtc_in_state(dm->cached_state, crtc, new_crtc_state, i) {
fcb4019e
LSL
2851 dm_new_crtc_state = to_dm_crtc_state(new_crtc_state);
2852 if (dm_new_crtc_state->stream) {
2853 WARN_ON(kref_read(&dm_new_crtc_state->stream->refcount) > 1);
2854 dc_stream_release(dm_new_crtc_state->stream);
2855 dm_new_crtc_state->stream = NULL;
2856 }
2857 }
2858
a80aa93d 2859 for_each_new_plane_in_state(dm->cached_state, plane, new_plane_state, i) {
fcb4019e
LSL
2860 dm_new_plane_state = to_dm_plane_state(new_plane_state);
2861 if (dm_new_plane_state->dc_state) {
2862 WARN_ON(kref_read(&dm_new_plane_state->dc_state->refcount) > 1);
2863 dc_plane_state_release(dm_new_plane_state->dc_state);
2864 dm_new_plane_state->dc_state = NULL;
2865 }
2866 }
2867
2d1af6a1 2868 drm_atomic_helper_resume(ddev, dm->cached_state);
4562236b 2869
a80aa93d 2870 dm->cached_state = NULL;
0a214e2f 2871
9faa4237 2872 amdgpu_dm_irq_resume_late(adev);
4562236b 2873
9340dfd3
HW
2874 amdgpu_dm_smu_write_watermarks_table(adev);
2875
2d1af6a1 2876 return 0;
4562236b
HW
2877}
2878
b8592b48
LL
2879/**
2880 * DOC: DM Lifecycle
2881 *
2882 * DM (and consequently DC) is registered in the amdgpu base driver as a IP
2883 * block. When CONFIG_DRM_AMD_DC is enabled, the DM device IP block is added to
2884 * the base driver's device list to be initialized and torn down accordingly.
2885 *
2886 * The functions to do so are provided as hooks in &struct amd_ip_funcs.
2887 */
2888
4562236b
HW
2889static const struct amd_ip_funcs amdgpu_dm_funcs = {
2890 .name = "dm",
2891 .early_init = dm_early_init,
7abcf6b5 2892 .late_init = dm_late_init,
4562236b
HW
2893 .sw_init = dm_sw_init,
2894 .sw_fini = dm_sw_fini,
e9669fb7 2895 .early_fini = amdgpu_dm_early_fini,
4562236b
HW
2896 .hw_init = dm_hw_init,
2897 .hw_fini = dm_hw_fini,
2898 .suspend = dm_suspend,
2899 .resume = dm_resume,
2900 .is_idle = dm_is_idle,
2901 .wait_for_idle = dm_wait_for_idle,
2902 .check_soft_reset = dm_check_soft_reset,
2903 .soft_reset = dm_soft_reset,
2904 .set_clockgating_state = dm_set_clockgating_state,
2905 .set_powergating_state = dm_set_powergating_state,
2906};
2907
2908const struct amdgpu_ip_block_version dm_ip_block =
2909{
2910 .type = AMD_IP_BLOCK_TYPE_DCE,
2911 .major = 1,
2912 .minor = 0,
2913 .rev = 0,
2914 .funcs = &amdgpu_dm_funcs,
2915};
2916
ca3268c4 2917
b8592b48
LL
2918/**
2919 * DOC: atomic
2920 *
2921 * *WIP*
2922 */
0a323b84 2923
b3663f70 2924static const struct drm_mode_config_funcs amdgpu_dm_mode_funcs = {
4d4772f6 2925 .fb_create = amdgpu_display_user_framebuffer_create,
dfbbfe3c 2926 .get_format_info = amd_get_format_info,
4562236b 2927 .atomic_check = amdgpu_dm_atomic_check,
0269764a 2928 .atomic_commit = drm_atomic_helper_commit,
54f5499a
AG
2929};
2930
2931static struct drm_mode_config_helper_funcs amdgpu_dm_mode_config_helperfuncs = {
a5c2c0d1
LP
2932 .atomic_commit_tail = amdgpu_dm_atomic_commit_tail,
2933 .atomic_commit_setup = drm_dp_mst_atomic_setup_commit,
4562236b
HW
2934};
2935
94562810
RS
2936static void update_connector_ext_caps(struct amdgpu_dm_connector *aconnector)
2937{
94562810
RS
2938 struct amdgpu_dm_backlight_caps *caps;
2939 struct amdgpu_display_manager *dm;
2940 struct drm_connector *conn_base;
2941 struct amdgpu_device *adev;
ec11fe37 2942 struct dc_link *link = NULL;
a61bb342 2943 struct drm_luminance_range_info *luminance_range;
7fd13bae 2944 int i;
94562810
RS
2945
2946 if (!aconnector || !aconnector->dc_link)
2947 return;
2948
ec11fe37 2949 link = aconnector->dc_link;
2950 if (link->connector_signal != SIGNAL_TYPE_EDP)
2951 return;
2952
94562810 2953 conn_base = &aconnector->base;
1348969a 2954 adev = drm_to_adev(conn_base->dev);
94562810 2955 dm = &adev->dm;
7fd13bae
AD
2956 for (i = 0; i < dm->num_of_edps; i++) {
2957 if (link == dm->backlight_link[i])
2958 break;
2959 }
2960 if (i >= dm->num_of_edps)
2961 return;
2962 caps = &dm->backlight_caps[i];
94562810
RS
2963 caps->ext_caps = &aconnector->dc_link->dpcd_sink_ext_caps;
2964 caps->aux_support = false;
94562810 2965
d0ae0b64 2966 if (caps->ext_caps->bits.oled == 1 /*||
94562810 2967 caps->ext_caps->bits.sdr_aux_backlight_control == 1 ||
d0ae0b64 2968 caps->ext_caps->bits.hdr_aux_backlight_control == 1*/)
94562810
RS
2969 caps->aux_support = true;
2970
7a46f05e
TI
2971 if (amdgpu_backlight == 0)
2972 caps->aux_support = false;
2973 else if (amdgpu_backlight == 1)
2974 caps->aux_support = true;
2975
a61bb342
JH
2976 luminance_range = &conn_base->display_info.luminance_range;
2977 caps->aux_min_input_signal = luminance_range->min_luminance;
2978 caps->aux_max_input_signal = luminance_range->max_luminance;
94562810
RS
2979}
2980
97e51c16
HW
2981void amdgpu_dm_update_connector_after_detect(
2982 struct amdgpu_dm_connector *aconnector)
4562236b
HW
2983{
2984 struct drm_connector *connector = &aconnector->base;
2985 struct drm_device *dev = connector->dev;
b73a22d3 2986 struct dc_sink *sink;
4562236b
HW
2987
2988 /* MST handled by drm_mst framework */
2989 if (aconnector->mst_mgr.mst_state == true)
2990 return;
2991
4562236b 2992 sink = aconnector->dc_link->local_sink;
dcd5fb82
MF
2993 if (sink)
2994 dc_sink_retain(sink);
4562236b 2995
1f6010a9
DF
2996 /*
2997 * Edid mgmt connector gets first update only in mode_valid hook and then
4562236b 2998 * the connector sink is set to either fake or physical sink depends on link status.
1f6010a9 2999 * Skip if already done during boot.
4562236b
HW
3000 */
3001 if (aconnector->base.force != DRM_FORCE_UNSPECIFIED
3002 && aconnector->dc_em_sink) {
3003
1f6010a9
DF
3004 /*
3005 * For S3 resume with headless use eml_sink to fake stream
3006 * because on resume connector->sink is set to NULL
4562236b
HW
3007 */
3008 mutex_lock(&dev->mode_config.mutex);
3009
3010 if (sink) {
922aa1e1 3011 if (aconnector->dc_sink) {
98e6436d 3012 amdgpu_dm_update_freesync_caps(connector, NULL);
1f6010a9
DF
3013 /*
3014 * retain and release below are used to
3015 * bump up refcount for sink because the link doesn't point
3016 * to it anymore after disconnect, so on next crtc to connector
922aa1e1
AG
3017 * reshuffle by UMD we will get into unwanted dc_sink release
3018 */
dcd5fb82 3019 dc_sink_release(aconnector->dc_sink);
922aa1e1 3020 }
4562236b 3021 aconnector->dc_sink = sink;
dcd5fb82 3022 dc_sink_retain(aconnector->dc_sink);
98e6436d
AK
3023 amdgpu_dm_update_freesync_caps(connector,
3024 aconnector->edid);
4562236b 3025 } else {
98e6436d 3026 amdgpu_dm_update_freesync_caps(connector, NULL);
dcd5fb82 3027 if (!aconnector->dc_sink) {
4562236b 3028 aconnector->dc_sink = aconnector->dc_em_sink;
922aa1e1 3029 dc_sink_retain(aconnector->dc_sink);
dcd5fb82 3030 }
4562236b
HW
3031 }
3032
3033 mutex_unlock(&dev->mode_config.mutex);
dcd5fb82
MF
3034
3035 if (sink)
3036 dc_sink_release(sink);
4562236b
HW
3037 return;
3038 }
3039
3040 /*
3041 * TODO: temporary guard to look for proper fix
3042 * if this sink is MST sink, we should not do anything
3043 */
dcd5fb82
MF
3044 if (sink && sink->sink_signal == SIGNAL_TYPE_DISPLAY_PORT_MST) {
3045 dc_sink_release(sink);
4562236b 3046 return;
dcd5fb82 3047 }
4562236b
HW
3048
3049 if (aconnector->dc_sink == sink) {
1f6010a9
DF
3050 /*
3051 * We got a DP short pulse (Link Loss, DP CTS, etc...).
3052 * Do nothing!!
3053 */
f1ad2f5e 3054 DRM_DEBUG_DRIVER("DCHPD: connector_id=%d: dc_sink didn't change.\n",
4562236b 3055 aconnector->connector_id);
dcd5fb82
MF
3056 if (sink)
3057 dc_sink_release(sink);
4562236b
HW
3058 return;
3059 }
3060
f1ad2f5e 3061 DRM_DEBUG_DRIVER("DCHPD: connector_id=%d: Old sink=%p New sink=%p\n",
4562236b
HW
3062 aconnector->connector_id, aconnector->dc_sink, sink);
3063
3064 mutex_lock(&dev->mode_config.mutex);
3065
1f6010a9
DF
3066 /*
3067 * 1. Update status of the drm connector
3068 * 2. Send an event and let userspace tell us what to do
3069 */
4562236b 3070 if (sink) {
1f6010a9
DF
3071 /*
3072 * TODO: check if we still need the S3 mode update workaround.
3073 * If yes, put it here.
3074 */
c64b0d6b 3075 if (aconnector->dc_sink) {
98e6436d 3076 amdgpu_dm_update_freesync_caps(connector, NULL);
c64b0d6b
VL
3077 dc_sink_release(aconnector->dc_sink);
3078 }
4562236b
HW
3079
3080 aconnector->dc_sink = sink;
dcd5fb82 3081 dc_sink_retain(aconnector->dc_sink);
900b3cb1 3082 if (sink->dc_edid.length == 0) {
4562236b 3083 aconnector->edid = NULL;
e6142dd5
AP
3084 if (aconnector->dc_link->aux_mode) {
3085 drm_dp_cec_unset_edid(
3086 &aconnector->dm_dp_aux.aux);
3087 }
900b3cb1 3088 } else {
4562236b 3089 aconnector->edid =
e6142dd5 3090 (struct edid *)sink->dc_edid.raw_edid;
4562236b 3091
e6142dd5
AP
3092 if (aconnector->dc_link->aux_mode)
3093 drm_dp_cec_set_edid(&aconnector->dm_dp_aux.aux,
3094 aconnector->edid);
4562236b 3095 }
e6142dd5 3096
028c4ccf
QZ
3097 aconnector->timing_requested = kzalloc(sizeof(struct dc_crtc_timing), GFP_KERNEL);
3098 if (!aconnector->timing_requested)
3099 dm_error("%s: failed to create aconnector->requested_timing\n", __func__);
3100
20543be9 3101 drm_connector_update_edid_property(connector, aconnector->edid);
98e6436d 3102 amdgpu_dm_update_freesync_caps(connector, aconnector->edid);
94562810 3103 update_connector_ext_caps(aconnector);
4562236b 3104 } else {
e86e8947 3105 drm_dp_cec_unset_edid(&aconnector->dm_dp_aux.aux);
98e6436d 3106 amdgpu_dm_update_freesync_caps(connector, NULL);
c555f023 3107 drm_connector_update_edid_property(connector, NULL);
4562236b 3108 aconnector->num_modes = 0;
dcd5fb82 3109 dc_sink_release(aconnector->dc_sink);
4562236b 3110 aconnector->dc_sink = NULL;
5326c452 3111 aconnector->edid = NULL;
028c4ccf
QZ
3112 kfree(aconnector->timing_requested);
3113 aconnector->timing_requested = NULL;
0c8620d6
BL
3114#ifdef CONFIG_DRM_AMD_DC_HDCP
3115 /* Set CP to DESIRED if it was ENABLED, so we can re-enable it again on hotplug */
3116 if (connector->state->content_protection == DRM_MODE_CONTENT_PROTECTION_ENABLED)
3117 connector->state->content_protection = DRM_MODE_CONTENT_PROTECTION_DESIRED;
3118#endif
4562236b
HW
3119 }
3120
3121 mutex_unlock(&dev->mode_config.mutex);
dcd5fb82 3122
0f877894
OV
3123 update_subconnector_property(aconnector);
3124
dcd5fb82
MF
3125 if (sink)
3126 dc_sink_release(sink);
4562236b
HW
3127}
3128
e27c41d5 3129static void handle_hpd_irq_helper(struct amdgpu_dm_connector *aconnector)
4562236b 3130{
4562236b
HW
3131 struct drm_connector *connector = &aconnector->base;
3132 struct drm_device *dev = connector->dev;
fbbdadf2 3133 enum dc_connection_type new_connection_type = dc_connection_none;
1348969a 3134 struct amdgpu_device *adev = drm_to_adev(dev);
10a36226 3135#ifdef CONFIG_DRM_AMD_DC_HDCP
97f6c917 3136 struct dm_connector_state *dm_con_state = to_dm_connector_state(connector->state);
10a36226 3137#endif
15c735e7 3138 bool ret = false;
4562236b 3139
b972b4f9
HW
3140 if (adev->dm.disable_hpd_irq)
3141 return;
3142
1f6010a9
DF
3143 /*
3144 * In case of failure or MST no need to update connector status or notify the OS
3145 * since (for MST case) MST does this in its own context.
4562236b
HW
3146 */
3147 mutex_lock(&aconnector->hpd_lock);
2e0ac3d6 3148
0c8620d6 3149#ifdef CONFIG_DRM_AMD_DC_HDCP
97f6c917 3150 if (adev->dm.hdcp_workqueue) {
96a3b32e 3151 hdcp_reset_display(adev->dm.hdcp_workqueue, aconnector->dc_link->link_index);
97f6c917
BL
3152 dm_con_state->update_hdcp = true;
3153 }
0c8620d6 3154#endif
2e0ac3d6
HW
3155 if (aconnector->fake_enable)
3156 aconnector->fake_enable = false;
3157
028c4ccf
QZ
3158 aconnector->timing_changed = false;
3159
54618888 3160 if (!dc_link_detect_connection_type(aconnector->dc_link, &new_connection_type))
fbbdadf2
BL
3161 DRM_ERROR("KMS: Failed to detect connector\n");
3162
3163 if (aconnector->base.force && new_connection_type == dc_connection_none) {
3164 emulated_link_detect(aconnector->dc_link);
3165
fbbdadf2
BL
3166 drm_modeset_lock_all(dev);
3167 dm_restore_drm_connector_state(dev, connector);
3168 drm_modeset_unlock_all(dev);
3169
3170 if (aconnector->base.force == DRM_FORCE_UNSPECIFIED)
fc320a6f 3171 drm_kms_helper_connector_hotplug_event(connector);
15c735e7
WL
3172 } else {
3173 mutex_lock(&adev->dm.dc_lock);
3174 ret = dc_link_detect(aconnector->dc_link, DETECT_REASON_HPD);
3175 mutex_unlock(&adev->dm.dc_lock);
3176 if (ret) {
3177 amdgpu_dm_update_connector_after_detect(aconnector);
fbbdadf2 3178
15c735e7
WL
3179 drm_modeset_lock_all(dev);
3180 dm_restore_drm_connector_state(dev, connector);
3181 drm_modeset_unlock_all(dev);
4562236b 3182
15c735e7
WL
3183 if (aconnector->base.force == DRM_FORCE_UNSPECIFIED)
3184 drm_kms_helper_connector_hotplug_event(connector);
3185 }
4562236b
HW
3186 }
3187 mutex_unlock(&aconnector->hpd_lock);
3188
3189}
3190
e27c41d5
JS
3191static void handle_hpd_irq(void *param)
3192{
3193 struct amdgpu_dm_connector *aconnector = (struct amdgpu_dm_connector *)param;
3194
3195 handle_hpd_irq_helper(aconnector);
3196
3197}
3198
8e794421 3199static void dm_handle_mst_sideband_msg(struct amdgpu_dm_connector *aconnector)
4562236b 3200{
ae67558b
SS
3201 u8 esi[DP_PSR_ERROR_STATUS - DP_SINK_COUNT_ESI] = { 0 };
3202 u8 dret;
4562236b
HW
3203 bool new_irq_handled = false;
3204 int dpcd_addr;
3205 int dpcd_bytes_to_read;
3206
3207 const int max_process_count = 30;
3208 int process_count = 0;
3209
3210 const struct dc_link_status *link_status = dc_link_get_status(aconnector->dc_link);
3211
3212 if (link_status->dpcd_caps->dpcd_rev.raw < 0x12) {
3213 dpcd_bytes_to_read = DP_LANE0_1_STATUS - DP_SINK_COUNT;
3214 /* DPCD 0x200 - 0x201 for downstream IRQ */
3215 dpcd_addr = DP_SINK_COUNT;
3216 } else {
3217 dpcd_bytes_to_read = DP_PSR_ERROR_STATUS - DP_SINK_COUNT_ESI;
3218 /* DPCD 0x2002 - 0x2005 for downstream IRQ */
3219 dpcd_addr = DP_SINK_COUNT_ESI;
3220 }
3221
3222 dret = drm_dp_dpcd_read(
3223 &aconnector->dm_dp_aux.aux,
3224 dpcd_addr,
3225 esi,
3226 dpcd_bytes_to_read);
3227
3228 while (dret == dpcd_bytes_to_read &&
3229 process_count < max_process_count) {
ae67558b 3230 u8 retry;
4562236b
HW
3231 dret = 0;
3232
3233 process_count++;
3234
f1ad2f5e 3235 DRM_DEBUG_DRIVER("ESI %02x %02x %02x\n", esi[0], esi[1], esi[2]);
4562236b
HW
3236 /* handle HPD short pulse irq */
3237 if (aconnector->mst_mgr.mst_state)
3238 drm_dp_mst_hpd_irq(
3239 &aconnector->mst_mgr,
3240 esi,
3241 &new_irq_handled);
4562236b
HW
3242
3243 if (new_irq_handled) {
3244 /* ACK at DPCD to notify down stream */
3245 const int ack_dpcd_bytes_to_write =
3246 dpcd_bytes_to_read - 1;
3247
3248 for (retry = 0; retry < 3; retry++) {
ae67558b 3249 u8 wret;
4562236b
HW
3250
3251 wret = drm_dp_dpcd_write(
3252 &aconnector->dm_dp_aux.aux,
3253 dpcd_addr + 1,
3254 &esi[1],
3255 ack_dpcd_bytes_to_write);
3256 if (wret == ack_dpcd_bytes_to_write)
3257 break;
3258 }
3259
1f6010a9 3260 /* check if there is new irq to be handled */
4562236b
HW
3261 dret = drm_dp_dpcd_read(
3262 &aconnector->dm_dp_aux.aux,
3263 dpcd_addr,
3264 esi,
3265 dpcd_bytes_to_read);
3266
3267 new_irq_handled = false;
d4a6e8a9 3268 } else {
4562236b 3269 break;
d4a6e8a9 3270 }
4562236b
HW
3271 }
3272
3273 if (process_count == max_process_count)
f1ad2f5e 3274 DRM_DEBUG_DRIVER("Loop exceeded max iterations\n");
4562236b
HW
3275}
3276
8e794421
WL
3277static void schedule_hpd_rx_offload_work(struct hpd_rx_irq_offload_work_queue *offload_wq,
3278 union hpd_irq_data hpd_irq_data)
3279{
3280 struct hpd_rx_irq_offload_work *offload_work =
3281 kzalloc(sizeof(*offload_work), GFP_KERNEL);
3282
3283 if (!offload_work) {
3284 DRM_ERROR("Failed to allocate hpd_rx_irq_offload_work.\n");
3285 return;
3286 }
3287
3288 INIT_WORK(&offload_work->work, dm_handle_hpd_rx_offload_work);
3289 offload_work->data = hpd_irq_data;
3290 offload_work->offload_wq = offload_wq;
3291
3292 queue_work(offload_wq->wq, &offload_work->work);
3293 DRM_DEBUG_KMS("queue work to handle hpd_rx offload work");
3294}
3295
4562236b
HW
3296static void handle_hpd_rx_irq(void *param)
3297{
c84dec2f 3298 struct amdgpu_dm_connector *aconnector = (struct amdgpu_dm_connector *)param;
4562236b
HW
3299 struct drm_connector *connector = &aconnector->base;
3300 struct drm_device *dev = connector->dev;
53cbf65c 3301 struct dc_link *dc_link = aconnector->dc_link;
4562236b 3302 bool is_mst_root_connector = aconnector->mst_mgr.mst_state;
c8ea79a8 3303 bool result = false;
fbbdadf2 3304 enum dc_connection_type new_connection_type = dc_connection_none;
c8ea79a8 3305 struct amdgpu_device *adev = drm_to_adev(dev);
2a0f9270 3306 union hpd_irq_data hpd_irq_data;
8e794421
WL
3307 bool link_loss = false;
3308 bool has_left_work = false;
e322843e 3309 int idx = dc_link->link_index;
8e794421 3310 struct hpd_rx_irq_offload_work_queue *offload_wq = &adev->dm.hpd_rx_offload_wq[idx];
2a0f9270
BL
3311
3312 memset(&hpd_irq_data, 0, sizeof(hpd_irq_data));
4562236b 3313
b972b4f9
HW
3314 if (adev->dm.disable_hpd_irq)
3315 return;
3316
1f6010a9
DF
3317 /*
3318 * TODO:Temporary add mutex to protect hpd interrupt not have a gpio
4562236b
HW
3319 * conflict, after implement i2c helper, this mutex should be
3320 * retired.
3321 */
b86e7eef 3322 mutex_lock(&aconnector->hpd_lock);
4562236b 3323
8e794421
WL
3324 result = dc_link_handle_hpd_rx_irq(dc_link, &hpd_irq_data,
3325 &link_loss, true, &has_left_work);
3083a984 3326
8e794421
WL
3327 if (!has_left_work)
3328 goto out;
3329
3330 if (hpd_irq_data.bytes.device_service_irq.bits.AUTOMATED_TEST) {
3331 schedule_hpd_rx_offload_work(offload_wq, hpd_irq_data);
3332 goto out;
3333 }
3334
3335 if (dc_link_dp_allow_hpd_rx_irq(dc_link)) {
3336 if (hpd_irq_data.bytes.device_service_irq.bits.UP_REQ_MSG_RDY ||
3337 hpd_irq_data.bytes.device_service_irq.bits.DOWN_REP_MSG_RDY) {
3338 dm_handle_mst_sideband_msg(aconnector);
3083a984
QZ
3339 goto out;
3340 }
3083a984 3341
8e794421
WL
3342 if (link_loss) {
3343 bool skip = false;
d2aa1356 3344
8e794421
WL
3345 spin_lock(&offload_wq->offload_lock);
3346 skip = offload_wq->is_handling_link_loss;
3347
3348 if (!skip)
3349 offload_wq->is_handling_link_loss = true;
3350
3351 spin_unlock(&offload_wq->offload_lock);
3352
3353 if (!skip)
3354 schedule_hpd_rx_offload_work(offload_wq, hpd_irq_data);
3355
3356 goto out;
3357 }
3358 }
c8ea79a8 3359
3083a984 3360out:
c8ea79a8 3361 if (result && !is_mst_root_connector) {
4562236b 3362 /* Downstream Port status changed. */
54618888 3363 if (!dc_link_detect_connection_type(dc_link, &new_connection_type))
fbbdadf2
BL
3364 DRM_ERROR("KMS: Failed to detect connector\n");
3365
3366 if (aconnector->base.force && new_connection_type == dc_connection_none) {
3367 emulated_link_detect(dc_link);
3368
3369 if (aconnector->fake_enable)
3370 aconnector->fake_enable = false;
3371
3372 amdgpu_dm_update_connector_after_detect(aconnector);
3373
3374
3375 drm_modeset_lock_all(dev);
3376 dm_restore_drm_connector_state(dev, connector);
3377 drm_modeset_unlock_all(dev);
3378
fc320a6f 3379 drm_kms_helper_connector_hotplug_event(connector);
15c735e7
WL
3380 } else {
3381 bool ret = false;
88ac3dda 3382
15c735e7
WL
3383 mutex_lock(&adev->dm.dc_lock);
3384 ret = dc_link_detect(dc_link, DETECT_REASON_HPDRX);
3385 mutex_unlock(&adev->dm.dc_lock);
88ac3dda 3386
15c735e7
WL
3387 if (ret) {
3388 if (aconnector->fake_enable)
3389 aconnector->fake_enable = false;
4562236b 3390
15c735e7 3391 amdgpu_dm_update_connector_after_detect(aconnector);
4562236b 3392
15c735e7
WL
3393 drm_modeset_lock_all(dev);
3394 dm_restore_drm_connector_state(dev, connector);
3395 drm_modeset_unlock_all(dev);
4562236b 3396
15c735e7
WL
3397 drm_kms_helper_connector_hotplug_event(connector);
3398 }
4562236b
HW
3399 }
3400 }
2a0f9270 3401#ifdef CONFIG_DRM_AMD_DC_HDCP
95f247e7
DC
3402 if (hpd_irq_data.bytes.device_service_irq.bits.CP_IRQ) {
3403 if (adev->dm.hdcp_workqueue)
3404 hdcp_handle_cpirq(adev->dm.hdcp_workqueue, aconnector->base.index);
3405 }
2a0f9270 3406#endif
4562236b 3407
b86e7eef 3408 if (dc_link->type != dc_connection_mst_branch)
e86e8947 3409 drm_dp_cec_irq(&aconnector->dm_dp_aux.aux);
b86e7eef
NC
3410
3411 mutex_unlock(&aconnector->hpd_lock);
4562236b
HW
3412}
3413
3414static void register_hpd_handlers(struct amdgpu_device *adev)
3415{
4a580877 3416 struct drm_device *dev = adev_to_drm(adev);
4562236b 3417 struct drm_connector *connector;
c84dec2f 3418 struct amdgpu_dm_connector *aconnector;
4562236b
HW
3419 const struct dc_link *dc_link;
3420 struct dc_interrupt_params int_params = {0};
3421
3422 int_params.requested_polarity = INTERRUPT_POLARITY_DEFAULT;
3423 int_params.current_polarity = INTERRUPT_POLARITY_DEFAULT;
3424
3425 list_for_each_entry(connector,
3426 &dev->mode_config.connector_list, head) {
3427
c84dec2f 3428 aconnector = to_amdgpu_dm_connector(connector);
4562236b
HW
3429 dc_link = aconnector->dc_link;
3430
3431 if (DC_IRQ_SOURCE_INVALID != dc_link->irq_source_hpd) {
3432 int_params.int_context = INTERRUPT_LOW_IRQ_CONTEXT;
3433 int_params.irq_source = dc_link->irq_source_hpd;
3434
3435 amdgpu_dm_irq_register_interrupt(adev, &int_params,
3436 handle_hpd_irq,
3437 (void *) aconnector);
3438 }
3439
3440 if (DC_IRQ_SOURCE_INVALID != dc_link->irq_source_hpd_rx) {
3441
3442 /* Also register for DP short pulse (hpd_rx). */
3443 int_params.int_context = INTERRUPT_LOW_IRQ_CONTEXT;
3444 int_params.irq_source = dc_link->irq_source_hpd_rx;
3445
3446 amdgpu_dm_irq_register_interrupt(adev, &int_params,
3447 handle_hpd_rx_irq,
3448 (void *) aconnector);
8e794421
WL
3449
3450 if (adev->dm.hpd_rx_offload_wq)
e322843e 3451 adev->dm.hpd_rx_offload_wq[dc_link->link_index].aconnector =
8e794421 3452 aconnector;
4562236b
HW
3453 }
3454 }
3455}
3456
55e56389
MR
3457#if defined(CONFIG_DRM_AMD_DC_SI)
3458/* Register IRQ sources and initialize IRQ callbacks */
3459static int dce60_register_irq_handlers(struct amdgpu_device *adev)
3460{
3461 struct dc *dc = adev->dm.dc;
3462 struct common_irq_params *c_irq_params;
3463 struct dc_interrupt_params int_params = {0};
3464 int r;
3465 int i;
3466 unsigned client_id = AMDGPU_IRQ_CLIENTID_LEGACY;
3467
3468 int_params.requested_polarity = INTERRUPT_POLARITY_DEFAULT;
3469 int_params.current_polarity = INTERRUPT_POLARITY_DEFAULT;
3470
3471 /*
3472 * Actions of amdgpu_irq_add_id():
3473 * 1. Register a set() function with base driver.
3474 * Base driver will call set() function to enable/disable an
3475 * interrupt in DC hardware.
3476 * 2. Register amdgpu_dm_irq_handler().
3477 * Base driver will call amdgpu_dm_irq_handler() for ALL interrupts
3478 * coming from DC hardware.
3479 * amdgpu_dm_irq_handler() will re-direct the interrupt to DC
3480 * for acknowledging and handling. */
3481
3482 /* Use VBLANK interrupt */
3483 for (i = 0; i < adev->mode_info.num_crtc; i++) {
3484 r = amdgpu_irq_add_id(adev, client_id, i+1 , &adev->crtc_irq);
3485 if (r) {
3486 DRM_ERROR("Failed to add crtc irq id!\n");
3487 return r;
3488 }
3489
3490 int_params.int_context = INTERRUPT_HIGH_IRQ_CONTEXT;
3491 int_params.irq_source =
3492 dc_interrupt_to_irq_source(dc, i+1 , 0);
3493
3494 c_irq_params = &adev->dm.vblank_params[int_params.irq_source - DC_IRQ_SOURCE_VBLANK1];
3495
3496 c_irq_params->adev = adev;
3497 c_irq_params->irq_src = int_params.irq_source;
3498
3499 amdgpu_dm_irq_register_interrupt(adev, &int_params,
3500 dm_crtc_high_irq, c_irq_params);
3501 }
3502
3503 /* Use GRPH_PFLIP interrupt */
3504 for (i = VISLANDS30_IV_SRCID_D1_GRPH_PFLIP;
3505 i <= VISLANDS30_IV_SRCID_D6_GRPH_PFLIP; i += 2) {
3506 r = amdgpu_irq_add_id(adev, client_id, i, &adev->pageflip_irq);
3507 if (r) {
3508 DRM_ERROR("Failed to add page flip irq id!\n");
3509 return r;
3510 }
3511
3512 int_params.int_context = INTERRUPT_HIGH_IRQ_CONTEXT;
3513 int_params.irq_source =
3514 dc_interrupt_to_irq_source(dc, i, 0);
3515
3516 c_irq_params = &adev->dm.pflip_params[int_params.irq_source - DC_IRQ_SOURCE_PFLIP_FIRST];
3517
3518 c_irq_params->adev = adev;
3519 c_irq_params->irq_src = int_params.irq_source;
3520
3521 amdgpu_dm_irq_register_interrupt(adev, &int_params,
3522 dm_pflip_high_irq, c_irq_params);
3523
3524 }
3525
3526 /* HPD */
3527 r = amdgpu_irq_add_id(adev, client_id,
3528 VISLANDS30_IV_SRCID_HOTPLUG_DETECT_A, &adev->hpd_irq);
3529 if (r) {
3530 DRM_ERROR("Failed to add hpd irq id!\n");
3531 return r;
3532 }
3533
3534 register_hpd_handlers(adev);
3535
3536 return 0;
3537}
3538#endif
3539
4562236b
HW
3540/* Register IRQ sources and initialize IRQ callbacks */
3541static int dce110_register_irq_handlers(struct amdgpu_device *adev)
3542{
3543 struct dc *dc = adev->dm.dc;
3544 struct common_irq_params *c_irq_params;
3545 struct dc_interrupt_params int_params = {0};
3546 int r;
3547 int i;
1ffdeca6 3548 unsigned client_id = AMDGPU_IRQ_CLIENTID_LEGACY;
2c8ad2d5 3549
c08182f2 3550 if (adev->family >= AMDGPU_FAMILY_AI)
3760f76c 3551 client_id = SOC15_IH_CLIENTID_DCE;
4562236b
HW
3552
3553 int_params.requested_polarity = INTERRUPT_POLARITY_DEFAULT;
3554 int_params.current_polarity = INTERRUPT_POLARITY_DEFAULT;
3555
1f6010a9
DF
3556 /*
3557 * Actions of amdgpu_irq_add_id():
4562236b
HW
3558 * 1. Register a set() function with base driver.
3559 * Base driver will call set() function to enable/disable an
3560 * interrupt in DC hardware.
3561 * 2. Register amdgpu_dm_irq_handler().
3562 * Base driver will call amdgpu_dm_irq_handler() for ALL interrupts
3563 * coming from DC hardware.
3564 * amdgpu_dm_irq_handler() will re-direct the interrupt to DC
3565 * for acknowledging and handling. */
3566
b57de80a 3567 /* Use VBLANK interrupt */
e9029155 3568 for (i = VISLANDS30_IV_SRCID_D1_VERTICAL_INTERRUPT0; i <= VISLANDS30_IV_SRCID_D6_VERTICAL_INTERRUPT0; i++) {
2c8ad2d5 3569 r = amdgpu_irq_add_id(adev, client_id, i, &adev->crtc_irq);
4562236b
HW
3570 if (r) {
3571 DRM_ERROR("Failed to add crtc irq id!\n");
3572 return r;
3573 }
3574
3575 int_params.int_context = INTERRUPT_HIGH_IRQ_CONTEXT;
3576 int_params.irq_source =
3d761e79 3577 dc_interrupt_to_irq_source(dc, i, 0);
4562236b 3578
b57de80a 3579 c_irq_params = &adev->dm.vblank_params[int_params.irq_source - DC_IRQ_SOURCE_VBLANK1];
4562236b
HW
3580
3581 c_irq_params->adev = adev;
3582 c_irq_params->irq_src = int_params.irq_source;
3583
3584 amdgpu_dm_irq_register_interrupt(adev, &int_params,
3585 dm_crtc_high_irq, c_irq_params);
3586 }
3587
d2574c33
MK
3588 /* Use VUPDATE interrupt */
3589 for (i = VISLANDS30_IV_SRCID_D1_V_UPDATE_INT; i <= VISLANDS30_IV_SRCID_D6_V_UPDATE_INT; i += 2) {
3590 r = amdgpu_irq_add_id(adev, client_id, i, &adev->vupdate_irq);
3591 if (r) {
3592 DRM_ERROR("Failed to add vupdate irq id!\n");
3593 return r;
3594 }
3595
3596 int_params.int_context = INTERRUPT_HIGH_IRQ_CONTEXT;
3597 int_params.irq_source =
3598 dc_interrupt_to_irq_source(dc, i, 0);
3599
3600 c_irq_params = &adev->dm.vupdate_params[int_params.irq_source - DC_IRQ_SOURCE_VUPDATE1];
3601
3602 c_irq_params->adev = adev;
3603 c_irq_params->irq_src = int_params.irq_source;
3604
3605 amdgpu_dm_irq_register_interrupt(adev, &int_params,
3606 dm_vupdate_high_irq, c_irq_params);
3607 }
3608
3d761e79 3609 /* Use GRPH_PFLIP interrupt */
4562236b
HW
3610 for (i = VISLANDS30_IV_SRCID_D1_GRPH_PFLIP;
3611 i <= VISLANDS30_IV_SRCID_D6_GRPH_PFLIP; i += 2) {
2c8ad2d5 3612 r = amdgpu_irq_add_id(adev, client_id, i, &adev->pageflip_irq);
4562236b
HW
3613 if (r) {
3614 DRM_ERROR("Failed to add page flip irq id!\n");
3615 return r;
3616 }
3617
3618 int_params.int_context = INTERRUPT_HIGH_IRQ_CONTEXT;
3619 int_params.irq_source =
3620 dc_interrupt_to_irq_source(dc, i, 0);
3621
3622 c_irq_params = &adev->dm.pflip_params[int_params.irq_source - DC_IRQ_SOURCE_PFLIP_FIRST];
3623
3624 c_irq_params->adev = adev;
3625 c_irq_params->irq_src = int_params.irq_source;
3626
3627 amdgpu_dm_irq_register_interrupt(adev, &int_params,
3628 dm_pflip_high_irq, c_irq_params);
3629
3630 }
3631
3632 /* HPD */
2c8ad2d5
AD
3633 r = amdgpu_irq_add_id(adev, client_id,
3634 VISLANDS30_IV_SRCID_HOTPLUG_DETECT_A, &adev->hpd_irq);
4562236b
HW
3635 if (r) {
3636 DRM_ERROR("Failed to add hpd irq id!\n");
3637 return r;
3638 }
3639
3640 register_hpd_handlers(adev);
3641
3642 return 0;
3643}
3644
ff5ef992
AD
3645/* Register IRQ sources and initialize IRQ callbacks */
3646static int dcn10_register_irq_handlers(struct amdgpu_device *adev)
3647{
3648 struct dc *dc = adev->dm.dc;
3649 struct common_irq_params *c_irq_params;
3650 struct dc_interrupt_params int_params = {0};
3651 int r;
3652 int i;
660d5406
WL
3653#if defined(CONFIG_DRM_AMD_SECURE_DISPLAY)
3654 static const unsigned int vrtl_int_srcid[] = {
3655 DCN_1_0__SRCID__OTG1_VERTICAL_INTERRUPT0_CONTROL,
3656 DCN_1_0__SRCID__OTG2_VERTICAL_INTERRUPT0_CONTROL,
3657 DCN_1_0__SRCID__OTG3_VERTICAL_INTERRUPT0_CONTROL,
3658 DCN_1_0__SRCID__OTG4_VERTICAL_INTERRUPT0_CONTROL,
3659 DCN_1_0__SRCID__OTG5_VERTICAL_INTERRUPT0_CONTROL,
3660 DCN_1_0__SRCID__OTG6_VERTICAL_INTERRUPT0_CONTROL
3661 };
3662#endif
ff5ef992
AD
3663
3664 int_params.requested_polarity = INTERRUPT_POLARITY_DEFAULT;
3665 int_params.current_polarity = INTERRUPT_POLARITY_DEFAULT;
3666
1f6010a9
DF
3667 /*
3668 * Actions of amdgpu_irq_add_id():
ff5ef992
AD
3669 * 1. Register a set() function with base driver.
3670 * Base driver will call set() function to enable/disable an
3671 * interrupt in DC hardware.
3672 * 2. Register amdgpu_dm_irq_handler().
3673 * Base driver will call amdgpu_dm_irq_handler() for ALL interrupts
3674 * coming from DC hardware.
3675 * amdgpu_dm_irq_handler() will re-direct the interrupt to DC
3676 * for acknowledging and handling.
1f6010a9 3677 */
ff5ef992
AD
3678
3679 /* Use VSTARTUP interrupt */
3680 for (i = DCN_1_0__SRCID__DC_D1_OTG_VSTARTUP;
3681 i <= DCN_1_0__SRCID__DC_D1_OTG_VSTARTUP + adev->mode_info.num_crtc - 1;
3682 i++) {
3760f76c 3683 r = amdgpu_irq_add_id(adev, SOC15_IH_CLIENTID_DCE, i, &adev->crtc_irq);
ff5ef992
AD
3684
3685 if (r) {
3686 DRM_ERROR("Failed to add crtc irq id!\n");
3687 return r;
3688 }
3689
3690 int_params.int_context = INTERRUPT_HIGH_IRQ_CONTEXT;
3691 int_params.irq_source =
3692 dc_interrupt_to_irq_source(dc, i, 0);
3693
3694 c_irq_params = &adev->dm.vblank_params[int_params.irq_source - DC_IRQ_SOURCE_VBLANK1];
3695
3696 c_irq_params->adev = adev;
3697 c_irq_params->irq_src = int_params.irq_source;
3698
2346ef47
NK
3699 amdgpu_dm_irq_register_interrupt(
3700 adev, &int_params, dm_crtc_high_irq, c_irq_params);
3701 }
3702
86bc2219
WL
3703 /* Use otg vertical line interrupt */
3704#if defined(CONFIG_DRM_AMD_SECURE_DISPLAY)
660d5406
WL
3705 for (i = 0; i <= adev->mode_info.num_crtc - 1; i++) {
3706 r = amdgpu_irq_add_id(adev, SOC15_IH_CLIENTID_DCE,
3707 vrtl_int_srcid[i], &adev->vline0_irq);
86bc2219
WL
3708
3709 if (r) {
3710 DRM_ERROR("Failed to add vline0 irq id!\n");
3711 return r;
3712 }
3713
3714 int_params.int_context = INTERRUPT_HIGH_IRQ_CONTEXT;
3715 int_params.irq_source =
660d5406
WL
3716 dc_interrupt_to_irq_source(dc, vrtl_int_srcid[i], 0);
3717
3718 if (int_params.irq_source == DC_IRQ_SOURCE_INVALID) {
3719 DRM_ERROR("Failed to register vline0 irq %d!\n", vrtl_int_srcid[i]);
3720 break;
3721 }
86bc2219
WL
3722
3723 c_irq_params = &adev->dm.vline0_params[int_params.irq_source
3724 - DC_IRQ_SOURCE_DC1_VLINE0];
3725
3726 c_irq_params->adev = adev;
3727 c_irq_params->irq_src = int_params.irq_source;
3728
3729 amdgpu_dm_irq_register_interrupt(adev, &int_params,
3730 dm_dcn_vertical_interrupt0_high_irq, c_irq_params);
3731 }
3732#endif
3733
2346ef47
NK
3734 /* Use VUPDATE_NO_LOCK interrupt on DCN, which seems to correspond to
3735 * the regular VUPDATE interrupt on DCE. We want DC_IRQ_SOURCE_VUPDATEx
3736 * to trigger at end of each vblank, regardless of state of the lock,
3737 * matching DCE behaviour.
3738 */
3739 for (i = DCN_1_0__SRCID__OTG0_IHC_V_UPDATE_NO_LOCK_INTERRUPT;
3740 i <= DCN_1_0__SRCID__OTG0_IHC_V_UPDATE_NO_LOCK_INTERRUPT + adev->mode_info.num_crtc - 1;
3741 i++) {
3742 r = amdgpu_irq_add_id(adev, SOC15_IH_CLIENTID_DCE, i, &adev->vupdate_irq);
3743
3744 if (r) {
3745 DRM_ERROR("Failed to add vupdate irq id!\n");
3746 return r;
3747 }
3748
3749 int_params.int_context = INTERRUPT_HIGH_IRQ_CONTEXT;
3750 int_params.irq_source =
3751 dc_interrupt_to_irq_source(dc, i, 0);
3752
3753 c_irq_params = &adev->dm.vupdate_params[int_params.irq_source - DC_IRQ_SOURCE_VUPDATE1];
3754
3755 c_irq_params->adev = adev;
3756 c_irq_params->irq_src = int_params.irq_source;
3757
ff5ef992 3758 amdgpu_dm_irq_register_interrupt(adev, &int_params,
2346ef47 3759 dm_vupdate_high_irq, c_irq_params);
d2574c33
MK
3760 }
3761
ff5ef992
AD
3762 /* Use GRPH_PFLIP interrupt */
3763 for (i = DCN_1_0__SRCID__HUBP0_FLIP_INTERRUPT;
de95753c 3764 i <= DCN_1_0__SRCID__HUBP0_FLIP_INTERRUPT + dc->caps.max_otg_num - 1;
ff5ef992 3765 i++) {
3760f76c 3766 r = amdgpu_irq_add_id(adev, SOC15_IH_CLIENTID_DCE, i, &adev->pageflip_irq);
ff5ef992
AD
3767 if (r) {
3768 DRM_ERROR("Failed to add page flip irq id!\n");
3769 return r;
3770 }
3771
3772 int_params.int_context = INTERRUPT_HIGH_IRQ_CONTEXT;
3773 int_params.irq_source =
3774 dc_interrupt_to_irq_source(dc, i, 0);
3775
3776 c_irq_params = &adev->dm.pflip_params[int_params.irq_source - DC_IRQ_SOURCE_PFLIP_FIRST];
3777
3778 c_irq_params->adev = adev;
3779 c_irq_params->irq_src = int_params.irq_source;
3780
3781 amdgpu_dm_irq_register_interrupt(adev, &int_params,
3782 dm_pflip_high_irq, c_irq_params);
3783
3784 }
3785
81927e28
JS
3786 /* HPD */
3787 r = amdgpu_irq_add_id(adev, SOC15_IH_CLIENTID_DCE, DCN_1_0__SRCID__DC_HPD1_INT,
3788 &adev->hpd_irq);
3789 if (r) {
3790 DRM_ERROR("Failed to add hpd irq id!\n");
3791 return r;
3792 }
a08f16cf 3793
81927e28 3794 register_hpd_handlers(adev);
a08f16cf 3795
81927e28
JS
3796 return 0;
3797}
3798/* Register Outbox IRQ sources and initialize IRQ callbacks */
3799static int register_outbox_irq_handlers(struct amdgpu_device *adev)
3800{
3801 struct dc *dc = adev->dm.dc;
3802 struct common_irq_params *c_irq_params;
3803 struct dc_interrupt_params int_params = {0};
3804 int r, i;
3805
3806 int_params.requested_polarity = INTERRUPT_POLARITY_DEFAULT;
3807 int_params.current_polarity = INTERRUPT_POLARITY_DEFAULT;
3808
3809 r = amdgpu_irq_add_id(adev, SOC15_IH_CLIENTID_DCE, DCN_1_0__SRCID__DMCUB_OUTBOX_LOW_PRIORITY_READY_INT,
3810 &adev->dmub_outbox_irq);
3811 if (r) {
3812 DRM_ERROR("Failed to add outbox irq id!\n");
3813 return r;
3814 }
3815
3816 if (dc->ctx->dmub_srv) {
3817 i = DCN_1_0__SRCID__DMCUB_OUTBOX_LOW_PRIORITY_READY_INT;
3818 int_params.int_context = INTERRUPT_LOW_IRQ_CONTEXT;
a08f16cf 3819 int_params.irq_source =
81927e28 3820 dc_interrupt_to_irq_source(dc, i, 0);
a08f16cf 3821
81927e28 3822 c_irq_params = &adev->dm.dmub_outbox_params[0];
a08f16cf
LHM
3823
3824 c_irq_params->adev = adev;
3825 c_irq_params->irq_src = int_params.irq_source;
3826
3827 amdgpu_dm_irq_register_interrupt(adev, &int_params,
81927e28 3828 dm_dmub_outbox1_low_irq, c_irq_params);
ff5ef992
AD
3829 }
3830
ff5ef992
AD
3831 return 0;
3832}
ff5ef992 3833
eb3dc897
NK
3834/*
3835 * Acquires the lock for the atomic state object and returns
3836 * the new atomic state.
3837 *
3838 * This should only be called during atomic check.
3839 */
17ce8a69
RL
3840int dm_atomic_get_state(struct drm_atomic_state *state,
3841 struct dm_atomic_state **dm_state)
eb3dc897
NK
3842{
3843 struct drm_device *dev = state->dev;
1348969a 3844 struct amdgpu_device *adev = drm_to_adev(dev);
eb3dc897
NK
3845 struct amdgpu_display_manager *dm = &adev->dm;
3846 struct drm_private_state *priv_state;
eb3dc897
NK
3847
3848 if (*dm_state)
3849 return 0;
3850
eb3dc897
NK
3851 priv_state = drm_atomic_get_private_obj_state(state, &dm->atomic_obj);
3852 if (IS_ERR(priv_state))
3853 return PTR_ERR(priv_state);
3854
3855 *dm_state = to_dm_atomic_state(priv_state);
3856
3857 return 0;
3858}
3859
dfd84d90 3860static struct dm_atomic_state *
eb3dc897
NK
3861dm_atomic_get_new_state(struct drm_atomic_state *state)
3862{
3863 struct drm_device *dev = state->dev;
1348969a 3864 struct amdgpu_device *adev = drm_to_adev(dev);
eb3dc897
NK
3865 struct amdgpu_display_manager *dm = &adev->dm;
3866 struct drm_private_obj *obj;
3867 struct drm_private_state *new_obj_state;
3868 int i;
3869
3870 for_each_new_private_obj_in_state(state, obj, new_obj_state, i) {
3871 if (obj->funcs == dm->atomic_obj.funcs)
3872 return to_dm_atomic_state(new_obj_state);
3873 }
3874
3875 return NULL;
3876}
3877
eb3dc897
NK
3878static struct drm_private_state *
3879dm_atomic_duplicate_state(struct drm_private_obj *obj)
3880{
3881 struct dm_atomic_state *old_state, *new_state;
3882
3883 new_state = kzalloc(sizeof(*new_state), GFP_KERNEL);
3884 if (!new_state)
3885 return NULL;
3886
3887 __drm_atomic_helper_private_obj_duplicate_state(obj, &new_state->base);
3888
813d20dc
AW
3889 old_state = to_dm_atomic_state(obj->state);
3890
3891 if (old_state && old_state->context)
3892 new_state->context = dc_copy_state(old_state->context);
3893
eb3dc897
NK
3894 if (!new_state->context) {
3895 kfree(new_state);
3896 return NULL;
3897 }
3898
eb3dc897
NK
3899 return &new_state->base;
3900}
3901
3902static void dm_atomic_destroy_state(struct drm_private_obj *obj,
3903 struct drm_private_state *state)
3904{
3905 struct dm_atomic_state *dm_state = to_dm_atomic_state(state);
3906
3907 if (dm_state && dm_state->context)
3908 dc_release_state(dm_state->context);
3909
3910 kfree(dm_state);
3911}
3912
3913static struct drm_private_state_funcs dm_atomic_state_funcs = {
3914 .atomic_duplicate_state = dm_atomic_duplicate_state,
3915 .atomic_destroy_state = dm_atomic_destroy_state,
3916};
3917
4562236b
HW
3918static int amdgpu_dm_mode_config_init(struct amdgpu_device *adev)
3919{
eb3dc897 3920 struct dm_atomic_state *state;
4562236b
HW
3921 int r;
3922
3923 adev->mode_info.mode_config_initialized = true;
3924
4a580877
LT
3925 adev_to_drm(adev)->mode_config.funcs = (void *)&amdgpu_dm_mode_funcs;
3926 adev_to_drm(adev)->mode_config.helper_private = &amdgpu_dm_mode_config_helperfuncs;
4562236b 3927
4a580877
LT
3928 adev_to_drm(adev)->mode_config.max_width = 16384;
3929 adev_to_drm(adev)->mode_config.max_height = 16384;
4562236b 3930
4a580877 3931 adev_to_drm(adev)->mode_config.preferred_depth = 24;
a6250bdb
AD
3932 if (adev->asic_type == CHIP_HAWAII)
3933 /* disable prefer shadow for now due to hibernation issues */
3934 adev_to_drm(adev)->mode_config.prefer_shadow = 0;
3935 else
3936 adev_to_drm(adev)->mode_config.prefer_shadow = 1;
1f6010a9 3937 /* indicates support for immediate flip */
4a580877 3938 adev_to_drm(adev)->mode_config.async_page_flip = true;
4562236b 3939
eb3dc897
NK
3940 state = kzalloc(sizeof(*state), GFP_KERNEL);
3941 if (!state)
3942 return -ENOMEM;
3943
813d20dc 3944 state->context = dc_create_state(adev->dm.dc);
eb3dc897
NK
3945 if (!state->context) {
3946 kfree(state);
3947 return -ENOMEM;
3948 }
3949
3950 dc_resource_state_copy_construct_current(adev->dm.dc, state->context);
3951
4a580877 3952 drm_atomic_private_obj_init(adev_to_drm(adev),
8c1a765b 3953 &adev->dm.atomic_obj,
eb3dc897
NK
3954 &state->base,
3955 &dm_atomic_state_funcs);
3956
3dc9b1ce 3957 r = amdgpu_display_modeset_create_props(adev);
b67a468a
DL
3958 if (r) {
3959 dc_release_state(state->context);
3960 kfree(state);
4562236b 3961 return r;
b67a468a 3962 }
4562236b 3963
6ce8f316 3964 r = amdgpu_dm_audio_init(adev);
b67a468a
DL
3965 if (r) {
3966 dc_release_state(state->context);
3967 kfree(state);
6ce8f316 3968 return r;
b67a468a 3969 }
6ce8f316 3970
4562236b
HW
3971 return 0;
3972}
3973
206bbafe
DF
3974#define AMDGPU_DM_DEFAULT_MIN_BACKLIGHT 12
3975#define AMDGPU_DM_DEFAULT_MAX_BACKLIGHT 255
94562810 3976#define AUX_BL_DEFAULT_TRANSITION_TIME_MS 50
206bbafe 3977
7fd13bae
AD
3978static void amdgpu_dm_update_backlight_caps(struct amdgpu_display_manager *dm,
3979 int bl_idx)
206bbafe
DF
3980{
3981#if defined(CONFIG_ACPI)
3982 struct amdgpu_dm_backlight_caps caps;
3983
58965855
FS
3984 memset(&caps, 0, sizeof(caps));
3985
7fd13bae 3986 if (dm->backlight_caps[bl_idx].caps_valid)
206bbafe
DF
3987 return;
3988
f9b7f370 3989 amdgpu_acpi_get_backlight_caps(&caps);
206bbafe 3990 if (caps.caps_valid) {
7fd13bae 3991 dm->backlight_caps[bl_idx].caps_valid = true;
94562810
RS
3992 if (caps.aux_support)
3993 return;
7fd13bae
AD
3994 dm->backlight_caps[bl_idx].min_input_signal = caps.min_input_signal;
3995 dm->backlight_caps[bl_idx].max_input_signal = caps.max_input_signal;
206bbafe 3996 } else {
7fd13bae 3997 dm->backlight_caps[bl_idx].min_input_signal =
206bbafe 3998 AMDGPU_DM_DEFAULT_MIN_BACKLIGHT;
7fd13bae 3999 dm->backlight_caps[bl_idx].max_input_signal =
206bbafe
DF
4000 AMDGPU_DM_DEFAULT_MAX_BACKLIGHT;
4001 }
4002#else
7fd13bae 4003 if (dm->backlight_caps[bl_idx].aux_support)
94562810
RS
4004 return;
4005
7fd13bae
AD
4006 dm->backlight_caps[bl_idx].min_input_signal = AMDGPU_DM_DEFAULT_MIN_BACKLIGHT;
4007 dm->backlight_caps[bl_idx].max_input_signal = AMDGPU_DM_DEFAULT_MAX_BACKLIGHT;
206bbafe
DF
4008#endif
4009}
4010
69d9f427
AM
4011static int get_brightness_range(const struct amdgpu_dm_backlight_caps *caps,
4012 unsigned *min, unsigned *max)
94562810 4013{
94562810 4014 if (!caps)
69d9f427 4015 return 0;
94562810 4016
69d9f427
AM
4017 if (caps->aux_support) {
4018 // Firmware limits are in nits, DC API wants millinits.
4019 *max = 1000 * caps->aux_max_input_signal;
4020 *min = 1000 * caps->aux_min_input_signal;
94562810 4021 } else {
69d9f427
AM
4022 // Firmware limits are 8-bit, PWM control is 16-bit.
4023 *max = 0x101 * caps->max_input_signal;
4024 *min = 0x101 * caps->min_input_signal;
94562810 4025 }
69d9f427
AM
4026 return 1;
4027}
94562810 4028
69d9f427
AM
4029static u32 convert_brightness_from_user(const struct amdgpu_dm_backlight_caps *caps,
4030 uint32_t brightness)
4031{
4032 unsigned min, max;
94562810 4033
69d9f427
AM
4034 if (!get_brightness_range(caps, &min, &max))
4035 return brightness;
4036
4037 // Rescale 0..255 to min..max
4038 return min + DIV_ROUND_CLOSEST((max - min) * brightness,
4039 AMDGPU_MAX_BL_LEVEL);
4040}
4041
4042static u32 convert_brightness_to_user(const struct amdgpu_dm_backlight_caps *caps,
4043 uint32_t brightness)
4044{
4045 unsigned min, max;
4046
4047 if (!get_brightness_range(caps, &min, &max))
4048 return brightness;
4049
4050 if (brightness < min)
4051 return 0;
4052 // Rescale min..max to 0..255
4053 return DIV_ROUND_CLOSEST(AMDGPU_MAX_BL_LEVEL * (brightness - min),
4054 max - min);
94562810
RS
4055}
4056
4052287a 4057static void amdgpu_dm_backlight_set_level(struct amdgpu_display_manager *dm,
7fd13bae 4058 int bl_idx,
3d6c9164 4059 u32 user_brightness)
4562236b 4060{
206bbafe 4061 struct amdgpu_dm_backlight_caps caps;
7fd13bae
AD
4062 struct dc_link *link;
4063 u32 brightness;
94562810 4064 bool rc;
4562236b 4065
7fd13bae
AD
4066 amdgpu_dm_update_backlight_caps(dm, bl_idx);
4067 caps = dm->backlight_caps[bl_idx];
94562810 4068
7fd13bae 4069 dm->brightness[bl_idx] = user_brightness;
1f579254
AD
4070 /* update scratch register */
4071 if (bl_idx == 0)
4072 amdgpu_atombios_scratch_regs_set_backlight_level(dm->adev, dm->brightness[bl_idx]);
7fd13bae
AD
4073 brightness = convert_brightness_from_user(&caps, dm->brightness[bl_idx]);
4074 link = (struct dc_link *)dm->backlight_link[bl_idx];
94562810 4075
3d6c9164 4076 /* Change brightness based on AUX property */
118b4627 4077 if (caps.aux_support) {
7fd13bae
AD
4078 rc = dc_link_set_backlight_level_nits(link, true, brightness,
4079 AUX_BL_DEFAULT_TRANSITION_TIME_MS);
4080 if (!rc)
4081 DRM_DEBUG("DM: Failed to update backlight via AUX on eDP[%d]\n", bl_idx);
118b4627 4082 } else {
7fd13bae
AD
4083 rc = dc_link_set_backlight_level(link, brightness, 0);
4084 if (!rc)
4085 DRM_DEBUG("DM: Failed to update backlight on eDP[%d]\n", bl_idx);
118b4627 4086 }
94562810 4087
4052287a
S
4088 if (rc)
4089 dm->actual_brightness[bl_idx] = user_brightness;
4562236b
HW
4090}
4091
3d6c9164 4092static int amdgpu_dm_backlight_update_status(struct backlight_device *bd)
4562236b 4093{
620a0d27 4094 struct amdgpu_display_manager *dm = bl_get_data(bd);
7fd13bae 4095 int i;
3d6c9164 4096
7fd13bae
AD
4097 for (i = 0; i < dm->num_of_edps; i++) {
4098 if (bd == dm->backlight_dev[i])
4099 break;
4100 }
4101 if (i >= AMDGPU_DM_MAX_NUM_EDP)
4102 i = 0;
4103 amdgpu_dm_backlight_set_level(dm, i, bd->props.brightness);
3d6c9164
AD
4104
4105 return 0;
4106}
4107
7fd13bae
AD
4108static u32 amdgpu_dm_backlight_get_level(struct amdgpu_display_manager *dm,
4109 int bl_idx)
3d6c9164 4110{
0ad3e64e 4111 struct amdgpu_dm_backlight_caps caps;
7fd13bae 4112 struct dc_link *link = (struct dc_link *)dm->backlight_link[bl_idx];
0ad3e64e 4113
7fd13bae
AD
4114 amdgpu_dm_update_backlight_caps(dm, bl_idx);
4115 caps = dm->backlight_caps[bl_idx];
620a0d27 4116
0ad3e64e 4117 if (caps.aux_support) {
0ad3e64e
AD
4118 u32 avg, peak;
4119 bool rc;
4120
4121 rc = dc_link_get_backlight_level_nits(link, &avg, &peak);
4122 if (!rc)
7fd13bae 4123 return dm->brightness[bl_idx];
0ad3e64e
AD
4124 return convert_brightness_to_user(&caps, avg);
4125 } else {
7fd13bae 4126 int ret = dc_link_get_backlight_level(link);
0ad3e64e
AD
4127
4128 if (ret == DC_ERROR_UNEXPECTED)
7fd13bae 4129 return dm->brightness[bl_idx];
0ad3e64e
AD
4130 return convert_brightness_to_user(&caps, ret);
4131 }
4562236b
HW
4132}
4133
3d6c9164
AD
4134static int amdgpu_dm_backlight_get_brightness(struct backlight_device *bd)
4135{
4136 struct amdgpu_display_manager *dm = bl_get_data(bd);
7fd13bae 4137 int i;
3d6c9164 4138
7fd13bae
AD
4139 for (i = 0; i < dm->num_of_edps; i++) {
4140 if (bd == dm->backlight_dev[i])
4141 break;
4142 }
4143 if (i >= AMDGPU_DM_MAX_NUM_EDP)
4144 i = 0;
4145 return amdgpu_dm_backlight_get_level(dm, i);
3d6c9164
AD
4146}
4147
4562236b 4148static const struct backlight_ops amdgpu_dm_backlight_ops = {
bb264220 4149 .options = BL_CORE_SUSPENDRESUME,
4562236b
HW
4150 .get_brightness = amdgpu_dm_backlight_get_brightness,
4151 .update_status = amdgpu_dm_backlight_update_status,
4152};
4153
7578ecda 4154static void
d24b77e4
HG
4155amdgpu_dm_register_backlight_device(struct amdgpu_display_manager *dm,
4156 struct amdgpu_dm_connector *aconnector)
4562236b
HW
4157{
4158 char bl_name[16];
4159 struct backlight_properties props = { 0 };
4160
7fd13bae
AD
4161 amdgpu_dm_update_backlight_caps(dm, dm->num_of_edps);
4162 dm->brightness[dm->num_of_edps] = AMDGPU_MAX_BL_LEVEL;
206bbafe 4163
da11ef83
HG
4164 if (!acpi_video_backlight_use_native()) {
4165 drm_info(adev_to_drm(dm->adev), "Skipping amdgpu DM backlight registration\n");
c0f50c5d
HG
4166 /* Try registering an ACPI video backlight device instead. */
4167 acpi_video_register_backlight();
da11ef83
HG
4168 return;
4169 }
4170
4562236b 4171 props.max_brightness = AMDGPU_MAX_BL_LEVEL;
53a53f86 4172 props.brightness = AMDGPU_MAX_BL_LEVEL;
4562236b
HW
4173 props.type = BACKLIGHT_RAW;
4174
4175 snprintf(bl_name, sizeof(bl_name), "amdgpu_bl%d",
7fd13bae 4176 adev_to_drm(dm->adev)->primary->index + dm->num_of_edps);
4562236b 4177
7fd13bae 4178 dm->backlight_dev[dm->num_of_edps] = backlight_device_register(bl_name,
d24b77e4 4179 aconnector->base.kdev,
7fd13bae
AD
4180 dm,
4181 &amdgpu_dm_backlight_ops,
4182 &props);
4562236b 4183
7fd13bae 4184 if (IS_ERR(dm->backlight_dev[dm->num_of_edps]))
4562236b
HW
4185 DRM_ERROR("DM: Backlight registration failed!\n");
4186 else
f1ad2f5e 4187 DRM_DEBUG_DRIVER("DM: Registered Backlight device: %s\n", bl_name);
4562236b 4188}
4562236b 4189
df534fff 4190static int initialize_plane(struct amdgpu_display_manager *dm,
b2fddb13 4191 struct amdgpu_mode_info *mode_info, int plane_id,
cc1fec57
NK
4192 enum drm_plane_type plane_type,
4193 const struct dc_plane_cap *plane_cap)
df534fff 4194{
f180b4bc 4195 struct drm_plane *plane;
df534fff
S
4196 unsigned long possible_crtcs;
4197 int ret = 0;
4198
f180b4bc 4199 plane = kzalloc(sizeof(struct drm_plane), GFP_KERNEL);
df534fff
S
4200 if (!plane) {
4201 DRM_ERROR("KMS: Failed to allocate plane\n");
4202 return -ENOMEM;
4203 }
b2fddb13 4204 plane->type = plane_type;
df534fff
S
4205
4206 /*
b2fddb13
NK
4207 * HACK: IGT tests expect that the primary plane for a CRTC
4208 * can only have one possible CRTC. Only expose support for
4209 * any CRTC if they're not going to be used as a primary plane
4210 * for a CRTC - like overlay or underlay planes.
df534fff
S
4211 */
4212 possible_crtcs = 1 << plane_id;
4213 if (plane_id >= dm->dc->caps.max_streams)
4214 possible_crtcs = 0xff;
4215
cc1fec57 4216 ret = amdgpu_dm_plane_init(dm, plane, possible_crtcs, plane_cap);
df534fff
S
4217
4218 if (ret) {
4219 DRM_ERROR("KMS: Failed to initialize plane\n");
54087768 4220 kfree(plane);
df534fff
S
4221 return ret;
4222 }
4223
54087768
NK
4224 if (mode_info)
4225 mode_info->planes[plane_id] = plane;
4226
df534fff
S
4227 return ret;
4228}
4229
89fc8d4e
HW
4230
4231static void register_backlight_device(struct amdgpu_display_manager *dm,
d24b77e4 4232 struct amdgpu_dm_connector *aconnector,
89fc8d4e
HW
4233 struct dc_link *link)
4234{
89fc8d4e
HW
4235 if ((link->connector_signal & (SIGNAL_TYPE_EDP | SIGNAL_TYPE_LVDS)) &&
4236 link->type != dc_connection_none) {
1f6010a9
DF
4237 /*
4238 * Event if registration failed, we should continue with
89fc8d4e
HW
4239 * DM initialization because not having a backlight control
4240 * is better then a black screen.
4241 */
7fd13bae 4242 if (!dm->backlight_dev[dm->num_of_edps])
d24b77e4 4243 amdgpu_dm_register_backlight_device(dm, aconnector);
89fc8d4e 4244
7fd13bae 4245 if (dm->backlight_dev[dm->num_of_edps]) {
118b4627
ML
4246 dm->backlight_link[dm->num_of_edps] = link;
4247 dm->num_of_edps++;
4248 }
89fc8d4e 4249 }
89fc8d4e
HW
4250}
4251
acc96ae0 4252static void amdgpu_set_panel_orientation(struct drm_connector *connector);
89fc8d4e 4253
1f6010a9
DF
4254/*
4255 * In this architecture, the association
4562236b
HW
4256 * connector -> encoder -> crtc
4257 * id not really requried. The crtc and connector will hold the
4258 * display_index as an abstraction to use with DAL component
4259 *
4260 * Returns 0 on success
4261 */
7578ecda 4262static int amdgpu_dm_initialize_drm_device(struct amdgpu_device *adev)
4562236b
HW
4263{
4264 struct amdgpu_display_manager *dm = &adev->dm;
ae67558b 4265 s32 i;
c84dec2f 4266 struct amdgpu_dm_connector *aconnector = NULL;
f2a0f5e6 4267 struct amdgpu_encoder *aencoder = NULL;
d4e13b0d 4268 struct amdgpu_mode_info *mode_info = &adev->mode_info;
ae67558b
SS
4269 u32 link_cnt;
4270 s32 primary_planes;
fbbdadf2 4271 enum dc_connection_type new_connection_type = dc_connection_none;
cc1fec57 4272 const struct dc_plane_cap *plane;
9470620e 4273 bool psr_feature_enabled = false;
35f33086 4274 int max_overlay = dm->dc->caps.max_slave_planes;
4562236b 4275
d58159de
AD
4276 dm->display_indexes_num = dm->dc->caps.max_streams;
4277 /* Update the actual used number of crtc */
4278 adev->mode_info.num_crtc = adev->dm.display_indexes_num;
4279
60971b20 4280 amdgpu_dm_set_irq_funcs(adev);
4281
4562236b 4282 link_cnt = dm->dc->caps.max_links;
4562236b
HW
4283 if (amdgpu_dm_mode_config_init(dm->adev)) {
4284 DRM_ERROR("DM: Failed to initialize mode config\n");
59d0f396 4285 return -EINVAL;
4562236b
HW
4286 }
4287
b2fddb13
NK
4288 /* There is one primary plane per CRTC */
4289 primary_planes = dm->dc->caps.max_streams;
54087768 4290 ASSERT(primary_planes <= AMDGPU_MAX_PLANES);
efa6a8b7 4291
b2fddb13
NK
4292 /*
4293 * Initialize primary planes, implicit planes for legacy IOCTLS.
4294 * Order is reversed to match iteration order in atomic check.
4295 */
4296 for (i = (primary_planes - 1); i >= 0; i--) {
cc1fec57
NK
4297 plane = &dm->dc->caps.planes[i];
4298
b2fddb13 4299 if (initialize_plane(dm, mode_info, i,
cc1fec57 4300 DRM_PLANE_TYPE_PRIMARY, plane)) {
df534fff 4301 DRM_ERROR("KMS: Failed to initialize primary plane\n");
cd8a2ae8 4302 goto fail;
d4e13b0d 4303 }
df534fff 4304 }
92f3ac40 4305
0d579c7e
NK
4306 /*
4307 * Initialize overlay planes, index starting after primary planes.
4308 * These planes have a higher DRM index than the primary planes since
4309 * they should be considered as having a higher z-order.
4310 * Order is reversed to match iteration order in atomic check.
cc1fec57
NK
4311 *
4312 * Only support DCN for now, and only expose one so we don't encourage
4313 * userspace to use up all the pipes.
0d579c7e 4314 */
cc1fec57
NK
4315 for (i = 0; i < dm->dc->caps.max_planes; ++i) {
4316 struct dc_plane_cap *plane = &dm->dc->caps.planes[i];
4317
8813381a
LL
4318 /* Do not create overlay if MPO disabled */
4319 if (amdgpu_dc_debug_mask & DC_DISABLE_MPO)
4320 break;
4321
cc1fec57
NK
4322 if (plane->type != DC_PLANE_TYPE_DCN_UNIVERSAL)
4323 continue;
4324
4325 if (!plane->blends_with_above || !plane->blends_with_below)
4326 continue;
4327
ea36ad34 4328 if (!plane->pixel_format_support.argb8888)
cc1fec57
NK
4329 continue;
4330
35f33086
BL
4331 if (max_overlay-- == 0)
4332 break;
4333
54087768 4334 if (initialize_plane(dm, NULL, primary_planes + i,
cc1fec57 4335 DRM_PLANE_TYPE_OVERLAY, plane)) {
0d579c7e 4336 DRM_ERROR("KMS: Failed to initialize overlay plane\n");
cd8a2ae8 4337 goto fail;
d4e13b0d
AD
4338 }
4339 }
4562236b 4340
d4e13b0d 4341 for (i = 0; i < dm->dc->caps.max_streams; i++)
f180b4bc 4342 if (amdgpu_dm_crtc_init(dm, mode_info->planes[i], i)) {
4562236b 4343 DRM_ERROR("KMS: Failed to initialize crtc\n");
cd8a2ae8 4344 goto fail;
4562236b 4345 }
4562236b 4346
81927e28 4347 /* Use Outbox interrupt */
1d789535 4348 switch (adev->ip_versions[DCE_HWIP][0]) {
c08182f2
AD
4349 case IP_VERSION(3, 0, 0):
4350 case IP_VERSION(3, 1, 2):
4351 case IP_VERSION(3, 1, 3):
e850f6b1 4352 case IP_VERSION(3, 1, 4):
b5b8ed44 4353 case IP_VERSION(3, 1, 5):
de7cc1b4 4354 case IP_VERSION(3, 1, 6):
577359ca
AP
4355 case IP_VERSION(3, 2, 0):
4356 case IP_VERSION(3, 2, 1):
c08182f2 4357 case IP_VERSION(2, 1, 0):
81927e28
JS
4358 if (register_outbox_irq_handlers(dm->adev)) {
4359 DRM_ERROR("DM: Failed to initialize IRQ\n");
4360 goto fail;
4361 }
4362 break;
4363 default:
c08182f2 4364 DRM_DEBUG_KMS("Unsupported DCN IP version for outbox: 0x%X\n",
1d789535 4365 adev->ip_versions[DCE_HWIP][0]);
81927e28 4366 }
9470620e
NK
4367
4368 /* Determine whether to enable PSR support by default. */
4369 if (!(amdgpu_dc_debug_mask & DC_DISABLE_PSR)) {
4370 switch (adev->ip_versions[DCE_HWIP][0]) {
4371 case IP_VERSION(3, 1, 2):
4372 case IP_VERSION(3, 1, 3):
e850f6b1 4373 case IP_VERSION(3, 1, 4):
b5b8ed44 4374 case IP_VERSION(3, 1, 5):
de7cc1b4 4375 case IP_VERSION(3, 1, 6):
577359ca
AP
4376 case IP_VERSION(3, 2, 0):
4377 case IP_VERSION(3, 2, 1):
9470620e
NK
4378 psr_feature_enabled = true;
4379 break;
4380 default:
4381 psr_feature_enabled = amdgpu_dc_feature_mask & DC_PSR_MASK;
4382 break;
4383 }
4384 }
81927e28 4385
4562236b
HW
4386 /* loops over all connectors on the board */
4387 for (i = 0; i < link_cnt; i++) {
89fc8d4e 4388 struct dc_link *link = NULL;
4562236b
HW
4389
4390 if (i > AMDGPU_DM_MAX_DISPLAY_INDEX) {
4391 DRM_ERROR(
4392 "KMS: Cannot support more than %d display indexes\n",
4393 AMDGPU_DM_MAX_DISPLAY_INDEX);
4394 continue;
4395 }
4396
4397 aconnector = kzalloc(sizeof(*aconnector), GFP_KERNEL);
4398 if (!aconnector)
cd8a2ae8 4399 goto fail;
4562236b
HW
4400
4401 aencoder = kzalloc(sizeof(*aencoder), GFP_KERNEL);
8440c304 4402 if (!aencoder)
cd8a2ae8 4403 goto fail;
4562236b
HW
4404
4405 if (amdgpu_dm_encoder_init(dm->ddev, aencoder, i)) {
4406 DRM_ERROR("KMS: Failed to initialize encoder\n");
cd8a2ae8 4407 goto fail;
4562236b
HW
4408 }
4409
4410 if (amdgpu_dm_connector_init(dm, aconnector, i, aencoder)) {
4411 DRM_ERROR("KMS: Failed to initialize connector\n");
cd8a2ae8 4412 goto fail;
4562236b
HW
4413 }
4414
89fc8d4e
HW
4415 link = dc_get_link_at_index(dm->dc, i);
4416
54618888 4417 if (!dc_link_detect_connection_type(link, &new_connection_type))
fbbdadf2
BL
4418 DRM_ERROR("KMS: Failed to detect connector\n");
4419
4420 if (aconnector->base.force && new_connection_type == dc_connection_none) {
4421 emulated_link_detect(link);
4422 amdgpu_dm_update_connector_after_detect(aconnector);
15c735e7
WL
4423 } else {
4424 bool ret = false;
fbbdadf2 4425
15c735e7
WL
4426 mutex_lock(&dm->dc_lock);
4427 ret = dc_link_detect(link, DETECT_REASON_BOOT);
4428 mutex_unlock(&dm->dc_lock);
4429
4430 if (ret) {
4431 amdgpu_dm_update_connector_after_detect(aconnector);
d24b77e4 4432 register_backlight_device(dm, aconnector, link);
89fc8d4e 4433
15c735e7
WL
4434 if (dm->num_of_edps)
4435 update_connector_ext_caps(aconnector);
89fc8d4e 4436
15c735e7
WL
4437 if (psr_feature_enabled)
4438 amdgpu_dm_set_psr_caps(link);
89fc8d4e 4439
15c735e7
WL
4440 /* TODO: Fix vblank control helpers to delay PSR entry to allow this when
4441 * PSR is also supported.
4442 */
4443 if (link->psr_settings.psr_feature_enabled)
4444 adev_to_drm(adev)->vblank_disable_immediate = false;
4445 }
4446 }
acc96ae0 4447 amdgpu_set_panel_orientation(&aconnector->base);
4562236b
HW
4448 }
4449
c573e240
ML
4450 /* If we didn't find a panel, notify the acpi video detection */
4451 if (dm->adev->flags & AMD_IS_APU && dm->num_of_edps == 0)
4452 acpi_video_report_nolcd();
4453
4562236b
HW
4454 /* Software is initialized. Now we can register interrupt handlers. */
4455 switch (adev->asic_type) {
55e56389
MR
4456#if defined(CONFIG_DRM_AMD_DC_SI)
4457 case CHIP_TAHITI:
4458 case CHIP_PITCAIRN:
4459 case CHIP_VERDE:
4460 case CHIP_OLAND:
4461 if (dce60_register_irq_handlers(dm->adev)) {
4462 DRM_ERROR("DM: Failed to initialize IRQ\n");
4463 goto fail;
4464 }
4465 break;
4466#endif
4562236b
HW
4467 case CHIP_BONAIRE:
4468 case CHIP_HAWAII:
cd4b356f
AD
4469 case CHIP_KAVERI:
4470 case CHIP_KABINI:
4471 case CHIP_MULLINS:
4562236b
HW
4472 case CHIP_TONGA:
4473 case CHIP_FIJI:
4474 case CHIP_CARRIZO:
4475 case CHIP_STONEY:
4476 case CHIP_POLARIS11:
4477 case CHIP_POLARIS10:
b264d345 4478 case CHIP_POLARIS12:
7737de91 4479 case CHIP_VEGAM:
2c8ad2d5 4480 case CHIP_VEGA10:
2325ff30 4481 case CHIP_VEGA12:
1fe6bf2f 4482 case CHIP_VEGA20:
4562236b
HW
4483 if (dce110_register_irq_handlers(dm->adev)) {
4484 DRM_ERROR("DM: Failed to initialize IRQ\n");
cd8a2ae8 4485 goto fail;
4562236b
HW
4486 }
4487 break;
4488 default:
1d789535 4489 switch (adev->ip_versions[DCE_HWIP][0]) {
559f591d
AD
4490 case IP_VERSION(1, 0, 0):
4491 case IP_VERSION(1, 0, 1):
c08182f2
AD
4492 case IP_VERSION(2, 0, 2):
4493 case IP_VERSION(2, 0, 3):
4494 case IP_VERSION(2, 0, 0):
4495 case IP_VERSION(2, 1, 0):
4496 case IP_VERSION(3, 0, 0):
4497 case IP_VERSION(3, 0, 2):
4498 case IP_VERSION(3, 0, 3):
4499 case IP_VERSION(3, 0, 1):
4500 case IP_VERSION(3, 1, 2):
4501 case IP_VERSION(3, 1, 3):
e850f6b1 4502 case IP_VERSION(3, 1, 4):
b5b8ed44 4503 case IP_VERSION(3, 1, 5):
de7cc1b4 4504 case IP_VERSION(3, 1, 6):
577359ca
AP
4505 case IP_VERSION(3, 2, 0):
4506 case IP_VERSION(3, 2, 1):
c08182f2
AD
4507 if (dcn10_register_irq_handlers(dm->adev)) {
4508 DRM_ERROR("DM: Failed to initialize IRQ\n");
4509 goto fail;
4510 }
4511 break;
4512 default:
2cbc6f42 4513 DRM_ERROR("Unsupported DCE IP versions: 0x%X\n",
1d789535 4514 adev->ip_versions[DCE_HWIP][0]);
2cbc6f42 4515 goto fail;
c08182f2 4516 }
2cbc6f42 4517 break;
4562236b
HW
4518 }
4519
4562236b 4520 return 0;
cd8a2ae8 4521fail:
4562236b 4522 kfree(aencoder);
4562236b 4523 kfree(aconnector);
54087768 4524
59d0f396 4525 return -EINVAL;
4562236b
HW
4526}
4527
7578ecda 4528static void amdgpu_dm_destroy_drm_device(struct amdgpu_display_manager *dm)
4562236b 4529{
eb3dc897 4530 drm_atomic_private_obj_fini(&dm->atomic_obj);
4562236b
HW
4531 return;
4532}
4533
4534/******************************************************************************
4535 * amdgpu_display_funcs functions
4536 *****************************************************************************/
4537
1f6010a9 4538/*
4562236b
HW
4539 * dm_bandwidth_update - program display watermarks
4540 *
4541 * @adev: amdgpu_device pointer
4542 *
4543 * Calculate and program the display watermarks and line buffer allocation.
4544 */
4545static void dm_bandwidth_update(struct amdgpu_device *adev)
4546{
49c07a99 4547 /* TODO: implement later */
4562236b
HW
4548}
4549
39cc5be2 4550static const struct amdgpu_display_funcs dm_display_funcs = {
4562236b
HW
4551 .bandwidth_update = dm_bandwidth_update, /* called unconditionally */
4552 .vblank_get_counter = dm_vblank_get_counter,/* called unconditionally */
7b42573b
HW
4553 .backlight_set_level = NULL, /* never called for DC */
4554 .backlight_get_level = NULL, /* never called for DC */
4562236b
HW
4555 .hpd_sense = NULL,/* called unconditionally */
4556 .hpd_set_polarity = NULL, /* called unconditionally */
4557 .hpd_get_gpio_reg = NULL, /* VBIOS parsing. DAL does it. */
4562236b
HW
4558 .page_flip_get_scanoutpos =
4559 dm_crtc_get_scanoutpos,/* called unconditionally */
4560 .add_encoder = NULL, /* VBIOS parsing. DAL does it. */
4561 .add_connector = NULL, /* VBIOS parsing. DAL does it. */
4562236b
HW
4562};
4563
4564#if defined(CONFIG_DEBUG_KERNEL_DC)
4565
3ee6b26b
AD
4566static ssize_t s3_debug_store(struct device *device,
4567 struct device_attribute *attr,
4568 const char *buf,
4569 size_t count)
4562236b
HW
4570{
4571 int ret;
4572 int s3_state;
ef1de361 4573 struct drm_device *drm_dev = dev_get_drvdata(device);
1348969a 4574 struct amdgpu_device *adev = drm_to_adev(drm_dev);
4562236b
HW
4575
4576 ret = kstrtoint(buf, 0, &s3_state);
4577
4578 if (ret == 0) {
4579 if (s3_state) {
4580 dm_resume(adev);
4a580877 4581 drm_kms_helper_hotplug_event(adev_to_drm(adev));
4562236b
HW
4582 } else
4583 dm_suspend(adev);
4584 }
4585
4586 return ret == 0 ? count : 0;
4587}
4588
4589DEVICE_ATTR_WO(s3_debug);
4590
4591#endif
4592
a7ab3451
ML
4593static int dm_init_microcode(struct amdgpu_device *adev)
4594{
4595 char *fw_name_dmub;
4596 int r;
4597
4598 switch (adev->ip_versions[DCE_HWIP][0]) {
4599 case IP_VERSION(2, 1, 0):
4600 fw_name_dmub = FIRMWARE_RENOIR_DMUB;
4601 if (ASICREV_IS_GREEN_SARDINE(adev->external_rev_id))
4602 fw_name_dmub = FIRMWARE_GREEN_SARDINE_DMUB;
4603 break;
4604 case IP_VERSION(3, 0, 0):
4605 if (adev->ip_versions[GC_HWIP][0] == IP_VERSION(10, 3, 0))
4606 fw_name_dmub = FIRMWARE_SIENNA_CICHLID_DMUB;
4607 else
4608 fw_name_dmub = FIRMWARE_NAVY_FLOUNDER_DMUB;
4609 break;
4610 case IP_VERSION(3, 0, 1):
4611 fw_name_dmub = FIRMWARE_VANGOGH_DMUB;
4612 break;
4613 case IP_VERSION(3, 0, 2):
4614 fw_name_dmub = FIRMWARE_DIMGREY_CAVEFISH_DMUB;
4615 break;
4616 case IP_VERSION(3, 0, 3):
4617 fw_name_dmub = FIRMWARE_BEIGE_GOBY_DMUB;
4618 break;
4619 case IP_VERSION(3, 1, 2):
4620 case IP_VERSION(3, 1, 3):
4621 fw_name_dmub = FIRMWARE_YELLOW_CARP_DMUB;
4622 break;
4623 case IP_VERSION(3, 1, 4):
4624 fw_name_dmub = FIRMWARE_DCN_314_DMUB;
4625 break;
4626 case IP_VERSION(3, 1, 5):
4627 fw_name_dmub = FIRMWARE_DCN_315_DMUB;
4628 break;
4629 case IP_VERSION(3, 1, 6):
4630 fw_name_dmub = FIRMWARE_DCN316_DMUB;
4631 break;
4632 case IP_VERSION(3, 2, 0):
4633 fw_name_dmub = FIRMWARE_DCN_V3_2_0_DMCUB;
4634 break;
4635 case IP_VERSION(3, 2, 1):
4636 fw_name_dmub = FIRMWARE_DCN_V3_2_1_DMCUB;
4637 break;
4638 default:
4639 /* ASIC doesn't support DMUB. */
4640 return 0;
4641 }
4642 r = amdgpu_ucode_request(adev, &adev->dm.dmub_fw, fw_name_dmub);
4643 if (r)
4644 DRM_ERROR("DMUB firmware loading failed: %d\n", r);
4645 return r;
4646}
4647
4562236b
HW
4648static int dm_early_init(void *handle)
4649{
4650 struct amdgpu_device *adev = (struct amdgpu_device *)handle;
44900af0
AD
4651 struct amdgpu_mode_info *mode_info = &adev->mode_info;
4652 struct atom_context *ctx = mode_info->atom_context;
4653 int index = GetIndexIntoMasterTable(DATA, Object_Header);
4654 u16 data_offset;
4655
4656 /* if there is no object header, skip DM */
4657 if (!amdgpu_atom_parse_data_header(ctx, index, NULL, NULL, NULL, &data_offset)) {
4658 adev->harvest_ip_mask |= AMD_HARVEST_IP_DMU_MASK;
4659 dev_info(adev->dev, "No object header, skipping DM\n");
4660 return -ENOENT;
4661 }
4562236b 4662
4562236b 4663 switch (adev->asic_type) {
55e56389
MR
4664#if defined(CONFIG_DRM_AMD_DC_SI)
4665 case CHIP_TAHITI:
4666 case CHIP_PITCAIRN:
4667 case CHIP_VERDE:
4668 adev->mode_info.num_crtc = 6;
4669 adev->mode_info.num_hpd = 6;
4670 adev->mode_info.num_dig = 6;
4671 break;
4672 case CHIP_OLAND:
4673 adev->mode_info.num_crtc = 2;
4674 adev->mode_info.num_hpd = 2;
4675 adev->mode_info.num_dig = 2;
4676 break;
4677#endif
4562236b
HW
4678 case CHIP_BONAIRE:
4679 case CHIP_HAWAII:
4680 adev->mode_info.num_crtc = 6;
4681 adev->mode_info.num_hpd = 6;
4682 adev->mode_info.num_dig = 6;
4562236b 4683 break;
cd4b356f
AD
4684 case CHIP_KAVERI:
4685 adev->mode_info.num_crtc = 4;
4686 adev->mode_info.num_hpd = 6;
4687 adev->mode_info.num_dig = 7;
cd4b356f
AD
4688 break;
4689 case CHIP_KABINI:
4690 case CHIP_MULLINS:
4691 adev->mode_info.num_crtc = 2;
4692 adev->mode_info.num_hpd = 6;
4693 adev->mode_info.num_dig = 6;
cd4b356f 4694 break;
4562236b
HW
4695 case CHIP_FIJI:
4696 case CHIP_TONGA:
4697 adev->mode_info.num_crtc = 6;
4698 adev->mode_info.num_hpd = 6;
4699 adev->mode_info.num_dig = 7;
4562236b
HW
4700 break;
4701 case CHIP_CARRIZO:
4702 adev->mode_info.num_crtc = 3;
4703 adev->mode_info.num_hpd = 6;
4704 adev->mode_info.num_dig = 9;
4562236b
HW
4705 break;
4706 case CHIP_STONEY:
4707 adev->mode_info.num_crtc = 2;
4708 adev->mode_info.num_hpd = 6;
4709 adev->mode_info.num_dig = 9;
4562236b
HW
4710 break;
4711 case CHIP_POLARIS11:
b264d345 4712 case CHIP_POLARIS12:
4562236b
HW
4713 adev->mode_info.num_crtc = 5;
4714 adev->mode_info.num_hpd = 5;
4715 adev->mode_info.num_dig = 5;
4562236b
HW
4716 break;
4717 case CHIP_POLARIS10:
7737de91 4718 case CHIP_VEGAM:
4562236b
HW
4719 adev->mode_info.num_crtc = 6;
4720 adev->mode_info.num_hpd = 6;
4721 adev->mode_info.num_dig = 6;
4562236b 4722 break;
2c8ad2d5 4723 case CHIP_VEGA10:
2325ff30 4724 case CHIP_VEGA12:
1fe6bf2f 4725 case CHIP_VEGA20:
2c8ad2d5
AD
4726 adev->mode_info.num_crtc = 6;
4727 adev->mode_info.num_hpd = 6;
4728 adev->mode_info.num_dig = 6;
4729 break;
4562236b 4730 default:
cae5c1ab 4731
1d789535 4732 switch (adev->ip_versions[DCE_HWIP][0]) {
c08182f2
AD
4733 case IP_VERSION(2, 0, 2):
4734 case IP_VERSION(3, 0, 0):
4735 adev->mode_info.num_crtc = 6;
4736 adev->mode_info.num_hpd = 6;
4737 adev->mode_info.num_dig = 6;
4738 break;
4739 case IP_VERSION(2, 0, 0):
4740 case IP_VERSION(3, 0, 2):
4741 adev->mode_info.num_crtc = 5;
4742 adev->mode_info.num_hpd = 5;
4743 adev->mode_info.num_dig = 5;
4744 break;
4745 case IP_VERSION(2, 0, 3):
4746 case IP_VERSION(3, 0, 3):
4747 adev->mode_info.num_crtc = 2;
4748 adev->mode_info.num_hpd = 2;
4749 adev->mode_info.num_dig = 2;
4750 break;
559f591d
AD
4751 case IP_VERSION(1, 0, 0):
4752 case IP_VERSION(1, 0, 1):
c08182f2
AD
4753 case IP_VERSION(3, 0, 1):
4754 case IP_VERSION(2, 1, 0):
4755 case IP_VERSION(3, 1, 2):
4756 case IP_VERSION(3, 1, 3):
e850f6b1 4757 case IP_VERSION(3, 1, 4):
b5b8ed44 4758 case IP_VERSION(3, 1, 5):
de7cc1b4 4759 case IP_VERSION(3, 1, 6):
577359ca
AP
4760 case IP_VERSION(3, 2, 0):
4761 case IP_VERSION(3, 2, 1):
c08182f2
AD
4762 adev->mode_info.num_crtc = 4;
4763 adev->mode_info.num_hpd = 4;
4764 adev->mode_info.num_dig = 4;
4765 break;
4766 default:
2cbc6f42 4767 DRM_ERROR("Unsupported DCE IP versions: 0x%x\n",
1d789535 4768 adev->ip_versions[DCE_HWIP][0]);
2cbc6f42 4769 return -EINVAL;
c08182f2 4770 }
2cbc6f42 4771 break;
4562236b
HW
4772 }
4773
39cc5be2
AD
4774 if (adev->mode_info.funcs == NULL)
4775 adev->mode_info.funcs = &dm_display_funcs;
4776
1f6010a9
DF
4777 /*
4778 * Note: Do NOT change adev->audio_endpt_rreg and
4562236b 4779 * adev->audio_endpt_wreg because they are initialised in
1f6010a9
DF
4780 * amdgpu_device_init()
4781 */
4562236b
HW
4782#if defined(CONFIG_DEBUG_KERNEL_DC)
4783 device_create_file(
4a580877 4784 adev_to_drm(adev)->dev,
4562236b
HW
4785 &dev_attr_s3_debug);
4786#endif
d09ef243 4787 adev->dc_enabled = true;
4562236b 4788
a7ab3451 4789 return dm_init_microcode(adev);
4562236b
HW
4790}
4791
e7b07cee
HW
4792static bool modereset_required(struct drm_crtc_state *crtc_state)
4793{
2afda735 4794 return !crtc_state->active && drm_atomic_crtc_needs_modeset(crtc_state);
e7b07cee
HW
4795}
4796
7578ecda 4797static void amdgpu_dm_encoder_destroy(struct drm_encoder *encoder)
e7b07cee
HW
4798{
4799 drm_encoder_cleanup(encoder);
4800 kfree(encoder);
4801}
4802
4803static const struct drm_encoder_funcs amdgpu_dm_encoder_funcs = {
4804 .destroy = amdgpu_dm_encoder_destroy,
4805};
4806
5d945cbc
RS
4807static int
4808fill_plane_color_attributes(const struct drm_plane_state *plane_state,
4809 const enum surface_pixel_format format,
4810 enum dc_color_space *color_space)
6300b3bd 4811{
5d945cbc 4812 bool full_range;
6300b3bd 4813
5d945cbc
RS
4814 *color_space = COLOR_SPACE_SRGB;
4815
4816 /* DRM color properties only affect non-RGB formats. */
4817 if (format < SURFACE_PIXEL_FORMAT_VIDEO_BEGIN)
4818 return 0;
4819
4820 full_range = (plane_state->color_range == DRM_COLOR_YCBCR_FULL_RANGE);
4821
4822 switch (plane_state->color_encoding) {
4823 case DRM_COLOR_YCBCR_BT601:
4824 if (full_range)
4825 *color_space = COLOR_SPACE_YCBCR601;
4826 else
4827 *color_space = COLOR_SPACE_YCBCR601_LIMITED;
6300b3bd
MK
4828 break;
4829
5d945cbc
RS
4830 case DRM_COLOR_YCBCR_BT709:
4831 if (full_range)
4832 *color_space = COLOR_SPACE_YCBCR709;
4833 else
4834 *color_space = COLOR_SPACE_YCBCR709_LIMITED;
6300b3bd
MK
4835 break;
4836
5d945cbc
RS
4837 case DRM_COLOR_YCBCR_BT2020:
4838 if (full_range)
4839 *color_space = COLOR_SPACE_2020_YCBCR;
4840 else
4841 return -EINVAL;
6300b3bd 4842 break;
6300b3bd 4843
5d945cbc
RS
4844 default:
4845 return -EINVAL;
4846 }
6300b3bd 4847
5d945cbc 4848 return 0;
6300b3bd
MK
4849}
4850
5d945cbc
RS
4851static int
4852fill_dc_plane_info_and_addr(struct amdgpu_device *adev,
4853 const struct drm_plane_state *plane_state,
ae67558b 4854 const u64 tiling_flags,
5d945cbc
RS
4855 struct dc_plane_info *plane_info,
4856 struct dc_plane_address *address,
4857 bool tmz_surface,
4858 bool force_disable_dcc)
e7b07cee 4859{
5d945cbc
RS
4860 const struct drm_framebuffer *fb = plane_state->fb;
4861 const struct amdgpu_framebuffer *afb =
4862 to_amdgpu_framebuffer(plane_state->fb);
4863 int ret;
e7b07cee 4864
5d945cbc 4865 memset(plane_info, 0, sizeof(*plane_info));
e7b07cee 4866
5d945cbc
RS
4867 switch (fb->format->format) {
4868 case DRM_FORMAT_C8:
4869 plane_info->format =
4870 SURFACE_PIXEL_FORMAT_GRPH_PALETA_256_COLORS;
4871 break;
4872 case DRM_FORMAT_RGB565:
4873 plane_info->format = SURFACE_PIXEL_FORMAT_GRPH_RGB565;
4874 break;
4875 case DRM_FORMAT_XRGB8888:
4876 case DRM_FORMAT_ARGB8888:
4877 plane_info->format = SURFACE_PIXEL_FORMAT_GRPH_ARGB8888;
4878 break;
4879 case DRM_FORMAT_XRGB2101010:
4880 case DRM_FORMAT_ARGB2101010:
4881 plane_info->format = SURFACE_PIXEL_FORMAT_GRPH_ARGB2101010;
4882 break;
4883 case DRM_FORMAT_XBGR2101010:
4884 case DRM_FORMAT_ABGR2101010:
4885 plane_info->format = SURFACE_PIXEL_FORMAT_GRPH_ABGR2101010;
4886 break;
4887 case DRM_FORMAT_XBGR8888:
4888 case DRM_FORMAT_ABGR8888:
4889 plane_info->format = SURFACE_PIXEL_FORMAT_GRPH_ABGR8888;
4890 break;
4891 case DRM_FORMAT_NV21:
4892 plane_info->format = SURFACE_PIXEL_FORMAT_VIDEO_420_YCbCr;
4893 break;
4894 case DRM_FORMAT_NV12:
4895 plane_info->format = SURFACE_PIXEL_FORMAT_VIDEO_420_YCrCb;
4896 break;
4897 case DRM_FORMAT_P010:
4898 plane_info->format = SURFACE_PIXEL_FORMAT_VIDEO_420_10bpc_YCrCb;
4899 break;
4900 case DRM_FORMAT_XRGB16161616F:
4901 case DRM_FORMAT_ARGB16161616F:
4902 plane_info->format = SURFACE_PIXEL_FORMAT_GRPH_ARGB16161616F;
4903 break;
4904 case DRM_FORMAT_XBGR16161616F:
4905 case DRM_FORMAT_ABGR16161616F:
4906 plane_info->format = SURFACE_PIXEL_FORMAT_GRPH_ABGR16161616F;
4907 break;
4908 case DRM_FORMAT_XRGB16161616:
4909 case DRM_FORMAT_ARGB16161616:
4910 plane_info->format = SURFACE_PIXEL_FORMAT_GRPH_ARGB16161616;
4911 break;
4912 case DRM_FORMAT_XBGR16161616:
4913 case DRM_FORMAT_ABGR16161616:
4914 plane_info->format = SURFACE_PIXEL_FORMAT_GRPH_ABGR16161616;
4915 break;
4916 default:
4917 DRM_ERROR(
4918 "Unsupported screen format %p4cc\n",
4919 &fb->format->format);
d89f6048 4920 return -EINVAL;
5d945cbc 4921 }
d89f6048 4922
5d945cbc
RS
4923 switch (plane_state->rotation & DRM_MODE_ROTATE_MASK) {
4924 case DRM_MODE_ROTATE_0:
4925 plane_info->rotation = ROTATION_ANGLE_0;
4926 break;
4927 case DRM_MODE_ROTATE_90:
4928 plane_info->rotation = ROTATION_ANGLE_90;
4929 break;
4930 case DRM_MODE_ROTATE_180:
4931 plane_info->rotation = ROTATION_ANGLE_180;
4932 break;
4933 case DRM_MODE_ROTATE_270:
4934 plane_info->rotation = ROTATION_ANGLE_270;
4935 break;
4936 default:
4937 plane_info->rotation = ROTATION_ANGLE_0;
4938 break;
4939 }
695af5f9 4940
695af5f9 4941
5d945cbc
RS
4942 plane_info->visible = true;
4943 plane_info->stereo_format = PLANE_STEREO_FORMAT_NONE;
e7b07cee 4944
22c42b0e 4945 plane_info->layer_index = plane_state->normalized_zpos;
e7b07cee 4946
5d945cbc
RS
4947 ret = fill_plane_color_attributes(plane_state, plane_info->format,
4948 &plane_info->color_space);
4949 if (ret)
4950 return ret;
e7b07cee 4951
5d945cbc
RS
4952 ret = fill_plane_buffer_attributes(adev, afb, plane_info->format,
4953 plane_info->rotation, tiling_flags,
4954 &plane_info->tiling_info,
4955 &plane_info->plane_size,
4956 &plane_info->dcc, address,
4957 tmz_surface, force_disable_dcc);
4958 if (ret)
4959 return ret;
e7b07cee 4960
5d945cbc
RS
4961 fill_blending_from_plane_state(
4962 plane_state, &plane_info->per_pixel_alpha, &plane_info->pre_multiplied_alpha,
4963 &plane_info->global_alpha, &plane_info->global_alpha_value);
e7b07cee 4964
5d945cbc
RS
4965 return 0;
4966}
e7b07cee 4967
5d945cbc
RS
4968static int fill_dc_plane_attributes(struct amdgpu_device *adev,
4969 struct dc_plane_state *dc_plane_state,
4970 struct drm_plane_state *plane_state,
4971 struct drm_crtc_state *crtc_state)
4972{
4973 struct dm_crtc_state *dm_crtc_state = to_dm_crtc_state(crtc_state);
4974 struct amdgpu_framebuffer *afb = (struct amdgpu_framebuffer *)plane_state->fb;
4975 struct dc_scaling_info scaling_info;
4976 struct dc_plane_info plane_info;
4977 int ret;
4978 bool force_disable_dcc = false;
6300b3bd 4979
5d945cbc
RS
4980 ret = fill_dc_scaling_info(adev, plane_state, &scaling_info);
4981 if (ret)
4982 return ret;
e7b07cee 4983
5d945cbc
RS
4984 dc_plane_state->src_rect = scaling_info.src_rect;
4985 dc_plane_state->dst_rect = scaling_info.dst_rect;
4986 dc_plane_state->clip_rect = scaling_info.clip_rect;
4987 dc_plane_state->scaling_quality = scaling_info.scaling_quality;
6491f0c0 4988
5d945cbc
RS
4989 force_disable_dcc = adev->asic_type == CHIP_RAVEN && adev->in_suspend;
4990 ret = fill_dc_plane_info_and_addr(adev, plane_state,
4991 afb->tiling_flags,
4992 &plane_info,
4993 &dc_plane_state->address,
4994 afb->tmz_surface,
4995 force_disable_dcc);
4996 if (ret)
4997 return ret;
6491f0c0 4998
5d945cbc
RS
4999 dc_plane_state->format = plane_info.format;
5000 dc_plane_state->color_space = plane_info.color_space;
5001 dc_plane_state->format = plane_info.format;
5002 dc_plane_state->plane_size = plane_info.plane_size;
5003 dc_plane_state->rotation = plane_info.rotation;
5004 dc_plane_state->horizontal_mirror = plane_info.horizontal_mirror;
5005 dc_plane_state->stereo_format = plane_info.stereo_format;
5006 dc_plane_state->tiling_info = plane_info.tiling_info;
5007 dc_plane_state->visible = plane_info.visible;
5008 dc_plane_state->per_pixel_alpha = plane_info.per_pixel_alpha;
5009 dc_plane_state->pre_multiplied_alpha = plane_info.pre_multiplied_alpha;
5010 dc_plane_state->global_alpha = plane_info.global_alpha;
5011 dc_plane_state->global_alpha_value = plane_info.global_alpha_value;
5012 dc_plane_state->dcc = plane_info.dcc;
22c42b0e 5013 dc_plane_state->layer_index = plane_info.layer_index;
5d945cbc 5014 dc_plane_state->flip_int_enabled = true;
6491f0c0 5015
695af5f9 5016 /*
5d945cbc
RS
5017 * Always set input transfer function, since plane state is refreshed
5018 * every time.
695af5f9 5019 */
5d945cbc
RS
5020 ret = amdgpu_dm_update_plane_color_mgmt(dm_crtc_state, dc_plane_state);
5021 if (ret)
5022 return ret;
e7b07cee 5023
695af5f9 5024 return 0;
4562236b 5025}
695af5f9 5026
30ebe415
HM
5027static inline void fill_dc_dirty_rect(struct drm_plane *plane,
5028 struct rect *dirty_rect, int32_t x,
ae67558b 5029 s32 y, s32 width, s32 height,
30ebe415
HM
5030 int *i, bool ffu)
5031{
5032 if (*i > DC_MAX_DIRTY_RECTS)
5033 return;
5034
5035 if (*i == DC_MAX_DIRTY_RECTS)
5036 goto out;
5037
5038 dirty_rect->x = x;
5039 dirty_rect->y = y;
5040 dirty_rect->width = width;
5041 dirty_rect->height = height;
5042
5043 if (ffu)
5044 drm_dbg(plane->dev,
5045 "[PLANE:%d] PSR FFU dirty rect size (%d, %d)\n",
5046 plane->base.id, width, height);
5047 else
5048 drm_dbg(plane->dev,
5049 "[PLANE:%d] PSR SU dirty rect at (%d, %d) size (%d, %d)",
5050 plane->base.id, x, y, width, height);
5051
5052out:
5053 (*i)++;
5054}
5055
5d945cbc
RS
5056/**
5057 * fill_dc_dirty_rects() - Fill DC dirty regions for PSR selective updates
5058 *
5059 * @plane: DRM plane containing dirty regions that need to be flushed to the eDP
5060 * remote fb
5061 * @old_plane_state: Old state of @plane
5062 * @new_plane_state: New state of @plane
5063 * @crtc_state: New state of CRTC connected to the @plane
5064 * @flip_addrs: DC flip tracking struct, which also tracts dirty rects
d6ed6d0d 5065 * @dirty_regions_changed: dirty regions changed
5d945cbc
RS
5066 *
5067 * For PSR SU, DC informs the DMUB uController of dirty rectangle regions
5068 * (referred to as "damage clips" in DRM nomenclature) that require updating on
5069 * the eDP remote buffer. The responsibility of specifying the dirty regions is
5070 * amdgpu_dm's.
5071 *
5072 * A damage-aware DRM client should fill the FB_DAMAGE_CLIPS property on the
5073 * plane with regions that require flushing to the eDP remote buffer. In
5074 * addition, certain use cases - such as cursor and multi-plane overlay (MPO) -
5075 * implicitly provide damage clips without any client support via the plane
5076 * bounds.
5d945cbc
RS
5077 */
5078static void fill_dc_dirty_rects(struct drm_plane *plane,
5079 struct drm_plane_state *old_plane_state,
5080 struct drm_plane_state *new_plane_state,
5081 struct drm_crtc_state *crtc_state,
d6ed6d0d
TC
5082 struct dc_flip_addrs *flip_addrs,
5083 bool *dirty_regions_changed)
5d945cbc
RS
5084{
5085 struct dm_crtc_state *dm_crtc_state = to_dm_crtc_state(crtc_state);
5086 struct rect *dirty_rects = flip_addrs->dirty_rects;
ae67558b 5087 u32 num_clips;
30ebe415 5088 struct drm_mode_rect *clips;
5d945cbc
RS
5089 bool bb_changed;
5090 bool fb_changed;
ae67558b 5091 u32 i = 0;
d6ed6d0d 5092 *dirty_regions_changed = false;
e7b07cee 5093
7cc191ee
LL
5094 /*
5095 * Cursor plane has it's own dirty rect update interface. See
5096 * dcn10_dmub_update_cursor_data and dmub_cmd_update_cursor_info_data
5097 */
5098 if (plane->type == DRM_PLANE_TYPE_CURSOR)
5099 return;
5100
30ebe415
HM
5101 num_clips = drm_plane_get_damage_clips_count(new_plane_state);
5102 clips = drm_plane_get_damage_clips(new_plane_state);
5103
7cc191ee 5104 if (!dm_crtc_state->mpo_requested) {
30ebe415
HM
5105 if (!num_clips || num_clips > DC_MAX_DIRTY_RECTS)
5106 goto ffu;
5107
5108 for (; flip_addrs->dirty_rect_count < num_clips; clips++)
5109 fill_dc_dirty_rect(new_plane_state->plane,
5110 &dirty_rects[i], clips->x1,
5111 clips->y1, clips->x2 - clips->x1,
5112 clips->y2 - clips->y1,
5113 &flip_addrs->dirty_rect_count,
5114 false);
7cc191ee
LL
5115 return;
5116 }
5117
5118 /*
5119 * MPO is requested. Add entire plane bounding box to dirty rects if
5120 * flipped to or damaged.
5121 *
5122 * If plane is moved or resized, also add old bounding box to dirty
5123 * rects.
5124 */
7cc191ee
LL
5125 fb_changed = old_plane_state->fb->base.id !=
5126 new_plane_state->fb->base.id;
5127 bb_changed = (old_plane_state->crtc_x != new_plane_state->crtc_x ||
5128 old_plane_state->crtc_y != new_plane_state->crtc_y ||
5129 old_plane_state->crtc_w != new_plane_state->crtc_w ||
5130 old_plane_state->crtc_h != new_plane_state->crtc_h);
5131
30ebe415
HM
5132 drm_dbg(plane->dev,
5133 "[PLANE:%d] PSR bb_changed:%d fb_changed:%d num_clips:%d\n",
5134 new_plane_state->plane->base.id,
5135 bb_changed, fb_changed, num_clips);
7cc191ee 5136
d6ed6d0d
TC
5137 *dirty_regions_changed = bb_changed;
5138
7cc191ee 5139 if (bb_changed) {
30ebe415
HM
5140 fill_dc_dirty_rect(new_plane_state->plane, &dirty_rects[i],
5141 new_plane_state->crtc_x,
5142 new_plane_state->crtc_y,
5143 new_plane_state->crtc_w,
5144 new_plane_state->crtc_h, &i, false);
5145
5146 /* Add old plane bounding-box if plane is moved or resized */
5147 fill_dc_dirty_rect(new_plane_state->plane, &dirty_rects[i],
5148 old_plane_state->crtc_x,
5149 old_plane_state->crtc_y,
5150 old_plane_state->crtc_w,
5151 old_plane_state->crtc_h, &i, false);
5152 }
5153
5154 if (num_clips) {
5155 for (; i < num_clips; clips++)
5156 fill_dc_dirty_rect(new_plane_state->plane,
5157 &dirty_rects[i], clips->x1,
5158 clips->y1, clips->x2 - clips->x1,
5159 clips->y2 - clips->y1, &i, false);
5160 } else if (fb_changed && !bb_changed) {
5161 fill_dc_dirty_rect(new_plane_state->plane, &dirty_rects[i],
5162 new_plane_state->crtc_x,
5163 new_plane_state->crtc_y,
5164 new_plane_state->crtc_w,
5165 new_plane_state->crtc_h, &i, false);
5166 }
5167
5168 if (i > DC_MAX_DIRTY_RECTS)
5169 goto ffu;
7cc191ee
LL
5170
5171 flip_addrs->dirty_rect_count = i;
30ebe415
HM
5172 return;
5173
5174ffu:
5175 fill_dc_dirty_rect(new_plane_state->plane, &dirty_rects[0], 0, 0,
5176 dm_crtc_state->base.mode.crtc_hdisplay,
5177 dm_crtc_state->base.mode.crtc_vdisplay,
5178 &flip_addrs->dirty_rect_count, true);
7cc191ee
LL
5179}
5180
3ee6b26b
AD
5181static void update_stream_scaling_settings(const struct drm_display_mode *mode,
5182 const struct dm_connector_state *dm_state,
5183 struct dc_stream_state *stream)
e7b07cee
HW
5184{
5185 enum amdgpu_rmx_type rmx_type;
5186
5187 struct rect src = { 0 }; /* viewport in composition space*/
5188 struct rect dst = { 0 }; /* stream addressable area */
5189
5190 /* no mode. nothing to be done */
5191 if (!mode)
5192 return;
5193
5194 /* Full screen scaling by default */
5195 src.width = mode->hdisplay;
5196 src.height = mode->vdisplay;
5197 dst.width = stream->timing.h_addressable;
5198 dst.height = stream->timing.v_addressable;
5199
f4791779
HW
5200 if (dm_state) {
5201 rmx_type = dm_state->scaling;
5202 if (rmx_type == RMX_ASPECT || rmx_type == RMX_OFF) {
5203 if (src.width * dst.height <
5204 src.height * dst.width) {
5205 /* height needs less upscaling/more downscaling */
5206 dst.width = src.width *
5207 dst.height / src.height;
5208 } else {
5209 /* width needs less upscaling/more downscaling */
5210 dst.height = src.height *
5211 dst.width / src.width;
5212 }
5213 } else if (rmx_type == RMX_CENTER) {
5214 dst = src;
e7b07cee 5215 }
e7b07cee 5216
f4791779
HW
5217 dst.x = (stream->timing.h_addressable - dst.width) / 2;
5218 dst.y = (stream->timing.v_addressable - dst.height) / 2;
e7b07cee 5219
f4791779
HW
5220 if (dm_state->underscan_enable) {
5221 dst.x += dm_state->underscan_hborder / 2;
5222 dst.y += dm_state->underscan_vborder / 2;
5223 dst.width -= dm_state->underscan_hborder;
5224 dst.height -= dm_state->underscan_vborder;
5225 }
e7b07cee
HW
5226 }
5227
5228 stream->src = src;
5229 stream->dst = dst;
5230
4711c033
LT
5231 DRM_DEBUG_KMS("Destination Rectangle x:%d y:%d width:%d height:%d\n",
5232 dst.x, dst.y, dst.width, dst.height);
e7b07cee
HW
5233
5234}
5235
3ee6b26b 5236static enum dc_color_depth
42ba01fc 5237convert_color_depth_from_display_info(const struct drm_connector *connector,
cbd14ae7 5238 bool is_y420, int requested_bpc)
e7b07cee 5239{
ae67558b 5240 u8 bpc;
01c22997 5241
1bc22f20
SW
5242 if (is_y420) {
5243 bpc = 8;
5244
5245 /* Cap display bpc based on HDMI 2.0 HF-VSDB */
5246 if (connector->display_info.hdmi.y420_dc_modes & DRM_EDID_YCBCR420_DC_48)
5247 bpc = 16;
5248 else if (connector->display_info.hdmi.y420_dc_modes & DRM_EDID_YCBCR420_DC_36)
5249 bpc = 12;
5250 else if (connector->display_info.hdmi.y420_dc_modes & DRM_EDID_YCBCR420_DC_30)
5251 bpc = 10;
5252 } else {
5253 bpc = (uint8_t)connector->display_info.bpc;
5254 /* Assume 8 bpc by default if no bpc is specified. */
5255 bpc = bpc ? bpc : 8;
5256 }
e7b07cee 5257
cbd14ae7 5258 if (requested_bpc > 0) {
01c22997
NK
5259 /*
5260 * Cap display bpc based on the user requested value.
5261 *
5262 * The value for state->max_bpc may not correctly updated
5263 * depending on when the connector gets added to the state
5264 * or if this was called outside of atomic check, so it
5265 * can't be used directly.
5266 */
cbd14ae7 5267 bpc = min_t(u8, bpc, requested_bpc);
01c22997 5268
1825fd34
NK
5269 /* Round down to the nearest even number. */
5270 bpc = bpc - (bpc & 1);
5271 }
07e3a1cf 5272
e7b07cee
HW
5273 switch (bpc) {
5274 case 0:
1f6010a9
DF
5275 /*
5276 * Temporary Work around, DRM doesn't parse color depth for
e7b07cee
HW
5277 * EDID revision before 1.4
5278 * TODO: Fix edid parsing
5279 */
5280 return COLOR_DEPTH_888;
5281 case 6:
5282 return COLOR_DEPTH_666;
5283 case 8:
5284 return COLOR_DEPTH_888;
5285 case 10:
5286 return COLOR_DEPTH_101010;
5287 case 12:
5288 return COLOR_DEPTH_121212;
5289 case 14:
5290 return COLOR_DEPTH_141414;
5291 case 16:
5292 return COLOR_DEPTH_161616;
5293 default:
5294 return COLOR_DEPTH_UNDEFINED;
5295 }
5296}
5297
3ee6b26b
AD
5298static enum dc_aspect_ratio
5299get_aspect_ratio(const struct drm_display_mode *mode_in)
e7b07cee 5300{
e11d4147
LSL
5301 /* 1-1 mapping, since both enums follow the HDMI spec. */
5302 return (enum dc_aspect_ratio) mode_in->picture_aspect_ratio;
e7b07cee
HW
5303}
5304
3ee6b26b
AD
5305static enum dc_color_space
5306get_output_color_space(const struct dc_crtc_timing *dc_crtc_timing)
e7b07cee
HW
5307{
5308 enum dc_color_space color_space = COLOR_SPACE_SRGB;
5309
5310 switch (dc_crtc_timing->pixel_encoding) {
5311 case PIXEL_ENCODING_YCBCR422:
5312 case PIXEL_ENCODING_YCBCR444:
5313 case PIXEL_ENCODING_YCBCR420:
5314 {
5315 /*
5316 * 27030khz is the separation point between HDTV and SDTV
5317 * according to HDMI spec, we use YCbCr709 and YCbCr601
5318 * respectively
5319 */
380604e2 5320 if (dc_crtc_timing->pix_clk_100hz > 270300) {
e7b07cee
HW
5321 if (dc_crtc_timing->flags.Y_ONLY)
5322 color_space =
5323 COLOR_SPACE_YCBCR709_LIMITED;
5324 else
5325 color_space = COLOR_SPACE_YCBCR709;
5326 } else {
5327 if (dc_crtc_timing->flags.Y_ONLY)
5328 color_space =
5329 COLOR_SPACE_YCBCR601_LIMITED;
5330 else
5331 color_space = COLOR_SPACE_YCBCR601;
5332 }
5333
5334 }
5335 break;
5336 case PIXEL_ENCODING_RGB:
5337 color_space = COLOR_SPACE_SRGB;
5338 break;
5339
5340 default:
5341 WARN_ON(1);
5342 break;
5343 }
5344
5345 return color_space;
5346}
5347
ea117312
TA
5348static bool adjust_colour_depth_from_display_info(
5349 struct dc_crtc_timing *timing_out,
5350 const struct drm_display_info *info)
400443e8 5351{
ea117312 5352 enum dc_color_depth depth = timing_out->display_color_depth;
400443e8 5353 int normalized_clk;
400443e8 5354 do {
380604e2 5355 normalized_clk = timing_out->pix_clk_100hz / 10;
400443e8
ML
5356 /* YCbCr 4:2:0 requires additional adjustment of 1/2 */
5357 if (timing_out->pixel_encoding == PIXEL_ENCODING_YCBCR420)
5358 normalized_clk /= 2;
5359 /* Adjusting pix clock following on HDMI spec based on colour depth */
ea117312
TA
5360 switch (depth) {
5361 case COLOR_DEPTH_888:
5362 break;
400443e8
ML
5363 case COLOR_DEPTH_101010:
5364 normalized_clk = (normalized_clk * 30) / 24;
5365 break;
5366 case COLOR_DEPTH_121212:
5367 normalized_clk = (normalized_clk * 36) / 24;
5368 break;
5369 case COLOR_DEPTH_161616:
5370 normalized_clk = (normalized_clk * 48) / 24;
5371 break;
5372 default:
ea117312
TA
5373 /* The above depths are the only ones valid for HDMI. */
5374 return false;
400443e8 5375 }
ea117312
TA
5376 if (normalized_clk <= info->max_tmds_clock) {
5377 timing_out->display_color_depth = depth;
5378 return true;
5379 }
5380 } while (--depth > COLOR_DEPTH_666);
5381 return false;
400443e8 5382}
e7b07cee 5383
42ba01fc
NK
5384static void fill_stream_properties_from_drm_display_mode(
5385 struct dc_stream_state *stream,
5386 const struct drm_display_mode *mode_in,
5387 const struct drm_connector *connector,
5388 const struct drm_connector_state *connector_state,
cbd14ae7
SW
5389 const struct dc_stream_state *old_stream,
5390 int requested_bpc)
e7b07cee
HW
5391{
5392 struct dc_crtc_timing *timing_out = &stream->timing;
fe61a2f1 5393 const struct drm_display_info *info = &connector->display_info;
d4252eee 5394 struct amdgpu_dm_connector *aconnector = to_amdgpu_dm_connector(connector);
1cb1d477
WL
5395 struct hdmi_vendor_infoframe hv_frame;
5396 struct hdmi_avi_infoframe avi_frame;
e7b07cee 5397
acf83f86
WL
5398 memset(&hv_frame, 0, sizeof(hv_frame));
5399 memset(&avi_frame, 0, sizeof(avi_frame));
5400
e7b07cee
HW
5401 timing_out->h_border_left = 0;
5402 timing_out->h_border_right = 0;
5403 timing_out->v_border_top = 0;
5404 timing_out->v_border_bottom = 0;
5405 /* TODO: un-hardcode */
fe61a2f1 5406 if (drm_mode_is_420_only(info, mode_in)
ceb3dbb4 5407 && stream->signal == SIGNAL_TYPE_HDMI_TYPE_A)
fe61a2f1 5408 timing_out->pixel_encoding = PIXEL_ENCODING_YCBCR420;
d4252eee
SW
5409 else if (drm_mode_is_420_also(info, mode_in)
5410 && aconnector->force_yuv420_output)
5411 timing_out->pixel_encoding = PIXEL_ENCODING_YCBCR420;
c03d0b52 5412 else if ((connector->display_info.color_formats & DRM_COLOR_FORMAT_YCBCR444)
ceb3dbb4 5413 && stream->signal == SIGNAL_TYPE_HDMI_TYPE_A)
e7b07cee
HW
5414 timing_out->pixel_encoding = PIXEL_ENCODING_YCBCR444;
5415 else
5416 timing_out->pixel_encoding = PIXEL_ENCODING_RGB;
5417
5418 timing_out->timing_3d_format = TIMING_3D_FORMAT_NONE;
5419 timing_out->display_color_depth = convert_color_depth_from_display_info(
cbd14ae7
SW
5420 connector,
5421 (timing_out->pixel_encoding == PIXEL_ENCODING_YCBCR420),
5422 requested_bpc);
e7b07cee
HW
5423 timing_out->scan_type = SCANNING_TYPE_NODATA;
5424 timing_out->hdmi_vic = 0;
b333730d 5425
5d945cbc 5426 if (old_stream) {
b333730d
BL
5427 timing_out->vic = old_stream->timing.vic;
5428 timing_out->flags.HSYNC_POSITIVE_POLARITY = old_stream->timing.flags.HSYNC_POSITIVE_POLARITY;
5429 timing_out->flags.VSYNC_POSITIVE_POLARITY = old_stream->timing.flags.VSYNC_POSITIVE_POLARITY;
5430 } else {
5431 timing_out->vic = drm_match_cea_mode(mode_in);
5432 if (mode_in->flags & DRM_MODE_FLAG_PHSYNC)
5433 timing_out->flags.HSYNC_POSITIVE_POLARITY = 1;
5434 if (mode_in->flags & DRM_MODE_FLAG_PVSYNC)
5435 timing_out->flags.VSYNC_POSITIVE_POLARITY = 1;
5436 }
e7b07cee 5437
1cb1d477
WL
5438 if (stream->signal == SIGNAL_TYPE_HDMI_TYPE_A) {
5439 drm_hdmi_avi_infoframe_from_display_mode(&avi_frame, (struct drm_connector *)connector, mode_in);
5440 timing_out->vic = avi_frame.video_code;
5441 drm_hdmi_vendor_infoframe_from_display_mode(&hv_frame, (struct drm_connector *)connector, mode_in);
5442 timing_out->hdmi_vic = hv_frame.vic;
5443 }
5444
fe8858bb
NC
5445 if (is_freesync_video_mode(mode_in, aconnector)) {
5446 timing_out->h_addressable = mode_in->hdisplay;
5447 timing_out->h_total = mode_in->htotal;
5448 timing_out->h_sync_width = mode_in->hsync_end - mode_in->hsync_start;
5449 timing_out->h_front_porch = mode_in->hsync_start - mode_in->hdisplay;
5450 timing_out->v_total = mode_in->vtotal;
5451 timing_out->v_addressable = mode_in->vdisplay;
5452 timing_out->v_front_porch = mode_in->vsync_start - mode_in->vdisplay;
5453 timing_out->v_sync_width = mode_in->vsync_end - mode_in->vsync_start;
5454 timing_out->pix_clk_100hz = mode_in->clock * 10;
5455 } else {
5456 timing_out->h_addressable = mode_in->crtc_hdisplay;
5457 timing_out->h_total = mode_in->crtc_htotal;
5458 timing_out->h_sync_width = mode_in->crtc_hsync_end - mode_in->crtc_hsync_start;
5459 timing_out->h_front_porch = mode_in->crtc_hsync_start - mode_in->crtc_hdisplay;
5460 timing_out->v_total = mode_in->crtc_vtotal;
5461 timing_out->v_addressable = mode_in->crtc_vdisplay;
5462 timing_out->v_front_porch = mode_in->crtc_vsync_start - mode_in->crtc_vdisplay;
5463 timing_out->v_sync_width = mode_in->crtc_vsync_end - mode_in->crtc_vsync_start;
5464 timing_out->pix_clk_100hz = mode_in->crtc_clock * 10;
5465 }
a85ba005 5466
e7b07cee 5467 timing_out->aspect_ratio = get_aspect_ratio(mode_in);
e7b07cee 5468
e43a432c
AK
5469 stream->out_transfer_func->type = TF_TYPE_PREDEFINED;
5470 stream->out_transfer_func->tf = TRANSFER_FUNCTION_SRGB;
ea117312
TA
5471 if (stream->signal == SIGNAL_TYPE_HDMI_TYPE_A) {
5472 if (!adjust_colour_depth_from_display_info(timing_out, info) &&
5473 drm_mode_is_420_also(info, mode_in) &&
5474 timing_out->pixel_encoding != PIXEL_ENCODING_YCBCR420) {
5475 timing_out->pixel_encoding = PIXEL_ENCODING_YCBCR420;
5476 adjust_colour_depth_from_display_info(timing_out, info);
5477 }
5478 }
766f1792
JA
5479
5480 stream->output_color_space = get_output_color_space(timing_out);
e7b07cee
HW
5481}
5482
3ee6b26b
AD
5483static void fill_audio_info(struct audio_info *audio_info,
5484 const struct drm_connector *drm_connector,
5485 const struct dc_sink *dc_sink)
e7b07cee
HW
5486{
5487 int i = 0;
5488 int cea_revision = 0;
5489 const struct dc_edid_caps *edid_caps = &dc_sink->edid_caps;
5490
5491 audio_info->manufacture_id = edid_caps->manufacturer_id;
5492 audio_info->product_id = edid_caps->product_id;
5493
5494 cea_revision = drm_connector->display_info.cea_rev;
5495
090afc1e 5496 strscpy(audio_info->display_name,
d2b2562c 5497 edid_caps->display_name,
090afc1e 5498 AUDIO_INFO_DISPLAY_NAME_SIZE_IN_CHARS);
e7b07cee 5499
b830ebc9 5500 if (cea_revision >= 3) {
e7b07cee
HW
5501 audio_info->mode_count = edid_caps->audio_mode_count;
5502
5503 for (i = 0; i < audio_info->mode_count; ++i) {
5504 audio_info->modes[i].format_code =
5505 (enum audio_format_code)
5506 (edid_caps->audio_modes[i].format_code);
5507 audio_info->modes[i].channel_count =
5508 edid_caps->audio_modes[i].channel_count;
5509 audio_info->modes[i].sample_rates.all =
5510 edid_caps->audio_modes[i].sample_rate;
5511 audio_info->modes[i].sample_size =
5512 edid_caps->audio_modes[i].sample_size;
5513 }
5514 }
5515
5516 audio_info->flags.all = edid_caps->speaker_flags;
5517
5518 /* TODO: We only check for the progressive mode, check for interlace mode too */
b830ebc9 5519 if (drm_connector->latency_present[0]) {
e7b07cee
HW
5520 audio_info->video_latency = drm_connector->video_latency[0];
5521 audio_info->audio_latency = drm_connector->audio_latency[0];
5522 }
5523
5524 /* TODO: For DP, video and audio latency should be calculated from DPCD caps */
5525
5526}
5527
3ee6b26b
AD
5528static void
5529copy_crtc_timing_for_drm_display_mode(const struct drm_display_mode *src_mode,
5530 struct drm_display_mode *dst_mode)
e7b07cee
HW
5531{
5532 dst_mode->crtc_hdisplay = src_mode->crtc_hdisplay;
5533 dst_mode->crtc_vdisplay = src_mode->crtc_vdisplay;
5534 dst_mode->crtc_clock = src_mode->crtc_clock;
5535 dst_mode->crtc_hblank_start = src_mode->crtc_hblank_start;
5536 dst_mode->crtc_hblank_end = src_mode->crtc_hblank_end;
b830ebc9 5537 dst_mode->crtc_hsync_start = src_mode->crtc_hsync_start;
e7b07cee
HW
5538 dst_mode->crtc_hsync_end = src_mode->crtc_hsync_end;
5539 dst_mode->crtc_htotal = src_mode->crtc_htotal;
5540 dst_mode->crtc_hskew = src_mode->crtc_hskew;
5541 dst_mode->crtc_vblank_start = src_mode->crtc_vblank_start;
5542 dst_mode->crtc_vblank_end = src_mode->crtc_vblank_end;
5543 dst_mode->crtc_vsync_start = src_mode->crtc_vsync_start;
5544 dst_mode->crtc_vsync_end = src_mode->crtc_vsync_end;
5545 dst_mode->crtc_vtotal = src_mode->crtc_vtotal;
5546}
5547
3ee6b26b
AD
5548static void
5549decide_crtc_timing_for_drm_display_mode(struct drm_display_mode *drm_mode,
5550 const struct drm_display_mode *native_mode,
5551 bool scale_enabled)
e7b07cee
HW
5552{
5553 if (scale_enabled) {
5554 copy_crtc_timing_for_drm_display_mode(native_mode, drm_mode);
5555 } else if (native_mode->clock == drm_mode->clock &&
5556 native_mode->htotal == drm_mode->htotal &&
5557 native_mode->vtotal == drm_mode->vtotal) {
5558 copy_crtc_timing_for_drm_display_mode(native_mode, drm_mode);
5559 } else {
5560 /* no scaling nor amdgpu inserted, no need to patch */
5561 }
5562}
5563
aed15309
ML
5564static struct dc_sink *
5565create_fake_sink(struct amdgpu_dm_connector *aconnector)
2e0ac3d6 5566{
2e0ac3d6 5567 struct dc_sink_init_data sink_init_data = { 0 };
aed15309 5568 struct dc_sink *sink = NULL;
2e0ac3d6
HW
5569 sink_init_data.link = aconnector->dc_link;
5570 sink_init_data.sink_signal = aconnector->dc_link->connector_signal;
5571
5572 sink = dc_sink_create(&sink_init_data);
423788c7 5573 if (!sink) {
2e0ac3d6 5574 DRM_ERROR("Failed to create sink!\n");
aed15309 5575 return NULL;
423788c7 5576 }
2e0ac3d6 5577 sink->sink_signal = SIGNAL_TYPE_VIRTUAL;
423788c7 5578
aed15309 5579 return sink;
2e0ac3d6
HW
5580}
5581
fa2123db
ML
5582static void set_multisync_trigger_params(
5583 struct dc_stream_state *stream)
5584{
ec372186
ML
5585 struct dc_stream_state *master = NULL;
5586
fa2123db 5587 if (stream->triggered_crtc_reset.enabled) {
ec372186
ML
5588 master = stream->triggered_crtc_reset.event_source;
5589 stream->triggered_crtc_reset.event =
5590 master->timing.flags.VSYNC_POSITIVE_POLARITY ?
5591 CRTC_EVENT_VSYNC_RISING : CRTC_EVENT_VSYNC_FALLING;
5592 stream->triggered_crtc_reset.delay = TRIGGER_DELAY_NEXT_PIXEL;
fa2123db
ML
5593 }
5594}
5595
5596static void set_master_stream(struct dc_stream_state *stream_set[],
5597 int stream_count)
5598{
5599 int j, highest_rfr = 0, master_stream = 0;
5600
5601 for (j = 0; j < stream_count; j++) {
5602 if (stream_set[j] && stream_set[j]->triggered_crtc_reset.enabled) {
5603 int refresh_rate = 0;
5604
380604e2 5605 refresh_rate = (stream_set[j]->timing.pix_clk_100hz*100)/
fa2123db
ML
5606 (stream_set[j]->timing.h_total*stream_set[j]->timing.v_total);
5607 if (refresh_rate > highest_rfr) {
5608 highest_rfr = refresh_rate;
5609 master_stream = j;
5610 }
5611 }
5612 }
5613 for (j = 0; j < stream_count; j++) {
03736f4c 5614 if (stream_set[j])
fa2123db
ML
5615 stream_set[j]->triggered_crtc_reset.event_source = stream_set[master_stream];
5616 }
5617}
5618
5619static void dm_enable_per_frame_crtc_master_sync(struct dc_state *context)
5620{
5621 int i = 0;
ec372186 5622 struct dc_stream_state *stream;
fa2123db
ML
5623
5624 if (context->stream_count < 2)
5625 return;
5626 for (i = 0; i < context->stream_count ; i++) {
5627 if (!context->streams[i])
5628 continue;
1f6010a9
DF
5629 /*
5630 * TODO: add a function to read AMD VSDB bits and set
fa2123db 5631 * crtc_sync_master.multi_sync_enabled flag
1f6010a9 5632 * For now it's set to false
fa2123db 5633 */
fa2123db 5634 }
ec372186 5635
fa2123db 5636 set_master_stream(context->streams, context->stream_count);
ec372186
ML
5637
5638 for (i = 0; i < context->stream_count ; i++) {
5639 stream = context->streams[i];
5640
5641 if (!stream)
5642 continue;
5643
5644 set_multisync_trigger_params(stream);
5645 }
fa2123db
ML
5646}
5647
5d945cbc
RS
5648/**
5649 * DOC: FreeSync Video
5650 *
5651 * When a userspace application wants to play a video, the content follows a
5652 * standard format definition that usually specifies the FPS for that format.
5653 * The below list illustrates some video format and the expected FPS,
5654 * respectively:
5655 *
5656 * - TV/NTSC (23.976 FPS)
5657 * - Cinema (24 FPS)
5658 * - TV/PAL (25 FPS)
5659 * - TV/NTSC (29.97 FPS)
5660 * - TV/NTSC (30 FPS)
5661 * - Cinema HFR (48 FPS)
5662 * - TV/PAL (50 FPS)
5663 * - Commonly used (60 FPS)
5664 * - Multiples of 24 (48,72,96 FPS)
5665 *
5666 * The list of standards video format is not huge and can be added to the
5667 * connector modeset list beforehand. With that, userspace can leverage
5668 * FreeSync to extends the front porch in order to attain the target refresh
5669 * rate. Such a switch will happen seamlessly, without screen blanking or
5670 * reprogramming of the output in any other way. If the userspace requests a
5671 * modesetting change compatible with FreeSync modes that only differ in the
5672 * refresh rate, DC will skip the full update and avoid blink during the
5673 * transition. For example, the video player can change the modesetting from
5674 * 60Hz to 30Hz for playing TV/NTSC content when it goes full screen without
5675 * causing any display blink. This same concept can be applied to a mode
5676 * setting change.
5677 */
5678static struct drm_display_mode *
5679get_highest_refresh_rate_mode(struct amdgpu_dm_connector *aconnector,
5680 bool use_probed_modes)
5681{
5682 struct drm_display_mode *m, *m_pref = NULL;
5683 u16 current_refresh, highest_refresh;
5684 struct list_head *list_head = use_probed_modes ?
5685 &aconnector->base.probed_modes :
5686 &aconnector->base.modes;
5687
5688 if (aconnector->freesync_vid_base.clock != 0)
5689 return &aconnector->freesync_vid_base;
5690
5691 /* Find the preferred mode */
5692 list_for_each_entry (m, list_head, head) {
5693 if (m->type & DRM_MODE_TYPE_PREFERRED) {
5694 m_pref = m;
5695 break;
5696 }
5697 }
5698
5699 if (!m_pref) {
5700 /* Probably an EDID with no preferred mode. Fallback to first entry */
5701 m_pref = list_first_entry_or_null(
5702 &aconnector->base.modes, struct drm_display_mode, head);
5703 if (!m_pref) {
5704 DRM_DEBUG_DRIVER("No preferred mode found in EDID\n");
5705 return NULL;
5706 }
5707 }
5708
5709 highest_refresh = drm_mode_vrefresh(m_pref);
5710
5711 /*
5712 * Find the mode with highest refresh rate with same resolution.
5713 * For some monitors, preferred mode is not the mode with highest
5714 * supported refresh rate.
5715 */
5716 list_for_each_entry (m, list_head, head) {
5717 current_refresh = drm_mode_vrefresh(m);
5718
5719 if (m->hdisplay == m_pref->hdisplay &&
5720 m->vdisplay == m_pref->vdisplay &&
5721 highest_refresh < current_refresh) {
5722 highest_refresh = current_refresh;
5723 m_pref = m;
5724 }
5725 }
5726
5727 drm_mode_copy(&aconnector->freesync_vid_base, m_pref);
5728 return m_pref;
5729}
5730
5731static bool is_freesync_video_mode(const struct drm_display_mode *mode,
5732 struct amdgpu_dm_connector *aconnector)
5733{
5734 struct drm_display_mode *high_mode;
5735 int timing_diff;
5736
5737 high_mode = get_highest_refresh_rate_mode(aconnector, false);
5738 if (!high_mode || !mode)
5739 return false;
5740
5741 timing_diff = high_mode->vtotal - mode->vtotal;
5742
5743 if (high_mode->clock == 0 || high_mode->clock != mode->clock ||
5744 high_mode->hdisplay != mode->hdisplay ||
5745 high_mode->vdisplay != mode->vdisplay ||
5746 high_mode->hsync_start != mode->hsync_start ||
5747 high_mode->hsync_end != mode->hsync_end ||
5748 high_mode->htotal != mode->htotal ||
5749 high_mode->hskew != mode->hskew ||
5750 high_mode->vscan != mode->vscan ||
5751 high_mode->vsync_start - mode->vsync_start != timing_diff ||
5752 high_mode->vsync_end - mode->vsync_end != timing_diff)
5753 return false;
5754 else
5755 return true;
5756}
5757
ea2be5c0 5758#if defined(CONFIG_DRM_AMD_DC_DCN)
998b7ad2 5759static void update_dsc_caps(struct amdgpu_dm_connector *aconnector,
5d945cbc
RS
5760 struct dc_sink *sink, struct dc_stream_state *stream,
5761 struct dsc_dec_dpcd_caps *dsc_caps)
998b7ad2
FZ
5762{
5763 stream->timing.flags.DSC = 0;
63ad5371 5764 dsc_caps->is_dsc_supported = false;
998b7ad2 5765
2665f63a 5766 if (aconnector->dc_link && (sink->sink_signal == SIGNAL_TYPE_DISPLAY_PORT ||
5d945cbc 5767 sink->sink_signal == SIGNAL_TYPE_EDP)) {
50b1f44e
FZ
5768 if (sink->link->dpcd_caps.dongle_type == DISPLAY_DONGLE_NONE ||
5769 sink->link->dpcd_caps.dongle_type == DISPLAY_DONGLE_DP_HDMI_CONVERTER)
5770 dc_dsc_parse_dsc_dpcd(aconnector->dc_link->ctx->dc,
5771 aconnector->dc_link->dpcd_caps.dsc_caps.dsc_basic_caps.raw,
5772 aconnector->dc_link->dpcd_caps.dsc_caps.dsc_branch_decoder_caps.raw,
5773 dsc_caps);
998b7ad2
FZ
5774 }
5775}
5776
5d945cbc 5777
2665f63a
ML
5778static void apply_dsc_policy_for_edp(struct amdgpu_dm_connector *aconnector,
5779 struct dc_sink *sink, struct dc_stream_state *stream,
5780 struct dsc_dec_dpcd_caps *dsc_caps,
5781 uint32_t max_dsc_target_bpp_limit_override)
5782{
5783 const struct dc_link_settings *verified_link_cap = NULL;
ae67558b
SS
5784 u32 link_bw_in_kbps;
5785 u32 edp_min_bpp_x16, edp_max_bpp_x16;
2665f63a
ML
5786 struct dc *dc = sink->ctx->dc;
5787 struct dc_dsc_bw_range bw_range = {0};
5788 struct dc_dsc_config dsc_cfg = {0};
5789
5790 verified_link_cap = dc_link_get_link_cap(stream->link);
5791 link_bw_in_kbps = dc_link_bandwidth_kbps(stream->link, verified_link_cap);
5792 edp_min_bpp_x16 = 8 * 16;
5793 edp_max_bpp_x16 = 8 * 16;
5794
5795 if (edp_max_bpp_x16 > dsc_caps->edp_max_bits_per_pixel)
5796 edp_max_bpp_x16 = dsc_caps->edp_max_bits_per_pixel;
5797
5798 if (edp_max_bpp_x16 < edp_min_bpp_x16)
5799 edp_min_bpp_x16 = edp_max_bpp_x16;
5800
5801 if (dc_dsc_compute_bandwidth_range(dc->res_pool->dscs[0],
5802 dc->debug.dsc_min_slice_height_override,
5803 edp_min_bpp_x16, edp_max_bpp_x16,
5804 dsc_caps,
5805 &stream->timing,
5806 &bw_range)) {
5807
5808 if (bw_range.max_kbps < link_bw_in_kbps) {
5809 if (dc_dsc_compute_config(dc->res_pool->dscs[0],
5810 dsc_caps,
5811 dc->debug.dsc_min_slice_height_override,
5812 max_dsc_target_bpp_limit_override,
5813 0,
5814 &stream->timing,
5815 &dsc_cfg)) {
5816 stream->timing.dsc_cfg = dsc_cfg;
5817 stream->timing.flags.DSC = 1;
5818 stream->timing.dsc_cfg.bits_per_pixel = edp_max_bpp_x16;
5819 }
5820 return;
5821 }
5822 }
5823
5824 if (dc_dsc_compute_config(dc->res_pool->dscs[0],
5825 dsc_caps,
5826 dc->debug.dsc_min_slice_height_override,
5827 max_dsc_target_bpp_limit_override,
5828 link_bw_in_kbps,
5829 &stream->timing,
5830 &dsc_cfg)) {
5831 stream->timing.dsc_cfg = dsc_cfg;
5832 stream->timing.flags.DSC = 1;
5833 }
5834}
5835
5d945cbc 5836
998b7ad2 5837static void apply_dsc_policy_for_stream(struct amdgpu_dm_connector *aconnector,
5d945cbc
RS
5838 struct dc_sink *sink, struct dc_stream_state *stream,
5839 struct dsc_dec_dpcd_caps *dsc_caps)
998b7ad2
FZ
5840{
5841 struct drm_connector *drm_connector = &aconnector->base;
ae67558b 5842 u32 link_bandwidth_kbps;
2665f63a 5843 struct dc *dc = sink->ctx->dc;
ae67558b
SS
5844 u32 max_supported_bw_in_kbps, timing_bw_in_kbps;
5845 u32 dsc_max_supported_bw_in_kbps;
5846 u32 max_dsc_target_bpp_limit_override =
6e5abe94 5847 drm_connector->display_info.max_dsc_bpp;
998b7ad2
FZ
5848
5849 link_bandwidth_kbps = dc_link_bandwidth_kbps(aconnector->dc_link,
5850 dc_link_get_link_cap(aconnector->dc_link));
de7cc1b4 5851
998b7ad2
FZ
5852 /* Set DSC policy according to dsc_clock_en */
5853 dc_dsc_policy_set_enable_dsc_when_not_needed(
5854 aconnector->dsc_settings.dsc_force_enable == DSC_CLK_FORCE_ENABLE);
5855
c17a34e0
IC
5856 if (aconnector->dc_link && sink->sink_signal == SIGNAL_TYPE_EDP &&
5857 !aconnector->dc_link->panel_config.dsc.disable_dsc_edp &&
2665f63a
ML
5858 dc->caps.edp_dsc_support && aconnector->dsc_settings.dsc_force_enable != DSC_CLK_FORCE_DISABLE) {
5859
5860 apply_dsc_policy_for_edp(aconnector, sink, stream, dsc_caps, max_dsc_target_bpp_limit_override);
5861
5862 } else if (aconnector->dc_link && sink->sink_signal == SIGNAL_TYPE_DISPLAY_PORT) {
50b1f44e
FZ
5863 if (sink->link->dpcd_caps.dongle_type == DISPLAY_DONGLE_NONE) {
5864 if (dc_dsc_compute_config(aconnector->dc_link->ctx->dc->res_pool->dscs[0],
998b7ad2
FZ
5865 dsc_caps,
5866 aconnector->dc_link->ctx->dc->debug.dsc_min_slice_height_override,
f1c1a982 5867 max_dsc_target_bpp_limit_override,
998b7ad2
FZ
5868 link_bandwidth_kbps,
5869 &stream->timing,
5870 &stream->timing.dsc_cfg)) {
50b1f44e 5871 stream->timing.flags.DSC = 1;
5d945cbc 5872 DRM_DEBUG_DRIVER("%s: [%s] DSC is selected from SST RX\n", __func__, drm_connector->name);
50b1f44e
FZ
5873 }
5874 } else if (sink->link->dpcd_caps.dongle_type == DISPLAY_DONGLE_DP_HDMI_CONVERTER) {
5875 timing_bw_in_kbps = dc_bandwidth_in_kbps_from_timing(&stream->timing);
5876 max_supported_bw_in_kbps = link_bandwidth_kbps;
5877 dsc_max_supported_bw_in_kbps = link_bandwidth_kbps;
5878
5879 if (timing_bw_in_kbps > max_supported_bw_in_kbps &&
5880 max_supported_bw_in_kbps > 0 &&
5881 dsc_max_supported_bw_in_kbps > 0)
5882 if (dc_dsc_compute_config(aconnector->dc_link->ctx->dc->res_pool->dscs[0],
5883 dsc_caps,
5884 aconnector->dc_link->ctx->dc->debug.dsc_min_slice_height_override,
5885 max_dsc_target_bpp_limit_override,
5886 dsc_max_supported_bw_in_kbps,
5887 &stream->timing,
5888 &stream->timing.dsc_cfg)) {
5889 stream->timing.flags.DSC = 1;
5890 DRM_DEBUG_DRIVER("%s: [%s] DSC is selected from DP-HDMI PCON\n",
5891 __func__, drm_connector->name);
5892 }
998b7ad2
FZ
5893 }
5894 }
5895
5896 /* Overwrite the stream flag if DSC is enabled through debugfs */
5897 if (aconnector->dsc_settings.dsc_force_enable == DSC_CLK_FORCE_ENABLE)
5898 stream->timing.flags.DSC = 1;
5899
5d945cbc
RS
5900 if (stream->timing.flags.DSC && aconnector->dsc_settings.dsc_num_slices_h)
5901 stream->timing.dsc_cfg.num_slices_h = aconnector->dsc_settings.dsc_num_slices_h;
a85ba005 5902
5d945cbc
RS
5903 if (stream->timing.flags.DSC && aconnector->dsc_settings.dsc_num_slices_v)
5904 stream->timing.dsc_cfg.num_slices_v = aconnector->dsc_settings.dsc_num_slices_v;
a85ba005 5905
5d945cbc
RS
5906 if (stream->timing.flags.DSC && aconnector->dsc_settings.dsc_bits_per_pixel)
5907 stream->timing.dsc_cfg.bits_per_pixel = aconnector->dsc_settings.dsc_bits_per_pixel;
a85ba005 5908}
5d945cbc 5909#endif /* CONFIG_DRM_AMD_DC_DCN */
a85ba005 5910
f11d9373 5911static struct dc_stream_state *
3ee6b26b
AD
5912create_stream_for_sink(struct amdgpu_dm_connector *aconnector,
5913 const struct drm_display_mode *drm_mode,
b333730d 5914 const struct dm_connector_state *dm_state,
cbd14ae7
SW
5915 const struct dc_stream_state *old_stream,
5916 int requested_bpc)
e7b07cee
HW
5917{
5918 struct drm_display_mode *preferred_mode = NULL;
391ef035 5919 struct drm_connector *drm_connector;
42ba01fc
NK
5920 const struct drm_connector_state *con_state =
5921 dm_state ? &dm_state->base : NULL;
0971c40e 5922 struct dc_stream_state *stream = NULL;
0a204ce0 5923 struct drm_display_mode mode;
a85ba005
NC
5924 struct drm_display_mode saved_mode;
5925 struct drm_display_mode *freesync_mode = NULL;
e7b07cee 5926 bool native_mode_found = false;
b0781603
NK
5927 bool recalculate_timing = false;
5928 bool scale = dm_state ? (dm_state->scaling != RMX_OFF) : false;
b333730d 5929 int mode_refresh;
58124bf8 5930 int preferred_refresh = 0;
b1a98cf8 5931 enum color_transfer_func tf = TRANSFER_FUNC_UNKNOWN;
defeb878 5932#if defined(CONFIG_DRM_AMD_DC_DCN)
df2f1015 5933 struct dsc_dec_dpcd_caps dsc_caps;
7c431455 5934#endif
5d945cbc 5935
aed15309 5936 struct dc_sink *sink = NULL;
a85ba005 5937
0a204ce0 5938 drm_mode_init(&mode, drm_mode);
a85ba005
NC
5939 memset(&saved_mode, 0, sizeof(saved_mode));
5940
b830ebc9 5941 if (aconnector == NULL) {
e7b07cee 5942 DRM_ERROR("aconnector is NULL!\n");
64245fa7 5943 return stream;
e7b07cee
HW
5944 }
5945
e7b07cee 5946 drm_connector = &aconnector->base;
2e0ac3d6 5947
f4ac176e 5948 if (!aconnector->dc_sink) {
e3fa5c4c
JFZ
5949 sink = create_fake_sink(aconnector);
5950 if (!sink)
5951 return stream;
aed15309
ML
5952 } else {
5953 sink = aconnector->dc_sink;
dcd5fb82 5954 dc_sink_retain(sink);
f4ac176e 5955 }
2e0ac3d6 5956
aed15309 5957 stream = dc_create_stream_for_sink(sink);
4562236b 5958
b830ebc9 5959 if (stream == NULL) {
e7b07cee 5960 DRM_ERROR("Failed to create stream for sink!\n");
aed15309 5961 goto finish;
e7b07cee
HW
5962 }
5963
ceb3dbb4
JL
5964 stream->dm_stream_context = aconnector;
5965
4a36fcba
WL
5966 stream->timing.flags.LTE_340MCSC_SCRAMBLE =
5967 drm_connector->display_info.hdmi.scdc.scrambling.low_rates;
5968
e7b07cee
HW
5969 list_for_each_entry(preferred_mode, &aconnector->base.modes, head) {
5970 /* Search for preferred mode */
5971 if (preferred_mode->type & DRM_MODE_TYPE_PREFERRED) {
5972 native_mode_found = true;
5973 break;
5974 }
5975 }
5976 if (!native_mode_found)
5977 preferred_mode = list_first_entry_or_null(
5978 &aconnector->base.modes,
5979 struct drm_display_mode,
5980 head);
5981
b333730d
BL
5982 mode_refresh = drm_mode_vrefresh(&mode);
5983
b830ebc9 5984 if (preferred_mode == NULL) {
1f6010a9
DF
5985 /*
5986 * This may not be an error, the use case is when we have no
e7b07cee
HW
5987 * usermode calls to reset and set mode upon hotplug. In this
5988 * case, we call set mode ourselves to restore the previous mode
5989 * and the modelist may not be filled in in time.
5990 */
f1ad2f5e 5991 DRM_DEBUG_DRIVER("No preferred mode found\n");
e7b07cee 5992 } else {
4243c84a
MD
5993 recalculate_timing = amdgpu_freesync_vid_mode &&
5994 is_freesync_video_mode(&mode, aconnector);
a85ba005
NC
5995 if (recalculate_timing) {
5996 freesync_mode = get_highest_refresh_rate_mode(aconnector, false);
426c89aa
VS
5997 drm_mode_copy(&saved_mode, &mode);
5998 drm_mode_copy(&mode, freesync_mode);
a85ba005
NC
5999 } else {
6000 decide_crtc_timing_for_drm_display_mode(
5d945cbc 6001 &mode, preferred_mode, scale);
a85ba005 6002
b0781603
NK
6003 preferred_refresh = drm_mode_vrefresh(preferred_mode);
6004 }
e7b07cee
HW
6005 }
6006
a85ba005
NC
6007 if (recalculate_timing)
6008 drm_mode_set_crtcinfo(&saved_mode, 0);
fe8858bb 6009 else if (!dm_state)
f783577c
JFZ
6010 drm_mode_set_crtcinfo(&mode, 0);
6011
5d945cbc 6012 /*
b333730d
BL
6013 * If scaling is enabled and refresh rate didn't change
6014 * we copy the vic and polarities of the old timings
6015 */
b0781603 6016 if (!scale || mode_refresh != preferred_refresh)
a85ba005
NC
6017 fill_stream_properties_from_drm_display_mode(
6018 stream, &mode, &aconnector->base, con_state, NULL,
6019 requested_bpc);
b333730d 6020 else
a85ba005
NC
6021 fill_stream_properties_from_drm_display_mode(
6022 stream, &mode, &aconnector->base, con_state, old_stream,
6023 requested_bpc);
b333730d 6024
028c4ccf
QZ
6025 if (aconnector->timing_changed) {
6026 DC_LOG_DEBUG("%s: overriding timing for automated test, bpc %d, changing to %d\n",
6027 __func__,
6028 stream->timing.display_color_depth,
6029 aconnector->timing_requested->display_color_depth);
6030 stream->timing = *aconnector->timing_requested;
6031 }
6032
defeb878 6033#if defined(CONFIG_DRM_AMD_DC_DCN)
998b7ad2
FZ
6034 /* SST DSC determination policy */
6035 update_dsc_caps(aconnector, sink, stream, &dsc_caps);
6036 if (aconnector->dsc_settings.dsc_force_enable != DSC_CLK_FORCE_DISABLE && dsc_caps.is_dsc_supported)
6037 apply_dsc_policy_for_stream(aconnector, sink, stream, &dsc_caps);
39a4eb85
WL
6038#endif
6039
e7b07cee
HW
6040 update_stream_scaling_settings(&mode, dm_state, stream);
6041
6042 fill_audio_info(
6043 &stream->audio_info,
6044 drm_connector,
aed15309 6045 sink);
e7b07cee 6046
ceb3dbb4 6047 update_stream_signal(stream, sink);
9182b4cb 6048
d832fc3b 6049 if (stream->signal == SIGNAL_TYPE_HDMI_TYPE_A)
75f77aaf
WL
6050 mod_build_hf_vsif_infopacket(stream, &stream->vsp_infopacket);
6051
8a488f5d
RL
6052 if (stream->link->psr_settings.psr_feature_enabled) {
6053 //
6054 // should decide stream support vsc sdp colorimetry capability
6055 // before building vsc info packet
6056 //
6057 stream->use_vsc_sdp_for_colorimetry = false;
6058 if (aconnector->dc_sink->sink_signal == SIGNAL_TYPE_DISPLAY_PORT_MST) {
6059 stream->use_vsc_sdp_for_colorimetry =
6060 aconnector->dc_sink->is_vsc_sdp_colorimetry_supported;
6061 } else {
6062 if (stream->link->dpcd_caps.dprx_feature.bits.VSC_SDP_COLORIMETRY_SUPPORTED)
6063 stream->use_vsc_sdp_for_colorimetry = true;
8c322309 6064 }
b1a98cf8
MH
6065 if (stream->out_transfer_func->tf == TRANSFER_FUNCTION_GAMMA22)
6066 tf = TRANSFER_FUNC_GAMMA_22;
6067 mod_build_vsc_infopacket(stream, &stream->vsc_infopacket, stream->output_color_space, tf);
1a365683
RL
6068 aconnector->psr_skip_count = AMDGPU_DM_PSR_ENTRY_DELAY;
6069
8c322309 6070 }
aed15309 6071finish:
dcd5fb82 6072 dc_sink_release(sink);
9e3efe3e 6073
e7b07cee
HW
6074 return stream;
6075}
6076
e7b07cee
HW
6077static enum drm_connector_status
6078amdgpu_dm_connector_detect(struct drm_connector *connector, bool force)
6079{
6080 bool connected;
c84dec2f 6081 struct amdgpu_dm_connector *aconnector = to_amdgpu_dm_connector(connector);
e7b07cee 6082
1f6010a9
DF
6083 /*
6084 * Notes:
e7b07cee
HW
6085 * 1. This interface is NOT called in context of HPD irq.
6086 * 2. This interface *is called* in context of user-mode ioctl. Which
1f6010a9
DF
6087 * makes it a bad place for *any* MST-related activity.
6088 */
e7b07cee 6089
8580d60b
HW
6090 if (aconnector->base.force == DRM_FORCE_UNSPECIFIED &&
6091 !aconnector->fake_enable)
e7b07cee
HW
6092 connected = (aconnector->dc_sink != NULL);
6093 else
5d945cbc
RS
6094 connected = (aconnector->base.force == DRM_FORCE_ON ||
6095 aconnector->base.force == DRM_FORCE_ON_DIGITAL);
e7b07cee 6096
0f877894
OV
6097 update_subconnector_property(aconnector);
6098
e7b07cee
HW
6099 return (connected ? connector_status_connected :
6100 connector_status_disconnected);
6101}
6102
3ee6b26b
AD
6103int amdgpu_dm_connector_atomic_set_property(struct drm_connector *connector,
6104 struct drm_connector_state *connector_state,
6105 struct drm_property *property,
6106 uint64_t val)
e7b07cee
HW
6107{
6108 struct drm_device *dev = connector->dev;
1348969a 6109 struct amdgpu_device *adev = drm_to_adev(dev);
e7b07cee
HW
6110 struct dm_connector_state *dm_old_state =
6111 to_dm_connector_state(connector->state);
6112 struct dm_connector_state *dm_new_state =
6113 to_dm_connector_state(connector_state);
6114
6115 int ret = -EINVAL;
6116
6117 if (property == dev->mode_config.scaling_mode_property) {
6118 enum amdgpu_rmx_type rmx_type;
6119
6120 switch (val) {
6121 case DRM_MODE_SCALE_CENTER:
6122 rmx_type = RMX_CENTER;
6123 break;
6124 case DRM_MODE_SCALE_ASPECT:
6125 rmx_type = RMX_ASPECT;
6126 break;
6127 case DRM_MODE_SCALE_FULLSCREEN:
6128 rmx_type = RMX_FULL;
6129 break;
6130 case DRM_MODE_SCALE_NONE:
6131 default:
6132 rmx_type = RMX_OFF;
6133 break;
6134 }
6135
6136 if (dm_old_state->scaling == rmx_type)
6137 return 0;
6138
6139 dm_new_state->scaling = rmx_type;
6140 ret = 0;
6141 } else if (property == adev->mode_info.underscan_hborder_property) {
6142 dm_new_state->underscan_hborder = val;
6143 ret = 0;
6144 } else if (property == adev->mode_info.underscan_vborder_property) {
6145 dm_new_state->underscan_vborder = val;
6146 ret = 0;
6147 } else if (property == adev->mode_info.underscan_property) {
6148 dm_new_state->underscan_enable = val;
6149 ret = 0;
c1ee92f9
DF
6150 } else if (property == adev->mode_info.abm_level_property) {
6151 dm_new_state->abm_level = val;
6152 ret = 0;
e7b07cee
HW
6153 }
6154
6155 return ret;
6156}
6157
3ee6b26b
AD
6158int amdgpu_dm_connector_atomic_get_property(struct drm_connector *connector,
6159 const struct drm_connector_state *state,
6160 struct drm_property *property,
6161 uint64_t *val)
e7b07cee
HW
6162{
6163 struct drm_device *dev = connector->dev;
1348969a 6164 struct amdgpu_device *adev = drm_to_adev(dev);
e7b07cee
HW
6165 struct dm_connector_state *dm_state =
6166 to_dm_connector_state(state);
6167 int ret = -EINVAL;
6168
6169 if (property == dev->mode_config.scaling_mode_property) {
6170 switch (dm_state->scaling) {
6171 case RMX_CENTER:
6172 *val = DRM_MODE_SCALE_CENTER;
6173 break;
6174 case RMX_ASPECT:
6175 *val = DRM_MODE_SCALE_ASPECT;
6176 break;
6177 case RMX_FULL:
6178 *val = DRM_MODE_SCALE_FULLSCREEN;
6179 break;
6180 case RMX_OFF:
6181 default:
6182 *val = DRM_MODE_SCALE_NONE;
6183 break;
6184 }
6185 ret = 0;
6186 } else if (property == adev->mode_info.underscan_hborder_property) {
6187 *val = dm_state->underscan_hborder;
6188 ret = 0;
6189 } else if (property == adev->mode_info.underscan_vborder_property) {
6190 *val = dm_state->underscan_vborder;
6191 ret = 0;
6192 } else if (property == adev->mode_info.underscan_property) {
6193 *val = dm_state->underscan_enable;
6194 ret = 0;
c1ee92f9
DF
6195 } else if (property == adev->mode_info.abm_level_property) {
6196 *val = dm_state->abm_level;
6197 ret = 0;
e7b07cee 6198 }
c1ee92f9 6199
e7b07cee
HW
6200 return ret;
6201}
6202
526c654a
ED
6203static void amdgpu_dm_connector_unregister(struct drm_connector *connector)
6204{
6205 struct amdgpu_dm_connector *amdgpu_dm_connector = to_amdgpu_dm_connector(connector);
6206
6207 drm_dp_aux_unregister(&amdgpu_dm_connector->dm_dp_aux.aux);
6208}
6209
7578ecda 6210static void amdgpu_dm_connector_destroy(struct drm_connector *connector)
e7b07cee 6211{
c84dec2f 6212 struct amdgpu_dm_connector *aconnector = to_amdgpu_dm_connector(connector);
e7b07cee 6213 const struct dc_link *link = aconnector->dc_link;
1348969a 6214 struct amdgpu_device *adev = drm_to_adev(connector->dev);
e7b07cee 6215 struct amdgpu_display_manager *dm = &adev->dm;
7fd13bae 6216 int i;
ada8ce15 6217
5dff80bd 6218 /*
5d945cbc 6219 * Call only if mst_mgr was initialized before since it's not done
5dff80bd
AG
6220 * for all connector types.
6221 */
6222 if (aconnector->mst_mgr.dev)
6223 drm_dp_mst_topology_mgr_destroy(&aconnector->mst_mgr);
6224
7fd13bae
AD
6225 for (i = 0; i < dm->num_of_edps; i++) {
6226 if ((link == dm->backlight_link[i]) && dm->backlight_dev[i]) {
6227 backlight_device_unregister(dm->backlight_dev[i]);
6228 dm->backlight_dev[i] = NULL;
6229 }
e7b07cee 6230 }
dcd5fb82
MF
6231
6232 if (aconnector->dc_em_sink)
6233 dc_sink_release(aconnector->dc_em_sink);
6234 aconnector->dc_em_sink = NULL;
6235 if (aconnector->dc_sink)
6236 dc_sink_release(aconnector->dc_sink);
6237 aconnector->dc_sink = NULL;
6238
e86e8947 6239 drm_dp_cec_unregister_connector(&aconnector->dm_dp_aux.aux);
e7b07cee
HW
6240 drm_connector_unregister(connector);
6241 drm_connector_cleanup(connector);
526c654a
ED
6242 if (aconnector->i2c) {
6243 i2c_del_adapter(&aconnector->i2c->base);
6244 kfree(aconnector->i2c);
6245 }
7daec99f 6246 kfree(aconnector->dm_dp_aux.aux.name);
526c654a 6247
e7b07cee
HW
6248 kfree(connector);
6249}
6250
6251void amdgpu_dm_connector_funcs_reset(struct drm_connector *connector)
6252{
6253 struct dm_connector_state *state =
6254 to_dm_connector_state(connector->state);
6255
df099b9b
LSL
6256 if (connector->state)
6257 __drm_atomic_helper_connector_destroy_state(connector->state);
6258
e7b07cee
HW
6259 kfree(state);
6260
6261 state = kzalloc(sizeof(*state), GFP_KERNEL);
6262
6263 if (state) {
6264 state->scaling = RMX_OFF;
6265 state->underscan_enable = false;
6266 state->underscan_hborder = 0;
6267 state->underscan_vborder = 0;
01933ba4 6268 state->base.max_requested_bpc = 8;
3261e013
ML
6269 state->vcpi_slots = 0;
6270 state->pbn = 0;
5d945cbc 6271
c3e50f89
NK
6272 if (connector->connector_type == DRM_MODE_CONNECTOR_eDP)
6273 state->abm_level = amdgpu_dm_abm_level;
6274
df099b9b 6275 __drm_atomic_helper_connector_reset(connector, &state->base);
e7b07cee
HW
6276 }
6277}
6278
3ee6b26b
AD
6279struct drm_connector_state *
6280amdgpu_dm_connector_atomic_duplicate_state(struct drm_connector *connector)
e7b07cee
HW
6281{
6282 struct dm_connector_state *state =
6283 to_dm_connector_state(connector->state);
6284
6285 struct dm_connector_state *new_state =
6286 kmemdup(state, sizeof(*state), GFP_KERNEL);
6287
98e6436d
AK
6288 if (!new_state)
6289 return NULL;
e7b07cee 6290
98e6436d
AK
6291 __drm_atomic_helper_connector_duplicate_state(connector, &new_state->base);
6292
6293 new_state->freesync_capable = state->freesync_capable;
c1ee92f9 6294 new_state->abm_level = state->abm_level;
922454c2
NK
6295 new_state->scaling = state->scaling;
6296 new_state->underscan_enable = state->underscan_enable;
6297 new_state->underscan_hborder = state->underscan_hborder;
6298 new_state->underscan_vborder = state->underscan_vborder;
3261e013
ML
6299 new_state->vcpi_slots = state->vcpi_slots;
6300 new_state->pbn = state->pbn;
98e6436d 6301 return &new_state->base;
e7b07cee
HW
6302}
6303
14f04fa4
AD
6304static int
6305amdgpu_dm_connector_late_register(struct drm_connector *connector)
6306{
6307 struct amdgpu_dm_connector *amdgpu_dm_connector =
6308 to_amdgpu_dm_connector(connector);
00a8037e 6309 int r;
14f04fa4 6310
00a8037e
AD
6311 if ((connector->connector_type == DRM_MODE_CONNECTOR_DisplayPort) ||
6312 (connector->connector_type == DRM_MODE_CONNECTOR_eDP)) {
6313 amdgpu_dm_connector->dm_dp_aux.aux.dev = connector->kdev;
6314 r = drm_dp_aux_register(&amdgpu_dm_connector->dm_dp_aux.aux);
6315 if (r)
6316 return r;
6317 }
6318
6319#if defined(CONFIG_DEBUG_FS)
14f04fa4
AD
6320 connector_debugfs_init(amdgpu_dm_connector);
6321#endif
6322
6323 return 0;
6324}
6325
e7b07cee
HW
6326static const struct drm_connector_funcs amdgpu_dm_connector_funcs = {
6327 .reset = amdgpu_dm_connector_funcs_reset,
6328 .detect = amdgpu_dm_connector_detect,
6329 .fill_modes = drm_helper_probe_single_connector_modes,
6330 .destroy = amdgpu_dm_connector_destroy,
6331 .atomic_duplicate_state = amdgpu_dm_connector_atomic_duplicate_state,
6332 .atomic_destroy_state = drm_atomic_helper_connector_destroy_state,
6333 .atomic_set_property = amdgpu_dm_connector_atomic_set_property,
526c654a 6334 .atomic_get_property = amdgpu_dm_connector_atomic_get_property,
14f04fa4 6335 .late_register = amdgpu_dm_connector_late_register,
526c654a 6336 .early_unregister = amdgpu_dm_connector_unregister
e7b07cee
HW
6337};
6338
e7b07cee
HW
6339static int get_modes(struct drm_connector *connector)
6340{
6341 return amdgpu_dm_connector_get_modes(connector);
6342}
6343
c84dec2f 6344static void create_eml_sink(struct amdgpu_dm_connector *aconnector)
e7b07cee
HW
6345{
6346 struct dc_sink_init_data init_params = {
6347 .link = aconnector->dc_link,
6348 .sink_signal = SIGNAL_TYPE_VIRTUAL
6349 };
70e8ffc5 6350 struct edid *edid;
e7b07cee 6351
a89ff457 6352 if (!aconnector->base.edid_blob_ptr) {
e7b07cee
HW
6353 DRM_ERROR("No EDID firmware found on connector: %s ,forcing to OFF!\n",
6354 aconnector->base.name);
6355
6356 aconnector->base.force = DRM_FORCE_OFF;
e7b07cee
HW
6357 return;
6358 }
6359
70e8ffc5
HW
6360 edid = (struct edid *) aconnector->base.edid_blob_ptr->data;
6361
e7b07cee
HW
6362 aconnector->edid = edid;
6363
6364 aconnector->dc_em_sink = dc_link_add_remote_sink(
6365 aconnector->dc_link,
6366 (uint8_t *)edid,
6367 (edid->extensions + 1) * EDID_LENGTH,
6368 &init_params);
6369
dcd5fb82 6370 if (aconnector->base.force == DRM_FORCE_ON) {
e7b07cee
HW
6371 aconnector->dc_sink = aconnector->dc_link->local_sink ?
6372 aconnector->dc_link->local_sink :
6373 aconnector->dc_em_sink;
dcd5fb82
MF
6374 dc_sink_retain(aconnector->dc_sink);
6375 }
e7b07cee
HW
6376}
6377
c84dec2f 6378static void handle_edid_mgmt(struct amdgpu_dm_connector *aconnector)
e7b07cee
HW
6379{
6380 struct dc_link *link = (struct dc_link *)aconnector->dc_link;
6381
1f6010a9
DF
6382 /*
6383 * In case of headless boot with force on for DP managed connector
e7b07cee
HW
6384 * Those settings have to be != 0 to get initial modeset
6385 */
6386 if (link->connector_signal == SIGNAL_TYPE_DISPLAY_PORT) {
6387 link->verified_link_cap.lane_count = LANE_COUNT_FOUR;
6388 link->verified_link_cap.link_rate = LINK_RATE_HIGH2;
6389 }
6390
e7b07cee
HW
6391 create_eml_sink(aconnector);
6392}
6393
5468c36d
FZ
6394static enum dc_status dm_validate_stream_and_context(struct dc *dc,
6395 struct dc_stream_state *stream)
6396{
6397 enum dc_status dc_result = DC_ERROR_UNEXPECTED;
6398 struct dc_plane_state *dc_plane_state = NULL;
6399 struct dc_state *dc_state = NULL;
6400
6401 if (!stream)
6402 goto cleanup;
6403
6404 dc_plane_state = dc_create_plane_state(dc);
6405 if (!dc_plane_state)
6406 goto cleanup;
6407
6408 dc_state = dc_create_state(dc);
6409 if (!dc_state)
6410 goto cleanup;
6411
6412 /* populate stream to plane */
6413 dc_plane_state->src_rect.height = stream->src.height;
6414 dc_plane_state->src_rect.width = stream->src.width;
6415 dc_plane_state->dst_rect.height = stream->src.height;
6416 dc_plane_state->dst_rect.width = stream->src.width;
6417 dc_plane_state->clip_rect.height = stream->src.height;
6418 dc_plane_state->clip_rect.width = stream->src.width;
6419 dc_plane_state->plane_size.surface_pitch = ((stream->src.width + 255) / 256) * 256;
6420 dc_plane_state->plane_size.surface_size.height = stream->src.height;
6421 dc_plane_state->plane_size.surface_size.width = stream->src.width;
6422 dc_plane_state->plane_size.chroma_size.height = stream->src.height;
6423 dc_plane_state->plane_size.chroma_size.width = stream->src.width;
5468c36d
FZ
6424 dc_plane_state->format = SURFACE_PIXEL_FORMAT_GRPH_ARGB8888;
6425 dc_plane_state->tiling_info.gfx9.swizzle = DC_SW_UNKNOWN;
6426 dc_plane_state->rotation = ROTATION_ANGLE_0;
6427 dc_plane_state->is_tiling_rotated = false;
6428 dc_plane_state->tiling_info.gfx8.array_mode = DC_ARRAY_LINEAR_GENERAL;
6429
6430 dc_result = dc_validate_stream(dc, stream);
6431 if (dc_result == DC_OK)
6432 dc_result = dc_validate_plane(dc, dc_plane_state);
6433
6434 if (dc_result == DC_OK)
6435 dc_result = dc_add_stream_to_ctx(dc, dc_state, stream);
6436
6437 if (dc_result == DC_OK && !dc_add_plane_to_context(
6438 dc,
6439 stream,
6440 dc_plane_state,
6441 dc_state))
6442 dc_result = DC_FAIL_ATTACH_SURFACES;
6443
6444 if (dc_result == DC_OK)
6445 dc_result = dc_validate_global_state(dc, dc_state, true);
6446
6447cleanup:
6448 if (dc_state)
6449 dc_release_state(dc_state);
6450
6451 if (dc_plane_state)
6452 dc_plane_state_release(dc_plane_state);
6453
6454 return dc_result;
6455}
6456
17ce8a69 6457struct dc_stream_state *
cbd14ae7
SW
6458create_validate_stream_for_sink(struct amdgpu_dm_connector *aconnector,
6459 const struct drm_display_mode *drm_mode,
6460 const struct dm_connector_state *dm_state,
6461 const struct dc_stream_state *old_stream)
6462{
6463 struct drm_connector *connector = &aconnector->base;
1348969a 6464 struct amdgpu_device *adev = drm_to_adev(connector->dev);
cbd14ae7 6465 struct dc_stream_state *stream;
4b7da34b
SW
6466 const struct drm_connector_state *drm_state = dm_state ? &dm_state->base : NULL;
6467 int requested_bpc = drm_state ? drm_state->max_requested_bpc : 8;
cbd14ae7
SW
6468 enum dc_status dc_result = DC_OK;
6469
6470 do {
6471 stream = create_stream_for_sink(aconnector, drm_mode,
6472 dm_state, old_stream,
6473 requested_bpc);
6474 if (stream == NULL) {
6475 DRM_ERROR("Failed to create stream for sink!\n");
6476 break;
6477 }
6478
e9a7d236
RS
6479 dc_result = dc_validate_stream(adev->dm.dc, stream);
6480 if (dc_result == DC_OK && stream->signal == SIGNAL_TYPE_DISPLAY_PORT_MST)
f04d275d 6481 dc_result = dm_dp_mst_is_port_support_mode(aconnector, stream);
6482
5468c36d
FZ
6483 if (dc_result == DC_OK)
6484 dc_result = dm_validate_stream_and_context(adev->dm.dc, stream);
6485
cbd14ae7 6486 if (dc_result != DC_OK) {
74a16675 6487 DRM_DEBUG_KMS("Mode %dx%d (clk %d) failed DC validation with error %d (%s)\n",
cbd14ae7
SW
6488 drm_mode->hdisplay,
6489 drm_mode->vdisplay,
6490 drm_mode->clock,
74a16675
RS
6491 dc_result,
6492 dc_status_to_str(dc_result));
cbd14ae7
SW
6493
6494 dc_stream_release(stream);
6495 stream = NULL;
6496 requested_bpc -= 2; /* lower bpc to retry validation */
6497 }
6498
6499 } while (stream == NULL && requested_bpc >= 6);
6500
68eb3ae3
WS
6501 if (dc_result == DC_FAIL_ENC_VALIDATE && !aconnector->force_yuv420_output) {
6502 DRM_DEBUG_KMS("Retry forcing YCbCr420 encoding\n");
6503
6504 aconnector->force_yuv420_output = true;
6505 stream = create_validate_stream_for_sink(aconnector, drm_mode,
6506 dm_state, old_stream);
6507 aconnector->force_yuv420_output = false;
6508 }
6509
cbd14ae7
SW
6510 return stream;
6511}
6512
ba9ca088 6513enum drm_mode_status amdgpu_dm_connector_mode_valid(struct drm_connector *connector,
3ee6b26b 6514 struct drm_display_mode *mode)
e7b07cee
HW
6515{
6516 int result = MODE_ERROR;
6517 struct dc_sink *dc_sink;
e7b07cee 6518 /* TODO: Unhardcode stream count */
0971c40e 6519 struct dc_stream_state *stream;
c84dec2f 6520 struct amdgpu_dm_connector *aconnector = to_amdgpu_dm_connector(connector);
e7b07cee
HW
6521
6522 if ((mode->flags & DRM_MODE_FLAG_INTERLACE) ||
6523 (mode->flags & DRM_MODE_FLAG_DBLSCAN))
6524 return result;
6525
1f6010a9
DF
6526 /*
6527 * Only run this the first time mode_valid is called to initilialize
e7b07cee
HW
6528 * EDID mgmt
6529 */
6530 if (aconnector->base.force != DRM_FORCE_UNSPECIFIED &&
6531 !aconnector->dc_em_sink)
6532 handle_edid_mgmt(aconnector);
6533
c84dec2f 6534 dc_sink = to_amdgpu_dm_connector(connector)->dc_sink;
e7b07cee 6535
ad975f44
VL
6536 if (dc_sink == NULL && aconnector->base.force != DRM_FORCE_ON_DIGITAL &&
6537 aconnector->base.force != DRM_FORCE_ON) {
e7b07cee
HW
6538 DRM_ERROR("dc_sink is NULL!\n");
6539 goto fail;
6540 }
6541
cbd14ae7
SW
6542 stream = create_validate_stream_for_sink(aconnector, mode, NULL, NULL);
6543 if (stream) {
6544 dc_stream_release(stream);
e7b07cee 6545 result = MODE_OK;
cbd14ae7 6546 }
e7b07cee
HW
6547
6548fail:
6549 /* TODO: error handling*/
6550 return result;
6551}
6552
88694af9
NK
6553static int fill_hdr_info_packet(const struct drm_connector_state *state,
6554 struct dc_info_packet *out)
6555{
6556 struct hdmi_drm_infoframe frame;
6557 unsigned char buf[30]; /* 26 + 4 */
6558 ssize_t len;
6559 int ret, i;
6560
6561 memset(out, 0, sizeof(*out));
6562
6563 if (!state->hdr_output_metadata)
6564 return 0;
6565
6566 ret = drm_hdmi_infoframe_set_hdr_metadata(&frame, state);
6567 if (ret)
6568 return ret;
6569
6570 len = hdmi_drm_infoframe_pack_only(&frame, buf, sizeof(buf));
6571 if (len < 0)
6572 return (int)len;
6573
6574 /* Static metadata is a fixed 26 bytes + 4 byte header. */
6575 if (len != 30)
6576 return -EINVAL;
6577
6578 /* Prepare the infopacket for DC. */
6579 switch (state->connector->connector_type) {
6580 case DRM_MODE_CONNECTOR_HDMIA:
6581 out->hb0 = 0x87; /* type */
6582 out->hb1 = 0x01; /* version */
6583 out->hb2 = 0x1A; /* length */
6584 out->sb[0] = buf[3]; /* checksum */
6585 i = 1;
6586 break;
6587
6588 case DRM_MODE_CONNECTOR_DisplayPort:
6589 case DRM_MODE_CONNECTOR_eDP:
6590 out->hb0 = 0x00; /* sdp id, zero */
6591 out->hb1 = 0x87; /* type */
6592 out->hb2 = 0x1D; /* payload len - 1 */
6593 out->hb3 = (0x13 << 2); /* sdp version */
6594 out->sb[0] = 0x01; /* version */
6595 out->sb[1] = 0x1A; /* length */
6596 i = 2;
6597 break;
6598
6599 default:
6600 return -EINVAL;
6601 }
6602
6603 memcpy(&out->sb[i], &buf[4], 26);
6604 out->valid = true;
6605
6606 print_hex_dump(KERN_DEBUG, "HDR SB:", DUMP_PREFIX_NONE, 16, 1, out->sb,
6607 sizeof(out->sb), false);
6608
6609 return 0;
6610}
6611
88694af9
NK
6612static int
6613amdgpu_dm_connector_atomic_check(struct drm_connector *conn,
51e857af 6614 struct drm_atomic_state *state)
88694af9 6615{
51e857af
SP
6616 struct drm_connector_state *new_con_state =
6617 drm_atomic_get_new_connector_state(state, conn);
88694af9
NK
6618 struct drm_connector_state *old_con_state =
6619 drm_atomic_get_old_connector_state(state, conn);
6620 struct drm_crtc *crtc = new_con_state->crtc;
6621 struct drm_crtc_state *new_crtc_state;
a76eb429 6622 struct amdgpu_dm_connector *aconn = to_amdgpu_dm_connector(conn);
88694af9
NK
6623 int ret;
6624
e8a98235
RS
6625 trace_amdgpu_dm_connector_atomic_check(new_con_state);
6626
a76eb429
LP
6627 if (conn->connector_type == DRM_MODE_CONNECTOR_DisplayPort) {
6628 ret = drm_dp_mst_root_conn_atomic_check(new_con_state, &aconn->mst_mgr);
6629 if (ret < 0)
6630 return ret;
6631 }
6632
88694af9
NK
6633 if (!crtc)
6634 return 0;
6635
72921cdf 6636 if (!drm_connector_atomic_hdr_metadata_equal(old_con_state, new_con_state)) {
88694af9
NK
6637 struct dc_info_packet hdr_infopacket;
6638
6639 ret = fill_hdr_info_packet(new_con_state, &hdr_infopacket);
6640 if (ret)
6641 return ret;
6642
6643 new_crtc_state = drm_atomic_get_crtc_state(state, crtc);
6644 if (IS_ERR(new_crtc_state))
6645 return PTR_ERR(new_crtc_state);
6646
6647 /*
6648 * DC considers the stream backends changed if the
6649 * static metadata changes. Forcing the modeset also
6650 * gives a simple way for userspace to switch from
b232d4ed
NK
6651 * 8bpc to 10bpc when setting the metadata to enter
6652 * or exit HDR.
6653 *
6654 * Changing the static metadata after it's been
6655 * set is permissible, however. So only force a
6656 * modeset if we're entering or exiting HDR.
88694af9 6657 */
b232d4ed
NK
6658 new_crtc_state->mode_changed =
6659 !old_con_state->hdr_output_metadata ||
6660 !new_con_state->hdr_output_metadata;
88694af9
NK
6661 }
6662
6663 return 0;
6664}
6665
e7b07cee
HW
6666static const struct drm_connector_helper_funcs
6667amdgpu_dm_connector_helper_funcs = {
6668 /*
1f6010a9 6669 * If hotplugging a second bigger display in FB Con mode, bigger resolution
b830ebc9 6670 * modes will be filtered by drm_mode_validate_size(), and those modes
1f6010a9 6671 * are missing after user start lightdm. So we need to renew modes list.
b830ebc9
HW
6672 * in get_modes call back, not just return the modes count
6673 */
e7b07cee
HW
6674 .get_modes = get_modes,
6675 .mode_valid = amdgpu_dm_connector_mode_valid,
88694af9 6676 .atomic_check = amdgpu_dm_connector_atomic_check,
e7b07cee
HW
6677};
6678
e7b07cee
HW
6679static void dm_encoder_helper_disable(struct drm_encoder *encoder)
6680{
6681
6682}
6683
f04d275d 6684int convert_dc_color_depth_into_bpc(enum dc_color_depth display_color_depth)
3261e013
ML
6685{
6686 switch (display_color_depth) {
5d945cbc
RS
6687 case COLOR_DEPTH_666:
6688 return 6;
6689 case COLOR_DEPTH_888:
6690 return 8;
6691 case COLOR_DEPTH_101010:
6692 return 10;
6693 case COLOR_DEPTH_121212:
6694 return 12;
6695 case COLOR_DEPTH_141414:
6696 return 14;
6697 case COLOR_DEPTH_161616:
6698 return 16;
6699 default:
6700 break;
6701 }
3261e013
ML
6702 return 0;
6703}
6704
3ee6b26b
AD
6705static int dm_encoder_helper_atomic_check(struct drm_encoder *encoder,
6706 struct drm_crtc_state *crtc_state,
6707 struct drm_connector_state *conn_state)
e7b07cee 6708{
3261e013
ML
6709 struct drm_atomic_state *state = crtc_state->state;
6710 struct drm_connector *connector = conn_state->connector;
6711 struct amdgpu_dm_connector *aconnector = to_amdgpu_dm_connector(connector);
6712 struct dm_connector_state *dm_new_connector_state = to_dm_connector_state(conn_state);
6713 const struct drm_display_mode *adjusted_mode = &crtc_state->adjusted_mode;
6714 struct drm_dp_mst_topology_mgr *mst_mgr;
6715 struct drm_dp_mst_port *mst_port;
4d07b0bc 6716 struct drm_dp_mst_topology_state *mst_state;
3261e013
ML
6717 enum dc_color_depth color_depth;
6718 int clock, bpp = 0;
1bc22f20 6719 bool is_y420 = false;
3261e013 6720
f0127cb1 6721 if (!aconnector->mst_output_port || !aconnector->dc_sink)
3261e013
ML
6722 return 0;
6723
f0127cb1
WL
6724 mst_port = aconnector->mst_output_port;
6725 mst_mgr = &aconnector->mst_root->mst_mgr;
3261e013
ML
6726
6727 if (!crtc_state->connectors_changed && !crtc_state->mode_changed)
6728 return 0;
6729
4d07b0bc
LP
6730 mst_state = drm_atomic_get_mst_topology_state(state, mst_mgr);
6731 if (IS_ERR(mst_state))
6732 return PTR_ERR(mst_state);
6733
6734 if (!mst_state->pbn_div)
f0127cb1 6735 mst_state->pbn_div = dm_mst_get_pbn_divider(aconnector->mst_root->dc_link);
4d07b0bc 6736
3261e013 6737 if (!state->duplicated) {
cbd14ae7 6738 int max_bpc = conn_state->max_requested_bpc;
1bc22f20 6739 is_y420 = drm_mode_is_420_also(&connector->display_info, adjusted_mode) &&
5d945cbc 6740 aconnector->force_yuv420_output;
cbd14ae7
SW
6741 color_depth = convert_color_depth_from_display_info(connector,
6742 is_y420,
6743 max_bpc);
3261e013
ML
6744 bpp = convert_dc_color_depth_into_bpc(color_depth) * 3;
6745 clock = adjusted_mode->clock;
dc48529f 6746 dm_new_connector_state->pbn = drm_dp_calc_pbn_mode(clock, bpp, false);
3261e013 6747 }
4d07b0bc
LP
6748
6749 dm_new_connector_state->vcpi_slots =
6750 drm_dp_atomic_find_time_slots(state, mst_mgr, mst_port,
6751 dm_new_connector_state->pbn);
3261e013
ML
6752 if (dm_new_connector_state->vcpi_slots < 0) {
6753 DRM_DEBUG_ATOMIC("failed finding vcpi slots: %d\n", (int)dm_new_connector_state->vcpi_slots);
6754 return dm_new_connector_state->vcpi_slots;
6755 }
e7b07cee
HW
6756 return 0;
6757}
6758
6759const struct drm_encoder_helper_funcs amdgpu_dm_encoder_helper_funcs = {
6760 .disable = dm_encoder_helper_disable,
6761 .atomic_check = dm_encoder_helper_atomic_check
6762};
6763
d9fe1a4c 6764#if defined(CONFIG_DRM_AMD_DC_DCN)
29b9ba74 6765static int dm_update_mst_vcpi_slots_for_dsc(struct drm_atomic_state *state,
6513104b
HW
6766 struct dc_state *dc_state,
6767 struct dsc_mst_fairness_vars *vars)
29b9ba74
ML
6768{
6769 struct dc_stream_state *stream = NULL;
6770 struct drm_connector *connector;
5760dcb9 6771 struct drm_connector_state *new_con_state;
29b9ba74
ML
6772 struct amdgpu_dm_connector *aconnector;
6773 struct dm_connector_state *dm_conn_state;
7cce4cd6 6774 int i, j, ret;
a550bb16 6775 int vcpi, pbn_div, pbn, slot_num = 0;
29b9ba74 6776
5760dcb9 6777 for_each_new_connector_in_state(state, connector, new_con_state, i) {
29b9ba74
ML
6778
6779 aconnector = to_amdgpu_dm_connector(connector);
6780
f0127cb1 6781 if (!aconnector->mst_output_port)
29b9ba74
ML
6782 continue;
6783
6784 if (!new_con_state || !new_con_state->crtc)
6785 continue;
6786
6787 dm_conn_state = to_dm_connector_state(new_con_state);
6788
6789 for (j = 0; j < dc_state->stream_count; j++) {
6790 stream = dc_state->streams[j];
6791 if (!stream)
6792 continue;
6793
5d945cbc 6794 if ((struct amdgpu_dm_connector *)stream->dm_stream_context == aconnector)
29b9ba74
ML
6795 break;
6796
6797 stream = NULL;
6798 }
6799
6800 if (!stream)
6801 continue;
6802
29b9ba74 6803 pbn_div = dm_mst_get_pbn_divider(stream->link);
6513104b
HW
6804 /* pbn is calculated by compute_mst_dsc_configs_for_state*/
6805 for (j = 0; j < dc_state->stream_count; j++) {
6806 if (vars[j].aconnector == aconnector) {
6807 pbn = vars[j].pbn;
6808 break;
6809 }
6810 }
6811
a550bb16
HW
6812 if (j == dc_state->stream_count)
6813 continue;
6814
6815 slot_num = DIV_ROUND_UP(pbn, pbn_div);
6816
6817 if (stream->timing.flags.DSC != 1) {
6818 dm_conn_state->pbn = pbn;
6819 dm_conn_state->vcpi_slots = slot_num;
6820
f0127cb1 6821 ret = drm_dp_mst_atomic_enable_dsc(state, aconnector->mst_output_port,
7cce4cd6
LP
6822 dm_conn_state->pbn, false);
6823 if (ret < 0)
6824 return ret;
6825
a550bb16
HW
6826 continue;
6827 }
6828
f0127cb1 6829 vcpi = drm_dp_mst_atomic_enable_dsc(state, aconnector->mst_output_port, pbn, true);
29b9ba74
ML
6830 if (vcpi < 0)
6831 return vcpi;
6832
6833 dm_conn_state->pbn = pbn;
6834 dm_conn_state->vcpi_slots = vcpi;
6835 }
6836 return 0;
6837}
d9fe1a4c 6838#endif
29b9ba74 6839
e7b07cee
HW
6840static int to_drm_connector_type(enum signal_type st)
6841{
6842 switch (st) {
6843 case SIGNAL_TYPE_HDMI_TYPE_A:
6844 return DRM_MODE_CONNECTOR_HDMIA;
6845 case SIGNAL_TYPE_EDP:
6846 return DRM_MODE_CONNECTOR_eDP;
11c3ee48
AD
6847 case SIGNAL_TYPE_LVDS:
6848 return DRM_MODE_CONNECTOR_LVDS;
e7b07cee
HW
6849 case SIGNAL_TYPE_RGB:
6850 return DRM_MODE_CONNECTOR_VGA;
6851 case SIGNAL_TYPE_DISPLAY_PORT:
6852 case SIGNAL_TYPE_DISPLAY_PORT_MST:
6853 return DRM_MODE_CONNECTOR_DisplayPort;
6854 case SIGNAL_TYPE_DVI_DUAL_LINK:
6855 case SIGNAL_TYPE_DVI_SINGLE_LINK:
6856 return DRM_MODE_CONNECTOR_DVID;
6857 case SIGNAL_TYPE_VIRTUAL:
6858 return DRM_MODE_CONNECTOR_VIRTUAL;
6859
6860 default:
6861 return DRM_MODE_CONNECTOR_Unknown;
6862 }
6863}
6864
2b4c1c05
DV
6865static struct drm_encoder *amdgpu_dm_connector_to_encoder(struct drm_connector *connector)
6866{
62afb4ad
JRS
6867 struct drm_encoder *encoder;
6868
6869 /* There is only one encoder per connector */
6870 drm_connector_for_each_possible_encoder(connector, encoder)
6871 return encoder;
6872
6873 return NULL;
2b4c1c05
DV
6874}
6875
e7b07cee
HW
6876static void amdgpu_dm_get_native_mode(struct drm_connector *connector)
6877{
e7b07cee
HW
6878 struct drm_encoder *encoder;
6879 struct amdgpu_encoder *amdgpu_encoder;
6880
2b4c1c05 6881 encoder = amdgpu_dm_connector_to_encoder(connector);
e7b07cee
HW
6882
6883 if (encoder == NULL)
6884 return;
6885
6886 amdgpu_encoder = to_amdgpu_encoder(encoder);
6887
6888 amdgpu_encoder->native_mode.clock = 0;
6889
6890 if (!list_empty(&connector->probed_modes)) {
6891 struct drm_display_mode *preferred_mode = NULL;
b830ebc9 6892
e7b07cee 6893 list_for_each_entry(preferred_mode,
b830ebc9
HW
6894 &connector->probed_modes,
6895 head) {
6896 if (preferred_mode->type & DRM_MODE_TYPE_PREFERRED)
6897 amdgpu_encoder->native_mode = *preferred_mode;
6898
e7b07cee
HW
6899 break;
6900 }
6901
6902 }
6903}
6904
3ee6b26b
AD
6905static struct drm_display_mode *
6906amdgpu_dm_create_common_mode(struct drm_encoder *encoder,
6907 char *name,
6908 int hdisplay, int vdisplay)
e7b07cee
HW
6909{
6910 struct drm_device *dev = encoder->dev;
6911 struct amdgpu_encoder *amdgpu_encoder = to_amdgpu_encoder(encoder);
6912 struct drm_display_mode *mode = NULL;
6913 struct drm_display_mode *native_mode = &amdgpu_encoder->native_mode;
6914
6915 mode = drm_mode_duplicate(dev, native_mode);
6916
b830ebc9 6917 if (mode == NULL)
e7b07cee
HW
6918 return NULL;
6919
6920 mode->hdisplay = hdisplay;
6921 mode->vdisplay = vdisplay;
6922 mode->type &= ~DRM_MODE_TYPE_PREFERRED;
090afc1e 6923 strscpy(mode->name, name, DRM_DISPLAY_MODE_LEN);
e7b07cee
HW
6924
6925 return mode;
6926
6927}
6928
6929static void amdgpu_dm_connector_add_common_modes(struct drm_encoder *encoder,
3ee6b26b 6930 struct drm_connector *connector)
e7b07cee
HW
6931{
6932 struct amdgpu_encoder *amdgpu_encoder = to_amdgpu_encoder(encoder);
6933 struct drm_display_mode *mode = NULL;
6934 struct drm_display_mode *native_mode = &amdgpu_encoder->native_mode;
c84dec2f
HW
6935 struct amdgpu_dm_connector *amdgpu_dm_connector =
6936 to_amdgpu_dm_connector(connector);
e7b07cee
HW
6937 int i;
6938 int n;
6939 struct mode_size {
6940 char name[DRM_DISPLAY_MODE_LEN];
6941 int w;
6942 int h;
b830ebc9 6943 } common_modes[] = {
e7b07cee
HW
6944 { "640x480", 640, 480},
6945 { "800x600", 800, 600},
6946 { "1024x768", 1024, 768},
6947 { "1280x720", 1280, 720},
6948 { "1280x800", 1280, 800},
6949 {"1280x1024", 1280, 1024},
6950 { "1440x900", 1440, 900},
6951 {"1680x1050", 1680, 1050},
6952 {"1600x1200", 1600, 1200},
6953 {"1920x1080", 1920, 1080},
6954 {"1920x1200", 1920, 1200}
6955 };
6956
b830ebc9 6957 n = ARRAY_SIZE(common_modes);
e7b07cee
HW
6958
6959 for (i = 0; i < n; i++) {
6960 struct drm_display_mode *curmode = NULL;
6961 bool mode_existed = false;
6962
6963 if (common_modes[i].w > native_mode->hdisplay ||
b830ebc9
HW
6964 common_modes[i].h > native_mode->vdisplay ||
6965 (common_modes[i].w == native_mode->hdisplay &&
6966 common_modes[i].h == native_mode->vdisplay))
6967 continue;
e7b07cee
HW
6968
6969 list_for_each_entry(curmode, &connector->probed_modes, head) {
6970 if (common_modes[i].w == curmode->hdisplay &&
b830ebc9 6971 common_modes[i].h == curmode->vdisplay) {
e7b07cee
HW
6972 mode_existed = true;
6973 break;
6974 }
6975 }
6976
6977 if (mode_existed)
6978 continue;
6979
6980 mode = amdgpu_dm_create_common_mode(encoder,
6981 common_modes[i].name, common_modes[i].w,
6982 common_modes[i].h);
588a7017
ZQ
6983 if (!mode)
6984 continue;
6985
e7b07cee 6986 drm_mode_probed_add(connector, mode);
c84dec2f 6987 amdgpu_dm_connector->num_modes++;
e7b07cee
HW
6988 }
6989}
6990
d77de788
SS
6991static void amdgpu_set_panel_orientation(struct drm_connector *connector)
6992{
6993 struct drm_encoder *encoder;
6994 struct amdgpu_encoder *amdgpu_encoder;
6995 const struct drm_display_mode *native_mode;
6996
6997 if (connector->connector_type != DRM_MODE_CONNECTOR_eDP &&
6998 connector->connector_type != DRM_MODE_CONNECTOR_LVDS)
6999 return;
7000
acc96ae0
MW
7001 mutex_lock(&connector->dev->mode_config.mutex);
7002 amdgpu_dm_connector_get_modes(connector);
7003 mutex_unlock(&connector->dev->mode_config.mutex);
7004
d77de788
SS
7005 encoder = amdgpu_dm_connector_to_encoder(connector);
7006 if (!encoder)
7007 return;
7008
7009 amdgpu_encoder = to_amdgpu_encoder(encoder);
7010
7011 native_mode = &amdgpu_encoder->native_mode;
7012 if (native_mode->hdisplay == 0 || native_mode->vdisplay == 0)
7013 return;
7014
7015 drm_connector_set_panel_orientation_with_quirk(connector,
7016 DRM_MODE_PANEL_ORIENTATION_UNKNOWN,
7017 native_mode->hdisplay,
7018 native_mode->vdisplay);
7019}
7020
3ee6b26b
AD
7021static void amdgpu_dm_connector_ddc_get_modes(struct drm_connector *connector,
7022 struct edid *edid)
e7b07cee 7023{
c84dec2f
HW
7024 struct amdgpu_dm_connector *amdgpu_dm_connector =
7025 to_amdgpu_dm_connector(connector);
e7b07cee
HW
7026
7027 if (edid) {
7028 /* empty probed_modes */
7029 INIT_LIST_HEAD(&connector->probed_modes);
c84dec2f 7030 amdgpu_dm_connector->num_modes =
e7b07cee
HW
7031 drm_add_edid_modes(connector, edid);
7032
f1e5e913
YMM
7033 /* sorting the probed modes before calling function
7034 * amdgpu_dm_get_native_mode() since EDID can have
7035 * more than one preferred mode. The modes that are
7036 * later in the probed mode list could be of higher
7037 * and preferred resolution. For example, 3840x2160
7038 * resolution in base EDID preferred timing and 4096x2160
7039 * preferred resolution in DID extension block later.
7040 */
7041 drm_mode_sort(&connector->probed_modes);
e7b07cee 7042 amdgpu_dm_get_native_mode(connector);
f9b4f20c
SW
7043
7044 /* Freesync capabilities are reset by calling
7045 * drm_add_edid_modes() and need to be
7046 * restored here.
7047 */
7048 amdgpu_dm_update_freesync_caps(connector, edid);
a8d8d3dc 7049 } else {
c84dec2f 7050 amdgpu_dm_connector->num_modes = 0;
a8d8d3dc 7051 }
e7b07cee
HW
7052}
7053
a85ba005
NC
7054static bool is_duplicate_mode(struct amdgpu_dm_connector *aconnector,
7055 struct drm_display_mode *mode)
7056{
7057 struct drm_display_mode *m;
7058
7059 list_for_each_entry (m, &aconnector->base.probed_modes, head) {
7060 if (drm_mode_equal(m, mode))
7061 return true;
7062 }
7063
7064 return false;
7065}
7066
7067static uint add_fs_modes(struct amdgpu_dm_connector *aconnector)
7068{
7069 const struct drm_display_mode *m;
7070 struct drm_display_mode *new_mode;
7071 uint i;
ae67558b 7072 u32 new_modes_count = 0;
a85ba005
NC
7073
7074 /* Standard FPS values
7075 *
12cdff6b
SC
7076 * 23.976 - TV/NTSC
7077 * 24 - Cinema
7078 * 25 - TV/PAL
7079 * 29.97 - TV/NTSC
7080 * 30 - TV/NTSC
7081 * 48 - Cinema HFR
7082 * 50 - TV/PAL
7083 * 60 - Commonly used
7084 * 48,72,96,120 - Multiples of 24
a85ba005 7085 */
ae67558b 7086 static const u32 common_rates[] = {
9ce5ed6e 7087 23976, 24000, 25000, 29970, 30000,
12cdff6b 7088 48000, 50000, 60000, 72000, 96000, 120000
9ce5ed6e 7089 };
a85ba005
NC
7090
7091 /*
7092 * Find mode with highest refresh rate with the same resolution
7093 * as the preferred mode. Some monitors report a preferred mode
7094 * with lower resolution than the highest refresh rate supported.
7095 */
7096
7097 m = get_highest_refresh_rate_mode(aconnector, true);
7098 if (!m)
7099 return 0;
7100
7101 for (i = 0; i < ARRAY_SIZE(common_rates); i++) {
ae67558b
SS
7102 u64 target_vtotal, target_vtotal_diff;
7103 u64 num, den;
a85ba005
NC
7104
7105 if (drm_mode_vrefresh(m) * 1000 < common_rates[i])
7106 continue;
7107
7108 if (common_rates[i] < aconnector->min_vfreq * 1000 ||
7109 common_rates[i] > aconnector->max_vfreq * 1000)
7110 continue;
7111
7112 num = (unsigned long long)m->clock * 1000 * 1000;
7113 den = common_rates[i] * (unsigned long long)m->htotal;
7114 target_vtotal = div_u64(num, den);
7115 target_vtotal_diff = target_vtotal - m->vtotal;
7116
7117 /* Check for illegal modes */
7118 if (m->vsync_start + target_vtotal_diff < m->vdisplay ||
7119 m->vsync_end + target_vtotal_diff < m->vsync_start ||
7120 m->vtotal + target_vtotal_diff < m->vsync_end)
7121 continue;
7122
7123 new_mode = drm_mode_duplicate(aconnector->base.dev, m);
7124 if (!new_mode)
7125 goto out;
7126
7127 new_mode->vtotal += (u16)target_vtotal_diff;
7128 new_mode->vsync_start += (u16)target_vtotal_diff;
7129 new_mode->vsync_end += (u16)target_vtotal_diff;
7130 new_mode->type &= ~DRM_MODE_TYPE_PREFERRED;
7131 new_mode->type |= DRM_MODE_TYPE_DRIVER;
7132
7133 if (!is_duplicate_mode(aconnector, new_mode)) {
7134 drm_mode_probed_add(&aconnector->base, new_mode);
7135 new_modes_count += 1;
7136 } else
7137 drm_mode_destroy(aconnector->base.dev, new_mode);
7138 }
7139 out:
7140 return new_modes_count;
7141}
7142
7143static void amdgpu_dm_connector_add_freesync_modes(struct drm_connector *connector,
7144 struct edid *edid)
7145{
7146 struct amdgpu_dm_connector *amdgpu_dm_connector =
7147 to_amdgpu_dm_connector(connector);
7148
4243c84a 7149 if (!(amdgpu_freesync_vid_mode && edid))
a85ba005 7150 return;
fe8858bb 7151
a85ba005
NC
7152 if (amdgpu_dm_connector->max_vfreq - amdgpu_dm_connector->min_vfreq > 10)
7153 amdgpu_dm_connector->num_modes +=
7154 add_fs_modes(amdgpu_dm_connector);
7155}
7156
7578ecda 7157static int amdgpu_dm_connector_get_modes(struct drm_connector *connector)
e7b07cee 7158{
c84dec2f
HW
7159 struct amdgpu_dm_connector *amdgpu_dm_connector =
7160 to_amdgpu_dm_connector(connector);
e7b07cee 7161 struct drm_encoder *encoder;
c84dec2f 7162 struct edid *edid = amdgpu_dm_connector->edid;
c32699ca
JD
7163 struct dc_link_settings *verified_link_cap =
7164 &amdgpu_dm_connector->dc_link->verified_link_cap;
e7b07cee 7165
2b4c1c05 7166 encoder = amdgpu_dm_connector_to_encoder(connector);
3e332d3a 7167
5c0e6840 7168 if (!drm_edid_is_valid(edid)) {
1b369d3c
ML
7169 amdgpu_dm_connector->num_modes =
7170 drm_add_modes_noedid(connector, 640, 480);
c32699ca
JD
7171 if (link_dp_get_encoding_format(verified_link_cap) == DP_128b_132b_ENCODING)
7172 amdgpu_dm_connector->num_modes +=
7173 drm_add_modes_noedid(connector, 1920, 1080);
85ee15d6
ML
7174 } else {
7175 amdgpu_dm_connector_ddc_get_modes(connector, edid);
7176 amdgpu_dm_connector_add_common_modes(encoder, connector);
a85ba005 7177 amdgpu_dm_connector_add_freesync_modes(connector, edid);
85ee15d6 7178 }
3e332d3a 7179 amdgpu_dm_fbc_init(connector);
5099114b 7180
c84dec2f 7181 return amdgpu_dm_connector->num_modes;
e7b07cee
HW
7182}
7183
3ee6b26b
AD
7184void amdgpu_dm_connector_init_helper(struct amdgpu_display_manager *dm,
7185 struct amdgpu_dm_connector *aconnector,
7186 int connector_type,
7187 struct dc_link *link,
7188 int link_index)
e7b07cee 7189{
1348969a 7190 struct amdgpu_device *adev = drm_to_adev(dm->ddev);
e7b07cee 7191
f04bee34
NK
7192 /*
7193 * Some of the properties below require access to state, like bpc.
7194 * Allocate some default initial connector state with our reset helper.
7195 */
7196 if (aconnector->base.funcs->reset)
7197 aconnector->base.funcs->reset(&aconnector->base);
7198
e7b07cee
HW
7199 aconnector->connector_id = link_index;
7200 aconnector->dc_link = link;
7201 aconnector->base.interlace_allowed = false;
7202 aconnector->base.doublescan_allowed = false;
7203 aconnector->base.stereo_allowed = false;
7204 aconnector->base.dpms = DRM_MODE_DPMS_OFF;
7205 aconnector->hpd.hpd = AMDGPU_HPD_NONE; /* not used */
6ce8f316 7206 aconnector->audio_inst = -1;
5b49da02
SJK
7207 aconnector->pack_sdp_v1_3 = false;
7208 aconnector->as_type = ADAPTIVE_SYNC_TYPE_NONE;
7209 memset(&aconnector->vsdb_info, 0, sizeof(aconnector->vsdb_info));
e7b07cee
HW
7210 mutex_init(&aconnector->hpd_lock);
7211
1f6010a9
DF
7212 /*
7213 * configure support HPD hot plug connector_>polled default value is 0
b830ebc9
HW
7214 * which means HPD hot plug not supported
7215 */
e7b07cee
HW
7216 switch (connector_type) {
7217 case DRM_MODE_CONNECTOR_HDMIA:
7218 aconnector->base.polled = DRM_CONNECTOR_POLL_HPD;
e7baae1c 7219 aconnector->base.ycbcr_420_allowed =
9ea59d5a 7220 link->link_enc->features.hdmi_ycbcr420_supported ? true : false;
e7b07cee
HW
7221 break;
7222 case DRM_MODE_CONNECTOR_DisplayPort:
7223 aconnector->base.polled = DRM_CONNECTOR_POLL_HPD;
d715c9a2 7224 link->link_enc = link_enc_cfg_get_link_enc(link);
7b201d53 7225 ASSERT(link->link_enc);
f6e03f80
JS
7226 if (link->link_enc)
7227 aconnector->base.ycbcr_420_allowed =
9ea59d5a 7228 link->link_enc->features.dp_ycbcr420_supported ? true : false;
e7b07cee
HW
7229 break;
7230 case DRM_MODE_CONNECTOR_DVID:
7231 aconnector->base.polled = DRM_CONNECTOR_POLL_HPD;
7232 break;
7233 default:
7234 break;
7235 }
7236
7237 drm_object_attach_property(&aconnector->base.base,
7238 dm->ddev->mode_config.scaling_mode_property,
7239 DRM_MODE_SCALE_NONE);
7240
7241 drm_object_attach_property(&aconnector->base.base,
7242 adev->mode_info.underscan_property,
7243 UNDERSCAN_OFF);
7244 drm_object_attach_property(&aconnector->base.base,
7245 adev->mode_info.underscan_hborder_property,
7246 0);
7247 drm_object_attach_property(&aconnector->base.base,
7248 adev->mode_info.underscan_vborder_property,
7249 0);
1825fd34 7250
f0127cb1 7251 if (!aconnector->mst_root)
8c61b31e 7252 drm_connector_attach_max_bpc_property(&aconnector->base, 8, 16);
1825fd34 7253
4a8ca46b 7254 /* This defaults to the max in the range, but we want 8bpc for non-edp. */
e47f1691 7255 aconnector->base.state->max_bpc = 16;
4a8ca46b 7256 aconnector->base.state->max_requested_bpc = aconnector->base.state->max_bpc;
e7b07cee 7257
c1ee92f9 7258 if (connector_type == DRM_MODE_CONNECTOR_eDP &&
5cb32419 7259 (dc_is_dmcu_initialized(adev->dm.dc) || adev->dm.dc->ctx->dmub_srv)) {
c1ee92f9
DF
7260 drm_object_attach_property(&aconnector->base.base,
7261 adev->mode_info.abm_level_property, 0);
7262 }
bb47de73
NK
7263
7264 if (connector_type == DRM_MODE_CONNECTOR_HDMIA ||
7fad8da1
NK
7265 connector_type == DRM_MODE_CONNECTOR_DisplayPort ||
7266 connector_type == DRM_MODE_CONNECTOR_eDP) {
e057b52c 7267 drm_connector_attach_hdr_output_metadata_property(&aconnector->base);
88694af9 7268
f0127cb1 7269 if (!aconnector->mst_root)
8c61b31e
JFZ
7270 drm_connector_attach_vrr_capable_property(&aconnector->base);
7271
0c8620d6 7272#ifdef CONFIG_DRM_AMD_DC_HDCP
e22bb562 7273 if (adev->dm.hdcp_workqueue)
53e108aa 7274 drm_connector_attach_content_protection_property(&aconnector->base, true);
0c8620d6 7275#endif
bb47de73 7276 }
e7b07cee
HW
7277}
7278
7578ecda
AD
7279static int amdgpu_dm_i2c_xfer(struct i2c_adapter *i2c_adap,
7280 struct i2c_msg *msgs, int num)
e7b07cee
HW
7281{
7282 struct amdgpu_i2c_adapter *i2c = i2c_get_adapdata(i2c_adap);
7283 struct ddc_service *ddc_service = i2c->ddc_service;
7284 struct i2c_command cmd;
7285 int i;
7286 int result = -EIO;
7287
b830ebc9 7288 cmd.payloads = kcalloc(num, sizeof(struct i2c_payload), GFP_KERNEL);
e7b07cee
HW
7289
7290 if (!cmd.payloads)
7291 return result;
7292
7293 cmd.number_of_payloads = num;
7294 cmd.engine = I2C_COMMAND_ENGINE_DEFAULT;
7295 cmd.speed = 100;
7296
7297 for (i = 0; i < num; i++) {
7298 cmd.payloads[i].write = !(msgs[i].flags & I2C_M_RD);
7299 cmd.payloads[i].address = msgs[i].addr;
7300 cmd.payloads[i].length = msgs[i].len;
7301 cmd.payloads[i].data = msgs[i].buf;
7302 }
7303
c85e6e54
DF
7304 if (dc_submit_i2c(
7305 ddc_service->ctx->dc,
22676bc5 7306 ddc_service->link->link_index,
e7b07cee
HW
7307 &cmd))
7308 result = num;
7309
7310 kfree(cmd.payloads);
7311 return result;
7312}
7313
7578ecda 7314static u32 amdgpu_dm_i2c_func(struct i2c_adapter *adap)
e7b07cee
HW
7315{
7316 return I2C_FUNC_I2C | I2C_FUNC_SMBUS_EMUL;
7317}
7318
7319static const struct i2c_algorithm amdgpu_dm_i2c_algo = {
7320 .master_xfer = amdgpu_dm_i2c_xfer,
7321 .functionality = amdgpu_dm_i2c_func,
7322};
7323
3ee6b26b
AD
7324static struct amdgpu_i2c_adapter *
7325create_i2c(struct ddc_service *ddc_service,
7326 int link_index,
7327 int *res)
e7b07cee
HW
7328{
7329 struct amdgpu_device *adev = ddc_service->ctx->driver_context;
7330 struct amdgpu_i2c_adapter *i2c;
7331
b830ebc9 7332 i2c = kzalloc(sizeof(struct amdgpu_i2c_adapter), GFP_KERNEL);
2a55f096
ES
7333 if (!i2c)
7334 return NULL;
e7b07cee
HW
7335 i2c->base.owner = THIS_MODULE;
7336 i2c->base.class = I2C_CLASS_DDC;
7337 i2c->base.dev.parent = &adev->pdev->dev;
7338 i2c->base.algo = &amdgpu_dm_i2c_algo;
b830ebc9 7339 snprintf(i2c->base.name, sizeof(i2c->base.name), "AMDGPU DM i2c hw bus %d", link_index);
e7b07cee
HW
7340 i2c_set_adapdata(&i2c->base, i2c);
7341 i2c->ddc_service = ddc_service;
7342
7343 return i2c;
7344}
7345
89fc8d4e 7346
1f6010a9
DF
7347/*
7348 * Note: this function assumes that dc_link_detect() was called for the
b830ebc9
HW
7349 * dc_link which will be represented by this aconnector.
7350 */
7578ecda
AD
7351static int amdgpu_dm_connector_init(struct amdgpu_display_manager *dm,
7352 struct amdgpu_dm_connector *aconnector,
ae67558b 7353 u32 link_index,
7578ecda 7354 struct amdgpu_encoder *aencoder)
e7b07cee
HW
7355{
7356 int res = 0;
7357 int connector_type;
7358 struct dc *dc = dm->dc;
7359 struct dc_link *link = dc_get_link_at_index(dc, link_index);
7360 struct amdgpu_i2c_adapter *i2c;
9a227d26
TSD
7361
7362 link->priv = aconnector;
e7b07cee 7363
f1ad2f5e 7364 DRM_DEBUG_DRIVER("%s()\n", __func__);
e7b07cee
HW
7365
7366 i2c = create_i2c(link->ddc, link->link_index, &res);
2a55f096
ES
7367 if (!i2c) {
7368 DRM_ERROR("Failed to create i2c adapter data\n");
7369 return -ENOMEM;
7370 }
7371
e7b07cee
HW
7372 aconnector->i2c = i2c;
7373 res = i2c_add_adapter(&i2c->base);
7374
7375 if (res) {
7376 DRM_ERROR("Failed to register hw i2c %d\n", link->link_index);
7377 goto out_free;
7378 }
7379
7380 connector_type = to_drm_connector_type(link->connector_signal);
7381
17165de2 7382 res = drm_connector_init_with_ddc(
e7b07cee
HW
7383 dm->ddev,
7384 &aconnector->base,
7385 &amdgpu_dm_connector_funcs,
17165de2
AP
7386 connector_type,
7387 &i2c->base);
e7b07cee
HW
7388
7389 if (res) {
7390 DRM_ERROR("connector_init failed\n");
7391 aconnector->connector_id = -1;
7392 goto out_free;
7393 }
7394
7395 drm_connector_helper_add(
7396 &aconnector->base,
7397 &amdgpu_dm_connector_helper_funcs);
7398
7399 amdgpu_dm_connector_init_helper(
7400 dm,
7401 aconnector,
7402 connector_type,
7403 link,
7404 link_index);
7405
cde4c44d 7406 drm_connector_attach_encoder(
e7b07cee
HW
7407 &aconnector->base, &aencoder->base);
7408
e7b07cee
HW
7409 if (connector_type == DRM_MODE_CONNECTOR_DisplayPort
7410 || connector_type == DRM_MODE_CONNECTOR_eDP)
7daec99f 7411 amdgpu_dm_initialize_dp_connector(dm, aconnector, link->link_index);
e7b07cee 7412
e7b07cee
HW
7413out_free:
7414 if (res) {
7415 kfree(i2c);
7416 aconnector->i2c = NULL;
7417 }
7418 return res;
7419}
7420
7421int amdgpu_dm_get_encoder_crtc_mask(struct amdgpu_device *adev)
7422{
7423 switch (adev->mode_info.num_crtc) {
7424 case 1:
7425 return 0x1;
7426 case 2:
7427 return 0x3;
7428 case 3:
7429 return 0x7;
7430 case 4:
7431 return 0xf;
7432 case 5:
7433 return 0x1f;
7434 case 6:
7435 default:
7436 return 0x3f;
7437 }
7438}
7439
7578ecda
AD
7440static int amdgpu_dm_encoder_init(struct drm_device *dev,
7441 struct amdgpu_encoder *aencoder,
7442 uint32_t link_index)
e7b07cee 7443{
1348969a 7444 struct amdgpu_device *adev = drm_to_adev(dev);
e7b07cee
HW
7445
7446 int res = drm_encoder_init(dev,
7447 &aencoder->base,
7448 &amdgpu_dm_encoder_funcs,
7449 DRM_MODE_ENCODER_TMDS,
7450 NULL);
7451
7452 aencoder->base.possible_crtcs = amdgpu_dm_get_encoder_crtc_mask(adev);
7453
7454 if (!res)
7455 aencoder->encoder_id = link_index;
7456 else
7457 aencoder->encoder_id = -1;
7458
7459 drm_encoder_helper_add(&aencoder->base, &amdgpu_dm_encoder_helper_funcs);
7460
7461 return res;
7462}
7463
3ee6b26b
AD
7464static void manage_dm_interrupts(struct amdgpu_device *adev,
7465 struct amdgpu_crtc *acrtc,
7466 bool enable)
e7b07cee
HW
7467{
7468 /*
8fe684e9
NK
7469 * We have no guarantee that the frontend index maps to the same
7470 * backend index - some even map to more than one.
7471 *
7472 * TODO: Use a different interrupt or check DC itself for the mapping.
e7b07cee
HW
7473 */
7474 int irq_type =
734dd01d 7475 amdgpu_display_crtc_idx_to_irq_type(
e7b07cee
HW
7476 adev,
7477 acrtc->crtc_id);
7478
7479 if (enable) {
7480 drm_crtc_vblank_on(&acrtc->base);
7481 amdgpu_irq_get(
7482 adev,
7483 &adev->pageflip_irq,
7484 irq_type);
86bc2219
WL
7485#if defined(CONFIG_DRM_AMD_SECURE_DISPLAY)
7486 amdgpu_irq_get(
7487 adev,
7488 &adev->vline0_irq,
7489 irq_type);
7490#endif
e7b07cee 7491 } else {
86bc2219
WL
7492#if defined(CONFIG_DRM_AMD_SECURE_DISPLAY)
7493 amdgpu_irq_put(
7494 adev,
7495 &adev->vline0_irq,
7496 irq_type);
7497#endif
e7b07cee
HW
7498 amdgpu_irq_put(
7499 adev,
7500 &adev->pageflip_irq,
7501 irq_type);
7502 drm_crtc_vblank_off(&acrtc->base);
7503 }
7504}
7505
8fe684e9
NK
7506static void dm_update_pflip_irq_state(struct amdgpu_device *adev,
7507 struct amdgpu_crtc *acrtc)
7508{
7509 int irq_type =
7510 amdgpu_display_crtc_idx_to_irq_type(adev, acrtc->crtc_id);
7511
7512 /**
7513 * This reads the current state for the IRQ and force reapplies
7514 * the setting to hardware.
7515 */
7516 amdgpu_irq_update(adev, &adev->pageflip_irq, irq_type);
7517}
7518
3ee6b26b
AD
7519static bool
7520is_scaling_state_different(const struct dm_connector_state *dm_state,
7521 const struct dm_connector_state *old_dm_state)
e7b07cee
HW
7522{
7523 if (dm_state->scaling != old_dm_state->scaling)
7524 return true;
7525 if (!dm_state->underscan_enable && old_dm_state->underscan_enable) {
7526 if (old_dm_state->underscan_hborder != 0 && old_dm_state->underscan_vborder != 0)
7527 return true;
7528 } else if (dm_state->underscan_enable && !old_dm_state->underscan_enable) {
7529 if (dm_state->underscan_hborder != 0 && dm_state->underscan_vborder != 0)
7530 return true;
b830ebc9
HW
7531 } else if (dm_state->underscan_hborder != old_dm_state->underscan_hborder ||
7532 dm_state->underscan_vborder != old_dm_state->underscan_vborder)
7533 return true;
e7b07cee
HW
7534 return false;
7535}
7536
0c8620d6 7537#ifdef CONFIG_DRM_AMD_DC_HDCP
e8fd3eeb 7538static bool is_content_protection_different(struct drm_crtc_state *new_crtc_state,
7539 struct drm_crtc_state *old_crtc_state,
7540 struct drm_connector_state *new_conn_state,
7541 struct drm_connector_state *old_conn_state,
7542 const struct drm_connector *connector,
7543 struct hdcp_workqueue *hdcp_w)
0c8620d6
BL
7544{
7545 struct amdgpu_dm_connector *aconnector = to_amdgpu_dm_connector(connector);
97f6c917 7546 struct dm_connector_state *dm_con_state = to_dm_connector_state(connector->state);
0c8620d6 7547
e8fd3eeb 7548 pr_debug("[HDCP_DM] connector->index: %x connect_status: %x dpms: %x\n",
7549 connector->index, connector->status, connector->dpms);
7550 pr_debug("[HDCP_DM] state protection old: %x new: %x\n",
7551 old_conn_state->content_protection, new_conn_state->content_protection);
7552
7553 if (old_crtc_state)
7554 pr_debug("[HDCP_DM] old crtc en: %x a: %x m: %x a-chg: %x c-chg: %x\n",
7555 old_crtc_state->enable,
7556 old_crtc_state->active,
7557 old_crtc_state->mode_changed,
7558 old_crtc_state->active_changed,
7559 old_crtc_state->connectors_changed);
7560
7561 if (new_crtc_state)
7562 pr_debug("[HDCP_DM] NEW crtc en: %x a: %x m: %x a-chg: %x c-chg: %x\n",
7563 new_crtc_state->enable,
7564 new_crtc_state->active,
7565 new_crtc_state->mode_changed,
7566 new_crtc_state->active_changed,
7567 new_crtc_state->connectors_changed);
7568
7569 /* hdcp content type change */
7570 if (old_conn_state->hdcp_content_type != new_conn_state->hdcp_content_type &&
7571 new_conn_state->content_protection != DRM_MODE_CONTENT_PROTECTION_UNDESIRED) {
7572 new_conn_state->content_protection = DRM_MODE_CONTENT_PROTECTION_DESIRED;
7573 pr_debug("[HDCP_DM] Type0/1 change %s :true\n", __func__);
53e108aa
BL
7574 return true;
7575 }
7576
e8fd3eeb 7577 /* CP is being re enabled, ignore this */
7578 if (old_conn_state->content_protection == DRM_MODE_CONTENT_PROTECTION_ENABLED &&
7579 new_conn_state->content_protection == DRM_MODE_CONTENT_PROTECTION_DESIRED) {
7580 if (new_crtc_state && new_crtc_state->mode_changed) {
7581 new_conn_state->content_protection = DRM_MODE_CONTENT_PROTECTION_DESIRED;
7582 pr_debug("[HDCP_DM] ENABLED->DESIRED & mode_changed %s :true\n", __func__);
7583 return true;
0b8f42ab 7584 }
e8fd3eeb 7585 new_conn_state->content_protection = DRM_MODE_CONTENT_PROTECTION_ENABLED;
7586 pr_debug("[HDCP_DM] ENABLED -> DESIRED %s :false\n", __func__);
0c8620d6
BL
7587 return false;
7588 }
7589
31c0ed90
BL
7590 /* S3 resume case, since old state will always be 0 (UNDESIRED) and the restored state will be ENABLED
7591 *
7592 * Handles: UNDESIRED -> ENABLED
7593 */
e8fd3eeb 7594 if (old_conn_state->content_protection == DRM_MODE_CONTENT_PROTECTION_UNDESIRED &&
7595 new_conn_state->content_protection == DRM_MODE_CONTENT_PROTECTION_ENABLED)
7596 new_conn_state->content_protection = DRM_MODE_CONTENT_PROTECTION_DESIRED;
0c8620d6 7597
0d9a947b
QZ
7598 /* Stream removed and re-enabled
7599 *
7600 * Can sometimes overlap with the HPD case,
7601 * thus set update_hdcp to false to avoid
7602 * setting HDCP multiple times.
7603 *
7604 * Handles: DESIRED -> DESIRED (Special case)
7605 */
e8fd3eeb 7606 if (!(old_conn_state->crtc && old_conn_state->crtc->enabled) &&
7607 new_conn_state->crtc && new_conn_state->crtc->enabled &&
0d9a947b
QZ
7608 connector->state->content_protection == DRM_MODE_CONTENT_PROTECTION_DESIRED) {
7609 dm_con_state->update_hdcp = false;
e8fd3eeb 7610 pr_debug("[HDCP_DM] DESIRED->DESIRED (Stream removed and re-enabled) %s :true\n",
7611 __func__);
0d9a947b
QZ
7612 return true;
7613 }
7614
7615 /* Hot-plug, headless s3, dpms
7616 *
7617 * Only start HDCP if the display is connected/enabled.
7618 * update_hdcp flag will be set to false until the next
7619 * HPD comes in.
31c0ed90
BL
7620 *
7621 * Handles: DESIRED -> DESIRED (Special case)
0c8620d6 7622 */
e8fd3eeb 7623 if (dm_con_state->update_hdcp &&
7624 new_conn_state->content_protection == DRM_MODE_CONTENT_PROTECTION_DESIRED &&
7625 connector->dpms == DRM_MODE_DPMS_ON && aconnector->dc_sink != NULL) {
97f6c917 7626 dm_con_state->update_hdcp = false;
e8fd3eeb 7627 pr_debug("[HDCP_DM] DESIRED->DESIRED (Hot-plug, headless s3, dpms) %s :true\n",
7628 __func__);
0c8620d6 7629 return true;
97f6c917 7630 }
0c8620d6 7631
e8fd3eeb 7632 if (old_conn_state->content_protection == new_conn_state->content_protection) {
7633 if (new_conn_state->content_protection >= DRM_MODE_CONTENT_PROTECTION_DESIRED) {
7634 if (new_crtc_state && new_crtc_state->mode_changed) {
7635 pr_debug("[HDCP_DM] DESIRED->DESIRED or ENABLE->ENABLE mode_change %s :true\n",
7636 __func__);
7637 return true;
0b8f42ab 7638 }
e8fd3eeb 7639 pr_debug("[HDCP_DM] DESIRED->DESIRED & ENABLE->ENABLE %s :false\n",
7640 __func__);
7641 return false;
0b8f42ab 7642 }
e8fd3eeb 7643
7644 pr_debug("[HDCP_DM] UNDESIRED->UNDESIRED %s :false\n", __func__);
0c8620d6 7645 return false;
e8fd3eeb 7646 }
0c8620d6 7647
e8fd3eeb 7648 if (new_conn_state->content_protection != DRM_MODE_CONTENT_PROTECTION_ENABLED) {
7649 pr_debug("[HDCP_DM] UNDESIRED->DESIRED or DESIRED->UNDESIRED or ENABLED->UNDESIRED %s :true\n",
7650 __func__);
0c8620d6 7651 return true;
e8fd3eeb 7652 }
0c8620d6 7653
e8fd3eeb 7654 pr_debug("[HDCP_DM] DESIRED->ENABLED %s :false\n", __func__);
0c8620d6
BL
7655 return false;
7656}
0c8620d6 7657#endif
e8fd3eeb 7658
3ee6b26b
AD
7659static void remove_stream(struct amdgpu_device *adev,
7660 struct amdgpu_crtc *acrtc,
7661 struct dc_stream_state *stream)
e7b07cee
HW
7662{
7663 /* this is the update mode case */
e7b07cee
HW
7664
7665 acrtc->otg_inst = -1;
7666 acrtc->enabled = false;
7667}
7668
e7b07cee
HW
7669static void prepare_flip_isr(struct amdgpu_crtc *acrtc)
7670{
7671
7672 assert_spin_locked(&acrtc->base.dev->event_lock);
7673 WARN_ON(acrtc->event);
7674
7675 acrtc->event = acrtc->base.state->event;
7676
7677 /* Set the flip status */
7678 acrtc->pflip_status = AMDGPU_FLIP_SUBMITTED;
7679
7680 /* Mark this event as consumed */
7681 acrtc->base.state->event = NULL;
7682
cb2318b7
VL
7683 DC_LOG_PFLIP("crtc:%d, pflip_stat:AMDGPU_FLIP_SUBMITTED\n",
7684 acrtc->crtc_id);
e7b07cee
HW
7685}
7686
bb47de73
NK
7687static void update_freesync_state_on_stream(
7688 struct amdgpu_display_manager *dm,
7689 struct dm_crtc_state *new_crtc_state,
180db303
NK
7690 struct dc_stream_state *new_stream,
7691 struct dc_plane_state *surface,
7692 u32 flip_timestamp_in_us)
bb47de73 7693{
09aef2c4 7694 struct mod_vrr_params vrr_params;
bb47de73 7695 struct dc_info_packet vrr_infopacket = {0};
09aef2c4 7696 struct amdgpu_device *adev = dm->adev;
585d450c 7697 struct amdgpu_crtc *acrtc = to_amdgpu_crtc(new_crtc_state->base.crtc);
09aef2c4 7698 unsigned long flags;
4cda3243 7699 bool pack_sdp_v1_3 = false;
5b49da02
SJK
7700 struct amdgpu_dm_connector *aconn;
7701 enum vrr_packet_type packet_type = PACKET_TYPE_VRR;
bb47de73
NK
7702
7703 if (!new_stream)
7704 return;
7705
7706 /*
7707 * TODO: Determine why min/max totals and vrefresh can be 0 here.
7708 * For now it's sufficient to just guard against these conditions.
7709 */
7710
7711 if (!new_stream->timing.h_total || !new_stream->timing.v_total)
7712 return;
7713
4a580877 7714 spin_lock_irqsave(&adev_to_drm(adev)->event_lock, flags);
585d450c 7715 vrr_params = acrtc->dm_irq_params.vrr_params;
09aef2c4 7716
180db303
NK
7717 if (surface) {
7718 mod_freesync_handle_preflip(
7719 dm->freesync_module,
7720 surface,
7721 new_stream,
7722 flip_timestamp_in_us,
7723 &vrr_params);
09aef2c4
MK
7724
7725 if (adev->family < AMDGPU_FAMILY_AI &&
7726 amdgpu_dm_vrr_active(new_crtc_state)) {
7727 mod_freesync_handle_v_update(dm->freesync_module,
7728 new_stream, &vrr_params);
e63e2491
EB
7729
7730 /* Need to call this before the frame ends. */
7731 dc_stream_adjust_vmin_vmax(dm->dc,
7732 new_crtc_state->stream,
7733 &vrr_params.adjust);
09aef2c4 7734 }
180db303 7735 }
bb47de73 7736
5b49da02
SJK
7737 aconn = (struct amdgpu_dm_connector *)new_stream->dm_stream_context;
7738
7739 if (aconn && aconn->as_type == FREESYNC_TYPE_PCON_IN_WHITELIST) {
7740 pack_sdp_v1_3 = aconn->pack_sdp_v1_3;
7741
7742 if (aconn->vsdb_info.amd_vsdb_version == 1)
7743 packet_type = PACKET_TYPE_FS_V1;
7744 else if (aconn->vsdb_info.amd_vsdb_version == 2)
7745 packet_type = PACKET_TYPE_FS_V2;
7746 else if (aconn->vsdb_info.amd_vsdb_version == 3)
7747 packet_type = PACKET_TYPE_FS_V3;
7748
7749 mod_build_adaptive_sync_infopacket(new_stream, aconn->as_type, NULL,
7750 &new_stream->adaptive_sync_infopacket);
7751 }
7752
bb47de73
NK
7753 mod_freesync_build_vrr_infopacket(
7754 dm->freesync_module,
7755 new_stream,
180db303 7756 &vrr_params,
5b49da02 7757 packet_type,
ecd0136b 7758 TRANSFER_FUNC_UNKNOWN,
4cda3243
MT
7759 &vrr_infopacket,
7760 pack_sdp_v1_3);
bb47de73 7761
8a48b44c 7762 new_crtc_state->freesync_vrr_info_changed |=
bb47de73
NK
7763 (memcmp(&new_crtc_state->vrr_infopacket,
7764 &vrr_infopacket,
7765 sizeof(vrr_infopacket)) != 0);
7766
585d450c 7767 acrtc->dm_irq_params.vrr_params = vrr_params;
bb47de73
NK
7768 new_crtc_state->vrr_infopacket = vrr_infopacket;
7769
bb47de73 7770 new_stream->vrr_infopacket = vrr_infopacket;
7eaef116 7771 new_stream->allow_freesync = mod_freesync_get_freesync_enabled(&vrr_params);
bb47de73
NK
7772
7773 if (new_crtc_state->freesync_vrr_info_changed)
7774 DRM_DEBUG_KMS("VRR packet update: crtc=%u enabled=%d state=%d",
7775 new_crtc_state->base.crtc->base.id,
7776 (int)new_crtc_state->base.vrr_enabled,
180db303 7777 (int)vrr_params.state);
09aef2c4 7778
4a580877 7779 spin_unlock_irqrestore(&adev_to_drm(adev)->event_lock, flags);
bb47de73
NK
7780}
7781
585d450c 7782static void update_stream_irq_parameters(
e854194c
MK
7783 struct amdgpu_display_manager *dm,
7784 struct dm_crtc_state *new_crtc_state)
7785{
7786 struct dc_stream_state *new_stream = new_crtc_state->stream;
09aef2c4 7787 struct mod_vrr_params vrr_params;
e854194c 7788 struct mod_freesync_config config = new_crtc_state->freesync_config;
09aef2c4 7789 struct amdgpu_device *adev = dm->adev;
585d450c 7790 struct amdgpu_crtc *acrtc = to_amdgpu_crtc(new_crtc_state->base.crtc);
09aef2c4 7791 unsigned long flags;
e854194c
MK
7792
7793 if (!new_stream)
7794 return;
7795
7796 /*
7797 * TODO: Determine why min/max totals and vrefresh can be 0 here.
7798 * For now it's sufficient to just guard against these conditions.
7799 */
7800 if (!new_stream->timing.h_total || !new_stream->timing.v_total)
7801 return;
7802
4a580877 7803 spin_lock_irqsave(&adev_to_drm(adev)->event_lock, flags);
585d450c 7804 vrr_params = acrtc->dm_irq_params.vrr_params;
09aef2c4 7805
e854194c
MK
7806 if (new_crtc_state->vrr_supported &&
7807 config.min_refresh_in_uhz &&
7808 config.max_refresh_in_uhz) {
a85ba005
NC
7809 /*
7810 * if freesync compatible mode was set, config.state will be set
7811 * in atomic check
7812 */
7813 if (config.state == VRR_STATE_ACTIVE_FIXED && config.fixed_refresh_in_uhz &&
7814 (!drm_atomic_crtc_needs_modeset(&new_crtc_state->base) ||
7815 new_crtc_state->freesync_config.state == VRR_STATE_ACTIVE_FIXED)) {
7816 vrr_params.max_refresh_in_uhz = config.max_refresh_in_uhz;
7817 vrr_params.min_refresh_in_uhz = config.min_refresh_in_uhz;
7818 vrr_params.fixed_refresh_in_uhz = config.fixed_refresh_in_uhz;
7819 vrr_params.state = VRR_STATE_ACTIVE_FIXED;
7820 } else {
7821 config.state = new_crtc_state->base.vrr_enabled ?
7822 VRR_STATE_ACTIVE_VARIABLE :
7823 VRR_STATE_INACTIVE;
7824 }
e854194c
MK
7825 } else {
7826 config.state = VRR_STATE_UNSUPPORTED;
7827 }
7828
7829 mod_freesync_build_vrr_params(dm->freesync_module,
7830 new_stream,
7831 &config, &vrr_params);
7832
585d450c
AP
7833 new_crtc_state->freesync_config = config;
7834 /* Copy state for access from DM IRQ handler */
7835 acrtc->dm_irq_params.freesync_config = config;
7836 acrtc->dm_irq_params.active_planes = new_crtc_state->active_planes;
7837 acrtc->dm_irq_params.vrr_params = vrr_params;
4a580877 7838 spin_unlock_irqrestore(&adev_to_drm(adev)->event_lock, flags);
e854194c
MK
7839}
7840
66b0c973
MK
7841static void amdgpu_dm_handle_vrr_transition(struct dm_crtc_state *old_state,
7842 struct dm_crtc_state *new_state)
7843{
7844 bool old_vrr_active = amdgpu_dm_vrr_active(old_state);
7845 bool new_vrr_active = amdgpu_dm_vrr_active(new_state);
7846
7847 if (!old_vrr_active && new_vrr_active) {
7848 /* Transition VRR inactive -> active:
7849 * While VRR is active, we must not disable vblank irq, as a
7850 * reenable after disable would compute bogus vblank/pflip
7851 * timestamps if it likely happened inside display front-porch.
d2574c33
MK
7852 *
7853 * We also need vupdate irq for the actual core vblank handling
7854 * at end of vblank.
66b0c973 7855 */
8799c0be
YL
7856 WARN_ON(dm_set_vupdate_irq(new_state->base.crtc, true) != 0);
7857 WARN_ON(drm_crtc_vblank_get(new_state->base.crtc) != 0);
66b0c973
MK
7858 DRM_DEBUG_DRIVER("%s: crtc=%u VRR off->on: Get vblank ref\n",
7859 __func__, new_state->base.crtc->base.id);
7860 } else if (old_vrr_active && !new_vrr_active) {
7861 /* Transition VRR active -> inactive:
7862 * Allow vblank irq disable again for fixed refresh rate.
7863 */
8799c0be 7864 WARN_ON(dm_set_vupdate_irq(new_state->base.crtc, false) != 0);
66b0c973
MK
7865 drm_crtc_vblank_put(new_state->base.crtc);
7866 DRM_DEBUG_DRIVER("%s: crtc=%u VRR on->off: Drop vblank ref\n",
7867 __func__, new_state->base.crtc->base.id);
7868 }
7869}
7870
8ad27806
NK
7871static void amdgpu_dm_commit_cursors(struct drm_atomic_state *state)
7872{
7873 struct drm_plane *plane;
5760dcb9 7874 struct drm_plane_state *old_plane_state;
8ad27806
NK
7875 int i;
7876
7877 /*
7878 * TODO: Make this per-stream so we don't issue redundant updates for
7879 * commits with multiple streams.
7880 */
5760dcb9 7881 for_each_old_plane_in_state(state, plane, old_plane_state, i)
8ad27806
NK
7882 if (plane->type == DRM_PLANE_TYPE_CURSOR)
7883 handle_cursor_update(plane, old_plane_state);
7884}
7885
3be5262e 7886static void amdgpu_dm_commit_planes(struct drm_atomic_state *state,
eb3dc897 7887 struct dc_state *dc_state,
3ee6b26b
AD
7888 struct drm_device *dev,
7889 struct amdgpu_display_manager *dm,
7890 struct drm_crtc *pcrtc,
420cd472 7891 bool wait_for_vblank)
e7b07cee 7892{
ae67558b 7893 u32 i;
d6ed6d0d 7894 u64 timestamp_ns = ktime_get_ns();
e7b07cee 7895 struct drm_plane *plane;
0bc9706d 7896 struct drm_plane_state *old_plane_state, *new_plane_state;
e7b07cee 7897 struct amdgpu_crtc *acrtc_attach = to_amdgpu_crtc(pcrtc);
0bc9706d
LSL
7898 struct drm_crtc_state *new_pcrtc_state =
7899 drm_atomic_get_new_crtc_state(state, pcrtc);
7900 struct dm_crtc_state *acrtc_state = to_dm_crtc_state(new_pcrtc_state);
44d09c6a
HW
7901 struct dm_crtc_state *dm_old_crtc_state =
7902 to_dm_crtc_state(drm_atomic_get_old_crtc_state(state, pcrtc));
74aa7bd4 7903 int planes_count = 0, vpos, hpos;
e7b07cee 7904 unsigned long flags;
ae67558b 7905 u32 target_vblank, last_flip_vblank;
fdd1fe57 7906 bool vrr_active = amdgpu_dm_vrr_active(acrtc_state);
cc79950b 7907 bool cursor_update = false;
74aa7bd4 7908 bool pflip_present = false;
d6ed6d0d 7909 bool dirty_rects_changed = false;
bc7f670e
DF
7910 struct {
7911 struct dc_surface_update surface_updates[MAX_SURFACES];
7912 struct dc_plane_info plane_infos[MAX_SURFACES];
7913 struct dc_scaling_info scaling_infos[MAX_SURFACES];
74aa7bd4 7914 struct dc_flip_addrs flip_addrs[MAX_SURFACES];
bc7f670e 7915 struct dc_stream_update stream_update;
74aa7bd4 7916 } *bundle;
bc7f670e 7917
74aa7bd4 7918 bundle = kzalloc(sizeof(*bundle), GFP_KERNEL);
8a48b44c 7919
74aa7bd4
DF
7920 if (!bundle) {
7921 dm_error("Failed to allocate update bundle\n");
4b510503
NK
7922 goto cleanup;
7923 }
e7b07cee 7924
8ad27806
NK
7925 /*
7926 * Disable the cursor first if we're disabling all the planes.
7927 * It'll remain on the screen after the planes are re-enabled
7928 * if we don't.
7929 */
7930 if (acrtc_state->active_planes == 0)
7931 amdgpu_dm_commit_cursors(state);
7932
e7b07cee 7933 /* update planes when needed */
efc8278e 7934 for_each_oldnew_plane_in_state(state, plane, old_plane_state, new_plane_state, i) {
0bc9706d 7935 struct drm_crtc *crtc = new_plane_state->crtc;
f5ba60fe 7936 struct drm_crtc_state *new_crtc_state;
0bc9706d 7937 struct drm_framebuffer *fb = new_plane_state->fb;
6eed95b0 7938 struct amdgpu_framebuffer *afb = (struct amdgpu_framebuffer *)fb;
34bafd27 7939 bool plane_needs_flip;
c7af5f77 7940 struct dc_plane_state *dc_plane;
54d76575 7941 struct dm_plane_state *dm_new_plane_state = to_dm_plane_state(new_plane_state);
e7b07cee 7942
80c218d5 7943 /* Cursor plane is handled after stream updates */
cc79950b
MD
7944 if (plane->type == DRM_PLANE_TYPE_CURSOR) {
7945 if ((fb && crtc == pcrtc) ||
7946 (old_plane_state->fb && old_plane_state->crtc == pcrtc))
7947 cursor_update = true;
7948
e7b07cee 7949 continue;
cc79950b 7950 }
e7b07cee 7951
f5ba60fe
DD
7952 if (!fb || !crtc || pcrtc != crtc)
7953 continue;
7954
7955 new_crtc_state = drm_atomic_get_new_crtc_state(state, crtc);
7956 if (!new_crtc_state->active)
e7b07cee
HW
7957 continue;
7958
bc7f670e 7959 dc_plane = dm_new_plane_state->dc_state;
e7b07cee 7960
74aa7bd4 7961 bundle->surface_updates[planes_count].surface = dc_plane;
bc7f670e 7962 if (new_pcrtc_state->color_mgmt_changed) {
74aa7bd4
DF
7963 bundle->surface_updates[planes_count].gamma = dc_plane->gamma_correction;
7964 bundle->surface_updates[planes_count].in_transfer_func = dc_plane->in_transfer_func;
44efb784 7965 bundle->surface_updates[planes_count].gamut_remap_matrix = &dc_plane->gamut_remap_matrix;
bc7f670e 7966 }
8a48b44c 7967
4375d625 7968 fill_dc_scaling_info(dm->adev, new_plane_state,
695af5f9 7969 &bundle->scaling_infos[planes_count]);
8a48b44c 7970
695af5f9
NK
7971 bundle->surface_updates[planes_count].scaling_info =
7972 &bundle->scaling_infos[planes_count];
8a48b44c 7973
f5031000 7974 plane_needs_flip = old_plane_state->fb && new_plane_state->fb;
8a48b44c 7975
f5031000 7976 pflip_present = pflip_present || plane_needs_flip;
8a48b44c 7977
f5031000
DF
7978 if (!plane_needs_flip) {
7979 planes_count += 1;
7980 continue;
7981 }
8a48b44c 7982
695af5f9 7983 fill_dc_plane_info_and_addr(
8ce5d842 7984 dm->adev, new_plane_state,
6eed95b0 7985 afb->tiling_flags,
695af5f9 7986 &bundle->plane_infos[planes_count],
87b7ebc2 7987 &bundle->flip_addrs[planes_count].address,
6eed95b0 7988 afb->tmz_surface, false);
87b7ebc2 7989
9f07550b 7990 drm_dbg_state(state->dev, "plane: id=%d dcc_en=%d\n",
87b7ebc2
RS
7991 new_plane_state->plane->index,
7992 bundle->plane_infos[planes_count].dcc.enable);
695af5f9
NK
7993
7994 bundle->surface_updates[planes_count].plane_info =
7995 &bundle->plane_infos[planes_count];
8a48b44c 7996
d6ed6d0d 7997 if (acrtc_state->stream->link->psr_settings.psr_feature_enabled) {
d852871c
HM
7998 fill_dc_dirty_rects(plane, old_plane_state,
7999 new_plane_state, new_crtc_state,
d6ed6d0d
TC
8000 &bundle->flip_addrs[planes_count],
8001 &dirty_rects_changed);
8002
8003 /*
8004 * If the dirty regions changed, PSR-SU need to be disabled temporarily
8005 * and enabled it again after dirty regions are stable to avoid video glitch.
8006 * PSR-SU will be enabled in vblank_control_worker() if user pause the video
8007 * during the PSR-SU was disabled.
8008 */
8009 if (acrtc_state->stream->link->psr_settings.psr_version >= DC_PSR_VERSION_SU_1 &&
8010 acrtc_attach->dm_irq_params.allow_psr_entry &&
8011#ifdef CONFIG_DRM_AMD_SECURE_DISPLAY
8012 !amdgpu_dm_crc_window_is_activated(acrtc_state->base.crtc) &&
8013#endif
8014 dirty_rects_changed) {
8015 mutex_lock(&dm->dc_lock);
8016 acrtc_state->stream->link->psr_settings.psr_dirty_rects_change_timestamp_ns =
8017 timestamp_ns;
8018 if (acrtc_state->stream->link->psr_settings.psr_allow_active)
8019 amdgpu_dm_psr_disable(acrtc_state->stream);
8020 mutex_unlock(&dm->dc_lock);
8021 }
8022 }
7cc191ee 8023
caff0e66
NK
8024 /*
8025 * Only allow immediate flips for fast updates that don't
8026 * change FB pitch, DCC state, rotation or mirroing.
8027 */
f5031000 8028 bundle->flip_addrs[planes_count].flip_immediate =
4d85f45c 8029 crtc->state->async_flip &&
caff0e66 8030 acrtc_state->update_type == UPDATE_TYPE_FAST;
8a48b44c 8031
f5031000
DF
8032 timestamp_ns = ktime_get_ns();
8033 bundle->flip_addrs[planes_count].flip_timestamp_in_us = div_u64(timestamp_ns, 1000);
8034 bundle->surface_updates[planes_count].flip_addr = &bundle->flip_addrs[planes_count];
8035 bundle->surface_updates[planes_count].surface = dc_plane;
8a48b44c 8036
f5031000
DF
8037 if (!bundle->surface_updates[planes_count].surface) {
8038 DRM_ERROR("No surface for CRTC: id=%d\n",
8039 acrtc_attach->crtc_id);
8040 continue;
bc7f670e
DF
8041 }
8042
f5031000
DF
8043 if (plane == pcrtc->primary)
8044 update_freesync_state_on_stream(
8045 dm,
8046 acrtc_state,
8047 acrtc_state->stream,
8048 dc_plane,
8049 bundle->flip_addrs[planes_count].flip_timestamp_in_us);
bc7f670e 8050
9f07550b 8051 drm_dbg_state(state->dev, "%s Flipping to hi: 0x%x, low: 0x%x\n",
f5031000
DF
8052 __func__,
8053 bundle->flip_addrs[planes_count].address.grph.addr.high_part,
8054 bundle->flip_addrs[planes_count].address.grph.addr.low_part);
bc7f670e
DF
8055
8056 planes_count += 1;
8057
8a48b44c
DF
8058 }
8059
74aa7bd4 8060 if (pflip_present) {
634092b1
MK
8061 if (!vrr_active) {
8062 /* Use old throttling in non-vrr fixed refresh rate mode
8063 * to keep flip scheduling based on target vblank counts
8064 * working in a backwards compatible way, e.g., for
8065 * clients using the GLX_OML_sync_control extension or
8066 * DRI3/Present extension with defined target_msc.
8067 */
e3eff4b5 8068 last_flip_vblank = amdgpu_get_vblank_counter_kms(pcrtc);
634092b1
MK
8069 }
8070 else {
8071 /* For variable refresh rate mode only:
8072 * Get vblank of last completed flip to avoid > 1 vrr
8073 * flips per video frame by use of throttling, but allow
8074 * flip programming anywhere in the possibly large
8075 * variable vrr vblank interval for fine-grained flip
8076 * timing control and more opportunity to avoid stutter
8077 * on late submission of flips.
8078 */
8079 spin_lock_irqsave(&pcrtc->dev->event_lock, flags);
5d1c59c4 8080 last_flip_vblank = acrtc_attach->dm_irq_params.last_flip_vblank;
634092b1
MK
8081 spin_unlock_irqrestore(&pcrtc->dev->event_lock, flags);
8082 }
8083
fdd1fe57 8084 target_vblank = last_flip_vblank + wait_for_vblank;
8a48b44c
DF
8085
8086 /*
8087 * Wait until we're out of the vertical blank period before the one
8088 * targeted by the flip
8089 */
8090 while ((acrtc_attach->enabled &&
8091 (amdgpu_display_get_crtc_scanoutpos(dm->ddev, acrtc_attach->crtc_id,
8092 0, &vpos, &hpos, NULL,
8093 NULL, &pcrtc->hwmode)
8094 & (DRM_SCANOUTPOS_VALID | DRM_SCANOUTPOS_IN_VBLANK)) ==
8095 (DRM_SCANOUTPOS_VALID | DRM_SCANOUTPOS_IN_VBLANK) &&
8096 (int)(target_vblank -
e3eff4b5 8097 amdgpu_get_vblank_counter_kms(pcrtc)) > 0)) {
8a48b44c
DF
8098 usleep_range(1000, 1100);
8099 }
8100
8fe684e9
NK
8101 /**
8102 * Prepare the flip event for the pageflip interrupt to handle.
8103 *
8104 * This only works in the case where we've already turned on the
8105 * appropriate hardware blocks (eg. HUBP) so in the transition case
8106 * from 0 -> n planes we have to skip a hardware generated event
8107 * and rely on sending it from software.
8108 */
8109 if (acrtc_attach->base.state->event &&
10a36226 8110 acrtc_state->active_planes > 0) {
8a48b44c
DF
8111 drm_crtc_vblank_get(pcrtc);
8112
8113 spin_lock_irqsave(&pcrtc->dev->event_lock, flags);
8114
8115 WARN_ON(acrtc_attach->pflip_status != AMDGPU_FLIP_NONE);
8116 prepare_flip_isr(acrtc_attach);
8117
8118 spin_unlock_irqrestore(&pcrtc->dev->event_lock, flags);
8119 }
8120
8121 if (acrtc_state->stream) {
8a48b44c 8122 if (acrtc_state->freesync_vrr_info_changed)
74aa7bd4 8123 bundle->stream_update.vrr_infopacket =
8a48b44c 8124 &acrtc_state->stream->vrr_infopacket;
e7b07cee 8125 }
cc79950b
MD
8126 } else if (cursor_update && acrtc_state->active_planes > 0 &&
8127 acrtc_attach->base.state->event) {
8128 drm_crtc_vblank_get(pcrtc);
8129
8130 spin_lock_irqsave(&pcrtc->dev->event_lock, flags);
8131
8132 acrtc_attach->event = acrtc_attach->base.state->event;
8133 acrtc_attach->base.state->event = NULL;
8134
8135 spin_unlock_irqrestore(&pcrtc->dev->event_lock, flags);
e7b07cee
HW
8136 }
8137
bc92c065 8138 /* Update the planes if changed or disable if we don't have any. */
ed9656fb
ES
8139 if ((planes_count || acrtc_state->active_planes == 0) &&
8140 acrtc_state->stream) {
58aa1c50
NK
8141 /*
8142 * If PSR or idle optimizations are enabled then flush out
8143 * any pending work before hardware programming.
8144 */
06dd1888
NK
8145 if (dm->vblank_control_workqueue)
8146 flush_workqueue(dm->vblank_control_workqueue);
58aa1c50 8147
b6e881c9 8148 bundle->stream_update.stream = acrtc_state->stream;
bc7f670e 8149 if (new_pcrtc_state->mode_changed) {
74aa7bd4
DF
8150 bundle->stream_update.src = acrtc_state->stream->src;
8151 bundle->stream_update.dst = acrtc_state->stream->dst;
e7b07cee
HW
8152 }
8153
cf020d49
NK
8154 if (new_pcrtc_state->color_mgmt_changed) {
8155 /*
8156 * TODO: This isn't fully correct since we've actually
8157 * already modified the stream in place.
8158 */
8159 bundle->stream_update.gamut_remap =
8160 &acrtc_state->stream->gamut_remap_matrix;
8161 bundle->stream_update.output_csc_transform =
8162 &acrtc_state->stream->csc_color_matrix;
8163 bundle->stream_update.out_transfer_func =
8164 acrtc_state->stream->out_transfer_func;
8165 }
bc7f670e 8166
8a48b44c 8167 acrtc_state->stream->abm_level = acrtc_state->abm_level;
bc7f670e 8168 if (acrtc_state->abm_level != dm_old_crtc_state->abm_level)
74aa7bd4 8169 bundle->stream_update.abm_level = &acrtc_state->abm_level;
44d09c6a 8170
e63e2491
EB
8171 /*
8172 * If FreeSync state on the stream has changed then we need to
8173 * re-adjust the min/max bounds now that DC doesn't handle this
8174 * as part of commit.
8175 */
a85ba005 8176 if (is_dc_timing_adjust_needed(dm_old_crtc_state, acrtc_state)) {
e63e2491
EB
8177 spin_lock_irqsave(&pcrtc->dev->event_lock, flags);
8178 dc_stream_adjust_vmin_vmax(
8179 dm->dc, acrtc_state->stream,
585d450c 8180 &acrtc_attach->dm_irq_params.vrr_params.adjust);
e63e2491
EB
8181 spin_unlock_irqrestore(&pcrtc->dev->event_lock, flags);
8182 }
bc7f670e 8183 mutex_lock(&dm->dc_lock);
8c322309 8184 if ((acrtc_state->update_type > UPDATE_TYPE_FAST) &&
d1ebfdd8 8185 acrtc_state->stream->link->psr_settings.psr_allow_active)
8c322309
RL
8186 amdgpu_dm_psr_disable(acrtc_state->stream);
8187
bc7f670e 8188 dc_commit_updates_for_stream(dm->dc,
74aa7bd4 8189 bundle->surface_updates,
bc7f670e
DF
8190 planes_count,
8191 acrtc_state->stream,
efc8278e
AJ
8192 &bundle->stream_update,
8193 dc_state);
8c322309 8194
8fe684e9
NK
8195 /**
8196 * Enable or disable the interrupts on the backend.
8197 *
8198 * Most pipes are put into power gating when unused.
8199 *
8200 * When power gating is enabled on a pipe we lose the
8201 * interrupt enablement state when power gating is disabled.
8202 *
8203 * So we need to update the IRQ control state in hardware
8204 * whenever the pipe turns on (since it could be previously
8205 * power gated) or off (since some pipes can't be power gated
8206 * on some ASICs).
8207 */
8208 if (dm_old_crtc_state->active_planes != acrtc_state->active_planes)
1348969a
LT
8209 dm_update_pflip_irq_state(drm_to_adev(dev),
8210 acrtc_attach);
8fe684e9 8211
8c322309 8212 if ((acrtc_state->update_type > UPDATE_TYPE_FAST) &&
1cfbbdde 8213 acrtc_state->stream->link->psr_settings.psr_version != DC_PSR_VERSION_UNSUPPORTED &&
d1ebfdd8 8214 !acrtc_state->stream->link->psr_settings.psr_feature_enabled)
8c322309 8215 amdgpu_dm_link_setup_psr(acrtc_state->stream);
58aa1c50
NK
8216
8217 /* Decrement skip count when PSR is enabled and we're doing fast updates. */
8218 if (acrtc_state->update_type == UPDATE_TYPE_FAST &&
8219 acrtc_state->stream->link->psr_settings.psr_feature_enabled) {
8220 struct amdgpu_dm_connector *aconn =
8221 (struct amdgpu_dm_connector *)acrtc_state->stream->dm_stream_context;
1a365683
RL
8222
8223 if (aconn->psr_skip_count > 0)
8224 aconn->psr_skip_count--;
58aa1c50
NK
8225
8226 /* Allow PSR when skip count is 0. */
8227 acrtc_attach->dm_irq_params.allow_psr_entry = !aconn->psr_skip_count;
7cc191ee
LL
8228
8229 /*
8230 * If sink supports PSR SU, there is no need to rely on
8231 * a vblank event disable request to enable PSR. PSR SU
8232 * can be enabled immediately once OS demonstrates an
8233 * adequate number of fast atomic commits to notify KMD
8234 * of update events. See `vblank_control_worker()`.
8235 */
8236 if (acrtc_state->stream->link->psr_settings.psr_version >= DC_PSR_VERSION_SU_1 &&
8237 acrtc_attach->dm_irq_params.allow_psr_entry &&
c0459bdd
AL
8238#ifdef CONFIG_DRM_AMD_SECURE_DISPLAY
8239 !amdgpu_dm_crc_window_is_activated(acrtc_state->base.crtc) &&
8240#endif
d6ed6d0d
TC
8241 !acrtc_state->stream->link->psr_settings.psr_allow_active &&
8242 (timestamp_ns -
8243 acrtc_state->stream->link->psr_settings.psr_dirty_rects_change_timestamp_ns) >
8244 500000000)
7cc191ee 8245 amdgpu_dm_psr_enable(acrtc_state->stream);
58aa1c50
NK
8246 } else {
8247 acrtc_attach->dm_irq_params.allow_psr_entry = false;
8c322309
RL
8248 }
8249
bc7f670e 8250 mutex_unlock(&dm->dc_lock);
e7b07cee 8251 }
4b510503 8252
8ad27806
NK
8253 /*
8254 * Update cursor state *after* programming all the planes.
8255 * This avoids redundant programming in the case where we're going
8256 * to be disabling a single plane - those pipes are being disabled.
8257 */
8258 if (acrtc_state->active_planes)
8259 amdgpu_dm_commit_cursors(state);
80c218d5 8260
4b510503 8261cleanup:
74aa7bd4 8262 kfree(bundle);
e7b07cee
HW
8263}
8264
6ce8f316
NK
8265static void amdgpu_dm_commit_audio(struct drm_device *dev,
8266 struct drm_atomic_state *state)
8267{
1348969a 8268 struct amdgpu_device *adev = drm_to_adev(dev);
6ce8f316
NK
8269 struct amdgpu_dm_connector *aconnector;
8270 struct drm_connector *connector;
8271 struct drm_connector_state *old_con_state, *new_con_state;
8272 struct drm_crtc_state *new_crtc_state;
8273 struct dm_crtc_state *new_dm_crtc_state;
8274 const struct dc_stream_status *status;
8275 int i, inst;
8276
8277 /* Notify device removals. */
8278 for_each_oldnew_connector_in_state(state, connector, old_con_state, new_con_state, i) {
8279 if (old_con_state->crtc != new_con_state->crtc) {
8280 /* CRTC changes require notification. */
8281 goto notify;
8282 }
8283
8284 if (!new_con_state->crtc)
8285 continue;
8286
8287 new_crtc_state = drm_atomic_get_new_crtc_state(
8288 state, new_con_state->crtc);
8289
8290 if (!new_crtc_state)
8291 continue;
8292
8293 if (!drm_atomic_crtc_needs_modeset(new_crtc_state))
8294 continue;
8295
8296 notify:
8297 aconnector = to_amdgpu_dm_connector(connector);
8298
8299 mutex_lock(&adev->dm.audio_lock);
8300 inst = aconnector->audio_inst;
8301 aconnector->audio_inst = -1;
8302 mutex_unlock(&adev->dm.audio_lock);
8303
8304 amdgpu_dm_audio_eld_notify(adev, inst);
8305 }
8306
8307 /* Notify audio device additions. */
8308 for_each_new_connector_in_state(state, connector, new_con_state, i) {
8309 if (!new_con_state->crtc)
8310 continue;
8311
8312 new_crtc_state = drm_atomic_get_new_crtc_state(
8313 state, new_con_state->crtc);
8314
8315 if (!new_crtc_state)
8316 continue;
8317
8318 if (!drm_atomic_crtc_needs_modeset(new_crtc_state))
8319 continue;
8320
8321 new_dm_crtc_state = to_dm_crtc_state(new_crtc_state);
8322 if (!new_dm_crtc_state->stream)
8323 continue;
8324
8325 status = dc_stream_get_status(new_dm_crtc_state->stream);
8326 if (!status)
8327 continue;
8328
8329 aconnector = to_amdgpu_dm_connector(connector);
8330
8331 mutex_lock(&adev->dm.audio_lock);
8332 inst = status->audio_inst;
8333 aconnector->audio_inst = inst;
8334 mutex_unlock(&adev->dm.audio_lock);
8335
8336 amdgpu_dm_audio_eld_notify(adev, inst);
8337 }
8338}
8339
1f6010a9 8340/*
27b3f4fc
LSL
8341 * amdgpu_dm_crtc_copy_transient_flags - copy mirrored flags from DRM to DC
8342 * @crtc_state: the DRM CRTC state
8343 * @stream_state: the DC stream state.
8344 *
8345 * Copy the mirrored transient state flags from DRM, to DC. It is used to bring
8346 * a dc_stream_state's flags in sync with a drm_crtc_state's flags.
8347 */
8348static void amdgpu_dm_crtc_copy_transient_flags(struct drm_crtc_state *crtc_state,
8349 struct dc_stream_state *stream_state)
8350{
b9952f93 8351 stream_state->mode_changed = drm_atomic_crtc_needs_modeset(crtc_state);
27b3f4fc 8352}
e7b07cee 8353
b8592b48
LL
8354/**
8355 * amdgpu_dm_atomic_commit_tail() - AMDgpu DM's commit tail implementation.
8356 * @state: The atomic state to commit
8357 *
8358 * This will tell DC to commit the constructed DC state from atomic_check,
8359 * programming the hardware. Any failures here implies a hardware failure, since
8360 * atomic check should have filtered anything non-kosher.
8361 */
7578ecda 8362static void amdgpu_dm_atomic_commit_tail(struct drm_atomic_state *state)
e7b07cee
HW
8363{
8364 struct drm_device *dev = state->dev;
1348969a 8365 struct amdgpu_device *adev = drm_to_adev(dev);
e7b07cee
HW
8366 struct amdgpu_display_manager *dm = &adev->dm;
8367 struct dm_atomic_state *dm_state;
eb3dc897 8368 struct dc_state *dc_state = NULL, *dc_state_temp = NULL;
ae67558b 8369 u32 i, j;
5cc6dcbd 8370 struct drm_crtc *crtc;
0bc9706d 8371 struct drm_crtc_state *old_crtc_state, *new_crtc_state;
e7b07cee
HW
8372 unsigned long flags;
8373 bool wait_for_vblank = true;
8374 struct drm_connector *connector;
c2cea706 8375 struct drm_connector_state *old_con_state, *new_con_state;
54d76575 8376 struct dm_crtc_state *dm_old_crtc_state, *dm_new_crtc_state;
fe2a1965 8377 int crtc_disable_count = 0;
6ee90e88 8378 bool mode_set_reset_required = false;
047de3f1 8379 int r;
e7b07cee 8380
e8a98235
RS
8381 trace_amdgpu_dm_atomic_commit_tail_begin(state);
8382
047de3f1
CK
8383 r = drm_atomic_helper_wait_for_fences(dev, state, false);
8384 if (unlikely(r))
8385 DRM_ERROR("Waiting for fences timed out!");
8386
e7b07cee 8387 drm_atomic_helper_update_legacy_modeset_state(dev, state);
a5c2c0d1 8388 drm_dp_mst_atomic_wait_for_dependencies(state);
e7b07cee 8389
eb3dc897
NK
8390 dm_state = dm_atomic_get_new_state(state);
8391 if (dm_state && dm_state->context) {
8392 dc_state = dm_state->context;
8393 } else {
8394 /* No state changes, retain current state. */
813d20dc 8395 dc_state_temp = dc_create_state(dm->dc);
eb3dc897
NK
8396 ASSERT(dc_state_temp);
8397 dc_state = dc_state_temp;
8398 dc_resource_state_copy_construct_current(dm->dc, dc_state);
8399 }
e7b07cee 8400
6d90a208
AP
8401 for_each_oldnew_crtc_in_state (state, crtc, old_crtc_state,
8402 new_crtc_state, i) {
8403 struct amdgpu_crtc *acrtc = to_amdgpu_crtc(crtc);
8404
8405 dm_old_crtc_state = to_dm_crtc_state(old_crtc_state);
8406
8407 if (old_crtc_state->active &&
8408 (!new_crtc_state->active ||
8409 drm_atomic_crtc_needs_modeset(new_crtc_state))) {
8410 manage_dm_interrupts(adev, acrtc, false);
8411 dc_stream_release(dm_old_crtc_state->stream);
8412 }
8413 }
8414
8976f73b
RS
8415 drm_atomic_helper_calc_timestamping_constants(state);
8416
e7b07cee 8417 /* update changed items */
0bc9706d 8418 for_each_oldnew_crtc_in_state(state, crtc, old_crtc_state, new_crtc_state, i) {
e7b07cee 8419 struct amdgpu_crtc *acrtc = to_amdgpu_crtc(crtc);
b830ebc9 8420
54d76575
LSL
8421 dm_new_crtc_state = to_dm_crtc_state(new_crtc_state);
8422 dm_old_crtc_state = to_dm_crtc_state(old_crtc_state);
e7b07cee 8423
9f07550b 8424 drm_dbg_state(state->dev,
e7b07cee
HW
8425 "amdgpu_crtc id:%d crtc_state_flags: enable:%d, active:%d, "
8426 "planes_changed:%d, mode_changed:%d,active_changed:%d,"
8427 "connectors_changed:%d\n",
8428 acrtc->crtc_id,
0bc9706d
LSL
8429 new_crtc_state->enable,
8430 new_crtc_state->active,
8431 new_crtc_state->planes_changed,
8432 new_crtc_state->mode_changed,
8433 new_crtc_state->active_changed,
8434 new_crtc_state->connectors_changed);
e7b07cee 8435
5c68c652
VL
8436 /* Disable cursor if disabling crtc */
8437 if (old_crtc_state->active && !new_crtc_state->active) {
8438 struct dc_cursor_position position;
8439
8440 memset(&position, 0, sizeof(position));
8441 mutex_lock(&dm->dc_lock);
8442 dc_stream_set_cursor_position(dm_old_crtc_state->stream, &position);
8443 mutex_unlock(&dm->dc_lock);
8444 }
8445
27b3f4fc
LSL
8446 /* Copy all transient state flags into dc state */
8447 if (dm_new_crtc_state->stream) {
8448 amdgpu_dm_crtc_copy_transient_flags(&dm_new_crtc_state->base,
8449 dm_new_crtc_state->stream);
8450 }
8451
e7b07cee
HW
8452 /* handles headless hotplug case, updating new_state and
8453 * aconnector as needed
8454 */
8455
54d76575 8456 if (modeset_required(new_crtc_state, dm_new_crtc_state->stream, dm_old_crtc_state->stream)) {
e7b07cee 8457
4711c033 8458 DRM_DEBUG_ATOMIC("Atomic commit: SET crtc id %d: [%p]\n", acrtc->crtc_id, acrtc);
e7b07cee 8459
54d76575 8460 if (!dm_new_crtc_state->stream) {
e7b07cee 8461 /*
b830ebc9
HW
8462 * this could happen because of issues with
8463 * userspace notifications delivery.
8464 * In this case userspace tries to set mode on
1f6010a9
DF
8465 * display which is disconnected in fact.
8466 * dc_sink is NULL in this case on aconnector.
b830ebc9
HW
8467 * We expect reset mode will come soon.
8468 *
8469 * This can also happen when unplug is done
8470 * during resume sequence ended
8471 *
8472 * In this case, we want to pretend we still
8473 * have a sink to keep the pipe running so that
8474 * hw state is consistent with the sw state
8475 */
f1ad2f5e 8476 DRM_DEBUG_DRIVER("%s: Failed to create new stream for crtc %d\n",
e7b07cee
HW
8477 __func__, acrtc->base.base.id);
8478 continue;
8479 }
8480
54d76575
LSL
8481 if (dm_old_crtc_state->stream)
8482 remove_stream(adev, acrtc, dm_old_crtc_state->stream);
e7b07cee 8483
97028037
LP
8484 pm_runtime_get_noresume(dev->dev);
8485
e7b07cee 8486 acrtc->enabled = true;
0bc9706d
LSL
8487 acrtc->hw_mode = new_crtc_state->mode;
8488 crtc->hwmode = new_crtc_state->mode;
6ee90e88 8489 mode_set_reset_required = true;
0bc9706d 8490 } else if (modereset_required(new_crtc_state)) {
4711c033 8491 DRM_DEBUG_ATOMIC("Atomic commit: RESET. crtc id %d:[%p]\n", acrtc->crtc_id, acrtc);
e7b07cee 8492 /* i.e. reset mode */
6ee90e88 8493 if (dm_old_crtc_state->stream)
54d76575 8494 remove_stream(adev, acrtc, dm_old_crtc_state->stream);
a85ba005 8495
6ee90e88 8496 mode_set_reset_required = true;
e7b07cee
HW
8497 }
8498 } /* for_each_crtc_in_state() */
8499
eb3dc897 8500 if (dc_state) {
6ee90e88 8501 /* if there mode set or reset, disable eDP PSR */
58aa1c50 8502 if (mode_set_reset_required) {
06dd1888
NK
8503 if (dm->vblank_control_workqueue)
8504 flush_workqueue(dm->vblank_control_workqueue);
cae5c1ab 8505
6ee90e88 8506 amdgpu_dm_psr_disable_all(dm);
58aa1c50 8507 }
6ee90e88 8508
eb3dc897 8509 dm_enable_per_frame_crtc_master_sync(dc_state);
674e78ac 8510 mutex_lock(&dm->dc_lock);
eb3dc897 8511 WARN_ON(!dc_commit_state(dm->dc, dc_state));
f3106c94
JC
8512
8513 /* Allow idle optimization when vblank count is 0 for display off */
8514 if (dm->active_vblank_irq_count == 0)
8515 dc_allow_idle_optimizations(dm->dc, true);
674e78ac 8516 mutex_unlock(&dm->dc_lock);
fa2123db 8517 }
fe8858bb 8518
0bc9706d 8519 for_each_new_crtc_in_state(state, crtc, new_crtc_state, i) {
e7b07cee 8520 struct amdgpu_crtc *acrtc = to_amdgpu_crtc(crtc);
b830ebc9 8521
54d76575 8522 dm_new_crtc_state = to_dm_crtc_state(new_crtc_state);
e7b07cee 8523
54d76575 8524 if (dm_new_crtc_state->stream != NULL) {
e7b07cee 8525 const struct dc_stream_status *status =
54d76575 8526 dc_stream_get_status(dm_new_crtc_state->stream);
e7b07cee 8527
eb3dc897 8528 if (!status)
09f609c3
LL
8529 status = dc_stream_get_status_from_state(dc_state,
8530 dm_new_crtc_state->stream);
e7b07cee 8531 if (!status)
54d76575 8532 DC_ERR("got no status for stream %p on acrtc%p\n", dm_new_crtc_state->stream, acrtc);
e7b07cee
HW
8533 else
8534 acrtc->otg_inst = status->primary_otg_inst;
8535 }
8536 }
0c8620d6
BL
8537#ifdef CONFIG_DRM_AMD_DC_HDCP
8538 for_each_oldnew_connector_in_state(state, connector, old_con_state, new_con_state, i) {
8539 struct dm_connector_state *dm_new_con_state = to_dm_connector_state(new_con_state);
8540 struct amdgpu_crtc *acrtc = to_amdgpu_crtc(dm_new_con_state->base.crtc);
8541 struct amdgpu_dm_connector *aconnector = to_amdgpu_dm_connector(connector);
8542
e8fd3eeb 8543 pr_debug("[HDCP_DM] -------------- i : %x ----------\n", i);
8544
8545 if (!connector)
8546 continue;
8547
8548 pr_debug("[HDCP_DM] connector->index: %x connect_status: %x dpms: %x\n",
8549 connector->index, connector->status, connector->dpms);
8550 pr_debug("[HDCP_DM] state protection old: %x new: %x\n",
8551 old_con_state->content_protection, new_con_state->content_protection);
8552
8553 if (aconnector->dc_sink) {
8554 if (aconnector->dc_sink->sink_signal != SIGNAL_TYPE_VIRTUAL &&
8555 aconnector->dc_sink->sink_signal != SIGNAL_TYPE_NONE) {
8556 pr_debug("[HDCP_DM] pipe_ctx dispname=%s\n",
8557 aconnector->dc_sink->edid_caps.display_name);
8558 }
8559 }
8560
0c8620d6 8561 new_crtc_state = NULL;
e8fd3eeb 8562 old_crtc_state = NULL;
0c8620d6 8563
e8fd3eeb 8564 if (acrtc) {
0c8620d6 8565 new_crtc_state = drm_atomic_get_new_crtc_state(state, &acrtc->base);
e8fd3eeb 8566 old_crtc_state = drm_atomic_get_old_crtc_state(state, &acrtc->base);
8567 }
8568
8569 if (old_crtc_state)
8570 pr_debug("old crtc en: %x a: %x m: %x a-chg: %x c-chg: %x\n",
8571 old_crtc_state->enable,
8572 old_crtc_state->active,
8573 old_crtc_state->mode_changed,
8574 old_crtc_state->active_changed,
8575 old_crtc_state->connectors_changed);
8576
8577 if (new_crtc_state)
8578 pr_debug("NEW crtc en: %x a: %x m: %x a-chg: %x c-chg: %x\n",
8579 new_crtc_state->enable,
8580 new_crtc_state->active,
8581 new_crtc_state->mode_changed,
8582 new_crtc_state->active_changed,
8583 new_crtc_state->connectors_changed);
8584 }
8585
8586 for_each_oldnew_connector_in_state(state, connector, old_con_state, new_con_state, i) {
8587 struct dm_connector_state *dm_new_con_state = to_dm_connector_state(new_con_state);
8588 struct amdgpu_crtc *acrtc = to_amdgpu_crtc(dm_new_con_state->base.crtc);
8589 struct amdgpu_dm_connector *aconnector = to_amdgpu_dm_connector(connector);
8590
8591 new_crtc_state = NULL;
8592 old_crtc_state = NULL;
8593
8594 if (acrtc) {
8595 new_crtc_state = drm_atomic_get_new_crtc_state(state, &acrtc->base);
8596 old_crtc_state = drm_atomic_get_old_crtc_state(state, &acrtc->base);
8597 }
0c8620d6
BL
8598
8599 dm_new_crtc_state = to_dm_crtc_state(new_crtc_state);
8600
8601 if (dm_new_crtc_state && dm_new_crtc_state->stream == NULL &&
8602 connector->state->content_protection == DRM_MODE_CONTENT_PROTECTION_ENABLED) {
8603 hdcp_reset_display(adev->dm.hdcp_workqueue, aconnector->dc_link->link_index);
8604 new_con_state->content_protection = DRM_MODE_CONTENT_PROTECTION_DESIRED;
97f6c917 8605 dm_new_con_state->update_hdcp = true;
0c8620d6
BL
8606 continue;
8607 }
8608
e8fd3eeb 8609 if (is_content_protection_different(new_crtc_state, old_crtc_state, new_con_state,
8610 old_con_state, connector, adev->dm.hdcp_workqueue)) {
82986fd6 8611 /* when display is unplugged from mst hub, connctor will
8612 * be destroyed within dm_dp_mst_connector_destroy. connector
8613 * hdcp perperties, like type, undesired, desired, enabled,
8614 * will be lost. So, save hdcp properties into hdcp_work within
8615 * amdgpu_dm_atomic_commit_tail. if the same display is
8616 * plugged back with same display index, its hdcp properties
8617 * will be retrieved from hdcp_work within dm_dp_mst_get_modes
8618 */
8619
e8fd3eeb 8620 bool enable_encryption = false;
8621
8622 if (new_con_state->content_protection == DRM_MODE_CONTENT_PROTECTION_DESIRED)
8623 enable_encryption = true;
8624
82986fd6 8625 if (aconnector->dc_link && aconnector->dc_sink &&
8626 aconnector->dc_link->type == dc_connection_mst_branch) {
8627 struct hdcp_workqueue *hdcp_work = adev->dm.hdcp_workqueue;
8628 struct hdcp_workqueue *hdcp_w =
8629 &hdcp_work[aconnector->dc_link->link_index];
8630
8631 hdcp_w->hdcp_content_type[connector->index] =
8632 new_con_state->hdcp_content_type;
8633 hdcp_w->content_protection[connector->index] =
8634 new_con_state->content_protection;
8635 }
8636
e8fd3eeb 8637 if (new_crtc_state && new_crtc_state->mode_changed &&
8638 new_con_state->content_protection >= DRM_MODE_CONTENT_PROTECTION_DESIRED)
8639 enable_encryption = true;
8640
8641 DRM_INFO("[HDCP_DM] hdcp_update_display enable_encryption = %x\n", enable_encryption);
8642
b1abe558
BL
8643 hdcp_update_display(
8644 adev->dm.hdcp_workqueue, aconnector->dc_link->link_index, aconnector,
e8fd3eeb 8645 new_con_state->hdcp_content_type, enable_encryption);
8646 }
0c8620d6
BL
8647 }
8648#endif
e7b07cee 8649
02d6a6fc 8650 /* Handle connector state changes */
c2cea706 8651 for_each_oldnew_connector_in_state(state, connector, old_con_state, new_con_state, i) {
54d76575
LSL
8652 struct dm_connector_state *dm_new_con_state = to_dm_connector_state(new_con_state);
8653 struct dm_connector_state *dm_old_con_state = to_dm_connector_state(old_con_state);
8654 struct amdgpu_crtc *acrtc = to_amdgpu_crtc(dm_new_con_state->base.crtc);
efc8278e 8655 struct dc_surface_update dummy_updates[MAX_SURFACES];
19afd799 8656 struct dc_stream_update stream_update;
b232d4ed 8657 struct dc_info_packet hdr_packet;
e7b07cee 8658 struct dc_stream_status *status = NULL;
b232d4ed 8659 bool abm_changed, hdr_changed, scaling_changed;
e7b07cee 8660
efc8278e 8661 memset(&dummy_updates, 0, sizeof(dummy_updates));
19afd799
NC
8662 memset(&stream_update, 0, sizeof(stream_update));
8663
44d09c6a 8664 if (acrtc) {
0bc9706d 8665 new_crtc_state = drm_atomic_get_new_crtc_state(state, &acrtc->base);
44d09c6a
HW
8666 old_crtc_state = drm_atomic_get_old_crtc_state(state, &acrtc->base);
8667 }
0bc9706d 8668
e7b07cee 8669 /* Skip any modesets/resets */
0bc9706d 8670 if (!acrtc || drm_atomic_crtc_needs_modeset(new_crtc_state))
e7b07cee
HW
8671 continue;
8672
54d76575 8673 dm_new_crtc_state = to_dm_crtc_state(new_crtc_state);
c1ee92f9
DF
8674 dm_old_crtc_state = to_dm_crtc_state(old_crtc_state);
8675
b232d4ed
NK
8676 scaling_changed = is_scaling_state_different(dm_new_con_state,
8677 dm_old_con_state);
8678
8679 abm_changed = dm_new_crtc_state->abm_level !=
8680 dm_old_crtc_state->abm_level;
8681
8682 hdr_changed =
72921cdf 8683 !drm_connector_atomic_hdr_metadata_equal(old_con_state, new_con_state);
b232d4ed
NK
8684
8685 if (!scaling_changed && !abm_changed && !hdr_changed)
c1ee92f9 8686 continue;
e7b07cee 8687
b6e881c9 8688 stream_update.stream = dm_new_crtc_state->stream;
b232d4ed 8689 if (scaling_changed) {
02d6a6fc 8690 update_stream_scaling_settings(&dm_new_con_state->base.crtc->mode,
b6e881c9 8691 dm_new_con_state, dm_new_crtc_state->stream);
e7b07cee 8692
02d6a6fc
DF
8693 stream_update.src = dm_new_crtc_state->stream->src;
8694 stream_update.dst = dm_new_crtc_state->stream->dst;
8695 }
8696
b232d4ed 8697 if (abm_changed) {
02d6a6fc
DF
8698 dm_new_crtc_state->stream->abm_level = dm_new_crtc_state->abm_level;
8699
8700 stream_update.abm_level = &dm_new_crtc_state->abm_level;
8701 }
70e8ffc5 8702
b232d4ed
NK
8703 if (hdr_changed) {
8704 fill_hdr_info_packet(new_con_state, &hdr_packet);
8705 stream_update.hdr_static_metadata = &hdr_packet;
8706 }
8707
54d76575 8708 status = dc_stream_get_status(dm_new_crtc_state->stream);
57738ae4
ND
8709
8710 if (WARN_ON(!status))
8711 continue;
8712
3be5262e 8713 WARN_ON(!status->plane_count);
e7b07cee 8714
02d6a6fc
DF
8715 /*
8716 * TODO: DC refuses to perform stream updates without a dc_surface_update.
8717 * Here we create an empty update on each plane.
8718 * To fix this, DC should permit updating only stream properties.
8719 */
8720 for (j = 0; j < status->plane_count; j++)
efc8278e 8721 dummy_updates[j].surface = status->plane_states[0];
02d6a6fc
DF
8722
8723
8724 mutex_lock(&dm->dc_lock);
8725 dc_commit_updates_for_stream(dm->dc,
efc8278e 8726 dummy_updates,
02d6a6fc
DF
8727 status->plane_count,
8728 dm_new_crtc_state->stream,
efc8278e
AJ
8729 &stream_update,
8730 dc_state);
02d6a6fc 8731 mutex_unlock(&dm->dc_lock);
e7b07cee
HW
8732 }
8733
8fe684e9
NK
8734 /**
8735 * Enable interrupts for CRTCs that are newly enabled or went through
8736 * a modeset. It was intentionally deferred until after the front end
8737 * state was modified to wait until the OTG was on and so the IRQ
8738 * handlers didn't access stale or invalid state.
8739 */
8740 for_each_oldnew_crtc_in_state(state, crtc, old_crtc_state, new_crtc_state, i) {
8741 struct amdgpu_crtc *acrtc = to_amdgpu_crtc(crtc);
8e7b6fee
WL
8742#ifdef CONFIG_DEBUG_FS
8743 enum amdgpu_dm_pipe_crc_source cur_crc_src;
8799c0be
YL
8744#endif
8745 /* Count number of newly disabled CRTCs for dropping PM refs later. */
8746 if (old_crtc_state->active && !new_crtc_state->active)
8747 crtc_disable_count++;
8748
8749 dm_new_crtc_state = to_dm_crtc_state(new_crtc_state);
8750 dm_old_crtc_state = to_dm_crtc_state(old_crtc_state);
8751
8752 /* For freesync config update on crtc state and params for irq */
8753 update_stream_irq_parameters(dm, dm_new_crtc_state);
8754
8755#ifdef CONFIG_DEBUG_FS
d98af272
WL
8756 spin_lock_irqsave(&adev_to_drm(adev)->event_lock, flags);
8757 cur_crc_src = acrtc->dm_irq_params.crc_src;
8758 spin_unlock_irqrestore(&adev_to_drm(adev)->event_lock, flags);
8e7b6fee 8759#endif
585d450c 8760
8fe684e9
NK
8761 if (new_crtc_state->active &&
8762 (!old_crtc_state->active ||
8763 drm_atomic_crtc_needs_modeset(new_crtc_state))) {
585d450c
AP
8764 dc_stream_retain(dm_new_crtc_state->stream);
8765 acrtc->dm_irq_params.stream = dm_new_crtc_state->stream;
8fe684e9 8766 manage_dm_interrupts(adev, acrtc, true);
8799c0be
YL
8767 }
8768 /* Handle vrr on->off / off->on transitions */
8769 amdgpu_dm_handle_vrr_transition(dm_old_crtc_state, dm_new_crtc_state);
e2881d6d 8770
24eb9374 8771#ifdef CONFIG_DEBUG_FS
8799c0be
YL
8772 if (new_crtc_state->active &&
8773 (!old_crtc_state->active ||
8774 drm_atomic_crtc_needs_modeset(new_crtc_state))) {
8fe684e9
NK
8775 /**
8776 * Frontend may have changed so reapply the CRC capture
8777 * settings for the stream.
8778 */
8e7b6fee 8779 if (amdgpu_dm_is_valid_crc_source(cur_crc_src)) {
86bc2219 8780#if defined(CONFIG_DRM_AMD_SECURE_DISPLAY)
d98af272
WL
8781 if (amdgpu_dm_crc_window_is_activated(crtc)) {
8782 spin_lock_irqsave(&adev_to_drm(adev)->event_lock, flags);
c0459bdd 8783 acrtc->dm_irq_params.window_param.update_win = true;
1b11ff76
AL
8784
8785 /**
8786 * It takes 2 frames for HW to stably generate CRC when
8787 * resuming from suspend, so we set skip_frame_cnt 2.
8788 */
c0459bdd 8789 acrtc->dm_irq_params.window_param.skip_frame_cnt = 2;
d98af272
WL
8790 spin_unlock_irqrestore(&adev_to_drm(adev)->event_lock, flags);
8791 }
86bc2219 8792#endif
bbc49fc0
WL
8793 if (amdgpu_dm_crtc_configure_crc_source(
8794 crtc, dm_new_crtc_state, cur_crc_src))
8795 DRM_DEBUG_DRIVER("Failed to configure crc source");
8799c0be 8796 }
8fe684e9 8797 }
2130b87b 8798#endif
8fe684e9 8799 }
e7b07cee 8800
420cd472 8801 for_each_new_crtc_in_state(state, crtc, new_crtc_state, j)
4d85f45c 8802 if (new_crtc_state->async_flip)
420cd472
DF
8803 wait_for_vblank = false;
8804
e7b07cee 8805 /* update planes when needed per crtc*/
5cc6dcbd 8806 for_each_new_crtc_in_state(state, crtc, new_crtc_state, j) {
54d76575 8807 dm_new_crtc_state = to_dm_crtc_state(new_crtc_state);
e7b07cee 8808
54d76575 8809 if (dm_new_crtc_state->stream)
eb3dc897 8810 amdgpu_dm_commit_planes(state, dc_state, dev,
420cd472 8811 dm, crtc, wait_for_vblank);
e7b07cee
HW
8812 }
8813
6ce8f316
NK
8814 /* Update audio instances for each connector. */
8815 amdgpu_dm_commit_audio(dev, state);
8816
7230362c 8817 /* restore the backlight level */
7fd13bae
AD
8818 for (i = 0; i < dm->num_of_edps; i++) {
8819 if (dm->backlight_dev[i] &&
4052287a 8820 (dm->actual_brightness[i] != dm->brightness[i]))
7fd13bae
AD
8821 amdgpu_dm_backlight_set_level(dm, i, dm->brightness[i]);
8822 }
83a3439d 8823
e7b07cee
HW
8824 /*
8825 * send vblank event on all events not handled in flip and
8826 * mark consumed event for drm_atomic_helper_commit_hw_done
8827 */
4a580877 8828 spin_lock_irqsave(&adev_to_drm(adev)->event_lock, flags);
0bc9706d 8829 for_each_new_crtc_in_state(state, crtc, new_crtc_state, i) {
e7b07cee 8830
0bc9706d
LSL
8831 if (new_crtc_state->event)
8832 drm_send_event_locked(dev, &new_crtc_state->event->base);
e7b07cee 8833
0bc9706d 8834 new_crtc_state->event = NULL;
e7b07cee 8835 }
4a580877 8836 spin_unlock_irqrestore(&adev_to_drm(adev)->event_lock, flags);
e7b07cee 8837
29c8f234
LL
8838 /* Signal HW programming completion */
8839 drm_atomic_helper_commit_hw_done(state);
e7b07cee
HW
8840
8841 if (wait_for_vblank)
320a1274 8842 drm_atomic_helper_wait_for_flip_done(dev, state);
e7b07cee
HW
8843
8844 drm_atomic_helper_cleanup_planes(dev, state);
97028037 8845
5f6fab24
AD
8846 /* return the stolen vga memory back to VRAM */
8847 if (!adev->mman.keep_stolen_vga_memory)
8848 amdgpu_bo_free_kernel(&adev->mman.stolen_vga_memory, NULL, NULL);
8849 amdgpu_bo_free_kernel(&adev->mman.stolen_extended_memory, NULL, NULL);
8850
1f6010a9
DF
8851 /*
8852 * Finally, drop a runtime PM reference for each newly disabled CRTC,
97028037
LP
8853 * so we can put the GPU into runtime suspend if we're not driving any
8854 * displays anymore
8855 */
fe2a1965
LP
8856 for (i = 0; i < crtc_disable_count; i++)
8857 pm_runtime_put_autosuspend(dev->dev);
97028037 8858 pm_runtime_mark_last_busy(dev->dev);
eb3dc897
NK
8859
8860 if (dc_state_temp)
8861 dc_release_state(dc_state_temp);
e7b07cee
HW
8862}
8863
e7b07cee
HW
8864static int dm_force_atomic_commit(struct drm_connector *connector)
8865{
8866 int ret = 0;
8867 struct drm_device *ddev = connector->dev;
8868 struct drm_atomic_state *state = drm_atomic_state_alloc(ddev);
8869 struct amdgpu_crtc *disconnected_acrtc = to_amdgpu_crtc(connector->encoder->crtc);
8870 struct drm_plane *plane = disconnected_acrtc->base.primary;
8871 struct drm_connector_state *conn_state;
8872 struct drm_crtc_state *crtc_state;
8873 struct drm_plane_state *plane_state;
8874
8875 if (!state)
8876 return -ENOMEM;
8877
8878 state->acquire_ctx = ddev->mode_config.acquire_ctx;
8879
8880 /* Construct an atomic state to restore previous display setting */
8881
8882 /*
8883 * Attach connectors to drm_atomic_state
8884 */
8885 conn_state = drm_atomic_get_connector_state(state, connector);
8886
8887 ret = PTR_ERR_OR_ZERO(conn_state);
8888 if (ret)
2dc39051 8889 goto out;
e7b07cee
HW
8890
8891 /* Attach crtc to drm_atomic_state*/
8892 crtc_state = drm_atomic_get_crtc_state(state, &disconnected_acrtc->base);
8893
8894 ret = PTR_ERR_OR_ZERO(crtc_state);
8895 if (ret)
2dc39051 8896 goto out;
e7b07cee
HW
8897
8898 /* force a restore */
8899 crtc_state->mode_changed = true;
8900
8901 /* Attach plane to drm_atomic_state */
8902 plane_state = drm_atomic_get_plane_state(state, plane);
8903
8904 ret = PTR_ERR_OR_ZERO(plane_state);
8905 if (ret)
2dc39051 8906 goto out;
e7b07cee
HW
8907
8908 /* Call commit internally with the state we just constructed */
8909 ret = drm_atomic_commit(state);
e7b07cee 8910
2dc39051 8911out:
e7b07cee 8912 drm_atomic_state_put(state);
2dc39051
VL
8913 if (ret)
8914 DRM_ERROR("Restoring old state failed with %i\n", ret);
e7b07cee
HW
8915
8916 return ret;
8917}
8918
8919/*
1f6010a9
DF
8920 * This function handles all cases when set mode does not come upon hotplug.
8921 * This includes when a display is unplugged then plugged back into the
8922 * same port and when running without usermode desktop manager supprot
e7b07cee 8923 */
3ee6b26b
AD
8924void dm_restore_drm_connector_state(struct drm_device *dev,
8925 struct drm_connector *connector)
e7b07cee 8926{
c84dec2f 8927 struct amdgpu_dm_connector *aconnector = to_amdgpu_dm_connector(connector);
e7b07cee
HW
8928 struct amdgpu_crtc *disconnected_acrtc;
8929 struct dm_crtc_state *acrtc_state;
8930
8931 if (!aconnector->dc_sink || !connector->state || !connector->encoder)
8932 return;
8933
8934 disconnected_acrtc = to_amdgpu_crtc(connector->encoder->crtc);
70e8ffc5
HW
8935 if (!disconnected_acrtc)
8936 return;
e7b07cee 8937
70e8ffc5
HW
8938 acrtc_state = to_dm_crtc_state(disconnected_acrtc->base.state);
8939 if (!acrtc_state->stream)
e7b07cee
HW
8940 return;
8941
8942 /*
8943 * If the previous sink is not released and different from the current,
8944 * we deduce we are in a state where we can not rely on usermode call
8945 * to turn on the display, so we do it here
8946 */
8947 if (acrtc_state->stream->sink != aconnector->dc_sink)
8948 dm_force_atomic_commit(&aconnector->base);
8949}
8950
1f6010a9 8951/*
e7b07cee
HW
8952 * Grabs all modesetting locks to serialize against any blocking commits,
8953 * Waits for completion of all non blocking commits.
8954 */
3ee6b26b
AD
8955static int do_aquire_global_lock(struct drm_device *dev,
8956 struct drm_atomic_state *state)
e7b07cee
HW
8957{
8958 struct drm_crtc *crtc;
8959 struct drm_crtc_commit *commit;
8960 long ret;
8961
1f6010a9
DF
8962 /*
8963 * Adding all modeset locks to aquire_ctx will
e7b07cee
HW
8964 * ensure that when the framework release it the
8965 * extra locks we are locking here will get released to
8966 */
8967 ret = drm_modeset_lock_all_ctx(dev, state->acquire_ctx);
8968 if (ret)
8969 return ret;
8970
8971 list_for_each_entry(crtc, &dev->mode_config.crtc_list, head) {
8972 spin_lock(&crtc->commit_lock);
8973 commit = list_first_entry_or_null(&crtc->commit_list,
8974 struct drm_crtc_commit, commit_entry);
8975 if (commit)
8976 drm_crtc_commit_get(commit);
8977 spin_unlock(&crtc->commit_lock);
8978
8979 if (!commit)
8980 continue;
8981
1f6010a9
DF
8982 /*
8983 * Make sure all pending HW programming completed and
e7b07cee
HW
8984 * page flips done
8985 */
8986 ret = wait_for_completion_interruptible_timeout(&commit->hw_done, 10*HZ);
8987
8988 if (ret > 0)
8989 ret = wait_for_completion_interruptible_timeout(
8990 &commit->flip_done, 10*HZ);
8991
8992 if (ret == 0)
8993 DRM_ERROR("[CRTC:%d:%s] hw_done or flip_done "
b830ebc9 8994 "timed out\n", crtc->base.id, crtc->name);
e7b07cee
HW
8995
8996 drm_crtc_commit_put(commit);
8997 }
8998
8999 return ret < 0 ? ret : 0;
9000}
9001
bb47de73
NK
9002static void get_freesync_config_for_crtc(
9003 struct dm_crtc_state *new_crtc_state,
9004 struct dm_connector_state *new_con_state)
98e6436d
AK
9005{
9006 struct mod_freesync_config config = {0};
98e6436d
AK
9007 struct amdgpu_dm_connector *aconnector =
9008 to_amdgpu_dm_connector(new_con_state->base.connector);
a057ec46 9009 struct drm_display_mode *mode = &new_crtc_state->base.mode;
0ab925d3 9010 int vrefresh = drm_mode_vrefresh(mode);
a85ba005 9011 bool fs_vid_mode = false;
98e6436d 9012
a057ec46 9013 new_crtc_state->vrr_supported = new_con_state->freesync_capable &&
0ab925d3
NK
9014 vrefresh >= aconnector->min_vfreq &&
9015 vrefresh <= aconnector->max_vfreq;
bb47de73 9016
6ffa6799 9017 if (new_crtc_state->vrr_supported) {
7e5098ab 9018 new_crtc_state->stream->ignore_msa_timing_param = true;
6ffa6799 9019 fs_vid_mode = new_crtc_state->freesync_config.state == VRR_STATE_ACTIVE_FIXED;
7e5098ab 9020
a85ba005
NC
9021 config.min_refresh_in_uhz = aconnector->min_vfreq * 1000000;
9022 config.max_refresh_in_uhz = aconnector->max_vfreq * 1000000;
69ff8845 9023 config.vsif_supported = true;
180db303 9024 config.btr = true;
98e6436d 9025
a85ba005
NC
9026 if (fs_vid_mode) {
9027 config.state = VRR_STATE_ACTIVE_FIXED;
9028 config.fixed_refresh_in_uhz = new_crtc_state->freesync_config.fixed_refresh_in_uhz;
9029 goto out;
9030 } else if (new_crtc_state->base.vrr_enabled) {
9031 config.state = VRR_STATE_ACTIVE_VARIABLE;
9032 } else {
9033 config.state = VRR_STATE_INACTIVE;
9034 }
9035 }
9036out:
bb47de73
NK
9037 new_crtc_state->freesync_config = config;
9038}
98e6436d 9039
bb47de73
NK
9040static void reset_freesync_config_for_crtc(
9041 struct dm_crtc_state *new_crtc_state)
9042{
9043 new_crtc_state->vrr_supported = false;
98e6436d 9044
bb47de73
NK
9045 memset(&new_crtc_state->vrr_infopacket, 0,
9046 sizeof(new_crtc_state->vrr_infopacket));
98e6436d
AK
9047}
9048
a85ba005
NC
9049static bool
9050is_timing_unchanged_for_freesync(struct drm_crtc_state *old_crtc_state,
9051 struct drm_crtc_state *new_crtc_state)
9052{
1cbd7887 9053 const struct drm_display_mode *old_mode, *new_mode;
a85ba005
NC
9054
9055 if (!old_crtc_state || !new_crtc_state)
9056 return false;
9057
1cbd7887
VS
9058 old_mode = &old_crtc_state->mode;
9059 new_mode = &new_crtc_state->mode;
9060
9061 if (old_mode->clock == new_mode->clock &&
9062 old_mode->hdisplay == new_mode->hdisplay &&
9063 old_mode->vdisplay == new_mode->vdisplay &&
9064 old_mode->htotal == new_mode->htotal &&
9065 old_mode->vtotal != new_mode->vtotal &&
9066 old_mode->hsync_start == new_mode->hsync_start &&
9067 old_mode->vsync_start != new_mode->vsync_start &&
9068 old_mode->hsync_end == new_mode->hsync_end &&
9069 old_mode->vsync_end != new_mode->vsync_end &&
9070 old_mode->hskew == new_mode->hskew &&
9071 old_mode->vscan == new_mode->vscan &&
9072 (old_mode->vsync_end - old_mode->vsync_start) ==
9073 (new_mode->vsync_end - new_mode->vsync_start))
a85ba005
NC
9074 return true;
9075
9076 return false;
9077}
9078
9079static void set_freesync_fixed_config(struct dm_crtc_state *dm_new_crtc_state) {
ae67558b 9080 u64 num, den, res;
a85ba005
NC
9081 struct drm_crtc_state *new_crtc_state = &dm_new_crtc_state->base;
9082
9083 dm_new_crtc_state->freesync_config.state = VRR_STATE_ACTIVE_FIXED;
9084
9085 num = (unsigned long long)new_crtc_state->mode.clock * 1000 * 1000000;
9086 den = (unsigned long long)new_crtc_state->mode.htotal *
9087 (unsigned long long)new_crtc_state->mode.vtotal;
9088
9089 res = div_u64(num, den);
9090 dm_new_crtc_state->freesync_config.fixed_refresh_in_uhz = res;
9091}
9092
f11d9373 9093static int dm_update_crtc_state(struct amdgpu_display_manager *dm,
17ce8a69
RL
9094 struct drm_atomic_state *state,
9095 struct drm_crtc *crtc,
9096 struct drm_crtc_state *old_crtc_state,
9097 struct drm_crtc_state *new_crtc_state,
9098 bool enable,
9099 bool *lock_and_validation_needed)
e7b07cee 9100{
eb3dc897 9101 struct dm_atomic_state *dm_state = NULL;
54d76575 9102 struct dm_crtc_state *dm_old_crtc_state, *dm_new_crtc_state;
9635b754 9103 struct dc_stream_state *new_stream;
62f55537 9104 int ret = 0;
d4d4a645 9105
1f6010a9
DF
9106 /*
9107 * TODO Move this code into dm_crtc_atomic_check once we get rid of dc_validation_set
9108 * update changed items
9109 */
4b9674e5
LL
9110 struct amdgpu_crtc *acrtc = NULL;
9111 struct amdgpu_dm_connector *aconnector = NULL;
9112 struct drm_connector_state *drm_new_conn_state = NULL, *drm_old_conn_state = NULL;
9113 struct dm_connector_state *dm_new_conn_state = NULL, *dm_old_conn_state = NULL;
e7b07cee 9114
4b9674e5 9115 new_stream = NULL;
9635b754 9116
4b9674e5
LL
9117 dm_old_crtc_state = to_dm_crtc_state(old_crtc_state);
9118 dm_new_crtc_state = to_dm_crtc_state(new_crtc_state);
9119 acrtc = to_amdgpu_crtc(crtc);
4b9674e5 9120 aconnector = amdgpu_dm_find_first_crtc_matching_connector(state, crtc);
19f89e23 9121
4b9674e5
LL
9122 /* TODO This hack should go away */
9123 if (aconnector && enable) {
9124 /* Make sure fake sink is created in plug-in scenario */
9125 drm_new_conn_state = drm_atomic_get_new_connector_state(state,
9126 &aconnector->base);
9127 drm_old_conn_state = drm_atomic_get_old_connector_state(state,
9128 &aconnector->base);
19f89e23 9129
4b9674e5
LL
9130 if (IS_ERR(drm_new_conn_state)) {
9131 ret = PTR_ERR_OR_ZERO(drm_new_conn_state);
9132 goto fail;
9133 }
19f89e23 9134
4b9674e5
LL
9135 dm_new_conn_state = to_dm_connector_state(drm_new_conn_state);
9136 dm_old_conn_state = to_dm_connector_state(drm_old_conn_state);
19f89e23 9137
02d35a67
JFZ
9138 if (!drm_atomic_crtc_needs_modeset(new_crtc_state))
9139 goto skip_modeset;
9140
cbd14ae7
SW
9141 new_stream = create_validate_stream_for_sink(aconnector,
9142 &new_crtc_state->mode,
9143 dm_new_conn_state,
9144 dm_old_crtc_state->stream);
19f89e23 9145
4b9674e5
LL
9146 /*
9147 * we can have no stream on ACTION_SET if a display
9148 * was disconnected during S3, in this case it is not an
9149 * error, the OS will be updated after detection, and
9150 * will do the right thing on next atomic commit
9151 */
19f89e23 9152
4b9674e5
LL
9153 if (!new_stream) {
9154 DRM_DEBUG_DRIVER("%s: Failed to create new stream for crtc %d\n",
9155 __func__, acrtc->base.base.id);
9156 ret = -ENOMEM;
9157 goto fail;
9158 }
e7b07cee 9159
3d4e52d0
VL
9160 /*
9161 * TODO: Check VSDB bits to decide whether this should
9162 * be enabled or not.
9163 */
9164 new_stream->triggered_crtc_reset.enabled =
9165 dm->force_timing_sync;
9166
4b9674e5 9167 dm_new_crtc_state->abm_level = dm_new_conn_state->abm_level;
98e6436d 9168
88694af9
NK
9169 ret = fill_hdr_info_packet(drm_new_conn_state,
9170 &new_stream->hdr_static_metadata);
9171 if (ret)
9172 goto fail;
9173
7e930949
NK
9174 /*
9175 * If we already removed the old stream from the context
9176 * (and set the new stream to NULL) then we can't reuse
9177 * the old stream even if the stream and scaling are unchanged.
9178 * We'll hit the BUG_ON and black screen.
9179 *
9180 * TODO: Refactor this function to allow this check to work
9181 * in all conditions.
9182 */
4243c84a
MD
9183 if (amdgpu_freesync_vid_mode &&
9184 dm_new_crtc_state->stream &&
a85ba005
NC
9185 is_timing_unchanged_for_freesync(new_crtc_state, old_crtc_state))
9186 goto skip_modeset;
9187
7e930949
NK
9188 if (dm_new_crtc_state->stream &&
9189 dc_is_stream_unchanged(new_stream, dm_old_crtc_state->stream) &&
4b9674e5
LL
9190 dc_is_stream_scaling_unchanged(new_stream, dm_old_crtc_state->stream)) {
9191 new_crtc_state->mode_changed = false;
9192 DRM_DEBUG_DRIVER("Mode change not required, setting mode_changed to %d",
9193 new_crtc_state->mode_changed);
62f55537 9194 }
4b9674e5 9195 }
b830ebc9 9196
02d35a67 9197 /* mode_changed flag may get updated above, need to check again */
4b9674e5
LL
9198 if (!drm_atomic_crtc_needs_modeset(new_crtc_state))
9199 goto skip_modeset;
e7b07cee 9200
9f07550b 9201 drm_dbg_state(state->dev,
4b9674e5
LL
9202 "amdgpu_crtc id:%d crtc_state_flags: enable:%d, active:%d, "
9203 "planes_changed:%d, mode_changed:%d,active_changed:%d,"
9204 "connectors_changed:%d\n",
9205 acrtc->crtc_id,
9206 new_crtc_state->enable,
9207 new_crtc_state->active,
9208 new_crtc_state->planes_changed,
9209 new_crtc_state->mode_changed,
9210 new_crtc_state->active_changed,
9211 new_crtc_state->connectors_changed);
62f55537 9212
4b9674e5
LL
9213 /* Remove stream for any changed/disabled CRTC */
9214 if (!enable) {
62f55537 9215
4b9674e5
LL
9216 if (!dm_old_crtc_state->stream)
9217 goto skip_modeset;
eb3dc897 9218
0f5f1ee4
AP
9219 /* Unset freesync video if it was active before */
9220 if (dm_old_crtc_state->freesync_config.state == VRR_STATE_ACTIVE_FIXED) {
9221 dm_new_crtc_state->freesync_config.state = VRR_STATE_INACTIVE;
9222 dm_new_crtc_state->freesync_config.fixed_refresh_in_uhz = 0;
9223 }
9224
9225 /* Now check if we should set freesync video mode */
4243c84a 9226 if (amdgpu_freesync_vid_mode && dm_new_crtc_state->stream &&
a85ba005
NC
9227 is_timing_unchanged_for_freesync(new_crtc_state,
9228 old_crtc_state)) {
9229 new_crtc_state->mode_changed = false;
9230 DRM_DEBUG_DRIVER(
9231 "Mode change not required for front porch change, "
9232 "setting mode_changed to %d",
9233 new_crtc_state->mode_changed);
9234
9235 set_freesync_fixed_config(dm_new_crtc_state);
9236
9237 goto skip_modeset;
4243c84a 9238 } else if (amdgpu_freesync_vid_mode && aconnector &&
a85ba005
NC
9239 is_freesync_video_mode(&new_crtc_state->mode,
9240 aconnector)) {
e88ebd83
SC
9241 struct drm_display_mode *high_mode;
9242
9243 high_mode = get_highest_refresh_rate_mode(aconnector, false);
9244 if (!drm_mode_equal(&new_crtc_state->mode, high_mode)) {
9245 set_freesync_fixed_config(dm_new_crtc_state);
9246 }
a85ba005
NC
9247 }
9248
4b9674e5
LL
9249 ret = dm_atomic_get_state(state, &dm_state);
9250 if (ret)
9251 goto fail;
e7b07cee 9252
4b9674e5
LL
9253 DRM_DEBUG_DRIVER("Disabling DRM crtc: %d\n",
9254 crtc->base.id);
62f55537 9255
4b9674e5
LL
9256 /* i.e. reset mode */
9257 if (dc_remove_stream_from_ctx(
9258 dm->dc,
9259 dm_state->context,
9260 dm_old_crtc_state->stream) != DC_OK) {
9261 ret = -EINVAL;
9262 goto fail;
9263 }
62f55537 9264
4b9674e5
LL
9265 dc_stream_release(dm_old_crtc_state->stream);
9266 dm_new_crtc_state->stream = NULL;
bb47de73 9267
4b9674e5 9268 reset_freesync_config_for_crtc(dm_new_crtc_state);
62f55537 9269
4b9674e5 9270 *lock_and_validation_needed = true;
62f55537 9271
4b9674e5
LL
9272 } else {/* Add stream for any updated/enabled CRTC */
9273 /*
9274 * Quick fix to prevent NULL pointer on new_stream when
9275 * added MST connectors not found in existing crtc_state in the chained mode
9276 * TODO: need to dig out the root cause of that
9277 */
84a8b390 9278 if (!aconnector)
4b9674e5 9279 goto skip_modeset;
62f55537 9280
4b9674e5
LL
9281 if (modereset_required(new_crtc_state))
9282 goto skip_modeset;
62f55537 9283
4b9674e5
LL
9284 if (modeset_required(new_crtc_state, new_stream,
9285 dm_old_crtc_state->stream)) {
62f55537 9286
4b9674e5 9287 WARN_ON(dm_new_crtc_state->stream);
eb3dc897 9288
4b9674e5
LL
9289 ret = dm_atomic_get_state(state, &dm_state);
9290 if (ret)
9291 goto fail;
27b3f4fc 9292
4b9674e5 9293 dm_new_crtc_state->stream = new_stream;
62f55537 9294
4b9674e5 9295 dc_stream_retain(new_stream);
1dc90497 9296
4711c033
LT
9297 DRM_DEBUG_ATOMIC("Enabling DRM crtc: %d\n",
9298 crtc->base.id);
1dc90497 9299
4b9674e5
LL
9300 if (dc_add_stream_to_ctx(
9301 dm->dc,
9302 dm_state->context,
9303 dm_new_crtc_state->stream) != DC_OK) {
9304 ret = -EINVAL;
9305 goto fail;
9b690ef3
BL
9306 }
9307
4b9674e5
LL
9308 *lock_and_validation_needed = true;
9309 }
9310 }
e277adc5 9311
4b9674e5
LL
9312skip_modeset:
9313 /* Release extra reference */
9314 if (new_stream)
9315 dc_stream_release(new_stream);
e277adc5 9316
4b9674e5
LL
9317 /*
9318 * We want to do dc stream updates that do not require a
9319 * full modeset below.
9320 */
2afda735 9321 if (!(enable && aconnector && new_crtc_state->active))
4b9674e5
LL
9322 return 0;
9323 /*
9324 * Given above conditions, the dc state cannot be NULL because:
9325 * 1. We're in the process of enabling CRTCs (just been added
9326 * to the dc context, or already is on the context)
9327 * 2. Has a valid connector attached, and
9328 * 3. Is currently active and enabled.
9329 * => The dc stream state currently exists.
9330 */
9331 BUG_ON(dm_new_crtc_state->stream == NULL);
a9e8d275 9332
4b9674e5 9333 /* Scaling or underscan settings */
c521fc31
RL
9334 if (is_scaling_state_different(dm_old_conn_state, dm_new_conn_state) ||
9335 drm_atomic_crtc_needs_modeset(new_crtc_state))
4b9674e5
LL
9336 update_stream_scaling_settings(
9337 &new_crtc_state->mode, dm_new_conn_state, dm_new_crtc_state->stream);
98e6436d 9338
b05e2c5e
DF
9339 /* ABM settings */
9340 dm_new_crtc_state->abm_level = dm_new_conn_state->abm_level;
9341
4b9674e5
LL
9342 /*
9343 * Color management settings. We also update color properties
9344 * when a modeset is needed, to ensure it gets reprogrammed.
9345 */
9346 if (dm_new_crtc_state->base.color_mgmt_changed ||
9347 drm_atomic_crtc_needs_modeset(new_crtc_state)) {
cf020d49 9348 ret = amdgpu_dm_update_crtc_color_mgmt(dm_new_crtc_state);
4b9674e5
LL
9349 if (ret)
9350 goto fail;
62f55537 9351 }
e7b07cee 9352
4b9674e5
LL
9353 /* Update Freesync settings. */
9354 get_freesync_config_for_crtc(dm_new_crtc_state,
9355 dm_new_conn_state);
9356
62f55537 9357 return ret;
9635b754
DS
9358
9359fail:
9360 if (new_stream)
9361 dc_stream_release(new_stream);
9362 return ret;
62f55537 9363}
9b690ef3 9364
f6ff2a08
NK
9365static bool should_reset_plane(struct drm_atomic_state *state,
9366 struct drm_plane *plane,
9367 struct drm_plane_state *old_plane_state,
9368 struct drm_plane_state *new_plane_state)
9369{
9370 struct drm_plane *other;
9371 struct drm_plane_state *old_other_state, *new_other_state;
9372 struct drm_crtc_state *new_crtc_state;
9373 int i;
9374
70a1efac
NK
9375 /*
9376 * TODO: Remove this hack once the checks below are sufficient
9377 * enough to determine when we need to reset all the planes on
9378 * the stream.
9379 */
9380 if (state->allow_modeset)
9381 return true;
9382
f6ff2a08
NK
9383 /* Exit early if we know that we're adding or removing the plane. */
9384 if (old_plane_state->crtc != new_plane_state->crtc)
9385 return true;
9386
9387 /* old crtc == new_crtc == NULL, plane not in context. */
9388 if (!new_plane_state->crtc)
9389 return false;
9390
9391 new_crtc_state =
9392 drm_atomic_get_new_crtc_state(state, new_plane_state->crtc);
9393
9394 if (!new_crtc_state)
9395 return true;
9396
7316c4ad
NK
9397 /* CRTC Degamma changes currently require us to recreate planes. */
9398 if (new_crtc_state->color_mgmt_changed)
9399 return true;
9400
f6ff2a08
NK
9401 if (drm_atomic_crtc_needs_modeset(new_crtc_state))
9402 return true;
9403
9404 /*
9405 * If there are any new primary or overlay planes being added or
9406 * removed then the z-order can potentially change. To ensure
9407 * correct z-order and pipe acquisition the current DC architecture
9408 * requires us to remove and recreate all existing planes.
9409 *
9410 * TODO: Come up with a more elegant solution for this.
9411 */
9412 for_each_oldnew_plane_in_state(state, other, old_other_state, new_other_state, i) {
6eed95b0 9413 struct amdgpu_framebuffer *old_afb, *new_afb;
f6ff2a08
NK
9414 if (other->type == DRM_PLANE_TYPE_CURSOR)
9415 continue;
9416
9417 if (old_other_state->crtc != new_plane_state->crtc &&
9418 new_other_state->crtc != new_plane_state->crtc)
9419 continue;
9420
9421 if (old_other_state->crtc != new_other_state->crtc)
9422 return true;
9423
dc4cb30d
NK
9424 /* Src/dst size and scaling updates. */
9425 if (old_other_state->src_w != new_other_state->src_w ||
9426 old_other_state->src_h != new_other_state->src_h ||
9427 old_other_state->crtc_w != new_other_state->crtc_w ||
9428 old_other_state->crtc_h != new_other_state->crtc_h)
9429 return true;
9430
9431 /* Rotation / mirroring updates. */
9432 if (old_other_state->rotation != new_other_state->rotation)
9433 return true;
9434
9435 /* Blending updates. */
9436 if (old_other_state->pixel_blend_mode !=
9437 new_other_state->pixel_blend_mode)
9438 return true;
9439
9440 /* Alpha updates. */
9441 if (old_other_state->alpha != new_other_state->alpha)
9442 return true;
9443
9444 /* Colorspace changes. */
9445 if (old_other_state->color_range != new_other_state->color_range ||
9446 old_other_state->color_encoding != new_other_state->color_encoding)
9447 return true;
9448
9a81cc60
NK
9449 /* Framebuffer checks fall at the end. */
9450 if (!old_other_state->fb || !new_other_state->fb)
9451 continue;
9452
9453 /* Pixel format changes can require bandwidth updates. */
9454 if (old_other_state->fb->format != new_other_state->fb->format)
9455 return true;
9456
6eed95b0
BN
9457 old_afb = (struct amdgpu_framebuffer *)old_other_state->fb;
9458 new_afb = (struct amdgpu_framebuffer *)new_other_state->fb;
9a81cc60
NK
9459
9460 /* Tiling and DCC changes also require bandwidth updates. */
37384b3f
BN
9461 if (old_afb->tiling_flags != new_afb->tiling_flags ||
9462 old_afb->base.modifier != new_afb->base.modifier)
f6ff2a08
NK
9463 return true;
9464 }
9465
9466 return false;
9467}
9468
b0455fda
SS
9469static int dm_check_cursor_fb(struct amdgpu_crtc *new_acrtc,
9470 struct drm_plane_state *new_plane_state,
9471 struct drm_framebuffer *fb)
9472{
e72868c4
SS
9473 struct amdgpu_device *adev = drm_to_adev(new_acrtc->base.dev);
9474 struct amdgpu_framebuffer *afb = to_amdgpu_framebuffer(fb);
b0455fda 9475 unsigned int pitch;
e72868c4 9476 bool linear;
b0455fda
SS
9477
9478 if (fb->width > new_acrtc->max_cursor_width ||
9479 fb->height > new_acrtc->max_cursor_height) {
9480 DRM_DEBUG_ATOMIC("Bad cursor FB size %dx%d\n",
9481 new_plane_state->fb->width,
9482 new_plane_state->fb->height);
9483 return -EINVAL;
9484 }
9485 if (new_plane_state->src_w != fb->width << 16 ||
9486 new_plane_state->src_h != fb->height << 16) {
9487 DRM_DEBUG_ATOMIC("Cropping not supported for cursor plane\n");
9488 return -EINVAL;
9489 }
9490
9491 /* Pitch in pixels */
9492 pitch = fb->pitches[0] / fb->format->cpp[0];
9493
9494 if (fb->width != pitch) {
9495 DRM_DEBUG_ATOMIC("Cursor FB width %d doesn't match pitch %d",
9496 fb->width, pitch);
9497 return -EINVAL;
9498 }
9499
9500 switch (pitch) {
9501 case 64:
9502 case 128:
9503 case 256:
9504 /* FB pitch is supported by cursor plane */
9505 break;
9506 default:
9507 DRM_DEBUG_ATOMIC("Bad cursor FB pitch %d px\n", pitch);
9508 return -EINVAL;
9509 }
9510
e72868c4
SS
9511 /* Core DRM takes care of checking FB modifiers, so we only need to
9512 * check tiling flags when the FB doesn't have a modifier. */
9513 if (!(fb->flags & DRM_MODE_FB_MODIFIERS)) {
9514 if (adev->family < AMDGPU_FAMILY_AI) {
9515 linear = AMDGPU_TILING_GET(afb->tiling_flags, ARRAY_MODE) != DC_ARRAY_2D_TILED_THIN1 &&
9516 AMDGPU_TILING_GET(afb->tiling_flags, ARRAY_MODE) != DC_ARRAY_1D_TILED_THIN1 &&
9517 AMDGPU_TILING_GET(afb->tiling_flags, MICRO_TILE_MODE) == 0;
9518 } else {
9519 linear = AMDGPU_TILING_GET(afb->tiling_flags, SWIZZLE_MODE) == 0;
9520 }
9521 if (!linear) {
9522 DRM_DEBUG_ATOMIC("Cursor FB not linear");
9523 return -EINVAL;
9524 }
9525 }
9526
b0455fda
SS
9527 return 0;
9528}
9529
9e869063
LL
9530static int dm_update_plane_state(struct dc *dc,
9531 struct drm_atomic_state *state,
9532 struct drm_plane *plane,
9533 struct drm_plane_state *old_plane_state,
9534 struct drm_plane_state *new_plane_state,
9535 bool enable,
35f33086
BL
9536 bool *lock_and_validation_needed,
9537 bool *is_top_most_overlay)
62f55537 9538{
eb3dc897
NK
9539
9540 struct dm_atomic_state *dm_state = NULL;
62f55537 9541 struct drm_crtc *new_plane_crtc, *old_plane_crtc;
0bc9706d 9542 struct drm_crtc_state *old_crtc_state, *new_crtc_state;
54d76575 9543 struct dm_crtc_state *dm_new_crtc_state, *dm_old_crtc_state;
54d76575 9544 struct dm_plane_state *dm_new_plane_state, *dm_old_plane_state;
626bf90f 9545 struct amdgpu_crtc *new_acrtc;
f6ff2a08 9546 bool needs_reset;
62f55537 9547 int ret = 0;
e7b07cee 9548
9b690ef3 9549
9e869063
LL
9550 new_plane_crtc = new_plane_state->crtc;
9551 old_plane_crtc = old_plane_state->crtc;
9552 dm_new_plane_state = to_dm_plane_state(new_plane_state);
9553 dm_old_plane_state = to_dm_plane_state(old_plane_state);
62f55537 9554
626bf90f
SS
9555 if (plane->type == DRM_PLANE_TYPE_CURSOR) {
9556 if (!enable || !new_plane_crtc ||
9557 drm_atomic_plane_disabling(plane->state, new_plane_state))
9558 return 0;
9559
9560 new_acrtc = to_amdgpu_crtc(new_plane_crtc);
9561
5f581248
SS
9562 if (new_plane_state->src_x != 0 || new_plane_state->src_y != 0) {
9563 DRM_DEBUG_ATOMIC("Cropping not supported for cursor plane\n");
9564 return -EINVAL;
9565 }
9566
24f99d2b 9567 if (new_plane_state->fb) {
b0455fda
SS
9568 ret = dm_check_cursor_fb(new_acrtc, new_plane_state,
9569 new_plane_state->fb);
9570 if (ret)
9571 return ret;
24f99d2b
SS
9572 }
9573
9e869063 9574 return 0;
626bf90f 9575 }
9b690ef3 9576
f6ff2a08
NK
9577 needs_reset = should_reset_plane(state, plane, old_plane_state,
9578 new_plane_state);
9579
9e869063
LL
9580 /* Remove any changed/removed planes */
9581 if (!enable) {
f6ff2a08 9582 if (!needs_reset)
9e869063 9583 return 0;
a7b06724 9584
9e869063
LL
9585 if (!old_plane_crtc)
9586 return 0;
62f55537 9587
9e869063
LL
9588 old_crtc_state = drm_atomic_get_old_crtc_state(
9589 state, old_plane_crtc);
9590 dm_old_crtc_state = to_dm_crtc_state(old_crtc_state);
9b690ef3 9591
9e869063
LL
9592 if (!dm_old_crtc_state->stream)
9593 return 0;
62f55537 9594
9e869063
LL
9595 DRM_DEBUG_ATOMIC("Disabling DRM plane: %d on DRM crtc %d\n",
9596 plane->base.id, old_plane_crtc->base.id);
9b690ef3 9597
9e869063
LL
9598 ret = dm_atomic_get_state(state, &dm_state);
9599 if (ret)
9600 return ret;
eb3dc897 9601
9e869063
LL
9602 if (!dc_remove_plane_from_context(
9603 dc,
9604 dm_old_crtc_state->stream,
9605 dm_old_plane_state->dc_state,
9606 dm_state->context)) {
62f55537 9607
c3537613 9608 return -EINVAL;
9e869063 9609 }
e7b07cee 9610
9b690ef3 9611
9e869063
LL
9612 dc_plane_state_release(dm_old_plane_state->dc_state);
9613 dm_new_plane_state->dc_state = NULL;
1dc90497 9614
9e869063 9615 *lock_and_validation_needed = true;
1dc90497 9616
9e869063
LL
9617 } else { /* Add new planes */
9618 struct dc_plane_state *dc_new_plane_state;
1dc90497 9619
9e869063
LL
9620 if (drm_atomic_plane_disabling(plane->state, new_plane_state))
9621 return 0;
e7b07cee 9622
9e869063
LL
9623 if (!new_plane_crtc)
9624 return 0;
e7b07cee 9625
9e869063
LL
9626 new_crtc_state = drm_atomic_get_new_crtc_state(state, new_plane_crtc);
9627 dm_new_crtc_state = to_dm_crtc_state(new_crtc_state);
1dc90497 9628
9e869063
LL
9629 if (!dm_new_crtc_state->stream)
9630 return 0;
62f55537 9631
f6ff2a08 9632 if (!needs_reset)
9e869063 9633 return 0;
62f55537 9634
8c44515b
AP
9635 ret = dm_plane_helper_check_state(new_plane_state, new_crtc_state);
9636 if (ret)
9637 return ret;
9638
9e869063 9639 WARN_ON(dm_new_plane_state->dc_state);
9b690ef3 9640
9e869063
LL
9641 dc_new_plane_state = dc_create_plane_state(dc);
9642 if (!dc_new_plane_state)
9643 return -ENOMEM;
62f55537 9644
35f33086
BL
9645 /* Block top most plane from being a video plane */
9646 if (plane->type == DRM_PLANE_TYPE_OVERLAY) {
9647 if (is_video_format(new_plane_state->fb->format->format) && *is_top_most_overlay)
9648 return -EINVAL;
9649 else
9650 *is_top_most_overlay = false;
9651 }
9652
4711c033
LT
9653 DRM_DEBUG_ATOMIC("Enabling DRM plane: %d on DRM crtc %d\n",
9654 plane->base.id, new_plane_crtc->base.id);
8c45c5db 9655
695af5f9 9656 ret = fill_dc_plane_attributes(
1348969a 9657 drm_to_adev(new_plane_crtc->dev),
9e869063
LL
9658 dc_new_plane_state,
9659 new_plane_state,
9660 new_crtc_state);
9661 if (ret) {
9662 dc_plane_state_release(dc_new_plane_state);
9663 return ret;
9664 }
62f55537 9665
9e869063
LL
9666 ret = dm_atomic_get_state(state, &dm_state);
9667 if (ret) {
9668 dc_plane_state_release(dc_new_plane_state);
9669 return ret;
9670 }
eb3dc897 9671
9e869063
LL
9672 /*
9673 * Any atomic check errors that occur after this will
9674 * not need a release. The plane state will be attached
9675 * to the stream, and therefore part of the atomic
9676 * state. It'll be released when the atomic state is
9677 * cleaned.
9678 */
9679 if (!dc_add_plane_to_context(
9680 dc,
9681 dm_new_crtc_state->stream,
9682 dc_new_plane_state,
9683 dm_state->context)) {
62f55537 9684
9e869063
LL
9685 dc_plane_state_release(dc_new_plane_state);
9686 return -EINVAL;
9687 }
8c45c5db 9688
9e869063 9689 dm_new_plane_state->dc_state = dc_new_plane_state;
000b59ea 9690
214993e1
ML
9691 dm_new_crtc_state->mpo_requested |= (plane->type == DRM_PLANE_TYPE_OVERLAY);
9692
9e869063
LL
9693 /* Tell DC to do a full surface update every time there
9694 * is a plane change. Inefficient, but works for now.
9695 */
9696 dm_new_plane_state->dc_state->update_flags.bits.full_update = 1;
9697
9698 *lock_and_validation_needed = true;
62f55537 9699 }
e7b07cee
HW
9700
9701
62f55537
AG
9702 return ret;
9703}
a87fa993 9704
69cb5629
VZ
9705static void dm_get_oriented_plane_size(struct drm_plane_state *plane_state,
9706 int *src_w, int *src_h)
9707{
9708 switch (plane_state->rotation & DRM_MODE_ROTATE_MASK) {
9709 case DRM_MODE_ROTATE_90:
9710 case DRM_MODE_ROTATE_270:
9711 *src_w = plane_state->src_h >> 16;
9712 *src_h = plane_state->src_w >> 16;
9713 break;
9714 case DRM_MODE_ROTATE_0:
9715 case DRM_MODE_ROTATE_180:
9716 default:
9717 *src_w = plane_state->src_w >> 16;
9718 *src_h = plane_state->src_h >> 16;
9719 break;
9720 }
9721}
9722
12f4849a
SS
9723static int dm_check_crtc_cursor(struct drm_atomic_state *state,
9724 struct drm_crtc *crtc,
9725 struct drm_crtc_state *new_crtc_state)
9726{
d1bfbe8a
SS
9727 struct drm_plane *cursor = crtc->cursor, *underlying;
9728 struct drm_plane_state *new_cursor_state, *new_underlying_state;
9729 int i;
9730 int cursor_scale_w, cursor_scale_h, underlying_scale_w, underlying_scale_h;
69cb5629
VZ
9731 int cursor_src_w, cursor_src_h;
9732 int underlying_src_w, underlying_src_h;
12f4849a
SS
9733
9734 /* On DCE and DCN there is no dedicated hardware cursor plane. We get a
9735 * cursor per pipe but it's going to inherit the scaling and
9736 * positioning from the underlying pipe. Check the cursor plane's
d1bfbe8a 9737 * blending properties match the underlying planes'. */
12f4849a 9738
d1bfbe8a
SS
9739 new_cursor_state = drm_atomic_get_new_plane_state(state, cursor);
9740 if (!new_cursor_state || !new_cursor_state->fb) {
12f4849a
SS
9741 return 0;
9742 }
9743
69cb5629
VZ
9744 dm_get_oriented_plane_size(new_cursor_state, &cursor_src_w, &cursor_src_h);
9745 cursor_scale_w = new_cursor_state->crtc_w * 1000 / cursor_src_w;
9746 cursor_scale_h = new_cursor_state->crtc_h * 1000 / cursor_src_h;
12f4849a 9747
d1bfbe8a
SS
9748 for_each_new_plane_in_state_reverse(state, underlying, new_underlying_state, i) {
9749 /* Narrow down to non-cursor planes on the same CRTC as the cursor */
9750 if (new_underlying_state->crtc != crtc || underlying == crtc->cursor)
9751 continue;
12f4849a 9752
d1bfbe8a
SS
9753 /* Ignore disabled planes */
9754 if (!new_underlying_state->fb)
9755 continue;
9756
69cb5629
VZ
9757 dm_get_oriented_plane_size(new_underlying_state,
9758 &underlying_src_w, &underlying_src_h);
9759 underlying_scale_w = new_underlying_state->crtc_w * 1000 / underlying_src_w;
9760 underlying_scale_h = new_underlying_state->crtc_h * 1000 / underlying_src_h;
d1bfbe8a
SS
9761
9762 if (cursor_scale_w != underlying_scale_w ||
9763 cursor_scale_h != underlying_scale_h) {
9764 drm_dbg_atomic(crtc->dev,
9765 "Cursor [PLANE:%d:%s] scaling doesn't match underlying [PLANE:%d:%s]\n",
9766 cursor->base.id, cursor->name, underlying->base.id, underlying->name);
9767 return -EINVAL;
9768 }
9769
9770 /* If this plane covers the whole CRTC, no need to check planes underneath */
9771 if (new_underlying_state->crtc_x <= 0 &&
9772 new_underlying_state->crtc_y <= 0 &&
9773 new_underlying_state->crtc_x + new_underlying_state->crtc_w >= new_crtc_state->mode.hdisplay &&
9774 new_underlying_state->crtc_y + new_underlying_state->crtc_h >= new_crtc_state->mode.vdisplay)
9775 break;
12f4849a
SS
9776 }
9777
9778 return 0;
9779}
9780
e10517b3 9781#if defined(CONFIG_DRM_AMD_DC_DCN)
44be939f
ML
9782static int add_affected_mst_dsc_crtcs(struct drm_atomic_state *state, struct drm_crtc *crtc)
9783{
9784 struct drm_connector *connector;
128f8ed5 9785 struct drm_connector_state *conn_state, *old_conn_state;
44be939f
ML
9786 struct amdgpu_dm_connector *aconnector = NULL;
9787 int i;
128f8ed5
RL
9788 for_each_oldnew_connector_in_state(state, connector, old_conn_state, conn_state, i) {
9789 if (!conn_state->crtc)
9790 conn_state = old_conn_state;
9791
44be939f
ML
9792 if (conn_state->crtc != crtc)
9793 continue;
9794
9795 aconnector = to_amdgpu_dm_connector(connector);
f0127cb1 9796 if (!aconnector->mst_output_port || !aconnector->mst_root)
44be939f
ML
9797 aconnector = NULL;
9798 else
9799 break;
9800 }
9801
9802 if (!aconnector)
9803 return 0;
9804
f0127cb1 9805 return drm_dp_mst_add_affected_dsc_crtcs(state, &aconnector->mst_root->mst_mgr);
44be939f 9806}
e10517b3 9807#endif
44be939f 9808
b8592b48
LL
9809/**
9810 * amdgpu_dm_atomic_check() - Atomic check implementation for AMDgpu DM.
c620e79b 9811 *
b8592b48
LL
9812 * @dev: The DRM device
9813 * @state: The atomic state to commit
9814 *
9815 * Validate that the given atomic state is programmable by DC into hardware.
9816 * This involves constructing a &struct dc_state reflecting the new hardware
9817 * state we wish to commit, then querying DC to see if it is programmable. It's
9818 * important not to modify the existing DC state. Otherwise, atomic_check
9819 * may unexpectedly commit hardware changes.
9820 *
9821 * When validating the DC state, it's important that the right locks are
9822 * acquired. For full updates case which removes/adds/updates streams on one
9823 * CRTC while flipping on another CRTC, acquiring global lock will guarantee
9824 * that any such full update commit will wait for completion of any outstanding
f6d7c7fa 9825 * flip using DRMs synchronization events.
b8592b48
LL
9826 *
9827 * Note that DM adds the affected connectors for all CRTCs in state, when that
9828 * might not seem necessary. This is because DC stream creation requires the
9829 * DC sink, which is tied to the DRM connector state. Cleaning this up should
9830 * be possible but non-trivial - a possible TODO item.
9831 *
9832 * Return: -Error code if validation failed.
9833 */
7578ecda
AD
9834static int amdgpu_dm_atomic_check(struct drm_device *dev,
9835 struct drm_atomic_state *state)
62f55537 9836{
1348969a 9837 struct amdgpu_device *adev = drm_to_adev(dev);
eb3dc897 9838 struct dm_atomic_state *dm_state = NULL;
62f55537 9839 struct dc *dc = adev->dm.dc;
62f55537 9840 struct drm_connector *connector;
c2cea706 9841 struct drm_connector_state *old_con_state, *new_con_state;
62f55537 9842 struct drm_crtc *crtc;
fc9e9920 9843 struct drm_crtc_state *old_crtc_state, *new_crtc_state;
9e869063
LL
9844 struct drm_plane *plane;
9845 struct drm_plane_state *old_plane_state, *new_plane_state;
74a16675 9846 enum dc_status status;
1e88ad0a 9847 int ret, i;
62f55537 9848 bool lock_and_validation_needed = false;
35f33086 9849 bool is_top_most_overlay = true;
214993e1 9850 struct dm_crtc_state *dm_old_crtc_state, *dm_new_crtc_state;
6513104b 9851#if defined(CONFIG_DRM_AMD_DC_DCN)
cdf657fc
DA
9852 struct drm_dp_mst_topology_mgr *mgr;
9853 struct drm_dp_mst_topology_state *mst_state;
6513104b
HW
9854 struct dsc_mst_fairness_vars vars[MAX_PIPES];
9855#endif
62f55537 9856
e8a98235 9857 trace_amdgpu_dm_atomic_check_begin(state);
c44a22b3 9858
62f55537 9859 ret = drm_atomic_helper_check_modeset(dev, state);
68ca1c3e
S
9860 if (ret) {
9861 DRM_DEBUG_DRIVER("drm_atomic_helper_check_modeset() failed\n");
01e28f9c 9862 goto fail;
68ca1c3e 9863 }
62f55537 9864
c5892a10
SW
9865 /* Check connector changes */
9866 for_each_oldnew_connector_in_state(state, connector, old_con_state, new_con_state, i) {
9867 struct dm_connector_state *dm_old_con_state = to_dm_connector_state(old_con_state);
9868 struct dm_connector_state *dm_new_con_state = to_dm_connector_state(new_con_state);
9869
9870 /* Skip connectors that are disabled or part of modeset already. */
c5892a10
SW
9871 if (!new_con_state->crtc)
9872 continue;
9873
9874 new_crtc_state = drm_atomic_get_crtc_state(state, new_con_state->crtc);
9875 if (IS_ERR(new_crtc_state)) {
68ca1c3e 9876 DRM_DEBUG_DRIVER("drm_atomic_get_crtc_state() failed\n");
c5892a10
SW
9877 ret = PTR_ERR(new_crtc_state);
9878 goto fail;
9879 }
9880
3c6d1aeb 9881 if (dm_old_con_state->abm_level != dm_new_con_state->abm_level ||
9882 dm_old_con_state->scaling != dm_new_con_state->scaling)
c5892a10
SW
9883 new_crtc_state->connectors_changed = true;
9884 }
9885
e10517b3 9886#if defined(CONFIG_DRM_AMD_DC_DCN)
349a19b2 9887 if (dc_resource_is_dsc_encoding_supported(dc)) {
44be939f
ML
9888 for_each_oldnew_crtc_in_state(state, crtc, old_crtc_state, new_crtc_state, i) {
9889 if (drm_atomic_crtc_needs_modeset(new_crtc_state)) {
9890 ret = add_affected_mst_dsc_crtcs(state, crtc);
68ca1c3e
S
9891 if (ret) {
9892 DRM_DEBUG_DRIVER("add_affected_mst_dsc_crtcs() failed\n");
44be939f 9893 goto fail;
68ca1c3e 9894 }
44be939f
ML
9895 }
9896 }
9897 }
e10517b3 9898#endif
1e88ad0a 9899 for_each_oldnew_crtc_in_state(state, crtc, old_crtc_state, new_crtc_state, i) {
886876ec
EB
9900 dm_old_crtc_state = to_dm_crtc_state(old_crtc_state);
9901
1e88ad0a 9902 if (!drm_atomic_crtc_needs_modeset(new_crtc_state) &&
98e6436d 9903 !new_crtc_state->color_mgmt_changed &&
886876ec
EB
9904 old_crtc_state->vrr_enabled == new_crtc_state->vrr_enabled &&
9905 dm_old_crtc_state->dsc_force_changed == false)
1e88ad0a 9906 continue;
7bef1af3 9907
03fc4cf4 9908 ret = amdgpu_dm_verify_lut_sizes(new_crtc_state);
68ca1c3e
S
9909 if (ret) {
9910 DRM_DEBUG_DRIVER("amdgpu_dm_verify_lut_sizes() failed\n");
03fc4cf4 9911 goto fail;
68ca1c3e 9912 }
03fc4cf4 9913
1e88ad0a
S
9914 if (!new_crtc_state->enable)
9915 continue;
fc9e9920 9916
1e88ad0a 9917 ret = drm_atomic_add_affected_connectors(state, crtc);
68ca1c3e
S
9918 if (ret) {
9919 DRM_DEBUG_DRIVER("drm_atomic_add_affected_connectors() failed\n");
706bc8c5 9920 goto fail;
68ca1c3e 9921 }
fc9e9920 9922
1e88ad0a 9923 ret = drm_atomic_add_affected_planes(state, crtc);
68ca1c3e
S
9924 if (ret) {
9925 DRM_DEBUG_DRIVER("drm_atomic_add_affected_planes() failed\n");
1e88ad0a 9926 goto fail;
68ca1c3e 9927 }
115a385c 9928
cbac53f7 9929 if (dm_old_crtc_state->dsc_force_changed)
115a385c 9930 new_crtc_state->mode_changed = true;
e7b07cee
HW
9931 }
9932
2d9e6431
NK
9933 /*
9934 * Add all primary and overlay planes on the CRTC to the state
9935 * whenever a plane is enabled to maintain correct z-ordering
9936 * and to enable fast surface updates.
9937 */
9938 drm_for_each_crtc(crtc, dev) {
9939 bool modified = false;
9940
9941 for_each_oldnew_plane_in_state(state, plane, old_plane_state, new_plane_state, i) {
9942 if (plane->type == DRM_PLANE_TYPE_CURSOR)
9943 continue;
9944
9945 if (new_plane_state->crtc == crtc ||
9946 old_plane_state->crtc == crtc) {
9947 modified = true;
9948 break;
9949 }
9950 }
9951
9952 if (!modified)
9953 continue;
9954
9955 drm_for_each_plane_mask(plane, state->dev, crtc->state->plane_mask) {
9956 if (plane->type == DRM_PLANE_TYPE_CURSOR)
9957 continue;
9958
9959 new_plane_state =
9960 drm_atomic_get_plane_state(state, plane);
9961
9962 if (IS_ERR(new_plane_state)) {
9963 ret = PTR_ERR(new_plane_state);
68ca1c3e 9964 DRM_DEBUG_DRIVER("new_plane_state is BAD\n");
2d9e6431
NK
9965 goto fail;
9966 }
9967 }
9968 }
9969
22c42b0e
LL
9970 /*
9971 * DC consults the zpos (layer_index in DC terminology) to determine the
9972 * hw plane on which to enable the hw cursor (see
9973 * `dcn10_can_pipe_disable_cursor`). By now, all modified planes are in
9974 * atomic state, so call drm helper to normalize zpos.
9975 */
ac0bb08d
LL
9976 ret = drm_atomic_normalize_zpos(dev, state);
9977 if (ret) {
9978 drm_dbg(dev, "drm_atomic_normalize_zpos() failed\n");
9979 goto fail;
9980 }
22c42b0e 9981
62f55537 9982 /* Remove exiting planes if they are modified */
9e869063
LL
9983 for_each_oldnew_plane_in_state_reverse(state, plane, old_plane_state, new_plane_state, i) {
9984 ret = dm_update_plane_state(dc, state, plane,
9985 old_plane_state,
9986 new_plane_state,
9987 false,
35f33086
BL
9988 &lock_and_validation_needed,
9989 &is_top_most_overlay);
68ca1c3e
S
9990 if (ret) {
9991 DRM_DEBUG_DRIVER("dm_update_plane_state() failed\n");
9e869063 9992 goto fail;
68ca1c3e 9993 }
62f55537
AG
9994 }
9995
9996 /* Disable all crtcs which require disable */
4b9674e5
LL
9997 for_each_oldnew_crtc_in_state(state, crtc, old_crtc_state, new_crtc_state, i) {
9998 ret = dm_update_crtc_state(&adev->dm, state, crtc,
9999 old_crtc_state,
10000 new_crtc_state,
10001 false,
10002 &lock_and_validation_needed);
68ca1c3e
S
10003 if (ret) {
10004 DRM_DEBUG_DRIVER("DISABLE: dm_update_crtc_state() failed\n");
4b9674e5 10005 goto fail;
68ca1c3e 10006 }
62f55537
AG
10007 }
10008
10009 /* Enable all crtcs which require enable */
4b9674e5
LL
10010 for_each_oldnew_crtc_in_state(state, crtc, old_crtc_state, new_crtc_state, i) {
10011 ret = dm_update_crtc_state(&adev->dm, state, crtc,
10012 old_crtc_state,
10013 new_crtc_state,
10014 true,
10015 &lock_and_validation_needed);
68ca1c3e
S
10016 if (ret) {
10017 DRM_DEBUG_DRIVER("ENABLE: dm_update_crtc_state() failed\n");
4b9674e5 10018 goto fail;
68ca1c3e 10019 }
62f55537
AG
10020 }
10021
10022 /* Add new/modified planes */
9e869063
LL
10023 for_each_oldnew_plane_in_state_reverse(state, plane, old_plane_state, new_plane_state, i) {
10024 ret = dm_update_plane_state(dc, state, plane,
10025 old_plane_state,
10026 new_plane_state,
10027 true,
35f33086
BL
10028 &lock_and_validation_needed,
10029 &is_top_most_overlay);
68ca1c3e
S
10030 if (ret) {
10031 DRM_DEBUG_DRIVER("dm_update_plane_state() failed\n");
9e869063 10032 goto fail;
68ca1c3e 10033 }
62f55537
AG
10034 }
10035
876fcc42
FZ
10036#if defined(CONFIG_DRM_AMD_DC_DCN)
10037 if (dc_resource_is_dsc_encoding_supported(dc)) {
7cce4cd6
LP
10038 ret = pre_validate_dsc(state, &dm_state, vars);
10039 if (ret != 0)
876fcc42 10040 goto fail;
876fcc42
FZ
10041 }
10042#endif
10043
b349f76e
ES
10044 /* Run this here since we want to validate the streams we created */
10045 ret = drm_atomic_helper_check_planes(dev, state);
68ca1c3e
S
10046 if (ret) {
10047 DRM_DEBUG_DRIVER("drm_atomic_helper_check_planes() failed\n");
b349f76e 10048 goto fail;
68ca1c3e 10049 }
62f55537 10050
214993e1
ML
10051 for_each_new_crtc_in_state(state, crtc, new_crtc_state, i) {
10052 dm_new_crtc_state = to_dm_crtc_state(new_crtc_state);
10053 if (dm_new_crtc_state->mpo_requested)
10054 DRM_DEBUG_DRIVER("MPO enablement requested on crtc:[%p]\n", crtc);
10055 }
10056
12f4849a
SS
10057 /* Check cursor planes scaling */
10058 for_each_new_crtc_in_state(state, crtc, new_crtc_state, i) {
10059 ret = dm_check_crtc_cursor(state, crtc, new_crtc_state);
68ca1c3e
S
10060 if (ret) {
10061 DRM_DEBUG_DRIVER("dm_check_crtc_cursor() failed\n");
12f4849a 10062 goto fail;
68ca1c3e 10063 }
12f4849a
SS
10064 }
10065
43d10d30
NK
10066 if (state->legacy_cursor_update) {
10067 /*
10068 * This is a fast cursor update coming from the plane update
10069 * helper, check if it can be done asynchronously for better
10070 * performance.
10071 */
10072 state->async_update =
10073 !drm_atomic_helper_async_check(dev, state);
10074
10075 /*
10076 * Skip the remaining global validation if this is an async
10077 * update. Cursor updates can be done without affecting
10078 * state or bandwidth calcs and this avoids the performance
10079 * penalty of locking the private state object and
10080 * allocating a new dc_state.
10081 */
10082 if (state->async_update)
10083 return 0;
10084 }
10085
ebdd27e1 10086 /* Check scaling and underscan changes*/
1f6010a9 10087 /* TODO Removed scaling changes validation due to inability to commit
e7b07cee
HW
10088 * new stream into context w\o causing full reset. Need to
10089 * decide how to handle.
10090 */
c2cea706 10091 for_each_oldnew_connector_in_state(state, connector, old_con_state, new_con_state, i) {
54d76575
LSL
10092 struct dm_connector_state *dm_old_con_state = to_dm_connector_state(old_con_state);
10093 struct dm_connector_state *dm_new_con_state = to_dm_connector_state(new_con_state);
10094 struct amdgpu_crtc *acrtc = to_amdgpu_crtc(dm_new_con_state->base.crtc);
e7b07cee
HW
10095
10096 /* Skip any modesets/resets */
0bc9706d
LSL
10097 if (!acrtc || drm_atomic_crtc_needs_modeset(
10098 drm_atomic_get_new_crtc_state(state, &acrtc->base)))
e7b07cee
HW
10099 continue;
10100
b830ebc9 10101 /* Skip any thing not scale or underscan changes */
54d76575 10102 if (!is_scaling_state_different(dm_new_con_state, dm_old_con_state))
e7b07cee
HW
10103 continue;
10104
10105 lock_and_validation_needed = true;
10106 }
10107
c689e1e3
LP
10108#if defined(CONFIG_DRM_AMD_DC_DCN)
10109 /* set the slot info for each mst_state based on the link encoding format */
10110 for_each_new_mst_mgr_in_state(state, mgr, mst_state, i) {
10111 struct amdgpu_dm_connector *aconnector;
10112 struct drm_connector *connector;
10113 struct drm_connector_list_iter iter;
10114 u8 link_coding_cap;
10115
10116 drm_connector_list_iter_begin(dev, &iter);
10117 drm_for_each_connector_iter(connector, &iter) {
10118 if (connector->index == mst_state->mgr->conn_base_id) {
10119 aconnector = to_amdgpu_dm_connector(connector);
10120 link_coding_cap = dc_link_dp_mst_decide_link_encoding_format(aconnector->dc_link);
10121 drm_dp_mst_update_slots(mst_state, link_coding_cap);
10122
10123 break;
10124 }
10125 }
10126 drm_connector_list_iter_end(&iter);
10127 }
10128#endif
10129
f6d7c7fa
NK
10130 /**
10131 * Streams and planes are reset when there are changes that affect
10132 * bandwidth. Anything that affects bandwidth needs to go through
10133 * DC global validation to ensure that the configuration can be applied
10134 * to hardware.
10135 *
10136 * We have to currently stall out here in atomic_check for outstanding
10137 * commits to finish in this case because our IRQ handlers reference
10138 * DRM state directly - we can end up disabling interrupts too early
10139 * if we don't.
10140 *
10141 * TODO: Remove this stall and drop DM state private objects.
a87fa993 10142 */
f6d7c7fa 10143 if (lock_and_validation_needed) {
eb3dc897 10144 ret = dm_atomic_get_state(state, &dm_state);
68ca1c3e
S
10145 if (ret) {
10146 DRM_DEBUG_DRIVER("dm_atomic_get_state() failed\n");
eb3dc897 10147 goto fail;
68ca1c3e 10148 }
e7b07cee
HW
10149
10150 ret = do_aquire_global_lock(dev, state);
68ca1c3e
S
10151 if (ret) {
10152 DRM_DEBUG_DRIVER("do_aquire_global_lock() failed\n");
e7b07cee 10153 goto fail;
68ca1c3e 10154 }
1dc90497 10155
d9fe1a4c 10156#if defined(CONFIG_DRM_AMD_DC_DCN)
7cce4cd6
LP
10157 ret = compute_mst_dsc_configs_for_state(state, dm_state->context, vars);
10158 if (ret) {
68ca1c3e 10159 DRM_DEBUG_DRIVER("compute_mst_dsc_configs_for_state() failed\n");
8c20a1ed 10160 goto fail;
68ca1c3e 10161 }
8c20a1ed 10162
6513104b 10163 ret = dm_update_mst_vcpi_slots_for_dsc(state, dm_state->context, vars);
68ca1c3e
S
10164 if (ret) {
10165 DRM_DEBUG_DRIVER("dm_update_mst_vcpi_slots_for_dsc() failed\n");
29b9ba74 10166 goto fail;
68ca1c3e 10167 }
d9fe1a4c 10168#endif
29b9ba74 10169
ded58c7b
ZL
10170 /*
10171 * Perform validation of MST topology in the state:
10172 * We need to perform MST atomic check before calling
10173 * dc_validate_global_state(), or there is a chance
10174 * to get stuck in an infinite loop and hang eventually.
10175 */
10176 ret = drm_dp_mst_atomic_check(state);
68ca1c3e
S
10177 if (ret) {
10178 DRM_DEBUG_DRIVER("drm_dp_mst_atomic_check() failed\n");
ded58c7b 10179 goto fail;
68ca1c3e 10180 }
85fb8bb9 10181 status = dc_validate_global_state(dc, dm_state->context, true);
74a16675 10182 if (status != DC_OK) {
68ca1c3e 10183 DRM_DEBUG_DRIVER("DC global validation failure: %s (%d)",
74a16675 10184 dc_status_to_str(status), status);
e7b07cee
HW
10185 ret = -EINVAL;
10186 goto fail;
10187 }
bd200d19 10188 } else {
674e78ac 10189 /*
bd200d19
NK
10190 * The commit is a fast update. Fast updates shouldn't change
10191 * the DC context, affect global validation, and can have their
10192 * commit work done in parallel with other commits not touching
10193 * the same resource. If we have a new DC context as part of
10194 * the DM atomic state from validation we need to free it and
10195 * retain the existing one instead.
fde9f39a
MR
10196 *
10197 * Furthermore, since the DM atomic state only contains the DC
10198 * context and can safely be annulled, we can free the state
10199 * and clear the associated private object now to free
10200 * some memory and avoid a possible use-after-free later.
674e78ac 10201 */
bd200d19 10202
fde9f39a
MR
10203 for (i = 0; i < state->num_private_objs; i++) {
10204 struct drm_private_obj *obj = state->private_objs[i].ptr;
bd200d19 10205
fde9f39a
MR
10206 if (obj->funcs == adev->dm.atomic_obj.funcs) {
10207 int j = state->num_private_objs-1;
bd200d19 10208
fde9f39a
MR
10209 dm_atomic_destroy_state(obj,
10210 state->private_objs[i].state);
10211
10212 /* If i is not at the end of the array then the
10213 * last element needs to be moved to where i was
10214 * before the array can safely be truncated.
10215 */
10216 if (i != j)
10217 state->private_objs[i] =
10218 state->private_objs[j];
bd200d19 10219
fde9f39a
MR
10220 state->private_objs[j].ptr = NULL;
10221 state->private_objs[j].state = NULL;
10222 state->private_objs[j].old_state = NULL;
10223 state->private_objs[j].new_state = NULL;
10224
10225 state->num_private_objs = j;
10226 break;
10227 }
bd200d19 10228 }
e7b07cee
HW
10229 }
10230
caff0e66
NK
10231 /* Store the overall update type for use later in atomic check. */
10232 for_each_new_crtc_in_state (state, crtc, new_crtc_state, i) {
10233 struct dm_crtc_state *dm_new_crtc_state =
10234 to_dm_crtc_state(new_crtc_state);
10235
f6d7c7fa
NK
10236 dm_new_crtc_state->update_type = lock_and_validation_needed ?
10237 UPDATE_TYPE_FULL :
10238 UPDATE_TYPE_FAST;
e7b07cee
HW
10239 }
10240
10241 /* Must be success */
10242 WARN_ON(ret);
e8a98235
RS
10243
10244 trace_amdgpu_dm_atomic_check_finish(state, ret);
10245
e7b07cee
HW
10246 return ret;
10247
10248fail:
10249 if (ret == -EDEADLK)
01e28f9c 10250 DRM_DEBUG_DRIVER("Atomic check stopped to avoid deadlock.\n");
e7b07cee 10251 else if (ret == -EINTR || ret == -EAGAIN || ret == -ERESTARTSYS)
01e28f9c 10252 DRM_DEBUG_DRIVER("Atomic check stopped due to signal.\n");
e7b07cee 10253 else
01e28f9c 10254 DRM_DEBUG_DRIVER("Atomic check failed with err: %d \n", ret);
e7b07cee 10255
e8a98235
RS
10256 trace_amdgpu_dm_atomic_check_finish(state, ret);
10257
e7b07cee
HW
10258 return ret;
10259}
10260
3ee6b26b
AD
10261static bool is_dp_capable_without_timing_msa(struct dc *dc,
10262 struct amdgpu_dm_connector *amdgpu_dm_connector)
e7b07cee 10263{
ae67558b 10264 u8 dpcd_data;
e7b07cee
HW
10265 bool capable = false;
10266
c84dec2f 10267 if (amdgpu_dm_connector->dc_link &&
e7b07cee
HW
10268 dm_helpers_dp_read_dpcd(
10269 NULL,
c84dec2f 10270 amdgpu_dm_connector->dc_link,
e7b07cee
HW
10271 DP_DOWN_STREAM_PORT_COUNT,
10272 &dpcd_data,
10273 sizeof(dpcd_data))) {
10274 capable = (dpcd_data & DP_MSA_TIMING_PAR_IGNORED) ? true:false;
10275 }
10276
10277 return capable;
10278}
f9b4f20c 10279
46db138d
SW
10280static bool dm_edid_parser_send_cea(struct amdgpu_display_manager *dm,
10281 unsigned int offset,
10282 unsigned int total_length,
ae67558b 10283 u8 *data,
46db138d
SW
10284 unsigned int length,
10285 struct amdgpu_hdmi_vsdb_info *vsdb)
10286{
10287 bool res;
10288 union dmub_rb_cmd cmd;
10289 struct dmub_cmd_send_edid_cea *input;
10290 struct dmub_cmd_edid_cea_output *output;
10291
10292 if (length > DMUB_EDID_CEA_DATA_CHUNK_BYTES)
10293 return false;
10294
10295 memset(&cmd, 0, sizeof(cmd));
10296
10297 input = &cmd.edid_cea.data.input;
10298
10299 cmd.edid_cea.header.type = DMUB_CMD__EDID_CEA;
10300 cmd.edid_cea.header.sub_type = 0;
10301 cmd.edid_cea.header.payload_bytes =
10302 sizeof(cmd.edid_cea) - sizeof(cmd.edid_cea.header);
10303 input->offset = offset;
10304 input->length = length;
eb9e59eb 10305 input->cea_total_length = total_length;
46db138d
SW
10306 memcpy(input->payload, data, length);
10307
10308 res = dc_dmub_srv_cmd_with_reply_data(dm->dc->ctx->dmub_srv, &cmd);
10309 if (!res) {
10310 DRM_ERROR("EDID CEA parser failed\n");
10311 return false;
10312 }
10313
10314 output = &cmd.edid_cea.data.output;
10315
10316 if (output->type == DMUB_CMD__EDID_CEA_ACK) {
10317 if (!output->ack.success) {
10318 DRM_ERROR("EDID CEA ack failed at offset %d\n",
10319 output->ack.offset);
10320 }
10321 } else if (output->type == DMUB_CMD__EDID_CEA_AMD_VSDB) {
10322 if (!output->amd_vsdb.vsdb_found)
10323 return false;
10324
10325 vsdb->freesync_supported = output->amd_vsdb.freesync_supported;
10326 vsdb->amd_vsdb_version = output->amd_vsdb.amd_vsdb_version;
10327 vsdb->min_refresh_rate_hz = output->amd_vsdb.min_frame_rate;
10328 vsdb->max_refresh_rate_hz = output->amd_vsdb.max_frame_rate;
10329 } else {
b76a8062 10330 DRM_WARN("Unknown EDID CEA parser results\n");
46db138d
SW
10331 return false;
10332 }
10333
10334 return true;
10335}
10336
10337static bool parse_edid_cea_dmcu(struct amdgpu_display_manager *dm,
ae67558b 10338 u8 *edid_ext, int len,
f9b4f20c
SW
10339 struct amdgpu_hdmi_vsdb_info *vsdb_info)
10340{
10341 int i;
f9b4f20c
SW
10342
10343 /* send extension block to DMCU for parsing */
10344 for (i = 0; i < len; i += 8) {
10345 bool res;
10346 int offset;
10347
10348 /* send 8 bytes a time */
46db138d 10349 if (!dc_edid_parser_send_cea(dm->dc, i, len, &edid_ext[i], 8))
f9b4f20c
SW
10350 return false;
10351
10352 if (i+8 == len) {
10353 /* EDID block sent completed, expect result */
10354 int version, min_rate, max_rate;
10355
46db138d 10356 res = dc_edid_parser_recv_amd_vsdb(dm->dc, &version, &min_rate, &max_rate);
f9b4f20c
SW
10357 if (res) {
10358 /* amd vsdb found */
10359 vsdb_info->freesync_supported = 1;
10360 vsdb_info->amd_vsdb_version = version;
10361 vsdb_info->min_refresh_rate_hz = min_rate;
10362 vsdb_info->max_refresh_rate_hz = max_rate;
10363 return true;
10364 }
10365 /* not amd vsdb */
10366 return false;
10367 }
10368
10369 /* check for ack*/
46db138d 10370 res = dc_edid_parser_recv_cea_ack(dm->dc, &offset);
f9b4f20c
SW
10371 if (!res)
10372 return false;
10373 }
10374
10375 return false;
10376}
10377
46db138d 10378static bool parse_edid_cea_dmub(struct amdgpu_display_manager *dm,
ae67558b 10379 u8 *edid_ext, int len,
46db138d
SW
10380 struct amdgpu_hdmi_vsdb_info *vsdb_info)
10381{
10382 int i;
10383
10384 /* send extension block to DMCU for parsing */
10385 for (i = 0; i < len; i += 8) {
10386 /* send 8 bytes a time */
10387 if (!dm_edid_parser_send_cea(dm, i, len, &edid_ext[i], 8, vsdb_info))
10388 return false;
10389 }
10390
10391 return vsdb_info->freesync_supported;
10392}
10393
10394static bool parse_edid_cea(struct amdgpu_dm_connector *aconnector,
ae67558b 10395 u8 *edid_ext, int len,
46db138d
SW
10396 struct amdgpu_hdmi_vsdb_info *vsdb_info)
10397{
10398 struct amdgpu_device *adev = drm_to_adev(aconnector->base.dev);
53f4da73 10399 bool ret;
46db138d 10400
53f4da73 10401 mutex_lock(&adev->dm.dc_lock);
46db138d 10402 if (adev->dm.dmub_srv)
53f4da73 10403 ret = parse_edid_cea_dmub(&adev->dm, edid_ext, len, vsdb_info);
46db138d 10404 else
53f4da73
SW
10405 ret = parse_edid_cea_dmcu(&adev->dm, edid_ext, len, vsdb_info);
10406 mutex_unlock(&adev->dm.dc_lock);
10407 return ret;
46db138d
SW
10408}
10409
7c7dd774 10410static int parse_hdmi_amd_vsdb(struct amdgpu_dm_connector *aconnector,
f9b4f20c
SW
10411 struct edid *edid, struct amdgpu_hdmi_vsdb_info *vsdb_info)
10412{
ae67558b 10413 u8 *edid_ext = NULL;
f9b4f20c
SW
10414 int i;
10415 bool valid_vsdb_found = false;
10416
10417 /*----- drm_find_cea_extension() -----*/
10418 /* No EDID or EDID extensions */
10419 if (edid == NULL || edid->extensions == 0)
7c7dd774 10420 return -ENODEV;
f9b4f20c
SW
10421
10422 /* Find CEA extension */
10423 for (i = 0; i < edid->extensions; i++) {
10424 edid_ext = (uint8_t *)edid + EDID_LENGTH * (i + 1);
10425 if (edid_ext[0] == CEA_EXT)
10426 break;
10427 }
10428
10429 if (i == edid->extensions)
7c7dd774 10430 return -ENODEV;
f9b4f20c
SW
10431
10432 /*----- cea_db_offsets() -----*/
10433 if (edid_ext[0] != CEA_EXT)
7c7dd774 10434 return -ENODEV;
f9b4f20c
SW
10435
10436 valid_vsdb_found = parse_edid_cea(aconnector, edid_ext, EDID_LENGTH, vsdb_info);
7c7dd774
AB
10437
10438 return valid_vsdb_found ? i : -ENODEV;
f9b4f20c
SW
10439}
10440
c620e79b
RS
10441/**
10442 * amdgpu_dm_update_freesync_caps - Update Freesync capabilities
10443 *
41ee1f18
AD
10444 * @connector: Connector to query.
10445 * @edid: EDID from monitor
c620e79b
RS
10446 *
10447 * Amdgpu supports Freesync in DP and HDMI displays, and it is required to keep
10448 * track of some of the display information in the internal data struct used by
10449 * amdgpu_dm. This function checks which type of connector we need to set the
10450 * FreeSync parameters.
10451 */
98e6436d 10452void amdgpu_dm_update_freesync_caps(struct drm_connector *connector,
c620e79b 10453 struct edid *edid)
e7b07cee 10454{
eb0709ba 10455 int i = 0;
e7b07cee
HW
10456 struct detailed_timing *timing;
10457 struct detailed_non_pixel *data;
10458 struct detailed_data_monitor_range *range;
c84dec2f
HW
10459 struct amdgpu_dm_connector *amdgpu_dm_connector =
10460 to_amdgpu_dm_connector(connector);
bb47de73 10461 struct dm_connector_state *dm_con_state = NULL;
9ad54467 10462 struct dc_sink *sink;
e7b07cee
HW
10463
10464 struct drm_device *dev = connector->dev;
1348969a 10465 struct amdgpu_device *adev = drm_to_adev(dev);
f9b4f20c 10466 struct amdgpu_hdmi_vsdb_info vsdb_info = {0};
c620e79b 10467 bool freesync_capable = false;
5b49da02 10468 enum adaptive_sync_type as_type = ADAPTIVE_SYNC_TYPE_NONE;
b830ebc9 10469
8218d7f1
HW
10470 if (!connector->state) {
10471 DRM_ERROR("%s - Connector has no state", __func__);
bb47de73 10472 goto update;
8218d7f1
HW
10473 }
10474
9b2fdc33
AP
10475 sink = amdgpu_dm_connector->dc_sink ?
10476 amdgpu_dm_connector->dc_sink :
10477 amdgpu_dm_connector->dc_em_sink;
10478
10479 if (!edid || !sink) {
98e6436d
AK
10480 dm_con_state = to_dm_connector_state(connector->state);
10481
10482 amdgpu_dm_connector->min_vfreq = 0;
10483 amdgpu_dm_connector->max_vfreq = 0;
10484 amdgpu_dm_connector->pixel_clock_mhz = 0;
9b2fdc33
AP
10485 connector->display_info.monitor_range.min_vfreq = 0;
10486 connector->display_info.monitor_range.max_vfreq = 0;
10487 freesync_capable = false;
98e6436d 10488
bb47de73 10489 goto update;
98e6436d
AK
10490 }
10491
8218d7f1
HW
10492 dm_con_state = to_dm_connector_state(connector->state);
10493
e7b07cee 10494 if (!adev->dm.freesync_module)
bb47de73 10495 goto update;
f9b4f20c 10496
9b2fdc33
AP
10497 if (sink->sink_signal == SIGNAL_TYPE_DISPLAY_PORT
10498 || sink->sink_signal == SIGNAL_TYPE_EDP) {
f9b4f20c
SW
10499 bool edid_check_required = false;
10500
10501 if (edid) {
e7b07cee
HW
10502 edid_check_required = is_dp_capable_without_timing_msa(
10503 adev->dm.dc,
c84dec2f 10504 amdgpu_dm_connector);
e7b07cee 10505 }
e7b07cee 10506
f9b4f20c
SW
10507 if (edid_check_required == true && (edid->version > 1 ||
10508 (edid->version == 1 && edid->revision > 1))) {
10509 for (i = 0; i < 4; i++) {
e7b07cee 10510
f9b4f20c
SW
10511 timing = &edid->detailed_timings[i];
10512 data = &timing->data.other_data;
10513 range = &data->data.range;
10514 /*
10515 * Check if monitor has continuous frequency mode
10516 */
10517 if (data->type != EDID_DETAIL_MONITOR_RANGE)
10518 continue;
10519 /*
10520 * Check for flag range limits only. If flag == 1 then
10521 * no additional timing information provided.
10522 * Default GTF, GTF Secondary curve and CVT are not
10523 * supported
10524 */
10525 if (range->flags != 1)
10526 continue;
a0ffc3fd 10527
f9b4f20c
SW
10528 amdgpu_dm_connector->min_vfreq = range->min_vfreq;
10529 amdgpu_dm_connector->max_vfreq = range->max_vfreq;
10530 amdgpu_dm_connector->pixel_clock_mhz =
10531 range->pixel_clock_mhz * 10;
a0ffc3fd 10532
f9b4f20c
SW
10533 connector->display_info.monitor_range.min_vfreq = range->min_vfreq;
10534 connector->display_info.monitor_range.max_vfreq = range->max_vfreq;
e7b07cee 10535
f9b4f20c
SW
10536 break;
10537 }
98e6436d 10538
f9b4f20c
SW
10539 if (amdgpu_dm_connector->max_vfreq -
10540 amdgpu_dm_connector->min_vfreq > 10) {
98e6436d 10541
f9b4f20c
SW
10542 freesync_capable = true;
10543 }
10544 }
9b2fdc33 10545 } else if (edid && sink->sink_signal == SIGNAL_TYPE_HDMI_TYPE_A) {
7c7dd774
AB
10546 i = parse_hdmi_amd_vsdb(amdgpu_dm_connector, edid, &vsdb_info);
10547 if (i >= 0 && vsdb_info.freesync_supported) {
f9b4f20c
SW
10548 timing = &edid->detailed_timings[i];
10549 data = &timing->data.other_data;
10550
10551 amdgpu_dm_connector->min_vfreq = vsdb_info.min_refresh_rate_hz;
5b49da02
SJK
10552 amdgpu_dm_connector->max_vfreq = vsdb_info.max_refresh_rate_hz;
10553 if (amdgpu_dm_connector->max_vfreq - amdgpu_dm_connector->min_vfreq > 10)
10554 freesync_capable = true;
10555
10556 connector->display_info.monitor_range.min_vfreq = vsdb_info.min_refresh_rate_hz;
10557 connector->display_info.monitor_range.max_vfreq = vsdb_info.max_refresh_rate_hz;
10558 }
10559 }
10560
10561 as_type = dm_get_adaptive_sync_support_type(amdgpu_dm_connector->dc_link);
10562
10563 if (as_type == FREESYNC_TYPE_PCON_IN_WHITELIST) {
10564 i = parse_hdmi_amd_vsdb(amdgpu_dm_connector, edid, &vsdb_info);
10565 if (i >= 0 && vsdb_info.freesync_supported && vsdb_info.amd_vsdb_version > 0) {
10566
10567 amdgpu_dm_connector->pack_sdp_v1_3 = true;
10568 amdgpu_dm_connector->as_type = as_type;
10569 amdgpu_dm_connector->vsdb_info = vsdb_info;
10570
10571 amdgpu_dm_connector->min_vfreq = vsdb_info.min_refresh_rate_hz;
f9b4f20c
SW
10572 amdgpu_dm_connector->max_vfreq = vsdb_info.max_refresh_rate_hz;
10573 if (amdgpu_dm_connector->max_vfreq - amdgpu_dm_connector->min_vfreq > 10)
10574 freesync_capable = true;
10575
10576 connector->display_info.monitor_range.min_vfreq = vsdb_info.min_refresh_rate_hz;
10577 connector->display_info.monitor_range.max_vfreq = vsdb_info.max_refresh_rate_hz;
e7b07cee
HW
10578 }
10579 }
bb47de73
NK
10580
10581update:
10582 if (dm_con_state)
10583 dm_con_state->freesync_capable = freesync_capable;
10584
10585 if (connector->vrr_capable_property)
10586 drm_connector_set_vrr_capable_property(connector,
10587 freesync_capable);
e7b07cee
HW
10588}
10589
3d4e52d0
VL
10590void amdgpu_dm_trigger_timing_sync(struct drm_device *dev)
10591{
1348969a 10592 struct amdgpu_device *adev = drm_to_adev(dev);
3d4e52d0
VL
10593 struct dc *dc = adev->dm.dc;
10594 int i;
10595
10596 mutex_lock(&adev->dm.dc_lock);
10597 if (dc->current_state) {
10598 for (i = 0; i < dc->current_state->stream_count; ++i)
10599 dc->current_state->streams[i]
10600 ->triggered_crtc_reset.enabled =
10601 adev->dm.force_timing_sync;
10602
10603 dm_enable_per_frame_crtc_master_sync(dc->current_state);
10604 dc_trigger_sync(dc, dc->current_state);
10605 }
10606 mutex_unlock(&adev->dm.dc_lock);
10607}
9d83722d
RS
10608
10609void dm_write_reg_func(const struct dc_context *ctx, uint32_t address,
ae67558b 10610 u32 value, const char *func_name)
9d83722d
RS
10611{
10612#ifdef DM_CHECK_ADDR_0
10613 if (address == 0) {
10614 DC_ERR("invalid register write. address = 0");
10615 return;
10616 }
10617#endif
10618 cgs_write_register(ctx->cgs_device, address, value);
10619 trace_amdgpu_dc_wreg(&ctx->perf_trace->write_count, address, value);
10620}
10621
10622uint32_t dm_read_reg_func(const struct dc_context *ctx, uint32_t address,
10623 const char *func_name)
10624{
ae67558b 10625 u32 value;
9d83722d
RS
10626#ifdef DM_CHECK_ADDR_0
10627 if (address == 0) {
10628 DC_ERR("invalid register read; address = 0\n");
10629 return 0;
10630 }
10631#endif
10632
10633 if (ctx->dmub_srv &&
10634 ctx->dmub_srv->reg_helper_offload.gather_in_progress &&
10635 !ctx->dmub_srv->reg_helper_offload.should_burst_write) {
10636 ASSERT(false);
10637 return 0;
10638 }
10639
10640 value = cgs_read_register(ctx->cgs_device, address);
10641
10642 trace_amdgpu_dc_rreg(&ctx->perf_trace->read_count, address, value);
10643
10644 return value;
10645}
81927e28 10646
ead08b95
SW
10647int amdgpu_dm_process_dmub_aux_transfer_sync(
10648 struct dc_context *ctx,
10649 unsigned int link_index,
10650 struct aux_payload *payload,
10651 enum aux_return_code_type *operation_result)
88f52b1f
JS
10652{
10653 struct amdgpu_device *adev = ctx->driver_context;
88f52b1f 10654 struct dmub_notification *p_notify = adev->dm.dmub_notify;
ead08b95 10655 int ret = -1;
88f52b1f 10656
ead08b95
SW
10657 mutex_lock(&adev->dm.dpia_aux_lock);
10658 if (!dc_process_dmub_aux_transfer_async(ctx->dc, link_index, payload)) {
10659 *operation_result = AUX_RET_ERROR_ENGINE_ACQUIRE;
10660 goto out;
10661 }
10662
10663 if (!wait_for_completion_timeout(&adev->dm.dmub_aux_transfer_done, 10 * HZ)) {
10664 DRM_ERROR("wait_for_completion_timeout timeout!");
10665 *operation_result = AUX_RET_ERROR_TIMEOUT;
10666 goto out;
10667 }
10668
10669 if (p_notify->result != AUX_RET_SUCCESS) {
10670 /*
10671 * Transient states before tunneling is enabled could
10672 * lead to this error. We can ignore this for now.
10673 */
10674 if (p_notify->result != AUX_RET_ERROR_PROTOCOL_ERROR) {
10675 DRM_WARN("DPIA AUX failed on 0x%x(%d), error %d\n",
10676 payload->address, payload->length,
10677 p_notify->result);
88f52b1f 10678 }
ead08b95
SW
10679 *operation_result = AUX_RET_ERROR_INVALID_REPLY;
10680 goto out;
10681 }
10682
10683
10684 payload->reply[0] = adev->dm.dmub_notify->aux_reply.command;
10685 if (!payload->write && p_notify->aux_reply.length &&
10686 (payload->reply[0] == AUX_TRANSACTION_REPLY_AUX_ACK)) {
10687
10688 if (payload->length != p_notify->aux_reply.length) {
10689 DRM_WARN("invalid read length %d from DPIA AUX 0x%x(%d)!\n",
10690 p_notify->aux_reply.length,
10691 payload->address, payload->length);
10692 *operation_result = AUX_RET_ERROR_INVALID_REPLY;
10693 goto out;
88f52b1f 10694 }
ead08b95
SW
10695
10696 memcpy(payload->data, p_notify->aux_reply.data,
10697 p_notify->aux_reply.length);
88f52b1f
JS
10698 }
10699
ead08b95
SW
10700 /* success */
10701 ret = p_notify->aux_reply.length;
10702 *operation_result = p_notify->result;
10703out:
0cf8307a 10704 reinit_completion(&adev->dm.dmub_aux_transfer_done);
ead08b95
SW
10705 mutex_unlock(&adev->dm.dpia_aux_lock);
10706 return ret;
88f52b1f
JS
10707}
10708
ead08b95
SW
10709int amdgpu_dm_process_dmub_set_config_sync(
10710 struct dc_context *ctx,
10711 unsigned int link_index,
10712 struct set_config_cmd_payload *payload,
10713 enum set_config_status *operation_result)
81927e28
JS
10714{
10715 struct amdgpu_device *adev = ctx->driver_context;
ead08b95
SW
10716 bool is_cmd_complete;
10717 int ret;
81927e28 10718
ead08b95
SW
10719 mutex_lock(&adev->dm.dpia_aux_lock);
10720 is_cmd_complete = dc_process_dmub_set_config_async(ctx->dc,
10721 link_index, payload, adev->dm.dmub_notify);
88f52b1f 10722
ead08b95
SW
10723 if (is_cmd_complete || wait_for_completion_timeout(&adev->dm.dmub_aux_transfer_done, 10 * HZ)) {
10724 ret = 0;
10725 *operation_result = adev->dm.dmub_notify->sc_status;
10726 } else {
9e3a50d2 10727 DRM_ERROR("wait_for_completion_timeout timeout!");
ead08b95
SW
10728 ret = -1;
10729 *operation_result = SET_CONFIG_UNKNOWN_ERROR;
81927e28
JS
10730 }
10731
0cf8307a
SW
10732 if (!is_cmd_complete)
10733 reinit_completion(&adev->dm.dmub_aux_transfer_done);
ead08b95
SW
10734 mutex_unlock(&adev->dm.dpia_aux_lock);
10735 return ret;
81927e28 10736}
1edf5ae1
ZL
10737
10738/*
10739 * Check whether seamless boot is supported.
10740 *
10741 * So far we only support seamless boot on CHIP_VANGOGH.
10742 * If everything goes well, we may consider expanding
10743 * seamless boot to other ASICs.
10744 */
10745bool check_seamless_boot_capability(struct amdgpu_device *adev)
10746{
20875141
PY
10747 switch (adev->ip_versions[DCE_HWIP][0]) {
10748 case IP_VERSION(3, 0, 1):
1edf5ae1
ZL
10749 if (!adev->mman.keep_stolen_vga_memory)
10750 return true;
10751 break;
10752 default:
10753 break;
10754 }
10755
10756 return false;
10757}