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