• 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 <linux/slab.h>
26 #include <linux/mm.h>
27 
28 #include "dm_services.h"
29 
30 #include "dc.h"
31 
32 #include "core_status.h"
33 #include "core_types.h"
34 #include "hw_sequencer.h"
35 #include "dce/dce_hwseq.h"
36 
37 #include "resource.h"
38 
39 #include "clk_mgr.h"
40 #include "clock_source.h"
41 #include "dc_bios_types.h"
42 
43 #include "bios_parser_interface.h"
44 #include "include/irq_service_interface.h"
45 #include "transform.h"
46 #include "dmcu.h"
47 #include "dpp.h"
48 #include "timing_generator.h"
49 #include "abm.h"
50 #include "virtual/virtual_link_encoder.h"
51 
52 #include "link_hwss.h"
53 #include "link_encoder.h"
54 
55 #include "dc_link_ddc.h"
56 #include "dm_helpers.h"
57 #include "mem_input.h"
58 #include "hubp.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 "dce/dmub_hw_lock_mgr.h"
72 
73 #define CTX \
74 	dc->ctx
75 
76 #define DC_LOGGER \
77 	dc->ctx->logger
78 
79 static const char DC_BUILD_ID[] = "production-build";
80 
81 /**
82  * DOC: Overview
83  *
84  * DC is the OS-agnostic component of the amdgpu DC driver.
85  *
86  * DC maintains and validates a set of structs representing the state of the
87  * driver and writes that state to AMD hardware
88  *
89  * Main DC HW structs:
90  *
91  * struct dc - The central struct.  One per driver.  Created on driver load,
92  * destroyed on driver unload.
93  *
94  * struct dc_context - One per driver.
95  * Used as a backpointer by most other structs in dc.
96  *
97  * struct dc_link - One per connector (the physical DP, HDMI, miniDP, or eDP
98  * plugpoints).  Created on driver load, destroyed on driver unload.
99  *
100  * struct dc_sink - One per display.  Created on boot or hotplug.
101  * Destroyed on shutdown or hotunplug.  A dc_link can have a local sink
102  * (the display directly attached).  It may also have one or more remote
103  * sinks (in the Multi-Stream Transport case)
104  *
105  * struct resource_pool - One per driver.  Represents the hw blocks not in the
106  * main pipeline.  Not directly accessible by dm.
107  *
108  * Main dc state structs:
109  *
110  * These structs can be created and destroyed as needed.  There is a full set of
111  * these structs in dc->current_state representing the currently programmed state.
112  *
113  * struct dc_state - The global DC state to track global state information,
114  * such as bandwidth values.
115  *
116  * struct dc_stream_state - Represents the hw configuration for the pipeline from
117  * a framebuffer to a display.  Maps one-to-one with dc_sink.
118  *
119  * struct dc_plane_state - Represents a framebuffer.  Each stream has at least one,
120  * and may have more in the Multi-Plane Overlay case.
121  *
122  * struct resource_context - Represents the programmable state of everything in
123  * the resource_pool.  Not directly accessible by dm.
124  *
125  * struct pipe_ctx - A member of struct resource_context.  Represents the
126  * internal hardware pipeline components.  Each dc_plane_state has either
127  * one or two (in the pipe-split case).
128  */
129 
130 /*******************************************************************************
131  * Private functions
132  ******************************************************************************/
133 
elevate_update_type(enum surface_update_type * original,enum surface_update_type new)134 static inline void elevate_update_type(enum surface_update_type *original, enum surface_update_type new)
135 {
136 	if (new > *original)
137 		*original = new;
138 }
139 
destroy_links(struct dc * dc)140 static void destroy_links(struct dc *dc)
141 {
142 	uint32_t i;
143 
144 	for (i = 0; i < dc->link_count; i++) {
145 		if (NULL != dc->links[i])
146 			link_destroy(&dc->links[i]);
147 	}
148 }
149 
create_links(struct dc * dc,uint32_t num_virtual_links)150 static bool create_links(
151 		struct dc *dc,
152 		uint32_t num_virtual_links)
153 {
154 	int i;
155 	int connectors_num;
156 	struct dc_bios *bios = dc->ctx->dc_bios;
157 
158 	dc->link_count = 0;
159 
160 	connectors_num = bios->funcs->get_connectors_number(bios);
161 
162 	if (connectors_num > ENUM_ID_COUNT) {
163 		dm_error(
164 			"DC: Number of connectors %d exceeds maximum of %d!\n",
165 			connectors_num,
166 			ENUM_ID_COUNT);
167 		return false;
168 	}
169 
170 	dm_output_to_console(
171 		"DC: %s: connectors_num: physical:%d, virtual:%d\n",
172 		__func__,
173 		connectors_num,
174 		num_virtual_links);
175 
176 	for (i = 0; i < connectors_num; i++) {
177 		struct link_init_data link_init_params = {0};
178 		struct dc_link *link;
179 
180 		link_init_params.ctx = dc->ctx;
181 		/* next BIOS object table connector */
182 		link_init_params.connector_index = i;
183 		link_init_params.link_index = dc->link_count;
184 		link_init_params.dc = dc;
185 		link = link_create(&link_init_params);
186 
187 		if (link) {
188 			bool should_destory_link = false;
189 
190 			if (link->connector_signal == SIGNAL_TYPE_EDP) {
191 				if (dc->config.edp_not_connected) {
192 					if (!IS_DIAG_DC(dc->ctx->dce_environment))
193 						should_destory_link = true;
194 				} else {
195 					enum dc_connection_type type;
196 					dc_link_detect_sink(link, &type);
197 					if (type == dc_connection_none)
198 						should_destory_link = true;
199 				}
200 			}
201 
202 			if (dc->config.force_enum_edp || !should_destory_link) {
203 				dc->links[dc->link_count] = link;
204 				link->dc = dc;
205 				++dc->link_count;
206 			} else {
207 				link_destroy(&link);
208 			}
209 		}
210 	}
211 
212 	for (i = 0; i < num_virtual_links; i++) {
213 		struct dc_link *link = kzalloc(sizeof(*link), GFP_KERNEL);
214 		struct encoder_init_data enc_init = {0};
215 
216 		if (link == NULL) {
217 			BREAK_TO_DEBUGGER();
218 			goto failed_alloc;
219 		}
220 
221 		link->link_index = dc->link_count;
222 		dc->links[dc->link_count] = link;
223 		dc->link_count++;
224 
225 		link->ctx = dc->ctx;
226 		link->dc = dc;
227 		link->connector_signal = SIGNAL_TYPE_VIRTUAL;
228 		link->link_id.type = OBJECT_TYPE_CONNECTOR;
229 		link->link_id.id = CONNECTOR_ID_VIRTUAL;
230 		link->link_id.enum_id = ENUM_ID_1;
231 		link->link_enc = kzalloc(sizeof(*link->link_enc), GFP_KERNEL);
232 
233 		if (!link->link_enc) {
234 			BREAK_TO_DEBUGGER();
235 			goto failed_alloc;
236 		}
237 
238 		link->link_status.dpcd_caps = &link->dpcd_caps;
239 
240 		enc_init.ctx = dc->ctx;
241 		enc_init.channel = CHANNEL_ID_UNKNOWN;
242 		enc_init.hpd_source = HPD_SOURCEID_UNKNOWN;
243 		enc_init.transmitter = TRANSMITTER_UNKNOWN;
244 		enc_init.connector = link->link_id;
245 		enc_init.encoder.type = OBJECT_TYPE_ENCODER;
246 		enc_init.encoder.id = ENCODER_ID_INTERNAL_VIRTUAL;
247 		enc_init.encoder.enum_id = ENUM_ID_1;
248 		virtual_link_encoder_construct(link->link_enc, &enc_init);
249 	}
250 
251 	return true;
252 
253 failed_alloc:
254 	return false;
255 }
256 
dc_perf_trace_create(void)257 static struct dc_perf_trace *dc_perf_trace_create(void)
258 {
259 	return kzalloc(sizeof(struct dc_perf_trace), GFP_KERNEL);
260 }
261 
dc_perf_trace_destroy(struct dc_perf_trace ** perf_trace)262 static void dc_perf_trace_destroy(struct dc_perf_trace **perf_trace)
263 {
264 	kfree(*perf_trace);
265 	*perf_trace = NULL;
266 }
267 
268 /**
269  *****************************************************************************
270  *  Function: dc_stream_adjust_vmin_vmax
271  *
272  *  @brief
273  *     Looks up the pipe context of dc_stream_state and updates the
274  *     vertical_total_min and vertical_total_max of the DRR, Dynamic Refresh
275  *     Rate, which is a power-saving feature that targets reducing panel
276  *     refresh rate while the screen is static
277  *
278  *  @param [in] dc: dc reference
279  *  @param [in] stream: Initial dc stream state
280  *  @param [in] adjust: Updated parameters for vertical_total_min and
281  *  vertical_total_max
282  *****************************************************************************
283  */
dc_stream_adjust_vmin_vmax(struct dc * dc,struct dc_stream_state * stream,struct dc_crtc_timing_adjust * adjust)284 bool dc_stream_adjust_vmin_vmax(struct dc *dc,
285 		struct dc_stream_state *stream,
286 		struct dc_crtc_timing_adjust *adjust)
287 {
288 	int i = 0;
289 	bool ret = false;
290 
291 	stream->adjust = *adjust;
292 
293 	for (i = 0; i < MAX_PIPES; i++) {
294 		struct pipe_ctx *pipe = &dc->current_state->res_ctx.pipe_ctx[i];
295 
296 		if (pipe->stream == stream && pipe->stream_res.tg) {
297 			dc->hwss.set_drr(&pipe,
298 					1,
299 					adjust->v_total_min,
300 					adjust->v_total_max,
301 					adjust->v_total_mid,
302 					adjust->v_total_mid_frame_num);
303 
304 			ret = true;
305 		}
306 	}
307 	return ret;
308 }
309 
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)310 bool dc_stream_get_crtc_position(struct dc *dc,
311 		struct dc_stream_state **streams, int num_streams,
312 		unsigned int *v_pos, unsigned int *nom_v_pos)
313 {
314 	/* TODO: Support multiple streams */
315 	const struct dc_stream_state *stream = streams[0];
316 	int i = 0;
317 	bool ret = false;
318 	struct crtc_position position;
319 
320 	for (i = 0; i < MAX_PIPES; i++) {
321 		struct pipe_ctx *pipe =
322 				&dc->current_state->res_ctx.pipe_ctx[i];
323 
324 		if (pipe->stream == stream && pipe->stream_res.stream_enc) {
325 			dc->hwss.get_position(&pipe, 1, &position);
326 
327 			*v_pos = position.vertical_count;
328 			*nom_v_pos = position.nominal_vcount;
329 			ret = true;
330 		}
331 	}
332 	return ret;
333 }
334 
335 /**
336  * dc_stream_configure_crc() - Configure CRC capture for the given stream.
337  * @dc: DC Object
338  * @stream: The stream to configure CRC on.
339  * @enable: Enable CRC if true, disable otherwise.
340  * @continuous: Capture CRC on every frame if true. Otherwise, only capture
341  *              once.
342  *
343  * By default, only CRC0 is configured, and the entire frame is used to
344  * calculate the crc.
345  */
dc_stream_configure_crc(struct dc * dc,struct dc_stream_state * stream,bool enable,bool continuous)346 bool dc_stream_configure_crc(struct dc *dc, struct dc_stream_state *stream,
347 			     bool enable, bool continuous)
348 {
349 	int i;
350 	struct pipe_ctx *pipe;
351 	struct crc_params param;
352 	struct timing_generator *tg;
353 
354 	for (i = 0; i < MAX_PIPES; i++) {
355 		pipe = &dc->current_state->res_ctx.pipe_ctx[i];
356 		if (pipe->stream == stream && !pipe->top_pipe && !pipe->prev_odm_pipe)
357 			break;
358 	}
359 	/* Stream not found */
360 	if (i == MAX_PIPES)
361 		return false;
362 
363 	/* Always capture the full frame */
364 	param.windowa_x_start = 0;
365 	param.windowa_y_start = 0;
366 	param.windowa_x_end = pipe->stream->timing.h_addressable;
367 	param.windowa_y_end = pipe->stream->timing.v_addressable;
368 	param.windowb_x_start = 0;
369 	param.windowb_y_start = 0;
370 	param.windowb_x_end = pipe->stream->timing.h_addressable;
371 	param.windowb_y_end = pipe->stream->timing.v_addressable;
372 
373 	param.dsc_mode = pipe->stream->timing.flags.DSC ? 1:0;
374 	param.odm_mode = pipe->next_odm_pipe ? 1:0;
375 
376 	/* Default to the union of both windows */
377 	param.selection = UNION_WINDOW_A_B;
378 	param.continuous_mode = continuous;
379 	param.enable = enable;
380 
381 	tg = pipe->stream_res.tg;
382 
383 	/* Only call if supported */
384 	if (tg->funcs->configure_crc)
385 		return tg->funcs->configure_crc(tg, &param);
386 	DC_LOG_WARNING("CRC capture not supported.");
387 	return false;
388 }
389 
390 /**
391  * dc_stream_get_crc() - Get CRC values for the given stream.
392  * @dc: DC object
393  * @stream: The DC stream state of the stream to get CRCs from.
394  * @r_cr, g_y, b_cb: CRC values for the three channels are stored here.
395  *
396  * dc_stream_configure_crc needs to be called beforehand to enable CRCs.
397  * Return false if stream is not found, or if CRCs are not enabled.
398  */
dc_stream_get_crc(struct dc * dc,struct dc_stream_state * stream,uint32_t * r_cr,uint32_t * g_y,uint32_t * b_cb)399 bool dc_stream_get_crc(struct dc *dc, struct dc_stream_state *stream,
400 		       uint32_t *r_cr, uint32_t *g_y, uint32_t *b_cb)
401 {
402 	int i;
403 	struct pipe_ctx *pipe;
404 	struct timing_generator *tg;
405 
406 	for (i = 0; i < MAX_PIPES; i++) {
407 		pipe = &dc->current_state->res_ctx.pipe_ctx[i];
408 		if (pipe->stream == stream)
409 			break;
410 	}
411 	/* Stream not found */
412 	if (i == MAX_PIPES)
413 		return false;
414 
415 	tg = pipe->stream_res.tg;
416 
417 	if (tg->funcs->get_crc)
418 		return tg->funcs->get_crc(tg, r_cr, g_y, b_cb);
419 	DC_LOG_WARNING("CRC capture not supported.");
420 	return false;
421 }
422 
dc_stream_set_dyn_expansion(struct dc * dc,struct dc_stream_state * stream,enum dc_dynamic_expansion option)423 void dc_stream_set_dyn_expansion(struct dc *dc, struct dc_stream_state *stream,
424 		enum dc_dynamic_expansion option)
425 {
426 	/* OPP FMT dyn expansion updates*/
427 	int i = 0;
428 	struct pipe_ctx *pipe_ctx;
429 
430 	for (i = 0; i < MAX_PIPES; i++) {
431 		if (dc->current_state->res_ctx.pipe_ctx[i].stream
432 				== stream) {
433 			pipe_ctx = &dc->current_state->res_ctx.pipe_ctx[i];
434 			pipe_ctx->stream_res.opp->dyn_expansion = option;
435 			pipe_ctx->stream_res.opp->funcs->opp_set_dyn_expansion(
436 					pipe_ctx->stream_res.opp,
437 					COLOR_SPACE_YCBCR601,
438 					stream->timing.display_color_depth,
439 					stream->signal);
440 		}
441 	}
442 }
443 
dc_stream_set_dither_option(struct dc_stream_state * stream,enum dc_dither_option option)444 void dc_stream_set_dither_option(struct dc_stream_state *stream,
445 		enum dc_dither_option option)
446 {
447 	struct bit_depth_reduction_params params;
448 	struct dc_link *link = stream->link;
449 	struct pipe_ctx *pipes = NULL;
450 	int i;
451 
452 	for (i = 0; i < MAX_PIPES; i++) {
453 		if (link->dc->current_state->res_ctx.pipe_ctx[i].stream ==
454 				stream) {
455 			pipes = &link->dc->current_state->res_ctx.pipe_ctx[i];
456 			break;
457 		}
458 	}
459 
460 	if (!pipes)
461 		return;
462 	if (option > DITHER_OPTION_MAX)
463 		return;
464 
465 	stream->dither_option = option;
466 
467 	memset(&params, 0, sizeof(params));
468 	resource_build_bit_depth_reduction_params(stream, &params);
469 	stream->bit_depth_params = params;
470 
471 	if (pipes->plane_res.xfm &&
472 	    pipes->plane_res.xfm->funcs->transform_set_pixel_storage_depth) {
473 		pipes->plane_res.xfm->funcs->transform_set_pixel_storage_depth(
474 			pipes->plane_res.xfm,
475 			pipes->plane_res.scl_data.lb_params.depth,
476 			&stream->bit_depth_params);
477 	}
478 
479 	pipes->stream_res.opp->funcs->
480 		opp_program_bit_depth_reduction(pipes->stream_res.opp, &params);
481 }
482 
dc_stream_set_gamut_remap(struct dc * dc,const struct dc_stream_state * stream)483 bool dc_stream_set_gamut_remap(struct dc *dc, const struct dc_stream_state *stream)
484 {
485 	int i = 0;
486 	bool ret = false;
487 	struct pipe_ctx *pipes;
488 
489 	for (i = 0; i < MAX_PIPES; i++) {
490 		if (dc->current_state->res_ctx.pipe_ctx[i].stream == stream) {
491 			pipes = &dc->current_state->res_ctx.pipe_ctx[i];
492 			dc->hwss.program_gamut_remap(pipes);
493 			ret = true;
494 		}
495 	}
496 
497 	return ret;
498 }
499 
dc_stream_program_csc_matrix(struct dc * dc,struct dc_stream_state * stream)500 bool dc_stream_program_csc_matrix(struct dc *dc, struct dc_stream_state *stream)
501 {
502 	int i = 0;
503 	bool ret = false;
504 	struct pipe_ctx *pipes;
505 
506 	for (i = 0; i < MAX_PIPES; i++) {
507 		if (dc->current_state->res_ctx.pipe_ctx[i].stream
508 				== stream) {
509 
510 			pipes = &dc->current_state->res_ctx.pipe_ctx[i];
511 			dc->hwss.program_output_csc(dc,
512 					pipes,
513 					stream->output_color_space,
514 					stream->csc_color_matrix.matrix,
515 					pipes->stream_res.opp->inst);
516 			ret = true;
517 		}
518 	}
519 
520 	return ret;
521 }
522 
dc_stream_set_static_screen_params(struct dc * dc,struct dc_stream_state ** streams,int num_streams,const struct dc_static_screen_params * params)523 void dc_stream_set_static_screen_params(struct dc *dc,
524 		struct dc_stream_state **streams,
525 		int num_streams,
526 		const struct dc_static_screen_params *params)
527 {
528 	int i = 0;
529 	int j = 0;
530 	struct pipe_ctx *pipes_affected[MAX_PIPES];
531 	int num_pipes_affected = 0;
532 
533 	for (i = 0; i < num_streams; i++) {
534 		struct dc_stream_state *stream = streams[i];
535 
536 		for (j = 0; j < MAX_PIPES; j++) {
537 			if (dc->current_state->res_ctx.pipe_ctx[j].stream
538 					== stream) {
539 				pipes_affected[num_pipes_affected++] =
540 						&dc->current_state->res_ctx.pipe_ctx[j];
541 			}
542 		}
543 	}
544 
545 	dc->hwss.set_static_screen_control(pipes_affected, num_pipes_affected, params);
546 }
547 
dc_destruct(struct dc * dc)548 static void dc_destruct(struct dc *dc)
549 {
550 	if (dc->current_state) {
551 		dc_release_state(dc->current_state);
552 		dc->current_state = NULL;
553 	}
554 
555 	destroy_links(dc);
556 
557 	if (dc->clk_mgr) {
558 		dc_destroy_clk_mgr(dc->clk_mgr);
559 		dc->clk_mgr = NULL;
560 	}
561 
562 	dc_destroy_resource_pool(dc);
563 
564 	if (dc->ctx->gpio_service)
565 		dal_gpio_service_destroy(&dc->ctx->gpio_service);
566 
567 	if (dc->ctx->created_bios)
568 		dal_bios_parser_destroy(&dc->ctx->dc_bios);
569 
570 	dc_perf_trace_destroy(&dc->ctx->perf_trace);
571 
572 	kfree(dc->ctx);
573 	dc->ctx = NULL;
574 
575 	kfree(dc->bw_vbios);
576 	dc->bw_vbios = NULL;
577 
578 	kfree(dc->bw_dceip);
579 	dc->bw_dceip = NULL;
580 
581 #ifdef CONFIG_DRM_AMD_DC_DCN
582 	kfree(dc->dcn_soc);
583 	dc->dcn_soc = NULL;
584 
585 	kfree(dc->dcn_ip);
586 	dc->dcn_ip = NULL;
587 
588 #endif
589 	kfree(dc->vm_helper);
590 	dc->vm_helper = NULL;
591 
592 }
593 
dc_construct_ctx(struct dc * dc,const struct dc_init_data * init_params)594 static bool dc_construct_ctx(struct dc *dc,
595 		const struct dc_init_data *init_params)
596 {
597 	struct dc_context *dc_ctx;
598 	enum dce_version dc_version = DCE_VERSION_UNKNOWN;
599 
600 	dc_ctx = kzalloc(sizeof(*dc_ctx), GFP_KERNEL);
601 	if (!dc_ctx)
602 		return false;
603 
604 	dc_ctx->cgs_device = init_params->cgs_device;
605 	dc_ctx->driver_context = init_params->driver;
606 	dc_ctx->dc = dc;
607 	dc_ctx->asic_id = init_params->asic_id;
608 	dc_ctx->dc_sink_id_count = 0;
609 	dc_ctx->dc_stream_id_count = 0;
610 	dc_ctx->dce_environment = init_params->dce_environment;
611 
612 	/* Create logger */
613 
614 	dc_version = resource_parse_asic_id(init_params->asic_id);
615 	dc_ctx->dce_version = dc_version;
616 
617 	dc_ctx->perf_trace = dc_perf_trace_create();
618 	if (!dc_ctx->perf_trace) {
619 		kfree(dc_ctx);
620 		ASSERT_CRITICAL(false);
621 		return false;
622 	}
623 
624 	dc->ctx = dc_ctx;
625 
626 	return true;
627 }
628 
dc_construct(struct dc * dc,const struct dc_init_data * init_params)629 static bool dc_construct(struct dc *dc,
630 		const struct dc_init_data *init_params)
631 {
632 	struct dc_context *dc_ctx;
633 	struct bw_calcs_dceip *dc_dceip;
634 	struct bw_calcs_vbios *dc_vbios;
635 #ifdef CONFIG_DRM_AMD_DC_DCN
636 	struct dcn_soc_bounding_box *dcn_soc;
637 	struct dcn_ip_params *dcn_ip;
638 #endif
639 
640 	dc->config = init_params->flags;
641 
642 	// Allocate memory for the vm_helper
643 	dc->vm_helper = kzalloc(sizeof(struct vm_helper), GFP_KERNEL);
644 	if (!dc->vm_helper) {
645 		dm_error("%s: failed to create dc->vm_helper\n", __func__);
646 		goto fail;
647 	}
648 
649 	memcpy(&dc->bb_overrides, &init_params->bb_overrides, sizeof(dc->bb_overrides));
650 
651 	dc_dceip = kzalloc(sizeof(*dc_dceip), GFP_KERNEL);
652 	if (!dc_dceip) {
653 		dm_error("%s: failed to create dceip\n", __func__);
654 		goto fail;
655 	}
656 
657 	dc->bw_dceip = dc_dceip;
658 
659 	dc_vbios = kzalloc(sizeof(*dc_vbios), GFP_KERNEL);
660 	if (!dc_vbios) {
661 		dm_error("%s: failed to create vbios\n", __func__);
662 		goto fail;
663 	}
664 
665 	dc->bw_vbios = dc_vbios;
666 #ifdef CONFIG_DRM_AMD_DC_DCN
667 	dcn_soc = kzalloc(sizeof(*dcn_soc), GFP_KERNEL);
668 	if (!dcn_soc) {
669 		dm_error("%s: failed to create dcn_soc\n", __func__);
670 		goto fail;
671 	}
672 
673 	dc->dcn_soc = dcn_soc;
674 
675 	dcn_ip = kzalloc(sizeof(*dcn_ip), GFP_KERNEL);
676 	if (!dcn_ip) {
677 		dm_error("%s: failed to create dcn_ip\n", __func__);
678 		goto fail;
679 	}
680 
681 	dc->dcn_ip = dcn_ip;
682 	dc->soc_bounding_box = init_params->soc_bounding_box;
683 #endif
684 
685 	if (!dc_construct_ctx(dc, init_params)) {
686 		dm_error("%s: failed to create ctx\n", __func__);
687 		goto fail;
688 	}
689 
690         dc_ctx = dc->ctx;
691 
692 	/* Resource should construct all asic specific resources.
693 	 * This should be the only place where we need to parse the asic id
694 	 */
695 	if (init_params->vbios_override)
696 		dc_ctx->dc_bios = init_params->vbios_override;
697 	else {
698 		/* Create BIOS parser */
699 		struct bp_init_data bp_init_data;
700 
701 		bp_init_data.ctx = dc_ctx;
702 		bp_init_data.bios = init_params->asic_id.atombios_base_address;
703 
704 		dc_ctx->dc_bios = dal_bios_parser_create(
705 				&bp_init_data, dc_ctx->dce_version);
706 
707 		if (!dc_ctx->dc_bios) {
708 			ASSERT_CRITICAL(false);
709 			goto fail;
710 		}
711 
712 		dc_ctx->created_bios = true;
713 	}
714 
715 	dc->vendor_signature = init_params->vendor_signature;
716 
717 	/* Create GPIO service */
718 	dc_ctx->gpio_service = dal_gpio_service_create(
719 			dc_ctx->dce_version,
720 			dc_ctx->dce_environment,
721 			dc_ctx);
722 
723 	if (!dc_ctx->gpio_service) {
724 		ASSERT_CRITICAL(false);
725 		goto fail;
726 	}
727 
728 	dc->res_pool = dc_create_resource_pool(dc, init_params, dc_ctx->dce_version);
729 	if (!dc->res_pool)
730 		goto fail;
731 
732 	dc->clk_mgr = dc_clk_mgr_create(dc->ctx, dc->res_pool->pp_smu, dc->res_pool->dccg);
733 	if (!dc->clk_mgr)
734 		goto fail;
735 #ifdef CONFIG_DRM_AMD_DC_DCN3_0
736 	dc->clk_mgr->force_smu_not_present = init_params->force_smu_not_present;
737 
738 	dc->debug.force_ignore_link_settings = init_params->force_ignore_link_settings;
739 
740 	if (dc->res_pool->funcs->update_bw_bounding_box) {
741 		DC_FP_START();
742 		dc->res_pool->funcs->update_bw_bounding_box(dc, dc->clk_mgr->bw_params);
743 		DC_FP_END();
744 	}
745 #endif
746 
747 	/* Creation of current_state must occur after dc->dml
748 	 * is initialized in dc_create_resource_pool because
749 	 * on creation it copies the contents of dc->dml
750 	 */
751 
752 	dc->current_state = dc_create_state(dc);
753 
754 	if (!dc->current_state) {
755 		dm_error("%s: failed to create validate ctx\n", __func__);
756 		goto fail;
757 	}
758 
759 	dc_resource_state_construct(dc, dc->current_state);
760 
761 	if (!create_links(dc, init_params->num_virtual_links))
762 		goto fail;
763 
764 	return true;
765 
766 fail:
767 	return false;
768 }
769 
disable_all_writeback_pipes_for_stream(const struct dc * dc,struct dc_stream_state * stream,struct dc_state * context)770 static bool disable_all_writeback_pipes_for_stream(
771 		const struct dc *dc,
772 		struct dc_stream_state *stream,
773 		struct dc_state *context)
774 {
775 	int i;
776 
777 	for (i = 0; i < stream->num_wb_info; i++)
778 		stream->writeback_info[i].wb_enabled = false;
779 
780 	return true;
781 }
782 
apply_ctx_interdependent_lock(struct dc * dc,struct dc_state * context,struct dc_stream_state * stream,bool lock)783 void apply_ctx_interdependent_lock(struct dc *dc, struct dc_state *context, struct dc_stream_state *stream, bool lock)
784 {
785 	int i = 0;
786 
787 	/* Checks if interdependent update function pointer is NULL or not, takes care of DCE110 case */
788 	if (dc->hwss.interdependent_update_lock)
789 		dc->hwss.interdependent_update_lock(dc, context, lock);
790 	else {
791 		for (i = 0; i < dc->res_pool->pipe_count; i++) {
792 			struct pipe_ctx *pipe_ctx = &context->res_ctx.pipe_ctx[i];
793 			struct pipe_ctx *old_pipe_ctx = &dc->current_state->res_ctx.pipe_ctx[i];
794 
795 			// Copied conditions that were previously in dce110_apply_ctx_for_surface
796 			if (stream == pipe_ctx->stream) {
797 				if (!pipe_ctx->top_pipe &&
798 					(pipe_ctx->plane_state || old_pipe_ctx->plane_state))
799 					dc->hwss.pipe_control_lock(dc, pipe_ctx, lock);
800 			}
801 		}
802 	}
803 }
804 
disable_dangling_plane(struct dc * dc,struct dc_state * context)805 static void disable_dangling_plane(struct dc *dc, struct dc_state *context)
806 {
807 	int i, j;
808 	struct dc_state *dangling_context = dc_create_state(dc);
809 	struct dc_state *current_ctx;
810 
811 	if (dangling_context == NULL)
812 		return;
813 
814 	dc_resource_state_copy_construct(dc->current_state, dangling_context);
815 
816 	for (i = 0; i < dc->res_pool->pipe_count; i++) {
817 		struct dc_stream_state *old_stream =
818 				dc->current_state->res_ctx.pipe_ctx[i].stream;
819 		bool should_disable = true;
820 
821 		for (j = 0; j < context->stream_count; j++) {
822 			if (old_stream == context->streams[j]) {
823 				should_disable = false;
824 				break;
825 			}
826 		}
827 		if (should_disable && old_stream) {
828 			dc_rem_all_planes_for_stream(dc, old_stream, dangling_context);
829 			disable_all_writeback_pipes_for_stream(dc, old_stream, dangling_context);
830 
831 			if (dc->hwss.apply_ctx_for_surface) {
832 				apply_ctx_interdependent_lock(dc, dc->current_state, old_stream, true);
833 				dc->hwss.apply_ctx_for_surface(dc, old_stream, 0, dangling_context);
834 				apply_ctx_interdependent_lock(dc, dc->current_state, old_stream, false);
835 				dc->hwss.post_unlock_program_front_end(dc, dangling_context);
836 			}
837 			if (dc->hwss.program_front_end_for_ctx) {
838 				dc->hwss.interdependent_update_lock(dc, dc->current_state, true);
839 				dc->hwss.program_front_end_for_ctx(dc, dangling_context);
840 				dc->hwss.interdependent_update_lock(dc, dc->current_state, false);
841 				dc->hwss.post_unlock_program_front_end(dc, dangling_context);
842 			}
843 		}
844 	}
845 
846 	current_ctx = dc->current_state;
847 	dc->current_state = dangling_context;
848 	dc_release_state(current_ctx);
849 }
850 
disable_vbios_mode_if_required(struct dc * dc,struct dc_state * context)851 static void disable_vbios_mode_if_required(
852 		struct dc *dc,
853 		struct dc_state *context)
854 {
855 	unsigned int i, j;
856 
857 	/* check if timing_changed, disable stream*/
858 	for (i = 0; i < dc->res_pool->pipe_count; i++) {
859 		struct dc_stream_state *stream = NULL;
860 		struct dc_link *link = NULL;
861 		struct pipe_ctx *pipe = NULL;
862 
863 		pipe = &context->res_ctx.pipe_ctx[i];
864 		stream = pipe->stream;
865 		if (stream == NULL)
866 			continue;
867 
868 		if (stream->apply_seamless_boot_optimization)
869 			continue;
870 
871 		// only looking for first odm pipe
872 		if (pipe->prev_odm_pipe)
873 			continue;
874 
875 		if (stream->link->local_sink &&
876 			stream->link->local_sink->sink_signal == SIGNAL_TYPE_EDP) {
877 			link = stream->link;
878 		}
879 
880 		if (link != NULL && link->link_enc->funcs->is_dig_enabled(link->link_enc)) {
881 			unsigned int enc_inst, tg_inst = 0;
882 			unsigned int pix_clk_100hz;
883 
884 			enc_inst = link->link_enc->funcs->get_dig_frontend(link->link_enc);
885 			if (enc_inst != ENGINE_ID_UNKNOWN) {
886 				for (j = 0; j < dc->res_pool->stream_enc_count; j++) {
887 					if (dc->res_pool->stream_enc[j]->id == enc_inst) {
888 						tg_inst = dc->res_pool->stream_enc[j]->funcs->dig_source_otg(
889 							dc->res_pool->stream_enc[j]);
890 						break;
891 					}
892 				}
893 
894 				dc->res_pool->dp_clock_source->funcs->get_pixel_clk_frequency_100hz(
895 					dc->res_pool->dp_clock_source,
896 					tg_inst, &pix_clk_100hz);
897 
898 				if (link->link_status.link_active) {
899 					uint32_t requested_pix_clk_100hz =
900 						pipe->stream_res.pix_clk_params.requested_pix_clk_100hz;
901 
902 					if (pix_clk_100hz != requested_pix_clk_100hz) {
903 						core_link_disable_stream(pipe);
904 						pipe->stream->dpms_off = false;
905 					}
906 				}
907 			}
908 		}
909 	}
910 }
911 
wait_for_no_pipes_pending(struct dc * dc,struct dc_state * context)912 static void wait_for_no_pipes_pending(struct dc *dc, struct dc_state *context)
913 {
914 	int i;
915 	PERF_TRACE();
916 	for (i = 0; i < MAX_PIPES; i++) {
917 		int count = 0;
918 		struct pipe_ctx *pipe = &context->res_ctx.pipe_ctx[i];
919 
920 		if (!pipe->plane_state)
921 			continue;
922 
923 		/* Timeout 100 ms */
924 		while (count < 100000) {
925 			/* Must set to false to start with, due to OR in update function */
926 			pipe->plane_state->status.is_flip_pending = false;
927 			dc->hwss.update_pending_status(pipe);
928 			if (!pipe->plane_state->status.is_flip_pending)
929 				break;
930 			udelay(1);
931 			count++;
932 		}
933 		ASSERT(!pipe->plane_state->status.is_flip_pending);
934 	}
935 	PERF_TRACE();
936 }
937 
938 /*******************************************************************************
939  * Public functions
940  ******************************************************************************/
941 
dc_create(const struct dc_init_data * init_params)942 struct dc *dc_create(const struct dc_init_data *init_params)
943 {
944 	struct dc *dc = kzalloc(sizeof(*dc), GFP_KERNEL);
945 	unsigned int full_pipe_count;
946 
947 	if (NULL == dc)
948 		goto alloc_fail;
949 
950 	if (init_params->dce_environment == DCE_ENV_VIRTUAL_HW) {
951 		if (false == dc_construct_ctx(dc, init_params)) {
952 			dc_destruct(dc);
953 			goto construct_fail;
954 		}
955 	} else {
956 		if (false == dc_construct(dc, init_params)) {
957 			dc_destruct(dc);
958 			goto construct_fail;
959 		}
960 
961 		full_pipe_count = dc->res_pool->pipe_count;
962 		if (dc->res_pool->underlay_pipe_index != NO_UNDERLAY_PIPE)
963 			full_pipe_count--;
964 		dc->caps.max_streams = min(
965 				full_pipe_count,
966 				dc->res_pool->stream_enc_count);
967 
968 		dc->optimize_seamless_boot_streams = 0;
969 		dc->caps.max_links = dc->link_count;
970 		dc->caps.max_audios = dc->res_pool->audio_count;
971 		dc->caps.linear_pitch_alignment = 64;
972 
973 		dc->caps.max_dp_protocol_version = DP_VERSION_1_4;
974 
975 		if (dc->res_pool->dmcu != NULL)
976 			dc->versions.dmcu_version = dc->res_pool->dmcu->dmcu_version;
977 	}
978 
979 	/* Populate versioning information */
980 	dc->versions.dc_ver = DC_VER;
981 
982 	dc->build_id = DC_BUILD_ID;
983 
984 	DC_LOG_DC("Display Core initialized\n");
985 
986 
987 
988 	return dc;
989 
990 construct_fail:
991 	kfree(dc);
992 
993 alloc_fail:
994 	return NULL;
995 }
996 
dc_hardware_init(struct dc * dc)997 void dc_hardware_init(struct dc *dc)
998 {
999 	if (dc->ctx->dce_environment != DCE_ENV_VIRTUAL_HW)
1000 		dc->hwss.init_hw(dc);
1001 }
1002 
dc_init_callbacks(struct dc * dc,const struct dc_callback_init * init_params)1003 void dc_init_callbacks(struct dc *dc,
1004 		const struct dc_callback_init *init_params)
1005 {
1006 #ifdef CONFIG_DRM_AMD_DC_HDCP
1007 	dc->ctx->cp_psp = init_params->cp_psp;
1008 #endif
1009 }
1010 
dc_deinit_callbacks(struct dc * dc)1011 void dc_deinit_callbacks(struct dc *dc)
1012 {
1013 #ifdef CONFIG_DRM_AMD_DC_HDCP
1014 	memset(&dc->ctx->cp_psp, 0, sizeof(dc->ctx->cp_psp));
1015 #endif
1016 }
1017 
dc_destroy(struct dc ** dc)1018 void dc_destroy(struct dc **dc)
1019 {
1020 	dc_destruct(*dc);
1021 	kfree(*dc);
1022 	*dc = NULL;
1023 }
1024 
enable_timing_multisync(struct dc * dc,struct dc_state * ctx)1025 static void enable_timing_multisync(
1026 		struct dc *dc,
1027 		struct dc_state *ctx)
1028 {
1029 	int i = 0, multisync_count = 0;
1030 	int pipe_count = dc->res_pool->pipe_count;
1031 	struct pipe_ctx *multisync_pipes[MAX_PIPES] = { NULL };
1032 
1033 	for (i = 0; i < pipe_count; i++) {
1034 		if (!ctx->res_ctx.pipe_ctx[i].stream ||
1035 				!ctx->res_ctx.pipe_ctx[i].stream->triggered_crtc_reset.enabled)
1036 			continue;
1037 		if (ctx->res_ctx.pipe_ctx[i].stream == ctx->res_ctx.pipe_ctx[i].stream->triggered_crtc_reset.event_source)
1038 			continue;
1039 		multisync_pipes[multisync_count] = &ctx->res_ctx.pipe_ctx[i];
1040 		multisync_count++;
1041 	}
1042 
1043 	if (multisync_count > 0) {
1044 		dc->hwss.enable_per_frame_crtc_position_reset(
1045 			dc, multisync_count, multisync_pipes);
1046 	}
1047 }
1048 
program_timing_sync(struct dc * dc,struct dc_state * ctx)1049 static void program_timing_sync(
1050 		struct dc *dc,
1051 		struct dc_state *ctx)
1052 {
1053 	int i, j, k;
1054 	int group_index = 0;
1055 	int num_group = 0;
1056 	int pipe_count = dc->res_pool->pipe_count;
1057 	struct pipe_ctx *unsynced_pipes[MAX_PIPES] = { NULL };
1058 
1059 	for (i = 0; i < pipe_count; i++) {
1060 		if (!ctx->res_ctx.pipe_ctx[i].stream || ctx->res_ctx.pipe_ctx[i].top_pipe)
1061 			continue;
1062 
1063 		unsynced_pipes[i] = &ctx->res_ctx.pipe_ctx[i];
1064 	}
1065 
1066 	for (i = 0; i < pipe_count; i++) {
1067 		int group_size = 1;
1068 		struct pipe_ctx *pipe_set[MAX_PIPES];
1069 
1070 		if (!unsynced_pipes[i])
1071 			continue;
1072 
1073 		pipe_set[0] = unsynced_pipes[i];
1074 		unsynced_pipes[i] = NULL;
1075 
1076 		/* Add tg to the set, search rest of the tg's for ones with
1077 		 * same timing, add all tgs with same timing to the group
1078 		 */
1079 		for (j = i + 1; j < pipe_count; j++) {
1080 			if (!unsynced_pipes[j])
1081 				continue;
1082 
1083 			if (resource_are_streams_timing_synchronizable(
1084 					unsynced_pipes[j]->stream,
1085 					pipe_set[0]->stream)) {
1086 				pipe_set[group_size] = unsynced_pipes[j];
1087 				unsynced_pipes[j] = NULL;
1088 				group_size++;
1089 			}
1090 		}
1091 
1092 		/* set first unblanked pipe as master */
1093 		for (j = 0; j < group_size; j++) {
1094 			bool is_blanked;
1095 
1096 			if (pipe_set[j]->stream_res.opp->funcs->dpg_is_blanked)
1097 				is_blanked =
1098 					pipe_set[j]->stream_res.opp->funcs->dpg_is_blanked(pipe_set[j]->stream_res.opp);
1099 			else
1100 				is_blanked =
1101 					pipe_set[j]->stream_res.tg->funcs->is_blanked(pipe_set[j]->stream_res.tg);
1102 			if (!is_blanked) {
1103 				if (j == 0)
1104 					break;
1105 
1106 				swap(pipe_set[0], pipe_set[j]);
1107 				break;
1108 			}
1109 		}
1110 
1111 
1112 		for (k = 0; k < group_size; k++) {
1113 			struct dc_stream_status *status = dc_stream_get_status_from_state(ctx, pipe_set[k]->stream);
1114 
1115 			status->timing_sync_info.group_id = num_group;
1116 			status->timing_sync_info.group_size = group_size;
1117 			if (k == 0)
1118 				status->timing_sync_info.master = true;
1119 			else
1120 				status->timing_sync_info.master = false;
1121 
1122 		}
1123 		/* remove any other unblanked pipes as they have already been synced */
1124 		for (j = j + 1; j < group_size; j++) {
1125 			bool is_blanked;
1126 
1127 			if (pipe_set[j]->stream_res.opp->funcs->dpg_is_blanked)
1128 				is_blanked =
1129 					pipe_set[j]->stream_res.opp->funcs->dpg_is_blanked(pipe_set[j]->stream_res.opp);
1130 			else
1131 				is_blanked =
1132 					pipe_set[j]->stream_res.tg->funcs->is_blanked(pipe_set[j]->stream_res.tg);
1133 			if (!is_blanked) {
1134 				group_size--;
1135 				pipe_set[j] = pipe_set[group_size];
1136 				j--;
1137 			}
1138 		}
1139 
1140 		if (group_size > 1) {
1141 			dc->hwss.enable_timing_synchronization(
1142 				dc, group_index, group_size, pipe_set);
1143 			group_index++;
1144 		}
1145 		num_group++;
1146 	}
1147 }
1148 
context_changed(struct dc * dc,struct dc_state * context)1149 static bool context_changed(
1150 		struct dc *dc,
1151 		struct dc_state *context)
1152 {
1153 	uint8_t i;
1154 
1155 	if (context->stream_count != dc->current_state->stream_count)
1156 		return true;
1157 
1158 	for (i = 0; i < dc->current_state->stream_count; i++) {
1159 		if (dc->current_state->streams[i] != context->streams[i])
1160 			return true;
1161 	}
1162 
1163 	return false;
1164 }
1165 
dc_validate_seamless_boot_timing(const struct dc * dc,const struct dc_sink * sink,struct dc_crtc_timing * crtc_timing)1166 bool dc_validate_seamless_boot_timing(const struct dc *dc,
1167 				const struct dc_sink *sink,
1168 				struct dc_crtc_timing *crtc_timing)
1169 {
1170 	struct timing_generator *tg;
1171 	struct stream_encoder *se = NULL;
1172 
1173 	struct dc_crtc_timing hw_crtc_timing = {0};
1174 
1175 	struct dc_link *link = sink->link;
1176 	unsigned int i, enc_inst, tg_inst = 0;
1177 
1178 	// Seamless port only support single DP and EDP so far
1179 	if (sink->sink_signal != SIGNAL_TYPE_DISPLAY_PORT &&
1180 		sink->sink_signal != SIGNAL_TYPE_EDP)
1181 		return false;
1182 
1183 	/* Check for enabled DIG to identify enabled display */
1184 	if (!link->link_enc->funcs->is_dig_enabled(link->link_enc))
1185 		return false;
1186 
1187 	enc_inst = link->link_enc->funcs->get_dig_frontend(link->link_enc);
1188 
1189 	if (enc_inst == ENGINE_ID_UNKNOWN)
1190 		return false;
1191 
1192 	for (i = 0; i < dc->res_pool->stream_enc_count; i++) {
1193 		if (dc->res_pool->stream_enc[i]->id == enc_inst) {
1194 
1195 			se = dc->res_pool->stream_enc[i];
1196 
1197 			tg_inst = dc->res_pool->stream_enc[i]->funcs->dig_source_otg(
1198 				dc->res_pool->stream_enc[i]);
1199 			break;
1200 		}
1201 	}
1202 
1203 	// tg_inst not found
1204 	if (i == dc->res_pool->stream_enc_count)
1205 		return false;
1206 
1207 	if (tg_inst >= dc->res_pool->timing_generator_count)
1208 		return false;
1209 
1210 	tg = dc->res_pool->timing_generators[tg_inst];
1211 
1212 	if (!tg->funcs->get_hw_timing)
1213 		return false;
1214 
1215 	if (!tg->funcs->get_hw_timing(tg, &hw_crtc_timing))
1216 		return false;
1217 
1218 	if (crtc_timing->h_total != hw_crtc_timing.h_total)
1219 		return false;
1220 
1221 	if (crtc_timing->h_border_left != hw_crtc_timing.h_border_left)
1222 		return false;
1223 
1224 	if (crtc_timing->h_addressable != hw_crtc_timing.h_addressable)
1225 		return false;
1226 
1227 	if (crtc_timing->h_border_right != hw_crtc_timing.h_border_right)
1228 		return false;
1229 
1230 	if (crtc_timing->h_front_porch != hw_crtc_timing.h_front_porch)
1231 		return false;
1232 
1233 	if (crtc_timing->h_sync_width != hw_crtc_timing.h_sync_width)
1234 		return false;
1235 
1236 	if (crtc_timing->v_total != hw_crtc_timing.v_total)
1237 		return false;
1238 
1239 	if (crtc_timing->v_border_top != hw_crtc_timing.v_border_top)
1240 		return false;
1241 
1242 	if (crtc_timing->v_addressable != hw_crtc_timing.v_addressable)
1243 		return false;
1244 
1245 	if (crtc_timing->v_border_bottom != hw_crtc_timing.v_border_bottom)
1246 		return false;
1247 
1248 	if (crtc_timing->v_front_porch != hw_crtc_timing.v_front_porch)
1249 		return false;
1250 
1251 	if (crtc_timing->v_sync_width != hw_crtc_timing.v_sync_width)
1252 		return false;
1253 
1254 	if (dc_is_dp_signal(link->connector_signal)) {
1255 		unsigned int pix_clk_100hz;
1256 
1257 		dc->res_pool->dp_clock_source->funcs->get_pixel_clk_frequency_100hz(
1258 			dc->res_pool->dp_clock_source,
1259 			tg_inst, &pix_clk_100hz);
1260 
1261 		if (crtc_timing->pix_clk_100hz != pix_clk_100hz)
1262 			return false;
1263 
1264 		if (!se->funcs->dp_get_pixel_format)
1265 			return false;
1266 
1267 		if (!se->funcs->dp_get_pixel_format(
1268 			se,
1269 			&hw_crtc_timing.pixel_encoding,
1270 			&hw_crtc_timing.display_color_depth))
1271 			return false;
1272 
1273 		if (hw_crtc_timing.display_color_depth != crtc_timing->display_color_depth)
1274 			return false;
1275 
1276 		if (hw_crtc_timing.pixel_encoding != crtc_timing->pixel_encoding)
1277 			return false;
1278 	}
1279 
1280 	return true;
1281 }
1282 
dc_enable_stereo(struct dc * dc,struct dc_state * context,struct dc_stream_state * streams[],uint8_t stream_count)1283 bool dc_enable_stereo(
1284 	struct dc *dc,
1285 	struct dc_state *context,
1286 	struct dc_stream_state *streams[],
1287 	uint8_t stream_count)
1288 {
1289 	bool ret = true;
1290 	int i, j;
1291 	struct pipe_ctx *pipe;
1292 
1293 	for (i = 0; i < MAX_PIPES; i++) {
1294 		if (context != NULL)
1295 			pipe = &context->res_ctx.pipe_ctx[i];
1296 		else
1297 			pipe = &dc->current_state->res_ctx.pipe_ctx[i];
1298 		for (j = 0 ; pipe && j < stream_count; j++)  {
1299 			if (streams[j] && streams[j] == pipe->stream &&
1300 				dc->hwss.setup_stereo)
1301 				dc->hwss.setup_stereo(pipe, dc);
1302 		}
1303 	}
1304 
1305 	return ret;
1306 }
1307 
dc_trigger_sync(struct dc * dc,struct dc_state * context)1308 void dc_trigger_sync(struct dc *dc, struct dc_state *context)
1309 {
1310 	if (context->stream_count > 1 && !dc->debug.disable_timing_sync) {
1311 		enable_timing_multisync(dc, context);
1312 		program_timing_sync(dc, context);
1313 	}
1314 }
1315 
get_stream_mask(struct dc * dc,struct dc_state * context)1316 static uint8_t get_stream_mask(struct dc *dc, struct dc_state *context)
1317 {
1318 	int i;
1319 	unsigned int stream_mask = 0;
1320 
1321 	for (i = 0; i < dc->res_pool->pipe_count; i++) {
1322 		if (context->res_ctx.pipe_ctx[i].stream)
1323 			stream_mask |= 1 << i;
1324 	}
1325 
1326 	return stream_mask;
1327 }
1328 
1329 /*
1330  * Applies given context to HW and copy it into current context.
1331  * It's up to the user to release the src context afterwards.
1332  */
dc_commit_state_no_check(struct dc * dc,struct dc_state * context)1333 static enum dc_status dc_commit_state_no_check(struct dc *dc, struct dc_state *context)
1334 {
1335 	struct dc_bios *dcb = dc->ctx->dc_bios;
1336 	enum dc_status result = DC_ERROR_UNEXPECTED;
1337 	struct pipe_ctx *pipe;
1338 	int i, k, l;
1339 	struct dc_stream_state *dc_streams[MAX_STREAMS] = {0};
1340 
1341 #if defined(CONFIG_DRM_AMD_DC_DCN3_0)
1342 	dc_allow_idle_optimizations(dc, false);
1343 #endif
1344 
1345 	for (i = 0; i < context->stream_count; i++)
1346 		dc_streams[i] =  context->streams[i];
1347 
1348 	if (!dcb->funcs->is_accelerated_mode(dcb)) {
1349 		disable_vbios_mode_if_required(dc, context);
1350 		dc->hwss.enable_accelerated_mode(dc, context);
1351 	}
1352 
1353 	for (i = 0; i < context->stream_count; i++)
1354 		if (context->streams[i]->apply_seamless_boot_optimization)
1355 			dc->optimize_seamless_boot_streams++;
1356 
1357 	if (context->stream_count > dc->optimize_seamless_boot_streams ||
1358 		context->stream_count == 0)
1359 		dc->hwss.prepare_bandwidth(dc, context);
1360 
1361 	disable_dangling_plane(dc, context);
1362 	/* re-program planes for existing stream, in case we need to
1363 	 * free up plane resource for later use
1364 	 */
1365 	if (dc->hwss.apply_ctx_for_surface) {
1366 		for (i = 0; i < context->stream_count; i++) {
1367 			if (context->streams[i]->mode_changed)
1368 				continue;
1369 			apply_ctx_interdependent_lock(dc, context, context->streams[i], true);
1370 			dc->hwss.apply_ctx_for_surface(
1371 				dc, context->streams[i],
1372 				context->stream_status[i].plane_count,
1373 				context); /* use new pipe config in new context */
1374 			apply_ctx_interdependent_lock(dc, context, context->streams[i], false);
1375 			dc->hwss.post_unlock_program_front_end(dc, context);
1376 		}
1377 	}
1378 
1379 	/* Program hardware */
1380 	for (i = 0; i < dc->res_pool->pipe_count; i++) {
1381 		pipe = &context->res_ctx.pipe_ctx[i];
1382 		dc->hwss.wait_for_mpcc_disconnect(dc, dc->res_pool, pipe);
1383 	}
1384 
1385 	result = dc->hwss.apply_ctx_to_hw(dc, context);
1386 
1387 	if (result != DC_OK)
1388 		return result;
1389 
1390 	dc_trigger_sync(dc, context);
1391 
1392 	/* Program all planes within new context*/
1393 	if (dc->hwss.program_front_end_for_ctx) {
1394 		dc->hwss.interdependent_update_lock(dc, context, true);
1395 		dc->hwss.program_front_end_for_ctx(dc, context);
1396 		dc->hwss.interdependent_update_lock(dc, context, false);
1397 		dc->hwss.post_unlock_program_front_end(dc, context);
1398 	}
1399 	for (i = 0; i < context->stream_count; i++) {
1400 		const struct dc_link *link = context->streams[i]->link;
1401 
1402 		if (!context->streams[i]->mode_changed)
1403 			continue;
1404 
1405 		if (dc->hwss.apply_ctx_for_surface) {
1406 			apply_ctx_interdependent_lock(dc, context, context->streams[i], true);
1407 			dc->hwss.apply_ctx_for_surface(
1408 					dc, context->streams[i],
1409 					context->stream_status[i].plane_count,
1410 					context);
1411 			apply_ctx_interdependent_lock(dc, context, context->streams[i], false);
1412 			dc->hwss.post_unlock_program_front_end(dc, context);
1413 		}
1414 
1415 		/*
1416 		 * enable stereo
1417 		 * TODO rework dc_enable_stereo call to work with validation sets?
1418 		 */
1419 		for (k = 0; k < MAX_PIPES; k++) {
1420 			pipe = &context->res_ctx.pipe_ctx[k];
1421 
1422 			for (l = 0 ; pipe && l < context->stream_count; l++)  {
1423 				if (context->streams[l] &&
1424 					context->streams[l] == pipe->stream &&
1425 					dc->hwss.setup_stereo)
1426 					dc->hwss.setup_stereo(pipe, dc);
1427 			}
1428 		}
1429 
1430 		CONN_MSG_MODE(link, "{%dx%d, %dx%d@%dKhz}",
1431 				context->streams[i]->timing.h_addressable,
1432 				context->streams[i]->timing.v_addressable,
1433 				context->streams[i]->timing.h_total,
1434 				context->streams[i]->timing.v_total,
1435 				context->streams[i]->timing.pix_clk_100hz / 10);
1436 	}
1437 
1438 	dc_enable_stereo(dc, context, dc_streams, context->stream_count);
1439 
1440 	if (context->stream_count > dc->optimize_seamless_boot_streams ||
1441 		context->stream_count == 0) {
1442 		/* Must wait for no flips to be pending before doing optimize bw */
1443 		wait_for_no_pipes_pending(dc, context);
1444 		/* pplib is notified if disp_num changed */
1445 		dc->hwss.optimize_bandwidth(dc, context);
1446 		/* Need to do otg sync again as otg could be out of sync due to otg
1447 		 * workaround applied during clock update
1448 		 */
1449 		dc_trigger_sync(dc, context);
1450 	}
1451 
1452 	context->stream_mask = get_stream_mask(dc, context);
1453 
1454 	if (context->stream_mask != dc->current_state->stream_mask)
1455 		dc_dmub_srv_notify_stream_mask(dc->ctx->dmub_srv, context->stream_mask);
1456 
1457 	for (i = 0; i < context->stream_count; i++)
1458 		context->streams[i]->mode_changed = false;
1459 
1460 	dc_release_state(dc->current_state);
1461 
1462 	dc->current_state = context;
1463 
1464 	dc_retain_state(dc->current_state);
1465 
1466 	return result;
1467 }
1468 
dc_commit_state(struct dc * dc,struct dc_state * context)1469 bool dc_commit_state(struct dc *dc, struct dc_state *context)
1470 {
1471 	enum dc_status result = DC_ERROR_UNEXPECTED;
1472 	int i;
1473 
1474 	if (false == context_changed(dc, context))
1475 		return DC_OK;
1476 
1477 	DC_LOG_DC("%s: %d streams\n",
1478 				__func__, context->stream_count);
1479 
1480 	for (i = 0; i < context->stream_count; i++) {
1481 		struct dc_stream_state *stream = context->streams[i];
1482 
1483 		dc_stream_log(dc, stream);
1484 	}
1485 
1486 	result = dc_commit_state_no_check(dc, context);
1487 
1488 	return (result == DC_OK);
1489 }
1490 
1491 #if defined(CONFIG_DRM_AMD_DC_DCN3_0)
dc_acquire_release_mpc_3dlut(struct dc * dc,bool acquire,struct dc_stream_state * stream,struct dc_3dlut ** lut,struct dc_transfer_func ** shaper)1492 bool dc_acquire_release_mpc_3dlut(
1493 		struct dc *dc, bool acquire,
1494 		struct dc_stream_state *stream,
1495 		struct dc_3dlut **lut,
1496 		struct dc_transfer_func **shaper)
1497 {
1498 	int pipe_idx;
1499 	bool ret = false;
1500 	bool found_pipe_idx = false;
1501 	const struct resource_pool *pool = dc->res_pool;
1502 	struct resource_context *res_ctx = &dc->current_state->res_ctx;
1503 	int mpcc_id = 0;
1504 
1505 	if (pool && res_ctx) {
1506 		if (acquire) {
1507 			/*find pipe idx for the given stream*/
1508 			for (pipe_idx = 0; pipe_idx < pool->pipe_count; pipe_idx++) {
1509 				if (res_ctx->pipe_ctx[pipe_idx].stream == stream) {
1510 					found_pipe_idx = true;
1511 					mpcc_id = res_ctx->pipe_ctx[pipe_idx].plane_res.hubp->inst;
1512 					break;
1513 				}
1514 			}
1515 		} else
1516 			found_pipe_idx = true;/*for release pipe_idx is not required*/
1517 
1518 		if (found_pipe_idx) {
1519 			if (acquire && pool->funcs->acquire_post_bldn_3dlut)
1520 				ret = pool->funcs->acquire_post_bldn_3dlut(res_ctx, pool, mpcc_id, lut, shaper);
1521 			else if (acquire == false && pool->funcs->release_post_bldn_3dlut)
1522 				ret = pool->funcs->release_post_bldn_3dlut(res_ctx, pool, lut, shaper);
1523 		}
1524 	}
1525 	return ret;
1526 }
1527 #endif
is_flip_pending_in_pipes(struct dc * dc,struct dc_state * context)1528 static bool is_flip_pending_in_pipes(struct dc *dc, struct dc_state *context)
1529 {
1530 	int i;
1531 	struct pipe_ctx *pipe;
1532 
1533 	for (i = 0; i < MAX_PIPES; i++) {
1534 		pipe = &context->res_ctx.pipe_ctx[i];
1535 
1536 		if (!pipe->plane_state)
1537 			continue;
1538 
1539 		/* Must set to false to start with, due to OR in update function */
1540 		pipe->plane_state->status.is_flip_pending = false;
1541 		dc->hwss.update_pending_status(pipe);
1542 		if (pipe->plane_state->status.is_flip_pending)
1543 			return true;
1544 	}
1545 	return false;
1546 }
1547 
dc_post_update_surfaces_to_stream(struct dc * dc)1548 bool dc_post_update_surfaces_to_stream(struct dc *dc)
1549 {
1550 	int i;
1551 	struct dc_state *context = dc->current_state;
1552 
1553 	if ((!dc->optimized_required) || dc->optimize_seamless_boot_streams > 0)
1554 		return true;
1555 
1556 	post_surface_trace(dc);
1557 
1558 	if (is_flip_pending_in_pipes(dc, context))
1559 		return true;
1560 
1561 	for (i = 0; i < dc->res_pool->pipe_count; i++)
1562 		if (context->res_ctx.pipe_ctx[i].stream == NULL ||
1563 		    context->res_ctx.pipe_ctx[i].plane_state == NULL) {
1564 			context->res_ctx.pipe_ctx[i].pipe_idx = i;
1565 			dc->hwss.disable_plane(dc, &context->res_ctx.pipe_ctx[i]);
1566 		}
1567 
1568 	dc->hwss.optimize_bandwidth(dc, context);
1569 
1570 	dc->optimized_required = false;
1571 	dc->wm_optimized_required = false;
1572 
1573 	return true;
1574 }
1575 
init_state(struct dc * dc,struct dc_state * context)1576 static void init_state(struct dc *dc, struct dc_state *context)
1577 {
1578 	/* Each context must have their own instance of VBA and in order to
1579 	 * initialize and obtain IP and SOC the base DML instance from DC is
1580 	 * initially copied into every context
1581 	 */
1582 #ifdef CONFIG_DRM_AMD_DC_DCN
1583 	memcpy(&context->bw_ctx.dml, &dc->dml, sizeof(struct display_mode_lib));
1584 #endif
1585 }
1586 
dc_create_state(struct dc * dc)1587 struct dc_state *dc_create_state(struct dc *dc)
1588 {
1589 	struct dc_state *context = kvzalloc(sizeof(struct dc_state),
1590 					    GFP_KERNEL);
1591 
1592 	if (!context)
1593 		return NULL;
1594 
1595 	init_state(dc, context);
1596 
1597 	kref_init(&context->refcount);
1598 
1599 	return context;
1600 }
1601 
dc_copy_state(struct dc_state * src_ctx)1602 struct dc_state *dc_copy_state(struct dc_state *src_ctx)
1603 {
1604 	int i, j;
1605 	struct dc_state *new_ctx = kvmalloc(sizeof(struct dc_state), GFP_KERNEL);
1606 
1607 	if (!new_ctx)
1608 		return NULL;
1609 	memcpy(new_ctx, src_ctx, sizeof(struct dc_state));
1610 
1611 	for (i = 0; i < MAX_PIPES; i++) {
1612 			struct pipe_ctx *cur_pipe = &new_ctx->res_ctx.pipe_ctx[i];
1613 
1614 			if (cur_pipe->top_pipe)
1615 				cur_pipe->top_pipe =  &new_ctx->res_ctx.pipe_ctx[cur_pipe->top_pipe->pipe_idx];
1616 
1617 			if (cur_pipe->bottom_pipe)
1618 				cur_pipe->bottom_pipe = &new_ctx->res_ctx.pipe_ctx[cur_pipe->bottom_pipe->pipe_idx];
1619 
1620 			if (cur_pipe->prev_odm_pipe)
1621 				cur_pipe->prev_odm_pipe =  &new_ctx->res_ctx.pipe_ctx[cur_pipe->prev_odm_pipe->pipe_idx];
1622 
1623 			if (cur_pipe->next_odm_pipe)
1624 				cur_pipe->next_odm_pipe = &new_ctx->res_ctx.pipe_ctx[cur_pipe->next_odm_pipe->pipe_idx];
1625 
1626 	}
1627 
1628 	for (i = 0; i < new_ctx->stream_count; i++) {
1629 			dc_stream_retain(new_ctx->streams[i]);
1630 			for (j = 0; j < new_ctx->stream_status[i].plane_count; j++)
1631 				dc_plane_state_retain(
1632 					new_ctx->stream_status[i].plane_states[j]);
1633 	}
1634 
1635 	kref_init(&new_ctx->refcount);
1636 
1637 	return new_ctx;
1638 }
1639 
dc_retain_state(struct dc_state * context)1640 void dc_retain_state(struct dc_state *context)
1641 {
1642 	kref_get(&context->refcount);
1643 }
1644 
dc_state_free(struct kref * kref)1645 static void dc_state_free(struct kref *kref)
1646 {
1647 	struct dc_state *context = container_of(kref, struct dc_state, refcount);
1648 	dc_resource_state_destruct(context);
1649 	kvfree(context);
1650 }
1651 
dc_release_state(struct dc_state * context)1652 void dc_release_state(struct dc_state *context)
1653 {
1654 	kref_put(&context->refcount, dc_state_free);
1655 }
1656 
dc_set_generic_gpio_for_stereo(bool enable,struct gpio_service * gpio_service)1657 bool dc_set_generic_gpio_for_stereo(bool enable,
1658 		struct gpio_service *gpio_service)
1659 {
1660 	enum gpio_result gpio_result = GPIO_RESULT_NON_SPECIFIC_ERROR;
1661 	struct gpio_pin_info pin_info;
1662 	struct gpio *generic;
1663 	struct gpio_generic_mux_config *config = kzalloc(sizeof(struct gpio_generic_mux_config),
1664 			   GFP_KERNEL);
1665 
1666 	if (!config)
1667 		return false;
1668 	pin_info = dal_gpio_get_generic_pin_info(gpio_service, GPIO_ID_GENERIC, 0);
1669 
1670 	if (pin_info.mask == 0xFFFFFFFF || pin_info.offset == 0xFFFFFFFF) {
1671 		kfree(config);
1672 		return false;
1673 	} else {
1674 		generic = dal_gpio_service_create_generic_mux(
1675 			gpio_service,
1676 			pin_info.offset,
1677 			pin_info.mask);
1678 	}
1679 
1680 	if (!generic) {
1681 		kfree(config);
1682 		return false;
1683 	}
1684 
1685 	gpio_result = dal_gpio_open(generic, GPIO_MODE_OUTPUT);
1686 
1687 	config->enable_output_from_mux = enable;
1688 	config->mux_select = GPIO_SIGNAL_SOURCE_PASS_THROUGH_STEREO_SYNC;
1689 
1690 	if (gpio_result == GPIO_RESULT_OK)
1691 		gpio_result = dal_mux_setup_config(generic, config);
1692 
1693 	if (gpio_result == GPIO_RESULT_OK) {
1694 		dal_gpio_close(generic);
1695 		dal_gpio_destroy_generic_mux(&generic);
1696 		kfree(config);
1697 		return true;
1698 	} else {
1699 		dal_gpio_close(generic);
1700 		dal_gpio_destroy_generic_mux(&generic);
1701 		kfree(config);
1702 		return false;
1703 	}
1704 }
1705 
is_surface_in_context(const struct dc_state * context,const struct dc_plane_state * plane_state)1706 static bool is_surface_in_context(
1707 		const struct dc_state *context,
1708 		const struct dc_plane_state *plane_state)
1709 {
1710 	int j;
1711 
1712 	for (j = 0; j < MAX_PIPES; j++) {
1713 		const struct pipe_ctx *pipe_ctx = &context->res_ctx.pipe_ctx[j];
1714 
1715 		if (plane_state == pipe_ctx->plane_state) {
1716 			return true;
1717 		}
1718 	}
1719 
1720 	return false;
1721 }
1722 
get_plane_info_update_type(const struct dc_surface_update * u)1723 static enum surface_update_type get_plane_info_update_type(const struct dc_surface_update *u)
1724 {
1725 	union surface_update_flags *update_flags = &u->surface->update_flags;
1726 	enum surface_update_type update_type = UPDATE_TYPE_FAST;
1727 
1728 	if (!u->plane_info)
1729 		return UPDATE_TYPE_FAST;
1730 
1731 	if (u->plane_info->color_space != u->surface->color_space) {
1732 		update_flags->bits.color_space_change = 1;
1733 		elevate_update_type(&update_type, UPDATE_TYPE_MED);
1734 	}
1735 
1736 	if (u->plane_info->horizontal_mirror != u->surface->horizontal_mirror) {
1737 		update_flags->bits.horizontal_mirror_change = 1;
1738 		elevate_update_type(&update_type, UPDATE_TYPE_MED);
1739 	}
1740 
1741 	if (u->plane_info->rotation != u->surface->rotation) {
1742 		update_flags->bits.rotation_change = 1;
1743 		elevate_update_type(&update_type, UPDATE_TYPE_FULL);
1744 	}
1745 
1746 	if (u->plane_info->format != u->surface->format) {
1747 		update_flags->bits.pixel_format_change = 1;
1748 		elevate_update_type(&update_type, UPDATE_TYPE_FULL);
1749 	}
1750 
1751 	if (u->plane_info->stereo_format != u->surface->stereo_format) {
1752 		update_flags->bits.stereo_format_change = 1;
1753 		elevate_update_type(&update_type, UPDATE_TYPE_FULL);
1754 	}
1755 
1756 	if (u->plane_info->per_pixel_alpha != u->surface->per_pixel_alpha) {
1757 		update_flags->bits.per_pixel_alpha_change = 1;
1758 		elevate_update_type(&update_type, UPDATE_TYPE_MED);
1759 	}
1760 
1761 	if (u->plane_info->global_alpha_value != u->surface->global_alpha_value) {
1762 		update_flags->bits.global_alpha_change = 1;
1763 		elevate_update_type(&update_type, UPDATE_TYPE_MED);
1764 	}
1765 
1766 	if (u->plane_info->dcc.enable != u->surface->dcc.enable
1767 			|| u->plane_info->dcc.independent_64b_blks != u->surface->dcc.independent_64b_blks
1768 			|| u->plane_info->dcc.meta_pitch != u->surface->dcc.meta_pitch) {
1769 		update_flags->bits.dcc_change = 1;
1770 		elevate_update_type(&update_type, UPDATE_TYPE_MED);
1771 	}
1772 
1773 	if (resource_pixel_format_to_bpp(u->plane_info->format) !=
1774 			resource_pixel_format_to_bpp(u->surface->format)) {
1775 		/* different bytes per element will require full bandwidth
1776 		 * and DML calculation
1777 		 */
1778 		update_flags->bits.bpp_change = 1;
1779 		elevate_update_type(&update_type, UPDATE_TYPE_FULL);
1780 	}
1781 
1782 	if (u->plane_info->plane_size.surface_pitch != u->surface->plane_size.surface_pitch
1783 			|| u->plane_info->plane_size.chroma_pitch != u->surface->plane_size.chroma_pitch) {
1784 		update_flags->bits.plane_size_change = 1;
1785 		elevate_update_type(&update_type, UPDATE_TYPE_MED);
1786 	}
1787 
1788 
1789 	if (memcmp(&u->plane_info->tiling_info, &u->surface->tiling_info,
1790 			sizeof(union dc_tiling_info)) != 0) {
1791 		update_flags->bits.swizzle_change = 1;
1792 		elevate_update_type(&update_type, UPDATE_TYPE_MED);
1793 
1794 		/* todo: below are HW dependent, we should add a hook to
1795 		 * DCE/N resource and validated there.
1796 		 */
1797 		if (u->plane_info->tiling_info.gfx9.swizzle != DC_SW_LINEAR) {
1798 			/* swizzled mode requires RQ to be setup properly,
1799 			 * thus need to run DML to calculate RQ settings
1800 			 */
1801 			update_flags->bits.bandwidth_change = 1;
1802 			elevate_update_type(&update_type, UPDATE_TYPE_FULL);
1803 		}
1804 	}
1805 
1806 	/* This should be UPDATE_TYPE_FAST if nothing has changed. */
1807 	return update_type;
1808 }
1809 
get_scaling_info_update_type(const struct dc_surface_update * u)1810 static enum surface_update_type get_scaling_info_update_type(
1811 		const struct dc_surface_update *u)
1812 {
1813 	union surface_update_flags *update_flags = &u->surface->update_flags;
1814 
1815 	if (!u->scaling_info)
1816 		return UPDATE_TYPE_FAST;
1817 
1818 	if (u->scaling_info->clip_rect.width != u->surface->clip_rect.width
1819 			|| u->scaling_info->clip_rect.height != u->surface->clip_rect.height
1820 			|| u->scaling_info->dst_rect.width != u->surface->dst_rect.width
1821 			|| u->scaling_info->dst_rect.height != u->surface->dst_rect.height
1822 			|| u->scaling_info->scaling_quality.integer_scaling !=
1823 				u->surface->scaling_quality.integer_scaling
1824 			) {
1825 		update_flags->bits.scaling_change = 1;
1826 
1827 		if ((u->scaling_info->dst_rect.width < u->surface->dst_rect.width
1828 			|| u->scaling_info->dst_rect.height < u->surface->dst_rect.height)
1829 				&& (u->scaling_info->dst_rect.width < u->surface->src_rect.width
1830 					|| u->scaling_info->dst_rect.height < u->surface->src_rect.height))
1831 			/* Making dst rect smaller requires a bandwidth change */
1832 			update_flags->bits.bandwidth_change = 1;
1833 	}
1834 
1835 	if (u->scaling_info->src_rect.width != u->surface->src_rect.width
1836 		|| u->scaling_info->src_rect.height != u->surface->src_rect.height) {
1837 
1838 		update_flags->bits.scaling_change = 1;
1839 		if (u->scaling_info->src_rect.width > u->surface->src_rect.width
1840 				|| u->scaling_info->src_rect.height > u->surface->src_rect.height)
1841 			/* Making src rect bigger requires a bandwidth change */
1842 			update_flags->bits.clock_change = 1;
1843 	}
1844 
1845 	if (u->scaling_info->src_rect.x != u->surface->src_rect.x
1846 			|| u->scaling_info->src_rect.y != u->surface->src_rect.y
1847 			|| u->scaling_info->clip_rect.x != u->surface->clip_rect.x
1848 			|| u->scaling_info->clip_rect.y != u->surface->clip_rect.y
1849 			|| u->scaling_info->dst_rect.x != u->surface->dst_rect.x
1850 			|| u->scaling_info->dst_rect.y != u->surface->dst_rect.y)
1851 		update_flags->bits.position_change = 1;
1852 
1853 	if (update_flags->bits.clock_change
1854 			|| update_flags->bits.bandwidth_change
1855 			|| update_flags->bits.scaling_change)
1856 		return UPDATE_TYPE_FULL;
1857 
1858 	if (update_flags->bits.position_change)
1859 		return UPDATE_TYPE_MED;
1860 
1861 	return UPDATE_TYPE_FAST;
1862 }
1863 
det_surface_update(const struct dc * dc,const struct dc_surface_update * u)1864 static enum surface_update_type det_surface_update(const struct dc *dc,
1865 		const struct dc_surface_update *u)
1866 {
1867 	const struct dc_state *context = dc->current_state;
1868 	enum surface_update_type type;
1869 	enum surface_update_type overall_type = UPDATE_TYPE_FAST;
1870 	union surface_update_flags *update_flags = &u->surface->update_flags;
1871 
1872 	if (!is_surface_in_context(context, u->surface) || u->surface->force_full_update) {
1873 		update_flags->raw = 0xFFFFFFFF;
1874 		return UPDATE_TYPE_FULL;
1875 	}
1876 
1877 	update_flags->raw = 0; // Reset all flags
1878 
1879 	type = get_plane_info_update_type(u);
1880 	elevate_update_type(&overall_type, type);
1881 
1882 	type = get_scaling_info_update_type(u);
1883 	elevate_update_type(&overall_type, type);
1884 
1885 	if (u->flip_addr)
1886 		update_flags->bits.addr_update = 1;
1887 
1888 	if (u->in_transfer_func)
1889 		update_flags->bits.in_transfer_func_change = 1;
1890 
1891 	if (u->input_csc_color_matrix)
1892 		update_flags->bits.input_csc_change = 1;
1893 
1894 	if (u->coeff_reduction_factor)
1895 		update_flags->bits.coeff_reduction_change = 1;
1896 
1897 	if (u->gamut_remap_matrix)
1898 		update_flags->bits.gamut_remap_change = 1;
1899 
1900 	if (u->gamma) {
1901 		enum surface_pixel_format format = SURFACE_PIXEL_FORMAT_GRPH_BEGIN;
1902 
1903 		if (u->plane_info)
1904 			format = u->plane_info->format;
1905 		else if (u->surface)
1906 			format = u->surface->format;
1907 
1908 		if (dce_use_lut(format))
1909 			update_flags->bits.gamma_change = 1;
1910 	}
1911 
1912 	if (u->hdr_mult.value)
1913 		if (u->hdr_mult.value != u->surface->hdr_mult.value) {
1914 			update_flags->bits.hdr_mult = 1;
1915 			elevate_update_type(&overall_type, UPDATE_TYPE_MED);
1916 		}
1917 
1918 	if (update_flags->bits.in_transfer_func_change) {
1919 		type = UPDATE_TYPE_MED;
1920 		elevate_update_type(&overall_type, type);
1921 	}
1922 
1923 	if (update_flags->bits.input_csc_change
1924 			|| update_flags->bits.coeff_reduction_change
1925 			|| update_flags->bits.gamma_change
1926 			|| update_flags->bits.gamut_remap_change) {
1927 		type = UPDATE_TYPE_FULL;
1928 		elevate_update_type(&overall_type, type);
1929 	}
1930 
1931 	return overall_type;
1932 }
1933 
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)1934 static enum surface_update_type check_update_surfaces_for_stream(
1935 		struct dc *dc,
1936 		struct dc_surface_update *updates,
1937 		int surface_count,
1938 		struct dc_stream_update *stream_update,
1939 		const struct dc_stream_status *stream_status)
1940 {
1941 	int i;
1942 	enum surface_update_type overall_type = UPDATE_TYPE_FAST;
1943 
1944 #if defined(CONFIG_DRM_AMD_DC_DCN3_0)
1945 	if (dc->idle_optimizations_allowed)
1946 		overall_type = UPDATE_TYPE_FULL;
1947 
1948 #endif
1949 	if (stream_status == NULL || stream_status->plane_count != surface_count)
1950 		overall_type = UPDATE_TYPE_FULL;
1951 
1952 	/* some stream updates require passive update */
1953 	if (stream_update) {
1954 		union stream_update_flags *su_flags = &stream_update->stream->update_flags;
1955 
1956 		if ((stream_update->src.height != 0 && stream_update->src.width != 0) ||
1957 			(stream_update->dst.height != 0 && stream_update->dst.width != 0) ||
1958 			stream_update->integer_scaling_update)
1959 			su_flags->bits.scaling = 1;
1960 
1961 		if (stream_update->out_transfer_func)
1962 			su_flags->bits.out_tf = 1;
1963 
1964 		if (stream_update->abm_level)
1965 			su_flags->bits.abm_level = 1;
1966 
1967 		if (stream_update->dpms_off)
1968 			su_flags->bits.dpms_off = 1;
1969 
1970 		if (stream_update->gamut_remap)
1971 			su_flags->bits.gamut_remap = 1;
1972 
1973 		if (stream_update->wb_update)
1974 			su_flags->bits.wb_update = 1;
1975 
1976 		if (stream_update->dsc_config)
1977 			su_flags->bits.dsc_changed = 1;
1978 
1979 		if (su_flags->raw != 0)
1980 			overall_type = UPDATE_TYPE_FULL;
1981 
1982 		if (stream_update->output_csc_transform || stream_update->output_color_space)
1983 			su_flags->bits.out_csc = 1;
1984 	}
1985 
1986 	for (i = 0 ; i < surface_count; i++) {
1987 		enum surface_update_type type =
1988 				det_surface_update(dc, &updates[i]);
1989 
1990 		elevate_update_type(&overall_type, type);
1991 	}
1992 
1993 	return overall_type;
1994 }
1995 
1996 /**
1997  * dc_check_update_surfaces_for_stream() - Determine update type (fast, med, or full)
1998  *
1999  * See :c:type:`enum surface_update_type <surface_update_type>` for explanation of update types
2000  */
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)2001 enum surface_update_type dc_check_update_surfaces_for_stream(
2002 		struct dc *dc,
2003 		struct dc_surface_update *updates,
2004 		int surface_count,
2005 		struct dc_stream_update *stream_update,
2006 		const struct dc_stream_status *stream_status)
2007 {
2008 	int i;
2009 	enum surface_update_type type;
2010 
2011 	if (stream_update)
2012 		stream_update->stream->update_flags.raw = 0;
2013 	for (i = 0; i < surface_count; i++)
2014 		updates[i].surface->update_flags.raw = 0;
2015 
2016 	type = check_update_surfaces_for_stream(dc, updates, surface_count, stream_update, stream_status);
2017 	if (type == UPDATE_TYPE_FULL) {
2018 		if (stream_update) {
2019 			uint32_t dsc_changed = stream_update->stream->update_flags.bits.dsc_changed;
2020 			stream_update->stream->update_flags.raw = 0xFFFFFFFF;
2021 			stream_update->stream->update_flags.bits.dsc_changed = dsc_changed;
2022 		}
2023 		for (i = 0; i < surface_count; i++)
2024 			updates[i].surface->update_flags.raw = 0xFFFFFFFF;
2025 	}
2026 
2027 	if (type == UPDATE_TYPE_FAST) {
2028 		// If there's an available clock comparator, we use that.
2029 		if (dc->clk_mgr->funcs->are_clock_states_equal) {
2030 			if (!dc->clk_mgr->funcs->are_clock_states_equal(&dc->clk_mgr->clks, &dc->current_state->bw_ctx.bw.dcn.clk))
2031 				dc->optimized_required = true;
2032 		// Else we fallback to mem compare.
2033 		} 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) {
2034 			dc->optimized_required = true;
2035 		}
2036 
2037 		dc->optimized_required |= dc->wm_optimized_required;
2038 	}
2039 
2040 	return type;
2041 }
2042 
stream_get_status(struct dc_state * ctx,struct dc_stream_state * stream)2043 static struct dc_stream_status *stream_get_status(
2044 	struct dc_state *ctx,
2045 	struct dc_stream_state *stream)
2046 {
2047 	uint8_t i;
2048 
2049 	for (i = 0; i < ctx->stream_count; i++) {
2050 		if (stream == ctx->streams[i]) {
2051 			return &ctx->stream_status[i];
2052 		}
2053 	}
2054 
2055 	return NULL;
2056 }
2057 
2058 static const enum surface_update_type update_surface_trace_level = UPDATE_TYPE_FULL;
2059 
copy_surface_update_to_plane(struct dc_plane_state * surface,struct dc_surface_update * srf_update)2060 static void copy_surface_update_to_plane(
2061 		struct dc_plane_state *surface,
2062 		struct dc_surface_update *srf_update)
2063 {
2064 	if (srf_update->flip_addr) {
2065 		surface->address = srf_update->flip_addr->address;
2066 		surface->flip_immediate =
2067 			srf_update->flip_addr->flip_immediate;
2068 		surface->time.time_elapsed_in_us[surface->time.index] =
2069 			srf_update->flip_addr->flip_timestamp_in_us -
2070 				surface->time.prev_update_time_in_us;
2071 		surface->time.prev_update_time_in_us =
2072 			srf_update->flip_addr->flip_timestamp_in_us;
2073 		surface->time.index++;
2074 		if (surface->time.index >= DC_PLANE_UPDATE_TIMES_MAX)
2075 			surface->time.index = 0;
2076 
2077 		surface->triplebuffer_flips = srf_update->flip_addr->triplebuffer_flips;
2078 	}
2079 
2080 	if (srf_update->scaling_info) {
2081 		surface->scaling_quality =
2082 				srf_update->scaling_info->scaling_quality;
2083 		surface->dst_rect =
2084 				srf_update->scaling_info->dst_rect;
2085 		surface->src_rect =
2086 				srf_update->scaling_info->src_rect;
2087 		surface->clip_rect =
2088 				srf_update->scaling_info->clip_rect;
2089 	}
2090 
2091 	if (srf_update->plane_info) {
2092 		surface->color_space =
2093 				srf_update->plane_info->color_space;
2094 		surface->format =
2095 				srf_update->plane_info->format;
2096 		surface->plane_size =
2097 				srf_update->plane_info->plane_size;
2098 		surface->rotation =
2099 				srf_update->plane_info->rotation;
2100 		surface->horizontal_mirror =
2101 				srf_update->plane_info->horizontal_mirror;
2102 		surface->stereo_format =
2103 				srf_update->plane_info->stereo_format;
2104 		surface->tiling_info =
2105 				srf_update->plane_info->tiling_info;
2106 		surface->visible =
2107 				srf_update->plane_info->visible;
2108 		surface->per_pixel_alpha =
2109 				srf_update->plane_info->per_pixel_alpha;
2110 		surface->global_alpha =
2111 				srf_update->plane_info->global_alpha;
2112 		surface->global_alpha_value =
2113 				srf_update->plane_info->global_alpha_value;
2114 		surface->dcc =
2115 				srf_update->plane_info->dcc;
2116 		surface->layer_index =
2117 				srf_update->plane_info->layer_index;
2118 	}
2119 
2120 	if (srf_update->gamma &&
2121 			(surface->gamma_correction !=
2122 					srf_update->gamma)) {
2123 		memcpy(&surface->gamma_correction->entries,
2124 			&srf_update->gamma->entries,
2125 			sizeof(struct dc_gamma_entries));
2126 		surface->gamma_correction->is_identity =
2127 			srf_update->gamma->is_identity;
2128 		surface->gamma_correction->num_entries =
2129 			srf_update->gamma->num_entries;
2130 		surface->gamma_correction->type =
2131 			srf_update->gamma->type;
2132 	}
2133 
2134 	if (srf_update->in_transfer_func &&
2135 			(surface->in_transfer_func !=
2136 				srf_update->in_transfer_func)) {
2137 		surface->in_transfer_func->sdr_ref_white_level =
2138 			srf_update->in_transfer_func->sdr_ref_white_level;
2139 		surface->in_transfer_func->tf =
2140 			srf_update->in_transfer_func->tf;
2141 		surface->in_transfer_func->type =
2142 			srf_update->in_transfer_func->type;
2143 		memcpy(&surface->in_transfer_func->tf_pts,
2144 			&srf_update->in_transfer_func->tf_pts,
2145 			sizeof(struct dc_transfer_func_distributed_points));
2146 	}
2147 
2148 	if (srf_update->func_shaper &&
2149 			(surface->in_shaper_func !=
2150 			srf_update->func_shaper))
2151 		memcpy(surface->in_shaper_func, srf_update->func_shaper,
2152 		sizeof(*surface->in_shaper_func));
2153 
2154 	if (srf_update->lut3d_func &&
2155 			(surface->lut3d_func !=
2156 			srf_update->lut3d_func))
2157 		memcpy(surface->lut3d_func, srf_update->lut3d_func,
2158 		sizeof(*surface->lut3d_func));
2159 
2160 	if (srf_update->hdr_mult.value)
2161 		surface->hdr_mult =
2162 				srf_update->hdr_mult;
2163 
2164 	if (srf_update->blend_tf &&
2165 			(surface->blend_tf !=
2166 			srf_update->blend_tf))
2167 		memcpy(surface->blend_tf, srf_update->blend_tf,
2168 		sizeof(*surface->blend_tf));
2169 
2170 	if (srf_update->input_csc_color_matrix)
2171 		surface->input_csc_color_matrix =
2172 			*srf_update->input_csc_color_matrix;
2173 
2174 	if (srf_update->coeff_reduction_factor)
2175 		surface->coeff_reduction_factor =
2176 			*srf_update->coeff_reduction_factor;
2177 
2178 	if (srf_update->gamut_remap_matrix)
2179 		surface->gamut_remap_matrix =
2180 			*srf_update->gamut_remap_matrix;
2181 }
2182 
copy_stream_update_to_stream(struct dc * dc,struct dc_state * context,struct dc_stream_state * stream,struct dc_stream_update * update)2183 static void copy_stream_update_to_stream(struct dc *dc,
2184 					 struct dc_state *context,
2185 					 struct dc_stream_state *stream,
2186 					 struct dc_stream_update *update)
2187 {
2188 	struct dc_context *dc_ctx = dc->ctx;
2189 
2190 	if (update == NULL || stream == NULL)
2191 		return;
2192 
2193 	if (update->src.height && update->src.width)
2194 		stream->src = update->src;
2195 
2196 	if (update->dst.height && update->dst.width)
2197 		stream->dst = update->dst;
2198 
2199 	if (update->out_transfer_func &&
2200 	    stream->out_transfer_func != update->out_transfer_func) {
2201 		stream->out_transfer_func->sdr_ref_white_level =
2202 			update->out_transfer_func->sdr_ref_white_level;
2203 		stream->out_transfer_func->tf = update->out_transfer_func->tf;
2204 		stream->out_transfer_func->type =
2205 			update->out_transfer_func->type;
2206 		memcpy(&stream->out_transfer_func->tf_pts,
2207 		       &update->out_transfer_func->tf_pts,
2208 		       sizeof(struct dc_transfer_func_distributed_points));
2209 	}
2210 
2211 	if (update->hdr_static_metadata)
2212 		stream->hdr_static_metadata = *update->hdr_static_metadata;
2213 
2214 	if (update->abm_level)
2215 		stream->abm_level = *update->abm_level;
2216 
2217 	if (update->periodic_interrupt)
2218 		stream->periodic_interrupt = *update->periodic_interrupt;
2219 
2220 	if (update->gamut_remap)
2221 		stream->gamut_remap_matrix = *update->gamut_remap;
2222 
2223 	/* Note: this being updated after mode set is currently not a use case
2224 	 * however if it arises OCSC would need to be reprogrammed at the
2225 	 * minimum
2226 	 */
2227 	if (update->output_color_space)
2228 		stream->output_color_space = *update->output_color_space;
2229 
2230 	if (update->output_csc_transform)
2231 		stream->csc_color_matrix = *update->output_csc_transform;
2232 
2233 	if (update->vrr_infopacket)
2234 		stream->vrr_infopacket = *update->vrr_infopacket;
2235 
2236 	if (update->dpms_off)
2237 		stream->dpms_off = *update->dpms_off;
2238 
2239 	if (update->vsc_infopacket)
2240 		stream->vsc_infopacket = *update->vsc_infopacket;
2241 
2242 	if (update->vsp_infopacket)
2243 		stream->vsp_infopacket = *update->vsp_infopacket;
2244 
2245 	if (update->dither_option)
2246 		stream->dither_option = *update->dither_option;
2247 	/* update current stream with writeback info */
2248 	if (update->wb_update) {
2249 		int i;
2250 
2251 		stream->num_wb_info = update->wb_update->num_wb_info;
2252 		ASSERT(stream->num_wb_info <= MAX_DWB_PIPES);
2253 		for (i = 0; i < stream->num_wb_info; i++)
2254 			stream->writeback_info[i] =
2255 				update->wb_update->writeback_info[i];
2256 	}
2257 	if (update->dsc_config) {
2258 		struct dc_dsc_config old_dsc_cfg = stream->timing.dsc_cfg;
2259 		uint32_t old_dsc_enabled = stream->timing.flags.DSC;
2260 		uint32_t enable_dsc = (update->dsc_config->num_slices_h != 0 &&
2261 				       update->dsc_config->num_slices_v != 0);
2262 
2263 		/* Use temporarry context for validating new DSC config */
2264 		struct dc_state *dsc_validate_context = dc_create_state(dc);
2265 
2266 		if (dsc_validate_context) {
2267 			dc_resource_state_copy_construct(dc->current_state, dsc_validate_context);
2268 
2269 			stream->timing.dsc_cfg = *update->dsc_config;
2270 			stream->timing.flags.DSC = enable_dsc;
2271 			if (!dc->res_pool->funcs->validate_bandwidth(dc, dsc_validate_context, true)) {
2272 				stream->timing.dsc_cfg = old_dsc_cfg;
2273 				stream->timing.flags.DSC = old_dsc_enabled;
2274 				update->dsc_config = NULL;
2275 			}
2276 
2277 			dc_release_state(dsc_validate_context);
2278 		} else {
2279 			DC_ERROR("Failed to allocate new validate context for DSC change\n");
2280 			update->dsc_config = NULL;
2281 		}
2282 	}
2283 }
2284 
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)2285 static void commit_planes_do_stream_update(struct dc *dc,
2286 		struct dc_stream_state *stream,
2287 		struct dc_stream_update *stream_update,
2288 		enum surface_update_type update_type,
2289 		struct dc_state *context)
2290 {
2291 	int j;
2292 	bool should_program_abm;
2293 
2294 	// Stream updates
2295 	for (j = 0; j < dc->res_pool->pipe_count; j++) {
2296 		struct pipe_ctx *pipe_ctx = &context->res_ctx.pipe_ctx[j];
2297 
2298 		if (!pipe_ctx->top_pipe &&  !pipe_ctx->prev_odm_pipe && pipe_ctx->stream == stream) {
2299 
2300 			if (stream_update->periodic_interrupt && dc->hwss.setup_periodic_interrupt)
2301 				dc->hwss.setup_periodic_interrupt(dc, pipe_ctx);
2302 
2303 			if ((stream_update->hdr_static_metadata && !stream->use_dynamic_meta) ||
2304 					stream_update->vrr_infopacket ||
2305 					stream_update->vsc_infopacket ||
2306 					stream_update->vsp_infopacket) {
2307 				resource_build_info_frame(pipe_ctx);
2308 				dc->hwss.update_info_frame(pipe_ctx);
2309 			}
2310 
2311 			if (stream_update->hdr_static_metadata &&
2312 					stream->use_dynamic_meta &&
2313 					dc->hwss.set_dmdata_attributes &&
2314 					pipe_ctx->stream->dmdata_address.quad_part != 0)
2315 				dc->hwss.set_dmdata_attributes(pipe_ctx);
2316 
2317 			if (stream_update->gamut_remap)
2318 				dc_stream_set_gamut_remap(dc, stream);
2319 
2320 			if (stream_update->output_csc_transform)
2321 				dc_stream_program_csc_matrix(dc, stream);
2322 
2323 			if (stream_update->dither_option) {
2324 				struct pipe_ctx *odm_pipe = pipe_ctx->next_odm_pipe;
2325 				resource_build_bit_depth_reduction_params(pipe_ctx->stream,
2326 									&pipe_ctx->stream->bit_depth_params);
2327 				pipe_ctx->stream_res.opp->funcs->opp_program_fmt(pipe_ctx->stream_res.opp,
2328 						&stream->bit_depth_params,
2329 						&stream->clamping);
2330 				while (odm_pipe) {
2331 					odm_pipe->stream_res.opp->funcs->opp_program_fmt(odm_pipe->stream_res.opp,
2332 							&stream->bit_depth_params,
2333 							&stream->clamping);
2334 					odm_pipe = odm_pipe->next_odm_pipe;
2335 				}
2336 			}
2337 
2338 			/* Full fe update*/
2339 			if (update_type == UPDATE_TYPE_FAST)
2340 				continue;
2341 
2342 			if (stream_update->dsc_config)
2343 				dp_update_dsc_config(pipe_ctx);
2344 
2345 			if (stream_update->dpms_off) {
2346 				if (*stream_update->dpms_off) {
2347 					core_link_disable_stream(pipe_ctx);
2348 					/* for dpms, keep acquired resources*/
2349 					if (pipe_ctx->stream_res.audio && !dc->debug.az_endpoint_mute_only)
2350 						pipe_ctx->stream_res.audio->funcs->az_disable(pipe_ctx->stream_res.audio);
2351 
2352 					dc->optimized_required = true;
2353 
2354 				} else {
2355 					if (dc->optimize_seamless_boot_streams == 0)
2356 						dc->hwss.prepare_bandwidth(dc, dc->current_state);
2357 
2358 					core_link_enable_stream(dc->current_state, pipe_ctx);
2359 				}
2360 			}
2361 
2362 			if (stream_update->abm_level && pipe_ctx->stream_res.abm) {
2363 				should_program_abm = true;
2364 
2365 				// if otg funcs defined check if blanked before programming
2366 				if (pipe_ctx->stream_res.tg->funcs->is_blanked)
2367 					if (pipe_ctx->stream_res.tg->funcs->is_blanked(pipe_ctx->stream_res.tg))
2368 						should_program_abm = false;
2369 
2370 				if (should_program_abm) {
2371 					if (*stream_update->abm_level == ABM_LEVEL_IMMEDIATE_DISABLE) {
2372 						dc->hwss.set_abm_immediate_disable(pipe_ctx);
2373 					} else {
2374 						pipe_ctx->stream_res.abm->funcs->set_abm_level(
2375 							pipe_ctx->stream_res.abm, stream->abm_level);
2376 					}
2377 				}
2378 			}
2379 		}
2380 	}
2381 }
2382 
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)2383 static void commit_planes_for_stream(struct dc *dc,
2384 		struct dc_surface_update *srf_updates,
2385 		int surface_count,
2386 		struct dc_stream_state *stream,
2387 		struct dc_stream_update *stream_update,
2388 		enum surface_update_type update_type,
2389 		struct dc_state *context)
2390 {
2391 	bool mpcc_disconnected = false;
2392 	int i, j;
2393 	struct pipe_ctx *top_pipe_to_program = NULL;
2394 
2395 	if (dc->optimize_seamless_boot_streams > 0 && surface_count > 0) {
2396 		/* Optimize seamless boot flag keeps clocks and watermarks high until
2397 		 * first flip. After first flip, optimization is required to lower
2398 		 * bandwidth. Important to note that it is expected UEFI will
2399 		 * only light up a single display on POST, therefore we only expect
2400 		 * one stream with seamless boot flag set.
2401 		 */
2402 		if (stream->apply_seamless_boot_optimization) {
2403 			stream->apply_seamless_boot_optimization = false;
2404 			dc->optimize_seamless_boot_streams--;
2405 
2406 			if (dc->optimize_seamless_boot_streams == 0)
2407 				dc->optimized_required = true;
2408 		}
2409 	}
2410 
2411 	if (update_type == UPDATE_TYPE_FULL) {
2412 #if defined(CONFIG_DRM_AMD_DC_DCN3_0)
2413 		dc_allow_idle_optimizations(dc, false);
2414 
2415 #endif
2416 		if (dc->optimize_seamless_boot_streams == 0)
2417 			dc->hwss.prepare_bandwidth(dc, context);
2418 
2419 		context_clock_trace(dc, context);
2420 	}
2421 
2422 	if (update_type != UPDATE_TYPE_FAST && dc->hwss.interdependent_update_lock &&
2423 		dc->hwss.disconnect_pipes && dc->hwss.wait_for_pending_cleared){
2424 		dc->hwss.interdependent_update_lock(dc, context, true);
2425 		mpcc_disconnected = dc->hwss.disconnect_pipes(dc, context);
2426 		dc->hwss.interdependent_update_lock(dc, context, false);
2427 		if (mpcc_disconnected)
2428 			dc->hwss.wait_for_pending_cleared(dc, context);
2429 	}
2430 
2431 	for (j = 0; j < dc->res_pool->pipe_count; j++) {
2432 		struct pipe_ctx *pipe_ctx = &context->res_ctx.pipe_ctx[j];
2433 
2434 		if (!pipe_ctx->top_pipe &&
2435 			!pipe_ctx->prev_odm_pipe &&
2436 			pipe_ctx->stream &&
2437 			pipe_ctx->stream == stream) {
2438 			top_pipe_to_program = pipe_ctx;
2439 		}
2440 	}
2441 
2442 	if ((update_type != UPDATE_TYPE_FAST) && stream->update_flags.bits.dsc_changed)
2443 		if (top_pipe_to_program &&
2444 			top_pipe_to_program->stream_res.tg->funcs->lock_doublebuffer_enable) {
2445 			if (should_use_dmub_lock(stream->link)) {
2446 				union dmub_hw_lock_flags hw_locks = { 0 };
2447 				struct dmub_hw_lock_inst_flags inst_flags = { 0 };
2448 
2449 				hw_locks.bits.lock_dig = 1;
2450 				inst_flags.dig_inst = top_pipe_to_program->stream_res.tg->inst;
2451 
2452 				dmub_hw_lock_mgr_cmd(dc->ctx->dmub_srv,
2453 							true,
2454 							&hw_locks,
2455 							&inst_flags);
2456 			} else
2457 				top_pipe_to_program->stream_res.tg->funcs->lock_doublebuffer_enable(
2458 						top_pipe_to_program->stream_res.tg);
2459 		}
2460 
2461 	if ((update_type != UPDATE_TYPE_FAST) && dc->hwss.interdependent_update_lock)
2462 		dc->hwss.interdependent_update_lock(dc, context, true);
2463 	else
2464 		/* Lock the top pipe while updating plane addrs, since freesync requires
2465 		 *  plane addr update event triggers to be synchronized.
2466 		 *  top_pipe_to_program is expected to never be NULL
2467 		 */
2468 		dc->hwss.pipe_control_lock(dc, top_pipe_to_program, true);
2469 
2470 
2471 	// Stream updates
2472 	if (stream_update)
2473 		commit_planes_do_stream_update(dc, stream, stream_update, update_type, context);
2474 
2475 	if (surface_count == 0) {
2476 		/*
2477 		 * In case of turning off screen, no need to program front end a second time.
2478 		 * just return after program blank.
2479 		 */
2480 		if (dc->hwss.apply_ctx_for_surface)
2481 			dc->hwss.apply_ctx_for_surface(dc, stream, 0, context);
2482 		if (dc->hwss.program_front_end_for_ctx)
2483 			dc->hwss.program_front_end_for_ctx(dc, context);
2484 
2485 		if ((update_type != UPDATE_TYPE_FAST) && dc->hwss.interdependent_update_lock)
2486 			dc->hwss.interdependent_update_lock(dc, context, false);
2487 		else
2488 			dc->hwss.pipe_control_lock(dc, top_pipe_to_program, false);
2489 
2490 		dc->hwss.post_unlock_program_front_end(dc, context);
2491 		return;
2492 	}
2493 
2494 	if (!IS_DIAG_DC(dc->ctx->dce_environment)) {
2495 		for (i = 0; i < surface_count; i++) {
2496 			struct dc_plane_state *plane_state = srf_updates[i].surface;
2497 			/*set logical flag for lock/unlock use*/
2498 			for (j = 0; j < dc->res_pool->pipe_count; j++) {
2499 				struct pipe_ctx *pipe_ctx = &context->res_ctx.pipe_ctx[j];
2500 				if (!pipe_ctx->plane_state)
2501 					continue;
2502 				if (pipe_ctx->plane_state != plane_state)
2503 					continue;
2504 				plane_state->triplebuffer_flips = false;
2505 				if (update_type == UPDATE_TYPE_FAST &&
2506 					dc->hwss.program_triplebuffer != NULL &&
2507 					!plane_state->flip_immediate && dc->debug.enable_tri_buf) {
2508 						/*triple buffer for VUpdate  only*/
2509 						plane_state->triplebuffer_flips = true;
2510 				}
2511 			}
2512 			if (update_type == UPDATE_TYPE_FULL) {
2513 				/* force vsync flip when reconfiguring pipes to prevent underflow */
2514 				plane_state->flip_immediate = false;
2515 			}
2516 		}
2517 	}
2518 
2519 	// Update Type FULL, Surface updates
2520 	for (j = 0; j < dc->res_pool->pipe_count; j++) {
2521 		struct pipe_ctx *pipe_ctx = &context->res_ctx.pipe_ctx[j];
2522 
2523 		if (!pipe_ctx->top_pipe &&
2524 			!pipe_ctx->prev_odm_pipe &&
2525 			pipe_ctx->stream &&
2526 			pipe_ctx->stream == stream) {
2527 			struct dc_stream_status *stream_status = NULL;
2528 
2529 			if (!pipe_ctx->plane_state)
2530 				continue;
2531 
2532 			/* Full fe update*/
2533 			if (update_type == UPDATE_TYPE_FAST)
2534 				continue;
2535 
2536 			ASSERT(!pipe_ctx->plane_state->triplebuffer_flips);
2537 
2538 			if (dc->hwss.program_triplebuffer != NULL && dc->debug.enable_tri_buf) {
2539 				/*turn off triple buffer for full update*/
2540 				dc->hwss.program_triplebuffer(
2541 					dc, pipe_ctx, pipe_ctx->plane_state->triplebuffer_flips);
2542 			}
2543 			stream_status =
2544 				stream_get_status(context, pipe_ctx->stream);
2545 
2546 			if (dc->hwss.apply_ctx_for_surface)
2547 				dc->hwss.apply_ctx_for_surface(
2548 					dc, pipe_ctx->stream, stream_status->plane_count, context);
2549 		}
2550 	}
2551 	if (dc->hwss.program_front_end_for_ctx && update_type != UPDATE_TYPE_FAST) {
2552 		dc->hwss.program_front_end_for_ctx(dc, context);
2553 #ifdef CONFIG_DRM_AMD_DC_DCN
2554 		if (dc->debug.validate_dml_output) {
2555 			for (i = 0; i < dc->res_pool->pipe_count; i++) {
2556 				struct pipe_ctx cur_pipe = context->res_ctx.pipe_ctx[i];
2557 				if (cur_pipe.stream == NULL)
2558 					continue;
2559 
2560 				cur_pipe.plane_res.hubp->funcs->validate_dml_output(
2561 						cur_pipe.plane_res.hubp, dc->ctx,
2562 						&context->res_ctx.pipe_ctx[i].rq_regs,
2563 						&context->res_ctx.pipe_ctx[i].dlg_regs,
2564 						&context->res_ctx.pipe_ctx[i].ttu_regs);
2565 			}
2566 		}
2567 #endif
2568 	}
2569 
2570 	// Update Type FAST, Surface updates
2571 	if (update_type == UPDATE_TYPE_FAST) {
2572 		if (dc->hwss.set_flip_control_gsl)
2573 			for (i = 0; i < surface_count; i++) {
2574 				struct dc_plane_state *plane_state = srf_updates[i].surface;
2575 
2576 				for (j = 0; j < dc->res_pool->pipe_count; j++) {
2577 					struct pipe_ctx *pipe_ctx = &context->res_ctx.pipe_ctx[j];
2578 
2579 					if (pipe_ctx->stream != stream)
2580 						continue;
2581 
2582 					if (pipe_ctx->plane_state != plane_state)
2583 						continue;
2584 
2585 					// GSL has to be used for flip immediate
2586 					dc->hwss.set_flip_control_gsl(pipe_ctx,
2587 							plane_state->flip_immediate);
2588 				}
2589 			}
2590 		/* Perform requested Updates */
2591 		for (i = 0; i < surface_count; i++) {
2592 			struct dc_plane_state *plane_state = srf_updates[i].surface;
2593 
2594 			for (j = 0; j < dc->res_pool->pipe_count; j++) {
2595 				struct pipe_ctx *pipe_ctx = &context->res_ctx.pipe_ctx[j];
2596 
2597 				if (pipe_ctx->stream != stream)
2598 					continue;
2599 
2600 				if (pipe_ctx->plane_state != plane_state)
2601 					continue;
2602 				/*program triple buffer after lock based on flip type*/
2603 				if (dc->hwss.program_triplebuffer != NULL && dc->debug.enable_tri_buf) {
2604 					/*only enable triplebuffer for  fast_update*/
2605 					dc->hwss.program_triplebuffer(
2606 						dc, pipe_ctx, plane_state->triplebuffer_flips);
2607 				}
2608 				if (srf_updates[i].flip_addr)
2609 					dc->hwss.update_plane_addr(dc, pipe_ctx);
2610 			}
2611 		}
2612 	}
2613 
2614 	if ((update_type != UPDATE_TYPE_FAST) && dc->hwss.interdependent_update_lock)
2615 		dc->hwss.interdependent_update_lock(dc, context, false);
2616 	else
2617 		dc->hwss.pipe_control_lock(dc, top_pipe_to_program, false);
2618 
2619 	if ((update_type != UPDATE_TYPE_FAST) && stream->update_flags.bits.dsc_changed)
2620 		if (top_pipe_to_program->stream_res.tg->funcs->lock_doublebuffer_enable) {
2621 			top_pipe_to_program->stream_res.tg->funcs->wait_for_state(
2622 					top_pipe_to_program->stream_res.tg,
2623 					CRTC_STATE_VACTIVE);
2624 			top_pipe_to_program->stream_res.tg->funcs->wait_for_state(
2625 					top_pipe_to_program->stream_res.tg,
2626 					CRTC_STATE_VBLANK);
2627 			top_pipe_to_program->stream_res.tg->funcs->wait_for_state(
2628 					top_pipe_to_program->stream_res.tg,
2629 					CRTC_STATE_VACTIVE);
2630 
2631 			if (stream && should_use_dmub_lock(stream->link)) {
2632 				union dmub_hw_lock_flags hw_locks = { 0 };
2633 				struct dmub_hw_lock_inst_flags inst_flags = { 0 };
2634 
2635 				hw_locks.bits.lock_dig = 1;
2636 				inst_flags.dig_inst = top_pipe_to_program->stream_res.tg->inst;
2637 
2638 				dmub_hw_lock_mgr_cmd(dc->ctx->dmub_srv,
2639 							false,
2640 							&hw_locks,
2641 							&inst_flags);
2642 			} else
2643 				top_pipe_to_program->stream_res.tg->funcs->lock_doublebuffer_disable(
2644 					top_pipe_to_program->stream_res.tg);
2645 		}
2646 
2647 	if (update_type != UPDATE_TYPE_FAST)
2648 		dc->hwss.post_unlock_program_front_end(dc, context);
2649 
2650 	// Fire manual trigger only when bottom plane is flipped
2651 	for (j = 0; j < dc->res_pool->pipe_count; j++) {
2652 		struct pipe_ctx *pipe_ctx = &context->res_ctx.pipe_ctx[j];
2653 
2654 		if (pipe_ctx->bottom_pipe ||
2655 				!pipe_ctx->stream ||
2656 				pipe_ctx->stream != stream ||
2657 				!pipe_ctx->plane_state->update_flags.bits.addr_update)
2658 			continue;
2659 
2660 		if (pipe_ctx->stream_res.tg->funcs->program_manual_trigger)
2661 			pipe_ctx->stream_res.tg->funcs->program_manual_trigger(pipe_ctx->stream_res.tg);
2662 	}
2663 }
2664 
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)2665 void dc_commit_updates_for_stream(struct dc *dc,
2666 		struct dc_surface_update *srf_updates,
2667 		int surface_count,
2668 		struct dc_stream_state *stream,
2669 		struct dc_stream_update *stream_update,
2670 		struct dc_state *state)
2671 {
2672 	const struct dc_stream_status *stream_status;
2673 	enum surface_update_type update_type;
2674 	struct dc_state *context;
2675 	struct dc_context *dc_ctx = dc->ctx;
2676 	int i, j;
2677 
2678 	stream_status = dc_stream_get_status(stream);
2679 	context = dc->current_state;
2680 
2681 	update_type = dc_check_update_surfaces_for_stream(
2682 				dc, srf_updates, surface_count, stream_update, stream_status);
2683 
2684 	if (update_type >= update_surface_trace_level)
2685 		update_surface_trace(dc, srf_updates, surface_count);
2686 
2687 
2688 	if (update_type >= UPDATE_TYPE_FULL) {
2689 
2690 		/* initialize scratch memory for building context */
2691 		context = dc_create_state(dc);
2692 		if (context == NULL) {
2693 			DC_ERROR("Failed to allocate new validate context!\n");
2694 			return;
2695 		}
2696 
2697 		dc_resource_state_copy_construct(state, context);
2698 
2699 		for (i = 0; i < dc->res_pool->pipe_count; i++) {
2700 			struct pipe_ctx *new_pipe = &context->res_ctx.pipe_ctx[i];
2701 			struct pipe_ctx *old_pipe = &dc->current_state->res_ctx.pipe_ctx[i];
2702 
2703 			if (new_pipe->plane_state && new_pipe->plane_state != old_pipe->plane_state)
2704 				new_pipe->plane_state->force_full_update = true;
2705 		}
2706 	}
2707 
2708 
2709 	for (i = 0; i < surface_count; i++) {
2710 		struct dc_plane_state *surface = srf_updates[i].surface;
2711 
2712 		copy_surface_update_to_plane(surface, &srf_updates[i]);
2713 
2714 		if (update_type >= UPDATE_TYPE_MED) {
2715 			for (j = 0; j < dc->res_pool->pipe_count; j++) {
2716 				struct pipe_ctx *pipe_ctx =
2717 					&context->res_ctx.pipe_ctx[j];
2718 
2719 				if (pipe_ctx->plane_state != surface)
2720 					continue;
2721 
2722 				resource_build_scaling_params(pipe_ctx);
2723 			}
2724 		}
2725 	}
2726 
2727 	copy_stream_update_to_stream(dc, context, stream, stream_update);
2728 
2729 	if (update_type >= UPDATE_TYPE_FULL) {
2730 		if (!dc->res_pool->funcs->validate_bandwidth(dc, context, false)) {
2731 			DC_ERROR("Mode validation failed for stream update!\n");
2732 			dc_release_state(context);
2733 			return;
2734 		}
2735 	}
2736 
2737 	commit_planes_for_stream(
2738 				dc,
2739 				srf_updates,
2740 				surface_count,
2741 				stream,
2742 				stream_update,
2743 				update_type,
2744 				context);
2745 	/*update current_State*/
2746 	if (dc->current_state != context) {
2747 
2748 		struct dc_state *old = dc->current_state;
2749 
2750 		dc->current_state = context;
2751 		dc_release_state(old);
2752 
2753 		for (i = 0; i < dc->res_pool->pipe_count; i++) {
2754 			struct pipe_ctx *pipe_ctx = &context->res_ctx.pipe_ctx[i];
2755 
2756 			if (pipe_ctx->plane_state && pipe_ctx->stream == stream)
2757 				pipe_ctx->plane_state->force_full_update = false;
2758 		}
2759 	}
2760 	/*let's use current_state to update watermark etc*/
2761 	if (update_type >= UPDATE_TYPE_FULL)
2762 		dc_post_update_surfaces_to_stream(dc);
2763 
2764 	return;
2765 
2766 }
2767 
dc_get_current_stream_count(struct dc * dc)2768 uint8_t dc_get_current_stream_count(struct dc *dc)
2769 {
2770 	return dc->current_state->stream_count;
2771 }
2772 
dc_get_stream_at_index(struct dc * dc,uint8_t i)2773 struct dc_stream_state *dc_get_stream_at_index(struct dc *dc, uint8_t i)
2774 {
2775 	if (i < dc->current_state->stream_count)
2776 		return dc->current_state->streams[i];
2777 	return NULL;
2778 }
2779 
dc_stream_find_from_link(const struct dc_link * link)2780 struct dc_stream_state *dc_stream_find_from_link(const struct dc_link *link)
2781 {
2782 	uint8_t i;
2783 	struct dc_context *ctx = link->ctx;
2784 
2785 	for (i = 0; i < ctx->dc->current_state->stream_count; i++) {
2786 		if (ctx->dc->current_state->streams[i]->link == link)
2787 			return ctx->dc->current_state->streams[i];
2788 	}
2789 
2790 	return NULL;
2791 }
2792 
dc_interrupt_to_irq_source(struct dc * dc,uint32_t src_id,uint32_t ext_id)2793 enum dc_irq_source dc_interrupt_to_irq_source(
2794 		struct dc *dc,
2795 		uint32_t src_id,
2796 		uint32_t ext_id)
2797 {
2798 	return dal_irq_service_to_irq_source(dc->res_pool->irqs, src_id, ext_id);
2799 }
2800 
2801 /**
2802  * dc_interrupt_set() - Enable/disable an AMD hw interrupt source
2803  */
dc_interrupt_set(struct dc * dc,enum dc_irq_source src,bool enable)2804 bool dc_interrupt_set(struct dc *dc, enum dc_irq_source src, bool enable)
2805 {
2806 
2807 	if (dc == NULL)
2808 		return false;
2809 
2810 	return dal_irq_service_set(dc->res_pool->irqs, src, enable);
2811 }
2812 
dc_interrupt_ack(struct dc * dc,enum dc_irq_source src)2813 void dc_interrupt_ack(struct dc *dc, enum dc_irq_source src)
2814 {
2815 	dal_irq_service_ack(dc->res_pool->irqs, src);
2816 }
2817 
dc_power_down_on_boot(struct dc * dc)2818 void dc_power_down_on_boot(struct dc *dc)
2819 {
2820 	if (dc->ctx->dce_environment != DCE_ENV_VIRTUAL_HW &&
2821 			dc->hwss.power_down_on_boot)
2822 		dc->hwss.power_down_on_boot(dc);
2823 }
2824 
dc_set_power_state(struct dc * dc,enum dc_acpi_cm_power_state power_state)2825 void dc_set_power_state(
2826 	struct dc *dc,
2827 	enum dc_acpi_cm_power_state power_state)
2828 {
2829 	struct kref refcount;
2830 	struct display_mode_lib *dml;
2831 
2832 	switch (power_state) {
2833 	case DC_ACPI_CM_POWER_STATE_D0:
2834 		dc_resource_state_construct(dc, dc->current_state);
2835 
2836 		if (dc->ctx->dmub_srv)
2837 			dc_dmub_srv_wait_phy_init(dc->ctx->dmub_srv);
2838 
2839 		dc->hwss.init_hw(dc);
2840 
2841 		if (dc->hwss.init_sys_ctx != NULL &&
2842 			dc->vm_pa_config.valid) {
2843 			dc->hwss.init_sys_ctx(dc->hwseq, dc, &dc->vm_pa_config);
2844 		}
2845 
2846 		break;
2847 	default:
2848 		ASSERT(dc->current_state->stream_count == 0);
2849 		/* Zero out the current context so that on resume we start with
2850 		 * clean state, and dc hw programming optimizations will not
2851 		 * cause any trouble.
2852 		 */
2853 		dml = kzalloc(sizeof(struct display_mode_lib),
2854 				GFP_KERNEL);
2855 
2856 		ASSERT(dml);
2857 		if (!dml)
2858 			return;
2859 
2860 		/* Preserve refcount */
2861 		refcount = dc->current_state->refcount;
2862 		/* Preserve display mode lib */
2863 		memcpy(dml, &dc->current_state->bw_ctx.dml, sizeof(struct display_mode_lib));
2864 
2865 		dc_resource_state_destruct(dc->current_state);
2866 		memset(dc->current_state, 0,
2867 				sizeof(*dc->current_state));
2868 
2869 		dc->current_state->refcount = refcount;
2870 		dc->current_state->bw_ctx.dml = *dml;
2871 
2872 		kfree(dml);
2873 
2874 		break;
2875 	}
2876 }
2877 
dc_resume(struct dc * dc)2878 void dc_resume(struct dc *dc)
2879 {
2880 	uint32_t i;
2881 
2882 	for (i = 0; i < dc->link_count; i++)
2883 		core_link_resume(dc->links[i]);
2884 }
2885 
dc_is_dmcu_initialized(struct dc * dc)2886 bool dc_is_dmcu_initialized(struct dc *dc)
2887 {
2888 	struct dmcu *dmcu = dc->res_pool->dmcu;
2889 
2890 	if (dmcu)
2891 		return dmcu->funcs->is_dmcu_initialized(dmcu);
2892 	return false;
2893 }
2894 
dc_submit_i2c(struct dc * dc,uint32_t link_index,struct i2c_command * cmd)2895 bool dc_submit_i2c(
2896 		struct dc *dc,
2897 		uint32_t link_index,
2898 		struct i2c_command *cmd)
2899 {
2900 
2901 	struct dc_link *link = dc->links[link_index];
2902 	struct ddc_service *ddc = link->ddc;
2903 	return dce_i2c_submit_command(
2904 		dc->res_pool,
2905 		ddc->ddc_pin,
2906 		cmd);
2907 }
2908 
dc_submit_i2c_oem(struct dc * dc,struct i2c_command * cmd)2909 bool dc_submit_i2c_oem(
2910 		struct dc *dc,
2911 		struct i2c_command *cmd)
2912 {
2913 	struct ddc_service *ddc = dc->res_pool->oem_device;
2914 	return dce_i2c_submit_command(
2915 		dc->res_pool,
2916 		ddc->ddc_pin,
2917 		cmd);
2918 }
2919 
link_add_remote_sink_helper(struct dc_link * dc_link,struct dc_sink * sink)2920 static bool link_add_remote_sink_helper(struct dc_link *dc_link, struct dc_sink *sink)
2921 {
2922 	if (dc_link->sink_count >= MAX_SINKS_PER_LINK) {
2923 		BREAK_TO_DEBUGGER();
2924 		return false;
2925 	}
2926 
2927 	dc_sink_retain(sink);
2928 
2929 	dc_link->remote_sinks[dc_link->sink_count] = sink;
2930 	dc_link->sink_count++;
2931 
2932 	return true;
2933 }
2934 
2935 /**
2936  * dc_link_add_remote_sink() - Create a sink and attach it to an existing link
2937  *
2938  * EDID length is in bytes
2939  */
dc_link_add_remote_sink(struct dc_link * link,const uint8_t * edid,int len,struct dc_sink_init_data * init_data)2940 struct dc_sink *dc_link_add_remote_sink(
2941 		struct dc_link *link,
2942 		const uint8_t *edid,
2943 		int len,
2944 		struct dc_sink_init_data *init_data)
2945 {
2946 	struct dc_sink *dc_sink;
2947 	enum dc_edid_status edid_status;
2948 
2949 	if (len > DC_MAX_EDID_BUFFER_SIZE) {
2950 		dm_error("Max EDID buffer size breached!\n");
2951 		return NULL;
2952 	}
2953 
2954 	if (!init_data) {
2955 		BREAK_TO_DEBUGGER();
2956 		return NULL;
2957 	}
2958 
2959 	if (!init_data->link) {
2960 		BREAK_TO_DEBUGGER();
2961 		return NULL;
2962 	}
2963 
2964 	dc_sink = dc_sink_create(init_data);
2965 
2966 	if (!dc_sink)
2967 		return NULL;
2968 
2969 	memmove(dc_sink->dc_edid.raw_edid, edid, len);
2970 	dc_sink->dc_edid.length = len;
2971 
2972 	if (!link_add_remote_sink_helper(
2973 			link,
2974 			dc_sink))
2975 		goto fail_add_sink;
2976 
2977 	edid_status = dm_helpers_parse_edid_caps(
2978 			link->ctx,
2979 			&dc_sink->dc_edid,
2980 			&dc_sink->edid_caps);
2981 
2982 	/*
2983 	 * Treat device as no EDID device if EDID
2984 	 * parsing fails
2985 	 */
2986 	if (edid_status != EDID_OK) {
2987 		dc_sink->dc_edid.length = 0;
2988 		dm_error("Bad EDID, status%d!\n", edid_status);
2989 	}
2990 
2991 	return dc_sink;
2992 
2993 fail_add_sink:
2994 	dc_sink_release(dc_sink);
2995 	return NULL;
2996 }
2997 
2998 /**
2999  * dc_link_remove_remote_sink() - Remove a remote sink from a dc_link
3000  *
3001  * Note that this just removes the struct dc_sink - it doesn't
3002  * program hardware or alter other members of dc_link
3003  */
dc_link_remove_remote_sink(struct dc_link * link,struct dc_sink * sink)3004 void dc_link_remove_remote_sink(struct dc_link *link, struct dc_sink *sink)
3005 {
3006 	int i;
3007 
3008 	if (!link->sink_count) {
3009 		BREAK_TO_DEBUGGER();
3010 		return;
3011 	}
3012 
3013 	for (i = 0; i < link->sink_count; i++) {
3014 		if (link->remote_sinks[i] == sink) {
3015 			dc_sink_release(sink);
3016 			link->remote_sinks[i] = NULL;
3017 
3018 			/* shrink array to remove empty place */
3019 			while (i < link->sink_count - 1) {
3020 				link->remote_sinks[i] = link->remote_sinks[i+1];
3021 				i++;
3022 			}
3023 			link->remote_sinks[i] = NULL;
3024 			link->sink_count--;
3025 			return;
3026 		}
3027 	}
3028 }
3029 
get_clock_requirements_for_state(struct dc_state * state,struct AsicStateEx * info)3030 void get_clock_requirements_for_state(struct dc_state *state, struct AsicStateEx *info)
3031 {
3032 	info->displayClock				= (unsigned int)state->bw_ctx.bw.dcn.clk.dispclk_khz;
3033 	info->engineClock				= (unsigned int)state->bw_ctx.bw.dcn.clk.dcfclk_khz;
3034 	info->memoryClock				= (unsigned int)state->bw_ctx.bw.dcn.clk.dramclk_khz;
3035 	info->maxSupportedDppClock		= (unsigned int)state->bw_ctx.bw.dcn.clk.max_supported_dppclk_khz;
3036 	info->dppClock					= (unsigned int)state->bw_ctx.bw.dcn.clk.dppclk_khz;
3037 	info->socClock					= (unsigned int)state->bw_ctx.bw.dcn.clk.socclk_khz;
3038 	info->dcfClockDeepSleep			= (unsigned int)state->bw_ctx.bw.dcn.clk.dcfclk_deep_sleep_khz;
3039 	info->fClock					= (unsigned int)state->bw_ctx.bw.dcn.clk.fclk_khz;
3040 	info->phyClock					= (unsigned int)state->bw_ctx.bw.dcn.clk.phyclk_khz;
3041 }
dc_set_clock(struct dc * dc,enum dc_clock_type clock_type,uint32_t clk_khz,uint32_t stepping)3042 enum dc_status dc_set_clock(struct dc *dc, enum dc_clock_type clock_type, uint32_t clk_khz, uint32_t stepping)
3043 {
3044 	if (dc->hwss.set_clock)
3045 		return dc->hwss.set_clock(dc, clock_type, clk_khz, stepping);
3046 	return DC_ERROR_UNEXPECTED;
3047 }
dc_get_clock(struct dc * dc,enum dc_clock_type clock_type,struct dc_clock_config * clock_cfg)3048 void dc_get_clock(struct dc *dc, enum dc_clock_type clock_type, struct dc_clock_config *clock_cfg)
3049 {
3050 	if (dc->hwss.get_clock)
3051 		dc->hwss.get_clock(dc, clock_type, clock_cfg);
3052 }
3053 
3054 /* enable/disable eDP PSR without specify stream for eDP */
dc_set_psr_allow_active(struct dc * dc,bool enable)3055 bool dc_set_psr_allow_active(struct dc *dc, bool enable)
3056 {
3057 	int i;
3058 
3059 	for (i = 0; i < dc->current_state->stream_count ; i++) {
3060 		struct dc_link *link;
3061 		struct dc_stream_state *stream = dc->current_state->streams[i];
3062 
3063 		link = stream->link;
3064 		if (!link)
3065 			continue;
3066 
3067 		if (link->psr_settings.psr_feature_enabled) {
3068 			if (enable && !link->psr_settings.psr_allow_active)
3069 				return dc_link_set_psr_allow_active(link, true, false);
3070 			else if (!enable && link->psr_settings.psr_allow_active)
3071 				return dc_link_set_psr_allow_active(link, false, true);
3072 		}
3073 	}
3074 
3075 	return true;
3076 }
3077 
3078 #if defined(CONFIG_DRM_AMD_DC_DCN3_0)
3079 
dc_allow_idle_optimizations(struct dc * dc,bool allow)3080 void dc_allow_idle_optimizations(struct dc *dc, bool allow)
3081 {
3082 	if (dc->debug.disable_idle_power_optimizations)
3083 		return;
3084 
3085 	if (allow == dc->idle_optimizations_allowed)
3086 		return;
3087 
3088 	if (dc->hwss.apply_idle_power_optimizations && dc->hwss.apply_idle_power_optimizations(dc, allow))
3089 		dc->idle_optimizations_allowed = allow;
3090 }
3091 
3092 /*
3093  * blank all streams, and set min and max memory clock to
3094  * lowest and highest DPM level, respectively
3095  */
dc_unlock_memory_clock_frequency(struct dc * dc)3096 void dc_unlock_memory_clock_frequency(struct dc *dc)
3097 {
3098 	unsigned int i;
3099 
3100 	for (i = 0; i < MAX_PIPES; i++)
3101 		if (dc->current_state->res_ctx.pipe_ctx[i].plane_state)
3102 			core_link_disable_stream(&dc->current_state->res_ctx.pipe_ctx[i]);
3103 
3104 	dc->clk_mgr->funcs->set_hard_min_memclk(dc->clk_mgr, false);
3105 	dc->clk_mgr->funcs->set_hard_max_memclk(dc->clk_mgr);
3106 }
3107 
3108 /*
3109  * set min memory clock to the min required for current mode,
3110  * max to maxDPM, and unblank streams
3111  */
dc_lock_memory_clock_frequency(struct dc * dc)3112 void dc_lock_memory_clock_frequency(struct dc *dc)
3113 {
3114 	unsigned int i;
3115 
3116 	dc->clk_mgr->funcs->get_memclk_states_from_smu(dc->clk_mgr);
3117 	dc->clk_mgr->funcs->set_hard_min_memclk(dc->clk_mgr, true);
3118 	dc->clk_mgr->funcs->set_hard_max_memclk(dc->clk_mgr);
3119 
3120 	for (i = 0; i < MAX_PIPES; i++)
3121 		if (dc->current_state->res_ctx.pipe_ctx[i].plane_state)
3122 			core_link_enable_stream(dc->current_state, &dc->current_state->res_ctx.pipe_ctx[i]);
3123 }
3124 
dc_is_plane_eligible_for_idle_optimizaitons(struct dc * dc,struct dc_plane_state * plane)3125 bool dc_is_plane_eligible_for_idle_optimizaitons(struct dc *dc,
3126 						 struct dc_plane_state *plane)
3127 {
3128 	return false;
3129 }
3130 #endif
3131