From: Gaghik Khachatrian <[email protected]>

[Why]
Compiler build generates type conversion warnings throughout dc/dml2_0
where values are implicitly narrowed (e.g. int/uint32_t/uint64_t assigned
to uint8_t, unsigned char, char, bool, or dml_bool_t), cluttering build
output and masking genuine issues.

[How]
Add explicit casts at each narrowing assignment with ASSERT guards
to catch out-of-range values in debug builds:
- uint8_t: otg_inst, num_planes, pipe_idx, vblank_index fields
- unsigned char: pipe_dlg_param.otg_inst from tg->inst
- char: mcache num_pipes from num_dpps_required
- bool/dml_bool_t: INTERLACE bitfield and fams2 enable flag use != 0
- uint64_t: widen min_hardware_refresh_in_uhz to hold div64_u64 result,
  then cast to unsigned long for min_refresh_uhz with ASSERT

Reviewed-by: Austin Zheng <[email protected]>
Signed-off-by: Gaghik Khachatrian <[email protected]>
Signed-off-by: Chuanyu Tseng <[email protected]>
---
 .../dc/dml2_0/dml21/dml21_translation_helper.c | 13 +++++++++----
 .../amd/display/dc/dml2_0/dml21/dml21_utils.c  | 15 +++++++++++----
 .../dc/dml2_0/dml21/dml21_wrapper_fpu.c        |  6 ++++--
 .../display/dc/dml2_0/dml2_dc_resource_mgmt.c  | 18 ++++++++++++------
 .../amd/display/dc/dml2_0/dml2_mall_phantom.c  |  3 ++-
 .../dc/dml2_0/dml2_translation_helper.c        |  2 +-
 .../gpu/drm/amd/display/dc/dml2_0/dml2_utils.c |  3 ++-
 7 files changed, 41 insertions(+), 19 deletions(-)

diff --git 
a/drivers/gpu/drm/amd/display/dc/dml2_0/dml21/dml21_translation_helper.c 
b/drivers/gpu/drm/amd/display/dc/dml2_0/dml21/dml21_translation_helper.c
index eadf84842ca0..2f0e0048bea8 100644
--- a/drivers/gpu/drm/amd/display/dc/dml2_0/dml21/dml21_translation_helper.c
+++ b/drivers/gpu/drm/amd/display/dc/dml2_0/dml21/dml21_translation_helper.c
@@ -90,7 +90,8 @@ static void 
populate_dml21_timing_config_from_stream_state(struct dml2_timing_cf
                struct pipe_ctx *pipe_ctx,
                struct dml2_context *dml_ctx)
 {
-       unsigned int hblank_start, vblank_start, min_hardware_refresh_in_uhz;
+       unsigned int hblank_start, vblank_start;
+       uint64_t min_hardware_refresh_in_uhz;
        uint32_t pix_clk_100hz;
 
        timing->h_active = stream->timing.h_addressable + 
stream->timing.h_border_left + stream->timing.h_border_right + 
pipe_ctx->dsc_padding_params.dsc_hactive_padding;
@@ -105,7 +106,7 @@ static void 
populate_dml21_timing_config_from_stream_state(struct dml2_timing_cf
        timing->h_total = stream->timing.h_total + 
pipe_ctx->dsc_padding_params.dsc_htotal_padding;
        timing->v_total = stream->timing.v_total;
        timing->h_sync_width = stream->timing.h_sync_width;
-       timing->interlaced = stream->timing.flags.INTERLACE;
+       timing->interlaced = (stream->timing.flags.INTERLACE != 0);
 
        hblank_start = stream->timing.h_total - stream->timing.h_front_porch;
 
@@ -137,7 +138,11 @@ static void 
populate_dml21_timing_config_from_stream_state(struct dml2_timing_cf
                                (timing->h_total * (long 
long)calc_max_hardware_v_total(stream)));
        }
 
-       timing->drr_config.min_refresh_uhz = 
max(stream->timing.min_refresh_in_uhz, min_hardware_refresh_in_uhz);
+       {
+               uint64_t min_refresh = 
max((uint64_t)stream->timing.min_refresh_in_uhz, min_hardware_refresh_in_uhz);
+               ASSERT(min_refresh <= ULONG_MAX);
+               timing->drr_config.min_refresh_uhz = (unsigned long)min_refresh;
+       }
 
        if 
(dml_ctx->config.callbacks.get_max_flickerless_instant_vtotal_increase &&
                        stream->ctx->dc->config.enable_fpo_flicker_detection == 
1)
@@ -697,7 +702,7 @@ unsigned int map_plane_to_dml21_display_cfg(const struct 
dml2_context *dml_ctx,
 
        if (!dml21_wrapper_get_plane_id(context, stream_id, plane, &plane_id)) {
                ASSERT(false);
-               return -1;
+               return UINT_MAX;
        }
 
        for (i = 0; i < __DML2_WRAPPER_MAX_STREAMS_PLANES__; i++) {
diff --git a/drivers/gpu/drm/amd/display/dc/dml2_0/dml21/dml21_utils.c 
b/drivers/gpu/drm/amd/display/dc/dml2_0/dml21/dml21_utils.c
index ab7ec24268be..4724b08c77e1 100644
--- a/drivers/gpu/drm/amd/display/dc/dml2_0/dml21/dml21_utils.c
+++ b/drivers/gpu/drm/amd/display/dc/dml2_0/dml21/dml21_utils.c
@@ -420,8 +420,12 @@ static unsigned int 
dml21_build_fams2_stream_programming_v2(const struct dc *dc,
                        type = static_base_state->stream_v1.base.type;
 
                        /* get information from context */
-                       static_base_state->stream_v1.base.num_planes = 
context->stream_status[dc_stream_idx].plane_count;
-                       static_base_state->stream_v1.base.otg_inst = 
context->stream_status[dc_stream_idx].primary_otg_inst;
+                       
ASSERT(context->stream_status[dc_stream_idx].plane_count >= 0 &&
+                                       
context->stream_status[dc_stream_idx].plane_count <= 0xFF);
+                       
ASSERT(context->stream_status[dc_stream_idx].primary_otg_inst >= 0 &&
+                                       
context->stream_status[dc_stream_idx].primary_otg_inst <= 0xFF);
+                       static_base_state->stream_v1.base.num_planes = 
(uint8_t)context->stream_status[dc_stream_idx].plane_count;
+                       static_base_state->stream_v1.base.otg_inst = 
(uint8_t)context->stream_status[dc_stream_idx].primary_otg_inst;
 
                        /* populate pipe masks for planes */
                        for (dc_plane_idx = 0; dc_plane_idx < 
context->stream_status[dc_stream_idx].plane_count; dc_plane_idx++) {
@@ -458,7 +462,9 @@ static unsigned int 
dml21_build_fams2_stream_programming_v2(const struct dc *dc,
                        switch (dc->debug.fams_version.minor) {
                        case 1:
                        default:
-                               
static_sub_state->stream_v1.sub_state.subvp.phantom_otg_inst = 
phantom_status->primary_otg_inst;
+                               ASSERT(phantom_status->primary_otg_inst >= 0 &&
+                                               
phantom_status->primary_otg_inst <= 0xFF);
+                               
static_sub_state->stream_v1.sub_state.subvp.phantom_otg_inst = 
(uint8_t)phantom_status->primary_otg_inst;
 
                                /* populate pipe masks for phantom planes */
                                for (dc_plane_idx = 0; dc_plane_idx < 
phantom_status->plane_count; dc_plane_idx++) {
@@ -516,7 +522,8 @@ void dml21_build_fams2_programming(const struct dc *dc,
                context->bw_ctx.bw.dcn.fams2_global_config.num_streams = 
num_fams2_streams;
        }
 
-       context->bw_ctx.bw.dcn.clk.fw_based_mclk_switching = 
context->bw_ctx.bw.dcn.fams2_global_config.features.bits.enable;
+       context->bw_ctx.bw.dcn.clk.fw_based_mclk_switching =
+                       
(context->bw_ctx.bw.dcn.fams2_global_config.features.bits.enable != 0);
 }
 
 bool dml21_is_plane1_enabled(enum dml2_source_format_class source_format)
diff --git a/drivers/gpu/drm/amd/display/dc/dml2_0/dml21/dml21_wrapper_fpu.c 
b/drivers/gpu/drm/amd/display/dc/dml2_0/dml21/dml21_wrapper_fpu.c
index f3abfdbe6805..cc992af6ac9c 100644
--- a/drivers/gpu/drm/amd/display/dc/dml2_0/dml21/dml21_wrapper_fpu.c
+++ b/drivers/gpu/drm/amd/display/dc/dml2_0/dml21/dml21_wrapper_fpu.c
@@ -297,7 +297,8 @@ void dml21_prepare_mcache_programming(struct dc *in_dc, 
struct dc_state *context
                memset(mcache_config, 0, sizeof(struct 
dml2_plane_mcache_configuration_descriptor));
                mcache_config->plane_descriptor = pln_prog->plane_descriptor;
                mcache_config->mcache_allocation = 
&context->bw_ctx.bw.dcn.mcache_allocations[dml_prog_idx];
-               mcache_config->num_pipes = pln_prog->num_dpps_required;
+               ASSERT(pln_prog->num_dpps_required <= 0x7F);
+               mcache_config->num_pipes = (char)pln_prog->num_dpps_required;
                l->build_mcache_programming_params.num_configurations++;
 
                if (pln_prog->num_dpps_required == 0) {
@@ -324,7 +325,8 @@ void dml21_prepare_mcache_programming(struct dc *in_dc, 
struct dc_state *context
                        memset(mcache_config, 0, sizeof(struct 
dml2_plane_mcache_configuration_descriptor));
                        mcache_config->plane_descriptor = 
pln_prog->plane_descriptor;
                        mcache_config->mcache_allocation = 
&context->bw_ctx.bw.dcn.mcache_allocations[dml_phantom_prog_idx];
-                       mcache_config->num_pipes = pln_prog->num_dpps_required;
+                       ASSERT(pln_prog->num_dpps_required <= 0x7F);
+                       mcache_config->num_pipes = 
(char)pln_prog->num_dpps_required;
                        l->build_mcache_programming_params.num_configurations++;
 
                        for (dc_pipe_index = 0; dc_pipe_index < num_pipes; 
dc_pipe_index++) {
diff --git a/drivers/gpu/drm/amd/display/dc/dml2_0/dml2_dc_resource_mgmt.c 
b/drivers/gpu/drm/amd/display/dc/dml2_0/dml2_dc_resource_mgmt.c
index 40f2f1ebab3a..6ef93c6fc1cd 100644
--- a/drivers/gpu/drm/amd/display/dc/dml2_0/dml2_dc_resource_mgmt.c
+++ b/drivers/gpu/drm/amd/display/dc/dml2_0/dml2_dc_resource_mgmt.c
@@ -366,7 +366,8 @@ static bool find_more_pipes_for_stream(struct dml2_context 
*ctx,
                if (!is_plane_using_pipe(pipe)) {
                        pipes_needed--;
                        // TODO: This doens't make sense really, pipe_idx 
should always be valid
-                       pipe->pipe_idx = preferred_pipe_candidates[i];
+                       ASSERT(preferred_pipe_candidates[i] <= 0xFF);
+                       pipe->pipe_idx = (uint8_t)preferred_pipe_candidates[i];
                        assigned_pipes[(*assigned_pipe_count)++] = 
pipe->pipe_idx;
                }
        }
@@ -382,7 +383,8 @@ static bool find_more_pipes_for_stream(struct dml2_context 
*ctx,
                if (!is_plane_using_pipe(pipe)) {
                        pipes_needed--;
                        // TODO: This doens't make sense really, pipe_idx 
should always be valid
-                       pipe->pipe_idx = i;
+                       ASSERT(i >= 0 && i <= 0xFF);
+                       pipe->pipe_idx = (uint8_t)i;
                        assigned_pipes[(*assigned_pipe_count)++] = 
pipe->pipe_idx;
                }
        }
@@ -393,7 +395,8 @@ static bool find_more_pipes_for_stream(struct dml2_context 
*ctx,
                if (!is_plane_using_pipe(pipe)) {
                        pipes_needed--;
                        // TODO: This doens't make sense really, pipe_idx 
should always be valid
-                       pipe->pipe_idx = last_resort_pipe_candidates[i];
+                       ASSERT(last_resort_pipe_candidates[i] <= 0xFF);
+                       pipe->pipe_idx = 
(uint8_t)last_resort_pipe_candidates[i];
                        assigned_pipes[(*assigned_pipe_count)++] = 
pipe->pipe_idx;
                }
        }
@@ -432,7 +435,8 @@ static bool find_more_free_pipes(struct dml2_context *ctx,
                if (is_pipe_free(pipe)) {
                        pipes_needed--;
                        // TODO: This doens't make sense really, pipe_idx 
should always be valid
-                       pipe->pipe_idx = preferred_pipe_candidates[i];
+                       ASSERT(preferred_pipe_candidates[i] <= 0xFF);
+                       pipe->pipe_idx = (uint8_t)preferred_pipe_candidates[i];
                        assigned_pipes[(*assigned_pipe_count)++] = 
pipe->pipe_idx;
                }
        }
@@ -448,7 +452,8 @@ static bool find_more_free_pipes(struct dml2_context *ctx,
                if (is_pipe_free(pipe)) {
                        pipes_needed--;
                        // TODO: This doens't make sense really, pipe_idx 
should always be valid
-                       pipe->pipe_idx = i;
+                       ASSERT(i >= 0 && i <= 0xFF);
+                       pipe->pipe_idx = (uint8_t)i;
                        assigned_pipes[(*assigned_pipe_count)++] = 
pipe->pipe_idx;
                }
        }
@@ -459,7 +464,8 @@ static bool find_more_free_pipes(struct dml2_context *ctx,
                if (is_pipe_free(pipe)) {
                        pipes_needed--;
                        // TODO: This doens't make sense really, pipe_idx 
should always be valid
-                       pipe->pipe_idx = last_resort_pipe_candidates[i];
+                       ASSERT(last_resort_pipe_candidates[i] <= 0xFF);
+                       pipe->pipe_idx = 
(uint8_t)last_resort_pipe_candidates[i];
                        assigned_pipes[(*assigned_pipe_count)++] = 
pipe->pipe_idx;
                }
        }
diff --git a/drivers/gpu/drm/amd/display/dc/dml2_0/dml2_mall_phantom.c 
b/drivers/gpu/drm/amd/display/dc/dml2_0/dml2_mall_phantom.c
index d56e58ce26c7..9bbe4e058be7 100644
--- a/drivers/gpu/drm/amd/display/dc/dml2_0/dml2_mall_phantom.c
+++ b/drivers/gpu/drm/amd/display/dc/dml2_0/dml2_mall_phantom.c
@@ -555,7 +555,8 @@ static bool subvp_vblank_schedulable(struct dml2_context 
*ctx, struct dc_state *
 
                if (!found && pipe_mall_type == SUBVP_NONE) {
                        // Found pipe which is not SubVP or Phantom (i.e. the 
VBLANK pipe).
-                       vblank_index = i;
+                       ASSERT(i <= 0xFF);
+                       vblank_index = (uint8_t)i;
                        found = true;
                }
 
diff --git a/drivers/gpu/drm/amd/display/dc/dml2_0/dml2_translation_helper.c 
b/drivers/gpu/drm/amd/display/dc/dml2_0/dml2_translation_helper.c
index 57f45b27de1d..cf3a69aba638 100644
--- a/drivers/gpu/drm/amd/display/dc/dml2_0/dml2_translation_helper.c
+++ b/drivers/gpu/drm/amd/display/dc/dml2_0/dml2_translation_helper.c
@@ -765,7 +765,7 @@ static void 
populate_dml_timing_cfg_from_stream_state(struct dml_timing_cfg_st *
                out->PixelClock[location] *= 2;
        out->HTotal[location] = in->timing.h_total;
        out->VTotal[location] = in->timing.v_total;
-       out->Interlace[location] = in->timing.flags.INTERLACE;
+       out->Interlace[location] = (in->timing.flags.INTERLACE != 0);
        hblank_start = in->timing.h_total - in->timing.h_front_porch;
        out->HBlankEnd[location] = hblank_start
                                        - in->timing.h_addressable
diff --git a/drivers/gpu/drm/amd/display/dc/dml2_0/dml2_utils.c 
b/drivers/gpu/drm/amd/display/dc/dml2_0/dml2_utils.c
index 9a33158b63bf..6c7cdf102906 100644
--- a/drivers/gpu/drm/amd/display/dc/dml2_0/dml2_utils.c
+++ b/drivers/gpu/drm/amd/display/dc/dml2_0/dml2_utils.c
@@ -255,7 +255,8 @@ static void populate_pipe_ctx_dlg_params_from_dml(struct 
pipe_ctx *pipe_ctx, str
        pipe_ctx->pipe_dlg_param.vupdate_width = 
dml_get_vupdate_width(mode_lib, pipe_idx);
        pipe_ctx->pipe_dlg_param.vready_offset = 
dml_get_vready_offset(mode_lib, pipe_idx);
 
-       pipe_ctx->pipe_dlg_param.otg_inst = pipe_ctx->stream_res.tg->inst;
+       ASSERT(pipe_ctx->stream_res.tg->inst >= 0 && 
pipe_ctx->stream_res.tg->inst <= 0xFF);
+       pipe_ctx->pipe_dlg_param.otg_inst = (unsigned 
char)pipe_ctx->stream_res.tg->inst;
 
        pipe_ctx->pipe_dlg_param.hactive = hactive;
        pipe_ctx->pipe_dlg_param.vactive = vactive;
-- 
2.43.0

Reply via email to