• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 "dc.h"
28 
29 #include "core_status.h"
30 #include "core_types.h"
31 #include "hw_sequencer.h"
32 #include "dce/dce_hwseq.h"
33 
34 #include "resource.h"
35 
36 #include "clk_mgr.h"
37 #include "clock_source.h"
38 #include "dc_bios_types.h"
39 
40 #include "bios_parser_interface.h"
41 #include "bios/bios_parser_helper.h"
42 #include "include/irq_service_interface.h"
43 #include "transform.h"
44 #include "dmcu.h"
45 #include "dpp.h"
46 #include "timing_generator.h"
47 #include "abm.h"
48 #include "virtual/virtual_link_encoder.h"
49 #include "hubp.h"
50 
51 #include "link_hwss.h"
52 #include "link_encoder.h"
53 #include "link_enc_cfg.h"
54 
55 #include "dc_link.h"
56 #include "dc_link_ddc.h"
57 #include "dm_helpers.h"
58 #include "mem_input.h"
59 
60 #include "dc_link_dp.h"
61 #include "dc_dmub_srv.h"
62 
63 #include "dsc.h"
64 
65 #include "vm_helper.h"
66 
67 #include "dce/dce_i2c.h"
68 
69 #include "dmub/dmub_srv.h"
70 
71 #include "i2caux_interface.h"
72 
73 #include "dce/dmub_psr.h"
74 
75 #include "dce/dmub_hw_lock_mgr.h"
76 
77 #include "dc_trace.h"
78 
79 #include "dce/dmub_outbox.h"
80 
81 #define CTX \
82 	dc->ctx
83 
84 #define DC_LOGGER \
85 	dc->ctx->logger
86 
87 static const char DC_BUILD_ID[] = "production-build";
88 
89 /**
90  * DOC: Overview
91  *
92  * DC is the OS-agnostic component of the amdgpu DC driver.
93  *
94  * DC maintains and validates a set of structs representing the state of the
95  * driver and writes that state to AMD hardware
96  *
97  * Main DC HW structs:
98  *
99  * struct dc - The central struct.  One per driver.  Created on driver load,
100  * destroyed on driver unload.
101  *
102  * struct dc_context - One per driver.
103  * Used as a backpointer by most other structs in dc.
104  *
105  * struct dc_link - One per connector (the physical DP, HDMI, miniDP, or eDP
106  * plugpoints).  Created on driver load, destroyed on driver unload.
107  *
108  * struct dc_sink - One per display.  Created on boot or hotplug.
109  * Destroyed on shutdown or hotunplug.  A dc_link can have a local sink
110  * (the display directly attached).  It may also have one or more remote
111  * sinks (in the Multi-Stream Transport case)
112  *
113  * struct resource_pool - One per driver.  Represents the hw blocks not in the
114  * main pipeline.  Not directly accessible by dm.
115  *
116  * Main dc state structs:
117  *
118  * These structs can be created and destroyed as needed.  There is a full set of
119  * these structs in dc->current_state representing the currently programmed state.
120  *
121  * struct dc_state - The global DC state to track global state information,
122  * such as bandwidth values.
123  *
124  * struct dc_stream_state - Represents the hw configuration for the pipeline from
125  * a framebuffer to a display.  Maps one-to-one with dc_sink.
126  *
127  * struct dc_plane_state - Represents a framebuffer.  Each stream has at least one,
128  * and may have more in the Multi-Plane Overlay case.
129  *
130  * struct resource_context - Represents the programmable state of everything in
131  * the resource_pool.  Not directly accessible by dm.
132  *
133  * struct pipe_ctx - A member of struct resource_context.  Represents the
134  * internal hardware pipeline components.  Each dc_plane_state has either
135  * one or two (in the pipe-split case).
136  */
137 
138 /* Private functions */
139 
elevate_update_type(enum surface_update_type * original,enum surface_update_type new)140 static inline void elevate_update_type(enum surface_update_type *original, enum surface_update_type new)
141 {
142 	if (new > *original)
143 		*original = new;
144 }
145 
destroy_links(struct dc * dc)146 static void destroy_links(struct dc *dc)
147 {
148 	uint32_t i;
149 
150 	for (i = 0; i < dc->link_count; i++) {
151 		if (NULL != dc->links[i])
152 			link_destroy(&dc->links[i]);
153 	}
154 }
155 
get_num_of_internal_disp(struct dc_link ** links,uint32_t num_links)156 static uint32_t get_num_of_internal_disp(struct dc_link **links, uint32_t num_links)
157 {
158 	int i;
159 	uint32_t count = 0;
160 
161 	for (i = 0; i < num_links; i++) {
162 		if (links[i]->connector_signal == SIGNAL_TYPE_EDP ||
163 				links[i]->is_internal_display)
164 			count++;
165 	}
166 
167 	return count;
168 }
169 
get_seamless_boot_stream_count(struct dc_state * ctx)170 static int get_seamless_boot_stream_count(struct dc_state *ctx)
171 {
172 	uint8_t i;
173 	uint8_t seamless_boot_stream_count = 0;
174 
175 	for (i = 0; i < ctx->stream_count; i++)
176 		if (ctx->streams[i]->apply_seamless_boot_optimization)
177 			seamless_boot_stream_count++;
178 
179 	return seamless_boot_stream_count;
180 }
181 
create_links(struct dc * dc,uint32_t num_virtual_links)182 static bool create_links(
183 		struct dc *dc,
184 		uint32_t num_virtual_links)
185 {
186 	int i;
187 	int connectors_num;
188 	struct dc_bios *bios = dc->ctx->dc_bios;
189 
190 	dc->link_count = 0;
191 
192 	connectors_num = bios->funcs->get_connectors_number(bios);
193 
194 	DC_LOG_DC("BIOS object table - number of connectors: %d", connectors_num);
195 
196 	if (connectors_num > ENUM_ID_COUNT) {
197 		dm_error(
198 			"DC: Number of connectors %d exceeds maximum of %d!\n",
199 			connectors_num,
200 			ENUM_ID_COUNT);
201 		return false;
202 	}
203 
204 	dm_output_to_console(
205 		"DC: %s: connectors_num: physical:%d, virtual:%d\n",
206 		__func__,
207 		connectors_num,
208 		num_virtual_links);
209 
210 	for (i = 0; i < connectors_num; i++) {
211 		struct link_init_data link_init_params = {0};
212 		struct dc_link *link;
213 
214 		DC_LOG_DC("BIOS object table - printing link object info for connector number: %d, link_index: %d", i, dc->link_count);
215 
216 		link_init_params.ctx = dc->ctx;
217 		/* next BIOS object table connector */
218 		link_init_params.connector_index = i;
219 		link_init_params.link_index = dc->link_count;
220 		link_init_params.dc = dc;
221 		link = link_create(&link_init_params);
222 
223 		if (link) {
224 			dc->links[dc->link_count] = link;
225 			link->dc = dc;
226 			++dc->link_count;
227 		}
228 	}
229 
230 	DC_LOG_DC("BIOS object table - end");
231 
232 	/* Create a link for each usb4 dpia port */
233 	for (i = 0; i < dc->res_pool->usb4_dpia_count; i++) {
234 		struct link_init_data link_init_params = {0};
235 		struct dc_link *link;
236 
237 		link_init_params.ctx = dc->ctx;
238 		link_init_params.connector_index = i;
239 		link_init_params.link_index = dc->link_count;
240 		link_init_params.dc = dc;
241 		link_init_params.is_dpia_link = true;
242 
243 		link = link_create(&link_init_params);
244 		if (link) {
245 			dc->links[dc->link_count] = link;
246 			link->dc = dc;
247 			++dc->link_count;
248 		}
249 	}
250 
251 	for (i = 0; i < num_virtual_links; i++) {
252 		struct dc_link *link = kzalloc(sizeof(*link), GFP_KERNEL);
253 		struct encoder_init_data enc_init = {0};
254 
255 		if (link == NULL) {
256 			BREAK_TO_DEBUGGER();
257 			goto failed_alloc;
258 		}
259 
260 		link->link_index = dc->link_count;
261 		dc->links[dc->link_count] = link;
262 		dc->link_count++;
263 
264 		link->ctx = dc->ctx;
265 		link->dc = dc;
266 		link->connector_signal = SIGNAL_TYPE_VIRTUAL;
267 		link->link_id.type = OBJECT_TYPE_CONNECTOR;
268 		link->link_id.id = CONNECTOR_ID_VIRTUAL;
269 		link->link_id.enum_id = ENUM_ID_1;
270 		link->link_enc = kzalloc(sizeof(*link->link_enc), GFP_KERNEL);
271 
272 		if (!link->link_enc) {
273 			BREAK_TO_DEBUGGER();
274 			goto failed_alloc;
275 		}
276 
277 		link->link_status.dpcd_caps = &link->dpcd_caps;
278 
279 		enc_init.ctx = dc->ctx;
280 		enc_init.channel = CHANNEL_ID_UNKNOWN;
281 		enc_init.hpd_source = HPD_SOURCEID_UNKNOWN;
282 		enc_init.transmitter = TRANSMITTER_UNKNOWN;
283 		enc_init.connector = link->link_id;
284 		enc_init.encoder.type = OBJECT_TYPE_ENCODER;
285 		enc_init.encoder.id = ENCODER_ID_INTERNAL_VIRTUAL;
286 		enc_init.encoder.enum_id = ENUM_ID_1;
287 		virtual_link_encoder_construct(link->link_enc, &enc_init);
288 	}
289 
290 	dc->caps.num_of_internal_disp = get_num_of_internal_disp(dc->links, dc->link_count);
291 
292 	return true;
293 
294 failed_alloc:
295 	return false;
296 }
297 
298 /* Create additional DIG link encoder objects if fewer than the platform
299  * supports were created during link construction. This can happen if the
300  * number of physical connectors is less than the number of DIGs.
301  */
create_link_encoders(struct dc * dc)302 static bool create_link_encoders(struct dc *dc)
303 {
304 	bool res = true;
305 	unsigned int num_usb4_dpia = dc->res_pool->res_cap->num_usb4_dpia;
306 	unsigned int num_dig_link_enc = dc->res_pool->res_cap->num_dig_link_enc;
307 	int i;
308 
309 	/* A platform without USB4 DPIA endpoints has a fixed mapping between DIG
310 	 * link encoders and physical display endpoints and does not require
311 	 * additional link encoder objects.
312 	 */
313 	if (num_usb4_dpia == 0)
314 		return res;
315 
316 	/* Create as many link encoder objects as the platform supports. DPIA
317 	 * endpoints can be programmably mapped to any DIG.
318 	 */
319 	if (num_dig_link_enc > dc->res_pool->dig_link_enc_count) {
320 		for (i = 0; i < num_dig_link_enc; i++) {
321 			struct link_encoder *link_enc = dc->res_pool->link_encoders[i];
322 
323 			if (!link_enc && dc->res_pool->funcs->link_enc_create_minimal) {
324 				link_enc = dc->res_pool->funcs->link_enc_create_minimal(dc->ctx,
325 						(enum engine_id)(ENGINE_ID_DIGA + i));
326 				if (link_enc) {
327 					dc->res_pool->link_encoders[i] = link_enc;
328 					dc->res_pool->dig_link_enc_count++;
329 				} else {
330 					res = false;
331 				}
332 			}
333 		}
334 	}
335 
336 	return res;
337 }
338 
339 /* Destroy any additional DIG link encoder objects created by
340  * create_link_encoders().
341  * NB: Must only be called after destroy_links().
342  */
destroy_link_encoders(struct dc * dc)343 static void destroy_link_encoders(struct dc *dc)
344 {
345 	unsigned int num_usb4_dpia;
346 	unsigned int num_dig_link_enc;
347 	int i;
348 
349 	if (!dc->res_pool)
350 		return;
351 
352 	num_usb4_dpia = dc->res_pool->res_cap->num_usb4_dpia;
353 	num_dig_link_enc = dc->res_pool->res_cap->num_dig_link_enc;
354 
355 	/* A platform without USB4 DPIA endpoints has a fixed mapping between DIG
356 	 * link encoders and physical display endpoints and does not require
357 	 * additional link encoder objects.
358 	 */
359 	if (num_usb4_dpia == 0)
360 		return;
361 
362 	for (i = 0; i < num_dig_link_enc; i++) {
363 		struct link_encoder *link_enc = dc->res_pool->link_encoders[i];
364 
365 		if (link_enc) {
366 			link_enc->funcs->destroy(&link_enc);
367 			dc->res_pool->link_encoders[i] = NULL;
368 			dc->res_pool->dig_link_enc_count--;
369 		}
370 	}
371 }
372 
dc_perf_trace_create(void)373 static struct dc_perf_trace *dc_perf_trace_create(void)
374 {
375 	return kzalloc(sizeof(struct dc_perf_trace), GFP_KERNEL);
376 }
377 
dc_perf_trace_destroy(struct dc_perf_trace ** perf_trace)378 static void dc_perf_trace_destroy(struct dc_perf_trace **perf_trace)
379 {
380 	kfree(*perf_trace);
381 	*perf_trace = NULL;
382 }
383 
384 /**
385  *  dc_stream_adjust_vmin_vmax - look up pipe context & update parts of DRR
386  *  @dc:     dc reference
387  *  @stream: Initial dc stream state
388  *  @adjust: Updated parameters for vertical_total_min and vertical_total_max
389  *
390  *  Looks up the pipe context of dc_stream_state and updates the
391  *  vertical_total_min and vertical_total_max of the DRR, Dynamic Refresh
392  *  Rate, which is a power-saving feature that targets reducing panel
393  *  refresh rate while the screen is static
394  *
395  *  Return: %true if the pipe context is found and adjusted;
396  *          %false if the pipe context is not found.
397  */
dc_stream_adjust_vmin_vmax(struct dc * dc,struct dc_stream_state * stream,struct dc_crtc_timing_adjust * adjust)398 bool dc_stream_adjust_vmin_vmax(struct dc *dc,
399 		struct dc_stream_state *stream,
400 		struct dc_crtc_timing_adjust *adjust)
401 {
402 	int i;
403 
404 	/*
405 	 * Don't adjust DRR while there's bandwidth optimizations pending to
406 	 * avoid conflicting with firmware updates.
407 	 */
408 	if (dc->ctx->dce_version > DCE_VERSION_MAX)
409 		if (dc->optimized_required || dc->wm_optimized_required)
410 			return false;
411 
412 	stream->adjust.v_total_max = adjust->v_total_max;
413 	stream->adjust.v_total_mid = adjust->v_total_mid;
414 	stream->adjust.v_total_mid_frame_num = adjust->v_total_mid_frame_num;
415 	stream->adjust.v_total_min = adjust->v_total_min;
416 
417 	for (i = 0; i < MAX_PIPES; i++) {
418 		struct pipe_ctx *pipe = &dc->current_state->res_ctx.pipe_ctx[i];
419 
420 		if (pipe->stream == stream && pipe->stream_res.tg) {
421 			dc->hwss.set_drr(&pipe,
422 					1,
423 					*adjust);
424 
425 			return true;
426 		}
427 	}
428 	return false;
429 }
430 
431 /**
432  * dc_stream_get_last_used_drr_vtotal - Looks up the pipe context of
433  * dc_stream_state and gets the last VTOTAL used by DRR (Dynamic Refresh Rate)
434  *
435  * @dc: [in] dc reference
436  * @stream: [in] Initial dc stream state
437  * @refresh_rate: [in] new refresh_rate
438  *
439  * Return: %true if the pipe context is found and there is an associated
440  *         timing_generator for the DC;
441  *         %false if the pipe context is not found or there is no
442  *         timing_generator for the DC.
443  */
dc_stream_get_last_used_drr_vtotal(struct dc * dc,struct dc_stream_state * stream,uint32_t * refresh_rate)444 bool dc_stream_get_last_used_drr_vtotal(struct dc *dc,
445 		struct dc_stream_state *stream,
446 		uint32_t *refresh_rate)
447 {
448 	bool status = false;
449 
450 	int i = 0;
451 
452 	for (i = 0; i < MAX_PIPES; i++) {
453 		struct pipe_ctx *pipe = &dc->current_state->res_ctx.pipe_ctx[i];
454 
455 		if (pipe->stream == stream && pipe->stream_res.tg) {
456 			/* Only execute if a function pointer has been defined for
457 			 * the DC version in question
458 			 */
459 			if (pipe->stream_res.tg->funcs->get_last_used_drr_vtotal) {
460 				pipe->stream_res.tg->funcs->get_last_used_drr_vtotal(pipe->stream_res.tg, refresh_rate);
461 
462 				status = true;
463 
464 				break;
465 			}
466 		}
467 	}
468 
469 	return status;
470 }
471 
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)472 bool dc_stream_get_crtc_position(struct dc *dc,
473 		struct dc_stream_state **streams, int num_streams,
474 		unsigned int *v_pos, unsigned int *nom_v_pos)
475 {
476 	/* TODO: Support multiple streams */
477 	const struct dc_stream_state *stream = streams[0];
478 	int i;
479 	bool ret = false;
480 	struct crtc_position position;
481 
482 	for (i = 0; i < MAX_PIPES; i++) {
483 		struct pipe_ctx *pipe =
484 				&dc->current_state->res_ctx.pipe_ctx[i];
485 
486 		if (pipe->stream == stream && pipe->stream_res.stream_enc) {
487 			dc->hwss.get_position(&pipe, 1, &position);
488 
489 			*v_pos = position.vertical_count;
490 			*nom_v_pos = position.nominal_vcount;
491 			ret = true;
492 		}
493 	}
494 	return ret;
495 }
496 
497 #if defined(CONFIG_DRM_AMD_SECURE_DISPLAY)
dc_stream_forward_dmcu_crc_window(struct dc * dc,struct dc_stream_state * stream,struct crc_params * crc_window)498 bool dc_stream_forward_dmcu_crc_window(struct dc *dc, struct dc_stream_state *stream,
499 			     struct crc_params *crc_window)
500 {
501 	int i;
502 	struct dmcu *dmcu = dc->res_pool->dmcu;
503 	struct pipe_ctx *pipe;
504 	struct crc_region tmp_win, *crc_win;
505 	struct otg_phy_mux mapping_tmp, *mux_mapping;
506 
507 	/*crc window can't be null*/
508 	if (!crc_window)
509 		return false;
510 
511 	if ((dmcu != NULL && dmcu->funcs->is_dmcu_initialized(dmcu))) {
512 		crc_win = &tmp_win;
513 		mux_mapping = &mapping_tmp;
514 		/*set crc window*/
515 		tmp_win.x_start = crc_window->windowa_x_start;
516 		tmp_win.y_start = crc_window->windowa_y_start;
517 		tmp_win.x_end = crc_window->windowa_x_end;
518 		tmp_win.y_end = crc_window->windowa_y_end;
519 
520 		for (i = 0; i < MAX_PIPES; i++) {
521 			pipe = &dc->current_state->res_ctx.pipe_ctx[i];
522 			if (pipe->stream == stream && !pipe->top_pipe && !pipe->prev_odm_pipe)
523 				break;
524 		}
525 
526 		/* Stream not found */
527 		if (i == MAX_PIPES)
528 			return false;
529 
530 
531 		/*set mux routing info*/
532 		mapping_tmp.phy_output_num = stream->link->link_enc_hw_inst;
533 		mapping_tmp.otg_output_num = pipe->stream_res.tg->inst;
534 
535 		dmcu->funcs->forward_crc_window(dmcu, crc_win, mux_mapping);
536 	} else {
537 		DC_LOG_DC("dmcu is not initialized");
538 		return false;
539 	}
540 
541 	return true;
542 }
543 
dc_stream_stop_dmcu_crc_win_update(struct dc * dc,struct dc_stream_state * stream)544 bool dc_stream_stop_dmcu_crc_win_update(struct dc *dc, struct dc_stream_state *stream)
545 {
546 	int i;
547 	struct dmcu *dmcu = dc->res_pool->dmcu;
548 	struct pipe_ctx *pipe;
549 	struct otg_phy_mux mapping_tmp, *mux_mapping;
550 
551 	if ((dmcu != NULL && dmcu->funcs->is_dmcu_initialized(dmcu))) {
552 		mux_mapping = &mapping_tmp;
553 
554 		for (i = 0; i < MAX_PIPES; i++) {
555 			pipe = &dc->current_state->res_ctx.pipe_ctx[i];
556 			if (pipe->stream == stream && !pipe->top_pipe && !pipe->prev_odm_pipe)
557 				break;
558 		}
559 
560 		/* Stream not found */
561 		if (i == MAX_PIPES)
562 			return false;
563 
564 
565 		/*set mux routing info*/
566 		mapping_tmp.phy_output_num = stream->link->link_enc_hw_inst;
567 		mapping_tmp.otg_output_num = pipe->stream_res.tg->inst;
568 
569 		dmcu->funcs->stop_crc_win_update(dmcu, mux_mapping);
570 	} else {
571 		DC_LOG_DC("dmcu is not initialized");
572 		return false;
573 	}
574 
575 	return true;
576 }
577 #endif
578 
579 /**
580  * dc_stream_configure_crc() - Configure CRC capture for the given stream.
581  * @dc: DC Object
582  * @stream: The stream to configure CRC on.
583  * @enable: Enable CRC if true, disable otherwise.
584  * @crc_window: CRC window (x/y start/end) information
585  * @continuous: Capture CRC on every frame if true. Otherwise, only capture
586  *              once.
587  *
588  * By default, only CRC0 is configured, and the entire frame is used to
589  * calculate the CRC.
590  *
591  * Return: %false if the stream is not found or CRC capture is not supported;
592  *         %true if the stream has been configured.
593  */
dc_stream_configure_crc(struct dc * dc,struct dc_stream_state * stream,struct crc_params * crc_window,bool enable,bool continuous)594 bool dc_stream_configure_crc(struct dc *dc, struct dc_stream_state *stream,
595 			     struct crc_params *crc_window, bool enable, bool continuous)
596 {
597 	int i;
598 	struct pipe_ctx *pipe;
599 	struct crc_params param;
600 	struct timing_generator *tg;
601 
602 	for (i = 0; i < MAX_PIPES; i++) {
603 		pipe = &dc->current_state->res_ctx.pipe_ctx[i];
604 		if (pipe->stream == stream && !pipe->top_pipe && !pipe->prev_odm_pipe)
605 			break;
606 	}
607 	/* Stream not found */
608 	if (i == MAX_PIPES)
609 		return false;
610 
611 	/* By default, capture the full frame */
612 	param.windowa_x_start = 0;
613 	param.windowa_y_start = 0;
614 	param.windowa_x_end = pipe->stream->timing.h_addressable;
615 	param.windowa_y_end = pipe->stream->timing.v_addressable;
616 	param.windowb_x_start = 0;
617 	param.windowb_y_start = 0;
618 	param.windowb_x_end = pipe->stream->timing.h_addressable;
619 	param.windowb_y_end = pipe->stream->timing.v_addressable;
620 
621 	if (crc_window) {
622 		param.windowa_x_start = crc_window->windowa_x_start;
623 		param.windowa_y_start = crc_window->windowa_y_start;
624 		param.windowa_x_end = crc_window->windowa_x_end;
625 		param.windowa_y_end = crc_window->windowa_y_end;
626 		param.windowb_x_start = crc_window->windowb_x_start;
627 		param.windowb_y_start = crc_window->windowb_y_start;
628 		param.windowb_x_end = crc_window->windowb_x_end;
629 		param.windowb_y_end = crc_window->windowb_y_end;
630 	}
631 
632 	param.dsc_mode = pipe->stream->timing.flags.DSC ? 1:0;
633 	param.odm_mode = pipe->next_odm_pipe ? 1:0;
634 
635 	/* Default to the union of both windows */
636 	param.selection = UNION_WINDOW_A_B;
637 	param.continuous_mode = continuous;
638 	param.enable = enable;
639 
640 	tg = pipe->stream_res.tg;
641 
642 	/* Only call if supported */
643 	if (tg->funcs->configure_crc)
644 		return tg->funcs->configure_crc(tg, &param);
645 	DC_LOG_WARNING("CRC capture not supported.");
646 	return false;
647 }
648 
649 /**
650  * dc_stream_get_crc() - Get CRC values for the given stream.
651  *
652  * @dc: DC object.
653  * @stream: The DC stream state of the stream to get CRCs from.
654  * @r_cr: CRC value for the red component.
655  * @g_y:  CRC value for the green component.
656  * @b_cb: CRC value for the blue component.
657  *
658  * dc_stream_configure_crc needs to be called beforehand to enable CRCs.
659  *
660  * Return:
661  * %false if stream is not found, or if CRCs are not enabled.
662  */
dc_stream_get_crc(struct dc * dc,struct dc_stream_state * stream,uint32_t * r_cr,uint32_t * g_y,uint32_t * b_cb)663 bool dc_stream_get_crc(struct dc *dc, struct dc_stream_state *stream,
664 		       uint32_t *r_cr, uint32_t *g_y, uint32_t *b_cb)
665 {
666 	int i;
667 	struct pipe_ctx *pipe;
668 	struct timing_generator *tg;
669 
670 	for (i = 0; i < MAX_PIPES; i++) {
671 		pipe = &dc->current_state->res_ctx.pipe_ctx[i];
672 		if (pipe->stream == stream)
673 			break;
674 	}
675 	/* Stream not found */
676 	if (i == MAX_PIPES)
677 		return false;
678 
679 	tg = pipe->stream_res.tg;
680 
681 	if (tg->funcs->get_crc)
682 		return tg->funcs->get_crc(tg, r_cr, g_y, b_cb);
683 	DC_LOG_WARNING("CRC capture not supported.");
684 	return false;
685 }
686 
dc_stream_set_dyn_expansion(struct dc * dc,struct dc_stream_state * stream,enum dc_dynamic_expansion option)687 void dc_stream_set_dyn_expansion(struct dc *dc, struct dc_stream_state *stream,
688 		enum dc_dynamic_expansion option)
689 {
690 	/* OPP FMT dyn expansion updates*/
691 	int i;
692 	struct pipe_ctx *pipe_ctx;
693 
694 	for (i = 0; i < MAX_PIPES; i++) {
695 		if (dc->current_state->res_ctx.pipe_ctx[i].stream
696 				== stream) {
697 			pipe_ctx = &dc->current_state->res_ctx.pipe_ctx[i];
698 			pipe_ctx->stream_res.opp->dyn_expansion = option;
699 			pipe_ctx->stream_res.opp->funcs->opp_set_dyn_expansion(
700 					pipe_ctx->stream_res.opp,
701 					COLOR_SPACE_YCBCR601,
702 					stream->timing.display_color_depth,
703 					stream->signal);
704 		}
705 	}
706 }
707 
dc_stream_set_dither_option(struct dc_stream_state * stream,enum dc_dither_option option)708 void dc_stream_set_dither_option(struct dc_stream_state *stream,
709 		enum dc_dither_option option)
710 {
711 	struct bit_depth_reduction_params params;
712 	struct dc_link *link = stream->link;
713 	struct pipe_ctx *pipes = NULL;
714 	int i;
715 
716 	for (i = 0; i < MAX_PIPES; i++) {
717 		if (link->dc->current_state->res_ctx.pipe_ctx[i].stream ==
718 				stream) {
719 			pipes = &link->dc->current_state->res_ctx.pipe_ctx[i];
720 			break;
721 		}
722 	}
723 
724 	if (!pipes)
725 		return;
726 	if (option > DITHER_OPTION_MAX)
727 		return;
728 
729 	stream->dither_option = option;
730 
731 	memset(&params, 0, sizeof(params));
732 	resource_build_bit_depth_reduction_params(stream, &params);
733 	stream->bit_depth_params = params;
734 
735 	if (pipes->plane_res.xfm &&
736 	    pipes->plane_res.xfm->funcs->transform_set_pixel_storage_depth) {
737 		pipes->plane_res.xfm->funcs->transform_set_pixel_storage_depth(
738 			pipes->plane_res.xfm,
739 			pipes->plane_res.scl_data.lb_params.depth,
740 			&stream->bit_depth_params);
741 	}
742 
743 	pipes->stream_res.opp->funcs->
744 		opp_program_bit_depth_reduction(pipes->stream_res.opp, &params);
745 }
746 
dc_stream_set_gamut_remap(struct dc * dc,const struct dc_stream_state * stream)747 bool dc_stream_set_gamut_remap(struct dc *dc, const struct dc_stream_state *stream)
748 {
749 	int i;
750 	bool ret = false;
751 	struct pipe_ctx *pipes;
752 
753 	for (i = 0; i < MAX_PIPES; i++) {
754 		if (dc->current_state->res_ctx.pipe_ctx[i].stream == stream) {
755 			pipes = &dc->current_state->res_ctx.pipe_ctx[i];
756 			dc->hwss.program_gamut_remap(pipes);
757 			ret = true;
758 		}
759 	}
760 
761 	return ret;
762 }
763 
dc_stream_program_csc_matrix(struct dc * dc,struct dc_stream_state * stream)764 bool dc_stream_program_csc_matrix(struct dc *dc, struct dc_stream_state *stream)
765 {
766 	int i;
767 	bool ret = false;
768 	struct pipe_ctx *pipes;
769 
770 	for (i = 0; i < MAX_PIPES; i++) {
771 		if (dc->current_state->res_ctx.pipe_ctx[i].stream
772 				== stream) {
773 
774 			pipes = &dc->current_state->res_ctx.pipe_ctx[i];
775 			dc->hwss.program_output_csc(dc,
776 					pipes,
777 					stream->output_color_space,
778 					stream->csc_color_matrix.matrix,
779 					pipes->stream_res.opp->inst);
780 			ret = true;
781 		}
782 	}
783 
784 	return ret;
785 }
786 
dc_stream_set_static_screen_params(struct dc * dc,struct dc_stream_state ** streams,int num_streams,const struct dc_static_screen_params * params)787 void dc_stream_set_static_screen_params(struct dc *dc,
788 		struct dc_stream_state **streams,
789 		int num_streams,
790 		const struct dc_static_screen_params *params)
791 {
792 	int i, j;
793 	struct pipe_ctx *pipes_affected[MAX_PIPES];
794 	int num_pipes_affected = 0;
795 
796 	for (i = 0; i < num_streams; i++) {
797 		struct dc_stream_state *stream = streams[i];
798 
799 		for (j = 0; j < MAX_PIPES; j++) {
800 			if (dc->current_state->res_ctx.pipe_ctx[j].stream
801 					== stream) {
802 				pipes_affected[num_pipes_affected++] =
803 						&dc->current_state->res_ctx.pipe_ctx[j];
804 			}
805 		}
806 	}
807 
808 	dc->hwss.set_static_screen_control(pipes_affected, num_pipes_affected, params);
809 }
810 
dc_destruct(struct dc * dc)811 static void dc_destruct(struct dc *dc)
812 {
813 	// reset link encoder assignment table on destruct
814 	if (dc->res_pool && dc->res_pool->funcs->link_encs_assign)
815 		link_enc_cfg_init(dc, dc->current_state);
816 
817 	if (dc->current_state) {
818 		dc_release_state(dc->current_state);
819 		dc->current_state = NULL;
820 	}
821 
822 	destroy_links(dc);
823 
824 	destroy_link_encoders(dc);
825 
826 	if (dc->clk_mgr) {
827 		dc_destroy_clk_mgr(dc->clk_mgr);
828 		dc->clk_mgr = NULL;
829 	}
830 
831 	dc_destroy_resource_pool(dc);
832 
833 	if (dc->ctx->gpio_service)
834 		dal_gpio_service_destroy(&dc->ctx->gpio_service);
835 
836 	if (dc->ctx->created_bios)
837 		dal_bios_parser_destroy(&dc->ctx->dc_bios);
838 
839 	dc_perf_trace_destroy(&dc->ctx->perf_trace);
840 
841 	kfree(dc->ctx);
842 	dc->ctx = NULL;
843 
844 	kfree(dc->bw_vbios);
845 	dc->bw_vbios = NULL;
846 
847 	kfree(dc->bw_dceip);
848 	dc->bw_dceip = NULL;
849 
850 	kfree(dc->dcn_soc);
851 	dc->dcn_soc = NULL;
852 
853 	kfree(dc->dcn_ip);
854 	dc->dcn_ip = NULL;
855 
856 	kfree(dc->vm_helper);
857 	dc->vm_helper = NULL;
858 
859 }
860 
dc_construct_ctx(struct dc * dc,const struct dc_init_data * init_params)861 static bool dc_construct_ctx(struct dc *dc,
862 		const struct dc_init_data *init_params)
863 {
864 	struct dc_context *dc_ctx;
865 	enum dce_version dc_version = DCE_VERSION_UNKNOWN;
866 
867 	dc_ctx = kzalloc(sizeof(*dc_ctx), GFP_KERNEL);
868 	if (!dc_ctx)
869 		return false;
870 
871 	dc_ctx->cgs_device = init_params->cgs_device;
872 	dc_ctx->driver_context = init_params->driver;
873 	dc_ctx->dc = dc;
874 	dc_ctx->asic_id = init_params->asic_id;
875 	dc_ctx->dc_sink_id_count = 0;
876 	dc_ctx->dc_stream_id_count = 0;
877 	dc_ctx->dce_environment = init_params->dce_environment;
878 	dc_ctx->dcn_reg_offsets = init_params->dcn_reg_offsets;
879 	dc_ctx->nbio_reg_offsets = init_params->nbio_reg_offsets;
880 
881 	/* Create logger */
882 
883 	dc_version = resource_parse_asic_id(init_params->asic_id);
884 	dc_ctx->dce_version = dc_version;
885 
886 	dc_ctx->perf_trace = dc_perf_trace_create();
887 	if (!dc_ctx->perf_trace) {
888 		kfree(dc_ctx);
889 		ASSERT_CRITICAL(false);
890 		return false;
891 	}
892 
893 	dc->ctx = dc_ctx;
894 
895 	return true;
896 }
897 
dc_construct(struct dc * dc,const struct dc_init_data * init_params)898 static bool dc_construct(struct dc *dc,
899 		const struct dc_init_data *init_params)
900 {
901 	struct dc_context *dc_ctx;
902 	struct bw_calcs_dceip *dc_dceip;
903 	struct bw_calcs_vbios *dc_vbios;
904 	struct dcn_soc_bounding_box *dcn_soc;
905 	struct dcn_ip_params *dcn_ip;
906 
907 	dc->config = init_params->flags;
908 
909 	// Allocate memory for the vm_helper
910 	dc->vm_helper = kzalloc(sizeof(struct vm_helper), GFP_KERNEL);
911 	if (!dc->vm_helper) {
912 		dm_error("%s: failed to create dc->vm_helper\n", __func__);
913 		goto fail;
914 	}
915 
916 	memcpy(&dc->bb_overrides, &init_params->bb_overrides, sizeof(dc->bb_overrides));
917 
918 	dc_dceip = kzalloc(sizeof(*dc_dceip), GFP_KERNEL);
919 	if (!dc_dceip) {
920 		dm_error("%s: failed to create dceip\n", __func__);
921 		goto fail;
922 	}
923 
924 	dc->bw_dceip = dc_dceip;
925 
926 	dc_vbios = kzalloc(sizeof(*dc_vbios), GFP_KERNEL);
927 	if (!dc_vbios) {
928 		dm_error("%s: failed to create vbios\n", __func__);
929 		goto fail;
930 	}
931 
932 	dc->bw_vbios = dc_vbios;
933 	dcn_soc = kzalloc(sizeof(*dcn_soc), GFP_KERNEL);
934 	if (!dcn_soc) {
935 		dm_error("%s: failed to create dcn_soc\n", __func__);
936 		goto fail;
937 	}
938 
939 	dc->dcn_soc = dcn_soc;
940 
941 	dcn_ip = kzalloc(sizeof(*dcn_ip), GFP_KERNEL);
942 	if (!dcn_ip) {
943 		dm_error("%s: failed to create dcn_ip\n", __func__);
944 		goto fail;
945 	}
946 
947 	dc->dcn_ip = dcn_ip;
948 
949 	if (!dc_construct_ctx(dc, init_params)) {
950 		dm_error("%s: failed to create ctx\n", __func__);
951 		goto fail;
952 	}
953 
954         dc_ctx = dc->ctx;
955 
956 	/* Resource should construct all asic specific resources.
957 	 * This should be the only place where we need to parse the asic id
958 	 */
959 	if (init_params->vbios_override)
960 		dc_ctx->dc_bios = init_params->vbios_override;
961 	else {
962 		/* Create BIOS parser */
963 		struct bp_init_data bp_init_data;
964 
965 		bp_init_data.ctx = dc_ctx;
966 		bp_init_data.bios = init_params->asic_id.atombios_base_address;
967 
968 		dc_ctx->dc_bios = dal_bios_parser_create(
969 				&bp_init_data, dc_ctx->dce_version);
970 
971 		if (!dc_ctx->dc_bios) {
972 			ASSERT_CRITICAL(false);
973 			goto fail;
974 		}
975 
976 		dc_ctx->created_bios = true;
977 	}
978 
979 	dc->vendor_signature = init_params->vendor_signature;
980 
981 	/* Create GPIO service */
982 	dc_ctx->gpio_service = dal_gpio_service_create(
983 			dc_ctx->dce_version,
984 			dc_ctx->dce_environment,
985 			dc_ctx);
986 
987 	if (!dc_ctx->gpio_service) {
988 		ASSERT_CRITICAL(false);
989 		goto fail;
990 	}
991 
992 	dc->res_pool = dc_create_resource_pool(dc, init_params, dc_ctx->dce_version);
993 	if (!dc->res_pool)
994 		goto fail;
995 
996 	/* set i2c speed if not done by the respective dcnxxx__resource.c */
997 	if (dc->caps.i2c_speed_in_khz_hdcp == 0)
998 		dc->caps.i2c_speed_in_khz_hdcp = dc->caps.i2c_speed_in_khz;
999 	if (dc->caps.max_optimizable_video_width == 0)
1000 		dc->caps.max_optimizable_video_width = 5120;
1001 	dc->clk_mgr = dc_clk_mgr_create(dc->ctx, dc->res_pool->pp_smu, dc->res_pool->dccg);
1002 	if (!dc->clk_mgr)
1003 		goto fail;
1004 #ifdef CONFIG_DRM_AMD_DC_DCN
1005 	dc->clk_mgr->force_smu_not_present = init_params->force_smu_not_present;
1006 
1007 	if (dc->res_pool->funcs->update_bw_bounding_box) {
1008 		DC_FP_START();
1009 		dc->res_pool->funcs->update_bw_bounding_box(dc, dc->clk_mgr->bw_params);
1010 		DC_FP_END();
1011 	}
1012 #endif
1013 
1014 	/* Creation of current_state must occur after dc->dml
1015 	 * is initialized in dc_create_resource_pool because
1016 	 * on creation it copies the contents of dc->dml
1017 	 */
1018 
1019 	dc->current_state = dc_create_state(dc);
1020 
1021 	if (!dc->current_state) {
1022 		dm_error("%s: failed to create validate ctx\n", __func__);
1023 		goto fail;
1024 	}
1025 
1026 	if (!create_links(dc, init_params->num_virtual_links))
1027 		goto fail;
1028 
1029 	/* Create additional DIG link encoder objects if fewer than the platform
1030 	 * supports were created during link construction.
1031 	 */
1032 	if (!create_link_encoders(dc))
1033 		goto fail;
1034 
1035 	dc_resource_state_construct(dc, dc->current_state);
1036 
1037 	return true;
1038 
1039 fail:
1040 	return false;
1041 }
1042 
disable_all_writeback_pipes_for_stream(const struct dc * dc,struct dc_stream_state * stream,struct dc_state * context)1043 static void disable_all_writeback_pipes_for_stream(
1044 		const struct dc *dc,
1045 		struct dc_stream_state *stream,
1046 		struct dc_state *context)
1047 {
1048 	int i;
1049 
1050 	for (i = 0; i < stream->num_wb_info; i++)
1051 		stream->writeback_info[i].wb_enabled = false;
1052 }
1053 
apply_ctx_interdependent_lock(struct dc * dc,struct dc_state * context,struct dc_stream_state * stream,bool lock)1054 static void apply_ctx_interdependent_lock(struct dc *dc, struct dc_state *context,
1055 					  struct dc_stream_state *stream, bool lock)
1056 {
1057 	int i;
1058 
1059 	/* Checks if interdependent update function pointer is NULL or not, takes care of DCE110 case */
1060 	if (dc->hwss.interdependent_update_lock)
1061 		dc->hwss.interdependent_update_lock(dc, context, lock);
1062 	else {
1063 		for (i = 0; i < dc->res_pool->pipe_count; i++) {
1064 			struct pipe_ctx *pipe_ctx = &context->res_ctx.pipe_ctx[i];
1065 			struct pipe_ctx *old_pipe_ctx = &dc->current_state->res_ctx.pipe_ctx[i];
1066 
1067 			// Copied conditions that were previously in dce110_apply_ctx_for_surface
1068 			if (stream == pipe_ctx->stream) {
1069 				if (!pipe_ctx->top_pipe &&
1070 					(pipe_ctx->plane_state || old_pipe_ctx->plane_state))
1071 					dc->hwss.pipe_control_lock(dc, pipe_ctx, lock);
1072 			}
1073 		}
1074 	}
1075 }
1076 
disable_dangling_plane(struct dc * dc,struct dc_state * context)1077 static void disable_dangling_plane(struct dc *dc, struct dc_state *context)
1078 {
1079 	int i, j;
1080 	struct dc_state *dangling_context = dc_create_state(dc);
1081 	struct dc_state *current_ctx;
1082 	struct pipe_ctx *pipe;
1083 	struct timing_generator *tg;
1084 
1085 	if (dangling_context == NULL)
1086 		return;
1087 
1088 	dc_resource_state_copy_construct(dc->current_state, dangling_context);
1089 
1090 	for (i = 0; i < dc->res_pool->pipe_count; i++) {
1091 		struct dc_stream_state *old_stream =
1092 				dc->current_state->res_ctx.pipe_ctx[i].stream;
1093 		bool should_disable = true;
1094 		bool pipe_split_change = false;
1095 
1096 		if ((context->res_ctx.pipe_ctx[i].top_pipe) &&
1097 			(dc->current_state->res_ctx.pipe_ctx[i].top_pipe))
1098 			pipe_split_change = context->res_ctx.pipe_ctx[i].top_pipe->pipe_idx !=
1099 				dc->current_state->res_ctx.pipe_ctx[i].top_pipe->pipe_idx;
1100 		else
1101 			pipe_split_change = context->res_ctx.pipe_ctx[i].top_pipe !=
1102 				dc->current_state->res_ctx.pipe_ctx[i].top_pipe;
1103 
1104 		for (j = 0; j < context->stream_count; j++) {
1105 			if (old_stream == context->streams[j]) {
1106 				should_disable = false;
1107 				break;
1108 			}
1109 		}
1110 		if (!should_disable && pipe_split_change &&
1111 				dc->current_state->stream_count != context->stream_count)
1112 			should_disable = true;
1113 
1114 		if (old_stream && !dc->current_state->res_ctx.pipe_ctx[i].top_pipe &&
1115 				!dc->current_state->res_ctx.pipe_ctx[i].prev_odm_pipe) {
1116 			struct pipe_ctx *old_pipe, *new_pipe;
1117 
1118 			old_pipe = &dc->current_state->res_ctx.pipe_ctx[i];
1119 			new_pipe = &context->res_ctx.pipe_ctx[i];
1120 
1121 			if (old_pipe->plane_state && !new_pipe->plane_state)
1122 				should_disable = true;
1123 		}
1124 
1125 		if (should_disable && old_stream) {
1126 			pipe = &dc->current_state->res_ctx.pipe_ctx[i];
1127 			tg = pipe->stream_res.tg;
1128 			/* When disabling plane for a phantom pipe, we must turn on the
1129 			 * phantom OTG so the disable programming gets the double buffer
1130 			 * update. Otherwise the pipe will be left in a partially disabled
1131 			 * state that can result in underflow or hang when enabling it
1132 			 * again for different use.
1133 			 */
1134 			if (old_stream->mall_stream_config.type == SUBVP_PHANTOM) {
1135 				if (tg->funcs->enable_crtc)
1136 					tg->funcs->enable_crtc(tg);
1137 			}
1138 			dc_rem_all_planes_for_stream(dc, old_stream, dangling_context);
1139 			disable_all_writeback_pipes_for_stream(dc, old_stream, dangling_context);
1140 
1141 			if (dc->hwss.apply_ctx_for_surface) {
1142 				apply_ctx_interdependent_lock(dc, dc->current_state, old_stream, true);
1143 				dc->hwss.apply_ctx_for_surface(dc, old_stream, 0, dangling_context);
1144 				apply_ctx_interdependent_lock(dc, dc->current_state, old_stream, false);
1145 				dc->hwss.post_unlock_program_front_end(dc, dangling_context);
1146 			}
1147 			if (dc->hwss.program_front_end_for_ctx) {
1148 				dc->hwss.interdependent_update_lock(dc, dc->current_state, true);
1149 				dc->hwss.program_front_end_for_ctx(dc, dangling_context);
1150 				dc->hwss.interdependent_update_lock(dc, dc->current_state, false);
1151 				dc->hwss.post_unlock_program_front_end(dc, dangling_context);
1152 			}
1153 			/* We need to put the phantom OTG back into it's default (disabled) state or we
1154 			 * can get corruption when transition from one SubVP config to a different one.
1155 			 * The OTG is set to disable on falling edge of VUPDATE so the plane disable
1156 			 * will still get it's double buffer update.
1157 			 */
1158 			if (old_stream->mall_stream_config.type == SUBVP_PHANTOM) {
1159 				if (tg->funcs->disable_phantom_crtc)
1160 					tg->funcs->disable_phantom_crtc(tg);
1161 			}
1162 		}
1163 	}
1164 
1165 	current_ctx = dc->current_state;
1166 	dc->current_state = dangling_context;
1167 	dc_release_state(current_ctx);
1168 }
1169 
disable_vbios_mode_if_required(struct dc * dc,struct dc_state * context)1170 static void disable_vbios_mode_if_required(
1171 		struct dc *dc,
1172 		struct dc_state *context)
1173 {
1174 	unsigned int i, j;
1175 
1176 	/* check if timing_changed, disable stream*/
1177 	for (i = 0; i < dc->res_pool->pipe_count; i++) {
1178 		struct dc_stream_state *stream = NULL;
1179 		struct dc_link *link = NULL;
1180 		struct pipe_ctx *pipe = NULL;
1181 
1182 		pipe = &context->res_ctx.pipe_ctx[i];
1183 		stream = pipe->stream;
1184 		if (stream == NULL)
1185 			continue;
1186 
1187 		if (stream->apply_seamless_boot_optimization)
1188 			continue;
1189 
1190 		// only looking for first odm pipe
1191 		if (pipe->prev_odm_pipe)
1192 			continue;
1193 
1194 		if (stream->link->local_sink &&
1195 			stream->link->local_sink->sink_signal == SIGNAL_TYPE_EDP) {
1196 			link = stream->link;
1197 		}
1198 
1199 		if (link != NULL && link->link_enc->funcs->is_dig_enabled(link->link_enc)) {
1200 			unsigned int enc_inst, tg_inst = 0;
1201 			unsigned int pix_clk_100hz;
1202 
1203 			enc_inst = link->link_enc->funcs->get_dig_frontend(link->link_enc);
1204 			if (enc_inst != ENGINE_ID_UNKNOWN) {
1205 				for (j = 0; j < dc->res_pool->stream_enc_count; j++) {
1206 					if (dc->res_pool->stream_enc[j]->id == enc_inst) {
1207 						tg_inst = dc->res_pool->stream_enc[j]->funcs->dig_source_otg(
1208 							dc->res_pool->stream_enc[j]);
1209 						break;
1210 					}
1211 				}
1212 
1213 				dc->res_pool->dp_clock_source->funcs->get_pixel_clk_frequency_100hz(
1214 					dc->res_pool->dp_clock_source,
1215 					tg_inst, &pix_clk_100hz);
1216 
1217 				if (link->link_status.link_active) {
1218 					uint32_t requested_pix_clk_100hz =
1219 						pipe->stream_res.pix_clk_params.requested_pix_clk_100hz;
1220 
1221 					if (pix_clk_100hz != requested_pix_clk_100hz) {
1222 						core_link_disable_stream(pipe);
1223 						pipe->stream->dpms_off = false;
1224 					}
1225 				}
1226 			}
1227 		}
1228 	}
1229 }
1230 
wait_for_no_pipes_pending(struct dc * dc,struct dc_state * context)1231 static void wait_for_no_pipes_pending(struct dc *dc, struct dc_state *context)
1232 {
1233 	int i;
1234 	PERF_TRACE();
1235 	for (i = 0; i < MAX_PIPES; i++) {
1236 		int count = 0;
1237 		struct pipe_ctx *pipe = &context->res_ctx.pipe_ctx[i];
1238 
1239 		if (!pipe->plane_state || pipe->stream->mall_stream_config.type == SUBVP_PHANTOM)
1240 			continue;
1241 
1242 		/* Timeout 100 ms */
1243 		while (count < 100000) {
1244 			/* Must set to false to start with, due to OR in update function */
1245 			pipe->plane_state->status.is_flip_pending = false;
1246 			dc->hwss.update_pending_status(pipe);
1247 			if (!pipe->plane_state->status.is_flip_pending)
1248 				break;
1249 			udelay(1);
1250 			count++;
1251 		}
1252 		ASSERT(!pipe->plane_state->status.is_flip_pending);
1253 	}
1254 	PERF_TRACE();
1255 }
1256 
1257 /* Public functions */
1258 
dc_create(const struct dc_init_data * init_params)1259 struct dc *dc_create(const struct dc_init_data *init_params)
1260 {
1261 	struct dc *dc = kzalloc(sizeof(*dc), GFP_KERNEL);
1262 	unsigned int full_pipe_count;
1263 
1264 	if (!dc)
1265 		return NULL;
1266 
1267 	if (init_params->dce_environment == DCE_ENV_VIRTUAL_HW) {
1268 		if (!dc_construct_ctx(dc, init_params))
1269 			goto destruct_dc;
1270 	} else {
1271 		if (!dc_construct(dc, init_params))
1272 			goto destruct_dc;
1273 
1274 		full_pipe_count = dc->res_pool->pipe_count;
1275 		if (dc->res_pool->underlay_pipe_index != NO_UNDERLAY_PIPE)
1276 			full_pipe_count--;
1277 		dc->caps.max_streams = min(
1278 				full_pipe_count,
1279 				dc->res_pool->stream_enc_count);
1280 
1281 		dc->caps.max_links = dc->link_count;
1282 		dc->caps.max_audios = dc->res_pool->audio_count;
1283 		dc->caps.linear_pitch_alignment = 64;
1284 
1285 		dc->caps.max_dp_protocol_version = DP_VERSION_1_4;
1286 
1287 		dc->caps.max_otg_num = dc->res_pool->res_cap->num_timing_generator;
1288 
1289 		if (dc->res_pool->dmcu != NULL)
1290 			dc->versions.dmcu_version = dc->res_pool->dmcu->dmcu_version;
1291 	}
1292 
1293 	dc->dcn_reg_offsets = init_params->dcn_reg_offsets;
1294 	dc->nbio_reg_offsets = init_params->nbio_reg_offsets;
1295 
1296 	/* Populate versioning information */
1297 	dc->versions.dc_ver = DC_VER;
1298 
1299 	dc->build_id = DC_BUILD_ID;
1300 
1301 	DC_LOG_DC("Display Core initialized\n");
1302 
1303 
1304 
1305 	return dc;
1306 
1307 destruct_dc:
1308 	dc_destruct(dc);
1309 	kfree(dc);
1310 	return NULL;
1311 }
1312 
detect_edp_presence(struct dc * dc)1313 static void detect_edp_presence(struct dc *dc)
1314 {
1315 	struct dc_link *edp_links[MAX_NUM_EDP];
1316 	struct dc_link *edp_link = NULL;
1317 	enum dc_connection_type type;
1318 	int i;
1319 	int edp_num;
1320 
1321 	get_edp_links(dc, edp_links, &edp_num);
1322 	if (!edp_num)
1323 		return;
1324 
1325 	for (i = 0; i < edp_num; i++) {
1326 		edp_link = edp_links[i];
1327 		if (dc->config.edp_not_connected) {
1328 			edp_link->edp_sink_present = false;
1329 		} else {
1330 			dc_link_detect_sink(edp_link, &type);
1331 			edp_link->edp_sink_present = (type != dc_connection_none);
1332 		}
1333 	}
1334 }
1335 
dc_hardware_init(struct dc * dc)1336 void dc_hardware_init(struct dc *dc)
1337 {
1338 
1339 	detect_edp_presence(dc);
1340 	if (dc->ctx->dce_environment != DCE_ENV_VIRTUAL_HW)
1341 		dc->hwss.init_hw(dc);
1342 }
1343 
dc_init_callbacks(struct dc * dc,const struct dc_callback_init * init_params)1344 void dc_init_callbacks(struct dc *dc,
1345 		const struct dc_callback_init *init_params)
1346 {
1347 #ifdef CONFIG_DRM_AMD_DC_HDCP
1348 	dc->ctx->cp_psp = init_params->cp_psp;
1349 #endif
1350 }
1351 
dc_deinit_callbacks(struct dc * dc)1352 void dc_deinit_callbacks(struct dc *dc)
1353 {
1354 #ifdef CONFIG_DRM_AMD_DC_HDCP
1355 	memset(&dc->ctx->cp_psp, 0, sizeof(dc->ctx->cp_psp));
1356 #endif
1357 }
1358 
dc_destroy(struct dc ** dc)1359 void dc_destroy(struct dc **dc)
1360 {
1361 	dc_destruct(*dc);
1362 	kfree(*dc);
1363 	*dc = NULL;
1364 }
1365 
enable_timing_multisync(struct dc * dc,struct dc_state * ctx)1366 static void enable_timing_multisync(
1367 		struct dc *dc,
1368 		struct dc_state *ctx)
1369 {
1370 	int i, multisync_count = 0;
1371 	int pipe_count = dc->res_pool->pipe_count;
1372 	struct pipe_ctx *multisync_pipes[MAX_PIPES] = { NULL };
1373 
1374 	for (i = 0; i < pipe_count; i++) {
1375 		if (!ctx->res_ctx.pipe_ctx[i].stream ||
1376 				!ctx->res_ctx.pipe_ctx[i].stream->triggered_crtc_reset.enabled)
1377 			continue;
1378 		if (ctx->res_ctx.pipe_ctx[i].stream == ctx->res_ctx.pipe_ctx[i].stream->triggered_crtc_reset.event_source)
1379 			continue;
1380 		multisync_pipes[multisync_count] = &ctx->res_ctx.pipe_ctx[i];
1381 		multisync_count++;
1382 	}
1383 
1384 	if (multisync_count > 0) {
1385 		dc->hwss.enable_per_frame_crtc_position_reset(
1386 			dc, multisync_count, multisync_pipes);
1387 	}
1388 }
1389 
program_timing_sync(struct dc * dc,struct dc_state * ctx)1390 static void program_timing_sync(
1391 		struct dc *dc,
1392 		struct dc_state *ctx)
1393 {
1394 	int i, j, k;
1395 	int group_index = 0;
1396 	int num_group = 0;
1397 	int pipe_count = dc->res_pool->pipe_count;
1398 	struct pipe_ctx *unsynced_pipes[MAX_PIPES] = { NULL };
1399 
1400 	for (i = 0; i < pipe_count; i++) {
1401 		if (!ctx->res_ctx.pipe_ctx[i].stream
1402 				|| ctx->res_ctx.pipe_ctx[i].top_pipe
1403 				|| ctx->res_ctx.pipe_ctx[i].prev_odm_pipe)
1404 			continue;
1405 
1406 		unsynced_pipes[i] = &ctx->res_ctx.pipe_ctx[i];
1407 	}
1408 
1409 	for (i = 0; i < pipe_count; i++) {
1410 		int group_size = 1;
1411 		enum timing_synchronization_type sync_type = NOT_SYNCHRONIZABLE;
1412 		struct pipe_ctx *pipe_set[MAX_PIPES];
1413 
1414 		if (!unsynced_pipes[i])
1415 			continue;
1416 
1417 		pipe_set[0] = unsynced_pipes[i];
1418 		unsynced_pipes[i] = NULL;
1419 
1420 		/* Add tg to the set, search rest of the tg's for ones with
1421 		 * same timing, add all tgs with same timing to the group
1422 		 */
1423 		for (j = i + 1; j < pipe_count; j++) {
1424 			if (!unsynced_pipes[j])
1425 				continue;
1426 			if (sync_type != TIMING_SYNCHRONIZABLE &&
1427 				dc->hwss.enable_vblanks_synchronization &&
1428 				unsynced_pipes[j]->stream_res.tg->funcs->align_vblanks &&
1429 				resource_are_vblanks_synchronizable(
1430 					unsynced_pipes[j]->stream,
1431 					pipe_set[0]->stream)) {
1432 				sync_type = VBLANK_SYNCHRONIZABLE;
1433 				pipe_set[group_size] = unsynced_pipes[j];
1434 				unsynced_pipes[j] = NULL;
1435 				group_size++;
1436 			} else
1437 			if (sync_type != VBLANK_SYNCHRONIZABLE &&
1438 				resource_are_streams_timing_synchronizable(
1439 					unsynced_pipes[j]->stream,
1440 					pipe_set[0]->stream)) {
1441 				sync_type = TIMING_SYNCHRONIZABLE;
1442 				pipe_set[group_size] = unsynced_pipes[j];
1443 				unsynced_pipes[j] = NULL;
1444 				group_size++;
1445 			}
1446 		}
1447 
1448 		/* set first unblanked pipe as master */
1449 		for (j = 0; j < group_size; j++) {
1450 			bool is_blanked;
1451 
1452 			if (pipe_set[j]->stream_res.opp->funcs->dpg_is_blanked)
1453 				is_blanked =
1454 					pipe_set[j]->stream_res.opp->funcs->dpg_is_blanked(pipe_set[j]->stream_res.opp);
1455 			else
1456 				is_blanked =
1457 					pipe_set[j]->stream_res.tg->funcs->is_blanked(pipe_set[j]->stream_res.tg);
1458 			if (!is_blanked) {
1459 				if (j == 0)
1460 					break;
1461 
1462 				swap(pipe_set[0], pipe_set[j]);
1463 				break;
1464 			}
1465 		}
1466 
1467 		for (k = 0; k < group_size; k++) {
1468 			struct dc_stream_status *status = dc_stream_get_status_from_state(ctx, pipe_set[k]->stream);
1469 
1470 			status->timing_sync_info.group_id = num_group;
1471 			status->timing_sync_info.group_size = group_size;
1472 			if (k == 0)
1473 				status->timing_sync_info.master = true;
1474 			else
1475 				status->timing_sync_info.master = false;
1476 
1477 		}
1478 
1479 		/* remove any other pipes that are already been synced */
1480 		if (dc->config.use_pipe_ctx_sync_logic) {
1481 			/* check pipe's syncd to decide which pipe to be removed */
1482 			for (j = 1; j < group_size; j++) {
1483 				if (pipe_set[j]->pipe_idx_syncd == pipe_set[0]->pipe_idx_syncd) {
1484 					group_size--;
1485 					pipe_set[j] = pipe_set[group_size];
1486 					j--;
1487 				} else
1488 					/* link slave pipe's syncd with master pipe */
1489 					pipe_set[j]->pipe_idx_syncd = pipe_set[0]->pipe_idx_syncd;
1490 			}
1491 		} else {
1492 			for (j = j + 1; j < group_size; j++) {
1493 				bool is_blanked;
1494 
1495 				if (pipe_set[j]->stream_res.opp->funcs->dpg_is_blanked)
1496 					is_blanked =
1497 						pipe_set[j]->stream_res.opp->funcs->dpg_is_blanked(pipe_set[j]->stream_res.opp);
1498 				else
1499 					is_blanked =
1500 						pipe_set[j]->stream_res.tg->funcs->is_blanked(pipe_set[j]->stream_res.tg);
1501 				if (!is_blanked) {
1502 					group_size--;
1503 					pipe_set[j] = pipe_set[group_size];
1504 					j--;
1505 				}
1506 			}
1507 		}
1508 
1509 		if (group_size > 1) {
1510 			if (sync_type == TIMING_SYNCHRONIZABLE) {
1511 				dc->hwss.enable_timing_synchronization(
1512 					dc, group_index, group_size, pipe_set);
1513 			} else
1514 				if (sync_type == VBLANK_SYNCHRONIZABLE) {
1515 				dc->hwss.enable_vblanks_synchronization(
1516 					dc, group_index, group_size, pipe_set);
1517 				}
1518 			group_index++;
1519 		}
1520 		num_group++;
1521 	}
1522 }
1523 
streams_changed(struct dc * dc,struct dc_stream_state * streams[],uint8_t stream_count)1524 static bool streams_changed(struct dc *dc,
1525 			    struct dc_stream_state *streams[],
1526 			    uint8_t stream_count)
1527 {
1528 	uint8_t i;
1529 
1530 	if (stream_count != dc->current_state->stream_count)
1531 		return true;
1532 
1533 	for (i = 0; i < dc->current_state->stream_count; i++) {
1534 		if (dc->current_state->streams[i] != streams[i])
1535 			return true;
1536 		if (!streams[i]->link->link_state_valid)
1537 			return true;
1538 	}
1539 
1540 	return false;
1541 }
1542 
dc_validate_boot_timing(const struct dc * dc,const struct dc_sink * sink,struct dc_crtc_timing * crtc_timing)1543 bool dc_validate_boot_timing(const struct dc *dc,
1544 				const struct dc_sink *sink,
1545 				struct dc_crtc_timing *crtc_timing)
1546 {
1547 	struct timing_generator *tg;
1548 	struct stream_encoder *se = NULL;
1549 
1550 	struct dc_crtc_timing hw_crtc_timing = {0};
1551 
1552 	struct dc_link *link = sink->link;
1553 	unsigned int i, enc_inst, tg_inst = 0;
1554 
1555 	/* Support seamless boot on EDP displays only */
1556 	if (sink->sink_signal != SIGNAL_TYPE_EDP) {
1557 		return false;
1558 	}
1559 
1560 	if (dc->debug.force_odm_combine)
1561 		return false;
1562 
1563 	/* Check for enabled DIG to identify enabled display */
1564 	if (!link->link_enc->funcs->is_dig_enabled(link->link_enc))
1565 		return false;
1566 
1567 	enc_inst = link->link_enc->funcs->get_dig_frontend(link->link_enc);
1568 
1569 	if (enc_inst == ENGINE_ID_UNKNOWN)
1570 		return false;
1571 
1572 	for (i = 0; i < dc->res_pool->stream_enc_count; i++) {
1573 		if (dc->res_pool->stream_enc[i]->id == enc_inst) {
1574 
1575 			se = dc->res_pool->stream_enc[i];
1576 
1577 			tg_inst = dc->res_pool->stream_enc[i]->funcs->dig_source_otg(
1578 				dc->res_pool->stream_enc[i]);
1579 			break;
1580 		}
1581 	}
1582 
1583 	// tg_inst not found
1584 	if (i == dc->res_pool->stream_enc_count)
1585 		return false;
1586 
1587 	if (tg_inst >= dc->res_pool->timing_generator_count)
1588 		return false;
1589 
1590 	tg = dc->res_pool->timing_generators[tg_inst];
1591 
1592 	if (!tg->funcs->get_hw_timing)
1593 		return false;
1594 
1595 	if (!tg->funcs->get_hw_timing(tg, &hw_crtc_timing))
1596 		return false;
1597 
1598 	if (crtc_timing->h_total != hw_crtc_timing.h_total)
1599 		return false;
1600 
1601 	if (crtc_timing->h_border_left != hw_crtc_timing.h_border_left)
1602 		return false;
1603 
1604 	if (crtc_timing->h_addressable != hw_crtc_timing.h_addressable)
1605 		return false;
1606 
1607 	if (crtc_timing->h_border_right != hw_crtc_timing.h_border_right)
1608 		return false;
1609 
1610 	if (crtc_timing->h_front_porch != hw_crtc_timing.h_front_porch)
1611 		return false;
1612 
1613 	if (crtc_timing->h_sync_width != hw_crtc_timing.h_sync_width)
1614 		return false;
1615 
1616 	if (crtc_timing->v_total != hw_crtc_timing.v_total)
1617 		return false;
1618 
1619 	if (crtc_timing->v_border_top != hw_crtc_timing.v_border_top)
1620 		return false;
1621 
1622 	if (crtc_timing->v_addressable != hw_crtc_timing.v_addressable)
1623 		return false;
1624 
1625 	if (crtc_timing->v_border_bottom != hw_crtc_timing.v_border_bottom)
1626 		return false;
1627 
1628 	if (crtc_timing->v_front_porch != hw_crtc_timing.v_front_porch)
1629 		return false;
1630 
1631 	if (crtc_timing->v_sync_width != hw_crtc_timing.v_sync_width)
1632 		return false;
1633 
1634 	/* block DSC for now, as VBIOS does not currently support DSC timings */
1635 	if (crtc_timing->flags.DSC)
1636 		return false;
1637 
1638 	if (dc_is_dp_signal(link->connector_signal)) {
1639 		unsigned int pix_clk_100hz;
1640 		uint32_t numOdmPipes = 1;
1641 		uint32_t id_src[4] = {0};
1642 
1643 		dc->res_pool->dp_clock_source->funcs->get_pixel_clk_frequency_100hz(
1644 			dc->res_pool->dp_clock_source,
1645 			tg_inst, &pix_clk_100hz);
1646 
1647 		if (tg->funcs->get_optc_source)
1648 			tg->funcs->get_optc_source(tg,
1649 						&numOdmPipes, &id_src[0], &id_src[1]);
1650 
1651 		if (numOdmPipes == 2)
1652 			pix_clk_100hz *= 2;
1653 		if (numOdmPipes == 4)
1654 			pix_clk_100hz *= 4;
1655 
1656 		// Note: In rare cases, HW pixclk may differ from crtc's pixclk
1657 		// slightly due to rounding issues in 10 kHz units.
1658 		if (crtc_timing->pix_clk_100hz != pix_clk_100hz)
1659 			return false;
1660 
1661 		if (!se->funcs->dp_get_pixel_format)
1662 			return false;
1663 
1664 		if (!se->funcs->dp_get_pixel_format(
1665 			se,
1666 			&hw_crtc_timing.pixel_encoding,
1667 			&hw_crtc_timing.display_color_depth))
1668 			return false;
1669 
1670 		if (hw_crtc_timing.display_color_depth != crtc_timing->display_color_depth)
1671 			return false;
1672 
1673 		if (hw_crtc_timing.pixel_encoding != crtc_timing->pixel_encoding)
1674 			return false;
1675 	}
1676 
1677 	if (link->dpcd_caps.dprx_feature.bits.VSC_SDP_COLORIMETRY_SUPPORTED) {
1678 		return false;
1679 	}
1680 
1681 	if (is_edp_ilr_optimization_required(link, crtc_timing)) {
1682 		DC_LOG_EVENT_LINK_TRAINING("Seamless boot disabled to optimize eDP link rate\n");
1683 		return false;
1684 	}
1685 
1686 	return true;
1687 }
1688 
should_update_pipe_for_stream(struct dc_state * context,struct pipe_ctx * pipe_ctx,struct dc_stream_state * stream)1689 static inline bool should_update_pipe_for_stream(
1690 		struct dc_state *context,
1691 		struct pipe_ctx *pipe_ctx,
1692 		struct dc_stream_state *stream)
1693 {
1694 	return (pipe_ctx->stream && pipe_ctx->stream == stream);
1695 }
1696 
should_update_pipe_for_plane(struct dc_state * context,struct pipe_ctx * pipe_ctx,struct dc_plane_state * plane_state)1697 static inline bool should_update_pipe_for_plane(
1698 		struct dc_state *context,
1699 		struct pipe_ctx *pipe_ctx,
1700 		struct dc_plane_state *plane_state)
1701 {
1702 	return (pipe_ctx->plane_state == plane_state);
1703 }
1704 
dc_enable_stereo(struct dc * dc,struct dc_state * context,struct dc_stream_state * streams[],uint8_t stream_count)1705 void dc_enable_stereo(
1706 	struct dc *dc,
1707 	struct dc_state *context,
1708 	struct dc_stream_state *streams[],
1709 	uint8_t stream_count)
1710 {
1711 	int i, j;
1712 	struct pipe_ctx *pipe;
1713 
1714 	for (i = 0; i < MAX_PIPES; i++) {
1715 		if (context != NULL) {
1716 			pipe = &context->res_ctx.pipe_ctx[i];
1717 		} else {
1718 			context = dc->current_state;
1719 			pipe = &dc->current_state->res_ctx.pipe_ctx[i];
1720 		}
1721 
1722 		for (j = 0; pipe && j < stream_count; j++)  {
1723 			if (should_update_pipe_for_stream(context, pipe, streams[j]) &&
1724 				dc->hwss.setup_stereo)
1725 				dc->hwss.setup_stereo(pipe, dc);
1726 		}
1727 	}
1728 }
1729 
dc_trigger_sync(struct dc * dc,struct dc_state * context)1730 void dc_trigger_sync(struct dc *dc, struct dc_state *context)
1731 {
1732 	if (context->stream_count > 1 && !dc->debug.disable_timing_sync) {
1733 		enable_timing_multisync(dc, context);
1734 		program_timing_sync(dc, context);
1735 	}
1736 }
1737 
get_stream_mask(struct dc * dc,struct dc_state * context)1738 static uint8_t get_stream_mask(struct dc *dc, struct dc_state *context)
1739 {
1740 	int i;
1741 	unsigned int stream_mask = 0;
1742 
1743 	for (i = 0; i < dc->res_pool->pipe_count; i++) {
1744 		if (context->res_ctx.pipe_ctx[i].stream)
1745 			stream_mask |= 1 << i;
1746 	}
1747 
1748 	return stream_mask;
1749 }
1750 
dc_z10_restore(const struct dc * dc)1751 void dc_z10_restore(const struct dc *dc)
1752 {
1753 	if (dc->hwss.z10_restore)
1754 		dc->hwss.z10_restore(dc);
1755 }
1756 
dc_z10_save_init(struct dc * dc)1757 void dc_z10_save_init(struct dc *dc)
1758 {
1759 	if (dc->hwss.z10_save_init)
1760 		dc->hwss.z10_save_init(dc);
1761 }
1762 
1763 /*
1764  * Applies given context to HW and copy it into current context.
1765  * It's up to the user to release the src context afterwards.
1766  *
1767  * Return: an enum dc_status result code for the operation
1768  */
dc_commit_state_no_check(struct dc * dc,struct dc_state * context)1769 static enum dc_status dc_commit_state_no_check(struct dc *dc, struct dc_state *context)
1770 {
1771 	struct dc_bios *dcb = dc->ctx->dc_bios;
1772 	enum dc_status result = DC_ERROR_UNEXPECTED;
1773 	struct pipe_ctx *pipe;
1774 	int i, k, l;
1775 	struct dc_stream_state *dc_streams[MAX_STREAMS] = {0};
1776 	struct dc_state *old_state;
1777 	bool subvp_prev_use = false;
1778 
1779 	dc_z10_restore(dc);
1780 	dc_allow_idle_optimizations(dc, false);
1781 
1782 	for (i = 0; i < dc->res_pool->pipe_count; i++) {
1783 		struct pipe_ctx *old_pipe = &dc->current_state->res_ctx.pipe_ctx[i];
1784 
1785 		/* Check old context for SubVP */
1786 		subvp_prev_use |= (old_pipe->stream && old_pipe->stream->mall_stream_config.type == SUBVP_PHANTOM);
1787 		if (subvp_prev_use)
1788 			break;
1789 	}
1790 
1791 	for (i = 0; i < context->stream_count; i++)
1792 		dc_streams[i] =  context->streams[i];
1793 
1794 	if (!dcb->funcs->is_accelerated_mode(dcb)) {
1795 		disable_vbios_mode_if_required(dc, context);
1796 		dc->hwss.enable_accelerated_mode(dc, context);
1797 	}
1798 
1799 	if (context->stream_count > get_seamless_boot_stream_count(context) ||
1800 		context->stream_count == 0)
1801 		dc->hwss.prepare_bandwidth(dc, context);
1802 
1803 	/* When SubVP is active, all HW programming must be done while
1804 	 * SubVP lock is acquired
1805 	 */
1806 	if (dc->hwss.subvp_pipe_control_lock)
1807 		dc->hwss.subvp_pipe_control_lock(dc, context, true, true, NULL, subvp_prev_use);
1808 
1809 	if (dc->hwss.update_dsc_pg)
1810 		dc->hwss.update_dsc_pg(dc, context, false);
1811 
1812 	disable_dangling_plane(dc, context);
1813 	/* re-program planes for existing stream, in case we need to
1814 	 * free up plane resource for later use
1815 	 */
1816 	if (dc->hwss.apply_ctx_for_surface) {
1817 		for (i = 0; i < context->stream_count; i++) {
1818 			if (context->streams[i]->mode_changed)
1819 				continue;
1820 			apply_ctx_interdependent_lock(dc, context, context->streams[i], true);
1821 			dc->hwss.apply_ctx_for_surface(
1822 				dc, context->streams[i],
1823 				context->stream_status[i].plane_count,
1824 				context); /* use new pipe config in new context */
1825 			apply_ctx_interdependent_lock(dc, context, context->streams[i], false);
1826 			dc->hwss.post_unlock_program_front_end(dc, context);
1827 		}
1828 	}
1829 
1830 	/* Program hardware */
1831 	for (i = 0; i < dc->res_pool->pipe_count; i++) {
1832 		pipe = &context->res_ctx.pipe_ctx[i];
1833 		dc->hwss.wait_for_mpcc_disconnect(dc, dc->res_pool, pipe);
1834 	}
1835 
1836 	result = dc->hwss.apply_ctx_to_hw(dc, context);
1837 
1838 	if (result != DC_OK) {
1839 		/* Application of dc_state to hardware stopped. */
1840 		dc->current_state->res_ctx.link_enc_cfg_ctx.mode = LINK_ENC_CFG_STEADY;
1841 		return result;
1842 	}
1843 
1844 	dc_trigger_sync(dc, context);
1845 
1846 	/* Program all planes within new context*/
1847 	if (dc->hwss.program_front_end_for_ctx) {
1848 		dc->hwss.interdependent_update_lock(dc, context, true);
1849 		dc->hwss.program_front_end_for_ctx(dc, context);
1850 		dc->hwss.interdependent_update_lock(dc, context, false);
1851 		dc->hwss.post_unlock_program_front_end(dc, context);
1852 	}
1853 
1854 	if (dc->hwss.commit_subvp_config)
1855 		dc->hwss.commit_subvp_config(dc, context);
1856 	if (dc->hwss.subvp_pipe_control_lock)
1857 		dc->hwss.subvp_pipe_control_lock(dc, context, false, true, NULL, subvp_prev_use);
1858 
1859 	for (i = 0; i < context->stream_count; i++) {
1860 		const struct dc_link *link = context->streams[i]->link;
1861 
1862 		if (!context->streams[i]->mode_changed)
1863 			continue;
1864 
1865 		if (dc->hwss.apply_ctx_for_surface) {
1866 			apply_ctx_interdependent_lock(dc, context, context->streams[i], true);
1867 			dc->hwss.apply_ctx_for_surface(
1868 					dc, context->streams[i],
1869 					context->stream_status[i].plane_count,
1870 					context);
1871 			apply_ctx_interdependent_lock(dc, context, context->streams[i], false);
1872 			dc->hwss.post_unlock_program_front_end(dc, context);
1873 		}
1874 
1875 		/*
1876 		 * enable stereo
1877 		 * TODO rework dc_enable_stereo call to work with validation sets?
1878 		 */
1879 		for (k = 0; k < MAX_PIPES; k++) {
1880 			pipe = &context->res_ctx.pipe_ctx[k];
1881 
1882 			for (l = 0 ; pipe && l < context->stream_count; l++)  {
1883 				if (context->streams[l] &&
1884 					context->streams[l] == pipe->stream &&
1885 					dc->hwss.setup_stereo)
1886 					dc->hwss.setup_stereo(pipe, dc);
1887 			}
1888 		}
1889 
1890 		CONN_MSG_MODE(link, "{%dx%d, %dx%d@%dKhz}",
1891 				context->streams[i]->timing.h_addressable,
1892 				context->streams[i]->timing.v_addressable,
1893 				context->streams[i]->timing.h_total,
1894 				context->streams[i]->timing.v_total,
1895 				context->streams[i]->timing.pix_clk_100hz / 10);
1896 	}
1897 
1898 	dc_enable_stereo(dc, context, dc_streams, context->stream_count);
1899 
1900 	if (context->stream_count > get_seamless_boot_stream_count(context) ||
1901 		context->stream_count == 0) {
1902 		/* Must wait for no flips to be pending before doing optimize bw */
1903 		wait_for_no_pipes_pending(dc, context);
1904 		/* pplib is notified if disp_num changed */
1905 		dc->hwss.optimize_bandwidth(dc, context);
1906 		/* Need to do otg sync again as otg could be out of sync due to otg
1907 		 * workaround applied during clock update
1908 		 */
1909 		dc_trigger_sync(dc, context);
1910 	}
1911 
1912 	if (dc->hwss.update_dsc_pg)
1913 		dc->hwss.update_dsc_pg(dc, context, true);
1914 
1915 	if (dc->ctx->dce_version >= DCE_VERSION_MAX)
1916 		TRACE_DCN_CLOCK_STATE(&context->bw_ctx.bw.dcn.clk);
1917 	else
1918 		TRACE_DCE_CLOCK_STATE(&context->bw_ctx.bw.dce);
1919 
1920 	context->stream_mask = get_stream_mask(dc, context);
1921 
1922 	if (context->stream_mask != dc->current_state->stream_mask)
1923 		dc_dmub_srv_notify_stream_mask(dc->ctx->dmub_srv, context->stream_mask);
1924 
1925 	for (i = 0; i < context->stream_count; i++)
1926 		context->streams[i]->mode_changed = false;
1927 
1928 	old_state = dc->current_state;
1929 	dc->current_state = context;
1930 
1931 	dc_release_state(old_state);
1932 
1933 	dc_retain_state(dc->current_state);
1934 
1935 	return result;
1936 }
1937 
1938 static bool commit_minimal_transition_state(struct dc *dc,
1939 		struct dc_state *transition_base_context);
1940 
1941 /**
1942  * dc_commit_streams - Commit current stream state
1943  *
1944  * @dc: DC object with the commit state to be configured in the hardware
1945  * @streams: Array with a list of stream state
1946  * @stream_count: Total of streams
1947  *
1948  * Function responsible for commit streams change to the hardware.
1949  *
1950  * Return:
1951  * Return DC_OK if everything work as expected, otherwise, return a dc_status
1952  * code.
1953  */
dc_commit_streams(struct dc * dc,struct dc_stream_state * streams[],uint8_t stream_count)1954 enum dc_status dc_commit_streams(struct dc *dc,
1955 				 struct dc_stream_state *streams[],
1956 				 uint8_t stream_count)
1957 {
1958 	int i, j;
1959 	struct dc_state *context;
1960 	enum dc_status res = DC_OK;
1961 	struct dc_validation_set set[MAX_STREAMS] = {0};
1962 	struct pipe_ctx *pipe;
1963 	bool handle_exit_odm2to1 = false;
1964 
1965 	if (dc->ctx->dce_environment == DCE_ENV_VIRTUAL_HW)
1966 		return res;
1967 
1968 	if (!streams_changed(dc, streams, stream_count))
1969 		return res;
1970 
1971 	DC_LOG_DC("%s: %d streams\n", __func__, stream_count);
1972 
1973 	for (i = 0; i < stream_count; i++) {
1974 		struct dc_stream_state *stream = streams[i];
1975 		struct dc_stream_status *status = dc_stream_get_status(stream);
1976 
1977 		dc_stream_log(dc, stream);
1978 
1979 		set[i].stream = stream;
1980 
1981 		if (status) {
1982 			set[i].plane_count = status->plane_count;
1983 			for (j = 0; j < status->plane_count; j++)
1984 				set[i].plane_states[j] = status->plane_states[j];
1985 		}
1986 	}
1987 
1988 	/* ODM Combine 2:1 power optimization is only applied for single stream
1989 	 * scenario, it uses extra pipes than needed to reduce power consumption
1990 	 * We need to switch off this feature to make room for new streams.
1991 	 */
1992 	if (stream_count > dc->current_state->stream_count &&
1993 			dc->current_state->stream_count == 1) {
1994 		for (i = 0; i < dc->res_pool->pipe_count; i++) {
1995 			pipe = &dc->current_state->res_ctx.pipe_ctx[i];
1996 			if (pipe->next_odm_pipe)
1997 				handle_exit_odm2to1 = true;
1998 		}
1999 	}
2000 
2001 	if (handle_exit_odm2to1)
2002 		res = commit_minimal_transition_state(dc, dc->current_state);
2003 
2004 	context = dc_create_state(dc);
2005 	if (!context)
2006 		goto context_alloc_fail;
2007 
2008 	dc_resource_state_copy_construct_current(dc, context);
2009 
2010 	res = dc_validate_with_context(dc, set, stream_count, context, false);
2011 	if (res != DC_OK) {
2012 		BREAK_TO_DEBUGGER();
2013 		goto fail;
2014 	}
2015 
2016 	res = dc_commit_state_no_check(dc, context);
2017 
2018 	for (i = 0; i < stream_count; i++) {
2019 		for (j = 0; j < context->stream_count; j++) {
2020 			if (streams[i]->stream_id == context->streams[j]->stream_id)
2021 				streams[i]->out.otg_offset = context->stream_status[j].primary_otg_inst;
2022 
2023 			if (dc_is_embedded_signal(streams[i]->signal)) {
2024 				struct dc_stream_status *status = dc_stream_get_status_from_state(context, streams[i]);
2025 
2026 				if (dc->hwss.is_abm_supported)
2027 					status->is_abm_supported = dc->hwss.is_abm_supported(dc, context, streams[i]);
2028 				else
2029 					status->is_abm_supported = true;
2030 			}
2031 		}
2032 	}
2033 
2034 fail:
2035 	dc_release_state(context);
2036 
2037 context_alloc_fail:
2038 
2039 	DC_LOG_DC("%s Finished.\n", __func__);
2040 
2041 	return (res == DC_OK);
2042 }
2043 
2044 /* TODO: When the transition to the new commit sequence is done, remove this
2045  * function in favor of dc_commit_streams. */
dc_commit_state(struct dc * dc,struct dc_state * context)2046 bool dc_commit_state(struct dc *dc, struct dc_state *context)
2047 {
2048 	enum dc_status result = DC_ERROR_UNEXPECTED;
2049 	int i;
2050 
2051 	/* TODO: Since change commit sequence can have a huge impact,
2052 	 * we decided to only enable it for DCN3x. However, as soon as
2053 	 * we get more confident about this change we'll need to enable
2054 	 * the new sequence for all ASICs. */
2055 	if (dc->ctx->dce_version >= DCN_VERSION_3_2) {
2056 		result = dc_commit_streams(dc, context->streams, context->stream_count);
2057 		return result == DC_OK;
2058 	}
2059 
2060 	if (!streams_changed(dc, context->streams, context->stream_count))
2061 		return DC_OK;
2062 
2063 	DC_LOG_DC("%s: %d streams\n",
2064 				__func__, context->stream_count);
2065 
2066 	for (i = 0; i < context->stream_count; i++) {
2067 		struct dc_stream_state *stream = context->streams[i];
2068 
2069 		dc_stream_log(dc, stream);
2070 	}
2071 
2072 	/*
2073 	 * Previous validation was perfomred with fast_validation = true and
2074 	 * the full DML state required for hardware programming was skipped.
2075 	 *
2076 	 * Re-validate here to calculate these parameters / watermarks.
2077 	 */
2078 	result = dc_validate_global_state(dc, context, false);
2079 	if (result != DC_OK) {
2080 		DC_LOG_ERROR("DC commit global validation failure: %s (%d)",
2081 			     dc_status_to_str(result), result);
2082 		return result;
2083 	}
2084 
2085 	result = dc_commit_state_no_check(dc, context);
2086 
2087 	return (result == DC_OK);
2088 }
2089 
dc_acquire_release_mpc_3dlut(struct dc * dc,bool acquire,struct dc_stream_state * stream,struct dc_3dlut ** lut,struct dc_transfer_func ** shaper)2090 bool dc_acquire_release_mpc_3dlut(
2091 		struct dc *dc, bool acquire,
2092 		struct dc_stream_state *stream,
2093 		struct dc_3dlut **lut,
2094 		struct dc_transfer_func **shaper)
2095 {
2096 	int pipe_idx;
2097 	bool ret = false;
2098 	bool found_pipe_idx = false;
2099 	const struct resource_pool *pool = dc->res_pool;
2100 	struct resource_context *res_ctx = &dc->current_state->res_ctx;
2101 	int mpcc_id = 0;
2102 
2103 	if (pool && res_ctx) {
2104 		if (acquire) {
2105 			/*find pipe idx for the given stream*/
2106 			for (pipe_idx = 0; pipe_idx < pool->pipe_count; pipe_idx++) {
2107 				if (res_ctx->pipe_ctx[pipe_idx].stream == stream) {
2108 					found_pipe_idx = true;
2109 					mpcc_id = res_ctx->pipe_ctx[pipe_idx].plane_res.hubp->inst;
2110 					break;
2111 				}
2112 			}
2113 		} else
2114 			found_pipe_idx = true;/*for release pipe_idx is not required*/
2115 
2116 		if (found_pipe_idx) {
2117 			if (acquire && pool->funcs->acquire_post_bldn_3dlut)
2118 				ret = pool->funcs->acquire_post_bldn_3dlut(res_ctx, pool, mpcc_id, lut, shaper);
2119 			else if (!acquire && pool->funcs->release_post_bldn_3dlut)
2120 				ret = pool->funcs->release_post_bldn_3dlut(res_ctx, pool, lut, shaper);
2121 		}
2122 	}
2123 	return ret;
2124 }
2125 
is_flip_pending_in_pipes(struct dc * dc,struct dc_state * context)2126 static bool is_flip_pending_in_pipes(struct dc *dc, struct dc_state *context)
2127 {
2128 	int i;
2129 	struct pipe_ctx *pipe;
2130 
2131 	for (i = 0; i < MAX_PIPES; i++) {
2132 		pipe = &context->res_ctx.pipe_ctx[i];
2133 
2134 		// Don't check flip pending on phantom pipes
2135 		if (!pipe->plane_state || (pipe->stream && pipe->stream->mall_stream_config.type == SUBVP_PHANTOM))
2136 			continue;
2137 
2138 		/* Must set to false to start with, due to OR in update function */
2139 		pipe->plane_state->status.is_flip_pending = false;
2140 		dc->hwss.update_pending_status(pipe);
2141 		if (pipe->plane_state->status.is_flip_pending)
2142 			return true;
2143 	}
2144 	return false;
2145 }
2146 
2147 /* Perform updates here which need to be deferred until next vupdate
2148  *
2149  * i.e. blnd lut, 3dlut, and shaper lut bypass regs are double buffered
2150  * but forcing lut memory to shutdown state is immediate. This causes
2151  * single frame corruption as lut gets disabled mid-frame unless shutdown
2152  * is deferred until after entering bypass.
2153  */
process_deferred_updates(struct dc * dc)2154 static void process_deferred_updates(struct dc *dc)
2155 {
2156 	int i = 0;
2157 
2158 	if (dc->debug.enable_mem_low_power.bits.cm) {
2159 		ASSERT(dc->dcn_ip->max_num_dpp);
2160 		for (i = 0; i < dc->dcn_ip->max_num_dpp; i++)
2161 			if (dc->res_pool->dpps[i]->funcs->dpp_deferred_update)
2162 				dc->res_pool->dpps[i]->funcs->dpp_deferred_update(dc->res_pool->dpps[i]);
2163 	}
2164 }
2165 
dc_post_update_surfaces_to_stream(struct dc * dc)2166 void dc_post_update_surfaces_to_stream(struct dc *dc)
2167 {
2168 	int i;
2169 	struct dc_state *context = dc->current_state;
2170 
2171 	if ((!dc->optimized_required) || get_seamless_boot_stream_count(context) > 0)
2172 		return;
2173 
2174 	post_surface_trace(dc);
2175 
2176 	/*
2177 	 * Only relevant for DCN behavior where we can guarantee the optimization
2178 	 * is safe to apply - retain the legacy behavior for DCE.
2179 	 */
2180 
2181 	if (dc->ctx->dce_version < DCE_VERSION_MAX)
2182 		TRACE_DCE_CLOCK_STATE(&context->bw_ctx.bw.dce);
2183 	else {
2184 		TRACE_DCN_CLOCK_STATE(&context->bw_ctx.bw.dcn.clk);
2185 
2186 		if (is_flip_pending_in_pipes(dc, context))
2187 			return;
2188 
2189 		for (i = 0; i < dc->res_pool->pipe_count; i++)
2190 			if (context->res_ctx.pipe_ctx[i].stream == NULL ||
2191 					context->res_ctx.pipe_ctx[i].plane_state == NULL) {
2192 				context->res_ctx.pipe_ctx[i].pipe_idx = i;
2193 				dc->hwss.disable_plane(dc, &context->res_ctx.pipe_ctx[i]);
2194 			}
2195 
2196 		process_deferred_updates(dc);
2197 
2198 		dc->hwss.optimize_bandwidth(dc, context);
2199 
2200 		if (dc->hwss.update_dsc_pg)
2201 			dc->hwss.update_dsc_pg(dc, context, true);
2202 	}
2203 
2204 	dc->optimized_required = false;
2205 	dc->wm_optimized_required = false;
2206 }
2207 
init_state(struct dc * dc,struct dc_state * context)2208 static void init_state(struct dc *dc, struct dc_state *context)
2209 {
2210 	/* Each context must have their own instance of VBA and in order to
2211 	 * initialize and obtain IP and SOC the base DML instance from DC is
2212 	 * initially copied into every context
2213 	 */
2214 	memcpy(&context->bw_ctx.dml, &dc->dml, sizeof(struct display_mode_lib));
2215 }
2216 
dc_create_state(struct dc * dc)2217 struct dc_state *dc_create_state(struct dc *dc)
2218 {
2219 	struct dc_state *context = kvzalloc(sizeof(struct dc_state),
2220 					    GFP_KERNEL);
2221 
2222 	if (!context)
2223 		return NULL;
2224 
2225 	init_state(dc, context);
2226 
2227 	kref_init(&context->refcount);
2228 
2229 	return context;
2230 }
2231 
dc_copy_state(struct dc_state * src_ctx)2232 struct dc_state *dc_copy_state(struct dc_state *src_ctx)
2233 {
2234 	int i, j;
2235 	struct dc_state *new_ctx = kvmalloc(sizeof(struct dc_state), GFP_KERNEL);
2236 
2237 	if (!new_ctx)
2238 		return NULL;
2239 	memcpy(new_ctx, src_ctx, sizeof(struct dc_state));
2240 
2241 	for (i = 0; i < MAX_PIPES; i++) {
2242 			struct pipe_ctx *cur_pipe = &new_ctx->res_ctx.pipe_ctx[i];
2243 
2244 			if (cur_pipe->top_pipe)
2245 				cur_pipe->top_pipe =  &new_ctx->res_ctx.pipe_ctx[cur_pipe->top_pipe->pipe_idx];
2246 
2247 			if (cur_pipe->bottom_pipe)
2248 				cur_pipe->bottom_pipe = &new_ctx->res_ctx.pipe_ctx[cur_pipe->bottom_pipe->pipe_idx];
2249 
2250 			if (cur_pipe->prev_odm_pipe)
2251 				cur_pipe->prev_odm_pipe =  &new_ctx->res_ctx.pipe_ctx[cur_pipe->prev_odm_pipe->pipe_idx];
2252 
2253 			if (cur_pipe->next_odm_pipe)
2254 				cur_pipe->next_odm_pipe = &new_ctx->res_ctx.pipe_ctx[cur_pipe->next_odm_pipe->pipe_idx];
2255 
2256 	}
2257 
2258 	for (i = 0; i < new_ctx->stream_count; i++) {
2259 			dc_stream_retain(new_ctx->streams[i]);
2260 			for (j = 0; j < new_ctx->stream_status[i].plane_count; j++)
2261 				dc_plane_state_retain(
2262 					new_ctx->stream_status[i].plane_states[j]);
2263 	}
2264 
2265 	kref_init(&new_ctx->refcount);
2266 
2267 	return new_ctx;
2268 }
2269 
dc_retain_state(struct dc_state * context)2270 void dc_retain_state(struct dc_state *context)
2271 {
2272 	kref_get(&context->refcount);
2273 }
2274 
dc_state_free(struct kref * kref)2275 static void dc_state_free(struct kref *kref)
2276 {
2277 	struct dc_state *context = container_of(kref, struct dc_state, refcount);
2278 	dc_resource_state_destruct(context);
2279 	kvfree(context);
2280 }
2281 
dc_release_state(struct dc_state * context)2282 void dc_release_state(struct dc_state *context)
2283 {
2284 	kref_put(&context->refcount, dc_state_free);
2285 }
2286 
dc_set_generic_gpio_for_stereo(bool enable,struct gpio_service * gpio_service)2287 bool dc_set_generic_gpio_for_stereo(bool enable,
2288 		struct gpio_service *gpio_service)
2289 {
2290 	enum gpio_result gpio_result = GPIO_RESULT_NON_SPECIFIC_ERROR;
2291 	struct gpio_pin_info pin_info;
2292 	struct gpio *generic;
2293 	struct gpio_generic_mux_config *config = kzalloc(sizeof(struct gpio_generic_mux_config),
2294 			   GFP_KERNEL);
2295 
2296 	if (!config)
2297 		return false;
2298 	pin_info = dal_gpio_get_generic_pin_info(gpio_service, GPIO_ID_GENERIC, 0);
2299 
2300 	if (pin_info.mask == 0xFFFFFFFF || pin_info.offset == 0xFFFFFFFF) {
2301 		kfree(config);
2302 		return false;
2303 	} else {
2304 		generic = dal_gpio_service_create_generic_mux(
2305 			gpio_service,
2306 			pin_info.offset,
2307 			pin_info.mask);
2308 	}
2309 
2310 	if (!generic) {
2311 		kfree(config);
2312 		return false;
2313 	}
2314 
2315 	gpio_result = dal_gpio_open(generic, GPIO_MODE_OUTPUT);
2316 
2317 	config->enable_output_from_mux = enable;
2318 	config->mux_select = GPIO_SIGNAL_SOURCE_PASS_THROUGH_STEREO_SYNC;
2319 
2320 	if (gpio_result == GPIO_RESULT_OK)
2321 		gpio_result = dal_mux_setup_config(generic, config);
2322 
2323 	if (gpio_result == GPIO_RESULT_OK) {
2324 		dal_gpio_close(generic);
2325 		dal_gpio_destroy_generic_mux(&generic);
2326 		kfree(config);
2327 		return true;
2328 	} else {
2329 		dal_gpio_close(generic);
2330 		dal_gpio_destroy_generic_mux(&generic);
2331 		kfree(config);
2332 		return false;
2333 	}
2334 }
2335 
is_surface_in_context(const struct dc_state * context,const struct dc_plane_state * plane_state)2336 static bool is_surface_in_context(
2337 		const struct dc_state *context,
2338 		const struct dc_plane_state *plane_state)
2339 {
2340 	int j;
2341 
2342 	for (j = 0; j < MAX_PIPES; j++) {
2343 		const struct pipe_ctx *pipe_ctx = &context->res_ctx.pipe_ctx[j];
2344 
2345 		if (plane_state == pipe_ctx->plane_state) {
2346 			return true;
2347 		}
2348 	}
2349 
2350 	return false;
2351 }
2352 
get_plane_info_update_type(const struct dc_surface_update * u)2353 static enum surface_update_type get_plane_info_update_type(const struct dc_surface_update *u)
2354 {
2355 	union surface_update_flags *update_flags = &u->surface->update_flags;
2356 	enum surface_update_type update_type = UPDATE_TYPE_FAST;
2357 
2358 	if (!u->plane_info)
2359 		return UPDATE_TYPE_FAST;
2360 
2361 	if (u->plane_info->color_space != u->surface->color_space) {
2362 		update_flags->bits.color_space_change = 1;
2363 		elevate_update_type(&update_type, UPDATE_TYPE_MED);
2364 	}
2365 
2366 	if (u->plane_info->horizontal_mirror != u->surface->horizontal_mirror) {
2367 		update_flags->bits.horizontal_mirror_change = 1;
2368 		elevate_update_type(&update_type, UPDATE_TYPE_MED);
2369 	}
2370 
2371 	if (u->plane_info->rotation != u->surface->rotation) {
2372 		update_flags->bits.rotation_change = 1;
2373 		elevate_update_type(&update_type, UPDATE_TYPE_FULL);
2374 	}
2375 
2376 	if (u->plane_info->format != u->surface->format) {
2377 		update_flags->bits.pixel_format_change = 1;
2378 		elevate_update_type(&update_type, UPDATE_TYPE_FULL);
2379 	}
2380 
2381 	if (u->plane_info->stereo_format != u->surface->stereo_format) {
2382 		update_flags->bits.stereo_format_change = 1;
2383 		elevate_update_type(&update_type, UPDATE_TYPE_FULL);
2384 	}
2385 
2386 	if (u->plane_info->per_pixel_alpha != u->surface->per_pixel_alpha) {
2387 		update_flags->bits.per_pixel_alpha_change = 1;
2388 		elevate_update_type(&update_type, UPDATE_TYPE_MED);
2389 	}
2390 
2391 	if (u->plane_info->global_alpha_value != u->surface->global_alpha_value) {
2392 		update_flags->bits.global_alpha_change = 1;
2393 		elevate_update_type(&update_type, UPDATE_TYPE_MED);
2394 	}
2395 
2396 	if (u->plane_info->dcc.enable != u->surface->dcc.enable
2397 			|| u->plane_info->dcc.dcc_ind_blk != u->surface->dcc.dcc_ind_blk
2398 			|| u->plane_info->dcc.meta_pitch != u->surface->dcc.meta_pitch) {
2399 		/* During DCC on/off, stutter period is calculated before
2400 		 * DCC has fully transitioned. This results in incorrect
2401 		 * stutter period calculation. Triggering a full update will
2402 		 * recalculate stutter period.
2403 		 */
2404 		update_flags->bits.dcc_change = 1;
2405 		elevate_update_type(&update_type, UPDATE_TYPE_FULL);
2406 	}
2407 
2408 	if (resource_pixel_format_to_bpp(u->plane_info->format) !=
2409 			resource_pixel_format_to_bpp(u->surface->format)) {
2410 		/* different bytes per element will require full bandwidth
2411 		 * and DML calculation
2412 		 */
2413 		update_flags->bits.bpp_change = 1;
2414 		elevate_update_type(&update_type, UPDATE_TYPE_FULL);
2415 	}
2416 
2417 	if (u->plane_info->plane_size.surface_pitch != u->surface->plane_size.surface_pitch
2418 			|| u->plane_info->plane_size.chroma_pitch != u->surface->plane_size.chroma_pitch) {
2419 		update_flags->bits.plane_size_change = 1;
2420 		elevate_update_type(&update_type, UPDATE_TYPE_MED);
2421 	}
2422 
2423 
2424 	if (memcmp(&u->plane_info->tiling_info, &u->surface->tiling_info,
2425 			sizeof(union dc_tiling_info)) != 0) {
2426 		update_flags->bits.swizzle_change = 1;
2427 		elevate_update_type(&update_type, UPDATE_TYPE_MED);
2428 
2429 		/* todo: below are HW dependent, we should add a hook to
2430 		 * DCE/N resource and validated there.
2431 		 */
2432 		if (u->plane_info->tiling_info.gfx9.swizzle != DC_SW_LINEAR) {
2433 			/* swizzled mode requires RQ to be setup properly,
2434 			 * thus need to run DML to calculate RQ settings
2435 			 */
2436 			update_flags->bits.bandwidth_change = 1;
2437 			elevate_update_type(&update_type, UPDATE_TYPE_FULL);
2438 		}
2439 	}
2440 
2441 	/* This should be UPDATE_TYPE_FAST if nothing has changed. */
2442 	return update_type;
2443 }
2444 
get_scaling_info_update_type(const struct dc * dc,const struct dc_surface_update * u)2445 static enum surface_update_type get_scaling_info_update_type(
2446 		const struct dc *dc,
2447 		const struct dc_surface_update *u)
2448 {
2449 	union surface_update_flags *update_flags = &u->surface->update_flags;
2450 
2451 	if (!u->scaling_info)
2452 		return UPDATE_TYPE_FAST;
2453 
2454 	if (u->scaling_info->clip_rect.width != u->surface->clip_rect.width
2455 			|| u->scaling_info->clip_rect.height != u->surface->clip_rect.height
2456 			|| u->scaling_info->dst_rect.width != u->surface->dst_rect.width
2457 			|| u->scaling_info->dst_rect.height != u->surface->dst_rect.height
2458 			|| u->scaling_info->scaling_quality.integer_scaling !=
2459 				u->surface->scaling_quality.integer_scaling
2460 			) {
2461 		update_flags->bits.scaling_change = 1;
2462 
2463 		if ((u->scaling_info->dst_rect.width < u->surface->dst_rect.width
2464 			|| u->scaling_info->dst_rect.height < u->surface->dst_rect.height)
2465 				&& (u->scaling_info->dst_rect.width < u->surface->src_rect.width
2466 					|| u->scaling_info->dst_rect.height < u->surface->src_rect.height))
2467 			/* Making dst rect smaller requires a bandwidth change */
2468 			update_flags->bits.bandwidth_change = 1;
2469 	}
2470 
2471 	if (u->scaling_info->src_rect.width != u->surface->src_rect.width
2472 		|| u->scaling_info->src_rect.height != u->surface->src_rect.height) {
2473 
2474 		update_flags->bits.scaling_change = 1;
2475 		if (u->scaling_info->src_rect.width > u->surface->src_rect.width
2476 				|| u->scaling_info->src_rect.height > u->surface->src_rect.height)
2477 			/* Making src rect bigger requires a bandwidth change */
2478 			update_flags->bits.clock_change = 1;
2479 	}
2480 
2481 	if (u->scaling_info->src_rect.width > dc->caps.max_optimizable_video_width &&
2482 		(u->scaling_info->clip_rect.width > u->surface->clip_rect.width ||
2483 		 u->scaling_info->clip_rect.height > u->surface->clip_rect.height))
2484 		 /* Changing clip size of a large surface may result in MPC slice count change */
2485 		update_flags->bits.bandwidth_change = 1;
2486 
2487 	if (u->scaling_info->src_rect.x != u->surface->src_rect.x
2488 			|| u->scaling_info->src_rect.y != u->surface->src_rect.y
2489 			|| u->scaling_info->clip_rect.x != u->surface->clip_rect.x
2490 			|| u->scaling_info->clip_rect.y != u->surface->clip_rect.y
2491 			|| u->scaling_info->dst_rect.x != u->surface->dst_rect.x
2492 			|| u->scaling_info->dst_rect.y != u->surface->dst_rect.y)
2493 		update_flags->bits.position_change = 1;
2494 
2495 	if (update_flags->bits.clock_change
2496 			|| update_flags->bits.bandwidth_change
2497 			|| update_flags->bits.scaling_change)
2498 		return UPDATE_TYPE_FULL;
2499 
2500 	if (update_flags->bits.position_change)
2501 		return UPDATE_TYPE_MED;
2502 
2503 	return UPDATE_TYPE_FAST;
2504 }
2505 
det_surface_update(const struct dc * dc,const struct dc_surface_update * u)2506 static enum surface_update_type det_surface_update(const struct dc *dc,
2507 		const struct dc_surface_update *u)
2508 {
2509 	const struct dc_state *context = dc->current_state;
2510 	enum surface_update_type type;
2511 	enum surface_update_type overall_type = UPDATE_TYPE_FAST;
2512 	union surface_update_flags *update_flags = &u->surface->update_flags;
2513 
2514 	if (!is_surface_in_context(context, u->surface) || u->surface->force_full_update) {
2515 		update_flags->raw = 0xFFFFFFFF;
2516 		return UPDATE_TYPE_FULL;
2517 	}
2518 
2519 	update_flags->raw = 0; // Reset all flags
2520 
2521 	type = get_plane_info_update_type(u);
2522 	elevate_update_type(&overall_type, type);
2523 
2524 	type = get_scaling_info_update_type(dc, u);
2525 	elevate_update_type(&overall_type, type);
2526 
2527 	if (u->flip_addr) {
2528 		update_flags->bits.addr_update = 1;
2529 		if (u->flip_addr->address.tmz_surface != u->surface->address.tmz_surface) {
2530 			update_flags->bits.tmz_changed = 1;
2531 			elevate_update_type(&overall_type, UPDATE_TYPE_FULL);
2532 		}
2533 	}
2534 	if (u->in_transfer_func)
2535 		update_flags->bits.in_transfer_func_change = 1;
2536 
2537 	if (u->input_csc_color_matrix)
2538 		update_flags->bits.input_csc_change = 1;
2539 
2540 	if (u->coeff_reduction_factor)
2541 		update_flags->bits.coeff_reduction_change = 1;
2542 
2543 	if (u->gamut_remap_matrix)
2544 		update_flags->bits.gamut_remap_change = 1;
2545 
2546 	if (u->gamma) {
2547 		enum surface_pixel_format format = SURFACE_PIXEL_FORMAT_GRPH_BEGIN;
2548 
2549 		if (u->plane_info)
2550 			format = u->plane_info->format;
2551 		else if (u->surface)
2552 			format = u->surface->format;
2553 
2554 		if (dce_use_lut(format))
2555 			update_flags->bits.gamma_change = 1;
2556 	}
2557 
2558 	if (u->lut3d_func || u->func_shaper)
2559 		update_flags->bits.lut_3d = 1;
2560 
2561 	if (u->hdr_mult.value)
2562 		if (u->hdr_mult.value != u->surface->hdr_mult.value) {
2563 			update_flags->bits.hdr_mult = 1;
2564 			elevate_update_type(&overall_type, UPDATE_TYPE_MED);
2565 		}
2566 
2567 	if (update_flags->bits.in_transfer_func_change) {
2568 		type = UPDATE_TYPE_MED;
2569 		elevate_update_type(&overall_type, type);
2570 	}
2571 
2572 	if (update_flags->bits.input_csc_change
2573 			|| update_flags->bits.coeff_reduction_change
2574 			|| update_flags->bits.lut_3d
2575 			|| update_flags->bits.gamma_change
2576 			|| update_flags->bits.gamut_remap_change) {
2577 		type = UPDATE_TYPE_FULL;
2578 		elevate_update_type(&overall_type, type);
2579 	}
2580 
2581 	return overall_type;
2582 }
2583 
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)2584 static enum surface_update_type check_update_surfaces_for_stream(
2585 		struct dc *dc,
2586 		struct dc_surface_update *updates,
2587 		int surface_count,
2588 		struct dc_stream_update *stream_update,
2589 		const struct dc_stream_status *stream_status)
2590 {
2591 	int i;
2592 	enum surface_update_type overall_type = UPDATE_TYPE_FAST;
2593 
2594 	if (dc->idle_optimizations_allowed)
2595 		overall_type = UPDATE_TYPE_FULL;
2596 
2597 	if (stream_status == NULL || stream_status->plane_count != surface_count)
2598 		overall_type = UPDATE_TYPE_FULL;
2599 
2600 	if (stream_update && stream_update->pending_test_pattern) {
2601 		overall_type = UPDATE_TYPE_FULL;
2602 	}
2603 
2604 	/* some stream updates require passive update */
2605 	if (stream_update) {
2606 		union stream_update_flags *su_flags = &stream_update->stream->update_flags;
2607 
2608 		if ((stream_update->src.height != 0 && stream_update->src.width != 0) ||
2609 			(stream_update->dst.height != 0 && stream_update->dst.width != 0) ||
2610 			stream_update->integer_scaling_update)
2611 			su_flags->bits.scaling = 1;
2612 
2613 		if (stream_update->out_transfer_func)
2614 			su_flags->bits.out_tf = 1;
2615 
2616 		if (stream_update->abm_level)
2617 			su_flags->bits.abm_level = 1;
2618 
2619 		if (stream_update->dpms_off)
2620 			su_flags->bits.dpms_off = 1;
2621 
2622 		if (stream_update->gamut_remap)
2623 			su_flags->bits.gamut_remap = 1;
2624 
2625 		if (stream_update->wb_update)
2626 			su_flags->bits.wb_update = 1;
2627 
2628 		if (stream_update->dsc_config)
2629 			su_flags->bits.dsc_changed = 1;
2630 
2631 		if (stream_update->mst_bw_update)
2632 			su_flags->bits.mst_bw = 1;
2633 
2634 		if (stream_update->stream && stream_update->stream->freesync_on_desktop &&
2635 			(stream_update->vrr_infopacket || stream_update->allow_freesync ||
2636 				stream_update->vrr_active_variable))
2637 			su_flags->bits.fams_changed = 1;
2638 
2639 		if (su_flags->raw != 0)
2640 			overall_type = UPDATE_TYPE_FULL;
2641 
2642 		if (stream_update->output_csc_transform || stream_update->output_color_space)
2643 			su_flags->bits.out_csc = 1;
2644 	}
2645 
2646 	for (i = 0 ; i < surface_count; i++) {
2647 		enum surface_update_type type =
2648 				det_surface_update(dc, &updates[i]);
2649 
2650 		elevate_update_type(&overall_type, type);
2651 	}
2652 
2653 	return overall_type;
2654 }
2655 
dc_check_is_fullscreen_video(struct rect src,struct rect clip_rect)2656 static bool dc_check_is_fullscreen_video(struct rect src, struct rect clip_rect)
2657 {
2658 	int view_height, view_width, clip_x, clip_y, clip_width, clip_height;
2659 
2660 	view_height = src.height;
2661 	view_width = src.width;
2662 
2663 	clip_x = clip_rect.x;
2664 	clip_y = clip_rect.y;
2665 
2666 	clip_width = clip_rect.width;
2667 	clip_height = clip_rect.height;
2668 
2669 	/* check for centered video accounting for off by 1 scaling truncation */
2670 	if ((view_height - clip_y - clip_height <= clip_y + 1) &&
2671 			(view_width - clip_x - clip_width <= clip_x + 1) &&
2672 			(view_height - clip_y - clip_height >= clip_y - 1) &&
2673 			(view_width - clip_x - clip_width >= clip_x - 1)) {
2674 
2675 		/* when OS scales up/down to letter box, it may end up
2676 		 * with few blank pixels on the border due to truncating.
2677 		 * Add offset margin to account for this
2678 		 */
2679 		if (clip_x <= 4 || clip_y <= 4)
2680 			return true;
2681 	}
2682 
2683 	return false;
2684 }
2685 
check_boundary_crossing_for_windowed_mpo_with_odm(struct dc * dc,struct dc_surface_update * srf_updates,int surface_count,enum surface_update_type update_type)2686 static enum surface_update_type check_boundary_crossing_for_windowed_mpo_with_odm(struct dc *dc,
2687 		struct dc_surface_update *srf_updates, int surface_count,
2688 		enum surface_update_type update_type)
2689 {
2690 	enum surface_update_type new_update_type = update_type;
2691 	int i, j;
2692 	struct pipe_ctx *pipe = NULL;
2693 	struct dc_stream_state *stream;
2694 
2695 	/* Check that we are in windowed MPO with ODM
2696 	 * - look for MPO pipe by scanning pipes for first pipe matching
2697 	 *   surface that has moved ( position change )
2698 	 * - MPO pipe will have top pipe
2699 	 * - check that top pipe has ODM pointer
2700 	 */
2701 	if ((surface_count > 1) && dc->config.enable_windowed_mpo_odm) {
2702 		for (i = 0; i < surface_count; i++) {
2703 			if (srf_updates[i].surface && srf_updates[i].scaling_info
2704 					&& srf_updates[i].surface->update_flags.bits.position_change) {
2705 
2706 				for (j = 0; j < dc->res_pool->pipe_count; j++) {
2707 					if (srf_updates[i].surface == dc->current_state->res_ctx.pipe_ctx[j].plane_state) {
2708 						pipe = &dc->current_state->res_ctx.pipe_ctx[j];
2709 						stream = pipe->stream;
2710 						break;
2711 					}
2712 				}
2713 
2714 				if (pipe && pipe->top_pipe && (get_num_odm_splits(pipe->top_pipe) > 0) && stream
2715 						&& !dc_check_is_fullscreen_video(stream->src, srf_updates[i].scaling_info->clip_rect)) {
2716 					struct rect old_clip_rect, new_clip_rect;
2717 					bool old_clip_rect_left, old_clip_rect_right, old_clip_rect_middle;
2718 					bool new_clip_rect_left, new_clip_rect_right, new_clip_rect_middle;
2719 
2720 					old_clip_rect = srf_updates[i].surface->clip_rect;
2721 					new_clip_rect = srf_updates[i].scaling_info->clip_rect;
2722 
2723 					old_clip_rect_left = ((old_clip_rect.x + old_clip_rect.width) <= (stream->src.x + (stream->src.width/2)));
2724 					old_clip_rect_right = (old_clip_rect.x >= (stream->src.x + (stream->src.width/2)));
2725 					old_clip_rect_middle = !old_clip_rect_left && !old_clip_rect_right;
2726 
2727 					new_clip_rect_left = ((new_clip_rect.x + new_clip_rect.width) <= (stream->src.x + (stream->src.width/2)));
2728 					new_clip_rect_right = (new_clip_rect.x >= (stream->src.x + (stream->src.width/2)));
2729 					new_clip_rect_middle = !new_clip_rect_left && !new_clip_rect_right;
2730 
2731 					if (old_clip_rect_left && new_clip_rect_middle)
2732 						new_update_type = UPDATE_TYPE_FULL;
2733 					else if (old_clip_rect_middle && new_clip_rect_right)
2734 						new_update_type = UPDATE_TYPE_FULL;
2735 					else if (old_clip_rect_right && new_clip_rect_middle)
2736 						new_update_type = UPDATE_TYPE_FULL;
2737 					else if (old_clip_rect_middle && new_clip_rect_left)
2738 						new_update_type = UPDATE_TYPE_FULL;
2739 				}
2740 			}
2741 		}
2742 	}
2743 	return new_update_type;
2744 }
2745 
2746 /*
2747  * dc_check_update_surfaces_for_stream() - Determine update type (fast, med, or full)
2748  *
2749  * See :c:type:`enum surface_update_type <surface_update_type>` for explanation of update types
2750  */
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)2751 enum surface_update_type dc_check_update_surfaces_for_stream(
2752 		struct dc *dc,
2753 		struct dc_surface_update *updates,
2754 		int surface_count,
2755 		struct dc_stream_update *stream_update,
2756 		const struct dc_stream_status *stream_status)
2757 {
2758 	int i;
2759 	enum surface_update_type type;
2760 
2761 	if (stream_update)
2762 		stream_update->stream->update_flags.raw = 0;
2763 	for (i = 0; i < surface_count; i++)
2764 		updates[i].surface->update_flags.raw = 0;
2765 
2766 	type = check_update_surfaces_for_stream(dc, updates, surface_count, stream_update, stream_status);
2767 	if (type == UPDATE_TYPE_FULL) {
2768 		if (stream_update) {
2769 			uint32_t dsc_changed = stream_update->stream->update_flags.bits.dsc_changed;
2770 			stream_update->stream->update_flags.raw = 0xFFFFFFFF;
2771 			stream_update->stream->update_flags.bits.dsc_changed = dsc_changed;
2772 		}
2773 		for (i = 0; i < surface_count; i++)
2774 			updates[i].surface->update_flags.raw = 0xFFFFFFFF;
2775 	}
2776 
2777 	if (type == UPDATE_TYPE_MED)
2778 		type = check_boundary_crossing_for_windowed_mpo_with_odm(dc,
2779 				updates, surface_count, type);
2780 
2781 	if (type == UPDATE_TYPE_FAST) {
2782 		// If there's an available clock comparator, we use that.
2783 		if (dc->clk_mgr->funcs->are_clock_states_equal) {
2784 			if (!dc->clk_mgr->funcs->are_clock_states_equal(&dc->clk_mgr->clks, &dc->current_state->bw_ctx.bw.dcn.clk))
2785 				dc->optimized_required = true;
2786 		// Else we fallback to mem compare.
2787 		} 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) {
2788 			dc->optimized_required = true;
2789 		}
2790 
2791 		dc->optimized_required |= dc->wm_optimized_required;
2792 	}
2793 
2794 	return type;
2795 }
2796 
stream_get_status(struct dc_state * ctx,struct dc_stream_state * stream)2797 static struct dc_stream_status *stream_get_status(
2798 	struct dc_state *ctx,
2799 	struct dc_stream_state *stream)
2800 {
2801 	uint8_t i;
2802 
2803 	for (i = 0; i < ctx->stream_count; i++) {
2804 		if (stream == ctx->streams[i]) {
2805 			return &ctx->stream_status[i];
2806 		}
2807 	}
2808 
2809 	return NULL;
2810 }
2811 
2812 static const enum surface_update_type update_surface_trace_level = UPDATE_TYPE_FULL;
2813 
copy_surface_update_to_plane(struct dc_plane_state * surface,struct dc_surface_update * srf_update)2814 static void copy_surface_update_to_plane(
2815 		struct dc_plane_state *surface,
2816 		struct dc_surface_update *srf_update)
2817 {
2818 	if (srf_update->flip_addr) {
2819 		surface->address = srf_update->flip_addr->address;
2820 		surface->flip_immediate =
2821 			srf_update->flip_addr->flip_immediate;
2822 		surface->time.time_elapsed_in_us[surface->time.index] =
2823 			srf_update->flip_addr->flip_timestamp_in_us -
2824 				surface->time.prev_update_time_in_us;
2825 		surface->time.prev_update_time_in_us =
2826 			srf_update->flip_addr->flip_timestamp_in_us;
2827 		surface->time.index++;
2828 		if (surface->time.index >= DC_PLANE_UPDATE_TIMES_MAX)
2829 			surface->time.index = 0;
2830 
2831 		surface->triplebuffer_flips = srf_update->flip_addr->triplebuffer_flips;
2832 	}
2833 
2834 	if (srf_update->scaling_info) {
2835 		surface->scaling_quality =
2836 				srf_update->scaling_info->scaling_quality;
2837 		surface->dst_rect =
2838 				srf_update->scaling_info->dst_rect;
2839 		surface->src_rect =
2840 				srf_update->scaling_info->src_rect;
2841 		surface->clip_rect =
2842 				srf_update->scaling_info->clip_rect;
2843 	}
2844 
2845 	if (srf_update->plane_info) {
2846 		surface->color_space =
2847 				srf_update->plane_info->color_space;
2848 		surface->format =
2849 				srf_update->plane_info->format;
2850 		surface->plane_size =
2851 				srf_update->plane_info->plane_size;
2852 		surface->rotation =
2853 				srf_update->plane_info->rotation;
2854 		surface->horizontal_mirror =
2855 				srf_update->plane_info->horizontal_mirror;
2856 		surface->stereo_format =
2857 				srf_update->plane_info->stereo_format;
2858 		surface->tiling_info =
2859 				srf_update->plane_info->tiling_info;
2860 		surface->visible =
2861 				srf_update->plane_info->visible;
2862 		surface->per_pixel_alpha =
2863 				srf_update->plane_info->per_pixel_alpha;
2864 		surface->global_alpha =
2865 				srf_update->plane_info->global_alpha;
2866 		surface->global_alpha_value =
2867 				srf_update->plane_info->global_alpha_value;
2868 		surface->dcc =
2869 				srf_update->plane_info->dcc;
2870 		surface->layer_index =
2871 				srf_update->plane_info->layer_index;
2872 	}
2873 
2874 	if (srf_update->gamma &&
2875 			(surface->gamma_correction !=
2876 					srf_update->gamma)) {
2877 		memcpy(&surface->gamma_correction->entries,
2878 			&srf_update->gamma->entries,
2879 			sizeof(struct dc_gamma_entries));
2880 		surface->gamma_correction->is_identity =
2881 			srf_update->gamma->is_identity;
2882 		surface->gamma_correction->num_entries =
2883 			srf_update->gamma->num_entries;
2884 		surface->gamma_correction->type =
2885 			srf_update->gamma->type;
2886 	}
2887 
2888 	if (srf_update->in_transfer_func &&
2889 			(surface->in_transfer_func !=
2890 				srf_update->in_transfer_func)) {
2891 		surface->in_transfer_func->sdr_ref_white_level =
2892 			srf_update->in_transfer_func->sdr_ref_white_level;
2893 		surface->in_transfer_func->tf =
2894 			srf_update->in_transfer_func->tf;
2895 		surface->in_transfer_func->type =
2896 			srf_update->in_transfer_func->type;
2897 		memcpy(&surface->in_transfer_func->tf_pts,
2898 			&srf_update->in_transfer_func->tf_pts,
2899 			sizeof(struct dc_transfer_func_distributed_points));
2900 	}
2901 
2902 	if (srf_update->func_shaper &&
2903 			(surface->in_shaper_func !=
2904 			srf_update->func_shaper))
2905 		memcpy(surface->in_shaper_func, srf_update->func_shaper,
2906 		sizeof(*surface->in_shaper_func));
2907 
2908 	if (srf_update->lut3d_func &&
2909 			(surface->lut3d_func !=
2910 			srf_update->lut3d_func))
2911 		memcpy(surface->lut3d_func, srf_update->lut3d_func,
2912 		sizeof(*surface->lut3d_func));
2913 
2914 	if (srf_update->hdr_mult.value)
2915 		surface->hdr_mult =
2916 				srf_update->hdr_mult;
2917 
2918 	if (srf_update->blend_tf &&
2919 			(surface->blend_tf !=
2920 			srf_update->blend_tf))
2921 		memcpy(surface->blend_tf, srf_update->blend_tf,
2922 		sizeof(*surface->blend_tf));
2923 
2924 	if (srf_update->input_csc_color_matrix)
2925 		surface->input_csc_color_matrix =
2926 			*srf_update->input_csc_color_matrix;
2927 
2928 	if (srf_update->coeff_reduction_factor)
2929 		surface->coeff_reduction_factor =
2930 			*srf_update->coeff_reduction_factor;
2931 
2932 	if (srf_update->gamut_remap_matrix)
2933 		surface->gamut_remap_matrix =
2934 			*srf_update->gamut_remap_matrix;
2935 }
2936 
copy_stream_update_to_stream(struct dc * dc,struct dc_state * context,struct dc_stream_state * stream,struct dc_stream_update * update)2937 static void copy_stream_update_to_stream(struct dc *dc,
2938 					 struct dc_state *context,
2939 					 struct dc_stream_state *stream,
2940 					 struct dc_stream_update *update)
2941 {
2942 	struct dc_context *dc_ctx = dc->ctx;
2943 
2944 	if (update == NULL || stream == NULL)
2945 		return;
2946 
2947 	if (update->src.height && update->src.width)
2948 		stream->src = update->src;
2949 
2950 	if (update->dst.height && update->dst.width)
2951 		stream->dst = update->dst;
2952 
2953 	if (update->out_transfer_func &&
2954 	    stream->out_transfer_func != update->out_transfer_func) {
2955 		stream->out_transfer_func->sdr_ref_white_level =
2956 			update->out_transfer_func->sdr_ref_white_level;
2957 		stream->out_transfer_func->tf = update->out_transfer_func->tf;
2958 		stream->out_transfer_func->type =
2959 			update->out_transfer_func->type;
2960 		memcpy(&stream->out_transfer_func->tf_pts,
2961 		       &update->out_transfer_func->tf_pts,
2962 		       sizeof(struct dc_transfer_func_distributed_points));
2963 	}
2964 
2965 	if (update->hdr_static_metadata)
2966 		stream->hdr_static_metadata = *update->hdr_static_metadata;
2967 
2968 	if (update->abm_level)
2969 		stream->abm_level = *update->abm_level;
2970 
2971 	if (update->periodic_interrupt)
2972 		stream->periodic_interrupt = *update->periodic_interrupt;
2973 
2974 	if (update->gamut_remap)
2975 		stream->gamut_remap_matrix = *update->gamut_remap;
2976 
2977 	/* Note: this being updated after mode set is currently not a use case
2978 	 * however if it arises OCSC would need to be reprogrammed at the
2979 	 * minimum
2980 	 */
2981 	if (update->output_color_space)
2982 		stream->output_color_space = *update->output_color_space;
2983 
2984 	if (update->output_csc_transform)
2985 		stream->csc_color_matrix = *update->output_csc_transform;
2986 
2987 	if (update->vrr_infopacket)
2988 		stream->vrr_infopacket = *update->vrr_infopacket;
2989 
2990 	if (update->allow_freesync)
2991 		stream->allow_freesync = *update->allow_freesync;
2992 
2993 	if (update->vrr_active_variable)
2994 		stream->vrr_active_variable = *update->vrr_active_variable;
2995 
2996 	if (update->crtc_timing_adjust)
2997 		stream->adjust = *update->crtc_timing_adjust;
2998 
2999 	if (update->dpms_off)
3000 		stream->dpms_off = *update->dpms_off;
3001 
3002 	if (update->hfvsif_infopacket)
3003 		stream->hfvsif_infopacket = *update->hfvsif_infopacket;
3004 
3005 	if (update->vtem_infopacket)
3006 		stream->vtem_infopacket = *update->vtem_infopacket;
3007 
3008 	if (update->vsc_infopacket)
3009 		stream->vsc_infopacket = *update->vsc_infopacket;
3010 
3011 	if (update->vsp_infopacket)
3012 		stream->vsp_infopacket = *update->vsp_infopacket;
3013 
3014 	if (update->dither_option)
3015 		stream->dither_option = *update->dither_option;
3016 
3017 	if (update->pending_test_pattern)
3018 		stream->test_pattern = *update->pending_test_pattern;
3019 	/* update current stream with writeback info */
3020 	if (update->wb_update) {
3021 		int i;
3022 
3023 		stream->num_wb_info = update->wb_update->num_wb_info;
3024 		ASSERT(stream->num_wb_info <= MAX_DWB_PIPES);
3025 		for (i = 0; i < stream->num_wb_info; i++)
3026 			stream->writeback_info[i] =
3027 				update->wb_update->writeback_info[i];
3028 	}
3029 	if (update->dsc_config) {
3030 		struct dc_dsc_config old_dsc_cfg = stream->timing.dsc_cfg;
3031 		uint32_t old_dsc_enabled = stream->timing.flags.DSC;
3032 		uint32_t enable_dsc = (update->dsc_config->num_slices_h != 0 &&
3033 				       update->dsc_config->num_slices_v != 0);
3034 
3035 		/* Use temporarry context for validating new DSC config */
3036 		struct dc_state *dsc_validate_context = dc_create_state(dc);
3037 
3038 		if (dsc_validate_context) {
3039 			dc_resource_state_copy_construct(dc->current_state, dsc_validate_context);
3040 
3041 			stream->timing.dsc_cfg = *update->dsc_config;
3042 			stream->timing.flags.DSC = enable_dsc;
3043 			if (!dc->res_pool->funcs->validate_bandwidth(dc, dsc_validate_context, true)) {
3044 				stream->timing.dsc_cfg = old_dsc_cfg;
3045 				stream->timing.flags.DSC = old_dsc_enabled;
3046 				update->dsc_config = NULL;
3047 			}
3048 
3049 			dc_release_state(dsc_validate_context);
3050 		} else {
3051 			DC_ERROR("Failed to allocate new validate context for DSC change\n");
3052 			update->dsc_config = NULL;
3053 		}
3054 	}
3055 }
3056 
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)3057 static bool update_planes_and_stream_state(struct dc *dc,
3058 		struct dc_surface_update *srf_updates, int surface_count,
3059 		struct dc_stream_state *stream,
3060 		struct dc_stream_update *stream_update,
3061 		enum surface_update_type *new_update_type,
3062 		struct dc_state **new_context)
3063 {
3064 	struct dc_state *context;
3065 	int i, j;
3066 	enum surface_update_type update_type;
3067 	const struct dc_stream_status *stream_status;
3068 	struct dc_context *dc_ctx = dc->ctx;
3069 
3070 	stream_status = dc_stream_get_status(stream);
3071 
3072 	if (!stream_status) {
3073 		if (surface_count) /* Only an error condition if surf_count non-zero*/
3074 			ASSERT(false);
3075 
3076 		return false; /* Cannot commit surface to stream that is not committed */
3077 	}
3078 
3079 	context = dc->current_state;
3080 
3081 	update_type = dc_check_update_surfaces_for_stream(
3082 			dc, srf_updates, surface_count, stream_update, stream_status);
3083 
3084 	/* update current stream with the new updates */
3085 	copy_stream_update_to_stream(dc, context, stream, stream_update);
3086 
3087 	/* do not perform surface update if surface has invalid dimensions
3088 	 * (all zero) and no scaling_info is provided
3089 	 */
3090 	if (surface_count > 0) {
3091 		for (i = 0; i < surface_count; i++) {
3092 			if ((srf_updates[i].surface->src_rect.width == 0 ||
3093 				 srf_updates[i].surface->src_rect.height == 0 ||
3094 				 srf_updates[i].surface->dst_rect.width == 0 ||
3095 				 srf_updates[i].surface->dst_rect.height == 0) &&
3096 				(!srf_updates[i].scaling_info ||
3097 				  srf_updates[i].scaling_info->src_rect.width == 0 ||
3098 				  srf_updates[i].scaling_info->src_rect.height == 0 ||
3099 				  srf_updates[i].scaling_info->dst_rect.width == 0 ||
3100 				  srf_updates[i].scaling_info->dst_rect.height == 0)) {
3101 				DC_ERROR("Invalid src/dst rects in surface update!\n");
3102 				return false;
3103 			}
3104 		}
3105 	}
3106 
3107 	if (update_type >= update_surface_trace_level)
3108 		update_surface_trace(dc, srf_updates, surface_count);
3109 
3110 	if (update_type >= UPDATE_TYPE_FULL) {
3111 		struct dc_plane_state *new_planes[MAX_SURFACES] = {0};
3112 
3113 		for (i = 0; i < surface_count; i++)
3114 			new_planes[i] = srf_updates[i].surface;
3115 
3116 		/* initialize scratch memory for building context */
3117 		context = dc_create_state(dc);
3118 		if (context == NULL) {
3119 			DC_ERROR("Failed to allocate new validate context!\n");
3120 			return false;
3121 		}
3122 
3123 		dc_resource_state_copy_construct(
3124 				dc->current_state, context);
3125 
3126 		/* For each full update, remove all existing phantom pipes first.
3127 		 * Ensures that we have enough pipes for newly added MPO planes
3128 		 */
3129 		if (dc->res_pool->funcs->remove_phantom_pipes)
3130 			dc->res_pool->funcs->remove_phantom_pipes(dc, context);
3131 
3132 		/*remove old surfaces from context */
3133 		if (!dc_rem_all_planes_for_stream(dc, stream, context)) {
3134 
3135 			BREAK_TO_DEBUGGER();
3136 			goto fail;
3137 		}
3138 
3139 		/* add surface to context */
3140 		if (!dc_add_all_planes_for_stream(dc, stream, new_planes, surface_count, context)) {
3141 
3142 			BREAK_TO_DEBUGGER();
3143 			goto fail;
3144 		}
3145 	}
3146 
3147 	/* save update parameters into surface */
3148 	for (i = 0; i < surface_count; i++) {
3149 		struct dc_plane_state *surface = srf_updates[i].surface;
3150 
3151 		copy_surface_update_to_plane(surface, &srf_updates[i]);
3152 
3153 		if (update_type >= UPDATE_TYPE_MED) {
3154 			for (j = 0; j < dc->res_pool->pipe_count; j++) {
3155 				struct pipe_ctx *pipe_ctx = &context->res_ctx.pipe_ctx[j];
3156 
3157 				if (pipe_ctx->plane_state != surface)
3158 					continue;
3159 
3160 				resource_build_scaling_params(pipe_ctx);
3161 			}
3162 		}
3163 	}
3164 
3165 	if (update_type == UPDATE_TYPE_FULL) {
3166 		if (!dc->res_pool->funcs->validate_bandwidth(dc, context, false)) {
3167 			/* For phantom pipes we remove and create a new set of phantom pipes
3168 			 * for each full update (because we don't know if we'll need phantom
3169 			 * pipes until after the first round of validation). However, if validation
3170 			 * fails we need to keep the existing phantom pipes (because we don't update
3171 			 * the dc->current_state).
3172 			 *
3173 			 * The phantom stream/plane refcount is decremented for validation because
3174 			 * we assume it'll be removed (the free comes when the dc_state is freed),
3175 			 * but if validation fails we have to increment back the refcount so it's
3176 			 * consistent.
3177 			 */
3178 			if (dc->res_pool->funcs->retain_phantom_pipes)
3179 				dc->res_pool->funcs->retain_phantom_pipes(dc, dc->current_state);
3180 			BREAK_TO_DEBUGGER();
3181 			goto fail;
3182 		}
3183 	}
3184 
3185 	*new_context = context;
3186 	*new_update_type = update_type;
3187 
3188 	return true;
3189 
3190 fail:
3191 	dc_release_state(context);
3192 
3193 	return false;
3194 
3195 }
3196 
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)3197 static void commit_planes_do_stream_update(struct dc *dc,
3198 		struct dc_stream_state *stream,
3199 		struct dc_stream_update *stream_update,
3200 		enum surface_update_type update_type,
3201 		struct dc_state *context)
3202 {
3203 	int j;
3204 
3205 	// Stream updates
3206 	for (j = 0; j < dc->res_pool->pipe_count; j++) {
3207 		struct pipe_ctx *pipe_ctx = &context->res_ctx.pipe_ctx[j];
3208 
3209 		if (!pipe_ctx->top_pipe &&  !pipe_ctx->prev_odm_pipe && pipe_ctx->stream == stream) {
3210 
3211 			if (stream_update->periodic_interrupt && dc->hwss.setup_periodic_interrupt)
3212 				dc->hwss.setup_periodic_interrupt(dc, pipe_ctx);
3213 
3214 			if ((stream_update->hdr_static_metadata && !stream->use_dynamic_meta) ||
3215 					stream_update->vrr_infopacket ||
3216 					stream_update->vsc_infopacket ||
3217 					stream_update->vsp_infopacket ||
3218 					stream_update->hfvsif_infopacket ||
3219 					stream_update->vtem_infopacket) {
3220 				resource_build_info_frame(pipe_ctx);
3221 				dc->hwss.update_info_frame(pipe_ctx);
3222 
3223 				if (dc_is_dp_signal(pipe_ctx->stream->signal))
3224 					dp_source_sequence_trace(pipe_ctx->stream->link, DPCD_SOURCE_SEQ_AFTER_UPDATE_INFO_FRAME);
3225 			}
3226 
3227 			if (stream_update->hdr_static_metadata &&
3228 					stream->use_dynamic_meta &&
3229 					dc->hwss.set_dmdata_attributes &&
3230 					pipe_ctx->stream->dmdata_address.quad_part != 0)
3231 				dc->hwss.set_dmdata_attributes(pipe_ctx);
3232 
3233 			if (stream_update->gamut_remap)
3234 				dc_stream_set_gamut_remap(dc, stream);
3235 
3236 			if (stream_update->output_csc_transform)
3237 				dc_stream_program_csc_matrix(dc, stream);
3238 
3239 			if (stream_update->dither_option) {
3240 				struct pipe_ctx *odm_pipe = pipe_ctx->next_odm_pipe;
3241 				resource_build_bit_depth_reduction_params(pipe_ctx->stream,
3242 									&pipe_ctx->stream->bit_depth_params);
3243 				pipe_ctx->stream_res.opp->funcs->opp_program_fmt(pipe_ctx->stream_res.opp,
3244 						&stream->bit_depth_params,
3245 						&stream->clamping);
3246 				while (odm_pipe) {
3247 					odm_pipe->stream_res.opp->funcs->opp_program_fmt(odm_pipe->stream_res.opp,
3248 							&stream->bit_depth_params,
3249 							&stream->clamping);
3250 					odm_pipe = odm_pipe->next_odm_pipe;
3251 				}
3252 			}
3253 
3254 
3255 			/* Full fe update*/
3256 			if (update_type == UPDATE_TYPE_FAST)
3257 				continue;
3258 
3259 			if (stream_update->dsc_config)
3260 				dp_update_dsc_config(pipe_ctx);
3261 
3262 			if (stream_update->mst_bw_update) {
3263 				if (stream_update->mst_bw_update->is_increase)
3264 					dc_link_increase_mst_payload(pipe_ctx, stream_update->mst_bw_update->mst_stream_bw);
3265 				else
3266 					dc_link_reduce_mst_payload(pipe_ctx, stream_update->mst_bw_update->mst_stream_bw);
3267 			}
3268 
3269 			if (stream_update->pending_test_pattern) {
3270 				dc_link_dp_set_test_pattern(stream->link,
3271 					stream->test_pattern.type,
3272 					stream->test_pattern.color_space,
3273 					stream->test_pattern.p_link_settings,
3274 					stream->test_pattern.p_custom_pattern,
3275 					stream->test_pattern.cust_pattern_size);
3276 			}
3277 
3278 			if (stream_update->dpms_off) {
3279 				if (*stream_update->dpms_off) {
3280 					core_link_disable_stream(pipe_ctx);
3281 					/* for dpms, keep acquired resources*/
3282 					if (pipe_ctx->stream_res.audio && !dc->debug.az_endpoint_mute_only)
3283 						pipe_ctx->stream_res.audio->funcs->az_disable(pipe_ctx->stream_res.audio);
3284 
3285 					dc->optimized_required = true;
3286 
3287 				} else {
3288 					if (get_seamless_boot_stream_count(context) == 0)
3289 						dc->hwss.prepare_bandwidth(dc, dc->current_state);
3290 					core_link_enable_stream(dc->current_state, pipe_ctx);
3291 				}
3292 			}
3293 
3294 			if (stream_update->abm_level && pipe_ctx->stream_res.abm) {
3295 				bool should_program_abm = true;
3296 
3297 				// if otg funcs defined check if blanked before programming
3298 				if (pipe_ctx->stream_res.tg->funcs->is_blanked)
3299 					if (pipe_ctx->stream_res.tg->funcs->is_blanked(pipe_ctx->stream_res.tg))
3300 						should_program_abm = false;
3301 
3302 				if (should_program_abm) {
3303 					if (*stream_update->abm_level == ABM_LEVEL_IMMEDIATE_DISABLE) {
3304 						dc->hwss.set_abm_immediate_disable(pipe_ctx);
3305 					} else {
3306 						pipe_ctx->stream_res.abm->funcs->set_abm_level(
3307 							pipe_ctx->stream_res.abm, stream->abm_level);
3308 					}
3309 				}
3310 			}
3311 		}
3312 	}
3313 }
3314 
dc_dmub_should_send_dirty_rect_cmd(struct dc * dc,struct dc_stream_state * stream)3315 static bool dc_dmub_should_send_dirty_rect_cmd(struct dc *dc, struct dc_stream_state *stream)
3316 {
3317 	if ((stream->link->psr_settings.psr_version == DC_PSR_VERSION_SU_1
3318 			|| stream->link->psr_settings.psr_version == DC_PSR_VERSION_1)
3319 			&& stream->ctx->dce_version >= DCN_VERSION_3_1)
3320 		return true;
3321 
3322 	return false;
3323 }
3324 
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)3325 void dc_dmub_update_dirty_rect(struct dc *dc,
3326 			       int surface_count,
3327 			       struct dc_stream_state *stream,
3328 			       struct dc_surface_update *srf_updates,
3329 			       struct dc_state *context)
3330 {
3331 	union dmub_rb_cmd cmd;
3332 	struct dc_context *dc_ctx = dc->ctx;
3333 	struct dmub_cmd_update_dirty_rect_data *update_dirty_rect;
3334 	unsigned int i, j;
3335 	unsigned int panel_inst = 0;
3336 
3337 	if (!dc_dmub_should_send_dirty_rect_cmd(dc, stream))
3338 		return;
3339 
3340 	if (!dc_get_edp_link_panel_inst(dc, stream->link, &panel_inst))
3341 		return;
3342 
3343 	memset(&cmd, 0x0, sizeof(cmd));
3344 	cmd.update_dirty_rect.header.type = DMUB_CMD__UPDATE_DIRTY_RECT;
3345 	cmd.update_dirty_rect.header.sub_type = 0;
3346 	cmd.update_dirty_rect.header.payload_bytes =
3347 		sizeof(cmd.update_dirty_rect) -
3348 		sizeof(cmd.update_dirty_rect.header);
3349 	update_dirty_rect = &cmd.update_dirty_rect.update_dirty_rect_data;
3350 	for (i = 0; i < surface_count; i++) {
3351 		struct dc_plane_state *plane_state = srf_updates[i].surface;
3352 		const struct dc_flip_addrs *flip_addr = srf_updates[i].flip_addr;
3353 
3354 		if (!srf_updates[i].surface || !flip_addr)
3355 			continue;
3356 		/* Do not send in immediate flip mode */
3357 		if (srf_updates[i].surface->flip_immediate)
3358 			continue;
3359 
3360 		update_dirty_rect->dirty_rect_count = flip_addr->dirty_rect_count;
3361 		memcpy(update_dirty_rect->src_dirty_rects, flip_addr->dirty_rects,
3362 				sizeof(flip_addr->dirty_rects));
3363 		for (j = 0; j < dc->res_pool->pipe_count; j++) {
3364 			struct pipe_ctx *pipe_ctx = &context->res_ctx.pipe_ctx[j];
3365 
3366 			if (pipe_ctx->stream != stream)
3367 				continue;
3368 			if (pipe_ctx->plane_state != plane_state)
3369 				continue;
3370 
3371 			update_dirty_rect->panel_inst = panel_inst;
3372 			update_dirty_rect->pipe_idx = j;
3373 			dc_dmub_srv_cmd_queue(dc_ctx->dmub_srv, &cmd);
3374 			dc_dmub_srv_cmd_execute(dc_ctx->dmub_srv);
3375 		}
3376 	}
3377 }
3378 
wait_for_outstanding_hw_updates(struct dc * dc,const struct dc_state * dc_context)3379 static void wait_for_outstanding_hw_updates(struct dc *dc, const struct dc_state *dc_context)
3380 {
3381 /*
3382  * This function calls HWSS to wait for any potentially double buffered
3383  * operations to complete. It should be invoked as a pre-amble prior
3384  * to full update programming before asserting any HW locks.
3385  */
3386 	int pipe_idx;
3387 	int opp_inst;
3388 	int opp_count = dc->res_pool->pipe_count;
3389 	struct hubp *hubp;
3390 	int mpcc_inst;
3391 	const struct pipe_ctx *pipe_ctx;
3392 
3393 	for (pipe_idx = 0; pipe_idx < dc->res_pool->pipe_count; pipe_idx++) {
3394 		pipe_ctx = &dc_context->res_ctx.pipe_ctx[pipe_idx];
3395 
3396 		if (!pipe_ctx->stream)
3397 			continue;
3398 
3399 		if (pipe_ctx->stream_res.tg->funcs->wait_drr_doublebuffer_pending_clear)
3400 			pipe_ctx->stream_res.tg->funcs->wait_drr_doublebuffer_pending_clear(pipe_ctx->stream_res.tg);
3401 
3402 		hubp = pipe_ctx->plane_res.hubp;
3403 		if (!hubp)
3404 			continue;
3405 
3406 		mpcc_inst = hubp->inst;
3407 		// MPCC inst is equal to pipe index in practice
3408 		for (opp_inst = 0; opp_inst < opp_count; opp_inst++) {
3409 			if (dc->res_pool->opps[opp_inst]->mpcc_disconnect_pending[mpcc_inst]) {
3410 				dc->res_pool->mpc->funcs->wait_for_idle(dc->res_pool->mpc, mpcc_inst);
3411 				dc->res_pool->opps[opp_inst]->mpcc_disconnect_pending[mpcc_inst] = false;
3412 				break;
3413 			}
3414 		}
3415 	}
3416 }
3417 
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)3418 static void commit_planes_for_stream(struct dc *dc,
3419 		struct dc_surface_update *srf_updates,
3420 		int surface_count,
3421 		struct dc_stream_state *stream,
3422 		struct dc_stream_update *stream_update,
3423 		enum surface_update_type update_type,
3424 		struct dc_state *context)
3425 {
3426 	int i, j;
3427 	struct pipe_ctx *top_pipe_to_program = NULL;
3428 	bool should_lock_all_pipes = (update_type != UPDATE_TYPE_FAST);
3429 	bool subvp_prev_use = false;
3430 
3431 	// Once we apply the new subvp context to hardware it won't be in the
3432 	// dc->current_state anymore, so we have to cache it before we apply
3433 	// the new SubVP context
3434 	subvp_prev_use = false;
3435 	dc_z10_restore(dc);
3436 	if (update_type == UPDATE_TYPE_FULL)
3437 		wait_for_outstanding_hw_updates(dc, context);
3438 
3439 	if (get_seamless_boot_stream_count(context) > 0 && surface_count > 0) {
3440 		/* Optimize seamless boot flag keeps clocks and watermarks high until
3441 		 * first flip. After first flip, optimization is required to lower
3442 		 * bandwidth. Important to note that it is expected UEFI will
3443 		 * only light up a single display on POST, therefore we only expect
3444 		 * one stream with seamless boot flag set.
3445 		 */
3446 		if (stream->apply_seamless_boot_optimization) {
3447 			stream->apply_seamless_boot_optimization = false;
3448 
3449 			if (get_seamless_boot_stream_count(context) == 0)
3450 				dc->optimized_required = true;
3451 		}
3452 	}
3453 
3454 	if (update_type == UPDATE_TYPE_FULL) {
3455 		dc_allow_idle_optimizations(dc, false);
3456 
3457 		if (get_seamless_boot_stream_count(context) == 0)
3458 			dc->hwss.prepare_bandwidth(dc, context);
3459 
3460 		if (dc->hwss.update_dsc_pg)
3461 			dc->hwss.update_dsc_pg(dc, context, false);
3462 
3463 		context_clock_trace(dc, context);
3464 	}
3465 
3466 	for (j = 0; j < dc->res_pool->pipe_count; j++) {
3467 		struct pipe_ctx *pipe_ctx = &context->res_ctx.pipe_ctx[j];
3468 
3469 		if (!pipe_ctx->top_pipe &&
3470 			!pipe_ctx->prev_odm_pipe &&
3471 			pipe_ctx->stream &&
3472 			pipe_ctx->stream == stream) {
3473 			top_pipe_to_program = pipe_ctx;
3474 		}
3475 	}
3476 
3477 	for (i = 0; i < dc->res_pool->pipe_count; i++) {
3478 		struct pipe_ctx *old_pipe = &dc->current_state->res_ctx.pipe_ctx[i];
3479 
3480 		// Check old context for SubVP
3481 		subvp_prev_use |= (old_pipe->stream && old_pipe->stream->mall_stream_config.type == SUBVP_PHANTOM);
3482 		if (subvp_prev_use)
3483 			break;
3484 	}
3485 
3486 	if (stream->test_pattern.type != DP_TEST_PATTERN_VIDEO_MODE) {
3487 		struct pipe_ctx *mpcc_pipe;
3488 		struct pipe_ctx *odm_pipe;
3489 
3490 		for (mpcc_pipe = top_pipe_to_program; mpcc_pipe; mpcc_pipe = mpcc_pipe->bottom_pipe)
3491 			for (odm_pipe = mpcc_pipe; odm_pipe; odm_pipe = odm_pipe->next_odm_pipe)
3492 				odm_pipe->ttu_regs.min_ttu_vblank = MAX_TTU;
3493 	}
3494 
3495 	if ((update_type != UPDATE_TYPE_FAST) && stream->update_flags.bits.dsc_changed)
3496 		if (top_pipe_to_program &&
3497 			top_pipe_to_program->stream_res.tg->funcs->lock_doublebuffer_enable) {
3498 			if (should_use_dmub_lock(stream->link)) {
3499 				union dmub_hw_lock_flags hw_locks = { 0 };
3500 				struct dmub_hw_lock_inst_flags inst_flags = { 0 };
3501 
3502 				hw_locks.bits.lock_dig = 1;
3503 				inst_flags.dig_inst = top_pipe_to_program->stream_res.tg->inst;
3504 
3505 				dmub_hw_lock_mgr_cmd(dc->ctx->dmub_srv,
3506 							true,
3507 							&hw_locks,
3508 							&inst_flags);
3509 			} else
3510 				top_pipe_to_program->stream_res.tg->funcs->lock_doublebuffer_enable(
3511 						top_pipe_to_program->stream_res.tg);
3512 		}
3513 
3514 	if (should_lock_all_pipes && dc->hwss.interdependent_update_lock) {
3515 		if (dc->hwss.subvp_pipe_control_lock)
3516 				dc->hwss.subvp_pipe_control_lock(dc, context, true, should_lock_all_pipes, NULL, subvp_prev_use);
3517 		dc->hwss.interdependent_update_lock(dc, context, true);
3518 
3519 	} else {
3520 		if (dc->hwss.subvp_pipe_control_lock)
3521 			dc->hwss.subvp_pipe_control_lock(dc, context, true, should_lock_all_pipes, top_pipe_to_program, subvp_prev_use);
3522 		/* Lock the top pipe while updating plane addrs, since freesync requires
3523 		 *  plane addr update event triggers to be synchronized.
3524 		 *  top_pipe_to_program is expected to never be NULL
3525 		 */
3526 		dc->hwss.pipe_control_lock(dc, top_pipe_to_program, true);
3527 	}
3528 
3529 	if (update_type != UPDATE_TYPE_FAST) {
3530 		for (i = 0; i < dc->res_pool->pipe_count; i++) {
3531 			struct pipe_ctx *new_pipe = &context->res_ctx.pipe_ctx[i];
3532 
3533 			if ((new_pipe->stream && new_pipe->stream->mall_stream_config.type == SUBVP_PHANTOM) ||
3534 					subvp_prev_use) {
3535 				// If old context or new context has phantom pipes, apply
3536 				// the phantom timings now. We can't change the phantom
3537 				// pipe configuration safely without driver acquiring
3538 				// the DMCUB lock first.
3539 				dc->hwss.apply_ctx_to_hw(dc, context);
3540 				break;
3541 			}
3542 		}
3543 	}
3544 
3545 	dc_dmub_update_dirty_rect(dc, surface_count, stream, srf_updates, context);
3546 
3547 	if (update_type != UPDATE_TYPE_FAST) {
3548 		for (i = 0; i < dc->res_pool->pipe_count; i++) {
3549 			struct pipe_ctx *new_pipe = &context->res_ctx.pipe_ctx[i];
3550 
3551 			if ((new_pipe->stream && new_pipe->stream->mall_stream_config.type == SUBVP_PHANTOM) ||
3552 					subvp_prev_use) {
3553 				// If old context or new context has phantom pipes, apply
3554 				// the phantom timings now. We can't change the phantom
3555 				// pipe configuration safely without driver acquiring
3556 				// the DMCUB lock first.
3557 				dc->hwss.apply_ctx_to_hw(dc, context);
3558 				break;
3559 			}
3560 		}
3561 	}
3562 
3563 	// Stream updates
3564 	if (stream_update)
3565 		commit_planes_do_stream_update(dc, stream, stream_update, update_type, context);
3566 
3567 	if (surface_count == 0) {
3568 		/*
3569 		 * In case of turning off screen, no need to program front end a second time.
3570 		 * just return after program blank.
3571 		 */
3572 		if (dc->hwss.apply_ctx_for_surface)
3573 			dc->hwss.apply_ctx_for_surface(dc, stream, 0, context);
3574 		if (dc->hwss.program_front_end_for_ctx)
3575 			dc->hwss.program_front_end_for_ctx(dc, context);
3576 
3577 		if (should_lock_all_pipes && dc->hwss.interdependent_update_lock) {
3578 			dc->hwss.interdependent_update_lock(dc, context, false);
3579 		} else {
3580 			dc->hwss.pipe_control_lock(dc, top_pipe_to_program, false);
3581 		}
3582 		dc->hwss.post_unlock_program_front_end(dc, context);
3583 
3584 		if (update_type != UPDATE_TYPE_FAST)
3585 			if (dc->hwss.commit_subvp_config)
3586 				dc->hwss.commit_subvp_config(dc, context);
3587 
3588 		/* Since phantom pipe programming is moved to post_unlock_program_front_end,
3589 		 * move the SubVP lock to after the phantom pipes have been setup
3590 		 */
3591 		if (should_lock_all_pipes && dc->hwss.interdependent_update_lock) {
3592 			if (dc->hwss.subvp_pipe_control_lock)
3593 				dc->hwss.subvp_pipe_control_lock(dc, context, false, should_lock_all_pipes, NULL, subvp_prev_use);
3594 		} else {
3595 			if (dc->hwss.subvp_pipe_control_lock)
3596 				dc->hwss.subvp_pipe_control_lock(dc, context, false, should_lock_all_pipes, NULL, subvp_prev_use);
3597 		}
3598 
3599 		return;
3600 	}
3601 
3602 	if (!IS_DIAG_DC(dc->ctx->dce_environment)) {
3603 		for (i = 0; i < surface_count; i++) {
3604 			struct dc_plane_state *plane_state = srf_updates[i].surface;
3605 			/*set logical flag for lock/unlock use*/
3606 			for (j = 0; j < dc->res_pool->pipe_count; j++) {
3607 				struct pipe_ctx *pipe_ctx = &context->res_ctx.pipe_ctx[j];
3608 				if (!pipe_ctx->plane_state)
3609 					continue;
3610 				if (should_update_pipe_for_plane(context, pipe_ctx, plane_state))
3611 					continue;
3612 				pipe_ctx->plane_state->triplebuffer_flips = false;
3613 				if (update_type == UPDATE_TYPE_FAST &&
3614 					dc->hwss.program_triplebuffer != NULL &&
3615 					!pipe_ctx->plane_state->flip_immediate && dc->debug.enable_tri_buf) {
3616 						/*triple buffer for VUpdate  only*/
3617 						pipe_ctx->plane_state->triplebuffer_flips = true;
3618 				}
3619 			}
3620 			if (update_type == UPDATE_TYPE_FULL) {
3621 				/* force vsync flip when reconfiguring pipes to prevent underflow */
3622 				plane_state->flip_immediate = false;
3623 			}
3624 		}
3625 	}
3626 
3627 	// Update Type FULL, Surface updates
3628 	for (j = 0; j < dc->res_pool->pipe_count; j++) {
3629 		struct pipe_ctx *pipe_ctx = &context->res_ctx.pipe_ctx[j];
3630 
3631 		if (!pipe_ctx->top_pipe &&
3632 			!pipe_ctx->prev_odm_pipe &&
3633 			should_update_pipe_for_stream(context, pipe_ctx, stream)) {
3634 			struct dc_stream_status *stream_status = NULL;
3635 
3636 			if (!pipe_ctx->plane_state)
3637 				continue;
3638 
3639 			/* Full fe update*/
3640 			if (update_type == UPDATE_TYPE_FAST)
3641 				continue;
3642 
3643 			ASSERT(!pipe_ctx->plane_state->triplebuffer_flips);
3644 
3645 			if (dc->hwss.program_triplebuffer != NULL && dc->debug.enable_tri_buf) {
3646 				/*turn off triple buffer for full update*/
3647 				dc->hwss.program_triplebuffer(
3648 					dc, pipe_ctx, pipe_ctx->plane_state->triplebuffer_flips);
3649 			}
3650 			stream_status =
3651 				stream_get_status(context, pipe_ctx->stream);
3652 
3653 			if (dc->hwss.apply_ctx_for_surface)
3654 				dc->hwss.apply_ctx_for_surface(
3655 					dc, pipe_ctx->stream, stream_status->plane_count, context);
3656 		}
3657 	}
3658 	if (dc->hwss.program_front_end_for_ctx && update_type != UPDATE_TYPE_FAST) {
3659 		dc->hwss.program_front_end_for_ctx(dc, context);
3660 		if (dc->debug.validate_dml_output) {
3661 			for (i = 0; i < dc->res_pool->pipe_count; i++) {
3662 				struct pipe_ctx *cur_pipe = &context->res_ctx.pipe_ctx[i];
3663 				if (cur_pipe->stream == NULL)
3664 					continue;
3665 
3666 				cur_pipe->plane_res.hubp->funcs->validate_dml_output(
3667 						cur_pipe->plane_res.hubp, dc->ctx,
3668 						&context->res_ctx.pipe_ctx[i].rq_regs,
3669 						&context->res_ctx.pipe_ctx[i].dlg_regs,
3670 						&context->res_ctx.pipe_ctx[i].ttu_regs);
3671 			}
3672 		}
3673 	}
3674 
3675 	// Update Type FAST, Surface updates
3676 	if (update_type == UPDATE_TYPE_FAST) {
3677 		if (dc->hwss.set_flip_control_gsl)
3678 			for (i = 0; i < surface_count; i++) {
3679 				struct dc_plane_state *plane_state = srf_updates[i].surface;
3680 
3681 				for (j = 0; j < dc->res_pool->pipe_count; j++) {
3682 					struct pipe_ctx *pipe_ctx = &context->res_ctx.pipe_ctx[j];
3683 
3684 					if (!should_update_pipe_for_stream(context, pipe_ctx, stream))
3685 						continue;
3686 
3687 					if (!should_update_pipe_for_plane(context, pipe_ctx, plane_state))
3688 						continue;
3689 
3690 					// GSL has to be used for flip immediate
3691 					dc->hwss.set_flip_control_gsl(pipe_ctx,
3692 							pipe_ctx->plane_state->flip_immediate);
3693 				}
3694 			}
3695 
3696 		/* Perform requested Updates */
3697 		for (i = 0; i < surface_count; i++) {
3698 			struct dc_plane_state *plane_state = srf_updates[i].surface;
3699 
3700 			for (j = 0; j < dc->res_pool->pipe_count; j++) {
3701 				struct pipe_ctx *pipe_ctx = &context->res_ctx.pipe_ctx[j];
3702 
3703 				if (!should_update_pipe_for_stream(context, pipe_ctx, stream))
3704 					continue;
3705 
3706 				if (!should_update_pipe_for_plane(context, pipe_ctx, plane_state))
3707 					continue;
3708 
3709 				/*program triple buffer after lock based on flip type*/
3710 				if (dc->hwss.program_triplebuffer != NULL && dc->debug.enable_tri_buf) {
3711 					/*only enable triplebuffer for  fast_update*/
3712 					dc->hwss.program_triplebuffer(
3713 						dc, pipe_ctx, pipe_ctx->plane_state->triplebuffer_flips);
3714 				}
3715 				if (pipe_ctx->plane_state->update_flags.bits.addr_update)
3716 					dc->hwss.update_plane_addr(dc, pipe_ctx);
3717 			}
3718 		}
3719 
3720 	}
3721 
3722 	if (should_lock_all_pipes && dc->hwss.interdependent_update_lock) {
3723 		dc->hwss.interdependent_update_lock(dc, context, false);
3724 	} else {
3725 		dc->hwss.pipe_control_lock(dc, top_pipe_to_program, false);
3726 	}
3727 
3728 	if ((update_type != UPDATE_TYPE_FAST) && stream->update_flags.bits.dsc_changed)
3729 		if (top_pipe_to_program->stream_res.tg->funcs->lock_doublebuffer_enable) {
3730 			top_pipe_to_program->stream_res.tg->funcs->wait_for_state(
3731 				top_pipe_to_program->stream_res.tg,
3732 				CRTC_STATE_VACTIVE);
3733 			top_pipe_to_program->stream_res.tg->funcs->wait_for_state(
3734 				top_pipe_to_program->stream_res.tg,
3735 				CRTC_STATE_VBLANK);
3736 			top_pipe_to_program->stream_res.tg->funcs->wait_for_state(
3737 				top_pipe_to_program->stream_res.tg,
3738 				CRTC_STATE_VACTIVE);
3739 
3740 			if (should_use_dmub_lock(stream->link)) {
3741 				union dmub_hw_lock_flags hw_locks = { 0 };
3742 				struct dmub_hw_lock_inst_flags inst_flags = { 0 };
3743 
3744 				hw_locks.bits.lock_dig = 1;
3745 				inst_flags.dig_inst = top_pipe_to_program->stream_res.tg->inst;
3746 
3747 				dmub_hw_lock_mgr_cmd(dc->ctx->dmub_srv,
3748 							false,
3749 							&hw_locks,
3750 							&inst_flags);
3751 			} else
3752 				top_pipe_to_program->stream_res.tg->funcs->lock_doublebuffer_disable(
3753 					top_pipe_to_program->stream_res.tg);
3754 		}
3755 
3756 	if (update_type != UPDATE_TYPE_FAST)
3757 		dc->hwss.post_unlock_program_front_end(dc, context);
3758 	if (update_type != UPDATE_TYPE_FAST)
3759 		if (dc->hwss.commit_subvp_config)
3760 			dc->hwss.commit_subvp_config(dc, context);
3761 
3762 	if (update_type != UPDATE_TYPE_FAST)
3763 		if (dc->hwss.commit_subvp_config)
3764 			dc->hwss.commit_subvp_config(dc, context);
3765 
3766 	/* Since phantom pipe programming is moved to post_unlock_program_front_end,
3767 	 * move the SubVP lock to after the phantom pipes have been setup
3768 	 */
3769 	if (should_lock_all_pipes && dc->hwss.interdependent_update_lock) {
3770 		if (dc->hwss.subvp_pipe_control_lock)
3771 			dc->hwss.subvp_pipe_control_lock(dc, context, false, should_lock_all_pipes, NULL, subvp_prev_use);
3772 	} else {
3773 		if (dc->hwss.subvp_pipe_control_lock)
3774 			dc->hwss.subvp_pipe_control_lock(dc, context, false, should_lock_all_pipes, top_pipe_to_program, subvp_prev_use);
3775 	}
3776 
3777 	// Fire manual trigger only when bottom plane is flipped
3778 	for (j = 0; j < dc->res_pool->pipe_count; j++) {
3779 		struct pipe_ctx *pipe_ctx = &context->res_ctx.pipe_ctx[j];
3780 
3781 		if (!pipe_ctx->plane_state)
3782 			continue;
3783 
3784 		if (pipe_ctx->bottom_pipe || pipe_ctx->next_odm_pipe ||
3785 				!pipe_ctx->stream || !should_update_pipe_for_stream(context, pipe_ctx, stream) ||
3786 				!pipe_ctx->plane_state->update_flags.bits.addr_update ||
3787 				pipe_ctx->plane_state->skip_manual_trigger)
3788 			continue;
3789 
3790 		if (pipe_ctx->stream_res.tg->funcs->program_manual_trigger)
3791 			pipe_ctx->stream_res.tg->funcs->program_manual_trigger(pipe_ctx->stream_res.tg);
3792 	}
3793 }
3794 
3795 /* Determines if the incoming context requires a applying transition state with unnecessary
3796  * pipe splitting and ODM disabled, due to hardware limitations. In a case where
3797  * the OPP associated with an MPCC might change due to plane additions, this function
3798  * returns true.
3799  */
could_mpcc_tree_change_for_active_pipes(struct dc * dc,struct dc_stream_state * stream,int surface_count,bool * is_plane_addition)3800 static bool could_mpcc_tree_change_for_active_pipes(struct dc *dc,
3801 		struct dc_stream_state *stream,
3802 		int surface_count,
3803 		bool *is_plane_addition)
3804 {
3805 
3806 	struct dc_stream_status *cur_stream_status = stream_get_status(dc->current_state, stream);
3807 	bool force_minimal_pipe_splitting = false;
3808 
3809 	*is_plane_addition = false;
3810 
3811 	if (cur_stream_status &&
3812 			dc->current_state->stream_count > 0 &&
3813 			dc->debug.pipe_split_policy != MPC_SPLIT_AVOID) {
3814 		/* determine if minimal transition is required due to MPC*/
3815 		if (surface_count > 0) {
3816 			if (cur_stream_status->plane_count > surface_count) {
3817 				force_minimal_pipe_splitting = true;
3818 			} else if (cur_stream_status->plane_count < surface_count) {
3819 				force_minimal_pipe_splitting = true;
3820 				*is_plane_addition = true;
3821 			}
3822 		}
3823 	}
3824 
3825 	if (cur_stream_status &&
3826 			dc->current_state->stream_count == 1 &&
3827 			dc->debug.enable_single_display_2to1_odm_policy) {
3828 		/* determine if minimal transition is required due to dynamic ODM*/
3829 		if (surface_count > 0) {
3830 			if (cur_stream_status->plane_count > 2 && cur_stream_status->plane_count > surface_count) {
3831 				force_minimal_pipe_splitting = true;
3832 			} else if (surface_count > 2 && cur_stream_status->plane_count < surface_count) {
3833 				force_minimal_pipe_splitting = true;
3834 				*is_plane_addition = true;
3835 			}
3836 		}
3837 	}
3838 
3839 	/* For SubVP when adding or removing planes we need to add a minimal transition
3840 	 * (even when disabling all planes). Whenever disabling a phantom pipe, we
3841 	 * must use the minimal transition path to disable the pipe correctly.
3842 	 */
3843 	if (cur_stream_status && stream->mall_stream_config.type == SUBVP_MAIN) {
3844 		/* determine if minimal transition is required due to SubVP*/
3845 		if (cur_stream_status->plane_count > surface_count) {
3846 			force_minimal_pipe_splitting = true;
3847 		} else if (cur_stream_status->plane_count < surface_count) {
3848 			force_minimal_pipe_splitting = true;
3849 			*is_plane_addition = true;
3850 		}
3851 	}
3852 
3853 	return force_minimal_pipe_splitting;
3854 }
3855 
commit_minimal_transition_state(struct dc * dc,struct dc_state * transition_base_context)3856 static bool commit_minimal_transition_state(struct dc *dc,
3857 		struct dc_state *transition_base_context)
3858 {
3859 	struct dc_state *transition_context = dc_create_state(dc);
3860 	enum pipe_split_policy tmp_mpc_policy;
3861 	bool temp_dynamic_odm_policy;
3862 	bool temp_subvp_policy;
3863 	enum dc_status ret = DC_ERROR_UNEXPECTED;
3864 	unsigned int i, j;
3865 	unsigned int pipe_in_use = 0;
3866 	bool subvp_in_use = false;
3867 	bool odm_in_use = false;
3868 
3869 	if (!transition_context)
3870 		return false;
3871 
3872 	/* check current pipes in use*/
3873 	for (i = 0; i < dc->res_pool->pipe_count; i++) {
3874 		struct pipe_ctx *pipe = &transition_base_context->res_ctx.pipe_ctx[i];
3875 
3876 		if (pipe->plane_state)
3877 			pipe_in_use++;
3878 	}
3879 
3880 	/* If SubVP is enabled and we are adding or removing planes from any main subvp
3881 	 * pipe, we must use the minimal transition.
3882 	 */
3883 	for (i = 0; i < dc->res_pool->pipe_count; i++) {
3884 		struct pipe_ctx *pipe = &dc->current_state->res_ctx.pipe_ctx[i];
3885 
3886 		if (pipe->stream && pipe->stream->mall_stream_config.type == SUBVP_PHANTOM) {
3887 			subvp_in_use = true;
3888 			break;
3889 		}
3890 	}
3891 
3892 	/* If ODM is enabled and we are adding or removing planes from any ODM
3893 	 * pipe, we must use the minimal transition.
3894 	 */
3895 	for (i = 0; i < dc->res_pool->pipe_count; i++) {
3896 		struct pipe_ctx *pipe = &dc->current_state->res_ctx.pipe_ctx[i];
3897 
3898 		if (pipe->stream && pipe->next_odm_pipe) {
3899 			odm_in_use = true;
3900 			break;
3901 		}
3902 	}
3903 
3904 	/* When the OS add a new surface if we have been used all of pipes with odm combine
3905 	 * and mpc split feature, it need use commit_minimal_transition_state to transition safely.
3906 	 * After OS exit MPO, it will back to use odm and mpc split with all of pipes, we need
3907 	 * call it again. Otherwise return true to skip.
3908 	 *
3909 	 * Reduce the scenarios to use dc_commit_state_no_check in the stage of flip. Especially
3910 	 * enter/exit MPO when DCN still have enough resources.
3911 	 */
3912 	if (pipe_in_use != dc->res_pool->pipe_count && !subvp_in_use && !odm_in_use) {
3913 		dc_release_state(transition_context);
3914 		return true;
3915 	}
3916 
3917 	if (!dc->config.is_vmin_only_asic) {
3918 		tmp_mpc_policy = dc->debug.pipe_split_policy;
3919 		dc->debug.pipe_split_policy = MPC_SPLIT_AVOID;
3920 	}
3921 
3922 	temp_dynamic_odm_policy = dc->debug.enable_single_display_2to1_odm_policy;
3923 	dc->debug.enable_single_display_2to1_odm_policy = false;
3924 
3925 	temp_subvp_policy = dc->debug.force_disable_subvp;
3926 	dc->debug.force_disable_subvp = true;
3927 
3928 	dc_resource_state_copy_construct(transition_base_context, transition_context);
3929 
3930 	//commit minimal state
3931 	if (dc->res_pool->funcs->validate_bandwidth(dc, transition_context, false)) {
3932 		for (i = 0; i < transition_context->stream_count; i++) {
3933 			struct dc_stream_status *stream_status = &transition_context->stream_status[i];
3934 
3935 			for (j = 0; j < stream_status->plane_count; j++) {
3936 				struct dc_plane_state *plane_state = stream_status->plane_states[j];
3937 
3938 				/* force vsync flip when reconfiguring pipes to prevent underflow
3939 				 * and corruption
3940 				 */
3941 				plane_state->flip_immediate = false;
3942 			}
3943 		}
3944 
3945 		ret = dc_commit_state_no_check(dc, transition_context);
3946 	}
3947 
3948 	/*always release as dc_commit_state_no_check retains in good case*/
3949 	dc_release_state(transition_context);
3950 
3951 	/*restore previous pipe split and odm policy*/
3952 	if (!dc->config.is_vmin_only_asic)
3953 		dc->debug.pipe_split_policy = tmp_mpc_policy;
3954 
3955 	dc->debug.enable_single_display_2to1_odm_policy = temp_dynamic_odm_policy;
3956 	dc->debug.force_disable_subvp = temp_subvp_policy;
3957 
3958 	if (ret != DC_OK) {
3959 		/*this should never happen*/
3960 		BREAK_TO_DEBUGGER();
3961 		return false;
3962 	}
3963 
3964 	/*force full surface update*/
3965 	for (i = 0; i < dc->current_state->stream_count; i++) {
3966 		for (j = 0; j < dc->current_state->stream_status[i].plane_count; j++) {
3967 			dc->current_state->stream_status[i].plane_states[j]->update_flags.raw = 0xFFFFFFFF;
3968 		}
3969 	}
3970 
3971 	return true;
3972 }
3973 
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)3974 bool dc_update_planes_and_stream(struct dc *dc,
3975 		struct dc_surface_update *srf_updates, int surface_count,
3976 		struct dc_stream_state *stream,
3977 		struct dc_stream_update *stream_update)
3978 {
3979 	struct dc_state *context;
3980 	enum surface_update_type update_type;
3981 	int i;
3982 
3983 	/* In cases where MPO and split or ODM are used transitions can
3984 	 * cause underflow. Apply stream configuration with minimal pipe
3985 	 * split first to avoid unsupported transitions for active pipes.
3986 	 */
3987 	bool force_minimal_pipe_splitting;
3988 	bool is_plane_addition;
3989 
3990 	force_minimal_pipe_splitting = could_mpcc_tree_change_for_active_pipes(
3991 			dc,
3992 			stream,
3993 			surface_count,
3994 			&is_plane_addition);
3995 
3996 	/* on plane addition, minimal state is the current one */
3997 	if (force_minimal_pipe_splitting && is_plane_addition &&
3998 		!commit_minimal_transition_state(dc, dc->current_state))
3999 				return false;
4000 
4001 	if (!update_planes_and_stream_state(
4002 			dc,
4003 			srf_updates,
4004 			surface_count,
4005 			stream,
4006 			stream_update,
4007 			&update_type,
4008 			&context))
4009 		return false;
4010 
4011 	/* on plane removal, minimal state is the new one */
4012 	if (force_minimal_pipe_splitting && !is_plane_addition) {
4013 		if (!commit_minimal_transition_state(dc, context)) {
4014 			dc_release_state(context);
4015 			return false;
4016 		}
4017 
4018 		update_type = UPDATE_TYPE_FULL;
4019 	}
4020 
4021 	commit_planes_for_stream(
4022 			dc,
4023 			srf_updates,
4024 			surface_count,
4025 			stream,
4026 			stream_update,
4027 			update_type,
4028 			context);
4029 
4030 	if (dc->current_state != context) {
4031 
4032 		/* Since memory free requires elevated IRQL, an interrupt
4033 		 * request is generated by mem free. If this happens
4034 		 * between freeing and reassigning the context, our vsync
4035 		 * interrupt will call into dc and cause a memory
4036 		 * corruption BSOD. Hence, we first reassign the context,
4037 		 * then free the old context.
4038 		 */
4039 
4040 		struct dc_state *old = dc->current_state;
4041 
4042 		dc->current_state = context;
4043 		dc_release_state(old);
4044 
4045 		// clear any forced full updates
4046 		for (i = 0; i < dc->res_pool->pipe_count; i++) {
4047 			struct pipe_ctx *pipe_ctx = &context->res_ctx.pipe_ctx[i];
4048 
4049 			if (pipe_ctx->plane_state && pipe_ctx->stream == stream)
4050 				pipe_ctx->plane_state->force_full_update = false;
4051 		}
4052 	}
4053 	return true;
4054 }
4055 
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)4056 void dc_commit_updates_for_stream(struct dc *dc,
4057 		struct dc_surface_update *srf_updates,
4058 		int surface_count,
4059 		struct dc_stream_state *stream,
4060 		struct dc_stream_update *stream_update,
4061 		struct dc_state *state)
4062 {
4063 	const struct dc_stream_status *stream_status;
4064 	enum surface_update_type update_type;
4065 	struct dc_state *context;
4066 	struct dc_context *dc_ctx = dc->ctx;
4067 	int i, j;
4068 
4069 	/* TODO: Since change commit sequence can have a huge impact,
4070 	 * we decided to only enable it for DCN3x. However, as soon as
4071 	 * we get more confident about this change we'll need to enable
4072 	 * the new sequence for all ASICs.
4073 	 */
4074 	if (dc->ctx->dce_version >= DCN_VERSION_3_2) {
4075 		dc_update_planes_and_stream(dc, srf_updates,
4076 					    surface_count, stream,
4077 					    stream_update);
4078 		return;
4079 	}
4080 
4081 	stream_status = dc_stream_get_status(stream);
4082 	context = dc->current_state;
4083 
4084 	update_type = dc_check_update_surfaces_for_stream(
4085 				dc, srf_updates, surface_count, stream_update, stream_status);
4086 
4087 	if (update_type >= update_surface_trace_level)
4088 		update_surface_trace(dc, srf_updates, surface_count);
4089 
4090 
4091 	if (update_type >= UPDATE_TYPE_FULL) {
4092 
4093 		/* initialize scratch memory for building context */
4094 		context = dc_create_state(dc);
4095 		if (context == NULL) {
4096 			DC_ERROR("Failed to allocate new validate context!\n");
4097 			return;
4098 		}
4099 
4100 		dc_resource_state_copy_construct(state, context);
4101 
4102 		for (i = 0; i < dc->res_pool->pipe_count; i++) {
4103 			struct pipe_ctx *new_pipe = &context->res_ctx.pipe_ctx[i];
4104 			struct pipe_ctx *old_pipe = &dc->current_state->res_ctx.pipe_ctx[i];
4105 
4106 			if (new_pipe->plane_state && new_pipe->plane_state != old_pipe->plane_state)
4107 				new_pipe->plane_state->force_full_update = true;
4108 		}
4109 	} else if (update_type == UPDATE_TYPE_FAST) {
4110 		/*
4111 		 * Previous frame finished and HW is ready for optimization.
4112 		 */
4113 		dc_post_update_surfaces_to_stream(dc);
4114 	}
4115 
4116 
4117 	for (i = 0; i < surface_count; i++) {
4118 		struct dc_plane_state *surface = srf_updates[i].surface;
4119 
4120 		copy_surface_update_to_plane(surface, &srf_updates[i]);
4121 
4122 		if (update_type >= UPDATE_TYPE_MED) {
4123 			for (j = 0; j < dc->res_pool->pipe_count; j++) {
4124 				struct pipe_ctx *pipe_ctx =
4125 					&context->res_ctx.pipe_ctx[j];
4126 
4127 				if (pipe_ctx->plane_state != surface)
4128 					continue;
4129 
4130 				resource_build_scaling_params(pipe_ctx);
4131 			}
4132 		}
4133 	}
4134 
4135 	copy_stream_update_to_stream(dc, context, stream, stream_update);
4136 
4137 	if (update_type >= UPDATE_TYPE_FULL) {
4138 		if (!dc->res_pool->funcs->validate_bandwidth(dc, context, false)) {
4139 			DC_ERROR("Mode validation failed for stream update!\n");
4140 			dc_release_state(context);
4141 			return;
4142 		}
4143 	}
4144 
4145 	TRACE_DC_PIPE_STATE(pipe_ctx, i, MAX_PIPES);
4146 
4147 	commit_planes_for_stream(
4148 				dc,
4149 				srf_updates,
4150 				surface_count,
4151 				stream,
4152 				stream_update,
4153 				update_type,
4154 				context);
4155 	/*update current_State*/
4156 	if (dc->current_state != context) {
4157 
4158 		struct dc_state *old = dc->current_state;
4159 
4160 		dc->current_state = context;
4161 		dc_release_state(old);
4162 
4163 		for (i = 0; i < dc->res_pool->pipe_count; i++) {
4164 			struct pipe_ctx *pipe_ctx = &context->res_ctx.pipe_ctx[i];
4165 
4166 			if (pipe_ctx->plane_state && pipe_ctx->stream == stream)
4167 				pipe_ctx->plane_state->force_full_update = false;
4168 		}
4169 	}
4170 
4171 	/* Legacy optimization path for DCE. */
4172 	if (update_type >= UPDATE_TYPE_FULL && dc_ctx->dce_version < DCE_VERSION_MAX) {
4173 		dc_post_update_surfaces_to_stream(dc);
4174 		TRACE_DCE_CLOCK_STATE(&context->bw_ctx.bw.dce);
4175 	}
4176 
4177 	return;
4178 
4179 }
4180 
dc_get_current_stream_count(struct dc * dc)4181 uint8_t dc_get_current_stream_count(struct dc *dc)
4182 {
4183 	return dc->current_state->stream_count;
4184 }
4185 
dc_get_stream_at_index(struct dc * dc,uint8_t i)4186 struct dc_stream_state *dc_get_stream_at_index(struct dc *dc, uint8_t i)
4187 {
4188 	if (i < dc->current_state->stream_count)
4189 		return dc->current_state->streams[i];
4190 	return NULL;
4191 }
4192 
dc_interrupt_to_irq_source(struct dc * dc,uint32_t src_id,uint32_t ext_id)4193 enum dc_irq_source dc_interrupt_to_irq_source(
4194 		struct dc *dc,
4195 		uint32_t src_id,
4196 		uint32_t ext_id)
4197 {
4198 	return dal_irq_service_to_irq_source(dc->res_pool->irqs, src_id, ext_id);
4199 }
4200 
4201 /*
4202  * dc_interrupt_set() - Enable/disable an AMD hw interrupt source
4203  */
dc_interrupt_set(struct dc * dc,enum dc_irq_source src,bool enable)4204 bool dc_interrupt_set(struct dc *dc, enum dc_irq_source src, bool enable)
4205 {
4206 
4207 	if (dc == NULL)
4208 		return false;
4209 
4210 	return dal_irq_service_set(dc->res_pool->irqs, src, enable);
4211 }
4212 
dc_interrupt_ack(struct dc * dc,enum dc_irq_source src)4213 void dc_interrupt_ack(struct dc *dc, enum dc_irq_source src)
4214 {
4215 	dal_irq_service_ack(dc->res_pool->irqs, src);
4216 }
4217 
dc_power_down_on_boot(struct dc * dc)4218 void dc_power_down_on_boot(struct dc *dc)
4219 {
4220 	if (dc->ctx->dce_environment != DCE_ENV_VIRTUAL_HW &&
4221 			dc->hwss.power_down_on_boot)
4222 		dc->hwss.power_down_on_boot(dc);
4223 }
4224 
dc_set_power_state(struct dc * dc,enum dc_acpi_cm_power_state power_state)4225 void dc_set_power_state(
4226 	struct dc *dc,
4227 	enum dc_acpi_cm_power_state power_state)
4228 {
4229 	struct kref refcount;
4230 	struct display_mode_lib *dml;
4231 
4232 	if (!dc->current_state)
4233 		return;
4234 
4235 	switch (power_state) {
4236 	case DC_ACPI_CM_POWER_STATE_D0:
4237 		dc_resource_state_construct(dc, dc->current_state);
4238 
4239 		dc_z10_restore(dc);
4240 
4241 		if (dc->ctx->dmub_srv)
4242 			dc_dmub_srv_wait_phy_init(dc->ctx->dmub_srv);
4243 
4244 		dc->hwss.init_hw(dc);
4245 
4246 		if (dc->hwss.init_sys_ctx != NULL &&
4247 			dc->vm_pa_config.valid) {
4248 			dc->hwss.init_sys_ctx(dc->hwseq, dc, &dc->vm_pa_config);
4249 		}
4250 
4251 		break;
4252 	default:
4253 		ASSERT(dc->current_state->stream_count == 0);
4254 		/* Zero out the current context so that on resume we start with
4255 		 * clean state, and dc hw programming optimizations will not
4256 		 * cause any trouble.
4257 		 */
4258 		dml = kzalloc(sizeof(struct display_mode_lib),
4259 				GFP_KERNEL);
4260 
4261 		ASSERT(dml);
4262 		if (!dml)
4263 			return;
4264 
4265 		/* Preserve refcount */
4266 		refcount = dc->current_state->refcount;
4267 		/* Preserve display mode lib */
4268 		memcpy(dml, &dc->current_state->bw_ctx.dml, sizeof(struct display_mode_lib));
4269 
4270 		dc_resource_state_destruct(dc->current_state);
4271 		memset(dc->current_state, 0,
4272 				sizeof(*dc->current_state));
4273 
4274 		dc->current_state->refcount = refcount;
4275 		dc->current_state->bw_ctx.dml = *dml;
4276 
4277 		kfree(dml);
4278 
4279 		break;
4280 	}
4281 }
4282 
dc_resume(struct dc * dc)4283 void dc_resume(struct dc *dc)
4284 {
4285 	uint32_t i;
4286 
4287 	for (i = 0; i < dc->link_count; i++)
4288 		core_link_resume(dc->links[i]);
4289 }
4290 
dc_is_dmcu_initialized(struct dc * dc)4291 bool dc_is_dmcu_initialized(struct dc *dc)
4292 {
4293 	struct dmcu *dmcu = dc->res_pool->dmcu;
4294 
4295 	if (dmcu)
4296 		return dmcu->funcs->is_dmcu_initialized(dmcu);
4297 	return false;
4298 }
4299 
dc_is_oem_i2c_device_present(struct dc * dc,size_t slave_address)4300 bool dc_is_oem_i2c_device_present(
4301 	struct dc *dc,
4302 	size_t slave_address)
4303 {
4304 	if (dc->res_pool->oem_device)
4305 		return dce_i2c_oem_device_present(
4306 			dc->res_pool,
4307 			dc->res_pool->oem_device,
4308 			slave_address);
4309 
4310 	return false;
4311 }
4312 
dc_submit_i2c(struct dc * dc,uint32_t link_index,struct i2c_command * cmd)4313 bool dc_submit_i2c(
4314 		struct dc *dc,
4315 		uint32_t link_index,
4316 		struct i2c_command *cmd)
4317 {
4318 
4319 	struct dc_link *link = dc->links[link_index];
4320 	struct ddc_service *ddc = link->ddc;
4321 	return dce_i2c_submit_command(
4322 		dc->res_pool,
4323 		ddc->ddc_pin,
4324 		cmd);
4325 }
4326 
dc_submit_i2c_oem(struct dc * dc,struct i2c_command * cmd)4327 bool dc_submit_i2c_oem(
4328 		struct dc *dc,
4329 		struct i2c_command *cmd)
4330 {
4331 	struct ddc_service *ddc = dc->res_pool->oem_device;
4332 	if (ddc)
4333 		return dce_i2c_submit_command(
4334 			dc->res_pool,
4335 			ddc->ddc_pin,
4336 			cmd);
4337 
4338 	return false;
4339 }
4340 
link_add_remote_sink_helper(struct dc_link * dc_link,struct dc_sink * sink)4341 static bool link_add_remote_sink_helper(struct dc_link *dc_link, struct dc_sink *sink)
4342 {
4343 	if (dc_link->sink_count >= MAX_SINKS_PER_LINK) {
4344 		BREAK_TO_DEBUGGER();
4345 		return false;
4346 	}
4347 
4348 	dc_sink_retain(sink);
4349 
4350 	dc_link->remote_sinks[dc_link->sink_count] = sink;
4351 	dc_link->sink_count++;
4352 
4353 	return true;
4354 }
4355 
4356 /*
4357  * dc_link_add_remote_sink() - Create a sink and attach it to an existing link
4358  *
4359  * EDID length is in bytes
4360  */
dc_link_add_remote_sink(struct dc_link * link,const uint8_t * edid,int len,struct dc_sink_init_data * init_data)4361 struct dc_sink *dc_link_add_remote_sink(
4362 		struct dc_link *link,
4363 		const uint8_t *edid,
4364 		int len,
4365 		struct dc_sink_init_data *init_data)
4366 {
4367 	struct dc_sink *dc_sink;
4368 	enum dc_edid_status edid_status;
4369 
4370 	if (len > DC_MAX_EDID_BUFFER_SIZE) {
4371 		dm_error("Max EDID buffer size breached!\n");
4372 		return NULL;
4373 	}
4374 
4375 	if (!init_data) {
4376 		BREAK_TO_DEBUGGER();
4377 		return NULL;
4378 	}
4379 
4380 	if (!init_data->link) {
4381 		BREAK_TO_DEBUGGER();
4382 		return NULL;
4383 	}
4384 
4385 	dc_sink = dc_sink_create(init_data);
4386 
4387 	if (!dc_sink)
4388 		return NULL;
4389 
4390 	memmove(dc_sink->dc_edid.raw_edid, edid, len);
4391 	dc_sink->dc_edid.length = len;
4392 
4393 	if (!link_add_remote_sink_helper(
4394 			link,
4395 			dc_sink))
4396 		goto fail_add_sink;
4397 
4398 	edid_status = dm_helpers_parse_edid_caps(
4399 			link,
4400 			&dc_sink->dc_edid,
4401 			&dc_sink->edid_caps);
4402 
4403 	/*
4404 	 * Treat device as no EDID device if EDID
4405 	 * parsing fails
4406 	 */
4407 	if (edid_status != EDID_OK && edid_status != EDID_PARTIAL_VALID) {
4408 		dc_sink->dc_edid.length = 0;
4409 		dm_error("Bad EDID, status%d!\n", edid_status);
4410 	}
4411 
4412 	return dc_sink;
4413 
4414 fail_add_sink:
4415 	dc_sink_release(dc_sink);
4416 	return NULL;
4417 }
4418 
4419 /*
4420  * dc_link_remove_remote_sink() - Remove a remote sink from a dc_link
4421  *
4422  * Note that this just removes the struct dc_sink - it doesn't
4423  * program hardware or alter other members of dc_link
4424  */
dc_link_remove_remote_sink(struct dc_link * link,struct dc_sink * sink)4425 void dc_link_remove_remote_sink(struct dc_link *link, struct dc_sink *sink)
4426 {
4427 	int i;
4428 
4429 	if (!link->sink_count) {
4430 		BREAK_TO_DEBUGGER();
4431 		return;
4432 	}
4433 
4434 	for (i = 0; i < link->sink_count; i++) {
4435 		if (link->remote_sinks[i] == sink) {
4436 			dc_sink_release(sink);
4437 			link->remote_sinks[i] = NULL;
4438 
4439 			/* shrink array to remove empty place */
4440 			while (i < link->sink_count - 1) {
4441 				link->remote_sinks[i] = link->remote_sinks[i+1];
4442 				i++;
4443 			}
4444 			link->remote_sinks[i] = NULL;
4445 			link->sink_count--;
4446 			return;
4447 		}
4448 	}
4449 }
4450 
get_clock_requirements_for_state(struct dc_state * state,struct AsicStateEx * info)4451 void get_clock_requirements_for_state(struct dc_state *state, struct AsicStateEx *info)
4452 {
4453 	info->displayClock				= (unsigned int)state->bw_ctx.bw.dcn.clk.dispclk_khz;
4454 	info->engineClock				= (unsigned int)state->bw_ctx.bw.dcn.clk.dcfclk_khz;
4455 	info->memoryClock				= (unsigned int)state->bw_ctx.bw.dcn.clk.dramclk_khz;
4456 	info->maxSupportedDppClock		= (unsigned int)state->bw_ctx.bw.dcn.clk.max_supported_dppclk_khz;
4457 	info->dppClock					= (unsigned int)state->bw_ctx.bw.dcn.clk.dppclk_khz;
4458 	info->socClock					= (unsigned int)state->bw_ctx.bw.dcn.clk.socclk_khz;
4459 	info->dcfClockDeepSleep			= (unsigned int)state->bw_ctx.bw.dcn.clk.dcfclk_deep_sleep_khz;
4460 	info->fClock					= (unsigned int)state->bw_ctx.bw.dcn.clk.fclk_khz;
4461 	info->phyClock					= (unsigned int)state->bw_ctx.bw.dcn.clk.phyclk_khz;
4462 }
dc_set_clock(struct dc * dc,enum dc_clock_type clock_type,uint32_t clk_khz,uint32_t stepping)4463 enum dc_status dc_set_clock(struct dc *dc, enum dc_clock_type clock_type, uint32_t clk_khz, uint32_t stepping)
4464 {
4465 	if (dc->hwss.set_clock)
4466 		return dc->hwss.set_clock(dc, clock_type, clk_khz, stepping);
4467 	return DC_ERROR_UNEXPECTED;
4468 }
dc_get_clock(struct dc * dc,enum dc_clock_type clock_type,struct dc_clock_config * clock_cfg)4469 void dc_get_clock(struct dc *dc, enum dc_clock_type clock_type, struct dc_clock_config *clock_cfg)
4470 {
4471 	if (dc->hwss.get_clock)
4472 		dc->hwss.get_clock(dc, clock_type, clock_cfg);
4473 }
4474 
4475 /* enable/disable eDP PSR without specify stream for eDP */
dc_set_psr_allow_active(struct dc * dc,bool enable)4476 bool dc_set_psr_allow_active(struct dc *dc, bool enable)
4477 {
4478 	int i;
4479 	bool allow_active;
4480 
4481 	for (i = 0; i < dc->current_state->stream_count ; i++) {
4482 		struct dc_link *link;
4483 		struct dc_stream_state *stream = dc->current_state->streams[i];
4484 
4485 		link = stream->link;
4486 		if (!link)
4487 			continue;
4488 
4489 		if (link->psr_settings.psr_feature_enabled) {
4490 			if (enable && !link->psr_settings.psr_allow_active) {
4491 				allow_active = true;
4492 				if (!dc_link_set_psr_allow_active(link, &allow_active, false, false, NULL))
4493 					return false;
4494 			} else if (!enable && link->psr_settings.psr_allow_active) {
4495 				allow_active = false;
4496 				if (!dc_link_set_psr_allow_active(link, &allow_active, true, false, NULL))
4497 					return false;
4498 			}
4499 		}
4500 	}
4501 
4502 	return true;
4503 }
4504 
dc_allow_idle_optimizations(struct dc * dc,bool allow)4505 void dc_allow_idle_optimizations(struct dc *dc, bool allow)
4506 {
4507 	if (dc->debug.disable_idle_power_optimizations)
4508 		return;
4509 
4510 	if (dc->clk_mgr != NULL && dc->clk_mgr->funcs->is_smu_present)
4511 		if (!dc->clk_mgr->funcs->is_smu_present(dc->clk_mgr))
4512 			return;
4513 
4514 	if (allow == dc->idle_optimizations_allowed)
4515 		return;
4516 
4517 	if (dc->hwss.apply_idle_power_optimizations && dc->hwss.apply_idle_power_optimizations(dc, allow))
4518 		dc->idle_optimizations_allowed = allow;
4519 }
4520 
4521 /* set min and max memory clock to lowest and highest DPM level, respectively */
dc_unlock_memory_clock_frequency(struct dc * dc)4522 void dc_unlock_memory_clock_frequency(struct dc *dc)
4523 {
4524 	if (dc->clk_mgr->funcs->set_hard_min_memclk)
4525 		dc->clk_mgr->funcs->set_hard_min_memclk(dc->clk_mgr, false);
4526 
4527 	if (dc->clk_mgr->funcs->set_hard_max_memclk)
4528 		dc->clk_mgr->funcs->set_hard_max_memclk(dc->clk_mgr);
4529 }
4530 
4531 /* set min memory clock to the min required for current mode, max to maxDPM */
dc_lock_memory_clock_frequency(struct dc * dc)4532 void dc_lock_memory_clock_frequency(struct dc *dc)
4533 {
4534 	if (dc->clk_mgr->funcs->get_memclk_states_from_smu)
4535 		dc->clk_mgr->funcs->get_memclk_states_from_smu(dc->clk_mgr);
4536 
4537 	if (dc->clk_mgr->funcs->set_hard_min_memclk)
4538 		dc->clk_mgr->funcs->set_hard_min_memclk(dc->clk_mgr, true);
4539 
4540 	if (dc->clk_mgr->funcs->set_hard_max_memclk)
4541 		dc->clk_mgr->funcs->set_hard_max_memclk(dc->clk_mgr);
4542 }
4543 
blank_and_force_memclk(struct dc * dc,bool apply,unsigned int memclk_mhz)4544 static void blank_and_force_memclk(struct dc *dc, bool apply, unsigned int memclk_mhz)
4545 {
4546 	struct dc_state *context = dc->current_state;
4547 	struct hubp *hubp;
4548 	struct pipe_ctx *pipe;
4549 	int i;
4550 
4551 	for (i = 0; i < dc->res_pool->pipe_count; i++) {
4552 		pipe = &context->res_ctx.pipe_ctx[i];
4553 
4554 		if (pipe->stream != NULL) {
4555 			dc->hwss.disable_pixel_data(dc, pipe, true);
4556 
4557 			// wait for double buffer
4558 			pipe->stream_res.tg->funcs->wait_for_state(pipe->stream_res.tg, CRTC_STATE_VACTIVE);
4559 			pipe->stream_res.tg->funcs->wait_for_state(pipe->stream_res.tg, CRTC_STATE_VBLANK);
4560 			pipe->stream_res.tg->funcs->wait_for_state(pipe->stream_res.tg, CRTC_STATE_VACTIVE);
4561 
4562 			hubp = pipe->plane_res.hubp;
4563 			hubp->funcs->set_blank_regs(hubp, true);
4564 		}
4565 	}
4566 
4567 	dc->clk_mgr->funcs->set_max_memclk(dc->clk_mgr, memclk_mhz);
4568 	dc->clk_mgr->funcs->set_min_memclk(dc->clk_mgr, memclk_mhz);
4569 
4570 	for (i = 0; i < dc->res_pool->pipe_count; i++) {
4571 		pipe = &context->res_ctx.pipe_ctx[i];
4572 
4573 		if (pipe->stream != NULL) {
4574 			dc->hwss.disable_pixel_data(dc, pipe, false);
4575 
4576 			hubp = pipe->plane_res.hubp;
4577 			hubp->funcs->set_blank_regs(hubp, false);
4578 		}
4579 	}
4580 }
4581 
4582 
4583 /**
4584  * dc_enable_dcmode_clk_limit() - lower clocks in dc (battery) mode
4585  * @dc: pointer to dc of the dm calling this
4586  * @enable: True = transition to DC mode, false = transition back to AC mode
4587  *
4588  * Some SoCs define additional clock limits when in DC mode, DM should
4589  * invoke this function when the platform undergoes a power source transition
4590  * so DC can apply/unapply the limit. This interface may be disruptive to
4591  * the onscreen content.
4592  *
4593  * Context: Triggered by OS through DM interface, or manually by escape calls.
4594  * Need to hold a dclock when doing so.
4595  *
4596  * Return: none (void function)
4597  *
4598  */
dc_enable_dcmode_clk_limit(struct dc * dc,bool enable)4599 void dc_enable_dcmode_clk_limit(struct dc *dc, bool enable)
4600 {
4601 	uint32_t hw_internal_rev = dc->ctx->asic_id.hw_internal_rev;
4602 	unsigned int softMax, maxDPM, funcMin;
4603 	bool p_state_change_support;
4604 
4605 	if (!ASICREV_IS_BEIGE_GOBY_P(hw_internal_rev))
4606 		return;
4607 
4608 	softMax = dc->clk_mgr->bw_params->dc_mode_softmax_memclk;
4609 	maxDPM = dc->clk_mgr->bw_params->clk_table.entries[dc->clk_mgr->bw_params->clk_table.num_entries - 1].memclk_mhz;
4610 	funcMin = (dc->clk_mgr->clks.dramclk_khz + 999) / 1000;
4611 	p_state_change_support = dc->clk_mgr->clks.p_state_change_support;
4612 
4613 	if (enable && !dc->clk_mgr->dc_mode_softmax_enabled) {
4614 		if (p_state_change_support) {
4615 			if (funcMin <= softMax)
4616 				dc->clk_mgr->funcs->set_max_memclk(dc->clk_mgr, softMax);
4617 			// else: No-Op
4618 		} else {
4619 			if (funcMin <= softMax)
4620 				blank_and_force_memclk(dc, true, softMax);
4621 			// else: No-Op
4622 		}
4623 	} else if (!enable && dc->clk_mgr->dc_mode_softmax_enabled) {
4624 		if (p_state_change_support) {
4625 			if (funcMin <= softMax)
4626 				dc->clk_mgr->funcs->set_max_memclk(dc->clk_mgr, maxDPM);
4627 			// else: No-Op
4628 		} else {
4629 			if (funcMin <= softMax)
4630 				blank_and_force_memclk(dc, true, maxDPM);
4631 			// else: No-Op
4632 		}
4633 	}
4634 	dc->clk_mgr->dc_mode_softmax_enabled = enable;
4635 }
dc_is_plane_eligible_for_idle_optimizations(struct dc * dc,struct dc_plane_state * plane,struct dc_cursor_attributes * cursor_attr)4636 bool dc_is_plane_eligible_for_idle_optimizations(struct dc *dc, struct dc_plane_state *plane,
4637 		struct dc_cursor_attributes *cursor_attr)
4638 {
4639 	if (dc->hwss.does_plane_fit_in_mall && dc->hwss.does_plane_fit_in_mall(dc, plane, cursor_attr))
4640 		return true;
4641 	return false;
4642 }
4643 
4644 /* cleanup on driver unload */
dc_hardware_release(struct dc * dc)4645 void dc_hardware_release(struct dc *dc)
4646 {
4647 	dc_mclk_switch_using_fw_based_vblank_stretch_shut_down(dc);
4648 
4649 	if (dc->hwss.hardware_release)
4650 		dc->hwss.hardware_release(dc);
4651 }
4652 
dc_mclk_switch_using_fw_based_vblank_stretch_shut_down(struct dc * dc)4653 void dc_mclk_switch_using_fw_based_vblank_stretch_shut_down(struct dc *dc)
4654 {
4655 	if (dc->current_state)
4656 		dc->current_state->bw_ctx.bw.dcn.clk.fw_based_mclk_switching_shut_down = true;
4657 }
4658 
4659 /**
4660  * dc_is_dmub_outbox_supported - Check if DMUB firmware support outbox notification
4661  *
4662  * @dc: [in] dc structure
4663  *
4664  * Checks whether DMUB FW supports outbox notifications, if supported DM
4665  * should register outbox interrupt prior to actually enabling interrupts
4666  * via dc_enable_dmub_outbox
4667  *
4668  * Return:
4669  * True if DMUB FW supports outbox notifications, False otherwise
4670  */
dc_is_dmub_outbox_supported(struct dc * dc)4671 bool dc_is_dmub_outbox_supported(struct dc *dc)
4672 {
4673 	/* DCN31 B0 USB4 DPIA needs dmub notifications for interrupts */
4674 	if (dc->ctx->asic_id.chip_family == FAMILY_YELLOW_CARP &&
4675 	    dc->ctx->asic_id.hw_internal_rev == YELLOW_CARP_B0 &&
4676 	    !dc->debug.dpia_debug.bits.disable_dpia)
4677 		return true;
4678 
4679 	if (dc->ctx->asic_id.chip_family == AMDGPU_FAMILY_GC_11_0_1 &&
4680 	    !dc->debug.dpia_debug.bits.disable_dpia)
4681 		return true;
4682 
4683 	/* dmub aux needs dmub notifications to be enabled */
4684 	return dc->debug.enable_dmub_aux_for_legacy_ddc;
4685 }
4686 
4687 /**
4688  * dc_enable_dmub_notifications - Check if dmub fw supports outbox
4689  *
4690  * @dc: [in] dc structure
4691  *
4692  * Calls dc_is_dmub_outbox_supported to check if dmub fw supports outbox
4693  * notifications. All DMs shall switch to dc_is_dmub_outbox_supported.  This
4694  * API shall be removed after switching.
4695  *
4696  * Return:
4697  * True if DMUB FW supports outbox notifications, False otherwise
4698  */
dc_enable_dmub_notifications(struct dc * dc)4699 bool dc_enable_dmub_notifications(struct dc *dc)
4700 {
4701 	return dc_is_dmub_outbox_supported(dc);
4702 }
4703 
4704 /**
4705  * dc_enable_dmub_outbox - Enables DMUB unsolicited notification
4706  *
4707  * @dc: [in] dc structure
4708  *
4709  * Enables DMUB unsolicited notifications to x86 via outbox.
4710  */
dc_enable_dmub_outbox(struct dc * dc)4711 void dc_enable_dmub_outbox(struct dc *dc)
4712 {
4713 	struct dc_context *dc_ctx = dc->ctx;
4714 
4715 	dmub_enable_outbox_notification(dc_ctx->dmub_srv);
4716 	DC_LOG_DC("%s: dmub outbox notifications enabled\n", __func__);
4717 }
4718 
4719 /**
4720  * dc_process_dmub_aux_transfer_async - Submits aux command to dmub via inbox message
4721  *                                      Sets port index appropriately for legacy DDC
4722  * @dc: dc structure
4723  * @link_index: link index
4724  * @payload: aux payload
4725  *
4726  * Returns: True if successful, False if failure
4727  */
dc_process_dmub_aux_transfer_async(struct dc * dc,uint32_t link_index,struct aux_payload * payload)4728 bool dc_process_dmub_aux_transfer_async(struct dc *dc,
4729 				uint32_t link_index,
4730 				struct aux_payload *payload)
4731 {
4732 	uint8_t action;
4733 	union dmub_rb_cmd cmd = {0};
4734 	struct dc_dmub_srv *dmub_srv = dc->ctx->dmub_srv;
4735 
4736 	ASSERT(payload->length <= 16);
4737 
4738 	cmd.dp_aux_access.header.type = DMUB_CMD__DP_AUX_ACCESS;
4739 	cmd.dp_aux_access.header.payload_bytes = 0;
4740 	/* For dpia, ddc_pin is set to NULL */
4741 	if (!dc->links[link_index]->ddc->ddc_pin)
4742 		cmd.dp_aux_access.aux_control.type = AUX_CHANNEL_DPIA;
4743 	else
4744 		cmd.dp_aux_access.aux_control.type = AUX_CHANNEL_LEGACY_DDC;
4745 
4746 	cmd.dp_aux_access.aux_control.instance = dc->links[link_index]->ddc_hw_inst;
4747 	cmd.dp_aux_access.aux_control.sw_crc_enabled = 0;
4748 	cmd.dp_aux_access.aux_control.timeout = 0;
4749 	cmd.dp_aux_access.aux_control.dpaux.address = payload->address;
4750 	cmd.dp_aux_access.aux_control.dpaux.is_i2c_over_aux = payload->i2c_over_aux;
4751 	cmd.dp_aux_access.aux_control.dpaux.length = payload->length;
4752 
4753 	/* set aux action */
4754 	if (payload->i2c_over_aux) {
4755 		if (payload->write) {
4756 			if (payload->mot)
4757 				action = DP_AUX_REQ_ACTION_I2C_WRITE_MOT;
4758 			else
4759 				action = DP_AUX_REQ_ACTION_I2C_WRITE;
4760 		} else {
4761 			if (payload->mot)
4762 				action = DP_AUX_REQ_ACTION_I2C_READ_MOT;
4763 			else
4764 				action = DP_AUX_REQ_ACTION_I2C_READ;
4765 			}
4766 	} else {
4767 		if (payload->write)
4768 			action = DP_AUX_REQ_ACTION_DPCD_WRITE;
4769 		else
4770 			action = DP_AUX_REQ_ACTION_DPCD_READ;
4771 	}
4772 
4773 	cmd.dp_aux_access.aux_control.dpaux.action = action;
4774 
4775 	if (payload->length && payload->write) {
4776 		memcpy(cmd.dp_aux_access.aux_control.dpaux.data,
4777 			payload->data,
4778 			payload->length
4779 			);
4780 	}
4781 
4782 	dc_dmub_srv_cmd_queue(dmub_srv, &cmd);
4783 	dc_dmub_srv_cmd_execute(dmub_srv);
4784 	dc_dmub_srv_wait_idle(dmub_srv);
4785 
4786 	return true;
4787 }
4788 
get_link_index_from_dpia_port_index(const struct dc * dc,uint8_t dpia_port_index)4789 uint8_t get_link_index_from_dpia_port_index(const struct dc *dc,
4790 					    uint8_t dpia_port_index)
4791 {
4792 	uint8_t index, link_index = 0xFF;
4793 
4794 	for (index = 0; index < dc->link_count; index++) {
4795 		/* ddc_hw_inst has dpia port index for dpia links
4796 		 * and ddc instance for legacy links
4797 		 */
4798 		if (!dc->links[index]->ddc->ddc_pin) {
4799 			if (dc->links[index]->ddc_hw_inst == dpia_port_index) {
4800 				link_index = index;
4801 				break;
4802 			}
4803 		}
4804 	}
4805 	ASSERT(link_index != 0xFF);
4806 	return link_index;
4807 }
4808 
4809 /**
4810  * dc_process_dmub_set_config_async - Submits set_config command
4811  *
4812  * @dc: [in] dc structure
4813  * @link_index: [in] link_index: link index
4814  * @payload: [in] aux payload
4815  * @notify: [out] set_config immediate reply
4816  *
4817  * Submits set_config command to dmub via inbox message.
4818  *
4819  * Return:
4820  * True if successful, False if failure
4821  */
dc_process_dmub_set_config_async(struct dc * dc,uint32_t link_index,struct set_config_cmd_payload * payload,struct dmub_notification * notify)4822 bool dc_process_dmub_set_config_async(struct dc *dc,
4823 				uint32_t link_index,
4824 				struct set_config_cmd_payload *payload,
4825 				struct dmub_notification *notify)
4826 {
4827 	union dmub_rb_cmd cmd = {0};
4828 	struct dc_dmub_srv *dmub_srv = dc->ctx->dmub_srv;
4829 	bool is_cmd_complete = true;
4830 
4831 	/* prepare SET_CONFIG command */
4832 	cmd.set_config_access.header.type = DMUB_CMD__DPIA;
4833 	cmd.set_config_access.header.sub_type = DMUB_CMD__DPIA_SET_CONFIG_ACCESS;
4834 
4835 	cmd.set_config_access.set_config_control.instance = dc->links[link_index]->ddc_hw_inst;
4836 	cmd.set_config_access.set_config_control.cmd_pkt.msg_type = payload->msg_type;
4837 	cmd.set_config_access.set_config_control.cmd_pkt.msg_data = payload->msg_data;
4838 
4839 	if (!dc_dmub_srv_cmd_with_reply_data(dmub_srv, &cmd)) {
4840 		/* command is not processed by dmub */
4841 		notify->sc_status = SET_CONFIG_UNKNOWN_ERROR;
4842 		return is_cmd_complete;
4843 	}
4844 
4845 	/* command processed by dmub, if ret_status is 1, it is completed instantly */
4846 	if (cmd.set_config_access.header.ret_status == 1)
4847 		notify->sc_status = cmd.set_config_access.set_config_control.immed_status;
4848 	else
4849 		/* cmd pending, will receive notification via outbox */
4850 		is_cmd_complete = false;
4851 
4852 	return is_cmd_complete;
4853 }
4854 
4855 /**
4856  * dc_process_dmub_set_mst_slots - Submits MST solt allocation
4857  *
4858  * @dc: [in] dc structure
4859  * @link_index: [in] link index
4860  * @mst_alloc_slots: [in] mst slots to be allotted
4861  * @mst_slots_in_use: [out] mst slots in use returned in failure case
4862  *
4863  * Submits mst slot allocation command to dmub via inbox message
4864  *
4865  * Return:
4866  * DC_OK if successful, DC_ERROR if failure
4867  */
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)4868 enum dc_status dc_process_dmub_set_mst_slots(const struct dc *dc,
4869 				uint32_t link_index,
4870 				uint8_t mst_alloc_slots,
4871 				uint8_t *mst_slots_in_use)
4872 {
4873 	union dmub_rb_cmd cmd = {0};
4874 	struct dc_dmub_srv *dmub_srv = dc->ctx->dmub_srv;
4875 
4876 	/* prepare MST_ALLOC_SLOTS command */
4877 	cmd.set_mst_alloc_slots.header.type = DMUB_CMD__DPIA;
4878 	cmd.set_mst_alloc_slots.header.sub_type = DMUB_CMD__DPIA_MST_ALLOC_SLOTS;
4879 
4880 	cmd.set_mst_alloc_slots.mst_slots_control.instance = dc->links[link_index]->ddc_hw_inst;
4881 	cmd.set_mst_alloc_slots.mst_slots_control.mst_alloc_slots = mst_alloc_slots;
4882 
4883 	if (!dc_dmub_srv_cmd_with_reply_data(dmub_srv, &cmd))
4884 		/* command is not processed by dmub */
4885 		return DC_ERROR_UNEXPECTED;
4886 
4887 	/* command processed by dmub, if ret_status is 1 */
4888 	if (cmd.set_config_access.header.ret_status != 1)
4889 		/* command processing error */
4890 		return DC_ERROR_UNEXPECTED;
4891 
4892 	/* command processed and we have a status of 2, mst not enabled in dpia */
4893 	if (cmd.set_mst_alloc_slots.mst_slots_control.immed_status == 2)
4894 		return DC_FAIL_UNSUPPORTED_1;
4895 
4896 	/* previously configured mst alloc and used slots did not match */
4897 	if (cmd.set_mst_alloc_slots.mst_slots_control.immed_status == 3) {
4898 		*mst_slots_in_use = cmd.set_mst_alloc_slots.mst_slots_control.mst_slots_in_use;
4899 		return DC_NOT_SUPPORTED;
4900 	}
4901 
4902 	return DC_OK;
4903 }
4904 
4905 /**
4906  * dc_process_dmub_dpia_hpd_int_enable - Submits DPIA DPD interruption
4907  *
4908  * @dc: [in] dc structure
4909  * @hpd_int_enable: [in] 1 for hpd int enable, 0 to disable
4910  *
4911  * Submits dpia hpd int enable command to dmub via inbox message
4912  */
dc_process_dmub_dpia_hpd_int_enable(const struct dc * dc,uint32_t hpd_int_enable)4913 void dc_process_dmub_dpia_hpd_int_enable(const struct dc *dc,
4914 				uint32_t hpd_int_enable)
4915 {
4916 	union dmub_rb_cmd cmd = {0};
4917 	struct dc_dmub_srv *dmub_srv = dc->ctx->dmub_srv;
4918 
4919 	cmd.dpia_hpd_int_enable.header.type = DMUB_CMD__DPIA_HPD_INT_ENABLE;
4920 	cmd.dpia_hpd_int_enable.enable = hpd_int_enable;
4921 
4922 	dc_dmub_srv_cmd_queue(dmub_srv, &cmd);
4923 	dc_dmub_srv_cmd_execute(dmub_srv);
4924 	dc_dmub_srv_wait_idle(dmub_srv);
4925 
4926 	DC_LOG_DEBUG("%s: hpd_int_enable(%d)\n", __func__, hpd_int_enable);
4927 }
4928 
4929 /**
4930  * dc_disable_accelerated_mode - disable accelerated mode
4931  * @dc: dc structure
4932  */
dc_disable_accelerated_mode(struct dc * dc)4933 void dc_disable_accelerated_mode(struct dc *dc)
4934 {
4935 	bios_set_scratch_acc_mode_change(dc->ctx->dc_bios, 0);
4936 }
4937 
4938 
4939 /**
4940  *  dc_notify_vsync_int_state - notifies vsync enable/disable state
4941  *  @dc: dc structure
4942  *  @stream: stream where vsync int state changed
4943  *  @enable: whether vsync is enabled or disabled
4944  *
4945  *  Called when vsync is enabled/disabled Will notify DMUB to start/stop ABM
4946  *  interrupts after steady state is reached.
4947  */
dc_notify_vsync_int_state(struct dc * dc,struct dc_stream_state * stream,bool enable)4948 void dc_notify_vsync_int_state(struct dc *dc, struct dc_stream_state *stream, bool enable)
4949 {
4950 	int i;
4951 	int edp_num;
4952 	struct pipe_ctx *pipe = NULL;
4953 	struct dc_link *link = stream->sink->link;
4954 	struct dc_link *edp_links[MAX_NUM_EDP];
4955 
4956 
4957 	if (link->psr_settings.psr_feature_enabled)
4958 		return;
4959 
4960 	/*find primary pipe associated with stream*/
4961 	for (i = 0; i < MAX_PIPES; i++) {
4962 		pipe = &dc->current_state->res_ctx.pipe_ctx[i];
4963 
4964 		if (pipe->stream == stream && pipe->stream_res.tg)
4965 			break;
4966 	}
4967 
4968 	if (i == MAX_PIPES) {
4969 		ASSERT(0);
4970 		return;
4971 	}
4972 
4973 	get_edp_links(dc, edp_links, &edp_num);
4974 
4975 	/* Determine panel inst */
4976 	for (i = 0; i < edp_num; i++) {
4977 		if (edp_links[i] == link)
4978 			break;
4979 	}
4980 
4981 	if (i == edp_num) {
4982 		return;
4983 	}
4984 
4985 	if (pipe->stream_res.abm && pipe->stream_res.abm->funcs->set_abm_pause)
4986 		pipe->stream_res.abm->funcs->set_abm_pause(pipe->stream_res.abm, !enable, i, pipe->stream_res.tg->inst);
4987 }
4988