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 		if (dc->ctx->dmub_srv)
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 		if (dc->ctx->dmub_srv)
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;
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 		ASSERT(stream->num_wb_info + 1 <= MAX_DWB_PIPES);
485 		stream->writeback_info[stream->num_wb_info++] = *wb_info;
486 	}
487 
488 	if (dc->hwss.enable_writeback) {
489 		struct dc_stream_status *stream_status = dc_stream_get_status(stream);
490 		struct dwbc *dwb = dc->res_pool->dwbc[wb_info->dwb_pipe_inst];
491 		dwb->otg_inst = stream_status->primary_otg_inst;
492 	}
493 	return true;
494 }
495 
496 bool dc_stream_remove_writeback(struct dc *dc,
497 		struct dc_stream_state *stream,
498 		uint32_t dwb_pipe_inst)
499 {
500 	int i = 0, j = 0;
501 	if (stream == NULL) {
502 		dm_error("DC: dc_stream is NULL!\n");
503 		return false;
504 	}
505 
506 	if (dwb_pipe_inst >= MAX_DWB_PIPES) {
507 		dm_error("DC: writeback pipe is invalid!\n");
508 		return false;
509 	}
510 
511 	if (stream->num_wb_info > MAX_DWB_PIPES) {
512 		dm_error("DC: num_wb_info is invalid!\n");
513 		return false;
514 	}
515 
516 //	stream->writeback_info[dwb_pipe_inst].wb_enabled = false;
517 	for (i = 0; i < stream->num_wb_info; i++) {
518 		/*dynamic update*/
519 		if (stream->writeback_info[i].wb_enabled &&
520 			stream->writeback_info[i].dwb_pipe_inst == dwb_pipe_inst) {
521 			stream->writeback_info[i].wb_enabled = false;
522 		}
523 	}
524 
525 	/* remove writeback info for disabled writeback pipes from stream */
526 	for (i = 0, j = 0; i < stream->num_wb_info; i++) {
527 		if (stream->writeback_info[i].wb_enabled) {
528 			if (j < i)
529 				/* trim the array */
530 				memcpy(&stream->writeback_info[j], &stream->writeback_info[i],
531 						sizeof(struct dc_writeback_info));
532 			j++;
533 		}
534 	}
535 	stream->num_wb_info = j;
536 
537 	return true;
538 }
539 
540 bool dc_stream_warmup_writeback(struct dc *dc,
541 		int num_dwb,
542 		struct dc_writeback_info *wb_info)
543 {
544 	if (dc->hwss.mmhubbub_warmup)
545 		return dc->hwss.mmhubbub_warmup(dc, num_dwb, wb_info);
546 	else
547 		return false;
548 }
549 uint32_t dc_stream_get_vblank_counter(const struct dc_stream_state *stream)
550 {
551 	uint8_t i;
552 	struct dc  *dc = stream->ctx->dc;
553 	struct resource_context *res_ctx =
554 		&dc->current_state->res_ctx;
555 
556 	for (i = 0; i < MAX_PIPES; i++) {
557 		struct timing_generator *tg = res_ctx->pipe_ctx[i].stream_res.tg;
558 
559 		if (res_ctx->pipe_ctx[i].stream != stream)
560 			continue;
561 
562 		return tg->funcs->get_frame_count(tg);
563 	}
564 
565 	return 0;
566 }
567 
568 bool dc_stream_send_dp_sdp(const struct dc_stream_state *stream,
569 		const uint8_t *custom_sdp_message,
570 		unsigned int sdp_message_size)
571 {
572 	int i;
573 	struct dc  *dc;
574 	struct resource_context *res_ctx;
575 
576 	if (stream == NULL) {
577 		dm_error("DC: dc_stream is NULL!\n");
578 		return false;
579 	}
580 
581 	dc = stream->ctx->dc;
582 	res_ctx = &dc->current_state->res_ctx;
583 
584 	for (i = 0; i < MAX_PIPES; i++) {
585 		struct pipe_ctx *pipe_ctx = &res_ctx->pipe_ctx[i];
586 
587 		if (pipe_ctx->stream != stream)
588 			continue;
589 
590 		if (dc->hwss.send_immediate_sdp_message != NULL)
591 			dc->hwss.send_immediate_sdp_message(pipe_ctx,
592 								custom_sdp_message,
593 								sdp_message_size);
594 		else
595 			DC_LOG_WARNING("%s:send_immediate_sdp_message not implemented on this ASIC\n",
596 			__func__);
597 
598 	}
599 
600 	return true;
601 }
602 
603 bool dc_stream_get_scanoutpos(const struct dc_stream_state *stream,
604 				  uint32_t *v_blank_start,
605 				  uint32_t *v_blank_end,
606 				  uint32_t *h_position,
607 				  uint32_t *v_position)
608 {
609 	uint8_t i;
610 	bool ret = false;
611 	struct dc  *dc = stream->ctx->dc;
612 	struct resource_context *res_ctx =
613 		&dc->current_state->res_ctx;
614 
615 	for (i = 0; i < MAX_PIPES; i++) {
616 		struct timing_generator *tg = res_ctx->pipe_ctx[i].stream_res.tg;
617 
618 		if (res_ctx->pipe_ctx[i].stream != stream)
619 			continue;
620 
621 		tg->funcs->get_scanoutpos(tg,
622 					  v_blank_start,
623 					  v_blank_end,
624 					  h_position,
625 					  v_position);
626 
627 		ret = true;
628 		break;
629 	}
630 
631 	return ret;
632 }
633 
634 bool dc_stream_dmdata_status_done(struct dc *dc, struct dc_stream_state *stream)
635 {
636 	struct pipe_ctx *pipe = NULL;
637 	int i;
638 
639 	if (!dc->hwss.dmdata_status_done)
640 		return false;
641 
642 	for (i = 0; i < MAX_PIPES; i++) {
643 		pipe = &dc->current_state->res_ctx.pipe_ctx[i];
644 		if (pipe->stream == stream)
645 			break;
646 	}
647 	/* Stream not found, by default we'll assume HUBP fetched dm data */
648 	if (i == MAX_PIPES)
649 		return true;
650 
651 	return dc->hwss.dmdata_status_done(pipe);
652 }
653 
654 bool dc_stream_set_dynamic_metadata(struct dc *dc,
655 		struct dc_stream_state *stream,
656 		struct dc_dmdata_attributes *attr)
657 {
658 	struct pipe_ctx *pipe_ctx = NULL;
659 	struct hubp *hubp;
660 	int i;
661 
662 	/* Dynamic metadata is only supported on HDMI or DP */
663 	if (!dc_is_hdmi_signal(stream->signal) && !dc_is_dp_signal(stream->signal))
664 		return false;
665 
666 	/* Check hardware support */
667 	if (!dc->hwss.program_dmdata_engine)
668 		return false;
669 
670 	for (i = 0; i < MAX_PIPES; i++) {
671 		pipe_ctx = &dc->current_state->res_ctx.pipe_ctx[i];
672 		if (pipe_ctx->stream == stream)
673 			break;
674 	}
675 
676 	if (i == MAX_PIPES)
677 		return false;
678 
679 	hubp = pipe_ctx->plane_res.hubp;
680 	if (hubp == NULL)
681 		return false;
682 
683 	pipe_ctx->stream->dmdata_address = attr->address;
684 
685 	dc->hwss.program_dmdata_engine(pipe_ctx);
686 
687 	if (hubp->funcs->dmdata_set_attributes != NULL &&
688 			pipe_ctx->stream->dmdata_address.quad_part != 0) {
689 		hubp->funcs->dmdata_set_attributes(hubp, attr);
690 	}
691 
692 	return true;
693 }
694 
695 enum dc_status dc_stream_add_dsc_to_resource(struct dc *dc,
696 		struct dc_state *state,
697 		struct dc_stream_state *stream)
698 {
699 	if (dc->res_pool->funcs->add_dsc_to_stream_resource) {
700 		return dc->res_pool->funcs->add_dsc_to_stream_resource(dc, state, stream);
701 	} else {
702 		return DC_NO_DSC_RESOURCE;
703 	}
704 }
705 
706 struct pipe_ctx *dc_stream_get_pipe_ctx(struct dc_stream_state *stream)
707 {
708 	int i = 0;
709 
710 	for (i = 0; i < MAX_PIPES; i++) {
711 		struct pipe_ctx *pipe = &stream->ctx->dc->current_state->res_ctx.pipe_ctx[i];
712 
713 		if (pipe->stream == stream)
714 			return pipe;
715 	}
716 
717 	return NULL;
718 }
719 
720 void dc_stream_log(const struct dc *dc, const struct dc_stream_state *stream)
721 {
722 	DC_LOG_DC(
723 			"core_stream 0x%p: src: %d, %d, %d, %d; dst: %d, %d, %d, %d, colorSpace:%d\n",
724 			stream,
725 			stream->src.x,
726 			stream->src.y,
727 			stream->src.width,
728 			stream->src.height,
729 			stream->dst.x,
730 			stream->dst.y,
731 			stream->dst.width,
732 			stream->dst.height,
733 			stream->output_color_space);
734 	DC_LOG_DC(
735 			"\tpix_clk_khz: %d, h_total: %d, v_total: %d, pixelencoder:%d, displaycolorDepth:%d\n",
736 			stream->timing.pix_clk_100hz / 10,
737 			stream->timing.h_total,
738 			stream->timing.v_total,
739 			stream->timing.pixel_encoding,
740 			stream->timing.display_color_depth);
741 	DC_LOG_DC(
742 			"\tlink: %d\n",
743 			stream->link->link_index);
744 
745 	DC_LOG_DC(
746 			"\tdsc: %d, mst_pbn: %d\n",
747 			stream->timing.flags.DSC,
748 			stream->timing.dsc_cfg.mst_pbn);
749 
750 	if (stream->sink) {
751 		if (stream->sink->sink_signal != SIGNAL_TYPE_VIRTUAL &&
752 			stream->sink->sink_signal != SIGNAL_TYPE_NONE) {
753 
754 			DC_LOG_DC(
755 					"\tdispname: %s signal: %x\n",
756 					stream->sink->edid_caps.display_name,
757 					stream->signal);
758 		}
759 	}
760 }
761 
762