xref: /dflybsd-src/sys/dev/drm/amd/display/amdgpu_dm/amdgpu_dm.c (revision b843c749addef9340ee7d4e250b09fdd492602a1)
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 #include "dm_services_types.h"
27 #include "dc.h"
28 #include "dc/inc/core_types.h"
29 
30 #include "vid.h"
31 #include "amdgpu.h"
32 #include "amdgpu_display.h"
33 #include "atom.h"
34 #include "amdgpu_dm.h"
35 #include "amdgpu_pm.h"
36 
37 #include "amd_shared.h"
38 #include "amdgpu_dm_irq.h"
39 #include "dm_helpers.h"
40 #include "dm_services_types.h"
41 #include "amdgpu_dm_mst_types.h"
42 #if defined(CONFIG_DEBUG_FS)
43 #include "amdgpu_dm_debugfs.h"
44 #endif
45 
46 #include "ivsrcid/ivsrcid_vislands30.h"
47 
48 #include <linux/module.h>
49 #include <linux/moduleparam.h>
50 #include <linux/version.h>
51 #include <linux/types.h>
52 #include <linux/pm_runtime.h>
53 
54 #include <drm/drmP.h>
55 #include <drm/drm_atomic.h>
56 #include <drm/drm_atomic_helper.h>
57 #include <drm/drm_dp_mst_helper.h>
58 #include <drm/drm_fb_helper.h>
59 #include <drm/drm_edid.h>
60 
61 #if defined(CONFIG_DRM_AMD_DC_DCN1_0)
62 #include "ivsrcid/irqsrcs_dcn_1_0.h"
63 
64 #include "dcn/dcn_1_0_offset.h"
65 #include "dcn/dcn_1_0_sh_mask.h"
66 #include "soc15_hw_ip.h"
67 #include "vega10_ip_offset.h"
68 
69 #include "soc15_common.h"
70 #endif
71 
72 #include "modules/inc/mod_freesync.h"
73 
74 #include "i2caux_interface.h"
75 
76 /* basic init/fini API */
77 static int amdgpu_dm_init(struct amdgpu_device *adev);
78 static void amdgpu_dm_fini(struct amdgpu_device *adev);
79 
80 /* initializes drm_device display related structures, based on the information
81  * provided by DAL. The drm strcutures are: drm_crtc, drm_connector,
82  * drm_encoder, drm_mode_config
83  *
84  * Returns 0 on success
85  */
86 static int amdgpu_dm_initialize_drm_device(struct amdgpu_device *adev);
87 /* removes and deallocates the drm structures, created by the above function */
88 static void amdgpu_dm_destroy_drm_device(struct amdgpu_display_manager *dm);
89 
90 static void
91 amdgpu_dm_update_connector_after_detect(struct amdgpu_dm_connector *aconnector);
92 
93 static int amdgpu_dm_plane_init(struct amdgpu_display_manager *dm,
94 				struct amdgpu_plane *aplane,
95 				unsigned long possible_crtcs);
96 static int amdgpu_dm_crtc_init(struct amdgpu_display_manager *dm,
97 			       struct drm_plane *plane,
98 			       uint32_t link_index);
99 static int amdgpu_dm_connector_init(struct amdgpu_display_manager *dm,
100 				    struct amdgpu_dm_connector *amdgpu_dm_connector,
101 				    uint32_t link_index,
102 				    struct amdgpu_encoder *amdgpu_encoder);
103 static int amdgpu_dm_encoder_init(struct drm_device *dev,
104 				  struct amdgpu_encoder *aencoder,
105 				  uint32_t link_index);
106 
107 static int amdgpu_dm_connector_get_modes(struct drm_connector *connector);
108 
109 static int amdgpu_dm_atomic_commit(struct drm_device *dev,
110 				   struct drm_atomic_state *state,
111 				   bool nonblock);
112 
113 static void amdgpu_dm_atomic_commit_tail(struct drm_atomic_state *state);
114 
115 static int amdgpu_dm_atomic_check(struct drm_device *dev,
116 				  struct drm_atomic_state *state);
117 
118 
119 
120 
121 static const enum drm_plane_type dm_plane_type_default[AMDGPU_MAX_PLANES] = {
122 	DRM_PLANE_TYPE_PRIMARY,
123 	DRM_PLANE_TYPE_PRIMARY,
124 	DRM_PLANE_TYPE_PRIMARY,
125 	DRM_PLANE_TYPE_PRIMARY,
126 	DRM_PLANE_TYPE_PRIMARY,
127 	DRM_PLANE_TYPE_PRIMARY,
128 };
129 
130 static const enum drm_plane_type dm_plane_type_carizzo[AMDGPU_MAX_PLANES] = {
131 	DRM_PLANE_TYPE_PRIMARY,
132 	DRM_PLANE_TYPE_PRIMARY,
133 	DRM_PLANE_TYPE_PRIMARY,
134 	DRM_PLANE_TYPE_OVERLAY,/* YUV Capable Underlay */
135 };
136 
137 static const enum drm_plane_type dm_plane_type_stoney[AMDGPU_MAX_PLANES] = {
138 	DRM_PLANE_TYPE_PRIMARY,
139 	DRM_PLANE_TYPE_PRIMARY,
140 	DRM_PLANE_TYPE_OVERLAY, /* YUV Capable Underlay */
141 };
142 
143 /*
144  * dm_vblank_get_counter
145  *
146  * @brief
147  * Get counter for number of vertical blanks
148  *
149  * @param
150  * struct amdgpu_device *adev - [in] desired amdgpu device
151  * int disp_idx - [in] which CRTC to get the counter from
152  *
153  * @return
154  * Counter for vertical blanks
155  */
156 static u32 dm_vblank_get_counter(struct amdgpu_device *adev, int crtc)
157 {
158 	if (crtc >= adev->mode_info.num_crtc)
159 		return 0;
160 	else {
161 		struct amdgpu_crtc *acrtc = adev->mode_info.crtcs[crtc];
162 		struct dm_crtc_state *acrtc_state = to_dm_crtc_state(
163 				acrtc->base.state);
164 
165 
166 		if (acrtc_state->stream == NULL) {
167 			DRM_ERROR("dc_stream_state is NULL for crtc '%d'!\n",
168 				  crtc);
169 			return 0;
170 		}
171 
172 		return dc_stream_get_vblank_counter(acrtc_state->stream);
173 	}
174 }
175 
176 static int dm_crtc_get_scanoutpos(struct amdgpu_device *adev, int crtc,
177 				  u32 *vbl, u32 *position)
178 {
179 	uint32_t v_blank_start, v_blank_end, h_position, v_position;
180 
181 	if ((crtc < 0) || (crtc >= adev->mode_info.num_crtc))
182 		return -EINVAL;
183 	else {
184 		struct amdgpu_crtc *acrtc = adev->mode_info.crtcs[crtc];
185 		struct dm_crtc_state *acrtc_state = to_dm_crtc_state(
186 						acrtc->base.state);
187 
188 		if (acrtc_state->stream ==  NULL) {
189 			DRM_ERROR("dc_stream_state is NULL for crtc '%d'!\n",
190 				  crtc);
191 			return 0;
192 		}
193 
194 		/*
195 		 * TODO rework base driver to use values directly.
196 		 * for now parse it back into reg-format
197 		 */
198 		dc_stream_get_scanoutpos(acrtc_state->stream,
199 					 &v_blank_start,
200 					 &v_blank_end,
201 					 &h_position,
202 					 &v_position);
203 
204 		*position = v_position | (h_position << 16);
205 		*vbl = v_blank_start | (v_blank_end << 16);
206 	}
207 
208 	return 0;
209 }
210 
211 static bool dm_is_idle(void *handle)
212 {
213 	/* XXX todo */
214 	return true;
215 }
216 
217 static int dm_wait_for_idle(void *handle)
218 {
219 	/* XXX todo */
220 	return 0;
221 }
222 
223 static bool dm_check_soft_reset(void *handle)
224 {
225 	return false;
226 }
227 
228 static int dm_soft_reset(void *handle)
229 {
230 	/* XXX todo */
231 	return 0;
232 }
233 
234 static struct amdgpu_crtc *
235 get_crtc_by_otg_inst(struct amdgpu_device *adev,
236 		     int otg_inst)
237 {
238 	struct drm_device *dev = adev->ddev;
239 	struct drm_crtc *crtc;
240 	struct amdgpu_crtc *amdgpu_crtc;
241 
242 	/*
243 	 * following if is check inherited from both functions where this one is
244 	 * used now. Need to be checked why it could happen.
245 	 */
246 	if (otg_inst == -1) {
247 		WARN_ON(1);
248 		return adev->mode_info.crtcs[0];
249 	}
250 
251 	list_for_each_entry(crtc, &dev->mode_config.crtc_list, head) {
252 		amdgpu_crtc = to_amdgpu_crtc(crtc);
253 
254 		if (amdgpu_crtc->otg_inst == otg_inst)
255 			return amdgpu_crtc;
256 	}
257 
258 	return NULL;
259 }
260 
261 static void dm_pflip_high_irq(void *interrupt_params)
262 {
263 	struct amdgpu_crtc *amdgpu_crtc;
264 	struct common_irq_params *irq_params = interrupt_params;
265 	struct amdgpu_device *adev = irq_params->adev;
266 	unsigned long flags;
267 
268 	amdgpu_crtc = get_crtc_by_otg_inst(adev, irq_params->irq_src - IRQ_TYPE_PFLIP);
269 
270 	/* IRQ could occur when in initial stage */
271 	/*TODO work and BO cleanup */
272 	if (amdgpu_crtc == NULL) {
273 		DRM_DEBUG_DRIVER("CRTC is null, returning.\n");
274 		return;
275 	}
276 
277 	spin_lock_irqsave(&adev->ddev->event_lock, flags);
278 
279 	if (amdgpu_crtc->pflip_status != AMDGPU_FLIP_SUBMITTED){
280 		DRM_DEBUG_DRIVER("amdgpu_crtc->pflip_status = %d !=AMDGPU_FLIP_SUBMITTED(%d) on crtc:%d[%p] \n",
281 						 amdgpu_crtc->pflip_status,
282 						 AMDGPU_FLIP_SUBMITTED,
283 						 amdgpu_crtc->crtc_id,
284 						 amdgpu_crtc);
285 		spin_unlock_irqrestore(&adev->ddev->event_lock, flags);
286 		return;
287 	}
288 
289 
290 	/* wakeup usersapce */
291 	if (amdgpu_crtc->event) {
292 		/* Update to correct count/ts if racing with vblank irq */
293 		drm_crtc_accurate_vblank_count(&amdgpu_crtc->base);
294 
295 		drm_crtc_send_vblank_event(&amdgpu_crtc->base, amdgpu_crtc->event);
296 
297 		/* page flip completed. clean up */
298 		amdgpu_crtc->event = NULL;
299 
300 	} else
301 		WARN_ON(1);
302 
303 	amdgpu_crtc->pflip_status = AMDGPU_FLIP_NONE;
304 	spin_unlock_irqrestore(&adev->ddev->event_lock, flags);
305 
306 	DRM_DEBUG_DRIVER("%s - crtc :%d[%p], pflip_stat:AMDGPU_FLIP_NONE\n",
307 					__func__, amdgpu_crtc->crtc_id, amdgpu_crtc);
308 
309 	drm_crtc_vblank_put(&amdgpu_crtc->base);
310 }
311 
312 static void dm_crtc_high_irq(void *interrupt_params)
313 {
314 	struct common_irq_params *irq_params = interrupt_params;
315 	struct amdgpu_device *adev = irq_params->adev;
316 	uint8_t crtc_index = 0;
317 	struct amdgpu_crtc *acrtc;
318 
319 	acrtc = get_crtc_by_otg_inst(adev, irq_params->irq_src - IRQ_TYPE_VBLANK);
320 
321 	if (acrtc)
322 		crtc_index = acrtc->crtc_id;
323 
324 	drm_handle_vblank(adev->ddev, crtc_index);
325 	amdgpu_dm_crtc_handle_crc_irq(&acrtc->base);
326 }
327 
328 static int dm_set_clockgating_state(void *handle,
329 		  enum amd_clockgating_state state)
330 {
331 	return 0;
332 }
333 
334 static int dm_set_powergating_state(void *handle,
335 		  enum amd_powergating_state state)
336 {
337 	return 0;
338 }
339 
340 /* Prototypes of private functions */
341 static int dm_early_init(void* handle);
342 
343 static void hotplug_notify_work_func(struct work_struct *work)
344 {
345 	struct amdgpu_display_manager *dm = container_of(work, struct amdgpu_display_manager, mst_hotplug_work);
346 	struct drm_device *dev = dm->ddev;
347 
348 	drm_kms_helper_hotplug_event(dev);
349 }
350 
351 /* Allocate memory for FBC compressed data  */
352 static void amdgpu_dm_fbc_init(struct drm_connector *connector)
353 {
354 	struct drm_device *dev = connector->dev;
355 	struct amdgpu_device *adev = dev->dev_private;
356 	struct dm_comressor_info *compressor = &adev->dm.compressor;
357 	struct amdgpu_dm_connector *aconn = to_amdgpu_dm_connector(connector);
358 	struct drm_display_mode *mode;
359 	unsigned long max_size = 0;
360 
361 	if (adev->dm.dc->fbc_compressor == NULL)
362 		return;
363 
364 	if (aconn->dc_link->connector_signal != SIGNAL_TYPE_EDP)
365 		return;
366 
367 	if (compressor->bo_ptr)
368 		return;
369 
370 
371 	list_for_each_entry(mode, &connector->modes, head) {
372 		if (max_size < mode->htotal * mode->vtotal)
373 			max_size = mode->htotal * mode->vtotal;
374 	}
375 
376 	if (max_size) {
377 		int r = amdgpu_bo_create_kernel(adev, max_size * 4, PAGE_SIZE,
378 			    AMDGPU_GEM_DOMAIN_GTT, &compressor->bo_ptr,
379 			    &compressor->gpu_addr, &compressor->cpu_addr);
380 
381 		if (r)
382 			DRM_ERROR("DM: Failed to initialize FBC\n");
383 		else {
384 			adev->dm.dc->ctx->fbc_gpu_addr = compressor->gpu_addr;
385 			DRM_INFO("DM: FBC alloc %lu\n", max_size*4);
386 		}
387 
388 	}
389 
390 }
391 
392 
393 /* Init display KMS
394  *
395  * Returns 0 on success
396  */
397 static int amdgpu_dm_init(struct amdgpu_device *adev)
398 {
399 	struct dc_init_data init_data;
400 	adev->dm.ddev = adev->ddev;
401 	adev->dm.adev = adev;
402 
403 	/* Zero all the fields */
404 	memset(&init_data, 0, sizeof(init_data));
405 
406 	if(amdgpu_dm_irq_init(adev)) {
407 		DRM_ERROR("amdgpu: failed to initialize DM IRQ support.\n");
408 		goto error;
409 	}
410 
411 	init_data.asic_id.chip_family = adev->family;
412 
413 	init_data.asic_id.pci_revision_id = adev->rev_id;
414 	init_data.asic_id.hw_internal_rev = adev->external_rev_id;
415 	init_data.asic_id.chip_id = adev->pdev->device;
416 
417 	init_data.asic_id.vram_width = adev->gmc.vram_width;
418 	/* TODO: initialize init_data.asic_id.vram_type here!!!! */
419 	init_data.asic_id.atombios_base_address =
420 		adev->mode_info.atom_context->bios;
421 
422 	init_data.driver = adev;
423 
424 	adev->dm.cgs_device = amdgpu_cgs_create_device(adev);
425 
426 	if (!adev->dm.cgs_device) {
427 		DRM_ERROR("amdgpu: failed to create cgs device.\n");
428 		goto error;
429 	}
430 
431 	init_data.cgs_device = adev->dm.cgs_device;
432 
433 	adev->dm.dal = NULL;
434 
435 	init_data.dce_environment = DCE_ENV_PRODUCTION_DRV;
436 
437 	/*
438 	 * TODO debug why this doesn't work on Raven
439 	 */
440 	if (adev->flags & AMD_IS_APU &&
441 	    adev->asic_type >= CHIP_CARRIZO &&
442 	    adev->asic_type < CHIP_RAVEN)
443 		init_data.flags.gpu_vm_support = true;
444 
445 	/* Display Core create. */
446 	adev->dm.dc = dc_create(&init_data);
447 
448 	if (adev->dm.dc) {
449 		DRM_INFO("Display Core initialized with v%s!\n", DC_VER);
450 	} else {
451 		DRM_INFO("Display Core failed to initialize with v%s!\n", DC_VER);
452 		goto error;
453 	}
454 
455 	INIT_WORK(&adev->dm.mst_hotplug_work, hotplug_notify_work_func);
456 
457 	adev->dm.freesync_module = mod_freesync_create(adev->dm.dc);
458 	if (!adev->dm.freesync_module) {
459 		DRM_ERROR(
460 		"amdgpu: failed to initialize freesync_module.\n");
461 	} else
462 		DRM_DEBUG_DRIVER("amdgpu: freesync_module init done %p.\n",
463 				adev->dm.freesync_module);
464 
465 	amdgpu_dm_init_color_mod();
466 
467 	if (amdgpu_dm_initialize_drm_device(adev)) {
468 		DRM_ERROR(
469 		"amdgpu: failed to initialize sw for display support.\n");
470 		goto error;
471 	}
472 
473 	/* Update the actual used number of crtc */
474 	adev->mode_info.num_crtc = adev->dm.display_indexes_num;
475 
476 	/* TODO: Add_display_info? */
477 
478 	/* TODO use dynamic cursor width */
479 	adev->ddev->mode_config.cursor_width = adev->dm.dc->caps.max_cursor_size;
480 	adev->ddev->mode_config.cursor_height = adev->dm.dc->caps.max_cursor_size;
481 
482 	if (drm_vblank_init(adev->ddev, adev->dm.display_indexes_num)) {
483 		DRM_ERROR(
484 		"amdgpu: failed to initialize sw for display support.\n");
485 		goto error;
486 	}
487 
488 	DRM_DEBUG_DRIVER("KMS initialized.\n");
489 
490 	return 0;
491 error:
492 	amdgpu_dm_fini(adev);
493 
494 	return -1;
495 }
496 
497 static void amdgpu_dm_fini(struct amdgpu_device *adev)
498 {
499 	amdgpu_dm_destroy_drm_device(&adev->dm);
500 	/*
501 	 * TODO: pageflip, vlank interrupt
502 	 *
503 	 * amdgpu_dm_irq_fini(adev);
504 	 */
505 
506 	if (adev->dm.cgs_device) {
507 		amdgpu_cgs_destroy_device(adev->dm.cgs_device);
508 		adev->dm.cgs_device = NULL;
509 	}
510 	if (adev->dm.freesync_module) {
511 		mod_freesync_destroy(adev->dm.freesync_module);
512 		adev->dm.freesync_module = NULL;
513 	}
514 	/* DC Destroy TODO: Replace destroy DAL */
515 	if (adev->dm.dc)
516 		dc_destroy(&adev->dm.dc);
517 	return;
518 }
519 
520 static int dm_sw_init(void *handle)
521 {
522 	return 0;
523 }
524 
525 static int dm_sw_fini(void *handle)
526 {
527 	return 0;
528 }
529 
530 static int detect_mst_link_for_all_connectors(struct drm_device *dev)
531 {
532 	struct amdgpu_dm_connector *aconnector;
533 	struct drm_connector *connector;
534 	int ret = 0;
535 
536 	drm_modeset_lock(&dev->mode_config.connection_mutex, NULL);
537 
538 	list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
539 		aconnector = to_amdgpu_dm_connector(connector);
540 		if (aconnector->dc_link->type == dc_connection_mst_branch &&
541 		    aconnector->mst_mgr.aux) {
542 			DRM_DEBUG_DRIVER("DM_MST: starting TM on aconnector: %p [id: %d]\n",
543 					aconnector, aconnector->base.base.id);
544 
545 			ret = drm_dp_mst_topology_mgr_set_mst(&aconnector->mst_mgr, true);
546 			if (ret < 0) {
547 				DRM_ERROR("DM_MST: Failed to start MST\n");
548 				((struct dc_link *)aconnector->dc_link)->type = dc_connection_single;
549 				return ret;
550 				}
551 			}
552 	}
553 
554 	drm_modeset_unlock(&dev->mode_config.connection_mutex);
555 	return ret;
556 }
557 
558 static int dm_late_init(void *handle)
559 {
560 	struct amdgpu_device *adev = (struct amdgpu_device *)handle;
561 
562 	return detect_mst_link_for_all_connectors(adev->ddev);
563 }
564 
565 static void s3_handle_mst(struct drm_device *dev, bool suspend)
566 {
567 	struct amdgpu_dm_connector *aconnector;
568 	struct drm_connector *connector;
569 	struct drm_dp_mst_topology_mgr *mgr;
570 	int ret;
571 	bool need_hotplug = false;
572 
573 	drm_modeset_lock(&dev->mode_config.connection_mutex, NULL);
574 
575 	list_for_each_entry(connector, &dev->mode_config.connector_list,
576 			    head) {
577 		aconnector = to_amdgpu_dm_connector(connector);
578 		if (aconnector->dc_link->type != dc_connection_mst_branch ||
579 		    aconnector->mst_port)
580 			continue;
581 
582 		mgr = &aconnector->mst_mgr;
583 
584 		if (suspend) {
585 			drm_dp_mst_topology_mgr_suspend(mgr);
586 		} else {
587 			ret = drm_dp_mst_topology_mgr_resume(mgr);
588 			if (ret < 0) {
589 				drm_dp_mst_topology_mgr_set_mst(mgr, false);
590 				need_hotplug = true;
591 			}
592 		}
593 	}
594 
595 	drm_modeset_unlock(&dev->mode_config.connection_mutex);
596 
597 	if (need_hotplug)
598 		drm_kms_helper_hotplug_event(dev);
599 }
600 
601 static int dm_hw_init(void *handle)
602 {
603 	struct amdgpu_device *adev = (struct amdgpu_device *)handle;
604 	/* Create DAL display manager */
605 	amdgpu_dm_init(adev);
606 	amdgpu_dm_hpd_init(adev);
607 
608 	return 0;
609 }
610 
611 static int dm_hw_fini(void *handle)
612 {
613 	struct amdgpu_device *adev = (struct amdgpu_device *)handle;
614 
615 	amdgpu_dm_hpd_fini(adev);
616 
617 	amdgpu_dm_irq_fini(adev);
618 	amdgpu_dm_fini(adev);
619 	return 0;
620 }
621 
622 static int dm_suspend(void *handle)
623 {
624 	struct amdgpu_device *adev = handle;
625 	struct amdgpu_display_manager *dm = &adev->dm;
626 	int ret = 0;
627 
628 	WARN_ON(adev->dm.cached_state);
629 	adev->dm.cached_state = drm_atomic_helper_suspend(adev->ddev);
630 
631 	s3_handle_mst(adev->ddev, true);
632 
633 	amdgpu_dm_irq_suspend(adev);
634 
635 
636 	dc_set_power_state(dm->dc, DC_ACPI_CM_POWER_STATE_D3);
637 
638 	return ret;
639 }
640 
641 static struct amdgpu_dm_connector *
642 amdgpu_dm_find_first_crtc_matching_connector(struct drm_atomic_state *state,
643 					     struct drm_crtc *crtc)
644 {
645 	uint32_t i;
646 	struct drm_connector_state *new_con_state;
647 	struct drm_connector *connector;
648 	struct drm_crtc *crtc_from_state;
649 
650 	for_each_new_connector_in_state(state, connector, new_con_state, i) {
651 		crtc_from_state = new_con_state->crtc;
652 
653 		if (crtc_from_state == crtc)
654 			return to_amdgpu_dm_connector(connector);
655 	}
656 
657 	return NULL;
658 }
659 
660 static void emulated_link_detect(struct dc_link *link)
661 {
662 	struct dc_sink_init_data sink_init_data = { 0 };
663 	struct display_sink_capability sink_caps = { 0 };
664 	enum dc_edid_status edid_status;
665 	struct dc_context *dc_ctx = link->ctx;
666 	struct dc_sink *sink = NULL;
667 	struct dc_sink *prev_sink = NULL;
668 
669 	link->type = dc_connection_none;
670 	prev_sink = link->local_sink;
671 
672 	if (prev_sink)
673 		dc_sink_release(prev_sink);
674 
675 	switch (link->connector_signal) {
676 	case SIGNAL_TYPE_HDMI_TYPE_A: {
677 		sink_caps.transaction_type = DDC_TRANSACTION_TYPE_I2C;
678 		sink_caps.signal = SIGNAL_TYPE_HDMI_TYPE_A;
679 		break;
680 	}
681 
682 	case SIGNAL_TYPE_DVI_SINGLE_LINK: {
683 		sink_caps.transaction_type = DDC_TRANSACTION_TYPE_I2C;
684 		sink_caps.signal = SIGNAL_TYPE_DVI_SINGLE_LINK;
685 		break;
686 	}
687 
688 	case SIGNAL_TYPE_DVI_DUAL_LINK: {
689 		sink_caps.transaction_type = DDC_TRANSACTION_TYPE_I2C;
690 		sink_caps.signal = SIGNAL_TYPE_DVI_DUAL_LINK;
691 		break;
692 	}
693 
694 	case SIGNAL_TYPE_LVDS: {
695 		sink_caps.transaction_type = DDC_TRANSACTION_TYPE_I2C;
696 		sink_caps.signal = SIGNAL_TYPE_LVDS;
697 		break;
698 	}
699 
700 	case SIGNAL_TYPE_EDP: {
701 		sink_caps.transaction_type =
702 			DDC_TRANSACTION_TYPE_I2C_OVER_AUX;
703 		sink_caps.signal = SIGNAL_TYPE_EDP;
704 		break;
705 	}
706 
707 	case SIGNAL_TYPE_DISPLAY_PORT: {
708 		sink_caps.transaction_type =
709 			DDC_TRANSACTION_TYPE_I2C_OVER_AUX;
710 		sink_caps.signal = SIGNAL_TYPE_VIRTUAL;
711 		break;
712 	}
713 
714 	default:
715 		DC_ERROR("Invalid connector type! signal:%d\n",
716 			link->connector_signal);
717 		return;
718 	}
719 
720 	sink_init_data.link = link;
721 	sink_init_data.sink_signal = sink_caps.signal;
722 
723 	sink = dc_sink_create(&sink_init_data);
724 	if (!sink) {
725 		DC_ERROR("Failed to create sink!\n");
726 		return;
727 	}
728 
729 	link->local_sink = sink;
730 
731 	edid_status = dm_helpers_read_local_edid(
732 			link->ctx,
733 			link,
734 			sink);
735 
736 	if (edid_status != EDID_OK)
737 		DC_ERROR("Failed to read EDID");
738 
739 }
740 
741 static int dm_resume(void *handle)
742 {
743 	struct amdgpu_device *adev = handle;
744 	struct drm_device *ddev = adev->ddev;
745 	struct amdgpu_display_manager *dm = &adev->dm;
746 	struct amdgpu_dm_connector *aconnector;
747 	struct drm_connector *connector;
748 	struct drm_crtc *crtc;
749 	struct drm_crtc_state *new_crtc_state;
750 	struct dm_crtc_state *dm_new_crtc_state;
751 	struct drm_plane *plane;
752 	struct drm_plane_state *new_plane_state;
753 	struct dm_plane_state *dm_new_plane_state;
754 	enum dc_connection_type new_connection_type = dc_connection_none;
755 	int i;
756 
757 	/* power on hardware */
758 	dc_set_power_state(dm->dc, DC_ACPI_CM_POWER_STATE_D0);
759 
760 	/* program HPD filter */
761 	dc_resume(dm->dc);
762 
763 	/* On resume we need to  rewrite the MSTM control bits to enamble MST*/
764 	s3_handle_mst(ddev, false);
765 
766 	/*
767 	 * early enable HPD Rx IRQ, should be done before set mode as short
768 	 * pulse interrupts are used for MST
769 	 */
770 	amdgpu_dm_irq_resume_early(adev);
771 
772 	/* Do detection*/
773 	list_for_each_entry(connector, &ddev->mode_config.connector_list, head) {
774 		aconnector = to_amdgpu_dm_connector(connector);
775 
776 		/*
777 		 * this is the case when traversing through already created
778 		 * MST connectors, should be skipped
779 		 */
780 		if (aconnector->mst_port)
781 			continue;
782 
783 		mutex_lock(&aconnector->hpd_lock);
784 		if (!dc_link_detect_sink(aconnector->dc_link, &new_connection_type))
785 			DRM_ERROR("KMS: Failed to detect connector\n");
786 
787 		if (aconnector->base.force && new_connection_type == dc_connection_none)
788 			emulated_link_detect(aconnector->dc_link);
789 		else
790 			dc_link_detect(aconnector->dc_link, DETECT_REASON_HPD);
791 
792 		if (aconnector->fake_enable && aconnector->dc_link->local_sink)
793 			aconnector->fake_enable = false;
794 
795 		aconnector->dc_sink = NULL;
796 		amdgpu_dm_update_connector_after_detect(aconnector);
797 		mutex_unlock(&aconnector->hpd_lock);
798 	}
799 
800 	/* Force mode set in atomic comit */
801 	for_each_new_crtc_in_state(dm->cached_state, crtc, new_crtc_state, i)
802 		new_crtc_state->active_changed = true;
803 
804 	/*
805 	 * atomic_check is expected to create the dc states. We need to release
806 	 * them here, since they were duplicated as part of the suspend
807 	 * procedure.
808 	 */
809 	for_each_new_crtc_in_state(dm->cached_state, crtc, new_crtc_state, i) {
810 		dm_new_crtc_state = to_dm_crtc_state(new_crtc_state);
811 		if (dm_new_crtc_state->stream) {
812 			WARN_ON(kref_read(&dm_new_crtc_state->stream->refcount) > 1);
813 			dc_stream_release(dm_new_crtc_state->stream);
814 			dm_new_crtc_state->stream = NULL;
815 		}
816 	}
817 
818 	for_each_new_plane_in_state(dm->cached_state, plane, new_plane_state, i) {
819 		dm_new_plane_state = to_dm_plane_state(new_plane_state);
820 		if (dm_new_plane_state->dc_state) {
821 			WARN_ON(kref_read(&dm_new_plane_state->dc_state->refcount) > 1);
822 			dc_plane_state_release(dm_new_plane_state->dc_state);
823 			dm_new_plane_state->dc_state = NULL;
824 		}
825 	}
826 
827 	drm_atomic_helper_resume(ddev, dm->cached_state);
828 
829 	dm->cached_state = NULL;
830 
831 	amdgpu_dm_irq_resume_late(adev);
832 
833 	return 0;
834 }
835 
836 static const struct amd_ip_funcs amdgpu_dm_funcs = {
837 	.name = "dm",
838 	.early_init = dm_early_init,
839 	.late_init = dm_late_init,
840 	.sw_init = dm_sw_init,
841 	.sw_fini = dm_sw_fini,
842 	.hw_init = dm_hw_init,
843 	.hw_fini = dm_hw_fini,
844 	.suspend = dm_suspend,
845 	.resume = dm_resume,
846 	.is_idle = dm_is_idle,
847 	.wait_for_idle = dm_wait_for_idle,
848 	.check_soft_reset = dm_check_soft_reset,
849 	.soft_reset = dm_soft_reset,
850 	.set_clockgating_state = dm_set_clockgating_state,
851 	.set_powergating_state = dm_set_powergating_state,
852 };
853 
854 const struct amdgpu_ip_block_version dm_ip_block =
855 {
856 	.type = AMD_IP_BLOCK_TYPE_DCE,
857 	.major = 1,
858 	.minor = 0,
859 	.rev = 0,
860 	.funcs = &amdgpu_dm_funcs,
861 };
862 
863 
864 static struct drm_atomic_state *
865 dm_atomic_state_alloc(struct drm_device *dev)
866 {
867 	struct dm_atomic_state *state = kzalloc(sizeof(*state), GFP_KERNEL);
868 
869 	if (!state)
870 		return NULL;
871 
872 	if (drm_atomic_state_init(dev, &state->base) < 0)
873 		goto fail;
874 
875 	return &state->base;
876 
877 fail:
878 	kfree(state);
879 	return NULL;
880 }
881 
882 static void
883 dm_atomic_state_clear(struct drm_atomic_state *state)
884 {
885 	struct dm_atomic_state *dm_state = to_dm_atomic_state(state);
886 
887 	if (dm_state->context) {
888 		dc_release_state(dm_state->context);
889 		dm_state->context = NULL;
890 	}
891 
892 	drm_atomic_state_default_clear(state);
893 }
894 
895 static void
896 dm_atomic_state_alloc_free(struct drm_atomic_state *state)
897 {
898 	struct dm_atomic_state *dm_state = to_dm_atomic_state(state);
899 	drm_atomic_state_default_release(state);
900 	kfree(dm_state);
901 }
902 
903 static const struct drm_mode_config_funcs amdgpu_dm_mode_funcs = {
904 	.fb_create = amdgpu_display_user_framebuffer_create,
905 	.output_poll_changed = drm_fb_helper_output_poll_changed,
906 	.atomic_check = amdgpu_dm_atomic_check,
907 	.atomic_commit = amdgpu_dm_atomic_commit,
908 	.atomic_state_alloc = dm_atomic_state_alloc,
909 	.atomic_state_clear = dm_atomic_state_clear,
910 	.atomic_state_free = dm_atomic_state_alloc_free
911 };
912 
913 static struct drm_mode_config_helper_funcs amdgpu_dm_mode_config_helperfuncs = {
914 	.atomic_commit_tail = amdgpu_dm_atomic_commit_tail
915 };
916 
917 static void
918 amdgpu_dm_update_connector_after_detect(struct amdgpu_dm_connector *aconnector)
919 {
920 	struct drm_connector *connector = &aconnector->base;
921 	struct drm_device *dev = connector->dev;
922 	struct dc_sink *sink;
923 
924 	/* MST handled by drm_mst framework */
925 	if (aconnector->mst_mgr.mst_state == true)
926 		return;
927 
928 
929 	sink = aconnector->dc_link->local_sink;
930 
931 	/* Edid mgmt connector gets first update only in mode_valid hook and then
932 	 * the connector sink is set to either fake or physical sink depends on link status.
933 	 * don't do it here if u are during boot
934 	 */
935 	if (aconnector->base.force != DRM_FORCE_UNSPECIFIED
936 			&& aconnector->dc_em_sink) {
937 
938 		/* For S3 resume with headless use eml_sink to fake stream
939 		 * because on resume connecotr->sink is set ti NULL
940 		 */
941 		mutex_lock(&dev->mode_config.mutex);
942 
943 		if (sink) {
944 			if (aconnector->dc_sink) {
945 				amdgpu_dm_remove_sink_from_freesync_module(
946 								connector);
947 				/* retain and release bellow are used for
948 				 * bump up refcount for sink because the link don't point
949 				 * to it anymore after disconnect so on next crtc to connector
950 				 * reshuffle by UMD we will get into unwanted dc_sink release
951 				 */
952 				if (aconnector->dc_sink != aconnector->dc_em_sink)
953 					dc_sink_release(aconnector->dc_sink);
954 			}
955 			aconnector->dc_sink = sink;
956 			amdgpu_dm_add_sink_to_freesync_module(
957 						connector, aconnector->edid);
958 		} else {
959 			amdgpu_dm_remove_sink_from_freesync_module(connector);
960 			if (!aconnector->dc_sink)
961 				aconnector->dc_sink = aconnector->dc_em_sink;
962 			else if (aconnector->dc_sink != aconnector->dc_em_sink)
963 				dc_sink_retain(aconnector->dc_sink);
964 		}
965 
966 		mutex_unlock(&dev->mode_config.mutex);
967 		return;
968 	}
969 
970 	/*
971 	 * TODO: temporary guard to look for proper fix
972 	 * if this sink is MST sink, we should not do anything
973 	 */
974 	if (sink && sink->sink_signal == SIGNAL_TYPE_DISPLAY_PORT_MST)
975 		return;
976 
977 	if (aconnector->dc_sink == sink) {
978 		/* We got a DP short pulse (Link Loss, DP CTS, etc...).
979 		 * Do nothing!! */
980 		DRM_DEBUG_DRIVER("DCHPD: connector_id=%d: dc_sink didn't change.\n",
981 				aconnector->connector_id);
982 		return;
983 	}
984 
985 	DRM_DEBUG_DRIVER("DCHPD: connector_id=%d: Old sink=%p New sink=%p\n",
986 		aconnector->connector_id, aconnector->dc_sink, sink);
987 
988 	mutex_lock(&dev->mode_config.mutex);
989 
990 	/* 1. Update status of the drm connector
991 	 * 2. Send an event and let userspace tell us what to do */
992 	if (sink) {
993 		/* TODO: check if we still need the S3 mode update workaround.
994 		 * If yes, put it here. */
995 		if (aconnector->dc_sink)
996 			amdgpu_dm_remove_sink_from_freesync_module(
997 							connector);
998 
999 		aconnector->dc_sink = sink;
1000 		if (sink->dc_edid.length == 0) {
1001 			aconnector->edid = NULL;
1002 		} else {
1003 			aconnector->edid =
1004 				(struct edid *) sink->dc_edid.raw_edid;
1005 
1006 
1007 			drm_connector_update_edid_property(connector,
1008 					aconnector->edid);
1009 		}
1010 		amdgpu_dm_add_sink_to_freesync_module(connector, aconnector->edid);
1011 
1012 	} else {
1013 		amdgpu_dm_remove_sink_from_freesync_module(connector);
1014 		drm_connector_update_edid_property(connector, NULL);
1015 		aconnector->num_modes = 0;
1016 		aconnector->dc_sink = NULL;
1017 		aconnector->edid = NULL;
1018 	}
1019 
1020 	mutex_unlock(&dev->mode_config.mutex);
1021 }
1022 
1023 static void handle_hpd_irq(void *param)
1024 {
1025 	struct amdgpu_dm_connector *aconnector = (struct amdgpu_dm_connector *)param;
1026 	struct drm_connector *connector = &aconnector->base;
1027 	struct drm_device *dev = connector->dev;
1028 	enum dc_connection_type new_connection_type = dc_connection_none;
1029 
1030 	/* In case of failure or MST no need to update connector status or notify the OS
1031 	 * since (for MST case) MST does this in it's own context.
1032 	 */
1033 	mutex_lock(&aconnector->hpd_lock);
1034 
1035 	if (aconnector->fake_enable)
1036 		aconnector->fake_enable = false;
1037 
1038 	if (!dc_link_detect_sink(aconnector->dc_link, &new_connection_type))
1039 		DRM_ERROR("KMS: Failed to detect connector\n");
1040 
1041 	if (aconnector->base.force && new_connection_type == dc_connection_none) {
1042 		emulated_link_detect(aconnector->dc_link);
1043 
1044 
1045 		drm_modeset_lock_all(dev);
1046 		dm_restore_drm_connector_state(dev, connector);
1047 		drm_modeset_unlock_all(dev);
1048 
1049 		if (aconnector->base.force == DRM_FORCE_UNSPECIFIED)
1050 			drm_kms_helper_hotplug_event(dev);
1051 
1052 	} else if (dc_link_detect(aconnector->dc_link, DETECT_REASON_HPD)) {
1053 		amdgpu_dm_update_connector_after_detect(aconnector);
1054 
1055 
1056 		drm_modeset_lock_all(dev);
1057 		dm_restore_drm_connector_state(dev, connector);
1058 		drm_modeset_unlock_all(dev);
1059 
1060 		if (aconnector->base.force == DRM_FORCE_UNSPECIFIED)
1061 			drm_kms_helper_hotplug_event(dev);
1062 	}
1063 	mutex_unlock(&aconnector->hpd_lock);
1064 
1065 }
1066 
1067 static void dm_handle_hpd_rx_irq(struct amdgpu_dm_connector *aconnector)
1068 {
1069 	uint8_t esi[DP_PSR_ERROR_STATUS - DP_SINK_COUNT_ESI] = { 0 };
1070 	uint8_t dret;
1071 	bool new_irq_handled = false;
1072 	int dpcd_addr;
1073 	int dpcd_bytes_to_read;
1074 
1075 	const int max_process_count = 30;
1076 	int process_count = 0;
1077 
1078 	const struct dc_link_status *link_status = dc_link_get_status(aconnector->dc_link);
1079 
1080 	if (link_status->dpcd_caps->dpcd_rev.raw < 0x12) {
1081 		dpcd_bytes_to_read = DP_LANE0_1_STATUS - DP_SINK_COUNT;
1082 		/* DPCD 0x200 - 0x201 for downstream IRQ */
1083 		dpcd_addr = DP_SINK_COUNT;
1084 	} else {
1085 		dpcd_bytes_to_read = DP_PSR_ERROR_STATUS - DP_SINK_COUNT_ESI;
1086 		/* DPCD 0x2002 - 0x2005 for downstream IRQ */
1087 		dpcd_addr = DP_SINK_COUNT_ESI;
1088 	}
1089 
1090 	dret = drm_dp_dpcd_read(
1091 		&aconnector->dm_dp_aux.aux,
1092 		dpcd_addr,
1093 		esi,
1094 		dpcd_bytes_to_read);
1095 
1096 	while (dret == dpcd_bytes_to_read &&
1097 		process_count < max_process_count) {
1098 		uint8_t retry;
1099 		dret = 0;
1100 
1101 		process_count++;
1102 
1103 		DRM_DEBUG_DRIVER("ESI %02x %02x %02x\n", esi[0], esi[1], esi[2]);
1104 		/* handle HPD short pulse irq */
1105 		if (aconnector->mst_mgr.mst_state)
1106 			drm_dp_mst_hpd_irq(
1107 				&aconnector->mst_mgr,
1108 				esi,
1109 				&new_irq_handled);
1110 
1111 		if (new_irq_handled) {
1112 			/* ACK at DPCD to notify down stream */
1113 			const int ack_dpcd_bytes_to_write =
1114 				dpcd_bytes_to_read - 1;
1115 
1116 			for (retry = 0; retry < 3; retry++) {
1117 				uint8_t wret;
1118 
1119 				wret = drm_dp_dpcd_write(
1120 					&aconnector->dm_dp_aux.aux,
1121 					dpcd_addr + 1,
1122 					&esi[1],
1123 					ack_dpcd_bytes_to_write);
1124 				if (wret == ack_dpcd_bytes_to_write)
1125 					break;
1126 			}
1127 
1128 			/* check if there is new irq to be handle */
1129 			dret = drm_dp_dpcd_read(
1130 				&aconnector->dm_dp_aux.aux,
1131 				dpcd_addr,
1132 				esi,
1133 				dpcd_bytes_to_read);
1134 
1135 			new_irq_handled = false;
1136 		} else {
1137 			break;
1138 		}
1139 	}
1140 
1141 	if (process_count == max_process_count)
1142 		DRM_DEBUG_DRIVER("Loop exceeded max iterations\n");
1143 }
1144 
1145 static void handle_hpd_rx_irq(void *param)
1146 {
1147 	struct amdgpu_dm_connector *aconnector = (struct amdgpu_dm_connector *)param;
1148 	struct drm_connector *connector = &aconnector->base;
1149 	struct drm_device *dev = connector->dev;
1150 	struct dc_link *dc_link = aconnector->dc_link;
1151 	bool is_mst_root_connector = aconnector->mst_mgr.mst_state;
1152 	enum dc_connection_type new_connection_type = dc_connection_none;
1153 
1154 	/* TODO:Temporary add mutex to protect hpd interrupt not have a gpio
1155 	 * conflict, after implement i2c helper, this mutex should be
1156 	 * retired.
1157 	 */
1158 	if (dc_link->type != dc_connection_mst_branch)
1159 		mutex_lock(&aconnector->hpd_lock);
1160 
1161 	if (dc_link_handle_hpd_rx_irq(dc_link, NULL, NULL) &&
1162 			!is_mst_root_connector) {
1163 		/* Downstream Port status changed. */
1164 		if (!dc_link_detect_sink(dc_link, &new_connection_type))
1165 			DRM_ERROR("KMS: Failed to detect connector\n");
1166 
1167 		if (aconnector->base.force && new_connection_type == dc_connection_none) {
1168 			emulated_link_detect(dc_link);
1169 
1170 			if (aconnector->fake_enable)
1171 				aconnector->fake_enable = false;
1172 
1173 			amdgpu_dm_update_connector_after_detect(aconnector);
1174 
1175 
1176 			drm_modeset_lock_all(dev);
1177 			dm_restore_drm_connector_state(dev, connector);
1178 			drm_modeset_unlock_all(dev);
1179 
1180 			drm_kms_helper_hotplug_event(dev);
1181 		} else if (dc_link_detect(dc_link, DETECT_REASON_HPDRX)) {
1182 
1183 			if (aconnector->fake_enable)
1184 				aconnector->fake_enable = false;
1185 
1186 			amdgpu_dm_update_connector_after_detect(aconnector);
1187 
1188 
1189 			drm_modeset_lock_all(dev);
1190 			dm_restore_drm_connector_state(dev, connector);
1191 			drm_modeset_unlock_all(dev);
1192 
1193 			drm_kms_helper_hotplug_event(dev);
1194 		}
1195 	}
1196 	if ((dc_link->cur_link_settings.lane_count != LANE_COUNT_UNKNOWN) ||
1197 	    (dc_link->type == dc_connection_mst_branch))
1198 		dm_handle_hpd_rx_irq(aconnector);
1199 
1200 	if (dc_link->type != dc_connection_mst_branch)
1201 		mutex_unlock(&aconnector->hpd_lock);
1202 }
1203 
1204 static void register_hpd_handlers(struct amdgpu_device *adev)
1205 {
1206 	struct drm_device *dev = adev->ddev;
1207 	struct drm_connector *connector;
1208 	struct amdgpu_dm_connector *aconnector;
1209 	const struct dc_link *dc_link;
1210 	struct dc_interrupt_params int_params = {0};
1211 
1212 	int_params.requested_polarity = INTERRUPT_POLARITY_DEFAULT;
1213 	int_params.current_polarity = INTERRUPT_POLARITY_DEFAULT;
1214 
1215 	list_for_each_entry(connector,
1216 			&dev->mode_config.connector_list, head)	{
1217 
1218 		aconnector = to_amdgpu_dm_connector(connector);
1219 		dc_link = aconnector->dc_link;
1220 
1221 		if (DC_IRQ_SOURCE_INVALID != dc_link->irq_source_hpd) {
1222 			int_params.int_context = INTERRUPT_LOW_IRQ_CONTEXT;
1223 			int_params.irq_source = dc_link->irq_source_hpd;
1224 
1225 			amdgpu_dm_irq_register_interrupt(adev, &int_params,
1226 					handle_hpd_irq,
1227 					(void *) aconnector);
1228 		}
1229 
1230 		if (DC_IRQ_SOURCE_INVALID != dc_link->irq_source_hpd_rx) {
1231 
1232 			/* Also register for DP short pulse (hpd_rx). */
1233 			int_params.int_context = INTERRUPT_LOW_IRQ_CONTEXT;
1234 			int_params.irq_source =	dc_link->irq_source_hpd_rx;
1235 
1236 			amdgpu_dm_irq_register_interrupt(adev, &int_params,
1237 					handle_hpd_rx_irq,
1238 					(void *) aconnector);
1239 		}
1240 	}
1241 }
1242 
1243 /* Register IRQ sources and initialize IRQ callbacks */
1244 static int dce110_register_irq_handlers(struct amdgpu_device *adev)
1245 {
1246 	struct dc *dc = adev->dm.dc;
1247 	struct common_irq_params *c_irq_params;
1248 	struct dc_interrupt_params int_params = {0};
1249 	int r;
1250 	int i;
1251 	unsigned client_id = AMDGPU_IH_CLIENTID_LEGACY;
1252 
1253 	if (adev->asic_type == CHIP_VEGA10 ||
1254 	    adev->asic_type == CHIP_VEGA12 ||
1255 	    adev->asic_type == CHIP_VEGA20 ||
1256 	    adev->asic_type == CHIP_RAVEN)
1257 		client_id = SOC15_IH_CLIENTID_DCE;
1258 
1259 	int_params.requested_polarity = INTERRUPT_POLARITY_DEFAULT;
1260 	int_params.current_polarity = INTERRUPT_POLARITY_DEFAULT;
1261 
1262 	/* Actions of amdgpu_irq_add_id():
1263 	 * 1. Register a set() function with base driver.
1264 	 *    Base driver will call set() function to enable/disable an
1265 	 *    interrupt in DC hardware.
1266 	 * 2. Register amdgpu_dm_irq_handler().
1267 	 *    Base driver will call amdgpu_dm_irq_handler() for ALL interrupts
1268 	 *    coming from DC hardware.
1269 	 *    amdgpu_dm_irq_handler() will re-direct the interrupt to DC
1270 	 *    for acknowledging and handling. */
1271 
1272 	/* Use VBLANK interrupt */
1273 	for (i = VISLANDS30_IV_SRCID_D1_VERTICAL_INTERRUPT0; i <= VISLANDS30_IV_SRCID_D6_VERTICAL_INTERRUPT0; i++) {
1274 		r = amdgpu_irq_add_id(adev, client_id, i, &adev->crtc_irq);
1275 		if (r) {
1276 			DRM_ERROR("Failed to add crtc irq id!\n");
1277 			return r;
1278 		}
1279 
1280 		int_params.int_context = INTERRUPT_HIGH_IRQ_CONTEXT;
1281 		int_params.irq_source =
1282 			dc_interrupt_to_irq_source(dc, i, 0);
1283 
1284 		c_irq_params = &adev->dm.vblank_params[int_params.irq_source - DC_IRQ_SOURCE_VBLANK1];
1285 
1286 		c_irq_params->adev = adev;
1287 		c_irq_params->irq_src = int_params.irq_source;
1288 
1289 		amdgpu_dm_irq_register_interrupt(adev, &int_params,
1290 				dm_crtc_high_irq, c_irq_params);
1291 	}
1292 
1293 	/* Use GRPH_PFLIP interrupt */
1294 	for (i = VISLANDS30_IV_SRCID_D1_GRPH_PFLIP;
1295 			i <= VISLANDS30_IV_SRCID_D6_GRPH_PFLIP; i += 2) {
1296 		r = amdgpu_irq_add_id(adev, client_id, i, &adev->pageflip_irq);
1297 		if (r) {
1298 			DRM_ERROR("Failed to add page flip irq id!\n");
1299 			return r;
1300 		}
1301 
1302 		int_params.int_context = INTERRUPT_HIGH_IRQ_CONTEXT;
1303 		int_params.irq_source =
1304 			dc_interrupt_to_irq_source(dc, i, 0);
1305 
1306 		c_irq_params = &adev->dm.pflip_params[int_params.irq_source - DC_IRQ_SOURCE_PFLIP_FIRST];
1307 
1308 		c_irq_params->adev = adev;
1309 		c_irq_params->irq_src = int_params.irq_source;
1310 
1311 		amdgpu_dm_irq_register_interrupt(adev, &int_params,
1312 				dm_pflip_high_irq, c_irq_params);
1313 
1314 	}
1315 
1316 	/* HPD */
1317 	r = amdgpu_irq_add_id(adev, client_id,
1318 			VISLANDS30_IV_SRCID_HOTPLUG_DETECT_A, &adev->hpd_irq);
1319 	if (r) {
1320 		DRM_ERROR("Failed to add hpd irq id!\n");
1321 		return r;
1322 	}
1323 
1324 	register_hpd_handlers(adev);
1325 
1326 	return 0;
1327 }
1328 
1329 #if defined(CONFIG_DRM_AMD_DC_DCN1_0)
1330 /* Register IRQ sources and initialize IRQ callbacks */
1331 static int dcn10_register_irq_handlers(struct amdgpu_device *adev)
1332 {
1333 	struct dc *dc = adev->dm.dc;
1334 	struct common_irq_params *c_irq_params;
1335 	struct dc_interrupt_params int_params = {0};
1336 	int r;
1337 	int i;
1338 
1339 	int_params.requested_polarity = INTERRUPT_POLARITY_DEFAULT;
1340 	int_params.current_polarity = INTERRUPT_POLARITY_DEFAULT;
1341 
1342 	/* Actions of amdgpu_irq_add_id():
1343 	 * 1. Register a set() function with base driver.
1344 	 *    Base driver will call set() function to enable/disable an
1345 	 *    interrupt in DC hardware.
1346 	 * 2. Register amdgpu_dm_irq_handler().
1347 	 *    Base driver will call amdgpu_dm_irq_handler() for ALL interrupts
1348 	 *    coming from DC hardware.
1349 	 *    amdgpu_dm_irq_handler() will re-direct the interrupt to DC
1350 	 *    for acknowledging and handling.
1351 	 * */
1352 
1353 	/* Use VSTARTUP interrupt */
1354 	for (i = DCN_1_0__SRCID__DC_D1_OTG_VSTARTUP;
1355 			i <= DCN_1_0__SRCID__DC_D1_OTG_VSTARTUP + adev->mode_info.num_crtc - 1;
1356 			i++) {
1357 		r = amdgpu_irq_add_id(adev, SOC15_IH_CLIENTID_DCE, i, &adev->crtc_irq);
1358 
1359 		if (r) {
1360 			DRM_ERROR("Failed to add crtc irq id!\n");
1361 			return r;
1362 		}
1363 
1364 		int_params.int_context = INTERRUPT_HIGH_IRQ_CONTEXT;
1365 		int_params.irq_source =
1366 			dc_interrupt_to_irq_source(dc, i, 0);
1367 
1368 		c_irq_params = &adev->dm.vblank_params[int_params.irq_source - DC_IRQ_SOURCE_VBLANK1];
1369 
1370 		c_irq_params->adev = adev;
1371 		c_irq_params->irq_src = int_params.irq_source;
1372 
1373 		amdgpu_dm_irq_register_interrupt(adev, &int_params,
1374 				dm_crtc_high_irq, c_irq_params);
1375 	}
1376 
1377 	/* Use GRPH_PFLIP interrupt */
1378 	for (i = DCN_1_0__SRCID__HUBP0_FLIP_INTERRUPT;
1379 			i <= DCN_1_0__SRCID__HUBP0_FLIP_INTERRUPT + adev->mode_info.num_crtc - 1;
1380 			i++) {
1381 		r = amdgpu_irq_add_id(adev, SOC15_IH_CLIENTID_DCE, i, &adev->pageflip_irq);
1382 		if (r) {
1383 			DRM_ERROR("Failed to add page flip irq id!\n");
1384 			return r;
1385 		}
1386 
1387 		int_params.int_context = INTERRUPT_HIGH_IRQ_CONTEXT;
1388 		int_params.irq_source =
1389 			dc_interrupt_to_irq_source(dc, i, 0);
1390 
1391 		c_irq_params = &adev->dm.pflip_params[int_params.irq_source - DC_IRQ_SOURCE_PFLIP_FIRST];
1392 
1393 		c_irq_params->adev = adev;
1394 		c_irq_params->irq_src = int_params.irq_source;
1395 
1396 		amdgpu_dm_irq_register_interrupt(adev, &int_params,
1397 				dm_pflip_high_irq, c_irq_params);
1398 
1399 	}
1400 
1401 	/* HPD */
1402 	r = amdgpu_irq_add_id(adev, SOC15_IH_CLIENTID_DCE, DCN_1_0__SRCID__DC_HPD1_INT,
1403 			&adev->hpd_irq);
1404 	if (r) {
1405 		DRM_ERROR("Failed to add hpd irq id!\n");
1406 		return r;
1407 	}
1408 
1409 	register_hpd_handlers(adev);
1410 
1411 	return 0;
1412 }
1413 #endif
1414 
1415 static int amdgpu_dm_mode_config_init(struct amdgpu_device *adev)
1416 {
1417 	int r;
1418 
1419 	adev->mode_info.mode_config_initialized = true;
1420 
1421 	adev->ddev->mode_config.funcs = (void *)&amdgpu_dm_mode_funcs;
1422 	adev->ddev->mode_config.helper_private = &amdgpu_dm_mode_config_helperfuncs;
1423 
1424 	adev->ddev->mode_config.max_width = 16384;
1425 	adev->ddev->mode_config.max_height = 16384;
1426 
1427 	adev->ddev->mode_config.preferred_depth = 24;
1428 	adev->ddev->mode_config.prefer_shadow = 1;
1429 	/* indicate support of immediate flip */
1430 	adev->ddev->mode_config.async_page_flip = true;
1431 
1432 	adev->ddev->mode_config.fb_base = adev->gmc.aper_base;
1433 
1434 	r = amdgpu_display_modeset_create_props(adev);
1435 	if (r)
1436 		return r;
1437 
1438 	return 0;
1439 }
1440 
1441 #if defined(CONFIG_BACKLIGHT_CLASS_DEVICE) ||\
1442 	defined(CONFIG_BACKLIGHT_CLASS_DEVICE_MODULE)
1443 
1444 static int amdgpu_dm_backlight_update_status(struct backlight_device *bd)
1445 {
1446 	struct amdgpu_display_manager *dm = bl_get_data(bd);
1447 
1448 	if (dc_link_set_backlight_level(dm->backlight_link,
1449 			bd->props.brightness, 0, 0))
1450 		return 0;
1451 	else
1452 		return 1;
1453 }
1454 
1455 static int amdgpu_dm_backlight_get_brightness(struct backlight_device *bd)
1456 {
1457 	struct amdgpu_display_manager *dm = bl_get_data(bd);
1458 	int ret = dc_link_get_backlight_level(dm->backlight_link);
1459 
1460 	if (ret == DC_ERROR_UNEXPECTED)
1461 		return bd->props.brightness;
1462 	return ret;
1463 }
1464 
1465 static const struct backlight_ops amdgpu_dm_backlight_ops = {
1466 	.options = BL_CORE_SUSPENDRESUME,
1467 	.get_brightness = amdgpu_dm_backlight_get_brightness,
1468 	.update_status	= amdgpu_dm_backlight_update_status,
1469 };
1470 
1471 static void
1472 amdgpu_dm_register_backlight_device(struct amdgpu_display_manager *dm)
1473 {
1474 	char bl_name[16];
1475 	struct backlight_properties props = { 0 };
1476 
1477 	props.max_brightness = AMDGPU_MAX_BL_LEVEL;
1478 	props.brightness = AMDGPU_MAX_BL_LEVEL;
1479 	props.type = BACKLIGHT_RAW;
1480 
1481 	snprintf(bl_name, sizeof(bl_name), "amdgpu_bl%d",
1482 			dm->adev->ddev->primary->index);
1483 
1484 	dm->backlight_dev = backlight_device_register(bl_name,
1485 			dm->adev->ddev->dev,
1486 			dm,
1487 			&amdgpu_dm_backlight_ops,
1488 			&props);
1489 
1490 	if (IS_ERR(dm->backlight_dev))
1491 		DRM_ERROR("DM: Backlight registration failed!\n");
1492 	else
1493 		DRM_DEBUG_DRIVER("DM: Registered Backlight device: %s\n", bl_name);
1494 }
1495 
1496 #endif
1497 
1498 static int initialize_plane(struct amdgpu_display_manager *dm,
1499 			     struct amdgpu_mode_info *mode_info,
1500 			     int plane_id)
1501 {
1502 	struct amdgpu_plane *plane;
1503 	unsigned long possible_crtcs;
1504 	int ret = 0;
1505 
1506 	plane = kzalloc(sizeof(struct amdgpu_plane), GFP_KERNEL);
1507 	mode_info->planes[plane_id] = plane;
1508 
1509 	if (!plane) {
1510 		DRM_ERROR("KMS: Failed to allocate plane\n");
1511 		return -ENOMEM;
1512 	}
1513 	plane->base.type = mode_info->plane_type[plane_id];
1514 
1515 	/*
1516 	 * HACK: IGT tests expect that each plane can only have one
1517 	 * one possible CRTC. For now, set one CRTC for each
1518 	 * plane that is not an underlay, but still allow multiple
1519 	 * CRTCs for underlay planes.
1520 	 */
1521 	possible_crtcs = 1 << plane_id;
1522 	if (plane_id >= dm->dc->caps.max_streams)
1523 		possible_crtcs = 0xff;
1524 
1525 	ret = amdgpu_dm_plane_init(dm, mode_info->planes[plane_id], possible_crtcs);
1526 
1527 	if (ret) {
1528 		DRM_ERROR("KMS: Failed to initialize plane\n");
1529 		return ret;
1530 	}
1531 
1532 	return ret;
1533 }
1534 
1535 
1536 static void register_backlight_device(struct amdgpu_display_manager *dm,
1537 				      struct dc_link *link)
1538 {
1539 #if defined(CONFIG_BACKLIGHT_CLASS_DEVICE) ||\
1540 	defined(CONFIG_BACKLIGHT_CLASS_DEVICE_MODULE)
1541 
1542 	if ((link->connector_signal & (SIGNAL_TYPE_EDP | SIGNAL_TYPE_LVDS)) &&
1543 	    link->type != dc_connection_none) {
1544 		/* Event if registration failed, we should continue with
1545 		 * DM initialization because not having a backlight control
1546 		 * is better then a black screen.
1547 		 */
1548 		amdgpu_dm_register_backlight_device(dm);
1549 
1550 		if (dm->backlight_dev)
1551 			dm->backlight_link = link;
1552 	}
1553 #endif
1554 }
1555 
1556 
1557 /* In this architecture, the association
1558  * connector -> encoder -> crtc
1559  * id not really requried. The crtc and connector will hold the
1560  * display_index as an abstraction to use with DAL component
1561  *
1562  * Returns 0 on success
1563  */
1564 static int amdgpu_dm_initialize_drm_device(struct amdgpu_device *adev)
1565 {
1566 	struct amdgpu_display_manager *dm = &adev->dm;
1567 	int32_t i;
1568 	struct amdgpu_dm_connector *aconnector = NULL;
1569 	struct amdgpu_encoder *aencoder = NULL;
1570 	struct amdgpu_mode_info *mode_info = &adev->mode_info;
1571 	uint32_t link_cnt;
1572 	int32_t total_overlay_planes, total_primary_planes;
1573 	enum dc_connection_type new_connection_type = dc_connection_none;
1574 
1575 	link_cnt = dm->dc->caps.max_links;
1576 	if (amdgpu_dm_mode_config_init(dm->adev)) {
1577 		DRM_ERROR("DM: Failed to initialize mode config\n");
1578 		return -1;
1579 	}
1580 
1581 	/* Identify the number of planes to be initialized */
1582 	total_overlay_planes = dm->dc->caps.max_slave_planes;
1583 	total_primary_planes = dm->dc->caps.max_planes - dm->dc->caps.max_slave_planes;
1584 
1585 	/* First initialize overlay planes, index starting after primary planes */
1586 	for (i = (total_overlay_planes - 1); i >= 0; i--) {
1587 		if (initialize_plane(dm, mode_info, (total_primary_planes + i))) {
1588 			DRM_ERROR("KMS: Failed to initialize overlay plane\n");
1589 			goto fail;
1590 		}
1591 	}
1592 
1593 	/* Initialize primary planes */
1594 	for (i = (total_primary_planes - 1); i >= 0; i--) {
1595 		if (initialize_plane(dm, mode_info, i)) {
1596 			DRM_ERROR("KMS: Failed to initialize primary plane\n");
1597 			goto fail;
1598 		}
1599 	}
1600 
1601 	for (i = 0; i < dm->dc->caps.max_streams; i++)
1602 		if (amdgpu_dm_crtc_init(dm, &mode_info->planes[i]->base, i)) {
1603 			DRM_ERROR("KMS: Failed to initialize crtc\n");
1604 			goto fail;
1605 		}
1606 
1607 	dm->display_indexes_num = dm->dc->caps.max_streams;
1608 
1609 	/* loops over all connectors on the board */
1610 	for (i = 0; i < link_cnt; i++) {
1611 		struct dc_link *link = NULL;
1612 
1613 		if (i > AMDGPU_DM_MAX_DISPLAY_INDEX) {
1614 			DRM_ERROR(
1615 				"KMS: Cannot support more than %d display indexes\n",
1616 					AMDGPU_DM_MAX_DISPLAY_INDEX);
1617 			continue;
1618 		}
1619 
1620 		aconnector = kzalloc(sizeof(*aconnector), GFP_KERNEL);
1621 		if (!aconnector)
1622 			goto fail;
1623 
1624 		aencoder = kzalloc(sizeof(*aencoder), GFP_KERNEL);
1625 		if (!aencoder)
1626 			goto fail;
1627 
1628 		if (amdgpu_dm_encoder_init(dm->ddev, aencoder, i)) {
1629 			DRM_ERROR("KMS: Failed to initialize encoder\n");
1630 			goto fail;
1631 		}
1632 
1633 		if (amdgpu_dm_connector_init(dm, aconnector, i, aencoder)) {
1634 			DRM_ERROR("KMS: Failed to initialize connector\n");
1635 			goto fail;
1636 		}
1637 
1638 		link = dc_get_link_at_index(dm->dc, i);
1639 
1640 		if (!dc_link_detect_sink(link, &new_connection_type))
1641 			DRM_ERROR("KMS: Failed to detect connector\n");
1642 
1643 		if (aconnector->base.force && new_connection_type == dc_connection_none) {
1644 			emulated_link_detect(link);
1645 			amdgpu_dm_update_connector_after_detect(aconnector);
1646 
1647 		} else if (dc_link_detect(link, DETECT_REASON_BOOT)) {
1648 			amdgpu_dm_update_connector_after_detect(aconnector);
1649 			register_backlight_device(dm, link);
1650 		}
1651 
1652 
1653 	}
1654 
1655 	/* Software is initialized. Now we can register interrupt handlers. */
1656 	switch (adev->asic_type) {
1657 	case CHIP_BONAIRE:
1658 	case CHIP_HAWAII:
1659 	case CHIP_KAVERI:
1660 	case CHIP_KABINI:
1661 	case CHIP_MULLINS:
1662 	case CHIP_TONGA:
1663 	case CHIP_FIJI:
1664 	case CHIP_CARRIZO:
1665 	case CHIP_STONEY:
1666 	case CHIP_POLARIS11:
1667 	case CHIP_POLARIS10:
1668 	case CHIP_POLARIS12:
1669 	case CHIP_VEGAM:
1670 	case CHIP_VEGA10:
1671 	case CHIP_VEGA12:
1672 	case CHIP_VEGA20:
1673 		if (dce110_register_irq_handlers(dm->adev)) {
1674 			DRM_ERROR("DM: Failed to initialize IRQ\n");
1675 			goto fail;
1676 		}
1677 		break;
1678 #if defined(CONFIG_DRM_AMD_DC_DCN1_0)
1679 	case CHIP_RAVEN:
1680 		if (dcn10_register_irq_handlers(dm->adev)) {
1681 			DRM_ERROR("DM: Failed to initialize IRQ\n");
1682 			goto fail;
1683 		}
1684 		break;
1685 #endif
1686 	default:
1687 		DRM_ERROR("Unsupported ASIC type: 0x%X\n", adev->asic_type);
1688 		goto fail;
1689 	}
1690 
1691 	if (adev->asic_type != CHIP_CARRIZO && adev->asic_type != CHIP_STONEY)
1692 		dm->dc->debug.disable_stutter = amdgpu_pp_feature_mask & PP_STUTTER_MODE ? false : true;
1693 
1694 	return 0;
1695 fail:
1696 	kfree(aencoder);
1697 	kfree(aconnector);
1698 	for (i = 0; i < dm->dc->caps.max_planes; i++)
1699 		kfree(mode_info->planes[i]);
1700 	return -1;
1701 }
1702 
1703 static void amdgpu_dm_destroy_drm_device(struct amdgpu_display_manager *dm)
1704 {
1705 	drm_mode_config_cleanup(dm->ddev);
1706 	return;
1707 }
1708 
1709 /******************************************************************************
1710  * amdgpu_display_funcs functions
1711  *****************************************************************************/
1712 
1713 /**
1714  * dm_bandwidth_update - program display watermarks
1715  *
1716  * @adev: amdgpu_device pointer
1717  *
1718  * Calculate and program the display watermarks and line buffer allocation.
1719  */
1720 static void dm_bandwidth_update(struct amdgpu_device *adev)
1721 {
1722 	/* TODO: implement later */
1723 }
1724 
1725 static int amdgpu_notify_freesync(struct drm_device *dev, void *data,
1726 				struct drm_file *filp)
1727 {
1728 	struct mod_freesync_params freesync_params;
1729 	uint8_t num_streams;
1730 	uint8_t i;
1731 
1732 	struct amdgpu_device *adev = dev->dev_private;
1733 	int r = 0;
1734 
1735 	/* Get freesync enable flag from DRM */
1736 
1737 	num_streams = dc_get_current_stream_count(adev->dm.dc);
1738 
1739 	for (i = 0; i < num_streams; i++) {
1740 		struct dc_stream_state *stream;
1741 		stream = dc_get_stream_at_index(adev->dm.dc, i);
1742 
1743 		mod_freesync_update_state(adev->dm.freesync_module,
1744 					  &stream, 1, &freesync_params);
1745 	}
1746 
1747 	return r;
1748 }
1749 
1750 static const struct amdgpu_display_funcs dm_display_funcs = {
1751 	.bandwidth_update = dm_bandwidth_update, /* called unconditionally */
1752 	.vblank_get_counter = dm_vblank_get_counter,/* called unconditionally */
1753 	.backlight_set_level = NULL, /* never called for DC */
1754 	.backlight_get_level = NULL, /* never called for DC */
1755 	.hpd_sense = NULL,/* called unconditionally */
1756 	.hpd_set_polarity = NULL, /* called unconditionally */
1757 	.hpd_get_gpio_reg = NULL, /* VBIOS parsing. DAL does it. */
1758 	.page_flip_get_scanoutpos =
1759 		dm_crtc_get_scanoutpos,/* called unconditionally */
1760 	.add_encoder = NULL, /* VBIOS parsing. DAL does it. */
1761 	.add_connector = NULL, /* VBIOS parsing. DAL does it. */
1762 	.notify_freesync = amdgpu_notify_freesync,
1763 
1764 };
1765 
1766 #if defined(CONFIG_DEBUG_KERNEL_DC)
1767 
1768 static ssize_t s3_debug_store(struct device *device,
1769 			      struct device_attribute *attr,
1770 			      const char *buf,
1771 			      size_t count)
1772 {
1773 	int ret;
1774 	int s3_state;
1775 	struct pci_dev *pdev = to_pci_dev(device);
1776 	struct drm_device *drm_dev = pci_get_drvdata(pdev);
1777 	struct amdgpu_device *adev = drm_dev->dev_private;
1778 
1779 	ret = kstrtoint(buf, 0, &s3_state);
1780 
1781 	if (ret == 0) {
1782 		if (s3_state) {
1783 			dm_resume(adev);
1784 			drm_kms_helper_hotplug_event(adev->ddev);
1785 		} else
1786 			dm_suspend(adev);
1787 	}
1788 
1789 	return ret == 0 ? count : 0;
1790 }
1791 
1792 DEVICE_ATTR_WO(s3_debug);
1793 
1794 #endif
1795 
1796 static int dm_early_init(void *handle)
1797 {
1798 	struct amdgpu_device *adev = (struct amdgpu_device *)handle;
1799 
1800 	switch (adev->asic_type) {
1801 	case CHIP_BONAIRE:
1802 	case CHIP_HAWAII:
1803 		adev->mode_info.num_crtc = 6;
1804 		adev->mode_info.num_hpd = 6;
1805 		adev->mode_info.num_dig = 6;
1806 		adev->mode_info.plane_type = dm_plane_type_default;
1807 		break;
1808 	case CHIP_KAVERI:
1809 		adev->mode_info.num_crtc = 4;
1810 		adev->mode_info.num_hpd = 6;
1811 		adev->mode_info.num_dig = 7;
1812 		adev->mode_info.plane_type = dm_plane_type_default;
1813 		break;
1814 	case CHIP_KABINI:
1815 	case CHIP_MULLINS:
1816 		adev->mode_info.num_crtc = 2;
1817 		adev->mode_info.num_hpd = 6;
1818 		adev->mode_info.num_dig = 6;
1819 		adev->mode_info.plane_type = dm_plane_type_default;
1820 		break;
1821 	case CHIP_FIJI:
1822 	case CHIP_TONGA:
1823 		adev->mode_info.num_crtc = 6;
1824 		adev->mode_info.num_hpd = 6;
1825 		adev->mode_info.num_dig = 7;
1826 		adev->mode_info.plane_type = dm_plane_type_default;
1827 		break;
1828 	case CHIP_CARRIZO:
1829 		adev->mode_info.num_crtc = 3;
1830 		adev->mode_info.num_hpd = 6;
1831 		adev->mode_info.num_dig = 9;
1832 		adev->mode_info.plane_type = dm_plane_type_carizzo;
1833 		break;
1834 	case CHIP_STONEY:
1835 		adev->mode_info.num_crtc = 2;
1836 		adev->mode_info.num_hpd = 6;
1837 		adev->mode_info.num_dig = 9;
1838 		adev->mode_info.plane_type = dm_plane_type_stoney;
1839 		break;
1840 	case CHIP_POLARIS11:
1841 	case CHIP_POLARIS12:
1842 		adev->mode_info.num_crtc = 5;
1843 		adev->mode_info.num_hpd = 5;
1844 		adev->mode_info.num_dig = 5;
1845 		adev->mode_info.plane_type = dm_plane_type_default;
1846 		break;
1847 	case CHIP_POLARIS10:
1848 	case CHIP_VEGAM:
1849 		adev->mode_info.num_crtc = 6;
1850 		adev->mode_info.num_hpd = 6;
1851 		adev->mode_info.num_dig = 6;
1852 		adev->mode_info.plane_type = dm_plane_type_default;
1853 		break;
1854 	case CHIP_VEGA10:
1855 	case CHIP_VEGA12:
1856 	case CHIP_VEGA20:
1857 		adev->mode_info.num_crtc = 6;
1858 		adev->mode_info.num_hpd = 6;
1859 		adev->mode_info.num_dig = 6;
1860 		adev->mode_info.plane_type = dm_plane_type_default;
1861 		break;
1862 #if defined(CONFIG_DRM_AMD_DC_DCN1_0)
1863 	case CHIP_RAVEN:
1864 		adev->mode_info.num_crtc = 4;
1865 		adev->mode_info.num_hpd = 4;
1866 		adev->mode_info.num_dig = 4;
1867 		adev->mode_info.plane_type = dm_plane_type_default;
1868 		break;
1869 #endif
1870 	default:
1871 		DRM_ERROR("Unsupported ASIC type: 0x%X\n", adev->asic_type);
1872 		return -EINVAL;
1873 	}
1874 
1875 	amdgpu_dm_set_irq_funcs(adev);
1876 
1877 	if (adev->mode_info.funcs == NULL)
1878 		adev->mode_info.funcs = &dm_display_funcs;
1879 
1880 	/* Note: Do NOT change adev->audio_endpt_rreg and
1881 	 * adev->audio_endpt_wreg because they are initialised in
1882 	 * amdgpu_device_init() */
1883 #if defined(CONFIG_DEBUG_KERNEL_DC)
1884 	device_create_file(
1885 		adev->ddev->dev,
1886 		&dev_attr_s3_debug);
1887 #endif
1888 
1889 	return 0;
1890 }
1891 
1892 static bool modeset_required(struct drm_crtc_state *crtc_state,
1893 			     struct dc_stream_state *new_stream,
1894 			     struct dc_stream_state *old_stream)
1895 {
1896 	if (!drm_atomic_crtc_needs_modeset(crtc_state))
1897 		return false;
1898 
1899 	if (!crtc_state->enable)
1900 		return false;
1901 
1902 	return crtc_state->active;
1903 }
1904 
1905 static bool modereset_required(struct drm_crtc_state *crtc_state)
1906 {
1907 	if (!drm_atomic_crtc_needs_modeset(crtc_state))
1908 		return false;
1909 
1910 	return !crtc_state->enable || !crtc_state->active;
1911 }
1912 
1913 static void amdgpu_dm_encoder_destroy(struct drm_encoder *encoder)
1914 {
1915 	drm_encoder_cleanup(encoder);
1916 	kfree(encoder);
1917 }
1918 
1919 static const struct drm_encoder_funcs amdgpu_dm_encoder_funcs = {
1920 	.destroy = amdgpu_dm_encoder_destroy,
1921 };
1922 
1923 static bool fill_rects_from_plane_state(const struct drm_plane_state *state,
1924 					struct dc_plane_state *plane_state)
1925 {
1926 	plane_state->src_rect.x = state->src_x >> 16;
1927 	plane_state->src_rect.y = state->src_y >> 16;
1928 	/*we ignore for now mantissa and do not to deal with floating pixels :(*/
1929 	plane_state->src_rect.width = state->src_w >> 16;
1930 
1931 	if (plane_state->src_rect.width == 0)
1932 		return false;
1933 
1934 	plane_state->src_rect.height = state->src_h >> 16;
1935 	if (plane_state->src_rect.height == 0)
1936 		return false;
1937 
1938 	plane_state->dst_rect.x = state->crtc_x;
1939 	plane_state->dst_rect.y = state->crtc_y;
1940 
1941 	if (state->crtc_w == 0)
1942 		return false;
1943 
1944 	plane_state->dst_rect.width = state->crtc_w;
1945 
1946 	if (state->crtc_h == 0)
1947 		return false;
1948 
1949 	plane_state->dst_rect.height = state->crtc_h;
1950 
1951 	plane_state->clip_rect = plane_state->dst_rect;
1952 
1953 	switch (state->rotation & DRM_MODE_ROTATE_MASK) {
1954 	case DRM_MODE_ROTATE_0:
1955 		plane_state->rotation = ROTATION_ANGLE_0;
1956 		break;
1957 	case DRM_MODE_ROTATE_90:
1958 		plane_state->rotation = ROTATION_ANGLE_90;
1959 		break;
1960 	case DRM_MODE_ROTATE_180:
1961 		plane_state->rotation = ROTATION_ANGLE_180;
1962 		break;
1963 	case DRM_MODE_ROTATE_270:
1964 		plane_state->rotation = ROTATION_ANGLE_270;
1965 		break;
1966 	default:
1967 		plane_state->rotation = ROTATION_ANGLE_0;
1968 		break;
1969 	}
1970 
1971 	return true;
1972 }
1973 static int get_fb_info(const struct amdgpu_framebuffer *amdgpu_fb,
1974 		       uint64_t *tiling_flags)
1975 {
1976 	struct amdgpu_bo *rbo = gem_to_amdgpu_bo(amdgpu_fb->base.obj[0]);
1977 	int r = amdgpu_bo_reserve(rbo, false);
1978 
1979 	if (unlikely(r)) {
1980 		// Don't show error msg. when return -ERESTARTSYS
1981 		if (r != -ERESTARTSYS)
1982 			DRM_ERROR("Unable to reserve buffer: %d\n", r);
1983 		return r;
1984 	}
1985 
1986 	if (tiling_flags)
1987 		amdgpu_bo_get_tiling_flags(rbo, tiling_flags);
1988 
1989 	amdgpu_bo_unreserve(rbo);
1990 
1991 	return r;
1992 }
1993 
1994 static int fill_plane_attributes_from_fb(struct amdgpu_device *adev,
1995 					 struct dc_plane_state *plane_state,
1996 					 const struct amdgpu_framebuffer *amdgpu_fb)
1997 {
1998 	uint64_t tiling_flags;
1999 	unsigned int awidth;
2000 	const struct drm_framebuffer *fb = &amdgpu_fb->base;
2001 	int ret = 0;
2002 	struct drm_format_name_buf format_name;
2003 
2004 	ret = get_fb_info(
2005 		amdgpu_fb,
2006 		&tiling_flags);
2007 
2008 	if (ret)
2009 		return ret;
2010 
2011 	switch (fb->format->format) {
2012 	case DRM_FORMAT_C8:
2013 		plane_state->format = SURFACE_PIXEL_FORMAT_GRPH_PALETA_256_COLORS;
2014 		break;
2015 	case DRM_FORMAT_RGB565:
2016 		plane_state->format = SURFACE_PIXEL_FORMAT_GRPH_RGB565;
2017 		break;
2018 	case DRM_FORMAT_XRGB8888:
2019 	case DRM_FORMAT_ARGB8888:
2020 		plane_state->format = SURFACE_PIXEL_FORMAT_GRPH_ARGB8888;
2021 		break;
2022 	case DRM_FORMAT_XRGB2101010:
2023 	case DRM_FORMAT_ARGB2101010:
2024 		plane_state->format = SURFACE_PIXEL_FORMAT_GRPH_ARGB2101010;
2025 		break;
2026 	case DRM_FORMAT_XBGR2101010:
2027 	case DRM_FORMAT_ABGR2101010:
2028 		plane_state->format = SURFACE_PIXEL_FORMAT_GRPH_ABGR2101010;
2029 		break;
2030 	case DRM_FORMAT_NV21:
2031 		plane_state->format = SURFACE_PIXEL_FORMAT_VIDEO_420_YCbCr;
2032 		break;
2033 	case DRM_FORMAT_NV12:
2034 		plane_state->format = SURFACE_PIXEL_FORMAT_VIDEO_420_YCrCb;
2035 		break;
2036 	default:
2037 		DRM_ERROR("Unsupported screen format %s\n",
2038 			  drm_get_format_name(fb->format->format, &format_name));
2039 		return -EINVAL;
2040 	}
2041 
2042 	if (plane_state->format < SURFACE_PIXEL_FORMAT_VIDEO_BEGIN) {
2043 		plane_state->address.type = PLN_ADDR_TYPE_GRAPHICS;
2044 		plane_state->plane_size.grph.surface_size.x = 0;
2045 		plane_state->plane_size.grph.surface_size.y = 0;
2046 		plane_state->plane_size.grph.surface_size.width = fb->width;
2047 		plane_state->plane_size.grph.surface_size.height = fb->height;
2048 		plane_state->plane_size.grph.surface_pitch =
2049 				fb->pitches[0] / fb->format->cpp[0];
2050 		/* TODO: unhardcode */
2051 		plane_state->color_space = COLOR_SPACE_SRGB;
2052 
2053 	} else {
2054 		awidth = ALIGN(fb->width, 64);
2055 		plane_state->address.type = PLN_ADDR_TYPE_VIDEO_PROGRESSIVE;
2056 		plane_state->plane_size.video.luma_size.x = 0;
2057 		plane_state->plane_size.video.luma_size.y = 0;
2058 		plane_state->plane_size.video.luma_size.width = awidth;
2059 		plane_state->plane_size.video.luma_size.height = fb->height;
2060 		/* TODO: unhardcode */
2061 		plane_state->plane_size.video.luma_pitch = awidth;
2062 
2063 		plane_state->plane_size.video.chroma_size.x = 0;
2064 		plane_state->plane_size.video.chroma_size.y = 0;
2065 		plane_state->plane_size.video.chroma_size.width = awidth;
2066 		plane_state->plane_size.video.chroma_size.height = fb->height;
2067 		plane_state->plane_size.video.chroma_pitch = awidth / 2;
2068 
2069 		/* TODO: unhardcode */
2070 		plane_state->color_space = COLOR_SPACE_YCBCR709;
2071 	}
2072 
2073 	memset(&plane_state->tiling_info, 0, sizeof(plane_state->tiling_info));
2074 
2075 	/* Fill GFX8 params */
2076 	if (AMDGPU_TILING_GET(tiling_flags, ARRAY_MODE) == DC_ARRAY_2D_TILED_THIN1) {
2077 		unsigned int bankw, bankh, mtaspect, tile_split, num_banks;
2078 
2079 		bankw = AMDGPU_TILING_GET(tiling_flags, BANK_WIDTH);
2080 		bankh = AMDGPU_TILING_GET(tiling_flags, BANK_HEIGHT);
2081 		mtaspect = AMDGPU_TILING_GET(tiling_flags, MACRO_TILE_ASPECT);
2082 		tile_split = AMDGPU_TILING_GET(tiling_flags, TILE_SPLIT);
2083 		num_banks = AMDGPU_TILING_GET(tiling_flags, NUM_BANKS);
2084 
2085 		/* XXX fix me for VI */
2086 		plane_state->tiling_info.gfx8.num_banks = num_banks;
2087 		plane_state->tiling_info.gfx8.array_mode =
2088 				DC_ARRAY_2D_TILED_THIN1;
2089 		plane_state->tiling_info.gfx8.tile_split = tile_split;
2090 		plane_state->tiling_info.gfx8.bank_width = bankw;
2091 		plane_state->tiling_info.gfx8.bank_height = bankh;
2092 		plane_state->tiling_info.gfx8.tile_aspect = mtaspect;
2093 		plane_state->tiling_info.gfx8.tile_mode =
2094 				DC_ADDR_SURF_MICRO_TILING_DISPLAY;
2095 	} else if (AMDGPU_TILING_GET(tiling_flags, ARRAY_MODE)
2096 			== DC_ARRAY_1D_TILED_THIN1) {
2097 		plane_state->tiling_info.gfx8.array_mode = DC_ARRAY_1D_TILED_THIN1;
2098 	}
2099 
2100 	plane_state->tiling_info.gfx8.pipe_config =
2101 			AMDGPU_TILING_GET(tiling_flags, PIPE_CONFIG);
2102 
2103 	if (adev->asic_type == CHIP_VEGA10 ||
2104 	    adev->asic_type == CHIP_VEGA12 ||
2105 	    adev->asic_type == CHIP_VEGA20 ||
2106 	    adev->asic_type == CHIP_RAVEN) {
2107 		/* Fill GFX9 params */
2108 		plane_state->tiling_info.gfx9.num_pipes =
2109 			adev->gfx.config.gb_addr_config_fields.num_pipes;
2110 		plane_state->tiling_info.gfx9.num_banks =
2111 			adev->gfx.config.gb_addr_config_fields.num_banks;
2112 		plane_state->tiling_info.gfx9.pipe_interleave =
2113 			adev->gfx.config.gb_addr_config_fields.pipe_interleave_size;
2114 		plane_state->tiling_info.gfx9.num_shader_engines =
2115 			adev->gfx.config.gb_addr_config_fields.num_se;
2116 		plane_state->tiling_info.gfx9.max_compressed_frags =
2117 			adev->gfx.config.gb_addr_config_fields.max_compress_frags;
2118 		plane_state->tiling_info.gfx9.num_rb_per_se =
2119 			adev->gfx.config.gb_addr_config_fields.num_rb_per_se;
2120 		plane_state->tiling_info.gfx9.swizzle =
2121 			AMDGPU_TILING_GET(tiling_flags, SWIZZLE_MODE);
2122 		plane_state->tiling_info.gfx9.shaderEnable = 1;
2123 	}
2124 
2125 	plane_state->visible = true;
2126 	plane_state->scaling_quality.h_taps_c = 0;
2127 	plane_state->scaling_quality.v_taps_c = 0;
2128 
2129 	/* is this needed? is plane_state zeroed at allocation? */
2130 	plane_state->scaling_quality.h_taps = 0;
2131 	plane_state->scaling_quality.v_taps = 0;
2132 	plane_state->stereo_format = PLANE_STEREO_FORMAT_NONE;
2133 
2134 	return ret;
2135 
2136 }
2137 
2138 static int fill_plane_attributes(struct amdgpu_device *adev,
2139 				 struct dc_plane_state *dc_plane_state,
2140 				 struct drm_plane_state *plane_state,
2141 				 struct drm_crtc_state *crtc_state)
2142 {
2143 	const struct amdgpu_framebuffer *amdgpu_fb =
2144 		to_amdgpu_framebuffer(plane_state->fb);
2145 	const struct drm_crtc *crtc = plane_state->crtc;
2146 	int ret = 0;
2147 
2148 	if (!fill_rects_from_plane_state(plane_state, dc_plane_state))
2149 		return -EINVAL;
2150 
2151 	ret = fill_plane_attributes_from_fb(
2152 		crtc->dev->dev_private,
2153 		dc_plane_state,
2154 		amdgpu_fb);
2155 
2156 	if (ret)
2157 		return ret;
2158 
2159 	/*
2160 	 * Always set input transfer function, since plane state is refreshed
2161 	 * every time.
2162 	 */
2163 	ret = amdgpu_dm_set_degamma_lut(crtc_state, dc_plane_state);
2164 	if (ret) {
2165 		dc_transfer_func_release(dc_plane_state->in_transfer_func);
2166 		dc_plane_state->in_transfer_func = NULL;
2167 	}
2168 
2169 	return ret;
2170 }
2171 
2172 /*****************************************************************************/
2173 
2174 static void update_stream_scaling_settings(const struct drm_display_mode *mode,
2175 					   const struct dm_connector_state *dm_state,
2176 					   struct dc_stream_state *stream)
2177 {
2178 	enum amdgpu_rmx_type rmx_type;
2179 
2180 	struct rect src = { 0 }; /* viewport in composition space*/
2181 	struct rect dst = { 0 }; /* stream addressable area */
2182 
2183 	/* no mode. nothing to be done */
2184 	if (!mode)
2185 		return;
2186 
2187 	/* Full screen scaling by default */
2188 	src.width = mode->hdisplay;
2189 	src.height = mode->vdisplay;
2190 	dst.width = stream->timing.h_addressable;
2191 	dst.height = stream->timing.v_addressable;
2192 
2193 	if (dm_state) {
2194 		rmx_type = dm_state->scaling;
2195 		if (rmx_type == RMX_ASPECT || rmx_type == RMX_OFF) {
2196 			if (src.width * dst.height <
2197 					src.height * dst.width) {
2198 				/* height needs less upscaling/more downscaling */
2199 				dst.width = src.width *
2200 						dst.height / src.height;
2201 			} else {
2202 				/* width needs less upscaling/more downscaling */
2203 				dst.height = src.height *
2204 						dst.width / src.width;
2205 			}
2206 		} else if (rmx_type == RMX_CENTER) {
2207 			dst = src;
2208 		}
2209 
2210 		dst.x = (stream->timing.h_addressable - dst.width) / 2;
2211 		dst.y = (stream->timing.v_addressable - dst.height) / 2;
2212 
2213 		if (dm_state->underscan_enable) {
2214 			dst.x += dm_state->underscan_hborder / 2;
2215 			dst.y += dm_state->underscan_vborder / 2;
2216 			dst.width -= dm_state->underscan_hborder;
2217 			dst.height -= dm_state->underscan_vborder;
2218 		}
2219 	}
2220 
2221 	stream->src = src;
2222 	stream->dst = dst;
2223 
2224 	DRM_DEBUG_DRIVER("Destination Rectangle x:%d  y:%d  width:%d  height:%d\n",
2225 			dst.x, dst.y, dst.width, dst.height);
2226 
2227 }
2228 
2229 static enum dc_color_depth
2230 convert_color_depth_from_display_info(const struct drm_connector *connector)
2231 {
2232 	struct dm_connector_state *dm_conn_state =
2233 		to_dm_connector_state(connector->state);
2234 	uint32_t bpc = connector->display_info.bpc;
2235 
2236 	/* TODO: Remove this when there's support for max_bpc in drm */
2237 	if (dm_conn_state && bpc > dm_conn_state->max_bpc)
2238 		/* Round down to nearest even number. */
2239 		bpc = dm_conn_state->max_bpc - (dm_conn_state->max_bpc & 1);
2240 
2241 	switch (bpc) {
2242 	case 0:
2243 		/* Temporary Work around, DRM don't parse color depth for
2244 		 * EDID revision before 1.4
2245 		 * TODO: Fix edid parsing
2246 		 */
2247 		return COLOR_DEPTH_888;
2248 	case 6:
2249 		return COLOR_DEPTH_666;
2250 	case 8:
2251 		return COLOR_DEPTH_888;
2252 	case 10:
2253 		return COLOR_DEPTH_101010;
2254 	case 12:
2255 		return COLOR_DEPTH_121212;
2256 	case 14:
2257 		return COLOR_DEPTH_141414;
2258 	case 16:
2259 		return COLOR_DEPTH_161616;
2260 	default:
2261 		return COLOR_DEPTH_UNDEFINED;
2262 	}
2263 }
2264 
2265 static enum dc_aspect_ratio
2266 get_aspect_ratio(const struct drm_display_mode *mode_in)
2267 {
2268 	/* 1-1 mapping, since both enums follow the HDMI spec. */
2269 	return (enum dc_aspect_ratio) mode_in->picture_aspect_ratio;
2270 }
2271 
2272 static enum dc_color_space
2273 get_output_color_space(const struct dc_crtc_timing *dc_crtc_timing)
2274 {
2275 	enum dc_color_space color_space = COLOR_SPACE_SRGB;
2276 
2277 	switch (dc_crtc_timing->pixel_encoding)	{
2278 	case PIXEL_ENCODING_YCBCR422:
2279 	case PIXEL_ENCODING_YCBCR444:
2280 	case PIXEL_ENCODING_YCBCR420:
2281 	{
2282 		/*
2283 		 * 27030khz is the separation point between HDTV and SDTV
2284 		 * according to HDMI spec, we use YCbCr709 and YCbCr601
2285 		 * respectively
2286 		 */
2287 		if (dc_crtc_timing->pix_clk_khz > 27030) {
2288 			if (dc_crtc_timing->flags.Y_ONLY)
2289 				color_space =
2290 					COLOR_SPACE_YCBCR709_LIMITED;
2291 			else
2292 				color_space = COLOR_SPACE_YCBCR709;
2293 		} else {
2294 			if (dc_crtc_timing->flags.Y_ONLY)
2295 				color_space =
2296 					COLOR_SPACE_YCBCR601_LIMITED;
2297 			else
2298 				color_space = COLOR_SPACE_YCBCR601;
2299 		}
2300 
2301 	}
2302 	break;
2303 	case PIXEL_ENCODING_RGB:
2304 		color_space = COLOR_SPACE_SRGB;
2305 		break;
2306 
2307 	default:
2308 		WARN_ON(1);
2309 		break;
2310 	}
2311 
2312 	return color_space;
2313 }
2314 
2315 static void reduce_mode_colour_depth(struct dc_crtc_timing *timing_out)
2316 {
2317 	if (timing_out->display_color_depth <= COLOR_DEPTH_888)
2318 		return;
2319 
2320 	timing_out->display_color_depth--;
2321 }
2322 
2323 static void adjust_colour_depth_from_display_info(struct dc_crtc_timing *timing_out,
2324 						const struct drm_display_info *info)
2325 {
2326 	int normalized_clk;
2327 	if (timing_out->display_color_depth <= COLOR_DEPTH_888)
2328 		return;
2329 	do {
2330 		normalized_clk = timing_out->pix_clk_khz;
2331 		/* YCbCr 4:2:0 requires additional adjustment of 1/2 */
2332 		if (timing_out->pixel_encoding == PIXEL_ENCODING_YCBCR420)
2333 			normalized_clk /= 2;
2334 		/* Adjusting pix clock following on HDMI spec based on colour depth */
2335 		switch (timing_out->display_color_depth) {
2336 		case COLOR_DEPTH_101010:
2337 			normalized_clk = (normalized_clk * 30) / 24;
2338 			break;
2339 		case COLOR_DEPTH_121212:
2340 			normalized_clk = (normalized_clk * 36) / 24;
2341 			break;
2342 		case COLOR_DEPTH_161616:
2343 			normalized_clk = (normalized_clk * 48) / 24;
2344 			break;
2345 		default:
2346 			return;
2347 		}
2348 		if (normalized_clk <= info->max_tmds_clock)
2349 			return;
2350 		reduce_mode_colour_depth(timing_out);
2351 
2352 	} while (timing_out->display_color_depth > COLOR_DEPTH_888);
2353 
2354 }
2355 /*****************************************************************************/
2356 
2357 static void
2358 fill_stream_properties_from_drm_display_mode(struct dc_stream_state *stream,
2359 					     const struct drm_display_mode *mode_in,
2360 					     const struct drm_connector *connector)
2361 {
2362 	struct dc_crtc_timing *timing_out = &stream->timing;
2363 	const struct drm_display_info *info = &connector->display_info;
2364 
2365 	memset(timing_out, 0, sizeof(struct dc_crtc_timing));
2366 
2367 	timing_out->h_border_left = 0;
2368 	timing_out->h_border_right = 0;
2369 	timing_out->v_border_top = 0;
2370 	timing_out->v_border_bottom = 0;
2371 	/* TODO: un-hardcode */
2372 	if (drm_mode_is_420_only(info, mode_in)
2373 			&& stream->sink->sink_signal == SIGNAL_TYPE_HDMI_TYPE_A)
2374 		timing_out->pixel_encoding = PIXEL_ENCODING_YCBCR420;
2375 	else if ((connector->display_info.color_formats & DRM_COLOR_FORMAT_YCRCB444)
2376 			&& stream->sink->sink_signal == SIGNAL_TYPE_HDMI_TYPE_A)
2377 		timing_out->pixel_encoding = PIXEL_ENCODING_YCBCR444;
2378 	else
2379 		timing_out->pixel_encoding = PIXEL_ENCODING_RGB;
2380 
2381 	timing_out->timing_3d_format = TIMING_3D_FORMAT_NONE;
2382 	timing_out->display_color_depth = convert_color_depth_from_display_info(
2383 			connector);
2384 	timing_out->scan_type = SCANNING_TYPE_NODATA;
2385 	timing_out->hdmi_vic = 0;
2386 	timing_out->vic = drm_match_cea_mode(mode_in);
2387 
2388 	timing_out->h_addressable = mode_in->crtc_hdisplay;
2389 	timing_out->h_total = mode_in->crtc_htotal;
2390 	timing_out->h_sync_width =
2391 		mode_in->crtc_hsync_end - mode_in->crtc_hsync_start;
2392 	timing_out->h_front_porch =
2393 		mode_in->crtc_hsync_start - mode_in->crtc_hdisplay;
2394 	timing_out->v_total = mode_in->crtc_vtotal;
2395 	timing_out->v_addressable = mode_in->crtc_vdisplay;
2396 	timing_out->v_front_porch =
2397 		mode_in->crtc_vsync_start - mode_in->crtc_vdisplay;
2398 	timing_out->v_sync_width =
2399 		mode_in->crtc_vsync_end - mode_in->crtc_vsync_start;
2400 	timing_out->pix_clk_khz = mode_in->crtc_clock;
2401 	timing_out->aspect_ratio = get_aspect_ratio(mode_in);
2402 	if (mode_in->flags & DRM_MODE_FLAG_PHSYNC)
2403 		timing_out->flags.HSYNC_POSITIVE_POLARITY = 1;
2404 	if (mode_in->flags & DRM_MODE_FLAG_PVSYNC)
2405 		timing_out->flags.VSYNC_POSITIVE_POLARITY = 1;
2406 
2407 	stream->output_color_space = get_output_color_space(timing_out);
2408 
2409 	stream->out_transfer_func->type = TF_TYPE_PREDEFINED;
2410 	stream->out_transfer_func->tf = TRANSFER_FUNCTION_SRGB;
2411 	if (stream->sink->sink_signal == SIGNAL_TYPE_HDMI_TYPE_A)
2412 		adjust_colour_depth_from_display_info(timing_out, info);
2413 }
2414 
2415 static void fill_audio_info(struct audio_info *audio_info,
2416 			    const struct drm_connector *drm_connector,
2417 			    const struct dc_sink *dc_sink)
2418 {
2419 	int i = 0;
2420 	int cea_revision = 0;
2421 	const struct dc_edid_caps *edid_caps = &dc_sink->edid_caps;
2422 
2423 	audio_info->manufacture_id = edid_caps->manufacturer_id;
2424 	audio_info->product_id = edid_caps->product_id;
2425 
2426 	cea_revision = drm_connector->display_info.cea_rev;
2427 
2428 	strncpy(audio_info->display_name,
2429 		edid_caps->display_name,
2430 		AUDIO_INFO_DISPLAY_NAME_SIZE_IN_CHARS - 1);
2431 
2432 	if (cea_revision >= 3) {
2433 		audio_info->mode_count = edid_caps->audio_mode_count;
2434 
2435 		for (i = 0; i < audio_info->mode_count; ++i) {
2436 			audio_info->modes[i].format_code =
2437 					(enum audio_format_code)
2438 					(edid_caps->audio_modes[i].format_code);
2439 			audio_info->modes[i].channel_count =
2440 					edid_caps->audio_modes[i].channel_count;
2441 			audio_info->modes[i].sample_rates.all =
2442 					edid_caps->audio_modes[i].sample_rate;
2443 			audio_info->modes[i].sample_size =
2444 					edid_caps->audio_modes[i].sample_size;
2445 		}
2446 	}
2447 
2448 	audio_info->flags.all = edid_caps->speaker_flags;
2449 
2450 	/* TODO: We only check for the progressive mode, check for interlace mode too */
2451 	if (drm_connector->latency_present[0]) {
2452 		audio_info->video_latency = drm_connector->video_latency[0];
2453 		audio_info->audio_latency = drm_connector->audio_latency[0];
2454 	}
2455 
2456 	/* TODO: For DP, video and audio latency should be calculated from DPCD caps */
2457 
2458 }
2459 
2460 static void
2461 copy_crtc_timing_for_drm_display_mode(const struct drm_display_mode *src_mode,
2462 				      struct drm_display_mode *dst_mode)
2463 {
2464 	dst_mode->crtc_hdisplay = src_mode->crtc_hdisplay;
2465 	dst_mode->crtc_vdisplay = src_mode->crtc_vdisplay;
2466 	dst_mode->crtc_clock = src_mode->crtc_clock;
2467 	dst_mode->crtc_hblank_start = src_mode->crtc_hblank_start;
2468 	dst_mode->crtc_hblank_end = src_mode->crtc_hblank_end;
2469 	dst_mode->crtc_hsync_start =  src_mode->crtc_hsync_start;
2470 	dst_mode->crtc_hsync_end = src_mode->crtc_hsync_end;
2471 	dst_mode->crtc_htotal = src_mode->crtc_htotal;
2472 	dst_mode->crtc_hskew = src_mode->crtc_hskew;
2473 	dst_mode->crtc_vblank_start = src_mode->crtc_vblank_start;
2474 	dst_mode->crtc_vblank_end = src_mode->crtc_vblank_end;
2475 	dst_mode->crtc_vsync_start = src_mode->crtc_vsync_start;
2476 	dst_mode->crtc_vsync_end = src_mode->crtc_vsync_end;
2477 	dst_mode->crtc_vtotal = src_mode->crtc_vtotal;
2478 }
2479 
2480 static void
2481 decide_crtc_timing_for_drm_display_mode(struct drm_display_mode *drm_mode,
2482 					const struct drm_display_mode *native_mode,
2483 					bool scale_enabled)
2484 {
2485 	if (scale_enabled) {
2486 		copy_crtc_timing_for_drm_display_mode(native_mode, drm_mode);
2487 	} else if (native_mode->clock == drm_mode->clock &&
2488 			native_mode->htotal == drm_mode->htotal &&
2489 			native_mode->vtotal == drm_mode->vtotal) {
2490 		copy_crtc_timing_for_drm_display_mode(native_mode, drm_mode);
2491 	} else {
2492 		/* no scaling nor amdgpu inserted, no need to patch */
2493 	}
2494 }
2495 
2496 static struct dc_sink *
2497 create_fake_sink(struct amdgpu_dm_connector *aconnector)
2498 {
2499 	struct dc_sink_init_data sink_init_data = { 0 };
2500 	struct dc_sink *sink = NULL;
2501 	sink_init_data.link = aconnector->dc_link;
2502 	sink_init_data.sink_signal = aconnector->dc_link->connector_signal;
2503 
2504 	sink = dc_sink_create(&sink_init_data);
2505 	if (!sink) {
2506 		DRM_ERROR("Failed to create sink!\n");
2507 		return NULL;
2508 	}
2509 	sink->sink_signal = SIGNAL_TYPE_VIRTUAL;
2510 
2511 	return sink;
2512 }
2513 
2514 static void set_multisync_trigger_params(
2515 		struct dc_stream_state *stream)
2516 {
2517 	if (stream->triggered_crtc_reset.enabled) {
2518 		stream->triggered_crtc_reset.event = CRTC_EVENT_VSYNC_RISING;
2519 		stream->triggered_crtc_reset.delay = TRIGGER_DELAY_NEXT_LINE;
2520 	}
2521 }
2522 
2523 static void set_master_stream(struct dc_stream_state *stream_set[],
2524 			      int stream_count)
2525 {
2526 	int j, highest_rfr = 0, master_stream = 0;
2527 
2528 	for (j = 0;  j < stream_count; j++) {
2529 		if (stream_set[j] && stream_set[j]->triggered_crtc_reset.enabled) {
2530 			int refresh_rate = 0;
2531 
2532 			refresh_rate = (stream_set[j]->timing.pix_clk_khz*1000)/
2533 				(stream_set[j]->timing.h_total*stream_set[j]->timing.v_total);
2534 			if (refresh_rate > highest_rfr) {
2535 				highest_rfr = refresh_rate;
2536 				master_stream = j;
2537 			}
2538 		}
2539 	}
2540 	for (j = 0;  j < stream_count; j++) {
2541 		if (stream_set[j])
2542 			stream_set[j]->triggered_crtc_reset.event_source = stream_set[master_stream];
2543 	}
2544 }
2545 
2546 static void dm_enable_per_frame_crtc_master_sync(struct dc_state *context)
2547 {
2548 	int i = 0;
2549 
2550 	if (context->stream_count < 2)
2551 		return;
2552 	for (i = 0; i < context->stream_count ; i++) {
2553 		if (!context->streams[i])
2554 			continue;
2555 		/* TODO: add a function to read AMD VSDB bits and will set
2556 		 * crtc_sync_master.multi_sync_enabled flag
2557 		 * For now its set to false
2558 		 */
2559 		set_multisync_trigger_params(context->streams[i]);
2560 	}
2561 	set_master_stream(context->streams, context->stream_count);
2562 }
2563 
2564 static struct dc_stream_state *
2565 create_stream_for_sink(struct amdgpu_dm_connector *aconnector,
2566 		       const struct drm_display_mode *drm_mode,
2567 		       const struct dm_connector_state *dm_state)
2568 {
2569 	struct drm_display_mode *preferred_mode = NULL;
2570 	struct drm_connector *drm_connector;
2571 	struct dc_stream_state *stream = NULL;
2572 	struct drm_display_mode mode = *drm_mode;
2573 	bool native_mode_found = false;
2574 	struct dc_sink *sink = NULL;
2575 	if (aconnector == NULL) {
2576 		DRM_ERROR("aconnector is NULL!\n");
2577 		return stream;
2578 	}
2579 
2580 	drm_connector = &aconnector->base;
2581 
2582 	if (!aconnector->dc_sink) {
2583 		/*
2584 		 * Create dc_sink when necessary to MST
2585 		 * Don't apply fake_sink to MST
2586 		 */
2587 		if (aconnector->mst_port) {
2588 			dm_dp_mst_dc_sink_create(drm_connector);
2589 			return stream;
2590 		}
2591 
2592 		sink = create_fake_sink(aconnector);
2593 		if (!sink)
2594 			return stream;
2595 	} else {
2596 		sink = aconnector->dc_sink;
2597 	}
2598 
2599 	stream = dc_create_stream_for_sink(sink);
2600 
2601 	if (stream == NULL) {
2602 		DRM_ERROR("Failed to create stream for sink!\n");
2603 		goto finish;
2604 	}
2605 
2606 	list_for_each_entry(preferred_mode, &aconnector->base.modes, head) {
2607 		/* Search for preferred mode */
2608 		if (preferred_mode->type & DRM_MODE_TYPE_PREFERRED) {
2609 			native_mode_found = true;
2610 			break;
2611 		}
2612 	}
2613 	if (!native_mode_found)
2614 		preferred_mode = list_first_entry_or_null(
2615 				&aconnector->base.modes,
2616 				struct drm_display_mode,
2617 				head);
2618 
2619 	if (preferred_mode == NULL) {
2620 		/* This may not be an error, the use case is when we we have no
2621 		 * usermode calls to reset and set mode upon hotplug. In this
2622 		 * case, we call set mode ourselves to restore the previous mode
2623 		 * and the modelist may not be filled in in time.
2624 		 */
2625 		DRM_DEBUG_DRIVER("No preferred mode found\n");
2626 	} else {
2627 		decide_crtc_timing_for_drm_display_mode(
2628 				&mode, preferred_mode,
2629 				dm_state ? (dm_state->scaling != RMX_OFF) : false);
2630 	}
2631 
2632 	if (!dm_state)
2633 		drm_mode_set_crtcinfo(&mode, 0);
2634 
2635 	fill_stream_properties_from_drm_display_mode(stream,
2636 			&mode, &aconnector->base);
2637 	update_stream_scaling_settings(&mode, dm_state, stream);
2638 
2639 	fill_audio_info(
2640 		&stream->audio_info,
2641 		drm_connector,
2642 		sink);
2643 
2644 	update_stream_signal(stream);
2645 
2646 	if (dm_state && dm_state->freesync_capable)
2647 		stream->ignore_msa_timing_param = true;
2648 finish:
2649 	if (sink && sink->sink_signal == SIGNAL_TYPE_VIRTUAL && aconnector->base.force != DRM_FORCE_ON)
2650 		dc_sink_release(sink);
2651 
2652 	return stream;
2653 }
2654 
2655 static void amdgpu_dm_crtc_destroy(struct drm_crtc *crtc)
2656 {
2657 	drm_crtc_cleanup(crtc);
2658 	kfree(crtc);
2659 }
2660 
2661 static void dm_crtc_destroy_state(struct drm_crtc *crtc,
2662 				  struct drm_crtc_state *state)
2663 {
2664 	struct dm_crtc_state *cur = to_dm_crtc_state(state);
2665 
2666 	/* TODO Destroy dc_stream objects are stream object is flattened */
2667 	if (cur->stream)
2668 		dc_stream_release(cur->stream);
2669 
2670 
2671 	__drm_atomic_helper_crtc_destroy_state(state);
2672 
2673 
2674 	kfree(state);
2675 }
2676 
2677 static void dm_crtc_reset_state(struct drm_crtc *crtc)
2678 {
2679 	struct dm_crtc_state *state;
2680 
2681 	if (crtc->state)
2682 		dm_crtc_destroy_state(crtc, crtc->state);
2683 
2684 	state = kzalloc(sizeof(*state), GFP_KERNEL);
2685 	if (WARN_ON(!state))
2686 		return;
2687 
2688 	crtc->state = &state->base;
2689 	crtc->state->crtc = crtc;
2690 
2691 }
2692 
2693 static struct drm_crtc_state *
2694 dm_crtc_duplicate_state(struct drm_crtc *crtc)
2695 {
2696 	struct dm_crtc_state *state, *cur;
2697 
2698 	cur = to_dm_crtc_state(crtc->state);
2699 
2700 	if (WARN_ON(!crtc->state))
2701 		return NULL;
2702 
2703 	state = kzalloc(sizeof(*state), GFP_KERNEL);
2704 	if (!state)
2705 		return NULL;
2706 
2707 	__drm_atomic_helper_crtc_duplicate_state(crtc, &state->base);
2708 
2709 	if (cur->stream) {
2710 		state->stream = cur->stream;
2711 		dc_stream_retain(state->stream);
2712 	}
2713 
2714 	/* TODO Duplicate dc_stream after objects are stream object is flattened */
2715 
2716 	return &state->base;
2717 }
2718 
2719 
2720 static inline int dm_set_vblank(struct drm_crtc *crtc, bool enable)
2721 {
2722 	enum dc_irq_source irq_source;
2723 	struct amdgpu_crtc *acrtc = to_amdgpu_crtc(crtc);
2724 	struct amdgpu_device *adev = crtc->dev->dev_private;
2725 
2726 	irq_source = IRQ_TYPE_VBLANK + acrtc->otg_inst;
2727 	return dc_interrupt_set(adev->dm.dc, irq_source, enable) ? 0 : -EBUSY;
2728 }
2729 
2730 static int dm_enable_vblank(struct drm_crtc *crtc)
2731 {
2732 	return dm_set_vblank(crtc, true);
2733 }
2734 
2735 static void dm_disable_vblank(struct drm_crtc *crtc)
2736 {
2737 	dm_set_vblank(crtc, false);
2738 }
2739 
2740 /* Implemented only the options currently availible for the driver */
2741 static const struct drm_crtc_funcs amdgpu_dm_crtc_funcs = {
2742 	.reset = dm_crtc_reset_state,
2743 	.destroy = amdgpu_dm_crtc_destroy,
2744 	.gamma_set = drm_atomic_helper_legacy_gamma_set,
2745 	.set_config = drm_atomic_helper_set_config,
2746 	.page_flip = drm_atomic_helper_page_flip,
2747 	.atomic_duplicate_state = dm_crtc_duplicate_state,
2748 	.atomic_destroy_state = dm_crtc_destroy_state,
2749 	.set_crc_source = amdgpu_dm_crtc_set_crc_source,
2750 	.enable_vblank = dm_enable_vblank,
2751 	.disable_vblank = dm_disable_vblank,
2752 };
2753 
2754 static enum drm_connector_status
2755 amdgpu_dm_connector_detect(struct drm_connector *connector, bool force)
2756 {
2757 	bool connected;
2758 	struct amdgpu_dm_connector *aconnector = to_amdgpu_dm_connector(connector);
2759 
2760 	/* Notes:
2761 	 * 1. This interface is NOT called in context of HPD irq.
2762 	 * 2. This interface *is called* in context of user-mode ioctl. Which
2763 	 * makes it a bad place for *any* MST-related activit. */
2764 
2765 	if (aconnector->base.force == DRM_FORCE_UNSPECIFIED &&
2766 	    !aconnector->fake_enable)
2767 		connected = (aconnector->dc_sink != NULL);
2768 	else
2769 		connected = (aconnector->base.force == DRM_FORCE_ON);
2770 
2771 	return (connected ? connector_status_connected :
2772 			connector_status_disconnected);
2773 }
2774 
2775 int amdgpu_dm_connector_atomic_set_property(struct drm_connector *connector,
2776 					    struct drm_connector_state *connector_state,
2777 					    struct drm_property *property,
2778 					    uint64_t val)
2779 {
2780 	struct drm_device *dev = connector->dev;
2781 	struct amdgpu_device *adev = dev->dev_private;
2782 	struct dm_connector_state *dm_old_state =
2783 		to_dm_connector_state(connector->state);
2784 	struct dm_connector_state *dm_new_state =
2785 		to_dm_connector_state(connector_state);
2786 
2787 	int ret = -EINVAL;
2788 
2789 	if (property == dev->mode_config.scaling_mode_property) {
2790 		enum amdgpu_rmx_type rmx_type;
2791 
2792 		switch (val) {
2793 		case DRM_MODE_SCALE_CENTER:
2794 			rmx_type = RMX_CENTER;
2795 			break;
2796 		case DRM_MODE_SCALE_ASPECT:
2797 			rmx_type = RMX_ASPECT;
2798 			break;
2799 		case DRM_MODE_SCALE_FULLSCREEN:
2800 			rmx_type = RMX_FULL;
2801 			break;
2802 		case DRM_MODE_SCALE_NONE:
2803 		default:
2804 			rmx_type = RMX_OFF;
2805 			break;
2806 		}
2807 
2808 		if (dm_old_state->scaling == rmx_type)
2809 			return 0;
2810 
2811 		dm_new_state->scaling = rmx_type;
2812 		ret = 0;
2813 	} else if (property == adev->mode_info.underscan_hborder_property) {
2814 		dm_new_state->underscan_hborder = val;
2815 		ret = 0;
2816 	} else if (property == adev->mode_info.underscan_vborder_property) {
2817 		dm_new_state->underscan_vborder = val;
2818 		ret = 0;
2819 	} else if (property == adev->mode_info.underscan_property) {
2820 		dm_new_state->underscan_enable = val;
2821 		ret = 0;
2822 	} else if (property == adev->mode_info.max_bpc_property) {
2823 		dm_new_state->max_bpc = val;
2824 		ret = 0;
2825 	}
2826 
2827 	return ret;
2828 }
2829 
2830 int amdgpu_dm_connector_atomic_get_property(struct drm_connector *connector,
2831 					    const struct drm_connector_state *state,
2832 					    struct drm_property *property,
2833 					    uint64_t *val)
2834 {
2835 	struct drm_device *dev = connector->dev;
2836 	struct amdgpu_device *adev = dev->dev_private;
2837 	struct dm_connector_state *dm_state =
2838 		to_dm_connector_state(state);
2839 	int ret = -EINVAL;
2840 
2841 	if (property == dev->mode_config.scaling_mode_property) {
2842 		switch (dm_state->scaling) {
2843 		case RMX_CENTER:
2844 			*val = DRM_MODE_SCALE_CENTER;
2845 			break;
2846 		case RMX_ASPECT:
2847 			*val = DRM_MODE_SCALE_ASPECT;
2848 			break;
2849 		case RMX_FULL:
2850 			*val = DRM_MODE_SCALE_FULLSCREEN;
2851 			break;
2852 		case RMX_OFF:
2853 		default:
2854 			*val = DRM_MODE_SCALE_NONE;
2855 			break;
2856 		}
2857 		ret = 0;
2858 	} else if (property == adev->mode_info.underscan_hborder_property) {
2859 		*val = dm_state->underscan_hborder;
2860 		ret = 0;
2861 	} else if (property == adev->mode_info.underscan_vborder_property) {
2862 		*val = dm_state->underscan_vborder;
2863 		ret = 0;
2864 	} else if (property == adev->mode_info.underscan_property) {
2865 		*val = dm_state->underscan_enable;
2866 		ret = 0;
2867 	} else if (property == adev->mode_info.max_bpc_property) {
2868 		*val = dm_state->max_bpc;
2869 		ret = 0;
2870 	}
2871 	return ret;
2872 }
2873 
2874 static void amdgpu_dm_connector_destroy(struct drm_connector *connector)
2875 {
2876 	struct amdgpu_dm_connector *aconnector = to_amdgpu_dm_connector(connector);
2877 	const struct dc_link *link = aconnector->dc_link;
2878 	struct amdgpu_device *adev = connector->dev->dev_private;
2879 	struct amdgpu_display_manager *dm = &adev->dm;
2880 
2881 #if defined(CONFIG_BACKLIGHT_CLASS_DEVICE) ||\
2882 	defined(CONFIG_BACKLIGHT_CLASS_DEVICE_MODULE)
2883 
2884 	if ((link->connector_signal & (SIGNAL_TYPE_EDP | SIGNAL_TYPE_LVDS)) &&
2885 	    link->type != dc_connection_none &&
2886 	    dm->backlight_dev) {
2887 		backlight_device_unregister(dm->backlight_dev);
2888 		dm->backlight_dev = NULL;
2889 	}
2890 #endif
2891 	drm_connector_unregister(connector);
2892 	drm_connector_cleanup(connector);
2893 	kfree(connector);
2894 }
2895 
2896 void amdgpu_dm_connector_funcs_reset(struct drm_connector *connector)
2897 {
2898 	struct dm_connector_state *state =
2899 		to_dm_connector_state(connector->state);
2900 
2901 	if (connector->state)
2902 		__drm_atomic_helper_connector_destroy_state(connector->state);
2903 
2904 	kfree(state);
2905 
2906 	state = kzalloc(sizeof(*state), GFP_KERNEL);
2907 
2908 	if (state) {
2909 		state->scaling = RMX_OFF;
2910 		state->underscan_enable = false;
2911 		state->underscan_hborder = 0;
2912 		state->underscan_vborder = 0;
2913 		state->max_bpc = 8;
2914 
2915 		__drm_atomic_helper_connector_reset(connector, &state->base);
2916 	}
2917 }
2918 
2919 struct drm_connector_state *
2920 amdgpu_dm_connector_atomic_duplicate_state(struct drm_connector *connector)
2921 {
2922 	struct dm_connector_state *state =
2923 		to_dm_connector_state(connector->state);
2924 
2925 	struct dm_connector_state *new_state =
2926 			kmemdup(state, sizeof(*state), GFP_KERNEL);
2927 
2928 	if (new_state) {
2929 		__drm_atomic_helper_connector_duplicate_state(connector,
2930 							      &new_state->base);
2931 		new_state->max_bpc = state->max_bpc;
2932 		return &new_state->base;
2933 	}
2934 
2935 	return NULL;
2936 }
2937 
2938 static const struct drm_connector_funcs amdgpu_dm_connector_funcs = {
2939 	.reset = amdgpu_dm_connector_funcs_reset,
2940 	.detect = amdgpu_dm_connector_detect,
2941 	.fill_modes = drm_helper_probe_single_connector_modes,
2942 	.destroy = amdgpu_dm_connector_destroy,
2943 	.atomic_duplicate_state = amdgpu_dm_connector_atomic_duplicate_state,
2944 	.atomic_destroy_state = drm_atomic_helper_connector_destroy_state,
2945 	.atomic_set_property = amdgpu_dm_connector_atomic_set_property,
2946 	.atomic_get_property = amdgpu_dm_connector_atomic_get_property
2947 };
2948 
2949 static struct drm_encoder *best_encoder(struct drm_connector *connector)
2950 {
2951 	int enc_id = connector->encoder_ids[0];
2952 	struct drm_mode_object *obj;
2953 	struct drm_encoder *encoder;
2954 
2955 	DRM_DEBUG_DRIVER("Finding the best encoder\n");
2956 
2957 	/* pick the encoder ids */
2958 	if (enc_id) {
2959 		obj = drm_mode_object_find(connector->dev, NULL, enc_id, DRM_MODE_OBJECT_ENCODER);
2960 		if (!obj) {
2961 			DRM_ERROR("Couldn't find a matching encoder for our connector\n");
2962 			return NULL;
2963 		}
2964 		encoder = obj_to_encoder(obj);
2965 		return encoder;
2966 	}
2967 	DRM_ERROR("No encoder id\n");
2968 	return NULL;
2969 }
2970 
2971 static int get_modes(struct drm_connector *connector)
2972 {
2973 	return amdgpu_dm_connector_get_modes(connector);
2974 }
2975 
2976 static void create_eml_sink(struct amdgpu_dm_connector *aconnector)
2977 {
2978 	struct dc_sink_init_data init_params = {
2979 			.link = aconnector->dc_link,
2980 			.sink_signal = SIGNAL_TYPE_VIRTUAL
2981 	};
2982 	struct edid *edid;
2983 
2984 	if (!aconnector->base.edid_blob_ptr) {
2985 		DRM_ERROR("No EDID firmware found on connector: %s ,forcing to OFF!\n",
2986 				aconnector->base.name);
2987 
2988 		aconnector->base.force = DRM_FORCE_OFF;
2989 		aconnector->base.override_edid = false;
2990 		return;
2991 	}
2992 
2993 	edid = (struct edid *) aconnector->base.edid_blob_ptr->data;
2994 
2995 	aconnector->edid = edid;
2996 
2997 	aconnector->dc_em_sink = dc_link_add_remote_sink(
2998 		aconnector->dc_link,
2999 		(uint8_t *)edid,
3000 		(edid->extensions + 1) * EDID_LENGTH,
3001 		&init_params);
3002 
3003 	if (aconnector->base.force == DRM_FORCE_ON)
3004 		aconnector->dc_sink = aconnector->dc_link->local_sink ?
3005 		aconnector->dc_link->local_sink :
3006 		aconnector->dc_em_sink;
3007 }
3008 
3009 static void handle_edid_mgmt(struct amdgpu_dm_connector *aconnector)
3010 {
3011 	struct dc_link *link = (struct dc_link *)aconnector->dc_link;
3012 
3013 	/* In case of headless boot with force on for DP managed connector
3014 	 * Those settings have to be != 0 to get initial modeset
3015 	 */
3016 	if (link->connector_signal == SIGNAL_TYPE_DISPLAY_PORT) {
3017 		link->verified_link_cap.lane_count = LANE_COUNT_FOUR;
3018 		link->verified_link_cap.link_rate = LINK_RATE_HIGH2;
3019 	}
3020 
3021 
3022 	aconnector->base.override_edid = true;
3023 	create_eml_sink(aconnector);
3024 }
3025 
3026 enum drm_mode_status amdgpu_dm_connector_mode_valid(struct drm_connector *connector,
3027 				   struct drm_display_mode *mode)
3028 {
3029 	int result = MODE_ERROR;
3030 	struct dc_sink *dc_sink;
3031 	struct amdgpu_device *adev = connector->dev->dev_private;
3032 	/* TODO: Unhardcode stream count */
3033 	struct dc_stream_state *stream;
3034 	struct amdgpu_dm_connector *aconnector = to_amdgpu_dm_connector(connector);
3035 	enum dc_status dc_result = DC_OK;
3036 
3037 	if ((mode->flags & DRM_MODE_FLAG_INTERLACE) ||
3038 			(mode->flags & DRM_MODE_FLAG_DBLSCAN))
3039 		return result;
3040 
3041 	/* Only run this the first time mode_valid is called to initilialize
3042 	 * EDID mgmt
3043 	 */
3044 	if (aconnector->base.force != DRM_FORCE_UNSPECIFIED &&
3045 		!aconnector->dc_em_sink)
3046 		handle_edid_mgmt(aconnector);
3047 
3048 	dc_sink = to_amdgpu_dm_connector(connector)->dc_sink;
3049 
3050 	if (dc_sink == NULL) {
3051 		DRM_ERROR("dc_sink is NULL!\n");
3052 		goto fail;
3053 	}
3054 
3055 	stream = create_stream_for_sink(aconnector, mode, NULL);
3056 	if (stream == NULL) {
3057 		DRM_ERROR("Failed to create stream for sink!\n");
3058 		goto fail;
3059 	}
3060 
3061 	dc_result = dc_validate_stream(adev->dm.dc, stream);
3062 
3063 	if (dc_result == DC_OK)
3064 		result = MODE_OK;
3065 	else
3066 		DRM_DEBUG_KMS("Mode %dx%d (clk %d) failed DC validation with error %d\n",
3067 			      mode->vdisplay,
3068 			      mode->hdisplay,
3069 			      mode->clock,
3070 			      dc_result);
3071 
3072 	dc_stream_release(stream);
3073 
3074 fail:
3075 	/* TODO: error handling*/
3076 	return result;
3077 }
3078 
3079 static const struct drm_connector_helper_funcs
3080 amdgpu_dm_connector_helper_funcs = {
3081 	/*
3082 	 * If hotplug a second bigger display in FB Con mode, bigger resolution
3083 	 * modes will be filtered by drm_mode_validate_size(), and those modes
3084 	 * is missing after user start lightdm. So we need to renew modes list.
3085 	 * in get_modes call back, not just return the modes count
3086 	 */
3087 	.get_modes = get_modes,
3088 	.mode_valid = amdgpu_dm_connector_mode_valid,
3089 	.best_encoder = best_encoder
3090 };
3091 
3092 static void dm_crtc_helper_disable(struct drm_crtc *crtc)
3093 {
3094 }
3095 
3096 static int dm_crtc_helper_atomic_check(struct drm_crtc *crtc,
3097 				       struct drm_crtc_state *state)
3098 {
3099 	struct amdgpu_device *adev = crtc->dev->dev_private;
3100 	struct dc *dc = adev->dm.dc;
3101 	struct dm_crtc_state *dm_crtc_state = to_dm_crtc_state(state);
3102 	int ret = -EINVAL;
3103 
3104 	if (unlikely(!dm_crtc_state->stream &&
3105 		     modeset_required(state, NULL, dm_crtc_state->stream))) {
3106 		WARN_ON(1);
3107 		return ret;
3108 	}
3109 
3110 	/* In some use cases, like reset, no stream  is attached */
3111 	if (!dm_crtc_state->stream)
3112 		return 0;
3113 
3114 	if (dc_validate_stream(dc, dm_crtc_state->stream) == DC_OK)
3115 		return 0;
3116 
3117 	return ret;
3118 }
3119 
3120 static bool dm_crtc_helper_mode_fixup(struct drm_crtc *crtc,
3121 				      const struct drm_display_mode *mode,
3122 				      struct drm_display_mode *adjusted_mode)
3123 {
3124 	return true;
3125 }
3126 
3127 static const struct drm_crtc_helper_funcs amdgpu_dm_crtc_helper_funcs = {
3128 	.disable = dm_crtc_helper_disable,
3129 	.atomic_check = dm_crtc_helper_atomic_check,
3130 	.mode_fixup = dm_crtc_helper_mode_fixup
3131 };
3132 
3133 static void dm_encoder_helper_disable(struct drm_encoder *encoder)
3134 {
3135 
3136 }
3137 
3138 static int dm_encoder_helper_atomic_check(struct drm_encoder *encoder,
3139 					  struct drm_crtc_state *crtc_state,
3140 					  struct drm_connector_state *conn_state)
3141 {
3142 	return 0;
3143 }
3144 
3145 const struct drm_encoder_helper_funcs amdgpu_dm_encoder_helper_funcs = {
3146 	.disable = dm_encoder_helper_disable,
3147 	.atomic_check = dm_encoder_helper_atomic_check
3148 };
3149 
3150 static void dm_drm_plane_reset(struct drm_plane *plane)
3151 {
3152 	struct dm_plane_state *amdgpu_state = NULL;
3153 
3154 	if (plane->state)
3155 		plane->funcs->atomic_destroy_state(plane, plane->state);
3156 
3157 	amdgpu_state = kzalloc(sizeof(*amdgpu_state), GFP_KERNEL);
3158 	WARN_ON(amdgpu_state == NULL);
3159 
3160 	if (amdgpu_state) {
3161 		plane->state = &amdgpu_state->base;
3162 		plane->state->plane = plane;
3163 		plane->state->rotation = DRM_MODE_ROTATE_0;
3164 	}
3165 }
3166 
3167 static struct drm_plane_state *
3168 dm_drm_plane_duplicate_state(struct drm_plane *plane)
3169 {
3170 	struct dm_plane_state *dm_plane_state, *old_dm_plane_state;
3171 
3172 	old_dm_plane_state = to_dm_plane_state(plane->state);
3173 	dm_plane_state = kzalloc(sizeof(*dm_plane_state), GFP_KERNEL);
3174 	if (!dm_plane_state)
3175 		return NULL;
3176 
3177 	__drm_atomic_helper_plane_duplicate_state(plane, &dm_plane_state->base);
3178 
3179 	if (old_dm_plane_state->dc_state) {
3180 		dm_plane_state->dc_state = old_dm_plane_state->dc_state;
3181 		dc_plane_state_retain(dm_plane_state->dc_state);
3182 	}
3183 
3184 	return &dm_plane_state->base;
3185 }
3186 
3187 void dm_drm_plane_destroy_state(struct drm_plane *plane,
3188 				struct drm_plane_state *state)
3189 {
3190 	struct dm_plane_state *dm_plane_state = to_dm_plane_state(state);
3191 
3192 	if (dm_plane_state->dc_state)
3193 		dc_plane_state_release(dm_plane_state->dc_state);
3194 
3195 	drm_atomic_helper_plane_destroy_state(plane, state);
3196 }
3197 
3198 static const struct drm_plane_funcs dm_plane_funcs = {
3199 	.update_plane	= drm_atomic_helper_update_plane,
3200 	.disable_plane	= drm_atomic_helper_disable_plane,
3201 	.destroy	= drm_primary_helper_destroy,
3202 	.reset = dm_drm_plane_reset,
3203 	.atomic_duplicate_state = dm_drm_plane_duplicate_state,
3204 	.atomic_destroy_state = dm_drm_plane_destroy_state,
3205 };
3206 
3207 static int dm_plane_helper_prepare_fb(struct drm_plane *plane,
3208 				      struct drm_plane_state *new_state)
3209 {
3210 	struct amdgpu_framebuffer *afb;
3211 	struct drm_gem_object *obj;
3212 	struct amdgpu_device *adev;
3213 	struct amdgpu_bo *rbo;
3214 	uint64_t chroma_addr = 0;
3215 	struct dm_plane_state *dm_plane_state_new, *dm_plane_state_old;
3216 	unsigned int awidth;
3217 	uint32_t domain;
3218 	int r;
3219 
3220 	dm_plane_state_old = to_dm_plane_state(plane->state);
3221 	dm_plane_state_new = to_dm_plane_state(new_state);
3222 
3223 	if (!new_state->fb) {
3224 		DRM_DEBUG_DRIVER("No FB bound\n");
3225 		return 0;
3226 	}
3227 
3228 	afb = to_amdgpu_framebuffer(new_state->fb);
3229 	obj = new_state->fb->obj[0];
3230 	rbo = gem_to_amdgpu_bo(obj);
3231 	adev = amdgpu_ttm_adev(rbo->tbo.bdev);
3232 	r = amdgpu_bo_reserve(rbo, false);
3233 	if (unlikely(r != 0))
3234 		return r;
3235 
3236 	if (plane->type != DRM_PLANE_TYPE_CURSOR)
3237 		domain = amdgpu_display_supported_domains(adev);
3238 	else
3239 		domain = AMDGPU_GEM_DOMAIN_VRAM;
3240 
3241 	r = amdgpu_bo_pin(rbo, domain);
3242 	if (unlikely(r != 0)) {
3243 		if (r != -ERESTARTSYS)
3244 			DRM_ERROR("Failed to pin framebuffer with error %d\n", r);
3245 		amdgpu_bo_unreserve(rbo);
3246 		return r;
3247 	}
3248 
3249 	r = amdgpu_ttm_alloc_gart(&rbo->tbo);
3250 	if (unlikely(r != 0)) {
3251 		amdgpu_bo_unpin(rbo);
3252 		amdgpu_bo_unreserve(rbo);
3253 		DRM_ERROR("%p bind failed\n", rbo);
3254 		return r;
3255 	}
3256 	amdgpu_bo_unreserve(rbo);
3257 
3258 	afb->address = amdgpu_bo_gpu_offset(rbo);
3259 
3260 	amdgpu_bo_ref(rbo);
3261 
3262 	if (dm_plane_state_new->dc_state &&
3263 			dm_plane_state_old->dc_state != dm_plane_state_new->dc_state) {
3264 		struct dc_plane_state *plane_state = dm_plane_state_new->dc_state;
3265 
3266 		if (plane_state->format < SURFACE_PIXEL_FORMAT_VIDEO_BEGIN) {
3267 			plane_state->address.grph.addr.low_part = lower_32_bits(afb->address);
3268 			plane_state->address.grph.addr.high_part = upper_32_bits(afb->address);
3269 		} else {
3270 			awidth = ALIGN(new_state->fb->width, 64);
3271 			plane_state->address.type = PLN_ADDR_TYPE_VIDEO_PROGRESSIVE;
3272 			plane_state->address.video_progressive.luma_addr.low_part
3273 							= lower_32_bits(afb->address);
3274 			plane_state->address.video_progressive.luma_addr.high_part
3275 							= upper_32_bits(afb->address);
3276 			chroma_addr = afb->address + (u64)awidth * new_state->fb->height;
3277 			plane_state->address.video_progressive.chroma_addr.low_part
3278 							= lower_32_bits(chroma_addr);
3279 			plane_state->address.video_progressive.chroma_addr.high_part
3280 							= upper_32_bits(chroma_addr);
3281 		}
3282 	}
3283 
3284 	return 0;
3285 }
3286 
3287 static void dm_plane_helper_cleanup_fb(struct drm_plane *plane,
3288 				       struct drm_plane_state *old_state)
3289 {
3290 	struct amdgpu_bo *rbo;
3291 	int r;
3292 
3293 	if (!old_state->fb)
3294 		return;
3295 
3296 	rbo = gem_to_amdgpu_bo(old_state->fb->obj[0]);
3297 	r = amdgpu_bo_reserve(rbo, false);
3298 	if (unlikely(r)) {
3299 		DRM_ERROR("failed to reserve rbo before unpin\n");
3300 		return;
3301 	}
3302 
3303 	amdgpu_bo_unpin(rbo);
3304 	amdgpu_bo_unreserve(rbo);
3305 	amdgpu_bo_unref(&rbo);
3306 }
3307 
3308 static int dm_plane_atomic_check(struct drm_plane *plane,
3309 				 struct drm_plane_state *state)
3310 {
3311 	struct amdgpu_device *adev = plane->dev->dev_private;
3312 	struct dc *dc = adev->dm.dc;
3313 	struct dm_plane_state *dm_plane_state = to_dm_plane_state(state);
3314 
3315 	if (!dm_plane_state->dc_state)
3316 		return 0;
3317 
3318 	if (!fill_rects_from_plane_state(state, dm_plane_state->dc_state))
3319 		return -EINVAL;
3320 
3321 	if (dc_validate_plane(dc, dm_plane_state->dc_state) == DC_OK)
3322 		return 0;
3323 
3324 	return -EINVAL;
3325 }
3326 
3327 static const struct drm_plane_helper_funcs dm_plane_helper_funcs = {
3328 	.prepare_fb = dm_plane_helper_prepare_fb,
3329 	.cleanup_fb = dm_plane_helper_cleanup_fb,
3330 	.atomic_check = dm_plane_atomic_check,
3331 };
3332 
3333 /*
3334  * TODO: these are currently initialized to rgb formats only.
3335  * For future use cases we should either initialize them dynamically based on
3336  * plane capabilities, or initialize this array to all formats, so internal drm
3337  * check will succeed, and let DC to implement proper check
3338  */
3339 static const uint32_t rgb_formats[] = {
3340 	DRM_FORMAT_RGB888,
3341 	DRM_FORMAT_XRGB8888,
3342 	DRM_FORMAT_ARGB8888,
3343 	DRM_FORMAT_RGBA8888,
3344 	DRM_FORMAT_XRGB2101010,
3345 	DRM_FORMAT_XBGR2101010,
3346 	DRM_FORMAT_ARGB2101010,
3347 	DRM_FORMAT_ABGR2101010,
3348 };
3349 
3350 static const uint32_t yuv_formats[] = {
3351 	DRM_FORMAT_NV12,
3352 	DRM_FORMAT_NV21,
3353 };
3354 
3355 static const u32 cursor_formats[] = {
3356 	DRM_FORMAT_ARGB8888
3357 };
3358 
3359 static int amdgpu_dm_plane_init(struct amdgpu_display_manager *dm,
3360 				struct amdgpu_plane *aplane,
3361 				unsigned long possible_crtcs)
3362 {
3363 	int res = -EPERM;
3364 
3365 	switch (aplane->base.type) {
3366 	case DRM_PLANE_TYPE_PRIMARY:
3367 		res = drm_universal_plane_init(
3368 				dm->adev->ddev,
3369 				&aplane->base,
3370 				possible_crtcs,
3371 				&dm_plane_funcs,
3372 				rgb_formats,
3373 				ARRAY_SIZE(rgb_formats),
3374 				NULL, aplane->base.type, NULL);
3375 		break;
3376 	case DRM_PLANE_TYPE_OVERLAY:
3377 		res = drm_universal_plane_init(
3378 				dm->adev->ddev,
3379 				&aplane->base,
3380 				possible_crtcs,
3381 				&dm_plane_funcs,
3382 				yuv_formats,
3383 				ARRAY_SIZE(yuv_formats),
3384 				NULL, aplane->base.type, NULL);
3385 		break;
3386 	case DRM_PLANE_TYPE_CURSOR:
3387 		res = drm_universal_plane_init(
3388 				dm->adev->ddev,
3389 				&aplane->base,
3390 				possible_crtcs,
3391 				&dm_plane_funcs,
3392 				cursor_formats,
3393 				ARRAY_SIZE(cursor_formats),
3394 				NULL, aplane->base.type, NULL);
3395 		break;
3396 	}
3397 
3398 	drm_plane_helper_add(&aplane->base, &dm_plane_helper_funcs);
3399 
3400 	/* Create (reset) the plane state */
3401 	if (aplane->base.funcs->reset)
3402 		aplane->base.funcs->reset(&aplane->base);
3403 
3404 
3405 	return res;
3406 }
3407 
3408 static int amdgpu_dm_crtc_init(struct amdgpu_display_manager *dm,
3409 			       struct drm_plane *plane,
3410 			       uint32_t crtc_index)
3411 {
3412 	struct amdgpu_crtc *acrtc = NULL;
3413 	struct amdgpu_plane *cursor_plane;
3414 
3415 	int res = -ENOMEM;
3416 
3417 	cursor_plane = kzalloc(sizeof(*cursor_plane), GFP_KERNEL);
3418 	if (!cursor_plane)
3419 		goto fail;
3420 
3421 	cursor_plane->base.type = DRM_PLANE_TYPE_CURSOR;
3422 	res = amdgpu_dm_plane_init(dm, cursor_plane, 0);
3423 
3424 	acrtc = kzalloc(sizeof(struct amdgpu_crtc), GFP_KERNEL);
3425 	if (!acrtc)
3426 		goto fail;
3427 
3428 	res = drm_crtc_init_with_planes(
3429 			dm->ddev,
3430 			&acrtc->base,
3431 			plane,
3432 			&cursor_plane->base,
3433 			&amdgpu_dm_crtc_funcs, NULL);
3434 
3435 	if (res)
3436 		goto fail;
3437 
3438 	drm_crtc_helper_add(&acrtc->base, &amdgpu_dm_crtc_helper_funcs);
3439 
3440 	/* Create (reset) the plane state */
3441 	if (acrtc->base.funcs->reset)
3442 		acrtc->base.funcs->reset(&acrtc->base);
3443 
3444 	acrtc->max_cursor_width = dm->adev->dm.dc->caps.max_cursor_size;
3445 	acrtc->max_cursor_height = dm->adev->dm.dc->caps.max_cursor_size;
3446 
3447 	acrtc->crtc_id = crtc_index;
3448 	acrtc->base.enabled = false;
3449 
3450 	dm->adev->mode_info.crtcs[crtc_index] = acrtc;
3451 	drm_crtc_enable_color_mgmt(&acrtc->base, MAX_COLOR_LUT_ENTRIES,
3452 				   true, MAX_COLOR_LUT_ENTRIES);
3453 	drm_mode_crtc_set_gamma_size(&acrtc->base, MAX_COLOR_LEGACY_LUT_ENTRIES);
3454 
3455 	return 0;
3456 
3457 fail:
3458 	kfree(acrtc);
3459 	kfree(cursor_plane);
3460 	return res;
3461 }
3462 
3463 
3464 static int to_drm_connector_type(enum signal_type st)
3465 {
3466 	switch (st) {
3467 	case SIGNAL_TYPE_HDMI_TYPE_A:
3468 		return DRM_MODE_CONNECTOR_HDMIA;
3469 	case SIGNAL_TYPE_EDP:
3470 		return DRM_MODE_CONNECTOR_eDP;
3471 	case SIGNAL_TYPE_RGB:
3472 		return DRM_MODE_CONNECTOR_VGA;
3473 	case SIGNAL_TYPE_DISPLAY_PORT:
3474 	case SIGNAL_TYPE_DISPLAY_PORT_MST:
3475 		return DRM_MODE_CONNECTOR_DisplayPort;
3476 	case SIGNAL_TYPE_DVI_DUAL_LINK:
3477 	case SIGNAL_TYPE_DVI_SINGLE_LINK:
3478 		return DRM_MODE_CONNECTOR_DVID;
3479 	case SIGNAL_TYPE_VIRTUAL:
3480 		return DRM_MODE_CONNECTOR_VIRTUAL;
3481 
3482 	default:
3483 		return DRM_MODE_CONNECTOR_Unknown;
3484 	}
3485 }
3486 
3487 static void amdgpu_dm_get_native_mode(struct drm_connector *connector)
3488 {
3489 	const struct drm_connector_helper_funcs *helper =
3490 		connector->helper_private;
3491 	struct drm_encoder *encoder;
3492 	struct amdgpu_encoder *amdgpu_encoder;
3493 
3494 	encoder = helper->best_encoder(connector);
3495 
3496 	if (encoder == NULL)
3497 		return;
3498 
3499 	amdgpu_encoder = to_amdgpu_encoder(encoder);
3500 
3501 	amdgpu_encoder->native_mode.clock = 0;
3502 
3503 	if (!list_empty(&connector->probed_modes)) {
3504 		struct drm_display_mode *preferred_mode = NULL;
3505 
3506 		list_for_each_entry(preferred_mode,
3507 				    &connector->probed_modes,
3508 				    head) {
3509 			if (preferred_mode->type & DRM_MODE_TYPE_PREFERRED)
3510 				amdgpu_encoder->native_mode = *preferred_mode;
3511 
3512 			break;
3513 		}
3514 
3515 	}
3516 }
3517 
3518 static struct drm_display_mode *
3519 amdgpu_dm_create_common_mode(struct drm_encoder *encoder,
3520 			     char *name,
3521 			     int hdisplay, int vdisplay)
3522 {
3523 	struct drm_device *dev = encoder->dev;
3524 	struct amdgpu_encoder *amdgpu_encoder = to_amdgpu_encoder(encoder);
3525 	struct drm_display_mode *mode = NULL;
3526 	struct drm_display_mode *native_mode = &amdgpu_encoder->native_mode;
3527 
3528 	mode = drm_mode_duplicate(dev, native_mode);
3529 
3530 	if (mode == NULL)
3531 		return NULL;
3532 
3533 	mode->hdisplay = hdisplay;
3534 	mode->vdisplay = vdisplay;
3535 	mode->type &= ~DRM_MODE_TYPE_PREFERRED;
3536 	strncpy(mode->name, name, DRM_DISPLAY_MODE_LEN);
3537 
3538 	return mode;
3539 
3540 }
3541 
3542 static void amdgpu_dm_connector_add_common_modes(struct drm_encoder *encoder,
3543 						 struct drm_connector *connector)
3544 {
3545 	struct amdgpu_encoder *amdgpu_encoder = to_amdgpu_encoder(encoder);
3546 	struct drm_display_mode *mode = NULL;
3547 	struct drm_display_mode *native_mode = &amdgpu_encoder->native_mode;
3548 	struct amdgpu_dm_connector *amdgpu_dm_connector =
3549 				to_amdgpu_dm_connector(connector);
3550 	int i;
3551 	int n;
3552 	struct mode_size {
3553 		char name[DRM_DISPLAY_MODE_LEN];
3554 		int w;
3555 		int h;
3556 	} common_modes[] = {
3557 		{  "640x480",  640,  480},
3558 		{  "800x600",  800,  600},
3559 		{ "1024x768", 1024,  768},
3560 		{ "1280x720", 1280,  720},
3561 		{ "1280x800", 1280,  800},
3562 		{"1280x1024", 1280, 1024},
3563 		{ "1440x900", 1440,  900},
3564 		{"1680x1050", 1680, 1050},
3565 		{"1600x1200", 1600, 1200},
3566 		{"1920x1080", 1920, 1080},
3567 		{"1920x1200", 1920, 1200}
3568 	};
3569 
3570 	n = ARRAY_SIZE(common_modes);
3571 
3572 	for (i = 0; i < n; i++) {
3573 		struct drm_display_mode *curmode = NULL;
3574 		bool mode_existed = false;
3575 
3576 		if (common_modes[i].w > native_mode->hdisplay ||
3577 		    common_modes[i].h > native_mode->vdisplay ||
3578 		   (common_modes[i].w == native_mode->hdisplay &&
3579 		    common_modes[i].h == native_mode->vdisplay))
3580 			continue;
3581 
3582 		list_for_each_entry(curmode, &connector->probed_modes, head) {
3583 			if (common_modes[i].w == curmode->hdisplay &&
3584 			    common_modes[i].h == curmode->vdisplay) {
3585 				mode_existed = true;
3586 				break;
3587 			}
3588 		}
3589 
3590 		if (mode_existed)
3591 			continue;
3592 
3593 		mode = amdgpu_dm_create_common_mode(encoder,
3594 				common_modes[i].name, common_modes[i].w,
3595 				common_modes[i].h);
3596 		drm_mode_probed_add(connector, mode);
3597 		amdgpu_dm_connector->num_modes++;
3598 	}
3599 }
3600 
3601 static void amdgpu_dm_connector_ddc_get_modes(struct drm_connector *connector,
3602 					      struct edid *edid)
3603 {
3604 	struct amdgpu_dm_connector *amdgpu_dm_connector =
3605 			to_amdgpu_dm_connector(connector);
3606 
3607 	if (edid) {
3608 		/* empty probed_modes */
3609 		INIT_LIST_HEAD(&connector->probed_modes);
3610 		amdgpu_dm_connector->num_modes =
3611 				drm_add_edid_modes(connector, edid);
3612 
3613 		amdgpu_dm_get_native_mode(connector);
3614 	} else {
3615 		amdgpu_dm_connector->num_modes = 0;
3616 	}
3617 }
3618 
3619 static int amdgpu_dm_connector_get_modes(struct drm_connector *connector)
3620 {
3621 	const struct drm_connector_helper_funcs *helper =
3622 			connector->helper_private;
3623 	struct amdgpu_dm_connector *amdgpu_dm_connector =
3624 			to_amdgpu_dm_connector(connector);
3625 	struct drm_encoder *encoder;
3626 	struct edid *edid = amdgpu_dm_connector->edid;
3627 
3628 	encoder = helper->best_encoder(connector);
3629 
3630 	if (!edid || !drm_edid_is_valid(edid)) {
3631 		drm_add_modes_noedid(connector, 640, 480);
3632 	} else {
3633 		amdgpu_dm_connector_ddc_get_modes(connector, edid);
3634 		amdgpu_dm_connector_add_common_modes(encoder, connector);
3635 	}
3636 	amdgpu_dm_fbc_init(connector);
3637 
3638 	return amdgpu_dm_connector->num_modes;
3639 }
3640 
3641 void amdgpu_dm_connector_init_helper(struct amdgpu_display_manager *dm,
3642 				     struct amdgpu_dm_connector *aconnector,
3643 				     int connector_type,
3644 				     struct dc_link *link,
3645 				     int link_index)
3646 {
3647 	struct amdgpu_device *adev = dm->ddev->dev_private;
3648 
3649 	/*
3650 	 * Some of the properties below require access to state, like bpc.
3651 	 * Allocate some default initial connector state with our reset helper.
3652 	 */
3653 	if (aconnector->base.funcs->reset)
3654 		aconnector->base.funcs->reset(&aconnector->base);
3655 
3656 	aconnector->connector_id = link_index;
3657 	aconnector->dc_link = link;
3658 	aconnector->base.interlace_allowed = false;
3659 	aconnector->base.doublescan_allowed = false;
3660 	aconnector->base.stereo_allowed = false;
3661 	aconnector->base.dpms = DRM_MODE_DPMS_OFF;
3662 	aconnector->hpd.hpd = AMDGPU_HPD_NONE; /* not used */
3663 	mutex_init(&aconnector->hpd_lock);
3664 
3665 	/* configure support HPD hot plug connector_>polled default value is 0
3666 	 * which means HPD hot plug not supported
3667 	 */
3668 	switch (connector_type) {
3669 	case DRM_MODE_CONNECTOR_HDMIA:
3670 		aconnector->base.polled = DRM_CONNECTOR_POLL_HPD;
3671 		aconnector->base.ycbcr_420_allowed =
3672 			link->link_enc->features.ycbcr420_supported ? true : false;
3673 		break;
3674 	case DRM_MODE_CONNECTOR_DisplayPort:
3675 		aconnector->base.polled = DRM_CONNECTOR_POLL_HPD;
3676 		aconnector->base.ycbcr_420_allowed =
3677 			link->link_enc->features.ycbcr420_supported ? true : false;
3678 		break;
3679 	case DRM_MODE_CONNECTOR_DVID:
3680 		aconnector->base.polled = DRM_CONNECTOR_POLL_HPD;
3681 		break;
3682 	default:
3683 		break;
3684 	}
3685 
3686 	drm_object_attach_property(&aconnector->base.base,
3687 				dm->ddev->mode_config.scaling_mode_property,
3688 				DRM_MODE_SCALE_NONE);
3689 
3690 	drm_object_attach_property(&aconnector->base.base,
3691 				adev->mode_info.underscan_property,
3692 				UNDERSCAN_OFF);
3693 	drm_object_attach_property(&aconnector->base.base,
3694 				adev->mode_info.underscan_hborder_property,
3695 				0);
3696 	drm_object_attach_property(&aconnector->base.base,
3697 				adev->mode_info.underscan_vborder_property,
3698 				0);
3699 	drm_object_attach_property(&aconnector->base.base,
3700 				adev->mode_info.max_bpc_property,
3701 				0);
3702 
3703 }
3704 
3705 static int amdgpu_dm_i2c_xfer(struct i2c_adapter *i2c_adap,
3706 			      struct i2c_msg *msgs, int num)
3707 {
3708 	struct amdgpu_i2c_adapter *i2c = i2c_get_adapdata(i2c_adap);
3709 	struct ddc_service *ddc_service = i2c->ddc_service;
3710 	struct i2c_command cmd;
3711 	int i;
3712 	int result = -EIO;
3713 
3714 	cmd.payloads = kcalloc(num, sizeof(struct i2c_payload), GFP_KERNEL);
3715 
3716 	if (!cmd.payloads)
3717 		return result;
3718 
3719 	cmd.number_of_payloads = num;
3720 	cmd.engine = I2C_COMMAND_ENGINE_DEFAULT;
3721 	cmd.speed = 100;
3722 
3723 	for (i = 0; i < num; i++) {
3724 		cmd.payloads[i].write = !(msgs[i].flags & I2C_M_RD);
3725 		cmd.payloads[i].address = msgs[i].addr;
3726 		cmd.payloads[i].length = msgs[i].len;
3727 		cmd.payloads[i].data = msgs[i].buf;
3728 	}
3729 
3730 	if (dal_i2caux_submit_i2c_command(
3731 			ddc_service->ctx->i2caux,
3732 			ddc_service->ddc_pin,
3733 			&cmd))
3734 		result = num;
3735 
3736 	kfree(cmd.payloads);
3737 	return result;
3738 }
3739 
3740 static u32 amdgpu_dm_i2c_func(struct i2c_adapter *adap)
3741 {
3742 	return I2C_FUNC_I2C | I2C_FUNC_SMBUS_EMUL;
3743 }
3744 
3745 static const struct i2c_algorithm amdgpu_dm_i2c_algo = {
3746 	.master_xfer = amdgpu_dm_i2c_xfer,
3747 	.functionality = amdgpu_dm_i2c_func,
3748 };
3749 
3750 static struct amdgpu_i2c_adapter *
3751 create_i2c(struct ddc_service *ddc_service,
3752 	   int link_index,
3753 	   int *res)
3754 {
3755 	struct amdgpu_device *adev = ddc_service->ctx->driver_context;
3756 	struct amdgpu_i2c_adapter *i2c;
3757 
3758 	i2c = kzalloc(sizeof(struct amdgpu_i2c_adapter), GFP_KERNEL);
3759 	if (!i2c)
3760 		return NULL;
3761 	i2c->base.owner = THIS_MODULE;
3762 	i2c->base.class = I2C_CLASS_DDC;
3763 	i2c->base.dev.parent = &adev->pdev->dev;
3764 	i2c->base.algo = &amdgpu_dm_i2c_algo;
3765 	snprintf(i2c->base.name, sizeof(i2c->base.name), "AMDGPU DM i2c hw bus %d", link_index);
3766 	i2c_set_adapdata(&i2c->base, i2c);
3767 	i2c->ddc_service = ddc_service;
3768 
3769 	return i2c;
3770 }
3771 
3772 
3773 /* Note: this function assumes that dc_link_detect() was called for the
3774  * dc_link which will be represented by this aconnector.
3775  */
3776 static int amdgpu_dm_connector_init(struct amdgpu_display_manager *dm,
3777 				    struct amdgpu_dm_connector *aconnector,
3778 				    uint32_t link_index,
3779 				    struct amdgpu_encoder *aencoder)
3780 {
3781 	int res = 0;
3782 	int connector_type;
3783 	struct dc *dc = dm->dc;
3784 	struct dc_link *link = dc_get_link_at_index(dc, link_index);
3785 	struct amdgpu_i2c_adapter *i2c;
3786 
3787 	link->priv = aconnector;
3788 
3789 	DRM_DEBUG_DRIVER("%s()\n", __func__);
3790 
3791 	i2c = create_i2c(link->ddc, link->link_index, &res);
3792 	if (!i2c) {
3793 		DRM_ERROR("Failed to create i2c adapter data\n");
3794 		return -ENOMEM;
3795 	}
3796 
3797 	aconnector->i2c = i2c;
3798 	res = i2c_add_adapter(&i2c->base);
3799 
3800 	if (res) {
3801 		DRM_ERROR("Failed to register hw i2c %d\n", link->link_index);
3802 		goto out_free;
3803 	}
3804 
3805 	connector_type = to_drm_connector_type(link->connector_signal);
3806 
3807 	res = drm_connector_init(
3808 			dm->ddev,
3809 			&aconnector->base,
3810 			&amdgpu_dm_connector_funcs,
3811 			connector_type);
3812 
3813 	if (res) {
3814 		DRM_ERROR("connector_init failed\n");
3815 		aconnector->connector_id = -1;
3816 		goto out_free;
3817 	}
3818 
3819 	drm_connector_helper_add(
3820 			&aconnector->base,
3821 			&amdgpu_dm_connector_helper_funcs);
3822 
3823 	amdgpu_dm_connector_init_helper(
3824 		dm,
3825 		aconnector,
3826 		connector_type,
3827 		link,
3828 		link_index);
3829 
3830 	drm_connector_attach_encoder(
3831 		&aconnector->base, &aencoder->base);
3832 
3833 	drm_connector_register(&aconnector->base);
3834 #if defined(CONFIG_DEBUG_FS)
3835 	res = connector_debugfs_init(aconnector);
3836 	if (res) {
3837 		DRM_ERROR("Failed to create debugfs for connector");
3838 		goto out_free;
3839 	}
3840 #endif
3841 
3842 	if (connector_type == DRM_MODE_CONNECTOR_DisplayPort
3843 		|| connector_type == DRM_MODE_CONNECTOR_eDP)
3844 		amdgpu_dm_initialize_dp_connector(dm, aconnector);
3845 
3846 out_free:
3847 	if (res) {
3848 		kfree(i2c);
3849 		aconnector->i2c = NULL;
3850 	}
3851 	return res;
3852 }
3853 
3854 int amdgpu_dm_get_encoder_crtc_mask(struct amdgpu_device *adev)
3855 {
3856 	switch (adev->mode_info.num_crtc) {
3857 	case 1:
3858 		return 0x1;
3859 	case 2:
3860 		return 0x3;
3861 	case 3:
3862 		return 0x7;
3863 	case 4:
3864 		return 0xf;
3865 	case 5:
3866 		return 0x1f;
3867 	case 6:
3868 	default:
3869 		return 0x3f;
3870 	}
3871 }
3872 
3873 static int amdgpu_dm_encoder_init(struct drm_device *dev,
3874 				  struct amdgpu_encoder *aencoder,
3875 				  uint32_t link_index)
3876 {
3877 	struct amdgpu_device *adev = dev->dev_private;
3878 
3879 	int res = drm_encoder_init(dev,
3880 				   &aencoder->base,
3881 				   &amdgpu_dm_encoder_funcs,
3882 				   DRM_MODE_ENCODER_TMDS,
3883 				   NULL);
3884 
3885 	aencoder->base.possible_crtcs = amdgpu_dm_get_encoder_crtc_mask(adev);
3886 
3887 	if (!res)
3888 		aencoder->encoder_id = link_index;
3889 	else
3890 		aencoder->encoder_id = -1;
3891 
3892 	drm_encoder_helper_add(&aencoder->base, &amdgpu_dm_encoder_helper_funcs);
3893 
3894 	return res;
3895 }
3896 
3897 static void manage_dm_interrupts(struct amdgpu_device *adev,
3898 				 struct amdgpu_crtc *acrtc,
3899 				 bool enable)
3900 {
3901 	/*
3902 	 * this is not correct translation but will work as soon as VBLANK
3903 	 * constant is the same as PFLIP
3904 	 */
3905 	int irq_type =
3906 		amdgpu_display_crtc_idx_to_irq_type(
3907 			adev,
3908 			acrtc->crtc_id);
3909 
3910 	if (enable) {
3911 		drm_crtc_vblank_on(&acrtc->base);
3912 		amdgpu_irq_get(
3913 			adev,
3914 			&adev->pageflip_irq,
3915 			irq_type);
3916 	} else {
3917 
3918 		amdgpu_irq_put(
3919 			adev,
3920 			&adev->pageflip_irq,
3921 			irq_type);
3922 		drm_crtc_vblank_off(&acrtc->base);
3923 	}
3924 }
3925 
3926 static bool
3927 is_scaling_state_different(const struct dm_connector_state *dm_state,
3928 			   const struct dm_connector_state *old_dm_state)
3929 {
3930 	if (dm_state->scaling != old_dm_state->scaling)
3931 		return true;
3932 	if (!dm_state->underscan_enable && old_dm_state->underscan_enable) {
3933 		if (old_dm_state->underscan_hborder != 0 && old_dm_state->underscan_vborder != 0)
3934 			return true;
3935 	} else  if (dm_state->underscan_enable && !old_dm_state->underscan_enable) {
3936 		if (dm_state->underscan_hborder != 0 && dm_state->underscan_vborder != 0)
3937 			return true;
3938 	} else if (dm_state->underscan_hborder != old_dm_state->underscan_hborder ||
3939 		   dm_state->underscan_vborder != old_dm_state->underscan_vborder)
3940 		return true;
3941 	return false;
3942 }
3943 
3944 static void remove_stream(struct amdgpu_device *adev,
3945 			  struct amdgpu_crtc *acrtc,
3946 			  struct dc_stream_state *stream)
3947 {
3948 	/* this is the update mode case */
3949 	if (adev->dm.freesync_module)
3950 		mod_freesync_remove_stream(adev->dm.freesync_module, stream);
3951 
3952 	acrtc->otg_inst = -1;
3953 	acrtc->enabled = false;
3954 }
3955 
3956 static int get_cursor_position(struct drm_plane *plane, struct drm_crtc *crtc,
3957 			       struct dc_cursor_position *position)
3958 {
3959 	struct amdgpu_crtc *amdgpu_crtc = to_amdgpu_crtc(crtc);
3960 	int x, y;
3961 	int xorigin = 0, yorigin = 0;
3962 
3963 	if (!crtc || !plane->state->fb) {
3964 		position->enable = false;
3965 		position->x = 0;
3966 		position->y = 0;
3967 		return 0;
3968 	}
3969 
3970 	if ((plane->state->crtc_w > amdgpu_crtc->max_cursor_width) ||
3971 	    (plane->state->crtc_h > amdgpu_crtc->max_cursor_height)) {
3972 		DRM_ERROR("%s: bad cursor width or height %d x %d\n",
3973 			  __func__,
3974 			  plane->state->crtc_w,
3975 			  plane->state->crtc_h);
3976 		return -EINVAL;
3977 	}
3978 
3979 	x = plane->state->crtc_x;
3980 	y = plane->state->crtc_y;
3981 	/* avivo cursor are offset into the total surface */
3982 	x += crtc->primary->state->src_x >> 16;
3983 	y += crtc->primary->state->src_y >> 16;
3984 	if (x < 0) {
3985 		xorigin = min(-x, amdgpu_crtc->max_cursor_width - 1);
3986 		x = 0;
3987 	}
3988 	if (y < 0) {
3989 		yorigin = min(-y, amdgpu_crtc->max_cursor_height - 1);
3990 		y = 0;
3991 	}
3992 	position->enable = true;
3993 	position->x = x;
3994 	position->y = y;
3995 	position->x_hotspot = xorigin;
3996 	position->y_hotspot = yorigin;
3997 
3998 	return 0;
3999 }
4000 
4001 static void handle_cursor_update(struct drm_plane *plane,
4002 				 struct drm_plane_state *old_plane_state)
4003 {
4004 	struct amdgpu_framebuffer *afb = to_amdgpu_framebuffer(plane->state->fb);
4005 	struct drm_crtc *crtc = afb ? plane->state->crtc : old_plane_state->crtc;
4006 	struct dm_crtc_state *crtc_state = crtc ? to_dm_crtc_state(crtc->state) : NULL;
4007 	struct amdgpu_crtc *amdgpu_crtc = to_amdgpu_crtc(crtc);
4008 	uint64_t address = afb ? afb->address : 0;
4009 	struct dc_cursor_position position;
4010 	struct dc_cursor_attributes attributes;
4011 	int ret;
4012 
4013 	if (!plane->state->fb && !old_plane_state->fb)
4014 		return;
4015 
4016 	DRM_DEBUG_DRIVER("%s: crtc_id=%d with size %d to %d\n",
4017 			 __func__,
4018 			 amdgpu_crtc->crtc_id,
4019 			 plane->state->crtc_w,
4020 			 plane->state->crtc_h);
4021 
4022 	ret = get_cursor_position(plane, crtc, &position);
4023 	if (ret)
4024 		return;
4025 
4026 	if (!position.enable) {
4027 		/* turn off cursor */
4028 		if (crtc_state && crtc_state->stream)
4029 			dc_stream_set_cursor_position(crtc_state->stream,
4030 						      &position);
4031 		return;
4032 	}
4033 
4034 	amdgpu_crtc->cursor_width = plane->state->crtc_w;
4035 	amdgpu_crtc->cursor_height = plane->state->crtc_h;
4036 
4037 	memset(&attributes, 0, sizeof(attributes));
4038 	attributes.address.high_part = upper_32_bits(address);
4039 	attributes.address.low_part  = lower_32_bits(address);
4040 	attributes.width             = plane->state->crtc_w;
4041 	attributes.height            = plane->state->crtc_h;
4042 	attributes.color_format      = CURSOR_MODE_COLOR_PRE_MULTIPLIED_ALPHA;
4043 	attributes.rotation_angle    = 0;
4044 	attributes.attribute_flags.value = 0;
4045 
4046 	attributes.pitch = attributes.width;
4047 
4048 	if (crtc_state->stream) {
4049 		if (!dc_stream_set_cursor_attributes(crtc_state->stream,
4050 							 &attributes))
4051 			DRM_ERROR("DC failed to set cursor attributes\n");
4052 
4053 		if (!dc_stream_set_cursor_position(crtc_state->stream,
4054 						   &position))
4055 			DRM_ERROR("DC failed to set cursor position\n");
4056 	}
4057 }
4058 
4059 static void prepare_flip_isr(struct amdgpu_crtc *acrtc)
4060 {
4061 
4062 	assert_spin_locked(&acrtc->base.dev->event_lock);
4063 	WARN_ON(acrtc->event);
4064 
4065 	acrtc->event = acrtc->base.state->event;
4066 
4067 	/* Set the flip status */
4068 	acrtc->pflip_status = AMDGPU_FLIP_SUBMITTED;
4069 
4070 	/* Mark this event as consumed */
4071 	acrtc->base.state->event = NULL;
4072 
4073 	DRM_DEBUG_DRIVER("crtc:%d, pflip_stat:AMDGPU_FLIP_SUBMITTED\n",
4074 						 acrtc->crtc_id);
4075 }
4076 
4077 /*
4078  * Executes flip
4079  *
4080  * Waits on all BO's fences and for proper vblank count
4081  */
4082 static void amdgpu_dm_do_flip(struct drm_crtc *crtc,
4083 			      struct drm_framebuffer *fb,
4084 			      uint32_t target,
4085 			      struct dc_state *state)
4086 {
4087 	unsigned long flags;
4088 	uint32_t target_vblank;
4089 	int r, vpos, hpos;
4090 	struct amdgpu_crtc *acrtc = to_amdgpu_crtc(crtc);
4091 	struct amdgpu_framebuffer *afb = to_amdgpu_framebuffer(fb);
4092 	struct amdgpu_bo *abo = gem_to_amdgpu_bo(fb->obj[0]);
4093 	struct amdgpu_device *adev = crtc->dev->dev_private;
4094 	bool async_flip = (crtc->state->pageflip_flags & DRM_MODE_PAGE_FLIP_ASYNC) != 0;
4095 	struct dc_flip_addrs addr = { {0} };
4096 	/* TODO eliminate or rename surface_update */
4097 	struct dc_surface_update surface_updates[1] = { {0} };
4098 	struct dm_crtc_state *acrtc_state = to_dm_crtc_state(crtc->state);
4099 
4100 
4101 	/* Prepare wait for target vblank early - before the fence-waits */
4102 	target_vblank = target - (uint32_t)drm_crtc_vblank_count(crtc) +
4103 			amdgpu_get_vblank_counter_kms(crtc->dev, acrtc->crtc_id);
4104 
4105 	/* TODO This might fail and hence better not used, wait
4106 	 * explicitly on fences instead
4107 	 * and in general should be called for
4108 	 * blocking commit to as per framework helpers
4109 	 */
4110 	r = amdgpu_bo_reserve(abo, true);
4111 	if (unlikely(r != 0)) {
4112 		DRM_ERROR("failed to reserve buffer before flip\n");
4113 		WARN_ON(1);
4114 	}
4115 
4116 	/* Wait for all fences on this FB */
4117 	WARN_ON(reservation_object_wait_timeout_rcu(abo->tbo.resv, true, false,
4118 								    MAX_SCHEDULE_TIMEOUT) < 0);
4119 
4120 	amdgpu_bo_unreserve(abo);
4121 
4122 	/* Wait until we're out of the vertical blank period before the one
4123 	 * targeted by the flip
4124 	 */
4125 	while ((acrtc->enabled &&
4126 		(amdgpu_display_get_crtc_scanoutpos(adev->ddev, acrtc->crtc_id,
4127 						    0, &vpos, &hpos, NULL,
4128 						    NULL, &crtc->hwmode)
4129 		 & (DRM_SCANOUTPOS_VALID | DRM_SCANOUTPOS_IN_VBLANK)) ==
4130 		(DRM_SCANOUTPOS_VALID | DRM_SCANOUTPOS_IN_VBLANK) &&
4131 		(int)(target_vblank -
4132 		  amdgpu_get_vblank_counter_kms(adev->ddev, acrtc->crtc_id)) > 0)) {
4133 		usleep_range(1000, 1100);
4134 	}
4135 
4136 	/* Flip */
4137 	spin_lock_irqsave(&crtc->dev->event_lock, flags);
4138 
4139 	WARN_ON(acrtc->pflip_status != AMDGPU_FLIP_NONE);
4140 	WARN_ON(!acrtc_state->stream);
4141 
4142 	addr.address.grph.addr.low_part = lower_32_bits(afb->address);
4143 	addr.address.grph.addr.high_part = upper_32_bits(afb->address);
4144 	addr.flip_immediate = async_flip;
4145 
4146 
4147 	if (acrtc->base.state->event)
4148 		prepare_flip_isr(acrtc);
4149 
4150 	spin_unlock_irqrestore(&crtc->dev->event_lock, flags);
4151 
4152 	surface_updates->surface = dc_stream_get_status(acrtc_state->stream)->plane_states[0];
4153 	surface_updates->flip_addr = &addr;
4154 
4155 	dc_commit_updates_for_stream(adev->dm.dc,
4156 					     surface_updates,
4157 					     1,
4158 					     acrtc_state->stream,
4159 					     NULL,
4160 					     &surface_updates->surface,
4161 					     state);
4162 
4163 	DRM_DEBUG_DRIVER("%s Flipping to hi: 0x%x, low: 0x%x \n",
4164 			 __func__,
4165 			 addr.address.grph.addr.high_part,
4166 			 addr.address.grph.addr.low_part);
4167 }
4168 
4169 /*
4170  * TODO this whole function needs to go
4171  *
4172  * dc_surface_update is needlessly complex. See if we can just replace this
4173  * with a dc_plane_state and follow the atomic model a bit more closely here.
4174  */
4175 static bool commit_planes_to_stream(
4176 		struct dc *dc,
4177 		struct dc_plane_state **plane_states,
4178 		uint8_t new_plane_count,
4179 		struct dm_crtc_state *dm_new_crtc_state,
4180 		struct dm_crtc_state *dm_old_crtc_state,
4181 		struct dc_state *state)
4182 {
4183 	/* no need to dynamically allocate this. it's pretty small */
4184 	struct dc_surface_update updates[MAX_SURFACES];
4185 	struct dc_flip_addrs *flip_addr;
4186 	struct dc_plane_info *plane_info;
4187 	struct dc_scaling_info *scaling_info;
4188 	int i;
4189 	struct dc_stream_state *dc_stream = dm_new_crtc_state->stream;
4190 	struct dc_stream_update *stream_update =
4191 			kzalloc(sizeof(struct dc_stream_update), GFP_KERNEL);
4192 
4193 	if (!stream_update) {
4194 		BREAK_TO_DEBUGGER();
4195 		return false;
4196 	}
4197 
4198 	flip_addr = kcalloc(MAX_SURFACES, sizeof(struct dc_flip_addrs),
4199 			    GFP_KERNEL);
4200 	plane_info = kcalloc(MAX_SURFACES, sizeof(struct dc_plane_info),
4201 			     GFP_KERNEL);
4202 	scaling_info = kcalloc(MAX_SURFACES, sizeof(struct dc_scaling_info),
4203 			       GFP_KERNEL);
4204 
4205 	if (!flip_addr || !plane_info || !scaling_info) {
4206 		kfree(flip_addr);
4207 		kfree(plane_info);
4208 		kfree(scaling_info);
4209 		kfree(stream_update);
4210 		return false;
4211 	}
4212 
4213 	memset(updates, 0, sizeof(updates));
4214 
4215 	stream_update->src = dc_stream->src;
4216 	stream_update->dst = dc_stream->dst;
4217 	stream_update->out_transfer_func = dc_stream->out_transfer_func;
4218 
4219 	for (i = 0; i < new_plane_count; i++) {
4220 		updates[i].surface = plane_states[i];
4221 		updates[i].gamma =
4222 			(struct dc_gamma *)plane_states[i]->gamma_correction;
4223 		updates[i].in_transfer_func = plane_states[i]->in_transfer_func;
4224 		flip_addr[i].address = plane_states[i]->address;
4225 		flip_addr[i].flip_immediate = plane_states[i]->flip_immediate;
4226 		plane_info[i].color_space = plane_states[i]->color_space;
4227 		plane_info[i].format = plane_states[i]->format;
4228 		plane_info[i].plane_size = plane_states[i]->plane_size;
4229 		plane_info[i].rotation = plane_states[i]->rotation;
4230 		plane_info[i].horizontal_mirror = plane_states[i]->horizontal_mirror;
4231 		plane_info[i].stereo_format = plane_states[i]->stereo_format;
4232 		plane_info[i].tiling_info = plane_states[i]->tiling_info;
4233 		plane_info[i].visible = plane_states[i]->visible;
4234 		plane_info[i].per_pixel_alpha = plane_states[i]->per_pixel_alpha;
4235 		plane_info[i].dcc = plane_states[i]->dcc;
4236 		scaling_info[i].scaling_quality = plane_states[i]->scaling_quality;
4237 		scaling_info[i].src_rect = plane_states[i]->src_rect;
4238 		scaling_info[i].dst_rect = plane_states[i]->dst_rect;
4239 		scaling_info[i].clip_rect = plane_states[i]->clip_rect;
4240 
4241 		updates[i].flip_addr = &flip_addr[i];
4242 		updates[i].plane_info = &plane_info[i];
4243 		updates[i].scaling_info = &scaling_info[i];
4244 	}
4245 
4246 	dc_commit_updates_for_stream(
4247 			dc,
4248 			updates,
4249 			new_plane_count,
4250 			dc_stream, stream_update, plane_states, state);
4251 
4252 	kfree(flip_addr);
4253 	kfree(plane_info);
4254 	kfree(scaling_info);
4255 	kfree(stream_update);
4256 	return true;
4257 }
4258 
4259 static void amdgpu_dm_commit_planes(struct drm_atomic_state *state,
4260 				    struct drm_device *dev,
4261 				    struct amdgpu_display_manager *dm,
4262 				    struct drm_crtc *pcrtc,
4263 				    bool *wait_for_vblank)
4264 {
4265 	uint32_t i;
4266 	struct drm_plane *plane;
4267 	struct drm_plane_state *old_plane_state, *new_plane_state;
4268 	struct dc_stream_state *dc_stream_attach;
4269 	struct dc_plane_state *plane_states_constructed[MAX_SURFACES];
4270 	struct amdgpu_crtc *acrtc_attach = to_amdgpu_crtc(pcrtc);
4271 	struct drm_crtc_state *new_pcrtc_state =
4272 			drm_atomic_get_new_crtc_state(state, pcrtc);
4273 	struct dm_crtc_state *acrtc_state = to_dm_crtc_state(new_pcrtc_state);
4274 	struct dm_crtc_state *dm_old_crtc_state =
4275 			to_dm_crtc_state(drm_atomic_get_old_crtc_state(state, pcrtc));
4276 	struct dm_atomic_state *dm_state = to_dm_atomic_state(state);
4277 	int planes_count = 0;
4278 	unsigned long flags;
4279 
4280 	/* update planes when needed */
4281 	for_each_oldnew_plane_in_state(state, plane, old_plane_state, new_plane_state, i) {
4282 		struct drm_crtc *crtc = new_plane_state->crtc;
4283 		struct drm_crtc_state *new_crtc_state;
4284 		struct drm_framebuffer *fb = new_plane_state->fb;
4285 		bool pflip_needed;
4286 		struct dm_plane_state *dm_new_plane_state = to_dm_plane_state(new_plane_state);
4287 
4288 		if (plane->type == DRM_PLANE_TYPE_CURSOR) {
4289 			handle_cursor_update(plane, old_plane_state);
4290 			continue;
4291 		}
4292 
4293 		if (!fb || !crtc || pcrtc != crtc)
4294 			continue;
4295 
4296 		new_crtc_state = drm_atomic_get_new_crtc_state(state, crtc);
4297 		if (!new_crtc_state->active)
4298 			continue;
4299 
4300 		pflip_needed = !state->allow_modeset;
4301 
4302 		spin_lock_irqsave(&crtc->dev->event_lock, flags);
4303 		if (acrtc_attach->pflip_status != AMDGPU_FLIP_NONE) {
4304 			DRM_ERROR("%s: acrtc %d, already busy\n",
4305 				  __func__,
4306 				  acrtc_attach->crtc_id);
4307 			/* In commit tail framework this cannot happen */
4308 			WARN_ON(1);
4309 		}
4310 		spin_unlock_irqrestore(&crtc->dev->event_lock, flags);
4311 
4312 		if (!pflip_needed || plane->type == DRM_PLANE_TYPE_OVERLAY) {
4313 			WARN_ON(!dm_new_plane_state->dc_state);
4314 
4315 			plane_states_constructed[planes_count] = dm_new_plane_state->dc_state;
4316 
4317 			dc_stream_attach = acrtc_state->stream;
4318 			planes_count++;
4319 
4320 		} else if (new_crtc_state->planes_changed) {
4321 			/* Assume even ONE crtc with immediate flip means
4322 			 * entire can't wait for VBLANK
4323 			 * TODO Check if it's correct
4324 			 */
4325 			*wait_for_vblank =
4326 					new_pcrtc_state->pageflip_flags & DRM_MODE_PAGE_FLIP_ASYNC ?
4327 				false : true;
4328 
4329 			/* TODO: Needs rework for multiplane flip */
4330 			if (plane->type == DRM_PLANE_TYPE_PRIMARY)
4331 				drm_crtc_vblank_get(crtc);
4332 
4333 			amdgpu_dm_do_flip(
4334 				crtc,
4335 				fb,
4336 				(uint32_t)drm_crtc_vblank_count(crtc) + *wait_for_vblank,
4337 				dm_state->context);
4338 		}
4339 
4340 	}
4341 
4342 	if (planes_count) {
4343 		unsigned long flags;
4344 
4345 		if (new_pcrtc_state->event) {
4346 
4347 			drm_crtc_vblank_get(pcrtc);
4348 
4349 			spin_lock_irqsave(&pcrtc->dev->event_lock, flags);
4350 			prepare_flip_isr(acrtc_attach);
4351 			spin_unlock_irqrestore(&pcrtc->dev->event_lock, flags);
4352 		}
4353 
4354 
4355 		if (false == commit_planes_to_stream(dm->dc,
4356 							plane_states_constructed,
4357 							planes_count,
4358 							acrtc_state,
4359 							dm_old_crtc_state,
4360 							dm_state->context))
4361 			dm_error("%s: Failed to attach plane!\n", __func__);
4362 	} else {
4363 		/*TODO BUG Here should go disable planes on CRTC. */
4364 	}
4365 }
4366 
4367 /**
4368  * amdgpu_dm_crtc_copy_transient_flags - copy mirrored flags from DRM to DC
4369  * @crtc_state: the DRM CRTC state
4370  * @stream_state: the DC stream state.
4371  *
4372  * Copy the mirrored transient state flags from DRM, to DC. It is used to bring
4373  * a dc_stream_state's flags in sync with a drm_crtc_state's flags.
4374  */
4375 static void amdgpu_dm_crtc_copy_transient_flags(struct drm_crtc_state *crtc_state,
4376 						struct dc_stream_state *stream_state)
4377 {
4378 	stream_state->mode_changed = drm_atomic_crtc_needs_modeset(crtc_state);
4379 }
4380 
4381 static int amdgpu_dm_atomic_commit(struct drm_device *dev,
4382 				   struct drm_atomic_state *state,
4383 				   bool nonblock)
4384 {
4385 	struct drm_crtc *crtc;
4386 	struct drm_crtc_state *old_crtc_state, *new_crtc_state;
4387 	struct amdgpu_device *adev = dev->dev_private;
4388 	int i;
4389 
4390 	/*
4391 	 * We evade vblanks and pflips on crtc that
4392 	 * should be changed. We do it here to flush & disable
4393 	 * interrupts before drm_swap_state is called in drm_atomic_helper_commit
4394 	 * it will update crtc->dm_crtc_state->stream pointer which is used in
4395 	 * the ISRs.
4396 	 */
4397 	for_each_oldnew_crtc_in_state(state, crtc, old_crtc_state, new_crtc_state, i) {
4398 		struct dm_crtc_state *dm_old_crtc_state = to_dm_crtc_state(old_crtc_state);
4399 		struct dm_crtc_state *dm_new_crtc_state = to_dm_crtc_state(new_crtc_state);
4400 		struct amdgpu_crtc *acrtc = to_amdgpu_crtc(crtc);
4401 
4402 		if (drm_atomic_crtc_needs_modeset(new_crtc_state)
4403 		    && dm_old_crtc_state->stream) {
4404 			/*
4405 			 * CRC capture was enabled but not disabled.
4406 			 * Release the vblank reference.
4407 			 */
4408 			if (dm_new_crtc_state->crc_enabled) {
4409 				drm_crtc_vblank_put(crtc);
4410 				dm_new_crtc_state->crc_enabled = false;
4411 			}
4412 
4413 			manage_dm_interrupts(adev, acrtc, false);
4414 		}
4415 	}
4416 	/* Add check here for SoC's that support hardware cursor plane, to
4417 	 * unset legacy_cursor_update */
4418 
4419 	return drm_atomic_helper_commit(dev, state, nonblock);
4420 
4421 	/*TODO Handle EINTR, reenable IRQ*/
4422 }
4423 
4424 static void amdgpu_dm_atomic_commit_tail(struct drm_atomic_state *state)
4425 {
4426 	struct drm_device *dev = state->dev;
4427 	struct amdgpu_device *adev = dev->dev_private;
4428 	struct amdgpu_display_manager *dm = &adev->dm;
4429 	struct dm_atomic_state *dm_state;
4430 	uint32_t i, j;
4431 	struct drm_crtc *crtc;
4432 	struct drm_crtc_state *old_crtc_state, *new_crtc_state;
4433 	unsigned long flags;
4434 	bool wait_for_vblank = true;
4435 	struct drm_connector *connector;
4436 	struct drm_connector_state *old_con_state, *new_con_state;
4437 	struct dm_crtc_state *dm_old_crtc_state, *dm_new_crtc_state;
4438 	int crtc_disable_count = 0;
4439 
4440 	drm_atomic_helper_update_legacy_modeset_state(dev, state);
4441 
4442 	dm_state = to_dm_atomic_state(state);
4443 
4444 	/* update changed items */
4445 	for_each_oldnew_crtc_in_state(state, crtc, old_crtc_state, new_crtc_state, i) {
4446 		struct amdgpu_crtc *acrtc = to_amdgpu_crtc(crtc);
4447 
4448 		dm_new_crtc_state = to_dm_crtc_state(new_crtc_state);
4449 		dm_old_crtc_state = to_dm_crtc_state(old_crtc_state);
4450 
4451 		DRM_DEBUG_DRIVER(
4452 			"amdgpu_crtc id:%d crtc_state_flags: enable:%d, active:%d, "
4453 			"planes_changed:%d, mode_changed:%d,active_changed:%d,"
4454 			"connectors_changed:%d\n",
4455 			acrtc->crtc_id,
4456 			new_crtc_state->enable,
4457 			new_crtc_state->active,
4458 			new_crtc_state->planes_changed,
4459 			new_crtc_state->mode_changed,
4460 			new_crtc_state->active_changed,
4461 			new_crtc_state->connectors_changed);
4462 
4463 		/* Copy all transient state flags into dc state */
4464 		if (dm_new_crtc_state->stream) {
4465 			amdgpu_dm_crtc_copy_transient_flags(&dm_new_crtc_state->base,
4466 							    dm_new_crtc_state->stream);
4467 		}
4468 
4469 		/* handles headless hotplug case, updating new_state and
4470 		 * aconnector as needed
4471 		 */
4472 
4473 		if (modeset_required(new_crtc_state, dm_new_crtc_state->stream, dm_old_crtc_state->stream)) {
4474 
4475 			DRM_DEBUG_DRIVER("Atomic commit: SET crtc id %d: [%p]\n", acrtc->crtc_id, acrtc);
4476 
4477 			if (!dm_new_crtc_state->stream) {
4478 				/*
4479 				 * this could happen because of issues with
4480 				 * userspace notifications delivery.
4481 				 * In this case userspace tries to set mode on
4482 				 * display which is disconnect in fact.
4483 				 * dc_sink in NULL in this case on aconnector.
4484 				 * We expect reset mode will come soon.
4485 				 *
4486 				 * This can also happen when unplug is done
4487 				 * during resume sequence ended
4488 				 *
4489 				 * In this case, we want to pretend we still
4490 				 * have a sink to keep the pipe running so that
4491 				 * hw state is consistent with the sw state
4492 				 */
4493 				DRM_DEBUG_DRIVER("%s: Failed to create new stream for crtc %d\n",
4494 						__func__, acrtc->base.base.id);
4495 				continue;
4496 			}
4497 
4498 			if (dm_old_crtc_state->stream)
4499 				remove_stream(adev, acrtc, dm_old_crtc_state->stream);
4500 
4501 			pm_runtime_get_noresume(dev->dev);
4502 
4503 			acrtc->enabled = true;
4504 			acrtc->hw_mode = new_crtc_state->mode;
4505 			crtc->hwmode = new_crtc_state->mode;
4506 		} else if (modereset_required(new_crtc_state)) {
4507 			DRM_DEBUG_DRIVER("Atomic commit: RESET. crtc id %d:[%p]\n", acrtc->crtc_id, acrtc);
4508 
4509 			/* i.e. reset mode */
4510 			if (dm_old_crtc_state->stream)
4511 				remove_stream(adev, acrtc, dm_old_crtc_state->stream);
4512 		}
4513 	} /* for_each_crtc_in_state() */
4514 
4515 	/*
4516 	 * Add streams after required streams from new and replaced streams
4517 	 * are removed from freesync module
4518 	 */
4519 	if (adev->dm.freesync_module) {
4520 		for_each_oldnew_crtc_in_state(state, crtc, old_crtc_state,
4521 					      new_crtc_state, i) {
4522 			struct amdgpu_dm_connector *aconnector = NULL;
4523 			struct dm_connector_state *dm_new_con_state = NULL;
4524 			struct amdgpu_crtc *acrtc = NULL;
4525 			bool modeset_needed;
4526 
4527 			dm_new_crtc_state = to_dm_crtc_state(new_crtc_state);
4528 			dm_old_crtc_state = to_dm_crtc_state(old_crtc_state);
4529 			modeset_needed = modeset_required(
4530 					new_crtc_state,
4531 					dm_new_crtc_state->stream,
4532 					dm_old_crtc_state->stream);
4533 			/* We add stream to freesync if:
4534 			 * 1. Said stream is not null, and
4535 			 * 2. A modeset is requested. This means that the
4536 			 *    stream was removed previously, and needs to be
4537 			 *    replaced.
4538 			 */
4539 			if (dm_new_crtc_state->stream == NULL ||
4540 					!modeset_needed)
4541 				continue;
4542 
4543 			acrtc = to_amdgpu_crtc(crtc);
4544 
4545 			aconnector =
4546 				amdgpu_dm_find_first_crtc_matching_connector(
4547 					state, crtc);
4548 			if (!aconnector) {
4549 				DRM_DEBUG_DRIVER("Atomic commit: Failed to "
4550 						 "find connector for acrtc "
4551 						 "id:%d skipping freesync "
4552 						 "init\n",
4553 						 acrtc->crtc_id);
4554 				continue;
4555 			}
4556 
4557 			mod_freesync_add_stream(adev->dm.freesync_module,
4558 						dm_new_crtc_state->stream,
4559 						&aconnector->caps);
4560 			new_con_state = drm_atomic_get_new_connector_state(
4561 					state, &aconnector->base);
4562 			dm_new_con_state = to_dm_connector_state(new_con_state);
4563 
4564 			mod_freesync_set_user_enable(adev->dm.freesync_module,
4565 						     &dm_new_crtc_state->stream,
4566 						     1,
4567 						     &dm_new_con_state->user_enable);
4568 		}
4569 	}
4570 
4571 	if (dm_state->context) {
4572 		dm_enable_per_frame_crtc_master_sync(dm_state->context);
4573 		WARN_ON(!dc_commit_state(dm->dc, dm_state->context));
4574 	}
4575 
4576 	for_each_new_crtc_in_state(state, crtc, new_crtc_state, i) {
4577 		struct amdgpu_crtc *acrtc = to_amdgpu_crtc(crtc);
4578 
4579 		dm_new_crtc_state = to_dm_crtc_state(new_crtc_state);
4580 
4581 		if (dm_new_crtc_state->stream != NULL) {
4582 			const struct dc_stream_status *status =
4583 					dc_stream_get_status(dm_new_crtc_state->stream);
4584 
4585 			if (!status)
4586 				DC_ERR("got no status for stream %p on acrtc%p\n", dm_new_crtc_state->stream, acrtc);
4587 			else
4588 				acrtc->otg_inst = status->primary_otg_inst;
4589 		}
4590 	}
4591 
4592 	/* Handle scaling and underscan changes*/
4593 	for_each_oldnew_connector_in_state(state, connector, old_con_state, new_con_state, i) {
4594 		struct dm_connector_state *dm_new_con_state = to_dm_connector_state(new_con_state);
4595 		struct dm_connector_state *dm_old_con_state = to_dm_connector_state(old_con_state);
4596 		struct amdgpu_crtc *acrtc = to_amdgpu_crtc(dm_new_con_state->base.crtc);
4597 		struct dc_stream_status *status = NULL;
4598 
4599 		if (acrtc) {
4600 			new_crtc_state = drm_atomic_get_new_crtc_state(state, &acrtc->base);
4601 			old_crtc_state = drm_atomic_get_old_crtc_state(state, &acrtc->base);
4602 		}
4603 
4604 		/* Skip any modesets/resets */
4605 		if (!acrtc || drm_atomic_crtc_needs_modeset(new_crtc_state))
4606 			continue;
4607 
4608 		/* Skip any thing not scale or underscan changes */
4609 		if (!is_scaling_state_different(dm_new_con_state, dm_old_con_state))
4610 			continue;
4611 
4612 		dm_new_crtc_state = to_dm_crtc_state(new_crtc_state);
4613 
4614 		update_stream_scaling_settings(&dm_new_con_state->base.crtc->mode,
4615 				dm_new_con_state, (struct dc_stream_state *)dm_new_crtc_state->stream);
4616 
4617 		if (!dm_new_crtc_state->stream)
4618 			continue;
4619 
4620 		status = dc_stream_get_status(dm_new_crtc_state->stream);
4621 		WARN_ON(!status);
4622 		WARN_ON(!status->plane_count);
4623 
4624 		/*TODO How it works with MPO ?*/
4625 		if (!commit_planes_to_stream(
4626 				dm->dc,
4627 				status->plane_states,
4628 				status->plane_count,
4629 				dm_new_crtc_state,
4630 				to_dm_crtc_state(old_crtc_state),
4631 				dm_state->context))
4632 			dm_error("%s: Failed to update stream scaling!\n", __func__);
4633 	}
4634 
4635 	for_each_oldnew_crtc_in_state(state, crtc, old_crtc_state,
4636 			new_crtc_state, i) {
4637 		/*
4638 		 * loop to enable interrupts on newly arrived crtc
4639 		 */
4640 		struct amdgpu_crtc *acrtc = to_amdgpu_crtc(crtc);
4641 		bool modeset_needed;
4642 
4643 		if (old_crtc_state->active && !new_crtc_state->active)
4644 			crtc_disable_count++;
4645 
4646 		dm_new_crtc_state = to_dm_crtc_state(new_crtc_state);
4647 		dm_old_crtc_state = to_dm_crtc_state(old_crtc_state);
4648 		modeset_needed = modeset_required(
4649 				new_crtc_state,
4650 				dm_new_crtc_state->stream,
4651 				dm_old_crtc_state->stream);
4652 
4653 		if (dm_new_crtc_state->stream == NULL || !modeset_needed)
4654 			continue;
4655 
4656 		if (adev->dm.freesync_module)
4657 			mod_freesync_notify_mode_change(
4658 				adev->dm.freesync_module,
4659 				&dm_new_crtc_state->stream, 1);
4660 
4661 		manage_dm_interrupts(adev, acrtc, true);
4662 	}
4663 
4664 	/* update planes when needed per crtc*/
4665 	for_each_new_crtc_in_state(state, crtc, new_crtc_state, j) {
4666 		dm_new_crtc_state = to_dm_crtc_state(new_crtc_state);
4667 
4668 		if (dm_new_crtc_state->stream)
4669 			amdgpu_dm_commit_planes(state, dev, dm, crtc, &wait_for_vblank);
4670 	}
4671 
4672 
4673 	/*
4674 	 * send vblank event on all events not handled in flip and
4675 	 * mark consumed event for drm_atomic_helper_commit_hw_done
4676 	 */
4677 	spin_lock_irqsave(&adev->ddev->event_lock, flags);
4678 	for_each_new_crtc_in_state(state, crtc, new_crtc_state, i) {
4679 
4680 		if (new_crtc_state->event)
4681 			drm_send_event_locked(dev, &new_crtc_state->event->base);
4682 
4683 		new_crtc_state->event = NULL;
4684 	}
4685 	spin_unlock_irqrestore(&adev->ddev->event_lock, flags);
4686 
4687 
4688 	if (wait_for_vblank)
4689 		drm_atomic_helper_wait_for_flip_done(dev, state);
4690 
4691 	/*
4692 	 * FIXME:
4693 	 * Delay hw_done() until flip_done() is signaled. This is to block
4694 	 * another commit from freeing the CRTC state while we're still
4695 	 * waiting on flip_done.
4696 	 */
4697 	drm_atomic_helper_commit_hw_done(state);
4698 
4699 	drm_atomic_helper_cleanup_planes(dev, state);
4700 
4701 	/* Finally, drop a runtime PM reference for each newly disabled CRTC,
4702 	 * so we can put the GPU into runtime suspend if we're not driving any
4703 	 * displays anymore
4704 	 */
4705 	for (i = 0; i < crtc_disable_count; i++)
4706 		pm_runtime_put_autosuspend(dev->dev);
4707 	pm_runtime_mark_last_busy(dev->dev);
4708 }
4709 
4710 
4711 static int dm_force_atomic_commit(struct drm_connector *connector)
4712 {
4713 	int ret = 0;
4714 	struct drm_device *ddev = connector->dev;
4715 	struct drm_atomic_state *state = drm_atomic_state_alloc(ddev);
4716 	struct amdgpu_crtc *disconnected_acrtc = to_amdgpu_crtc(connector->encoder->crtc);
4717 	struct drm_plane *plane = disconnected_acrtc->base.primary;
4718 	struct drm_connector_state *conn_state;
4719 	struct drm_crtc_state *crtc_state;
4720 	struct drm_plane_state *plane_state;
4721 
4722 	if (!state)
4723 		return -ENOMEM;
4724 
4725 	state->acquire_ctx = ddev->mode_config.acquire_ctx;
4726 
4727 	/* Construct an atomic state to restore previous display setting */
4728 
4729 	/*
4730 	 * Attach connectors to drm_atomic_state
4731 	 */
4732 	conn_state = drm_atomic_get_connector_state(state, connector);
4733 
4734 	ret = PTR_ERR_OR_ZERO(conn_state);
4735 	if (ret)
4736 		goto out;
4737 
4738 	/* Attach crtc to drm_atomic_state*/
4739 	crtc_state = drm_atomic_get_crtc_state(state, &disconnected_acrtc->base);
4740 
4741 	ret = PTR_ERR_OR_ZERO(crtc_state);
4742 	if (ret)
4743 		goto out;
4744 
4745 	/* force a restore */
4746 	crtc_state->mode_changed = true;
4747 
4748 	/* Attach plane to drm_atomic_state */
4749 	plane_state = drm_atomic_get_plane_state(state, plane);
4750 
4751 	ret = PTR_ERR_OR_ZERO(plane_state);
4752 	if (ret)
4753 		goto out;
4754 
4755 	/* Call commit internally with the state we just constructed */
4756 	ret = drm_atomic_commit(state);
4757 
4758 out:
4759 	drm_atomic_state_put(state);
4760 	if (ret)
4761 		DRM_ERROR("Restoring old state failed with %i\n", ret);
4762 
4763 	return ret;
4764 }
4765 
4766 /*
4767  * This functions handle all cases when set mode does not come upon hotplug.
4768  * This include when the same display is unplugged then plugged back into the
4769  * same port and when we are running without usermode desktop manager supprot
4770  */
4771 void dm_restore_drm_connector_state(struct drm_device *dev,
4772 				    struct drm_connector *connector)
4773 {
4774 	struct amdgpu_dm_connector *aconnector = to_amdgpu_dm_connector(connector);
4775 	struct amdgpu_crtc *disconnected_acrtc;
4776 	struct dm_crtc_state *acrtc_state;
4777 
4778 	if (!aconnector->dc_sink || !connector->state || !connector->encoder)
4779 		return;
4780 
4781 	disconnected_acrtc = to_amdgpu_crtc(connector->encoder->crtc);
4782 	if (!disconnected_acrtc)
4783 		return;
4784 
4785 	acrtc_state = to_dm_crtc_state(disconnected_acrtc->base.state);
4786 	if (!acrtc_state->stream)
4787 		return;
4788 
4789 	/*
4790 	 * If the previous sink is not released and different from the current,
4791 	 * we deduce we are in a state where we can not rely on usermode call
4792 	 * to turn on the display, so we do it here
4793 	 */
4794 	if (acrtc_state->stream->sink != aconnector->dc_sink)
4795 		dm_force_atomic_commit(&aconnector->base);
4796 }
4797 
4798 /*`
4799  * Grabs all modesetting locks to serialize against any blocking commits,
4800  * Waits for completion of all non blocking commits.
4801  */
4802 static int do_aquire_global_lock(struct drm_device *dev,
4803 				 struct drm_atomic_state *state)
4804 {
4805 	struct drm_crtc *crtc;
4806 	struct drm_crtc_commit *commit;
4807 	long ret;
4808 
4809 	/* Adding all modeset locks to aquire_ctx will
4810 	 * ensure that when the framework release it the
4811 	 * extra locks we are locking here will get released to
4812 	 */
4813 	ret = drm_modeset_lock_all_ctx(dev, state->acquire_ctx);
4814 	if (ret)
4815 		return ret;
4816 
4817 	list_for_each_entry(crtc, &dev->mode_config.crtc_list, head) {
4818 		spin_lock(&crtc->commit_lock);
4819 		commit = list_first_entry_or_null(&crtc->commit_list,
4820 				struct drm_crtc_commit, commit_entry);
4821 		if (commit)
4822 			drm_crtc_commit_get(commit);
4823 		spin_unlock(&crtc->commit_lock);
4824 
4825 		if (!commit)
4826 			continue;
4827 
4828 		/* Make sure all pending HW programming completed and
4829 		 * page flips done
4830 		 */
4831 		ret = wait_for_completion_interruptible_timeout(&commit->hw_done, 10*HZ);
4832 
4833 		if (ret > 0)
4834 			ret = wait_for_completion_interruptible_timeout(
4835 					&commit->flip_done, 10*HZ);
4836 
4837 		if (ret == 0)
4838 			DRM_ERROR("[CRTC:%d:%s] hw_done or flip_done "
4839 				  "timed out\n", crtc->base.id, crtc->name);
4840 
4841 		drm_crtc_commit_put(commit);
4842 	}
4843 
4844 	return ret < 0 ? ret : 0;
4845 }
4846 
4847 static int dm_update_crtcs_state(struct dc *dc,
4848 				 struct drm_atomic_state *state,
4849 				 bool enable,
4850 				 bool *lock_and_validation_needed)
4851 {
4852 	struct drm_crtc *crtc;
4853 	struct drm_crtc_state *old_crtc_state, *new_crtc_state;
4854 	int i;
4855 	struct dm_crtc_state *dm_old_crtc_state, *dm_new_crtc_state;
4856 	struct dm_atomic_state *dm_state = to_dm_atomic_state(state);
4857 	struct dc_stream_state *new_stream;
4858 	int ret = 0;
4859 
4860 	/*TODO Move this code into dm_crtc_atomic_check once we get rid of dc_validation_set */
4861 	/* update changed items */
4862 	for_each_oldnew_crtc_in_state(state, crtc, old_crtc_state, new_crtc_state, i) {
4863 		struct amdgpu_crtc *acrtc = NULL;
4864 		struct amdgpu_dm_connector *aconnector = NULL;
4865 		struct drm_connector_state *drm_new_conn_state = NULL, *drm_old_conn_state = NULL;
4866 		struct dm_connector_state *dm_new_conn_state = NULL, *dm_old_conn_state = NULL;
4867 		struct drm_plane_state *new_plane_state = NULL;
4868 
4869 		new_stream = NULL;
4870 
4871 		dm_old_crtc_state = to_dm_crtc_state(old_crtc_state);
4872 		dm_new_crtc_state = to_dm_crtc_state(new_crtc_state);
4873 		acrtc = to_amdgpu_crtc(crtc);
4874 
4875 		new_plane_state = drm_atomic_get_new_plane_state(state, new_crtc_state->crtc->primary);
4876 
4877 		if (new_crtc_state->enable && new_plane_state && !new_plane_state->fb) {
4878 			ret = -EINVAL;
4879 			goto fail;
4880 		}
4881 
4882 		aconnector = amdgpu_dm_find_first_crtc_matching_connector(state, crtc);
4883 
4884 		/* TODO This hack should go away */
4885 		if (aconnector && enable) {
4886 			// Make sure fake sink is created in plug-in scenario
4887 			drm_new_conn_state = drm_atomic_get_new_connector_state(state,
4888  								    &aconnector->base);
4889 			drm_old_conn_state = drm_atomic_get_old_connector_state(state,
4890 								    &aconnector->base);
4891 
4892 			if (IS_ERR(drm_new_conn_state)) {
4893 				ret = PTR_ERR_OR_ZERO(drm_new_conn_state);
4894 				break;
4895 			}
4896 
4897 			dm_new_conn_state = to_dm_connector_state(drm_new_conn_state);
4898 			dm_old_conn_state = to_dm_connector_state(drm_old_conn_state);
4899 
4900 			new_stream = create_stream_for_sink(aconnector,
4901 							     &new_crtc_state->mode,
4902 							    dm_new_conn_state);
4903 
4904 			/*
4905 			 * we can have no stream on ACTION_SET if a display
4906 			 * was disconnected during S3, in this case it not and
4907 			 * error, the OS will be updated after detection, and
4908 			 * do the right thing on next atomic commit
4909 			 */
4910 
4911 			if (!new_stream) {
4912 				DRM_DEBUG_DRIVER("%s: Failed to create new stream for crtc %d\n",
4913 						__func__, acrtc->base.base.id);
4914 				break;
4915 			}
4916 
4917 			if (dc_is_stream_unchanged(new_stream, dm_old_crtc_state->stream) &&
4918 			    dc_is_stream_scaling_unchanged(new_stream, dm_old_crtc_state->stream)) {
4919 				new_crtc_state->mode_changed = false;
4920 				DRM_DEBUG_DRIVER("Mode change not required, setting mode_changed to %d",
4921 						 new_crtc_state->mode_changed);
4922 			}
4923 		}
4924 
4925 		if (!drm_atomic_crtc_needs_modeset(new_crtc_state))
4926 			goto next_crtc;
4927 
4928 		DRM_DEBUG_DRIVER(
4929 			"amdgpu_crtc id:%d crtc_state_flags: enable:%d, active:%d, "
4930 			"planes_changed:%d, mode_changed:%d,active_changed:%d,"
4931 			"connectors_changed:%d\n",
4932 			acrtc->crtc_id,
4933 			new_crtc_state->enable,
4934 			new_crtc_state->active,
4935 			new_crtc_state->planes_changed,
4936 			new_crtc_state->mode_changed,
4937 			new_crtc_state->active_changed,
4938 			new_crtc_state->connectors_changed);
4939 
4940 		/* Remove stream for any changed/disabled CRTC */
4941 		if (!enable) {
4942 
4943 			if (!dm_old_crtc_state->stream)
4944 				goto next_crtc;
4945 
4946 			DRM_DEBUG_DRIVER("Disabling DRM crtc: %d\n",
4947 					crtc->base.id);
4948 
4949 			/* i.e. reset mode */
4950 			if (dc_remove_stream_from_ctx(
4951 					dc,
4952 					dm_state->context,
4953 					dm_old_crtc_state->stream) != DC_OK) {
4954 				ret = -EINVAL;
4955 				goto fail;
4956 			}
4957 
4958 			dc_stream_release(dm_old_crtc_state->stream);
4959 			dm_new_crtc_state->stream = NULL;
4960 
4961 			*lock_and_validation_needed = true;
4962 
4963 		} else {/* Add stream for any updated/enabled CRTC */
4964 			/*
4965 			 * Quick fix to prevent NULL pointer on new_stream when
4966 			 * added MST connectors not found in existing crtc_state in the chained mode
4967 			 * TODO: need to dig out the root cause of that
4968 			 */
4969 			if (!aconnector || (!aconnector->dc_sink && aconnector->mst_port))
4970 				goto next_crtc;
4971 
4972 			if (modereset_required(new_crtc_state))
4973 				goto next_crtc;
4974 
4975 			if (modeset_required(new_crtc_state, new_stream,
4976 					     dm_old_crtc_state->stream)) {
4977 
4978 				WARN_ON(dm_new_crtc_state->stream);
4979 
4980 				dm_new_crtc_state->stream = new_stream;
4981 
4982 				dc_stream_retain(new_stream);
4983 
4984 				DRM_DEBUG_DRIVER("Enabling DRM crtc: %d\n",
4985 							crtc->base.id);
4986 
4987 				if (dc_add_stream_to_ctx(
4988 						dc,
4989 						dm_state->context,
4990 						dm_new_crtc_state->stream) != DC_OK) {
4991 					ret = -EINVAL;
4992 					goto fail;
4993 				}
4994 
4995 				*lock_and_validation_needed = true;
4996 			}
4997 		}
4998 
4999 next_crtc:
5000 		/* Release extra reference */
5001 		if (new_stream)
5002 			 dc_stream_release(new_stream);
5003 
5004 		/*
5005 		 * We want to do dc stream updates that do not require a
5006 		 * full modeset below.
5007 		 */
5008 		if (!(enable && aconnector && new_crtc_state->enable &&
5009 		      new_crtc_state->active))
5010 			continue;
5011 		/*
5012 		 * Given above conditions, the dc state cannot be NULL because:
5013 		 * 1. We're in the process of enabling CRTCs (just been added
5014 		 *    to the dc context, or already is on the context)
5015 		 * 2. Has a valid connector attached, and
5016 		 * 3. Is currently active and enabled.
5017 		 * => The dc stream state currently exists.
5018 		 */
5019 		BUG_ON(dm_new_crtc_state->stream == NULL);
5020 
5021 		/* Scaling or underscan settings */
5022 		if (is_scaling_state_different(dm_old_conn_state, dm_new_conn_state))
5023 			update_stream_scaling_settings(
5024 				&new_crtc_state->mode, dm_new_conn_state, dm_new_crtc_state->stream);
5025 
5026 		/*
5027 		 * Color management settings. We also update color properties
5028 		 * when a modeset is needed, to ensure it gets reprogrammed.
5029 		 */
5030 		if (dm_new_crtc_state->base.color_mgmt_changed ||
5031 		    drm_atomic_crtc_needs_modeset(new_crtc_state)) {
5032 			ret = amdgpu_dm_set_regamma_lut(dm_new_crtc_state);
5033 			if (ret)
5034 				goto fail;
5035 			amdgpu_dm_set_ctm(dm_new_crtc_state);
5036 		}
5037 	}
5038 
5039 	return ret;
5040 
5041 fail:
5042 	if (new_stream)
5043 		dc_stream_release(new_stream);
5044 	return ret;
5045 }
5046 
5047 static int dm_update_planes_state(struct dc *dc,
5048 				  struct drm_atomic_state *state,
5049 				  bool enable,
5050 				  bool *lock_and_validation_needed)
5051 {
5052 	struct drm_crtc *new_plane_crtc, *old_plane_crtc;
5053 	struct drm_crtc_state *old_crtc_state, *new_crtc_state;
5054 	struct drm_plane *plane;
5055 	struct drm_plane_state *old_plane_state, *new_plane_state;
5056 	struct dm_crtc_state *dm_new_crtc_state, *dm_old_crtc_state;
5057 	struct dm_atomic_state *dm_state = to_dm_atomic_state(state);
5058 	struct dm_plane_state *dm_new_plane_state, *dm_old_plane_state;
5059 	int i ;
5060 	/* TODO return page_flip_needed() function */
5061 	bool pflip_needed  = !state->allow_modeset;
5062 	int ret = 0;
5063 
5064 
5065 	/* Add new planes, in reverse order as DC expectation */
5066 	for_each_oldnew_plane_in_state_reverse(state, plane, old_plane_state, new_plane_state, i) {
5067 		new_plane_crtc = new_plane_state->crtc;
5068 		old_plane_crtc = old_plane_state->crtc;
5069 		dm_new_plane_state = to_dm_plane_state(new_plane_state);
5070 		dm_old_plane_state = to_dm_plane_state(old_plane_state);
5071 
5072 		/*TODO Implement atomic check for cursor plane */
5073 		if (plane->type == DRM_PLANE_TYPE_CURSOR)
5074 			continue;
5075 
5076 		/* Remove any changed/removed planes */
5077 		if (!enable) {
5078 			if (pflip_needed &&
5079 			    plane->type != DRM_PLANE_TYPE_OVERLAY)
5080 				continue;
5081 
5082 			if (!old_plane_crtc)
5083 				continue;
5084 
5085 			old_crtc_state = drm_atomic_get_old_crtc_state(
5086 					state, old_plane_crtc);
5087 			dm_old_crtc_state = to_dm_crtc_state(old_crtc_state);
5088 
5089 			if (!dm_old_crtc_state->stream)
5090 				continue;
5091 
5092 			DRM_DEBUG_ATOMIC("Disabling DRM plane: %d on DRM crtc %d\n",
5093 					plane->base.id, old_plane_crtc->base.id);
5094 
5095 			if (!dc_remove_plane_from_context(
5096 					dc,
5097 					dm_old_crtc_state->stream,
5098 					dm_old_plane_state->dc_state,
5099 					dm_state->context)) {
5100 
5101 				ret = EINVAL;
5102 				return ret;
5103 			}
5104 
5105 
5106 			dc_plane_state_release(dm_old_plane_state->dc_state);
5107 			dm_new_plane_state->dc_state = NULL;
5108 
5109 			*lock_and_validation_needed = true;
5110 
5111 		} else { /* Add new planes */
5112 			struct dc_plane_state *dc_new_plane_state;
5113 
5114 			if (drm_atomic_plane_disabling(plane->state, new_plane_state))
5115 				continue;
5116 
5117 			if (!new_plane_crtc)
5118 				continue;
5119 
5120 			new_crtc_state = drm_atomic_get_new_crtc_state(state, new_plane_crtc);
5121 			dm_new_crtc_state = to_dm_crtc_state(new_crtc_state);
5122 
5123 			if (!dm_new_crtc_state->stream)
5124 				continue;
5125 
5126 			if (pflip_needed &&
5127 			    plane->type != DRM_PLANE_TYPE_OVERLAY)
5128 				continue;
5129 
5130 			WARN_ON(dm_new_plane_state->dc_state);
5131 
5132 			dc_new_plane_state = dc_create_plane_state(dc);
5133 			if (!dc_new_plane_state)
5134 				return -ENOMEM;
5135 
5136 			DRM_DEBUG_DRIVER("Enabling DRM plane: %d on DRM crtc %d\n",
5137 					plane->base.id, new_plane_crtc->base.id);
5138 
5139 			ret = fill_plane_attributes(
5140 				new_plane_crtc->dev->dev_private,
5141 				dc_new_plane_state,
5142 				new_plane_state,
5143 				new_crtc_state);
5144 			if (ret) {
5145 				dc_plane_state_release(dc_new_plane_state);
5146 				return ret;
5147 			}
5148 
5149 			/*
5150 			 * Any atomic check errors that occur after this will
5151 			 * not need a release. The plane state will be attached
5152 			 * to the stream, and therefore part of the atomic
5153 			 * state. It'll be released when the atomic state is
5154 			 * cleaned.
5155 			 */
5156 			if (!dc_add_plane_to_context(
5157 					dc,
5158 					dm_new_crtc_state->stream,
5159 					dc_new_plane_state,
5160 					dm_state->context)) {
5161 
5162 				dc_plane_state_release(dc_new_plane_state);
5163 				return -EINVAL;
5164 			}
5165 
5166 			dm_new_plane_state->dc_state = dc_new_plane_state;
5167 
5168 			/* Tell DC to do a full surface update every time there
5169 			 * is a plane change. Inefficient, but works for now.
5170 			 */
5171 			dm_new_plane_state->dc_state->update_flags.bits.full_update = 1;
5172 
5173 			*lock_and_validation_needed = true;
5174 		}
5175 	}
5176 
5177 
5178 	return ret;
5179 }
5180 
5181 static int amdgpu_dm_atomic_check(struct drm_device *dev,
5182 				  struct drm_atomic_state *state)
5183 {
5184 	struct amdgpu_device *adev = dev->dev_private;
5185 	struct dc *dc = adev->dm.dc;
5186 	struct dm_atomic_state *dm_state = to_dm_atomic_state(state);
5187 	struct drm_connector *connector;
5188 	struct drm_connector_state *old_con_state, *new_con_state;
5189 	struct drm_crtc *crtc;
5190 	struct drm_crtc_state *old_crtc_state, *new_crtc_state;
5191 	int ret, i;
5192 
5193 	/*
5194 	 * This bool will be set for true for any modeset/reset
5195 	 * or plane update which implies non fast surface update.
5196 	 */
5197 	bool lock_and_validation_needed = false;
5198 
5199 	ret = drm_atomic_helper_check_modeset(dev, state);
5200 	if (ret)
5201 		goto fail;
5202 
5203 	for_each_oldnew_crtc_in_state(state, crtc, old_crtc_state, new_crtc_state, i) {
5204 		if (!drm_atomic_crtc_needs_modeset(new_crtc_state) &&
5205 		    !new_crtc_state->color_mgmt_changed)
5206 			continue;
5207 
5208 		if (!new_crtc_state->enable)
5209 			continue;
5210 
5211 		ret = drm_atomic_add_affected_connectors(state, crtc);
5212 		if (ret)
5213 			return ret;
5214 
5215 		ret = drm_atomic_add_affected_planes(state, crtc);
5216 		if (ret)
5217 			goto fail;
5218 	}
5219 
5220 	dm_state->context = dc_create_state();
5221 	ASSERT(dm_state->context);
5222 	dc_resource_state_copy_construct_current(dc, dm_state->context);
5223 
5224 	/* Remove exiting planes if they are modified */
5225 	ret = dm_update_planes_state(dc, state, false, &lock_and_validation_needed);
5226 	if (ret) {
5227 		goto fail;
5228 	}
5229 
5230 	/* Disable all crtcs which require disable */
5231 	ret = dm_update_crtcs_state(dc, state, false, &lock_and_validation_needed);
5232 	if (ret) {
5233 		goto fail;
5234 	}
5235 
5236 	/* Enable all crtcs which require enable */
5237 	ret = dm_update_crtcs_state(dc, state, true, &lock_and_validation_needed);
5238 	if (ret) {
5239 		goto fail;
5240 	}
5241 
5242 	/* Add new/modified planes */
5243 	ret = dm_update_planes_state(dc, state, true, &lock_and_validation_needed);
5244 	if (ret) {
5245 		goto fail;
5246 	}
5247 
5248 	/* Run this here since we want to validate the streams we created */
5249 	ret = drm_atomic_helper_check_planes(dev, state);
5250 	if (ret)
5251 		goto fail;
5252 
5253 	/* Check scaling and underscan changes*/
5254 	/*TODO Removed scaling changes validation due to inability to commit
5255 	 * new stream into context w\o causing full reset. Need to
5256 	 * decide how to handle.
5257 	 */
5258 	for_each_oldnew_connector_in_state(state, connector, old_con_state, new_con_state, i) {
5259 		struct dm_connector_state *dm_old_con_state = to_dm_connector_state(old_con_state);
5260 		struct dm_connector_state *dm_new_con_state = to_dm_connector_state(new_con_state);
5261 		struct amdgpu_crtc *acrtc = to_amdgpu_crtc(dm_new_con_state->base.crtc);
5262 
5263 		/* Skip any modesets/resets */
5264 		if (!acrtc || drm_atomic_crtc_needs_modeset(
5265 				drm_atomic_get_new_crtc_state(state, &acrtc->base)))
5266 			continue;
5267 
5268 		/* Skip any thing not scale or underscan changes */
5269 		if (!is_scaling_state_different(dm_new_con_state, dm_old_con_state))
5270 			continue;
5271 
5272 		lock_and_validation_needed = true;
5273 	}
5274 
5275 	/*
5276 	 * For full updates case when
5277 	 * removing/adding/updating  streams on once CRTC while flipping
5278 	 * on another CRTC,
5279 	 * acquiring global lock  will guarantee that any such full
5280 	 * update commit
5281 	 * will wait for completion of any outstanding flip using DRMs
5282 	 * synchronization events.
5283 	 */
5284 
5285 	if (lock_and_validation_needed) {
5286 
5287 		ret = do_aquire_global_lock(dev, state);
5288 		if (ret)
5289 			goto fail;
5290 
5291 		if (dc_validate_global_state(dc, dm_state->context) != DC_OK) {
5292 			ret = -EINVAL;
5293 			goto fail;
5294 		}
5295 	}
5296 
5297 	/* Must be success */
5298 	WARN_ON(ret);
5299 	return ret;
5300 
5301 fail:
5302 	if (ret == -EDEADLK)
5303 		DRM_DEBUG_DRIVER("Atomic check stopped to avoid deadlock.\n");
5304 	else if (ret == -EINTR || ret == -EAGAIN || ret == -ERESTARTSYS)
5305 		DRM_DEBUG_DRIVER("Atomic check stopped due to signal.\n");
5306 	else
5307 		DRM_DEBUG_DRIVER("Atomic check failed with err: %d \n", ret);
5308 
5309 	return ret;
5310 }
5311 
5312 static bool is_dp_capable_without_timing_msa(struct dc *dc,
5313 					     struct amdgpu_dm_connector *amdgpu_dm_connector)
5314 {
5315 	uint8_t dpcd_data;
5316 	bool capable = false;
5317 
5318 	if (amdgpu_dm_connector->dc_link &&
5319 		dm_helpers_dp_read_dpcd(
5320 				NULL,
5321 				amdgpu_dm_connector->dc_link,
5322 				DP_DOWN_STREAM_PORT_COUNT,
5323 				&dpcd_data,
5324 				sizeof(dpcd_data))) {
5325 		capable = (dpcd_data & DP_MSA_TIMING_PAR_IGNORED) ? true:false;
5326 	}
5327 
5328 	return capable;
5329 }
5330 void amdgpu_dm_add_sink_to_freesync_module(struct drm_connector *connector,
5331 					   struct edid *edid)
5332 {
5333 	int i;
5334 	bool edid_check_required;
5335 	struct detailed_timing *timing;
5336 	struct detailed_non_pixel *data;
5337 	struct detailed_data_monitor_range *range;
5338 	struct amdgpu_dm_connector *amdgpu_dm_connector =
5339 			to_amdgpu_dm_connector(connector);
5340 	struct dm_connector_state *dm_con_state;
5341 
5342 	struct drm_device *dev = connector->dev;
5343 	struct amdgpu_device *adev = dev->dev_private;
5344 
5345 	if (!connector->state) {
5346 		DRM_ERROR("%s - Connector has no state", __func__);
5347 		return;
5348 	}
5349 
5350 	dm_con_state = to_dm_connector_state(connector->state);
5351 
5352 	edid_check_required = false;
5353 	if (!amdgpu_dm_connector->dc_sink) {
5354 		DRM_ERROR("dc_sink NULL, could not add free_sync module.\n");
5355 		return;
5356 	}
5357 	if (!adev->dm.freesync_module)
5358 		return;
5359 	/*
5360 	 * if edid non zero restrict freesync only for dp and edp
5361 	 */
5362 	if (edid) {
5363 		if (amdgpu_dm_connector->dc_sink->sink_signal == SIGNAL_TYPE_DISPLAY_PORT
5364 			|| amdgpu_dm_connector->dc_sink->sink_signal == SIGNAL_TYPE_EDP) {
5365 			edid_check_required = is_dp_capable_without_timing_msa(
5366 						adev->dm.dc,
5367 						amdgpu_dm_connector);
5368 		}
5369 	}
5370 	dm_con_state->freesync_capable = false;
5371 	if (edid_check_required == true && (edid->version > 1 ||
5372 	   (edid->version == 1 && edid->revision > 1))) {
5373 		for (i = 0; i < 4; i++) {
5374 
5375 			timing	= &edid->detailed_timings[i];
5376 			data	= &timing->data.other_data;
5377 			range	= &data->data.range;
5378 			/*
5379 			 * Check if monitor has continuous frequency mode
5380 			 */
5381 			if (data->type != EDID_DETAIL_MONITOR_RANGE)
5382 				continue;
5383 			/*
5384 			 * Check for flag range limits only. If flag == 1 then
5385 			 * no additional timing information provided.
5386 			 * Default GTF, GTF Secondary curve and CVT are not
5387 			 * supported
5388 			 */
5389 			if (range->flags != 1)
5390 				continue;
5391 
5392 			amdgpu_dm_connector->min_vfreq = range->min_vfreq;
5393 			amdgpu_dm_connector->max_vfreq = range->max_vfreq;
5394 			amdgpu_dm_connector->pixel_clock_mhz =
5395 				range->pixel_clock_mhz * 10;
5396 			break;
5397 		}
5398 
5399 		if (amdgpu_dm_connector->max_vfreq -
5400 				amdgpu_dm_connector->min_vfreq > 10) {
5401 			amdgpu_dm_connector->caps.supported = true;
5402 			amdgpu_dm_connector->caps.min_refresh_in_micro_hz =
5403 					amdgpu_dm_connector->min_vfreq * 1000000;
5404 			amdgpu_dm_connector->caps.max_refresh_in_micro_hz =
5405 					amdgpu_dm_connector->max_vfreq * 1000000;
5406 			dm_con_state->freesync_capable = true;
5407 		}
5408 	}
5409 
5410 	/*
5411 	 * TODO figure out how to notify user-mode or DRM of freesync caps
5412 	 * once we figure out how to deal with freesync in an upstreamable
5413 	 * fashion
5414 	 */
5415 
5416 }
5417 
5418 void amdgpu_dm_remove_sink_from_freesync_module(struct drm_connector *connector)
5419 {
5420 	/*
5421 	 * TODO fill in once we figure out how to deal with freesync in
5422 	 * an upstreamable fashion
5423 	 */
5424 }
5425