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