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