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