[PATCH v7 07/12] platform/x86: hfi: add online and offline callback support

Mario Limonciello posted 12 patches 1 year, 2 months ago
There is a newer version of this series
[PATCH v7 07/12] platform/x86: hfi: add online and offline callback support
Posted by Mario Limonciello 1 year, 2 months ago
From: Perry Yuan <Perry.Yuan@amd.com>

There are some firmware parameters that need to be configured
when a CPU core is brought online or offline.

when CPU is online, it will initialize the workload classification
parameters to CPU firmware which will trigger the workload class ID
updating function.

Once the CPU is going to offline, it will need to disable the workload
classification function and clear the history.

Reviewed-by: Gautham R. Shenoy <gautham.shenoy@amd.com>
Signed-off-by: Perry Yuan <Perry.Yuan@amd.com>
Co-developed-by: Mario Limonciello <mario.limonciello@amd.com>
Signed-off-by: Mario Limonciello <mario.limonciello@amd.com>
---
v7:
 * move mutex to this patch
---
 drivers/platform/x86/amd/hfi/hfi.c | 81 ++++++++++++++++++++++++++++++
 1 file changed, 81 insertions(+)

diff --git a/drivers/platform/x86/amd/hfi/hfi.c b/drivers/platform/x86/amd/hfi/hfi.c
index 9671f07c76390..922a9ff0d447a 100644
--- a/drivers/platform/x86/amd/hfi/hfi.c
+++ b/drivers/platform/x86/amd/hfi/hfi.c
@@ -113,6 +113,8 @@ struct amd_hfi_cpuinfo {
 
 static DEFINE_PER_CPU(struct amd_hfi_cpuinfo, amd_hfi_cpuinfo) = {.class_index = -1};
 
+static DEFINE_MUTEX(hfi_cpuinfo_lock);
+
 static int find_cpu_index_by_apicid(unsigned int target_apicid)
 {
 	int cpu_index;
@@ -235,6 +237,80 @@ static int amd_set_hfi_ipcc_score(struct amd_hfi_cpuinfo *hfi_cpuinfo, int cpu)
 	return 0;
 }
 
+static int amd_hfi_set_state(unsigned int cpu, bool state)
+{
+	int ret;
+
+	ret = wrmsrl_on_cpu(cpu, AMD_WORKLOAD_CLASS_CONFIG, state);
+	if (ret)
+		return ret;
+
+	return wrmsrl_on_cpu(cpu, AMD_WORKLOAD_HRST, 0x1);
+}
+
+/**
+ * amd_hfi_online() - Enable workload classification on @cpu
+ * @cpu: CPU in which the workload classification will be enabled
+ *
+ * Return: 0 on success, negative error code on failure
+ */
+static int amd_hfi_online(unsigned int cpu)
+{
+	struct amd_hfi_cpuinfo *hfi_info = per_cpu_ptr(&amd_hfi_cpuinfo, cpu);
+	struct amd_hfi_classes *hfi_classes;
+	int ret;
+
+	if (WARN_ON_ONCE(!hfi_info))
+		return -EINVAL;
+
+	/*
+	 * Check if @cpu as an associated, initialized and ranking data must be filled
+	 */
+	hfi_classes = hfi_info->amd_hfi_classes;
+	if (!hfi_classes)
+		return -EINVAL;
+
+	guard(mutex)(&hfi_cpuinfo_lock);
+
+	if (!zalloc_cpumask_var(&hfi_info->cpus, GFP_KERNEL))
+		return -ENOMEM;
+
+	cpumask_set_cpu(cpu, hfi_info->cpus);
+
+	ret = amd_hfi_set_state(cpu, true);
+	if (ret)
+		pr_err("WCT enable failed for CPU %d\n", cpu);
+
+	return ret;
+}
+
+/**
+ * amd_hfi_offline() - Disable workload classification on @cpu
+ * @cpu: CPU in which the workload classification will be disabled
+ *
+ * Remove @cpu from those covered by its HFI instance.
+ *
+ * Return: 0 on success, negative error code on failure
+ */
+static int amd_hfi_offline(unsigned int cpu)
+{
+	struct amd_hfi_cpuinfo *hfi_info = &per_cpu(amd_hfi_cpuinfo, cpu);
+	int ret;
+
+	if (WARN_ON_ONCE(!hfi_info))
+		return -EINVAL;
+
+	guard(mutex)(&hfi_cpuinfo_lock);
+
+	ret = amd_hfi_set_state(cpu, false);
+	if (ret)
+		pr_err("WCT disable failed for CPU %d\n", cpu);
+
+	free_cpumask_var(hfi_info->cpus);
+
+	return ret;
+}
+
 static int update_hfi_ipcc_scores(void)
 {
 	int cpu;
@@ -340,6 +416,11 @@ static int amd_hfi_probe(struct platform_device *pdev)
 	if (ret)
 		return ret;
 
+	ret = cpuhp_setup_state(CPUHP_AP_ONLINE_DYN, "x86/amd_hfi:online",
+				amd_hfi_online, amd_hfi_offline);
+	if (ret < 0)
+		return ret;
+
 	return 0;
 }
 
-- 
2.43.0
Re: [PATCH v7 07/12] platform/x86: hfi: add online and offline callback support
Posted by Peter Zijlstra 1 year, 2 months ago
On Sat, Nov 30, 2024 at 08:06:58AM -0600, Mario Limonciello wrote:

> @@ -340,6 +416,11 @@ static int amd_hfi_probe(struct platform_device *pdev)
>  	if (ret)
>  		return ret;
>  
> +	ret = cpuhp_setup_state(CPUHP_AP_ONLINE_DYN, "x86/amd_hfi:online",
> +				amd_hfi_online, amd_hfi_offline);

By using online notifier, you will already have tasks running on this
CPU before you initialize the HFI bits, is that okay?
Re: [PATCH v7 07/12] platform/x86: hfi: add online and offline callback support
Posted by Mario Limonciello 1 year, 2 months ago
On 12/2/2024 05:38, Peter Zijlstra wrote:
> On Sat, Nov 30, 2024 at 08:06:58AM -0600, Mario Limonciello wrote:
> 
>> @@ -340,6 +416,11 @@ static int amd_hfi_probe(struct platform_device *pdev)
>>   	if (ret)
>>   		return ret;
>>   
>> +	ret = cpuhp_setup_state(CPUHP_AP_ONLINE_DYN, "x86/amd_hfi:online",
>> +				amd_hfi_online, amd_hfi_offline);
> 
> By using online notifier, you will already have tasks running on this
> CPU before you initialize the HFI bits, is that okay?

Yeah; AFAICT it should be fine.
Re: [PATCH v7 07/12] platform/x86: hfi: add online and offline callback support
Posted by Peter Zijlstra 1 year, 2 months ago
On Tue, Dec 03, 2024 at 02:28:37PM -0600, Mario Limonciello wrote:
> On 12/2/2024 05:38, Peter Zijlstra wrote:
> > On Sat, Nov 30, 2024 at 08:06:58AM -0600, Mario Limonciello wrote:
> > 
> > > @@ -340,6 +416,11 @@ static int amd_hfi_probe(struct platform_device *pdev)
> > >   	if (ret)
> > >   		return ret;
> > > +	ret = cpuhp_setup_state(CPUHP_AP_ONLINE_DYN, "x86/amd_hfi:online",
> > > +				amd_hfi_online, amd_hfi_offline);
> > 
> > By using online notifier, you will already have tasks running on this
> > CPU before you initialize the HFI bits, is that okay?
> 
> Yeah; AFAICT it should be fine.

Please add a comment to clarify this for future readers of the code.