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