• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright 2015 Advanced Micro Devices, Inc.
3  *
4  * Permission is hereby granted, free of charge, to any person obtaining a
5  * copy of this software and associated documentation files (the "Software"),
6  * to deal in the Software without restriction, including without limitation
7  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8  * and/or sell copies of the Software, and to permit persons to whom the
9  * Software is furnished to do so, subject to the following conditions:
10  *
11  * The above copyright notice and this permission notice shall be included in
12  * all copies or substantial portions of the Software.
13  *
14  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
17  * THE COPYRIGHT HOLDER(S) OR AUTHOR(S) BE LIABLE FOR ANY CLAIM, DAMAGES OR
18  * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
19  * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
20  * OTHER DEALINGS IN THE SOFTWARE.
21  *
22  * Authors: AMD
23  */
24 
25 #include "dm_services.h"
26 
27 #include "amdgpu.h"
28 
29 #include "dc.h"
30 
31 #include "core_status.h"
32 #include "core_types.h"
33 #include "hw_sequencer.h"
34 #include "dce/dce_hwseq.h"
35 
36 #include "resource.h"
37 #include "dc_state.h"
38 #include "dc_state_priv.h"
39 #include "dc_plane_priv.h"
40 
41 #include "gpio_service_interface.h"
42 #include "clk_mgr.h"
43 #include "clock_source.h"
44 #include "dc_bios_types.h"
45 
46 #include "bios_parser_interface.h"
47 #include "bios/bios_parser_helper.h"
48 #include "include/irq_service_interface.h"
49 #include "transform.h"
50 #include "dmcu.h"
51 #include "dpp.h"
52 #include "timing_generator.h"
53 #include "abm.h"
54 #include "virtual/virtual_link_encoder.h"
55 #include "hubp.h"
56 
57 #include "link_hwss.h"
58 #include "link_encoder.h"
59 #include "link_enc_cfg.h"
60 
61 #include "link.h"
62 #include "dm_helpers.h"
63 #include "mem_input.h"
64 
65 #include "dc_dmub_srv.h"
66 
67 #include "dsc.h"
68 
69 #include "vm_helper.h"
70 
71 #include "dce/dce_i2c.h"
72 
73 #include "dmub/dmub_srv.h"
74 
75 #include "dce/dmub_psr.h"
76 
77 #include "dce/dmub_hw_lock_mgr.h"
78 
79 #include "dc_trace.h"
80 
81 #include "hw_sequencer_private.h"
82 
83 #if defined(CONFIG_DRM_AMD_DC_FP)
84 #include "dml2/dml2_internal_types.h"
85 #endif
86 
87 #include "dce/dmub_outbox.h"
88 
89 #define CTX \
90 	dc->ctx
91 
92 #define DC_LOGGER \
93 	dc->ctx->logger
94 
95 static const char DC_BUILD_ID[] = "production-build";
96 
97 /**
98  * DOC: Overview
99  *
100  * DC is the OS-agnostic component of the amdgpu DC driver.
101  *
102  * DC maintains and validates a set of structs representing the state of the
103  * driver and writes that state to AMD hardware
104  *
105  * Main DC HW structs:
106  *
107  * struct dc - The central struct.  One per driver.  Created on driver load,
108  * destroyed on driver unload.
109  *
110  * struct dc_context - One per driver.
111  * Used as a backpointer by most other structs in dc.
112  *
113  * struct dc_link - One per connector (the physical DP, HDMI, miniDP, or eDP
114  * plugpoints).  Created on driver load, destroyed on driver unload.
115  *
116  * struct dc_sink - One per display.  Created on boot or hotplug.
117  * Destroyed on shutdown or hotunplug.  A dc_link can have a local sink
118  * (the display directly attached).  It may also have one or more remote
119  * sinks (in the Multi-Stream Transport case)
120  *
121  * struct resource_pool - One per driver.  Represents the hw blocks not in the
122  * main pipeline.  Not directly accessible by dm.
123  *
124  * Main dc state structs:
125  *
126  * These structs can be created and destroyed as needed.  There is a full set of
127  * these structs in dc->current_state representing the currently programmed state.
128  *
129  * struct dc_state - The global DC state to track global state information,
130  * such as bandwidth values.
131  *
132  * struct dc_stream_state - Represents the hw configuration for the pipeline from
133  * a framebuffer to a display.  Maps one-to-one with dc_sink.
134  *
135  * struct dc_plane_state - Represents a framebuffer.  Each stream has at least one,
136  * and may have more in the Multi-Plane Overlay case.
137  *
138  * struct resource_context - Represents the programmable state of everything in
139  * the resource_pool.  Not directly accessible by dm.
140  *
141  * struct pipe_ctx - A member of struct resource_context.  Represents the
142  * internal hardware pipeline components.  Each dc_plane_state has either
143  * one or two (in the pipe-split case).
144  */
145 
146 /* Private functions */
147 
elevate_update_type(enum surface_update_type * original,enum surface_update_type new)148 static inline void elevate_update_type(enum surface_update_type *original, enum surface_update_type new)
149 {
150 	if (new > *original)
151 		*original = new;
152 }
153 
destroy_links(struct dc * dc)154 static void destroy_links(struct dc *dc)
155 {
156 	uint32_t i;
157 
158 	for (i = 0; i < dc->link_count; i++) {
159 		if (NULL != dc->links[i])
160 			dc->link_srv->destroy_link(&dc->links[i]);
161 	}
162 }
163 
get_num_of_internal_disp(struct dc_link ** links,uint32_t num_links)164 static uint32_t get_num_of_internal_disp(struct dc_link **links, uint32_t num_links)
165 {
166 	int i;
167 	uint32_t count = 0;
168 
169 	for (i = 0; i < num_links; i++) {
170 		if (links[i]->connector_signal == SIGNAL_TYPE_EDP ||
171 				links[i]->is_internal_display)
172 			count++;
173 	}
174 
175 	return count;
176 }
177 
get_seamless_boot_stream_count(struct dc_state * ctx)178 static int get_seamless_boot_stream_count(struct dc_state *ctx)
179 {
180 	uint8_t i;
181 	uint8_t seamless_boot_stream_count = 0;
182 
183 	for (i = 0; i < ctx->stream_count; i++)
184 		if (ctx->streams[i]->apply_seamless_boot_optimization)
185 			seamless_boot_stream_count++;
186 
187 	return seamless_boot_stream_count;
188 }
189 
create_links(struct dc * dc,uint32_t num_virtual_links)190 static bool create_links(
191 		struct dc *dc,
192 		uint32_t num_virtual_links)
193 {
194 	int i;
195 	int connectors_num;
196 	struct dc_bios *bios = dc->ctx->dc_bios;
197 
198 	dc->link_count = 0;
199 
200 	connectors_num = bios->funcs->get_connectors_number(bios);
201 
202 	DC_LOG_DC("BIOS object table - number of connectors: %d", connectors_num);
203 
204 	if (connectors_num > ENUM_ID_COUNT) {
205 		dm_error(
206 			"DC: Number of connectors %d exceeds maximum of %d!\n",
207 			connectors_num,
208 			ENUM_ID_COUNT);
209 		return false;
210 	}
211 
212 	dm_output_to_console(
213 		"DC: %s: connectors_num: physical:%d, virtual:%d\n",
214 		__func__,
215 		connectors_num,
216 		num_virtual_links);
217 
218 	/* When getting the number of connectors, the VBIOS reports the number of valid indices,
219 	 * but it doesn't say which indices are valid, and not every index has an actual connector.
220 	 * So, if we don't find a connector on an index, that is not an error.
221 	 *
222 	 * - There is no guarantee that the first N indices will be valid
223 	 * - VBIOS may report a higher amount of valid indices than there are actual connectors
224 	 * - Some VBIOS have valid configurations for more connectors than there actually are
225 	 *   on the card. This may be because the manufacturer used the same VBIOS for different
226 	 *   variants of the same card.
227 	 */
228 	for (i = 0; dc->link_count < connectors_num && i < MAX_LINKS; i++) {
229 		struct graphics_object_id connector_id = bios->funcs->get_connector_id(bios, i);
230 		struct link_init_data link_init_params = {0};
231 		struct dc_link *link;
232 
233 		if (connector_id.id == CONNECTOR_ID_UNKNOWN)
234 			continue;
235 
236 		DC_LOG_DC("BIOS object table - printing link object info for connector number: %d, link_index: %d", i, dc->link_count);
237 
238 		link_init_params.ctx = dc->ctx;
239 		/* next BIOS object table connector */
240 		link_init_params.connector_index = i;
241 		link_init_params.link_index = dc->link_count;
242 		link_init_params.dc = dc;
243 		link = dc->link_srv->create_link(&link_init_params);
244 
245 		if (link) {
246 			dc->links[dc->link_count] = link;
247 			link->dc = dc;
248 			++dc->link_count;
249 		}
250 	}
251 
252 	DC_LOG_DC("BIOS object table - end");
253 
254 	/* Create a link for each usb4 dpia port */
255 	for (i = 0; i < dc->res_pool->usb4_dpia_count; i++) {
256 		struct link_init_data link_init_params = {0};
257 		struct dc_link *link;
258 
259 		link_init_params.ctx = dc->ctx;
260 		link_init_params.connector_index = i;
261 		link_init_params.link_index = dc->link_count;
262 		link_init_params.dc = dc;
263 		link_init_params.is_dpia_link = true;
264 
265 		link = dc->link_srv->create_link(&link_init_params);
266 		if (link) {
267 			dc->links[dc->link_count] = link;
268 			link->dc = dc;
269 			++dc->link_count;
270 		}
271 	}
272 
273 	for (i = 0; i < num_virtual_links; i++) {
274 		struct dc_link *link = kzalloc(sizeof(*link), GFP_KERNEL);
275 		struct encoder_init_data enc_init = {0};
276 
277 		if (link == NULL) {
278 			BREAK_TO_DEBUGGER();
279 			goto failed_alloc;
280 		}
281 
282 		link->link_index = dc->link_count;
283 		dc->links[dc->link_count] = link;
284 		dc->link_count++;
285 
286 		link->ctx = dc->ctx;
287 		link->dc = dc;
288 		link->connector_signal = SIGNAL_TYPE_VIRTUAL;
289 		link->link_id.type = OBJECT_TYPE_CONNECTOR;
290 		link->link_id.id = CONNECTOR_ID_VIRTUAL;
291 		link->link_id.enum_id = ENUM_ID_1;
292 		link->psr_settings.psr_version = DC_PSR_VERSION_UNSUPPORTED;
293 		link->link_enc = kzalloc(sizeof(*link->link_enc), GFP_KERNEL);
294 
295 		if (!link->link_enc) {
296 			BREAK_TO_DEBUGGER();
297 			goto failed_alloc;
298 		}
299 
300 		link->link_status.dpcd_caps = &link->dpcd_caps;
301 
302 		enc_init.ctx = dc->ctx;
303 		enc_init.channel = CHANNEL_ID_UNKNOWN;
304 		enc_init.hpd_source = HPD_SOURCEID_UNKNOWN;
305 		enc_init.transmitter = TRANSMITTER_UNKNOWN;
306 		enc_init.connector = link->link_id;
307 		enc_init.encoder.type = OBJECT_TYPE_ENCODER;
308 		enc_init.encoder.id = ENCODER_ID_INTERNAL_VIRTUAL;
309 		enc_init.encoder.enum_id = ENUM_ID_1;
310 		virtual_link_encoder_construct(link->link_enc, &enc_init);
311 	}
312 
313 	dc->caps.num_of_internal_disp = get_num_of_internal_disp(dc->links, dc->link_count);
314 
315 	return true;
316 
317 failed_alloc:
318 	return false;
319 }
320 
321 /* Create additional DIG link encoder objects if fewer than the platform
322  * supports were created during link construction. This can happen if the
323  * number of physical connectors is less than the number of DIGs.
324  */
create_link_encoders(struct dc * dc)325 static bool create_link_encoders(struct dc *dc)
326 {
327 	bool res = true;
328 	unsigned int num_usb4_dpia = dc->res_pool->res_cap->num_usb4_dpia;
329 	unsigned int num_dig_link_enc = dc->res_pool->res_cap->num_dig_link_enc;
330 	int i;
331 
332 	/* A platform without USB4 DPIA endpoints has a fixed mapping between DIG
333 	 * link encoders and physical display endpoints and does not require
334 	 * additional link encoder objects.
335 	 */
336 	if (num_usb4_dpia == 0)
337 		return res;
338 
339 	/* Create as many link encoder objects as the platform supports. DPIA
340 	 * endpoints can be programmably mapped to any DIG.
341 	 */
342 	if (num_dig_link_enc > dc->res_pool->dig_link_enc_count) {
343 		for (i = 0; i < num_dig_link_enc; i++) {
344 			struct link_encoder *link_enc = dc->res_pool->link_encoders[i];
345 
346 			if (!link_enc && dc->res_pool->funcs->link_enc_create_minimal) {
347 				link_enc = dc->res_pool->funcs->link_enc_create_minimal(dc->ctx,
348 						(enum engine_id)(ENGINE_ID_DIGA + i));
349 				if (link_enc) {
350 					dc->res_pool->link_encoders[i] = link_enc;
351 					dc->res_pool->dig_link_enc_count++;
352 				} else {
353 					res = false;
354 				}
355 			}
356 		}
357 	}
358 
359 	return res;
360 }
361 
362 /* Destroy any additional DIG link encoder objects created by
363  * create_link_encoders().
364  * NB: Must only be called after destroy_links().
365  */
destroy_link_encoders(struct dc * dc)366 static void destroy_link_encoders(struct dc *dc)
367 {
368 	unsigned int num_usb4_dpia;
369 	unsigned int num_dig_link_enc;
370 	int i;
371 
372 	if (!dc->res_pool)
373 		return;
374 
375 	num_usb4_dpia = dc->res_pool->res_cap->num_usb4_dpia;
376 	num_dig_link_enc = dc->res_pool->res_cap->num_dig_link_enc;
377 
378 	/* A platform without USB4 DPIA endpoints has a fixed mapping between DIG
379 	 * link encoders and physical display endpoints and does not require
380 	 * additional link encoder objects.
381 	 */
382 	if (num_usb4_dpia == 0)
383 		return;
384 
385 	for (i = 0; i < num_dig_link_enc; i++) {
386 		struct link_encoder *link_enc = dc->res_pool->link_encoders[i];
387 
388 		if (link_enc) {
389 			link_enc->funcs->destroy(&link_enc);
390 			dc->res_pool->link_encoders[i] = NULL;
391 			dc->res_pool->dig_link_enc_count--;
392 		}
393 	}
394 }
395 
dc_perf_trace_create(void)396 static struct dc_perf_trace *dc_perf_trace_create(void)
397 {
398 	return kzalloc(sizeof(struct dc_perf_trace), GFP_KERNEL);
399 }
400 
dc_perf_trace_destroy(struct dc_perf_trace ** perf_trace)401 static void dc_perf_trace_destroy(struct dc_perf_trace **perf_trace)
402 {
403 	kfree(*perf_trace);
404 	*perf_trace = NULL;
405 }
406 
set_long_vtotal(struct dc * dc,struct dc_stream_state * stream,struct dc_crtc_timing_adjust * adjust)407 static bool set_long_vtotal(struct dc *dc, struct dc_stream_state *stream, struct dc_crtc_timing_adjust *adjust)
408 {
409 	if (!dc || !stream || !adjust)
410 		return false;
411 
412 	if (!dc->current_state)
413 		return false;
414 
415 	int i;
416 
417 	for (i = 0; i < MAX_PIPES; i++) {
418 		struct pipe_ctx *pipe = &dc->current_state->res_ctx.pipe_ctx[i];
419 
420 		if (pipe->stream == stream && pipe->stream_res.tg) {
421 			if (dc->hwss.set_long_vtotal)
422 				dc->hwss.set_long_vtotal(&pipe, 1, adjust->v_total_min, adjust->v_total_max);
423 
424 			return true;
425 		}
426 	}
427 
428 	return false;
429 }
430 
431 /**
432  *  dc_stream_adjust_vmin_vmax - look up pipe context & update parts of DRR
433  *  @dc:     dc reference
434  *  @stream: Initial dc stream state
435  *  @adjust: Updated parameters for vertical_total_min and vertical_total_max
436  *
437  *  Looks up the pipe context of dc_stream_state and updates the
438  *  vertical_total_min and vertical_total_max of the DRR, Dynamic Refresh
439  *  Rate, which is a power-saving feature that targets reducing panel
440  *  refresh rate while the screen is static
441  *
442  *  Return: %true if the pipe context is found and adjusted;
443  *          %false if the pipe context is not found.
444  */
dc_stream_adjust_vmin_vmax(struct dc * dc,struct dc_stream_state * stream,struct dc_crtc_timing_adjust * adjust)445 bool dc_stream_adjust_vmin_vmax(struct dc *dc,
446 		struct dc_stream_state *stream,
447 		struct dc_crtc_timing_adjust *adjust)
448 {
449 	int i;
450 
451 	/*
452 	 * Don't adjust DRR while there's bandwidth optimizations pending to
453 	 * avoid conflicting with firmware updates.
454 	 */
455 	if (dc->ctx->dce_version > DCE_VERSION_MAX) {
456 		if (dc->optimized_required || dc->wm_optimized_required) {
457 			stream->adjust.timing_adjust_pending = true;
458 			return false;
459 		}
460 	}
461 
462 	dc_exit_ips_for_hw_access(dc);
463 
464 	stream->adjust.v_total_max = adjust->v_total_max;
465 	stream->adjust.v_total_mid = adjust->v_total_mid;
466 	stream->adjust.v_total_mid_frame_num = adjust->v_total_mid_frame_num;
467 	stream->adjust.v_total_min = adjust->v_total_min;
468 	stream->adjust.allow_otg_v_count_halt = adjust->allow_otg_v_count_halt;
469 
470 	if (dc->caps.max_v_total != 0 &&
471 		(adjust->v_total_max > dc->caps.max_v_total || adjust->v_total_min > dc->caps.max_v_total)) {
472 		stream->adjust.timing_adjust_pending = false;
473 		if (adjust->allow_otg_v_count_halt)
474 			return set_long_vtotal(dc, stream, adjust);
475 		else
476 			return false;
477 	}
478 
479 	for (i = 0; i < MAX_PIPES; i++) {
480 		struct pipe_ctx *pipe = &dc->current_state->res_ctx.pipe_ctx[i];
481 
482 		if (pipe->stream == stream && pipe->stream_res.tg) {
483 			dc->hwss.set_drr(&pipe,
484 					1,
485 					*adjust);
486 			stream->adjust.timing_adjust_pending = false;
487 			return true;
488 		}
489 	}
490 	return false;
491 }
492 
493 /**
494  * dc_stream_get_last_used_drr_vtotal - Looks up the pipe context of
495  * dc_stream_state and gets the last VTOTAL used by DRR (Dynamic Refresh Rate)
496  *
497  * @dc: [in] dc reference
498  * @stream: [in] Initial dc stream state
499  * @refresh_rate: [in] new refresh_rate
500  *
501  * Return: %true if the pipe context is found and there is an associated
502  *         timing_generator for the DC;
503  *         %false if the pipe context is not found or there is no
504  *         timing_generator for the DC.
505  */
dc_stream_get_last_used_drr_vtotal(struct dc * dc,struct dc_stream_state * stream,uint32_t * refresh_rate)506 bool dc_stream_get_last_used_drr_vtotal(struct dc *dc,
507 		struct dc_stream_state *stream,
508 		uint32_t *refresh_rate)
509 {
510 	bool status = false;
511 
512 	int i = 0;
513 
514 	dc_exit_ips_for_hw_access(dc);
515 
516 	for (i = 0; i < MAX_PIPES; i++) {
517 		struct pipe_ctx *pipe = &dc->current_state->res_ctx.pipe_ctx[i];
518 
519 		if (pipe->stream == stream && pipe->stream_res.tg) {
520 			/* Only execute if a function pointer has been defined for
521 			 * the DC version in question
522 			 */
523 			if (pipe->stream_res.tg->funcs->get_last_used_drr_vtotal) {
524 				pipe->stream_res.tg->funcs->get_last_used_drr_vtotal(pipe->stream_res.tg, refresh_rate);
525 
526 				status = true;
527 
528 				break;
529 			}
530 		}
531 	}
532 
533 	return status;
534 }
535 
dc_stream_get_crtc_position(struct dc * dc,struct dc_stream_state ** streams,int num_streams,unsigned int * v_pos,unsigned int * nom_v_pos)536 bool dc_stream_get_crtc_position(struct dc *dc,
537 		struct dc_stream_state **streams, int num_streams,
538 		unsigned int *v_pos, unsigned int *nom_v_pos)
539 {
540 	/* TODO: Support multiple streams */
541 	const struct dc_stream_state *stream = streams[0];
542 	int i;
543 	bool ret = false;
544 	struct crtc_position position;
545 
546 	dc_exit_ips_for_hw_access(dc);
547 
548 	for (i = 0; i < MAX_PIPES; i++) {
549 		struct pipe_ctx *pipe =
550 				&dc->current_state->res_ctx.pipe_ctx[i];
551 
552 		if (pipe->stream == stream && pipe->stream_res.stream_enc) {
553 			dc->hwss.get_position(&pipe, 1, &position);
554 
555 			*v_pos = position.vertical_count;
556 			*nom_v_pos = position.nominal_vcount;
557 			ret = true;
558 		}
559 	}
560 	return ret;
561 }
562 
563 #if defined(CONFIG_DRM_AMD_SECURE_DISPLAY)
564 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)565 dc_stream_forward_dmub_crc_window(struct dc_dmub_srv *dmub_srv,
566 		struct rect *rect, struct otg_phy_mux *mux_mapping, bool is_stop)
567 {
568 	union dmub_rb_cmd cmd = {0};
569 
570 	cmd.secure_display.roi_info.phy_id = mux_mapping->phy_output_num;
571 	cmd.secure_display.roi_info.otg_id = mux_mapping->otg_output_num;
572 
573 	if (is_stop) {
574 		cmd.secure_display.header.type = DMUB_CMD__SECURE_DISPLAY;
575 		cmd.secure_display.header.sub_type = DMUB_CMD__SECURE_DISPLAY_CRC_STOP_UPDATE;
576 	} else {
577 		cmd.secure_display.header.type = DMUB_CMD__SECURE_DISPLAY;
578 		cmd.secure_display.header.sub_type = DMUB_CMD__SECURE_DISPLAY_CRC_WIN_NOTIFY;
579 		cmd.secure_display.roi_info.x_start = rect->x;
580 		cmd.secure_display.roi_info.y_start = rect->y;
581 		cmd.secure_display.roi_info.x_end = rect->x + rect->width;
582 		cmd.secure_display.roi_info.y_end = rect->y + rect->height;
583 	}
584 
585 	dc_wake_and_execute_dmub_cmd(dmub_srv->ctx, &cmd, DM_DMUB_WAIT_TYPE_NO_WAIT);
586 }
587 
588 static inline void
dc_stream_forward_dmcu_crc_window(struct dmcu * dmcu,struct rect * rect,struct otg_phy_mux * mux_mapping,bool is_stop)589 dc_stream_forward_dmcu_crc_window(struct dmcu *dmcu,
590 		struct rect *rect, struct otg_phy_mux *mux_mapping, bool is_stop)
591 {
592 	if (is_stop)
593 		dmcu->funcs->stop_crc_win_update(dmcu, mux_mapping);
594 	else
595 		dmcu->funcs->forward_crc_window(dmcu, rect, mux_mapping);
596 }
597 
598 bool
dc_stream_forward_crc_window(struct dc_stream_state * stream,struct rect * rect,bool is_stop)599 dc_stream_forward_crc_window(struct dc_stream_state *stream,
600 		struct rect *rect, bool is_stop)
601 {
602 	struct dmcu *dmcu;
603 	struct dc_dmub_srv *dmub_srv;
604 	struct otg_phy_mux mux_mapping;
605 	struct pipe_ctx *pipe;
606 	int i;
607 	struct dc *dc = stream->ctx->dc;
608 
609 	for (i = 0; i < MAX_PIPES; i++) {
610 		pipe = &dc->current_state->res_ctx.pipe_ctx[i];
611 		if (pipe->stream == stream && !pipe->top_pipe && !pipe->prev_odm_pipe)
612 			break;
613 	}
614 
615 	/* Stream not found */
616 	if (i == MAX_PIPES)
617 		return false;
618 
619 	mux_mapping.phy_output_num = stream->link->link_enc_hw_inst;
620 	mux_mapping.otg_output_num = pipe->stream_res.tg->inst;
621 
622 	dmcu = dc->res_pool->dmcu;
623 	dmub_srv = dc->ctx->dmub_srv;
624 
625 	/* forward to dmub */
626 	if (dmub_srv)
627 		dc_stream_forward_dmub_crc_window(dmub_srv, rect, &mux_mapping, is_stop);
628 	/* forward to dmcu */
629 	else if (dmcu && dmcu->funcs->is_dmcu_initialized(dmcu))
630 		dc_stream_forward_dmcu_crc_window(dmcu, rect, &mux_mapping, is_stop);
631 	else
632 		return false;
633 
634 	return true;
635 }
636 #endif /* CONFIG_DRM_AMD_SECURE_DISPLAY */
637 
638 /**
639  * dc_stream_configure_crc() - Configure CRC capture for the given stream.
640  * @dc: DC Object
641  * @stream: The stream to configure CRC on.
642  * @enable: Enable CRC if true, disable otherwise.
643  * @crc_window: CRC window (x/y start/end) information
644  * @continuous: Capture CRC on every frame if true. Otherwise, only capture
645  *              once.
646  *
647  * By default, only CRC0 is configured, and the entire frame is used to
648  * calculate the CRC.
649  *
650  * Return: %false if the stream is not found or CRC capture is not supported;
651  *         %true if the stream has been configured.
652  */
dc_stream_configure_crc(struct dc * dc,struct dc_stream_state * stream,struct crc_params * crc_window,bool enable,bool continuous)653 bool dc_stream_configure_crc(struct dc *dc, struct dc_stream_state *stream,
654 			     struct crc_params *crc_window, bool enable, bool continuous)
655 {
656 	struct pipe_ctx *pipe;
657 	struct crc_params param;
658 	struct timing_generator *tg;
659 
660 	pipe = resource_get_otg_master_for_stream(
661 			&dc->current_state->res_ctx, stream);
662 
663 	/* Stream not found */
664 	if (pipe == NULL)
665 		return false;
666 
667 	dc_exit_ips_for_hw_access(dc);
668 
669 	/* By default, capture the full frame */
670 	param.windowa_x_start = 0;
671 	param.windowa_y_start = 0;
672 	param.windowa_x_end = pipe->stream->timing.h_addressable;
673 	param.windowa_y_end = pipe->stream->timing.v_addressable;
674 	param.windowb_x_start = 0;
675 	param.windowb_y_start = 0;
676 	param.windowb_x_end = pipe->stream->timing.h_addressable;
677 	param.windowb_y_end = pipe->stream->timing.v_addressable;
678 
679 	if (crc_window) {
680 		param.windowa_x_start = crc_window->windowa_x_start;
681 		param.windowa_y_start = crc_window->windowa_y_start;
682 		param.windowa_x_end = crc_window->windowa_x_end;
683 		param.windowa_y_end = crc_window->windowa_y_end;
684 		param.windowb_x_start = crc_window->windowb_x_start;
685 		param.windowb_y_start = crc_window->windowb_y_start;
686 		param.windowb_x_end = crc_window->windowb_x_end;
687 		param.windowb_y_end = crc_window->windowb_y_end;
688 	}
689 
690 	param.dsc_mode = pipe->stream->timing.flags.DSC ? 1:0;
691 	param.odm_mode = pipe->next_odm_pipe ? 1:0;
692 
693 	/* Default to the union of both windows */
694 	param.selection = UNION_WINDOW_A_B;
695 	param.continuous_mode = continuous;
696 	param.enable = enable;
697 
698 	tg = pipe->stream_res.tg;
699 
700 	/* Only call if supported */
701 	if (tg->funcs->configure_crc)
702 		return tg->funcs->configure_crc(tg, &param);
703 	DC_LOG_WARNING("CRC capture not supported.");
704 	return false;
705 }
706 
707 /**
708  * dc_stream_get_crc() - Get CRC values for the given stream.
709  *
710  * @dc: DC object.
711  * @stream: The DC stream state of the stream to get CRCs from.
712  * @r_cr: CRC value for the red component.
713  * @g_y:  CRC value for the green component.
714  * @b_cb: CRC value for the blue component.
715  *
716  * dc_stream_configure_crc needs to be called beforehand to enable CRCs.
717  *
718  * Return:
719  * %false if stream is not found, or if CRCs are not enabled.
720  */
dc_stream_get_crc(struct dc * dc,struct dc_stream_state * stream,uint32_t * r_cr,uint32_t * g_y,uint32_t * b_cb)721 bool dc_stream_get_crc(struct dc *dc, struct dc_stream_state *stream,
722 		       uint32_t *r_cr, uint32_t *g_y, uint32_t *b_cb)
723 {
724 	int i;
725 	struct pipe_ctx *pipe;
726 	struct timing_generator *tg;
727 
728 	dc_exit_ips_for_hw_access(dc);
729 
730 	for (i = 0; i < MAX_PIPES; i++) {
731 		pipe = &dc->current_state->res_ctx.pipe_ctx[i];
732 		if (pipe->stream == stream)
733 			break;
734 	}
735 	/* Stream not found */
736 	if (i == MAX_PIPES)
737 		return false;
738 
739 	tg = pipe->stream_res.tg;
740 
741 	if (tg->funcs->get_crc)
742 		return tg->funcs->get_crc(tg, r_cr, g_y, b_cb);
743 	DC_LOG_WARNING("CRC capture not supported.");
744 	return false;
745 }
746 
dc_stream_set_dyn_expansion(struct dc * dc,struct dc_stream_state * stream,enum dc_dynamic_expansion option)747 void dc_stream_set_dyn_expansion(struct dc *dc, struct dc_stream_state *stream,
748 		enum dc_dynamic_expansion option)
749 {
750 	/* OPP FMT dyn expansion updates*/
751 	int i;
752 	struct pipe_ctx *pipe_ctx;
753 
754 	dc_exit_ips_for_hw_access(dc);
755 
756 	for (i = 0; i < MAX_PIPES; i++) {
757 		if (dc->current_state->res_ctx.pipe_ctx[i].stream
758 				== stream) {
759 			pipe_ctx = &dc->current_state->res_ctx.pipe_ctx[i];
760 			pipe_ctx->stream_res.opp->dyn_expansion = option;
761 			pipe_ctx->stream_res.opp->funcs->opp_set_dyn_expansion(
762 					pipe_ctx->stream_res.opp,
763 					COLOR_SPACE_YCBCR601,
764 					stream->timing.display_color_depth,
765 					stream->signal);
766 		}
767 	}
768 }
769 
dc_stream_set_dither_option(struct dc_stream_state * stream,enum dc_dither_option option)770 void dc_stream_set_dither_option(struct dc_stream_state *stream,
771 		enum dc_dither_option option)
772 {
773 	struct bit_depth_reduction_params params;
774 	struct dc_link *link = stream->link;
775 	struct pipe_ctx *pipes = NULL;
776 	int i;
777 
778 	for (i = 0; i < MAX_PIPES; i++) {
779 		if (link->dc->current_state->res_ctx.pipe_ctx[i].stream ==
780 				stream) {
781 			pipes = &link->dc->current_state->res_ctx.pipe_ctx[i];
782 			break;
783 		}
784 	}
785 
786 	if (!pipes)
787 		return;
788 	if (option > DITHER_OPTION_MAX)
789 		return;
790 
791 	dc_exit_ips_for_hw_access(stream->ctx->dc);
792 
793 	stream->dither_option = option;
794 
795 	memset(&params, 0, sizeof(params));
796 	resource_build_bit_depth_reduction_params(stream, &params);
797 	stream->bit_depth_params = params;
798 
799 	if (pipes->plane_res.xfm &&
800 	    pipes->plane_res.xfm->funcs->transform_set_pixel_storage_depth) {
801 		pipes->plane_res.xfm->funcs->transform_set_pixel_storage_depth(
802 			pipes->plane_res.xfm,
803 			pipes->plane_res.scl_data.lb_params.depth,
804 			&stream->bit_depth_params);
805 	}
806 
807 	pipes->stream_res.opp->funcs->
808 		opp_program_bit_depth_reduction(pipes->stream_res.opp, &params);
809 }
810 
dc_stream_set_gamut_remap(struct dc * dc,const struct dc_stream_state * stream)811 bool dc_stream_set_gamut_remap(struct dc *dc, const struct dc_stream_state *stream)
812 {
813 	int i;
814 	bool ret = false;
815 	struct pipe_ctx *pipes;
816 
817 	dc_exit_ips_for_hw_access(dc);
818 
819 	for (i = 0; i < MAX_PIPES; i++) {
820 		if (dc->current_state->res_ctx.pipe_ctx[i].stream == stream) {
821 			pipes = &dc->current_state->res_ctx.pipe_ctx[i];
822 			dc->hwss.program_gamut_remap(pipes);
823 			ret = true;
824 		}
825 	}
826 
827 	return ret;
828 }
829 
dc_stream_program_csc_matrix(struct dc * dc,struct dc_stream_state * stream)830 bool dc_stream_program_csc_matrix(struct dc *dc, struct dc_stream_state *stream)
831 {
832 	int i;
833 	bool ret = false;
834 	struct pipe_ctx *pipes;
835 
836 	dc_exit_ips_for_hw_access(dc);
837 
838 	for (i = 0; i < MAX_PIPES; i++) {
839 		if (dc->current_state->res_ctx.pipe_ctx[i].stream
840 				== stream) {
841 
842 			pipes = &dc->current_state->res_ctx.pipe_ctx[i];
843 			dc->hwss.program_output_csc(dc,
844 					pipes,
845 					stream->output_color_space,
846 					stream->csc_color_matrix.matrix,
847 					pipes->stream_res.opp->inst);
848 			ret = true;
849 		}
850 	}
851 
852 	return ret;
853 }
854 
dc_stream_set_static_screen_params(struct dc * dc,struct dc_stream_state ** streams,int num_streams,const struct dc_static_screen_params * params)855 void dc_stream_set_static_screen_params(struct dc *dc,
856 		struct dc_stream_state **streams,
857 		int num_streams,
858 		const struct dc_static_screen_params *params)
859 {
860 	int i, j;
861 	struct pipe_ctx *pipes_affected[MAX_PIPES];
862 	int num_pipes_affected = 0;
863 
864 	dc_exit_ips_for_hw_access(dc);
865 
866 	for (i = 0; i < num_streams; i++) {
867 		struct dc_stream_state *stream = streams[i];
868 
869 		for (j = 0; j < MAX_PIPES; j++) {
870 			if (dc->current_state->res_ctx.pipe_ctx[j].stream
871 					== stream) {
872 				pipes_affected[num_pipes_affected++] =
873 						&dc->current_state->res_ctx.pipe_ctx[j];
874 			}
875 		}
876 	}
877 
878 	dc->hwss.set_static_screen_control(pipes_affected, num_pipes_affected, params);
879 }
880 
dc_destruct(struct dc * dc)881 static void dc_destruct(struct dc *dc)
882 {
883 	// reset link encoder assignment table on destruct
884 	if (dc->res_pool && dc->res_pool->funcs->link_encs_assign)
885 		link_enc_cfg_init(dc, dc->current_state);
886 
887 	if (dc->current_state) {
888 		dc_state_release(dc->current_state);
889 		dc->current_state = NULL;
890 	}
891 
892 	destroy_links(dc);
893 
894 	destroy_link_encoders(dc);
895 
896 	if (dc->clk_mgr) {
897 		dc_destroy_clk_mgr(dc->clk_mgr);
898 		dc->clk_mgr = NULL;
899 	}
900 
901 	dc_destroy_resource_pool(dc);
902 
903 	if (dc->link_srv)
904 		link_destroy_link_service(&dc->link_srv);
905 
906 	if (dc->ctx) {
907 		if (dc->ctx->gpio_service)
908 			dal_gpio_service_destroy(&dc->ctx->gpio_service);
909 
910 		if (dc->ctx->created_bios)
911 			dal_bios_parser_destroy(&dc->ctx->dc_bios);
912 		kfree(dc->ctx->logger);
913 		dc_perf_trace_destroy(&dc->ctx->perf_trace);
914 
915 		kfree(dc->ctx);
916 		dc->ctx = NULL;
917 	}
918 
919 	kfree(dc->bw_vbios);
920 	dc->bw_vbios = NULL;
921 
922 	kfree(dc->bw_dceip);
923 	dc->bw_dceip = NULL;
924 
925 	kfree(dc->dcn_soc);
926 	dc->dcn_soc = NULL;
927 
928 	kfree(dc->dcn_ip);
929 	dc->dcn_ip = NULL;
930 
931 	kfree(dc->vm_helper);
932 	dc->vm_helper = NULL;
933 
934 }
935 
dc_construct_ctx(struct dc * dc,const struct dc_init_data * init_params)936 static bool dc_construct_ctx(struct dc *dc,
937 		const struct dc_init_data *init_params)
938 {
939 	struct dc_context *dc_ctx;
940 
941 	dc_ctx = kzalloc(sizeof(*dc_ctx), GFP_KERNEL);
942 	if (!dc_ctx)
943 		return false;
944 
945 	dc_ctx->cgs_device = init_params->cgs_device;
946 	dc_ctx->driver_context = init_params->driver;
947 	dc_ctx->dc = dc;
948 	dc_ctx->asic_id = init_params->asic_id;
949 	dc_ctx->dc_sink_id_count = 0;
950 	dc_ctx->dc_stream_id_count = 0;
951 	dc_ctx->dce_environment = init_params->dce_environment;
952 	dc_ctx->dcn_reg_offsets = init_params->dcn_reg_offsets;
953 	dc_ctx->nbio_reg_offsets = init_params->nbio_reg_offsets;
954 	dc_ctx->clk_reg_offsets = init_params->clk_reg_offsets;
955 
956 	/* Create logger */
957 	dc_ctx->logger = kmalloc(sizeof(*dc_ctx->logger), GFP_KERNEL);
958 
959 	if (!dc_ctx->logger) {
960 		kfree(dc_ctx);
961 		return false;
962 	}
963 
964 	dc_ctx->logger->dev = adev_to_drm(init_params->driver);
965 	dc->dml.logger = dc_ctx->logger;
966 
967 	dc_ctx->dce_version = resource_parse_asic_id(init_params->asic_id);
968 
969 	dc_ctx->perf_trace = dc_perf_trace_create();
970 	if (!dc_ctx->perf_trace) {
971 		kfree(dc_ctx);
972 		ASSERT_CRITICAL(false);
973 		return false;
974 	}
975 
976 	dc->ctx = dc_ctx;
977 
978 	dc->link_srv = link_create_link_service();
979 	if (!dc->link_srv)
980 		return false;
981 
982 	return true;
983 }
984 
dc_construct(struct dc * dc,const struct dc_init_data * init_params)985 static bool dc_construct(struct dc *dc,
986 		const struct dc_init_data *init_params)
987 {
988 	struct dc_context *dc_ctx;
989 	struct bw_calcs_dceip *dc_dceip;
990 	struct bw_calcs_vbios *dc_vbios;
991 	struct dcn_soc_bounding_box *dcn_soc;
992 	struct dcn_ip_params *dcn_ip;
993 
994 	dc->config = init_params->flags;
995 
996 	// Allocate memory for the vm_helper
997 	dc->vm_helper = kzalloc(sizeof(struct vm_helper), GFP_KERNEL);
998 	if (!dc->vm_helper) {
999 		dm_error("%s: failed to create dc->vm_helper\n", __func__);
1000 		goto fail;
1001 	}
1002 
1003 	memcpy(&dc->bb_overrides, &init_params->bb_overrides, sizeof(dc->bb_overrides));
1004 
1005 	dc_dceip = kzalloc(sizeof(*dc_dceip), GFP_KERNEL);
1006 	if (!dc_dceip) {
1007 		dm_error("%s: failed to create dceip\n", __func__);
1008 		goto fail;
1009 	}
1010 
1011 	dc->bw_dceip = dc_dceip;
1012 
1013 	dc_vbios = kzalloc(sizeof(*dc_vbios), GFP_KERNEL);
1014 	if (!dc_vbios) {
1015 		dm_error("%s: failed to create vbios\n", __func__);
1016 		goto fail;
1017 	}
1018 
1019 	dc->bw_vbios = dc_vbios;
1020 	dcn_soc = kzalloc(sizeof(*dcn_soc), GFP_KERNEL);
1021 	if (!dcn_soc) {
1022 		dm_error("%s: failed to create dcn_soc\n", __func__);
1023 		goto fail;
1024 	}
1025 
1026 	dc->dcn_soc = dcn_soc;
1027 
1028 	dcn_ip = kzalloc(sizeof(*dcn_ip), GFP_KERNEL);
1029 	if (!dcn_ip) {
1030 		dm_error("%s: failed to create dcn_ip\n", __func__);
1031 		goto fail;
1032 	}
1033 
1034 	dc->dcn_ip = dcn_ip;
1035 
1036 	if (init_params->bb_from_dmub)
1037 		dc->dml2_options.bb_from_dmub = init_params->bb_from_dmub;
1038 	else
1039 		dc->dml2_options.bb_from_dmub = NULL;
1040 
1041 	if (!dc_construct_ctx(dc, init_params)) {
1042 		dm_error("%s: failed to create ctx\n", __func__);
1043 		goto fail;
1044 	}
1045 
1046 	dc_ctx = dc->ctx;
1047 
1048 	/* Resource should construct all asic specific resources.
1049 	 * This should be the only place where we need to parse the asic id
1050 	 */
1051 	if (init_params->vbios_override)
1052 		dc_ctx->dc_bios = init_params->vbios_override;
1053 	else {
1054 		/* Create BIOS parser */
1055 		struct bp_init_data bp_init_data;
1056 
1057 		bp_init_data.ctx = dc_ctx;
1058 		bp_init_data.bios = init_params->asic_id.atombios_base_address;
1059 
1060 		dc_ctx->dc_bios = dal_bios_parser_create(
1061 				&bp_init_data, dc_ctx->dce_version);
1062 
1063 		if (!dc_ctx->dc_bios) {
1064 			ASSERT_CRITICAL(false);
1065 			goto fail;
1066 		}
1067 
1068 		dc_ctx->created_bios = true;
1069 	}
1070 
1071 	dc->vendor_signature = init_params->vendor_signature;
1072 
1073 	/* Create GPIO service */
1074 	dc_ctx->gpio_service = dal_gpio_service_create(
1075 			dc_ctx->dce_version,
1076 			dc_ctx->dce_environment,
1077 			dc_ctx);
1078 
1079 	if (!dc_ctx->gpio_service) {
1080 		ASSERT_CRITICAL(false);
1081 		goto fail;
1082 	}
1083 
1084 	dc->res_pool = dc_create_resource_pool(dc, init_params, dc_ctx->dce_version);
1085 	if (!dc->res_pool)
1086 		goto fail;
1087 
1088 	/* set i2c speed if not done by the respective dcnxxx__resource.c */
1089 	if (dc->caps.i2c_speed_in_khz_hdcp == 0)
1090 		dc->caps.i2c_speed_in_khz_hdcp = dc->caps.i2c_speed_in_khz;
1091 	if (dc->caps.max_optimizable_video_width == 0)
1092 		dc->caps.max_optimizable_video_width = 5120;
1093 	dc->clk_mgr = dc_clk_mgr_create(dc->ctx, dc->res_pool->pp_smu, dc->res_pool->dccg);
1094 	if (!dc->clk_mgr)
1095 		goto fail;
1096 #ifdef CONFIG_DRM_AMD_DC_FP
1097 	dc->clk_mgr->force_smu_not_present = init_params->force_smu_not_present;
1098 
1099 	if (dc->res_pool->funcs->update_bw_bounding_box) {
1100 		DC_FP_START();
1101 		dc->res_pool->funcs->update_bw_bounding_box(dc, dc->clk_mgr->bw_params);
1102 		DC_FP_END();
1103 	}
1104 #endif
1105 
1106 	if (!create_links(dc, init_params->num_virtual_links))
1107 		goto fail;
1108 
1109 	/* Create additional DIG link encoder objects if fewer than the platform
1110 	 * supports were created during link construction.
1111 	 */
1112 	if (!create_link_encoders(dc))
1113 		goto fail;
1114 
1115 	/* Creation of current_state must occur after dc->dml
1116 	 * is initialized in dc_create_resource_pool because
1117 	 * on creation it copies the contents of dc->dml
1118 	 */
1119 	dc->current_state = dc_state_create(dc, NULL);
1120 
1121 	if (!dc->current_state) {
1122 		dm_error("%s: failed to create validate ctx\n", __func__);
1123 		goto fail;
1124 	}
1125 
1126 	return true;
1127 
1128 fail:
1129 	return false;
1130 }
1131 
disable_all_writeback_pipes_for_stream(const struct dc * dc,struct dc_stream_state * stream,struct dc_state * context)1132 static void disable_all_writeback_pipes_for_stream(
1133 		const struct dc *dc,
1134 		struct dc_stream_state *stream,
1135 		struct dc_state *context)
1136 {
1137 	int i;
1138 
1139 	for (i = 0; i < stream->num_wb_info; i++)
1140 		stream->writeback_info[i].wb_enabled = false;
1141 }
1142 
apply_ctx_interdependent_lock(struct dc * dc,struct dc_state * context,struct dc_stream_state * stream,bool lock)1143 static void apply_ctx_interdependent_lock(struct dc *dc,
1144 					  struct dc_state *context,
1145 					  struct dc_stream_state *stream,
1146 					  bool lock)
1147 {
1148 	int i;
1149 
1150 	/* Checks if interdependent update function pointer is NULL or not, takes care of DCE110 case */
1151 	if (dc->hwss.interdependent_update_lock)
1152 		dc->hwss.interdependent_update_lock(dc, context, lock);
1153 	else {
1154 		for (i = 0; i < dc->res_pool->pipe_count; i++) {
1155 			struct pipe_ctx *pipe_ctx = &context->res_ctx.pipe_ctx[i];
1156 			struct pipe_ctx *old_pipe_ctx = &dc->current_state->res_ctx.pipe_ctx[i];
1157 
1158 			// Copied conditions that were previously in dce110_apply_ctx_for_surface
1159 			if (stream == pipe_ctx->stream) {
1160 				if (resource_is_pipe_type(pipe_ctx, OPP_HEAD) &&
1161 					(pipe_ctx->plane_state || old_pipe_ctx->plane_state))
1162 					dc->hwss.pipe_control_lock(dc, pipe_ctx, lock);
1163 			}
1164 		}
1165 	}
1166 }
1167 
dc_update_visual_confirm_color(struct dc * dc,struct dc_state * context,struct pipe_ctx * pipe_ctx)1168 static void dc_update_visual_confirm_color(struct dc *dc, struct dc_state *context, struct pipe_ctx *pipe_ctx)
1169 {
1170 	if (dc->ctx->dce_version >= DCN_VERSION_1_0) {
1171 		memset(&pipe_ctx->visual_confirm_color, 0, sizeof(struct tg_color));
1172 
1173 		if (dc->debug.visual_confirm == VISUAL_CONFIRM_HDR)
1174 			get_hdr_visual_confirm_color(pipe_ctx, &(pipe_ctx->visual_confirm_color));
1175 		else if (dc->debug.visual_confirm == VISUAL_CONFIRM_SURFACE)
1176 			get_surface_visual_confirm_color(pipe_ctx, &(pipe_ctx->visual_confirm_color));
1177 		else if (dc->debug.visual_confirm == VISUAL_CONFIRM_SWIZZLE)
1178 			get_surface_tile_visual_confirm_color(pipe_ctx, &(pipe_ctx->visual_confirm_color));
1179 		else {
1180 			if (dc->ctx->dce_version < DCN_VERSION_2_0)
1181 				color_space_to_black_color(
1182 					dc, pipe_ctx->stream->output_color_space, &(pipe_ctx->visual_confirm_color));
1183 		}
1184 		if (dc->ctx->dce_version >= DCN_VERSION_2_0) {
1185 			if (dc->debug.visual_confirm == VISUAL_CONFIRM_MPCTREE)
1186 				get_mpctree_visual_confirm_color(pipe_ctx, &(pipe_ctx->visual_confirm_color));
1187 			else if (dc->debug.visual_confirm == VISUAL_CONFIRM_SUBVP)
1188 				get_subvp_visual_confirm_color(pipe_ctx, &(pipe_ctx->visual_confirm_color));
1189 			else if (dc->debug.visual_confirm == VISUAL_CONFIRM_MCLK_SWITCH)
1190 				get_mclk_switch_visual_confirm_color(pipe_ctx, &(pipe_ctx->visual_confirm_color));
1191 			else if (dc->debug.visual_confirm == VISUAL_CONFIRM_FAMS2)
1192 				get_fams2_visual_confirm_color(dc, context, pipe_ctx, &(pipe_ctx->visual_confirm_color));
1193 		}
1194 	}
1195 }
1196 
disable_dangling_plane(struct dc * dc,struct dc_state * context)1197 static void disable_dangling_plane(struct dc *dc, struct dc_state *context)
1198 {
1199 	int i, j;
1200 	struct dc_state *dangling_context = dc_state_create_current_copy(dc);
1201 	struct dc_state *current_ctx;
1202 	struct pipe_ctx *pipe;
1203 	struct timing_generator *tg;
1204 
1205 	if (dangling_context == NULL)
1206 		return;
1207 
1208 	for (i = 0; i < dc->res_pool->pipe_count; i++) {
1209 		struct dc_stream_state *old_stream =
1210 				dc->current_state->res_ctx.pipe_ctx[i].stream;
1211 		bool should_disable = true;
1212 		bool pipe_split_change = false;
1213 
1214 		if ((context->res_ctx.pipe_ctx[i].top_pipe) &&
1215 			(dc->current_state->res_ctx.pipe_ctx[i].top_pipe))
1216 			pipe_split_change = context->res_ctx.pipe_ctx[i].top_pipe->pipe_idx !=
1217 				dc->current_state->res_ctx.pipe_ctx[i].top_pipe->pipe_idx;
1218 		else
1219 			pipe_split_change = context->res_ctx.pipe_ctx[i].top_pipe !=
1220 				dc->current_state->res_ctx.pipe_ctx[i].top_pipe;
1221 
1222 		for (j = 0; j < context->stream_count; j++) {
1223 			if (old_stream == context->streams[j]) {
1224 				should_disable = false;
1225 				break;
1226 			}
1227 		}
1228 		if (!should_disable && pipe_split_change &&
1229 				dc->current_state->stream_count != context->stream_count)
1230 			should_disable = true;
1231 
1232 		if (old_stream && !dc->current_state->res_ctx.pipe_ctx[i].top_pipe &&
1233 				!dc->current_state->res_ctx.pipe_ctx[i].prev_odm_pipe) {
1234 			struct pipe_ctx *old_pipe, *new_pipe;
1235 
1236 			old_pipe = &dc->current_state->res_ctx.pipe_ctx[i];
1237 			new_pipe = &context->res_ctx.pipe_ctx[i];
1238 
1239 			if (old_pipe->plane_state && !new_pipe->plane_state)
1240 				should_disable = true;
1241 		}
1242 
1243 		if (should_disable && old_stream) {
1244 			bool is_phantom = dc_state_get_stream_subvp_type(dc->current_state, old_stream) == SUBVP_PHANTOM;
1245 			pipe = &dc->current_state->res_ctx.pipe_ctx[i];
1246 			tg = pipe->stream_res.tg;
1247 			/* When disabling plane for a phantom pipe, we must turn on the
1248 			 * phantom OTG so the disable programming gets the double buffer
1249 			 * update. Otherwise the pipe will be left in a partially disabled
1250 			 * state that can result in underflow or hang when enabling it
1251 			 * again for different use.
1252 			 */
1253 			if (is_phantom) {
1254 				if (tg->funcs->enable_crtc) {
1255 					int main_pipe_width = 0, main_pipe_height = 0;
1256 					struct dc_stream_state *old_paired_stream = dc_state_get_paired_subvp_stream(dc->current_state, old_stream);
1257 
1258 					if (old_paired_stream) {
1259 						main_pipe_width = old_paired_stream->dst.width;
1260 						main_pipe_height = old_paired_stream->dst.height;
1261 					}
1262 
1263 					if (dc->hwss.blank_phantom)
1264 						dc->hwss.blank_phantom(dc, tg, main_pipe_width, main_pipe_height);
1265 					tg->funcs->enable_crtc(tg);
1266 				}
1267 			}
1268 
1269 			if (is_phantom)
1270 				dc_state_rem_all_phantom_planes_for_stream(dc, old_stream, dangling_context, true);
1271 			else
1272 				dc_state_rem_all_planes_for_stream(dc, old_stream, dangling_context);
1273 			disable_all_writeback_pipes_for_stream(dc, old_stream, dangling_context);
1274 
1275 			if (pipe->stream && pipe->plane_state) {
1276 				if (!dc->debug.using_dml2)
1277 					set_p_state_switch_method(dc, context, pipe);
1278 				dc_update_visual_confirm_color(dc, context, pipe);
1279 			}
1280 
1281 			if (dc->hwss.apply_ctx_for_surface) {
1282 				apply_ctx_interdependent_lock(dc, dc->current_state, old_stream, true);
1283 				dc->hwss.apply_ctx_for_surface(dc, old_stream, 0, dangling_context);
1284 				apply_ctx_interdependent_lock(dc, dc->current_state, old_stream, false);
1285 				dc->hwss.post_unlock_program_front_end(dc, dangling_context);
1286 			}
1287 
1288 			if (dc->res_pool->funcs->prepare_mcache_programming)
1289 				dc->res_pool->funcs->prepare_mcache_programming(dc, dangling_context);
1290 			if (dc->hwss.program_front_end_for_ctx) {
1291 				dc->hwss.interdependent_update_lock(dc, dc->current_state, true);
1292 				dc->hwss.program_front_end_for_ctx(dc, dangling_context);
1293 				dc->hwss.interdependent_update_lock(dc, dc->current_state, false);
1294 				dc->hwss.post_unlock_program_front_end(dc, dangling_context);
1295 			}
1296 			/* We need to put the phantom OTG back into it's default (disabled) state or we
1297 			 * can get corruption when transition from one SubVP config to a different one.
1298 			 * The OTG is set to disable on falling edge of VUPDATE so the plane disable
1299 			 * will still get it's double buffer update.
1300 			 */
1301 			if (is_phantom) {
1302 				if (tg->funcs->disable_phantom_crtc)
1303 					tg->funcs->disable_phantom_crtc(tg);
1304 			}
1305 		}
1306 	}
1307 
1308 	current_ctx = dc->current_state;
1309 	dc->current_state = dangling_context;
1310 	dc_state_release(current_ctx);
1311 }
1312 
disable_vbios_mode_if_required(struct dc * dc,struct dc_state * context)1313 static void disable_vbios_mode_if_required(
1314 		struct dc *dc,
1315 		struct dc_state *context)
1316 {
1317 	unsigned int i, j;
1318 
1319 	/* check if timing_changed, disable stream*/
1320 	for (i = 0; i < dc->res_pool->pipe_count; i++) {
1321 		struct dc_stream_state *stream = NULL;
1322 		struct dc_link *link = NULL;
1323 		struct pipe_ctx *pipe = NULL;
1324 
1325 		pipe = &context->res_ctx.pipe_ctx[i];
1326 		stream = pipe->stream;
1327 		if (stream == NULL)
1328 			continue;
1329 
1330 		if (stream->apply_seamless_boot_optimization)
1331 			continue;
1332 
1333 		// only looking for first odm pipe
1334 		if (pipe->prev_odm_pipe)
1335 			continue;
1336 
1337 		if (stream->link->local_sink &&
1338 			stream->link->local_sink->sink_signal == SIGNAL_TYPE_EDP) {
1339 			link = stream->link;
1340 		}
1341 
1342 		if (link != NULL && link->link_enc->funcs->is_dig_enabled(link->link_enc)) {
1343 			unsigned int enc_inst, tg_inst = 0;
1344 			unsigned int pix_clk_100hz = 0;
1345 
1346 			enc_inst = link->link_enc->funcs->get_dig_frontend(link->link_enc);
1347 			if (enc_inst != ENGINE_ID_UNKNOWN) {
1348 				for (j = 0; j < dc->res_pool->stream_enc_count; j++) {
1349 					if (dc->res_pool->stream_enc[j]->id == enc_inst) {
1350 						tg_inst = dc->res_pool->stream_enc[j]->funcs->dig_source_otg(
1351 							dc->res_pool->stream_enc[j]);
1352 						break;
1353 					}
1354 				}
1355 
1356 				dc->res_pool->dp_clock_source->funcs->get_pixel_clk_frequency_100hz(
1357 					dc->res_pool->dp_clock_source,
1358 					tg_inst, &pix_clk_100hz);
1359 
1360 				if (link->link_status.link_active) {
1361 					uint32_t requested_pix_clk_100hz =
1362 						pipe->stream_res.pix_clk_params.requested_pix_clk_100hz;
1363 
1364 					if (pix_clk_100hz != requested_pix_clk_100hz) {
1365 						dc->link_srv->set_dpms_off(pipe);
1366 						pipe->stream->dpms_off = false;
1367 					}
1368 				}
1369 			}
1370 		}
1371 	}
1372 }
1373 
1374 /* Public functions */
1375 
dc_create(const struct dc_init_data * init_params)1376 struct dc *dc_create(const struct dc_init_data *init_params)
1377 {
1378 	struct dc *dc = kzalloc(sizeof(*dc), GFP_KERNEL);
1379 	unsigned int full_pipe_count;
1380 
1381 	if (!dc)
1382 		return NULL;
1383 
1384 	if (init_params->dce_environment == DCE_ENV_VIRTUAL_HW) {
1385 		dc->caps.linear_pitch_alignment = 64;
1386 		if (!dc_construct_ctx(dc, init_params))
1387 			goto destruct_dc;
1388 	} else {
1389 		if (!dc_construct(dc, init_params))
1390 			goto destruct_dc;
1391 
1392 		full_pipe_count = dc->res_pool->pipe_count;
1393 		if (dc->res_pool->underlay_pipe_index != NO_UNDERLAY_PIPE)
1394 			full_pipe_count--;
1395 		dc->caps.max_streams = min(
1396 				full_pipe_count,
1397 				dc->res_pool->stream_enc_count);
1398 
1399 		dc->caps.max_links = dc->link_count;
1400 		dc->caps.max_audios = dc->res_pool->audio_count;
1401 		dc->caps.linear_pitch_alignment = 64;
1402 
1403 		dc->caps.max_dp_protocol_version = DP_VERSION_1_4;
1404 
1405 		dc->caps.max_otg_num = dc->res_pool->res_cap->num_timing_generator;
1406 
1407 		if (dc->res_pool->dmcu != NULL)
1408 			dc->versions.dmcu_version = dc->res_pool->dmcu->dmcu_version;
1409 	}
1410 
1411 	dc->dcn_reg_offsets = init_params->dcn_reg_offsets;
1412 	dc->nbio_reg_offsets = init_params->nbio_reg_offsets;
1413 	dc->clk_reg_offsets = init_params->clk_reg_offsets;
1414 
1415 	/* Populate versioning information */
1416 	dc->versions.dc_ver = DC_VER;
1417 
1418 	dc->build_id = DC_BUILD_ID;
1419 
1420 	DC_LOG_DC("Display Core initialized\n");
1421 
1422 	return dc;
1423 
1424 destruct_dc:
1425 	dc_destruct(dc);
1426 	kfree(dc);
1427 	return NULL;
1428 }
1429 
detect_edp_presence(struct dc * dc)1430 static void detect_edp_presence(struct dc *dc)
1431 {
1432 	struct dc_link *edp_links[MAX_NUM_EDP];
1433 	struct dc_link *edp_link = NULL;
1434 	enum dc_connection_type type;
1435 	int i;
1436 	int edp_num;
1437 
1438 	dc_get_edp_links(dc, edp_links, &edp_num);
1439 	if (!edp_num)
1440 		return;
1441 
1442 	for (i = 0; i < edp_num; i++) {
1443 		edp_link = edp_links[i];
1444 		if (dc->config.edp_not_connected) {
1445 			edp_link->edp_sink_present = false;
1446 		} else {
1447 			dc_link_detect_connection_type(edp_link, &type);
1448 			edp_link->edp_sink_present = (type != dc_connection_none);
1449 		}
1450 	}
1451 }
1452 
dc_hardware_init(struct dc * dc)1453 void dc_hardware_init(struct dc *dc)
1454 {
1455 
1456 	detect_edp_presence(dc);
1457 	if (dc->ctx->dce_environment != DCE_ENV_VIRTUAL_HW)
1458 		dc->hwss.init_hw(dc);
1459 }
1460 
dc_init_callbacks(struct dc * dc,const struct dc_callback_init * init_params)1461 void dc_init_callbacks(struct dc *dc,
1462 		const struct dc_callback_init *init_params)
1463 {
1464 	dc->ctx->cp_psp = init_params->cp_psp;
1465 }
1466 
dc_deinit_callbacks(struct dc * dc)1467 void dc_deinit_callbacks(struct dc *dc)
1468 {
1469 	memset(&dc->ctx->cp_psp, 0, sizeof(dc->ctx->cp_psp));
1470 }
1471 
dc_destroy(struct dc ** dc)1472 void dc_destroy(struct dc **dc)
1473 {
1474 	dc_destruct(*dc);
1475 	kfree(*dc);
1476 	*dc = NULL;
1477 }
1478 
enable_timing_multisync(struct dc * dc,struct dc_state * ctx)1479 static void enable_timing_multisync(
1480 		struct dc *dc,
1481 		struct dc_state *ctx)
1482 {
1483 	int i, multisync_count = 0;
1484 	int pipe_count = dc->res_pool->pipe_count;
1485 	struct pipe_ctx *multisync_pipes[MAX_PIPES] = { NULL };
1486 
1487 	for (i = 0; i < pipe_count; i++) {
1488 		if (!ctx->res_ctx.pipe_ctx[i].stream ||
1489 				!ctx->res_ctx.pipe_ctx[i].stream->triggered_crtc_reset.enabled)
1490 			continue;
1491 		if (ctx->res_ctx.pipe_ctx[i].stream == ctx->res_ctx.pipe_ctx[i].stream->triggered_crtc_reset.event_source)
1492 			continue;
1493 		multisync_pipes[multisync_count] = &ctx->res_ctx.pipe_ctx[i];
1494 		multisync_count++;
1495 	}
1496 
1497 	if (multisync_count > 0) {
1498 		dc->hwss.enable_per_frame_crtc_position_reset(
1499 			dc, multisync_count, multisync_pipes);
1500 	}
1501 }
1502 
program_timing_sync(struct dc * dc,struct dc_state * ctx)1503 static void program_timing_sync(
1504 		struct dc *dc,
1505 		struct dc_state *ctx)
1506 {
1507 	int i, j, k;
1508 	int group_index = 0;
1509 	int num_group = 0;
1510 	int pipe_count = dc->res_pool->pipe_count;
1511 	struct pipe_ctx *unsynced_pipes[MAX_PIPES] = { NULL };
1512 
1513 	for (i = 0; i < pipe_count; i++) {
1514 		if (!ctx->res_ctx.pipe_ctx[i].stream
1515 				|| ctx->res_ctx.pipe_ctx[i].top_pipe
1516 				|| ctx->res_ctx.pipe_ctx[i].prev_odm_pipe)
1517 			continue;
1518 
1519 		unsynced_pipes[i] = &ctx->res_ctx.pipe_ctx[i];
1520 	}
1521 
1522 	for (i = 0; i < pipe_count; i++) {
1523 		int group_size = 1;
1524 		enum timing_synchronization_type sync_type = NOT_SYNCHRONIZABLE;
1525 		struct pipe_ctx *pipe_set[MAX_PIPES];
1526 
1527 		if (!unsynced_pipes[i])
1528 			continue;
1529 
1530 		pipe_set[0] = unsynced_pipes[i];
1531 		unsynced_pipes[i] = NULL;
1532 
1533 		/* Add tg to the set, search rest of the tg's for ones with
1534 		 * same timing, add all tgs with same timing to the group
1535 		 */
1536 		for (j = i + 1; j < pipe_count; j++) {
1537 			if (!unsynced_pipes[j])
1538 				continue;
1539 			if (sync_type != TIMING_SYNCHRONIZABLE &&
1540 				dc->hwss.enable_vblanks_synchronization &&
1541 				unsynced_pipes[j]->stream_res.tg->funcs->align_vblanks &&
1542 				resource_are_vblanks_synchronizable(
1543 					unsynced_pipes[j]->stream,
1544 					pipe_set[0]->stream)) {
1545 				sync_type = VBLANK_SYNCHRONIZABLE;
1546 				pipe_set[group_size] = unsynced_pipes[j];
1547 				unsynced_pipes[j] = NULL;
1548 				group_size++;
1549 			} else
1550 			if (sync_type != VBLANK_SYNCHRONIZABLE &&
1551 				resource_are_streams_timing_synchronizable(
1552 					unsynced_pipes[j]->stream,
1553 					pipe_set[0]->stream)) {
1554 				sync_type = TIMING_SYNCHRONIZABLE;
1555 				pipe_set[group_size] = unsynced_pipes[j];
1556 				unsynced_pipes[j] = NULL;
1557 				group_size++;
1558 			}
1559 		}
1560 
1561 		/* set first unblanked pipe as master */
1562 		for (j = 0; j < group_size; j++) {
1563 			bool is_blanked;
1564 
1565 			if (pipe_set[j]->stream_res.opp->funcs->dpg_is_blanked)
1566 				is_blanked =
1567 					pipe_set[j]->stream_res.opp->funcs->dpg_is_blanked(pipe_set[j]->stream_res.opp);
1568 			else
1569 				is_blanked =
1570 					pipe_set[j]->stream_res.tg->funcs->is_blanked(pipe_set[j]->stream_res.tg);
1571 			if (!is_blanked) {
1572 				if (j == 0)
1573 					break;
1574 
1575 				swap(pipe_set[0], pipe_set[j]);
1576 				break;
1577 			}
1578 		}
1579 
1580 		for (k = 0; k < group_size; k++) {
1581 			struct dc_stream_status *status = dc_state_get_stream_status(ctx, pipe_set[k]->stream);
1582 
1583 			if (!status)
1584 				continue;
1585 
1586 			status->timing_sync_info.group_id = num_group;
1587 			status->timing_sync_info.group_size = group_size;
1588 			if (k == 0)
1589 				status->timing_sync_info.master = true;
1590 			else
1591 				status->timing_sync_info.master = false;
1592 
1593 		}
1594 
1595 		/* remove any other unblanked pipes as they have already been synced */
1596 		if (dc->config.use_pipe_ctx_sync_logic) {
1597 			/* check pipe's syncd to decide which pipe to be removed */
1598 			for (j = 1; j < group_size; j++) {
1599 				if (pipe_set[j]->pipe_idx_syncd == pipe_set[0]->pipe_idx_syncd) {
1600 					group_size--;
1601 					pipe_set[j] = pipe_set[group_size];
1602 					j--;
1603 				} else
1604 					/* link slave pipe's syncd with master pipe */
1605 					pipe_set[j]->pipe_idx_syncd = pipe_set[0]->pipe_idx_syncd;
1606 			}
1607 		} else {
1608 			/* remove any other pipes by checking valid plane */
1609 			for (j = j + 1; j < group_size; j++) {
1610 				bool is_blanked;
1611 
1612 				if (pipe_set[j]->stream_res.opp->funcs->dpg_is_blanked)
1613 					is_blanked =
1614 						pipe_set[j]->stream_res.opp->funcs->dpg_is_blanked(pipe_set[j]->stream_res.opp);
1615 				else
1616 					is_blanked =
1617 						pipe_set[j]->stream_res.tg->funcs->is_blanked(pipe_set[j]->stream_res.tg);
1618 				if (!is_blanked) {
1619 					group_size--;
1620 					pipe_set[j] = pipe_set[group_size];
1621 					j--;
1622 				}
1623 			}
1624 		}
1625 
1626 		if (group_size > 1) {
1627 			if (sync_type == TIMING_SYNCHRONIZABLE) {
1628 				dc->hwss.enable_timing_synchronization(
1629 					dc, ctx, group_index, group_size, pipe_set);
1630 			} else
1631 				if (sync_type == VBLANK_SYNCHRONIZABLE) {
1632 				dc->hwss.enable_vblanks_synchronization(
1633 					dc, group_index, group_size, pipe_set);
1634 				}
1635 			group_index++;
1636 		}
1637 		num_group++;
1638 	}
1639 }
1640 
streams_changed(struct dc * dc,struct dc_stream_state * streams[],uint8_t stream_count)1641 static bool streams_changed(struct dc *dc,
1642 			    struct dc_stream_state *streams[],
1643 			    uint8_t stream_count)
1644 {
1645 	uint8_t i;
1646 
1647 	if (stream_count != dc->current_state->stream_count)
1648 		return true;
1649 
1650 	for (i = 0; i < dc->current_state->stream_count; i++) {
1651 		if (dc->current_state->streams[i] != streams[i])
1652 			return true;
1653 		if (!streams[i]->link->link_state_valid)
1654 			return true;
1655 	}
1656 
1657 	return false;
1658 }
1659 
dc_validate_boot_timing(const struct dc * dc,const struct dc_sink * sink,struct dc_crtc_timing * crtc_timing)1660 bool dc_validate_boot_timing(const struct dc *dc,
1661 				const struct dc_sink *sink,
1662 				struct dc_crtc_timing *crtc_timing)
1663 {
1664 	struct timing_generator *tg;
1665 	struct stream_encoder *se = NULL;
1666 
1667 	struct dc_crtc_timing hw_crtc_timing = {0};
1668 
1669 	struct dc_link *link = sink->link;
1670 	unsigned int i, enc_inst, tg_inst = 0;
1671 
1672 	/* Support seamless boot on EDP displays only */
1673 	if (sink->sink_signal != SIGNAL_TYPE_EDP) {
1674 		return false;
1675 	}
1676 
1677 	if (dc->debug.force_odm_combine)
1678 		return false;
1679 
1680 	/* Check for enabled DIG to identify enabled display */
1681 	if (!link->link_enc->funcs->is_dig_enabled(link->link_enc))
1682 		return false;
1683 
1684 	enc_inst = link->link_enc->funcs->get_dig_frontend(link->link_enc);
1685 
1686 	if (enc_inst == ENGINE_ID_UNKNOWN)
1687 		return false;
1688 
1689 	for (i = 0; i < dc->res_pool->stream_enc_count; i++) {
1690 		if (dc->res_pool->stream_enc[i]->id == enc_inst) {
1691 
1692 			se = dc->res_pool->stream_enc[i];
1693 
1694 			tg_inst = dc->res_pool->stream_enc[i]->funcs->dig_source_otg(
1695 				dc->res_pool->stream_enc[i]);
1696 			break;
1697 		}
1698 	}
1699 
1700 	// tg_inst not found
1701 	if (i == dc->res_pool->stream_enc_count)
1702 		return false;
1703 
1704 	if (tg_inst >= dc->res_pool->timing_generator_count)
1705 		return false;
1706 
1707 	if (tg_inst != link->link_enc->preferred_engine)
1708 		return false;
1709 
1710 	tg = dc->res_pool->timing_generators[tg_inst];
1711 
1712 	if (!tg->funcs->get_hw_timing)
1713 		return false;
1714 
1715 	if (!tg->funcs->get_hw_timing(tg, &hw_crtc_timing))
1716 		return false;
1717 
1718 	if (crtc_timing->h_total != hw_crtc_timing.h_total)
1719 		return false;
1720 
1721 	if (crtc_timing->h_border_left != hw_crtc_timing.h_border_left)
1722 		return false;
1723 
1724 	if (crtc_timing->h_addressable != hw_crtc_timing.h_addressable)
1725 		return false;
1726 
1727 	if (crtc_timing->h_border_right != hw_crtc_timing.h_border_right)
1728 		return false;
1729 
1730 	if (crtc_timing->h_front_porch != hw_crtc_timing.h_front_porch)
1731 		return false;
1732 
1733 	if (crtc_timing->h_sync_width != hw_crtc_timing.h_sync_width)
1734 		return false;
1735 
1736 	if (crtc_timing->v_total != hw_crtc_timing.v_total)
1737 		return false;
1738 
1739 	if (crtc_timing->v_border_top != hw_crtc_timing.v_border_top)
1740 		return false;
1741 
1742 	if (crtc_timing->v_addressable != hw_crtc_timing.v_addressable)
1743 		return false;
1744 
1745 	if (crtc_timing->v_border_bottom != hw_crtc_timing.v_border_bottom)
1746 		return false;
1747 
1748 	if (crtc_timing->v_front_porch != hw_crtc_timing.v_front_porch)
1749 		return false;
1750 
1751 	if (crtc_timing->v_sync_width != hw_crtc_timing.v_sync_width)
1752 		return false;
1753 
1754 	/* block DSC for now, as VBIOS does not currently support DSC timings */
1755 	if (crtc_timing->flags.DSC)
1756 		return false;
1757 
1758 	if (dc_is_dp_signal(link->connector_signal)) {
1759 		unsigned int pix_clk_100hz = 0;
1760 		uint32_t numOdmPipes = 1;
1761 		uint32_t id_src[4] = {0};
1762 
1763 		dc->res_pool->dp_clock_source->funcs->get_pixel_clk_frequency_100hz(
1764 			dc->res_pool->dp_clock_source,
1765 			tg_inst, &pix_clk_100hz);
1766 
1767 		if (tg->funcs->get_optc_source)
1768 			tg->funcs->get_optc_source(tg,
1769 						&numOdmPipes, &id_src[0], &id_src[1]);
1770 
1771 		if (numOdmPipes == 2) {
1772 			pix_clk_100hz *= 2;
1773 		} else if (numOdmPipes == 4) {
1774 			pix_clk_100hz *= 4;
1775 		} else if (se && se->funcs->get_pixels_per_cycle) {
1776 			uint32_t pixels_per_cycle = se->funcs->get_pixels_per_cycle(se);
1777 
1778 			if (pixels_per_cycle != 1 && !dc->debug.enable_dp_dig_pixel_rate_div_policy)
1779 				return false;
1780 
1781 			pix_clk_100hz *= pixels_per_cycle;
1782 		}
1783 
1784 		// Note: In rare cases, HW pixclk may differ from crtc's pixclk
1785 		// slightly due to rounding issues in 10 kHz units.
1786 		if (crtc_timing->pix_clk_100hz != pix_clk_100hz)
1787 			return false;
1788 
1789 		if (!se || !se->funcs->dp_get_pixel_format)
1790 			return false;
1791 
1792 		if (!se->funcs->dp_get_pixel_format(
1793 			se,
1794 			&hw_crtc_timing.pixel_encoding,
1795 			&hw_crtc_timing.display_color_depth))
1796 			return false;
1797 
1798 		if (hw_crtc_timing.display_color_depth != crtc_timing->display_color_depth)
1799 			return false;
1800 
1801 		if (hw_crtc_timing.pixel_encoding != crtc_timing->pixel_encoding)
1802 			return false;
1803 	}
1804 
1805 	if (link->dpcd_caps.dprx_feature.bits.VSC_SDP_COLORIMETRY_SUPPORTED) {
1806 		return false;
1807 	}
1808 
1809 	if (link->dpcd_caps.channel_coding_cap.bits.DP_128b_132b_SUPPORTED)
1810 		return false;
1811 
1812 	if (dc->link_srv->edp_is_ilr_optimization_required(link, crtc_timing)) {
1813 		DC_LOG_EVENT_LINK_TRAINING("Seamless boot disabled to optimize eDP link rate\n");
1814 		return false;
1815 	}
1816 
1817 	return true;
1818 }
1819 
should_update_pipe_for_stream(struct dc_state * context,struct pipe_ctx * pipe_ctx,struct dc_stream_state * stream)1820 static inline bool should_update_pipe_for_stream(
1821 		struct dc_state *context,
1822 		struct pipe_ctx *pipe_ctx,
1823 		struct dc_stream_state *stream)
1824 {
1825 	return (pipe_ctx->stream && pipe_ctx->stream == stream);
1826 }
1827 
should_update_pipe_for_plane(struct dc_state * context,struct pipe_ctx * pipe_ctx,struct dc_plane_state * plane_state)1828 static inline bool should_update_pipe_for_plane(
1829 		struct dc_state *context,
1830 		struct pipe_ctx *pipe_ctx,
1831 		struct dc_plane_state *plane_state)
1832 {
1833 	return (pipe_ctx->plane_state == plane_state);
1834 }
1835 
dc_enable_stereo(struct dc * dc,struct dc_state * context,struct dc_stream_state * streams[],uint8_t stream_count)1836 void dc_enable_stereo(
1837 	struct dc *dc,
1838 	struct dc_state *context,
1839 	struct dc_stream_state *streams[],
1840 	uint8_t stream_count)
1841 {
1842 	int i, j;
1843 	struct pipe_ctx *pipe;
1844 
1845 	dc_exit_ips_for_hw_access(dc);
1846 
1847 	for (i = 0; i < MAX_PIPES; i++) {
1848 		if (context != NULL) {
1849 			pipe = &context->res_ctx.pipe_ctx[i];
1850 		} else {
1851 			context = dc->current_state;
1852 			pipe = &dc->current_state->res_ctx.pipe_ctx[i];
1853 		}
1854 
1855 		for (j = 0; pipe && j < stream_count; j++)  {
1856 			if (should_update_pipe_for_stream(context, pipe, streams[j]) &&
1857 				dc->hwss.setup_stereo)
1858 				dc->hwss.setup_stereo(pipe, dc);
1859 		}
1860 	}
1861 }
1862 
dc_trigger_sync(struct dc * dc,struct dc_state * context)1863 void dc_trigger_sync(struct dc *dc, struct dc_state *context)
1864 {
1865 	if (context->stream_count > 1 && !dc->debug.disable_timing_sync) {
1866 		dc_exit_ips_for_hw_access(dc);
1867 
1868 		enable_timing_multisync(dc, context);
1869 		program_timing_sync(dc, context);
1870 	}
1871 }
1872 
get_stream_mask(struct dc * dc,struct dc_state * context)1873 static uint8_t get_stream_mask(struct dc *dc, struct dc_state *context)
1874 {
1875 	int i;
1876 	unsigned int stream_mask = 0;
1877 
1878 	for (i = 0; i < dc->res_pool->pipe_count; i++) {
1879 		if (context->res_ctx.pipe_ctx[i].stream)
1880 			stream_mask |= 1 << i;
1881 	}
1882 
1883 	return stream_mask;
1884 }
1885 
dc_z10_restore(const struct dc * dc)1886 void dc_z10_restore(const struct dc *dc)
1887 {
1888 	if (dc->hwss.z10_restore)
1889 		dc->hwss.z10_restore(dc);
1890 }
1891 
dc_z10_save_init(struct dc * dc)1892 void dc_z10_save_init(struct dc *dc)
1893 {
1894 	if (dc->hwss.z10_save_init)
1895 		dc->hwss.z10_save_init(dc);
1896 }
1897 
1898 /**
1899  * dc_commit_state_no_check - Apply context to the hardware
1900  *
1901  * @dc: DC object with the current status to be updated
1902  * @context: New state that will become the current status at the end of this function
1903  *
1904  * Applies given context to the hardware and copy it into current context.
1905  * It's up to the user to release the src context afterwards.
1906  *
1907  * Return: an enum dc_status result code for the operation
1908  */
dc_commit_state_no_check(struct dc * dc,struct dc_state * context)1909 static enum dc_status dc_commit_state_no_check(struct dc *dc, struct dc_state *context)
1910 {
1911 	struct dc_bios *dcb = dc->ctx->dc_bios;
1912 	enum dc_status result = DC_ERROR_UNEXPECTED;
1913 	struct pipe_ctx *pipe;
1914 	int i, k, l;
1915 	struct dc_stream_state *dc_streams[MAX_STREAMS] = {0};
1916 	struct dc_state *old_state;
1917 	bool subvp_prev_use = false;
1918 
1919 	dc_z10_restore(dc);
1920 	dc_allow_idle_optimizations(dc, false);
1921 
1922 	for (i = 0; i < dc->res_pool->pipe_count; i++) {
1923 		struct pipe_ctx *old_pipe = &dc->current_state->res_ctx.pipe_ctx[i];
1924 
1925 		/* Check old context for SubVP */
1926 		subvp_prev_use |= (dc_state_get_pipe_subvp_type(dc->current_state, old_pipe) == SUBVP_PHANTOM);
1927 		if (subvp_prev_use)
1928 			break;
1929 	}
1930 
1931 	for (i = 0; i < context->stream_count; i++)
1932 		dc_streams[i] =  context->streams[i];
1933 
1934 	if (!dcb->funcs->is_accelerated_mode(dcb)) {
1935 		disable_vbios_mode_if_required(dc, context);
1936 		dc->hwss.enable_accelerated_mode(dc, context);
1937 	}
1938 
1939 	if (context->stream_count > get_seamless_boot_stream_count(context) ||
1940 		context->stream_count == 0)
1941 		dc->hwss.prepare_bandwidth(dc, context);
1942 
1943 	/* When SubVP is active, all HW programming must be done while
1944 	 * SubVP lock is acquired
1945 	 */
1946 	if (dc->hwss.subvp_pipe_control_lock)
1947 		dc->hwss.subvp_pipe_control_lock(dc, context, true, true, NULL, subvp_prev_use);
1948 	if (dc->hwss.fams2_global_control_lock)
1949 		dc->hwss.fams2_global_control_lock(dc, context, true);
1950 
1951 	if (dc->hwss.update_dsc_pg)
1952 		dc->hwss.update_dsc_pg(dc, context, false);
1953 
1954 	disable_dangling_plane(dc, context);
1955 	/* re-program planes for existing stream, in case we need to
1956 	 * free up plane resource for later use
1957 	 */
1958 	if (dc->hwss.apply_ctx_for_surface) {
1959 		for (i = 0; i < context->stream_count; i++) {
1960 			if (context->streams[i]->mode_changed)
1961 				continue;
1962 			apply_ctx_interdependent_lock(dc, context, context->streams[i], true);
1963 			dc->hwss.apply_ctx_for_surface(
1964 				dc, context->streams[i],
1965 				context->stream_status[i].plane_count,
1966 				context); /* use new pipe config in new context */
1967 			apply_ctx_interdependent_lock(dc, context, context->streams[i], false);
1968 			dc->hwss.post_unlock_program_front_end(dc, context);
1969 		}
1970 	}
1971 
1972 	/* Program hardware */
1973 	for (i = 0; i < dc->res_pool->pipe_count; i++) {
1974 		pipe = &context->res_ctx.pipe_ctx[i];
1975 		dc->hwss.wait_for_mpcc_disconnect(dc, dc->res_pool, pipe);
1976 	}
1977 
1978 	result = dc->hwss.apply_ctx_to_hw(dc, context);
1979 
1980 	if (result != DC_OK) {
1981 		/* Application of dc_state to hardware stopped. */
1982 		dc->current_state->res_ctx.link_enc_cfg_ctx.mode = LINK_ENC_CFG_STEADY;
1983 		return result;
1984 	}
1985 
1986 	dc_trigger_sync(dc, context);
1987 
1988 	/* Full update should unconditionally be triggered when dc_commit_state_no_check is called */
1989 	for (i = 0; i < context->stream_count; i++) {
1990 		uint32_t prev_dsc_changed = context->streams[i]->update_flags.bits.dsc_changed;
1991 
1992 		context->streams[i]->update_flags.raw = 0xFFFFFFFF;
1993 		context->streams[i]->update_flags.bits.dsc_changed = prev_dsc_changed;
1994 	}
1995 
1996 	/* Program all planes within new context*/
1997 	if (dc->res_pool->funcs->prepare_mcache_programming)
1998 		dc->res_pool->funcs->prepare_mcache_programming(dc, context);
1999 	if (dc->hwss.program_front_end_for_ctx) {
2000 		dc->hwss.interdependent_update_lock(dc, context, true);
2001 		dc->hwss.program_front_end_for_ctx(dc, context);
2002 		dc->hwss.interdependent_update_lock(dc, context, false);
2003 		dc->hwss.post_unlock_program_front_end(dc, context);
2004 	}
2005 
2006 	if (dc->hwss.commit_subvp_config)
2007 		dc->hwss.commit_subvp_config(dc, context);
2008 	if (dc->hwss.subvp_pipe_control_lock)
2009 		dc->hwss.subvp_pipe_control_lock(dc, context, false, true, NULL, subvp_prev_use);
2010 	if (dc->hwss.fams2_global_control_lock)
2011 		dc->hwss.fams2_global_control_lock(dc, context, false);
2012 
2013 	for (i = 0; i < context->stream_count; i++) {
2014 		const struct dc_link *link = context->streams[i]->link;
2015 
2016 		if (!context->streams[i]->mode_changed)
2017 			continue;
2018 
2019 		if (dc->hwss.apply_ctx_for_surface) {
2020 			apply_ctx_interdependent_lock(dc, context, context->streams[i], true);
2021 			dc->hwss.apply_ctx_for_surface(
2022 					dc, context->streams[i],
2023 					context->stream_status[i].plane_count,
2024 					context);
2025 			apply_ctx_interdependent_lock(dc, context, context->streams[i], false);
2026 			dc->hwss.post_unlock_program_front_end(dc, context);
2027 		}
2028 
2029 		/*
2030 		 * enable stereo
2031 		 * TODO rework dc_enable_stereo call to work with validation sets?
2032 		 */
2033 		for (k = 0; k < MAX_PIPES; k++) {
2034 			pipe = &context->res_ctx.pipe_ctx[k];
2035 
2036 			for (l = 0 ; pipe && l < context->stream_count; l++)  {
2037 				if (context->streams[l] &&
2038 					context->streams[l] == pipe->stream &&
2039 					dc->hwss.setup_stereo)
2040 					dc->hwss.setup_stereo(pipe, dc);
2041 			}
2042 		}
2043 
2044 		CONN_MSG_MODE(link, "{%dx%d, %dx%d@%dKhz}",
2045 				context->streams[i]->timing.h_addressable,
2046 				context->streams[i]->timing.v_addressable,
2047 				context->streams[i]->timing.h_total,
2048 				context->streams[i]->timing.v_total,
2049 				context->streams[i]->timing.pix_clk_100hz / 10);
2050 	}
2051 
2052 	dc_enable_stereo(dc, context, dc_streams, context->stream_count);
2053 
2054 	if (get_seamless_boot_stream_count(context) == 0 ||
2055 		context->stream_count == 0) {
2056 		/* Must wait for no flips to be pending before doing optimize bw */
2057 		hwss_wait_for_no_pipes_pending(dc, context);
2058 		/*
2059 		 * optimized dispclk depends on ODM setup. Need to wait for ODM
2060 		 * update pending complete before optimizing bandwidth.
2061 		 */
2062 		hwss_wait_for_odm_update_pending_complete(dc, context);
2063 		/* pplib is notified if disp_num changed */
2064 		dc->hwss.optimize_bandwidth(dc, context);
2065 		/* Need to do otg sync again as otg could be out of sync due to otg
2066 		 * workaround applied during clock update
2067 		 */
2068 		dc_trigger_sync(dc, context);
2069 	}
2070 
2071 	if (dc->hwss.update_dsc_pg)
2072 		dc->hwss.update_dsc_pg(dc, context, true);
2073 
2074 	if (dc->ctx->dce_version >= DCE_VERSION_MAX)
2075 		TRACE_DCN_CLOCK_STATE(&context->bw_ctx.bw.dcn.clk);
2076 	else
2077 		TRACE_DCE_CLOCK_STATE(&context->bw_ctx.bw.dce);
2078 
2079 	context->stream_mask = get_stream_mask(dc, context);
2080 
2081 	if (context->stream_mask != dc->current_state->stream_mask)
2082 		dc_dmub_srv_notify_stream_mask(dc->ctx->dmub_srv, context->stream_mask);
2083 
2084 	for (i = 0; i < context->stream_count; i++)
2085 		context->streams[i]->mode_changed = false;
2086 
2087 	/* Clear update flags that were set earlier to avoid redundant programming */
2088 	for (i = 0; i < context->stream_count; i++) {
2089 		context->streams[i]->update_flags.raw = 0x0;
2090 	}
2091 
2092 	old_state = dc->current_state;
2093 	dc->current_state = context;
2094 
2095 	dc_state_release(old_state);
2096 
2097 	dc_state_retain(dc->current_state);
2098 
2099 	return result;
2100 }
2101 
2102 static bool commit_minimal_transition_state(struct dc *dc,
2103 		struct dc_state *transition_base_context);
2104 
2105 /**
2106  * dc_commit_streams - Commit current stream state
2107  *
2108  * @dc: DC object with the commit state to be configured in the hardware
2109  * @params: Parameters for the commit, including the streams to be committed
2110  *
2111  * Function responsible for commit streams change to the hardware.
2112  *
2113  * Return:
2114  * Return DC_OK if everything work as expected, otherwise, return a dc_status
2115  * code.
2116  */
dc_commit_streams(struct dc * dc,struct dc_commit_streams_params * params)2117 enum dc_status dc_commit_streams(struct dc *dc, struct dc_commit_streams_params *params)
2118 {
2119 	int i, j;
2120 	struct dc_state *context;
2121 	enum dc_status res = DC_OK;
2122 	struct dc_validation_set set[MAX_STREAMS] = {0};
2123 	struct pipe_ctx *pipe;
2124 	bool handle_exit_odm2to1 = false;
2125 
2126 	if (!params)
2127 		return DC_ERROR_UNEXPECTED;
2128 
2129 	if (dc->ctx->dce_environment == DCE_ENV_VIRTUAL_HW)
2130 		return res;
2131 
2132 	if (!streams_changed(dc, params->streams, params->stream_count) &&
2133 			dc->current_state->power_source == params->power_source)
2134 		return res;
2135 
2136 	dc_exit_ips_for_hw_access(dc);
2137 
2138 	DC_LOG_DC("%s: %d streams\n", __func__, params->stream_count);
2139 
2140 	for (i = 0; i < params->stream_count; i++) {
2141 		struct dc_stream_state *stream = params->streams[i];
2142 		struct dc_stream_status *status = dc_stream_get_status(stream);
2143 
2144 		dc_stream_log(dc, stream);
2145 
2146 		set[i].stream = stream;
2147 
2148 		if (status) {
2149 			set[i].plane_count = status->plane_count;
2150 			for (j = 0; j < status->plane_count; j++)
2151 				set[i].plane_states[j] = status->plane_states[j];
2152 		}
2153 	}
2154 
2155 	/* ODM Combine 2:1 power optimization is only applied for single stream
2156 	 * scenario, it uses extra pipes than needed to reduce power consumption
2157 	 * We need to switch off this feature to make room for new streams.
2158 	 */
2159 	if (params->stream_count > dc->current_state->stream_count &&
2160 			dc->current_state->stream_count == 1) {
2161 		for (i = 0; i < dc->res_pool->pipe_count; i++) {
2162 			pipe = &dc->current_state->res_ctx.pipe_ctx[i];
2163 			if (pipe->next_odm_pipe)
2164 				handle_exit_odm2to1 = true;
2165 		}
2166 	}
2167 
2168 	if (handle_exit_odm2to1)
2169 		res = commit_minimal_transition_state(dc, dc->current_state);
2170 
2171 	context = dc_state_create_current_copy(dc);
2172 	if (!context)
2173 		goto context_alloc_fail;
2174 
2175 	context->power_source = params->power_source;
2176 
2177 	res = dc_validate_with_context(dc, set, params->stream_count, context, false);
2178 	if (res != DC_OK) {
2179 		BREAK_TO_DEBUGGER();
2180 		goto fail;
2181 	}
2182 
2183 	res = dc_commit_state_no_check(dc, context);
2184 
2185 	for (i = 0; i < params->stream_count; i++) {
2186 		for (j = 0; j < context->stream_count; j++) {
2187 			if (params->streams[i]->stream_id == context->streams[j]->stream_id)
2188 				params->streams[i]->out.otg_offset = context->stream_status[j].primary_otg_inst;
2189 
2190 			if (dc_is_embedded_signal(params->streams[i]->signal)) {
2191 				struct dc_stream_status *status = dc_state_get_stream_status(context, params->streams[i]);
2192 
2193 				if (!status)
2194 					continue;
2195 
2196 				if (dc->hwss.is_abm_supported)
2197 					status->is_abm_supported = dc->hwss.is_abm_supported(dc, context, params->streams[i]);
2198 				else
2199 					status->is_abm_supported = true;
2200 			}
2201 		}
2202 	}
2203 
2204 fail:
2205 	dc_state_release(context);
2206 
2207 context_alloc_fail:
2208 
2209 	DC_LOG_DC("%s Finished.\n", __func__);
2210 
2211 	return res;
2212 }
2213 
dc_acquire_release_mpc_3dlut(struct dc * dc,bool acquire,struct dc_stream_state * stream,struct dc_3dlut ** lut,struct dc_transfer_func ** shaper)2214 bool dc_acquire_release_mpc_3dlut(
2215 		struct dc *dc, bool acquire,
2216 		struct dc_stream_state *stream,
2217 		struct dc_3dlut **lut,
2218 		struct dc_transfer_func **shaper)
2219 {
2220 	int pipe_idx;
2221 	bool ret = false;
2222 	bool found_pipe_idx = false;
2223 	const struct resource_pool *pool = dc->res_pool;
2224 	struct resource_context *res_ctx = &dc->current_state->res_ctx;
2225 	int mpcc_id = 0;
2226 
2227 	if (pool && res_ctx) {
2228 		if (acquire) {
2229 			/*find pipe idx for the given stream*/
2230 			for (pipe_idx = 0; pipe_idx < pool->pipe_count; pipe_idx++) {
2231 				if (res_ctx->pipe_ctx[pipe_idx].stream == stream) {
2232 					found_pipe_idx = true;
2233 					mpcc_id = res_ctx->pipe_ctx[pipe_idx].plane_res.hubp->inst;
2234 					break;
2235 				}
2236 			}
2237 		} else
2238 			found_pipe_idx = true;/*for release pipe_idx is not required*/
2239 
2240 		if (found_pipe_idx) {
2241 			if (acquire && pool->funcs->acquire_post_bldn_3dlut)
2242 				ret = pool->funcs->acquire_post_bldn_3dlut(res_ctx, pool, mpcc_id, lut, shaper);
2243 			else if (!acquire && pool->funcs->release_post_bldn_3dlut)
2244 				ret = pool->funcs->release_post_bldn_3dlut(res_ctx, pool, lut, shaper);
2245 		}
2246 	}
2247 	return ret;
2248 }
2249 
is_flip_pending_in_pipes(struct dc * dc,struct dc_state * context)2250 static bool is_flip_pending_in_pipes(struct dc *dc, struct dc_state *context)
2251 {
2252 	int i;
2253 	struct pipe_ctx *pipe;
2254 
2255 	for (i = 0; i < MAX_PIPES; i++) {
2256 		pipe = &context->res_ctx.pipe_ctx[i];
2257 
2258 		// Don't check flip pending on phantom pipes
2259 		if (!pipe->plane_state || (dc_state_get_pipe_subvp_type(context, pipe) == SUBVP_PHANTOM))
2260 			continue;
2261 
2262 		/* Must set to false to start with, due to OR in update function */
2263 		pipe->plane_state->status.is_flip_pending = false;
2264 		dc->hwss.update_pending_status(pipe);
2265 		if (pipe->plane_state->status.is_flip_pending)
2266 			return true;
2267 	}
2268 	return false;
2269 }
2270 
2271 /* Perform updates here which need to be deferred until next vupdate
2272  *
2273  * i.e. blnd lut, 3dlut, and shaper lut bypass regs are double buffered
2274  * but forcing lut memory to shutdown state is immediate. This causes
2275  * single frame corruption as lut gets disabled mid-frame unless shutdown
2276  * is deferred until after entering bypass.
2277  */
process_deferred_updates(struct dc * dc)2278 static void process_deferred_updates(struct dc *dc)
2279 {
2280 	int i = 0;
2281 
2282 	if (dc->debug.enable_mem_low_power.bits.cm) {
2283 		ASSERT(dc->dcn_ip->max_num_dpp);
2284 		for (i = 0; i < dc->dcn_ip->max_num_dpp; i++)
2285 			if (dc->res_pool->dpps[i]->funcs->dpp_deferred_update)
2286 				dc->res_pool->dpps[i]->funcs->dpp_deferred_update(dc->res_pool->dpps[i]);
2287 	}
2288 }
2289 
dc_post_update_surfaces_to_stream(struct dc * dc)2290 void dc_post_update_surfaces_to_stream(struct dc *dc)
2291 {
2292 	int i;
2293 	struct dc_state *context = dc->current_state;
2294 
2295 	if ((!dc->optimized_required) || get_seamless_boot_stream_count(context) > 0)
2296 		return;
2297 
2298 	post_surface_trace(dc);
2299 
2300 	/*
2301 	 * Only relevant for DCN behavior where we can guarantee the optimization
2302 	 * is safe to apply - retain the legacy behavior for DCE.
2303 	 */
2304 
2305 	if (dc->ctx->dce_version < DCE_VERSION_MAX)
2306 		TRACE_DCE_CLOCK_STATE(&context->bw_ctx.bw.dce);
2307 	else {
2308 		TRACE_DCN_CLOCK_STATE(&context->bw_ctx.bw.dcn.clk);
2309 
2310 		if (is_flip_pending_in_pipes(dc, context))
2311 			return;
2312 
2313 		for (i = 0; i < dc->res_pool->pipe_count; i++)
2314 			if (context->res_ctx.pipe_ctx[i].stream == NULL ||
2315 					context->res_ctx.pipe_ctx[i].plane_state == NULL) {
2316 				context->res_ctx.pipe_ctx[i].pipe_idx = i;
2317 				dc->hwss.disable_plane(dc, context, &context->res_ctx.pipe_ctx[i]);
2318 			}
2319 
2320 		process_deferred_updates(dc);
2321 
2322 		dc->hwss.optimize_bandwidth(dc, context);
2323 
2324 		if (dc->hwss.update_dsc_pg)
2325 			dc->hwss.update_dsc_pg(dc, context, true);
2326 	}
2327 
2328 	dc->optimized_required = false;
2329 	dc->wm_optimized_required = false;
2330 }
2331 
dc_set_generic_gpio_for_stereo(bool enable,struct gpio_service * gpio_service)2332 bool dc_set_generic_gpio_for_stereo(bool enable,
2333 		struct gpio_service *gpio_service)
2334 {
2335 	enum gpio_result gpio_result = GPIO_RESULT_NON_SPECIFIC_ERROR;
2336 	struct gpio_pin_info pin_info;
2337 	struct gpio *generic;
2338 	struct gpio_generic_mux_config *config = kzalloc(sizeof(struct gpio_generic_mux_config),
2339 			   GFP_KERNEL);
2340 
2341 	if (!config)
2342 		return false;
2343 	pin_info = dal_gpio_get_generic_pin_info(gpio_service, GPIO_ID_GENERIC, 0);
2344 
2345 	if (pin_info.mask == 0xFFFFFFFF || pin_info.offset == 0xFFFFFFFF) {
2346 		kfree(config);
2347 		return false;
2348 	} else {
2349 		generic = dal_gpio_service_create_generic_mux(
2350 			gpio_service,
2351 			pin_info.offset,
2352 			pin_info.mask);
2353 	}
2354 
2355 	if (!generic) {
2356 		kfree(config);
2357 		return false;
2358 	}
2359 
2360 	gpio_result = dal_gpio_open(generic, GPIO_MODE_OUTPUT);
2361 
2362 	config->enable_output_from_mux = enable;
2363 	config->mux_select = GPIO_SIGNAL_SOURCE_PASS_THROUGH_STEREO_SYNC;
2364 
2365 	if (gpio_result == GPIO_RESULT_OK)
2366 		gpio_result = dal_mux_setup_config(generic, config);
2367 
2368 	if (gpio_result == GPIO_RESULT_OK) {
2369 		dal_gpio_close(generic);
2370 		dal_gpio_destroy_generic_mux(&generic);
2371 		kfree(config);
2372 		return true;
2373 	} else {
2374 		dal_gpio_close(generic);
2375 		dal_gpio_destroy_generic_mux(&generic);
2376 		kfree(config);
2377 		return false;
2378 	}
2379 }
2380 
is_surface_in_context(const struct dc_state * context,const struct dc_plane_state * plane_state)2381 static bool is_surface_in_context(
2382 		const struct dc_state *context,
2383 		const struct dc_plane_state *plane_state)
2384 {
2385 	int j;
2386 
2387 	for (j = 0; j < MAX_PIPES; j++) {
2388 		const struct pipe_ctx *pipe_ctx = &context->res_ctx.pipe_ctx[j];
2389 
2390 		if (plane_state == pipe_ctx->plane_state) {
2391 			return true;
2392 		}
2393 	}
2394 
2395 	return false;
2396 }
2397 
get_plane_info_update_type(const struct dc * dc,const struct dc_surface_update * u)2398 static enum surface_update_type get_plane_info_update_type(const struct dc *dc, const struct dc_surface_update *u)
2399 {
2400 	union surface_update_flags *update_flags = &u->surface->update_flags;
2401 	enum surface_update_type update_type = UPDATE_TYPE_FAST;
2402 
2403 	if (!u->plane_info)
2404 		return UPDATE_TYPE_FAST;
2405 
2406 	if (u->plane_info->color_space != u->surface->color_space) {
2407 		update_flags->bits.color_space_change = 1;
2408 		elevate_update_type(&update_type, UPDATE_TYPE_MED);
2409 	}
2410 
2411 	if (u->plane_info->horizontal_mirror != u->surface->horizontal_mirror) {
2412 		update_flags->bits.horizontal_mirror_change = 1;
2413 		elevate_update_type(&update_type, UPDATE_TYPE_MED);
2414 	}
2415 
2416 	if (u->plane_info->rotation != u->surface->rotation) {
2417 		update_flags->bits.rotation_change = 1;
2418 		elevate_update_type(&update_type, UPDATE_TYPE_FULL);
2419 	}
2420 
2421 	if (u->plane_info->format != u->surface->format) {
2422 		update_flags->bits.pixel_format_change = 1;
2423 		elevate_update_type(&update_type, UPDATE_TYPE_FULL);
2424 	}
2425 
2426 	if (u->plane_info->stereo_format != u->surface->stereo_format) {
2427 		update_flags->bits.stereo_format_change = 1;
2428 		elevate_update_type(&update_type, UPDATE_TYPE_FULL);
2429 	}
2430 
2431 	if (u->plane_info->per_pixel_alpha != u->surface->per_pixel_alpha) {
2432 		update_flags->bits.per_pixel_alpha_change = 1;
2433 		elevate_update_type(&update_type, UPDATE_TYPE_MED);
2434 	}
2435 
2436 	if (u->plane_info->global_alpha_value != u->surface->global_alpha_value) {
2437 		update_flags->bits.global_alpha_change = 1;
2438 		elevate_update_type(&update_type, UPDATE_TYPE_MED);
2439 	}
2440 
2441 	if (u->plane_info->dcc.enable != u->surface->dcc.enable
2442 			|| u->plane_info->dcc.dcc_ind_blk != u->surface->dcc.dcc_ind_blk
2443 			|| u->plane_info->dcc.meta_pitch != u->surface->dcc.meta_pitch) {
2444 		/* During DCC on/off, stutter period is calculated before
2445 		 * DCC has fully transitioned. This results in incorrect
2446 		 * stutter period calculation. Triggering a full update will
2447 		 * recalculate stutter period.
2448 		 */
2449 		update_flags->bits.dcc_change = 1;
2450 		elevate_update_type(&update_type, UPDATE_TYPE_FULL);
2451 	}
2452 
2453 	if (resource_pixel_format_to_bpp(u->plane_info->format) !=
2454 			resource_pixel_format_to_bpp(u->surface->format)) {
2455 		/* different bytes per element will require full bandwidth
2456 		 * and DML calculation
2457 		 */
2458 		update_flags->bits.bpp_change = 1;
2459 		elevate_update_type(&update_type, UPDATE_TYPE_FULL);
2460 	}
2461 
2462 	if (u->plane_info->plane_size.surface_pitch != u->surface->plane_size.surface_pitch
2463 			|| u->plane_info->plane_size.chroma_pitch != u->surface->plane_size.chroma_pitch) {
2464 		update_flags->bits.plane_size_change = 1;
2465 		elevate_update_type(&update_type, UPDATE_TYPE_MED);
2466 	}
2467 
2468 
2469 	if (memcmp(&u->plane_info->tiling_info, &u->surface->tiling_info,
2470 			sizeof(union dc_tiling_info)) != 0) {
2471 		update_flags->bits.swizzle_change = 1;
2472 		elevate_update_type(&update_type, UPDATE_TYPE_MED);
2473 
2474 		/* todo: below are HW dependent, we should add a hook to
2475 		 * DCE/N resource and validated there.
2476 		 */
2477 		if (!dc->debug.skip_full_updated_if_possible) {
2478 			/* swizzled mode requires RQ to be setup properly,
2479 			 * thus need to run DML to calculate RQ settings
2480 			 */
2481 			update_flags->bits.bandwidth_change = 1;
2482 			elevate_update_type(&update_type, UPDATE_TYPE_FULL);
2483 		}
2484 	}
2485 
2486 	/* This should be UPDATE_TYPE_FAST if nothing has changed. */
2487 	return update_type;
2488 }
2489 
get_scaling_info_update_type(const struct dc * dc,const struct dc_surface_update * u)2490 static enum surface_update_type get_scaling_info_update_type(
2491 		const struct dc *dc,
2492 		const struct dc_surface_update *u)
2493 {
2494 	union surface_update_flags *update_flags = &u->surface->update_flags;
2495 
2496 	if (!u->scaling_info)
2497 		return UPDATE_TYPE_FAST;
2498 
2499 	if (u->scaling_info->dst_rect.width != u->surface->dst_rect.width
2500 			|| u->scaling_info->dst_rect.height != u->surface->dst_rect.height
2501 			|| u->scaling_info->scaling_quality.integer_scaling !=
2502 				u->surface->scaling_quality.integer_scaling
2503 			) {
2504 		update_flags->bits.scaling_change = 1;
2505 
2506 		if ((u->scaling_info->dst_rect.width < u->surface->dst_rect.width
2507 			|| u->scaling_info->dst_rect.height < u->surface->dst_rect.height)
2508 				&& (u->scaling_info->dst_rect.width < u->surface->src_rect.width
2509 					|| u->scaling_info->dst_rect.height < u->surface->src_rect.height))
2510 			/* Making dst rect smaller requires a bandwidth change */
2511 			update_flags->bits.bandwidth_change = 1;
2512 	}
2513 
2514 	if (u->scaling_info->src_rect.width != u->surface->src_rect.width
2515 		|| u->scaling_info->src_rect.height != u->surface->src_rect.height) {
2516 
2517 		update_flags->bits.scaling_change = 1;
2518 		if (u->scaling_info->src_rect.width > u->surface->src_rect.width
2519 				|| u->scaling_info->src_rect.height > u->surface->src_rect.height)
2520 			/* Making src rect bigger requires a bandwidth change */
2521 			update_flags->bits.clock_change = 1;
2522 	}
2523 
2524 	if (u->scaling_info->src_rect.width > dc->caps.max_optimizable_video_width &&
2525 		(u->scaling_info->clip_rect.width > u->surface->clip_rect.width ||
2526 		 u->scaling_info->clip_rect.height > u->surface->clip_rect.height))
2527 		 /* Changing clip size of a large surface may result in MPC slice count change */
2528 		update_flags->bits.bandwidth_change = 1;
2529 
2530 	if (u->scaling_info->clip_rect.width != u->surface->clip_rect.width ||
2531 			u->scaling_info->clip_rect.height != u->surface->clip_rect.height)
2532 		update_flags->bits.clip_size_change = 1;
2533 
2534 	if (u->scaling_info->src_rect.x != u->surface->src_rect.x
2535 			|| u->scaling_info->src_rect.y != u->surface->src_rect.y
2536 			|| u->scaling_info->clip_rect.x != u->surface->clip_rect.x
2537 			|| u->scaling_info->clip_rect.y != u->surface->clip_rect.y
2538 			|| u->scaling_info->dst_rect.x != u->surface->dst_rect.x
2539 			|| u->scaling_info->dst_rect.y != u->surface->dst_rect.y)
2540 		update_flags->bits.position_change = 1;
2541 
2542 	if (update_flags->bits.clock_change
2543 			|| update_flags->bits.bandwidth_change
2544 			|| update_flags->bits.scaling_change)
2545 		return UPDATE_TYPE_FULL;
2546 
2547 	if (update_flags->bits.position_change ||
2548 			update_flags->bits.clip_size_change)
2549 		return UPDATE_TYPE_MED;
2550 
2551 	return UPDATE_TYPE_FAST;
2552 }
2553 
det_surface_update(const struct dc * dc,const struct dc_surface_update * u)2554 static enum surface_update_type det_surface_update(const struct dc *dc,
2555 		const struct dc_surface_update *u)
2556 {
2557 	const struct dc_state *context = dc->current_state;
2558 	enum surface_update_type type;
2559 	enum surface_update_type overall_type = UPDATE_TYPE_FAST;
2560 	union surface_update_flags *update_flags = &u->surface->update_flags;
2561 
2562 	if (!is_surface_in_context(context, u->surface) || u->surface->force_full_update) {
2563 		update_flags->raw = 0xFFFFFFFF;
2564 		return UPDATE_TYPE_FULL;
2565 	}
2566 
2567 	update_flags->raw = 0; // Reset all flags
2568 
2569 	type = get_plane_info_update_type(dc, u);
2570 	elevate_update_type(&overall_type, type);
2571 
2572 	type = get_scaling_info_update_type(dc, u);
2573 	elevate_update_type(&overall_type, type);
2574 
2575 	if (u->flip_addr) {
2576 		update_flags->bits.addr_update = 1;
2577 		if (u->flip_addr->address.tmz_surface != u->surface->address.tmz_surface) {
2578 			update_flags->bits.tmz_changed = 1;
2579 			elevate_update_type(&overall_type, UPDATE_TYPE_FULL);
2580 		}
2581 	}
2582 	if (u->in_transfer_func)
2583 		update_flags->bits.in_transfer_func_change = 1;
2584 
2585 	if (u->input_csc_color_matrix)
2586 		update_flags->bits.input_csc_change = 1;
2587 
2588 	if (u->coeff_reduction_factor)
2589 		update_flags->bits.coeff_reduction_change = 1;
2590 
2591 	if (u->gamut_remap_matrix)
2592 		update_flags->bits.gamut_remap_change = 1;
2593 
2594 	if (u->blend_tf)
2595 		update_flags->bits.gamma_change = 1;
2596 
2597 	if (u->gamma) {
2598 		enum surface_pixel_format format = SURFACE_PIXEL_FORMAT_GRPH_BEGIN;
2599 
2600 		if (u->plane_info)
2601 			format = u->plane_info->format;
2602 		else
2603 			format = u->surface->format;
2604 
2605 		if (dce_use_lut(format))
2606 			update_flags->bits.gamma_change = 1;
2607 	}
2608 
2609 	if (u->lut3d_func || u->func_shaper)
2610 		update_flags->bits.lut_3d = 1;
2611 
2612 	if (u->hdr_mult.value)
2613 		if (u->hdr_mult.value != u->surface->hdr_mult.value) {
2614 			update_flags->bits.hdr_mult = 1;
2615 			elevate_update_type(&overall_type, UPDATE_TYPE_MED);
2616 		}
2617 
2618 	if (u->sdr_white_level_nits)
2619 		if (u->sdr_white_level_nits != u->surface->sdr_white_level_nits) {
2620 			update_flags->bits.sdr_white_level_nits = 1;
2621 			elevate_update_type(&overall_type, UPDATE_TYPE_FULL);
2622 		}
2623 
2624 	if (u->cm2_params) {
2625 		if ((u->cm2_params->component_settings.shaper_3dlut_setting
2626 					!= u->surface->mcm_shaper_3dlut_setting)
2627 				|| (u->cm2_params->component_settings.lut1d_enable
2628 					!= u->surface->mcm_lut1d_enable))
2629 			update_flags->bits.mcm_transfer_function_enable_change = 1;
2630 		if (u->cm2_params->cm2_luts.lut3d_data.lut3d_src
2631 				!= u->surface->mcm_luts.lut3d_data.lut3d_src)
2632 			update_flags->bits.mcm_transfer_function_enable_change = 1;
2633 	}
2634 	if (update_flags->bits.in_transfer_func_change) {
2635 		type = UPDATE_TYPE_MED;
2636 		elevate_update_type(&overall_type, type);
2637 	}
2638 
2639 	if (update_flags->bits.lut_3d) {
2640 		type = UPDATE_TYPE_FULL;
2641 		elevate_update_type(&overall_type, type);
2642 	}
2643 	if (update_flags->bits.mcm_transfer_function_enable_change) {
2644 		type = UPDATE_TYPE_FULL;
2645 		elevate_update_type(&overall_type, type);
2646 	}
2647 
2648 	if (dc->debug.enable_legacy_fast_update &&
2649 			(update_flags->bits.gamma_change ||
2650 			update_flags->bits.gamut_remap_change ||
2651 			update_flags->bits.input_csc_change ||
2652 			update_flags->bits.coeff_reduction_change)) {
2653 		type = UPDATE_TYPE_FULL;
2654 		elevate_update_type(&overall_type, type);
2655 	}
2656 	return overall_type;
2657 }
2658 
check_update_surfaces_for_stream(struct dc * dc,struct dc_surface_update * updates,int surface_count,struct dc_stream_update * stream_update,const struct dc_stream_status * stream_status)2659 static enum surface_update_type check_update_surfaces_for_stream(
2660 		struct dc *dc,
2661 		struct dc_surface_update *updates,
2662 		int surface_count,
2663 		struct dc_stream_update *stream_update,
2664 		const struct dc_stream_status *stream_status)
2665 {
2666 	int i;
2667 	enum surface_update_type overall_type = UPDATE_TYPE_FAST;
2668 
2669 	if (dc->idle_optimizations_allowed)
2670 		overall_type = UPDATE_TYPE_FULL;
2671 
2672 	if (stream_status == NULL || stream_status->plane_count != surface_count)
2673 		overall_type = UPDATE_TYPE_FULL;
2674 
2675 	if (stream_update && stream_update->pending_test_pattern) {
2676 		overall_type = UPDATE_TYPE_FULL;
2677 	}
2678 
2679 	if (stream_update && stream_update->hw_cursor_req) {
2680 		overall_type = UPDATE_TYPE_FULL;
2681 	}
2682 
2683 	/* some stream updates require passive update */
2684 	if (stream_update) {
2685 		union stream_update_flags *su_flags = &stream_update->stream->update_flags;
2686 
2687 		if ((stream_update->src.height != 0 && stream_update->src.width != 0) ||
2688 			(stream_update->dst.height != 0 && stream_update->dst.width != 0) ||
2689 			stream_update->integer_scaling_update)
2690 			su_flags->bits.scaling = 1;
2691 
2692 		if (dc->debug.enable_legacy_fast_update && stream_update->out_transfer_func)
2693 			su_flags->bits.out_tf = 1;
2694 
2695 		if (stream_update->abm_level)
2696 			su_flags->bits.abm_level = 1;
2697 
2698 		if (stream_update->dpms_off)
2699 			su_flags->bits.dpms_off = 1;
2700 
2701 		if (stream_update->gamut_remap)
2702 			su_flags->bits.gamut_remap = 1;
2703 
2704 		if (stream_update->wb_update)
2705 			su_flags->bits.wb_update = 1;
2706 
2707 		if (stream_update->dsc_config)
2708 			su_flags->bits.dsc_changed = 1;
2709 
2710 		if (stream_update->mst_bw_update)
2711 			su_flags->bits.mst_bw = 1;
2712 
2713 		if (stream_update->stream->freesync_on_desktop &&
2714 			(stream_update->vrr_infopacket || stream_update->allow_freesync ||
2715 				stream_update->vrr_active_variable || stream_update->vrr_active_fixed))
2716 			su_flags->bits.fams_changed = 1;
2717 
2718 		if (stream_update->scaler_sharpener_update)
2719 			su_flags->bits.scaler_sharpener = 1;
2720 
2721 		if (su_flags->raw != 0)
2722 			overall_type = UPDATE_TYPE_FULL;
2723 
2724 		if (stream_update->output_csc_transform || stream_update->output_color_space)
2725 			su_flags->bits.out_csc = 1;
2726 
2727 		/* Output transfer function changes do not require bandwidth recalculation,
2728 		 * so don't trigger a full update
2729 		 */
2730 		if (!dc->debug.enable_legacy_fast_update && stream_update->out_transfer_func)
2731 			su_flags->bits.out_tf = 1;
2732 	}
2733 
2734 	for (i = 0 ; i < surface_count; i++) {
2735 		enum surface_update_type type =
2736 				det_surface_update(dc, &updates[i]);
2737 
2738 		elevate_update_type(&overall_type, type);
2739 	}
2740 
2741 	return overall_type;
2742 }
2743 
2744 /*
2745  * dc_check_update_surfaces_for_stream() - Determine update type (fast, med, or full)
2746  *
2747  * See :c:type:`enum surface_update_type <surface_update_type>` for explanation of update types
2748  */
dc_check_update_surfaces_for_stream(struct dc * dc,struct dc_surface_update * updates,int surface_count,struct dc_stream_update * stream_update,const struct dc_stream_status * stream_status)2749 enum surface_update_type dc_check_update_surfaces_for_stream(
2750 		struct dc *dc,
2751 		struct dc_surface_update *updates,
2752 		int surface_count,
2753 		struct dc_stream_update *stream_update,
2754 		const struct dc_stream_status *stream_status)
2755 {
2756 	int i;
2757 	enum surface_update_type type;
2758 
2759 	if (stream_update)
2760 		stream_update->stream->update_flags.raw = 0;
2761 	for (i = 0; i < surface_count; i++)
2762 		updates[i].surface->update_flags.raw = 0;
2763 
2764 	type = check_update_surfaces_for_stream(dc, updates, surface_count, stream_update, stream_status);
2765 	if (type == UPDATE_TYPE_FULL) {
2766 		if (stream_update) {
2767 			uint32_t dsc_changed = stream_update->stream->update_flags.bits.dsc_changed;
2768 			stream_update->stream->update_flags.raw = 0xFFFFFFFF;
2769 			stream_update->stream->update_flags.bits.dsc_changed = dsc_changed;
2770 		}
2771 		for (i = 0; i < surface_count; i++)
2772 			updates[i].surface->update_flags.raw = 0xFFFFFFFF;
2773 	}
2774 
2775 	if (type == UPDATE_TYPE_FAST) {
2776 		// If there's an available clock comparator, we use that.
2777 		if (dc->clk_mgr->funcs->are_clock_states_equal) {
2778 			if (!dc->clk_mgr->funcs->are_clock_states_equal(&dc->clk_mgr->clks, &dc->current_state->bw_ctx.bw.dcn.clk))
2779 				dc->optimized_required = true;
2780 		// Else we fallback to mem compare.
2781 		} else if (memcmp(&dc->current_state->bw_ctx.bw.dcn.clk, &dc->clk_mgr->clks, offsetof(struct dc_clocks, prev_p_state_change_support)) != 0) {
2782 			dc->optimized_required = true;
2783 		}
2784 
2785 		dc->optimized_required |= dc->wm_optimized_required;
2786 	}
2787 
2788 	return type;
2789 }
2790 
stream_get_status(struct dc_state * ctx,struct dc_stream_state * stream)2791 static struct dc_stream_status *stream_get_status(
2792 	struct dc_state *ctx,
2793 	struct dc_stream_state *stream)
2794 {
2795 	uint8_t i;
2796 
2797 	for (i = 0; i < ctx->stream_count; i++) {
2798 		if (stream == ctx->streams[i]) {
2799 			return &ctx->stream_status[i];
2800 		}
2801 	}
2802 
2803 	return NULL;
2804 }
2805 
2806 static const enum surface_update_type update_surface_trace_level = UPDATE_TYPE_FULL;
2807 
copy_surface_update_to_plane(struct dc_plane_state * surface,struct dc_surface_update * srf_update)2808 static void copy_surface_update_to_plane(
2809 		struct dc_plane_state *surface,
2810 		struct dc_surface_update *srf_update)
2811 {
2812 	if (srf_update->flip_addr) {
2813 		surface->address = srf_update->flip_addr->address;
2814 		surface->flip_immediate =
2815 			srf_update->flip_addr->flip_immediate;
2816 		surface->time.time_elapsed_in_us[surface->time.index] =
2817 			srf_update->flip_addr->flip_timestamp_in_us -
2818 				surface->time.prev_update_time_in_us;
2819 		surface->time.prev_update_time_in_us =
2820 			srf_update->flip_addr->flip_timestamp_in_us;
2821 		surface->time.index++;
2822 		if (surface->time.index >= DC_PLANE_UPDATE_TIMES_MAX)
2823 			surface->time.index = 0;
2824 
2825 		surface->triplebuffer_flips = srf_update->flip_addr->triplebuffer_flips;
2826 	}
2827 
2828 	if (srf_update->scaling_info) {
2829 		surface->scaling_quality =
2830 				srf_update->scaling_info->scaling_quality;
2831 		surface->dst_rect =
2832 				srf_update->scaling_info->dst_rect;
2833 		surface->src_rect =
2834 				srf_update->scaling_info->src_rect;
2835 		surface->clip_rect =
2836 				srf_update->scaling_info->clip_rect;
2837 	}
2838 
2839 	if (srf_update->plane_info) {
2840 		surface->color_space =
2841 				srf_update->plane_info->color_space;
2842 		surface->format =
2843 				srf_update->plane_info->format;
2844 		surface->plane_size =
2845 				srf_update->plane_info->plane_size;
2846 		surface->rotation =
2847 				srf_update->plane_info->rotation;
2848 		surface->horizontal_mirror =
2849 				srf_update->plane_info->horizontal_mirror;
2850 		surface->stereo_format =
2851 				srf_update->plane_info->stereo_format;
2852 		surface->tiling_info =
2853 				srf_update->plane_info->tiling_info;
2854 		surface->visible =
2855 				srf_update->plane_info->visible;
2856 		surface->per_pixel_alpha =
2857 				srf_update->plane_info->per_pixel_alpha;
2858 		surface->global_alpha =
2859 				srf_update->plane_info->global_alpha;
2860 		surface->global_alpha_value =
2861 				srf_update->plane_info->global_alpha_value;
2862 		surface->dcc =
2863 				srf_update->plane_info->dcc;
2864 		surface->layer_index =
2865 				srf_update->plane_info->layer_index;
2866 	}
2867 
2868 	if (srf_update->gamma) {
2869 		memcpy(&surface->gamma_correction.entries,
2870 			&srf_update->gamma->entries,
2871 			sizeof(struct dc_gamma_entries));
2872 		surface->gamma_correction.is_identity =
2873 			srf_update->gamma->is_identity;
2874 		surface->gamma_correction.num_entries =
2875 			srf_update->gamma->num_entries;
2876 		surface->gamma_correction.type =
2877 			srf_update->gamma->type;
2878 	}
2879 
2880 	if (srf_update->in_transfer_func) {
2881 		surface->in_transfer_func.sdr_ref_white_level =
2882 			srf_update->in_transfer_func->sdr_ref_white_level;
2883 		surface->in_transfer_func.tf =
2884 			srf_update->in_transfer_func->tf;
2885 		surface->in_transfer_func.type =
2886 			srf_update->in_transfer_func->type;
2887 		memcpy(&surface->in_transfer_func.tf_pts,
2888 			&srf_update->in_transfer_func->tf_pts,
2889 			sizeof(struct dc_transfer_func_distributed_points));
2890 	}
2891 
2892 	if (srf_update->func_shaper)
2893 		memcpy(&surface->in_shaper_func, srf_update->func_shaper,
2894 		sizeof(surface->in_shaper_func));
2895 
2896 	if (srf_update->lut3d_func)
2897 		memcpy(&surface->lut3d_func, srf_update->lut3d_func,
2898 		sizeof(surface->lut3d_func));
2899 
2900 	if (srf_update->hdr_mult.value)
2901 		surface->hdr_mult =
2902 				srf_update->hdr_mult;
2903 
2904 	if (srf_update->sdr_white_level_nits)
2905 		surface->sdr_white_level_nits =
2906 				srf_update->sdr_white_level_nits;
2907 
2908 	if (srf_update->blend_tf)
2909 		memcpy(&surface->blend_tf, srf_update->blend_tf,
2910 		sizeof(surface->blend_tf));
2911 
2912 	if (srf_update->input_csc_color_matrix)
2913 		surface->input_csc_color_matrix =
2914 			*srf_update->input_csc_color_matrix;
2915 
2916 	if (srf_update->coeff_reduction_factor)
2917 		surface->coeff_reduction_factor =
2918 			*srf_update->coeff_reduction_factor;
2919 
2920 	if (srf_update->gamut_remap_matrix)
2921 		surface->gamut_remap_matrix =
2922 			*srf_update->gamut_remap_matrix;
2923 	if (srf_update->cm2_params) {
2924 		surface->mcm_shaper_3dlut_setting = srf_update->cm2_params->component_settings.shaper_3dlut_setting;
2925 		surface->mcm_lut1d_enable = srf_update->cm2_params->component_settings.lut1d_enable;
2926 		surface->mcm_luts = srf_update->cm2_params->cm2_luts;
2927 	}
2928 	if (srf_update->cursor_csc_color_matrix)
2929 		surface->cursor_csc_color_matrix =
2930 			*srf_update->cursor_csc_color_matrix;
2931 }
2932 
copy_stream_update_to_stream(struct dc * dc,struct dc_state * context,struct dc_stream_state * stream,struct dc_stream_update * update)2933 static void copy_stream_update_to_stream(struct dc *dc,
2934 					 struct dc_state *context,
2935 					 struct dc_stream_state *stream,
2936 					 struct dc_stream_update *update)
2937 {
2938 	struct dc_context *dc_ctx = dc->ctx;
2939 
2940 	if (update == NULL || stream == NULL)
2941 		return;
2942 
2943 	if (update->src.height && update->src.width)
2944 		stream->src = update->src;
2945 
2946 	if (update->dst.height && update->dst.width)
2947 		stream->dst = update->dst;
2948 
2949 	if (update->out_transfer_func) {
2950 		stream->out_transfer_func.sdr_ref_white_level =
2951 			update->out_transfer_func->sdr_ref_white_level;
2952 		stream->out_transfer_func.tf = update->out_transfer_func->tf;
2953 		stream->out_transfer_func.type =
2954 			update->out_transfer_func->type;
2955 		memcpy(&stream->out_transfer_func.tf_pts,
2956 		       &update->out_transfer_func->tf_pts,
2957 		       sizeof(struct dc_transfer_func_distributed_points));
2958 	}
2959 
2960 	if (update->hdr_static_metadata)
2961 		stream->hdr_static_metadata = *update->hdr_static_metadata;
2962 
2963 	if (update->abm_level)
2964 		stream->abm_level = *update->abm_level;
2965 
2966 	if (update->periodic_interrupt)
2967 		stream->periodic_interrupt = *update->periodic_interrupt;
2968 
2969 	if (update->gamut_remap)
2970 		stream->gamut_remap_matrix = *update->gamut_remap;
2971 
2972 	/* Note: this being updated after mode set is currently not a use case
2973 	 * however if it arises OCSC would need to be reprogrammed at the
2974 	 * minimum
2975 	 */
2976 	if (update->output_color_space)
2977 		stream->output_color_space = *update->output_color_space;
2978 
2979 	if (update->output_csc_transform)
2980 		stream->csc_color_matrix = *update->output_csc_transform;
2981 
2982 	if (update->vrr_infopacket)
2983 		stream->vrr_infopacket = *update->vrr_infopacket;
2984 
2985 	if (update->hw_cursor_req)
2986 		stream->hw_cursor_req = *update->hw_cursor_req;
2987 
2988 	if (update->allow_freesync)
2989 		stream->allow_freesync = *update->allow_freesync;
2990 
2991 	if (update->vrr_active_variable)
2992 		stream->vrr_active_variable = *update->vrr_active_variable;
2993 
2994 	if (update->vrr_active_fixed)
2995 		stream->vrr_active_fixed = *update->vrr_active_fixed;
2996 
2997 	if (update->crtc_timing_adjust) {
2998 		if (stream->adjust.v_total_min != update->crtc_timing_adjust->v_total_min ||
2999 			stream->adjust.v_total_max != update->crtc_timing_adjust->v_total_max ||
3000 			stream->adjust.timing_adjust_pending)
3001 			update->crtc_timing_adjust->timing_adjust_pending = true;
3002 		stream->adjust = *update->crtc_timing_adjust;
3003 		update->crtc_timing_adjust->timing_adjust_pending = false;
3004 	}
3005 
3006 	if (update->dpms_off)
3007 		stream->dpms_off = *update->dpms_off;
3008 
3009 	if (update->hfvsif_infopacket)
3010 		stream->hfvsif_infopacket = *update->hfvsif_infopacket;
3011 
3012 	if (update->vtem_infopacket)
3013 		stream->vtem_infopacket = *update->vtem_infopacket;
3014 
3015 	if (update->vsc_infopacket)
3016 		stream->vsc_infopacket = *update->vsc_infopacket;
3017 
3018 	if (update->vsp_infopacket)
3019 		stream->vsp_infopacket = *update->vsp_infopacket;
3020 
3021 	if (update->adaptive_sync_infopacket)
3022 		stream->adaptive_sync_infopacket = *update->adaptive_sync_infopacket;
3023 
3024 	if (update->dither_option)
3025 		stream->dither_option = *update->dither_option;
3026 
3027 	if (update->pending_test_pattern)
3028 		stream->test_pattern = *update->pending_test_pattern;
3029 	/* update current stream with writeback info */
3030 	if (update->wb_update) {
3031 		int i;
3032 
3033 		stream->num_wb_info = update->wb_update->num_wb_info;
3034 		ASSERT(stream->num_wb_info <= MAX_DWB_PIPES);
3035 		for (i = 0; i < stream->num_wb_info; i++)
3036 			stream->writeback_info[i] =
3037 				update->wb_update->writeback_info[i];
3038 	}
3039 	if (update->dsc_config) {
3040 		struct dc_dsc_config old_dsc_cfg = stream->timing.dsc_cfg;
3041 		uint32_t old_dsc_enabled = stream->timing.flags.DSC;
3042 		uint32_t enable_dsc = (update->dsc_config->num_slices_h != 0 &&
3043 				       update->dsc_config->num_slices_v != 0);
3044 
3045 		/* Use temporarry context for validating new DSC config */
3046 		struct dc_state *dsc_validate_context = dc_state_create_copy(dc->current_state);
3047 
3048 		if (dsc_validate_context) {
3049 			stream->timing.dsc_cfg = *update->dsc_config;
3050 			stream->timing.flags.DSC = enable_dsc;
3051 			if (!dc->res_pool->funcs->validate_bandwidth(dc, dsc_validate_context, true)) {
3052 				stream->timing.dsc_cfg = old_dsc_cfg;
3053 				stream->timing.flags.DSC = old_dsc_enabled;
3054 				update->dsc_config = NULL;
3055 			}
3056 
3057 			dc_state_release(dsc_validate_context);
3058 		} else {
3059 			DC_ERROR("Failed to allocate new validate context for DSC change\n");
3060 			update->dsc_config = NULL;
3061 		}
3062 	}
3063 	if (update->scaler_sharpener_update)
3064 		stream->scaler_sharpener_update = *update->scaler_sharpener_update;
3065 }
3066 
backup_planes_and_stream_state(struct dc_scratch_space * scratch,struct dc_stream_state * stream)3067 static void backup_planes_and_stream_state(
3068 		struct dc_scratch_space *scratch,
3069 		struct dc_stream_state *stream)
3070 {
3071 	int i;
3072 	struct dc_stream_status *status = dc_stream_get_status(stream);
3073 
3074 	if (!status)
3075 		return;
3076 
3077 	for (i = 0; i < status->plane_count; i++) {
3078 		scratch->plane_states[i] = *status->plane_states[i];
3079 	}
3080 	scratch->stream_state = *stream;
3081 }
3082 
restore_planes_and_stream_state(struct dc_scratch_space * scratch,struct dc_stream_state * stream)3083 static void restore_planes_and_stream_state(
3084 		struct dc_scratch_space *scratch,
3085 		struct dc_stream_state *stream)
3086 {
3087 	int i;
3088 	struct dc_stream_status *status = dc_stream_get_status(stream);
3089 
3090 	if (!status)
3091 		return;
3092 
3093 	for (i = 0; i < status->plane_count; i++) {
3094 		/* refcount will always be valid, restore everything else */
3095 		struct kref refcount = status->plane_states[i]->refcount;
3096 		*status->plane_states[i] = scratch->plane_states[i];
3097 		status->plane_states[i]->refcount = refcount;
3098 	}
3099 	*stream = scratch->stream_state;
3100 }
3101 
3102 /**
3103  * update_seamless_boot_flags() - Helper function for updating seamless boot flags
3104  *
3105  * @dc: Current DC state
3106  * @context: New DC state to be programmed
3107  * @surface_count: Number of surfaces that have an updated
3108  * @stream: Corresponding stream to be updated in the current flip
3109  *
3110  * Updating seamless boot flags do not need to be part of the commit sequence. This
3111  * helper function will update the seamless boot flags on each flip (if required)
3112  * outside of the HW commit sequence (fast or slow).
3113  *
3114  * Return: void
3115  */
update_seamless_boot_flags(struct dc * dc,struct dc_state * context,int surface_count,struct dc_stream_state * stream)3116 static void update_seamless_boot_flags(struct dc *dc,
3117 		struct dc_state *context,
3118 		int surface_count,
3119 		struct dc_stream_state *stream)
3120 {
3121 	if (get_seamless_boot_stream_count(context) > 0 && surface_count > 0) {
3122 		/* Optimize seamless boot flag keeps clocks and watermarks high until
3123 		 * first flip. After first flip, optimization is required to lower
3124 		 * bandwidth. Important to note that it is expected UEFI will
3125 		 * only light up a single display on POST, therefore we only expect
3126 		 * one stream with seamless boot flag set.
3127 		 */
3128 		if (stream->apply_seamless_boot_optimization) {
3129 			stream->apply_seamless_boot_optimization = false;
3130 
3131 			if (get_seamless_boot_stream_count(context) == 0)
3132 				dc->optimized_required = true;
3133 		}
3134 	}
3135 }
3136 
3137 /**
3138  * update_planes_and_stream_state() - The function takes planes and stream
3139  * updates as inputs and determines the appropriate update type. If update type
3140  * is FULL, the function allocates a new context, populates and validates it.
3141  * Otherwise, it updates current dc context. The function will return both
3142  * new_context and new_update_type back to the caller. The function also backs
3143  * up both current and new contexts into corresponding dc state scratch memory.
3144  * TODO: The function does too many things, and even conditionally allocates dc
3145  * context memory implicitly. We should consider to break it down.
3146  *
3147  * @dc: Current DC state
3148  * @srf_updates: an array of surface updates
3149  * @surface_count: surface update count
3150  * @stream: Corresponding stream to be updated
3151  * @stream_update: stream update
3152  * @new_update_type: [out] determined update type by the function
3153  * @new_context: [out] new context allocated and validated if update type is
3154  * FULL, reference to current context if update type is less than FULL.
3155  *
3156  * Return: true if a valid update is populated into new_context, false
3157  * otherwise.
3158  */
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 surface_update_type * new_update_type,struct dc_state ** new_context)3159 static bool update_planes_and_stream_state(struct dc *dc,
3160 		struct dc_surface_update *srf_updates, int surface_count,
3161 		struct dc_stream_state *stream,
3162 		struct dc_stream_update *stream_update,
3163 		enum surface_update_type *new_update_type,
3164 		struct dc_state **new_context)
3165 {
3166 	struct dc_state *context;
3167 	int i, j;
3168 	enum surface_update_type update_type;
3169 	const struct dc_stream_status *stream_status;
3170 	struct dc_context *dc_ctx = dc->ctx;
3171 
3172 	stream_status = dc_stream_get_status(stream);
3173 
3174 	if (!stream_status) {
3175 		if (surface_count) /* Only an error condition if surf_count non-zero*/
3176 			ASSERT(false);
3177 
3178 		return false; /* Cannot commit surface to stream that is not committed */
3179 	}
3180 
3181 	context = dc->current_state;
3182 	update_type = dc_check_update_surfaces_for_stream(
3183 			dc, srf_updates, surface_count, stream_update, stream_status);
3184 	if (update_type == UPDATE_TYPE_FULL)
3185 		backup_planes_and_stream_state(&dc->scratch.current_state, stream);
3186 
3187 	/* update current stream with the new updates */
3188 	copy_stream_update_to_stream(dc, context, stream, stream_update);
3189 
3190 	/* do not perform surface update if surface has invalid dimensions
3191 	 * (all zero) and no scaling_info is provided
3192 	 */
3193 	if (surface_count > 0) {
3194 		for (i = 0; i < surface_count; i++) {
3195 			if ((srf_updates[i].surface->src_rect.width == 0 ||
3196 				 srf_updates[i].surface->src_rect.height == 0 ||
3197 				 srf_updates[i].surface->dst_rect.width == 0 ||
3198 				 srf_updates[i].surface->dst_rect.height == 0) &&
3199 				(!srf_updates[i].scaling_info ||
3200 				  srf_updates[i].scaling_info->src_rect.width == 0 ||
3201 				  srf_updates[i].scaling_info->src_rect.height == 0 ||
3202 				  srf_updates[i].scaling_info->dst_rect.width == 0 ||
3203 				  srf_updates[i].scaling_info->dst_rect.height == 0)) {
3204 				DC_ERROR("Invalid src/dst rects in surface update!\n");
3205 				return false;
3206 			}
3207 		}
3208 	}
3209 
3210 	if (update_type >= update_surface_trace_level)
3211 		update_surface_trace(dc, srf_updates, surface_count);
3212 
3213 	for (i = 0; i < surface_count; i++)
3214 		copy_surface_update_to_plane(srf_updates[i].surface, &srf_updates[i]);
3215 
3216 	if (update_type >= UPDATE_TYPE_FULL) {
3217 		struct dc_plane_state *new_planes[MAX_SURFACES] = {0};
3218 
3219 		for (i = 0; i < surface_count; i++)
3220 			new_planes[i] = srf_updates[i].surface;
3221 
3222 		/* initialize scratch memory for building context */
3223 		context = dc_state_create_copy(dc->current_state);
3224 		if (context == NULL) {
3225 			DC_ERROR("Failed to allocate new validate context!\n");
3226 			return false;
3227 		}
3228 
3229 		/* For each full update, remove all existing phantom pipes first.
3230 		 * Ensures that we have enough pipes for newly added MPO planes
3231 		 */
3232 		dc_state_remove_phantom_streams_and_planes(dc, context);
3233 		dc_state_release_phantom_streams_and_planes(dc, context);
3234 
3235 		/*remove old surfaces from context */
3236 		if (!dc_state_rem_all_planes_for_stream(dc, stream, context)) {
3237 
3238 			BREAK_TO_DEBUGGER();
3239 			goto fail;
3240 		}
3241 
3242 		/* add surface to context */
3243 		if (!dc_state_add_all_planes_for_stream(dc, stream, new_planes, surface_count, context)) {
3244 
3245 			BREAK_TO_DEBUGGER();
3246 			goto fail;
3247 		}
3248 	}
3249 
3250 	/* save update parameters into surface */
3251 	for (i = 0; i < surface_count; i++) {
3252 		struct dc_plane_state *surface = srf_updates[i].surface;
3253 
3254 		if (update_type != UPDATE_TYPE_MED)
3255 			continue;
3256 		if (surface->update_flags.bits.clip_size_change ||
3257 				surface->update_flags.bits.position_change) {
3258 			for (j = 0; j < dc->res_pool->pipe_count; j++) {
3259 				struct pipe_ctx *pipe_ctx = &context->res_ctx.pipe_ctx[j];
3260 
3261 				if (pipe_ctx->plane_state != surface)
3262 					continue;
3263 
3264 				resource_build_scaling_params(pipe_ctx);
3265 			}
3266 		}
3267 	}
3268 
3269 	if (update_type == UPDATE_TYPE_FULL) {
3270 		if (!dc->res_pool->funcs->validate_bandwidth(dc, context, false)) {
3271 			BREAK_TO_DEBUGGER();
3272 			goto fail;
3273 		}
3274 	}
3275 	update_seamless_boot_flags(dc, context, surface_count, stream);
3276 
3277 	*new_context = context;
3278 	*new_update_type = update_type;
3279 	if (update_type == UPDATE_TYPE_FULL)
3280 		backup_planes_and_stream_state(&dc->scratch.new_state, stream);
3281 
3282 	return true;
3283 
3284 fail:
3285 	dc_state_release(context);
3286 
3287 	return false;
3288 
3289 }
3290 
commit_planes_do_stream_update(struct dc * dc,struct dc_stream_state * stream,struct dc_stream_update * stream_update,enum surface_update_type update_type,struct dc_state * context)3291 static void commit_planes_do_stream_update(struct dc *dc,
3292 		struct dc_stream_state *stream,
3293 		struct dc_stream_update *stream_update,
3294 		enum surface_update_type update_type,
3295 		struct dc_state *context)
3296 {
3297 	int j;
3298 
3299 	// Stream updates
3300 	for (j = 0; j < dc->res_pool->pipe_count; j++) {
3301 		struct pipe_ctx *pipe_ctx = &context->res_ctx.pipe_ctx[j];
3302 
3303 		if (resource_is_pipe_type(pipe_ctx, OTG_MASTER) && pipe_ctx->stream == stream) {
3304 
3305 			if (stream_update->periodic_interrupt && dc->hwss.setup_periodic_interrupt)
3306 				dc->hwss.setup_periodic_interrupt(dc, pipe_ctx);
3307 
3308 			if ((stream_update->hdr_static_metadata && !stream->use_dynamic_meta) ||
3309 					stream_update->vrr_infopacket ||
3310 					stream_update->vsc_infopacket ||
3311 					stream_update->vsp_infopacket ||
3312 					stream_update->hfvsif_infopacket ||
3313 					stream_update->adaptive_sync_infopacket ||
3314 					stream_update->vtem_infopacket) {
3315 				resource_build_info_frame(pipe_ctx);
3316 				dc->hwss.update_info_frame(pipe_ctx);
3317 
3318 				if (dc_is_dp_signal(pipe_ctx->stream->signal))
3319 					dc->link_srv->dp_trace_source_sequence(
3320 							pipe_ctx->stream->link,
3321 							DPCD_SOURCE_SEQ_AFTER_UPDATE_INFO_FRAME);
3322 			}
3323 
3324 			if (stream_update->hdr_static_metadata &&
3325 					stream->use_dynamic_meta &&
3326 					dc->hwss.set_dmdata_attributes &&
3327 					pipe_ctx->stream->dmdata_address.quad_part != 0)
3328 				dc->hwss.set_dmdata_attributes(pipe_ctx);
3329 
3330 			if (stream_update->gamut_remap)
3331 				dc_stream_set_gamut_remap(dc, stream);
3332 
3333 			if (stream_update->output_csc_transform)
3334 				dc_stream_program_csc_matrix(dc, stream);
3335 
3336 			if (stream_update->dither_option) {
3337 				struct pipe_ctx *odm_pipe = pipe_ctx->next_odm_pipe;
3338 				resource_build_bit_depth_reduction_params(pipe_ctx->stream,
3339 									&pipe_ctx->stream->bit_depth_params);
3340 				pipe_ctx->stream_res.opp->funcs->opp_program_fmt(pipe_ctx->stream_res.opp,
3341 						&stream->bit_depth_params,
3342 						&stream->clamping);
3343 				while (odm_pipe) {
3344 					odm_pipe->stream_res.opp->funcs->opp_program_fmt(odm_pipe->stream_res.opp,
3345 							&stream->bit_depth_params,
3346 							&stream->clamping);
3347 					odm_pipe = odm_pipe->next_odm_pipe;
3348 				}
3349 			}
3350 
3351 			if (stream_update->cursor_attributes)
3352 				program_cursor_attributes(dc, stream);
3353 
3354 			if (stream_update->cursor_position)
3355 				program_cursor_position(dc, stream);
3356 
3357 			/* Full fe update*/
3358 			if (update_type == UPDATE_TYPE_FAST)
3359 				continue;
3360 
3361 			if (stream_update->dsc_config)
3362 				dc->link_srv->update_dsc_config(pipe_ctx);
3363 
3364 			if (stream_update->mst_bw_update) {
3365 				if (stream_update->mst_bw_update->is_increase)
3366 					dc->link_srv->increase_mst_payload(pipe_ctx,
3367 							stream_update->mst_bw_update->mst_stream_bw);
3368 				else
3369 					dc->link_srv->reduce_mst_payload(pipe_ctx,
3370 							stream_update->mst_bw_update->mst_stream_bw);
3371 			}
3372 
3373 			if (stream_update->pending_test_pattern) {
3374 				/*
3375 				 * test pattern params depends on ODM topology
3376 				 * changes that we could be applying to front
3377 				 * end. Since at the current stage front end
3378 				 * changes are not yet applied. We can only
3379 				 * apply test pattern in hw based on current
3380 				 * state and populate the final test pattern
3381 				 * params in new state. If current and new test
3382 				 * pattern params are different as result of
3383 				 * different ODM topology being used, it will be
3384 				 * detected and handle during front end
3385 				 * programming update.
3386 				 */
3387 				dc->link_srv->dp_set_test_pattern(stream->link,
3388 					stream->test_pattern.type,
3389 					stream->test_pattern.color_space,
3390 					stream->test_pattern.p_link_settings,
3391 					stream->test_pattern.p_custom_pattern,
3392 					stream->test_pattern.cust_pattern_size);
3393 				resource_build_test_pattern_params(&context->res_ctx, pipe_ctx);
3394 			}
3395 
3396 			if (stream_update->dpms_off) {
3397 				if (*stream_update->dpms_off) {
3398 					dc->link_srv->set_dpms_off(pipe_ctx);
3399 					/* for dpms, keep acquired resources*/
3400 					if (pipe_ctx->stream_res.audio && !dc->debug.az_endpoint_mute_only)
3401 						pipe_ctx->stream_res.audio->funcs->az_disable(pipe_ctx->stream_res.audio);
3402 
3403 					dc->optimized_required = true;
3404 
3405 				} else {
3406 					if (get_seamless_boot_stream_count(context) == 0)
3407 						dc->hwss.prepare_bandwidth(dc, dc->current_state);
3408 					dc->link_srv->set_dpms_on(dc->current_state, pipe_ctx);
3409 				}
3410 			} else if (pipe_ctx->stream->link->wa_flags.blank_stream_on_ocs_change && stream_update->output_color_space
3411 					&& !stream->dpms_off && dc_is_dp_signal(pipe_ctx->stream->signal)) {
3412 				/*
3413 				 * Workaround for firmware issue in some receivers where they don't pick up
3414 				 * correct output color space unless DP link is disabled/re-enabled
3415 				 */
3416 				dc->link_srv->set_dpms_on(dc->current_state, pipe_ctx);
3417 			}
3418 
3419 			if (stream_update->abm_level && pipe_ctx->stream_res.abm) {
3420 				bool should_program_abm = true;
3421 
3422 				// if otg funcs defined check if blanked before programming
3423 				if (pipe_ctx->stream_res.tg->funcs->is_blanked)
3424 					if (pipe_ctx->stream_res.tg->funcs->is_blanked(pipe_ctx->stream_res.tg))
3425 						should_program_abm = false;
3426 
3427 				if (should_program_abm) {
3428 					if (*stream_update->abm_level == ABM_LEVEL_IMMEDIATE_DISABLE) {
3429 						dc->hwss.set_abm_immediate_disable(pipe_ctx);
3430 					} else {
3431 						pipe_ctx->stream_res.abm->funcs->set_abm_level(
3432 							pipe_ctx->stream_res.abm, stream->abm_level);
3433 					}
3434 				}
3435 			}
3436 		}
3437 	}
3438 }
3439 
dc_dmub_should_send_dirty_rect_cmd(struct dc * dc,struct dc_stream_state * stream)3440 static bool dc_dmub_should_send_dirty_rect_cmd(struct dc *dc, struct dc_stream_state *stream)
3441 {
3442 	if ((stream->link->psr_settings.psr_version == DC_PSR_VERSION_SU_1
3443 			|| stream->link->psr_settings.psr_version == DC_PSR_VERSION_1)
3444 			&& stream->ctx->dce_version >= DCN_VERSION_3_1)
3445 		return true;
3446 
3447 	if (stream->link->replay_settings.config.replay_supported)
3448 		return true;
3449 
3450 	if (stream->ctx->dce_version >= DCN_VERSION_3_5 && stream->abm_level)
3451 		return true;
3452 
3453 	return false;
3454 }
3455 
dc_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)3456 void dc_dmub_update_dirty_rect(struct dc *dc,
3457 			       int surface_count,
3458 			       struct dc_stream_state *stream,
3459 			       struct dc_surface_update *srf_updates,
3460 			       struct dc_state *context)
3461 {
3462 	union dmub_rb_cmd cmd;
3463 	struct dmub_cmd_update_dirty_rect_data *update_dirty_rect;
3464 	unsigned int i, j;
3465 	unsigned int panel_inst = 0;
3466 
3467 	if (!dc_dmub_should_send_dirty_rect_cmd(dc, stream))
3468 		return;
3469 
3470 	if (!dc_get_edp_link_panel_inst(dc, stream->link, &panel_inst))
3471 		return;
3472 
3473 	memset(&cmd, 0x0, sizeof(cmd));
3474 	cmd.update_dirty_rect.header.type = DMUB_CMD__UPDATE_DIRTY_RECT;
3475 	cmd.update_dirty_rect.header.sub_type = 0;
3476 	cmd.update_dirty_rect.header.payload_bytes =
3477 		sizeof(cmd.update_dirty_rect) -
3478 		sizeof(cmd.update_dirty_rect.header);
3479 	update_dirty_rect = &cmd.update_dirty_rect.update_dirty_rect_data;
3480 	for (i = 0; i < surface_count; i++) {
3481 		struct dc_plane_state *plane_state = srf_updates[i].surface;
3482 		const struct dc_flip_addrs *flip_addr = srf_updates[i].flip_addr;
3483 
3484 		if (!srf_updates[i].surface || !flip_addr)
3485 			continue;
3486 		/* Do not send in immediate flip mode */
3487 		if (srf_updates[i].surface->flip_immediate)
3488 			continue;
3489 
3490 		update_dirty_rect->cmd_version = DMUB_CMD_PSR_CONTROL_VERSION_1;
3491 		update_dirty_rect->dirty_rect_count = flip_addr->dirty_rect_count;
3492 		memcpy(update_dirty_rect->src_dirty_rects, flip_addr->dirty_rects,
3493 				sizeof(flip_addr->dirty_rects));
3494 		for (j = 0; j < dc->res_pool->pipe_count; j++) {
3495 			struct pipe_ctx *pipe_ctx = &context->res_ctx.pipe_ctx[j];
3496 
3497 			if (pipe_ctx->stream != stream)
3498 				continue;
3499 			if (pipe_ctx->plane_state != plane_state)
3500 				continue;
3501 
3502 			update_dirty_rect->panel_inst = panel_inst;
3503 			update_dirty_rect->pipe_idx = j;
3504 			dc_wake_and_execute_dmub_cmd(dc->ctx, &cmd, DM_DMUB_WAIT_TYPE_NO_WAIT);
3505 		}
3506 	}
3507 }
3508 
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)3509 static void build_dmub_update_dirty_rect(
3510 		struct dc *dc,
3511 		int surface_count,
3512 		struct dc_stream_state *stream,
3513 		struct dc_surface_update *srf_updates,
3514 		struct dc_state *context,
3515 		struct dc_dmub_cmd dc_dmub_cmd[],
3516 		unsigned int *dmub_cmd_count)
3517 {
3518 	union dmub_rb_cmd cmd;
3519 	struct dmub_cmd_update_dirty_rect_data *update_dirty_rect;
3520 	unsigned int i, j;
3521 	unsigned int panel_inst = 0;
3522 
3523 	if (!dc_dmub_should_send_dirty_rect_cmd(dc, stream))
3524 		return;
3525 
3526 	if (!dc_get_edp_link_panel_inst(dc, stream->link, &panel_inst))
3527 		return;
3528 
3529 	memset(&cmd, 0x0, sizeof(cmd));
3530 	cmd.update_dirty_rect.header.type = DMUB_CMD__UPDATE_DIRTY_RECT;
3531 	cmd.update_dirty_rect.header.sub_type = 0;
3532 	cmd.update_dirty_rect.header.payload_bytes =
3533 		sizeof(cmd.update_dirty_rect) -
3534 		sizeof(cmd.update_dirty_rect.header);
3535 	update_dirty_rect = &cmd.update_dirty_rect.update_dirty_rect_data;
3536 	for (i = 0; i < surface_count; i++) {
3537 		struct dc_plane_state *plane_state = srf_updates[i].surface;
3538 		const struct dc_flip_addrs *flip_addr = srf_updates[i].flip_addr;
3539 
3540 		if (!srf_updates[i].surface || !flip_addr)
3541 			continue;
3542 		/* Do not send in immediate flip mode */
3543 		if (srf_updates[i].surface->flip_immediate)
3544 			continue;
3545 		update_dirty_rect->cmd_version = DMUB_CMD_PSR_CONTROL_VERSION_1;
3546 		update_dirty_rect->dirty_rect_count = flip_addr->dirty_rect_count;
3547 		memcpy(update_dirty_rect->src_dirty_rects, flip_addr->dirty_rects,
3548 				sizeof(flip_addr->dirty_rects));
3549 		for (j = 0; j < dc->res_pool->pipe_count; j++) {
3550 			struct pipe_ctx *pipe_ctx = &context->res_ctx.pipe_ctx[j];
3551 
3552 			if (pipe_ctx->stream != stream)
3553 				continue;
3554 			if (pipe_ctx->plane_state != plane_state)
3555 				continue;
3556 			update_dirty_rect->panel_inst = panel_inst;
3557 			update_dirty_rect->pipe_idx = j;
3558 			dc_dmub_cmd[*dmub_cmd_count].dmub_cmd = cmd;
3559 			dc_dmub_cmd[*dmub_cmd_count].wait_type = DM_DMUB_WAIT_TYPE_NO_WAIT;
3560 			(*dmub_cmd_count)++;
3561 		}
3562 	}
3563 }
3564 
check_address_only_update(union surface_update_flags update_flags)3565 static bool check_address_only_update(union surface_update_flags update_flags)
3566 {
3567 	union surface_update_flags addr_only_update_flags;
3568 	addr_only_update_flags.raw = 0;
3569 	addr_only_update_flags.bits.addr_update = 1;
3570 
3571 	return update_flags.bits.addr_update &&
3572 			!(update_flags.raw & ~addr_only_update_flags.raw);
3573 }
3574 
3575 /**
3576  * build_dmub_cmd_list() - Build an array of DMCUB commands to be sent to DMCUB
3577  *
3578  * @dc: Current DC state
3579  * @srf_updates: Array of surface updates
3580  * @surface_count: Number of surfaces that have an updated
3581  * @stream: Corresponding stream to be updated in the current flip
3582  * @context: New DC state to be programmed
3583  *
3584  * @dc_dmub_cmd: Array of DMCUB commands to be sent to DMCUB
3585  * @dmub_cmd_count: Count indicating the number of DMCUB commands in dc_dmub_cmd array
3586  *
3587  * This function builds an array of DMCUB commands to be sent to DMCUB. This function is required
3588  * to build an array of commands and have them sent while the OTG lock is acquired.
3589  *
3590  * Return: void
3591  */
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)3592 static void build_dmub_cmd_list(struct dc *dc,
3593 		struct dc_surface_update *srf_updates,
3594 		int surface_count,
3595 		struct dc_stream_state *stream,
3596 		struct dc_state *context,
3597 		struct dc_dmub_cmd dc_dmub_cmd[],
3598 		unsigned int *dmub_cmd_count)
3599 {
3600 	// Initialize cmd count to 0
3601 	*dmub_cmd_count = 0;
3602 	build_dmub_update_dirty_rect(dc, surface_count, stream, srf_updates, context, dc_dmub_cmd, dmub_cmd_count);
3603 }
3604 
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)3605 static void commit_plane_for_stream_offload_fams2_flip(struct dc *dc,
3606 		struct dc_surface_update *srf_updates,
3607 		int surface_count,
3608 		struct dc_stream_state *stream,
3609 		struct dc_state *context)
3610 {
3611 	int i, j;
3612 
3613 	/* update dirty rect for PSR */
3614 	dc_dmub_update_dirty_rect(dc, surface_count, stream,
3615 			srf_updates, context);
3616 
3617 	/* Perform requested Updates */
3618 	for (i = 0; i < surface_count; i++) {
3619 		struct dc_plane_state *plane_state = srf_updates[i].surface;
3620 
3621 		for (j = 0; j < dc->res_pool->pipe_count; j++) {
3622 			struct pipe_ctx *pipe_ctx = &context->res_ctx.pipe_ctx[j];
3623 
3624 			if (!should_update_pipe_for_stream(context, pipe_ctx, stream))
3625 				continue;
3626 
3627 			if (!should_update_pipe_for_plane(context, pipe_ctx, plane_state))
3628 				continue;
3629 
3630 			/* update pipe context for plane */
3631 			if (pipe_ctx->plane_state->update_flags.bits.addr_update)
3632 				dc->hwss.update_plane_addr(dc, pipe_ctx);
3633 		}
3634 	}
3635 
3636 	/* Send commands to DMCUB */
3637 	dc_dmub_srv_fams2_passthrough_flip(dc,
3638 				context,
3639 				stream,
3640 				srf_updates,
3641 				surface_count);
3642 }
3643 
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 surface_update_type update_type,struct dc_state * context)3644 static void commit_planes_for_stream_fast(struct dc *dc,
3645 		struct dc_surface_update *srf_updates,
3646 		int surface_count,
3647 		struct dc_stream_state *stream,
3648 		struct dc_stream_update *stream_update,
3649 		enum surface_update_type update_type,
3650 		struct dc_state *context)
3651 {
3652 	int i, j;
3653 	struct pipe_ctx *top_pipe_to_program = NULL;
3654 	struct dc_stream_status *stream_status = NULL;
3655 	bool should_offload_fams2_flip = false;
3656 
3657 	if (dc->debug.fams2_config.bits.enable &&
3658 			dc->debug.fams2_config.bits.enable_offload_flip &&
3659 			dc_state_is_fams2_in_use(dc, context)) {
3660 		/* if not offloading to HWFQ, offload to FAMS2 if needed */
3661 		should_offload_fams2_flip = true;
3662 		for (i = 0; i < surface_count; i++) {
3663 			if (srf_updates[i].surface &&
3664 					srf_updates[i].surface->update_flags.raw &&
3665 					!check_address_only_update(srf_updates[i].surface->update_flags)) {
3666 				/* more than address update, need to acquire FAMS2 lock */
3667 				should_offload_fams2_flip = false;
3668 				break;
3669 			}
3670 		}
3671 		if (stream_update) {
3672 			/* more than address update, need to acquire FAMS2 lock */
3673 			should_offload_fams2_flip = false;
3674 		}
3675 	}
3676 
3677 	dc_exit_ips_for_hw_access(dc);
3678 
3679 	dc_z10_restore(dc);
3680 
3681 	top_pipe_to_program = resource_get_otg_master_for_stream(
3682 			&context->res_ctx,
3683 			stream);
3684 
3685 	if (!top_pipe_to_program)
3686 		return;
3687 
3688 	for (i = 0; i < dc->res_pool->pipe_count; i++) {
3689 		struct pipe_ctx *pipe = &context->res_ctx.pipe_ctx[i];
3690 
3691 		if (pipe->stream && pipe->plane_state) {
3692 			if (!dc->debug.using_dml2)
3693 				set_p_state_switch_method(dc, context, pipe);
3694 
3695 			if (dc->debug.visual_confirm)
3696 				dc_update_visual_confirm_color(dc, context, pipe);
3697 		}
3698 	}
3699 
3700 	for (i = 0; i < surface_count; i++) {
3701 		struct dc_plane_state *plane_state = srf_updates[i].surface;
3702 		/*set logical flag for lock/unlock use*/
3703 		for (j = 0; j < dc->res_pool->pipe_count; j++) {
3704 			struct pipe_ctx *pipe_ctx = &context->res_ctx.pipe_ctx[j];
3705 
3706 			if (!pipe_ctx->plane_state)
3707 				continue;
3708 			if (should_update_pipe_for_plane(context, pipe_ctx, plane_state))
3709 				continue;
3710 			pipe_ctx->plane_state->triplebuffer_flips = false;
3711 			if (update_type == UPDATE_TYPE_FAST &&
3712 			    dc->hwss.program_triplebuffer != NULL &&
3713 			    !pipe_ctx->plane_state->flip_immediate && dc->debug.enable_tri_buf) {
3714 				/*triple buffer for VUpdate  only*/
3715 				pipe_ctx->plane_state->triplebuffer_flips = true;
3716 			}
3717 		}
3718 	}
3719 
3720 	stream_status = dc_state_get_stream_status(context, stream);
3721 
3722 	if (should_offload_fams2_flip) {
3723 		commit_plane_for_stream_offload_fams2_flip(dc,
3724 				srf_updates,
3725 				surface_count,
3726 				stream,
3727 				context);
3728 	} else if (stream_status) {
3729 		build_dmub_cmd_list(dc,
3730 				srf_updates,
3731 				surface_count,
3732 				stream,
3733 				context,
3734 				context->dc_dmub_cmd,
3735 				&(context->dmub_cmd_count));
3736 		hwss_build_fast_sequence(dc,
3737 				context->dc_dmub_cmd,
3738 				context->dmub_cmd_count,
3739 				context->block_sequence,
3740 				&(context->block_sequence_steps),
3741 				top_pipe_to_program,
3742 				stream_status,
3743 				context);
3744 		hwss_execute_sequence(dc,
3745 				context->block_sequence,
3746 				context->block_sequence_steps);
3747 	}
3748 
3749 	/* Clear update flags so next flip doesn't have redundant programming
3750 	 * (if there's no stream update, the update flags are not cleared).
3751 	 * Surface updates are cleared unconditionally at the beginning of each flip,
3752 	 * so no need to clear here.
3753 	 */
3754 	if (top_pipe_to_program->stream)
3755 		top_pipe_to_program->stream->update_flags.raw = 0;
3756 }
3757 
commit_planes_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,enum surface_update_type update_type,struct dc_state * context)3758 static void commit_planes_for_stream(struct dc *dc,
3759 		struct dc_surface_update *srf_updates,
3760 		int surface_count,
3761 		struct dc_stream_state *stream,
3762 		struct dc_stream_update *stream_update,
3763 		enum surface_update_type update_type,
3764 		struct dc_state *context)
3765 {
3766 	int i, j;
3767 	struct pipe_ctx *top_pipe_to_program = NULL;
3768 	bool should_lock_all_pipes = (update_type != UPDATE_TYPE_FAST);
3769 	bool subvp_prev_use = false;
3770 	bool subvp_curr_use = false;
3771 	uint8_t current_stream_mask = 0;
3772 
3773 	// Once we apply the new subvp context to hardware it won't be in the
3774 	// dc->current_state anymore, so we have to cache it before we apply
3775 	// the new SubVP context
3776 	subvp_prev_use = false;
3777 	dc_exit_ips_for_hw_access(dc);
3778 
3779 	dc_z10_restore(dc);
3780 	if (update_type == UPDATE_TYPE_FULL)
3781 		hwss_process_outstanding_hw_updates(dc, dc->current_state);
3782 
3783 	for (i = 0; i < dc->res_pool->pipe_count; i++) {
3784 		struct pipe_ctx *pipe = &context->res_ctx.pipe_ctx[i];
3785 
3786 		if (pipe->stream && pipe->plane_state) {
3787 			if (!dc->debug.using_dml2)
3788 				set_p_state_switch_method(dc, context, pipe);
3789 
3790 			if (dc->debug.visual_confirm)
3791 				dc_update_visual_confirm_color(dc, context, pipe);
3792 		}
3793 	}
3794 
3795 	if (update_type == UPDATE_TYPE_FULL) {
3796 		dc_allow_idle_optimizations(dc, false);
3797 
3798 		if (get_seamless_boot_stream_count(context) == 0)
3799 			dc->hwss.prepare_bandwidth(dc, context);
3800 
3801 		if (dc->hwss.update_dsc_pg)
3802 			dc->hwss.update_dsc_pg(dc, context, false);
3803 
3804 		context_clock_trace(dc, context);
3805 	}
3806 
3807 	top_pipe_to_program = resource_get_otg_master_for_stream(
3808 				&context->res_ctx,
3809 				stream);
3810 	ASSERT(top_pipe_to_program != NULL);
3811 	for (i = 0; i < dc->res_pool->pipe_count; i++) {
3812 		struct pipe_ctx *old_pipe = &dc->current_state->res_ctx.pipe_ctx[i];
3813 
3814 		// Check old context for SubVP
3815 		subvp_prev_use |= (dc_state_get_pipe_subvp_type(dc->current_state, old_pipe) == SUBVP_PHANTOM);
3816 		if (subvp_prev_use)
3817 			break;
3818 	}
3819 
3820 	for (i = 0; i < dc->res_pool->pipe_count; i++) {
3821 		struct pipe_ctx *pipe = &context->res_ctx.pipe_ctx[i];
3822 
3823 		if (dc_state_get_pipe_subvp_type(context, pipe) == SUBVP_PHANTOM) {
3824 			subvp_curr_use = true;
3825 			break;
3826 		}
3827 	}
3828 
3829 	if (stream->test_pattern.type != DP_TEST_PATTERN_VIDEO_MODE) {
3830 		struct pipe_ctx *mpcc_pipe;
3831 		struct pipe_ctx *odm_pipe;
3832 
3833 		for (mpcc_pipe = top_pipe_to_program; mpcc_pipe; mpcc_pipe = mpcc_pipe->bottom_pipe)
3834 			for (odm_pipe = mpcc_pipe; odm_pipe; odm_pipe = odm_pipe->next_odm_pipe)
3835 				odm_pipe->ttu_regs.min_ttu_vblank = MAX_TTU;
3836 	}
3837 
3838 	if (update_type != UPDATE_TYPE_FAST && dc->res_pool->funcs->prepare_mcache_programming)
3839 		dc->res_pool->funcs->prepare_mcache_programming(dc, context);
3840 
3841 	if ((update_type != UPDATE_TYPE_FAST) && stream->update_flags.bits.dsc_changed)
3842 		if (top_pipe_to_program &&
3843 			top_pipe_to_program->stream_res.tg->funcs->lock_doublebuffer_enable) {
3844 			if (should_use_dmub_lock(stream->link)) {
3845 				union dmub_hw_lock_flags hw_locks = { 0 };
3846 				struct dmub_hw_lock_inst_flags inst_flags = { 0 };
3847 
3848 				hw_locks.bits.lock_dig = 1;
3849 				inst_flags.dig_inst = top_pipe_to_program->stream_res.tg->inst;
3850 
3851 				dmub_hw_lock_mgr_cmd(dc->ctx->dmub_srv,
3852 							true,
3853 							&hw_locks,
3854 							&inst_flags);
3855 			} else
3856 				top_pipe_to_program->stream_res.tg->funcs->lock_doublebuffer_enable(
3857 						top_pipe_to_program->stream_res.tg);
3858 		}
3859 
3860 	if (dc->hwss.wait_for_dcc_meta_propagation) {
3861 		dc->hwss.wait_for_dcc_meta_propagation(dc, top_pipe_to_program);
3862 	}
3863 
3864 	if (should_lock_all_pipes && dc->hwss.interdependent_update_lock) {
3865 		if (dc->hwss.subvp_pipe_control_lock)
3866 			dc->hwss.subvp_pipe_control_lock(dc, context, true, should_lock_all_pipes, NULL, subvp_prev_use);
3867 
3868 		if (dc->hwss.fams2_global_control_lock)
3869 			dc->hwss.fams2_global_control_lock(dc, context, true);
3870 
3871 		dc->hwss.interdependent_update_lock(dc, context, true);
3872 	} else {
3873 		if (dc->hwss.subvp_pipe_control_lock)
3874 			dc->hwss.subvp_pipe_control_lock(dc, context, true, should_lock_all_pipes, top_pipe_to_program, subvp_prev_use);
3875 
3876 		if (dc->hwss.fams2_global_control_lock)
3877 			dc->hwss.fams2_global_control_lock(dc, context, true);
3878 
3879 		/* Lock the top pipe while updating plane addrs, since freesync requires
3880 		 *  plane addr update event triggers to be synchronized.
3881 		 *  top_pipe_to_program is expected to never be NULL
3882 		 */
3883 		dc->hwss.pipe_control_lock(dc, top_pipe_to_program, true);
3884 	}
3885 
3886 	dc_dmub_update_dirty_rect(dc, surface_count, stream, srf_updates, context);
3887 
3888 	// Stream updates
3889 	if (stream_update)
3890 		commit_planes_do_stream_update(dc, stream, stream_update, update_type, context);
3891 
3892 	if (surface_count == 0) {
3893 		/*
3894 		 * In case of turning off screen, no need to program front end a second time.
3895 		 * just return after program blank.
3896 		 */
3897 		if (dc->hwss.apply_ctx_for_surface)
3898 			dc->hwss.apply_ctx_for_surface(dc, stream, 0, context);
3899 		if (dc->hwss.program_front_end_for_ctx)
3900 			dc->hwss.program_front_end_for_ctx(dc, context);
3901 
3902 		if (should_lock_all_pipes && dc->hwss.interdependent_update_lock) {
3903 			dc->hwss.interdependent_update_lock(dc, context, false);
3904 		} else {
3905 			dc->hwss.pipe_control_lock(dc, top_pipe_to_program, false);
3906 		}
3907 		dc->hwss.post_unlock_program_front_end(dc, context);
3908 
3909 		if (update_type != UPDATE_TYPE_FAST)
3910 			if (dc->hwss.commit_subvp_config)
3911 				dc->hwss.commit_subvp_config(dc, context);
3912 
3913 		/* Since phantom pipe programming is moved to post_unlock_program_front_end,
3914 		 * move the SubVP lock to after the phantom pipes have been setup
3915 		 */
3916 		if (dc->hwss.subvp_pipe_control_lock)
3917 			dc->hwss.subvp_pipe_control_lock(dc, context, false, should_lock_all_pipes,
3918 							 NULL, subvp_prev_use);
3919 
3920 		if (dc->hwss.fams2_global_control_lock)
3921 			dc->hwss.fams2_global_control_lock(dc, context, false);
3922 
3923 		return;
3924 	}
3925 
3926 	if (update_type != UPDATE_TYPE_FAST) {
3927 		for (j = 0; j < dc->res_pool->pipe_count; j++) {
3928 			struct pipe_ctx *pipe_ctx = &context->res_ctx.pipe_ctx[j];
3929 
3930 			if ((dc->debug.visual_confirm == VISUAL_CONFIRM_SUBVP ||
3931 				dc->debug.visual_confirm == VISUAL_CONFIRM_MCLK_SWITCH) &&
3932 				pipe_ctx->stream && pipe_ctx->plane_state) {
3933 				/* Only update visual confirm for SUBVP and Mclk switching here.
3934 				 * The bar appears on all pipes, so we need to update the bar on all displays,
3935 				 * so the information doesn't get stale.
3936 				 */
3937 				dc->hwss.update_visual_confirm_color(dc, pipe_ctx,
3938 						pipe_ctx->plane_res.hubp->inst);
3939 			}
3940 		}
3941 	}
3942 
3943 	for (i = 0; i < surface_count; i++) {
3944 		struct dc_plane_state *plane_state = srf_updates[i].surface;
3945 
3946 		/*set logical flag for lock/unlock use*/
3947 		for (j = 0; j < dc->res_pool->pipe_count; j++) {
3948 			struct pipe_ctx *pipe_ctx = &context->res_ctx.pipe_ctx[j];
3949 			if (!pipe_ctx->plane_state)
3950 				continue;
3951 			if (should_update_pipe_for_plane(context, pipe_ctx, plane_state))
3952 				continue;
3953 			pipe_ctx->plane_state->triplebuffer_flips = false;
3954 			if (update_type == UPDATE_TYPE_FAST &&
3955 				dc->hwss.program_triplebuffer != NULL &&
3956 				!pipe_ctx->plane_state->flip_immediate && dc->debug.enable_tri_buf) {
3957 					/*triple buffer for VUpdate  only*/
3958 					pipe_ctx->plane_state->triplebuffer_flips = true;
3959 			}
3960 		}
3961 		if (update_type == UPDATE_TYPE_FULL) {
3962 			/* force vsync flip when reconfiguring pipes to prevent underflow */
3963 			plane_state->flip_immediate = false;
3964 		}
3965 	}
3966 
3967 	// Update Type FULL, Surface updates
3968 	for (j = 0; j < dc->res_pool->pipe_count; j++) {
3969 		struct pipe_ctx *pipe_ctx = &context->res_ctx.pipe_ctx[j];
3970 
3971 		if (!pipe_ctx->top_pipe &&
3972 			!pipe_ctx->prev_odm_pipe &&
3973 			should_update_pipe_for_stream(context, pipe_ctx, stream)) {
3974 			struct dc_stream_status *stream_status = NULL;
3975 
3976 			if (!pipe_ctx->plane_state)
3977 				continue;
3978 
3979 			/* Full fe update*/
3980 			if (update_type == UPDATE_TYPE_FAST)
3981 				continue;
3982 
3983 			ASSERT(!pipe_ctx->plane_state->triplebuffer_flips);
3984 
3985 			if (dc->hwss.program_triplebuffer != NULL && dc->debug.enable_tri_buf) {
3986 				/*turn off triple buffer for full update*/
3987 				dc->hwss.program_triplebuffer(
3988 					dc, pipe_ctx, pipe_ctx->plane_state->triplebuffer_flips);
3989 			}
3990 			stream_status =
3991 				stream_get_status(context, pipe_ctx->stream);
3992 
3993 			if (dc->hwss.apply_ctx_for_surface && stream_status)
3994 				dc->hwss.apply_ctx_for_surface(
3995 					dc, pipe_ctx->stream, stream_status->plane_count, context);
3996 		}
3997 	}
3998 	if (dc->hwss.program_front_end_for_ctx && update_type != UPDATE_TYPE_FAST) {
3999 		dc->hwss.program_front_end_for_ctx(dc, context);
4000 		if (dc->debug.validate_dml_output) {
4001 			for (i = 0; i < dc->res_pool->pipe_count; i++) {
4002 				struct pipe_ctx *cur_pipe = &context->res_ctx.pipe_ctx[i];
4003 				if (cur_pipe->stream == NULL)
4004 					continue;
4005 
4006 				cur_pipe->plane_res.hubp->funcs->validate_dml_output(
4007 						cur_pipe->plane_res.hubp, dc->ctx,
4008 						&context->res_ctx.pipe_ctx[i].rq_regs,
4009 						&context->res_ctx.pipe_ctx[i].dlg_regs,
4010 						&context->res_ctx.pipe_ctx[i].ttu_regs);
4011 			}
4012 		}
4013 	}
4014 
4015 	// Update Type FAST, Surface updates
4016 	if (update_type == UPDATE_TYPE_FAST) {
4017 		if (dc->hwss.set_flip_control_gsl)
4018 			for (i = 0; i < surface_count; i++) {
4019 				struct dc_plane_state *plane_state = srf_updates[i].surface;
4020 
4021 				for (j = 0; j < dc->res_pool->pipe_count; j++) {
4022 					struct pipe_ctx *pipe_ctx = &context->res_ctx.pipe_ctx[j];
4023 
4024 					if (!should_update_pipe_for_stream(context, pipe_ctx, stream))
4025 						continue;
4026 
4027 					if (!should_update_pipe_for_plane(context, pipe_ctx, plane_state))
4028 						continue;
4029 
4030 					// GSL has to be used for flip immediate
4031 					dc->hwss.set_flip_control_gsl(pipe_ctx,
4032 							pipe_ctx->plane_state->flip_immediate);
4033 				}
4034 			}
4035 
4036 		/* Perform requested Updates */
4037 		for (i = 0; i < surface_count; i++) {
4038 			struct dc_plane_state *plane_state = srf_updates[i].surface;
4039 
4040 			for (j = 0; j < dc->res_pool->pipe_count; j++) {
4041 				struct pipe_ctx *pipe_ctx = &context->res_ctx.pipe_ctx[j];
4042 
4043 				if (!should_update_pipe_for_stream(context, pipe_ctx, stream))
4044 					continue;
4045 
4046 				if (!should_update_pipe_for_plane(context, pipe_ctx, plane_state))
4047 					continue;
4048 
4049 				if (srf_updates[i].cm2_params &&
4050 						srf_updates[i].cm2_params->cm2_luts.lut3d_data.lut3d_src ==
4051 								DC_CM2_TRANSFER_FUNC_SOURCE_VIDMEM &&
4052 						srf_updates[i].cm2_params->component_settings.shaper_3dlut_setting ==
4053 								DC_CM2_SHAPER_3DLUT_SETTING_ENABLE_SHAPER_3DLUT &&
4054 						dc->hwss.trigger_3dlut_dma_load)
4055 					dc->hwss.trigger_3dlut_dma_load(dc, pipe_ctx);
4056 
4057 				/*program triple buffer after lock based on flip type*/
4058 				if (dc->hwss.program_triplebuffer != NULL && dc->debug.enable_tri_buf) {
4059 					/*only enable triplebuffer for  fast_update*/
4060 					dc->hwss.program_triplebuffer(
4061 						dc, pipe_ctx, pipe_ctx->plane_state->triplebuffer_flips);
4062 				}
4063 				if (pipe_ctx->plane_state->update_flags.bits.addr_update)
4064 					dc->hwss.update_plane_addr(dc, pipe_ctx);
4065 			}
4066 		}
4067 	}
4068 
4069 	if (should_lock_all_pipes && dc->hwss.interdependent_update_lock) {
4070 		dc->hwss.interdependent_update_lock(dc, context, false);
4071 	} else {
4072 		dc->hwss.pipe_control_lock(dc, top_pipe_to_program, false);
4073 	}
4074 
4075 	if ((update_type != UPDATE_TYPE_FAST) && stream->update_flags.bits.dsc_changed)
4076 		if (top_pipe_to_program &&
4077 		    top_pipe_to_program->stream_res.tg->funcs->lock_doublebuffer_enable) {
4078 			top_pipe_to_program->stream_res.tg->funcs->wait_for_state(
4079 				top_pipe_to_program->stream_res.tg,
4080 				CRTC_STATE_VACTIVE);
4081 			top_pipe_to_program->stream_res.tg->funcs->wait_for_state(
4082 				top_pipe_to_program->stream_res.tg,
4083 				CRTC_STATE_VBLANK);
4084 			top_pipe_to_program->stream_res.tg->funcs->wait_for_state(
4085 				top_pipe_to_program->stream_res.tg,
4086 				CRTC_STATE_VACTIVE);
4087 
4088 			if (should_use_dmub_lock(stream->link)) {
4089 				union dmub_hw_lock_flags hw_locks = { 0 };
4090 				struct dmub_hw_lock_inst_flags inst_flags = { 0 };
4091 
4092 				hw_locks.bits.lock_dig = 1;
4093 				inst_flags.dig_inst = top_pipe_to_program->stream_res.tg->inst;
4094 
4095 				dmub_hw_lock_mgr_cmd(dc->ctx->dmub_srv,
4096 							false,
4097 							&hw_locks,
4098 							&inst_flags);
4099 			} else
4100 				top_pipe_to_program->stream_res.tg->funcs->lock_doublebuffer_disable(
4101 					top_pipe_to_program->stream_res.tg);
4102 		}
4103 
4104 	if (subvp_curr_use) {
4105 		/* If enabling subvp or transitioning from subvp->subvp, enable the
4106 		 * phantom streams before we program front end for the phantom pipes.
4107 		 */
4108 		if (update_type != UPDATE_TYPE_FAST) {
4109 			if (dc->hwss.enable_phantom_streams)
4110 				dc->hwss.enable_phantom_streams(dc, context);
4111 		}
4112 	}
4113 
4114 	if (update_type != UPDATE_TYPE_FAST)
4115 		dc->hwss.post_unlock_program_front_end(dc, context);
4116 
4117 	if (subvp_prev_use && !subvp_curr_use) {
4118 		/* If disabling subvp, disable phantom streams after front end
4119 		 * programming has completed (we turn on phantom OTG in order
4120 		 * to complete the plane disable for phantom pipes).
4121 		 */
4122 
4123 		if (dc->hwss.disable_phantom_streams)
4124 			dc->hwss.disable_phantom_streams(dc, context);
4125 	}
4126 
4127 	if (update_type != UPDATE_TYPE_FAST)
4128 		if (dc->hwss.commit_subvp_config)
4129 			dc->hwss.commit_subvp_config(dc, context);
4130 	/* Since phantom pipe programming is moved to post_unlock_program_front_end,
4131 	 * move the SubVP lock to after the phantom pipes have been setup
4132 	 */
4133 	if (should_lock_all_pipes && dc->hwss.interdependent_update_lock) {
4134 		if (dc->hwss.subvp_pipe_control_lock)
4135 			dc->hwss.subvp_pipe_control_lock(dc, context, false, should_lock_all_pipes, NULL, subvp_prev_use);
4136 		if (dc->hwss.fams2_global_control_lock)
4137 			dc->hwss.fams2_global_control_lock(dc, context, false);
4138 	} else {
4139 		if (dc->hwss.subvp_pipe_control_lock)
4140 			dc->hwss.subvp_pipe_control_lock(dc, context, false, should_lock_all_pipes, top_pipe_to_program, subvp_prev_use);
4141 		if (dc->hwss.fams2_global_control_lock)
4142 			dc->hwss.fams2_global_control_lock(dc, context, false);
4143 	}
4144 
4145 	// Fire manual trigger only when bottom plane is flipped
4146 	for (j = 0; j < dc->res_pool->pipe_count; j++) {
4147 		struct pipe_ctx *pipe_ctx = &context->res_ctx.pipe_ctx[j];
4148 
4149 		if (!pipe_ctx->plane_state)
4150 			continue;
4151 
4152 		if (pipe_ctx->bottom_pipe || pipe_ctx->next_odm_pipe ||
4153 				!pipe_ctx->stream || !should_update_pipe_for_stream(context, pipe_ctx, stream) ||
4154 				!pipe_ctx->plane_state->update_flags.bits.addr_update ||
4155 				pipe_ctx->plane_state->skip_manual_trigger)
4156 			continue;
4157 
4158 		if (pipe_ctx->stream_res.tg->funcs->program_manual_trigger)
4159 			pipe_ctx->stream_res.tg->funcs->program_manual_trigger(pipe_ctx->stream_res.tg);
4160 	}
4161 
4162 	current_stream_mask = get_stream_mask(dc, context);
4163 	if (current_stream_mask != context->stream_mask) {
4164 		context->stream_mask = current_stream_mask;
4165 		dc_dmub_srv_notify_stream_mask(dc->ctx->dmub_srv, current_stream_mask);
4166 	}
4167 }
4168 
4169 /**
4170  * could_mpcc_tree_change_for_active_pipes - Check if an OPP associated with MPCC might change
4171  *
4172  * @dc: Used to get the current state status
4173  * @stream: Target stream, which we want to remove the attached planes
4174  * @srf_updates: Array of surface updates
4175  * @surface_count: Number of surface update
4176  * @is_plane_addition: [in] Fill out with true if it is a plane addition case
4177  *
4178  * DCN32x and newer support a feature named Dynamic ODM which can conflict with
4179  * the MPO if used simultaneously in some specific configurations (e.g.,
4180  * 4k@144). This function checks if the incoming context requires applying a
4181  * transition state with unnecessary pipe splitting and ODM disabled to
4182  * circumvent our hardware limitations to prevent this edge case. If the OPP
4183  * associated with an MPCC might change due to plane additions, this function
4184  * returns true.
4185  *
4186  * Return:
4187  * Return true if OPP and MPCC might change, otherwise, return false.
4188  */
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)4189 static bool could_mpcc_tree_change_for_active_pipes(struct dc *dc,
4190 		struct dc_stream_state *stream,
4191 		struct dc_surface_update *srf_updates,
4192 		int surface_count,
4193 		bool *is_plane_addition)
4194 {
4195 
4196 	struct dc_stream_status *cur_stream_status = stream_get_status(dc->current_state, stream);
4197 	bool force_minimal_pipe_splitting = false;
4198 	bool subvp_active = false;
4199 	uint32_t i;
4200 
4201 	*is_plane_addition = false;
4202 
4203 	if (cur_stream_status &&
4204 			dc->current_state->stream_count > 0 &&
4205 			dc->debug.pipe_split_policy != MPC_SPLIT_AVOID) {
4206 		/* determine if minimal transition is required due to MPC*/
4207 		if (surface_count > 0) {
4208 			if (cur_stream_status->plane_count > surface_count) {
4209 				force_minimal_pipe_splitting = true;
4210 			} else if (cur_stream_status->plane_count < surface_count) {
4211 				force_minimal_pipe_splitting = true;
4212 				*is_plane_addition = true;
4213 			}
4214 		}
4215 	}
4216 
4217 	if (cur_stream_status &&
4218 			dc->current_state->stream_count == 1 &&
4219 			dc->debug.enable_single_display_2to1_odm_policy) {
4220 		/* determine if minimal transition is required due to dynamic ODM*/
4221 		if (surface_count > 0) {
4222 			if (cur_stream_status->plane_count > 2 && cur_stream_status->plane_count > surface_count) {
4223 				force_minimal_pipe_splitting = true;
4224 			} else if (surface_count > 2 && cur_stream_status->plane_count < surface_count) {
4225 				force_minimal_pipe_splitting = true;
4226 				*is_plane_addition = true;
4227 			}
4228 		}
4229 	}
4230 
4231 	for (i = 0; i < dc->res_pool->pipe_count; i++) {
4232 		struct pipe_ctx *pipe = &dc->current_state->res_ctx.pipe_ctx[i];
4233 
4234 		if (dc_state_get_pipe_subvp_type(dc->current_state, pipe) != SUBVP_NONE) {
4235 			subvp_active = true;
4236 			break;
4237 		}
4238 	}
4239 
4240 	/* For SubVP when adding or removing planes we need to add a minimal transition
4241 	 * (even when disabling all planes). Whenever disabling a phantom pipe, we
4242 	 * must use the minimal transition path to disable the pipe correctly.
4243 	 *
4244 	 * We want to use the minimal transition whenever subvp is active, not only if
4245 	 * a plane is being added / removed from a subvp stream (MPO plane can be added
4246 	 * to a DRR pipe of SubVP + DRR config, in which case we still want to run through
4247 	 * a min transition to disable subvp.
4248 	 */
4249 	if (cur_stream_status && subvp_active) {
4250 		/* determine if minimal transition is required due to SubVP*/
4251 		if (cur_stream_status->plane_count > surface_count) {
4252 			force_minimal_pipe_splitting = true;
4253 		} else if (cur_stream_status->plane_count < surface_count) {
4254 			force_minimal_pipe_splitting = true;
4255 			*is_plane_addition = true;
4256 		}
4257 	}
4258 
4259 	return force_minimal_pipe_splitting;
4260 }
4261 
4262 struct pipe_split_policy_backup {
4263 	bool dynamic_odm_policy;
4264 	bool subvp_policy;
4265 	enum pipe_split_policy mpc_policy;
4266 	char force_odm[MAX_PIPES];
4267 };
4268 
backup_and_set_minimal_pipe_split_policy(struct dc * dc,struct dc_state * context,struct pipe_split_policy_backup * policy)4269 static void backup_and_set_minimal_pipe_split_policy(struct dc *dc,
4270 		struct dc_state *context,
4271 		struct pipe_split_policy_backup *policy)
4272 {
4273 	int i;
4274 
4275 	if (!dc->config.is_vmin_only_asic) {
4276 		policy->mpc_policy = dc->debug.pipe_split_policy;
4277 		dc->debug.pipe_split_policy = MPC_SPLIT_AVOID;
4278 	}
4279 	policy->dynamic_odm_policy = dc->debug.enable_single_display_2to1_odm_policy;
4280 	dc->debug.enable_single_display_2to1_odm_policy = false;
4281 	policy->subvp_policy = dc->debug.force_disable_subvp;
4282 	dc->debug.force_disable_subvp = true;
4283 	for (i = 0; i < context->stream_count; i++) {
4284 		policy->force_odm[i] = context->streams[i]->debug.force_odm_combine_segments;
4285 		if (context->streams[i]->debug.allow_transition_for_forced_odm)
4286 			context->streams[i]->debug.force_odm_combine_segments = 0;
4287 	}
4288 }
4289 
restore_minimal_pipe_split_policy(struct dc * dc,struct dc_state * context,struct pipe_split_policy_backup * policy)4290 static void restore_minimal_pipe_split_policy(struct dc *dc,
4291 		struct dc_state *context,
4292 		struct pipe_split_policy_backup *policy)
4293 {
4294 	uint8_t i;
4295 
4296 	if (!dc->config.is_vmin_only_asic)
4297 		dc->debug.pipe_split_policy = policy->mpc_policy;
4298 	dc->debug.enable_single_display_2to1_odm_policy =
4299 			policy->dynamic_odm_policy;
4300 	dc->debug.force_disable_subvp = policy->subvp_policy;
4301 	for (i = 0; i < context->stream_count; i++)
4302 		context->streams[i]->debug.force_odm_combine_segments = policy->force_odm[i];
4303 }
4304 
release_minimal_transition_state(struct dc * dc,struct dc_state * minimal_transition_context,struct dc_state * base_context,struct pipe_split_policy_backup * policy)4305 static void release_minimal_transition_state(struct dc *dc,
4306 		struct dc_state *minimal_transition_context,
4307 		struct dc_state *base_context,
4308 		struct pipe_split_policy_backup *policy)
4309 {
4310 	restore_minimal_pipe_split_policy(dc, base_context, policy);
4311 	dc_state_release(minimal_transition_context);
4312 }
4313 
force_vsync_flip_in_minimal_transition_context(struct dc_state * context)4314 static void force_vsync_flip_in_minimal_transition_context(struct dc_state *context)
4315 {
4316 	uint8_t i;
4317 	int j;
4318 	struct dc_stream_status *stream_status;
4319 
4320 	for (i = 0; i < context->stream_count; i++) {
4321 		stream_status = &context->stream_status[i];
4322 
4323 		for (j = 0; j < stream_status->plane_count; j++)
4324 			stream_status->plane_states[j]->flip_immediate = false;
4325 	}
4326 }
4327 
create_minimal_transition_state(struct dc * dc,struct dc_state * base_context,struct pipe_split_policy_backup * policy)4328 static struct dc_state *create_minimal_transition_state(struct dc *dc,
4329 		struct dc_state *base_context, struct pipe_split_policy_backup *policy)
4330 {
4331 	struct dc_state *minimal_transition_context = NULL;
4332 
4333 	minimal_transition_context = dc_state_create_copy(base_context);
4334 	if (!minimal_transition_context)
4335 		return NULL;
4336 
4337 	backup_and_set_minimal_pipe_split_policy(dc, base_context, policy);
4338 	/* commit minimal state */
4339 	if (dc->res_pool->funcs->validate_bandwidth(dc, minimal_transition_context, false)) {
4340 		/* prevent underflow and corruption when reconfiguring pipes */
4341 		force_vsync_flip_in_minimal_transition_context(minimal_transition_context);
4342 	} else {
4343 		/*
4344 		 * This should never happen, minimal transition state should
4345 		 * always be validated first before adding pipe split features.
4346 		 */
4347 		release_minimal_transition_state(dc, minimal_transition_context, base_context, policy);
4348 		BREAK_TO_DEBUGGER();
4349 		minimal_transition_context = NULL;
4350 	}
4351 	return minimal_transition_context;
4352 }
4353 
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)4354 static bool is_pipe_topology_transition_seamless_with_intermediate_step(
4355 		struct dc *dc,
4356 		struct dc_state *initial_state,
4357 		struct dc_state *intermediate_state,
4358 		struct dc_state *final_state)
4359 {
4360 	return dc->hwss.is_pipe_topology_transition_seamless(dc, initial_state,
4361 			intermediate_state) &&
4362 			dc->hwss.is_pipe_topology_transition_seamless(dc,
4363 					intermediate_state, final_state);
4364 }
4365 
swap_and_release_current_context(struct dc * dc,struct dc_state * new_context,struct dc_stream_state * stream)4366 static void swap_and_release_current_context(struct dc *dc,
4367 		struct dc_state *new_context, struct dc_stream_state *stream)
4368 {
4369 
4370 	int i;
4371 	struct dc_state *old = dc->current_state;
4372 	struct pipe_ctx *pipe_ctx;
4373 
4374 	/* Since memory free requires elevated IRQ, an interrupt
4375 	 * request is generated by mem free. If this happens
4376 	 * between freeing and reassigning the context, our vsync
4377 	 * interrupt will call into dc and cause a memory
4378 	 * corruption. Hence, we first reassign the context,
4379 	 * then free the old context.
4380 	 */
4381 	dc->current_state = new_context;
4382 	dc_state_release(old);
4383 
4384 	// clear any forced full updates
4385 	for (i = 0; i < dc->res_pool->pipe_count; i++) {
4386 		pipe_ctx = &new_context->res_ctx.pipe_ctx[i];
4387 
4388 		if (pipe_ctx->plane_state && pipe_ctx->stream == stream)
4389 			pipe_ctx->plane_state->force_full_update = false;
4390 	}
4391 }
4392 
initialize_empty_surface_updates(struct dc_stream_state * stream,struct dc_surface_update * srf_updates)4393 static int initialize_empty_surface_updates(
4394 		struct dc_stream_state *stream,
4395 		struct dc_surface_update *srf_updates)
4396 {
4397 	struct dc_stream_status *status = dc_stream_get_status(stream);
4398 	int i;
4399 
4400 	if (!status)
4401 		return 0;
4402 
4403 	for (i = 0; i < status->plane_count; i++)
4404 		srf_updates[i].surface = status->plane_states[i];
4405 
4406 	return status->plane_count;
4407 }
4408 
commit_minimal_transition_based_on_new_context(struct dc * dc,struct dc_state * new_context,struct dc_stream_state * stream,struct dc_surface_update * srf_updates,int surface_count)4409 static bool commit_minimal_transition_based_on_new_context(struct dc *dc,
4410 		struct dc_state *new_context,
4411 		struct dc_stream_state *stream,
4412 		struct dc_surface_update *srf_updates,
4413 		int surface_count)
4414 {
4415 	bool success = false;
4416 	struct pipe_split_policy_backup policy;
4417 	struct dc_state *intermediate_context =
4418 			create_minimal_transition_state(dc, new_context,
4419 					&policy);
4420 
4421 	if (intermediate_context) {
4422 		if (is_pipe_topology_transition_seamless_with_intermediate_step(
4423 				dc,
4424 				dc->current_state,
4425 				intermediate_context,
4426 				new_context)) {
4427 			DC_LOG_DC("commit minimal transition state: base = new state\n");
4428 			commit_planes_for_stream(dc, srf_updates,
4429 					surface_count, stream, NULL,
4430 					UPDATE_TYPE_FULL, intermediate_context);
4431 			swap_and_release_current_context(
4432 					dc, intermediate_context, stream);
4433 			dc_state_retain(dc->current_state);
4434 			success = true;
4435 		}
4436 		release_minimal_transition_state(
4437 				dc, intermediate_context, new_context, &policy);
4438 	}
4439 	return success;
4440 }
4441 
commit_minimal_transition_based_on_current_context(struct dc * dc,struct dc_state * new_context,struct dc_stream_state * stream)4442 static bool commit_minimal_transition_based_on_current_context(struct dc *dc,
4443 		struct dc_state *new_context, struct dc_stream_state *stream)
4444 {
4445 	bool success = false;
4446 	struct pipe_split_policy_backup policy;
4447 	struct dc_state *intermediate_context;
4448 	struct dc_state *old_current_state = dc->current_state;
4449 	struct dc_surface_update srf_updates[MAX_SURFACES] = {0};
4450 	int surface_count;
4451 
4452 	/*
4453 	 * Both current and new contexts share the same stream and plane state
4454 	 * pointers. When new context is validated, stream and planes get
4455 	 * populated with new updates such as new plane addresses. This makes
4456 	 * the current context no longer valid because stream and planes are
4457 	 * modified from the original. We backup current stream and plane states
4458 	 * into scratch space whenever we are populating new context. So we can
4459 	 * restore the original values back by calling the restore function now.
4460 	 * This restores back the original stream and plane states associated
4461 	 * with the current state.
4462 	 */
4463 	restore_planes_and_stream_state(&dc->scratch.current_state, stream);
4464 	dc_state_retain(old_current_state);
4465 	intermediate_context = create_minimal_transition_state(dc,
4466 			old_current_state, &policy);
4467 
4468 	if (intermediate_context) {
4469 		if (is_pipe_topology_transition_seamless_with_intermediate_step(
4470 				dc,
4471 				dc->current_state,
4472 				intermediate_context,
4473 				new_context)) {
4474 			DC_LOG_DC("commit minimal transition state: base = current state\n");
4475 			surface_count = initialize_empty_surface_updates(
4476 					stream, srf_updates);
4477 			commit_planes_for_stream(dc, srf_updates,
4478 					surface_count, stream, NULL,
4479 					UPDATE_TYPE_FULL, intermediate_context);
4480 			swap_and_release_current_context(
4481 					dc, intermediate_context, stream);
4482 			dc_state_retain(dc->current_state);
4483 			success = true;
4484 		}
4485 		release_minimal_transition_state(dc, intermediate_context,
4486 				old_current_state, &policy);
4487 	}
4488 	dc_state_release(old_current_state);
4489 	/*
4490 	 * Restore stream and plane states back to the values associated with
4491 	 * new context.
4492 	 */
4493 	restore_planes_and_stream_state(&dc->scratch.new_state, stream);
4494 	return success;
4495 }
4496 
4497 /**
4498  * commit_minimal_transition_state_in_dc_update - Commit a minimal state based
4499  * on current or new context
4500  *
4501  * @dc: DC structure, used to get the current state
4502  * @new_context: New context
4503  * @stream: Stream getting the update for the flip
4504  * @srf_updates: Surface updates
4505  * @surface_count: Number of surfaces
4506  *
4507  * The function takes in current state and new state and determine a minimal
4508  * transition state as the intermediate step which could make the transition
4509  * between current and new states seamless. If found, it will commit the minimal
4510  * transition state and update current state to this minimal transition state
4511  * and return true, if not, it will return false.
4512  *
4513  * Return:
4514  * Return True if the minimal transition succeeded, false otherwise
4515  */
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)4516 static bool commit_minimal_transition_state_in_dc_update(struct dc *dc,
4517 		struct dc_state *new_context,
4518 		struct dc_stream_state *stream,
4519 		struct dc_surface_update *srf_updates,
4520 		int surface_count)
4521 {
4522 	bool success = commit_minimal_transition_based_on_new_context(
4523 				dc, new_context, stream, srf_updates,
4524 				surface_count);
4525 	if (!success)
4526 		success = commit_minimal_transition_based_on_current_context(dc,
4527 				new_context, stream);
4528 	if (!success)
4529 		DC_LOG_ERROR("Fail to commit a seamless minimal transition state between current and new states.\nThis pipe topology update is non-seamless!\n");
4530 	return success;
4531 }
4532 
4533 /**
4534  * commit_minimal_transition_state - Create a transition pipe split state
4535  *
4536  * @dc: Used to get the current state status
4537  * @transition_base_context: New transition state
4538  *
4539  * In some specific configurations, such as pipe split on multi-display with
4540  * MPO and/or Dynamic ODM, removing a plane may cause unsupported pipe
4541  * programming when moving to new planes. To mitigate those types of problems,
4542  * this function adds a transition state that minimizes pipe usage before
4543  * programming the new configuration. When adding a new plane, the current
4544  * state requires the least pipes, so it is applied without splitting. When
4545  * removing a plane, the new state requires the least pipes, so it is applied
4546  * without splitting.
4547  *
4548  * Return:
4549  * Return false if something is wrong in the transition state.
4550  */
commit_minimal_transition_state(struct dc * dc,struct dc_state * transition_base_context)4551 static bool commit_minimal_transition_state(struct dc *dc,
4552 		struct dc_state *transition_base_context)
4553 {
4554 	struct dc_state *transition_context;
4555 	struct pipe_split_policy_backup policy;
4556 	enum dc_status ret = DC_ERROR_UNEXPECTED;
4557 	unsigned int i, j;
4558 	unsigned int pipe_in_use = 0;
4559 	bool subvp_in_use = false;
4560 	bool odm_in_use = false;
4561 
4562 	/* check current pipes in use*/
4563 	for (i = 0; i < dc->res_pool->pipe_count; i++) {
4564 		struct pipe_ctx *pipe = &transition_base_context->res_ctx.pipe_ctx[i];
4565 
4566 		if (pipe->plane_state)
4567 			pipe_in_use++;
4568 	}
4569 
4570 	/* If SubVP is enabled and we are adding or removing planes from any main subvp
4571 	 * pipe, we must use the minimal transition.
4572 	 */
4573 	for (i = 0; i < dc->res_pool->pipe_count; i++) {
4574 		struct pipe_ctx *pipe = &dc->current_state->res_ctx.pipe_ctx[i];
4575 
4576 		if (pipe->stream && dc_state_get_pipe_subvp_type(dc->current_state, pipe) == SUBVP_PHANTOM) {
4577 			subvp_in_use = true;
4578 			break;
4579 		}
4580 	}
4581 
4582 	/* If ODM is enabled and we are adding or removing planes from any ODM
4583 	 * pipe, we must use the minimal transition.
4584 	 */
4585 	for (i = 0; i < dc->res_pool->pipe_count; i++) {
4586 		struct pipe_ctx *pipe = &transition_base_context->res_ctx.pipe_ctx[i];
4587 
4588 		if (resource_is_pipe_type(pipe, OTG_MASTER)) {
4589 			odm_in_use = resource_get_odm_slice_count(pipe) > 1;
4590 			break;
4591 		}
4592 	}
4593 
4594 	/* When the OS add a new surface if we have been used all of pipes with odm combine
4595 	 * and mpc split feature, it need use commit_minimal_transition_state to transition safely.
4596 	 * After OS exit MPO, it will back to use odm and mpc split with all of pipes, we need
4597 	 * call it again. Otherwise return true to skip.
4598 	 *
4599 	 * Reduce the scenarios to use dc_commit_state_no_check in the stage of flip. Especially
4600 	 * enter/exit MPO when DCN still have enough resources.
4601 	 */
4602 	if (pipe_in_use != dc->res_pool->pipe_count && !subvp_in_use && !odm_in_use)
4603 		return true;
4604 
4605 	DC_LOG_DC("%s base = %s state, reason = %s\n", __func__,
4606 			dc->current_state == transition_base_context ? "current" : "new",
4607 			subvp_in_use ? "Subvp In Use" :
4608 			odm_in_use ? "ODM in Use" :
4609 			dc->debug.pipe_split_policy != MPC_SPLIT_AVOID ? "MPC in Use" :
4610 			"Unknown");
4611 
4612 	dc_state_retain(transition_base_context);
4613 	transition_context = create_minimal_transition_state(dc,
4614 			transition_base_context, &policy);
4615 	if (transition_context) {
4616 		ret = dc_commit_state_no_check(dc, transition_context);
4617 		release_minimal_transition_state(dc, transition_context, transition_base_context, &policy);
4618 	}
4619 	dc_state_release(transition_base_context);
4620 
4621 	if (ret != DC_OK) {
4622 		/* this should never happen */
4623 		BREAK_TO_DEBUGGER();
4624 		return false;
4625 	}
4626 
4627 	/* force full surface update */
4628 	for (i = 0; i < dc->current_state->stream_count; i++) {
4629 		for (j = 0; j < dc->current_state->stream_status[i].plane_count; j++) {
4630 			dc->current_state->stream_status[i].plane_states[j]->update_flags.raw = 0xFFFFFFFF;
4631 		}
4632 	}
4633 
4634 	return true;
4635 }
4636 
populate_fast_updates(struct dc_fast_update * fast_update,struct dc_surface_update * srf_updates,int surface_count,struct dc_stream_update * stream_update)4637 void populate_fast_updates(struct dc_fast_update *fast_update,
4638 		struct dc_surface_update *srf_updates,
4639 		int surface_count,
4640 		struct dc_stream_update *stream_update)
4641 {
4642 	int i = 0;
4643 
4644 	if (stream_update) {
4645 		fast_update[0].out_transfer_func = stream_update->out_transfer_func;
4646 		fast_update[0].output_csc_transform = stream_update->output_csc_transform;
4647 	} else {
4648 		fast_update[0].out_transfer_func = NULL;
4649 		fast_update[0].output_csc_transform = NULL;
4650 	}
4651 
4652 	for (i = 0; i < surface_count; i++) {
4653 		fast_update[i].flip_addr = srf_updates[i].flip_addr;
4654 		fast_update[i].gamma = srf_updates[i].gamma;
4655 		fast_update[i].gamut_remap_matrix = srf_updates[i].gamut_remap_matrix;
4656 		fast_update[i].input_csc_color_matrix = srf_updates[i].input_csc_color_matrix;
4657 		fast_update[i].coeff_reduction_factor = srf_updates[i].coeff_reduction_factor;
4658 		fast_update[i].cursor_csc_color_matrix = srf_updates[i].cursor_csc_color_matrix;
4659 	}
4660 }
4661 
fast_updates_exist(struct dc_fast_update * fast_update,int surface_count)4662 static bool fast_updates_exist(struct dc_fast_update *fast_update, int surface_count)
4663 {
4664 	int i;
4665 
4666 	if (fast_update[0].out_transfer_func ||
4667 		fast_update[0].output_csc_transform)
4668 		return true;
4669 
4670 	for (i = 0; i < surface_count; i++) {
4671 		if (fast_update[i].flip_addr ||
4672 				fast_update[i].gamma ||
4673 				fast_update[i].gamut_remap_matrix ||
4674 				fast_update[i].input_csc_color_matrix ||
4675 				fast_update[i].cursor_csc_color_matrix ||
4676 				fast_update[i].coeff_reduction_factor)
4677 			return true;
4678 	}
4679 
4680 	return false;
4681 }
4682 
fast_nonaddr_updates_exist(struct dc_fast_update * fast_update,int surface_count)4683 bool fast_nonaddr_updates_exist(struct dc_fast_update *fast_update, int surface_count)
4684 {
4685 	int i;
4686 
4687 	if (fast_update[0].out_transfer_func ||
4688 		fast_update[0].output_csc_transform)
4689 		return true;
4690 
4691 	for (i = 0; i < surface_count; i++) {
4692 		if (fast_update[i].input_csc_color_matrix ||
4693 				fast_update[i].gamma ||
4694 				fast_update[i].gamut_remap_matrix ||
4695 				fast_update[i].coeff_reduction_factor ||
4696 				fast_update[i].cursor_csc_color_matrix)
4697 			return true;
4698 	}
4699 
4700 	return false;
4701 }
4702 
full_update_required(struct dc * dc,struct dc_surface_update * srf_updates,int surface_count,struct dc_stream_update * stream_update,struct dc_stream_state * stream)4703 static bool full_update_required(struct dc *dc,
4704 		struct dc_surface_update *srf_updates,
4705 		int surface_count,
4706 		struct dc_stream_update *stream_update,
4707 		struct dc_stream_state *stream)
4708 {
4709 
4710 	int i;
4711 	struct dc_stream_status *stream_status;
4712 	const struct dc_state *context = dc->current_state;
4713 
4714 	for (i = 0; i < surface_count; i++) {
4715 		if (srf_updates &&
4716 				(srf_updates[i].plane_info ||
4717 				srf_updates[i].scaling_info ||
4718 				(srf_updates[i].hdr_mult.value &&
4719 				srf_updates[i].hdr_mult.value != srf_updates->surface->hdr_mult.value) ||
4720 				(srf_updates[i].sdr_white_level_nits &&
4721 				srf_updates[i].sdr_white_level_nits != srf_updates->surface->sdr_white_level_nits) ||
4722 				srf_updates[i].in_transfer_func ||
4723 				srf_updates[i].func_shaper ||
4724 				srf_updates[i].lut3d_func ||
4725 				srf_updates[i].surface->force_full_update ||
4726 				(srf_updates[i].flip_addr &&
4727 				srf_updates[i].flip_addr->address.tmz_surface != srf_updates[i].surface->address.tmz_surface) ||
4728 				(srf_updates[i].cm2_params &&
4729 				 (srf_updates[i].cm2_params->component_settings.shaper_3dlut_setting != srf_updates[i].surface->mcm_shaper_3dlut_setting ||
4730 				  srf_updates[i].cm2_params->component_settings.lut1d_enable != srf_updates[i].surface->mcm_lut1d_enable)) ||
4731 				!is_surface_in_context(context, srf_updates[i].surface)))
4732 			return true;
4733 	}
4734 
4735 	if (stream_update &&
4736 			(((stream_update->src.height != 0 && stream_update->src.width != 0) ||
4737 			(stream_update->dst.height != 0 && stream_update->dst.width != 0) ||
4738 			stream_update->integer_scaling_update) ||
4739 			stream_update->hdr_static_metadata ||
4740 			stream_update->abm_level ||
4741 			stream_update->periodic_interrupt ||
4742 			stream_update->vrr_infopacket ||
4743 			stream_update->vsc_infopacket ||
4744 			stream_update->vsp_infopacket ||
4745 			stream_update->hfvsif_infopacket ||
4746 			stream_update->vtem_infopacket ||
4747 			stream_update->adaptive_sync_infopacket ||
4748 			stream_update->dpms_off ||
4749 			stream_update->allow_freesync ||
4750 			stream_update->vrr_active_variable ||
4751 			stream_update->vrr_active_fixed ||
4752 			stream_update->gamut_remap ||
4753 			stream_update->output_color_space ||
4754 			stream_update->dither_option ||
4755 			stream_update->wb_update ||
4756 			stream_update->dsc_config ||
4757 			stream_update->mst_bw_update ||
4758 			stream_update->func_shaper ||
4759 			stream_update->lut3d_func ||
4760 			stream_update->pending_test_pattern ||
4761 			stream_update->crtc_timing_adjust ||
4762 			stream_update->scaler_sharpener_update ||
4763 			stream_update->hw_cursor_req))
4764 		return true;
4765 
4766 	if (stream) {
4767 		stream_status = dc_stream_get_status(stream);
4768 		if (stream_status == NULL || stream_status->plane_count != surface_count)
4769 			return true;
4770 	}
4771 	if (dc->idle_optimizations_allowed)
4772 		return true;
4773 
4774 	return false;
4775 }
4776 
fast_update_only(struct dc * dc,struct dc_fast_update * fast_update,struct dc_surface_update * srf_updates,int surface_count,struct dc_stream_update * stream_update,struct dc_stream_state * stream)4777 static bool fast_update_only(struct dc *dc,
4778 		struct dc_fast_update *fast_update,
4779 		struct dc_surface_update *srf_updates,
4780 		int surface_count,
4781 		struct dc_stream_update *stream_update,
4782 		struct dc_stream_state *stream)
4783 {
4784 	return fast_updates_exist(fast_update, surface_count)
4785 			&& !full_update_required(dc, srf_updates, surface_count, stream_update, stream);
4786 }
4787 
update_planes_and_stream_v1(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)4788 static bool update_planes_and_stream_v1(struct dc *dc,
4789 		struct dc_surface_update *srf_updates, int surface_count,
4790 		struct dc_stream_state *stream,
4791 		struct dc_stream_update *stream_update,
4792 		struct dc_state *state)
4793 {
4794 	const struct dc_stream_status *stream_status;
4795 	enum surface_update_type update_type;
4796 	struct dc_state *context;
4797 	struct dc_context *dc_ctx = dc->ctx;
4798 	int i, j;
4799 	struct dc_fast_update fast_update[MAX_SURFACES] = {0};
4800 
4801 	dc_exit_ips_for_hw_access(dc);
4802 
4803 	populate_fast_updates(fast_update, srf_updates, surface_count, stream_update);
4804 	stream_status = dc_stream_get_status(stream);
4805 	context = dc->current_state;
4806 
4807 	update_type = dc_check_update_surfaces_for_stream(
4808 				dc, srf_updates, surface_count, stream_update, stream_status);
4809 
4810 	if (update_type >= UPDATE_TYPE_FULL) {
4811 
4812 		/* initialize scratch memory for building context */
4813 		context = dc_state_create_copy(state);
4814 		if (context == NULL) {
4815 			DC_ERROR("Failed to allocate new validate context!\n");
4816 			return false;
4817 		}
4818 
4819 		for (i = 0; i < dc->res_pool->pipe_count; i++) {
4820 			struct pipe_ctx *new_pipe = &context->res_ctx.pipe_ctx[i];
4821 			struct pipe_ctx *old_pipe = &dc->current_state->res_ctx.pipe_ctx[i];
4822 
4823 			if (new_pipe->plane_state && new_pipe->plane_state != old_pipe->plane_state)
4824 				new_pipe->plane_state->force_full_update = true;
4825 		}
4826 	} else if (update_type == UPDATE_TYPE_FAST) {
4827 		/*
4828 		 * Previous frame finished and HW is ready for optimization.
4829 		 */
4830 		dc_post_update_surfaces_to_stream(dc);
4831 	}
4832 
4833 	for (i = 0; i < surface_count; i++) {
4834 		struct dc_plane_state *surface = srf_updates[i].surface;
4835 
4836 		copy_surface_update_to_plane(surface, &srf_updates[i]);
4837 
4838 		if (update_type >= UPDATE_TYPE_MED) {
4839 			for (j = 0; j < dc->res_pool->pipe_count; j++) {
4840 				struct pipe_ctx *pipe_ctx =
4841 					&context->res_ctx.pipe_ctx[j];
4842 
4843 				if (pipe_ctx->plane_state != surface)
4844 					continue;
4845 
4846 				resource_build_scaling_params(pipe_ctx);
4847 			}
4848 		}
4849 	}
4850 
4851 	copy_stream_update_to_stream(dc, context, stream, stream_update);
4852 
4853 	if (update_type >= UPDATE_TYPE_FULL) {
4854 		if (!dc->res_pool->funcs->validate_bandwidth(dc, context, false)) {
4855 			DC_ERROR("Mode validation failed for stream update!\n");
4856 			dc_state_release(context);
4857 			return false;
4858 		}
4859 	}
4860 
4861 	TRACE_DC_PIPE_STATE(pipe_ctx, i, MAX_PIPES);
4862 
4863 	if (fast_update_only(dc, fast_update, srf_updates, surface_count, stream_update, stream) &&
4864 			!dc->debug.enable_legacy_fast_update) {
4865 		commit_planes_for_stream_fast(dc,
4866 				srf_updates,
4867 				surface_count,
4868 				stream,
4869 				stream_update,
4870 				update_type,
4871 				context);
4872 	} else {
4873 		commit_planes_for_stream(
4874 				dc,
4875 				srf_updates,
4876 				surface_count,
4877 				stream,
4878 				stream_update,
4879 				update_type,
4880 				context);
4881 	}
4882 	/*update current_State*/
4883 	if (dc->current_state != context) {
4884 
4885 		struct dc_state *old = dc->current_state;
4886 
4887 		dc->current_state = context;
4888 		dc_state_release(old);
4889 
4890 		for (i = 0; i < dc->res_pool->pipe_count; i++) {
4891 			struct pipe_ctx *pipe_ctx = &context->res_ctx.pipe_ctx[i];
4892 
4893 			if (pipe_ctx->plane_state && pipe_ctx->stream == stream)
4894 				pipe_ctx->plane_state->force_full_update = false;
4895 		}
4896 	}
4897 
4898 	/* Legacy optimization path for DCE. */
4899 	if (update_type >= UPDATE_TYPE_FULL && dc_ctx->dce_version < DCE_VERSION_MAX) {
4900 		dc_post_update_surfaces_to_stream(dc);
4901 		TRACE_DCE_CLOCK_STATE(&context->bw_ctx.bw.dce);
4902 	}
4903 	return true;
4904 }
4905 
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)4906 static bool update_planes_and_stream_v2(struct dc *dc,
4907 		struct dc_surface_update *srf_updates, int surface_count,
4908 		struct dc_stream_state *stream,
4909 		struct dc_stream_update *stream_update)
4910 {
4911 	struct dc_state *context;
4912 	enum surface_update_type update_type;
4913 	struct dc_fast_update fast_update[MAX_SURFACES] = {0};
4914 
4915 	/* In cases where MPO and split or ODM are used transitions can
4916 	 * cause underflow. Apply stream configuration with minimal pipe
4917 	 * split first to avoid unsupported transitions for active pipes.
4918 	 */
4919 	bool force_minimal_pipe_splitting = 0;
4920 	bool is_plane_addition = 0;
4921 	bool is_fast_update_only;
4922 
4923 	populate_fast_updates(fast_update, srf_updates, surface_count, stream_update);
4924 	is_fast_update_only = fast_update_only(dc, fast_update, srf_updates,
4925 			surface_count, stream_update, stream);
4926 	force_minimal_pipe_splitting = could_mpcc_tree_change_for_active_pipes(
4927 			dc,
4928 			stream,
4929 			srf_updates,
4930 			surface_count,
4931 			&is_plane_addition);
4932 
4933 	/* on plane addition, minimal state is the current one */
4934 	if (force_minimal_pipe_splitting && is_plane_addition &&
4935 		!commit_minimal_transition_state(dc, dc->current_state))
4936 		return false;
4937 
4938 	if (!update_planes_and_stream_state(
4939 			dc,
4940 			srf_updates,
4941 			surface_count,
4942 			stream,
4943 			stream_update,
4944 			&update_type,
4945 			&context))
4946 		return false;
4947 
4948 	/* on plane removal, minimal state is the new one */
4949 	if (force_minimal_pipe_splitting && !is_plane_addition) {
4950 		if (!commit_minimal_transition_state(dc, context)) {
4951 			dc_state_release(context);
4952 			return false;
4953 		}
4954 		update_type = UPDATE_TYPE_FULL;
4955 	}
4956 
4957 	if (dc->hwss.is_pipe_topology_transition_seamless &&
4958 			!dc->hwss.is_pipe_topology_transition_seamless(
4959 					dc, dc->current_state, context))
4960 		commit_minimal_transition_state_in_dc_update(dc, context, stream,
4961 				srf_updates, surface_count);
4962 
4963 	if (is_fast_update_only && !dc->debug.enable_legacy_fast_update) {
4964 		commit_planes_for_stream_fast(dc,
4965 				srf_updates,
4966 				surface_count,
4967 				stream,
4968 				stream_update,
4969 				update_type,
4970 				context);
4971 	} else {
4972 		if (!stream_update &&
4973 				dc->hwss.is_pipe_topology_transition_seamless &&
4974 				!dc->hwss.is_pipe_topology_transition_seamless(
4975 						dc, dc->current_state, context)) {
4976 			DC_LOG_ERROR("performing non-seamless pipe topology transition with surface only update!\n");
4977 			BREAK_TO_DEBUGGER();
4978 		}
4979 		commit_planes_for_stream(
4980 				dc,
4981 				srf_updates,
4982 				surface_count,
4983 				stream,
4984 				stream_update,
4985 				update_type,
4986 				context);
4987 	}
4988 	if (dc->current_state != context)
4989 		swap_and_release_current_context(dc, context, stream);
4990 	return true;
4991 }
4992 
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 surface_update_type update_type)4993 static void commit_planes_and_stream_update_on_current_context(struct dc *dc,
4994 		struct dc_surface_update *srf_updates, int surface_count,
4995 		struct dc_stream_state *stream,
4996 		struct dc_stream_update *stream_update,
4997 		enum surface_update_type update_type)
4998 {
4999 	struct dc_fast_update fast_update[MAX_SURFACES] = {0};
5000 
5001 	ASSERT(update_type < UPDATE_TYPE_FULL);
5002 	populate_fast_updates(fast_update, srf_updates, surface_count,
5003 			stream_update);
5004 	if (fast_update_only(dc, fast_update, srf_updates, surface_count,
5005 			stream_update, stream) &&
5006 			!dc->debug.enable_legacy_fast_update)
5007 		commit_planes_for_stream_fast(dc,
5008 				srf_updates,
5009 				surface_count,
5010 				stream,
5011 				stream_update,
5012 				update_type,
5013 				dc->current_state);
5014 	else
5015 		commit_planes_for_stream(
5016 				dc,
5017 				srf_updates,
5018 				surface_count,
5019 				stream,
5020 				stream_update,
5021 				update_type,
5022 				dc->current_state);
5023 }
5024 
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 surface_update_type update_type,struct dc_state * new_context)5025 static void commit_planes_and_stream_update_with_new_context(struct dc *dc,
5026 		struct dc_surface_update *srf_updates, int surface_count,
5027 		struct dc_stream_state *stream,
5028 		struct dc_stream_update *stream_update,
5029 		enum surface_update_type update_type,
5030 		struct dc_state *new_context)
5031 {
5032 	ASSERT(update_type >= UPDATE_TYPE_FULL);
5033 	if (!dc->hwss.is_pipe_topology_transition_seamless(dc,
5034 			dc->current_state, new_context))
5035 		/*
5036 		 * It is required by the feature design that all pipe topologies
5037 		 * using extra free pipes for power saving purposes such as
5038 		 * dynamic ODM or SubVp shall only be enabled when it can be
5039 		 * transitioned seamlessly to AND from its minimal transition
5040 		 * state. A minimal transition state is defined as the same dc
5041 		 * state but with all power saving features disabled. So it uses
5042 		 * the minimum pipe topology. When we can't seamlessly
5043 		 * transition from state A to state B, we will insert the
5044 		 * minimal transition state A' or B' in between so seamless
5045 		 * transition between A and B can be made possible.
5046 		 */
5047 		commit_minimal_transition_state_in_dc_update(dc, new_context,
5048 				stream, srf_updates, surface_count);
5049 
5050 	commit_planes_for_stream(
5051 			dc,
5052 			srf_updates,
5053 			surface_count,
5054 			stream,
5055 			stream_update,
5056 			update_type,
5057 			new_context);
5058 }
5059 
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)5060 static bool update_planes_and_stream_v3(struct dc *dc,
5061 		struct dc_surface_update *srf_updates, int surface_count,
5062 		struct dc_stream_state *stream,
5063 		struct dc_stream_update *stream_update)
5064 {
5065 	struct dc_state *new_context;
5066 	enum surface_update_type update_type;
5067 
5068 	/*
5069 	 * When this function returns true and new_context is not equal to
5070 	 * current state, the function allocates and validates a new dc state
5071 	 * and assigns it to new_context. The function expects that the caller
5072 	 * is responsible to free this memory when new_context is no longer
5073 	 * used. We swap current with new context and free current instead. So
5074 	 * new_context's memory will live until the next full update after it is
5075 	 * replaced by a newer context. Refer to the use of
5076 	 * swap_and_free_current_context below.
5077 	 */
5078 	if (!update_planes_and_stream_state(dc, srf_updates, surface_count,
5079 				stream, stream_update, &update_type,
5080 				&new_context))
5081 		return false;
5082 
5083 	if (new_context == dc->current_state) {
5084 		commit_planes_and_stream_update_on_current_context(dc,
5085 				srf_updates, surface_count, stream,
5086 				stream_update, update_type);
5087 	} else {
5088 		commit_planes_and_stream_update_with_new_context(dc,
5089 				srf_updates, surface_count, stream,
5090 				stream_update, update_type, new_context);
5091 		swap_and_release_current_context(dc, new_context, stream);
5092 	}
5093 
5094 	return true;
5095 }
5096 
clear_update_flags(struct dc_surface_update * srf_updates,int surface_count,struct dc_stream_state * stream)5097 static void clear_update_flags(struct dc_surface_update *srf_updates,
5098 	int surface_count, struct dc_stream_state *stream)
5099 {
5100 	int i;
5101 
5102 	if (stream)
5103 		stream->update_flags.raw = 0;
5104 
5105 	for (i = 0; i < surface_count; i++)
5106 		if (srf_updates[i].surface)
5107 			srf_updates[i].surface->update_flags.raw = 0;
5108 }
5109 
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)5110 bool dc_update_planes_and_stream(struct dc *dc,
5111 		struct dc_surface_update *srf_updates, int surface_count,
5112 		struct dc_stream_state *stream,
5113 		struct dc_stream_update *stream_update)
5114 {
5115 	bool ret = false;
5116 
5117 	dc_exit_ips_for_hw_access(dc);
5118 	/*
5119 	 * update planes and stream version 3 separates FULL and FAST updates
5120 	 * to their own sequences. It aims to clean up frequent checks for
5121 	 * update type resulting unnecessary branching in logic flow. It also
5122 	 * adds a new commit minimal transition sequence, which detects the need
5123 	 * for minimal transition based on the actual comparison of current and
5124 	 * new states instead of "predicting" it based on per feature software
5125 	 * policy.i.e could_mpcc_tree_change_for_active_pipes. The new commit
5126 	 * minimal transition sequence is made universal to any power saving
5127 	 * features that would use extra free pipes such as Dynamic ODM/MPC
5128 	 * Combine, MPO or SubVp. Therefore there is no longer a need to
5129 	 * specially handle compatibility problems with transitions among those
5130 	 * features as they are now transparent to the new sequence.
5131 	 */
5132 	if (dc->ctx->dce_version >= DCN_VERSION_4_01)
5133 		ret = update_planes_and_stream_v3(dc, srf_updates,
5134 				surface_count, stream, stream_update);
5135 	else
5136 		ret = update_planes_and_stream_v2(dc, srf_updates,
5137 			surface_count, stream, stream_update);
5138 	if (ret && (dc->ctx->dce_version >= DCN_VERSION_3_2 ||
5139 		dc->ctx->dce_version == DCN_VERSION_3_01))
5140 		clear_update_flags(srf_updates, surface_count, stream);
5141 
5142 	return ret;
5143 }
5144 
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)5145 void dc_commit_updates_for_stream(struct dc *dc,
5146 		struct dc_surface_update *srf_updates,
5147 		int surface_count,
5148 		struct dc_stream_state *stream,
5149 		struct dc_stream_update *stream_update,
5150 		struct dc_state *state)
5151 {
5152 	bool ret = false;
5153 
5154 	dc_exit_ips_for_hw_access(dc);
5155 	/* TODO: Since change commit sequence can have a huge impact,
5156 	 * we decided to only enable it for DCN3x. However, as soon as
5157 	 * we get more confident about this change we'll need to enable
5158 	 * the new sequence for all ASICs.
5159 	 */
5160 	if (dc->ctx->dce_version >= DCN_VERSION_4_01) {
5161 		ret = update_planes_and_stream_v3(dc, srf_updates, surface_count,
5162 				stream, stream_update);
5163 	} else if (dc->ctx->dce_version >= DCN_VERSION_3_2) {
5164 		ret = update_planes_and_stream_v2(dc, srf_updates, surface_count,
5165 				stream, stream_update);
5166 	} else
5167 		ret = update_planes_and_stream_v1(dc, srf_updates, surface_count, stream,
5168 				stream_update, state);
5169 
5170 	if (ret && dc->ctx->dce_version >= DCN_VERSION_3_2)
5171 		clear_update_flags(srf_updates, surface_count, stream);
5172 }
5173 
dc_get_current_stream_count(struct dc * dc)5174 uint8_t dc_get_current_stream_count(struct dc *dc)
5175 {
5176 	return dc->current_state->stream_count;
5177 }
5178 
dc_get_stream_at_index(struct dc * dc,uint8_t i)5179 struct dc_stream_state *dc_get_stream_at_index(struct dc *dc, uint8_t i)
5180 {
5181 	if (i < dc->current_state->stream_count)
5182 		return dc->current_state->streams[i];
5183 	return NULL;
5184 }
5185 
dc_interrupt_to_irq_source(struct dc * dc,uint32_t src_id,uint32_t ext_id)5186 enum dc_irq_source dc_interrupt_to_irq_source(
5187 		struct dc *dc,
5188 		uint32_t src_id,
5189 		uint32_t ext_id)
5190 {
5191 	return dal_irq_service_to_irq_source(dc->res_pool->irqs, src_id, ext_id);
5192 }
5193 
5194 /*
5195  * dc_interrupt_set() - Enable/disable an AMD hw interrupt source
5196  */
dc_interrupt_set(struct dc * dc,enum dc_irq_source src,bool enable)5197 bool dc_interrupt_set(struct dc *dc, enum dc_irq_source src, bool enable)
5198 {
5199 
5200 	if (dc == NULL)
5201 		return false;
5202 
5203 	return dal_irq_service_set(dc->res_pool->irqs, src, enable);
5204 }
5205 
dc_interrupt_ack(struct dc * dc,enum dc_irq_source src)5206 void dc_interrupt_ack(struct dc *dc, enum dc_irq_source src)
5207 {
5208 	dal_irq_service_ack(dc->res_pool->irqs, src);
5209 }
5210 
dc_power_down_on_boot(struct dc * dc)5211 void dc_power_down_on_boot(struct dc *dc)
5212 {
5213 	if (dc->ctx->dce_environment != DCE_ENV_VIRTUAL_HW &&
5214 	    dc->hwss.power_down_on_boot) {
5215 		if (dc->caps.ips_support)
5216 			dc_exit_ips_for_hw_access(dc);
5217 		dc->hwss.power_down_on_boot(dc);
5218 	}
5219 }
5220 
dc_set_power_state(struct dc * dc,enum dc_acpi_cm_power_state power_state)5221 void dc_set_power_state(struct dc *dc, enum dc_acpi_cm_power_state power_state)
5222 {
5223 	if (!dc->current_state)
5224 		return;
5225 
5226 	switch (power_state) {
5227 	case DC_ACPI_CM_POWER_STATE_D0:
5228 		dc_state_construct(dc, dc->current_state);
5229 
5230 		dc_exit_ips_for_hw_access(dc);
5231 
5232 		dc_z10_restore(dc);
5233 
5234 		dc_dmub_srv_notify_fw_dc_power_state(dc->ctx->dmub_srv, power_state);
5235 
5236 		dc->hwss.init_hw(dc);
5237 
5238 		if (dc->hwss.init_sys_ctx != NULL &&
5239 			dc->vm_pa_config.valid) {
5240 			dc->hwss.init_sys_ctx(dc->hwseq, dc, &dc->vm_pa_config);
5241 		}
5242 
5243 		break;
5244 	default:
5245 		ASSERT(dc->current_state->stream_count == 0);
5246 
5247 		dc_dmub_srv_notify_fw_dc_power_state(dc->ctx->dmub_srv, power_state);
5248 
5249 		dc_state_destruct(dc->current_state);
5250 
5251 		break;
5252 	}
5253 }
5254 
dc_resume(struct dc * dc)5255 void dc_resume(struct dc *dc)
5256 {
5257 	uint32_t i;
5258 
5259 	for (i = 0; i < dc->link_count; i++)
5260 		dc->link_srv->resume(dc->links[i]);
5261 }
5262 
dc_is_dmcu_initialized(struct dc * dc)5263 bool dc_is_dmcu_initialized(struct dc *dc)
5264 {
5265 	struct dmcu *dmcu = dc->res_pool->dmcu;
5266 
5267 	if (dmcu)
5268 		return dmcu->funcs->is_dmcu_initialized(dmcu);
5269 	return false;
5270 }
5271 
get_clock_requirements_for_state(struct dc_state * state,struct AsicStateEx * info)5272 void get_clock_requirements_for_state(struct dc_state *state, struct AsicStateEx *info)
5273 {
5274 	info->displayClock				= (unsigned int)state->bw_ctx.bw.dcn.clk.dispclk_khz;
5275 	info->engineClock				= (unsigned int)state->bw_ctx.bw.dcn.clk.dcfclk_khz;
5276 	info->memoryClock				= (unsigned int)state->bw_ctx.bw.dcn.clk.dramclk_khz;
5277 	info->maxSupportedDppClock		= (unsigned int)state->bw_ctx.bw.dcn.clk.max_supported_dppclk_khz;
5278 	info->dppClock					= (unsigned int)state->bw_ctx.bw.dcn.clk.dppclk_khz;
5279 	info->socClock					= (unsigned int)state->bw_ctx.bw.dcn.clk.socclk_khz;
5280 	info->dcfClockDeepSleep			= (unsigned int)state->bw_ctx.bw.dcn.clk.dcfclk_deep_sleep_khz;
5281 	info->fClock					= (unsigned int)state->bw_ctx.bw.dcn.clk.fclk_khz;
5282 	info->phyClock					= (unsigned int)state->bw_ctx.bw.dcn.clk.phyclk_khz;
5283 }
dc_set_clock(struct dc * dc,enum dc_clock_type clock_type,uint32_t clk_khz,uint32_t stepping)5284 enum dc_status dc_set_clock(struct dc *dc, enum dc_clock_type clock_type, uint32_t clk_khz, uint32_t stepping)
5285 {
5286 	if (dc->hwss.set_clock)
5287 		return dc->hwss.set_clock(dc, clock_type, clk_khz, stepping);
5288 	return DC_ERROR_UNEXPECTED;
5289 }
dc_get_clock(struct dc * dc,enum dc_clock_type clock_type,struct dc_clock_config * clock_cfg)5290 void dc_get_clock(struct dc *dc, enum dc_clock_type clock_type, struct dc_clock_config *clock_cfg)
5291 {
5292 	if (dc->hwss.get_clock)
5293 		dc->hwss.get_clock(dc, clock_type, clock_cfg);
5294 }
5295 
5296 /* enable/disable eDP PSR without specify stream for eDP */
dc_set_psr_allow_active(struct dc * dc,bool enable)5297 bool dc_set_psr_allow_active(struct dc *dc, bool enable)
5298 {
5299 	int i;
5300 	bool allow_active;
5301 
5302 	for (i = 0; i < dc->current_state->stream_count ; i++) {
5303 		struct dc_link *link;
5304 		struct dc_stream_state *stream = dc->current_state->streams[i];
5305 
5306 		link = stream->link;
5307 		if (!link)
5308 			continue;
5309 
5310 		if (link->psr_settings.psr_feature_enabled) {
5311 			if (enable && !link->psr_settings.psr_allow_active) {
5312 				allow_active = true;
5313 				if (!dc_link_set_psr_allow_active(link, &allow_active, false, false, NULL))
5314 					return false;
5315 			} else if (!enable && link->psr_settings.psr_allow_active) {
5316 				allow_active = false;
5317 				if (!dc_link_set_psr_allow_active(link, &allow_active, true, false, NULL))
5318 					return false;
5319 			}
5320 		}
5321 	}
5322 
5323 	return true;
5324 }
5325 
5326 /* enable/disable eDP Replay without specify stream for eDP */
dc_set_replay_allow_active(struct dc * dc,bool active)5327 bool dc_set_replay_allow_active(struct dc *dc, bool active)
5328 {
5329 	int i;
5330 	bool allow_active;
5331 
5332 	for (i = 0; i < dc->current_state->stream_count; i++) {
5333 		struct dc_link *link;
5334 		struct dc_stream_state *stream = dc->current_state->streams[i];
5335 
5336 		link = stream->link;
5337 		if (!link)
5338 			continue;
5339 
5340 		if (link->replay_settings.replay_feature_enabled) {
5341 			if (active && !link->replay_settings.replay_allow_active) {
5342 				allow_active = true;
5343 				if (!dc_link_set_replay_allow_active(link, &allow_active,
5344 					false, false, NULL))
5345 					return false;
5346 			} else if (!active && link->replay_settings.replay_allow_active) {
5347 				allow_active = false;
5348 				if (!dc_link_set_replay_allow_active(link, &allow_active,
5349 					true, false, NULL))
5350 					return false;
5351 			}
5352 		}
5353 	}
5354 
5355 	return true;
5356 }
5357 
5358 /* set IPS disable state */
dc_set_ips_disable(struct dc * dc,unsigned int disable_ips)5359 bool dc_set_ips_disable(struct dc *dc, unsigned int disable_ips)
5360 {
5361 	dc_exit_ips_for_hw_access(dc);
5362 
5363 	dc->config.disable_ips = disable_ips;
5364 
5365 	return true;
5366 }
5367 
dc_allow_idle_optimizations_internal(struct dc * dc,bool allow,char const * caller_name)5368 void dc_allow_idle_optimizations_internal(struct dc *dc, bool allow, char const *caller_name)
5369 {
5370 	if (dc->debug.disable_idle_power_optimizations)
5371 		return;
5372 
5373 	if (allow != dc->idle_optimizations_allowed)
5374 		DC_LOG_IPS("%s: allow_idle old=%d new=%d (caller=%s)\n", __func__,
5375 			   dc->idle_optimizations_allowed, allow, caller_name);
5376 
5377 	if (dc->caps.ips_support && (dc->config.disable_ips == DMUB_IPS_DISABLE_ALL))
5378 		return;
5379 
5380 	if (dc->clk_mgr != NULL && dc->clk_mgr->funcs->is_smu_present)
5381 		if (!dc->clk_mgr->funcs->is_smu_present(dc->clk_mgr))
5382 			return;
5383 
5384 	if (allow == dc->idle_optimizations_allowed)
5385 		return;
5386 
5387 	if (dc->hwss.apply_idle_power_optimizations && dc->clk_mgr != NULL &&
5388 	    dc->hwss.apply_idle_power_optimizations(dc, allow))
5389 		dc->idle_optimizations_allowed = allow;
5390 }
5391 
dc_exit_ips_for_hw_access_internal(struct dc * dc,const char * caller_name)5392 void dc_exit_ips_for_hw_access_internal(struct dc *dc, const char *caller_name)
5393 {
5394 	if (dc->caps.ips_support)
5395 		dc_allow_idle_optimizations_internal(dc, false, caller_name);
5396 }
5397 
dc_dmub_is_ips_idle_state(struct dc * dc)5398 bool dc_dmub_is_ips_idle_state(struct dc *dc)
5399 {
5400 	if (dc->debug.disable_idle_power_optimizations)
5401 		return false;
5402 
5403 	if (!dc->caps.ips_support || (dc->config.disable_ips == DMUB_IPS_DISABLE_ALL))
5404 		return false;
5405 
5406 	if (!dc->ctx->dmub_srv)
5407 		return false;
5408 
5409 	return dc->ctx->dmub_srv->idle_allowed;
5410 }
5411 
5412 /* set min and max memory clock to lowest and highest DPM level, respectively */
dc_unlock_memory_clock_frequency(struct dc * dc)5413 void dc_unlock_memory_clock_frequency(struct dc *dc)
5414 {
5415 	if (dc->clk_mgr->funcs->set_hard_min_memclk)
5416 		dc->clk_mgr->funcs->set_hard_min_memclk(dc->clk_mgr, false);
5417 
5418 	if (dc->clk_mgr->funcs->set_hard_max_memclk)
5419 		dc->clk_mgr->funcs->set_hard_max_memclk(dc->clk_mgr);
5420 }
5421 
5422 /* set min memory clock to the min required for current mode, max to maxDPM */
dc_lock_memory_clock_frequency(struct dc * dc)5423 void dc_lock_memory_clock_frequency(struct dc *dc)
5424 {
5425 	if (dc->clk_mgr->funcs->get_memclk_states_from_smu)
5426 		dc->clk_mgr->funcs->get_memclk_states_from_smu(dc->clk_mgr);
5427 
5428 	if (dc->clk_mgr->funcs->set_hard_min_memclk)
5429 		dc->clk_mgr->funcs->set_hard_min_memclk(dc->clk_mgr, true);
5430 
5431 	if (dc->clk_mgr->funcs->set_hard_max_memclk)
5432 		dc->clk_mgr->funcs->set_hard_max_memclk(dc->clk_mgr);
5433 }
5434 
blank_and_force_memclk(struct dc * dc,bool apply,unsigned int memclk_mhz)5435 static void blank_and_force_memclk(struct dc *dc, bool apply, unsigned int memclk_mhz)
5436 {
5437 	struct dc_state *context = dc->current_state;
5438 	struct hubp *hubp;
5439 	struct pipe_ctx *pipe;
5440 	int i;
5441 
5442 	for (i = 0; i < dc->res_pool->pipe_count; i++) {
5443 		pipe = &context->res_ctx.pipe_ctx[i];
5444 
5445 		if (pipe->stream != NULL) {
5446 			dc->hwss.disable_pixel_data(dc, pipe, true);
5447 
5448 			// wait for double buffer
5449 			pipe->stream_res.tg->funcs->wait_for_state(pipe->stream_res.tg, CRTC_STATE_VACTIVE);
5450 			pipe->stream_res.tg->funcs->wait_for_state(pipe->stream_res.tg, CRTC_STATE_VBLANK);
5451 			pipe->stream_res.tg->funcs->wait_for_state(pipe->stream_res.tg, CRTC_STATE_VACTIVE);
5452 
5453 			hubp = pipe->plane_res.hubp;
5454 			hubp->funcs->set_blank_regs(hubp, true);
5455 		}
5456 	}
5457 	if (dc->clk_mgr->funcs->set_max_memclk)
5458 		dc->clk_mgr->funcs->set_max_memclk(dc->clk_mgr, memclk_mhz);
5459 	if (dc->clk_mgr->funcs->set_min_memclk)
5460 		dc->clk_mgr->funcs->set_min_memclk(dc->clk_mgr, memclk_mhz);
5461 
5462 	for (i = 0; i < dc->res_pool->pipe_count; i++) {
5463 		pipe = &context->res_ctx.pipe_ctx[i];
5464 
5465 		if (pipe->stream != NULL) {
5466 			dc->hwss.disable_pixel_data(dc, pipe, false);
5467 
5468 			hubp = pipe->plane_res.hubp;
5469 			hubp->funcs->set_blank_regs(hubp, false);
5470 		}
5471 	}
5472 }
5473 
5474 
5475 /**
5476  * dc_enable_dcmode_clk_limit() - lower clocks in dc (battery) mode
5477  * @dc: pointer to dc of the dm calling this
5478  * @enable: True = transition to DC mode, false = transition back to AC mode
5479  *
5480  * Some SoCs define additional clock limits when in DC mode, DM should
5481  * invoke this function when the platform undergoes a power source transition
5482  * so DC can apply/unapply the limit. This interface may be disruptive to
5483  * the onscreen content.
5484  *
5485  * Context: Triggered by OS through DM interface, or manually by escape calls.
5486  * Need to hold a dclock when doing so.
5487  *
5488  * Return: none (void function)
5489  *
5490  */
dc_enable_dcmode_clk_limit(struct dc * dc,bool enable)5491 void dc_enable_dcmode_clk_limit(struct dc *dc, bool enable)
5492 {
5493 	unsigned int softMax = 0, maxDPM = 0, funcMin = 0, i;
5494 	bool p_state_change_support;
5495 
5496 	if (!dc->config.dc_mode_clk_limit_support)
5497 		return;
5498 
5499 	softMax = dc->clk_mgr->bw_params->dc_mode_softmax_memclk;
5500 	for (i = 0; i < dc->clk_mgr->bw_params->clk_table.num_entries; i++) {
5501 		if (dc->clk_mgr->bw_params->clk_table.entries[i].memclk_mhz > maxDPM)
5502 			maxDPM = dc->clk_mgr->bw_params->clk_table.entries[i].memclk_mhz;
5503 	}
5504 	funcMin = (dc->clk_mgr->clks.dramclk_khz + 999) / 1000;
5505 	p_state_change_support = dc->clk_mgr->clks.p_state_change_support;
5506 
5507 	if (enable && !dc->clk_mgr->dc_mode_softmax_enabled) {
5508 		if (p_state_change_support) {
5509 			if (funcMin <= softMax && dc->clk_mgr->funcs->set_max_memclk)
5510 				dc->clk_mgr->funcs->set_max_memclk(dc->clk_mgr, softMax);
5511 			// else: No-Op
5512 		} else {
5513 			if (funcMin <= softMax)
5514 				blank_and_force_memclk(dc, true, softMax);
5515 			// else: No-Op
5516 		}
5517 	} else if (!enable && dc->clk_mgr->dc_mode_softmax_enabled) {
5518 		if (p_state_change_support) {
5519 			if (funcMin <= softMax && dc->clk_mgr->funcs->set_max_memclk)
5520 				dc->clk_mgr->funcs->set_max_memclk(dc->clk_mgr, maxDPM);
5521 			// else: No-Op
5522 		} else {
5523 			if (funcMin <= softMax)
5524 				blank_and_force_memclk(dc, true, maxDPM);
5525 			// else: No-Op
5526 		}
5527 	}
5528 	dc->clk_mgr->dc_mode_softmax_enabled = enable;
5529 }
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)5530 bool dc_is_plane_eligible_for_idle_optimizations(struct dc *dc,
5531 		unsigned int pitch,
5532 		unsigned int height,
5533 		enum surface_pixel_format format,
5534 		struct dc_cursor_attributes *cursor_attr)
5535 {
5536 	if (dc->hwss.does_plane_fit_in_mall && dc->hwss.does_plane_fit_in_mall(dc, pitch, height, format, cursor_attr))
5537 		return true;
5538 	return false;
5539 }
5540 
5541 /* cleanup on driver unload */
dc_hardware_release(struct dc * dc)5542 void dc_hardware_release(struct dc *dc)
5543 {
5544 	dc_mclk_switch_using_fw_based_vblank_stretch_shut_down(dc);
5545 
5546 	if (dc->hwss.hardware_release)
5547 		dc->hwss.hardware_release(dc);
5548 }
5549 
dc_mclk_switch_using_fw_based_vblank_stretch_shut_down(struct dc * dc)5550 void dc_mclk_switch_using_fw_based_vblank_stretch_shut_down(struct dc *dc)
5551 {
5552 	if (dc->current_state)
5553 		dc->current_state->bw_ctx.bw.dcn.clk.fw_based_mclk_switching_shut_down = true;
5554 }
5555 
5556 /**
5557  * dc_is_dmub_outbox_supported - Check if DMUB firmware support outbox notification
5558  *
5559  * @dc: [in] dc structure
5560  *
5561  * Checks whether DMUB FW supports outbox notifications, if supported DM
5562  * should register outbox interrupt prior to actually enabling interrupts
5563  * via dc_enable_dmub_outbox
5564  *
5565  * Return:
5566  * True if DMUB FW supports outbox notifications, False otherwise
5567  */
dc_is_dmub_outbox_supported(struct dc * dc)5568 bool dc_is_dmub_outbox_supported(struct dc *dc)
5569 {
5570 	if (!dc->caps.dmcub_support)
5571 		return false;
5572 
5573 	switch (dc->ctx->asic_id.chip_family) {
5574 
5575 	case FAMILY_YELLOW_CARP:
5576 		/* DCN31 B0 USB4 DPIA needs dmub notifications for interrupts */
5577 		if (dc->ctx->asic_id.hw_internal_rev == YELLOW_CARP_B0 &&
5578 		    !dc->debug.dpia_debug.bits.disable_dpia)
5579 			return true;
5580 	break;
5581 
5582 	case AMDGPU_FAMILY_GC_11_0_1:
5583 	case AMDGPU_FAMILY_GC_11_5_0:
5584 		if (!dc->debug.dpia_debug.bits.disable_dpia)
5585 			return true;
5586 	break;
5587 
5588 	default:
5589 		break;
5590 	}
5591 
5592 	/* dmub aux needs dmub notifications to be enabled */
5593 	return dc->debug.enable_dmub_aux_for_legacy_ddc;
5594 
5595 }
5596 
5597 /**
5598  * dc_enable_dmub_notifications - Check if dmub fw supports outbox
5599  *
5600  * @dc: [in] dc structure
5601  *
5602  * Calls dc_is_dmub_outbox_supported to check if dmub fw supports outbox
5603  * notifications. All DMs shall switch to dc_is_dmub_outbox_supported.  This
5604  * API shall be removed after switching.
5605  *
5606  * Return:
5607  * True if DMUB FW supports outbox notifications, False otherwise
5608  */
dc_enable_dmub_notifications(struct dc * dc)5609 bool dc_enable_dmub_notifications(struct dc *dc)
5610 {
5611 	return dc_is_dmub_outbox_supported(dc);
5612 }
5613 
5614 /**
5615  * dc_enable_dmub_outbox - Enables DMUB unsolicited notification
5616  *
5617  * @dc: [in] dc structure
5618  *
5619  * Enables DMUB unsolicited notifications to x86 via outbox.
5620  */
dc_enable_dmub_outbox(struct dc * dc)5621 void dc_enable_dmub_outbox(struct dc *dc)
5622 {
5623 	struct dc_context *dc_ctx = dc->ctx;
5624 
5625 	dmub_enable_outbox_notification(dc_ctx->dmub_srv);
5626 	DC_LOG_DC("%s: dmub outbox notifications enabled\n", __func__);
5627 }
5628 
5629 /**
5630  * dc_process_dmub_aux_transfer_async - Submits aux command to dmub via inbox message
5631  *                                      Sets port index appropriately for legacy DDC
5632  * @dc: dc structure
5633  * @link_index: link index
5634  * @payload: aux payload
5635  *
5636  * Returns: True if successful, False if failure
5637  */
dc_process_dmub_aux_transfer_async(struct dc * dc,uint32_t link_index,struct aux_payload * payload)5638 bool dc_process_dmub_aux_transfer_async(struct dc *dc,
5639 				uint32_t link_index,
5640 				struct aux_payload *payload)
5641 {
5642 	uint8_t action;
5643 	union dmub_rb_cmd cmd = {0};
5644 
5645 	ASSERT(payload->length <= 16);
5646 
5647 	cmd.dp_aux_access.header.type = DMUB_CMD__DP_AUX_ACCESS;
5648 	cmd.dp_aux_access.header.payload_bytes = 0;
5649 	/* For dpia, ddc_pin is set to NULL */
5650 	if (!dc->links[link_index]->ddc->ddc_pin)
5651 		cmd.dp_aux_access.aux_control.type = AUX_CHANNEL_DPIA;
5652 	else
5653 		cmd.dp_aux_access.aux_control.type = AUX_CHANNEL_LEGACY_DDC;
5654 
5655 	cmd.dp_aux_access.aux_control.instance = dc->links[link_index]->ddc_hw_inst;
5656 	cmd.dp_aux_access.aux_control.sw_crc_enabled = 0;
5657 	cmd.dp_aux_access.aux_control.timeout = 0;
5658 	cmd.dp_aux_access.aux_control.dpaux.address = payload->address;
5659 	cmd.dp_aux_access.aux_control.dpaux.is_i2c_over_aux = payload->i2c_over_aux;
5660 	cmd.dp_aux_access.aux_control.dpaux.length = payload->length;
5661 
5662 	/* set aux action */
5663 	if (payload->i2c_over_aux) {
5664 		if (payload->write) {
5665 			if (payload->mot)
5666 				action = DP_AUX_REQ_ACTION_I2C_WRITE_MOT;
5667 			else
5668 				action = DP_AUX_REQ_ACTION_I2C_WRITE;
5669 		} else {
5670 			if (payload->mot)
5671 				action = DP_AUX_REQ_ACTION_I2C_READ_MOT;
5672 			else
5673 				action = DP_AUX_REQ_ACTION_I2C_READ;
5674 			}
5675 	} else {
5676 		if (payload->write)
5677 			action = DP_AUX_REQ_ACTION_DPCD_WRITE;
5678 		else
5679 			action = DP_AUX_REQ_ACTION_DPCD_READ;
5680 	}
5681 
5682 	cmd.dp_aux_access.aux_control.dpaux.action = action;
5683 
5684 	if (payload->length && payload->write) {
5685 		memcpy(cmd.dp_aux_access.aux_control.dpaux.data,
5686 			payload->data,
5687 			payload->length
5688 			);
5689 	}
5690 
5691 	dc_wake_and_execute_dmub_cmd(dc->ctx, &cmd, DM_DMUB_WAIT_TYPE_WAIT);
5692 
5693 	return true;
5694 }
5695 
get_link_index_from_dpia_port_index(const struct dc * dc,uint8_t dpia_port_index)5696 uint8_t get_link_index_from_dpia_port_index(const struct dc *dc,
5697 					    uint8_t dpia_port_index)
5698 {
5699 	uint8_t index, link_index = 0xFF;
5700 
5701 	for (index = 0; index < dc->link_count; index++) {
5702 		/* ddc_hw_inst has dpia port index for dpia links
5703 		 * and ddc instance for legacy links
5704 		 */
5705 		if (!dc->links[index]->ddc->ddc_pin) {
5706 			if (dc->links[index]->ddc_hw_inst == dpia_port_index) {
5707 				link_index = index;
5708 				break;
5709 			}
5710 		}
5711 	}
5712 	ASSERT(link_index != 0xFF);
5713 	return link_index;
5714 }
5715 
5716 /**
5717  * dc_process_dmub_set_config_async - Submits set_config command
5718  *
5719  * @dc: [in] dc structure
5720  * @link_index: [in] link_index: link index
5721  * @payload: [in] aux payload
5722  * @notify: [out] set_config immediate reply
5723  *
5724  * Submits set_config command to dmub via inbox message.
5725  *
5726  * Return:
5727  * True if successful, False if failure
5728  */
dc_process_dmub_set_config_async(struct dc * dc,uint32_t link_index,struct set_config_cmd_payload * payload,struct dmub_notification * notify)5729 bool dc_process_dmub_set_config_async(struct dc *dc,
5730 				uint32_t link_index,
5731 				struct set_config_cmd_payload *payload,
5732 				struct dmub_notification *notify)
5733 {
5734 	union dmub_rb_cmd cmd = {0};
5735 	bool is_cmd_complete = true;
5736 
5737 	/* prepare SET_CONFIG command */
5738 	cmd.set_config_access.header.type = DMUB_CMD__DPIA;
5739 	cmd.set_config_access.header.sub_type = DMUB_CMD__DPIA_SET_CONFIG_ACCESS;
5740 
5741 	cmd.set_config_access.set_config_control.instance = dc->links[link_index]->ddc_hw_inst;
5742 	cmd.set_config_access.set_config_control.cmd_pkt.msg_type = payload->msg_type;
5743 	cmd.set_config_access.set_config_control.cmd_pkt.msg_data = payload->msg_data;
5744 
5745 	if (!dc_wake_and_execute_dmub_cmd(dc->ctx, &cmd, DM_DMUB_WAIT_TYPE_WAIT_WITH_REPLY)) {
5746 		/* command is not processed by dmub */
5747 		notify->sc_status = SET_CONFIG_UNKNOWN_ERROR;
5748 		return is_cmd_complete;
5749 	}
5750 
5751 	/* command processed by dmub, if ret_status is 1, it is completed instantly */
5752 	if (cmd.set_config_access.header.ret_status == 1)
5753 		notify->sc_status = cmd.set_config_access.set_config_control.immed_status;
5754 	else
5755 		/* cmd pending, will receive notification via outbox */
5756 		is_cmd_complete = false;
5757 
5758 	return is_cmd_complete;
5759 }
5760 
5761 /**
5762  * dc_process_dmub_set_mst_slots - Submits MST solt allocation
5763  *
5764  * @dc: [in] dc structure
5765  * @link_index: [in] link index
5766  * @mst_alloc_slots: [in] mst slots to be allotted
5767  * @mst_slots_in_use: [out] mst slots in use returned in failure case
5768  *
5769  * Submits mst slot allocation command to dmub via inbox message
5770  *
5771  * Return:
5772  * DC_OK if successful, DC_ERROR if failure
5773  */
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)5774 enum dc_status dc_process_dmub_set_mst_slots(const struct dc *dc,
5775 				uint32_t link_index,
5776 				uint8_t mst_alloc_slots,
5777 				uint8_t *mst_slots_in_use)
5778 {
5779 	union dmub_rb_cmd cmd = {0};
5780 
5781 	/* prepare MST_ALLOC_SLOTS command */
5782 	cmd.set_mst_alloc_slots.header.type = DMUB_CMD__DPIA;
5783 	cmd.set_mst_alloc_slots.header.sub_type = DMUB_CMD__DPIA_MST_ALLOC_SLOTS;
5784 
5785 	cmd.set_mst_alloc_slots.mst_slots_control.instance = dc->links[link_index]->ddc_hw_inst;
5786 	cmd.set_mst_alloc_slots.mst_slots_control.mst_alloc_slots = mst_alloc_slots;
5787 
5788 	if (!dc_wake_and_execute_dmub_cmd(dc->ctx, &cmd, DM_DMUB_WAIT_TYPE_WAIT_WITH_REPLY))
5789 		/* command is not processed by dmub */
5790 		return DC_ERROR_UNEXPECTED;
5791 
5792 	/* command processed by dmub, if ret_status is 1 */
5793 	if (cmd.set_config_access.header.ret_status != 1)
5794 		/* command processing error */
5795 		return DC_ERROR_UNEXPECTED;
5796 
5797 	/* command processed and we have a status of 2, mst not enabled in dpia */
5798 	if (cmd.set_mst_alloc_slots.mst_slots_control.immed_status == 2)
5799 		return DC_FAIL_UNSUPPORTED_1;
5800 
5801 	/* previously configured mst alloc and used slots did not match */
5802 	if (cmd.set_mst_alloc_slots.mst_slots_control.immed_status == 3) {
5803 		*mst_slots_in_use = cmd.set_mst_alloc_slots.mst_slots_control.mst_slots_in_use;
5804 		return DC_NOT_SUPPORTED;
5805 	}
5806 
5807 	return DC_OK;
5808 }
5809 
5810 /**
5811  * dc_process_dmub_dpia_set_tps_notification - Submits tps notification
5812  *
5813  * @dc: [in] dc structure
5814  * @link_index: [in] link index
5815  * @tps: [in] request tps
5816  *
5817  * Submits set_tps_notification command to dmub via inbox message
5818  */
dc_process_dmub_dpia_set_tps_notification(const struct dc * dc,uint32_t link_index,uint8_t tps)5819 void dc_process_dmub_dpia_set_tps_notification(const struct dc *dc, uint32_t link_index, uint8_t tps)
5820 {
5821 	union dmub_rb_cmd cmd = {0};
5822 
5823 	cmd.set_tps_notification.header.type = DMUB_CMD__DPIA;
5824 	cmd.set_tps_notification.header.sub_type = DMUB_CMD__DPIA_SET_TPS_NOTIFICATION;
5825 	cmd.set_tps_notification.tps_notification.instance = dc->links[link_index]->ddc_hw_inst;
5826 	cmd.set_tps_notification.tps_notification.tps = tps;
5827 
5828 	dc_wake_and_execute_dmub_cmd(dc->ctx, &cmd, DM_DMUB_WAIT_TYPE_WAIT);
5829 }
5830 
5831 /**
5832  * dc_process_dmub_dpia_hpd_int_enable - Submits DPIA DPD interruption
5833  *
5834  * @dc: [in] dc structure
5835  * @hpd_int_enable: [in] 1 for hpd int enable, 0 to disable
5836  *
5837  * Submits dpia hpd int enable command to dmub via inbox message
5838  */
dc_process_dmub_dpia_hpd_int_enable(const struct dc * dc,uint32_t hpd_int_enable)5839 void dc_process_dmub_dpia_hpd_int_enable(const struct dc *dc,
5840 				uint32_t hpd_int_enable)
5841 {
5842 	union dmub_rb_cmd cmd = {0};
5843 
5844 	cmd.dpia_hpd_int_enable.header.type = DMUB_CMD__DPIA_HPD_INT_ENABLE;
5845 	cmd.dpia_hpd_int_enable.enable = hpd_int_enable;
5846 
5847 	dc_wake_and_execute_dmub_cmd(dc->ctx, &cmd, DM_DMUB_WAIT_TYPE_WAIT);
5848 
5849 	DC_LOG_DEBUG("%s: hpd_int_enable(%d)\n", __func__, hpd_int_enable);
5850 }
5851 
5852 /**
5853  * dc_print_dmub_diagnostic_data - Print DMUB diagnostic data for debugging
5854  *
5855  * @dc: [in] dc structure
5856  *
5857  *
5858  */
dc_print_dmub_diagnostic_data(const struct dc * dc)5859 void dc_print_dmub_diagnostic_data(const struct dc *dc)
5860 {
5861 	dc_dmub_srv_log_diagnostic_data(dc->ctx->dmub_srv);
5862 }
5863 
5864 /**
5865  * dc_disable_accelerated_mode - disable accelerated mode
5866  * @dc: dc structure
5867  */
dc_disable_accelerated_mode(struct dc * dc)5868 void dc_disable_accelerated_mode(struct dc *dc)
5869 {
5870 	bios_set_scratch_acc_mode_change(dc->ctx->dc_bios, 0);
5871 }
5872 
5873 
5874 /**
5875  *  dc_notify_vsync_int_state - notifies vsync enable/disable state
5876  *  @dc: dc structure
5877  *  @stream: stream where vsync int state changed
5878  *  @enable: whether vsync is enabled or disabled
5879  *
5880  *  Called when vsync is enabled/disabled Will notify DMUB to start/stop ABM
5881  *  interrupts after steady state is reached.
5882  */
dc_notify_vsync_int_state(struct dc * dc,struct dc_stream_state * stream,bool enable)5883 void dc_notify_vsync_int_state(struct dc *dc, struct dc_stream_state *stream, bool enable)
5884 {
5885 	int i;
5886 	int edp_num;
5887 	struct pipe_ctx *pipe = NULL;
5888 	struct dc_link *link = stream->sink->link;
5889 	struct dc_link *edp_links[MAX_NUM_EDP];
5890 
5891 
5892 	if (link->psr_settings.psr_feature_enabled)
5893 		return;
5894 
5895 	if (link->replay_settings.replay_feature_enabled)
5896 		return;
5897 
5898 	/*find primary pipe associated with stream*/
5899 	for (i = 0; i < MAX_PIPES; i++) {
5900 		pipe = &dc->current_state->res_ctx.pipe_ctx[i];
5901 
5902 		if (pipe->stream == stream && pipe->stream_res.tg)
5903 			break;
5904 	}
5905 
5906 	if (i == MAX_PIPES) {
5907 		ASSERT(0);
5908 		return;
5909 	}
5910 
5911 	dc_get_edp_links(dc, edp_links, &edp_num);
5912 
5913 	/* Determine panel inst */
5914 	for (i = 0; i < edp_num; i++) {
5915 		if (edp_links[i] == link)
5916 			break;
5917 	}
5918 
5919 	if (i == edp_num) {
5920 		return;
5921 	}
5922 
5923 	if (pipe->stream_res.abm && pipe->stream_res.abm->funcs->set_abm_pause)
5924 		pipe->stream_res.abm->funcs->set_abm_pause(pipe->stream_res.abm, !enable, i, pipe->stream_res.tg->inst);
5925 }
5926 
5927 /*****************************************************************************
5928  *  dc_abm_save_restore() - Interface to DC for save+pause and restore+un-pause
5929  *                          ABM
5930  *  @dc: dc structure
5931  *	@stream: stream where vsync int state changed
5932  *  @pData: abm hw states
5933  *
5934  ****************************************************************************/
dc_abm_save_restore(struct dc * dc,struct dc_stream_state * stream,struct abm_save_restore * pData)5935 bool dc_abm_save_restore(
5936 		struct dc *dc,
5937 		struct dc_stream_state *stream,
5938 		struct abm_save_restore *pData)
5939 {
5940 	int i;
5941 	int edp_num;
5942 	struct pipe_ctx *pipe = NULL;
5943 	struct dc_link *link = stream->sink->link;
5944 	struct dc_link *edp_links[MAX_NUM_EDP];
5945 
5946 	if (link->replay_settings.replay_feature_enabled)
5947 		return false;
5948 
5949 	/*find primary pipe associated with stream*/
5950 	for (i = 0; i < MAX_PIPES; i++) {
5951 		pipe = &dc->current_state->res_ctx.pipe_ctx[i];
5952 
5953 		if (pipe->stream == stream && pipe->stream_res.tg)
5954 			break;
5955 	}
5956 
5957 	if (i == MAX_PIPES) {
5958 		ASSERT(0);
5959 		return false;
5960 	}
5961 
5962 	dc_get_edp_links(dc, edp_links, &edp_num);
5963 
5964 	/* Determine panel inst */
5965 	for (i = 0; i < edp_num; i++)
5966 		if (edp_links[i] == link)
5967 			break;
5968 
5969 	if (i == edp_num)
5970 		return false;
5971 
5972 	if (pipe->stream_res.abm &&
5973 		pipe->stream_res.abm->funcs->save_restore)
5974 		return pipe->stream_res.abm->funcs->save_restore(
5975 				pipe->stream_res.abm,
5976 				i,
5977 				pData);
5978 	return false;
5979 }
5980 
dc_query_current_properties(struct dc * dc,struct dc_current_properties * properties)5981 void dc_query_current_properties(struct dc *dc, struct dc_current_properties *properties)
5982 {
5983 	unsigned int i;
5984 	bool subvp_sw_cursor_req = false;
5985 
5986 	for (i = 0; i < dc->current_state->stream_count; i++) {
5987 		if (check_subvp_sw_cursor_fallback_req(dc, dc->current_state->streams[i])) {
5988 			subvp_sw_cursor_req = true;
5989 			break;
5990 		}
5991 	}
5992 	properties->cursor_size_limit = subvp_sw_cursor_req ? 64 : dc->caps.max_cursor_size;
5993 }
5994 
5995 /**
5996  * dc_set_edp_power() - DM controls eDP power to be ON/OFF
5997  *
5998  * Called when DM wants to power on/off eDP.
5999  *     Only work on links with flag skip_implict_edp_power_control is set.
6000  *
6001  * @dc: Current DC state
6002  * @edp_link: a link with eDP connector signal type
6003  * @powerOn: power on/off eDP
6004  *
6005  * Return: void
6006  */
dc_set_edp_power(const struct dc * dc,struct dc_link * edp_link,bool powerOn)6007 void dc_set_edp_power(const struct dc *dc, struct dc_link *edp_link,
6008 				 bool powerOn)
6009 {
6010 	if (edp_link->connector_signal != SIGNAL_TYPE_EDP)
6011 		return;
6012 
6013 	if (edp_link->skip_implict_edp_power_control == false)
6014 		return;
6015 
6016 	edp_link->dc->link_srv->edp_set_panel_power(edp_link, powerOn);
6017 }
6018 
6019 /*
6020  *****************************************************************************
6021  * dc_get_power_profile_for_dc_state() - extracts power profile from dc state
6022  *
6023  * Called when DM wants to make power policy decisions based on dc_state
6024  *
6025  *****************************************************************************
6026  */
dc_get_power_profile_for_dc_state(const struct dc_state * context)6027 struct dc_power_profile dc_get_power_profile_for_dc_state(const struct dc_state *context)
6028 {
6029 	struct dc_power_profile profile = { 0 };
6030 
6031 	profile.power_level += !context->bw_ctx.bw.dcn.clk.p_state_change_support;
6032 
6033 	return profile;
6034 }
6035 
6036 /*
6037  **********************************************************************************
6038  * dc_get_det_buffer_size_from_state() - extracts detile buffer size from dc state
6039  *
6040  * Called when DM wants to log detile buffer size from dc_state
6041  *
6042  **********************************************************************************
6043  */
dc_get_det_buffer_size_from_state(const struct dc_state * context)6044 unsigned int dc_get_det_buffer_size_from_state(const struct dc_state *context)
6045 {
6046 	struct dc *dc = context->clk_mgr->ctx->dc;
6047 
6048 	if (dc->res_pool->funcs->get_det_buffer_size)
6049 		return dc->res_pool->funcs->get_det_buffer_size(context);
6050 	else
6051 		return 0;
6052 }
6053