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 
35 #define DC_LOGGER dc->ctx->logger
36 
37 /*******************************************************************************
38  * Private functions
39  ******************************************************************************/
40 void update_stream_signal(struct dc_stream_state *stream, struct dc_sink *sink)
41 {
42 	if (sink->sink_signal == SIGNAL_TYPE_NONE)
43 		stream->signal = stream->link->connector_signal;
44 	else
45 		stream->signal = sink->sink_signal;
46 
47 	if (dc_is_dvi_signal(stream->signal)) {
48 		if (stream->ctx->dc->caps.dual_link_dvi &&
49 			(stream->timing.pix_clk_100hz / 10) > TMDS_MAX_PIXEL_CLOCK &&
50 			sink->sink_signal != SIGNAL_TYPE_DVI_SINGLE_LINK)
51 			stream->signal = SIGNAL_TYPE_DVI_DUAL_LINK;
52 		else
53 			stream->signal = SIGNAL_TYPE_DVI_SINGLE_LINK;
54 	}
55 }
56 
57 static bool dc_stream_construct(struct dc_stream_state *stream,
58 	struct dc_sink *dc_sink_data)
59 {
60 	uint32_t i = 0;
61 
62 	stream->sink = dc_sink_data;
63 	dc_sink_retain(dc_sink_data);
64 
65 	stream->ctx = dc_sink_data->ctx;
66 	stream->link = dc_sink_data->link;
67 	stream->sink_patches = dc_sink_data->edid_caps.panel_patch;
68 	stream->converter_disable_audio = dc_sink_data->converter_disable_audio;
69 	stream->qs_bit = dc_sink_data->edid_caps.qs_bit;
70 	stream->qy_bit = dc_sink_data->edid_caps.qy_bit;
71 
72 	/* Copy audio modes */
73 	/* TODO - Remove this translation */
74 	for (i = 0; i < (dc_sink_data->edid_caps.audio_mode_count); i++)
75 	{
76 		stream->audio_info.modes[i].channel_count = dc_sink_data->edid_caps.audio_modes[i].channel_count;
77 		stream->audio_info.modes[i].format_code = dc_sink_data->edid_caps.audio_modes[i].format_code;
78 		stream->audio_info.modes[i].sample_rates.all = dc_sink_data->edid_caps.audio_modes[i].sample_rate;
79 		stream->audio_info.modes[i].sample_size = dc_sink_data->edid_caps.audio_modes[i].sample_size;
80 	}
81 	stream->audio_info.mode_count = dc_sink_data->edid_caps.audio_mode_count;
82 	stream->audio_info.audio_latency = dc_sink_data->edid_caps.audio_latency;
83 	stream->audio_info.video_latency = dc_sink_data->edid_caps.video_latency;
84 	memmove(
85 		stream->audio_info.display_name,
86 		dc_sink_data->edid_caps.display_name,
87 		AUDIO_INFO_DISPLAY_NAME_SIZE_IN_CHARS);
88 	stream->audio_info.manufacture_id = dc_sink_data->edid_caps.manufacturer_id;
89 	stream->audio_info.product_id = dc_sink_data->edid_caps.product_id;
90 	stream->audio_info.flags.all = dc_sink_data->edid_caps.speaker_flags;
91 
92 	if (dc_sink_data->dc_container_id != NULL) {
93 		struct dc_container_id *dc_container_id = dc_sink_data->dc_container_id;
94 
95 		stream->audio_info.port_id[0] = dc_container_id->portId[0];
96 		stream->audio_info.port_id[1] = dc_container_id->portId[1];
97 	} else {
98 		/* TODO - WindowDM has implemented,
99 		other DMs need Unhardcode port_id */
100 		stream->audio_info.port_id[0] = 0x5558859e;
101 		stream->audio_info.port_id[1] = 0xd989449;
102 	}
103 
104 	/* EDID CAP translation for HDMI 2.0 */
105 	stream->timing.flags.LTE_340MCSC_SCRAMBLE = dc_sink_data->edid_caps.lte_340mcsc_scramble;
106 
107 	memset(&stream->timing.dsc_cfg, 0, sizeof(stream->timing.dsc_cfg));
108 	stream->timing.dsc_cfg.num_slices_h = 0;
109 	stream->timing.dsc_cfg.num_slices_v = 0;
110 	stream->timing.dsc_cfg.bits_per_pixel = 128;
111 	stream->timing.dsc_cfg.block_pred_enable = 1;
112 	stream->timing.dsc_cfg.linebuf_depth = 9;
113 	stream->timing.dsc_cfg.version_minor = 2;
114 	stream->timing.dsc_cfg.ycbcr422_simple = 0;
115 
116 	update_stream_signal(stream, dc_sink_data);
117 
118 	stream->out_transfer_func = dc_create_transfer_func();
119 	if (stream->out_transfer_func == NULL) {
120 		dc_sink_release(dc_sink_data);
121 		return false;
122 	}
123 	stream->out_transfer_func->type = TF_TYPE_BYPASS;
124 
125 	stream->stream_id = stream->ctx->dc_stream_id_count;
126 	stream->ctx->dc_stream_id_count++;
127 
128 	return true;
129 }
130 
131 static void dc_stream_destruct(struct dc_stream_state *stream)
132 {
133 	dc_sink_release(stream->sink);
134 	if (stream->out_transfer_func != NULL) {
135 		dc_transfer_func_release(stream->out_transfer_func);
136 		stream->out_transfer_func = NULL;
137 	}
138 }
139 
140 void dc_stream_retain(struct dc_stream_state *stream)
141 {
142 	kref_get(&stream->refcount);
143 }
144 
145 static void dc_stream_free(struct kref *kref)
146 {
147 	struct dc_stream_state *stream = container_of(kref, struct dc_stream_state, refcount);
148 
149 	dc_stream_destruct(stream);
150 	kfree(stream);
151 }
152 
153 void dc_stream_release(struct dc_stream_state *stream)
154 {
155 	if (stream != NULL) {
156 		kref_put(&stream->refcount, dc_stream_free);
157 	}
158 }
159 
160 struct dc_stream_state *dc_create_stream_for_sink(
161 		struct dc_sink *sink)
162 {
163 	struct dc_stream_state *stream;
164 
165 	if (sink == NULL)
166 		return NULL;
167 
168 	stream = kzalloc(sizeof(struct dc_stream_state), GFP_KERNEL);
169 	if (stream == NULL)
170 		goto alloc_fail;
171 
172 	if (dc_stream_construct(stream, sink) == false)
173 		goto construct_fail;
174 
175 	kref_init(&stream->refcount);
176 
177 	return stream;
178 
179 construct_fail:
180 	kfree(stream);
181 
182 alloc_fail:
183 	return NULL;
184 }
185 
186 struct dc_stream_state *dc_copy_stream(const struct dc_stream_state *stream)
187 {
188 	struct dc_stream_state *new_stream;
189 
190 	new_stream = kmemdup(stream, sizeof(struct dc_stream_state), GFP_KERNEL);
191 	if (!new_stream)
192 		return NULL;
193 
194 	if (new_stream->sink)
195 		dc_sink_retain(new_stream->sink);
196 
197 	if (new_stream->out_transfer_func)
198 		dc_transfer_func_retain(new_stream->out_transfer_func);
199 
200 	new_stream->stream_id = new_stream->ctx->dc_stream_id_count;
201 	new_stream->ctx->dc_stream_id_count++;
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_from_state - Get stream status from given dc state
214  * @state: DC state to find the stream status in
215  * @stream: The stream to get the stream status for
216  *
217  * The given stream is expected to exist in the given dc state. Otherwise, NULL
218  * will be returned.
219  */
220 struct dc_stream_status *dc_stream_get_status_from_state(
221 	struct dc_state *state,
222 	struct dc_stream_state *stream)
223 {
224 	uint8_t i;
225 
226 	if (state == NULL)
227 		return NULL;
228 
229 	for (i = 0; i < state->stream_count; i++) {
230 		if (stream == state->streams[i])
231 			return &state->stream_status[i];
232 	}
233 
234 	return NULL;
235 }
236 
237 /**
238  * dc_stream_get_status() - Get current stream status of the given stream state
239  * @stream: The stream to get the stream status for.
240  *
241  * The given stream is expected to exist in dc->current_state. Otherwise, NULL
242  * will be returned.
243  */
244 struct dc_stream_status *dc_stream_get_status(
245 	struct dc_stream_state *stream)
246 {
247 	struct dc *dc = stream->ctx->dc;
248 	return dc_stream_get_status_from_state(dc->current_state, stream);
249 }
250 
251 static void program_cursor_attributes(
252 	struct dc *dc,
253 	struct dc_stream_state *stream,
254 	const struct dc_cursor_attributes *attributes)
255 {
256 	int i;
257 	struct resource_context *res_ctx;
258 	struct pipe_ctx *pipe_to_program = NULL;
259 
260 	if (!stream)
261 		return;
262 
263 	res_ctx = &dc->current_state->res_ctx;
264 
265 	for (i = 0; i < MAX_PIPES; i++) {
266 		struct pipe_ctx *pipe_ctx = &res_ctx->pipe_ctx[i];
267 
268 		if (pipe_ctx->stream != stream)
269 			continue;
270 
271 		if (!pipe_to_program) {
272 			pipe_to_program = pipe_ctx;
273 			dc->hwss.cursor_lock(dc, pipe_to_program, true);
274 			if (pipe_to_program->next_odm_pipe)
275 				dc->hwss.cursor_lock(dc, pipe_to_program->next_odm_pipe, true);
276 		}
277 
278 		dc->hwss.set_cursor_attribute(pipe_ctx);
279 
280 		dc_send_update_cursor_info_to_dmu(pipe_ctx, i);
281 		if (dc->hwss.set_cursor_sdr_white_level)
282 			dc->hwss.set_cursor_sdr_white_level(pipe_ctx);
283 	}
284 
285 	if (pipe_to_program) {
286 		dc->hwss.cursor_lock(dc, pipe_to_program, false);
287 		if (pipe_to_program->next_odm_pipe)
288 			dc->hwss.cursor_lock(dc, pipe_to_program->next_odm_pipe, false);
289 	}
290 }
291 
292 #ifndef TRIM_FSFT
293 /*
294  * dc_optimize_timing_for_fsft() - dc to optimize timing
295  */
296 bool dc_optimize_timing_for_fsft(
297 	struct dc_stream_state *pStream,
298 	unsigned int max_input_rate_in_khz)
299 {
300 	struct dc  *dc;
301 
302 	dc = pStream->ctx->dc;
303 
304 	return (dc->hwss.optimize_timing_for_fsft &&
305 		dc->hwss.optimize_timing_for_fsft(dc, &pStream->timing, max_input_rate_in_khz));
306 }
307 #endif
308 
309 /*
310  * dc_stream_set_cursor_attributes() - Update cursor attributes and set cursor surface address
311  */
312 bool dc_stream_set_cursor_attributes(
313 	struct dc_stream_state *stream,
314 	const struct dc_cursor_attributes *attributes)
315 {
316 	struct dc  *dc;
317 	bool reset_idle_optimizations = false;
318 
319 	if (NULL == stream) {
320 		dm_error("DC: dc_stream is NULL!\n");
321 		return false;
322 	}
323 	if (NULL == attributes) {
324 		dm_error("DC: attributes is NULL!\n");
325 		return false;
326 	}
327 
328 	if (attributes->address.quad_part == 0) {
329 		dm_output_to_console("DC: Cursor address is 0!\n");
330 		return false;
331 	}
332 
333 	dc = stream->ctx->dc;
334 
335 	/* SubVP is not compatible with HW cursor larger than 64 x 64 x 4.
336 	 * Therefore, if cursor is greater than 64 x 64 x 4, fallback to SW cursor in the following case:
337 	 * 1. For single display cases, if resolution is >= 5K and refresh rate < 120hz
338 	 * 2. For multi display cases, if resolution is >= 4K and refresh rate < 120hz
339 	 *
340 	 * [< 120hz is a requirement for SubVP configs]
341 	 */
342 	if (dc->debug.allow_sw_cursor_fallback && attributes->height * attributes->width * 4 > 16384) {
343 		if (dc->current_state->stream_count == 1 && stream->timing.v_addressable >= 2880 &&
344 				((stream->timing.pix_clk_100hz * 100) / stream->timing.v_total / stream->timing.h_total) < 120)
345 			return false;
346 		else if (dc->current_state->stream_count > 1 && stream->timing.v_addressable >= 2160 &&
347 				((stream->timing.pix_clk_100hz * 100) / stream->timing.v_total / stream->timing.h_total) < 120)
348 			return false;
349 	}
350 
351 	stream->cursor_attributes = *attributes;
352 
353 	dc_z10_restore(dc);
354 	/* disable idle optimizations while updating cursor */
355 	if (dc->idle_optimizations_allowed) {
356 		dc_allow_idle_optimizations(dc, false);
357 		reset_idle_optimizations = true;
358 	}
359 
360 	program_cursor_attributes(dc, stream, attributes);
361 
362 	/* re-enable idle optimizations if necessary */
363 	if (reset_idle_optimizations)
364 		dc_allow_idle_optimizations(dc, true);
365 
366 	return true;
367 }
368 
369 static void program_cursor_position(
370 	struct dc *dc,
371 	struct dc_stream_state *stream,
372 	const struct dc_cursor_position *position)
373 {
374 	int i;
375 	struct resource_context *res_ctx;
376 	struct pipe_ctx *pipe_to_program = NULL;
377 
378 	if (!stream)
379 		return;
380 
381 	res_ctx = &dc->current_state->res_ctx;
382 
383 	for (i = 0; i < MAX_PIPES; i++) {
384 		struct pipe_ctx *pipe_ctx = &res_ctx->pipe_ctx[i];
385 
386 		if (pipe_ctx->stream != stream ||
387 				(!pipe_ctx->plane_res.mi  && !pipe_ctx->plane_res.hubp) ||
388 				!pipe_ctx->plane_state ||
389 				(!pipe_ctx->plane_res.xfm && !pipe_ctx->plane_res.dpp) ||
390 				(!pipe_ctx->plane_res.ipp && !pipe_ctx->plane_res.dpp))
391 			continue;
392 
393 		if (!pipe_to_program) {
394 			pipe_to_program = pipe_ctx;
395 			dc->hwss.cursor_lock(dc, pipe_to_program, true);
396 		}
397 
398 		dc->hwss.set_cursor_position(pipe_ctx);
399 
400 		dc_send_update_cursor_info_to_dmu(pipe_ctx, i);
401 	}
402 
403 	if (pipe_to_program)
404 		dc->hwss.cursor_lock(dc, pipe_to_program, false);
405 }
406 
407 bool dc_stream_set_cursor_position(
408 	struct dc_stream_state *stream,
409 	const struct dc_cursor_position *position)
410 {
411 	struct dc  *dc = stream->ctx->dc;
412 	bool reset_idle_optimizations = false;
413 
414 	if (NULL == stream) {
415 		dm_error("DC: dc_stream is NULL!\n");
416 		return false;
417 	}
418 
419 	if (NULL == position) {
420 		dm_error("DC: cursor position is NULL!\n");
421 		return false;
422 	}
423 
424 	dc = stream->ctx->dc;
425 	dc_z10_restore(dc);
426 
427 	/* disable idle optimizations if enabling cursor */
428 	if (dc->idle_optimizations_allowed && (!stream->cursor_position.enable || dc->debug.exit_idle_opt_for_cursor_updates)
429 			&& position->enable) {
430 		dc_allow_idle_optimizations(dc, false);
431 		reset_idle_optimizations = true;
432 	}
433 
434 	stream->cursor_position = *position;
435 
436 	program_cursor_position(dc, stream, position);
437 	/* re-enable idle optimizations if necessary */
438 	if (reset_idle_optimizations)
439 		dc_allow_idle_optimizations(dc, true);
440 
441 	return true;
442 }
443 
444 bool dc_stream_add_writeback(struct dc *dc,
445 		struct dc_stream_state *stream,
446 		struct dc_writeback_info *wb_info)
447 {
448 	bool isDrc = false;
449 	int i = 0;
450 	struct dwbc *dwb;
451 
452 	if (stream == NULL) {
453 		dm_error("DC: dc_stream is NULL!\n");
454 		return false;
455 	}
456 
457 	if (wb_info == NULL) {
458 		dm_error("DC: dc_writeback_info is NULL!\n");
459 		return false;
460 	}
461 
462 	if (wb_info->dwb_pipe_inst >= MAX_DWB_PIPES) {
463 		dm_error("DC: writeback pipe is invalid!\n");
464 		return false;
465 	}
466 
467 	wb_info->dwb_params.out_transfer_func = stream->out_transfer_func;
468 
469 	dwb = dc->res_pool->dwbc[wb_info->dwb_pipe_inst];
470 	dwb->dwb_is_drc = false;
471 
472 	/* recalculate and apply DML parameters */
473 
474 	for (i = 0; i < stream->num_wb_info; i++) {
475 		/*dynamic update*/
476 		if (stream->writeback_info[i].wb_enabled &&
477 			stream->writeback_info[i].dwb_pipe_inst == wb_info->dwb_pipe_inst) {
478 			stream->writeback_info[i] = *wb_info;
479 			isDrc = true;
480 		}
481 	}
482 
483 	if (!isDrc) {
484 		stream->writeback_info[stream->num_wb_info++] = *wb_info;
485 	}
486 
487 	if (dc->hwss.enable_writeback) {
488 		struct dc_stream_status *stream_status = dc_stream_get_status(stream);
489 		struct dwbc *dwb = dc->res_pool->dwbc[wb_info->dwb_pipe_inst];
490 		dwb->otg_inst = stream_status->primary_otg_inst;
491 	}
492 	if (IS_DIAG_DC(dc->ctx->dce_environment)) {
493 		if (!dc->hwss.update_bandwidth(dc, dc->current_state)) {
494 			dm_error("DC: update_bandwidth failed!\n");
495 			return false;
496 		}
497 
498 		/* enable writeback */
499 		if (dc->hwss.enable_writeback) {
500 			struct dwbc *dwb = dc->res_pool->dwbc[wb_info->dwb_pipe_inst];
501 
502 			if (dwb->funcs->is_enabled(dwb)) {
503 				/* writeback pipe already enabled, only need to update */
504 				dc->hwss.update_writeback(dc, wb_info, dc->current_state);
505 			} else {
506 				/* Enable writeback pipe from scratch*/
507 				dc->hwss.enable_writeback(dc, wb_info, dc->current_state);
508 			}
509 		}
510 	}
511 	return true;
512 }
513 
514 bool dc_stream_remove_writeback(struct dc *dc,
515 		struct dc_stream_state *stream,
516 		uint32_t dwb_pipe_inst)
517 {
518 	int i = 0, j = 0;
519 	if (stream == NULL) {
520 		dm_error("DC: dc_stream is NULL!\n");
521 		return false;
522 	}
523 
524 	if (dwb_pipe_inst >= MAX_DWB_PIPES) {
525 		dm_error("DC: writeback pipe is invalid!\n");
526 		return false;
527 	}
528 
529 //	stream->writeback_info[dwb_pipe_inst].wb_enabled = false;
530 	for (i = 0; i < stream->num_wb_info; i++) {
531 		/*dynamic update*/
532 		if (stream->writeback_info[i].wb_enabled &&
533 			stream->writeback_info[i].dwb_pipe_inst == dwb_pipe_inst) {
534 			stream->writeback_info[i].wb_enabled = false;
535 		}
536 	}
537 
538 	/* remove writeback info for disabled writeback pipes from stream */
539 	for (i = 0, j = 0; i < stream->num_wb_info; i++) {
540 		if (stream->writeback_info[i].wb_enabled) {
541 			if (j < i)
542 				/* trim the array */
543 				stream->writeback_info[j] = stream->writeback_info[i];
544 			j++;
545 		}
546 	}
547 	stream->num_wb_info = j;
548 
549 	if (IS_DIAG_DC(dc->ctx->dce_environment)) {
550 		/* recalculate and apply DML parameters */
551 		if (!dc->hwss.update_bandwidth(dc, dc->current_state)) {
552 			dm_error("DC: update_bandwidth failed!\n");
553 			return false;
554 		}
555 
556 		/* disable writeback */
557 		if (dc->hwss.disable_writeback)
558 			dc->hwss.disable_writeback(dc, dwb_pipe_inst);
559 	}
560 	return true;
561 }
562 
563 bool dc_stream_warmup_writeback(struct dc *dc,
564 		int num_dwb,
565 		struct dc_writeback_info *wb_info)
566 {
567 	if (dc->hwss.mmhubbub_warmup)
568 		return dc->hwss.mmhubbub_warmup(dc, num_dwb, wb_info);
569 	else
570 		return false;
571 }
572 uint32_t dc_stream_get_vblank_counter(const struct dc_stream_state *stream)
573 {
574 	uint8_t i;
575 	struct dc  *dc = stream->ctx->dc;
576 	struct resource_context *res_ctx =
577 		&dc->current_state->res_ctx;
578 
579 	for (i = 0; i < MAX_PIPES; i++) {
580 		struct timing_generator *tg = res_ctx->pipe_ctx[i].stream_res.tg;
581 
582 		if (res_ctx->pipe_ctx[i].stream != stream)
583 			continue;
584 
585 		return tg->funcs->get_frame_count(tg);
586 	}
587 
588 	return 0;
589 }
590 
591 bool dc_stream_send_dp_sdp(const struct dc_stream_state *stream,
592 		const uint8_t *custom_sdp_message,
593 		unsigned int sdp_message_size)
594 {
595 	int i;
596 	struct dc  *dc;
597 	struct resource_context *res_ctx;
598 
599 	if (stream == NULL) {
600 		dm_error("DC: dc_stream is NULL!\n");
601 		return false;
602 	}
603 
604 	dc = stream->ctx->dc;
605 	res_ctx = &dc->current_state->res_ctx;
606 
607 	for (i = 0; i < MAX_PIPES; i++) {
608 		struct pipe_ctx *pipe_ctx = &res_ctx->pipe_ctx[i];
609 
610 		if (pipe_ctx->stream != stream)
611 			continue;
612 
613 		if (dc->hwss.send_immediate_sdp_message != NULL)
614 			dc->hwss.send_immediate_sdp_message(pipe_ctx,
615 								custom_sdp_message,
616 								sdp_message_size);
617 		else
618 			DC_LOG_WARNING("%s:send_immediate_sdp_message not implemented on this ASIC\n",
619 			__func__);
620 
621 	}
622 
623 	return true;
624 }
625 
626 bool dc_stream_get_scanoutpos(const struct dc_stream_state *stream,
627 				  uint32_t *v_blank_start,
628 				  uint32_t *v_blank_end,
629 				  uint32_t *h_position,
630 				  uint32_t *v_position)
631 {
632 	uint8_t i;
633 	bool ret = false;
634 	struct dc  *dc = stream->ctx->dc;
635 	struct resource_context *res_ctx =
636 		&dc->current_state->res_ctx;
637 
638 	for (i = 0; i < MAX_PIPES; i++) {
639 		struct timing_generator *tg = res_ctx->pipe_ctx[i].stream_res.tg;
640 
641 		if (res_ctx->pipe_ctx[i].stream != stream)
642 			continue;
643 
644 		tg->funcs->get_scanoutpos(tg,
645 					  v_blank_start,
646 					  v_blank_end,
647 					  h_position,
648 					  v_position);
649 
650 		ret = true;
651 		break;
652 	}
653 
654 	return ret;
655 }
656 
657 bool dc_stream_dmdata_status_done(struct dc *dc, struct dc_stream_state *stream)
658 {
659 	struct pipe_ctx *pipe = NULL;
660 	int i;
661 
662 	if (!dc->hwss.dmdata_status_done)
663 		return false;
664 
665 	for (i = 0; i < MAX_PIPES; i++) {
666 		pipe = &dc->current_state->res_ctx.pipe_ctx[i];
667 		if (pipe->stream == stream)
668 			break;
669 	}
670 	/* Stream not found, by default we'll assume HUBP fetched dm data */
671 	if (i == MAX_PIPES)
672 		return true;
673 
674 	return dc->hwss.dmdata_status_done(pipe);
675 }
676 
677 bool dc_stream_set_dynamic_metadata(struct dc *dc,
678 		struct dc_stream_state *stream,
679 		struct dc_dmdata_attributes *attr)
680 {
681 	struct pipe_ctx *pipe_ctx = NULL;
682 	struct hubp *hubp;
683 	int i;
684 
685 	/* Dynamic metadata is only supported on HDMI or DP */
686 	if (!dc_is_hdmi_signal(stream->signal) && !dc_is_dp_signal(stream->signal))
687 		return false;
688 
689 	/* Check hardware support */
690 	if (!dc->hwss.program_dmdata_engine)
691 		return false;
692 
693 	for (i = 0; i < MAX_PIPES; i++) {
694 		pipe_ctx = &dc->current_state->res_ctx.pipe_ctx[i];
695 		if (pipe_ctx->stream == stream)
696 			break;
697 	}
698 
699 	if (i == MAX_PIPES)
700 		return false;
701 
702 	hubp = pipe_ctx->plane_res.hubp;
703 	if (hubp == NULL)
704 		return false;
705 
706 	pipe_ctx->stream->dmdata_address = attr->address;
707 
708 	dc->hwss.program_dmdata_engine(pipe_ctx);
709 
710 	if (hubp->funcs->dmdata_set_attributes != NULL &&
711 			pipe_ctx->stream->dmdata_address.quad_part != 0) {
712 		hubp->funcs->dmdata_set_attributes(hubp, attr);
713 	}
714 
715 	return true;
716 }
717 
718 enum dc_status dc_stream_add_dsc_to_resource(struct dc *dc,
719 		struct dc_state *state,
720 		struct dc_stream_state *stream)
721 {
722 	if (dc->res_pool->funcs->add_dsc_to_stream_resource) {
723 		return dc->res_pool->funcs->add_dsc_to_stream_resource(dc, state, stream);
724 	} else {
725 		return DC_NO_DSC_RESOURCE;
726 	}
727 }
728 
729 struct pipe_ctx *dc_stream_get_pipe_ctx(struct dc_stream_state *stream)
730 {
731 	int i = 0;
732 
733 	for (i = 0; i < MAX_PIPES; i++) {
734 		struct pipe_ctx *pipe = &stream->ctx->dc->current_state->res_ctx.pipe_ctx[i];
735 
736 		if (pipe->stream == stream)
737 			return pipe;
738 	}
739 
740 	return NULL;
741 }
742 
743 void dc_stream_log(const struct dc *dc, const struct dc_stream_state *stream)
744 {
745 	DC_LOG_DC(
746 			"core_stream 0x%p: src: %d, %d, %d, %d; dst: %d, %d, %d, %d, colorSpace:%d\n",
747 			stream,
748 			stream->src.x,
749 			stream->src.y,
750 			stream->src.width,
751 			stream->src.height,
752 			stream->dst.x,
753 			stream->dst.y,
754 			stream->dst.width,
755 			stream->dst.height,
756 			stream->output_color_space);
757 	DC_LOG_DC(
758 			"\tpix_clk_khz: %d, h_total: %d, v_total: %d, pixelencoder:%d, displaycolorDepth:%d\n",
759 			stream->timing.pix_clk_100hz / 10,
760 			stream->timing.h_total,
761 			stream->timing.v_total,
762 			stream->timing.pixel_encoding,
763 			stream->timing.display_color_depth);
764 	DC_LOG_DC(
765 			"\tlink: %d\n",
766 			stream->link->link_index);
767 
768 	DC_LOG_DC(
769 			"\tdsc: %d, mst_pbn: %d\n",
770 			stream->timing.flags.DSC,
771 			stream->timing.dsc_cfg.mst_pbn);
772 
773 	if (stream->sink) {
774 		if (stream->sink->sink_signal != SIGNAL_TYPE_VIRTUAL &&
775 			stream->sink->sink_signal != SIGNAL_TYPE_NONE) {
776 
777 			DC_LOG_DC(
778 					"\tdispname: %s signal: %x\n",
779 					stream->sink->edid_caps.display_name,
780 					stream->signal);
781 		}
782 	}
783 }
784 
785