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