xref: /linux/drivers/gpu/drm/amd/display/amdgpu_dm/amdgpu_dm.c (revision 8cba48e7a813eacf241470ead5301bfcb680513a)
1 // SPDX-License-Identifier: MIT
2 /*
3  * Copyright 2015-2026 Advanced Micro Devices, Inc.
4  *
5  * Permission is hereby granted, free of charge, to any person obtaining a
6  * copy of this software and associated documentation files (the "Software"),
7  * to deal in the Software without restriction, including without limitation
8  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
9  * and/or sell copies of the Software, and to permit persons to whom the
10  * Software is furnished to do so, subject to the following conditions:
11  *
12  * The above copyright notice and this permission notice shall be included in
13  * all copies or substantial portions of the Software.
14  *
15  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
18  * THE COPYRIGHT HOLDER(S) OR AUTHOR(S) BE LIABLE FOR ANY CLAIM, DAMAGES OR
19  * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
20  * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
21  * OTHER DEALINGS IN THE SOFTWARE.
22  *
23  * Authors: AMD
24  *
25  */
26 
27 /* The caprices of the preprocessor require that this be declared right here */
28 #define CREATE_TRACE_POINTS
29 
30 #include "dm_services_types.h"
31 #include "dc.h"
32 #include "link_enc_cfg.h"
33 #include "dc/inc/core_types.h"
34 #include "dal_asic_id.h"
35 #include "dmub/dmub_srv.h"
36 #include "dc/inc/hw/dmcu.h"
37 #include "dc/inc/hw/abm.h"
38 #include "dc/dc_dmub_srv.h"
39 #include "dc/dc_edid_parser.h"
40 #include "dc/dc_stat.h"
41 #include "dc/dc_state.h"
42 #include "amdgpu_dm_trace.h"
43 #include "link/protocols/link_dpcd.h"
44 #include "link_service_types.h"
45 #include "link/protocols/link_dp_capability.h"
46 #include "link/protocols/link_ddc.h"
47 
48 #include "amdgpu.h"
49 #include "amdgpu_display.h"
50 #include "amdgpu_ucode.h"
51 #include "atom.h"
52 #include "amdgpu_dm.h"
53 #include "amdgpu_dm_plane.h"
54 #include "amdgpu_dm_crtc.h"
55 #include "amdgpu_dm_hdcp.h"
56 #include <drm/display/drm_hdcp_helper.h>
57 #include "amdgpu_dm_wb.h"
58 #include "amdgpu_atombios.h"
59 
60 #include "amd_shared.h"
61 #include "amdgpu_dm_irq.h"
62 #include "dm_helpers.h"
63 #include "amdgpu_dm_mst_types.h"
64 #if defined(CONFIG_DEBUG_FS)
65 #include "amdgpu_dm_debugfs.h"
66 #endif
67 #include "amdgpu_dm_psr.h"
68 #include "amdgpu_dm_replay.h"
69 #include "amdgpu_dm_backlight.h"
70 #include "amdgpu_dm_audio.h"
71 #include "amdgpu_dm_dmub.h"
72 #include "amdgpu_dm_connector.h"
73 
74 #include "ivsrcid/ivsrcid_vislands30.h"
75 
76 #include <linux/backlight.h>
77 #include <linux/module.h>
78 #include <linux/moduleparam.h>
79 #include <linux/types.h>
80 #include <linux/pm_runtime.h>
81 #include <linux/pci.h>
82 #include <linux/power_supply.h>
83 #include <linux/firmware.h>
84 #include <linux/component.h>
85 #include <linux/sort.h>
86 
87 #include <drm/drm_privacy_screen_consumer.h>
88 #include <drm/display/drm_dp_mst_helper.h>
89 #include <drm/display/drm_hdmi_helper.h>
90 #include <drm/drm_atomic.h>
91 #include <drm/drm_atomic_uapi.h>
92 #include <drm/drm_atomic_helper.h>
93 #include <drm/drm_blend.h>
94 #include <drm/drm_fixed.h>
95 #include <drm/drm_fourcc.h>
96 #include <drm/drm_edid.h>
97 #include <drm/drm_eld.h>
98 #include <drm/drm_mode.h>
99 #include <drm/drm_utils.h>
100 #include <drm/drm_vblank.h>
101 #include <drm/drm_colorop.h>
102 #include <drm/drm_gem_atomic_helper.h>
103 
104 #include <media/cec-notifier.h>
105 #include <acpi/video.h>
106 
107 #include "ivsrcid/dcn/irqsrcs_dcn_1_0.h"
108 
109 #include "modules/inc/mod_freesync.h"
110 #include "modules/inc/mod_power.h"
111 #include "modules/power/power_helpers.h"
112 
113 MODULE_FIRMWARE(FIRMWARE_RAVEN_DMCU);
114 MODULE_FIRMWARE(FIRMWARE_NAVI12_DMCU);
115 
116 /**
117  * DOC: overview
118  *
119  * The AMDgpu display manager, **amdgpu_dm** (or even simpler,
120  * **dm**) sits between DRM and DC. It acts as a liaison, converting DRM
121  * requests into DC requests, and DC responses into DRM responses.
122  *
123  * The root control structure is &struct amdgpu_display_manager.
124  */
125 
126 /* basic init/fini API */
127 static int amdgpu_dm_init(struct amdgpu_device *adev);
128 static void amdgpu_dm_fini(struct amdgpu_device *adev);
129 STATIC_IFN_KUNIT void reset_freesync_config_for_crtc(struct dm_crtc_state *new_crtc_state);
130 
131 /*
132  * initializes drm_device display related structures, based on the information
133  * provided by DAL. The drm strcutures are: drm_crtc, drm_connector,
134  * drm_encoder, drm_mode_config
135  *
136  * Returns 0 on success
137  */
138 static int amdgpu_dm_initialize_drm_device(struct amdgpu_device *adev);
139 /* removes and deallocates the drm structures, created by the above function */
140 static void amdgpu_dm_destroy_drm_device(struct amdgpu_display_manager *dm);
141 
142 static int amdgpu_dm_atomic_setup_commit(struct drm_atomic_commit *state);
143 static void amdgpu_dm_atomic_commit_tail(struct drm_atomic_commit *state);
144 STATIC_IFN_KUNIT void dm_enable_per_frame_crtc_master_sync(struct dc_state *context);
145 
146 static int amdgpu_dm_atomic_check(struct drm_device *dev,
147 				  struct drm_atomic_commit *state);
148 
149 STATIC_IFN_KUNIT bool
150 is_timing_unchanged_for_freesync(struct drm_crtc_state *old_crtc_state,
151 				 struct drm_crtc_state *new_crtc_state);
152 
153 static inline void amdgpu_dm_exit_ips_for_hw_access(struct dc *dc)
154 {
155 	if (dc->ctx->dmub_srv && !dc->ctx->dmub_srv->idle_exit_counter)
156 		dc_exit_ips_for_hw_access(dc);
157 }
158 
159 /*
160  * dm_vblank_get_counter
161  *
162  * @brief
163  * Get counter for number of vertical blanks
164  *
165  * @param
166  * struct amdgpu_device *adev - [in] desired amdgpu device
167  * int disp_idx - [in] which CRTC to get the counter from
168  *
169  * @return
170  * Counter for vertical blanks
171  */
172 STATIC_IFN_KUNIT u32 dm_vblank_get_counter(struct amdgpu_device *adev, int crtc)
173 {
174 	struct amdgpu_crtc *acrtc = NULL;
175 
176 	if (crtc >= adev->mode_info.num_crtc)
177 		return 0;
178 
179 	acrtc = adev->mode_info.crtcs[crtc];
180 
181 	if (!acrtc->dm_irq_params.stream) {
182 		drm_err(adev_to_drm(adev), "dc_stream_state is NULL for crtc '%d'!\n",
183 			  crtc);
184 		return 0;
185 	}
186 
187 	return dc_stream_get_vblank_counter(acrtc->dm_irq_params.stream);
188 }
189 EXPORT_IF_KUNIT(dm_vblank_get_counter);
190 
191 STATIC_IFN_KUNIT int dm_crtc_get_scanoutpos(struct amdgpu_device *adev, int crtc,
192 					    u32 *vbl, u32 *position)
193 {
194 	u32 v_blank_start = 0, v_blank_end = 0, h_position = 0, v_position = 0;
195 	struct amdgpu_crtc *acrtc = NULL;
196 	struct dc *dc = adev->dm.dc;
197 
198 	if ((crtc < 0) || (crtc >= adev->mode_info.num_crtc))
199 		return -EINVAL;
200 
201 	acrtc = adev->mode_info.crtcs[crtc];
202 
203 	if (!acrtc->dm_irq_params.stream) {
204 		drm_err(adev_to_drm(adev), "dc_stream_state is NULL for crtc '%d'!\n",
205 			  crtc);
206 		return 0;
207 	}
208 
209 	if (dc && dc->caps.ips_support && dc->idle_optimizations_allowed)
210 		dc_allow_idle_optimizations(dc, false);
211 
212 	/*
213 	 * TODO rework base driver to use values directly.
214 	 * for now parse it back into reg-format
215 	 */
216 	dc_stream_get_scanoutpos(acrtc->dm_irq_params.stream,
217 				 &v_blank_start,
218 				 &v_blank_end,
219 				 &h_position,
220 				 &v_position);
221 
222 	*position = v_position | (h_position << 16);
223 	*vbl = v_blank_start | (v_blank_end << 16);
224 
225 	return 0;
226 }
227 EXPORT_IF_KUNIT(dm_crtc_get_scanoutpos);
228 
229 STATIC_IFN_KUNIT bool dm_is_idle(struct amdgpu_ip_block *ip_block)
230 {
231 	/* XXX todo */
232 	return true;
233 }
234 EXPORT_IF_KUNIT(dm_is_idle);
235 
236 STATIC_IFN_KUNIT int dm_wait_for_idle(struct amdgpu_ip_block *ip_block)
237 {
238 	/* XXX todo */
239 	return 0;
240 }
241 EXPORT_IF_KUNIT(dm_wait_for_idle);
242 
243 STATIC_IFN_KUNIT int dm_soft_reset(struct amdgpu_ip_block *ip_block)
244 {
245 	/* XXX todo */
246 	return 0;
247 }
248 EXPORT_IF_KUNIT(dm_soft_reset);
249 
250 STATIC_IFN_KUNIT bool is_dc_timing_adjust_needed(struct dm_crtc_state *old_state,
251 						 struct dm_crtc_state *new_state)
252 {
253 	if (new_state->stream->adjust.timing_adjust_pending)
254 		return true;
255 	if (new_state->freesync_config.state ==  VRR_STATE_ACTIVE_FIXED)
256 		return true;
257 	else if (amdgpu_dm_crtc_vrr_active(old_state) != amdgpu_dm_crtc_vrr_active(new_state))
258 		return true;
259 	else
260 		return false;
261 }
262 EXPORT_IF_KUNIT(is_dc_timing_adjust_needed);
263 
264 /*
265  * DC will program planes with their z-order determined by their ordering
266  * in the dc_surface_updates array. This comparator is used to sort them
267  * by descending zpos.
268  */
269 STATIC_IFN_KUNIT int dm_plane_layer_index_cmp(const void *a, const void *b)
270 {
271 	const struct dc_surface_update *sa = (struct dc_surface_update *)a;
272 	const struct dc_surface_update *sb = (struct dc_surface_update *)b;
273 
274 	/* Sort by descending dc_plane layer_index (i.e. normalized_zpos) */
275 	return sb->surface->layer_index - sa->surface->layer_index;
276 }
277 EXPORT_IF_KUNIT(dm_plane_layer_index_cmp);
278 
279 /**
280  * update_planes_and_stream_adapter() - Send planes to be updated in DC
281  *
282  * DC has a generic way to update planes and stream via
283  * dc_update_planes_and_stream function; however, DM might need some
284  * adjustments and preparation before calling it. This function is a wrapper
285  * for the dc_update_planes_and_stream that does any required configuration
286  * before passing control to DC.
287  *
288  * @dc: Display Core control structure
289  * @update_type: specify whether it is FULL/MEDIUM/FAST update
290  * @planes_count: planes count to update
291  * @stream: stream state
292  * @stream_update: stream update
293  * @array_of_surface_update: dc surface update pointer
294  *
295  */
296 static inline bool update_planes_and_stream_adapter(struct dc *dc,
297 						    int update_type,
298 						    int planes_count,
299 						    struct dc_stream_state *stream,
300 						    struct dc_stream_update *stream_update,
301 						    struct dc_surface_update *array_of_surface_update)
302 {
303 	sort(array_of_surface_update, planes_count,
304 	     sizeof(*array_of_surface_update), dm_plane_layer_index_cmp, NULL);
305 
306 	/*
307 	 * Previous frame finished and HW is ready for optimization.
308 	 */
309 	dc_post_update_surfaces_to_stream(dc);
310 
311 	return dc_update_planes_and_stream(dc,
312 					   array_of_surface_update,
313 					   planes_count,
314 					   stream,
315 					   stream_update);
316 }
317 
318 STATIC_IFN_KUNIT int dm_set_clockgating_state(struct amdgpu_ip_block *ip_block,
319 					      enum amd_clockgating_state state)
320 {
321 	return 0;
322 }
323 EXPORT_IF_KUNIT(dm_set_clockgating_state);
324 
325 STATIC_IFN_KUNIT int dm_set_powergating_state(struct amdgpu_ip_block *ip_block,
326 					      enum amd_powergating_state state)
327 {
328 	return 0;
329 }
330 EXPORT_IF_KUNIT(dm_set_powergating_state);
331 
332 /* Prototypes of private functions */
333 static int dm_early_init(struct amdgpu_ip_block *ip_block);
334 
335 /* Allocate memory for FBC compressed data  */
336 static void mmhub_read_system_context(struct amdgpu_device *adev, struct dc_phy_addr_space_config *pa_config)
337 {
338 	u64 pt_base;
339 	u32 logical_addr_low;
340 	u32 logical_addr_high;
341 	u32 agp_base, agp_bot, agp_top;
342 	PHYSICAL_ADDRESS_LOC page_table_start, page_table_end, page_table_base;
343 
344 	memset(pa_config, 0, sizeof(*pa_config));
345 
346 	agp_base = 0;
347 	agp_bot = adev->gmc.agp_start >> 24;
348 	agp_top = adev->gmc.agp_end >> 24;
349 
350 	/* AGP aperture is disabled */
351 	if (agp_bot > agp_top) {
352 		logical_addr_low = adev->gmc.fb_start >> 18;
353 		if (adev->apu_flags & (AMD_APU_IS_RAVEN2 |
354 				       AMD_APU_IS_RENOIR |
355 				       AMD_APU_IS_GREEN_SARDINE))
356 			/*
357 			 * Raven2 has a HW issue that it is unable to use the vram which
358 			 * is out of MC_VM_SYSTEM_APERTURE_HIGH_ADDR. So here is the
359 			 * workaround that increase system aperture high address (add 1)
360 			 * to get rid of the VM fault and hardware hang.
361 			 */
362 			logical_addr_high = (adev->gmc.fb_end >> 18) + 0x1;
363 		else
364 			logical_addr_high = adev->gmc.fb_end >> 18;
365 	} else {
366 		logical_addr_low = min(adev->gmc.fb_start, adev->gmc.agp_start) >> 18;
367 		if (adev->apu_flags & (AMD_APU_IS_RAVEN2 |
368 				       AMD_APU_IS_RENOIR |
369 				       AMD_APU_IS_GREEN_SARDINE))
370 			/*
371 			 * Raven2 has a HW issue that it is unable to use the vram which
372 			 * is out of MC_VM_SYSTEM_APERTURE_HIGH_ADDR. So here is the
373 			 * workaround that increase system aperture high address (add 1)
374 			 * to get rid of the VM fault and hardware hang.
375 			 */
376 			logical_addr_high = max((adev->gmc.fb_end >> 18) + 0x1, adev->gmc.agp_end >> 18);
377 		else
378 			logical_addr_high = max(adev->gmc.fb_end, adev->gmc.agp_end) >> 18;
379 	}
380 
381 	pt_base = amdgpu_gmc_pd_addr(adev->gart.bo);
382 
383 	page_table_start.high_part = upper_32_bits(adev->gmc.gart_start >>
384 						   AMDGPU_GPU_PAGE_SHIFT);
385 	page_table_start.low_part = lower_32_bits(adev->gmc.gart_start >>
386 						  AMDGPU_GPU_PAGE_SHIFT);
387 	page_table_end.high_part = upper_32_bits(adev->gmc.gart_end >>
388 						 AMDGPU_GPU_PAGE_SHIFT);
389 	page_table_end.low_part = lower_32_bits(adev->gmc.gart_end >>
390 						AMDGPU_GPU_PAGE_SHIFT);
391 	page_table_base.high_part = upper_32_bits(pt_base);
392 	page_table_base.low_part = lower_32_bits(pt_base);
393 
394 	pa_config->system_aperture.start_addr = (uint64_t)logical_addr_low << 18;
395 	pa_config->system_aperture.end_addr = (uint64_t)logical_addr_high << 18;
396 
397 	pa_config->system_aperture.agp_base = (uint64_t)agp_base << 24;
398 	pa_config->system_aperture.agp_bot = (uint64_t)agp_bot << 24;
399 	pa_config->system_aperture.agp_top = (uint64_t)agp_top << 24;
400 
401 	pa_config->system_aperture.fb_base = adev->gmc.fb_start;
402 	pa_config->system_aperture.fb_offset = adev->vm_manager.vram_base_offset;
403 	pa_config->system_aperture.fb_top = adev->gmc.fb_end;
404 
405 	pa_config->gart_config.page_table_start_addr = page_table_start.quad_part << 12;
406 	pa_config->gart_config.page_table_end_addr = page_table_end.quad_part << 12;
407 	pa_config->gart_config.page_table_base_addr = page_table_base.quad_part;
408 
409 	pa_config->is_hvm_enabled = adev->mode_info.gpu_vm_support;
410 
411 }
412 
413 struct amdgpu_stutter_quirk {
414 	u16 chip_vendor;
415 	u16 chip_device;
416 	u16 subsys_vendor;
417 	u16 subsys_device;
418 	u8 revision;
419 };
420 
421 static const struct amdgpu_stutter_quirk amdgpu_stutter_quirk_list[] = {
422 	/* https://bugzilla.kernel.org/show_bug.cgi?id=214417 */
423 	{ 0x1002, 0x15dd, 0x1002, 0x15dd, 0xc8 },
424 	{ 0, 0, 0, 0, 0 },
425 };
426 
427 STATIC_IFN_KUNIT bool dm_should_disable_stutter(struct pci_dev *pdev)
428 {
429 	const struct amdgpu_stutter_quirk *p = amdgpu_stutter_quirk_list;
430 
431 	while (p && p->chip_device != 0) {
432 		if (pdev->vendor == p->chip_vendor &&
433 		    pdev->device == p->chip_device &&
434 		    pdev->subsystem_vendor == p->subsys_vendor &&
435 		    pdev->subsystem_device == p->subsys_device &&
436 		    pdev->revision == p->revision) {
437 			return true;
438 		}
439 		++p;
440 	}
441 	return false;
442 }
443 EXPORT_IF_KUNIT(dm_should_disable_stutter);
444 
445 
446 void*
447 dm_allocate_gpu_mem(
448 		struct amdgpu_device *adev,
449 		enum dc_gpu_mem_alloc_type type,
450 		size_t size,
451 		long long *addr)
452 {
453 	struct dal_allocation *da;
454 	u32 domain = (type == DC_MEM_ALLOC_TYPE_GART) ?
455 		AMDGPU_GEM_DOMAIN_GTT : AMDGPU_GEM_DOMAIN_VRAM;
456 	int ret;
457 
458 	da = kzalloc_obj(struct dal_allocation);
459 	if (!da)
460 		return NULL;
461 
462 	ret = amdgpu_bo_create_kernel(adev, size, PAGE_SIZE,
463 				      domain, &da->bo,
464 				      &da->gpu_addr, &da->cpu_ptr);
465 
466 	*addr = da->gpu_addr;
467 
468 	if (ret) {
469 		kfree(da);
470 		return NULL;
471 	}
472 
473 	/* add da to list in dm */
474 	list_add(&da->list, &adev->dm.da_list);
475 
476 	return da->cpu_ptr;
477 }
478 
479 void
480 dm_free_gpu_mem(
481 		struct amdgpu_device *adev,
482 		enum dc_gpu_mem_alloc_type type,
483 		void *pvMem)
484 {
485 	struct dal_allocation *da;
486 
487 	/* walk the da list in DM */
488 	list_for_each_entry(da, &adev->dm.da_list, list) {
489 		if (pvMem == da->cpu_ptr) {
490 			amdgpu_bo_free_kernel(&da->bo, &da->gpu_addr, &da->cpu_ptr);
491 			list_del(&da->list);
492 			kfree(da);
493 			break;
494 		}
495 	}
496 
497 }
498 
499 static int amdgpu_dm_init_power_module(struct amdgpu_display_manager *dm)
500 {
501 	struct mod_power_init_params init_data[MAX_NUM_EDP];
502 
503 	if (dm->num_of_edps == 0) {
504 		drm_dbg_driver(
505 			dm->ddev,
506 			"amdgpu: No eDP detected, skip initializing power module\n");
507 		return 0;
508 	}
509 
510 	/* Initialize all the power module parameters */
511 	for (int i = 0; i < dm->num_of_edps; i++) {
512 		init_data[i].allow_psr_smu_optimizations =
513 			!!(amdgpu_dc_feature_mask & DC_PSR_ALLOW_SMU_OPT);
514 		init_data[i].allow_psr_multi_disp_optimizations =
515 			!!(amdgpu_dc_feature_mask & DC_PSR_ALLOW_MULTI_DISP_OPT);
516 		/* See dm_late_init */
517 		init_data[i].backlight_ramping_override = false;
518 		init_data[i].backlight_ramping_start = 0xCCCC;
519 		init_data[i].backlight_ramping_reduction = 0xCCCCCCCC;
520 		init_data[i].def_varibright_level = 0;
521 		init_data[i].abm_config_setting = 0;
522 		init_data[i].num_backlight_levels = 101;
523 		init_data[i].use_nits_based_brightness = false;
524 		init_data[i].panel_max_millinits = 0;
525 		init_data[i].panel_min_millinits = 0;
526 		init_data[i].disable_fractional_pwm =
527 			!(amdgpu_dc_feature_mask & DC_DISABLE_FRACTIONAL_PWM_MASK);
528 		init_data[i].use_custom_backlight_caps = false;
529 		init_data[i].custom_backlight_caps_config_no = 0;
530 		init_data[i].use_linear_backlight_curve = false;
531 		init_data[i].def_varibright_enable = 0;
532 		init_data[i].varibright_level = 0;
533 		/*
534 		 * Power module uses 16-bit backlight levels (0xFFFF max) rather
535 		 * than 8-bit(0XFF max)
536 		 */
537 		init_data[i].min_backlight_pwm =
538 			dm->backlight_caps[i].min_input_signal * 0x101;
539 		init_data[i].max_backlight_pwm =
540 			dm->backlight_caps[i].max_input_signal * 0x101;
541 		init_data[i].min_abm_backlight =
542 			dm->backlight_caps[i].min_input_signal * 0x101;
543 
544 		/* Min backlight level after ABM reduction,  Don't allow below 1%
545 		 * 0xFFFF x 0.01 = 0x28F
546 		 */
547 		init_data[i].min_abm_backlight = (init_data[i].min_abm_backlight < 0x28F) ?
548 			0x28F : init_data[i].min_abm_backlight;
549 	}
550 
551 	dm->power_module = mod_power_create(dm->dc, init_data, dm->num_of_edps);
552 	if (!dm->power_module) {
553 		drm_err(dm->ddev, "amdgpu: Error allocating memory for power module\n");
554 		return -ENOMEM;
555 	}
556 
557 	mod_power_hw_init(dm->power_module);
558 	drm_dbg_driver(dm->ddev, "amdgpu: Power module init done\n");
559 
560 	return 0;
561 }
562 
563 static int amdgpu_dm_init(struct amdgpu_device *adev)
564 {
565 	struct dc_init_data init_data;
566 	struct dc_callback_init init_params;
567 	int r;
568 
569 	adev->dm.ddev = adev_to_drm(adev);
570 	adev->dm.adev = adev;
571 
572 	/* Zero all the fields */
573 	memset(&init_data, 0, sizeof(init_data));
574 	memset(&init_params, 0, sizeof(init_params));
575 
576 	mutex_init(&adev->dm.dpia_aux_lock);
577 	mutex_init(&adev->dm.dc_lock);
578 	mutex_init(&adev->dm.audio_lock);
579 
580 	spin_lock_init(&adev->dm.dmub_lock);
581 
582 	if (amdgpu_dm_irq_init(adev)) {
583 		drm_err(adev_to_drm(adev), "failed to initialize DM IRQ support.\n");
584 		goto error;
585 	}
586 
587 	/* special handling for early revisions of GC 11.5.4 */
588 	if (amdgpu_ip_version(adev, GC_HWIP, 0) == IP_VERSION(11, 5, 4))
589 		init_data.asic_id.chip_family = AMDGPU_FAMILY_GC_11_5_4;
590 	else
591 		init_data.asic_id.chip_family = adev->family;
592 
593 	init_data.asic_id.pci_revision_id = adev->pdev->revision;
594 	init_data.asic_id.hw_internal_rev = adev->external_rev_id;
595 	init_data.asic_id.chip_id = adev->pdev->device;
596 
597 	init_data.asic_id.vram_width = adev->gmc.vram_width;
598 	/* TODO: initialize init_data.asic_id.vram_type here!!!! */
599 	init_data.asic_id.atombios_base_address =
600 		adev->mode_info.atom_context->bios;
601 
602 	init_data.driver = adev;
603 
604 	/* cgs_device was created in dm_sw_init() */
605 	init_data.cgs_device = adev->dm.cgs_device;
606 
607 	init_data.dce_environment = DCE_ENV_PRODUCTION_DRV;
608 
609 	switch (amdgpu_ip_version(adev, DCE_HWIP, 0)) {
610 	case IP_VERSION(2, 1, 0):
611 		switch (adev->dm.dmcub_fw_version) {
612 		case 0: /* development */
613 		case 0x1: /* linux-firmware.git hash 6d9f399 */
614 		case 0x01000000: /* linux-firmware.git hash 9a0b0f4 */
615 			init_data.flags.disable_dmcu = false;
616 			break;
617 		default:
618 			init_data.flags.disable_dmcu = true;
619 		}
620 		break;
621 	case IP_VERSION(2, 0, 3):
622 		init_data.flags.disable_dmcu = true;
623 		break;
624 	default:
625 		break;
626 	}
627 
628 	/* APU support S/G display by default except:
629 	 * ASICs before Carrizo,
630 	 * RAVEN1 (Users reported stability issue)
631 	 */
632 
633 	if (adev->asic_type < CHIP_CARRIZO) {
634 		init_data.flags.gpu_vm_support = false;
635 	} else if (adev->asic_type == CHIP_RAVEN) {
636 		if (adev->apu_flags & AMD_APU_IS_RAVEN)
637 			init_data.flags.gpu_vm_support = false;
638 		else
639 			init_data.flags.gpu_vm_support = (amdgpu_sg_display != 0);
640 	} else {
641 		if (amdgpu_ip_version(adev, DCE_HWIP, 0) == IP_VERSION(2, 0, 3))
642 			init_data.flags.gpu_vm_support = (amdgpu_sg_display == 1);
643 		else
644 			init_data.flags.gpu_vm_support =
645 				(amdgpu_sg_display != 0) && (adev->flags & AMD_IS_APU);
646 	}
647 
648 	adev->mode_info.gpu_vm_support = init_data.flags.gpu_vm_support;
649 
650 	if (amdgpu_dc_feature_mask & DC_FBC_MASK)
651 		init_data.flags.fbc_support = true;
652 
653 	if (amdgpu_dc_feature_mask & DC_MULTI_MON_PP_MCLK_SWITCH_MASK)
654 		init_data.flags.multi_mon_pp_mclk_switch = true;
655 
656 	if (amdgpu_dc_feature_mask & DC_DISABLE_FRACTIONAL_PWM_MASK)
657 		init_data.flags.disable_fractional_pwm = true;
658 
659 	if (amdgpu_dc_feature_mask & DC_EDP_NO_POWER_SEQUENCING)
660 		init_data.flags.edp_no_power_sequencing = true;
661 
662 	if (amdgpu_dc_feature_mask & DC_DISABLE_LTTPR_DP1_4A)
663 		init_data.flags.allow_lttpr_non_transparent_mode.bits.DP1_4A = true;
664 	if (amdgpu_dc_feature_mask & DC_DISABLE_LTTPR_DP2_0)
665 		init_data.flags.allow_lttpr_non_transparent_mode.bits.DP2_0 = true;
666 
667 	if (amdgpu_dc_feature_mask & DC_FRL_MASK)
668 		init_data.flags.enable_frl = true;
669 
670 	init_data.flags.seamless_boot_edp_requested = false;
671 
672 	if (amdgpu_device_seamless_boot_supported(adev)) {
673 		init_data.flags.seamless_boot_edp_requested = true;
674 		init_data.flags.allow_seamless_boot_optimization = true;
675 		drm_dbg(adev->dm.ddev, "Seamless boot requested\n");
676 	}
677 
678 	init_data.flags.enable_mipi_converter_optimization = true;
679 
680 	init_data.dcn_reg_offsets = adev->reg_offset[DCE_HWIP][0];
681 	init_data.nbio_reg_offsets = adev->reg_offset[NBIO_HWIP][0];
682 	init_data.clk_reg_offsets = adev->reg_offset[CLK_HWIP][0];
683 
684 	if (amdgpu_dc_debug_mask & DC_DISABLE_IPS)
685 		init_data.flags.disable_ips = DMUB_IPS_DISABLE_ALL;
686 	else if (amdgpu_dc_debug_mask & DC_DISABLE_IPS_DYNAMIC)
687 		init_data.flags.disable_ips = DMUB_IPS_DISABLE_DYNAMIC;
688 	else if (amdgpu_dc_debug_mask & DC_DISABLE_IPS2_DYNAMIC)
689 		init_data.flags.disable_ips = DMUB_IPS_RCG_IN_ACTIVE_IPS2_IN_OFF;
690 	else if (amdgpu_dc_debug_mask & DC_FORCE_IPS_ENABLE)
691 		init_data.flags.disable_ips = DMUB_IPS_ENABLE;
692 	else
693 		init_data.flags.disable_ips = dm_get_default_ips_mode(adev);
694 
695 	init_data.flags.disable_ips_in_vpb = 0;
696 
697 	/* DCN35 and above supports dynamic DTBCLK switch */
698 	if (amdgpu_ip_version(adev, DCE_HWIP, 0) >= IP_VERSION(3, 5, 0))
699 		init_data.flags.allow_0_dtb_clk = true;
700 
701 	/* Enable DWB for tested platforms only */
702 	if (amdgpu_ip_version(adev, DCE_HWIP, 0) >= IP_VERSION(3, 0, 0))
703 		init_data.num_virtual_links = 1;
704 
705 	/* DCN42 and above dpia switch to unified link training path */
706 	if (amdgpu_ip_version(adev, DCE_HWIP, 0) >= IP_VERSION(4, 2, 0)) {
707 		init_data.flags.consolidated_dpia_dp_lt = true;
708 		init_data.flags.enable_dpia_pre_training = true;
709 		init_data.flags.unify_link_enc_assignment = true;
710 		init_data.flags.usb4_bw_alloc_support = true;
711 	}
712 	retrieve_dmi_info(&adev->dm);
713 	if (adev->dm.edp0_on_dp1_quirk)
714 		init_data.flags.support_edp0_on_dp1 = true;
715 
716 	if (adev->dm.bb_from_dmub)
717 		init_data.bb_from_dmub = adev->dm.bb_from_dmub;
718 	else
719 		init_data.bb_from_dmub = NULL;
720 
721 	/* Display Core create. */
722 	adev->dm.dc = dc_create(&init_data);
723 
724 	if (adev->dm.dc) {
725 		drm_info(adev_to_drm(adev), "Display Core v%s initialized on %s\n", DC_VER,
726 			 dce_version_to_string(adev->dm.dc->ctx->dce_version));
727 	} else {
728 		drm_info(adev_to_drm(adev), "Display Core failed to initialize with v%s!\n", DC_VER);
729 		goto error;
730 	}
731 
732 	if (amdgpu_dc_debug_mask & DC_DISABLE_PIPE_SPLIT) {
733 		adev->dm.dc->debug.force_single_disp_pipe_split = false;
734 		adev->dm.dc->debug.pipe_split_policy = MPC_SPLIT_AVOID;
735 	}
736 
737 	if (adev->asic_type != CHIP_CARRIZO && adev->asic_type != CHIP_STONEY)
738 		adev->dm.dc->debug.disable_stutter = amdgpu_pp_feature_mask & PP_STUTTER_MODE ? false : true;
739 	if (dm_should_disable_stutter(adev->pdev))
740 		adev->dm.dc->debug.disable_stutter = true;
741 
742 	if (amdgpu_dc_debug_mask & DC_DISABLE_STUTTER)
743 		adev->dm.dc->debug.disable_stutter = true;
744 
745 	if (amdgpu_dc_debug_mask & DC_DISABLE_DSC)
746 		adev->dm.dc->debug.disable_dsc = true;
747 
748 	if (amdgpu_dc_debug_mask & DC_DISABLE_CLOCK_GATING)
749 		adev->dm.dc->debug.disable_clock_gate = true;
750 
751 	if (amdgpu_dc_debug_mask & DC_FORCE_SUBVP_MCLK_SWITCH)
752 		adev->dm.dc->debug.force_subvp_mclk_switch = true;
753 
754 	if (amdgpu_dc_debug_mask & DC_DISABLE_SUBVP_FAMS) {
755 		adev->dm.dc->debug.force_disable_subvp = true;
756 		adev->dm.dc->debug.fams2_config.bits.enable = false;
757 	}
758 
759 	if (amdgpu_dc_debug_mask & DC_ENABLE_DML2) {
760 		adev->dm.dc->debug.using_dml2 = true;
761 		adev->dm.dc->debug.using_dml21 = true;
762 	}
763 
764 	if (amdgpu_dc_debug_mask & DC_HDCP_LC_FORCE_FW_ENABLE)
765 		adev->dm.dc->debug.hdcp_lc_force_fw_enable = true;
766 
767 	if (amdgpu_dc_debug_mask & DC_HDCP_LC_ENABLE_SW_FALLBACK)
768 		adev->dm.dc->debug.hdcp_lc_enable_sw_fallback = true;
769 
770 	if (amdgpu_dc_debug_mask & DC_SKIP_DETECTION_LT)
771 		adev->dm.dc->debug.skip_detection_link_training = true;
772 
773 	adev->dm.dc->debug.visual_confirm = amdgpu_dc_visual_confirm;
774 
775 	/* TODO: Remove after DP2 receiver gets proper support of Cable ID feature */
776 	adev->dm.dc->debug.ignore_cable_id = true;
777 
778 	if (adev->dm.dc->caps.dp_hdmi21_pcon_support)
779 		drm_info(adev_to_drm(adev), "DP-HDMI FRL PCON supported\n");
780 
781 	r = dm_dmub_hw_init(adev);
782 	if (r) {
783 		drm_err(adev_to_drm(adev), "DMUB interface failed to initialize: status=%d\n", r);
784 		goto error;
785 	}
786 
787 	dc_hardware_init(adev->dm.dc);
788 
789 	/* GOP/vBIOS may leave an OPTC enabled for a display present at power-on
790 	 * but no longer driven (e.g. an external DP unplugged at boot). Such a
791 	 * dangling pipe keeps DCN out of idle and blocks s0i3. Power it down
792 	 * here when nothing needs to be preserved.
793 	 */
794 	if (adev->flags & AMD_IS_APU)
795 		dc_disable_dangling_timing_generators(adev->dm.dc);
796 
797 	adev->dm.hpd_rx_offload_wq = amdgpu_dm_hpd_rx_irq_create_workqueue(adev);
798 	if (!adev->dm.hpd_rx_offload_wq) {
799 		drm_err(adev_to_drm(adev), "failed to create hpd rx offload workqueue.\n");
800 		goto error;
801 	}
802 
803 	if ((adev->flags & AMD_IS_APU) && (adev->asic_type >= CHIP_CARRIZO)) {
804 		struct dc_phy_addr_space_config pa_config;
805 
806 		mmhub_read_system_context(adev, &pa_config);
807 
808 		// Call the DC init_memory func
809 		dc_setup_system_context(adev->dm.dc, &pa_config);
810 	}
811 
812 	adev->dm.freesync_module = mod_freesync_create(adev->dm.dc);
813 	if (!adev->dm.freesync_module) {
814 		drm_err(adev_to_drm(adev),
815 		"failed to initialize freesync_module.\n");
816 	} else
817 		drm_dbg_driver(adev_to_drm(adev), "freesync_module init done %p.\n",
818 				adev->dm.freesync_module);
819 
820 	amdgpu_dm_init_color_mod();
821 
822 	if (adev->dm.dc->caps.max_links > 0) {
823 		adev->dm.vblank_control_workqueue =
824 			create_singlethread_workqueue("dm_vblank_control_workqueue");
825 		if (!adev->dm.vblank_control_workqueue)
826 			drm_err(adev_to_drm(adev), "failed to initialize vblank_workqueue.\n");
827 	}
828 
829 	if (adev->dm.dc->caps.ips_support &&
830 	    adev->dm.dc->config.disable_ips != DMUB_IPS_DISABLE_ALL)
831 		adev->dm.idle_workqueue = idle_create_workqueue(adev);
832 
833 	if (adev->dm.dc->caps.max_links > 0 && adev->family >= AMDGPU_FAMILY_RV) {
834 		adev->dm.hdcp_workqueue = hdcp_create_workqueue(adev, &init_params.cp_psp, adev->dm.dc);
835 
836 		if (!adev->dm.hdcp_workqueue)
837 			drm_err(adev_to_drm(adev), "failed to initialize hdcp_workqueue.\n");
838 		else
839 			drm_dbg_driver(adev_to_drm(adev),
840 				       "hdcp_workqueue init done %p.\n",
841 				       adev->dm.hdcp_workqueue);
842 
843 		dc_init_callbacks(adev->dm.dc, &init_params);
844 	}
845 	if (adev->dm.dc->caps.max_links > 0) {
846 		adev->dm.hdmi_frl_status_polling_wq =
847 			create_singlethread_workqueue("hdmi_frl_status_polling_workqueue");
848 		if (!adev->dm.hdmi_frl_status_polling_wq)
849 			drm_err(adev_to_drm(adev), "failed to initialize hdmi_frl_status_polling_workqueue\n");
850 	}
851 	if (dc_is_dmub_outbox_supported(adev->dm.dc)) {
852 		init_completion(&adev->dm.dmub_aux_transfer_done);
853 		adev->dm.dmub_notify = kzalloc_obj(struct dmub_notification);
854 		if (!adev->dm.dmub_notify) {
855 			drm_info(adev_to_drm(adev), "fail to allocate adev->dm.dmub_notify");
856 			goto error;
857 		}
858 
859 		adev->dm.delayed_hpd_wq = create_singlethread_workqueue("amdgpu_dm_hpd_wq");
860 		if (!adev->dm.delayed_hpd_wq) {
861 			drm_err(adev_to_drm(adev), "failed to create hpd offload workqueue.\n");
862 			goto error;
863 		}
864 
865 		amdgpu_dm_outbox_init(adev);
866 		if (!dm_register_dmub_notify_callback(adev, DMUB_NOTIFICATION_AUX_REPLY,
867 			dm_dmub_aux_setconfig_callback, false)) {
868 			drm_err(adev_to_drm(adev), "fail to register dmub aux callback");
869 			goto error;
870 		}
871 
872 		for (size_t i = 0; i < ARRAY_SIZE(adev->dm.fused_io); i++)
873 			init_completion(&adev->dm.fused_io[i].replied);
874 
875 		if (!dm_register_dmub_notify_callback(adev, DMUB_NOTIFICATION_FUSED_IO,
876 			dm_dmub_aux_fused_io_callback, false)) {
877 			drm_err(adev_to_drm(adev), "fail to register dmub fused io callback");
878 			goto error;
879 		}
880 		/* Enable outbox notification only after IRQ handlers are registered and DMUB is alive.
881 		 * It is expected that DMUB will resend any pending notifications at this point. Note
882 		 * that hpd and hpd_irq handler registration are deferred to
883 		 * amdgpu_dm_register_hpd_handlers() to align legacy interface initialization
884 		 * sequence. Connection status will be proactivly detected once in the
885 		 * amdgpu_dm_initialize_drm_device.
886 		 */
887 		dc_enable_dmub_outbox(adev->dm.dc);
888 
889 		/* DPIA trace goes to dmesg logs only if outbox is enabled */
890 		if (amdgpu_dc_debug_mask & DC_ENABLE_DPIA_TRACE)
891 			dc_dmub_srv_enable_dpia_trace(adev->dm.dc);
892 	}
893 
894 	if (amdgpu_dm_initialize_drm_device(adev)) {
895 		drm_err(adev_to_drm(adev),
896 		"failed to initialize sw for display support.\n");
897 		goto error;
898 	}
899 
900 	if (amdgpu_dm_init_power_module(&adev->dm))
901 		goto error;
902 
903 	/* create fake encoders for MST */
904 	dm_dp_create_fake_mst_encoders(adev);
905 
906 	/* TODO: Add_display_info? */
907 
908 	/* TODO use dynamic cursor width */
909 	adev_to_drm(adev)->mode_config.cursor_width = adev->dm.dc->caps.max_cursor_size;
910 	adev_to_drm(adev)->mode_config.cursor_height = adev->dm.dc->caps.max_cursor_size;
911 
912 	if (drm_vblank_init(adev_to_drm(adev), adev->dm.display_indexes_num)) {
913 		drm_err(adev_to_drm(adev),
914 		"failed to initialize vblank for display support.\n");
915 		goto error;
916 	}
917 
918 #if defined(CONFIG_DRM_AMD_SECURE_DISPLAY)
919 	amdgpu_dm_crtc_secure_display_create_contexts(adev);
920 	if (!adev->dm.secure_display_ctx.crtc_ctx)
921 		drm_err(adev_to_drm(adev), "failed to initialize secure display contexts.\n");
922 
923 	if (amdgpu_ip_version(adev, DCE_HWIP, 0) >= IP_VERSION(4, 0, 1))
924 		adev->dm.secure_display_ctx.support_mul_roi = true;
925 
926 #endif
927 
928 	drm_dbg_driver(adev_to_drm(adev), "KMS initialized.\n");
929 
930 	return 0;
931 error:
932 	amdgpu_dm_fini(adev);
933 
934 	return -EINVAL;
935 }
936 
937 static int amdgpu_dm_early_fini(struct amdgpu_ip_block *ip_block)
938 {
939 	struct amdgpu_device *adev = ip_block->adev;
940 
941 	amdgpu_dm_audio_fini(adev);
942 
943 	return 0;
944 }
945 
946 static void amdgpu_dm_fini(struct amdgpu_device *adev)
947 {
948 	int i;
949 
950 	if (adev->dm.vblank_control_workqueue) {
951 		destroy_workqueue(adev->dm.vblank_control_workqueue);
952 		adev->dm.vblank_control_workqueue = NULL;
953 	}
954 
955 	if (adev->dm.idle_workqueue) {
956 		if (adev->dm.idle_workqueue->running) {
957 			adev->dm.idle_workqueue->enable = false;
958 			flush_work(&adev->dm.idle_workqueue->work);
959 		}
960 
961 		kfree(adev->dm.idle_workqueue);
962 		adev->dm.idle_workqueue = NULL;
963 	}
964 
965 	/*
966 	 * Disable ISM before dc_destroy() invalidates dm->dc.
967 	 *
968 	 * Quiesce workers first without dc_lock (they take dc_lock
969 	 * themselves, so syncing under it would deadlock), then drive the
970 	 * FSM back to FULL_POWER_RUNNING under dc_lock.
971 	 */
972 	amdgpu_dm_ism_disable(&adev->dm);
973 	scoped_guard(mutex, &adev->dm.dc_lock)
974 		amdgpu_dm_ism_force_full_power(&adev->dm);
975 
976 	amdgpu_dm_destroy_drm_device(&adev->dm);
977 
978 #if defined(CONFIG_DRM_AMD_SECURE_DISPLAY)
979 	if (adev->dm.secure_display_ctx.crtc_ctx) {
980 		for (i = 0; i < adev->mode_info.num_crtc; i++) {
981 			if (adev->dm.secure_display_ctx.crtc_ctx[i].crtc) {
982 				flush_work(&adev->dm.secure_display_ctx.crtc_ctx[i].notify_ta_work);
983 				flush_work(&adev->dm.secure_display_ctx.crtc_ctx[i].forward_roi_work);
984 			}
985 		}
986 		kfree(adev->dm.secure_display_ctx.crtc_ctx);
987 		adev->dm.secure_display_ctx.crtc_ctx = NULL;
988 	}
989 #endif
990 	if (adev->dm.hdcp_workqueue) {
991 		hdcp_destroy(&adev->dev->kobj, adev->dm.hdcp_workqueue);
992 		adev->dm.hdcp_workqueue = NULL;
993 	}
994 
995 	if (adev->dm.dc) {
996 		dc_deinit_callbacks(adev->dm.dc);
997 		dc_dmub_srv_destroy(&adev->dm.dc->ctx->dmub_srv);
998 		if (dc_enable_dmub_notifications(adev->dm.dc)) {
999 			kfree(adev->dm.dmub_notify);
1000 			adev->dm.dmub_notify = NULL;
1001 			destroy_workqueue(adev->dm.delayed_hpd_wq);
1002 			adev->dm.delayed_hpd_wq = NULL;
1003 		}
1004 	}
1005 
1006 	if (adev->dm.dmub_bo)
1007 		amdgpu_bo_free_kernel(&adev->dm.dmub_bo,
1008 				      &adev->dm.dmub_bo_gpu_addr,
1009 				      &adev->dm.dmub_bo_cpu_addr);
1010 
1011 	if (adev->dm.boot_time_crc_info.bo_ptr)
1012 		amdgpu_bo_free_kernel(&adev->dm.boot_time_crc_info.bo_ptr,
1013 					&adev->dm.boot_time_crc_info.gpu_addr,
1014 					&adev->dm.boot_time_crc_info.cpu_addr);
1015 
1016 	if (adev->dm.hpd_rx_offload_wq && adev->dm.dc) {
1017 		for (i = 0; i < adev->dm.dc->caps.max_links; i++) {
1018 			if (adev->dm.hpd_rx_offload_wq[i].wq) {
1019 				destroy_workqueue(adev->dm.hpd_rx_offload_wq[i].wq);
1020 				adev->dm.hpd_rx_offload_wq[i].wq = NULL;
1021 			}
1022 		}
1023 
1024 		kfree(adev->dm.hpd_rx_offload_wq);
1025 		adev->dm.hpd_rx_offload_wq = NULL;
1026 	}
1027 
1028 	amdgpu_dm_irq_fini(adev);
1029 
1030 	/* DC Destroy TODO: Replace destroy DAL */
1031 	if (adev->dm.dc)
1032 		dc_destroy(&adev->dm.dc);
1033 
1034 	if (adev->dm.cgs_device) {
1035 		amdgpu_cgs_destroy_device(adev->dm.cgs_device);
1036 		adev->dm.cgs_device = NULL;
1037 	}
1038 	if (adev->dm.freesync_module) {
1039 		mod_freesync_destroy(adev->dm.freesync_module);
1040 		adev->dm.freesync_module = NULL;
1041 	}
1042 
1043 	if (adev->dm.power_module) {
1044 		mod_power_destroy(adev->dm.power_module);
1045 		adev->dm.power_module = NULL;
1046 	}
1047 	mutex_destroy(&adev->dm.audio_lock);
1048 	mutex_destroy(&adev->dm.dc_lock);
1049 	mutex_destroy(&adev->dm.dpia_aux_lock);
1050 }
1051 
1052 static int load_dmcu_fw(struct amdgpu_device *adev)
1053 {
1054 	const char *fw_name_dmcu = NULL;
1055 	int r;
1056 	const struct dmcu_firmware_header_v1_0 *hdr;
1057 
1058 	switch (adev->asic_type) {
1059 #if defined(CONFIG_DRM_AMD_DC_SI)
1060 	case CHIP_TAHITI:
1061 	case CHIP_PITCAIRN:
1062 	case CHIP_VERDE:
1063 	case CHIP_OLAND:
1064 #endif
1065 	case CHIP_BONAIRE:
1066 	case CHIP_HAWAII:
1067 	case CHIP_KAVERI:
1068 	case CHIP_KABINI:
1069 	case CHIP_MULLINS:
1070 	case CHIP_TONGA:
1071 	case CHIP_FIJI:
1072 	case CHIP_CARRIZO:
1073 	case CHIP_STONEY:
1074 	case CHIP_POLARIS11:
1075 	case CHIP_POLARIS10:
1076 	case CHIP_POLARIS12:
1077 	case CHIP_VEGAM:
1078 	case CHIP_VEGA10:
1079 	case CHIP_VEGA12:
1080 	case CHIP_VEGA20:
1081 		return 0;
1082 	case CHIP_NAVI12:
1083 		fw_name_dmcu = FIRMWARE_NAVI12_DMCU;
1084 		break;
1085 	case CHIP_RAVEN:
1086 		if (ASICREV_IS_PICASSO(adev->external_rev_id))
1087 			fw_name_dmcu = FIRMWARE_RAVEN_DMCU;
1088 		else if (ASICREV_IS_RAVEN2(adev->external_rev_id))
1089 			fw_name_dmcu = FIRMWARE_RAVEN_DMCU;
1090 		else
1091 			return 0;
1092 		break;
1093 	default:
1094 		switch (amdgpu_ip_version(adev, DCE_HWIP, 0)) {
1095 		case IP_VERSION(2, 0, 2):
1096 		case IP_VERSION(2, 0, 3):
1097 		case IP_VERSION(2, 0, 0):
1098 		case IP_VERSION(2, 1, 0):
1099 		case IP_VERSION(3, 0, 0):
1100 		case IP_VERSION(3, 0, 2):
1101 		case IP_VERSION(3, 0, 3):
1102 		case IP_VERSION(3, 0, 1):
1103 		case IP_VERSION(3, 1, 2):
1104 		case IP_VERSION(3, 1, 3):
1105 		case IP_VERSION(3, 1, 4):
1106 		case IP_VERSION(3, 1, 5):
1107 		case IP_VERSION(3, 1, 6):
1108 		case IP_VERSION(3, 2, 0):
1109 		case IP_VERSION(3, 2, 1):
1110 		case IP_VERSION(3, 5, 0):
1111 		case IP_VERSION(3, 5, 1):
1112 		case IP_VERSION(3, 6, 0):
1113 		case IP_VERSION(4, 0, 1):
1114 		case IP_VERSION(4, 2, 0):
1115 		case IP_VERSION(4, 2, 1):
1116 			return 0;
1117 		default:
1118 			break;
1119 		}
1120 		drm_err(adev_to_drm(adev), "Unsupported ASIC type: 0x%X\n", adev->asic_type);
1121 		return -EINVAL;
1122 	}
1123 
1124 	if (adev->firmware.load_type != AMDGPU_FW_LOAD_PSP) {
1125 		drm_dbg_kms(adev_to_drm(adev), "dm: DMCU firmware not supported on direct or SMU loading\n");
1126 		return 0;
1127 	}
1128 
1129 	r = amdgpu_ucode_request(adev, &adev->dm.fw_dmcu, AMDGPU_UCODE_REQUIRED,
1130 				 "%s", fw_name_dmcu);
1131 	if (r == -ENODEV) {
1132 		/* DMCU firmware is not necessary, so don't raise a fuss if it's missing */
1133 		drm_dbg_kms(adev_to_drm(adev), "dm: DMCU firmware not found\n");
1134 		adev->dm.fw_dmcu = NULL;
1135 		return 0;
1136 	}
1137 	if (r) {
1138 		drm_err(adev_to_drm(adev), "amdgpu_dm: Can't validate firmware \"%s\"\n",
1139 			fw_name_dmcu);
1140 		amdgpu_ucode_release(&adev->dm.fw_dmcu);
1141 		return r;
1142 	}
1143 
1144 	hdr = (const struct dmcu_firmware_header_v1_0 *)adev->dm.fw_dmcu->data;
1145 	adev->firmware.ucode[AMDGPU_UCODE_ID_DMCU_ERAM].ucode_id = AMDGPU_UCODE_ID_DMCU_ERAM;
1146 	adev->firmware.ucode[AMDGPU_UCODE_ID_DMCU_ERAM].fw = adev->dm.fw_dmcu;
1147 	adev->firmware.fw_size +=
1148 		ALIGN(le32_to_cpu(hdr->header.ucode_size_bytes) - le32_to_cpu(hdr->intv_size_bytes), PAGE_SIZE);
1149 
1150 	adev->firmware.ucode[AMDGPU_UCODE_ID_DMCU_INTV].ucode_id = AMDGPU_UCODE_ID_DMCU_INTV;
1151 	adev->firmware.ucode[AMDGPU_UCODE_ID_DMCU_INTV].fw = adev->dm.fw_dmcu;
1152 	adev->firmware.fw_size +=
1153 		ALIGN(le32_to_cpu(hdr->intv_size_bytes), PAGE_SIZE);
1154 
1155 	adev->dm.dmcu_fw_version = le32_to_cpu(hdr->header.ucode_version);
1156 
1157 	drm_dbg_kms(adev_to_drm(adev), "PSP loading DMCU firmware\n");
1158 
1159 	return 0;
1160 }
1161 
1162 static int dm_sw_init(struct amdgpu_ip_block *ip_block)
1163 {
1164 	struct amdgpu_device *adev = ip_block->adev;
1165 	int r;
1166 
1167 	adev->dm.cgs_device = amdgpu_cgs_create_device(adev);
1168 
1169 	if (!adev->dm.cgs_device) {
1170 		drm_err(adev_to_drm(adev), "failed to create cgs device.\n");
1171 		return -EINVAL;
1172 	}
1173 
1174 	/* Moved from dm init since we need to use allocations for storing bounding box data */
1175 	INIT_LIST_HEAD(&adev->dm.da_list);
1176 
1177 	r = dm_dmub_sw_init(adev);
1178 	if (r)
1179 		return r;
1180 
1181 	return load_dmcu_fw(adev);
1182 }
1183 
1184 static int dm_sw_fini(struct amdgpu_ip_block *ip_block)
1185 {
1186 	struct amdgpu_device *adev = ip_block->adev;
1187 	struct dal_allocation *da;
1188 
1189 	list_for_each_entry(da, &adev->dm.da_list, list) {
1190 		if (adev->dm.bb_from_dmub == (void *) da->cpu_ptr) {
1191 			amdgpu_bo_free_kernel(&da->bo, &da->gpu_addr, &da->cpu_ptr);
1192 			list_del(&da->list);
1193 			kfree(da);
1194 			adev->dm.bb_from_dmub = NULL;
1195 			break;
1196 		}
1197 	}
1198 
1199 
1200 	kfree(adev->dm.dmub_fb_info);
1201 	adev->dm.dmub_fb_info = NULL;
1202 
1203 	if (adev->dm.dmub_srv) {
1204 		dmub_srv_destroy(adev->dm.dmub_srv);
1205 		kfree(adev->dm.dmub_srv);
1206 		adev->dm.dmub_srv = NULL;
1207 	}
1208 
1209 	amdgpu_ucode_release(&adev->dm.dmub_fw);
1210 	amdgpu_ucode_release(&adev->dm.fw_dmcu);
1211 
1212 	return 0;
1213 }
1214 
1215 
1216 static void amdgpu_dm_boot_time_crc_init(struct amdgpu_device *adev)
1217 {
1218 	struct dm_boot_time_crc_info *bootcrc_info = NULL;
1219 	struct dmub_srv *dmub = NULL;
1220 	union dmub_fw_boot_options option = {0};
1221 	int ret = 0;
1222 	const uint32_t fb_size = 3 * 1024 * 1024;	/* 3MB for DCC pattern */
1223 
1224 	if (!adev || !adev->dm.dc || !adev->dm.dc->ctx ||
1225 		!adev->dm.dc->ctx->dmub_srv) {
1226 		return;
1227 	}
1228 
1229 	dmub = adev->dm.dc->ctx->dmub_srv->dmub;
1230 	bootcrc_info = &adev->dm.boot_time_crc_info;
1231 
1232 	if (!dmub || !dmub->hw_funcs.get_fw_boot_option) {
1233 		drm_dbg(adev_to_drm(adev), "failed to init boot time crc buffer\n");
1234 		return;
1235 	}
1236 
1237 	option = dmub->hw_funcs.get_fw_boot_option(dmub);
1238 
1239 	/* Return if boot time CRC is not enabled */
1240 	if (option.bits.bootcrc_en_at_S0i3 == 0)
1241 		return;
1242 
1243 	/* Create a buffer for boot time CRC */
1244 	ret = amdgpu_bo_create_kernel(adev, fb_size, PAGE_SIZE,
1245 		AMDGPU_GEM_DOMAIN_VRAM | AMDGPU_GEM_DOMAIN_GTT,
1246 		&bootcrc_info->bo_ptr,
1247 		&bootcrc_info->gpu_addr,
1248 		&bootcrc_info->cpu_addr);
1249 
1250 	if (ret) {
1251 		drm_dbg(adev_to_drm(adev), "failed to create boot time crc buffer\n");
1252 	} else {
1253 		bootcrc_info->size = fb_size;
1254 
1255 		drm_dbg(adev_to_drm(adev), "boot time crc buffer created addr 0x%llx, size %u\n",
1256 			bootcrc_info->gpu_addr, bootcrc_info->size);
1257 
1258 		/* Send the buffer info to DMUB */
1259 		dc_dmub_srv_boot_time_crc_init(adev->dm.dc,
1260 			bootcrc_info->gpu_addr, bootcrc_info->size);
1261 	}
1262 }
1263 
1264 static int dm_late_init(struct amdgpu_ip_block *ip_block)
1265 {
1266 	struct amdgpu_device *adev = ip_block->adev;
1267 
1268 	struct dmcu_iram_parameters params;
1269 	unsigned int linear_lut[16];
1270 	int i;
1271 	struct dmcu *dmcu = NULL;
1272 
1273 	dmcu = adev->dm.dc->res_pool->dmcu;
1274 
1275 	/* Init the boot time CRC (skip in resume) */
1276 	if ((adev->in_suspend == 0) &&
1277 		(amdgpu_ip_version(adev, DCE_HWIP, 0) == IP_VERSION(3, 6, 0)))
1278 		amdgpu_dm_boot_time_crc_init(adev);
1279 
1280 	for (i = 0; i < 16; i++)
1281 		linear_lut[i] = 0xFFFF * i / 15;
1282 
1283 	params.set = 0;
1284 	params.backlight_ramping_override = false;
1285 	params.backlight_ramping_start = 0xCCCC;
1286 	params.backlight_ramping_reduction = 0xCCCCCCCC;
1287 	params.backlight_lut_array_size = 16;
1288 	params.backlight_lut_array = linear_lut;
1289 
1290 	/* Min backlight level after ABM reduction,  Don't allow below 1%
1291 	 * 0xFFFF x 0.01 = 0x28F
1292 	 */
1293 	params.min_abm_backlight = 0x28F;
1294 	/* In the case where abm is implemented on dmcub,
1295 	 * dmcu object will be null.
1296 	 * ABM 2.4 and up are implemented on dmcub.
1297 	 */
1298 	if (dmcu) {
1299 		if (!dmcu_load_iram(dmcu, params))
1300 			return -EINVAL;
1301 	} else if (adev->dm.dc->ctx->dmub_srv) {
1302 		struct dc_link *edp_links[MAX_NUM_EDP];
1303 		int edp_num;
1304 
1305 		dc_get_edp_links(adev->dm.dc, edp_links, &edp_num);
1306 		for (i = 0; i < edp_num; i++) {
1307 			if (!dmub_init_abm_config(adev->dm.dc->res_pool, params, i))
1308 				return -EINVAL;
1309 		}
1310 	}
1311 
1312 	return amdgpu_dm_detect_mst_link_for_all_connectors(adev_to_drm(adev));
1313 }
1314 
1315 static void resume_mst_branch_status(struct drm_dp_mst_topology_mgr *mgr)
1316 {
1317 	u8 buf[UUID_SIZE];
1318 	guid_t guid;
1319 	int ret;
1320 
1321 	mutex_lock(&mgr->lock);
1322 	if (!mgr->mst_primary)
1323 		goto out_fail;
1324 
1325 	if (drm_dp_read_dpcd_caps(mgr->aux, mgr->dpcd) < 0) {
1326 		drm_dbg_kms(mgr->dev, "dpcd read failed - undocked during suspend?\n");
1327 		goto out_fail;
1328 	}
1329 
1330 	ret = drm_dp_dpcd_writeb(mgr->aux, DP_MSTM_CTRL,
1331 				 DP_MST_EN |
1332 				 DP_UP_REQ_EN |
1333 				 DP_UPSTREAM_IS_SRC);
1334 	if (ret < 0) {
1335 		drm_dbg_kms(mgr->dev, "mst write failed - undocked during suspend?\n");
1336 		goto out_fail;
1337 	}
1338 
1339 	/* Some hubs forget their guids after they resume */
1340 	ret = drm_dp_dpcd_read(mgr->aux, DP_GUID, buf, sizeof(buf));
1341 	if (ret != sizeof(buf)) {
1342 		drm_dbg_kms(mgr->dev, "dpcd read failed - undocked during suspend?\n");
1343 		goto out_fail;
1344 	}
1345 
1346 	import_guid(&guid, buf);
1347 
1348 	if (guid_is_null(&guid)) {
1349 		guid_gen(&guid);
1350 		export_guid(buf, &guid);
1351 
1352 		ret = drm_dp_dpcd_write(mgr->aux, DP_GUID, buf, sizeof(buf));
1353 
1354 		if (ret != sizeof(buf)) {
1355 			drm_dbg_kms(mgr->dev, "check mstb guid failed - undocked during suspend?\n");
1356 			goto out_fail;
1357 		}
1358 	}
1359 
1360 	guid_copy(&mgr->mst_primary->guid, &guid);
1361 
1362 out_fail:
1363 	mutex_unlock(&mgr->lock);
1364 }
1365 
1366 static void s3_handle_mst(struct drm_device *dev, bool suspend)
1367 {
1368 	struct amdgpu_dm_connector *aconnector;
1369 	struct drm_connector *connector;
1370 	struct drm_connector_list_iter iter;
1371 	struct drm_dp_mst_topology_mgr *mgr;
1372 
1373 	drm_connector_list_iter_begin(dev, &iter);
1374 	drm_for_each_connector_iter(connector, &iter) {
1375 
1376 		if (connector->connector_type == DRM_MODE_CONNECTOR_WRITEBACK)
1377 			continue;
1378 
1379 		aconnector = to_amdgpu_dm_connector(connector);
1380 		if (aconnector->dc_link->type != dc_connection_mst_branch ||
1381 		    aconnector->mst_root)
1382 			continue;
1383 
1384 		mgr = &aconnector->mst_mgr;
1385 
1386 		if (suspend) {
1387 			drm_dp_mst_topology_mgr_suspend(mgr);
1388 		} else {
1389 			/* if extended timeout is supported in hardware,
1390 			 * default to LTTPR timeout (3.2ms) first as a W/A for DP link layer
1391 			 * CTS 4.2.1.1 regression introduced by CTS specs requirement update.
1392 			 */
1393 			try_to_configure_aux_timeout(aconnector->dc_link->ddc, LINK_AUX_DEFAULT_LTTPR_TIMEOUT_PERIOD);
1394 			if (!dp_is_lttpr_present(aconnector->dc_link))
1395 				try_to_configure_aux_timeout(aconnector->dc_link->ddc, LINK_AUX_DEFAULT_TIMEOUT_PERIOD);
1396 
1397 			/* TODO: move resume_mst_branch_status() into drm mst resume again
1398 			 * once topology probing work is pulled out from mst resume into mst
1399 			 * resume 2nd step. mst resume 2nd step should be called after old
1400 			 * state getting restored (i.e. drm_atomic_helper_resume()).
1401 			 */
1402 			resume_mst_branch_status(mgr);
1403 		}
1404 	}
1405 	drm_connector_list_iter_end(&iter);
1406 }
1407 
1408 STATIC_IFN_KUNIT int amdgpu_dm_smu_write_watermarks_table(struct amdgpu_device *adev)
1409 {
1410 	int ret = 0;
1411 
1412 	/* This interface is for dGPU Navi1x.Linux dc-pplib interface depends
1413 	 * on window driver dc implementation.
1414 	 * For Navi1x, clock settings of dcn watermarks are fixed. the settings
1415 	 * should be passed to smu during boot up and resume from s3.
1416 	 * boot up: dc calculate dcn watermark clock settings within dc_create,
1417 	 * dcn20_resource_construct
1418 	 * then call pplib functions below to pass the settings to smu:
1419 	 * smu_set_watermarks_for_clock_ranges
1420 	 * smu_set_watermarks_table
1421 	 * navi10_set_watermarks_table
1422 	 * smu_write_watermarks_table
1423 	 *
1424 	 * For Renoir, clock settings of dcn watermark are also fixed values.
1425 	 * dc has implemented different flow for window driver:
1426 	 * dc_hardware_init / dc_set_power_state
1427 	 * dcn10_init_hw
1428 	 * notify_wm_ranges
1429 	 * set_wm_ranges
1430 	 * -- Linux
1431 	 * smu_set_watermarks_for_clock_ranges
1432 	 * renoir_set_watermarks_table
1433 	 * smu_write_watermarks_table
1434 	 *
1435 	 * For Linux,
1436 	 * dc_hardware_init -> amdgpu_dm_init
1437 	 * dc_set_power_state --> dm_resume
1438 	 *
1439 	 * therefore, this function apply to navi10/12/14 but not Renoir
1440 	 * *
1441 	 */
1442 	switch (amdgpu_ip_version(adev, DCE_HWIP, 0)) {
1443 	case IP_VERSION(2, 0, 2):
1444 	case IP_VERSION(2, 0, 0):
1445 		break;
1446 	default:
1447 		return 0;
1448 	}
1449 
1450 	ret = amdgpu_dpm_write_watermarks_table(adev);
1451 	if (ret) {
1452 		drm_err(adev_to_drm(adev), "Failed to update WMTABLE!\n");
1453 		return ret;
1454 	}
1455 
1456 	return 0;
1457 }
1458 EXPORT_IF_KUNIT(amdgpu_dm_smu_write_watermarks_table);
1459 
1460 static int dm_oem_i2c_hw_init(struct amdgpu_device *adev)
1461 {
1462 	struct amdgpu_display_manager *dm = &adev->dm;
1463 	struct amdgpu_i2c_adapter *oem_i2c;
1464 	struct ddc_service *oem_ddc_service;
1465 	int r;
1466 
1467 	oem_ddc_service = dc_get_oem_i2c_device(adev->dm.dc);
1468 	if (oem_ddc_service) {
1469 		oem_i2c = amdgpu_dm_create_i2c(oem_ddc_service, true);
1470 		if (!oem_i2c) {
1471 			drm_info(adev_to_drm(adev), "Failed to create oem i2c adapter data\n");
1472 			return -ENOMEM;
1473 		}
1474 
1475 		r = devm_i2c_add_adapter(adev->dev, &oem_i2c->base);
1476 		if (r) {
1477 			drm_info(adev_to_drm(adev), "Failed to register oem i2c\n");
1478 			kfree(oem_i2c);
1479 			return r;
1480 		}
1481 		dm->oem_i2c = oem_i2c;
1482 	}
1483 
1484 	return 0;
1485 }
1486 
1487 /**
1488  * dm_hw_init() - Initialize DC device
1489  * @ip_block: Pointer to the amdgpu_ip_block for this hw instance.
1490  *
1491  * Initialize the &struct amdgpu_display_manager device. This involves calling
1492  * the initializers of each DM component, then populating the struct with them.
1493  *
1494  * Although the function implies hardware initialization, both hardware and
1495  * software are initialized here. Splitting them out to their relevant init
1496  * hooks is a future TODO item.
1497  *
1498  * Some notable things that are initialized here:
1499  *
1500  * - Display Core, both software and hardware
1501  * - DC modules that we need (freesync and color management)
1502  * - DRM software states
1503  * - Interrupt sources and handlers
1504  * - Vblank support
1505  * - Debug FS entries, if enabled
1506  */
1507 static int dm_hw_init(struct amdgpu_ip_block *ip_block)
1508 {
1509 	struct amdgpu_device *adev = ip_block->adev;
1510 	int r;
1511 
1512 	adev->dm.i2c_devres_group = devres_open_group(adev->dev, NULL, GFP_KERNEL);
1513 	if (!adev->dm.i2c_devres_group)
1514 		return -ENOMEM;
1515 
1516 	/* Create DAL display manager */
1517 	r = amdgpu_dm_init(adev);
1518 	if (r)
1519 		goto err_release_i2c;
1520 	amdgpu_dm_hpd_init(adev);
1521 
1522 	r = dm_oem_i2c_hw_init(adev);
1523 	if (r)
1524 		drm_info(adev_to_drm(adev), "Failed to add OEM i2c bus\n");
1525 
1526 	devres_close_group(adev->dev, adev->dm.i2c_devres_group);
1527 	return 0;
1528 
1529 err_release_i2c:
1530 	devres_release_group(adev->dev, adev->dm.i2c_devres_group);
1531 	return r;
1532 }
1533 
1534 /**
1535  * dm_hw_fini() - Teardown DC device
1536  * @ip_block: Pointer to the amdgpu_ip_block for this hw instance.
1537  *
1538  * Teardown components within &struct amdgpu_display_manager that require
1539  * cleanup. This involves cleaning up the DRM device, DC, and any modules that
1540  * were loaded. Also flush IRQ workqueues and disable them.
1541  */
1542 static int dm_hw_fini(struct amdgpu_ip_block *ip_block)
1543 {
1544 	struct amdgpu_device *adev = ip_block->adev;
1545 
1546 	if (adev->dm.i2c_devres_group)
1547 		devres_release_group(adev->dev, adev->dm.i2c_devres_group);
1548 
1549 	amdgpu_dm_hpd_fini(adev);
1550 
1551 	amdgpu_dm_fini(adev);
1552 	return 0;
1553 }
1554 
1555 
1556 static void dm_gpureset_toggle_interrupts(struct amdgpu_device *adev,
1557 				 struct dc_state *state, bool enable)
1558 {
1559 	enum dc_irq_source irq_source;
1560 	struct amdgpu_crtc *acrtc;
1561 	int rc = -EBUSY;
1562 	int i = 0;
1563 
1564 	for (i = 0; i < state->stream_count; i++) {
1565 		acrtc = amdgpu_dm_get_crtc_by_otg_inst(
1566 				adev, state->stream_status[i].primary_otg_inst);
1567 
1568 		if (acrtc && state->stream_status[i].plane_count != 0 &&
1569 		    amdgpu_ip_version(adev, DCE_HWIP, 0) == 0) {
1570 			irq_source = IRQ_TYPE_PFLIP + acrtc->otg_inst;
1571 			rc = dc_interrupt_set(adev->dm.dc, irq_source, enable) ? 0 : -EBUSY;
1572 			if (rc)
1573 				drm_warn(adev_to_drm(adev), "Failed to %s pflip interrupts\n",
1574 					 enable ? "enable" : "disable");
1575 
1576 			if (dc_supports_vrr(adev->dm.dc->ctx->dce_version)) {
1577 				if (enable) {
1578 					if (amdgpu_dm_crtc_vrr_active(
1579 							to_dm_crtc_state(acrtc->base.state)))
1580 						rc = amdgpu_dm_crtc_set_vupdate_irq(
1581 							&acrtc->base, true);
1582 				} else
1583 					rc = amdgpu_dm_crtc_set_vupdate_irq(
1584 							&acrtc->base, false);
1585 
1586 				if (rc)
1587 					drm_warn(adev_to_drm(adev), "Failed to %sable vupdate interrupt\n",
1588 						enable ? "en" : "dis");
1589 			}
1590 
1591 			irq_source = IRQ_TYPE_VBLANK + acrtc->otg_inst;
1592 			/* During gpu-reset we disable and then enable vblank irq, so
1593 			 * don't use amdgpu_irq_get/put() to avoid refcount change.
1594 			 */
1595 			if (!dc_interrupt_set(adev->dm.dc, irq_source, enable))
1596 				drm_warn(adev_to_drm(adev), "Failed to %sable vblank interrupt\n", enable ? "en" : "dis");
1597 
1598 		} else if (acrtc && state->stream_status[i].plane_count != 0) {
1599 			/* DCN only needs to toggle VUPDATE_NO_LOCK */
1600 			rc = amdgpu_dm_crtc_set_vupdate_irq(&acrtc->base, enable);
1601 			if (rc)
1602 				drm_warn(adev_to_drm(adev), "Failed to %sable vupdate interrupt\n",
1603 					 enable ? "en" : "dis");
1604 		}
1605 	}
1606 
1607 }
1608 
1609 DEFINE_FREE(state_release, struct dc_state *, if (_T) dc_state_release(_T))
1610 
1611 static enum dc_status amdgpu_dm_commit_zero_streams(struct dc *dc)
1612 {
1613 	struct dc_state *context __free(state_release) = NULL;
1614 	int i;
1615 	struct dc_stream_state *del_streams[MAX_PIPES];
1616 	int del_streams_count = 0;
1617 	struct dc_commit_streams_params params = {};
1618 
1619 	memset(del_streams, 0, sizeof(del_streams));
1620 
1621 	context = dc_state_create_current_copy(dc);
1622 	if (context == NULL)
1623 		return DC_ERROR_UNEXPECTED;
1624 
1625 	/* First remove from context all streams */
1626 	for (i = 0; i < context->stream_count; i++) {
1627 		struct dc_stream_state *stream = context->streams[i];
1628 
1629 		del_streams[del_streams_count++] = stream;
1630 	}
1631 
1632 	/* Remove all planes for removed streams and then remove the streams */
1633 	for (i = 0; i < del_streams_count; i++) {
1634 		enum dc_status res;
1635 
1636 		if (!dc_state_rem_all_planes_for_stream(dc, del_streams[i], context))
1637 			return DC_FAIL_DETACH_SURFACES;
1638 
1639 		res = dc_state_remove_stream(dc, context, del_streams[i]);
1640 		if (res != DC_OK)
1641 			return res;
1642 	}
1643 
1644 	params.streams = context->streams;
1645 	params.stream_count = context->stream_count;
1646 
1647 	return dc_commit_streams(dc, &params);
1648 }
1649 
1650 static int dm_cache_state(struct amdgpu_device *adev)
1651 {
1652 	int r;
1653 
1654 	adev->dm.cached_state = drm_atomic_helper_suspend(adev_to_drm(adev));
1655 	if (IS_ERR(adev->dm.cached_state)) {
1656 		r = PTR_ERR(adev->dm.cached_state);
1657 		adev->dm.cached_state = NULL;
1658 	}
1659 
1660 	return adev->dm.cached_state ? 0 : r;
1661 }
1662 
1663 static void dm_destroy_cached_state(struct amdgpu_device *adev)
1664 {
1665 	struct amdgpu_display_manager *dm = &adev->dm;
1666 	struct drm_device *ddev = adev_to_drm(adev);
1667 	struct dm_plane_state *dm_new_plane_state;
1668 	struct drm_plane_state *new_plane_state;
1669 	struct dm_crtc_state *dm_new_crtc_state;
1670 	struct drm_crtc_state *new_crtc_state;
1671 	struct drm_plane *plane;
1672 	struct drm_crtc *crtc;
1673 	int i;
1674 
1675 	if (!dm->cached_state)
1676 		return;
1677 
1678 	/* Force mode set in atomic commit */
1679 	for_each_new_crtc_in_state(dm->cached_state, crtc, new_crtc_state, i) {
1680 		new_crtc_state->active_changed = true;
1681 		dm_new_crtc_state = to_dm_crtc_state(new_crtc_state);
1682 		reset_freesync_config_for_crtc(dm_new_crtc_state);
1683 	}
1684 
1685 	/*
1686 	 * atomic_check is expected to create the dc states. We need to release
1687 	 * them here, since they were duplicated as part of the suspend
1688 	 * procedure.
1689 	 */
1690 	for_each_new_crtc_in_state(dm->cached_state, crtc, new_crtc_state, i) {
1691 		dm_new_crtc_state = to_dm_crtc_state(new_crtc_state);
1692 		if (dm_new_crtc_state->stream) {
1693 			WARN_ON(kref_read(&dm_new_crtc_state->stream->refcount) > 1);
1694 			dc_stream_release(dm_new_crtc_state->stream);
1695 			dm_new_crtc_state->stream = NULL;
1696 		}
1697 		dm_new_crtc_state->base.color_mgmt_changed = true;
1698 	}
1699 
1700 	for_each_new_plane_in_state(dm->cached_state, plane, new_plane_state, i) {
1701 		dm_new_plane_state = to_dm_plane_state(new_plane_state);
1702 		if (dm_new_plane_state->dc_state) {
1703 			WARN_ON(kref_read(&dm_new_plane_state->dc_state->refcount) > 1);
1704 			dc_plane_state_release(dm_new_plane_state->dc_state);
1705 			dm_new_plane_state->dc_state = NULL;
1706 		}
1707 	}
1708 
1709 	drm_atomic_helper_resume(ddev, dm->cached_state);
1710 
1711 	dm->cached_state = NULL;
1712 }
1713 
1714 static int dm_suspend(struct amdgpu_ip_block *ip_block)
1715 {
1716 	struct amdgpu_device *adev = ip_block->adev;
1717 	struct amdgpu_display_manager *dm = &adev->dm;
1718 
1719 	if (amdgpu_in_reset(adev)) {
1720 		enum dc_status res;
1721 
1722 		/* Quiesce ISM workers before taking dc_lock (workers take
1723 		 * dc_lock themselves; syncing under it would deadlock).
1724 		 */
1725 		amdgpu_dm_ism_disable(dm);
1726 
1727 		mutex_lock(&dm->dc_lock);
1728 
1729 		amdgpu_dm_ism_force_full_power(dm);
1730 		dc_allow_idle_optimizations(adev->dm.dc, false);
1731 
1732 		dm->cached_dc_state = dc_state_create_copy(dm->dc->current_state);
1733 
1734 		if (dm->cached_dc_state)
1735 			dm_gpureset_toggle_interrupts(adev, dm->cached_dc_state, false);
1736 
1737 		res = amdgpu_dm_commit_zero_streams(dm->dc);
1738 		if (res != DC_OK) {
1739 			drm_err(adev_to_drm(adev), "Failed to commit zero streams: %d\n", res);
1740 			return -EINVAL;
1741 		}
1742 
1743 		amdgpu_dm_irq_suspend(adev);
1744 
1745 		amdgpu_dm_hpd_rx_irq_work_suspend(dm);
1746 
1747 		return 0;
1748 	}
1749 
1750 	if (!adev->dm.cached_state) {
1751 		int r = dm_cache_state(adev);
1752 
1753 		if (r)
1754 			return r;
1755 	}
1756 
1757 	amdgpu_dm_s3_handle_hdmi_cec(adev_to_drm(adev), true);
1758 
1759 	s3_handle_mst(adev_to_drm(adev), true);
1760 
1761 	amdgpu_dm_irq_suspend(adev);
1762 
1763 	/*
1764 	 * Quiesce ISM workers before taking dc_lock (workers take dc_lock
1765 	 * themselves; syncing under it would deadlock).
1766 	 */
1767 	amdgpu_dm_ism_disable(dm);
1768 	scoped_guard(mutex, &dm->dc_lock)
1769 		amdgpu_dm_ism_force_full_power(dm);
1770 
1771 	amdgpu_dm_hpd_rx_irq_work_suspend(dm);
1772 
1773 	dc_set_power_state(dm->dc, DC_ACPI_CM_POWER_STATE_D3);
1774 
1775 	if (dm->dc->caps.ips_support && adev->in_s0ix)
1776 		dc_allow_idle_optimizations(dm->dc, true);
1777 
1778 	dc_dmub_srv_set_power_state(dm->dc->ctx->dmub_srv, DC_ACPI_CM_POWER_STATE_D3);
1779 
1780 	return 0;
1781 }
1782 
1783 void amdgpu_dm_emulated_link_detect(struct dc_link *link)
1784 {
1785 	struct dc_sink_init_data sink_init_data = { 0 };
1786 	struct display_sink_capability sink_caps = { 0 };
1787 	enum dc_edid_status edid_status;
1788 	struct dc_context *dc_ctx = link->ctx;
1789 	struct drm_device *dev = adev_to_drm(dc_ctx->driver_context);
1790 	struct dc_sink *sink = NULL;
1791 	struct dc_sink *prev_sink = NULL;
1792 
1793 	link->type = dc_connection_none;
1794 	prev_sink = link->local_sink;
1795 
1796 	if (prev_sink)
1797 		dc_sink_release(prev_sink);
1798 
1799 	switch (link->connector_signal) {
1800 	case SIGNAL_TYPE_HDMI_TYPE_A: {
1801 		sink_caps.transaction_type = DDC_TRANSACTION_TYPE_I2C;
1802 		sink_caps.signal = SIGNAL_TYPE_HDMI_TYPE_A;
1803 		break;
1804 	}
1805 
1806 	case SIGNAL_TYPE_DVI_SINGLE_LINK: {
1807 		sink_caps.transaction_type = DDC_TRANSACTION_TYPE_I2C;
1808 		sink_caps.signal = SIGNAL_TYPE_DVI_SINGLE_LINK;
1809 		break;
1810 	}
1811 
1812 	case SIGNAL_TYPE_DVI_DUAL_LINK: {
1813 		sink_caps.transaction_type = DDC_TRANSACTION_TYPE_I2C;
1814 		sink_caps.signal = SIGNAL_TYPE_DVI_DUAL_LINK;
1815 		break;
1816 	}
1817 
1818 	case SIGNAL_TYPE_LVDS: {
1819 		sink_caps.transaction_type = DDC_TRANSACTION_TYPE_I2C;
1820 		sink_caps.signal = SIGNAL_TYPE_LVDS;
1821 		break;
1822 	}
1823 
1824 	case SIGNAL_TYPE_EDP: {
1825 		sink_caps.transaction_type =
1826 			DDC_TRANSACTION_TYPE_I2C_OVER_AUX;
1827 		sink_caps.signal = SIGNAL_TYPE_EDP;
1828 		break;
1829 	}
1830 
1831 	case SIGNAL_TYPE_DISPLAY_PORT: {
1832 		sink_caps.transaction_type =
1833 			DDC_TRANSACTION_TYPE_I2C_OVER_AUX;
1834 		sink_caps.signal = SIGNAL_TYPE_VIRTUAL;
1835 		break;
1836 	}
1837 
1838 	default:
1839 		drm_err(dev, "Invalid connector type! signal:%d\n",
1840 			link->connector_signal);
1841 		return;
1842 	}
1843 
1844 	sink_init_data.link = link;
1845 	sink_init_data.sink_signal = sink_caps.signal;
1846 
1847 	sink = dc_sink_create(&sink_init_data);
1848 	if (!sink) {
1849 		drm_err(dev, "Failed to create sink!\n");
1850 		return;
1851 	}
1852 
1853 	/* dc_sink_create returns a new reference */
1854 	link->local_sink = sink;
1855 
1856 	edid_status = dm_helpers_read_local_edid(
1857 			link->ctx,
1858 			link,
1859 			sink);
1860 
1861 	if (edid_status != EDID_OK)
1862 		drm_err(dev, "Failed to read EDID\n");
1863 
1864 }
1865 
1866 static void dm_gpureset_commit_state(struct dc_state *dc_state,
1867 				     struct amdgpu_display_manager *dm)
1868 {
1869 	struct {
1870 		struct dc_surface_update surface_updates[MAX_SURFACES];
1871 		struct dc_plane_info plane_infos[MAX_SURFACES];
1872 		struct dc_scaling_info scaling_infos[MAX_SURFACES];
1873 		struct dc_flip_addrs flip_addrs[MAX_SURFACES];
1874 		struct dc_stream_update stream_update;
1875 	} *bundle __free(kfree);
1876 	int k, m;
1877 
1878 	bundle = kzalloc_obj(*bundle);
1879 
1880 	if (!bundle) {
1881 		drm_err(dm->ddev, "Failed to allocate update bundle\n");
1882 		return;
1883 	}
1884 
1885 	for (k = 0; k < dc_state->stream_count; k++) {
1886 		bundle->stream_update.stream = dc_state->streams[k];
1887 
1888 		for (m = 0; m < dc_state->stream_status[k].plane_count; m++) {
1889 			bundle->surface_updates[m].surface =
1890 				dc_state->stream_status[k].plane_states[m];
1891 			bundle->surface_updates[m].surface->force_full_update =
1892 				true;
1893 		}
1894 
1895 		update_planes_and_stream_adapter(dm->dc,
1896 					 UPDATE_TYPE_FULL,
1897 					 dc_state->stream_status[k].plane_count,
1898 					 dc_state->streams[k],
1899 					 &bundle->stream_update,
1900 					 bundle->surface_updates);
1901 	}
1902 }
1903 
1904 void amdgpu_dm_apply_delay_after_dpcd_poweroff(struct amdgpu_device *adev,
1905 											   struct dc_sink *sink)
1906 {
1907 	struct dc_panel_patch *ppatch = NULL;
1908 
1909 	if (!sink)
1910 		return;
1911 
1912 	ppatch = &sink->edid_caps.panel_patch;
1913 	if (ppatch->wait_after_dpcd_poweroff_ms) {
1914 		msleep(ppatch->wait_after_dpcd_poweroff_ms);
1915 		drm_dbg_driver(adev_to_drm(adev),
1916 			       "%s: adding a %ds delay as w/a for panel\n",
1917 			       __func__,
1918 			       ppatch->wait_after_dpcd_poweroff_ms / 1000);
1919 	}
1920 }
1921 EXPORT_IF_KUNIT(amdgpu_dm_apply_delay_after_dpcd_poweroff);
1922 
1923 /**
1924  * amdgpu_dm_dump_links_and_sinks - Debug dump of all DC links and their sinks
1925  * @adev: amdgpu device pointer
1926  *
1927  * Iterates through all DC links and dumps information about local and remote
1928  * (MST) sinks. Should be called after connector detection is complete to see
1929  * the final state of all links.
1930  */
1931 static void amdgpu_dm_dump_links_and_sinks(struct amdgpu_device *adev)
1932 {
1933 	struct dc *dc = adev->dm.dc;
1934 	struct drm_device *dev = adev_to_drm(adev);
1935 	int li;
1936 
1937 	if (!dc)
1938 		return;
1939 
1940 	for (li = 0; li < dc->link_count; li++) {
1941 		struct dc_link *l = dc->links[li];
1942 		const char *name = NULL;
1943 		int rs;
1944 
1945 		if (!l)
1946 			continue;
1947 		if (l->local_sink && l->local_sink->edid_caps.display_name[0])
1948 			name = l->local_sink->edid_caps.display_name;
1949 		else
1950 			name = "n/a";
1951 
1952 		drm_dbg_kms(dev,
1953 			"LINK_DUMP[%d]: local_sink=%p type=%d sink_signal=%d sink_count=%u edid_name=%s mst_capable=%d mst_alloc_streams=%d\n",
1954 			li,
1955 			l->local_sink,
1956 			l->type,
1957 			l->local_sink ? l->local_sink->sink_signal : SIGNAL_TYPE_NONE,
1958 			l->sink_count,
1959 			name,
1960 			l->dpcd_caps.is_mst_capable,
1961 			l->mst_stream_alloc_table.stream_count);
1962 
1963 		/* Dump remote (MST) sinks if any */
1964 		for (rs = 0; rs < l->sink_count; rs++) {
1965 			struct dc_sink *rsink = l->remote_sinks[rs];
1966 			const char *rname = NULL;
1967 
1968 			if (!rsink)
1969 				continue;
1970 			if (rsink->edid_caps.display_name[0])
1971 				rname = rsink->edid_caps.display_name;
1972 			else
1973 				rname = "n/a";
1974 			drm_dbg_kms(dev,
1975 				"  REMOTE_SINK[%d:%d]: sink=%p signal=%d edid_name=%s\n",
1976 				li, rs,
1977 				rsink,
1978 				rsink->sink_signal,
1979 				rname);
1980 		}
1981 	}
1982 }
1983 
1984 static int dm_resume(struct amdgpu_ip_block *ip_block)
1985 {
1986 	struct amdgpu_device *adev = ip_block->adev;
1987 	struct drm_device *ddev = adev_to_drm(adev);
1988 	struct amdgpu_display_manager *dm = &adev->dm;
1989 	struct amdgpu_dm_connector *aconnector;
1990 	struct drm_connector *connector;
1991 	struct drm_connector_list_iter iter;
1992 	struct dm_atomic_state *dm_state = to_dm_atomic_state(dm->atomic_obj.state);
1993 	enum dc_connection_type new_connection_type = dc_connection_none;
1994 	struct dc_state *dc_state;
1995 	int i, r, j;
1996 	struct dc_commit_streams_params commit_params = {};
1997 
1998 	if (dm->dc->caps.ips_support) {
1999 		if (!amdgpu_in_reset(adev))
2000 			mutex_lock(&dm->dc_lock);
2001 
2002 		/* Need to set POWER_STATE_D0 first or it will not execute
2003 		 * idle_power_optimizations command to DMUB.
2004 		 */
2005 		dc_dmub_srv_set_power_state(dm->dc->ctx->dmub_srv, DC_ACPI_CM_POWER_STATE_D0);
2006 		dc_dmub_srv_apply_idle_power_optimizations(dm->dc, false);
2007 
2008 		if (!amdgpu_in_reset(adev))
2009 			mutex_unlock(&dm->dc_lock);
2010 	}
2011 
2012 	if (amdgpu_in_reset(adev)) {
2013 		dc_state = dm->cached_dc_state;
2014 
2015 		/*
2016 		 * The dc->current_state is backed up into dm->cached_dc_state
2017 		 * before we commit 0 streams.
2018 		 *
2019 		 * DC will clear link encoder assignments on the real state
2020 		 * but the changes won't propagate over to the copy we made
2021 		 * before the 0 streams commit.
2022 		 *
2023 		 * DC expects that link encoder assignments are *not* valid
2024 		 * when committing a state, so as a workaround we can copy
2025 		 * off of the current state.
2026 		 *
2027 		 * We lose the previous assignments, but we had already
2028 		 * commit 0 streams anyway.
2029 		 */
2030 		link_enc_cfg_copy(adev->dm.dc->current_state, dc_state);
2031 
2032 		r = dm_dmub_hw_init(adev);
2033 		if (r) {
2034 			drm_err(adev_to_drm(adev), "DMUB interface failed to initialize: status=%d\n", r);
2035 			return r;
2036 		}
2037 
2038 		dc_dmub_srv_set_power_state(dm->dc->ctx->dmub_srv, DC_ACPI_CM_POWER_STATE_D0);
2039 		dc_set_power_state(dm->dc, DC_ACPI_CM_POWER_STATE_D0);
2040 
2041 		dc_resume(dm->dc);
2042 
2043 		amdgpu_dm_ism_enable(dm);
2044 		amdgpu_dm_irq_resume_early(adev);
2045 
2046 		for (i = 0; i < dc_state->stream_count; i++) {
2047 			dc_state->streams[i]->mode_changed = true;
2048 			for (j = 0; j < dc_state->stream_status[i].plane_count; j++) {
2049 				dc_pipe_update_bits_set_full(&dc_state->stream_status[i].plane_states[j]->update_bits);
2050 			}
2051 		}
2052 
2053 		if (dc_is_dmub_outbox_supported(adev->dm.dc)) {
2054 			amdgpu_dm_outbox_init(adev);
2055 			dc_enable_dmub_outbox(adev->dm.dc);
2056 		}
2057 
2058 		commit_params.streams = dc_state->streams;
2059 		commit_params.stream_count = dc_state->stream_count;
2060 		dc_exit_ips_for_hw_access(dm->dc);
2061 		WARN_ON(!dc_commit_streams(dm->dc, &commit_params));
2062 
2063 		dm_gpureset_commit_state(dm->cached_dc_state, dm);
2064 
2065 		dm_gpureset_toggle_interrupts(adev, dm->cached_dc_state, true);
2066 
2067 		dc_state_release(dm->cached_dc_state);
2068 		dm->cached_dc_state = NULL;
2069 
2070 		amdgpu_dm_irq_resume_late(adev);
2071 
2072 		mutex_unlock(&dm->dc_lock);
2073 
2074 		/* set the backlight after a reset */
2075 		for (i = 0; i < dm->num_of_edps; i++) {
2076 			if (dm->backlight_dev[i])
2077 				amdgpu_dm_backlight_set_level(dm, i, dm->brightness[i]);
2078 		}
2079 
2080 		return 0;
2081 	}
2082 	/* Recreate dc_state - DC invalidates it when setting power state to S3. */
2083 	dc_state_release(dm_state->context);
2084 	dm_state->context = dc_state_create(dm->dc, NULL);
2085 	/* TODO: Remove dc_state->dccg, use dc->dccg directly. */
2086 
2087 	/* Before powering on DC we need to re-initialize DMUB. */
2088 	dm_dmub_hw_resume(adev);
2089 
2090 	/* Re-enable outbox interrupts for DPIA. */
2091 	if (dc_is_dmub_outbox_supported(adev->dm.dc)) {
2092 		amdgpu_dm_outbox_init(adev);
2093 		dc_enable_dmub_outbox(adev->dm.dc);
2094 	}
2095 
2096 	/* power on hardware */
2097 	dc_dmub_srv_set_power_state(dm->dc->ctx->dmub_srv, DC_ACPI_CM_POWER_STATE_D0);
2098 	dc_set_power_state(dm->dc, DC_ACPI_CM_POWER_STATE_D0);
2099 
2100 	/* program HPD filter */
2101 	dc_resume(dm->dc);
2102 
2103 	scoped_guard(mutex, &dm->dc_lock)
2104 		amdgpu_dm_ism_enable(dm);
2105 
2106 	/*
2107 	 * early enable HPD Rx IRQ, should be done before set mode as short
2108 	 * pulse interrupts are used for MST
2109 	 */
2110 	amdgpu_dm_irq_resume_early(adev);
2111 
2112 	amdgpu_dm_s3_handle_hdmi_cec(ddev, false);
2113 
2114 	/* On resume we need to rewrite the MSTM control bits to enable MST*/
2115 	s3_handle_mst(ddev, false);
2116 
2117 	/* Do detection*/
2118 	drm_connector_list_iter_begin(ddev, &iter);
2119 	drm_for_each_connector_iter(connector, &iter) {
2120 		bool ret;
2121 
2122 		if (connector->connector_type == DRM_MODE_CONNECTOR_WRITEBACK)
2123 			continue;
2124 
2125 		aconnector = to_amdgpu_dm_connector(connector);
2126 
2127 		if (!aconnector->dc_link)
2128 			continue;
2129 
2130 		/*
2131 		 * this is the case when traversing through already created end sink
2132 		 * MST connectors, should be skipped
2133 		 */
2134 		if (aconnector->mst_root)
2135 			continue;
2136 
2137 		/* Skip eDP detection, when there is no sink present */
2138 		if (aconnector->dc_link->connector_signal == SIGNAL_TYPE_EDP &&
2139 		    !aconnector->dc_link->edp_sink_present)
2140 			continue;
2141 
2142 		guard(mutex)(&aconnector->hpd_lock);
2143 		if (!dc_link_detect_connection_type(aconnector->dc_link, &new_connection_type))
2144 			drm_err(adev_to_drm(adev), "KMS: Failed to detect connector\n");
2145 
2146 		if (aconnector->base.force && new_connection_type == dc_connection_none) {
2147 			amdgpu_dm_emulated_link_detect(aconnector->dc_link);
2148 		} else {
2149 			guard(mutex)(&dm->dc_lock);
2150 			dc_exit_ips_for_hw_access(dm->dc);
2151 			ret = dc_link_detect(aconnector->dc_link, DETECT_REASON_RESUMEFROMS3S4);
2152 			if (ret) {
2153 				/* w/a delay for certain panels */
2154 				amdgpu_dm_apply_delay_after_dpcd_poweroff(adev, aconnector->dc_sink);
2155 			}
2156 		}
2157 
2158 		if (aconnector->fake_enable && aconnector->dc_link->local_sink)
2159 			aconnector->fake_enable = false;
2160 
2161 		if (aconnector->dc_sink)
2162 			dc_sink_release(aconnector->dc_sink);
2163 		aconnector->dc_sink = NULL;
2164 		amdgpu_dm_update_connector_after_detect(aconnector);
2165 	}
2166 	drm_connector_list_iter_end(&iter);
2167 
2168 	dm_destroy_cached_state(adev);
2169 
2170 	/* Do mst topology probing after resuming cached state*/
2171 	drm_connector_list_iter_begin(ddev, &iter);
2172 	drm_for_each_connector_iter(connector, &iter) {
2173 		bool init = false;
2174 
2175 		if (connector->connector_type == DRM_MODE_CONNECTOR_WRITEBACK)
2176 			continue;
2177 
2178 		aconnector = to_amdgpu_dm_connector(connector);
2179 		if (aconnector->dc_link->type != dc_connection_mst_branch ||
2180 		    aconnector->mst_root)
2181 			continue;
2182 
2183 		scoped_guard(mutex, &aconnector->mst_mgr.lock) {
2184 			init = !aconnector->mst_mgr.mst_primary;
2185 		}
2186 		if (init)
2187 			dm_helpers_dp_mst_start_top_mgr(aconnector->dc_link->ctx,
2188 				aconnector->dc_link, false);
2189 		else
2190 			drm_dp_mst_topology_queue_probe(&aconnector->mst_mgr);
2191 	}
2192 	drm_connector_list_iter_end(&iter);
2193 
2194 	/* Debug dump: list all DC links and their associated sinks after detection
2195 	 * is complete for all connectors. This provides a comprehensive view of the
2196 	 * final state without repeating the dump for each connector.
2197 	 */
2198 	amdgpu_dm_dump_links_and_sinks(adev);
2199 
2200 	amdgpu_dm_irq_resume_late(adev);
2201 
2202 	amdgpu_dm_smu_write_watermarks_table(adev);
2203 
2204 	drm_kms_helper_hotplug_event(ddev);
2205 
2206 	return 0;
2207 }
2208 
2209 /**
2210  * DOC: DM Lifecycle
2211  *
2212  * DM (and consequently DC) is registered in the amdgpu base driver as a IP
2213  * block. When CONFIG_DRM_AMD_DC is enabled, the DM device IP block is added to
2214  * the base driver's device list to be initialized and torn down accordingly.
2215  *
2216  * The functions to do so are provided as hooks in &struct amd_ip_funcs.
2217  */
2218 
2219 static const struct amd_ip_funcs amdgpu_dm_funcs = {
2220 	.name = "dm",
2221 	.early_init = dm_early_init,
2222 	.late_init = dm_late_init,
2223 	.sw_init = dm_sw_init,
2224 	.sw_fini = dm_sw_fini,
2225 	.early_fini = amdgpu_dm_early_fini,
2226 	.hw_init = dm_hw_init,
2227 	.hw_fini = dm_hw_fini,
2228 	.suspend = dm_suspend,
2229 	.resume = dm_resume,
2230 	.is_idle = dm_is_idle,
2231 	.wait_for_idle = dm_wait_for_idle,
2232 	.soft_reset = dm_soft_reset,
2233 	.set_clockgating_state = dm_set_clockgating_state,
2234 	.set_powergating_state = dm_set_powergating_state,
2235 };
2236 
2237 const struct amdgpu_ip_block_version dm_ip_block = {
2238 	.type = AMD_IP_BLOCK_TYPE_DCE,
2239 	.major = 1,
2240 	.minor = 0,
2241 	.rev = 0,
2242 	.funcs = &amdgpu_dm_funcs,
2243 };
2244 
2245 
2246 /**
2247  * DOC: atomic
2248  *
2249  * *WIP*
2250  */
2251 
2252 static const struct drm_mode_config_funcs amdgpu_dm_mode_funcs = {
2253 	.fb_create = amdgpu_display_user_framebuffer_create,
2254 	.get_format_info = amdgpu_dm_plane_get_format_info,
2255 	.atomic_check = amdgpu_dm_atomic_check,
2256 	.atomic_commit = drm_atomic_helper_commit,
2257 };
2258 
2259 static struct drm_mode_config_helper_funcs amdgpu_dm_mode_config_helperfuncs = {
2260 	.atomic_commit_tail = amdgpu_dm_atomic_commit_tail,
2261 	.atomic_commit_setup = amdgpu_dm_atomic_setup_commit,
2262 };
2263 
2264 /*
2265  * Acquires the lock for the atomic state object and returns
2266  * the new atomic state.
2267  *
2268  * This should only be called during atomic check.
2269  */
2270 int dm_atomic_get_state(struct drm_atomic_commit *state,
2271 			struct dm_atomic_state **dm_state)
2272 {
2273 	struct drm_device *dev = state->dev;
2274 	struct amdgpu_device *adev = drm_to_adev(dev);
2275 	struct amdgpu_display_manager *dm = &adev->dm;
2276 	struct drm_private_state *priv_state;
2277 
2278 	if (*dm_state)
2279 		return 0;
2280 
2281 	priv_state = drm_atomic_get_private_obj_state(state, &dm->atomic_obj);
2282 	if (IS_ERR(priv_state))
2283 		return PTR_ERR(priv_state);
2284 
2285 	*dm_state = to_dm_atomic_state(priv_state);
2286 
2287 	return 0;
2288 }
2289 
2290 STATIC_IFN_KUNIT struct dm_atomic_state *
2291 dm_atomic_get_new_state(struct drm_atomic_commit *state)
2292 {
2293 	struct drm_device *dev = state->dev;
2294 	struct amdgpu_device *adev = drm_to_adev(dev);
2295 	struct amdgpu_display_manager *dm = &adev->dm;
2296 	struct drm_private_obj *obj;
2297 	struct drm_private_state *new_obj_state;
2298 	int i;
2299 
2300 	for_each_new_private_obj_in_state(state, obj, new_obj_state, i) {
2301 		if (obj->funcs == dm->atomic_obj.funcs)
2302 			return to_dm_atomic_state(new_obj_state);
2303 	}
2304 
2305 	return NULL;
2306 }
2307 EXPORT_IF_KUNIT(dm_atomic_get_new_state);
2308 
2309 static struct drm_private_state *
2310 dm_atomic_duplicate_state(struct drm_private_obj *obj)
2311 {
2312 	struct dm_atomic_state *old_state, *new_state;
2313 
2314 	new_state = kzalloc_obj(*new_state);
2315 	if (!new_state)
2316 		return NULL;
2317 
2318 	__drm_atomic_helper_private_obj_duplicate_state(obj, &new_state->base);
2319 
2320 	old_state = to_dm_atomic_state(obj->state);
2321 
2322 	if (old_state && old_state->context)
2323 		new_state->context = dc_state_create_copy(old_state->context);
2324 
2325 	if (!new_state->context) {
2326 		kfree(new_state);
2327 		return NULL;
2328 	}
2329 
2330 	return &new_state->base;
2331 }
2332 
2333 STATIC_IFN_KUNIT void dm_atomic_destroy_state(struct drm_private_obj *obj,
2334 					      struct drm_private_state *state)
2335 {
2336 	struct dm_atomic_state *dm_state = to_dm_atomic_state(state);
2337 
2338 	if (dm_state && dm_state->context)
2339 		dc_state_release(dm_state->context);
2340 
2341 	kfree(dm_state);
2342 }
2343 EXPORT_IF_KUNIT(dm_atomic_destroy_state);
2344 
2345 static struct drm_private_state *
2346 dm_atomic_create_state(struct drm_private_obj *obj)
2347 {
2348 	struct amdgpu_device *adev = drm_to_adev(obj->dev);
2349 	struct dm_atomic_state *dm_state;
2350 	struct dc_state *context;
2351 
2352 	dm_state = kzalloc_obj(*dm_state);
2353 	if (!dm_state)
2354 		return ERR_PTR(-ENOMEM);
2355 
2356 	context = dc_state_create_current_copy(adev->dm.dc);
2357 	if (!context) {
2358 		kfree(dm_state);
2359 		return ERR_PTR(-ENOMEM);
2360 	}
2361 
2362 	__drm_atomic_helper_private_obj_create_state(obj, &dm_state->base);
2363 	dm_state->context = context;
2364 
2365 	return &dm_state->base;
2366 }
2367 
2368 static struct drm_private_state_funcs dm_atomic_state_funcs = {
2369 	.atomic_create_state = dm_atomic_create_state,
2370 	.atomic_duplicate_state = dm_atomic_duplicate_state,
2371 	.atomic_destroy_state = dm_atomic_destroy_state,
2372 };
2373 
2374 static int amdgpu_dm_mode_config_init(struct amdgpu_device *adev)
2375 {
2376 	int r;
2377 
2378 	adev->mode_info.mode_config_initialized = true;
2379 
2380 	adev_to_drm(adev)->mode_config.funcs = (void *)&amdgpu_dm_mode_funcs;
2381 	adev_to_drm(adev)->mode_config.helper_private = &amdgpu_dm_mode_config_helperfuncs;
2382 
2383 	adev_to_drm(adev)->mode_config.max_width = 16384;
2384 	adev_to_drm(adev)->mode_config.max_height = 16384;
2385 
2386 	adev_to_drm(adev)->mode_config.preferred_depth = 24;
2387 	if (adev->asic_type == CHIP_HAWAII)
2388 		/* disable prefer shadow for now due to hibernation issues */
2389 		adev_to_drm(adev)->mode_config.prefer_shadow = 0;
2390 	else
2391 		adev_to_drm(adev)->mode_config.prefer_shadow = 1;
2392 	/* indicates support for immediate flip */
2393 	adev_to_drm(adev)->mode_config.async_page_flip = true;
2394 
2395 	drm_atomic_private_obj_init(adev_to_drm(adev),
2396 				    &adev->dm.atomic_obj,
2397 				    &dm_atomic_state_funcs);
2398 
2399 	r = amdgpu_display_modeset_create_props(adev);
2400 	if (r)
2401 		return r;
2402 
2403 #ifdef AMD_PRIVATE_COLOR
2404 	if (amdgpu_dm_create_color_properties(adev))
2405 		return -ENOMEM;
2406 #endif
2407 
2408 	r = amdgpu_dm_audio_init(adev);
2409 	if (r)
2410 		return r;
2411 
2412 	return 0;
2413 }
2414 
2415 static int initialize_plane(struct amdgpu_display_manager *dm,
2416 			    struct amdgpu_mode_info *mode_info, int plane_id,
2417 			    enum drm_plane_type plane_type,
2418 			    const struct dc_plane_cap *plane_cap)
2419 {
2420 	struct drm_plane *plane;
2421 	unsigned long possible_crtcs;
2422 	int ret = 0;
2423 
2424 	plane = kzalloc_obj(struct drm_plane);
2425 	if (!plane) {
2426 		drm_err(adev_to_drm(dm->adev), "KMS: Failed to allocate plane\n");
2427 		return -ENOMEM;
2428 	}
2429 	plane->type = plane_type;
2430 
2431 	/*
2432 	 * HACK: IGT tests expect that the primary plane for a CRTC
2433 	 * can only have one possible CRTC. Only expose support for
2434 	 * any CRTC if they're not going to be used as a primary plane
2435 	 * for a CRTC - like overlay or underlay planes.
2436 	 */
2437 	possible_crtcs = 1 << plane_id;
2438 	if (plane_id >= dm->dc->caps.max_streams)
2439 		possible_crtcs = 0xff;
2440 
2441 	ret = amdgpu_dm_plane_init(dm, plane, possible_crtcs, plane_cap);
2442 
2443 	if (ret) {
2444 		drm_err(adev_to_drm(dm->adev), "KMS: Failed to initialize plane\n");
2445 		kfree(plane);
2446 		return ret;
2447 	}
2448 
2449 	if (mode_info)
2450 		mode_info->planes[plane_id] = plane;
2451 
2452 	return ret;
2453 }
2454 
2455 
2456 /*
2457  * In this architecture, the association
2458  * connector -> encoder -> crtc
2459  * id not really requried. The crtc and connector will hold the
2460  * display_index as an abstraction to use with DAL component
2461  *
2462  * Returns 0 on success
2463  */
2464 static int amdgpu_dm_initialize_drm_device(struct amdgpu_device *adev)
2465 {
2466 	struct amdgpu_display_manager *dm = &adev->dm;
2467 	s32 i;
2468 	struct amdgpu_dm_connector *aconnector = NULL;
2469 	struct amdgpu_encoder *aencoder = NULL;
2470 	struct amdgpu_mode_info *mode_info = &adev->mode_info;
2471 	u32 link_cnt;
2472 	s32 primary_planes;
2473 	enum dc_connection_type new_connection_type = dc_connection_none;
2474 	const struct dc_plane_cap *plane;
2475 	bool psr_feature_enabled = false;
2476 	bool replay_feature_enabled = false;
2477 	int max_overlay = dm->dc->caps.max_slave_planes;
2478 
2479 	dm->display_indexes_num = dm->dc->caps.max_streams;
2480 	/* Update the actual used number of crtc */
2481 	adev->mode_info.num_crtc = adev->dm.display_indexes_num;
2482 
2483 	amdgpu_dm_set_irq_funcs(adev);
2484 
2485 	link_cnt = dm->dc->caps.max_links;
2486 	if (amdgpu_dm_mode_config_init(dm->adev)) {
2487 		drm_err(adev_to_drm(adev), "DM: Failed to initialize mode config\n");
2488 		return -EINVAL;
2489 	}
2490 
2491 	/* There is one primary plane per CRTC */
2492 	primary_planes = dm->dc->caps.max_streams;
2493 	if (primary_planes > AMDGPU_MAX_PLANES) {
2494 		drm_err(adev_to_drm(adev), "DM: Plane nums out of 6 planes\n");
2495 		return -EINVAL;
2496 	}
2497 
2498 	/*
2499 	 * Initialize primary planes, implicit planes for legacy IOCTLS.
2500 	 * Order is reversed to match iteration order in atomic check.
2501 	 */
2502 	for (i = (primary_planes - 1); i >= 0; i--) {
2503 		plane = &dm->dc->caps.planes[i];
2504 
2505 		if (initialize_plane(dm, mode_info, i,
2506 				     DRM_PLANE_TYPE_PRIMARY, plane)) {
2507 			drm_err(adev_to_drm(adev), "KMS: Failed to initialize primary plane\n");
2508 			goto fail;
2509 		}
2510 	}
2511 
2512 	/*
2513 	 * Initialize overlay planes, index starting after primary planes.
2514 	 * These planes have a higher DRM index than the primary planes since
2515 	 * they should be considered as having a higher z-order.
2516 	 * Order is reversed to match iteration order in atomic check.
2517 	 *
2518 	 * Only support DCN for now, and only expose one so we don't encourage
2519 	 * userspace to use up all the pipes.
2520 	 */
2521 	for (i = 0; i < dm->dc->caps.max_planes; ++i) {
2522 		struct dc_plane_cap *plane = &dm->dc->caps.planes[i];
2523 
2524 		/* Do not create overlay if MPO disabled */
2525 		if (amdgpu_dc_debug_mask & DC_DISABLE_MPO)
2526 			break;
2527 
2528 		if (plane->type != DC_PLANE_TYPE_DCN_UNIVERSAL)
2529 			continue;
2530 
2531 		if (!plane->pixel_format_support.argb8888)
2532 			continue;
2533 
2534 		if (max_overlay-- == 0)
2535 			break;
2536 
2537 		if (initialize_plane(dm, NULL, primary_planes + i,
2538 				     DRM_PLANE_TYPE_OVERLAY, plane)) {
2539 			drm_err(adev_to_drm(adev), "KMS: Failed to initialize overlay plane\n");
2540 			goto fail;
2541 		}
2542 	}
2543 
2544 	for (i = 0; i < dm->dc->caps.max_streams; i++)
2545 		if (amdgpu_dm_crtc_init(dm, mode_info->planes[i], i)) {
2546 			drm_err(adev_to_drm(adev), "KMS: Failed to initialize crtc\n");
2547 			goto fail;
2548 		}
2549 
2550 	/* Use Outbox interrupt */
2551 	switch (amdgpu_ip_version(adev, DCE_HWIP, 0)) {
2552 	case IP_VERSION(3, 0, 0):
2553 	case IP_VERSION(3, 1, 2):
2554 	case IP_VERSION(3, 1, 3):
2555 	case IP_VERSION(3, 1, 4):
2556 	case IP_VERSION(3, 1, 5):
2557 	case IP_VERSION(3, 1, 6):
2558 	case IP_VERSION(3, 2, 0):
2559 	case IP_VERSION(3, 2, 1):
2560 	case IP_VERSION(2, 1, 0):
2561 	case IP_VERSION(3, 5, 0):
2562 	case IP_VERSION(3, 5, 1):
2563 	case IP_VERSION(3, 6, 0):
2564 	case IP_VERSION(4, 0, 1):
2565 	case IP_VERSION(4, 2, 0):
2566 	case IP_VERSION(4, 2, 1):
2567 		if (amdgpu_dm_register_outbox_irq_handlers(dm->adev)) {
2568 			drm_err(adev_to_drm(adev), "DM: Failed to initialize IRQ\n");
2569 			goto fail;
2570 		}
2571 		break;
2572 	default:
2573 		drm_dbg_kms(adev_to_drm(adev), "Unsupported DCN IP version for outbox: 0x%X\n",
2574 			      amdgpu_ip_version(adev, DCE_HWIP, 0));
2575 	}
2576 
2577 	/* Determine whether to enable PSR support by default. */
2578 	if (!(amdgpu_dc_debug_mask & DC_DISABLE_PSR)) {
2579 		switch (amdgpu_ip_version(adev, DCE_HWIP, 0)) {
2580 		case IP_VERSION(3, 1, 2):
2581 		case IP_VERSION(3, 1, 3):
2582 		case IP_VERSION(3, 1, 4):
2583 		case IP_VERSION(3, 1, 5):
2584 		case IP_VERSION(3, 1, 6):
2585 		case IP_VERSION(3, 2, 0):
2586 		case IP_VERSION(3, 2, 1):
2587 		case IP_VERSION(3, 5, 0):
2588 		case IP_VERSION(3, 5, 1):
2589 		case IP_VERSION(3, 6, 0):
2590 		case IP_VERSION(4, 0, 1):
2591 		case IP_VERSION(4, 2, 0):
2592 		case IP_VERSION(4, 2, 1):
2593 			psr_feature_enabled = true;
2594 			break;
2595 		default:
2596 			psr_feature_enabled = amdgpu_dc_feature_mask & DC_PSR_MASK;
2597 			break;
2598 		}
2599 	}
2600 
2601 	/* Determine whether to enable Replay support by default. */
2602 	if (!(amdgpu_dc_debug_mask & DC_DISABLE_REPLAY)) {
2603 		switch (amdgpu_ip_version(adev, DCE_HWIP, 0)) {
2604 		case IP_VERSION(3, 1, 4):
2605 		case IP_VERSION(3, 2, 0):
2606 		case IP_VERSION(3, 2, 1):
2607 		case IP_VERSION(3, 5, 0):
2608 		case IP_VERSION(3, 5, 1):
2609 		case IP_VERSION(3, 6, 0):
2610 		case IP_VERSION(4, 2, 0):
2611 		case IP_VERSION(4, 2, 1):
2612 			replay_feature_enabled = true;
2613 			break;
2614 
2615 		default:
2616 			replay_feature_enabled = amdgpu_dc_feature_mask & DC_REPLAY_MASK;
2617 			break;
2618 		}
2619 	}
2620 
2621 	if (link_cnt > MAX_LINKS) {
2622 		drm_err(adev_to_drm(adev),
2623 			"KMS: Cannot support more than %d display indexes\n",
2624 				MAX_LINKS);
2625 		goto fail;
2626 	}
2627 
2628 	/* loops over all connectors on the board */
2629 	for (i = 0; i < link_cnt; i++) {
2630 		struct dc_link *link = NULL;
2631 
2632 		link = dc_get_link_at_index(dm->dc, i);
2633 
2634 		if (link->connector_signal == SIGNAL_TYPE_VIRTUAL) {
2635 			struct amdgpu_dm_wb_connector *wbcon = kzalloc_obj(*wbcon);
2636 
2637 			if (!wbcon) {
2638 				drm_err(adev_to_drm(adev), "KMS: Failed to allocate writeback connector\n");
2639 				continue;
2640 			}
2641 
2642 			if (amdgpu_dm_wb_connector_init(dm, wbcon, i)) {
2643 				drm_err(adev_to_drm(adev), "KMS: Failed to initialize writeback connector\n");
2644 				kfree(wbcon);
2645 				continue;
2646 			}
2647 
2648 			link->psr_settings.psr_feature_enabled = false;
2649 			link->psr_settings.psr_version = DC_PSR_VERSION_UNSUPPORTED;
2650 
2651 			continue;
2652 		}
2653 
2654 		aconnector = kzalloc_obj(*aconnector);
2655 		if (!aconnector)
2656 			goto fail;
2657 
2658 		aencoder = kzalloc_obj(*aencoder);
2659 		if (!aencoder)
2660 			goto fail;
2661 
2662 		if (amdgpu_dm_encoder_init(dm->ddev, aencoder, i)) {
2663 			drm_err(adev_to_drm(adev), "KMS: Failed to initialize encoder\n");
2664 			goto fail;
2665 		}
2666 
2667 		if (amdgpu_dm_connector_init(dm, aconnector, i, aencoder)) {
2668 			drm_err(adev_to_drm(adev), "KMS: Failed to initialize connector\n");
2669 			goto fail;
2670 		}
2671 
2672 		if (dm->hpd_rx_offload_wq)
2673 			dm->hpd_rx_offload_wq[aconnector->base.index].aconnector =
2674 				aconnector;
2675 
2676 		if (!dc_link_detect_connection_type(link, &new_connection_type))
2677 			drm_err(adev_to_drm(adev), "KMS: Failed to detect connector\n");
2678 
2679 		if (aconnector->base.force && new_connection_type == dc_connection_none) {
2680 			amdgpu_dm_emulated_link_detect(link);
2681 			amdgpu_dm_update_connector_after_detect(aconnector);
2682 		} else {
2683 			bool ret = false;
2684 
2685 			mutex_lock(&dm->dc_lock);
2686 			dc_exit_ips_for_hw_access(dm->dc);
2687 			ret = dc_link_detect(link, DETECT_REASON_BOOT);
2688 			mutex_unlock(&dm->dc_lock);
2689 
2690 			if (ret) {
2691 				amdgpu_dm_update_connector_after_detect(aconnector);
2692 				amdgpu_dm_setup_backlight_device(dm, aconnector);
2693 
2694 				/* Disable PSR if Replay can be enabled */
2695 				if (replay_feature_enabled)
2696 					if (amdgpu_dm_set_replay_caps(link, aconnector))
2697 						psr_feature_enabled = false;
2698 
2699 				if (psr_feature_enabled) {
2700 					amdgpu_dm_set_psr_caps(link, aconnector);
2701 					drm_info(adev_to_drm(adev), "%s: PSR support %d, DC PSR ver %d, sink PSR ver %d DPCD caps 0x%x su_y_granularity %d\n",
2702 						 aconnector->base.name,
2703 						 link->psr_settings.psr_feature_enabled,
2704 						 link->psr_settings.psr_version,
2705 						 link->dpcd_caps.psr_info.psr_version,
2706 						 link->dpcd_caps.psr_info.psr_dpcd_caps.raw,
2707 						 link->dpcd_caps.psr_info.psr2_su_y_granularity_cap);
2708 				}
2709 			}
2710 		}
2711 		amdgpu_set_panel_orientation(&aconnector->base);
2712 	}
2713 
2714 	/* Debug dump: list all DC links and their associated sinks after detection
2715 	 * is complete for all connectors. This provides a comprehensive view of the
2716 	 * final state without repeating the dump for each connector.
2717 	 */
2718 	amdgpu_dm_dump_links_and_sinks(adev);
2719 
2720 	/* Software is initialized. Now we can register interrupt handlers. */
2721 	switch (adev->asic_type) {
2722 #if defined(CONFIG_DRM_AMD_DC_SI)
2723 	case CHIP_TAHITI:
2724 	case CHIP_PITCAIRN:
2725 	case CHIP_VERDE:
2726 	case CHIP_OLAND:
2727 #endif
2728 	case CHIP_BONAIRE:
2729 	case CHIP_HAWAII:
2730 	case CHIP_KAVERI:
2731 	case CHIP_KABINI:
2732 	case CHIP_MULLINS:
2733 	case CHIP_TONGA:
2734 	case CHIP_FIJI:
2735 	case CHIP_CARRIZO:
2736 	case CHIP_STONEY:
2737 	case CHIP_POLARIS11:
2738 	case CHIP_POLARIS10:
2739 	case CHIP_POLARIS12:
2740 	case CHIP_VEGAM:
2741 	case CHIP_VEGA10:
2742 	case CHIP_VEGA12:
2743 	case CHIP_VEGA20:
2744 		if (amdgpu_dm_dce110_register_irq_handlers(dm->adev)) {
2745 			drm_err(adev_to_drm(adev), "DM: Failed to initialize IRQ\n");
2746 			goto fail;
2747 		}
2748 		break;
2749 	default:
2750 		switch (amdgpu_ip_version(adev, DCE_HWIP, 0)) {
2751 		case IP_VERSION(1, 0, 0):
2752 		case IP_VERSION(1, 0, 1):
2753 		case IP_VERSION(2, 0, 2):
2754 		case IP_VERSION(2, 0, 3):
2755 		case IP_VERSION(2, 0, 0):
2756 		case IP_VERSION(2, 1, 0):
2757 		case IP_VERSION(3, 0, 0):
2758 		case IP_VERSION(3, 0, 2):
2759 		case IP_VERSION(3, 0, 3):
2760 		case IP_VERSION(3, 0, 1):
2761 		case IP_VERSION(3, 1, 2):
2762 		case IP_VERSION(3, 1, 3):
2763 		case IP_VERSION(3, 1, 4):
2764 		case IP_VERSION(3, 1, 5):
2765 		case IP_VERSION(3, 1, 6):
2766 		case IP_VERSION(3, 2, 0):
2767 		case IP_VERSION(3, 2, 1):
2768 		case IP_VERSION(3, 5, 0):
2769 		case IP_VERSION(3, 5, 1):
2770 		case IP_VERSION(3, 6, 0):
2771 		case IP_VERSION(4, 0, 1):
2772 		case IP_VERSION(4, 2, 0):
2773 		case IP_VERSION(4, 2, 1):
2774 			if (amdgpu_dm_dcn10_register_irq_handlers(dm->adev)) {
2775 				drm_err(adev_to_drm(adev), "DM: Failed to initialize IRQ\n");
2776 				goto fail;
2777 			}
2778 			break;
2779 		default:
2780 			drm_err(adev_to_drm(adev), "Unsupported DCE IP versions: 0x%X\n",
2781 					amdgpu_ip_version(adev, DCE_HWIP, 0));
2782 			goto fail;
2783 		}
2784 		break;
2785 	}
2786 
2787 	return 0;
2788 fail:
2789 	kfree(aencoder);
2790 	kfree(aconnector);
2791 
2792 	return -EINVAL;
2793 }
2794 
2795 static void amdgpu_dm_destroy_drm_device(struct amdgpu_display_manager *dm)
2796 {
2797 	if (dm->atomic_obj.state)
2798 		drm_atomic_private_obj_fini(&dm->atomic_obj);
2799 }
2800 
2801 /******************************************************************************
2802  * amdgpu_display_funcs functions
2803  *****************************************************************************/
2804 
2805 /*
2806  * dm_bandwidth_update - program display watermarks
2807  *
2808  * @adev: amdgpu_device pointer
2809  *
2810  * Calculate and program the display watermarks and line buffer allocation.
2811  */
2812 STATIC_IFN_KUNIT void dm_bandwidth_update(struct amdgpu_device *adev)
2813 {
2814 	/* TODO: implement later */
2815 }
2816 EXPORT_IF_KUNIT(dm_bandwidth_update);
2817 
2818 static const struct amdgpu_display_funcs dm_display_funcs = {
2819 	.bandwidth_update = dm_bandwidth_update, /* called unconditionally */
2820 	.vblank_get_counter = dm_vblank_get_counter,/* called unconditionally */
2821 	.backlight_set_level = NULL, /* never called for DC */
2822 	.backlight_get_level = NULL, /* never called for DC */
2823 	.hpd_sense = NULL,/* called unconditionally */
2824 	.hpd_set_polarity = NULL, /* called unconditionally */
2825 	.hpd_get_gpio_reg = NULL, /* VBIOS parsing. DAL does it. */
2826 	.page_flip_get_scanoutpos =
2827 		dm_crtc_get_scanoutpos,/* called unconditionally */
2828 	.add_encoder = NULL, /* VBIOS parsing. DAL does it. */
2829 	.add_connector = NULL, /* VBIOS parsing. DAL does it. */
2830 };
2831 
2832 #if defined(CONFIG_DEBUG_KERNEL_DC)
2833 
2834 static ssize_t s3_debug_store(struct device *device,
2835 			      struct device_attribute *attr,
2836 			      const char *buf,
2837 			      size_t count)
2838 {
2839 	int ret;
2840 	int s3_state;
2841 	struct drm_device *drm_dev = dev_get_drvdata(device);
2842 	struct amdgpu_device *adev = drm_to_adev(drm_dev);
2843 	struct amdgpu_ip_block *ip_block;
2844 
2845 	ip_block = amdgpu_device_ip_get_ip_block(adev, AMD_IP_BLOCK_TYPE_DCE);
2846 	if (!ip_block)
2847 		return -EINVAL;
2848 
2849 	ret = kstrtoint(buf, 0, &s3_state);
2850 
2851 	if (ret == 0) {
2852 		if (s3_state) {
2853 			dm_resume(ip_block);
2854 			drm_kms_helper_hotplug_event(adev_to_drm(adev));
2855 		} else
2856 			dm_suspend(ip_block);
2857 	}
2858 
2859 	return ret == 0 ? count : 0;
2860 }
2861 
2862 DEVICE_ATTR_WO(s3_debug);
2863 
2864 #endif
2865 
2866 static int dm_early_init(struct amdgpu_ip_block *ip_block)
2867 {
2868 	struct amdgpu_device *adev = ip_block->adev;
2869 	struct amdgpu_mode_info *mode_info = &adev->mode_info;
2870 	struct atom_context *ctx = mode_info->atom_context;
2871 	int index = GetIndexIntoMasterTable(DATA, Object_Header);
2872 	u16 data_offset;
2873 
2874 	/* if there is no object header, skip DM */
2875 	if (!amdgpu_atom_parse_data_header(ctx, index, NULL, NULL, NULL, &data_offset)) {
2876 		adev->harvest_ip_mask |= AMD_HARVEST_IP_DMU_MASK;
2877 		drm_info(adev_to_drm(adev), "No object header, skipping DM\n");
2878 		return -ENOENT;
2879 	}
2880 
2881 	switch (adev->asic_type) {
2882 #if defined(CONFIG_DRM_AMD_DC_SI)
2883 	case CHIP_TAHITI:
2884 	case CHIP_PITCAIRN:
2885 	case CHIP_VERDE:
2886 		adev->mode_info.num_crtc = 6;
2887 		adev->mode_info.num_hpd = 6;
2888 		adev->mode_info.num_dig = 6;
2889 		break;
2890 	case CHIP_OLAND:
2891 		adev->mode_info.num_crtc = 2;
2892 		adev->mode_info.num_hpd = 2;
2893 		adev->mode_info.num_dig = 2;
2894 		break;
2895 #endif
2896 	case CHIP_BONAIRE:
2897 	case CHIP_HAWAII:
2898 		adev->mode_info.num_crtc = 6;
2899 		adev->mode_info.num_hpd = 6;
2900 		adev->mode_info.num_dig = 6;
2901 		break;
2902 	case CHIP_KAVERI:
2903 		adev->mode_info.num_crtc = 4;
2904 		adev->mode_info.num_hpd = 6;
2905 		adev->mode_info.num_dig = 7;
2906 		break;
2907 	case CHIP_KABINI:
2908 	case CHIP_MULLINS:
2909 		adev->mode_info.num_crtc = 2;
2910 		adev->mode_info.num_hpd = 6;
2911 		adev->mode_info.num_dig = 6;
2912 		break;
2913 	case CHIP_FIJI:
2914 	case CHIP_TONGA:
2915 		adev->mode_info.num_crtc = 6;
2916 		adev->mode_info.num_hpd = 6;
2917 		adev->mode_info.num_dig = 7;
2918 		break;
2919 	case CHIP_CARRIZO:
2920 		adev->mode_info.num_crtc = 3;
2921 		adev->mode_info.num_hpd = 6;
2922 		adev->mode_info.num_dig = 9;
2923 		break;
2924 	case CHIP_STONEY:
2925 		adev->mode_info.num_crtc = 2;
2926 		adev->mode_info.num_hpd = 6;
2927 		adev->mode_info.num_dig = 9;
2928 		break;
2929 	case CHIP_POLARIS11:
2930 	case CHIP_POLARIS12:
2931 		adev->mode_info.num_crtc = 5;
2932 		adev->mode_info.num_hpd = 5;
2933 		adev->mode_info.num_dig = 5;
2934 		break;
2935 	case CHIP_POLARIS10:
2936 	case CHIP_VEGAM:
2937 		adev->mode_info.num_crtc = 6;
2938 		adev->mode_info.num_hpd = 6;
2939 		adev->mode_info.num_dig = 6;
2940 		break;
2941 	case CHIP_VEGA10:
2942 	case CHIP_VEGA12:
2943 	case CHIP_VEGA20:
2944 		adev->mode_info.num_crtc = 6;
2945 		adev->mode_info.num_hpd = 6;
2946 		adev->mode_info.num_dig = 6;
2947 		break;
2948 	default:
2949 
2950 		switch (amdgpu_ip_version(adev, DCE_HWIP, 0)) {
2951 		case IP_VERSION(2, 0, 2):
2952 		case IP_VERSION(3, 0, 0):
2953 			adev->mode_info.num_crtc = 6;
2954 			adev->mode_info.num_hpd = 6;
2955 			adev->mode_info.num_dig = 6;
2956 			break;
2957 		case IP_VERSION(2, 0, 0):
2958 		case IP_VERSION(3, 0, 2):
2959 			adev->mode_info.num_crtc = 5;
2960 			adev->mode_info.num_hpd = 5;
2961 			adev->mode_info.num_dig = 5;
2962 			break;
2963 		case IP_VERSION(2, 0, 3):
2964 		case IP_VERSION(3, 0, 3):
2965 			adev->mode_info.num_crtc = 2;
2966 			adev->mode_info.num_hpd = 2;
2967 			adev->mode_info.num_dig = 2;
2968 			break;
2969 		case IP_VERSION(1, 0, 0):
2970 		case IP_VERSION(1, 0, 1):
2971 		case IP_VERSION(3, 0, 1):
2972 		case IP_VERSION(2, 1, 0):
2973 		case IP_VERSION(3, 1, 2):
2974 		case IP_VERSION(3, 1, 3):
2975 		case IP_VERSION(3, 1, 4):
2976 		case IP_VERSION(3, 1, 5):
2977 		case IP_VERSION(3, 1, 6):
2978 		case IP_VERSION(3, 2, 0):
2979 		case IP_VERSION(3, 2, 1):
2980 		case IP_VERSION(3, 5, 0):
2981 		case IP_VERSION(3, 5, 1):
2982 		case IP_VERSION(3, 6, 0):
2983 		case IP_VERSION(4, 0, 1):
2984 		case IP_VERSION(4, 2, 0):
2985 		case IP_VERSION(4, 2, 1):
2986 			adev->mode_info.num_crtc = 4;
2987 			adev->mode_info.num_hpd = 4;
2988 			adev->mode_info.num_dig = 4;
2989 			break;
2990 		default:
2991 			drm_err(adev_to_drm(adev), "Unsupported DCE IP versions: 0x%x\n",
2992 					amdgpu_ip_version(adev, DCE_HWIP, 0));
2993 			return -EINVAL;
2994 		}
2995 		break;
2996 	}
2997 
2998 	if (adev->mode_info.funcs == NULL)
2999 		adev->mode_info.funcs = &dm_display_funcs;
3000 
3001 	/*
3002 	 * Note: Do NOT change adev->reg.audio_endpt.rreg and
3003 	 * adev->reg.audio_endpt.wreg because they are initialised in
3004 	 * amdgpu_device_init()
3005 	 */
3006 #if defined(CONFIG_DEBUG_KERNEL_DC)
3007 	device_create_file(
3008 		adev_to_drm(adev)->dev,
3009 		&dev_attr_s3_debug);
3010 #endif
3011 	adev->dc_enabled = true;
3012 
3013 	return dm_init_microcode(adev);
3014 }
3015 
3016 STATIC_IFN_KUNIT bool modereset_required(struct drm_crtc_state *crtc_state)
3017 {
3018 	return !crtc_state->active && drm_atomic_crtc_needs_modeset(crtc_state);
3019 }
3020 EXPORT_IF_KUNIT(modereset_required);
3021 
3022 STATIC_IFN_KUNIT int
3023 fill_plane_color_attributes(const struct drm_plane_state *plane_state,
3024 			    const enum surface_pixel_format format,
3025 			    enum dc_color_space *color_space)
3026 {
3027 	bool full_range;
3028 
3029 	*color_space = COLOR_SPACE_SRGB;
3030 
3031 	/* Ignore properties when DRM_CLIENT_CAP_PLANE_COLOR_PIPELINE is set */
3032 	if (plane_state->state && plane_state->state->plane_color_pipeline)
3033 		return 0;
3034 
3035 	/* DRM color properties only affect non-RGB formats. */
3036 	if (format < SURFACE_PIXEL_FORMAT_VIDEO_BEGIN)
3037 		return 0;
3038 
3039 	full_range = (plane_state->color_range == DRM_COLOR_YCBCR_FULL_RANGE);
3040 
3041 	switch (plane_state->color_encoding) {
3042 	case DRM_COLOR_YCBCR_BT601:
3043 		if (full_range)
3044 			*color_space = COLOR_SPACE_YCBCR601;
3045 		else
3046 			*color_space = COLOR_SPACE_YCBCR601_LIMITED;
3047 		break;
3048 
3049 	case DRM_COLOR_YCBCR_BT709:
3050 		if (full_range)
3051 			*color_space = COLOR_SPACE_YCBCR709;
3052 		else
3053 			*color_space = COLOR_SPACE_YCBCR709_LIMITED;
3054 		break;
3055 
3056 	case DRM_COLOR_YCBCR_BT2020:
3057 		if (full_range)
3058 			*color_space = COLOR_SPACE_2020_YCBCR_FULL;
3059 		else
3060 			*color_space = COLOR_SPACE_2020_YCBCR_LIMITED;
3061 		break;
3062 
3063 	default:
3064 		return -EINVAL;
3065 	}
3066 
3067 	return 0;
3068 }
3069 EXPORT_IF_KUNIT(fill_plane_color_attributes);
3070 
3071 static int
3072 fill_dc_plane_info_and_addr(struct amdgpu_device *adev,
3073 			    const struct drm_plane_state *plane_state,
3074 			    const u64 tiling_flags,
3075 			    struct dc_plane_info *plane_info,
3076 			    struct dc_plane_address *address,
3077 			    bool tmz_surface)
3078 {
3079 	const struct drm_framebuffer *fb = plane_state->fb;
3080 	const struct amdgpu_framebuffer *afb =
3081 		to_amdgpu_framebuffer(plane_state->fb);
3082 	int ret;
3083 
3084 	memset(plane_info, 0, sizeof(*plane_info));
3085 
3086 	switch (fb->format->format) {
3087 	case DRM_FORMAT_C8:
3088 		plane_info->format =
3089 			SURFACE_PIXEL_FORMAT_GRPH_PALETA_256_COLORS;
3090 		break;
3091 	case DRM_FORMAT_RGB565:
3092 		plane_info->format = SURFACE_PIXEL_FORMAT_GRPH_RGB565;
3093 		break;
3094 	case DRM_FORMAT_XRGB8888:
3095 	case DRM_FORMAT_ARGB8888:
3096 		plane_info->format = SURFACE_PIXEL_FORMAT_GRPH_ARGB8888;
3097 		break;
3098 	case DRM_FORMAT_XRGB2101010:
3099 	case DRM_FORMAT_ARGB2101010:
3100 		plane_info->format = SURFACE_PIXEL_FORMAT_GRPH_ARGB2101010;
3101 		break;
3102 	case DRM_FORMAT_XBGR2101010:
3103 	case DRM_FORMAT_ABGR2101010:
3104 		plane_info->format = SURFACE_PIXEL_FORMAT_GRPH_ABGR2101010;
3105 		break;
3106 	case DRM_FORMAT_XBGR8888:
3107 	case DRM_FORMAT_ABGR8888:
3108 		plane_info->format = SURFACE_PIXEL_FORMAT_GRPH_ABGR8888;
3109 		break;
3110 	case DRM_FORMAT_NV21:
3111 		plane_info->format = SURFACE_PIXEL_FORMAT_VIDEO_420_YCbCr;
3112 		break;
3113 	case DRM_FORMAT_NV12:
3114 		plane_info->format = SURFACE_PIXEL_FORMAT_VIDEO_420_YCrCb;
3115 		break;
3116 	case DRM_FORMAT_P010:
3117 		plane_info->format = SURFACE_PIXEL_FORMAT_VIDEO_420_10bpc_YCrCb;
3118 		break;
3119 	case DRM_FORMAT_XRGB16161616F:
3120 	case DRM_FORMAT_ARGB16161616F:
3121 		plane_info->format = SURFACE_PIXEL_FORMAT_GRPH_ARGB16161616F;
3122 		break;
3123 	case DRM_FORMAT_XBGR16161616F:
3124 	case DRM_FORMAT_ABGR16161616F:
3125 		plane_info->format = SURFACE_PIXEL_FORMAT_GRPH_ABGR16161616F;
3126 		break;
3127 	case DRM_FORMAT_XRGB16161616:
3128 	case DRM_FORMAT_ARGB16161616:
3129 		plane_info->format = SURFACE_PIXEL_FORMAT_GRPH_ARGB16161616;
3130 		break;
3131 	case DRM_FORMAT_XBGR16161616:
3132 	case DRM_FORMAT_ABGR16161616:
3133 		plane_info->format = SURFACE_PIXEL_FORMAT_GRPH_ABGR16161616;
3134 		break;
3135 	default:
3136 		drm_err(adev_to_drm(adev),
3137 			"Unsupported screen format %p4cc\n",
3138 			&fb->format->format);
3139 		return -EINVAL;
3140 	}
3141 
3142 	switch (plane_state->rotation & DRM_MODE_ROTATE_MASK) {
3143 	case DRM_MODE_ROTATE_0:
3144 		plane_info->rotation = ROTATION_ANGLE_0;
3145 		break;
3146 	case DRM_MODE_ROTATE_90:
3147 		plane_info->rotation = ROTATION_ANGLE_90;
3148 		break;
3149 	case DRM_MODE_ROTATE_180:
3150 		plane_info->rotation = ROTATION_ANGLE_180;
3151 		break;
3152 	case DRM_MODE_ROTATE_270:
3153 		plane_info->rotation = ROTATION_ANGLE_270;
3154 		break;
3155 	default:
3156 		plane_info->rotation = ROTATION_ANGLE_0;
3157 		break;
3158 	}
3159 
3160 
3161 	plane_info->visible = true;
3162 	plane_info->stereo_format = PLANE_STEREO_FORMAT_NONE;
3163 
3164 	plane_info->layer_index = plane_state->normalized_zpos;
3165 
3166 	ret = fill_plane_color_attributes(plane_state, plane_info->format,
3167 					  &plane_info->color_space);
3168 	if (ret)
3169 		return ret;
3170 
3171 	ret = amdgpu_dm_plane_fill_plane_buffer_attributes(adev, afb, plane_info->format,
3172 					   plane_info->rotation, tiling_flags,
3173 					   &plane_info->tiling_info,
3174 					   &plane_info->plane_size,
3175 					   &plane_info->dcc, address,
3176 					   tmz_surface);
3177 	if (ret)
3178 		return ret;
3179 
3180 	amdgpu_dm_plane_fill_blending_from_plane_state(
3181 		plane_state, &plane_info->per_pixel_alpha, &plane_info->pre_multiplied_alpha,
3182 		&plane_info->global_alpha, &plane_info->global_alpha_value);
3183 
3184 	return 0;
3185 }
3186 
3187 static int fill_dc_plane_attributes(struct amdgpu_device *adev,
3188 				    struct dc_plane_state *dc_plane_state,
3189 				    struct drm_plane_state *plane_state,
3190 				    struct drm_crtc_state *crtc_state)
3191 {
3192 	struct dm_crtc_state *dm_crtc_state = to_dm_crtc_state(crtc_state);
3193 	struct amdgpu_framebuffer *afb = (struct amdgpu_framebuffer *)plane_state->fb;
3194 	struct dc_scaling_info scaling_info;
3195 	struct dc_plane_info plane_info;
3196 	int ret;
3197 
3198 	ret = amdgpu_dm_plane_fill_dc_scaling_info(adev, plane_state, &scaling_info);
3199 	if (ret)
3200 		return ret;
3201 
3202 	dc_plane_state->src_rect = scaling_info.src_rect;
3203 	dc_plane_state->dst_rect = scaling_info.dst_rect;
3204 	dc_plane_state->clip_rect = scaling_info.clip_rect;
3205 	dc_plane_state->scaling_quality = scaling_info.scaling_quality;
3206 
3207 	ret = fill_dc_plane_info_and_addr(adev, plane_state,
3208 					  afb->tiling_flags,
3209 					  &plane_info,
3210 					  &dc_plane_state->address,
3211 					  afb->tmz_surface);
3212 	if (ret)
3213 		return ret;
3214 
3215 	dc_plane_state->format = plane_info.format;
3216 	dc_plane_state->color_space = plane_info.color_space;
3217 	dc_plane_state->format = plane_info.format;
3218 	dc_plane_state->plane_size = plane_info.plane_size;
3219 	dc_plane_state->rotation = plane_info.rotation;
3220 	dc_plane_state->horizontal_mirror = plane_info.horizontal_mirror;
3221 	dc_plane_state->stereo_format = plane_info.stereo_format;
3222 	dc_plane_state->tiling_info = plane_info.tiling_info;
3223 	dc_plane_state->visible = plane_info.visible;
3224 	dc_plane_state->per_pixel_alpha = plane_info.per_pixel_alpha;
3225 	dc_plane_state->pre_multiplied_alpha = plane_info.pre_multiplied_alpha;
3226 	dc_plane_state->global_alpha = plane_info.global_alpha;
3227 	dc_plane_state->global_alpha_value = plane_info.global_alpha_value;
3228 	dc_plane_state->dcc = plane_info.dcc;
3229 	dc_plane_state->layer_index = plane_info.layer_index;
3230 	dc_plane_state->flip_int_enabled = true;
3231 
3232 	/*
3233 	 * Always set input transfer function, since plane state is refreshed
3234 	 * every time.
3235 	 */
3236 	ret = amdgpu_dm_update_plane_color_mgmt(dm_crtc_state,
3237 						plane_state,
3238 						dc_plane_state);
3239 	if (ret)
3240 		return ret;
3241 
3242 	return 0;
3243 }
3244 
3245 static inline void fill_dc_dirty_rect(struct drm_plane *plane,
3246 				      struct rect *dirty_rect, int32_t x,
3247 				      s32 y, s32 width, s32 height,
3248 				      int *i, bool ffu)
3249 {
3250 	WARN_ON(*i >= DC_MAX_DIRTY_RECTS);
3251 
3252 	dirty_rect->x = x;
3253 	dirty_rect->y = y;
3254 	dirty_rect->width = width;
3255 	dirty_rect->height = height;
3256 
3257 	if (ffu)
3258 		drm_dbg(plane->dev,
3259 			"[PLANE:%d] PSR FFU dirty rect size (%d, %d)\n",
3260 			plane->base.id, width, height);
3261 	else
3262 		drm_dbg(plane->dev,
3263 			"[PLANE:%d] PSR SU dirty rect at (%d, %d) size (%d, %d)",
3264 			plane->base.id, x, y, width, height);
3265 
3266 	(*i)++;
3267 }
3268 
3269 /**
3270  * fill_dc_dirty_rects() - Fill DC dirty regions for PSR selective updates
3271  *
3272  * @plane: DRM plane containing dirty regions that need to be flushed to the eDP
3273  *         remote fb
3274  * @old_plane_state: Old state of @plane
3275  * @new_plane_state: New state of @plane
3276  * @crtc_state: New state of CRTC connected to the @plane
3277  * @flip_addrs: DC flip tracking struct, which also tracts dirty rects
3278  * @is_psr_su: Flag indicating whether Panel Self Refresh Selective Update (PSR SU) is enabled.
3279  *             If PSR SU is enabled and damage clips are available, only the regions of the screen
3280  *             that have changed will be updated. If PSR SU is not enabled,
3281  *             or if damage clips are not available, the entire screen will be updated.
3282  * @dirty_regions_changed: dirty regions changed
3283  *
3284  * For PSR SU, DC informs the DMUB uController of dirty rectangle regions
3285  * (referred to as "damage clips" in DRM nomenclature) that require updating on
3286  * the eDP remote buffer. The responsibility of specifying the dirty regions is
3287  * amdgpu_dm's.
3288  *
3289  * A damage-aware DRM client should fill the FB_DAMAGE_CLIPS property on the
3290  * plane with regions that require flushing to the eDP remote buffer. In
3291  * addition, certain use cases - such as cursor and multi-plane overlay (MPO) -
3292  * implicitly provide damage clips without any client support via the plane
3293  * bounds.
3294  */
3295 static void fill_dc_dirty_rects(struct drm_plane *plane,
3296 				struct drm_plane_state *old_plane_state,
3297 				struct drm_plane_state *new_plane_state,
3298 				struct drm_crtc_state *crtc_state,
3299 				struct dc_flip_addrs *flip_addrs,
3300 				bool is_psr_su,
3301 				bool *dirty_regions_changed)
3302 {
3303 	struct dm_crtc_state *dm_crtc_state = to_dm_crtc_state(crtc_state);
3304 	struct rect *dirty_rects = flip_addrs->dirty_rects;
3305 	u32 num_clips = 0;
3306 	struct drm_mode_rect *clips = NULL;
3307 	bool bb_changed;
3308 	bool fb_changed;
3309 	u32 i = 0;
3310 	*dirty_regions_changed = false;
3311 
3312 	/*
3313 	 * Cursor plane has it's own dirty rect update interface. See
3314 	 * dcn10_dmub_update_cursor_data and dmub_cmd_update_cursor_info_data
3315 	 */
3316 	if (plane->type == DRM_PLANE_TYPE_CURSOR)
3317 		return;
3318 
3319 	if (new_plane_state->rotation != DRM_MODE_ROTATE_0)
3320 		goto ffu;
3321 
3322 	if (!new_plane_state->ignore_damage_clips) {
3323 		num_clips = drm_plane_get_damage_clips_count(new_plane_state);
3324 		clips = drm_plane_get_damage_clips(new_plane_state);
3325 	}
3326 
3327 	if (num_clips && (!amdgpu_damage_clips || (amdgpu_damage_clips < 0 &&
3328 						   is_psr_su)))
3329 		goto ffu;
3330 
3331 	if (!dm_crtc_state->mpo_requested) {
3332 		if (!num_clips || num_clips > DC_MAX_DIRTY_RECTS)
3333 			goto ffu;
3334 
3335 		for (; flip_addrs->dirty_rect_count < num_clips; clips++)
3336 			fill_dc_dirty_rect(new_plane_state->plane,
3337 					   &dirty_rects[flip_addrs->dirty_rect_count],
3338 					   clips->x1, clips->y1,
3339 					   clips->x2 - clips->x1, clips->y2 - clips->y1,
3340 					   &flip_addrs->dirty_rect_count,
3341 					   false);
3342 		return;
3343 	}
3344 
3345 	/*
3346 	 * MPO is requested. Add entire plane bounding box to dirty rects if
3347 	 * flipped to or damaged.
3348 	 *
3349 	 * If plane is moved or resized, also add old bounding box to dirty
3350 	 * rects.
3351 	 */
3352 	fb_changed = old_plane_state->fb->base.id !=
3353 		     new_plane_state->fb->base.id;
3354 	bb_changed = (old_plane_state->crtc_x != new_plane_state->crtc_x ||
3355 		      old_plane_state->crtc_y != new_plane_state->crtc_y ||
3356 		      old_plane_state->crtc_w != new_plane_state->crtc_w ||
3357 		      old_plane_state->crtc_h != new_plane_state->crtc_h);
3358 
3359 	drm_dbg(plane->dev,
3360 		"[PLANE:%d] PSR bb_changed:%d fb_changed:%d num_clips:%d\n",
3361 		new_plane_state->plane->base.id,
3362 		bb_changed, fb_changed, num_clips);
3363 
3364 	*dirty_regions_changed = bb_changed;
3365 
3366 	if ((num_clips + (bb_changed ? 2 : 0)) > DC_MAX_DIRTY_RECTS)
3367 		goto ffu;
3368 
3369 	if (bb_changed) {
3370 		fill_dc_dirty_rect(new_plane_state->plane, &dirty_rects[i],
3371 				   new_plane_state->crtc_x,
3372 				   new_plane_state->crtc_y,
3373 				   new_plane_state->crtc_w,
3374 				   new_plane_state->crtc_h, &i, false);
3375 
3376 		/* Add old plane bounding-box if plane is moved or resized */
3377 		fill_dc_dirty_rect(new_plane_state->plane, &dirty_rects[i],
3378 				   old_plane_state->crtc_x,
3379 				   old_plane_state->crtc_y,
3380 				   old_plane_state->crtc_w,
3381 				   old_plane_state->crtc_h, &i, false);
3382 	}
3383 
3384 	if (num_clips) {
3385 		for (; i < num_clips; clips++)
3386 			fill_dc_dirty_rect(new_plane_state->plane,
3387 					   &dirty_rects[i], clips->x1,
3388 					   clips->y1, clips->x2 - clips->x1,
3389 					   clips->y2 - clips->y1, &i, false);
3390 	} else if (fb_changed && !bb_changed) {
3391 		fill_dc_dirty_rect(new_plane_state->plane, &dirty_rects[i],
3392 				   new_plane_state->crtc_x,
3393 				   new_plane_state->crtc_y,
3394 				   new_plane_state->crtc_w,
3395 				   new_plane_state->crtc_h, &i, false);
3396 	}
3397 
3398 	flip_addrs->dirty_rect_count = i;
3399 	return;
3400 
3401 ffu:
3402 	fill_dc_dirty_rect(new_plane_state->plane, &dirty_rects[0], 0, 0,
3403 			   dm_crtc_state->base.mode.crtc_hdisplay,
3404 			   dm_crtc_state->base.mode.crtc_vdisplay,
3405 			   &flip_addrs->dirty_rect_count, true);
3406 }
3407 
3408 void amdgpu_dm_update_stream_scaling_settings(struct drm_device *dev,
3409 					   const struct drm_display_mode *mode,
3410 					   const struct dm_connector_state *dm_state,
3411 					   struct dc_stream_state *stream)
3412 {
3413 	enum amdgpu_rmx_type rmx_type;
3414 
3415 	struct rect src = { 0 }; /* viewport in composition space*/
3416 	struct rect dst = { 0 }; /* stream addressable area */
3417 
3418 	/* no mode. nothing to be done */
3419 	if (!mode)
3420 		return;
3421 
3422 	/* Full screen scaling by default */
3423 	src.width = mode->hdisplay;
3424 	src.height = mode->vdisplay;
3425 	dst.width = stream->timing.h_addressable;
3426 	dst.height = stream->timing.v_addressable;
3427 
3428 	if (dm_state) {
3429 		rmx_type = dm_state->scaling;
3430 		if (rmx_type == RMX_ASPECT || rmx_type == RMX_OFF) {
3431 			if (src.width * dst.height <
3432 					src.height * dst.width) {
3433 				/* height needs less upscaling/more downscaling */
3434 				dst.width = src.width *
3435 						dst.height / src.height;
3436 			} else {
3437 				/* width needs less upscaling/more downscaling */
3438 				dst.height = src.height *
3439 						dst.width / src.width;
3440 			}
3441 		} else if (rmx_type == RMX_CENTER) {
3442 			dst = src;
3443 		}
3444 
3445 		dst.x = (stream->timing.h_addressable - dst.width) / 2;
3446 		dst.y = (stream->timing.v_addressable - dst.height) / 2;
3447 
3448 		if (dm_state->underscan_enable) {
3449 			dst.x += dm_state->underscan_hborder / 2;
3450 			dst.y += dm_state->underscan_vborder / 2;
3451 			dst.width -= dm_state->underscan_hborder;
3452 			dst.height -= dm_state->underscan_vborder;
3453 		}
3454 	}
3455 
3456 	stream->src = src;
3457 	stream->dst = dst;
3458 
3459 	drm_dbg_kms(dev, "Destination Rectangle x:%d  y:%d  width:%d  height:%d\n",
3460 		    dst.x, dst.y, dst.width, dst.height);
3461 
3462 }
3463 EXPORT_IF_KUNIT(amdgpu_dm_update_stream_scaling_settings);
3464 
3465 static int dm_update_mst_vcpi_slots_for_dsc(struct drm_atomic_commit *state,
3466 					    struct dc_state *dc_state,
3467 					    struct dsc_mst_fairness_vars *vars)
3468 {
3469 	struct dc_stream_state *stream = NULL;
3470 	struct drm_connector *connector;
3471 	struct drm_connector_state *new_con_state;
3472 	struct amdgpu_dm_connector *aconnector;
3473 	struct dm_connector_state *dm_conn_state;
3474 	int i, j, ret;
3475 	int vcpi, pbn_div, pbn = 0, slot_num = 0;
3476 
3477 	for_each_new_connector_in_state(state, connector, new_con_state, i) {
3478 
3479 		if (connector->connector_type == DRM_MODE_CONNECTOR_WRITEBACK)
3480 			continue;
3481 
3482 		aconnector = to_amdgpu_dm_connector(connector);
3483 
3484 		if (!aconnector->mst_output_port)
3485 			continue;
3486 
3487 		if (!new_con_state || !new_con_state->crtc)
3488 			continue;
3489 
3490 		dm_conn_state = to_dm_connector_state(new_con_state);
3491 
3492 		for (j = 0; j < dc_state->stream_count; j++) {
3493 			stream = dc_state->streams[j];
3494 			if (!stream)
3495 				continue;
3496 
3497 			if ((struct amdgpu_dm_connector *)stream->dm_stream_context == aconnector)
3498 				break;
3499 
3500 			stream = NULL;
3501 		}
3502 
3503 		if (!stream)
3504 			continue;
3505 
3506 		pbn_div = dm_mst_get_pbn_divider(stream->link);
3507 		/* pbn is calculated by compute_mst_dsc_configs_for_state*/
3508 		for (j = 0; j < dc_state->stream_count; j++) {
3509 			if (vars[j].aconnector == aconnector) {
3510 				pbn = vars[j].pbn;
3511 				break;
3512 			}
3513 		}
3514 
3515 		if (j == dc_state->stream_count || pbn_div == 0)
3516 			continue;
3517 
3518 		slot_num = DIV_ROUND_UP(pbn, pbn_div);
3519 
3520 		if (stream->timing.flags.DSC != 1) {
3521 			dm_conn_state->pbn = pbn;
3522 			dm_conn_state->vcpi_slots = slot_num;
3523 
3524 			ret = drm_dp_mst_atomic_enable_dsc(state, aconnector->mst_output_port,
3525 							   dm_conn_state->pbn, false);
3526 			if (ret < 0)
3527 				return ret;
3528 
3529 			continue;
3530 		}
3531 
3532 		vcpi = drm_dp_mst_atomic_enable_dsc(state, aconnector->mst_output_port, pbn, true);
3533 		if (vcpi < 0)
3534 			return vcpi;
3535 
3536 		dm_conn_state->pbn = pbn;
3537 		dm_conn_state->vcpi_slots = vcpi;
3538 	}
3539 	return 0;
3540 }
3541 
3542 static void manage_dm_interrupts(struct amdgpu_device *adev,
3543 				 struct amdgpu_crtc *acrtc,
3544 				 struct dm_crtc_state *acrtc_state)
3545 {	/*
3546 	 * We cannot be sure that the frontend index maps to the same
3547 	 * backend index - some even map to more than one.
3548 	 * So we have to go through the CRTC to find the right IRQ.
3549 	 */
3550 	int irq_type = amdgpu_display_crtc_idx_to_irq_type(
3551 			adev,
3552 			acrtc->crtc_id);
3553 	struct drm_device *dev = adev_to_drm(adev);
3554 
3555 	struct drm_vblank_crtc_config config = {0};
3556 	struct dc_crtc_timing *timing;
3557 	int offdelay;
3558 
3559 	if (acrtc_state) {
3560 		timing = &acrtc_state->stream->timing;
3561 
3562 		if (amdgpu_ip_version(adev, DCE_HWIP, 0) <
3563 			   IP_VERSION(3, 5, 0) ||
3564 			   !(adev->flags & AMD_IS_APU)) {
3565 			/*
3566 			 * Older HW and DGPU have issues with instant off;
3567 			 * use a 2 frame offdelay.
3568 			 */
3569 			offdelay = DIV64_U64_ROUND_UP((u64)20 *
3570 						      timing->v_total *
3571 						      timing->h_total,
3572 						      timing->pix_clk_100hz);
3573 
3574 			config.offdelay_ms = offdelay ?: 30;
3575 		} else {
3576 			/* offdelay_ms = 0 will never disable vblank */
3577 			config.offdelay_ms = 1;
3578 			config.disable_immediate = true;
3579 		}
3580 
3581 		drm_crtc_vblank_on_config(&acrtc->base,
3582 					  &config);
3583 		/*
3584 		 * Since pflip_high_irq is no longer registered for DCN, grab an
3585 		 * extra reference to vupdate irq instead to workaround this
3586 		 * issue:
3587 		 * https://gitlab.freedesktop.org/drm/amd/-/work_items/3936
3588 		 *
3589 		 * The callbacks to drm_vblank_on/off should really take care of
3590 		 * this though.
3591 		 */
3592 		switch (amdgpu_ip_version(adev, DCE_HWIP, 0)) {
3593 		case IP_VERSION(3, 0, 0):
3594 		case IP_VERSION(3, 0, 2):
3595 		case IP_VERSION(3, 0, 3):
3596 		case IP_VERSION(3, 2, 0):
3597 			if (amdgpu_irq_get(adev, &adev->vupdate_irq, irq_type))
3598 				drm_err(dev, "DM_IRQ: Cannot get vupdate irq!\n");
3599 #if defined(CONFIG_DRM_AMD_SECURE_DISPLAY)
3600 			if (amdgpu_irq_get(adev, &adev->vline0_irq, irq_type))
3601 				drm_err(dev, "DM_IRQ: Cannot get vline0 irq!\n");
3602 #endif
3603 		}
3604 
3605 	} else {
3606 		/* Allow RX6xxx, RX7700, RX7800 GPUs to call amdgpu_irq_put.*/
3607 		switch (amdgpu_ip_version(adev, DCE_HWIP, 0)) {
3608 		case IP_VERSION(3, 0, 0):
3609 		case IP_VERSION(3, 0, 2):
3610 		case IP_VERSION(3, 0, 3):
3611 		case IP_VERSION(3, 2, 0):
3612 #if defined(CONFIG_DRM_AMD_SECURE_DISPLAY)
3613 			if (amdgpu_irq_put(adev, &adev->vline0_irq, irq_type))
3614 				drm_err(dev, "DM_IRQ: Cannot put vline0 irq!\n");
3615 #endif
3616 			if (amdgpu_irq_put(adev, &adev->vupdate_irq, irq_type))
3617 				drm_err(dev, "DM_IRQ: Cannot put vupdate irq!\n");
3618 		}
3619 
3620 		drm_crtc_vblank_off(&acrtc->base);
3621 	}
3622 }
3623 
3624 static void dm_update_pflip_irq_state(struct amdgpu_device *adev,
3625 				      struct amdgpu_crtc *acrtc)
3626 {
3627 	int irq_type =
3628 		amdgpu_display_crtc_idx_to_irq_type(adev, acrtc->crtc_id);
3629 
3630 	/* GRPH_PFLIP is not used on DCN; nothing to reapply. */
3631 	if (amdgpu_ip_version(adev, DCE_HWIP, 0) != 0)
3632 		return;
3633 
3634 	/**
3635 	 * This reads the current state for the IRQ and force reapplies
3636 	 * the setting to hardware.
3637 	 */
3638 	amdgpu_irq_update(adev, &adev->pageflip_irq, irq_type);
3639 }
3640 
3641 STATIC_IFN_KUNIT bool
3642 is_scaling_state_different(const struct dm_connector_state *dm_state,
3643 			   const struct dm_connector_state *old_dm_state)
3644 {
3645 	if (dm_state->scaling != old_dm_state->scaling)
3646 		return true;
3647 	if (!dm_state->underscan_enable && old_dm_state->underscan_enable) {
3648 		if (old_dm_state->underscan_hborder != 0 && old_dm_state->underscan_vborder != 0)
3649 			return true;
3650 	} else  if (dm_state->underscan_enable && !old_dm_state->underscan_enable) {
3651 		if (dm_state->underscan_hborder != 0 && dm_state->underscan_vborder != 0)
3652 			return true;
3653 	} else if (dm_state->underscan_hborder != old_dm_state->underscan_hborder ||
3654 		   dm_state->underscan_vborder != old_dm_state->underscan_vborder)
3655 		return true;
3656 	return false;
3657 }
3658 EXPORT_IF_KUNIT(is_scaling_state_different);
3659 
3660 STATIC_IFN_KUNIT bool
3661 is_content_protection_different(struct drm_crtc_state *new_crtc_state,
3662 				struct drm_crtc_state *old_crtc_state,
3663 				struct drm_connector_state *new_conn_state,
3664 				struct drm_connector_state *old_conn_state,
3665 				const struct drm_connector *connector,
3666 				struct hdcp_workqueue *hdcp_w)
3667 {
3668 	struct amdgpu_dm_connector *aconnector = to_amdgpu_dm_connector(connector);
3669 	struct dm_connector_state *dm_con_state = to_dm_connector_state(connector->state);
3670 
3671 	pr_debug("[HDCP_DM] connector->index: %x connect_status: %x dpms: %x\n",
3672 		connector->index, connector->status, connector->dpms);
3673 	pr_debug("[HDCP_DM] state protection old: %x new: %x\n",
3674 		old_conn_state->content_protection, new_conn_state->content_protection);
3675 
3676 	if (old_crtc_state)
3677 		pr_debug("[HDCP_DM] old crtc en: %x a: %x m: %x a-chg: %x c-chg: %x\n",
3678 		old_crtc_state->enable,
3679 		old_crtc_state->active,
3680 		old_crtc_state->mode_changed,
3681 		old_crtc_state->active_changed,
3682 		old_crtc_state->connectors_changed);
3683 
3684 	if (new_crtc_state)
3685 		pr_debug("[HDCP_DM] NEW crtc en: %x a: %x m: %x a-chg: %x c-chg: %x\n",
3686 		new_crtc_state->enable,
3687 		new_crtc_state->active,
3688 		new_crtc_state->mode_changed,
3689 		new_crtc_state->active_changed,
3690 		new_crtc_state->connectors_changed);
3691 
3692 	/* hdcp content type change */
3693 	if (old_conn_state->hdcp_content_type != new_conn_state->hdcp_content_type &&
3694 	    new_conn_state->content_protection != DRM_MODE_CONTENT_PROTECTION_UNDESIRED) {
3695 		new_conn_state->content_protection = DRM_MODE_CONTENT_PROTECTION_DESIRED;
3696 		pr_debug("[HDCP_DM] Type0/1 change %s :true\n", __func__);
3697 		return true;
3698 	}
3699 
3700 	/* CP is being re enabled, ignore this */
3701 	if (old_conn_state->content_protection == DRM_MODE_CONTENT_PROTECTION_ENABLED &&
3702 	    new_conn_state->content_protection == DRM_MODE_CONTENT_PROTECTION_DESIRED) {
3703 		if (new_crtc_state && new_crtc_state->mode_changed) {
3704 			new_conn_state->content_protection = DRM_MODE_CONTENT_PROTECTION_DESIRED;
3705 			pr_debug("[HDCP_DM] ENABLED->DESIRED & mode_changed %s :true\n", __func__);
3706 			return true;
3707 		}
3708 		new_conn_state->content_protection = DRM_MODE_CONTENT_PROTECTION_ENABLED;
3709 		pr_debug("[HDCP_DM] ENABLED -> DESIRED %s :false\n", __func__);
3710 		return false;
3711 	}
3712 
3713 	/* S3 resume case, since old state will always be 0 (UNDESIRED) and the restored state will be ENABLED
3714 	 *
3715 	 * Handles:	UNDESIRED -> ENABLED
3716 	 */
3717 	if (old_conn_state->content_protection == DRM_MODE_CONTENT_PROTECTION_UNDESIRED &&
3718 	    new_conn_state->content_protection == DRM_MODE_CONTENT_PROTECTION_ENABLED)
3719 		new_conn_state->content_protection = DRM_MODE_CONTENT_PROTECTION_DESIRED;
3720 
3721 	/* Stream removed and re-enabled
3722 	 *
3723 	 * Can sometimes overlap with the HPD case,
3724 	 * thus set update_hdcp to false to avoid
3725 	 * setting HDCP multiple times.
3726 	 *
3727 	 * Handles:	DESIRED -> DESIRED (Special case)
3728 	 */
3729 	if (!(old_conn_state->crtc && old_conn_state->crtc->enabled) &&
3730 		new_conn_state->crtc && new_conn_state->crtc->enabled &&
3731 		connector->state->content_protection == DRM_MODE_CONTENT_PROTECTION_DESIRED) {
3732 		dm_con_state->update_hdcp = false;
3733 		pr_debug("[HDCP_DM] DESIRED->DESIRED (Stream removed and re-enabled) %s :true\n",
3734 			__func__);
3735 		return true;
3736 	}
3737 
3738 	/* Hot-plug, headless s3, dpms
3739 	 *
3740 	 * Only start HDCP if the display is connected/enabled.
3741 	 * update_hdcp flag will be set to false until the next
3742 	 * HPD comes in.
3743 	 *
3744 	 * Handles:	DESIRED -> DESIRED (Special case)
3745 	 */
3746 	if (dm_con_state->update_hdcp &&
3747 	new_conn_state->content_protection == DRM_MODE_CONTENT_PROTECTION_DESIRED &&
3748 	connector->dpms == DRM_MODE_DPMS_ON && aconnector->dc_sink != NULL) {
3749 		dm_con_state->update_hdcp = false;
3750 		pr_debug("[HDCP_DM] DESIRED->DESIRED (Hot-plug, headless s3, dpms) %s :true\n",
3751 			__func__);
3752 		return true;
3753 	}
3754 
3755 	if (old_conn_state->content_protection == new_conn_state->content_protection) {
3756 		if (new_conn_state->content_protection >= DRM_MODE_CONTENT_PROTECTION_DESIRED) {
3757 			if (new_crtc_state && new_crtc_state->mode_changed) {
3758 				pr_debug("[HDCP_DM] DESIRED->DESIRED or ENABLE->ENABLE mode_change %s :true\n",
3759 					__func__);
3760 				return true;
3761 			}
3762 			pr_debug("[HDCP_DM] DESIRED->DESIRED & ENABLE->ENABLE %s :false\n",
3763 				__func__);
3764 			return false;
3765 		}
3766 
3767 		pr_debug("[HDCP_DM] UNDESIRED->UNDESIRED %s :false\n", __func__);
3768 		return false;
3769 	}
3770 
3771 	if (new_conn_state->content_protection != DRM_MODE_CONTENT_PROTECTION_ENABLED) {
3772 		pr_debug("[HDCP_DM] UNDESIRED->DESIRED or DESIRED->UNDESIRED or ENABLED->UNDESIRED %s :true\n",
3773 			__func__);
3774 		return true;
3775 	}
3776 
3777 	pr_debug("[HDCP_DM] DESIRED->ENABLED %s :false\n", __func__);
3778 	return false;
3779 }
3780 EXPORT_IF_KUNIT(is_content_protection_different);
3781 
3782 static void remove_stream(struct amdgpu_device *adev,
3783 			  struct amdgpu_crtc *acrtc,
3784 			  struct dc_stream_state *stream)
3785 {
3786 	/* this is the update mode case */
3787 
3788 	acrtc->otg_inst = -1;
3789 	acrtc->enabled = false;
3790 }
3791 
3792 static void prepare_flip_isr(struct amdgpu_crtc *acrtc)
3793 {
3794 
3795 	assert_spin_locked(&acrtc->base.dev->event_lock);
3796 	WARN_ON(acrtc->event);
3797 
3798 	acrtc->event = acrtc->base.state->event;
3799 
3800 	/* Set the flip status */
3801 	acrtc->pflip_status = AMDGPU_FLIP_SUBMITTED;
3802 
3803 	/* Mark this event as consumed */
3804 	acrtc->base.state->event = NULL;
3805 
3806 	drm_dbg_state(acrtc->base.dev,
3807 		      "crtc:%d, pflip_stat:AMDGPU_FLIP_SUBMITTED\n",
3808 		      acrtc->crtc_id);
3809 }
3810 
3811 static void update_freesync_state_on_stream(
3812 	struct amdgpu_display_manager *dm,
3813 	struct dm_crtc_state *new_crtc_state,
3814 	struct dc_stream_state *new_stream,
3815 	struct dc_plane_state *surface,
3816 	u32 flip_timestamp_in_us)
3817 {
3818 	struct mod_vrr_params vrr_params;
3819 	struct dc_info_packet vrr_infopacket = {0};
3820 	struct amdgpu_device *adev = dm->adev;
3821 	struct amdgpu_crtc *acrtc = to_amdgpu_crtc(new_crtc_state->base.crtc);
3822 	unsigned long flags;
3823 	bool pack_sdp_v1_3 = false;
3824 	struct amdgpu_dm_connector *aconn;
3825 	enum vrr_packet_type packet_type = PACKET_TYPE_VRR;
3826 
3827 	if (!new_stream)
3828 		return;
3829 
3830 	/*
3831 	 * TODO: Determine why min/max totals and vrefresh can be 0 here.
3832 	 * For now it's sufficient to just guard against these conditions.
3833 	 */
3834 
3835 	if (!new_stream->timing.h_total || !new_stream->timing.v_total)
3836 		return;
3837 
3838 	spin_lock_irqsave(&adev_to_drm(adev)->event_lock, flags);
3839 	vrr_params = acrtc->dm_irq_params.vrr_params;
3840 
3841 	if (surface) {
3842 		mod_freesync_handle_preflip(
3843 			dm->freesync_module,
3844 			surface,
3845 			new_stream,
3846 			flip_timestamp_in_us,
3847 			&vrr_params);
3848 
3849 		if (adev->family < AMDGPU_FAMILY_AI &&
3850 		    amdgpu_dm_crtc_vrr_active(new_crtc_state)) {
3851 			mod_freesync_handle_v_update(dm->freesync_module,
3852 						     new_stream, &vrr_params);
3853 
3854 			/* Need to call this before the frame ends. */
3855 			dc_stream_adjust_vmin_vmax(dm->dc,
3856 						   new_crtc_state->stream,
3857 						   &vrr_params.adjust);
3858 		}
3859 	}
3860 
3861 	aconn = (struct amdgpu_dm_connector *)new_stream->dm_stream_context;
3862 
3863 	if (aconn && (aconn->as_type == FREESYNC_TYPE_PCON_IN_WHITELIST || aconn->vsdb_info.replay_mode)) {
3864 		pack_sdp_v1_3 = aconn->pack_sdp_v1_3;
3865 
3866 		if (aconn->vsdb_info.amd_vsdb_version == 1)
3867 			packet_type = PACKET_TYPE_FS_V1;
3868 		else if (aconn->vsdb_info.amd_vsdb_version == 2)
3869 			packet_type = PACKET_TYPE_FS_V2;
3870 		else if (aconn->vsdb_info.amd_vsdb_version == 3)
3871 			packet_type = PACKET_TYPE_FS_V3;
3872 
3873 		mod_build_adaptive_sync_infopacket(new_stream, aconn->as_type, NULL,
3874 					&new_stream->adaptive_sync_infopacket);
3875 	}
3876 
3877 	mod_freesync_build_vrr_infopacket(
3878 		dm->freesync_module,
3879 		new_stream,
3880 		&vrr_params,
3881 		packet_type,
3882 		TRANSFER_FUNC_UNKNOWN,
3883 		&vrr_infopacket,
3884 		pack_sdp_v1_3);
3885 
3886 	new_crtc_state->freesync_vrr_info_changed |=
3887 		(memcmp(&new_crtc_state->vrr_infopacket,
3888 			&vrr_infopacket,
3889 			sizeof(vrr_infopacket)) != 0);
3890 
3891 	acrtc->dm_irq_params.vrr_params = vrr_params;
3892 	new_crtc_state->vrr_infopacket = vrr_infopacket;
3893 
3894 	new_stream->vrr_infopacket = vrr_infopacket;
3895 	new_stream->allow_freesync = mod_freesync_get_freesync_enabled(&vrr_params);
3896 
3897 	if (new_crtc_state->freesync_vrr_info_changed)
3898 		drm_dbg_kms(adev_to_drm(adev), "VRR packet update: crtc=%u enabled=%d state=%d",
3899 			      new_crtc_state->base.crtc->base.id,
3900 			      (int)new_crtc_state->base.vrr_enabled,
3901 			      (int)vrr_params.state);
3902 
3903 	spin_unlock_irqrestore(&adev_to_drm(adev)->event_lock, flags);
3904 }
3905 
3906 static void update_stream_irq_parameters(
3907 	struct amdgpu_display_manager *dm,
3908 	struct dm_crtc_state *new_crtc_state)
3909 {
3910 	struct dc_stream_state *new_stream = new_crtc_state->stream;
3911 	struct mod_vrr_params vrr_params;
3912 	struct mod_freesync_config config = new_crtc_state->freesync_config;
3913 	struct amdgpu_device *adev = dm->adev;
3914 	struct amdgpu_crtc *acrtc = to_amdgpu_crtc(new_crtc_state->base.crtc);
3915 	unsigned long flags;
3916 
3917 	if (!new_stream)
3918 		return;
3919 
3920 	/*
3921 	 * TODO: Determine why min/max totals and vrefresh can be 0 here.
3922 	 * For now it's sufficient to just guard against these conditions.
3923 	 */
3924 	if (!new_stream->timing.h_total || !new_stream->timing.v_total)
3925 		return;
3926 
3927 	spin_lock_irqsave(&adev_to_drm(adev)->event_lock, flags);
3928 	vrr_params = acrtc->dm_irq_params.vrr_params;
3929 
3930 	if (new_crtc_state->vrr_supported &&
3931 	    config.min_refresh_in_uhz &&
3932 	    config.max_refresh_in_uhz) {
3933 		/*
3934 		 * if freesync compatible mode was set, config.state will be set
3935 		 * in atomic check
3936 		 */
3937 		if (config.state == VRR_STATE_ACTIVE_FIXED && config.fixed_refresh_in_uhz &&
3938 		    (!drm_atomic_crtc_needs_modeset(&new_crtc_state->base) ||
3939 		     new_crtc_state->freesync_config.state == VRR_STATE_ACTIVE_FIXED)) {
3940 			vrr_params.max_refresh_in_uhz = config.max_refresh_in_uhz;
3941 			vrr_params.min_refresh_in_uhz = config.min_refresh_in_uhz;
3942 			vrr_params.fixed_refresh_in_uhz = config.fixed_refresh_in_uhz;
3943 			vrr_params.state = VRR_STATE_ACTIVE_FIXED;
3944 		} else {
3945 			config.state = new_crtc_state->base.vrr_enabled ?
3946 						     VRR_STATE_ACTIVE_VARIABLE :
3947 						     VRR_STATE_INACTIVE;
3948 		}
3949 	} else {
3950 		config.state = VRR_STATE_UNSUPPORTED;
3951 	}
3952 
3953 	mod_freesync_build_vrr_params(dm->freesync_module,
3954 				      new_stream,
3955 				      &config, &vrr_params);
3956 
3957 	new_crtc_state->freesync_config = config;
3958 	/* Copy state for access from DM IRQ handler */
3959 	acrtc->dm_irq_params.freesync_config = config;
3960 	acrtc->dm_irq_params.active_planes = new_crtc_state->active_planes;
3961 	acrtc->dm_irq_params.vrr_params = vrr_params;
3962 	spin_unlock_irqrestore(&adev_to_drm(adev)->event_lock, flags);
3963 }
3964 
3965 static void amdgpu_dm_handle_vrr_transition(struct amdgpu_display_manager *dm,
3966 					    struct dm_crtc_state *old_state,
3967 					    struct dm_crtc_state *new_state)
3968 {
3969 	struct amdgpu_device *adev = dm->adev;
3970 	bool old_vrr_active = amdgpu_dm_crtc_vrr_active(old_state);
3971 	bool new_vrr_active = amdgpu_dm_crtc_vrr_active(new_state);
3972 
3973 	/* Only DCE gates vupdate on VRR, keep it enabled for DCN */
3974 	bool vrr_gates_vupdate = amdgpu_ip_version(adev, DCE_HWIP, 0) == 0;
3975 
3976 	if (!old_vrr_active && new_vrr_active) {
3977 		/* Transition VRR inactive -> active:
3978 		 * While VRR is active, we must not disable vblank irq, as a
3979 		 * reenable after disable would compute bogus vblank/pflip
3980 		 * timestamps if it likely happened inside display front-porch.
3981 		 *
3982 		 * We also need vupdate irq for the actual core vblank handling
3983 		 * at end of vblank.
3984 		 */
3985 		if (vrr_gates_vupdate)
3986 			WARN_ON(amdgpu_dm_crtc_set_vupdate_irq(new_state->base.crtc, true) != 0);
3987 		WARN_ON(drm_crtc_vblank_get(new_state->base.crtc) != 0);
3988 		drm_dbg_driver(new_state->base.crtc->dev, "%s: crtc=%u VRR off->on: Get vblank ref\n",
3989 				 __func__, new_state->base.crtc->base.id);
3990 
3991 		scoped_guard(mutex, &dm->dc_lock) {
3992 			dc_exit_ips_for_hw_access(dm->dc);
3993 			amdgpu_dm_psr_set_event(dm, new_state->stream, true,
3994 				psr_event_vrr_transition, true);
3995 			amdgpu_dm_replay_set_event(dm, new_state->stream, true,
3996 				replay_event_vrr, true);
3997 		}
3998 	} else if (old_vrr_active && !new_vrr_active) {
3999 		/* Transition VRR active -> inactive:
4000 		 * Allow vblank irq disable again for fixed refresh rate.
4001 		 */
4002 		if (vrr_gates_vupdate)
4003 			WARN_ON(amdgpu_dm_crtc_set_vupdate_irq(new_state->base.crtc, false) != 0);
4004 		drm_crtc_vblank_put(new_state->base.crtc);
4005 		drm_dbg_driver(new_state->base.crtc->dev, "%s: crtc=%u VRR on->off: Drop vblank ref\n",
4006 				 __func__, new_state->base.crtc->base.id);
4007 
4008 		scoped_guard(mutex, &dm->dc_lock) {
4009 			dc_exit_ips_for_hw_access(dm->dc);
4010 			amdgpu_dm_psr_set_event(dm, new_state->stream, false,
4011 				psr_event_vrr_transition, false);
4012 			amdgpu_dm_replay_set_event(dm, new_state->stream, false,
4013 				replay_event_vrr, false);
4014 		}
4015 	}
4016 }
4017 
4018 static void amdgpu_dm_commit_cursors(struct drm_atomic_commit *state)
4019 {
4020 	struct drm_plane *plane;
4021 	struct drm_plane_state *old_plane_state;
4022 	int i;
4023 
4024 	/*
4025 	 * TODO: Make this per-stream so we don't issue redundant updates for
4026 	 * commits with multiple streams.
4027 	 */
4028 	for_each_old_plane_in_state(state, plane, old_plane_state, i)
4029 		if (plane->type == DRM_PLANE_TYPE_CURSOR)
4030 			amdgpu_dm_plane_handle_cursor_update(plane, old_plane_state);
4031 }
4032 
4033 static inline uint32_t get_mem_type(struct drm_framebuffer *fb)
4034 {
4035 	struct amdgpu_bo *abo = gem_to_amdgpu_bo(fb->obj[0]);
4036 
4037 	return abo->tbo.resource ? abo->tbo.resource->mem_type : 0;
4038 }
4039 
4040 static void amdgpu_dm_update_cursor(struct drm_plane *plane,
4041 				    struct drm_plane_state *old_plane_state,
4042 				    struct dc_stream_update *update)
4043 {
4044 	struct amdgpu_device *adev = drm_to_adev(plane->dev);
4045 	struct amdgpu_framebuffer *afb = to_amdgpu_framebuffer(plane->state->fb);
4046 	struct drm_crtc *crtc = afb ? plane->state->crtc : old_plane_state->crtc;
4047 	struct dm_crtc_state *crtc_state = crtc ? to_dm_crtc_state(crtc->state) : NULL;
4048 	struct amdgpu_crtc *amdgpu_crtc = to_amdgpu_crtc(crtc);
4049 	uint64_t address = afb ? afb->address : 0;
4050 	struct dc_cursor_position position = {0};
4051 	struct dc_cursor_attributes attributes;
4052 	int ret;
4053 
4054 	if (!plane->state->fb && !old_plane_state->fb)
4055 		return;
4056 
4057 	drm_dbg_atomic(plane->dev, "crtc_id=%d with size %d to %d\n",
4058 		       amdgpu_crtc->crtc_id, plane->state->crtc_w,
4059 		       plane->state->crtc_h);
4060 
4061 	ret = amdgpu_dm_plane_get_cursor_position(plane, crtc, &position);
4062 	if (ret)
4063 		return;
4064 
4065 	if (!position.enable) {
4066 		/* turn off cursor */
4067 		if (crtc_state && crtc_state->stream) {
4068 			dc_stream_set_cursor_position(crtc_state->stream,
4069 						      &position);
4070 			update->cursor_position = &crtc_state->stream->cursor_position;
4071 		}
4072 		return;
4073 	}
4074 
4075 	amdgpu_crtc->cursor_width = plane->state->crtc_w;
4076 	amdgpu_crtc->cursor_height = plane->state->crtc_h;
4077 
4078 	memset(&attributes, 0, sizeof(attributes));
4079 	attributes.address.high_part = upper_32_bits(address);
4080 	attributes.address.low_part  = lower_32_bits(address);
4081 	attributes.width             = plane->state->crtc_w;
4082 	attributes.height            = plane->state->crtc_h;
4083 	attributes.color_format      = CURSOR_MODE_COLOR_PRE_MULTIPLIED_ALPHA;
4084 	attributes.rotation_angle    = 0;
4085 	attributes.attribute_flags.value = 0;
4086 
4087 	/* Enable cursor degamma ROM on DCN3+ for implicit sRGB degamma in DRM
4088 	 * legacy gamma setup.
4089 	 */
4090 	if (crtc_state->cm_is_degamma_srgb &&
4091 	    adev->dm.dc->caps.color.dpp.gamma_corr)
4092 		attributes.attribute_flags.bits.ENABLE_CURSOR_DEGAMMA = 1;
4093 
4094 	if (afb)
4095 		attributes.pitch = afb->base.pitches[0] / afb->base.format->cpp[0];
4096 
4097 	if (crtc_state->stream) {
4098 		if (!dc_stream_set_cursor_attributes(crtc_state->stream,
4099 						     &attributes))
4100 			drm_err(adev_to_drm(adev), "DC failed to set cursor attributes\n");
4101 
4102 		update->cursor_attributes = &crtc_state->stream->cursor_attributes;
4103 
4104 		if (!dc_stream_set_cursor_position(crtc_state->stream,
4105 						   &position))
4106 			drm_err(adev_to_drm(adev), "DC failed to set cursor position\n");
4107 
4108 		update->cursor_position = &crtc_state->stream->cursor_position;
4109 	}
4110 }
4111 
4112 static void amdgpu_dm_enable_self_refresh(struct amdgpu_display_manager *dm,
4113 					  struct amdgpu_crtc *acrtc_attach,
4114 					  const struct dm_crtc_state *acrtc_state,
4115 					  const u64 current_ts)
4116 {
4117 	struct psr_settings *psr = &acrtc_state->stream->link->psr_settings;
4118 	struct replay_settings *pr = &acrtc_state->stream->link->replay_settings;
4119 	struct amdgpu_dm_connector *aconn =
4120 		(struct amdgpu_dm_connector *)acrtc_state->stream->dm_stream_context;
4121 
4122 	/* Decrement skip count when SR is enabled and we're doing fast updates. */
4123 	if (acrtc_state->update_type == UPDATE_TYPE_FAST &&
4124 	    (psr->psr_feature_enabled || pr->replay_feature_enabled)) {
4125 		if (aconn->sr_skip_count > 0)
4126 			aconn->sr_skip_count--;
4127 
4128 		/* Allow SR when skip count is 0. */
4129 		acrtc_attach->dm_irq_params.allow_sr_entry = !aconn->sr_skip_count;
4130 
4131 		/*
4132 		 * If sink supports PSR SU/Panel Replay, there is no need to rely on
4133 		 * a vblank event disable request to enable PSR/RP. PSR SU/RP
4134 		 * can be enabled immediately once OS demonstrates an
4135 		 * adequate number of fast atomic commits to notify KMD
4136 		 * of update events.
4137 		 * See `amdgpu_dm_crtc_vblank_control_worker()`.
4138 		 */
4139 		if (acrtc_attach->dm_irq_params.allow_sr_entry &&
4140 			(current_ts - psr->psr_dirty_rects_change_timestamp_ns) > 500000000) {
4141 			amdgpu_dm_psr_set_event(dm, acrtc_state->stream, false,
4142 				psr_event_hw_programming, false);
4143 
4144 			amdgpu_dm_replay_set_event(dm, acrtc_state->stream, false,
4145 				replay_event_hw_programming, false);
4146 		}
4147 	} else {
4148 		acrtc_attach->dm_irq_params.allow_sr_entry = false;
4149 	}
4150 }
4151 
4152 static void dm_arm_vblank_event(struct amdgpu_crtc *acrtc,
4153 				struct dm_crtc_state *acrtc_state,
4154 				bool pflip_update,
4155 				bool cursor_update)
4156 {
4157 	assert_spin_locked(&acrtc->base.dev->event_lock);
4158 
4159 	if (!acrtc->base.state->event || acrtc_state->active_planes == 0)
4160 		return;
4161 
4162 	if (pflip_update) {
4163 		drm_crtc_vblank_get(&acrtc->base);
4164 		WARN_ON(acrtc->pflip_status != AMDGPU_FLIP_NONE);
4165 		/* Arm flip completion handling and event delivery after programming. */
4166 		prepare_flip_isr(acrtc);
4167 	} else if (cursor_update) {
4168 		drm_crtc_vblank_get(&acrtc->base);
4169 		acrtc->event = acrtc->base.state->event;
4170 		acrtc->base.state->event = NULL;
4171 	}
4172 }
4173 
4174 static void amdgpu_dm_commit_planes(struct drm_atomic_commit *state,
4175 				    struct drm_device *dev,
4176 				    struct amdgpu_display_manager *dm,
4177 				    struct drm_crtc *pcrtc,
4178 				    bool wait_for_vblank)
4179 {
4180 	u32 i;
4181 	u64 timestamp_ns = ktime_get_ns();
4182 	struct drm_plane *plane;
4183 	struct drm_plane_state *old_plane_state, *new_plane_state;
4184 	struct amdgpu_crtc *acrtc_attach = to_amdgpu_crtc(pcrtc);
4185 	struct drm_crtc_state *new_pcrtc_state =
4186 			drm_atomic_get_new_crtc_state(state, pcrtc);
4187 	struct dm_crtc_state *acrtc_state = to_dm_crtc_state(new_pcrtc_state);
4188 	struct dm_crtc_state *dm_old_crtc_state =
4189 			to_dm_crtc_state(drm_atomic_get_old_crtc_state(state, pcrtc));
4190 	int planes_count = 0, vpos, hpos;
4191 	unsigned long flags;
4192 	u32 target_vblank, last_flip_vblank;
4193 	bool vrr_active = amdgpu_dm_crtc_vrr_active(acrtc_state);
4194 	bool cursor_update = false;
4195 	bool pflip_present = false;
4196 	bool immediate_flip = false;
4197 	bool flip_latched_during_prog = false;
4198 	bool dirty_rects_changed = false;
4199 	bool updated_planes_and_streams = false;
4200 	struct {
4201 		struct dc_surface_update surface_updates[MAX_SURFACES];
4202 		struct dc_plane_info plane_infos[MAX_SURFACES];
4203 		struct dc_scaling_info scaling_infos[MAX_SURFACES];
4204 		struct dc_flip_addrs flip_addrs[MAX_SURFACES];
4205 		struct dc_stream_update stream_update;
4206 	} *bundle;
4207 
4208 	bundle = kzalloc_obj(*bundle);
4209 
4210 	if (!bundle) {
4211 		drm_err(dev, "Failed to allocate update bundle\n");
4212 		goto cleanup;
4213 	}
4214 
4215 	/*
4216 	 * Disable the cursor first if we're disabling all the planes.
4217 	 * It'll remain on the screen after the planes are re-enabled
4218 	 * if we don't.
4219 	 *
4220 	 * If the cursor is transitioning from native to overlay mode, the
4221 	 * native cursor needs to be disabled first.
4222 	 */
4223 	if (acrtc_state->cursor_mode == DM_CURSOR_OVERLAY_MODE &&
4224 	    dm_old_crtc_state->cursor_mode == DM_CURSOR_NATIVE_MODE) {
4225 		struct dc_cursor_position cursor_position = {0};
4226 
4227 		if (!dc_stream_set_cursor_position(acrtc_state->stream,
4228 						   &cursor_position))
4229 			drm_err(dev, "DC failed to disable native cursor\n");
4230 
4231 		bundle->stream_update.cursor_position =
4232 				&acrtc_state->stream->cursor_position;
4233 	}
4234 
4235 	if (acrtc_state->active_planes == 0 &&
4236 	    dm_old_crtc_state->cursor_mode == DM_CURSOR_NATIVE_MODE)
4237 		amdgpu_dm_commit_cursors(state);
4238 
4239 	/* update planes when needed */
4240 	for_each_oldnew_plane_in_state(state, plane, old_plane_state, new_plane_state, i) {
4241 		struct drm_crtc *crtc = new_plane_state->crtc;
4242 		struct drm_crtc_state *new_crtc_state;
4243 		struct drm_framebuffer *fb = new_plane_state->fb;
4244 		struct amdgpu_framebuffer *afb = (struct amdgpu_framebuffer *)fb;
4245 		bool plane_needs_flip;
4246 		struct dc_plane_state *dc_plane;
4247 		struct dm_plane_state *dm_new_plane_state = to_dm_plane_state(new_plane_state);
4248 
4249 		/* Cursor plane is handled after stream updates */
4250 		if (plane->type == DRM_PLANE_TYPE_CURSOR &&
4251 		    acrtc_state->cursor_mode == DM_CURSOR_NATIVE_MODE) {
4252 			if ((fb && crtc == pcrtc) ||
4253 			    (old_plane_state->fb && old_plane_state->crtc == pcrtc)) {
4254 				cursor_update = true;
4255 				if (amdgpu_ip_version(dm->adev, DCE_HWIP, 0) != 0)
4256 					amdgpu_dm_update_cursor(plane, old_plane_state, &bundle->stream_update);
4257 			}
4258 
4259 			continue;
4260 		}
4261 
4262 		if (!fb || !crtc || pcrtc != crtc)
4263 			continue;
4264 
4265 		new_crtc_state = drm_atomic_get_new_crtc_state(state, crtc);
4266 		if (!new_crtc_state->active)
4267 			continue;
4268 
4269 		dc_plane = dm_new_plane_state->dc_state;
4270 		if (!dc_plane)
4271 			continue;
4272 
4273 		bundle->surface_updates[planes_count].surface = dc_plane;
4274 		if (new_pcrtc_state->color_mgmt_changed) {
4275 			bundle->surface_updates[planes_count].gamma = &dc_plane->gamma_correction;
4276 			bundle->surface_updates[planes_count].in_transfer_func = &dc_plane->in_transfer_func;
4277 			bundle->surface_updates[planes_count].gamut_remap_matrix = &dc_plane->gamut_remap_matrix;
4278 			bundle->surface_updates[planes_count].hdr_mult = dc_plane->hdr_mult;
4279 			bundle->surface_updates[planes_count].cm = &dc_plane->cm;
4280 		}
4281 
4282 		amdgpu_dm_plane_fill_dc_scaling_info(dm->adev, new_plane_state,
4283 				     &bundle->scaling_infos[planes_count]);
4284 
4285 		bundle->surface_updates[planes_count].scaling_info =
4286 			&bundle->scaling_infos[planes_count];
4287 
4288 		plane_needs_flip = old_plane_state->fb && new_plane_state->fb;
4289 
4290 		pflip_present = pflip_present || plane_needs_flip;
4291 
4292 		if (!plane_needs_flip) {
4293 			planes_count += 1;
4294 			continue;
4295 		}
4296 
4297 		fill_dc_plane_info_and_addr(
4298 			dm->adev, new_plane_state,
4299 			afb->tiling_flags,
4300 			&bundle->plane_infos[planes_count],
4301 			&bundle->flip_addrs[planes_count].address,
4302 			afb->tmz_surface);
4303 
4304 		drm_dbg_state(state->dev, "plane: id=%d dcc_en=%d\n",
4305 				 new_plane_state->plane->index,
4306 				 bundle->plane_infos[planes_count].dcc.enable);
4307 
4308 		bundle->surface_updates[planes_count].plane_info =
4309 			&bundle->plane_infos[planes_count];
4310 
4311 		if (acrtc_state->stream->link->psr_settings.psr_feature_enabled ||
4312 		    acrtc_state->stream->link->replay_settings.replay_feature_enabled) {
4313 			fill_dc_dirty_rects(plane, old_plane_state,
4314 					    new_plane_state, new_crtc_state,
4315 					    &bundle->flip_addrs[planes_count],
4316 					    acrtc_state->stream->link->psr_settings.psr_version ==
4317 					    DC_PSR_VERSION_SU_1,
4318 					    &dirty_rects_changed);
4319 
4320 			/*
4321 			 * If the dirty regions changed, PSR-SU need to be disabled temporarily
4322 			 * and enabled it again after dirty regions are stable to avoid video glitch.
4323 			 * PSR-SU will be enabled in
4324 			 * amdgpu_dm_crtc_vblank_control_worker() if user
4325 			 * pause the video during the PSR-SU was disabled.
4326 			 */
4327 			if (acrtc_state->stream->link->psr_settings.psr_version >= DC_PSR_VERSION_SU_1 &&
4328 			    acrtc_attach->dm_irq_params.allow_sr_entry &&
4329 			    dirty_rects_changed) {
4330 				mutex_lock(&dm->dc_lock);
4331 				acrtc_state->stream->link->psr_settings.psr_dirty_rects_change_timestamp_ns =
4332 				timestamp_ns;
4333 				dc_exit_ips_for_hw_access(dm->dc);
4334 				amdgpu_dm_psr_set_event(dm, acrtc_state->stream, true,
4335 					psr_event_hw_programming, true);
4336 				mutex_unlock(&dm->dc_lock);
4337 			}
4338 		}
4339 
4340 		/*
4341 		 * Only allow immediate flips for fast updates that don't
4342 		 * change memory domain, FB pitch, DCC state, rotation or
4343 		 * mirroring.
4344 		 *
4345 		 * dm_crtc_helper_atomic_check() only accepts async flips with
4346 		 * fast updates.
4347 		 */
4348 		if (crtc->state->async_flip &&
4349 		    (acrtc_state->update_type != UPDATE_TYPE_FAST ||
4350 		     get_mem_type(old_plane_state->fb) != get_mem_type(fb)))
4351 			drm_warn_once(state->dev,
4352 				      "[PLANE:%d:%s] async flip with non-fast update\n",
4353 				      plane->base.id, plane->name);
4354 
4355 		bundle->flip_addrs[planes_count].flip_immediate =
4356 			crtc->state->async_flip &&
4357 			acrtc_state->update_type == UPDATE_TYPE_FAST &&
4358 			get_mem_type(old_plane_state->fb) == get_mem_type(fb);
4359 
4360 		immediate_flip |= bundle->flip_addrs[planes_count].flip_immediate;
4361 
4362 		timestamp_ns = ktime_get_ns();
4363 		bundle->flip_addrs[planes_count].flip_timestamp_in_us = div_u64(timestamp_ns, 1000);
4364 		bundle->surface_updates[planes_count].flip_addr = &bundle->flip_addrs[planes_count];
4365 		bundle->surface_updates[planes_count].surface = dc_plane;
4366 
4367 		if (!bundle->surface_updates[planes_count].surface) {
4368 			drm_err(dev, "No surface for CRTC: id=%d\n",
4369 					acrtc_attach->crtc_id);
4370 			continue;
4371 		}
4372 
4373 		if (plane == pcrtc->primary)
4374 			update_freesync_state_on_stream(
4375 				dm,
4376 				acrtc_state,
4377 				acrtc_state->stream,
4378 				dc_plane,
4379 				bundle->flip_addrs[planes_count].flip_timestamp_in_us);
4380 
4381 		drm_dbg_state(state->dev, "%s Flipping to hi: 0x%x, low: 0x%x\n",
4382 				 __func__,
4383 				 bundle->flip_addrs[planes_count].address.grph.addr.high_part,
4384 				 bundle->flip_addrs[planes_count].address.grph.addr.low_part);
4385 
4386 		planes_count += 1;
4387 
4388 	}
4389 
4390 	if (pflip_present) {
4391 		if (!vrr_active) {
4392 			/* Use old throttling in non-vrr fixed refresh rate mode
4393 			 * to keep flip scheduling based on target vblank counts
4394 			 * working in a backwards compatible way, e.g., for
4395 			 * clients using the GLX_OML_sync_control extension or
4396 			 * DRI3/Present extension with defined target_msc.
4397 			 */
4398 			last_flip_vblank = amdgpu_get_vblank_counter_kms(pcrtc);
4399 		} else {
4400 			/* For variable refresh rate mode only:
4401 			 * Get vblank of last completed flip to avoid > 1 vrr
4402 			 * flips per video frame by use of throttling, but allow
4403 			 * flip programming anywhere in the possibly large
4404 			 * variable vrr vblank interval for fine-grained flip
4405 			 * timing control and more opportunity to avoid stutter
4406 			 * on late submission of flips.
4407 			 */
4408 			spin_lock_irqsave(&pcrtc->dev->event_lock, flags);
4409 			last_flip_vblank = acrtc_attach->dm_irq_params.last_flip_vblank;
4410 			spin_unlock_irqrestore(&pcrtc->dev->event_lock, flags);
4411 		}
4412 
4413 		target_vblank = last_flip_vblank + wait_for_vblank;
4414 
4415 		/*
4416 		 * Wait until we're out of the vertical blank period before the one
4417 		 * targeted by the flip
4418 		 */
4419 		while ((acrtc_attach->enabled &&
4420 			(amdgpu_display_get_crtc_scanoutpos(dm->ddev, acrtc_attach->crtc_id,
4421 							    0, &vpos, &hpos, NULL,
4422 							    NULL, &pcrtc->hwmode)
4423 			 & (DRM_SCANOUTPOS_VALID | DRM_SCANOUTPOS_IN_VBLANK)) ==
4424 			(DRM_SCANOUTPOS_VALID | DRM_SCANOUTPOS_IN_VBLANK) &&
4425 			(int)(target_vblank -
4426 			  amdgpu_get_vblank_counter_kms(pcrtc)) > 0)) {
4427 			usleep_range(1000, 1100);
4428 		}
4429 
4430 		if (acrtc_state->stream) {
4431 			if (acrtc_state->freesync_vrr_info_changed)
4432 				bundle->stream_update.vrr_infopacket =
4433 					&acrtc_state->stream->vrr_infopacket;
4434 		}
4435 	}
4436 
4437 	/*
4438 	 * DCE depends on a combination of GRPH_FLIP, VLINE0, and VUPDATE for
4439 	 * event delivery. Only GRPH_FLIP handler can send pflip events, and it
4440 	 * only fires if HW latched to the flip. Maintain legacy behavior by
4441 	 * arming event before programming.
4442 	 */
4443 	if (amdgpu_ip_version(dm->adev, DCE_HWIP, 0) == 0) {
4444 		scoped_guard(spinlock_irqsave, &pcrtc->dev->event_lock) {
4445 			dm_arm_vblank_event(acrtc_attach, acrtc_state,
4446 					pflip_present, cursor_update);
4447 		}
4448 	}
4449 
4450 	/* Update the planes if changed or disable if we don't have any. */
4451 	if ((planes_count || acrtc_state->active_planes == 0) &&
4452 		acrtc_state->stream) {
4453 		/*
4454 		 * If PSR or idle optimizations are enabled then flush out
4455 		 * any pending work before hardware programming.
4456 		 */
4457 		if (dm->vblank_control_workqueue)
4458 			flush_workqueue(dm->vblank_control_workqueue);
4459 
4460 		bundle->stream_update.stream = acrtc_state->stream;
4461 		if (new_pcrtc_state->mode_changed) {
4462 			bundle->stream_update.src = acrtc_state->stream->src;
4463 			bundle->stream_update.dst = acrtc_state->stream->dst;
4464 		}
4465 
4466 		if (new_pcrtc_state->color_mgmt_changed) {
4467 			/*
4468 			 * TODO: This isn't fully correct since we've actually
4469 			 * already modified the stream in place.
4470 			 */
4471 			bundle->stream_update.gamut_remap =
4472 				&acrtc_state->stream->gamut_remap_matrix;
4473 			bundle->stream_update.output_csc_transform =
4474 				&acrtc_state->stream->csc_color_matrix;
4475 			bundle->stream_update.out_transfer_func =
4476 				&acrtc_state->stream->out_transfer_func;
4477 			bundle->stream_update.lut3d_func =
4478 				(struct dc_3dlut *) acrtc_state->stream->lut3d_func;
4479 			bundle->stream_update.func_shaper =
4480 				(struct dc_transfer_func *) acrtc_state->stream->func_shaper;
4481 		}
4482 
4483 		acrtc_state->stream->abm_level = acrtc_state->abm_level;
4484 		if (acrtc_state->abm_level != dm_old_crtc_state->abm_level)
4485 			bundle->stream_update.abm_level = &acrtc_state->abm_level;
4486 
4487 		/*
4488 		 * If FreeSync state on the stream has changed then we need to
4489 		 * re-adjust the min/max bounds now that DC doesn't handle this
4490 		 * as part of commit.
4491 		 */
4492 		if (is_dc_timing_adjust_needed(dm_old_crtc_state, acrtc_state)) {
4493 			spin_lock_irqsave(&pcrtc->dev->event_lock, flags);
4494 			dc_stream_adjust_vmin_vmax(
4495 				dm->dc, acrtc_state->stream,
4496 				&acrtc_attach->dm_irq_params.vrr_params.adjust);
4497 			spin_unlock_irqrestore(&pcrtc->dev->event_lock, flags);
4498 		}
4499 		mutex_lock(&dm->dc_lock);
4500 		update_planes_and_stream_adapter(dm->dc,
4501 					 acrtc_state->update_type,
4502 					 planes_count,
4503 					 acrtc_state->stream,
4504 					 &bundle->stream_update,
4505 					 bundle->surface_updates);
4506 		updated_planes_and_streams = true;
4507 
4508 		/**
4509 		 * Enable or disable the interrupts on the backend.
4510 		 *
4511 		 * Most pipes are put into power gating when unused.
4512 		 *
4513 		 * When power gating is enabled on a pipe we lose the
4514 		 * interrupt enablement state when power gating is disabled.
4515 		 *
4516 		 * So we need to update the IRQ control state in hardware
4517 		 * whenever the pipe turns on (since it could be previously
4518 		 * power gated) or off (since some pipes can't be power gated
4519 		 * on some ASICs).
4520 		 */
4521 		if (dm_old_crtc_state->active_planes != acrtc_state->active_planes)
4522 			dm_update_pflip_irq_state(drm_to_adev(dev),
4523 						  acrtc_attach);
4524 		amdgpu_dm_enable_self_refresh(dm, acrtc_attach, acrtc_state,
4525 					      timestamp_ns);
4526 		mutex_unlock(&dm->dc_lock);
4527 	}
4528 
4529 	/*
4530 	 * Update cursor state *after* programming all the planes.
4531 	 * This avoids redundant programming in the case where we're going
4532 	 * to be disabling a single plane - those pipes are being disabled.
4533 	 */
4534 	if (acrtc_state->active_planes &&
4535 	    (!updated_planes_and_streams || amdgpu_ip_version(dm->adev, DCE_HWIP, 0) == 0) &&
4536 	    acrtc_state->cursor_mode == DM_CURSOR_NATIVE_MODE)
4537 		amdgpu_dm_commit_cursors(state);
4538 
4539 	/*
4540 	 * DCN specific vblank handling
4541 	 * ============================
4542 	 *
4543 	 * With the event_lock held, arm the vblank event, and determine whether
4544 	 * deliver it immediately, or in VUPDATE_NO_LOCK IRQ (i.e. HW latch
4545 	 * point) handler. Do this *after* programming so that the IRQ handler
4546 	 * will not deliver the event before HW laches onto the programmed
4547 	 * values:
4548 	 *
4549 	 *     Commit thread      IRQ handler                       HW
4550 	 *     -----------------------------------------------------------------
4551 	 *     arm_vblank_event()
4552 	 *                                                          vupdate()
4553 	 *                        vupdate_handler()
4554 	 *                          cook_timestamp()
4555 	 *                          # prev flip already latched,
4556 	 *                          # so flip_latched == true.
4557 	 *                          if event_armed && flip_latched:
4558 	 *                            send_vblank_event()
4559 	 *                            # sent before latch, **BAD!**
4560 	 *     hw_program()
4561 	 *                                                          vupdate()
4562 	 *                                                          **latch**
4563 	 *
4564 	 * There's a consequence of arming after: it's possible for HW to latch
4565 	 * between start of HW programming and acrtc->event/pflip_status arming.
4566 	 * When this happens, the IRQ handler will send the event on the next
4567 	 * immediate latch point, even though HW has already latched. This is
4568 	 * handled by optimistically checking for HW latch after programming,
4569 	 * and if latched, send the event immediately:
4570 	 *
4571 	 *     Commit thread             IRQ handler                HW
4572 	 *     -----------------------------------------------------------------
4573 	 *     hw_program()
4574 	 *                                                          vupdate()
4575 	 *                                                          **latch**
4576 	 *                               vupdate_handler()
4577 	 *                                 cook_timestamp()
4578 	 *                                 # event_armed == false
4579 	 *                                 # **no event sent!**
4580 	 *     arm_vblank_event()
4581 	 *     if flip_latched:
4582 	 *       **send_vblank_event()**
4583 	 *       disarm_vblank_event()
4584 	 *
4585 	 * The IRQ handler is expected to cook the timestamp, but we need to
4586 	 * cook the timestamp before optimistic sending as well. That's because
4587 	 * the following sequence is possible:
4588 	 *
4589 	 *     Commit thread              IRQ handler              HW
4590 	 *     -----------------------------------------------------------------
4591 	 *     hw_program()
4592 	 *     arm_vblank_event()
4593 	 *                                                         vupdate()
4594 	 *                                                         **latch**
4595 	 *     if flip_latched:
4596 	 *       # Need cook before send!
4597 	 *       **cook_timestamp()**
4598 	 *       send_vblank_event()
4599 	 *       disarm_vblank_event()
4600 	 *                                vupdate_handler()
4601 	 *                                  cook_timestamp()
4602 	 *                                  # event_armed == false
4603 	 *                                  # no event sent!
4604 	 *
4605 	 * Cooking twice is OK, since DRM scanout accurate timestamps report A)
4606 	 * the previous vactive start if currently in vactive, or B) the next
4607 	 * vactive start if currently in vblank (see &get_vblank_counter). 'A)'
4608 	 * is what we want for the optimistic send, and for 'B)', we'll cook a
4609 	 * timestamp no later than the next IRQ handler run.
4610 	 *
4611 	 * The more correct fix is to wrap programming and arming with the
4612 	 * event_lock and thus serializing it with the IRQ handler. However,
4613 	 * there are various sleep-waits within
4614 	 * update_planes_and_stream_adapter() that makes spin locking illegal.
4615 	 * And on full updates, it can take 1-2 frame-times to return (see
4616 	 * commit_planes_for_stream).
4617 	 *
4618 	 * On DCE, GRPH_PFLIP IRQ is used and takes care of this.
4619 	 */
4620 	if (amdgpu_ip_version(dm->adev, DCE_HWIP, 0) != 0) {
4621 		spin_lock_irqsave(&pcrtc->dev->event_lock, flags);
4622 
4623 		if (updated_planes_and_streams) {
4624 			flip_latched_during_prog =
4625 				!dc_get_flip_pending_on_otg(dm->dc, acrtc_attach->otg_inst);
4626 		}
4627 
4628 		dm_arm_vblank_event(acrtc_attach, acrtc_state,
4629 				    pflip_present, cursor_update);
4630 
4631 		/*
4632 		 * Deliver the event immediately on immediate flip, or on a
4633 		 * update that has already latched.
4634 		 */
4635 		if ((immediate_flip || flip_latched_during_prog) &&
4636 		    acrtc_attach->pflip_status == AMDGPU_FLIP_SUBMITTED &&
4637 		    acrtc_attach->event) {
4638 			drm_crtc_accurate_vblank_count(&acrtc_attach->base);
4639 			drm_crtc_send_vblank_event(&acrtc_attach->base,
4640 						   acrtc_attach->event);
4641 			acrtc_attach->event = NULL;
4642 			drm_crtc_vblank_put(&acrtc_attach->base);
4643 			acrtc_attach->pflip_status = AMDGPU_FLIP_NONE;
4644 		}
4645 		spin_unlock_irqrestore(&pcrtc->dev->event_lock, flags);
4646 	}
4647 
4648 cleanup:
4649 	kfree(bundle);
4650 }
4651 
4652 /*
4653  * amdgpu_dm_crtc_copy_transient_flags - copy mirrored flags from DRM to DC
4654  * @crtc_state: the DRM CRTC state
4655  * @stream_state: the DC stream state.
4656  *
4657  * Copy the mirrored transient state flags from DRM, to DC. It is used to bring
4658  * a dc_stream_state's flags in sync with a drm_crtc_state's flags.
4659  */
4660 static void amdgpu_dm_crtc_copy_transient_flags(struct drm_crtc_state *crtc_state,
4661 						struct dc_stream_state *stream_state)
4662 {
4663 	stream_state->mode_changed = drm_atomic_crtc_needs_modeset(crtc_state);
4664 }
4665 
4666 /**
4667  * amdgpu_dm_crtc_complete_writeback - finish a pending writeback job
4668  * @acrtc: the CRTC whose pending writeback should be completed
4669  *
4670  * Clears the pending state, signals the writeback out fence and releases the
4671  * vblank reference taken in dm_set_writeback() while the writeback was armed.
4672  * The pending flag is tested and cleared under the writeback job lock, so this
4673  * is safe to call concurrently from the completion vblank IRQ
4674  * (dm_crtc_high_irq()) and from the writeback teardown path
4675  * (dm_clear_writeback()); only the caller that observes the pending job
4676  * performs the completion.
4677  *
4678  * Return: true if a pending writeback job was completed by this call.
4679  */
4680 bool amdgpu_dm_crtc_complete_writeback(struct amdgpu_crtc *acrtc)
4681 {
4682 	unsigned long flags;
4683 	bool pending;
4684 
4685 	if (!acrtc->wb_conn)
4686 		return false;
4687 
4688 	spin_lock_irqsave(&acrtc->wb_conn->job_lock, flags);
4689 	pending = acrtc->wb_pending;
4690 	acrtc->wb_pending = false;
4691 	spin_unlock_irqrestore(&acrtc->wb_conn->job_lock, flags);
4692 
4693 	if (!pending)
4694 		return false;
4695 
4696 	drm_writeback_signal_completion(acrtc->wb_conn, 0);
4697 	drm_crtc_vblank_put(&acrtc->base);
4698 
4699 	return true;
4700 }
4701 EXPORT_IF_KUNIT(amdgpu_dm_crtc_complete_writeback);
4702 
4703 static void dm_clear_writeback(struct amdgpu_display_manager *dm,
4704 			      struct amdgpu_crtc *acrtc,
4705 			      struct dm_crtc_state *crtc_state)
4706 {
4707 	dc_stream_remove_writeback(dm->dc, crtc_state->stream, 0);
4708 
4709 	/*
4710 	 * If the writeback is still pending when it is torn down (its
4711 	 * completion vblank IRQ never fired), signal the out fence so a
4712 	 * waiting client does not stall and release the vblank reference
4713 	 * taken in dm_set_writeback().
4714 	 */
4715 	amdgpu_dm_crtc_complete_writeback(acrtc);
4716 }
4717 
4718 /**
4719  * amdgpu_dm_mod_power_update_streams - update mod_power stream state on modeset
4720  * @state: the drm atomic state
4721  * @dm: the display manager to update mod_power on
4722  *
4723  * Notify mod_power of stream changes on modeset events, and disable PSR/Replay
4724  * in preparation for hardware programming. See also
4725  * amdgpu_dm_mod_power_setup_streams() for post-modeset mod_power setup.
4726  */
4727 static void amdgpu_dm_mod_power_update_streams(struct drm_atomic_commit *state,
4728 					       struct amdgpu_display_manager *dm)
4729 {
4730 	struct dm_crtc_state *dm_old_crtc_state, *dm_new_crtc_state;
4731 	struct drm_crtc_state *old_crtc_state, *new_crtc_state;
4732 	struct amdgpu_dm_connector *aconnector;
4733 	struct drm_crtc *crtc;
4734 	int i = 0;
4735 
4736 	for_each_oldnew_crtc_in_state(state, crtc, old_crtc_state, new_crtc_state, i) {
4737 		dm_old_crtc_state = to_dm_crtc_state(old_crtc_state);
4738 		dm_new_crtc_state = to_dm_crtc_state(new_crtc_state);
4739 
4740 		if (!drm_atomic_crtc_needs_modeset(new_crtc_state))
4741 			continue;
4742 
4743 		/*
4744 		 * Update mod_power on modeset event in preparation for hw
4745 		 * programming. Always use the old stream, since it would have
4746 		 * been previously added to mod_power. If old stream is null (on
4747 		 * crtc enable, for example), mod_power will no-op, which is the
4748 		 * desried behavior.
4749 		 */
4750 		if (old_crtc_state->active) {
4751 			scoped_guard(mutex, &dm->dc_lock) {
4752 				dc_exit_ips_for_hw_access(dm->dc);
4753 				amdgpu_dm_psr_set_event(dm, dm_old_crtc_state->stream, true,
4754 					psr_event_hw_programming, true);
4755 				amdgpu_dm_replay_set_event(dm, dm_old_crtc_state->stream, true,
4756 					replay_event_hw_programming, true);
4757 				amdgpu_dm_replay_set_event(dm, dm_old_crtc_state->stream, false,
4758 					replay_event_general_ui, false);
4759 			}
4760 		}
4761 
4762 		if (new_crtc_state->active) {
4763 			aconnector = (struct amdgpu_dm_connector *)
4764 				dm_new_crtc_state->stream->dm_stream_context;
4765 			if (old_crtc_state->active) {
4766 				mod_power_replace_stream(dm->power_module,
4767 					dm_old_crtc_state->stream,
4768 					dm_new_crtc_state->stream,
4769 					&aconnector->psr_caps);
4770 			} else {
4771 				mod_power_add_stream(dm->power_module,
4772 					dm_new_crtc_state->stream,
4773 					&aconnector->psr_caps);
4774 			}
4775 		} else if (old_crtc_state->active) {
4776 			mod_power_remove_stream(dm->power_module,
4777 				dm_old_crtc_state->stream);
4778 		}
4779 	}
4780 }
4781 
4782 /**
4783  * amdgpu_dm_mod_power_setup_streams - setup mod_power stream state post modeset
4784  * @state: the drm atomic state
4785  * @dm: the display manager to update mod_power on
4786  *
4787  * Notify mod_power of mode_change. This needs to be done after dc_stream
4788  * updates have been committed, and VRR parameters have been updated.
4789  */
4790 static void amdgpu_dm_mod_power_setup_streams(struct drm_atomic_commit *state,
4791 					      struct amdgpu_display_manager *dm)
4792 {
4793 	struct dm_crtc_state *dm_new_crtc_state;
4794 	struct drm_crtc_state *new_crtc_state;
4795 	struct amdgpu_crtc *acrtc;
4796 	struct drm_crtc *crtc;
4797 	int i = 0;
4798 
4799 	for_each_new_crtc_in_state(state, crtc, new_crtc_state, i) {
4800 		dm_new_crtc_state = to_dm_crtc_state(new_crtc_state);
4801 		acrtc = to_amdgpu_crtc(crtc);
4802 
4803 		if (!drm_atomic_crtc_needs_modeset(new_crtc_state))
4804 			continue;
4805 
4806 		if (new_crtc_state->active) {
4807 			amdgpu_dm_link_setup_replay(dm_new_crtc_state->stream,
4808 					&acrtc->dm_irq_params.vrr_params);
4809 			mod_power_notify_mode_change(dm->power_module,
4810 						dm_new_crtc_state->stream,
4811 						false);
4812 
4813 			/*
4814 			 * Block PSR / Replay on the new stream until display settles post-modeset.
4815 			 * These events will be cleared by amdgpu_dm_enable_self_refresh() once
4816 			 * allow_sr_entry becomes true.
4817 			 */
4818 			amdgpu_dm_psr_set_event(dm, dm_new_crtc_state->stream, true,
4819 				psr_event_hw_programming, true);
4820 
4821 			amdgpu_dm_replay_set_event(dm, dm_new_crtc_state->stream, true,
4822 				replay_event_hw_programming | replay_event_general_ui,
4823 				true);
4824 		}
4825 	}
4826 
4827 }
4828 
4829 static void amdgpu_dm_commit_streams(struct drm_atomic_commit *state,
4830 					struct dc_state *dc_state)
4831 {
4832 	struct drm_device *dev = state->dev;
4833 	struct amdgpu_device *adev = drm_to_adev(dev);
4834 	struct amdgpu_display_manager *dm = &adev->dm;
4835 	struct drm_crtc *crtc;
4836 	struct drm_crtc_state *old_crtc_state, *new_crtc_state;
4837 	struct dm_crtc_state *dm_old_crtc_state, *dm_new_crtc_state;
4838 	struct drm_connector_state *old_con_state;
4839 	struct drm_connector *connector;
4840 	bool mode_set_reset_required = false;
4841 	u32 i;
4842 	struct dc_commit_streams_params params = {dc_state->streams, dc_state->stream_count};
4843 	bool set_backlight_level = false;
4844 
4845 	/* Disable writeback */
4846 	for_each_old_connector_in_state(state, connector, old_con_state, i) {
4847 		struct dm_connector_state *dm_old_con_state;
4848 		struct amdgpu_crtc *acrtc;
4849 
4850 		if (connector->connector_type != DRM_MODE_CONNECTOR_WRITEBACK)
4851 			continue;
4852 
4853 		old_crtc_state = NULL;
4854 
4855 		dm_old_con_state = to_dm_connector_state(old_con_state);
4856 		if (!dm_old_con_state->base.crtc)
4857 			continue;
4858 
4859 		acrtc = to_amdgpu_crtc(dm_old_con_state->base.crtc);
4860 		if (acrtc)
4861 			old_crtc_state = drm_atomic_get_old_crtc_state(state, &acrtc->base);
4862 
4863 		if (!acrtc || !acrtc->wb_enabled)
4864 			continue;
4865 
4866 		dm_old_crtc_state = to_dm_crtc_state(old_crtc_state);
4867 
4868 		dm_clear_writeback(dm, acrtc, dm_old_crtc_state);
4869 		acrtc->wb_enabled = false;
4870 	}
4871 
4872 	amdgpu_dm_mod_power_update_streams(state, dm);
4873 
4874 	for_each_oldnew_crtc_in_state(state, crtc, old_crtc_state,
4875 				      new_crtc_state, i) {
4876 		struct amdgpu_crtc *acrtc = to_amdgpu_crtc(crtc);
4877 
4878 		dm_old_crtc_state = to_dm_crtc_state(old_crtc_state);
4879 
4880 		if (old_crtc_state->active &&
4881 		    (!new_crtc_state->active ||
4882 		     drm_atomic_crtc_needs_modeset(new_crtc_state))) {
4883 			manage_dm_interrupts(adev, acrtc, NULL);
4884 			dc_stream_release(dm_old_crtc_state->stream);
4885 		}
4886 	}
4887 
4888 	drm_atomic_helper_calc_timestamping_constants(state);
4889 
4890 	/* update changed items */
4891 	for_each_oldnew_crtc_in_state(state, crtc, old_crtc_state, new_crtc_state, i) {
4892 		struct amdgpu_crtc *acrtc = to_amdgpu_crtc(crtc);
4893 
4894 		dm_new_crtc_state = to_dm_crtc_state(new_crtc_state);
4895 		dm_old_crtc_state = to_dm_crtc_state(old_crtc_state);
4896 
4897 		drm_dbg_state(state->dev,
4898 			"amdgpu_crtc id:%d crtc_state_flags: enable:%d, active:%d, planes_changed:%d, mode_changed:%d,active_changed:%d,connectors_changed:%d\n",
4899 			acrtc->crtc_id,
4900 			new_crtc_state->enable,
4901 			new_crtc_state->active,
4902 			new_crtc_state->planes_changed,
4903 			new_crtc_state->mode_changed,
4904 			new_crtc_state->active_changed,
4905 			new_crtc_state->connectors_changed);
4906 
4907 		/* Disable cursor if disabling crtc */
4908 		if (old_crtc_state->active && !new_crtc_state->active) {
4909 			struct dc_cursor_position position;
4910 
4911 			memset(&position, 0, sizeof(position));
4912 			mutex_lock(&dm->dc_lock);
4913 			dc_exit_ips_for_hw_access(dm->dc);
4914 			dc_stream_program_cursor_position(dm_old_crtc_state->stream, &position);
4915 			mutex_unlock(&dm->dc_lock);
4916 		}
4917 
4918 		/* Copy all transient state flags into dc state */
4919 		if (dm_new_crtc_state->stream) {
4920 			amdgpu_dm_crtc_copy_transient_flags(&dm_new_crtc_state->base,
4921 							    dm_new_crtc_state->stream);
4922 		}
4923 
4924 		/* handles headless hotplug case, updating new_state and
4925 		 * aconnector as needed
4926 		 */
4927 
4928 		if (amdgpu_dm_crtc_modeset_required(new_crtc_state, dm_new_crtc_state->stream, dm_old_crtc_state->stream)) {
4929 
4930 			drm_dbg_atomic(dev,
4931 				       "Atomic commit: SET crtc id %d: [%p]\n",
4932 				       acrtc->crtc_id, acrtc);
4933 
4934 			if (!dm_new_crtc_state->stream) {
4935 				/*
4936 				 * this could happen because of issues with
4937 				 * userspace notifications delivery.
4938 				 * In this case userspace tries to set mode on
4939 				 * display which is disconnected in fact.
4940 				 * dc_sink is NULL in this case on aconnector.
4941 				 * We expect reset mode will come soon.
4942 				 *
4943 				 * This can also happen when unplug is done
4944 				 * during resume sequence ended
4945 				 *
4946 				 * In this case, we want to pretend we still
4947 				 * have a sink to keep the pipe running so that
4948 				 * hw state is consistent with the sw state
4949 				 */
4950 				drm_dbg_atomic(dev,
4951 					       "Failed to create new stream for crtc %d\n",
4952 						acrtc->base.base.id);
4953 				continue;
4954 			}
4955 
4956 			if (dm_old_crtc_state->stream)
4957 				remove_stream(adev, acrtc, dm_old_crtc_state->stream);
4958 
4959 			pm_runtime_get_noresume(dev->dev);
4960 
4961 			acrtc->enabled = true;
4962 			acrtc->hw_mode = new_crtc_state->mode;
4963 			crtc->hwmode = new_crtc_state->mode;
4964 			mode_set_reset_required = true;
4965 			set_backlight_level = true;
4966 		} else if (modereset_required(new_crtc_state)) {
4967 			drm_dbg_atomic(dev,
4968 				       "Atomic commit: RESET. crtc id %d:[%p]\n",
4969 				       acrtc->crtc_id, acrtc);
4970 			/* i.e. reset mode */
4971 			if (dm_old_crtc_state->stream)
4972 				remove_stream(adev, acrtc, dm_old_crtc_state->stream);
4973 
4974 			mode_set_reset_required = true;
4975 		}
4976 	} /* for_each_crtc_in_state() */
4977 
4978 	/* if there mode set or reset, flush vblank work queue */
4979 	if (mode_set_reset_required) {
4980 		if (dm->vblank_control_workqueue)
4981 			flush_workqueue(dm->vblank_control_workqueue);
4982 	}
4983 
4984 	dm_enable_per_frame_crtc_master_sync(dc_state);
4985 	mutex_lock(&dm->dc_lock);
4986 	dc_exit_ips_for_hw_access(dm->dc);
4987 	WARN_ON(!dc_commit_streams(dm->dc, &params));
4988 
4989 	bool frl_stream_found = false;
4990 
4991 	for (i = 0; i < params.stream_count; i++) {
4992 		struct dc_stream_state *stream = params.streams[i];
4993 
4994 		if (stream->signal == SIGNAL_TYPE_HDMI_FRL) {
4995 			frl_stream_found = true;
4996 			break;
4997 		}
4998 	}
4999 	if (frl_stream_found) {
5000 		if (queue_delayed_work(dm->hdmi_frl_status_polling_wq,
5001 				       &dm->hdmi_frl_status_polling_work,
5002 				       msecs_to_jiffies(dm->hdmi_frl_status_polling_delay_ms)))
5003 			drm_dbg_kms(dev, "200ms frl status polling starts ...\n");
5004 	} else {
5005 		if (cancel_delayed_work_sync(&dm->hdmi_frl_status_polling_work))
5006 			drm_dbg_kms(dev, "200ms frl status polling stops ...\n");
5007 	}
5008 	/* Allow idle optimization when vblank count is 0 for display off */
5009 	if ((dm->active_vblank_irq_count == 0) && amdgpu_dm_is_headless(dm->adev))
5010 		dc_allow_idle_optimizations(dm->dc, true);
5011 	mutex_unlock(&dm->dc_lock);
5012 
5013 	for_each_new_crtc_in_state(state, crtc, new_crtc_state, i) {
5014 		struct amdgpu_crtc *acrtc = to_amdgpu_crtc(crtc);
5015 
5016 		dm_new_crtc_state = to_dm_crtc_state(new_crtc_state);
5017 
5018 		if (dm_new_crtc_state->stream != NULL) {
5019 			const struct dc_stream_status *status =
5020 					dc_stream_get_status(dm_new_crtc_state->stream);
5021 
5022 			if (!status)
5023 				status = dc_state_get_stream_status(dc_state,
5024 									 dm_new_crtc_state->stream);
5025 			if (!status)
5026 				drm_err(dev,
5027 					"got no status for stream %p on acrtc%p\n",
5028 					dm_new_crtc_state->stream, acrtc);
5029 			else
5030 				acrtc->otg_inst = status->primary_otg_inst;
5031 		}
5032 	}
5033 
5034 	/* During boot up and resume the DC layer will reset the panel brightness
5035 	 * to fix a flicker issue.
5036 	 * It will cause the dm->actual_brightness is not the current panel brightness
5037 	 * level. (the dm->brightness is the correct panel level)
5038 	 * So we set the backlight level with dm->brightness value after set mode
5039 	 */
5040 	if (set_backlight_level) {
5041 		for (i = 0; i < dm->num_of_edps; i++) {
5042 			if (dm->backlight_dev[i])
5043 				amdgpu_dm_backlight_set_level(dm, i, dm->brightness[i]);
5044 		}
5045 	}
5046 }
5047 
5048 static void dm_set_writeback(struct amdgpu_display_manager *dm,
5049 			      struct dm_crtc_state *crtc_state,
5050 			      struct drm_connector *connector,
5051 			      struct drm_connector_state *new_con_state)
5052 {
5053 	struct drm_writeback_connector *wb_conn = drm_connector_to_writeback(connector);
5054 	struct amdgpu_device *adev = dm->adev;
5055 	struct amdgpu_crtc *acrtc;
5056 	struct dc_writeback_info *wb_info;
5057 	struct pipe_ctx *pipe = NULL;
5058 	struct amdgpu_framebuffer *afb;
5059 	int i = 0;
5060 
5061 	wb_info = kzalloc_obj(*wb_info);
5062 	if (!wb_info) {
5063 		drm_err(adev_to_drm(adev), "Failed to allocate wb_info\n");
5064 		return;
5065 	}
5066 
5067 	acrtc = to_amdgpu_crtc(wb_conn->encoder.crtc);
5068 	if (!acrtc) {
5069 		drm_err(adev_to_drm(adev), "no amdgpu_crtc found\n");
5070 		kfree(wb_info);
5071 		return;
5072 	}
5073 
5074 	afb = to_amdgpu_framebuffer(new_con_state->writeback_job->fb);
5075 	if (!afb) {
5076 		drm_err(adev_to_drm(adev), "No amdgpu_framebuffer found\n");
5077 		kfree(wb_info);
5078 		return;
5079 	}
5080 
5081 	for (i = 0; i < MAX_PIPES; i++) {
5082 		if (dm->dc->current_state->res_ctx.pipe_ctx[i].stream == crtc_state->stream) {
5083 			pipe = &dm->dc->current_state->res_ctx.pipe_ctx[i];
5084 			break;
5085 		}
5086 	}
5087 
5088 	/* fill in wb_info */
5089 	wb_info->wb_enabled = true;
5090 
5091 	wb_info->dwb_pipe_inst = 0;
5092 	wb_info->dwb_params.dwbscl_black_color = 0;
5093 	wb_info->dwb_params.hdr_mult = 0x1F000;
5094 	wb_info->dwb_params.csc_params.gamut_adjust_type = CM_GAMUT_ADJUST_TYPE_BYPASS;
5095 	wb_info->dwb_params.csc_params.gamut_coef_format = CM_GAMUT_REMAP_COEF_FORMAT_S2_13;
5096 	wb_info->dwb_params.output_depth = DWB_OUTPUT_PIXEL_DEPTH_10BPC;
5097 	wb_info->dwb_params.cnv_params.cnv_out_bpc = DWB_CNV_OUT_BPC_10BPC;
5098 
5099 	/* width & height from crtc */
5100 	wb_info->dwb_params.cnv_params.src_width = acrtc->base.mode.crtc_hdisplay;
5101 	wb_info->dwb_params.cnv_params.src_height = acrtc->base.mode.crtc_vdisplay;
5102 	wb_info->dwb_params.dest_width = acrtc->base.mode.crtc_hdisplay;
5103 	wb_info->dwb_params.dest_height = acrtc->base.mode.crtc_vdisplay;
5104 
5105 	wb_info->dwb_params.cnv_params.crop_en = false;
5106 	wb_info->dwb_params.stereo_params.stereo_enabled = false;
5107 
5108 	wb_info->dwb_params.cnv_params.out_max_pix_val = 0x3ff;	// 10 bits
5109 	wb_info->dwb_params.cnv_params.out_min_pix_val = 0;
5110 	wb_info->dwb_params.cnv_params.fc_out_format = DWB_OUT_FORMAT_32BPP_ARGB;
5111 	wb_info->dwb_params.cnv_params.out_denorm_mode = DWB_OUT_DENORM_BYPASS;
5112 
5113 	wb_info->dwb_params.out_format = dwb_scaler_mode_bypass444;
5114 
5115 	wb_info->dwb_params.capture_rate = dwb_capture_rate_0;
5116 
5117 	wb_info->dwb_params.scaler_taps.h_taps = 1;
5118 	wb_info->dwb_params.scaler_taps.v_taps = 1;
5119 	wb_info->dwb_params.scaler_taps.h_taps_c = 1;
5120 	wb_info->dwb_params.scaler_taps.v_taps_c = 1;
5121 	wb_info->dwb_params.subsample_position = DWB_INTERSTITIAL_SUBSAMPLING;
5122 
5123 	wb_info->mcif_buf_params.luma_pitch = afb->base.pitches[0];
5124 	wb_info->mcif_buf_params.chroma_pitch = afb->base.pitches[1];
5125 
5126 	for (i = 0; i < DWB_MCIF_BUF_COUNT; i++) {
5127 		wb_info->mcif_buf_params.luma_address[i] = afb->address;
5128 		wb_info->mcif_buf_params.chroma_address[i] = 0;
5129 	}
5130 
5131 	wb_info->mcif_buf_params.p_vmid = 1;
5132 	if (amdgpu_ip_version(adev, DCE_HWIP, 0) >= IP_VERSION(3, 0, 0)) {
5133 		wb_info->mcif_warmup_params.start_address.quad_part = afb->address;
5134 		wb_info->mcif_warmup_params.region_size =
5135 			wb_info->mcif_buf_params.luma_pitch * wb_info->dwb_params.dest_height;
5136 	}
5137 	wb_info->mcif_warmup_params.p_vmid = 1;
5138 	wb_info->writeback_source_plane = pipe->plane_state;
5139 
5140 	dc_stream_add_writeback(dm->dc, crtc_state->stream, wb_info);
5141 
5142 	acrtc->wb_conn = wb_conn;
5143 	drm_writeback_queue_job(wb_conn, new_con_state);
5144 
5145 	/*
5146 	 * Writeback completion is detected in the CRTC vblank IRQ
5147 	 * (dm_crtc_high_irq()). Take a vblank reference so the vblank interrupt
5148 	 * stays enabled while the writeback is pending; otherwise a
5149 	 * writeback-only commit right after drm_crtc_vblank_on() (e.g.
5150 	 * re-enabling a CRTC that was disabled) has no other vblank reference,
5151 	 * the IRQ never fires and the out fence times out. The matching put
5152 	 * happens once completion is signalled in dm_crtc_high_irq(), or when
5153 	 * the writeback is torn down in dm_clear_writeback().
5154 	 *
5155 	 * Arm wb_pending only after the reference is held so the completion IRQ
5156 	 * cannot run its matching vblank_put before this get.
5157 	 */
5158 	WARN_ON(drm_crtc_vblank_get(&acrtc->base));
5159 	acrtc->wb_pending = true;
5160 }
5161 
5162 static void amdgpu_dm_update_hdcp(struct drm_atomic_commit *state)
5163 {
5164 	struct drm_connector_state *old_con_state, *new_con_state;
5165 	struct drm_device *dev = state->dev;
5166 	struct drm_connector *connector;
5167 	struct amdgpu_device *adev = drm_to_adev(dev);
5168 	int i;
5169 
5170 	if (!adev->dm.hdcp_workqueue)
5171 		return;
5172 
5173 	for_each_oldnew_connector_in_state(state, connector, old_con_state, new_con_state, i) {
5174 		struct dm_connector_state *dm_new_con_state = to_dm_connector_state(new_con_state);
5175 		struct amdgpu_crtc *acrtc = to_amdgpu_crtc(dm_new_con_state->base.crtc);
5176 		struct drm_crtc_state *old_crtc_state, *new_crtc_state;
5177 		struct dm_crtc_state *dm_new_crtc_state;
5178 		struct amdgpu_dm_connector *aconnector;
5179 
5180 		if (!connector || connector->connector_type == DRM_MODE_CONNECTOR_WRITEBACK)
5181 			continue;
5182 
5183 		aconnector = to_amdgpu_dm_connector(connector);
5184 
5185 		drm_dbg(dev, "[HDCP_DM] -------------- i : %x ----------\n", i);
5186 
5187 		drm_dbg(dev, "[HDCP_DM] connector->index: %x connect_status: %x dpms: %x\n",
5188 			connector->index, connector->status, connector->dpms);
5189 		drm_dbg(dev, "[HDCP_DM] state protection old: %x new: %x\n",
5190 			old_con_state->content_protection, new_con_state->content_protection);
5191 
5192 		if (aconnector->dc_sink) {
5193 			if (aconnector->dc_sink->sink_signal != SIGNAL_TYPE_VIRTUAL &&
5194 				aconnector->dc_sink->sink_signal != SIGNAL_TYPE_NONE) {
5195 				drm_dbg(dev, "[HDCP_DM] pipe_ctx dispname=%s\n",
5196 				aconnector->dc_sink->edid_caps.display_name);
5197 			}
5198 		}
5199 
5200 		new_crtc_state = NULL;
5201 		old_crtc_state = NULL;
5202 
5203 		if (acrtc) {
5204 			new_crtc_state = drm_atomic_get_new_crtc_state(state, &acrtc->base);
5205 			old_crtc_state = drm_atomic_get_old_crtc_state(state, &acrtc->base);
5206 		}
5207 
5208 		if (old_crtc_state)
5209 			drm_dbg(dev, "old crtc en: %x a: %x m: %x a-chg: %x c-chg: %x\n",
5210 			old_crtc_state->enable,
5211 			old_crtc_state->active,
5212 			old_crtc_state->mode_changed,
5213 			old_crtc_state->active_changed,
5214 			old_crtc_state->connectors_changed);
5215 
5216 		if (new_crtc_state)
5217 			drm_dbg(dev, "NEW crtc en: %x a: %x m: %x a-chg: %x c-chg: %x\n",
5218 			new_crtc_state->enable,
5219 			new_crtc_state->active,
5220 			new_crtc_state->mode_changed,
5221 			new_crtc_state->active_changed,
5222 			new_crtc_state->connectors_changed);
5223 
5224 
5225 		dm_new_crtc_state = to_dm_crtc_state(new_crtc_state);
5226 
5227 		if (dm_new_crtc_state && dm_new_crtc_state->stream == NULL &&
5228 		    connector->state->content_protection == DRM_MODE_CONTENT_PROTECTION_ENABLED) {
5229 			hdcp_reset_display(adev->dm.hdcp_workqueue, aconnector->dc_link->link_index);
5230 			new_con_state->content_protection = DRM_MODE_CONTENT_PROTECTION_DESIRED;
5231 			dm_new_con_state->update_hdcp = true;
5232 			continue;
5233 		}
5234 
5235 		if (is_content_protection_different(new_crtc_state, old_crtc_state, new_con_state,
5236 											old_con_state, connector, adev->dm.hdcp_workqueue)) {
5237 			/* when display is unplugged from mst hub, connctor will
5238 			 * be destroyed within dm_dp_mst_connector_destroy. connector
5239 			 * hdcp perperties, like type, undesired, desired, enabled,
5240 			 * will be lost. So, save hdcp properties into hdcp_work within
5241 			 * amdgpu_dm_atomic_commit_tail. if the same display is
5242 			 * plugged back with same display index, its hdcp properties
5243 			 * will be retrieved from hdcp_work within dm_dp_mst_get_modes
5244 			 */
5245 
5246 			bool enable_encryption = false;
5247 
5248 			if (new_con_state->content_protection == DRM_MODE_CONTENT_PROTECTION_DESIRED)
5249 				enable_encryption = true;
5250 
5251 			if (aconnector->dc_link && aconnector->dc_sink &&
5252 				aconnector->dc_link->type == dc_connection_mst_branch) {
5253 				struct hdcp_workqueue *hdcp_work = adev->dm.hdcp_workqueue;
5254 				struct hdcp_workqueue *hdcp_w =
5255 					&hdcp_work[aconnector->dc_link->link_index];
5256 
5257 				hdcp_w->hdcp_content_type[connector->index] =
5258 					new_con_state->hdcp_content_type;
5259 				hdcp_w->content_protection[connector->index] =
5260 					new_con_state->content_protection;
5261 			}
5262 
5263 			if (new_crtc_state && new_crtc_state->mode_changed &&
5264 				new_con_state->content_protection >= DRM_MODE_CONTENT_PROTECTION_DESIRED)
5265 				enable_encryption = true;
5266 
5267 			drm_info(dev, "[HDCP_DM] hdcp_update_display enable_encryption = %x\n", enable_encryption);
5268 
5269 			if (aconnector->dc_link)
5270 				hdcp_update_display(
5271 					adev->dm.hdcp_workqueue, aconnector->dc_link->link_index, aconnector,
5272 					new_con_state->hdcp_content_type, enable_encryption);
5273 		}
5274 	}
5275 }
5276 
5277 static int amdgpu_dm_atomic_setup_commit(struct drm_atomic_commit *state)
5278 {
5279 	struct drm_crtc *crtc;
5280 	struct drm_crtc_state *old_crtc_state, *new_crtc_state;
5281 	struct dm_crtc_state *dm_old_crtc_state, *dm_new_crtc_state;
5282 	int i, ret;
5283 
5284 	ret = drm_dp_mst_atomic_setup_commit(state);
5285 	if (ret)
5286 		return ret;
5287 
5288 	for_each_oldnew_crtc_in_state(state, crtc, old_crtc_state, new_crtc_state, i) {
5289 		dm_old_crtc_state = to_dm_crtc_state(old_crtc_state);
5290 		dm_new_crtc_state = to_dm_crtc_state(new_crtc_state);
5291 		/*
5292 		 * Color management settings. We also update color properties
5293 		 * when a modeset is needed, to ensure it gets reprogrammed.
5294 		 */
5295 		if (dm_new_crtc_state->base.active && dm_new_crtc_state->stream &&
5296 		    (dm_new_crtc_state->base.color_mgmt_changed ||
5297 		     dm_old_crtc_state->regamma_tf != dm_new_crtc_state->regamma_tf ||
5298 		     drm_atomic_crtc_needs_modeset(new_crtc_state))) {
5299 			ret = amdgpu_dm_update_crtc_color_mgmt(dm_new_crtc_state);
5300 			if (ret) {
5301 				drm_dbg_atomic(state->dev, "Failed to update color state\n");
5302 				return ret;
5303 			}
5304 		}
5305 	}
5306 
5307 	return 0;
5308 }
5309 
5310 STATIC_IFN_KUNIT void set_multisync_trigger_params(
5311 		struct dc_stream_state *stream)
5312 {
5313 	struct dc_stream_state *master = NULL;
5314 
5315 	if (stream->triggered_crtc_reset.enabled) {
5316 		master = stream->triggered_crtc_reset.event_source;
5317 		stream->triggered_crtc_reset.event =
5318 			master->timing.flags.VSYNC_POSITIVE_POLARITY ?
5319 			CRTC_EVENT_VSYNC_RISING : CRTC_EVENT_VSYNC_FALLING;
5320 		stream->triggered_crtc_reset.delay = TRIGGER_DELAY_NEXT_PIXEL;
5321 	}
5322 }
5323 EXPORT_IF_KUNIT(set_multisync_trigger_params);
5324 
5325 STATIC_IFN_KUNIT void set_master_stream(struct dc_stream_state *stream_set[],
5326 					int stream_count)
5327 {
5328 	int j, highest_rfr = 0, master_stream = 0;
5329 
5330 	for (j = 0;  j < stream_count; j++) {
5331 		if (stream_set[j] && stream_set[j]->triggered_crtc_reset.enabled) {
5332 			int refresh_rate = 0;
5333 
5334 			refresh_rate = (stream_set[j]->timing.pix_clk_100hz*100)/
5335 				(stream_set[j]->timing.h_total*stream_set[j]->timing.v_total);
5336 			if (refresh_rate > highest_rfr) {
5337 				highest_rfr = refresh_rate;
5338 				master_stream = j;
5339 			}
5340 		}
5341 	}
5342 	for (j = 0;  j < stream_count; j++) {
5343 		if (stream_set[j])
5344 			stream_set[j]->triggered_crtc_reset.event_source = stream_set[master_stream];
5345 	}
5346 }
5347 EXPORT_IF_KUNIT(set_master_stream);
5348 
5349 STATIC_IFN_KUNIT void dm_enable_per_frame_crtc_master_sync(struct dc_state *context)
5350 {
5351 	int i = 0;
5352 	struct dc_stream_state *stream;
5353 
5354 	if (context->stream_count < 2)
5355 		return;
5356 	for (i = 0; i < context->stream_count ; i++) {
5357 		if (!context->streams[i])
5358 			continue;
5359 		/*
5360 		 * TODO: add a function to read AMD VSDB bits and set
5361 		 * crtc_sync_master.multi_sync_enabled flag
5362 		 * For now it's set to false
5363 		 */
5364 	}
5365 
5366 	set_master_stream(context->streams, context->stream_count);
5367 
5368 	for (i = 0; i < context->stream_count ; i++) {
5369 		stream = context->streams[i];
5370 
5371 		if (!stream)
5372 			continue;
5373 
5374 		set_multisync_trigger_params(stream);
5375 	}
5376 }
5377 EXPORT_IF_KUNIT(dm_enable_per_frame_crtc_master_sync);
5378 
5379 /**
5380  * amdgpu_dm_atomic_commit_tail() - AMDgpu DM's commit tail implementation.
5381  * @state: The atomic state to commit
5382  *
5383  * This will tell DC to commit the constructed DC state from atomic_check,
5384  * programming the hardware. Any failures here implies a hardware failure, since
5385  * atomic check should have filtered anything non-kosher.
5386  */
5387 static void amdgpu_dm_atomic_commit_tail(struct drm_atomic_commit *state)
5388 {
5389 	struct drm_device *dev = state->dev;
5390 	struct amdgpu_device *adev = drm_to_adev(dev);
5391 	struct amdgpu_display_manager *dm = &adev->dm;
5392 	struct dm_atomic_state *dm_state;
5393 	struct dc_state *dc_state = NULL;
5394 	u32 i, j;
5395 	struct drm_crtc *crtc;
5396 	struct drm_crtc_state *old_crtc_state, *new_crtc_state;
5397 	unsigned long flags;
5398 	bool wait_for_vblank = true;
5399 	struct drm_connector *connector;
5400 	struct drm_connector_state *old_con_state = NULL, *new_con_state = NULL;
5401 	struct dm_crtc_state *dm_old_crtc_state, *dm_new_crtc_state;
5402 	int crtc_disable_count = 0;
5403 
5404 	trace_amdgpu_dm_atomic_commit_tail_begin(state);
5405 
5406 	drm_atomic_helper_update_legacy_modeset_state(dev, state);
5407 	drm_dp_mst_atomic_wait_for_dependencies(state);
5408 
5409 	dm_state = dm_atomic_get_new_state(state);
5410 	if (dm_state && dm_state->context) {
5411 		dc_state = dm_state->context;
5412 		amdgpu_dm_commit_streams(state, dc_state);
5413 	}
5414 
5415 	amdgpu_dm_update_hdcp(state);
5416 
5417 	/* Handle connector state changes */
5418 	for_each_oldnew_connector_in_state(state, connector, old_con_state, new_con_state, i) {
5419 		struct dm_connector_state *dm_new_con_state = to_dm_connector_state(new_con_state);
5420 		struct dm_connector_state *dm_old_con_state = to_dm_connector_state(old_con_state);
5421 		struct amdgpu_crtc *acrtc = to_amdgpu_crtc(dm_new_con_state->base.crtc);
5422 		struct dc_surface_update *dummy_updates;
5423 		struct dc_stream_update stream_update;
5424 		struct dc_info_packet hdr_packet;
5425 		struct dc_stream_status *status = NULL;
5426 		bool abm_changed, hdr_changed, scaling_changed, output_color_space_changed = false;
5427 
5428 		memset(&stream_update, 0, sizeof(stream_update));
5429 
5430 		if (acrtc) {
5431 			new_crtc_state = drm_atomic_get_new_crtc_state(state, &acrtc->base);
5432 			old_crtc_state = drm_atomic_get_old_crtc_state(state, &acrtc->base);
5433 		}
5434 
5435 		/* Skip any modesets/resets */
5436 		if (!acrtc || drm_atomic_crtc_needs_modeset(new_crtc_state))
5437 			continue;
5438 
5439 		dm_new_crtc_state = to_dm_crtc_state(new_crtc_state);
5440 		dm_old_crtc_state = to_dm_crtc_state(old_crtc_state);
5441 
5442 		scaling_changed = is_scaling_state_different(dm_new_con_state,
5443 							     dm_old_con_state);
5444 
5445 		if ((new_con_state->hdmi.broadcast_rgb != old_con_state->hdmi.broadcast_rgb) &&
5446 			(dm_old_crtc_state->stream->output_color_space !=
5447 				amdgpu_dm_get_output_color_space(&dm_new_crtc_state->stream->timing, new_con_state)))
5448 			output_color_space_changed = true;
5449 
5450 		abm_changed = dm_new_crtc_state->abm_level !=
5451 			      dm_old_crtc_state->abm_level;
5452 
5453 		hdr_changed =
5454 			!drm_connector_atomic_hdr_metadata_equal(old_con_state, new_con_state);
5455 
5456 		if (!scaling_changed && !abm_changed && !hdr_changed && !output_color_space_changed)
5457 			continue;
5458 
5459 		stream_update.stream = dm_new_crtc_state->stream;
5460 		if (scaling_changed) {
5461 			amdgpu_dm_update_stream_scaling_settings(dev, &dm_new_con_state->base.crtc->mode,
5462 					dm_new_con_state, dm_new_crtc_state->stream);
5463 
5464 			stream_update.src = dm_new_crtc_state->stream->src;
5465 			stream_update.dst = dm_new_crtc_state->stream->dst;
5466 		}
5467 
5468 		if (output_color_space_changed) {
5469 			dm_new_crtc_state->stream->output_color_space
5470 				= amdgpu_dm_get_output_color_space(&dm_new_crtc_state->stream->timing, new_con_state);
5471 
5472 			stream_update.output_color_space = &dm_new_crtc_state->stream->output_color_space;
5473 		}
5474 
5475 		if (abm_changed) {
5476 			dm_new_crtc_state->stream->abm_level = dm_new_crtc_state->abm_level;
5477 
5478 			stream_update.abm_level = &dm_new_crtc_state->abm_level;
5479 		}
5480 
5481 		if (hdr_changed) {
5482 			amdgpu_dm_fill_hdr_info_packet(new_con_state, &hdr_packet);
5483 			stream_update.hdr_static_metadata = &hdr_packet;
5484 		}
5485 
5486 		status = dc_stream_get_status(dm_new_crtc_state->stream);
5487 
5488 		if (WARN_ON(!status))
5489 			continue;
5490 
5491 		WARN_ON(!status->plane_count);
5492 
5493 		/*
5494 		 * TODO: DC refuses to perform stream updates without a dc_surface_update.
5495 		 * Here we create an empty update on each plane.
5496 		 * To fix this, DC should permit updating only stream properties.
5497 		 */
5498 		dummy_updates = kzalloc(sizeof(struct dc_surface_update) * MAX_SURFACES, GFP_KERNEL);
5499 		if (!dummy_updates) {
5500 			drm_err(adev_to_drm(adev), "Failed to allocate memory for dummy_updates.\n");
5501 			continue;
5502 		}
5503 		for (j = 0; j < status->plane_count; j++)
5504 			dummy_updates[j].surface = status->plane_states[j];
5505 
5506 		sort(dummy_updates, status->plane_count,
5507 		     sizeof(*dummy_updates), dm_plane_layer_index_cmp, NULL);
5508 
5509 		mutex_lock(&dm->dc_lock);
5510 		dc_exit_ips_for_hw_access(dm->dc);
5511 		dc_update_planes_and_stream(dm->dc,
5512 					    dummy_updates,
5513 					    status->plane_count,
5514 					    dm_new_crtc_state->stream,
5515 					    &stream_update);
5516 		mutex_unlock(&dm->dc_lock);
5517 		kfree(dummy_updates);
5518 
5519 		drm_connector_update_privacy_screen(new_con_state);
5520 	}
5521 
5522 	/**
5523 	 * Enable interrupts for CRTCs that are newly enabled or went through
5524 	 * a modeset. It was intentionally deferred until after the front end
5525 	 * state was modified to wait until the OTG was on and so the IRQ
5526 	 * handlers didn't access stale or invalid state.
5527 	 */
5528 	for_each_oldnew_crtc_in_state(state, crtc, old_crtc_state, new_crtc_state, i) {
5529 		struct amdgpu_crtc *acrtc = to_amdgpu_crtc(crtc);
5530 #ifdef CONFIG_DEBUG_FS
5531 		enum amdgpu_dm_pipe_crc_source cur_crc_src;
5532 #endif
5533 		/* Count number of newly disabled CRTCs for dropping PM refs later. */
5534 		if (old_crtc_state->active && !new_crtc_state->active)
5535 			crtc_disable_count++;
5536 
5537 		dm_new_crtc_state = to_dm_crtc_state(new_crtc_state);
5538 		dm_old_crtc_state = to_dm_crtc_state(old_crtc_state);
5539 
5540 		/* For freesync config update on crtc state and params for irq */
5541 		update_stream_irq_parameters(dm, dm_new_crtc_state);
5542 
5543 #ifdef CONFIG_DEBUG_FS
5544 		spin_lock_irqsave(&adev_to_drm(adev)->event_lock, flags);
5545 		cur_crc_src = acrtc->dm_irq_params.crc_src;
5546 		spin_unlock_irqrestore(&adev_to_drm(adev)->event_lock, flags);
5547 #endif
5548 
5549 		if (new_crtc_state->active &&
5550 		    (!old_crtc_state->active ||
5551 		     drm_atomic_crtc_needs_modeset(new_crtc_state))) {
5552 			dc_stream_retain(dm_new_crtc_state->stream);
5553 			acrtc->dm_irq_params.stream = dm_new_crtc_state->stream;
5554 			manage_dm_interrupts(adev, acrtc, dm_new_crtc_state);
5555 		}
5556 		/* Handle vrr on->off / off->on transitions */
5557 		amdgpu_dm_handle_vrr_transition(dm, dm_old_crtc_state, dm_new_crtc_state);
5558 
5559 #ifdef CONFIG_DEBUG_FS
5560 		if (new_crtc_state->active &&
5561 		    (!old_crtc_state->active ||
5562 		     drm_atomic_crtc_needs_modeset(new_crtc_state))) {
5563 			/**
5564 			 * Frontend may have changed so reapply the CRC capture
5565 			 * settings for the stream.
5566 			 */
5567 			if (amdgpu_dm_is_valid_crc_source(cur_crc_src)) {
5568 #if defined(CONFIG_DRM_AMD_SECURE_DISPLAY)
5569 				if (amdgpu_dm_crc_window_is_activated(crtc)) {
5570 					uint8_t cnt;
5571 
5572 					spin_lock_irqsave(&adev_to_drm(adev)->event_lock, flags);
5573 					for (cnt = 0; cnt < MAX_CRC_WINDOW_NUM; cnt++) {
5574 						if (acrtc->dm_irq_params.window_param[cnt].enable) {
5575 							acrtc->dm_irq_params.window_param[cnt].update_win = true;
5576 
5577 							/**
5578 							 * It takes 2 frames for HW to stably generate CRC when
5579 							 * resuming from suspend, so we set skip_frame_cnt 2.
5580 							 */
5581 							acrtc->dm_irq_params.window_param[cnt].skip_frame_cnt = 2;
5582 						}
5583 					}
5584 					spin_unlock_irqrestore(&adev_to_drm(adev)->event_lock, flags);
5585 				}
5586 #endif
5587 				if (amdgpu_dm_crtc_configure_crc_source(
5588 					crtc, dm_new_crtc_state, cur_crc_src))
5589 					drm_dbg_atomic(dev, "Failed to configure crc source");
5590 			}
5591 		}
5592 #endif
5593 	}
5594 
5595 	amdgpu_dm_mod_power_setup_streams(state, dm);
5596 
5597 	for_each_new_crtc_in_state(state, crtc, new_crtc_state, j)
5598 		if (new_crtc_state->async_flip)
5599 			wait_for_vblank = false;
5600 
5601 	/* update planes when needed per crtc*/
5602 	for_each_new_crtc_in_state(state, crtc, new_crtc_state, j) {
5603 		dm_new_crtc_state = to_dm_crtc_state(new_crtc_state);
5604 
5605 		if (dm_new_crtc_state->stream)
5606 			amdgpu_dm_commit_planes(state, dev, dm, crtc, wait_for_vblank);
5607 	}
5608 
5609 	/* Enable writeback */
5610 	for_each_new_connector_in_state(state, connector, new_con_state, i) {
5611 		struct dm_connector_state *dm_new_con_state = to_dm_connector_state(new_con_state);
5612 		struct amdgpu_crtc *acrtc = to_amdgpu_crtc(dm_new_con_state->base.crtc);
5613 
5614 		if (connector->connector_type != DRM_MODE_CONNECTOR_WRITEBACK)
5615 			continue;
5616 
5617 		if (!new_con_state->writeback_job)
5618 			continue;
5619 
5620 		new_crtc_state = drm_atomic_get_new_crtc_state(state, &acrtc->base);
5621 
5622 		if (!new_crtc_state)
5623 			continue;
5624 
5625 		if (acrtc->wb_enabled)
5626 			continue;
5627 
5628 		dm_new_crtc_state = to_dm_crtc_state(new_crtc_state);
5629 
5630 		dm_set_writeback(dm, dm_new_crtc_state, connector, new_con_state);
5631 		acrtc->wb_enabled = true;
5632 	}
5633 
5634 	/* Update audio instances for each connector. */
5635 	amdgpu_dm_commit_audio(dev, state);
5636 
5637 	/* restore the backlight level */
5638 	for (i = 0; i < dm->num_of_edps; i++) {
5639 		if (dm->backlight_dev[i] &&
5640 		    (dm->actual_brightness[i] != dm->brightness[i]))
5641 			amdgpu_dm_backlight_set_level(dm, i, dm->brightness[i]);
5642 	}
5643 
5644 	/*
5645 	 * send vblank event on all events not handled in flip and
5646 	 * mark consumed event for drm_atomic_helper_commit_hw_done
5647 	 */
5648 	spin_lock_irqsave(&adev_to_drm(adev)->event_lock, flags);
5649 	for_each_new_crtc_in_state(state, crtc, new_crtc_state, i) {
5650 
5651 		if (new_crtc_state->event)
5652 			drm_send_event_locked(dev, &new_crtc_state->event->base);
5653 
5654 		new_crtc_state->event = NULL;
5655 	}
5656 	spin_unlock_irqrestore(&adev_to_drm(adev)->event_lock, flags);
5657 
5658 	/* Signal HW programming completion */
5659 	drm_atomic_helper_commit_hw_done(state);
5660 
5661 	if (wait_for_vblank)
5662 		drm_atomic_helper_wait_for_flip_done(dev, state);
5663 
5664 	drm_atomic_helper_cleanup_planes(dev, state);
5665 
5666 	/* Don't free the memory if we are hitting this as part of suspend.
5667 	 * This way we don't free any memory during suspend; see
5668 	 * amdgpu_bo_free_kernel().  The memory will be freed in the first
5669 	 * non-suspend modeset or when the driver is torn down.
5670 	 */
5671 	if (!adev->in_suspend) {
5672 		/* return the stolen vga memory back to VRAM */
5673 		if (!adev->mman.keep_stolen_vga_memory)
5674 			amdgpu_ttm_unmark_vram_reserved(adev, AMDGPU_RESV_STOLEN_VGA);
5675 		amdgpu_ttm_unmark_vram_reserved(adev, AMDGPU_RESV_STOLEN_EXTENDED);
5676 	}
5677 
5678 	/*
5679 	 * Finally, drop a runtime PM reference for each newly disabled CRTC,
5680 	 * so we can put the GPU into runtime suspend if we're not driving any
5681 	 * displays anymore
5682 	 */
5683 	for (i = 0; i < crtc_disable_count; i++)
5684 		pm_runtime_put_autosuspend(dev->dev);
5685 	pm_runtime_mark_last_busy(dev->dev);
5686 
5687 	trace_amdgpu_dm_atomic_commit_tail_finish(state);
5688 }
5689 
5690 /*
5691  * Grabs all modesetting locks to serialize against any blocking commits,
5692  * Waits for completion of all non blocking commits.
5693  */
5694 static int do_aquire_global_lock(struct drm_device *dev,
5695 				 struct drm_atomic_commit *state)
5696 {
5697 	struct drm_crtc *crtc;
5698 	struct drm_crtc_commit *commit;
5699 	long ret;
5700 
5701 	/*
5702 	 * Adding all modeset locks to aquire_ctx will
5703 	 * ensure that when the framework release it the
5704 	 * extra locks we are locking here will get released to
5705 	 */
5706 	ret = drm_modeset_lock_all_ctx(dev, state->acquire_ctx);
5707 	if (ret)
5708 		return ret;
5709 
5710 	list_for_each_entry(crtc, &dev->mode_config.crtc_list, head) {
5711 		spin_lock(&crtc->commit_lock);
5712 		commit = list_first_entry_or_null(&crtc->commit_list,
5713 				struct drm_crtc_commit, commit_entry);
5714 		if (commit)
5715 			drm_crtc_commit_get(commit);
5716 		spin_unlock(&crtc->commit_lock);
5717 
5718 		if (!commit)
5719 			continue;
5720 
5721 		/*
5722 		 * Make sure all pending HW programming completed and
5723 		 * page flips done
5724 		 */
5725 		ret = wait_for_completion_interruptible_timeout(&commit->hw_done, 10*HZ);
5726 
5727 		if (ret > 0)
5728 			ret = wait_for_completion_interruptible_timeout(
5729 					&commit->flip_done, 10*HZ);
5730 
5731 		if (ret == 0)
5732 			drm_err(dev, "[CRTC:%d:%s] hw_done or flip_done timed out\n",
5733 				  crtc->base.id, crtc->name);
5734 
5735 		drm_crtc_commit_put(commit);
5736 	}
5737 
5738 	return ret < 0 ? ret : 0;
5739 }
5740 
5741 STATIC_IFN_KUNIT void get_freesync_config_for_crtc(
5742 	struct dm_crtc_state *new_crtc_state,
5743 	struct dm_connector_state *new_con_state)
5744 {
5745 	struct mod_freesync_config config = {0};
5746 	struct amdgpu_dm_connector *aconnector;
5747 	struct drm_display_mode *mode = &new_crtc_state->base.mode;
5748 	int vrefresh = drm_mode_vrefresh(mode);
5749 	bool fs_vid_mode = false;
5750 
5751 	if (new_con_state->base.connector->connector_type == DRM_MODE_CONNECTOR_WRITEBACK)
5752 		return;
5753 
5754 	aconnector = to_amdgpu_dm_connector(new_con_state->base.connector);
5755 
5756 	new_crtc_state->vrr_supported = new_con_state->freesync_capable &&
5757 					vrefresh >= aconnector->min_vfreq &&
5758 					vrefresh <= aconnector->max_vfreq;
5759 
5760 	if (new_crtc_state->vrr_supported) {
5761 		new_crtc_state->stream->ignore_msa_timing_param = true;
5762 		fs_vid_mode = new_crtc_state->freesync_config.state == VRR_STATE_ACTIVE_FIXED;
5763 
5764 		config.min_refresh_in_uhz = aconnector->min_vfreq * 1000000;
5765 		config.max_refresh_in_uhz = aconnector->max_vfreq * 1000000;
5766 		config.vsif_supported = true;
5767 		config.btr = true;
5768 
5769 		if (fs_vid_mode) {
5770 			config.state = VRR_STATE_ACTIVE_FIXED;
5771 			config.fixed_refresh_in_uhz = new_crtc_state->freesync_config.fixed_refresh_in_uhz;
5772 			goto out;
5773 		} else if (new_crtc_state->base.vrr_enabled) {
5774 			config.state = VRR_STATE_ACTIVE_VARIABLE;
5775 		} else {
5776 			config.state = VRR_STATE_INACTIVE;
5777 		}
5778 	} else {
5779 		config.state = VRR_STATE_UNSUPPORTED;
5780 	}
5781 out:
5782 	new_crtc_state->freesync_config = config;
5783 }
5784 EXPORT_IF_KUNIT(get_freesync_config_for_crtc);
5785 
5786 STATIC_IFN_KUNIT void reset_freesync_config_for_crtc(
5787 	struct dm_crtc_state *new_crtc_state)
5788 {
5789 	new_crtc_state->vrr_supported = false;
5790 
5791 	memset(&new_crtc_state->vrr_infopacket, 0,
5792 	       sizeof(new_crtc_state->vrr_infopacket));
5793 }
5794 EXPORT_IF_KUNIT(reset_freesync_config_for_crtc);
5795 
5796 STATIC_IFN_KUNIT bool
5797 is_timing_unchanged_for_freesync(struct drm_crtc_state *old_crtc_state,
5798 				 struct drm_crtc_state *new_crtc_state)
5799 {
5800 	const struct drm_display_mode *old_mode, *new_mode;
5801 
5802 	if (!old_crtc_state || !new_crtc_state)
5803 		return false;
5804 
5805 	old_mode = &old_crtc_state->mode;
5806 	new_mode = &new_crtc_state->mode;
5807 
5808 	if (old_mode->clock       == new_mode->clock &&
5809 	    old_mode->hdisplay    == new_mode->hdisplay &&
5810 	    old_mode->vdisplay    == new_mode->vdisplay &&
5811 	    old_mode->htotal      == new_mode->htotal &&
5812 	    old_mode->vtotal      != new_mode->vtotal &&
5813 	    old_mode->hsync_start == new_mode->hsync_start &&
5814 	    old_mode->vsync_start != new_mode->vsync_start &&
5815 	    old_mode->hsync_end   == new_mode->hsync_end &&
5816 	    old_mode->vsync_end   != new_mode->vsync_end &&
5817 	    old_mode->hskew       == new_mode->hskew &&
5818 	    old_mode->vscan       == new_mode->vscan &&
5819 	    (old_mode->vsync_end - old_mode->vsync_start) ==
5820 	    (new_mode->vsync_end - new_mode->vsync_start))
5821 		return true;
5822 
5823 	return false;
5824 }
5825 EXPORT_IF_KUNIT(is_timing_unchanged_for_freesync);
5826 
5827 STATIC_IFN_KUNIT void set_freesync_fixed_config(struct dm_crtc_state *dm_new_crtc_state)
5828 {
5829 	u64 num, den, res;
5830 	struct drm_crtc_state *new_crtc_state = &dm_new_crtc_state->base;
5831 
5832 	dm_new_crtc_state->freesync_config.state = VRR_STATE_ACTIVE_FIXED;
5833 
5834 	num = (unsigned long long)new_crtc_state->mode.clock * 1000 * 1000000;
5835 	den = (unsigned long long)new_crtc_state->mode.htotal *
5836 	      (unsigned long long)new_crtc_state->mode.vtotal;
5837 
5838 	res = div_u64(num, den);
5839 	dm_new_crtc_state->freesync_config.fixed_refresh_in_uhz = res;
5840 }
5841 EXPORT_IF_KUNIT(set_freesync_fixed_config);
5842 
5843 static int dm_update_crtc_state(struct amdgpu_display_manager *dm,
5844 			 struct drm_atomic_commit *state,
5845 			 struct drm_crtc *crtc,
5846 			 struct drm_crtc_state *old_crtc_state,
5847 			 struct drm_crtc_state *new_crtc_state,
5848 			 bool enable,
5849 			 bool *lock_and_validation_needed)
5850 {
5851 	struct dm_atomic_state *dm_state = NULL;
5852 	struct dm_crtc_state *dm_old_crtc_state, *dm_new_crtc_state;
5853 	struct dc_stream_state *new_stream;
5854 	struct amdgpu_device *adev = dm->adev;
5855 	int ret = 0;
5856 
5857 	/*
5858 	 * TODO Move this code into dm_crtc_atomic_check once we get rid of dc_validation_set
5859 	 * update changed items
5860 	 */
5861 	struct amdgpu_crtc *acrtc = NULL;
5862 	struct drm_connector *connector = NULL;
5863 	struct amdgpu_dm_connector *aconnector = NULL;
5864 	struct drm_connector_state *drm_new_conn_state = NULL, *drm_old_conn_state = NULL;
5865 	struct dm_connector_state *dm_new_conn_state = NULL, *dm_old_conn_state = NULL;
5866 
5867 	new_stream = NULL;
5868 
5869 	dm_old_crtc_state = to_dm_crtc_state(old_crtc_state);
5870 	dm_new_crtc_state = to_dm_crtc_state(new_crtc_state);
5871 	acrtc = to_amdgpu_crtc(crtc);
5872 	connector = amdgpu_dm_find_first_crtc_matching_connector(state, crtc);
5873 	if (connector)
5874 		aconnector = to_amdgpu_dm_connector(connector);
5875 
5876 	/* TODO This hack should go away */
5877 	if (connector && enable) {
5878 		/* Make sure fake sink is created in plug-in scenario */
5879 		drm_new_conn_state = drm_atomic_get_new_connector_state(state,
5880 									connector);
5881 		drm_old_conn_state = drm_atomic_get_old_connector_state(state,
5882 									connector);
5883 
5884 		if (WARN_ON(!drm_new_conn_state)) {
5885 			ret = -EINVAL;
5886 			goto fail;
5887 		}
5888 
5889 		dm_new_conn_state = to_dm_connector_state(drm_new_conn_state);
5890 		dm_old_conn_state = to_dm_connector_state(drm_old_conn_state);
5891 
5892 		if (!drm_atomic_crtc_needs_modeset(new_crtc_state))
5893 			goto skip_modeset;
5894 
5895 		new_stream = amdgpu_dm_create_validate_stream_for_sink(connector,
5896 							     &new_crtc_state->mode,
5897 							     dm_new_conn_state,
5898 							     dm_old_crtc_state->stream);
5899 
5900 		/*
5901 		 * we can have no stream on ACTION_SET if a display
5902 		 * was disconnected during S3, in this case it is not an
5903 		 * error, the OS will be updated after detection, and
5904 		 * will do the right thing on next atomic commit
5905 		 */
5906 
5907 		if (!new_stream) {
5908 			drm_dbg_driver(adev_to_drm(adev), "%s: Failed to create new stream for crtc %d\n",
5909 					__func__, acrtc->base.base.id);
5910 			ret = -ENOMEM;
5911 			goto fail;
5912 		}
5913 
5914 		/*
5915 		 * TODO: Check VSDB bits to decide whether this should
5916 		 * be enabled or not.
5917 		 */
5918 		new_stream->triggered_crtc_reset.enabled =
5919 			dm->force_timing_sync;
5920 
5921 		dm_new_crtc_state->abm_level = dm_new_conn_state->abm_level;
5922 
5923 		ret = amdgpu_dm_fill_hdr_info_packet(drm_new_conn_state,
5924 					   &new_stream->hdr_static_metadata);
5925 		if (ret)
5926 			goto fail;
5927 
5928 		/*
5929 		 * If we already removed the old stream from the context
5930 		 * (and set the new stream to NULL) then we can't reuse
5931 		 * the old stream even if the stream and scaling are unchanged.
5932 		 * We'll hit the BUG_ON and black screen.
5933 		 *
5934 		 * TODO: Refactor this function to allow this check to work
5935 		 * in all conditions.
5936 		 */
5937 		if (amdgpu_freesync_vid_mode &&
5938 		    dm_new_crtc_state->stream &&
5939 		    is_timing_unchanged_for_freesync(new_crtc_state, old_crtc_state))
5940 			goto skip_modeset;
5941 
5942 		if (dm_new_crtc_state->stream &&
5943 		    dc_is_stream_unchanged(new_stream, dm_old_crtc_state->stream) &&
5944 		    dc_is_stream_scaling_unchanged(new_stream, dm_old_crtc_state->stream)) {
5945 			new_crtc_state->mode_changed = false;
5946 			drm_dbg_driver(adev_to_drm(adev), "Mode change not required, setting mode_changed to %d",
5947 					 new_crtc_state->mode_changed);
5948 		}
5949 	}
5950 
5951 	/* mode_changed flag may get updated above, need to check again */
5952 	if (!drm_atomic_crtc_needs_modeset(new_crtc_state))
5953 		goto skip_modeset;
5954 
5955 	drm_dbg_state(state->dev,
5956 		"amdgpu_crtc id:%d crtc_state_flags: enable:%d, active:%d, planes_changed:%d, mode_changed:%d,active_changed:%d,connectors_changed:%d\n",
5957 		acrtc->crtc_id,
5958 		new_crtc_state->enable,
5959 		new_crtc_state->active,
5960 		new_crtc_state->planes_changed,
5961 		new_crtc_state->mode_changed,
5962 		new_crtc_state->active_changed,
5963 		new_crtc_state->connectors_changed);
5964 
5965 	/* Remove stream for any changed/disabled CRTC */
5966 	if (!enable) {
5967 
5968 		if (!dm_old_crtc_state->stream)
5969 			goto skip_modeset;
5970 
5971 		/* Unset freesync video if it was active before */
5972 		if (dm_old_crtc_state->freesync_config.state == VRR_STATE_ACTIVE_FIXED) {
5973 			dm_new_crtc_state->freesync_config.state = VRR_STATE_INACTIVE;
5974 			dm_new_crtc_state->freesync_config.fixed_refresh_in_uhz = 0;
5975 		}
5976 
5977 		/* Now check if we should set freesync video mode */
5978 		if (amdgpu_freesync_vid_mode && dm_new_crtc_state->stream &&
5979 		    dc_is_stream_unchanged(new_stream, dm_old_crtc_state->stream) &&
5980 		    dc_is_stream_scaling_unchanged(new_stream, dm_old_crtc_state->stream) &&
5981 		    is_timing_unchanged_for_freesync(new_crtc_state,
5982 						     old_crtc_state)) {
5983 			new_crtc_state->mode_changed = false;
5984 			drm_dbg_driver(adev_to_drm(adev),
5985 				"Mode change not required for front porch change, setting mode_changed to %d",
5986 				new_crtc_state->mode_changed);
5987 
5988 			set_freesync_fixed_config(dm_new_crtc_state);
5989 
5990 			goto skip_modeset;
5991 		} else if (amdgpu_freesync_vid_mode && aconnector &&
5992 			   amdgpu_dm_is_freesync_video_mode(&new_crtc_state->mode,
5993 						  aconnector)) {
5994 			struct drm_display_mode *high_mode;
5995 
5996 			high_mode = amdgpu_dm_get_highest_refresh_rate_mode(aconnector, false);
5997 			if (!drm_mode_equal(&new_crtc_state->mode, high_mode))
5998 				set_freesync_fixed_config(dm_new_crtc_state);
5999 		}
6000 
6001 		ret = dm_atomic_get_state(state, &dm_state);
6002 		if (ret)
6003 			goto fail;
6004 
6005 		drm_dbg_driver(adev_to_drm(adev), "Disabling DRM crtc: %d\n",
6006 				crtc->base.id);
6007 
6008 		/* i.e. reset mode */
6009 		if (dc_state_remove_stream(
6010 				dm->dc,
6011 				dm_state->context,
6012 				dm_old_crtc_state->stream) != DC_OK) {
6013 			ret = -EINVAL;
6014 			goto fail;
6015 		}
6016 
6017 		dc_stream_release(dm_old_crtc_state->stream);
6018 		dm_new_crtc_state->stream = NULL;
6019 
6020 		reset_freesync_config_for_crtc(dm_new_crtc_state);
6021 
6022 		*lock_and_validation_needed = true;
6023 
6024 	} else {/* Add stream for any updated/enabled CRTC */
6025 		/*
6026 		 * Quick fix to prevent NULL pointer on new_stream when
6027 		 * added MST connectors not found in existing crtc_state in the chained mode
6028 		 * TODO: need to dig out the root cause of that
6029 		 */
6030 		if (!connector)
6031 			goto skip_modeset;
6032 
6033 		if (modereset_required(new_crtc_state))
6034 			goto skip_modeset;
6035 
6036 		if (amdgpu_dm_crtc_modeset_required(new_crtc_state, new_stream,
6037 				     dm_old_crtc_state->stream)) {
6038 
6039 			WARN_ON(dm_new_crtc_state->stream);
6040 
6041 			ret = dm_atomic_get_state(state, &dm_state);
6042 			if (ret)
6043 				goto fail;
6044 
6045 			dm_new_crtc_state->stream = new_stream;
6046 
6047 			dc_stream_retain(new_stream);
6048 
6049 			drm_dbg_atomic(adev_to_drm(adev), "Enabling DRM crtc: %d\n",
6050 					 crtc->base.id);
6051 
6052 			if (dc_state_add_stream(
6053 					dm->dc,
6054 					dm_state->context,
6055 					dm_new_crtc_state->stream) != DC_OK) {
6056 				ret = -EINVAL;
6057 				goto fail;
6058 			}
6059 
6060 			*lock_and_validation_needed = true;
6061 		}
6062 	}
6063 
6064 skip_modeset:
6065 	/* Release extra reference */
6066 	if (new_stream)
6067 		dc_stream_release(new_stream);
6068 	new_stream = NULL;
6069 
6070 	/*
6071 	 * We want to do dc stream updates that do not require a
6072 	 * full modeset below.
6073 	 */
6074 	if (!(enable && connector && new_crtc_state->active))
6075 		return 0;
6076 	/*
6077 	 * Given above conditions, the dc state cannot be NULL because:
6078 	 * 1. We're in the process of enabling CRTCs (just been added
6079 	 *    to the dc context, or already is on the context)
6080 	 * 2. Has a valid connector attached, and
6081 	 * 3. Is currently active and enabled.
6082 	 * => The dc stream state currently exists.
6083 	 */
6084 	BUG_ON(dm_new_crtc_state->stream == NULL);
6085 
6086 	/* Scaling or underscan settings */
6087 	if (is_scaling_state_different(dm_old_conn_state, dm_new_conn_state) ||
6088 				drm_atomic_crtc_needs_modeset(new_crtc_state))
6089 		amdgpu_dm_update_stream_scaling_settings(adev_to_drm(adev),
6090 			&new_crtc_state->mode, dm_new_conn_state, dm_new_crtc_state->stream);
6091 
6092 	/* ABM settings */
6093 	dm_new_crtc_state->abm_level = dm_new_conn_state->abm_level;
6094 
6095 	/*
6096 	 * Color management settings. We also update color properties
6097 	 * when a modeset is needed, to ensure it gets reprogrammed.
6098 	 */
6099 	if (dm_new_crtc_state->base.color_mgmt_changed ||
6100 	    dm_old_crtc_state->regamma_tf != dm_new_crtc_state->regamma_tf ||
6101 	    drm_atomic_crtc_needs_modeset(new_crtc_state)) {
6102 		ret = amdgpu_dm_check_crtc_color_mgmt(dm_new_crtc_state, true);
6103 		if (ret)
6104 			goto fail;
6105 	}
6106 
6107 	/* Update Freesync settings. */
6108 	get_freesync_config_for_crtc(dm_new_crtc_state,
6109 				     dm_new_conn_state);
6110 
6111 	return ret;
6112 
6113 fail:
6114 	if (new_stream)
6115 		dc_stream_release(new_stream);
6116 	return ret;
6117 }
6118 
6119 static bool should_reset_plane(struct drm_atomic_commit *state,
6120 			       struct drm_plane *plane,
6121 			       struct drm_plane_state *old_plane_state,
6122 			       struct drm_plane_state *new_plane_state)
6123 {
6124 	struct drm_plane *other;
6125 	struct drm_plane_state *old_other_state, *new_other_state;
6126 	struct drm_crtc_state *old_crtc_state, *new_crtc_state;
6127 	struct dm_crtc_state *old_dm_crtc_state, *new_dm_crtc_state;
6128 	struct amdgpu_device *adev = drm_to_adev(plane->dev);
6129 	struct drm_connector_state *new_con_state;
6130 	struct drm_connector *connector;
6131 	int i;
6132 
6133 	/*
6134 	 * TODO: Remove this hack for all asics once it proves that the
6135 	 * fast updates works fine on DCN3.2+.
6136 	 */
6137 	if (amdgpu_ip_version(adev, DCE_HWIP, 0) < IP_VERSION(3, 2, 0) &&
6138 	    state->allow_modeset)
6139 		return true;
6140 
6141 	/* Check for writeback commit */
6142 	for_each_new_connector_in_state(state, connector, new_con_state, i) {
6143 		if (connector->connector_type != DRM_MODE_CONNECTOR_WRITEBACK)
6144 			continue;
6145 
6146 		if (new_con_state->writeback_job)
6147 			return true;
6148 	}
6149 
6150 	if (amdgpu_in_reset(adev) && state->allow_modeset)
6151 		return true;
6152 
6153 	/* Exit early if we know that we're adding or removing the plane. */
6154 	if (old_plane_state->crtc != new_plane_state->crtc)
6155 		return true;
6156 
6157 	/* old crtc == new_crtc == NULL, plane not in context. */
6158 	if (!new_plane_state->crtc)
6159 		return false;
6160 
6161 	new_crtc_state =
6162 		drm_atomic_get_new_crtc_state(state, new_plane_state->crtc);
6163 	old_crtc_state =
6164 		drm_atomic_get_old_crtc_state(state, old_plane_state->crtc);
6165 
6166 	if (!new_crtc_state)
6167 		return true;
6168 
6169 	/*
6170 	 * A change in cursor mode means a new dc pipe needs to be acquired or
6171 	 * released from the state
6172 	 */
6173 	old_dm_crtc_state = to_dm_crtc_state(old_crtc_state);
6174 	new_dm_crtc_state = to_dm_crtc_state(new_crtc_state);
6175 	if (plane->type == DRM_PLANE_TYPE_CURSOR &&
6176 	    old_dm_crtc_state != NULL &&
6177 	    old_dm_crtc_state->cursor_mode != new_dm_crtc_state->cursor_mode) {
6178 		return true;
6179 	}
6180 
6181 	/* CRTC Degamma changes currently require us to recreate planes. */
6182 	if (new_crtc_state->color_mgmt_changed)
6183 		return true;
6184 
6185 	/*
6186 	 * On zpos change, planes need to be reordered by removing and re-adding
6187 	 * them one by one to the dc state, in order of descending zpos.
6188 	 *
6189 	 * TODO: We can likely skip bandwidth validation if the only thing that
6190 	 * changed about the plane was it'z z-ordering.
6191 	 */
6192 	if (old_plane_state->normalized_zpos != new_plane_state->normalized_zpos)
6193 		return true;
6194 
6195 	if (drm_atomic_crtc_needs_modeset(new_crtc_state))
6196 		return true;
6197 
6198 	/*
6199 	 * If there are any new primary or overlay planes being added or
6200 	 * removed then the z-order can potentially change. To ensure
6201 	 * correct z-order and pipe acquisition the current DC architecture
6202 	 * requires us to remove and recreate all existing planes.
6203 	 *
6204 	 * TODO: Come up with a more elegant solution for this.
6205 	 */
6206 	for_each_oldnew_plane_in_state(state, other, old_other_state, new_other_state, i) {
6207 		struct amdgpu_framebuffer *old_afb, *new_afb;
6208 		struct dm_plane_state *dm_new_other_state, *dm_old_other_state;
6209 
6210 		dm_new_other_state = to_dm_plane_state(new_other_state);
6211 		dm_old_other_state = to_dm_plane_state(old_other_state);
6212 
6213 		if (other->type == DRM_PLANE_TYPE_CURSOR)
6214 			continue;
6215 
6216 		if (old_other_state->crtc != new_plane_state->crtc &&
6217 		    new_other_state->crtc != new_plane_state->crtc)
6218 			continue;
6219 
6220 		if (old_other_state->crtc != new_other_state->crtc)
6221 			return true;
6222 
6223 		/* Src/dst size and scaling updates. */
6224 		if (old_other_state->src_w != new_other_state->src_w ||
6225 		    old_other_state->src_h != new_other_state->src_h ||
6226 		    old_other_state->crtc_w != new_other_state->crtc_w ||
6227 		    old_other_state->crtc_h != new_other_state->crtc_h)
6228 			return true;
6229 
6230 		/* Rotation / mirroring updates. */
6231 		if (old_other_state->rotation != new_other_state->rotation)
6232 			return true;
6233 
6234 		/* Blending updates. */
6235 		if (old_other_state->pixel_blend_mode !=
6236 		    new_other_state->pixel_blend_mode)
6237 			return true;
6238 
6239 		/* Alpha updates. */
6240 		if (old_other_state->alpha != new_other_state->alpha)
6241 			return true;
6242 
6243 		/* Colorspace changes. */
6244 		if (old_other_state->color_range != new_other_state->color_range ||
6245 		    old_other_state->color_encoding != new_other_state->color_encoding)
6246 			return true;
6247 
6248 		/* HDR/Transfer Function changes. */
6249 		if (dm_old_other_state->degamma_tf != dm_new_other_state->degamma_tf ||
6250 		    dm_old_other_state->degamma_lut != dm_new_other_state->degamma_lut ||
6251 		    dm_old_other_state->hdr_mult != dm_new_other_state->hdr_mult ||
6252 		    dm_old_other_state->ctm != dm_new_other_state->ctm ||
6253 		    dm_old_other_state->shaper_lut != dm_new_other_state->shaper_lut ||
6254 		    dm_old_other_state->shaper_tf != dm_new_other_state->shaper_tf ||
6255 		    dm_old_other_state->lut3d != dm_new_other_state->lut3d ||
6256 		    dm_old_other_state->blend_lut != dm_new_other_state->blend_lut ||
6257 		    dm_old_other_state->blend_tf != dm_new_other_state->blend_tf)
6258 			return true;
6259 
6260 		/* Framebuffer checks fall at the end. */
6261 		if (!old_other_state->fb || !new_other_state->fb)
6262 			continue;
6263 
6264 		/* Pixel format changes can require bandwidth updates. */
6265 		if (old_other_state->fb->format != new_other_state->fb->format)
6266 			return true;
6267 
6268 		old_afb = (struct amdgpu_framebuffer *)old_other_state->fb;
6269 		new_afb = (struct amdgpu_framebuffer *)new_other_state->fb;
6270 
6271 		/* Tiling and DCC changes also require bandwidth updates. */
6272 		if (old_afb->tiling_flags != new_afb->tiling_flags ||
6273 		    old_afb->base.modifier != new_afb->base.modifier)
6274 			return true;
6275 	}
6276 
6277 	return false;
6278 }
6279 
6280 static int dm_check_cursor_fb(struct amdgpu_crtc *new_acrtc,
6281 			      struct drm_plane_state *new_plane_state,
6282 			      struct drm_framebuffer *fb)
6283 {
6284 	struct amdgpu_device *adev = drm_to_adev(new_acrtc->base.dev);
6285 	struct amdgpu_framebuffer *afb = to_amdgpu_framebuffer(fb);
6286 	unsigned int pitch;
6287 	bool linear;
6288 
6289 	if (fb->width > new_acrtc->max_cursor_width ||
6290 	    fb->height > new_acrtc->max_cursor_height) {
6291 		drm_dbg_atomic(adev_to_drm(adev), "Bad cursor FB size %dx%d\n",
6292 				 new_plane_state->fb->width,
6293 				 new_plane_state->fb->height);
6294 		return -EINVAL;
6295 	}
6296 	if (new_plane_state->src_w != fb->width << 16 ||
6297 	    new_plane_state->src_h != fb->height << 16) {
6298 		drm_dbg_atomic(adev_to_drm(adev), "Cropping not supported for cursor plane\n");
6299 		return -EINVAL;
6300 	}
6301 
6302 	/* Pitch in pixels */
6303 	pitch = fb->pitches[0] / fb->format->cpp[0];
6304 
6305 	if (fb->width != pitch) {
6306 		drm_dbg_atomic(adev_to_drm(adev), "Cursor FB width %d doesn't match pitch %d",
6307 				 fb->width, pitch);
6308 		return -EINVAL;
6309 	}
6310 
6311 	switch (pitch) {
6312 	case 64:
6313 	case 128:
6314 	case 256:
6315 		/* FB pitch is supported by cursor plane */
6316 		break;
6317 	default:
6318 		drm_dbg_atomic(adev_to_drm(adev), "Bad cursor FB pitch %d px\n", pitch);
6319 		return -EINVAL;
6320 	}
6321 
6322 	/* Core DRM takes care of checking FB modifiers, so we only need to
6323 	 * check tiling flags when the FB doesn't have a modifier.
6324 	 */
6325 	if (!(fb->flags & DRM_MODE_FB_MODIFIERS)) {
6326 		if (adev->family == AMDGPU_FAMILY_GC_12_0_0) {
6327 			linear = AMDGPU_TILING_GET(afb->tiling_flags, GFX12_SWIZZLE_MODE) == 0;
6328 		} else if (adev->family >= AMDGPU_FAMILY_AI) {
6329 			linear = AMDGPU_TILING_GET(afb->tiling_flags, SWIZZLE_MODE) == 0;
6330 		} else {
6331 			linear = AMDGPU_TILING_GET(afb->tiling_flags, ARRAY_MODE) != DC_ARRAY_2D_TILED_THIN1 &&
6332 				 AMDGPU_TILING_GET(afb->tiling_flags, ARRAY_MODE) != DC_ARRAY_1D_TILED_THIN1 &&
6333 				 AMDGPU_TILING_GET(afb->tiling_flags, MICRO_TILE_MODE) == 0;
6334 		}
6335 		if (!linear) {
6336 			drm_dbg_atomic(adev_to_drm(adev), "Cursor FB not linear");
6337 			return -EINVAL;
6338 		}
6339 	}
6340 
6341 	return 0;
6342 }
6343 
6344 /*
6345  * Helper function for checking the cursor in native mode
6346  */
6347 static int dm_check_native_cursor_state(struct drm_crtc *new_plane_crtc,
6348 					struct drm_plane *plane,
6349 					struct drm_plane_state *new_plane_state,
6350 					bool enable)
6351 {
6352 
6353 	struct amdgpu_crtc *new_acrtc;
6354 	int ret;
6355 
6356 	if (!enable || !new_plane_crtc ||
6357 	    drm_atomic_plane_disabling(plane->state, new_plane_state))
6358 		return 0;
6359 
6360 	new_acrtc = to_amdgpu_crtc(new_plane_crtc);
6361 
6362 	if (new_plane_state->src_x != 0 || new_plane_state->src_y != 0) {
6363 		drm_dbg_atomic(new_plane_crtc->dev, "Cropping not supported for cursor plane\n");
6364 		return -EINVAL;
6365 	}
6366 
6367 	if (new_plane_state->fb) {
6368 		ret = dm_check_cursor_fb(new_acrtc, new_plane_state,
6369 						new_plane_state->fb);
6370 		if (ret)
6371 			return ret;
6372 	}
6373 
6374 	return 0;
6375 }
6376 
6377 STATIC_IFN_KUNIT bool dm_should_update_native_cursor(struct drm_atomic_commit *state,
6378 						     struct drm_crtc *old_plane_crtc,
6379 						     struct drm_crtc *new_plane_crtc,
6380 						     bool enable)
6381 {
6382 	struct drm_crtc_state *old_crtc_state, *new_crtc_state;
6383 	struct dm_crtc_state *dm_old_crtc_state, *dm_new_crtc_state;
6384 
6385 	if (!enable) {
6386 		if (old_plane_crtc == NULL)
6387 			return true;
6388 
6389 		old_crtc_state = drm_atomic_get_old_crtc_state(
6390 			state, old_plane_crtc);
6391 		dm_old_crtc_state = to_dm_crtc_state(old_crtc_state);
6392 
6393 		return dm_old_crtc_state->cursor_mode == DM_CURSOR_NATIVE_MODE;
6394 	} else {
6395 		if (new_plane_crtc == NULL)
6396 			return true;
6397 
6398 		new_crtc_state = drm_atomic_get_new_crtc_state(
6399 			state, new_plane_crtc);
6400 		dm_new_crtc_state = to_dm_crtc_state(new_crtc_state);
6401 
6402 		return dm_new_crtc_state->cursor_mode == DM_CURSOR_NATIVE_MODE;
6403 	}
6404 }
6405 EXPORT_IF_KUNIT(dm_should_update_native_cursor);
6406 
6407 static int dm_update_plane_state(struct dc *dc,
6408 				 struct drm_atomic_commit *state,
6409 				 struct drm_plane *plane,
6410 				 struct drm_plane_state *old_plane_state,
6411 				 struct drm_plane_state *new_plane_state,
6412 				 bool enable,
6413 				 bool *lock_and_validation_needed,
6414 				 bool *is_top_most_overlay)
6415 {
6416 
6417 	struct dm_atomic_state *dm_state = NULL;
6418 	struct drm_crtc *new_plane_crtc, *old_plane_crtc;
6419 	struct drm_crtc_state *old_crtc_state, *new_crtc_state;
6420 	struct dm_crtc_state *dm_new_crtc_state, *dm_old_crtc_state;
6421 	struct dm_plane_state *dm_new_plane_state, *dm_old_plane_state;
6422 	bool needs_reset, update_native_cursor;
6423 	int ret = 0;
6424 
6425 
6426 	new_plane_crtc = new_plane_state->crtc;
6427 	old_plane_crtc = old_plane_state->crtc;
6428 	dm_new_plane_state = to_dm_plane_state(new_plane_state);
6429 	dm_old_plane_state = to_dm_plane_state(old_plane_state);
6430 
6431 	update_native_cursor = dm_should_update_native_cursor(state,
6432 							      old_plane_crtc,
6433 							      new_plane_crtc,
6434 							      enable);
6435 
6436 	if (plane->type == DRM_PLANE_TYPE_CURSOR && update_native_cursor) {
6437 		ret = dm_check_native_cursor_state(new_plane_crtc, plane,
6438 						    new_plane_state, enable);
6439 		if (ret)
6440 			return ret;
6441 
6442 		return 0;
6443 	}
6444 
6445 	needs_reset = should_reset_plane(state, plane, old_plane_state,
6446 					 new_plane_state);
6447 
6448 	/* Remove any changed/removed planes */
6449 	if (!enable) {
6450 		if (!needs_reset)
6451 			return 0;
6452 
6453 		if (!old_plane_crtc)
6454 			return 0;
6455 
6456 		old_crtc_state = drm_atomic_get_old_crtc_state(
6457 				state, old_plane_crtc);
6458 		dm_old_crtc_state = to_dm_crtc_state(old_crtc_state);
6459 
6460 		if (!dm_old_crtc_state->stream)
6461 			return 0;
6462 
6463 		drm_dbg_atomic(old_plane_crtc->dev, "Disabling DRM plane: %d on DRM crtc %d\n",
6464 				plane->base.id, old_plane_crtc->base.id);
6465 
6466 		ret = dm_atomic_get_state(state, &dm_state);
6467 		if (ret)
6468 			return ret;
6469 
6470 		if (!dc_state_remove_plane(
6471 				dc,
6472 				dm_old_crtc_state->stream,
6473 				dm_old_plane_state->dc_state,
6474 				dm_state->context)) {
6475 
6476 			return -EINVAL;
6477 		}
6478 
6479 		if (dm_old_plane_state->dc_state)
6480 			dc_plane_state_release(dm_old_plane_state->dc_state);
6481 
6482 		dm_new_plane_state->dc_state = NULL;
6483 
6484 		*lock_and_validation_needed = true;
6485 
6486 	} else { /* Add new planes */
6487 		struct dc_plane_state *dc_new_plane_state;
6488 
6489 		if (drm_atomic_plane_disabling(plane->state, new_plane_state))
6490 			return 0;
6491 
6492 		if (!new_plane_crtc)
6493 			return 0;
6494 
6495 		new_crtc_state = drm_atomic_get_new_crtc_state(state, new_plane_crtc);
6496 		dm_new_crtc_state = to_dm_crtc_state(new_crtc_state);
6497 
6498 		if (!dm_new_crtc_state->stream)
6499 			return 0;
6500 
6501 		if (!needs_reset)
6502 			return 0;
6503 
6504 		ret = amdgpu_dm_plane_helper_check_state(new_plane_state, new_crtc_state);
6505 		if (ret)
6506 			goto out;
6507 
6508 		WARN_ON(dm_new_plane_state->dc_state);
6509 
6510 		dc_new_plane_state = dc_create_plane_state(dc);
6511 		if (!dc_new_plane_state) {
6512 			ret = -ENOMEM;
6513 			goto out;
6514 		}
6515 
6516 		drm_dbg_atomic(new_plane_crtc->dev, "Enabling DRM plane: %d on DRM crtc %d\n",
6517 				 plane->base.id, new_plane_crtc->base.id);
6518 
6519 		ret = fill_dc_plane_attributes(
6520 			drm_to_adev(new_plane_crtc->dev),
6521 			dc_new_plane_state,
6522 			new_plane_state,
6523 			new_crtc_state);
6524 		if (ret) {
6525 			dc_plane_state_release(dc_new_plane_state);
6526 			goto out;
6527 		}
6528 
6529 		ret = dm_atomic_get_state(state, &dm_state);
6530 		if (ret) {
6531 			dc_plane_state_release(dc_new_plane_state);
6532 			goto out;
6533 		}
6534 
6535 		/*
6536 		 * Any atomic check errors that occur after this will
6537 		 * not need a release. The plane state will be attached
6538 		 * to the stream, and therefore part of the atomic
6539 		 * state. It'll be released when the atomic state is
6540 		 * cleaned.
6541 		 */
6542 		if (!dc_state_add_plane(
6543 				dc,
6544 				dm_new_crtc_state->stream,
6545 				dc_new_plane_state,
6546 				dm_state->context)) {
6547 
6548 			dc_plane_state_release(dc_new_plane_state);
6549 			ret = -EINVAL;
6550 			goto out;
6551 		}
6552 
6553 		dm_new_plane_state->dc_state = dc_new_plane_state;
6554 
6555 		dm_new_crtc_state->mpo_requested |= (plane->type == DRM_PLANE_TYPE_OVERLAY);
6556 
6557 		/* Tell DC to do a full surface update every time there
6558 		 * is a plane change. Inefficient, but works for now.
6559 		 */
6560 		dm_new_plane_state->dc_state->update_bits.full_update = 1;
6561 
6562 		*lock_and_validation_needed = true;
6563 	}
6564 
6565 out:
6566 	/* If enabling cursor overlay failed, attempt fallback to native mode */
6567 	if (enable && ret == -EINVAL && plane->type == DRM_PLANE_TYPE_CURSOR) {
6568 		ret = dm_check_native_cursor_state(new_plane_crtc, plane,
6569 						    new_plane_state, enable);
6570 		if (ret)
6571 			return ret;
6572 
6573 		dm_new_crtc_state->cursor_mode = DM_CURSOR_NATIVE_MODE;
6574 	}
6575 
6576 	return ret;
6577 }
6578 
6579 STATIC_IFN_KUNIT void dm_get_oriented_plane_size(struct drm_plane_state *plane_state,
6580 					 int *src_w, int *src_h)
6581 {
6582 	switch (plane_state->rotation & DRM_MODE_ROTATE_MASK) {
6583 	case DRM_MODE_ROTATE_90:
6584 	case DRM_MODE_ROTATE_270:
6585 		*src_w = plane_state->src_h >> 16;
6586 		*src_h = plane_state->src_w >> 16;
6587 		break;
6588 	case DRM_MODE_ROTATE_0:
6589 	case DRM_MODE_ROTATE_180:
6590 	default:
6591 		*src_w = plane_state->src_w >> 16;
6592 		*src_h = plane_state->src_h >> 16;
6593 		break;
6594 	}
6595 }
6596 EXPORT_IF_KUNIT(dm_get_oriented_plane_size);
6597 
6598 STATIC_IFN_KUNIT void
6599 dm_get_plane_scale(struct drm_plane_state *plane_state,
6600 		   int *out_plane_scale_w, int *out_plane_scale_h)
6601 {
6602 	int plane_src_w, plane_src_h;
6603 
6604 	dm_get_oriented_plane_size(plane_state, &plane_src_w, &plane_src_h);
6605 	*out_plane_scale_w = plane_src_w ? plane_state->crtc_w * 1000 / plane_src_w : 0;
6606 	*out_plane_scale_h = plane_src_h ? plane_state->crtc_h * 1000 / plane_src_h : 0;
6607 }
6608 EXPORT_IF_KUNIT(dm_get_plane_scale);
6609 
6610 /*
6611  * The normalized_zpos value cannot be used by this iterator directly. It's only
6612  * calculated for enabled planes, potentially causing normalized_zpos collisions
6613  * between enabled/disabled planes in the atomic state. We need a unique value
6614  * so that the iterator will not generate the same object twice, or loop
6615  * indefinitely.
6616  */
6617 static inline struct __drm_planes_state *__get_next_zpos(
6618 	struct drm_atomic_commit *state,
6619 	struct __drm_planes_state *prev)
6620 {
6621 	unsigned int highest_zpos = 0, prev_zpos = 256;
6622 	uint32_t highest_id = 0, prev_id = UINT_MAX;
6623 	struct drm_plane_state *new_plane_state;
6624 	struct drm_plane *plane;
6625 	int i, highest_i = -1;
6626 
6627 	if (prev != NULL) {
6628 		prev_zpos = prev->new_state->zpos;
6629 		prev_id = prev->ptr->base.id;
6630 	}
6631 
6632 	for_each_new_plane_in_state(state, plane, new_plane_state, i) {
6633 		/* Skip planes with higher zpos than the previously returned */
6634 		if (new_plane_state->zpos > prev_zpos ||
6635 		    (new_plane_state->zpos == prev_zpos &&
6636 		     plane->base.id >= prev_id))
6637 			continue;
6638 
6639 		/* Save the index of the plane with highest zpos */
6640 		if (new_plane_state->zpos > highest_zpos ||
6641 		    (new_plane_state->zpos == highest_zpos &&
6642 		     plane->base.id > highest_id)) {
6643 			highest_zpos = new_plane_state->zpos;
6644 			highest_id = plane->base.id;
6645 			highest_i = i;
6646 		}
6647 	}
6648 
6649 	if (highest_i < 0)
6650 		return NULL;
6651 
6652 	return &state->planes[highest_i];
6653 }
6654 
6655 /*
6656  * Use the uniqueness of the plane's (zpos, drm obj ID) combination to iterate
6657  * by descending zpos, as read from the new plane state. This is the same
6658  * ordering as defined by drm_atomic_normalize_zpos().
6659  */
6660 #define for_each_oldnew_plane_in_descending_zpos(__state, plane, old_plane_state, new_plane_state) \
6661 	for (struct __drm_planes_state *__i = __get_next_zpos((__state), NULL); \
6662 	     __i != NULL; __i = __get_next_zpos((__state), __i))		\
6663 		for_each_if(((plane) = __i->ptr,				\
6664 			     (void)(plane) /* Only to avoid unused-but-set-variable warning */, \
6665 			     (old_plane_state) = __i->old_state,		\
6666 			     (new_plane_state) = __i->new_state, 1))
6667 
6668 static int add_affected_mst_dsc_crtcs(struct drm_atomic_commit *state, struct drm_crtc *crtc)
6669 {
6670 	struct drm_connector *connector;
6671 	struct drm_connector_state *conn_state, *old_conn_state;
6672 	struct amdgpu_dm_connector *aconnector = NULL;
6673 	int i;
6674 
6675 	for_each_oldnew_connector_in_state(state, connector, old_conn_state, conn_state, i) {
6676 		if (!conn_state->crtc)
6677 			conn_state = old_conn_state;
6678 
6679 		if (conn_state->crtc != crtc)
6680 			continue;
6681 
6682 		if (connector->connector_type == DRM_MODE_CONNECTOR_WRITEBACK)
6683 			continue;
6684 
6685 		aconnector = to_amdgpu_dm_connector(connector);
6686 		if (!aconnector->mst_output_port || !aconnector->mst_root)
6687 			aconnector = NULL;
6688 		else
6689 			break;
6690 	}
6691 
6692 	if (!aconnector)
6693 		return 0;
6694 
6695 	return drm_dp_mst_add_affected_dsc_crtcs(state, &aconnector->mst_root->mst_mgr);
6696 }
6697 
6698 /**
6699  * DOC: Cursor Modes - Native vs Overlay
6700  *
6701  * In native mode, the cursor uses a integrated cursor pipe within each DCN hw
6702  * plane. It does not require a dedicated hw plane to enable, but it is
6703  * subjected to the same z-order and scaling as the hw plane. It also has format
6704  * restrictions, a RGB cursor in native mode cannot be enabled within a non-RGB
6705  * hw plane.
6706  *
6707  * In overlay mode, the cursor uses a separate DCN hw plane, and thus has its
6708  * own scaling and z-pos. It also has no blending restrictions. It lends to a
6709  * cursor behavior more akin to a DRM client's expectations. However, it does
6710  * occupy an extra DCN plane, and therefore will only be used if a DCN plane is
6711  * available.
6712  */
6713 
6714 /**
6715  * dm_plane_color_pipeline_active() - Check if a plane's color pipeline active.
6716  * @state: DRM atomic state
6717  * @plane: DRM plane to check
6718  * @use_old: if true, inspect the old colorop states; otherwise the new ones
6719  *
6720  * A color pipeline may be selected (color_pipeline != NULL) but still is
6721  * inactive if every colorop in the chain is bypassed.  Only return
6722  * true when at least one colorop has bypass == false, meaning the cursor
6723  * would be subjected to the transformation in native mode.
6724  *
6725  * Return: true if the pipeline modifies pixels, false otherwise.
6726  */
6727 static bool dm_plane_color_pipeline_active(struct drm_atomic_commit *state,
6728 					   struct drm_plane *plane,
6729 					   bool use_old)
6730 {
6731 	struct drm_colorop *colorop;
6732 	struct drm_colorop_state *old_colorop_state, *new_colorop_state;
6733 	int i;
6734 
6735 	for_each_oldnew_colorop_in_state(state, colorop, old_colorop_state, new_colorop_state, i) {
6736 		struct drm_colorop_state *cstate = use_old ? old_colorop_state : new_colorop_state;
6737 
6738 		if (cstate->colorop->plane != plane)
6739 			continue;
6740 		if (!cstate->bypass)
6741 			return true;
6742 	}
6743 	return false;
6744 }
6745 
6746 /**
6747  * dm_crtc_get_cursor_mode() - Determine the required cursor mode on crtc
6748  * @adev: amdgpu device
6749  * @state: DRM atomic state
6750  * @dm_crtc_state: amdgpu state for the CRTC containing the cursor
6751  * @cursor_mode: Returns the required cursor mode on dm_crtc_state
6752  *
6753  * Get whether the cursor should be enabled in native mode, or overlay mode, on
6754  * the dm_crtc_state.
6755  *
6756  * The cursor should be enabled in overlay mode if there exists an underlying
6757  * plane - on which the cursor may be blended - that is either YUV formatted,
6758  * scaled differently from the cursor, or has a color pipeline active.
6759  *
6760  * Since zpos info is required, drm_atomic_normalize_zpos must be called before
6761  * calling this function.
6762  *
6763  * Return: 0 on success, or an error code if getting the cursor plane state
6764  * failed.
6765  */
6766 static int dm_crtc_get_cursor_mode(struct amdgpu_device *adev,
6767 				   struct drm_atomic_commit *state,
6768 				   struct dm_crtc_state *dm_crtc_state,
6769 				   enum amdgpu_dm_cursor_mode *cursor_mode)
6770 {
6771 	struct drm_plane_state *old_plane_state, *plane_state, *cursor_state;
6772 	struct drm_crtc_state *crtc_state = &dm_crtc_state->base;
6773 	struct drm_plane *plane;
6774 	bool consider_mode_change = false;
6775 	bool entire_crtc_covered = false;
6776 	bool cursor_changed = false;
6777 	int underlying_scale_w, underlying_scale_h;
6778 	int cursor_scale_w, cursor_scale_h;
6779 	int i;
6780 
6781 	/* Overlay cursor not supported on HW before DCN
6782 	 * DCN401/420 does not have the cursor-on-scaled-plane or cursor-on-yuv-plane restrictions
6783 	 * as previous DCN generations, so enable native mode on DCN401/420
6784 	 *
6785 	 * Always set native cursor mode when the CRTC is disabled,
6786 	 * to make sure it doesn't cause atomic commits to fail when
6787 	 * they are trying to disable the CRTC.
6788 	 */
6789 	if (amdgpu_ip_version(adev, DCE_HWIP, 0) == IP_VERSION(4, 0, 1) ||
6790 	    amdgpu_ip_version(adev, DCE_HWIP, 0) == IP_VERSION(4, 2, 0) ||
6791 	    amdgpu_ip_version(adev, DCE_HWIP, 0) == IP_VERSION(4, 2, 1) ||
6792 	    !dm_crtc_state->base.enable) {
6793 		*cursor_mode = DM_CURSOR_NATIVE_MODE;
6794 		return 0;
6795 	}
6796 
6797 	/* Init cursor_mode to be the same as current */
6798 	*cursor_mode = dm_crtc_state->cursor_mode;
6799 
6800 	/*
6801 	 * Cursor mode can change if a plane's format changes, scale changes, is
6802 	 * enabled/disabled, z-order changes, or color management properties change.
6803 	 */
6804 	for_each_oldnew_plane_in_state(state, plane, old_plane_state, plane_state, i) {
6805 		int new_scale_w, new_scale_h, old_scale_w, old_scale_h;
6806 
6807 		/* Only care about planes on this CRTC */
6808 		if ((drm_plane_mask(plane) & crtc_state->plane_mask) == 0)
6809 			continue;
6810 
6811 		if (plane->type == DRM_PLANE_TYPE_CURSOR)
6812 			cursor_changed = true;
6813 
6814 		if (drm_atomic_plane_enabling(old_plane_state, plane_state) ||
6815 		    drm_atomic_plane_disabling(old_plane_state, plane_state) ||
6816 		    old_plane_state->fb->format != plane_state->fb->format) {
6817 			consider_mode_change = true;
6818 			break;
6819 		}
6820 
6821 		dm_get_plane_scale(plane_state, &new_scale_w, &new_scale_h);
6822 		dm_get_plane_scale(old_plane_state, &old_scale_w, &old_scale_h);
6823 		if (new_scale_w != old_scale_w || new_scale_h != old_scale_h) {
6824 			consider_mode_change = true;
6825 			break;
6826 		}
6827 
6828 		/*
6829 		 * A non-cursor plane moving or resizing (without a scale change)
6830 		 * changes how much of the CRTC it covers. This can create or
6831 		 * remove a hole under the cursor and thus flip the required
6832 		 * cursor mode (native vs overlay), so its destination rect must
6833 		 * be re-evaluated too.
6834 		 *
6835 		 * The cursor plane itself is deliberately excluded: the cursor
6836 		 * mode depends on the underlying planes' coverage, not on the
6837 		 * cursor's position (see the entire_crtc_covered logic below).
6838 		 * Triggering on cursor movement would force every legacy cursor
6839 		 * update off its fast path, and in a cursor-only commit - where
6840 		 * the underlying planes are not part of the state - the coverage
6841 		 * loop would see no covering plane and misevaluate the mode as
6842 		 * overlay, regressing flip-vs-cursor-legacy.
6843 		 */
6844 		if (plane->type != DRM_PLANE_TYPE_CURSOR &&
6845 		    (old_plane_state->crtc_x != plane_state->crtc_x ||
6846 		     old_plane_state->crtc_y != plane_state->crtc_y ||
6847 		     old_plane_state->crtc_w != plane_state->crtc_w ||
6848 		     old_plane_state->crtc_h != plane_state->crtc_h)) {
6849 			consider_mode_change = true;
6850 			break;
6851 		}
6852 
6853 		if (dm_plane_color_pipeline_active(state, plane, true) !=
6854 		    dm_plane_color_pipeline_active(state, plane, false)) {
6855 			consider_mode_change = true;
6856 			break;
6857 		}
6858 	}
6859 
6860 	if (!consider_mode_change && !crtc_state->zpos_changed)
6861 		return 0;
6862 
6863 	/*
6864 	 * If no cursor change on this CRTC, and not enabled on this CRTC, then
6865 	 * no need to set cursor mode. This avoids needlessly locking the cursor
6866 	 * state.
6867 	 */
6868 	if (!cursor_changed &&
6869 	    !(drm_plane_mask(crtc_state->crtc->cursor) & crtc_state->plane_mask)) {
6870 		return 0;
6871 	}
6872 
6873 	cursor_state = drm_atomic_get_plane_state(state,
6874 						  crtc_state->crtc->cursor);
6875 	if (IS_ERR(cursor_state))
6876 		return PTR_ERR(cursor_state);
6877 
6878 	/* Cursor is disabled */
6879 	if (!cursor_state->fb)
6880 		return 0;
6881 
6882 	/* For all planes in descending z-order (all of which are below cursor
6883 	 * as per zpos definitions), check their scaling and format
6884 	 */
6885 	for_each_oldnew_plane_in_descending_zpos(state, plane, old_plane_state, plane_state) {
6886 
6887 		/* Only care about non-cursor planes on this CRTC */
6888 		if ((drm_plane_mask(plane) & crtc_state->plane_mask) == 0 ||
6889 		    plane->type == DRM_PLANE_TYPE_CURSOR)
6890 			continue;
6891 
6892 		/* Underlying plane is YUV format - use overlay cursor */
6893 		if (amdgpu_dm_plane_is_video_format(plane_state->fb->format->format)) {
6894 			*cursor_mode = DM_CURSOR_OVERLAY_MODE;
6895 			return 0;
6896 		}
6897 
6898 		/* Underlying plane has an active color pipeline - cursor would be transformed */
6899 		if (dm_plane_color_pipeline_active(state, plane, false)) {
6900 			*cursor_mode = DM_CURSOR_OVERLAY_MODE;
6901 			return 0;
6902 		}
6903 
6904 		dm_get_plane_scale(plane_state,
6905 				   &underlying_scale_w, &underlying_scale_h);
6906 		dm_get_plane_scale(cursor_state,
6907 				   &cursor_scale_w, &cursor_scale_h);
6908 
6909 		/* Underlying plane has different scale - use overlay cursor */
6910 		if (cursor_scale_w != underlying_scale_w &&
6911 		    cursor_scale_h != underlying_scale_h) {
6912 			*cursor_mode = DM_CURSOR_OVERLAY_MODE;
6913 			return 0;
6914 		}
6915 
6916 		/* If this plane covers the whole CRTC, no need to check planes underneath */
6917 		if (plane_state->crtc_x <= 0 && plane_state->crtc_y <= 0 &&
6918 		    plane_state->crtc_x + plane_state->crtc_w >= crtc_state->mode.hdisplay &&
6919 		    plane_state->crtc_y + plane_state->crtc_h >= crtc_state->mode.vdisplay) {
6920 			entire_crtc_covered = true;
6921 			break;
6922 		}
6923 	}
6924 
6925 	/* If planes do not cover the entire CRTC, use overlay mode to enable
6926 	 * cursor over holes
6927 	 */
6928 	if (entire_crtc_covered)
6929 		*cursor_mode = DM_CURSOR_NATIVE_MODE;
6930 	else
6931 		*cursor_mode = DM_CURSOR_OVERLAY_MODE;
6932 
6933 	return 0;
6934 }
6935 
6936 static bool amdgpu_dm_crtc_mem_type_changed(struct drm_device *dev,
6937 					    struct drm_atomic_commit *state,
6938 					    struct drm_crtc_state *crtc_state)
6939 {
6940 	struct drm_plane *plane;
6941 	struct drm_plane_state *new_plane_state, *old_plane_state;
6942 
6943 	drm_for_each_plane_mask(plane, dev, crtc_state->plane_mask) {
6944 		new_plane_state = drm_atomic_get_new_plane_state(state, plane);
6945 		old_plane_state = drm_atomic_get_old_plane_state(state, plane);
6946 
6947 		if (!old_plane_state || !new_plane_state)
6948 			continue;
6949 
6950 		if (old_plane_state->fb && new_plane_state->fb &&
6951 		    get_mem_type(old_plane_state->fb) != get_mem_type(new_plane_state->fb))
6952 			return true;
6953 	}
6954 
6955 	return false;
6956 }
6957 
6958 /**
6959  * amdgpu_dm_atomic_check() - Atomic check implementation for AMDgpu DM.
6960  *
6961  * @dev: The DRM device
6962  * @state: The atomic state to commit
6963  *
6964  * Validate that the given atomic state is programmable by DC into hardware.
6965  * This involves constructing a &struct dc_state reflecting the new hardware
6966  * state we wish to commit, then querying DC to see if it is programmable. It's
6967  * important not to modify the existing DC state. Otherwise, atomic_check
6968  * may unexpectedly commit hardware changes.
6969  *
6970  * When validating the DC state, it's important that the right locks are
6971  * acquired. For full updates case which removes/adds/updates streams on one
6972  * CRTC while flipping on another CRTC, acquiring global lock will guarantee
6973  * that any such full update commit will wait for completion of any outstanding
6974  * flip using DRMs synchronization events.
6975  *
6976  * Note that DM adds the affected connectors for all CRTCs in state, when that
6977  * might not seem necessary. This is because DC stream creation requires the
6978  * DC sink, which is tied to the DRM connector state. Cleaning this up should
6979  * be possible but non-trivial - a possible TODO item.
6980  *
6981  * Return: -Error code if validation failed.
6982  */
6983 static int amdgpu_dm_atomic_check(struct drm_device *dev,
6984 				  struct drm_atomic_commit *state)
6985 {
6986 	struct amdgpu_device *adev = drm_to_adev(dev);
6987 	struct dm_atomic_state *dm_state = NULL;
6988 	struct dc *dc = adev->dm.dc;
6989 	struct drm_connector *connector;
6990 	struct drm_connector_state *old_con_state, *new_con_state;
6991 	struct drm_crtc *crtc;
6992 	struct drm_crtc_state *old_crtc_state, *new_crtc_state;
6993 	struct drm_plane *plane;
6994 	struct drm_plane_state *old_plane_state, *new_plane_state, *new_cursor_state;
6995 	enum dc_status status;
6996 	int ret, i;
6997 	bool lock_and_validation_needed = false;
6998 	bool is_top_most_overlay = true;
6999 	struct dm_crtc_state *dm_old_crtc_state, *dm_new_crtc_state;
7000 	struct drm_dp_mst_topology_mgr *mgr;
7001 	struct drm_dp_mst_topology_state *mst_state;
7002 	struct dsc_mst_fairness_vars vars[MAX_PIPES] = {0};
7003 
7004 	trace_amdgpu_dm_atomic_check_begin(state);
7005 
7006 	ret = drm_atomic_helper_check_modeset(dev, state);
7007 	if (ret) {
7008 		drm_dbg_atomic(dev, "drm_atomic_helper_check_modeset() failed: %pe\n", ERR_PTR(ret));
7009 		goto fail;
7010 	}
7011 
7012 	/* Check connector changes */
7013 	for_each_oldnew_connector_in_state(state, connector, old_con_state, new_con_state, i) {
7014 		struct dm_connector_state *dm_old_con_state = to_dm_connector_state(old_con_state);
7015 		struct dm_connector_state *dm_new_con_state = to_dm_connector_state(new_con_state);
7016 
7017 		/* Skip connectors that are disabled or part of modeset already. */
7018 		if (!new_con_state->crtc)
7019 			continue;
7020 
7021 		new_crtc_state = drm_atomic_get_crtc_state(state, new_con_state->crtc);
7022 		if (IS_ERR(new_crtc_state)) {
7023 			drm_dbg_atomic(dev, "drm_atomic_get_crtc_state() failed: %pe\n", new_crtc_state);
7024 			ret = PTR_ERR(new_crtc_state);
7025 			goto fail;
7026 		}
7027 
7028 		if (dm_old_con_state->abm_level != dm_new_con_state->abm_level ||
7029 		    dm_old_con_state->scaling != dm_new_con_state->scaling)
7030 			new_crtc_state->connectors_changed = true;
7031 	}
7032 
7033 	if (dc_resource_is_dsc_encoding_supported(dc)) {
7034 		for_each_oldnew_crtc_in_state(state, crtc, old_crtc_state, new_crtc_state, i) {
7035 			dm_new_crtc_state = to_dm_crtc_state(new_crtc_state);
7036 			dm_new_crtc_state->mode_changed_independent_from_dsc = new_crtc_state->mode_changed;
7037 		}
7038 
7039 		for_each_oldnew_crtc_in_state(state, crtc, old_crtc_state, new_crtc_state, i) {
7040 			if (drm_atomic_crtc_needs_modeset(new_crtc_state)) {
7041 				ret = add_affected_mst_dsc_crtcs(state, crtc);
7042 				if (ret) {
7043 					drm_dbg_atomic(dev, "add_affected_mst_dsc_crtcs() failed: %pe\n", ERR_PTR(ret));
7044 					goto fail;
7045 				}
7046 			}
7047 		}
7048 	}
7049 	for_each_oldnew_crtc_in_state(state, crtc, old_crtc_state, new_crtc_state, i) {
7050 		dm_old_crtc_state = to_dm_crtc_state(old_crtc_state);
7051 
7052 		if (!drm_atomic_crtc_needs_modeset(new_crtc_state) &&
7053 		    !new_crtc_state->color_mgmt_changed &&
7054 		    old_crtc_state->vrr_enabled == new_crtc_state->vrr_enabled &&
7055 			dm_old_crtc_state->dsc_force_changed == false)
7056 			continue;
7057 
7058 		ret = amdgpu_dm_verify_lut_sizes(new_crtc_state);
7059 		if (ret) {
7060 			drm_dbg_atomic(dev, "amdgpu_dm_verify_lut_sizes() failed: %pe\n", ERR_PTR(ret));
7061 			goto fail;
7062 		}
7063 
7064 		if (!new_crtc_state->enable)
7065 			continue;
7066 
7067 		ret = drm_atomic_add_affected_connectors(state, crtc);
7068 		if (ret) {
7069 			drm_dbg_atomic(dev, "drm_atomic_add_affected_connectors() failed: %pe\n", ERR_PTR(ret));
7070 			goto fail;
7071 		}
7072 
7073 		ret = drm_atomic_add_affected_planes(state, crtc);
7074 		if (ret) {
7075 			drm_dbg_atomic(dev, "drm_atomic_add_affected_planes() failed: %pe\n", ERR_PTR(ret));
7076 			goto fail;
7077 		}
7078 
7079 		if (dm_old_crtc_state->dsc_force_changed)
7080 			new_crtc_state->mode_changed = true;
7081 	}
7082 
7083 	/*
7084 	 * Add all primary and overlay planes on the CRTC to the state
7085 	 * whenever a plane is enabled to maintain correct z-ordering
7086 	 * and to enable fast surface updates.
7087 	 */
7088 	drm_for_each_crtc(crtc, dev) {
7089 		bool modified = false;
7090 
7091 		for_each_oldnew_plane_in_state(state, plane, old_plane_state, new_plane_state, i) {
7092 			if (plane->type == DRM_PLANE_TYPE_CURSOR)
7093 				continue;
7094 
7095 			if (new_plane_state->crtc == crtc ||
7096 			    old_plane_state->crtc == crtc) {
7097 				modified = true;
7098 				break;
7099 			}
7100 		}
7101 
7102 		if (!modified)
7103 			continue;
7104 
7105 		drm_for_each_plane_mask(plane, state->dev, crtc->state->plane_mask) {
7106 			if (plane->type == DRM_PLANE_TYPE_CURSOR)
7107 				continue;
7108 
7109 			new_plane_state =
7110 				drm_atomic_get_plane_state(state, plane);
7111 
7112 			if (IS_ERR(new_plane_state)) {
7113 				ret = PTR_ERR(new_plane_state);
7114 				drm_dbg_atomic(dev, "new_plane_state is BAD: %pe\n", new_plane_state);
7115 				goto fail;
7116 			}
7117 		}
7118 	}
7119 
7120 	/*
7121 	 * DC consults the zpos (layer_index in DC terminology) to determine the
7122 	 * hw plane on which to enable the hw cursor (see
7123 	 * `dcn10_can_pipe_disable_cursor`). By now, all modified planes are in
7124 	 * atomic state, so call drm helper to normalize zpos.
7125 	 */
7126 	ret = drm_atomic_normalize_zpos(dev, state);
7127 	if (ret) {
7128 		drm_dbg(dev, "drm_atomic_normalize_zpos() failed: %pe\n", ERR_PTR(ret));
7129 		goto fail;
7130 	}
7131 
7132 	/*
7133 	 * Determine whether cursors on each CRTC should be enabled in native or
7134 	 * overlay mode.
7135 	 */
7136 	for_each_new_crtc_in_state(state, crtc, new_crtc_state, i) {
7137 		dm_new_crtc_state = to_dm_crtc_state(new_crtc_state);
7138 
7139 		ret = dm_crtc_get_cursor_mode(adev, state, dm_new_crtc_state,
7140 					      &dm_new_crtc_state->cursor_mode);
7141 		if (ret) {
7142 			drm_dbg(dev, "Failed to determine cursor mode: %pe\n", ERR_PTR(ret));
7143 			goto fail;
7144 		}
7145 
7146 		/*
7147 		 * If overlay cursor is needed, DC cannot go through the
7148 		 * native cursor update path. All enabled planes on the CRTC
7149 		 * need to be added for DC to not disable a plane by mistake
7150 		 */
7151 		if (dm_new_crtc_state->cursor_mode == DM_CURSOR_OVERLAY_MODE) {
7152 			if (amdgpu_ip_version(adev, DCE_HWIP, 0) == 0) {
7153 				drm_dbg(dev, "Overlay cursor not supported on DCE\n");
7154 				ret = -EINVAL;
7155 				goto fail;
7156 			}
7157 
7158 			ret = drm_atomic_add_affected_planes(state, crtc);
7159 			if (ret)
7160 				goto fail;
7161 		}
7162 	}
7163 
7164 	/* Remove exiting planes if they are modified */
7165 	for_each_oldnew_plane_in_descending_zpos(state, plane, old_plane_state, new_plane_state) {
7166 
7167 		ret = dm_update_plane_state(dc, state, plane,
7168 					    old_plane_state,
7169 					    new_plane_state,
7170 					    false,
7171 					    &lock_and_validation_needed,
7172 					    &is_top_most_overlay);
7173 		if (ret) {
7174 			drm_dbg_atomic(dev, "dm_update_plane_state() failed: %pe\n", ERR_PTR(ret));
7175 			goto fail;
7176 		}
7177 	}
7178 
7179 	/* Disable all crtcs which require disable */
7180 	for_each_oldnew_crtc_in_state(state, crtc, old_crtc_state, new_crtc_state, i) {
7181 		ret = dm_update_crtc_state(&adev->dm, state, crtc,
7182 					   old_crtc_state,
7183 					   new_crtc_state,
7184 					   false,
7185 					   &lock_and_validation_needed);
7186 		if (ret) {
7187 			drm_dbg_atomic(dev, "DISABLE: dm_update_crtc_state() failed: %pe\n", ERR_PTR(ret));
7188 			goto fail;
7189 		}
7190 	}
7191 
7192 	/* Enable all crtcs which require enable */
7193 	for_each_oldnew_crtc_in_state(state, crtc, old_crtc_state, new_crtc_state, i) {
7194 		ret = dm_update_crtc_state(&adev->dm, state, crtc,
7195 					   old_crtc_state,
7196 					   new_crtc_state,
7197 					   true,
7198 					   &lock_and_validation_needed);
7199 		if (ret) {
7200 			drm_dbg_atomic(dev, "ENABLE: dm_update_crtc_state() failed: %pe\n", ERR_PTR(ret));
7201 			goto fail;
7202 		}
7203 	}
7204 
7205 	/* Add new/modified planes */
7206 	for_each_oldnew_plane_in_descending_zpos(state, plane, old_plane_state, new_plane_state) {
7207 		ret = dm_update_plane_state(dc, state, plane,
7208 					    old_plane_state,
7209 					    new_plane_state,
7210 					    true,
7211 					    &lock_and_validation_needed,
7212 					    &is_top_most_overlay);
7213 		if (ret) {
7214 			drm_dbg_atomic(dev, "dm_update_plane_state() failed: %pe\n", ERR_PTR(ret));
7215 			goto fail;
7216 		}
7217 	}
7218 
7219 #if defined(CONFIG_DRM_AMD_DC_FP)
7220 	if (dc_resource_is_dsc_encoding_supported(dc)) {
7221 		ret = pre_validate_dsc(state, &dm_state, vars);
7222 		if (ret != 0)
7223 			goto fail;
7224 	}
7225 #endif
7226 
7227 	/* Run this here since we want to validate the streams we created */
7228 	ret = drm_atomic_helper_check_planes(dev, state);
7229 	if (ret) {
7230 		drm_dbg_atomic(dev, "drm_atomic_helper_check_planes() failed: %pe\n", ERR_PTR(ret));
7231 		goto fail;
7232 	}
7233 
7234 	for_each_new_crtc_in_state(state, crtc, new_crtc_state, i) {
7235 		dm_new_crtc_state = to_dm_crtc_state(new_crtc_state);
7236 		if (dm_new_crtc_state->mpo_requested)
7237 			drm_dbg_atomic(dev, "MPO enablement requested on crtc:[%p]\n", crtc);
7238 	}
7239 
7240 	/* Check cursor restrictions */
7241 	for_each_new_crtc_in_state(state, crtc, new_crtc_state, i) {
7242 		enum amdgpu_dm_cursor_mode required_cursor_mode;
7243 		int is_rotated, is_scaled;
7244 
7245 		/* Overlay cusor not subject to native cursor restrictions */
7246 		dm_new_crtc_state = to_dm_crtc_state(new_crtc_state);
7247 		if (dm_new_crtc_state->cursor_mode == DM_CURSOR_OVERLAY_MODE)
7248 			continue;
7249 
7250 		/* Check if rotation or scaling is enabled on DCN401 */
7251 		if ((drm_plane_mask(crtc->cursor) &
7252 		     new_crtc_state->plane_mask) &&
7253 		    (amdgpu_ip_version(adev, DCE_HWIP, 0) == IP_VERSION(4, 2, 1) ||
7254 		     amdgpu_ip_version(adev, DCE_HWIP, 0) == IP_VERSION(4, 2, 0) ||
7255 		     amdgpu_ip_version(adev, DCE_HWIP, 0) == IP_VERSION(4, 0, 1))) {
7256 			new_cursor_state = drm_atomic_get_new_plane_state(state, crtc->cursor);
7257 
7258 			is_rotated = new_cursor_state &&
7259 				((new_cursor_state->rotation & DRM_MODE_ROTATE_MASK) != DRM_MODE_ROTATE_0);
7260 			is_scaled = new_cursor_state && ((new_cursor_state->src_w >> 16 != new_cursor_state->crtc_w) ||
7261 				(new_cursor_state->src_h >> 16 != new_cursor_state->crtc_h));
7262 
7263 			if (is_rotated || is_scaled) {
7264 				drm_dbg_driver(
7265 					crtc->dev,
7266 					"[CRTC:%d:%s] cannot enable hardware cursor due to rotation/scaling\n",
7267 					crtc->base.id, crtc->name);
7268 				ret = -EINVAL;
7269 				goto fail;
7270 			}
7271 		}
7272 
7273 		/* If HW can only do native cursor, check restrictions again */
7274 		ret = dm_crtc_get_cursor_mode(adev, state, dm_new_crtc_state,
7275 					      &required_cursor_mode);
7276 		if (ret) {
7277 			drm_dbg_driver(crtc->dev,
7278 				       "[CRTC:%d:%s] Checking cursor mode failed\n",
7279 				       crtc->base.id, crtc->name);
7280 			goto fail;
7281 		} else if (required_cursor_mode == DM_CURSOR_OVERLAY_MODE) {
7282 			drm_dbg_driver(crtc->dev,
7283 				       "[CRTC:%d:%s] Cannot enable native cursor due to scaling, YUV, or color pipeline restrictions\n",
7284 				       crtc->base.id, crtc->name);
7285 			ret = -EINVAL;
7286 			goto fail;
7287 		}
7288 	}
7289 
7290 	if (state->legacy_cursor_update) {
7291 		/*
7292 		 * This is a fast cursor update coming from the plane update
7293 		 * helper, check if it can be done asynchronously for better
7294 		 * performance.
7295 		 */
7296 		state->async_update =
7297 			!drm_atomic_helper_async_check(dev, state);
7298 
7299 		/*
7300 		 * Skip the remaining global validation if this is an async
7301 		 * update. Cursor updates can be done without affecting
7302 		 * state or bandwidth calcs and this avoids the performance
7303 		 * penalty of locking the private state object and
7304 		 * allocating a new dc_state.
7305 		 */
7306 		if (state->async_update)
7307 			return 0;
7308 	}
7309 
7310 	/* Check scaling and underscan changes*/
7311 	/* TODO Removed scaling changes validation due to inability to commit
7312 	 * new stream into context w\o causing full reset. Need to
7313 	 * decide how to handle.
7314 	 */
7315 	for_each_oldnew_connector_in_state(state, connector, old_con_state, new_con_state, i) {
7316 		struct dm_connector_state *dm_old_con_state = to_dm_connector_state(old_con_state);
7317 		struct dm_connector_state *dm_new_con_state = to_dm_connector_state(new_con_state);
7318 		struct amdgpu_crtc *acrtc = to_amdgpu_crtc(dm_new_con_state->base.crtc);
7319 
7320 		/* Skip any modesets/resets */
7321 		if (!acrtc || drm_atomic_crtc_needs_modeset(
7322 				drm_atomic_get_new_crtc_state(state, &acrtc->base)))
7323 			continue;
7324 
7325 		/* Skip any thing not scale or underscan changes */
7326 		if (!is_scaling_state_different(dm_new_con_state, dm_old_con_state))
7327 			continue;
7328 
7329 		lock_and_validation_needed = true;
7330 	}
7331 
7332 	/* set the slot info for each mst_state based on the link encoding format */
7333 	for_each_new_mst_mgr_in_state(state, mgr, mst_state, i) {
7334 		struct amdgpu_dm_connector *aconnector;
7335 		struct drm_connector *connector;
7336 		struct drm_connector_list_iter iter;
7337 		u8 link_coding_cap;
7338 
7339 		drm_connector_list_iter_begin(dev, &iter);
7340 		drm_for_each_connector_iter(connector, &iter) {
7341 			if (connector->index == mst_state->mgr->conn_base_id) {
7342 				aconnector = to_amdgpu_dm_connector(connector);
7343 				link_coding_cap = dc_link_dp_mst_decide_link_encoding_format(aconnector->dc_link);
7344 				drm_dp_mst_update_slots(mst_state, link_coding_cap);
7345 
7346 				break;
7347 			}
7348 		}
7349 		drm_connector_list_iter_end(&iter);
7350 	}
7351 
7352 	/**
7353 	 * Streams and planes are reset when there are changes that affect
7354 	 * bandwidth. Anything that affects bandwidth needs to go through
7355 	 * DC global validation to ensure that the configuration can be applied
7356 	 * to hardware.
7357 	 *
7358 	 * We have to currently stall out here in atomic_check for outstanding
7359 	 * commits to finish in this case because our IRQ handlers reference
7360 	 * DRM state directly - we can end up disabling interrupts too early
7361 	 * if we don't.
7362 	 *
7363 	 * TODO: Remove this stall and drop DM state private objects.
7364 	 */
7365 	if (lock_and_validation_needed) {
7366 		ret = dm_atomic_get_state(state, &dm_state);
7367 		if (ret) {
7368 			drm_dbg_atomic(dev, "dm_atomic_get_state() failed: %pe\n", ERR_PTR(ret));
7369 			goto fail;
7370 		}
7371 
7372 		ret = do_aquire_global_lock(dev, state);
7373 		if (ret) {
7374 			drm_dbg_atomic(dev, "do_aquire_global_lock() failed: %pe\n", ERR_PTR(ret));
7375 			goto fail;
7376 		}
7377 
7378 #if defined(CONFIG_DRM_AMD_DC_FP)
7379 		if (dc_resource_is_dsc_encoding_supported(dc)) {
7380 			ret = compute_mst_dsc_configs_for_state(state, dm_state->context, vars);
7381 			if (ret) {
7382 				drm_dbg_atomic(dev, "MST_DSC compute_mst_dsc_configs_for_state() failed: %pe\n", ERR_PTR(ret));
7383 				ret = -EINVAL;
7384 				goto fail;
7385 			}
7386 		}
7387 #endif
7388 
7389 		ret = dm_update_mst_vcpi_slots_for_dsc(state, dm_state->context, vars);
7390 		if (ret) {
7391 			drm_dbg_atomic(dev, "dm_update_mst_vcpi_slots_for_dsc() failed: %pe\n", ERR_PTR(ret));
7392 			goto fail;
7393 		}
7394 
7395 		/*
7396 		 * Perform validation of MST topology in the state:
7397 		 * We need to perform MST atomic check before calling
7398 		 * dc_validate_global_state(), or there is a chance
7399 		 * to get stuck in an infinite loop and hang eventually.
7400 		 */
7401 		ret = drm_dp_mst_atomic_check(state);
7402 		if (ret) {
7403 			drm_dbg_atomic(dev, "MST drm_dp_mst_atomic_check() failed: %pe\n", ERR_PTR(ret));
7404 			goto fail;
7405 		}
7406 		status = dc_validate_global_state(dc, dm_state->context, DC_VALIDATE_MODE_ONLY);
7407 		if (status != DC_OK) {
7408 			drm_dbg_atomic(dev, "DC global validation failure: %s (%d)",
7409 				       dc_status_to_str(status), status);
7410 			ret = -EINVAL;
7411 			goto fail;
7412 		}
7413 	} else {
7414 		/*
7415 		 * The commit is a fast update. Fast updates shouldn't change
7416 		 * the DC context, affect global validation, and can have their
7417 		 * commit work done in parallel with other commits not touching
7418 		 * the same resource. If we have a new DC context as part of
7419 		 * the DM atomic state from validation we need to free it and
7420 		 * retain the existing one instead.
7421 		 *
7422 		 * Furthermore, since the DM atomic state only contains the DC
7423 		 * context and can safely be annulled, we can free the state
7424 		 * and clear the associated private object now to free
7425 		 * some memory and avoid a possible use-after-free later.
7426 		 */
7427 
7428 		for (i = 0; i < state->num_private_objs; i++) {
7429 			struct drm_private_obj *obj = state->private_objs[i].ptr;
7430 
7431 			if (obj->funcs == adev->dm.atomic_obj.funcs) {
7432 				int j = state->num_private_objs-1;
7433 
7434 				dm_atomic_destroy_state(obj,
7435 						state->private_objs[i].state_to_destroy);
7436 
7437 				/* If i is not at the end of the array then the
7438 				 * last element needs to be moved to where i was
7439 				 * before the array can safely be truncated.
7440 				 */
7441 				if (i != j)
7442 					state->private_objs[i] =
7443 						state->private_objs[j];
7444 
7445 				state->private_objs[j].ptr = NULL;
7446 				state->private_objs[j].state_to_destroy = NULL;
7447 				state->private_objs[j].old_state = NULL;
7448 				state->private_objs[j].new_state = NULL;
7449 
7450 				state->num_private_objs = j;
7451 				break;
7452 			}
7453 		}
7454 	}
7455 
7456 	/* Store the overall update type for use later in atomic check. */
7457 	for_each_new_crtc_in_state(state, crtc, new_crtc_state, i) {
7458 		struct dm_crtc_state *dm_new_crtc_state =
7459 			to_dm_crtc_state(new_crtc_state);
7460 
7461 		/*
7462 		 * Only allow async flips for fast updates that don't change
7463 		 * the FB pitch, the DCC state, rotation, mem_type, etc.
7464 		 */
7465 		if (new_crtc_state->async_flip &&
7466 		    (lock_and_validation_needed ||
7467 		     amdgpu_dm_crtc_mem_type_changed(dev, state, new_crtc_state))) {
7468 			drm_dbg_atomic(crtc->dev,
7469 				       "[CRTC:%d:%s] async flips are only supported for fast updates\n",
7470 				       crtc->base.id, crtc->name);
7471 			ret = -EINVAL;
7472 			goto fail;
7473 		}
7474 
7475 		dm_new_crtc_state->update_type = lock_and_validation_needed ?
7476 			UPDATE_TYPE_FULL : UPDATE_TYPE_FAST;
7477 	}
7478 
7479 	/* Must be success */
7480 	WARN_ON(ret);
7481 
7482 	trace_amdgpu_dm_atomic_check_finish(state, ret);
7483 
7484 	return ret;
7485 
7486 fail:
7487 	if (ret == -EDEADLK)
7488 		drm_dbg_atomic(dev, "Atomic check stopped to avoid deadlock.\n");
7489 	else if (ret == -EINTR || ret == -EAGAIN || ret == -ERESTARTSYS)
7490 		drm_dbg_atomic(dev, "Atomic check stopped due to signal.\n");
7491 	else
7492 		drm_dbg_atomic(dev, "Atomic check failed: %pe\n", ERR_PTR(ret));
7493 
7494 	trace_amdgpu_dm_atomic_check_finish(state, ret);
7495 
7496 	return ret;
7497 }
7498 
7499 void amdgpu_dm_trigger_timing_sync(struct drm_device *dev)
7500 {
7501 	struct amdgpu_device *adev = drm_to_adev(dev);
7502 	struct dc *dc = adev->dm.dc;
7503 	int i;
7504 
7505 	mutex_lock(&adev->dm.dc_lock);
7506 	if (dc->current_state) {
7507 		for (i = 0; i < dc->current_state->stream_count; ++i)
7508 			dc->current_state->streams[i]
7509 				->triggered_crtc_reset.enabled =
7510 				adev->dm.force_timing_sync;
7511 
7512 		dm_enable_per_frame_crtc_master_sync(dc->current_state);
7513 		dc_trigger_sync(dc, dc->current_state);
7514 	}
7515 	mutex_unlock(&adev->dm.dc_lock);
7516 }
7517 
7518 void dm_write_reg_func(const struct dc_context *ctx, uint32_t address,
7519 		       u32 value, const char *func_name)
7520 {
7521 #ifdef DM_CHECK_ADDR_0
7522 	if (address == 0) {
7523 		drm_err(adev_to_drm(ctx->driver_context),
7524 			"invalid register write. address = 0");
7525 		return;
7526 	}
7527 #endif
7528 
7529 	amdgpu_dm_exit_ips_for_hw_access(ctx->dc);
7530 	cgs_write_register(ctx->cgs_device, address, value);
7531 	trace_amdgpu_dc_wreg(&ctx->perf_trace->write_count, address, value);
7532 }
7533 
7534 uint32_t dm_read_reg_func(const struct dc_context *ctx, uint32_t address,
7535 			  const char *func_name)
7536 {
7537 	u32 value;
7538 #ifdef DM_CHECK_ADDR_0
7539 	if (address == 0) {
7540 		drm_err(adev_to_drm(ctx->driver_context),
7541 			"invalid register read; address = 0\n");
7542 		return 0;
7543 	}
7544 #endif
7545 
7546 	amdgpu_dm_exit_ips_for_hw_access(ctx->dc);
7547 
7548 	value = cgs_read_register(ctx->cgs_device, address);
7549 
7550 	trace_amdgpu_dc_rreg(&ctx->perf_trace->read_count, address, value);
7551 
7552 	return value;
7553 }
7554 
7555 void dm_acpi_process_phy_transition_interlock(
7556 	const struct dc_context *ctx,
7557 	struct dm_process_phy_transition_init_params process_phy_transition_init_params)
7558 {
7559 	// Not yet implemented
7560 }
7561