From nobody Sat Dec 27 03:20:25 2025 Received: from galois.linutronix.de (Galois.linutronix.de [193.142.43.55]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id A3C86125C7; Sat, 23 Dec 2023 16:09:59 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=linutronix.de Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=linutronix.de Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=linutronix.de header.i=@linutronix.de header.b="TNqdHDZr"; dkim=permerror (0-bit key) header.d=linutronix.de header.i=@linutronix.de header.b="aOK+CAxz" Date: Sat, 23 Dec 2023 16:09:52 -0000 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linutronix.de; s=2020; t=1703347793; h=from:from:sender:sender:reply-to:reply-to:subject:subject:date:date: message-id:message-id:to:to:cc:cc:mime-version:mime-version: content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=fhI0GhLKOXGnd3a2lTztEQf93Oiytwnj2pfLpjcFvnM=; b=TNqdHDZrQrQc6uXbn0XbSgQvm2diftJX0SLqHurK1PV17buBGkhYYZyHF2GVvyxXMWV1q8 k0A6LMdXeHwtSUuW0EQGNojqJpe2ONV9r9UYlcVSx9/7ucn4UCFi7yoXW9EZs0HVrTu6ZY vMzgJ2EcpGblfoss5h/25gTkuVTuru77Sn65itLkUwocKy3vRdtH2qwcxKJbMqcdEDRX+9 8aKZnlYQmSITFiOT0AMMoMqSkqF/FUcCdjz/NeHXZG3e16YNqOLf59ClZVzx4XWQQ7Eysw hEZ4ZGbfH9Z6T4/hVgGCdk4yoZnprAtWsmdxBfXVTXjWHA0qvC8lGXCchqJ65A== DKIM-Signature: v=1; a=ed25519-sha256; c=relaxed/relaxed; d=linutronix.de; s=2020e; t=1703347793; h=from:from:sender:sender:reply-to:reply-to:subject:subject:date:date: message-id:message-id:to:to:cc:cc:mime-version:mime-version: content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=fhI0GhLKOXGnd3a2lTztEQf93Oiytwnj2pfLpjcFvnM=; b=aOK+CAxzjU/FwQqGOAtji9U6r2A4MG0bJAumtiGDtTJ9wXgZXYJ5FEPOzPjj+4LZwidPtz W2p6YM6eagxO9XBA== From: "tip-bot2 for Vincent Guittot" Sender: tip-bot2@linutronix.de Reply-to: linux-kernel@vger.kernel.org To: linux-tip-commits@vger.kernel.org Subject: [tip: sched/core] cpufreq/cppc: Move and rename cppc_cpufreq_{perf_to_khz|khz_to_perf}() Cc: Vincent Guittot , Ingo Molnar , Pierre Gondois , "Rafael J. Wysocki" , Viresh Kumar , x86@kernel.org, linux-kernel@vger.kernel.org In-Reply-To: <20231211104855.558096-6-vincent.guittot@linaro.org> References: <20231211104855.558096-6-vincent.guittot@linaro.org> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Message-ID: <170334779249.398.953606785352073949.tip-bot2@tip-bot2> Robot-ID: Robot-Unsubscribe: Contact to get blacklisted from these emails Precedence: bulk Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: quoted-printable The following commit has been merged into the sched/core branch of tip: Commit-ID: 50b813b147e9eb6546a1fc49d4e703e6d23691f2 Gitweb: https://git.kernel.org/tip/50b813b147e9eb6546a1fc49d4e703e6d= 23691f2 Author: Vincent Guittot AuthorDate: Mon, 11 Dec 2023 11:48:53 +01:00 Committer: Ingo Molnar CommitterDate: Sat, 23 Dec 2023 15:52:35 +01:00 cpufreq/cppc: Move and rename cppc_cpufreq_{perf_to_khz|khz_to_perf}() Move and rename cppc_cpufreq_perf_to_khz() and cppc_cpufreq_khz_to_perf() to use them outside cppc_cpufreq in topology_init_cpu_capacity_cppc(). Modify the interface to use struct cppc_perf_caps *caps instead of struct cppc_cpudata *cpu_data as we only use the fields of cppc_perf_caps. cppc_cpufreq was converting the lowest and nominal freq from MHz to kHz before using them. We move this conversion inside cppc_perf_to_khz and cppc_khz_to_perf to make them generic and usable outside cppc_cpufreq. No functional change Signed-off-by: Vincent Guittot Signed-off-by: Ingo Molnar Tested-by: Pierre Gondois Acked-by: Rafael J. Wysocki Acked-by: Viresh Kumar Link: https://lore.kernel.org/r/20231211104855.558096-6-vincent.guittot@lin= aro.org --- drivers/acpi/cppc_acpi.c | 104 ++++++++++++++++++++++++- drivers/cpufreq/cppc_cpufreq.c | 139 +++----------------------------- include/acpi/cppc_acpi.h | 2 +- 3 files changed, 123 insertions(+), 122 deletions(-) diff --git a/drivers/acpi/cppc_acpi.c b/drivers/acpi/cppc_acpi.c index 7ff269a..d155a86 100644 --- a/drivers/acpi/cppc_acpi.c +++ b/drivers/acpi/cppc_acpi.c @@ -39,6 +39,9 @@ #include #include #include +#include +#include +#include =20 #include =20 @@ -1760,3 +1763,104 @@ unsigned int cppc_get_transition_latency(int cpu_nu= m) return latency_ns; } EXPORT_SYMBOL_GPL(cppc_get_transition_latency); + +/* Minimum struct length needed for the DMI processor entry we want */ +#define DMI_ENTRY_PROCESSOR_MIN_LENGTH 48 + +/* Offset in the DMI processor structure for the max frequency */ +#define DMI_PROCESSOR_MAX_SPEED 0x14 + +/* Callback function used to retrieve the max frequency from DMI */ +static void cppc_find_dmi_mhz(const struct dmi_header *dm, void *private) +{ + const u8 *dmi_data =3D (const u8 *)dm; + u16 *mhz =3D (u16 *)private; + + if (dm->type =3D=3D DMI_ENTRY_PROCESSOR && + dm->length >=3D DMI_ENTRY_PROCESSOR_MIN_LENGTH) { + u16 val =3D (u16)get_unaligned((const u16 *) + (dmi_data + DMI_PROCESSOR_MAX_SPEED)); + *mhz =3D val > *mhz ? val : *mhz; + } +} + +/* Look up the max frequency in DMI */ +static u64 cppc_get_dmi_max_khz(void) +{ + u16 mhz =3D 0; + + dmi_walk(cppc_find_dmi_mhz, &mhz); + + /* + * Real stupid fallback value, just in case there is no + * actual value set. + */ + mhz =3D mhz ? mhz : 1; + + return KHZ_PER_MHZ * mhz; +} + +/* + * If CPPC lowest_freq and nominal_freq registers are exposed then we can + * use them to convert perf to freq and vice versa. The conversion is + * extrapolated as an affine function passing by the 2 points: + * - (Low perf, Low freq) + * - (Nominal perf, Nominal freq) + */ +unsigned int cppc_perf_to_khz(struct cppc_perf_caps *caps, unsigned int pe= rf) +{ + s64 retval, offset =3D 0; + static u64 max_khz; + u64 mul, div; + + if (caps->lowest_freq && caps->nominal_freq) { + mul =3D caps->nominal_freq - caps->lowest_freq; + mul *=3D KHZ_PER_MHZ; + div =3D caps->nominal_perf - caps->lowest_perf; + offset =3D caps->nominal_freq * KHZ_PER_MHZ - + div64_u64(caps->nominal_perf * mul, div); + } else { + if (!max_khz) + max_khz =3D cppc_get_dmi_max_khz(); + mul =3D max_khz; + div =3D caps->highest_perf; + } + + retval =3D offset + div64_u64(perf * mul, div); + if (retval >=3D 0) + return retval; + return 0; +} +EXPORT_SYMBOL_GPL(cppc_perf_to_khz); + +unsigned int cppc_khz_to_perf(struct cppc_perf_caps *caps, unsigned int fr= eq) +{ + s64 retval, offset =3D 0; + static u64 max_khz; + u64 mul, div; + + if (caps->lowest_freq && caps->nominal_freq) { + mul =3D caps->nominal_perf - caps->lowest_perf; + div =3D caps->nominal_freq - caps->lowest_freq; + /* + * We don't need to convert to kHz for computing offset and can + * directly use nominal_freq and lowest_freq as the div64_u64 + * will remove the frequency unit. + */ + offset =3D caps->nominal_perf - + div64_u64(caps->nominal_freq * mul, div); + /* But we need it for computing the perf level. */ + div *=3D KHZ_PER_MHZ; + } else { + if (!max_khz) + max_khz =3D cppc_get_dmi_max_khz(); + mul =3D caps->highest_perf; + div =3D max_khz; + } + + retval =3D offset + div64_u64(freq * mul, div); + if (retval >=3D 0) + return retval; + return 0; +} +EXPORT_SYMBOL_GPL(cppc_khz_to_perf); diff --git a/drivers/cpufreq/cppc_cpufreq.c b/drivers/cpufreq/cppc_cpufreq.c index fe08ca4..64420d9 100644 --- a/drivers/cpufreq/cppc_cpufreq.c +++ b/drivers/cpufreq/cppc_cpufreq.c @@ -16,7 +16,6 @@ #include #include #include -#include #include #include #include @@ -27,12 +26,6 @@ =20 #include =20 -/* Minimum struct length needed for the DMI processor entry we want */ -#define DMI_ENTRY_PROCESSOR_MIN_LENGTH 48 - -/* Offset in the DMI processor structure for the max frequency */ -#define DMI_PROCESSOR_MAX_SPEED 0x14 - /* * This list contains information parsed from per CPU ACPI _CPC and _PSD * structures: e.g. the highest and lowest supported performance, capabili= ties, @@ -291,97 +284,9 @@ static inline void cppc_freq_invariance_exit(void) } #endif /* CONFIG_ACPI_CPPC_CPUFREQ_FIE */ =20 -/* Callback function used to retrieve the max frequency from DMI */ -static void cppc_find_dmi_mhz(const struct dmi_header *dm, void *private) -{ - const u8 *dmi_data =3D (const u8 *)dm; - u16 *mhz =3D (u16 *)private; - - if (dm->type =3D=3D DMI_ENTRY_PROCESSOR && - dm->length >=3D DMI_ENTRY_PROCESSOR_MIN_LENGTH) { - u16 val =3D (u16)get_unaligned((const u16 *) - (dmi_data + DMI_PROCESSOR_MAX_SPEED)); - *mhz =3D val > *mhz ? val : *mhz; - } -} - -/* Look up the max frequency in DMI */ -static u64 cppc_get_dmi_max_khz(void) -{ - u16 mhz =3D 0; - - dmi_walk(cppc_find_dmi_mhz, &mhz); - - /* - * Real stupid fallback value, just in case there is no - * actual value set. - */ - mhz =3D mhz ? mhz : 1; - - return (1000 * mhz); -} - -/* - * If CPPC lowest_freq and nominal_freq registers are exposed then we can - * use them to convert perf to freq and vice versa. The conversion is - * extrapolated as an affine function passing by the 2 points: - * - (Low perf, Low freq) - * - (Nominal perf, Nominal perf) - */ -static unsigned int cppc_cpufreq_perf_to_khz(struct cppc_cpudata *cpu_data, - unsigned int perf) -{ - struct cppc_perf_caps *caps =3D &cpu_data->perf_caps; - s64 retval, offset =3D 0; - static u64 max_khz; - u64 mul, div; - - if (caps->lowest_freq && caps->nominal_freq) { - mul =3D caps->nominal_freq - caps->lowest_freq; - div =3D caps->nominal_perf - caps->lowest_perf; - offset =3D caps->nominal_freq - div64_u64(caps->nominal_perf * mul, div); - } else { - if (!max_khz) - max_khz =3D cppc_get_dmi_max_khz(); - mul =3D max_khz; - div =3D caps->highest_perf; - } - - retval =3D offset + div64_u64(perf * mul, div); - if (retval >=3D 0) - return retval; - return 0; -} - -static unsigned int cppc_cpufreq_khz_to_perf(struct cppc_cpudata *cpu_data, - unsigned int freq) -{ - struct cppc_perf_caps *caps =3D &cpu_data->perf_caps; - s64 retval, offset =3D 0; - static u64 max_khz; - u64 mul, div; - - if (caps->lowest_freq && caps->nominal_freq) { - mul =3D caps->nominal_perf - caps->lowest_perf; - div =3D caps->nominal_freq - caps->lowest_freq; - offset =3D caps->nominal_perf - div64_u64(caps->nominal_freq * mul, div); - } else { - if (!max_khz) - max_khz =3D cppc_get_dmi_max_khz(); - mul =3D caps->highest_perf; - div =3D max_khz; - } - - retval =3D offset + div64_u64(freq * mul, div); - if (retval >=3D 0) - return retval; - return 0; -} - static int cppc_cpufreq_set_target(struct cpufreq_policy *policy, unsigned int target_freq, unsigned int relation) - { struct cppc_cpudata *cpu_data =3D policy->driver_data; unsigned int cpu =3D policy->cpu; @@ -389,7 +294,7 @@ static int cppc_cpufreq_set_target(struct cpufreq_polic= y *policy, u32 desired_perf; int ret =3D 0; =20 - desired_perf =3D cppc_cpufreq_khz_to_perf(cpu_data, target_freq); + desired_perf =3D cppc_khz_to_perf(&cpu_data->perf_caps, target_freq); /* Return if it is exactly the same perf */ if (desired_perf =3D=3D cpu_data->perf_ctrls.desired_perf) return ret; @@ -417,7 +322,7 @@ static unsigned int cppc_cpufreq_fast_switch(struct cpu= freq_policy *policy, u32 desired_perf; int ret; =20 - desired_perf =3D cppc_cpufreq_khz_to_perf(cpu_data, target_freq); + desired_perf =3D cppc_khz_to_perf(&cpu_data->perf_caps, target_freq); cpu_data->perf_ctrls.desired_perf =3D desired_perf; ret =3D cppc_set_perf(cpu, &cpu_data->perf_ctrls); =20 @@ -530,7 +435,7 @@ static int cppc_get_cpu_power(struct device *cpu_dev, min_step =3D min_cap / CPPC_EM_CAP_STEP; max_step =3D max_cap / CPPC_EM_CAP_STEP; =20 - perf_prev =3D cppc_cpufreq_khz_to_perf(cpu_data, *KHz); + perf_prev =3D cppc_khz_to_perf(perf_caps, *KHz); step =3D perf_prev / perf_step; =20 if (step > max_step) @@ -550,8 +455,8 @@ static int cppc_get_cpu_power(struct device *cpu_dev, perf =3D step * perf_step; } =20 - *KHz =3D cppc_cpufreq_perf_to_khz(cpu_data, perf); - perf_check =3D cppc_cpufreq_khz_to_perf(cpu_data, *KHz); + *KHz =3D cppc_perf_to_khz(perf_caps, perf); + perf_check =3D cppc_khz_to_perf(perf_caps, *KHz); step_check =3D perf_check / perf_step; =20 /* @@ -561,8 +466,8 @@ static int cppc_get_cpu_power(struct device *cpu_dev, */ while ((*KHz =3D=3D prev_freq) || (step_check !=3D step)) { perf++; - *KHz =3D cppc_cpufreq_perf_to_khz(cpu_data, perf); - perf_check =3D cppc_cpufreq_khz_to_perf(cpu_data, *KHz); + *KHz =3D cppc_perf_to_khz(perf_caps, perf); + perf_check =3D cppc_khz_to_perf(perf_caps, *KHz); step_check =3D perf_check / perf_step; } =20 @@ -591,7 +496,7 @@ static int cppc_get_cpu_cost(struct device *cpu_dev, un= signed long KHz, perf_caps =3D &cpu_data->perf_caps; max_cap =3D arch_scale_cpu_capacity(cpu_dev->id); =20 - perf_prev =3D cppc_cpufreq_khz_to_perf(cpu_data, KHz); + perf_prev =3D cppc_khz_to_perf(perf_caps, KHz); perf_step =3D CPPC_EM_CAP_STEP * perf_caps->highest_perf / max_cap; step =3D perf_prev / perf_step; =20 @@ -679,10 +584,6 @@ static struct cppc_cpudata *cppc_cpufreq_get_cpu_data(= unsigned int cpu) goto free_mask; } =20 - /* Convert the lowest and nominal freq from MHz to KHz */ - cpu_data->perf_caps.lowest_freq *=3D 1000; - cpu_data->perf_caps.nominal_freq *=3D 1000; - list_add(&cpu_data->node, &cpu_data_list); =20 return cpu_data; @@ -724,20 +625,16 @@ static int cppc_cpufreq_cpu_init(struct cpufreq_polic= y *policy) * Set min to lowest nonlinear perf to avoid any efficiency penalty (see * Section 8.4.7.1.1.5 of ACPI 6.1 spec) */ - policy->min =3D cppc_cpufreq_perf_to_khz(cpu_data, - caps->lowest_nonlinear_perf); - policy->max =3D cppc_cpufreq_perf_to_khz(cpu_data, - caps->nominal_perf); + policy->min =3D cppc_perf_to_khz(caps, caps->lowest_nonlinear_perf); + policy->max =3D cppc_perf_to_khz(caps, caps->nominal_perf); =20 /* * Set cpuinfo.min_freq to Lowest to make the full range of performance * available if userspace wants to use any perf between lowest & lowest * nonlinear perf */ - policy->cpuinfo.min_freq =3D cppc_cpufreq_perf_to_khz(cpu_data, - caps->lowest_perf); - policy->cpuinfo.max_freq =3D cppc_cpufreq_perf_to_khz(cpu_data, - caps->nominal_perf); + policy->cpuinfo.min_freq =3D cppc_perf_to_khz(caps, caps->lowest_perf); + policy->cpuinfo.max_freq =3D cppc_perf_to_khz(caps, caps->nominal_perf); =20 policy->transition_delay_us =3D cppc_cpufreq_get_transition_delay_us(cpu); policy->shared_type =3D cpu_data->shared_type; @@ -773,7 +670,7 @@ static int cppc_cpufreq_cpu_init(struct cpufreq_policy = *policy) boost_supported =3D true; =20 /* Set policy->cur to max now. The governors will adjust later. */ - policy->cur =3D cppc_cpufreq_perf_to_khz(cpu_data, caps->highest_perf); + policy->cur =3D cppc_perf_to_khz(caps, caps->highest_perf); cpu_data->perf_ctrls.desired_perf =3D caps->highest_perf; =20 ret =3D cppc_set_perf(cpu, &cpu_data->perf_ctrls); @@ -863,7 +760,7 @@ static unsigned int cppc_cpufreq_get_rate(unsigned int = cpu) delivered_perf =3D cppc_perf_from_fbctrs(cpu_data, &fb_ctrs_t0, &fb_ctrs_t1); =20 - return cppc_cpufreq_perf_to_khz(cpu_data, delivered_perf); + return cppc_perf_to_khz(&cpu_data->perf_caps, delivered_perf); } =20 static int cppc_cpufreq_set_boost(struct cpufreq_policy *policy, int state) @@ -878,11 +775,9 @@ static int cppc_cpufreq_set_boost(struct cpufreq_polic= y *policy, int state) } =20 if (state) - policy->max =3D cppc_cpufreq_perf_to_khz(cpu_data, - caps->highest_perf); + policy->max =3D cppc_perf_to_khz(caps, caps->highest_perf); else - policy->max =3D cppc_cpufreq_perf_to_khz(cpu_data, - caps->nominal_perf); + policy->max =3D cppc_perf_to_khz(caps, caps->nominal_perf); policy->cpuinfo.max_freq =3D policy->max; =20 ret =3D freq_qos_update_request(policy->max_freq_req, policy->max); @@ -937,7 +832,7 @@ static unsigned int hisi_cppc_cpufreq_get_rate(unsigned= int cpu) if (ret < 0) return -EIO; =20 - return cppc_cpufreq_perf_to_khz(cpu_data, desired_perf); + return cppc_perf_to_khz(&cpu_data->perf_caps, desired_perf); } =20 static void cppc_check_hisi_workaround(void) diff --git a/include/acpi/cppc_acpi.h b/include/acpi/cppc_acpi.h index 6126c97..3a0995f 100644 --- a/include/acpi/cppc_acpi.h +++ b/include/acpi/cppc_acpi.h @@ -144,6 +144,8 @@ extern int cppc_set_perf(int cpu, struct cppc_perf_ctrl= s *perf_ctrls); extern int cppc_set_enable(int cpu, bool enable); extern int cppc_get_perf_caps(int cpu, struct cppc_perf_caps *caps); extern bool cppc_perf_ctrs_in_pcc(void); +extern unsigned int cppc_perf_to_khz(struct cppc_perf_caps *caps, unsigned= int perf); +extern unsigned int cppc_khz_to_perf(struct cppc_perf_caps *caps, unsigned= int freq); extern bool acpi_cpc_valid(void); extern bool cppc_allow_fast_switch(void); extern int acpi_get_psd_map(unsigned int cpu, struct cppc_cpudata *cpu_dat= a);