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