xref: /linux/drivers/gpu/drm/amd/display/dc/core/dc_stream.c (revision 2c1ed907520c50326b8f604907a8478b27881a2e)
1 /*
2  * Copyright 2012-15 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 
26 #include "dm_services.h"
27 #include "basics/dc_common.h"
28 #include "dc.h"
29 #include "core_types.h"
30 #include "resource.h"
31 #include "ipp.h"
32 #include "timing_generator.h"
33 #include "dc_dmub_srv.h"
34 #include "dc_state_priv.h"
35 #include "dc_stream_priv.h"
36 
37 #define DC_LOGGER dc->ctx->logger
38 #ifndef MIN
39 #define MIN(X, Y) ((X) < (Y) ? (X) : (Y))
40 #endif
41 #ifndef MAX
42 #define MAX(x, y) ((x > y) ? x : y)
43 #endif
44 
45 /*******************************************************************************
46  * Private functions
47  ******************************************************************************/
update_stream_signal(struct dc_stream_state * stream,struct dc_sink * sink)48 void update_stream_signal(struct dc_stream_state *stream, struct dc_sink *sink)
49 {
50 	if (sink->sink_signal == SIGNAL_TYPE_NONE)
51 		stream->signal = stream->link->connector_signal;
52 	else
53 		stream->signal = sink->sink_signal;
54 
55 	if (dc_is_dvi_signal(stream->signal)) {
56 		if (stream->ctx->dc->caps.dual_link_dvi &&
57 			(stream->timing.pix_clk_100hz / 10) > TMDS_MAX_PIXEL_CLOCK &&
58 			sink->sink_signal != SIGNAL_TYPE_DVI_SINGLE_LINK)
59 			stream->signal = SIGNAL_TYPE_DVI_DUAL_LINK;
60 		else
61 			stream->signal = SIGNAL_TYPE_DVI_SINGLE_LINK;
62 	}
63 }
64 
dc_stream_construct(struct dc_stream_state * stream,struct dc_sink * dc_sink_data)65 bool dc_stream_construct(struct dc_stream_state *stream,
66 	struct dc_sink *dc_sink_data)
67 {
68 	uint32_t i = 0;
69 
70 	stream->sink = dc_sink_data;
71 	dc_sink_retain(dc_sink_data);
72 
73 	stream->ctx = dc_sink_data->ctx;
74 	stream->link = dc_sink_data->link;
75 	stream->sink_patches = dc_sink_data->edid_caps.panel_patch;
76 	stream->converter_disable_audio = dc_sink_data->converter_disable_audio;
77 	stream->qs_bit = dc_sink_data->edid_caps.qs_bit;
78 	stream->qy_bit = dc_sink_data->edid_caps.qy_bit;
79 
80 	/* Copy audio modes */
81 	/* TODO - Remove this translation */
82 	for (i = 0; i < (dc_sink_data->edid_caps.audio_mode_count); i++) {
83 		stream->audio_info.modes[i].channel_count = dc_sink_data->edid_caps.audio_modes[i].channel_count;
84 		stream->audio_info.modes[i].format_code = dc_sink_data->edid_caps.audio_modes[i].format_code;
85 		stream->audio_info.modes[i].sample_rates.all = dc_sink_data->edid_caps.audio_modes[i].sample_rate;
86 		stream->audio_info.modes[i].sample_size = dc_sink_data->edid_caps.audio_modes[i].sample_size;
87 	}
88 	stream->audio_info.mode_count = dc_sink_data->edid_caps.audio_mode_count;
89 	stream->audio_info.audio_latency = dc_sink_data->edid_caps.audio_latency;
90 	stream->audio_info.video_latency = dc_sink_data->edid_caps.video_latency;
91 	memmove(
92 		stream->audio_info.display_name,
93 		dc_sink_data->edid_caps.display_name,
94 		AUDIO_INFO_DISPLAY_NAME_SIZE_IN_CHARS);
95 	stream->audio_info.manufacture_id = dc_sink_data->edid_caps.manufacturer_id;
96 	stream->audio_info.product_id = dc_sink_data->edid_caps.product_id;
97 	stream->audio_info.flags.all = dc_sink_data->edid_caps.speaker_flags;
98 
99 	if (dc_sink_data->dc_container_id != NULL) {
100 		struct dc_container_id *dc_container_id = dc_sink_data->dc_container_id;
101 
102 		stream->audio_info.port_id[0] = dc_container_id->portId[0];
103 		stream->audio_info.port_id[1] = dc_container_id->portId[1];
104 	} else {
105 		/* TODO - WindowDM has implemented,
106 		other DMs need Unhardcode port_id */
107 		stream->audio_info.port_id[0] = 0x5558859e;
108 		stream->audio_info.port_id[1] = 0xd989449;
109 	}
110 
111 	/* EDID CAP translation for HDMI 2.0 */
112 	stream->timing.flags.LTE_340MCSC_SCRAMBLE = dc_sink_data->edid_caps.lte_340mcsc_scramble;
113 
114 	memset(&stream->timing.dsc_cfg, 0, sizeof(stream->timing.dsc_cfg));
115 	stream->timing.dsc_cfg.num_slices_h = 0;
116 	stream->timing.dsc_cfg.num_slices_v = 0;
117 	stream->timing.dsc_cfg.bits_per_pixel = 128;
118 	stream->timing.dsc_cfg.block_pred_enable = 1;
119 	stream->timing.dsc_cfg.linebuf_depth = 9;
120 	stream->timing.dsc_cfg.version_minor = 2;
121 	stream->timing.dsc_cfg.ycbcr422_simple = 0;
122 
123 	update_stream_signal(stream, dc_sink_data);
124 
125 	stream->out_transfer_func.type = TF_TYPE_BYPASS;
126 
127 	dc_stream_assign_stream_id(stream);
128 
129 	return true;
130 }
131 
dc_stream_destruct(struct dc_stream_state * stream)132 void dc_stream_destruct(struct dc_stream_state *stream)
133 {
134 	dc_sink_release(stream->sink);
135 }
136 
dc_stream_assign_stream_id(struct dc_stream_state * stream)137 void dc_stream_assign_stream_id(struct dc_stream_state *stream)
138 {
139 	/* MSB is reserved to indicate phantoms */
140 	stream->stream_id = stream->ctx->dc_stream_id_count;
141 	stream->ctx->dc_stream_id_count++;
142 }
143 
dc_stream_retain(struct dc_stream_state * stream)144 void dc_stream_retain(struct dc_stream_state *stream)
145 {
146 	kref_get(&stream->refcount);
147 }
148 
dc_stream_free(struct kref * kref)149 static void dc_stream_free(struct kref *kref)
150 {
151 	struct dc_stream_state *stream = container_of(kref, struct dc_stream_state, refcount);
152 
153 	dc_stream_destruct(stream);
154 	kfree(stream);
155 }
156 
dc_stream_release(struct dc_stream_state * stream)157 void dc_stream_release(struct dc_stream_state *stream)
158 {
159 	if (stream != NULL) {
160 		kref_put(&stream->refcount, dc_stream_free);
161 	}
162 }
163 
dc_create_stream_for_sink(struct dc_sink * sink)164 struct dc_stream_state *dc_create_stream_for_sink(
165 		struct dc_sink *sink)
166 {
167 	struct dc_stream_state *stream;
168 
169 	if (sink == NULL)
170 		return NULL;
171 
172 	stream = kzalloc(sizeof(struct dc_stream_state), GFP_KERNEL);
173 	if (stream == NULL)
174 		goto alloc_fail;
175 
176 	if (dc_stream_construct(stream, sink) == false)
177 		goto construct_fail;
178 
179 	kref_init(&stream->refcount);
180 
181 	return stream;
182 
183 construct_fail:
184 	kfree(stream);
185 
186 alloc_fail:
187 	return NULL;
188 }
189 
dc_copy_stream(const struct dc_stream_state * stream)190 struct dc_stream_state *dc_copy_stream(const struct dc_stream_state *stream)
191 {
192 	struct dc_stream_state *new_stream;
193 
194 	new_stream = kmemdup(stream, sizeof(struct dc_stream_state), GFP_KERNEL);
195 	if (!new_stream)
196 		return NULL;
197 
198 	if (new_stream->sink)
199 		dc_sink_retain(new_stream->sink);
200 
201 	dc_stream_assign_stream_id(new_stream);
202 
203 	/* If using dynamic encoder assignment, wait till stream committed to assign encoder. */
204 	if (new_stream->ctx->dc->res_pool->funcs->link_encs_assign)
205 		new_stream->link_enc = NULL;
206 
207 	kref_init(&new_stream->refcount);
208 
209 	return new_stream;
210 }
211 
212 /**
213  * dc_stream_get_status() - Get current stream status of the given stream state
214  * @stream: The stream to get the stream status for.
215  *
216  * The given stream is expected to exist in dc->current_state. Otherwise, NULL
217  * will be returned.
218  */
dc_stream_get_status(struct dc_stream_state * stream)219 struct dc_stream_status *dc_stream_get_status(
220 	struct dc_stream_state *stream)
221 {
222 	struct dc *dc = stream->ctx->dc;
223 	return dc_state_get_stream_status(dc->current_state, stream);
224 }
225 
program_cursor_attributes(struct dc * dc,struct dc_stream_state * stream)226 void program_cursor_attributes(
227 	struct dc *dc,
228 	struct dc_stream_state *stream)
229 {
230 	int i;
231 	struct resource_context *res_ctx;
232 	struct pipe_ctx *pipe_to_program = NULL;
233 
234 	if (!stream)
235 		return;
236 
237 	res_ctx = &dc->current_state->res_ctx;
238 
239 	for (i = 0; i < MAX_PIPES; i++) {
240 		struct pipe_ctx *pipe_ctx = &res_ctx->pipe_ctx[i];
241 
242 		if (pipe_ctx->stream != stream)
243 			continue;
244 
245 		if (!pipe_to_program) {
246 			pipe_to_program = pipe_ctx;
247 			dc->hwss.cursor_lock(dc, pipe_to_program, true);
248 			if (pipe_to_program->next_odm_pipe)
249 				dc->hwss.cursor_lock(dc, pipe_to_program->next_odm_pipe, true);
250 		}
251 
252 		dc->hwss.set_cursor_attribute(pipe_ctx);
253 		if (dc->ctx->dmub_srv)
254 			dc_send_update_cursor_info_to_dmu(pipe_ctx, i);
255 		if (dc->hwss.set_cursor_sdr_white_level)
256 			dc->hwss.set_cursor_sdr_white_level(pipe_ctx);
257 	}
258 
259 	if (pipe_to_program) {
260 		dc->hwss.cursor_lock(dc, pipe_to_program, false);
261 		if (pipe_to_program->next_odm_pipe)
262 			dc->hwss.cursor_lock(dc, pipe_to_program->next_odm_pipe, false);
263 	}
264 }
265 
266 /*
267  * dc_stream_set_cursor_attributes() - Update cursor attributes and set cursor surface address
268  */
dc_stream_set_cursor_attributes(struct dc_stream_state * stream,const struct dc_cursor_attributes * attributes)269 bool dc_stream_set_cursor_attributes(
270 	struct dc_stream_state *stream,
271 	const struct dc_cursor_attributes *attributes)
272 {
273 	struct dc  *dc;
274 
275 	if (NULL == stream) {
276 		dm_error("DC: dc_stream is NULL!\n");
277 		return false;
278 	}
279 	if (NULL == attributes) {
280 		dm_error("DC: attributes is NULL!\n");
281 		return false;
282 	}
283 
284 	if (attributes->address.quad_part == 0) {
285 		dm_output_to_console("DC: Cursor address is 0!\n");
286 		return false;
287 	}
288 
289 	dc = stream->ctx->dc;
290 
291 	/* SubVP is not compatible with HW cursor larger than 64 x 64 x 4.
292 	 * Therefore, if cursor is greater than 64 x 64 x 4, fallback to SW cursor in the following case:
293 	 * 1. If the config is a candidate for SubVP high refresh (both single an dual display configs)
294 	 * 2. If not subvp high refresh, for single display cases, if resolution is >= 5K and refresh rate < 120hz
295 	 * 3. If not subvp high refresh, for multi display cases, if resolution is >= 4K and refresh rate < 120hz
296 	 */
297 	if (dc->debug.allow_sw_cursor_fallback &&
298 		attributes->height * attributes->width * 4 > 16384 &&
299 		!stream->hw_cursor_req) {
300 		if (check_subvp_sw_cursor_fallback_req(dc, stream))
301 			return false;
302 	}
303 
304 	stream->cursor_attributes = *attributes;
305 
306 	return true;
307 }
308 
dc_stream_program_cursor_attributes(struct dc_stream_state * stream,const struct dc_cursor_attributes * attributes)309 bool dc_stream_program_cursor_attributes(
310 	struct dc_stream_state *stream,
311 	const struct dc_cursor_attributes *attributes)
312 {
313 	struct dc  *dc;
314 	bool reset_idle_optimizations = false;
315 
316 	dc = stream ? stream->ctx->dc : NULL;
317 
318 	if (dc_stream_set_cursor_attributes(stream, attributes)) {
319 		dc_z10_restore(dc);
320 		/* disable idle optimizations while updating cursor */
321 		if (dc->idle_optimizations_allowed) {
322 			dc_allow_idle_optimizations(dc, false);
323 			reset_idle_optimizations = true;
324 		}
325 
326 		program_cursor_attributes(dc, stream);
327 
328 		/* re-enable idle optimizations if necessary */
329 		if (reset_idle_optimizations && !dc->debug.disable_dmub_reallow_idle)
330 			dc_allow_idle_optimizations(dc, true);
331 
332 		return true;
333 	}
334 
335 	return false;
336 }
337 
program_cursor_position(struct dc * dc,struct dc_stream_state * stream)338 void program_cursor_position(
339 	struct dc *dc,
340 	struct dc_stream_state *stream)
341 {
342 	int i;
343 	struct resource_context *res_ctx;
344 	struct pipe_ctx *pipe_to_program = NULL;
345 
346 	if (!stream)
347 		return;
348 
349 	res_ctx = &dc->current_state->res_ctx;
350 
351 	for (i = 0; i < MAX_PIPES; i++) {
352 		struct pipe_ctx *pipe_ctx = &res_ctx->pipe_ctx[i];
353 
354 		if (pipe_ctx->stream != stream ||
355 				(!pipe_ctx->plane_res.mi  && !pipe_ctx->plane_res.hubp) ||
356 				!pipe_ctx->plane_state ||
357 				(!pipe_ctx->plane_res.xfm && !pipe_ctx->plane_res.dpp) ||
358 				(!pipe_ctx->plane_res.ipp && !pipe_ctx->plane_res.dpp))
359 			continue;
360 
361 		if (!pipe_to_program) {
362 			pipe_to_program = pipe_ctx;
363 			dc->hwss.cursor_lock(dc, pipe_to_program, true);
364 		}
365 
366 		dc->hwss.set_cursor_position(pipe_ctx);
367 		if (dc->ctx->dmub_srv)
368 			dc_send_update_cursor_info_to_dmu(pipe_ctx, i);
369 	}
370 
371 	if (pipe_to_program)
372 		dc->hwss.cursor_lock(dc, pipe_to_program, false);
373 }
374 
dc_stream_set_cursor_position(struct dc_stream_state * stream,const struct dc_cursor_position * position)375 bool dc_stream_set_cursor_position(
376 	struct dc_stream_state *stream,
377 	const struct dc_cursor_position *position)
378 {
379 	if (NULL == stream) {
380 		dm_error("DC: dc_stream is NULL!\n");
381 		return false;
382 	}
383 
384 	if (NULL == position) {
385 		dm_error("DC: cursor position is NULL!\n");
386 		return false;
387 	}
388 
389 	stream->cursor_position = *position;
390 
391 
392 	return true;
393 }
394 
dc_stream_program_cursor_position(struct dc_stream_state * stream,const struct dc_cursor_position * position)395 bool dc_stream_program_cursor_position(
396 	struct dc_stream_state *stream,
397 	const struct dc_cursor_position *position)
398 {
399 	struct dc *dc;
400 	bool reset_idle_optimizations = false;
401 	const struct dc_cursor_position *old_position;
402 
403 	if (!stream)
404 		return false;
405 
406 	old_position = &stream->cursor_position;
407 	dc = stream->ctx->dc;
408 
409 	if (dc_stream_set_cursor_position(stream, position)) {
410 		dc_z10_restore(dc);
411 
412 		/* disable idle optimizations if enabling cursor */
413 		if (dc->idle_optimizations_allowed &&
414 		    (!old_position->enable || dc->debug.exit_idle_opt_for_cursor_updates) &&
415 		    position->enable) {
416 			dc_allow_idle_optimizations(dc, false);
417 			reset_idle_optimizations = true;
418 		}
419 
420 		program_cursor_position(dc, stream);
421 		/* re-enable idle optimizations if necessary */
422 		if (reset_idle_optimizations && !dc->debug.disable_dmub_reallow_idle)
423 			dc_allow_idle_optimizations(dc, true);
424 
425 		/* apply/update visual confirm */
426 		if (dc->debug.visual_confirm == VISUAL_CONFIRM_HW_CURSOR) {
427 			/* update software state */
428 			int i;
429 
430 			for (i = 0; i < dc->res_pool->pipe_count; i++) {
431 				struct pipe_ctx *pipe_ctx = &dc->current_state->res_ctx.pipe_ctx[i];
432 
433 				/* adjust visual confirm color for all pipes with current stream */
434 				if (stream == pipe_ctx->stream) {
435 					get_cursor_visual_confirm_color(pipe_ctx, &(pipe_ctx->visual_confirm_color));
436 
437 					/* programming hardware */
438 					if (pipe_ctx->plane_state)
439 						dc->hwss.update_visual_confirm_color(dc, pipe_ctx,
440 								pipe_ctx->plane_res.hubp->mpcc_id);
441 				}
442 			}
443 		}
444 
445 		return true;
446 	}
447 
448 	return false;
449 }
450 
dc_stream_add_writeback(struct dc * dc,struct dc_stream_state * stream,struct dc_writeback_info * wb_info)451 bool dc_stream_add_writeback(struct dc *dc,
452 		struct dc_stream_state *stream,
453 		struct dc_writeback_info *wb_info)
454 {
455 	bool isDrc = false;
456 	int i = 0;
457 	struct dwbc *dwb;
458 
459 	if (stream == NULL) {
460 		dm_error("DC: dc_stream is NULL!\n");
461 		return false;
462 	}
463 
464 	if (wb_info == NULL) {
465 		dm_error("DC: dc_writeback_info is NULL!\n");
466 		return false;
467 	}
468 
469 	if (wb_info->dwb_pipe_inst >= MAX_DWB_PIPES) {
470 		dm_error("DC: writeback pipe is invalid!\n");
471 		return false;
472 	}
473 
474 	dc_exit_ips_for_hw_access(dc);
475 
476 	wb_info->dwb_params.out_transfer_func = &stream->out_transfer_func;
477 
478 	dwb = dc->res_pool->dwbc[wb_info->dwb_pipe_inst];
479 	dwb->dwb_is_drc = false;
480 
481 	/* recalculate and apply DML parameters */
482 
483 	for (i = 0; i < stream->num_wb_info; i++) {
484 		/*dynamic update*/
485 		if (stream->writeback_info[i].wb_enabled &&
486 			stream->writeback_info[i].dwb_pipe_inst == wb_info->dwb_pipe_inst) {
487 			stream->writeback_info[i] = *wb_info;
488 			isDrc = true;
489 		}
490 	}
491 
492 	if (!isDrc) {
493 		ASSERT(stream->num_wb_info + 1 <= MAX_DWB_PIPES);
494 		stream->writeback_info[stream->num_wb_info++] = *wb_info;
495 	}
496 
497 	if (dc->hwss.enable_writeback) {
498 		struct dc_stream_status *stream_status = dc_stream_get_status(stream);
499 		struct dwbc *dwb = dc->res_pool->dwbc[wb_info->dwb_pipe_inst];
500 		if (stream_status)
501 			dwb->otg_inst = stream_status->primary_otg_inst;
502 	}
503 
504 	if (!dc->hwss.update_bandwidth(dc, dc->current_state)) {
505 		dm_error("DC: update_bandwidth failed!\n");
506 		return false;
507 	}
508 
509 	/* enable writeback */
510 	if (dc->hwss.enable_writeback) {
511 		struct dwbc *dwb = dc->res_pool->dwbc[wb_info->dwb_pipe_inst];
512 
513 		if (dwb->funcs->is_enabled(dwb)) {
514 			/* writeback pipe already enabled, only need to update */
515 			dc->hwss.update_writeback(dc, wb_info, dc->current_state);
516 		} else {
517 			/* Enable writeback pipe from scratch*/
518 			dc->hwss.enable_writeback(dc, wb_info, dc->current_state);
519 		}
520 	}
521 
522 	return true;
523 }
524 
dc_stream_fc_disable_writeback(struct dc * dc,struct dc_stream_state * stream,uint32_t dwb_pipe_inst)525 bool dc_stream_fc_disable_writeback(struct dc *dc,
526 		struct dc_stream_state *stream,
527 		uint32_t dwb_pipe_inst)
528 {
529 	struct dwbc *dwb = dc->res_pool->dwbc[dwb_pipe_inst];
530 
531 	if (stream == NULL) {
532 		dm_error("DC: dc_stream is NULL!\n");
533 		return false;
534 	}
535 
536 	if (dwb_pipe_inst >= MAX_DWB_PIPES) {
537 		dm_error("DC: writeback pipe is invalid!\n");
538 		return false;
539 	}
540 
541 	if (stream->num_wb_info > MAX_DWB_PIPES) {
542 		dm_error("DC: num_wb_info is invalid!\n");
543 		return false;
544 	}
545 
546 	dc_exit_ips_for_hw_access(dc);
547 
548 	if (dwb->funcs->set_fc_enable)
549 		dwb->funcs->set_fc_enable(dwb, DWB_FRAME_CAPTURE_DISABLE);
550 
551 	return true;
552 }
553 
dc_stream_remove_writeback(struct dc * dc,struct dc_stream_state * stream,uint32_t dwb_pipe_inst)554 bool dc_stream_remove_writeback(struct dc *dc,
555 		struct dc_stream_state *stream,
556 		uint32_t dwb_pipe_inst)
557 {
558 	unsigned int i, j;
559 	if (stream == NULL) {
560 		dm_error("DC: dc_stream is NULL!\n");
561 		return false;
562 	}
563 
564 	if (dwb_pipe_inst >= MAX_DWB_PIPES) {
565 		dm_error("DC: writeback pipe is invalid!\n");
566 		return false;
567 	}
568 
569 	if (stream->num_wb_info > MAX_DWB_PIPES) {
570 		dm_error("DC: num_wb_info is invalid!\n");
571 		return false;
572 	}
573 
574 	/* remove writeback info for disabled writeback pipes from stream */
575 	for (i = 0, j = 0; i < stream->num_wb_info; i++) {
576 		if (stream->writeback_info[i].wb_enabled) {
577 
578 			if (stream->writeback_info[i].dwb_pipe_inst == dwb_pipe_inst)
579 				stream->writeback_info[i].wb_enabled = false;
580 
581 			/* trim the array */
582 			if (j < i) {
583 				memcpy(&stream->writeback_info[j], &stream->writeback_info[i],
584 						sizeof(struct dc_writeback_info));
585 				j++;
586 			}
587 		}
588 	}
589 	stream->num_wb_info = j;
590 
591 	/* recalculate and apply DML parameters */
592 	if (!dc->hwss.update_bandwidth(dc, dc->current_state)) {
593 		dm_error("DC: update_bandwidth failed!\n");
594 		return false;
595 	}
596 
597 	dc_exit_ips_for_hw_access(dc);
598 
599 	/* disable writeback */
600 	if (dc->hwss.disable_writeback) {
601 		struct dwbc *dwb = dc->res_pool->dwbc[dwb_pipe_inst];
602 
603 		if (dwb->funcs->is_enabled(dwb))
604 			dc->hwss.disable_writeback(dc, dwb_pipe_inst);
605 	}
606 
607 	return true;
608 }
609 
dc_stream_get_vblank_counter(const struct dc_stream_state * stream)610 uint32_t dc_stream_get_vblank_counter(const struct dc_stream_state *stream)
611 {
612 	uint8_t i;
613 	struct dc  *dc = stream->ctx->dc;
614 	struct resource_context *res_ctx =
615 		&dc->current_state->res_ctx;
616 
617 	dc_exit_ips_for_hw_access(dc);
618 
619 	for (i = 0; i < MAX_PIPES; i++) {
620 		struct timing_generator *tg = res_ctx->pipe_ctx[i].stream_res.tg;
621 
622 		if (res_ctx->pipe_ctx[i].stream != stream || !tg)
623 			continue;
624 
625 		return tg->funcs->get_frame_count(tg);
626 	}
627 
628 	return 0;
629 }
630 
dc_stream_send_dp_sdp(const struct dc_stream_state * stream,const uint8_t * custom_sdp_message,unsigned int sdp_message_size)631 bool dc_stream_send_dp_sdp(const struct dc_stream_state *stream,
632 		const uint8_t *custom_sdp_message,
633 		unsigned int sdp_message_size)
634 {
635 	int i;
636 	struct dc  *dc;
637 	struct resource_context *res_ctx;
638 
639 	if (stream == NULL) {
640 		dm_error("DC: dc_stream is NULL!\n");
641 		return false;
642 	}
643 
644 	dc = stream->ctx->dc;
645 	res_ctx = &dc->current_state->res_ctx;
646 
647 	dc_exit_ips_for_hw_access(dc);
648 
649 	for (i = 0; i < MAX_PIPES; i++) {
650 		struct pipe_ctx *pipe_ctx = &res_ctx->pipe_ctx[i];
651 
652 		if (pipe_ctx->stream != stream)
653 			continue;
654 
655 		if (dc->hwss.send_immediate_sdp_message != NULL)
656 			dc->hwss.send_immediate_sdp_message(pipe_ctx,
657 								custom_sdp_message,
658 								sdp_message_size);
659 		else
660 			DC_LOG_WARNING("%s:send_immediate_sdp_message not implemented on this ASIC\n",
661 			__func__);
662 
663 	}
664 
665 	return true;
666 }
667 
dc_stream_get_scanoutpos(const struct dc_stream_state * stream,uint32_t * v_blank_start,uint32_t * v_blank_end,uint32_t * h_position,uint32_t * v_position)668 bool dc_stream_get_scanoutpos(const struct dc_stream_state *stream,
669 				  uint32_t *v_blank_start,
670 				  uint32_t *v_blank_end,
671 				  uint32_t *h_position,
672 				  uint32_t *v_position)
673 {
674 	uint8_t i;
675 	bool ret = false;
676 	struct dc  *dc = stream->ctx->dc;
677 	struct resource_context *res_ctx =
678 		&dc->current_state->res_ctx;
679 
680 	dc_exit_ips_for_hw_access(dc);
681 
682 	for (i = 0; i < MAX_PIPES; i++) {
683 		struct timing_generator *tg = res_ctx->pipe_ctx[i].stream_res.tg;
684 
685 		if (res_ctx->pipe_ctx[i].stream != stream || !tg)
686 			continue;
687 
688 		tg->funcs->get_scanoutpos(tg,
689 					  v_blank_start,
690 					  v_blank_end,
691 					  h_position,
692 					  v_position);
693 
694 		ret = true;
695 		break;
696 	}
697 
698 	return ret;
699 }
700 
dc_stream_dmdata_status_done(struct dc * dc,struct dc_stream_state * stream)701 bool dc_stream_dmdata_status_done(struct dc *dc, struct dc_stream_state *stream)
702 {
703 	struct pipe_ctx *pipe = NULL;
704 	int i;
705 
706 	if (!dc->hwss.dmdata_status_done)
707 		return false;
708 
709 	for (i = 0; i < MAX_PIPES; i++) {
710 		pipe = &dc->current_state->res_ctx.pipe_ctx[i];
711 		if (pipe->stream == stream)
712 			break;
713 	}
714 	/* Stream not found, by default we'll assume HUBP fetched dm data */
715 	if (i == MAX_PIPES)
716 		return true;
717 
718 	dc_exit_ips_for_hw_access(dc);
719 
720 	return dc->hwss.dmdata_status_done(pipe);
721 }
722 
dc_stream_set_dynamic_metadata(struct dc * dc,struct dc_stream_state * stream,struct dc_dmdata_attributes * attr)723 bool dc_stream_set_dynamic_metadata(struct dc *dc,
724 		struct dc_stream_state *stream,
725 		struct dc_dmdata_attributes *attr)
726 {
727 	struct pipe_ctx *pipe_ctx = NULL;
728 	struct hubp *hubp;
729 	int i;
730 
731 	/* Dynamic metadata is only supported on HDMI or DP */
732 	if (!dc_is_hdmi_signal(stream->signal) && !dc_is_dp_signal(stream->signal))
733 		return false;
734 
735 	/* Check hardware support */
736 	if (!dc->hwss.program_dmdata_engine)
737 		return false;
738 
739 	for (i = 0; i < MAX_PIPES; i++) {
740 		pipe_ctx = &dc->current_state->res_ctx.pipe_ctx[i];
741 		if (pipe_ctx->stream == stream)
742 			break;
743 	}
744 
745 	if (i == MAX_PIPES)
746 		return false;
747 
748 	hubp = pipe_ctx->plane_res.hubp;
749 	if (hubp == NULL)
750 		return false;
751 
752 	pipe_ctx->stream->dmdata_address = attr->address;
753 
754 	dc_exit_ips_for_hw_access(dc);
755 
756 	dc->hwss.program_dmdata_engine(pipe_ctx);
757 
758 	if (hubp->funcs->dmdata_set_attributes != NULL &&
759 			pipe_ctx->stream->dmdata_address.quad_part != 0) {
760 		hubp->funcs->dmdata_set_attributes(hubp, attr);
761 	}
762 
763 	return true;
764 }
765 
dc_stream_add_dsc_to_resource(struct dc * dc,struct dc_state * state,struct dc_stream_state * stream)766 enum dc_status dc_stream_add_dsc_to_resource(struct dc *dc,
767 		struct dc_state *state,
768 		struct dc_stream_state *stream)
769 {
770 	if (dc->res_pool->funcs->add_dsc_to_stream_resource) {
771 		return dc->res_pool->funcs->add_dsc_to_stream_resource(dc, state, stream);
772 	} else {
773 		return DC_NO_DSC_RESOURCE;
774 	}
775 }
776 
dc_stream_get_pipe_ctx(struct dc_stream_state * stream)777 struct pipe_ctx *dc_stream_get_pipe_ctx(struct dc_stream_state *stream)
778 {
779 	int i = 0;
780 
781 	for (i = 0; i < MAX_PIPES; i++) {
782 		struct pipe_ctx *pipe = &stream->ctx->dc->current_state->res_ctx.pipe_ctx[i];
783 
784 		if (pipe->stream == stream)
785 			return pipe;
786 	}
787 
788 	return NULL;
789 }
790 
dc_stream_log(const struct dc * dc,const struct dc_stream_state * stream)791 void dc_stream_log(const struct dc *dc, const struct dc_stream_state *stream)
792 {
793 	DC_LOG_DC(
794 			"core_stream 0x%p: src: %d, %d, %d, %d; dst: %d, %d, %d, %d, colorSpace:%d\n",
795 			stream,
796 			stream->src.x,
797 			stream->src.y,
798 			stream->src.width,
799 			stream->src.height,
800 			stream->dst.x,
801 			stream->dst.y,
802 			stream->dst.width,
803 			stream->dst.height,
804 			stream->output_color_space);
805 	DC_LOG_DC(
806 			"\tpix_clk_khz: %d, h_total: %d, v_total: %d, pixel_encoding:%s, color_depth:%s\n",
807 			stream->timing.pix_clk_100hz / 10,
808 			stream->timing.h_total,
809 			stream->timing.v_total,
810 			dc_pixel_encoding_to_str(stream->timing.pixel_encoding),
811 			dc_color_depth_to_str(stream->timing.display_color_depth));
812 	DC_LOG_DC(
813 			"\tlink: %d\n",
814 			stream->link->link_index);
815 
816 	DC_LOG_DC(
817 			"\tdsc: %d, mst_pbn: %d\n",
818 			stream->timing.flags.DSC,
819 			stream->timing.dsc_cfg.mst_pbn);
820 
821 	if (stream->sink) {
822 		if (stream->sink->sink_signal != SIGNAL_TYPE_VIRTUAL &&
823 			stream->sink->sink_signal != SIGNAL_TYPE_NONE) {
824 
825 			DC_LOG_DC(
826 					"\tdispname: %s signal: %x\n",
827 					stream->sink->edid_caps.display_name,
828 					stream->signal);
829 		}
830 	}
831 }
832 
833 /*
834  * Finds the greatest index in refresh_rate_hz that contains a value <= refresh
835  */
dc_stream_get_nearest_smallest_index(struct dc_stream_state * stream,int refresh)836 static int dc_stream_get_nearest_smallest_index(struct dc_stream_state *stream, int refresh)
837 {
838 	for (int i = 0; i < (LUMINANCE_DATA_TABLE_SIZE - 1); ++i) {
839 		if ((stream->lumin_data.refresh_rate_hz[i] <= refresh) && (refresh < stream->lumin_data.refresh_rate_hz[i + 1])) {
840 			return i;
841 		}
842 	}
843 	return 9;
844 }
845 
846 /*
847  * Finds a corresponding brightness for a given refresh rate between 2 given indices, where index1 < index2
848  */
dc_stream_get_brightness_millinits_linear_interpolation(struct dc_stream_state * stream,int index1,int index2,int refresh_hz)849 static int dc_stream_get_brightness_millinits_linear_interpolation (struct dc_stream_state *stream,
850 								     int index1,
851 								     int index2,
852 								     int refresh_hz)
853 {
854 	long long slope = 0;
855 	if (stream->lumin_data.refresh_rate_hz[index2] != stream->lumin_data.refresh_rate_hz[index1]) {
856 		slope = (stream->lumin_data.luminance_millinits[index2] - stream->lumin_data.luminance_millinits[index1]) /
857 			    (stream->lumin_data.refresh_rate_hz[index2] - stream->lumin_data.refresh_rate_hz[index1]);
858 	}
859 
860 	int y_intercept = stream->lumin_data.luminance_millinits[index2] - slope * stream->lumin_data.refresh_rate_hz[index2];
861 
862 	return (y_intercept + refresh_hz * slope);
863 }
864 
865 /*
866  * Finds a corresponding refresh rate for a given brightness between 2 given indices, where index1 < index2
867  */
dc_stream_get_refresh_hz_linear_interpolation(struct dc_stream_state * stream,int index1,int index2,int brightness_millinits)868 static int dc_stream_get_refresh_hz_linear_interpolation (struct dc_stream_state *stream,
869 							   int index1,
870 							   int index2,
871 							   int brightness_millinits)
872 {
873 	long long slope = 1;
874 	if (stream->lumin_data.refresh_rate_hz[index2] != stream->lumin_data.refresh_rate_hz[index1]) {
875 		slope = (stream->lumin_data.luminance_millinits[index2] - stream->lumin_data.luminance_millinits[index1]) /
876 				(stream->lumin_data.refresh_rate_hz[index2] - stream->lumin_data.refresh_rate_hz[index1]);
877 	}
878 
879 	int y_intercept = stream->lumin_data.luminance_millinits[index2] - slope * stream->lumin_data.refresh_rate_hz[index2];
880 
881 	return ((int)div64_s64((brightness_millinits - y_intercept), slope));
882 }
883 
884 /*
885  * Finds the current brightness in millinits given a refresh rate
886  */
dc_stream_get_brightness_millinits_from_refresh(struct dc_stream_state * stream,int refresh_hz)887 static int dc_stream_get_brightness_millinits_from_refresh (struct dc_stream_state *stream, int refresh_hz)
888 {
889 	int nearest_smallest_index = dc_stream_get_nearest_smallest_index(stream, refresh_hz);
890 	int nearest_smallest_value = stream->lumin_data.refresh_rate_hz[nearest_smallest_index];
891 
892 	if (nearest_smallest_value == refresh_hz)
893 		return stream->lumin_data.luminance_millinits[nearest_smallest_index];
894 
895 	if (nearest_smallest_index >= 9)
896 		return dc_stream_get_brightness_millinits_linear_interpolation(stream, nearest_smallest_index - 1, nearest_smallest_index, refresh_hz);
897 
898 	if (nearest_smallest_value == stream->lumin_data.refresh_rate_hz[nearest_smallest_index + 1])
899 		return stream->lumin_data.luminance_millinits[nearest_smallest_index];
900 
901 	return dc_stream_get_brightness_millinits_linear_interpolation(stream, nearest_smallest_index, nearest_smallest_index + 1, refresh_hz);
902 }
903 
904 /*
905  * Finds the lowest/highest refresh rate (depending on search_for_max_increase)
906  * that can be achieved from starting_refresh_hz while staying
907  * within flicker criteria
908  */
dc_stream_calculate_flickerless_refresh_rate(struct dc_stream_state * stream,int current_brightness,int starting_refresh_hz,bool is_gaming,bool search_for_max_increase)909 static int dc_stream_calculate_flickerless_refresh_rate(struct dc_stream_state *stream,
910 							 int current_brightness,
911 							 int starting_refresh_hz,
912 							 bool is_gaming,
913 							 bool search_for_max_increase)
914 {
915 	int nearest_smallest_index = dc_stream_get_nearest_smallest_index(stream, starting_refresh_hz);
916 
917 	int flicker_criteria_millinits = is_gaming ?
918 					 stream->lumin_data.flicker_criteria_milli_nits_GAMING :
919 					 stream->lumin_data.flicker_criteria_milli_nits_STATIC;
920 
921 	int safe_upper_bound = current_brightness + flicker_criteria_millinits;
922 	int safe_lower_bound = current_brightness - flicker_criteria_millinits;
923 	int lumin_millinits_temp = 0;
924 
925 	int offset = -1;
926 	if (search_for_max_increase) {
927 		offset = 1;
928 	}
929 
930 	/*
931 	 * Increments up or down by 1 depending on search_for_max_increase
932 	 */
933 	for (int i = nearest_smallest_index; (i > 0 && !search_for_max_increase) || (i < (LUMINANCE_DATA_TABLE_SIZE - 1) && search_for_max_increase); i += offset) {
934 
935 		lumin_millinits_temp = stream->lumin_data.luminance_millinits[i + offset];
936 
937 		if ((lumin_millinits_temp >= safe_upper_bound) || (lumin_millinits_temp <= safe_lower_bound)) {
938 
939 			if (stream->lumin_data.refresh_rate_hz[i + offset] == stream->lumin_data.refresh_rate_hz[i])
940 				return stream->lumin_data.refresh_rate_hz[i];
941 
942 			int target_brightness = (stream->lumin_data.luminance_millinits[i + offset] >= (current_brightness + flicker_criteria_millinits)) ?
943 											current_brightness + flicker_criteria_millinits :
944 											current_brightness - flicker_criteria_millinits;
945 
946 			int refresh = 0;
947 
948 			/*
949 			 * Need the second input to be < third input for dc_stream_get_refresh_hz_linear_interpolation
950 			 */
951 			if (search_for_max_increase)
952 				refresh = dc_stream_get_refresh_hz_linear_interpolation(stream, i, i + offset, target_brightness);
953 			else
954 				refresh = dc_stream_get_refresh_hz_linear_interpolation(stream, i + offset, i, target_brightness);
955 
956 			if (refresh == stream->lumin_data.refresh_rate_hz[i + offset])
957 				return stream->lumin_data.refresh_rate_hz[i + offset];
958 
959 			return refresh;
960 		}
961 	}
962 
963 	if (search_for_max_increase)
964 		return (int)div64_s64((long long)stream->timing.pix_clk_100hz*100, stream->timing.v_total*(long long)stream->timing.h_total);
965 	else
966 		return stream->lumin_data.refresh_rate_hz[0];
967 }
968 
969 /*
970  * Gets the max delta luminance within a specified refresh range
971  */
dc_stream_get_max_delta_lumin_millinits(struct dc_stream_state * stream,int hz1,int hz2,bool isGaming)972 static int dc_stream_get_max_delta_lumin_millinits(struct dc_stream_state *stream, int hz1, int hz2, bool isGaming)
973 {
974 	int lower_refresh_brightness = dc_stream_get_brightness_millinits_from_refresh (stream, hz1);
975 	int higher_refresh_brightness = dc_stream_get_brightness_millinits_from_refresh (stream, hz2);
976 
977 	int min = lower_refresh_brightness;
978 	int max = higher_refresh_brightness;
979 
980 	/*
981 	 * Static screen, therefore no need to scan through array
982 	 */
983 	if (!isGaming) {
984 		if (lower_refresh_brightness >= higher_refresh_brightness) {
985 			return lower_refresh_brightness - higher_refresh_brightness;
986 		}
987 		return higher_refresh_brightness - lower_refresh_brightness;
988 	}
989 
990 	min = MIN(lower_refresh_brightness, higher_refresh_brightness);
991 	max = MAX(lower_refresh_brightness, higher_refresh_brightness);
992 
993 	int nearest_smallest_index = dc_stream_get_nearest_smallest_index(stream, hz1);
994 
995 	for (; nearest_smallest_index < (LUMINANCE_DATA_TABLE_SIZE - 1) &&
996 			stream->lumin_data.refresh_rate_hz[nearest_smallest_index + 1] <= hz2 ; nearest_smallest_index++) {
997 		min = MIN(min, stream->lumin_data.luminance_millinits[nearest_smallest_index + 1]);
998 		max = MAX(max, stream->lumin_data.luminance_millinits[nearest_smallest_index + 1]);
999 	}
1000 
1001 	return (max - min);
1002 }
1003 
1004 /*
1005  * Determines the max flickerless instant vtotal delta for a stream.
1006  * Determines vtotal increase/decrease based on the bool "increase"
1007  */
dc_stream_get_max_flickerless_instant_vtotal_delta(struct dc_stream_state * stream,bool is_gaming,bool increase)1008 static unsigned int dc_stream_get_max_flickerless_instant_vtotal_delta(struct dc_stream_state *stream, bool is_gaming, bool increase)
1009 {
1010 	if (stream->timing.v_total * stream->timing.h_total == 0)
1011 		return 0;
1012 
1013 	int current_refresh_hz = (int)div64_s64((long long)stream->timing.pix_clk_100hz*100, stream->timing.v_total*(long long)stream->timing.h_total);
1014 
1015 	int safe_refresh_hz = dc_stream_calculate_flickerless_refresh_rate(stream,
1016 							 dc_stream_get_brightness_millinits_from_refresh(stream, current_refresh_hz),
1017 							 current_refresh_hz,
1018 							 is_gaming,
1019 							 increase);
1020 
1021 	int safe_refresh_v_total = (int)div64_s64((long long)stream->timing.pix_clk_100hz*100, safe_refresh_hz*(long long)stream->timing.h_total);
1022 
1023 	if (increase)
1024 		return (((int) stream->timing.v_total - safe_refresh_v_total) >= 0) ? (stream->timing.v_total - safe_refresh_v_total) : 0;
1025 
1026 	return ((safe_refresh_v_total - (int) stream->timing.v_total) >= 0) ? (safe_refresh_v_total - stream->timing.v_total) : 0;
1027 }
1028 
1029 /*
1030  * Finds the highest refresh rate that can be achieved
1031  * from starting_refresh_hz while staying within flicker criteria
1032  */
dc_stream_calculate_max_flickerless_refresh_rate(struct dc_stream_state * stream,int starting_refresh_hz,bool is_gaming)1033 int dc_stream_calculate_max_flickerless_refresh_rate(struct dc_stream_state *stream, int starting_refresh_hz, bool is_gaming)
1034 {
1035 	if (!stream->lumin_data.is_valid)
1036 		return 0;
1037 
1038 	int current_brightness = dc_stream_get_brightness_millinits_from_refresh(stream, starting_refresh_hz);
1039 
1040 	return dc_stream_calculate_flickerless_refresh_rate(stream,
1041 							    current_brightness,
1042 							    starting_refresh_hz,
1043 							    is_gaming,
1044 							    true);
1045 }
1046 
1047 /*
1048  * Finds the lowest refresh rate that can be achieved
1049  * from starting_refresh_hz while staying within flicker criteria
1050  */
dc_stream_calculate_min_flickerless_refresh_rate(struct dc_stream_state * stream,int starting_refresh_hz,bool is_gaming)1051 int dc_stream_calculate_min_flickerless_refresh_rate(struct dc_stream_state *stream, int starting_refresh_hz, bool is_gaming)
1052 {
1053 	if (!stream->lumin_data.is_valid)
1054 			return 0;
1055 
1056 	int current_brightness = dc_stream_get_brightness_millinits_from_refresh(stream, starting_refresh_hz);
1057 
1058 	return dc_stream_calculate_flickerless_refresh_rate(stream,
1059 							    current_brightness,
1060 							    starting_refresh_hz,
1061 							    is_gaming,
1062 							    false);
1063 }
1064 
1065 /*
1066  * Determines if there will be a flicker when moving between 2 refresh rates
1067  */
dc_stream_is_refresh_rate_range_flickerless(struct dc_stream_state * stream,int hz1,int hz2,bool is_gaming)1068 bool dc_stream_is_refresh_rate_range_flickerless(struct dc_stream_state *stream, int hz1, int hz2, bool is_gaming)
1069 {
1070 
1071 	/*
1072 	 * Assume that we wont flicker if there is invalid data
1073 	 */
1074 	if (!stream->lumin_data.is_valid)
1075 		return false;
1076 
1077 	int dl = dc_stream_get_max_delta_lumin_millinits(stream, hz1, hz2, is_gaming);
1078 
1079 	int flicker_criteria_millinits = (is_gaming) ?
1080 					  stream->lumin_data.flicker_criteria_milli_nits_GAMING :
1081 					  stream->lumin_data.flicker_criteria_milli_nits_STATIC;
1082 
1083 	return (dl <= flicker_criteria_millinits);
1084 }
1085 
1086 /*
1087  * Determines the max instant vtotal delta increase that can be applied without
1088  * flickering for a given stream
1089  */
dc_stream_get_max_flickerless_instant_vtotal_decrease(struct dc_stream_state * stream,bool is_gaming)1090 unsigned int dc_stream_get_max_flickerless_instant_vtotal_decrease(struct dc_stream_state *stream,
1091 									  bool is_gaming)
1092 {
1093 	if (!stream->lumin_data.is_valid)
1094 		return 0;
1095 
1096 	return dc_stream_get_max_flickerless_instant_vtotal_delta(stream, is_gaming, true);
1097 }
1098 
1099 /*
1100  * Determines the max instant vtotal delta decrease that can be applied without
1101  * flickering for a given stream
1102  */
dc_stream_get_max_flickerless_instant_vtotal_increase(struct dc_stream_state * stream,bool is_gaming)1103 unsigned int dc_stream_get_max_flickerless_instant_vtotal_increase(struct dc_stream_state *stream,
1104 									  bool is_gaming)
1105 {
1106 	if (!stream->lumin_data.is_valid)
1107 		return 0;
1108 
1109 	return dc_stream_get_max_flickerless_instant_vtotal_delta(stream, is_gaming, false);
1110 }
1111