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