xref: /linux/drivers/gpu/drm/amd/display/dc/core/dc.c (revision 1fc5a74b108fc90951890ec513ac81869f5eaff1)
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 
elevate_update_type(struct dc_update_descriptor * descriptor,enum dc_update_type new_type,enum dc_lock_descriptor new_locks)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 
destroy_links(struct dc * dc)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 
get_num_of_internal_disp(struct dc_link ** links,uint32_t num_links)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 
get_seamless_boot_stream_count(struct dc_state * ctx)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 
create_links(struct dc * dc,uint32_t num_virtual_links)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  */
create_link_encoders(struct dc * dc)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  */
destroy_link_encoders(struct dc * dc)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 
dc_perf_trace_create(void)412 static struct dc_perf_trace *dc_perf_trace_create(void)
413 {
414 	return kzalloc_obj(struct dc_perf_trace);
415 }
416 
dc_perf_trace_destroy(struct dc_perf_trace ** perf_trace)417 static void dc_perf_trace_destroy(struct dc_perf_trace **perf_trace)
418 {
419 	kfree(*perf_trace);
420 	*perf_trace = NULL;
421 }
422 
set_long_vtotal(struct dc * dc,struct dc_stream_state * stream,struct dc_crtc_timing_adjust * adjust)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  */
dc_stream_get_last_used_drr_vtotal(struct dc * dc,struct dc_stream_state * stream,uint32_t * refresh_rate)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
dc_stream_forward_dmub_crc_window(struct dc_dmub_srv * dmub_srv,struct rect * rect,struct otg_phy_mux * mux_mapping,bool is_stop)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
dc_stream_forward_dmcu_crc_window(struct dmcu * dmcu,struct rect * rect,struct otg_phy_mux * mux_mapping,bool is_stop)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
dc_stream_forward_crc_window(struct dc_stream_state * stream,struct rect * rect,uint8_t phy_id,bool is_stop)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
dc_stream_forward_dmub_multiple_crc_window(struct dc_dmub_srv * dmub_srv,struct crc_window * window,struct otg_phy_mux * mux_mapping,bool stop)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
dc_stream_forward_multiple_crc_window(struct dc_stream_state * stream,struct crc_window * window,uint8_t phy_id,bool stop)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  */
dc_stream_configure_crc(struct dc * dc,struct dc_stream_state * stream,struct crc_params * crc_window,bool enable,bool continuous,uint8_t idx,bool reset,enum crc_poly_mode crc_poly_mode)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  */
dc_stream_get_crc(struct dc * dc,struct dc_stream_state * stream,uint8_t idx,uint32_t * r_cr,uint32_t * g_y,uint32_t * b_cb)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 
dc_stream_set_dyn_expansion(struct dc * dc,struct dc_stream_state * stream,enum dc_dynamic_expansion option)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 
dc_stream_set_dither_option(struct dc_stream_state * stream,enum dc_dither_option option)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 
dc_stream_set_gamut_remap(struct dc * dc,const struct dc_stream_state * stream)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 
dc_stream_program_csc_matrix(struct dc * dc,struct dc_stream_state * stream)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 
dc_stream_set_static_screen_params(struct dc * dc,struct dc_stream_state ** streams,int num_streams,const struct dc_static_screen_params * params)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 
dc_destruct_update_scratch_pool(struct dc * dc)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 
dc_construct_update_scratch_pool(struct dc * dc)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 
dc_destruct(struct dc * dc)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 
dc_construct_ctx(struct dc * dc,const struct dc_init_data * init_params)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 
dc_construct(struct dc * dc,const struct dc_init_data * init_params)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 
disable_all_writeback_pipes_for_stream(const struct dc * dc,struct dc_stream_state * stream,struct dc_state * context)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 
apply_ctx_interdependent_lock(struct dc * dc,struct dc_state * context,struct dc_stream_state * stream,bool lock)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 
dc_update_visual_confirm_color(struct dc * dc,struct dc_state * context,struct pipe_ctx * pipe_ctx)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 
dc_get_visual_confirm_for_stream(struct dc * dc,struct dc_stream_state * stream_state,struct tg_color * color)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  */
dc_stream_adjust_vmin_vmax(struct dc * dc,struct dc_stream_state * stream,struct dc_crtc_timing_adjust * adjust)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 
disable_dangling_plane(struct dc * dc,struct dc_state * context)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 
disable_vbios_mode_if_required(struct dc * dc,struct dc_state * context)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 
dc_create(const struct dc_init_data * init_params)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 
detect_edp_presence(struct dc * dc)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 
dc_hardware_init(struct dc * dc)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 
dc_init_callbacks(struct dc * dc,const struct dc_callback_init * init_params)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 
dc_deinit_callbacks(struct dc * dc)1656 void dc_deinit_callbacks(struct dc *dc)
1657 {
1658 	memset(&dc->ctx->cp_psp, 0, sizeof(dc->ctx->cp_psp));
1659 }
1660 
dc_destroy(struct dc ** dc)1661 void dc_destroy(struct dc **dc)
1662 {
1663 	dc_destruct(*dc);
1664 	kvfree(*dc);
1665 	*dc = NULL;
1666 }
1667 
enable_timing_multisync(struct dc * dc,struct dc_state * ctx)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 
program_timing_sync(struct dc * dc,struct dc_state * ctx)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 
streams_changed(struct dc * dc,struct dc_stream_state * streams[],uint8_t stream_count)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 
dc_validate_boot_timing(const struct dc * dc,const struct dc_sink * sink,struct dc_crtc_timing * crtc_timing)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 
should_update_pipe_for_stream(struct dc_state * context,struct pipe_ctx * pipe_ctx,struct dc_stream_state * stream)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 
should_update_pipe_for_plane(struct dc_state * context,struct pipe_ctx * pipe_ctx,struct dc_plane_state * plane_state)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 
dc_enable_stereo(struct dc * dc,struct dc_state * context,struct dc_stream_state * streams[],uint8_t stream_count)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 
dc_trigger_sync(struct dc * dc,struct dc_state * context)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 
get_stream_mask(struct dc * dc,struct dc_state * context)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 
dc_z10_restore(const struct dc * dc)2208 void dc_z10_restore(const struct dc *dc)
2209 {
2210 	if (dc->hwss.z10_restore)
2211 		dc->hwss.z10_restore(dc);
2212 }
2213 
dc_z10_save_init(struct dc * dc)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  */
determine_pipe_unlock_order(struct dc * dc,struct dc_state * context)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  */
dc_commit_state_no_check(struct dc * dc,struct dc_state * context)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  */
dc_commit_streams(struct dc * dc,struct dc_commit_streams_params * params)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 
dc_acquire_release_mpc_3dlut(struct dc * dc,bool acquire,struct dc_stream_state * stream,struct dc_3dlut ** lut,struct dc_transfer_func ** shaper)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 
is_flip_pending_in_pipes(struct dc * dc,struct dc_state * context)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  */
process_deferred_updates(struct dc * dc)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 
dc_post_update_surfaces_to_stream(struct dc * dc)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 
dc_get_default_tiling_info(const struct dc * dc,struct dc_tiling_info * tiling_info)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 
dc_set_generic_gpio_for_stereo(bool enable,struct gpio_service * gpio_service)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 
is_surface_in_context(const struct dc_state * context,const struct dc_plane_state * plane_state)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 
get_plane_info_update_type(const struct dc_surface_update * u)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 
get_scaling_info_update_type(const struct dc_check_config * check_config,const struct dc_surface_update * u)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 
det_surface_update(const struct dc_check_config * check_config,struct dc_surface_update * u)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  */
force_immediate_gsl_plane_flip(struct dc * dc,struct dc_surface_update * updates,int surface_count)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 
check_update_surfaces_for_stream(const struct dc_check_config * check_config,struct dc_surface_update * updates,int surface_count,struct dc_stream_update * stream_update)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->vrr_infopacket ||
3202 				stream_update->vsc_infopacket ||
3203 				stream_update->vsp_infopacket ||
3204 				stream_update->hfvsif_infopacket ||
3205 				stream_update->adaptive_sync_infopacket ||
3206 				stream_update->vtem_infopacket ||
3207 				stream_update->avi_infopacket) {
3208 			su_flags->bits.info_frame = 1;
3209 			elevate_update_type(&overall_type, UPDATE_TYPE_FAST, LOCK_DESCRIPTOR_STREAM);
3210 		}
3211 
3212 		if (stream_update->hdr_static_metadata && stream_update->stream->use_dynamic_meta) {
3213 			su_flags->bits.dmdata = 1;
3214 			elevate_update_type(&overall_type, UPDATE_TYPE_FAST, LOCK_DESCRIPTOR_STREAM);
3215 		}
3216 
3217 		if (stream_update->output_csc_transform) {
3218 			su_flags->bits.out_csc = 1;
3219 			elevate_update_type(&overall_type, UPDATE_TYPE_FAST, LOCK_DESCRIPTOR_STREAM);
3220 		}
3221 
3222 		if (!check_config->enable_legacy_fast_update && stream_update->out_transfer_func) {
3223 			su_flags->bits.out_tf = 1;
3224 			elevate_update_type(&overall_type, UPDATE_TYPE_FAST, LOCK_DESCRIPTOR_STREAM);
3225 		}
3226 
3227 		if (stream_update->periodic_interrupt) {
3228 			su_flags->bits.periodic_interrupt = 1;
3229 			elevate_update_type(&overall_type, UPDATE_TYPE_FAST, LOCK_DESCRIPTOR_STREAM);
3230 		}
3231 
3232 		if (stream_update->dither_option) {
3233 			su_flags->bits.dither = 1;
3234 			elevate_update_type(&overall_type, UPDATE_TYPE_FAST, LOCK_DESCRIPTOR_STREAM);
3235 		}
3236 
3237 		if (stream_update->cursor_attributes) {
3238 			su_flags->bits.cursor_attr = 1;
3239 			elevate_update_type(&overall_type, UPDATE_TYPE_FAST, LOCK_DESCRIPTOR_STREAM);
3240 		}
3241 
3242 		if (stream_update->cursor_position) {
3243 			su_flags->bits.cursor_pos = 1;
3244 			elevate_update_type(&overall_type, UPDATE_TYPE_FAST, LOCK_DESCRIPTOR_STREAM);
3245 		}
3246 	}
3247 
3248 	for (int i = 0 ; i < surface_count; i++) {
3249 		struct dc_update_descriptor inner_type =
3250 				det_surface_update(check_config, &updates[i]);
3251 
3252 		elevate_update_type(&overall_type, inner_type.update_type, inner_type.lock_descriptor);
3253 	}
3254 
3255 	return overall_type;
3256 }
3257 
3258 /**
3259  * dc_check_state_update - Classify a dc_state_update by locking / re-entrancy requirements.
3260  * @check_config:  ASIC capabilities and display configuration context
3261  * @updates:       root update object describing the full desired commit
3262  *
3263  * Determines whether the update requires a fast, medium, or full lock
3264  * by inspecting the stream, stream_update, and surface_updates carried on
3265  * the root object. A probe update elevates the result to at least MED with
3266  * the PROBE lock, so a probe-carrying commit takes the probe mutex.
3267  *
3268  * Return: dc_update_descriptor with update_type and lock_descriptor.
3269  */
dc_check_state_update(const struct dc_check_config * check_config,const struct dc_state_update * updates)3270 struct dc_update_descriptor dc_check_state_update(
3271 		const struct dc_check_config *check_config,
3272 		const struct dc_state_update *updates)
3273 {
3274 	struct dc_update_descriptor desc = {0};
3275 
3276 	if (updates->stream_update)
3277 		stream_update_flags_clear(&updates->stream_update->stream->update_flags);
3278 	for (int i = 0; i < updates->surface_count; i++)
3279 		dc_pipe_update_bits_clear(&updates->surface_updates[i].surface->update_bits);
3280 
3281 	desc = check_update_surfaces_for_stream(check_config, updates->surface_updates,
3282 			updates->surface_count, updates->stream_update);
3283 
3284 	if (updates->probe_updates && updates->probe_updates->probe_count > 0)
3285 		elevate_update_type(&desc, UPDATE_TYPE_MED, LOCK_DESCRIPTOR_PROBE);
3286 
3287 	return desc;
3288 }
3289 
3290 /**
3291  * dc_check_update_surfaces_for_stream - Shim for dc_check_state_update.
3292  * @check_config:   ASIC capabilities and display configuration context
3293  * @updates:        array of surface update descriptors
3294  * @surface_count:  number of entries in @updates
3295  * @stream_update:  optional stream update
3296  *
3297  * Packs the individual arguments into a dc_state_update and forwards to
3298  * dc_check_state_update(). Preserved for out-of-tree and incremental callers.
3299  *
3300  * Return: dc_update_descriptor with update_type and lock_descriptor.
3301  */
dc_check_update_surfaces_for_stream(const struct dc_check_config * check_config,struct dc_surface_update * updates,int surface_count,struct dc_stream_update * stream_update)3302 struct dc_update_descriptor dc_check_update_surfaces_for_stream(
3303 		const struct dc_check_config *check_config,
3304 		struct dc_surface_update *updates,
3305 		int surface_count,
3306 		struct dc_stream_update *stream_update)
3307 {
3308 	struct dc_state_update root = {
3309 		.stream          = stream_update ? stream_update->stream : NULL,
3310 		.stream_update   = stream_update,
3311 		.surface_updates = updates,
3312 		.surface_count   = surface_count,
3313 		.probe_updates   = NULL
3314 	};
3315 
3316 	return dc_check_state_update(check_config, &root);
3317 }
3318 
stream_get_status(struct dc_state * ctx,struct dc_stream_state * stream)3319 static struct dc_stream_status *stream_get_status(
3320 	struct dc_state *ctx,
3321 	struct dc_stream_state *stream)
3322 {
3323 	uint8_t i;
3324 
3325 	for (i = 0; i < ctx->stream_count; i++) {
3326 		if (stream == ctx->streams[i]) {
3327 			return &ctx->stream_status[i];
3328 		}
3329 	}
3330 
3331 	return NULL;
3332 }
3333 
3334 static const enum dc_update_type update_surface_trace_level = UPDATE_TYPE_FULL;
3335 
copy_surface_update_to_plane(struct dc_plane_state * surface,struct dc_surface_update * srf_update)3336 static void copy_surface_update_to_plane(
3337 		struct dc_plane_state *surface,
3338 		struct dc_surface_update *srf_update)
3339 {
3340 	if (srf_update->flip_addr) {
3341 		surface->address = srf_update->flip_addr->address;
3342 		surface->flip_immediate =
3343 			srf_update->flip_addr->flip_immediate;
3344 		surface->time.time_elapsed_in_us[surface->time.index] =
3345 			(unsigned int)(srf_update->flip_addr->flip_timestamp_in_us -
3346 				surface->time.prev_update_time_in_us);
3347 		surface->time.prev_update_time_in_us =
3348 			(unsigned int)srf_update->flip_addr->flip_timestamp_in_us;
3349 		surface->time.index++;
3350 		if (surface->time.index >= DC_PLANE_UPDATE_TIMES_MAX)
3351 			surface->time.index = 0;
3352 
3353 		surface->triplebuffer_flips = srf_update->flip_addr->triplebuffer_flips;
3354 	}
3355 
3356 	if (srf_update->scaling_info) {
3357 		surface->scaling_quality =
3358 				srf_update->scaling_info->scaling_quality;
3359 		surface->dst_rect =
3360 				srf_update->scaling_info->dst_rect;
3361 		surface->src_rect =
3362 				srf_update->scaling_info->src_rect;
3363 		surface->clip_rect =
3364 				srf_update->scaling_info->clip_rect;
3365 	}
3366 
3367 	if (srf_update->plane_info) {
3368 		surface->color_space =
3369 				srf_update->plane_info->color_space;
3370 		surface->format =
3371 				srf_update->plane_info->format;
3372 		surface->plane_size =
3373 				srf_update->plane_info->plane_size;
3374 		surface->rotation =
3375 				srf_update->plane_info->rotation;
3376 		surface->horizontal_mirror =
3377 				srf_update->plane_info->horizontal_mirror;
3378 		surface->stereo_format =
3379 				srf_update->plane_info->stereo_format;
3380 		surface->tiling_info =
3381 				srf_update->plane_info->tiling_info;
3382 		surface->visible =
3383 				srf_update->plane_info->visible;
3384 		surface->per_pixel_alpha =
3385 				srf_update->plane_info->per_pixel_alpha;
3386 		surface->global_alpha =
3387 				srf_update->plane_info->global_alpha;
3388 		surface->global_alpha_value =
3389 				srf_update->plane_info->global_alpha_value;
3390 		surface->dcc =
3391 				srf_update->plane_info->dcc;
3392 		surface->layer_index =
3393 				srf_update->plane_info->layer_index;
3394 		surface->scaling_linearity =
3395 				srf_update->plane_info->scaling_linearity;
3396 		surface->cositing =
3397 				srf_update->plane_info->cositing;
3398 	}
3399 
3400 	if (srf_update->gamma) {
3401 		memcpy(&surface->gamma_correction.entries,
3402 			&srf_update->gamma->entries,
3403 			sizeof(struct dc_gamma_entries));
3404 		surface->gamma_correction.is_identity =
3405 			srf_update->gamma->is_identity;
3406 		surface->gamma_correction.num_entries =
3407 			srf_update->gamma->num_entries;
3408 		surface->gamma_correction.type =
3409 			srf_update->gamma->type;
3410 	}
3411 	if (srf_update->cm_hist_control) {
3412 		memcpy(&surface->cm_hist_control,
3413 			srf_update->cm_hist_control,
3414 			sizeof(surface->cm_hist_control));
3415 	}
3416 
3417 	if (srf_update->in_transfer_func) {
3418 		surface->in_transfer_func.sdr_ref_white_level =
3419 			srf_update->in_transfer_func->sdr_ref_white_level;
3420 		surface->in_transfer_func.tf =
3421 			srf_update->in_transfer_func->tf;
3422 		surface->in_transfer_func.type =
3423 			srf_update->in_transfer_func->type;
3424 		memcpy(&surface->in_transfer_func.tf_pts,
3425 			&srf_update->in_transfer_func->tf_pts,
3426 			sizeof(struct dc_transfer_func_distributed_points));
3427 	}
3428 
3429 	/* Shaper, 3DLUT, 1DLUT */
3430 	if (srf_update->cm) {
3431 		struct kref refcount = surface->cm.refcount;
3432 
3433 		memcpy(&surface->cm, srf_update->cm, sizeof(surface->cm));
3434 		surface->cm.refcount = refcount;
3435 
3436 	}
3437 
3438 	if (srf_update->hdr_mult.value)
3439 		surface->hdr_mult =
3440 				srf_update->hdr_mult;
3441 
3442 	if (srf_update->sdr_white_level_nits)
3443 		surface->sdr_white_level_nits =
3444 				srf_update->sdr_white_level_nits;
3445 
3446 	if (srf_update->input_csc_color_matrix)
3447 		surface->input_csc_color_matrix =
3448 			*srf_update->input_csc_color_matrix;
3449 
3450 	if (srf_update->coeff_reduction_factor)
3451 		surface->coeff_reduction_factor =
3452 			*srf_update->coeff_reduction_factor;
3453 
3454 	if (srf_update->gamut_remap_matrix)
3455 		surface->gamut_remap_matrix =
3456 			*srf_update->gamut_remap_matrix;
3457 
3458 	if (srf_update->cursor_csc_color_matrix)
3459 		surface->cursor_csc_color_matrix =
3460 			*srf_update->cursor_csc_color_matrix;
3461 
3462 	if (srf_update->bias_and_scale.bias_and_scale_valid)
3463 			surface->bias_and_scale =
3464 					srf_update->bias_and_scale;
3465 }
3466 
copy_stream_update_to_stream(struct dc * dc,struct dc_state * context,struct dc_stream_state * stream,struct dc_stream_update * update)3467 static void copy_stream_update_to_stream(struct dc *dc,
3468 					 struct dc_state *context,
3469 					 struct dc_stream_state *stream,
3470 					 struct dc_stream_update *update)
3471 {
3472 	(void)context;
3473 	struct dc_context *dc_ctx = dc->ctx;
3474 
3475 	if (update == NULL || stream == NULL)
3476 		return;
3477 
3478 	if (update->src.height && update->src.width)
3479 		stream->src = update->src;
3480 
3481 	if (update->dst.height && update->dst.width)
3482 		stream->dst = update->dst;
3483 
3484 	if (update->out_transfer_func) {
3485 		stream->out_transfer_func.sdr_ref_white_level =
3486 			update->out_transfer_func->sdr_ref_white_level;
3487 		stream->out_transfer_func.tf = update->out_transfer_func->tf;
3488 		stream->out_transfer_func.type =
3489 			update->out_transfer_func->type;
3490 		memcpy(&stream->out_transfer_func.tf_pts,
3491 		       &update->out_transfer_func->tf_pts,
3492 		       sizeof(struct dc_transfer_func_distributed_points));
3493 	}
3494 
3495 	if (update->hdr_static_metadata)
3496 		stream->hdr_static_metadata = *update->hdr_static_metadata;
3497 
3498 	if (update->abm_level)
3499 		stream->abm_level = *update->abm_level;
3500 
3501 	if (update->periodic_interrupt)
3502 		stream->periodic_interrupt = *update->periodic_interrupt;
3503 
3504 	if (update->gamut_remap)
3505 		stream->gamut_remap_matrix = *update->gamut_remap;
3506 
3507 	/* Note: this being updated after mode set is currently not a use case
3508 	 * however if it arises OCSC would need to be reprogrammed at the
3509 	 * minimum
3510 	 */
3511 	if (update->output_color_space)
3512 		stream->output_color_space = *update->output_color_space;
3513 
3514 	if (update->output_csc_transform)
3515 		stream->csc_color_matrix = *update->output_csc_transform;
3516 
3517 	if (update->vrr_infopacket)
3518 		stream->vrr_infopacket = *update->vrr_infopacket;
3519 
3520 	if (update->hw_cursor_req)
3521 		stream->hw_cursor_req = *update->hw_cursor_req;
3522 
3523 	if (update->allow_freesync)
3524 		stream->allow_freesync = *update->allow_freesync;
3525 
3526 	if (update->vrr_active_variable)
3527 		stream->vrr_active_variable = *update->vrr_active_variable;
3528 
3529 	if (update->vrr_active_fixed)
3530 		stream->vrr_active_fixed = *update->vrr_active_fixed;
3531 
3532 	if (update->crtc_timing_adjust) {
3533 		if (stream->adjust.v_total_min != update->crtc_timing_adjust->v_total_min ||
3534 			stream->adjust.v_total_max != update->crtc_timing_adjust->v_total_max ||
3535 			stream->adjust.timing_adjust_pending)
3536 			update->crtc_timing_adjust->timing_adjust_pending = true;
3537 		stream->adjust = *update->crtc_timing_adjust;
3538 		update->crtc_timing_adjust->timing_adjust_pending = false;
3539 	}
3540 
3541 	if (update->dpms_off)
3542 		stream->dpms_off = *update->dpms_off;
3543 
3544 	if (update->hfvsif_infopacket)
3545 		stream->hfvsif_infopacket = *update->hfvsif_infopacket;
3546 
3547 	if (update->vtem_infopacket)
3548 		stream->vtem_infopacket = *update->vtem_infopacket;
3549 
3550 	if (update->vsc_infopacket)
3551 		stream->vsc_infopacket = *update->vsc_infopacket;
3552 
3553 	if (update->vsp_infopacket)
3554 		stream->vsp_infopacket = *update->vsp_infopacket;
3555 
3556 	if (update->adaptive_sync_infopacket)
3557 		stream->adaptive_sync_infopacket = *update->adaptive_sync_infopacket;
3558 
3559 	if (update->avi_infopacket)
3560 		stream->avi_infopacket = *update->avi_infopacket;
3561 
3562 	if (update->dither_option)
3563 		stream->dither_option = *update->dither_option;
3564 
3565 	if (update->pending_test_pattern)
3566 		stream->test_pattern = *update->pending_test_pattern;
3567 	/* update current stream with writeback info */
3568 	if (update->wb_update) {
3569 		unsigned int i;
3570 
3571 		stream->num_wb_info = update->wb_update->num_wb_info;
3572 		ASSERT(stream->num_wb_info <= MAX_DWB_PIPES);
3573 		for (i = 0; i < stream->num_wb_info; i++)
3574 			stream->writeback_info[i] =
3575 				update->wb_update->writeback_info[i];
3576 	}
3577 	if (update->dsc_config) {
3578 		struct dc_dsc_config old_dsc_cfg = stream->timing.dsc_cfg;
3579 		uint32_t old_dsc_enabled = stream->timing.flags.DSC;
3580 		uint32_t enable_dsc = (update->dsc_config->num_slices_h != 0 &&
3581 				       update->dsc_config->num_slices_v != 0);
3582 
3583 		/* Use temporarry context for validating new DSC config */
3584 		struct dc_state *dsc_validate_context = dc_state_create_copy(dc->current_state);
3585 
3586 		if (dsc_validate_context) {
3587 			stream->timing.dsc_cfg = *update->dsc_config;
3588 			stream->timing.flags.DSC = enable_dsc;
3589 			if (dc->res_pool->funcs->validate_bandwidth(dc, dsc_validate_context,
3590 				DC_VALIDATE_MODE_ONLY) != DC_OK) {
3591 				stream->timing.dsc_cfg = old_dsc_cfg;
3592 				stream->timing.flags.DSC = old_dsc_enabled;
3593 				update->dsc_config = NULL;
3594 			}
3595 
3596 			dc_state_release(dsc_validate_context);
3597 		} else {
3598 			DC_ERROR("Failed to allocate new validate context for DSC change\n");
3599 			update->dsc_config = NULL;
3600 		}
3601 	}
3602 	if (update->scaler_sharpener_update)
3603 		stream->scaler_sharpener_update = *update->scaler_sharpener_update;
3604 	if (update->sharpening_required)
3605 		stream->sharpening_required = *update->sharpening_required;
3606 
3607 	if (update->blending_linearity)
3608 		stream->blending_linearity = *update->blending_linearity;
3609 
3610 	if (update->drr_trigger_mode) {
3611 		stream->drr_trigger_mode = *update->drr_trigger_mode;
3612 	}
3613 }
3614 
backup_planes_and_stream_state(struct dc_scratch_space * scratch,struct dc_stream_state * stream)3615 static void backup_planes_and_stream_state(
3616 		struct dc_scratch_space *scratch,
3617 		struct dc_stream_state *stream)
3618 {
3619 	int i;
3620 	struct dc_stream_status *status = dc_stream_get_status(stream);
3621 
3622 	if (!status)
3623 		return;
3624 
3625 	for (i = 0; i < status->plane_count; i++) {
3626 		dc_plane_copy_config(&scratch->plane_states[i], status->plane_states[i]);
3627 	}
3628 	scratch->stream_state = *stream;
3629 }
3630 
restore_planes_and_stream_state(struct dc_scratch_space * scratch,struct dc_stream_state * stream)3631 static void restore_planes_and_stream_state(
3632 		struct dc_scratch_space *scratch,
3633 		struct dc_stream_state *stream)
3634 {
3635 	int i;
3636 	struct dc_stream_status *status = dc_stream_get_status(stream);
3637 
3638 	if (!status)
3639 		return;
3640 
3641 	for (i = 0; i < status->plane_count; i++) {
3642 		dc_plane_copy_config(status->plane_states[i], &scratch->plane_states[i]);
3643 	}
3644 
3645 	// refcount is persistent
3646 	struct kref temp_refcount = stream->refcount;
3647 	*stream = scratch->stream_state;
3648 	stream->refcount = temp_refcount;
3649 }
3650 
3651 /**
3652  * update_seamless_boot_flags() - Helper function for updating seamless boot flags
3653  *
3654  * @dc: Current DC state
3655  * @context: New DC state to be programmed
3656  * @surface_count: Number of surfaces that have an updated
3657  * @stream: Corresponding stream to be updated in the current flip
3658  *
3659  * Updating seamless boot flags do not need to be part of the commit sequence. This
3660  * helper function will update the seamless boot flags on each flip (if required)
3661  * outside of the HW commit sequence (fast or slow).
3662  *
3663  * Return: void
3664  */
update_seamless_boot_flags(struct dc * dc,struct dc_state * context,int surface_count,struct dc_stream_state * stream)3665 static void update_seamless_boot_flags(struct dc *dc,
3666 		struct dc_state *context,
3667 		int surface_count,
3668 		struct dc_stream_state *stream)
3669 {
3670 	if (get_seamless_boot_stream_count(context) > 0 && (surface_count > 0 || stream->dpms_off)) {
3671 		/* Optimize seamless boot flag keeps clocks and watermarks high until
3672 		 * first flip. After first flip, optimization is required to lower
3673 		 * bandwidth. Important to note that it is expected UEFI will
3674 		 * only light up a single display on POST, therefore we only expect
3675 		 * one stream with seamless boot flag set.
3676 		 */
3677 		if (stream->apply_seamless_boot_optimization) {
3678 			stream->apply_seamless_boot_optimization = false;
3679 
3680 			if (get_seamless_boot_stream_count(context) == 0)
3681 				dc->optimized_required = true;
3682 		}
3683 	}
3684 }
3685 
3686 static bool full_update_required_weak(
3687 		const struct dc *dc,
3688 		const struct dc_surface_update *srf_updates,
3689 		int surface_count,
3690 		const struct dc_stream_update *stream_update,
3691 		const struct dc_stream_state *stream);
3692 
backup_and_set_minimal_pipe_split_policy(struct dc * dc,struct dc_state * context,struct pipe_split_policy_backup * policy)3693 static void backup_and_set_minimal_pipe_split_policy(struct dc *dc,
3694 		struct dc_state *context,
3695 		struct pipe_split_policy_backup *policy)
3696 {
3697 	int i;
3698 
3699 	if (!dc->config.is_vmin_only_asic) {
3700 		policy->mpc_policy = dc->debug.pipe_split_policy;
3701 		dc->debug.pipe_split_policy = MPC_SPLIT_AVOID;
3702 	}
3703 	policy->dynamic_odm_policy = dc->debug.enable_single_display_2to1_odm_policy;
3704 	dc->debug.enable_single_display_2to1_odm_policy = false;
3705 	policy->subvp_policy = dc->debug.force_disable_subvp;
3706 	dc->debug.force_disable_subvp = true;
3707 	for (i = 0; i < context->stream_count; i++) {
3708 		policy->force_odm[i] = context->streams[i]->debug.force_odm_combine_segments;
3709 		if (context->streams[i]->debug.allow_transition_for_forced_odm)
3710 			context->streams[i]->debug.force_odm_combine_segments = 0;
3711 	}
3712 }
3713 
restore_minimal_pipe_split_policy(struct dc * dc,struct dc_state * context,struct pipe_split_policy_backup * policy)3714 static void restore_minimal_pipe_split_policy(struct dc *dc,
3715 		struct dc_state *context,
3716 		struct pipe_split_policy_backup *policy)
3717 {
3718 	uint8_t i;
3719 
3720 	if (!dc->config.is_vmin_only_asic)
3721 		dc->debug.pipe_split_policy = policy->mpc_policy;
3722 	dc->debug.enable_single_display_2to1_odm_policy =
3723 			policy->dynamic_odm_policy;
3724 	dc->debug.force_disable_subvp = policy->subvp_policy;
3725 	for (i = 0; i < context->stream_count; i++)
3726 		context->streams[i]->debug.force_odm_combine_segments = policy->force_odm[i];
3727 }
3728 
3729 /**
3730  * update_planes_and_stream_state() - The function takes planes and stream
3731  * updates as inputs and determines the appropriate update type. If update type
3732  * is FULL, the function allocates a new context, populates and validates it.
3733  * Otherwise, it updates current dc context. The function will return both
3734  * new_context and new_update_type back to the caller. The function also backs
3735  * up both current and new contexts into corresponding dc state scratch memory.
3736  * TODO: The function does too many things, and even conditionally allocates dc
3737  * context memory implicitly. We should consider to break it down.
3738  *
3739  * @dc: Current DC state
3740  * @srf_updates: an array of surface updates
3741  * @surface_count: surface update count
3742  * @stream: Corresponding stream to be updated
3743  * @stream_update: stream update
3744  * @update_descriptor: describes what plane and stream changes to apply
3745  * @new_update_type: [out] determined update type by the function
3746  * @new_context: [out] new context allocated and validated if update type is
3747  * FULL, reference to current context if update type is less than FULL.
3748  *
3749  * Return: true if a valid update is populated into new_context, false
3750  * otherwise.
3751  */
update_planes_and_stream_state(struct dc * dc,struct dc_surface_update * srf_updates,int surface_count,struct dc_stream_state * stream,struct dc_stream_update * stream_update,enum dc_update_type * new_update_type,struct dc_state ** new_context)3752 static bool update_planes_and_stream_state(struct dc *dc,
3753 		struct dc_surface_update *srf_updates, int surface_count,
3754 		struct dc_stream_state *stream,
3755 		struct dc_stream_update *stream_update,
3756 		enum dc_update_type *new_update_type,
3757 		struct dc_state **new_context)
3758 {
3759 	struct dc_state *context;
3760 	int i;
3761 	unsigned int j;
3762 	enum dc_update_type update_type;
3763 	const struct dc_stream_status *stream_status;
3764 	struct dc_context *dc_ctx = dc->ctx;
3765 
3766 	stream_status = dc_stream_get_status(stream);
3767 
3768 	if (!stream_status) {
3769 		if (surface_count) /* Only an error condition if surf_count non-zero*/
3770 			ASSERT(false);
3771 
3772 		return false; /* Cannot commit surface to stream that is not committed */
3773 	}
3774 
3775 	context = dc->current_state;
3776 	update_type = dc_check_update_surfaces_for_stream(
3777 			&dc->check_config, srf_updates, surface_count, stream_update).update_type;
3778 	if (full_update_required_weak(dc, srf_updates, surface_count, stream_update, stream))
3779 		update_type = UPDATE_TYPE_FULL;
3780 
3781 	/* It is possible to receive a flip for one plane while there are multiple flip_immediate planes in the same stream.
3782 	 * E.g. Desktop and MPO plane are flip_immediate but only the MPO plane received a flip
3783 	 * Force the other flip_immediate planes to flip so GSL doesn't wait for a flip that won't come.
3784 	 */
3785 	force_immediate_gsl_plane_flip(dc, srf_updates, surface_count);
3786 	if (update_type == UPDATE_TYPE_FULL)
3787 		backup_planes_and_stream_state(&dc->scratch.current_state, stream);
3788 
3789 	/* update current stream with the new updates */
3790 	copy_stream_update_to_stream(dc, context, stream, stream_update);
3791 
3792 	/* do not perform surface update if surface has invalid dimensions
3793 	 * (all zero) and no scaling_info is provided
3794 	 */
3795 	if (surface_count > 0) {
3796 		for (i = 0; i < surface_count; i++) {
3797 			if ((srf_updates[i].surface->src_rect.width == 0 ||
3798 				 srf_updates[i].surface->src_rect.height == 0 ||
3799 				 srf_updates[i].surface->dst_rect.width == 0 ||
3800 				 srf_updates[i].surface->dst_rect.height == 0) &&
3801 				(!srf_updates[i].scaling_info ||
3802 				  srf_updates[i].scaling_info->src_rect.width == 0 ||
3803 				  srf_updates[i].scaling_info->src_rect.height == 0 ||
3804 				  srf_updates[i].scaling_info->dst_rect.width == 0 ||
3805 				  srf_updates[i].scaling_info->dst_rect.height == 0)) {
3806 				DC_ERROR("Invalid src/dst rects in surface update!\n");
3807 				return false;
3808 			}
3809 		}
3810 	}
3811 
3812 	if (update_type == UPDATE_TYPE_FULL) {
3813 		if (stream_update) {
3814 			uint32_t dsc_changed = stream_update->stream->update_flags.bits.dsc_changed;
3815 			stream_update_flags_set_full(&stream_update->stream->update_flags);
3816 			stream_update->stream->update_flags.bits.dsc_changed = dsc_changed;
3817 		}
3818 		for (i = 0; i < surface_count; i++)
3819 			dc_pipe_update_bits_set_full(&srf_updates[i].surface->update_bits);
3820 	}
3821 
3822 	if (update_type >= update_surface_trace_level)
3823 		update_surface_trace(dc, srf_updates, surface_count);
3824 
3825 	for (i = 0; i < surface_count; i++)
3826 		copy_surface_update_to_plane(srf_updates[i].surface, &srf_updates[i]);
3827 
3828 	if (update_type >= UPDATE_TYPE_FULL) {
3829 		struct dc_plane_state *new_planes[MAX_SURFACES] = {0};
3830 
3831 		for (i = 0; i < surface_count; i++)
3832 			new_planes[i] = srf_updates[i].surface;
3833 
3834 		/* initialize scratch memory for building context */
3835 		context = dc_state_create_copy(dc->current_state);
3836 		if (context == NULL) {
3837 			DC_ERROR("Failed to allocate new validate context!\n");
3838 			return false;
3839 		}
3840 
3841 		/* For each full update, remove all existing phantom pipes first.
3842 		 * Ensures that we have enough pipes for newly added MPO planes
3843 		 */
3844 		dc_state_remove_phantom_streams_and_planes(dc, context);
3845 		dc_state_release_phantom_streams_and_planes(dc, context);
3846 
3847 		/*remove old surfaces from context */
3848 		if (!dc_state_rem_all_planes_for_stream(dc, stream, context)) {
3849 
3850 			BREAK_TO_DEBUGGER();
3851 			goto fail;
3852 		}
3853 
3854 		/* add surface to context */
3855 		if (!dc_state_add_all_planes_for_stream(dc, stream, new_planes, surface_count, context)) {
3856 
3857 			BREAK_TO_DEBUGGER();
3858 			goto fail;
3859 		}
3860 	}
3861 
3862 	/* save update parameters into surface */
3863 	for (i = 0; i < surface_count; i++) {
3864 		struct dc_plane_state *surface = srf_updates[i].surface;
3865 
3866 		if (update_type != UPDATE_TYPE_MED)
3867 			continue;
3868 		if (surface->update_bits.position_change) {
3869 			for (j = 0; j < dc->res_pool->pipe_count; j++) {
3870 				struct pipe_ctx *pipe_ctx = &context->res_ctx.pipe_ctx[j];
3871 
3872 				if (pipe_ctx->plane_state != surface)
3873 					continue;
3874 
3875 				resource_build_scaling_params(pipe_ctx);
3876 			}
3877 		}
3878 	}
3879 
3880 	if (update_type == UPDATE_TYPE_FULL) {
3881 		struct pipe_split_policy_backup policy;
3882 		bool minimize = false;
3883 
3884 		if (dc->check_config.deferred_transition_state) {
3885 			if (dc->check_config.transition_countdown_to_steady_state) {
3886 				/* During countdown, all new contexts created as minimal transition states */
3887 				minimize = true;
3888 			} else {
3889 				dc->check_config.deferred_transition_state = false;
3890 			}
3891 		}
3892 
3893 		if (minimize)
3894 			backup_and_set_minimal_pipe_split_policy(dc, context, &policy);
3895 
3896 		if (dc->res_pool->funcs->validate_bandwidth(dc, context, DC_VALIDATE_MODE_AND_PROGRAMMING) != DC_OK) {
3897 			if (minimize)
3898 				restore_minimal_pipe_split_policy(dc, context, &policy);
3899 			BREAK_TO_DEBUGGER();
3900 			goto fail;
3901 		}
3902 
3903 		if (minimize)
3904 			restore_minimal_pipe_split_policy(dc, context, &policy);
3905 	}
3906 	update_seamless_boot_flags(dc, context, surface_count, stream);
3907 
3908 	*new_context = context;
3909 	*new_update_type = update_type;
3910 	if (update_type == UPDATE_TYPE_FULL)
3911 		backup_planes_and_stream_state(&dc->scratch.new_state, stream);
3912 
3913 	return true;
3914 
3915 fail:
3916 	dc_state_release(context);
3917 
3918 	return false;
3919 
3920 }
3921 
program_cursor_attributes_sequence(struct dc * dc,struct dc_stream_state * stream,struct dc_state * context,struct block_sequence_state * seq_state)3922 static void program_cursor_attributes_sequence(
3923 		struct dc *dc,
3924 		struct dc_stream_state *stream,
3925 		struct dc_state *context,
3926 		struct block_sequence_state *seq_state)
3927 {
3928 	int k;
3929 	struct pipe_ctx *pipe_to_program = NULL;
3930 	bool enable_cursor_offload = dc_dmub_srv_is_cursor_offload_enabled(dc);
3931 
3932 	for (k = 0; k < (int)dc->res_pool->pipe_count; k++) {
3933 		struct pipe_ctx *tmp_pipe = &context->res_ctx.pipe_ctx[k];
3934 
3935 		if (tmp_pipe->stream != stream)
3936 			continue;
3937 
3938 		if (!pipe_to_program) {
3939 			pipe_to_program = tmp_pipe;
3940 
3941 			if (enable_cursor_offload && dc->hwss.begin_cursor_offload_update) {
3942 				hwss_add_begin_cursor_offload_update(seq_state, dc, tmp_pipe);
3943 			} else {
3944 				hwss_add_cursor_lock(seq_state, dc, pipe_to_program, true);
3945 				if (pipe_to_program->next_odm_pipe)
3946 					hwss_add_cursor_lock(seq_state, dc, pipe_to_program->next_odm_pipe, true);
3947 			}
3948 		}
3949 
3950 		hwss_add_set_cursor_attribute(seq_state, dc, tmp_pipe);
3951 		if (dc->ctx->dmub_srv)
3952 			hwss_add_send_update_cursor_info_to_dmu(seq_state, tmp_pipe, k);
3953 		if (dc->hwss.set_cursor_sdr_white_level)
3954 			hwss_add_set_cursor_sdr_white_level(seq_state, dc, tmp_pipe);
3955 		if (enable_cursor_offload && dc->hwss.update_cursor_offload_pipe)
3956 			hwss_add_update_cursor_offload_pipe(seq_state, dc, tmp_pipe);
3957 	}
3958 
3959 	if (pipe_to_program) {
3960 		if (enable_cursor_offload && dc->hwss.commit_cursor_offload_update) {
3961 			hwss_add_commit_cursor_offload_update(seq_state, dc, pipe_to_program);
3962 		} else {
3963 			hwss_add_cursor_lock(seq_state, dc, pipe_to_program, false);
3964 			if (pipe_to_program->next_odm_pipe)
3965 				hwss_add_cursor_lock(seq_state, dc, pipe_to_program->next_odm_pipe, false);
3966 		}
3967 	}
3968 }
3969 
program_cursor_position_sequence(struct dc * dc,struct dc_stream_state * stream,struct dc_state * context,struct block_sequence_state * seq_state)3970 static void program_cursor_position_sequence(
3971 		struct dc *dc,
3972 		struct dc_stream_state *stream,
3973 		struct dc_state *context,
3974 		struct block_sequence_state *seq_state)
3975 {
3976 	int k;
3977 	struct pipe_ctx *pipe_to_program = NULL;
3978 	bool enable_cursor_offload = dc_dmub_srv_is_cursor_offload_enabled(dc);
3979 
3980 	for (k = 0; k < (int)dc->res_pool->pipe_count; k++) {
3981 		struct pipe_ctx *tmp_pipe = &context->res_ctx.pipe_ctx[k];
3982 
3983 		if (tmp_pipe->stream != stream ||
3984 				(!tmp_pipe->plane_res.mi  && !tmp_pipe->plane_res.hubp) ||
3985 				!tmp_pipe->plane_state ||
3986 				(!tmp_pipe->plane_res.xfm && !tmp_pipe->plane_res.dpp) ||
3987 				(!tmp_pipe->plane_res.ipp && !tmp_pipe->plane_res.dpp))
3988 			continue;
3989 
3990 		if (!pipe_to_program) {
3991 			pipe_to_program = tmp_pipe;
3992 
3993 			if (enable_cursor_offload && dc->hwss.begin_cursor_offload_update)
3994 				hwss_add_begin_cursor_offload_update(seq_state, dc, tmp_pipe);
3995 			else
3996 				hwss_add_cursor_lock(seq_state, dc, pipe_to_program, true);
3997 		}
3998 
3999 		hwss_add_set_cursor_position(seq_state, dc, tmp_pipe);
4000 		if (enable_cursor_offload && dc->hwss.update_cursor_offload_pipe)
4001 			hwss_add_update_cursor_offload_pipe(seq_state, dc, tmp_pipe);
4002 
4003 		if (dc->ctx->dmub_srv)
4004 			hwss_add_send_update_cursor_info_to_dmu(seq_state, tmp_pipe, k);
4005 	}
4006 
4007 	if (pipe_to_program) {
4008 		if (enable_cursor_offload && dc->hwss.commit_cursor_offload_update)
4009 			hwss_add_commit_cursor_offload_update(seq_state, dc, pipe_to_program);
4010 		else
4011 			hwss_add_cursor_lock(seq_state, dc, pipe_to_program, false);
4012 	}
4013 }
4014 
add_update_info_frame_sequence(struct block_sequence_state * seq_state,struct pipe_ctx * pipe_ctx)4015 static void add_update_info_frame_sequence(
4016 		struct block_sequence_state *seq_state,
4017 		struct pipe_ctx *pipe_ctx)
4018 {
4019 	bool is_hdmi_tmds;
4020 	bool is_dp;
4021 	bool is_hdmi_frl;
4022 
4023 	if (!pipe_ctx || !pipe_ctx->stream)
4024 		return;
4025 
4026 	if (pipe_ctx->stream_res.stream_enc == NULL &&
4027 			pipe_ctx->stream_res.hpo_frl_stream_enc == NULL)
4028 		return;
4029 
4030 	is_hdmi_tmds = dc_is_hdmi_tmds_signal(pipe_ctx->stream->signal);
4031 	is_dp = dc_is_dp_signal(pipe_ctx->stream->signal);
4032 
4033 	is_hdmi_frl = dc_is_hdmi_frl_signal(pipe_ctx->stream->signal);
4034 	if (!is_hdmi_tmds && !is_dp && !is_hdmi_frl)
4035 		return;
4036 
4037 	if (is_hdmi_tmds) {
4038 		hwss_add_stream_enc_update_hdmi_info_packets(seq_state, pipe_ctx);
4039 		return;
4040 	}
4041 
4042 	if (is_hdmi_frl) {
4043 		hwss_add_hpo_frl_stream_enc_update_hdmi_info_packets(seq_state, pipe_ctx);
4044 		return;
4045 	}
4046 
4047 	if (is_dp) {
4048 		if (dp_is_128b_132b_signal(pipe_ctx)) {
4049 			hwss_add_hpo_dp_stream_enc_update_dp_info_packets_sdp_line_num(seq_state, pipe_ctx);
4050 			hwss_add_hpo_dp_stream_enc_update_dp_info_packets(seq_state, pipe_ctx);
4051 		} else {
4052 			hwss_add_stream_enc_update_dp_info_packets_sdp_line_num(seq_state, pipe_ctx);
4053 			hwss_add_stream_enc_update_dp_info_packets(seq_state, pipe_ctx);
4054 		}
4055 	}
4056 }
4057 
add_link_update_dsc_config_sequence(struct block_sequence_state * seq_state,struct pipe_ctx * pipe_ctx,struct dsc_config * dsc_cfg,struct dsc_optc_config * dsc_optc_cfg)4058 static void add_link_update_dsc_config_sequence(
4059 		struct block_sequence_state *seq_state,
4060 		struct pipe_ctx *pipe_ctx,
4061 		struct dsc_config *dsc_cfg,
4062 		struct dsc_optc_config *dsc_optc_cfg)
4063 {
4064 	struct display_stream_compressor *dsc = pipe_ctx->stream_res.dsc;
4065 	struct dc_stream_state *stream = pipe_ctx->stream;
4066 	struct dc *dc = stream->ctx->dc;
4067 	struct dccg *dccg = dc->res_pool->dccg;
4068 	struct pipe_ctx *top_pipe = pipe_ctx;
4069 	struct pipe_ctx *odm_pipe = NULL;
4070 	int opp_cnt = 1;
4071 	bool should_use_dto_dscclk = false;
4072 	struct dsc_config dsc_pps_cfg;
4073 	uint8_t *dsc_packed_pps = stream->dsc_packed_pps;
4074 	int last_dsc_set_config_step = 0;
4075 
4076 	if (!stream->timing.flags.DSC || !dsc)
4077 		return;
4078 
4079 	while (top_pipe->prev_odm_pipe)
4080 		top_pipe = top_pipe->prev_odm_pipe;
4081 
4082 	for (odm_pipe = top_pipe->next_odm_pipe; odm_pipe; odm_pipe = odm_pipe->next_odm_pipe)
4083 		opp_cnt++;
4084 
4085 	memset(dsc_cfg, 0, sizeof(*dsc_cfg));
4086 	memset(dsc_optc_cfg, 0, sizeof(*dsc_optc_cfg));
4087 
4088 	dsc_cfg->pic_width = (stream->timing.h_addressable +
4089 		top_pipe->dsc_padding_params.dsc_hactive_padding +
4090 		stream->timing.h_border_left +
4091 		stream->timing.h_border_right) / opp_cnt;
4092 	dsc_cfg->pic_height = stream->timing.v_addressable +
4093 		stream->timing.v_border_top +
4094 		stream->timing.v_border_bottom;
4095 	dsc_cfg->pixel_encoding = stream->timing.pixel_encoding;
4096 	dsc_cfg->color_depth = stream->timing.display_color_depth;
4097 	dsc_cfg->is_odm = top_pipe->next_odm_pipe ? true : false;
4098 	dsc_cfg->dc_dsc_cfg = stream->timing.dsc_cfg;
4099 	ASSERT(dsc_cfg->dc_dsc_cfg.num_slices_h % opp_cnt == 0);
4100 	dsc_cfg->dc_dsc_cfg.num_slices_h /= opp_cnt;
4101 	dsc_cfg->dsc_padding = 0;
4102 
4103 	if (dccg && dccg->funcs->set_dto_dscclk &&
4104 			stream->timing.pix_clk_100hz > 480000)
4105 		should_use_dto_dscclk = true;
4106 
4107 	if (should_use_dto_dscclk)
4108 		hwss_add_dccg_set_dto_dscclk(seq_state, dccg, dsc->inst,
4109 			dsc_cfg->dc_dsc_cfg.num_slices_h);
4110 
4111 	last_dsc_set_config_step = *seq_state->num_steps;
4112 	hwss_add_dsc_set_config(seq_state, dsc, dsc_cfg, dsc_optc_cfg);
4113 	hwss_add_dsc_enable_with_opp(seq_state, top_pipe);
4114 
4115 	for (odm_pipe = top_pipe->next_odm_pipe; odm_pipe; odm_pipe = odm_pipe->next_odm_pipe) {
4116 		struct display_stream_compressor *odm_dsc = odm_pipe->stream_res.dsc;
4117 
4118 		if (should_use_dto_dscclk)
4119 			hwss_add_dccg_set_dto_dscclk(seq_state, dccg, odm_dsc->inst,
4120 				dsc_cfg->dc_dsc_cfg.num_slices_h);
4121 
4122 		last_dsc_set_config_step = *seq_state->num_steps;
4123 		hwss_add_dsc_set_config(seq_state, odm_dsc, dsc_cfg, dsc_optc_cfg);
4124 		hwss_add_dsc_enable_with_opp(seq_state, odm_pipe);
4125 	}
4126 
4127 	if (dc_is_dp_signal(stream->signal) && !dp_is_128b_132b_signal(pipe_ctx))
4128 		hwss_add_stream_enc_dp_set_dsc_config(seq_state,
4129 			pipe_ctx->stream_res.stream_enc,
4130 			&seq_state->steps[last_dsc_set_config_step].params.dsc_set_config_simple_params.dsc_optc_cfg);
4131 
4132 	hwss_add_tg_set_dsc_config(seq_state, top_pipe->stream_res.tg,
4133 		&seq_state->steps[last_dsc_set_config_step].params.dsc_set_config_simple_params.dsc_optc_cfg, true);
4134 
4135 	memset(&dsc_pps_cfg, 0, sizeof(dsc_pps_cfg));
4136 	dsc_pps_cfg.pic_width = stream->timing.h_addressable +
4137 		stream->timing.h_border_left + stream->timing.h_border_right;
4138 	dsc_pps_cfg.pic_height = stream->timing.v_addressable +
4139 		stream->timing.v_border_top + stream->timing.v_border_bottom;
4140 	dsc_pps_cfg.pixel_encoding = stream->timing.pixel_encoding;
4141 	dsc_pps_cfg.color_depth = stream->timing.display_color_depth;
4142 	dsc_pps_cfg.is_odm = top_pipe->next_odm_pipe ? true : false;
4143 	dsc_pps_cfg.dc_dsc_cfg = stream->timing.dsc_cfg;
4144 	dsc_pps_cfg.dsc_padding = top_pipe->dsc_padding_params.dsc_hactive_padding;
4145 
4146 	if (dsc->funcs->dsc_get_packed_pps) {
4147 		dsc->funcs->dsc_get_packed_pps(dsc, &dsc_pps_cfg, dsc_packed_pps);
4148 
4149 		if (dc_is_dp_signal(stream->signal)) {
4150 			if (dp_is_128b_132b_signal(pipe_ctx))
4151 				hwss_add_hpo_dp_stream_enc_dp_set_dsc_pps_info_packet(seq_state,
4152 					pipe_ctx->stream_res.hpo_dp_stream_enc,
4153 					true, dsc_packed_pps, false);
4154 			else
4155 				hwss_add_stream_enc_dp_set_dsc_pps_info_packet(seq_state,
4156 					pipe_ctx->stream_res.stream_enc,
4157 					true, dsc_packed_pps, false);
4158 		}
4159 		else if (dc_is_hdmi_frl_signal(stream->signal)) {
4160 			hwss_add_hpo_frl_stream_enc_set_dsc_config(seq_state,
4161 				pipe_ctx->stream_res.hpo_frl_stream_enc,
4162 				&stream->timing,
4163 				dsc_packed_pps);
4164 		}
4165 	}
4166 }
4167 
commit_planes_do_stream_update_sequence(struct dc * dc,struct dc_stream_state * stream,struct dc_stream_update * stream_update,enum dc_update_type update_type,struct dc_state * context,struct block_sequence block_sequence[MAX_HWSS_BLOCK_SEQUENCE_SIZE],unsigned int * num_steps)4168 static void commit_planes_do_stream_update_sequence(struct dc *dc,
4169 		struct dc_stream_state *stream,
4170 		struct dc_stream_update *stream_update,
4171 		enum dc_update_type update_type,
4172 		struct dc_state *context,
4173 		struct block_sequence block_sequence[MAX_HWSS_BLOCK_SEQUENCE_SIZE],
4174 		unsigned int *num_steps)
4175 {
4176 	int j;
4177 	struct block_sequence_state seq_state = { .steps = block_sequence, .num_steps = num_steps };
4178 	unsigned int dsc_cfg_index = 0;
4179 	*num_steps = 0; // Initialize to 0
4180 
4181 	// Stream updates
4182 	for (j = 0; j < (int)dc->res_pool->pipe_count; j++) {
4183 		struct pipe_ctx *pipe_ctx = &context->res_ctx.pipe_ctx[j];
4184 
4185 		if (resource_is_pipe_type(pipe_ctx, OTG_MASTER) && pipe_ctx->stream == stream) {
4186 
4187 			if (stream_update->periodic_interrupt && dc->hwss.setup_periodic_interrupt)
4188 				hwss_add_setup_periodic_interrupt(&seq_state, dc, pipe_ctx);
4189 
4190 			if ((stream_update->hdr_static_metadata && !stream->use_dynamic_meta) ||
4191 					stream_update->vrr_infopacket ||
4192 					stream_update->vsc_infopacket ||
4193 					stream_update->vsp_infopacket ||
4194 					stream_update->hfvsif_infopacket ||
4195 					stream_update->adaptive_sync_infopacket ||
4196 					stream_update->vtem_infopacket ||
4197 					stream_update->avi_infopacket) {
4198 				resource_build_info_frame(pipe_ctx);
4199 				add_update_info_frame_sequence(&seq_state, pipe_ctx);
4200 
4201 				if (dc_is_dp_signal(pipe_ctx->stream->signal))
4202 					hwss_add_dp_trace_source_sequence(&seq_state,
4203 							pipe_ctx->stream->link,
4204 							DPCD_SOURCE_SEQ_AFTER_UPDATE_INFO_FRAME);
4205 			}
4206 
4207 			if (stream_update->hdr_static_metadata &&
4208 					stream->use_dynamic_meta &&
4209 					dc->hwss.set_dmdata_attributes &&
4210 					pipe_ctx->stream->dmdata_address.quad_part != 0)
4211 				hwss_add_set_dmdata_attributes(&seq_state, pipe_ctx);
4212 
4213 			if (stream_update->gamut_remap)
4214 				hwss_add_dpp_program_gamut_remap(&seq_state, pipe_ctx);
4215 
4216 			if (stream_update->output_csc_transform)
4217 				hwss_add_program_output_csc(&seq_state, dc, pipe_ctx,
4218 					stream->output_color_space,
4219 					stream->csc_color_matrix.matrix,
4220 					pipe_ctx->stream_res.opp->inst);
4221 
4222 			if (stream_update->dither_option) {
4223 				struct pipe_ctx *odm_pipe = pipe_ctx->next_odm_pipe;
4224 				resource_build_bit_depth_reduction_params(pipe_ctx->stream,
4225 								&pipe_ctx->stream->bit_depth_params);
4226 				hwss_add_opp_program_fmt(&seq_state, pipe_ctx->stream_res.opp,
4227 						&stream->bit_depth_params,
4228 						&stream->clamping);
4229 				while (odm_pipe) {
4230 					hwss_add_opp_program_fmt(&seq_state, odm_pipe->stream_res.opp,
4231 							&stream->bit_depth_params,
4232 							&stream->clamping);
4233 					odm_pipe = odm_pipe->next_odm_pipe;
4234 				}
4235 			}
4236 
4237 			if (stream_update->cursor_attributes)
4238 				program_cursor_attributes_sequence(dc, stream, context, &seq_state);
4239 
4240 			if (stream_update->cursor_position)
4241 				program_cursor_position_sequence(dc, stream, context, &seq_state);
4242 
4243 			/* Full fe update*/
4244 			if (update_type == UPDATE_TYPE_FAST)
4245 				continue;
4246 
4247 			if (stream_update->dsc_config)
4248 				if (dsc_cfg_index < MAX_PIPES) {
4249 					struct dsc_config dsc_cfg;
4250 					struct dsc_optc_config dsc_optc_cfg;
4251 
4252 					add_link_update_dsc_config_sequence(&seq_state,
4253 						pipe_ctx,
4254 						&dsc_cfg,
4255 						&dsc_optc_cfg);
4256 				}
4257 
4258 			if (stream_update->mst_bw_update) {
4259 				if (stream_update->mst_bw_update->is_increase)
4260 					hwss_add_link_increase_mst_payload(&seq_state,
4261 							pipe_ctx,
4262 							stream_update->mst_bw_update->mst_stream_bw);
4263 				else
4264 					hwss_add_link_reduce_mst_payload(&seq_state,
4265 							pipe_ctx,
4266 							stream_update->mst_bw_update->mst_stream_bw);
4267 			}
4268 
4269 			if (stream_update->pending_test_pattern) {
4270 				/*
4271 				 * test pattern params depends on ODM topology
4272 				 * changes that we could be applying to front
4273 				 * end. Since at the current stage front end
4274 				 * changes are not yet applied. We can only
4275 				 * apply test pattern in hw based on current
4276 				 * state and populate the final test pattern
4277 				 * params in new state. If current and new test
4278 				 * pattern params are different as result of
4279 				 * different ODM topology being used, it will be
4280 				 * detected and handle during front end
4281 				 * programming update.
4282 				 */
4283 				hwss_add_dp_set_test_pattern(&seq_state,
4284 					stream->link,
4285 					stream->test_pattern.type,
4286 					stream->test_pattern.color_space,
4287 					stream->test_pattern.p_link_settings,
4288 					stream->test_pattern.p_custom_pattern,
4289 					stream->test_pattern.cust_pattern_size);
4290 				resource_build_test_pattern_params(&context->res_ctx, pipe_ctx);
4291 			}
4292 
4293 			if (stream_update->dpms_off) {
4294 				// DPMS should not use partially updated pipe context
4295 				struct pipe_ctx *dpms_pipe_ctx = &dc->current_state->res_ctx.pipe_ctx[j];
4296 
4297 				if (*stream_update->dpms_off) {
4298 					hwss_add_link_set_dpms_off(&seq_state, dpms_pipe_ctx);
4299 					/* for dpms, keep acquired resources*/
4300 					if (dpms_pipe_ctx->stream_res.audio && !dc->debug.az_endpoint_mute_only)
4301 						hwss_add_disable_audio_stream(&seq_state, dpms_pipe_ctx);
4302 
4303 					hwss_add_dc_set_optimized_required(&seq_state, dc, true);
4304 
4305 				} else {
4306 					if (get_seamless_boot_stream_count(context) == 0 && dc->hwss.prepare_bandwidth_sequence)
4307 						dc->hwss.prepare_bandwidth_sequence(dc, dc->current_state, &seq_state);
4308 					hwss_add_link_set_dpms_on(&seq_state, dc->current_state, dpms_pipe_ctx);
4309 				}
4310 			} else if (pipe_ctx->stream->link->wa_flags.blank_stream_on_ocs_change && stream_update->output_color_space
4311 					&& !stream->dpms_off && dc_is_dp_signal(pipe_ctx->stream->signal)) {
4312 				/*
4313 				 * Workaround for firmware issue in some receivers where they don't pick up
4314 				 * correct output color space unless DP link is disabled/re-enabled
4315 				 */
4316 					hwss_add_link_set_dpms_on(&seq_state, dc->current_state, pipe_ctx);
4317 			}
4318 
4319 			if (stream_update->abm_level && pipe_ctx->stream_res.abm) {
4320 				bool should_program_abm = true;
4321 
4322 				// if otg funcs defined check if blanked before programming
4323 				if (pipe_ctx->stream_res.tg->funcs->is_blanked)
4324 					if (pipe_ctx->stream_res.tg->funcs->is_blanked(pipe_ctx->stream_res.tg))
4325 						should_program_abm = false;
4326 
4327 				if (should_program_abm) {
4328 					if (*stream_update->abm_level == ABM_LEVEL_IMMEDIATE_DISABLE) {
4329 						hwss_add_abm_set_immediate_disable(&seq_state, dc, pipe_ctx);
4330 					} else {
4331 						hwss_add_abm_set_level(&seq_state, pipe_ctx->stream_res.abm, stream->abm_level);
4332 					}
4333 				}
4334 			}
4335 		}
4336 	}
4337 }
4338 
commit_planes_do_stream_update(struct dc * dc,struct dc_stream_state * stream,struct dc_stream_update * stream_update,enum dc_update_type update_type,struct dc_state * context)4339 static void commit_planes_do_stream_update(struct dc *dc,
4340 		struct dc_stream_state *stream,
4341 		struct dc_stream_update *stream_update,
4342 		enum dc_update_type update_type,
4343 		struct dc_state *context)
4344 {
4345 	unsigned int j;
4346 
4347 	// Check if block sequence programming is enabled
4348 	if (dc->debug.enable_block_sequence_programming) {
4349 		unsigned int num_steps = 0;
4350 
4351 		// Build the block sequence using context's pre-allocated array
4352 		commit_planes_do_stream_update_sequence(dc, stream, stream_update,
4353 				update_type, context, context->block_sequence, &num_steps);
4354 
4355 		// Execute the block sequence
4356 		if (num_steps > 0)
4357 			hwss_execute_sequence(dc, context->block_sequence, num_steps);
4358 
4359 		return;
4360 	}
4361 
4362 	// Legacy path (existing implementation)
4363 	// Stream updates
4364 	for (j = 0; j < dc->res_pool->pipe_count; j++) {
4365 		struct pipe_ctx *pipe_ctx = &context->res_ctx.pipe_ctx[j];
4366 
4367 		if (resource_is_pipe_type(pipe_ctx, OTG_MASTER) && pipe_ctx->stream == stream) {
4368 
4369 			if (stream_update->periodic_interrupt && dc->hwss.setup_periodic_interrupt)
4370 				dc->hwss.setup_periodic_interrupt(dc, pipe_ctx);
4371 
4372 			if ((stream_update->hdr_static_metadata && !stream->use_dynamic_meta) ||
4373 					stream_update->vrr_infopacket ||
4374 					stream_update->vsc_infopacket ||
4375 					stream_update->vsp_infopacket ||
4376 					stream_update->hfvsif_infopacket ||
4377 					stream_update->adaptive_sync_infopacket ||
4378 					stream_update->vtem_infopacket ||
4379 					stream_update->avi_infopacket) {
4380 				resource_build_info_frame(pipe_ctx);
4381 				dc->hwss.update_info_frame(pipe_ctx);
4382 
4383 				if (dc_is_dp_signal(pipe_ctx->stream->signal))
4384 					dc->link_srv->dp_trace_source_sequence(
4385 							pipe_ctx->stream->link,
4386 							DPCD_SOURCE_SEQ_AFTER_UPDATE_INFO_FRAME);
4387 			}
4388 
4389 			if (stream_update->hdr_static_metadata &&
4390 					stream->use_dynamic_meta &&
4391 					dc->hwss.set_dmdata_attributes &&
4392 					pipe_ctx->stream->dmdata_address.quad_part != 0)
4393 				dc->hwss.set_dmdata_attributes(pipe_ctx);
4394 
4395 			if (stream_update->gamut_remap)
4396 				dc_stream_set_gamut_remap(dc, stream);
4397 
4398 			if (stream_update->output_csc_transform)
4399 				dc_stream_program_csc_matrix(dc, stream);
4400 
4401 			if (stream_update->dither_option) {
4402 				struct pipe_ctx *odm_pipe = pipe_ctx->next_odm_pipe;
4403 				resource_build_bit_depth_reduction_params(pipe_ctx->stream,
4404 									&pipe_ctx->stream->bit_depth_params);
4405 				pipe_ctx->stream_res.opp->funcs->opp_program_fmt(pipe_ctx->stream_res.opp,
4406 						&stream->bit_depth_params,
4407 						&stream->clamping);
4408 				while (odm_pipe) {
4409 					odm_pipe->stream_res.opp->funcs->opp_program_fmt(odm_pipe->stream_res.opp,
4410 							&stream->bit_depth_params,
4411 							&stream->clamping);
4412 					odm_pipe = odm_pipe->next_odm_pipe;
4413 				}
4414 			}
4415 
4416 			if (stream_update->cursor_attributes)
4417 				program_cursor_attributes(dc, stream);
4418 
4419 			if (stream_update->cursor_position)
4420 				program_cursor_position(dc, stream);
4421 
4422 			/* Full fe update*/
4423 			if (update_type == UPDATE_TYPE_FAST)
4424 				continue;
4425 
4426 			if (stream_update->dsc_config)
4427 				dc->link_srv->update_dsc_config(pipe_ctx);
4428 
4429 			if (stream_update->mst_bw_update) {
4430 				if (stream_update->mst_bw_update->is_increase)
4431 					dc->link_srv->increase_mst_payload(pipe_ctx,
4432 							stream_update->mst_bw_update->mst_stream_bw);
4433 				else
4434 					dc->link_srv->reduce_mst_payload(pipe_ctx,
4435 							stream_update->mst_bw_update->mst_stream_bw);
4436 			}
4437 
4438 			if (stream_update->pending_test_pattern) {
4439 				/*
4440 				 * test pattern params depends on ODM topology
4441 				 * changes that we could be applying to front
4442 				 * end. Since at the current stage front end
4443 				 * changes are not yet applied. We can only
4444 				 * apply test pattern in hw based on current
4445 				 * state and populate the final test pattern
4446 				 * params in new state. If current and new test
4447 				 * pattern params are different as result of
4448 				 * different ODM topology being used, it will be
4449 				 * detected and handle during front end
4450 				 * programming update.
4451 				 */
4452 				dc->link_srv->dp_set_test_pattern(stream->link,
4453 					stream->test_pattern.type,
4454 					stream->test_pattern.color_space,
4455 					stream->test_pattern.p_link_settings,
4456 					stream->test_pattern.p_custom_pattern,
4457 					stream->test_pattern.cust_pattern_size);
4458 				resource_build_test_pattern_params(&context->res_ctx, pipe_ctx);
4459 			}
4460 
4461 			// DPMS should not use partially updated pipe context
4462 			struct pipe_ctx *dpms_pipe_ctx = &dc->current_state->res_ctx.pipe_ctx[j];
4463 
4464 			if (stream_update->dpms_off) {
4465 				if (*stream_update->dpms_off) {
4466 					dc->link_srv->set_dpms_off(dpms_pipe_ctx);
4467 					/* for dpms, keep acquired resources*/
4468 					if (dpms_pipe_ctx->stream_res.audio && !dc->debug.az_endpoint_mute_only) {
4469 						struct audio *audio = dpms_pipe_ctx->stream_res.audio;
4470 
4471 						audio->funcs->az_disable(audio);
4472 					}
4473 
4474 					dc->optimized_required = true;
4475 
4476 				} else {
4477 					if (get_seamless_boot_stream_count(context) == 0)
4478 						dc->hwss.prepare_bandwidth(dc, dc->current_state);
4479 					dc->link_srv->set_dpms_on(dc->current_state, dpms_pipe_ctx);
4480 				}
4481 			} else if (dpms_pipe_ctx->stream->link->wa_flags.blank_stream_on_ocs_change &&
4482 					stream_update->output_color_space &&
4483 					!stream->dpms_off && dc_is_dp_signal(dpms_pipe_ctx->stream->signal)) {
4484 				/*
4485 				 * Workaround for firmware issue in some receivers where they don't pick up
4486 				 * correct output color space unless DP link is disabled/re-enabled
4487 				 */
4488 				dc->link_srv->set_dpms_on(dc->current_state, dpms_pipe_ctx);
4489 			}
4490 
4491 			if (stream_update->abm_level && pipe_ctx->stream_res.abm) {
4492 				bool should_program_abm = true;
4493 
4494 				// if otg funcs defined check if blanked before programming
4495 				if (pipe_ctx->stream_res.tg->funcs->is_blanked)
4496 					if (pipe_ctx->stream_res.tg->funcs->is_blanked(pipe_ctx->stream_res.tg))
4497 						should_program_abm = false;
4498 
4499 				if (should_program_abm) {
4500 					if (*stream_update->abm_level == ABM_LEVEL_IMMEDIATE_DISABLE) {
4501 						dc->hwss.set_abm_immediate_disable(pipe_ctx);
4502 					} else {
4503 						pipe_ctx->stream_res.abm->funcs->set_abm_level(
4504 							pipe_ctx->stream_res.abm, stream->abm_level);
4505 					}
4506 				}
4507 			}
4508 		}
4509 	}
4510 }
4511 
dc_dmub_should_send_dirty_rect_cmd(struct dc * dc,struct dc_stream_state * stream)4512 static bool dc_dmub_should_send_dirty_rect_cmd(struct dc *dc, struct dc_stream_state *stream)
4513 {
4514 	(void)dc;
4515 	if ((stream->link->psr_settings.psr_version == DC_PSR_VERSION_SU_1
4516 			|| stream->link->psr_settings.psr_version == DC_PSR_VERSION_1)
4517 			&& stream->ctx->dce_version >= DCN_VERSION_3_1)
4518 		return true;
4519 
4520 	if (stream->link->replay_settings.config.replay_supported)
4521 		return true;
4522 
4523 	if (stream->ctx->dce_version >= DCN_VERSION_3_5 && stream->abm_level)
4524 		return true;
4525 
4526 	return false;
4527 }
4528 
dc_dmub_update_dirty_rect(struct dc * dc,int surface_count,struct dc_stream_state * stream,const struct dc_surface_update * srf_updates,struct dc_state * context)4529 void dc_dmub_update_dirty_rect(struct dc *dc,
4530 			       int surface_count,
4531 			       struct dc_stream_state *stream,
4532 			       const struct dc_surface_update *srf_updates,
4533 			       struct dc_state *context)
4534 {
4535 	union dmub_rb_cmd cmd;
4536 	struct dmub_cmd_update_dirty_rect_data *update_dirty_rect;
4537 	int i;
4538 	unsigned int j;
4539 	unsigned int panel_inst = 0;
4540 
4541 	if (!dc_dmub_should_send_dirty_rect_cmd(dc, stream))
4542 		return;
4543 
4544 	if (!dc->config.frame_update_cmd_version2 && !dc_get_edp_link_panel_inst(dc, stream->link, &panel_inst))
4545 		return;
4546 
4547 	memset(&cmd, 0x0, sizeof(cmd));
4548 	cmd.update_dirty_rect.header.type = DMUB_CMD__UPDATE_DIRTY_RECT;
4549 	cmd.update_dirty_rect.header.sub_type = 0;
4550 	cmd.update_dirty_rect.header.payload_bytes =
4551 		sizeof(cmd.update_dirty_rect) -
4552 		sizeof(cmd.update_dirty_rect.header);
4553 	update_dirty_rect = &cmd.update_dirty_rect.update_dirty_rect_data;
4554 	for (i = 0; i < surface_count; i++) {
4555 		struct dc_plane_state *plane_state = srf_updates[i].surface;
4556 		const struct dc_flip_addrs *flip_addr = srf_updates[i].flip_addr;
4557 
4558 		if (!srf_updates[i].surface || !flip_addr)
4559 			continue;
4560 		/* Do not send in immediate flip mode */
4561 		if (srf_updates[i].surface->flip_immediate)
4562 			continue;
4563 
4564 		if (dc->config.frame_update_cmd_version2)
4565 			update_dirty_rect->cmd_version = DMUB_CMD_CURSOR_UPDATE_VERSION_2;
4566 		else
4567 			update_dirty_rect->cmd_version = DMUB_CMD_CURSOR_UPDATE_VERSION_1;
4568 
4569 		update_dirty_rect->dirty_rect_count = (uint8_t)flip_addr->dirty_rect_count;
4570 		memcpy(update_dirty_rect->src_dirty_rects, flip_addr->dirty_rects,
4571 				sizeof(flip_addr->dirty_rects));
4572 		for (j = 0; j < dc->res_pool->pipe_count; j++) {
4573 			struct pipe_ctx *pipe_ctx = &context->res_ctx.pipe_ctx[j];
4574 
4575 			if (pipe_ctx->stream != stream)
4576 				continue;
4577 			if (pipe_ctx->plane_state != plane_state)
4578 				continue;
4579 
4580 			update_dirty_rect->panel_inst = (uint8_t)panel_inst;
4581 			update_dirty_rect->pipe_idx = (uint8_t)j;
4582 			update_dirty_rect->otg_inst = (uint8_t)pipe_ctx->stream_res.tg->inst;
4583 			dc_wake_and_execute_dmub_cmd(dc->ctx, &cmd, DM_DMUB_WAIT_TYPE_NO_WAIT);
4584 		}
4585 	}
4586 }
4587 
build_dmub_update_dirty_rect(struct dc * dc,int surface_count,struct dc_stream_state * stream,struct dc_surface_update * srf_updates,struct dc_state * context,struct dc_dmub_cmd dc_dmub_cmd[],unsigned int * dmub_cmd_count)4588 static void build_dmub_update_dirty_rect(
4589 		struct dc *dc,
4590 		int surface_count,
4591 		struct dc_stream_state *stream,
4592 		struct dc_surface_update *srf_updates,
4593 		struct dc_state *context,
4594 		struct dc_dmub_cmd dc_dmub_cmd[],
4595 		unsigned int *dmub_cmd_count)
4596 {
4597 	union dmub_rb_cmd cmd;
4598 	struct dmub_cmd_update_dirty_rect_data *update_dirty_rect;
4599 	int i;
4600 	unsigned int j;
4601 	unsigned int panel_inst = 0;
4602 
4603 	if (!dc_dmub_should_send_dirty_rect_cmd(dc, stream))
4604 		return;
4605 
4606 	if (!dc->config.frame_update_cmd_version2 && !dc_get_edp_link_panel_inst(dc, stream->link, &panel_inst))
4607 		return;
4608 
4609 	memset(&cmd, 0x0, sizeof(cmd));
4610 	cmd.update_dirty_rect.header.type = DMUB_CMD__UPDATE_DIRTY_RECT;
4611 	cmd.update_dirty_rect.header.sub_type = 0;
4612 	cmd.update_dirty_rect.header.payload_bytes =
4613 		sizeof(cmd.update_dirty_rect) -
4614 		sizeof(cmd.update_dirty_rect.header);
4615 	update_dirty_rect = &cmd.update_dirty_rect.update_dirty_rect_data;
4616 	for (i = 0; i < surface_count; i++) {
4617 		struct dc_plane_state *plane_state = srf_updates[i].surface;
4618 		const struct dc_flip_addrs *flip_addr = srf_updates[i].flip_addr;
4619 
4620 		if (!srf_updates[i].surface || !flip_addr)
4621 			continue;
4622 		/* Do not send in immediate flip mode */
4623 		if (srf_updates[i].surface->flip_immediate)
4624 			continue;
4625 
4626 		if (dc->config.frame_update_cmd_version2)
4627 			update_dirty_rect->cmd_version = DMUB_CMD_CURSOR_UPDATE_VERSION_2;
4628 		else
4629 			update_dirty_rect->cmd_version = DMUB_CMD_CURSOR_UPDATE_VERSION_1;
4630 
4631 		update_dirty_rect->dirty_rect_count = (uint8_t)flip_addr->dirty_rect_count;
4632 		memcpy(update_dirty_rect->src_dirty_rects, flip_addr->dirty_rects,
4633 				sizeof(flip_addr->dirty_rects));
4634 		for (j = 0; j < dc->res_pool->pipe_count; j++) {
4635 			struct pipe_ctx *pipe_ctx = &context->res_ctx.pipe_ctx[j];
4636 
4637 			if (pipe_ctx->stream != stream)
4638 				continue;
4639 			if (pipe_ctx->plane_state != plane_state)
4640 				continue;
4641 			update_dirty_rect->panel_inst = (uint8_t)panel_inst;
4642 			update_dirty_rect->pipe_idx = (uint8_t)j;
4643 			update_dirty_rect->otg_inst = (uint8_t)pipe_ctx->stream_res.tg->inst;
4644 			dc_dmub_cmd[*dmub_cmd_count].dmub_cmd = cmd;
4645 			dc_dmub_cmd[*dmub_cmd_count].wait_type = DM_DMUB_WAIT_TYPE_NO_WAIT;
4646 			(*dmub_cmd_count)++;
4647 		}
4648 	}
4649 }
4650 
4651 /**
4652  * dc_check_address_only_update - Check if addr_update is the sole flag set
4653  *
4654  * @update_bits: The pipe update bits to check
4655  *
4656  * Determines whether an update contains only an address change with no other
4657  * pending updates.
4658  *
4659  * Return: %true if addr_update is the sole bit set, %false otherwise.
4660  */
dc_check_address_only_update(struct pipe_update_bits update_bits)4661 bool dc_check_address_only_update(struct pipe_update_bits update_bits)
4662 {
4663 	struct pipe_update_bits check = update_bits;  /* 1. Copy all flags from input */
4664 
4665 	check.addr_update = 0;                        /* 2. Zero the addr_update bit in the copy */
4666 	return update_bits.addr_update &&             /* 3. Check addr_update was set in original */
4667 			!dc_pipe_update_bits_is_any_set(&check); /* 4. Check no other bits remain in the copy */
4668 }
4669 
4670 /**
4671  * build_dmub_cmd_list() - Build an array of DMCUB commands to be sent to DMCUB
4672  *
4673  * @dc: Current DC state
4674  * @srf_updates: Array of surface updates
4675  * @surface_count: Number of surfaces that have an updated
4676  * @stream: Corresponding stream to be updated in the current flip
4677  * @context: New DC state to be programmed
4678  *
4679  * @dc_dmub_cmd: Array of DMCUB commands to be sent to DMCUB
4680  * @dmub_cmd_count: Count indicating the number of DMCUB commands in dc_dmub_cmd array
4681  *
4682  * This function builds an array of DMCUB commands to be sent to DMCUB. This function is required
4683  * to build an array of commands and have them sent while the OTG lock is acquired.
4684  *
4685  * Return: void
4686  */
build_dmub_cmd_list(struct dc * dc,struct dc_surface_update * srf_updates,int surface_count,struct dc_stream_state * stream,struct dc_state * context,struct dc_dmub_cmd dc_dmub_cmd[],unsigned int * dmub_cmd_count)4687 static void build_dmub_cmd_list(struct dc *dc,
4688 		struct dc_surface_update *srf_updates,
4689 		int surface_count,
4690 		struct dc_stream_state *stream,
4691 		struct dc_state *context,
4692 		struct dc_dmub_cmd dc_dmub_cmd[],
4693 		unsigned int *dmub_cmd_count)
4694 {
4695 	// Initialize cmd count to 0
4696 	*dmub_cmd_count = 0;
4697 	build_dmub_update_dirty_rect(dc, surface_count, stream, srf_updates, context, dc_dmub_cmd, dmub_cmd_count);
4698 }
4699 
commit_plane_for_stream_offload_fams2_flip(struct dc * dc,struct dc_surface_update * srf_updates,int surface_count,struct dc_stream_state * stream,struct dc_state * context)4700 static void commit_plane_for_stream_offload_fams2_flip(struct dc *dc,
4701 		struct dc_surface_update *srf_updates,
4702 		int surface_count,
4703 		struct dc_stream_state *stream,
4704 		struct dc_state *context)
4705 {
4706 	int i;
4707 	unsigned int j;
4708 
4709 	/* update dirty rect for PSR */
4710 	dc_dmub_update_dirty_rect(dc, surface_count, stream,
4711 			srf_updates, context);
4712 
4713 	/* Perform requested Updates */
4714 	for (i = 0; i < surface_count; i++) {
4715 		struct dc_plane_state *plane_state = srf_updates[i].surface;
4716 
4717 		for (j = 0; j < dc->res_pool->pipe_count; j++) {
4718 			struct pipe_ctx *pipe_ctx = &context->res_ctx.pipe_ctx[j];
4719 
4720 			if (!should_update_pipe_for_stream(context, pipe_ctx, stream))
4721 				continue;
4722 
4723 			if (!should_update_pipe_for_plane(context, pipe_ctx, plane_state))
4724 				continue;
4725 
4726 			/* update pipe context for plane */
4727 			if (pipe_ctx->plane_state->update_bits.addr_update)
4728 				dc->hwss.update_plane_addr(dc, pipe_ctx);
4729 		}
4730 	}
4731 
4732 	/* Send commands to DMCUB */
4733 	dc_dmub_srv_fams2_passthrough_flip(dc,
4734 				context,
4735 				stream,
4736 				srf_updates,
4737 				surface_count);
4738 }
4739 
commit_planes_for_stream_fast(struct dc * dc,struct dc_surface_update * srf_updates,int surface_count,struct dc_stream_state * stream,struct dc_stream_update * stream_update,enum dc_update_type update_type,struct dc_state * context)4740 static void commit_planes_for_stream_fast(struct dc *dc,
4741 		struct dc_surface_update *srf_updates,
4742 		int surface_count,
4743 		struct dc_stream_state *stream,
4744 		struct dc_stream_update *stream_update,
4745 		enum dc_update_type update_type,
4746 		struct dc_state *context)
4747 {
4748 	int i;
4749 	unsigned int j;
4750 	struct pipe_ctx *top_pipe_to_program = NULL;
4751 	struct dc_stream_status *stream_status = NULL;
4752 	bool should_offload_fams2_flip = false;
4753 	bool should_lock_all_pipes = (update_type != UPDATE_TYPE_FAST);
4754 
4755 	if (should_lock_all_pipes)
4756 		determine_pipe_unlock_order(dc, context);
4757 
4758 	if (dc->debug.fams2_config.bits.enable &&
4759 			dc->debug.fams2_config.bits.enable_offload_flip &&
4760 			dc_state_is_fams2_in_use(dc, context)) {
4761 		/* if not offloading to HWFQ, offload to FAMS2 if needed */
4762 		should_offload_fams2_flip = true;
4763 		for (i = 0; i < surface_count; i++) {
4764 			if (srf_updates[i].surface &&
4765 					dc_pipe_update_bits_is_any_set(&srf_updates[i].surface->update_bits) &&
4766 					!dc_check_address_only_update(srf_updates[i].surface->update_bits)) {
4767 				/* more than address update, need to acquire FAMS2 lock */
4768 				should_offload_fams2_flip = false;
4769 				break;
4770 			}
4771 		}
4772 		if (stream_update) {
4773 			/* more than address update, need to acquire FAMS2 lock */
4774 			should_offload_fams2_flip = false;
4775 		}
4776 	}
4777 
4778 	dc_exit_ips_for_hw_access(dc);
4779 
4780 	dc_z10_restore(dc);
4781 
4782 	top_pipe_to_program = resource_get_otg_master_for_stream(
4783 			&context->res_ctx,
4784 			stream);
4785 
4786 	if (!top_pipe_to_program)
4787 		return;
4788 
4789 	for (i = 0; i < (int)dc->res_pool->pipe_count; i++) {
4790 		struct pipe_ctx *pipe = &context->res_ctx.pipe_ctx[i];
4791 
4792 		if (pipe->stream && pipe->plane_state) {
4793 			if (!dc->debug.using_dml2)
4794 				set_p_state_switch_method(dc, context, pipe);
4795 
4796 			if (dc->debug.visual_confirm)
4797 				dc_update_visual_confirm_color(dc, context, pipe);
4798 		}
4799 	}
4800 
4801 	for (i = 0; i < surface_count; i++) {
4802 		struct dc_plane_state *plane_state = srf_updates[i].surface;
4803 		/*set logical flag for lock/unlock use*/
4804 		for (j = 0; j < dc->res_pool->pipe_count; j++) {
4805 			struct pipe_ctx *pipe_ctx = &context->res_ctx.pipe_ctx[j];
4806 
4807 			if (!pipe_ctx->plane_state)
4808 				continue;
4809 			if (!should_update_pipe_for_plane(context, pipe_ctx, plane_state))
4810 				continue;
4811 
4812 			pipe_ctx->plane_state->triplebuffer_flips = false;
4813 			if (update_type == UPDATE_TYPE_FAST &&
4814 					dc->hwss.program_triplebuffer != NULL &&
4815 					!pipe_ctx->plane_state->flip_immediate && dc->debug.enable_tri_buf) {
4816 				/*triple buffer for VUpdate only*/
4817 				pipe_ctx->plane_state->triplebuffer_flips = true;
4818 			}
4819 		}
4820 	}
4821 
4822 	stream_status = dc_state_get_stream_status(context, stream);
4823 
4824 	if (should_offload_fams2_flip) {
4825 		commit_plane_for_stream_offload_fams2_flip(dc,
4826 				srf_updates,
4827 				surface_count,
4828 				stream,
4829 				context);
4830 	} else if (stream_status) {
4831 		build_dmub_cmd_list(dc,
4832 				srf_updates,
4833 				surface_count,
4834 				stream,
4835 				context,
4836 				context->dc_dmub_cmd,
4837 				&(context->dmub_cmd_count));
4838 		hwss_build_fast_sequence(dc,
4839 				context->dc_dmub_cmd,
4840 				context->dmub_cmd_count,
4841 				context->block_sequence,
4842 				&(context->block_sequence_steps),
4843 				top_pipe_to_program,
4844 				stream_status,
4845 				context);
4846 		hwss_execute_sequence(dc,
4847 				context->block_sequence,
4848 				context->block_sequence_steps);
4849 	}
4850 
4851 	/* Clear update flags so next flip doesn't have redundant programming
4852 	 * (if there's no stream update, the update flags are not cleared).
4853 	 * Surface updates are cleared unconditionally at the beginning of each flip,
4854 	 * so no need to clear here.
4855 	 */
4856 	if (top_pipe_to_program->stream)
4857 		stream_update_flags_clear(&top_pipe_to_program->stream->update_flags);
4858 }
4859 
commit_planes_for_stream(struct dc * dc,const struct dc_surface_update * srf_updates,int surface_count,struct dc_stream_state * stream,struct dc_stream_update * stream_update,enum dc_update_type update_type,struct dc_state * context)4860 static void commit_planes_for_stream(struct dc *dc,
4861 		const struct dc_surface_update *srf_updates,
4862 		int surface_count,
4863 		struct dc_stream_state *stream,
4864 		struct dc_stream_update *stream_update,
4865 		enum dc_update_type update_type,
4866 		struct dc_state *context)
4867 {
4868 	int i;
4869 	unsigned int j, pipe_idx;
4870 	struct pipe_ctx *top_pipe_to_program = NULL;
4871 	bool should_lock_all_pipes = (update_type != UPDATE_TYPE_FAST);
4872 	bool subvp_prev_use = false;
4873 	bool subvp_curr_use = false;
4874 	uint8_t current_stream_mask = 0;
4875 
4876 	if (should_lock_all_pipes)
4877 		determine_pipe_unlock_order(dc, context);
4878 	// Once we apply the new subvp context to hardware it won't be in the
4879 	// dc->current_state anymore, so we have to cache it before we apply
4880 	// the new SubVP context
4881 	subvp_prev_use = false;
4882 	dc_exit_ips_for_hw_access(dc);
4883 
4884 	dc_z10_restore(dc);
4885 	if (update_type == UPDATE_TYPE_FULL && dc->optimized_required)
4886 		hwss_process_outstanding_hw_updates(dc, dc->current_state);
4887 
4888 	if (update_type != UPDATE_TYPE_FAST && dc->res_pool->funcs->prepare_mcache_programming)
4889 		dc->res_pool->funcs->prepare_mcache_programming(dc, context);
4890 
4891 	for (pipe_idx = 0; pipe_idx < dc->res_pool->pipe_count; pipe_idx++) {
4892 		struct pipe_ctx *pipe = &context->res_ctx.pipe_ctx[pipe_idx];
4893 
4894 		if (pipe->stream && pipe->plane_state) {
4895 			if (!dc->debug.using_dml2)
4896 				set_p_state_switch_method(dc, context, pipe);
4897 
4898 			if (dc->debug.visual_confirm)
4899 				dc_update_visual_confirm_color(dc, context, pipe);
4900 		}
4901 	}
4902 
4903 	if (update_type == UPDATE_TYPE_FULL) {
4904 		dc_allow_idle_optimizations(dc, false);
4905 
4906 		if (get_seamless_boot_stream_count(context) == 0)
4907 			dc->hwss.prepare_bandwidth(dc, context);
4908 
4909 		if (dc->hwss.update_dsc_pg)
4910 			dc->hwss.update_dsc_pg(dc, context, false);
4911 
4912 		context_clock_trace(dc, context);
4913 	}
4914 
4915 	if (update_type == UPDATE_TYPE_FULL)
4916 		hwss_wait_for_outstanding_hw_updates(dc, dc->current_state);
4917 
4918 	top_pipe_to_program = resource_get_otg_master_for_stream(
4919 				&context->res_ctx,
4920 				stream);
4921 	ASSERT(top_pipe_to_program != NULL);
4922 
4923 	for (pipe_idx = 0; pipe_idx < dc->res_pool->pipe_count; pipe_idx++) {
4924 		struct pipe_ctx *old_pipe = &dc->current_state->res_ctx.pipe_ctx[pipe_idx];
4925 
4926 		// Check old context for SubVP
4927 		subvp_prev_use |= (dc_state_get_pipe_subvp_type(dc->current_state, old_pipe) == SUBVP_PHANTOM);
4928 		if (subvp_prev_use)
4929 			break;
4930 	}
4931 
4932 	for (pipe_idx = 0; pipe_idx < dc->res_pool->pipe_count; pipe_idx++) {
4933 		struct pipe_ctx *pipe = &context->res_ctx.pipe_ctx[pipe_idx];
4934 
4935 		if (dc_state_get_pipe_subvp_type(context, pipe) == SUBVP_PHANTOM) {
4936 			subvp_curr_use = true;
4937 			break;
4938 		}
4939 	}
4940 
4941 	if (stream->test_pattern.type != DP_TEST_PATTERN_VIDEO_MODE) {
4942 		struct pipe_ctx *mpcc_pipe;
4943 		struct pipe_ctx *odm_pipe;
4944 
4945 		for (mpcc_pipe = top_pipe_to_program; mpcc_pipe; mpcc_pipe = mpcc_pipe->bottom_pipe)
4946 			for (odm_pipe = mpcc_pipe; odm_pipe; odm_pipe = odm_pipe->next_odm_pipe)
4947 				odm_pipe->ttu_regs.min_ttu_vblank = MAX_TTU;
4948 	}
4949 
4950 	if ((update_type != UPDATE_TYPE_FAST) && stream->update_flags.bits.dsc_changed)
4951 		if (top_pipe_to_program &&
4952 			top_pipe_to_program->stream_res.tg->funcs->lock_doublebuffer_enable) {
4953 			if (should_use_dmub_inbox1_lock(dc, stream->link)) {
4954 				union dmub_hw_lock_flags hw_locks = { 0 };
4955 				struct dmub_hw_lock_inst_flags inst_flags = { 0 };
4956 
4957 				hw_locks.bits.lock_dig = 1;
4958 				inst_flags.dig_inst = (uint8_t)top_pipe_to_program->stream_res.tg->inst;
4959 
4960 				dmub_hw_lock_mgr_cmd(dc->ctx->dmub_srv,
4961 							true,
4962 							&hw_locks,
4963 							&inst_flags);
4964 			} else
4965 				top_pipe_to_program->stream_res.tg->funcs->lock_doublebuffer_enable(
4966 						top_pipe_to_program->stream_res.tg);
4967 		}
4968 
4969 	if (dc->hwss.wait_for_dcc_meta_propagation) {
4970 		dc->hwss.wait_for_dcc_meta_propagation(dc, top_pipe_to_program);
4971 	}
4972 
4973 	if (dc->hwseq->funcs.wait_for_pipe_update_if_needed)
4974 		dc->hwseq->funcs.wait_for_pipe_update_if_needed(dc, top_pipe_to_program, update_type < UPDATE_TYPE_FULL);
4975 
4976 	if (should_lock_all_pipes && dc->hwss.interdependent_update_lock) {
4977 		if (dc->hwss.subvp_pipe_control_lock)
4978 			dc->hwss.subvp_pipe_control_lock(dc, context, true, should_lock_all_pipes, NULL, subvp_prev_use);
4979 
4980 		if (dc->hwss.dmub_hw_control_lock)
4981 			dc->hwss.dmub_hw_control_lock(dc, context, true);
4982 
4983 		dc->hwss.interdependent_update_lock(dc, context, true);
4984 	} else {
4985 		if (dc->hwss.subvp_pipe_control_lock)
4986 			dc->hwss.subvp_pipe_control_lock(dc, context, true, should_lock_all_pipes, top_pipe_to_program, subvp_prev_use);
4987 
4988 		if (dc->hwss.dmub_hw_control_lock)
4989 			dc->hwss.dmub_hw_control_lock(dc, context, true);
4990 
4991 		/* Lock the top pipe while updating plane addrs, since freesync requires
4992 		 *  plane addr update event triggers to be synchronized.
4993 		 *  top_pipe_to_program is expected to never be NULL
4994 		 */
4995 		dc->hwss.pipe_control_lock(dc, top_pipe_to_program, true);
4996 	}
4997 
4998 	dc_dmub_update_dirty_rect(dc, surface_count, stream, srf_updates, context);
4999 
5000 	// Stream updates
5001 	if (stream_update)
5002 		commit_planes_do_stream_update(dc, stream, stream_update, update_type, context);
5003 
5004 	if (surface_count == 0) {
5005 		/*
5006 		 * In case of turning off screen, no need to program front end a second time.
5007 		 * just return after program blank.
5008 		 */
5009 		if (dc->hwss.apply_ctx_for_surface)
5010 			dc->hwss.apply_ctx_for_surface(dc, stream, 0, context);
5011 		if (dc->hwss.program_front_end_for_ctx)
5012 			dc->hwss.program_front_end_for_ctx(dc, context);
5013 
5014 		if (should_lock_all_pipes && dc->hwss.interdependent_update_lock) {
5015 			dc->hwss.interdependent_update_lock(dc, context, false);
5016 		} else {
5017 			dc->hwss.pipe_control_lock(dc, top_pipe_to_program, false);
5018 		}
5019 		dc->hwss.post_unlock_program_front_end(dc, context);
5020 
5021 		if (update_type != UPDATE_TYPE_FAST)
5022 			if (dc->hwss.commit_subvp_config)
5023 				dc->hwss.commit_subvp_config(dc, context);
5024 
5025 		/* Since phantom pipe programming is moved to post_unlock_program_front_end,
5026 		 * move the SubVP lock to after the phantom pipes have been setup
5027 		 */
5028 		if (dc->hwss.subvp_pipe_control_lock)
5029 			dc->hwss.subvp_pipe_control_lock(dc, context, false, should_lock_all_pipes,
5030 							 NULL, subvp_prev_use);
5031 
5032 		if (dc->hwss.dmub_hw_control_lock)
5033 			dc->hwss.dmub_hw_control_lock(dc, context, false);
5034 		return;
5035 	}
5036 
5037 	if (update_type != UPDATE_TYPE_FAST) {
5038 		for (j = 0; j < dc->res_pool->pipe_count; j++) {
5039 			struct pipe_ctx *pipe_ctx = &context->res_ctx.pipe_ctx[j];
5040 
5041 			if ((dc->debug.visual_confirm == VISUAL_CONFIRM_SUBVP ||
5042 				dc->debug.visual_confirm == VISUAL_CONFIRM_MCLK_SWITCH) &&
5043 				pipe_ctx->stream && pipe_ctx->plane_state) {
5044 				/* Only update visual confirm for SUBVP and Mclk switching here.
5045 				 * The bar appears on all pipes, so we need to update the bar on all displays,
5046 				 * so the information doesn't get stale.
5047 				 */
5048 				dc->hwss.update_visual_confirm_color(dc, pipe_ctx,
5049 						pipe_ctx->plane_res.hubp->inst);
5050 			}
5051 		}
5052 	}
5053 
5054 	for (i = 0; i < surface_count; i++) {
5055 		struct dc_plane_state *plane_state = srf_updates[i].surface;
5056 
5057 		/*set logical flag for lock/unlock use*/
5058 		for (j = 0; j < dc->res_pool->pipe_count; j++) {
5059 			struct pipe_ctx *pipe_ctx = &context->res_ctx.pipe_ctx[j];
5060 			if (!pipe_ctx->plane_state)
5061 				continue;
5062 			if (!should_update_pipe_for_plane(context, pipe_ctx, plane_state))
5063 				continue;
5064 			pipe_ctx->plane_state->triplebuffer_flips = false;
5065 			if (update_type == UPDATE_TYPE_FAST &&
5066 					dc->hwss.program_triplebuffer != NULL &&
5067 					!pipe_ctx->plane_state->flip_immediate && dc->debug.enable_tri_buf) {
5068 				/*triple buffer for VUpdate only*/
5069 				pipe_ctx->plane_state->triplebuffer_flips = true;
5070 			}
5071 		}
5072 		if (update_type == UPDATE_TYPE_FULL) {
5073 			/* force vsync flip when reconfiguring pipes to prevent underflow */
5074 			plane_state->flip_immediate = false;
5075 			plane_state->triplebuffer_flips = false;
5076 		}
5077 	}
5078 
5079 	// Update Type FULL, Surface updates
5080 	for (j = 0; j < dc->res_pool->pipe_count; j++) {
5081 		struct pipe_ctx *pipe_ctx = &context->res_ctx.pipe_ctx[j];
5082 
5083 		if (!pipe_ctx->top_pipe &&
5084 			!pipe_ctx->prev_odm_pipe &&
5085 			should_update_pipe_for_stream(context, pipe_ctx, stream)) {
5086 			struct dc_stream_status *pipe_stream_status = NULL;
5087 
5088 			if (!pipe_ctx->plane_state)
5089 				continue;
5090 
5091 			/* Full fe update*/
5092 			if (update_type == UPDATE_TYPE_FAST)
5093 				continue;
5094 
5095 			pipe_stream_status =
5096 				stream_get_status(context, pipe_ctx->stream);
5097 
5098 			if (dc->hwss.apply_ctx_for_surface && pipe_stream_status)
5099 				dc->hwss.apply_ctx_for_surface(
5100 					dc, pipe_ctx->stream, pipe_stream_status->plane_count, context);
5101 		}
5102 	}
5103 
5104 	for (j = 0; j < dc->res_pool->pipe_count; j++) {
5105 		struct pipe_ctx *pipe_ctx = &context->res_ctx.pipe_ctx[j];
5106 
5107 		if (!pipe_ctx->plane_state)
5108 			continue;
5109 
5110 		/* Full fe update*/
5111 		if (update_type == UPDATE_TYPE_FAST)
5112 			continue;
5113 
5114 		ASSERT(!pipe_ctx->plane_state->triplebuffer_flips);
5115 		if (dc->hwss.program_triplebuffer != NULL && dc->debug.enable_tri_buf) {
5116 			/*turn off triple buffer for full update*/
5117 			dc->hwss.program_triplebuffer(
5118 				dc, pipe_ctx, pipe_ctx->plane_state->triplebuffer_flips);
5119 		}
5120 	}
5121 
5122 	if (dc->hwss.program_front_end_for_ctx && update_type != UPDATE_TYPE_FAST) {
5123 		dc->hwss.program_front_end_for_ctx(dc, context);
5124 
5125 		//Pipe busy until some frame and line #
5126 		if (dc->hwseq->funcs.set_wait_for_update_needed_for_pipe && update_type == UPDATE_TYPE_FULL) {
5127 			for (j = 0; j < dc->res_pool->pipe_count; j++) {
5128 				struct pipe_ctx *pipe_ctx = &context->res_ctx.pipe_ctx[j];
5129 
5130 				dc->hwseq->funcs.set_wait_for_update_needed_for_pipe(dc, pipe_ctx);
5131 			}
5132 		}
5133 
5134 		if (dc->debug.validate_dml_output) {
5135 			for (pipe_idx = 0; pipe_idx < dc->res_pool->pipe_count; pipe_idx++) {
5136 				struct pipe_ctx *cur_pipe = &context->res_ctx.pipe_ctx[pipe_idx];
5137 				if (cur_pipe->stream == NULL)
5138 					continue;
5139 
5140 				cur_pipe->plane_res.hubp->funcs->validate_dml_output(
5141 						cur_pipe->plane_res.hubp, dc->ctx,
5142 						&context->res_ctx.pipe_ctx[pipe_idx].rq_regs,
5143 						&context->res_ctx.pipe_ctx[pipe_idx].dlg_regs,
5144 						&context->res_ctx.pipe_ctx[pipe_idx].ttu_regs);
5145 			}
5146 		}
5147 	}
5148 
5149 	// Update Type FAST, Surface updates
5150 	if (update_type == UPDATE_TYPE_FAST) {
5151 		if (dc->hwss.set_flip_control_gsl)
5152 			for (i = 0; i < surface_count; i++) {
5153 				struct dc_plane_state *plane_state = srf_updates[i].surface;
5154 
5155 				for (j = 0; j < dc->res_pool->pipe_count; j++) {
5156 					struct pipe_ctx *pipe_ctx = &context->res_ctx.pipe_ctx[j];
5157 
5158 					if (!should_update_pipe_for_stream(context, pipe_ctx, stream))
5159 						continue;
5160 
5161 					if (!should_update_pipe_for_plane(context, pipe_ctx, plane_state))
5162 						continue;
5163 
5164 					// GSL has to be used for flip immediate
5165 					dc->hwss.set_flip_control_gsl(pipe_ctx,
5166 							pipe_ctx->plane_state->flip_immediate);
5167 				}
5168 			}
5169 
5170 		/* Perform requested Updates */
5171 		for (i = 0; i < surface_count; i++) {
5172 			struct dc_plane_state *plane_state = srf_updates[i].surface;
5173 
5174 			for (j = 0; j < dc->res_pool->pipe_count; j++) {
5175 				struct pipe_ctx *pipe_ctx = &context->res_ctx.pipe_ctx[j];
5176 
5177 				if (!should_update_pipe_for_stream(context, pipe_ctx, stream))
5178 					continue;
5179 
5180 				if (!should_update_pipe_for_plane(context, pipe_ctx, plane_state))
5181 					continue;
5182 
5183 				if (srf_updates[i].cm &&
5184 						srf_updates[i].cm->flags.bits.lut3d_enable &&
5185 						srf_updates[i].cm->flags.bits.lut3d_dma_enable &&
5186 						dc->hwss.trigger_3dlut_dma_load)
5187 					dc->hwss.trigger_3dlut_dma_load(pipe_ctx);
5188 
5189 				/*program triple buffer after lock based on flip type*/
5190 				if (dc->hwss.program_triplebuffer != NULL && dc->debug.enable_tri_buf) {
5191 					/*only enable triplebuffer for fast_update*/
5192 					dc->hwss.program_triplebuffer(
5193 						dc, pipe_ctx, pipe_ctx->plane_state->triplebuffer_flips);
5194 				}
5195 				if (pipe_ctx->plane_state->update_bits.addr_update)
5196 					dc->hwss.update_plane_addr(dc, pipe_ctx);
5197 			}
5198 		}
5199 	}
5200 
5201 	if (should_lock_all_pipes && dc->hwss.interdependent_update_lock) {
5202 		dc->hwss.interdependent_update_lock(dc, context, false);
5203 	} else {
5204 		dc->hwss.pipe_control_lock(dc, top_pipe_to_program, false);
5205 	}
5206 
5207 	if ((update_type != UPDATE_TYPE_FAST) && stream->update_flags.bits.dsc_changed)
5208 		if (top_pipe_to_program &&
5209 		    top_pipe_to_program->stream_res.tg->funcs->lock_doublebuffer_enable) {
5210 			top_pipe_to_program->stream_res.tg->funcs->wait_for_state(
5211 				top_pipe_to_program->stream_res.tg,
5212 				CRTC_STATE_VACTIVE);
5213 			top_pipe_to_program->stream_res.tg->funcs->wait_for_state(
5214 				top_pipe_to_program->stream_res.tg,
5215 				CRTC_STATE_VBLANK);
5216 			top_pipe_to_program->stream_res.tg->funcs->wait_for_state(
5217 				top_pipe_to_program->stream_res.tg,
5218 				CRTC_STATE_VACTIVE);
5219 
5220 			if (should_use_dmub_inbox1_lock(dc, stream->link)) {
5221 				union dmub_hw_lock_flags hw_locks = { 0 };
5222 				struct dmub_hw_lock_inst_flags inst_flags = { 0 };
5223 
5224 				hw_locks.bits.lock_dig = 1;
5225 				inst_flags.dig_inst = (uint8_t)top_pipe_to_program->stream_res.tg->inst;
5226 
5227 				dmub_hw_lock_mgr_cmd(dc->ctx->dmub_srv,
5228 							false,
5229 							&hw_locks,
5230 							&inst_flags);
5231 			} else
5232 				top_pipe_to_program->stream_res.tg->funcs->lock_doublebuffer_disable(
5233 					top_pipe_to_program->stream_res.tg);
5234 		}
5235 
5236 	if (subvp_curr_use) {
5237 		/* If enabling subvp or transitioning from subvp->subvp, enable the
5238 		 * phantom streams before we program front end for the phantom pipes.
5239 		 */
5240 		if (update_type != UPDATE_TYPE_FAST) {
5241 			if (dc->hwss.enable_phantom_streams)
5242 				dc->hwss.enable_phantom_streams(dc, context);
5243 		}
5244 	}
5245 
5246 	if (update_type != UPDATE_TYPE_FAST)
5247 		dc->hwss.post_unlock_program_front_end(dc, context);
5248 
5249 	if (subvp_prev_use && !subvp_curr_use) {
5250 		/* If disabling subvp, disable phantom streams after front end
5251 		 * programming has completed (we turn on phantom OTG in order
5252 		 * to complete the plane disable for phantom pipes).
5253 		 */
5254 
5255 		if (dc->hwss.disable_phantom_streams)
5256 			dc->hwss.disable_phantom_streams(dc, context);
5257 	}
5258 
5259 	if (update_type != UPDATE_TYPE_FAST)
5260 		if (dc->hwss.commit_subvp_config)
5261 			dc->hwss.commit_subvp_config(dc, context);
5262 	/* Since phantom pipe programming is moved to post_unlock_program_front_end,
5263 	 * move the SubVP lock to after the phantom pipes have been setup
5264 	 */
5265 	if (should_lock_all_pipes && dc->hwss.interdependent_update_lock) {
5266 		if (dc->hwss.subvp_pipe_control_lock)
5267 			dc->hwss.subvp_pipe_control_lock(dc, context, false, should_lock_all_pipes, NULL, subvp_prev_use);
5268 		if (dc->hwss.dmub_hw_control_lock)
5269 			dc->hwss.dmub_hw_control_lock(dc, context, false);
5270 	} else {
5271 		if (dc->hwss.subvp_pipe_control_lock)
5272 			dc->hwss.subvp_pipe_control_lock(dc, context, false, should_lock_all_pipes, top_pipe_to_program, subvp_prev_use);
5273 		if (dc->hwss.dmub_hw_control_lock)
5274 			dc->hwss.dmub_hw_control_lock(dc, context, false);
5275 	}
5276 
5277 	// Fire manual trigger only when bottom plane is flipped
5278 	for (j = 0; j < dc->res_pool->pipe_count; j++) {
5279 		struct pipe_ctx *pipe_ctx = &context->res_ctx.pipe_ctx[j];
5280 
5281 		if (!pipe_ctx->plane_state)
5282 			continue;
5283 
5284 		if (pipe_ctx->bottom_pipe || pipe_ctx->next_odm_pipe ||
5285 				!pipe_ctx->stream || !should_update_pipe_for_stream(context, pipe_ctx, stream) ||
5286 				!pipe_ctx->plane_state->update_bits.addr_update ||
5287 				pipe_ctx->plane_state->skip_manual_trigger)
5288 			continue;
5289 
5290 		if (dc->hwss.program_cursor_offload_now)
5291 			dc->hwss.program_cursor_offload_now(dc, pipe_ctx);
5292 		if (pipe_ctx->stream_res.tg->funcs->program_manual_trigger)
5293 			pipe_ctx->stream_res.tg->funcs->program_manual_trigger(pipe_ctx->stream_res.tg);
5294 	}
5295 
5296 	current_stream_mask = get_stream_mask(dc, context);
5297 	if (current_stream_mask != context->stream_mask) {
5298 		context->stream_mask = current_stream_mask;
5299 		dc_dmub_srv_notify_stream_mask(dc->ctx->dmub_srv, current_stream_mask);
5300 	}
5301 }
5302 
5303 /**
5304  * could_mpcc_tree_change_for_active_pipes - Check if an OPP associated with MPCC might change
5305  *
5306  * @dc: Used to get the current state status
5307  * @stream: Target stream, which we want to remove the attached planes
5308  * @srf_updates: Array of surface updates
5309  * @surface_count: Number of surface update
5310  * @is_plane_addition: [in] Fill out with true if it is a plane addition case
5311  *
5312  * DCN32x and newer support a feature named Dynamic ODM which can conflict with
5313  * the MPO if used simultaneously in some specific configurations (e.g.,
5314  * 4k@144). This function checks if the incoming context requires applying a
5315  * transition state with unnecessary pipe splitting and ODM disabled to
5316  * circumvent our hardware limitations to prevent this edge case. If the OPP
5317  * associated with an MPCC might change due to plane additions, this function
5318  * returns true.
5319  *
5320  * Return:
5321  * Return true if OPP and MPCC might change, otherwise, return false.
5322  */
could_mpcc_tree_change_for_active_pipes(struct dc * dc,struct dc_stream_state * stream,struct dc_surface_update * srf_updates,int surface_count,bool * is_plane_addition)5323 static bool could_mpcc_tree_change_for_active_pipes(struct dc *dc,
5324 		struct dc_stream_state *stream,
5325 		struct dc_surface_update *srf_updates,
5326 		int surface_count,
5327 		bool *is_plane_addition)
5328 {
5329 	(void)srf_updates;
5330 
5331 	struct dc_stream_status *cur_stream_status = stream_get_status(dc->current_state, stream);
5332 	bool force_minimal_pipe_splitting = false;
5333 	bool subvp_active = false;
5334 	uint32_t i;
5335 
5336 	*is_plane_addition = false;
5337 
5338 	if (cur_stream_status &&
5339 			dc->current_state->stream_count > 0 &&
5340 			dc->debug.pipe_split_policy != MPC_SPLIT_AVOID) {
5341 		/* determine if minimal transition is required due to MPC*/
5342 		if (surface_count > 0) {
5343 			if (cur_stream_status->plane_count > surface_count) {
5344 				force_minimal_pipe_splitting = true;
5345 			} else if (cur_stream_status->plane_count < surface_count) {
5346 				force_minimal_pipe_splitting = true;
5347 				*is_plane_addition = true;
5348 			}
5349 		}
5350 	}
5351 
5352 	if (cur_stream_status &&
5353 			dc->current_state->stream_count == 1 &&
5354 			dc->debug.enable_single_display_2to1_odm_policy) {
5355 		/* determine if minimal transition is required due to dynamic ODM*/
5356 		if (surface_count > 0) {
5357 			if (cur_stream_status->plane_count > 2 && cur_stream_status->plane_count > surface_count) {
5358 				force_minimal_pipe_splitting = true;
5359 			} else if (surface_count > 2 && cur_stream_status->plane_count < surface_count) {
5360 				force_minimal_pipe_splitting = true;
5361 				*is_plane_addition = true;
5362 			}
5363 		}
5364 	}
5365 
5366 	for (i = 0; i < dc->res_pool->pipe_count; i++) {
5367 		struct pipe_ctx *pipe = &dc->current_state->res_ctx.pipe_ctx[i];
5368 
5369 		if (dc_state_get_pipe_subvp_type(dc->current_state, pipe) != SUBVP_NONE) {
5370 			subvp_active = true;
5371 			break;
5372 		}
5373 	}
5374 
5375 	/* For SubVP when adding or removing planes we need to add a minimal transition
5376 	 * (even when disabling all planes). Whenever disabling a phantom pipe, we
5377 	 * must use the minimal transition path to disable the pipe correctly.
5378 	 *
5379 	 * We want to use the minimal transition whenever subvp is active, not only if
5380 	 * a plane is being added / removed from a subvp stream (MPO plane can be added
5381 	 * to a DRR pipe of SubVP + DRR config, in which case we still want to run through
5382 	 * a min transition to disable subvp.
5383 	 */
5384 	if (cur_stream_status && subvp_active) {
5385 		/* determine if minimal transition is required due to SubVP*/
5386 		if (cur_stream_status->plane_count > surface_count) {
5387 			force_minimal_pipe_splitting = true;
5388 		} else if (cur_stream_status->plane_count < surface_count) {
5389 			force_minimal_pipe_splitting = true;
5390 			*is_plane_addition = true;
5391 		}
5392 	}
5393 
5394 	return force_minimal_pipe_splitting;
5395 }
5396 
5397 
release_minimal_transition_state(struct dc * dc,struct dc_state * minimal_transition_context,struct dc_state * base_context,struct pipe_split_policy_backup * policy)5398 static void release_minimal_transition_state(struct dc *dc,
5399 		struct dc_state *minimal_transition_context,
5400 		struct dc_state *base_context,
5401 		struct pipe_split_policy_backup *policy)
5402 {
5403 	restore_minimal_pipe_split_policy(dc, base_context, policy);
5404 	dc_state_release(minimal_transition_context);
5405 }
5406 
force_vsync_flip_in_minimal_transition_context(struct dc_state * context)5407 static void force_vsync_flip_in_minimal_transition_context(struct dc_state *context)
5408 {
5409 	uint8_t i;
5410 	int j;
5411 	struct dc_stream_status *stream_status;
5412 
5413 	for (i = 0; i < context->stream_count; i++) {
5414 		stream_status = &context->stream_status[i];
5415 
5416 		for (j = 0; j < stream_status->plane_count; j++)
5417 			stream_status->plane_states[j]->flip_immediate = false;
5418 	}
5419 }
5420 
create_minimal_transition_state(struct dc * dc,struct dc_state * base_context,struct pipe_split_policy_backup * policy)5421 static struct dc_state *create_minimal_transition_state(struct dc *dc,
5422 		struct dc_state *base_context, struct pipe_split_policy_backup *policy)
5423 {
5424 	struct dc_state *minimal_transition_context = NULL;
5425 
5426 	minimal_transition_context = dc_state_create_copy(base_context);
5427 	if (!minimal_transition_context)
5428 		return NULL;
5429 
5430 	backup_and_set_minimal_pipe_split_policy(dc, base_context, policy);
5431 	/* commit minimal state */
5432 	if (dc->res_pool->funcs->validate_bandwidth(dc, minimal_transition_context,
5433 		DC_VALIDATE_MODE_AND_PROGRAMMING) == DC_OK) {
5434 		/* prevent underflow and corruption when reconfiguring pipes */
5435 		force_vsync_flip_in_minimal_transition_context(minimal_transition_context);
5436 	} else {
5437 		/*
5438 		 * This should never happen, minimal transition state should
5439 		 * always be validated first before adding pipe split features.
5440 		 */
5441 		release_minimal_transition_state(dc, minimal_transition_context, base_context, policy);
5442 		BREAK_TO_DEBUGGER();
5443 		minimal_transition_context = NULL;
5444 	}
5445 	return minimal_transition_context;
5446 }
5447 
is_pipe_topology_transition_seamless_with_intermediate_step(struct dc * dc,struct dc_state * initial_state,struct dc_state * intermediate_state,struct dc_state * final_state)5448 static bool is_pipe_topology_transition_seamless_with_intermediate_step(
5449 		struct dc *dc,
5450 		struct dc_state *initial_state,
5451 		struct dc_state *intermediate_state,
5452 		struct dc_state *final_state)
5453 {
5454 	return dc->hwss.is_pipe_topology_transition_seamless(dc, initial_state,
5455 			intermediate_state) &&
5456 			dc->hwss.is_pipe_topology_transition_seamless(dc,
5457 					intermediate_state, final_state);
5458 }
5459 
swap_and_release_current_context(struct dc * dc,struct dc_state * new_context,struct dc_stream_state * stream)5460 static void swap_and_release_current_context(struct dc *dc,
5461 		struct dc_state *new_context, struct dc_stream_state *stream)
5462 {
5463 
5464 	unsigned int i;
5465 	struct dc_state *old = dc->current_state;
5466 	struct pipe_ctx *pipe_ctx;
5467 
5468 	/* Since memory free requires elevated IRQ, an interrupt
5469 	 * request is generated by mem free. If this happens
5470 	 * between freeing and reassigning the context, our vsync
5471 	 * interrupt will call into dc and cause a memory
5472 	 * corruption. Hence, we first reassign the context,
5473 	 * then free the old context.
5474 	 */
5475 	dc->current_state = new_context;
5476 	dc_state_release(old);
5477 
5478 	// clear any forced full updates
5479 	for (i = 0; i < dc->res_pool->pipe_count; i++) {
5480 		pipe_ctx = &new_context->res_ctx.pipe_ctx[i];
5481 
5482 		if (pipe_ctx->plane_state && pipe_ctx->stream == stream)
5483 			pipe_ctx->plane_state->force_full_update = false;
5484 	}
5485 }
5486 
initialize_empty_surface_updates(struct dc_stream_state * stream,struct dc_surface_update * srf_updates)5487 static int initialize_empty_surface_updates(
5488 		struct dc_stream_state *stream,
5489 		struct dc_surface_update *srf_updates)
5490 {
5491 	struct dc_stream_status *status = dc_stream_get_status(stream);
5492 	int i;
5493 
5494 	if (!status)
5495 		return 0;
5496 
5497 	for (i = 0; i < status->plane_count; i++)
5498 		srf_updates[i].surface = status->plane_states[i];
5499 
5500 	return status->plane_count;
5501 }
5502 
commit_minimal_transition_based_on_new_context(struct dc * dc,struct dc_state * new_context,struct dc_stream_state * stream,struct dc_stream_update * stream_update,struct dc_surface_update * srf_updates,int surface_count)5503 static bool commit_minimal_transition_based_on_new_context(struct dc *dc,
5504 		struct dc_state *new_context,
5505 		struct dc_stream_state *stream,
5506 		struct dc_stream_update *stream_update,
5507 		struct dc_surface_update *srf_updates,
5508 		int surface_count)
5509 {
5510 	bool success = false;
5511 	struct pipe_split_policy_backup policy;
5512 	struct dc_state *intermediate_context =
5513 			create_minimal_transition_state(dc, new_context,
5514 					&policy);
5515 
5516 	if (intermediate_context) {
5517 		if (is_pipe_topology_transition_seamless_with_intermediate_step(
5518 				dc,
5519 				dc->current_state,
5520 				intermediate_context,
5521 				new_context)) {
5522 			DC_LOG_DC("commit minimal transition state: base = new state\n");
5523 			commit_planes_for_stream(dc, srf_updates,
5524 					surface_count, stream, stream_update,
5525 					UPDATE_TYPE_FULL, intermediate_context);
5526 			swap_and_release_current_context(
5527 					dc, intermediate_context, stream);
5528 			dc_state_retain(dc->current_state);
5529 			success = true;
5530 		}
5531 		release_minimal_transition_state(
5532 				dc, intermediate_context, new_context, &policy);
5533 	}
5534 	return success;
5535 }
5536 
commit_minimal_transition_based_on_current_context(struct dc * dc,struct dc_state * new_context,struct dc_stream_state * stream)5537 static bool commit_minimal_transition_based_on_current_context(struct dc *dc,
5538 		struct dc_state *new_context, struct dc_stream_state *stream)
5539 {
5540 	bool success = false;
5541 	struct pipe_split_policy_backup policy;
5542 	struct dc_state *intermediate_context;
5543 	struct dc_state *old_current_state = dc->current_state;
5544 	struct dc_surface_update srf_updates[MAX_SURFACES] = {0};
5545 	int surface_count;
5546 
5547 	/*
5548 	 * Both current and new contexts share the same stream and plane state
5549 	 * pointers. When new context is validated, stream and planes get
5550 	 * populated with new updates such as new plane addresses. This makes
5551 	 * the current context no longer valid because stream and planes are
5552 	 * modified from the original. We backup current stream and plane states
5553 	 * into scratch space whenever we are populating new context. So we can
5554 	 * restore the original values back by calling the restore function now.
5555 	 * This restores back the original stream and plane states associated
5556 	 * with the current state.
5557 	 */
5558 	restore_planes_and_stream_state(&dc->scratch.current_state, stream);
5559 	dc_state_retain(old_current_state);
5560 	intermediate_context = create_minimal_transition_state(dc,
5561 			old_current_state, &policy);
5562 
5563 	if (intermediate_context) {
5564 		if (is_pipe_topology_transition_seamless_with_intermediate_step(
5565 				dc,
5566 				dc->current_state,
5567 				intermediate_context,
5568 				new_context)) {
5569 			DC_LOG_DC("commit minimal transition state: base = current state\n");
5570 			surface_count = initialize_empty_surface_updates(
5571 					stream, srf_updates);
5572 			commit_planes_for_stream(dc, srf_updates,
5573 					surface_count, stream, NULL,
5574 					UPDATE_TYPE_FULL, intermediate_context);
5575 			swap_and_release_current_context(
5576 					dc, intermediate_context, stream);
5577 			dc_state_retain(dc->current_state);
5578 			success = true;
5579 		}
5580 		release_minimal_transition_state(dc, intermediate_context,
5581 				old_current_state, &policy);
5582 	}
5583 	dc_state_release(old_current_state);
5584 	/*
5585 	 * Restore stream and plane states back to the values associated with
5586 	 * new context.
5587 	 */
5588 	restore_planes_and_stream_state(&dc->scratch.new_state, stream);
5589 	return success;
5590 }
5591 
5592 /**
5593  * commit_minimal_transition_state_in_dc_update - Commit a minimal state based
5594  * on current or new context
5595  *
5596  * @dc: DC structure, used to get the current state
5597  * @new_context: New context
5598  * @stream: Stream getting the update for the flip
5599  * @srf_updates: Surface updates
5600  * @surface_count: Number of surfaces
5601  *
5602  * The function takes in current state and new state and determine a minimal
5603  * transition state as the intermediate step which could make the transition
5604  * between current and new states seamless. If found, it will commit the minimal
5605  * transition state and update current state to this minimal transition state
5606  * and return true, if not, it will return false.
5607  *
5608  * Return:
5609  * Return True if the minimal transition succeeded, false otherwise
5610  */
commit_minimal_transition_state_in_dc_update(struct dc * dc,struct dc_state * new_context,struct dc_stream_state * stream,struct dc_surface_update * srf_updates,int surface_count)5611 static bool commit_minimal_transition_state_in_dc_update(struct dc *dc,
5612 		struct dc_state *new_context,
5613 		struct dc_stream_state *stream,
5614 		struct dc_surface_update *srf_updates,
5615 		int surface_count)
5616 {
5617 	bool success = commit_minimal_transition_based_on_new_context(
5618 				dc, new_context, stream, NULL,
5619 				srf_updates, surface_count);
5620 	if (!success)
5621 		success = commit_minimal_transition_based_on_current_context(dc,
5622 				new_context, stream);
5623 	if (!success)
5624 		DC_LOG_ERROR("Fail to commit a seamless minimal transition state between current and new states.\nThis pipe topology update is non-seamless!\n");
5625 	return success;
5626 }
5627 
5628 /**
5629  * commit_minimal_transition_state - Create a transition pipe split state
5630  *
5631  * @dc: Used to get the current state status
5632  * @transition_base_context: New transition state
5633  *
5634  * In some specific configurations, such as pipe split on multi-display with
5635  * MPO and/or Dynamic ODM, removing a plane may cause unsupported pipe
5636  * programming when moving to new planes. To mitigate those types of problems,
5637  * this function adds a transition state that minimizes pipe usage before
5638  * programming the new configuration. When adding a new plane, the current
5639  * state requires the least pipes, so it is applied without splitting. When
5640  * removing a plane, the new state requires the least pipes, so it is applied
5641  * without splitting.
5642  *
5643  * Return:
5644  * Return false if something is wrong in the transition state.
5645  */
commit_minimal_transition_state(struct dc * dc,struct dc_state * transition_base_context)5646 static bool commit_minimal_transition_state(struct dc *dc,
5647 		struct dc_state *transition_base_context)
5648 {
5649 	struct dc_state *transition_context;
5650 	struct pipe_split_policy_backup policy;
5651 	enum dc_status ret = DC_ERROR_UNEXPECTED;
5652 	unsigned int i, j;
5653 	unsigned int pipe_in_use = 0;
5654 	bool subvp_in_use = false;
5655 	bool odm_in_use = false;
5656 
5657 	/* check current pipes in use*/
5658 	for (i = 0; i < dc->res_pool->pipe_count; i++) {
5659 		struct pipe_ctx *pipe = &transition_base_context->res_ctx.pipe_ctx[i];
5660 
5661 		if (pipe->plane_state)
5662 			pipe_in_use++;
5663 	}
5664 
5665 	/* If SubVP is enabled and we are adding or removing planes from any main subvp
5666 	 * pipe, we must use the minimal transition.
5667 	 */
5668 	for (i = 0; i < dc->res_pool->pipe_count; i++) {
5669 		struct pipe_ctx *pipe = &dc->current_state->res_ctx.pipe_ctx[i];
5670 
5671 		if (pipe->stream && dc_state_get_pipe_subvp_type(dc->current_state, pipe) == SUBVP_PHANTOM) {
5672 			subvp_in_use = true;
5673 			break;
5674 		}
5675 	}
5676 
5677 	/* If ODM is enabled and we are adding or removing planes from any ODM
5678 	 * pipe, we must use the minimal transition.
5679 	 */
5680 	for (i = 0; i < dc->res_pool->pipe_count; i++) {
5681 		struct pipe_ctx *pipe = &transition_base_context->res_ctx.pipe_ctx[i];
5682 
5683 		if (resource_is_pipe_type(pipe, OTG_MASTER)) {
5684 			odm_in_use = resource_get_odm_slice_count(pipe) > 1;
5685 			break;
5686 		}
5687 	}
5688 
5689 	/* When the OS add a new surface if we have been used all of pipes with odm combine
5690 	 * and mpc split feature, it need use commit_minimal_transition_state to transition safely.
5691 	 * After OS exit MPO, it will back to use odm and mpc split with all of pipes, we need
5692 	 * call it again. Otherwise return true to skip.
5693 	 *
5694 	 * Reduce the scenarios to use dc_commit_state_no_check in the stage of flip. Especially
5695 	 * enter/exit MPO when DCN still have enough resources.
5696 	 */
5697 	if (pipe_in_use != dc->res_pool->pipe_count && !subvp_in_use && !odm_in_use)
5698 		return true;
5699 
5700 	DC_LOG_DC("%s base = %s state, reason = %s\n", __func__,
5701 			dc->current_state == transition_base_context ? "current" : "new",
5702 			subvp_in_use ? "Subvp In Use" :
5703 			odm_in_use ? "ODM in Use" :
5704 			dc->debug.pipe_split_policy != MPC_SPLIT_AVOID ? "MPC in Use" :
5705 			"Unknown");
5706 
5707 	dc_state_retain(transition_base_context);
5708 	transition_context = create_minimal_transition_state(dc,
5709 			transition_base_context, &policy);
5710 	if (transition_context) {
5711 		ret = dc_commit_state_no_check(dc, transition_context);
5712 		release_minimal_transition_state(dc, transition_context, transition_base_context, &policy);
5713 	}
5714 	dc_state_release(transition_base_context);
5715 
5716 	if (ret != DC_OK) {
5717 		/* this should never happen */
5718 		BREAK_TO_DEBUGGER();
5719 		return false;
5720 	}
5721 
5722 	/* force full surface update */
5723 	for (i = 0; i < dc->current_state->stream_count; i++) {
5724 		for (j = 0; j < (unsigned int)dc->current_state->stream_status[i].plane_count; j++) {
5725 			dc_pipe_update_bits_set_full(&dc->current_state->stream_status[i].plane_states[j]->update_bits);
5726 		}
5727 	}
5728 
5729 	return true;
5730 }
5731 
populate_fast_updates(struct dc_fast_update * fast_update,struct dc_surface_update * srf_updates,int surface_count,struct dc_stream_update * stream_update)5732 void populate_fast_updates(struct dc_fast_update *fast_update,
5733 		struct dc_surface_update *srf_updates,
5734 		int surface_count,
5735 		struct dc_stream_update *stream_update)
5736 {
5737 	int i = 0;
5738 
5739 	if (stream_update) {
5740 		fast_update[0].out_transfer_func = stream_update->out_transfer_func;
5741 		fast_update[0].output_csc_transform = stream_update->output_csc_transform;
5742 		fast_update[0].cursor_attributes = stream_update->cursor_attributes;
5743 		fast_update[0].cursor_position = stream_update->cursor_position;
5744 		fast_update[0].periodic_interrupt = stream_update->periodic_interrupt;
5745 		fast_update[0].dither_option = stream_update->dither_option;
5746 		fast_update[0].gamut_remap = stream_update->gamut_remap;
5747 		fast_update[0].vrr_infopacket = stream_update->vrr_infopacket;
5748 		fast_update[0].vsc_infopacket = stream_update->vsc_infopacket;
5749 		fast_update[0].vsp_infopacket = stream_update->vsp_infopacket;
5750 		fast_update[0].hfvsif_infopacket = stream_update->hfvsif_infopacket;
5751 		fast_update[0].vtem_infopacket = stream_update->vtem_infopacket;
5752 		fast_update[0].adaptive_sync_infopacket = stream_update->adaptive_sync_infopacket;
5753 		fast_update[0].avi_infopacket = stream_update->avi_infopacket;
5754 		fast_update[0].hdr_static_metadata = stream_update->hdr_static_metadata;
5755 	} else {
5756 		fast_update[0].out_transfer_func = NULL;
5757 		fast_update[0].output_csc_transform = NULL;
5758 		fast_update[0].cursor_attributes = NULL;
5759 		fast_update[0].cursor_position = NULL;
5760 		fast_update[0].periodic_interrupt = NULL;
5761 		fast_update[0].dither_option = NULL;
5762 		fast_update[0].gamut_remap = NULL;
5763 		fast_update[0].vrr_infopacket = NULL;
5764 		fast_update[0].vsc_infopacket = NULL;
5765 		fast_update[0].vsp_infopacket = NULL;
5766 		fast_update[0].hfvsif_infopacket = NULL;
5767 		fast_update[0].vtem_infopacket = NULL;
5768 		fast_update[0].adaptive_sync_infopacket = NULL;
5769 		fast_update[0].avi_infopacket = NULL;
5770 		fast_update[0].hdr_static_metadata = NULL;
5771 	}
5772 
5773 	for (i = 0; i < surface_count; i++) {
5774 		fast_update[i].flip_addr = srf_updates[i].flip_addr;
5775 		fast_update[i].gamma = srf_updates[i].gamma;
5776 		fast_update[i].gamut_remap_matrix = srf_updates[i].gamut_remap_matrix;
5777 		fast_update[i].input_csc_color_matrix = srf_updates[i].input_csc_color_matrix;
5778 		fast_update[i].coeff_reduction_factor = srf_updates[i].coeff_reduction_factor;
5779 		fast_update[i].cursor_csc_color_matrix = srf_updates[i].cursor_csc_color_matrix;
5780 		fast_update[i].cm_hist_control = srf_updates[i].cm_hist_control;
5781 	}
5782 }
5783 
fast_updates_exist(const struct dc_fast_update * fast_update,int surface_count)5784 static bool fast_updates_exist(const struct dc_fast_update *fast_update, int surface_count)
5785 {
5786 	int i;
5787 
5788 	if (fast_update[0].out_transfer_func ||
5789 		fast_update[0].output_csc_transform ||
5790 		fast_update[0].cursor_attributes ||
5791 		fast_update[0].cursor_position ||
5792 		fast_update[0].periodic_interrupt ||
5793 		fast_update[0].dither_option ||
5794 		fast_update[0].gamut_remap ||
5795 		fast_update[0].vrr_infopacket ||
5796 		fast_update[0].vsc_infopacket ||
5797 		fast_update[0].vsp_infopacket ||
5798 		fast_update[0].hfvsif_infopacket ||
5799 		fast_update[0].vtem_infopacket ||
5800 		fast_update[0].adaptive_sync_infopacket ||
5801 		fast_update[0].avi_infopacket ||
5802 		fast_update[0].hdr_static_metadata)
5803 		return true;
5804 
5805 	for (i = 0; i < surface_count; i++) {
5806 		if (fast_update[i].flip_addr ||
5807 				fast_update[i].gamma ||
5808 				fast_update[i].gamut_remap_matrix ||
5809 				fast_update[i].input_csc_color_matrix ||
5810 				fast_update[i].cursor_csc_color_matrix ||
5811 				fast_update[i].cm_hist_control ||
5812 				fast_update[i].coeff_reduction_factor)
5813 			return true;
5814 	}
5815 
5816 	return false;
5817 }
5818 
fast_nonaddr_updates_exist(struct dc_fast_update * fast_update,int surface_count)5819 bool fast_nonaddr_updates_exist(struct dc_fast_update *fast_update, int surface_count)
5820 {
5821 	int i;
5822 
5823 	if (fast_update[0].out_transfer_func ||
5824 		fast_update[0].output_csc_transform ||
5825 		fast_update[0].gamut_remap ||
5826 		fast_update[0].cursor_attributes ||
5827 		fast_update[0].cursor_position ||
5828 		fast_update[0].periodic_interrupt ||
5829 		fast_update[0].dither_option ||
5830 		fast_update[0].vrr_infopacket ||
5831 		fast_update[0].vsc_infopacket ||
5832 		fast_update[0].vsp_infopacket ||
5833 		fast_update[0].hfvsif_infopacket ||
5834 		fast_update[0].vtem_infopacket ||
5835 		fast_update[0].adaptive_sync_infopacket ||
5836 		fast_update[0].avi_infopacket ||
5837 		fast_update[0].hdr_static_metadata)
5838 		return true;
5839 
5840 	for (i = 0; i < surface_count; i++) {
5841 		if (fast_update[i].input_csc_color_matrix ||
5842 				fast_update[i].gamma ||
5843 				fast_update[i].gamut_remap_matrix ||
5844 				fast_update[i].coeff_reduction_factor ||
5845 				fast_update[i].cm_hist_control ||
5846 				fast_update[i].cursor_csc_color_matrix)
5847 			return true;
5848 	}
5849 
5850 	return false;
5851 }
5852 
full_update_required_weak(const struct dc * dc,const struct dc_surface_update * srf_updates,int surface_count,const struct dc_stream_update * stream_update,const struct dc_stream_state * stream)5853 static bool full_update_required_weak(
5854 		const struct dc *dc,
5855 		const struct dc_surface_update *srf_updates,
5856 		int surface_count,
5857 		const struct dc_stream_update *stream_update,
5858 		const struct dc_stream_state *stream)
5859 {
5860 	(void)stream_update;
5861 	const struct dc_state *context = dc->current_state;
5862 	if (srf_updates)
5863 		for (int i = 0; i < surface_count; i++)
5864 			if (!is_surface_in_context(context, srf_updates[i].surface))
5865 				return true;
5866 
5867 	if (stream) {
5868 		const struct dc_stream_status *stream_status = dc_stream_get_status_const(stream);
5869 		if (stream_status == NULL || stream_status->plane_count != surface_count)
5870 			return true;
5871 	}
5872 	if (dc->idle_optimizations_allowed)
5873 		return true;
5874 
5875 	if (dc_can_clear_cursor_limit(dc))
5876 		return true;
5877 
5878 	return false;
5879 }
5880 
full_update_required(const struct dc * dc,const struct dc_surface_update * srf_updates,int surface_count,const struct dc_stream_update * stream_update,const struct dc_stream_state * stream)5881 static bool full_update_required(
5882 		const struct dc *dc,
5883 		const struct dc_surface_update *srf_updates,
5884 		int surface_count,
5885 		const struct dc_stream_update *stream_update,
5886 		const struct dc_stream_state *stream)
5887 {
5888 	if (full_update_required_weak(dc, srf_updates, surface_count, stream_update, stream))
5889 		return true;
5890 
5891 	for (int i = 0; i < surface_count; i++) {
5892 		if (srf_updates &&
5893 				(srf_updates[i].plane_info ||
5894 				srf_updates[i].scaling_info ||
5895 				(srf_updates[i].hdr_mult.value &&
5896 				srf_updates[i].hdr_mult.value != srf_updates->surface->hdr_mult.value) ||
5897 				(srf_updates[i].sdr_white_level_nits &&
5898 				srf_updates[i].sdr_white_level_nits != srf_updates->surface->sdr_white_level_nits) ||
5899 				srf_updates[i].in_transfer_func ||
5900 				srf_updates[i].surface->force_full_update ||
5901 				(srf_updates[i].flip_addr &&
5902 				srf_updates[i].flip_addr->address.tmz_surface != srf_updates[i].surface->address.tmz_surface)))
5903 			return true;
5904 	}
5905 
5906 	if (stream_update &&
5907 			(((stream_update->src.height != 0 && stream_update->src.width != 0) ||
5908 			(stream_update->dst.height != 0 && stream_update->dst.width != 0) ||
5909 			stream_update->integer_scaling_update) ||
5910 			stream_update->abm_level ||
5911 			stream_update->dpms_off ||
5912 			stream_update->allow_freesync ||
5913 			stream_update->vrr_active_variable ||
5914 			stream_update->vrr_active_fixed ||
5915 			stream_update->output_color_space ||
5916 			stream_update->wb_update ||
5917 			stream_update->dsc_config ||
5918 			stream_update->mst_bw_update ||
5919 			stream_update->func_shaper ||
5920 			stream_update->lut3d_func ||
5921 			stream_update->pending_test_pattern ||
5922 			stream_update->crtc_timing_adjust ||
5923 			stream_update->scaler_sharpener_update ||
5924 			stream_update->hw_cursor_req))
5925 		return true;
5926 
5927 	return false;
5928 }
5929 
fast_update_only(const struct dc * dc,const struct dc_fast_update * fast_update,const struct dc_surface_update * srf_updates,int surface_count,const struct dc_stream_update * stream_update,const struct dc_stream_state * stream)5930 static bool fast_update_only(
5931 		const struct dc *dc,
5932 		const struct dc_fast_update *fast_update,
5933 		const struct dc_surface_update *srf_updates,
5934 		int surface_count,
5935 		const struct dc_stream_update *stream_update,
5936 		const struct dc_stream_state *stream)
5937 {
5938 	return fast_updates_exist(fast_update, surface_count)
5939 			&& !full_update_required(dc, srf_updates, surface_count, stream_update, stream);
5940 }
5941 
update_planes_and_stream_v2(struct dc * dc,struct dc_surface_update * srf_updates,int surface_count,struct dc_stream_state * stream,struct dc_stream_update * stream_update)5942 static bool update_planes_and_stream_v2(struct dc *dc,
5943 		struct dc_surface_update *srf_updates, int surface_count,
5944 		struct dc_stream_state *stream,
5945 		struct dc_stream_update *stream_update)
5946 {
5947 	struct dc_state *context;
5948 	enum dc_update_type update_type;
5949 	struct dc_fast_update fast_update[MAX_SURFACES] = {0};
5950 
5951 	/* In cases where MPO and split or ODM are used transitions can
5952 	 * cause underflow. Apply stream configuration with minimal pipe
5953 	 * split first to avoid unsupported transitions for active pipes.
5954 	 */
5955 	bool force_minimal_pipe_splitting = 0;
5956 	bool is_plane_addition = 0;
5957 	bool is_fast_update_only;
5958 
5959 	populate_fast_updates(fast_update, srf_updates, surface_count, stream_update);
5960 	is_fast_update_only = fast_update_only(dc, fast_update, srf_updates,
5961 			surface_count, stream_update, stream);
5962 	force_minimal_pipe_splitting = could_mpcc_tree_change_for_active_pipes(
5963 			dc,
5964 			stream,
5965 			srf_updates,
5966 			surface_count,
5967 			&is_plane_addition);
5968 
5969 	/* on plane addition, minimal state is the current one */
5970 	if (force_minimal_pipe_splitting && is_plane_addition &&
5971 		!commit_minimal_transition_state(dc, dc->current_state))
5972 		return false;
5973 
5974 	if (!update_planes_and_stream_state(
5975 			dc,
5976 			srf_updates,
5977 			surface_count,
5978 			stream,
5979 			stream_update,
5980 			&update_type,
5981 			&context))
5982 		return false;
5983 
5984 	/* on plane removal, minimal state is the new one */
5985 	if (force_minimal_pipe_splitting && !is_plane_addition) {
5986 		if (!commit_minimal_transition_state(dc, context)) {
5987 			dc_state_release(context);
5988 			return false;
5989 		}
5990 		update_type = UPDATE_TYPE_FULL;
5991 	}
5992 
5993 	if (dc->hwss.is_pipe_topology_transition_seamless &&
5994 			!dc->hwss.is_pipe_topology_transition_seamless(
5995 					dc, dc->current_state, context))
5996 		commit_minimal_transition_state_in_dc_update(dc, context, stream,
5997 				srf_updates, surface_count);
5998 
5999 	if (is_fast_update_only && !dc->check_config.enable_legacy_fast_update) {
6000 		commit_planes_for_stream_fast(dc,
6001 				srf_updates,
6002 				surface_count,
6003 				stream,
6004 				stream_update,
6005 				update_type,
6006 				context);
6007 	} else {
6008 		if (!stream_update &&
6009 				dc->hwss.is_pipe_topology_transition_seamless &&
6010 				!dc->hwss.is_pipe_topology_transition_seamless(
6011 						dc, dc->current_state, context)) {
6012 			DC_LOG_ERROR("performing non-seamless pipe topology transition with surface only update!\n");
6013 			BREAK_TO_DEBUGGER();
6014 		}
6015 		commit_planes_for_stream(
6016 				dc,
6017 				srf_updates,
6018 				surface_count,
6019 				stream,
6020 				stream_update,
6021 				update_type,
6022 				context);
6023 	}
6024 	if (dc->current_state != context)
6025 		swap_and_release_current_context(dc, context, stream);
6026 	return true;
6027 }
6028 
commit_planes_and_stream_update_on_current_context(struct dc * dc,struct dc_surface_update * srf_updates,int surface_count,struct dc_stream_state * stream,struct dc_stream_update * stream_update,enum dc_update_type update_type)6029 static void commit_planes_and_stream_update_on_current_context(struct dc *dc,
6030 		struct dc_surface_update *srf_updates, int surface_count,
6031 		struct dc_stream_state *stream,
6032 		struct dc_stream_update *stream_update,
6033 		enum dc_update_type update_type)
6034 {
6035 	struct dc_fast_update fast_update[MAX_SURFACES] = {0};
6036 
6037 	ASSERT(update_type < UPDATE_TYPE_FULL);
6038 	populate_fast_updates(fast_update, srf_updates, surface_count,
6039 			stream_update);
6040 	if (fast_update_only(dc, fast_update, srf_updates, surface_count,
6041 			stream_update, stream) &&
6042 			!dc->check_config.enable_legacy_fast_update)
6043 		commit_planes_for_stream_fast(dc,
6044 				srf_updates,
6045 				surface_count,
6046 				stream,
6047 				stream_update,
6048 				update_type,
6049 				dc->current_state);
6050 	else
6051 		commit_planes_for_stream(
6052 				dc,
6053 				srf_updates,
6054 				surface_count,
6055 				stream,
6056 				stream_update,
6057 				update_type,
6058 				dc->current_state);
6059 }
6060 
commit_planes_and_stream_update_with_new_context(struct dc * dc,struct dc_surface_update * srf_updates,int surface_count,struct dc_stream_state * stream,struct dc_stream_update * stream_update,enum dc_update_type update_type,struct dc_state * new_context)6061 static void commit_planes_and_stream_update_with_new_context(struct dc *dc,
6062 		struct dc_surface_update *srf_updates, int surface_count,
6063 		struct dc_stream_state *stream,
6064 		struct dc_stream_update *stream_update,
6065 		enum dc_update_type update_type,
6066 		struct dc_state *new_context)
6067 {
6068 	bool skip_new_context = false;
6069 	ASSERT(update_type >= UPDATE_TYPE_FULL);
6070 	/*
6071 	 * It is required by the feature design that all pipe topologies
6072 	 * using extra free pipes for power saving purposes such as
6073 	 * dynamic ODM or SubVp shall only be enabled when it can be
6074 	 * transitioned seamlessly to AND from its minimal transition
6075 	 * state. A minimal transition state is defined as the same dc
6076 	 * state but with all power saving features disabled. So it uses
6077 	 * the minimum pipe topology. When we can't seamlessly
6078 	 * transition from state A to state B, we will insert the
6079 	 * minimal transition state A' or B' in between so seamless
6080 	 * transition between A and B can be made possible.
6081 	 *
6082 	 * To optimize for the time it takes to execute flips,
6083 	 * the transition from the minimal state to the final state is
6084 	 * deferred until a steady state (no more transitions) is reached.
6085 	 */
6086 	if (!dc->hwss.is_pipe_topology_transition_seamless(dc, dc->current_state, new_context)) {
6087 		if (!dc->debug.disable_deferred_minimal_transitions) {
6088 			dc->check_config.deferred_transition_state = true;
6089 			dc->check_config.transition_countdown_to_steady_state =
6090 					dc->debug.num_fast_flips_to_steady_state_override ?
6091 					dc->debug.num_fast_flips_to_steady_state_override :
6092 					NUM_FAST_FLIPS_TO_STEADY_STATE;
6093 
6094 			if (commit_minimal_transition_based_on_new_context(dc, new_context, stream, stream_update,
6095 					srf_updates, surface_count)) {
6096 				skip_new_context = true;
6097 				dc_state_release(new_context);
6098 				new_context = dc->current_state;
6099 			} else {
6100 				/*
6101 				 * In this case a new mpo plane is being enabled on pipes that were
6102 				 * previously in use, and the surface update to the existing plane
6103 				 * includes an alpha box where the new plane will be, so the update
6104 				 * from minimal to final cannot be deferred as the alpha box would
6105 				 * be visible to the user
6106 				 */
6107 				commit_minimal_transition_based_on_current_context(dc, new_context, stream);
6108 			}
6109 		} else {
6110 			commit_minimal_transition_state_in_dc_update(dc, new_context, stream,
6111 					srf_updates, surface_count);
6112 		}
6113 	} else if (dc->check_config.deferred_transition_state) {
6114 		/* reset countdown as steady state not reached */
6115 		dc->check_config.transition_countdown_to_steady_state =
6116 				dc->debug.num_fast_flips_to_steady_state_override ?
6117 				dc->debug.num_fast_flips_to_steady_state_override :
6118 				NUM_FAST_FLIPS_TO_STEADY_STATE;
6119 	}
6120 
6121 	if (!skip_new_context) {
6122 		commit_planes_for_stream(dc, srf_updates, surface_count, stream, stream_update, update_type, new_context);
6123 		swap_and_release_current_context(dc, new_context, stream);
6124 	}
6125 }
6126 
update_planes_and_stream_v3(struct dc * dc,struct dc_surface_update * srf_updates,int surface_count,struct dc_stream_state * stream,struct dc_stream_update * stream_update)6127 static bool update_planes_and_stream_v3(struct dc *dc,
6128 		struct dc_surface_update *srf_updates, int surface_count,
6129 		struct dc_stream_state *stream,
6130 		struct dc_stream_update *stream_update)
6131 {
6132 	struct dc_state *new_context;
6133 	enum dc_update_type update_type;
6134 
6135 	/*
6136 	 * When this function returns true and new_context is not equal to
6137 	 * current state, the function allocates and validates a new dc state
6138 	 * and assigns it to new_context. The function expects that the caller
6139 	 * is responsible to free this memory when new_context is no longer
6140 	 * used. We swap current with new context and free current instead. So
6141 	 * new_context's memory will live until the next full update after it is
6142 	 * replaced by a newer context. Refer to the use of
6143 	 * swap_and_free_current_context below.
6144 	 */
6145 	if (!update_planes_and_stream_state(dc, srf_updates, surface_count,
6146 				stream, stream_update, &update_type,
6147 				&new_context))
6148 		return false;
6149 
6150 	if (new_context == dc->current_state) {
6151 		commit_planes_and_stream_update_on_current_context(dc,
6152 				srf_updates, surface_count, stream,
6153 				stream_update, update_type);
6154 
6155 		if (dc->check_config.transition_countdown_to_steady_state)
6156 			dc->check_config.transition_countdown_to_steady_state--;
6157 	} else {
6158 		commit_planes_and_stream_update_with_new_context(dc,
6159 				srf_updates, surface_count, stream,
6160 				stream_update, update_type, new_context);
6161 	}
6162 
6163 	return true;
6164 }
6165 
clear_update_bits(struct dc_surface_update * srf_updates,int surface_count,struct dc_stream_state * stream)6166 static void clear_update_bits(struct dc_surface_update *srf_updates,
6167 	int surface_count, struct dc_stream_state *stream)
6168 {
6169 	int i;
6170 
6171 	if (stream)
6172 		stream_update_flags_clear(&stream->update_flags);
6173 
6174 	for (i = 0; i < surface_count; i++)
6175 		if (srf_updates[i].surface)
6176 			dc_pipe_update_bits_clear(&srf_updates[i].surface->update_bits);
6177 }
6178 
dc_update_scratch_acquire(struct dc * dc)6179 static struct dc_update_scratch_space *dc_update_scratch_acquire(struct dc *dc)
6180 {
6181 	unsigned int i;
6182 
6183 	for (i = 0; i < ARRAY_SIZE(dc->update_scratch_pool); i++) {
6184 		if (dc->update_scratch_in_use[i])
6185 			continue;
6186 
6187 		dc->update_scratch_in_use[i] = true;
6188 		return dc->update_scratch_pool[i];
6189 	}
6190 
6191 	/* TODO: add recoverable scratch acquisition failure handling. */
6192 	ASSERT(false);
6193 	return NULL;
6194 }
6195 
dc_update_scratch_release(struct dc * dc,struct dc_update_scratch_space * scratch)6196 static void dc_update_scratch_release(struct dc *dc,
6197 		struct dc_update_scratch_space *scratch)
6198 {
6199 	unsigned int i;
6200 
6201 	for (i = 0; i < ARRAY_SIZE(dc->update_scratch_pool); i++) {
6202 		if (dc->update_scratch_pool[i] == scratch) {
6203 			dc->update_scratch_in_use[i] = false;
6204 			return;
6205 		}
6206 	}
6207 }
6208 
6209 /**
6210  * dc_update_state - Commit an absolute dc_state_update.
6211  * @dc:      DC structure
6212  * @updates: root update object carrying stream, plane, and probe updates
6213  * Return: true on success, false on failure.
6214  */
dc_update_state(struct dc * dc,const struct dc_state_update * updates)6215 bool dc_update_state(struct dc *dc, const struct dc_state_update *updates)
6216 {
6217 	struct dc_update_scratch_space *scratch;
6218 	bool more = true;
6219 
6220 	if (!dc || !updates)
6221 		return false;
6222 
6223 	scratch = dc_update_state_init(dc, updates);
6224 	if (!scratch)
6225 		return false;
6226 
6227 	while (more) {
6228 		if (!dc_update_state_prepare(scratch))
6229 			return false;
6230 
6231 		dc_update_state_execute(scratch);
6232 		more = dc_update_state_cleanup(scratch);
6233 	}
6234 
6235 	return true;
6236 }
6237 
6238 /**
6239  * dc_update_planes_and_stream - Shim for dc_update_state.
6240  * @dc:             DC structure
6241  * @srf_updates:    array of surface update descriptors
6242  * @surface_count:  number of entries in @srf_updates
6243  * @stream:         target stream
6244  * @stream_update:  optional stream update
6245  *
6246  * Packs the individual arguments into a dc_state_update and forwards to
6247  * dc_update_state(). Preserved for out-of-tree and incremental callers.
6248  *
6249  * Return: true on success; false on failure.
6250  */
dc_update_planes_and_stream(struct dc * dc,struct dc_surface_update * srf_updates,int surface_count,struct dc_stream_state * stream,struct dc_stream_update * stream_update)6251 bool dc_update_planes_and_stream(struct dc *dc,
6252 		struct dc_surface_update *srf_updates, int surface_count,
6253 		struct dc_stream_state *stream,
6254 		struct dc_stream_update *stream_update)
6255 {
6256 	struct dc_state_update updates = {
6257 		.stream          = stream,
6258 		.stream_update   = stream_update,
6259 		.surface_updates = srf_updates,
6260 		.surface_count   = surface_count,
6261 	};
6262 
6263 	return dc_update_state(dc, &updates);
6264 }
6265 
dc_commit_updates_for_stream(struct dc * dc,struct dc_surface_update * srf_updates,int surface_count,struct dc_stream_state * stream,struct dc_stream_update * stream_update,struct dc_state * state)6266 void dc_commit_updates_for_stream(struct dc *dc,
6267 		struct dc_surface_update *srf_updates,
6268 		int surface_count,
6269 		struct dc_stream_state *stream,
6270 		struct dc_stream_update *stream_update,
6271 		struct dc_state *state)
6272 {
6273 	(void)state;
6274 	bool ret = false;
6275 
6276 	dc_exit_ips_for_hw_access(dc);
6277 	/* TODO: Since change commit sequence can have a huge impact,
6278 	 * we decided to only enable it for DCN3x. However, as soon as
6279 	 * we get more confident about this change we'll need to enable
6280 	 * the new sequence for all ASICs.
6281 	 */
6282 	if (dc->ctx->dce_version >= DCN_VERSION_4_01) {
6283 		ret = update_planes_and_stream_v3(dc, srf_updates, surface_count,
6284 				stream, stream_update);
6285 	} else {
6286 		ret = update_planes_and_stream_v2(dc, srf_updates, surface_count,
6287 				stream, stream_update);
6288 	}
6289 
6290 	if (ret && dc->ctx->dce_version >= DCN_VERSION_3_2)
6291 		clear_update_bits(srf_updates, surface_count, stream);
6292 }
6293 
dc_get_current_stream_count(struct dc * dc)6294 uint8_t dc_get_current_stream_count(struct dc *dc)
6295 {
6296 	return dc->current_state->stream_count;
6297 }
6298 
dc_get_stream_at_index(struct dc * dc,uint8_t i)6299 struct dc_stream_state *dc_get_stream_at_index(struct dc *dc, uint8_t i)
6300 {
6301 	if (i < dc->current_state->stream_count)
6302 		return dc->current_state->streams[i];
6303 	return NULL;
6304 }
6305 
dc_interrupt_to_irq_source(struct dc * dc,uint32_t src_id,uint32_t ext_id)6306 enum dc_irq_source dc_interrupt_to_irq_source(
6307 		struct dc *dc,
6308 		uint32_t src_id,
6309 		uint32_t ext_id)
6310 {
6311 	return dal_irq_service_to_irq_source(dc->res_pool->irqs, src_id, ext_id);
6312 }
6313 
6314 /*
6315  * dc_interrupt_set() - Enable/disable an AMD hw interrupt source
6316  */
dc_interrupt_set(struct dc * dc,enum dc_irq_source src,bool enable)6317 bool dc_interrupt_set(struct dc *dc, enum dc_irq_source src, bool enable)
6318 {
6319 
6320 	if (dc == NULL)
6321 		return false;
6322 
6323 	return dal_irq_service_set(dc->res_pool->irqs, src, enable);
6324 }
6325 
dc_interrupt_ack(struct dc * dc,enum dc_irq_source src)6326 void dc_interrupt_ack(struct dc *dc, enum dc_irq_source src)
6327 {
6328 	dal_irq_service_ack(dc->res_pool->irqs, src);
6329 }
6330 
6331 /* Preserve this tg if a physical link is still lighting a present display */
should_preserve_tg(struct dc * dc,struct timing_generator * tg)6332 static bool should_preserve_tg(struct dc *dc, struct timing_generator *tg)
6333 {
6334 	unsigned int i, j;
6335 
6336 	/* Check if a physical link is lighting this tg */
6337 	for (i = 0; i < dc->link_count; i++) {
6338 		struct dc_link *link = dc->links[i];
6339 		int fe;
6340 
6341 		if (!link || link->ep_type != DISPLAY_ENDPOINT_PHY ||
6342 				!link->link_enc ||
6343 				!link->link_enc->funcs->is_dig_enabled ||
6344 				!link->link_enc->funcs->is_dig_enabled(link->link_enc) ||
6345 				!link->link_enc->funcs->get_dig_frontend)
6346 			continue;
6347 
6348 		/* Get the DIG front-end this link's encoder drives; skip if none */
6349 		fe = link->link_enc->funcs->get_dig_frontend(link->link_enc);
6350 		if (fe == ENGINE_ID_UNKNOWN)
6351 			continue;
6352 
6353 		/* Find the stream encoder bound to this link's front-end */
6354 		for (j = 0; j < dc->res_pool->stream_enc_count; j++) {
6355 			struct stream_encoder *se = dc->res_pool->stream_enc[j];
6356 
6357 			/* Skip unless this stream encoder feeds our front-end and drives this tg */
6358 			if (se->id != fe || !se->funcs->dig_source_otg ||
6359 					(int)se->funcs->dig_source_otg(se) != tg->inst)
6360 				continue;
6361 
6362 			/* This link drives the OTG: keep a seamless-boot eDP, or
6363 			 * any external link whose sink is still connected.
6364 			 */
6365 			if (link->connector_signal == SIGNAL_TYPE_EDP)
6366 				return true;
6367 			if (link->link_enc->funcs->get_hpd_state &&
6368 					dc->link_srv->get_hpd_state(link))
6369 				return true;
6370 		}
6371 	}
6372 
6373 	return false;
6374 }
6375 
6376 /*
6377  * GOP/vBIOS may leave an OPTC enabled for a display present at power-on but no
6378  * longer driven (e.g. external DP unplugged at boot). Such a dangling pipe keeps
6379  * DCN out of idle and blocks s0i3. If nothing needs to survive (no committed
6380  * stream or seamless-boot eDP) and no sink is still connected, power down all hw
6381  * blocks.
6382  */
dc_disable_dangling_timing_generators(struct dc * dc)6383 void dc_disable_dangling_timing_generators(struct dc *dc)
6384 {
6385 	struct dce_hwseq *hws = dc->hwseq;
6386 	bool any_dangling = false;
6387 	bool any_preserved = false;
6388 	bool any_connected = false;
6389 	unsigned int i;
6390 
6391 	/* No real hw to touch on a virtual/emulated environment */
6392 	if (dc->ctx->dce_environment == DCE_ENV_VIRTUAL_HW)
6393 		return;
6394 
6395 	/* Wake hw out of IPS before reading/touching tg state */
6396 	dc_exit_ips_for_hw_access(dc);
6397 
6398 	/* Classify every enabled tg as either to-preserve or dangling */
6399 	for (i = 0; i < dc->res_pool->timing_generator_count; i++) {
6400 		struct timing_generator *tg = dc->res_pool->timing_generators[i];
6401 
6402 		if (!tg || !tg->funcs->is_tg_enabled ||
6403 				!tg->funcs->is_tg_enabled(tg))
6404 			continue;
6405 
6406 		if (should_preserve_tg(dc, tg))
6407 			any_preserved = true;
6408 		else
6409 			any_dangling = true;
6410 	}
6411 
6412 	/* A physically connected sink (HPD asserted) will be re-lit by a
6413 	 * subsequent atomic commit. For that case we don't call the global
6414 	 * power_down().
6415 	 */
6416 	for (i = 0; i < dc->link_count; i++) {
6417 		struct dc_link *link = dc->links[i];
6418 
6419 		if (link && link->ep_type == DISPLAY_ENDPOINT_PHY &&
6420 				link->link_enc && link->link_enc->funcs &&
6421 				link->link_enc->funcs->get_hpd_state &&
6422 				dc->link_srv->get_hpd_state(link)) {
6423 			any_connected = true;
6424 			break;
6425 		}
6426 	}
6427 
6428 	if (!any_dangling)
6429 		return;
6430 
6431 	if (!any_preserved && !any_connected && hws && hws->funcs.power_down) {
6432 		/* Truly headless / all sinks unplugged: nothing to preserve */
6433 		DC_LOG_DC("%s: powering down dangling hw blocks to allow idle\n",
6434 				__func__);
6435 		hws->funcs.power_down(dc);
6436 		return;
6437 	}
6438 }
6439 
6440 /*
6441  * dc_get_flip_pending_on_otg() - Check if a GRPH_FLIP is still pending on OTG
6442  *
6443  * @dc: display core context @otg_inst: OTG instance to query
6444  *
6445  * Reads the HUBP flip-pending status for the pipe(s) bound to @otg_inst,
6446  * returning true if any of them has not yet latched its programmed surface
6447  * address.
6448  *
6449  * Unlike dc_plane_get_status(), this does not take or mutate a dc_plane_state,
6450  * so it is safe to call from interrupt context without racing a concurrent
6451  * commit that may be updating plane state.
6452  *
6453  * Return: true if a flip is still pending on the OTG, false otherwise.
6454  */
dc_get_flip_pending_on_otg(struct dc * dc,int otg_inst)6455 bool dc_get_flip_pending_on_otg(struct dc *dc, int otg_inst)
6456 {
6457 	bool flip_pending = false;
6458 	unsigned int i;
6459 
6460 	if (!dc || !dc->current_state)
6461 		return false;
6462 
6463 	dc_exit_ips_for_hw_access(dc);
6464 
6465 	for (i = 0; i < dc->res_pool->pipe_count; i++) {
6466 		struct pipe_ctx *pipe_ctx = &dc->current_state->res_ctx.pipe_ctx[i];
6467 		struct hubp *hubp = pipe_ctx->plane_res.hubp;
6468 
6469 		if (!pipe_ctx->plane_state || !pipe_ctx->stream_res.tg)
6470 			continue;
6471 
6472 		if (pipe_ctx->stream_res.tg->inst != otg_inst)
6473 			continue;
6474 
6475 		if (hubp && hubp->funcs->hubp_is_flip_pending &&
6476 		    hubp->funcs->hubp_is_flip_pending(hubp)) {
6477 			flip_pending = true;
6478 			break;
6479 		}
6480 	}
6481 
6482 	return flip_pending;
6483 }
6484 
dc_power_down_on_boot(struct dc * dc)6485 void dc_power_down_on_boot(struct dc *dc)
6486 {
6487 	if (dc->ctx->dce_environment != DCE_ENV_VIRTUAL_HW &&
6488 	    dc->hwss.power_down_on_boot) {
6489 		if (dc->current_state->stream_count > 0)
6490 			return;
6491 
6492 		if (dc->caps.ips_support)
6493 			dc_exit_ips_for_hw_access(dc);
6494 		dc->hwss.power_down_on_boot(dc);
6495 
6496 		if (dc->clk_mgr && dc->clk_mgr->funcs && dc->clk_mgr->funcs->notify_cstate_disable)
6497 			dc->clk_mgr->funcs->notify_cstate_disable(dc->clk_mgr, false);
6498 	}
6499 }
6500 
dc_set_power_state(struct dc * dc,enum dc_acpi_cm_power_state power_state)6501 void dc_set_power_state(struct dc *dc, enum dc_acpi_cm_power_state power_state)
6502 {
6503 	if (!dc->current_state)
6504 		return;
6505 
6506 	dc_exit_ips_for_hw_access(dc);
6507 
6508 	switch (power_state) {
6509 	case DC_ACPI_CM_POWER_STATE_D0:
6510 		dc_state_construct(dc, dc->current_state);
6511 
6512 		dc_z10_restore(dc);
6513 
6514 		dc_dmub_srv_notify_fw_dc_power_state(dc->ctx->dmub_srv, power_state);
6515 
6516 		dc->hwss.init_hw(dc);
6517 
6518 		if (dc->hwss.init_sys_ctx != NULL &&
6519 			dc->vm_pa_config.valid) {
6520 			dc->hwss.init_sys_ctx(dc->hwseq, dc, &dc->vm_pa_config);
6521 		}
6522 		break;
6523 	case DC_ACPI_CM_POWER_STATE_D3:
6524 		if (dc->caps.ips_support)
6525 			dc_dmub_srv_notify_fw_dc_power_state(dc->ctx->dmub_srv, DC_ACPI_CM_POWER_STATE_D3);
6526 
6527 		if (dc->caps.ips_v2_support) {
6528 			if (dc->clk_mgr->funcs->set_low_power_state)
6529 				dc->clk_mgr->funcs->set_low_power_state(dc->clk_mgr);
6530 		}
6531 		break;
6532 	default:
6533 		ASSERT(dc->current_state->stream_count == 0);
6534 		dc_dmub_srv_notify_fw_dc_power_state(dc->ctx->dmub_srv, power_state);
6535 
6536 		dc_state_destruct(dc->current_state);
6537 
6538 		break;
6539 	}
6540 }
6541 
dc_resume(struct dc * dc)6542 void dc_resume(struct dc *dc)
6543 {
6544 	uint32_t i;
6545 
6546 	for (i = 0; i < dc->link_count; i++)
6547 		dc->link_srv->resume(dc->links[i]);
6548 }
6549 
dc_is_dmcu_initialized(struct dc * dc)6550 bool dc_is_dmcu_initialized(struct dc *dc)
6551 {
6552 	struct dmcu *dmcu = dc->res_pool->dmcu;
6553 
6554 	if (dmcu)
6555 		return dmcu->funcs->is_dmcu_initialized(dmcu);
6556 	return false;
6557 }
6558 
dc_set_clock(struct dc * dc,enum dc_clock_type clock_type,uint32_t clk_khz,uint32_t stepping)6559 enum dc_status dc_set_clock(struct dc *dc, enum dc_clock_type clock_type, uint32_t clk_khz, uint32_t stepping)
6560 {
6561 	if (dc->hwss.set_clock)
6562 		return dc->hwss.set_clock(dc, clock_type, clk_khz, stepping);
6563 	return DC_ERROR_UNEXPECTED;
6564 }
dc_get_clock(struct dc * dc,enum dc_clock_type clock_type,struct dc_clock_config * clock_cfg)6565 void dc_get_clock(struct dc *dc, enum dc_clock_type clock_type, struct dc_clock_config *clock_cfg)
6566 {
6567 	if (dc->hwss.get_clock)
6568 		dc->hwss.get_clock(dc, clock_type, clock_cfg);
6569 }
6570 
6571 /* enable/disable eDP PSR without specify stream for eDP */
dc_set_psr_allow_active(struct dc * dc,bool enable)6572 bool dc_set_psr_allow_active(struct dc *dc, bool enable)
6573 {
6574 	int i;
6575 	bool allow_active;
6576 
6577 	for (i = 0; i < dc->current_state->stream_count ; i++) {
6578 		struct dc_link *link;
6579 		struct dc_stream_state *stream = dc->current_state->streams[i];
6580 
6581 		link = stream->link;
6582 		if (!link)
6583 			continue;
6584 
6585 		if (link->psr_settings.psr_feature_enabled) {
6586 			if (enable && !link->psr_settings.psr_allow_active) {
6587 				allow_active = true;
6588 				if (!dc_link_set_psr_allow_active(link, &allow_active, false, false, NULL))
6589 					return false;
6590 			} else if (!enable && link->psr_settings.psr_allow_active) {
6591 				allow_active = false;
6592 				if (!dc_link_set_psr_allow_active(link, &allow_active, true, false, NULL))
6593 					return false;
6594 			}
6595 		}
6596 	}
6597 
6598 	return true;
6599 }
6600 
6601 /* enable/disable eDP Replay without specify stream for eDP */
dc_set_replay_allow_active(struct dc * dc,bool active)6602 bool dc_set_replay_allow_active(struct dc *dc, bool active)
6603 {
6604 	int i;
6605 	bool allow_active;
6606 
6607 	for (i = 0; i < dc->current_state->stream_count; i++) {
6608 		struct dc_link *link;
6609 		struct dc_stream_state *stream = dc->current_state->streams[i];
6610 
6611 		link = stream->link;
6612 		if (!link)
6613 			continue;
6614 
6615 		if (link->replay_settings.replay_feature_enabled) {
6616 			if (active && !link->replay_settings.replay_allow_active) {
6617 				allow_active = true;
6618 				if (!dc_link_set_replay_allow_active(link, &allow_active,
6619 					false, false, NULL))
6620 					return false;
6621 			} else if (!active && link->replay_settings.replay_allow_active) {
6622 				allow_active = false;
6623 				if (!dc_link_set_replay_allow_active(link, &allow_active,
6624 					true, false, NULL))
6625 					return false;
6626 			}
6627 		}
6628 	}
6629 
6630 	return true;
6631 }
6632 
6633 /* set IPS disable state */
dc_set_ips_disable(struct dc * dc,unsigned int disable_ips)6634 bool dc_set_ips_disable(struct dc *dc, unsigned int disable_ips)
6635 {
6636 	dc_exit_ips_for_hw_access(dc);
6637 
6638 	dc->config.disable_ips = disable_ips;
6639 
6640 	return true;
6641 }
6642 
dc_allow_idle_optimizations_internal(struct dc * dc,bool allow,char const * caller_name)6643 void dc_allow_idle_optimizations_internal(struct dc *dc, bool allow, char const *caller_name)
6644 {
6645 	int idle_fclk_khz = 0, idle_dramclk_khz = 0;
6646 	unsigned int i = 0;
6647 	enum mall_stream_type subvp_pipe_type[MAX_PIPES] = {0};
6648 	struct pipe_ctx *pipe = NULL;
6649 	struct dc_state *context = dc->current_state;
6650 
6651 	if (dc->debug.disable_idle_power_optimizations) {
6652 		DC_LOG_DEBUG("%s: disabled\n", __func__);
6653 		return;
6654 	}
6655 
6656 	if (allow != dc->idle_optimizations_allowed)
6657 		DC_LOG_IPS("%s: allow_idle old=%d new=%d (caller=%s)\n", __func__,
6658 			   dc->idle_optimizations_allowed, allow, caller_name);
6659 
6660 	if (dc->caps.ips_support && (dc->config.disable_ips == DMUB_IPS_DISABLE_ALL))
6661 		return;
6662 
6663 	if (dc->clk_mgr != NULL && dc->clk_mgr->funcs->is_smu_present)
6664 		if (!dc->clk_mgr->funcs->is_smu_present(dc->clk_mgr))
6665 			return;
6666 
6667 	if (allow == dc->idle_optimizations_allowed)
6668 		return;
6669 
6670 	if (dc->hwss.apply_idle_power_optimizations && dc->clk_mgr != NULL &&
6671 	    dc->hwss.apply_idle_power_optimizations(dc, allow)) {
6672 		dc->idle_optimizations_allowed = allow;
6673 		DC_LOG_DEBUG("%s: %s\n", __func__, allow ? "enabled" : "disabled");
6674 	}
6675 
6676 	// log idle clocks and sub vp pipe types at idle optimization time
6677 	if (dc->clk_mgr != NULL && dc->clk_mgr->funcs->get_hard_min_fclk)
6678 		idle_fclk_khz = dc->clk_mgr->funcs->get_hard_min_fclk(dc->clk_mgr);
6679 
6680 	if (dc->clk_mgr != NULL && dc->clk_mgr->funcs->get_hard_min_memclk)
6681 		idle_dramclk_khz = dc->clk_mgr->funcs->get_hard_min_memclk(dc->clk_mgr);
6682 
6683 	if (dc->res_pool && context) {
6684 		for (i = 0; i < dc->res_pool->pipe_count; i++) {
6685 			pipe = &context->res_ctx.pipe_ctx[i];
6686 			subvp_pipe_type[i] = dc_state_get_pipe_subvp_type(context, pipe);
6687 		}
6688 	}
6689 	if (!dc->caps.is_apu)
6690 		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",
6691 			__func__, allow, idle_fclk_khz, idle_dramclk_khz, subvp_pipe_type[0], subvp_pipe_type[1], subvp_pipe_type[2],
6692 			subvp_pipe_type[3], subvp_pipe_type[4], subvp_pipe_type[5], caller_name);
6693 
6694 }
6695 
dc_exit_ips_for_hw_access_internal(struct dc * dc,const char * caller_name)6696 void dc_exit_ips_for_hw_access_internal(struct dc *dc, const char *caller_name)
6697 {
6698 	if (dc->caps.ips_support)
6699 		dc_allow_idle_optimizations_internal(dc, false, caller_name);
6700 }
6701 
dc_dmub_is_ips_idle_state(struct dc * dc)6702 bool dc_dmub_is_ips_idle_state(struct dc *dc)
6703 {
6704 	if (dc->debug.disable_idle_power_optimizations)
6705 		return false;
6706 
6707 	if (!dc->caps.ips_support || (dc->config.disable_ips == DMUB_IPS_DISABLE_ALL))
6708 		return false;
6709 
6710 	if (!dc->ctx->dmub_srv)
6711 		return false;
6712 
6713 	return dc->ctx->dmub_srv->idle_allowed;
6714 }
6715 
6716 /* set min and max memory clock to lowest and highest DPM level, respectively */
dc_unlock_memory_clock_frequency(struct dc * dc)6717 void dc_unlock_memory_clock_frequency(struct dc *dc)
6718 {
6719 	if (dc->clk_mgr->funcs->set_hard_min_memclk)
6720 		dc->clk_mgr->funcs->set_hard_min_memclk(dc->clk_mgr, false);
6721 
6722 	if (dc->clk_mgr->funcs->set_hard_max_memclk)
6723 		dc->clk_mgr->funcs->set_hard_max_memclk(dc->clk_mgr);
6724 }
6725 
6726 /* set min memory clock to the min required for current mode, max to maxDPM */
dc_lock_memory_clock_frequency(struct dc * dc)6727 void dc_lock_memory_clock_frequency(struct dc *dc)
6728 {
6729 	if (dc->clk_mgr->funcs->get_memclk_states_from_smu)
6730 		dc->clk_mgr->funcs->get_memclk_states_from_smu(dc->clk_mgr);
6731 
6732 	if (dc->clk_mgr->funcs->set_hard_min_memclk)
6733 		dc->clk_mgr->funcs->set_hard_min_memclk(dc->clk_mgr, true);
6734 
6735 	if (dc->clk_mgr->funcs->set_hard_max_memclk)
6736 		dc->clk_mgr->funcs->set_hard_max_memclk(dc->clk_mgr);
6737 }
6738 
blank_and_force_memclk(struct dc * dc,bool apply,unsigned int memclk_mhz)6739 static void blank_and_force_memclk(struct dc *dc, bool apply, unsigned int memclk_mhz)
6740 {
6741 	(void)apply;
6742 	struct dc_state *context = dc->current_state;
6743 	struct hubp *hubp;
6744 	struct pipe_ctx *pipe;
6745 	unsigned int i;
6746 
6747 	for (i = 0; i < dc->res_pool->pipe_count; i++) {
6748 		pipe = &context->res_ctx.pipe_ctx[i];
6749 
6750 		if (pipe->stream != NULL) {
6751 			dc->hwss.disable_pixel_data(dc, pipe, true);
6752 
6753 			// wait for double buffer
6754 			pipe->stream_res.tg->funcs->wait_for_state(pipe->stream_res.tg, CRTC_STATE_VACTIVE);
6755 			pipe->stream_res.tg->funcs->wait_for_state(pipe->stream_res.tg, CRTC_STATE_VBLANK);
6756 			pipe->stream_res.tg->funcs->wait_for_state(pipe->stream_res.tg, CRTC_STATE_VACTIVE);
6757 
6758 			hubp = pipe->plane_res.hubp;
6759 			hubp->funcs->set_blank_regs(hubp, true);
6760 		}
6761 	}
6762 	if (dc->clk_mgr->funcs->set_max_memclk)
6763 		dc->clk_mgr->funcs->set_max_memclk(dc->clk_mgr, memclk_mhz);
6764 	if (dc->clk_mgr->funcs->set_min_memclk)
6765 		dc->clk_mgr->funcs->set_min_memclk(dc->clk_mgr, memclk_mhz);
6766 
6767 	for (i = 0; i < dc->res_pool->pipe_count; i++) {
6768 		pipe = &context->res_ctx.pipe_ctx[i];
6769 
6770 		if (pipe->stream != NULL) {
6771 			dc->hwss.disable_pixel_data(dc, pipe, false);
6772 
6773 			hubp = pipe->plane_res.hubp;
6774 			hubp->funcs->set_blank_regs(hubp, false);
6775 		}
6776 	}
6777 }
6778 
6779 
6780 /**
6781  * dc_enable_dcmode_clk_limit() - lower clocks in dc (battery) mode
6782  * @dc: pointer to dc of the dm calling this
6783  * @enable: True = transition to DC mode, false = transition back to AC mode
6784  *
6785  * Some SoCs define additional clock limits when in DC mode, DM should
6786  * invoke this function when the platform undergoes a power source transition
6787  * so DC can apply/unapply the limit. This interface may be disruptive to
6788  * the onscreen content.
6789  *
6790  * Context: Triggered by OS through DM interface, or manually by escape calls.
6791  * Need to hold a dclock when doing so.
6792  *
6793  * Return: none (void function)
6794  *
6795  */
dc_enable_dcmode_clk_limit(struct dc * dc,bool enable)6796 void dc_enable_dcmode_clk_limit(struct dc *dc, bool enable)
6797 {
6798 	unsigned int softMax = 0, maxDPM = 0, funcMin = 0, i;
6799 	bool p_state_change_support;
6800 
6801 	if (!dc->config.dc_mode_clk_limit_support)
6802 		return;
6803 
6804 	softMax = dc->clk_mgr->bw_params->dc_mode_softmax_memclk;
6805 	for (i = 0; i < dc->clk_mgr->bw_params->clk_table.num_entries; i++) {
6806 		if (dc->clk_mgr->bw_params->clk_table.entries[i].memclk_mhz > maxDPM)
6807 			maxDPM = dc->clk_mgr->bw_params->clk_table.entries[i].memclk_mhz;
6808 	}
6809 	funcMin = (dc->clk_mgr->clks.dramclk_khz + 999) / 1000;
6810 	p_state_change_support = dc->clk_mgr->clks.p_state_change_support;
6811 
6812 	if (enable && !dc->clk_mgr->dc_mode_softmax_enabled) {
6813 		if (p_state_change_support) {
6814 			if (funcMin <= softMax && dc->clk_mgr->funcs->set_max_memclk)
6815 				dc->clk_mgr->funcs->set_max_memclk(dc->clk_mgr, softMax);
6816 			// else: No-Op
6817 		} else {
6818 			if (funcMin <= softMax)
6819 				blank_and_force_memclk(dc, true, softMax);
6820 			// else: No-Op
6821 		}
6822 	} else if (!enable && dc->clk_mgr->dc_mode_softmax_enabled) {
6823 		if (p_state_change_support) {
6824 			if (funcMin <= softMax && dc->clk_mgr->funcs->set_max_memclk)
6825 				dc->clk_mgr->funcs->set_max_memclk(dc->clk_mgr, maxDPM);
6826 			// else: No-Op
6827 		} else {
6828 			if (funcMin <= softMax)
6829 				blank_and_force_memclk(dc, true, maxDPM);
6830 			// else: No-Op
6831 		}
6832 	}
6833 	dc->clk_mgr->dc_mode_softmax_enabled = enable;
6834 }
dc_is_plane_eligible_for_idle_optimizations(struct dc * dc,unsigned int pitch,unsigned int height,enum surface_pixel_format format,struct dc_cursor_attributes * cursor_attr)6835 bool dc_is_plane_eligible_for_idle_optimizations(struct dc *dc,
6836 		unsigned int pitch,
6837 		unsigned int height,
6838 		enum surface_pixel_format format,
6839 		struct dc_cursor_attributes *cursor_attr)
6840 {
6841 	if (dc->hwss.does_plane_fit_in_mall && dc->hwss.does_plane_fit_in_mall(dc, pitch, height, format, cursor_attr))
6842 		return true;
6843 	return false;
6844 }
6845 
6846 /* cleanup on driver unload */
dc_hardware_release(struct dc * dc)6847 void dc_hardware_release(struct dc *dc)
6848 {
6849 	dc_mclk_switch_using_fw_based_vblank_stretch_shut_down(dc);
6850 
6851 	if (dc->hwss.hardware_release)
6852 		dc->hwss.hardware_release(dc);
6853 }
6854 
dc_mclk_switch_using_fw_based_vblank_stretch_shut_down(struct dc * dc)6855 void dc_mclk_switch_using_fw_based_vblank_stretch_shut_down(struct dc *dc)
6856 {
6857 	if (dc->current_state)
6858 		dc->current_state->bw_ctx.bw.dcn.clk.fw_based_mclk_switching_shut_down = true;
6859 }
6860 
6861 /**
6862  * dc_is_dmub_outbox_supported - Check if DMUB firmware support outbox notification
6863  *
6864  * @dc: [in] dc structure
6865  *
6866  * Checks whether DMUB FW supports outbox notifications, if supported DM
6867  * should register outbox interrupt prior to actually enabling interrupts
6868  * via dc_enable_dmub_outbox
6869  *
6870  * Return:
6871  * True if DMUB FW supports outbox notifications, False otherwise
6872  */
dc_is_dmub_outbox_supported(struct dc * dc)6873 bool dc_is_dmub_outbox_supported(struct dc *dc)
6874 {
6875 	if (!dc->caps.dmcub_support)
6876 		return false;
6877 
6878 	switch (dc->ctx->asic_id.chip_family) {
6879 
6880 	case FAMILY_YELLOW_CARP:
6881 		/* DCN31 B0 USB4 DPIA needs dmub notifications for interrupts */
6882 		if (dc->ctx->asic_id.hw_internal_rev == YELLOW_CARP_B0 &&
6883 		    !dc->debug.dpia_debug.bits.disable_dpia)
6884 			return true;
6885 	break;
6886 
6887 	case AMDGPU_FAMILY_GC_11_0_1:
6888 	case AMDGPU_FAMILY_GC_11_5_0:
6889 	case AMDGPU_FAMILY_GC_11_5_4:
6890 		if (!dc->debug.dpia_debug.bits.disable_dpia)
6891 			return true;
6892 	break;
6893 
6894 	default:
6895 		break;
6896 	}
6897 
6898 	/* dmub aux needs dmub notifications to be enabled */
6899 	return dc->debug.enable_dmub_aux_for_legacy_ddc;
6900 
6901 }
6902 
6903 /**
6904  * dc_enable_dmub_notifications - Check if dmub fw supports outbox
6905  *
6906  * @dc: [in] dc structure
6907  *
6908  * Calls dc_is_dmub_outbox_supported to check if dmub fw supports outbox
6909  * notifications. All DMs shall switch to dc_is_dmub_outbox_supported.  This
6910  * API shall be removed after switching.
6911  *
6912  * Return:
6913  * True if DMUB FW supports outbox notifications, False otherwise
6914  */
dc_enable_dmub_notifications(struct dc * dc)6915 bool dc_enable_dmub_notifications(struct dc *dc)
6916 {
6917 	return dc_is_dmub_outbox_supported(dc);
6918 }
6919 
6920 /**
6921  * dc_enable_dmub_outbox - Enables DMUB unsolicited notification
6922  *
6923  * @dc: [in] dc structure
6924  *
6925  * Enables DMUB unsolicited notifications to x86 via outbox.
6926  */
dc_enable_dmub_outbox(struct dc * dc)6927 void dc_enable_dmub_outbox(struct dc *dc)
6928 {
6929 	struct dc_context *dc_ctx = dc->ctx;
6930 
6931 	dmub_enable_outbox_notification(dc_ctx->dmub_srv);
6932 	DC_LOG_DC("%s: dmub outbox notifications enabled\n", __func__);
6933 }
6934 
6935 /**
6936  * dc_process_dmub_aux_transfer_async - Submits aux command to dmub via inbox message
6937  *                                      Sets port index appropriately for legacy DDC
6938  * @dc: dc structure
6939  * @link_index: link index
6940  * @payload: aux payload
6941  *
6942  * Returns: True if successful, False if failure
6943  */
dc_process_dmub_aux_transfer_async(struct dc * dc,uint32_t link_index,struct aux_payload * payload)6944 bool dc_process_dmub_aux_transfer_async(struct dc *dc,
6945 				uint32_t link_index,
6946 				struct aux_payload *payload)
6947 {
6948 	uint8_t action;
6949 	union dmub_rb_cmd cmd = {0};
6950 
6951 	if (link_index >= dc->link_count || !dc->links[link_index])
6952 		return false;
6953 
6954 	if (payload->length > sizeof(cmd.dp_aux_access.aux_control.dpaux.data))
6955 		return false;
6956 
6957 	cmd.dp_aux_access.header.type = DMUB_CMD__DP_AUX_ACCESS;
6958 	cmd.dp_aux_access.header.payload_bytes = 0;
6959 	/* For dpia, ddc_pin is set to NULL */
6960 	if (!dc->links[link_index]->ddc->ddc_pin)
6961 		cmd.dp_aux_access.aux_control.type = AUX_CHANNEL_DPIA;
6962 	else
6963 		cmd.dp_aux_access.aux_control.type = AUX_CHANNEL_LEGACY_DDC;
6964 
6965 	cmd.dp_aux_access.aux_control.instance = (uint8_t)dc->links[link_index]->ddc_hw_inst;
6966 	cmd.dp_aux_access.aux_control.sw_crc_enabled = 0;
6967 	cmd.dp_aux_access.aux_control.timeout = 0;
6968 	cmd.dp_aux_access.aux_control.dpaux.address = payload->address;
6969 	cmd.dp_aux_access.aux_control.dpaux.is_i2c_over_aux = payload->i2c_over_aux;
6970 	cmd.dp_aux_access.aux_control.dpaux.length = (uint8_t)payload->length;
6971 
6972 	/* set aux action */
6973 	if (payload->i2c_over_aux) {
6974 		if (payload->write) {
6975 			if (payload->mot)
6976 				action = DP_AUX_REQ_ACTION_I2C_WRITE_MOT;
6977 			else
6978 				action = DP_AUX_REQ_ACTION_I2C_WRITE;
6979 		} else {
6980 			if (payload->mot)
6981 				action = DP_AUX_REQ_ACTION_I2C_READ_MOT;
6982 			else
6983 				action = DP_AUX_REQ_ACTION_I2C_READ;
6984 			}
6985 	} else {
6986 		if (payload->write)
6987 			action = DP_AUX_REQ_ACTION_DPCD_WRITE;
6988 		else
6989 			action = DP_AUX_REQ_ACTION_DPCD_READ;
6990 	}
6991 
6992 	cmd.dp_aux_access.aux_control.dpaux.action = action;
6993 
6994 	if (payload->length && payload->write) {
6995 		memcpy(cmd.dp_aux_access.aux_control.dpaux.data,
6996 			payload->data,
6997 			payload->length
6998 			);
6999 	}
7000 
7001 	dc_wake_and_execute_dmub_cmd(dc->ctx, &cmd, DM_DMUB_WAIT_TYPE_WAIT);
7002 
7003 	return true;
7004 }
7005 
dc_smart_power_oled_enable(const struct dc_link * link,bool enable,uint16_t peak_nits,uint8_t debug_control,uint16_t fixed_CLL,uint32_t triggerline)7006 bool dc_smart_power_oled_enable(const struct dc_link *link, bool enable, uint16_t peak_nits,
7007 			uint8_t debug_control, uint16_t fixed_CLL, uint32_t triggerline)
7008 {
7009 	bool status = false;
7010 	struct dc *dc = link->ctx->dc;
7011 	union dmub_rb_cmd cmd;
7012 	uint8_t otg_inst = 0;
7013 	unsigned int panel_inst = 0;
7014 	struct pipe_ctx *pipe_ctx = NULL;
7015 	struct resource_context *res_ctx = &link->ctx->dc->current_state->res_ctx;
7016 	int i = 0;
7017 
7018 	// get panel_inst
7019 	if (!dc_get_edp_link_panel_inst(dc, link, &panel_inst))
7020 		return status;
7021 
7022 	// get otg_inst
7023 	for (i = 0; i < MAX_PIPES; i++) {
7024 		if (res_ctx &&
7025 			res_ctx->pipe_ctx[i].stream &&
7026 			res_ctx->pipe_ctx[i].stream->link &&
7027 			res_ctx->pipe_ctx[i].stream->link == link &&
7028 			res_ctx->pipe_ctx[i].stream->link->connector_signal == SIGNAL_TYPE_EDP) {
7029 			pipe_ctx = &res_ctx->pipe_ctx[i];
7030 			//TODO: refactor for multi edp support
7031 			break;
7032 		}
7033 	}
7034 
7035 	if (pipe_ctx)
7036 		otg_inst = (uint8_t)pipe_ctx->stream_res.tg->inst;
7037 
7038 	// before enable smart power OLED, we need to call set pipe for DMUB to set ABM config
7039 	if (enable) {
7040 		if (dc->hwss.set_pipe && pipe_ctx)
7041 			dc->hwss.set_pipe(pipe_ctx);
7042 	}
7043 
7044 	// fill in cmd
7045 	memset(&cmd, 0, sizeof(cmd));
7046 
7047 	cmd.smart_power_oled_enable.header.type = DMUB_CMD__SMART_POWER_OLED;
7048 	cmd.smart_power_oled_enable.header.sub_type = DMUB_CMD__SMART_POWER_OLED_ENABLE;
7049 	cmd.smart_power_oled_enable.header.payload_bytes =
7050 		sizeof(struct dmub_rb_cmd_smart_power_oled_enable_data) - sizeof(struct dmub_cmd_header);
7051 	cmd.smart_power_oled_enable.header.ret_status = 1;
7052 	cmd.smart_power_oled_enable.data.enable = enable;
7053 	cmd.smart_power_oled_enable.data.panel_inst = (uint8_t)panel_inst;
7054 	cmd.smart_power_oled_enable.data.peak_nits = peak_nits;
7055 	cmd.smart_power_oled_enable.data.otg_inst = otg_inst;
7056 	cmd.smart_power_oled_enable.data.digfe_inst = (uint8_t)link->link_enc->preferred_engine;
7057 	cmd.smart_power_oled_enable.data.digbe_inst = (uint8_t)link->link_enc->transmitter;
7058 
7059 	cmd.smart_power_oled_enable.data.debugcontrol = debug_control;
7060 	cmd.smart_power_oled_enable.data.triggerline = triggerline;
7061 	cmd.smart_power_oled_enable.data.fixed_max_cll = fixed_CLL;
7062 
7063 	// send cmd
7064 	status = dc_wake_and_execute_dmub_cmd(dc->ctx, &cmd, DM_DMUB_WAIT_TYPE_WAIT);
7065 
7066 	// Update firmware_controlled_hdr_info_packet state on successful command execution
7067 	if (status && pipe_ctx)
7068 		pipe_ctx->stream->firmware_controlled_hdr_info_packet = enable;
7069 
7070 	return status;
7071 }
7072 
dc_smart_power_oled_get_max_cll(const struct dc_link * link,unsigned int * pCurrent_MaxCLL)7073 bool dc_smart_power_oled_get_max_cll(const struct dc_link *link, unsigned int *pCurrent_MaxCLL)
7074 {
7075 	struct dc *dc = link->ctx->dc;
7076 	union dmub_rb_cmd cmd;
7077 	bool status = false;
7078 	unsigned int panel_inst = 0;
7079 
7080 	// get panel_inst
7081 	if (!dc_get_edp_link_panel_inst(dc, link, &panel_inst))
7082 		return status;
7083 
7084 	// fill in cmd
7085 	memset(&cmd, 0, sizeof(cmd));
7086 
7087 	cmd.smart_power_oled_getmaxcll.header.type = DMUB_CMD__SMART_POWER_OLED;
7088 	cmd.smart_power_oled_getmaxcll.header.sub_type = DMUB_CMD__SMART_POWER_OLED_GETMAXCLL;
7089 	cmd.smart_power_oled_getmaxcll.header.payload_bytes = sizeof(cmd.smart_power_oled_getmaxcll.data);
7090 	cmd.smart_power_oled_getmaxcll.header.ret_status = 1;
7091 
7092 	cmd.smart_power_oled_getmaxcll.data.input.panel_inst = (uint8_t)panel_inst;
7093 
7094 	// send cmd and wait for reply
7095 	status = dc_wake_and_execute_dmub_cmd(dc->ctx, &cmd, DM_DMUB_WAIT_TYPE_WAIT_WITH_REPLY);
7096 
7097 	if (status)
7098 		*pCurrent_MaxCLL = cmd.smart_power_oled_getmaxcll.data.output.current_max_cll;
7099 	else
7100 		*pCurrent_MaxCLL = 0;
7101 
7102 	return status;
7103 }
7104 
get_link_index_from_dpia_port_index(const struct dc * dc,uint8_t dpia_port_index)7105 uint8_t get_link_index_from_dpia_port_index(const struct dc *dc,
7106 					    uint8_t dpia_port_index)
7107 {
7108 	uint8_t index, link_index = 0xFF;
7109 
7110 	for (index = 0; index < dc->link_count; index++) {
7111 		/* ddc_hw_inst has dpia port index for dpia links
7112 		 * and ddc instance for legacy links
7113 		 */
7114 		if (!dc->links[index]->ddc->ddc_pin) {
7115 			if (dc->links[index]->ddc_hw_inst == dpia_port_index) {
7116 				link_index = index;
7117 				break;
7118 			}
7119 		}
7120 	}
7121 	ASSERT(link_index != 0xFF);
7122 	return link_index;
7123 }
7124 
7125 /**
7126  * dc_process_dmub_set_config_async - Submits set_config command
7127  *
7128  * @dc: [in] dc structure
7129  * @link_index: [in] link_index: link index
7130  * @payload: [in] aux payload
7131  * @notify: [out] set_config immediate reply
7132  *
7133  * Submits set_config command to dmub via inbox message.
7134  *
7135  * Return:
7136  * True if successful, False if failure
7137  */
dc_process_dmub_set_config_async(struct dc * dc,uint32_t link_index,struct set_config_cmd_payload * payload,struct dmub_notification * notify)7138 bool dc_process_dmub_set_config_async(struct dc *dc,
7139 				uint32_t link_index,
7140 				struct set_config_cmd_payload *payload,
7141 				struct dmub_notification *notify)
7142 {
7143 	union dmub_rb_cmd cmd = {0};
7144 	bool is_cmd_complete = true;
7145 
7146 	/* prepare SET_CONFIG command */
7147 	cmd.set_config_access.header.type = DMUB_CMD__DPIA;
7148 	cmd.set_config_access.header.sub_type = DMUB_CMD__DPIA_SET_CONFIG_ACCESS;
7149 
7150 	cmd.set_config_access.set_config_control.instance = (uint8_t)dc->links[link_index]->ddc_hw_inst;
7151 	cmd.set_config_access.set_config_control.cmd_pkt.msg_type = payload->msg_type;
7152 	cmd.set_config_access.set_config_control.cmd_pkt.msg_data = payload->msg_data;
7153 
7154 	if (!dc_wake_and_execute_dmub_cmd(dc->ctx, &cmd, DM_DMUB_WAIT_TYPE_WAIT_WITH_REPLY)) {
7155 		/* command is not processed by dmub */
7156 		notify->sc_status = SET_CONFIG_UNKNOWN_ERROR;
7157 		return is_cmd_complete;
7158 	}
7159 
7160 	/* command processed by dmub, if ret_status is 1, it is completed instantly */
7161 	if (cmd.set_config_access.header.ret_status == 1)
7162 		notify->sc_status = cmd.set_config_access.set_config_control.immed_status;
7163 	else
7164 		/* cmd pending, will receive notification via outbox */
7165 		is_cmd_complete = false;
7166 
7167 	return is_cmd_complete;
7168 }
7169 
7170 /**
7171  * dc_process_dmub_set_mst_slots - Submits MST solt allocation
7172  *
7173  * @dc: [in] dc structure
7174  * @link_index: [in] link index
7175  * @mst_alloc_slots: [in] mst slots to be allotted
7176  * @mst_slots_in_use: [out] mst slots in use returned in failure case
7177  *
7178  * Submits mst slot allocation command to dmub via inbox message
7179  *
7180  * Return:
7181  * DC_OK if successful, DC_ERROR if failure
7182  */
dc_process_dmub_set_mst_slots(const struct dc * dc,uint32_t link_index,uint8_t mst_alloc_slots,uint8_t * mst_slots_in_use)7183 enum dc_status dc_process_dmub_set_mst_slots(const struct dc *dc,
7184 				uint32_t link_index,
7185 				uint8_t mst_alloc_slots,
7186 				uint8_t *mst_slots_in_use)
7187 {
7188 	union dmub_rb_cmd cmd = {0};
7189 
7190 	/* prepare MST_ALLOC_SLOTS command */
7191 	cmd.set_mst_alloc_slots.header.type = DMUB_CMD__DPIA;
7192 	cmd.set_mst_alloc_slots.header.sub_type = DMUB_CMD__DPIA_MST_ALLOC_SLOTS;
7193 
7194 	cmd.set_mst_alloc_slots.mst_slots_control.instance = (uint8_t)dc->links[link_index]->ddc_hw_inst;
7195 	cmd.set_mst_alloc_slots.mst_slots_control.mst_alloc_slots = mst_alloc_slots;
7196 
7197 	if (!dc_wake_and_execute_dmub_cmd(dc->ctx, &cmd, DM_DMUB_WAIT_TYPE_WAIT_WITH_REPLY))
7198 		/* command is not processed by dmub */
7199 		return DC_ERROR_UNEXPECTED;
7200 
7201 	/* command processed by dmub, if ret_status is 1 */
7202 	if (cmd.set_config_access.header.ret_status != 1)
7203 		/* command processing error */
7204 		return DC_ERROR_UNEXPECTED;
7205 
7206 	/* command processed and we have a status of 2, mst not enabled in dpia */
7207 	if (cmd.set_mst_alloc_slots.mst_slots_control.immed_status == 2)
7208 		return DC_FAIL_UNSUPPORTED_1;
7209 
7210 	/* previously configured mst alloc and used slots did not match */
7211 	if (cmd.set_mst_alloc_slots.mst_slots_control.immed_status == 3) {
7212 		*mst_slots_in_use = cmd.set_mst_alloc_slots.mst_slots_control.mst_slots_in_use;
7213 		return DC_NOT_SUPPORTED;
7214 	}
7215 
7216 	return DC_OK;
7217 }
7218 
7219 /**
7220  * dc_process_dmub_dpia_set_tps_notification - Submits tps notification
7221  *
7222  * @dc: [in] dc structure
7223  * @link_index: [in] link index
7224  * @tps: [in] request tps
7225  *
7226  * Submits set_tps_notification command to dmub via inbox message
7227  */
dc_process_dmub_dpia_set_tps_notification(const struct dc * dc,uint32_t link_index,uint8_t tps)7228 void dc_process_dmub_dpia_set_tps_notification(const struct dc *dc, uint32_t link_index, uint8_t tps)
7229 {
7230 	union dmub_rb_cmd cmd = {0};
7231 
7232 	cmd.set_tps_notification.header.type = DMUB_CMD__DPIA;
7233 	cmd.set_tps_notification.header.sub_type = DMUB_CMD__DPIA_SET_TPS_NOTIFICATION;
7234 	cmd.set_tps_notification.tps_notification.instance = (uint8_t)dc->links[link_index]->ddc_hw_inst;
7235 	cmd.set_tps_notification.tps_notification.tps = tps;
7236 
7237 	dc_wake_and_execute_dmub_cmd(dc->ctx, &cmd, DM_DMUB_WAIT_TYPE_WAIT);
7238 }
7239 
7240 /**
7241  * dc_process_dmub_dpia_hpd_int_enable - Submits DPIA DPD interruption
7242  *
7243  * @dc: [in] dc structure
7244  * @hpd_int_enable: [in] 1 for hpd int enable, 0 to disable
7245  *
7246  * Submits dpia hpd int enable command to dmub via inbox message
7247  */
dc_process_dmub_dpia_hpd_int_enable(const struct dc * dc,uint32_t hpd_int_enable)7248 void dc_process_dmub_dpia_hpd_int_enable(const struct dc *dc,
7249 				uint32_t hpd_int_enable)
7250 {
7251 	union dmub_rb_cmd cmd = {0};
7252 
7253 	cmd.dpia_hpd_int_enable.header.type = DMUB_CMD__DPIA_HPD_INT_ENABLE;
7254 	cmd.dpia_hpd_int_enable.enable = hpd_int_enable;
7255 
7256 	dc_wake_and_execute_dmub_cmd(dc->ctx, &cmd, DM_DMUB_WAIT_TYPE_WAIT);
7257 
7258 	DC_LOG_DEBUG("%s: hpd_int_enable(%d)\n", __func__, hpd_int_enable);
7259 }
7260 
7261 /**
7262  * dc_print_dmub_diagnostic_data - Print DMUB diagnostic data for debugging
7263  *
7264  * @dc: [in] dc structure
7265  *
7266  *
7267  */
dc_print_dmub_diagnostic_data(const struct dc * dc)7268 void dc_print_dmub_diagnostic_data(const struct dc *dc)
7269 {
7270 	dc_dmub_srv_log_diagnostic_data(dc->ctx->dmub_srv);
7271 }
7272 
7273 /**
7274  * dc_disable_accelerated_mode - disable accelerated mode
7275  * @dc: dc structure
7276  */
dc_disable_accelerated_mode(struct dc * dc)7277 void dc_disable_accelerated_mode(struct dc *dc)
7278 {
7279 	bios_set_scratch_acc_mode_change(dc->ctx->dc_bios, 0);
7280 }
7281 
7282 
7283 /**
7284  *  dc_notify_vsync_int_state - notifies vsync enable/disable state
7285  *  @dc: dc structure
7286  *  @stream: stream where vsync int state changed
7287  *  @enable: whether vsync is enabled or disabled
7288  *
7289  *  Called when vsync is enabled/disabled Will notify DMUB to start/stop ABM
7290  *  interrupts after steady state is reached.
7291  */
dc_notify_vsync_int_state(struct dc * dc,struct dc_stream_state * stream,bool enable)7292 void dc_notify_vsync_int_state(struct dc *dc, struct dc_stream_state *stream, bool enable)
7293 {
7294 	unsigned int i, edp_num;
7295 	struct pipe_ctx *pipe = NULL;
7296 	struct dc_link *link = stream->sink->link;
7297 	struct dc_link *edp_links[MAX_NUM_EDP];
7298 
7299 
7300 	if (link->psr_settings.psr_feature_enabled)
7301 		return;
7302 
7303 	if (link->replay_settings.replay_feature_enabled)
7304 		return;
7305 
7306 	/*find primary pipe associated with stream*/
7307 	for (i = 0; i < MAX_PIPES; i++) {
7308 		pipe = &dc->current_state->res_ctx.pipe_ctx[i];
7309 
7310 		if (pipe->stream == stream && pipe->stream_res.tg)
7311 			break;
7312 	}
7313 
7314 	if (i == MAX_PIPES) {
7315 		ASSERT(0);
7316 		return;
7317 	}
7318 
7319 	dc_get_edp_links(dc, edp_links, &edp_num);
7320 
7321 	/* Determine panel inst */
7322 	for (i = 0; i < edp_num; i++) {
7323 		if (edp_links[i] == link)
7324 			break;
7325 	}
7326 
7327 	if (i == edp_num) {
7328 		return;
7329 	}
7330 
7331 	if (pipe->stream_res.abm && pipe->stream_res.abm->funcs->set_abm_pause)
7332 		pipe->stream_res.abm->funcs->set_abm_pause(pipe->stream_res.abm, !enable, i, pipe->stream_res.tg->inst);
7333 }
7334 
7335 /*****************************************************************************
7336  *  dc_abm_save_restore() - Interface to DC for save+pause and restore+un-pause
7337  *                          ABM
7338  *  @dc: dc structure
7339  *	@stream: stream where vsync int state changed
7340  *  @pData: abm hw states
7341  *
7342  ****************************************************************************/
dc_abm_save_restore(struct dc * dc,struct dc_stream_state * stream,struct abm_save_restore * pData)7343 bool dc_abm_save_restore(
7344 		struct dc *dc,
7345 		struct dc_stream_state *stream,
7346 		struct abm_save_restore *pData)
7347 {
7348 	unsigned int i, edp_num;
7349 	struct pipe_ctx *pipe = NULL;
7350 	struct dc_link *link = stream->sink->link;
7351 	struct dc_link *edp_links[MAX_NUM_EDP];
7352 
7353 	if (link->replay_settings.replay_feature_enabled)
7354 		return false;
7355 
7356 	/*find primary pipe associated with stream*/
7357 	for (i = 0; i < MAX_PIPES; i++) {
7358 		pipe = &dc->current_state->res_ctx.pipe_ctx[i];
7359 
7360 		if (pipe->stream == stream && pipe->stream_res.tg)
7361 			break;
7362 	}
7363 
7364 	if (i == MAX_PIPES) {
7365 		ASSERT(0);
7366 		return false;
7367 	}
7368 
7369 	dc_get_edp_links(dc, edp_links, &edp_num);
7370 
7371 	/* Determine panel inst */
7372 	for (i = 0; i < edp_num; i++)
7373 		if (edp_links[i] == link)
7374 			break;
7375 
7376 	if (i == edp_num)
7377 		return false;
7378 
7379 	if (pipe->stream_res.abm &&
7380 		pipe->stream_res.abm->funcs->save_restore)
7381 		return pipe->stream_res.abm->funcs->save_restore(
7382 				pipe->stream_res.abm,
7383 				i,
7384 				pData);
7385 	return false;
7386 }
7387 
dc_query_current_properties(struct dc * dc,struct dc_current_properties * properties)7388 void dc_query_current_properties(struct dc *dc, struct dc_current_properties *properties)
7389 {
7390 	unsigned int i;
7391 	unsigned int max_cursor_size = dc->caps.max_cursor_size;
7392 	unsigned int stream_cursor_size;
7393 
7394 	if (dc->debug.allow_sw_cursor_fallback && dc->res_pool->funcs->get_max_hw_cursor_size) {
7395 		for (i = 0; i < dc->current_state->stream_count; i++) {
7396 			stream_cursor_size = dc->res_pool->funcs->get_max_hw_cursor_size(dc,
7397 					dc->current_state,
7398 					dc->current_state->streams[i]);
7399 
7400 			if (stream_cursor_size < max_cursor_size) {
7401 				max_cursor_size = stream_cursor_size;
7402 			}
7403 		}
7404 	}
7405 
7406 	properties->cursor_size_limit = max_cursor_size;
7407 }
7408 
7409 /**
7410  * dc_set_edp_power() - DM controls eDP power to be ON/OFF
7411  *
7412  * Called when DM wants to power on/off eDP.
7413  *     Only work on links with flag skip_implict_edp_power_control is set.
7414  *
7415  * @dc: Current DC state
7416  * @edp_link: a link with eDP connector signal type
7417  * @powerOn: power on/off eDP
7418  *
7419  * Return: void
7420  */
dc_set_edp_power(const struct dc * dc,struct dc_link * edp_link,bool powerOn)7421 void dc_set_edp_power(const struct dc *dc, struct dc_link *edp_link,
7422 				 bool powerOn)
7423 {
7424 	(void)dc;
7425 	if (edp_link->connector_signal != SIGNAL_TYPE_EDP)
7426 		return;
7427 
7428 	if (edp_link->skip_implict_edp_power_control == false)
7429 		return;
7430 
7431 	edp_link->dc->link_srv->edp_set_panel_power(edp_link, powerOn);
7432 }
7433 
7434 /**
7435  * dc_get_power_profile_for_dc_state() - extracts power profile from dc state
7436  *
7437  * Called when DM wants to make power policy decisions based on dc_state
7438  *
7439  * @context: Pointer to the dc_state from which the power profile is extracted.
7440  *
7441  * Return: The power profile structure containing the power level information.
7442  */
dc_get_power_profile_for_dc_state(const struct dc_state * context)7443 struct dc_power_profile dc_get_power_profile_for_dc_state(const struct dc_state *context)
7444 {
7445 	struct dc_power_profile profile = { 0 };
7446 
7447 	profile.power_level = !context->bw_ctx.bw.dcn.clk.p_state_change_support;
7448 	if (!context->clk_mgr || !context->clk_mgr->ctx || !context->clk_mgr->ctx->dc)
7449 		return profile;
7450 	struct dc *dc = context->clk_mgr->ctx->dc;
7451 
7452 	if (dc->res_pool->funcs->get_power_profile)
7453 		profile.power_level = dc->res_pool->funcs->get_power_profile(context);
7454 	return profile;
7455 }
7456 
7457 /**
7458  * dc_get_det_buffer_size_from_state() - extracts detile buffer size from dc state
7459  *
7460  * This function is called to log the detile buffer size from the dc_state.
7461  *
7462  * @context: a pointer to the dc_state from which the detile buffer size is extracted.
7463  *
7464  * Return: the size of the detile buffer, or 0 if not available.
7465  */
dc_get_det_buffer_size_from_state(const struct dc_state * context)7466 unsigned int dc_get_det_buffer_size_from_state(const struct dc_state *context)
7467 {
7468 	struct dc *dc = context->clk_mgr->ctx->dc;
7469 
7470 	if (dc->res_pool->funcs->get_det_buffer_size)
7471 		return dc->res_pool->funcs->get_det_buffer_size(context);
7472 	else
7473 		return 0;
7474 }
7475 
7476 /**
7477  * dc_get_host_router_index: Get index of host router from a dpia link
7478  *
7479  * This function return a host router index of the target link. If the target link is dpia link.
7480  *
7481  * @link: Pointer to the target link (input)
7482  * @host_router_index: Pointer to store the host router index of the target link (output).
7483  *
7484  * Return: true if the host router index is found and valid.
7485  *
7486  */
dc_get_host_router_index(const struct dc_link * link,unsigned int * host_router_index)7487 bool dc_get_host_router_index(const struct dc_link *link, unsigned int *host_router_index)
7488 {
7489 	struct dc *dc;
7490 
7491 	if (!link || !host_router_index || link->ep_type != DISPLAY_ENDPOINT_USB4_DPIA)
7492 		return false;
7493 
7494 	dc = link->ctx->dc;
7495 
7496 	if (link->link_index < dc->lowest_dpia_link_index)
7497 		return false;
7498 
7499 	*host_router_index = (link->link_index - dc->lowest_dpia_link_index) / dc->caps.num_of_dpias_per_host_router;
7500 	if (*host_router_index < dc->caps.num_of_host_routers)
7501 		return true;
7502 	else
7503 		return false;
7504 }
7505 
dc_is_cursor_limit_pending(struct dc * dc)7506 bool dc_is_cursor_limit_pending(struct dc *dc)
7507 {
7508 	uint32_t i;
7509 
7510 	for (i = 0; i < dc->current_state->stream_count; i++) {
7511 		if (dc_stream_is_cursor_limit_pending(dc, dc->current_state->streams[i]))
7512 			return true;
7513 	}
7514 
7515 	return false;
7516 }
7517 
dc_can_clear_cursor_limit(const struct dc * dc)7518 bool dc_can_clear_cursor_limit(const struct dc *dc)
7519 {
7520 	uint32_t i;
7521 
7522 	for (i = 0; i < dc->current_state->stream_count; i++) {
7523 		if (dc_state_can_clear_stream_cursor_subvp_limit(dc->current_state->streams[i], dc->current_state))
7524 			return true;
7525 	}
7526 
7527 	return false;
7528 }
7529 
dc_get_underflow_debug_data_for_otg(struct dc * dc,unsigned int primary_otg_inst,struct dc_underflow_debug_data * out_data)7530 void dc_get_underflow_debug_data_for_otg(struct dc *dc, unsigned int primary_otg_inst,
7531 				struct dc_underflow_debug_data *out_data)
7532 {
7533 	struct timing_generator *tg = NULL;
7534 
7535 	for (int i = 0; i < MAX_PIPES; i++) {
7536 		if (dc->res_pool->timing_generators[i] &&
7537 			dc->res_pool->timing_generators[i]->inst == primary_otg_inst) {
7538 				tg = dc->res_pool->timing_generators[i];
7539 				break;
7540 		}
7541 	}
7542 
7543 	dc_exit_ips_for_hw_access(dc);
7544 	if (dc->hwss.get_underflow_debug_data)
7545 		dc->hwss.get_underflow_debug_data(dc, tg, out_data);
7546 }
7547 
dc_get_power_feature_status(struct dc * dc,unsigned int primary_otg_inst,struct power_features * out_data)7548 void dc_get_power_feature_status(struct dc *dc, unsigned int primary_otg_inst,
7549 				struct power_features *out_data)
7550 {
7551 	(void)primary_otg_inst;
7552 	out_data->uclk_p_state = dc->current_state->clk_mgr->clks.p_state_change_support;
7553 	out_data->fams = dc->current_state->bw_ctx.bw.dcn.clk.fw_based_mclk_switching;
7554 }
7555 
dc_capture_register_software_state(struct dc * dc,struct dc_register_software_state * state)7556 bool dc_capture_register_software_state(struct dc *dc, struct dc_register_software_state *state)
7557 {
7558 	struct dc_state *context;
7559 	struct resource_context *res_ctx;
7560 	unsigned int i;
7561 	const unsigned int max_pipes = MAX_PIPES;
7562 
7563 	if (!dc || !dc->current_state || !state) {
7564 		if (state)
7565 			state->state_valid = false;
7566 		return false;
7567 	}
7568 
7569 	/* Initialize the state structure */
7570 	memset(state, 0, sizeof(struct dc_register_software_state));
7571 
7572 	context = dc->current_state;
7573 	res_ctx = &context->res_ctx;
7574 
7575 	/* Count active pipes and streams */
7576 	state->active_pipe_count = 0;
7577 	state->active_stream_count = context->stream_count;
7578 
7579 	for (i = 0; i < dc->res_pool->pipe_count; i++) {
7580 		if (res_ctx->pipe_ctx[i].stream)
7581 			state->active_pipe_count++;
7582 	}
7583 
7584 	/* Capture HUBP programming state for each pipe */
7585 	for (i = 0; i < max_pipes && i < dc->res_pool->pipe_count; i++) {
7586 		struct pipe_ctx *pipe_ctx = &res_ctx->pipe_ctx[i];
7587 
7588 		state->hubp[i].valid_stream = false;
7589 		if (!pipe_ctx->stream)
7590 			continue;
7591 
7592 		state->hubp[i].valid_stream = true;
7593 
7594 		/* HUBP register programming variables */
7595 		if (pipe_ctx->stream_res.tg)
7596 			state->hubp[i].vtg_sel = pipe_ctx->stream_res.tg->inst;
7597 
7598 		state->hubp[i].hubp_clock_enable = (pipe_ctx->plane_res.hubp != NULL) ? 1 : 0;
7599 
7600 		state->hubp[i].valid_plane_state = false;
7601 		if (pipe_ctx->plane_state) {
7602 			state->hubp[i].valid_plane_state = true;
7603 			state->hubp[i].surface_pixel_format = pipe_ctx->plane_state->format;
7604 			state->hubp[i].rotation_angle = pipe_ctx->plane_state->rotation;
7605 			state->hubp[i].h_mirror_en = pipe_ctx->plane_state->horizontal_mirror ? 1 : 0;
7606 
7607 			/* Surface size */
7608 			if (pipe_ctx->plane_state->plane_size.surface_size.width > 0) {
7609 				state->hubp[i].surface_size_width = pipe_ctx->plane_state->plane_size.surface_size.width;
7610 				state->hubp[i].surface_size_height = pipe_ctx->plane_state->plane_size.surface_size.height;
7611 			}
7612 
7613 			/* Viewport dimensions from scaler data */
7614 			if (pipe_ctx->plane_state->src_rect.width > 0) {
7615 				state->hubp[i].pri_viewport_width = pipe_ctx->plane_state->src_rect.width;
7616 				state->hubp[i].pri_viewport_height = pipe_ctx->plane_state->src_rect.height;
7617 				state->hubp[i].pri_viewport_x_start = pipe_ctx->plane_state->src_rect.x;
7618 				state->hubp[i].pri_viewport_y_start = pipe_ctx->plane_state->src_rect.y;
7619 			}
7620 
7621 			/* DCC settings */
7622 			state->hubp[i].surface_dcc_en = (pipe_ctx->plane_state->dcc.enable) ? 1 : 0;
7623 			state->hubp[i].surface_dcc_ind_64b_blk = pipe_ctx->plane_state->dcc.independent_64b_blks;
7624 			state->hubp[i].surface_dcc_ind_128b_blk = pipe_ctx->plane_state->dcc.dcc_ind_blk;
7625 
7626 			/* Surface pitch */
7627 			state->hubp[i].surface_pitch = pipe_ctx->plane_state->plane_size.surface_pitch;
7628 			state->hubp[i].meta_pitch = pipe_ctx->plane_state->dcc.meta_pitch;
7629 			state->hubp[i].chroma_pitch = pipe_ctx->plane_state->plane_size.chroma_pitch;
7630 			state->hubp[i].meta_pitch_c = pipe_ctx->plane_state->dcc.meta_pitch_c;
7631 
7632 			/* Surface addresses - primary */
7633 			state->hubp[i].primary_surface_address_low = pipe_ctx->plane_state->address.grph.addr.low_part;
7634 			state->hubp[i].primary_surface_address_high = pipe_ctx->plane_state->address.grph.addr.high_part;
7635 			state->hubp[i].primary_meta_surface_address_low = pipe_ctx->plane_state->address.grph.meta_addr.low_part;
7636 			state->hubp[i].primary_meta_surface_address_high = pipe_ctx->plane_state->address.grph.meta_addr.high_part;
7637 
7638 			/* TMZ settings */
7639 			state->hubp[i].primary_surface_tmz = pipe_ctx->plane_state->address.tmz_surface;
7640 			state->hubp[i].primary_meta_surface_tmz = pipe_ctx->plane_state->address.tmz_surface;
7641 
7642 			/* Tiling configuration */
7643 			state->hubp[i].min_dc_gfx_version9 = false;
7644 			if (pipe_ctx->plane_state->tiling_info.gfxversion >= DcGfxVersion9) {
7645 				state->hubp[i].min_dc_gfx_version9 = true;
7646 				state->hubp[i].sw_mode = pipe_ctx->plane_state->tiling_info.gfx9.swizzle;
7647 				state->hubp[i].num_pipes = pipe_ctx->plane_state->tiling_info.gfx9.num_pipes;
7648 				state->hubp[i].num_banks = pipe_ctx->plane_state->tiling_info.gfx9.num_banks;
7649 				state->hubp[i].pipe_interleave = pipe_ctx->plane_state->tiling_info.gfx9.pipe_interleave;
7650 				state->hubp[i].num_shader_engines = pipe_ctx->plane_state->tiling_info.gfx9.num_shader_engines;
7651 				state->hubp[i].num_rb_per_se = pipe_ctx->plane_state->tiling_info.gfx9.num_rb_per_se;
7652 				state->hubp[i].num_pkrs = pipe_ctx->plane_state->tiling_info.gfx9.num_pkrs;
7653 			}
7654 		}
7655 
7656 		/* DML Request Size Configuration */
7657 		if (pipe_ctx->rq_regs.rq_regs_l.chunk_size > 0) {
7658 			state->hubp[i].rq_chunk_size = pipe_ctx->rq_regs.rq_regs_l.chunk_size;
7659 			state->hubp[i].rq_min_chunk_size = pipe_ctx->rq_regs.rq_regs_l.min_chunk_size;
7660 			state->hubp[i].rq_meta_chunk_size = pipe_ctx->rq_regs.rq_regs_l.meta_chunk_size;
7661 			state->hubp[i].rq_min_meta_chunk_size = pipe_ctx->rq_regs.rq_regs_l.min_meta_chunk_size;
7662 			state->hubp[i].rq_dpte_group_size = pipe_ctx->rq_regs.rq_regs_l.dpte_group_size;
7663 			state->hubp[i].rq_mpte_group_size = pipe_ctx->rq_regs.rq_regs_l.mpte_group_size;
7664 			state->hubp[i].rq_swath_height_l = pipe_ctx->rq_regs.rq_regs_l.swath_height;
7665 			state->hubp[i].rq_pte_row_height_l = pipe_ctx->rq_regs.rq_regs_l.pte_row_height_linear;
7666 		}
7667 
7668 		/* Chroma request size configuration */
7669 		if (pipe_ctx->rq_regs.rq_regs_c.chunk_size > 0) {
7670 			state->hubp[i].rq_chunk_size_c = pipe_ctx->rq_regs.rq_regs_c.chunk_size;
7671 			state->hubp[i].rq_min_chunk_size_c = pipe_ctx->rq_regs.rq_regs_c.min_chunk_size;
7672 			state->hubp[i].rq_meta_chunk_size_c = pipe_ctx->rq_regs.rq_regs_c.meta_chunk_size;
7673 			state->hubp[i].rq_min_meta_chunk_size_c = pipe_ctx->rq_regs.rq_regs_c.min_meta_chunk_size;
7674 			state->hubp[i].rq_dpte_group_size_c = pipe_ctx->rq_regs.rq_regs_c.dpte_group_size;
7675 			state->hubp[i].rq_mpte_group_size_c = pipe_ctx->rq_regs.rq_regs_c.mpte_group_size;
7676 			state->hubp[i].rq_swath_height_c = pipe_ctx->rq_regs.rq_regs_c.swath_height;
7677 			state->hubp[i].rq_pte_row_height_c = pipe_ctx->rq_regs.rq_regs_c.pte_row_height_linear;
7678 		}
7679 
7680 		/* DML expansion modes */
7681 		state->hubp[i].drq_expansion_mode = pipe_ctx->rq_regs.drq_expansion_mode;
7682 		state->hubp[i].prq_expansion_mode = pipe_ctx->rq_regs.prq_expansion_mode;
7683 		state->hubp[i].mrq_expansion_mode = pipe_ctx->rq_regs.mrq_expansion_mode;
7684 		state->hubp[i].crq_expansion_mode = pipe_ctx->rq_regs.crq_expansion_mode;
7685 
7686 		/* DML DLG parameters - nominal */
7687 		state->hubp[i].dst_y_per_vm_vblank = pipe_ctx->dlg_regs.dst_y_per_vm_vblank;
7688 		state->hubp[i].dst_y_per_row_vblank = pipe_ctx->dlg_regs.dst_y_per_row_vblank;
7689 		state->hubp[i].dst_y_per_vm_flip = pipe_ctx->dlg_regs.dst_y_per_vm_flip;
7690 		state->hubp[i].dst_y_per_row_flip = pipe_ctx->dlg_regs.dst_y_per_row_flip;
7691 
7692 		/* DML prefetch settings */
7693 		state->hubp[i].dst_y_prefetch = pipe_ctx->dlg_regs.dst_y_prefetch;
7694 		state->hubp[i].vratio_prefetch = pipe_ctx->dlg_regs.vratio_prefetch;
7695 		state->hubp[i].vratio_prefetch_c = pipe_ctx->dlg_regs.vratio_prefetch_c;
7696 
7697 		/* TTU parameters */
7698 		state->hubp[i].qos_level_low_wm = pipe_ctx->ttu_regs.qos_level_low_wm;
7699 		state->hubp[i].qos_level_high_wm = pipe_ctx->ttu_regs.qos_level_high_wm;
7700 		state->hubp[i].qos_level_flip = pipe_ctx->ttu_regs.qos_level_flip;
7701 		state->hubp[i].min_ttu_vblank = pipe_ctx->ttu_regs.min_ttu_vblank;
7702 	}
7703 
7704 	/* Capture HUBBUB programming state */
7705 	if (dc->res_pool->hubbub) {
7706 		/* Individual DET buffer sizes - software state variables that program DET registers */
7707 		for (i = 0; i < 4u && i < dc->res_pool->pipe_count; i++) {
7708 			uint32_t det_size = res_ctx->pipe_ctx[i].det_buffer_size_kb;
7709 			switch (i) {
7710 			case 0:
7711 				state->hubbub.det0_size = det_size;
7712 				break;
7713 			case 1:
7714 				state->hubbub.det1_size = det_size;
7715 				break;
7716 			case 2:
7717 				state->hubbub.det2_size = det_size;
7718 				break;
7719 			case 3:
7720 				state->hubbub.det3_size = det_size;
7721 				break;
7722 			}
7723 		}
7724 
7725 		/* Compression buffer configuration - software state that programs COMPBUF_SIZE register */
7726 		// TODO: Handle logic for legacy DCN pre-DCN401
7727 		state->hubbub.compbuf_size = context->bw_ctx.bw.dcn.arb_regs.compbuf_size;
7728 	}
7729 
7730 	/* Capture DPP programming state for each pipe */
7731 	for (i = 0; i < max_pipes && i < dc->res_pool->pipe_count; i++) {
7732 		struct pipe_ctx *pipe_ctx = &res_ctx->pipe_ctx[i];
7733 
7734 		if (!pipe_ctx->stream)
7735 			continue;
7736 
7737 		state->dpp[i].dpp_clock_enable = (pipe_ctx->plane_res.dpp != NULL) ? 1 : 0;
7738 
7739 		if (pipe_ctx->plane_state && pipe_ctx->plane_res.scl_data.recout.width > 0) {
7740 			/* Access dscl_prog_data directly - this contains the actual software state used for register programming */
7741 			struct dscl_prog_data *dscl_data = &pipe_ctx->plane_res.scl_data.dscl_prog_data;
7742 
7743 			/* Recout (Rectangle of Interest) configuration - software state that programs RECOUT registers */
7744 			state->dpp[i].recout_start_x = dscl_data->recout.x;
7745 			state->dpp[i].recout_start_y = dscl_data->recout.y;
7746 			state->dpp[i].recout_width = dscl_data->recout.width;
7747 			state->dpp[i].recout_height = dscl_data->recout.height;
7748 
7749 			/* MPC (Multiple Pipe/Plane Combiner) size - software state that programs MPC_SIZE registers */
7750 			state->dpp[i].mpc_width = dscl_data->mpc_size.width;
7751 			state->dpp[i].mpc_height = dscl_data->mpc_size.height;
7752 
7753 			/* DSCL mode - software state that programs SCL_MODE registers */
7754 			state->dpp[i].dscl_mode = dscl_data->dscl_mode;
7755 
7756 			/* Scaler ratios - software state that programs scale ratio registers (use actual programmed ratios) */
7757 			state->dpp[i].horz_ratio_int = dscl_data->ratios.h_scale_ratio >> 19; // Extract integer part from programmed ratio
7758 			state->dpp[i].vert_ratio_int = dscl_data->ratios.v_scale_ratio >> 19; // Extract integer part from programmed ratio
7759 
7760 			/* Basic scaler taps - software state that programs tap control registers (use actual programmed taps) */
7761 			state->dpp[i].h_taps = dscl_data->taps.h_taps + 1; // dscl_prog_data.taps stores (taps - 1), so add 1 back
7762 			state->dpp[i].v_taps = dscl_data->taps.v_taps + 1; // dscl_prog_data.taps stores (taps - 1), so add 1 back
7763 		}
7764 	}
7765 
7766 	/* Capture essential clock state for underflow analysis */
7767 	if (dc->clk_mgr && dc->clk_mgr->clks.dispclk_khz > 0) {
7768 		/* Core display clocks affecting bandwidth and timing */
7769 		state->dccg.dispclk_khz = dc->clk_mgr->clks.dispclk_khz;
7770 
7771 		/* Per-pipe clock configuration - only capture what's essential */
7772 		for (i = 0; i < max_pipes && i < dc->res_pool->pipe_count; i++) {
7773 			struct pipe_ctx *pipe_ctx = &res_ctx->pipe_ctx[i];
7774 			if (pipe_ctx->stream) {
7775 				/* Essential clocks that directly affect underflow risk */
7776 				state->dccg.dppclk_khz[i] = dc->clk_mgr->clks.dppclk_khz;
7777 				state->dccg.pixclk_khz[i] = pipe_ctx->stream->timing.pix_clk_100hz / 10;
7778 				state->dccg.dppclk_enable[i] = 1;
7779 
7780 				/* DP stream clock only for DP signals */
7781 				if (pipe_ctx->stream->signal == SIGNAL_TYPE_DISPLAY_PORT ||
7782 						pipe_ctx->stream->signal == SIGNAL_TYPE_DISPLAY_PORT_MST) {
7783 					state->dccg.dpstreamclk_enable[i] = 1;
7784 				} else {
7785 					state->dccg.dpstreamclk_enable[i] = 0;
7786 				}
7787 			} else {
7788 				/* Inactive pipe - no clocks */
7789 				state->dccg.dppclk_khz[i] = 0;
7790 				state->dccg.pixclk_khz[i] = 0;
7791 				state->dccg.dppclk_enable[i] = 0;
7792 				if (i < 4) {
7793 					state->dccg.dpstreamclk_enable[i] = 0;
7794 				}
7795 			}
7796 		}
7797 
7798 		/* DSC clock state - only when actually using DSC */
7799 		for (i = 0; i < max_pipes; i++) {
7800 			struct pipe_ctx *pipe_ctx = (i < dc->res_pool->pipe_count) ? &res_ctx->pipe_ctx[i] : NULL;
7801 			if (pipe_ctx && pipe_ctx->stream && pipe_ctx->stream->timing.dsc_cfg.num_slices_h > 0) {
7802 				state->dccg.dscclk_khz[i] = 400000; /* Typical DSC clock frequency */
7803 			} else {
7804 				state->dccg.dscclk_khz[i] = 0;
7805 			}
7806 		}
7807 
7808 		/* SYMCLK32 LE Control - only the essential HPO state for underflow analysis */
7809 		for (i = 0; i < 2; i++) {
7810 			state->dccg.symclk32_le_enable[i] = 0; /* Default: disabled */
7811 		}
7812 
7813 		/* Check for active HPO usage that affects symclk32_le */
7814 		for (unsigned int pipe_idx = 0; pipe_idx < MAX_PIPES && pipe_idx < dc->res_pool->pipe_count; pipe_idx++) {
7815 			struct pipe_ctx *pipe_ctx = &res_ctx->pipe_ctx[pipe_idx];
7816 			if (!pipe_ctx->stream)
7817 				continue;
7818 
7819 			/* HPO FRL (HDMI FRL) streams use symclk32_le */
7820 			if (pipe_ctx->stream_res.hpo_frl_stream_enc && pipe_ctx->link_res.hpo_frl_link_enc) {
7821 				int hpo_le_inst = pipe_ctx->link_res.hpo_frl_link_enc->inst;
7822 				if (hpo_le_inst >= 0 && hpo_le_inst < 2) {
7823 					state->dccg.symclk32_le_enable[hpo_le_inst] = 1;
7824 				}
7825 			}
7826 		}
7827 	}
7828 
7829 	/* Capture essential DSC configuration for underflow analysis */
7830 	for (i = 0; i < max_pipes && i < dc->res_pool->pipe_count; i++) {
7831 		struct pipe_ctx *pipe_ctx = &res_ctx->pipe_ctx[i];
7832 
7833 		if (pipe_ctx->stream && pipe_ctx->stream->timing.dsc_cfg.num_slices_h > 0) {
7834 			/* DSC is enabled - capture essential configuration */
7835 			state->dsc[i].dsc_clock_enable = 1;
7836 
7837 			/* DSC configuration affecting bandwidth and timing */
7838 			struct dc_dsc_config *dsc_cfg = &pipe_ctx->stream->timing.dsc_cfg;
7839 			state->dsc[i].dsc_num_slices_h = dsc_cfg->num_slices_h;
7840 			state->dsc[i].dsc_num_slices_v = dsc_cfg->num_slices_v;
7841 			state->dsc[i].dsc_bits_per_pixel = dsc_cfg->bits_per_pixel;
7842 
7843 			/* OPP pipe source for DSC forwarding */
7844 			if (pipe_ctx->stream_res.opp) {
7845 				state->dsc[i].dscrm_dsc_forward_enable = 1;
7846 				state->dsc[i].dscrm_dsc_opp_pipe_source = pipe_ctx->stream_res.opp->inst;
7847 			} else {
7848 				state->dsc[i].dscrm_dsc_forward_enable = 0;
7849 				state->dsc[i].dscrm_dsc_opp_pipe_source = 0;
7850 			}
7851 		} else {
7852 			/* DSC not enabled - clear all fields */
7853 			memset(&state->dsc[i], 0, sizeof(state->dsc[i]));
7854 		}
7855 	}
7856 
7857 	/* Capture MPC programming state - comprehensive register field coverage */
7858 	for (i = 0; i < max_pipes && i < dc->res_pool->pipe_count; i++) {
7859 		struct pipe_ctx *pipe_ctx = &res_ctx->pipe_ctx[i];
7860 
7861 		if (pipe_ctx->plane_state && pipe_ctx->stream) {
7862 			struct dc_plane_state *plane_state = pipe_ctx->plane_state;
7863 
7864 			/* MPCC blending tree and mode control - capture actual blend configuration */
7865 			state->mpc.mpcc_mode[i] = (plane_state->cm.blend_func.type != TF_TYPE_BYPASS) ? 1 : 0;
7866 			state->mpc.mpcc_alpha_blend_mode[i] = plane_state->per_pixel_alpha ? 1 : 0;
7867 			state->mpc.mpcc_alpha_multiplied_mode[i] = plane_state->pre_multiplied_alpha ? 1 : 0;
7868 			state->mpc.mpcc_blnd_active_overlap_only[i] = 0; /* Default - no overlap restriction */
7869 			state->mpc.mpcc_global_alpha[i] = plane_state->global_alpha_value;
7870 			state->mpc.mpcc_global_gain[i] = plane_state->global_alpha ? 255 : 0;
7871 			state->mpc.mpcc_bg_bpc[i] = 8; /* Standard 8-bit background */
7872 			state->mpc.mpcc_bot_gain_mode[i] = 0; /* Standard gain mode */
7873 
7874 			/* MPCC blending tree connections - capture tree topology */
7875 			if (pipe_ctx->bottom_pipe) {
7876 				state->mpc.mpcc_bot_sel[i] = pipe_ctx->bottom_pipe->pipe_idx;
7877 			} else {
7878 				state->mpc.mpcc_bot_sel[i] = 0xF; /* No bottom connection */
7879 			}
7880 			state->mpc.mpcc_top_sel[i] = pipe_ctx->pipe_idx; /* This pipe's DPP ID */
7881 
7882 			/* MPCC output gamma control - capture gamma programming */
7883 			if (plane_state->gamma_correction.type != GAMMA_CS_TFM_1D && plane_state->gamma_correction.num_entries > 0) {
7884 				state->mpc.mpcc_ogam_mode[i] = 1; /* Gamma enabled */
7885 				state->mpc.mpcc_ogam_select[i] = 0; /* Bank A selection */
7886 				state->mpc.mpcc_ogam_pwl_disable[i] = 0; /* PWL enabled */
7887 			} else {
7888 				state->mpc.mpcc_ogam_mode[i] = 0; /* Bypass mode */
7889 				state->mpc.mpcc_ogam_select[i] = 0;
7890 				state->mpc.mpcc_ogam_pwl_disable[i] = 1; /* PWL disabled */
7891 			}
7892 
7893 			/* MPCC pipe assignment and operational status */
7894 			if (pipe_ctx->stream_res.opp) {
7895 				state->mpc.mpcc_opp_id[i] = pipe_ctx->stream_res.opp->inst;
7896 			} else {
7897 				state->mpc.mpcc_opp_id[i] = 0xF; /* No OPP assignment */
7898 			}
7899 
7900 			/* MPCC status indicators - active pipe state */
7901 			state->mpc.mpcc_idle[i] = 0; /* Active pipe - not idle */
7902 			state->mpc.mpcc_busy[i] = 1; /* Active pipe - busy processing */
7903 
7904 		} else {
7905 			/* Pipe not active - set disabled/idle state for all fields */
7906 			state->mpc.mpcc_mode[i] = 0;
7907 			state->mpc.mpcc_alpha_blend_mode[i] = 0;
7908 			state->mpc.mpcc_alpha_multiplied_mode[i] = 0;
7909 			state->mpc.mpcc_blnd_active_overlap_only[i] = 0;
7910 			state->mpc.mpcc_global_alpha[i] = 0;
7911 			state->mpc.mpcc_global_gain[i] = 0;
7912 			state->mpc.mpcc_bg_bpc[i] = 0;
7913 			state->mpc.mpcc_bot_gain_mode[i] = 0;
7914 			state->mpc.mpcc_bot_sel[i] = 0xF; /* No bottom connection */
7915 			state->mpc.mpcc_top_sel[i] = 0xF; /* No top connection */
7916 			state->mpc.mpcc_ogam_mode[i] = 0; /* Bypass */
7917 			state->mpc.mpcc_ogam_select[i] = 0;
7918 			state->mpc.mpcc_ogam_pwl_disable[i] = 1; /* PWL disabled */
7919 			state->mpc.mpcc_opp_id[i] = 0xF; /* No OPP assignment */
7920 			state->mpc.mpcc_idle[i] = 1; /* Idle */
7921 			state->mpc.mpcc_busy[i] = 0; /* Not busy */
7922 		}
7923 	}
7924 
7925 	/* Capture OPP programming state for each pipe - comprehensive register field coverage */
7926 	for (i = 0; i < max_pipes && i < dc->res_pool->pipe_count; i++) {
7927 		struct pipe_ctx *pipe_ctx = &res_ctx->pipe_ctx[i];
7928 
7929 		if (!pipe_ctx->stream)
7930 			continue;
7931 
7932 		if (pipe_ctx->stream_res.opp) {
7933 			struct dc_crtc_timing *timing = &pipe_ctx->stream->timing;
7934 
7935 			/* OPP Pipe Control */
7936 			state->opp[i].opp_pipe_clock_enable = 1; /* Active pipe has clock enabled */
7937 
7938 			/* Display Pattern Generator (DPG) Control - 19 fields */
7939 			if (pipe_ctx->stream->test_pattern.type != DP_TEST_PATTERN_VIDEO_MODE) {
7940 				state->opp[i].dpg_enable = 1;
7941 			} else {
7942 				/* Video mode - DPG disabled */
7943 				state->opp[i].dpg_enable = 0;
7944 			}
7945 
7946 			/* Format Control (FMT) - 18 fields */
7947 			state->opp[i].fmt_pixel_encoding = timing->pixel_encoding;
7948 
7949 			/* Chroma subsampling mode based on pixel encoding */
7950 			if (timing->pixel_encoding == PIXEL_ENCODING_YCBCR420) {
7951 				state->opp[i].fmt_subsampling_mode = 1; /* 4:2:0 subsampling */
7952 			} else if (timing->pixel_encoding == PIXEL_ENCODING_YCBCR422) {
7953 				state->opp[i].fmt_subsampling_mode = 2; /* 4:2:2 subsampling */
7954 			} else {
7955 				state->opp[i].fmt_subsampling_mode = 0; /* No subsampling (4:4:4) */
7956 			}
7957 
7958 			state->opp[i].fmt_cbcr_bit_reduction_bypass = (timing->pixel_encoding == PIXEL_ENCODING_RGB) ? 1 : 0;
7959 			state->opp[i].fmt_stereosync_override = (timing->timing_3d_format != TIMING_3D_FORMAT_NONE) ? 1 : 0;
7960 
7961 			/* Dithering control based on bit depth */
7962 			if (timing->display_color_depth < COLOR_DEPTH_121212) {
7963 				state->opp[i].fmt_spatial_dither_frame_counter_max = 15; /* Typical frame counter max */
7964 				state->opp[i].fmt_spatial_dither_frame_counter_bit_swap = 0; /* No bit swapping */
7965 				state->opp[i].fmt_spatial_dither_enable = 1;
7966 				state->opp[i].fmt_spatial_dither_mode = 0; /* Spatial dithering mode */
7967 				state->opp[i].fmt_spatial_dither_depth = timing->display_color_depth;
7968 				state->opp[i].fmt_temporal_dither_enable = 0; /* Spatial dithering preferred */
7969 			} else {
7970 				state->opp[i].fmt_spatial_dither_frame_counter_max = 0;
7971 				state->opp[i].fmt_spatial_dither_frame_counter_bit_swap = 0;
7972 				state->opp[i].fmt_spatial_dither_enable = 0;
7973 				state->opp[i].fmt_spatial_dither_mode = 0;
7974 				state->opp[i].fmt_spatial_dither_depth = 0;
7975 				state->opp[i].fmt_temporal_dither_enable = 0;
7976 			}
7977 
7978 			/* Truncation control for bit depth reduction */
7979 			if (timing->display_color_depth < COLOR_DEPTH_121212) {
7980 				state->opp[i].fmt_truncate_enable = 1;
7981 				state->opp[i].fmt_truncate_depth = timing->display_color_depth;
7982 				state->opp[i].fmt_truncate_mode = 0; /* Round mode */
7983 			} else {
7984 				state->opp[i].fmt_truncate_enable = 0;
7985 				state->opp[i].fmt_truncate_depth = 0;
7986 				state->opp[i].fmt_truncate_mode = 0;
7987 			}
7988 
7989 			/* Data clamping control */
7990 			state->opp[i].fmt_clamp_data_enable = 1; /* Clamping typically enabled */
7991 			state->opp[i].fmt_clamp_color_format = timing->pixel_encoding;
7992 
7993 			/* Dynamic expansion for limited range content */
7994 			if (timing->pixel_encoding != PIXEL_ENCODING_RGB) {
7995 				state->opp[i].fmt_dynamic_exp_enable = 1; /* YCbCr typically needs expansion */
7996 				state->opp[i].fmt_dynamic_exp_mode = 0; /* Standard expansion */
7997 			} else {
7998 				state->opp[i].fmt_dynamic_exp_enable = 0; /* RGB typically full range */
7999 				state->opp[i].fmt_dynamic_exp_mode = 0;
8000 			}
8001 
8002 			/* Legacy field for compatibility */
8003 			state->opp[i].fmt_bit_depth_control = timing->display_color_depth;
8004 
8005 			/* Output Buffer (OPPBUF) Control - 6 fields */
8006 			state->opp[i].oppbuf_active_width = timing->h_addressable;
8007 			state->opp[i].oppbuf_pixel_repetition = 0; /* No pixel repetition by default */
8008 
8009 			/* Multi-Stream Output (MSO) / ODM segmentation */
8010 			if (pipe_ctx->next_odm_pipe) {
8011 				state->opp[i].oppbuf_display_segmentation = 1; /* Segmented display */
8012 				state->opp[i].oppbuf_overlap_pixel_num = 0; /* ODM overlap pixels */
8013 			} else {
8014 				state->opp[i].oppbuf_display_segmentation = 0; /* Single segment */
8015 				state->opp[i].oppbuf_overlap_pixel_num = 0;
8016 			}
8017 
8018 			/* 3D/Stereo control */
8019 			if (timing->timing_3d_format != TIMING_3D_FORMAT_NONE) {
8020 				state->opp[i].oppbuf_3d_vact_space1_size = 30; /* Typical stereo blanking */
8021 				state->opp[i].oppbuf_3d_vact_space2_size = 30;
8022 			} else {
8023 				state->opp[i].oppbuf_3d_vact_space1_size = 0;
8024 				state->opp[i].oppbuf_3d_vact_space2_size = 0;
8025 			}
8026 
8027 			/* DSC Forward Config - 3 fields */
8028 			if (timing->dsc_cfg.num_slices_h > 0) {
8029 				state->opp[i].dscrm_dsc_forward_enable = 1;
8030 				state->opp[i].dscrm_dsc_opp_pipe_source = pipe_ctx->stream_res.opp->inst;
8031 				state->opp[i].dscrm_dsc_forward_enable_status = 1; /* Status follows enable */
8032 			} else {
8033 				state->opp[i].dscrm_dsc_forward_enable = 0;
8034 				state->opp[i].dscrm_dsc_opp_pipe_source = 0;
8035 				state->opp[i].dscrm_dsc_forward_enable_status = 0;
8036 			}
8037 		} else {
8038 			/* No OPP resource - set all fields to disabled state */
8039 			memset(&state->opp[i], 0, sizeof(state->opp[i]));
8040 		}
8041 	}
8042 
8043 	/* Capture OPTC programming state for each pipe - comprehensive register field coverage */
8044 	for (i = 0; i < max_pipes && i < dc->res_pool->pipe_count; i++) {
8045 		struct pipe_ctx *pipe_ctx = &res_ctx->pipe_ctx[i];
8046 
8047 		if (!pipe_ctx->stream)
8048 			continue;
8049 
8050 		if (pipe_ctx->stream_res.tg) {
8051 			struct dc_crtc_timing *timing = &pipe_ctx->stream->timing;
8052 
8053 			state->optc[i].otg_master_inst = pipe_ctx->stream_res.tg->inst;
8054 
8055 			/* OTG_CONTROL register - 5 fields */
8056 			state->optc[i].otg_master_enable = 1; /* Active stream */
8057 			state->optc[i].otg_disable_point_cntl = 0; /* Normal operation */
8058 			state->optc[i].otg_start_point_cntl = 0; /* Normal start */
8059 			state->optc[i].otg_field_number_cntl = (timing->flags.INTERLACE) ? 1 : 0;
8060 			state->optc[i].otg_out_mux = 0; /* Direct output */
8061 
8062 			/* OTG Horizontal Timing - 7 fields */
8063 			state->optc[i].otg_h_total = timing->h_total;
8064 			state->optc[i].otg_h_blank_start = timing->h_addressable;
8065 			state->optc[i].otg_h_blank_end = timing->h_total - timing->h_front_porch;
8066 			state->optc[i].otg_h_sync_start = timing->h_addressable + timing->h_front_porch;
8067 			state->optc[i].otg_h_sync_end = timing->h_addressable + timing->h_front_porch + timing->h_sync_width;
8068 			state->optc[i].otg_h_sync_polarity = timing->flags.HSYNC_POSITIVE_POLARITY ? 0 : 1;
8069 			state->optc[i].otg_h_timing_div_mode = (pipe_ctx->next_odm_pipe) ? 1 : 0; /* ODM divide mode */
8070 
8071 			/* OTG Vertical Timing - 7 fields */
8072 			state->optc[i].otg_v_total = timing->v_total;
8073 			state->optc[i].otg_v_blank_start = timing->v_addressable;
8074 			state->optc[i].otg_v_blank_end = timing->v_total - timing->v_front_porch;
8075 			state->optc[i].otg_v_sync_start = timing->v_addressable + timing->v_front_porch;
8076 			state->optc[i].otg_v_sync_end = timing->v_addressable + timing->v_front_porch + timing->v_sync_width;
8077 			state->optc[i].otg_v_sync_polarity = timing->flags.VSYNC_POSITIVE_POLARITY ? 0 : 1;
8078 			state->optc[i].otg_v_sync_mode = 0; /* Normal sync mode */
8079 
8080 			/* Initialize remaining core fields with appropriate defaults */
8081 			// TODO: Update logic for accurate vtotal min/max
8082 			state->optc[i].otg_v_total_max = timing->v_total + 100; /* Typical DRR range */
8083 			state->optc[i].otg_v_total_min = timing->v_total - 50;
8084 			state->optc[i].otg_v_total_mid = timing->v_total;
8085 
8086 			/* ODM configuration */
8087 			// TODO: Update logic to have complete ODM mappings (e.g. 3:1 and 4:1) stored in single pipe
8088 			if (pipe_ctx->next_odm_pipe) {
8089 				state->optc[i].optc_seg0_src_sel = pipe_ctx->stream_res.opp ? pipe_ctx->stream_res.opp->inst : 0;
8090 				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;
8091 				state->optc[i].optc_num_of_input_segment = 1; /* 2 segments - 1 */
8092 			} else {
8093 				state->optc[i].optc_seg0_src_sel = pipe_ctx->stream_res.opp ? pipe_ctx->stream_res.opp->inst : 0;
8094 				state->optc[i].optc_seg1_src_sel = 0;
8095 				state->optc[i].optc_num_of_input_segment = 0; /* Single segment */
8096 			}
8097 
8098 			/* DSC configuration */
8099 			if (timing->dsc_cfg.num_slices_h > 0) {
8100 				state->optc[i].optc_dsc_mode = 1; /* DSC enabled */
8101 				state->optc[i].optc_dsc_bytes_per_pixel = timing->dsc_cfg.bits_per_pixel / 16; /* Convert to bytes */
8102 				state->optc[i].optc_dsc_slice_width = timing->h_addressable / timing->dsc_cfg.num_slices_h;
8103 			} else {
8104 				state->optc[i].optc_dsc_mode = 0;
8105 				state->optc[i].optc_dsc_bytes_per_pixel = 0;
8106 				state->optc[i].optc_dsc_slice_width = 0;
8107 			}
8108 
8109 			/* Essential control fields */
8110 			state->optc[i].otg_stereo_enable = (timing->timing_3d_format != TIMING_3D_FORMAT_NONE) ? 1 : 0;
8111 			state->optc[i].otg_interlace_enable = timing->flags.INTERLACE ? 1 : 0;
8112 			state->optc[i].otg_clock_enable = 1; /* OTG clock enabled */
8113 			state->optc[i].vtg0_enable = 1; /* VTG enabled for timing generation */
8114 
8115 			/* Initialize other key fields to defaults */
8116 			state->optc[i].optc_input_pix_clk_en = 1;
8117 			state->optc[i].optc_segment_width = (pipe_ctx->next_odm_pipe) ? (timing->h_addressable / 2) : timing->h_addressable;
8118 			state->optc[i].otg_vready_offset = 1;
8119 			state->optc[i].otg_vstartup_start = timing->v_addressable + 10;
8120 			state->optc[i].otg_vupdate_offset = 0;
8121 			state->optc[i].otg_vupdate_width = 5;
8122 		} else {
8123 			/* No timing generator resource - initialize all fields to 0 */
8124 			memset(&state->optc[i], 0, sizeof(state->optc[i]));
8125 		}
8126 	}
8127 
8128 	state->state_valid = true;
8129 	return true;
8130 }
8131 
dc_log_preos_dmcub_info(const struct dc * dc)8132 void dc_log_preos_dmcub_info(const struct dc *dc)
8133 {
8134 	dc_dmub_srv_log_preos_dmcub_info(dc->ctx->dmub_srv);
8135 }
8136 
dc_get_qos_info(struct dc * dc,struct dc_qos_info * info)8137 bool dc_get_qos_info(struct dc *dc, struct dc_qos_info *info)
8138 {
8139 	const struct dc_clocks *clk = &dc->current_state->bw_ctx.bw.dcn.clk;
8140 	struct dc_requested_memory_qos requested = {};
8141 
8142 	memset(info, 0, sizeof(*info));
8143 
8144 	info->dcn_bandwidth_ub_in_mbps = (uint32_t)(clk->fclk_khz / 1000 * 64);
8145 
8146 	if (dc->clk_mgr && dc->clk_mgr->funcs->get_requested_memory_qos) {
8147 		dc->clk_mgr->funcs->get_requested_memory_qos(dc->clk_mgr, &requested);
8148 		info->qos_bandwidth_lb_in_mbps    = requested.bandwidth_lb_in_mbps;
8149 		info->calculated_avg_bw_in_mbps   = requested.calculated_avg_bw_in_mbps;
8150 		info->qos_max_latency_ub_in_ns    = requested.max_latency_ub_in_ns;
8151 		info->qos_avg_latency_ub_in_ns    = requested.avg_latency_ub_in_ns;
8152 		info->qos_max_bw_budget_in_mbps   = requested.max_bw_budget_in_mbps;
8153 	}
8154 
8155 	return true;
8156 }
8157 
dc_override_memory_bandwidth_request(struct dc * dc,unsigned int bw_mbps)8158 unsigned int dc_override_memory_bandwidth_request(
8159 		struct dc *dc,
8160 		unsigned int bw_mbps)
8161 {
8162 	if (!dc->clk_mgr || !dc->clk_mgr->funcs)
8163 		return 0;
8164 
8165 	return dc->clk_mgr->funcs->override_memory_bandwidth_request(
8166 			dc->clk_mgr, bw_mbps * 1000) / 1000;
8167 }
8168 
update_planes_and_stream_prepare_v2(struct dc_update_scratch_space * scratch)8169 static bool update_planes_and_stream_prepare_v2(
8170 		struct dc_update_scratch_space *scratch
8171 )
8172 {
8173 	// v2 is too tangled to break into stages, so just execute everything under lock
8174 	dc_exit_ips_for_hw_access(scratch->dc);
8175 	return update_planes_and_stream_v2(
8176 		scratch->dc,
8177 		scratch->surface_updates,
8178 		scratch->surface_count,
8179 		scratch->stream,
8180 		scratch->stream_update
8181 	);
8182 }
8183 
update_planes_and_stream_execute_v2(const struct dc_update_scratch_space * scratch)8184 static void update_planes_and_stream_execute_v2(
8185 		const struct dc_update_scratch_space *scratch
8186 )
8187 {
8188 	// Nothing to do, see `update_planes_and_stream_prepare_v2`
8189 	(void) scratch;
8190 }
8191 
update_planes_and_stream_cleanup_v2(const struct dc_update_scratch_space * scratch)8192 static bool update_planes_and_stream_cleanup_v2(
8193 		const struct dc_update_scratch_space *scratch
8194 )
8195 {
8196 	if (scratch->do_clear_update_bits)
8197 		clear_update_bits(scratch->surface_updates, scratch->surface_count, scratch->stream);
8198 
8199 	return false;
8200 }
8201 
8202 static void update_planes_and_stream_cleanup_v3_release_minimal(
8203 		struct dc_update_scratch_space *scratch,
8204 		bool backup
8205 );
8206 
update_planes_and_stream_prepare_v3_intermediate_seamless(struct dc_update_scratch_space * scratch)8207 static bool update_planes_and_stream_prepare_v3_intermediate_seamless(
8208 		struct dc_update_scratch_space *scratch
8209 )
8210 {
8211 	return is_pipe_topology_transition_seamless_with_intermediate_step(
8212 			scratch->dc,
8213 			scratch->dc->current_state,
8214 			scratch->intermediate_context,
8215 			scratch->new_context
8216 	);
8217 }
8218 
transition_countdown_init(struct dc * dc)8219 static void transition_countdown_init(struct dc *dc)
8220 {
8221 	dc->check_config.transition_countdown_to_steady_state =
8222 			dc->debug.num_fast_flips_to_steady_state_override ?
8223 			dc->debug.num_fast_flips_to_steady_state_override :
8224 			NUM_FAST_FLIPS_TO_STEADY_STATE;
8225 }
8226 
update_planes_and_stream_prepare_v3(struct dc_update_scratch_space * scratch)8227 static bool update_planes_and_stream_prepare_v3(
8228 		struct dc_update_scratch_space *scratch
8229 )
8230 {
8231 	if (scratch->flow == UPDATE_V3_FLOW_NEW_CONTEXT_SEAMLESS) {
8232 		return true;
8233 	}
8234 	ASSERT(scratch->flow == UPDATE_V3_FLOW_INVALID);
8235 	dc_exit_ips_for_hw_access(scratch->dc);
8236 
8237 	/* HWSS path determination needs to be done prior to updating the surface and stream states. */
8238 	struct dc_fast_update fast_update[MAX_SURFACES] = { 0 };
8239 
8240 	populate_fast_updates(fast_update,
8241 			      scratch->surface_updates,
8242 			      scratch->surface_count,
8243 			      scratch->stream_update);
8244 
8245 	const bool is_hwss_fast_path_only =
8246 		fast_update_only(scratch->dc,
8247 				 fast_update,
8248 				 scratch->surface_updates,
8249 				 scratch->surface_count,
8250 				 scratch->stream_update,
8251 				 scratch->stream) &&
8252 		!scratch->dc->check_config.enable_legacy_fast_update;
8253 
8254 	if (!update_planes_and_stream_state(
8255 			scratch->dc,
8256 			scratch->surface_updates,
8257 			scratch->surface_count,
8258 			scratch->stream,
8259 			scratch->stream_update,
8260 			&scratch->update_type,
8261 			&scratch->new_context
8262 	)) {
8263 		return false;
8264 	}
8265 
8266 	if (scratch->new_context == scratch->dc->current_state) {
8267 		ASSERT(scratch->update_type < UPDATE_TYPE_FULL);
8268 
8269 		scratch->flow = is_hwss_fast_path_only
8270 				? UPDATE_V3_FLOW_NO_NEW_CONTEXT_CONTEXT_FAST
8271 				: UPDATE_V3_FLOW_NO_NEW_CONTEXT_CONTEXT_FULL;
8272 		return true;
8273 	}
8274 
8275 	ASSERT(scratch->update_type >= UPDATE_TYPE_FULL);
8276 
8277 	const bool seamless = scratch->dc->hwss.is_pipe_topology_transition_seamless(
8278 			scratch->dc,
8279 			scratch->dc->current_state,
8280 			scratch->new_context
8281 	);
8282 	if (seamless) {
8283 		scratch->flow = UPDATE_V3_FLOW_NEW_CONTEXT_SEAMLESS;
8284 		if (scratch->dc->check_config.deferred_transition_state)
8285 			/* reset countdown as steady state not reached */
8286 			transition_countdown_init(scratch->dc);
8287 		return true;
8288 	}
8289 
8290 	if (!scratch->dc->debug.disable_deferred_minimal_transitions) {
8291 		scratch->dc->check_config.deferred_transition_state = true;
8292 		transition_countdown_init(scratch->dc);
8293 	}
8294 
8295 	scratch->intermediate_context = create_minimal_transition_state(
8296 		scratch->dc,
8297 		scratch->new_context,
8298 		&scratch->intermediate_policy
8299 	);
8300 	if (scratch->intermediate_context) {
8301 		if (update_planes_and_stream_prepare_v3_intermediate_seamless(scratch)) {
8302 			scratch->flow = UPDATE_V3_FLOW_NEW_CONTEXT_MINIMAL_NEW;
8303 			return true;
8304 		}
8305 
8306 		update_planes_and_stream_cleanup_v3_release_minimal(scratch, false);
8307 	}
8308 
8309 	scratch->backup_context = scratch->dc->current_state;
8310 	restore_planes_and_stream_state(&scratch->dc->scratch.current_state, scratch->stream);
8311 	dc_state_retain(scratch->backup_context);
8312 	scratch->intermediate_context = create_minimal_transition_state(
8313 			scratch->dc,
8314 			scratch->backup_context,
8315 			&scratch->intermediate_policy
8316 	);
8317 	if (scratch->intermediate_context) {
8318 		if (update_planes_and_stream_prepare_v3_intermediate_seamless(scratch)) {
8319 			scratch->flow = UPDATE_V3_FLOW_NEW_CONTEXT_MINIMAL_CURRENT;
8320 			scratch->intermediate_count = initialize_empty_surface_updates(
8321 					scratch->stream, scratch->intermediate_updates
8322 			);
8323 			return true;
8324 		}
8325 
8326 		update_planes_and_stream_cleanup_v3_release_minimal(scratch, true);
8327 	}
8328 
8329 	scratch->flow = UPDATE_V3_FLOW_INVALID;
8330 	dc_state_release(scratch->backup_context);
8331 	restore_planes_and_stream_state(&scratch->dc->scratch.new_state, scratch->stream);
8332 	return false;
8333 }
8334 
8335 /**
8336  * should_commit_intermediate_context - Does this flow commit a transient
8337  * minimal-transition intermediate context
8338  * @flow: the commit flow selected for this iteration
8339  *
8340  * Return: true if this iteration commits the intermediate context.
8341  */
should_commit_intermediate_context(enum update_v3_flow flow)8342 static bool should_commit_intermediate_context(enum update_v3_flow flow)
8343 {
8344 	return flow == UPDATE_V3_FLOW_NEW_CONTEXT_MINIMAL_NEW
8345 			|| flow == UPDATE_V3_FLOW_NEW_CONTEXT_MINIMAL_CURRENT;
8346 }
8347 
update_planes_and_stream_execute_v3_commit(const struct dc_update_scratch_space * scratch,bool intermediate_update,bool intermediate_context,bool use_stream_update)8348 static void update_planes_and_stream_execute_v3_commit(
8349 		const struct dc_update_scratch_space *scratch,
8350 		bool intermediate_update,
8351 		bool intermediate_context,
8352 		bool use_stream_update
8353 )
8354 {
8355 	commit_planes_for_stream(
8356 			scratch->dc,
8357 			intermediate_update ? scratch->intermediate_updates : scratch->surface_updates,
8358 			intermediate_update ? scratch->intermediate_count : scratch->surface_count,
8359 			scratch->stream,
8360 			use_stream_update ? scratch->stream_update : NULL,
8361 			intermediate_context ? UPDATE_TYPE_FULL : scratch->update_type,
8362 			// `dc->current_state` only used in `NO_NEW_CONTEXT`, where it is equal to `new_context`
8363 			intermediate_context ? scratch->intermediate_context : scratch->new_context
8364 	);
8365 }
8366 
update_planes_and_stream_execute_v3(const struct dc_update_scratch_space * scratch)8367 static void update_planes_and_stream_execute_v3(
8368 		const struct dc_update_scratch_space *scratch
8369 )
8370 {
8371 	bool intermediate_context = should_commit_intermediate_context(scratch->flow);
8372 
8373 	switch (scratch->flow) {
8374 	case UPDATE_V3_FLOW_NO_NEW_CONTEXT_CONTEXT_FAST:
8375 		commit_planes_for_stream_fast(
8376 				scratch->dc,
8377 				scratch->surface_updates,
8378 				scratch->surface_count,
8379 				scratch->stream,
8380 				scratch->stream_update,
8381 				scratch->update_type,
8382 				scratch->new_context
8383 		);
8384 		break;
8385 
8386 	case UPDATE_V3_FLOW_NO_NEW_CONTEXT_CONTEXT_FULL:
8387 	case UPDATE_V3_FLOW_NEW_CONTEXT_SEAMLESS:
8388 		update_planes_and_stream_execute_v3_commit(scratch, false, intermediate_context, true);
8389 		break;
8390 
8391 	case UPDATE_V3_FLOW_NEW_CONTEXT_MINIMAL_NEW:
8392 		update_planes_and_stream_execute_v3_commit(scratch, false, intermediate_context,
8393 				scratch->dc->check_config.deferred_transition_state);
8394 		break;
8395 
8396 	case UPDATE_V3_FLOW_NEW_CONTEXT_MINIMAL_CURRENT:
8397 		update_planes_and_stream_execute_v3_commit(scratch, true, intermediate_context, false);
8398 		break;
8399 
8400 	case UPDATE_V3_FLOW_INVALID:
8401 	default:
8402 		ASSERT(false);
8403 	}
8404 }
8405 
update_planes_and_stream_cleanup_v3_release_minimal(struct dc_update_scratch_space * scratch,bool backup)8406 static void update_planes_and_stream_cleanup_v3_release_minimal(
8407 		struct dc_update_scratch_space *scratch,
8408 		bool backup
8409 )
8410 {
8411 	release_minimal_transition_state(
8412 			scratch->dc,
8413 			scratch->intermediate_context,
8414 			backup ? scratch->backup_context : scratch->new_context,
8415 			&scratch->intermediate_policy
8416 	);
8417 }
8418 
update_planes_and_stream_cleanup_v3_intermediate(struct dc_update_scratch_space * scratch,bool backup)8419 static void update_planes_and_stream_cleanup_v3_intermediate(
8420 		struct dc_update_scratch_space *scratch,
8421 		bool backup
8422 )
8423 {
8424 	swap_and_release_current_context(scratch->dc, scratch->intermediate_context, scratch->stream);
8425 	dc_state_retain(scratch->dc->current_state);
8426 	update_planes_and_stream_cleanup_v3_release_minimal(scratch, backup);
8427 }
8428 
update_planes_and_stream_cleanup_v3(struct dc_update_scratch_space * scratch)8429 static bool update_planes_and_stream_cleanup_v3(
8430 		struct dc_update_scratch_space *scratch
8431 )
8432 {
8433 	switch (scratch->flow) {
8434 	case UPDATE_V3_FLOW_NO_NEW_CONTEXT_CONTEXT_FAST:
8435 	case UPDATE_V3_FLOW_NO_NEW_CONTEXT_CONTEXT_FULL:
8436 		if (scratch->dc->check_config.transition_countdown_to_steady_state)
8437 			scratch->dc->check_config.transition_countdown_to_steady_state--;
8438 		break;
8439 
8440 	case UPDATE_V3_FLOW_NEW_CONTEXT_SEAMLESS:
8441 		swap_and_release_current_context(scratch->dc, scratch->new_context, scratch->stream);
8442 		break;
8443 
8444 	case UPDATE_V3_FLOW_NEW_CONTEXT_MINIMAL_NEW:
8445 		update_planes_and_stream_cleanup_v3_intermediate(scratch, false);
8446 		if (scratch->dc->check_config.deferred_transition_state) {
8447 			dc_state_release(scratch->new_context);
8448 		} else {
8449 			scratch->flow = UPDATE_V3_FLOW_NEW_CONTEXT_SEAMLESS;
8450 			return true;
8451 		}
8452 		break;
8453 
8454 	case UPDATE_V3_FLOW_NEW_CONTEXT_MINIMAL_CURRENT:
8455 		update_planes_and_stream_cleanup_v3_intermediate(scratch, true);
8456 		dc_state_release(scratch->backup_context);
8457 		restore_planes_and_stream_state(&scratch->dc->scratch.new_state, scratch->stream);
8458 		scratch->flow = UPDATE_V3_FLOW_NEW_CONTEXT_SEAMLESS;
8459 		return true;
8460 
8461 	case UPDATE_V3_FLOW_INVALID:
8462 	default:
8463 		ASSERT(false);
8464 	}
8465 
8466 	if (scratch->do_clear_update_bits)
8467 		clear_update_bits(scratch->surface_updates, scratch->surface_count, scratch->stream);
8468 
8469 	return false;
8470 }
8471 
dc_update_state_init(struct dc * dc,const struct dc_state_update * updates)8472 struct dc_update_scratch_space *dc_update_state_init(
8473 		struct dc *dc,
8474 		const struct dc_state_update *updates
8475 )
8476 {
8477 	const enum dce_version version = dc->ctx->dce_version;
8478 	struct dc_update_scratch_space *scratch = dc_update_scratch_acquire(dc);
8479 	const bool has_stream_or_plane = updates->stream || updates->stream_update || updates->surface_updates;
8480 	const bool has_probe = updates->probe_updates;
8481 	const bool surface_without_stream = updates->surface_updates && !updates->stream;
8482 	const bool stream_update_without_stream = updates->stream_update && !updates->stream;
8483 	const bool bad_surface_count = updates->surface_count > 0 && !updates->surface_updates;
8484 
8485 	if (!scratch)
8486 		return NULL;
8487 
8488 	if (!has_stream_or_plane && !has_probe) {
8489 		dc_update_scratch_release(dc, scratch);
8490 		return NULL;
8491 	}
8492 
8493 	if (surface_without_stream || stream_update_without_stream || bad_surface_count) {
8494 		dc_update_scratch_release(dc, scratch);
8495 		return NULL;
8496 	}
8497 
8498 	*scratch = (struct dc_update_scratch_space){
8499 		.dc = dc,
8500 		.surface_updates = updates->surface_updates,
8501 		.surface_count = updates->surface_count,
8502 		.stream = updates->stream,
8503 		.stream_update = updates->stream_update,
8504 		.probe_updates = updates->probe_updates,
8505 		.update_v3 = version >= DCN_VERSION_4_01
8506 				|| version == DCN_VERSION_3_2
8507 				|| version == DCN_VERSION_3_21,
8508 		.do_clear_update_bits = version >= DCN_VERSION_1_0,
8509 		.new_context = NULL,
8510 		.flow = UPDATE_V3_FLOW_INVALID,
8511 	};
8512 
8513 	return scratch;
8514 }
8515 
8516 /**
8517  * dc_update_probes_prepare - Commit the desired probe set into new_context.
8518  * @scratch: commit scratch carrying the probe updates
8519  *
8520  * Return: true on success or when there is nothing to do; false when the
8521  * desired set is unachievable.
8522  */
dc_update_probes_prepare(struct dc_update_scratch_space * scratch)8523 static bool dc_update_probes_prepare(struct dc_update_scratch_space *scratch)
8524 {
8525 	struct dc *dc = scratch->dc;
8526 	const struct dc_probe_updates *probe_updates = scratch->probe_updates;
8527 	uint8_t i;
8528 
8529 	if (!probe_updates)
8530 		return true;
8531 
8532 	if (resource_validate_probe_set(dc, probe_updates->probes,
8533 			(uint8_t)probe_updates->probe_count) != DC_OK)
8534 		return false;
8535 
8536 	if (!scratch->new_context)
8537 		scratch->new_context = dc->current_state;
8538 
8539 	for (i = 0; i < probe_updates->probe_count && i < MAX_PROBES; i++)
8540 		scratch->new_context->probes[i] = probe_updates->probes[i];
8541 	scratch->new_context->probe_count = probe_updates->probe_count;
8542 
8543 	return true;
8544 }
8545 
8546 /**
8547  * dc_update_probes_execute - Program the committed probes.
8548  * @scratch: commit scratch carrying the probe updates
8549  *
8550  */
dc_update_probes_execute(const struct dc_update_scratch_space * scratch)8551 static void dc_update_probes_execute(const struct dc_update_scratch_space *scratch)
8552 {
8553 	struct dc *dc = scratch->dc;
8554 
8555 	if (should_commit_intermediate_context(scratch->flow))
8556 		return;
8557 
8558 	if (dc->hwss.program_perfmon)
8559 		dc->hwss.program_perfmon(dc, scratch->new_context);
8560 }
8561 
dc_update_state_prepare(struct dc_update_scratch_space * scratch)8562 bool dc_update_state_prepare(struct dc_update_scratch_space *scratch)
8563 {
8564 	if (scratch->stream) {
8565 		bool ok = scratch->update_v3
8566 				? update_planes_and_stream_prepare_v3(scratch)
8567 				: update_planes_and_stream_prepare_v2(scratch);
8568 
8569 		if (!ok)
8570 			goto release_scratch;
8571 	}
8572 
8573 	if (!dc_update_probes_prepare(scratch))
8574 		goto release_scratch;
8575 
8576 	return true;
8577 
8578 release_scratch:
8579 	/* execute and cleanup never run on this path, so release here. */
8580 	dc_update_scratch_release(scratch->dc, scratch);
8581 	return false;
8582 }
8583 
dc_update_state_execute(const struct dc_update_scratch_space * scratch)8584 void dc_update_state_execute(
8585 		const struct dc_update_scratch_space *scratch
8586 )
8587 {
8588 	if (scratch->stream)
8589 		scratch->update_v3
8590 				? update_planes_and_stream_execute_v3(scratch)
8591 				: update_planes_and_stream_execute_v2(scratch);
8592 
8593 	if (scratch->probe_updates)
8594 		dc_update_probes_execute(scratch);
8595 }
8596 
dc_update_state_cleanup(struct dc_update_scratch_space * scratch)8597 bool dc_update_state_cleanup(
8598 		struct dc_update_scratch_space *scratch
8599 )
8600 {
8601 	bool more = false;
8602 
8603 	if (scratch->stream)
8604 		more = scratch->update_v3
8605 				? update_planes_and_stream_cleanup_v3(scratch)
8606 				: update_planes_and_stream_cleanup_v2(scratch);
8607 
8608 	if (!more)
8609 		dc_update_scratch_release(scratch->dc, scratch);
8610 
8611 	return more;
8612 }
8613 
8614