• 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 #endif
738 
739 	dc->debug.force_ignore_link_settings = init_params->force_ignore_link_settings;
740 
741 	if (dc->res_pool->funcs->update_bw_bounding_box)
742 		dc->res_pool->funcs->update_bw_bounding_box(dc, dc->clk_mgr->bw_params);
743 
744 	/* Creation of current_state must occur after dc->dml
745 	 * is initialized in dc_create_resource_pool because
746 	 * on creation it copies the contents of dc->dml
747 	 */
748 
749 	dc->current_state = dc_create_state(dc);
750 
751 	if (!dc->current_state) {
752 		dm_error("%s: failed to create validate ctx\n", __func__);
753 		goto fail;
754 	}
755 
756 	dc_resource_state_construct(dc, dc->current_state);
757 
758 	if (!create_links(dc, init_params->num_virtual_links))
759 		goto fail;
760 
761 	return true;
762 
763 fail:
764 	return false;
765 }
766 
disable_all_writeback_pipes_for_stream(const struct dc * dc,struct dc_stream_state * stream,struct dc_state * context)767 static bool disable_all_writeback_pipes_for_stream(
768 		const struct dc *dc,
769 		struct dc_stream_state *stream,
770 		struct dc_state *context)
771 {
772 	int i;
773 
774 	for (i = 0; i < stream->num_wb_info; i++)
775 		stream->writeback_info[i].wb_enabled = false;
776 
777 	return true;
778 }
779 
apply_ctx_interdependent_lock(struct dc * dc,struct dc_state * context,struct dc_stream_state * stream,bool lock)780 void apply_ctx_interdependent_lock(struct dc *dc, struct dc_state *context, struct dc_stream_state *stream, bool lock)
781 {
782 	int i = 0;
783 
784 	/* Checks if interdependent update function pointer is NULL or not, takes care of DCE110 case */
785 	if (dc->hwss.interdependent_update_lock)
786 		dc->hwss.interdependent_update_lock(dc, context, lock);
787 	else {
788 		for (i = 0; i < dc->res_pool->pipe_count; i++) {
789 			struct pipe_ctx *pipe_ctx = &context->res_ctx.pipe_ctx[i];
790 			struct pipe_ctx *old_pipe_ctx = &dc->current_state->res_ctx.pipe_ctx[i];
791 
792 			// Copied conditions that were previously in dce110_apply_ctx_for_surface
793 			if (stream == pipe_ctx->stream) {
794 				if (!pipe_ctx->top_pipe &&
795 					(pipe_ctx->plane_state || old_pipe_ctx->plane_state))
796 					dc->hwss.pipe_control_lock(dc, pipe_ctx, lock);
797 			}
798 		}
799 	}
800 }
801 
disable_dangling_plane(struct dc * dc,struct dc_state * context)802 static void disable_dangling_plane(struct dc *dc, struct dc_state *context)
803 {
804 	int i, j;
805 	struct dc_state *dangling_context = dc_create_state(dc);
806 	struct dc_state *current_ctx;
807 
808 	if (dangling_context == NULL)
809 		return;
810 
811 	dc_resource_state_copy_construct(dc->current_state, dangling_context);
812 
813 	for (i = 0; i < dc->res_pool->pipe_count; i++) {
814 		struct dc_stream_state *old_stream =
815 				dc->current_state->res_ctx.pipe_ctx[i].stream;
816 		bool should_disable = true;
817 
818 		for (j = 0; j < context->stream_count; j++) {
819 			if (old_stream == context->streams[j]) {
820 				should_disable = false;
821 				break;
822 			}
823 		}
824 		if (should_disable && old_stream) {
825 			dc_rem_all_planes_for_stream(dc, old_stream, dangling_context);
826 			disable_all_writeback_pipes_for_stream(dc, old_stream, dangling_context);
827 
828 			if (dc->hwss.apply_ctx_for_surface) {
829 				apply_ctx_interdependent_lock(dc, dc->current_state, old_stream, true);
830 				dc->hwss.apply_ctx_for_surface(dc, old_stream, 0, dangling_context);
831 				apply_ctx_interdependent_lock(dc, dc->current_state, old_stream, false);
832 				dc->hwss.post_unlock_program_front_end(dc, dangling_context);
833 			}
834 			if (dc->hwss.program_front_end_for_ctx) {
835 				dc->hwss.interdependent_update_lock(dc, dc->current_state, true);
836 				dc->hwss.program_front_end_for_ctx(dc, dangling_context);
837 				dc->hwss.interdependent_update_lock(dc, dc->current_state, false);
838 				dc->hwss.post_unlock_program_front_end(dc, dangling_context);
839 			}
840 		}
841 	}
842 
843 	current_ctx = dc->current_state;
844 	dc->current_state = dangling_context;
845 	dc_release_state(current_ctx);
846 }
847 
disable_vbios_mode_if_required(struct dc * dc,struct dc_state * context)848 static void disable_vbios_mode_if_required(
849 		struct dc *dc,
850 		struct dc_state *context)
851 {
852 	unsigned int i, j;
853 
854 	/* check if timing_changed, disable stream*/
855 	for (i = 0; i < dc->res_pool->pipe_count; i++) {
856 		struct dc_stream_state *stream = NULL;
857 		struct dc_link *link = NULL;
858 		struct pipe_ctx *pipe = NULL;
859 
860 		pipe = &context->res_ctx.pipe_ctx[i];
861 		stream = pipe->stream;
862 		if (stream == NULL)
863 			continue;
864 
865 		if (stream->link->local_sink &&
866 			stream->link->local_sink->sink_signal == SIGNAL_TYPE_EDP) {
867 			link = stream->link;
868 		}
869 
870 		if (link != NULL) {
871 			unsigned int enc_inst, tg_inst = 0;
872 			unsigned int pix_clk_100hz;
873 
874 			enc_inst = link->link_enc->funcs->get_dig_frontend(link->link_enc);
875 			if (enc_inst != ENGINE_ID_UNKNOWN) {
876 				for (j = 0; j < dc->res_pool->stream_enc_count; j++) {
877 					if (dc->res_pool->stream_enc[j]->id == enc_inst) {
878 						tg_inst = dc->res_pool->stream_enc[j]->funcs->dig_source_otg(
879 							dc->res_pool->stream_enc[j]);
880 						break;
881 					}
882 				}
883 
884 				dc->res_pool->dp_clock_source->funcs->get_pixel_clk_frequency_100hz(
885 					dc->res_pool->dp_clock_source,
886 					tg_inst, &pix_clk_100hz);
887 
888 				if (link->link_status.link_active) {
889 					uint32_t requested_pix_clk_100hz =
890 						pipe->stream_res.pix_clk_params.requested_pix_clk_100hz;
891 
892 					if (pix_clk_100hz != requested_pix_clk_100hz) {
893 						core_link_disable_stream(pipe);
894 						pipe->stream->dpms_off = false;
895 					}
896 				}
897 			}
898 		}
899 	}
900 }
901 
wait_for_no_pipes_pending(struct dc * dc,struct dc_state * context)902 static void wait_for_no_pipes_pending(struct dc *dc, struct dc_state *context)
903 {
904 	int i;
905 	PERF_TRACE();
906 	for (i = 0; i < MAX_PIPES; i++) {
907 		int count = 0;
908 		struct pipe_ctx *pipe = &context->res_ctx.pipe_ctx[i];
909 
910 		if (!pipe->plane_state)
911 			continue;
912 
913 		/* Timeout 100 ms */
914 		while (count < 100000) {
915 			/* Must set to false to start with, due to OR in update function */
916 			pipe->plane_state->status.is_flip_pending = false;
917 			dc->hwss.update_pending_status(pipe);
918 			if (!pipe->plane_state->status.is_flip_pending)
919 				break;
920 			udelay(1);
921 			count++;
922 		}
923 		ASSERT(!pipe->plane_state->status.is_flip_pending);
924 	}
925 	PERF_TRACE();
926 }
927 
928 /*******************************************************************************
929  * Public functions
930  ******************************************************************************/
931 
dc_create(const struct dc_init_data * init_params)932 struct dc *dc_create(const struct dc_init_data *init_params)
933 {
934 	struct dc *dc = kzalloc(sizeof(*dc), GFP_KERNEL);
935 	unsigned int full_pipe_count;
936 
937 	if (NULL == dc)
938 		goto alloc_fail;
939 
940 	if (init_params->dce_environment == DCE_ENV_VIRTUAL_HW) {
941 		if (false == dc_construct_ctx(dc, init_params)) {
942 			dc_destruct(dc);
943 			goto construct_fail;
944 		}
945 	} else {
946 		if (false == dc_construct(dc, init_params)) {
947 			dc_destruct(dc);
948 			goto construct_fail;
949 		}
950 
951 		full_pipe_count = dc->res_pool->pipe_count;
952 		if (dc->res_pool->underlay_pipe_index != NO_UNDERLAY_PIPE)
953 			full_pipe_count--;
954 		dc->caps.max_streams = min(
955 				full_pipe_count,
956 				dc->res_pool->stream_enc_count);
957 
958 		dc->optimize_seamless_boot_streams = 0;
959 		dc->caps.max_links = dc->link_count;
960 		dc->caps.max_audios = dc->res_pool->audio_count;
961 		dc->caps.linear_pitch_alignment = 64;
962 
963 		dc->caps.max_dp_protocol_version = DP_VERSION_1_4;
964 
965 		if (dc->res_pool->dmcu != NULL)
966 			dc->versions.dmcu_version = dc->res_pool->dmcu->dmcu_version;
967 	}
968 
969 	/* Populate versioning information */
970 	dc->versions.dc_ver = DC_VER;
971 
972 	dc->build_id = DC_BUILD_ID;
973 
974 	DC_LOG_DC("Display Core initialized\n");
975 
976 
977 
978 	return dc;
979 
980 construct_fail:
981 	kfree(dc);
982 
983 alloc_fail:
984 	return NULL;
985 }
986 
dc_hardware_init(struct dc * dc)987 void dc_hardware_init(struct dc *dc)
988 {
989 	if (dc->ctx->dce_environment != DCE_ENV_VIRTUAL_HW)
990 		dc->hwss.init_hw(dc);
991 }
992 
dc_init_callbacks(struct dc * dc,const struct dc_callback_init * init_params)993 void dc_init_callbacks(struct dc *dc,
994 		const struct dc_callback_init *init_params)
995 {
996 #ifdef CONFIG_DRM_AMD_DC_HDCP
997 	dc->ctx->cp_psp = init_params->cp_psp;
998 #endif
999 }
1000 
dc_deinit_callbacks(struct dc * dc)1001 void dc_deinit_callbacks(struct dc *dc)
1002 {
1003 #ifdef CONFIG_DRM_AMD_DC_HDCP
1004 	memset(&dc->ctx->cp_psp, 0, sizeof(dc->ctx->cp_psp));
1005 #endif
1006 }
1007 
dc_destroy(struct dc ** dc)1008 void dc_destroy(struct dc **dc)
1009 {
1010 	dc_destruct(*dc);
1011 	kfree(*dc);
1012 	*dc = NULL;
1013 }
1014 
enable_timing_multisync(struct dc * dc,struct dc_state * ctx)1015 static void enable_timing_multisync(
1016 		struct dc *dc,
1017 		struct dc_state *ctx)
1018 {
1019 	int i = 0, multisync_count = 0;
1020 	int pipe_count = dc->res_pool->pipe_count;
1021 	struct pipe_ctx *multisync_pipes[MAX_PIPES] = { NULL };
1022 
1023 	for (i = 0; i < pipe_count; i++) {
1024 		if (!ctx->res_ctx.pipe_ctx[i].stream ||
1025 				!ctx->res_ctx.pipe_ctx[i].stream->triggered_crtc_reset.enabled)
1026 			continue;
1027 		if (ctx->res_ctx.pipe_ctx[i].stream == ctx->res_ctx.pipe_ctx[i].stream->triggered_crtc_reset.event_source)
1028 			continue;
1029 		multisync_pipes[multisync_count] = &ctx->res_ctx.pipe_ctx[i];
1030 		multisync_count++;
1031 	}
1032 
1033 	if (multisync_count > 0) {
1034 		dc->hwss.enable_per_frame_crtc_position_reset(
1035 			dc, multisync_count, multisync_pipes);
1036 	}
1037 }
1038 
program_timing_sync(struct dc * dc,struct dc_state * ctx)1039 static void program_timing_sync(
1040 		struct dc *dc,
1041 		struct dc_state *ctx)
1042 {
1043 	int i, j, k;
1044 	int group_index = 0;
1045 	int num_group = 0;
1046 	int pipe_count = dc->res_pool->pipe_count;
1047 	struct pipe_ctx *unsynced_pipes[MAX_PIPES] = { NULL };
1048 
1049 	for (i = 0; i < pipe_count; i++) {
1050 		if (!ctx->res_ctx.pipe_ctx[i].stream || ctx->res_ctx.pipe_ctx[i].top_pipe)
1051 			continue;
1052 
1053 		unsynced_pipes[i] = &ctx->res_ctx.pipe_ctx[i];
1054 	}
1055 
1056 	for (i = 0; i < pipe_count; i++) {
1057 		int group_size = 1;
1058 		struct pipe_ctx *pipe_set[MAX_PIPES];
1059 
1060 		if (!unsynced_pipes[i])
1061 			continue;
1062 
1063 		pipe_set[0] = unsynced_pipes[i];
1064 		unsynced_pipes[i] = NULL;
1065 
1066 		/* Add tg to the set, search rest of the tg's for ones with
1067 		 * same timing, add all tgs with same timing to the group
1068 		 */
1069 		for (j = i + 1; j < pipe_count; j++) {
1070 			if (!unsynced_pipes[j])
1071 				continue;
1072 
1073 			if (resource_are_streams_timing_synchronizable(
1074 					unsynced_pipes[j]->stream,
1075 					pipe_set[0]->stream)) {
1076 				pipe_set[group_size] = unsynced_pipes[j];
1077 				unsynced_pipes[j] = NULL;
1078 				group_size++;
1079 			}
1080 		}
1081 
1082 		/* set first unblanked pipe as master */
1083 		for (j = 0; j < group_size; j++) {
1084 			bool is_blanked;
1085 
1086 			if (pipe_set[j]->stream_res.opp->funcs->dpg_is_blanked)
1087 				is_blanked =
1088 					pipe_set[j]->stream_res.opp->funcs->dpg_is_blanked(pipe_set[j]->stream_res.opp);
1089 			else
1090 				is_blanked =
1091 					pipe_set[j]->stream_res.tg->funcs->is_blanked(pipe_set[j]->stream_res.tg);
1092 			if (!is_blanked) {
1093 				if (j == 0)
1094 					break;
1095 
1096 				swap(pipe_set[0], pipe_set[j]);
1097 				break;
1098 			}
1099 		}
1100 
1101 
1102 		for (k = 0; k < group_size; k++) {
1103 			struct dc_stream_status *status = dc_stream_get_status_from_state(ctx, pipe_set[k]->stream);
1104 
1105 			status->timing_sync_info.group_id = num_group;
1106 			status->timing_sync_info.group_size = group_size;
1107 			if (k == 0)
1108 				status->timing_sync_info.master = true;
1109 			else
1110 				status->timing_sync_info.master = false;
1111 
1112 		}
1113 		/* remove any other unblanked pipes as they have already been synced */
1114 		for (j = j + 1; j < group_size; j++) {
1115 			bool is_blanked;
1116 
1117 			if (pipe_set[j]->stream_res.opp->funcs->dpg_is_blanked)
1118 				is_blanked =
1119 					pipe_set[j]->stream_res.opp->funcs->dpg_is_blanked(pipe_set[j]->stream_res.opp);
1120 			else
1121 				is_blanked =
1122 					pipe_set[j]->stream_res.tg->funcs->is_blanked(pipe_set[j]->stream_res.tg);
1123 			if (!is_blanked) {
1124 				group_size--;
1125 				pipe_set[j] = pipe_set[group_size];
1126 				j--;
1127 			}
1128 		}
1129 
1130 		if (group_size > 1) {
1131 			dc->hwss.enable_timing_synchronization(
1132 				dc, group_index, group_size, pipe_set);
1133 			group_index++;
1134 		}
1135 		num_group++;
1136 	}
1137 }
1138 
context_changed(struct dc * dc,struct dc_state * context)1139 static bool context_changed(
1140 		struct dc *dc,
1141 		struct dc_state *context)
1142 {
1143 	uint8_t i;
1144 
1145 	if (context->stream_count != dc->current_state->stream_count)
1146 		return true;
1147 
1148 	for (i = 0; i < dc->current_state->stream_count; i++) {
1149 		if (dc->current_state->streams[i] != context->streams[i])
1150 			return true;
1151 	}
1152 
1153 	return false;
1154 }
1155 
dc_validate_seamless_boot_timing(const struct dc * dc,const struct dc_sink * sink,struct dc_crtc_timing * crtc_timing)1156 bool dc_validate_seamless_boot_timing(const struct dc *dc,
1157 				const struct dc_sink *sink,
1158 				struct dc_crtc_timing *crtc_timing)
1159 {
1160 	struct timing_generator *tg;
1161 	struct stream_encoder *se = NULL;
1162 
1163 	struct dc_crtc_timing hw_crtc_timing = {0};
1164 
1165 	struct dc_link *link = sink->link;
1166 	unsigned int i, enc_inst, tg_inst = 0;
1167 
1168 	// Seamless port only support single DP and EDP so far
1169 	if (sink->sink_signal != SIGNAL_TYPE_DISPLAY_PORT &&
1170 		sink->sink_signal != SIGNAL_TYPE_EDP)
1171 		return false;
1172 
1173 	/* Check for enabled DIG to identify enabled display */
1174 	if (!link->link_enc->funcs->is_dig_enabled(link->link_enc))
1175 		return false;
1176 
1177 	enc_inst = link->link_enc->funcs->get_dig_frontend(link->link_enc);
1178 
1179 	if (enc_inst == ENGINE_ID_UNKNOWN)
1180 		return false;
1181 
1182 	for (i = 0; i < dc->res_pool->stream_enc_count; i++) {
1183 		if (dc->res_pool->stream_enc[i]->id == enc_inst) {
1184 
1185 			se = dc->res_pool->stream_enc[i];
1186 
1187 			tg_inst = dc->res_pool->stream_enc[i]->funcs->dig_source_otg(
1188 				dc->res_pool->stream_enc[i]);
1189 			break;
1190 		}
1191 	}
1192 
1193 	// tg_inst not found
1194 	if (i == dc->res_pool->stream_enc_count)
1195 		return false;
1196 
1197 	if (tg_inst >= dc->res_pool->timing_generator_count)
1198 		return false;
1199 
1200 	tg = dc->res_pool->timing_generators[tg_inst];
1201 
1202 	if (!tg->funcs->get_hw_timing)
1203 		return false;
1204 
1205 	if (!tg->funcs->get_hw_timing(tg, &hw_crtc_timing))
1206 		return false;
1207 
1208 	if (crtc_timing->h_total != hw_crtc_timing.h_total)
1209 		return false;
1210 
1211 	if (crtc_timing->h_border_left != hw_crtc_timing.h_border_left)
1212 		return false;
1213 
1214 	if (crtc_timing->h_addressable != hw_crtc_timing.h_addressable)
1215 		return false;
1216 
1217 	if (crtc_timing->h_border_right != hw_crtc_timing.h_border_right)
1218 		return false;
1219 
1220 	if (crtc_timing->h_front_porch != hw_crtc_timing.h_front_porch)
1221 		return false;
1222 
1223 	if (crtc_timing->h_sync_width != hw_crtc_timing.h_sync_width)
1224 		return false;
1225 
1226 	if (crtc_timing->v_total != hw_crtc_timing.v_total)
1227 		return false;
1228 
1229 	if (crtc_timing->v_border_top != hw_crtc_timing.v_border_top)
1230 		return false;
1231 
1232 	if (crtc_timing->v_addressable != hw_crtc_timing.v_addressable)
1233 		return false;
1234 
1235 	if (crtc_timing->v_border_bottom != hw_crtc_timing.v_border_bottom)
1236 		return false;
1237 
1238 	if (crtc_timing->v_front_porch != hw_crtc_timing.v_front_porch)
1239 		return false;
1240 
1241 	if (crtc_timing->v_sync_width != hw_crtc_timing.v_sync_width)
1242 		return false;
1243 
1244 	if (dc_is_dp_signal(link->connector_signal)) {
1245 		unsigned int pix_clk_100hz;
1246 
1247 		dc->res_pool->dp_clock_source->funcs->get_pixel_clk_frequency_100hz(
1248 			dc->res_pool->dp_clock_source,
1249 			tg_inst, &pix_clk_100hz);
1250 
1251 		if (crtc_timing->pix_clk_100hz != pix_clk_100hz)
1252 			return false;
1253 
1254 		if (!se->funcs->dp_get_pixel_format)
1255 			return false;
1256 
1257 		if (!se->funcs->dp_get_pixel_format(
1258 			se,
1259 			&hw_crtc_timing.pixel_encoding,
1260 			&hw_crtc_timing.display_color_depth))
1261 			return false;
1262 
1263 		if (hw_crtc_timing.display_color_depth != crtc_timing->display_color_depth)
1264 			return false;
1265 
1266 		if (hw_crtc_timing.pixel_encoding != crtc_timing->pixel_encoding)
1267 			return false;
1268 	}
1269 
1270 	return true;
1271 }
1272 
dc_enable_stereo(struct dc * dc,struct dc_state * context,struct dc_stream_state * streams[],uint8_t stream_count)1273 bool dc_enable_stereo(
1274 	struct dc *dc,
1275 	struct dc_state *context,
1276 	struct dc_stream_state *streams[],
1277 	uint8_t stream_count)
1278 {
1279 	bool ret = true;
1280 	int i, j;
1281 	struct pipe_ctx *pipe;
1282 
1283 	for (i = 0; i < MAX_PIPES; i++) {
1284 		if (context != NULL)
1285 			pipe = &context->res_ctx.pipe_ctx[i];
1286 		else
1287 			pipe = &dc->current_state->res_ctx.pipe_ctx[i];
1288 		for (j = 0 ; pipe && j < stream_count; j++)  {
1289 			if (streams[j] && streams[j] == pipe->stream &&
1290 				dc->hwss.setup_stereo)
1291 				dc->hwss.setup_stereo(pipe, dc);
1292 		}
1293 	}
1294 
1295 	return ret;
1296 }
1297 
dc_trigger_sync(struct dc * dc,struct dc_state * context)1298 void dc_trigger_sync(struct dc *dc, struct dc_state *context)
1299 {
1300 	if (context->stream_count > 1 && !dc->debug.disable_timing_sync) {
1301 		enable_timing_multisync(dc, context);
1302 		program_timing_sync(dc, context);
1303 	}
1304 }
1305 
get_stream_mask(struct dc * dc,struct dc_state * context)1306 static uint8_t get_stream_mask(struct dc *dc, struct dc_state *context)
1307 {
1308 	int i;
1309 	unsigned int stream_mask = 0;
1310 
1311 	for (i = 0; i < dc->res_pool->pipe_count; i++) {
1312 		if (context->res_ctx.pipe_ctx[i].stream)
1313 			stream_mask |= 1 << i;
1314 	}
1315 
1316 	return stream_mask;
1317 }
1318 
1319 /*
1320  * Applies given context to HW and copy it into current context.
1321  * It's up to the user to release the src context afterwards.
1322  */
dc_commit_state_no_check(struct dc * dc,struct dc_state * context)1323 static enum dc_status dc_commit_state_no_check(struct dc *dc, struct dc_state *context)
1324 {
1325 	struct dc_bios *dcb = dc->ctx->dc_bios;
1326 	enum dc_status result = DC_ERROR_UNEXPECTED;
1327 	struct pipe_ctx *pipe;
1328 	int i, k, l;
1329 	struct dc_stream_state *dc_streams[MAX_STREAMS] = {0};
1330 
1331 #if defined(CONFIG_DRM_AMD_DC_DCN3_0)
1332 	dc_allow_idle_optimizations(dc, false);
1333 #endif
1334 
1335 	for (i = 0; i < context->stream_count; i++)
1336 		dc_streams[i] =  context->streams[i];
1337 
1338 	if (!dcb->funcs->is_accelerated_mode(dcb)) {
1339 		disable_vbios_mode_if_required(dc, context);
1340 		dc->hwss.enable_accelerated_mode(dc, context);
1341 	}
1342 
1343 	for (i = 0; i < context->stream_count; i++)
1344 		if (context->streams[i]->apply_seamless_boot_optimization)
1345 			dc->optimize_seamless_boot_streams++;
1346 
1347 	if (context->stream_count > dc->optimize_seamless_boot_streams ||
1348 		context->stream_count == 0)
1349 		dc->hwss.prepare_bandwidth(dc, context);
1350 
1351 	disable_dangling_plane(dc, context);
1352 	/* re-program planes for existing stream, in case we need to
1353 	 * free up plane resource for later use
1354 	 */
1355 	if (dc->hwss.apply_ctx_for_surface) {
1356 		for (i = 0; i < context->stream_count; i++) {
1357 			if (context->streams[i]->mode_changed)
1358 				continue;
1359 			apply_ctx_interdependent_lock(dc, context, context->streams[i], true);
1360 			dc->hwss.apply_ctx_for_surface(
1361 				dc, context->streams[i],
1362 				context->stream_status[i].plane_count,
1363 				context); /* use new pipe config in new context */
1364 			apply_ctx_interdependent_lock(dc, context, context->streams[i], false);
1365 			dc->hwss.post_unlock_program_front_end(dc, context);
1366 		}
1367 	}
1368 
1369 	/* Program hardware */
1370 	for (i = 0; i < dc->res_pool->pipe_count; i++) {
1371 		pipe = &context->res_ctx.pipe_ctx[i];
1372 		dc->hwss.wait_for_mpcc_disconnect(dc, dc->res_pool, pipe);
1373 	}
1374 
1375 	result = dc->hwss.apply_ctx_to_hw(dc, context);
1376 
1377 	if (result != DC_OK)
1378 		return result;
1379 
1380 	dc_trigger_sync(dc, context);
1381 
1382 	/* Program all planes within new context*/
1383 	if (dc->hwss.program_front_end_for_ctx) {
1384 		dc->hwss.interdependent_update_lock(dc, context, true);
1385 		dc->hwss.program_front_end_for_ctx(dc, context);
1386 		dc->hwss.interdependent_update_lock(dc, context, false);
1387 		dc->hwss.post_unlock_program_front_end(dc, context);
1388 	}
1389 	for (i = 0; i < context->stream_count; i++) {
1390 		const struct dc_link *link = context->streams[i]->link;
1391 
1392 		if (!context->streams[i]->mode_changed)
1393 			continue;
1394 
1395 		if (dc->hwss.apply_ctx_for_surface) {
1396 			apply_ctx_interdependent_lock(dc, context, context->streams[i], true);
1397 			dc->hwss.apply_ctx_for_surface(
1398 					dc, context->streams[i],
1399 					context->stream_status[i].plane_count,
1400 					context);
1401 			apply_ctx_interdependent_lock(dc, context, context->streams[i], false);
1402 			dc->hwss.post_unlock_program_front_end(dc, context);
1403 		}
1404 
1405 		/*
1406 		 * enable stereo
1407 		 * TODO rework dc_enable_stereo call to work with validation sets?
1408 		 */
1409 		for (k = 0; k < MAX_PIPES; k++) {
1410 			pipe = &context->res_ctx.pipe_ctx[k];
1411 
1412 			for (l = 0 ; pipe && l < context->stream_count; l++)  {
1413 				if (context->streams[l] &&
1414 					context->streams[l] == pipe->stream &&
1415 					dc->hwss.setup_stereo)
1416 					dc->hwss.setup_stereo(pipe, dc);
1417 			}
1418 		}
1419 
1420 		CONN_MSG_MODE(link, "{%dx%d, %dx%d@%dKhz}",
1421 				context->streams[i]->timing.h_addressable,
1422 				context->streams[i]->timing.v_addressable,
1423 				context->streams[i]->timing.h_total,
1424 				context->streams[i]->timing.v_total,
1425 				context->streams[i]->timing.pix_clk_100hz / 10);
1426 	}
1427 
1428 	dc_enable_stereo(dc, context, dc_streams, context->stream_count);
1429 
1430 	if (context->stream_count > dc->optimize_seamless_boot_streams ||
1431 		context->stream_count == 0) {
1432 		/* Must wait for no flips to be pending before doing optimize bw */
1433 		wait_for_no_pipes_pending(dc, context);
1434 		/* pplib is notified if disp_num changed */
1435 		dc->hwss.optimize_bandwidth(dc, context);
1436 	}
1437 
1438 	context->stream_mask = get_stream_mask(dc, context);
1439 
1440 	if (context->stream_mask != dc->current_state->stream_mask)
1441 		dc_dmub_srv_notify_stream_mask(dc->ctx->dmub_srv, context->stream_mask);
1442 
1443 	for (i = 0; i < context->stream_count; i++)
1444 		context->streams[i]->mode_changed = false;
1445 
1446 	dc_release_state(dc->current_state);
1447 
1448 	dc->current_state = context;
1449 
1450 	dc_retain_state(dc->current_state);
1451 
1452 	return result;
1453 }
1454 
dc_commit_state(struct dc * dc,struct dc_state * context)1455 bool dc_commit_state(struct dc *dc, struct dc_state *context)
1456 {
1457 	enum dc_status result = DC_ERROR_UNEXPECTED;
1458 	int i;
1459 
1460 	if (false == context_changed(dc, context))
1461 		return DC_OK;
1462 
1463 	DC_LOG_DC("%s: %d streams\n",
1464 				__func__, context->stream_count);
1465 
1466 	for (i = 0; i < context->stream_count; i++) {
1467 		struct dc_stream_state *stream = context->streams[i];
1468 
1469 		dc_stream_log(dc, stream);
1470 	}
1471 
1472 	result = dc_commit_state_no_check(dc, context);
1473 
1474 	return (result == DC_OK);
1475 }
1476 
1477 #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)1478 bool dc_acquire_release_mpc_3dlut(
1479 		struct dc *dc, bool acquire,
1480 		struct dc_stream_state *stream,
1481 		struct dc_3dlut **lut,
1482 		struct dc_transfer_func **shaper)
1483 {
1484 	int pipe_idx;
1485 	bool ret = false;
1486 	bool found_pipe_idx = false;
1487 	const struct resource_pool *pool = dc->res_pool;
1488 	struct resource_context *res_ctx = &dc->current_state->res_ctx;
1489 	int mpcc_id = 0;
1490 
1491 	if (pool && res_ctx) {
1492 		if (acquire) {
1493 			/*find pipe idx for the given stream*/
1494 			for (pipe_idx = 0; pipe_idx < pool->pipe_count; pipe_idx++) {
1495 				if (res_ctx->pipe_ctx[pipe_idx].stream == stream) {
1496 					found_pipe_idx = true;
1497 					mpcc_id = res_ctx->pipe_ctx[pipe_idx].plane_res.hubp->inst;
1498 					break;
1499 				}
1500 			}
1501 		} else
1502 			found_pipe_idx = true;/*for release pipe_idx is not required*/
1503 
1504 		if (found_pipe_idx) {
1505 			if (acquire && pool->funcs->acquire_post_bldn_3dlut)
1506 				ret = pool->funcs->acquire_post_bldn_3dlut(res_ctx, pool, mpcc_id, lut, shaper);
1507 			else if (acquire == false && pool->funcs->release_post_bldn_3dlut)
1508 				ret = pool->funcs->release_post_bldn_3dlut(res_ctx, pool, lut, shaper);
1509 		}
1510 	}
1511 	return ret;
1512 }
1513 #endif
is_flip_pending_in_pipes(struct dc * dc,struct dc_state * context)1514 static bool is_flip_pending_in_pipes(struct dc *dc, struct dc_state *context)
1515 {
1516 	int i;
1517 	struct pipe_ctx *pipe;
1518 
1519 	for (i = 0; i < MAX_PIPES; i++) {
1520 		pipe = &context->res_ctx.pipe_ctx[i];
1521 
1522 		if (!pipe->plane_state)
1523 			continue;
1524 
1525 		/* Must set to false to start with, due to OR in update function */
1526 		pipe->plane_state->status.is_flip_pending = false;
1527 		dc->hwss.update_pending_status(pipe);
1528 		if (pipe->plane_state->status.is_flip_pending)
1529 			return true;
1530 	}
1531 	return false;
1532 }
1533 
dc_post_update_surfaces_to_stream(struct dc * dc)1534 bool dc_post_update_surfaces_to_stream(struct dc *dc)
1535 {
1536 	int i;
1537 	struct dc_state *context = dc->current_state;
1538 
1539 	if ((!dc->optimized_required) || dc->optimize_seamless_boot_streams > 0)
1540 		return true;
1541 
1542 	post_surface_trace(dc);
1543 
1544 	if (is_flip_pending_in_pipes(dc, context))
1545 		return true;
1546 
1547 	for (i = 0; i < dc->res_pool->pipe_count; i++)
1548 		if (context->res_ctx.pipe_ctx[i].stream == NULL ||
1549 		    context->res_ctx.pipe_ctx[i].plane_state == NULL) {
1550 			context->res_ctx.pipe_ctx[i].pipe_idx = i;
1551 			dc->hwss.disable_plane(dc, &context->res_ctx.pipe_ctx[i]);
1552 		}
1553 
1554 	dc->hwss.optimize_bandwidth(dc, context);
1555 
1556 	dc->optimized_required = false;
1557 	dc->wm_optimized_required = false;
1558 
1559 	return true;
1560 }
1561 
init_state(struct dc * dc,struct dc_state * context)1562 static void init_state(struct dc *dc, struct dc_state *context)
1563 {
1564 	/* Each context must have their own instance of VBA and in order to
1565 	 * initialize and obtain IP and SOC the base DML instance from DC is
1566 	 * initially copied into every context
1567 	 */
1568 #ifdef CONFIG_DRM_AMD_DC_DCN
1569 	memcpy(&context->bw_ctx.dml, &dc->dml, sizeof(struct display_mode_lib));
1570 #endif
1571 }
1572 
dc_create_state(struct dc * dc)1573 struct dc_state *dc_create_state(struct dc *dc)
1574 {
1575 	struct dc_state *context = kvzalloc(sizeof(struct dc_state),
1576 					    GFP_KERNEL);
1577 
1578 	if (!context)
1579 		return NULL;
1580 
1581 	init_state(dc, context);
1582 
1583 	kref_init(&context->refcount);
1584 
1585 	return context;
1586 }
1587 
dc_copy_state(struct dc_state * src_ctx)1588 struct dc_state *dc_copy_state(struct dc_state *src_ctx)
1589 {
1590 	int i, j;
1591 	struct dc_state *new_ctx = kvmalloc(sizeof(struct dc_state), GFP_KERNEL);
1592 
1593 	if (!new_ctx)
1594 		return NULL;
1595 	memcpy(new_ctx, src_ctx, sizeof(struct dc_state));
1596 
1597 	for (i = 0; i < MAX_PIPES; i++) {
1598 			struct pipe_ctx *cur_pipe = &new_ctx->res_ctx.pipe_ctx[i];
1599 
1600 			if (cur_pipe->top_pipe)
1601 				cur_pipe->top_pipe =  &new_ctx->res_ctx.pipe_ctx[cur_pipe->top_pipe->pipe_idx];
1602 
1603 			if (cur_pipe->bottom_pipe)
1604 				cur_pipe->bottom_pipe = &new_ctx->res_ctx.pipe_ctx[cur_pipe->bottom_pipe->pipe_idx];
1605 
1606 			if (cur_pipe->prev_odm_pipe)
1607 				cur_pipe->prev_odm_pipe =  &new_ctx->res_ctx.pipe_ctx[cur_pipe->prev_odm_pipe->pipe_idx];
1608 
1609 			if (cur_pipe->next_odm_pipe)
1610 				cur_pipe->next_odm_pipe = &new_ctx->res_ctx.pipe_ctx[cur_pipe->next_odm_pipe->pipe_idx];
1611 
1612 	}
1613 
1614 	for (i = 0; i < new_ctx->stream_count; i++) {
1615 			dc_stream_retain(new_ctx->streams[i]);
1616 			for (j = 0; j < new_ctx->stream_status[i].plane_count; j++)
1617 				dc_plane_state_retain(
1618 					new_ctx->stream_status[i].plane_states[j]);
1619 	}
1620 
1621 	kref_init(&new_ctx->refcount);
1622 
1623 	return new_ctx;
1624 }
1625 
dc_retain_state(struct dc_state * context)1626 void dc_retain_state(struct dc_state *context)
1627 {
1628 	kref_get(&context->refcount);
1629 }
1630 
dc_state_free(struct kref * kref)1631 static void dc_state_free(struct kref *kref)
1632 {
1633 	struct dc_state *context = container_of(kref, struct dc_state, refcount);
1634 	dc_resource_state_destruct(context);
1635 	kvfree(context);
1636 }
1637 
dc_release_state(struct dc_state * context)1638 void dc_release_state(struct dc_state *context)
1639 {
1640 	kref_put(&context->refcount, dc_state_free);
1641 }
1642 
dc_set_generic_gpio_for_stereo(bool enable,struct gpio_service * gpio_service)1643 bool dc_set_generic_gpio_for_stereo(bool enable,
1644 		struct gpio_service *gpio_service)
1645 {
1646 	enum gpio_result gpio_result = GPIO_RESULT_NON_SPECIFIC_ERROR;
1647 	struct gpio_pin_info pin_info;
1648 	struct gpio *generic;
1649 	struct gpio_generic_mux_config *config = kzalloc(sizeof(struct gpio_generic_mux_config),
1650 			   GFP_KERNEL);
1651 
1652 	if (!config)
1653 		return false;
1654 	pin_info = dal_gpio_get_generic_pin_info(gpio_service, GPIO_ID_GENERIC, 0);
1655 
1656 	if (pin_info.mask == 0xFFFFFFFF || pin_info.offset == 0xFFFFFFFF) {
1657 		kfree(config);
1658 		return false;
1659 	} else {
1660 		generic = dal_gpio_service_create_generic_mux(
1661 			gpio_service,
1662 			pin_info.offset,
1663 			pin_info.mask);
1664 	}
1665 
1666 	if (!generic) {
1667 		kfree(config);
1668 		return false;
1669 	}
1670 
1671 	gpio_result = dal_gpio_open(generic, GPIO_MODE_OUTPUT);
1672 
1673 	config->enable_output_from_mux = enable;
1674 	config->mux_select = GPIO_SIGNAL_SOURCE_PASS_THROUGH_STEREO_SYNC;
1675 
1676 	if (gpio_result == GPIO_RESULT_OK)
1677 		gpio_result = dal_mux_setup_config(generic, config);
1678 
1679 	if (gpio_result == GPIO_RESULT_OK) {
1680 		dal_gpio_close(generic);
1681 		dal_gpio_destroy_generic_mux(&generic);
1682 		kfree(config);
1683 		return true;
1684 	} else {
1685 		dal_gpio_close(generic);
1686 		dal_gpio_destroy_generic_mux(&generic);
1687 		kfree(config);
1688 		return false;
1689 	}
1690 }
1691 
is_surface_in_context(const struct dc_state * context,const struct dc_plane_state * plane_state)1692 static bool is_surface_in_context(
1693 		const struct dc_state *context,
1694 		const struct dc_plane_state *plane_state)
1695 {
1696 	int j;
1697 
1698 	for (j = 0; j < MAX_PIPES; j++) {
1699 		const struct pipe_ctx *pipe_ctx = &context->res_ctx.pipe_ctx[j];
1700 
1701 		if (plane_state == pipe_ctx->plane_state) {
1702 			return true;
1703 		}
1704 	}
1705 
1706 	return false;
1707 }
1708 
get_plane_info_update_type(const struct dc_surface_update * u)1709 static enum surface_update_type get_plane_info_update_type(const struct dc_surface_update *u)
1710 {
1711 	union surface_update_flags *update_flags = &u->surface->update_flags;
1712 	enum surface_update_type update_type = UPDATE_TYPE_FAST;
1713 
1714 	if (!u->plane_info)
1715 		return UPDATE_TYPE_FAST;
1716 
1717 	if (u->plane_info->color_space != u->surface->color_space) {
1718 		update_flags->bits.color_space_change = 1;
1719 		elevate_update_type(&update_type, UPDATE_TYPE_MED);
1720 	}
1721 
1722 	if (u->plane_info->horizontal_mirror != u->surface->horizontal_mirror) {
1723 		update_flags->bits.horizontal_mirror_change = 1;
1724 		elevate_update_type(&update_type, UPDATE_TYPE_MED);
1725 	}
1726 
1727 	if (u->plane_info->rotation != u->surface->rotation) {
1728 		update_flags->bits.rotation_change = 1;
1729 		elevate_update_type(&update_type, UPDATE_TYPE_FULL);
1730 	}
1731 
1732 	if (u->plane_info->format != u->surface->format) {
1733 		update_flags->bits.pixel_format_change = 1;
1734 		elevate_update_type(&update_type, UPDATE_TYPE_FULL);
1735 	}
1736 
1737 	if (u->plane_info->stereo_format != u->surface->stereo_format) {
1738 		update_flags->bits.stereo_format_change = 1;
1739 		elevate_update_type(&update_type, UPDATE_TYPE_FULL);
1740 	}
1741 
1742 	if (u->plane_info->per_pixel_alpha != u->surface->per_pixel_alpha) {
1743 		update_flags->bits.per_pixel_alpha_change = 1;
1744 		elevate_update_type(&update_type, UPDATE_TYPE_MED);
1745 	}
1746 
1747 	if (u->plane_info->global_alpha_value != u->surface->global_alpha_value) {
1748 		update_flags->bits.global_alpha_change = 1;
1749 		elevate_update_type(&update_type, UPDATE_TYPE_MED);
1750 	}
1751 
1752 	if (u->plane_info->dcc.enable != u->surface->dcc.enable
1753 			|| u->plane_info->dcc.independent_64b_blks != u->surface->dcc.independent_64b_blks
1754 			|| u->plane_info->dcc.meta_pitch != u->surface->dcc.meta_pitch) {
1755 		update_flags->bits.dcc_change = 1;
1756 		elevate_update_type(&update_type, UPDATE_TYPE_MED);
1757 	}
1758 
1759 	if (resource_pixel_format_to_bpp(u->plane_info->format) !=
1760 			resource_pixel_format_to_bpp(u->surface->format)) {
1761 		/* different bytes per element will require full bandwidth
1762 		 * and DML calculation
1763 		 */
1764 		update_flags->bits.bpp_change = 1;
1765 		elevate_update_type(&update_type, UPDATE_TYPE_FULL);
1766 	}
1767 
1768 	if (u->plane_info->plane_size.surface_pitch != u->surface->plane_size.surface_pitch
1769 			|| u->plane_info->plane_size.chroma_pitch != u->surface->plane_size.chroma_pitch) {
1770 		update_flags->bits.plane_size_change = 1;
1771 		elevate_update_type(&update_type, UPDATE_TYPE_MED);
1772 	}
1773 
1774 
1775 	if (memcmp(&u->plane_info->tiling_info, &u->surface->tiling_info,
1776 			sizeof(union dc_tiling_info)) != 0) {
1777 		update_flags->bits.swizzle_change = 1;
1778 		elevate_update_type(&update_type, UPDATE_TYPE_MED);
1779 
1780 		/* todo: below are HW dependent, we should add a hook to
1781 		 * DCE/N resource and validated there.
1782 		 */
1783 		if (u->plane_info->tiling_info.gfx9.swizzle != DC_SW_LINEAR) {
1784 			/* swizzled mode requires RQ to be setup properly,
1785 			 * thus need to run DML to calculate RQ settings
1786 			 */
1787 			update_flags->bits.bandwidth_change = 1;
1788 			elevate_update_type(&update_type, UPDATE_TYPE_FULL);
1789 		}
1790 	}
1791 
1792 	/* This should be UPDATE_TYPE_FAST if nothing has changed. */
1793 	return update_type;
1794 }
1795 
get_scaling_info_update_type(const struct dc_surface_update * u)1796 static enum surface_update_type get_scaling_info_update_type(
1797 		const struct dc_surface_update *u)
1798 {
1799 	union surface_update_flags *update_flags = &u->surface->update_flags;
1800 
1801 	if (!u->scaling_info)
1802 		return UPDATE_TYPE_FAST;
1803 
1804 	if (u->scaling_info->clip_rect.width != u->surface->clip_rect.width
1805 			|| u->scaling_info->clip_rect.height != u->surface->clip_rect.height
1806 			|| u->scaling_info->dst_rect.width != u->surface->dst_rect.width
1807 			|| u->scaling_info->dst_rect.height != u->surface->dst_rect.height
1808 			|| u->scaling_info->scaling_quality.integer_scaling !=
1809 				u->surface->scaling_quality.integer_scaling
1810 			) {
1811 		update_flags->bits.scaling_change = 1;
1812 
1813 		if ((u->scaling_info->dst_rect.width < u->surface->dst_rect.width
1814 			|| u->scaling_info->dst_rect.height < u->surface->dst_rect.height)
1815 				&& (u->scaling_info->dst_rect.width < u->surface->src_rect.width
1816 					|| u->scaling_info->dst_rect.height < u->surface->src_rect.height))
1817 			/* Making dst rect smaller requires a bandwidth change */
1818 			update_flags->bits.bandwidth_change = 1;
1819 	}
1820 
1821 	if (u->scaling_info->src_rect.width != u->surface->src_rect.width
1822 		|| u->scaling_info->src_rect.height != u->surface->src_rect.height) {
1823 
1824 		update_flags->bits.scaling_change = 1;
1825 		if (u->scaling_info->src_rect.width > u->surface->src_rect.width
1826 				|| u->scaling_info->src_rect.height > u->surface->src_rect.height)
1827 			/* Making src rect bigger requires a bandwidth change */
1828 			update_flags->bits.clock_change = 1;
1829 	}
1830 
1831 	if (u->scaling_info->src_rect.x != u->surface->src_rect.x
1832 			|| u->scaling_info->src_rect.y != u->surface->src_rect.y
1833 			|| u->scaling_info->clip_rect.x != u->surface->clip_rect.x
1834 			|| u->scaling_info->clip_rect.y != u->surface->clip_rect.y
1835 			|| u->scaling_info->dst_rect.x != u->surface->dst_rect.x
1836 			|| u->scaling_info->dst_rect.y != u->surface->dst_rect.y)
1837 		update_flags->bits.position_change = 1;
1838 
1839 	if (update_flags->bits.clock_change
1840 			|| update_flags->bits.bandwidth_change
1841 			|| update_flags->bits.scaling_change)
1842 		return UPDATE_TYPE_FULL;
1843 
1844 	if (update_flags->bits.position_change)
1845 		return UPDATE_TYPE_MED;
1846 
1847 	return UPDATE_TYPE_FAST;
1848 }
1849 
det_surface_update(const struct dc * dc,const struct dc_surface_update * u)1850 static enum surface_update_type det_surface_update(const struct dc *dc,
1851 		const struct dc_surface_update *u)
1852 {
1853 	const struct dc_state *context = dc->current_state;
1854 	enum surface_update_type type;
1855 	enum surface_update_type overall_type = UPDATE_TYPE_FAST;
1856 	union surface_update_flags *update_flags = &u->surface->update_flags;
1857 
1858 	if (u->flip_addr)
1859 		update_flags->bits.addr_update = 1;
1860 
1861 	if (!is_surface_in_context(context, u->surface) || u->surface->force_full_update) {
1862 		update_flags->raw = 0xFFFFFFFF;
1863 		return UPDATE_TYPE_FULL;
1864 	}
1865 
1866 	update_flags->raw = 0; // Reset all flags
1867 
1868 	type = get_plane_info_update_type(u);
1869 	elevate_update_type(&overall_type, type);
1870 
1871 	type = get_scaling_info_update_type(u);
1872 	elevate_update_type(&overall_type, type);
1873 
1874 	if (u->flip_addr)
1875 		update_flags->bits.addr_update = 1;
1876 
1877 	if (u->in_transfer_func)
1878 		update_flags->bits.in_transfer_func_change = 1;
1879 
1880 	if (u->input_csc_color_matrix)
1881 		update_flags->bits.input_csc_change = 1;
1882 
1883 	if (u->coeff_reduction_factor)
1884 		update_flags->bits.coeff_reduction_change = 1;
1885 
1886 	if (u->gamut_remap_matrix)
1887 		update_flags->bits.gamut_remap_change = 1;
1888 
1889 	if (u->gamma) {
1890 		enum surface_pixel_format format = SURFACE_PIXEL_FORMAT_GRPH_BEGIN;
1891 
1892 		if (u->plane_info)
1893 			format = u->plane_info->format;
1894 		else if (u->surface)
1895 			format = u->surface->format;
1896 
1897 		if (dce_use_lut(format))
1898 			update_flags->bits.gamma_change = 1;
1899 	}
1900 
1901 	if (u->hdr_mult.value)
1902 		if (u->hdr_mult.value != u->surface->hdr_mult.value) {
1903 			update_flags->bits.hdr_mult = 1;
1904 			elevate_update_type(&overall_type, UPDATE_TYPE_MED);
1905 		}
1906 
1907 	if (update_flags->bits.in_transfer_func_change) {
1908 		type = UPDATE_TYPE_MED;
1909 		elevate_update_type(&overall_type, type);
1910 	}
1911 
1912 	if (update_flags->bits.input_csc_change
1913 			|| update_flags->bits.coeff_reduction_change
1914 			|| update_flags->bits.gamma_change
1915 			|| update_flags->bits.gamut_remap_change) {
1916 		type = UPDATE_TYPE_FULL;
1917 		elevate_update_type(&overall_type, type);
1918 	}
1919 
1920 	return overall_type;
1921 }
1922 
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)1923 static enum surface_update_type check_update_surfaces_for_stream(
1924 		struct dc *dc,
1925 		struct dc_surface_update *updates,
1926 		int surface_count,
1927 		struct dc_stream_update *stream_update,
1928 		const struct dc_stream_status *stream_status)
1929 {
1930 	int i;
1931 	enum surface_update_type overall_type = UPDATE_TYPE_FAST;
1932 
1933 #if defined(CONFIG_DRM_AMD_DC_DCN3_0)
1934 	if (dc->idle_optimizations_allowed)
1935 		overall_type = UPDATE_TYPE_FULL;
1936 
1937 #endif
1938 	if (stream_status == NULL || stream_status->plane_count != surface_count)
1939 		overall_type = UPDATE_TYPE_FULL;
1940 
1941 	/* some stream updates require passive update */
1942 	if (stream_update) {
1943 		union stream_update_flags *su_flags = &stream_update->stream->update_flags;
1944 
1945 		if ((stream_update->src.height != 0 && stream_update->src.width != 0) ||
1946 			(stream_update->dst.height != 0 && stream_update->dst.width != 0) ||
1947 			stream_update->integer_scaling_update)
1948 			su_flags->bits.scaling = 1;
1949 
1950 		if (stream_update->out_transfer_func)
1951 			su_flags->bits.out_tf = 1;
1952 
1953 		if (stream_update->abm_level)
1954 			su_flags->bits.abm_level = 1;
1955 
1956 		if (stream_update->dpms_off)
1957 			su_flags->bits.dpms_off = 1;
1958 
1959 		if (stream_update->gamut_remap)
1960 			su_flags->bits.gamut_remap = 1;
1961 
1962 		if (stream_update->wb_update)
1963 			su_flags->bits.wb_update = 1;
1964 
1965 		if (stream_update->dsc_config)
1966 			su_flags->bits.dsc_changed = 1;
1967 
1968 		if (su_flags->raw != 0)
1969 			overall_type = UPDATE_TYPE_FULL;
1970 
1971 		if (stream_update->output_csc_transform || stream_update->output_color_space)
1972 			su_flags->bits.out_csc = 1;
1973 	}
1974 
1975 	for (i = 0 ; i < surface_count; i++) {
1976 		enum surface_update_type type =
1977 				det_surface_update(dc, &updates[i]);
1978 
1979 		elevate_update_type(&overall_type, type);
1980 	}
1981 
1982 	return overall_type;
1983 }
1984 
1985 /**
1986  * dc_check_update_surfaces_for_stream() - Determine update type (fast, med, or full)
1987  *
1988  * See :c:type:`enum surface_update_type <surface_update_type>` for explanation of update types
1989  */
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)1990 enum surface_update_type dc_check_update_surfaces_for_stream(
1991 		struct dc *dc,
1992 		struct dc_surface_update *updates,
1993 		int surface_count,
1994 		struct dc_stream_update *stream_update,
1995 		const struct dc_stream_status *stream_status)
1996 {
1997 	int i;
1998 	enum surface_update_type type;
1999 
2000 	if (stream_update)
2001 		stream_update->stream->update_flags.raw = 0;
2002 	for (i = 0; i < surface_count; i++)
2003 		updates[i].surface->update_flags.raw = 0;
2004 
2005 	type = check_update_surfaces_for_stream(dc, updates, surface_count, stream_update, stream_status);
2006 	if (type == UPDATE_TYPE_FULL) {
2007 		if (stream_update) {
2008 			uint32_t dsc_changed = stream_update->stream->update_flags.bits.dsc_changed;
2009 			stream_update->stream->update_flags.raw = 0xFFFFFFFF;
2010 			stream_update->stream->update_flags.bits.dsc_changed = dsc_changed;
2011 		}
2012 		for (i = 0; i < surface_count; i++)
2013 			updates[i].surface->update_flags.raw = 0xFFFFFFFF;
2014 	}
2015 
2016 	if (type == UPDATE_TYPE_FAST) {
2017 		// If there's an available clock comparator, we use that.
2018 		if (dc->clk_mgr->funcs->are_clock_states_equal) {
2019 			if (!dc->clk_mgr->funcs->are_clock_states_equal(&dc->clk_mgr->clks, &dc->current_state->bw_ctx.bw.dcn.clk))
2020 				dc->optimized_required = true;
2021 		// Else we fallback to mem compare.
2022 		} 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) {
2023 			dc->optimized_required = true;
2024 		}
2025 
2026 		dc->optimized_required |= dc->wm_optimized_required;
2027 	}
2028 
2029 	return type;
2030 }
2031 
stream_get_status(struct dc_state * ctx,struct dc_stream_state * stream)2032 static struct dc_stream_status *stream_get_status(
2033 	struct dc_state *ctx,
2034 	struct dc_stream_state *stream)
2035 {
2036 	uint8_t i;
2037 
2038 	for (i = 0; i < ctx->stream_count; i++) {
2039 		if (stream == ctx->streams[i]) {
2040 			return &ctx->stream_status[i];
2041 		}
2042 	}
2043 
2044 	return NULL;
2045 }
2046 
2047 static const enum surface_update_type update_surface_trace_level = UPDATE_TYPE_FULL;
2048 
copy_surface_update_to_plane(struct dc_plane_state * surface,struct dc_surface_update * srf_update)2049 static void copy_surface_update_to_plane(
2050 		struct dc_plane_state *surface,
2051 		struct dc_surface_update *srf_update)
2052 {
2053 	if (srf_update->flip_addr) {
2054 		surface->address = srf_update->flip_addr->address;
2055 		surface->flip_immediate =
2056 			srf_update->flip_addr->flip_immediate;
2057 		surface->time.time_elapsed_in_us[surface->time.index] =
2058 			srf_update->flip_addr->flip_timestamp_in_us -
2059 				surface->time.prev_update_time_in_us;
2060 		surface->time.prev_update_time_in_us =
2061 			srf_update->flip_addr->flip_timestamp_in_us;
2062 		surface->time.index++;
2063 		if (surface->time.index >= DC_PLANE_UPDATE_TIMES_MAX)
2064 			surface->time.index = 0;
2065 
2066 		surface->triplebuffer_flips = srf_update->flip_addr->triplebuffer_flips;
2067 	}
2068 
2069 	if (srf_update->scaling_info) {
2070 		surface->scaling_quality =
2071 				srf_update->scaling_info->scaling_quality;
2072 		surface->dst_rect =
2073 				srf_update->scaling_info->dst_rect;
2074 		surface->src_rect =
2075 				srf_update->scaling_info->src_rect;
2076 		surface->clip_rect =
2077 				srf_update->scaling_info->clip_rect;
2078 	}
2079 
2080 	if (srf_update->plane_info) {
2081 		surface->color_space =
2082 				srf_update->plane_info->color_space;
2083 		surface->format =
2084 				srf_update->plane_info->format;
2085 		surface->plane_size =
2086 				srf_update->plane_info->plane_size;
2087 		surface->rotation =
2088 				srf_update->plane_info->rotation;
2089 		surface->horizontal_mirror =
2090 				srf_update->plane_info->horizontal_mirror;
2091 		surface->stereo_format =
2092 				srf_update->plane_info->stereo_format;
2093 		surface->tiling_info =
2094 				srf_update->plane_info->tiling_info;
2095 		surface->visible =
2096 				srf_update->plane_info->visible;
2097 		surface->per_pixel_alpha =
2098 				srf_update->plane_info->per_pixel_alpha;
2099 		surface->global_alpha =
2100 				srf_update->plane_info->global_alpha;
2101 		surface->global_alpha_value =
2102 				srf_update->plane_info->global_alpha_value;
2103 		surface->dcc =
2104 				srf_update->plane_info->dcc;
2105 		surface->layer_index =
2106 				srf_update->plane_info->layer_index;
2107 	}
2108 
2109 	if (srf_update->gamma &&
2110 			(surface->gamma_correction !=
2111 					srf_update->gamma)) {
2112 		memcpy(&surface->gamma_correction->entries,
2113 			&srf_update->gamma->entries,
2114 			sizeof(struct dc_gamma_entries));
2115 		surface->gamma_correction->is_identity =
2116 			srf_update->gamma->is_identity;
2117 		surface->gamma_correction->num_entries =
2118 			srf_update->gamma->num_entries;
2119 		surface->gamma_correction->type =
2120 			srf_update->gamma->type;
2121 	}
2122 
2123 	if (srf_update->in_transfer_func &&
2124 			(surface->in_transfer_func !=
2125 				srf_update->in_transfer_func)) {
2126 		surface->in_transfer_func->sdr_ref_white_level =
2127 			srf_update->in_transfer_func->sdr_ref_white_level;
2128 		surface->in_transfer_func->tf =
2129 			srf_update->in_transfer_func->tf;
2130 		surface->in_transfer_func->type =
2131 			srf_update->in_transfer_func->type;
2132 		memcpy(&surface->in_transfer_func->tf_pts,
2133 			&srf_update->in_transfer_func->tf_pts,
2134 			sizeof(struct dc_transfer_func_distributed_points));
2135 	}
2136 
2137 	if (srf_update->func_shaper &&
2138 			(surface->in_shaper_func !=
2139 			srf_update->func_shaper))
2140 		memcpy(surface->in_shaper_func, srf_update->func_shaper,
2141 		sizeof(*surface->in_shaper_func));
2142 
2143 	if (srf_update->lut3d_func &&
2144 			(surface->lut3d_func !=
2145 			srf_update->lut3d_func))
2146 		memcpy(surface->lut3d_func, srf_update->lut3d_func,
2147 		sizeof(*surface->lut3d_func));
2148 
2149 	if (srf_update->hdr_mult.value)
2150 		surface->hdr_mult =
2151 				srf_update->hdr_mult;
2152 
2153 	if (srf_update->blend_tf &&
2154 			(surface->blend_tf !=
2155 			srf_update->blend_tf))
2156 		memcpy(surface->blend_tf, srf_update->blend_tf,
2157 		sizeof(*surface->blend_tf));
2158 
2159 	if (srf_update->input_csc_color_matrix)
2160 		surface->input_csc_color_matrix =
2161 			*srf_update->input_csc_color_matrix;
2162 
2163 	if (srf_update->coeff_reduction_factor)
2164 		surface->coeff_reduction_factor =
2165 			*srf_update->coeff_reduction_factor;
2166 
2167 	if (srf_update->gamut_remap_matrix)
2168 		surface->gamut_remap_matrix =
2169 			*srf_update->gamut_remap_matrix;
2170 }
2171 
copy_stream_update_to_stream(struct dc * dc,struct dc_state * context,struct dc_stream_state * stream,struct dc_stream_update * update)2172 static void copy_stream_update_to_stream(struct dc *dc,
2173 					 struct dc_state *context,
2174 					 struct dc_stream_state *stream,
2175 					 struct dc_stream_update *update)
2176 {
2177 	struct dc_context *dc_ctx = dc->ctx;
2178 
2179 	if (update == NULL || stream == NULL)
2180 		return;
2181 
2182 	if (update->src.height && update->src.width)
2183 		stream->src = update->src;
2184 
2185 	if (update->dst.height && update->dst.width)
2186 		stream->dst = update->dst;
2187 
2188 	if (update->out_transfer_func &&
2189 	    stream->out_transfer_func != update->out_transfer_func) {
2190 		stream->out_transfer_func->sdr_ref_white_level =
2191 			update->out_transfer_func->sdr_ref_white_level;
2192 		stream->out_transfer_func->tf = update->out_transfer_func->tf;
2193 		stream->out_transfer_func->type =
2194 			update->out_transfer_func->type;
2195 		memcpy(&stream->out_transfer_func->tf_pts,
2196 		       &update->out_transfer_func->tf_pts,
2197 		       sizeof(struct dc_transfer_func_distributed_points));
2198 	}
2199 
2200 	if (update->hdr_static_metadata)
2201 		stream->hdr_static_metadata = *update->hdr_static_metadata;
2202 
2203 	if (update->abm_level)
2204 		stream->abm_level = *update->abm_level;
2205 
2206 	if (update->periodic_interrupt)
2207 		stream->periodic_interrupt = *update->periodic_interrupt;
2208 
2209 	if (update->gamut_remap)
2210 		stream->gamut_remap_matrix = *update->gamut_remap;
2211 
2212 	/* Note: this being updated after mode set is currently not a use case
2213 	 * however if it arises OCSC would need to be reprogrammed at the
2214 	 * minimum
2215 	 */
2216 	if (update->output_color_space)
2217 		stream->output_color_space = *update->output_color_space;
2218 
2219 	if (update->output_csc_transform)
2220 		stream->csc_color_matrix = *update->output_csc_transform;
2221 
2222 	if (update->vrr_infopacket)
2223 		stream->vrr_infopacket = *update->vrr_infopacket;
2224 
2225 	if (update->dpms_off)
2226 		stream->dpms_off = *update->dpms_off;
2227 
2228 	if (update->vsc_infopacket)
2229 		stream->vsc_infopacket = *update->vsc_infopacket;
2230 
2231 	if (update->vsp_infopacket)
2232 		stream->vsp_infopacket = *update->vsp_infopacket;
2233 
2234 	if (update->dither_option)
2235 		stream->dither_option = *update->dither_option;
2236 	/* update current stream with writeback info */
2237 	if (update->wb_update) {
2238 		int i;
2239 
2240 		stream->num_wb_info = update->wb_update->num_wb_info;
2241 		ASSERT(stream->num_wb_info <= MAX_DWB_PIPES);
2242 		for (i = 0; i < stream->num_wb_info; i++)
2243 			stream->writeback_info[i] =
2244 				update->wb_update->writeback_info[i];
2245 	}
2246 	if (update->dsc_config) {
2247 		struct dc_dsc_config old_dsc_cfg = stream->timing.dsc_cfg;
2248 		uint32_t old_dsc_enabled = stream->timing.flags.DSC;
2249 		uint32_t enable_dsc = (update->dsc_config->num_slices_h != 0 &&
2250 				       update->dsc_config->num_slices_v != 0);
2251 
2252 		/* Use temporarry context for validating new DSC config */
2253 		struct dc_state *dsc_validate_context = dc_create_state(dc);
2254 
2255 		if (dsc_validate_context) {
2256 			dc_resource_state_copy_construct(dc->current_state, dsc_validate_context);
2257 
2258 			stream->timing.dsc_cfg = *update->dsc_config;
2259 			stream->timing.flags.DSC = enable_dsc;
2260 			if (!dc->res_pool->funcs->validate_bandwidth(dc, dsc_validate_context, true)) {
2261 				stream->timing.dsc_cfg = old_dsc_cfg;
2262 				stream->timing.flags.DSC = old_dsc_enabled;
2263 				update->dsc_config = NULL;
2264 			}
2265 
2266 			dc_release_state(dsc_validate_context);
2267 		} else {
2268 			DC_ERROR("Failed to allocate new validate context for DSC change\n");
2269 			update->dsc_config = NULL;
2270 		}
2271 	}
2272 }
2273 
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)2274 static void commit_planes_do_stream_update(struct dc *dc,
2275 		struct dc_stream_state *stream,
2276 		struct dc_stream_update *stream_update,
2277 		enum surface_update_type update_type,
2278 		struct dc_state *context)
2279 {
2280 	int j;
2281 	bool should_program_abm;
2282 
2283 	// Stream updates
2284 	for (j = 0; j < dc->res_pool->pipe_count; j++) {
2285 		struct pipe_ctx *pipe_ctx = &context->res_ctx.pipe_ctx[j];
2286 
2287 		if (!pipe_ctx->top_pipe &&  !pipe_ctx->prev_odm_pipe && pipe_ctx->stream == stream) {
2288 
2289 			if (stream_update->periodic_interrupt && dc->hwss.setup_periodic_interrupt)
2290 				dc->hwss.setup_periodic_interrupt(dc, pipe_ctx);
2291 
2292 			if ((stream_update->hdr_static_metadata && !stream->use_dynamic_meta) ||
2293 					stream_update->vrr_infopacket ||
2294 					stream_update->vsc_infopacket ||
2295 					stream_update->vsp_infopacket) {
2296 				resource_build_info_frame(pipe_ctx);
2297 				dc->hwss.update_info_frame(pipe_ctx);
2298 			}
2299 
2300 			if (stream_update->hdr_static_metadata &&
2301 					stream->use_dynamic_meta &&
2302 					dc->hwss.set_dmdata_attributes &&
2303 					pipe_ctx->stream->dmdata_address.quad_part != 0)
2304 				dc->hwss.set_dmdata_attributes(pipe_ctx);
2305 
2306 			if (stream_update->gamut_remap)
2307 				dc_stream_set_gamut_remap(dc, stream);
2308 
2309 			if (stream_update->output_csc_transform)
2310 				dc_stream_program_csc_matrix(dc, stream);
2311 
2312 			if (stream_update->dither_option) {
2313 				struct pipe_ctx *odm_pipe = pipe_ctx->next_odm_pipe;
2314 				resource_build_bit_depth_reduction_params(pipe_ctx->stream,
2315 									&pipe_ctx->stream->bit_depth_params);
2316 				pipe_ctx->stream_res.opp->funcs->opp_program_fmt(pipe_ctx->stream_res.opp,
2317 						&stream->bit_depth_params,
2318 						&stream->clamping);
2319 				while (odm_pipe) {
2320 					odm_pipe->stream_res.opp->funcs->opp_program_fmt(odm_pipe->stream_res.opp,
2321 							&stream->bit_depth_params,
2322 							&stream->clamping);
2323 					odm_pipe = odm_pipe->next_odm_pipe;
2324 				}
2325 			}
2326 
2327 			/* Full fe update*/
2328 			if (update_type == UPDATE_TYPE_FAST)
2329 				continue;
2330 
2331 			if (stream_update->dsc_config)
2332 				dp_update_dsc_config(pipe_ctx);
2333 
2334 			if (stream_update->dpms_off) {
2335 				if (*stream_update->dpms_off) {
2336 					core_link_disable_stream(pipe_ctx);
2337 					/* for dpms, keep acquired resources*/
2338 					if (pipe_ctx->stream_res.audio && !dc->debug.az_endpoint_mute_only)
2339 						pipe_ctx->stream_res.audio->funcs->az_disable(pipe_ctx->stream_res.audio);
2340 
2341 					dc->optimized_required = true;
2342 
2343 				} else {
2344 					if (dc->optimize_seamless_boot_streams == 0)
2345 						dc->hwss.prepare_bandwidth(dc, dc->current_state);
2346 
2347 					core_link_enable_stream(dc->current_state, pipe_ctx);
2348 				}
2349 			}
2350 
2351 			if (stream_update->abm_level && pipe_ctx->stream_res.abm) {
2352 				should_program_abm = true;
2353 
2354 				// if otg funcs defined check if blanked before programming
2355 				if (pipe_ctx->stream_res.tg->funcs->is_blanked)
2356 					if (pipe_ctx->stream_res.tg->funcs->is_blanked(pipe_ctx->stream_res.tg))
2357 						should_program_abm = false;
2358 
2359 				if (should_program_abm) {
2360 					if (*stream_update->abm_level == ABM_LEVEL_IMMEDIATE_DISABLE) {
2361 						dc->hwss.set_abm_immediate_disable(pipe_ctx);
2362 					} else {
2363 						pipe_ctx->stream_res.abm->funcs->set_abm_level(
2364 							pipe_ctx->stream_res.abm, stream->abm_level);
2365 					}
2366 				}
2367 			}
2368 		}
2369 	}
2370 }
2371 
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)2372 static void commit_planes_for_stream(struct dc *dc,
2373 		struct dc_surface_update *srf_updates,
2374 		int surface_count,
2375 		struct dc_stream_state *stream,
2376 		struct dc_stream_update *stream_update,
2377 		enum surface_update_type update_type,
2378 		struct dc_state *context)
2379 {
2380 	bool mpcc_disconnected = false;
2381 	int i, j;
2382 	struct pipe_ctx *top_pipe_to_program = NULL;
2383 
2384 	if (dc->optimize_seamless_boot_streams > 0 && surface_count > 0) {
2385 		/* Optimize seamless boot flag keeps clocks and watermarks high until
2386 		 * first flip. After first flip, optimization is required to lower
2387 		 * bandwidth. Important to note that it is expected UEFI will
2388 		 * only light up a single display on POST, therefore we only expect
2389 		 * one stream with seamless boot flag set.
2390 		 */
2391 		if (stream->apply_seamless_boot_optimization) {
2392 			stream->apply_seamless_boot_optimization = false;
2393 			dc->optimize_seamless_boot_streams--;
2394 
2395 			if (dc->optimize_seamless_boot_streams == 0)
2396 				dc->optimized_required = true;
2397 		}
2398 	}
2399 
2400 	if (update_type == UPDATE_TYPE_FULL) {
2401 #if defined(CONFIG_DRM_AMD_DC_DCN3_0)
2402 		dc_allow_idle_optimizations(dc, false);
2403 
2404 #endif
2405 		if (dc->optimize_seamless_boot_streams == 0)
2406 			dc->hwss.prepare_bandwidth(dc, context);
2407 
2408 		context_clock_trace(dc, context);
2409 	}
2410 
2411 	if (update_type != UPDATE_TYPE_FAST && dc->hwss.interdependent_update_lock &&
2412 		dc->hwss.disconnect_pipes && dc->hwss.wait_for_pending_cleared){
2413 		dc->hwss.interdependent_update_lock(dc, context, true);
2414 		mpcc_disconnected = dc->hwss.disconnect_pipes(dc, context);
2415 		dc->hwss.interdependent_update_lock(dc, context, false);
2416 		if (mpcc_disconnected)
2417 			dc->hwss.wait_for_pending_cleared(dc, context);
2418 	}
2419 
2420 	for (j = 0; j < dc->res_pool->pipe_count; j++) {
2421 		struct pipe_ctx *pipe_ctx = &context->res_ctx.pipe_ctx[j];
2422 
2423 		if (!pipe_ctx->top_pipe &&
2424 			!pipe_ctx->prev_odm_pipe &&
2425 			pipe_ctx->stream &&
2426 			pipe_ctx->stream == stream) {
2427 			top_pipe_to_program = pipe_ctx;
2428 		}
2429 	}
2430 
2431 	if ((update_type != UPDATE_TYPE_FAST) && stream->update_flags.bits.dsc_changed)
2432 		if (top_pipe_to_program &&
2433 			top_pipe_to_program->stream_res.tg->funcs->lock_doublebuffer_enable) {
2434 			if (should_use_dmub_lock(stream->link)) {
2435 				union dmub_hw_lock_flags hw_locks = { 0 };
2436 				struct dmub_hw_lock_inst_flags inst_flags = { 0 };
2437 
2438 				hw_locks.bits.lock_dig = 1;
2439 				inst_flags.dig_inst = top_pipe_to_program->stream_res.tg->inst;
2440 
2441 				dmub_hw_lock_mgr_cmd(dc->ctx->dmub_srv,
2442 							true,
2443 							&hw_locks,
2444 							&inst_flags);
2445 			} else
2446 				top_pipe_to_program->stream_res.tg->funcs->lock_doublebuffer_enable(
2447 						top_pipe_to_program->stream_res.tg);
2448 		}
2449 
2450 	if ((update_type != UPDATE_TYPE_FAST) && dc->hwss.interdependent_update_lock)
2451 		dc->hwss.interdependent_update_lock(dc, context, true);
2452 	else
2453 		/* Lock the top pipe while updating plane addrs, since freesync requires
2454 		 *  plane addr update event triggers to be synchronized.
2455 		 *  top_pipe_to_program is expected to never be NULL
2456 		 */
2457 		dc->hwss.pipe_control_lock(dc, top_pipe_to_program, true);
2458 
2459 
2460 	// Stream updates
2461 	if (stream_update)
2462 		commit_planes_do_stream_update(dc, stream, stream_update, update_type, context);
2463 
2464 	if (surface_count == 0) {
2465 		/*
2466 		 * In case of turning off screen, no need to program front end a second time.
2467 		 * just return after program blank.
2468 		 */
2469 		if (dc->hwss.apply_ctx_for_surface)
2470 			dc->hwss.apply_ctx_for_surface(dc, stream, 0, context);
2471 		if (dc->hwss.program_front_end_for_ctx)
2472 			dc->hwss.program_front_end_for_ctx(dc, context);
2473 
2474 		if ((update_type != UPDATE_TYPE_FAST) && dc->hwss.interdependent_update_lock)
2475 			dc->hwss.interdependent_update_lock(dc, context, false);
2476 		else
2477 			dc->hwss.pipe_control_lock(dc, top_pipe_to_program, false);
2478 
2479 		dc->hwss.post_unlock_program_front_end(dc, context);
2480 		return;
2481 	}
2482 
2483 	if (!IS_DIAG_DC(dc->ctx->dce_environment)) {
2484 		for (i = 0; i < surface_count; i++) {
2485 			struct dc_plane_state *plane_state = srf_updates[i].surface;
2486 			/*set logical flag for lock/unlock use*/
2487 			for (j = 0; j < dc->res_pool->pipe_count; j++) {
2488 				struct pipe_ctx *pipe_ctx = &context->res_ctx.pipe_ctx[j];
2489 				if (!pipe_ctx->plane_state)
2490 					continue;
2491 				if (pipe_ctx->plane_state != plane_state)
2492 					continue;
2493 				plane_state->triplebuffer_flips = false;
2494 				if (update_type == UPDATE_TYPE_FAST &&
2495 					dc->hwss.program_triplebuffer != NULL &&
2496 					!plane_state->flip_immediate && dc->debug.enable_tri_buf) {
2497 						/*triple buffer for VUpdate  only*/
2498 						plane_state->triplebuffer_flips = true;
2499 				}
2500 			}
2501 			if (update_type == UPDATE_TYPE_FULL) {
2502 				/* force vsync flip when reconfiguring pipes to prevent underflow */
2503 				plane_state->flip_immediate = false;
2504 			}
2505 		}
2506 	}
2507 
2508 	// Update Type FULL, Surface updates
2509 	for (j = 0; j < dc->res_pool->pipe_count; j++) {
2510 		struct pipe_ctx *pipe_ctx = &context->res_ctx.pipe_ctx[j];
2511 
2512 		if (!pipe_ctx->top_pipe &&
2513 			!pipe_ctx->prev_odm_pipe &&
2514 			pipe_ctx->stream &&
2515 			pipe_ctx->stream == stream) {
2516 			struct dc_stream_status *stream_status = NULL;
2517 
2518 			if (!pipe_ctx->plane_state)
2519 				continue;
2520 
2521 			/* Full fe update*/
2522 			if (update_type == UPDATE_TYPE_FAST)
2523 				continue;
2524 
2525 			ASSERT(!pipe_ctx->plane_state->triplebuffer_flips);
2526 
2527 			if (dc->hwss.program_triplebuffer != NULL && dc->debug.enable_tri_buf) {
2528 				/*turn off triple buffer for full update*/
2529 				dc->hwss.program_triplebuffer(
2530 					dc, pipe_ctx, pipe_ctx->plane_state->triplebuffer_flips);
2531 			}
2532 			stream_status =
2533 				stream_get_status(context, pipe_ctx->stream);
2534 
2535 			if (dc->hwss.apply_ctx_for_surface)
2536 				dc->hwss.apply_ctx_for_surface(
2537 					dc, pipe_ctx->stream, stream_status->plane_count, context);
2538 		}
2539 	}
2540 	if (dc->hwss.program_front_end_for_ctx && update_type != UPDATE_TYPE_FAST) {
2541 		dc->hwss.program_front_end_for_ctx(dc, context);
2542 #ifdef CONFIG_DRM_AMD_DC_DCN
2543 		if (dc->debug.validate_dml_output) {
2544 			for (i = 0; i < dc->res_pool->pipe_count; i++) {
2545 				struct pipe_ctx cur_pipe = context->res_ctx.pipe_ctx[i];
2546 				if (cur_pipe.stream == NULL)
2547 					continue;
2548 
2549 				cur_pipe.plane_res.hubp->funcs->validate_dml_output(
2550 						cur_pipe.plane_res.hubp, dc->ctx,
2551 						&context->res_ctx.pipe_ctx[i].rq_regs,
2552 						&context->res_ctx.pipe_ctx[i].dlg_regs,
2553 						&context->res_ctx.pipe_ctx[i].ttu_regs);
2554 			}
2555 		}
2556 #endif
2557 	}
2558 
2559 	// Update Type FAST, Surface updates
2560 	if (update_type == UPDATE_TYPE_FAST) {
2561 		if (dc->hwss.set_flip_control_gsl)
2562 			for (i = 0; i < surface_count; i++) {
2563 				struct dc_plane_state *plane_state = srf_updates[i].surface;
2564 
2565 				for (j = 0; j < dc->res_pool->pipe_count; j++) {
2566 					struct pipe_ctx *pipe_ctx = &context->res_ctx.pipe_ctx[j];
2567 
2568 					if (pipe_ctx->stream != stream)
2569 						continue;
2570 
2571 					if (pipe_ctx->plane_state != plane_state)
2572 						continue;
2573 
2574 					// GSL has to be used for flip immediate
2575 					dc->hwss.set_flip_control_gsl(pipe_ctx,
2576 							plane_state->flip_immediate);
2577 				}
2578 			}
2579 		/* Perform requested Updates */
2580 		for (i = 0; i < surface_count; i++) {
2581 			struct dc_plane_state *plane_state = srf_updates[i].surface;
2582 
2583 			for (j = 0; j < dc->res_pool->pipe_count; j++) {
2584 				struct pipe_ctx *pipe_ctx = &context->res_ctx.pipe_ctx[j];
2585 
2586 				if (pipe_ctx->stream != stream)
2587 					continue;
2588 
2589 				if (pipe_ctx->plane_state != plane_state)
2590 					continue;
2591 				/*program triple buffer after lock based on flip type*/
2592 				if (dc->hwss.program_triplebuffer != NULL && dc->debug.enable_tri_buf) {
2593 					/*only enable triplebuffer for  fast_update*/
2594 					dc->hwss.program_triplebuffer(
2595 						dc, pipe_ctx, plane_state->triplebuffer_flips);
2596 				}
2597 				if (srf_updates[i].flip_addr)
2598 					dc->hwss.update_plane_addr(dc, pipe_ctx);
2599 			}
2600 		}
2601 	}
2602 
2603 	if ((update_type != UPDATE_TYPE_FAST) && dc->hwss.interdependent_update_lock)
2604 		dc->hwss.interdependent_update_lock(dc, context, false);
2605 	else
2606 		dc->hwss.pipe_control_lock(dc, top_pipe_to_program, false);
2607 
2608 	if ((update_type != UPDATE_TYPE_FAST) && stream->update_flags.bits.dsc_changed)
2609 		if (top_pipe_to_program->stream_res.tg->funcs->lock_doublebuffer_enable) {
2610 			top_pipe_to_program->stream_res.tg->funcs->wait_for_state(
2611 					top_pipe_to_program->stream_res.tg,
2612 					CRTC_STATE_VACTIVE);
2613 			top_pipe_to_program->stream_res.tg->funcs->wait_for_state(
2614 					top_pipe_to_program->stream_res.tg,
2615 					CRTC_STATE_VBLANK);
2616 			top_pipe_to_program->stream_res.tg->funcs->wait_for_state(
2617 					top_pipe_to_program->stream_res.tg,
2618 					CRTC_STATE_VACTIVE);
2619 
2620 			if (stream && should_use_dmub_lock(stream->link)) {
2621 				union dmub_hw_lock_flags hw_locks = { 0 };
2622 				struct dmub_hw_lock_inst_flags inst_flags = { 0 };
2623 
2624 				hw_locks.bits.lock_dig = 1;
2625 				inst_flags.dig_inst = top_pipe_to_program->stream_res.tg->inst;
2626 
2627 				dmub_hw_lock_mgr_cmd(dc->ctx->dmub_srv,
2628 							false,
2629 							&hw_locks,
2630 							&inst_flags);
2631 			} else
2632 				top_pipe_to_program->stream_res.tg->funcs->lock_doublebuffer_disable(
2633 					top_pipe_to_program->stream_res.tg);
2634 		}
2635 
2636 	if (update_type != UPDATE_TYPE_FAST)
2637 		dc->hwss.post_unlock_program_front_end(dc, context);
2638 
2639 	// Fire manual trigger only when bottom plane is flipped
2640 	for (j = 0; j < dc->res_pool->pipe_count; j++) {
2641 		struct pipe_ctx *pipe_ctx = &context->res_ctx.pipe_ctx[j];
2642 
2643 		if (pipe_ctx->bottom_pipe ||
2644 				!pipe_ctx->stream ||
2645 				pipe_ctx->stream != stream ||
2646 				!pipe_ctx->plane_state->update_flags.bits.addr_update)
2647 			continue;
2648 
2649 		if (pipe_ctx->stream_res.tg->funcs->program_manual_trigger)
2650 			pipe_ctx->stream_res.tg->funcs->program_manual_trigger(pipe_ctx->stream_res.tg);
2651 	}
2652 }
2653 
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)2654 void dc_commit_updates_for_stream(struct dc *dc,
2655 		struct dc_surface_update *srf_updates,
2656 		int surface_count,
2657 		struct dc_stream_state *stream,
2658 		struct dc_stream_update *stream_update,
2659 		struct dc_state *state)
2660 {
2661 	const struct dc_stream_status *stream_status;
2662 	enum surface_update_type update_type;
2663 	struct dc_state *context;
2664 	struct dc_context *dc_ctx = dc->ctx;
2665 	int i, j;
2666 
2667 	stream_status = dc_stream_get_status(stream);
2668 	context = dc->current_state;
2669 
2670 	update_type = dc_check_update_surfaces_for_stream(
2671 				dc, srf_updates, surface_count, stream_update, stream_status);
2672 
2673 	if (update_type >= update_surface_trace_level)
2674 		update_surface_trace(dc, srf_updates, surface_count);
2675 
2676 
2677 	if (update_type >= UPDATE_TYPE_FULL) {
2678 
2679 		/* initialize scratch memory for building context */
2680 		context = dc_create_state(dc);
2681 		if (context == NULL) {
2682 			DC_ERROR("Failed to allocate new validate context!\n");
2683 			return;
2684 		}
2685 
2686 		dc_resource_state_copy_construct(state, context);
2687 
2688 		for (i = 0; i < dc->res_pool->pipe_count; i++) {
2689 			struct pipe_ctx *new_pipe = &context->res_ctx.pipe_ctx[i];
2690 			struct pipe_ctx *old_pipe = &dc->current_state->res_ctx.pipe_ctx[i];
2691 
2692 			if (new_pipe->plane_state && new_pipe->plane_state != old_pipe->plane_state)
2693 				new_pipe->plane_state->force_full_update = true;
2694 		}
2695 	}
2696 
2697 
2698 	for (i = 0; i < surface_count; i++) {
2699 		struct dc_plane_state *surface = srf_updates[i].surface;
2700 
2701 		copy_surface_update_to_plane(surface, &srf_updates[i]);
2702 
2703 		if (update_type >= UPDATE_TYPE_MED) {
2704 			for (j = 0; j < dc->res_pool->pipe_count; j++) {
2705 				struct pipe_ctx *pipe_ctx =
2706 					&context->res_ctx.pipe_ctx[j];
2707 
2708 				if (pipe_ctx->plane_state != surface)
2709 					continue;
2710 
2711 				resource_build_scaling_params(pipe_ctx);
2712 			}
2713 		}
2714 	}
2715 
2716 	copy_stream_update_to_stream(dc, context, stream, stream_update);
2717 
2718 	if (update_type >= UPDATE_TYPE_FULL) {
2719 		if (!dc->res_pool->funcs->validate_bandwidth(dc, context, false)) {
2720 			DC_ERROR("Mode validation failed for stream update!\n");
2721 			dc_release_state(context);
2722 			return;
2723 		}
2724 	}
2725 
2726 	commit_planes_for_stream(
2727 				dc,
2728 				srf_updates,
2729 				surface_count,
2730 				stream,
2731 				stream_update,
2732 				update_type,
2733 				context);
2734 	/*update current_State*/
2735 	if (dc->current_state != context) {
2736 
2737 		struct dc_state *old = dc->current_state;
2738 
2739 		dc->current_state = context;
2740 		dc_release_state(old);
2741 
2742 		for (i = 0; i < dc->res_pool->pipe_count; i++) {
2743 			struct pipe_ctx *pipe_ctx = &context->res_ctx.pipe_ctx[i];
2744 
2745 			if (pipe_ctx->plane_state && pipe_ctx->stream == stream)
2746 				pipe_ctx->plane_state->force_full_update = false;
2747 		}
2748 	}
2749 	/*let's use current_state to update watermark etc*/
2750 	if (update_type >= UPDATE_TYPE_FULL)
2751 		dc_post_update_surfaces_to_stream(dc);
2752 
2753 	return;
2754 
2755 }
2756 
dc_get_current_stream_count(struct dc * dc)2757 uint8_t dc_get_current_stream_count(struct dc *dc)
2758 {
2759 	return dc->current_state->stream_count;
2760 }
2761 
dc_get_stream_at_index(struct dc * dc,uint8_t i)2762 struct dc_stream_state *dc_get_stream_at_index(struct dc *dc, uint8_t i)
2763 {
2764 	if (i < dc->current_state->stream_count)
2765 		return dc->current_state->streams[i];
2766 	return NULL;
2767 }
2768 
dc_stream_find_from_link(const struct dc_link * link)2769 struct dc_stream_state *dc_stream_find_from_link(const struct dc_link *link)
2770 {
2771 	uint8_t i;
2772 	struct dc_context *ctx = link->ctx;
2773 
2774 	for (i = 0; i < ctx->dc->current_state->stream_count; i++) {
2775 		if (ctx->dc->current_state->streams[i]->link == link)
2776 			return ctx->dc->current_state->streams[i];
2777 	}
2778 
2779 	return NULL;
2780 }
2781 
dc_interrupt_to_irq_source(struct dc * dc,uint32_t src_id,uint32_t ext_id)2782 enum dc_irq_source dc_interrupt_to_irq_source(
2783 		struct dc *dc,
2784 		uint32_t src_id,
2785 		uint32_t ext_id)
2786 {
2787 	return dal_irq_service_to_irq_source(dc->res_pool->irqs, src_id, ext_id);
2788 }
2789 
2790 /**
2791  * dc_interrupt_set() - Enable/disable an AMD hw interrupt source
2792  */
dc_interrupt_set(struct dc * dc,enum dc_irq_source src,bool enable)2793 bool dc_interrupt_set(struct dc *dc, enum dc_irq_source src, bool enable)
2794 {
2795 
2796 	if (dc == NULL)
2797 		return false;
2798 
2799 	return dal_irq_service_set(dc->res_pool->irqs, src, enable);
2800 }
2801 
dc_interrupt_ack(struct dc * dc,enum dc_irq_source src)2802 void dc_interrupt_ack(struct dc *dc, enum dc_irq_source src)
2803 {
2804 	dal_irq_service_ack(dc->res_pool->irqs, src);
2805 }
2806 
dc_power_down_on_boot(struct dc * dc)2807 void dc_power_down_on_boot(struct dc *dc)
2808 {
2809 	if (dc->ctx->dce_environment != DCE_ENV_VIRTUAL_HW &&
2810 			dc->hwss.power_down_on_boot)
2811 		dc->hwss.power_down_on_boot(dc);
2812 }
2813 
dc_set_power_state(struct dc * dc,enum dc_acpi_cm_power_state power_state)2814 void dc_set_power_state(
2815 	struct dc *dc,
2816 	enum dc_acpi_cm_power_state power_state)
2817 {
2818 	struct kref refcount;
2819 	struct display_mode_lib *dml;
2820 
2821 	switch (power_state) {
2822 	case DC_ACPI_CM_POWER_STATE_D0:
2823 		dc_resource_state_construct(dc, dc->current_state);
2824 
2825 		if (dc->ctx->dmub_srv)
2826 			dc_dmub_srv_wait_phy_init(dc->ctx->dmub_srv);
2827 
2828 		dc->hwss.init_hw(dc);
2829 
2830 		if (dc->hwss.init_sys_ctx != NULL &&
2831 			dc->vm_pa_config.valid) {
2832 			dc->hwss.init_sys_ctx(dc->hwseq, dc, &dc->vm_pa_config);
2833 		}
2834 
2835 		break;
2836 	default:
2837 		ASSERT(dc->current_state->stream_count == 0);
2838 		/* Zero out the current context so that on resume we start with
2839 		 * clean state, and dc hw programming optimizations will not
2840 		 * cause any trouble.
2841 		 */
2842 		dml = kzalloc(sizeof(struct display_mode_lib),
2843 				GFP_KERNEL);
2844 
2845 		ASSERT(dml);
2846 		if (!dml)
2847 			return;
2848 
2849 		/* Preserve refcount */
2850 		refcount = dc->current_state->refcount;
2851 		/* Preserve display mode lib */
2852 		memcpy(dml, &dc->current_state->bw_ctx.dml, sizeof(struct display_mode_lib));
2853 
2854 		dc_resource_state_destruct(dc->current_state);
2855 		memset(dc->current_state, 0,
2856 				sizeof(*dc->current_state));
2857 
2858 		dc->current_state->refcount = refcount;
2859 		dc->current_state->bw_ctx.dml = *dml;
2860 
2861 		kfree(dml);
2862 
2863 		break;
2864 	}
2865 }
2866 
dc_resume(struct dc * dc)2867 void dc_resume(struct dc *dc)
2868 {
2869 	uint32_t i;
2870 
2871 	for (i = 0; i < dc->link_count; i++)
2872 		core_link_resume(dc->links[i]);
2873 }
2874 
dc_is_dmcu_initialized(struct dc * dc)2875 bool dc_is_dmcu_initialized(struct dc *dc)
2876 {
2877 	struct dmcu *dmcu = dc->res_pool->dmcu;
2878 
2879 	if (dmcu)
2880 		return dmcu->funcs->is_dmcu_initialized(dmcu);
2881 	return false;
2882 }
2883 
dc_submit_i2c(struct dc * dc,uint32_t link_index,struct i2c_command * cmd)2884 bool dc_submit_i2c(
2885 		struct dc *dc,
2886 		uint32_t link_index,
2887 		struct i2c_command *cmd)
2888 {
2889 
2890 	struct dc_link *link = dc->links[link_index];
2891 	struct ddc_service *ddc = link->ddc;
2892 	return dce_i2c_submit_command(
2893 		dc->res_pool,
2894 		ddc->ddc_pin,
2895 		cmd);
2896 }
2897 
dc_submit_i2c_oem(struct dc * dc,struct i2c_command * cmd)2898 bool dc_submit_i2c_oem(
2899 		struct dc *dc,
2900 		struct i2c_command *cmd)
2901 {
2902 	struct ddc_service *ddc = dc->res_pool->oem_device;
2903 	return dce_i2c_submit_command(
2904 		dc->res_pool,
2905 		ddc->ddc_pin,
2906 		cmd);
2907 }
2908 
link_add_remote_sink_helper(struct dc_link * dc_link,struct dc_sink * sink)2909 static bool link_add_remote_sink_helper(struct dc_link *dc_link, struct dc_sink *sink)
2910 {
2911 	if (dc_link->sink_count >= MAX_SINKS_PER_LINK) {
2912 		BREAK_TO_DEBUGGER();
2913 		return false;
2914 	}
2915 
2916 	dc_sink_retain(sink);
2917 
2918 	dc_link->remote_sinks[dc_link->sink_count] = sink;
2919 	dc_link->sink_count++;
2920 
2921 	return true;
2922 }
2923 
2924 /**
2925  * dc_link_add_remote_sink() - Create a sink and attach it to an existing link
2926  *
2927  * EDID length is in bytes
2928  */
dc_link_add_remote_sink(struct dc_link * link,const uint8_t * edid,int len,struct dc_sink_init_data * init_data)2929 struct dc_sink *dc_link_add_remote_sink(
2930 		struct dc_link *link,
2931 		const uint8_t *edid,
2932 		int len,
2933 		struct dc_sink_init_data *init_data)
2934 {
2935 	struct dc_sink *dc_sink;
2936 	enum dc_edid_status edid_status;
2937 
2938 	if (len > DC_MAX_EDID_BUFFER_SIZE) {
2939 		dm_error("Max EDID buffer size breached!\n");
2940 		return NULL;
2941 	}
2942 
2943 	if (!init_data) {
2944 		BREAK_TO_DEBUGGER();
2945 		return NULL;
2946 	}
2947 
2948 	if (!init_data->link) {
2949 		BREAK_TO_DEBUGGER();
2950 		return NULL;
2951 	}
2952 
2953 	dc_sink = dc_sink_create(init_data);
2954 
2955 	if (!dc_sink)
2956 		return NULL;
2957 
2958 	memmove(dc_sink->dc_edid.raw_edid, edid, len);
2959 	dc_sink->dc_edid.length = len;
2960 
2961 	if (!link_add_remote_sink_helper(
2962 			link,
2963 			dc_sink))
2964 		goto fail_add_sink;
2965 
2966 	edid_status = dm_helpers_parse_edid_caps(
2967 			link->ctx,
2968 			&dc_sink->dc_edid,
2969 			&dc_sink->edid_caps);
2970 
2971 	/*
2972 	 * Treat device as no EDID device if EDID
2973 	 * parsing fails
2974 	 */
2975 	if (edid_status != EDID_OK) {
2976 		dc_sink->dc_edid.length = 0;
2977 		dm_error("Bad EDID, status%d!\n", edid_status);
2978 	}
2979 
2980 	return dc_sink;
2981 
2982 fail_add_sink:
2983 	dc_sink_release(dc_sink);
2984 	return NULL;
2985 }
2986 
2987 /**
2988  * dc_link_remove_remote_sink() - Remove a remote sink from a dc_link
2989  *
2990  * Note that this just removes the struct dc_sink - it doesn't
2991  * program hardware or alter other members of dc_link
2992  */
dc_link_remove_remote_sink(struct dc_link * link,struct dc_sink * sink)2993 void dc_link_remove_remote_sink(struct dc_link *link, struct dc_sink *sink)
2994 {
2995 	int i;
2996 
2997 	if (!link->sink_count) {
2998 		BREAK_TO_DEBUGGER();
2999 		return;
3000 	}
3001 
3002 	for (i = 0; i < link->sink_count; i++) {
3003 		if (link->remote_sinks[i] == sink) {
3004 			dc_sink_release(sink);
3005 			link->remote_sinks[i] = NULL;
3006 
3007 			/* shrink array to remove empty place */
3008 			while (i < link->sink_count - 1) {
3009 				link->remote_sinks[i] = link->remote_sinks[i+1];
3010 				i++;
3011 			}
3012 			link->remote_sinks[i] = NULL;
3013 			link->sink_count--;
3014 			return;
3015 		}
3016 	}
3017 }
3018 
get_clock_requirements_for_state(struct dc_state * state,struct AsicStateEx * info)3019 void get_clock_requirements_for_state(struct dc_state *state, struct AsicStateEx *info)
3020 {
3021 	info->displayClock				= (unsigned int)state->bw_ctx.bw.dcn.clk.dispclk_khz;
3022 	info->engineClock				= (unsigned int)state->bw_ctx.bw.dcn.clk.dcfclk_khz;
3023 	info->memoryClock				= (unsigned int)state->bw_ctx.bw.dcn.clk.dramclk_khz;
3024 	info->maxSupportedDppClock		= (unsigned int)state->bw_ctx.bw.dcn.clk.max_supported_dppclk_khz;
3025 	info->dppClock					= (unsigned int)state->bw_ctx.bw.dcn.clk.dppclk_khz;
3026 	info->socClock					= (unsigned int)state->bw_ctx.bw.dcn.clk.socclk_khz;
3027 	info->dcfClockDeepSleep			= (unsigned int)state->bw_ctx.bw.dcn.clk.dcfclk_deep_sleep_khz;
3028 	info->fClock					= (unsigned int)state->bw_ctx.bw.dcn.clk.fclk_khz;
3029 	info->phyClock					= (unsigned int)state->bw_ctx.bw.dcn.clk.phyclk_khz;
3030 }
dc_set_clock(struct dc * dc,enum dc_clock_type clock_type,uint32_t clk_khz,uint32_t stepping)3031 enum dc_status dc_set_clock(struct dc *dc, enum dc_clock_type clock_type, uint32_t clk_khz, uint32_t stepping)
3032 {
3033 	if (dc->hwss.set_clock)
3034 		return dc->hwss.set_clock(dc, clock_type, clk_khz, stepping);
3035 	return DC_ERROR_UNEXPECTED;
3036 }
dc_get_clock(struct dc * dc,enum dc_clock_type clock_type,struct dc_clock_config * clock_cfg)3037 void dc_get_clock(struct dc *dc, enum dc_clock_type clock_type, struct dc_clock_config *clock_cfg)
3038 {
3039 	if (dc->hwss.get_clock)
3040 		dc->hwss.get_clock(dc, clock_type, clock_cfg);
3041 }
3042 
3043 /* enable/disable eDP PSR without specify stream for eDP */
dc_set_psr_allow_active(struct dc * dc,bool enable)3044 bool dc_set_psr_allow_active(struct dc *dc, bool enable)
3045 {
3046 	int i;
3047 
3048 	for (i = 0; i < dc->current_state->stream_count ; i++) {
3049 		struct dc_link *link;
3050 		struct dc_stream_state *stream = dc->current_state->streams[i];
3051 
3052 		link = stream->link;
3053 		if (!link)
3054 			continue;
3055 
3056 		if (link->psr_settings.psr_feature_enabled) {
3057 			if (enable && !link->psr_settings.psr_allow_active)
3058 				return dc_link_set_psr_allow_active(link, true, false);
3059 			else if (!enable && link->psr_settings.psr_allow_active)
3060 				return dc_link_set_psr_allow_active(link, false, true);
3061 		}
3062 	}
3063 
3064 	return true;
3065 }
3066 
3067 #if defined(CONFIG_DRM_AMD_DC_DCN3_0)
3068 
dc_allow_idle_optimizations(struct dc * dc,bool allow)3069 void dc_allow_idle_optimizations(struct dc *dc, bool allow)
3070 {
3071 	if (dc->debug.disable_idle_power_optimizations)
3072 		return;
3073 
3074 	if (allow == dc->idle_optimizations_allowed)
3075 		return;
3076 
3077 	if (dc->hwss.apply_idle_power_optimizations && dc->hwss.apply_idle_power_optimizations(dc, allow))
3078 		dc->idle_optimizations_allowed = allow;
3079 }
3080 
3081 /*
3082  * blank all streams, and set min and max memory clock to
3083  * lowest and highest DPM level, respectively
3084  */
dc_unlock_memory_clock_frequency(struct dc * dc)3085 void dc_unlock_memory_clock_frequency(struct dc *dc)
3086 {
3087 	unsigned int i;
3088 
3089 	for (i = 0; i < MAX_PIPES; i++)
3090 		if (dc->current_state->res_ctx.pipe_ctx[i].plane_state)
3091 			core_link_disable_stream(&dc->current_state->res_ctx.pipe_ctx[i]);
3092 
3093 	dc->clk_mgr->funcs->set_hard_min_memclk(dc->clk_mgr, false);
3094 	dc->clk_mgr->funcs->set_hard_max_memclk(dc->clk_mgr);
3095 }
3096 
3097 /*
3098  * set min memory clock to the min required for current mode,
3099  * max to maxDPM, and unblank streams
3100  */
dc_lock_memory_clock_frequency(struct dc * dc)3101 void dc_lock_memory_clock_frequency(struct dc *dc)
3102 {
3103 	unsigned int i;
3104 
3105 	dc->clk_mgr->funcs->get_memclk_states_from_smu(dc->clk_mgr);
3106 	dc->clk_mgr->funcs->set_hard_min_memclk(dc->clk_mgr, true);
3107 	dc->clk_mgr->funcs->set_hard_max_memclk(dc->clk_mgr);
3108 
3109 	for (i = 0; i < MAX_PIPES; i++)
3110 		if (dc->current_state->res_ctx.pipe_ctx[i].plane_state)
3111 			core_link_enable_stream(dc->current_state, &dc->current_state->res_ctx.pipe_ctx[i]);
3112 }
3113 
dc_is_plane_eligible_for_idle_optimizaitons(struct dc * dc,struct dc_plane_state * plane)3114 bool dc_is_plane_eligible_for_idle_optimizaitons(struct dc *dc,
3115 						 struct dc_plane_state *plane)
3116 {
3117 	return false;
3118 }
3119 #endif
3120