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