xref: /linux/drivers/gpu/drm/amd/display/dc/core/dc.c (revision 32a92f8c89326985e05dce8b22d3f0aa07a3e1bd)
1 /*
2  * Copyright 2015 Advanced Micro Devices, Inc.
3  *
4  * Permission is hereby granted, free of charge, to any person obtaining a
5  * copy of this software and associated documentation files (the "Software"),
6  * to deal in the Software without restriction, including without limitation
7  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8  * and/or sell copies of the Software, and to permit persons to whom the
9  * Software is furnished to do so, subject to the following conditions:
10  *
11  * The above copyright notice and this permission notice shall be included in
12  * all copies or substantial portions of the Software.
13  *
14  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
17  * THE COPYRIGHT HOLDER(S) OR AUTHOR(S) BE LIABLE FOR ANY CLAIM, DAMAGES OR
18  * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
19  * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
20  * OTHER DEALINGS IN THE SOFTWARE.
21  *
22  * Authors: AMD
23  */
24 
25 #include "dm_services.h"
26 
27 #include "amdgpu.h"
28 
29 #include "dc.h"
30 
31 #include "core_status.h"
32 #include "core_types.h"
33 #include "hw_sequencer.h"
34 #include "dce/dce_hwseq.h"
35 
36 #include "resource.h"
37 #include "dc_state.h"
38 #include "dc_state_priv.h"
39 #include "dc_plane.h"
40 #include "dc_plane_priv.h"
41 #include "dc_stream_priv.h"
42 
43 #include "gpio_service_interface.h"
44 #include "clk_mgr.h"
45 #include "clock_source.h"
46 #include "dc_bios_types.h"
47 
48 #include "bios_parser_interface.h"
49 #include "bios/bios_parser_helper.h"
50 #include "include/irq_service_interface.h"
51 #include "transform.h"
52 #include "dmcu.h"
53 #include "dpp.h"
54 #include "timing_generator.h"
55 #include "abm.h"
56 #include "dio/virtual/virtual_link_encoder.h"
57 #include "hubp.h"
58 
59 #include "link_hwss.h"
60 #include "link_encoder.h"
61 #include "link_enc_cfg.h"
62 
63 #include "link_service.h"
64 #include "dm_helpers.h"
65 #include "mem_input.h"
66 
67 #include "dc_dmub_srv.h"
68 
69 #include "dsc.h"
70 
71 #include "vm_helper.h"
72 
73 #include "dce/dce_i2c.h"
74 
75 #include "dmub/dmub_srv.h"
76 
77 #include "dce/dmub_psr.h"
78 
79 #include "dce/dmub_hw_lock_mgr.h"
80 
81 #include "dc_trace.h"
82 
83 #include "hw_sequencer_private.h"
84 
85 #if defined(CONFIG_DRM_AMD_DC_FP)
86 #include "dml2_0/dml2_internal_types.h"
87 #include "soc_and_ip_translator.h"
88 #endif
89 
90 #include "dce/dmub_outbox.h"
91 
92 #define CTX \
93 	dc->ctx
94 
95 #define DC_LOGGER \
96 	dc->ctx->logger
97 
98 static const char DC_BUILD_ID[] = "production-build";
99 
100 /**
101  * DOC: Overview
102  *
103  * DC is the OS-agnostic component of the amdgpu DC driver.
104  *
105  * DC maintains and validates a set of structs representing the state of the
106  * driver and writes that state to AMD hardware
107  *
108  * Main DC HW structs:
109  *
110  * struct dc - The central struct.  One per driver.  Created on driver load,
111  * destroyed on driver unload.
112  *
113  * struct dc_context - One per driver.
114  * Used as a backpointer by most other structs in dc.
115  *
116  * struct dc_link - One per connector (the physical DP, HDMI, miniDP, or eDP
117  * plugpoints).  Created on driver load, destroyed on driver unload.
118  *
119  * struct dc_sink - One per display.  Created on boot or hotplug.
120  * Destroyed on shutdown or hotunplug.  A dc_link can have a local sink
121  * (the display directly attached).  It may also have one or more remote
122  * sinks (in the Multi-Stream Transport case)
123  *
124  * struct resource_pool - One per driver.  Represents the hw blocks not in the
125  * main pipeline.  Not directly accessible by dm.
126  *
127  * Main dc state structs:
128  *
129  * These structs can be created and destroyed as needed.  There is a full set of
130  * these structs in dc->current_state representing the currently programmed state.
131  *
132  * struct dc_state - The global DC state to track global state information,
133  * such as bandwidth values.
134  *
135  * struct dc_stream_state - Represents the hw configuration for the pipeline from
136  * a framebuffer to a display.  Maps one-to-one with dc_sink.
137  *
138  * struct dc_plane_state - Represents a framebuffer.  Each stream has at least one,
139  * and may have more in the Multi-Plane Overlay case.
140  *
141  * struct resource_context - Represents the programmable state of everything in
142  * the resource_pool.  Not directly accessible by dm.
143  *
144  * struct pipe_ctx - A member of struct resource_context.  Represents the
145  * internal hardware pipeline components.  Each dc_plane_state has either
146  * one or two (in the pipe-split case).
147  */
148 
149 /* Private functions */
150 
151 static inline void elevate_update_type(
152 		struct surface_update_descriptor *descriptor,
153 		enum surface_update_type new_type,
154 		enum dc_lock_descriptor new_locks
155 )
156 {
157 	if (new_type > descriptor->update_type)
158 		descriptor->update_type = new_type;
159 
160 	descriptor->lock_descriptor |= new_locks;
161 }
162 
163 static void destroy_links(struct dc *dc)
164 {
165 	uint32_t i;
166 
167 	for (i = 0; i < dc->link_count; i++) {
168 		if (NULL != dc->links[i])
169 			dc->link_srv->destroy_link(&dc->links[i]);
170 	}
171 }
172 
173 static uint32_t get_num_of_internal_disp(struct dc_link **links, uint32_t num_links)
174 {
175 	int i;
176 	uint32_t count = 0;
177 
178 	for (i = 0; i < num_links; i++) {
179 		if (links[i]->connector_signal == SIGNAL_TYPE_EDP ||
180 				links[i]->is_internal_display)
181 			count++;
182 	}
183 
184 	return count;
185 }
186 
187 static int get_seamless_boot_stream_count(struct dc_state *ctx)
188 {
189 	uint8_t i;
190 	uint8_t seamless_boot_stream_count = 0;
191 
192 	for (i = 0; i < ctx->stream_count; i++)
193 		if (ctx->streams[i]->apply_seamless_boot_optimization)
194 			seamless_boot_stream_count++;
195 
196 	return seamless_boot_stream_count;
197 }
198 
199 static bool create_links(
200 		struct dc *dc,
201 		uint32_t num_virtual_links)
202 {
203 	int i;
204 	int connectors_num;
205 	struct dc_bios *bios = dc->ctx->dc_bios;
206 
207 	dc->link_count = 0;
208 
209 	connectors_num = bios->funcs->get_connectors_number(bios);
210 
211 	DC_LOG_DC("BIOS object table - number of connectors: %d", connectors_num);
212 
213 	if (connectors_num > ENUM_ID_COUNT) {
214 		dm_error(
215 			"DC: Number of connectors %d exceeds maximum of %d!\n",
216 			connectors_num,
217 			ENUM_ID_COUNT);
218 		return false;
219 	}
220 
221 	dm_output_to_console(
222 		"DC: %s: connectors_num: physical:%d, virtual:%d\n",
223 		__func__,
224 		connectors_num,
225 		num_virtual_links);
226 
227 	/* When getting the number of connectors, the VBIOS reports the number of valid indices,
228 	 * but it doesn't say which indices are valid, and not every index has an actual connector.
229 	 * So, if we don't find a connector on an index, that is not an error.
230 	 *
231 	 * - There is no guarantee that the first N indices will be valid
232 	 * - VBIOS may report a higher amount of valid indices than there are actual connectors
233 	 * - Some VBIOS have valid configurations for more connectors than there actually are
234 	 *   on the card. This may be because the manufacturer used the same VBIOS for different
235 	 *   variants of the same card.
236 	 */
237 	for (i = 0; dc->link_count < connectors_num && i < MAX_LINKS; i++) {
238 		struct graphics_object_id connector_id = bios->funcs->get_connector_id(bios, i);
239 		struct link_init_data link_init_params = {0};
240 		struct dc_link *link;
241 
242 		if (connector_id.id == CONNECTOR_ID_UNKNOWN)
243 			continue;
244 
245 		DC_LOG_DC("BIOS object table - printing link object info for connector number: %d, link_index: %d", i, dc->link_count);
246 
247 		link_init_params.ctx = dc->ctx;
248 		/* next BIOS object table connector */
249 		link_init_params.connector_index = i;
250 		link_init_params.link_index = dc->link_count;
251 		link_init_params.dc = dc;
252 		link = dc->link_srv->create_link(&link_init_params);
253 
254 		if (link) {
255 			dc->links[dc->link_count] = link;
256 			link->dc = dc;
257 			++dc->link_count;
258 		}
259 	}
260 
261 	DC_LOG_DC("BIOS object table - end");
262 
263 	/* Create a link for each usb4 dpia port */
264 	dc->lowest_dpia_link_index = MAX_LINKS;
265 	for (i = 0; i < dc->res_pool->usb4_dpia_count; i++) {
266 		struct link_init_data link_init_params = {0};
267 		struct dc_link *link;
268 
269 		link_init_params.ctx = dc->ctx;
270 		link_init_params.connector_index = i;
271 		link_init_params.link_index = dc->link_count;
272 		link_init_params.dc = dc;
273 		link_init_params.is_dpia_link = true;
274 
275 		link = dc->link_srv->create_link(&link_init_params);
276 		if (link) {
277 			if (dc->lowest_dpia_link_index > dc->link_count)
278 				dc->lowest_dpia_link_index = dc->link_count;
279 
280 			dc->links[dc->link_count] = link;
281 			link->dc = dc;
282 			++dc->link_count;
283 		}
284 	}
285 
286 	for (i = 0; i < num_virtual_links; i++) {
287 		struct dc_link *link = kzalloc_obj(*link);
288 		struct encoder_init_data enc_init = {0};
289 
290 		if (link == NULL) {
291 			BREAK_TO_DEBUGGER();
292 			goto failed_alloc;
293 		}
294 
295 		link->link_index = dc->link_count;
296 		dc->links[dc->link_count] = link;
297 		dc->link_count++;
298 
299 		link->ctx = dc->ctx;
300 		link->dc = dc;
301 		link->connector_signal = SIGNAL_TYPE_VIRTUAL;
302 		link->link_id.type = OBJECT_TYPE_CONNECTOR;
303 		link->link_id.id = CONNECTOR_ID_VIRTUAL;
304 		link->link_id.enum_id = ENUM_ID_1;
305 		link->psr_settings.psr_version = DC_PSR_VERSION_UNSUPPORTED;
306 		link->replay_settings.config.replay_version = DC_REPLAY_VERSION_UNSUPPORTED;
307 		link->link_enc = kzalloc_obj(*link->link_enc);
308 
309 		if (!link->link_enc) {
310 			BREAK_TO_DEBUGGER();
311 			goto failed_alloc;
312 		}
313 
314 		link->link_status.dpcd_caps = &link->dpcd_caps;
315 
316 		enc_init.ctx = dc->ctx;
317 		enc_init.channel = CHANNEL_ID_UNKNOWN;
318 		enc_init.hpd_source = HPD_SOURCEID_UNKNOWN;
319 		enc_init.transmitter = TRANSMITTER_UNKNOWN;
320 		enc_init.connector = link->link_id;
321 		enc_init.encoder.type = OBJECT_TYPE_ENCODER;
322 		enc_init.encoder.id = ENCODER_ID_INTERNAL_VIRTUAL;
323 		enc_init.encoder.enum_id = ENUM_ID_1;
324 		virtual_link_encoder_construct(link->link_enc, &enc_init);
325 	}
326 
327 	dc->caps.num_of_internal_disp = get_num_of_internal_disp(dc->links, dc->link_count);
328 
329 	return true;
330 
331 failed_alloc:
332 	return false;
333 }
334 
335 /* Create additional DIG link encoder objects if fewer than the platform
336  * supports were created during link construction. This can happen if the
337  * number of physical connectors is less than the number of DIGs.
338  */
339 static bool create_link_encoders(struct dc *dc)
340 {
341 	bool res = true;
342 	unsigned int num_usb4_dpia = dc->res_pool->res_cap->num_usb4_dpia;
343 	unsigned int num_dig_link_enc = dc->res_pool->res_cap->num_dig_link_enc;
344 	int i;
345 
346 	/* A platform without USB4 DPIA endpoints has a fixed mapping between DIG
347 	 * link encoders and physical display endpoints and does not require
348 	 * additional link encoder objects.
349 	 */
350 	if (num_usb4_dpia == 0)
351 		return res;
352 
353 	/* Create as many link encoder objects as the platform supports. DPIA
354 	 * endpoints can be programmably mapped to any DIG.
355 	 */
356 	if (num_dig_link_enc > dc->res_pool->dig_link_enc_count) {
357 		for (i = 0; i < num_dig_link_enc; i++) {
358 			struct link_encoder *link_enc = dc->res_pool->link_encoders[i];
359 
360 			if (!link_enc && dc->res_pool->funcs->link_enc_create_minimal) {
361 				link_enc = dc->res_pool->funcs->link_enc_create_minimal(dc->ctx,
362 						(enum engine_id)(ENGINE_ID_DIGA + i));
363 				if (link_enc) {
364 					dc->res_pool->link_encoders[i] = link_enc;
365 					dc->res_pool->dig_link_enc_count++;
366 				} else {
367 					res = false;
368 				}
369 			}
370 		}
371 	}
372 
373 	return res;
374 }
375 
376 /* Destroy any additional DIG link encoder objects created by
377  * create_link_encoders().
378  * NB: Must only be called after destroy_links().
379  */
380 static void destroy_link_encoders(struct dc *dc)
381 {
382 	unsigned int num_usb4_dpia;
383 	unsigned int num_dig_link_enc;
384 	int i;
385 
386 	if (!dc->res_pool)
387 		return;
388 
389 	num_usb4_dpia = dc->res_pool->res_cap->num_usb4_dpia;
390 	num_dig_link_enc = dc->res_pool->res_cap->num_dig_link_enc;
391 
392 	/* A platform without USB4 DPIA endpoints has a fixed mapping between DIG
393 	 * link encoders and physical display endpoints and does not require
394 	 * additional link encoder objects.
395 	 */
396 	if (num_usb4_dpia == 0)
397 		return;
398 
399 	for (i = 0; i < num_dig_link_enc; i++) {
400 		struct link_encoder *link_enc = dc->res_pool->link_encoders[i];
401 
402 		if (link_enc) {
403 			link_enc->funcs->destroy(&link_enc);
404 			dc->res_pool->link_encoders[i] = NULL;
405 			dc->res_pool->dig_link_enc_count--;
406 		}
407 	}
408 }
409 
410 static struct dc_perf_trace *dc_perf_trace_create(void)
411 {
412 	return kzalloc_obj(struct dc_perf_trace);
413 }
414 
415 static void dc_perf_trace_destroy(struct dc_perf_trace **perf_trace)
416 {
417 	kfree(*perf_trace);
418 	*perf_trace = NULL;
419 }
420 
421 static bool set_long_vtotal(struct dc *dc, struct dc_stream_state *stream, struct dc_crtc_timing_adjust *adjust)
422 {
423 	if (!dc || !stream || !adjust)
424 		return false;
425 
426 	if (!dc->current_state)
427 		return false;
428 
429 	int i;
430 
431 	for (i = 0; i < MAX_PIPES; i++) {
432 		struct pipe_ctx *pipe = &dc->current_state->res_ctx.pipe_ctx[i];
433 
434 		if (pipe->stream == stream && pipe->stream_res.tg) {
435 			if (dc->hwss.set_long_vtotal)
436 				dc->hwss.set_long_vtotal(&pipe, 1, adjust->v_total_min, adjust->v_total_max);
437 
438 			return true;
439 		}
440 	}
441 
442 	return false;
443 }
444 
445 /**
446  *  dc_stream_adjust_vmin_vmax - look up pipe context & update parts of DRR
447  *  @dc:     dc reference
448  *  @stream: Initial dc stream state
449  *  @adjust: Updated parameters for vertical_total_min and vertical_total_max
450  *
451  *  Looks up the pipe context of dc_stream_state and updates the
452  *  vertical_total_min and vertical_total_max of the DRR, Dynamic Refresh
453  *  Rate, which is a power-saving feature that targets reducing panel
454  *  refresh rate while the screen is static
455  *
456  *  Return: %true if the pipe context is found and adjusted;
457  *          %false if the pipe context is not found.
458  */
459 bool dc_stream_adjust_vmin_vmax(struct dc *dc,
460 		struct dc_stream_state *stream,
461 		struct dc_crtc_timing_adjust *adjust)
462 {
463 	int i;
464 
465 	/*
466 	 * Don't adjust DRR while there's bandwidth optimizations pending to
467 	 * avoid conflicting with firmware updates.
468 	 */
469 	if (dc->ctx->dce_version > DCE_VERSION_MAX) {
470 		if (dc->optimized_required &&
471 			(stream->adjust.v_total_max != adjust->v_total_max ||
472 			stream->adjust.v_total_min != adjust->v_total_min)) {
473 			stream->adjust.timing_adjust_pending = true;
474 			return false;
475 		}
476 	}
477 
478 	dc_exit_ips_for_hw_access(dc);
479 
480 	stream->adjust.v_total_max = adjust->v_total_max;
481 	stream->adjust.v_total_mid = adjust->v_total_mid;
482 	stream->adjust.v_total_mid_frame_num = adjust->v_total_mid_frame_num;
483 	stream->adjust.v_total_min = adjust->v_total_min;
484 	stream->adjust.allow_otg_v_count_halt = adjust->allow_otg_v_count_halt;
485 
486 	if (dc->caps.max_v_total != 0 &&
487 		(adjust->v_total_max > dc->caps.max_v_total || adjust->v_total_min > dc->caps.max_v_total)) {
488 		stream->adjust.timing_adjust_pending = false;
489 		if (adjust->allow_otg_v_count_halt)
490 			return set_long_vtotal(dc, stream, adjust);
491 		else
492 			return false;
493 	}
494 
495 	for (i = 0; i < MAX_PIPES; i++) {
496 		struct pipe_ctx *pipe = &dc->current_state->res_ctx.pipe_ctx[i];
497 
498 		if (pipe->stream == stream && pipe->stream_res.tg) {
499 			dc->hwss.set_drr(&pipe,
500 					1,
501 					*adjust);
502 			stream->adjust.timing_adjust_pending = false;
503 
504 			if (dc->hwss.notify_cursor_offload_drr_update)
505 				dc->hwss.notify_cursor_offload_drr_update(dc, dc->current_state, stream);
506 
507 			return true;
508 		}
509 	}
510 
511 	return false;
512 }
513 
514 /**
515  * dc_stream_get_last_used_drr_vtotal - Looks up the pipe context of
516  * dc_stream_state and gets the last VTOTAL used by DRR (Dynamic Refresh Rate)
517  *
518  * @dc: [in] dc reference
519  * @stream: [in] Initial dc stream state
520  * @refresh_rate: [in] new refresh_rate
521  *
522  * Return: %true if the pipe context is found and there is an associated
523  *         timing_generator for the DC;
524  *         %false if the pipe context is not found or there is no
525  *         timing_generator for the DC.
526  */
527 bool dc_stream_get_last_used_drr_vtotal(struct dc *dc,
528 		struct dc_stream_state *stream,
529 		uint32_t *refresh_rate)
530 {
531 	bool status = false;
532 
533 	int i = 0;
534 
535 	dc_exit_ips_for_hw_access(dc);
536 
537 	for (i = 0; i < MAX_PIPES; i++) {
538 		struct pipe_ctx *pipe = &dc->current_state->res_ctx.pipe_ctx[i];
539 
540 		if (pipe->stream == stream && pipe->stream_res.tg) {
541 			/* Only execute if a function pointer has been defined for
542 			 * the DC version in question
543 			 */
544 			if (pipe->stream_res.tg->funcs->get_last_used_drr_vtotal) {
545 				pipe->stream_res.tg->funcs->get_last_used_drr_vtotal(pipe->stream_res.tg, refresh_rate);
546 
547 				status = true;
548 
549 				break;
550 			}
551 		}
552 	}
553 
554 	return status;
555 }
556 
557 #if defined(CONFIG_DRM_AMD_SECURE_DISPLAY)
558 static inline void
559 dc_stream_forward_dmub_crc_window(struct dc_dmub_srv *dmub_srv,
560 		struct rect *rect, struct otg_phy_mux *mux_mapping, bool is_stop)
561 {
562 	union dmub_rb_cmd cmd = {0};
563 
564 	cmd.secure_display.roi_info.phy_id = mux_mapping->phy_output_num;
565 	cmd.secure_display.roi_info.otg_id = mux_mapping->otg_output_num;
566 
567 	if (is_stop) {
568 		cmd.secure_display.header.type = DMUB_CMD__SECURE_DISPLAY;
569 		cmd.secure_display.header.sub_type = DMUB_CMD__SECURE_DISPLAY_CRC_STOP_UPDATE;
570 	} else {
571 		cmd.secure_display.header.type = DMUB_CMD__SECURE_DISPLAY;
572 		cmd.secure_display.header.sub_type = DMUB_CMD__SECURE_DISPLAY_CRC_WIN_NOTIFY;
573 		cmd.secure_display.roi_info.x_start = rect->x;
574 		cmd.secure_display.roi_info.y_start = rect->y;
575 		cmd.secure_display.roi_info.x_end = rect->x + rect->width;
576 		cmd.secure_display.roi_info.y_end = rect->y + rect->height;
577 	}
578 
579 	dc_wake_and_execute_dmub_cmd(dmub_srv->ctx, &cmd, DM_DMUB_WAIT_TYPE_NO_WAIT);
580 }
581 
582 static inline void
583 dc_stream_forward_dmcu_crc_window(struct dmcu *dmcu,
584 		struct rect *rect, struct otg_phy_mux *mux_mapping, bool is_stop)
585 {
586 	if (is_stop)
587 		dmcu->funcs->stop_crc_win_update(dmcu, mux_mapping);
588 	else
589 		dmcu->funcs->forward_crc_window(dmcu, rect, mux_mapping);
590 }
591 
592 bool
593 dc_stream_forward_crc_window(struct dc_stream_state *stream,
594 		struct rect *rect, uint8_t phy_id, bool is_stop)
595 {
596 	struct dmcu *dmcu;
597 	struct dc_dmub_srv *dmub_srv;
598 	struct otg_phy_mux mux_mapping;
599 	struct pipe_ctx *pipe;
600 	int i;
601 	struct dc *dc = stream->ctx->dc;
602 
603 	for (i = 0; i < MAX_PIPES; i++) {
604 		pipe = &dc->current_state->res_ctx.pipe_ctx[i];
605 		if (pipe->stream == stream && !pipe->top_pipe && !pipe->prev_odm_pipe)
606 			break;
607 	}
608 
609 	/* Stream not found */
610 	if (i == MAX_PIPES)
611 		return false;
612 
613 	mux_mapping.phy_output_num = phy_id;
614 	mux_mapping.otg_output_num = pipe->stream_res.tg->inst;
615 
616 	dmcu = dc->res_pool->dmcu;
617 	dmub_srv = dc->ctx->dmub_srv;
618 
619 	/* forward to dmub */
620 	if (dmub_srv)
621 		dc_stream_forward_dmub_crc_window(dmub_srv, rect, &mux_mapping, is_stop);
622 	/* forward to dmcu */
623 	else if (dmcu && dmcu->funcs->is_dmcu_initialized(dmcu))
624 		dc_stream_forward_dmcu_crc_window(dmcu, rect, &mux_mapping, is_stop);
625 	else
626 		return false;
627 
628 	return true;
629 }
630 
631 static void
632 dc_stream_forward_dmub_multiple_crc_window(struct dc_dmub_srv *dmub_srv,
633 		struct crc_window *window, struct otg_phy_mux *mux_mapping, bool stop)
634 {
635 	int i;
636 	union dmub_rb_cmd cmd = {0};
637 
638 	cmd.secure_display.mul_roi_ctl.phy_id = mux_mapping->phy_output_num;
639 	cmd.secure_display.mul_roi_ctl.otg_id = mux_mapping->otg_output_num;
640 
641 	cmd.secure_display.header.type = DMUB_CMD__SECURE_DISPLAY;
642 
643 	if (stop) {
644 		cmd.secure_display.header.sub_type = DMUB_CMD__SECURE_DISPLAY_MULTIPLE_CRC_STOP_UPDATE;
645 	} else {
646 		cmd.secure_display.header.sub_type = DMUB_CMD__SECURE_DISPLAY_MULTIPLE_CRC_WIN_NOTIFY;
647 		for (i = 0; i < MAX_CRC_WINDOW_NUM; i++) {
648 			cmd.secure_display.mul_roi_ctl.roi_ctl[i].x_start = window[i].rect.x;
649 			cmd.secure_display.mul_roi_ctl.roi_ctl[i].y_start = window[i].rect.y;
650 			cmd.secure_display.mul_roi_ctl.roi_ctl[i].x_end = window[i].rect.x + window[i].rect.width;
651 			cmd.secure_display.mul_roi_ctl.roi_ctl[i].y_end = window[i].rect.y + window[i].rect.height;
652 			cmd.secure_display.mul_roi_ctl.roi_ctl[i].enable = window[i].enable;
653 		}
654 	}
655 
656 	dc_wake_and_execute_dmub_cmd(dmub_srv->ctx, &cmd, DM_DMUB_WAIT_TYPE_NO_WAIT);
657 }
658 
659 bool
660 dc_stream_forward_multiple_crc_window(struct dc_stream_state *stream,
661 		struct crc_window *window, uint8_t phy_id, bool stop)
662 {
663 	struct dc_dmub_srv *dmub_srv;
664 	struct otg_phy_mux mux_mapping;
665 	struct pipe_ctx *pipe;
666 	int i;
667 	struct dc *dc = stream->ctx->dc;
668 
669 	for (i = 0; i < MAX_PIPES; i++) {
670 		pipe = &dc->current_state->res_ctx.pipe_ctx[i];
671 		if (pipe->stream == stream && !pipe->top_pipe && !pipe->prev_odm_pipe)
672 			break;
673 	}
674 
675 	/* Stream not found */
676 	if (i == MAX_PIPES)
677 		return false;
678 
679 	mux_mapping.phy_output_num = phy_id;
680 	mux_mapping.otg_output_num = pipe->stream_res.tg->inst;
681 
682 	dmub_srv = dc->ctx->dmub_srv;
683 
684 	/* forward to dmub only. no dmcu support*/
685 	if (dmub_srv)
686 		dc_stream_forward_dmub_multiple_crc_window(dmub_srv, window, &mux_mapping, stop);
687 	else
688 		return false;
689 
690 	return true;
691 }
692 #endif /* CONFIG_DRM_AMD_SECURE_DISPLAY */
693 
694 /**
695  * dc_stream_configure_crc() - Configure CRC capture for the given stream.
696  * @dc: DC Object
697  * @stream: The stream to configure CRC on.
698  * @crc_window: CRC window (x/y start/end) information
699  * @enable: Enable CRC if true, disable otherwise.
700  * @continuous: Capture CRC on every frame if true. Otherwise, only capture
701  *              once.
702  * @idx: Capture CRC on which CRC engine instance
703  * @reset: Reset CRC engine before the configuration
704  * @crc_poly_mode: CRC polynomial mode
705  *
706  * By default, the entire frame is used to calculate the CRC.
707  *
708  * Return: %false if the stream is not found or CRC capture is not supported;
709  *         %true if the stream has been configured.
710  */
711 bool dc_stream_configure_crc(struct dc *dc, struct dc_stream_state *stream,
712 			     struct crc_params *crc_window, bool enable, bool continuous,
713 			     uint8_t idx, bool reset, enum crc_poly_mode crc_poly_mode)
714 {
715 	struct pipe_ctx *pipe;
716 	struct crc_params param;
717 	struct timing_generator *tg;
718 
719 	pipe = resource_get_otg_master_for_stream(
720 			&dc->current_state->res_ctx, stream);
721 
722 	/* Stream not found */
723 	if (pipe == NULL)
724 		return false;
725 
726 	dc_exit_ips_for_hw_access(dc);
727 
728 	/* By default, capture the full frame */
729 	param.windowa_x_start = 0;
730 	param.windowa_y_start = 0;
731 	param.windowa_x_end = pipe->stream->timing.h_addressable;
732 	param.windowa_y_end = pipe->stream->timing.v_addressable;
733 	param.windowb_x_start = 0;
734 	param.windowb_y_start = 0;
735 	param.windowb_x_end = pipe->stream->timing.h_addressable;
736 	param.windowb_y_end = pipe->stream->timing.v_addressable;
737 	param.crc_poly_mode = crc_poly_mode;
738 
739 	if (crc_window) {
740 		param.windowa_x_start = crc_window->windowa_x_start;
741 		param.windowa_y_start = crc_window->windowa_y_start;
742 		param.windowa_x_end = crc_window->windowa_x_end;
743 		param.windowa_y_end = crc_window->windowa_y_end;
744 		param.windowb_x_start = crc_window->windowb_x_start;
745 		param.windowb_y_start = crc_window->windowb_y_start;
746 		param.windowb_x_end = crc_window->windowb_x_end;
747 		param.windowb_y_end = crc_window->windowb_y_end;
748 	}
749 
750 	param.dsc_mode = pipe->stream->timing.flags.DSC ? 1:0;
751 	param.odm_mode = pipe->next_odm_pipe ? 1:0;
752 
753 	/* Default to the union of both windows */
754 	param.selection = UNION_WINDOW_A_B;
755 	param.continuous_mode = continuous;
756 	param.enable = enable;
757 
758 	param.crc_eng_inst = idx;
759 	param.reset = reset;
760 
761 	tg = pipe->stream_res.tg;
762 
763 	/* Only call if supported */
764 	if (tg->funcs->configure_crc)
765 		return tg->funcs->configure_crc(tg, &param);
766 	DC_LOG_WARNING("CRC capture not supported.");
767 	return false;
768 }
769 
770 /**
771  * dc_stream_get_crc() - Get CRC values for the given stream.
772  *
773  * @dc: DC object.
774  * @stream: The DC stream state of the stream to get CRCs from.
775  * @idx: index of crc engine to get CRC from
776  * @r_cr: CRC value for the red component.
777  * @g_y:  CRC value for the green component.
778  * @b_cb: CRC value for the blue component.
779  *
780  * dc_stream_configure_crc needs to be called beforehand to enable CRCs.
781  *
782  * Return:
783  * %false if stream is not found, or if CRCs are not enabled.
784  */
785 bool dc_stream_get_crc(struct dc *dc, struct dc_stream_state *stream, uint8_t idx,
786 		       uint32_t *r_cr, uint32_t *g_y, uint32_t *b_cb)
787 {
788 	int i;
789 	struct pipe_ctx *pipe = NULL;
790 	struct timing_generator *tg;
791 
792 	dc_exit_ips_for_hw_access(dc);
793 
794 	for (i = 0; i < MAX_PIPES; i++) {
795 		pipe = &dc->current_state->res_ctx.pipe_ctx[i];
796 		if (pipe->stream == stream)
797 			break;
798 	}
799 	/* Stream not found */
800 	if (i == MAX_PIPES)
801 		return false;
802 
803 	tg = pipe->stream_res.tg;
804 
805 	if (tg->funcs->get_crc)
806 		return tg->funcs->get_crc(tg, idx, r_cr, g_y, b_cb);
807 	DC_LOG_WARNING("CRC capture not supported.");
808 	return false;
809 }
810 
811 void dc_stream_set_dyn_expansion(struct dc *dc, struct dc_stream_state *stream,
812 		enum dc_dynamic_expansion option)
813 {
814 	/* OPP FMT dyn expansion updates*/
815 	int i;
816 	struct pipe_ctx *pipe_ctx;
817 
818 	dc_exit_ips_for_hw_access(dc);
819 
820 	for (i = 0; i < MAX_PIPES; i++) {
821 		if (dc->current_state->res_ctx.pipe_ctx[i].stream
822 				== stream) {
823 			pipe_ctx = &dc->current_state->res_ctx.pipe_ctx[i];
824 			pipe_ctx->stream_res.opp->dyn_expansion = option;
825 			pipe_ctx->stream_res.opp->funcs->opp_set_dyn_expansion(
826 					pipe_ctx->stream_res.opp,
827 					COLOR_SPACE_YCBCR601,
828 					stream->timing.display_color_depth,
829 					stream->signal);
830 		}
831 	}
832 }
833 
834 void dc_stream_set_dither_option(struct dc_stream_state *stream,
835 		enum dc_dither_option option)
836 {
837 	struct bit_depth_reduction_params params;
838 	struct dc_link *link = stream->link;
839 	struct pipe_ctx *pipes = NULL;
840 	int i;
841 
842 	for (i = 0; i < MAX_PIPES; i++) {
843 		if (link->dc->current_state->res_ctx.pipe_ctx[i].stream ==
844 				stream) {
845 			pipes = &link->dc->current_state->res_ctx.pipe_ctx[i];
846 			break;
847 		}
848 	}
849 
850 	if (!pipes)
851 		return;
852 	if (option > DITHER_OPTION_MAX)
853 		return;
854 
855 	dc_exit_ips_for_hw_access(stream->ctx->dc);
856 
857 	stream->dither_option = option;
858 
859 	memset(&params, 0, sizeof(params));
860 	resource_build_bit_depth_reduction_params(stream, &params);
861 	stream->bit_depth_params = params;
862 
863 	if (pipes->plane_res.xfm &&
864 	    pipes->plane_res.xfm->funcs->transform_set_pixel_storage_depth) {
865 		pipes->plane_res.xfm->funcs->transform_set_pixel_storage_depth(
866 			pipes->plane_res.xfm,
867 			pipes->plane_res.scl_data.lb_params.depth,
868 			&stream->bit_depth_params);
869 	}
870 
871 	pipes->stream_res.opp->funcs->
872 		opp_program_bit_depth_reduction(pipes->stream_res.opp, &params);
873 }
874 
875 bool dc_stream_set_gamut_remap(struct dc *dc, const struct dc_stream_state *stream)
876 {
877 	int i;
878 	bool ret = false;
879 	struct pipe_ctx *pipes;
880 
881 	dc_exit_ips_for_hw_access(dc);
882 
883 	for (i = 0; i < MAX_PIPES; i++) {
884 		if (dc->current_state->res_ctx.pipe_ctx[i].stream == stream) {
885 			pipes = &dc->current_state->res_ctx.pipe_ctx[i];
886 			dc->hwss.program_gamut_remap(pipes);
887 			ret = true;
888 		}
889 	}
890 
891 	return ret;
892 }
893 
894 bool dc_stream_program_csc_matrix(struct dc *dc, struct dc_stream_state *stream)
895 {
896 	int i;
897 	bool ret = false;
898 	struct pipe_ctx *pipes;
899 
900 	dc_exit_ips_for_hw_access(dc);
901 
902 	for (i = 0; i < MAX_PIPES; i++) {
903 		if (dc->current_state->res_ctx.pipe_ctx[i].stream
904 				== stream) {
905 
906 			pipes = &dc->current_state->res_ctx.pipe_ctx[i];
907 			dc->hwss.program_output_csc(dc,
908 					pipes,
909 					stream->output_color_space,
910 					stream->csc_color_matrix.matrix,
911 					pipes->stream_res.opp->inst);
912 			ret = true;
913 		}
914 	}
915 
916 	return ret;
917 }
918 
919 void dc_stream_set_static_screen_params(struct dc *dc,
920 		struct dc_stream_state **streams,
921 		int num_streams,
922 		const struct dc_static_screen_params *params)
923 {
924 	int i, j;
925 	struct pipe_ctx *pipes_affected[MAX_PIPES];
926 	int num_pipes_affected = 0;
927 
928 	dc_exit_ips_for_hw_access(dc);
929 
930 	for (i = 0; i < num_streams; i++) {
931 		struct dc_stream_state *stream = streams[i];
932 
933 		for (j = 0; j < MAX_PIPES; j++) {
934 			if (dc->current_state->res_ctx.pipe_ctx[j].stream
935 					== stream) {
936 				pipes_affected[num_pipes_affected++] =
937 						&dc->current_state->res_ctx.pipe_ctx[j];
938 			}
939 		}
940 	}
941 
942 	dc->hwss.set_static_screen_control(pipes_affected, num_pipes_affected, params);
943 }
944 
945 static void dc_destruct(struct dc *dc)
946 {
947 	// reset link encoder assignment table on destruct
948 	if (dc->res_pool && dc->res_pool->funcs->link_encs_assign &&
949 			!dc->config.unify_link_enc_assignment)
950 		link_enc_cfg_init(dc, dc->current_state);
951 
952 	if (dc->current_state) {
953 		dc_state_release(dc->current_state);
954 		dc->current_state = NULL;
955 	}
956 
957 	destroy_links(dc);
958 
959 	destroy_link_encoders(dc);
960 
961 	if (dc->clk_mgr) {
962 		dc_destroy_clk_mgr(dc->clk_mgr);
963 		dc->clk_mgr = NULL;
964 	}
965 
966 	dc_destroy_resource_pool(dc);
967 #ifdef CONFIG_DRM_AMD_DC_FP
968 	dc_destroy_soc_and_ip_translator(&dc->soc_and_ip_translator);
969 #endif
970 	if (dc->link_srv)
971 		link_destroy_link_service(&dc->link_srv);
972 
973 	if (dc->ctx) {
974 		if (dc->ctx->gpio_service)
975 			dal_gpio_service_destroy(&dc->ctx->gpio_service);
976 
977 		if (dc->ctx->created_bios)
978 			dal_bios_parser_destroy(&dc->ctx->dc_bios);
979 		kfree(dc->ctx->logger);
980 		dc_perf_trace_destroy(&dc->ctx->perf_trace);
981 
982 		kfree(dc->ctx);
983 		dc->ctx = NULL;
984 	}
985 
986 	kfree(dc->bw_vbios);
987 	dc->bw_vbios = NULL;
988 
989 	kfree(dc->bw_dceip);
990 	dc->bw_dceip = NULL;
991 
992 	kfree(dc->dcn_soc);
993 	dc->dcn_soc = NULL;
994 
995 	kfree(dc->dcn_ip);
996 	dc->dcn_ip = NULL;
997 
998 	kfree(dc->vm_helper);
999 	dc->vm_helper = NULL;
1000 
1001 }
1002 
1003 static bool dc_construct_ctx(struct dc *dc,
1004 		const struct dc_init_data *init_params)
1005 {
1006 	struct dc_context *dc_ctx;
1007 
1008 	dc_ctx = kzalloc_obj(*dc_ctx);
1009 	if (!dc_ctx)
1010 		return false;
1011 
1012 	dc_stream_init_rmcm_3dlut(dc);
1013 
1014 	dc_ctx->cgs_device = init_params->cgs_device;
1015 	dc_ctx->driver_context = init_params->driver;
1016 	dc_ctx->dc = dc;
1017 	dc_ctx->asic_id = init_params->asic_id;
1018 	dc_ctx->dc_sink_id_count = 0;
1019 	dc_ctx->dc_stream_id_count = 0;
1020 	dc_ctx->dce_environment = init_params->dce_environment;
1021 	dc_ctx->dcn_reg_offsets = init_params->dcn_reg_offsets;
1022 	dc_ctx->nbio_reg_offsets = init_params->nbio_reg_offsets;
1023 	dc_ctx->clk_reg_offsets = init_params->clk_reg_offsets;
1024 
1025 	/* Create logger */
1026 	dc_ctx->logger = kmalloc_obj(*dc_ctx->logger);
1027 
1028 	if (!dc_ctx->logger) {
1029 		kfree(dc_ctx);
1030 		return false;
1031 	}
1032 
1033 	dc_ctx->logger->dev = adev_to_drm(init_params->driver);
1034 	dc->dml.logger = dc_ctx->logger;
1035 
1036 	dc_ctx->dce_version = resource_parse_asic_id(init_params->asic_id);
1037 
1038 	dc_ctx->perf_trace = dc_perf_trace_create();
1039 	if (!dc_ctx->perf_trace) {
1040 		kfree(dc_ctx);
1041 		ASSERT_CRITICAL(false);
1042 		return false;
1043 	}
1044 
1045 	dc->ctx = dc_ctx;
1046 
1047 	dc->link_srv = link_create_link_service();
1048 	if (!dc->link_srv)
1049 		return false;
1050 
1051 	return true;
1052 }
1053 
1054 static bool dc_construct(struct dc *dc,
1055 		const struct dc_init_data *init_params)
1056 {
1057 	struct dc_context *dc_ctx;
1058 	struct bw_calcs_dceip *dc_dceip;
1059 	struct bw_calcs_vbios *dc_vbios;
1060 	struct dcn_soc_bounding_box *dcn_soc;
1061 	struct dcn_ip_params *dcn_ip;
1062 
1063 	dc->config = init_params->flags;
1064 
1065 	// Allocate memory for the vm_helper
1066 	dc->vm_helper = kzalloc_obj(struct vm_helper);
1067 	if (!dc->vm_helper) {
1068 		dm_error("%s: failed to create dc->vm_helper\n", __func__);
1069 		goto fail;
1070 	}
1071 
1072 	memcpy(&dc->bb_overrides, &init_params->bb_overrides, sizeof(dc->bb_overrides));
1073 
1074 	dc_dceip = kzalloc_obj(*dc_dceip);
1075 	if (!dc_dceip) {
1076 		dm_error("%s: failed to create dceip\n", __func__);
1077 		goto fail;
1078 	}
1079 
1080 	dc->bw_dceip = dc_dceip;
1081 
1082 	dc_vbios = kzalloc_obj(*dc_vbios);
1083 	if (!dc_vbios) {
1084 		dm_error("%s: failed to create vbios\n", __func__);
1085 		goto fail;
1086 	}
1087 
1088 	dc->bw_vbios = dc_vbios;
1089 	dcn_soc = kzalloc_obj(*dcn_soc);
1090 	if (!dcn_soc) {
1091 		dm_error("%s: failed to create dcn_soc\n", __func__);
1092 		goto fail;
1093 	}
1094 
1095 	dc->dcn_soc = dcn_soc;
1096 
1097 	dcn_ip = kzalloc_obj(*dcn_ip);
1098 	if (!dcn_ip) {
1099 		dm_error("%s: failed to create dcn_ip\n", __func__);
1100 		goto fail;
1101 	}
1102 
1103 	dc->dcn_ip = dcn_ip;
1104 
1105 	if (init_params->bb_from_dmub)
1106 		dc->dml2_options.bb_from_dmub = init_params->bb_from_dmub;
1107 	else
1108 		dc->dml2_options.bb_from_dmub = NULL;
1109 
1110 	if (!dc_construct_ctx(dc, init_params)) {
1111 		dm_error("%s: failed to create ctx\n", __func__);
1112 		goto fail;
1113 	}
1114 
1115 	dc_ctx = dc->ctx;
1116 
1117 	/* Resource should construct all asic specific resources.
1118 	 * This should be the only place where we need to parse the asic id
1119 	 */
1120 	if (init_params->vbios_override)
1121 		dc_ctx->dc_bios = init_params->vbios_override;
1122 	else {
1123 		/* Create BIOS parser */
1124 		struct bp_init_data bp_init_data;
1125 
1126 		bp_init_data.ctx = dc_ctx;
1127 		bp_init_data.bios = init_params->asic_id.atombios_base_address;
1128 
1129 		dc_ctx->dc_bios = dal_bios_parser_create(
1130 				&bp_init_data, dc_ctx->dce_version);
1131 
1132 		if (!dc_ctx->dc_bios) {
1133 			ASSERT_CRITICAL(false);
1134 			goto fail;
1135 		}
1136 
1137 		dc_ctx->created_bios = true;
1138 	}
1139 
1140 	dc->vendor_signature = init_params->vendor_signature;
1141 
1142 	/* Create GPIO service */
1143 	dc_ctx->gpio_service = dal_gpio_service_create(
1144 			dc_ctx->dce_version,
1145 			dc_ctx->dce_environment,
1146 			dc_ctx);
1147 
1148 	if (!dc_ctx->gpio_service) {
1149 		ASSERT_CRITICAL(false);
1150 		goto fail;
1151 	}
1152 
1153 	dc->res_pool = dc_create_resource_pool(dc, init_params, dc_ctx->dce_version);
1154 	if (!dc->res_pool)
1155 		goto fail;
1156 
1157 	/* set i2c speed if not done by the respective dcnxxx__resource.c */
1158 	if (dc->caps.i2c_speed_in_khz_hdcp == 0)
1159 		dc->caps.i2c_speed_in_khz_hdcp = dc->caps.i2c_speed_in_khz;
1160 	if (dc->check_config.max_optimizable_video_width == 0)
1161 		dc->check_config.max_optimizable_video_width = 5120;
1162 	dc->clk_mgr = dc_clk_mgr_create(dc->ctx, dc->res_pool->pp_smu, dc->res_pool->dccg);
1163 	if (!dc->clk_mgr)
1164 		goto fail;
1165 #ifdef CONFIG_DRM_AMD_DC_FP
1166 	dc->clk_mgr->force_smu_not_present = init_params->force_smu_not_present;
1167 
1168 	if (dc->res_pool->funcs->update_bw_bounding_box) {
1169 		DC_FP_START();
1170 		dc->res_pool->funcs->update_bw_bounding_box(dc, dc->clk_mgr->bw_params);
1171 		DC_FP_END();
1172 	}
1173 	dc->soc_and_ip_translator = dc_create_soc_and_ip_translator(dc_ctx->dce_version);
1174 	if (!dc->soc_and_ip_translator)
1175 		goto fail;
1176 #endif
1177 
1178 	if (!create_links(dc, init_params->num_virtual_links))
1179 		goto fail;
1180 
1181 	/* Create additional DIG link encoder objects if fewer than the platform
1182 	 * supports were created during link construction.
1183 	 */
1184 	if (!create_link_encoders(dc))
1185 		goto fail;
1186 
1187 	/* Creation of current_state must occur after dc->dml
1188 	 * is initialized in dc_create_resource_pool because
1189 	 * on creation it copies the contents of dc->dml
1190 	 */
1191 	dc->current_state = dc_state_create(dc, NULL);
1192 
1193 	if (!dc->current_state) {
1194 		dm_error("%s: failed to create validate ctx\n", __func__);
1195 		goto fail;
1196 	}
1197 
1198 	return true;
1199 
1200 fail:
1201 	return false;
1202 }
1203 
1204 static void disable_all_writeback_pipes_for_stream(
1205 		const struct dc *dc,
1206 		struct dc_stream_state *stream,
1207 		struct dc_state *context)
1208 {
1209 	int i;
1210 
1211 	for (i = 0; i < stream->num_wb_info; i++)
1212 		stream->writeback_info[i].wb_enabled = false;
1213 }
1214 
1215 static void apply_ctx_interdependent_lock(struct dc *dc,
1216 					  struct dc_state *context,
1217 					  struct dc_stream_state *stream,
1218 					  bool lock)
1219 {
1220 	int i;
1221 
1222 	/* Checks if interdependent update function pointer is NULL or not, takes care of DCE110 case */
1223 	if (dc->hwss.interdependent_update_lock)
1224 		dc->hwss.interdependent_update_lock(dc, context, lock);
1225 	else {
1226 		for (i = 0; i < dc->res_pool->pipe_count; i++) {
1227 			struct pipe_ctx *pipe_ctx = &context->res_ctx.pipe_ctx[i];
1228 			struct pipe_ctx *old_pipe_ctx = &dc->current_state->res_ctx.pipe_ctx[i];
1229 
1230 			// Copied conditions that were previously in dce110_apply_ctx_for_surface
1231 			if (stream == pipe_ctx->stream) {
1232 				if (resource_is_pipe_type(pipe_ctx, OPP_HEAD) &&
1233 					(pipe_ctx->plane_state || old_pipe_ctx->plane_state))
1234 					dc->hwss.pipe_control_lock(dc, pipe_ctx, lock);
1235 			}
1236 		}
1237 	}
1238 }
1239 
1240 static void dc_update_visual_confirm_color(struct dc *dc, struct dc_state *context, struct pipe_ctx *pipe_ctx)
1241 {
1242 	if (dc->debug.visual_confirm & VISUAL_CONFIRM_EXPLICIT) {
1243 		memcpy(&pipe_ctx->visual_confirm_color, &pipe_ctx->plane_state->visual_confirm_color,
1244 		sizeof(pipe_ctx->visual_confirm_color));
1245 		return;
1246 	}
1247 
1248 	if (dc->ctx->dce_version >= DCN_VERSION_1_0) {
1249 		memset(&pipe_ctx->visual_confirm_color, 0, sizeof(struct tg_color));
1250 
1251 		if (dc->debug.visual_confirm == VISUAL_CONFIRM_HDR)
1252 			get_hdr_visual_confirm_color(pipe_ctx, &(pipe_ctx->visual_confirm_color));
1253 		else if (dc->debug.visual_confirm == VISUAL_CONFIRM_SURFACE)
1254 			get_surface_visual_confirm_color(pipe_ctx, &(pipe_ctx->visual_confirm_color));
1255 		else if (dc->debug.visual_confirm == VISUAL_CONFIRM_SWIZZLE)
1256 			get_surface_tile_visual_confirm_color(pipe_ctx, &(pipe_ctx->visual_confirm_color));
1257 		else if (dc->debug.visual_confirm == VISUAL_CONFIRM_HW_CURSOR)
1258 			get_cursor_visual_confirm_color(pipe_ctx, &(pipe_ctx->visual_confirm_color));
1259 		else if (dc->debug.visual_confirm == VISUAL_CONFIRM_DCC)
1260 			get_dcc_visual_confirm_color(dc, pipe_ctx, &(pipe_ctx->visual_confirm_color));
1261 		else {
1262 			if (dc->ctx->dce_version < DCN_VERSION_2_0)
1263 				color_space_to_black_color(
1264 					dc, pipe_ctx->stream->output_color_space, &(pipe_ctx->visual_confirm_color));
1265 		}
1266 		if (dc->ctx->dce_version >= DCN_VERSION_2_0) {
1267 			if (dc->debug.visual_confirm == VISUAL_CONFIRM_MPCTREE)
1268 				get_mpctree_visual_confirm_color(pipe_ctx, &(pipe_ctx->visual_confirm_color));
1269 			else if (dc->debug.visual_confirm == VISUAL_CONFIRM_SUBVP)
1270 				get_subvp_visual_confirm_color(pipe_ctx, &(pipe_ctx->visual_confirm_color));
1271 			else if (dc->debug.visual_confirm == VISUAL_CONFIRM_MCLK_SWITCH)
1272 				get_mclk_switch_visual_confirm_color(pipe_ctx, &(pipe_ctx->visual_confirm_color));
1273 			else if (dc->debug.visual_confirm == VISUAL_CONFIRM_FAMS2)
1274 				get_fams2_visual_confirm_color(dc, context, pipe_ctx, &(pipe_ctx->visual_confirm_color));
1275 			else if (dc->debug.visual_confirm == VISUAL_CONFIRM_VABC)
1276 				get_vabc_visual_confirm_color(pipe_ctx, &(pipe_ctx->visual_confirm_color));
1277 		}
1278 	}
1279 }
1280 
1281 void dc_get_visual_confirm_for_stream(
1282 	struct dc *dc,
1283 	struct dc_stream_state *stream_state,
1284 	struct tg_color *color)
1285 {
1286 	struct dc_stream_status *stream_status = dc_stream_get_status(stream_state);
1287 	struct pipe_ctx *pipe_ctx;
1288 	int i;
1289 	struct dc_plane_state *plane_state = NULL;
1290 
1291 	if (!stream_status)
1292 		return;
1293 
1294 	switch (dc->debug.visual_confirm) {
1295 	case VISUAL_CONFIRM_DISABLE:
1296 		return;
1297 	case VISUAL_CONFIRM_PSR:
1298 	case VISUAL_CONFIRM_FAMS:
1299 		pipe_ctx = dc_stream_get_pipe_ctx(stream_state);
1300 		if (!pipe_ctx)
1301 			return;
1302 		dc_dmub_srv_get_visual_confirm_color_cmd(dc, pipe_ctx);
1303 		memcpy(color, &dc->ctx->dmub_srv->dmub->visual_confirm_color, sizeof(struct tg_color));
1304 		return;
1305 
1306 	default:
1307 		/* find plane with highest layer_index */
1308 		for (i = 0; i < stream_status->plane_count; i++) {
1309 			if (stream_status->plane_states[i]->visible)
1310 				plane_state = stream_status->plane_states[i];
1311 		}
1312 		if (!plane_state)
1313 			return;
1314 		/* find pipe that contains plane with highest layer index */
1315 		for (i = 0; i < MAX_PIPES; i++) {
1316 			struct pipe_ctx *pipe = &dc->current_state->res_ctx.pipe_ctx[i];
1317 
1318 			if (pipe->plane_state == plane_state) {
1319 				memcpy(color, &pipe->visual_confirm_color, sizeof(struct tg_color));
1320 				return;
1321 			}
1322 		}
1323 	}
1324 }
1325 
1326 static void disable_dangling_plane(struct dc *dc, struct dc_state *context)
1327 {
1328 	int i, j;
1329 	struct dc_state *dangling_context = dc_state_create_current_copy(dc);
1330 	struct dc_state *current_ctx;
1331 	struct pipe_ctx *pipe;
1332 	struct timing_generator *tg;
1333 
1334 	if (dangling_context == NULL)
1335 		return;
1336 
1337 	for (i = 0; i < dc->res_pool->pipe_count; i++) {
1338 		struct dc_stream_state *old_stream =
1339 				dc->current_state->res_ctx.pipe_ctx[i].stream;
1340 		bool should_disable = true;
1341 		bool pipe_split_change = false;
1342 
1343 		if ((context->res_ctx.pipe_ctx[i].top_pipe) &&
1344 			(dc->current_state->res_ctx.pipe_ctx[i].top_pipe))
1345 			pipe_split_change = context->res_ctx.pipe_ctx[i].top_pipe->pipe_idx !=
1346 				dc->current_state->res_ctx.pipe_ctx[i].top_pipe->pipe_idx;
1347 		else
1348 			pipe_split_change = context->res_ctx.pipe_ctx[i].top_pipe !=
1349 				dc->current_state->res_ctx.pipe_ctx[i].top_pipe;
1350 
1351 		for (j = 0; j < context->stream_count; j++) {
1352 			if (old_stream == context->streams[j]) {
1353 				should_disable = false;
1354 				break;
1355 			}
1356 		}
1357 		if (!should_disable && pipe_split_change &&
1358 				dc->current_state->stream_count != context->stream_count)
1359 			should_disable = true;
1360 
1361 		if (old_stream && !dc->current_state->res_ctx.pipe_ctx[i].top_pipe &&
1362 				!dc->current_state->res_ctx.pipe_ctx[i].prev_odm_pipe) {
1363 			struct pipe_ctx *old_pipe, *new_pipe;
1364 
1365 			old_pipe = &dc->current_state->res_ctx.pipe_ctx[i];
1366 			new_pipe = &context->res_ctx.pipe_ctx[i];
1367 
1368 			if (old_pipe->plane_state && !new_pipe->plane_state)
1369 				should_disable = true;
1370 		}
1371 
1372 		if (should_disable && old_stream) {
1373 			bool is_phantom = dc_state_get_stream_subvp_type(dc->current_state, old_stream) == SUBVP_PHANTOM;
1374 			pipe = &dc->current_state->res_ctx.pipe_ctx[i];
1375 			tg = pipe->stream_res.tg;
1376 			/* When disabling plane for a phantom pipe, we must turn on the
1377 			 * phantom OTG so the disable programming gets the double buffer
1378 			 * update. Otherwise the pipe will be left in a partially disabled
1379 			 * state that can result in underflow or hang when enabling it
1380 			 * again for different use.
1381 			 */
1382 			if (is_phantom) {
1383 				if (tg->funcs->enable_crtc) {
1384 					if (dc->hwseq->funcs.blank_pixel_data)
1385 						dc->hwseq->funcs.blank_pixel_data(dc, pipe, true);
1386 					tg->funcs->enable_crtc(tg);
1387 				}
1388 			}
1389 
1390 			if (is_phantom)
1391 				dc_state_rem_all_phantom_planes_for_stream(dc, old_stream, dangling_context, true);
1392 			else
1393 				dc_state_rem_all_planes_for_stream(dc, old_stream, dangling_context);
1394 			disable_all_writeback_pipes_for_stream(dc, old_stream, dangling_context);
1395 
1396 			if (pipe->stream && pipe->plane_state) {
1397 				if (!dc->debug.using_dml2)
1398 					set_p_state_switch_method(dc, context, pipe);
1399 				dc_update_visual_confirm_color(dc, context, pipe);
1400 			}
1401 
1402 			if (dc->hwss.apply_ctx_for_surface) {
1403 				apply_ctx_interdependent_lock(dc, dc->current_state, old_stream, true);
1404 				dc->hwss.apply_ctx_for_surface(dc, old_stream, 0, dangling_context);
1405 				apply_ctx_interdependent_lock(dc, dc->current_state, old_stream, false);
1406 				dc->hwss.post_unlock_program_front_end(dc, dangling_context);
1407 			}
1408 
1409 			if (dc->res_pool->funcs->prepare_mcache_programming)
1410 				dc->res_pool->funcs->prepare_mcache_programming(dc, dangling_context);
1411 			if (dc->hwss.program_front_end_for_ctx) {
1412 				dc->hwss.interdependent_update_lock(dc, dc->current_state, true);
1413 				dc->hwss.program_front_end_for_ctx(dc, dangling_context);
1414 				dc->hwss.interdependent_update_lock(dc, dc->current_state, false);
1415 				dc->hwss.post_unlock_program_front_end(dc, dangling_context);
1416 			}
1417 			/* We need to put the phantom OTG back into it's default (disabled) state or we
1418 			 * can get corruption when transition from one SubVP config to a different one.
1419 			 * The OTG is set to disable on falling edge of VUPDATE so the plane disable
1420 			 * will still get it's double buffer update.
1421 			 */
1422 			if (is_phantom) {
1423 				if (tg->funcs->disable_phantom_crtc)
1424 					tg->funcs->disable_phantom_crtc(tg);
1425 			}
1426 		}
1427 	}
1428 
1429 	current_ctx = dc->current_state;
1430 	dc->current_state = dangling_context;
1431 	dc_state_release(current_ctx);
1432 }
1433 
1434 static void disable_vbios_mode_if_required(
1435 		struct dc *dc,
1436 		struct dc_state *context)
1437 {
1438 	unsigned int i, j;
1439 
1440 	/* check if timing_changed, disable stream*/
1441 	for (i = 0; i < dc->res_pool->pipe_count; i++) {
1442 		struct dc_stream_state *stream = NULL;
1443 		struct dc_link *link = NULL;
1444 		struct pipe_ctx *pipe = NULL;
1445 
1446 		pipe = &context->res_ctx.pipe_ctx[i];
1447 		stream = pipe->stream;
1448 		if (stream == NULL)
1449 			continue;
1450 
1451 		if (stream->apply_seamless_boot_optimization)
1452 			continue;
1453 
1454 		// only looking for first odm pipe
1455 		if (pipe->prev_odm_pipe)
1456 			continue;
1457 
1458 		if (stream->link->local_sink &&
1459 			stream->link->local_sink->sink_signal == SIGNAL_TYPE_EDP) {
1460 			link = stream->link;
1461 		}
1462 
1463 		if (link != NULL && link->link_enc->funcs->is_dig_enabled(link->link_enc)) {
1464 			unsigned int enc_inst, tg_inst = 0;
1465 			unsigned int pix_clk_100hz = 0;
1466 
1467 			enc_inst = link->link_enc->funcs->get_dig_frontend(link->link_enc);
1468 			if (enc_inst != ENGINE_ID_UNKNOWN) {
1469 				for (j = 0; j < dc->res_pool->stream_enc_count; j++) {
1470 					if (dc->res_pool->stream_enc[j]->id == enc_inst) {
1471 						tg_inst = dc->res_pool->stream_enc[j]->funcs->dig_source_otg(
1472 							dc->res_pool->stream_enc[j]);
1473 						break;
1474 					}
1475 				}
1476 
1477 				dc->res_pool->dp_clock_source->funcs->get_pixel_clk_frequency_100hz(
1478 					dc->res_pool->dp_clock_source,
1479 					tg_inst, &pix_clk_100hz);
1480 
1481 				if (link->link_status.link_active) {
1482 					uint32_t requested_pix_clk_100hz =
1483 						pipe->stream_res.pix_clk_params.requested_pix_clk_100hz;
1484 
1485 					if (pix_clk_100hz != requested_pix_clk_100hz) {
1486 						dc->link_srv->set_dpms_off(pipe);
1487 						pipe->stream->dpms_off = false;
1488 					}
1489 				}
1490 			}
1491 		}
1492 	}
1493 }
1494 
1495 /* Public functions */
1496 
1497 struct dc *dc_create(const struct dc_init_data *init_params)
1498 {
1499 	struct dc *dc = kzalloc_obj(*dc);
1500 	unsigned int full_pipe_count;
1501 
1502 	if (!dc)
1503 		return NULL;
1504 
1505 	if (init_params->dce_environment == DCE_ENV_VIRTUAL_HW) {
1506 		dc->caps.linear_pitch_alignment = 64;
1507 		if (!dc_construct_ctx(dc, init_params))
1508 			goto destruct_dc;
1509 	} else {
1510 		if (!dc_construct(dc, init_params))
1511 			goto destruct_dc;
1512 
1513 		full_pipe_count = dc->res_pool->pipe_count;
1514 		if (dc->res_pool->underlay_pipe_index != NO_UNDERLAY_PIPE)
1515 			full_pipe_count--;
1516 		dc->caps.max_streams = min(
1517 				full_pipe_count,
1518 				dc->res_pool->stream_enc_count);
1519 
1520 		dc->caps.max_links = dc->link_count;
1521 		dc->caps.max_audios = dc->res_pool->audio_count;
1522 		dc->caps.linear_pitch_alignment = 64;
1523 
1524 		dc->caps.max_dp_protocol_version = DP_VERSION_1_4;
1525 
1526 		dc->caps.max_otg_num = dc->res_pool->res_cap->num_timing_generator;
1527 
1528 		if (dc->res_pool->dmcu != NULL)
1529 			dc->versions.dmcu_version = dc->res_pool->dmcu->dmcu_version;
1530 	}
1531 
1532 	dc->dcn_reg_offsets = init_params->dcn_reg_offsets;
1533 	dc->nbio_reg_offsets = init_params->nbio_reg_offsets;
1534 	dc->clk_reg_offsets = init_params->clk_reg_offsets;
1535 
1536 	/* Populate versioning information */
1537 	dc->versions.dc_ver = DC_VER;
1538 
1539 	dc->build_id = DC_BUILD_ID;
1540 
1541 	DC_LOG_DC("Display Core initialized\n");
1542 
1543 	return dc;
1544 
1545 destruct_dc:
1546 	dc_destruct(dc);
1547 	kfree(dc);
1548 	return NULL;
1549 }
1550 
1551 static void detect_edp_presence(struct dc *dc)
1552 {
1553 	struct dc_link *edp_links[MAX_NUM_EDP];
1554 	struct dc_link *edp_link = NULL;
1555 	enum dc_connection_type type;
1556 	int i;
1557 	int edp_num;
1558 
1559 	dc_get_edp_links(dc, edp_links, &edp_num);
1560 	if (!edp_num)
1561 		return;
1562 
1563 	for (i = 0; i < edp_num; i++) {
1564 		edp_link = edp_links[i];
1565 		if (dc->config.edp_not_connected) {
1566 			edp_link->edp_sink_present = false;
1567 		} else {
1568 			dc_link_detect_connection_type(edp_link, &type);
1569 			edp_link->edp_sink_present = (type != dc_connection_none);
1570 		}
1571 	}
1572 }
1573 
1574 void dc_hardware_init(struct dc *dc)
1575 {
1576 
1577 	detect_edp_presence(dc);
1578 	if (dc->ctx->dce_environment != DCE_ENV_VIRTUAL_HW)
1579 		dc->hwss.init_hw(dc);
1580 	dc_dmub_srv_notify_fw_dc_power_state(dc->ctx->dmub_srv, DC_ACPI_CM_POWER_STATE_D0);
1581 }
1582 
1583 void dc_init_callbacks(struct dc *dc,
1584 		const struct dc_callback_init *init_params)
1585 {
1586 	dc->ctx->cp_psp = init_params->cp_psp;
1587 }
1588 
1589 void dc_deinit_callbacks(struct dc *dc)
1590 {
1591 	memset(&dc->ctx->cp_psp, 0, sizeof(dc->ctx->cp_psp));
1592 }
1593 
1594 void dc_destroy(struct dc **dc)
1595 {
1596 	dc_destruct(*dc);
1597 	kfree(*dc);
1598 	*dc = NULL;
1599 }
1600 
1601 static void enable_timing_multisync(
1602 		struct dc *dc,
1603 		struct dc_state *ctx)
1604 {
1605 	int i, multisync_count = 0;
1606 	int pipe_count = dc->res_pool->pipe_count;
1607 	struct pipe_ctx *multisync_pipes[MAX_PIPES] = { NULL };
1608 
1609 	for (i = 0; i < pipe_count; i++) {
1610 		if (!ctx->res_ctx.pipe_ctx[i].stream ||
1611 				!ctx->res_ctx.pipe_ctx[i].stream->triggered_crtc_reset.enabled)
1612 			continue;
1613 		if (ctx->res_ctx.pipe_ctx[i].stream == ctx->res_ctx.pipe_ctx[i].stream->triggered_crtc_reset.event_source)
1614 			continue;
1615 		multisync_pipes[multisync_count] = &ctx->res_ctx.pipe_ctx[i];
1616 		multisync_count++;
1617 	}
1618 
1619 	if (multisync_count > 0) {
1620 		dc->hwss.enable_per_frame_crtc_position_reset(
1621 			dc, multisync_count, multisync_pipes);
1622 	}
1623 }
1624 
1625 static void program_timing_sync(
1626 		struct dc *dc,
1627 		struct dc_state *ctx)
1628 {
1629 	int i, j, k;
1630 	int group_index = 0;
1631 	int num_group = 0;
1632 	int pipe_count = dc->res_pool->pipe_count;
1633 	struct pipe_ctx *unsynced_pipes[MAX_PIPES] = { NULL };
1634 
1635 	for (i = 0; i < pipe_count; i++) {
1636 		if (!ctx->res_ctx.pipe_ctx[i].stream
1637 				|| ctx->res_ctx.pipe_ctx[i].top_pipe
1638 				|| ctx->res_ctx.pipe_ctx[i].prev_odm_pipe)
1639 			continue;
1640 
1641 		unsynced_pipes[i] = &ctx->res_ctx.pipe_ctx[i];
1642 	}
1643 
1644 	for (i = 0; i < pipe_count; i++) {
1645 		int group_size = 1;
1646 		enum timing_synchronization_type sync_type = NOT_SYNCHRONIZABLE;
1647 		struct pipe_ctx *pipe_set[MAX_PIPES];
1648 
1649 		if (!unsynced_pipes[i])
1650 			continue;
1651 
1652 		pipe_set[0] = unsynced_pipes[i];
1653 		unsynced_pipes[i] = NULL;
1654 
1655 		/* Add tg to the set, search rest of the tg's for ones with
1656 		 * same timing, add all tgs with same timing to the group
1657 		 */
1658 		for (j = i + 1; j < pipe_count; j++) {
1659 			if (!unsynced_pipes[j])
1660 				continue;
1661 			if (sync_type != TIMING_SYNCHRONIZABLE &&
1662 				dc->hwss.enable_vblanks_synchronization &&
1663 				unsynced_pipes[j]->stream_res.tg->funcs->align_vblanks &&
1664 				resource_are_vblanks_synchronizable(
1665 					unsynced_pipes[j]->stream,
1666 					pipe_set[0]->stream)) {
1667 				sync_type = VBLANK_SYNCHRONIZABLE;
1668 				pipe_set[group_size] = unsynced_pipes[j];
1669 				unsynced_pipes[j] = NULL;
1670 				group_size++;
1671 			} else
1672 			if (sync_type != VBLANK_SYNCHRONIZABLE &&
1673 				resource_are_streams_timing_synchronizable(
1674 					unsynced_pipes[j]->stream,
1675 					pipe_set[0]->stream)) {
1676 				sync_type = TIMING_SYNCHRONIZABLE;
1677 				pipe_set[group_size] = unsynced_pipes[j];
1678 				unsynced_pipes[j] = NULL;
1679 				group_size++;
1680 			}
1681 		}
1682 
1683 		/* set first unblanked pipe as master */
1684 		for (j = 0; j < group_size; j++) {
1685 			bool is_blanked;
1686 
1687 			if (pipe_set[j]->stream_res.opp->funcs->dpg_is_blanked)
1688 				is_blanked =
1689 					pipe_set[j]->stream_res.opp->funcs->dpg_is_blanked(pipe_set[j]->stream_res.opp);
1690 			else
1691 				is_blanked =
1692 					pipe_set[j]->stream_res.tg->funcs->is_blanked(pipe_set[j]->stream_res.tg);
1693 			if (!is_blanked) {
1694 				if (j == 0)
1695 					break;
1696 
1697 				swap(pipe_set[0], pipe_set[j]);
1698 				break;
1699 			}
1700 		}
1701 
1702 		for (k = 0; k < group_size; k++) {
1703 			struct dc_stream_status *status = dc_state_get_stream_status(ctx, pipe_set[k]->stream);
1704 
1705 			if (!status)
1706 				continue;
1707 
1708 			status->timing_sync_info.group_id = num_group;
1709 			status->timing_sync_info.group_size = group_size;
1710 			if (k == 0)
1711 				status->timing_sync_info.master = true;
1712 			else
1713 				status->timing_sync_info.master = false;
1714 
1715 		}
1716 
1717 		/* remove any other unblanked pipes as they have already been synced */
1718 		if (dc->config.use_pipe_ctx_sync_logic) {
1719 			/* check pipe's syncd to decide which pipe to be removed */
1720 			for (j = 1; j < group_size; j++) {
1721 				if (pipe_set[j]->pipe_idx_syncd == pipe_set[0]->pipe_idx_syncd) {
1722 					group_size--;
1723 					pipe_set[j] = pipe_set[group_size];
1724 					j--;
1725 				} else
1726 					/* link slave pipe's syncd with master pipe */
1727 					pipe_set[j]->pipe_idx_syncd = pipe_set[0]->pipe_idx_syncd;
1728 			}
1729 		} else {
1730 			/* remove any other pipes by checking valid plane */
1731 			for (j = j + 1; j < group_size; j++) {
1732 				bool is_blanked;
1733 
1734 				if (pipe_set[j]->stream_res.opp->funcs->dpg_is_blanked)
1735 					is_blanked =
1736 						pipe_set[j]->stream_res.opp->funcs->dpg_is_blanked(pipe_set[j]->stream_res.opp);
1737 				else
1738 					is_blanked =
1739 						pipe_set[j]->stream_res.tg->funcs->is_blanked(pipe_set[j]->stream_res.tg);
1740 				if (!is_blanked) {
1741 					group_size--;
1742 					pipe_set[j] = pipe_set[group_size];
1743 					j--;
1744 				}
1745 			}
1746 		}
1747 
1748 		if (group_size > 1) {
1749 			if (sync_type == TIMING_SYNCHRONIZABLE) {
1750 				dc->hwss.enable_timing_synchronization(
1751 					dc, ctx, group_index, group_size, pipe_set);
1752 			} else
1753 				if (sync_type == VBLANK_SYNCHRONIZABLE) {
1754 				dc->hwss.enable_vblanks_synchronization(
1755 					dc, group_index, group_size, pipe_set);
1756 				}
1757 			group_index++;
1758 		}
1759 		num_group++;
1760 	}
1761 }
1762 
1763 static bool streams_changed(struct dc *dc,
1764 			    struct dc_stream_state *streams[],
1765 			    uint8_t stream_count)
1766 {
1767 	uint8_t i;
1768 
1769 	if (stream_count != dc->current_state->stream_count)
1770 		return true;
1771 
1772 	for (i = 0; i < dc->current_state->stream_count; i++) {
1773 		if (dc->current_state->streams[i] != streams[i])
1774 			return true;
1775 		if (!streams[i]->link->link_state_valid)
1776 			return true;
1777 	}
1778 
1779 	return false;
1780 }
1781 
1782 bool dc_validate_boot_timing(const struct dc *dc,
1783 				const struct dc_sink *sink,
1784 				struct dc_crtc_timing *crtc_timing)
1785 {
1786 	struct timing_generator *tg;
1787 	struct stream_encoder *se = NULL;
1788 
1789 	struct dc_crtc_timing hw_crtc_timing = {0};
1790 
1791 	struct dc_link *link = sink->link;
1792 	unsigned int i, enc_inst, tg_inst = 0;
1793 
1794 	/* Support seamless boot on EDP displays only */
1795 	if (sink->sink_signal != SIGNAL_TYPE_EDP) {
1796 		return false;
1797 	}
1798 
1799 	if (dc->debug.force_odm_combine) {
1800 		DC_LOG_DEBUG("boot timing validation failed due to force_odm_combine\n");
1801 		return false;
1802 	}
1803 
1804 	/* Check for enabled DIG to identify enabled display */
1805 	if (!link->link_enc->funcs->is_dig_enabled(link->link_enc)) {
1806 		DC_LOG_DEBUG("boot timing validation failed due to disabled DIG\n");
1807 		return false;
1808 	}
1809 
1810 	enc_inst = link->link_enc->funcs->get_dig_frontend(link->link_enc);
1811 
1812 	if (enc_inst == ENGINE_ID_UNKNOWN) {
1813 		DC_LOG_DEBUG("boot timing validation failed due to unknown DIG engine ID\n");
1814 		return false;
1815 	}
1816 
1817 	for (i = 0; i < dc->res_pool->stream_enc_count; i++) {
1818 		if (dc->res_pool->stream_enc[i]->id == enc_inst) {
1819 
1820 			se = dc->res_pool->stream_enc[i];
1821 
1822 			tg_inst = dc->res_pool->stream_enc[i]->funcs->dig_source_otg(
1823 				dc->res_pool->stream_enc[i]);
1824 			break;
1825 		}
1826 	}
1827 
1828 	// tg_inst not found
1829 	if (i == dc->res_pool->stream_enc_count) {
1830 		DC_LOG_DEBUG("boot timing validation failed due to timing generator instance not found\n");
1831 		return false;
1832 	}
1833 
1834 	if (tg_inst >= dc->res_pool->timing_generator_count) {
1835 		DC_LOG_DEBUG("boot timing validation failed due to invalid timing generator count\n");
1836 		return false;
1837 	}
1838 
1839 	if (tg_inst != link->link_enc->preferred_engine) {
1840 		DC_LOG_DEBUG("boot timing validation failed due to non-preferred timing generator\n");
1841 		return false;
1842 	}
1843 
1844 	tg = dc->res_pool->timing_generators[tg_inst];
1845 
1846 	if (!tg->funcs->get_hw_timing) {
1847 		DC_LOG_DEBUG("boot timing validation failed due to missing get_hw_timing callback\n");
1848 		return false;
1849 	}
1850 
1851 	if (!tg->funcs->get_hw_timing(tg, &hw_crtc_timing)) {
1852 		DC_LOG_DEBUG("boot timing validation failed due to failed get_hw_timing return\n");
1853 		return false;
1854 	}
1855 
1856 	if (crtc_timing->h_total != hw_crtc_timing.h_total) {
1857 		DC_LOG_DEBUG("boot timing validation failed due to h_total mismatch\n");
1858 		return false;
1859 	}
1860 
1861 	if (crtc_timing->h_border_left != hw_crtc_timing.h_border_left) {
1862 		DC_LOG_DEBUG("boot timing validation failed due to h_border_left mismatch\n");
1863 		return false;
1864 	}
1865 
1866 	if (crtc_timing->h_addressable != hw_crtc_timing.h_addressable) {
1867 		DC_LOG_DEBUG("boot timing validation failed due to h_addressable mismatch\n");
1868 		return false;
1869 	}
1870 
1871 	if (crtc_timing->h_border_right != hw_crtc_timing.h_border_right) {
1872 		DC_LOG_DEBUG("boot timing validation failed due to h_border_right mismatch\n");
1873 		return false;
1874 	}
1875 
1876 	if (crtc_timing->h_front_porch != hw_crtc_timing.h_front_porch) {
1877 		DC_LOG_DEBUG("boot timing validation failed due to h_front_porch mismatch\n");
1878 		return false;
1879 	}
1880 
1881 	if (crtc_timing->h_sync_width != hw_crtc_timing.h_sync_width) {
1882 		DC_LOG_DEBUG("boot timing validation failed due to h_sync_width mismatch\n");
1883 		return false;
1884 	}
1885 
1886 	if (crtc_timing->v_total != hw_crtc_timing.v_total) {
1887 		DC_LOG_DEBUG("boot timing validation failed due to v_total mismatch\n");
1888 		return false;
1889 	}
1890 
1891 	if (crtc_timing->v_border_top != hw_crtc_timing.v_border_top) {
1892 		DC_LOG_DEBUG("boot timing validation failed due to v_border_top mismatch\n");
1893 		return false;
1894 	}
1895 
1896 	if (crtc_timing->v_addressable != hw_crtc_timing.v_addressable) {
1897 		DC_LOG_DEBUG("boot timing validation failed due to v_addressable mismatch\n");
1898 		return false;
1899 	}
1900 
1901 	if (crtc_timing->v_border_bottom != hw_crtc_timing.v_border_bottom) {
1902 		DC_LOG_DEBUG("boot timing validation failed due to v_border_bottom mismatch\n");
1903 		return false;
1904 	}
1905 
1906 	if (crtc_timing->v_front_porch != hw_crtc_timing.v_front_porch) {
1907 		DC_LOG_DEBUG("boot timing validation failed due to v_front_porch mismatch\n");
1908 		return false;
1909 	}
1910 
1911 	if (crtc_timing->v_sync_width != hw_crtc_timing.v_sync_width) {
1912 		DC_LOG_DEBUG("boot timing validation failed due to v_sync_width mismatch\n");
1913 		return false;
1914 	}
1915 
1916 	/* block DSC for now, as VBIOS does not currently support DSC timings */
1917 	if (crtc_timing->flags.DSC) {
1918 		DC_LOG_DEBUG("boot timing validation failed due to DSC\n");
1919 		return false;
1920 	}
1921 
1922 	if (dc_is_dp_signal(link->connector_signal)) {
1923 		unsigned int pix_clk_100hz = 0;
1924 		uint32_t numOdmPipes = 1;
1925 		uint32_t id_src[4] = {0};
1926 
1927 		dc->res_pool->dp_clock_source->funcs->get_pixel_clk_frequency_100hz(
1928 			dc->res_pool->dp_clock_source,
1929 			tg_inst, &pix_clk_100hz);
1930 
1931 		if (tg->funcs->get_optc_source)
1932 			tg->funcs->get_optc_source(tg,
1933 						&numOdmPipes, &id_src[0], &id_src[1]);
1934 
1935 		if (numOdmPipes == 2) {
1936 			pix_clk_100hz *= 2;
1937 		} else if (numOdmPipes == 4) {
1938 			pix_clk_100hz *= 4;
1939 		} else if (se && se->funcs->get_pixels_per_cycle) {
1940 			uint32_t pixels_per_cycle = se->funcs->get_pixels_per_cycle(se);
1941 
1942 			if (pixels_per_cycle != 1 && !dc->debug.enable_dp_dig_pixel_rate_div_policy) {
1943 				DC_LOG_DEBUG("boot timing validation failed due to pixels_per_cycle\n");
1944 				return false;
1945 			}
1946 
1947 			pix_clk_100hz *= pixels_per_cycle;
1948 		}
1949 
1950 		// Note: In rare cases, HW pixclk may differ from crtc's pixclk
1951 		// slightly due to rounding issues in 10 kHz units.
1952 		if (crtc_timing->pix_clk_100hz != pix_clk_100hz) {
1953 			DC_LOG_DEBUG("boot timing validation failed due to pix_clk_100hz mismatch\n");
1954 			return false;
1955 		}
1956 
1957 		if (!se || !se->funcs->dp_get_pixel_format) {
1958 			DC_LOG_DEBUG("boot timing validation failed due to missing dp_get_pixel_format\n");
1959 			return false;
1960 		}
1961 
1962 		if (!se->funcs->dp_get_pixel_format(
1963 			se,
1964 			&hw_crtc_timing.pixel_encoding,
1965 			&hw_crtc_timing.display_color_depth)) {
1966 			DC_LOG_DEBUG("boot timing validation failed due to dp_get_pixel_format failure\n");
1967 			return false;
1968 		}
1969 
1970 		if (hw_crtc_timing.display_color_depth != crtc_timing->display_color_depth) {
1971 			DC_LOG_DEBUG("boot timing validation failed due to display_color_depth mismatch\n");
1972 			return false;
1973 		}
1974 
1975 		if (hw_crtc_timing.pixel_encoding != crtc_timing->pixel_encoding) {
1976 			DC_LOG_DEBUG("boot timing validation failed due to pixel_encoding mismatch\n");
1977 			return false;
1978 		}
1979 	}
1980 
1981 
1982 	if (link->dpcd_caps.dprx_feature.bits.VSC_SDP_COLORIMETRY_SUPPORTED) {
1983 		DC_LOG_DEBUG("boot timing validation failed due to VSC SDP colorimetry\n");
1984 		return false;
1985 	}
1986 
1987 	if (link->dpcd_caps.channel_coding_cap.bits.DP_128b_132b_SUPPORTED) {
1988 		DC_LOG_DEBUG("boot timing validation failed due to DP 128b/132b\n");
1989 		return false;
1990 	}
1991 
1992 	if (dc->link_srv->edp_is_ilr_optimization_required(link, crtc_timing)) {
1993 		DC_LOG_EVENT_LINK_TRAINING("Seamless boot disabled to optimize eDP link rate\n");
1994 		return false;
1995 	}
1996 
1997 	return true;
1998 }
1999 
2000 static inline bool should_update_pipe_for_stream(
2001 		struct dc_state *context,
2002 		struct pipe_ctx *pipe_ctx,
2003 		struct dc_stream_state *stream)
2004 {
2005 	return (pipe_ctx->stream && pipe_ctx->stream == stream);
2006 }
2007 
2008 static inline bool should_update_pipe_for_plane(
2009 		struct dc_state *context,
2010 		struct pipe_ctx *pipe_ctx,
2011 		struct dc_plane_state *plane_state)
2012 {
2013 	return (pipe_ctx->plane_state == plane_state);
2014 }
2015 
2016 void dc_enable_stereo(
2017 	struct dc *dc,
2018 	struct dc_state *context,
2019 	struct dc_stream_state *streams[],
2020 	uint8_t stream_count)
2021 {
2022 	int i, j;
2023 	struct pipe_ctx *pipe;
2024 
2025 	dc_exit_ips_for_hw_access(dc);
2026 
2027 	for (i = 0; i < MAX_PIPES; i++) {
2028 		if (context != NULL) {
2029 			pipe = &context->res_ctx.pipe_ctx[i];
2030 		} else {
2031 			context = dc->current_state;
2032 			pipe = &dc->current_state->res_ctx.pipe_ctx[i];
2033 		}
2034 
2035 		for (j = 0; pipe && j < stream_count; j++)  {
2036 			if (should_update_pipe_for_stream(context, pipe, streams[j]) &&
2037 				dc->hwss.setup_stereo)
2038 				dc->hwss.setup_stereo(pipe, dc);
2039 		}
2040 	}
2041 }
2042 
2043 void dc_trigger_sync(struct dc *dc, struct dc_state *context)
2044 {
2045 	if (context->stream_count > 1 && !dc->debug.disable_timing_sync) {
2046 		dc_exit_ips_for_hw_access(dc);
2047 
2048 		enable_timing_multisync(dc, context);
2049 		program_timing_sync(dc, context);
2050 	}
2051 }
2052 
2053 static uint8_t get_stream_mask(struct dc *dc, struct dc_state *context)
2054 {
2055 	int i;
2056 	unsigned int stream_mask = 0;
2057 
2058 	for (i = 0; i < dc->res_pool->pipe_count; i++) {
2059 		if (context->res_ctx.pipe_ctx[i].stream)
2060 			stream_mask |= 1 << i;
2061 	}
2062 
2063 	return stream_mask;
2064 }
2065 
2066 void dc_z10_restore(const struct dc *dc)
2067 {
2068 	if (dc->hwss.z10_restore)
2069 		dc->hwss.z10_restore(dc);
2070 }
2071 
2072 void dc_z10_save_init(struct dc *dc)
2073 {
2074 	if (dc->hwss.z10_save_init)
2075 		dc->hwss.z10_save_init(dc);
2076 }
2077 
2078 /* Set a pipe unlock order based on the change in DET allocation and stores it in dc scratch memory
2079  * Prevents over allocation of DET during unlock process
2080  * e.g. 2 pipe config with different streams with a max of 20 DET segments
2081  *	Before:								After:
2082  *		- Pipe0: 10 DET segments			- Pipe0: 12 DET segments
2083  *		- Pipe1: 10 DET segments			- Pipe1: 8 DET segments
2084  * If Pipe0 gets updated first, 22 DET segments will be allocated
2085  */
2086 static void determine_pipe_unlock_order(struct dc *dc, struct dc_state *context)
2087 {
2088 	unsigned int i = 0;
2089 	struct pipe_ctx *pipe = NULL;
2090 	struct timing_generator *tg = NULL;
2091 
2092 	if (!dc->config.set_pipe_unlock_order)
2093 		return;
2094 
2095 	memset(dc->scratch.pipes_to_unlock_first, 0, sizeof(dc->scratch.pipes_to_unlock_first));
2096 	for (i = 0; i < dc->res_pool->pipe_count; i++) {
2097 		pipe = &context->res_ctx.pipe_ctx[i];
2098 		tg = pipe->stream_res.tg;
2099 
2100 		if (!resource_is_pipe_type(pipe, OTG_MASTER) ||
2101 				!tg->funcs->is_tg_enabled(tg) ||
2102 				dc_state_get_pipe_subvp_type(context, pipe) == SUBVP_PHANTOM) {
2103 			continue;
2104 		}
2105 
2106 		if (resource_calculate_det_for_stream(context, pipe) <
2107 				resource_calculate_det_for_stream(dc->current_state, &dc->current_state->res_ctx.pipe_ctx[i])) {
2108 			dc->scratch.pipes_to_unlock_first[i] = true;
2109 		}
2110 	}
2111 }
2112 
2113 /**
2114  * dc_commit_state_no_check - Apply context to the hardware
2115  *
2116  * @dc: DC object with the current status to be updated
2117  * @context: New state that will become the current status at the end of this function
2118  *
2119  * Applies given context to the hardware and copy it into current context.
2120  * It's up to the user to release the src context afterwards.
2121  *
2122  * Return: an enum dc_status result code for the operation
2123  */
2124 static enum dc_status dc_commit_state_no_check(struct dc *dc, struct dc_state *context)
2125 {
2126 	struct dc_bios *dcb = dc->ctx->dc_bios;
2127 	enum dc_status result = DC_ERROR_UNEXPECTED;
2128 	struct pipe_ctx *pipe;
2129 	int i, k, l;
2130 	struct dc_stream_state *dc_streams[MAX_STREAMS] = {0};
2131 	struct dc_state *old_state;
2132 	bool subvp_prev_use = false;
2133 
2134 	dc_z10_restore(dc);
2135 	dc_allow_idle_optimizations(dc, false);
2136 
2137 	for (i = 0; i < dc->res_pool->pipe_count; i++) {
2138 		struct pipe_ctx *old_pipe = &dc->current_state->res_ctx.pipe_ctx[i];
2139 
2140 		/* Check old context for SubVP */
2141 		subvp_prev_use |= (dc_state_get_pipe_subvp_type(dc->current_state, old_pipe) == SUBVP_PHANTOM);
2142 		if (subvp_prev_use)
2143 			break;
2144 	}
2145 
2146 	for (i = 0; i < context->stream_count; i++)
2147 		dc_streams[i] =  context->streams[i];
2148 
2149 	if (!dcb->funcs->is_accelerated_mode(dcb)) {
2150 		disable_vbios_mode_if_required(dc, context);
2151 		dc->hwss.enable_accelerated_mode(dc, context);
2152 	} else if (get_seamless_boot_stream_count(dc->current_state) > 0) {
2153 		/* If the previous Stream still retains the apply seamless boot flag,
2154 		 * it means the OS has not actually performed a flip yet.
2155 		 * At this point, if we receive dc_commit_streams again, we should
2156 		 * once more check whether the actual HW timing matches what the OS
2157 		 * has provided
2158 		 */
2159 		disable_vbios_mode_if_required(dc, context);
2160 	}
2161 
2162 	if (dc->hwseq->funcs.wait_for_pipe_update_if_needed) {
2163 		for (i = 0; i < dc->res_pool->pipe_count; i++) {
2164 			pipe = &context->res_ctx.pipe_ctx[i];
2165 			//Only delay otg master for a given config
2166 			if (resource_is_pipe_type(pipe, OTG_MASTER)) {
2167 				//dc_commit_state_no_check is always a full update
2168 				dc->hwseq->funcs.wait_for_pipe_update_if_needed(dc, pipe, false);
2169 				break;
2170 			}
2171 		}
2172 	}
2173 
2174 	if (context->stream_count > get_seamless_boot_stream_count(context) ||
2175 		context->stream_count == 0)
2176 		dc->hwss.prepare_bandwidth(dc, context);
2177 
2178 	/* When SubVP is active, all HW programming must be done while
2179 	 * SubVP lock is acquired
2180 	 */
2181 	if (dc->hwss.subvp_pipe_control_lock)
2182 		dc->hwss.subvp_pipe_control_lock(dc, context, true, true, NULL, subvp_prev_use);
2183 	if (dc->hwss.dmub_hw_control_lock)
2184 		dc->hwss.dmub_hw_control_lock(dc, context, true);
2185 
2186 	if (dc->hwss.update_dsc_pg)
2187 		dc->hwss.update_dsc_pg(dc, context, false);
2188 
2189 	disable_dangling_plane(dc, context);
2190 	/* re-program planes for existing stream, in case we need to
2191 	 * free up plane resource for later use
2192 	 */
2193 	if (dc->hwss.apply_ctx_for_surface) {
2194 		for (i = 0; i < context->stream_count; i++) {
2195 			if (context->streams[i]->mode_changed)
2196 				continue;
2197 			apply_ctx_interdependent_lock(dc, context, context->streams[i], true);
2198 			dc->hwss.apply_ctx_for_surface(
2199 				dc, context->streams[i],
2200 				context->stream_status[i].plane_count,
2201 				context); /* use new pipe config in new context */
2202 			apply_ctx_interdependent_lock(dc, context, context->streams[i], false);
2203 			dc->hwss.post_unlock_program_front_end(dc, context);
2204 		}
2205 	}
2206 
2207 	/* Program hardware */
2208 	for (i = 0; i < dc->res_pool->pipe_count; i++) {
2209 		pipe = &context->res_ctx.pipe_ctx[i];
2210 		dc->hwss.wait_for_mpcc_disconnect(dc, dc->res_pool, pipe);
2211 	}
2212 
2213 	for (i = 0; i < dc->current_state->stream_count; i++)
2214 		dc_dmub_srv_control_cursor_offload(dc, dc->current_state, dc->current_state->streams[i], false);
2215 
2216 	result = dc->hwss.apply_ctx_to_hw(dc, context);
2217 
2218 	for (i = 0; i < context->stream_count; i++)
2219 		dc_dmub_srv_control_cursor_offload(dc, context, context->streams[i], true);
2220 
2221 	if (result != DC_OK) {
2222 		/* Application of dc_state to hardware stopped. */
2223 		dc->current_state->res_ctx.link_enc_cfg_ctx.mode = LINK_ENC_CFG_STEADY;
2224 		return result;
2225 	}
2226 
2227 	dc_trigger_sync(dc, context);
2228 
2229 	/* Full update should unconditionally be triggered when dc_commit_state_no_check is called */
2230 	for (i = 0; i < context->stream_count; i++) {
2231 		uint32_t prev_dsc_changed = context->streams[i]->update_flags.bits.dsc_changed;
2232 
2233 		context->streams[i]->update_flags.raw = 0xFFFFFFFF;
2234 		context->streams[i]->update_flags.bits.dsc_changed = prev_dsc_changed;
2235 	}
2236 
2237 	determine_pipe_unlock_order(dc, context);
2238 	/* Program all planes within new context*/
2239 	if (dc->res_pool->funcs->prepare_mcache_programming)
2240 		dc->res_pool->funcs->prepare_mcache_programming(dc, context);
2241 	if (dc->hwss.program_front_end_for_ctx) {
2242 		dc->hwss.interdependent_update_lock(dc, context, true);
2243 		dc->hwss.program_front_end_for_ctx(dc, context);
2244 
2245 		if (dc->hwseq->funcs.set_wait_for_update_needed_for_pipe) {
2246 			for (i = 0; i < dc->res_pool->pipe_count; i++) {
2247 				pipe = &context->res_ctx.pipe_ctx[i];
2248 				dc->hwseq->funcs.set_wait_for_update_needed_for_pipe(dc, pipe);
2249 			}
2250 		}
2251 
2252 		dc->hwss.interdependent_update_lock(dc, context, false);
2253 		dc->hwss.post_unlock_program_front_end(dc, context);
2254 	}
2255 
2256 	if (dc->hwss.commit_subvp_config)
2257 		dc->hwss.commit_subvp_config(dc, context);
2258 	if (dc->hwss.subvp_pipe_control_lock)
2259 		dc->hwss.subvp_pipe_control_lock(dc, context, false, true, NULL, subvp_prev_use);
2260 	if (dc->hwss.dmub_hw_control_lock)
2261 		dc->hwss.dmub_hw_control_lock(dc, context, false);
2262 
2263 	for (i = 0; i < context->stream_count; i++) {
2264 		const struct dc_link *link = context->streams[i]->link;
2265 
2266 		if (!context->streams[i]->mode_changed)
2267 			continue;
2268 
2269 		if (dc->hwss.apply_ctx_for_surface) {
2270 			apply_ctx_interdependent_lock(dc, context, context->streams[i], true);
2271 			dc->hwss.apply_ctx_for_surface(
2272 					dc, context->streams[i],
2273 					context->stream_status[i].plane_count,
2274 					context);
2275 			apply_ctx_interdependent_lock(dc, context, context->streams[i], false);
2276 			dc->hwss.post_unlock_program_front_end(dc, context);
2277 		}
2278 
2279 		/*
2280 		 * enable stereo
2281 		 * TODO rework dc_enable_stereo call to work with validation sets?
2282 		 */
2283 		for (k = 0; k < MAX_PIPES; k++) {
2284 			pipe = &context->res_ctx.pipe_ctx[k];
2285 
2286 			for (l = 0 ; pipe && l < context->stream_count; l++)  {
2287 				if (context->streams[l] &&
2288 					context->streams[l] == pipe->stream &&
2289 					dc->hwss.setup_stereo)
2290 					dc->hwss.setup_stereo(pipe, dc);
2291 			}
2292 		}
2293 
2294 		CONN_MSG_MODE(link, "{%dx%d, %dx%d@%dKhz}",
2295 				context->streams[i]->timing.h_addressable,
2296 				context->streams[i]->timing.v_addressable,
2297 				context->streams[i]->timing.h_total,
2298 				context->streams[i]->timing.v_total,
2299 				context->streams[i]->timing.pix_clk_100hz / 10);
2300 	}
2301 
2302 	dc_enable_stereo(dc, context, dc_streams, context->stream_count);
2303 
2304 	if (get_seamless_boot_stream_count(context) == 0 ||
2305 		context->stream_count == 0) {
2306 		/* Must wait for no flips to be pending before doing optimize bw */
2307 		hwss_wait_for_no_pipes_pending(dc, context);
2308 		/*
2309 		 * optimized dispclk depends on ODM setup. Need to wait for ODM
2310 		 * update pending complete before optimizing bandwidth.
2311 		 */
2312 		hwss_wait_for_odm_update_pending_complete(dc, context);
2313 		/* pplib is notified if disp_num changed */
2314 		dc->hwss.optimize_bandwidth(dc, context);
2315 		/* Need to do otg sync again as otg could be out of sync due to otg
2316 		 * workaround applied during clock update
2317 		 */
2318 		dc_trigger_sync(dc, context);
2319 	}
2320 
2321 	if (dc->hwss.update_dsc_pg)
2322 		dc->hwss.update_dsc_pg(dc, context, true);
2323 
2324 	if (dc->ctx->dce_version >= DCE_VERSION_MAX)
2325 		TRACE_DCN_CLOCK_STATE(&context->bw_ctx.bw.dcn.clk);
2326 	else
2327 		TRACE_DCE_CLOCK_STATE(&context->bw_ctx.bw.dce);
2328 
2329 	context->stream_mask = get_stream_mask(dc, context);
2330 
2331 	if (context->stream_mask != dc->current_state->stream_mask)
2332 		dc_dmub_srv_notify_stream_mask(dc->ctx->dmub_srv, context->stream_mask);
2333 
2334 	for (i = 0; i < context->stream_count; i++)
2335 		context->streams[i]->mode_changed = false;
2336 
2337 	/* Clear update flags that were set earlier to avoid redundant programming */
2338 	for (i = 0; i < context->stream_count; i++) {
2339 		context->streams[i]->update_flags.raw = 0x0;
2340 	}
2341 
2342 	old_state = dc->current_state;
2343 	dc->current_state = context;
2344 
2345 	dc_state_release(old_state);
2346 
2347 	dc_state_retain(dc->current_state);
2348 
2349 	return result;
2350 }
2351 
2352 static bool commit_minimal_transition_state(struct dc *dc,
2353 		struct dc_state *transition_base_context);
2354 
2355 /**
2356  * dc_commit_streams - Commit current stream state
2357  *
2358  * @dc: DC object with the commit state to be configured in the hardware
2359  * @params: Parameters for the commit, including the streams to be committed
2360  *
2361  * Function responsible for commit streams change to the hardware.
2362  *
2363  * Return:
2364  * Return DC_OK if everything work as expected, otherwise, return a dc_status
2365  * code.
2366  */
2367 enum dc_status dc_commit_streams(struct dc *dc, struct dc_commit_streams_params *params)
2368 {
2369 	int i, j;
2370 	struct dc_state *context;
2371 	enum dc_status res = DC_OK;
2372 	struct dc_validation_set set[MAX_STREAMS] = {0};
2373 	struct pipe_ctx *pipe;
2374 	bool handle_exit_odm2to1 = false;
2375 
2376 	if (!params)
2377 		return DC_ERROR_UNEXPECTED;
2378 
2379 	if (dc->ctx->dce_environment == DCE_ENV_VIRTUAL_HW)
2380 		return res;
2381 
2382 	if (!streams_changed(dc, params->streams, params->stream_count) &&
2383 			dc->current_state->power_source == params->power_source)
2384 		return res;
2385 
2386 	dc_exit_ips_for_hw_access(dc);
2387 
2388 	DC_LOG_DC("%s: %d streams\n", __func__, params->stream_count);
2389 
2390 	for (i = 0; i < params->stream_count; i++) {
2391 		struct dc_stream_state *stream = params->streams[i];
2392 		struct dc_stream_status *status = dc_stream_get_status(stream);
2393 		struct dc_sink *sink = stream->sink;
2394 
2395 		/* revalidate streams */
2396 		if (!dc_is_virtual_signal(sink->sink_signal)) {
2397 			res = dc_validate_stream(dc, stream);
2398 			if (res != DC_OK)
2399 				return res;
2400 		}
2401 
2402 
2403 		dc_stream_log(dc, stream);
2404 
2405 		set[i].stream = stream;
2406 
2407 		if (status) {
2408 			set[i].plane_count = status->plane_count;
2409 			for (j = 0; j < status->plane_count; j++)
2410 				set[i].plane_states[j] = status->plane_states[j];
2411 		}
2412 	}
2413 
2414 	/* ODM Combine 2:1 power optimization is only applied for single stream
2415 	 * scenario, it uses extra pipes than needed to reduce power consumption
2416 	 * We need to switch off this feature to make room for new streams.
2417 	 */
2418 	if (params->stream_count > dc->current_state->stream_count &&
2419 			dc->current_state->stream_count == 1) {
2420 		for (i = 0; i < dc->res_pool->pipe_count; i++) {
2421 			pipe = &dc->current_state->res_ctx.pipe_ctx[i];
2422 			if (pipe->next_odm_pipe)
2423 				handle_exit_odm2to1 = true;
2424 		}
2425 	}
2426 
2427 	if (handle_exit_odm2to1)
2428 		res = commit_minimal_transition_state(dc, dc->current_state);
2429 
2430 	context = dc_state_create_current_copy(dc);
2431 	if (!context)
2432 		goto context_alloc_fail;
2433 
2434 	context->power_source = params->power_source;
2435 
2436 	res = dc_validate_with_context(dc, set, params->stream_count, context, DC_VALIDATE_MODE_AND_PROGRAMMING);
2437 
2438 	/*
2439 	 * Only update link encoder to stream assignment after bandwidth validation passed.
2440 	 */
2441 	if (res == DC_OK && dc->res_pool->funcs->link_encs_assign && !dc->config.unify_link_enc_assignment)
2442 		dc->res_pool->funcs->link_encs_assign(
2443 			dc, context, context->streams, context->stream_count);
2444 
2445 	if (res != DC_OK) {
2446 		BREAK_TO_DEBUGGER();
2447 		goto fail;
2448 	}
2449 
2450 	/*
2451 	 * If not already seamless, make transition seamless by inserting intermediate minimal transition
2452 	 */
2453 	if (dc->hwss.is_pipe_topology_transition_seamless &&
2454 			!dc->hwss.is_pipe_topology_transition_seamless(dc, dc->current_state, context)) {
2455 		res = commit_minimal_transition_state(dc, context);
2456 		if (res != DC_OK) {
2457 			BREAK_TO_DEBUGGER();
2458 			goto fail;
2459 		}
2460 	}
2461 
2462 	res = dc_commit_state_no_check(dc, context);
2463 
2464 	for (i = 0; i < params->stream_count; i++) {
2465 		for (j = 0; j < context->stream_count; j++) {
2466 			if (params->streams[i]->stream_id == context->streams[j]->stream_id)
2467 				params->streams[i]->out.otg_offset = context->stream_status[j].primary_otg_inst;
2468 
2469 			if (dc_is_embedded_signal(params->streams[i]->signal)) {
2470 				struct dc_stream_status *status = dc_state_get_stream_status(context, params->streams[i]);
2471 
2472 				if (!status)
2473 					continue;
2474 
2475 				if (dc->hwss.is_abm_supported)
2476 					status->is_abm_supported = dc->hwss.is_abm_supported(dc, context, params->streams[i]);
2477 				else
2478 					status->is_abm_supported = true;
2479 			}
2480 		}
2481 	}
2482 
2483 fail:
2484 	dc_state_release(context);
2485 
2486 context_alloc_fail:
2487 
2488 	DC_LOG_DC("%s Finished.\n", __func__);
2489 
2490 	return res;
2491 }
2492 
2493 bool dc_acquire_release_mpc_3dlut(
2494 		struct dc *dc, bool acquire,
2495 		struct dc_stream_state *stream,
2496 		struct dc_3dlut **lut,
2497 		struct dc_transfer_func **shaper)
2498 {
2499 	int pipe_idx;
2500 	bool ret = false;
2501 	bool found_pipe_idx = false;
2502 	const struct resource_pool *pool = dc->res_pool;
2503 	struct resource_context *res_ctx = &dc->current_state->res_ctx;
2504 	int mpcc_id = 0;
2505 
2506 	if (pool && res_ctx) {
2507 		if (acquire) {
2508 			/*find pipe idx for the given stream*/
2509 			for (pipe_idx = 0; pipe_idx < pool->pipe_count; pipe_idx++) {
2510 				if (res_ctx->pipe_ctx[pipe_idx].stream == stream) {
2511 					found_pipe_idx = true;
2512 					mpcc_id = res_ctx->pipe_ctx[pipe_idx].plane_res.hubp->inst;
2513 					break;
2514 				}
2515 			}
2516 		} else
2517 			found_pipe_idx = true;/*for release pipe_idx is not required*/
2518 
2519 		if (found_pipe_idx) {
2520 			if (acquire && pool->funcs->acquire_post_bldn_3dlut)
2521 				ret = pool->funcs->acquire_post_bldn_3dlut(res_ctx, pool, mpcc_id, lut, shaper);
2522 			else if (!acquire && pool->funcs->release_post_bldn_3dlut)
2523 				ret = pool->funcs->release_post_bldn_3dlut(res_ctx, pool, lut, shaper);
2524 		}
2525 	}
2526 	return ret;
2527 }
2528 
2529 static bool is_flip_pending_in_pipes(struct dc *dc, struct dc_state *context)
2530 {
2531 	int i;
2532 	struct pipe_ctx *pipe;
2533 
2534 	for (i = 0; i < MAX_PIPES; i++) {
2535 		pipe = &context->res_ctx.pipe_ctx[i];
2536 
2537 		// Don't check flip pending on phantom pipes
2538 		if (!pipe->plane_state || (dc_state_get_pipe_subvp_type(context, pipe) == SUBVP_PHANTOM))
2539 			continue;
2540 
2541 		/* Must set to false to start with, due to OR in update function */
2542 		pipe->plane_state->status.is_flip_pending = false;
2543 		dc->hwss.update_pending_status(pipe);
2544 		if (pipe->plane_state->status.is_flip_pending)
2545 			return true;
2546 	}
2547 	return false;
2548 }
2549 
2550 /* Perform updates here which need to be deferred until next vupdate
2551  *
2552  * i.e. blnd lut, 3dlut, and shaper lut bypass regs are double buffered
2553  * but forcing lut memory to shutdown state is immediate. This causes
2554  * single frame corruption as lut gets disabled mid-frame unless shutdown
2555  * is deferred until after entering bypass.
2556  */
2557 static void process_deferred_updates(struct dc *dc)
2558 {
2559 	int i = 0;
2560 
2561 	if (dc->debug.enable_mem_low_power.bits.cm) {
2562 		ASSERT(dc->dcn_ip->max_num_dpp);
2563 		for (i = 0; i < dc->dcn_ip->max_num_dpp; i++)
2564 			if (dc->res_pool->dpps[i]->funcs->dpp_deferred_update)
2565 				dc->res_pool->dpps[i]->funcs->dpp_deferred_update(dc->res_pool->dpps[i]);
2566 	}
2567 }
2568 
2569 void dc_post_update_surfaces_to_stream(struct dc *dc)
2570 {
2571 	int i;
2572 	struct dc_state *context = dc->current_state;
2573 
2574 	if ((!dc->optimized_required) || get_seamless_boot_stream_count(context) > 0)
2575 		return;
2576 
2577 	post_surface_trace(dc);
2578 
2579 	/*
2580 	 * Only relevant for DCN behavior where we can guarantee the optimization
2581 	 * is safe to apply - retain the legacy behavior for DCE.
2582 	 */
2583 
2584 	if (dc->ctx->dce_version < DCE_VERSION_MAX)
2585 		TRACE_DCE_CLOCK_STATE(&context->bw_ctx.bw.dce);
2586 	else {
2587 		TRACE_DCN_CLOCK_STATE(&context->bw_ctx.bw.dcn.clk);
2588 
2589 		if (is_flip_pending_in_pipes(dc, context))
2590 			return;
2591 
2592 		for (i = 0; i < dc->res_pool->pipe_count; i++)
2593 			if (context->res_ctx.pipe_ctx[i].stream == NULL ||
2594 					context->res_ctx.pipe_ctx[i].plane_state == NULL) {
2595 				context->res_ctx.pipe_ctx[i].pipe_idx = i;
2596 				dc->hwss.disable_plane(dc, context, &context->res_ctx.pipe_ctx[i]);
2597 			}
2598 
2599 		process_deferred_updates(dc);
2600 
2601 		dc->hwss.optimize_bandwidth(dc, context);
2602 
2603 		if (dc->hwss.update_dsc_pg)
2604 			dc->hwss.update_dsc_pg(dc, context, true);
2605 	}
2606 
2607 	dc->optimized_required = false;
2608 }
2609 
2610 bool dc_set_generic_gpio_for_stereo(bool enable,
2611 		struct gpio_service *gpio_service)
2612 {
2613 	enum gpio_result gpio_result = GPIO_RESULT_NON_SPECIFIC_ERROR;
2614 	struct gpio_pin_info pin_info;
2615 	struct gpio *generic;
2616 	struct gpio_generic_mux_config *config = kzalloc_obj(struct gpio_generic_mux_config);
2617 
2618 	if (!config)
2619 		return false;
2620 	pin_info = dal_gpio_get_generic_pin_info(gpio_service, GPIO_ID_GENERIC, 0);
2621 
2622 	if (pin_info.mask == 0xFFFFFFFF || pin_info.offset == 0xFFFFFFFF) {
2623 		kfree(config);
2624 		return false;
2625 	} else {
2626 		generic = dal_gpio_service_create_generic_mux(
2627 			gpio_service,
2628 			pin_info.offset,
2629 			pin_info.mask);
2630 	}
2631 
2632 	if (!generic) {
2633 		kfree(config);
2634 		return false;
2635 	}
2636 
2637 	gpio_result = dal_gpio_open(generic, GPIO_MODE_OUTPUT);
2638 
2639 	config->enable_output_from_mux = enable;
2640 	config->mux_select = GPIO_SIGNAL_SOURCE_PASS_THROUGH_STEREO_SYNC;
2641 
2642 	if (gpio_result == GPIO_RESULT_OK)
2643 		gpio_result = dal_mux_setup_config(generic, config);
2644 
2645 	if (gpio_result == GPIO_RESULT_OK) {
2646 		dal_gpio_close(generic);
2647 		dal_gpio_destroy_generic_mux(&generic);
2648 		kfree(config);
2649 		return true;
2650 	} else {
2651 		dal_gpio_close(generic);
2652 		dal_gpio_destroy_generic_mux(&generic);
2653 		kfree(config);
2654 		return false;
2655 	}
2656 }
2657 
2658 static bool is_surface_in_context(
2659 		const struct dc_state *context,
2660 		const struct dc_plane_state *plane_state)
2661 {
2662 	int j;
2663 
2664 	for (j = 0; j < MAX_PIPES; j++) {
2665 		const struct pipe_ctx *pipe_ctx = &context->res_ctx.pipe_ctx[j];
2666 
2667 		if (plane_state == pipe_ctx->plane_state) {
2668 			return true;
2669 		}
2670 	}
2671 
2672 	return false;
2673 }
2674 
2675 static struct surface_update_descriptor get_plane_info_update_type(const struct dc_surface_update *u)
2676 {
2677 	union surface_update_flags *update_flags = &u->surface->update_flags;
2678 	struct surface_update_descriptor update_type = { UPDATE_TYPE_FAST, LOCK_DESCRIPTOR_NONE };
2679 
2680 	if (!u->plane_info)
2681 		return update_type;
2682 
2683 	// `plane_info` present means at least `STREAM` lock is required
2684 	elevate_update_type(&update_type, UPDATE_TYPE_FAST, LOCK_DESCRIPTOR_STREAM);
2685 
2686 	if (u->plane_info->color_space != u->surface->color_space) {
2687 		update_flags->bits.color_space_change = 1;
2688 		elevate_update_type(&update_type, UPDATE_TYPE_MED, LOCK_DESCRIPTOR_STREAM);
2689 	}
2690 
2691 	if (u->plane_info->horizontal_mirror != u->surface->horizontal_mirror) {
2692 		update_flags->bits.horizontal_mirror_change = 1;
2693 		elevate_update_type(&update_type, UPDATE_TYPE_MED, LOCK_DESCRIPTOR_STREAM);
2694 	}
2695 
2696 	if (u->plane_info->rotation != u->surface->rotation) {
2697 		update_flags->bits.rotation_change = 1;
2698 		elevate_update_type(&update_type, UPDATE_TYPE_FULL, LOCK_DESCRIPTOR_GLOBAL);
2699 	}
2700 
2701 	if (u->plane_info->format != u->surface->format) {
2702 		update_flags->bits.pixel_format_change = 1;
2703 		elevate_update_type(&update_type, UPDATE_TYPE_FULL, LOCK_DESCRIPTOR_GLOBAL);
2704 	}
2705 
2706 	if (u->plane_info->stereo_format != u->surface->stereo_format) {
2707 		update_flags->bits.stereo_format_change = 1;
2708 		elevate_update_type(&update_type, UPDATE_TYPE_FULL, LOCK_DESCRIPTOR_GLOBAL);
2709 	}
2710 
2711 	if (u->plane_info->per_pixel_alpha != u->surface->per_pixel_alpha) {
2712 		update_flags->bits.per_pixel_alpha_change = 1;
2713 		elevate_update_type(&update_type, UPDATE_TYPE_MED, LOCK_DESCRIPTOR_STREAM);
2714 	}
2715 
2716 	if (u->plane_info->global_alpha_value != u->surface->global_alpha_value) {
2717 		update_flags->bits.global_alpha_change = 1;
2718 		elevate_update_type(&update_type, UPDATE_TYPE_MED, LOCK_DESCRIPTOR_STREAM);
2719 	}
2720 
2721 	if (u->plane_info->dcc.enable != u->surface->dcc.enable
2722 			|| u->plane_info->dcc.dcc_ind_blk != u->surface->dcc.dcc_ind_blk
2723 			|| u->plane_info->dcc.meta_pitch != u->surface->dcc.meta_pitch) {
2724 		/* During DCC on/off, stutter period is calculated before
2725 		 * DCC has fully transitioned. This results in incorrect
2726 		 * stutter period calculation. Triggering a full update will
2727 		 * recalculate stutter period.
2728 		 */
2729 		update_flags->bits.dcc_change = 1;
2730 		elevate_update_type(&update_type, UPDATE_TYPE_FULL, LOCK_DESCRIPTOR_GLOBAL);
2731 	}
2732 
2733 	if (resource_pixel_format_to_bpp(u->plane_info->format) !=
2734 			resource_pixel_format_to_bpp(u->surface->format)) {
2735 		/* different bytes per element will require full bandwidth
2736 		 * and DML calculation
2737 		 */
2738 		update_flags->bits.bpp_change = 1;
2739 		elevate_update_type(&update_type, UPDATE_TYPE_FULL, LOCK_DESCRIPTOR_GLOBAL);
2740 	}
2741 
2742 	if (u->plane_info->plane_size.surface_pitch != u->surface->plane_size.surface_pitch
2743 			|| u->plane_info->plane_size.chroma_pitch != u->surface->plane_size.chroma_pitch) {
2744 		update_flags->bits.plane_size_change = 1;
2745 		elevate_update_type(&update_type, UPDATE_TYPE_MED, LOCK_DESCRIPTOR_STREAM);
2746 	}
2747 
2748 	const struct dc_tiling_info *tiling = &u->plane_info->tiling_info;
2749 
2750 	if (memcmp(tiling, &u->surface->tiling_info, sizeof(*tiling)) != 0) {
2751 		update_flags->bits.swizzle_change = 1;
2752 		elevate_update_type(&update_type, UPDATE_TYPE_MED, LOCK_DESCRIPTOR_STREAM);
2753 
2754 		switch (tiling->gfxversion) {
2755 		case DcGfxVersion9:
2756 		case DcGfxVersion10:
2757 		case DcGfxVersion11:
2758 			if (tiling->gfx9.swizzle != DC_SW_LINEAR) {
2759 				update_flags->bits.bandwidth_change = 1;
2760 				elevate_update_type(&update_type, UPDATE_TYPE_FULL, LOCK_DESCRIPTOR_GLOBAL);
2761 			}
2762 			break;
2763 		case DcGfxAddr3:
2764 			if (tiling->gfx_addr3.swizzle != DC_ADDR3_SW_LINEAR) {
2765 				update_flags->bits.bandwidth_change = 1;
2766 				elevate_update_type(&update_type, UPDATE_TYPE_FULL, LOCK_DESCRIPTOR_GLOBAL);
2767 			}
2768 			break;
2769 		case DcGfxVersion7:
2770 		case DcGfxVersion8:
2771 		case DcGfxVersionUnknown:
2772 		default:
2773 			break;
2774 		}
2775 	}
2776 
2777 	/* This should be UPDATE_TYPE_FAST if nothing has changed. */
2778 	return update_type;
2779 }
2780 
2781 static struct surface_update_descriptor get_scaling_info_update_type(
2782 		const struct dc_check_config *check_config,
2783 		const struct dc_surface_update *u)
2784 {
2785 	union surface_update_flags *update_flags = &u->surface->update_flags;
2786 	struct surface_update_descriptor update_type = { UPDATE_TYPE_FAST, LOCK_DESCRIPTOR_NONE };
2787 
2788 	if (!u->scaling_info)
2789 		return update_type;
2790 
2791 	// `scaling_info` present means at least `STREAM` lock is required
2792 	elevate_update_type(&update_type, UPDATE_TYPE_FAST, LOCK_DESCRIPTOR_STREAM);
2793 
2794 	if (u->scaling_info->src_rect.width != u->surface->src_rect.width
2795 			|| u->scaling_info->src_rect.height != u->surface->src_rect.height
2796 			|| u->scaling_info->dst_rect.width != u->surface->dst_rect.width
2797 			|| u->scaling_info->dst_rect.height != u->surface->dst_rect.height
2798 			|| u->scaling_info->clip_rect.width != u->surface->clip_rect.width
2799 			|| u->scaling_info->clip_rect.height != u->surface->clip_rect.height
2800 			|| u->scaling_info->scaling_quality.integer_scaling !=
2801 					u->surface->scaling_quality.integer_scaling) {
2802 		update_flags->bits.scaling_change = 1;
2803 		elevate_update_type(&update_type, UPDATE_TYPE_FULL, LOCK_DESCRIPTOR_GLOBAL);
2804 
2805 		if (u->scaling_info->src_rect.width > u->surface->src_rect.width
2806 				|| u->scaling_info->src_rect.height > u->surface->src_rect.height)
2807 			/* Making src rect bigger requires a bandwidth change */
2808 			update_flags->bits.clock_change = 1;
2809 
2810 		if ((u->scaling_info->dst_rect.width < u->surface->dst_rect.width
2811 			|| u->scaling_info->dst_rect.height < u->surface->dst_rect.height)
2812 				&& (u->scaling_info->dst_rect.width < u->surface->src_rect.width
2813 					|| u->scaling_info->dst_rect.height < u->surface->src_rect.height))
2814 			/* Making dst rect smaller requires a bandwidth change */
2815 			update_flags->bits.bandwidth_change = 1;
2816 
2817 		if (u->scaling_info->src_rect.width > check_config->max_optimizable_video_width &&
2818 			(u->scaling_info->clip_rect.width > u->surface->clip_rect.width ||
2819 			 u->scaling_info->clip_rect.height > u->surface->clip_rect.height))
2820 			 /* Changing clip size of a large surface may result in MPC slice count change */
2821 			update_flags->bits.bandwidth_change = 1;
2822 	}
2823 
2824 	if (u->scaling_info->src_rect.x != u->surface->src_rect.x
2825 			|| u->scaling_info->src_rect.y != u->surface->src_rect.y
2826 			|| u->scaling_info->clip_rect.x != u->surface->clip_rect.x
2827 			|| u->scaling_info->clip_rect.y != u->surface->clip_rect.y
2828 			|| u->scaling_info->dst_rect.x != u->surface->dst_rect.x
2829 			|| u->scaling_info->dst_rect.y != u->surface->dst_rect.y) {
2830 		elevate_update_type(&update_type, UPDATE_TYPE_MED, LOCK_DESCRIPTOR_STREAM);
2831 		update_flags->bits.position_change = 1;
2832 	}
2833 
2834 	return update_type;
2835 }
2836 
2837 static struct surface_update_descriptor det_surface_update(
2838 		const struct dc_check_config *check_config,
2839 		struct dc_surface_update *u)
2840 {
2841 	struct surface_update_descriptor overall_type = { UPDATE_TYPE_FAST, LOCK_DESCRIPTOR_NONE };
2842 	union surface_update_flags *update_flags = &u->surface->update_flags;
2843 
2844 	if (u->surface->force_full_update) {
2845 		update_flags->raw = 0xFFFFFFFF;
2846 		elevate_update_type(&overall_type, UPDATE_TYPE_FULL, LOCK_DESCRIPTOR_GLOBAL);
2847 		return overall_type;
2848 	}
2849 
2850 	update_flags->raw = 0; // Reset all flags
2851 
2852 	struct surface_update_descriptor inner_type = get_plane_info_update_type(u);
2853 
2854 	elevate_update_type(&overall_type, inner_type.update_type, inner_type.lock_descriptor);
2855 
2856 	inner_type = get_scaling_info_update_type(check_config, u);
2857 	elevate_update_type(&overall_type, inner_type.update_type, inner_type.lock_descriptor);
2858 
2859 	if (u->flip_addr) {
2860 		update_flags->bits.addr_update = 1;
2861 		elevate_update_type(&overall_type, UPDATE_TYPE_FAST, LOCK_DESCRIPTOR_STREAM);
2862 
2863 		if (u->flip_addr->address.tmz_surface != u->surface->address.tmz_surface) {
2864 			update_flags->bits.tmz_changed = 1;
2865 			elevate_update_type(&overall_type, UPDATE_TYPE_FULL, LOCK_DESCRIPTOR_GLOBAL);
2866 		}
2867 	}
2868 	if (u->in_transfer_func) {
2869 		update_flags->bits.in_transfer_func_change = 1;
2870 		elevate_update_type(&overall_type, UPDATE_TYPE_MED, LOCK_DESCRIPTOR_STREAM);
2871 	}
2872 
2873 	if (u->input_csc_color_matrix) {
2874 		update_flags->bits.input_csc_change = 1;
2875 		elevate_update_type(&overall_type, UPDATE_TYPE_FAST, LOCK_DESCRIPTOR_STREAM);
2876 	}
2877 
2878 	if (u->coeff_reduction_factor) {
2879 		update_flags->bits.coeff_reduction_change = 1;
2880 		elevate_update_type(&overall_type, UPDATE_TYPE_FAST, LOCK_DESCRIPTOR_STREAM);
2881 	}
2882 
2883 	if (u->gamut_remap_matrix) {
2884 		update_flags->bits.gamut_remap_change = 1;
2885 		elevate_update_type(&overall_type, UPDATE_TYPE_FAST, LOCK_DESCRIPTOR_STREAM);
2886 	}
2887 
2888 	if (u->blend_tf || (u->gamma && dce_use_lut(u->plane_info ? u->plane_info->format : u->surface->format))) {
2889 		update_flags->bits.gamma_change = 1;
2890 		elevate_update_type(&overall_type, UPDATE_TYPE_FAST, LOCK_DESCRIPTOR_STREAM);
2891 	}
2892 
2893 	if (u->lut3d_func || u->func_shaper) {
2894 		update_flags->bits.lut_3d = 1;
2895 		elevate_update_type(&overall_type, UPDATE_TYPE_FAST, LOCK_DESCRIPTOR_STREAM);
2896 	}
2897 
2898 	if (u->hdr_mult.value)
2899 		if (u->hdr_mult.value != u->surface->hdr_mult.value) {
2900 			// TODO: Should be fast?
2901 			update_flags->bits.hdr_mult = 1;
2902 			elevate_update_type(&overall_type, UPDATE_TYPE_MED, LOCK_DESCRIPTOR_STREAM);
2903 		}
2904 
2905 	if (u->sdr_white_level_nits)
2906 		if (u->sdr_white_level_nits != u->surface->sdr_white_level_nits) {
2907 			// TODO: Should be fast?
2908 			update_flags->bits.sdr_white_level_nits = 1;
2909 			elevate_update_type(&overall_type, UPDATE_TYPE_FULL, LOCK_DESCRIPTOR_GLOBAL);
2910 		}
2911 
2912 	if (u->cm2_params) {
2913 		if (u->cm2_params->component_settings.shaper_3dlut_setting != u->surface->mcm_shaper_3dlut_setting
2914 				|| u->cm2_params->component_settings.lut1d_enable != u->surface->mcm_lut1d_enable
2915 				|| u->cm2_params->cm2_luts.lut3d_data.lut3d_src != u->surface->mcm_luts.lut3d_data.lut3d_src) {
2916 			update_flags->bits.mcm_transfer_function_enable_change = 1;
2917 			elevate_update_type(&overall_type, UPDATE_TYPE_FULL, LOCK_DESCRIPTOR_GLOBAL);
2918 		}
2919 	}
2920 
2921 	if (update_flags->bits.lut_3d &&
2922 			u->surface->mcm_luts.lut3d_data.lut3d_src != DC_CM2_TRANSFER_FUNC_SOURCE_VIDMEM) {
2923 		elevate_update_type(&overall_type, UPDATE_TYPE_FULL, LOCK_DESCRIPTOR_GLOBAL);
2924 	}
2925 
2926 	if (check_config->enable_legacy_fast_update &&
2927 			(update_flags->bits.gamma_change ||
2928 			update_flags->bits.gamut_remap_change ||
2929 			update_flags->bits.input_csc_change ||
2930 			update_flags->bits.coeff_reduction_change)) {
2931 		elevate_update_type(&overall_type, UPDATE_TYPE_FULL, LOCK_DESCRIPTOR_GLOBAL);
2932 	}
2933 	return overall_type;
2934 }
2935 
2936 /* May need to flip the desktop plane in cases where MPO plane receives a flip but desktop plane doesn't
2937  * while both planes are flip_immediate
2938  */
2939 static void force_immediate_gsl_plane_flip(struct dc *dc, struct dc_surface_update *updates, int surface_count)
2940 {
2941 	bool has_flip_immediate_plane = false;
2942 	int i;
2943 
2944 	for (i = 0; i < surface_count; i++) {
2945 		if (updates[i].surface->flip_immediate) {
2946 			has_flip_immediate_plane = true;
2947 			break;
2948 		}
2949 	}
2950 
2951 	if (has_flip_immediate_plane && surface_count > 1) {
2952 		for (i = 0; i < surface_count; i++) {
2953 			if (updates[i].surface->flip_immediate)
2954 				updates[i].surface->update_flags.bits.addr_update = 1;
2955 		}
2956 	}
2957 }
2958 
2959 static struct surface_update_descriptor check_update_surfaces_for_stream(
2960 		const struct dc_check_config *check_config,
2961 		struct dc_surface_update *updates,
2962 		int surface_count,
2963 		struct dc_stream_update *stream_update)
2964 {
2965 	struct surface_update_descriptor overall_type = { UPDATE_TYPE_FAST, LOCK_DESCRIPTOR_NONE };
2966 
2967 	/* When countdown finishes, promote this flip to full to trigger deferred final transition */
2968 	if (check_config->deferred_transition_state && !check_config->transition_countdown_to_steady_state) {
2969 		elevate_update_type(&overall_type, UPDATE_TYPE_FULL, LOCK_DESCRIPTOR_GLOBAL);
2970 	}
2971 
2972 	if (stream_update && stream_update->pending_test_pattern) {
2973 		elevate_update_type(&overall_type, UPDATE_TYPE_FULL, LOCK_DESCRIPTOR_GLOBAL);
2974 	}
2975 
2976 	if (stream_update && stream_update->hw_cursor_req) {
2977 		elevate_update_type(&overall_type, UPDATE_TYPE_FULL, LOCK_DESCRIPTOR_GLOBAL);
2978 	}
2979 
2980 	/* some stream updates require passive update */
2981 	if (stream_update) {
2982 		elevate_update_type(&overall_type, UPDATE_TYPE_FAST, LOCK_DESCRIPTOR_STREAM);
2983 
2984 		union stream_update_flags *su_flags = &stream_update->stream->update_flags;
2985 
2986 		if ((stream_update->src.height != 0 && stream_update->src.width != 0) ||
2987 			(stream_update->dst.height != 0 && stream_update->dst.width != 0) ||
2988 			stream_update->integer_scaling_update)
2989 			su_flags->bits.scaling = 1;
2990 
2991 		if (check_config->enable_legacy_fast_update && stream_update->out_transfer_func)
2992 			su_flags->bits.out_tf = 1;
2993 
2994 		if (stream_update->abm_level)
2995 			su_flags->bits.abm_level = 1;
2996 
2997 		if (stream_update->dpms_off) {
2998 			su_flags->bits.dpms_off = 1;
2999 			elevate_update_type(&overall_type, UPDATE_TYPE_FULL, LOCK_DESCRIPTOR_GLOBAL | LOCK_DESCRIPTOR_LINK);
3000 		}
3001 
3002 		if (stream_update->gamut_remap)
3003 			su_flags->bits.gamut_remap = 1;
3004 
3005 		if (stream_update->wb_update)
3006 			su_flags->bits.wb_update = 1;
3007 
3008 		if (stream_update->dsc_config)
3009 			su_flags->bits.dsc_changed = 1;
3010 
3011 		if (stream_update->mst_bw_update)
3012 			su_flags->bits.mst_bw = 1;
3013 
3014 		if (stream_update->stream->freesync_on_desktop &&
3015 			(stream_update->vrr_infopacket || stream_update->allow_freesync ||
3016 				stream_update->vrr_active_variable || stream_update->vrr_active_fixed))
3017 			su_flags->bits.fams_changed = 1;
3018 
3019 		if (stream_update->scaler_sharpener_update)
3020 			su_flags->bits.scaler_sharpener = 1;
3021 
3022 		if (stream_update->sharpening_required)
3023 			su_flags->bits.sharpening_required = 1;
3024 
3025 		if (stream_update->output_color_space)
3026 			su_flags->bits.out_csc = 1;
3027 
3028 		// TODO: Make each elevation explicit, as to not override fast stream in crct_timing_adjust
3029 		if (su_flags->raw)
3030 			elevate_update_type(&overall_type, UPDATE_TYPE_FULL, LOCK_DESCRIPTOR_GLOBAL);
3031 
3032 		// Non-global cases
3033 		if (stream_update->output_csc_transform) {
3034 			su_flags->bits.out_csc = 1;
3035 			elevate_update_type(&overall_type, UPDATE_TYPE_FAST, LOCK_DESCRIPTOR_STREAM);
3036 		}
3037 
3038 		if (!check_config->enable_legacy_fast_update && stream_update->out_transfer_func) {
3039 			su_flags->bits.out_tf = 1;
3040 			elevate_update_type(&overall_type, UPDATE_TYPE_FAST, LOCK_DESCRIPTOR_STREAM);
3041 		}
3042 	}
3043 
3044 	for (int i = 0 ; i < surface_count; i++) {
3045 		struct surface_update_descriptor inner_type =
3046 				det_surface_update(check_config, &updates[i]);
3047 
3048 		elevate_update_type(&overall_type, inner_type.update_type, inner_type.lock_descriptor);
3049 	}
3050 
3051 	return overall_type;
3052 }
3053 
3054 /*
3055  * dc_check_update_surfaces_for_stream() - Determine update type (fast, med, or full)
3056  *
3057  * See :c:type:`enum surface_update_type <surface_update_type>` for explanation of update types
3058  */
3059 struct surface_update_descriptor dc_check_update_surfaces_for_stream(
3060 		const struct dc_check_config *check_config,
3061 		struct dc_surface_update *updates,
3062 		int surface_count,
3063 		struct dc_stream_update *stream_update)
3064 {
3065 	if (stream_update)
3066 		stream_update->stream->update_flags.raw = 0;
3067 	for (size_t i = 0; i < surface_count; i++)
3068 		updates[i].surface->update_flags.raw = 0;
3069 
3070 	return check_update_surfaces_for_stream(check_config, updates, surface_count, stream_update);
3071 }
3072 
3073 static struct dc_stream_status *stream_get_status(
3074 	struct dc_state *ctx,
3075 	struct dc_stream_state *stream)
3076 {
3077 	uint8_t i;
3078 
3079 	for (i = 0; i < ctx->stream_count; i++) {
3080 		if (stream == ctx->streams[i]) {
3081 			return &ctx->stream_status[i];
3082 		}
3083 	}
3084 
3085 	return NULL;
3086 }
3087 
3088 static const enum surface_update_type update_surface_trace_level = UPDATE_TYPE_FULL;
3089 
3090 static void copy_surface_update_to_plane(
3091 		struct dc_plane_state *surface,
3092 		struct dc_surface_update *srf_update)
3093 {
3094 	if (srf_update->flip_addr) {
3095 		surface->address = srf_update->flip_addr->address;
3096 		surface->flip_immediate =
3097 			srf_update->flip_addr->flip_immediate;
3098 		surface->time.time_elapsed_in_us[surface->time.index] =
3099 			srf_update->flip_addr->flip_timestamp_in_us -
3100 				surface->time.prev_update_time_in_us;
3101 		surface->time.prev_update_time_in_us =
3102 			srf_update->flip_addr->flip_timestamp_in_us;
3103 		surface->time.index++;
3104 		if (surface->time.index >= DC_PLANE_UPDATE_TIMES_MAX)
3105 			surface->time.index = 0;
3106 
3107 		surface->triplebuffer_flips = srf_update->flip_addr->triplebuffer_flips;
3108 	}
3109 
3110 	if (srf_update->scaling_info) {
3111 		surface->scaling_quality =
3112 				srf_update->scaling_info->scaling_quality;
3113 		surface->dst_rect =
3114 				srf_update->scaling_info->dst_rect;
3115 		surface->src_rect =
3116 				srf_update->scaling_info->src_rect;
3117 		surface->clip_rect =
3118 				srf_update->scaling_info->clip_rect;
3119 	}
3120 
3121 	if (srf_update->plane_info) {
3122 		surface->color_space =
3123 				srf_update->plane_info->color_space;
3124 		surface->format =
3125 				srf_update->plane_info->format;
3126 		surface->plane_size =
3127 				srf_update->plane_info->plane_size;
3128 		surface->rotation =
3129 				srf_update->plane_info->rotation;
3130 		surface->horizontal_mirror =
3131 				srf_update->plane_info->horizontal_mirror;
3132 		surface->stereo_format =
3133 				srf_update->plane_info->stereo_format;
3134 		surface->tiling_info =
3135 				srf_update->plane_info->tiling_info;
3136 		surface->visible =
3137 				srf_update->plane_info->visible;
3138 		surface->per_pixel_alpha =
3139 				srf_update->plane_info->per_pixel_alpha;
3140 		surface->global_alpha =
3141 				srf_update->plane_info->global_alpha;
3142 		surface->global_alpha_value =
3143 				srf_update->plane_info->global_alpha_value;
3144 		surface->dcc =
3145 				srf_update->plane_info->dcc;
3146 		surface->layer_index =
3147 				srf_update->plane_info->layer_index;
3148 	}
3149 
3150 	if (srf_update->gamma) {
3151 		memcpy(&surface->gamma_correction.entries,
3152 			&srf_update->gamma->entries,
3153 			sizeof(struct dc_gamma_entries));
3154 		surface->gamma_correction.is_identity =
3155 			srf_update->gamma->is_identity;
3156 		surface->gamma_correction.num_entries =
3157 			srf_update->gamma->num_entries;
3158 		surface->gamma_correction.type =
3159 			srf_update->gamma->type;
3160 	}
3161 
3162 	if (srf_update->in_transfer_func) {
3163 		surface->in_transfer_func.sdr_ref_white_level =
3164 			srf_update->in_transfer_func->sdr_ref_white_level;
3165 		surface->in_transfer_func.tf =
3166 			srf_update->in_transfer_func->tf;
3167 		surface->in_transfer_func.type =
3168 			srf_update->in_transfer_func->type;
3169 		memcpy(&surface->in_transfer_func.tf_pts,
3170 			&srf_update->in_transfer_func->tf_pts,
3171 			sizeof(struct dc_transfer_func_distributed_points));
3172 	}
3173 
3174 	if (srf_update->cm2_params) {
3175 		surface->mcm_shaper_3dlut_setting = srf_update->cm2_params->component_settings.shaper_3dlut_setting;
3176 		surface->mcm_lut1d_enable = srf_update->cm2_params->component_settings.lut1d_enable;
3177 		surface->mcm_luts = srf_update->cm2_params->cm2_luts;
3178 	}
3179 
3180 	if (srf_update->func_shaper) {
3181 		memcpy(&surface->in_shaper_func, srf_update->func_shaper,
3182 		sizeof(surface->in_shaper_func));
3183 
3184 		if (surface->mcm_shaper_3dlut_setting >= DC_CM2_SHAPER_3DLUT_SETTING_ENABLE_SHAPER)
3185 			surface->mcm_luts.shaper = &surface->in_shaper_func;
3186 	}
3187 
3188 	if (srf_update->lut3d_func)
3189 		memcpy(&surface->lut3d_func, srf_update->lut3d_func,
3190 		sizeof(surface->lut3d_func));
3191 
3192 	if (srf_update->hdr_mult.value)
3193 		surface->hdr_mult =
3194 				srf_update->hdr_mult;
3195 
3196 	if (srf_update->sdr_white_level_nits)
3197 		surface->sdr_white_level_nits =
3198 				srf_update->sdr_white_level_nits;
3199 
3200 	if (srf_update->blend_tf) {
3201 		memcpy(&surface->blend_tf, srf_update->blend_tf,
3202 		sizeof(surface->blend_tf));
3203 
3204 		if (surface->mcm_lut1d_enable)
3205 			surface->mcm_luts.lut1d_func = &surface->blend_tf;
3206 	}
3207 
3208 	if (srf_update->cm2_params || srf_update->blend_tf)
3209 		surface->lut_bank_a = !surface->lut_bank_a;
3210 
3211 	if (srf_update->input_csc_color_matrix)
3212 		surface->input_csc_color_matrix =
3213 			*srf_update->input_csc_color_matrix;
3214 
3215 	if (srf_update->coeff_reduction_factor)
3216 		surface->coeff_reduction_factor =
3217 			*srf_update->coeff_reduction_factor;
3218 
3219 	if (srf_update->gamut_remap_matrix)
3220 		surface->gamut_remap_matrix =
3221 			*srf_update->gamut_remap_matrix;
3222 
3223 	if (srf_update->cursor_csc_color_matrix)
3224 		surface->cursor_csc_color_matrix =
3225 			*srf_update->cursor_csc_color_matrix;
3226 
3227 	if (srf_update->bias_and_scale.bias_and_scale_valid)
3228 			surface->bias_and_scale =
3229 					srf_update->bias_and_scale;
3230 }
3231 
3232 static void copy_stream_update_to_stream(struct dc *dc,
3233 					 struct dc_state *context,
3234 					 struct dc_stream_state *stream,
3235 					 struct dc_stream_update *update)
3236 {
3237 	struct dc_context *dc_ctx = dc->ctx;
3238 
3239 	if (update == NULL || stream == NULL)
3240 		return;
3241 
3242 	if (update->src.height && update->src.width)
3243 		stream->src = update->src;
3244 
3245 	if (update->dst.height && update->dst.width)
3246 		stream->dst = update->dst;
3247 
3248 	if (update->out_transfer_func) {
3249 		stream->out_transfer_func.sdr_ref_white_level =
3250 			update->out_transfer_func->sdr_ref_white_level;
3251 		stream->out_transfer_func.tf = update->out_transfer_func->tf;
3252 		stream->out_transfer_func.type =
3253 			update->out_transfer_func->type;
3254 		memcpy(&stream->out_transfer_func.tf_pts,
3255 		       &update->out_transfer_func->tf_pts,
3256 		       sizeof(struct dc_transfer_func_distributed_points));
3257 	}
3258 
3259 	if (update->hdr_static_metadata)
3260 		stream->hdr_static_metadata = *update->hdr_static_metadata;
3261 
3262 	if (update->abm_level)
3263 		stream->abm_level = *update->abm_level;
3264 
3265 	if (update->periodic_interrupt)
3266 		stream->periodic_interrupt = *update->periodic_interrupt;
3267 
3268 	if (update->gamut_remap)
3269 		stream->gamut_remap_matrix = *update->gamut_remap;
3270 
3271 	/* Note: this being updated after mode set is currently not a use case
3272 	 * however if it arises OCSC would need to be reprogrammed at the
3273 	 * minimum
3274 	 */
3275 	if (update->output_color_space)
3276 		stream->output_color_space = *update->output_color_space;
3277 
3278 	if (update->output_csc_transform)
3279 		stream->csc_color_matrix = *update->output_csc_transform;
3280 
3281 	if (update->vrr_infopacket)
3282 		stream->vrr_infopacket = *update->vrr_infopacket;
3283 
3284 	if (update->hw_cursor_req)
3285 		stream->hw_cursor_req = *update->hw_cursor_req;
3286 
3287 	if (update->allow_freesync)
3288 		stream->allow_freesync = *update->allow_freesync;
3289 
3290 	if (update->vrr_active_variable)
3291 		stream->vrr_active_variable = *update->vrr_active_variable;
3292 
3293 	if (update->vrr_active_fixed)
3294 		stream->vrr_active_fixed = *update->vrr_active_fixed;
3295 
3296 	if (update->crtc_timing_adjust) {
3297 		if (stream->adjust.v_total_min != update->crtc_timing_adjust->v_total_min ||
3298 			stream->adjust.v_total_max != update->crtc_timing_adjust->v_total_max ||
3299 			stream->adjust.timing_adjust_pending)
3300 			update->crtc_timing_adjust->timing_adjust_pending = true;
3301 		stream->adjust = *update->crtc_timing_adjust;
3302 		update->crtc_timing_adjust->timing_adjust_pending = false;
3303 	}
3304 
3305 	if (update->dpms_off)
3306 		stream->dpms_off = *update->dpms_off;
3307 
3308 	if (update->hfvsif_infopacket)
3309 		stream->hfvsif_infopacket = *update->hfvsif_infopacket;
3310 
3311 	if (update->vtem_infopacket)
3312 		stream->vtem_infopacket = *update->vtem_infopacket;
3313 
3314 	if (update->vsc_infopacket)
3315 		stream->vsc_infopacket = *update->vsc_infopacket;
3316 
3317 	if (update->vsp_infopacket)
3318 		stream->vsp_infopacket = *update->vsp_infopacket;
3319 
3320 	if (update->adaptive_sync_infopacket)
3321 		stream->adaptive_sync_infopacket = *update->adaptive_sync_infopacket;
3322 
3323 	if (update->avi_infopacket)
3324 		stream->avi_infopacket = *update->avi_infopacket;
3325 
3326 	if (update->dither_option)
3327 		stream->dither_option = *update->dither_option;
3328 
3329 	if (update->pending_test_pattern)
3330 		stream->test_pattern = *update->pending_test_pattern;
3331 	/* update current stream with writeback info */
3332 	if (update->wb_update) {
3333 		int i;
3334 
3335 		stream->num_wb_info = update->wb_update->num_wb_info;
3336 		ASSERT(stream->num_wb_info <= MAX_DWB_PIPES);
3337 		for (i = 0; i < stream->num_wb_info; i++)
3338 			stream->writeback_info[i] =
3339 				update->wb_update->writeback_info[i];
3340 	}
3341 	if (update->dsc_config) {
3342 		struct dc_dsc_config old_dsc_cfg = stream->timing.dsc_cfg;
3343 		uint32_t old_dsc_enabled = stream->timing.flags.DSC;
3344 		uint32_t enable_dsc = (update->dsc_config->num_slices_h != 0 &&
3345 				       update->dsc_config->num_slices_v != 0);
3346 
3347 		/* Use temporarry context for validating new DSC config */
3348 		struct dc_state *dsc_validate_context = dc_state_create_copy(dc->current_state);
3349 
3350 		if (dsc_validate_context) {
3351 			stream->timing.dsc_cfg = *update->dsc_config;
3352 			stream->timing.flags.DSC = enable_dsc;
3353 			if (dc->res_pool->funcs->validate_bandwidth(dc, dsc_validate_context,
3354 				DC_VALIDATE_MODE_ONLY) != DC_OK) {
3355 				stream->timing.dsc_cfg = old_dsc_cfg;
3356 				stream->timing.flags.DSC = old_dsc_enabled;
3357 				update->dsc_config = NULL;
3358 			}
3359 
3360 			dc_state_release(dsc_validate_context);
3361 		} else {
3362 			DC_ERROR("Failed to allocate new validate context for DSC change\n");
3363 			update->dsc_config = NULL;
3364 		}
3365 	}
3366 	if (update->scaler_sharpener_update)
3367 		stream->scaler_sharpener_update = *update->scaler_sharpener_update;
3368 	if (update->sharpening_required)
3369 		stream->sharpening_required = *update->sharpening_required;
3370 
3371 	if (update->drr_trigger_mode) {
3372 		stream->drr_trigger_mode = *update->drr_trigger_mode;
3373 	}
3374 }
3375 
3376 static void backup_planes_and_stream_state(
3377 		struct dc_scratch_space *scratch,
3378 		struct dc_stream_state *stream)
3379 {
3380 	int i;
3381 	struct dc_stream_status *status = dc_stream_get_status(stream);
3382 
3383 	if (!status)
3384 		return;
3385 
3386 	for (i = 0; i < status->plane_count; i++) {
3387 		dc_plane_copy_config(&scratch->plane_states[i], status->plane_states[i]);
3388 	}
3389 	scratch->stream_state = *stream;
3390 }
3391 
3392 static void restore_planes_and_stream_state(
3393 		struct dc_scratch_space *scratch,
3394 		struct dc_stream_state *stream)
3395 {
3396 	int i;
3397 	struct dc_stream_status *status = dc_stream_get_status(stream);
3398 
3399 	if (!status)
3400 		return;
3401 
3402 	for (i = 0; i < status->plane_count; i++) {
3403 		dc_plane_copy_config(status->plane_states[i], &scratch->plane_states[i]);
3404 	}
3405 
3406 	// refcount is persistent
3407 	struct kref temp_refcount = stream->refcount;
3408 	*stream = scratch->stream_state;
3409 	stream->refcount = temp_refcount;
3410 }
3411 
3412 /**
3413  * update_seamless_boot_flags() - Helper function for updating seamless boot flags
3414  *
3415  * @dc: Current DC state
3416  * @context: New DC state to be programmed
3417  * @surface_count: Number of surfaces that have an updated
3418  * @stream: Corresponding stream to be updated in the current flip
3419  *
3420  * Updating seamless boot flags do not need to be part of the commit sequence. This
3421  * helper function will update the seamless boot flags on each flip (if required)
3422  * outside of the HW commit sequence (fast or slow).
3423  *
3424  * Return: void
3425  */
3426 static void update_seamless_boot_flags(struct dc *dc,
3427 		struct dc_state *context,
3428 		int surface_count,
3429 		struct dc_stream_state *stream)
3430 {
3431 	if (get_seamless_boot_stream_count(context) > 0 && (surface_count > 0 || stream->dpms_off)) {
3432 		/* Optimize seamless boot flag keeps clocks and watermarks high until
3433 		 * first flip. After first flip, optimization is required to lower
3434 		 * bandwidth. Important to note that it is expected UEFI will
3435 		 * only light up a single display on POST, therefore we only expect
3436 		 * one stream with seamless boot flag set.
3437 		 */
3438 		if (stream->apply_seamless_boot_optimization) {
3439 			stream->apply_seamless_boot_optimization = false;
3440 
3441 			if (get_seamless_boot_stream_count(context) == 0)
3442 				dc->optimized_required = true;
3443 		}
3444 	}
3445 }
3446 
3447 static bool full_update_required_weak(
3448 		const struct dc *dc,
3449 		const struct dc_surface_update *srf_updates,
3450 		int surface_count,
3451 		const struct dc_stream_update *stream_update,
3452 		const struct dc_stream_state *stream);
3453 
3454 struct pipe_split_policy_backup {
3455 	bool dynamic_odm_policy;
3456 	bool subvp_policy;
3457 	enum pipe_split_policy mpc_policy;
3458 	char force_odm[MAX_PIPES];
3459 };
3460 
3461 static void backup_and_set_minimal_pipe_split_policy(struct dc *dc,
3462 		struct dc_state *context,
3463 		struct pipe_split_policy_backup *policy)
3464 {
3465 	int i;
3466 
3467 	if (!dc->config.is_vmin_only_asic) {
3468 		policy->mpc_policy = dc->debug.pipe_split_policy;
3469 		dc->debug.pipe_split_policy = MPC_SPLIT_AVOID;
3470 	}
3471 	policy->dynamic_odm_policy = dc->debug.enable_single_display_2to1_odm_policy;
3472 	dc->debug.enable_single_display_2to1_odm_policy = false;
3473 	policy->subvp_policy = dc->debug.force_disable_subvp;
3474 	dc->debug.force_disable_subvp = true;
3475 	for (i = 0; i < context->stream_count; i++) {
3476 		policy->force_odm[i] = context->streams[i]->debug.force_odm_combine_segments;
3477 		if (context->streams[i]->debug.allow_transition_for_forced_odm)
3478 			context->streams[i]->debug.force_odm_combine_segments = 0;
3479 	}
3480 }
3481 
3482 static void restore_minimal_pipe_split_policy(struct dc *dc,
3483 		struct dc_state *context,
3484 		struct pipe_split_policy_backup *policy)
3485 {
3486 	uint8_t i;
3487 
3488 	if (!dc->config.is_vmin_only_asic)
3489 		dc->debug.pipe_split_policy = policy->mpc_policy;
3490 	dc->debug.enable_single_display_2to1_odm_policy =
3491 			policy->dynamic_odm_policy;
3492 	dc->debug.force_disable_subvp = policy->subvp_policy;
3493 	for (i = 0; i < context->stream_count; i++)
3494 		context->streams[i]->debug.force_odm_combine_segments = policy->force_odm[i];
3495 }
3496 
3497 /**
3498  * update_planes_and_stream_state() - The function takes planes and stream
3499  * updates as inputs and determines the appropriate update type. If update type
3500  * is FULL, the function allocates a new context, populates and validates it.
3501  * Otherwise, it updates current dc context. The function will return both
3502  * new_context and new_update_type back to the caller. The function also backs
3503  * up both current and new contexts into corresponding dc state scratch memory.
3504  * TODO: The function does too many things, and even conditionally allocates dc
3505  * context memory implicitly. We should consider to break it down.
3506  *
3507  * @dc: Current DC state
3508  * @srf_updates: an array of surface updates
3509  * @surface_count: surface update count
3510  * @stream: Corresponding stream to be updated
3511  * @stream_update: stream update
3512  * @new_update_type: [out] determined update type by the function
3513  * @new_context: [out] new context allocated and validated if update type is
3514  * FULL, reference to current context if update type is less than FULL.
3515  *
3516  * Return: true if a valid update is populated into new_context, false
3517  * otherwise.
3518  */
3519 static bool update_planes_and_stream_state(struct dc *dc,
3520 		struct dc_surface_update *srf_updates, int surface_count,
3521 		struct dc_stream_state *stream,
3522 		struct dc_stream_update *stream_update,
3523 		enum surface_update_type *new_update_type,
3524 		struct dc_state **new_context)
3525 {
3526 	struct dc_state *context;
3527 	int i, j;
3528 	enum surface_update_type update_type;
3529 	const struct dc_stream_status *stream_status;
3530 	struct dc_context *dc_ctx = dc->ctx;
3531 
3532 	stream_status = dc_stream_get_status(stream);
3533 
3534 	if (!stream_status) {
3535 		if (surface_count) /* Only an error condition if surf_count non-zero*/
3536 			ASSERT(false);
3537 
3538 		return false; /* Cannot commit surface to stream that is not committed */
3539 	}
3540 
3541 	context = dc->current_state;
3542 	update_type = dc_check_update_surfaces_for_stream(
3543 			&dc->check_config, srf_updates, surface_count, stream_update).update_type;
3544 	if (full_update_required_weak(dc, srf_updates, surface_count, stream_update, stream))
3545 		update_type = UPDATE_TYPE_FULL;
3546 
3547 	/* It is possible to receive a flip for one plane while there are multiple flip_immediate planes in the same stream.
3548 	 * E.g. Desktop and MPO plane are flip_immediate but only the MPO plane received a flip
3549 	 * Force the other flip_immediate planes to flip so GSL doesn't wait for a flip that won't come.
3550 	 */
3551 	force_immediate_gsl_plane_flip(dc, srf_updates, surface_count);
3552 	if (update_type == UPDATE_TYPE_FULL)
3553 		backup_planes_and_stream_state(&dc->scratch.current_state, stream);
3554 
3555 	/* update current stream with the new updates */
3556 	copy_stream_update_to_stream(dc, context, stream, stream_update);
3557 
3558 	/* do not perform surface update if surface has invalid dimensions
3559 	 * (all zero) and no scaling_info is provided
3560 	 */
3561 	if (surface_count > 0) {
3562 		for (i = 0; i < surface_count; i++) {
3563 			if ((srf_updates[i].surface->src_rect.width == 0 ||
3564 				 srf_updates[i].surface->src_rect.height == 0 ||
3565 				 srf_updates[i].surface->dst_rect.width == 0 ||
3566 				 srf_updates[i].surface->dst_rect.height == 0) &&
3567 				(!srf_updates[i].scaling_info ||
3568 				  srf_updates[i].scaling_info->src_rect.width == 0 ||
3569 				  srf_updates[i].scaling_info->src_rect.height == 0 ||
3570 				  srf_updates[i].scaling_info->dst_rect.width == 0 ||
3571 				  srf_updates[i].scaling_info->dst_rect.height == 0)) {
3572 				DC_ERROR("Invalid src/dst rects in surface update!\n");
3573 				return false;
3574 			}
3575 		}
3576 	}
3577 
3578 	if (update_type == UPDATE_TYPE_FULL) {
3579 		if (stream_update) {
3580 			uint32_t dsc_changed = stream_update->stream->update_flags.bits.dsc_changed;
3581 			stream_update->stream->update_flags.raw = 0xFFFFFFFF;
3582 			stream_update->stream->update_flags.bits.dsc_changed = dsc_changed;
3583 		}
3584 		for (i = 0; i < surface_count; i++)
3585 			srf_updates[i].surface->update_flags.raw = 0xFFFFFFFF;
3586 	}
3587 
3588 	if (update_type >= update_surface_trace_level)
3589 		update_surface_trace(dc, srf_updates, surface_count);
3590 
3591 	for (i = 0; i < surface_count; i++)
3592 		copy_surface_update_to_plane(srf_updates[i].surface, &srf_updates[i]);
3593 
3594 	if (update_type >= UPDATE_TYPE_FULL) {
3595 		struct dc_plane_state *new_planes[MAX_SURFACES] = {0};
3596 
3597 		for (i = 0; i < surface_count; i++)
3598 			new_planes[i] = srf_updates[i].surface;
3599 
3600 		/* initialize scratch memory for building context */
3601 		context = dc_state_create_copy(dc->current_state);
3602 		if (context == NULL) {
3603 			DC_ERROR("Failed to allocate new validate context!\n");
3604 			return false;
3605 		}
3606 
3607 		/* For each full update, remove all existing phantom pipes first.
3608 		 * Ensures that we have enough pipes for newly added MPO planes
3609 		 */
3610 		dc_state_remove_phantom_streams_and_planes(dc, context);
3611 		dc_state_release_phantom_streams_and_planes(dc, context);
3612 
3613 		/*remove old surfaces from context */
3614 		if (!dc_state_rem_all_planes_for_stream(dc, stream, context)) {
3615 
3616 			BREAK_TO_DEBUGGER();
3617 			goto fail;
3618 		}
3619 
3620 		/* add surface to context */
3621 		if (!dc_state_add_all_planes_for_stream(dc, stream, new_planes, surface_count, context)) {
3622 
3623 			BREAK_TO_DEBUGGER();
3624 			goto fail;
3625 		}
3626 	}
3627 
3628 	/* save update parameters into surface */
3629 	for (i = 0; i < surface_count; i++) {
3630 		struct dc_plane_state *surface = srf_updates[i].surface;
3631 
3632 		if (update_type != UPDATE_TYPE_MED)
3633 			continue;
3634 		if (surface->update_flags.bits.position_change) {
3635 			for (j = 0; j < dc->res_pool->pipe_count; j++) {
3636 				struct pipe_ctx *pipe_ctx = &context->res_ctx.pipe_ctx[j];
3637 
3638 				if (pipe_ctx->plane_state != surface)
3639 					continue;
3640 
3641 				resource_build_scaling_params(pipe_ctx);
3642 			}
3643 		}
3644 	}
3645 
3646 	if (update_type == UPDATE_TYPE_FULL) {
3647 		struct pipe_split_policy_backup policy;
3648 		bool minimize = false;
3649 
3650 		if (dc->check_config.deferred_transition_state) {
3651 			if (dc->check_config.transition_countdown_to_steady_state) {
3652 				/* During countdown, all new contexts created as minimal transition states */
3653 				minimize = true;
3654 			} else {
3655 				dc->check_config.deferred_transition_state = false;
3656 			}
3657 		}
3658 
3659 		if (minimize)
3660 			backup_and_set_minimal_pipe_split_policy(dc, context, &policy);
3661 
3662 		if (dc->res_pool->funcs->validate_bandwidth(dc, context, DC_VALIDATE_MODE_AND_PROGRAMMING) != DC_OK) {
3663 			if (minimize)
3664 				restore_minimal_pipe_split_policy(dc, context, &policy);
3665 			BREAK_TO_DEBUGGER();
3666 			goto fail;
3667 		}
3668 
3669 		if (minimize)
3670 			restore_minimal_pipe_split_policy(dc, context, &policy);
3671 	}
3672 	update_seamless_boot_flags(dc, context, surface_count, stream);
3673 
3674 	*new_context = context;
3675 	*new_update_type = update_type;
3676 	if (update_type == UPDATE_TYPE_FULL)
3677 		backup_planes_and_stream_state(&dc->scratch.new_state, stream);
3678 
3679 	return true;
3680 
3681 fail:
3682 	dc_state_release(context);
3683 
3684 	return false;
3685 
3686 }
3687 
3688 static void commit_planes_do_stream_update(struct dc *dc,
3689 		struct dc_stream_state *stream,
3690 		struct dc_stream_update *stream_update,
3691 		enum surface_update_type update_type,
3692 		struct dc_state *context)
3693 {
3694 	int j;
3695 
3696 	// Stream updates
3697 	for (j = 0; j < dc->res_pool->pipe_count; j++) {
3698 		struct pipe_ctx *pipe_ctx = &context->res_ctx.pipe_ctx[j];
3699 
3700 		if (resource_is_pipe_type(pipe_ctx, OTG_MASTER) && pipe_ctx->stream == stream) {
3701 
3702 			if (stream_update->periodic_interrupt && dc->hwss.setup_periodic_interrupt)
3703 				dc->hwss.setup_periodic_interrupt(dc, pipe_ctx);
3704 
3705 			if ((stream_update->hdr_static_metadata && !stream->use_dynamic_meta) ||
3706 					stream_update->vrr_infopacket ||
3707 					stream_update->vsc_infopacket ||
3708 					stream_update->vsp_infopacket ||
3709 					stream_update->hfvsif_infopacket ||
3710 					stream_update->adaptive_sync_infopacket ||
3711 					stream_update->vtem_infopacket ||
3712 					stream_update->avi_infopacket) {
3713 				resource_build_info_frame(pipe_ctx);
3714 				dc->hwss.update_info_frame(pipe_ctx);
3715 
3716 				if (dc_is_dp_signal(pipe_ctx->stream->signal))
3717 					dc->link_srv->dp_trace_source_sequence(
3718 							pipe_ctx->stream->link,
3719 							DPCD_SOURCE_SEQ_AFTER_UPDATE_INFO_FRAME);
3720 			}
3721 
3722 			if (stream_update->hdr_static_metadata &&
3723 					stream->use_dynamic_meta &&
3724 					dc->hwss.set_dmdata_attributes &&
3725 					pipe_ctx->stream->dmdata_address.quad_part != 0)
3726 				dc->hwss.set_dmdata_attributes(pipe_ctx);
3727 
3728 			if (stream_update->gamut_remap)
3729 				dc_stream_set_gamut_remap(dc, stream);
3730 
3731 			if (stream_update->output_csc_transform)
3732 				dc_stream_program_csc_matrix(dc, stream);
3733 
3734 			if (stream_update->dither_option) {
3735 				struct pipe_ctx *odm_pipe = pipe_ctx->next_odm_pipe;
3736 				resource_build_bit_depth_reduction_params(pipe_ctx->stream,
3737 									&pipe_ctx->stream->bit_depth_params);
3738 				pipe_ctx->stream_res.opp->funcs->opp_program_fmt(pipe_ctx->stream_res.opp,
3739 						&stream->bit_depth_params,
3740 						&stream->clamping);
3741 				while (odm_pipe) {
3742 					odm_pipe->stream_res.opp->funcs->opp_program_fmt(odm_pipe->stream_res.opp,
3743 							&stream->bit_depth_params,
3744 							&stream->clamping);
3745 					odm_pipe = odm_pipe->next_odm_pipe;
3746 				}
3747 			}
3748 
3749 			if (stream_update->cursor_attributes)
3750 				program_cursor_attributes(dc, stream);
3751 
3752 			if (stream_update->cursor_position)
3753 				program_cursor_position(dc, stream);
3754 
3755 			/* Full fe update*/
3756 			if (update_type == UPDATE_TYPE_FAST)
3757 				continue;
3758 
3759 			if (stream_update->dsc_config)
3760 				dc->link_srv->update_dsc_config(pipe_ctx);
3761 
3762 			if (stream_update->mst_bw_update) {
3763 				if (stream_update->mst_bw_update->is_increase)
3764 					dc->link_srv->increase_mst_payload(pipe_ctx,
3765 							stream_update->mst_bw_update->mst_stream_bw);
3766 				else
3767 					dc->link_srv->reduce_mst_payload(pipe_ctx,
3768 							stream_update->mst_bw_update->mst_stream_bw);
3769 			}
3770 
3771 			if (stream_update->pending_test_pattern) {
3772 				/*
3773 				 * test pattern params depends on ODM topology
3774 				 * changes that we could be applying to front
3775 				 * end. Since at the current stage front end
3776 				 * changes are not yet applied. We can only
3777 				 * apply test pattern in hw based on current
3778 				 * state and populate the final test pattern
3779 				 * params in new state. If current and new test
3780 				 * pattern params are different as result of
3781 				 * different ODM topology being used, it will be
3782 				 * detected and handle during front end
3783 				 * programming update.
3784 				 */
3785 				dc->link_srv->dp_set_test_pattern(stream->link,
3786 					stream->test_pattern.type,
3787 					stream->test_pattern.color_space,
3788 					stream->test_pattern.p_link_settings,
3789 					stream->test_pattern.p_custom_pattern,
3790 					stream->test_pattern.cust_pattern_size);
3791 				resource_build_test_pattern_params(&context->res_ctx, pipe_ctx);
3792 			}
3793 
3794 			if (stream_update->dpms_off) {
3795 				if (*stream_update->dpms_off) {
3796 					dc->link_srv->set_dpms_off(pipe_ctx);
3797 					/* for dpms, keep acquired resources*/
3798 					if (pipe_ctx->stream_res.audio && !dc->debug.az_endpoint_mute_only)
3799 						pipe_ctx->stream_res.audio->funcs->az_disable(pipe_ctx->stream_res.audio);
3800 
3801 					dc->optimized_required = true;
3802 
3803 				} else {
3804 					if (get_seamless_boot_stream_count(context) == 0)
3805 						dc->hwss.prepare_bandwidth(dc, dc->current_state);
3806 					dc->link_srv->set_dpms_on(dc->current_state, pipe_ctx);
3807 				}
3808 			} else if (pipe_ctx->stream->link->wa_flags.blank_stream_on_ocs_change && stream_update->output_color_space
3809 					&& !stream->dpms_off && dc_is_dp_signal(pipe_ctx->stream->signal)) {
3810 				/*
3811 				 * Workaround for firmware issue in some receivers where they don't pick up
3812 				 * correct output color space unless DP link is disabled/re-enabled
3813 				 */
3814 				dc->link_srv->set_dpms_on(dc->current_state, pipe_ctx);
3815 			}
3816 
3817 			if (stream_update->abm_level && pipe_ctx->stream_res.abm) {
3818 				bool should_program_abm = true;
3819 
3820 				// if otg funcs defined check if blanked before programming
3821 				if (pipe_ctx->stream_res.tg->funcs->is_blanked)
3822 					if (pipe_ctx->stream_res.tg->funcs->is_blanked(pipe_ctx->stream_res.tg))
3823 						should_program_abm = false;
3824 
3825 				if (should_program_abm) {
3826 					if (*stream_update->abm_level == ABM_LEVEL_IMMEDIATE_DISABLE) {
3827 						dc->hwss.set_abm_immediate_disable(pipe_ctx);
3828 					} else {
3829 						pipe_ctx->stream_res.abm->funcs->set_abm_level(
3830 							pipe_ctx->stream_res.abm, stream->abm_level);
3831 					}
3832 				}
3833 			}
3834 		}
3835 	}
3836 }
3837 
3838 static bool dc_dmub_should_send_dirty_rect_cmd(struct dc *dc, struct dc_stream_state *stream)
3839 {
3840 	if ((stream->link->psr_settings.psr_version == DC_PSR_VERSION_SU_1
3841 			|| stream->link->psr_settings.psr_version == DC_PSR_VERSION_1)
3842 			&& stream->ctx->dce_version >= DCN_VERSION_3_1)
3843 		return true;
3844 
3845 	if (stream->link->replay_settings.config.replay_supported)
3846 		return true;
3847 
3848 	if (stream->ctx->dce_version >= DCN_VERSION_3_5 && stream->abm_level)
3849 		return true;
3850 
3851 	return false;
3852 }
3853 
3854 void dc_dmub_update_dirty_rect(struct dc *dc,
3855 			       int surface_count,
3856 			       struct dc_stream_state *stream,
3857 			       const struct dc_surface_update *srf_updates,
3858 			       struct dc_state *context)
3859 {
3860 	union dmub_rb_cmd cmd;
3861 	struct dmub_cmd_update_dirty_rect_data *update_dirty_rect;
3862 	unsigned int i, j;
3863 	unsigned int panel_inst = 0;
3864 
3865 	if (!dc_dmub_should_send_dirty_rect_cmd(dc, stream))
3866 		return;
3867 
3868 	if (!dc->config.frame_update_cmd_version2 && !dc_get_edp_link_panel_inst(dc, stream->link, &panel_inst))
3869 		return;
3870 
3871 	memset(&cmd, 0x0, sizeof(cmd));
3872 	cmd.update_dirty_rect.header.type = DMUB_CMD__UPDATE_DIRTY_RECT;
3873 	cmd.update_dirty_rect.header.sub_type = 0;
3874 	cmd.update_dirty_rect.header.payload_bytes =
3875 		sizeof(cmd.update_dirty_rect) -
3876 		sizeof(cmd.update_dirty_rect.header);
3877 	update_dirty_rect = &cmd.update_dirty_rect.update_dirty_rect_data;
3878 	for (i = 0; i < surface_count; i++) {
3879 		struct dc_plane_state *plane_state = srf_updates[i].surface;
3880 		const struct dc_flip_addrs *flip_addr = srf_updates[i].flip_addr;
3881 
3882 		if (!srf_updates[i].surface || !flip_addr)
3883 			continue;
3884 		/* Do not send in immediate flip mode */
3885 		if (srf_updates[i].surface->flip_immediate)
3886 			continue;
3887 
3888 		if (dc->config.frame_update_cmd_version2)
3889 			update_dirty_rect->cmd_version = DMUB_CMD_CURSOR_UPDATE_VERSION_2;
3890 		else
3891 			update_dirty_rect->cmd_version = DMUB_CMD_CURSOR_UPDATE_VERSION_1;
3892 
3893 		update_dirty_rect->dirty_rect_count = flip_addr->dirty_rect_count;
3894 		memcpy(update_dirty_rect->src_dirty_rects, flip_addr->dirty_rects,
3895 				sizeof(flip_addr->dirty_rects));
3896 		for (j = 0; j < dc->res_pool->pipe_count; j++) {
3897 			struct pipe_ctx *pipe_ctx = &context->res_ctx.pipe_ctx[j];
3898 
3899 			if (pipe_ctx->stream != stream)
3900 				continue;
3901 			if (pipe_ctx->plane_state != plane_state)
3902 				continue;
3903 
3904 			update_dirty_rect->panel_inst = panel_inst;
3905 			update_dirty_rect->pipe_idx = j;
3906 			update_dirty_rect->otg_inst = pipe_ctx->stream_res.tg->inst;
3907 			dc_wake_and_execute_dmub_cmd(dc->ctx, &cmd, DM_DMUB_WAIT_TYPE_NO_WAIT);
3908 		}
3909 	}
3910 }
3911 
3912 static void build_dmub_update_dirty_rect(
3913 		struct dc *dc,
3914 		int surface_count,
3915 		struct dc_stream_state *stream,
3916 		struct dc_surface_update *srf_updates,
3917 		struct dc_state *context,
3918 		struct dc_dmub_cmd dc_dmub_cmd[],
3919 		unsigned int *dmub_cmd_count)
3920 {
3921 	union dmub_rb_cmd cmd;
3922 	struct dmub_cmd_update_dirty_rect_data *update_dirty_rect;
3923 	unsigned int i, j;
3924 	unsigned int panel_inst = 0;
3925 
3926 	if (!dc_dmub_should_send_dirty_rect_cmd(dc, stream))
3927 		return;
3928 
3929 	if (!dc->config.frame_update_cmd_version2 && !dc_get_edp_link_panel_inst(dc, stream->link, &panel_inst))
3930 		return;
3931 
3932 	memset(&cmd, 0x0, sizeof(cmd));
3933 	cmd.update_dirty_rect.header.type = DMUB_CMD__UPDATE_DIRTY_RECT;
3934 	cmd.update_dirty_rect.header.sub_type = 0;
3935 	cmd.update_dirty_rect.header.payload_bytes =
3936 		sizeof(cmd.update_dirty_rect) -
3937 		sizeof(cmd.update_dirty_rect.header);
3938 	update_dirty_rect = &cmd.update_dirty_rect.update_dirty_rect_data;
3939 	for (i = 0; i < surface_count; i++) {
3940 		struct dc_plane_state *plane_state = srf_updates[i].surface;
3941 		const struct dc_flip_addrs *flip_addr = srf_updates[i].flip_addr;
3942 
3943 		if (!srf_updates[i].surface || !flip_addr)
3944 			continue;
3945 		/* Do not send in immediate flip mode */
3946 		if (srf_updates[i].surface->flip_immediate)
3947 			continue;
3948 
3949 		if (dc->config.frame_update_cmd_version2)
3950 			update_dirty_rect->cmd_version = DMUB_CMD_CURSOR_UPDATE_VERSION_2;
3951 		else
3952 			update_dirty_rect->cmd_version = DMUB_CMD_CURSOR_UPDATE_VERSION_1;
3953 
3954 		update_dirty_rect->dirty_rect_count = flip_addr->dirty_rect_count;
3955 		memcpy(update_dirty_rect->src_dirty_rects, flip_addr->dirty_rects,
3956 				sizeof(flip_addr->dirty_rects));
3957 		for (j = 0; j < dc->res_pool->pipe_count; j++) {
3958 			struct pipe_ctx *pipe_ctx = &context->res_ctx.pipe_ctx[j];
3959 
3960 			if (pipe_ctx->stream != stream)
3961 				continue;
3962 			if (pipe_ctx->plane_state != plane_state)
3963 				continue;
3964 			update_dirty_rect->panel_inst = panel_inst;
3965 			update_dirty_rect->pipe_idx = j;
3966 			update_dirty_rect->otg_inst = pipe_ctx->stream_res.tg->inst;
3967 			dc_dmub_cmd[*dmub_cmd_count].dmub_cmd = cmd;
3968 			dc_dmub_cmd[*dmub_cmd_count].wait_type = DM_DMUB_WAIT_TYPE_NO_WAIT;
3969 			(*dmub_cmd_count)++;
3970 		}
3971 	}
3972 }
3973 
3974 static bool check_address_only_update(union surface_update_flags update_flags)
3975 {
3976 	union surface_update_flags addr_only_update_flags;
3977 	addr_only_update_flags.raw = 0;
3978 	addr_only_update_flags.bits.addr_update = 1;
3979 
3980 	return update_flags.bits.addr_update &&
3981 			!(update_flags.raw & ~addr_only_update_flags.raw);
3982 }
3983 
3984 /**
3985  * build_dmub_cmd_list() - Build an array of DMCUB commands to be sent to DMCUB
3986  *
3987  * @dc: Current DC state
3988  * @srf_updates: Array of surface updates
3989  * @surface_count: Number of surfaces that have an updated
3990  * @stream: Corresponding stream to be updated in the current flip
3991  * @context: New DC state to be programmed
3992  *
3993  * @dc_dmub_cmd: Array of DMCUB commands to be sent to DMCUB
3994  * @dmub_cmd_count: Count indicating the number of DMCUB commands in dc_dmub_cmd array
3995  *
3996  * This function builds an array of DMCUB commands to be sent to DMCUB. This function is required
3997  * to build an array of commands and have them sent while the OTG lock is acquired.
3998  *
3999  * Return: void
4000  */
4001 static void build_dmub_cmd_list(struct dc *dc,
4002 		struct dc_surface_update *srf_updates,
4003 		int surface_count,
4004 		struct dc_stream_state *stream,
4005 		struct dc_state *context,
4006 		struct dc_dmub_cmd dc_dmub_cmd[],
4007 		unsigned int *dmub_cmd_count)
4008 {
4009 	// Initialize cmd count to 0
4010 	*dmub_cmd_count = 0;
4011 	build_dmub_update_dirty_rect(dc, surface_count, stream, srf_updates, context, dc_dmub_cmd, dmub_cmd_count);
4012 }
4013 
4014 static void commit_plane_for_stream_offload_fams2_flip(struct dc *dc,
4015 		struct dc_surface_update *srf_updates,
4016 		int surface_count,
4017 		struct dc_stream_state *stream,
4018 		struct dc_state *context)
4019 {
4020 	int i, j;
4021 
4022 	/* update dirty rect for PSR */
4023 	dc_dmub_update_dirty_rect(dc, surface_count, stream,
4024 			srf_updates, context);
4025 
4026 	/* Perform requested Updates */
4027 	for (i = 0; i < surface_count; i++) {
4028 		struct dc_plane_state *plane_state = srf_updates[i].surface;
4029 
4030 		for (j = 0; j < dc->res_pool->pipe_count; j++) {
4031 			struct pipe_ctx *pipe_ctx = &context->res_ctx.pipe_ctx[j];
4032 
4033 			if (!should_update_pipe_for_stream(context, pipe_ctx, stream))
4034 				continue;
4035 
4036 			if (!should_update_pipe_for_plane(context, pipe_ctx, plane_state))
4037 				continue;
4038 
4039 			/* update pipe context for plane */
4040 			if (pipe_ctx->plane_state->update_flags.bits.addr_update)
4041 				dc->hwss.update_plane_addr(dc, pipe_ctx);
4042 		}
4043 	}
4044 
4045 	/* Send commands to DMCUB */
4046 	dc_dmub_srv_fams2_passthrough_flip(dc,
4047 				context,
4048 				stream,
4049 				srf_updates,
4050 				surface_count);
4051 }
4052 
4053 static void commit_planes_for_stream_fast(struct dc *dc,
4054 		struct dc_surface_update *srf_updates,
4055 		int surface_count,
4056 		struct dc_stream_state *stream,
4057 		struct dc_stream_update *stream_update,
4058 		enum surface_update_type update_type,
4059 		struct dc_state *context)
4060 {
4061 	int i, j;
4062 	struct pipe_ctx *top_pipe_to_program = NULL;
4063 	struct dc_stream_status *stream_status = NULL;
4064 	bool should_offload_fams2_flip = false;
4065 	bool should_lock_all_pipes = (update_type != UPDATE_TYPE_FAST);
4066 
4067 	if (should_lock_all_pipes)
4068 		determine_pipe_unlock_order(dc, context);
4069 
4070 	if (dc->debug.fams2_config.bits.enable &&
4071 			dc->debug.fams2_config.bits.enable_offload_flip &&
4072 			dc_state_is_fams2_in_use(dc, context)) {
4073 		/* if not offloading to HWFQ, offload to FAMS2 if needed */
4074 		should_offload_fams2_flip = true;
4075 		for (i = 0; i < surface_count; i++) {
4076 			if (srf_updates[i].surface &&
4077 					srf_updates[i].surface->update_flags.raw &&
4078 					!check_address_only_update(srf_updates[i].surface->update_flags)) {
4079 				/* more than address update, need to acquire FAMS2 lock */
4080 				should_offload_fams2_flip = false;
4081 				break;
4082 			}
4083 		}
4084 		if (stream_update) {
4085 			/* more than address update, need to acquire FAMS2 lock */
4086 			should_offload_fams2_flip = false;
4087 		}
4088 	}
4089 
4090 	dc_exit_ips_for_hw_access(dc);
4091 
4092 	dc_z10_restore(dc);
4093 
4094 	top_pipe_to_program = resource_get_otg_master_for_stream(
4095 			&context->res_ctx,
4096 			stream);
4097 
4098 	if (!top_pipe_to_program)
4099 		return;
4100 
4101 	for (i = 0; i < dc->res_pool->pipe_count; i++) {
4102 		struct pipe_ctx *pipe = &context->res_ctx.pipe_ctx[i];
4103 
4104 		if (pipe->stream && pipe->plane_state) {
4105 			if (!dc->debug.using_dml2)
4106 				set_p_state_switch_method(dc, context, pipe);
4107 
4108 			if (dc->debug.visual_confirm)
4109 				dc_update_visual_confirm_color(dc, context, pipe);
4110 		}
4111 	}
4112 
4113 	for (i = 0; i < surface_count; i++) {
4114 		struct dc_plane_state *plane_state = srf_updates[i].surface;
4115 		/*set logical flag for lock/unlock use*/
4116 		for (j = 0; j < dc->res_pool->pipe_count; j++) {
4117 			struct pipe_ctx *pipe_ctx = &context->res_ctx.pipe_ctx[j];
4118 
4119 			if (!pipe_ctx->plane_state)
4120 				continue;
4121 			if (!should_update_pipe_for_plane(context, pipe_ctx, plane_state))
4122 				continue;
4123 
4124 			pipe_ctx->plane_state->triplebuffer_flips = false;
4125 			if (update_type == UPDATE_TYPE_FAST &&
4126 					dc->hwss.program_triplebuffer != NULL &&
4127 					!pipe_ctx->plane_state->flip_immediate && dc->debug.enable_tri_buf) {
4128 				/*triple buffer for VUpdate only*/
4129 				pipe_ctx->plane_state->triplebuffer_flips = true;
4130 			}
4131 		}
4132 	}
4133 
4134 	stream_status = dc_state_get_stream_status(context, stream);
4135 
4136 	if (should_offload_fams2_flip) {
4137 		commit_plane_for_stream_offload_fams2_flip(dc,
4138 				srf_updates,
4139 				surface_count,
4140 				stream,
4141 				context);
4142 	} else if (stream_status) {
4143 		build_dmub_cmd_list(dc,
4144 				srf_updates,
4145 				surface_count,
4146 				stream,
4147 				context,
4148 				context->dc_dmub_cmd,
4149 				&(context->dmub_cmd_count));
4150 		hwss_build_fast_sequence(dc,
4151 				context->dc_dmub_cmd,
4152 				context->dmub_cmd_count,
4153 				context->block_sequence,
4154 				&(context->block_sequence_steps),
4155 				top_pipe_to_program,
4156 				stream_status,
4157 				context);
4158 		hwss_execute_sequence(dc,
4159 				context->block_sequence,
4160 				context->block_sequence_steps);
4161 	}
4162 
4163 	/* Clear update flags so next flip doesn't have redundant programming
4164 	 * (if there's no stream update, the update flags are not cleared).
4165 	 * Surface updates are cleared unconditionally at the beginning of each flip,
4166 	 * so no need to clear here.
4167 	 */
4168 	if (top_pipe_to_program->stream)
4169 		top_pipe_to_program->stream->update_flags.raw = 0;
4170 }
4171 
4172 static void commit_planes_for_stream(struct dc *dc,
4173 		const struct dc_surface_update *srf_updates,
4174 		int surface_count,
4175 		struct dc_stream_state *stream,
4176 		struct dc_stream_update *stream_update,
4177 		enum surface_update_type update_type,
4178 		struct dc_state *context)
4179 {
4180 	int i, j;
4181 	struct pipe_ctx *top_pipe_to_program = NULL;
4182 	bool should_lock_all_pipes = (update_type != UPDATE_TYPE_FAST);
4183 	bool subvp_prev_use = false;
4184 	bool subvp_curr_use = false;
4185 	uint8_t current_stream_mask = 0;
4186 
4187 	if (should_lock_all_pipes)
4188 		determine_pipe_unlock_order(dc, context);
4189 	// Once we apply the new subvp context to hardware it won't be in the
4190 	// dc->current_state anymore, so we have to cache it before we apply
4191 	// the new SubVP context
4192 	subvp_prev_use = false;
4193 	dc_exit_ips_for_hw_access(dc);
4194 
4195 	dc_z10_restore(dc);
4196 	if (update_type == UPDATE_TYPE_FULL && dc->optimized_required)
4197 		hwss_process_outstanding_hw_updates(dc, dc->current_state);
4198 
4199 	if (update_type != UPDATE_TYPE_FAST && dc->res_pool->funcs->prepare_mcache_programming)
4200 		dc->res_pool->funcs->prepare_mcache_programming(dc, context);
4201 
4202 	for (i = 0; i < dc->res_pool->pipe_count; i++) {
4203 		struct pipe_ctx *pipe = &context->res_ctx.pipe_ctx[i];
4204 
4205 		if (pipe->stream && pipe->plane_state) {
4206 			if (!dc->debug.using_dml2)
4207 				set_p_state_switch_method(dc, context, pipe);
4208 
4209 			if (dc->debug.visual_confirm)
4210 				dc_update_visual_confirm_color(dc, context, pipe);
4211 		}
4212 	}
4213 
4214 	if (update_type == UPDATE_TYPE_FULL) {
4215 		dc_allow_idle_optimizations(dc, false);
4216 
4217 		if (get_seamless_boot_stream_count(context) == 0)
4218 			dc->hwss.prepare_bandwidth(dc, context);
4219 
4220 		if (dc->hwss.update_dsc_pg)
4221 			dc->hwss.update_dsc_pg(dc, context, false);
4222 
4223 		context_clock_trace(dc, context);
4224 	}
4225 
4226 	if (update_type == UPDATE_TYPE_FULL)
4227 		hwss_wait_for_outstanding_hw_updates(dc, dc->current_state);
4228 
4229 	top_pipe_to_program = resource_get_otg_master_for_stream(
4230 				&context->res_ctx,
4231 				stream);
4232 	ASSERT(top_pipe_to_program != NULL);
4233 
4234 	for (i = 0; i < dc->res_pool->pipe_count; i++) {
4235 		struct pipe_ctx *old_pipe = &dc->current_state->res_ctx.pipe_ctx[i];
4236 
4237 		// Check old context for SubVP
4238 		subvp_prev_use |= (dc_state_get_pipe_subvp_type(dc->current_state, old_pipe) == SUBVP_PHANTOM);
4239 		if (subvp_prev_use)
4240 			break;
4241 	}
4242 
4243 	for (i = 0; i < dc->res_pool->pipe_count; i++) {
4244 		struct pipe_ctx *pipe = &context->res_ctx.pipe_ctx[i];
4245 
4246 		if (dc_state_get_pipe_subvp_type(context, pipe) == SUBVP_PHANTOM) {
4247 			subvp_curr_use = true;
4248 			break;
4249 		}
4250 	}
4251 
4252 	if (stream->test_pattern.type != DP_TEST_PATTERN_VIDEO_MODE) {
4253 		struct pipe_ctx *mpcc_pipe;
4254 		struct pipe_ctx *odm_pipe;
4255 
4256 		for (mpcc_pipe = top_pipe_to_program; mpcc_pipe; mpcc_pipe = mpcc_pipe->bottom_pipe)
4257 			for (odm_pipe = mpcc_pipe; odm_pipe; odm_pipe = odm_pipe->next_odm_pipe)
4258 				odm_pipe->ttu_regs.min_ttu_vblank = MAX_TTU;
4259 	}
4260 
4261 	if ((update_type != UPDATE_TYPE_FAST) && stream->update_flags.bits.dsc_changed)
4262 		if (top_pipe_to_program &&
4263 			top_pipe_to_program->stream_res.tg->funcs->lock_doublebuffer_enable) {
4264 			if (should_use_dmub_inbox1_lock(dc, stream->link)) {
4265 				union dmub_hw_lock_flags hw_locks = { 0 };
4266 				struct dmub_hw_lock_inst_flags inst_flags = { 0 };
4267 
4268 				hw_locks.bits.lock_dig = 1;
4269 				inst_flags.dig_inst = top_pipe_to_program->stream_res.tg->inst;
4270 
4271 				dmub_hw_lock_mgr_cmd(dc->ctx->dmub_srv,
4272 							true,
4273 							&hw_locks,
4274 							&inst_flags);
4275 			} else
4276 				top_pipe_to_program->stream_res.tg->funcs->lock_doublebuffer_enable(
4277 						top_pipe_to_program->stream_res.tg);
4278 		}
4279 
4280 	if (dc->hwss.wait_for_dcc_meta_propagation) {
4281 		dc->hwss.wait_for_dcc_meta_propagation(dc, top_pipe_to_program);
4282 	}
4283 
4284 	if (dc->hwseq->funcs.wait_for_pipe_update_if_needed)
4285 		dc->hwseq->funcs.wait_for_pipe_update_if_needed(dc, top_pipe_to_program, update_type < UPDATE_TYPE_FULL);
4286 
4287 	if (should_lock_all_pipes && dc->hwss.interdependent_update_lock) {
4288 		if (dc->hwss.subvp_pipe_control_lock)
4289 			dc->hwss.subvp_pipe_control_lock(dc, context, true, should_lock_all_pipes, NULL, subvp_prev_use);
4290 
4291 		if (dc->hwss.dmub_hw_control_lock)
4292 			dc->hwss.dmub_hw_control_lock(dc, context, true);
4293 
4294 		dc->hwss.interdependent_update_lock(dc, context, true);
4295 	} else {
4296 		if (dc->hwss.subvp_pipe_control_lock)
4297 			dc->hwss.subvp_pipe_control_lock(dc, context, true, should_lock_all_pipes, top_pipe_to_program, subvp_prev_use);
4298 
4299 		if (dc->hwss.dmub_hw_control_lock)
4300 			dc->hwss.dmub_hw_control_lock(dc, context, true);
4301 
4302 		/* Lock the top pipe while updating plane addrs, since freesync requires
4303 		 *  plane addr update event triggers to be synchronized.
4304 		 *  top_pipe_to_program is expected to never be NULL
4305 		 */
4306 		dc->hwss.pipe_control_lock(dc, top_pipe_to_program, true);
4307 	}
4308 
4309 	dc_dmub_update_dirty_rect(dc, surface_count, stream, srf_updates, context);
4310 
4311 	// Stream updates
4312 	if (stream_update)
4313 		commit_planes_do_stream_update(dc, stream, stream_update, update_type, context);
4314 
4315 	if (surface_count == 0) {
4316 		/*
4317 		 * In case of turning off screen, no need to program front end a second time.
4318 		 * just return after program blank.
4319 		 */
4320 		if (dc->hwss.apply_ctx_for_surface)
4321 			dc->hwss.apply_ctx_for_surface(dc, stream, 0, context);
4322 		if (dc->hwss.program_front_end_for_ctx)
4323 			dc->hwss.program_front_end_for_ctx(dc, context);
4324 
4325 		if (should_lock_all_pipes && dc->hwss.interdependent_update_lock) {
4326 			dc->hwss.interdependent_update_lock(dc, context, false);
4327 		} else {
4328 			dc->hwss.pipe_control_lock(dc, top_pipe_to_program, false);
4329 		}
4330 		dc->hwss.post_unlock_program_front_end(dc, context);
4331 
4332 		if (update_type != UPDATE_TYPE_FAST)
4333 			if (dc->hwss.commit_subvp_config)
4334 				dc->hwss.commit_subvp_config(dc, context);
4335 
4336 		/* Since phantom pipe programming is moved to post_unlock_program_front_end,
4337 		 * move the SubVP lock to after the phantom pipes have been setup
4338 		 */
4339 		if (dc->hwss.subvp_pipe_control_lock)
4340 			dc->hwss.subvp_pipe_control_lock(dc, context, false, should_lock_all_pipes,
4341 							 NULL, subvp_prev_use);
4342 
4343 		if (dc->hwss.dmub_hw_control_lock)
4344 			dc->hwss.dmub_hw_control_lock(dc, context, false);
4345 		return;
4346 	}
4347 
4348 	if (update_type != UPDATE_TYPE_FAST) {
4349 		for (j = 0; j < dc->res_pool->pipe_count; j++) {
4350 			struct pipe_ctx *pipe_ctx = &context->res_ctx.pipe_ctx[j];
4351 
4352 			if ((dc->debug.visual_confirm == VISUAL_CONFIRM_SUBVP ||
4353 				dc->debug.visual_confirm == VISUAL_CONFIRM_MCLK_SWITCH) &&
4354 				pipe_ctx->stream && pipe_ctx->plane_state) {
4355 				/* Only update visual confirm for SUBVP and Mclk switching here.
4356 				 * The bar appears on all pipes, so we need to update the bar on all displays,
4357 				 * so the information doesn't get stale.
4358 				 */
4359 				dc->hwss.update_visual_confirm_color(dc, pipe_ctx,
4360 						pipe_ctx->plane_res.hubp->inst);
4361 			}
4362 		}
4363 	}
4364 
4365 	for (i = 0; i < surface_count; i++) {
4366 		struct dc_plane_state *plane_state = srf_updates[i].surface;
4367 
4368 		/*set logical flag for lock/unlock use*/
4369 		for (j = 0; j < dc->res_pool->pipe_count; j++) {
4370 			struct pipe_ctx *pipe_ctx = &context->res_ctx.pipe_ctx[j];
4371 			if (!pipe_ctx->plane_state)
4372 				continue;
4373 			if (!should_update_pipe_for_plane(context, pipe_ctx, plane_state))
4374 				continue;
4375 			pipe_ctx->plane_state->triplebuffer_flips = false;
4376 			if (update_type == UPDATE_TYPE_FAST &&
4377 					dc->hwss.program_triplebuffer != NULL &&
4378 					!pipe_ctx->plane_state->flip_immediate && dc->debug.enable_tri_buf) {
4379 				/*triple buffer for VUpdate only*/
4380 				pipe_ctx->plane_state->triplebuffer_flips = true;
4381 			}
4382 		}
4383 		if (update_type == UPDATE_TYPE_FULL) {
4384 			/* force vsync flip when reconfiguring pipes to prevent underflow */
4385 			plane_state->flip_immediate = false;
4386 			plane_state->triplebuffer_flips = false;
4387 		}
4388 	}
4389 
4390 	// Update Type FULL, Surface updates
4391 	for (j = 0; j < dc->res_pool->pipe_count; j++) {
4392 		struct pipe_ctx *pipe_ctx = &context->res_ctx.pipe_ctx[j];
4393 
4394 		if (!pipe_ctx->top_pipe &&
4395 			!pipe_ctx->prev_odm_pipe &&
4396 			should_update_pipe_for_stream(context, pipe_ctx, stream)) {
4397 			struct dc_stream_status *stream_status = NULL;
4398 
4399 			if (!pipe_ctx->plane_state)
4400 				continue;
4401 
4402 			/* Full fe update*/
4403 			if (update_type == UPDATE_TYPE_FAST)
4404 				continue;
4405 
4406 			stream_status =
4407 				stream_get_status(context, pipe_ctx->stream);
4408 
4409 			if (dc->hwss.apply_ctx_for_surface && stream_status)
4410 				dc->hwss.apply_ctx_for_surface(
4411 					dc, pipe_ctx->stream, stream_status->plane_count, context);
4412 		}
4413 	}
4414 
4415 	for (j = 0; j < dc->res_pool->pipe_count; j++) {
4416 		struct pipe_ctx *pipe_ctx = &context->res_ctx.pipe_ctx[j];
4417 
4418 		if (!pipe_ctx->plane_state)
4419 			continue;
4420 
4421 		/* Full fe update*/
4422 		if (update_type == UPDATE_TYPE_FAST)
4423 			continue;
4424 
4425 		ASSERT(!pipe_ctx->plane_state->triplebuffer_flips);
4426 		if (dc->hwss.program_triplebuffer != NULL && dc->debug.enable_tri_buf) {
4427 			/*turn off triple buffer for full update*/
4428 			dc->hwss.program_triplebuffer(
4429 				dc, pipe_ctx, pipe_ctx->plane_state->triplebuffer_flips);
4430 		}
4431 	}
4432 
4433 	if (dc->hwss.program_front_end_for_ctx && update_type != UPDATE_TYPE_FAST) {
4434 		dc->hwss.program_front_end_for_ctx(dc, context);
4435 
4436 		//Pipe busy until some frame and line #
4437 		if (dc->hwseq->funcs.set_wait_for_update_needed_for_pipe && update_type == UPDATE_TYPE_FULL) {
4438 			for (j = 0; j < dc->res_pool->pipe_count; j++) {
4439 				struct pipe_ctx *pipe_ctx = &context->res_ctx.pipe_ctx[j];
4440 
4441 				dc->hwseq->funcs.set_wait_for_update_needed_for_pipe(dc, pipe_ctx);
4442 			}
4443 		}
4444 
4445 		if (dc->debug.validate_dml_output) {
4446 			for (i = 0; i < dc->res_pool->pipe_count; i++) {
4447 				struct pipe_ctx *cur_pipe = &context->res_ctx.pipe_ctx[i];
4448 				if (cur_pipe->stream == NULL)
4449 					continue;
4450 
4451 				cur_pipe->plane_res.hubp->funcs->validate_dml_output(
4452 						cur_pipe->plane_res.hubp, dc->ctx,
4453 						&context->res_ctx.pipe_ctx[i].rq_regs,
4454 						&context->res_ctx.pipe_ctx[i].dlg_regs,
4455 						&context->res_ctx.pipe_ctx[i].ttu_regs);
4456 			}
4457 		}
4458 	}
4459 
4460 	// Update Type FAST, Surface updates
4461 	if (update_type == UPDATE_TYPE_FAST) {
4462 		if (dc->hwss.set_flip_control_gsl)
4463 			for (i = 0; i < surface_count; i++) {
4464 				struct dc_plane_state *plane_state = srf_updates[i].surface;
4465 
4466 				for (j = 0; j < dc->res_pool->pipe_count; j++) {
4467 					struct pipe_ctx *pipe_ctx = &context->res_ctx.pipe_ctx[j];
4468 
4469 					if (!should_update_pipe_for_stream(context, pipe_ctx, stream))
4470 						continue;
4471 
4472 					if (!should_update_pipe_for_plane(context, pipe_ctx, plane_state))
4473 						continue;
4474 
4475 					// GSL has to be used for flip immediate
4476 					dc->hwss.set_flip_control_gsl(pipe_ctx,
4477 							pipe_ctx->plane_state->flip_immediate);
4478 				}
4479 			}
4480 
4481 		/* Perform requested Updates */
4482 		for (i = 0; i < surface_count; i++) {
4483 			struct dc_plane_state *plane_state = srf_updates[i].surface;
4484 
4485 			for (j = 0; j < dc->res_pool->pipe_count; j++) {
4486 				struct pipe_ctx *pipe_ctx = &context->res_ctx.pipe_ctx[j];
4487 
4488 				if (!should_update_pipe_for_stream(context, pipe_ctx, stream))
4489 					continue;
4490 
4491 				if (!should_update_pipe_for_plane(context, pipe_ctx, plane_state))
4492 					continue;
4493 
4494 				if (srf_updates[i].cm2_params &&
4495 						srf_updates[i].cm2_params->cm2_luts.lut3d_data.lut3d_src ==
4496 								DC_CM2_TRANSFER_FUNC_SOURCE_VIDMEM &&
4497 						srf_updates[i].cm2_params->component_settings.shaper_3dlut_setting ==
4498 								DC_CM2_SHAPER_3DLUT_SETTING_ENABLE_SHAPER_3DLUT &&
4499 						dc->hwss.trigger_3dlut_dma_load)
4500 					dc->hwss.trigger_3dlut_dma_load(dc, pipe_ctx);
4501 
4502 				/*program triple buffer after lock based on flip type*/
4503 				if (dc->hwss.program_triplebuffer != NULL && dc->debug.enable_tri_buf) {
4504 					/*only enable triplebuffer for fast_update*/
4505 					dc->hwss.program_triplebuffer(
4506 						dc, pipe_ctx, pipe_ctx->plane_state->triplebuffer_flips);
4507 				}
4508 				if (pipe_ctx->plane_state->update_flags.bits.addr_update)
4509 					dc->hwss.update_plane_addr(dc, pipe_ctx);
4510 			}
4511 		}
4512 	}
4513 
4514 	if (should_lock_all_pipes && dc->hwss.interdependent_update_lock) {
4515 		dc->hwss.interdependent_update_lock(dc, context, false);
4516 	} else {
4517 		dc->hwss.pipe_control_lock(dc, top_pipe_to_program, false);
4518 	}
4519 
4520 	if ((update_type != UPDATE_TYPE_FAST) && stream->update_flags.bits.dsc_changed)
4521 		if (top_pipe_to_program &&
4522 		    top_pipe_to_program->stream_res.tg->funcs->lock_doublebuffer_enable) {
4523 			top_pipe_to_program->stream_res.tg->funcs->wait_for_state(
4524 				top_pipe_to_program->stream_res.tg,
4525 				CRTC_STATE_VACTIVE);
4526 			top_pipe_to_program->stream_res.tg->funcs->wait_for_state(
4527 				top_pipe_to_program->stream_res.tg,
4528 				CRTC_STATE_VBLANK);
4529 			top_pipe_to_program->stream_res.tg->funcs->wait_for_state(
4530 				top_pipe_to_program->stream_res.tg,
4531 				CRTC_STATE_VACTIVE);
4532 
4533 			if (should_use_dmub_inbox1_lock(dc, stream->link)) {
4534 				union dmub_hw_lock_flags hw_locks = { 0 };
4535 				struct dmub_hw_lock_inst_flags inst_flags = { 0 };
4536 
4537 				hw_locks.bits.lock_dig = 1;
4538 				inst_flags.dig_inst = top_pipe_to_program->stream_res.tg->inst;
4539 
4540 				dmub_hw_lock_mgr_cmd(dc->ctx->dmub_srv,
4541 							false,
4542 							&hw_locks,
4543 							&inst_flags);
4544 			} else
4545 				top_pipe_to_program->stream_res.tg->funcs->lock_doublebuffer_disable(
4546 					top_pipe_to_program->stream_res.tg);
4547 		}
4548 
4549 	if (subvp_curr_use) {
4550 		/* If enabling subvp or transitioning from subvp->subvp, enable the
4551 		 * phantom streams before we program front end for the phantom pipes.
4552 		 */
4553 		if (update_type != UPDATE_TYPE_FAST) {
4554 			if (dc->hwss.enable_phantom_streams)
4555 				dc->hwss.enable_phantom_streams(dc, context);
4556 		}
4557 	}
4558 
4559 	if (update_type != UPDATE_TYPE_FAST)
4560 		dc->hwss.post_unlock_program_front_end(dc, context);
4561 
4562 	if (subvp_prev_use && !subvp_curr_use) {
4563 		/* If disabling subvp, disable phantom streams after front end
4564 		 * programming has completed (we turn on phantom OTG in order
4565 		 * to complete the plane disable for phantom pipes).
4566 		 */
4567 
4568 		if (dc->hwss.disable_phantom_streams)
4569 			dc->hwss.disable_phantom_streams(dc, context);
4570 	}
4571 
4572 	if (update_type != UPDATE_TYPE_FAST)
4573 		if (dc->hwss.commit_subvp_config)
4574 			dc->hwss.commit_subvp_config(dc, context);
4575 	/* Since phantom pipe programming is moved to post_unlock_program_front_end,
4576 	 * move the SubVP lock to after the phantom pipes have been setup
4577 	 */
4578 	if (should_lock_all_pipes && dc->hwss.interdependent_update_lock) {
4579 		if (dc->hwss.subvp_pipe_control_lock)
4580 			dc->hwss.subvp_pipe_control_lock(dc, context, false, should_lock_all_pipes, NULL, subvp_prev_use);
4581 		if (dc->hwss.dmub_hw_control_lock)
4582 			dc->hwss.dmub_hw_control_lock(dc, context, false);
4583 	} else {
4584 		if (dc->hwss.subvp_pipe_control_lock)
4585 			dc->hwss.subvp_pipe_control_lock(dc, context, false, should_lock_all_pipes, top_pipe_to_program, subvp_prev_use);
4586 		if (dc->hwss.dmub_hw_control_lock)
4587 			dc->hwss.dmub_hw_control_lock(dc, context, false);
4588 	}
4589 
4590 	// Fire manual trigger only when bottom plane is flipped
4591 	for (j = 0; j < dc->res_pool->pipe_count; j++) {
4592 		struct pipe_ctx *pipe_ctx = &context->res_ctx.pipe_ctx[j];
4593 
4594 		if (!pipe_ctx->plane_state)
4595 			continue;
4596 
4597 		if (pipe_ctx->bottom_pipe || pipe_ctx->next_odm_pipe ||
4598 				!pipe_ctx->stream || !should_update_pipe_for_stream(context, pipe_ctx, stream) ||
4599 				!pipe_ctx->plane_state->update_flags.bits.addr_update ||
4600 				pipe_ctx->plane_state->skip_manual_trigger)
4601 			continue;
4602 
4603 		if (dc->hwss.program_cursor_offload_now)
4604 			dc->hwss.program_cursor_offload_now(dc, pipe_ctx);
4605 		if (pipe_ctx->stream_res.tg->funcs->program_manual_trigger)
4606 			pipe_ctx->stream_res.tg->funcs->program_manual_trigger(pipe_ctx->stream_res.tg);
4607 	}
4608 
4609 	current_stream_mask = get_stream_mask(dc, context);
4610 	if (current_stream_mask != context->stream_mask) {
4611 		context->stream_mask = current_stream_mask;
4612 		dc_dmub_srv_notify_stream_mask(dc->ctx->dmub_srv, current_stream_mask);
4613 	}
4614 }
4615 
4616 /**
4617  * could_mpcc_tree_change_for_active_pipes - Check if an OPP associated with MPCC might change
4618  *
4619  * @dc: Used to get the current state status
4620  * @stream: Target stream, which we want to remove the attached planes
4621  * @srf_updates: Array of surface updates
4622  * @surface_count: Number of surface update
4623  * @is_plane_addition: [in] Fill out with true if it is a plane addition case
4624  *
4625  * DCN32x and newer support a feature named Dynamic ODM which can conflict with
4626  * the MPO if used simultaneously in some specific configurations (e.g.,
4627  * 4k@144). This function checks if the incoming context requires applying a
4628  * transition state with unnecessary pipe splitting and ODM disabled to
4629  * circumvent our hardware limitations to prevent this edge case. If the OPP
4630  * associated with an MPCC might change due to plane additions, this function
4631  * returns true.
4632  *
4633  * Return:
4634  * Return true if OPP and MPCC might change, otherwise, return false.
4635  */
4636 static bool could_mpcc_tree_change_for_active_pipes(struct dc *dc,
4637 		struct dc_stream_state *stream,
4638 		struct dc_surface_update *srf_updates,
4639 		int surface_count,
4640 		bool *is_plane_addition)
4641 {
4642 
4643 	struct dc_stream_status *cur_stream_status = stream_get_status(dc->current_state, stream);
4644 	bool force_minimal_pipe_splitting = false;
4645 	bool subvp_active = false;
4646 	uint32_t i;
4647 
4648 	*is_plane_addition = false;
4649 
4650 	if (cur_stream_status &&
4651 			dc->current_state->stream_count > 0 &&
4652 			dc->debug.pipe_split_policy != MPC_SPLIT_AVOID) {
4653 		/* determine if minimal transition is required due to MPC*/
4654 		if (surface_count > 0) {
4655 			if (cur_stream_status->plane_count > surface_count) {
4656 				force_minimal_pipe_splitting = true;
4657 			} else if (cur_stream_status->plane_count < surface_count) {
4658 				force_minimal_pipe_splitting = true;
4659 				*is_plane_addition = true;
4660 			}
4661 		}
4662 	}
4663 
4664 	if (cur_stream_status &&
4665 			dc->current_state->stream_count == 1 &&
4666 			dc->debug.enable_single_display_2to1_odm_policy) {
4667 		/* determine if minimal transition is required due to dynamic ODM*/
4668 		if (surface_count > 0) {
4669 			if (cur_stream_status->plane_count > 2 && cur_stream_status->plane_count > surface_count) {
4670 				force_minimal_pipe_splitting = true;
4671 			} else if (surface_count > 2 && cur_stream_status->plane_count < surface_count) {
4672 				force_minimal_pipe_splitting = true;
4673 				*is_plane_addition = true;
4674 			}
4675 		}
4676 	}
4677 
4678 	for (i = 0; i < dc->res_pool->pipe_count; i++) {
4679 		struct pipe_ctx *pipe = &dc->current_state->res_ctx.pipe_ctx[i];
4680 
4681 		if (dc_state_get_pipe_subvp_type(dc->current_state, pipe) != SUBVP_NONE) {
4682 			subvp_active = true;
4683 			break;
4684 		}
4685 	}
4686 
4687 	/* For SubVP when adding or removing planes we need to add a minimal transition
4688 	 * (even when disabling all planes). Whenever disabling a phantom pipe, we
4689 	 * must use the minimal transition path to disable the pipe correctly.
4690 	 *
4691 	 * We want to use the minimal transition whenever subvp is active, not only if
4692 	 * a plane is being added / removed from a subvp stream (MPO plane can be added
4693 	 * to a DRR pipe of SubVP + DRR config, in which case we still want to run through
4694 	 * a min transition to disable subvp.
4695 	 */
4696 	if (cur_stream_status && subvp_active) {
4697 		/* determine if minimal transition is required due to SubVP*/
4698 		if (cur_stream_status->plane_count > surface_count) {
4699 			force_minimal_pipe_splitting = true;
4700 		} else if (cur_stream_status->plane_count < surface_count) {
4701 			force_minimal_pipe_splitting = true;
4702 			*is_plane_addition = true;
4703 		}
4704 	}
4705 
4706 	return force_minimal_pipe_splitting;
4707 }
4708 
4709 
4710 static void release_minimal_transition_state(struct dc *dc,
4711 		struct dc_state *minimal_transition_context,
4712 		struct dc_state *base_context,
4713 		struct pipe_split_policy_backup *policy)
4714 {
4715 	restore_minimal_pipe_split_policy(dc, base_context, policy);
4716 	dc_state_release(minimal_transition_context);
4717 }
4718 
4719 static void force_vsync_flip_in_minimal_transition_context(struct dc_state *context)
4720 {
4721 	uint8_t i;
4722 	int j;
4723 	struct dc_stream_status *stream_status;
4724 
4725 	for (i = 0; i < context->stream_count; i++) {
4726 		stream_status = &context->stream_status[i];
4727 
4728 		for (j = 0; j < stream_status->plane_count; j++)
4729 			stream_status->plane_states[j]->flip_immediate = false;
4730 	}
4731 }
4732 
4733 static struct dc_state *create_minimal_transition_state(struct dc *dc,
4734 		struct dc_state *base_context, struct pipe_split_policy_backup *policy)
4735 {
4736 	struct dc_state *minimal_transition_context = NULL;
4737 
4738 	minimal_transition_context = dc_state_create_copy(base_context);
4739 	if (!minimal_transition_context)
4740 		return NULL;
4741 
4742 	backup_and_set_minimal_pipe_split_policy(dc, base_context, policy);
4743 	/* commit minimal state */
4744 	if (dc->res_pool->funcs->validate_bandwidth(dc, minimal_transition_context,
4745 		DC_VALIDATE_MODE_AND_PROGRAMMING) == DC_OK) {
4746 		/* prevent underflow and corruption when reconfiguring pipes */
4747 		force_vsync_flip_in_minimal_transition_context(minimal_transition_context);
4748 	} else {
4749 		/*
4750 		 * This should never happen, minimal transition state should
4751 		 * always be validated first before adding pipe split features.
4752 		 */
4753 		release_minimal_transition_state(dc, minimal_transition_context, base_context, policy);
4754 		BREAK_TO_DEBUGGER();
4755 		minimal_transition_context = NULL;
4756 	}
4757 	return minimal_transition_context;
4758 }
4759 
4760 static bool is_pipe_topology_transition_seamless_with_intermediate_step(
4761 		struct dc *dc,
4762 		struct dc_state *initial_state,
4763 		struct dc_state *intermediate_state,
4764 		struct dc_state *final_state)
4765 {
4766 	return dc->hwss.is_pipe_topology_transition_seamless(dc, initial_state,
4767 			intermediate_state) &&
4768 			dc->hwss.is_pipe_topology_transition_seamless(dc,
4769 					intermediate_state, final_state);
4770 }
4771 
4772 static void swap_and_release_current_context(struct dc *dc,
4773 		struct dc_state *new_context, struct dc_stream_state *stream)
4774 {
4775 
4776 	int i;
4777 	struct dc_state *old = dc->current_state;
4778 	struct pipe_ctx *pipe_ctx;
4779 
4780 	/* Since memory free requires elevated IRQ, an interrupt
4781 	 * request is generated by mem free. If this happens
4782 	 * between freeing and reassigning the context, our vsync
4783 	 * interrupt will call into dc and cause a memory
4784 	 * corruption. Hence, we first reassign the context,
4785 	 * then free the old context.
4786 	 */
4787 	dc->current_state = new_context;
4788 	dc_state_release(old);
4789 
4790 	// clear any forced full updates
4791 	for (i = 0; i < dc->res_pool->pipe_count; i++) {
4792 		pipe_ctx = &new_context->res_ctx.pipe_ctx[i];
4793 
4794 		if (pipe_ctx->plane_state && pipe_ctx->stream == stream)
4795 			pipe_ctx->plane_state->force_full_update = false;
4796 	}
4797 }
4798 
4799 static int initialize_empty_surface_updates(
4800 		struct dc_stream_state *stream,
4801 		struct dc_surface_update *srf_updates)
4802 {
4803 	struct dc_stream_status *status = dc_stream_get_status(stream);
4804 	int i;
4805 
4806 	if (!status)
4807 		return 0;
4808 
4809 	for (i = 0; i < status->plane_count; i++)
4810 		srf_updates[i].surface = status->plane_states[i];
4811 
4812 	return status->plane_count;
4813 }
4814 
4815 static bool commit_minimal_transition_based_on_new_context(struct dc *dc,
4816 		struct dc_state *new_context,
4817 		struct dc_stream_state *stream,
4818 		struct dc_stream_update *stream_update,
4819 		struct dc_surface_update *srf_updates,
4820 		int surface_count)
4821 {
4822 	bool success = false;
4823 	struct pipe_split_policy_backup policy;
4824 	struct dc_state *intermediate_context =
4825 			create_minimal_transition_state(dc, new_context,
4826 					&policy);
4827 
4828 	if (intermediate_context) {
4829 		if (is_pipe_topology_transition_seamless_with_intermediate_step(
4830 				dc,
4831 				dc->current_state,
4832 				intermediate_context,
4833 				new_context)) {
4834 			DC_LOG_DC("commit minimal transition state: base = new state\n");
4835 			commit_planes_for_stream(dc, srf_updates,
4836 					surface_count, stream, stream_update,
4837 					UPDATE_TYPE_FULL, intermediate_context);
4838 			swap_and_release_current_context(
4839 					dc, intermediate_context, stream);
4840 			dc_state_retain(dc->current_state);
4841 			success = true;
4842 		}
4843 		release_minimal_transition_state(
4844 				dc, intermediate_context, new_context, &policy);
4845 	}
4846 	return success;
4847 }
4848 
4849 static bool commit_minimal_transition_based_on_current_context(struct dc *dc,
4850 		struct dc_state *new_context, struct dc_stream_state *stream)
4851 {
4852 	bool success = false;
4853 	struct pipe_split_policy_backup policy;
4854 	struct dc_state *intermediate_context;
4855 	struct dc_state *old_current_state = dc->current_state;
4856 	struct dc_surface_update srf_updates[MAX_SURFACES] = {0};
4857 	int surface_count;
4858 
4859 	/*
4860 	 * Both current and new contexts share the same stream and plane state
4861 	 * pointers. When new context is validated, stream and planes get
4862 	 * populated with new updates such as new plane addresses. This makes
4863 	 * the current context no longer valid because stream and planes are
4864 	 * modified from the original. We backup current stream and plane states
4865 	 * into scratch space whenever we are populating new context. So we can
4866 	 * restore the original values back by calling the restore function now.
4867 	 * This restores back the original stream and plane states associated
4868 	 * with the current state.
4869 	 */
4870 	restore_planes_and_stream_state(&dc->scratch.current_state, stream);
4871 	dc_state_retain(old_current_state);
4872 	intermediate_context = create_minimal_transition_state(dc,
4873 			old_current_state, &policy);
4874 
4875 	if (intermediate_context) {
4876 		if (is_pipe_topology_transition_seamless_with_intermediate_step(
4877 				dc,
4878 				dc->current_state,
4879 				intermediate_context,
4880 				new_context)) {
4881 			DC_LOG_DC("commit minimal transition state: base = current state\n");
4882 			surface_count = initialize_empty_surface_updates(
4883 					stream, srf_updates);
4884 			commit_planes_for_stream(dc, srf_updates,
4885 					surface_count, stream, NULL,
4886 					UPDATE_TYPE_FULL, intermediate_context);
4887 			swap_and_release_current_context(
4888 					dc, intermediate_context, stream);
4889 			dc_state_retain(dc->current_state);
4890 			success = true;
4891 		}
4892 		release_minimal_transition_state(dc, intermediate_context,
4893 				old_current_state, &policy);
4894 	}
4895 	dc_state_release(old_current_state);
4896 	/*
4897 	 * Restore stream and plane states back to the values associated with
4898 	 * new context.
4899 	 */
4900 	restore_planes_and_stream_state(&dc->scratch.new_state, stream);
4901 	return success;
4902 }
4903 
4904 /**
4905  * commit_minimal_transition_state_in_dc_update - Commit a minimal state based
4906  * on current or new context
4907  *
4908  * @dc: DC structure, used to get the current state
4909  * @new_context: New context
4910  * @stream: Stream getting the update for the flip
4911  * @srf_updates: Surface updates
4912  * @surface_count: Number of surfaces
4913  *
4914  * The function takes in current state and new state and determine a minimal
4915  * transition state as the intermediate step which could make the transition
4916  * between current and new states seamless. If found, it will commit the minimal
4917  * transition state and update current state to this minimal transition state
4918  * and return true, if not, it will return false.
4919  *
4920  * Return:
4921  * Return True if the minimal transition succeeded, false otherwise
4922  */
4923 static bool commit_minimal_transition_state_in_dc_update(struct dc *dc,
4924 		struct dc_state *new_context,
4925 		struct dc_stream_state *stream,
4926 		struct dc_surface_update *srf_updates,
4927 		int surface_count)
4928 {
4929 	bool success = commit_minimal_transition_based_on_new_context(
4930 				dc, new_context, stream, NULL,
4931 				srf_updates, surface_count);
4932 	if (!success)
4933 		success = commit_minimal_transition_based_on_current_context(dc,
4934 				new_context, stream);
4935 	if (!success)
4936 		DC_LOG_ERROR("Fail to commit a seamless minimal transition state between current and new states.\nThis pipe topology update is non-seamless!\n");
4937 	return success;
4938 }
4939 
4940 /**
4941  * commit_minimal_transition_state - Create a transition pipe split state
4942  *
4943  * @dc: Used to get the current state status
4944  * @transition_base_context: New transition state
4945  *
4946  * In some specific configurations, such as pipe split on multi-display with
4947  * MPO and/or Dynamic ODM, removing a plane may cause unsupported pipe
4948  * programming when moving to new planes. To mitigate those types of problems,
4949  * this function adds a transition state that minimizes pipe usage before
4950  * programming the new configuration. When adding a new plane, the current
4951  * state requires the least pipes, so it is applied without splitting. When
4952  * removing a plane, the new state requires the least pipes, so it is applied
4953  * without splitting.
4954  *
4955  * Return:
4956  * Return false if something is wrong in the transition state.
4957  */
4958 static bool commit_minimal_transition_state(struct dc *dc,
4959 		struct dc_state *transition_base_context)
4960 {
4961 	struct dc_state *transition_context;
4962 	struct pipe_split_policy_backup policy;
4963 	enum dc_status ret = DC_ERROR_UNEXPECTED;
4964 	unsigned int i, j;
4965 	unsigned int pipe_in_use = 0;
4966 	bool subvp_in_use = false;
4967 	bool odm_in_use = false;
4968 
4969 	/* check current pipes in use*/
4970 	for (i = 0; i < dc->res_pool->pipe_count; i++) {
4971 		struct pipe_ctx *pipe = &transition_base_context->res_ctx.pipe_ctx[i];
4972 
4973 		if (pipe->plane_state)
4974 			pipe_in_use++;
4975 	}
4976 
4977 	/* If SubVP is enabled and we are adding or removing planes from any main subvp
4978 	 * pipe, we must use the minimal transition.
4979 	 */
4980 	for (i = 0; i < dc->res_pool->pipe_count; i++) {
4981 		struct pipe_ctx *pipe = &dc->current_state->res_ctx.pipe_ctx[i];
4982 
4983 		if (pipe->stream && dc_state_get_pipe_subvp_type(dc->current_state, pipe) == SUBVP_PHANTOM) {
4984 			subvp_in_use = true;
4985 			break;
4986 		}
4987 	}
4988 
4989 	/* If ODM is enabled and we are adding or removing planes from any ODM
4990 	 * pipe, we must use the minimal transition.
4991 	 */
4992 	for (i = 0; i < dc->res_pool->pipe_count; i++) {
4993 		struct pipe_ctx *pipe = &transition_base_context->res_ctx.pipe_ctx[i];
4994 
4995 		if (resource_is_pipe_type(pipe, OTG_MASTER)) {
4996 			odm_in_use = resource_get_odm_slice_count(pipe) > 1;
4997 			break;
4998 		}
4999 	}
5000 
5001 	/* When the OS add a new surface if we have been used all of pipes with odm combine
5002 	 * and mpc split feature, it need use commit_minimal_transition_state to transition safely.
5003 	 * After OS exit MPO, it will back to use odm and mpc split with all of pipes, we need
5004 	 * call it again. Otherwise return true to skip.
5005 	 *
5006 	 * Reduce the scenarios to use dc_commit_state_no_check in the stage of flip. Especially
5007 	 * enter/exit MPO when DCN still have enough resources.
5008 	 */
5009 	if (pipe_in_use != dc->res_pool->pipe_count && !subvp_in_use && !odm_in_use)
5010 		return true;
5011 
5012 	DC_LOG_DC("%s base = %s state, reason = %s\n", __func__,
5013 			dc->current_state == transition_base_context ? "current" : "new",
5014 			subvp_in_use ? "Subvp In Use" :
5015 			odm_in_use ? "ODM in Use" :
5016 			dc->debug.pipe_split_policy != MPC_SPLIT_AVOID ? "MPC in Use" :
5017 			"Unknown");
5018 
5019 	dc_state_retain(transition_base_context);
5020 	transition_context = create_minimal_transition_state(dc,
5021 			transition_base_context, &policy);
5022 	if (transition_context) {
5023 		ret = dc_commit_state_no_check(dc, transition_context);
5024 		release_minimal_transition_state(dc, transition_context, transition_base_context, &policy);
5025 	}
5026 	dc_state_release(transition_base_context);
5027 
5028 	if (ret != DC_OK) {
5029 		/* this should never happen */
5030 		BREAK_TO_DEBUGGER();
5031 		return false;
5032 	}
5033 
5034 	/* force full surface update */
5035 	for (i = 0; i < dc->current_state->stream_count; i++) {
5036 		for (j = 0; j < dc->current_state->stream_status[i].plane_count; j++) {
5037 			dc->current_state->stream_status[i].plane_states[j]->update_flags.raw = 0xFFFFFFFF;
5038 		}
5039 	}
5040 
5041 	return true;
5042 }
5043 
5044 void populate_fast_updates(struct dc_fast_update *fast_update,
5045 		struct dc_surface_update *srf_updates,
5046 		int surface_count,
5047 		struct dc_stream_update *stream_update)
5048 {
5049 	int i = 0;
5050 
5051 	if (stream_update) {
5052 		fast_update[0].out_transfer_func = stream_update->out_transfer_func;
5053 		fast_update[0].output_csc_transform = stream_update->output_csc_transform;
5054 	} else {
5055 		fast_update[0].out_transfer_func = NULL;
5056 		fast_update[0].output_csc_transform = NULL;
5057 	}
5058 
5059 	for (i = 0; i < surface_count; i++) {
5060 		fast_update[i].flip_addr = srf_updates[i].flip_addr;
5061 		fast_update[i].gamma = srf_updates[i].gamma;
5062 		fast_update[i].gamut_remap_matrix = srf_updates[i].gamut_remap_matrix;
5063 		fast_update[i].input_csc_color_matrix = srf_updates[i].input_csc_color_matrix;
5064 		fast_update[i].coeff_reduction_factor = srf_updates[i].coeff_reduction_factor;
5065 		fast_update[i].cursor_csc_color_matrix = srf_updates[i].cursor_csc_color_matrix;
5066 	}
5067 }
5068 
5069 static bool fast_updates_exist(const struct dc_fast_update *fast_update, int surface_count)
5070 {
5071 	int i;
5072 
5073 	if (fast_update[0].out_transfer_func ||
5074 		fast_update[0].output_csc_transform)
5075 		return true;
5076 
5077 	for (i = 0; i < surface_count; i++) {
5078 		if (fast_update[i].flip_addr ||
5079 				fast_update[i].gamma ||
5080 				fast_update[i].gamut_remap_matrix ||
5081 				fast_update[i].input_csc_color_matrix ||
5082 				fast_update[i].cursor_csc_color_matrix ||
5083 				fast_update[i].coeff_reduction_factor)
5084 			return true;
5085 	}
5086 
5087 	return false;
5088 }
5089 
5090 bool fast_nonaddr_updates_exist(struct dc_fast_update *fast_update, int surface_count)
5091 {
5092 	int i;
5093 
5094 	if (fast_update[0].out_transfer_func ||
5095 		fast_update[0].output_csc_transform)
5096 		return true;
5097 
5098 	for (i = 0; i < surface_count; i++) {
5099 		if (fast_update[i].input_csc_color_matrix ||
5100 				fast_update[i].gamma ||
5101 				fast_update[i].gamut_remap_matrix ||
5102 				fast_update[i].coeff_reduction_factor ||
5103 				fast_update[i].cursor_csc_color_matrix)
5104 			return true;
5105 	}
5106 
5107 	return false;
5108 }
5109 
5110 static bool full_update_required_weak(
5111 		const struct dc *dc,
5112 		const struct dc_surface_update *srf_updates,
5113 		int surface_count,
5114 		const struct dc_stream_update *stream_update,
5115 		const struct dc_stream_state *stream)
5116 {
5117 	const struct dc_state *context = dc->current_state;
5118 	if (srf_updates)
5119 		for (int i = 0; i < surface_count; i++)
5120 			if (!is_surface_in_context(context, srf_updates[i].surface))
5121 				return true;
5122 
5123 	if (stream) {
5124 		const struct dc_stream_status *stream_status = dc_stream_get_status_const(stream);
5125 		if (stream_status == NULL || stream_status->plane_count != surface_count)
5126 			return true;
5127 	}
5128 	if (dc->idle_optimizations_allowed)
5129 		return true;
5130 
5131 	if (dc_can_clear_cursor_limit(dc))
5132 		return true;
5133 
5134 	return false;
5135 }
5136 
5137 static bool full_update_required(
5138 		const struct dc *dc,
5139 		const struct dc_surface_update *srf_updates,
5140 		int surface_count,
5141 		const struct dc_stream_update *stream_update,
5142 		const struct dc_stream_state *stream)
5143 {
5144 	if (full_update_required_weak(dc, srf_updates, surface_count, stream_update, stream))
5145 		return true;
5146 
5147 	for (int i = 0; i < surface_count; i++) {
5148 		if (srf_updates &&
5149 				(srf_updates[i].plane_info ||
5150 				srf_updates[i].scaling_info ||
5151 				(srf_updates[i].hdr_mult.value &&
5152 				srf_updates[i].hdr_mult.value != srf_updates->surface->hdr_mult.value) ||
5153 				(srf_updates[i].sdr_white_level_nits &&
5154 				srf_updates[i].sdr_white_level_nits != srf_updates->surface->sdr_white_level_nits) ||
5155 				srf_updates[i].in_transfer_func ||
5156 				srf_updates[i].func_shaper ||
5157 				srf_updates[i].lut3d_func ||
5158 				srf_updates[i].surface->force_full_update ||
5159 				(srf_updates[i].flip_addr &&
5160 				srf_updates[i].flip_addr->address.tmz_surface != srf_updates[i].surface->address.tmz_surface) ||
5161 				(srf_updates[i].cm2_params &&
5162 				 (srf_updates[i].cm2_params->component_settings.shaper_3dlut_setting != srf_updates[i].surface->mcm_shaper_3dlut_setting ||
5163 				  srf_updates[i].cm2_params->component_settings.lut1d_enable != srf_updates[i].surface->mcm_lut1d_enable))))
5164 			return true;
5165 	}
5166 
5167 	if (stream_update &&
5168 			(((stream_update->src.height != 0 && stream_update->src.width != 0) ||
5169 			(stream_update->dst.height != 0 && stream_update->dst.width != 0) ||
5170 			stream_update->integer_scaling_update) ||
5171 			stream_update->hdr_static_metadata ||
5172 			stream_update->abm_level ||
5173 			stream_update->periodic_interrupt ||
5174 			stream_update->vrr_infopacket ||
5175 			stream_update->vsc_infopacket ||
5176 			stream_update->vsp_infopacket ||
5177 			stream_update->hfvsif_infopacket ||
5178 			stream_update->vtem_infopacket ||
5179 			stream_update->adaptive_sync_infopacket ||
5180 			stream_update->avi_infopacket ||
5181 			stream_update->dpms_off ||
5182 			stream_update->allow_freesync ||
5183 			stream_update->vrr_active_variable ||
5184 			stream_update->vrr_active_fixed ||
5185 			stream_update->gamut_remap ||
5186 			stream_update->output_color_space ||
5187 			stream_update->dither_option ||
5188 			stream_update->wb_update ||
5189 			stream_update->dsc_config ||
5190 			stream_update->mst_bw_update ||
5191 			stream_update->func_shaper ||
5192 			stream_update->lut3d_func ||
5193 			stream_update->pending_test_pattern ||
5194 			stream_update->crtc_timing_adjust ||
5195 			stream_update->scaler_sharpener_update ||
5196 			stream_update->hw_cursor_req))
5197 		return true;
5198 
5199 	return false;
5200 }
5201 
5202 static bool fast_update_only(
5203 		const struct dc *dc,
5204 		const struct dc_fast_update *fast_update,
5205 		const struct dc_surface_update *srf_updates,
5206 		int surface_count,
5207 		const struct dc_stream_update *stream_update,
5208 		const struct dc_stream_state *stream)
5209 {
5210 	return fast_updates_exist(fast_update, surface_count)
5211 			&& !full_update_required(dc, srf_updates, surface_count, stream_update, stream);
5212 }
5213 
5214 static bool update_planes_and_stream_v2(struct dc *dc,
5215 		struct dc_surface_update *srf_updates, int surface_count,
5216 		struct dc_stream_state *stream,
5217 		struct dc_stream_update *stream_update)
5218 {
5219 	struct dc_state *context;
5220 	enum surface_update_type update_type;
5221 	struct dc_fast_update fast_update[MAX_SURFACES] = {0};
5222 
5223 	/* In cases where MPO and split or ODM are used transitions can
5224 	 * cause underflow. Apply stream configuration with minimal pipe
5225 	 * split first to avoid unsupported transitions for active pipes.
5226 	 */
5227 	bool force_minimal_pipe_splitting = 0;
5228 	bool is_plane_addition = 0;
5229 	bool is_fast_update_only;
5230 
5231 	populate_fast_updates(fast_update, srf_updates, surface_count, stream_update);
5232 	is_fast_update_only = fast_update_only(dc, fast_update, srf_updates,
5233 			surface_count, stream_update, stream);
5234 	force_minimal_pipe_splitting = could_mpcc_tree_change_for_active_pipes(
5235 			dc,
5236 			stream,
5237 			srf_updates,
5238 			surface_count,
5239 			&is_plane_addition);
5240 
5241 	/* on plane addition, minimal state is the current one */
5242 	if (force_minimal_pipe_splitting && is_plane_addition &&
5243 		!commit_minimal_transition_state(dc, dc->current_state))
5244 		return false;
5245 
5246 	if (!update_planes_and_stream_state(
5247 			dc,
5248 			srf_updates,
5249 			surface_count,
5250 			stream,
5251 			stream_update,
5252 			&update_type,
5253 			&context))
5254 		return false;
5255 
5256 	/* on plane removal, minimal state is the new one */
5257 	if (force_minimal_pipe_splitting && !is_plane_addition) {
5258 		if (!commit_minimal_transition_state(dc, context)) {
5259 			dc_state_release(context);
5260 			return false;
5261 		}
5262 		update_type = UPDATE_TYPE_FULL;
5263 	}
5264 
5265 	if (dc->hwss.is_pipe_topology_transition_seamless &&
5266 			!dc->hwss.is_pipe_topology_transition_seamless(
5267 					dc, dc->current_state, context))
5268 		commit_minimal_transition_state_in_dc_update(dc, context, stream,
5269 				srf_updates, surface_count);
5270 
5271 	if (is_fast_update_only && !dc->check_config.enable_legacy_fast_update) {
5272 		commit_planes_for_stream_fast(dc,
5273 				srf_updates,
5274 				surface_count,
5275 				stream,
5276 				stream_update,
5277 				update_type,
5278 				context);
5279 	} else {
5280 		if (!stream_update &&
5281 				dc->hwss.is_pipe_topology_transition_seamless &&
5282 				!dc->hwss.is_pipe_topology_transition_seamless(
5283 						dc, dc->current_state, context)) {
5284 			DC_LOG_ERROR("performing non-seamless pipe topology transition with surface only update!\n");
5285 			BREAK_TO_DEBUGGER();
5286 		}
5287 		commit_planes_for_stream(
5288 				dc,
5289 				srf_updates,
5290 				surface_count,
5291 				stream,
5292 				stream_update,
5293 				update_type,
5294 				context);
5295 	}
5296 	if (dc->current_state != context)
5297 		swap_and_release_current_context(dc, context, stream);
5298 	return true;
5299 }
5300 
5301 static void commit_planes_and_stream_update_on_current_context(struct dc *dc,
5302 		struct dc_surface_update *srf_updates, int surface_count,
5303 		struct dc_stream_state *stream,
5304 		struct dc_stream_update *stream_update,
5305 		enum surface_update_type update_type)
5306 {
5307 	struct dc_fast_update fast_update[MAX_SURFACES] = {0};
5308 
5309 	ASSERT(update_type < UPDATE_TYPE_FULL);
5310 	populate_fast_updates(fast_update, srf_updates, surface_count,
5311 			stream_update);
5312 	if (fast_update_only(dc, fast_update, srf_updates, surface_count,
5313 			stream_update, stream) &&
5314 			!dc->check_config.enable_legacy_fast_update)
5315 		commit_planes_for_stream_fast(dc,
5316 				srf_updates,
5317 				surface_count,
5318 				stream,
5319 				stream_update,
5320 				update_type,
5321 				dc->current_state);
5322 	else
5323 		commit_planes_for_stream(
5324 				dc,
5325 				srf_updates,
5326 				surface_count,
5327 				stream,
5328 				stream_update,
5329 				update_type,
5330 				dc->current_state);
5331 }
5332 
5333 static void commit_planes_and_stream_update_with_new_context(struct dc *dc,
5334 		struct dc_surface_update *srf_updates, int surface_count,
5335 		struct dc_stream_state *stream,
5336 		struct dc_stream_update *stream_update,
5337 		enum surface_update_type update_type,
5338 		struct dc_state *new_context)
5339 {
5340 	bool skip_new_context = false;
5341 	ASSERT(update_type >= UPDATE_TYPE_FULL);
5342 	/*
5343 	 * It is required by the feature design that all pipe topologies
5344 	 * using extra free pipes for power saving purposes such as
5345 	 * dynamic ODM or SubVp shall only be enabled when it can be
5346 	 * transitioned seamlessly to AND from its minimal transition
5347 	 * state. A minimal transition state is defined as the same dc
5348 	 * state but with all power saving features disabled. So it uses
5349 	 * the minimum pipe topology. When we can't seamlessly
5350 	 * transition from state A to state B, we will insert the
5351 	 * minimal transition state A' or B' in between so seamless
5352 	 * transition between A and B can be made possible.
5353 	 *
5354 	 * To optimize for the time it takes to execute flips,
5355 	 * the transition from the minimal state to the final state is
5356 	 * deferred until a steady state (no more transitions) is reached.
5357 	 */
5358 	if (!dc->hwss.is_pipe_topology_transition_seamless(dc, dc->current_state, new_context)) {
5359 		if (!dc->debug.disable_deferred_minimal_transitions) {
5360 			dc->check_config.deferred_transition_state = true;
5361 			dc->check_config.transition_countdown_to_steady_state =
5362 					dc->debug.num_fast_flips_to_steady_state_override ?
5363 					dc->debug.num_fast_flips_to_steady_state_override :
5364 					NUM_FAST_FLIPS_TO_STEADY_STATE;
5365 
5366 			if (commit_minimal_transition_based_on_new_context(dc, new_context, stream, stream_update,
5367 					srf_updates, surface_count)) {
5368 				skip_new_context = true;
5369 				dc_state_release(new_context);
5370 				new_context = dc->current_state;
5371 			} else {
5372 				/*
5373 				 * In this case a new mpo plane is being enabled on pipes that were
5374 				 * previously in use, and the surface update to the existing plane
5375 				 * includes an alpha box where the new plane will be, so the update
5376 				 * from minimal to final cannot be deferred as the alpha box would
5377 				 * be visible to the user
5378 				 */
5379 				commit_minimal_transition_based_on_current_context(dc, new_context, stream);
5380 			}
5381 		} else {
5382 			commit_minimal_transition_state_in_dc_update(dc, new_context, stream,
5383 					srf_updates, surface_count);
5384 		}
5385 	} else if (dc->check_config.deferred_transition_state) {
5386 		/* reset countdown as steady state not reached */
5387 		dc->check_config.transition_countdown_to_steady_state =
5388 				dc->debug.num_fast_flips_to_steady_state_override ?
5389 				dc->debug.num_fast_flips_to_steady_state_override :
5390 				NUM_FAST_FLIPS_TO_STEADY_STATE;
5391 	}
5392 
5393 	if (!skip_new_context) {
5394 		commit_planes_for_stream(dc, srf_updates, surface_count, stream, stream_update, update_type, new_context);
5395 		swap_and_release_current_context(dc, new_context, stream);
5396 	}
5397 }
5398 
5399 static bool update_planes_and_stream_v3(struct dc *dc,
5400 		struct dc_surface_update *srf_updates, int surface_count,
5401 		struct dc_stream_state *stream,
5402 		struct dc_stream_update *stream_update)
5403 {
5404 	struct dc_state *new_context;
5405 	enum surface_update_type update_type;
5406 
5407 	/*
5408 	 * When this function returns true and new_context is not equal to
5409 	 * current state, the function allocates and validates a new dc state
5410 	 * and assigns it to new_context. The function expects that the caller
5411 	 * is responsible to free this memory when new_context is no longer
5412 	 * used. We swap current with new context and free current instead. So
5413 	 * new_context's memory will live until the next full update after it is
5414 	 * replaced by a newer context. Refer to the use of
5415 	 * swap_and_free_current_context below.
5416 	 */
5417 	if (!update_planes_and_stream_state(dc, srf_updates, surface_count,
5418 				stream, stream_update, &update_type,
5419 				&new_context))
5420 		return false;
5421 
5422 	if (new_context == dc->current_state) {
5423 		commit_planes_and_stream_update_on_current_context(dc,
5424 				srf_updates, surface_count, stream,
5425 				stream_update, update_type);
5426 
5427 		if (dc->check_config.transition_countdown_to_steady_state)
5428 			dc->check_config.transition_countdown_to_steady_state--;
5429 	} else {
5430 		commit_planes_and_stream_update_with_new_context(dc,
5431 				srf_updates, surface_count, stream,
5432 				stream_update, update_type, new_context);
5433 	}
5434 
5435 	return true;
5436 }
5437 
5438 static void clear_update_flags(struct dc_surface_update *srf_updates,
5439 	int surface_count, struct dc_stream_state *stream)
5440 {
5441 	int i;
5442 
5443 	if (stream)
5444 		stream->update_flags.raw = 0;
5445 
5446 	for (i = 0; i < surface_count; i++)
5447 		if (srf_updates[i].surface)
5448 			srf_updates[i].surface->update_flags.raw = 0;
5449 }
5450 
5451 bool dc_update_planes_and_stream(struct dc *dc,
5452 		struct dc_surface_update *srf_updates, int surface_count,
5453 		struct dc_stream_state *stream,
5454 		struct dc_stream_update *stream_update)
5455 {
5456 	struct dc_update_scratch_space *scratch = dc_update_planes_and_stream_init(
5457 			dc,
5458 			srf_updates,
5459 			surface_count,
5460 			stream,
5461 			stream_update
5462 	);
5463 	bool more = true;
5464 
5465 	while (more) {
5466 		if (!dc_update_planes_and_stream_prepare(scratch))
5467 			return false;
5468 
5469 		dc_update_planes_and_stream_execute(scratch);
5470 		more = dc_update_planes_and_stream_cleanup(scratch);
5471 	}
5472 	return true;
5473 }
5474 
5475 void dc_commit_updates_for_stream(struct dc *dc,
5476 		struct dc_surface_update *srf_updates,
5477 		int surface_count,
5478 		struct dc_stream_state *stream,
5479 		struct dc_stream_update *stream_update,
5480 		struct dc_state *state)
5481 {
5482 	bool ret = false;
5483 
5484 	dc_exit_ips_for_hw_access(dc);
5485 	/* TODO: Since change commit sequence can have a huge impact,
5486 	 * we decided to only enable it for DCN3x. However, as soon as
5487 	 * we get more confident about this change we'll need to enable
5488 	 * the new sequence for all ASICs.
5489 	 */
5490 	if (dc->ctx->dce_version >= DCN_VERSION_4_01) {
5491 		ret = update_planes_and_stream_v3(dc, srf_updates, surface_count,
5492 				stream, stream_update);
5493 	} else {
5494 		ret = update_planes_and_stream_v2(dc, srf_updates, surface_count,
5495 				stream, stream_update);
5496 	}
5497 
5498 	if (ret && dc->ctx->dce_version >= DCN_VERSION_3_2)
5499 		clear_update_flags(srf_updates, surface_count, stream);
5500 }
5501 
5502 uint8_t dc_get_current_stream_count(struct dc *dc)
5503 {
5504 	return dc->current_state->stream_count;
5505 }
5506 
5507 struct dc_stream_state *dc_get_stream_at_index(struct dc *dc, uint8_t i)
5508 {
5509 	if (i < dc->current_state->stream_count)
5510 		return dc->current_state->streams[i];
5511 	return NULL;
5512 }
5513 
5514 enum dc_irq_source dc_interrupt_to_irq_source(
5515 		struct dc *dc,
5516 		uint32_t src_id,
5517 		uint32_t ext_id)
5518 {
5519 	return dal_irq_service_to_irq_source(dc->res_pool->irqs, src_id, ext_id);
5520 }
5521 
5522 /*
5523  * dc_interrupt_set() - Enable/disable an AMD hw interrupt source
5524  */
5525 bool dc_interrupt_set(struct dc *dc, enum dc_irq_source src, bool enable)
5526 {
5527 
5528 	if (dc == NULL)
5529 		return false;
5530 
5531 	return dal_irq_service_set(dc->res_pool->irqs, src, enable);
5532 }
5533 
5534 void dc_interrupt_ack(struct dc *dc, enum dc_irq_source src)
5535 {
5536 	dal_irq_service_ack(dc->res_pool->irqs, src);
5537 }
5538 
5539 void dc_power_down_on_boot(struct dc *dc)
5540 {
5541 	if (dc->ctx->dce_environment != DCE_ENV_VIRTUAL_HW &&
5542 	    dc->hwss.power_down_on_boot) {
5543 		if (dc->caps.ips_support)
5544 			dc_exit_ips_for_hw_access(dc);
5545 		dc->hwss.power_down_on_boot(dc);
5546 	}
5547 }
5548 
5549 void dc_set_power_state(struct dc *dc, enum dc_acpi_cm_power_state power_state)
5550 {
5551 	if (!dc->current_state)
5552 		return;
5553 
5554 	switch (power_state) {
5555 	case DC_ACPI_CM_POWER_STATE_D0:
5556 		dc_state_construct(dc, dc->current_state);
5557 
5558 		dc_exit_ips_for_hw_access(dc);
5559 
5560 		dc_z10_restore(dc);
5561 
5562 		dc_dmub_srv_notify_fw_dc_power_state(dc->ctx->dmub_srv, power_state);
5563 
5564 		dc->hwss.init_hw(dc);
5565 
5566 		if (dc->hwss.init_sys_ctx != NULL &&
5567 			dc->vm_pa_config.valid) {
5568 			dc->hwss.init_sys_ctx(dc->hwseq, dc, &dc->vm_pa_config);
5569 		}
5570 		break;
5571 	case DC_ACPI_CM_POWER_STATE_D3:
5572 		if (dc->caps.ips_support)
5573 			dc_dmub_srv_notify_fw_dc_power_state(dc->ctx->dmub_srv, DC_ACPI_CM_POWER_STATE_D3);
5574 
5575 		if (dc->caps.ips_v2_support) {
5576 			if (dc->clk_mgr->funcs->set_low_power_state)
5577 				dc->clk_mgr->funcs->set_low_power_state(dc->clk_mgr);
5578 		}
5579 		break;
5580 	default:
5581 		ASSERT(dc->current_state->stream_count == 0);
5582 		dc_dmub_srv_notify_fw_dc_power_state(dc->ctx->dmub_srv, power_state);
5583 
5584 		dc_state_destruct(dc->current_state);
5585 
5586 		break;
5587 	}
5588 }
5589 
5590 void dc_resume(struct dc *dc)
5591 {
5592 	uint32_t i;
5593 
5594 	for (i = 0; i < dc->link_count; i++)
5595 		dc->link_srv->resume(dc->links[i]);
5596 }
5597 
5598 bool dc_is_dmcu_initialized(struct dc *dc)
5599 {
5600 	struct dmcu *dmcu = dc->res_pool->dmcu;
5601 
5602 	if (dmcu)
5603 		return dmcu->funcs->is_dmcu_initialized(dmcu);
5604 	return false;
5605 }
5606 
5607 enum dc_status dc_set_clock(struct dc *dc, enum dc_clock_type clock_type, uint32_t clk_khz, uint32_t stepping)
5608 {
5609 	if (dc->hwss.set_clock)
5610 		return dc->hwss.set_clock(dc, clock_type, clk_khz, stepping);
5611 	return DC_ERROR_UNEXPECTED;
5612 }
5613 void dc_get_clock(struct dc *dc, enum dc_clock_type clock_type, struct dc_clock_config *clock_cfg)
5614 {
5615 	if (dc->hwss.get_clock)
5616 		dc->hwss.get_clock(dc, clock_type, clock_cfg);
5617 }
5618 
5619 /* enable/disable eDP PSR without specify stream for eDP */
5620 bool dc_set_psr_allow_active(struct dc *dc, bool enable)
5621 {
5622 	int i;
5623 	bool allow_active;
5624 
5625 	for (i = 0; i < dc->current_state->stream_count ; i++) {
5626 		struct dc_link *link;
5627 		struct dc_stream_state *stream = dc->current_state->streams[i];
5628 
5629 		link = stream->link;
5630 		if (!link)
5631 			continue;
5632 
5633 		if (link->psr_settings.psr_feature_enabled) {
5634 			if (enable && !link->psr_settings.psr_allow_active) {
5635 				allow_active = true;
5636 				if (!dc_link_set_psr_allow_active(link, &allow_active, false, false, NULL))
5637 					return false;
5638 			} else if (!enable && link->psr_settings.psr_allow_active) {
5639 				allow_active = false;
5640 				if (!dc_link_set_psr_allow_active(link, &allow_active, true, false, NULL))
5641 					return false;
5642 			}
5643 		}
5644 	}
5645 
5646 	return true;
5647 }
5648 
5649 /* enable/disable eDP Replay without specify stream for eDP */
5650 bool dc_set_replay_allow_active(struct dc *dc, bool active)
5651 {
5652 	int i;
5653 	bool allow_active;
5654 
5655 	for (i = 0; i < dc->current_state->stream_count; i++) {
5656 		struct dc_link *link;
5657 		struct dc_stream_state *stream = dc->current_state->streams[i];
5658 
5659 		link = stream->link;
5660 		if (!link)
5661 			continue;
5662 
5663 		if (link->replay_settings.replay_feature_enabled) {
5664 			if (active && !link->replay_settings.replay_allow_active) {
5665 				allow_active = true;
5666 				if (!dc_link_set_replay_allow_active(link, &allow_active,
5667 					false, false, NULL))
5668 					return false;
5669 			} else if (!active && link->replay_settings.replay_allow_active) {
5670 				allow_active = false;
5671 				if (!dc_link_set_replay_allow_active(link, &allow_active,
5672 					true, false, NULL))
5673 					return false;
5674 			}
5675 		}
5676 	}
5677 
5678 	return true;
5679 }
5680 
5681 /* set IPS disable state */
5682 bool dc_set_ips_disable(struct dc *dc, unsigned int disable_ips)
5683 {
5684 	dc_exit_ips_for_hw_access(dc);
5685 
5686 	dc->config.disable_ips = disable_ips;
5687 
5688 	return true;
5689 }
5690 
5691 void dc_allow_idle_optimizations_internal(struct dc *dc, bool allow, char const *caller_name)
5692 {
5693 	int idle_fclk_khz = 0, idle_dramclk_khz = 0, i = 0;
5694 	enum mall_stream_type subvp_pipe_type[MAX_PIPES] = {0};
5695 	struct pipe_ctx *pipe = NULL;
5696 	struct dc_state *context = dc->current_state;
5697 
5698 	if (dc->debug.disable_idle_power_optimizations) {
5699 		DC_LOG_DEBUG("%s: disabled\n", __func__);
5700 		return;
5701 	}
5702 
5703 	if (allow != dc->idle_optimizations_allowed)
5704 		DC_LOG_IPS("%s: allow_idle old=%d new=%d (caller=%s)\n", __func__,
5705 			   dc->idle_optimizations_allowed, allow, caller_name);
5706 
5707 	if (dc->caps.ips_support && (dc->config.disable_ips == DMUB_IPS_DISABLE_ALL))
5708 		return;
5709 
5710 	if (dc->clk_mgr != NULL && dc->clk_mgr->funcs->is_smu_present)
5711 		if (!dc->clk_mgr->funcs->is_smu_present(dc->clk_mgr))
5712 			return;
5713 
5714 	if (allow == dc->idle_optimizations_allowed)
5715 		return;
5716 
5717 	if (dc->hwss.apply_idle_power_optimizations && dc->clk_mgr != NULL &&
5718 	    dc->hwss.apply_idle_power_optimizations(dc, allow)) {
5719 		dc->idle_optimizations_allowed = allow;
5720 		DC_LOG_DEBUG("%s: %s\n", __func__, allow ? "enabled" : "disabled");
5721 	}
5722 
5723 	// log idle clocks and sub vp pipe types at idle optimization time
5724 	if (dc->clk_mgr != NULL && dc->clk_mgr->funcs->get_hard_min_fclk)
5725 		idle_fclk_khz = dc->clk_mgr->funcs->get_hard_min_fclk(dc->clk_mgr);
5726 
5727 	if (dc->clk_mgr != NULL && dc->clk_mgr->funcs->get_hard_min_memclk)
5728 		idle_dramclk_khz = dc->clk_mgr->funcs->get_hard_min_memclk(dc->clk_mgr);
5729 
5730 	if (dc->res_pool && context) {
5731 		for (i = 0; i < dc->res_pool->pipe_count; i++) {
5732 			pipe = &context->res_ctx.pipe_ctx[i];
5733 			subvp_pipe_type[i] = dc_state_get_pipe_subvp_type(context, pipe);
5734 		}
5735 	}
5736 	if (!dc->caps.is_apu)
5737 		DC_LOG_DC("%s: allow_idle=%d\n HardMinUClk_Khz=%d HardMinDramclk_Khz=%d\n Pipe_0=%d Pipe_1=%d Pipe_2=%d Pipe_3=%d Pipe_4=%d Pipe_5=%d (caller=%s)\n",
5738 			__func__, allow, idle_fclk_khz, idle_dramclk_khz, subvp_pipe_type[0], subvp_pipe_type[1], subvp_pipe_type[2],
5739 			subvp_pipe_type[3], subvp_pipe_type[4], subvp_pipe_type[5], caller_name);
5740 
5741 }
5742 
5743 void dc_exit_ips_for_hw_access_internal(struct dc *dc, const char *caller_name)
5744 {
5745 	if (dc->caps.ips_support)
5746 		dc_allow_idle_optimizations_internal(dc, false, caller_name);
5747 }
5748 
5749 bool dc_dmub_is_ips_idle_state(struct dc *dc)
5750 {
5751 	if (dc->debug.disable_idle_power_optimizations)
5752 		return false;
5753 
5754 	if (!dc->caps.ips_support || (dc->config.disable_ips == DMUB_IPS_DISABLE_ALL))
5755 		return false;
5756 
5757 	if (!dc->ctx->dmub_srv)
5758 		return false;
5759 
5760 	return dc->ctx->dmub_srv->idle_allowed;
5761 }
5762 
5763 /* set min and max memory clock to lowest and highest DPM level, respectively */
5764 void dc_unlock_memory_clock_frequency(struct dc *dc)
5765 {
5766 	if (dc->clk_mgr->funcs->set_hard_min_memclk)
5767 		dc->clk_mgr->funcs->set_hard_min_memclk(dc->clk_mgr, false);
5768 
5769 	if (dc->clk_mgr->funcs->set_hard_max_memclk)
5770 		dc->clk_mgr->funcs->set_hard_max_memclk(dc->clk_mgr);
5771 }
5772 
5773 /* set min memory clock to the min required for current mode, max to maxDPM */
5774 void dc_lock_memory_clock_frequency(struct dc *dc)
5775 {
5776 	if (dc->clk_mgr->funcs->get_memclk_states_from_smu)
5777 		dc->clk_mgr->funcs->get_memclk_states_from_smu(dc->clk_mgr);
5778 
5779 	if (dc->clk_mgr->funcs->set_hard_min_memclk)
5780 		dc->clk_mgr->funcs->set_hard_min_memclk(dc->clk_mgr, true);
5781 
5782 	if (dc->clk_mgr->funcs->set_hard_max_memclk)
5783 		dc->clk_mgr->funcs->set_hard_max_memclk(dc->clk_mgr);
5784 }
5785 
5786 static void blank_and_force_memclk(struct dc *dc, bool apply, unsigned int memclk_mhz)
5787 {
5788 	struct dc_state *context = dc->current_state;
5789 	struct hubp *hubp;
5790 	struct pipe_ctx *pipe;
5791 	int i;
5792 
5793 	for (i = 0; i < dc->res_pool->pipe_count; i++) {
5794 		pipe = &context->res_ctx.pipe_ctx[i];
5795 
5796 		if (pipe->stream != NULL) {
5797 			dc->hwss.disable_pixel_data(dc, pipe, true);
5798 
5799 			// wait for double buffer
5800 			pipe->stream_res.tg->funcs->wait_for_state(pipe->stream_res.tg, CRTC_STATE_VACTIVE);
5801 			pipe->stream_res.tg->funcs->wait_for_state(pipe->stream_res.tg, CRTC_STATE_VBLANK);
5802 			pipe->stream_res.tg->funcs->wait_for_state(pipe->stream_res.tg, CRTC_STATE_VACTIVE);
5803 
5804 			hubp = pipe->plane_res.hubp;
5805 			hubp->funcs->set_blank_regs(hubp, true);
5806 		}
5807 	}
5808 	if (dc->clk_mgr->funcs->set_max_memclk)
5809 		dc->clk_mgr->funcs->set_max_memclk(dc->clk_mgr, memclk_mhz);
5810 	if (dc->clk_mgr->funcs->set_min_memclk)
5811 		dc->clk_mgr->funcs->set_min_memclk(dc->clk_mgr, memclk_mhz);
5812 
5813 	for (i = 0; i < dc->res_pool->pipe_count; i++) {
5814 		pipe = &context->res_ctx.pipe_ctx[i];
5815 
5816 		if (pipe->stream != NULL) {
5817 			dc->hwss.disable_pixel_data(dc, pipe, false);
5818 
5819 			hubp = pipe->plane_res.hubp;
5820 			hubp->funcs->set_blank_regs(hubp, false);
5821 		}
5822 	}
5823 }
5824 
5825 
5826 /**
5827  * dc_enable_dcmode_clk_limit() - lower clocks in dc (battery) mode
5828  * @dc: pointer to dc of the dm calling this
5829  * @enable: True = transition to DC mode, false = transition back to AC mode
5830  *
5831  * Some SoCs define additional clock limits when in DC mode, DM should
5832  * invoke this function when the platform undergoes a power source transition
5833  * so DC can apply/unapply the limit. This interface may be disruptive to
5834  * the onscreen content.
5835  *
5836  * Context: Triggered by OS through DM interface, or manually by escape calls.
5837  * Need to hold a dclock when doing so.
5838  *
5839  * Return: none (void function)
5840  *
5841  */
5842 void dc_enable_dcmode_clk_limit(struct dc *dc, bool enable)
5843 {
5844 	unsigned int softMax = 0, maxDPM = 0, funcMin = 0, i;
5845 	bool p_state_change_support;
5846 
5847 	if (!dc->config.dc_mode_clk_limit_support)
5848 		return;
5849 
5850 	softMax = dc->clk_mgr->bw_params->dc_mode_softmax_memclk;
5851 	for (i = 0; i < dc->clk_mgr->bw_params->clk_table.num_entries; i++) {
5852 		if (dc->clk_mgr->bw_params->clk_table.entries[i].memclk_mhz > maxDPM)
5853 			maxDPM = dc->clk_mgr->bw_params->clk_table.entries[i].memclk_mhz;
5854 	}
5855 	funcMin = (dc->clk_mgr->clks.dramclk_khz + 999) / 1000;
5856 	p_state_change_support = dc->clk_mgr->clks.p_state_change_support;
5857 
5858 	if (enable && !dc->clk_mgr->dc_mode_softmax_enabled) {
5859 		if (p_state_change_support) {
5860 			if (funcMin <= softMax && dc->clk_mgr->funcs->set_max_memclk)
5861 				dc->clk_mgr->funcs->set_max_memclk(dc->clk_mgr, softMax);
5862 			// else: No-Op
5863 		} else {
5864 			if (funcMin <= softMax)
5865 				blank_and_force_memclk(dc, true, softMax);
5866 			// else: No-Op
5867 		}
5868 	} else if (!enable && dc->clk_mgr->dc_mode_softmax_enabled) {
5869 		if (p_state_change_support) {
5870 			if (funcMin <= softMax && dc->clk_mgr->funcs->set_max_memclk)
5871 				dc->clk_mgr->funcs->set_max_memclk(dc->clk_mgr, maxDPM);
5872 			// else: No-Op
5873 		} else {
5874 			if (funcMin <= softMax)
5875 				blank_and_force_memclk(dc, true, maxDPM);
5876 			// else: No-Op
5877 		}
5878 	}
5879 	dc->clk_mgr->dc_mode_softmax_enabled = enable;
5880 }
5881 bool dc_is_plane_eligible_for_idle_optimizations(struct dc *dc,
5882 		unsigned int pitch,
5883 		unsigned int height,
5884 		enum surface_pixel_format format,
5885 		struct dc_cursor_attributes *cursor_attr)
5886 {
5887 	if (dc->hwss.does_plane_fit_in_mall && dc->hwss.does_plane_fit_in_mall(dc, pitch, height, format, cursor_attr))
5888 		return true;
5889 	return false;
5890 }
5891 
5892 /* cleanup on driver unload */
5893 void dc_hardware_release(struct dc *dc)
5894 {
5895 	dc_mclk_switch_using_fw_based_vblank_stretch_shut_down(dc);
5896 
5897 	if (dc->hwss.hardware_release)
5898 		dc->hwss.hardware_release(dc);
5899 }
5900 
5901 void dc_mclk_switch_using_fw_based_vblank_stretch_shut_down(struct dc *dc)
5902 {
5903 	if (dc->current_state)
5904 		dc->current_state->bw_ctx.bw.dcn.clk.fw_based_mclk_switching_shut_down = true;
5905 }
5906 
5907 /**
5908  * dc_is_dmub_outbox_supported - Check if DMUB firmware support outbox notification
5909  *
5910  * @dc: [in] dc structure
5911  *
5912  * Checks whether DMUB FW supports outbox notifications, if supported DM
5913  * should register outbox interrupt prior to actually enabling interrupts
5914  * via dc_enable_dmub_outbox
5915  *
5916  * Return:
5917  * True if DMUB FW supports outbox notifications, False otherwise
5918  */
5919 bool dc_is_dmub_outbox_supported(struct dc *dc)
5920 {
5921 	if (!dc->caps.dmcub_support)
5922 		return false;
5923 
5924 	switch (dc->ctx->asic_id.chip_family) {
5925 
5926 	case FAMILY_YELLOW_CARP:
5927 		/* DCN31 B0 USB4 DPIA needs dmub notifications for interrupts */
5928 		if (dc->ctx->asic_id.hw_internal_rev == YELLOW_CARP_B0 &&
5929 		    !dc->debug.dpia_debug.bits.disable_dpia)
5930 			return true;
5931 	break;
5932 
5933 	case AMDGPU_FAMILY_GC_11_0_1:
5934 	case AMDGPU_FAMILY_GC_11_5_0:
5935 		if (!dc->debug.dpia_debug.bits.disable_dpia)
5936 			return true;
5937 	break;
5938 
5939 	default:
5940 		break;
5941 	}
5942 
5943 	/* dmub aux needs dmub notifications to be enabled */
5944 	return dc->debug.enable_dmub_aux_for_legacy_ddc;
5945 
5946 }
5947 
5948 /**
5949  * dc_enable_dmub_notifications - Check if dmub fw supports outbox
5950  *
5951  * @dc: [in] dc structure
5952  *
5953  * Calls dc_is_dmub_outbox_supported to check if dmub fw supports outbox
5954  * notifications. All DMs shall switch to dc_is_dmub_outbox_supported.  This
5955  * API shall be removed after switching.
5956  *
5957  * Return:
5958  * True if DMUB FW supports outbox notifications, False otherwise
5959  */
5960 bool dc_enable_dmub_notifications(struct dc *dc)
5961 {
5962 	return dc_is_dmub_outbox_supported(dc);
5963 }
5964 
5965 /**
5966  * dc_enable_dmub_outbox - Enables DMUB unsolicited notification
5967  *
5968  * @dc: [in] dc structure
5969  *
5970  * Enables DMUB unsolicited notifications to x86 via outbox.
5971  */
5972 void dc_enable_dmub_outbox(struct dc *dc)
5973 {
5974 	struct dc_context *dc_ctx = dc->ctx;
5975 
5976 	dmub_enable_outbox_notification(dc_ctx->dmub_srv);
5977 	DC_LOG_DC("%s: dmub outbox notifications enabled\n", __func__);
5978 }
5979 
5980 /**
5981  * dc_process_dmub_aux_transfer_async - Submits aux command to dmub via inbox message
5982  *                                      Sets port index appropriately for legacy DDC
5983  * @dc: dc structure
5984  * @link_index: link index
5985  * @payload: aux payload
5986  *
5987  * Returns: True if successful, False if failure
5988  */
5989 bool dc_process_dmub_aux_transfer_async(struct dc *dc,
5990 				uint32_t link_index,
5991 				struct aux_payload *payload)
5992 {
5993 	uint8_t action;
5994 	union dmub_rb_cmd cmd = {0};
5995 
5996 	ASSERT(payload->length <= 16);
5997 
5998 	cmd.dp_aux_access.header.type = DMUB_CMD__DP_AUX_ACCESS;
5999 	cmd.dp_aux_access.header.payload_bytes = 0;
6000 	/* For dpia, ddc_pin is set to NULL */
6001 	if (!dc->links[link_index]->ddc->ddc_pin)
6002 		cmd.dp_aux_access.aux_control.type = AUX_CHANNEL_DPIA;
6003 	else
6004 		cmd.dp_aux_access.aux_control.type = AUX_CHANNEL_LEGACY_DDC;
6005 
6006 	cmd.dp_aux_access.aux_control.instance = dc->links[link_index]->ddc_hw_inst;
6007 	cmd.dp_aux_access.aux_control.sw_crc_enabled = 0;
6008 	cmd.dp_aux_access.aux_control.timeout = 0;
6009 	cmd.dp_aux_access.aux_control.dpaux.address = payload->address;
6010 	cmd.dp_aux_access.aux_control.dpaux.is_i2c_over_aux = payload->i2c_over_aux;
6011 	cmd.dp_aux_access.aux_control.dpaux.length = payload->length;
6012 
6013 	/* set aux action */
6014 	if (payload->i2c_over_aux) {
6015 		if (payload->write) {
6016 			if (payload->mot)
6017 				action = DP_AUX_REQ_ACTION_I2C_WRITE_MOT;
6018 			else
6019 				action = DP_AUX_REQ_ACTION_I2C_WRITE;
6020 		} else {
6021 			if (payload->mot)
6022 				action = DP_AUX_REQ_ACTION_I2C_READ_MOT;
6023 			else
6024 				action = DP_AUX_REQ_ACTION_I2C_READ;
6025 			}
6026 	} else {
6027 		if (payload->write)
6028 			action = DP_AUX_REQ_ACTION_DPCD_WRITE;
6029 		else
6030 			action = DP_AUX_REQ_ACTION_DPCD_READ;
6031 	}
6032 
6033 	cmd.dp_aux_access.aux_control.dpaux.action = action;
6034 
6035 	if (payload->length && payload->write) {
6036 		memcpy(cmd.dp_aux_access.aux_control.dpaux.data,
6037 			payload->data,
6038 			payload->length
6039 			);
6040 	}
6041 
6042 	dc_wake_and_execute_dmub_cmd(dc->ctx, &cmd, DM_DMUB_WAIT_TYPE_WAIT);
6043 
6044 	return true;
6045 }
6046 
6047 bool dc_smart_power_oled_enable(const struct dc_link *link, bool enable, uint16_t peak_nits,
6048 			uint8_t debug_control, uint16_t fixed_CLL, uint32_t triggerline)
6049 {
6050 	bool status = false;
6051 	struct dc *dc = link->ctx->dc;
6052 	union dmub_rb_cmd cmd;
6053 	uint8_t otg_inst = 0;
6054 	unsigned int panel_inst = 0;
6055 	struct pipe_ctx *pipe_ctx = NULL;
6056 	struct resource_context *res_ctx = &link->ctx->dc->current_state->res_ctx;
6057 	int i = 0;
6058 
6059 	// get panel_inst
6060 	if (!dc_get_edp_link_panel_inst(dc, link, &panel_inst))
6061 		return status;
6062 
6063 	// get otg_inst
6064 	for (i = 0; i < MAX_PIPES; i++) {
6065 		if (res_ctx &&
6066 			res_ctx->pipe_ctx[i].stream &&
6067 			res_ctx->pipe_ctx[i].stream->link &&
6068 			res_ctx->pipe_ctx[i].stream->link == link &&
6069 			res_ctx->pipe_ctx[i].stream->link->connector_signal == SIGNAL_TYPE_EDP) {
6070 			pipe_ctx = &res_ctx->pipe_ctx[i];
6071 			//TODO: refactor for multi edp support
6072 			break;
6073 		}
6074 	}
6075 
6076 	if (pipe_ctx)
6077 		otg_inst = pipe_ctx->stream_res.tg->inst;
6078 
6079 	// before enable smart power OLED, we need to call set pipe for DMUB to set ABM config
6080 	if (enable) {
6081 		if (dc->hwss.set_pipe && pipe_ctx)
6082 			dc->hwss.set_pipe(pipe_ctx);
6083 	}
6084 
6085 	// fill in cmd
6086 	memset(&cmd, 0, sizeof(cmd));
6087 
6088 	cmd.smart_power_oled_enable.header.type = DMUB_CMD__SMART_POWER_OLED;
6089 	cmd.smart_power_oled_enable.header.sub_type = DMUB_CMD__SMART_POWER_OLED_ENABLE;
6090 	cmd.smart_power_oled_enable.header.payload_bytes =
6091 		sizeof(struct dmub_rb_cmd_smart_power_oled_enable_data) - sizeof(struct dmub_cmd_header);
6092 	cmd.smart_power_oled_enable.header.ret_status = 1;
6093 	cmd.smart_power_oled_enable.data.enable = enable;
6094 	cmd.smart_power_oled_enable.data.panel_inst = panel_inst;
6095 	cmd.smart_power_oled_enable.data.peak_nits = peak_nits;
6096 	cmd.smart_power_oled_enable.data.otg_inst = otg_inst;
6097 	cmd.smart_power_oled_enable.data.digfe_inst = link->link_enc->preferred_engine;
6098 	cmd.smart_power_oled_enable.data.digbe_inst = link->link_enc->transmitter;
6099 
6100 	cmd.smart_power_oled_enable.data.debugcontrol = debug_control;
6101 	cmd.smart_power_oled_enable.data.triggerline = triggerline;
6102 	cmd.smart_power_oled_enable.data.fixed_max_cll = fixed_CLL;
6103 
6104 	// send cmd
6105 	status = dc_wake_and_execute_dmub_cmd(dc->ctx, &cmd, DM_DMUB_WAIT_TYPE_WAIT);
6106 
6107 	return status;
6108 }
6109 
6110 bool dc_smart_power_oled_get_max_cll(const struct dc_link *link, unsigned int *pCurrent_MaxCLL)
6111 {
6112 	struct dc *dc = link->ctx->dc;
6113 	union dmub_rb_cmd cmd;
6114 	bool status = false;
6115 	unsigned int panel_inst = 0;
6116 
6117 	// get panel_inst
6118 	if (!dc_get_edp_link_panel_inst(dc, link, &panel_inst))
6119 		return status;
6120 
6121 	// fill in cmd
6122 	memset(&cmd, 0, sizeof(cmd));
6123 
6124 	cmd.smart_power_oled_getmaxcll.header.type = DMUB_CMD__SMART_POWER_OLED;
6125 	cmd.smart_power_oled_getmaxcll.header.sub_type = DMUB_CMD__SMART_POWER_OLED_GETMAXCLL;
6126 	cmd.smart_power_oled_getmaxcll.header.payload_bytes = sizeof(cmd.smart_power_oled_getmaxcll.data);
6127 	cmd.smart_power_oled_getmaxcll.header.ret_status = 1;
6128 
6129 	cmd.smart_power_oled_getmaxcll.data.input.panel_inst = panel_inst;
6130 
6131 	// send cmd and wait for reply
6132 	status = dc_wake_and_execute_dmub_cmd(dc->ctx, &cmd, DM_DMUB_WAIT_TYPE_WAIT_WITH_REPLY);
6133 
6134 	if (status)
6135 		*pCurrent_MaxCLL = cmd.smart_power_oled_getmaxcll.data.output.current_max_cll;
6136 	else
6137 		*pCurrent_MaxCLL = 0;
6138 
6139 	return status;
6140 }
6141 
6142 uint8_t get_link_index_from_dpia_port_index(const struct dc *dc,
6143 					    uint8_t dpia_port_index)
6144 {
6145 	uint8_t index, link_index = 0xFF;
6146 
6147 	for (index = 0; index < dc->link_count; index++) {
6148 		/* ddc_hw_inst has dpia port index for dpia links
6149 		 * and ddc instance for legacy links
6150 		 */
6151 		if (!dc->links[index]->ddc->ddc_pin) {
6152 			if (dc->links[index]->ddc_hw_inst == dpia_port_index) {
6153 				link_index = index;
6154 				break;
6155 			}
6156 		}
6157 	}
6158 	ASSERT(link_index != 0xFF);
6159 	return link_index;
6160 }
6161 
6162 /**
6163  * dc_process_dmub_set_config_async - Submits set_config command
6164  *
6165  * @dc: [in] dc structure
6166  * @link_index: [in] link_index: link index
6167  * @payload: [in] aux payload
6168  * @notify: [out] set_config immediate reply
6169  *
6170  * Submits set_config command to dmub via inbox message.
6171  *
6172  * Return:
6173  * True if successful, False if failure
6174  */
6175 bool dc_process_dmub_set_config_async(struct dc *dc,
6176 				uint32_t link_index,
6177 				struct set_config_cmd_payload *payload,
6178 				struct dmub_notification *notify)
6179 {
6180 	union dmub_rb_cmd cmd = {0};
6181 	bool is_cmd_complete = true;
6182 
6183 	/* prepare SET_CONFIG command */
6184 	cmd.set_config_access.header.type = DMUB_CMD__DPIA;
6185 	cmd.set_config_access.header.sub_type = DMUB_CMD__DPIA_SET_CONFIG_ACCESS;
6186 
6187 	cmd.set_config_access.set_config_control.instance = dc->links[link_index]->ddc_hw_inst;
6188 	cmd.set_config_access.set_config_control.cmd_pkt.msg_type = payload->msg_type;
6189 	cmd.set_config_access.set_config_control.cmd_pkt.msg_data = payload->msg_data;
6190 
6191 	if (!dc_wake_and_execute_dmub_cmd(dc->ctx, &cmd, DM_DMUB_WAIT_TYPE_WAIT_WITH_REPLY)) {
6192 		/* command is not processed by dmub */
6193 		notify->sc_status = SET_CONFIG_UNKNOWN_ERROR;
6194 		return is_cmd_complete;
6195 	}
6196 
6197 	/* command processed by dmub, if ret_status is 1, it is completed instantly */
6198 	if (cmd.set_config_access.header.ret_status == 1)
6199 		notify->sc_status = cmd.set_config_access.set_config_control.immed_status;
6200 	else
6201 		/* cmd pending, will receive notification via outbox */
6202 		is_cmd_complete = false;
6203 
6204 	return is_cmd_complete;
6205 }
6206 
6207 /**
6208  * dc_process_dmub_set_mst_slots - Submits MST solt allocation
6209  *
6210  * @dc: [in] dc structure
6211  * @link_index: [in] link index
6212  * @mst_alloc_slots: [in] mst slots to be allotted
6213  * @mst_slots_in_use: [out] mst slots in use returned in failure case
6214  *
6215  * Submits mst slot allocation command to dmub via inbox message
6216  *
6217  * Return:
6218  * DC_OK if successful, DC_ERROR if failure
6219  */
6220 enum dc_status dc_process_dmub_set_mst_slots(const struct dc *dc,
6221 				uint32_t link_index,
6222 				uint8_t mst_alloc_slots,
6223 				uint8_t *mst_slots_in_use)
6224 {
6225 	union dmub_rb_cmd cmd = {0};
6226 
6227 	/* prepare MST_ALLOC_SLOTS command */
6228 	cmd.set_mst_alloc_slots.header.type = DMUB_CMD__DPIA;
6229 	cmd.set_mst_alloc_slots.header.sub_type = DMUB_CMD__DPIA_MST_ALLOC_SLOTS;
6230 
6231 	cmd.set_mst_alloc_slots.mst_slots_control.instance = dc->links[link_index]->ddc_hw_inst;
6232 	cmd.set_mst_alloc_slots.mst_slots_control.mst_alloc_slots = mst_alloc_slots;
6233 
6234 	if (!dc_wake_and_execute_dmub_cmd(dc->ctx, &cmd, DM_DMUB_WAIT_TYPE_WAIT_WITH_REPLY))
6235 		/* command is not processed by dmub */
6236 		return DC_ERROR_UNEXPECTED;
6237 
6238 	/* command processed by dmub, if ret_status is 1 */
6239 	if (cmd.set_config_access.header.ret_status != 1)
6240 		/* command processing error */
6241 		return DC_ERROR_UNEXPECTED;
6242 
6243 	/* command processed and we have a status of 2, mst not enabled in dpia */
6244 	if (cmd.set_mst_alloc_slots.mst_slots_control.immed_status == 2)
6245 		return DC_FAIL_UNSUPPORTED_1;
6246 
6247 	/* previously configured mst alloc and used slots did not match */
6248 	if (cmd.set_mst_alloc_slots.mst_slots_control.immed_status == 3) {
6249 		*mst_slots_in_use = cmd.set_mst_alloc_slots.mst_slots_control.mst_slots_in_use;
6250 		return DC_NOT_SUPPORTED;
6251 	}
6252 
6253 	return DC_OK;
6254 }
6255 
6256 /**
6257  * dc_process_dmub_dpia_set_tps_notification - Submits tps notification
6258  *
6259  * @dc: [in] dc structure
6260  * @link_index: [in] link index
6261  * @tps: [in] request tps
6262  *
6263  * Submits set_tps_notification command to dmub via inbox message
6264  */
6265 void dc_process_dmub_dpia_set_tps_notification(const struct dc *dc, uint32_t link_index, uint8_t tps)
6266 {
6267 	union dmub_rb_cmd cmd = {0};
6268 
6269 	cmd.set_tps_notification.header.type = DMUB_CMD__DPIA;
6270 	cmd.set_tps_notification.header.sub_type = DMUB_CMD__DPIA_SET_TPS_NOTIFICATION;
6271 	cmd.set_tps_notification.tps_notification.instance = dc->links[link_index]->ddc_hw_inst;
6272 	cmd.set_tps_notification.tps_notification.tps = tps;
6273 
6274 	dc_wake_and_execute_dmub_cmd(dc->ctx, &cmd, DM_DMUB_WAIT_TYPE_WAIT);
6275 }
6276 
6277 /**
6278  * dc_process_dmub_dpia_hpd_int_enable - Submits DPIA DPD interruption
6279  *
6280  * @dc: [in] dc structure
6281  * @hpd_int_enable: [in] 1 for hpd int enable, 0 to disable
6282  *
6283  * Submits dpia hpd int enable command to dmub via inbox message
6284  */
6285 void dc_process_dmub_dpia_hpd_int_enable(const struct dc *dc,
6286 				uint32_t hpd_int_enable)
6287 {
6288 	union dmub_rb_cmd cmd = {0};
6289 
6290 	cmd.dpia_hpd_int_enable.header.type = DMUB_CMD__DPIA_HPD_INT_ENABLE;
6291 	cmd.dpia_hpd_int_enable.enable = hpd_int_enable;
6292 
6293 	dc_wake_and_execute_dmub_cmd(dc->ctx, &cmd, DM_DMUB_WAIT_TYPE_WAIT);
6294 
6295 	DC_LOG_DEBUG("%s: hpd_int_enable(%d)\n", __func__, hpd_int_enable);
6296 }
6297 
6298 /**
6299  * dc_print_dmub_diagnostic_data - Print DMUB diagnostic data for debugging
6300  *
6301  * @dc: [in] dc structure
6302  *
6303  *
6304  */
6305 void dc_print_dmub_diagnostic_data(const struct dc *dc)
6306 {
6307 	dc_dmub_srv_log_diagnostic_data(dc->ctx->dmub_srv);
6308 }
6309 
6310 /**
6311  * dc_disable_accelerated_mode - disable accelerated mode
6312  * @dc: dc structure
6313  */
6314 void dc_disable_accelerated_mode(struct dc *dc)
6315 {
6316 	bios_set_scratch_acc_mode_change(dc->ctx->dc_bios, 0);
6317 }
6318 
6319 
6320 /**
6321  *  dc_notify_vsync_int_state - notifies vsync enable/disable state
6322  *  @dc: dc structure
6323  *  @stream: stream where vsync int state changed
6324  *  @enable: whether vsync is enabled or disabled
6325  *
6326  *  Called when vsync is enabled/disabled Will notify DMUB to start/stop ABM
6327  *  interrupts after steady state is reached.
6328  */
6329 void dc_notify_vsync_int_state(struct dc *dc, struct dc_stream_state *stream, bool enable)
6330 {
6331 	int i;
6332 	int edp_num;
6333 	struct pipe_ctx *pipe = NULL;
6334 	struct dc_link *link = stream->sink->link;
6335 	struct dc_link *edp_links[MAX_NUM_EDP];
6336 
6337 
6338 	if (link->psr_settings.psr_feature_enabled)
6339 		return;
6340 
6341 	if (link->replay_settings.replay_feature_enabled)
6342 		return;
6343 
6344 	/*find primary pipe associated with stream*/
6345 	for (i = 0; i < MAX_PIPES; i++) {
6346 		pipe = &dc->current_state->res_ctx.pipe_ctx[i];
6347 
6348 		if (pipe->stream == stream && pipe->stream_res.tg)
6349 			break;
6350 	}
6351 
6352 	if (i == MAX_PIPES) {
6353 		ASSERT(0);
6354 		return;
6355 	}
6356 
6357 	dc_get_edp_links(dc, edp_links, &edp_num);
6358 
6359 	/* Determine panel inst */
6360 	for (i = 0; i < edp_num; i++) {
6361 		if (edp_links[i] == link)
6362 			break;
6363 	}
6364 
6365 	if (i == edp_num) {
6366 		return;
6367 	}
6368 
6369 	if (pipe->stream_res.abm && pipe->stream_res.abm->funcs->set_abm_pause)
6370 		pipe->stream_res.abm->funcs->set_abm_pause(pipe->stream_res.abm, !enable, i, pipe->stream_res.tg->inst);
6371 }
6372 
6373 /*****************************************************************************
6374  *  dc_abm_save_restore() - Interface to DC for save+pause and restore+un-pause
6375  *                          ABM
6376  *  @dc: dc structure
6377  *	@stream: stream where vsync int state changed
6378  *  @pData: abm hw states
6379  *
6380  ****************************************************************************/
6381 bool dc_abm_save_restore(
6382 		struct dc *dc,
6383 		struct dc_stream_state *stream,
6384 		struct abm_save_restore *pData)
6385 {
6386 	int i;
6387 	int edp_num;
6388 	struct pipe_ctx *pipe = NULL;
6389 	struct dc_link *link = stream->sink->link;
6390 	struct dc_link *edp_links[MAX_NUM_EDP];
6391 
6392 	if (link->replay_settings.replay_feature_enabled)
6393 		return false;
6394 
6395 	/*find primary pipe associated with stream*/
6396 	for (i = 0; i < MAX_PIPES; i++) {
6397 		pipe = &dc->current_state->res_ctx.pipe_ctx[i];
6398 
6399 		if (pipe->stream == stream && pipe->stream_res.tg)
6400 			break;
6401 	}
6402 
6403 	if (i == MAX_PIPES) {
6404 		ASSERT(0);
6405 		return false;
6406 	}
6407 
6408 	dc_get_edp_links(dc, edp_links, &edp_num);
6409 
6410 	/* Determine panel inst */
6411 	for (i = 0; i < edp_num; i++)
6412 		if (edp_links[i] == link)
6413 			break;
6414 
6415 	if (i == edp_num)
6416 		return false;
6417 
6418 	if (pipe->stream_res.abm &&
6419 		pipe->stream_res.abm->funcs->save_restore)
6420 		return pipe->stream_res.abm->funcs->save_restore(
6421 				pipe->stream_res.abm,
6422 				i,
6423 				pData);
6424 	return false;
6425 }
6426 
6427 void dc_query_current_properties(struct dc *dc, struct dc_current_properties *properties)
6428 {
6429 	unsigned int i;
6430 	unsigned int max_cursor_size = dc->caps.max_cursor_size;
6431 	unsigned int stream_cursor_size;
6432 
6433 	if (dc->debug.allow_sw_cursor_fallback && dc->res_pool->funcs->get_max_hw_cursor_size) {
6434 		for (i = 0; i < dc->current_state->stream_count; i++) {
6435 			stream_cursor_size = dc->res_pool->funcs->get_max_hw_cursor_size(dc,
6436 					dc->current_state,
6437 					dc->current_state->streams[i]);
6438 
6439 			if (stream_cursor_size < max_cursor_size) {
6440 				max_cursor_size = stream_cursor_size;
6441 			}
6442 		}
6443 	}
6444 
6445 	properties->cursor_size_limit = max_cursor_size;
6446 }
6447 
6448 /**
6449  * dc_set_edp_power() - DM controls eDP power to be ON/OFF
6450  *
6451  * Called when DM wants to power on/off eDP.
6452  *     Only work on links with flag skip_implict_edp_power_control is set.
6453  *
6454  * @dc: Current DC state
6455  * @edp_link: a link with eDP connector signal type
6456  * @powerOn: power on/off eDP
6457  *
6458  * Return: void
6459  */
6460 void dc_set_edp_power(const struct dc *dc, struct dc_link *edp_link,
6461 				 bool powerOn)
6462 {
6463 	if (edp_link->connector_signal != SIGNAL_TYPE_EDP)
6464 		return;
6465 
6466 	if (edp_link->skip_implict_edp_power_control == false)
6467 		return;
6468 
6469 	edp_link->dc->link_srv->edp_set_panel_power(edp_link, powerOn);
6470 }
6471 
6472 /**
6473  * dc_get_power_profile_for_dc_state() - extracts power profile from dc state
6474  *
6475  * Called when DM wants to make power policy decisions based on dc_state
6476  *
6477  * @context: Pointer to the dc_state from which the power profile is extracted.
6478  *
6479  * Return: The power profile structure containing the power level information.
6480  */
6481 struct dc_power_profile dc_get_power_profile_for_dc_state(const struct dc_state *context)
6482 {
6483 	struct dc_power_profile profile = { 0 };
6484 
6485 	profile.power_level = !context->bw_ctx.bw.dcn.clk.p_state_change_support;
6486 	if (!context->clk_mgr || !context->clk_mgr->ctx || !context->clk_mgr->ctx->dc)
6487 		return profile;
6488 	struct dc *dc = context->clk_mgr->ctx->dc;
6489 
6490 	if (dc->res_pool->funcs->get_power_profile)
6491 		profile.power_level = dc->res_pool->funcs->get_power_profile(context);
6492 	return profile;
6493 }
6494 
6495 /**
6496  * dc_get_det_buffer_size_from_state() - extracts detile buffer size from dc state
6497  *
6498  * This function is called to log the detile buffer size from the dc_state.
6499  *
6500  * @context: a pointer to the dc_state from which the detile buffer size is extracted.
6501  *
6502  * Return: the size of the detile buffer, or 0 if not available.
6503  */
6504 unsigned int dc_get_det_buffer_size_from_state(const struct dc_state *context)
6505 {
6506 	struct dc *dc = context->clk_mgr->ctx->dc;
6507 
6508 	if (dc->res_pool->funcs->get_det_buffer_size)
6509 		return dc->res_pool->funcs->get_det_buffer_size(context);
6510 	else
6511 		return 0;
6512 }
6513 
6514 /**
6515  * dc_get_host_router_index: Get index of host router from a dpia link
6516  *
6517  * This function return a host router index of the target link. If the target link is dpia link.
6518  *
6519  * @link: Pointer to the target link (input)
6520  * @host_router_index: Pointer to store the host router index of the target link (output).
6521  *
6522  * Return: true if the host router index is found and valid.
6523  *
6524  */
6525 bool dc_get_host_router_index(const struct dc_link *link, unsigned int *host_router_index)
6526 {
6527 	struct dc *dc;
6528 
6529 	if (!link || !host_router_index || link->ep_type != DISPLAY_ENDPOINT_USB4_DPIA)
6530 		return false;
6531 
6532 	dc = link->ctx->dc;
6533 
6534 	if (link->link_index < dc->lowest_dpia_link_index)
6535 		return false;
6536 
6537 	*host_router_index = (link->link_index - dc->lowest_dpia_link_index) / dc->caps.num_of_dpias_per_host_router;
6538 	if (*host_router_index < dc->caps.num_of_host_routers)
6539 		return true;
6540 	else
6541 		return false;
6542 }
6543 
6544 bool dc_is_cursor_limit_pending(struct dc *dc)
6545 {
6546 	uint32_t i;
6547 
6548 	for (i = 0; i < dc->current_state->stream_count; i++) {
6549 		if (dc_stream_is_cursor_limit_pending(dc, dc->current_state->streams[i]))
6550 			return true;
6551 	}
6552 
6553 	return false;
6554 }
6555 
6556 bool dc_can_clear_cursor_limit(const struct dc *dc)
6557 {
6558 	uint32_t i;
6559 
6560 	for (i = 0; i < dc->current_state->stream_count; i++) {
6561 		if (dc_state_can_clear_stream_cursor_subvp_limit(dc->current_state->streams[i], dc->current_state))
6562 			return true;
6563 	}
6564 
6565 	return false;
6566 }
6567 
6568 void dc_get_underflow_debug_data_for_otg(struct dc *dc, int primary_otg_inst,
6569 				struct dc_underflow_debug_data *out_data)
6570 {
6571 	struct timing_generator *tg = NULL;
6572 
6573 	for (int i = 0; i < MAX_PIPES; i++) {
6574 		if (dc->res_pool->timing_generators[i] &&
6575 			dc->res_pool->timing_generators[i]->inst == primary_otg_inst) {
6576 				tg = dc->res_pool->timing_generators[i];
6577 				break;
6578 		}
6579 	}
6580 
6581 	dc_exit_ips_for_hw_access(dc);
6582 	if (dc->hwss.get_underflow_debug_data)
6583 		dc->hwss.get_underflow_debug_data(dc, tg, out_data);
6584 }
6585 
6586 void dc_get_power_feature_status(struct dc *dc, int primary_otg_inst,
6587 				struct power_features *out_data)
6588 {
6589 	out_data->uclk_p_state = dc->current_state->clk_mgr->clks.p_state_change_support;
6590 	out_data->fams = dc->current_state->bw_ctx.bw.dcn.clk.fw_based_mclk_switching;
6591 }
6592 
6593 bool dc_capture_register_software_state(struct dc *dc, struct dc_register_software_state *state)
6594 {
6595 	struct dc_state *context;
6596 	struct resource_context *res_ctx;
6597 	int i;
6598 
6599 	if (!dc || !dc->current_state || !state) {
6600 		if (state)
6601 			state->state_valid = false;
6602 		return false;
6603 	}
6604 
6605 	/* Initialize the state structure */
6606 	memset(state, 0, sizeof(struct dc_register_software_state));
6607 
6608 	context = dc->current_state;
6609 	res_ctx = &context->res_ctx;
6610 
6611 	/* Count active pipes and streams */
6612 	state->active_pipe_count = 0;
6613 	state->active_stream_count = context->stream_count;
6614 
6615 	for (i = 0; i < dc->res_pool->pipe_count; i++) {
6616 		if (res_ctx->pipe_ctx[i].stream)
6617 			state->active_pipe_count++;
6618 	}
6619 
6620 	/* Capture HUBP programming state for each pipe */
6621 	for (i = 0; i < MAX_PIPES && i < dc->res_pool->pipe_count; i++) {
6622 		struct pipe_ctx *pipe_ctx = &res_ctx->pipe_ctx[i];
6623 
6624 		state->hubp[i].valid_stream = false;
6625 		if (!pipe_ctx->stream)
6626 			continue;
6627 
6628 		state->hubp[i].valid_stream = true;
6629 
6630 		/* HUBP register programming variables */
6631 		if (pipe_ctx->stream_res.tg)
6632 			state->hubp[i].vtg_sel = pipe_ctx->stream_res.tg->inst;
6633 
6634 		state->hubp[i].hubp_clock_enable = (pipe_ctx->plane_res.hubp != NULL) ? 1 : 0;
6635 
6636 		state->hubp[i].valid_plane_state = false;
6637 		if (pipe_ctx->plane_state) {
6638 			state->hubp[i].valid_plane_state = true;
6639 			state->hubp[i].surface_pixel_format = pipe_ctx->plane_state->format;
6640 			state->hubp[i].rotation_angle = pipe_ctx->plane_state->rotation;
6641 			state->hubp[i].h_mirror_en = pipe_ctx->plane_state->horizontal_mirror ? 1 : 0;
6642 
6643 			/* Surface size */
6644 			if (pipe_ctx->plane_state->plane_size.surface_size.width > 0) {
6645 				state->hubp[i].surface_size_width = pipe_ctx->plane_state->plane_size.surface_size.width;
6646 				state->hubp[i].surface_size_height = pipe_ctx->plane_state->plane_size.surface_size.height;
6647 			}
6648 
6649 			/* Viewport dimensions from scaler data */
6650 			if (pipe_ctx->plane_state->src_rect.width > 0) {
6651 				state->hubp[i].pri_viewport_width = pipe_ctx->plane_state->src_rect.width;
6652 				state->hubp[i].pri_viewport_height = pipe_ctx->plane_state->src_rect.height;
6653 				state->hubp[i].pri_viewport_x_start = pipe_ctx->plane_state->src_rect.x;
6654 				state->hubp[i].pri_viewport_y_start = pipe_ctx->plane_state->src_rect.y;
6655 			}
6656 
6657 			/* DCC settings */
6658 			state->hubp[i].surface_dcc_en = (pipe_ctx->plane_state->dcc.enable) ? 1 : 0;
6659 			state->hubp[i].surface_dcc_ind_64b_blk = pipe_ctx->plane_state->dcc.independent_64b_blks;
6660 			state->hubp[i].surface_dcc_ind_128b_blk = pipe_ctx->plane_state->dcc.dcc_ind_blk;
6661 
6662 			/* Surface pitch */
6663 			state->hubp[i].surface_pitch = pipe_ctx->plane_state->plane_size.surface_pitch;
6664 			state->hubp[i].meta_pitch = pipe_ctx->plane_state->dcc.meta_pitch;
6665 			state->hubp[i].chroma_pitch = pipe_ctx->plane_state->plane_size.chroma_pitch;
6666 			state->hubp[i].meta_pitch_c = pipe_ctx->plane_state->dcc.meta_pitch_c;
6667 
6668 			/* Surface addresses - primary */
6669 			state->hubp[i].primary_surface_address_low = pipe_ctx->plane_state->address.grph.addr.low_part;
6670 			state->hubp[i].primary_surface_address_high = pipe_ctx->plane_state->address.grph.addr.high_part;
6671 			state->hubp[i].primary_meta_surface_address_low = pipe_ctx->plane_state->address.grph.meta_addr.low_part;
6672 			state->hubp[i].primary_meta_surface_address_high = pipe_ctx->plane_state->address.grph.meta_addr.high_part;
6673 
6674 			/* TMZ settings */
6675 			state->hubp[i].primary_surface_tmz = pipe_ctx->plane_state->address.tmz_surface;
6676 			state->hubp[i].primary_meta_surface_tmz = pipe_ctx->plane_state->address.tmz_surface;
6677 
6678 			/* Tiling configuration */
6679 			state->hubp[i].min_dc_gfx_version9 = false;
6680 			if (pipe_ctx->plane_state->tiling_info.gfxversion >= DcGfxVersion9) {
6681 				state->hubp[i].min_dc_gfx_version9 = true;
6682 				state->hubp[i].sw_mode = pipe_ctx->plane_state->tiling_info.gfx9.swizzle;
6683 				state->hubp[i].num_pipes = pipe_ctx->plane_state->tiling_info.gfx9.num_pipes;
6684 				state->hubp[i].num_banks = pipe_ctx->plane_state->tiling_info.gfx9.num_banks;
6685 				state->hubp[i].pipe_interleave = pipe_ctx->plane_state->tiling_info.gfx9.pipe_interleave;
6686 				state->hubp[i].num_shader_engines = pipe_ctx->plane_state->tiling_info.gfx9.num_shader_engines;
6687 				state->hubp[i].num_rb_per_se = pipe_ctx->plane_state->tiling_info.gfx9.num_rb_per_se;
6688 				state->hubp[i].num_pkrs = pipe_ctx->plane_state->tiling_info.gfx9.num_pkrs;
6689 			}
6690 		}
6691 
6692 		/* DML Request Size Configuration */
6693 		if (pipe_ctx->rq_regs.rq_regs_l.chunk_size > 0) {
6694 			state->hubp[i].rq_chunk_size = pipe_ctx->rq_regs.rq_regs_l.chunk_size;
6695 			state->hubp[i].rq_min_chunk_size = pipe_ctx->rq_regs.rq_regs_l.min_chunk_size;
6696 			state->hubp[i].rq_meta_chunk_size = pipe_ctx->rq_regs.rq_regs_l.meta_chunk_size;
6697 			state->hubp[i].rq_min_meta_chunk_size = pipe_ctx->rq_regs.rq_regs_l.min_meta_chunk_size;
6698 			state->hubp[i].rq_dpte_group_size = pipe_ctx->rq_regs.rq_regs_l.dpte_group_size;
6699 			state->hubp[i].rq_mpte_group_size = pipe_ctx->rq_regs.rq_regs_l.mpte_group_size;
6700 			state->hubp[i].rq_swath_height_l = pipe_ctx->rq_regs.rq_regs_l.swath_height;
6701 			state->hubp[i].rq_pte_row_height_l = pipe_ctx->rq_regs.rq_regs_l.pte_row_height_linear;
6702 		}
6703 
6704 		/* Chroma request size configuration */
6705 		if (pipe_ctx->rq_regs.rq_regs_c.chunk_size > 0) {
6706 			state->hubp[i].rq_chunk_size_c = pipe_ctx->rq_regs.rq_regs_c.chunk_size;
6707 			state->hubp[i].rq_min_chunk_size_c = pipe_ctx->rq_regs.rq_regs_c.min_chunk_size;
6708 			state->hubp[i].rq_meta_chunk_size_c = pipe_ctx->rq_regs.rq_regs_c.meta_chunk_size;
6709 			state->hubp[i].rq_min_meta_chunk_size_c = pipe_ctx->rq_regs.rq_regs_c.min_meta_chunk_size;
6710 			state->hubp[i].rq_dpte_group_size_c = pipe_ctx->rq_regs.rq_regs_c.dpte_group_size;
6711 			state->hubp[i].rq_mpte_group_size_c = pipe_ctx->rq_regs.rq_regs_c.mpte_group_size;
6712 			state->hubp[i].rq_swath_height_c = pipe_ctx->rq_regs.rq_regs_c.swath_height;
6713 			state->hubp[i].rq_pte_row_height_c = pipe_ctx->rq_regs.rq_regs_c.pte_row_height_linear;
6714 		}
6715 
6716 		/* DML expansion modes */
6717 		state->hubp[i].drq_expansion_mode = pipe_ctx->rq_regs.drq_expansion_mode;
6718 		state->hubp[i].prq_expansion_mode = pipe_ctx->rq_regs.prq_expansion_mode;
6719 		state->hubp[i].mrq_expansion_mode = pipe_ctx->rq_regs.mrq_expansion_mode;
6720 		state->hubp[i].crq_expansion_mode = pipe_ctx->rq_regs.crq_expansion_mode;
6721 
6722 		/* DML DLG parameters - nominal */
6723 		state->hubp[i].dst_y_per_vm_vblank = pipe_ctx->dlg_regs.dst_y_per_vm_vblank;
6724 		state->hubp[i].dst_y_per_row_vblank = pipe_ctx->dlg_regs.dst_y_per_row_vblank;
6725 		state->hubp[i].dst_y_per_vm_flip = pipe_ctx->dlg_regs.dst_y_per_vm_flip;
6726 		state->hubp[i].dst_y_per_row_flip = pipe_ctx->dlg_regs.dst_y_per_row_flip;
6727 
6728 		/* DML prefetch settings */
6729 		state->hubp[i].dst_y_prefetch = pipe_ctx->dlg_regs.dst_y_prefetch;
6730 		state->hubp[i].vratio_prefetch = pipe_ctx->dlg_regs.vratio_prefetch;
6731 		state->hubp[i].vratio_prefetch_c = pipe_ctx->dlg_regs.vratio_prefetch_c;
6732 
6733 		/* TTU parameters */
6734 		state->hubp[i].qos_level_low_wm = pipe_ctx->ttu_regs.qos_level_low_wm;
6735 		state->hubp[i].qos_level_high_wm = pipe_ctx->ttu_regs.qos_level_high_wm;
6736 		state->hubp[i].qos_level_flip = pipe_ctx->ttu_regs.qos_level_flip;
6737 		state->hubp[i].min_ttu_vblank = pipe_ctx->ttu_regs.min_ttu_vblank;
6738 	}
6739 
6740 	/* Capture HUBBUB programming state */
6741 	if (dc->res_pool->hubbub) {
6742 		/* Individual DET buffer sizes - software state variables that program DET registers */
6743 		for (i = 0; i < 4 && i < dc->res_pool->pipe_count; i++) {
6744 			uint32_t det_size = res_ctx->pipe_ctx[i].det_buffer_size_kb;
6745 			switch (i) {
6746 			case 0:
6747 				state->hubbub.det0_size = det_size;
6748 				break;
6749 			case 1:
6750 				state->hubbub.det1_size = det_size;
6751 				break;
6752 			case 2:
6753 				state->hubbub.det2_size = det_size;
6754 				break;
6755 			case 3:
6756 				state->hubbub.det3_size = det_size;
6757 				break;
6758 			}
6759 		}
6760 
6761 		/* Compression buffer configuration - software state that programs COMPBUF_SIZE register */
6762 		// TODO: Handle logic for legacy DCN pre-DCN401
6763 		state->hubbub.compbuf_size = context->bw_ctx.bw.dcn.arb_regs.compbuf_size;
6764 	}
6765 
6766 	/* Capture DPP programming state for each pipe */
6767 	for (i = 0; i < MAX_PIPES && i < dc->res_pool->pipe_count; i++) {
6768 		struct pipe_ctx *pipe_ctx = &res_ctx->pipe_ctx[i];
6769 
6770 		if (!pipe_ctx->stream)
6771 			continue;
6772 
6773 		state->dpp[i].dpp_clock_enable = (pipe_ctx->plane_res.dpp != NULL) ? 1 : 0;
6774 
6775 		if (pipe_ctx->plane_state && pipe_ctx->plane_res.scl_data.recout.width > 0) {
6776 			/* Access dscl_prog_data directly - this contains the actual software state used for register programming */
6777 			struct dscl_prog_data *dscl_data = &pipe_ctx->plane_res.scl_data.dscl_prog_data;
6778 
6779 			/* Recout (Rectangle of Interest) configuration - software state that programs RECOUT registers */
6780 			state->dpp[i].recout_start_x = dscl_data->recout.x;
6781 			state->dpp[i].recout_start_y = dscl_data->recout.y;
6782 			state->dpp[i].recout_width = dscl_data->recout.width;
6783 			state->dpp[i].recout_height = dscl_data->recout.height;
6784 
6785 			/* MPC (Multiple Pipe/Plane Combiner) size - software state that programs MPC_SIZE registers */
6786 			state->dpp[i].mpc_width = dscl_data->mpc_size.width;
6787 			state->dpp[i].mpc_height = dscl_data->mpc_size.height;
6788 
6789 			/* DSCL mode - software state that programs SCL_MODE registers */
6790 			state->dpp[i].dscl_mode = dscl_data->dscl_mode;
6791 
6792 			/* Scaler ratios - software state that programs scale ratio registers (use actual programmed ratios) */
6793 			state->dpp[i].horz_ratio_int = dscl_data->ratios.h_scale_ratio >> 19; // Extract integer part from programmed ratio
6794 			state->dpp[i].vert_ratio_int = dscl_data->ratios.v_scale_ratio >> 19; // Extract integer part from programmed ratio
6795 
6796 			/* Basic scaler taps - software state that programs tap control registers (use actual programmed taps) */
6797 			state->dpp[i].h_taps = dscl_data->taps.h_taps + 1; // dscl_prog_data.taps stores (taps - 1), so add 1 back
6798 			state->dpp[i].v_taps = dscl_data->taps.v_taps + 1; // dscl_prog_data.taps stores (taps - 1), so add 1 back
6799 		}
6800 	}
6801 
6802 	/* Capture essential clock state for underflow analysis */
6803 	if (dc->clk_mgr && dc->clk_mgr->clks.dispclk_khz > 0) {
6804 		/* Core display clocks affecting bandwidth and timing */
6805 		state->dccg.dispclk_khz = dc->clk_mgr->clks.dispclk_khz;
6806 
6807 		/* Per-pipe clock configuration - only capture what's essential */
6808 		for (i = 0; i < MAX_PIPES && i < dc->res_pool->pipe_count; i++) {
6809 			struct pipe_ctx *pipe_ctx = &res_ctx->pipe_ctx[i];
6810 			if (pipe_ctx->stream) {
6811 				/* Essential clocks that directly affect underflow risk */
6812 				state->dccg.dppclk_khz[i] = dc->clk_mgr->clks.dppclk_khz;
6813 				state->dccg.pixclk_khz[i] = pipe_ctx->stream->timing.pix_clk_100hz / 10;
6814 				state->dccg.dppclk_enable[i] = 1;
6815 
6816 				/* DP stream clock only for DP signals */
6817 				if (pipe_ctx->stream->signal == SIGNAL_TYPE_DISPLAY_PORT ||
6818 						pipe_ctx->stream->signal == SIGNAL_TYPE_DISPLAY_PORT_MST) {
6819 					state->dccg.dpstreamclk_enable[i] = 1;
6820 				} else {
6821 					state->dccg.dpstreamclk_enable[i] = 0;
6822 				}
6823 			} else {
6824 				/* Inactive pipe - no clocks */
6825 				state->dccg.dppclk_khz[i] = 0;
6826 				state->dccg.pixclk_khz[i] = 0;
6827 				state->dccg.dppclk_enable[i] = 0;
6828 				if (i < 4) {
6829 					state->dccg.dpstreamclk_enable[i] = 0;
6830 				}
6831 			}
6832 		}
6833 
6834 		/* DSC clock state - only when actually using DSC */
6835 		for (i = 0; i < MAX_PIPES; i++) {
6836 			struct pipe_ctx *pipe_ctx = (i < dc->res_pool->pipe_count) ? &res_ctx->pipe_ctx[i] : NULL;
6837 			if (pipe_ctx && pipe_ctx->stream && pipe_ctx->stream->timing.dsc_cfg.num_slices_h > 0) {
6838 				state->dccg.dscclk_khz[i] = 400000; /* Typical DSC clock frequency */
6839 			} else {
6840 				state->dccg.dscclk_khz[i] = 0;
6841 			}
6842 		}
6843 
6844 		/* SYMCLK32 LE Control - only the essential HPO state for underflow analysis */
6845 		for (i = 0; i < 2; i++) {
6846 			state->dccg.symclk32_le_enable[i] = 0; /* Default: disabled */
6847 		}
6848 
6849 	}
6850 
6851 	/* Capture essential DSC configuration for underflow analysis */
6852 	for (i = 0; i < MAX_PIPES && i < dc->res_pool->pipe_count; i++) {
6853 		struct pipe_ctx *pipe_ctx = &res_ctx->pipe_ctx[i];
6854 
6855 		if (pipe_ctx->stream && pipe_ctx->stream->timing.dsc_cfg.num_slices_h > 0) {
6856 			/* DSC is enabled - capture essential configuration */
6857 			state->dsc[i].dsc_clock_enable = 1;
6858 
6859 			/* DSC configuration affecting bandwidth and timing */
6860 			struct dc_dsc_config *dsc_cfg = &pipe_ctx->stream->timing.dsc_cfg;
6861 			state->dsc[i].dsc_num_slices_h = dsc_cfg->num_slices_h;
6862 			state->dsc[i].dsc_num_slices_v = dsc_cfg->num_slices_v;
6863 			state->dsc[i].dsc_bits_per_pixel = dsc_cfg->bits_per_pixel;
6864 
6865 			/* OPP pipe source for DSC forwarding */
6866 			if (pipe_ctx->stream_res.opp) {
6867 				state->dsc[i].dscrm_dsc_forward_enable = 1;
6868 				state->dsc[i].dscrm_dsc_opp_pipe_source = pipe_ctx->stream_res.opp->inst;
6869 			} else {
6870 				state->dsc[i].dscrm_dsc_forward_enable = 0;
6871 				state->dsc[i].dscrm_dsc_opp_pipe_source = 0;
6872 			}
6873 		} else {
6874 			/* DSC not enabled - clear all fields */
6875 			memset(&state->dsc[i], 0, sizeof(state->dsc[i]));
6876 		}
6877 	}
6878 
6879 	/* Capture MPC programming state - comprehensive register field coverage */
6880 	for (i = 0; i < MAX_PIPES && i < dc->res_pool->pipe_count; i++) {
6881 		struct pipe_ctx *pipe_ctx = &res_ctx->pipe_ctx[i];
6882 
6883 		if (pipe_ctx->plane_state && pipe_ctx->stream) {
6884 			struct dc_plane_state *plane_state = pipe_ctx->plane_state;
6885 
6886 			/* MPCC blending tree and mode control - capture actual blend configuration */
6887 			state->mpc.mpcc_mode[i] = (plane_state->blend_tf.type != TF_TYPE_BYPASS) ? 1 : 0;
6888 			state->mpc.mpcc_alpha_blend_mode[i] = plane_state->per_pixel_alpha ? 1 : 0;
6889 			state->mpc.mpcc_alpha_multiplied_mode[i] = plane_state->pre_multiplied_alpha ? 1 : 0;
6890 			state->mpc.mpcc_blnd_active_overlap_only[i] = 0; /* Default - no overlap restriction */
6891 			state->mpc.mpcc_global_alpha[i] = plane_state->global_alpha_value;
6892 			state->mpc.mpcc_global_gain[i] = plane_state->global_alpha ? 255 : 0;
6893 			state->mpc.mpcc_bg_bpc[i] = 8; /* Standard 8-bit background */
6894 			state->mpc.mpcc_bot_gain_mode[i] = 0; /* Standard gain mode */
6895 
6896 			/* MPCC blending tree connections - capture tree topology */
6897 			if (pipe_ctx->bottom_pipe) {
6898 				state->mpc.mpcc_bot_sel[i] = pipe_ctx->bottom_pipe->pipe_idx;
6899 			} else {
6900 				state->mpc.mpcc_bot_sel[i] = 0xF; /* No bottom connection */
6901 			}
6902 			state->mpc.mpcc_top_sel[i] = pipe_ctx->pipe_idx; /* This pipe's DPP ID */
6903 
6904 			/* MPCC output gamma control - capture gamma programming */
6905 			if (plane_state->gamma_correction.type != GAMMA_CS_TFM_1D && plane_state->gamma_correction.num_entries > 0) {
6906 				state->mpc.mpcc_ogam_mode[i] = 1; /* Gamma enabled */
6907 				state->mpc.mpcc_ogam_select[i] = 0; /* Bank A selection */
6908 				state->mpc.mpcc_ogam_pwl_disable[i] = 0; /* PWL enabled */
6909 			} else {
6910 				state->mpc.mpcc_ogam_mode[i] = 0; /* Bypass mode */
6911 				state->mpc.mpcc_ogam_select[i] = 0;
6912 				state->mpc.mpcc_ogam_pwl_disable[i] = 1; /* PWL disabled */
6913 			}
6914 
6915 			/* MPCC pipe assignment and operational status */
6916 			if (pipe_ctx->stream_res.opp) {
6917 				state->mpc.mpcc_opp_id[i] = pipe_ctx->stream_res.opp->inst;
6918 			} else {
6919 				state->mpc.mpcc_opp_id[i] = 0xF; /* No OPP assignment */
6920 			}
6921 
6922 			/* MPCC status indicators - active pipe state */
6923 			state->mpc.mpcc_idle[i] = 0; /* Active pipe - not idle */
6924 			state->mpc.mpcc_busy[i] = 1; /* Active pipe - busy processing */
6925 
6926 		} else {
6927 			/* Pipe not active - set disabled/idle state for all fields */
6928 			state->mpc.mpcc_mode[i] = 0;
6929 			state->mpc.mpcc_alpha_blend_mode[i] = 0;
6930 			state->mpc.mpcc_alpha_multiplied_mode[i] = 0;
6931 			state->mpc.mpcc_blnd_active_overlap_only[i] = 0;
6932 			state->mpc.mpcc_global_alpha[i] = 0;
6933 			state->mpc.mpcc_global_gain[i] = 0;
6934 			state->mpc.mpcc_bg_bpc[i] = 0;
6935 			state->mpc.mpcc_bot_gain_mode[i] = 0;
6936 			state->mpc.mpcc_bot_sel[i] = 0xF; /* No bottom connection */
6937 			state->mpc.mpcc_top_sel[i] = 0xF; /* No top connection */
6938 			state->mpc.mpcc_ogam_mode[i] = 0; /* Bypass */
6939 			state->mpc.mpcc_ogam_select[i] = 0;
6940 			state->mpc.mpcc_ogam_pwl_disable[i] = 1; /* PWL disabled */
6941 			state->mpc.mpcc_opp_id[i] = 0xF; /* No OPP assignment */
6942 			state->mpc.mpcc_idle[i] = 1; /* Idle */
6943 			state->mpc.mpcc_busy[i] = 0; /* Not busy */
6944 		}
6945 	}
6946 
6947 	/* Capture OPP programming state for each pipe - comprehensive register field coverage */
6948 	for (i = 0; i < MAX_PIPES && i < dc->res_pool->pipe_count; i++) {
6949 		struct pipe_ctx *pipe_ctx = &res_ctx->pipe_ctx[i];
6950 
6951 		if (!pipe_ctx->stream)
6952 			continue;
6953 
6954 		if (pipe_ctx->stream_res.opp) {
6955 			struct dc_crtc_timing *timing = &pipe_ctx->stream->timing;
6956 
6957 			/* OPP Pipe Control */
6958 			state->opp[i].opp_pipe_clock_enable = 1; /* Active pipe has clock enabled */
6959 
6960 			/* Display Pattern Generator (DPG) Control - 19 fields */
6961 			if (pipe_ctx->stream->test_pattern.type != DP_TEST_PATTERN_VIDEO_MODE) {
6962 				state->opp[i].dpg_enable = 1;
6963 			} else {
6964 				/* Video mode - DPG disabled */
6965 				state->opp[i].dpg_enable = 0;
6966 			}
6967 
6968 			/* Format Control (FMT) - 18 fields */
6969 			state->opp[i].fmt_pixel_encoding = timing->pixel_encoding;
6970 
6971 			/* Chroma subsampling mode based on pixel encoding */
6972 			if (timing->pixel_encoding == PIXEL_ENCODING_YCBCR420) {
6973 				state->opp[i].fmt_subsampling_mode = 1; /* 4:2:0 subsampling */
6974 			} else if (timing->pixel_encoding == PIXEL_ENCODING_YCBCR422) {
6975 				state->opp[i].fmt_subsampling_mode = 2; /* 4:2:2 subsampling */
6976 			} else {
6977 				state->opp[i].fmt_subsampling_mode = 0; /* No subsampling (4:4:4) */
6978 			}
6979 
6980 			state->opp[i].fmt_cbcr_bit_reduction_bypass = (timing->pixel_encoding == PIXEL_ENCODING_RGB) ? 1 : 0;
6981 			state->opp[i].fmt_stereosync_override = (timing->timing_3d_format != TIMING_3D_FORMAT_NONE) ? 1 : 0;
6982 
6983 			/* Dithering control based on bit depth */
6984 			if (timing->display_color_depth < COLOR_DEPTH_121212) {
6985 				state->opp[i].fmt_spatial_dither_frame_counter_max = 15; /* Typical frame counter max */
6986 				state->opp[i].fmt_spatial_dither_frame_counter_bit_swap = 0; /* No bit swapping */
6987 				state->opp[i].fmt_spatial_dither_enable = 1;
6988 				state->opp[i].fmt_spatial_dither_mode = 0; /* Spatial dithering mode */
6989 				state->opp[i].fmt_spatial_dither_depth = timing->display_color_depth;
6990 				state->opp[i].fmt_temporal_dither_enable = 0; /* Spatial dithering preferred */
6991 			} else {
6992 				state->opp[i].fmt_spatial_dither_frame_counter_max = 0;
6993 				state->opp[i].fmt_spatial_dither_frame_counter_bit_swap = 0;
6994 				state->opp[i].fmt_spatial_dither_enable = 0;
6995 				state->opp[i].fmt_spatial_dither_mode = 0;
6996 				state->opp[i].fmt_spatial_dither_depth = 0;
6997 				state->opp[i].fmt_temporal_dither_enable = 0;
6998 			}
6999 
7000 			/* Truncation control for bit depth reduction */
7001 			if (timing->display_color_depth < COLOR_DEPTH_121212) {
7002 				state->opp[i].fmt_truncate_enable = 1;
7003 				state->opp[i].fmt_truncate_depth = timing->display_color_depth;
7004 				state->opp[i].fmt_truncate_mode = 0; /* Round mode */
7005 			} else {
7006 				state->opp[i].fmt_truncate_enable = 0;
7007 				state->opp[i].fmt_truncate_depth = 0;
7008 				state->opp[i].fmt_truncate_mode = 0;
7009 			}
7010 
7011 			/* Data clamping control */
7012 			state->opp[i].fmt_clamp_data_enable = 1; /* Clamping typically enabled */
7013 			state->opp[i].fmt_clamp_color_format = timing->pixel_encoding;
7014 
7015 			/* Dynamic expansion for limited range content */
7016 			if (timing->pixel_encoding != PIXEL_ENCODING_RGB) {
7017 				state->opp[i].fmt_dynamic_exp_enable = 1; /* YCbCr typically needs expansion */
7018 				state->opp[i].fmt_dynamic_exp_mode = 0; /* Standard expansion */
7019 			} else {
7020 				state->opp[i].fmt_dynamic_exp_enable = 0; /* RGB typically full range */
7021 				state->opp[i].fmt_dynamic_exp_mode = 0;
7022 			}
7023 
7024 			/* Legacy field for compatibility */
7025 			state->opp[i].fmt_bit_depth_control = timing->display_color_depth;
7026 
7027 			/* Output Buffer (OPPBUF) Control - 6 fields */
7028 			state->opp[i].oppbuf_active_width = timing->h_addressable;
7029 			state->opp[i].oppbuf_pixel_repetition = 0; /* No pixel repetition by default */
7030 
7031 			/* Multi-Stream Output (MSO) / ODM segmentation */
7032 			if (pipe_ctx->next_odm_pipe) {
7033 				state->opp[i].oppbuf_display_segmentation = 1; /* Segmented display */
7034 				state->opp[i].oppbuf_overlap_pixel_num = 0; /* ODM overlap pixels */
7035 			} else {
7036 				state->opp[i].oppbuf_display_segmentation = 0; /* Single segment */
7037 				state->opp[i].oppbuf_overlap_pixel_num = 0;
7038 			}
7039 
7040 			/* 3D/Stereo control */
7041 			if (timing->timing_3d_format != TIMING_3D_FORMAT_NONE) {
7042 				state->opp[i].oppbuf_3d_vact_space1_size = 30; /* Typical stereo blanking */
7043 				state->opp[i].oppbuf_3d_vact_space2_size = 30;
7044 			} else {
7045 				state->opp[i].oppbuf_3d_vact_space1_size = 0;
7046 				state->opp[i].oppbuf_3d_vact_space2_size = 0;
7047 			}
7048 
7049 			/* DSC Forward Config - 3 fields */
7050 			if (timing->dsc_cfg.num_slices_h > 0) {
7051 				state->opp[i].dscrm_dsc_forward_enable = 1;
7052 				state->opp[i].dscrm_dsc_opp_pipe_source = pipe_ctx->stream_res.opp->inst;
7053 				state->opp[i].dscrm_dsc_forward_enable_status = 1; /* Status follows enable */
7054 			} else {
7055 				state->opp[i].dscrm_dsc_forward_enable = 0;
7056 				state->opp[i].dscrm_dsc_opp_pipe_source = 0;
7057 				state->opp[i].dscrm_dsc_forward_enable_status = 0;
7058 			}
7059 		} else {
7060 			/* No OPP resource - set all fields to disabled state */
7061 			memset(&state->opp[i], 0, sizeof(state->opp[i]));
7062 		}
7063 	}
7064 
7065 	/* Capture OPTC programming state for each pipe - comprehensive register field coverage */
7066 	for (i = 0; i < MAX_PIPES && i < dc->res_pool->pipe_count; i++) {
7067 		struct pipe_ctx *pipe_ctx = &res_ctx->pipe_ctx[i];
7068 
7069 		if (!pipe_ctx->stream)
7070 			continue;
7071 
7072 		if (pipe_ctx->stream_res.tg) {
7073 			struct dc_crtc_timing *timing = &pipe_ctx->stream->timing;
7074 
7075 			state->optc[i].otg_master_inst = pipe_ctx->stream_res.tg->inst;
7076 
7077 			/* OTG_CONTROL register - 5 fields */
7078 			state->optc[i].otg_master_enable = 1; /* Active stream */
7079 			state->optc[i].otg_disable_point_cntl = 0; /* Normal operation */
7080 			state->optc[i].otg_start_point_cntl = 0; /* Normal start */
7081 			state->optc[i].otg_field_number_cntl = (timing->flags.INTERLACE) ? 1 : 0;
7082 			state->optc[i].otg_out_mux = 0; /* Direct output */
7083 
7084 			/* OTG Horizontal Timing - 7 fields */
7085 			state->optc[i].otg_h_total = timing->h_total;
7086 			state->optc[i].otg_h_blank_start = timing->h_addressable;
7087 			state->optc[i].otg_h_blank_end = timing->h_total - timing->h_front_porch;
7088 			state->optc[i].otg_h_sync_start = timing->h_addressable + timing->h_front_porch;
7089 			state->optc[i].otg_h_sync_end = timing->h_addressable + timing->h_front_porch + timing->h_sync_width;
7090 			state->optc[i].otg_h_sync_polarity = timing->flags.HSYNC_POSITIVE_POLARITY ? 0 : 1;
7091 			state->optc[i].otg_h_timing_div_mode = (pipe_ctx->next_odm_pipe) ? 1 : 0; /* ODM divide mode */
7092 
7093 			/* OTG Vertical Timing - 7 fields */
7094 			state->optc[i].otg_v_total = timing->v_total;
7095 			state->optc[i].otg_v_blank_start = timing->v_addressable;
7096 			state->optc[i].otg_v_blank_end = timing->v_total - timing->v_front_porch;
7097 			state->optc[i].otg_v_sync_start = timing->v_addressable + timing->v_front_porch;
7098 			state->optc[i].otg_v_sync_end = timing->v_addressable + timing->v_front_porch + timing->v_sync_width;
7099 			state->optc[i].otg_v_sync_polarity = timing->flags.VSYNC_POSITIVE_POLARITY ? 0 : 1;
7100 			state->optc[i].otg_v_sync_mode = 0; /* Normal sync mode */
7101 
7102 			/* Initialize remaining core fields with appropriate defaults */
7103 			// TODO: Update logic for accurate vtotal min/max
7104 			state->optc[i].otg_v_total_max = timing->v_total + 100; /* Typical DRR range */
7105 			state->optc[i].otg_v_total_min = timing->v_total - 50;
7106 			state->optc[i].otg_v_total_mid = timing->v_total;
7107 
7108 			/* ODM configuration */
7109 			// TODO: Update logic to have complete ODM mappings (e.g. 3:1 and 4:1) stored in single pipe
7110 			if (pipe_ctx->next_odm_pipe) {
7111 				state->optc[i].optc_seg0_src_sel = pipe_ctx->stream_res.opp ? pipe_ctx->stream_res.opp->inst : 0;
7112 				state->optc[i].optc_seg1_src_sel = pipe_ctx->next_odm_pipe->stream_res.opp ? pipe_ctx->next_odm_pipe->stream_res.opp->inst : 0;
7113 				state->optc[i].optc_num_of_input_segment = 1; /* 2 segments - 1 */
7114 			} else {
7115 				state->optc[i].optc_seg0_src_sel = pipe_ctx->stream_res.opp ? pipe_ctx->stream_res.opp->inst : 0;
7116 				state->optc[i].optc_seg1_src_sel = 0;
7117 				state->optc[i].optc_num_of_input_segment = 0; /* Single segment */
7118 			}
7119 
7120 			/* DSC configuration */
7121 			if (timing->dsc_cfg.num_slices_h > 0) {
7122 				state->optc[i].optc_dsc_mode = 1; /* DSC enabled */
7123 				state->optc[i].optc_dsc_bytes_per_pixel = timing->dsc_cfg.bits_per_pixel / 16; /* Convert to bytes */
7124 				state->optc[i].optc_dsc_slice_width = timing->h_addressable / timing->dsc_cfg.num_slices_h;
7125 			} else {
7126 				state->optc[i].optc_dsc_mode = 0;
7127 				state->optc[i].optc_dsc_bytes_per_pixel = 0;
7128 				state->optc[i].optc_dsc_slice_width = 0;
7129 			}
7130 
7131 			/* Essential control fields */
7132 			state->optc[i].otg_stereo_enable = (timing->timing_3d_format != TIMING_3D_FORMAT_NONE) ? 1 : 0;
7133 			state->optc[i].otg_interlace_enable = timing->flags.INTERLACE ? 1 : 0;
7134 			state->optc[i].otg_clock_enable = 1; /* OTG clock enabled */
7135 			state->optc[i].vtg0_enable = 1; /* VTG enabled for timing generation */
7136 
7137 			/* Initialize other key fields to defaults */
7138 			state->optc[i].optc_input_pix_clk_en = 1;
7139 			state->optc[i].optc_segment_width = (pipe_ctx->next_odm_pipe) ? (timing->h_addressable / 2) : timing->h_addressable;
7140 			state->optc[i].otg_vready_offset = 1;
7141 			state->optc[i].otg_vstartup_start = timing->v_addressable + 10;
7142 			state->optc[i].otg_vupdate_offset = 0;
7143 			state->optc[i].otg_vupdate_width = 5;
7144 		} else {
7145 			/* No timing generator resource - initialize all fields to 0 */
7146 			memset(&state->optc[i], 0, sizeof(state->optc[i]));
7147 		}
7148 	}
7149 
7150 	state->state_valid = true;
7151 	return true;
7152 }
7153 
7154 void dc_log_preos_dmcub_info(const struct dc *dc)
7155 {
7156 	dc_dmub_srv_log_preos_dmcub_info(dc->ctx->dmub_srv);
7157 }
7158 
7159 bool dc_get_qos_info(struct dc *dc, struct dc_qos_info *info)
7160 {
7161 	const struct dc_clocks *clk = &dc->current_state->bw_ctx.bw.dcn.clk;
7162 	struct memory_qos qos;
7163 
7164 	memset(info, 0, sizeof(*info));
7165 
7166 	// Check if measurement function is available
7167 	if (!dc->hwss.measure_memory_qos) {
7168 		return false;
7169 	}
7170 
7171 	// Call unified measurement function
7172 	dc->hwss.measure_memory_qos(dc, &qos);
7173 
7174 	// Populate info from measured qos
7175 	info->actual_peak_bw_in_mbps = qos.peak_bw_mbps;
7176 	info->actual_avg_bw_in_mbps = qos.avg_bw_mbps;
7177 	info->actual_min_latency_in_ns = qos.min_latency_ns;
7178 	info->actual_max_latency_in_ns = qos.max_latency_ns;
7179 	info->actual_avg_latency_in_ns = qos.avg_latency_ns;
7180 	info->dcn_bandwidth_ub_in_mbps = (uint32_t)(clk->fclk_khz / 1000 * 64);
7181 
7182 	return true;
7183 }
7184 
7185 enum update_v3_flow {
7186 	UPDATE_V3_FLOW_INVALID,
7187 	UPDATE_V3_FLOW_NO_NEW_CONTEXT_CONTEXT_FAST,
7188 	UPDATE_V3_FLOW_NO_NEW_CONTEXT_CONTEXT_FULL,
7189 	UPDATE_V3_FLOW_NEW_CONTEXT_SEAMLESS,
7190 	UPDATE_V3_FLOW_NEW_CONTEXT_MINIMAL_NEW,
7191 	UPDATE_V3_FLOW_NEW_CONTEXT_MINIMAL_CURRENT,
7192 };
7193 
7194 struct dc_update_scratch_space {
7195 	struct dc *dc;
7196 	struct dc_surface_update *surface_updates;
7197 	int surface_count;
7198 	struct dc_stream_state *stream;
7199 	struct dc_stream_update *stream_update;
7200 	bool update_v3;
7201 	bool do_clear_update_flags;
7202 	enum surface_update_type update_type;
7203 	struct dc_state *new_context;
7204 	enum update_v3_flow flow;
7205 	struct dc_state *backup_context;
7206 	struct dc_state *intermediate_context;
7207 	struct pipe_split_policy_backup intermediate_policy;
7208 	struct dc_surface_update intermediate_updates[MAX_SURFACES];
7209 	int intermediate_count;
7210 };
7211 
7212 size_t dc_update_scratch_space_size(void)
7213 {
7214 	return sizeof(struct dc_update_scratch_space);
7215 }
7216 
7217 static bool update_planes_and_stream_prepare_v2(
7218 		struct dc_update_scratch_space *scratch
7219 )
7220 {
7221 	// v2 is too tangled to break into stages, so just execute everything under lock
7222 	dc_exit_ips_for_hw_access(scratch->dc);
7223 	return update_planes_and_stream_v2(
7224 		scratch->dc,
7225 		scratch->surface_updates,
7226 		scratch->surface_count,
7227 		scratch->stream,
7228 		scratch->stream_update
7229 	);
7230 }
7231 
7232 static void update_planes_and_stream_execute_v2(
7233 		const struct dc_update_scratch_space *scratch
7234 )
7235 {
7236 	// Nothing to do, see `update_planes_and_stream_prepare_v2`
7237 	(void) scratch;
7238 }
7239 
7240 static bool update_planes_and_stream_cleanup_v2(
7241 		const struct dc_update_scratch_space *scratch
7242 )
7243 {
7244 	if (scratch->do_clear_update_flags)
7245 		clear_update_flags(scratch->surface_updates, scratch->surface_count, scratch->stream);
7246 
7247 	return false;
7248 }
7249 
7250 static void update_planes_and_stream_cleanup_v3_release_minimal(
7251 		struct dc_update_scratch_space *scratch,
7252 		bool backup
7253 );
7254 
7255 static bool update_planes_and_stream_prepare_v3_intermediate_seamless(
7256 		struct dc_update_scratch_space *scratch
7257 )
7258 {
7259 	return is_pipe_topology_transition_seamless_with_intermediate_step(
7260 			scratch->dc,
7261 			scratch->dc->current_state,
7262 			scratch->intermediate_context,
7263 			scratch->new_context
7264 	);
7265 }
7266 
7267 static void transition_countdown_init(struct dc *dc)
7268 {
7269 	dc->check_config.transition_countdown_to_steady_state =
7270 			dc->debug.num_fast_flips_to_steady_state_override ?
7271 			dc->debug.num_fast_flips_to_steady_state_override :
7272 			NUM_FAST_FLIPS_TO_STEADY_STATE;
7273 }
7274 
7275 static bool update_planes_and_stream_prepare_v3(
7276 		struct dc_update_scratch_space *scratch
7277 )
7278 {
7279 	if (scratch->flow == UPDATE_V3_FLOW_NEW_CONTEXT_SEAMLESS) {
7280 		return true;
7281 	}
7282 	ASSERT(scratch->flow == UPDATE_V3_FLOW_INVALID);
7283 	dc_exit_ips_for_hw_access(scratch->dc);
7284 
7285 	if (!update_planes_and_stream_state(
7286 			scratch->dc,
7287 			scratch->surface_updates,
7288 			scratch->surface_count,
7289 			scratch->stream,
7290 			scratch->stream_update,
7291 			&scratch->update_type,
7292 			&scratch->new_context
7293 	)) {
7294 		return false;
7295 	}
7296 
7297 	if (scratch->new_context == scratch->dc->current_state) {
7298 		ASSERT(scratch->update_type < UPDATE_TYPE_FULL);
7299 
7300 		// TODO: Do we need this to be alive in execute?
7301 		struct dc_fast_update fast_update[MAX_SURFACES] = { 0 };
7302 
7303 		populate_fast_updates(
7304 				fast_update,
7305 				scratch->surface_updates,
7306 				scratch->surface_count,
7307 				scratch->stream_update
7308 		);
7309 		const bool fast = fast_update_only(
7310 				scratch->dc,
7311 				fast_update,
7312 				scratch->surface_updates,
7313 				scratch->surface_count,
7314 				scratch->stream_update,
7315 				scratch->stream
7316 		)
7317 		// TODO: Can this be used to skip `populate_fast_updates`?
7318 				&& !scratch->dc->check_config.enable_legacy_fast_update;
7319 		scratch->flow = fast
7320 				? UPDATE_V3_FLOW_NO_NEW_CONTEXT_CONTEXT_FAST
7321 				: UPDATE_V3_FLOW_NO_NEW_CONTEXT_CONTEXT_FULL;
7322 		return true;
7323 	}
7324 
7325 	ASSERT(scratch->update_type >= UPDATE_TYPE_FULL);
7326 
7327 	const bool seamless = scratch->dc->hwss.is_pipe_topology_transition_seamless(
7328 			scratch->dc,
7329 			scratch->dc->current_state,
7330 			scratch->new_context
7331 	);
7332 	if (seamless) {
7333 		scratch->flow = UPDATE_V3_FLOW_NEW_CONTEXT_SEAMLESS;
7334 		if (scratch->dc->check_config.deferred_transition_state)
7335 			/* reset countdown as steady state not reached */
7336 			transition_countdown_init(scratch->dc);
7337 		return true;
7338 	}
7339 
7340 	if (!scratch->dc->debug.disable_deferred_minimal_transitions) {
7341 		scratch->dc->check_config.deferred_transition_state = true;
7342 		transition_countdown_init(scratch->dc);
7343 	}
7344 
7345 	scratch->intermediate_context = create_minimal_transition_state(
7346 		scratch->dc,
7347 		scratch->new_context,
7348 		&scratch->intermediate_policy
7349 	);
7350 	if (scratch->intermediate_context) {
7351 		if (update_planes_and_stream_prepare_v3_intermediate_seamless(scratch)) {
7352 			scratch->flow = UPDATE_V3_FLOW_NEW_CONTEXT_MINIMAL_NEW;
7353 			return true;
7354 		}
7355 
7356 		update_planes_and_stream_cleanup_v3_release_minimal(scratch, false);
7357 	}
7358 
7359 	scratch->backup_context = scratch->dc->current_state;
7360 	restore_planes_and_stream_state(&scratch->dc->scratch.current_state, scratch->stream);
7361 	dc_state_retain(scratch->backup_context);
7362 	scratch->intermediate_context = create_minimal_transition_state(
7363 			scratch->dc,
7364 			scratch->backup_context,
7365 			&scratch->intermediate_policy
7366 	);
7367 	if (scratch->intermediate_context) {
7368 		if (update_planes_and_stream_prepare_v3_intermediate_seamless(scratch)) {
7369 			scratch->flow = UPDATE_V3_FLOW_NEW_CONTEXT_MINIMAL_CURRENT;
7370 			scratch->intermediate_count = initialize_empty_surface_updates(
7371 					scratch->stream, scratch->intermediate_updates
7372 			);
7373 			return true;
7374 		}
7375 
7376 		update_planes_and_stream_cleanup_v3_release_minimal(scratch, true);
7377 	}
7378 
7379 	scratch->flow = UPDATE_V3_FLOW_INVALID;
7380 	dc_state_release(scratch->backup_context);
7381 	restore_planes_and_stream_state(&scratch->dc->scratch.new_state, scratch->stream);
7382 	return false;
7383 }
7384 
7385 static void update_planes_and_stream_execute_v3_commit(
7386 		const struct dc_update_scratch_space *scratch,
7387 		bool intermediate_update,
7388 		bool intermediate_context,
7389 		bool use_stream_update
7390 )
7391 {
7392 	commit_planes_for_stream(
7393 			scratch->dc,
7394 			intermediate_update ? scratch->intermediate_updates : scratch->surface_updates,
7395 			intermediate_update ? scratch->intermediate_count : scratch->surface_count,
7396 			scratch->stream,
7397 			use_stream_update ? scratch->stream_update : NULL,
7398 			intermediate_context ? UPDATE_TYPE_FULL : scratch->update_type,
7399 			// `dc->current_state` only used in `NO_NEW_CONTEXT`, where it is equal to `new_context`
7400 			intermediate_context ? scratch->intermediate_context : scratch->new_context
7401 	);
7402 }
7403 
7404 static void update_planes_and_stream_execute_v3(
7405 		const struct dc_update_scratch_space *scratch
7406 )
7407 {
7408 	switch (scratch->flow) {
7409 	case UPDATE_V3_FLOW_NO_NEW_CONTEXT_CONTEXT_FAST:
7410 		commit_planes_for_stream_fast(
7411 				scratch->dc,
7412 				scratch->surface_updates,
7413 				scratch->surface_count,
7414 				scratch->stream,
7415 				scratch->stream_update,
7416 				scratch->update_type,
7417 				scratch->new_context
7418 		);
7419 		break;
7420 
7421 	case UPDATE_V3_FLOW_NO_NEW_CONTEXT_CONTEXT_FULL:
7422 	case UPDATE_V3_FLOW_NEW_CONTEXT_SEAMLESS:
7423 		update_planes_and_stream_execute_v3_commit(scratch, false, false, true);
7424 		break;
7425 
7426 	case UPDATE_V3_FLOW_NEW_CONTEXT_MINIMAL_NEW:
7427 		update_planes_and_stream_execute_v3_commit(scratch, false, true,
7428 				scratch->dc->check_config.deferred_transition_state);
7429 		break;
7430 
7431 	case UPDATE_V3_FLOW_NEW_CONTEXT_MINIMAL_CURRENT:
7432 		update_planes_and_stream_execute_v3_commit(scratch, true, true, false);
7433 		break;
7434 
7435 	case UPDATE_V3_FLOW_INVALID:
7436 	default:
7437 		ASSERT(false);
7438 	}
7439 }
7440 
7441 static void update_planes_and_stream_cleanup_v3_release_minimal(
7442 		struct dc_update_scratch_space *scratch,
7443 		bool backup
7444 )
7445 {
7446 	release_minimal_transition_state(
7447 			scratch->dc,
7448 			scratch->intermediate_context,
7449 			backup ? scratch->backup_context : scratch->new_context,
7450 			&scratch->intermediate_policy
7451 	);
7452 }
7453 
7454 static void update_planes_and_stream_cleanup_v3_intermediate(
7455 		struct dc_update_scratch_space *scratch,
7456 		bool backup
7457 )
7458 {
7459 	swap_and_release_current_context(scratch->dc, scratch->intermediate_context, scratch->stream);
7460 	dc_state_retain(scratch->dc->current_state);
7461 	update_planes_and_stream_cleanup_v3_release_minimal(scratch, backup);
7462 }
7463 
7464 static bool update_planes_and_stream_cleanup_v3(
7465 		struct dc_update_scratch_space *scratch
7466 )
7467 {
7468 	switch (scratch->flow) {
7469 	case UPDATE_V3_FLOW_NO_NEW_CONTEXT_CONTEXT_FAST:
7470 	case UPDATE_V3_FLOW_NO_NEW_CONTEXT_CONTEXT_FULL:
7471 		if (scratch->dc->check_config.transition_countdown_to_steady_state)
7472 			scratch->dc->check_config.transition_countdown_to_steady_state--;
7473 		break;
7474 
7475 	case UPDATE_V3_FLOW_NEW_CONTEXT_SEAMLESS:
7476 		swap_and_release_current_context(scratch->dc, scratch->new_context, scratch->stream);
7477 		break;
7478 
7479 	case UPDATE_V3_FLOW_NEW_CONTEXT_MINIMAL_NEW:
7480 		update_planes_and_stream_cleanup_v3_intermediate(scratch, false);
7481 		if (scratch->dc->check_config.deferred_transition_state) {
7482 			dc_state_release(scratch->new_context);
7483 		} else {
7484 			scratch->flow = UPDATE_V3_FLOW_NEW_CONTEXT_SEAMLESS;
7485 			return true;
7486 		}
7487 		break;
7488 
7489 	case UPDATE_V3_FLOW_NEW_CONTEXT_MINIMAL_CURRENT:
7490 		update_planes_and_stream_cleanup_v3_intermediate(scratch, true);
7491 		dc_state_release(scratch->backup_context);
7492 		restore_planes_and_stream_state(&scratch->dc->scratch.new_state, scratch->stream);
7493 		scratch->flow = UPDATE_V3_FLOW_NEW_CONTEXT_SEAMLESS;
7494 		return true;
7495 
7496 	case UPDATE_V3_FLOW_INVALID:
7497 	default:
7498 		ASSERT(false);
7499 	}
7500 
7501 	if (scratch->do_clear_update_flags)
7502 		clear_update_flags(scratch->surface_updates, scratch->surface_count, scratch->stream);
7503 
7504 	return false;
7505 }
7506 
7507 struct dc_update_scratch_space *dc_update_planes_and_stream_init(
7508 		struct dc *dc,
7509 		struct dc_surface_update *surface_updates,
7510 		int surface_count,
7511 		struct dc_stream_state *stream,
7512 		struct dc_stream_update *stream_update
7513 )
7514 {
7515 	const enum dce_version version = dc->ctx->dce_version;
7516 	struct dc_update_scratch_space *scratch = stream->update_scratch;
7517 
7518 	*scratch = (struct dc_update_scratch_space){
7519 		.dc = dc,
7520 		.surface_updates = surface_updates,
7521 		.surface_count = surface_count,
7522 		.stream = stream,
7523 		.stream_update = stream_update,
7524 		.update_v3 = version >= DCN_VERSION_4_01 || version == DCN_VERSION_3_2 || version == DCN_VERSION_3_21,
7525 		.do_clear_update_flags = version >= DCN_VERSION_1_0,
7526 	};
7527 
7528 	return scratch;
7529 }
7530 
7531 bool dc_update_planes_and_stream_prepare(
7532 		struct dc_update_scratch_space *scratch
7533 )
7534 {
7535 	return scratch->update_v3
7536 			? update_planes_and_stream_prepare_v3(scratch)
7537 			: update_planes_and_stream_prepare_v2(scratch);
7538 }
7539 
7540 void dc_update_planes_and_stream_execute(
7541 		const struct dc_update_scratch_space *scratch
7542 )
7543 {
7544 	scratch->update_v3
7545 			? update_planes_and_stream_execute_v3(scratch)
7546 			: update_planes_and_stream_execute_v2(scratch);
7547 }
7548 
7549 bool dc_update_planes_and_stream_cleanup(
7550 		struct dc_update_scratch_space *scratch
7551 )
7552 {
7553 	return scratch->update_v3
7554 			? update_planes_and_stream_cleanup_v3(scratch)
7555 			: update_planes_and_stream_cleanup_v2(scratch);
7556 }
7557 
7558