2 * Copyright 2012-15 Advanced Micro Devices, Inc.
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:
11 * The above copyright notice and this permission notice shall be included in
12 * all copies or substantial portions of the Software.
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.
26 #include <linux/delay.h>
27 #include <linux/slab.h>
29 #include "dm_services.h"
30 #include "basics/dc_common.h"
32 #include "core_types.h"
35 #include "timing_generator.h"
37 #define DC_LOGGER dc->ctx->logger
39 /*******************************************************************************
41 ******************************************************************************/
42 void update_stream_signal(struct dc_stream_state *stream, struct dc_sink *sink)
44 if (sink->sink_signal == SIGNAL_TYPE_NONE)
45 stream->signal = stream->link->connector_signal;
47 stream->signal = sink->sink_signal;
49 if (dc_is_dvi_signal(stream->signal)) {
50 if (stream->ctx->dc->caps.dual_link_dvi &&
51 (stream->timing.pix_clk_100hz / 10) > TMDS_MAX_PIXEL_CLOCK &&
52 sink->sink_signal != SIGNAL_TYPE_DVI_SINGLE_LINK)
53 stream->signal = SIGNAL_TYPE_DVI_DUAL_LINK;
55 stream->signal = SIGNAL_TYPE_DVI_SINGLE_LINK;
59 static bool dc_stream_construct(struct dc_stream_state *stream,
60 struct dc_sink *dc_sink_data)
64 stream->sink = dc_sink_data;
65 dc_sink_retain(dc_sink_data);
67 stream->ctx = dc_sink_data->ctx;
68 stream->link = dc_sink_data->link;
69 stream->sink_patches = dc_sink_data->edid_caps.panel_patch;
70 stream->converter_disable_audio = dc_sink_data->converter_disable_audio;
71 stream->qs_bit = dc_sink_data->edid_caps.qs_bit;
72 stream->qy_bit = dc_sink_data->edid_caps.qy_bit;
74 /* Copy audio modes */
75 /* TODO - Remove this translation */
76 for (i = 0; i < (dc_sink_data->edid_caps.audio_mode_count); i++)
78 stream->audio_info.modes[i].channel_count = dc_sink_data->edid_caps.audio_modes[i].channel_count;
79 stream->audio_info.modes[i].format_code = dc_sink_data->edid_caps.audio_modes[i].format_code;
80 stream->audio_info.modes[i].sample_rates.all = dc_sink_data->edid_caps.audio_modes[i].sample_rate;
81 stream->audio_info.modes[i].sample_size = dc_sink_data->edid_caps.audio_modes[i].sample_size;
83 stream->audio_info.mode_count = dc_sink_data->edid_caps.audio_mode_count;
84 stream->audio_info.audio_latency = dc_sink_data->edid_caps.audio_latency;
85 stream->audio_info.video_latency = dc_sink_data->edid_caps.video_latency;
87 stream->audio_info.display_name,
88 dc_sink_data->edid_caps.display_name,
89 AUDIO_INFO_DISPLAY_NAME_SIZE_IN_CHARS);
90 stream->audio_info.manufacture_id = dc_sink_data->edid_caps.manufacturer_id;
91 stream->audio_info.product_id = dc_sink_data->edid_caps.product_id;
92 stream->audio_info.flags.all = dc_sink_data->edid_caps.speaker_flags;
94 if (dc_sink_data->dc_container_id != NULL) {
95 struct dc_container_id *dc_container_id = dc_sink_data->dc_container_id;
97 stream->audio_info.port_id[0] = dc_container_id->portId[0];
98 stream->audio_info.port_id[1] = dc_container_id->portId[1];
100 /* TODO - WindowDM has implemented,
101 other DMs need Unhardcode port_id */
102 stream->audio_info.port_id[0] = 0x5558859e;
103 stream->audio_info.port_id[1] = 0xd989449;
106 /* EDID CAP translation for HDMI 2.0 */
107 stream->timing.flags.LTE_340MCSC_SCRAMBLE = dc_sink_data->edid_caps.lte_340mcsc_scramble;
109 memset(&stream->timing.dsc_cfg, 0, sizeof(stream->timing.dsc_cfg));
110 stream->timing.dsc_cfg.num_slices_h = 0;
111 stream->timing.dsc_cfg.num_slices_v = 0;
112 stream->timing.dsc_cfg.bits_per_pixel = 128;
113 stream->timing.dsc_cfg.block_pred_enable = 1;
114 stream->timing.dsc_cfg.linebuf_depth = 9;
115 stream->timing.dsc_cfg.version_minor = 2;
116 stream->timing.dsc_cfg.ycbcr422_simple = 0;
118 update_stream_signal(stream, dc_sink_data);
120 stream->out_transfer_func = dc_create_transfer_func();
121 if (stream->out_transfer_func == NULL) {
122 dc_sink_release(dc_sink_data);
125 stream->out_transfer_func->type = TF_TYPE_BYPASS;
127 stream->stream_id = stream->ctx->dc_stream_id_count;
128 stream->ctx->dc_stream_id_count++;
133 static void dc_stream_destruct(struct dc_stream_state *stream)
135 dc_sink_release(stream->sink);
136 if (stream->out_transfer_func != NULL) {
137 dc_transfer_func_release(stream->out_transfer_func);
138 stream->out_transfer_func = NULL;
142 void dc_stream_retain(struct dc_stream_state *stream)
144 kref_get(&stream->refcount);
147 static void dc_stream_free(struct kref *kref)
149 struct dc_stream_state *stream = container_of(kref, struct dc_stream_state, refcount);
151 dc_stream_destruct(stream);
155 void dc_stream_release(struct dc_stream_state *stream)
157 if (stream != NULL) {
158 kref_put(&stream->refcount, dc_stream_free);
162 struct dc_stream_state *dc_create_stream_for_sink(
163 struct dc_sink *sink)
165 struct dc_stream_state *stream;
170 stream = kzalloc(sizeof(struct dc_stream_state), GFP_KERNEL);
174 if (dc_stream_construct(stream, sink) == false)
177 kref_init(&stream->refcount);
188 struct dc_stream_state *dc_copy_stream(const struct dc_stream_state *stream)
190 struct dc_stream_state *new_stream;
192 new_stream = kmemdup(stream, sizeof(struct dc_stream_state), GFP_KERNEL);
196 if (new_stream->sink)
197 dc_sink_retain(new_stream->sink);
199 if (new_stream->out_transfer_func)
200 dc_transfer_func_retain(new_stream->out_transfer_func);
202 new_stream->stream_id = new_stream->ctx->dc_stream_id_count;
203 new_stream->ctx->dc_stream_id_count++;
205 /* If using dynamic encoder assignment, wait till stream committed to assign encoder. */
206 if (new_stream->ctx->dc->res_pool->funcs->link_encs_assign)
207 new_stream->link_enc = NULL;
209 kref_init(&new_stream->refcount);
215 * dc_stream_get_status_from_state - Get stream status from given dc state
216 * @state: DC state to find the stream status in
217 * @stream: The stream to get the stream status for
219 * The given stream is expected to exist in the given dc state. Otherwise, NULL
222 struct dc_stream_status *dc_stream_get_status_from_state(
223 struct dc_state *state,
224 struct dc_stream_state *stream)
231 for (i = 0; i < state->stream_count; i++) {
232 if (stream == state->streams[i])
233 return &state->stream_status[i];
240 * dc_stream_get_status() - Get current stream status of the given stream state
241 * @stream: The stream to get the stream status for.
243 * The given stream is expected to exist in dc->current_state. Otherwise, NULL
246 struct dc_stream_status *dc_stream_get_status(
247 struct dc_stream_state *stream)
249 struct dc *dc = stream->ctx->dc;
250 return dc_stream_get_status_from_state(dc->current_state, stream);
253 static void program_cursor_attributes(
255 struct dc_stream_state *stream,
256 const struct dc_cursor_attributes *attributes)
259 struct resource_context *res_ctx;
260 struct pipe_ctx *pipe_to_program = NULL;
265 res_ctx = &dc->current_state->res_ctx;
267 for (i = 0; i < MAX_PIPES; i++) {
268 struct pipe_ctx *pipe_ctx = &res_ctx->pipe_ctx[i];
270 if (pipe_ctx->stream != stream)
273 if (!pipe_to_program) {
274 pipe_to_program = pipe_ctx;
275 dc->hwss.cursor_lock(dc, pipe_to_program, true);
278 dc->hwss.set_cursor_attribute(pipe_ctx);
279 if (dc->hwss.set_cursor_sdr_white_level)
280 dc->hwss.set_cursor_sdr_white_level(pipe_ctx);
284 dc->hwss.cursor_lock(dc, pipe_to_program, false);
289 * dc_optimize_timing_for_fsft() - dc to optimize timing
291 bool dc_optimize_timing_for_fsft(
292 struct dc_stream_state *pStream,
293 unsigned int max_input_rate_in_khz)
297 dc = pStream->ctx->dc;
299 return (dc->hwss.optimize_timing_for_fsft &&
300 dc->hwss.optimize_timing_for_fsft(dc, &pStream->timing, max_input_rate_in_khz));
305 * dc_stream_set_cursor_attributes() - Update cursor attributes and set cursor surface address
307 bool dc_stream_set_cursor_attributes(
308 struct dc_stream_state *stream,
309 const struct dc_cursor_attributes *attributes)
312 #if defined(CONFIG_DRM_AMD_DC_DCN)
313 bool reset_idle_optimizations = false;
316 if (NULL == stream) {
317 dm_error("DC: dc_stream is NULL!\n");
320 if (NULL == attributes) {
321 dm_error("DC: attributes is NULL!\n");
325 if (attributes->address.quad_part == 0) {
326 dm_output_to_console("DC: Cursor address is 0!\n");
330 dc = stream->ctx->dc;
331 stream->cursor_attributes = *attributes;
333 #if defined(CONFIG_DRM_AMD_DC_DCN)
335 /* disable idle optimizations while updating cursor */
336 if (dc->idle_optimizations_allowed) {
337 dc_allow_idle_optimizations(dc, false);
338 reset_idle_optimizations = true;
342 program_cursor_attributes(dc, stream, attributes);
344 #if defined(CONFIG_DRM_AMD_DC_DCN)
345 /* re-enable idle optimizations if necessary */
346 if (reset_idle_optimizations)
347 dc_allow_idle_optimizations(dc, true);
353 static void program_cursor_position(
355 struct dc_stream_state *stream,
356 const struct dc_cursor_position *position)
359 struct resource_context *res_ctx;
360 struct pipe_ctx *pipe_to_program = NULL;
365 res_ctx = &dc->current_state->res_ctx;
367 for (i = 0; i < MAX_PIPES; i++) {
368 struct pipe_ctx *pipe_ctx = &res_ctx->pipe_ctx[i];
370 if (pipe_ctx->stream != stream ||
371 (!pipe_ctx->plane_res.mi && !pipe_ctx->plane_res.hubp) ||
372 !pipe_ctx->plane_state ||
373 (!pipe_ctx->plane_res.xfm && !pipe_ctx->plane_res.dpp) ||
374 (!pipe_ctx->plane_res.ipp && !pipe_ctx->plane_res.dpp))
377 if (!pipe_to_program) {
378 pipe_to_program = pipe_ctx;
379 dc->hwss.cursor_lock(dc, pipe_to_program, true);
382 dc->hwss.set_cursor_position(pipe_ctx);
386 dc->hwss.cursor_lock(dc, pipe_to_program, false);
389 bool dc_stream_set_cursor_position(
390 struct dc_stream_state *stream,
391 const struct dc_cursor_position *position)
394 #if defined(CONFIG_DRM_AMD_DC_DCN)
395 bool reset_idle_optimizations = false;
398 if (NULL == stream) {
399 dm_error("DC: dc_stream is NULL!\n");
403 if (NULL == position) {
404 dm_error("DC: cursor position is NULL!\n");
408 dc = stream->ctx->dc;
409 #if defined(CONFIG_DRM_AMD_DC_DCN)
412 /* disable idle optimizations if enabling cursor */
413 if (dc->idle_optimizations_allowed && !stream->cursor_position.enable && position->enable) {
414 dc_allow_idle_optimizations(dc, false);
415 reset_idle_optimizations = true;
419 stream->cursor_position = *position;
421 program_cursor_position(dc, stream, position);
422 #if defined(CONFIG_DRM_AMD_DC_DCN)
423 /* re-enable idle optimizations if necessary */
424 if (reset_idle_optimizations)
425 dc_allow_idle_optimizations(dc, true);
431 bool dc_stream_add_writeback(struct dc *dc,
432 struct dc_stream_state *stream,
433 struct dc_writeback_info *wb_info)
439 if (stream == NULL) {
440 dm_error("DC: dc_stream is NULL!\n");
444 if (wb_info == NULL) {
445 dm_error("DC: dc_writeback_info is NULL!\n");
449 if (wb_info->dwb_pipe_inst >= MAX_DWB_PIPES) {
450 dm_error("DC: writeback pipe is invalid!\n");
454 wb_info->dwb_params.out_transfer_func = stream->out_transfer_func;
456 dwb = dc->res_pool->dwbc[wb_info->dwb_pipe_inst];
457 dwb->dwb_is_drc = false;
459 /* recalculate and apply DML parameters */
461 for (i = 0; i < stream->num_wb_info; i++) {
463 if (stream->writeback_info[i].wb_enabled &&
464 stream->writeback_info[i].dwb_pipe_inst == wb_info->dwb_pipe_inst) {
465 stream->writeback_info[i] = *wb_info;
471 stream->writeback_info[stream->num_wb_info++] = *wb_info;
474 if (dc->hwss.enable_writeback) {
475 struct dc_stream_status *stream_status = dc_stream_get_status(stream);
476 struct dwbc *dwb = dc->res_pool->dwbc[wb_info->dwb_pipe_inst];
477 dwb->otg_inst = stream_status->primary_otg_inst;
479 if (IS_DIAG_DC(dc->ctx->dce_environment)) {
480 if (!dc->hwss.update_bandwidth(dc, dc->current_state)) {
481 dm_error("DC: update_bandwidth failed!\n");
485 /* enable writeback */
486 if (dc->hwss.enable_writeback) {
487 struct dwbc *dwb = dc->res_pool->dwbc[wb_info->dwb_pipe_inst];
489 if (dwb->funcs->is_enabled(dwb)) {
490 /* writeback pipe already enabled, only need to update */
491 dc->hwss.update_writeback(dc, wb_info, dc->current_state);
493 /* Enable writeback pipe from scratch*/
494 dc->hwss.enable_writeback(dc, wb_info, dc->current_state);
501 bool dc_stream_remove_writeback(struct dc *dc,
502 struct dc_stream_state *stream,
503 uint32_t dwb_pipe_inst)
506 if (stream == NULL) {
507 dm_error("DC: dc_stream is NULL!\n");
511 if (dwb_pipe_inst >= MAX_DWB_PIPES) {
512 dm_error("DC: writeback pipe is invalid!\n");
516 // stream->writeback_info[dwb_pipe_inst].wb_enabled = false;
517 for (i = 0; i < stream->num_wb_info; i++) {
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;
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) {
530 stream->writeback_info[j] = stream->writeback_info[i];
534 stream->num_wb_info = j;
536 if (IS_DIAG_DC(dc->ctx->dce_environment)) {
537 /* recalculate and apply DML parameters */
538 if (!dc->hwss.update_bandwidth(dc, dc->current_state)) {
539 dm_error("DC: update_bandwidth failed!\n");
543 /* disable writeback */
544 if (dc->hwss.disable_writeback)
545 dc->hwss.disable_writeback(dc, dwb_pipe_inst);
550 bool dc_stream_warmup_writeback(struct dc *dc,
552 struct dc_writeback_info *wb_info)
554 if (dc->hwss.mmhubbub_warmup)
555 return dc->hwss.mmhubbub_warmup(dc, num_dwb, wb_info);
559 uint32_t dc_stream_get_vblank_counter(const struct dc_stream_state *stream)
562 struct dc *dc = stream->ctx->dc;
563 struct resource_context *res_ctx =
564 &dc->current_state->res_ctx;
566 for (i = 0; i < MAX_PIPES; i++) {
567 struct timing_generator *tg = res_ctx->pipe_ctx[i].stream_res.tg;
569 if (res_ctx->pipe_ctx[i].stream != stream)
572 return tg->funcs->get_frame_count(tg);
578 bool dc_stream_send_dp_sdp(const struct dc_stream_state *stream,
579 const uint8_t *custom_sdp_message,
580 unsigned int sdp_message_size)
584 struct resource_context *res_ctx;
586 if (stream == NULL) {
587 dm_error("DC: dc_stream is NULL!\n");
591 dc = stream->ctx->dc;
592 res_ctx = &dc->current_state->res_ctx;
594 for (i = 0; i < MAX_PIPES; i++) {
595 struct pipe_ctx *pipe_ctx = &res_ctx->pipe_ctx[i];
597 if (pipe_ctx->stream != stream)
600 if (dc->hwss.send_immediate_sdp_message != NULL)
601 dc->hwss.send_immediate_sdp_message(pipe_ctx,
605 DC_LOG_WARNING("%s:send_immediate_sdp_message not implemented on this ASIC\n",
613 bool dc_stream_get_scanoutpos(const struct dc_stream_state *stream,
614 uint32_t *v_blank_start,
615 uint32_t *v_blank_end,
616 uint32_t *h_position,
617 uint32_t *v_position)
621 struct dc *dc = stream->ctx->dc;
622 struct resource_context *res_ctx =
623 &dc->current_state->res_ctx;
625 for (i = 0; i < MAX_PIPES; i++) {
626 struct timing_generator *tg = res_ctx->pipe_ctx[i].stream_res.tg;
628 if (res_ctx->pipe_ctx[i].stream != stream)
631 tg->funcs->get_scanoutpos(tg,
644 bool dc_stream_dmdata_status_done(struct dc *dc, struct dc_stream_state *stream)
646 struct pipe_ctx *pipe = NULL;
649 if (!dc->hwss.dmdata_status_done)
652 for (i = 0; i < MAX_PIPES; i++) {
653 pipe = &dc->current_state->res_ctx.pipe_ctx[i];
654 if (pipe->stream == stream)
657 /* Stream not found, by default we'll assume HUBP fetched dm data */
661 return dc->hwss.dmdata_status_done(pipe);
664 bool dc_stream_set_dynamic_metadata(struct dc *dc,
665 struct dc_stream_state *stream,
666 struct dc_dmdata_attributes *attr)
668 struct pipe_ctx *pipe_ctx = NULL;
672 /* Dynamic metadata is only supported on HDMI or DP */
673 if (!dc_is_hdmi_signal(stream->signal) && !dc_is_dp_signal(stream->signal))
676 /* Check hardware support */
677 if (!dc->hwss.program_dmdata_engine)
680 for (i = 0; i < MAX_PIPES; i++) {
681 pipe_ctx = &dc->current_state->res_ctx.pipe_ctx[i];
682 if (pipe_ctx->stream == stream)
689 hubp = pipe_ctx->plane_res.hubp;
693 pipe_ctx->stream->dmdata_address = attr->address;
695 dc->hwss.program_dmdata_engine(pipe_ctx);
697 if (hubp->funcs->dmdata_set_attributes != NULL &&
698 pipe_ctx->stream->dmdata_address.quad_part != 0) {
699 hubp->funcs->dmdata_set_attributes(hubp, attr);
705 enum dc_status dc_stream_add_dsc_to_resource(struct dc *dc,
706 struct dc_state *state,
707 struct dc_stream_state *stream)
709 if (dc->res_pool->funcs->add_dsc_to_stream_resource) {
710 return dc->res_pool->funcs->add_dsc_to_stream_resource(dc, state, stream);
712 return DC_NO_DSC_RESOURCE;
716 void dc_stream_log(const struct dc *dc, const struct dc_stream_state *stream)
719 "core_stream 0x%p: src: %d, %d, %d, %d; dst: %d, %d, %d, %d, colorSpace:%d\n",
729 stream->output_color_space);
731 "\tpix_clk_khz: %d, h_total: %d, v_total: %d, pixelencoder:%d, displaycolorDepth:%d\n",
732 stream->timing.pix_clk_100hz / 10,
733 stream->timing.h_total,
734 stream->timing.v_total,
735 stream->timing.pixel_encoding,
736 stream->timing.display_color_depth);
739 stream->link->link_index);