This can avoid them to be handled in a wrong way without notice.
Since not all SMU messages/clocks are supported on every SMU11 ASIC.
Signed-off-by: Evan Quan <evan.quan@amd.com>
Reviewed-by: Feifei Xu <Feifei.Xu@amd.com>
Signed-off-by: Alex Deucher <alexander.deucher@amd.com>
        int ret = 0;
        int table_id = smu_table_get_index(smu, table_index);
 
-       if (!table_data || table_id >= smu_table->table_count)
+       if (!table_data || table_id >= smu_table->table_count || table_id < 0)
                return -EINVAL;
 
        table = &smu_table->tables[table_index];
 int smu_feature_is_enabled(struct smu_context *smu, enum smu_feature_mask mask)
 {
        struct smu_feature *feature = &smu->smu_feature;
-       uint32_t feature_id;
+       int feature_id;
        int ret = 0;
 
        feature_id = smu_feature_get_index(smu, mask);
+       if (feature_id < 0)
+               return 0;
 
        WARN_ON(feature_id > feature->feature_num);
 
                            bool enable)
 {
        struct smu_feature *feature = &smu->smu_feature;
-       uint32_t feature_id;
+       int feature_id;
        int ret = 0;
 
        feature_id = smu_feature_get_index(smu, mask);
+       if (feature_id < 0)
+               return -EINVAL;
 
        WARN_ON(feature_id > feature->feature_num);
 
 int smu_feature_is_supported(struct smu_context *smu, enum smu_feature_mask mask)
 {
        struct smu_feature *feature = &smu->smu_feature;
-       uint32_t feature_id;
+       int feature_id;
        int ret = 0;
 
        feature_id = smu_feature_get_index(smu, mask);
+       if (feature_id < 0)
+               return 0;
 
        WARN_ON(feature_id > feature->feature_num);
 
                              bool enable)
 {
        struct smu_feature *feature = &smu->smu_feature;
-       uint32_t feature_id;
+       int feature_id;
        int ret = 0;
 
        feature_id = smu_feature_get_index(smu, mask);
+       if (feature_id < 0)
+               return -EINVAL;
 
        WARN_ON(feature_id > feature->feature_num);
 
 
                return -EINVAL;
 
        mapping = navi10_message_map[index];
-       if (!(mapping.valid_mapping))
+       if (!(mapping.valid_mapping)) {
+               pr_warn("Unsupported SMU message: %d\n", index);
                return -EINVAL;
+       }
 
        return mapping.map_to;
 }
                return -EINVAL;
 
        mapping = navi10_clk_map[index];
-       if (!(mapping.valid_mapping))
+       if (!(mapping.valid_mapping)) {
+               pr_warn("Unsupported SMU clock: %d\n", index);
                return -EINVAL;
+       }
 
        return mapping.map_to;
 }
                return -EINVAL;
 
        mapping = navi10_feature_mask_map[index];
-       if (!(mapping.valid_mapping))
+       if (!(mapping.valid_mapping)) {
+               pr_warn("Unsupported SMU feature: %d\n", index);
                return -EINVAL;
+       }
 
        return mapping.map_to;
 }
                return -EINVAL;
 
        mapping = navi10_table_map[index];
-       if (!(mapping.valid_mapping))
+       if (!(mapping.valid_mapping)) {
+               pr_warn("Unsupported SMU table: %d\n", index);
                return -EINVAL;
+       }
 
        return mapping.map_to;
 }
                return -EINVAL;
 
        mapping = navi10_pwr_src_map[index];
-       if (!(mapping.valid_mapping))
+       if (!(mapping.valid_mapping)) {
+               pr_warn("Unsupported power source: %d\n", index);
                return -EINVAL;
+       }
 
        return mapping.map_to;
 }
                return -EINVAL;
 
        mapping = navi10_workload_map[profile];
-       if (!(mapping.valid_mapping))
+       if (!(mapping.valid_mapping)) {
+               pr_warn("Unsupported workload: %d\n", (int)profile);
                return -EINVAL;
+       }
 
        return mapping.map_to;
 }
 {
        DpmActivityMonitorCoeffInt_t activity_monitor;
        uint32_t i, size = 0;
-       uint16_t workload_type = 0;
+       int16_t workload_type = 0;
        static const char *profile_name[] = {
                                        "BOOTUP_DEFAULT",
                                        "3D_FULL_SCREEN",
        for (i = 0; i <= PP_SMC_POWER_PROFILE_CUSTOM; i++) {
                /* conv PP_SMC_POWER_PROFILE* to WORKLOAD_PPLIB_*_BIT */
                workload_type = smu_workload_get_type(smu, i);
+               if (workload_type < 0)
+                       return -EINVAL;
+
                result = smu_update_table(smu,
                                          SMU_TABLE_ACTIVITY_MONITOR_COEFF, workload_type,
                                          (void *)(&activity_monitor), false);
 
        /* conv PP_SMC_POWER_PROFILE* to WORKLOAD_PPLIB_*_BIT */
        workload_type = smu_workload_get_type(smu, smu->power_profile_mode);
+       if (workload_type < 0)
+               return -EINVAL;
        smu_send_smc_msg_with_param(smu, SMU_MSG_SetWorkloadMask,
                                    1 << workload_type);
 
 
                                    enum smu_clk_type clock_select)
 {
        int ret = 0;
+       int clk_id;
 
        if (!smu->pm_enabled)
                return ret;
+
+       clk_id = smu_clk_get_index(smu, clock_select);
+       if (clk_id < 0)
+               return -EINVAL;
+
        ret = smu_send_smc_msg_with_param(smu, SMU_MSG_GetDcModeMaxDpmFreq,
-                                         smu_clk_get_index(smu, clock_select) << 16);
+                                         clk_id << 16);
        if (ret) {
                pr_err("[GetMaxSustainableClock] Failed to get max DC clock from SMC!");
                return ret;
 
        /* if DC limit is zero, return AC limit */
        ret = smu_send_smc_msg_with_param(smu, SMU_MSG_GetMaxDpmFreq,
-                                         smu_clk_get_index(smu, clock_select) << 16);
+                                         clk_id << 16);
        if (ret) {
                pr_err("[GetMaxSustainableClock] failed to get max AC clock from SMC!");
                return ret;
                                     bool get_default)
 {
        int ret = 0;
+       int power_src;
+
+       power_src = smu_power_get_index(smu, SMU_POWER_SOURCE_AC);
+       if (power_src < 0)
+               return -EINVAL;
 
        if (get_default) {
                mutex_lock(&smu->mutex);
                mutex_unlock(&smu->mutex);
        } else {
                ret = smu_send_smc_msg_with_param(smu, SMU_MSG_GetPptLimit,
-                       smu_power_get_index(smu, SMU_POWER_SOURCE_AC) << 16);
+                       power_src << 16);
                if (ret) {
                        pr_err("[%s] get PPT limit failed!", __func__);
                        return ret;
 {
        int ret = 0;
        uint32_t freq = 0;
+       int asic_clk_id;
 
        if (clk_id >= SMU_CLK_COUNT || !value)
                return -EINVAL;
 
+       asic_clk_id = smu_clk_get_index(smu, clk_id);
+       if (asic_clk_id < 0)
+               return -EINVAL;
+
        /* if don't has GetDpmClockFreq Message, try get current clock by SmuMetrics_t */
-       if (smu_msg_get_index(smu, SMU_MSG_GetDpmClockFreq) == 0)
+       if (smu_msg_get_index(smu, SMU_MSG_GetDpmClockFreq) < 0)
                ret =  smu_get_current_clk_freq_by_table(smu, clk_id, &freq);
        else {
                ret = smu_send_smc_msg_with_param(smu, SMU_MSG_GetDpmClockFreq,
-                                                 (smu_clk_get_index(smu, clk_id) << 16));
+                                                 (asic_clk_id << 16));
                if (ret)
                        return ret;
 
        int ret = 0;
        enum smu_clk_type clk_select = 0;
        uint32_t clk_freq = clock_req->clock_freq_in_khz / 1000;
+       int clk_id;
 
        if (!smu->pm_enabled)
                return -EINVAL;
                if (ret)
                        goto failed;
 
+               clk_id = smu_clk_get_index(smu, clk_select);
+               if (clk_id < 0) {
+                       ret = -EINVAL;
+                       goto failed;
+               }
+
                mutex_lock(&smu->mutex);
                ret = smu_send_smc_msg_with_param(smu, SMU_MSG_SetHardMinByFreq,
-                       (smu_clk_get_index(smu, clk_select) << 16) | clk_freq);
+                       (clk_id << 16) | clk_freq);
                mutex_unlock(&smu->mutex);
        }
 
 
                return -EINVAL;
 
        mapping = vega20_table_map[index];
-       if (!(mapping.valid_mapping))
+       if (!(mapping.valid_mapping)) {
+               pr_warn("Unsupported SMU table: %d\n", index);
                return -EINVAL;
+       }
 
        return mapping.map_to;
 }
                return -EINVAL;
 
        mapping = vega20_pwr_src_map[index];
-       if (!(mapping.valid_mapping))
+       if (!(mapping.valid_mapping)) {
+               pr_warn("Unsupported power source: %d\n", index);
                return -EINVAL;
+       }
 
        return mapping.map_to;
 }
                return -EINVAL;
 
        mapping = vega20_feature_mask_map[index];
-       if (!(mapping.valid_mapping))
+       if (!(mapping.valid_mapping)) {
+               pr_warn("Unsupported SMU feature: %d\n", index);
                return -EINVAL;
+       }
 
        return mapping.map_to;
 }
                return -EINVAL;
 
        mapping = vega20_clk_map[index];
-       if (!(mapping.valid_mapping))
+       if (!(mapping.valid_mapping)) {
+               pr_warn("Unsupported SMU clock: %d\n", index);
                return -EINVAL;
+       }
 
        return mapping.map_to;
 }
                return -EINVAL;
 
        mapping = vega20_message_map[index];
-       if (!(mapping.valid_mapping))
+       if (!(mapping.valid_mapping)) {
+               pr_warn("Unsupported SMU message: %d\n", index);
                return -EINVAL;
+       }
 
        return mapping.map_to;
 }
                return -EINVAL;
 
        mapping = vega20_workload_map[profile];
-       if (!(mapping.valid_mapping))
+       if (!(mapping.valid_mapping)) {
+               pr_warn("Unsupported SMU workload: %d\n", (int)profile);
                return -EINVAL;
+       }
 
        return mapping.map_to;
 }
 {
        DpmActivityMonitorCoeffInt_t activity_monitor;
        uint32_t i, size = 0;
-       uint16_t workload_type = 0;
+       int16_t workload_type = 0;
        static const char *profile_name[] = {
                                        "BOOTUP_DEFAULT",
                                        "3D_FULL_SCREEN",
        for (i = 0; i <= PP_SMC_POWER_PROFILE_CUSTOM; i++) {
                /* conv PP_SMC_POWER_PROFILE* to WORKLOAD_PPLIB_*_BIT */
                workload_type = smu_workload_get_type(smu, i);
+               if (workload_type < 0)
+                       return -EINVAL;
+
                result = smu_update_table(smu,
                                          SMU_TABLE_ACTIVITY_MONITOR_COEFF, workload_type,
                                          (void *)(&activity_monitor), false);
 
        /* conv PP_SMC_POWER_PROFILE* to WORKLOAD_PPLIB_*_BIT */
        workload_type = smu_workload_get_type(smu, smu->power_profile_mode);
+       if (workload_type < 0)
+               return -EINVAL;
        smu_send_smc_msg_with_param(smu, SMU_MSG_SetWorkloadMask,
                                    1 << workload_type);