[PATCH v3 02/44] ASoC: Intel: avs: Include CPUID header at file scope

Ahmed S. Darwish posted 44 patches 3 months, 4 weeks ago
There is a newer version of this series
[PATCH v3 02/44] ASoC: Intel: avs: Include CPUID header at file scope
Posted by Ahmed S. Darwish 3 months, 4 weeks ago
Commit

    cbe37a4d2b3c ("ASoC: Intel: avs: Configure basefw on TGL-based platforms")

includes the main CPUID header from within a C function.  This obviously
works by luck and forbids valid refactorings inside the CPUID header.

Include the CPUID header at file scope instead.

Note, for the CPUID(0x15) leaf number, use CPUID_LEAF_TSC instead of
defining a custom local macro for it.

Signed-off-by: Ahmed S. Darwish <darwi@linutronix.de>
---
 sound/soc/intel/avs/tgl.c | 25 ++++++++++++++++---------
 1 file changed, 16 insertions(+), 9 deletions(-)

diff --git a/sound/soc/intel/avs/tgl.c b/sound/soc/intel/avs/tgl.c
index 9dbb3ad0954a..cf19d3a7ced2 100644
--- a/sound/soc/intel/avs/tgl.c
+++ b/sound/soc/intel/avs/tgl.c
@@ -10,8 +10,6 @@
 #include "avs.h"
 #include "messages.h"
 
-#define CPUID_TSC_LEAF 0x15
-
 static int avs_tgl_dsp_core_power(struct avs_dev *adev, u32 core_mask, bool power)
 {
 	core_mask &= AVS_MAIN_CORE_MASK;
@@ -39,22 +37,31 @@ static int avs_tgl_dsp_core_stall(struct avs_dev *adev, u32 core_mask, bool stal
 	return avs_dsp_core_stall(adev, core_mask, stall);
 }
 
+#ifdef CONFIG_X86
+#include <asm/cpuid/api.h>
+static unsigned int intel_crystal_freq_hz(void)
+{
+	return cpuid_ecx(CPUID_LEAF_TSC);
+}
+#else
+static unsigned int intel_crystal_freq_hz(void)
+{
+	return 0;
+}
+#endif /* !CONFIG_X86 */
+
 static int avs_tgl_config_basefw(struct avs_dev *adev)
 {
+	unsigned int freq = intel_crystal_freq_hz();
 	struct pci_dev *pci = adev->base.pci;
 	struct avs_bus_hwid hwid;
 	int ret;
-#ifdef CONFIG_X86
-	unsigned int ecx;
 
-#include <asm/cpuid/api.h>
-	ecx = cpuid_ecx(CPUID_TSC_LEAF);
-	if (ecx) {
-		ret = avs_ipc_set_fw_config(adev, 1, AVS_FW_CFG_XTAL_FREQ_HZ, sizeof(ecx), &ecx);
+	if (freq) {
+		ret = avs_ipc_set_fw_config(adev, 1, AVS_FW_CFG_XTAL_FREQ_HZ, sizeof(freq), &freq);
 		if (ret)
 			return AVS_IPC_RET(ret);
 	}
-#endif
 
 	hwid.device = pci->device;
 	hwid.subsystem = pci->subsystem_vendor | (pci->subsystem_device << 16);
-- 
2.49.0
Re: [PATCH v3 02/44] ASoC: Intel: avs: Include CPUID header at file scope
Posted by Borislav Petkov 3 months, 1 week ago
On Fri, Jun 13, 2025 at 01:39:28AM +0200, Ahmed S. Darwish wrote:
> +#ifdef CONFIG_X86
> +#include <asm/cpuid/api.h>
> +static unsigned int intel_crystal_freq_hz(void)
> +{
> +	return cpuid_ecx(CPUID_LEAF_TSC);

I'm guessing this will get eventually converted to the parsed struct in the
end?

I guess I'll have to continue reading and see... :)

-- 
Regards/Gruss,
    Boris.

https://people.kernel.org/tglx/notes-about-netiquette
Re: [PATCH v3 02/44] ASoC: Intel: avs: Include CPUID header at file scope
Posted by Ahmed S. Darwish 3 months ago
On Fri, 04 Jul 2025, Borislav Petkov wrote:
>
> I'm guessing this will get eventually converted to the parsed struct in
> the end?
>
> I guess I'll have to continue reading and see... :)
>

Yup, next iteration of this PQ shall also convert all the CPUID(0x15)
call sites, as they're quite simple :)
Re: [PATCH v3 02/44] ASoC: Intel: avs: Include CPUID header at file scope
Posted by Cezary Rojewski 3 months, 3 weeks ago
On 2025-06-13 1:39 AM, Ahmed S. Darwish wrote:
> Commit
> 
>      cbe37a4d2b3c ("ASoC: Intel: avs: Configure basefw on TGL-based platforms")
> 
> includes the main CPUID header from within a C function.  This obviously
> works by luck and forbids valid refactorings inside the CPUID header.
> 
> Include the CPUID header at file scope instead.
> 
> Note, for the CPUID(0x15) leaf number, use CPUID_LEAF_TSC instead of
> defining a custom local macro for it.


The existing location of #include isn't my best work, clearly. Thank you 
for addressing that, Ahmed. For the avs-driver bits:

Acked-by: Cezary Rojewski <cezary.rojewski@intel.com>

> Signed-off-by: Ahmed S. Darwish <darwi@linutronix.de>
> ---
>   sound/soc/intel/avs/tgl.c | 25 ++++++++++++++++---------
>   1 file changed, 16 insertions(+), 9 deletions(-)
> 
> diff --git a/sound/soc/intel/avs/tgl.c b/sound/soc/intel/avs/tgl.c
> index 9dbb3ad0954a..cf19d3a7ced2 100644
> --- a/sound/soc/intel/avs/tgl.c
> +++ b/sound/soc/intel/avs/tgl.c
> @@ -10,8 +10,6 @@
>   #include "avs.h"
>   #include "messages.h"
>   
> -#define CPUID_TSC_LEAF 0x15
> -
>   static int avs_tgl_dsp_core_power(struct avs_dev *adev, u32 core_mask, bool power)
>   {
>   	core_mask &= AVS_MAIN_CORE_MASK;
> @@ -39,22 +37,31 @@ static int avs_tgl_dsp_core_stall(struct avs_dev *adev, u32 core_mask, bool stal
>   	return avs_dsp_core_stall(adev, core_mask, stall);
>   }
>   
> +#ifdef CONFIG_X86
> +#include <asm/cpuid/api.h>
> +static unsigned int intel_crystal_freq_hz(void)
> +{
> +	return cpuid_ecx(CPUID_LEAF_TSC);
> +}
> +#else
> +static unsigned int intel_crystal_freq_hz(void)
> +{
> +	return 0;
> +}
> +#endif /* !CONFIG_X86 */
> +
>   static int avs_tgl_config_basefw(struct avs_dev *adev)
>   {
> +	unsigned int freq = intel_crystal_freq_hz();
>   	struct pci_dev *pci = adev->base.pci;
>   	struct avs_bus_hwid hwid;
>   	int ret;
> -#ifdef CONFIG_X86
> -	unsigned int ecx;
>   
> -#include <asm/cpuid/api.h>
> -	ecx = cpuid_ecx(CPUID_TSC_LEAF);
> -	if (ecx) {
> -		ret = avs_ipc_set_fw_config(adev, 1, AVS_FW_CFG_XTAL_FREQ_HZ, sizeof(ecx), &ecx);
> +	if (freq) {
> +		ret = avs_ipc_set_fw_config(adev, 1, AVS_FW_CFG_XTAL_FREQ_HZ, sizeof(freq), &freq);
>   		if (ret)
>   			return AVS_IPC_RET(ret);
>   	}
> -#endif
>   
>   	hwid.device = pci->device;
>   	hwid.subsystem = pci->subsystem_vendor | (pci->subsystem_device << 16);
Re: [PATCH v3 02/44] ASoC: Intel: avs: Include CPUID header at file scope
Posted by Ahmed S. Darwish 3 months ago
On Mon, 16 Jun 2025, Cezary Rojewski wrote:
>
> The existing location of #include isn't my best work, clearly. Thank
> you for addressing that, Ahmed. For the avs-driver bits:
>
> Acked-by: Cezary Rojewski <cezary.rojewski@intel.com>
>

Sure, no problem.  Thanks for the Acked-by :-)