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