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