drm/amd/display: Fix and simplify apply_below_the_range()
[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"
1dc90497 31#include "dc/inc/core_types.h"
4562236b
HW
32
33#include "vid.h"
34#include "amdgpu.h"
a49dcb88 35#include "amdgpu_display.h"
a94d5569 36#include "amdgpu_ucode.h"
4562236b
HW
37#include "atom.h"
38#include "amdgpu_dm.h"
e7b07cee 39#include "amdgpu_pm.h"
4562236b
HW
40
41#include "amd_shared.h"
42#include "amdgpu_dm_irq.h"
43#include "dm_helpers.h"
e7b07cee 44#include "amdgpu_dm_mst_types.h"
dc38fd9d
DF
45#if defined(CONFIG_DEBUG_FS)
46#include "amdgpu_dm_debugfs.h"
47#endif
4562236b
HW
48
49#include "ivsrcid/ivsrcid_vislands30.h"
50
51#include <linux/module.h>
52#include <linux/moduleparam.h>
53#include <linux/version.h>
e7b07cee 54#include <linux/types.h>
97028037 55#include <linux/pm_runtime.h>
a94d5569 56#include <linux/firmware.h>
4562236b 57
e7b07cee 58#include <drm/drmP.h>
4562236b 59#include <drm/drm_atomic.h>
674e78ac 60#include <drm/drm_atomic_uapi.h>
4562236b
HW
61#include <drm/drm_atomic_helper.h>
62#include <drm/drm_dp_mst_helper.h>
e7b07cee
HW
63#include <drm/drm_fb_helper.h>
64#include <drm/drm_edid.h>
4562236b 65
ff5ef992
AD
66#if defined(CONFIG_DRM_AMD_DC_DCN1_0)
67#include "ivsrcid/irqsrcs_dcn_1_0.h"
68
ad941f7a
FX
69#include "dcn/dcn_1_0_offset.h"
70#include "dcn/dcn_1_0_sh_mask.h"
407e7517
HZ
71#include "soc15_hw_ip.h"
72#include "vega10_ip_offset.h"
ff5ef992
AD
73
74#include "soc15_common.h"
75#endif
76
e7b07cee 77#include "modules/inc/mod_freesync.h"
bbf854dc 78#include "modules/power/power_helpers.h"
ecd0136b 79#include "modules/inc/mod_info_packet.h"
e7b07cee 80
a94d5569
DF
81#define FIRMWARE_RAVEN_DMCU "amdgpu/raven_dmcu.bin"
82MODULE_FIRMWARE(FIRMWARE_RAVEN_DMCU);
e7b07cee 83
b8592b48
LL
84/**
85 * DOC: overview
86 *
87 * The AMDgpu display manager, **amdgpu_dm** (or even simpler,
88 * **dm**) sits between DRM and DC. It acts as a liason, converting DRM
89 * requests into DC requests, and DC responses into DRM responses.
90 *
91 * The root control structure is &struct amdgpu_display_manager.
92 */
93
7578ecda
AD
94/* basic init/fini API */
95static int amdgpu_dm_init(struct amdgpu_device *adev);
96static void amdgpu_dm_fini(struct amdgpu_device *adev);
97
1f6010a9
DF
98/*
99 * initializes drm_device display related structures, based on the information
7578ecda
AD
100 * provided by DAL. The drm strcutures are: drm_crtc, drm_connector,
101 * drm_encoder, drm_mode_config
102 *
103 * Returns 0 on success
104 */
105static int amdgpu_dm_initialize_drm_device(struct amdgpu_device *adev);
106/* removes and deallocates the drm structures, created by the above function */
107static void amdgpu_dm_destroy_drm_device(struct amdgpu_display_manager *dm);
108
109static void
110amdgpu_dm_update_connector_after_detect(struct amdgpu_dm_connector *aconnector);
111
112static int amdgpu_dm_plane_init(struct amdgpu_display_manager *dm,
f180b4bc 113 struct drm_plane *plane,
cc1fec57
NK
114 unsigned long possible_crtcs,
115 const struct dc_plane_cap *plane_cap);
7578ecda
AD
116static int amdgpu_dm_crtc_init(struct amdgpu_display_manager *dm,
117 struct drm_plane *plane,
118 uint32_t link_index);
119static int amdgpu_dm_connector_init(struct amdgpu_display_manager *dm,
120 struct amdgpu_dm_connector *amdgpu_dm_connector,
121 uint32_t link_index,
122 struct amdgpu_encoder *amdgpu_encoder);
123static int amdgpu_dm_encoder_init(struct drm_device *dev,
124 struct amdgpu_encoder *aencoder,
125 uint32_t link_index);
126
127static int amdgpu_dm_connector_get_modes(struct drm_connector *connector);
128
129static int amdgpu_dm_atomic_commit(struct drm_device *dev,
130 struct drm_atomic_state *state,
131 bool nonblock);
132
133static void amdgpu_dm_atomic_commit_tail(struct drm_atomic_state *state);
134
135static int amdgpu_dm_atomic_check(struct drm_device *dev,
136 struct drm_atomic_state *state);
137
674e78ac
NK
138static void handle_cursor_update(struct drm_plane *plane,
139 struct drm_plane_state *old_plane_state);
7578ecda 140
4562236b
HW
141/*
142 * dm_vblank_get_counter
143 *
144 * @brief
145 * Get counter for number of vertical blanks
146 *
147 * @param
148 * struct amdgpu_device *adev - [in] desired amdgpu device
149 * int disp_idx - [in] which CRTC to get the counter from
150 *
151 * @return
152 * Counter for vertical blanks
153 */
154static u32 dm_vblank_get_counter(struct amdgpu_device *adev, int crtc)
155{
156 if (crtc >= adev->mode_info.num_crtc)
157 return 0;
158 else {
159 struct amdgpu_crtc *acrtc = adev->mode_info.crtcs[crtc];
da5c47f6
AG
160 struct dm_crtc_state *acrtc_state = to_dm_crtc_state(
161 acrtc->base.state);
4562236b 162
da5c47f6
AG
163
164 if (acrtc_state->stream == NULL) {
0971c40e
HW
165 DRM_ERROR("dc_stream_state is NULL for crtc '%d'!\n",
166 crtc);
4562236b
HW
167 return 0;
168 }
169
da5c47f6 170 return dc_stream_get_vblank_counter(acrtc_state->stream);
4562236b
HW
171 }
172}
173
174static int dm_crtc_get_scanoutpos(struct amdgpu_device *adev, int crtc,
3ee6b26b 175 u32 *vbl, u32 *position)
4562236b 176{
81c50963
ST
177 uint32_t v_blank_start, v_blank_end, h_position, v_position;
178
4562236b
HW
179 if ((crtc < 0) || (crtc >= adev->mode_info.num_crtc))
180 return -EINVAL;
181 else {
182 struct amdgpu_crtc *acrtc = adev->mode_info.crtcs[crtc];
da5c47f6
AG
183 struct dm_crtc_state *acrtc_state = to_dm_crtc_state(
184 acrtc->base.state);
4562236b 185
da5c47f6 186 if (acrtc_state->stream == NULL) {
0971c40e
HW
187 DRM_ERROR("dc_stream_state is NULL for crtc '%d'!\n",
188 crtc);
4562236b
HW
189 return 0;
190 }
191
81c50963
ST
192 /*
193 * TODO rework base driver to use values directly.
194 * for now parse it back into reg-format
195 */
da5c47f6 196 dc_stream_get_scanoutpos(acrtc_state->stream,
81c50963
ST
197 &v_blank_start,
198 &v_blank_end,
199 &h_position,
200 &v_position);
201
e806208d
AG
202 *position = v_position | (h_position << 16);
203 *vbl = v_blank_start | (v_blank_end << 16);
4562236b
HW
204 }
205
206 return 0;
207}
208
209static bool dm_is_idle(void *handle)
210{
211 /* XXX todo */
212 return true;
213}
214
215static int dm_wait_for_idle(void *handle)
216{
217 /* XXX todo */
218 return 0;
219}
220
221static bool dm_check_soft_reset(void *handle)
222{
223 return false;
224}
225
226static int dm_soft_reset(void *handle)
227{
228 /* XXX todo */
229 return 0;
230}
231
3ee6b26b
AD
232static struct amdgpu_crtc *
233get_crtc_by_otg_inst(struct amdgpu_device *adev,
234 int otg_inst)
4562236b
HW
235{
236 struct drm_device *dev = adev->ddev;
237 struct drm_crtc *crtc;
238 struct amdgpu_crtc *amdgpu_crtc;
239
4562236b
HW
240 if (otg_inst == -1) {
241 WARN_ON(1);
242 return adev->mode_info.crtcs[0];
243 }
244
245 list_for_each_entry(crtc, &dev->mode_config.crtc_list, head) {
246 amdgpu_crtc = to_amdgpu_crtc(crtc);
247
248 if (amdgpu_crtc->otg_inst == otg_inst)
249 return amdgpu_crtc;
250 }
251
252 return NULL;
253}
254
66b0c973
MK
255static inline bool amdgpu_dm_vrr_active(struct dm_crtc_state *dm_state)
256{
257 return dm_state->freesync_config.state == VRR_STATE_ACTIVE_VARIABLE ||
258 dm_state->freesync_config.state == VRR_STATE_ACTIVE_FIXED;
259}
260
4562236b
HW
261static void dm_pflip_high_irq(void *interrupt_params)
262{
4562236b
HW
263 struct amdgpu_crtc *amdgpu_crtc;
264 struct common_irq_params *irq_params = interrupt_params;
265 struct amdgpu_device *adev = irq_params->adev;
266 unsigned long flags;
71bbe51a
MK
267 struct drm_pending_vblank_event *e;
268 struct dm_crtc_state *acrtc_state;
269 uint32_t vpos, hpos, v_blank_start, v_blank_end;
270 bool vrr_active;
4562236b
HW
271
272 amdgpu_crtc = get_crtc_by_otg_inst(adev, irq_params->irq_src - IRQ_TYPE_PFLIP);
273
274 /* IRQ could occur when in initial stage */
1f6010a9 275 /* TODO work and BO cleanup */
4562236b
HW
276 if (amdgpu_crtc == NULL) {
277 DRM_DEBUG_DRIVER("CRTC is null, returning.\n");
278 return;
279 }
280
281 spin_lock_irqsave(&adev->ddev->event_lock, flags);
4562236b
HW
282
283 if (amdgpu_crtc->pflip_status != AMDGPU_FLIP_SUBMITTED){
284 DRM_DEBUG_DRIVER("amdgpu_crtc->pflip_status = %d !=AMDGPU_FLIP_SUBMITTED(%d) on crtc:%d[%p] \n",
285 amdgpu_crtc->pflip_status,
286 AMDGPU_FLIP_SUBMITTED,
287 amdgpu_crtc->crtc_id,
288 amdgpu_crtc);
289 spin_unlock_irqrestore(&adev->ddev->event_lock, flags);
290 return;
291 }
292
71bbe51a
MK
293 /* page flip completed. */
294 e = amdgpu_crtc->event;
295 amdgpu_crtc->event = NULL;
4562236b 296
71bbe51a
MK
297 if (!e)
298 WARN_ON(1);
1159898a 299
71bbe51a
MK
300 acrtc_state = to_dm_crtc_state(amdgpu_crtc->base.state);
301 vrr_active = amdgpu_dm_vrr_active(acrtc_state);
302
303 /* Fixed refresh rate, or VRR scanout position outside front-porch? */
304 if (!vrr_active ||
305 !dc_stream_get_scanoutpos(acrtc_state->stream, &v_blank_start,
306 &v_blank_end, &hpos, &vpos) ||
307 (vpos < v_blank_start)) {
308 /* Update to correct count and vblank timestamp if racing with
309 * vblank irq. This also updates to the correct vblank timestamp
310 * even in VRR mode, as scanout is past the front-porch atm.
311 */
312 drm_crtc_accurate_vblank_count(&amdgpu_crtc->base);
1159898a 313
71bbe51a
MK
314 /* Wake up userspace by sending the pageflip event with proper
315 * count and timestamp of vblank of flip completion.
316 */
317 if (e) {
318 drm_crtc_send_vblank_event(&amdgpu_crtc->base, e);
319
320 /* Event sent, so done with vblank for this flip */
321 drm_crtc_vblank_put(&amdgpu_crtc->base);
322 }
323 } else if (e) {
324 /* VRR active and inside front-porch: vblank count and
325 * timestamp for pageflip event will only be up to date after
326 * drm_crtc_handle_vblank() has been executed from late vblank
327 * irq handler after start of back-porch (vline 0). We queue the
328 * pageflip event for send-out by drm_crtc_handle_vblank() with
329 * updated timestamp and count, once it runs after us.
330 *
331 * We need to open-code this instead of using the helper
332 * drm_crtc_arm_vblank_event(), as that helper would
333 * call drm_crtc_accurate_vblank_count(), which we must
334 * not call in VRR mode while we are in front-porch!
335 */
336
337 /* sequence will be replaced by real count during send-out. */
338 e->sequence = drm_crtc_vblank_count(&amdgpu_crtc->base);
339 e->pipe = amdgpu_crtc->crtc_id;
340
341 list_add_tail(&e->base.link, &adev->ddev->vblank_event_list);
342 e = NULL;
343 }
4562236b 344
fdd1fe57
MK
345 /* Keep track of vblank of this flip for flip throttling. We use the
346 * cooked hw counter, as that one incremented at start of this vblank
347 * of pageflip completion, so last_flip_vblank is the forbidden count
348 * for queueing new pageflips if vsync + VRR is enabled.
349 */
350 amdgpu_crtc->last_flip_vblank = amdgpu_get_vblank_counter_kms(adev->ddev,
351 amdgpu_crtc->crtc_id);
352
54f5499a 353 amdgpu_crtc->pflip_status = AMDGPU_FLIP_NONE;
4562236b
HW
354 spin_unlock_irqrestore(&adev->ddev->event_lock, flags);
355
71bbe51a
MK
356 DRM_DEBUG_DRIVER("crtc:%d[%p], pflip_stat:AMDGPU_FLIP_NONE, vrr[%d]-fp %d\n",
357 amdgpu_crtc->crtc_id, amdgpu_crtc,
358 vrr_active, (int) !e);
4562236b
HW
359}
360
d2574c33
MK
361static void dm_vupdate_high_irq(void *interrupt_params)
362{
363 struct common_irq_params *irq_params = interrupt_params;
364 struct amdgpu_device *adev = irq_params->adev;
365 struct amdgpu_crtc *acrtc;
366 struct dm_crtc_state *acrtc_state;
367
368 acrtc = get_crtc_by_otg_inst(adev, irq_params->irq_src - IRQ_TYPE_VUPDATE);
369
370 if (acrtc) {
371 acrtc_state = to_dm_crtc_state(acrtc->base.state);
372
373 DRM_DEBUG_DRIVER("crtc:%d, vupdate-vrr:%d\n", acrtc->crtc_id,
374 amdgpu_dm_vrr_active(acrtc_state));
375
376 /* Core vblank handling is done here after end of front-porch in
377 * vrr mode, as vblank timestamping will give valid results
378 * while now done after front-porch. This will also deliver
379 * page-flip completion events that have been queued to us
380 * if a pageflip happened inside front-porch.
381 */
382 if (amdgpu_dm_vrr_active(acrtc_state))
383 drm_crtc_handle_vblank(&acrtc->base);
384 }
385}
386
4562236b
HW
387static void dm_crtc_high_irq(void *interrupt_params)
388{
389 struct common_irq_params *irq_params = interrupt_params;
390 struct amdgpu_device *adev = irq_params->adev;
4562236b 391 struct amdgpu_crtc *acrtc;
180db303 392 struct dm_crtc_state *acrtc_state;
4562236b 393
b57de80a 394 acrtc = get_crtc_by_otg_inst(adev, irq_params->irq_src - IRQ_TYPE_VBLANK);
4562236b 395
e5d0170e 396 if (acrtc) {
180db303
NK
397 acrtc_state = to_dm_crtc_state(acrtc->base.state);
398
d2574c33
MK
399 DRM_DEBUG_DRIVER("crtc:%d, vupdate-vrr:%d\n", acrtc->crtc_id,
400 amdgpu_dm_vrr_active(acrtc_state));
401
402 /* Core vblank handling at start of front-porch is only possible
403 * in non-vrr mode, as only there vblank timestamping will give
404 * valid results while done in front-porch. Otherwise defer it
405 * to dm_vupdate_high_irq after end of front-porch.
406 */
407 if (!amdgpu_dm_vrr_active(acrtc_state))
408 drm_crtc_handle_vblank(&acrtc->base);
409
410 /* Following stuff must happen at start of vblank, for crc
411 * computation and below-the-range btr support in vrr mode.
412 */
413 amdgpu_dm_crtc_handle_crc_irq(&acrtc->base);
414
180db303
NK
415 if (acrtc_state->stream &&
416 acrtc_state->vrr_params.supported &&
417 acrtc_state->freesync_config.state == VRR_STATE_ACTIVE_VARIABLE) {
418 mod_freesync_handle_v_update(
419 adev->dm.freesync_module,
420 acrtc_state->stream,
421 &acrtc_state->vrr_params);
422
423 dc_stream_adjust_vmin_vmax(
424 adev->dm.dc,
425 acrtc_state->stream,
426 &acrtc_state->vrr_params.adjust);
427 }
e5d0170e 428 }
4562236b
HW
429}
430
431static int dm_set_clockgating_state(void *handle,
432 enum amd_clockgating_state state)
433{
434 return 0;
435}
436
437static int dm_set_powergating_state(void *handle,
438 enum amd_powergating_state state)
439{
440 return 0;
441}
442
443/* Prototypes of private functions */
444static int dm_early_init(void* handle);
445
a32e24b4 446/* Allocate memory for FBC compressed data */
3e332d3a 447static void amdgpu_dm_fbc_init(struct drm_connector *connector)
a32e24b4 448{
3e332d3a
RL
449 struct drm_device *dev = connector->dev;
450 struct amdgpu_device *adev = dev->dev_private;
a32e24b4 451 struct dm_comressor_info *compressor = &adev->dm.compressor;
3e332d3a
RL
452 struct amdgpu_dm_connector *aconn = to_amdgpu_dm_connector(connector);
453 struct drm_display_mode *mode;
42e67c3b
RL
454 unsigned long max_size = 0;
455
456 if (adev->dm.dc->fbc_compressor == NULL)
457 return;
a32e24b4 458
3e332d3a 459 if (aconn->dc_link->connector_signal != SIGNAL_TYPE_EDP)
42e67c3b
RL
460 return;
461
3e332d3a
RL
462 if (compressor->bo_ptr)
463 return;
42e67c3b 464
42e67c3b 465
3e332d3a
RL
466 list_for_each_entry(mode, &connector->modes, head) {
467 if (max_size < mode->htotal * mode->vtotal)
468 max_size = mode->htotal * mode->vtotal;
42e67c3b
RL
469 }
470
471 if (max_size) {
472 int r = amdgpu_bo_create_kernel(adev, max_size * 4, PAGE_SIZE,
0e5916ff 473 AMDGPU_GEM_DOMAIN_GTT, &compressor->bo_ptr,
42e67c3b 474 &compressor->gpu_addr, &compressor->cpu_addr);
a32e24b4
RL
475
476 if (r)
42e67c3b
RL
477 DRM_ERROR("DM: Failed to initialize FBC\n");
478 else {
479 adev->dm.dc->ctx->fbc_gpu_addr = compressor->gpu_addr;
480 DRM_INFO("DM: FBC alloc %lu\n", max_size*4);
481 }
482
a32e24b4
RL
483 }
484
485}
a32e24b4 486
7578ecda 487static int amdgpu_dm_init(struct amdgpu_device *adev)
4562236b
HW
488{
489 struct dc_init_data init_data;
490 adev->dm.ddev = adev->ddev;
491 adev->dm.adev = adev;
492
4562236b
HW
493 /* Zero all the fields */
494 memset(&init_data, 0, sizeof(init_data));
495
674e78ac
NK
496 mutex_init(&adev->dm.dc_lock);
497
4562236b
HW
498 if(amdgpu_dm_irq_init(adev)) {
499 DRM_ERROR("amdgpu: failed to initialize DM IRQ support.\n");
500 goto error;
501 }
502
503 init_data.asic_id.chip_family = adev->family;
504
505 init_data.asic_id.pci_revision_id = adev->rev_id;
506 init_data.asic_id.hw_internal_rev = adev->external_rev_id;
507
770d13b1 508 init_data.asic_id.vram_width = adev->gmc.vram_width;
4562236b
HW
509 /* TODO: initialize init_data.asic_id.vram_type here!!!! */
510 init_data.asic_id.atombios_base_address =
511 adev->mode_info.atom_context->bios;
512
513 init_data.driver = adev;
514
515 adev->dm.cgs_device = amdgpu_cgs_create_device(adev);
516
517 if (!adev->dm.cgs_device) {
518 DRM_ERROR("amdgpu: failed to create cgs device.\n");
519 goto error;
520 }
521
522 init_data.cgs_device = adev->dm.cgs_device;
523
4562236b
HW
524 init_data.dce_environment = DCE_ENV_PRODUCTION_DRV;
525
6e227308
HW
526 /*
527 * TODO debug why this doesn't work on Raven
528 */
529 if (adev->flags & AMD_IS_APU &&
530 adev->asic_type >= CHIP_CARRIZO &&
531 adev->asic_type < CHIP_RAVEN)
532 init_data.flags.gpu_vm_support = true;
533
04b94af4
AD
534 if (amdgpu_dc_feature_mask & DC_FBC_MASK)
535 init_data.flags.fbc_support = true;
536
27eaa492
TL
537 init_data.flags.power_down_display_on_boot = true;
538
4562236b
HW
539 /* Display Core create. */
540 adev->dm.dc = dc_create(&init_data);
541
423788c7 542 if (adev->dm.dc) {
76121231 543 DRM_INFO("Display Core initialized with v%s!\n", DC_VER);
423788c7 544 } else {
76121231 545 DRM_INFO("Display Core failed to initialize with v%s!\n", DC_VER);
423788c7
ES
546 goto error;
547 }
4562236b 548
4562236b
HW
549 adev->dm.freesync_module = mod_freesync_create(adev->dm.dc);
550 if (!adev->dm.freesync_module) {
551 DRM_ERROR(
552 "amdgpu: failed to initialize freesync_module.\n");
553 } else
f1ad2f5e 554 DRM_DEBUG_DRIVER("amdgpu: freesync_module init done %p.\n",
4562236b
HW
555 adev->dm.freesync_module);
556
e277adc5
LSL
557 amdgpu_dm_init_color_mod();
558
4562236b
HW
559 if (amdgpu_dm_initialize_drm_device(adev)) {
560 DRM_ERROR(
561 "amdgpu: failed to initialize sw for display support.\n");
562 goto error;
563 }
564
565 /* Update the actual used number of crtc */
566 adev->mode_info.num_crtc = adev->dm.display_indexes_num;
567
568 /* TODO: Add_display_info? */
569
570 /* TODO use dynamic cursor width */
ce75805e
AG
571 adev->ddev->mode_config.cursor_width = adev->dm.dc->caps.max_cursor_size;
572 adev->ddev->mode_config.cursor_height = adev->dm.dc->caps.max_cursor_size;
4562236b
HW
573
574 if (drm_vblank_init(adev->ddev, adev->dm.display_indexes_num)) {
575 DRM_ERROR(
576 "amdgpu: failed to initialize sw for display support.\n");
577 goto error;
578 }
579
e498eb71
NK
580#if defined(CONFIG_DEBUG_FS)
581 if (dtn_debugfs_init(adev))
582 DRM_ERROR("amdgpu: failed initialize dtn debugfs support.\n");
583#endif
584
f1ad2f5e 585 DRM_DEBUG_DRIVER("KMS initialized.\n");
4562236b
HW
586
587 return 0;
588error:
589 amdgpu_dm_fini(adev);
590
59d0f396 591 return -EINVAL;
4562236b
HW
592}
593
7578ecda 594static void amdgpu_dm_fini(struct amdgpu_device *adev)
4562236b
HW
595{
596 amdgpu_dm_destroy_drm_device(&adev->dm);
597 /*
598 * TODO: pageflip, vlank interrupt
599 *
600 * amdgpu_dm_irq_fini(adev);
601 */
602
603 if (adev->dm.cgs_device) {
604 amdgpu_cgs_destroy_device(adev->dm.cgs_device);
605 adev->dm.cgs_device = NULL;
606 }
607 if (adev->dm.freesync_module) {
608 mod_freesync_destroy(adev->dm.freesync_module);
609 adev->dm.freesync_module = NULL;
610 }
611 /* DC Destroy TODO: Replace destroy DAL */
21de3396 612 if (adev->dm.dc)
4562236b 613 dc_destroy(&adev->dm.dc);
674e78ac
NK
614
615 mutex_destroy(&adev->dm.dc_lock);
616
4562236b
HW
617 return;
618}
619
a94d5569 620static int load_dmcu_fw(struct amdgpu_device *adev)
4562236b 621{
a94d5569
DF
622 const char *fw_name_dmcu;
623 int r;
624 const struct dmcu_firmware_header_v1_0 *hdr;
625
626 switch(adev->asic_type) {
627 case CHIP_BONAIRE:
628 case CHIP_HAWAII:
629 case CHIP_KAVERI:
630 case CHIP_KABINI:
631 case CHIP_MULLINS:
632 case CHIP_TONGA:
633 case CHIP_FIJI:
634 case CHIP_CARRIZO:
635 case CHIP_STONEY:
636 case CHIP_POLARIS11:
637 case CHIP_POLARIS10:
638 case CHIP_POLARIS12:
639 case CHIP_VEGAM:
640 case CHIP_VEGA10:
641 case CHIP_VEGA12:
642 case CHIP_VEGA20:
643 return 0;
644 case CHIP_RAVEN:
645 fw_name_dmcu = FIRMWARE_RAVEN_DMCU;
646 break;
647 default:
648 DRM_ERROR("Unsupported ASIC type: 0x%X\n", adev->asic_type);
59d0f396 649 return -EINVAL;
a94d5569
DF
650 }
651
652 if (adev->firmware.load_type != AMDGPU_FW_LOAD_PSP) {
653 DRM_DEBUG_KMS("dm: DMCU firmware not supported on direct or SMU loading\n");
654 return 0;
655 }
656
657 r = request_firmware_direct(&adev->dm.fw_dmcu, fw_name_dmcu, adev->dev);
658 if (r == -ENOENT) {
659 /* DMCU firmware is not necessary, so don't raise a fuss if it's missing */
660 DRM_DEBUG_KMS("dm: DMCU firmware not found\n");
661 adev->dm.fw_dmcu = NULL;
662 return 0;
663 }
664 if (r) {
665 dev_err(adev->dev, "amdgpu_dm: Can't load firmware \"%s\"\n",
666 fw_name_dmcu);
667 return r;
668 }
669
670 r = amdgpu_ucode_validate(adev->dm.fw_dmcu);
671 if (r) {
672 dev_err(adev->dev, "amdgpu_dm: Can't validate firmware \"%s\"\n",
673 fw_name_dmcu);
674 release_firmware(adev->dm.fw_dmcu);
675 adev->dm.fw_dmcu = NULL;
676 return r;
677 }
678
679 hdr = (const struct dmcu_firmware_header_v1_0 *)adev->dm.fw_dmcu->data;
680 adev->firmware.ucode[AMDGPU_UCODE_ID_DMCU_ERAM].ucode_id = AMDGPU_UCODE_ID_DMCU_ERAM;
681 adev->firmware.ucode[AMDGPU_UCODE_ID_DMCU_ERAM].fw = adev->dm.fw_dmcu;
682 adev->firmware.fw_size +=
683 ALIGN(le32_to_cpu(hdr->header.ucode_size_bytes) - le32_to_cpu(hdr->intv_size_bytes), PAGE_SIZE);
684
685 adev->firmware.ucode[AMDGPU_UCODE_ID_DMCU_INTV].ucode_id = AMDGPU_UCODE_ID_DMCU_INTV;
686 adev->firmware.ucode[AMDGPU_UCODE_ID_DMCU_INTV].fw = adev->dm.fw_dmcu;
687 adev->firmware.fw_size +=
688 ALIGN(le32_to_cpu(hdr->intv_size_bytes), PAGE_SIZE);
689
ee6e89c0
DF
690 adev->dm.dmcu_fw_version = le32_to_cpu(hdr->header.ucode_version);
691
a94d5569
DF
692 DRM_DEBUG_KMS("PSP loading DMCU firmware\n");
693
4562236b
HW
694 return 0;
695}
696
a94d5569
DF
697static int dm_sw_init(void *handle)
698{
699 struct amdgpu_device *adev = (struct amdgpu_device *)handle;
700
701 return load_dmcu_fw(adev);
702}
703
4562236b
HW
704static int dm_sw_fini(void *handle)
705{
a94d5569
DF
706 struct amdgpu_device *adev = (struct amdgpu_device *)handle;
707
708 if(adev->dm.fw_dmcu) {
709 release_firmware(adev->dm.fw_dmcu);
710 adev->dm.fw_dmcu = NULL;
711 }
712
4562236b
HW
713 return 0;
714}
715
7abcf6b5 716static int detect_mst_link_for_all_connectors(struct drm_device *dev)
4562236b 717{
c84dec2f 718 struct amdgpu_dm_connector *aconnector;
4562236b 719 struct drm_connector *connector;
7abcf6b5 720 int ret = 0;
4562236b
HW
721
722 drm_modeset_lock(&dev->mode_config.connection_mutex, NULL);
723
724 list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
b349f76e 725 aconnector = to_amdgpu_dm_connector(connector);
30ec2b97
JFZ
726 if (aconnector->dc_link->type == dc_connection_mst_branch &&
727 aconnector->mst_mgr.aux) {
f1ad2f5e 728 DRM_DEBUG_DRIVER("DM_MST: starting TM on aconnector: %p [id: %d]\n",
7abcf6b5
AG
729 aconnector, aconnector->base.base.id);
730
731 ret = drm_dp_mst_topology_mgr_set_mst(&aconnector->mst_mgr, true);
732 if (ret < 0) {
733 DRM_ERROR("DM_MST: Failed to start MST\n");
734 ((struct dc_link *)aconnector->dc_link)->type = dc_connection_single;
735 return ret;
4562236b 736 }
7abcf6b5 737 }
4562236b
HW
738 }
739
740 drm_modeset_unlock(&dev->mode_config.connection_mutex);
7abcf6b5
AG
741 return ret;
742}
743
744static int dm_late_init(void *handle)
745{
42e67c3b 746 struct amdgpu_device *adev = (struct amdgpu_device *)handle;
7abcf6b5 747
bbf854dc
DF
748 struct dmcu_iram_parameters params;
749 unsigned int linear_lut[16];
750 int i;
751 struct dmcu *dmcu = adev->dm.dc->res_pool->dmcu;
752 bool ret;
753
754 for (i = 0; i < 16; i++)
755 linear_lut[i] = 0xFFFF * i / 15;
756
757 params.set = 0;
758 params.backlight_ramping_start = 0xCCCC;
759 params.backlight_ramping_reduction = 0xCCCCCCCC;
760 params.backlight_lut_array_size = 16;
761 params.backlight_lut_array = linear_lut;
762
763 ret = dmcu_load_iram(dmcu, params);
764
765 if (!ret)
766 return -EINVAL;
767
42e67c3b 768 return detect_mst_link_for_all_connectors(adev->ddev);
4562236b
HW
769}
770
771static void s3_handle_mst(struct drm_device *dev, bool suspend)
772{
c84dec2f 773 struct amdgpu_dm_connector *aconnector;
4562236b 774 struct drm_connector *connector;
fe7553be
LP
775 struct drm_dp_mst_topology_mgr *mgr;
776 int ret;
777 bool need_hotplug = false;
4562236b
HW
778
779 drm_modeset_lock(&dev->mode_config.connection_mutex, NULL);
780
fe7553be
LP
781 list_for_each_entry(connector, &dev->mode_config.connector_list,
782 head) {
783 aconnector = to_amdgpu_dm_connector(connector);
784 if (aconnector->dc_link->type != dc_connection_mst_branch ||
785 aconnector->mst_port)
786 continue;
787
788 mgr = &aconnector->mst_mgr;
789
790 if (suspend) {
791 drm_dp_mst_topology_mgr_suspend(mgr);
792 } else {
793 ret = drm_dp_mst_topology_mgr_resume(mgr);
794 if (ret < 0) {
795 drm_dp_mst_topology_mgr_set_mst(mgr, false);
796 need_hotplug = true;
797 }
798 }
4562236b
HW
799 }
800
801 drm_modeset_unlock(&dev->mode_config.connection_mutex);
fe7553be
LP
802
803 if (need_hotplug)
804 drm_kms_helper_hotplug_event(dev);
4562236b
HW
805}
806
b8592b48
LL
807/**
808 * dm_hw_init() - Initialize DC device
809 * @handle: The base driver device containing the amdpgu_dm device.
810 *
811 * Initialize the &struct amdgpu_display_manager device. This involves calling
812 * the initializers of each DM component, then populating the struct with them.
813 *
814 * Although the function implies hardware initialization, both hardware and
815 * software are initialized here. Splitting them out to their relevant init
816 * hooks is a future TODO item.
817 *
818 * Some notable things that are initialized here:
819 *
820 * - Display Core, both software and hardware
821 * - DC modules that we need (freesync and color management)
822 * - DRM software states
823 * - Interrupt sources and handlers
824 * - Vblank support
825 * - Debug FS entries, if enabled
826 */
4562236b
HW
827static int dm_hw_init(void *handle)
828{
829 struct amdgpu_device *adev = (struct amdgpu_device *)handle;
830 /* Create DAL display manager */
831 amdgpu_dm_init(adev);
4562236b
HW
832 amdgpu_dm_hpd_init(adev);
833
4562236b
HW
834 return 0;
835}
836
b8592b48
LL
837/**
838 * dm_hw_fini() - Teardown DC device
839 * @handle: The base driver device containing the amdpgu_dm device.
840 *
841 * Teardown components within &struct amdgpu_display_manager that require
842 * cleanup. This involves cleaning up the DRM device, DC, and any modules that
843 * were loaded. Also flush IRQ workqueues and disable them.
844 */
4562236b
HW
845static int dm_hw_fini(void *handle)
846{
847 struct amdgpu_device *adev = (struct amdgpu_device *)handle;
848
849 amdgpu_dm_hpd_fini(adev);
850
851 amdgpu_dm_irq_fini(adev);
21de3396 852 amdgpu_dm_fini(adev);
4562236b
HW
853 return 0;
854}
855
856static int dm_suspend(void *handle)
857{
858 struct amdgpu_device *adev = handle;
859 struct amdgpu_display_manager *dm = &adev->dm;
860 int ret = 0;
4562236b 861
d2f0b53b
LHM
862 WARN_ON(adev->dm.cached_state);
863 adev->dm.cached_state = drm_atomic_helper_suspend(adev->ddev);
864
4562236b
HW
865 s3_handle_mst(adev->ddev, true);
866
4562236b
HW
867 amdgpu_dm_irq_suspend(adev);
868
a3621485 869
32f5062d 870 dc_set_power_state(dm->dc, DC_ACPI_CM_POWER_STATE_D3);
4562236b
HW
871
872 return ret;
873}
874
1daf8c63
AD
875static struct amdgpu_dm_connector *
876amdgpu_dm_find_first_crtc_matching_connector(struct drm_atomic_state *state,
877 struct drm_crtc *crtc)
4562236b
HW
878{
879 uint32_t i;
c2cea706 880 struct drm_connector_state *new_con_state;
4562236b
HW
881 struct drm_connector *connector;
882 struct drm_crtc *crtc_from_state;
883
c2cea706
LSL
884 for_each_new_connector_in_state(state, connector, new_con_state, i) {
885 crtc_from_state = new_con_state->crtc;
4562236b
HW
886
887 if (crtc_from_state == crtc)
c84dec2f 888 return to_amdgpu_dm_connector(connector);
4562236b
HW
889 }
890
891 return NULL;
892}
893
fbbdadf2
BL
894static void emulated_link_detect(struct dc_link *link)
895{
896 struct dc_sink_init_data sink_init_data = { 0 };
897 struct display_sink_capability sink_caps = { 0 };
898 enum dc_edid_status edid_status;
899 struct dc_context *dc_ctx = link->ctx;
900 struct dc_sink *sink = NULL;
901 struct dc_sink *prev_sink = NULL;
902
903 link->type = dc_connection_none;
904 prev_sink = link->local_sink;
905
906 if (prev_sink != NULL)
907 dc_sink_retain(prev_sink);
908
909 switch (link->connector_signal) {
910 case SIGNAL_TYPE_HDMI_TYPE_A: {
911 sink_caps.transaction_type = DDC_TRANSACTION_TYPE_I2C;
912 sink_caps.signal = SIGNAL_TYPE_HDMI_TYPE_A;
913 break;
914 }
915
916 case SIGNAL_TYPE_DVI_SINGLE_LINK: {
917 sink_caps.transaction_type = DDC_TRANSACTION_TYPE_I2C;
918 sink_caps.signal = SIGNAL_TYPE_DVI_SINGLE_LINK;
919 break;
920 }
921
922 case SIGNAL_TYPE_DVI_DUAL_LINK: {
923 sink_caps.transaction_type = DDC_TRANSACTION_TYPE_I2C;
924 sink_caps.signal = SIGNAL_TYPE_DVI_DUAL_LINK;
925 break;
926 }
927
928 case SIGNAL_TYPE_LVDS: {
929 sink_caps.transaction_type = DDC_TRANSACTION_TYPE_I2C;
930 sink_caps.signal = SIGNAL_TYPE_LVDS;
931 break;
932 }
933
934 case SIGNAL_TYPE_EDP: {
935 sink_caps.transaction_type =
936 DDC_TRANSACTION_TYPE_I2C_OVER_AUX;
937 sink_caps.signal = SIGNAL_TYPE_EDP;
938 break;
939 }
940
941 case SIGNAL_TYPE_DISPLAY_PORT: {
942 sink_caps.transaction_type =
943 DDC_TRANSACTION_TYPE_I2C_OVER_AUX;
944 sink_caps.signal = SIGNAL_TYPE_VIRTUAL;
945 break;
946 }
947
948 default:
949 DC_ERROR("Invalid connector type! signal:%d\n",
950 link->connector_signal);
951 return;
952 }
953
954 sink_init_data.link = link;
955 sink_init_data.sink_signal = sink_caps.signal;
956
957 sink = dc_sink_create(&sink_init_data);
958 if (!sink) {
959 DC_ERROR("Failed to create sink!\n");
960 return;
961 }
962
dcd5fb82 963 /* dc_sink_create returns a new reference */
fbbdadf2
BL
964 link->local_sink = sink;
965
966 edid_status = dm_helpers_read_local_edid(
967 link->ctx,
968 link,
969 sink);
970
971 if (edid_status != EDID_OK)
972 DC_ERROR("Failed to read EDID");
973
974}
975
4562236b
HW
976static int dm_resume(void *handle)
977{
978 struct amdgpu_device *adev = handle;
4562236b
HW
979 struct drm_device *ddev = adev->ddev;
980 struct amdgpu_display_manager *dm = &adev->dm;
c84dec2f 981 struct amdgpu_dm_connector *aconnector;
4562236b 982 struct drm_connector *connector;
4562236b 983 struct drm_crtc *crtc;
c2cea706 984 struct drm_crtc_state *new_crtc_state;
fcb4019e
LSL
985 struct dm_crtc_state *dm_new_crtc_state;
986 struct drm_plane *plane;
987 struct drm_plane_state *new_plane_state;
988 struct dm_plane_state *dm_new_plane_state;
113b7a01 989 struct dm_atomic_state *dm_state = to_dm_atomic_state(dm->atomic_obj.state);
fbbdadf2 990 enum dc_connection_type new_connection_type = dc_connection_none;
a3621485 991 int i;
4562236b 992
113b7a01
LL
993 /* Recreate dc_state - DC invalidates it when setting power state to S3. */
994 dc_release_state(dm_state->context);
995 dm_state->context = dc_create_state(dm->dc);
996 /* TODO: Remove dc_state->dccg, use dc->dccg directly. */
997 dc_resource_state_construct(dm->dc, dm_state->context);
998
a80aa93d
ML
999 /* power on hardware */
1000 dc_set_power_state(dm->dc, DC_ACPI_CM_POWER_STATE_D0);
1001
4562236b
HW
1002 /* program HPD filter */
1003 dc_resume(dm->dc);
1004
1005 /* On resume we need to rewrite the MSTM control bits to enamble MST*/
1006 s3_handle_mst(ddev, false);
1007
1008 /*
1009 * early enable HPD Rx IRQ, should be done before set mode as short
1010 * pulse interrupts are used for MST
1011 */
1012 amdgpu_dm_irq_resume_early(adev);
1013
4562236b 1014 /* Do detection*/
a80aa93d 1015 list_for_each_entry(connector, &ddev->mode_config.connector_list, head) {
c84dec2f 1016 aconnector = to_amdgpu_dm_connector(connector);
4562236b
HW
1017
1018 /*
1019 * this is the case when traversing through already created
1020 * MST connectors, should be skipped
1021 */
1022 if (aconnector->mst_port)
1023 continue;
1024
03ea364c 1025 mutex_lock(&aconnector->hpd_lock);
fbbdadf2
BL
1026 if (!dc_link_detect_sink(aconnector->dc_link, &new_connection_type))
1027 DRM_ERROR("KMS: Failed to detect connector\n");
1028
1029 if (aconnector->base.force && new_connection_type == dc_connection_none)
1030 emulated_link_detect(aconnector->dc_link);
1031 else
1032 dc_link_detect(aconnector->dc_link, DETECT_REASON_HPD);
3eb4eba4
RL
1033
1034 if (aconnector->fake_enable && aconnector->dc_link->local_sink)
1035 aconnector->fake_enable = false;
1036
dcd5fb82
MF
1037 if (aconnector->dc_sink)
1038 dc_sink_release(aconnector->dc_sink);
4562236b
HW
1039 aconnector->dc_sink = NULL;
1040 amdgpu_dm_update_connector_after_detect(aconnector);
03ea364c 1041 mutex_unlock(&aconnector->hpd_lock);
4562236b
HW
1042 }
1043
1f6010a9 1044 /* Force mode set in atomic commit */
a80aa93d 1045 for_each_new_crtc_in_state(dm->cached_state, crtc, new_crtc_state, i)
c2cea706 1046 new_crtc_state->active_changed = true;
4f346e65 1047
fcb4019e
LSL
1048 /*
1049 * atomic_check is expected to create the dc states. We need to release
1050 * them here, since they were duplicated as part of the suspend
1051 * procedure.
1052 */
a80aa93d 1053 for_each_new_crtc_in_state(dm->cached_state, crtc, new_crtc_state, i) {
fcb4019e
LSL
1054 dm_new_crtc_state = to_dm_crtc_state(new_crtc_state);
1055 if (dm_new_crtc_state->stream) {
1056 WARN_ON(kref_read(&dm_new_crtc_state->stream->refcount) > 1);
1057 dc_stream_release(dm_new_crtc_state->stream);
1058 dm_new_crtc_state->stream = NULL;
1059 }
1060 }
1061
a80aa93d 1062 for_each_new_plane_in_state(dm->cached_state, plane, new_plane_state, i) {
fcb4019e
LSL
1063 dm_new_plane_state = to_dm_plane_state(new_plane_state);
1064 if (dm_new_plane_state->dc_state) {
1065 WARN_ON(kref_read(&dm_new_plane_state->dc_state->refcount) > 1);
1066 dc_plane_state_release(dm_new_plane_state->dc_state);
1067 dm_new_plane_state->dc_state = NULL;
1068 }
1069 }
1070
2d1af6a1 1071 drm_atomic_helper_resume(ddev, dm->cached_state);
4562236b 1072
a80aa93d 1073 dm->cached_state = NULL;
0a214e2f 1074
9faa4237 1075 amdgpu_dm_irq_resume_late(adev);
4562236b 1076
2d1af6a1 1077 return 0;
4562236b
HW
1078}
1079
b8592b48
LL
1080/**
1081 * DOC: DM Lifecycle
1082 *
1083 * DM (and consequently DC) is registered in the amdgpu base driver as a IP
1084 * block. When CONFIG_DRM_AMD_DC is enabled, the DM device IP block is added to
1085 * the base driver's device list to be initialized and torn down accordingly.
1086 *
1087 * The functions to do so are provided as hooks in &struct amd_ip_funcs.
1088 */
1089
4562236b
HW
1090static const struct amd_ip_funcs amdgpu_dm_funcs = {
1091 .name = "dm",
1092 .early_init = dm_early_init,
7abcf6b5 1093 .late_init = dm_late_init,
4562236b
HW
1094 .sw_init = dm_sw_init,
1095 .sw_fini = dm_sw_fini,
1096 .hw_init = dm_hw_init,
1097 .hw_fini = dm_hw_fini,
1098 .suspend = dm_suspend,
1099 .resume = dm_resume,
1100 .is_idle = dm_is_idle,
1101 .wait_for_idle = dm_wait_for_idle,
1102 .check_soft_reset = dm_check_soft_reset,
1103 .soft_reset = dm_soft_reset,
1104 .set_clockgating_state = dm_set_clockgating_state,
1105 .set_powergating_state = dm_set_powergating_state,
1106};
1107
1108const struct amdgpu_ip_block_version dm_ip_block =
1109{
1110 .type = AMD_IP_BLOCK_TYPE_DCE,
1111 .major = 1,
1112 .minor = 0,
1113 .rev = 0,
1114 .funcs = &amdgpu_dm_funcs,
1115};
1116
ca3268c4 1117
b8592b48
LL
1118/**
1119 * DOC: atomic
1120 *
1121 * *WIP*
1122 */
0a323b84 1123
b3663f70 1124static const struct drm_mode_config_funcs amdgpu_dm_mode_funcs = {
4d4772f6 1125 .fb_create = amdgpu_display_user_framebuffer_create,
366c1baa 1126 .output_poll_changed = drm_fb_helper_output_poll_changed,
4562236b 1127 .atomic_check = amdgpu_dm_atomic_check,
da5c47f6 1128 .atomic_commit = amdgpu_dm_atomic_commit,
54f5499a
AG
1129};
1130
1131static struct drm_mode_config_helper_funcs amdgpu_dm_mode_config_helperfuncs = {
1132 .atomic_commit_tail = amdgpu_dm_atomic_commit_tail
4562236b
HW
1133};
1134
7578ecda 1135static void
3ee6b26b 1136amdgpu_dm_update_connector_after_detect(struct amdgpu_dm_connector *aconnector)
4562236b
HW
1137{
1138 struct drm_connector *connector = &aconnector->base;
1139 struct drm_device *dev = connector->dev;
b73a22d3 1140 struct dc_sink *sink;
4562236b
HW
1141
1142 /* MST handled by drm_mst framework */
1143 if (aconnector->mst_mgr.mst_state == true)
1144 return;
1145
1146
1147 sink = aconnector->dc_link->local_sink;
dcd5fb82
MF
1148 if (sink)
1149 dc_sink_retain(sink);
4562236b 1150
1f6010a9
DF
1151 /*
1152 * Edid mgmt connector gets first update only in mode_valid hook and then
4562236b 1153 * the connector sink is set to either fake or physical sink depends on link status.
1f6010a9 1154 * Skip if already done during boot.
4562236b
HW
1155 */
1156 if (aconnector->base.force != DRM_FORCE_UNSPECIFIED
1157 && aconnector->dc_em_sink) {
1158
1f6010a9
DF
1159 /*
1160 * For S3 resume with headless use eml_sink to fake stream
1161 * because on resume connector->sink is set to NULL
4562236b
HW
1162 */
1163 mutex_lock(&dev->mode_config.mutex);
1164
1165 if (sink) {
922aa1e1 1166 if (aconnector->dc_sink) {
98e6436d 1167 amdgpu_dm_update_freesync_caps(connector, NULL);
1f6010a9
DF
1168 /*
1169 * retain and release below are used to
1170 * bump up refcount for sink because the link doesn't point
1171 * to it anymore after disconnect, so on next crtc to connector
922aa1e1
AG
1172 * reshuffle by UMD we will get into unwanted dc_sink release
1173 */
dcd5fb82 1174 dc_sink_release(aconnector->dc_sink);
922aa1e1 1175 }
4562236b 1176 aconnector->dc_sink = sink;
dcd5fb82 1177 dc_sink_retain(aconnector->dc_sink);
98e6436d
AK
1178 amdgpu_dm_update_freesync_caps(connector,
1179 aconnector->edid);
4562236b 1180 } else {
98e6436d 1181 amdgpu_dm_update_freesync_caps(connector, NULL);
dcd5fb82 1182 if (!aconnector->dc_sink) {
4562236b 1183 aconnector->dc_sink = aconnector->dc_em_sink;
922aa1e1 1184 dc_sink_retain(aconnector->dc_sink);
dcd5fb82 1185 }
4562236b
HW
1186 }
1187
1188 mutex_unlock(&dev->mode_config.mutex);
dcd5fb82
MF
1189
1190 if (sink)
1191 dc_sink_release(sink);
4562236b
HW
1192 return;
1193 }
1194
1195 /*
1196 * TODO: temporary guard to look for proper fix
1197 * if this sink is MST sink, we should not do anything
1198 */
dcd5fb82
MF
1199 if (sink && sink->sink_signal == SIGNAL_TYPE_DISPLAY_PORT_MST) {
1200 dc_sink_release(sink);
4562236b 1201 return;
dcd5fb82 1202 }
4562236b
HW
1203
1204 if (aconnector->dc_sink == sink) {
1f6010a9
DF
1205 /*
1206 * We got a DP short pulse (Link Loss, DP CTS, etc...).
1207 * Do nothing!!
1208 */
f1ad2f5e 1209 DRM_DEBUG_DRIVER("DCHPD: connector_id=%d: dc_sink didn't change.\n",
4562236b 1210 aconnector->connector_id);
dcd5fb82
MF
1211 if (sink)
1212 dc_sink_release(sink);
4562236b
HW
1213 return;
1214 }
1215
f1ad2f5e 1216 DRM_DEBUG_DRIVER("DCHPD: connector_id=%d: Old sink=%p New sink=%p\n",
4562236b
HW
1217 aconnector->connector_id, aconnector->dc_sink, sink);
1218
1219 mutex_lock(&dev->mode_config.mutex);
1220
1f6010a9
DF
1221 /*
1222 * 1. Update status of the drm connector
1223 * 2. Send an event and let userspace tell us what to do
1224 */
4562236b 1225 if (sink) {
1f6010a9
DF
1226 /*
1227 * TODO: check if we still need the S3 mode update workaround.
1228 * If yes, put it here.
1229 */
4562236b 1230 if (aconnector->dc_sink)
98e6436d 1231 amdgpu_dm_update_freesync_caps(connector, NULL);
4562236b
HW
1232
1233 aconnector->dc_sink = sink;
dcd5fb82 1234 dc_sink_retain(aconnector->dc_sink);
900b3cb1 1235 if (sink->dc_edid.length == 0) {
4562236b 1236 aconnector->edid = NULL;
e86e8947 1237 drm_dp_cec_unset_edid(&aconnector->dm_dp_aux.aux);
900b3cb1 1238 } else {
4562236b
HW
1239 aconnector->edid =
1240 (struct edid *) sink->dc_edid.raw_edid;
1241
1242
c555f023 1243 drm_connector_update_edid_property(connector,
4562236b 1244 aconnector->edid);
e86e8947
HV
1245 drm_dp_cec_set_edid(&aconnector->dm_dp_aux.aux,
1246 aconnector->edid);
4562236b 1247 }
98e6436d 1248 amdgpu_dm_update_freesync_caps(connector, aconnector->edid);
4562236b
HW
1249
1250 } else {
e86e8947 1251 drm_dp_cec_unset_edid(&aconnector->dm_dp_aux.aux);
98e6436d 1252 amdgpu_dm_update_freesync_caps(connector, NULL);
c555f023 1253 drm_connector_update_edid_property(connector, NULL);
4562236b 1254 aconnector->num_modes = 0;
dcd5fb82 1255 dc_sink_release(aconnector->dc_sink);
4562236b 1256 aconnector->dc_sink = NULL;
5326c452 1257 aconnector->edid = NULL;
4562236b
HW
1258 }
1259
1260 mutex_unlock(&dev->mode_config.mutex);
dcd5fb82
MF
1261
1262 if (sink)
1263 dc_sink_release(sink);
4562236b
HW
1264}
1265
1266static void handle_hpd_irq(void *param)
1267{
c84dec2f 1268 struct amdgpu_dm_connector *aconnector = (struct amdgpu_dm_connector *)param;
4562236b
HW
1269 struct drm_connector *connector = &aconnector->base;
1270 struct drm_device *dev = connector->dev;
fbbdadf2 1271 enum dc_connection_type new_connection_type = dc_connection_none;
4562236b 1272
1f6010a9
DF
1273 /*
1274 * In case of failure or MST no need to update connector status or notify the OS
1275 * since (for MST case) MST does this in its own context.
4562236b
HW
1276 */
1277 mutex_lock(&aconnector->hpd_lock);
2e0ac3d6
HW
1278
1279 if (aconnector->fake_enable)
1280 aconnector->fake_enable = false;
1281
fbbdadf2
BL
1282 if (!dc_link_detect_sink(aconnector->dc_link, &new_connection_type))
1283 DRM_ERROR("KMS: Failed to detect connector\n");
1284
1285 if (aconnector->base.force && new_connection_type == dc_connection_none) {
1286 emulated_link_detect(aconnector->dc_link);
1287
1288
1289 drm_modeset_lock_all(dev);
1290 dm_restore_drm_connector_state(dev, connector);
1291 drm_modeset_unlock_all(dev);
1292
1293 if (aconnector->base.force == DRM_FORCE_UNSPECIFIED)
1294 drm_kms_helper_hotplug_event(dev);
1295
1296 } else if (dc_link_detect(aconnector->dc_link, DETECT_REASON_HPD)) {
4562236b
HW
1297 amdgpu_dm_update_connector_after_detect(aconnector);
1298
1299
1300 drm_modeset_lock_all(dev);
1301 dm_restore_drm_connector_state(dev, connector);
1302 drm_modeset_unlock_all(dev);
1303
1304 if (aconnector->base.force == DRM_FORCE_UNSPECIFIED)
1305 drm_kms_helper_hotplug_event(dev);
1306 }
1307 mutex_unlock(&aconnector->hpd_lock);
1308
1309}
1310
c84dec2f 1311static void dm_handle_hpd_rx_irq(struct amdgpu_dm_connector *aconnector)
4562236b
HW
1312{
1313 uint8_t esi[DP_PSR_ERROR_STATUS - DP_SINK_COUNT_ESI] = { 0 };
1314 uint8_t dret;
1315 bool new_irq_handled = false;
1316 int dpcd_addr;
1317 int dpcd_bytes_to_read;
1318
1319 const int max_process_count = 30;
1320 int process_count = 0;
1321
1322 const struct dc_link_status *link_status = dc_link_get_status(aconnector->dc_link);
1323
1324 if (link_status->dpcd_caps->dpcd_rev.raw < 0x12) {
1325 dpcd_bytes_to_read = DP_LANE0_1_STATUS - DP_SINK_COUNT;
1326 /* DPCD 0x200 - 0x201 for downstream IRQ */
1327 dpcd_addr = DP_SINK_COUNT;
1328 } else {
1329 dpcd_bytes_to_read = DP_PSR_ERROR_STATUS - DP_SINK_COUNT_ESI;
1330 /* DPCD 0x2002 - 0x2005 for downstream IRQ */
1331 dpcd_addr = DP_SINK_COUNT_ESI;
1332 }
1333
1334 dret = drm_dp_dpcd_read(
1335 &aconnector->dm_dp_aux.aux,
1336 dpcd_addr,
1337 esi,
1338 dpcd_bytes_to_read);
1339
1340 while (dret == dpcd_bytes_to_read &&
1341 process_count < max_process_count) {
1342 uint8_t retry;
1343 dret = 0;
1344
1345 process_count++;
1346
f1ad2f5e 1347 DRM_DEBUG_DRIVER("ESI %02x %02x %02x\n", esi[0], esi[1], esi[2]);
4562236b
HW
1348 /* handle HPD short pulse irq */
1349 if (aconnector->mst_mgr.mst_state)
1350 drm_dp_mst_hpd_irq(
1351 &aconnector->mst_mgr,
1352 esi,
1353 &new_irq_handled);
4562236b
HW
1354
1355 if (new_irq_handled) {
1356 /* ACK at DPCD to notify down stream */
1357 const int ack_dpcd_bytes_to_write =
1358 dpcd_bytes_to_read - 1;
1359
1360 for (retry = 0; retry < 3; retry++) {
1361 uint8_t wret;
1362
1363 wret = drm_dp_dpcd_write(
1364 &aconnector->dm_dp_aux.aux,
1365 dpcd_addr + 1,
1366 &esi[1],
1367 ack_dpcd_bytes_to_write);
1368 if (wret == ack_dpcd_bytes_to_write)
1369 break;
1370 }
1371
1f6010a9 1372 /* check if there is new irq to be handled */
4562236b
HW
1373 dret = drm_dp_dpcd_read(
1374 &aconnector->dm_dp_aux.aux,
1375 dpcd_addr,
1376 esi,
1377 dpcd_bytes_to_read);
1378
1379 new_irq_handled = false;
d4a6e8a9 1380 } else {
4562236b 1381 break;
d4a6e8a9 1382 }
4562236b
HW
1383 }
1384
1385 if (process_count == max_process_count)
f1ad2f5e 1386 DRM_DEBUG_DRIVER("Loop exceeded max iterations\n");
4562236b
HW
1387}
1388
1389static void handle_hpd_rx_irq(void *param)
1390{
c84dec2f 1391 struct amdgpu_dm_connector *aconnector = (struct amdgpu_dm_connector *)param;
4562236b
HW
1392 struct drm_connector *connector = &aconnector->base;
1393 struct drm_device *dev = connector->dev;
53cbf65c 1394 struct dc_link *dc_link = aconnector->dc_link;
4562236b 1395 bool is_mst_root_connector = aconnector->mst_mgr.mst_state;
fbbdadf2 1396 enum dc_connection_type new_connection_type = dc_connection_none;
4562236b 1397
1f6010a9
DF
1398 /*
1399 * TODO:Temporary add mutex to protect hpd interrupt not have a gpio
4562236b
HW
1400 * conflict, after implement i2c helper, this mutex should be
1401 * retired.
1402 */
53cbf65c 1403 if (dc_link->type != dc_connection_mst_branch)
4562236b
HW
1404 mutex_lock(&aconnector->hpd_lock);
1405
4e18814e 1406 if (dc_link_handle_hpd_rx_irq(dc_link, NULL, NULL) &&
4562236b
HW
1407 !is_mst_root_connector) {
1408 /* Downstream Port status changed. */
fbbdadf2
BL
1409 if (!dc_link_detect_sink(dc_link, &new_connection_type))
1410 DRM_ERROR("KMS: Failed to detect connector\n");
1411
1412 if (aconnector->base.force && new_connection_type == dc_connection_none) {
1413 emulated_link_detect(dc_link);
1414
1415 if (aconnector->fake_enable)
1416 aconnector->fake_enable = false;
1417
1418 amdgpu_dm_update_connector_after_detect(aconnector);
1419
1420
1421 drm_modeset_lock_all(dev);
1422 dm_restore_drm_connector_state(dev, connector);
1423 drm_modeset_unlock_all(dev);
1424
1425 drm_kms_helper_hotplug_event(dev);
1426 } else if (dc_link_detect(dc_link, DETECT_REASON_HPDRX)) {
88ac3dda
RL
1427
1428 if (aconnector->fake_enable)
1429 aconnector->fake_enable = false;
1430
4562236b
HW
1431 amdgpu_dm_update_connector_after_detect(aconnector);
1432
1433
1434 drm_modeset_lock_all(dev);
1435 dm_restore_drm_connector_state(dev, connector);
1436 drm_modeset_unlock_all(dev);
1437
1438 drm_kms_helper_hotplug_event(dev);
1439 }
1440 }
1441 if ((dc_link->cur_link_settings.lane_count != LANE_COUNT_UNKNOWN) ||
53cbf65c 1442 (dc_link->type == dc_connection_mst_branch))
4562236b
HW
1443 dm_handle_hpd_rx_irq(aconnector);
1444
e86e8947
HV
1445 if (dc_link->type != dc_connection_mst_branch) {
1446 drm_dp_cec_irq(&aconnector->dm_dp_aux.aux);
4562236b 1447 mutex_unlock(&aconnector->hpd_lock);
e86e8947 1448 }
4562236b
HW
1449}
1450
1451static void register_hpd_handlers(struct amdgpu_device *adev)
1452{
1453 struct drm_device *dev = adev->ddev;
1454 struct drm_connector *connector;
c84dec2f 1455 struct amdgpu_dm_connector *aconnector;
4562236b
HW
1456 const struct dc_link *dc_link;
1457 struct dc_interrupt_params int_params = {0};
1458
1459 int_params.requested_polarity = INTERRUPT_POLARITY_DEFAULT;
1460 int_params.current_polarity = INTERRUPT_POLARITY_DEFAULT;
1461
1462 list_for_each_entry(connector,
1463 &dev->mode_config.connector_list, head) {
1464
c84dec2f 1465 aconnector = to_amdgpu_dm_connector(connector);
4562236b
HW
1466 dc_link = aconnector->dc_link;
1467
1468 if (DC_IRQ_SOURCE_INVALID != dc_link->irq_source_hpd) {
1469 int_params.int_context = INTERRUPT_LOW_IRQ_CONTEXT;
1470 int_params.irq_source = dc_link->irq_source_hpd;
1471
1472 amdgpu_dm_irq_register_interrupt(adev, &int_params,
1473 handle_hpd_irq,
1474 (void *) aconnector);
1475 }
1476
1477 if (DC_IRQ_SOURCE_INVALID != dc_link->irq_source_hpd_rx) {
1478
1479 /* Also register for DP short pulse (hpd_rx). */
1480 int_params.int_context = INTERRUPT_LOW_IRQ_CONTEXT;
1481 int_params.irq_source = dc_link->irq_source_hpd_rx;
1482
1483 amdgpu_dm_irq_register_interrupt(adev, &int_params,
1484 handle_hpd_rx_irq,
1485 (void *) aconnector);
1486 }
1487 }
1488}
1489
1490/* Register IRQ sources and initialize IRQ callbacks */
1491static int dce110_register_irq_handlers(struct amdgpu_device *adev)
1492{
1493 struct dc *dc = adev->dm.dc;
1494 struct common_irq_params *c_irq_params;
1495 struct dc_interrupt_params int_params = {0};
1496 int r;
1497 int i;
1ffdeca6 1498 unsigned client_id = AMDGPU_IRQ_CLIENTID_LEGACY;
2c8ad2d5 1499
ff5ef992 1500 if (adev->asic_type == CHIP_VEGA10 ||
2325ff30 1501 adev->asic_type == CHIP_VEGA12 ||
1fe6bf2f 1502 adev->asic_type == CHIP_VEGA20 ||
ff5ef992 1503 adev->asic_type == CHIP_RAVEN)
3760f76c 1504 client_id = SOC15_IH_CLIENTID_DCE;
4562236b
HW
1505
1506 int_params.requested_polarity = INTERRUPT_POLARITY_DEFAULT;
1507 int_params.current_polarity = INTERRUPT_POLARITY_DEFAULT;
1508
1f6010a9
DF
1509 /*
1510 * Actions of amdgpu_irq_add_id():
4562236b
HW
1511 * 1. Register a set() function with base driver.
1512 * Base driver will call set() function to enable/disable an
1513 * interrupt in DC hardware.
1514 * 2. Register amdgpu_dm_irq_handler().
1515 * Base driver will call amdgpu_dm_irq_handler() for ALL interrupts
1516 * coming from DC hardware.
1517 * amdgpu_dm_irq_handler() will re-direct the interrupt to DC
1518 * for acknowledging and handling. */
1519
b57de80a 1520 /* Use VBLANK interrupt */
e9029155 1521 for (i = VISLANDS30_IV_SRCID_D1_VERTICAL_INTERRUPT0; i <= VISLANDS30_IV_SRCID_D6_VERTICAL_INTERRUPT0; i++) {
2c8ad2d5 1522 r = amdgpu_irq_add_id(adev, client_id, i, &adev->crtc_irq);
4562236b
HW
1523 if (r) {
1524 DRM_ERROR("Failed to add crtc irq id!\n");
1525 return r;
1526 }
1527
1528 int_params.int_context = INTERRUPT_HIGH_IRQ_CONTEXT;
1529 int_params.irq_source =
3d761e79 1530 dc_interrupt_to_irq_source(dc, i, 0);
4562236b 1531
b57de80a 1532 c_irq_params = &adev->dm.vblank_params[int_params.irq_source - DC_IRQ_SOURCE_VBLANK1];
4562236b
HW
1533
1534 c_irq_params->adev = adev;
1535 c_irq_params->irq_src = int_params.irq_source;
1536
1537 amdgpu_dm_irq_register_interrupt(adev, &int_params,
1538 dm_crtc_high_irq, c_irq_params);
1539 }
1540
d2574c33
MK
1541 /* Use VUPDATE interrupt */
1542 for (i = VISLANDS30_IV_SRCID_D1_V_UPDATE_INT; i <= VISLANDS30_IV_SRCID_D6_V_UPDATE_INT; i += 2) {
1543 r = amdgpu_irq_add_id(adev, client_id, i, &adev->vupdate_irq);
1544 if (r) {
1545 DRM_ERROR("Failed to add vupdate irq id!\n");
1546 return r;
1547 }
1548
1549 int_params.int_context = INTERRUPT_HIGH_IRQ_CONTEXT;
1550 int_params.irq_source =
1551 dc_interrupt_to_irq_source(dc, i, 0);
1552
1553 c_irq_params = &adev->dm.vupdate_params[int_params.irq_source - DC_IRQ_SOURCE_VUPDATE1];
1554
1555 c_irq_params->adev = adev;
1556 c_irq_params->irq_src = int_params.irq_source;
1557
1558 amdgpu_dm_irq_register_interrupt(adev, &int_params,
1559 dm_vupdate_high_irq, c_irq_params);
1560 }
1561
3d761e79 1562 /* Use GRPH_PFLIP interrupt */
4562236b
HW
1563 for (i = VISLANDS30_IV_SRCID_D1_GRPH_PFLIP;
1564 i <= VISLANDS30_IV_SRCID_D6_GRPH_PFLIP; i += 2) {
2c8ad2d5 1565 r = amdgpu_irq_add_id(adev, client_id, i, &adev->pageflip_irq);
4562236b
HW
1566 if (r) {
1567 DRM_ERROR("Failed to add page flip irq id!\n");
1568 return r;
1569 }
1570
1571 int_params.int_context = INTERRUPT_HIGH_IRQ_CONTEXT;
1572 int_params.irq_source =
1573 dc_interrupt_to_irq_source(dc, i, 0);
1574
1575 c_irq_params = &adev->dm.pflip_params[int_params.irq_source - DC_IRQ_SOURCE_PFLIP_FIRST];
1576
1577 c_irq_params->adev = adev;
1578 c_irq_params->irq_src = int_params.irq_source;
1579
1580 amdgpu_dm_irq_register_interrupt(adev, &int_params,
1581 dm_pflip_high_irq, c_irq_params);
1582
1583 }
1584
1585 /* HPD */
2c8ad2d5
AD
1586 r = amdgpu_irq_add_id(adev, client_id,
1587 VISLANDS30_IV_SRCID_HOTPLUG_DETECT_A, &adev->hpd_irq);
4562236b
HW
1588 if (r) {
1589 DRM_ERROR("Failed to add hpd irq id!\n");
1590 return r;
1591 }
1592
1593 register_hpd_handlers(adev);
1594
1595 return 0;
1596}
1597
ff5ef992
AD
1598#if defined(CONFIG_DRM_AMD_DC_DCN1_0)
1599/* Register IRQ sources and initialize IRQ callbacks */
1600static int dcn10_register_irq_handlers(struct amdgpu_device *adev)
1601{
1602 struct dc *dc = adev->dm.dc;
1603 struct common_irq_params *c_irq_params;
1604 struct dc_interrupt_params int_params = {0};
1605 int r;
1606 int i;
1607
1608 int_params.requested_polarity = INTERRUPT_POLARITY_DEFAULT;
1609 int_params.current_polarity = INTERRUPT_POLARITY_DEFAULT;
1610
1f6010a9
DF
1611 /*
1612 * Actions of amdgpu_irq_add_id():
ff5ef992
AD
1613 * 1. Register a set() function with base driver.
1614 * Base driver will call set() function to enable/disable an
1615 * interrupt in DC hardware.
1616 * 2. Register amdgpu_dm_irq_handler().
1617 * Base driver will call amdgpu_dm_irq_handler() for ALL interrupts
1618 * coming from DC hardware.
1619 * amdgpu_dm_irq_handler() will re-direct the interrupt to DC
1620 * for acknowledging and handling.
1f6010a9 1621 */
ff5ef992
AD
1622
1623 /* Use VSTARTUP interrupt */
1624 for (i = DCN_1_0__SRCID__DC_D1_OTG_VSTARTUP;
1625 i <= DCN_1_0__SRCID__DC_D1_OTG_VSTARTUP + adev->mode_info.num_crtc - 1;
1626 i++) {
3760f76c 1627 r = amdgpu_irq_add_id(adev, SOC15_IH_CLIENTID_DCE, i, &adev->crtc_irq);
ff5ef992
AD
1628
1629 if (r) {
1630 DRM_ERROR("Failed to add crtc irq id!\n");
1631 return r;
1632 }
1633
1634 int_params.int_context = INTERRUPT_HIGH_IRQ_CONTEXT;
1635 int_params.irq_source =
1636 dc_interrupt_to_irq_source(dc, i, 0);
1637
1638 c_irq_params = &adev->dm.vblank_params[int_params.irq_source - DC_IRQ_SOURCE_VBLANK1];
1639
1640 c_irq_params->adev = adev;
1641 c_irq_params->irq_src = int_params.irq_source;
1642
1643 amdgpu_dm_irq_register_interrupt(adev, &int_params,
1644 dm_crtc_high_irq, c_irq_params);
1645 }
1646
d2574c33
MK
1647 /* Use VUPDATE_NO_LOCK interrupt on DCN, which seems to correspond to
1648 * the regular VUPDATE interrupt on DCE. We want DC_IRQ_SOURCE_VUPDATEx
1649 * to trigger at end of each vblank, regardless of state of the lock,
1650 * matching DCE behaviour.
1651 */
1652 for (i = DCN_1_0__SRCID__OTG0_IHC_V_UPDATE_NO_LOCK_INTERRUPT;
1653 i <= DCN_1_0__SRCID__OTG0_IHC_V_UPDATE_NO_LOCK_INTERRUPT + adev->mode_info.num_crtc - 1;
1654 i++) {
1655 r = amdgpu_irq_add_id(adev, SOC15_IH_CLIENTID_DCE, i, &adev->vupdate_irq);
1656
1657 if (r) {
1658 DRM_ERROR("Failed to add vupdate irq id!\n");
1659 return r;
1660 }
1661
1662 int_params.int_context = INTERRUPT_HIGH_IRQ_CONTEXT;
1663 int_params.irq_source =
1664 dc_interrupt_to_irq_source(dc, i, 0);
1665
1666 c_irq_params = &adev->dm.vupdate_params[int_params.irq_source - DC_IRQ_SOURCE_VUPDATE1];
1667
1668 c_irq_params->adev = adev;
1669 c_irq_params->irq_src = int_params.irq_source;
1670
1671 amdgpu_dm_irq_register_interrupt(adev, &int_params,
1672 dm_vupdate_high_irq, c_irq_params);
1673 }
1674
ff5ef992
AD
1675 /* Use GRPH_PFLIP interrupt */
1676 for (i = DCN_1_0__SRCID__HUBP0_FLIP_INTERRUPT;
1677 i <= DCN_1_0__SRCID__HUBP0_FLIP_INTERRUPT + adev->mode_info.num_crtc - 1;
1678 i++) {
3760f76c 1679 r = amdgpu_irq_add_id(adev, SOC15_IH_CLIENTID_DCE, i, &adev->pageflip_irq);
ff5ef992
AD
1680 if (r) {
1681 DRM_ERROR("Failed to add page flip irq id!\n");
1682 return r;
1683 }
1684
1685 int_params.int_context = INTERRUPT_HIGH_IRQ_CONTEXT;
1686 int_params.irq_source =
1687 dc_interrupt_to_irq_source(dc, i, 0);
1688
1689 c_irq_params = &adev->dm.pflip_params[int_params.irq_source - DC_IRQ_SOURCE_PFLIP_FIRST];
1690
1691 c_irq_params->adev = adev;
1692 c_irq_params->irq_src = int_params.irq_source;
1693
1694 amdgpu_dm_irq_register_interrupt(adev, &int_params,
1695 dm_pflip_high_irq, c_irq_params);
1696
1697 }
1698
1699 /* HPD */
3760f76c 1700 r = amdgpu_irq_add_id(adev, SOC15_IH_CLIENTID_DCE, DCN_1_0__SRCID__DC_HPD1_INT,
ff5ef992
AD
1701 &adev->hpd_irq);
1702 if (r) {
1703 DRM_ERROR("Failed to add hpd irq id!\n");
1704 return r;
1705 }
1706
1707 register_hpd_handlers(adev);
1708
1709 return 0;
1710}
1711#endif
1712
eb3dc897
NK
1713/*
1714 * Acquires the lock for the atomic state object and returns
1715 * the new atomic state.
1716 *
1717 * This should only be called during atomic check.
1718 */
1719static int dm_atomic_get_state(struct drm_atomic_state *state,
1720 struct dm_atomic_state **dm_state)
1721{
1722 struct drm_device *dev = state->dev;
1723 struct amdgpu_device *adev = dev->dev_private;
1724 struct amdgpu_display_manager *dm = &adev->dm;
1725 struct drm_private_state *priv_state;
eb3dc897
NK
1726
1727 if (*dm_state)
1728 return 0;
1729
eb3dc897
NK
1730 priv_state = drm_atomic_get_private_obj_state(state, &dm->atomic_obj);
1731 if (IS_ERR(priv_state))
1732 return PTR_ERR(priv_state);
1733
1734 *dm_state = to_dm_atomic_state(priv_state);
1735
1736 return 0;
1737}
1738
1739struct dm_atomic_state *
1740dm_atomic_get_new_state(struct drm_atomic_state *state)
1741{
1742 struct drm_device *dev = state->dev;
1743 struct amdgpu_device *adev = dev->dev_private;
1744 struct amdgpu_display_manager *dm = &adev->dm;
1745 struct drm_private_obj *obj;
1746 struct drm_private_state *new_obj_state;
1747 int i;
1748
1749 for_each_new_private_obj_in_state(state, obj, new_obj_state, i) {
1750 if (obj->funcs == dm->atomic_obj.funcs)
1751 return to_dm_atomic_state(new_obj_state);
1752 }
1753
1754 return NULL;
1755}
1756
1757struct dm_atomic_state *
1758dm_atomic_get_old_state(struct drm_atomic_state *state)
1759{
1760 struct drm_device *dev = state->dev;
1761 struct amdgpu_device *adev = dev->dev_private;
1762 struct amdgpu_display_manager *dm = &adev->dm;
1763 struct drm_private_obj *obj;
1764 struct drm_private_state *old_obj_state;
1765 int i;
1766
1767 for_each_old_private_obj_in_state(state, obj, old_obj_state, i) {
1768 if (obj->funcs == dm->atomic_obj.funcs)
1769 return to_dm_atomic_state(old_obj_state);
1770 }
1771
1772 return NULL;
1773}
1774
1775static struct drm_private_state *
1776dm_atomic_duplicate_state(struct drm_private_obj *obj)
1777{
1778 struct dm_atomic_state *old_state, *new_state;
1779
1780 new_state = kzalloc(sizeof(*new_state), GFP_KERNEL);
1781 if (!new_state)
1782 return NULL;
1783
1784 __drm_atomic_helper_private_obj_duplicate_state(obj, &new_state->base);
1785
813d20dc
AW
1786 old_state = to_dm_atomic_state(obj->state);
1787
1788 if (old_state && old_state->context)
1789 new_state->context = dc_copy_state(old_state->context);
1790
eb3dc897
NK
1791 if (!new_state->context) {
1792 kfree(new_state);
1793 return NULL;
1794 }
1795
eb3dc897
NK
1796 return &new_state->base;
1797}
1798
1799static void dm_atomic_destroy_state(struct drm_private_obj *obj,
1800 struct drm_private_state *state)
1801{
1802 struct dm_atomic_state *dm_state = to_dm_atomic_state(state);
1803
1804 if (dm_state && dm_state->context)
1805 dc_release_state(dm_state->context);
1806
1807 kfree(dm_state);
1808}
1809
1810static struct drm_private_state_funcs dm_atomic_state_funcs = {
1811 .atomic_duplicate_state = dm_atomic_duplicate_state,
1812 .atomic_destroy_state = dm_atomic_destroy_state,
1813};
1814
4562236b
HW
1815static int amdgpu_dm_mode_config_init(struct amdgpu_device *adev)
1816{
eb3dc897 1817 struct dm_atomic_state *state;
4562236b
HW
1818 int r;
1819
1820 adev->mode_info.mode_config_initialized = true;
1821
4562236b 1822 adev->ddev->mode_config.funcs = (void *)&amdgpu_dm_mode_funcs;
54f5499a 1823 adev->ddev->mode_config.helper_private = &amdgpu_dm_mode_config_helperfuncs;
4562236b
HW
1824
1825 adev->ddev->mode_config.max_width = 16384;
1826 adev->ddev->mode_config.max_height = 16384;
1827
1828 adev->ddev->mode_config.preferred_depth = 24;
1829 adev->ddev->mode_config.prefer_shadow = 1;
1f6010a9 1830 /* indicates support for immediate flip */
4562236b
HW
1831 adev->ddev->mode_config.async_page_flip = true;
1832
770d13b1 1833 adev->ddev->mode_config.fb_base = adev->gmc.aper_base;
4562236b 1834
eb3dc897
NK
1835 state = kzalloc(sizeof(*state), GFP_KERNEL);
1836 if (!state)
1837 return -ENOMEM;
1838
813d20dc 1839 state->context = dc_create_state(adev->dm.dc);
eb3dc897
NK
1840 if (!state->context) {
1841 kfree(state);
1842 return -ENOMEM;
1843 }
1844
1845 dc_resource_state_copy_construct_current(adev->dm.dc, state->context);
1846
8c1a765b
DA
1847 drm_atomic_private_obj_init(adev->ddev,
1848 &adev->dm.atomic_obj,
eb3dc897
NK
1849 &state->base,
1850 &dm_atomic_state_funcs);
1851
3dc9b1ce 1852 r = amdgpu_display_modeset_create_props(adev);
4562236b
HW
1853 if (r)
1854 return r;
1855
1856 return 0;
1857}
1858
206bbafe
DF
1859#define AMDGPU_DM_DEFAULT_MIN_BACKLIGHT 12
1860#define AMDGPU_DM_DEFAULT_MAX_BACKLIGHT 255
1861
4562236b
HW
1862#if defined(CONFIG_BACKLIGHT_CLASS_DEVICE) ||\
1863 defined(CONFIG_BACKLIGHT_CLASS_DEVICE_MODULE)
1864
206bbafe
DF
1865static void amdgpu_dm_update_backlight_caps(struct amdgpu_display_manager *dm)
1866{
1867#if defined(CONFIG_ACPI)
1868 struct amdgpu_dm_backlight_caps caps;
1869
1870 if (dm->backlight_caps.caps_valid)
1871 return;
1872
1873 amdgpu_acpi_get_backlight_caps(dm->adev, &caps);
1874 if (caps.caps_valid) {
1875 dm->backlight_caps.min_input_signal = caps.min_input_signal;
1876 dm->backlight_caps.max_input_signal = caps.max_input_signal;
1877 dm->backlight_caps.caps_valid = true;
1878 } else {
1879 dm->backlight_caps.min_input_signal =
1880 AMDGPU_DM_DEFAULT_MIN_BACKLIGHT;
1881 dm->backlight_caps.max_input_signal =
1882 AMDGPU_DM_DEFAULT_MAX_BACKLIGHT;
1883 }
1884#else
8bcbc9ef
DF
1885 dm->backlight_caps.min_input_signal = AMDGPU_DM_DEFAULT_MIN_BACKLIGHT;
1886 dm->backlight_caps.max_input_signal = AMDGPU_DM_DEFAULT_MAX_BACKLIGHT;
206bbafe
DF
1887#endif
1888}
1889
4562236b
HW
1890static int amdgpu_dm_backlight_update_status(struct backlight_device *bd)
1891{
1892 struct amdgpu_display_manager *dm = bl_get_data(bd);
206bbafe
DF
1893 struct amdgpu_dm_backlight_caps caps;
1894 uint32_t brightness = bd->props.brightness;
4562236b 1895
206bbafe
DF
1896 amdgpu_dm_update_backlight_caps(dm);
1897 caps = dm->backlight_caps;
0cafc82f 1898 /*
206bbafe
DF
1899 * The brightness input is in the range 0-255
1900 * It needs to be rescaled to be between the
1901 * requested min and max input signal
1902 *
1903 * It also needs to be scaled up by 0x101 to
1904 * match the DC interface which has a range of
1905 * 0 to 0xffff
0cafc82f 1906 */
206bbafe
DF
1907 brightness =
1908 brightness
1909 * 0x101
1910 * (caps.max_input_signal - caps.min_input_signal)
1911 / AMDGPU_MAX_BL_LEVEL
1912 + caps.min_input_signal * 0x101;
4562236b
HW
1913
1914 if (dc_link_set_backlight_level(dm->backlight_link,
923fe495 1915 brightness, 0))
4562236b
HW
1916 return 0;
1917 else
1918 return 1;
1919}
1920
1921static int amdgpu_dm_backlight_get_brightness(struct backlight_device *bd)
1922{
620a0d27
DF
1923 struct amdgpu_display_manager *dm = bl_get_data(bd);
1924 int ret = dc_link_get_backlight_level(dm->backlight_link);
1925
1926 if (ret == DC_ERROR_UNEXPECTED)
1927 return bd->props.brightness;
1928 return ret;
4562236b
HW
1929}
1930
1931static const struct backlight_ops amdgpu_dm_backlight_ops = {
1932 .get_brightness = amdgpu_dm_backlight_get_brightness,
1933 .update_status = amdgpu_dm_backlight_update_status,
1934};
1935
7578ecda
AD
1936static void
1937amdgpu_dm_register_backlight_device(struct amdgpu_display_manager *dm)
4562236b
HW
1938{
1939 char bl_name[16];
1940 struct backlight_properties props = { 0 };
1941
206bbafe
DF
1942 amdgpu_dm_update_backlight_caps(dm);
1943
4562236b 1944 props.max_brightness = AMDGPU_MAX_BL_LEVEL;
53a53f86 1945 props.brightness = AMDGPU_MAX_BL_LEVEL;
4562236b
HW
1946 props.type = BACKLIGHT_RAW;
1947
1948 snprintf(bl_name, sizeof(bl_name), "amdgpu_bl%d",
1949 dm->adev->ddev->primary->index);
1950
1951 dm->backlight_dev = backlight_device_register(bl_name,
1952 dm->adev->ddev->dev,
1953 dm,
1954 &amdgpu_dm_backlight_ops,
1955 &props);
1956
74baea42 1957 if (IS_ERR(dm->backlight_dev))
4562236b
HW
1958 DRM_ERROR("DM: Backlight registration failed!\n");
1959 else
f1ad2f5e 1960 DRM_DEBUG_DRIVER("DM: Registered Backlight device: %s\n", bl_name);
4562236b
HW
1961}
1962
1963#endif
1964
df534fff 1965static int initialize_plane(struct amdgpu_display_manager *dm,
b2fddb13 1966 struct amdgpu_mode_info *mode_info, int plane_id,
cc1fec57
NK
1967 enum drm_plane_type plane_type,
1968 const struct dc_plane_cap *plane_cap)
df534fff 1969{
f180b4bc 1970 struct drm_plane *plane;
df534fff
S
1971 unsigned long possible_crtcs;
1972 int ret = 0;
1973
f180b4bc 1974 plane = kzalloc(sizeof(struct drm_plane), GFP_KERNEL);
df534fff
S
1975 if (!plane) {
1976 DRM_ERROR("KMS: Failed to allocate plane\n");
1977 return -ENOMEM;
1978 }
b2fddb13 1979 plane->type = plane_type;
df534fff
S
1980
1981 /*
b2fddb13
NK
1982 * HACK: IGT tests expect that the primary plane for a CRTC
1983 * can only have one possible CRTC. Only expose support for
1984 * any CRTC if they're not going to be used as a primary plane
1985 * for a CRTC - like overlay or underlay planes.
df534fff
S
1986 */
1987 possible_crtcs = 1 << plane_id;
1988 if (plane_id >= dm->dc->caps.max_streams)
1989 possible_crtcs = 0xff;
1990
cc1fec57 1991 ret = amdgpu_dm_plane_init(dm, plane, possible_crtcs, plane_cap);
df534fff
S
1992
1993 if (ret) {
1994 DRM_ERROR("KMS: Failed to initialize plane\n");
54087768 1995 kfree(plane);
df534fff
S
1996 return ret;
1997 }
1998
54087768
NK
1999 if (mode_info)
2000 mode_info->planes[plane_id] = plane;
2001
df534fff
S
2002 return ret;
2003}
2004
89fc8d4e
HW
2005
2006static void register_backlight_device(struct amdgpu_display_manager *dm,
2007 struct dc_link *link)
2008{
2009#if defined(CONFIG_BACKLIGHT_CLASS_DEVICE) ||\
2010 defined(CONFIG_BACKLIGHT_CLASS_DEVICE_MODULE)
2011
2012 if ((link->connector_signal & (SIGNAL_TYPE_EDP | SIGNAL_TYPE_LVDS)) &&
2013 link->type != dc_connection_none) {
1f6010a9
DF
2014 /*
2015 * Event if registration failed, we should continue with
89fc8d4e
HW
2016 * DM initialization because not having a backlight control
2017 * is better then a black screen.
2018 */
2019 amdgpu_dm_register_backlight_device(dm);
2020
2021 if (dm->backlight_dev)
2022 dm->backlight_link = link;
2023 }
2024#endif
2025}
2026
2027
1f6010a9
DF
2028/*
2029 * In this architecture, the association
4562236b
HW
2030 * connector -> encoder -> crtc
2031 * id not really requried. The crtc and connector will hold the
2032 * display_index as an abstraction to use with DAL component
2033 *
2034 * Returns 0 on success
2035 */
7578ecda 2036static int amdgpu_dm_initialize_drm_device(struct amdgpu_device *adev)
4562236b
HW
2037{
2038 struct amdgpu_display_manager *dm = &adev->dm;
df534fff 2039 int32_t i;
c84dec2f 2040 struct amdgpu_dm_connector *aconnector = NULL;
f2a0f5e6 2041 struct amdgpu_encoder *aencoder = NULL;
d4e13b0d 2042 struct amdgpu_mode_info *mode_info = &adev->mode_info;
4562236b 2043 uint32_t link_cnt;
cc1fec57 2044 int32_t primary_planes;
fbbdadf2 2045 enum dc_connection_type new_connection_type = dc_connection_none;
cc1fec57 2046 const struct dc_plane_cap *plane;
4562236b
HW
2047
2048 link_cnt = dm->dc->caps.max_links;
4562236b
HW
2049 if (amdgpu_dm_mode_config_init(dm->adev)) {
2050 DRM_ERROR("DM: Failed to initialize mode config\n");
59d0f396 2051 return -EINVAL;
4562236b
HW
2052 }
2053
b2fddb13
NK
2054 /* There is one primary plane per CRTC */
2055 primary_planes = dm->dc->caps.max_streams;
54087768 2056 ASSERT(primary_planes <= AMDGPU_MAX_PLANES);
efa6a8b7 2057
b2fddb13
NK
2058 /*
2059 * Initialize primary planes, implicit planes for legacy IOCTLS.
2060 * Order is reversed to match iteration order in atomic check.
2061 */
2062 for (i = (primary_planes - 1); i >= 0; i--) {
cc1fec57
NK
2063 plane = &dm->dc->caps.planes[i];
2064
b2fddb13 2065 if (initialize_plane(dm, mode_info, i,
cc1fec57 2066 DRM_PLANE_TYPE_PRIMARY, plane)) {
df534fff 2067 DRM_ERROR("KMS: Failed to initialize primary plane\n");
cd8a2ae8 2068 goto fail;
d4e13b0d 2069 }
df534fff 2070 }
92f3ac40 2071
0d579c7e
NK
2072 /*
2073 * Initialize overlay planes, index starting after primary planes.
2074 * These planes have a higher DRM index than the primary planes since
2075 * they should be considered as having a higher z-order.
2076 * Order is reversed to match iteration order in atomic check.
cc1fec57
NK
2077 *
2078 * Only support DCN for now, and only expose one so we don't encourage
2079 * userspace to use up all the pipes.
0d579c7e 2080 */
cc1fec57
NK
2081 for (i = 0; i < dm->dc->caps.max_planes; ++i) {
2082 struct dc_plane_cap *plane = &dm->dc->caps.planes[i];
2083
2084 if (plane->type != DC_PLANE_TYPE_DCN_UNIVERSAL)
2085 continue;
2086
2087 if (!plane->blends_with_above || !plane->blends_with_below)
2088 continue;
2089
ea36ad34 2090 if (!plane->pixel_format_support.argb8888)
cc1fec57
NK
2091 continue;
2092
54087768 2093 if (initialize_plane(dm, NULL, primary_planes + i,
cc1fec57 2094 DRM_PLANE_TYPE_OVERLAY, plane)) {
0d579c7e 2095 DRM_ERROR("KMS: Failed to initialize overlay plane\n");
cd8a2ae8 2096 goto fail;
d4e13b0d 2097 }
cc1fec57
NK
2098
2099 /* Only create one overlay plane. */
2100 break;
d4e13b0d 2101 }
4562236b 2102
d4e13b0d 2103 for (i = 0; i < dm->dc->caps.max_streams; i++)
f180b4bc 2104 if (amdgpu_dm_crtc_init(dm, mode_info->planes[i], i)) {
4562236b 2105 DRM_ERROR("KMS: Failed to initialize crtc\n");
cd8a2ae8 2106 goto fail;
4562236b 2107 }
4562236b 2108
ab2541b6 2109 dm->display_indexes_num = dm->dc->caps.max_streams;
4562236b
HW
2110
2111 /* loops over all connectors on the board */
2112 for (i = 0; i < link_cnt; i++) {
89fc8d4e 2113 struct dc_link *link = NULL;
4562236b
HW
2114
2115 if (i > AMDGPU_DM_MAX_DISPLAY_INDEX) {
2116 DRM_ERROR(
2117 "KMS: Cannot support more than %d display indexes\n",
2118 AMDGPU_DM_MAX_DISPLAY_INDEX);
2119 continue;
2120 }
2121
2122 aconnector = kzalloc(sizeof(*aconnector), GFP_KERNEL);
2123 if (!aconnector)
cd8a2ae8 2124 goto fail;
4562236b
HW
2125
2126 aencoder = kzalloc(sizeof(*aencoder), GFP_KERNEL);
8440c304 2127 if (!aencoder)
cd8a2ae8 2128 goto fail;
4562236b
HW
2129
2130 if (amdgpu_dm_encoder_init(dm->ddev, aencoder, i)) {
2131 DRM_ERROR("KMS: Failed to initialize encoder\n");
cd8a2ae8 2132 goto fail;
4562236b
HW
2133 }
2134
2135 if (amdgpu_dm_connector_init(dm, aconnector, i, aencoder)) {
2136 DRM_ERROR("KMS: Failed to initialize connector\n");
cd8a2ae8 2137 goto fail;
4562236b
HW
2138 }
2139
89fc8d4e
HW
2140 link = dc_get_link_at_index(dm->dc, i);
2141
fbbdadf2
BL
2142 if (!dc_link_detect_sink(link, &new_connection_type))
2143 DRM_ERROR("KMS: Failed to detect connector\n");
2144
2145 if (aconnector->base.force && new_connection_type == dc_connection_none) {
2146 emulated_link_detect(link);
2147 amdgpu_dm_update_connector_after_detect(aconnector);
2148
2149 } else if (dc_link_detect(link, DETECT_REASON_BOOT)) {
4562236b 2150 amdgpu_dm_update_connector_after_detect(aconnector);
89fc8d4e
HW
2151 register_backlight_device(dm, link);
2152 }
2153
2154
4562236b
HW
2155 }
2156
2157 /* Software is initialized. Now we can register interrupt handlers. */
2158 switch (adev->asic_type) {
2159 case CHIP_BONAIRE:
2160 case CHIP_HAWAII:
cd4b356f
AD
2161 case CHIP_KAVERI:
2162 case CHIP_KABINI:
2163 case CHIP_MULLINS:
4562236b
HW
2164 case CHIP_TONGA:
2165 case CHIP_FIJI:
2166 case CHIP_CARRIZO:
2167 case CHIP_STONEY:
2168 case CHIP_POLARIS11:
2169 case CHIP_POLARIS10:
b264d345 2170 case CHIP_POLARIS12:
7737de91 2171 case CHIP_VEGAM:
2c8ad2d5 2172 case CHIP_VEGA10:
2325ff30 2173 case CHIP_VEGA12:
1fe6bf2f 2174 case CHIP_VEGA20:
4562236b
HW
2175 if (dce110_register_irq_handlers(dm->adev)) {
2176 DRM_ERROR("DM: Failed to initialize IRQ\n");
cd8a2ae8 2177 goto fail;
4562236b
HW
2178 }
2179 break;
ff5ef992
AD
2180#if defined(CONFIG_DRM_AMD_DC_DCN1_0)
2181 case CHIP_RAVEN:
2182 if (dcn10_register_irq_handlers(dm->adev)) {
2183 DRM_ERROR("DM: Failed to initialize IRQ\n");
cd8a2ae8 2184 goto fail;
ff5ef992
AD
2185 }
2186 break;
2187#endif
4562236b 2188 default:
e63f8673 2189 DRM_ERROR("Unsupported ASIC type: 0x%X\n", adev->asic_type);
cd8a2ae8 2190 goto fail;
4562236b
HW
2191 }
2192
1bc460a4
HW
2193 if (adev->asic_type != CHIP_CARRIZO && adev->asic_type != CHIP_STONEY)
2194 dm->dc->debug.disable_stutter = amdgpu_pp_feature_mask & PP_STUTTER_MODE ? false : true;
2195
4562236b 2196 return 0;
cd8a2ae8 2197fail:
4562236b 2198 kfree(aencoder);
4562236b 2199 kfree(aconnector);
54087768 2200
59d0f396 2201 return -EINVAL;
4562236b
HW
2202}
2203
7578ecda 2204static void amdgpu_dm_destroy_drm_device(struct amdgpu_display_manager *dm)
4562236b
HW
2205{
2206 drm_mode_config_cleanup(dm->ddev);
eb3dc897 2207 drm_atomic_private_obj_fini(&dm->atomic_obj);
4562236b
HW
2208 return;
2209}
2210
2211/******************************************************************************
2212 * amdgpu_display_funcs functions
2213 *****************************************************************************/
2214
1f6010a9 2215/*
4562236b
HW
2216 * dm_bandwidth_update - program display watermarks
2217 *
2218 * @adev: amdgpu_device pointer
2219 *
2220 * Calculate and program the display watermarks and line buffer allocation.
2221 */
2222static void dm_bandwidth_update(struct amdgpu_device *adev)
2223{
49c07a99 2224 /* TODO: implement later */
4562236b
HW
2225}
2226
39cc5be2 2227static const struct amdgpu_display_funcs dm_display_funcs = {
4562236b
HW
2228 .bandwidth_update = dm_bandwidth_update, /* called unconditionally */
2229 .vblank_get_counter = dm_vblank_get_counter,/* called unconditionally */
7b42573b
HW
2230 .backlight_set_level = NULL, /* never called for DC */
2231 .backlight_get_level = NULL, /* never called for DC */
4562236b
HW
2232 .hpd_sense = NULL,/* called unconditionally */
2233 .hpd_set_polarity = NULL, /* called unconditionally */
2234 .hpd_get_gpio_reg = NULL, /* VBIOS parsing. DAL does it. */
4562236b
HW
2235 .page_flip_get_scanoutpos =
2236 dm_crtc_get_scanoutpos,/* called unconditionally */
2237 .add_encoder = NULL, /* VBIOS parsing. DAL does it. */
2238 .add_connector = NULL, /* VBIOS parsing. DAL does it. */
4562236b
HW
2239};
2240
2241#if defined(CONFIG_DEBUG_KERNEL_DC)
2242
3ee6b26b
AD
2243static ssize_t s3_debug_store(struct device *device,
2244 struct device_attribute *attr,
2245 const char *buf,
2246 size_t count)
4562236b
HW
2247{
2248 int ret;
2249 int s3_state;
2250 struct pci_dev *pdev = to_pci_dev(device);
2251 struct drm_device *drm_dev = pci_get_drvdata(pdev);
2252 struct amdgpu_device *adev = drm_dev->dev_private;
2253
2254 ret = kstrtoint(buf, 0, &s3_state);
2255
2256 if (ret == 0) {
2257 if (s3_state) {
2258 dm_resume(adev);
4562236b
HW
2259 drm_kms_helper_hotplug_event(adev->ddev);
2260 } else
2261 dm_suspend(adev);
2262 }
2263
2264 return ret == 0 ? count : 0;
2265}
2266
2267DEVICE_ATTR_WO(s3_debug);
2268
2269#endif
2270
2271static int dm_early_init(void *handle)
2272{
2273 struct amdgpu_device *adev = (struct amdgpu_device *)handle;
2274
4562236b
HW
2275 switch (adev->asic_type) {
2276 case CHIP_BONAIRE:
2277 case CHIP_HAWAII:
2278 adev->mode_info.num_crtc = 6;
2279 adev->mode_info.num_hpd = 6;
2280 adev->mode_info.num_dig = 6;
4562236b 2281 break;
cd4b356f
AD
2282 case CHIP_KAVERI:
2283 adev->mode_info.num_crtc = 4;
2284 adev->mode_info.num_hpd = 6;
2285 adev->mode_info.num_dig = 7;
cd4b356f
AD
2286 break;
2287 case CHIP_KABINI:
2288 case CHIP_MULLINS:
2289 adev->mode_info.num_crtc = 2;
2290 adev->mode_info.num_hpd = 6;
2291 adev->mode_info.num_dig = 6;
cd4b356f 2292 break;
4562236b
HW
2293 case CHIP_FIJI:
2294 case CHIP_TONGA:
2295 adev->mode_info.num_crtc = 6;
2296 adev->mode_info.num_hpd = 6;
2297 adev->mode_info.num_dig = 7;
4562236b
HW
2298 break;
2299 case CHIP_CARRIZO:
2300 adev->mode_info.num_crtc = 3;
2301 adev->mode_info.num_hpd = 6;
2302 adev->mode_info.num_dig = 9;
4562236b
HW
2303 break;
2304 case CHIP_STONEY:
2305 adev->mode_info.num_crtc = 2;
2306 adev->mode_info.num_hpd = 6;
2307 adev->mode_info.num_dig = 9;
4562236b
HW
2308 break;
2309 case CHIP_POLARIS11:
b264d345 2310 case CHIP_POLARIS12:
4562236b
HW
2311 adev->mode_info.num_crtc = 5;
2312 adev->mode_info.num_hpd = 5;
2313 adev->mode_info.num_dig = 5;
4562236b
HW
2314 break;
2315 case CHIP_POLARIS10:
7737de91 2316 case CHIP_VEGAM:
4562236b
HW
2317 adev->mode_info.num_crtc = 6;
2318 adev->mode_info.num_hpd = 6;
2319 adev->mode_info.num_dig = 6;
4562236b 2320 break;
2c8ad2d5 2321 case CHIP_VEGA10:
2325ff30 2322 case CHIP_VEGA12:
1fe6bf2f 2323 case CHIP_VEGA20:
2c8ad2d5
AD
2324 adev->mode_info.num_crtc = 6;
2325 adev->mode_info.num_hpd = 6;
2326 adev->mode_info.num_dig = 6;
2327 break;
ff5ef992
AD
2328#if defined(CONFIG_DRM_AMD_DC_DCN1_0)
2329 case CHIP_RAVEN:
2330 adev->mode_info.num_crtc = 4;
2331 adev->mode_info.num_hpd = 4;
2332 adev->mode_info.num_dig = 4;
ff5ef992
AD
2333 break;
2334#endif
4562236b 2335 default:
e63f8673 2336 DRM_ERROR("Unsupported ASIC type: 0x%X\n", adev->asic_type);
4562236b
HW
2337 return -EINVAL;
2338 }
2339
c8dd5715
MD
2340 amdgpu_dm_set_irq_funcs(adev);
2341
39cc5be2
AD
2342 if (adev->mode_info.funcs == NULL)
2343 adev->mode_info.funcs = &dm_display_funcs;
2344
1f6010a9
DF
2345 /*
2346 * Note: Do NOT change adev->audio_endpt_rreg and
4562236b 2347 * adev->audio_endpt_wreg because they are initialised in
1f6010a9
DF
2348 * amdgpu_device_init()
2349 */
4562236b
HW
2350#if defined(CONFIG_DEBUG_KERNEL_DC)
2351 device_create_file(
2352 adev->ddev->dev,
2353 &dev_attr_s3_debug);
2354#endif
2355
2356 return 0;
2357}
2358
9b690ef3 2359static bool modeset_required(struct drm_crtc_state *crtc_state,
0971c40e
HW
2360 struct dc_stream_state *new_stream,
2361 struct dc_stream_state *old_stream)
9b690ef3 2362{
e7b07cee
HW
2363 if (!drm_atomic_crtc_needs_modeset(crtc_state))
2364 return false;
2365
2366 if (!crtc_state->enable)
2367 return false;
2368
2369 return crtc_state->active;
2370}
2371
2372static bool modereset_required(struct drm_crtc_state *crtc_state)
2373{
2374 if (!drm_atomic_crtc_needs_modeset(crtc_state))
2375 return false;
2376
2377 return !crtc_state->enable || !crtc_state->active;
2378}
2379
7578ecda 2380static void amdgpu_dm_encoder_destroy(struct drm_encoder *encoder)
e7b07cee
HW
2381{
2382 drm_encoder_cleanup(encoder);
2383 kfree(encoder);
2384}
2385
2386static const struct drm_encoder_funcs amdgpu_dm_encoder_funcs = {
2387 .destroy = amdgpu_dm_encoder_destroy,
2388};
2389
e7b07cee 2390
695af5f9
NK
2391static int fill_dc_scaling_info(const struct drm_plane_state *state,
2392 struct dc_scaling_info *scaling_info)
2393{
6491f0c0
NK
2394 int scale_w, scale_h;
2395
695af5f9 2396 memset(scaling_info, 0, sizeof(*scaling_info));
e7b07cee 2397
695af5f9
NK
2398 /* Source is fixed 16.16 but we ignore mantissa for now... */
2399 scaling_info->src_rect.x = state->src_x >> 16;
2400 scaling_info->src_rect.y = state->src_y >> 16;
2401
2402 scaling_info->src_rect.width = state->src_w >> 16;
2403 if (scaling_info->src_rect.width == 0)
2404 return -EINVAL;
2405
2406 scaling_info->src_rect.height = state->src_h >> 16;
2407 if (scaling_info->src_rect.height == 0)
2408 return -EINVAL;
2409
2410 scaling_info->dst_rect.x = state->crtc_x;
2411 scaling_info->dst_rect.y = state->crtc_y;
e7b07cee
HW
2412
2413 if (state->crtc_w == 0)
695af5f9 2414 return -EINVAL;
e7b07cee 2415
695af5f9 2416 scaling_info->dst_rect.width = state->crtc_w;
e7b07cee
HW
2417
2418 if (state->crtc_h == 0)
695af5f9 2419 return -EINVAL;
e7b07cee 2420
695af5f9 2421 scaling_info->dst_rect.height = state->crtc_h;
e7b07cee 2422
695af5f9
NK
2423 /* DRM doesn't specify clipping on destination output. */
2424 scaling_info->clip_rect = scaling_info->dst_rect;
e7b07cee 2425
6491f0c0
NK
2426 /* TODO: Validate scaling per-format with DC plane caps */
2427 scale_w = scaling_info->dst_rect.width * 1000 /
2428 scaling_info->src_rect.width;
2429
2430 if (scale_w < 250 || scale_w > 16000)
2431 return -EINVAL;
2432
2433 scale_h = scaling_info->dst_rect.height * 1000 /
2434 scaling_info->src_rect.height;
2435
2436 if (scale_h < 250 || scale_h > 16000)
2437 return -EINVAL;
2438
695af5f9
NK
2439 /*
2440 * The "scaling_quality" can be ignored for now, quality = 0 has DC
2441 * assume reasonable defaults based on the format.
2442 */
e7b07cee 2443
695af5f9 2444 return 0;
4562236b 2445}
695af5f9 2446
3ee6b26b 2447static int get_fb_info(const struct amdgpu_framebuffer *amdgpu_fb,
9817d5f5 2448 uint64_t *tiling_flags)
e7b07cee 2449{
e68d14dd 2450 struct amdgpu_bo *rbo = gem_to_amdgpu_bo(amdgpu_fb->base.obj[0]);
e7b07cee 2451 int r = amdgpu_bo_reserve(rbo, false);
b830ebc9 2452
e7b07cee 2453 if (unlikely(r)) {
1f6010a9 2454 /* Don't show error message when returning -ERESTARTSYS */
9bbc3031
JZ
2455 if (r != -ERESTARTSYS)
2456 DRM_ERROR("Unable to reserve buffer: %d\n", r);
e7b07cee
HW
2457 return r;
2458 }
2459
e7b07cee
HW
2460 if (tiling_flags)
2461 amdgpu_bo_get_tiling_flags(rbo, tiling_flags);
2462
2463 amdgpu_bo_unreserve(rbo);
2464
2465 return r;
2466}
2467
7df7e505
NK
2468static inline uint64_t get_dcc_address(uint64_t address, uint64_t tiling_flags)
2469{
2470 uint32_t offset = AMDGPU_TILING_GET(tiling_flags, DCC_OFFSET_256B);
2471
2472 return offset ? (address + offset * 256) : 0;
2473}
2474
695af5f9
NK
2475static int
2476fill_plane_dcc_attributes(struct amdgpu_device *adev,
2477 const struct amdgpu_framebuffer *afb,
2478 const enum surface_pixel_format format,
2479 const enum dc_rotation_angle rotation,
2480 const union plane_size *plane_size,
2481 const union dc_tiling_info *tiling_info,
2482 const uint64_t info,
2483 struct dc_plane_dcc_param *dcc,
2484 struct dc_plane_address *address)
7df7e505
NK
2485{
2486 struct dc *dc = adev->dm.dc;
8daa1218
NC
2487 struct dc_dcc_surface_param input;
2488 struct dc_surface_dcc_cap output;
7df7e505
NK
2489 uint32_t offset = AMDGPU_TILING_GET(info, DCC_OFFSET_256B);
2490 uint32_t i64b = AMDGPU_TILING_GET(info, DCC_INDEPENDENT_64B) != 0;
2491 uint64_t dcc_address;
2492
8daa1218
NC
2493 memset(&input, 0, sizeof(input));
2494 memset(&output, 0, sizeof(output));
2495
7df7e505 2496 if (!offset)
09e5665a
NK
2497 return 0;
2498
695af5f9 2499 if (format >= SURFACE_PIXEL_FORMAT_VIDEO_BEGIN)
09e5665a 2500 return 0;
7df7e505
NK
2501
2502 if (!dc->cap_funcs.get_dcc_compression_cap)
09e5665a 2503 return -EINVAL;
7df7e505 2504
695af5f9
NK
2505 input.format = format;
2506 input.surface_size.width = plane_size->grph.surface_size.width;
2507 input.surface_size.height = plane_size->grph.surface_size.height;
2508 input.swizzle_mode = tiling_info->gfx9.swizzle;
7df7e505 2509
695af5f9 2510 if (rotation == ROTATION_ANGLE_0 || rotation == ROTATION_ANGLE_180)
7df7e505 2511 input.scan = SCAN_DIRECTION_HORIZONTAL;
695af5f9 2512 else if (rotation == ROTATION_ANGLE_90 || rotation == ROTATION_ANGLE_270)
7df7e505
NK
2513 input.scan = SCAN_DIRECTION_VERTICAL;
2514
2515 if (!dc->cap_funcs.get_dcc_compression_cap(dc, &input, &output))
09e5665a 2516 return -EINVAL;
7df7e505
NK
2517
2518 if (!output.capable)
09e5665a 2519 return -EINVAL;
7df7e505
NK
2520
2521 if (i64b == 0 && output.grph.rgb.independent_64b_blks != 0)
09e5665a 2522 return -EINVAL;
7df7e505 2523
09e5665a
NK
2524 dcc->enable = 1;
2525 dcc->grph.meta_pitch =
7df7e505 2526 AMDGPU_TILING_GET(info, DCC_PITCH_MAX) + 1;
09e5665a 2527 dcc->grph.independent_64b_blks = i64b;
7df7e505
NK
2528
2529 dcc_address = get_dcc_address(afb->address, info);
09e5665a
NK
2530 address->grph.meta_addr.low_part = lower_32_bits(dcc_address);
2531 address->grph.meta_addr.high_part = upper_32_bits(dcc_address);
7df7e505 2532
09e5665a
NK
2533 return 0;
2534}
2535
2536static int
320932bf 2537fill_plane_buffer_attributes(struct amdgpu_device *adev,
09e5665a 2538 const struct amdgpu_framebuffer *afb,
695af5f9
NK
2539 const enum surface_pixel_format format,
2540 const enum dc_rotation_angle rotation,
2541 const uint64_t tiling_flags,
09e5665a 2542 union dc_tiling_info *tiling_info,
320932bf 2543 union plane_size *plane_size,
09e5665a 2544 struct dc_plane_dcc_param *dcc,
695af5f9 2545 struct dc_plane_address *address)
09e5665a 2546{
320932bf 2547 const struct drm_framebuffer *fb = &afb->base;
09e5665a
NK
2548 int ret;
2549
2550 memset(tiling_info, 0, sizeof(*tiling_info));
320932bf 2551 memset(plane_size, 0, sizeof(*plane_size));
09e5665a 2552 memset(dcc, 0, sizeof(*dcc));
e0634e8d
NK
2553 memset(address, 0, sizeof(*address));
2554
695af5f9 2555 if (format < SURFACE_PIXEL_FORMAT_VIDEO_BEGIN) {
320932bf
NK
2556 plane_size->grph.surface_size.x = 0;
2557 plane_size->grph.surface_size.y = 0;
2558 plane_size->grph.surface_size.width = fb->width;
2559 plane_size->grph.surface_size.height = fb->height;
2560 plane_size->grph.surface_pitch =
2561 fb->pitches[0] / fb->format->cpp[0];
2562
e0634e8d
NK
2563 address->type = PLN_ADDR_TYPE_GRAPHICS;
2564 address->grph.addr.low_part = lower_32_bits(afb->address);
2565 address->grph.addr.high_part = upper_32_bits(afb->address);
2566 } else {
1791e54f 2567 uint64_t chroma_addr = afb->address + fb->offsets[1];
e0634e8d 2568
320932bf
NK
2569 plane_size->video.luma_size.x = 0;
2570 plane_size->video.luma_size.y = 0;
2571 plane_size->video.luma_size.width = fb->width;
2572 plane_size->video.luma_size.height = fb->height;
2573 plane_size->video.luma_pitch =
2574 fb->pitches[0] / fb->format->cpp[0];
2575
2576 plane_size->video.chroma_size.x = 0;
2577 plane_size->video.chroma_size.y = 0;
2578 /* TODO: set these based on surface format */
2579 plane_size->video.chroma_size.width = fb->width / 2;
2580 plane_size->video.chroma_size.height = fb->height / 2;
2581
2582 plane_size->video.chroma_pitch =
2583 fb->pitches[1] / fb->format->cpp[1];
2584
e0634e8d
NK
2585 address->type = PLN_ADDR_TYPE_VIDEO_PROGRESSIVE;
2586 address->video_progressive.luma_addr.low_part =
2587 lower_32_bits(afb->address);
2588 address->video_progressive.luma_addr.high_part =
2589 upper_32_bits(afb->address);
2590 address->video_progressive.chroma_addr.low_part =
2591 lower_32_bits(chroma_addr);
2592 address->video_progressive.chroma_addr.high_part =
2593 upper_32_bits(chroma_addr);
2594 }
09e5665a
NK
2595
2596 /* Fill GFX8 params */
2597 if (AMDGPU_TILING_GET(tiling_flags, ARRAY_MODE) == DC_ARRAY_2D_TILED_THIN1) {
2598 unsigned int bankw, bankh, mtaspect, tile_split, num_banks;
2599
2600 bankw = AMDGPU_TILING_GET(tiling_flags, BANK_WIDTH);
2601 bankh = AMDGPU_TILING_GET(tiling_flags, BANK_HEIGHT);
2602 mtaspect = AMDGPU_TILING_GET(tiling_flags, MACRO_TILE_ASPECT);
2603 tile_split = AMDGPU_TILING_GET(tiling_flags, TILE_SPLIT);
2604 num_banks = AMDGPU_TILING_GET(tiling_flags, NUM_BANKS);
2605
2606 /* XXX fix me for VI */
2607 tiling_info->gfx8.num_banks = num_banks;
2608 tiling_info->gfx8.array_mode =
2609 DC_ARRAY_2D_TILED_THIN1;
2610 tiling_info->gfx8.tile_split = tile_split;
2611 tiling_info->gfx8.bank_width = bankw;
2612 tiling_info->gfx8.bank_height = bankh;
2613 tiling_info->gfx8.tile_aspect = mtaspect;
2614 tiling_info->gfx8.tile_mode =
2615 DC_ADDR_SURF_MICRO_TILING_DISPLAY;
2616 } else if (AMDGPU_TILING_GET(tiling_flags, ARRAY_MODE)
2617 == DC_ARRAY_1D_TILED_THIN1) {
2618 tiling_info->gfx8.array_mode = DC_ARRAY_1D_TILED_THIN1;
2619 }
2620
2621 tiling_info->gfx8.pipe_config =
2622 AMDGPU_TILING_GET(tiling_flags, PIPE_CONFIG);
2623
2624 if (adev->asic_type == CHIP_VEGA10 ||
2625 adev->asic_type == CHIP_VEGA12 ||
2626 adev->asic_type == CHIP_VEGA20 ||
2627 adev->asic_type == CHIP_RAVEN) {
2628 /* Fill GFX9 params */
2629 tiling_info->gfx9.num_pipes =
2630 adev->gfx.config.gb_addr_config_fields.num_pipes;
2631 tiling_info->gfx9.num_banks =
2632 adev->gfx.config.gb_addr_config_fields.num_banks;
2633 tiling_info->gfx9.pipe_interleave =
2634 adev->gfx.config.gb_addr_config_fields.pipe_interleave_size;
2635 tiling_info->gfx9.num_shader_engines =
2636 adev->gfx.config.gb_addr_config_fields.num_se;
2637 tiling_info->gfx9.max_compressed_frags =
2638 adev->gfx.config.gb_addr_config_fields.max_compress_frags;
2639 tiling_info->gfx9.num_rb_per_se =
2640 adev->gfx.config.gb_addr_config_fields.num_rb_per_se;
2641 tiling_info->gfx9.swizzle =
2642 AMDGPU_TILING_GET(tiling_flags, SWIZZLE_MODE);
2643 tiling_info->gfx9.shaderEnable = 1;
2644
695af5f9
NK
2645 ret = fill_plane_dcc_attributes(adev, afb, format, rotation,
2646 plane_size, tiling_info,
2647 tiling_flags, dcc, address);
09e5665a
NK
2648 if (ret)
2649 return ret;
2650 }
2651
2652 return 0;
7df7e505
NK
2653}
2654
d74004b6 2655static void
695af5f9 2656fill_blending_from_plane_state(const struct drm_plane_state *plane_state,
d74004b6
NK
2657 bool *per_pixel_alpha, bool *global_alpha,
2658 int *global_alpha_value)
2659{
2660 *per_pixel_alpha = false;
2661 *global_alpha = false;
2662 *global_alpha_value = 0xff;
2663
2664 if (plane_state->plane->type != DRM_PLANE_TYPE_OVERLAY)
2665 return;
2666
2667 if (plane_state->pixel_blend_mode == DRM_MODE_BLEND_PREMULTI) {
2668 static const uint32_t alpha_formats[] = {
2669 DRM_FORMAT_ARGB8888,
2670 DRM_FORMAT_RGBA8888,
2671 DRM_FORMAT_ABGR8888,
2672 };
2673 uint32_t format = plane_state->fb->format->format;
2674 unsigned int i;
2675
2676 for (i = 0; i < ARRAY_SIZE(alpha_formats); ++i) {
2677 if (format == alpha_formats[i]) {
2678 *per_pixel_alpha = true;
2679 break;
2680 }
2681 }
2682 }
2683
2684 if (plane_state->alpha < 0xffff) {
2685 *global_alpha = true;
2686 *global_alpha_value = plane_state->alpha >> 8;
2687 }
2688}
2689
004fefa3
NK
2690static int
2691fill_plane_color_attributes(const struct drm_plane_state *plane_state,
695af5f9 2692 const enum surface_pixel_format format,
004fefa3
NK
2693 enum dc_color_space *color_space)
2694{
2695 bool full_range;
2696
2697 *color_space = COLOR_SPACE_SRGB;
2698
2699 /* DRM color properties only affect non-RGB formats. */
695af5f9 2700 if (format < SURFACE_PIXEL_FORMAT_VIDEO_BEGIN)
004fefa3
NK
2701 return 0;
2702
2703 full_range = (plane_state->color_range == DRM_COLOR_YCBCR_FULL_RANGE);
2704
2705 switch (plane_state->color_encoding) {
2706 case DRM_COLOR_YCBCR_BT601:
2707 if (full_range)
2708 *color_space = COLOR_SPACE_YCBCR601;
2709 else
2710 *color_space = COLOR_SPACE_YCBCR601_LIMITED;
2711 break;
2712
2713 case DRM_COLOR_YCBCR_BT709:
2714 if (full_range)
2715 *color_space = COLOR_SPACE_YCBCR709;
2716 else
2717 *color_space = COLOR_SPACE_YCBCR709_LIMITED;
2718 break;
2719
2720 case DRM_COLOR_YCBCR_BT2020:
2721 if (full_range)
2722 *color_space = COLOR_SPACE_2020_YCBCR;
2723 else
2724 return -EINVAL;
2725 break;
2726
2727 default:
2728 return -EINVAL;
2729 }
2730
2731 return 0;
2732}
2733
695af5f9
NK
2734static int
2735fill_dc_plane_info_and_addr(struct amdgpu_device *adev,
2736 const struct drm_plane_state *plane_state,
2737 const uint64_t tiling_flags,
2738 struct dc_plane_info *plane_info,
2739 struct dc_plane_address *address)
2740{
2741 const struct drm_framebuffer *fb = plane_state->fb;
2742 const struct amdgpu_framebuffer *afb =
2743 to_amdgpu_framebuffer(plane_state->fb);
2744 struct drm_format_name_buf format_name;
2745 int ret;
2746
2747 memset(plane_info, 0, sizeof(*plane_info));
2748
2749 switch (fb->format->format) {
2750 case DRM_FORMAT_C8:
2751 plane_info->format =
2752 SURFACE_PIXEL_FORMAT_GRPH_PALETA_256_COLORS;
2753 break;
2754 case DRM_FORMAT_RGB565:
2755 plane_info->format = SURFACE_PIXEL_FORMAT_GRPH_RGB565;
2756 break;
2757 case DRM_FORMAT_XRGB8888:
2758 case DRM_FORMAT_ARGB8888:
2759 plane_info->format = SURFACE_PIXEL_FORMAT_GRPH_ARGB8888;
2760 break;
2761 case DRM_FORMAT_XRGB2101010:
2762 case DRM_FORMAT_ARGB2101010:
2763 plane_info->format = SURFACE_PIXEL_FORMAT_GRPH_ARGB2101010;
2764 break;
2765 case DRM_FORMAT_XBGR2101010:
2766 case DRM_FORMAT_ABGR2101010:
2767 plane_info->format = SURFACE_PIXEL_FORMAT_GRPH_ABGR2101010;
2768 break;
2769 case DRM_FORMAT_XBGR8888:
2770 case DRM_FORMAT_ABGR8888:
2771 plane_info->format = SURFACE_PIXEL_FORMAT_GRPH_ABGR8888;
2772 break;
2773 case DRM_FORMAT_NV21:
2774 plane_info->format = SURFACE_PIXEL_FORMAT_VIDEO_420_YCbCr;
2775 break;
2776 case DRM_FORMAT_NV12:
2777 plane_info->format = SURFACE_PIXEL_FORMAT_VIDEO_420_YCrCb;
2778 break;
2779 default:
2780 DRM_ERROR(
2781 "Unsupported screen format %s\n",
2782 drm_get_format_name(fb->format->format, &format_name));
2783 return -EINVAL;
2784 }
2785
2786 switch (plane_state->rotation & DRM_MODE_ROTATE_MASK) {
2787 case DRM_MODE_ROTATE_0:
2788 plane_info->rotation = ROTATION_ANGLE_0;
2789 break;
2790 case DRM_MODE_ROTATE_90:
2791 plane_info->rotation = ROTATION_ANGLE_90;
2792 break;
2793 case DRM_MODE_ROTATE_180:
2794 plane_info->rotation = ROTATION_ANGLE_180;
2795 break;
2796 case DRM_MODE_ROTATE_270:
2797 plane_info->rotation = ROTATION_ANGLE_270;
2798 break;
2799 default:
2800 plane_info->rotation = ROTATION_ANGLE_0;
2801 break;
2802 }
2803
2804 plane_info->visible = true;
2805 plane_info->stereo_format = PLANE_STEREO_FORMAT_NONE;
2806
2807 ret = fill_plane_color_attributes(plane_state, plane_info->format,
2808 &plane_info->color_space);
2809 if (ret)
2810 return ret;
2811
2812 ret = fill_plane_buffer_attributes(adev, afb, plane_info->format,
2813 plane_info->rotation, tiling_flags,
2814 &plane_info->tiling_info,
2815 &plane_info->plane_size,
2816 &plane_info->dcc, address);
2817 if (ret)
2818 return ret;
2819
2820 fill_blending_from_plane_state(
2821 plane_state, &plane_info->per_pixel_alpha,
2822 &plane_info->global_alpha, &plane_info->global_alpha_value);
2823
2824 return 0;
2825}
2826
2827static int fill_dc_plane_attributes(struct amdgpu_device *adev,
2828 struct dc_plane_state *dc_plane_state,
2829 struct drm_plane_state *plane_state,
2830 struct drm_crtc_state *crtc_state)
e7b07cee
HW
2831{
2832 const struct amdgpu_framebuffer *amdgpu_fb =
2833 to_amdgpu_framebuffer(plane_state->fb);
695af5f9
NK
2834 struct dc_scaling_info scaling_info;
2835 struct dc_plane_info plane_info;
2836 uint64_t tiling_flags;
2837 int ret;
e7b07cee 2838
695af5f9
NK
2839 ret = fill_dc_scaling_info(plane_state, &scaling_info);
2840 if (ret)
2841 return ret;
e7b07cee 2842
695af5f9
NK
2843 dc_plane_state->src_rect = scaling_info.src_rect;
2844 dc_plane_state->dst_rect = scaling_info.dst_rect;
2845 dc_plane_state->clip_rect = scaling_info.clip_rect;
2846 dc_plane_state->scaling_quality = scaling_info.scaling_quality;
e7b07cee 2847
695af5f9 2848 ret = get_fb_info(amdgpu_fb, &tiling_flags);
e7b07cee
HW
2849 if (ret)
2850 return ret;
2851
695af5f9
NK
2852 ret = fill_dc_plane_info_and_addr(adev, plane_state, tiling_flags,
2853 &plane_info,
2854 &dc_plane_state->address);
004fefa3
NK
2855 if (ret)
2856 return ret;
2857
695af5f9
NK
2858 dc_plane_state->format = plane_info.format;
2859 dc_plane_state->color_space = plane_info.color_space;
2860 dc_plane_state->format = plane_info.format;
2861 dc_plane_state->plane_size = plane_info.plane_size;
2862 dc_plane_state->rotation = plane_info.rotation;
2863 dc_plane_state->horizontal_mirror = plane_info.horizontal_mirror;
2864 dc_plane_state->stereo_format = plane_info.stereo_format;
2865 dc_plane_state->tiling_info = plane_info.tiling_info;
2866 dc_plane_state->visible = plane_info.visible;
2867 dc_plane_state->per_pixel_alpha = plane_info.per_pixel_alpha;
2868 dc_plane_state->global_alpha = plane_info.global_alpha;
2869 dc_plane_state->global_alpha_value = plane_info.global_alpha_value;
2870 dc_plane_state->dcc = plane_info.dcc;
2871
e277adc5
LSL
2872 /*
2873 * Always set input transfer function, since plane state is refreshed
2874 * every time.
2875 */
2876 ret = amdgpu_dm_set_degamma_lut(crtc_state, dc_plane_state);
8c45c5db
LSL
2877 if (ret) {
2878 dc_transfer_func_release(dc_plane_state->in_transfer_func);
2879 dc_plane_state->in_transfer_func = NULL;
2880 }
e7b07cee
HW
2881
2882 return ret;
2883}
2884
3ee6b26b
AD
2885static void update_stream_scaling_settings(const struct drm_display_mode *mode,
2886 const struct dm_connector_state *dm_state,
2887 struct dc_stream_state *stream)
e7b07cee
HW
2888{
2889 enum amdgpu_rmx_type rmx_type;
2890
2891 struct rect src = { 0 }; /* viewport in composition space*/
2892 struct rect dst = { 0 }; /* stream addressable area */
2893
2894 /* no mode. nothing to be done */
2895 if (!mode)
2896 return;
2897
2898 /* Full screen scaling by default */
2899 src.width = mode->hdisplay;
2900 src.height = mode->vdisplay;
2901 dst.width = stream->timing.h_addressable;
2902 dst.height = stream->timing.v_addressable;
2903
f4791779
HW
2904 if (dm_state) {
2905 rmx_type = dm_state->scaling;
2906 if (rmx_type == RMX_ASPECT || rmx_type == RMX_OFF) {
2907 if (src.width * dst.height <
2908 src.height * dst.width) {
2909 /* height needs less upscaling/more downscaling */
2910 dst.width = src.width *
2911 dst.height / src.height;
2912 } else {
2913 /* width needs less upscaling/more downscaling */
2914 dst.height = src.height *
2915 dst.width / src.width;
2916 }
2917 } else if (rmx_type == RMX_CENTER) {
2918 dst = src;
e7b07cee 2919 }
e7b07cee 2920
f4791779
HW
2921 dst.x = (stream->timing.h_addressable - dst.width) / 2;
2922 dst.y = (stream->timing.v_addressable - dst.height) / 2;
e7b07cee 2923
f4791779
HW
2924 if (dm_state->underscan_enable) {
2925 dst.x += dm_state->underscan_hborder / 2;
2926 dst.y += dm_state->underscan_vborder / 2;
2927 dst.width -= dm_state->underscan_hborder;
2928 dst.height -= dm_state->underscan_vborder;
2929 }
e7b07cee
HW
2930 }
2931
2932 stream->src = src;
2933 stream->dst = dst;
2934
f1ad2f5e 2935 DRM_DEBUG_DRIVER("Destination Rectangle x:%d y:%d width:%d height:%d\n",
e7b07cee
HW
2936 dst.x, dst.y, dst.width, dst.height);
2937
2938}
2939
3ee6b26b
AD
2940static enum dc_color_depth
2941convert_color_depth_from_display_info(const struct drm_connector *connector)
e7b07cee 2942{
07e3a1cf
NK
2943 struct dm_connector_state *dm_conn_state =
2944 to_dm_connector_state(connector->state);
e7b07cee
HW
2945 uint32_t bpc = connector->display_info.bpc;
2946
07e3a1cf
NK
2947 /* TODO: Remove this when there's support for max_bpc in drm */
2948 if (dm_conn_state && bpc > dm_conn_state->max_bpc)
2949 /* Round down to nearest even number. */
2950 bpc = dm_conn_state->max_bpc - (dm_conn_state->max_bpc & 1);
2951
e7b07cee
HW
2952 switch (bpc) {
2953 case 0:
1f6010a9
DF
2954 /*
2955 * Temporary Work around, DRM doesn't parse color depth for
e7b07cee
HW
2956 * EDID revision before 1.4
2957 * TODO: Fix edid parsing
2958 */
2959 return COLOR_DEPTH_888;
2960 case 6:
2961 return COLOR_DEPTH_666;
2962 case 8:
2963 return COLOR_DEPTH_888;
2964 case 10:
2965 return COLOR_DEPTH_101010;
2966 case 12:
2967 return COLOR_DEPTH_121212;
2968 case 14:
2969 return COLOR_DEPTH_141414;
2970 case 16:
2971 return COLOR_DEPTH_161616;
2972 default:
2973 return COLOR_DEPTH_UNDEFINED;
2974 }
2975}
2976
3ee6b26b
AD
2977static enum dc_aspect_ratio
2978get_aspect_ratio(const struct drm_display_mode *mode_in)
e7b07cee 2979{
e11d4147
LSL
2980 /* 1-1 mapping, since both enums follow the HDMI spec. */
2981 return (enum dc_aspect_ratio) mode_in->picture_aspect_ratio;
e7b07cee
HW
2982}
2983
3ee6b26b
AD
2984static enum dc_color_space
2985get_output_color_space(const struct dc_crtc_timing *dc_crtc_timing)
e7b07cee
HW
2986{
2987 enum dc_color_space color_space = COLOR_SPACE_SRGB;
2988
2989 switch (dc_crtc_timing->pixel_encoding) {
2990 case PIXEL_ENCODING_YCBCR422:
2991 case PIXEL_ENCODING_YCBCR444:
2992 case PIXEL_ENCODING_YCBCR420:
2993 {
2994 /*
2995 * 27030khz is the separation point between HDTV and SDTV
2996 * according to HDMI spec, we use YCbCr709 and YCbCr601
2997 * respectively
2998 */
380604e2 2999 if (dc_crtc_timing->pix_clk_100hz > 270300) {
e7b07cee
HW
3000 if (dc_crtc_timing->flags.Y_ONLY)
3001 color_space =
3002 COLOR_SPACE_YCBCR709_LIMITED;
3003 else
3004 color_space = COLOR_SPACE_YCBCR709;
3005 } else {
3006 if (dc_crtc_timing->flags.Y_ONLY)
3007 color_space =
3008 COLOR_SPACE_YCBCR601_LIMITED;
3009 else
3010 color_space = COLOR_SPACE_YCBCR601;
3011 }
3012
3013 }
3014 break;
3015 case PIXEL_ENCODING_RGB:
3016 color_space = COLOR_SPACE_SRGB;
3017 break;
3018
3019 default:
3020 WARN_ON(1);
3021 break;
3022 }
3023
3024 return color_space;
3025}
3026
400443e8
ML
3027static void reduce_mode_colour_depth(struct dc_crtc_timing *timing_out)
3028{
3029 if (timing_out->display_color_depth <= COLOR_DEPTH_888)
3030 return;
3031
3032 timing_out->display_color_depth--;
3033}
3034
3035static void adjust_colour_depth_from_display_info(struct dc_crtc_timing *timing_out,
3036 const struct drm_display_info *info)
3037{
3038 int normalized_clk;
3039 if (timing_out->display_color_depth <= COLOR_DEPTH_888)
3040 return;
3041 do {
380604e2 3042 normalized_clk = timing_out->pix_clk_100hz / 10;
400443e8
ML
3043 /* YCbCr 4:2:0 requires additional adjustment of 1/2 */
3044 if (timing_out->pixel_encoding == PIXEL_ENCODING_YCBCR420)
3045 normalized_clk /= 2;
3046 /* Adjusting pix clock following on HDMI spec based on colour depth */
3047 switch (timing_out->display_color_depth) {
3048 case COLOR_DEPTH_101010:
3049 normalized_clk = (normalized_clk * 30) / 24;
3050 break;
3051 case COLOR_DEPTH_121212:
3052 normalized_clk = (normalized_clk * 36) / 24;
3053 break;
3054 case COLOR_DEPTH_161616:
3055 normalized_clk = (normalized_clk * 48) / 24;
3056 break;
3057 default:
3058 return;
3059 }
3060 if (normalized_clk <= info->max_tmds_clock)
3061 return;
3062 reduce_mode_colour_depth(timing_out);
3063
3064 } while (timing_out->display_color_depth > COLOR_DEPTH_888);
3065
3066}
e7b07cee 3067
3ee6b26b
AD
3068static void
3069fill_stream_properties_from_drm_display_mode(struct dc_stream_state *stream,
3070 const struct drm_display_mode *mode_in,
b333730d
BL
3071 const struct drm_connector *connector,
3072 const struct dc_stream_state *old_stream)
e7b07cee
HW
3073{
3074 struct dc_crtc_timing *timing_out = &stream->timing;
fe61a2f1 3075 const struct drm_display_info *info = &connector->display_info;
b830ebc9 3076
e7b07cee
HW
3077 memset(timing_out, 0, sizeof(struct dc_crtc_timing));
3078
3079 timing_out->h_border_left = 0;
3080 timing_out->h_border_right = 0;
3081 timing_out->v_border_top = 0;
3082 timing_out->v_border_bottom = 0;
3083 /* TODO: un-hardcode */
fe61a2f1 3084 if (drm_mode_is_420_only(info, mode_in)
ceb3dbb4 3085 && stream->signal == SIGNAL_TYPE_HDMI_TYPE_A)
fe61a2f1
ML
3086 timing_out->pixel_encoding = PIXEL_ENCODING_YCBCR420;
3087 else if ((connector->display_info.color_formats & DRM_COLOR_FORMAT_YCRCB444)
ceb3dbb4 3088 && stream->signal == SIGNAL_TYPE_HDMI_TYPE_A)
e7b07cee
HW
3089 timing_out->pixel_encoding = PIXEL_ENCODING_YCBCR444;
3090 else
3091 timing_out->pixel_encoding = PIXEL_ENCODING_RGB;
3092
3093 timing_out->timing_3d_format = TIMING_3D_FORMAT_NONE;
3094 timing_out->display_color_depth = convert_color_depth_from_display_info(
3095 connector);
3096 timing_out->scan_type = SCANNING_TYPE_NODATA;
3097 timing_out->hdmi_vic = 0;
b333730d
BL
3098
3099 if(old_stream) {
3100 timing_out->vic = old_stream->timing.vic;
3101 timing_out->flags.HSYNC_POSITIVE_POLARITY = old_stream->timing.flags.HSYNC_POSITIVE_POLARITY;
3102 timing_out->flags.VSYNC_POSITIVE_POLARITY = old_stream->timing.flags.VSYNC_POSITIVE_POLARITY;
3103 } else {
3104 timing_out->vic = drm_match_cea_mode(mode_in);
3105 if (mode_in->flags & DRM_MODE_FLAG_PHSYNC)
3106 timing_out->flags.HSYNC_POSITIVE_POLARITY = 1;
3107 if (mode_in->flags & DRM_MODE_FLAG_PVSYNC)
3108 timing_out->flags.VSYNC_POSITIVE_POLARITY = 1;
3109 }
e7b07cee
HW
3110
3111 timing_out->h_addressable = mode_in->crtc_hdisplay;
3112 timing_out->h_total = mode_in->crtc_htotal;
3113 timing_out->h_sync_width =
3114 mode_in->crtc_hsync_end - mode_in->crtc_hsync_start;
3115 timing_out->h_front_porch =
3116 mode_in->crtc_hsync_start - mode_in->crtc_hdisplay;
3117 timing_out->v_total = mode_in->crtc_vtotal;
3118 timing_out->v_addressable = mode_in->crtc_vdisplay;
3119 timing_out->v_front_porch =
3120 mode_in->crtc_vsync_start - mode_in->crtc_vdisplay;
3121 timing_out->v_sync_width =
3122 mode_in->crtc_vsync_end - mode_in->crtc_vsync_start;
380604e2 3123 timing_out->pix_clk_100hz = mode_in->crtc_clock * 10;
e7b07cee 3124 timing_out->aspect_ratio = get_aspect_ratio(mode_in);
e7b07cee
HW
3125
3126 stream->output_color_space = get_output_color_space(timing_out);
3127
e43a432c
AK
3128 stream->out_transfer_func->type = TF_TYPE_PREDEFINED;
3129 stream->out_transfer_func->tf = TRANSFER_FUNCTION_SRGB;
ceb3dbb4 3130 if (stream->signal == SIGNAL_TYPE_HDMI_TYPE_A)
400443e8 3131 adjust_colour_depth_from_display_info(timing_out, info);
e7b07cee
HW
3132}
3133
3ee6b26b
AD
3134static void fill_audio_info(struct audio_info *audio_info,
3135 const struct drm_connector *drm_connector,
3136 const struct dc_sink *dc_sink)
e7b07cee
HW
3137{
3138 int i = 0;
3139 int cea_revision = 0;
3140 const struct dc_edid_caps *edid_caps = &dc_sink->edid_caps;
3141
3142 audio_info->manufacture_id = edid_caps->manufacturer_id;
3143 audio_info->product_id = edid_caps->product_id;
3144
3145 cea_revision = drm_connector->display_info.cea_rev;
3146
090afc1e 3147 strscpy(audio_info->display_name,
d2b2562c 3148 edid_caps->display_name,
090afc1e 3149 AUDIO_INFO_DISPLAY_NAME_SIZE_IN_CHARS);
e7b07cee 3150
b830ebc9 3151 if (cea_revision >= 3) {
e7b07cee
HW
3152 audio_info->mode_count = edid_caps->audio_mode_count;
3153
3154 for (i = 0; i < audio_info->mode_count; ++i) {
3155 audio_info->modes[i].format_code =
3156 (enum audio_format_code)
3157 (edid_caps->audio_modes[i].format_code);
3158 audio_info->modes[i].channel_count =
3159 edid_caps->audio_modes[i].channel_count;
3160 audio_info->modes[i].sample_rates.all =
3161 edid_caps->audio_modes[i].sample_rate;
3162 audio_info->modes[i].sample_size =
3163 edid_caps->audio_modes[i].sample_size;
3164 }
3165 }
3166
3167 audio_info->flags.all = edid_caps->speaker_flags;
3168
3169 /* TODO: We only check for the progressive mode, check for interlace mode too */
b830ebc9 3170 if (drm_connector->latency_present[0]) {
e7b07cee
HW
3171 audio_info->video_latency = drm_connector->video_latency[0];
3172 audio_info->audio_latency = drm_connector->audio_latency[0];
3173 }
3174
3175 /* TODO: For DP, video and audio latency should be calculated from DPCD caps */
3176
3177}
3178
3ee6b26b
AD
3179static void
3180copy_crtc_timing_for_drm_display_mode(const struct drm_display_mode *src_mode,
3181 struct drm_display_mode *dst_mode)
e7b07cee
HW
3182{
3183 dst_mode->crtc_hdisplay = src_mode->crtc_hdisplay;
3184 dst_mode->crtc_vdisplay = src_mode->crtc_vdisplay;
3185 dst_mode->crtc_clock = src_mode->crtc_clock;
3186 dst_mode->crtc_hblank_start = src_mode->crtc_hblank_start;
3187 dst_mode->crtc_hblank_end = src_mode->crtc_hblank_end;
b830ebc9 3188 dst_mode->crtc_hsync_start = src_mode->crtc_hsync_start;
e7b07cee
HW
3189 dst_mode->crtc_hsync_end = src_mode->crtc_hsync_end;
3190 dst_mode->crtc_htotal = src_mode->crtc_htotal;
3191 dst_mode->crtc_hskew = src_mode->crtc_hskew;
3192 dst_mode->crtc_vblank_start = src_mode->crtc_vblank_start;
3193 dst_mode->crtc_vblank_end = src_mode->crtc_vblank_end;
3194 dst_mode->crtc_vsync_start = src_mode->crtc_vsync_start;
3195 dst_mode->crtc_vsync_end = src_mode->crtc_vsync_end;
3196 dst_mode->crtc_vtotal = src_mode->crtc_vtotal;
3197}
3198
3ee6b26b
AD
3199static void
3200decide_crtc_timing_for_drm_display_mode(struct drm_display_mode *drm_mode,
3201 const struct drm_display_mode *native_mode,
3202 bool scale_enabled)
e7b07cee
HW
3203{
3204 if (scale_enabled) {
3205 copy_crtc_timing_for_drm_display_mode(native_mode, drm_mode);
3206 } else if (native_mode->clock == drm_mode->clock &&
3207 native_mode->htotal == drm_mode->htotal &&
3208 native_mode->vtotal == drm_mode->vtotal) {
3209 copy_crtc_timing_for_drm_display_mode(native_mode, drm_mode);
3210 } else {
3211 /* no scaling nor amdgpu inserted, no need to patch */
3212 }
3213}
3214
aed15309
ML
3215static struct dc_sink *
3216create_fake_sink(struct amdgpu_dm_connector *aconnector)
2e0ac3d6 3217{
2e0ac3d6 3218 struct dc_sink_init_data sink_init_data = { 0 };
aed15309 3219 struct dc_sink *sink = NULL;
2e0ac3d6
HW
3220 sink_init_data.link = aconnector->dc_link;
3221 sink_init_data.sink_signal = aconnector->dc_link->connector_signal;
3222
3223 sink = dc_sink_create(&sink_init_data);
423788c7 3224 if (!sink) {
2e0ac3d6 3225 DRM_ERROR("Failed to create sink!\n");
aed15309 3226 return NULL;
423788c7 3227 }
2e0ac3d6 3228 sink->sink_signal = SIGNAL_TYPE_VIRTUAL;
423788c7 3229
aed15309 3230 return sink;
2e0ac3d6
HW
3231}
3232
fa2123db
ML
3233static void set_multisync_trigger_params(
3234 struct dc_stream_state *stream)
3235{
3236 if (stream->triggered_crtc_reset.enabled) {
3237 stream->triggered_crtc_reset.event = CRTC_EVENT_VSYNC_RISING;
3238 stream->triggered_crtc_reset.delay = TRIGGER_DELAY_NEXT_LINE;
3239 }
3240}
3241
3242static void set_master_stream(struct dc_stream_state *stream_set[],
3243 int stream_count)
3244{
3245 int j, highest_rfr = 0, master_stream = 0;
3246
3247 for (j = 0; j < stream_count; j++) {
3248 if (stream_set[j] && stream_set[j]->triggered_crtc_reset.enabled) {
3249 int refresh_rate = 0;
3250
380604e2 3251 refresh_rate = (stream_set[j]->timing.pix_clk_100hz*100)/
fa2123db
ML
3252 (stream_set[j]->timing.h_total*stream_set[j]->timing.v_total);
3253 if (refresh_rate > highest_rfr) {
3254 highest_rfr = refresh_rate;
3255 master_stream = j;
3256 }
3257 }
3258 }
3259 for (j = 0; j < stream_count; j++) {
03736f4c 3260 if (stream_set[j])
fa2123db
ML
3261 stream_set[j]->triggered_crtc_reset.event_source = stream_set[master_stream];
3262 }
3263}
3264
3265static void dm_enable_per_frame_crtc_master_sync(struct dc_state *context)
3266{
3267 int i = 0;
3268
3269 if (context->stream_count < 2)
3270 return;
3271 for (i = 0; i < context->stream_count ; i++) {
3272 if (!context->streams[i])
3273 continue;
1f6010a9
DF
3274 /*
3275 * TODO: add a function to read AMD VSDB bits and set
fa2123db 3276 * crtc_sync_master.multi_sync_enabled flag
1f6010a9 3277 * For now it's set to false
fa2123db
ML
3278 */
3279 set_multisync_trigger_params(context->streams[i]);
3280 }
3281 set_master_stream(context->streams, context->stream_count);
3282}
3283
3ee6b26b
AD
3284static struct dc_stream_state *
3285create_stream_for_sink(struct amdgpu_dm_connector *aconnector,
3286 const struct drm_display_mode *drm_mode,
b333730d
BL
3287 const struct dm_connector_state *dm_state,
3288 const struct dc_stream_state *old_stream)
e7b07cee
HW
3289{
3290 struct drm_display_mode *preferred_mode = NULL;
391ef035 3291 struct drm_connector *drm_connector;
0971c40e 3292 struct dc_stream_state *stream = NULL;
e7b07cee
HW
3293 struct drm_display_mode mode = *drm_mode;
3294 bool native_mode_found = false;
b333730d
BL
3295 bool scale = dm_state ? (dm_state->scaling != RMX_OFF) : false;
3296 int mode_refresh;
58124bf8 3297 int preferred_refresh = 0;
b333730d 3298
aed15309 3299 struct dc_sink *sink = NULL;
b830ebc9 3300 if (aconnector == NULL) {
e7b07cee 3301 DRM_ERROR("aconnector is NULL!\n");
64245fa7 3302 return stream;
e7b07cee
HW
3303 }
3304
e7b07cee 3305 drm_connector = &aconnector->base;
2e0ac3d6 3306
f4ac176e 3307 if (!aconnector->dc_sink) {
e3fa5c4c
JFZ
3308 sink = create_fake_sink(aconnector);
3309 if (!sink)
3310 return stream;
aed15309
ML
3311 } else {
3312 sink = aconnector->dc_sink;
dcd5fb82 3313 dc_sink_retain(sink);
f4ac176e 3314 }
2e0ac3d6 3315
aed15309 3316 stream = dc_create_stream_for_sink(sink);
4562236b 3317
b830ebc9 3318 if (stream == NULL) {
e7b07cee 3319 DRM_ERROR("Failed to create stream for sink!\n");
aed15309 3320 goto finish;
e7b07cee
HW
3321 }
3322
ceb3dbb4
JL
3323 stream->dm_stream_context = aconnector;
3324
e7b07cee
HW
3325 list_for_each_entry(preferred_mode, &aconnector->base.modes, head) {
3326 /* Search for preferred mode */
3327 if (preferred_mode->type & DRM_MODE_TYPE_PREFERRED) {
3328 native_mode_found = true;
3329 break;
3330 }
3331 }
3332 if (!native_mode_found)
3333 preferred_mode = list_first_entry_or_null(
3334 &aconnector->base.modes,
3335 struct drm_display_mode,
3336 head);
3337
b333730d
BL
3338 mode_refresh = drm_mode_vrefresh(&mode);
3339
b830ebc9 3340 if (preferred_mode == NULL) {
1f6010a9
DF
3341 /*
3342 * This may not be an error, the use case is when we have no
e7b07cee
HW
3343 * usermode calls to reset and set mode upon hotplug. In this
3344 * case, we call set mode ourselves to restore the previous mode
3345 * and the modelist may not be filled in in time.
3346 */
f1ad2f5e 3347 DRM_DEBUG_DRIVER("No preferred mode found\n");
e7b07cee
HW
3348 } else {
3349 decide_crtc_timing_for_drm_display_mode(
3350 &mode, preferred_mode,
f4791779 3351 dm_state ? (dm_state->scaling != RMX_OFF) : false);
58124bf8 3352 preferred_refresh = drm_mode_vrefresh(preferred_mode);
e7b07cee
HW
3353 }
3354
f783577c
JFZ
3355 if (!dm_state)
3356 drm_mode_set_crtcinfo(&mode, 0);
3357
b333730d
BL
3358 /*
3359 * If scaling is enabled and refresh rate didn't change
3360 * we copy the vic and polarities of the old timings
3361 */
3362 if (!scale || mode_refresh != preferred_refresh)
3363 fill_stream_properties_from_drm_display_mode(stream,
3364 &mode, &aconnector->base, NULL);
3365 else
3366 fill_stream_properties_from_drm_display_mode(stream,
3367 &mode, &aconnector->base, old_stream);
3368
e7b07cee
HW
3369 update_stream_scaling_settings(&mode, dm_state, stream);
3370
3371 fill_audio_info(
3372 &stream->audio_info,
3373 drm_connector,
aed15309 3374 sink);
e7b07cee 3375
ceb3dbb4 3376 update_stream_signal(stream, sink);
9182b4cb 3377
aed15309 3378finish:
dcd5fb82 3379 dc_sink_release(sink);
9e3efe3e 3380
e7b07cee
HW
3381 return stream;
3382}
3383
7578ecda 3384static void amdgpu_dm_crtc_destroy(struct drm_crtc *crtc)
e7b07cee
HW
3385{
3386 drm_crtc_cleanup(crtc);
3387 kfree(crtc);
3388}
3389
3390static void dm_crtc_destroy_state(struct drm_crtc *crtc,
3ee6b26b 3391 struct drm_crtc_state *state)
e7b07cee
HW
3392{
3393 struct dm_crtc_state *cur = to_dm_crtc_state(state);
3394
3395 /* TODO Destroy dc_stream objects are stream object is flattened */
3396 if (cur->stream)
3397 dc_stream_release(cur->stream);
3398
3399
3400 __drm_atomic_helper_crtc_destroy_state(state);
3401
3402
3403 kfree(state);
3404}
3405
3406static void dm_crtc_reset_state(struct drm_crtc *crtc)
3407{
3408 struct dm_crtc_state *state;
3409
3410 if (crtc->state)
3411 dm_crtc_destroy_state(crtc, crtc->state);
3412
3413 state = kzalloc(sizeof(*state), GFP_KERNEL);
3414 if (WARN_ON(!state))
3415 return;
3416
3417 crtc->state = &state->base;
3418 crtc->state->crtc = crtc;
3419
3420}
3421
3422static struct drm_crtc_state *
3423dm_crtc_duplicate_state(struct drm_crtc *crtc)
3424{
3425 struct dm_crtc_state *state, *cur;
3426
3427 cur = to_dm_crtc_state(crtc->state);
3428
3429 if (WARN_ON(!crtc->state))
3430 return NULL;
3431
2004f45e 3432 state = kzalloc(sizeof(*state), GFP_KERNEL);
2a55f096
ES
3433 if (!state)
3434 return NULL;
e7b07cee
HW
3435
3436 __drm_atomic_helper_crtc_duplicate_state(crtc, &state->base);
3437
3438 if (cur->stream) {
3439 state->stream = cur->stream;
3440 dc_stream_retain(state->stream);
3441 }
3442
d6ef9b41
NK
3443 state->active_planes = cur->active_planes;
3444 state->interrupts_enabled = cur->interrupts_enabled;
180db303 3445 state->vrr_params = cur->vrr_params;
98e6436d 3446 state->vrr_infopacket = cur->vrr_infopacket;
c1ee92f9 3447 state->abm_level = cur->abm_level;
bb47de73
NK
3448 state->vrr_supported = cur->vrr_supported;
3449 state->freesync_config = cur->freesync_config;
ed20dc0d 3450 state->crc_enabled = cur->crc_enabled;
98e6436d 3451
e7b07cee
HW
3452 /* TODO Duplicate dc_stream after objects are stream object is flattened */
3453
3454 return &state->base;
3455}
3456
d2574c33
MK
3457static inline int dm_set_vupdate_irq(struct drm_crtc *crtc, bool enable)
3458{
3459 enum dc_irq_source irq_source;
3460 struct amdgpu_crtc *acrtc = to_amdgpu_crtc(crtc);
3461 struct amdgpu_device *adev = crtc->dev->dev_private;
3462 int rc;
3463
3464 irq_source = IRQ_TYPE_VUPDATE + acrtc->otg_inst;
3465
3466 rc = dc_interrupt_set(adev->dm.dc, irq_source, enable) ? 0 : -EBUSY;
3467
3468 DRM_DEBUG_DRIVER("crtc %d - vupdate irq %sabling: r=%d\n",
3469 acrtc->crtc_id, enable ? "en" : "dis", rc);
3470 return rc;
3471}
589d2739
HW
3472
3473static inline int dm_set_vblank(struct drm_crtc *crtc, bool enable)
3474{
3475 enum dc_irq_source irq_source;
3476 struct amdgpu_crtc *acrtc = to_amdgpu_crtc(crtc);
3477 struct amdgpu_device *adev = crtc->dev->dev_private;
d2574c33
MK
3478 struct dm_crtc_state *acrtc_state = to_dm_crtc_state(crtc->state);
3479 int rc = 0;
3480
3481 if (enable) {
3482 /* vblank irq on -> Only need vupdate irq in vrr mode */
3483 if (amdgpu_dm_vrr_active(acrtc_state))
3484 rc = dm_set_vupdate_irq(crtc, true);
3485 } else {
3486 /* vblank irq off -> vupdate irq off */
3487 rc = dm_set_vupdate_irq(crtc, false);
3488 }
3489
3490 if (rc)
3491 return rc;
589d2739
HW
3492
3493 irq_source = IRQ_TYPE_VBLANK + acrtc->otg_inst;
a0e30392 3494 return dc_interrupt_set(adev->dm.dc, irq_source, enable) ? 0 : -EBUSY;
589d2739
HW
3495}
3496
3497static int dm_enable_vblank(struct drm_crtc *crtc)
3498{
3499 return dm_set_vblank(crtc, true);
3500}
3501
3502static void dm_disable_vblank(struct drm_crtc *crtc)
3503{
3504 dm_set_vblank(crtc, false);
3505}
3506
e7b07cee
HW
3507/* Implemented only the options currently availible for the driver */
3508static const struct drm_crtc_funcs amdgpu_dm_crtc_funcs = {
3509 .reset = dm_crtc_reset_state,
3510 .destroy = amdgpu_dm_crtc_destroy,
3511 .gamma_set = drm_atomic_helper_legacy_gamma_set,
3512 .set_config = drm_atomic_helper_set_config,
3513 .page_flip = drm_atomic_helper_page_flip,
3514 .atomic_duplicate_state = dm_crtc_duplicate_state,
3515 .atomic_destroy_state = dm_crtc_destroy_state,
31aec354 3516 .set_crc_source = amdgpu_dm_crtc_set_crc_source,
3b3b8448 3517 .verify_crc_source = amdgpu_dm_crtc_verify_crc_source,
589d2739
HW
3518 .enable_vblank = dm_enable_vblank,
3519 .disable_vblank = dm_disable_vblank,
e7b07cee
HW
3520};
3521
3522static enum drm_connector_status
3523amdgpu_dm_connector_detect(struct drm_connector *connector, bool force)
3524{
3525 bool connected;
c84dec2f 3526 struct amdgpu_dm_connector *aconnector = to_amdgpu_dm_connector(connector);
e7b07cee 3527
1f6010a9
DF
3528 /*
3529 * Notes:
e7b07cee
HW
3530 * 1. This interface is NOT called in context of HPD irq.
3531 * 2. This interface *is called* in context of user-mode ioctl. Which
1f6010a9
DF
3532 * makes it a bad place for *any* MST-related activity.
3533 */
e7b07cee 3534
8580d60b
HW
3535 if (aconnector->base.force == DRM_FORCE_UNSPECIFIED &&
3536 !aconnector->fake_enable)
e7b07cee
HW
3537 connected = (aconnector->dc_sink != NULL);
3538 else
3539 connected = (aconnector->base.force == DRM_FORCE_ON);
3540
3541 return (connected ? connector_status_connected :
3542 connector_status_disconnected);
3543}
3544
3ee6b26b
AD
3545int amdgpu_dm_connector_atomic_set_property(struct drm_connector *connector,
3546 struct drm_connector_state *connector_state,
3547 struct drm_property *property,
3548 uint64_t val)
e7b07cee
HW
3549{
3550 struct drm_device *dev = connector->dev;
3551 struct amdgpu_device *adev = dev->dev_private;
3552 struct dm_connector_state *dm_old_state =
3553 to_dm_connector_state(connector->state);
3554 struct dm_connector_state *dm_new_state =
3555 to_dm_connector_state(connector_state);
3556
3557 int ret = -EINVAL;
3558
3559 if (property == dev->mode_config.scaling_mode_property) {
3560 enum amdgpu_rmx_type rmx_type;
3561
3562 switch (val) {
3563 case DRM_MODE_SCALE_CENTER:
3564 rmx_type = RMX_CENTER;
3565 break;
3566 case DRM_MODE_SCALE_ASPECT:
3567 rmx_type = RMX_ASPECT;
3568 break;
3569 case DRM_MODE_SCALE_FULLSCREEN:
3570 rmx_type = RMX_FULL;
3571 break;
3572 case DRM_MODE_SCALE_NONE:
3573 default:
3574 rmx_type = RMX_OFF;
3575 break;
3576 }
3577
3578 if (dm_old_state->scaling == rmx_type)
3579 return 0;
3580
3581 dm_new_state->scaling = rmx_type;
3582 ret = 0;
3583 } else if (property == adev->mode_info.underscan_hborder_property) {
3584 dm_new_state->underscan_hborder = val;
3585 ret = 0;
3586 } else if (property == adev->mode_info.underscan_vborder_property) {
3587 dm_new_state->underscan_vborder = val;
3588 ret = 0;
3589 } else if (property == adev->mode_info.underscan_property) {
3590 dm_new_state->underscan_enable = val;
3591 ret = 0;
07e3a1cf
NK
3592 } else if (property == adev->mode_info.max_bpc_property) {
3593 dm_new_state->max_bpc = val;
3594 ret = 0;
c1ee92f9
DF
3595 } else if (property == adev->mode_info.abm_level_property) {
3596 dm_new_state->abm_level = val;
3597 ret = 0;
e7b07cee
HW
3598 }
3599
3600 return ret;
3601}
3602
3ee6b26b
AD
3603int amdgpu_dm_connector_atomic_get_property(struct drm_connector *connector,
3604 const struct drm_connector_state *state,
3605 struct drm_property *property,
3606 uint64_t *val)
e7b07cee
HW
3607{
3608 struct drm_device *dev = connector->dev;
3609 struct amdgpu_device *adev = dev->dev_private;
3610 struct dm_connector_state *dm_state =
3611 to_dm_connector_state(state);
3612 int ret = -EINVAL;
3613
3614 if (property == dev->mode_config.scaling_mode_property) {
3615 switch (dm_state->scaling) {
3616 case RMX_CENTER:
3617 *val = DRM_MODE_SCALE_CENTER;
3618 break;
3619 case RMX_ASPECT:
3620 *val = DRM_MODE_SCALE_ASPECT;
3621 break;
3622 case RMX_FULL:
3623 *val = DRM_MODE_SCALE_FULLSCREEN;
3624 break;
3625 case RMX_OFF:
3626 default:
3627 *val = DRM_MODE_SCALE_NONE;
3628 break;
3629 }
3630 ret = 0;
3631 } else if (property == adev->mode_info.underscan_hborder_property) {
3632 *val = dm_state->underscan_hborder;
3633 ret = 0;
3634 } else if (property == adev->mode_info.underscan_vborder_property) {
3635 *val = dm_state->underscan_vborder;
3636 ret = 0;
3637 } else if (property == adev->mode_info.underscan_property) {
3638 *val = dm_state->underscan_enable;
3639 ret = 0;
07e3a1cf
NK
3640 } else if (property == adev->mode_info.max_bpc_property) {
3641 *val = dm_state->max_bpc;
3642 ret = 0;
c1ee92f9
DF
3643 } else if (property == adev->mode_info.abm_level_property) {
3644 *val = dm_state->abm_level;
3645 ret = 0;
e7b07cee 3646 }
c1ee92f9 3647
e7b07cee
HW
3648 return ret;
3649}
3650
7578ecda 3651static void amdgpu_dm_connector_destroy(struct drm_connector *connector)
e7b07cee 3652{
c84dec2f 3653 struct amdgpu_dm_connector *aconnector = to_amdgpu_dm_connector(connector);
e7b07cee
HW
3654 const struct dc_link *link = aconnector->dc_link;
3655 struct amdgpu_device *adev = connector->dev->dev_private;
3656 struct amdgpu_display_manager *dm = &adev->dm;
ada8ce15 3657
e7b07cee
HW
3658#if defined(CONFIG_BACKLIGHT_CLASS_DEVICE) ||\
3659 defined(CONFIG_BACKLIGHT_CLASS_DEVICE_MODULE)
3660
89fc8d4e 3661 if ((link->connector_signal & (SIGNAL_TYPE_EDP | SIGNAL_TYPE_LVDS)) &&
5cd29ed0
HW
3662 link->type != dc_connection_none &&
3663 dm->backlight_dev) {
3664 backlight_device_unregister(dm->backlight_dev);
3665 dm->backlight_dev = NULL;
e7b07cee
HW
3666 }
3667#endif
dcd5fb82
MF
3668
3669 if (aconnector->dc_em_sink)
3670 dc_sink_release(aconnector->dc_em_sink);
3671 aconnector->dc_em_sink = NULL;
3672 if (aconnector->dc_sink)
3673 dc_sink_release(aconnector->dc_sink);
3674 aconnector->dc_sink = NULL;
3675
e86e8947 3676 drm_dp_cec_unregister_connector(&aconnector->dm_dp_aux.aux);
e7b07cee
HW
3677 drm_connector_unregister(connector);
3678 drm_connector_cleanup(connector);
3679 kfree(connector);
3680}
3681
3682void amdgpu_dm_connector_funcs_reset(struct drm_connector *connector)
3683{
3684 struct dm_connector_state *state =
3685 to_dm_connector_state(connector->state);
3686
df099b9b
LSL
3687 if (connector->state)
3688 __drm_atomic_helper_connector_destroy_state(connector->state);
3689
e7b07cee
HW
3690 kfree(state);
3691
3692 state = kzalloc(sizeof(*state), GFP_KERNEL);
3693
3694 if (state) {
3695 state->scaling = RMX_OFF;
3696 state->underscan_enable = false;
3697 state->underscan_hborder = 0;
3698 state->underscan_vborder = 0;
49f1c44b 3699 state->max_bpc = 8;
e7b07cee 3700
df099b9b 3701 __drm_atomic_helper_connector_reset(connector, &state->base);
e7b07cee
HW
3702 }
3703}
3704
3ee6b26b
AD
3705struct drm_connector_state *
3706amdgpu_dm_connector_atomic_duplicate_state(struct drm_connector *connector)
e7b07cee
HW
3707{
3708 struct dm_connector_state *state =
3709 to_dm_connector_state(connector->state);
3710
3711 struct dm_connector_state *new_state =
3712 kmemdup(state, sizeof(*state), GFP_KERNEL);
3713
98e6436d
AK
3714 if (!new_state)
3715 return NULL;
e7b07cee 3716
98e6436d
AK
3717 __drm_atomic_helper_connector_duplicate_state(connector, &new_state->base);
3718
3719 new_state->freesync_capable = state->freesync_capable;
c1ee92f9 3720 new_state->abm_level = state->abm_level;
922454c2
NK
3721 new_state->scaling = state->scaling;
3722 new_state->underscan_enable = state->underscan_enable;
3723 new_state->underscan_hborder = state->underscan_hborder;
3724 new_state->underscan_vborder = state->underscan_vborder;
49f1c44b 3725 new_state->max_bpc = state->max_bpc;
98e6436d
AK
3726
3727 return &new_state->base;
e7b07cee
HW
3728}
3729
3730static const struct drm_connector_funcs amdgpu_dm_connector_funcs = {
3731 .reset = amdgpu_dm_connector_funcs_reset,
3732 .detect = amdgpu_dm_connector_detect,
3733 .fill_modes = drm_helper_probe_single_connector_modes,
3734 .destroy = amdgpu_dm_connector_destroy,
3735 .atomic_duplicate_state = amdgpu_dm_connector_atomic_duplicate_state,
3736 .atomic_destroy_state = drm_atomic_helper_connector_destroy_state,
3737 .atomic_set_property = amdgpu_dm_connector_atomic_set_property,
3738 .atomic_get_property = amdgpu_dm_connector_atomic_get_property
3739};
3740
e7b07cee
HW
3741static int get_modes(struct drm_connector *connector)
3742{
3743 return amdgpu_dm_connector_get_modes(connector);
3744}
3745
c84dec2f 3746static void create_eml_sink(struct amdgpu_dm_connector *aconnector)
e7b07cee
HW
3747{
3748 struct dc_sink_init_data init_params = {
3749 .link = aconnector->dc_link,
3750 .sink_signal = SIGNAL_TYPE_VIRTUAL
3751 };
70e8ffc5 3752 struct edid *edid;
e7b07cee 3753
a89ff457 3754 if (!aconnector->base.edid_blob_ptr) {
e7b07cee
HW
3755 DRM_ERROR("No EDID firmware found on connector: %s ,forcing to OFF!\n",
3756 aconnector->base.name);
3757
3758 aconnector->base.force = DRM_FORCE_OFF;
3759 aconnector->base.override_edid = false;
3760 return;
3761 }
3762
70e8ffc5
HW
3763 edid = (struct edid *) aconnector->base.edid_blob_ptr->data;
3764
e7b07cee
HW
3765 aconnector->edid = edid;
3766
3767 aconnector->dc_em_sink = dc_link_add_remote_sink(
3768 aconnector->dc_link,
3769 (uint8_t *)edid,
3770 (edid->extensions + 1) * EDID_LENGTH,
3771 &init_params);
3772
dcd5fb82 3773 if (aconnector->base.force == DRM_FORCE_ON) {
e7b07cee
HW
3774 aconnector->dc_sink = aconnector->dc_link->local_sink ?
3775 aconnector->dc_link->local_sink :
3776 aconnector->dc_em_sink;
dcd5fb82
MF
3777 dc_sink_retain(aconnector->dc_sink);
3778 }
e7b07cee
HW
3779}
3780
c84dec2f 3781static void handle_edid_mgmt(struct amdgpu_dm_connector *aconnector)
e7b07cee
HW
3782{
3783 struct dc_link *link = (struct dc_link *)aconnector->dc_link;
3784
1f6010a9
DF
3785 /*
3786 * In case of headless boot with force on for DP managed connector
e7b07cee
HW
3787 * Those settings have to be != 0 to get initial modeset
3788 */
3789 if (link->connector_signal == SIGNAL_TYPE_DISPLAY_PORT) {
3790 link->verified_link_cap.lane_count = LANE_COUNT_FOUR;
3791 link->verified_link_cap.link_rate = LINK_RATE_HIGH2;
3792 }
3793
3794
3795 aconnector->base.override_edid = true;
3796 create_eml_sink(aconnector);
3797}
3798
ba9ca088 3799enum drm_mode_status amdgpu_dm_connector_mode_valid(struct drm_connector *connector,
3ee6b26b 3800 struct drm_display_mode *mode)
e7b07cee
HW
3801{
3802 int result = MODE_ERROR;
3803 struct dc_sink *dc_sink;
3804 struct amdgpu_device *adev = connector->dev->dev_private;
3805 /* TODO: Unhardcode stream count */
0971c40e 3806 struct dc_stream_state *stream;
c84dec2f 3807 struct amdgpu_dm_connector *aconnector = to_amdgpu_dm_connector(connector);
a39438f0 3808 enum dc_status dc_result = DC_OK;
e7b07cee
HW
3809
3810 if ((mode->flags & DRM_MODE_FLAG_INTERLACE) ||
3811 (mode->flags & DRM_MODE_FLAG_DBLSCAN))
3812 return result;
3813
1f6010a9
DF
3814 /*
3815 * Only run this the first time mode_valid is called to initilialize
e7b07cee
HW
3816 * EDID mgmt
3817 */
3818 if (aconnector->base.force != DRM_FORCE_UNSPECIFIED &&
3819 !aconnector->dc_em_sink)
3820 handle_edid_mgmt(aconnector);
3821
c84dec2f 3822 dc_sink = to_amdgpu_dm_connector(connector)->dc_sink;
e7b07cee 3823
b830ebc9 3824 if (dc_sink == NULL) {
e7b07cee
HW
3825 DRM_ERROR("dc_sink is NULL!\n");
3826 goto fail;
3827 }
3828
b333730d 3829 stream = create_stream_for_sink(aconnector, mode, NULL, NULL);
b830ebc9 3830 if (stream == NULL) {
e7b07cee
HW
3831 DRM_ERROR("Failed to create stream for sink!\n");
3832 goto fail;
3833 }
3834
a39438f0
HW
3835 dc_result = dc_validate_stream(adev->dm.dc, stream);
3836
3837 if (dc_result == DC_OK)
e7b07cee 3838 result = MODE_OK;
a39438f0 3839 else
9f921b14 3840 DRM_DEBUG_KMS("Mode %dx%d (clk %d) failed DC validation with error %d\n",
a39438f0
HW
3841 mode->vdisplay,
3842 mode->hdisplay,
9f921b14
HW
3843 mode->clock,
3844 dc_result);
e7b07cee
HW
3845
3846 dc_stream_release(stream);
3847
3848fail:
3849 /* TODO: error handling*/
3850 return result;
3851}
3852
3853static const struct drm_connector_helper_funcs
3854amdgpu_dm_connector_helper_funcs = {
3855 /*
1f6010a9 3856 * If hotplugging a second bigger display in FB Con mode, bigger resolution
b830ebc9 3857 * modes will be filtered by drm_mode_validate_size(), and those modes
1f6010a9 3858 * are missing after user start lightdm. So we need to renew modes list.
b830ebc9
HW
3859 * in get_modes call back, not just return the modes count
3860 */
e7b07cee
HW
3861 .get_modes = get_modes,
3862 .mode_valid = amdgpu_dm_connector_mode_valid,
e7b07cee
HW
3863};
3864
3865static void dm_crtc_helper_disable(struct drm_crtc *crtc)
3866{
3867}
3868
bc92c065
NK
3869static bool does_crtc_have_active_cursor(struct drm_crtc_state *new_crtc_state)
3870{
3871 struct drm_device *dev = new_crtc_state->crtc->dev;
3872 struct drm_plane *plane;
3873
3874 drm_for_each_plane_mask(plane, dev, new_crtc_state->plane_mask) {
3875 if (plane->type == DRM_PLANE_TYPE_CURSOR)
3876 return true;
3877 }
3878
3879 return false;
3880}
3881
d6ef9b41 3882static int count_crtc_active_planes(struct drm_crtc_state *new_crtc_state)
c14a005c
NK
3883{
3884 struct drm_atomic_state *state = new_crtc_state->state;
3885 struct drm_plane *plane;
3886 int num_active = 0;
3887
3888 drm_for_each_plane_mask(plane, state->dev, new_crtc_state->plane_mask) {
3889 struct drm_plane_state *new_plane_state;
3890
3891 /* Cursor planes are "fake". */
3892 if (plane->type == DRM_PLANE_TYPE_CURSOR)
3893 continue;
3894
3895 new_plane_state = drm_atomic_get_new_plane_state(state, plane);
3896
3897 if (!new_plane_state) {
3898 /*
3899 * The plane is enable on the CRTC and hasn't changed
3900 * state. This means that it previously passed
3901 * validation and is therefore enabled.
3902 */
3903 num_active += 1;
3904 continue;
3905 }
3906
3907 /* We need a framebuffer to be considered enabled. */
3908 num_active += (new_plane_state->fb != NULL);
3909 }
3910
d6ef9b41
NK
3911 return num_active;
3912}
3913
3914/*
3915 * Sets whether interrupts should be enabled on a specific CRTC.
3916 * We require that the stream be enabled and that there exist active
3917 * DC planes on the stream.
3918 */
3919static void
3920dm_update_crtc_interrupt_state(struct drm_crtc *crtc,
3921 struct drm_crtc_state *new_crtc_state)
3922{
3923 struct dm_crtc_state *dm_new_crtc_state =
3924 to_dm_crtc_state(new_crtc_state);
3925
3926 dm_new_crtc_state->active_planes = 0;
3927 dm_new_crtc_state->interrupts_enabled = false;
3928
3929 if (!dm_new_crtc_state->stream)
3930 return;
3931
3932 dm_new_crtc_state->active_planes =
3933 count_crtc_active_planes(new_crtc_state);
3934
3935 dm_new_crtc_state->interrupts_enabled =
3936 dm_new_crtc_state->active_planes > 0;
c14a005c
NK
3937}
3938
3ee6b26b
AD
3939static int dm_crtc_helper_atomic_check(struct drm_crtc *crtc,
3940 struct drm_crtc_state *state)
e7b07cee
HW
3941{
3942 struct amdgpu_device *adev = crtc->dev->dev_private;
3943 struct dc *dc = adev->dm.dc;
3944 struct dm_crtc_state *dm_crtc_state = to_dm_crtc_state(state);
3945 int ret = -EINVAL;
3946
d6ef9b41
NK
3947 /*
3948 * Update interrupt state for the CRTC. This needs to happen whenever
3949 * the CRTC has changed or whenever any of its planes have changed.
3950 * Atomic check satisfies both of these requirements since the CRTC
3951 * is added to the state by DRM during drm_atomic_helper_check_planes.
3952 */
3953 dm_update_crtc_interrupt_state(crtc, state);
3954
9b690ef3
BL
3955 if (unlikely(!dm_crtc_state->stream &&
3956 modeset_required(state, NULL, dm_crtc_state->stream))) {
e7b07cee
HW
3957 WARN_ON(1);
3958 return ret;
3959 }
3960
1f6010a9 3961 /* In some use cases, like reset, no stream is attached */
e7b07cee
HW
3962 if (!dm_crtc_state->stream)
3963 return 0;
3964
bc92c065
NK
3965 /*
3966 * We want at least one hardware plane enabled to use
3967 * the stream with a cursor enabled.
3968 */
c14a005c 3969 if (state->enable && state->active &&
bc92c065 3970 does_crtc_have_active_cursor(state) &&
d6ef9b41 3971 dm_crtc_state->active_planes == 0)
c14a005c
NK
3972 return -EINVAL;
3973
62c933f9 3974 if (dc_validate_stream(dc, dm_crtc_state->stream) == DC_OK)
e7b07cee
HW
3975 return 0;
3976
3977 return ret;
3978}
3979
3ee6b26b
AD
3980static bool dm_crtc_helper_mode_fixup(struct drm_crtc *crtc,
3981 const struct drm_display_mode *mode,
3982 struct drm_display_mode *adjusted_mode)
e7b07cee
HW
3983{
3984 return true;
3985}
3986
3987static const struct drm_crtc_helper_funcs amdgpu_dm_crtc_helper_funcs = {
3988 .disable = dm_crtc_helper_disable,
3989 .atomic_check = dm_crtc_helper_atomic_check,
3990 .mode_fixup = dm_crtc_helper_mode_fixup
3991};
3992
3993static void dm_encoder_helper_disable(struct drm_encoder *encoder)
3994{
3995
3996}
3997
3ee6b26b
AD
3998static int dm_encoder_helper_atomic_check(struct drm_encoder *encoder,
3999 struct drm_crtc_state *crtc_state,
4000 struct drm_connector_state *conn_state)
e7b07cee
HW
4001{
4002 return 0;
4003}
4004
4005const struct drm_encoder_helper_funcs amdgpu_dm_encoder_helper_funcs = {
4006 .disable = dm_encoder_helper_disable,
4007 .atomic_check = dm_encoder_helper_atomic_check
4008};
4009
4010static void dm_drm_plane_reset(struct drm_plane *plane)
4011{
4012 struct dm_plane_state *amdgpu_state = NULL;
4013
4014 if (plane->state)
4015 plane->funcs->atomic_destroy_state(plane, plane->state);
4016
4017 amdgpu_state = kzalloc(sizeof(*amdgpu_state), GFP_KERNEL);
f922237d 4018 WARN_ON(amdgpu_state == NULL);
1f6010a9 4019
7ddaef96
NK
4020 if (amdgpu_state)
4021 __drm_atomic_helper_plane_reset(plane, &amdgpu_state->base);
e7b07cee
HW
4022}
4023
4024static struct drm_plane_state *
4025dm_drm_plane_duplicate_state(struct drm_plane *plane)
4026{
4027 struct dm_plane_state *dm_plane_state, *old_dm_plane_state;
4028
4029 old_dm_plane_state = to_dm_plane_state(plane->state);
4030 dm_plane_state = kzalloc(sizeof(*dm_plane_state), GFP_KERNEL);
4031 if (!dm_plane_state)
4032 return NULL;
4033
4034 __drm_atomic_helper_plane_duplicate_state(plane, &dm_plane_state->base);
4035
3be5262e
HW
4036 if (old_dm_plane_state->dc_state) {
4037 dm_plane_state->dc_state = old_dm_plane_state->dc_state;
4038 dc_plane_state_retain(dm_plane_state->dc_state);
e7b07cee
HW
4039 }
4040
4041 return &dm_plane_state->base;
4042}
4043
4044void dm_drm_plane_destroy_state(struct drm_plane *plane,
3ee6b26b 4045 struct drm_plane_state *state)
e7b07cee
HW
4046{
4047 struct dm_plane_state *dm_plane_state = to_dm_plane_state(state);
4048
3be5262e
HW
4049 if (dm_plane_state->dc_state)
4050 dc_plane_state_release(dm_plane_state->dc_state);
e7b07cee 4051
0627bbd3 4052 drm_atomic_helper_plane_destroy_state(plane, state);
e7b07cee
HW
4053}
4054
4055static const struct drm_plane_funcs dm_plane_funcs = {
4056 .update_plane = drm_atomic_helper_update_plane,
4057 .disable_plane = drm_atomic_helper_disable_plane,
02680efb 4058 .destroy = drm_primary_helper_destroy,
e7b07cee
HW
4059 .reset = dm_drm_plane_reset,
4060 .atomic_duplicate_state = dm_drm_plane_duplicate_state,
4061 .atomic_destroy_state = dm_drm_plane_destroy_state,
4062};
4063
3ee6b26b
AD
4064static int dm_plane_helper_prepare_fb(struct drm_plane *plane,
4065 struct drm_plane_state *new_state)
e7b07cee
HW
4066{
4067 struct amdgpu_framebuffer *afb;
4068 struct drm_gem_object *obj;
5d43be0c 4069 struct amdgpu_device *adev;
e7b07cee 4070 struct amdgpu_bo *rbo;
e7b07cee 4071 struct dm_plane_state *dm_plane_state_new, *dm_plane_state_old;
e0634e8d 4072 uint64_t tiling_flags;
5d43be0c
CK
4073 uint32_t domain;
4074 int r;
e7b07cee
HW
4075
4076 dm_plane_state_old = to_dm_plane_state(plane->state);
4077 dm_plane_state_new = to_dm_plane_state(new_state);
4078
4079 if (!new_state->fb) {
f1ad2f5e 4080 DRM_DEBUG_DRIVER("No FB bound\n");
e7b07cee
HW
4081 return 0;
4082 }
4083
4084 afb = to_amdgpu_framebuffer(new_state->fb);
e68d14dd 4085 obj = new_state->fb->obj[0];
e7b07cee 4086 rbo = gem_to_amdgpu_bo(obj);
5d43be0c 4087 adev = amdgpu_ttm_adev(rbo->tbo.bdev);
e7b07cee
HW
4088 r = amdgpu_bo_reserve(rbo, false);
4089 if (unlikely(r != 0))
4090 return r;
4091
5d43be0c 4092 if (plane->type != DRM_PLANE_TYPE_CURSOR)
1d2361e5 4093 domain = amdgpu_display_supported_domains(adev);
5d43be0c
CK
4094 else
4095 domain = AMDGPU_GEM_DOMAIN_VRAM;
e7b07cee 4096
7b7c6c81 4097 r = amdgpu_bo_pin(rbo, domain);
e7b07cee 4098 if (unlikely(r != 0)) {
30b7c614
HW
4099 if (r != -ERESTARTSYS)
4100 DRM_ERROR("Failed to pin framebuffer with error %d\n", r);
bb812f1e 4101 amdgpu_bo_unreserve(rbo);
e7b07cee
HW
4102 return r;
4103 }
4104
bb812f1e
JZ
4105 r = amdgpu_ttm_alloc_gart(&rbo->tbo);
4106 if (unlikely(r != 0)) {
4107 amdgpu_bo_unpin(rbo);
4108 amdgpu_bo_unreserve(rbo);
4109 DRM_ERROR("%p bind failed\n", rbo);
e7b07cee
HW
4110 return r;
4111 }
7df7e505
NK
4112
4113 amdgpu_bo_get_tiling_flags(rbo, &tiling_flags);
4114
bb812f1e
JZ
4115 amdgpu_bo_unreserve(rbo);
4116
7b7c6c81 4117 afb->address = amdgpu_bo_gpu_offset(rbo);
e7b07cee
HW
4118
4119 amdgpu_bo_ref(rbo);
4120
3be5262e
HW
4121 if (dm_plane_state_new->dc_state &&
4122 dm_plane_state_old->dc_state != dm_plane_state_new->dc_state) {
4123 struct dc_plane_state *plane_state = dm_plane_state_new->dc_state;
e7b07cee 4124
320932bf 4125 fill_plane_buffer_attributes(
695af5f9
NK
4126 adev, afb, plane_state->format, plane_state->rotation,
4127 tiling_flags, &plane_state->tiling_info,
320932bf 4128 &plane_state->plane_size, &plane_state->dcc,
695af5f9 4129 &plane_state->address);
e7b07cee
HW
4130 }
4131
e7b07cee
HW
4132 return 0;
4133}
4134
3ee6b26b
AD
4135static void dm_plane_helper_cleanup_fb(struct drm_plane *plane,
4136 struct drm_plane_state *old_state)
e7b07cee
HW
4137{
4138 struct amdgpu_bo *rbo;
e7b07cee
HW
4139 int r;
4140
4141 if (!old_state->fb)
4142 return;
4143
e68d14dd 4144 rbo = gem_to_amdgpu_bo(old_state->fb->obj[0]);
e7b07cee
HW
4145 r = amdgpu_bo_reserve(rbo, false);
4146 if (unlikely(r)) {
4147 DRM_ERROR("failed to reserve rbo before unpin\n");
4148 return;
b830ebc9
HW
4149 }
4150
4151 amdgpu_bo_unpin(rbo);
4152 amdgpu_bo_unreserve(rbo);
4153 amdgpu_bo_unref(&rbo);
e7b07cee
HW
4154}
4155
7578ecda
AD
4156static int dm_plane_atomic_check(struct drm_plane *plane,
4157 struct drm_plane_state *state)
cbd19488
AG
4158{
4159 struct amdgpu_device *adev = plane->dev->dev_private;
4160 struct dc *dc = adev->dm.dc;
78171832 4161 struct dm_plane_state *dm_plane_state;
695af5f9
NK
4162 struct dc_scaling_info scaling_info;
4163 int ret;
78171832
NK
4164
4165 dm_plane_state = to_dm_plane_state(state);
cbd19488 4166
3be5262e 4167 if (!dm_plane_state->dc_state)
9a3329b1 4168 return 0;
cbd19488 4169
695af5f9
NK
4170 ret = fill_dc_scaling_info(state, &scaling_info);
4171 if (ret)
4172 return ret;
a05bcff1 4173
62c933f9 4174 if (dc_validate_plane(dc, dm_plane_state->dc_state) == DC_OK)
cbd19488
AG
4175 return 0;
4176
4177 return -EINVAL;
4178}
4179
674e78ac
NK
4180static int dm_plane_atomic_async_check(struct drm_plane *plane,
4181 struct drm_plane_state *new_plane_state)
4182{
77acd1cd
NK
4183 struct drm_plane_state *old_plane_state =
4184 drm_atomic_get_old_plane_state(new_plane_state->state, plane);
4185
674e78ac
NK
4186 /* Only support async updates on cursor planes. */
4187 if (plane->type != DRM_PLANE_TYPE_CURSOR)
4188 return -EINVAL;
4189
77acd1cd
NK
4190 /*
4191 * DRM calls prepare_fb and cleanup_fb on new_plane_state for
4192 * async commits so don't allow fb changes.
4193 */
4194 if (old_plane_state->fb != new_plane_state->fb)
4195 return -EINVAL;
4196
674e78ac
NK
4197 return 0;
4198}
4199
4200static void dm_plane_atomic_async_update(struct drm_plane *plane,
4201 struct drm_plane_state *new_state)
4202{
4203 struct drm_plane_state *old_state =
4204 drm_atomic_get_old_plane_state(new_state->state, plane);
4205
4206 if (plane->state->fb != new_state->fb)
4207 drm_atomic_set_fb_for_plane(plane->state, new_state->fb);
4208
4209 plane->state->src_x = new_state->src_x;
4210 plane->state->src_y = new_state->src_y;
4211 plane->state->src_w = new_state->src_w;
4212 plane->state->src_h = new_state->src_h;
4213 plane->state->crtc_x = new_state->crtc_x;
4214 plane->state->crtc_y = new_state->crtc_y;
4215 plane->state->crtc_w = new_state->crtc_w;
4216 plane->state->crtc_h = new_state->crtc_h;
4217
4218 handle_cursor_update(plane, old_state);
4219}
4220
e7b07cee
HW
4221static const struct drm_plane_helper_funcs dm_plane_helper_funcs = {
4222 .prepare_fb = dm_plane_helper_prepare_fb,
4223 .cleanup_fb = dm_plane_helper_cleanup_fb,
cbd19488 4224 .atomic_check = dm_plane_atomic_check,
674e78ac
NK
4225 .atomic_async_check = dm_plane_atomic_async_check,
4226 .atomic_async_update = dm_plane_atomic_async_update
e7b07cee
HW
4227};
4228
4229/*
4230 * TODO: these are currently initialized to rgb formats only.
4231 * For future use cases we should either initialize them dynamically based on
4232 * plane capabilities, or initialize this array to all formats, so internal drm
1f6010a9 4233 * check will succeed, and let DC implement proper check
e7b07cee 4234 */
d90371b0 4235static const uint32_t rgb_formats[] = {
e7b07cee
HW
4236 DRM_FORMAT_XRGB8888,
4237 DRM_FORMAT_ARGB8888,
4238 DRM_FORMAT_RGBA8888,
4239 DRM_FORMAT_XRGB2101010,
4240 DRM_FORMAT_XBGR2101010,
4241 DRM_FORMAT_ARGB2101010,
4242 DRM_FORMAT_ABGR2101010,
bcd47f60
MR
4243 DRM_FORMAT_XBGR8888,
4244 DRM_FORMAT_ABGR8888,
46dd9ff7 4245 DRM_FORMAT_RGB565,
e7b07cee
HW
4246};
4247
0d579c7e
NK
4248static const uint32_t overlay_formats[] = {
4249 DRM_FORMAT_XRGB8888,
4250 DRM_FORMAT_ARGB8888,
4251 DRM_FORMAT_RGBA8888,
4252 DRM_FORMAT_XBGR8888,
4253 DRM_FORMAT_ABGR8888,
7267a1a9 4254 DRM_FORMAT_RGB565
e7b07cee
HW
4255};
4256
4257static const u32 cursor_formats[] = {
4258 DRM_FORMAT_ARGB8888
4259};
4260
37c6a93b
NK
4261static int get_plane_formats(const struct drm_plane *plane,
4262 const struct dc_plane_cap *plane_cap,
4263 uint32_t *formats, int max_formats)
e7b07cee 4264{
37c6a93b
NK
4265 int i, num_formats = 0;
4266
4267 /*
4268 * TODO: Query support for each group of formats directly from
4269 * DC plane caps. This will require adding more formats to the
4270 * caps list.
4271 */
e7b07cee 4272
f180b4bc 4273 switch (plane->type) {
e7b07cee 4274 case DRM_PLANE_TYPE_PRIMARY:
37c6a93b
NK
4275 for (i = 0; i < ARRAY_SIZE(rgb_formats); ++i) {
4276 if (num_formats >= max_formats)
4277 break;
4278
4279 formats[num_formats++] = rgb_formats[i];
4280 }
4281
ea36ad34 4282 if (plane_cap && plane_cap->pixel_format_support.nv12)
37c6a93b 4283 formats[num_formats++] = DRM_FORMAT_NV12;
e7b07cee 4284 break;
37c6a93b 4285
e7b07cee 4286 case DRM_PLANE_TYPE_OVERLAY:
37c6a93b
NK
4287 for (i = 0; i < ARRAY_SIZE(overlay_formats); ++i) {
4288 if (num_formats >= max_formats)
4289 break;
4290
4291 formats[num_formats++] = overlay_formats[i];
4292 }
e7b07cee 4293 break;
37c6a93b 4294
e7b07cee 4295 case DRM_PLANE_TYPE_CURSOR:
37c6a93b
NK
4296 for (i = 0; i < ARRAY_SIZE(cursor_formats); ++i) {
4297 if (num_formats >= max_formats)
4298 break;
4299
4300 formats[num_formats++] = cursor_formats[i];
4301 }
e7b07cee
HW
4302 break;
4303 }
4304
37c6a93b
NK
4305 return num_formats;
4306}
4307
4308static int amdgpu_dm_plane_init(struct amdgpu_display_manager *dm,
4309 struct drm_plane *plane,
4310 unsigned long possible_crtcs,
4311 const struct dc_plane_cap *plane_cap)
4312{
4313 uint32_t formats[32];
4314 int num_formats;
4315 int res = -EPERM;
4316
4317 num_formats = get_plane_formats(plane, plane_cap, formats,
4318 ARRAY_SIZE(formats));
4319
4320 res = drm_universal_plane_init(dm->adev->ddev, plane, possible_crtcs,
4321 &dm_plane_funcs, formats, num_formats,
4322 NULL, plane->type, NULL);
4323 if (res)
4324 return res;
4325
cc1fec57
NK
4326 if (plane->type == DRM_PLANE_TYPE_OVERLAY &&
4327 plane_cap && plane_cap->per_pixel_alpha) {
d74004b6
NK
4328 unsigned int blend_caps = BIT(DRM_MODE_BLEND_PIXEL_NONE) |
4329 BIT(DRM_MODE_BLEND_PREMULTI);
4330
4331 drm_plane_create_alpha_property(plane);
4332 drm_plane_create_blend_mode_property(plane, blend_caps);
4333 }
4334
fc8e5230 4335 if (plane->type == DRM_PLANE_TYPE_PRIMARY &&
ea36ad34 4336 plane_cap && plane_cap->pixel_format_support.nv12) {
fc8e5230
NK
4337 /* This only affects YUV formats. */
4338 drm_plane_create_color_properties(
4339 plane,
4340 BIT(DRM_COLOR_YCBCR_BT601) |
4341 BIT(DRM_COLOR_YCBCR_BT709),
4342 BIT(DRM_COLOR_YCBCR_LIMITED_RANGE) |
4343 BIT(DRM_COLOR_YCBCR_FULL_RANGE),
4344 DRM_COLOR_YCBCR_BT709, DRM_COLOR_YCBCR_LIMITED_RANGE);
4345 }
4346
f180b4bc 4347 drm_plane_helper_add(plane, &dm_plane_helper_funcs);
e7b07cee 4348
96719c54 4349 /* Create (reset) the plane state */
f180b4bc
HW
4350 if (plane->funcs->reset)
4351 plane->funcs->reset(plane);
96719c54 4352
37c6a93b 4353 return 0;
e7b07cee
HW
4354}
4355
7578ecda
AD
4356static int amdgpu_dm_crtc_init(struct amdgpu_display_manager *dm,
4357 struct drm_plane *plane,
4358 uint32_t crtc_index)
e7b07cee
HW
4359{
4360 struct amdgpu_crtc *acrtc = NULL;
f180b4bc 4361 struct drm_plane *cursor_plane;
e7b07cee
HW
4362
4363 int res = -ENOMEM;
4364
4365 cursor_plane = kzalloc(sizeof(*cursor_plane), GFP_KERNEL);
4366 if (!cursor_plane)
4367 goto fail;
4368
f180b4bc 4369 cursor_plane->type = DRM_PLANE_TYPE_CURSOR;
cc1fec57 4370 res = amdgpu_dm_plane_init(dm, cursor_plane, 0, NULL);
e7b07cee
HW
4371
4372 acrtc = kzalloc(sizeof(struct amdgpu_crtc), GFP_KERNEL);
4373 if (!acrtc)
4374 goto fail;
4375
4376 res = drm_crtc_init_with_planes(
4377 dm->ddev,
4378 &acrtc->base,
4379 plane,
f180b4bc 4380 cursor_plane,
e7b07cee
HW
4381 &amdgpu_dm_crtc_funcs, NULL);
4382
4383 if (res)
4384 goto fail;
4385
4386 drm_crtc_helper_add(&acrtc->base, &amdgpu_dm_crtc_helper_funcs);
4387
96719c54
HW
4388 /* Create (reset) the plane state */
4389 if (acrtc->base.funcs->reset)
4390 acrtc->base.funcs->reset(&acrtc->base);
4391
e7b07cee
HW
4392 acrtc->max_cursor_width = dm->adev->dm.dc->caps.max_cursor_size;
4393 acrtc->max_cursor_height = dm->adev->dm.dc->caps.max_cursor_size;
4394
4395 acrtc->crtc_id = crtc_index;
4396 acrtc->base.enabled = false;
c37e2d29 4397 acrtc->otg_inst = -1;
e7b07cee
HW
4398
4399 dm->adev->mode_info.crtcs[crtc_index] = acrtc;
236d0e4f
LSL
4400 drm_crtc_enable_color_mgmt(&acrtc->base, MAX_COLOR_LUT_ENTRIES,
4401 true, MAX_COLOR_LUT_ENTRIES);
086247a4 4402 drm_mode_crtc_set_gamma_size(&acrtc->base, MAX_COLOR_LEGACY_LUT_ENTRIES);
e7b07cee
HW
4403
4404 return 0;
4405
4406fail:
b830ebc9
HW
4407 kfree(acrtc);
4408 kfree(cursor_plane);
e7b07cee
HW
4409 return res;
4410}
4411
4412
4413static int to_drm_connector_type(enum signal_type st)
4414{
4415 switch (st) {
4416 case SIGNAL_TYPE_HDMI_TYPE_A:
4417 return DRM_MODE_CONNECTOR_HDMIA;
4418 case SIGNAL_TYPE_EDP:
4419 return DRM_MODE_CONNECTOR_eDP;
11c3ee48
AD
4420 case SIGNAL_TYPE_LVDS:
4421 return DRM_MODE_CONNECTOR_LVDS;
e7b07cee
HW
4422 case SIGNAL_TYPE_RGB:
4423 return DRM_MODE_CONNECTOR_VGA;
4424 case SIGNAL_TYPE_DISPLAY_PORT:
4425 case SIGNAL_TYPE_DISPLAY_PORT_MST:
4426 return DRM_MODE_CONNECTOR_DisplayPort;
4427 case SIGNAL_TYPE_DVI_DUAL_LINK:
4428 case SIGNAL_TYPE_DVI_SINGLE_LINK:
4429 return DRM_MODE_CONNECTOR_DVID;
4430 case SIGNAL_TYPE_VIRTUAL:
4431 return DRM_MODE_CONNECTOR_VIRTUAL;
4432
4433 default:
4434 return DRM_MODE_CONNECTOR_Unknown;
4435 }
4436}
4437
2b4c1c05
DV
4438static struct drm_encoder *amdgpu_dm_connector_to_encoder(struct drm_connector *connector)
4439{
4440 return drm_encoder_find(connector->dev, NULL, connector->encoder_ids[0]);
4441}
4442
e7b07cee
HW
4443static void amdgpu_dm_get_native_mode(struct drm_connector *connector)
4444{
e7b07cee
HW
4445 struct drm_encoder *encoder;
4446 struct amdgpu_encoder *amdgpu_encoder;
4447
2b4c1c05 4448 encoder = amdgpu_dm_connector_to_encoder(connector);
e7b07cee
HW
4449
4450 if (encoder == NULL)
4451 return;
4452
4453 amdgpu_encoder = to_amdgpu_encoder(encoder);
4454
4455 amdgpu_encoder->native_mode.clock = 0;
4456
4457 if (!list_empty(&connector->probed_modes)) {
4458 struct drm_display_mode *preferred_mode = NULL;
b830ebc9 4459
e7b07cee 4460 list_for_each_entry(preferred_mode,
b830ebc9
HW
4461 &connector->probed_modes,
4462 head) {
4463 if (preferred_mode->type & DRM_MODE_TYPE_PREFERRED)
4464 amdgpu_encoder->native_mode = *preferred_mode;
4465
e7b07cee
HW
4466 break;
4467 }
4468
4469 }
4470}
4471
3ee6b26b
AD
4472static struct drm_display_mode *
4473amdgpu_dm_create_common_mode(struct drm_encoder *encoder,
4474 char *name,
4475 int hdisplay, int vdisplay)
e7b07cee
HW
4476{
4477 struct drm_device *dev = encoder->dev;
4478 struct amdgpu_encoder *amdgpu_encoder = to_amdgpu_encoder(encoder);
4479 struct drm_display_mode *mode = NULL;
4480 struct drm_display_mode *native_mode = &amdgpu_encoder->native_mode;
4481
4482 mode = drm_mode_duplicate(dev, native_mode);
4483
b830ebc9 4484 if (mode == NULL)
e7b07cee
HW
4485 return NULL;
4486
4487 mode->hdisplay = hdisplay;
4488 mode->vdisplay = vdisplay;
4489 mode->type &= ~DRM_MODE_TYPE_PREFERRED;
090afc1e 4490 strscpy(mode->name, name, DRM_DISPLAY_MODE_LEN);
e7b07cee
HW
4491
4492 return mode;
4493
4494}
4495
4496static void amdgpu_dm_connector_add_common_modes(struct drm_encoder *encoder,
3ee6b26b 4497 struct drm_connector *connector)
e7b07cee
HW
4498{
4499 struct amdgpu_encoder *amdgpu_encoder = to_amdgpu_encoder(encoder);
4500 struct drm_display_mode *mode = NULL;
4501 struct drm_display_mode *native_mode = &amdgpu_encoder->native_mode;
c84dec2f
HW
4502 struct amdgpu_dm_connector *amdgpu_dm_connector =
4503 to_amdgpu_dm_connector(connector);
e7b07cee
HW
4504 int i;
4505 int n;
4506 struct mode_size {
4507 char name[DRM_DISPLAY_MODE_LEN];
4508 int w;
4509 int h;
b830ebc9 4510 } common_modes[] = {
e7b07cee
HW
4511 { "640x480", 640, 480},
4512 { "800x600", 800, 600},
4513 { "1024x768", 1024, 768},
4514 { "1280x720", 1280, 720},
4515 { "1280x800", 1280, 800},
4516 {"1280x1024", 1280, 1024},
4517 { "1440x900", 1440, 900},
4518 {"1680x1050", 1680, 1050},
4519 {"1600x1200", 1600, 1200},
4520 {"1920x1080", 1920, 1080},
4521 {"1920x1200", 1920, 1200}
4522 };
4523
b830ebc9 4524 n = ARRAY_SIZE(common_modes);
e7b07cee
HW
4525
4526 for (i = 0; i < n; i++) {
4527 struct drm_display_mode *curmode = NULL;
4528 bool mode_existed = false;
4529
4530 if (common_modes[i].w > native_mode->hdisplay ||
b830ebc9
HW
4531 common_modes[i].h > native_mode->vdisplay ||
4532 (common_modes[i].w == native_mode->hdisplay &&
4533 common_modes[i].h == native_mode->vdisplay))
4534 continue;
e7b07cee
HW
4535
4536 list_for_each_entry(curmode, &connector->probed_modes, head) {
4537 if (common_modes[i].w == curmode->hdisplay &&
b830ebc9 4538 common_modes[i].h == curmode->vdisplay) {
e7b07cee
HW
4539 mode_existed = true;
4540 break;
4541 }
4542 }
4543
4544 if (mode_existed)
4545 continue;
4546
4547 mode = amdgpu_dm_create_common_mode(encoder,
4548 common_modes[i].name, common_modes[i].w,
4549 common_modes[i].h);
4550 drm_mode_probed_add(connector, mode);
c84dec2f 4551 amdgpu_dm_connector->num_modes++;
e7b07cee
HW
4552 }
4553}
4554
3ee6b26b
AD
4555static void amdgpu_dm_connector_ddc_get_modes(struct drm_connector *connector,
4556 struct edid *edid)
e7b07cee 4557{
c84dec2f
HW
4558 struct amdgpu_dm_connector *amdgpu_dm_connector =
4559 to_amdgpu_dm_connector(connector);
e7b07cee
HW
4560
4561 if (edid) {
4562 /* empty probed_modes */
4563 INIT_LIST_HEAD(&connector->probed_modes);
c84dec2f 4564 amdgpu_dm_connector->num_modes =
e7b07cee
HW
4565 drm_add_edid_modes(connector, edid);
4566
e7b07cee 4567 amdgpu_dm_get_native_mode(connector);
a8d8d3dc 4568 } else {
c84dec2f 4569 amdgpu_dm_connector->num_modes = 0;
a8d8d3dc 4570 }
e7b07cee
HW
4571}
4572
7578ecda 4573static int amdgpu_dm_connector_get_modes(struct drm_connector *connector)
e7b07cee 4574{
c84dec2f
HW
4575 struct amdgpu_dm_connector *amdgpu_dm_connector =
4576 to_amdgpu_dm_connector(connector);
e7b07cee 4577 struct drm_encoder *encoder;
c84dec2f 4578 struct edid *edid = amdgpu_dm_connector->edid;
e7b07cee 4579
2b4c1c05 4580 encoder = amdgpu_dm_connector_to_encoder(connector);
3e332d3a 4581
85ee15d6 4582 if (!edid || !drm_edid_is_valid(edid)) {
1b369d3c
ML
4583 amdgpu_dm_connector->num_modes =
4584 drm_add_modes_noedid(connector, 640, 480);
85ee15d6
ML
4585 } else {
4586 amdgpu_dm_connector_ddc_get_modes(connector, edid);
4587 amdgpu_dm_connector_add_common_modes(encoder, connector);
4588 }
3e332d3a 4589 amdgpu_dm_fbc_init(connector);
5099114b 4590
c84dec2f 4591 return amdgpu_dm_connector->num_modes;
e7b07cee
HW
4592}
4593
3ee6b26b
AD
4594void amdgpu_dm_connector_init_helper(struct amdgpu_display_manager *dm,
4595 struct amdgpu_dm_connector *aconnector,
4596 int connector_type,
4597 struct dc_link *link,
4598 int link_index)
e7b07cee
HW
4599{
4600 struct amdgpu_device *adev = dm->ddev->dev_private;
4601
4602 aconnector->connector_id = link_index;
4603 aconnector->dc_link = link;
4604 aconnector->base.interlace_allowed = false;
4605 aconnector->base.doublescan_allowed = false;
4606 aconnector->base.stereo_allowed = false;
4607 aconnector->base.dpms = DRM_MODE_DPMS_OFF;
4608 aconnector->hpd.hpd = AMDGPU_HPD_NONE; /* not used */
e7b07cee
HW
4609 mutex_init(&aconnector->hpd_lock);
4610
1f6010a9
DF
4611 /*
4612 * configure support HPD hot plug connector_>polled default value is 0
b830ebc9
HW
4613 * which means HPD hot plug not supported
4614 */
e7b07cee
HW
4615 switch (connector_type) {
4616 case DRM_MODE_CONNECTOR_HDMIA:
4617 aconnector->base.polled = DRM_CONNECTOR_POLL_HPD;
e7baae1c 4618 aconnector->base.ycbcr_420_allowed =
9ea59d5a 4619 link->link_enc->features.hdmi_ycbcr420_supported ? true : false;
e7b07cee
HW
4620 break;
4621 case DRM_MODE_CONNECTOR_DisplayPort:
4622 aconnector->base.polled = DRM_CONNECTOR_POLL_HPD;
e7baae1c 4623 aconnector->base.ycbcr_420_allowed =
9ea59d5a 4624 link->link_enc->features.dp_ycbcr420_supported ? true : false;
e7b07cee
HW
4625 break;
4626 case DRM_MODE_CONNECTOR_DVID:
4627 aconnector->base.polled = DRM_CONNECTOR_POLL_HPD;
4628 break;
4629 default:
4630 break;
4631 }
4632
4633 drm_object_attach_property(&aconnector->base.base,
4634 dm->ddev->mode_config.scaling_mode_property,
4635 DRM_MODE_SCALE_NONE);
4636
4637 drm_object_attach_property(&aconnector->base.base,
4638 adev->mode_info.underscan_property,
4639 UNDERSCAN_OFF);
4640 drm_object_attach_property(&aconnector->base.base,
4641 adev->mode_info.underscan_hborder_property,
4642 0);
4643 drm_object_attach_property(&aconnector->base.base,
4644 adev->mode_info.underscan_vborder_property,
4645 0);
07e3a1cf
NK
4646 drm_object_attach_property(&aconnector->base.base,
4647 adev->mode_info.max_bpc_property,
4648 0);
e7b07cee 4649
c1ee92f9
DF
4650 if (connector_type == DRM_MODE_CONNECTOR_eDP &&
4651 dc_is_dmcu_initialized(adev->dm.dc)) {
4652 drm_object_attach_property(&aconnector->base.base,
4653 adev->mode_info.abm_level_property, 0);
4654 }
bb47de73
NK
4655
4656 if (connector_type == DRM_MODE_CONNECTOR_HDMIA ||
7fad8da1
NK
4657 connector_type == DRM_MODE_CONNECTOR_DisplayPort ||
4658 connector_type == DRM_MODE_CONNECTOR_eDP) {
bb47de73
NK
4659 drm_connector_attach_vrr_capable_property(
4660 &aconnector->base);
4661 }
e7b07cee
HW
4662}
4663
7578ecda
AD
4664static int amdgpu_dm_i2c_xfer(struct i2c_adapter *i2c_adap,
4665 struct i2c_msg *msgs, int num)
e7b07cee
HW
4666{
4667 struct amdgpu_i2c_adapter *i2c = i2c_get_adapdata(i2c_adap);
4668 struct ddc_service *ddc_service = i2c->ddc_service;
4669 struct i2c_command cmd;
4670 int i;
4671 int result = -EIO;
4672
b830ebc9 4673 cmd.payloads = kcalloc(num, sizeof(struct i2c_payload), GFP_KERNEL);
e7b07cee
HW
4674
4675 if (!cmd.payloads)
4676 return result;
4677
4678 cmd.number_of_payloads = num;
4679 cmd.engine = I2C_COMMAND_ENGINE_DEFAULT;
4680 cmd.speed = 100;
4681
4682 for (i = 0; i < num; i++) {
4683 cmd.payloads[i].write = !(msgs[i].flags & I2C_M_RD);
4684 cmd.payloads[i].address = msgs[i].addr;
4685 cmd.payloads[i].length = msgs[i].len;
4686 cmd.payloads[i].data = msgs[i].buf;
4687 }
4688
c85e6e54
DF
4689 if (dc_submit_i2c(
4690 ddc_service->ctx->dc,
4691 ddc_service->ddc_pin->hw_info.ddc_channel,
e7b07cee
HW
4692 &cmd))
4693 result = num;
4694
4695 kfree(cmd.payloads);
4696 return result;
4697}
4698
7578ecda 4699static u32 amdgpu_dm_i2c_func(struct i2c_adapter *adap)
e7b07cee
HW
4700{
4701 return I2C_FUNC_I2C | I2C_FUNC_SMBUS_EMUL;
4702}
4703
4704static const struct i2c_algorithm amdgpu_dm_i2c_algo = {
4705 .master_xfer = amdgpu_dm_i2c_xfer,
4706 .functionality = amdgpu_dm_i2c_func,
4707};
4708
3ee6b26b
AD
4709static struct amdgpu_i2c_adapter *
4710create_i2c(struct ddc_service *ddc_service,
4711 int link_index,
4712 int *res)
e7b07cee
HW
4713{
4714 struct amdgpu_device *adev = ddc_service->ctx->driver_context;
4715 struct amdgpu_i2c_adapter *i2c;
4716
b830ebc9 4717 i2c = kzalloc(sizeof(struct amdgpu_i2c_adapter), GFP_KERNEL);
2a55f096
ES
4718 if (!i2c)
4719 return NULL;
e7b07cee
HW
4720 i2c->base.owner = THIS_MODULE;
4721 i2c->base.class = I2C_CLASS_DDC;
4722 i2c->base.dev.parent = &adev->pdev->dev;
4723 i2c->base.algo = &amdgpu_dm_i2c_algo;
b830ebc9 4724 snprintf(i2c->base.name, sizeof(i2c->base.name), "AMDGPU DM i2c hw bus %d", link_index);
e7b07cee
HW
4725 i2c_set_adapdata(&i2c->base, i2c);
4726 i2c->ddc_service = ddc_service;
c85e6e54 4727 i2c->ddc_service->ddc_pin->hw_info.ddc_channel = link_index;
e7b07cee
HW
4728
4729 return i2c;
4730}
4731
89fc8d4e 4732
1f6010a9
DF
4733/*
4734 * Note: this function assumes that dc_link_detect() was called for the
b830ebc9
HW
4735 * dc_link which will be represented by this aconnector.
4736 */
7578ecda
AD
4737static int amdgpu_dm_connector_init(struct amdgpu_display_manager *dm,
4738 struct amdgpu_dm_connector *aconnector,
4739 uint32_t link_index,
4740 struct amdgpu_encoder *aencoder)
e7b07cee
HW
4741{
4742 int res = 0;
4743 int connector_type;
4744 struct dc *dc = dm->dc;
4745 struct dc_link *link = dc_get_link_at_index(dc, link_index);
4746 struct amdgpu_i2c_adapter *i2c;
9a227d26
TSD
4747
4748 link->priv = aconnector;
e7b07cee 4749
f1ad2f5e 4750 DRM_DEBUG_DRIVER("%s()\n", __func__);
e7b07cee
HW
4751
4752 i2c = create_i2c(link->ddc, link->link_index, &res);
2a55f096
ES
4753 if (!i2c) {
4754 DRM_ERROR("Failed to create i2c adapter data\n");
4755 return -ENOMEM;
4756 }
4757
e7b07cee
HW
4758 aconnector->i2c = i2c;
4759 res = i2c_add_adapter(&i2c->base);
4760
4761 if (res) {
4762 DRM_ERROR("Failed to register hw i2c %d\n", link->link_index);
4763 goto out_free;
4764 }
4765
4766 connector_type = to_drm_connector_type(link->connector_signal);
4767
4768 res = drm_connector_init(
4769 dm->ddev,
4770 &aconnector->base,
4771 &amdgpu_dm_connector_funcs,
4772 connector_type);
4773
4774 if (res) {
4775 DRM_ERROR("connector_init failed\n");
4776 aconnector->connector_id = -1;
4777 goto out_free;
4778 }
4779
4780 drm_connector_helper_add(
4781 &aconnector->base,
4782 &amdgpu_dm_connector_helper_funcs);
4783
96719c54
HW
4784 if (aconnector->base.funcs->reset)
4785 aconnector->base.funcs->reset(&aconnector->base);
4786
e7b07cee
HW
4787 amdgpu_dm_connector_init_helper(
4788 dm,
4789 aconnector,
4790 connector_type,
4791 link,
4792 link_index);
4793
cde4c44d 4794 drm_connector_attach_encoder(
e7b07cee
HW
4795 &aconnector->base, &aencoder->base);
4796
4797 drm_connector_register(&aconnector->base);
dc38fd9d
DF
4798#if defined(CONFIG_DEBUG_FS)
4799 res = connector_debugfs_init(aconnector);
4800 if (res) {
4801 DRM_ERROR("Failed to create debugfs for connector");
4802 goto out_free;
4803 }
f258fee6
DF
4804 aconnector->debugfs_dpcd_address = 0;
4805 aconnector->debugfs_dpcd_size = 0;
dc38fd9d 4806#endif
e7b07cee
HW
4807
4808 if (connector_type == DRM_MODE_CONNECTOR_DisplayPort
4809 || connector_type == DRM_MODE_CONNECTOR_eDP)
4810 amdgpu_dm_initialize_dp_connector(dm, aconnector);
4811
e7b07cee
HW
4812out_free:
4813 if (res) {
4814 kfree(i2c);
4815 aconnector->i2c = NULL;
4816 }
4817 return res;
4818}
4819
4820int amdgpu_dm_get_encoder_crtc_mask(struct amdgpu_device *adev)
4821{
4822 switch (adev->mode_info.num_crtc) {
4823 case 1:
4824 return 0x1;
4825 case 2:
4826 return 0x3;
4827 case 3:
4828 return 0x7;
4829 case 4:
4830 return 0xf;
4831 case 5:
4832 return 0x1f;
4833 case 6:
4834 default:
4835 return 0x3f;
4836 }
4837}
4838
7578ecda
AD
4839static int amdgpu_dm_encoder_init(struct drm_device *dev,
4840 struct amdgpu_encoder *aencoder,
4841 uint32_t link_index)
e7b07cee
HW
4842{
4843 struct amdgpu_device *adev = dev->dev_private;
4844
4845 int res = drm_encoder_init(dev,
4846 &aencoder->base,
4847 &amdgpu_dm_encoder_funcs,
4848 DRM_MODE_ENCODER_TMDS,
4849 NULL);
4850
4851 aencoder->base.possible_crtcs = amdgpu_dm_get_encoder_crtc_mask(adev);
4852
4853 if (!res)
4854 aencoder->encoder_id = link_index;
4855 else
4856 aencoder->encoder_id = -1;
4857
4858 drm_encoder_helper_add(&aencoder->base, &amdgpu_dm_encoder_helper_funcs);
4859
4860 return res;
4861}
4862
3ee6b26b
AD
4863static void manage_dm_interrupts(struct amdgpu_device *adev,
4864 struct amdgpu_crtc *acrtc,
4865 bool enable)
e7b07cee
HW
4866{
4867 /*
4868 * this is not correct translation but will work as soon as VBLANK
4869 * constant is the same as PFLIP
4870 */
4871 int irq_type =
734dd01d 4872 amdgpu_display_crtc_idx_to_irq_type(
e7b07cee
HW
4873 adev,
4874 acrtc->crtc_id);
4875
4876 if (enable) {
4877 drm_crtc_vblank_on(&acrtc->base);
4878 amdgpu_irq_get(
4879 adev,
4880 &adev->pageflip_irq,
4881 irq_type);
4882 } else {
4883
4884 amdgpu_irq_put(
4885 adev,
4886 &adev->pageflip_irq,
4887 irq_type);
4888 drm_crtc_vblank_off(&acrtc->base);
4889 }
4890}
4891
3ee6b26b
AD
4892static bool
4893is_scaling_state_different(const struct dm_connector_state *dm_state,
4894 const struct dm_connector_state *old_dm_state)
e7b07cee
HW
4895{
4896 if (dm_state->scaling != old_dm_state->scaling)
4897 return true;
4898 if (!dm_state->underscan_enable && old_dm_state->underscan_enable) {
4899 if (old_dm_state->underscan_hborder != 0 && old_dm_state->underscan_vborder != 0)
4900 return true;
4901 } else if (dm_state->underscan_enable && !old_dm_state->underscan_enable) {
4902 if (dm_state->underscan_hborder != 0 && dm_state->underscan_vborder != 0)
4903 return true;
b830ebc9
HW
4904 } else if (dm_state->underscan_hborder != old_dm_state->underscan_hborder ||
4905 dm_state->underscan_vborder != old_dm_state->underscan_vborder)
4906 return true;
e7b07cee
HW
4907 return false;
4908}
4909
3ee6b26b
AD
4910static void remove_stream(struct amdgpu_device *adev,
4911 struct amdgpu_crtc *acrtc,
4912 struct dc_stream_state *stream)
e7b07cee
HW
4913{
4914 /* this is the update mode case */
e7b07cee
HW
4915
4916 acrtc->otg_inst = -1;
4917 acrtc->enabled = false;
4918}
4919
7578ecda
AD
4920static int get_cursor_position(struct drm_plane *plane, struct drm_crtc *crtc,
4921 struct dc_cursor_position *position)
2a8f6ccb 4922{
f4c2cc43 4923 struct amdgpu_crtc *amdgpu_crtc = to_amdgpu_crtc(crtc);
2a8f6ccb
HW
4924 int x, y;
4925 int xorigin = 0, yorigin = 0;
4926
4927 if (!crtc || !plane->state->fb) {
4928 position->enable = false;
4929 position->x = 0;
4930 position->y = 0;
4931 return 0;
4932 }
4933
4934 if ((plane->state->crtc_w > amdgpu_crtc->max_cursor_width) ||
4935 (plane->state->crtc_h > amdgpu_crtc->max_cursor_height)) {
4936 DRM_ERROR("%s: bad cursor width or height %d x %d\n",
4937 __func__,
4938 plane->state->crtc_w,
4939 plane->state->crtc_h);
4940 return -EINVAL;
4941 }
4942
4943 x = plane->state->crtc_x;
4944 y = plane->state->crtc_y;
c14a005c
NK
4945
4946 if (crtc->primary->state) {
4947 /* avivo cursor are offset into the total surface */
4948 x += crtc->primary->state->src_x >> 16;
4949 y += crtc->primary->state->src_y >> 16;
4950 }
4951
2a8f6ccb
HW
4952 if (x < 0) {
4953 xorigin = min(-x, amdgpu_crtc->max_cursor_width - 1);
4954 x = 0;
4955 }
4956 if (y < 0) {
4957 yorigin = min(-y, amdgpu_crtc->max_cursor_height - 1);
4958 y = 0;
4959 }
4960 position->enable = true;
4961 position->x = x;
4962 position->y = y;
4963 position->x_hotspot = xorigin;
4964 position->y_hotspot = yorigin;
4965
4966 return 0;
4967}
4968
3ee6b26b
AD
4969static void handle_cursor_update(struct drm_plane *plane,
4970 struct drm_plane_state *old_plane_state)
e7b07cee 4971{
674e78ac 4972 struct amdgpu_device *adev = plane->dev->dev_private;
2a8f6ccb
HW
4973 struct amdgpu_framebuffer *afb = to_amdgpu_framebuffer(plane->state->fb);
4974 struct drm_crtc *crtc = afb ? plane->state->crtc : old_plane_state->crtc;
4975 struct dm_crtc_state *crtc_state = crtc ? to_dm_crtc_state(crtc->state) : NULL;
4976 struct amdgpu_crtc *amdgpu_crtc = to_amdgpu_crtc(crtc);
4977 uint64_t address = afb ? afb->address : 0;
4978 struct dc_cursor_position position;
4979 struct dc_cursor_attributes attributes;
4980 int ret;
4981
e7b07cee
HW
4982 if (!plane->state->fb && !old_plane_state->fb)
4983 return;
4984
f1ad2f5e 4985 DRM_DEBUG_DRIVER("%s: crtc_id=%d with size %d to %d\n",
c12a7ba5
HW
4986 __func__,
4987 amdgpu_crtc->crtc_id,
4988 plane->state->crtc_w,
4989 plane->state->crtc_h);
2a8f6ccb
HW
4990
4991 ret = get_cursor_position(plane, crtc, &position);
4992 if (ret)
4993 return;
4994
4995 if (!position.enable) {
4996 /* turn off cursor */
674e78ac
NK
4997 if (crtc_state && crtc_state->stream) {
4998 mutex_lock(&adev->dm.dc_lock);
2a8f6ccb
HW
4999 dc_stream_set_cursor_position(crtc_state->stream,
5000 &position);
674e78ac
NK
5001 mutex_unlock(&adev->dm.dc_lock);
5002 }
2a8f6ccb 5003 return;
e7b07cee 5004 }
e7b07cee 5005
2a8f6ccb
HW
5006 amdgpu_crtc->cursor_width = plane->state->crtc_w;
5007 amdgpu_crtc->cursor_height = plane->state->crtc_h;
5008
5009 attributes.address.high_part = upper_32_bits(address);
5010 attributes.address.low_part = lower_32_bits(address);
5011 attributes.width = plane->state->crtc_w;
5012 attributes.height = plane->state->crtc_h;
5013 attributes.color_format = CURSOR_MODE_COLOR_PRE_MULTIPLIED_ALPHA;
5014 attributes.rotation_angle = 0;
5015 attributes.attribute_flags.value = 0;
5016
5017 attributes.pitch = attributes.width;
5018
886daac9 5019 if (crtc_state->stream) {
674e78ac 5020 mutex_lock(&adev->dm.dc_lock);
886daac9
JZ
5021 if (!dc_stream_set_cursor_attributes(crtc_state->stream,
5022 &attributes))
5023 DRM_ERROR("DC failed to set cursor attributes\n");
2a8f6ccb 5024
2a8f6ccb
HW
5025 if (!dc_stream_set_cursor_position(crtc_state->stream,
5026 &position))
5027 DRM_ERROR("DC failed to set cursor position\n");
674e78ac 5028 mutex_unlock(&adev->dm.dc_lock);
886daac9 5029 }
2a8f6ccb 5030}
e7b07cee
HW
5031
5032static void prepare_flip_isr(struct amdgpu_crtc *acrtc)
5033{
5034
5035 assert_spin_locked(&acrtc->base.dev->event_lock);
5036 WARN_ON(acrtc->event);
5037
5038 acrtc->event = acrtc->base.state->event;
5039
5040 /* Set the flip status */
5041 acrtc->pflip_status = AMDGPU_FLIP_SUBMITTED;
5042
5043 /* Mark this event as consumed */
5044 acrtc->base.state->event = NULL;
5045
5046 DRM_DEBUG_DRIVER("crtc:%d, pflip_stat:AMDGPU_FLIP_SUBMITTED\n",
5047 acrtc->crtc_id);
5048}
5049
bb47de73
NK
5050static void update_freesync_state_on_stream(
5051 struct amdgpu_display_manager *dm,
5052 struct dm_crtc_state *new_crtc_state,
180db303
NK
5053 struct dc_stream_state *new_stream,
5054 struct dc_plane_state *surface,
5055 u32 flip_timestamp_in_us)
bb47de73 5056{
180db303 5057 struct mod_vrr_params vrr_params = new_crtc_state->vrr_params;
bb47de73 5058 struct dc_info_packet vrr_infopacket = {0};
bb47de73
NK
5059
5060 if (!new_stream)
5061 return;
5062
5063 /*
5064 * TODO: Determine why min/max totals and vrefresh can be 0 here.
5065 * For now it's sufficient to just guard against these conditions.
5066 */
5067
5068 if (!new_stream->timing.h_total || !new_stream->timing.v_total)
5069 return;
5070
180db303
NK
5071 if (surface) {
5072 mod_freesync_handle_preflip(
5073 dm->freesync_module,
5074 surface,
5075 new_stream,
5076 flip_timestamp_in_us,
5077 &vrr_params);
5078 }
bb47de73
NK
5079
5080 mod_freesync_build_vrr_infopacket(
5081 dm->freesync_module,
5082 new_stream,
180db303 5083 &vrr_params,
ecd0136b
HT
5084 PACKET_TYPE_VRR,
5085 TRANSFER_FUNC_UNKNOWN,
bb47de73
NK
5086 &vrr_infopacket);
5087
8a48b44c 5088 new_crtc_state->freesync_timing_changed |=
180db303
NK
5089 (memcmp(&new_crtc_state->vrr_params.adjust,
5090 &vrr_params.adjust,
5091 sizeof(vrr_params.adjust)) != 0);
bb47de73 5092
8a48b44c 5093 new_crtc_state->freesync_vrr_info_changed |=
bb47de73
NK
5094 (memcmp(&new_crtc_state->vrr_infopacket,
5095 &vrr_infopacket,
5096 sizeof(vrr_infopacket)) != 0);
5097
180db303 5098 new_crtc_state->vrr_params = vrr_params;
bb47de73
NK
5099 new_crtc_state->vrr_infopacket = vrr_infopacket;
5100
180db303 5101 new_stream->adjust = new_crtc_state->vrr_params.adjust;
bb47de73
NK
5102 new_stream->vrr_infopacket = vrr_infopacket;
5103
5104 if (new_crtc_state->freesync_vrr_info_changed)
5105 DRM_DEBUG_KMS("VRR packet update: crtc=%u enabled=%d state=%d",
5106 new_crtc_state->base.crtc->base.id,
5107 (int)new_crtc_state->base.vrr_enabled,
180db303 5108 (int)vrr_params.state);
bb47de73
NK
5109}
5110
e854194c
MK
5111static void pre_update_freesync_state_on_stream(
5112 struct amdgpu_display_manager *dm,
5113 struct dm_crtc_state *new_crtc_state)
5114{
5115 struct dc_stream_state *new_stream = new_crtc_state->stream;
5116 struct mod_vrr_params vrr_params = new_crtc_state->vrr_params;
5117 struct mod_freesync_config config = new_crtc_state->freesync_config;
5118
5119 if (!new_stream)
5120 return;
5121
5122 /*
5123 * TODO: Determine why min/max totals and vrefresh can be 0 here.
5124 * For now it's sufficient to just guard against these conditions.
5125 */
5126 if (!new_stream->timing.h_total || !new_stream->timing.v_total)
5127 return;
5128
5129 if (new_crtc_state->vrr_supported &&
5130 config.min_refresh_in_uhz &&
5131 config.max_refresh_in_uhz) {
5132 config.state = new_crtc_state->base.vrr_enabled ?
5133 VRR_STATE_ACTIVE_VARIABLE :
5134 VRR_STATE_INACTIVE;
5135 } else {
5136 config.state = VRR_STATE_UNSUPPORTED;
5137 }
5138
5139 mod_freesync_build_vrr_params(dm->freesync_module,
5140 new_stream,
5141 &config, &vrr_params);
5142
5143 new_crtc_state->freesync_timing_changed |=
5144 (memcmp(&new_crtc_state->vrr_params.adjust,
5145 &vrr_params.adjust,
5146 sizeof(vrr_params.adjust)) != 0);
5147
5148 new_crtc_state->vrr_params = vrr_params;
5149}
5150
66b0c973
MK
5151static void amdgpu_dm_handle_vrr_transition(struct dm_crtc_state *old_state,
5152 struct dm_crtc_state *new_state)
5153{
5154 bool old_vrr_active = amdgpu_dm_vrr_active(old_state);
5155 bool new_vrr_active = amdgpu_dm_vrr_active(new_state);
5156
5157 if (!old_vrr_active && new_vrr_active) {
5158 /* Transition VRR inactive -> active:
5159 * While VRR is active, we must not disable vblank irq, as a
5160 * reenable after disable would compute bogus vblank/pflip
5161 * timestamps if it likely happened inside display front-porch.
d2574c33
MK
5162 *
5163 * We also need vupdate irq for the actual core vblank handling
5164 * at end of vblank.
66b0c973 5165 */
d2574c33 5166 dm_set_vupdate_irq(new_state->base.crtc, true);
66b0c973
MK
5167 drm_crtc_vblank_get(new_state->base.crtc);
5168 DRM_DEBUG_DRIVER("%s: crtc=%u VRR off->on: Get vblank ref\n",
5169 __func__, new_state->base.crtc->base.id);
5170 } else if (old_vrr_active && !new_vrr_active) {
5171 /* Transition VRR active -> inactive:
5172 * Allow vblank irq disable again for fixed refresh rate.
5173 */
d2574c33 5174 dm_set_vupdate_irq(new_state->base.crtc, false);
66b0c973
MK
5175 drm_crtc_vblank_put(new_state->base.crtc);
5176 DRM_DEBUG_DRIVER("%s: crtc=%u VRR on->off: Drop vblank ref\n",
5177 __func__, new_state->base.crtc->base.id);
5178 }
5179}
5180
8ad27806
NK
5181static void amdgpu_dm_commit_cursors(struct drm_atomic_state *state)
5182{
5183 struct drm_plane *plane;
5184 struct drm_plane_state *old_plane_state, *new_plane_state;
5185 int i;
5186
5187 /*
5188 * TODO: Make this per-stream so we don't issue redundant updates for
5189 * commits with multiple streams.
5190 */
5191 for_each_oldnew_plane_in_state(state, plane, old_plane_state,
5192 new_plane_state, i)
5193 if (plane->type == DRM_PLANE_TYPE_CURSOR)
5194 handle_cursor_update(plane, old_plane_state);
5195}
5196
3be5262e 5197static void amdgpu_dm_commit_planes(struct drm_atomic_state *state,
eb3dc897 5198 struct dc_state *dc_state,
3ee6b26b
AD
5199 struct drm_device *dev,
5200 struct amdgpu_display_manager *dm,
5201 struct drm_crtc *pcrtc,
420cd472 5202 bool wait_for_vblank)
e7b07cee 5203{
8a48b44c
DF
5204 uint32_t i, r;
5205 uint64_t timestamp_ns;
e7b07cee 5206 struct drm_plane *plane;
0bc9706d 5207 struct drm_plane_state *old_plane_state, *new_plane_state;
e7b07cee 5208 struct amdgpu_crtc *acrtc_attach = to_amdgpu_crtc(pcrtc);
0bc9706d
LSL
5209 struct drm_crtc_state *new_pcrtc_state =
5210 drm_atomic_get_new_crtc_state(state, pcrtc);
5211 struct dm_crtc_state *acrtc_state = to_dm_crtc_state(new_pcrtc_state);
44d09c6a
HW
5212 struct dm_crtc_state *dm_old_crtc_state =
5213 to_dm_crtc_state(drm_atomic_get_old_crtc_state(state, pcrtc));
74aa7bd4 5214 int planes_count = 0, vpos, hpos;
e7b07cee 5215 unsigned long flags;
8a48b44c 5216 struct amdgpu_bo *abo;
09e5665a 5217 uint64_t tiling_flags;
fdd1fe57
MK
5218 uint32_t target_vblank, last_flip_vblank;
5219 bool vrr_active = amdgpu_dm_vrr_active(acrtc_state);
74aa7bd4 5220 bool pflip_present = false;
bc7f670e
DF
5221 struct {
5222 struct dc_surface_update surface_updates[MAX_SURFACES];
5223 struct dc_plane_info plane_infos[MAX_SURFACES];
5224 struct dc_scaling_info scaling_infos[MAX_SURFACES];
74aa7bd4 5225 struct dc_flip_addrs flip_addrs[MAX_SURFACES];
bc7f670e 5226 struct dc_stream_update stream_update;
74aa7bd4 5227 } *bundle;
bc7f670e 5228
74aa7bd4 5229 bundle = kzalloc(sizeof(*bundle), GFP_KERNEL);
8a48b44c 5230
74aa7bd4
DF
5231 if (!bundle) {
5232 dm_error("Failed to allocate update bundle\n");
4b510503
NK
5233 goto cleanup;
5234 }
e7b07cee 5235
8ad27806
NK
5236 /*
5237 * Disable the cursor first if we're disabling all the planes.
5238 * It'll remain on the screen after the planes are re-enabled
5239 * if we don't.
5240 */
5241 if (acrtc_state->active_planes == 0)
5242 amdgpu_dm_commit_cursors(state);
5243
e7b07cee 5244 /* update planes when needed */
0bc9706d
LSL
5245 for_each_oldnew_plane_in_state(state, plane, old_plane_state, new_plane_state, i) {
5246 struct drm_crtc *crtc = new_plane_state->crtc;
f5ba60fe 5247 struct drm_crtc_state *new_crtc_state;
0bc9706d 5248 struct drm_framebuffer *fb = new_plane_state->fb;
34bafd27 5249 bool plane_needs_flip;
c7af5f77 5250 struct dc_plane_state *dc_plane;
54d76575 5251 struct dm_plane_state *dm_new_plane_state = to_dm_plane_state(new_plane_state);
e7b07cee 5252
80c218d5
NK
5253 /* Cursor plane is handled after stream updates */
5254 if (plane->type == DRM_PLANE_TYPE_CURSOR)
e7b07cee 5255 continue;
e7b07cee 5256
f5ba60fe
DD
5257 if (!fb || !crtc || pcrtc != crtc)
5258 continue;
5259
5260 new_crtc_state = drm_atomic_get_new_crtc_state(state, crtc);
5261 if (!new_crtc_state->active)
e7b07cee
HW
5262 continue;
5263
bc7f670e 5264 dc_plane = dm_new_plane_state->dc_state;
e7b07cee 5265
74aa7bd4 5266 bundle->surface_updates[planes_count].surface = dc_plane;
bc7f670e 5267 if (new_pcrtc_state->color_mgmt_changed) {
74aa7bd4
DF
5268 bundle->surface_updates[planes_count].gamma = dc_plane->gamma_correction;
5269 bundle->surface_updates[planes_count].in_transfer_func = dc_plane->in_transfer_func;
bc7f670e 5270 }
8a48b44c 5271
695af5f9
NK
5272 fill_dc_scaling_info(new_plane_state,
5273 &bundle->scaling_infos[planes_count]);
8a48b44c 5274
695af5f9
NK
5275 bundle->surface_updates[planes_count].scaling_info =
5276 &bundle->scaling_infos[planes_count];
8a48b44c 5277
f5031000 5278 plane_needs_flip = old_plane_state->fb && new_plane_state->fb;
8a48b44c 5279
f5031000 5280 pflip_present = pflip_present || plane_needs_flip;
8a48b44c 5281
f5031000
DF
5282 if (!plane_needs_flip) {
5283 planes_count += 1;
5284 continue;
5285 }
8a48b44c 5286
2fac0f53
CK
5287 abo = gem_to_amdgpu_bo(fb->obj[0]);
5288
f8308898
AG
5289 /*
5290 * Wait for all fences on this FB. Do limited wait to avoid
5291 * deadlock during GPU reset when this fence will not signal
5292 * but we hold reservation lock for the BO.
5293 */
2fac0f53
CK
5294 r = reservation_object_wait_timeout_rcu(abo->tbo.resv, true,
5295 false,
f8308898
AG
5296 msecs_to_jiffies(5000));
5297 if (unlikely(r <= 0))
5298 DRM_ERROR("Waiting for fences timed out or interrupted!");
2fac0f53 5299
f5031000
DF
5300 /*
5301 * TODO This might fail and hence better not used, wait
5302 * explicitly on fences instead
5303 * and in general should be called for
5304 * blocking commit to as per framework helpers
5305 */
f5031000 5306 r = amdgpu_bo_reserve(abo, true);
f8308898 5307 if (unlikely(r != 0))
f5031000 5308 DRM_ERROR("failed to reserve buffer before flip\n");
8a48b44c 5309
f5031000 5310 amdgpu_bo_get_tiling_flags(abo, &tiling_flags);
8a48b44c 5311
f5031000 5312 amdgpu_bo_unreserve(abo);
8a48b44c 5313
695af5f9
NK
5314 fill_dc_plane_info_and_addr(
5315 dm->adev, new_plane_state, tiling_flags,
5316 &bundle->plane_infos[planes_count],
5317 &bundle->flip_addrs[planes_count].address);
5318
5319 bundle->surface_updates[planes_count].plane_info =
5320 &bundle->plane_infos[planes_count];
8a48b44c 5321
f5031000
DF
5322 bundle->flip_addrs[planes_count].flip_immediate =
5323 (crtc->state->pageflip_flags & DRM_MODE_PAGE_FLIP_ASYNC) != 0;
8a48b44c 5324
f5031000
DF
5325 timestamp_ns = ktime_get_ns();
5326 bundle->flip_addrs[planes_count].flip_timestamp_in_us = div_u64(timestamp_ns, 1000);
5327 bundle->surface_updates[planes_count].flip_addr = &bundle->flip_addrs[planes_count];
5328 bundle->surface_updates[planes_count].surface = dc_plane;
8a48b44c 5329
f5031000
DF
5330 if (!bundle->surface_updates[planes_count].surface) {
5331 DRM_ERROR("No surface for CRTC: id=%d\n",
5332 acrtc_attach->crtc_id);
5333 continue;
bc7f670e
DF
5334 }
5335
f5031000
DF
5336 if (plane == pcrtc->primary)
5337 update_freesync_state_on_stream(
5338 dm,
5339 acrtc_state,
5340 acrtc_state->stream,
5341 dc_plane,
5342 bundle->flip_addrs[planes_count].flip_timestamp_in_us);
bc7f670e 5343
f5031000
DF
5344 DRM_DEBUG_DRIVER("%s Flipping to hi: 0x%x, low: 0x%x\n",
5345 __func__,
5346 bundle->flip_addrs[planes_count].address.grph.addr.high_part,
5347 bundle->flip_addrs[planes_count].address.grph.addr.low_part);
bc7f670e
DF
5348
5349 planes_count += 1;
5350
8a48b44c
DF
5351 }
5352
74aa7bd4 5353 if (pflip_present) {
634092b1
MK
5354 if (!vrr_active) {
5355 /* Use old throttling in non-vrr fixed refresh rate mode
5356 * to keep flip scheduling based on target vblank counts
5357 * working in a backwards compatible way, e.g., for
5358 * clients using the GLX_OML_sync_control extension or
5359 * DRI3/Present extension with defined target_msc.
5360 */
fdd1fe57 5361 last_flip_vblank = amdgpu_get_vblank_counter_kms(dm->ddev, acrtc_attach->crtc_id);
634092b1
MK
5362 }
5363 else {
5364 /* For variable refresh rate mode only:
5365 * Get vblank of last completed flip to avoid > 1 vrr
5366 * flips per video frame by use of throttling, but allow
5367 * flip programming anywhere in the possibly large
5368 * variable vrr vblank interval for fine-grained flip
5369 * timing control and more opportunity to avoid stutter
5370 * on late submission of flips.
5371 */
5372 spin_lock_irqsave(&pcrtc->dev->event_lock, flags);
5373 last_flip_vblank = acrtc_attach->last_flip_vblank;
5374 spin_unlock_irqrestore(&pcrtc->dev->event_lock, flags);
5375 }
5376
fdd1fe57 5377 target_vblank = last_flip_vblank + wait_for_vblank;
8a48b44c
DF
5378
5379 /*
5380 * Wait until we're out of the vertical blank period before the one
5381 * targeted by the flip
5382 */
5383 while ((acrtc_attach->enabled &&
5384 (amdgpu_display_get_crtc_scanoutpos(dm->ddev, acrtc_attach->crtc_id,
5385 0, &vpos, &hpos, NULL,
5386 NULL, &pcrtc->hwmode)
5387 & (DRM_SCANOUTPOS_VALID | DRM_SCANOUTPOS_IN_VBLANK)) ==
5388 (DRM_SCANOUTPOS_VALID | DRM_SCANOUTPOS_IN_VBLANK) &&
5389 (int)(target_vblank -
5390 amdgpu_get_vblank_counter_kms(dm->ddev, acrtc_attach->crtc_id)) > 0)) {
5391 usleep_range(1000, 1100);
5392 }
5393
5394 if (acrtc_attach->base.state->event) {
5395 drm_crtc_vblank_get(pcrtc);
5396
5397 spin_lock_irqsave(&pcrtc->dev->event_lock, flags);
5398
5399 WARN_ON(acrtc_attach->pflip_status != AMDGPU_FLIP_NONE);
5400 prepare_flip_isr(acrtc_attach);
5401
5402 spin_unlock_irqrestore(&pcrtc->dev->event_lock, flags);
5403 }
5404
5405 if (acrtc_state->stream) {
5406
5407 if (acrtc_state->freesync_timing_changed)
74aa7bd4 5408 bundle->stream_update.adjust =
8a48b44c
DF
5409 &acrtc_state->stream->adjust;
5410
5411 if (acrtc_state->freesync_vrr_info_changed)
74aa7bd4 5412 bundle->stream_update.vrr_infopacket =
8a48b44c 5413 &acrtc_state->stream->vrr_infopacket;
e7b07cee 5414 }
e7b07cee
HW
5415 }
5416
bc92c065
NK
5417 /* Update the planes if changed or disable if we don't have any. */
5418 if (planes_count || acrtc_state->active_planes == 0) {
bc7f670e 5419 if (new_pcrtc_state->mode_changed) {
74aa7bd4
DF
5420 bundle->stream_update.src = acrtc_state->stream->src;
5421 bundle->stream_update.dst = acrtc_state->stream->dst;
e7b07cee
HW
5422 }
5423
bc7f670e 5424 if (new_pcrtc_state->color_mgmt_changed)
74aa7bd4 5425 bundle->stream_update.out_transfer_func = acrtc_state->stream->out_transfer_func;
bc7f670e 5426
8a48b44c 5427 acrtc_state->stream->abm_level = acrtc_state->abm_level;
bc7f670e 5428 if (acrtc_state->abm_level != dm_old_crtc_state->abm_level)
74aa7bd4 5429 bundle->stream_update.abm_level = &acrtc_state->abm_level;
44d09c6a 5430
bc7f670e
DF
5431 mutex_lock(&dm->dc_lock);
5432 dc_commit_updates_for_stream(dm->dc,
74aa7bd4 5433 bundle->surface_updates,
bc7f670e
DF
5434 planes_count,
5435 acrtc_state->stream,
74aa7bd4 5436 &bundle->stream_update,
bc7f670e
DF
5437 dc_state);
5438 mutex_unlock(&dm->dc_lock);
e7b07cee 5439 }
4b510503 5440
8ad27806
NK
5441 /*
5442 * Update cursor state *after* programming all the planes.
5443 * This avoids redundant programming in the case where we're going
5444 * to be disabling a single plane - those pipes are being disabled.
5445 */
5446 if (acrtc_state->active_planes)
5447 amdgpu_dm_commit_cursors(state);
80c218d5 5448
4b510503 5449cleanup:
74aa7bd4 5450 kfree(bundle);
e7b07cee
HW
5451}
5452
b5e83f6f
NK
5453/*
5454 * Enable interrupts on CRTCs that are newly active, undergone
5455 * a modeset, or have active planes again.
5456 *
5457 * Done in two passes, based on the for_modeset flag:
5458 * Pass 1: For CRTCs going through modeset
5459 * Pass 2: For CRTCs going from 0 to n active planes
5460 *
5461 * Interrupts can only be enabled after the planes are programmed,
5462 * so this requires a two-pass approach since we don't want to
5463 * just defer the interrupts until after commit planes every time.
5464 */
5465static void amdgpu_dm_enable_crtc_interrupts(struct drm_device *dev,
5466 struct drm_atomic_state *state,
5467 bool for_modeset)
5468{
5469 struct amdgpu_device *adev = dev->dev_private;
5470 struct drm_crtc *crtc;
5471 struct drm_crtc_state *old_crtc_state, *new_crtc_state;
5472 int i;
5473
5474 for_each_oldnew_crtc_in_state(state, crtc, old_crtc_state,
5475 new_crtc_state, i) {
5476 struct amdgpu_crtc *acrtc = to_amdgpu_crtc(crtc);
5477 struct dm_crtc_state *dm_new_crtc_state =
5478 to_dm_crtc_state(new_crtc_state);
5479 struct dm_crtc_state *dm_old_crtc_state =
5480 to_dm_crtc_state(old_crtc_state);
5481 bool modeset = drm_atomic_crtc_needs_modeset(new_crtc_state);
5482 bool run_pass;
5483
5484 run_pass = (for_modeset && modeset) ||
5485 (!for_modeset && !modeset &&
5486 !dm_old_crtc_state->interrupts_enabled);
5487
5488 if (!run_pass)
5489 continue;
5490
b5e83f6f
NK
5491 if (!dm_new_crtc_state->interrupts_enabled)
5492 continue;
5493
5494 manage_dm_interrupts(adev, acrtc, true);
5495
5496#ifdef CONFIG_DEBUG_FS
5497 /* The stream has changed so CRC capture needs to re-enabled. */
5498 if (dm_new_crtc_state->crc_enabled) {
5499 dm_new_crtc_state->crc_enabled = false;
5500 amdgpu_dm_crtc_set_crc_source(crtc, "auto");
5501 }
5502#endif
5503 }
5504}
5505
1f6010a9 5506/*
27b3f4fc
LSL
5507 * amdgpu_dm_crtc_copy_transient_flags - copy mirrored flags from DRM to DC
5508 * @crtc_state: the DRM CRTC state
5509 * @stream_state: the DC stream state.
5510 *
5511 * Copy the mirrored transient state flags from DRM, to DC. It is used to bring
5512 * a dc_stream_state's flags in sync with a drm_crtc_state's flags.
5513 */
5514static void amdgpu_dm_crtc_copy_transient_flags(struct drm_crtc_state *crtc_state,
5515 struct dc_stream_state *stream_state)
5516{
b9952f93 5517 stream_state->mode_changed = drm_atomic_crtc_needs_modeset(crtc_state);
27b3f4fc 5518}
e7b07cee 5519
7578ecda
AD
5520static int amdgpu_dm_atomic_commit(struct drm_device *dev,
5521 struct drm_atomic_state *state,
5522 bool nonblock)
e7b07cee
HW
5523{
5524 struct drm_crtc *crtc;
c2cea706 5525 struct drm_crtc_state *old_crtc_state, *new_crtc_state;
e7b07cee
HW
5526 struct amdgpu_device *adev = dev->dev_private;
5527 int i;
5528
5529 /*
d6ef9b41
NK
5530 * We evade vblank and pflip interrupts on CRTCs that are undergoing
5531 * a modeset, being disabled, or have no active planes.
5532 *
5533 * It's done in atomic commit rather than commit tail for now since
5534 * some of these interrupt handlers access the current CRTC state and
5535 * potentially the stream pointer itself.
5536 *
5537 * Since the atomic state is swapped within atomic commit and not within
5538 * commit tail this would leave to new state (that hasn't been committed yet)
5539 * being accesssed from within the handlers.
5540 *
5541 * TODO: Fix this so we can do this in commit tail and not have to block
5542 * in atomic check.
e7b07cee 5543 */
c2cea706 5544 for_each_oldnew_crtc_in_state(state, crtc, old_crtc_state, new_crtc_state, i) {
54d76575 5545 struct dm_crtc_state *dm_old_crtc_state = to_dm_crtc_state(old_crtc_state);
428da2bd 5546 struct dm_crtc_state *dm_new_crtc_state = to_dm_crtc_state(new_crtc_state);
e7b07cee
HW
5547 struct amdgpu_crtc *acrtc = to_amdgpu_crtc(crtc);
5548
d6ef9b41
NK
5549 if (dm_old_crtc_state->interrupts_enabled &&
5550 (!dm_new_crtc_state->interrupts_enabled ||
5551 drm_atomic_crtc_needs_modeset(new_crtc_state))) {
428da2bd 5552 /*
e39575b9
NK
5553 * Drop the extra vblank reference added by CRC
5554 * capture if applicable.
428da2bd 5555 */
e39575b9 5556 if (dm_new_crtc_state->crc_enabled)
428da2bd 5557 drm_crtc_vblank_put(crtc);
e39575b9
NK
5558
5559 /*
5560 * Only keep CRC capture enabled if there's
5561 * still a stream for the CRTC.
5562 */
5563 if (!dm_new_crtc_state->stream)
428da2bd 5564 dm_new_crtc_state->crc_enabled = false;
428da2bd 5565
e7b07cee 5566 manage_dm_interrupts(adev, acrtc, false);
428da2bd 5567 }
e7b07cee 5568 }
1f6010a9
DF
5569 /*
5570 * Add check here for SoC's that support hardware cursor plane, to
5571 * unset legacy_cursor_update
5572 */
e7b07cee
HW
5573
5574 return drm_atomic_helper_commit(dev, state, nonblock);
5575
5576 /*TODO Handle EINTR, reenable IRQ*/
5577}
5578
b8592b48
LL
5579/**
5580 * amdgpu_dm_atomic_commit_tail() - AMDgpu DM's commit tail implementation.
5581 * @state: The atomic state to commit
5582 *
5583 * This will tell DC to commit the constructed DC state from atomic_check,
5584 * programming the hardware. Any failures here implies a hardware failure, since
5585 * atomic check should have filtered anything non-kosher.
5586 */
7578ecda 5587static void amdgpu_dm_atomic_commit_tail(struct drm_atomic_state *state)
e7b07cee
HW
5588{
5589 struct drm_device *dev = state->dev;
5590 struct amdgpu_device *adev = dev->dev_private;
5591 struct amdgpu_display_manager *dm = &adev->dm;
5592 struct dm_atomic_state *dm_state;
eb3dc897 5593 struct dc_state *dc_state = NULL, *dc_state_temp = NULL;
e7b07cee 5594 uint32_t i, j;
5cc6dcbd 5595 struct drm_crtc *crtc;
0bc9706d 5596 struct drm_crtc_state *old_crtc_state, *new_crtc_state;
e7b07cee
HW
5597 unsigned long flags;
5598 bool wait_for_vblank = true;
5599 struct drm_connector *connector;
c2cea706 5600 struct drm_connector_state *old_con_state, *new_con_state;
54d76575 5601 struct dm_crtc_state *dm_old_crtc_state, *dm_new_crtc_state;
fe2a1965 5602 int crtc_disable_count = 0;
e7b07cee
HW
5603
5604 drm_atomic_helper_update_legacy_modeset_state(dev, state);
5605
eb3dc897
NK
5606 dm_state = dm_atomic_get_new_state(state);
5607 if (dm_state && dm_state->context) {
5608 dc_state = dm_state->context;
5609 } else {
5610 /* No state changes, retain current state. */
813d20dc 5611 dc_state_temp = dc_create_state(dm->dc);
eb3dc897
NK
5612 ASSERT(dc_state_temp);
5613 dc_state = dc_state_temp;
5614 dc_resource_state_copy_construct_current(dm->dc, dc_state);
5615 }
e7b07cee
HW
5616
5617 /* update changed items */
0bc9706d 5618 for_each_oldnew_crtc_in_state(state, crtc, old_crtc_state, new_crtc_state, i) {
e7b07cee 5619 struct amdgpu_crtc *acrtc = to_amdgpu_crtc(crtc);
b830ebc9 5620
54d76575
LSL
5621 dm_new_crtc_state = to_dm_crtc_state(new_crtc_state);
5622 dm_old_crtc_state = to_dm_crtc_state(old_crtc_state);
e7b07cee 5623
f1ad2f5e 5624 DRM_DEBUG_DRIVER(
e7b07cee
HW
5625 "amdgpu_crtc id:%d crtc_state_flags: enable:%d, active:%d, "
5626 "planes_changed:%d, mode_changed:%d,active_changed:%d,"
5627 "connectors_changed:%d\n",
5628 acrtc->crtc_id,
0bc9706d
LSL
5629 new_crtc_state->enable,
5630 new_crtc_state->active,
5631 new_crtc_state->planes_changed,
5632 new_crtc_state->mode_changed,
5633 new_crtc_state->active_changed,
5634 new_crtc_state->connectors_changed);
e7b07cee 5635
27b3f4fc
LSL
5636 /* Copy all transient state flags into dc state */
5637 if (dm_new_crtc_state->stream) {
5638 amdgpu_dm_crtc_copy_transient_flags(&dm_new_crtc_state->base,
5639 dm_new_crtc_state->stream);
5640 }
5641
e7b07cee
HW
5642 /* handles headless hotplug case, updating new_state and
5643 * aconnector as needed
5644 */
5645
54d76575 5646 if (modeset_required(new_crtc_state, dm_new_crtc_state->stream, dm_old_crtc_state->stream)) {
e7b07cee 5647
f1ad2f5e 5648 DRM_DEBUG_DRIVER("Atomic commit: SET crtc id %d: [%p]\n", acrtc->crtc_id, acrtc);
e7b07cee 5649
54d76575 5650 if (!dm_new_crtc_state->stream) {
e7b07cee 5651 /*
b830ebc9
HW
5652 * this could happen because of issues with
5653 * userspace notifications delivery.
5654 * In this case userspace tries to set mode on
1f6010a9
DF
5655 * display which is disconnected in fact.
5656 * dc_sink is NULL in this case on aconnector.
b830ebc9
HW
5657 * We expect reset mode will come soon.
5658 *
5659 * This can also happen when unplug is done
5660 * during resume sequence ended
5661 *
5662 * In this case, we want to pretend we still
5663 * have a sink to keep the pipe running so that
5664 * hw state is consistent with the sw state
5665 */
f1ad2f5e 5666 DRM_DEBUG_DRIVER("%s: Failed to create new stream for crtc %d\n",
e7b07cee
HW
5667 __func__, acrtc->base.base.id);
5668 continue;
5669 }
5670
54d76575
LSL
5671 if (dm_old_crtc_state->stream)
5672 remove_stream(adev, acrtc, dm_old_crtc_state->stream);
e7b07cee 5673
97028037
LP
5674 pm_runtime_get_noresume(dev->dev);
5675
e7b07cee 5676 acrtc->enabled = true;
0bc9706d
LSL
5677 acrtc->hw_mode = new_crtc_state->mode;
5678 crtc->hwmode = new_crtc_state->mode;
5679 } else if (modereset_required(new_crtc_state)) {
f1ad2f5e 5680 DRM_DEBUG_DRIVER("Atomic commit: RESET. crtc id %d:[%p]\n", acrtc->crtc_id, acrtc);
e7b07cee
HW
5681
5682 /* i.e. reset mode */
54d76575
LSL
5683 if (dm_old_crtc_state->stream)
5684 remove_stream(adev, acrtc, dm_old_crtc_state->stream);
e7b07cee
HW
5685 }
5686 } /* for_each_crtc_in_state() */
5687
eb3dc897
NK
5688 if (dc_state) {
5689 dm_enable_per_frame_crtc_master_sync(dc_state);
674e78ac 5690 mutex_lock(&dm->dc_lock);
eb3dc897 5691 WARN_ON(!dc_commit_state(dm->dc, dc_state));
674e78ac 5692 mutex_unlock(&dm->dc_lock);
fa2123db 5693 }
e7b07cee 5694
0bc9706d 5695 for_each_new_crtc_in_state(state, crtc, new_crtc_state, i) {
e7b07cee 5696 struct amdgpu_crtc *acrtc = to_amdgpu_crtc(crtc);
b830ebc9 5697
54d76575 5698 dm_new_crtc_state = to_dm_crtc_state(new_crtc_state);
e7b07cee 5699
54d76575 5700 if (dm_new_crtc_state->stream != NULL) {
e7b07cee 5701 const struct dc_stream_status *status =
54d76575 5702 dc_stream_get_status(dm_new_crtc_state->stream);
e7b07cee 5703
eb3dc897 5704 if (!status)
09f609c3
LL
5705 status = dc_stream_get_status_from_state(dc_state,
5706 dm_new_crtc_state->stream);
eb3dc897 5707
e7b07cee 5708 if (!status)
54d76575 5709 DC_ERR("got no status for stream %p on acrtc%p\n", dm_new_crtc_state->stream, acrtc);
e7b07cee
HW
5710 else
5711 acrtc->otg_inst = status->primary_otg_inst;
5712 }
5713 }
5714
02d6a6fc 5715 /* Handle connector state changes */
c2cea706 5716 for_each_oldnew_connector_in_state(state, connector, old_con_state, new_con_state, i) {
54d76575
LSL
5717 struct dm_connector_state *dm_new_con_state = to_dm_connector_state(new_con_state);
5718 struct dm_connector_state *dm_old_con_state = to_dm_connector_state(old_con_state);
5719 struct amdgpu_crtc *acrtc = to_amdgpu_crtc(dm_new_con_state->base.crtc);
19afd799
NC
5720 struct dc_surface_update dummy_updates[MAX_SURFACES];
5721 struct dc_stream_update stream_update;
e7b07cee
HW
5722 struct dc_stream_status *status = NULL;
5723
19afd799
NC
5724 memset(&dummy_updates, 0, sizeof(dummy_updates));
5725 memset(&stream_update, 0, sizeof(stream_update));
5726
44d09c6a 5727 if (acrtc) {
0bc9706d 5728 new_crtc_state = drm_atomic_get_new_crtc_state(state, &acrtc->base);
44d09c6a
HW
5729 old_crtc_state = drm_atomic_get_old_crtc_state(state, &acrtc->base);
5730 }
0bc9706d 5731
e7b07cee 5732 /* Skip any modesets/resets */
0bc9706d 5733 if (!acrtc || drm_atomic_crtc_needs_modeset(new_crtc_state))
e7b07cee
HW
5734 continue;
5735
54d76575 5736 dm_new_crtc_state = to_dm_crtc_state(new_crtc_state);
c1ee92f9
DF
5737 dm_old_crtc_state = to_dm_crtc_state(old_crtc_state);
5738
c1ee92f9
DF
5739 if (!is_scaling_state_different(dm_new_con_state, dm_old_con_state) &&
5740 (dm_new_crtc_state->abm_level == dm_old_crtc_state->abm_level))
5741 continue;
e7b07cee 5742
02d6a6fc
DF
5743 if (is_scaling_state_different(dm_new_con_state, dm_old_con_state)) {
5744 update_stream_scaling_settings(&dm_new_con_state->base.crtc->mode,
5745 dm_new_con_state, (struct dc_stream_state *)dm_new_crtc_state->stream);
e7b07cee 5746
02d6a6fc
DF
5747 stream_update.src = dm_new_crtc_state->stream->src;
5748 stream_update.dst = dm_new_crtc_state->stream->dst;
5749 }
5750
5751 if (dm_new_crtc_state->abm_level != dm_old_crtc_state->abm_level) {
5752 dm_new_crtc_state->stream->abm_level = dm_new_crtc_state->abm_level;
5753
5754 stream_update.abm_level = &dm_new_crtc_state->abm_level;
5755 }
70e8ffc5 5756
54d76575 5757 status = dc_stream_get_status(dm_new_crtc_state->stream);
e7b07cee 5758 WARN_ON(!status);
3be5262e 5759 WARN_ON(!status->plane_count);
e7b07cee 5760
02d6a6fc
DF
5761 /*
5762 * TODO: DC refuses to perform stream updates without a dc_surface_update.
5763 * Here we create an empty update on each plane.
5764 * To fix this, DC should permit updating only stream properties.
5765 */
5766 for (j = 0; j < status->plane_count; j++)
5767 dummy_updates[j].surface = status->plane_states[0];
5768
5769
5770 mutex_lock(&dm->dc_lock);
5771 dc_commit_updates_for_stream(dm->dc,
5772 dummy_updates,
5773 status->plane_count,
5774 dm_new_crtc_state->stream,
5775 &stream_update,
5776 dc_state);
5777 mutex_unlock(&dm->dc_lock);
e7b07cee
HW
5778 }
5779
b5e83f6f 5780 /* Count number of newly disabled CRTCs for dropping PM refs later. */
e1fc2dca 5781 for_each_oldnew_crtc_in_state(state, crtc, old_crtc_state,
057be086 5782 new_crtc_state, i) {
fe2a1965
LP
5783 if (old_crtc_state->active && !new_crtc_state->active)
5784 crtc_disable_count++;
5785
057be086
NK
5786 dm_new_crtc_state = to_dm_crtc_state(new_crtc_state);
5787 dm_old_crtc_state = to_dm_crtc_state(old_crtc_state);
5788
5789 /* Update freesync active state. */
5790 pre_update_freesync_state_on_stream(dm, dm_new_crtc_state);
5791
5792 /* Handle vrr on->off / off->on transitions */
5793 amdgpu_dm_handle_vrr_transition(dm_old_crtc_state,
5794 dm_new_crtc_state);
5795 }
5796
b5e83f6f
NK
5797 /* Enable interrupts for CRTCs going through a modeset. */
5798 amdgpu_dm_enable_crtc_interrupts(dev, state, true);
e7b07cee 5799
420cd472
DF
5800 for_each_new_crtc_in_state(state, crtc, new_crtc_state, j)
5801 if (new_crtc_state->pageflip_flags & DRM_MODE_PAGE_FLIP_ASYNC)
5802 wait_for_vblank = false;
5803
e7b07cee 5804 /* update planes when needed per crtc*/
5cc6dcbd 5805 for_each_new_crtc_in_state(state, crtc, new_crtc_state, j) {
54d76575 5806 dm_new_crtc_state = to_dm_crtc_state(new_crtc_state);
e7b07cee 5807
54d76575 5808 if (dm_new_crtc_state->stream)
eb3dc897 5809 amdgpu_dm_commit_planes(state, dc_state, dev,
420cd472 5810 dm, crtc, wait_for_vblank);
e7b07cee
HW
5811 }
5812
b5e83f6f
NK
5813 /* Enable interrupts for CRTCs going from 0 to n active planes. */
5814 amdgpu_dm_enable_crtc_interrupts(dev, state, false);
e7b07cee
HW
5815
5816 /*
5817 * send vblank event on all events not handled in flip and
5818 * mark consumed event for drm_atomic_helper_commit_hw_done
5819 */
5820 spin_lock_irqsave(&adev->ddev->event_lock, flags);
0bc9706d 5821 for_each_new_crtc_in_state(state, crtc, new_crtc_state, i) {
e7b07cee 5822
0bc9706d
LSL
5823 if (new_crtc_state->event)
5824 drm_send_event_locked(dev, &new_crtc_state->event->base);
e7b07cee 5825
0bc9706d 5826 new_crtc_state->event = NULL;
e7b07cee
HW
5827 }
5828 spin_unlock_irqrestore(&adev->ddev->event_lock, flags);
5829
29c8f234
LL
5830 /* Signal HW programming completion */
5831 drm_atomic_helper_commit_hw_done(state);
e7b07cee
HW
5832
5833 if (wait_for_vblank)
320a1274 5834 drm_atomic_helper_wait_for_flip_done(dev, state);
e7b07cee
HW
5835
5836 drm_atomic_helper_cleanup_planes(dev, state);
97028037 5837
1f6010a9
DF
5838 /*
5839 * Finally, drop a runtime PM reference for each newly disabled CRTC,
97028037
LP
5840 * so we can put the GPU into runtime suspend if we're not driving any
5841 * displays anymore
5842 */
fe2a1965
LP
5843 for (i = 0; i < crtc_disable_count; i++)
5844 pm_runtime_put_autosuspend(dev->dev);
97028037 5845 pm_runtime_mark_last_busy(dev->dev);
eb3dc897
NK
5846
5847 if (dc_state_temp)
5848 dc_release_state(dc_state_temp);
e7b07cee
HW
5849}
5850
5851
5852static int dm_force_atomic_commit(struct drm_connector *connector)
5853{
5854 int ret = 0;
5855 struct drm_device *ddev = connector->dev;
5856 struct drm_atomic_state *state = drm_atomic_state_alloc(ddev);
5857 struct amdgpu_crtc *disconnected_acrtc = to_amdgpu_crtc(connector->encoder->crtc);
5858 struct drm_plane *plane = disconnected_acrtc->base.primary;
5859 struct drm_connector_state *conn_state;
5860 struct drm_crtc_state *crtc_state;
5861 struct drm_plane_state *plane_state;
5862
5863 if (!state)
5864 return -ENOMEM;
5865
5866 state->acquire_ctx = ddev->mode_config.acquire_ctx;
5867
5868 /* Construct an atomic state to restore previous display setting */
5869
5870 /*
5871 * Attach connectors to drm_atomic_state
5872 */
5873 conn_state = drm_atomic_get_connector_state(state, connector);
5874
5875 ret = PTR_ERR_OR_ZERO(conn_state);
5876 if (ret)
5877 goto err;
5878
5879 /* Attach crtc to drm_atomic_state*/
5880 crtc_state = drm_atomic_get_crtc_state(state, &disconnected_acrtc->base);
5881
5882 ret = PTR_ERR_OR_ZERO(crtc_state);
5883 if (ret)
5884 goto err;
5885
5886 /* force a restore */
5887 crtc_state->mode_changed = true;
5888
5889 /* Attach plane to drm_atomic_state */
5890 plane_state = drm_atomic_get_plane_state(state, plane);
5891
5892 ret = PTR_ERR_OR_ZERO(plane_state);
5893 if (ret)
5894 goto err;
5895
5896
5897 /* Call commit internally with the state we just constructed */
5898 ret = drm_atomic_commit(state);
5899 if (!ret)
5900 return 0;
5901
5902err:
5903 DRM_ERROR("Restoring old state failed with %i\n", ret);
5904 drm_atomic_state_put(state);
5905
5906 return ret;
5907}
5908
5909/*
1f6010a9
DF
5910 * This function handles all cases when set mode does not come upon hotplug.
5911 * This includes when a display is unplugged then plugged back into the
5912 * same port and when running without usermode desktop manager supprot
e7b07cee 5913 */
3ee6b26b
AD
5914void dm_restore_drm_connector_state(struct drm_device *dev,
5915 struct drm_connector *connector)
e7b07cee 5916{
c84dec2f 5917 struct amdgpu_dm_connector *aconnector = to_amdgpu_dm_connector(connector);
e7b07cee
HW
5918 struct amdgpu_crtc *disconnected_acrtc;
5919 struct dm_crtc_state *acrtc_state;
5920
5921 if (!aconnector->dc_sink || !connector->state || !connector->encoder)
5922 return;
5923
5924 disconnected_acrtc = to_amdgpu_crtc(connector->encoder->crtc);
70e8ffc5
HW
5925 if (!disconnected_acrtc)
5926 return;
e7b07cee 5927
70e8ffc5
HW
5928 acrtc_state = to_dm_crtc_state(disconnected_acrtc->base.state);
5929 if (!acrtc_state->stream)
e7b07cee
HW
5930 return;
5931
5932 /*
5933 * If the previous sink is not released and different from the current,
5934 * we deduce we are in a state where we can not rely on usermode call
5935 * to turn on the display, so we do it here
5936 */
5937 if (acrtc_state->stream->sink != aconnector->dc_sink)
5938 dm_force_atomic_commit(&aconnector->base);
5939}
5940
1f6010a9 5941/*
e7b07cee
HW
5942 * Grabs all modesetting locks to serialize against any blocking commits,
5943 * Waits for completion of all non blocking commits.
5944 */
3ee6b26b
AD
5945static int do_aquire_global_lock(struct drm_device *dev,
5946 struct drm_atomic_state *state)
e7b07cee
HW
5947{
5948 struct drm_crtc *crtc;
5949 struct drm_crtc_commit *commit;
5950 long ret;
5951
1f6010a9
DF
5952 /*
5953 * Adding all modeset locks to aquire_ctx will
e7b07cee
HW
5954 * ensure that when the framework release it the
5955 * extra locks we are locking here will get released to
5956 */
5957 ret = drm_modeset_lock_all_ctx(dev, state->acquire_ctx);
5958 if (ret)
5959 return ret;
5960
5961 list_for_each_entry(crtc, &dev->mode_config.crtc_list, head) {
5962 spin_lock(&crtc->commit_lock);
5963 commit = list_first_entry_or_null(&crtc->commit_list,
5964 struct drm_crtc_commit, commit_entry);
5965 if (commit)
5966 drm_crtc_commit_get(commit);
5967 spin_unlock(&crtc->commit_lock);
5968
5969 if (!commit)
5970 continue;
5971
1f6010a9
DF
5972 /*
5973 * Make sure all pending HW programming completed and
e7b07cee
HW
5974 * page flips done
5975 */
5976 ret = wait_for_completion_interruptible_timeout(&commit->hw_done, 10*HZ);
5977
5978 if (ret > 0)
5979 ret = wait_for_completion_interruptible_timeout(
5980 &commit->flip_done, 10*HZ);
5981
5982 if (ret == 0)
5983 DRM_ERROR("[CRTC:%d:%s] hw_done or flip_done "
b830ebc9 5984 "timed out\n", crtc->base.id, crtc->name);
e7b07cee
HW
5985
5986 drm_crtc_commit_put(commit);
5987 }
5988
5989 return ret < 0 ? ret : 0;
5990}
5991
bb47de73
NK
5992static void get_freesync_config_for_crtc(
5993 struct dm_crtc_state *new_crtc_state,
5994 struct dm_connector_state *new_con_state)
98e6436d
AK
5995{
5996 struct mod_freesync_config config = {0};
98e6436d
AK
5997 struct amdgpu_dm_connector *aconnector =
5998 to_amdgpu_dm_connector(new_con_state->base.connector);
a057ec46 5999 struct drm_display_mode *mode = &new_crtc_state->base.mode;
98e6436d 6000
a057ec46
IB
6001 new_crtc_state->vrr_supported = new_con_state->freesync_capable &&
6002 aconnector->min_vfreq <= drm_mode_vrefresh(mode);
bb47de73 6003
a057ec46
IB
6004 if (new_crtc_state->vrr_supported) {
6005 new_crtc_state->stream->ignore_msa_timing_param = true;
bb47de73 6006 config.state = new_crtc_state->base.vrr_enabled ?
98e6436d
AK
6007 VRR_STATE_ACTIVE_VARIABLE :
6008 VRR_STATE_INACTIVE;
6009 config.min_refresh_in_uhz =
6010 aconnector->min_vfreq * 1000000;
6011 config.max_refresh_in_uhz =
6012 aconnector->max_vfreq * 1000000;
69ff8845 6013 config.vsif_supported = true;
180db303 6014 config.btr = true;
98e6436d
AK
6015 }
6016
bb47de73
NK
6017 new_crtc_state->freesync_config = config;
6018}
98e6436d 6019
bb47de73
NK
6020static void reset_freesync_config_for_crtc(
6021 struct dm_crtc_state *new_crtc_state)
6022{
6023 new_crtc_state->vrr_supported = false;
98e6436d 6024
180db303
NK
6025 memset(&new_crtc_state->vrr_params, 0,
6026 sizeof(new_crtc_state->vrr_params));
bb47de73
NK
6027 memset(&new_crtc_state->vrr_infopacket, 0,
6028 sizeof(new_crtc_state->vrr_infopacket));
98e6436d
AK
6029}
6030
4b9674e5
LL
6031static int dm_update_crtc_state(struct amdgpu_display_manager *dm,
6032 struct drm_atomic_state *state,
6033 struct drm_crtc *crtc,
6034 struct drm_crtc_state *old_crtc_state,
6035 struct drm_crtc_state *new_crtc_state,
6036 bool enable,
6037 bool *lock_and_validation_needed)
e7b07cee 6038{
eb3dc897 6039 struct dm_atomic_state *dm_state = NULL;
54d76575 6040 struct dm_crtc_state *dm_old_crtc_state, *dm_new_crtc_state;
9635b754 6041 struct dc_stream_state *new_stream;
62f55537 6042 int ret = 0;
d4d4a645 6043
1f6010a9
DF
6044 /*
6045 * TODO Move this code into dm_crtc_atomic_check once we get rid of dc_validation_set
6046 * update changed items
6047 */
4b9674e5
LL
6048 struct amdgpu_crtc *acrtc = NULL;
6049 struct amdgpu_dm_connector *aconnector = NULL;
6050 struct drm_connector_state *drm_new_conn_state = NULL, *drm_old_conn_state = NULL;
6051 struct dm_connector_state *dm_new_conn_state = NULL, *dm_old_conn_state = NULL;
e7b07cee 6052
4b9674e5 6053 new_stream = NULL;
9635b754 6054
4b9674e5
LL
6055 dm_old_crtc_state = to_dm_crtc_state(old_crtc_state);
6056 dm_new_crtc_state = to_dm_crtc_state(new_crtc_state);
6057 acrtc = to_amdgpu_crtc(crtc);
4b9674e5 6058 aconnector = amdgpu_dm_find_first_crtc_matching_connector(state, crtc);
19f89e23 6059
4b9674e5
LL
6060 /* TODO This hack should go away */
6061 if (aconnector && enable) {
6062 /* Make sure fake sink is created in plug-in scenario */
6063 drm_new_conn_state = drm_atomic_get_new_connector_state(state,
6064 &aconnector->base);
6065 drm_old_conn_state = drm_atomic_get_old_connector_state(state,
6066 &aconnector->base);
19f89e23 6067
4b9674e5
LL
6068 if (IS_ERR(drm_new_conn_state)) {
6069 ret = PTR_ERR_OR_ZERO(drm_new_conn_state);
6070 goto fail;
6071 }
19f89e23 6072
4b9674e5
LL
6073 dm_new_conn_state = to_dm_connector_state(drm_new_conn_state);
6074 dm_old_conn_state = to_dm_connector_state(drm_old_conn_state);
19f89e23 6075
02d35a67
JFZ
6076 if (!drm_atomic_crtc_needs_modeset(new_crtc_state))
6077 goto skip_modeset;
6078
4b9674e5
LL
6079 new_stream = create_stream_for_sink(aconnector,
6080 &new_crtc_state->mode,
6081 dm_new_conn_state,
6082 dm_old_crtc_state->stream);
19f89e23 6083
4b9674e5
LL
6084 /*
6085 * we can have no stream on ACTION_SET if a display
6086 * was disconnected during S3, in this case it is not an
6087 * error, the OS will be updated after detection, and
6088 * will do the right thing on next atomic commit
6089 */
19f89e23 6090
4b9674e5
LL
6091 if (!new_stream) {
6092 DRM_DEBUG_DRIVER("%s: Failed to create new stream for crtc %d\n",
6093 __func__, acrtc->base.base.id);
6094 ret = -ENOMEM;
6095 goto fail;
6096 }
e7b07cee 6097
4b9674e5 6098 dm_new_crtc_state->abm_level = dm_new_conn_state->abm_level;
98e6436d 6099
4b9674e5
LL
6100 if (dc_is_stream_unchanged(new_stream, dm_old_crtc_state->stream) &&
6101 dc_is_stream_scaling_unchanged(new_stream, dm_old_crtc_state->stream)) {
6102 new_crtc_state->mode_changed = false;
6103 DRM_DEBUG_DRIVER("Mode change not required, setting mode_changed to %d",
6104 new_crtc_state->mode_changed);
62f55537 6105 }
4b9674e5 6106 }
b830ebc9 6107
02d35a67 6108 /* mode_changed flag may get updated above, need to check again */
4b9674e5
LL
6109 if (!drm_atomic_crtc_needs_modeset(new_crtc_state))
6110 goto skip_modeset;
e7b07cee 6111
4b9674e5
LL
6112 DRM_DEBUG_DRIVER(
6113 "amdgpu_crtc id:%d crtc_state_flags: enable:%d, active:%d, "
6114 "planes_changed:%d, mode_changed:%d,active_changed:%d,"
6115 "connectors_changed:%d\n",
6116 acrtc->crtc_id,
6117 new_crtc_state->enable,
6118 new_crtc_state->active,
6119 new_crtc_state->planes_changed,
6120 new_crtc_state->mode_changed,
6121 new_crtc_state->active_changed,
6122 new_crtc_state->connectors_changed);
62f55537 6123
4b9674e5
LL
6124 /* Remove stream for any changed/disabled CRTC */
6125 if (!enable) {
62f55537 6126
4b9674e5
LL
6127 if (!dm_old_crtc_state->stream)
6128 goto skip_modeset;
eb3dc897 6129
4b9674e5
LL
6130 ret = dm_atomic_get_state(state, &dm_state);
6131 if (ret)
6132 goto fail;
e7b07cee 6133
4b9674e5
LL
6134 DRM_DEBUG_DRIVER("Disabling DRM crtc: %d\n",
6135 crtc->base.id);
62f55537 6136
4b9674e5
LL
6137 /* i.e. reset mode */
6138 if (dc_remove_stream_from_ctx(
6139 dm->dc,
6140 dm_state->context,
6141 dm_old_crtc_state->stream) != DC_OK) {
6142 ret = -EINVAL;
6143 goto fail;
6144 }
62f55537 6145
4b9674e5
LL
6146 dc_stream_release(dm_old_crtc_state->stream);
6147 dm_new_crtc_state->stream = NULL;
bb47de73 6148
4b9674e5 6149 reset_freesync_config_for_crtc(dm_new_crtc_state);
62f55537 6150
4b9674e5 6151 *lock_and_validation_needed = true;
62f55537 6152
4b9674e5
LL
6153 } else {/* Add stream for any updated/enabled CRTC */
6154 /*
6155 * Quick fix to prevent NULL pointer on new_stream when
6156 * added MST connectors not found in existing crtc_state in the chained mode
6157 * TODO: need to dig out the root cause of that
6158 */
6159 if (!aconnector || (!aconnector->dc_sink && aconnector->mst_port))
6160 goto skip_modeset;
62f55537 6161
4b9674e5
LL
6162 if (modereset_required(new_crtc_state))
6163 goto skip_modeset;
62f55537 6164
4b9674e5
LL
6165 if (modeset_required(new_crtc_state, new_stream,
6166 dm_old_crtc_state->stream)) {
62f55537 6167
4b9674e5 6168 WARN_ON(dm_new_crtc_state->stream);
eb3dc897 6169
4b9674e5
LL
6170 ret = dm_atomic_get_state(state, &dm_state);
6171 if (ret)
6172 goto fail;
27b3f4fc 6173
4b9674e5 6174 dm_new_crtc_state->stream = new_stream;
62f55537 6175
4b9674e5 6176 dc_stream_retain(new_stream);
1dc90497 6177
4b9674e5
LL
6178 DRM_DEBUG_DRIVER("Enabling DRM crtc: %d\n",
6179 crtc->base.id);
1dc90497 6180
4b9674e5
LL
6181 if (dc_add_stream_to_ctx(
6182 dm->dc,
6183 dm_state->context,
6184 dm_new_crtc_state->stream) != DC_OK) {
6185 ret = -EINVAL;
6186 goto fail;
9b690ef3
BL
6187 }
6188
4b9674e5
LL
6189 *lock_and_validation_needed = true;
6190 }
6191 }
e277adc5 6192
4b9674e5
LL
6193skip_modeset:
6194 /* Release extra reference */
6195 if (new_stream)
6196 dc_stream_release(new_stream);
e277adc5 6197
4b9674e5
LL
6198 /*
6199 * We want to do dc stream updates that do not require a
6200 * full modeset below.
6201 */
6202 if (!(enable && aconnector && new_crtc_state->enable &&
6203 new_crtc_state->active))
6204 return 0;
6205 /*
6206 * Given above conditions, the dc state cannot be NULL because:
6207 * 1. We're in the process of enabling CRTCs (just been added
6208 * to the dc context, or already is on the context)
6209 * 2. Has a valid connector attached, and
6210 * 3. Is currently active and enabled.
6211 * => The dc stream state currently exists.
6212 */
6213 BUG_ON(dm_new_crtc_state->stream == NULL);
a9e8d275 6214
4b9674e5
LL
6215 /* Scaling or underscan settings */
6216 if (is_scaling_state_different(dm_old_conn_state, dm_new_conn_state))
6217 update_stream_scaling_settings(
6218 &new_crtc_state->mode, dm_new_conn_state, dm_new_crtc_state->stream);
98e6436d 6219
b05e2c5e
DF
6220 /* ABM settings */
6221 dm_new_crtc_state->abm_level = dm_new_conn_state->abm_level;
6222
4b9674e5
LL
6223 /*
6224 * Color management settings. We also update color properties
6225 * when a modeset is needed, to ensure it gets reprogrammed.
6226 */
6227 if (dm_new_crtc_state->base.color_mgmt_changed ||
6228 drm_atomic_crtc_needs_modeset(new_crtc_state)) {
6229 ret = amdgpu_dm_set_regamma_lut(dm_new_crtc_state);
6230 if (ret)
6231 goto fail;
6232 amdgpu_dm_set_ctm(dm_new_crtc_state);
62f55537 6233 }
e7b07cee 6234
4b9674e5
LL
6235 /* Update Freesync settings. */
6236 get_freesync_config_for_crtc(dm_new_crtc_state,
6237 dm_new_conn_state);
6238
62f55537 6239 return ret;
9635b754
DS
6240
6241fail:
6242 if (new_stream)
6243 dc_stream_release(new_stream);
6244 return ret;
62f55537 6245}
9b690ef3 6246
f6ff2a08
NK
6247static bool should_reset_plane(struct drm_atomic_state *state,
6248 struct drm_plane *plane,
6249 struct drm_plane_state *old_plane_state,
6250 struct drm_plane_state *new_plane_state)
6251{
6252 struct drm_plane *other;
6253 struct drm_plane_state *old_other_state, *new_other_state;
6254 struct drm_crtc_state *new_crtc_state;
6255 int i;
6256
6257 /*
6258 * TODO: Remove this hack once the checks below are sufficient
6259 * enough to determine when we need to reset all the planes on
6260 * the stream.
6261 */
6262 if (state->allow_modeset)
6263 return true;
6264
6265 /* Exit early if we know that we're adding or removing the plane. */
6266 if (old_plane_state->crtc != new_plane_state->crtc)
6267 return true;
6268
6269 /* old crtc == new_crtc == NULL, plane not in context. */
6270 if (!new_plane_state->crtc)
6271 return false;
6272
6273 new_crtc_state =
6274 drm_atomic_get_new_crtc_state(state, new_plane_state->crtc);
6275
6276 if (!new_crtc_state)
6277 return true;
6278
6279 if (drm_atomic_crtc_needs_modeset(new_crtc_state))
6280 return true;
6281
6282 /*
6283 * If there are any new primary or overlay planes being added or
6284 * removed then the z-order can potentially change. To ensure
6285 * correct z-order and pipe acquisition the current DC architecture
6286 * requires us to remove and recreate all existing planes.
6287 *
6288 * TODO: Come up with a more elegant solution for this.
6289 */
6290 for_each_oldnew_plane_in_state(state, other, old_other_state, new_other_state, i) {
6291 if (other->type == DRM_PLANE_TYPE_CURSOR)
6292 continue;
6293
6294 if (old_other_state->crtc != new_plane_state->crtc &&
6295 new_other_state->crtc != new_plane_state->crtc)
6296 continue;
6297
6298 if (old_other_state->crtc != new_other_state->crtc)
6299 return true;
6300
6301 /* TODO: Remove this once we can handle fast format changes. */
6302 if (old_other_state->fb && new_other_state->fb &&
6303 old_other_state->fb->format != new_other_state->fb->format)
6304 return true;
6305 }
6306
6307 return false;
6308}
6309
9e869063
LL
6310static int dm_update_plane_state(struct dc *dc,
6311 struct drm_atomic_state *state,
6312 struct drm_plane *plane,
6313 struct drm_plane_state *old_plane_state,
6314 struct drm_plane_state *new_plane_state,
6315 bool enable,
6316 bool *lock_and_validation_needed)
62f55537 6317{
eb3dc897
NK
6318
6319 struct dm_atomic_state *dm_state = NULL;
62f55537 6320 struct drm_crtc *new_plane_crtc, *old_plane_crtc;
0bc9706d 6321 struct drm_crtc_state *old_crtc_state, *new_crtc_state;
54d76575 6322 struct dm_crtc_state *dm_new_crtc_state, *dm_old_crtc_state;
54d76575 6323 struct dm_plane_state *dm_new_plane_state, *dm_old_plane_state;
f6ff2a08 6324 bool needs_reset;
62f55537 6325 int ret = 0;
e7b07cee 6326
9b690ef3 6327
9e869063
LL
6328 new_plane_crtc = new_plane_state->crtc;
6329 old_plane_crtc = old_plane_state->crtc;
6330 dm_new_plane_state = to_dm_plane_state(new_plane_state);
6331 dm_old_plane_state = to_dm_plane_state(old_plane_state);
62f55537 6332
9e869063
LL
6333 /*TODO Implement atomic check for cursor plane */
6334 if (plane->type == DRM_PLANE_TYPE_CURSOR)
6335 return 0;
9b690ef3 6336
f6ff2a08
NK
6337 needs_reset = should_reset_plane(state, plane, old_plane_state,
6338 new_plane_state);
6339
9e869063
LL
6340 /* Remove any changed/removed planes */
6341 if (!enable) {
f6ff2a08 6342 if (!needs_reset)
9e869063 6343 return 0;
a7b06724 6344
9e869063
LL
6345 if (!old_plane_crtc)
6346 return 0;
62f55537 6347
9e869063
LL
6348 old_crtc_state = drm_atomic_get_old_crtc_state(
6349 state, old_plane_crtc);
6350 dm_old_crtc_state = to_dm_crtc_state(old_crtc_state);
9b690ef3 6351
9e869063
LL
6352 if (!dm_old_crtc_state->stream)
6353 return 0;
62f55537 6354
9e869063
LL
6355 DRM_DEBUG_ATOMIC("Disabling DRM plane: %d on DRM crtc %d\n",
6356 plane->base.id, old_plane_crtc->base.id);
9b690ef3 6357
9e869063
LL
6358 ret = dm_atomic_get_state(state, &dm_state);
6359 if (ret)
6360 return ret;
eb3dc897 6361
9e869063
LL
6362 if (!dc_remove_plane_from_context(
6363 dc,
6364 dm_old_crtc_state->stream,
6365 dm_old_plane_state->dc_state,
6366 dm_state->context)) {
62f55537 6367
9e869063
LL
6368 ret = EINVAL;
6369 return ret;
6370 }
e7b07cee 6371
9b690ef3 6372
9e869063
LL
6373 dc_plane_state_release(dm_old_plane_state->dc_state);
6374 dm_new_plane_state->dc_state = NULL;
1dc90497 6375
9e869063 6376 *lock_and_validation_needed = true;
1dc90497 6377
9e869063
LL
6378 } else { /* Add new planes */
6379 struct dc_plane_state *dc_new_plane_state;
1dc90497 6380
9e869063
LL
6381 if (drm_atomic_plane_disabling(plane->state, new_plane_state))
6382 return 0;
e7b07cee 6383
9e869063
LL
6384 if (!new_plane_crtc)
6385 return 0;
e7b07cee 6386
9e869063
LL
6387 new_crtc_state = drm_atomic_get_new_crtc_state(state, new_plane_crtc);
6388 dm_new_crtc_state = to_dm_crtc_state(new_crtc_state);
1dc90497 6389
9e869063
LL
6390 if (!dm_new_crtc_state->stream)
6391 return 0;
62f55537 6392
f6ff2a08 6393 if (!needs_reset)
9e869063 6394 return 0;
62f55537 6395
9e869063 6396 WARN_ON(dm_new_plane_state->dc_state);
9b690ef3 6397
9e869063
LL
6398 dc_new_plane_state = dc_create_plane_state(dc);
6399 if (!dc_new_plane_state)
6400 return -ENOMEM;
62f55537 6401
9e869063
LL
6402 DRM_DEBUG_DRIVER("Enabling DRM plane: %d on DRM crtc %d\n",
6403 plane->base.id, new_plane_crtc->base.id);
8c45c5db 6404
695af5f9 6405 ret = fill_dc_plane_attributes(
9e869063
LL
6406 new_plane_crtc->dev->dev_private,
6407 dc_new_plane_state,
6408 new_plane_state,
6409 new_crtc_state);
6410 if (ret) {
6411 dc_plane_state_release(dc_new_plane_state);
6412 return ret;
6413 }
62f55537 6414
9e869063
LL
6415 ret = dm_atomic_get_state(state, &dm_state);
6416 if (ret) {
6417 dc_plane_state_release(dc_new_plane_state);
6418 return ret;
6419 }
eb3dc897 6420
9e869063
LL
6421 /*
6422 * Any atomic check errors that occur after this will
6423 * not need a release. The plane state will be attached
6424 * to the stream, and therefore part of the atomic
6425 * state. It'll be released when the atomic state is
6426 * cleaned.
6427 */
6428 if (!dc_add_plane_to_context(
6429 dc,
6430 dm_new_crtc_state->stream,
6431 dc_new_plane_state,
6432 dm_state->context)) {
62f55537 6433
9e869063
LL
6434 dc_plane_state_release(dc_new_plane_state);
6435 return -EINVAL;
6436 }
8c45c5db 6437
9e869063 6438 dm_new_plane_state->dc_state = dc_new_plane_state;
000b59ea 6439
9e869063
LL
6440 /* Tell DC to do a full surface update every time there
6441 * is a plane change. Inefficient, but works for now.
6442 */
6443 dm_new_plane_state->dc_state->update_flags.bits.full_update = 1;
6444
6445 *lock_and_validation_needed = true;
62f55537 6446 }
e7b07cee
HW
6447
6448
62f55537
AG
6449 return ret;
6450}
a87fa993 6451
eb3dc897 6452static int
f843b308 6453dm_determine_update_type_for_commit(struct amdgpu_display_manager *dm,
eb3dc897
NK
6454 struct drm_atomic_state *state,
6455 enum surface_update_type *out_type)
6456{
f843b308 6457 struct dc *dc = dm->dc;
eb3dc897
NK
6458 struct dm_atomic_state *dm_state = NULL, *old_dm_state = NULL;
6459 int i, j, num_plane, ret = 0;
a87fa993
BL
6460 struct drm_plane_state *old_plane_state, *new_plane_state;
6461 struct dm_plane_state *new_dm_plane_state, *old_dm_plane_state;
6462 struct drm_crtc *new_plane_crtc, *old_plane_crtc;
6463 struct drm_plane *plane;
6464
6465 struct drm_crtc *crtc;
6466 struct drm_crtc_state *new_crtc_state, *old_crtc_state;
6467 struct dm_crtc_state *new_dm_crtc_state, *old_dm_crtc_state;
6468 struct dc_stream_status *status = NULL;
6469
fe96b99d 6470 struct dc_surface_update *updates;
a87fa993
BL
6471 enum surface_update_type update_type = UPDATE_TYPE_FAST;
6472
fe96b99d 6473 updates = kcalloc(MAX_SURFACES, sizeof(*updates), GFP_KERNEL);
fe96b99d 6474
f843b308
NK
6475 if (!updates) {
6476 DRM_ERROR("Failed to allocate plane updates\n");
4f712911
BL
6477 /* Set type to FULL to avoid crashing in DC*/
6478 update_type = UPDATE_TYPE_FULL;
eb3dc897 6479 goto cleanup;
4f712911 6480 }
a87fa993
BL
6481
6482 for_each_oldnew_crtc_in_state(state, crtc, old_crtc_state, new_crtc_state, i) {
004b3938 6483 struct dc_scaling_info scaling_info;
2aa632c5
NK
6484 struct dc_stream_update stream_update;
6485
6486 memset(&stream_update, 0, sizeof(stream_update));
c448a53a 6487
a87fa993
BL
6488 new_dm_crtc_state = to_dm_crtc_state(new_crtc_state);
6489 old_dm_crtc_state = to_dm_crtc_state(old_crtc_state);
6490 num_plane = 0;
6491
6836d239
NK
6492 if (new_dm_crtc_state->stream != old_dm_crtc_state->stream) {
6493 update_type = UPDATE_TYPE_FULL;
6494 goto cleanup;
6495 }
a87fa993 6496
6836d239 6497 if (!new_dm_crtc_state->stream)
c744e974 6498 continue;
eb3dc897 6499
c744e974
NK
6500 for_each_oldnew_plane_in_state(state, plane, old_plane_state, new_plane_state, j) {
6501 new_plane_crtc = new_plane_state->crtc;
6502 old_plane_crtc = old_plane_state->crtc;
6503 new_dm_plane_state = to_dm_plane_state(new_plane_state);
6504 old_dm_plane_state = to_dm_plane_state(old_plane_state);
eb3dc897 6505
c744e974
NK
6506 if (plane->type == DRM_PLANE_TYPE_CURSOR)
6507 continue;
eb3dc897 6508
6836d239
NK
6509 if (new_dm_plane_state->dc_state != old_dm_plane_state->dc_state) {
6510 update_type = UPDATE_TYPE_FULL;
6511 goto cleanup;
6512 }
6513
c744e974
NK
6514 if (crtc != new_plane_crtc)
6515 continue;
6516
f843b308 6517 updates[num_plane].surface = new_dm_plane_state->dc_state;
c744e974
NK
6518
6519 if (new_crtc_state->mode_changed) {
c744e974
NK
6520 stream_update.dst = new_dm_crtc_state->stream->dst;
6521 stream_update.src = new_dm_crtc_state->stream->src;
6522 }
6523
6524 if (new_crtc_state->color_mgmt_changed) {
6525 updates[num_plane].gamma =
6526 new_dm_plane_state->dc_state->gamma_correction;
6527 updates[num_plane].in_transfer_func =
6528 new_dm_plane_state->dc_state->in_transfer_func;
6529 stream_update.gamut_remap =
6530 &new_dm_crtc_state->stream->gamut_remap_matrix;
6531 stream_update.out_transfer_func =
6532 new_dm_crtc_state->stream->out_transfer_func;
a87fa993
BL
6533 }
6534
004b3938
NK
6535 ret = fill_dc_scaling_info(new_plane_state,
6536 &scaling_info);
6537 if (ret)
6538 goto cleanup;
6539
6540 updates[num_plane].scaling_info = &scaling_info;
6541
c744e974
NK
6542 num_plane++;
6543 }
6544
6545 if (num_plane == 0)
6546 continue;
6547
6548 ret = dm_atomic_get_state(state, &dm_state);
6549 if (ret)
6550 goto cleanup;
6551
6552 old_dm_state = dm_atomic_get_old_state(state);
6553 if (!old_dm_state) {
6554 ret = -EINVAL;
6555 goto cleanup;
6556 }
6557
6558 status = dc_stream_get_status_from_state(old_dm_state->context,
6559 new_dm_crtc_state->stream);
6560
f843b308
NK
6561 /*
6562 * TODO: DC modifies the surface during this call so we need
6563 * to lock here - find a way to do this without locking.
6564 */
6565 mutex_lock(&dm->dc_lock);
c744e974
NK
6566 update_type = dc_check_update_surfaces_for_stream(dc, updates, num_plane,
6567 &stream_update, status);
f843b308 6568 mutex_unlock(&dm->dc_lock);
c744e974
NK
6569
6570 if (update_type > UPDATE_TYPE_MED) {
a87fa993 6571 update_type = UPDATE_TYPE_FULL;
eb3dc897 6572 goto cleanup;
a87fa993
BL
6573 }
6574 }
6575
eb3dc897 6576cleanup:
a87fa993 6577 kfree(updates);
a87fa993 6578
eb3dc897
NK
6579 *out_type = update_type;
6580 return ret;
a87fa993 6581}
62f55537 6582
b8592b48
LL
6583/**
6584 * amdgpu_dm_atomic_check() - Atomic check implementation for AMDgpu DM.
6585 * @dev: The DRM device
6586 * @state: The atomic state to commit
6587 *
6588 * Validate that the given atomic state is programmable by DC into hardware.
6589 * This involves constructing a &struct dc_state reflecting the new hardware
6590 * state we wish to commit, then querying DC to see if it is programmable. It's
6591 * important not to modify the existing DC state. Otherwise, atomic_check
6592 * may unexpectedly commit hardware changes.
6593 *
6594 * When validating the DC state, it's important that the right locks are
6595 * acquired. For full updates case which removes/adds/updates streams on one
6596 * CRTC while flipping on another CRTC, acquiring global lock will guarantee
6597 * that any such full update commit will wait for completion of any outstanding
6598 * flip using DRMs synchronization events. See
6599 * dm_determine_update_type_for_commit()
6600 *
6601 * Note that DM adds the affected connectors for all CRTCs in state, when that
6602 * might not seem necessary. This is because DC stream creation requires the
6603 * DC sink, which is tied to the DRM connector state. Cleaning this up should
6604 * be possible but non-trivial - a possible TODO item.
6605 *
6606 * Return: -Error code if validation failed.
6607 */
7578ecda
AD
6608static int amdgpu_dm_atomic_check(struct drm_device *dev,
6609 struct drm_atomic_state *state)
62f55537 6610{
62f55537 6611 struct amdgpu_device *adev = dev->dev_private;
eb3dc897 6612 struct dm_atomic_state *dm_state = NULL;
62f55537 6613 struct dc *dc = adev->dm.dc;
62f55537 6614 struct drm_connector *connector;
c2cea706 6615 struct drm_connector_state *old_con_state, *new_con_state;
62f55537 6616 struct drm_crtc *crtc;
fc9e9920 6617 struct drm_crtc_state *old_crtc_state, *new_crtc_state;
9e869063
LL
6618 struct drm_plane *plane;
6619 struct drm_plane_state *old_plane_state, *new_plane_state;
a87fa993
BL
6620 enum surface_update_type update_type = UPDATE_TYPE_FAST;
6621 enum surface_update_type overall_update_type = UPDATE_TYPE_FAST;
6622
1e88ad0a 6623 int ret, i;
e7b07cee 6624
62f55537
AG
6625 /*
6626 * This bool will be set for true for any modeset/reset
6627 * or plane update which implies non fast surface update.
6628 */
6629 bool lock_and_validation_needed = false;
6630
6631 ret = drm_atomic_helper_check_modeset(dev, state);
01e28f9c
MD
6632 if (ret)
6633 goto fail;
62f55537 6634
1e88ad0a
S
6635 for_each_oldnew_crtc_in_state(state, crtc, old_crtc_state, new_crtc_state, i) {
6636 if (!drm_atomic_crtc_needs_modeset(new_crtc_state) &&
98e6436d 6637 !new_crtc_state->color_mgmt_changed &&
a93587b3 6638 old_crtc_state->vrr_enabled == new_crtc_state->vrr_enabled)
1e88ad0a 6639 continue;
7bef1af3 6640
1e88ad0a
S
6641 if (!new_crtc_state->enable)
6642 continue;
fc9e9920 6643
1e88ad0a
S
6644 ret = drm_atomic_add_affected_connectors(state, crtc);
6645 if (ret)
6646 return ret;
fc9e9920 6647
1e88ad0a
S
6648 ret = drm_atomic_add_affected_planes(state, crtc);
6649 if (ret)
6650 goto fail;
e7b07cee
HW
6651 }
6652
2d9e6431
NK
6653 /*
6654 * Add all primary and overlay planes on the CRTC to the state
6655 * whenever a plane is enabled to maintain correct z-ordering
6656 * and to enable fast surface updates.
6657 */
6658 drm_for_each_crtc(crtc, dev) {
6659 bool modified = false;
6660
6661 for_each_oldnew_plane_in_state(state, plane, old_plane_state, new_plane_state, i) {
6662 if (plane->type == DRM_PLANE_TYPE_CURSOR)
6663 continue;
6664
6665 if (new_plane_state->crtc == crtc ||
6666 old_plane_state->crtc == crtc) {
6667 modified = true;
6668 break;
6669 }
6670 }
6671
6672 if (!modified)
6673 continue;
6674
6675 drm_for_each_plane_mask(plane, state->dev, crtc->state->plane_mask) {
6676 if (plane->type == DRM_PLANE_TYPE_CURSOR)
6677 continue;
6678
6679 new_plane_state =
6680 drm_atomic_get_plane_state(state, plane);
6681
6682 if (IS_ERR(new_plane_state)) {
6683 ret = PTR_ERR(new_plane_state);
6684 goto fail;
6685 }
6686 }
6687 }
6688
62f55537 6689 /* Remove exiting planes if they are modified */
9e869063
LL
6690 for_each_oldnew_plane_in_state_reverse(state, plane, old_plane_state, new_plane_state, i) {
6691 ret = dm_update_plane_state(dc, state, plane,
6692 old_plane_state,
6693 new_plane_state,
6694 false,
6695 &lock_and_validation_needed);
6696 if (ret)
6697 goto fail;
62f55537
AG
6698 }
6699
6700 /* Disable all crtcs which require disable */
4b9674e5
LL
6701 for_each_oldnew_crtc_in_state(state, crtc, old_crtc_state, new_crtc_state, i) {
6702 ret = dm_update_crtc_state(&adev->dm, state, crtc,
6703 old_crtc_state,
6704 new_crtc_state,
6705 false,
6706 &lock_and_validation_needed);
6707 if (ret)
6708 goto fail;
62f55537
AG
6709 }
6710
6711 /* Enable all crtcs which require enable */
4b9674e5
LL
6712 for_each_oldnew_crtc_in_state(state, crtc, old_crtc_state, new_crtc_state, i) {
6713 ret = dm_update_crtc_state(&adev->dm, state, crtc,
6714 old_crtc_state,
6715 new_crtc_state,
6716 true,
6717 &lock_and_validation_needed);
6718 if (ret)
6719 goto fail;
62f55537
AG
6720 }
6721
6722 /* Add new/modified planes */
9e869063
LL
6723 for_each_oldnew_plane_in_state_reverse(state, plane, old_plane_state, new_plane_state, i) {
6724 ret = dm_update_plane_state(dc, state, plane,
6725 old_plane_state,
6726 new_plane_state,
6727 true,
6728 &lock_and_validation_needed);
6729 if (ret)
6730 goto fail;
62f55537
AG
6731 }
6732
b349f76e
ES
6733 /* Run this here since we want to validate the streams we created */
6734 ret = drm_atomic_helper_check_planes(dev, state);
6735 if (ret)
6736 goto fail;
62f55537 6737
ebdd27e1 6738 /* Check scaling and underscan changes*/
1f6010a9 6739 /* TODO Removed scaling changes validation due to inability to commit
e7b07cee
HW
6740 * new stream into context w\o causing full reset. Need to
6741 * decide how to handle.
6742 */
c2cea706 6743 for_each_oldnew_connector_in_state(state, connector, old_con_state, new_con_state, i) {
54d76575
LSL
6744 struct dm_connector_state *dm_old_con_state = to_dm_connector_state(old_con_state);
6745 struct dm_connector_state *dm_new_con_state = to_dm_connector_state(new_con_state);
6746 struct amdgpu_crtc *acrtc = to_amdgpu_crtc(dm_new_con_state->base.crtc);
e7b07cee
HW
6747
6748 /* Skip any modesets/resets */
0bc9706d
LSL
6749 if (!acrtc || drm_atomic_crtc_needs_modeset(
6750 drm_atomic_get_new_crtc_state(state, &acrtc->base)))
e7b07cee
HW
6751 continue;
6752
b830ebc9 6753 /* Skip any thing not scale or underscan changes */
54d76575 6754 if (!is_scaling_state_different(dm_new_con_state, dm_old_con_state))
e7b07cee
HW
6755 continue;
6756
a87fa993 6757 overall_update_type = UPDATE_TYPE_FULL;
e7b07cee
HW
6758 lock_and_validation_needed = true;
6759 }
6760
f843b308 6761 ret = dm_determine_update_type_for_commit(&adev->dm, state, &update_type);
eb3dc897
NK
6762 if (ret)
6763 goto fail;
a87fa993
BL
6764
6765 if (overall_update_type < update_type)
6766 overall_update_type = update_type;
6767
6768 /*
6769 * lock_and_validation_needed was an old way to determine if we need to set
6770 * the global lock. Leaving it in to check if we broke any corner cases
6771 * lock_and_validation_needed true = UPDATE_TYPE_FULL or UPDATE_TYPE_MED
6772 * lock_and_validation_needed false = UPDATE_TYPE_FAST
6773 */
6774 if (lock_and_validation_needed && overall_update_type <= UPDATE_TYPE_FAST)
6775 WARN(1, "Global lock should be Set, overall_update_type should be UPDATE_TYPE_MED or UPDATE_TYPE_FULL");
e7b07cee 6776
a87fa993 6777 if (overall_update_type > UPDATE_TYPE_FAST) {
eb3dc897
NK
6778 ret = dm_atomic_get_state(state, &dm_state);
6779 if (ret)
6780 goto fail;
e7b07cee
HW
6781
6782 ret = do_aquire_global_lock(dev, state);
6783 if (ret)
6784 goto fail;
1dc90497 6785
afcd526b 6786 if (dc_validate_global_state(dc, dm_state->context, false) != DC_OK) {
e7b07cee
HW
6787 ret = -EINVAL;
6788 goto fail;
6789 }
674e78ac
NK
6790 } else if (state->legacy_cursor_update) {
6791 /*
6792 * This is a fast cursor update coming from the plane update
6793 * helper, check if it can be done asynchronously for better
6794 * performance.
6795 */
6796 state->async_update = !drm_atomic_helper_async_check(dev, state);
e7b07cee
HW
6797 }
6798
6799 /* Must be success */
6800 WARN_ON(ret);
6801 return ret;
6802
6803fail:
6804 if (ret == -EDEADLK)
01e28f9c 6805 DRM_DEBUG_DRIVER("Atomic check stopped to avoid deadlock.\n");
e7b07cee 6806 else if (ret == -EINTR || ret == -EAGAIN || ret == -ERESTARTSYS)
01e28f9c 6807 DRM_DEBUG_DRIVER("Atomic check stopped due to signal.\n");
e7b07cee 6808 else
01e28f9c 6809 DRM_DEBUG_DRIVER("Atomic check failed with err: %d \n", ret);
e7b07cee
HW
6810
6811 return ret;
6812}
6813
3ee6b26b
AD
6814static bool is_dp_capable_without_timing_msa(struct dc *dc,
6815 struct amdgpu_dm_connector *amdgpu_dm_connector)
e7b07cee
HW
6816{
6817 uint8_t dpcd_data;
6818 bool capable = false;
6819
c84dec2f 6820 if (amdgpu_dm_connector->dc_link &&
e7b07cee
HW
6821 dm_helpers_dp_read_dpcd(
6822 NULL,
c84dec2f 6823 amdgpu_dm_connector->dc_link,
e7b07cee
HW
6824 DP_DOWN_STREAM_PORT_COUNT,
6825 &dpcd_data,
6826 sizeof(dpcd_data))) {
6827 capable = (dpcd_data & DP_MSA_TIMING_PAR_IGNORED) ? true:false;
6828 }
6829
6830 return capable;
6831}
98e6436d
AK
6832void amdgpu_dm_update_freesync_caps(struct drm_connector *connector,
6833 struct edid *edid)
e7b07cee
HW
6834{
6835 int i;
e7b07cee
HW
6836 bool edid_check_required;
6837 struct detailed_timing *timing;
6838 struct detailed_non_pixel *data;
6839 struct detailed_data_monitor_range *range;
c84dec2f
HW
6840 struct amdgpu_dm_connector *amdgpu_dm_connector =
6841 to_amdgpu_dm_connector(connector);
bb47de73 6842 struct dm_connector_state *dm_con_state = NULL;
e7b07cee
HW
6843
6844 struct drm_device *dev = connector->dev;
6845 struct amdgpu_device *adev = dev->dev_private;
bb47de73 6846 bool freesync_capable = false;
b830ebc9 6847
8218d7f1
HW
6848 if (!connector->state) {
6849 DRM_ERROR("%s - Connector has no state", __func__);
bb47de73 6850 goto update;
8218d7f1
HW
6851 }
6852
98e6436d
AK
6853 if (!edid) {
6854 dm_con_state = to_dm_connector_state(connector->state);
6855
6856 amdgpu_dm_connector->min_vfreq = 0;
6857 amdgpu_dm_connector->max_vfreq = 0;
6858 amdgpu_dm_connector->pixel_clock_mhz = 0;
6859
bb47de73 6860 goto update;
98e6436d
AK
6861 }
6862
8218d7f1
HW
6863 dm_con_state = to_dm_connector_state(connector->state);
6864
e7b07cee 6865 edid_check_required = false;
c84dec2f 6866 if (!amdgpu_dm_connector->dc_sink) {
e7b07cee 6867 DRM_ERROR("dc_sink NULL, could not add free_sync module.\n");
bb47de73 6868 goto update;
e7b07cee
HW
6869 }
6870 if (!adev->dm.freesync_module)
bb47de73 6871 goto update;
e7b07cee
HW
6872 /*
6873 * if edid non zero restrict freesync only for dp and edp
6874 */
6875 if (edid) {
c84dec2f
HW
6876 if (amdgpu_dm_connector->dc_sink->sink_signal == SIGNAL_TYPE_DISPLAY_PORT
6877 || amdgpu_dm_connector->dc_sink->sink_signal == SIGNAL_TYPE_EDP) {
e7b07cee
HW
6878 edid_check_required = is_dp_capable_without_timing_msa(
6879 adev->dm.dc,
c84dec2f 6880 amdgpu_dm_connector);
e7b07cee
HW
6881 }
6882 }
e7b07cee
HW
6883 if (edid_check_required == true && (edid->version > 1 ||
6884 (edid->version == 1 && edid->revision > 1))) {
6885 for (i = 0; i < 4; i++) {
6886
6887 timing = &edid->detailed_timings[i];
6888 data = &timing->data.other_data;
6889 range = &data->data.range;
6890 /*
6891 * Check if monitor has continuous frequency mode
6892 */
6893 if (data->type != EDID_DETAIL_MONITOR_RANGE)
6894 continue;
6895 /*
6896 * Check for flag range limits only. If flag == 1 then
6897 * no additional timing information provided.
6898 * Default GTF, GTF Secondary curve and CVT are not
6899 * supported
6900 */
6901 if (range->flags != 1)
6902 continue;
6903
c84dec2f
HW
6904 amdgpu_dm_connector->min_vfreq = range->min_vfreq;
6905 amdgpu_dm_connector->max_vfreq = range->max_vfreq;
6906 amdgpu_dm_connector->pixel_clock_mhz =
e7b07cee
HW
6907 range->pixel_clock_mhz * 10;
6908 break;
6909 }
6910
c84dec2f 6911 if (amdgpu_dm_connector->max_vfreq -
98e6436d
AK
6912 amdgpu_dm_connector->min_vfreq > 10) {
6913
bb47de73 6914 freesync_capable = true;
e7b07cee
HW
6915 }
6916 }
bb47de73
NK
6917
6918update:
6919 if (dm_con_state)
6920 dm_con_state->freesync_capable = freesync_capable;
6921
6922 if (connector->vrr_capable_property)
6923 drm_connector_set_vrr_capable_property(connector,
6924 freesync_capable);
e7b07cee
HW
6925}
6926