2 * Copyright 2015 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.
25 #include "dm_services.h"
29 #include "core_status.h"
30 #include "core_types.h"
31 #include "hw_sequencer.h"
35 #include "clock_source.h"
36 #include "dc_bios_types.h"
38 #include "bios_parser_interface.h"
39 #include "include/irq_service_interface.h"
40 #include "transform.h"
42 #include "timing_generator.h"
43 #include "virtual/virtual_link_encoder.h"
45 #include "link_hwss.h"
46 #include "link_encoder.h"
48 #include "dc_link_ddc.h"
49 #include "dm_helpers.h"
50 #include "mem_input.h"
54 /*******************************************************************************
56 ******************************************************************************/
58 static inline void elevate_update_type(enum surface_update_type *original, enum surface_update_type new)
64 static void destroy_links(struct dc *dc)
68 for (i = 0; i < dc->link_count; i++) {
69 if (NULL != dc->links[i])
70 link_destroy(&dc->links[i]);
74 static bool create_links(
76 uint32_t num_virtual_links)
80 struct dc_bios *bios = dc->ctx->dc_bios;
84 connectors_num = bios->funcs->get_connectors_number(bios);
86 if (connectors_num > ENUM_ID_COUNT) {
88 "DC: Number of connectors %d exceeds maximum of %d!\n",
94 if (connectors_num == 0 && num_virtual_links == 0) {
95 dm_error("DC: Number of connectors is zero!\n");
99 "DC: %s: connectors_num: physical:%d, virtual:%d\n",
104 for (i = 0; i < connectors_num; i++) {
105 struct link_init_data link_init_params = {0};
106 struct dc_link *link;
108 link_init_params.ctx = dc->ctx;
109 /* next BIOS object table connector */
110 link_init_params.connector_index = i;
111 link_init_params.link_index = dc->link_count;
112 link_init_params.dc = dc;
113 link = link_create(&link_init_params);
116 dc->links[dc->link_count] = link;
122 for (i = 0; i < num_virtual_links; i++) {
123 struct dc_link *link = kzalloc(sizeof(*link), GFP_KERNEL);
124 struct encoder_init_data enc_init = {0};
131 link->link_index = dc->link_count;
132 dc->links[dc->link_count] = link;
137 link->connector_signal = SIGNAL_TYPE_VIRTUAL;
138 link->link_id.type = OBJECT_TYPE_CONNECTOR;
139 link->link_id.id = CONNECTOR_ID_VIRTUAL;
140 link->link_id.enum_id = ENUM_ID_1;
141 link->link_enc = kzalloc(sizeof(*link->link_enc), GFP_KERNEL);
143 if (!link->link_enc) {
148 link->link_status.dpcd_caps = &link->dpcd_caps;
150 enc_init.ctx = dc->ctx;
151 enc_init.channel = CHANNEL_ID_UNKNOWN;
152 enc_init.hpd_source = HPD_SOURCEID_UNKNOWN;
153 enc_init.transmitter = TRANSMITTER_UNKNOWN;
154 enc_init.connector = link->link_id;
155 enc_init.encoder.type = OBJECT_TYPE_ENCODER;
156 enc_init.encoder.id = ENCODER_ID_INTERNAL_VIRTUAL;
157 enc_init.encoder.enum_id = ENUM_ID_1;
158 virtual_link_encoder_construct(link->link_enc, &enc_init);
167 bool dc_stream_adjust_vmin_vmax(struct dc *dc,
168 struct dc_stream_state **streams, int num_streams,
171 /* TODO: Support multiple streams */
172 struct dc_stream_state *stream = streams[0];
176 for (i = 0; i < MAX_PIPES; i++) {
177 struct pipe_ctx *pipe = &dc->current_state->res_ctx.pipe_ctx[i];
179 if (pipe->stream == stream && pipe->stream_res.stream_enc) {
180 dc->hwss.set_drr(&pipe, 1, vmin, vmax);
182 /* build and update the info frame */
183 resource_build_info_frame(pipe);
184 dc->hwss.update_info_frame(pipe);
192 bool dc_stream_get_crtc_position(struct dc *dc,
193 struct dc_stream_state **streams, int num_streams,
194 unsigned int *v_pos, unsigned int *nom_v_pos)
196 /* TODO: Support multiple streams */
197 struct dc_stream_state *stream = streams[0];
200 struct crtc_position position;
202 for (i = 0; i < MAX_PIPES; i++) {
203 struct pipe_ctx *pipe =
204 &dc->current_state->res_ctx.pipe_ctx[i];
206 if (pipe->stream == stream && pipe->stream_res.stream_enc) {
207 dc->hwss.get_position(&pipe, 1, &position);
209 *v_pos = position.vertical_count;
210 *nom_v_pos = position.nominal_vcount;
217 void dc_stream_set_static_screen_events(struct dc *dc,
218 struct dc_stream_state **streams,
220 const struct dc_static_screen_events *events)
224 struct pipe_ctx *pipes_affected[MAX_PIPES];
225 int num_pipes_affected = 0;
227 for (i = 0; i < num_streams; i++) {
228 struct dc_stream_state *stream = streams[i];
230 for (j = 0; j < MAX_PIPES; j++) {
231 if (dc->current_state->res_ctx.pipe_ctx[j].stream
233 pipes_affected[num_pipes_affected++] =
234 &dc->current_state->res_ctx.pipe_ctx[j];
239 dc->hwss.set_static_screen_control(pipes_affected, num_pipes_affected, events);
242 static void destruct(struct dc *dc)
244 dc_release_state(dc->current_state);
245 dc->current_state = NULL;
249 dc_destroy_resource_pool(dc);
251 if (dc->ctx->gpio_service)
252 dal_gpio_service_destroy(&dc->ctx->gpio_service);
255 dal_i2caux_destroy(&dc->ctx->i2caux);
257 if (dc->ctx->created_bios)
258 dal_bios_parser_destroy(&dc->ctx->dc_bios);
261 dal_logger_destroy(&dc->ctx->logger);
272 #ifdef CONFIG_DRM_AMD_DC_DCN1_0
282 static bool construct(struct dc *dc,
283 const struct dc_init_data *init_params)
285 struct dal_logger *logger;
286 struct dc_context *dc_ctx;
287 struct bw_calcs_dceip *dc_dceip;
288 struct bw_calcs_vbios *dc_vbios;
289 #ifdef CONFIG_DRM_AMD_DC_DCN1_0
290 struct dcn_soc_bounding_box *dcn_soc;
291 struct dcn_ip_params *dcn_ip;
294 enum dce_version dc_version = DCE_VERSION_UNKNOWN;
296 dc_dceip = kzalloc(sizeof(*dc_dceip), GFP_KERNEL);
298 dm_error("%s: failed to create dceip\n", __func__);
302 dc->bw_dceip = dc_dceip;
304 dc_vbios = kzalloc(sizeof(*dc_vbios), GFP_KERNEL);
306 dm_error("%s: failed to create vbios\n", __func__);
310 dc->bw_vbios = dc_vbios;
311 #ifdef CONFIG_DRM_AMD_DC_DCN1_0
312 dcn_soc = kzalloc(sizeof(*dcn_soc), GFP_KERNEL);
314 dm_error("%s: failed to create dcn_soc\n", __func__);
318 dc->dcn_soc = dcn_soc;
320 dcn_ip = kzalloc(sizeof(*dcn_ip), GFP_KERNEL);
322 dm_error("%s: failed to create dcn_ip\n", __func__);
329 dc_ctx = kzalloc(sizeof(*dc_ctx), GFP_KERNEL);
331 dm_error("%s: failed to create ctx\n", __func__);
335 dc_ctx->cgs_device = init_params->cgs_device;
336 dc_ctx->driver_context = init_params->driver;
338 dc_ctx->asic_id = init_params->asic_id;
341 dc->current_state = dc_create_state();
343 if (!dc->current_state) {
344 dm_error("%s: failed to create validate ctx\n", __func__);
349 logger = dal_logger_create(dc_ctx, init_params->log_mask);
352 /* can *not* call logger. call base driver 'print error' */
353 dm_error("%s: failed to create Logger!\n", __func__);
356 dc_ctx->logger = logger;
357 dc_ctx->dce_environment = init_params->dce_environment;
359 dc_version = resource_parse_asic_id(init_params->asic_id);
360 dc_ctx->dce_version = dc_version;
362 #if defined(CONFIG_DRM_AMD_DC_FBC)
363 dc->ctx->fbc_gpu_addr = init_params->fbc_gpu_addr;
365 /* Resource should construct all asic specific resources.
366 * This should be the only place where we need to parse the asic id
368 if (init_params->vbios_override)
369 dc_ctx->dc_bios = init_params->vbios_override;
371 /* Create BIOS parser */
372 struct bp_init_data bp_init_data;
374 bp_init_data.ctx = dc_ctx;
375 bp_init_data.bios = init_params->asic_id.atombios_base_address;
377 dc_ctx->dc_bios = dal_bios_parser_create(
378 &bp_init_data, dc_version);
380 if (!dc_ctx->dc_bios) {
381 ASSERT_CRITICAL(false);
385 dc_ctx->created_bios = true;
389 dc_ctx->i2caux = dal_i2caux_create(dc_ctx);
391 if (!dc_ctx->i2caux) {
392 ASSERT_CRITICAL(false);
396 /* Create GPIO service */
397 dc_ctx->gpio_service = dal_gpio_service_create(
399 dc_ctx->dce_environment,
402 if (!dc_ctx->gpio_service) {
403 ASSERT_CRITICAL(false);
407 dc->res_pool = dc_create_resource_pool(
409 init_params->num_virtual_links,
411 init_params->asic_id);
415 dc_resource_state_construct(dc, dc->current_state);
417 if (!create_links(dc, init_params->num_virtual_links))
428 static void disable_dangling_plane(struct dc *dc, struct dc_state *context)
431 struct dc_state *dangling_context = dc_create_state();
432 struct dc_state *current_ctx;
434 if (dangling_context == NULL)
437 dc_resource_state_copy_construct(dc->current_state, dangling_context);
439 for (i = 0; i < dc->res_pool->pipe_count; i++) {
440 struct dc_stream_state *old_stream =
441 dc->current_state->res_ctx.pipe_ctx[i].stream;
442 bool should_disable = true;
444 for (j = 0; j < context->stream_count; j++) {
445 if (old_stream == context->streams[j]) {
446 should_disable = false;
450 if (should_disable && old_stream) {
451 dc_rem_all_planes_for_stream(dc, old_stream, dangling_context);
452 dc->hwss.apply_ctx_for_surface(dc, old_stream, 0, dangling_context);
456 current_ctx = dc->current_state;
457 dc->current_state = dangling_context;
458 dc_release_state(current_ctx);
461 /*******************************************************************************
463 ******************************************************************************/
465 struct dc *dc_create(const struct dc_init_data *init_params)
467 struct dc *dc = kzalloc(sizeof(*dc), GFP_KERNEL);
468 unsigned int full_pipe_count;
473 if (false == construct(dc, init_params))
476 /*TODO: separate HW and SW initialization*/
477 dc->hwss.init_hw(dc);
479 full_pipe_count = dc->res_pool->pipe_count;
480 if (dc->res_pool->underlay_pipe_index != NO_UNDERLAY_PIPE)
482 dc->caps.max_streams = min(
484 dc->res_pool->stream_enc_count);
486 dc->caps.max_links = dc->link_count;
487 dc->caps.max_audios = dc->res_pool->audio_count;
488 dc->caps.linear_pitch_alignment = 64;
490 dc->config = init_params->flags;
492 dm_logger_write(dc->ctx->logger, LOG_DC,
493 "Display Core initialized\n");
496 /* TODO: missing feature to be enabled */
497 dc->debug.disable_dfs_bypass = true;
508 void dc_destroy(struct dc **dc)
515 static void enable_timing_multisync(
517 struct dc_state *ctx)
519 int i = 0, multisync_count = 0;
520 int pipe_count = dc->res_pool->pipe_count;
521 struct pipe_ctx *multisync_pipes[MAX_PIPES] = { NULL };
523 for (i = 0; i < pipe_count; i++) {
524 if (!ctx->res_ctx.pipe_ctx[i].stream ||
525 !ctx->res_ctx.pipe_ctx[i].stream->triggered_crtc_reset.enabled)
527 multisync_pipes[multisync_count] = &ctx->res_ctx.pipe_ctx[i];
531 if (multisync_count > 1) {
532 dc->hwss.enable_per_frame_crtc_position_reset(
533 dc, multisync_count, multisync_pipes);
537 static void program_timing_sync(
539 struct dc_state *ctx)
543 int pipe_count = dc->res_pool->pipe_count;
544 struct pipe_ctx *unsynced_pipes[MAX_PIPES] = { NULL };
546 for (i = 0; i < pipe_count; i++) {
547 if (!ctx->res_ctx.pipe_ctx[i].stream || ctx->res_ctx.pipe_ctx[i].top_pipe)
550 unsynced_pipes[i] = &ctx->res_ctx.pipe_ctx[i];
553 for (i = 0; i < pipe_count; i++) {
555 struct pipe_ctx *pipe_set[MAX_PIPES];
557 if (!unsynced_pipes[i])
560 pipe_set[0] = unsynced_pipes[i];
561 unsynced_pipes[i] = NULL;
563 /* Add tg to the set, search rest of the tg's for ones with
564 * same timing, add all tgs with same timing to the group
566 for (j = i + 1; j < pipe_count; j++) {
567 if (!unsynced_pipes[j])
570 if (resource_are_streams_timing_synchronizable(
571 unsynced_pipes[j]->stream,
572 pipe_set[0]->stream)) {
573 pipe_set[group_size] = unsynced_pipes[j];
574 unsynced_pipes[j] = NULL;
579 /* set first unblanked pipe as master */
580 for (j = 0; j < group_size; j++) {
581 struct pipe_ctx *temp;
583 if (pipe_set[j]->stream_res.tg->funcs->is_blanked && !pipe_set[j]->stream_res.tg->funcs->is_blanked(pipe_set[j]->stream_res.tg)) {
588 pipe_set[0] = pipe_set[j];
594 /* remove any other unblanked pipes as they have already been synced */
595 for (j = j + 1; j < group_size; j++) {
596 if (pipe_set[j]->stream_res.tg->funcs->is_blanked && !pipe_set[j]->stream_res.tg->funcs->is_blanked(pipe_set[j]->stream_res.tg)) {
598 pipe_set[j] = pipe_set[group_size];
603 if (group_size > 1) {
604 dc->hwss.enable_timing_synchronization(
605 dc, group_index, group_size, pipe_set);
611 static bool context_changed(
613 struct dc_state *context)
617 if (context->stream_count != dc->current_state->stream_count)
620 for (i = 0; i < dc->current_state->stream_count; i++) {
621 if (dc->current_state->streams[i] != context->streams[i])
628 bool dc_enable_stereo(
630 struct dc_state *context,
631 struct dc_stream_state *streams[],
632 uint8_t stream_count)
636 struct pipe_ctx *pipe;
638 for (i = 0; i < MAX_PIPES; i++) {
640 pipe = &context->res_ctx.pipe_ctx[i];
642 pipe = &dc->current_state->res_ctx.pipe_ctx[i];
643 for (j = 0 ; pipe && j < stream_count; j++) {
644 if (streams[j] && streams[j] == pipe->stream &&
645 dc->hwss.setup_stereo)
646 dc->hwss.setup_stereo(pipe, dc);
655 * Applies given context to HW and copy it into current context.
656 * It's up to the user to release the src context afterwards.
658 static enum dc_status dc_commit_state_no_check(struct dc *dc, struct dc_state *context)
660 struct dc_bios *dcb = dc->ctx->dc_bios;
661 enum dc_status result = DC_ERROR_UNEXPECTED;
662 struct pipe_ctx *pipe;
664 struct dc_stream_state *dc_streams[MAX_STREAMS] = {0};
666 disable_dangling_plane(dc, context);
668 for (i = 0; i < context->stream_count; i++)
669 dc_streams[i] = context->streams[i];
671 if (!dcb->funcs->is_accelerated_mode(dcb))
672 dc->hwss.enable_accelerated_mode(dc);
674 /* re-program planes for existing stream, in case we need to
675 * free up plane resource for later use
677 for (i = 0; i < context->stream_count; i++) {
678 if (context->streams[i]->mode_changed)
681 dc->hwss.apply_ctx_for_surface(
682 dc, context->streams[i],
683 context->stream_status[i].plane_count,
684 context); /* use new pipe config in new context */
687 /* Program hardware */
688 dc->hwss.ready_shared_resources(dc, context);
690 for (i = 0; i < dc->res_pool->pipe_count; i++) {
691 pipe = &context->res_ctx.pipe_ctx[i];
692 dc->hwss.wait_for_mpcc_disconnect(dc, dc->res_pool, pipe);
695 result = dc->hwss.apply_ctx_to_hw(dc, context);
700 if (context->stream_count > 1) {
701 enable_timing_multisync(dc, context);
702 program_timing_sync(dc, context);
705 /* Program all planes within new context*/
706 for (i = 0; i < context->stream_count; i++) {
707 const struct dc_sink *sink = context->streams[i]->sink;
709 if (!context->streams[i]->mode_changed)
712 dc->hwss.apply_ctx_for_surface(
713 dc, context->streams[i],
714 context->stream_status[i].plane_count,
719 * TODO rework dc_enable_stereo call to work with validation sets?
721 for (k = 0; k < MAX_PIPES; k++) {
722 pipe = &context->res_ctx.pipe_ctx[k];
724 for (l = 0 ; pipe && l < context->stream_count; l++) {
725 if (context->streams[l] &&
726 context->streams[l] == pipe->stream &&
727 dc->hwss.setup_stereo)
728 dc->hwss.setup_stereo(pipe, dc);
732 CONN_MSG_MODE(sink->link, "{%dx%d, %dx%d@%dKhz}",
733 context->streams[i]->timing.h_addressable,
734 context->streams[i]->timing.v_addressable,
735 context->streams[i]->timing.h_total,
736 context->streams[i]->timing.v_total,
737 context->streams[i]->timing.pix_clk_khz);
740 dc_enable_stereo(dc, context, dc_streams, context->stream_count);
742 dc_release_state(dc->current_state);
744 dc->current_state = context;
746 dc_retain_state(dc->current_state);
748 dc->hwss.optimize_shared_resources(dc);
753 bool dc_commit_state(struct dc *dc, struct dc_state *context)
755 enum dc_status result = DC_ERROR_UNEXPECTED;
758 if (false == context_changed(dc, context))
761 dm_logger_write(dc->ctx->logger, LOG_DC, "%s: %d streams\n",
762 __func__, context->stream_count);
764 for (i = 0; i < context->stream_count; i++) {
765 struct dc_stream_state *stream = context->streams[i];
767 dc_stream_log(stream,
772 result = dc_commit_state_no_check(dc, context);
774 return (result == DC_OK);
777 bool dc_post_update_surfaces_to_stream(struct dc *dc)
780 struct dc_state *context = dc->current_state;
782 post_surface_trace(dc);
784 for (i = 0; i < dc->res_pool->pipe_count; i++)
785 if (context->res_ctx.pipe_ctx[i].stream == NULL ||
786 context->res_ctx.pipe_ctx[i].plane_state == NULL) {
787 context->res_ctx.pipe_ctx[i].pipe_idx = i;
788 dc->hwss.disable_plane(dc, &context->res_ctx.pipe_ctx[i]);
791 dc->optimized_required = false;
793 /* 3rd param should be true, temp w/a for RV*/
794 #if defined(CONFIG_DRM_AMD_DC_DCN1_0)
795 dc->hwss.set_bandwidth(dc, context, dc->ctx->dce_version < DCN_VERSION_1_0);
797 dc->hwss.set_bandwidth(dc, context, true);
803 * TODO this whole function needs to go
805 * dc_surface_update is needlessly complex. See if we can just replace this
806 * with a dc_plane_state and follow the atomic model a bit more closely here.
808 bool dc_commit_planes_to_stream(
810 struct dc_plane_state **plane_states,
811 uint8_t new_plane_count,
812 struct dc_stream_state *dc_stream,
813 struct dc_state *state)
815 /* no need to dynamically allocate this. it's pretty small */
816 struct dc_surface_update updates[MAX_SURFACES];
817 struct dc_flip_addrs *flip_addr;
818 struct dc_plane_info *plane_info;
819 struct dc_scaling_info *scaling_info;
821 struct dc_stream_update *stream_update =
822 kzalloc(sizeof(struct dc_stream_update), GFP_KERNEL);
824 if (!stream_update) {
829 flip_addr = kcalloc(MAX_SURFACES, sizeof(struct dc_flip_addrs),
831 plane_info = kcalloc(MAX_SURFACES, sizeof(struct dc_plane_info),
833 scaling_info = kcalloc(MAX_SURFACES, sizeof(struct dc_scaling_info),
836 if (!flip_addr || !plane_info || !scaling_info) {
840 kfree(stream_update);
844 memset(updates, 0, sizeof(updates));
846 stream_update->src = dc_stream->src;
847 stream_update->dst = dc_stream->dst;
848 stream_update->out_transfer_func = dc_stream->out_transfer_func;
850 for (i = 0; i < new_plane_count; i++) {
851 updates[i].surface = plane_states[i];
853 (struct dc_gamma *)plane_states[i]->gamma_correction;
854 updates[i].in_transfer_func = plane_states[i]->in_transfer_func;
855 flip_addr[i].address = plane_states[i]->address;
856 flip_addr[i].flip_immediate = plane_states[i]->flip_immediate;
857 plane_info[i].color_space = plane_states[i]->color_space;
858 plane_info[i].input_tf = plane_states[i]->input_tf;
859 plane_info[i].format = plane_states[i]->format;
860 plane_info[i].plane_size = plane_states[i]->plane_size;
861 plane_info[i].rotation = plane_states[i]->rotation;
862 plane_info[i].horizontal_mirror = plane_states[i]->horizontal_mirror;
863 plane_info[i].stereo_format = plane_states[i]->stereo_format;
864 plane_info[i].tiling_info = plane_states[i]->tiling_info;
865 plane_info[i].visible = plane_states[i]->visible;
866 plane_info[i].per_pixel_alpha = plane_states[i]->per_pixel_alpha;
867 plane_info[i].dcc = plane_states[i]->dcc;
868 scaling_info[i].scaling_quality = plane_states[i]->scaling_quality;
869 scaling_info[i].src_rect = plane_states[i]->src_rect;
870 scaling_info[i].dst_rect = plane_states[i]->dst_rect;
871 scaling_info[i].clip_rect = plane_states[i]->clip_rect;
873 updates[i].flip_addr = &flip_addr[i];
874 updates[i].plane_info = &plane_info[i];
875 updates[i].scaling_info = &scaling_info[i];
878 dc_commit_updates_for_stream(
882 dc_stream, stream_update, plane_states, state);
887 kfree(stream_update);
891 struct dc_state *dc_create_state(void)
893 struct dc_state *context = kzalloc(sizeof(struct dc_state),
899 kref_init(&context->refcount);
903 void dc_retain_state(struct dc_state *context)
905 kref_get(&context->refcount);
908 static void dc_state_free(struct kref *kref)
910 struct dc_state *context = container_of(kref, struct dc_state, refcount);
911 dc_resource_state_destruct(context);
915 void dc_release_state(struct dc_state *context)
917 kref_put(&context->refcount, dc_state_free);
920 static bool is_surface_in_context(
921 const struct dc_state *context,
922 const struct dc_plane_state *plane_state)
926 for (j = 0; j < MAX_PIPES; j++) {
927 const struct pipe_ctx *pipe_ctx = &context->res_ctx.pipe_ctx[j];
929 if (plane_state == pipe_ctx->plane_state) {
937 static unsigned int pixel_format_to_bpp(enum surface_pixel_format format)
940 case SURFACE_PIXEL_FORMAT_VIDEO_420_YCbCr:
941 case SURFACE_PIXEL_FORMAT_VIDEO_420_YCrCb:
943 case SURFACE_PIXEL_FORMAT_GRPH_ARGB1555:
944 case SURFACE_PIXEL_FORMAT_GRPH_RGB565:
945 case SURFACE_PIXEL_FORMAT_VIDEO_420_10bpc_YCbCr:
946 case SURFACE_PIXEL_FORMAT_VIDEO_420_10bpc_YCrCb:
948 case SURFACE_PIXEL_FORMAT_GRPH_ARGB8888:
949 case SURFACE_PIXEL_FORMAT_GRPH_ABGR8888:
950 case SURFACE_PIXEL_FORMAT_GRPH_ARGB2101010:
951 case SURFACE_PIXEL_FORMAT_GRPH_ABGR2101010:
953 case SURFACE_PIXEL_FORMAT_GRPH_ARGB16161616:
954 case SURFACE_PIXEL_FORMAT_GRPH_ARGB16161616F:
955 case SURFACE_PIXEL_FORMAT_GRPH_ABGR16161616F:
958 ASSERT_CRITICAL(false);
963 static enum surface_update_type get_plane_info_update_type(const struct dc_surface_update *u)
965 union surface_update_flags *update_flags = &u->surface->update_flags;
968 return UPDATE_TYPE_FAST;
970 if (u->plane_info->color_space != u->surface->color_space)
971 update_flags->bits.color_space_change = 1;
973 if (u->plane_info->input_tf != u->surface->input_tf)
974 update_flags->bits.input_tf_change = 1;
976 if (u->plane_info->horizontal_mirror != u->surface->horizontal_mirror)
977 update_flags->bits.horizontal_mirror_change = 1;
979 if (u->plane_info->rotation != u->surface->rotation)
980 update_flags->bits.rotation_change = 1;
982 if (u->plane_info->stereo_format != u->surface->stereo_format)
983 update_flags->bits.stereo_format_change = 1;
985 if (u->plane_info->per_pixel_alpha != u->surface->per_pixel_alpha)
986 update_flags->bits.per_pixel_alpha_change = 1;
988 if (u->plane_info->dcc.enable != u->surface->dcc.enable
989 || u->plane_info->dcc.grph.independent_64b_blks != u->surface->dcc.grph.independent_64b_blks
990 || u->plane_info->dcc.grph.meta_pitch != u->surface->dcc.grph.meta_pitch)
991 update_flags->bits.dcc_change = 1;
993 if (pixel_format_to_bpp(u->plane_info->format) !=
994 pixel_format_to_bpp(u->surface->format))
995 /* different bytes per element will require full bandwidth
996 * and DML calculation
998 update_flags->bits.bpp_change = 1;
1000 if (memcmp(&u->plane_info->tiling_info, &u->surface->tiling_info,
1001 sizeof(union dc_tiling_info)) != 0) {
1002 update_flags->bits.swizzle_change = 1;
1003 /* todo: below are HW dependent, we should add a hook to
1004 * DCE/N resource and validated there.
1006 if (u->plane_info->tiling_info.gfx9.swizzle != DC_SW_LINEAR)
1007 /* swizzled mode requires RQ to be setup properly,
1008 * thus need to run DML to calculate RQ settings
1010 update_flags->bits.bandwidth_change = 1;
1013 if (update_flags->bits.rotation_change
1014 || update_flags->bits.stereo_format_change
1015 || update_flags->bits.bpp_change
1016 || update_flags->bits.bandwidth_change)
1017 return UPDATE_TYPE_FULL;
1019 return UPDATE_TYPE_MED;
1022 static enum surface_update_type get_scaling_info_update_type(
1023 const struct dc_surface_update *u)
1025 union surface_update_flags *update_flags = &u->surface->update_flags;
1027 if (!u->scaling_info)
1028 return UPDATE_TYPE_FAST;
1030 if (u->scaling_info->clip_rect.width != u->surface->clip_rect.width
1031 || u->scaling_info->clip_rect.height != u->surface->clip_rect.height
1032 || u->scaling_info->dst_rect.width != u->surface->dst_rect.width
1033 || u->scaling_info->dst_rect.height != u->surface->dst_rect.height) {
1034 update_flags->bits.scaling_change = 1;
1036 if ((u->scaling_info->dst_rect.width < u->surface->dst_rect.width
1037 || u->scaling_info->dst_rect.height < u->surface->dst_rect.height)
1038 && (u->scaling_info->dst_rect.width < u->surface->src_rect.width
1039 || u->scaling_info->dst_rect.height < u->surface->src_rect.height))
1040 /* Making dst rect smaller requires a bandwidth change */
1041 update_flags->bits.bandwidth_change = 1;
1044 if (u->scaling_info->src_rect.width != u->surface->src_rect.width
1045 || u->scaling_info->src_rect.height != u->surface->src_rect.height) {
1047 update_flags->bits.scaling_change = 1;
1048 if (u->scaling_info->src_rect.width > u->surface->src_rect.width
1049 && u->scaling_info->src_rect.height > u->surface->src_rect.height)
1050 /* Making src rect bigger requires a bandwidth change */
1051 update_flags->bits.clock_change = 1;
1054 if (u->scaling_info->src_rect.x != u->surface->src_rect.x
1055 || u->scaling_info->src_rect.y != u->surface->src_rect.y
1056 || u->scaling_info->clip_rect.x != u->surface->clip_rect.x
1057 || u->scaling_info->clip_rect.y != u->surface->clip_rect.y
1058 || u->scaling_info->dst_rect.x != u->surface->dst_rect.x
1059 || u->scaling_info->dst_rect.y != u->surface->dst_rect.y)
1060 update_flags->bits.position_change = 1;
1062 if (update_flags->bits.clock_change
1063 || update_flags->bits.bandwidth_change)
1064 return UPDATE_TYPE_FULL;
1066 if (update_flags->bits.scaling_change
1067 || update_flags->bits.position_change)
1068 return UPDATE_TYPE_MED;
1070 return UPDATE_TYPE_FAST;
1073 static enum surface_update_type det_surface_update(const struct dc *dc,
1074 const struct dc_surface_update *u)
1076 const struct dc_state *context = dc->current_state;
1077 enum surface_update_type type;
1078 enum surface_update_type overall_type = UPDATE_TYPE_FAST;
1079 union surface_update_flags *update_flags = &u->surface->update_flags;
1081 update_flags->raw = 0; // Reset all flags
1083 if (!is_surface_in_context(context, u->surface)) {
1084 update_flags->bits.new_plane = 1;
1085 return UPDATE_TYPE_FULL;
1088 type = get_plane_info_update_type(u);
1089 elevate_update_type(&overall_type, type);
1091 type = get_scaling_info_update_type(u);
1092 elevate_update_type(&overall_type, type);
1094 if (u->in_transfer_func)
1095 update_flags->bits.in_transfer_func = 1;
1097 if (u->input_csc_color_matrix)
1098 update_flags->bits.input_csc_change = 1;
1100 if (update_flags->bits.in_transfer_func
1101 || update_flags->bits.input_csc_change) {
1102 type = UPDATE_TYPE_MED;
1103 elevate_update_type(&overall_type, type);
1106 return overall_type;
1109 static enum surface_update_type check_update_surfaces_for_stream(
1111 struct dc_surface_update *updates,
1113 struct dc_stream_update *stream_update,
1114 const struct dc_stream_status *stream_status)
1117 enum surface_update_type overall_type = UPDATE_TYPE_FAST;
1119 if (stream_status == NULL || stream_status->plane_count != surface_count)
1120 return UPDATE_TYPE_FULL;
1123 return UPDATE_TYPE_FULL;
1125 for (i = 0 ; i < surface_count; i++) {
1126 enum surface_update_type type =
1127 det_surface_update(dc, &updates[i]);
1129 if (type == UPDATE_TYPE_FULL)
1132 elevate_update_type(&overall_type, type);
1135 return overall_type;
1138 enum surface_update_type dc_check_update_surfaces_for_stream(
1140 struct dc_surface_update *updates,
1142 struct dc_stream_update *stream_update,
1143 const struct dc_stream_status *stream_status)
1146 enum surface_update_type type;
1148 for (i = 0; i < surface_count; i++)
1149 updates[i].surface->update_flags.raw = 0;
1151 type = check_update_surfaces_for_stream(dc, updates, surface_count, stream_update, stream_status);
1152 if (type == UPDATE_TYPE_FULL)
1153 for (i = 0; i < surface_count; i++)
1154 updates[i].surface->update_flags.bits.full_update = 1;
1159 static struct dc_stream_status *stream_get_status(
1160 struct dc_state *ctx,
1161 struct dc_stream_state *stream)
1165 for (i = 0; i < ctx->stream_count; i++) {
1166 if (stream == ctx->streams[i]) {
1167 return &ctx->stream_status[i];
1174 static const enum surface_update_type update_surface_trace_level = UPDATE_TYPE_FULL;
1177 static void commit_planes_for_stream(struct dc *dc,
1178 struct dc_surface_update *srf_updates,
1180 struct dc_stream_state *stream,
1181 struct dc_stream_update *stream_update,
1182 enum surface_update_type update_type,
1183 struct dc_state *context)
1187 if (update_type == UPDATE_TYPE_FULL) {
1188 dc->hwss.set_bandwidth(dc, context, false);
1189 context_clock_trace(dc, context);
1192 if (surface_count == 0) {
1194 * In case of turning off screen, no need to program front end a second time.
1195 * just return after program front end.
1197 dc->hwss.apply_ctx_for_surface(dc, stream, surface_count, context);
1202 for (j = 0; j < dc->res_pool->pipe_count; j++) {
1203 struct pipe_ctx *pipe_ctx = &context->res_ctx.pipe_ctx[j];
1205 if (update_type == UPDATE_TYPE_FAST || !pipe_ctx->plane_state)
1208 if (!pipe_ctx->top_pipe &&
1210 pipe_ctx->stream == stream) {
1211 struct dc_stream_status *stream_status =
1212 stream_get_status(context, pipe_ctx->stream);
1214 dc->hwss.apply_ctx_for_surface(
1215 dc, pipe_ctx->stream, stream_status->plane_count, context);
1219 if (update_type == UPDATE_TYPE_FULL)
1220 context_timing_trace(dc, &context->res_ctx);
1222 /* Perform requested Updates */
1223 for (i = 0; i < surface_count; i++) {
1224 struct dc_plane_state *plane_state = srf_updates[i].surface;
1226 for (j = 0; j < dc->res_pool->pipe_count; j++) {
1227 struct pipe_ctx *pipe_ctx = &context->res_ctx.pipe_ctx[j];
1229 if (pipe_ctx->stream != stream)
1232 if (pipe_ctx->plane_state != plane_state)
1235 if (update_type == UPDATE_TYPE_FAST && srf_updates[i].flip_addr)
1236 dc->hwss.update_plane_addr(dc, pipe_ctx);
1240 if (stream && stream_update && update_type > UPDATE_TYPE_FAST)
1241 for (j = 0; j < dc->res_pool->pipe_count; j++) {
1242 struct pipe_ctx *pipe_ctx =
1243 &context->res_ctx.pipe_ctx[j];
1245 if (pipe_ctx->stream != stream)
1248 if (stream_update->hdr_static_metadata) {
1249 resource_build_info_frame(pipe_ctx);
1250 dc->hwss.update_info_frame(pipe_ctx);
1255 void dc_commit_updates_for_stream(struct dc *dc,
1256 struct dc_surface_update *srf_updates,
1258 struct dc_stream_state *stream,
1259 struct dc_stream_update *stream_update,
1260 struct dc_plane_state **plane_states,
1261 struct dc_state *state)
1263 const struct dc_stream_status *stream_status;
1264 enum surface_update_type update_type;
1265 struct dc_state *context;
1266 struct dc_context *dc_ctx = dc->ctx;
1269 stream_status = dc_stream_get_status(stream);
1270 context = dc->current_state;
1272 update_type = dc_check_update_surfaces_for_stream(
1273 dc, srf_updates, surface_count, stream_update, stream_status);
1275 if (update_type >= update_surface_trace_level)
1276 update_surface_trace(dc, srf_updates, surface_count);
1279 if (update_type >= UPDATE_TYPE_FULL) {
1281 /* initialize scratch memory for building context */
1282 context = dc_create_state();
1283 if (context == NULL) {
1284 DC_ERROR("Failed to allocate new validate context!\n");
1288 dc_resource_state_copy_construct(state, context);
1292 for (i = 0; i < surface_count; i++) {
1293 struct dc_plane_state *surface = srf_updates[i].surface;
1295 /* TODO: On flip we don't build the state, so it still has the
1296 * old address. Which is why we are updating the address here
1298 if (srf_updates[i].flip_addr) {
1299 surface->address = srf_updates[i].flip_addr->address;
1300 surface->flip_immediate = srf_updates[i].flip_addr->flip_immediate;
1304 if (update_type >= UPDATE_TYPE_MED) {
1305 for (j = 0; j < dc->res_pool->pipe_count; j++) {
1306 struct pipe_ctx *pipe_ctx = &context->res_ctx.pipe_ctx[j];
1308 if (pipe_ctx->plane_state != surface)
1311 resource_build_scaling_params(pipe_ctx);
1316 commit_planes_for_stream(
1324 /*update current_State*/
1325 if (dc->current_state != context) {
1327 struct dc_state *old = dc->current_state;
1329 dc->current_state = context;
1330 dc_release_state(old);
1333 /*let's use current_state to update watermark etc*/
1334 if (update_type >= UPDATE_TYPE_FULL)
1335 dc_post_update_surfaces_to_stream(dc);
1341 uint8_t dc_get_current_stream_count(struct dc *dc)
1343 return dc->current_state->stream_count;
1346 struct dc_stream_state *dc_get_stream_at_index(struct dc *dc, uint8_t i)
1348 if (i < dc->current_state->stream_count)
1349 return dc->current_state->streams[i];
1353 enum dc_irq_source dc_interrupt_to_irq_source(
1358 return dal_irq_service_to_irq_source(dc->res_pool->irqs, src_id, ext_id);
1361 void dc_interrupt_set(struct dc *dc, enum dc_irq_source src, bool enable)
1367 dal_irq_service_set(dc->res_pool->irqs, src, enable);
1370 void dc_interrupt_ack(struct dc *dc, enum dc_irq_source src)
1372 dal_irq_service_ack(dc->res_pool->irqs, src);
1375 void dc_set_power_state(
1377 enum dc_acpi_cm_power_state power_state)
1379 struct kref refcount;
1381 switch (power_state) {
1382 case DC_ACPI_CM_POWER_STATE_D0:
1383 dc_resource_state_construct(dc, dc->current_state);
1385 dc->hwss.init_hw(dc);
1389 dc->hwss.power_down(dc);
1391 /* Zero out the current context so that on resume we start with
1392 * clean state, and dc hw programming optimizations will not
1393 * cause any trouble.
1396 /* Preserve refcount */
1397 refcount = dc->current_state->refcount;
1398 dc_resource_state_destruct(dc->current_state);
1399 memset(dc->current_state, 0,
1400 sizeof(*dc->current_state));
1402 dc->current_state->refcount = refcount;
1409 void dc_resume(struct dc *dc)
1414 for (i = 0; i < dc->link_count; i++)
1415 core_link_resume(dc->links[i]);
1420 uint32_t link_index,
1421 struct i2c_command *cmd)
1424 struct dc_link *link = dc->links[link_index];
1425 struct ddc_service *ddc = link->ddc;
1427 return dal_i2caux_submit_i2c_command(
1433 static bool link_add_remote_sink_helper(struct dc_link *dc_link, struct dc_sink *sink)
1435 if (dc_link->sink_count >= MAX_SINKS_PER_LINK) {
1436 BREAK_TO_DEBUGGER();
1440 dc_sink_retain(sink);
1442 dc_link->remote_sinks[dc_link->sink_count] = sink;
1443 dc_link->sink_count++;
1448 struct dc_sink *dc_link_add_remote_sink(
1449 struct dc_link *link,
1450 const uint8_t *edid,
1452 struct dc_sink_init_data *init_data)
1454 struct dc_sink *dc_sink;
1455 enum dc_edid_status edid_status;
1457 if (len > MAX_EDID_BUFFER_SIZE) {
1458 dm_error("Max EDID buffer size breached!\n");
1463 BREAK_TO_DEBUGGER();
1467 if (!init_data->link) {
1468 BREAK_TO_DEBUGGER();
1472 dc_sink = dc_sink_create(init_data);
1477 memmove(dc_sink->dc_edid.raw_edid, edid, len);
1478 dc_sink->dc_edid.length = len;
1480 if (!link_add_remote_sink_helper(
1485 edid_status = dm_helpers_parse_edid_caps(
1488 &dc_sink->edid_caps);
1490 if (edid_status != EDID_OK)
1495 dc_link_remove_remote_sink(link, dc_sink);
1497 dc_sink_release(dc_sink);
1501 void dc_link_remove_remote_sink(struct dc_link *link, struct dc_sink *sink)
1505 if (!link->sink_count) {
1506 BREAK_TO_DEBUGGER();
1510 for (i = 0; i < link->sink_count; i++) {
1511 if (link->remote_sinks[i] == sink) {
1512 dc_sink_release(sink);
1513 link->remote_sinks[i] = NULL;
1515 /* shrink array to remove empty place */
1516 while (i < link->sink_count - 1) {
1517 link->remote_sinks[i] = link->remote_sinks[i+1];
1520 link->remote_sinks[i] = NULL;