xref: /linux/drivers/gpu/drm/amd/display/dc/core/dc_stream.c (revision e538109ac71d801d26776af5f3c54f548296c29c)
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->ctx->dc->config.unify_link_enc_assignment)
206 		new_stream->link_enc = NULL;
207 
208 	kref_init(&new_stream->refcount);
209 
210 	return new_stream;
211 }
212 
213 /**
214  * dc_stream_get_status() - Get current stream status of the given stream state
215  * @stream: The stream to get the stream status for.
216  *
217  * The given stream is expected to exist in dc->current_state. Otherwise, NULL
218  * will be returned.
219  */
dc_stream_get_status(struct dc_stream_state * stream)220 struct dc_stream_status *dc_stream_get_status(
221 	struct dc_stream_state *stream)
222 {
223 	struct dc *dc = stream->ctx->dc;
224 	return dc_state_get_stream_status(dc->current_state, stream);
225 }
226 
program_cursor_attributes(struct dc * dc,struct dc_stream_state * stream)227 void program_cursor_attributes(
228 	struct dc *dc,
229 	struct dc_stream_state *stream)
230 {
231 	int i;
232 	struct resource_context *res_ctx;
233 	struct pipe_ctx *pipe_to_program = NULL;
234 
235 	if (!stream)
236 		return;
237 
238 	res_ctx = &dc->current_state->res_ctx;
239 
240 	for (i = 0; i < MAX_PIPES; i++) {
241 		struct pipe_ctx *pipe_ctx = &res_ctx->pipe_ctx[i];
242 
243 		if (pipe_ctx->stream != stream)
244 			continue;
245 
246 		if (!pipe_to_program) {
247 			pipe_to_program = pipe_ctx;
248 			dc->hwss.cursor_lock(dc, pipe_to_program, true);
249 			if (pipe_to_program->next_odm_pipe)
250 				dc->hwss.cursor_lock(dc, pipe_to_program->next_odm_pipe, true);
251 		}
252 
253 		dc->hwss.set_cursor_attribute(pipe_ctx);
254 		if (dc->ctx->dmub_srv)
255 			dc_send_update_cursor_info_to_dmu(pipe_ctx, i);
256 		if (dc->hwss.set_cursor_sdr_white_level)
257 			dc->hwss.set_cursor_sdr_white_level(pipe_ctx);
258 	}
259 
260 	if (pipe_to_program) {
261 		dc->hwss.cursor_lock(dc, pipe_to_program, false);
262 		if (pipe_to_program->next_odm_pipe)
263 			dc->hwss.cursor_lock(dc, pipe_to_program->next_odm_pipe, false);
264 	}
265 }
266 
267 /*
268  * dc_stream_check_cursor_attributes() - Check validitity of cursor attributes and surface address
269  */
dc_stream_check_cursor_attributes(const struct dc_stream_state * stream,struct dc_state * state,const struct dc_cursor_attributes * attributes)270 bool dc_stream_check_cursor_attributes(
271 	const struct dc_stream_state *stream,
272 	struct dc_state *state,
273 	const struct dc_cursor_attributes *attributes)
274 {
275 	const struct dc *dc;
276 
277 	unsigned int max_cursor_size;
278 
279 	if (NULL == stream) {
280 		dm_error("DC: dc_stream is NULL!\n");
281 		return false;
282 	}
283 	if (NULL == attributes) {
284 		dm_error("DC: attributes is NULL!\n");
285 		return false;
286 	}
287 
288 	if (attributes->address.quad_part == 0) {
289 		dm_output_to_console("DC: Cursor address is 0!\n");
290 		return false;
291 	}
292 
293 	dc = stream->ctx->dc;
294 
295 	/* SubVP is not compatible with HW cursor larger than what can fit in cursor SRAM.
296 	 * Therefore, if cursor is greater than this, fallback to SW cursor.
297 	 */
298 	if (dc->debug.allow_sw_cursor_fallback && dc->res_pool->funcs->get_max_hw_cursor_size) {
299 		max_cursor_size = dc->res_pool->funcs->get_max_hw_cursor_size(dc, state, stream);
300 		max_cursor_size = max_cursor_size * max_cursor_size * 4;
301 
302 		if (attributes->height * attributes->width * 4 > max_cursor_size) {
303 			return false;
304 		}
305 	}
306 
307 	return true;
308 }
309 
310 /*
311  * dc_stream_set_cursor_attributes() - Update cursor attributes and set cursor surface address
312  */
dc_stream_set_cursor_attributes(struct dc_stream_state * stream,const struct dc_cursor_attributes * attributes)313 bool dc_stream_set_cursor_attributes(
314 	struct dc_stream_state *stream,
315 	const struct dc_cursor_attributes *attributes)
316 {
317 	bool result = false;
318 
319 	if (!stream)
320 		return false;
321 
322 	if (dc_stream_check_cursor_attributes(stream, stream->ctx->dc->current_state, attributes)) {
323 		stream->cursor_attributes = *attributes;
324 		result = true;
325 	}
326 
327 	return result;
328 }
329 
dc_stream_program_cursor_attributes(struct dc_stream_state * stream,const struct dc_cursor_attributes * attributes)330 bool dc_stream_program_cursor_attributes(
331 	struct dc_stream_state *stream,
332 	const struct dc_cursor_attributes *attributes)
333 {
334 	struct dc  *dc;
335 	bool reset_idle_optimizations = false;
336 
337 	if (!stream)
338 		return false;
339 
340 	dc = stream->ctx->dc;
341 
342 	if (dc_stream_set_cursor_attributes(stream, attributes)) {
343 		dc_z10_restore(dc);
344 		/* disable idle optimizations while updating cursor */
345 		if (dc->idle_optimizations_allowed) {
346 			dc_allow_idle_optimizations(dc, false);
347 			reset_idle_optimizations = true;
348 		}
349 
350 		program_cursor_attributes(dc, stream);
351 
352 		/* re-enable idle optimizations if necessary */
353 		if (reset_idle_optimizations && !dc->debug.disable_dmub_reallow_idle)
354 			dc_allow_idle_optimizations(dc, true);
355 
356 		return true;
357 	}
358 
359 	return false;
360 }
361 
program_cursor_position(struct dc * dc,struct dc_stream_state * stream)362 void program_cursor_position(
363 	struct dc *dc,
364 	struct dc_stream_state *stream)
365 {
366 	int i;
367 	struct resource_context *res_ctx;
368 	struct pipe_ctx *pipe_to_program = NULL;
369 
370 	if (!stream)
371 		return;
372 
373 	res_ctx = &dc->current_state->res_ctx;
374 
375 	for (i = 0; i < MAX_PIPES; i++) {
376 		struct pipe_ctx *pipe_ctx = &res_ctx->pipe_ctx[i];
377 
378 		if (pipe_ctx->stream != stream ||
379 				(!pipe_ctx->plane_res.mi  && !pipe_ctx->plane_res.hubp) ||
380 				!pipe_ctx->plane_state ||
381 				(!pipe_ctx->plane_res.xfm && !pipe_ctx->plane_res.dpp) ||
382 				(!pipe_ctx->plane_res.ipp && !pipe_ctx->plane_res.dpp))
383 			continue;
384 
385 		if (!pipe_to_program) {
386 			pipe_to_program = pipe_ctx;
387 			dc->hwss.cursor_lock(dc, pipe_to_program, true);
388 		}
389 
390 		dc->hwss.set_cursor_position(pipe_ctx);
391 		if (dc->ctx->dmub_srv)
392 			dc_send_update_cursor_info_to_dmu(pipe_ctx, i);
393 	}
394 
395 	if (pipe_to_program)
396 		dc->hwss.cursor_lock(dc, pipe_to_program, false);
397 }
398 
dc_stream_set_cursor_position(struct dc_stream_state * stream,const struct dc_cursor_position * position)399 bool dc_stream_set_cursor_position(
400 	struct dc_stream_state *stream,
401 	const struct dc_cursor_position *position)
402 {
403 	if (NULL == stream) {
404 		dm_error("DC: dc_stream is NULL!\n");
405 		return false;
406 	}
407 
408 	if (NULL == position) {
409 		dm_error("DC: cursor position is NULL!\n");
410 		return false;
411 	}
412 
413 	stream->cursor_position = *position;
414 
415 
416 	return true;
417 }
418 
dc_stream_program_cursor_position(struct dc_stream_state * stream,const struct dc_cursor_position * position)419 bool dc_stream_program_cursor_position(
420 	struct dc_stream_state *stream,
421 	const struct dc_cursor_position *position)
422 {
423 	struct dc *dc;
424 	bool reset_idle_optimizations = false;
425 	const struct dc_cursor_position *old_position;
426 
427 	if (!stream)
428 		return false;
429 
430 	old_position = &stream->cursor_position;
431 	dc = stream->ctx->dc;
432 
433 	if (dc_stream_set_cursor_position(stream, position)) {
434 		dc_z10_restore(dc);
435 
436 		/* disable idle optimizations if enabling cursor */
437 		if (dc->idle_optimizations_allowed &&
438 		    (!old_position->enable || dc->debug.exit_idle_opt_for_cursor_updates) &&
439 		    position->enable) {
440 			dc_allow_idle_optimizations(dc, false);
441 			reset_idle_optimizations = true;
442 		}
443 
444 		program_cursor_position(dc, stream);
445 		/* re-enable idle optimizations if necessary */
446 		if (reset_idle_optimizations && !dc->debug.disable_dmub_reallow_idle)
447 			dc_allow_idle_optimizations(dc, true);
448 
449 		/* apply/update visual confirm */
450 		if (dc->debug.visual_confirm == VISUAL_CONFIRM_HW_CURSOR) {
451 			/* update software state */
452 			int i;
453 
454 			for (i = 0; i < dc->res_pool->pipe_count; i++) {
455 				struct pipe_ctx *pipe_ctx = &dc->current_state->res_ctx.pipe_ctx[i];
456 
457 				/* adjust visual confirm color for all pipes with current stream */
458 				if (stream == pipe_ctx->stream) {
459 					get_cursor_visual_confirm_color(pipe_ctx, &(pipe_ctx->visual_confirm_color));
460 
461 					/* programming hardware */
462 					if (pipe_ctx->plane_state)
463 						dc->hwss.update_visual_confirm_color(dc, pipe_ctx,
464 								pipe_ctx->plane_res.hubp->mpcc_id);
465 				}
466 			}
467 		}
468 
469 		return true;
470 	}
471 
472 	return false;
473 }
474 
dc_stream_add_writeback(struct dc * dc,struct dc_stream_state * stream,struct dc_writeback_info * wb_info)475 bool dc_stream_add_writeback(struct dc *dc,
476 		struct dc_stream_state *stream,
477 		struct dc_writeback_info *wb_info)
478 {
479 	bool isDrc = false;
480 	int i = 0;
481 	struct dwbc *dwb;
482 
483 	if (stream == NULL) {
484 		dm_error("DC: dc_stream is NULL!\n");
485 		return false;
486 	}
487 
488 	if (wb_info == NULL) {
489 		dm_error("DC: dc_writeback_info is NULL!\n");
490 		return false;
491 	}
492 
493 	if (wb_info->dwb_pipe_inst >= MAX_DWB_PIPES) {
494 		dm_error("DC: writeback pipe is invalid!\n");
495 		return false;
496 	}
497 
498 	dc_exit_ips_for_hw_access(dc);
499 
500 	wb_info->dwb_params.out_transfer_func = &stream->out_transfer_func;
501 
502 	dwb = dc->res_pool->dwbc[wb_info->dwb_pipe_inst];
503 	dwb->dwb_is_drc = false;
504 
505 	/* recalculate and apply DML parameters */
506 
507 	for (i = 0; i < stream->num_wb_info; i++) {
508 		/*dynamic update*/
509 		if (stream->writeback_info[i].wb_enabled &&
510 			stream->writeback_info[i].dwb_pipe_inst == wb_info->dwb_pipe_inst) {
511 			stream->writeback_info[i] = *wb_info;
512 			isDrc = true;
513 		}
514 	}
515 
516 	if (!isDrc) {
517 		ASSERT(stream->num_wb_info + 1 <= MAX_DWB_PIPES);
518 		stream->writeback_info[stream->num_wb_info++] = *wb_info;
519 	}
520 
521 	if (dc->hwss.enable_writeback) {
522 		struct dc_stream_status *stream_status = dc_stream_get_status(stream);
523 		struct dwbc *dwb = dc->res_pool->dwbc[wb_info->dwb_pipe_inst];
524 		if (stream_status)
525 			dwb->otg_inst = stream_status->primary_otg_inst;
526 	}
527 
528 	if (!dc->hwss.update_bandwidth(dc, dc->current_state)) {
529 		dm_error("DC: update_bandwidth failed!\n");
530 		return false;
531 	}
532 
533 	/* enable writeback */
534 	if (dc->hwss.enable_writeback) {
535 		struct dwbc *dwb = dc->res_pool->dwbc[wb_info->dwb_pipe_inst];
536 
537 		if (dwb->funcs->is_enabled(dwb)) {
538 			/* writeback pipe already enabled, only need to update */
539 			dc->hwss.update_writeback(dc, wb_info, dc->current_state);
540 		} else {
541 			/* Enable writeback pipe from scratch*/
542 			dc->hwss.enable_writeback(dc, wb_info, dc->current_state);
543 		}
544 	}
545 
546 	return true;
547 }
548 
dc_stream_fc_disable_writeback(struct dc * dc,struct dc_stream_state * stream,uint32_t dwb_pipe_inst)549 bool dc_stream_fc_disable_writeback(struct dc *dc,
550 		struct dc_stream_state *stream,
551 		uint32_t dwb_pipe_inst)
552 {
553 	struct dwbc *dwb = dc->res_pool->dwbc[dwb_pipe_inst];
554 
555 	if (stream == NULL) {
556 		dm_error("DC: dc_stream is NULL!\n");
557 		return false;
558 	}
559 
560 	if (dwb_pipe_inst >= MAX_DWB_PIPES) {
561 		dm_error("DC: writeback pipe is invalid!\n");
562 		return false;
563 	}
564 
565 	if (stream->num_wb_info > MAX_DWB_PIPES) {
566 		dm_error("DC: num_wb_info is invalid!\n");
567 		return false;
568 	}
569 
570 	dc_exit_ips_for_hw_access(dc);
571 
572 	if (dwb->funcs->set_fc_enable)
573 		dwb->funcs->set_fc_enable(dwb, DWB_FRAME_CAPTURE_DISABLE);
574 
575 	return true;
576 }
577 
578 /**
579  * dc_stream_remove_writeback() - Disables writeback and removes writeback info.
580  * @dc: Display core control structure.
581  * @stream: Display core stream state.
582  * @dwb_pipe_inst: Display writeback pipe.
583  *
584  * Return: returns true on success, false otherwise.
585  */
dc_stream_remove_writeback(struct dc * dc,struct dc_stream_state * stream,uint32_t dwb_pipe_inst)586 bool dc_stream_remove_writeback(struct dc *dc,
587 		struct dc_stream_state *stream,
588 		uint32_t dwb_pipe_inst)
589 {
590 	unsigned int i, j;
591 	if (stream == NULL) {
592 		dm_error("DC: dc_stream is NULL!\n");
593 		return false;
594 	}
595 
596 	if (dwb_pipe_inst >= MAX_DWB_PIPES) {
597 		dm_error("DC: writeback pipe is invalid!\n");
598 		return false;
599 	}
600 
601 	if (stream->num_wb_info > MAX_DWB_PIPES) {
602 		dm_error("DC: num_wb_info is invalid!\n");
603 		return false;
604 	}
605 
606 	/* remove writeback info for disabled writeback pipes from stream */
607 	for (i = 0, j = 0; i < stream->num_wb_info; i++) {
608 		if (stream->writeback_info[i].wb_enabled) {
609 
610 			if (stream->writeback_info[i].dwb_pipe_inst == dwb_pipe_inst)
611 				stream->writeback_info[i].wb_enabled = false;
612 
613 			/* trim the array */
614 			if (j < i) {
615 				memcpy(&stream->writeback_info[j], &stream->writeback_info[i],
616 						sizeof(struct dc_writeback_info));
617 				j++;
618 			}
619 		}
620 	}
621 	stream->num_wb_info = j;
622 
623 	/* recalculate and apply DML parameters */
624 	if (!dc->hwss.update_bandwidth(dc, dc->current_state)) {
625 		dm_error("DC: update_bandwidth failed!\n");
626 		return false;
627 	}
628 
629 	dc_exit_ips_for_hw_access(dc);
630 
631 	/* disable writeback */
632 	if (dc->hwss.disable_writeback) {
633 		struct dwbc *dwb = dc->res_pool->dwbc[dwb_pipe_inst];
634 
635 		if (dwb->funcs->is_enabled(dwb))
636 			dc->hwss.disable_writeback(dc, dwb_pipe_inst);
637 	}
638 
639 	return true;
640 }
641 
dc_stream_get_vblank_counter(const struct dc_stream_state * stream)642 uint32_t dc_stream_get_vblank_counter(const struct dc_stream_state *stream)
643 {
644 	uint8_t i;
645 	struct dc  *dc = stream->ctx->dc;
646 	struct resource_context *res_ctx =
647 		&dc->current_state->res_ctx;
648 
649 	dc_exit_ips_for_hw_access(dc);
650 
651 	for (i = 0; i < MAX_PIPES; i++) {
652 		struct timing_generator *tg = res_ctx->pipe_ctx[i].stream_res.tg;
653 
654 		if (res_ctx->pipe_ctx[i].stream != stream || !tg)
655 			continue;
656 
657 		return tg->funcs->get_frame_count(tg);
658 	}
659 
660 	return 0;
661 }
662 
dc_stream_send_dp_sdp(const struct dc_stream_state * stream,const uint8_t * custom_sdp_message,unsigned int sdp_message_size)663 bool dc_stream_send_dp_sdp(const struct dc_stream_state *stream,
664 		const uint8_t *custom_sdp_message,
665 		unsigned int sdp_message_size)
666 {
667 	int i;
668 	struct dc  *dc;
669 	struct resource_context *res_ctx;
670 
671 	if (stream == NULL) {
672 		dm_error("DC: dc_stream is NULL!\n");
673 		return false;
674 	}
675 
676 	dc = stream->ctx->dc;
677 	res_ctx = &dc->current_state->res_ctx;
678 
679 	dc_exit_ips_for_hw_access(dc);
680 
681 	for (i = 0; i < MAX_PIPES; i++) {
682 		struct pipe_ctx *pipe_ctx = &res_ctx->pipe_ctx[i];
683 
684 		if (pipe_ctx->stream != stream)
685 			continue;
686 
687 		if (dc->hwss.send_immediate_sdp_message != NULL)
688 			dc->hwss.send_immediate_sdp_message(pipe_ctx,
689 								custom_sdp_message,
690 								sdp_message_size);
691 		else
692 			DC_LOG_WARNING("%s:send_immediate_sdp_message not implemented on this ASIC\n",
693 			__func__);
694 
695 	}
696 
697 	return true;
698 }
699 
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)700 bool dc_stream_get_scanoutpos(const struct dc_stream_state *stream,
701 				  uint32_t *v_blank_start,
702 				  uint32_t *v_blank_end,
703 				  uint32_t *h_position,
704 				  uint32_t *v_position)
705 {
706 	uint8_t i;
707 	bool ret = false;
708 	struct dc  *dc;
709 	struct resource_context *res_ctx;
710 
711 	if (!stream->ctx)
712 		return false;
713 
714 	dc = stream->ctx->dc;
715 	res_ctx = &dc->current_state->res_ctx;
716 
717 	dc_exit_ips_for_hw_access(dc);
718 
719 	for (i = 0; i < MAX_PIPES; i++) {
720 		struct timing_generator *tg = res_ctx->pipe_ctx[i].stream_res.tg;
721 
722 		if (res_ctx->pipe_ctx[i].stream != stream || !tg)
723 			continue;
724 
725 		tg->funcs->get_scanoutpos(tg,
726 					  v_blank_start,
727 					  v_blank_end,
728 					  h_position,
729 					  v_position);
730 
731 		ret = true;
732 		break;
733 	}
734 
735 	return ret;
736 }
737 
dc_stream_dmdata_status_done(struct dc * dc,struct dc_stream_state * stream)738 bool dc_stream_dmdata_status_done(struct dc *dc, struct dc_stream_state *stream)
739 {
740 	struct pipe_ctx *pipe = NULL;
741 	int i;
742 
743 	if (!dc->hwss.dmdata_status_done)
744 		return false;
745 
746 	for (i = 0; i < MAX_PIPES; i++) {
747 		pipe = &dc->current_state->res_ctx.pipe_ctx[i];
748 		if (pipe->stream == stream)
749 			break;
750 	}
751 	/* Stream not found, by default we'll assume HUBP fetched dm data */
752 	if (i == MAX_PIPES)
753 		return true;
754 
755 	dc_exit_ips_for_hw_access(dc);
756 
757 	return dc->hwss.dmdata_status_done(pipe);
758 }
759 
dc_stream_set_dynamic_metadata(struct dc * dc,struct dc_stream_state * stream,struct dc_dmdata_attributes * attr)760 bool dc_stream_set_dynamic_metadata(struct dc *dc,
761 		struct dc_stream_state *stream,
762 		struct dc_dmdata_attributes *attr)
763 {
764 	struct pipe_ctx *pipe_ctx = NULL;
765 	struct hubp *hubp;
766 	int i;
767 
768 	/* Dynamic metadata is only supported on HDMI or DP */
769 	if (!dc_is_hdmi_signal(stream->signal) && !dc_is_dp_signal(stream->signal))
770 		return false;
771 
772 	/* Check hardware support */
773 	if (!dc->hwss.program_dmdata_engine)
774 		return false;
775 
776 	for (i = 0; i < MAX_PIPES; i++) {
777 		pipe_ctx = &dc->current_state->res_ctx.pipe_ctx[i];
778 		if (pipe_ctx->stream == stream)
779 			break;
780 	}
781 
782 	if (i == MAX_PIPES)
783 		return false;
784 
785 	hubp = pipe_ctx->plane_res.hubp;
786 	if (hubp == NULL)
787 		return false;
788 
789 	pipe_ctx->stream->dmdata_address = attr->address;
790 
791 	dc_exit_ips_for_hw_access(dc);
792 
793 	dc->hwss.program_dmdata_engine(pipe_ctx);
794 
795 	if (hubp->funcs->dmdata_set_attributes != NULL &&
796 			pipe_ctx->stream->dmdata_address.quad_part != 0) {
797 		hubp->funcs->dmdata_set_attributes(hubp, attr);
798 	}
799 
800 	return true;
801 }
802 
dc_stream_add_dsc_to_resource(struct dc * dc,struct dc_state * state,struct dc_stream_state * stream)803 enum dc_status dc_stream_add_dsc_to_resource(struct dc *dc,
804 		struct dc_state *state,
805 		struct dc_stream_state *stream)
806 {
807 	if (dc->res_pool->funcs->add_dsc_to_stream_resource) {
808 		return dc->res_pool->funcs->add_dsc_to_stream_resource(dc, state, stream);
809 	} else {
810 		return DC_NO_DSC_RESOURCE;
811 	}
812 }
813 
dc_stream_get_pipe_ctx(struct dc_stream_state * stream)814 struct pipe_ctx *dc_stream_get_pipe_ctx(struct dc_stream_state *stream)
815 {
816 	int i = 0;
817 
818 	for (i = 0; i < MAX_PIPES; i++) {
819 		struct pipe_ctx *pipe = &stream->ctx->dc->current_state->res_ctx.pipe_ctx[i];
820 
821 		if (pipe->stream == stream)
822 			return pipe;
823 	}
824 
825 	return NULL;
826 }
827 
dc_stream_log(const struct dc * dc,const struct dc_stream_state * stream)828 void dc_stream_log(const struct dc *dc, const struct dc_stream_state *stream)
829 {
830 	DC_LOG_DC(
831 			"core_stream 0x%p: src: %d, %d, %d, %d; dst: %d, %d, %d, %d, colorSpace:%d\n",
832 			stream,
833 			stream->src.x,
834 			stream->src.y,
835 			stream->src.width,
836 			stream->src.height,
837 			stream->dst.x,
838 			stream->dst.y,
839 			stream->dst.width,
840 			stream->dst.height,
841 			stream->output_color_space);
842 	DC_LOG_DC(
843 			"\tpix_clk_khz: %d, h_total: %d, v_total: %d, pixel_encoding:%s, color_depth:%s\n",
844 			stream->timing.pix_clk_100hz / 10,
845 			stream->timing.h_total,
846 			stream->timing.v_total,
847 			dc_pixel_encoding_to_str(stream->timing.pixel_encoding),
848 			dc_color_depth_to_str(stream->timing.display_color_depth));
849 	DC_LOG_DC(
850 			"\tlink: %d\n",
851 			stream->link->link_index);
852 
853 	DC_LOG_DC(
854 			"\tdsc: %d, mst_pbn: %d\n",
855 			stream->timing.flags.DSC,
856 			stream->timing.dsc_cfg.mst_pbn);
857 
858 	if (stream->sink) {
859 		if (stream->sink->sink_signal != SIGNAL_TYPE_VIRTUAL &&
860 			stream->sink->sink_signal != SIGNAL_TYPE_NONE) {
861 
862 			DC_LOG_DC(
863 					"\tdispname: %s signal: %x\n",
864 					stream->sink->edid_caps.display_name,
865 					stream->signal);
866 		}
867 	}
868 }
869 
870 /*
871 *	dc_stream_get_3dlut()
872 *	Requirements:
873 *	1. Is stream already owns an RMCM instance, return it.
874 *	2. If it doesn't and we don't need to allocate, return NULL.
875 *	3. If there's a free RMCM instance, assign to stream and return it.
876 *	4. If no free RMCM instances, return NULL.
877 */
878 
dc_stream_get_3dlut_for_stream(const struct dc * dc,const struct dc_stream_state * stream,bool allocate_one)879 struct dc_rmcm_3dlut *dc_stream_get_3dlut_for_stream(
880 	const struct dc *dc,
881 	const struct dc_stream_state *stream,
882 	bool allocate_one)
883 {
884 	unsigned int num_rmcm = dc->caps.color.mpc.num_rmcm_3dluts;
885 
886 	// see if one is allocated for this stream
887 	for (int i = 0; i < num_rmcm; i++) {
888 		if (dc->res_pool->rmcm_3dlut[i].isInUse &&
889 			dc->res_pool->rmcm_3dlut[i].stream == stream)
890 			return &dc->res_pool->rmcm_3dlut[i];
891 	}
892 
893 	//case: not found one, and dont need to allocate
894 	if (!allocate_one)
895 		return NULL;
896 
897 	//see if there is an unused 3dlut, allocate
898 	for (int i = 0; i < num_rmcm; i++) {
899 		if (!dc->res_pool->rmcm_3dlut[i].isInUse) {
900 			dc->res_pool->rmcm_3dlut[i].isInUse = true;
901 			dc->res_pool->rmcm_3dlut[i].stream = stream;
902 			return &dc->res_pool->rmcm_3dlut[i];
903 		}
904 	}
905 
906 	//dont have a 3dlut
907 	return NULL;
908 }
909 
910 
dc_stream_release_3dlut_for_stream(const struct dc * dc,const struct dc_stream_state * stream)911 void dc_stream_release_3dlut_for_stream(
912 	const struct dc *dc,
913 	const struct dc_stream_state *stream)
914 {
915 	struct dc_rmcm_3dlut *rmcm_3dlut =
916 		dc_stream_get_3dlut_for_stream(dc, stream, false);
917 
918 	if (rmcm_3dlut) {
919 		rmcm_3dlut->isInUse = false;
920 		rmcm_3dlut->stream  = NULL;
921 		rmcm_3dlut->protection_bits = 0;
922 	}
923 }
924 
925 
dc_stream_init_rmcm_3dlut(struct dc * dc)926 void dc_stream_init_rmcm_3dlut(struct dc *dc)
927 {
928 	unsigned int num_rmcm = dc->caps.color.mpc.num_rmcm_3dluts;
929 
930 	for (int i = 0; i < num_rmcm; i++) {
931 		dc->res_pool->rmcm_3dlut[i].isInUse = false;
932 		dc->res_pool->rmcm_3dlut[i].stream = NULL;
933 		dc->res_pool->rmcm_3dlut[i].protection_bits = 0;
934 	}
935 }
936 
937 /*
938  * Finds the greatest index in refresh_rate_hz that contains a value <= refresh
939  */
dc_stream_get_nearest_smallest_index(struct dc_stream_state * stream,int refresh)940 static int dc_stream_get_nearest_smallest_index(struct dc_stream_state *stream, int refresh)
941 {
942 	for (int i = 0; i < (LUMINANCE_DATA_TABLE_SIZE - 1); ++i) {
943 		if ((stream->lumin_data.refresh_rate_hz[i] <= refresh) && (refresh < stream->lumin_data.refresh_rate_hz[i + 1])) {
944 			return i;
945 		}
946 	}
947 	return 9;
948 }
949 
950 /*
951  * Finds a corresponding brightness for a given refresh rate between 2 given indices, where index1 < index2
952  */
dc_stream_get_brightness_millinits_linear_interpolation(struct dc_stream_state * stream,int index1,int index2,int refresh_hz)953 static int dc_stream_get_brightness_millinits_linear_interpolation (struct dc_stream_state *stream,
954 								     int index1,
955 								     int index2,
956 								     int refresh_hz)
957 {
958 	long long slope = 0;
959 	if (stream->lumin_data.refresh_rate_hz[index2] != stream->lumin_data.refresh_rate_hz[index1]) {
960 		slope = (stream->lumin_data.luminance_millinits[index2] - stream->lumin_data.luminance_millinits[index1]) /
961 			    (stream->lumin_data.refresh_rate_hz[index2] - stream->lumin_data.refresh_rate_hz[index1]);
962 	}
963 
964 	int y_intercept = stream->lumin_data.luminance_millinits[index2] - slope * stream->lumin_data.refresh_rate_hz[index2];
965 
966 	return (y_intercept + refresh_hz * slope);
967 }
968 
969 /*
970  * Finds a corresponding refresh rate for a given brightness between 2 given indices, where index1 < index2
971  */
dc_stream_get_refresh_hz_linear_interpolation(struct dc_stream_state * stream,int index1,int index2,int brightness_millinits)972 static int dc_stream_get_refresh_hz_linear_interpolation (struct dc_stream_state *stream,
973 							   int index1,
974 							   int index2,
975 							   int brightness_millinits)
976 {
977 	long long slope = 1;
978 	if (stream->lumin_data.refresh_rate_hz[index2] != stream->lumin_data.refresh_rate_hz[index1]) {
979 		slope = (stream->lumin_data.luminance_millinits[index2] - stream->lumin_data.luminance_millinits[index1]) /
980 				(stream->lumin_data.refresh_rate_hz[index2] - stream->lumin_data.refresh_rate_hz[index1]);
981 	}
982 
983 	int y_intercept = stream->lumin_data.luminance_millinits[index2] - slope * stream->lumin_data.refresh_rate_hz[index2];
984 
985 	return ((int)div64_s64((brightness_millinits - y_intercept), slope));
986 }
987 
988 /*
989  * Finds the current brightness in millinits given a refresh rate
990  */
dc_stream_get_brightness_millinits_from_refresh(struct dc_stream_state * stream,int refresh_hz)991 static int dc_stream_get_brightness_millinits_from_refresh (struct dc_stream_state *stream, int refresh_hz)
992 {
993 	int nearest_smallest_index = dc_stream_get_nearest_smallest_index(stream, refresh_hz);
994 	int nearest_smallest_value = stream->lumin_data.refresh_rate_hz[nearest_smallest_index];
995 
996 	if (nearest_smallest_value == refresh_hz)
997 		return stream->lumin_data.luminance_millinits[nearest_smallest_index];
998 
999 	if (nearest_smallest_index >= 9)
1000 		return dc_stream_get_brightness_millinits_linear_interpolation(stream, nearest_smallest_index - 1, nearest_smallest_index, refresh_hz);
1001 
1002 	if (nearest_smallest_value == stream->lumin_data.refresh_rate_hz[nearest_smallest_index + 1])
1003 		return stream->lumin_data.luminance_millinits[nearest_smallest_index];
1004 
1005 	return dc_stream_get_brightness_millinits_linear_interpolation(stream, nearest_smallest_index, nearest_smallest_index + 1, refresh_hz);
1006 }
1007 
1008 /*
1009  * Finds the lowest/highest refresh rate (depending on search_for_max_increase)
1010  * that can be achieved from starting_refresh_hz while staying
1011  * within flicker criteria
1012  */
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)1013 static int dc_stream_calculate_flickerless_refresh_rate(struct dc_stream_state *stream,
1014 							 int current_brightness,
1015 							 int starting_refresh_hz,
1016 							 bool is_gaming,
1017 							 bool search_for_max_increase)
1018 {
1019 	int nearest_smallest_index = dc_stream_get_nearest_smallest_index(stream, starting_refresh_hz);
1020 
1021 	int flicker_criteria_millinits = is_gaming ?
1022 					 stream->lumin_data.flicker_criteria_milli_nits_GAMING :
1023 					 stream->lumin_data.flicker_criteria_milli_nits_STATIC;
1024 
1025 	int safe_upper_bound = current_brightness + flicker_criteria_millinits;
1026 	int safe_lower_bound = current_brightness - flicker_criteria_millinits;
1027 	int lumin_millinits_temp = 0;
1028 
1029 	int offset = -1;
1030 	if (search_for_max_increase) {
1031 		offset = 1;
1032 	}
1033 
1034 	/*
1035 	 * Increments up or down by 1 depending on search_for_max_increase
1036 	 */
1037 	for (int i = nearest_smallest_index; (i > 0 && !search_for_max_increase) || (i < (LUMINANCE_DATA_TABLE_SIZE - 1) && search_for_max_increase); i += offset) {
1038 
1039 		lumin_millinits_temp = stream->lumin_data.luminance_millinits[i + offset];
1040 
1041 		if ((lumin_millinits_temp >= safe_upper_bound) || (lumin_millinits_temp <= safe_lower_bound)) {
1042 
1043 			if (stream->lumin_data.refresh_rate_hz[i + offset] == stream->lumin_data.refresh_rate_hz[i])
1044 				return stream->lumin_data.refresh_rate_hz[i];
1045 
1046 			int target_brightness = (stream->lumin_data.luminance_millinits[i + offset] >= (current_brightness + flicker_criteria_millinits)) ?
1047 											current_brightness + flicker_criteria_millinits :
1048 											current_brightness - flicker_criteria_millinits;
1049 
1050 			int refresh = 0;
1051 
1052 			/*
1053 			 * Need the second input to be < third input for dc_stream_get_refresh_hz_linear_interpolation
1054 			 */
1055 			if (search_for_max_increase)
1056 				refresh = dc_stream_get_refresh_hz_linear_interpolation(stream, i, i + offset, target_brightness);
1057 			else
1058 				refresh = dc_stream_get_refresh_hz_linear_interpolation(stream, i + offset, i, target_brightness);
1059 
1060 			if (refresh == stream->lumin_data.refresh_rate_hz[i + offset])
1061 				return stream->lumin_data.refresh_rate_hz[i + offset];
1062 
1063 			return refresh;
1064 		}
1065 	}
1066 
1067 	if (search_for_max_increase)
1068 		return (int)div64_s64((long long)stream->timing.pix_clk_100hz*100, stream->timing.v_total*(long long)stream->timing.h_total);
1069 	else
1070 		return stream->lumin_data.refresh_rate_hz[0];
1071 }
1072 
1073 /*
1074  * Gets the max delta luminance within a specified refresh range
1075  */
dc_stream_get_max_delta_lumin_millinits(struct dc_stream_state * stream,int hz1,int hz2,bool isGaming)1076 static int dc_stream_get_max_delta_lumin_millinits(struct dc_stream_state *stream, int hz1, int hz2, bool isGaming)
1077 {
1078 	int lower_refresh_brightness = dc_stream_get_brightness_millinits_from_refresh (stream, hz1);
1079 	int higher_refresh_brightness = dc_stream_get_brightness_millinits_from_refresh (stream, hz2);
1080 
1081 	int min = lower_refresh_brightness;
1082 	int max = higher_refresh_brightness;
1083 
1084 	/*
1085 	 * Static screen, therefore no need to scan through array
1086 	 */
1087 	if (!isGaming) {
1088 		if (lower_refresh_brightness >= higher_refresh_brightness) {
1089 			return lower_refresh_brightness - higher_refresh_brightness;
1090 		}
1091 		return higher_refresh_brightness - lower_refresh_brightness;
1092 	}
1093 
1094 	min = MIN(lower_refresh_brightness, higher_refresh_brightness);
1095 	max = MAX(lower_refresh_brightness, higher_refresh_brightness);
1096 
1097 	int nearest_smallest_index = dc_stream_get_nearest_smallest_index(stream, hz1);
1098 
1099 	for (; nearest_smallest_index < (LUMINANCE_DATA_TABLE_SIZE - 1) &&
1100 			stream->lumin_data.refresh_rate_hz[nearest_smallest_index + 1] <= hz2 ; nearest_smallest_index++) {
1101 		min = MIN(min, stream->lumin_data.luminance_millinits[nearest_smallest_index + 1]);
1102 		max = MAX(max, stream->lumin_data.luminance_millinits[nearest_smallest_index + 1]);
1103 	}
1104 
1105 	return (max - min);
1106 }
1107 
1108 /*
1109  * Determines the max flickerless instant vtotal delta for a stream.
1110  * Determines vtotal increase/decrease based on the bool "increase"
1111  */
dc_stream_get_max_flickerless_instant_vtotal_delta(struct dc_stream_state * stream,bool is_gaming,bool increase)1112 static unsigned int dc_stream_get_max_flickerless_instant_vtotal_delta(struct dc_stream_state *stream, bool is_gaming, bool increase)
1113 {
1114 	if (stream->timing.v_total * stream->timing.h_total == 0)
1115 		return 0;
1116 
1117 	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);
1118 
1119 	int safe_refresh_hz = dc_stream_calculate_flickerless_refresh_rate(stream,
1120 							 dc_stream_get_brightness_millinits_from_refresh(stream, current_refresh_hz),
1121 							 current_refresh_hz,
1122 							 is_gaming,
1123 							 increase);
1124 
1125 	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);
1126 
1127 	if (increase)
1128 		return (((int) stream->timing.v_total - safe_refresh_v_total) >= 0) ? (stream->timing.v_total - safe_refresh_v_total) : 0;
1129 
1130 	return ((safe_refresh_v_total - (int) stream->timing.v_total) >= 0) ? (safe_refresh_v_total - stream->timing.v_total) : 0;
1131 }
1132 
1133 /*
1134  * Finds the highest refresh rate that can be achieved
1135  * from starting_refresh_hz while staying within flicker criteria
1136  */
dc_stream_calculate_max_flickerless_refresh_rate(struct dc_stream_state * stream,int starting_refresh_hz,bool is_gaming)1137 int dc_stream_calculate_max_flickerless_refresh_rate(struct dc_stream_state *stream, int starting_refresh_hz, bool is_gaming)
1138 {
1139 	if (!stream->lumin_data.is_valid)
1140 		return 0;
1141 
1142 	int current_brightness = dc_stream_get_brightness_millinits_from_refresh(stream, starting_refresh_hz);
1143 
1144 	return dc_stream_calculate_flickerless_refresh_rate(stream,
1145 							    current_brightness,
1146 							    starting_refresh_hz,
1147 							    is_gaming,
1148 							    true);
1149 }
1150 
1151 /*
1152  * Finds the lowest refresh rate that can be achieved
1153  * from starting_refresh_hz while staying within flicker criteria
1154  */
dc_stream_calculate_min_flickerless_refresh_rate(struct dc_stream_state * stream,int starting_refresh_hz,bool is_gaming)1155 int dc_stream_calculate_min_flickerless_refresh_rate(struct dc_stream_state *stream, int starting_refresh_hz, bool is_gaming)
1156 {
1157 	if (!stream->lumin_data.is_valid)
1158 			return 0;
1159 
1160 	int current_brightness = dc_stream_get_brightness_millinits_from_refresh(stream, starting_refresh_hz);
1161 
1162 	return dc_stream_calculate_flickerless_refresh_rate(stream,
1163 							    current_brightness,
1164 							    starting_refresh_hz,
1165 							    is_gaming,
1166 							    false);
1167 }
1168 
1169 /*
1170  * Determines if there will be a flicker when moving between 2 refresh rates
1171  */
dc_stream_is_refresh_rate_range_flickerless(struct dc_stream_state * stream,int hz1,int hz2,bool is_gaming)1172 bool dc_stream_is_refresh_rate_range_flickerless(struct dc_stream_state *stream, int hz1, int hz2, bool is_gaming)
1173 {
1174 
1175 	/*
1176 	 * Assume that we wont flicker if there is invalid data
1177 	 */
1178 	if (!stream->lumin_data.is_valid)
1179 		return false;
1180 
1181 	int dl = dc_stream_get_max_delta_lumin_millinits(stream, hz1, hz2, is_gaming);
1182 
1183 	int flicker_criteria_millinits = (is_gaming) ?
1184 					  stream->lumin_data.flicker_criteria_milli_nits_GAMING :
1185 					  stream->lumin_data.flicker_criteria_milli_nits_STATIC;
1186 
1187 	return (dl <= flicker_criteria_millinits);
1188 }
1189 
1190 /*
1191  * Determines the max instant vtotal delta increase that can be applied without
1192  * flickering for a given stream
1193  */
dc_stream_get_max_flickerless_instant_vtotal_decrease(struct dc_stream_state * stream,bool is_gaming)1194 unsigned int dc_stream_get_max_flickerless_instant_vtotal_decrease(struct dc_stream_state *stream,
1195 									  bool is_gaming)
1196 {
1197 	if (!stream->lumin_data.is_valid)
1198 		return 0;
1199 
1200 	return dc_stream_get_max_flickerless_instant_vtotal_delta(stream, is_gaming, true);
1201 }
1202 
1203 /*
1204  * Determines the max instant vtotal delta decrease that can be applied without
1205  * flickering for a given stream
1206  */
dc_stream_get_max_flickerless_instant_vtotal_increase(struct dc_stream_state * stream,bool is_gaming)1207 unsigned int dc_stream_get_max_flickerless_instant_vtotal_increase(struct dc_stream_state *stream,
1208 									  bool is_gaming)
1209 {
1210 	if (!stream->lumin_data.is_valid)
1211 		return 0;
1212 
1213 	return dc_stream_get_max_flickerless_instant_vtotal_delta(stream, is_gaming, false);
1214 }
1215 
dc_stream_is_cursor_limit_pending(struct dc * dc,struct dc_stream_state * stream)1216 bool dc_stream_is_cursor_limit_pending(struct dc *dc, struct dc_stream_state *stream)
1217 {
1218 	bool is_limit_pending = false;
1219 
1220 	if (dc->current_state)
1221 		is_limit_pending = dc_state_get_stream_cursor_subvp_limit(stream, dc->current_state);
1222 
1223 	return is_limit_pending;
1224 }
1225 
dc_stream_can_clear_cursor_limit(struct dc * dc,struct dc_stream_state * stream)1226 bool dc_stream_can_clear_cursor_limit(struct dc *dc, struct dc_stream_state *stream)
1227 {
1228 	bool can_clear_limit = false;
1229 
1230 	if (dc->current_state)
1231 		can_clear_limit = dc_state_get_stream_cursor_subvp_limit(stream, dc->current_state) &&
1232 				(stream->hw_cursor_req ||
1233 				!stream->cursor_position.enable ||
1234 				dc_stream_check_cursor_attributes(stream, dc->current_state, &stream->cursor_attributes));
1235 
1236 	return can_clear_limit;
1237 }
1238