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