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 		/* refcount will always be valid, restore everything else */
3070 		struct kref refcount = status->plane_states[i]->refcount;
3071 		*status->plane_states[i] = scratch->plane_states[i];
3072 		status->plane_states[i]->refcount = refcount;
3073 	}
3074 	*stream = scratch->stream_state;
3075 }
3076 
3077 /**
3078  * update_seamless_boot_flags() - Helper function for updating seamless boot flags
3079  *
3080  * @dc: Current DC state
3081  * @context: New DC state to be programmed
3082  * @surface_count: Number of surfaces that have an updated
3083  * @stream: Corresponding stream to be updated in the current flip
3084  *
3085  * Updating seamless boot flags do not need to be part of the commit sequence. This
3086  * helper function will update the seamless boot flags on each flip (if required)
3087  * outside of the HW commit sequence (fast or slow).
3088  *
3089  * Return: void
3090  */
update_seamless_boot_flags(struct dc * dc,struct dc_state * context,int surface_count,struct dc_stream_state * stream)3091 static void update_seamless_boot_flags(struct dc *dc,
3092 		struct dc_state *context,
3093 		int surface_count,
3094 		struct dc_stream_state *stream)
3095 {
3096 	if (get_seamless_boot_stream_count(context) > 0 && surface_count > 0) {
3097 		/* Optimize seamless boot flag keeps clocks and watermarks high until
3098 		 * first flip. After first flip, optimization is required to lower
3099 		 * bandwidth. Important to note that it is expected UEFI will
3100 		 * only light up a single display on POST, therefore we only expect
3101 		 * one stream with seamless boot flag set.
3102 		 */
3103 		if (stream->apply_seamless_boot_optimization) {
3104 			stream->apply_seamless_boot_optimization = false;
3105 
3106 			if (get_seamless_boot_stream_count(context) == 0)
3107 				dc->optimized_required = true;
3108 		}
3109 	}
3110 }
3111 
3112 /**
3113  * update_planes_and_stream_state() - The function takes planes and stream
3114  * updates as inputs and determines the appropriate update type. If update type
3115  * is FULL, the function allocates a new context, populates and validates it.
3116  * Otherwise, it updates current dc context. The function will return both
3117  * new_context and new_update_type back to the caller. The function also backs
3118  * up both current and new contexts into corresponding dc state scratch memory.
3119  * TODO: The function does too many things, and even conditionally allocates dc
3120  * context memory implicitly. We should consider to break it down.
3121  *
3122  * @dc: Current DC state
3123  * @srf_updates: an array of surface updates
3124  * @surface_count: surface update count
3125  * @stream: Corresponding stream to be updated
3126  * @stream_update: stream update
3127  * @new_update_type: [out] determined update type by the function
3128  * @new_context: [out] new context allocated and validated if update type is
3129  * FULL, reference to current context if update type is less than FULL.
3130  *
3131  * Return: true if a valid update is populated into new_context, false
3132  * otherwise.
3133  */
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)3134 static bool update_planes_and_stream_state(struct dc *dc,
3135 		struct dc_surface_update *srf_updates, int surface_count,
3136 		struct dc_stream_state *stream,
3137 		struct dc_stream_update *stream_update,
3138 		enum surface_update_type *new_update_type,
3139 		struct dc_state **new_context)
3140 {
3141 	struct dc_state *context;
3142 	int i, j;
3143 	enum surface_update_type update_type;
3144 	const struct dc_stream_status *stream_status;
3145 	struct dc_context *dc_ctx = dc->ctx;
3146 
3147 	stream_status = dc_stream_get_status(stream);
3148 
3149 	if (!stream_status) {
3150 		if (surface_count) /* Only an error condition if surf_count non-zero*/
3151 			ASSERT(false);
3152 
3153 		return false; /* Cannot commit surface to stream that is not committed */
3154 	}
3155 
3156 	context = dc->current_state;
3157 	update_type = dc_check_update_surfaces_for_stream(
3158 			dc, srf_updates, surface_count, stream_update, stream_status);
3159 	if (update_type == UPDATE_TYPE_FULL)
3160 		backup_planes_and_stream_state(&dc->scratch.current_state, stream);
3161 
3162 	/* update current stream with the new updates */
3163 	copy_stream_update_to_stream(dc, context, stream, stream_update);
3164 
3165 	/* do not perform surface update if surface has invalid dimensions
3166 	 * (all zero) and no scaling_info is provided
3167 	 */
3168 	if (surface_count > 0) {
3169 		for (i = 0; i < surface_count; i++) {
3170 			if ((srf_updates[i].surface->src_rect.width == 0 ||
3171 				 srf_updates[i].surface->src_rect.height == 0 ||
3172 				 srf_updates[i].surface->dst_rect.width == 0 ||
3173 				 srf_updates[i].surface->dst_rect.height == 0) &&
3174 				(!srf_updates[i].scaling_info ||
3175 				  srf_updates[i].scaling_info->src_rect.width == 0 ||
3176 				  srf_updates[i].scaling_info->src_rect.height == 0 ||
3177 				  srf_updates[i].scaling_info->dst_rect.width == 0 ||
3178 				  srf_updates[i].scaling_info->dst_rect.height == 0)) {
3179 				DC_ERROR("Invalid src/dst rects in surface update!\n");
3180 				return false;
3181 			}
3182 		}
3183 	}
3184 
3185 	if (update_type >= update_surface_trace_level)
3186 		update_surface_trace(dc, srf_updates, surface_count);
3187 
3188 	for (i = 0; i < surface_count; i++)
3189 		copy_surface_update_to_plane(srf_updates[i].surface, &srf_updates[i]);
3190 
3191 	if (update_type >= UPDATE_TYPE_FULL) {
3192 		struct dc_plane_state *new_planes[MAX_SURFACES] = {0};
3193 
3194 		for (i = 0; i < surface_count; i++)
3195 			new_planes[i] = srf_updates[i].surface;
3196 
3197 		/* initialize scratch memory for building context */
3198 		context = dc_state_create_copy(dc->current_state);
3199 		if (context == NULL) {
3200 			DC_ERROR("Failed to allocate new validate context!\n");
3201 			return false;
3202 		}
3203 
3204 		/* For each full update, remove all existing phantom pipes first.
3205 		 * Ensures that we have enough pipes for newly added MPO planes
3206 		 */
3207 		dc_state_remove_phantom_streams_and_planes(dc, context);
3208 		dc_state_release_phantom_streams_and_planes(dc, context);
3209 
3210 		/*remove old surfaces from context */
3211 		if (!dc_state_rem_all_planes_for_stream(dc, stream, context)) {
3212 
3213 			BREAK_TO_DEBUGGER();
3214 			goto fail;
3215 		}
3216 
3217 		/* add surface to context */
3218 		if (!dc_state_add_all_planes_for_stream(dc, stream, new_planes, surface_count, context)) {
3219 
3220 			BREAK_TO_DEBUGGER();
3221 			goto fail;
3222 		}
3223 	}
3224 
3225 	/* save update parameters into surface */
3226 	for (i = 0; i < surface_count; i++) {
3227 		struct dc_plane_state *surface = srf_updates[i].surface;
3228 
3229 		if (update_type != UPDATE_TYPE_MED)
3230 			continue;
3231 		if (surface->update_flags.bits.clip_size_change ||
3232 				surface->update_flags.bits.position_change) {
3233 			for (j = 0; j < dc->res_pool->pipe_count; j++) {
3234 				struct pipe_ctx *pipe_ctx = &context->res_ctx.pipe_ctx[j];
3235 
3236 				if (pipe_ctx->plane_state != surface)
3237 					continue;
3238 
3239 				resource_build_scaling_params(pipe_ctx);
3240 			}
3241 		}
3242 	}
3243 
3244 	if (update_type == UPDATE_TYPE_FULL) {
3245 		if (!dc->res_pool->funcs->validate_bandwidth(dc, context, false)) {
3246 			BREAK_TO_DEBUGGER();
3247 			goto fail;
3248 		}
3249 	}
3250 	update_seamless_boot_flags(dc, context, surface_count, stream);
3251 
3252 	*new_context = context;
3253 	*new_update_type = update_type;
3254 	if (update_type == UPDATE_TYPE_FULL)
3255 		backup_planes_and_stream_state(&dc->scratch.new_state, stream);
3256 
3257 	return true;
3258 
3259 fail:
3260 	dc_state_release(context);
3261 
3262 	return false;
3263 
3264 }
3265 
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)3266 static void commit_planes_do_stream_update(struct dc *dc,
3267 		struct dc_stream_state *stream,
3268 		struct dc_stream_update *stream_update,
3269 		enum surface_update_type update_type,
3270 		struct dc_state *context)
3271 {
3272 	int j;
3273 
3274 	// Stream updates
3275 	for (j = 0; j < dc->res_pool->pipe_count; j++) {
3276 		struct pipe_ctx *pipe_ctx = &context->res_ctx.pipe_ctx[j];
3277 
3278 		if (resource_is_pipe_type(pipe_ctx, OTG_MASTER) && pipe_ctx->stream == stream) {
3279 
3280 			if (stream_update->periodic_interrupt && dc->hwss.setup_periodic_interrupt)
3281 				dc->hwss.setup_periodic_interrupt(dc, pipe_ctx);
3282 
3283 			if ((stream_update->hdr_static_metadata && !stream->use_dynamic_meta) ||
3284 					stream_update->vrr_infopacket ||
3285 					stream_update->vsc_infopacket ||
3286 					stream_update->vsp_infopacket ||
3287 					stream_update->hfvsif_infopacket ||
3288 					stream_update->adaptive_sync_infopacket ||
3289 					stream_update->vtem_infopacket) {
3290 				resource_build_info_frame(pipe_ctx);
3291 				dc->hwss.update_info_frame(pipe_ctx);
3292 
3293 				if (dc_is_dp_signal(pipe_ctx->stream->signal))
3294 					dc->link_srv->dp_trace_source_sequence(
3295 							pipe_ctx->stream->link,
3296 							DPCD_SOURCE_SEQ_AFTER_UPDATE_INFO_FRAME);
3297 			}
3298 
3299 			if (stream_update->hdr_static_metadata &&
3300 					stream->use_dynamic_meta &&
3301 					dc->hwss.set_dmdata_attributes &&
3302 					pipe_ctx->stream->dmdata_address.quad_part != 0)
3303 				dc->hwss.set_dmdata_attributes(pipe_ctx);
3304 
3305 			if (stream_update->gamut_remap)
3306 				dc_stream_set_gamut_remap(dc, stream);
3307 
3308 			if (stream_update->output_csc_transform)
3309 				dc_stream_program_csc_matrix(dc, stream);
3310 
3311 			if (stream_update->dither_option) {
3312 				struct pipe_ctx *odm_pipe = pipe_ctx->next_odm_pipe;
3313 				resource_build_bit_depth_reduction_params(pipe_ctx->stream,
3314 									&pipe_ctx->stream->bit_depth_params);
3315 				pipe_ctx->stream_res.opp->funcs->opp_program_fmt(pipe_ctx->stream_res.opp,
3316 						&stream->bit_depth_params,
3317 						&stream->clamping);
3318 				while (odm_pipe) {
3319 					odm_pipe->stream_res.opp->funcs->opp_program_fmt(odm_pipe->stream_res.opp,
3320 							&stream->bit_depth_params,
3321 							&stream->clamping);
3322 					odm_pipe = odm_pipe->next_odm_pipe;
3323 				}
3324 			}
3325 
3326 			if (stream_update->cursor_attributes)
3327 				program_cursor_attributes(dc, stream);
3328 
3329 			if (stream_update->cursor_position)
3330 				program_cursor_position(dc, stream);
3331 
3332 			/* Full fe update*/
3333 			if (update_type == UPDATE_TYPE_FAST)
3334 				continue;
3335 
3336 			if (stream_update->dsc_config)
3337 				dc->link_srv->update_dsc_config(pipe_ctx);
3338 
3339 			if (stream_update->mst_bw_update) {
3340 				if (stream_update->mst_bw_update->is_increase)
3341 					dc->link_srv->increase_mst_payload(pipe_ctx,
3342 							stream_update->mst_bw_update->mst_stream_bw);
3343 				else
3344 					dc->link_srv->reduce_mst_payload(pipe_ctx,
3345 							stream_update->mst_bw_update->mst_stream_bw);
3346 			}
3347 
3348 			if (stream_update->pending_test_pattern) {
3349 				/*
3350 				 * test pattern params depends on ODM topology
3351 				 * changes that we could be applying to front
3352 				 * end. Since at the current stage front end
3353 				 * changes are not yet applied. We can only
3354 				 * apply test pattern in hw based on current
3355 				 * state and populate the final test pattern
3356 				 * params in new state. If current and new test
3357 				 * pattern params are different as result of
3358 				 * different ODM topology being used, it will be
3359 				 * detected and handle during front end
3360 				 * programming update.
3361 				 */
3362 				dc->link_srv->dp_set_test_pattern(stream->link,
3363 					stream->test_pattern.type,
3364 					stream->test_pattern.color_space,
3365 					stream->test_pattern.p_link_settings,
3366 					stream->test_pattern.p_custom_pattern,
3367 					stream->test_pattern.cust_pattern_size);
3368 				resource_build_test_pattern_params(&context->res_ctx, pipe_ctx);
3369 			}
3370 
3371 			if (stream_update->dpms_off) {
3372 				if (*stream_update->dpms_off) {
3373 					dc->link_srv->set_dpms_off(pipe_ctx);
3374 					/* for dpms, keep acquired resources*/
3375 					if (pipe_ctx->stream_res.audio && !dc->debug.az_endpoint_mute_only)
3376 						pipe_ctx->stream_res.audio->funcs->az_disable(pipe_ctx->stream_res.audio);
3377 
3378 					dc->optimized_required = true;
3379 
3380 				} else {
3381 					if (get_seamless_boot_stream_count(context) == 0)
3382 						dc->hwss.prepare_bandwidth(dc, dc->current_state);
3383 					dc->link_srv->set_dpms_on(dc->current_state, pipe_ctx);
3384 				}
3385 			} else if (pipe_ctx->stream->link->wa_flags.blank_stream_on_ocs_change && stream_update->output_color_space
3386 					&& !stream->dpms_off && dc_is_dp_signal(pipe_ctx->stream->signal)) {
3387 				/*
3388 				 * Workaround for firmware issue in some receivers where they don't pick up
3389 				 * correct output color space unless DP link is disabled/re-enabled
3390 				 */
3391 				dc->link_srv->set_dpms_on(dc->current_state, pipe_ctx);
3392 			}
3393 
3394 			if (stream_update->abm_level && pipe_ctx->stream_res.abm) {
3395 				bool should_program_abm = true;
3396 
3397 				// if otg funcs defined check if blanked before programming
3398 				if (pipe_ctx->stream_res.tg->funcs->is_blanked)
3399 					if (pipe_ctx->stream_res.tg->funcs->is_blanked(pipe_ctx->stream_res.tg))
3400 						should_program_abm = false;
3401 
3402 				if (should_program_abm) {
3403 					if (*stream_update->abm_level == ABM_LEVEL_IMMEDIATE_DISABLE) {
3404 						dc->hwss.set_abm_immediate_disable(pipe_ctx);
3405 					} else {
3406 						pipe_ctx->stream_res.abm->funcs->set_abm_level(
3407 							pipe_ctx->stream_res.abm, stream->abm_level);
3408 					}
3409 				}
3410 			}
3411 		}
3412 	}
3413 }
3414 
dc_dmub_should_send_dirty_rect_cmd(struct dc * dc,struct dc_stream_state * stream)3415 static bool dc_dmub_should_send_dirty_rect_cmd(struct dc *dc, struct dc_stream_state *stream)
3416 {
3417 	if ((stream->link->psr_settings.psr_version == DC_PSR_VERSION_SU_1
3418 			|| stream->link->psr_settings.psr_version == DC_PSR_VERSION_1)
3419 			&& stream->ctx->dce_version >= DCN_VERSION_3_1)
3420 		return true;
3421 
3422 	if (stream->link->replay_settings.config.replay_supported)
3423 		return true;
3424 
3425 	if (stream->ctx->dce_version >= DCN_VERSION_3_5 && stream->abm_level)
3426 		return true;
3427 
3428 	return false;
3429 }
3430 
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)3431 void dc_dmub_update_dirty_rect(struct dc *dc,
3432 			       int surface_count,
3433 			       struct dc_stream_state *stream,
3434 			       struct dc_surface_update *srf_updates,
3435 			       struct dc_state *context)
3436 {
3437 	union dmub_rb_cmd cmd;
3438 	struct dmub_cmd_update_dirty_rect_data *update_dirty_rect;
3439 	unsigned int i, j;
3440 	unsigned int panel_inst = 0;
3441 
3442 	if (!dc_dmub_should_send_dirty_rect_cmd(dc, stream))
3443 		return;
3444 
3445 	if (!dc_get_edp_link_panel_inst(dc, stream->link, &panel_inst))
3446 		return;
3447 
3448 	memset(&cmd, 0x0, sizeof(cmd));
3449 	cmd.update_dirty_rect.header.type = DMUB_CMD__UPDATE_DIRTY_RECT;
3450 	cmd.update_dirty_rect.header.sub_type = 0;
3451 	cmd.update_dirty_rect.header.payload_bytes =
3452 		sizeof(cmd.update_dirty_rect) -
3453 		sizeof(cmd.update_dirty_rect.header);
3454 	update_dirty_rect = &cmd.update_dirty_rect.update_dirty_rect_data;
3455 	for (i = 0; i < surface_count; i++) {
3456 		struct dc_plane_state *plane_state = srf_updates[i].surface;
3457 		const struct dc_flip_addrs *flip_addr = srf_updates[i].flip_addr;
3458 
3459 		if (!srf_updates[i].surface || !flip_addr)
3460 			continue;
3461 		/* Do not send in immediate flip mode */
3462 		if (srf_updates[i].surface->flip_immediate)
3463 			continue;
3464 
3465 		update_dirty_rect->cmd_version = DMUB_CMD_PSR_CONTROL_VERSION_1;
3466 		update_dirty_rect->dirty_rect_count = flip_addr->dirty_rect_count;
3467 		memcpy(update_dirty_rect->src_dirty_rects, flip_addr->dirty_rects,
3468 				sizeof(flip_addr->dirty_rects));
3469 		for (j = 0; j < dc->res_pool->pipe_count; j++) {
3470 			struct pipe_ctx *pipe_ctx = &context->res_ctx.pipe_ctx[j];
3471 
3472 			if (pipe_ctx->stream != stream)
3473 				continue;
3474 			if (pipe_ctx->plane_state != plane_state)
3475 				continue;
3476 
3477 			update_dirty_rect->panel_inst = panel_inst;
3478 			update_dirty_rect->pipe_idx = j;
3479 			dc_wake_and_execute_dmub_cmd(dc->ctx, &cmd, DM_DMUB_WAIT_TYPE_NO_WAIT);
3480 		}
3481 	}
3482 }
3483 
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)3484 static void build_dmub_update_dirty_rect(
3485 		struct dc *dc,
3486 		int surface_count,
3487 		struct dc_stream_state *stream,
3488 		struct dc_surface_update *srf_updates,
3489 		struct dc_state *context,
3490 		struct dc_dmub_cmd dc_dmub_cmd[],
3491 		unsigned int *dmub_cmd_count)
3492 {
3493 	union dmub_rb_cmd cmd;
3494 	struct dmub_cmd_update_dirty_rect_data *update_dirty_rect;
3495 	unsigned int i, j;
3496 	unsigned int panel_inst = 0;
3497 
3498 	if (!dc_dmub_should_send_dirty_rect_cmd(dc, stream))
3499 		return;
3500 
3501 	if (!dc_get_edp_link_panel_inst(dc, stream->link, &panel_inst))
3502 		return;
3503 
3504 	memset(&cmd, 0x0, sizeof(cmd));
3505 	cmd.update_dirty_rect.header.type = DMUB_CMD__UPDATE_DIRTY_RECT;
3506 	cmd.update_dirty_rect.header.sub_type = 0;
3507 	cmd.update_dirty_rect.header.payload_bytes =
3508 		sizeof(cmd.update_dirty_rect) -
3509 		sizeof(cmd.update_dirty_rect.header);
3510 	update_dirty_rect = &cmd.update_dirty_rect.update_dirty_rect_data;
3511 	for (i = 0; i < surface_count; i++) {
3512 		struct dc_plane_state *plane_state = srf_updates[i].surface;
3513 		const struct dc_flip_addrs *flip_addr = srf_updates[i].flip_addr;
3514 
3515 		if (!srf_updates[i].surface || !flip_addr)
3516 			continue;
3517 		/* Do not send in immediate flip mode */
3518 		if (srf_updates[i].surface->flip_immediate)
3519 			continue;
3520 		update_dirty_rect->cmd_version = DMUB_CMD_PSR_CONTROL_VERSION_1;
3521 		update_dirty_rect->dirty_rect_count = flip_addr->dirty_rect_count;
3522 		memcpy(update_dirty_rect->src_dirty_rects, flip_addr->dirty_rects,
3523 				sizeof(flip_addr->dirty_rects));
3524 		for (j = 0; j < dc->res_pool->pipe_count; j++) {
3525 			struct pipe_ctx *pipe_ctx = &context->res_ctx.pipe_ctx[j];
3526 
3527 			if (pipe_ctx->stream != stream)
3528 				continue;
3529 			if (pipe_ctx->plane_state != plane_state)
3530 				continue;
3531 			update_dirty_rect->panel_inst = panel_inst;
3532 			update_dirty_rect->pipe_idx = j;
3533 			dc_dmub_cmd[*dmub_cmd_count].dmub_cmd = cmd;
3534 			dc_dmub_cmd[*dmub_cmd_count].wait_type = DM_DMUB_WAIT_TYPE_NO_WAIT;
3535 			(*dmub_cmd_count)++;
3536 		}
3537 	}
3538 }
3539 
check_address_only_update(union surface_update_flags update_flags)3540 static bool check_address_only_update(union surface_update_flags update_flags)
3541 {
3542 	union surface_update_flags addr_only_update_flags;
3543 	addr_only_update_flags.raw = 0;
3544 	addr_only_update_flags.bits.addr_update = 1;
3545 
3546 	return update_flags.bits.addr_update &&
3547 			!(update_flags.raw & ~addr_only_update_flags.raw);
3548 }
3549 
3550 /**
3551  * build_dmub_cmd_list() - Build an array of DMCUB commands to be sent to DMCUB
3552  *
3553  * @dc: Current DC state
3554  * @srf_updates: Array of surface updates
3555  * @surface_count: Number of surfaces that have an updated
3556  * @stream: Corresponding stream to be updated in the current flip
3557  * @context: New DC state to be programmed
3558  *
3559  * @dc_dmub_cmd: Array of DMCUB commands to be sent to DMCUB
3560  * @dmub_cmd_count: Count indicating the number of DMCUB commands in dc_dmub_cmd array
3561  *
3562  * This function builds an array of DMCUB commands to be sent to DMCUB. This function is required
3563  * to build an array of commands and have them sent while the OTG lock is acquired.
3564  *
3565  * Return: void
3566  */
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)3567 static void build_dmub_cmd_list(struct dc *dc,
3568 		struct dc_surface_update *srf_updates,
3569 		int surface_count,
3570 		struct dc_stream_state *stream,
3571 		struct dc_state *context,
3572 		struct dc_dmub_cmd dc_dmub_cmd[],
3573 		unsigned int *dmub_cmd_count)
3574 {
3575 	// Initialize cmd count to 0
3576 	*dmub_cmd_count = 0;
3577 	build_dmub_update_dirty_rect(dc, surface_count, stream, srf_updates, context, dc_dmub_cmd, dmub_cmd_count);
3578 }
3579 
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)3580 static void commit_plane_for_stream_offload_fams2_flip(struct dc *dc,
3581 		struct dc_surface_update *srf_updates,
3582 		int surface_count,
3583 		struct dc_stream_state *stream,
3584 		struct dc_state *context)
3585 {
3586 	int i, j;
3587 
3588 	/* update dirty rect for PSR */
3589 	dc_dmub_update_dirty_rect(dc, surface_count, stream,
3590 			srf_updates, context);
3591 
3592 	/* Perform requested Updates */
3593 	for (i = 0; i < surface_count; i++) {
3594 		struct dc_plane_state *plane_state = srf_updates[i].surface;
3595 
3596 		for (j = 0; j < dc->res_pool->pipe_count; j++) {
3597 			struct pipe_ctx *pipe_ctx = &context->res_ctx.pipe_ctx[j];
3598 
3599 			if (!should_update_pipe_for_stream(context, pipe_ctx, stream))
3600 				continue;
3601 
3602 			if (!should_update_pipe_for_plane(context, pipe_ctx, plane_state))
3603 				continue;
3604 
3605 			/* update pipe context for plane */
3606 			if (pipe_ctx->plane_state->update_flags.bits.addr_update)
3607 				dc->hwss.update_plane_addr(dc, pipe_ctx);
3608 		}
3609 	}
3610 
3611 	/* Send commands to DMCUB */
3612 	dc_dmub_srv_fams2_passthrough_flip(dc,
3613 				context,
3614 				stream,
3615 				srf_updates,
3616 				surface_count);
3617 }
3618 
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)3619 static void commit_planes_for_stream_fast(struct dc *dc,
3620 		struct dc_surface_update *srf_updates,
3621 		int surface_count,
3622 		struct dc_stream_state *stream,
3623 		struct dc_stream_update *stream_update,
3624 		enum surface_update_type update_type,
3625 		struct dc_state *context)
3626 {
3627 	int i, j;
3628 	struct pipe_ctx *top_pipe_to_program = NULL;
3629 	struct dc_stream_status *stream_status = NULL;
3630 	bool should_offload_fams2_flip = false;
3631 
3632 	if (dc->debug.fams2_config.bits.enable &&
3633 			dc->debug.fams2_config.bits.enable_offload_flip &&
3634 			dc_state_is_fams2_in_use(dc, context)) {
3635 		/* if not offloading to HWFQ, offload to FAMS2 if needed */
3636 		should_offload_fams2_flip = true;
3637 		for (i = 0; i < surface_count; i++) {
3638 			if (srf_updates[i].surface &&
3639 					srf_updates[i].surface->update_flags.raw &&
3640 					!check_address_only_update(srf_updates[i].surface->update_flags)) {
3641 				/* more than address update, need to acquire FAMS2 lock */
3642 				should_offload_fams2_flip = false;
3643 				break;
3644 			}
3645 		}
3646 		if (stream_update) {
3647 			/* more than address update, need to acquire FAMS2 lock */
3648 			should_offload_fams2_flip = false;
3649 		}
3650 	}
3651 
3652 	dc_exit_ips_for_hw_access(dc);
3653 
3654 	dc_z10_restore(dc);
3655 
3656 	top_pipe_to_program = resource_get_otg_master_for_stream(
3657 			&context->res_ctx,
3658 			stream);
3659 
3660 	if (!top_pipe_to_program)
3661 		return;
3662 
3663 	for (i = 0; i < dc->res_pool->pipe_count; i++) {
3664 		struct pipe_ctx *pipe = &context->res_ctx.pipe_ctx[i];
3665 
3666 		if (pipe->stream && pipe->plane_state) {
3667 			if (!dc->debug.using_dml2)
3668 				set_p_state_switch_method(dc, context, pipe);
3669 
3670 			if (dc->debug.visual_confirm)
3671 				dc_update_visual_confirm_color(dc, context, pipe);
3672 		}
3673 	}
3674 
3675 	for (i = 0; i < surface_count; i++) {
3676 		struct dc_plane_state *plane_state = srf_updates[i].surface;
3677 		/*set logical flag for lock/unlock use*/
3678 		for (j = 0; j < dc->res_pool->pipe_count; j++) {
3679 			struct pipe_ctx *pipe_ctx = &context->res_ctx.pipe_ctx[j];
3680 
3681 			if (!pipe_ctx->plane_state)
3682 				continue;
3683 			if (should_update_pipe_for_plane(context, pipe_ctx, plane_state))
3684 				continue;
3685 			pipe_ctx->plane_state->triplebuffer_flips = false;
3686 			if (update_type == UPDATE_TYPE_FAST &&
3687 			    dc->hwss.program_triplebuffer != NULL &&
3688 			    !pipe_ctx->plane_state->flip_immediate && dc->debug.enable_tri_buf) {
3689 				/*triple buffer for VUpdate  only*/
3690 				pipe_ctx->plane_state->triplebuffer_flips = true;
3691 			}
3692 		}
3693 	}
3694 
3695 	stream_status = dc_state_get_stream_status(context, stream);
3696 
3697 	if (should_offload_fams2_flip) {
3698 		commit_plane_for_stream_offload_fams2_flip(dc,
3699 				srf_updates,
3700 				surface_count,
3701 				stream,
3702 				context);
3703 	} else if (stream_status) {
3704 		build_dmub_cmd_list(dc,
3705 				srf_updates,
3706 				surface_count,
3707 				stream,
3708 				context,
3709 				context->dc_dmub_cmd,
3710 				&(context->dmub_cmd_count));
3711 		hwss_build_fast_sequence(dc,
3712 				context->dc_dmub_cmd,
3713 				context->dmub_cmd_count,
3714 				context->block_sequence,
3715 				&(context->block_sequence_steps),
3716 				top_pipe_to_program,
3717 				stream_status,
3718 				context);
3719 		hwss_execute_sequence(dc,
3720 				context->block_sequence,
3721 				context->block_sequence_steps);
3722 	}
3723 
3724 	/* Clear update flags so next flip doesn't have redundant programming
3725 	 * (if there's no stream update, the update flags are not cleared).
3726 	 * Surface updates are cleared unconditionally at the beginning of each flip,
3727 	 * so no need to clear here.
3728 	 */
3729 	if (top_pipe_to_program->stream)
3730 		top_pipe_to_program->stream->update_flags.raw = 0;
3731 }
3732 
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)3733 static void commit_planes_for_stream(struct dc *dc,
3734 		struct dc_surface_update *srf_updates,
3735 		int surface_count,
3736 		struct dc_stream_state *stream,
3737 		struct dc_stream_update *stream_update,
3738 		enum surface_update_type update_type,
3739 		struct dc_state *context)
3740 {
3741 	int i, j;
3742 	struct pipe_ctx *top_pipe_to_program = NULL;
3743 	bool should_lock_all_pipes = (update_type != UPDATE_TYPE_FAST);
3744 	bool subvp_prev_use = false;
3745 	bool subvp_curr_use = false;
3746 	uint8_t current_stream_mask = 0;
3747 
3748 	// Once we apply the new subvp context to hardware it won't be in the
3749 	// dc->current_state anymore, so we have to cache it before we apply
3750 	// the new SubVP context
3751 	subvp_prev_use = false;
3752 	dc_exit_ips_for_hw_access(dc);
3753 
3754 	dc_z10_restore(dc);
3755 	if (update_type == UPDATE_TYPE_FULL)
3756 		hwss_process_outstanding_hw_updates(dc, dc->current_state);
3757 
3758 	for (i = 0; i < dc->res_pool->pipe_count; i++) {
3759 		struct pipe_ctx *pipe = &context->res_ctx.pipe_ctx[i];
3760 
3761 		if (pipe->stream && pipe->plane_state) {
3762 			if (!dc->debug.using_dml2)
3763 				set_p_state_switch_method(dc, context, pipe);
3764 
3765 			if (dc->debug.visual_confirm)
3766 				dc_update_visual_confirm_color(dc, context, pipe);
3767 		}
3768 	}
3769 
3770 	if (update_type == UPDATE_TYPE_FULL) {
3771 		dc_allow_idle_optimizations(dc, false);
3772 
3773 		if (get_seamless_boot_stream_count(context) == 0)
3774 			dc->hwss.prepare_bandwidth(dc, context);
3775 
3776 		if (dc->hwss.update_dsc_pg)
3777 			dc->hwss.update_dsc_pg(dc, context, false);
3778 
3779 		context_clock_trace(dc, context);
3780 	}
3781 
3782 	top_pipe_to_program = resource_get_otg_master_for_stream(
3783 				&context->res_ctx,
3784 				stream);
3785 	ASSERT(top_pipe_to_program != NULL);
3786 	for (i = 0; i < dc->res_pool->pipe_count; i++) {
3787 		struct pipe_ctx *old_pipe = &dc->current_state->res_ctx.pipe_ctx[i];
3788 
3789 		// Check old context for SubVP
3790 		subvp_prev_use |= (dc_state_get_pipe_subvp_type(dc->current_state, old_pipe) == SUBVP_PHANTOM);
3791 		if (subvp_prev_use)
3792 			break;
3793 	}
3794 
3795 	for (i = 0; i < dc->res_pool->pipe_count; i++) {
3796 		struct pipe_ctx *pipe = &context->res_ctx.pipe_ctx[i];
3797 
3798 		if (dc_state_get_pipe_subvp_type(context, pipe) == SUBVP_PHANTOM) {
3799 			subvp_curr_use = true;
3800 			break;
3801 		}
3802 	}
3803 
3804 	if (stream->test_pattern.type != DP_TEST_PATTERN_VIDEO_MODE) {
3805 		struct pipe_ctx *mpcc_pipe;
3806 		struct pipe_ctx *odm_pipe;
3807 
3808 		for (mpcc_pipe = top_pipe_to_program; mpcc_pipe; mpcc_pipe = mpcc_pipe->bottom_pipe)
3809 			for (odm_pipe = mpcc_pipe; odm_pipe; odm_pipe = odm_pipe->next_odm_pipe)
3810 				odm_pipe->ttu_regs.min_ttu_vblank = MAX_TTU;
3811 	}
3812 
3813 	if (update_type != UPDATE_TYPE_FAST && dc->res_pool->funcs->prepare_mcache_programming)
3814 		dc->res_pool->funcs->prepare_mcache_programming(dc, context);
3815 
3816 	if ((update_type != UPDATE_TYPE_FAST) && stream->update_flags.bits.dsc_changed)
3817 		if (top_pipe_to_program &&
3818 			top_pipe_to_program->stream_res.tg->funcs->lock_doublebuffer_enable) {
3819 			if (should_use_dmub_lock(stream->link)) {
3820 				union dmub_hw_lock_flags hw_locks = { 0 };
3821 				struct dmub_hw_lock_inst_flags inst_flags = { 0 };
3822 
3823 				hw_locks.bits.lock_dig = 1;
3824 				inst_flags.dig_inst = top_pipe_to_program->stream_res.tg->inst;
3825 
3826 				dmub_hw_lock_mgr_cmd(dc->ctx->dmub_srv,
3827 							true,
3828 							&hw_locks,
3829 							&inst_flags);
3830 			} else
3831 				top_pipe_to_program->stream_res.tg->funcs->lock_doublebuffer_enable(
3832 						top_pipe_to_program->stream_res.tg);
3833 		}
3834 
3835 	if (dc->hwss.wait_for_dcc_meta_propagation) {
3836 		dc->hwss.wait_for_dcc_meta_propagation(dc, top_pipe_to_program);
3837 	}
3838 
3839 	if (should_lock_all_pipes && dc->hwss.interdependent_update_lock) {
3840 		if (dc->hwss.subvp_pipe_control_lock)
3841 			dc->hwss.subvp_pipe_control_lock(dc, context, true, should_lock_all_pipes, NULL, subvp_prev_use);
3842 
3843 		if (dc->hwss.fams2_global_control_lock)
3844 			dc->hwss.fams2_global_control_lock(dc, context, true);
3845 
3846 		dc->hwss.interdependent_update_lock(dc, context, true);
3847 	} else {
3848 		if (dc->hwss.subvp_pipe_control_lock)
3849 			dc->hwss.subvp_pipe_control_lock(dc, context, true, should_lock_all_pipes, top_pipe_to_program, subvp_prev_use);
3850 
3851 		if (dc->hwss.fams2_global_control_lock)
3852 			dc->hwss.fams2_global_control_lock(dc, context, true);
3853 
3854 		/* Lock the top pipe while updating plane addrs, since freesync requires
3855 		 *  plane addr update event triggers to be synchronized.
3856 		 *  top_pipe_to_program is expected to never be NULL
3857 		 */
3858 		dc->hwss.pipe_control_lock(dc, top_pipe_to_program, true);
3859 	}
3860 
3861 	dc_dmub_update_dirty_rect(dc, surface_count, stream, srf_updates, context);
3862 
3863 	// Stream updates
3864 	if (stream_update)
3865 		commit_planes_do_stream_update(dc, stream, stream_update, update_type, context);
3866 
3867 	if (surface_count == 0) {
3868 		/*
3869 		 * In case of turning off screen, no need to program front end a second time.
3870 		 * just return after program blank.
3871 		 */
3872 		if (dc->hwss.apply_ctx_for_surface)
3873 			dc->hwss.apply_ctx_for_surface(dc, stream, 0, context);
3874 		if (dc->hwss.program_front_end_for_ctx)
3875 			dc->hwss.program_front_end_for_ctx(dc, context);
3876 
3877 		if (should_lock_all_pipes && dc->hwss.interdependent_update_lock) {
3878 			dc->hwss.interdependent_update_lock(dc, context, false);
3879 		} else {
3880 			dc->hwss.pipe_control_lock(dc, top_pipe_to_program, false);
3881 		}
3882 		dc->hwss.post_unlock_program_front_end(dc, context);
3883 
3884 		if (update_type != UPDATE_TYPE_FAST)
3885 			if (dc->hwss.commit_subvp_config)
3886 				dc->hwss.commit_subvp_config(dc, context);
3887 
3888 		/* Since phantom pipe programming is moved to post_unlock_program_front_end,
3889 		 * move the SubVP lock to after the phantom pipes have been setup
3890 		 */
3891 		if (dc->hwss.subvp_pipe_control_lock)
3892 			dc->hwss.subvp_pipe_control_lock(dc, context, false, should_lock_all_pipes,
3893 							 NULL, subvp_prev_use);
3894 
3895 		if (dc->hwss.fams2_global_control_lock)
3896 			dc->hwss.fams2_global_control_lock(dc, context, false);
3897 
3898 		return;
3899 	}
3900 
3901 	if (update_type != UPDATE_TYPE_FAST) {
3902 		for (j = 0; j < dc->res_pool->pipe_count; j++) {
3903 			struct pipe_ctx *pipe_ctx = &context->res_ctx.pipe_ctx[j];
3904 
3905 			if ((dc->debug.visual_confirm == VISUAL_CONFIRM_SUBVP ||
3906 				dc->debug.visual_confirm == VISUAL_CONFIRM_MCLK_SWITCH) &&
3907 				pipe_ctx->stream && pipe_ctx->plane_state) {
3908 				/* Only update visual confirm for SUBVP and Mclk switching here.
3909 				 * The bar appears on all pipes, so we need to update the bar on all displays,
3910 				 * so the information doesn't get stale.
3911 				 */
3912 				dc->hwss.update_visual_confirm_color(dc, pipe_ctx,
3913 						pipe_ctx->plane_res.hubp->inst);
3914 			}
3915 		}
3916 	}
3917 
3918 	for (i = 0; i < surface_count; i++) {
3919 		struct dc_plane_state *plane_state = srf_updates[i].surface;
3920 
3921 		/*set logical flag for lock/unlock use*/
3922 		for (j = 0; j < dc->res_pool->pipe_count; j++) {
3923 			struct pipe_ctx *pipe_ctx = &context->res_ctx.pipe_ctx[j];
3924 			if (!pipe_ctx->plane_state)
3925 				continue;
3926 			if (should_update_pipe_for_plane(context, pipe_ctx, plane_state))
3927 				continue;
3928 			pipe_ctx->plane_state->triplebuffer_flips = false;
3929 			if (update_type == UPDATE_TYPE_FAST &&
3930 				dc->hwss.program_triplebuffer != NULL &&
3931 				!pipe_ctx->plane_state->flip_immediate && dc->debug.enable_tri_buf) {
3932 					/*triple buffer for VUpdate  only*/
3933 					pipe_ctx->plane_state->triplebuffer_flips = true;
3934 			}
3935 		}
3936 		if (update_type == UPDATE_TYPE_FULL) {
3937 			/* force vsync flip when reconfiguring pipes to prevent underflow */
3938 			plane_state->flip_immediate = false;
3939 		}
3940 	}
3941 
3942 	// Update Type FULL, Surface updates
3943 	for (j = 0; j < dc->res_pool->pipe_count; j++) {
3944 		struct pipe_ctx *pipe_ctx = &context->res_ctx.pipe_ctx[j];
3945 
3946 		if (!pipe_ctx->top_pipe &&
3947 			!pipe_ctx->prev_odm_pipe &&
3948 			should_update_pipe_for_stream(context, pipe_ctx, stream)) {
3949 			struct dc_stream_status *stream_status = NULL;
3950 
3951 			if (!pipe_ctx->plane_state)
3952 				continue;
3953 
3954 			/* Full fe update*/
3955 			if (update_type == UPDATE_TYPE_FAST)
3956 				continue;
3957 
3958 			ASSERT(!pipe_ctx->plane_state->triplebuffer_flips);
3959 
3960 			if (dc->hwss.program_triplebuffer != NULL && dc->debug.enable_tri_buf) {
3961 				/*turn off triple buffer for full update*/
3962 				dc->hwss.program_triplebuffer(
3963 					dc, pipe_ctx, pipe_ctx->plane_state->triplebuffer_flips);
3964 			}
3965 			stream_status =
3966 				stream_get_status(context, pipe_ctx->stream);
3967 
3968 			if (dc->hwss.apply_ctx_for_surface && stream_status)
3969 				dc->hwss.apply_ctx_for_surface(
3970 					dc, pipe_ctx->stream, stream_status->plane_count, context);
3971 		}
3972 	}
3973 	if (dc->hwss.program_front_end_for_ctx && update_type != UPDATE_TYPE_FAST) {
3974 		dc->hwss.program_front_end_for_ctx(dc, context);
3975 		if (dc->debug.validate_dml_output) {
3976 			for (i = 0; i < dc->res_pool->pipe_count; i++) {
3977 				struct pipe_ctx *cur_pipe = &context->res_ctx.pipe_ctx[i];
3978 				if (cur_pipe->stream == NULL)
3979 					continue;
3980 
3981 				cur_pipe->plane_res.hubp->funcs->validate_dml_output(
3982 						cur_pipe->plane_res.hubp, dc->ctx,
3983 						&context->res_ctx.pipe_ctx[i].rq_regs,
3984 						&context->res_ctx.pipe_ctx[i].dlg_regs,
3985 						&context->res_ctx.pipe_ctx[i].ttu_regs);
3986 			}
3987 		}
3988 	}
3989 
3990 	// Update Type FAST, Surface updates
3991 	if (update_type == UPDATE_TYPE_FAST) {
3992 		if (dc->hwss.set_flip_control_gsl)
3993 			for (i = 0; i < surface_count; i++) {
3994 				struct dc_plane_state *plane_state = srf_updates[i].surface;
3995 
3996 				for (j = 0; j < dc->res_pool->pipe_count; j++) {
3997 					struct pipe_ctx *pipe_ctx = &context->res_ctx.pipe_ctx[j];
3998 
3999 					if (!should_update_pipe_for_stream(context, pipe_ctx, stream))
4000 						continue;
4001 
4002 					if (!should_update_pipe_for_plane(context, pipe_ctx, plane_state))
4003 						continue;
4004 
4005 					// GSL has to be used for flip immediate
4006 					dc->hwss.set_flip_control_gsl(pipe_ctx,
4007 							pipe_ctx->plane_state->flip_immediate);
4008 				}
4009 			}
4010 
4011 		/* Perform requested Updates */
4012 		for (i = 0; i < surface_count; i++) {
4013 			struct dc_plane_state *plane_state = srf_updates[i].surface;
4014 
4015 			for (j = 0; j < dc->res_pool->pipe_count; j++) {
4016 				struct pipe_ctx *pipe_ctx = &context->res_ctx.pipe_ctx[j];
4017 
4018 				if (!should_update_pipe_for_stream(context, pipe_ctx, stream))
4019 					continue;
4020 
4021 				if (!should_update_pipe_for_plane(context, pipe_ctx, plane_state))
4022 					continue;
4023 
4024 				if (srf_updates[i].cm2_params &&
4025 						srf_updates[i].cm2_params->cm2_luts.lut3d_data.lut3d_src ==
4026 								DC_CM2_TRANSFER_FUNC_SOURCE_VIDMEM &&
4027 						srf_updates[i].cm2_params->component_settings.shaper_3dlut_setting ==
4028 								DC_CM2_SHAPER_3DLUT_SETTING_ENABLE_SHAPER_3DLUT &&
4029 						dc->hwss.trigger_3dlut_dma_load)
4030 					dc->hwss.trigger_3dlut_dma_load(dc, pipe_ctx);
4031 
4032 				/*program triple buffer after lock based on flip type*/
4033 				if (dc->hwss.program_triplebuffer != NULL && dc->debug.enable_tri_buf) {
4034 					/*only enable triplebuffer for  fast_update*/
4035 					dc->hwss.program_triplebuffer(
4036 						dc, pipe_ctx, pipe_ctx->plane_state->triplebuffer_flips);
4037 				}
4038 				if (pipe_ctx->plane_state->update_flags.bits.addr_update)
4039 					dc->hwss.update_plane_addr(dc, pipe_ctx);
4040 			}
4041 		}
4042 	}
4043 
4044 	if (should_lock_all_pipes && dc->hwss.interdependent_update_lock) {
4045 		dc->hwss.interdependent_update_lock(dc, context, false);
4046 	} else {
4047 		dc->hwss.pipe_control_lock(dc, top_pipe_to_program, false);
4048 	}
4049 
4050 	if ((update_type != UPDATE_TYPE_FAST) && stream->update_flags.bits.dsc_changed)
4051 		if (top_pipe_to_program &&
4052 		    top_pipe_to_program->stream_res.tg->funcs->lock_doublebuffer_enable) {
4053 			top_pipe_to_program->stream_res.tg->funcs->wait_for_state(
4054 				top_pipe_to_program->stream_res.tg,
4055 				CRTC_STATE_VACTIVE);
4056 			top_pipe_to_program->stream_res.tg->funcs->wait_for_state(
4057 				top_pipe_to_program->stream_res.tg,
4058 				CRTC_STATE_VBLANK);
4059 			top_pipe_to_program->stream_res.tg->funcs->wait_for_state(
4060 				top_pipe_to_program->stream_res.tg,
4061 				CRTC_STATE_VACTIVE);
4062 
4063 			if (should_use_dmub_lock(stream->link)) {
4064 				union dmub_hw_lock_flags hw_locks = { 0 };
4065 				struct dmub_hw_lock_inst_flags inst_flags = { 0 };
4066 
4067 				hw_locks.bits.lock_dig = 1;
4068 				inst_flags.dig_inst = top_pipe_to_program->stream_res.tg->inst;
4069 
4070 				dmub_hw_lock_mgr_cmd(dc->ctx->dmub_srv,
4071 							false,
4072 							&hw_locks,
4073 							&inst_flags);
4074 			} else
4075 				top_pipe_to_program->stream_res.tg->funcs->lock_doublebuffer_disable(
4076 					top_pipe_to_program->stream_res.tg);
4077 		}
4078 
4079 	if (subvp_curr_use) {
4080 		/* If enabling subvp or transitioning from subvp->subvp, enable the
4081 		 * phantom streams before we program front end for the phantom pipes.
4082 		 */
4083 		if (update_type != UPDATE_TYPE_FAST) {
4084 			if (dc->hwss.enable_phantom_streams)
4085 				dc->hwss.enable_phantom_streams(dc, context);
4086 		}
4087 	}
4088 
4089 	if (update_type != UPDATE_TYPE_FAST)
4090 		dc->hwss.post_unlock_program_front_end(dc, context);
4091 
4092 	if (subvp_prev_use && !subvp_curr_use) {
4093 		/* If disabling subvp, disable phantom streams after front end
4094 		 * programming has completed (we turn on phantom OTG in order
4095 		 * to complete the plane disable for phantom pipes).
4096 		 */
4097 
4098 		if (dc->hwss.disable_phantom_streams)
4099 			dc->hwss.disable_phantom_streams(dc, context);
4100 	}
4101 
4102 	if (update_type != UPDATE_TYPE_FAST)
4103 		if (dc->hwss.commit_subvp_config)
4104 			dc->hwss.commit_subvp_config(dc, context);
4105 	/* Since phantom pipe programming is moved to post_unlock_program_front_end,
4106 	 * move the SubVP lock to after the phantom pipes have been setup
4107 	 */
4108 	if (should_lock_all_pipes && dc->hwss.interdependent_update_lock) {
4109 		if (dc->hwss.subvp_pipe_control_lock)
4110 			dc->hwss.subvp_pipe_control_lock(dc, context, false, should_lock_all_pipes, NULL, subvp_prev_use);
4111 		if (dc->hwss.fams2_global_control_lock)
4112 			dc->hwss.fams2_global_control_lock(dc, context, false);
4113 	} else {
4114 		if (dc->hwss.subvp_pipe_control_lock)
4115 			dc->hwss.subvp_pipe_control_lock(dc, context, false, should_lock_all_pipes, top_pipe_to_program, subvp_prev_use);
4116 		if (dc->hwss.fams2_global_control_lock)
4117 			dc->hwss.fams2_global_control_lock(dc, context, false);
4118 	}
4119 
4120 	// Fire manual trigger only when bottom plane is flipped
4121 	for (j = 0; j < dc->res_pool->pipe_count; j++) {
4122 		struct pipe_ctx *pipe_ctx = &context->res_ctx.pipe_ctx[j];
4123 
4124 		if (!pipe_ctx->plane_state)
4125 			continue;
4126 
4127 		if (pipe_ctx->bottom_pipe || pipe_ctx->next_odm_pipe ||
4128 				!pipe_ctx->stream || !should_update_pipe_for_stream(context, pipe_ctx, stream) ||
4129 				!pipe_ctx->plane_state->update_flags.bits.addr_update ||
4130 				pipe_ctx->plane_state->skip_manual_trigger)
4131 			continue;
4132 
4133 		if (pipe_ctx->stream_res.tg->funcs->program_manual_trigger)
4134 			pipe_ctx->stream_res.tg->funcs->program_manual_trigger(pipe_ctx->stream_res.tg);
4135 	}
4136 
4137 	current_stream_mask = get_stream_mask(dc, context);
4138 	if (current_stream_mask != context->stream_mask) {
4139 		context->stream_mask = current_stream_mask;
4140 		dc_dmub_srv_notify_stream_mask(dc->ctx->dmub_srv, current_stream_mask);
4141 	}
4142 }
4143 
4144 /**
4145  * could_mpcc_tree_change_for_active_pipes - Check if an OPP associated with MPCC might change
4146  *
4147  * @dc: Used to get the current state status
4148  * @stream: Target stream, which we want to remove the attached planes
4149  * @srf_updates: Array of surface updates
4150  * @surface_count: Number of surface update
4151  * @is_plane_addition: [in] Fill out with true if it is a plane addition case
4152  *
4153  * DCN32x and newer support a feature named Dynamic ODM which can conflict with
4154  * the MPO if used simultaneously in some specific configurations (e.g.,
4155  * 4k@144). This function checks if the incoming context requires applying a
4156  * transition state with unnecessary pipe splitting and ODM disabled to
4157  * circumvent our hardware limitations to prevent this edge case. If the OPP
4158  * associated with an MPCC might change due to plane additions, this function
4159  * returns true.
4160  *
4161  * Return:
4162  * Return true if OPP and MPCC might change, otherwise, return false.
4163  */
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)4164 static bool could_mpcc_tree_change_for_active_pipes(struct dc *dc,
4165 		struct dc_stream_state *stream,
4166 		struct dc_surface_update *srf_updates,
4167 		int surface_count,
4168 		bool *is_plane_addition)
4169 {
4170 
4171 	struct dc_stream_status *cur_stream_status = stream_get_status(dc->current_state, stream);
4172 	bool force_minimal_pipe_splitting = false;
4173 	bool subvp_active = false;
4174 	uint32_t i;
4175 
4176 	*is_plane_addition = false;
4177 
4178 	if (cur_stream_status &&
4179 			dc->current_state->stream_count > 0 &&
4180 			dc->debug.pipe_split_policy != MPC_SPLIT_AVOID) {
4181 		/* determine if minimal transition is required due to MPC*/
4182 		if (surface_count > 0) {
4183 			if (cur_stream_status->plane_count > surface_count) {
4184 				force_minimal_pipe_splitting = true;
4185 			} else if (cur_stream_status->plane_count < surface_count) {
4186 				force_minimal_pipe_splitting = true;
4187 				*is_plane_addition = true;
4188 			}
4189 		}
4190 	}
4191 
4192 	if (cur_stream_status &&
4193 			dc->current_state->stream_count == 1 &&
4194 			dc->debug.enable_single_display_2to1_odm_policy) {
4195 		/* determine if minimal transition is required due to dynamic ODM*/
4196 		if (surface_count > 0) {
4197 			if (cur_stream_status->plane_count > 2 && cur_stream_status->plane_count > surface_count) {
4198 				force_minimal_pipe_splitting = true;
4199 			} else if (surface_count > 2 && cur_stream_status->plane_count < surface_count) {
4200 				force_minimal_pipe_splitting = true;
4201 				*is_plane_addition = true;
4202 			}
4203 		}
4204 	}
4205 
4206 	for (i = 0; i < dc->res_pool->pipe_count; i++) {
4207 		struct pipe_ctx *pipe = &dc->current_state->res_ctx.pipe_ctx[i];
4208 
4209 		if (dc_state_get_pipe_subvp_type(dc->current_state, pipe) != SUBVP_NONE) {
4210 			subvp_active = true;
4211 			break;
4212 		}
4213 	}
4214 
4215 	/* For SubVP when adding or removing planes we need to add a minimal transition
4216 	 * (even when disabling all planes). Whenever disabling a phantom pipe, we
4217 	 * must use the minimal transition path to disable the pipe correctly.
4218 	 *
4219 	 * We want to use the minimal transition whenever subvp is active, not only if
4220 	 * a plane is being added / removed from a subvp stream (MPO plane can be added
4221 	 * to a DRR pipe of SubVP + DRR config, in which case we still want to run through
4222 	 * a min transition to disable subvp.
4223 	 */
4224 	if (cur_stream_status && subvp_active) {
4225 		/* determine if minimal transition is required due to SubVP*/
4226 		if (cur_stream_status->plane_count > surface_count) {
4227 			force_minimal_pipe_splitting = true;
4228 		} else if (cur_stream_status->plane_count < surface_count) {
4229 			force_minimal_pipe_splitting = true;
4230 			*is_plane_addition = true;
4231 		}
4232 	}
4233 
4234 	return force_minimal_pipe_splitting;
4235 }
4236 
4237 struct pipe_split_policy_backup {
4238 	bool dynamic_odm_policy;
4239 	bool subvp_policy;
4240 	enum pipe_split_policy mpc_policy;
4241 	char force_odm[MAX_PIPES];
4242 };
4243 
backup_and_set_minimal_pipe_split_policy(struct dc * dc,struct dc_state * context,struct pipe_split_policy_backup * policy)4244 static void backup_and_set_minimal_pipe_split_policy(struct dc *dc,
4245 		struct dc_state *context,
4246 		struct pipe_split_policy_backup *policy)
4247 {
4248 	int i;
4249 
4250 	if (!dc->config.is_vmin_only_asic) {
4251 		policy->mpc_policy = dc->debug.pipe_split_policy;
4252 		dc->debug.pipe_split_policy = MPC_SPLIT_AVOID;
4253 	}
4254 	policy->dynamic_odm_policy = dc->debug.enable_single_display_2to1_odm_policy;
4255 	dc->debug.enable_single_display_2to1_odm_policy = false;
4256 	policy->subvp_policy = dc->debug.force_disable_subvp;
4257 	dc->debug.force_disable_subvp = true;
4258 	for (i = 0; i < context->stream_count; i++) {
4259 		policy->force_odm[i] = context->streams[i]->debug.force_odm_combine_segments;
4260 		if (context->streams[i]->debug.allow_transition_for_forced_odm)
4261 			context->streams[i]->debug.force_odm_combine_segments = 0;
4262 	}
4263 }
4264 
restore_minimal_pipe_split_policy(struct dc * dc,struct dc_state * context,struct pipe_split_policy_backup * policy)4265 static void restore_minimal_pipe_split_policy(struct dc *dc,
4266 		struct dc_state *context,
4267 		struct pipe_split_policy_backup *policy)
4268 {
4269 	uint8_t i;
4270 
4271 	if (!dc->config.is_vmin_only_asic)
4272 		dc->debug.pipe_split_policy = policy->mpc_policy;
4273 	dc->debug.enable_single_display_2to1_odm_policy =
4274 			policy->dynamic_odm_policy;
4275 	dc->debug.force_disable_subvp = policy->subvp_policy;
4276 	for (i = 0; i < context->stream_count; i++)
4277 		context->streams[i]->debug.force_odm_combine_segments = policy->force_odm[i];
4278 }
4279 
release_minimal_transition_state(struct dc * dc,struct dc_state * minimal_transition_context,struct dc_state * base_context,struct pipe_split_policy_backup * policy)4280 static void release_minimal_transition_state(struct dc *dc,
4281 		struct dc_state *minimal_transition_context,
4282 		struct dc_state *base_context,
4283 		struct pipe_split_policy_backup *policy)
4284 {
4285 	restore_minimal_pipe_split_policy(dc, base_context, policy);
4286 	dc_state_release(minimal_transition_context);
4287 }
4288 
force_vsync_flip_in_minimal_transition_context(struct dc_state * context)4289 static void force_vsync_flip_in_minimal_transition_context(struct dc_state *context)
4290 {
4291 	uint8_t i;
4292 	int j;
4293 	struct dc_stream_status *stream_status;
4294 
4295 	for (i = 0; i < context->stream_count; i++) {
4296 		stream_status = &context->stream_status[i];
4297 
4298 		for (j = 0; j < stream_status->plane_count; j++)
4299 			stream_status->plane_states[j]->flip_immediate = false;
4300 	}
4301 }
4302 
create_minimal_transition_state(struct dc * dc,struct dc_state * base_context,struct pipe_split_policy_backup * policy)4303 static struct dc_state *create_minimal_transition_state(struct dc *dc,
4304 		struct dc_state *base_context, struct pipe_split_policy_backup *policy)
4305 {
4306 	struct dc_state *minimal_transition_context = NULL;
4307 
4308 	minimal_transition_context = dc_state_create_copy(base_context);
4309 	if (!minimal_transition_context)
4310 		return NULL;
4311 
4312 	backup_and_set_minimal_pipe_split_policy(dc, base_context, policy);
4313 	/* commit minimal state */
4314 	if (dc->res_pool->funcs->validate_bandwidth(dc, minimal_transition_context, false)) {
4315 		/* prevent underflow and corruption when reconfiguring pipes */
4316 		force_vsync_flip_in_minimal_transition_context(minimal_transition_context);
4317 	} else {
4318 		/*
4319 		 * This should never happen, minimal transition state should
4320 		 * always be validated first before adding pipe split features.
4321 		 */
4322 		release_minimal_transition_state(dc, minimal_transition_context, base_context, policy);
4323 		BREAK_TO_DEBUGGER();
4324 		minimal_transition_context = NULL;
4325 	}
4326 	return minimal_transition_context;
4327 }
4328 
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)4329 static bool is_pipe_topology_transition_seamless_with_intermediate_step(
4330 		struct dc *dc,
4331 		struct dc_state *initial_state,
4332 		struct dc_state *intermediate_state,
4333 		struct dc_state *final_state)
4334 {
4335 	return dc->hwss.is_pipe_topology_transition_seamless(dc, initial_state,
4336 			intermediate_state) &&
4337 			dc->hwss.is_pipe_topology_transition_seamless(dc,
4338 					intermediate_state, final_state);
4339 }
4340 
swap_and_release_current_context(struct dc * dc,struct dc_state * new_context,struct dc_stream_state * stream)4341 static void swap_and_release_current_context(struct dc *dc,
4342 		struct dc_state *new_context, struct dc_stream_state *stream)
4343 {
4344 
4345 	int i;
4346 	struct dc_state *old = dc->current_state;
4347 	struct pipe_ctx *pipe_ctx;
4348 
4349 	/* Since memory free requires elevated IRQ, an interrupt
4350 	 * request is generated by mem free. If this happens
4351 	 * between freeing and reassigning the context, our vsync
4352 	 * interrupt will call into dc and cause a memory
4353 	 * corruption. Hence, we first reassign the context,
4354 	 * then free the old context.
4355 	 */
4356 	dc->current_state = new_context;
4357 	dc_state_release(old);
4358 
4359 	// clear any forced full updates
4360 	for (i = 0; i < dc->res_pool->pipe_count; i++) {
4361 		pipe_ctx = &new_context->res_ctx.pipe_ctx[i];
4362 
4363 		if (pipe_ctx->plane_state && pipe_ctx->stream == stream)
4364 			pipe_ctx->plane_state->force_full_update = false;
4365 	}
4366 }
4367 
initialize_empty_surface_updates(struct dc_stream_state * stream,struct dc_surface_update * srf_updates)4368 static int initialize_empty_surface_updates(
4369 		struct dc_stream_state *stream,
4370 		struct dc_surface_update *srf_updates)
4371 {
4372 	struct dc_stream_status *status = dc_stream_get_status(stream);
4373 	int i;
4374 
4375 	if (!status)
4376 		return 0;
4377 
4378 	for (i = 0; i < status->plane_count; i++)
4379 		srf_updates[i].surface = status->plane_states[i];
4380 
4381 	return status->plane_count;
4382 }
4383 
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)4384 static bool commit_minimal_transition_based_on_new_context(struct dc *dc,
4385 		struct dc_state *new_context,
4386 		struct dc_stream_state *stream,
4387 		struct dc_surface_update *srf_updates,
4388 		int surface_count)
4389 {
4390 	bool success = false;
4391 	struct pipe_split_policy_backup policy;
4392 	struct dc_state *intermediate_context =
4393 			create_minimal_transition_state(dc, new_context,
4394 					&policy);
4395 
4396 	if (intermediate_context) {
4397 		if (is_pipe_topology_transition_seamless_with_intermediate_step(
4398 				dc,
4399 				dc->current_state,
4400 				intermediate_context,
4401 				new_context)) {
4402 			DC_LOG_DC("commit minimal transition state: base = new state\n");
4403 			commit_planes_for_stream(dc, srf_updates,
4404 					surface_count, stream, NULL,
4405 					UPDATE_TYPE_FULL, intermediate_context);
4406 			swap_and_release_current_context(
4407 					dc, intermediate_context, stream);
4408 			dc_state_retain(dc->current_state);
4409 			success = true;
4410 		}
4411 		release_minimal_transition_state(
4412 				dc, intermediate_context, new_context, &policy);
4413 	}
4414 	return success;
4415 }
4416 
commit_minimal_transition_based_on_current_context(struct dc * dc,struct dc_state * new_context,struct dc_stream_state * stream)4417 static bool commit_minimal_transition_based_on_current_context(struct dc *dc,
4418 		struct dc_state *new_context, struct dc_stream_state *stream)
4419 {
4420 	bool success = false;
4421 	struct pipe_split_policy_backup policy;
4422 	struct dc_state *intermediate_context;
4423 	struct dc_state *old_current_state = dc->current_state;
4424 	struct dc_surface_update srf_updates[MAX_SURFACES] = {0};
4425 	int surface_count;
4426 
4427 	/*
4428 	 * Both current and new contexts share the same stream and plane state
4429 	 * pointers. When new context is validated, stream and planes get
4430 	 * populated with new updates such as new plane addresses. This makes
4431 	 * the current context no longer valid because stream and planes are
4432 	 * modified from the original. We backup current stream and plane states
4433 	 * into scratch space whenever we are populating new context. So we can
4434 	 * restore the original values back by calling the restore function now.
4435 	 * This restores back the original stream and plane states associated
4436 	 * with the current state.
4437 	 */
4438 	restore_planes_and_stream_state(&dc->scratch.current_state, stream);
4439 	dc_state_retain(old_current_state);
4440 	intermediate_context = create_minimal_transition_state(dc,
4441 			old_current_state, &policy);
4442 
4443 	if (intermediate_context) {
4444 		if (is_pipe_topology_transition_seamless_with_intermediate_step(
4445 				dc,
4446 				dc->current_state,
4447 				intermediate_context,
4448 				new_context)) {
4449 			DC_LOG_DC("commit minimal transition state: base = current state\n");
4450 			surface_count = initialize_empty_surface_updates(
4451 					stream, srf_updates);
4452 			commit_planes_for_stream(dc, srf_updates,
4453 					surface_count, stream, NULL,
4454 					UPDATE_TYPE_FULL, intermediate_context);
4455 			swap_and_release_current_context(
4456 					dc, intermediate_context, stream);
4457 			dc_state_retain(dc->current_state);
4458 			success = true;
4459 		}
4460 		release_minimal_transition_state(dc, intermediate_context,
4461 				old_current_state, &policy);
4462 	}
4463 	dc_state_release(old_current_state);
4464 	/*
4465 	 * Restore stream and plane states back to the values associated with
4466 	 * new context.
4467 	 */
4468 	restore_planes_and_stream_state(&dc->scratch.new_state, stream);
4469 	return success;
4470 }
4471 
4472 /**
4473  * commit_minimal_transition_state_in_dc_update - Commit a minimal state based
4474  * on current or new context
4475  *
4476  * @dc: DC structure, used to get the current state
4477  * @new_context: New context
4478  * @stream: Stream getting the update for the flip
4479  * @srf_updates: Surface updates
4480  * @surface_count: Number of surfaces
4481  *
4482  * The function takes in current state and new state and determine a minimal
4483  * transition state as the intermediate step which could make the transition
4484  * between current and new states seamless. If found, it will commit the minimal
4485  * transition state and update current state to this minimal transition state
4486  * and return true, if not, it will return false.
4487  *
4488  * Return:
4489  * Return True if the minimal transition succeeded, false otherwise
4490  */
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)4491 static bool commit_minimal_transition_state_in_dc_update(struct dc *dc,
4492 		struct dc_state *new_context,
4493 		struct dc_stream_state *stream,
4494 		struct dc_surface_update *srf_updates,
4495 		int surface_count)
4496 {
4497 	bool success = commit_minimal_transition_based_on_new_context(
4498 				dc, new_context, stream, srf_updates,
4499 				surface_count);
4500 	if (!success)
4501 		success = commit_minimal_transition_based_on_current_context(dc,
4502 				new_context, stream);
4503 	if (!success)
4504 		DC_LOG_ERROR("Fail to commit a seamless minimal transition state between current and new states.\nThis pipe topology update is non-seamless!\n");
4505 	return success;
4506 }
4507 
4508 /**
4509  * commit_minimal_transition_state - Create a transition pipe split state
4510  *
4511  * @dc: Used to get the current state status
4512  * @transition_base_context: New transition state
4513  *
4514  * In some specific configurations, such as pipe split on multi-display with
4515  * MPO and/or Dynamic ODM, removing a plane may cause unsupported pipe
4516  * programming when moving to new planes. To mitigate those types of problems,
4517  * this function adds a transition state that minimizes pipe usage before
4518  * programming the new configuration. When adding a new plane, the current
4519  * state requires the least pipes, so it is applied without splitting. When
4520  * removing a plane, the new state requires the least pipes, so it is applied
4521  * without splitting.
4522  *
4523  * Return:
4524  * Return false if something is wrong in the transition state.
4525  */
commit_minimal_transition_state(struct dc * dc,struct dc_state * transition_base_context)4526 static bool commit_minimal_transition_state(struct dc *dc,
4527 		struct dc_state *transition_base_context)
4528 {
4529 	struct dc_state *transition_context;
4530 	struct pipe_split_policy_backup policy;
4531 	enum dc_status ret = DC_ERROR_UNEXPECTED;
4532 	unsigned int i, j;
4533 	unsigned int pipe_in_use = 0;
4534 	bool subvp_in_use = false;
4535 	bool odm_in_use = false;
4536 
4537 	/* check current pipes in use*/
4538 	for (i = 0; i < dc->res_pool->pipe_count; i++) {
4539 		struct pipe_ctx *pipe = &transition_base_context->res_ctx.pipe_ctx[i];
4540 
4541 		if (pipe->plane_state)
4542 			pipe_in_use++;
4543 	}
4544 
4545 	/* If SubVP is enabled and we are adding or removing planes from any main subvp
4546 	 * pipe, we must use the minimal transition.
4547 	 */
4548 	for (i = 0; i < dc->res_pool->pipe_count; i++) {
4549 		struct pipe_ctx *pipe = &dc->current_state->res_ctx.pipe_ctx[i];
4550 
4551 		if (pipe->stream && dc_state_get_pipe_subvp_type(dc->current_state, pipe) == SUBVP_PHANTOM) {
4552 			subvp_in_use = true;
4553 			break;
4554 		}
4555 	}
4556 
4557 	/* If ODM is enabled and we are adding or removing planes from any ODM
4558 	 * pipe, we must use the minimal transition.
4559 	 */
4560 	for (i = 0; i < dc->res_pool->pipe_count; i++) {
4561 		struct pipe_ctx *pipe = &transition_base_context->res_ctx.pipe_ctx[i];
4562 
4563 		if (resource_is_pipe_type(pipe, OTG_MASTER)) {
4564 			odm_in_use = resource_get_odm_slice_count(pipe) > 1;
4565 			break;
4566 		}
4567 	}
4568 
4569 	/* When the OS add a new surface if we have been used all of pipes with odm combine
4570 	 * and mpc split feature, it need use commit_minimal_transition_state to transition safely.
4571 	 * After OS exit MPO, it will back to use odm and mpc split with all of pipes, we need
4572 	 * call it again. Otherwise return true to skip.
4573 	 *
4574 	 * Reduce the scenarios to use dc_commit_state_no_check in the stage of flip. Especially
4575 	 * enter/exit MPO when DCN still have enough resources.
4576 	 */
4577 	if (pipe_in_use != dc->res_pool->pipe_count && !subvp_in_use && !odm_in_use)
4578 		return true;
4579 
4580 	DC_LOG_DC("%s base = %s state, reason = %s\n", __func__,
4581 			dc->current_state == transition_base_context ? "current" : "new",
4582 			subvp_in_use ? "Subvp In Use" :
4583 			odm_in_use ? "ODM in Use" :
4584 			dc->debug.pipe_split_policy != MPC_SPLIT_AVOID ? "MPC in Use" :
4585 			"Unknown");
4586 
4587 	dc_state_retain(transition_base_context);
4588 	transition_context = create_minimal_transition_state(dc,
4589 			transition_base_context, &policy);
4590 	if (transition_context) {
4591 		ret = dc_commit_state_no_check(dc, transition_context);
4592 		release_minimal_transition_state(dc, transition_context, transition_base_context, &policy);
4593 	}
4594 	dc_state_release(transition_base_context);
4595 
4596 	if (ret != DC_OK) {
4597 		/* this should never happen */
4598 		BREAK_TO_DEBUGGER();
4599 		return false;
4600 	}
4601 
4602 	/* force full surface update */
4603 	for (i = 0; i < dc->current_state->stream_count; i++) {
4604 		for (j = 0; j < dc->current_state->stream_status[i].plane_count; j++) {
4605 			dc->current_state->stream_status[i].plane_states[j]->update_flags.raw = 0xFFFFFFFF;
4606 		}
4607 	}
4608 
4609 	return true;
4610 }
4611 
populate_fast_updates(struct dc_fast_update * fast_update,struct dc_surface_update * srf_updates,int surface_count,struct dc_stream_update * stream_update)4612 void populate_fast_updates(struct dc_fast_update *fast_update,
4613 		struct dc_surface_update *srf_updates,
4614 		int surface_count,
4615 		struct dc_stream_update *stream_update)
4616 {
4617 	int i = 0;
4618 
4619 	if (stream_update) {
4620 		fast_update[0].out_transfer_func = stream_update->out_transfer_func;
4621 		fast_update[0].output_csc_transform = stream_update->output_csc_transform;
4622 	} else {
4623 		fast_update[0].out_transfer_func = NULL;
4624 		fast_update[0].output_csc_transform = NULL;
4625 	}
4626 
4627 	for (i = 0; i < surface_count; i++) {
4628 		fast_update[i].flip_addr = srf_updates[i].flip_addr;
4629 		fast_update[i].gamma = srf_updates[i].gamma;
4630 		fast_update[i].gamut_remap_matrix = srf_updates[i].gamut_remap_matrix;
4631 		fast_update[i].input_csc_color_matrix = srf_updates[i].input_csc_color_matrix;
4632 		fast_update[i].coeff_reduction_factor = srf_updates[i].coeff_reduction_factor;
4633 		fast_update[i].cursor_csc_color_matrix = srf_updates[i].cursor_csc_color_matrix;
4634 	}
4635 }
4636 
fast_updates_exist(struct dc_fast_update * fast_update,int surface_count)4637 static bool fast_updates_exist(struct dc_fast_update *fast_update, int surface_count)
4638 {
4639 	int i;
4640 
4641 	if (fast_update[0].out_transfer_func ||
4642 		fast_update[0].output_csc_transform)
4643 		return true;
4644 
4645 	for (i = 0; i < surface_count; i++) {
4646 		if (fast_update[i].flip_addr ||
4647 				fast_update[i].gamma ||
4648 				fast_update[i].gamut_remap_matrix ||
4649 				fast_update[i].input_csc_color_matrix ||
4650 				fast_update[i].cursor_csc_color_matrix ||
4651 				fast_update[i].coeff_reduction_factor)
4652 			return true;
4653 	}
4654 
4655 	return false;
4656 }
4657 
fast_nonaddr_updates_exist(struct dc_fast_update * fast_update,int surface_count)4658 bool fast_nonaddr_updates_exist(struct dc_fast_update *fast_update, int surface_count)
4659 {
4660 	int i;
4661 
4662 	if (fast_update[0].out_transfer_func ||
4663 		fast_update[0].output_csc_transform)
4664 		return true;
4665 
4666 	for (i = 0; i < surface_count; i++) {
4667 		if (fast_update[i].input_csc_color_matrix ||
4668 				fast_update[i].gamma ||
4669 				fast_update[i].gamut_remap_matrix ||
4670 				fast_update[i].coeff_reduction_factor ||
4671 				fast_update[i].cursor_csc_color_matrix)
4672 			return true;
4673 	}
4674 
4675 	return false;
4676 }
4677 
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)4678 static bool full_update_required(struct dc *dc,
4679 		struct dc_surface_update *srf_updates,
4680 		int surface_count,
4681 		struct dc_stream_update *stream_update,
4682 		struct dc_stream_state *stream)
4683 {
4684 
4685 	int i;
4686 	struct dc_stream_status *stream_status;
4687 	const struct dc_state *context = dc->current_state;
4688 
4689 	for (i = 0; i < surface_count; i++) {
4690 		if (srf_updates &&
4691 				(srf_updates[i].plane_info ||
4692 				srf_updates[i].scaling_info ||
4693 				(srf_updates[i].hdr_mult.value &&
4694 				srf_updates[i].hdr_mult.value != srf_updates->surface->hdr_mult.value) ||
4695 				(srf_updates[i].sdr_white_level_nits &&
4696 				srf_updates[i].sdr_white_level_nits != srf_updates->surface->sdr_white_level_nits) ||
4697 				srf_updates[i].in_transfer_func ||
4698 				srf_updates[i].func_shaper ||
4699 				srf_updates[i].lut3d_func ||
4700 				srf_updates[i].surface->force_full_update ||
4701 				(srf_updates[i].flip_addr &&
4702 				srf_updates[i].flip_addr->address.tmz_surface != srf_updates[i].surface->address.tmz_surface) ||
4703 				(srf_updates[i].cm2_params &&
4704 				 (srf_updates[i].cm2_params->component_settings.shaper_3dlut_setting != srf_updates[i].surface->mcm_shaper_3dlut_setting ||
4705 				  srf_updates[i].cm2_params->component_settings.lut1d_enable != srf_updates[i].surface->mcm_lut1d_enable)) ||
4706 				!is_surface_in_context(context, srf_updates[i].surface)))
4707 			return true;
4708 	}
4709 
4710 	if (stream_update &&
4711 			(((stream_update->src.height != 0 && stream_update->src.width != 0) ||
4712 			(stream_update->dst.height != 0 && stream_update->dst.width != 0) ||
4713 			stream_update->integer_scaling_update) ||
4714 			stream_update->hdr_static_metadata ||
4715 			stream_update->abm_level ||
4716 			stream_update->periodic_interrupt ||
4717 			stream_update->vrr_infopacket ||
4718 			stream_update->vsc_infopacket ||
4719 			stream_update->vsp_infopacket ||
4720 			stream_update->hfvsif_infopacket ||
4721 			stream_update->vtem_infopacket ||
4722 			stream_update->adaptive_sync_infopacket ||
4723 			stream_update->dpms_off ||
4724 			stream_update->allow_freesync ||
4725 			stream_update->vrr_active_variable ||
4726 			stream_update->vrr_active_fixed ||
4727 			stream_update->gamut_remap ||
4728 			stream_update->output_color_space ||
4729 			stream_update->dither_option ||
4730 			stream_update->wb_update ||
4731 			stream_update->dsc_config ||
4732 			stream_update->mst_bw_update ||
4733 			stream_update->func_shaper ||
4734 			stream_update->lut3d_func ||
4735 			stream_update->pending_test_pattern ||
4736 			stream_update->crtc_timing_adjust ||
4737 			stream_update->scaler_sharpener_update))
4738 		return true;
4739 
4740 	if (stream) {
4741 		stream_status = dc_stream_get_status(stream);
4742 		if (stream_status == NULL || stream_status->plane_count != surface_count)
4743 			return true;
4744 	}
4745 	if (dc->idle_optimizations_allowed)
4746 		return true;
4747 
4748 	return false;
4749 }
4750 
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)4751 static bool fast_update_only(struct dc *dc,
4752 		struct dc_fast_update *fast_update,
4753 		struct dc_surface_update *srf_updates,
4754 		int surface_count,
4755 		struct dc_stream_update *stream_update,
4756 		struct dc_stream_state *stream)
4757 {
4758 	return fast_updates_exist(fast_update, surface_count)
4759 			&& !full_update_required(dc, srf_updates, surface_count, stream_update, stream);
4760 }
4761 
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)4762 static bool update_planes_and_stream_v1(struct dc *dc,
4763 		struct dc_surface_update *srf_updates, int surface_count,
4764 		struct dc_stream_state *stream,
4765 		struct dc_stream_update *stream_update,
4766 		struct dc_state *state)
4767 {
4768 	const struct dc_stream_status *stream_status;
4769 	enum surface_update_type update_type;
4770 	struct dc_state *context;
4771 	struct dc_context *dc_ctx = dc->ctx;
4772 	int i, j;
4773 	struct dc_fast_update fast_update[MAX_SURFACES] = {0};
4774 
4775 	dc_exit_ips_for_hw_access(dc);
4776 
4777 	populate_fast_updates(fast_update, srf_updates, surface_count, stream_update);
4778 	stream_status = dc_stream_get_status(stream);
4779 	context = dc->current_state;
4780 
4781 	update_type = dc_check_update_surfaces_for_stream(
4782 				dc, srf_updates, surface_count, stream_update, stream_status);
4783 
4784 	if (update_type >= UPDATE_TYPE_FULL) {
4785 
4786 		/* initialize scratch memory for building context */
4787 		context = dc_state_create_copy(state);
4788 		if (context == NULL) {
4789 			DC_ERROR("Failed to allocate new validate context!\n");
4790 			return false;
4791 		}
4792 
4793 		for (i = 0; i < dc->res_pool->pipe_count; i++) {
4794 			struct pipe_ctx *new_pipe = &context->res_ctx.pipe_ctx[i];
4795 			struct pipe_ctx *old_pipe = &dc->current_state->res_ctx.pipe_ctx[i];
4796 
4797 			if (new_pipe->plane_state && new_pipe->plane_state != old_pipe->plane_state)
4798 				new_pipe->plane_state->force_full_update = true;
4799 		}
4800 	} else if (update_type == UPDATE_TYPE_FAST) {
4801 		/*
4802 		 * Previous frame finished and HW is ready for optimization.
4803 		 */
4804 		dc_post_update_surfaces_to_stream(dc);
4805 	}
4806 
4807 	for (i = 0; i < surface_count; i++) {
4808 		struct dc_plane_state *surface = srf_updates[i].surface;
4809 
4810 		copy_surface_update_to_plane(surface, &srf_updates[i]);
4811 
4812 		if (update_type >= UPDATE_TYPE_MED) {
4813 			for (j = 0; j < dc->res_pool->pipe_count; j++) {
4814 				struct pipe_ctx *pipe_ctx =
4815 					&context->res_ctx.pipe_ctx[j];
4816 
4817 				if (pipe_ctx->plane_state != surface)
4818 					continue;
4819 
4820 				resource_build_scaling_params(pipe_ctx);
4821 			}
4822 		}
4823 	}
4824 
4825 	copy_stream_update_to_stream(dc, context, stream, stream_update);
4826 
4827 	if (update_type >= UPDATE_TYPE_FULL) {
4828 		if (!dc->res_pool->funcs->validate_bandwidth(dc, context, false)) {
4829 			DC_ERROR("Mode validation failed for stream update!\n");
4830 			dc_state_release(context);
4831 			return false;
4832 		}
4833 	}
4834 
4835 	TRACE_DC_PIPE_STATE(pipe_ctx, i, MAX_PIPES);
4836 
4837 	if (fast_update_only(dc, fast_update, srf_updates, surface_count, stream_update, stream) &&
4838 			!dc->debug.enable_legacy_fast_update) {
4839 		commit_planes_for_stream_fast(dc,
4840 				srf_updates,
4841 				surface_count,
4842 				stream,
4843 				stream_update,
4844 				update_type,
4845 				context);
4846 	} else {
4847 		commit_planes_for_stream(
4848 				dc,
4849 				srf_updates,
4850 				surface_count,
4851 				stream,
4852 				stream_update,
4853 				update_type,
4854 				context);
4855 	}
4856 	/*update current_State*/
4857 	if (dc->current_state != context) {
4858 
4859 		struct dc_state *old = dc->current_state;
4860 
4861 		dc->current_state = context;
4862 		dc_state_release(old);
4863 
4864 		for (i = 0; i < dc->res_pool->pipe_count; i++) {
4865 			struct pipe_ctx *pipe_ctx = &context->res_ctx.pipe_ctx[i];
4866 
4867 			if (pipe_ctx->plane_state && pipe_ctx->stream == stream)
4868 				pipe_ctx->plane_state->force_full_update = false;
4869 		}
4870 	}
4871 
4872 	/* Legacy optimization path for DCE. */
4873 	if (update_type >= UPDATE_TYPE_FULL && dc_ctx->dce_version < DCE_VERSION_MAX) {
4874 		dc_post_update_surfaces_to_stream(dc);
4875 		TRACE_DCE_CLOCK_STATE(&context->bw_ctx.bw.dce);
4876 	}
4877 	return true;
4878 }
4879 
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)4880 static bool update_planes_and_stream_v2(struct dc *dc,
4881 		struct dc_surface_update *srf_updates, int surface_count,
4882 		struct dc_stream_state *stream,
4883 		struct dc_stream_update *stream_update)
4884 {
4885 	struct dc_state *context;
4886 	enum surface_update_type update_type;
4887 	struct dc_fast_update fast_update[MAX_SURFACES] = {0};
4888 
4889 	/* In cases where MPO and split or ODM are used transitions can
4890 	 * cause underflow. Apply stream configuration with minimal pipe
4891 	 * split first to avoid unsupported transitions for active pipes.
4892 	 */
4893 	bool force_minimal_pipe_splitting = 0;
4894 	bool is_plane_addition = 0;
4895 	bool is_fast_update_only;
4896 
4897 	populate_fast_updates(fast_update, srf_updates, surface_count, stream_update);
4898 	is_fast_update_only = fast_update_only(dc, fast_update, srf_updates,
4899 			surface_count, stream_update, stream);
4900 	force_minimal_pipe_splitting = could_mpcc_tree_change_for_active_pipes(
4901 			dc,
4902 			stream,
4903 			srf_updates,
4904 			surface_count,
4905 			&is_plane_addition);
4906 
4907 	/* on plane addition, minimal state is the current one */
4908 	if (force_minimal_pipe_splitting && is_plane_addition &&
4909 		!commit_minimal_transition_state(dc, dc->current_state))
4910 		return false;
4911 
4912 	if (!update_planes_and_stream_state(
4913 			dc,
4914 			srf_updates,
4915 			surface_count,
4916 			stream,
4917 			stream_update,
4918 			&update_type,
4919 			&context))
4920 		return false;
4921 
4922 	/* on plane removal, minimal state is the new one */
4923 	if (force_minimal_pipe_splitting && !is_plane_addition) {
4924 		if (!commit_minimal_transition_state(dc, context)) {
4925 			dc_state_release(context);
4926 			return false;
4927 		}
4928 		update_type = UPDATE_TYPE_FULL;
4929 	}
4930 
4931 	if (dc->hwss.is_pipe_topology_transition_seamless &&
4932 			!dc->hwss.is_pipe_topology_transition_seamless(
4933 					dc, dc->current_state, context))
4934 		commit_minimal_transition_state_in_dc_update(dc, context, stream,
4935 				srf_updates, surface_count);
4936 
4937 	if (is_fast_update_only && !dc->debug.enable_legacy_fast_update) {
4938 		commit_planes_for_stream_fast(dc,
4939 				srf_updates,
4940 				surface_count,
4941 				stream,
4942 				stream_update,
4943 				update_type,
4944 				context);
4945 	} else {
4946 		if (!stream_update &&
4947 				dc->hwss.is_pipe_topology_transition_seamless &&
4948 				!dc->hwss.is_pipe_topology_transition_seamless(
4949 						dc, dc->current_state, context)) {
4950 			DC_LOG_ERROR("performing non-seamless pipe topology transition with surface only update!\n");
4951 			BREAK_TO_DEBUGGER();
4952 		}
4953 		commit_planes_for_stream(
4954 				dc,
4955 				srf_updates,
4956 				surface_count,
4957 				stream,
4958 				stream_update,
4959 				update_type,
4960 				context);
4961 	}
4962 	if (dc->current_state != context)
4963 		swap_and_release_current_context(dc, context, stream);
4964 	return true;
4965 }
4966 
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)4967 static void commit_planes_and_stream_update_on_current_context(struct dc *dc,
4968 		struct dc_surface_update *srf_updates, int surface_count,
4969 		struct dc_stream_state *stream,
4970 		struct dc_stream_update *stream_update,
4971 		enum surface_update_type update_type)
4972 {
4973 	struct dc_fast_update fast_update[MAX_SURFACES] = {0};
4974 
4975 	ASSERT(update_type < UPDATE_TYPE_FULL);
4976 	populate_fast_updates(fast_update, srf_updates, surface_count,
4977 			stream_update);
4978 	if (fast_update_only(dc, fast_update, srf_updates, surface_count,
4979 			stream_update, stream) &&
4980 			!dc->debug.enable_legacy_fast_update)
4981 		commit_planes_for_stream_fast(dc,
4982 				srf_updates,
4983 				surface_count,
4984 				stream,
4985 				stream_update,
4986 				update_type,
4987 				dc->current_state);
4988 	else
4989 		commit_planes_for_stream(
4990 				dc,
4991 				srf_updates,
4992 				surface_count,
4993 				stream,
4994 				stream_update,
4995 				update_type,
4996 				dc->current_state);
4997 }
4998 
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)4999 static void commit_planes_and_stream_update_with_new_context(struct dc *dc,
5000 		struct dc_surface_update *srf_updates, int surface_count,
5001 		struct dc_stream_state *stream,
5002 		struct dc_stream_update *stream_update,
5003 		enum surface_update_type update_type,
5004 		struct dc_state *new_context)
5005 {
5006 	ASSERT(update_type >= UPDATE_TYPE_FULL);
5007 	if (!dc->hwss.is_pipe_topology_transition_seamless(dc,
5008 			dc->current_state, new_context))
5009 		/*
5010 		 * It is required by the feature design that all pipe topologies
5011 		 * using extra free pipes for power saving purposes such as
5012 		 * dynamic ODM or SubVp shall only be enabled when it can be
5013 		 * transitioned seamlessly to AND from its minimal transition
5014 		 * state. A minimal transition state is defined as the same dc
5015 		 * state but with all power saving features disabled. So it uses
5016 		 * the minimum pipe topology. When we can't seamlessly
5017 		 * transition from state A to state B, we will insert the
5018 		 * minimal transition state A' or B' in between so seamless
5019 		 * transition between A and B can be made possible.
5020 		 */
5021 		commit_minimal_transition_state_in_dc_update(dc, new_context,
5022 				stream, srf_updates, surface_count);
5023 
5024 	commit_planes_for_stream(
5025 			dc,
5026 			srf_updates,
5027 			surface_count,
5028 			stream,
5029 			stream_update,
5030 			update_type,
5031 			new_context);
5032 }
5033 
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)5034 static bool update_planes_and_stream_v3(struct dc *dc,
5035 		struct dc_surface_update *srf_updates, int surface_count,
5036 		struct dc_stream_state *stream,
5037 		struct dc_stream_update *stream_update)
5038 {
5039 	struct dc_state *new_context;
5040 	enum surface_update_type update_type;
5041 
5042 	/*
5043 	 * When this function returns true and new_context is not equal to
5044 	 * current state, the function allocates and validates a new dc state
5045 	 * and assigns it to new_context. The function expects that the caller
5046 	 * is responsible to free this memory when new_context is no longer
5047 	 * used. We swap current with new context and free current instead. So
5048 	 * new_context's memory will live until the next full update after it is
5049 	 * replaced by a newer context. Refer to the use of
5050 	 * swap_and_free_current_context below.
5051 	 */
5052 	if (!update_planes_and_stream_state(dc, srf_updates, surface_count,
5053 				stream, stream_update, &update_type,
5054 				&new_context))
5055 		return false;
5056 
5057 	if (new_context == dc->current_state) {
5058 		commit_planes_and_stream_update_on_current_context(dc,
5059 				srf_updates, surface_count, stream,
5060 				stream_update, update_type);
5061 	} else {
5062 		commit_planes_and_stream_update_with_new_context(dc,
5063 				srf_updates, surface_count, stream,
5064 				stream_update, update_type, new_context);
5065 		swap_and_release_current_context(dc, new_context, stream);
5066 	}
5067 
5068 	return true;
5069 }
5070 
clear_update_flags(struct dc_surface_update * srf_updates,int surface_count,struct dc_stream_state * stream)5071 static void clear_update_flags(struct dc_surface_update *srf_updates,
5072 	int surface_count, struct dc_stream_state *stream)
5073 {
5074 	int i;
5075 
5076 	if (stream)
5077 		stream->update_flags.raw = 0;
5078 
5079 	for (i = 0; i < surface_count; i++)
5080 		if (srf_updates[i].surface)
5081 			srf_updates[i].surface->update_flags.raw = 0;
5082 }
5083 
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)5084 bool dc_update_planes_and_stream(struct dc *dc,
5085 		struct dc_surface_update *srf_updates, int surface_count,
5086 		struct dc_stream_state *stream,
5087 		struct dc_stream_update *stream_update)
5088 {
5089 	bool ret = false;
5090 
5091 	dc_exit_ips_for_hw_access(dc);
5092 	/*
5093 	 * update planes and stream version 3 separates FULL and FAST updates
5094 	 * to their own sequences. It aims to clean up frequent checks for
5095 	 * update type resulting unnecessary branching in logic flow. It also
5096 	 * adds a new commit minimal transition sequence, which detects the need
5097 	 * for minimal transition based on the actual comparison of current and
5098 	 * new states instead of "predicting" it based on per feature software
5099 	 * policy.i.e could_mpcc_tree_change_for_active_pipes. The new commit
5100 	 * minimal transition sequence is made universal to any power saving
5101 	 * features that would use extra free pipes such as Dynamic ODM/MPC
5102 	 * Combine, MPO or SubVp. Therefore there is no longer a need to
5103 	 * specially handle compatibility problems with transitions among those
5104 	 * features as they are now transparent to the new sequence.
5105 	 */
5106 	if (dc->ctx->dce_version >= DCN_VERSION_4_01)
5107 		ret = update_planes_and_stream_v3(dc, srf_updates,
5108 				surface_count, stream, stream_update);
5109 	else
5110 		ret = update_planes_and_stream_v2(dc, srf_updates,
5111 			surface_count, stream, stream_update);
5112 
5113 	if (ret)
5114 		clear_update_flags(srf_updates, surface_count, stream);
5115 
5116 	return ret;
5117 }
5118 
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)5119 void dc_commit_updates_for_stream(struct dc *dc,
5120 		struct dc_surface_update *srf_updates,
5121 		int surface_count,
5122 		struct dc_stream_state *stream,
5123 		struct dc_stream_update *stream_update,
5124 		struct dc_state *state)
5125 {
5126 	bool ret = false;
5127 
5128 	dc_exit_ips_for_hw_access(dc);
5129 	/* TODO: Since change commit sequence can have a huge impact,
5130 	 * we decided to only enable it for DCN3x. However, as soon as
5131 	 * we get more confident about this change we'll need to enable
5132 	 * the new sequence for all ASICs.
5133 	 */
5134 	if (dc->ctx->dce_version >= DCN_VERSION_4_01) {
5135 		ret = update_planes_and_stream_v3(dc, srf_updates, surface_count,
5136 				stream, stream_update);
5137 	} else if (dc->ctx->dce_version >= DCN_VERSION_3_2) {
5138 		ret = update_planes_and_stream_v2(dc, srf_updates, surface_count,
5139 				stream, stream_update);
5140 	} else
5141 		ret = update_planes_and_stream_v1(dc, srf_updates, surface_count, stream,
5142 				stream_update, state);
5143 
5144 	if (ret)
5145 		clear_update_flags(srf_updates, surface_count, stream);
5146 }
5147 
dc_get_current_stream_count(struct dc * dc)5148 uint8_t dc_get_current_stream_count(struct dc *dc)
5149 {
5150 	return dc->current_state->stream_count;
5151 }
5152 
dc_get_stream_at_index(struct dc * dc,uint8_t i)5153 struct dc_stream_state *dc_get_stream_at_index(struct dc *dc, uint8_t i)
5154 {
5155 	if (i < dc->current_state->stream_count)
5156 		return dc->current_state->streams[i];
5157 	return NULL;
5158 }
5159 
dc_interrupt_to_irq_source(struct dc * dc,uint32_t src_id,uint32_t ext_id)5160 enum dc_irq_source dc_interrupt_to_irq_source(
5161 		struct dc *dc,
5162 		uint32_t src_id,
5163 		uint32_t ext_id)
5164 {
5165 	return dal_irq_service_to_irq_source(dc->res_pool->irqs, src_id, ext_id);
5166 }
5167 
5168 /*
5169  * dc_interrupt_set() - Enable/disable an AMD hw interrupt source
5170  */
dc_interrupt_set(struct dc * dc,enum dc_irq_source src,bool enable)5171 bool dc_interrupt_set(struct dc *dc, enum dc_irq_source src, bool enable)
5172 {
5173 
5174 	if (dc == NULL)
5175 		return false;
5176 
5177 	return dal_irq_service_set(dc->res_pool->irqs, src, enable);
5178 }
5179 
dc_interrupt_ack(struct dc * dc,enum dc_irq_source src)5180 void dc_interrupt_ack(struct dc *dc, enum dc_irq_source src)
5181 {
5182 	dal_irq_service_ack(dc->res_pool->irqs, src);
5183 }
5184 
dc_power_down_on_boot(struct dc * dc)5185 void dc_power_down_on_boot(struct dc *dc)
5186 {
5187 	if (dc->ctx->dce_environment != DCE_ENV_VIRTUAL_HW &&
5188 	    dc->hwss.power_down_on_boot) {
5189 		if (dc->caps.ips_support)
5190 			dc_exit_ips_for_hw_access(dc);
5191 		dc->hwss.power_down_on_boot(dc);
5192 	}
5193 }
5194 
dc_set_power_state(struct dc * dc,enum dc_acpi_cm_power_state power_state)5195 void dc_set_power_state(struct dc *dc, enum dc_acpi_cm_power_state power_state)
5196 {
5197 	if (!dc->current_state)
5198 		return;
5199 
5200 	switch (power_state) {
5201 	case DC_ACPI_CM_POWER_STATE_D0:
5202 		dc_state_construct(dc, dc->current_state);
5203 
5204 		dc_exit_ips_for_hw_access(dc);
5205 
5206 		dc_z10_restore(dc);
5207 
5208 		dc_dmub_srv_notify_fw_dc_power_state(dc->ctx->dmub_srv, power_state);
5209 
5210 		dc->hwss.init_hw(dc);
5211 
5212 		if (dc->hwss.init_sys_ctx != NULL &&
5213 			dc->vm_pa_config.valid) {
5214 			dc->hwss.init_sys_ctx(dc->hwseq, dc, &dc->vm_pa_config);
5215 		}
5216 
5217 		break;
5218 	default:
5219 		ASSERT(dc->current_state->stream_count == 0);
5220 
5221 		dc_dmub_srv_notify_fw_dc_power_state(dc->ctx->dmub_srv, power_state);
5222 
5223 		dc_state_destruct(dc->current_state);
5224 
5225 		break;
5226 	}
5227 }
5228 
dc_resume(struct dc * dc)5229 void dc_resume(struct dc *dc)
5230 {
5231 	uint32_t i;
5232 
5233 	for (i = 0; i < dc->link_count; i++)
5234 		dc->link_srv->resume(dc->links[i]);
5235 }
5236 
dc_is_dmcu_initialized(struct dc * dc)5237 bool dc_is_dmcu_initialized(struct dc *dc)
5238 {
5239 	struct dmcu *dmcu = dc->res_pool->dmcu;
5240 
5241 	if (dmcu)
5242 		return dmcu->funcs->is_dmcu_initialized(dmcu);
5243 	return false;
5244 }
5245 
get_clock_requirements_for_state(struct dc_state * state,struct AsicStateEx * info)5246 void get_clock_requirements_for_state(struct dc_state *state, struct AsicStateEx *info)
5247 {
5248 	info->displayClock				= (unsigned int)state->bw_ctx.bw.dcn.clk.dispclk_khz;
5249 	info->engineClock				= (unsigned int)state->bw_ctx.bw.dcn.clk.dcfclk_khz;
5250 	info->memoryClock				= (unsigned int)state->bw_ctx.bw.dcn.clk.dramclk_khz;
5251 	info->maxSupportedDppClock		= (unsigned int)state->bw_ctx.bw.dcn.clk.max_supported_dppclk_khz;
5252 	info->dppClock					= (unsigned int)state->bw_ctx.bw.dcn.clk.dppclk_khz;
5253 	info->socClock					= (unsigned int)state->bw_ctx.bw.dcn.clk.socclk_khz;
5254 	info->dcfClockDeepSleep			= (unsigned int)state->bw_ctx.bw.dcn.clk.dcfclk_deep_sleep_khz;
5255 	info->fClock					= (unsigned int)state->bw_ctx.bw.dcn.clk.fclk_khz;
5256 	info->phyClock					= (unsigned int)state->bw_ctx.bw.dcn.clk.phyclk_khz;
5257 }
dc_set_clock(struct dc * dc,enum dc_clock_type clock_type,uint32_t clk_khz,uint32_t stepping)5258 enum dc_status dc_set_clock(struct dc *dc, enum dc_clock_type clock_type, uint32_t clk_khz, uint32_t stepping)
5259 {
5260 	if (dc->hwss.set_clock)
5261 		return dc->hwss.set_clock(dc, clock_type, clk_khz, stepping);
5262 	return DC_ERROR_UNEXPECTED;
5263 }
dc_get_clock(struct dc * dc,enum dc_clock_type clock_type,struct dc_clock_config * clock_cfg)5264 void dc_get_clock(struct dc *dc, enum dc_clock_type clock_type, struct dc_clock_config *clock_cfg)
5265 {
5266 	if (dc->hwss.get_clock)
5267 		dc->hwss.get_clock(dc, clock_type, clock_cfg);
5268 }
5269 
5270 /* enable/disable eDP PSR without specify stream for eDP */
dc_set_psr_allow_active(struct dc * dc,bool enable)5271 bool dc_set_psr_allow_active(struct dc *dc, bool enable)
5272 {
5273 	int i;
5274 	bool allow_active;
5275 
5276 	for (i = 0; i < dc->current_state->stream_count ; i++) {
5277 		struct dc_link *link;
5278 		struct dc_stream_state *stream = dc->current_state->streams[i];
5279 
5280 		link = stream->link;
5281 		if (!link)
5282 			continue;
5283 
5284 		if (link->psr_settings.psr_feature_enabled) {
5285 			if (enable && !link->psr_settings.psr_allow_active) {
5286 				allow_active = true;
5287 				if (!dc_link_set_psr_allow_active(link, &allow_active, false, false, NULL))
5288 					return false;
5289 			} else if (!enable && link->psr_settings.psr_allow_active) {
5290 				allow_active = false;
5291 				if (!dc_link_set_psr_allow_active(link, &allow_active, true, false, NULL))
5292 					return false;
5293 			}
5294 		}
5295 	}
5296 
5297 	return true;
5298 }
5299 
5300 /* enable/disable eDP Replay without specify stream for eDP */
dc_set_replay_allow_active(struct dc * dc,bool active)5301 bool dc_set_replay_allow_active(struct dc *dc, bool active)
5302 {
5303 	int i;
5304 	bool allow_active;
5305 
5306 	for (i = 0; i < dc->current_state->stream_count; i++) {
5307 		struct dc_link *link;
5308 		struct dc_stream_state *stream = dc->current_state->streams[i];
5309 
5310 		link = stream->link;
5311 		if (!link)
5312 			continue;
5313 
5314 		if (link->replay_settings.replay_feature_enabled) {
5315 			if (active && !link->replay_settings.replay_allow_active) {
5316 				allow_active = true;
5317 				if (!dc_link_set_replay_allow_active(link, &allow_active,
5318 					false, false, NULL))
5319 					return false;
5320 			} else if (!active && link->replay_settings.replay_allow_active) {
5321 				allow_active = false;
5322 				if (!dc_link_set_replay_allow_active(link, &allow_active,
5323 					true, false, NULL))
5324 					return false;
5325 			}
5326 		}
5327 	}
5328 
5329 	return true;
5330 }
5331 
5332 /* set IPS disable state */
dc_set_ips_disable(struct dc * dc,unsigned int disable_ips)5333 bool dc_set_ips_disable(struct dc *dc, unsigned int disable_ips)
5334 {
5335 	dc_exit_ips_for_hw_access(dc);
5336 
5337 	dc->config.disable_ips = disable_ips;
5338 
5339 	return true;
5340 }
5341 
dc_allow_idle_optimizations_internal(struct dc * dc,bool allow,char const * caller_name)5342 void dc_allow_idle_optimizations_internal(struct dc *dc, bool allow, char const *caller_name)
5343 {
5344 	if (dc->debug.disable_idle_power_optimizations)
5345 		return;
5346 
5347 	if (allow != dc->idle_optimizations_allowed)
5348 		DC_LOG_IPS("%s: allow_idle old=%d new=%d (caller=%s)\n", __func__,
5349 			   dc->idle_optimizations_allowed, allow, caller_name);
5350 
5351 	if (dc->caps.ips_support && (dc->config.disable_ips == DMUB_IPS_DISABLE_ALL))
5352 		return;
5353 
5354 	if (dc->clk_mgr != NULL && dc->clk_mgr->funcs->is_smu_present)
5355 		if (!dc->clk_mgr->funcs->is_smu_present(dc->clk_mgr))
5356 			return;
5357 
5358 	if (allow == dc->idle_optimizations_allowed)
5359 		return;
5360 
5361 	if (dc->hwss.apply_idle_power_optimizations && dc->clk_mgr != NULL &&
5362 	    dc->hwss.apply_idle_power_optimizations(dc, allow))
5363 		dc->idle_optimizations_allowed = allow;
5364 }
5365 
dc_exit_ips_for_hw_access_internal(struct dc * dc,const char * caller_name)5366 void dc_exit_ips_for_hw_access_internal(struct dc *dc, const char *caller_name)
5367 {
5368 	if (dc->caps.ips_support)
5369 		dc_allow_idle_optimizations_internal(dc, false, caller_name);
5370 }
5371 
dc_dmub_is_ips_idle_state(struct dc * dc)5372 bool dc_dmub_is_ips_idle_state(struct dc *dc)
5373 {
5374 	if (dc->debug.disable_idle_power_optimizations)
5375 		return false;
5376 
5377 	if (!dc->caps.ips_support || (dc->config.disable_ips == DMUB_IPS_DISABLE_ALL))
5378 		return false;
5379 
5380 	if (!dc->ctx->dmub_srv)
5381 		return false;
5382 
5383 	return dc->ctx->dmub_srv->idle_allowed;
5384 }
5385 
5386 /* set min and max memory clock to lowest and highest DPM level, respectively */
dc_unlock_memory_clock_frequency(struct dc * dc)5387 void dc_unlock_memory_clock_frequency(struct dc *dc)
5388 {
5389 	if (dc->clk_mgr->funcs->set_hard_min_memclk)
5390 		dc->clk_mgr->funcs->set_hard_min_memclk(dc->clk_mgr, false);
5391 
5392 	if (dc->clk_mgr->funcs->set_hard_max_memclk)
5393 		dc->clk_mgr->funcs->set_hard_max_memclk(dc->clk_mgr);
5394 }
5395 
5396 /* set min memory clock to the min required for current mode, max to maxDPM */
dc_lock_memory_clock_frequency(struct dc * dc)5397 void dc_lock_memory_clock_frequency(struct dc *dc)
5398 {
5399 	if (dc->clk_mgr->funcs->get_memclk_states_from_smu)
5400 		dc->clk_mgr->funcs->get_memclk_states_from_smu(dc->clk_mgr);
5401 
5402 	if (dc->clk_mgr->funcs->set_hard_min_memclk)
5403 		dc->clk_mgr->funcs->set_hard_min_memclk(dc->clk_mgr, true);
5404 
5405 	if (dc->clk_mgr->funcs->set_hard_max_memclk)
5406 		dc->clk_mgr->funcs->set_hard_max_memclk(dc->clk_mgr);
5407 }
5408 
blank_and_force_memclk(struct dc * dc,bool apply,unsigned int memclk_mhz)5409 static void blank_and_force_memclk(struct dc *dc, bool apply, unsigned int memclk_mhz)
5410 {
5411 	struct dc_state *context = dc->current_state;
5412 	struct hubp *hubp;
5413 	struct pipe_ctx *pipe;
5414 	int i;
5415 
5416 	for (i = 0; i < dc->res_pool->pipe_count; i++) {
5417 		pipe = &context->res_ctx.pipe_ctx[i];
5418 
5419 		if (pipe->stream != NULL) {
5420 			dc->hwss.disable_pixel_data(dc, pipe, true);
5421 
5422 			// wait for double buffer
5423 			pipe->stream_res.tg->funcs->wait_for_state(pipe->stream_res.tg, CRTC_STATE_VACTIVE);
5424 			pipe->stream_res.tg->funcs->wait_for_state(pipe->stream_res.tg, CRTC_STATE_VBLANK);
5425 			pipe->stream_res.tg->funcs->wait_for_state(pipe->stream_res.tg, CRTC_STATE_VACTIVE);
5426 
5427 			hubp = pipe->plane_res.hubp;
5428 			hubp->funcs->set_blank_regs(hubp, true);
5429 		}
5430 	}
5431 	if (dc->clk_mgr->funcs->set_max_memclk)
5432 		dc->clk_mgr->funcs->set_max_memclk(dc->clk_mgr, memclk_mhz);
5433 	if (dc->clk_mgr->funcs->set_min_memclk)
5434 		dc->clk_mgr->funcs->set_min_memclk(dc->clk_mgr, memclk_mhz);
5435 
5436 	for (i = 0; i < dc->res_pool->pipe_count; i++) {
5437 		pipe = &context->res_ctx.pipe_ctx[i];
5438 
5439 		if (pipe->stream != NULL) {
5440 			dc->hwss.disable_pixel_data(dc, pipe, false);
5441 
5442 			hubp = pipe->plane_res.hubp;
5443 			hubp->funcs->set_blank_regs(hubp, false);
5444 		}
5445 	}
5446 }
5447 
5448 
5449 /**
5450  * dc_enable_dcmode_clk_limit() - lower clocks in dc (battery) mode
5451  * @dc: pointer to dc of the dm calling this
5452  * @enable: True = transition to DC mode, false = transition back to AC mode
5453  *
5454  * Some SoCs define additional clock limits when in DC mode, DM should
5455  * invoke this function when the platform undergoes a power source transition
5456  * so DC can apply/unapply the limit. This interface may be disruptive to
5457  * the onscreen content.
5458  *
5459  * Context: Triggered by OS through DM interface, or manually by escape calls.
5460  * Need to hold a dclock when doing so.
5461  *
5462  * Return: none (void function)
5463  *
5464  */
dc_enable_dcmode_clk_limit(struct dc * dc,bool enable)5465 void dc_enable_dcmode_clk_limit(struct dc *dc, bool enable)
5466 {
5467 	unsigned int softMax = 0, maxDPM = 0, funcMin = 0, i;
5468 	bool p_state_change_support;
5469 
5470 	if (!dc->config.dc_mode_clk_limit_support)
5471 		return;
5472 
5473 	softMax = dc->clk_mgr->bw_params->dc_mode_softmax_memclk;
5474 	for (i = 0; i < dc->clk_mgr->bw_params->clk_table.num_entries; i++) {
5475 		if (dc->clk_mgr->bw_params->clk_table.entries[i].memclk_mhz > maxDPM)
5476 			maxDPM = dc->clk_mgr->bw_params->clk_table.entries[i].memclk_mhz;
5477 	}
5478 	funcMin = (dc->clk_mgr->clks.dramclk_khz + 999) / 1000;
5479 	p_state_change_support = dc->clk_mgr->clks.p_state_change_support;
5480 
5481 	if (enable && !dc->clk_mgr->dc_mode_softmax_enabled) {
5482 		if (p_state_change_support) {
5483 			if (funcMin <= softMax && dc->clk_mgr->funcs->set_max_memclk)
5484 				dc->clk_mgr->funcs->set_max_memclk(dc->clk_mgr, softMax);
5485 			// else: No-Op
5486 		} else {
5487 			if (funcMin <= softMax)
5488 				blank_and_force_memclk(dc, true, softMax);
5489 			// else: No-Op
5490 		}
5491 	} else if (!enable && dc->clk_mgr->dc_mode_softmax_enabled) {
5492 		if (p_state_change_support) {
5493 			if (funcMin <= softMax && dc->clk_mgr->funcs->set_max_memclk)
5494 				dc->clk_mgr->funcs->set_max_memclk(dc->clk_mgr, maxDPM);
5495 			// else: No-Op
5496 		} else {
5497 			if (funcMin <= softMax)
5498 				blank_and_force_memclk(dc, true, maxDPM);
5499 			// else: No-Op
5500 		}
5501 	}
5502 	dc->clk_mgr->dc_mode_softmax_enabled = enable;
5503 }
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)5504 bool dc_is_plane_eligible_for_idle_optimizations(struct dc *dc,
5505 		unsigned int pitch,
5506 		unsigned int height,
5507 		enum surface_pixel_format format,
5508 		struct dc_cursor_attributes *cursor_attr)
5509 {
5510 	if (dc->hwss.does_plane_fit_in_mall && dc->hwss.does_plane_fit_in_mall(dc, pitch, height, format, cursor_attr))
5511 		return true;
5512 	return false;
5513 }
5514 
5515 /* cleanup on driver unload */
dc_hardware_release(struct dc * dc)5516 void dc_hardware_release(struct dc *dc)
5517 {
5518 	dc_mclk_switch_using_fw_based_vblank_stretch_shut_down(dc);
5519 
5520 	if (dc->hwss.hardware_release)
5521 		dc->hwss.hardware_release(dc);
5522 }
5523 
dc_mclk_switch_using_fw_based_vblank_stretch_shut_down(struct dc * dc)5524 void dc_mclk_switch_using_fw_based_vblank_stretch_shut_down(struct dc *dc)
5525 {
5526 	if (dc->current_state)
5527 		dc->current_state->bw_ctx.bw.dcn.clk.fw_based_mclk_switching_shut_down = true;
5528 }
5529 
5530 /**
5531  * dc_is_dmub_outbox_supported - Check if DMUB firmware support outbox notification
5532  *
5533  * @dc: [in] dc structure
5534  *
5535  * Checks whether DMUB FW supports outbox notifications, if supported DM
5536  * should register outbox interrupt prior to actually enabling interrupts
5537  * via dc_enable_dmub_outbox
5538  *
5539  * Return:
5540  * True if DMUB FW supports outbox notifications, False otherwise
5541  */
dc_is_dmub_outbox_supported(struct dc * dc)5542 bool dc_is_dmub_outbox_supported(struct dc *dc)
5543 {
5544 	if (!dc->caps.dmcub_support)
5545 		return false;
5546 
5547 	switch (dc->ctx->asic_id.chip_family) {
5548 
5549 	case FAMILY_YELLOW_CARP:
5550 		/* DCN31 B0 USB4 DPIA needs dmub notifications for interrupts */
5551 		if (dc->ctx->asic_id.hw_internal_rev == YELLOW_CARP_B0 &&
5552 		    !dc->debug.dpia_debug.bits.disable_dpia)
5553 			return true;
5554 	break;
5555 
5556 	case AMDGPU_FAMILY_GC_11_0_1:
5557 	case AMDGPU_FAMILY_GC_11_5_0:
5558 		if (!dc->debug.dpia_debug.bits.disable_dpia)
5559 			return true;
5560 	break;
5561 
5562 	default:
5563 		break;
5564 	}
5565 
5566 	/* dmub aux needs dmub notifications to be enabled */
5567 	return dc->debug.enable_dmub_aux_for_legacy_ddc;
5568 
5569 }
5570 
5571 /**
5572  * dc_enable_dmub_notifications - Check if dmub fw supports outbox
5573  *
5574  * @dc: [in] dc structure
5575  *
5576  * Calls dc_is_dmub_outbox_supported to check if dmub fw supports outbox
5577  * notifications. All DMs shall switch to dc_is_dmub_outbox_supported.  This
5578  * API shall be removed after switching.
5579  *
5580  * Return:
5581  * True if DMUB FW supports outbox notifications, False otherwise
5582  */
dc_enable_dmub_notifications(struct dc * dc)5583 bool dc_enable_dmub_notifications(struct dc *dc)
5584 {
5585 	return dc_is_dmub_outbox_supported(dc);
5586 }
5587 
5588 /**
5589  * dc_enable_dmub_outbox - Enables DMUB unsolicited notification
5590  *
5591  * @dc: [in] dc structure
5592  *
5593  * Enables DMUB unsolicited notifications to x86 via outbox.
5594  */
dc_enable_dmub_outbox(struct dc * dc)5595 void dc_enable_dmub_outbox(struct dc *dc)
5596 {
5597 	struct dc_context *dc_ctx = dc->ctx;
5598 
5599 	dmub_enable_outbox_notification(dc_ctx->dmub_srv);
5600 	DC_LOG_DC("%s: dmub outbox notifications enabled\n", __func__);
5601 }
5602 
5603 /**
5604  * dc_process_dmub_aux_transfer_async - Submits aux command to dmub via inbox message
5605  *                                      Sets port index appropriately for legacy DDC
5606  * @dc: dc structure
5607  * @link_index: link index
5608  * @payload: aux payload
5609  *
5610  * Returns: True if successful, False if failure
5611  */
dc_process_dmub_aux_transfer_async(struct dc * dc,uint32_t link_index,struct aux_payload * payload)5612 bool dc_process_dmub_aux_transfer_async(struct dc *dc,
5613 				uint32_t link_index,
5614 				struct aux_payload *payload)
5615 {
5616 	uint8_t action;
5617 	union dmub_rb_cmd cmd = {0};
5618 
5619 	ASSERT(payload->length <= 16);
5620 
5621 	cmd.dp_aux_access.header.type = DMUB_CMD__DP_AUX_ACCESS;
5622 	cmd.dp_aux_access.header.payload_bytes = 0;
5623 	/* For dpia, ddc_pin is set to NULL */
5624 	if (!dc->links[link_index]->ddc->ddc_pin)
5625 		cmd.dp_aux_access.aux_control.type = AUX_CHANNEL_DPIA;
5626 	else
5627 		cmd.dp_aux_access.aux_control.type = AUX_CHANNEL_LEGACY_DDC;
5628 
5629 	cmd.dp_aux_access.aux_control.instance = dc->links[link_index]->ddc_hw_inst;
5630 	cmd.dp_aux_access.aux_control.sw_crc_enabled = 0;
5631 	cmd.dp_aux_access.aux_control.timeout = 0;
5632 	cmd.dp_aux_access.aux_control.dpaux.address = payload->address;
5633 	cmd.dp_aux_access.aux_control.dpaux.is_i2c_over_aux = payload->i2c_over_aux;
5634 	cmd.dp_aux_access.aux_control.dpaux.length = payload->length;
5635 
5636 	/* set aux action */
5637 	if (payload->i2c_over_aux) {
5638 		if (payload->write) {
5639 			if (payload->mot)
5640 				action = DP_AUX_REQ_ACTION_I2C_WRITE_MOT;
5641 			else
5642 				action = DP_AUX_REQ_ACTION_I2C_WRITE;
5643 		} else {
5644 			if (payload->mot)
5645 				action = DP_AUX_REQ_ACTION_I2C_READ_MOT;
5646 			else
5647 				action = DP_AUX_REQ_ACTION_I2C_READ;
5648 			}
5649 	} else {
5650 		if (payload->write)
5651 			action = DP_AUX_REQ_ACTION_DPCD_WRITE;
5652 		else
5653 			action = DP_AUX_REQ_ACTION_DPCD_READ;
5654 	}
5655 
5656 	cmd.dp_aux_access.aux_control.dpaux.action = action;
5657 
5658 	if (payload->length && payload->write) {
5659 		memcpy(cmd.dp_aux_access.aux_control.dpaux.data,
5660 			payload->data,
5661 			payload->length
5662 			);
5663 	}
5664 
5665 	dc_wake_and_execute_dmub_cmd(dc->ctx, &cmd, DM_DMUB_WAIT_TYPE_WAIT);
5666 
5667 	return true;
5668 }
5669 
get_link_index_from_dpia_port_index(const struct dc * dc,uint8_t dpia_port_index)5670 uint8_t get_link_index_from_dpia_port_index(const struct dc *dc,
5671 					    uint8_t dpia_port_index)
5672 {
5673 	uint8_t index, link_index = 0xFF;
5674 
5675 	for (index = 0; index < dc->link_count; index++) {
5676 		/* ddc_hw_inst has dpia port index for dpia links
5677 		 * and ddc instance for legacy links
5678 		 */
5679 		if (!dc->links[index]->ddc->ddc_pin) {
5680 			if (dc->links[index]->ddc_hw_inst == dpia_port_index) {
5681 				link_index = index;
5682 				break;
5683 			}
5684 		}
5685 	}
5686 	ASSERT(link_index != 0xFF);
5687 	return link_index;
5688 }
5689 
5690 /**
5691  * dc_process_dmub_set_config_async - Submits set_config command
5692  *
5693  * @dc: [in] dc structure
5694  * @link_index: [in] link_index: link index
5695  * @payload: [in] aux payload
5696  * @notify: [out] set_config immediate reply
5697  *
5698  * Submits set_config command to dmub via inbox message.
5699  *
5700  * Return:
5701  * True if successful, False if failure
5702  */
dc_process_dmub_set_config_async(struct dc * dc,uint32_t link_index,struct set_config_cmd_payload * payload,struct dmub_notification * notify)5703 bool dc_process_dmub_set_config_async(struct dc *dc,
5704 				uint32_t link_index,
5705 				struct set_config_cmd_payload *payload,
5706 				struct dmub_notification *notify)
5707 {
5708 	union dmub_rb_cmd cmd = {0};
5709 	bool is_cmd_complete = true;
5710 
5711 	/* prepare SET_CONFIG command */
5712 	cmd.set_config_access.header.type = DMUB_CMD__DPIA;
5713 	cmd.set_config_access.header.sub_type = DMUB_CMD__DPIA_SET_CONFIG_ACCESS;
5714 
5715 	cmd.set_config_access.set_config_control.instance = dc->links[link_index]->ddc_hw_inst;
5716 	cmd.set_config_access.set_config_control.cmd_pkt.msg_type = payload->msg_type;
5717 	cmd.set_config_access.set_config_control.cmd_pkt.msg_data = payload->msg_data;
5718 
5719 	if (!dc_wake_and_execute_dmub_cmd(dc->ctx, &cmd, DM_DMUB_WAIT_TYPE_WAIT_WITH_REPLY)) {
5720 		/* command is not processed by dmub */
5721 		notify->sc_status = SET_CONFIG_UNKNOWN_ERROR;
5722 		return is_cmd_complete;
5723 	}
5724 
5725 	/* command processed by dmub, if ret_status is 1, it is completed instantly */
5726 	if (cmd.set_config_access.header.ret_status == 1)
5727 		notify->sc_status = cmd.set_config_access.set_config_control.immed_status;
5728 	else
5729 		/* cmd pending, will receive notification via outbox */
5730 		is_cmd_complete = false;
5731 
5732 	return is_cmd_complete;
5733 }
5734 
5735 /**
5736  * dc_process_dmub_set_mst_slots - Submits MST solt allocation
5737  *
5738  * @dc: [in] dc structure
5739  * @link_index: [in] link index
5740  * @mst_alloc_slots: [in] mst slots to be allotted
5741  * @mst_slots_in_use: [out] mst slots in use returned in failure case
5742  *
5743  * Submits mst slot allocation command to dmub via inbox message
5744  *
5745  * Return:
5746  * DC_OK if successful, DC_ERROR if failure
5747  */
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)5748 enum dc_status dc_process_dmub_set_mst_slots(const struct dc *dc,
5749 				uint32_t link_index,
5750 				uint8_t mst_alloc_slots,
5751 				uint8_t *mst_slots_in_use)
5752 {
5753 	union dmub_rb_cmd cmd = {0};
5754 
5755 	/* prepare MST_ALLOC_SLOTS command */
5756 	cmd.set_mst_alloc_slots.header.type = DMUB_CMD__DPIA;
5757 	cmd.set_mst_alloc_slots.header.sub_type = DMUB_CMD__DPIA_MST_ALLOC_SLOTS;
5758 
5759 	cmd.set_mst_alloc_slots.mst_slots_control.instance = dc->links[link_index]->ddc_hw_inst;
5760 	cmd.set_mst_alloc_slots.mst_slots_control.mst_alloc_slots = mst_alloc_slots;
5761 
5762 	if (!dc_wake_and_execute_dmub_cmd(dc->ctx, &cmd, DM_DMUB_WAIT_TYPE_WAIT_WITH_REPLY))
5763 		/* command is not processed by dmub */
5764 		return DC_ERROR_UNEXPECTED;
5765 
5766 	/* command processed by dmub, if ret_status is 1 */
5767 	if (cmd.set_config_access.header.ret_status != 1)
5768 		/* command processing error */
5769 		return DC_ERROR_UNEXPECTED;
5770 
5771 	/* command processed and we have a status of 2, mst not enabled in dpia */
5772 	if (cmd.set_mst_alloc_slots.mst_slots_control.immed_status == 2)
5773 		return DC_FAIL_UNSUPPORTED_1;
5774 
5775 	/* previously configured mst alloc and used slots did not match */
5776 	if (cmd.set_mst_alloc_slots.mst_slots_control.immed_status == 3) {
5777 		*mst_slots_in_use = cmd.set_mst_alloc_slots.mst_slots_control.mst_slots_in_use;
5778 		return DC_NOT_SUPPORTED;
5779 	}
5780 
5781 	return DC_OK;
5782 }
5783 
5784 /**
5785  * dc_process_dmub_dpia_set_tps_notification - Submits tps notification
5786  *
5787  * @dc: [in] dc structure
5788  * @link_index: [in] link index
5789  * @tps: [in] request tps
5790  *
5791  * Submits set_tps_notification command to dmub via inbox message
5792  */
dc_process_dmub_dpia_set_tps_notification(const struct dc * dc,uint32_t link_index,uint8_t tps)5793 void dc_process_dmub_dpia_set_tps_notification(const struct dc *dc, uint32_t link_index, uint8_t tps)
5794 {
5795 	union dmub_rb_cmd cmd = {0};
5796 
5797 	cmd.set_tps_notification.header.type = DMUB_CMD__DPIA;
5798 	cmd.set_tps_notification.header.sub_type = DMUB_CMD__DPIA_SET_TPS_NOTIFICATION;
5799 	cmd.set_tps_notification.tps_notification.instance = dc->links[link_index]->ddc_hw_inst;
5800 	cmd.set_tps_notification.tps_notification.tps = tps;
5801 
5802 	dc_wake_and_execute_dmub_cmd(dc->ctx, &cmd, DM_DMUB_WAIT_TYPE_WAIT);
5803 }
5804 
5805 /**
5806  * dc_process_dmub_dpia_hpd_int_enable - Submits DPIA DPD interruption
5807  *
5808  * @dc: [in] dc structure
5809  * @hpd_int_enable: [in] 1 for hpd int enable, 0 to disable
5810  *
5811  * Submits dpia hpd int enable command to dmub via inbox message
5812  */
dc_process_dmub_dpia_hpd_int_enable(const struct dc * dc,uint32_t hpd_int_enable)5813 void dc_process_dmub_dpia_hpd_int_enable(const struct dc *dc,
5814 				uint32_t hpd_int_enable)
5815 {
5816 	union dmub_rb_cmd cmd = {0};
5817 
5818 	cmd.dpia_hpd_int_enable.header.type = DMUB_CMD__DPIA_HPD_INT_ENABLE;
5819 	cmd.dpia_hpd_int_enable.enable = hpd_int_enable;
5820 
5821 	dc_wake_and_execute_dmub_cmd(dc->ctx, &cmd, DM_DMUB_WAIT_TYPE_WAIT);
5822 
5823 	DC_LOG_DEBUG("%s: hpd_int_enable(%d)\n", __func__, hpd_int_enable);
5824 }
5825 
5826 /**
5827  * dc_print_dmub_diagnostic_data - Print DMUB diagnostic data for debugging
5828  *
5829  * @dc: [in] dc structure
5830  *
5831  *
5832  */
dc_print_dmub_diagnostic_data(const struct dc * dc)5833 void dc_print_dmub_diagnostic_data(const struct dc *dc)
5834 {
5835 	dc_dmub_srv_log_diagnostic_data(dc->ctx->dmub_srv);
5836 }
5837 
5838 /**
5839  * dc_disable_accelerated_mode - disable accelerated mode
5840  * @dc: dc structure
5841  */
dc_disable_accelerated_mode(struct dc * dc)5842 void dc_disable_accelerated_mode(struct dc *dc)
5843 {
5844 	bios_set_scratch_acc_mode_change(dc->ctx->dc_bios, 0);
5845 }
5846 
5847 
5848 /**
5849  *  dc_notify_vsync_int_state - notifies vsync enable/disable state
5850  *  @dc: dc structure
5851  *  @stream: stream where vsync int state changed
5852  *  @enable: whether vsync is enabled or disabled
5853  *
5854  *  Called when vsync is enabled/disabled Will notify DMUB to start/stop ABM
5855  *  interrupts after steady state is reached.
5856  */
dc_notify_vsync_int_state(struct dc * dc,struct dc_stream_state * stream,bool enable)5857 void dc_notify_vsync_int_state(struct dc *dc, struct dc_stream_state *stream, bool enable)
5858 {
5859 	int i;
5860 	int edp_num;
5861 	struct pipe_ctx *pipe = NULL;
5862 	struct dc_link *link = stream->sink->link;
5863 	struct dc_link *edp_links[MAX_NUM_EDP];
5864 
5865 
5866 	if (link->psr_settings.psr_feature_enabled)
5867 		return;
5868 
5869 	if (link->replay_settings.replay_feature_enabled)
5870 		return;
5871 
5872 	/*find primary pipe associated with stream*/
5873 	for (i = 0; i < MAX_PIPES; i++) {
5874 		pipe = &dc->current_state->res_ctx.pipe_ctx[i];
5875 
5876 		if (pipe->stream == stream && pipe->stream_res.tg)
5877 			break;
5878 	}
5879 
5880 	if (i == MAX_PIPES) {
5881 		ASSERT(0);
5882 		return;
5883 	}
5884 
5885 	dc_get_edp_links(dc, edp_links, &edp_num);
5886 
5887 	/* Determine panel inst */
5888 	for (i = 0; i < edp_num; i++) {
5889 		if (edp_links[i] == link)
5890 			break;
5891 	}
5892 
5893 	if (i == edp_num) {
5894 		return;
5895 	}
5896 
5897 	if (pipe->stream_res.abm && pipe->stream_res.abm->funcs->set_abm_pause)
5898 		pipe->stream_res.abm->funcs->set_abm_pause(pipe->stream_res.abm, !enable, i, pipe->stream_res.tg->inst);
5899 }
5900 
5901 /*****************************************************************************
5902  *  dc_abm_save_restore() - Interface to DC for save+pause and restore+un-pause
5903  *                          ABM
5904  *  @dc: dc structure
5905  *	@stream: stream where vsync int state changed
5906  *  @pData: abm hw states
5907  *
5908  ****************************************************************************/
dc_abm_save_restore(struct dc * dc,struct dc_stream_state * stream,struct abm_save_restore * pData)5909 bool dc_abm_save_restore(
5910 		struct dc *dc,
5911 		struct dc_stream_state *stream,
5912 		struct abm_save_restore *pData)
5913 {
5914 	int i;
5915 	int edp_num;
5916 	struct pipe_ctx *pipe = NULL;
5917 	struct dc_link *link = stream->sink->link;
5918 	struct dc_link *edp_links[MAX_NUM_EDP];
5919 
5920 	if (link->replay_settings.replay_feature_enabled)
5921 		return false;
5922 
5923 	/*find primary pipe associated with stream*/
5924 	for (i = 0; i < MAX_PIPES; i++) {
5925 		pipe = &dc->current_state->res_ctx.pipe_ctx[i];
5926 
5927 		if (pipe->stream == stream && pipe->stream_res.tg)
5928 			break;
5929 	}
5930 
5931 	if (i == MAX_PIPES) {
5932 		ASSERT(0);
5933 		return false;
5934 	}
5935 
5936 	dc_get_edp_links(dc, edp_links, &edp_num);
5937 
5938 	/* Determine panel inst */
5939 	for (i = 0; i < edp_num; i++)
5940 		if (edp_links[i] == link)
5941 			break;
5942 
5943 	if (i == edp_num)
5944 		return false;
5945 
5946 	if (pipe->stream_res.abm &&
5947 		pipe->stream_res.abm->funcs->save_restore)
5948 		return pipe->stream_res.abm->funcs->save_restore(
5949 				pipe->stream_res.abm,
5950 				i,
5951 				pData);
5952 	return false;
5953 }
5954 
dc_query_current_properties(struct dc * dc,struct dc_current_properties * properties)5955 void dc_query_current_properties(struct dc *dc, struct dc_current_properties *properties)
5956 {
5957 	unsigned int i;
5958 	bool subvp_sw_cursor_req = false;
5959 
5960 	for (i = 0; i < dc->current_state->stream_count; i++) {
5961 		if (check_subvp_sw_cursor_fallback_req(dc, dc->current_state->streams[i])) {
5962 			subvp_sw_cursor_req = true;
5963 			break;
5964 		}
5965 	}
5966 	properties->cursor_size_limit = subvp_sw_cursor_req ? 64 : dc->caps.max_cursor_size;
5967 }
5968 
5969 /**
5970  * dc_set_edp_power() - DM controls eDP power to be ON/OFF
5971  *
5972  * Called when DM wants to power on/off eDP.
5973  *     Only work on links with flag skip_implict_edp_power_control is set.
5974  *
5975  * @dc: Current DC state
5976  * @edp_link: a link with eDP connector signal type
5977  * @powerOn: power on/off eDP
5978  *
5979  * Return: void
5980  */
dc_set_edp_power(const struct dc * dc,struct dc_link * edp_link,bool powerOn)5981 void dc_set_edp_power(const struct dc *dc, struct dc_link *edp_link,
5982 				 bool powerOn)
5983 {
5984 	if (edp_link->connector_signal != SIGNAL_TYPE_EDP)
5985 		return;
5986 
5987 	if (edp_link->skip_implict_edp_power_control == false)
5988 		return;
5989 
5990 	edp_link->dc->link_srv->edp_set_panel_power(edp_link, powerOn);
5991 }
5992 
5993 /*
5994  *****************************************************************************
5995  * dc_get_power_profile_for_dc_state() - extracts power profile from dc state
5996  *
5997  * Called when DM wants to make power policy decisions based on dc_state
5998  *
5999  *****************************************************************************
6000  */
dc_get_power_profile_for_dc_state(const struct dc_state * context)6001 struct dc_power_profile dc_get_power_profile_for_dc_state(const struct dc_state *context)
6002 {
6003 	struct dc_power_profile profile = { 0 };
6004 
6005 	profile.power_level += !context->bw_ctx.bw.dcn.clk.p_state_change_support;
6006 
6007 	return profile;
6008 }
6009 
6010 /*
6011  **********************************************************************************
6012  * dc_get_det_buffer_size_from_state() - extracts detile buffer size from dc state
6013  *
6014  * Called when DM wants to log detile buffer size from dc_state
6015  *
6016  **********************************************************************************
6017  */
dc_get_det_buffer_size_from_state(const struct dc_state * context)6018 unsigned int dc_get_det_buffer_size_from_state(const struct dc_state *context)
6019 {
6020 	struct dc *dc = context->clk_mgr->ctx->dc;
6021 
6022 	if (dc->res_pool->funcs->get_det_buffer_size)
6023 		return dc->res_pool->funcs->get_det_buffer_size(context);
6024 	else
6025 		return 0;
6026 }
6027