Skip to content

Commit 620136c

Browse files
dhananjay-AMDsuperm1
authored andcommitted
cpufreq/amd-pstate: Modularize perf<->freq conversion
Delegate the perf<->frequency conversion to helper functions to reduce code duplication, and improve readability. Signed-off-by: Dhananjay Ugwekar <dhananjay.ugwekar@amd.com> Reviewed-by: Mario Limonciello <mario.limonciello@amd.com> Reviewed-by: Gautham R. Shenoy <gautham.shenoy@amd.com> Link: https://lore.kernel.org/r/20250205112523.201101-8-dhananjay.ugwekar@amd.com Signed-off-by: Mario Limonciello <mario.limonciello@amd.com>
1 parent 555bbe6 commit 620136c

1 file changed

Lines changed: 30 additions & 27 deletions

File tree

drivers/cpufreq/amd-pstate.c

Lines changed: 30 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -142,6 +142,20 @@ static struct quirk_entry quirk_amd_7k62 = {
142142
.lowest_freq = 550,
143143
};
144144

145+
static inline u8 freq_to_perf(struct amd_cpudata *cpudata, unsigned int freq_val)
146+
{
147+
u8 perf_val = DIV_ROUND_UP_ULL((u64)freq_val * cpudata->nominal_perf,
148+
cpudata->nominal_freq);
149+
150+
return clamp_t(u8, perf_val, cpudata->lowest_perf, cpudata->highest_perf);
151+
}
152+
153+
static inline u32 perf_to_freq(struct amd_cpudata *cpudata, u8 perf_val)
154+
{
155+
return DIV_ROUND_UP_ULL((u64)cpudata->nominal_freq * perf_val,
156+
cpudata->nominal_perf);
157+
}
158+
145159
static int __init dmi_matched_7k62_bios_bug(const struct dmi_system_id *dmi)
146160
{
147161
/**
@@ -534,14 +548,12 @@ static inline bool amd_pstate_sample(struct amd_cpudata *cpudata)
534548
static void amd_pstate_update(struct amd_cpudata *cpudata, u8 min_perf,
535549
u8 des_perf, u8 max_perf, bool fast_switch, int gov_flags)
536550
{
537-
unsigned long max_freq;
538551
struct cpufreq_policy *policy = cpufreq_cpu_get(cpudata->cpu);
539552
u8 nominal_perf = READ_ONCE(cpudata->nominal_perf);
540553

541554
des_perf = clamp_t(u8, des_perf, min_perf, max_perf);
542555

543-
max_freq = READ_ONCE(cpudata->max_limit_freq);
544-
policy->cur = div_u64(des_perf * max_freq, max_perf);
556+
policy->cur = perf_to_freq(cpudata, des_perf);
545557

546558
if ((cppc_state == AMD_PSTATE_GUIDED) && (gov_flags & CPUFREQ_GOV_DYNAMIC_SWITCHING)) {
547559
min_perf = des_perf;
@@ -591,14 +603,11 @@ static int amd_pstate_verify(struct cpufreq_policy_data *policy_data)
591603

592604
static int amd_pstate_update_min_max_limit(struct cpufreq_policy *policy)
593605
{
594-
u8 max_limit_perf, min_limit_perf, max_perf;
595-
u32 max_freq;
606+
u8 max_limit_perf, min_limit_perf;
596607
struct amd_cpudata *cpudata = policy->driver_data;
597608

598-
max_perf = READ_ONCE(cpudata->highest_perf);
599-
max_freq = READ_ONCE(cpudata->max_freq);
600-
max_limit_perf = div_u64(policy->max * max_perf, max_freq);
601-
min_limit_perf = div_u64(policy->min * max_perf, max_freq);
609+
max_limit_perf = freq_to_perf(cpudata, policy->max);
610+
min_limit_perf = freq_to_perf(cpudata, policy->min);
602611

603612
if (cpudata->policy == CPUFREQ_POLICY_PERFORMANCE)
604613
min_limit_perf = min(cpudata->nominal_perf, max_limit_perf);
@@ -616,21 +625,15 @@ static int amd_pstate_update_freq(struct cpufreq_policy *policy,
616625
{
617626
struct cpufreq_freqs freqs;
618627
struct amd_cpudata *cpudata = policy->driver_data;
619-
u8 des_perf, cap_perf;
620-
621-
if (!cpudata->max_freq)
622-
return -ENODEV;
628+
u8 des_perf;
623629

624630
if (policy->min != cpudata->min_limit_freq || policy->max != cpudata->max_limit_freq)
625631
amd_pstate_update_min_max_limit(policy);
626632

627-
cap_perf = READ_ONCE(cpudata->highest_perf);
628-
629633
freqs.old = policy->cur;
630634
freqs.new = target_freq;
631635

632-
des_perf = DIV_ROUND_CLOSEST(target_freq * cap_perf,
633-
cpudata->max_freq);
636+
des_perf = freq_to_perf(cpudata, target_freq);
634637

635638
WARN_ON(fast_switch && !policy->fast_switch_enabled);
636639
/*
@@ -905,7 +908,6 @@ static int amd_pstate_init_freq(struct amd_cpudata *cpudata)
905908
{
906909
int ret;
907910
u32 min_freq, max_freq;
908-
u8 highest_perf, nominal_perf, lowest_nonlinear_perf;
909911
u32 nominal_freq, lowest_nonlinear_freq;
910912
struct cppc_perf_caps cppc_perf;
911913

@@ -923,16 +925,17 @@ static int amd_pstate_init_freq(struct amd_cpudata *cpudata)
923925
else
924926
nominal_freq = cppc_perf.nominal_freq;
925927

926-
highest_perf = READ_ONCE(cpudata->highest_perf);
927-
nominal_perf = READ_ONCE(cpudata->nominal_perf);
928-
max_freq = div_u64((u64)highest_perf * nominal_freq, nominal_perf);
928+
min_freq *= 1000;
929+
nominal_freq *= 1000;
930+
931+
WRITE_ONCE(cpudata->nominal_freq, nominal_freq);
932+
WRITE_ONCE(cpudata->min_freq, min_freq);
933+
934+
max_freq = perf_to_freq(cpudata, cpudata->highest_perf);
935+
lowest_nonlinear_freq = perf_to_freq(cpudata, cpudata->lowest_nonlinear_perf);
929936

930-
lowest_nonlinear_perf = READ_ONCE(cpudata->lowest_nonlinear_perf);
931-
lowest_nonlinear_freq = div_u64((u64)nominal_freq * lowest_nonlinear_perf, nominal_perf);
932-
WRITE_ONCE(cpudata->min_freq, min_freq * 1000);
933-
WRITE_ONCE(cpudata->lowest_nonlinear_freq, lowest_nonlinear_freq * 1000);
934-
WRITE_ONCE(cpudata->nominal_freq, nominal_freq * 1000);
935-
WRITE_ONCE(cpudata->max_freq, max_freq * 1000);
937+
WRITE_ONCE(cpudata->lowest_nonlinear_freq, lowest_nonlinear_freq);
938+
WRITE_ONCE(cpudata->max_freq, max_freq);
936939

937940
/**
938941
* Below values need to be initialized correctly, otherwise driver will fail to load

0 commit comments

Comments
 (0)