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