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