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