Skip to contentSkip to conversation

cpufreq: driver data

PATCH In review Jan Beulich 3 participants 14 messages Jan 22 – Jan 27

lore
Share & tools
lore
#0 lore raw

cover cpufreq: driver data

Main goal being to eliminate the somewhat risky, as identified in earlier discussion, use of per-CPU data indexed by struct cpufreq_policy's cpu field.

1: eliminate cpufreq_drv_data[]
2: hwp: move driver data into policy
3: amd-cppc: move driver data into policy
4: amd-cppc: move epp_init into driver data
5: amd-cppc: move pxfreq_mhz into driver data

Jan

#1 lore raw

PATCH 1/5 cpufreq: eliminate cpufreq_drv_data[]

Possibly many slots of it may be unused (all of them when the HWP or CPPC drivers are in use), as it's always strictly associated with the CPU recorded in a policy (irrespective of that CPU intermediately being taken offline). It is shared by all CPUs sharing a policy. We could therefore put the respective pointers in struct cpufreq_policy, but even that level of indirection is pointless. Embed the driver data structure directly in the policy one.

---
v2: Re-base, use union.

xen/arch/x86/acpi/cpufreq/acpi.c +19 -42
@@ -174,17 +174,18 @@ static u32 get_cur_val(const cpumask_t *
         return 0;
 
     policy = per_cpu(cpufreq_cpu_policy, cpu);
-    if (!policy || !cpufreq_drv_data[policy->cpu])
+    if ( !policy )
         return 0;
 
-    switch (cpufreq_drv_data[policy->cpu]->arch_cpu_flags) {
+    switch ( policy->drv_data.acpi.arch_cpu_flags )
+    {
     case SYSTEM_INTEL_MSR_CAPABLE:
         cmd.type = SYSTEM_INTEL_MSR_CAPABLE;
         cmd.addr.msr.reg = MSR_IA32_PERF_STATUS;
         break;
     case SYSTEM_IO_CAPABLE:
         cmd.type = SYSTEM_IO_CAPABLE;
-        perf = cpufreq_drv_data[policy->cpu]->acpi_data;
+        perf = policy->drv_data.acpi.acpi_data;
         cmd.addr.io.port = perf->control_register.address;
         cmd.addr.io.bit_width = perf->control_register.bit_width;
         break;
@@ -210,9 +211,8 @@ static unsigned int cf_check get_cur_fre
     if (!policy)
         return 0;
 
-    data = cpufreq_drv_data[policy->cpu];
-    if (unlikely(data == NULL ||
-        data->acpi_data == NULL || data->freq_table == NULL))
+    data = &policy->drv_data.acpi;
+    if ( !data->acpi_data || !data->freq_table )
         return 0;
 
     return extract_freq(get_cur_val(cpumask_of(cpu)), data);
@@ -252,7 +252,7 @@ static int cf_check acpi_cpufreq_target(
     struct cpufreq_policy *policy,
     unsigned int target_freq, unsigned int relation)
 {
-    struct acpi_cpufreq_data *data = cpufreq_drv_data[policy->cpu];
+    struct acpi_cpufreq_data *data = &policy->drv_data.acpi;
     struct processor_performance *perf;
     struct cpufreq_freqs freqs;
     cpumask_t online_policy_cpus;
@@ -262,10 +262,8 @@ static int cf_check acpi_cpufreq_target(
     unsigned int j;
     int result = 0;
 
-    if (unlikely(data == NULL ||
-        data->acpi_data == NULL || data->freq_table == NULL)) {
+    if ( !data->acpi_data || !data->freq_table )
         return -ENODEV;
-    }
 
     if (policy->turbo == CPUFREQ_TURBO_DISABLED)
         if (target_freq > policy->cpuinfo.second_max_freq)
@@ -331,11 +329,9 @@ static int cf_check acpi_cpufreq_target(
 
 static int cf_check acpi_cpufreq_verify(struct cpufreq_policy *policy)
 {
-    struct acpi_cpufreq_data *data;
     struct processor_performance *perf;
 
-    if (!policy || !(data = cpufreq_drv_data[policy->cpu]) ||
-        !processor_pminfo[policy->cpu])
+    if ( !policy || !processor_pminfo[policy->cpu] )
         return -EINVAL;
 
     perf = &processor_pminfo[policy->cpu]->perf;
@@ -343,7 +339,8 @@ static int cf_check acpi_cpufreq_verify(
     cpufreq_verify_within_limits(policy, 0,
         perf->states[perf->platform_limit].core_frequency * 1000);
 
-    return cpufreq_frequency_table_verify(policy, data->freq_table);
+    return cpufreq_frequency_table_verify(policy,
+                                          policy->drv_data.acpi.freq_table);
 }
 
 static unsigned long
@@ -379,17 +376,11 @@ static int cf_check acpi_cpufreq_cpu_ini
     unsigned int i;
     unsigned int valid_states = 0;
     unsigned int cpu = policy->cpu;
-    struct acpi_cpufreq_data *data;
+    struct acpi_cpufreq_data *data = &policy->drv_data.acpi;
     unsigned int result = 0;
     struct cpuinfo_x86 *c = &cpu_data[policy->cpu];
     struct processor_performance *perf;
 
-    data = xzalloc(struct acpi_cpufreq_data);
-    if (!data)
-        return -ENOMEM;
-
-    cpufreq_drv_data[cpu] = data;
-
     data->acpi_data = &processor_pminfo[cpu]->perf;
 
     perf = data->acpi_data;
@@ -406,23 +397,18 @@ static int cf_check acpi_cpufreq_cpu_ini
         if (cpufreq_verbose)
             printk("xen_pminfo: @acpi_cpufreq_cpu_init,"
                    "HARDWARE addr space\n");
-        if (!cpu_has(c, X86_FEATURE_EIST)) {
-            result = -ENODEV;
-            goto err_unreg;
-        }
+        if ( !cpu_has(c, X86_FEATURE_EIST) )
+            return -ENODEV;
         data->arch_cpu_flags = SYSTEM_INTEL_MSR_CAPABLE;
         break;
     default:
-        result = -ENODEV;
-        goto err_unreg;
+        return -ENODEV;
     }
 
     data->freq_table = xmalloc_array(struct cpufreq_frequency_table,
                                     (perf->state_count+1));
-    if (!data->freq_table) {
-        result = -ENOMEM;
-        goto err_unreg;
-    }
+    if ( !data->freq_table )
+        return -ENOMEM;
 
     /* detect transition latency */
     policy->cpuinfo.transition_latency = 0;
@@ -480,23 +466,14 @@ static int cf_check acpi_cpufreq_cpu_ini
     return result;
 
 err_freqfree:
-    xfree(data->freq_table);
-err_unreg:
-    xfree(data);
-    cpufreq_drv_data[cpu] = NULL;
+    XFREE(data->freq_table);
 
     return result;
 }
 
 static int cf_check acpi_cpufreq_cpu_exit(struct cpufreq_policy *policy)
 {
-    struct acpi_cpufreq_data *data = cpufreq_drv_data[policy->cpu];
-
-    if (data) {
-        cpufreq_drv_data[policy->cpu] = NULL;
-        xfree(data->freq_table);
-        xfree(data);
-    }
+    XFREE(policy->drv_data.acpi.freq_table);
 
     return 0;
 }
xen/arch/x86/acpi/cpufreq/cpufreq.c +0 -2
@@ -35,8 +35,6 @@
 
 #include <acpi/cpufreq/cpufreq.h>
 
-struct acpi_cpufreq_data *cpufreq_drv_data[NR_CPUS];
-
 struct perf_pair {
     union {
         struct {
xen/arch/x86/acpi/cpufreq/powernow.c +14 -38

118 lines not on this page · raw message

xen/include/acpi/cpufreq/cpufreq.h +4 -2
@@ -38,8 +38,6 @@ struct acpi_cpufreq_data {
     unsigned int arch_cpu_flags;
 };
 
-extern struct acpi_cpufreq_data *cpufreq_drv_data[NR_CPUS];
-
 struct cpufreq_cpuinfo {
     unsigned int        max_freq;
     unsigned int        second_max_freq;    /* P1 if Turbo Mode is on */
@@ -82,6 +80,10 @@ struct cpufreq_policy {
                                  * -1 for disable, 1 for enabled
                                  * See CPUFREQ_TURBO_* below for defines */
     unsigned int        policy; /* CPUFREQ_POLICY_* */
+
+    union {
+        struct acpi_cpufreq_data acpi;
+    }                   drv_data;
 };
 DECLARE_PER_CPU(struct cpufreq_policy *, cpufreq_cpu_policy);
 
Reviewed-by on v1 1/5
#2 lore raw

Re: [PATCH 1/5] cpufreq: eliminate cpufreq_drv_data[]

Quoted 9 lines · Jan Beulich

Possibly many slots of it may be unused (all of them when the HWP or CPPC drivers are in use), as it's always strictly associated with the CPU recorded in a policy (irrespective of that CPU intermediately being taken offline). It is shared by all CPUs sharing a policy. We could therefore put the respective pointers in struct cpufreq_policy, but even that level of indirection is pointless. Embed the driver data structure directly in the policy one.

#3 lore raw

PATCH 2/5 cpufreq/hwp: move driver data into policy

In reply to Jan Beulich

Share space with the ACPI and powernow drivers, avoiding a separate allocation for each CPU. Except for get_hwp_para() all use sites already have the policy available, and this one function can simply be brought closer to its sibling set_hwp_para().

This then also eliminates the concern over hwp_cpufreq_cpu_init() being called for all CPUs, or a CPU going offline that's recorded in policy->cpu (which would result in accesses of per-CPU data of offline CPUs).

---
hwp_cpufreq_target() still requires policy->cpu to be online, though.

xen/arch/x86/acpi/cpufreq/hwp.c +14 -72
@@ -22,50 +22,6 @@ static bool __read_mostly feature_hdc;
 
 static bool __ro_after_init opt_cpufreq_hdc = true;
 
-union hwp_request
-{
-    struct
-    {
-        unsigned int min_perf:8;
-        unsigned int max_perf:8;
-        unsigned int desired:8;
-        unsigned int energy_perf:8;
-        unsigned int activity_window:10;
-        bool package_control:1;
-        unsigned int :16;
-        bool activity_window_valid:1;
-        bool energy_perf_valid:1;
-        bool desired_valid:1;
-        bool max_perf_valid:1;
-        bool min_perf_valid:1;
-    };
-    uint64_t raw;
-};
-
-struct hwp_drv_data
-{
-    union
-    {
-        uint64_t hwp_caps;
-        struct
-        {
-            unsigned int highest:8;
-            unsigned int guaranteed:8;
-            unsigned int most_efficient:8;
-            unsigned int lowest:8;
-            unsigned int :32;
-        } hw;
-    };
-    union hwp_request curr_req;
-    int ret;
-    uint16_t activity_window;
-    uint8_t minimum;
-    uint8_t maximum;
-    uint8_t desired;
-    uint8_t energy_perf;
-};
-static DEFINE_PER_CPU_READ_MOSTLY(struct hwp_drv_data *, hwp_drv_data);
-
 #define hwp_err(cpu, fmt, args...) \
     printk(XENLOG_ERR "HWP: CPU%u error: " fmt, cpu, ## args)
 #define hwp_info(fmt, args...)    printk(XENLOG_INFO "HWP: " fmt, ## args)
@@ -212,7 +168,7 @@ static bool __init hwp_available(void)
 
 static int cf_check hwp_cpufreq_verify(struct cpufreq_policy *policy)
 {
-    struct hwp_drv_data *data = per_cpu(hwp_drv_data, policy->cpu);
+    struct hwp_drv_data *data = &policy->drv_data.hwp;
 
     if ( !cpu_has_hwp_activity_window && data->activity_window )
     {
@@ -226,8 +182,8 @@ static int cf_check hwp_cpufreq_verify(s
 
 static void cf_check hwp_write_request(void *info)
 {
-    const struct cpufreq_policy *policy = info;
-    struct hwp_drv_data *data = this_cpu(hwp_drv_data);
+    struct cpufreq_policy *policy = info;
+    struct hwp_drv_data *data = &policy->drv_data.hwp;
     union hwp_request hwp_req = data->curr_req;
 
     data->ret = 0;
@@ -247,7 +203,7 @@ static int cf_check hwp_cpufreq_target(s
                                        unsigned int relation)
 {
     unsigned int cpu = policy->cpu;
-    struct hwp_drv_data *data = per_cpu(hwp_drv_data, cpu);
+    struct hwp_drv_data *data = &policy->drv_data.hwp;
     /* Zero everything to ensure reserved bits are zero... */
     union hwp_request hwp_req = { .raw = 0 };
 
@@ -338,7 +294,7 @@ static void hwp_get_cpu_speeds(struct cp
 static void cf_check hwp_init_msrs(void *info)
 {
     struct cpufreq_policy *policy = info;
-    struct hwp_drv_data *data = this_cpu(hwp_drv_data);
+    struct hwp_drv_data *data = &policy->drv_data.hwp;
     uint64_t val;
 
     /*
@@ -406,23 +362,15 @@ static int cf_check hwp_cpufreq_cpu_init
     static bool __read_mostly first_run = true;
     static union hwp_request initial_req;
     unsigned int cpu = policy->cpu;
-    struct hwp_drv_data *data;
-
-    data = xzalloc(struct hwp_drv_data);
-    if ( !data )
-        return -ENOMEM;
+    struct hwp_drv_data *data = &policy->drv_data.hwp;
 
     policy->governor = &cpufreq_gov_hwp;
 
-    per_cpu(hwp_drv_data, cpu) = data;
-
     on_selected_cpus(cpumask_of(cpu), hwp_init_msrs, policy, 1);
 
     if ( data->curr_req.raw == -1 )
     {
         hwp_err(cpu, "Could not initialize HWP properly\n");
-        per_cpu(hwp_drv_data, cpu) = NULL;
-        xfree(data);
         return -ENODEV;
     }
 
@@ -450,11 +398,6 @@ static int cf_check hwp_cpufreq_cpu_init
 
 static int cf_check hwp_cpufreq_cpu_exit(struct cpufreq_policy *policy)
 {
-    struct hwp_drv_data *data = per_cpu(hwp_drv_data, policy->cpu);
-
-    per_cpu(hwp_drv_data, policy->cpu) = NULL;
-    xfree(data);
-
     return 0;
 }
 
@@ -467,8 +410,8 @@ static int cf_check hwp_cpufreq_cpu_exit
  */
 static void cf_check hwp_set_misc_turbo(void *info)
 {
-    const struct cpufreq_policy *policy = info;
-    struct hwp_drv_data *data = per_cpu(hwp_drv_data, policy->cpu);
+    struct cpufreq_policy *policy = info;
+    struct hwp_drv_data *data = &policy->drv_data.hwp;
     uint64_t msr;
 
     data->ret = 0;
@@ -499,7 +442,7 @@ static int cf_check hwp_cpufreq_update(u
 {
     on_selected_cpus(cpumask_of(cpu), hwp_set_misc_turbo, policy, 1);
 
-    return per_cpu(hwp_drv_data, cpu)->ret;
+    return policy->drv_data.hwp.ret;
 }
 #endif /* CONFIG_PM_OP */
 
@@ -516,12 +459,12 @@ hwp_cpufreq_driver = {
 };
 
 #ifdef CONFIG_PM_OP
-int get_hwp_para(unsigned int cpu,
+int get_hwp_para(const struct cpufreq_policy *policy,
                  struct xen_get_cppc_para *cppc_para)
 {
-    const struct hwp_drv_data *data = per_cpu(hwp_drv_data, cpu);
+    const struct hwp_drv_data *data = &policy->drv_data.hwp;
 
-    if ( data == NULL )
+    if ( !data->maximum )
         return -ENODATA;
 
     cppc_para->features         =
@@ -542,11 +485,10 @@ int get_hwp_para(unsigned int cpu,
 int set_hwp_para(struct cpufreq_policy *policy,
                  struct xen_set_cppc_para *set_cppc)
 {
-    unsigned int cpu = policy->cpu;
-    struct hwp_drv_data *data = per_cpu(hwp_drv_data, cpu);
+    struct hwp_drv_data *data = &policy->drv_data.hwp;
     bool cleared_act_window = false;
 
-    if ( data == NULL )
+    if ( !data->maximum )
         return -ENOENT;
 
     /* Validate all parameters - Disallow reserved bits. */
xen/drivers/acpi/pm-op.c +4 -2
@@ -80,10 +80,12 @@ static int read_scaling_available_govern
 static int get_cpufreq_cppc(unsigned int cpu,
                             struct xen_get_cppc_para *cppc_para)
 {
+    const struct cpufreq_policy *policy =
+        per_cpu(cpufreq_cpu_policy, cpu);
     int ret = -ENODEV;
 
-    if ( hwp_active() )
-        ret = get_hwp_para(cpu, cppc_para);
+    if ( policy && hwp_active() )
+        ret = get_hwp_para(policy, cppc_para);
 
     return ret;
 }
xen/include/acpi/cpufreq/cpufreq.h +38 -1
@@ -38,6 +38,42 @@ struct acpi_cpufreq_data {
     unsigned int arch_cpu_flags;
 };
 
+struct hwp_drv_data {
+    union {
+        uint64_t hwp_caps;
+        struct {
+            unsigned int highest:8;
+            unsigned int guaranteed:8;
+            unsigned int most_efficient:8;
+            unsigned int lowest:8;
+            unsigned int :32;
+        } hw;
+    };
+    union hwp_request {
+        struct {
+            unsigned int min_perf:8;
+            unsigned int max_perf:8;
+            unsigned int desired:8;
+            unsigned int energy_perf:8;
+            unsigned int activity_window:10;
+            bool package_control:1;
+            unsigned int :16;
+            bool activity_window_valid:1;
+            bool energy_perf_valid:1;
+            bool desired_valid:1;
+            bool max_perf_valid:1;
+            bool min_perf_valid:1;
+        };
+        uint64_t raw;
+    } curr_req;
+    int ret;
+    uint16_t activity_window;
+    uint8_t minimum;
+    uint8_t maximum;
+    uint8_t desired;
+    uint8_t energy_perf;
+};
+
 struct cpufreq_cpuinfo {
     unsigned int        max_freq;
     unsigned int        second_max_freq;    /* P1 if Turbo Mode is on */
@@ -83,6 +119,7 @@ struct cpufreq_policy {
 
     union {
         struct acpi_cpufreq_data acpi;
+        struct hwp_drv_data hwp;
     }                   drv_data;
 };
 DECLARE_PER_CPU(struct cpufreq_policy *, cpufreq_cpu_policy);
@@ -286,7 +323,7 @@ bool hwp_active(void);
 static inline bool hwp_active(void) { return false; }
 #endif
 
-int get_hwp_para(unsigned int cpu,
+int get_hwp_para(const struct cpufreq_policy *policy,
                  struct xen_get_cppc_para *cppc_para);
 int set_hwp_para(struct cpufreq_policy *policy,
                  struct xen_set_cppc_para *set_cppc);
#4 lore raw

Re: [PATCH 2/5] cpufreq/hwp: move driver data into policy

Quoted 4 lines · Jan Beulich

Share space with the ACPI and powernow drivers, avoiding a separate allocation for each CPU. Except for get_hwp_para() all use sites already have the policy available, and this one function can simply be brought closer to its sibling set_hwp_para().

Minor, but maybe 's/function/function's signature/'. The original phrasing made me think it was code movement.

Quoted 3 lines

This then also eliminates the concern over hwp_cpufreq_cpu_init() being called for all CPUs

We expect hwp_cpufreq_cpu_init() to be called for each CPU, so I am confused by this statement. The data...

xen/arch/x86/acpi/cpufreq/hwp.c static bool __read_mostly feature_hdc; · line 67

Quoted 11 lines

, or a CPU going offline that's recorded in> policy->cpu (which would result in accesses of per-CPU data of offline CPUs).

---
hwp_cpufreq_target() still requires policy->cpu to be online, though.

xen/arch/x86/acpi/cpufreq/hwp.c +0 -1
 
-static DEFINE_PER_CPU_READ_MOSTLY(struct hwp_drv_data *, hwp_drv_data);

... here is tracked and filled per-CPU.

Do we need cpufreq_add_cpu() to force hw_all = 1 for HWP (and maybe AMD-CPPC) to ensure that policy is allocated per-CPU?

Are we implicitly relying on shared_type == CPUFREQ_SHARED_TYPE_HW to do that for us?

The code here is okay, fwiw.

Thanks,
Jason

#5 lore raw

Re: [PATCH 2/5] cpufreq/hwp: move driver data into policy

Quoted 8 lines · Jason Andryuk
Quoted 4 lines · Jan Beulich

Share space with the ACPI and powernow drivers, avoiding a separate allocation for each CPU. Except for get_hwp_para() all use sites already have the policy available, and this one function can simply be brought closer to its sibling set_hwp_para().

Minor, but maybe 's/function/function's signature/'. The original phrasing made me think it was code movement.

I don't see an issue there, but I've adjusted as you asked for.

Quoted 5 lines
Quoted 2 lines

This then also eliminates the concern over hwp_cpufreq_cpu_init() being called for all CPUs

We expect hwp_cpufreq_cpu_init() to be called for each CPU, so I am confused by this statement. The data...

Well, "expect" is the problem. Recall my pointing out of the problem when having noticed the same pattern in the amd-cppc driver patches. My recollection from the discussion is that there's no formal statement of
...

Quoted 11 lines
Quoted 1 line

, or a CPU going offline that's recorded in> policy->cpu (which would

result in accesses of per-CPU data of offline

Quoted 8 lines

CPUs).

---
hwp_cpufreq_target() still requires policy->cpu to be online, though.

xen/arch/x86/acpi/cpufreq/hwp.c +0 -0
Quoted 6 lines
Quoted 1 line

-static DEFINE_PER_CPU_READ_MOSTLY(struct hwp_drv_data *, hwp_drv_data);

... here is tracked and filled per-CPU.

Do we need cpufreq_add_cpu() to force hw_all = 1 for HWP (and maybe AMD-CPPC) to ensure that policy is allocated per-CPU?

... this being a correct thing to do, hence our code imo would better be resilient to it being different somewhere.

Quoted 2 lines

Are we implicitly relying on shared_type == CPUFREQ_SHARED_TYPE_HW to do that for us?

Right now we do, I believe, without - as said above - this being actually mandated by the spec.

Jan

Reviewed-by
#6 lore raw

Re: [PATCH 2/5] cpufreq/hwp: move driver data into policy

Quoted 11 lines · Jan Beulich
Quoted 8 lines · Jason Andryuk
Quoted 4 lines · Jan Beulich

Share space with the ACPI and powernow drivers, avoiding a separate allocation for each CPU. Except for get_hwp_para() all use sites already have the policy available, and this one function can simply be brought closer to its sibling set_hwp_para().

Minor, but maybe 's/function/function's signature/'. The original phrasing made me think it was code movement.

I don't see an issue there, but I've adjusted as you asked for.

Thank you.

Quoted 29 lines
Quoted 5 lines
Quoted 2 lines

This then also eliminates the concern over hwp_cpufreq_cpu_init() being called for all CPUs

We expect hwp_cpufreq_cpu_init() to be called for each CPU, so I am confused by this statement. The data...

Well, "expect" is the problem. Recall my pointing out of the problem when having noticed the same pattern in the amd-cppc driver patches. My recollection from the discussion is that there's no formal statement of
...

Quoted 11 lines
Quoted 1 line

, or a CPU going offline that's recorded in> policy->cpu (which would

result in accesses of per-CPU data of offline

Quoted 8 lines

CPUs).

---
hwp_cpufreq_target() still requires policy->cpu to be online, though.

xen/arch/x86/acpi/cpufreq/hwp.c +0 -0
Quoted 6 lines
Quoted 1 line

-static DEFINE_PER_CPU_READ_MOSTLY(struct hwp_drv_data *, hwp_drv_data);

... here is tracked and filled per-CPU.

Do we need cpufreq_add_cpu() to force hw_all = 1 for HWP (and maybe AMD-CPPC) to ensure that policy is allocated per-CPU?

Quoted 8 lines

... this being a correct thing to do, hence our code imo would better be resilient to it being different somewhere.

Quoted 2 lines

Are we implicitly relying on shared_type == CPUFREQ_SHARED_TYPE_HW to do that for us?

Right now we do, I believe, without - as said above - this being actually mandated by the spec.

HWP doesn't need ACPI data. I wrote the driver according to the SDM, which is just MSRs. It's Xen that needs ACPI data to initialize and use cpufreq.

Regardless of that, it looks like the checks for cpu_online() and performance_pminfo[] would constrain CPU accesses, so:

Thanks,
Jason

#7 lore raw

Re: [PATCH 2/5] cpufreq/hwp: move driver data into policy

Quoted 19 lines · Jason Andryuk
Quoted 18 lines · Jan Beulich
Quoted 10 lines · Jason Andryuk

xen/arch/x86/acpi/cpufreq/hwp.c static bool __read_mostly feature_hdc; · line 67

Quoted 4 lines · Jan Beulich
xen/arch/x86/acpi/cpufreq/hwp.c +0 -1
 
-static DEFINE_PER_CPU_READ_MOSTLY(struct hwp_drv_data *, hwp_drv_data);

... here is tracked and filled per-CPU.

Do we need cpufreq_add_cpu() to force hw_all = 1 for HWP (and maybe AMD-CPPC) to ensure that policy is allocated per-CPU?

... this being a correct thing to do, hence our code imo would better be resilient to it being different somewhere.

Quoted 2 lines

Are we implicitly relying on shared_type == CPUFREQ_SHARED_TYPE_HW to do that for us?

Quoted 6 lines
Quoted 2 lines

Right now we do, I believe, without - as said above - this being actually mandated by the spec.

HWP doesn't need ACPI data. I wrote the driver according to the SDM, which is just MSRs. It's Xen that needs ACPI data to initialize and use cpufreq.

Maybe we should see about lifting that restriction then? Becoming independent of Dom0's xen-acpi-processor driver would be quite a meaningful gain, I suppose. See e.g. the thread rooted at
https://lists.xen.org/archives/html/xen-devel/2025-12/msg01114.html.

Quoted 4 lines

Regardless of that, it looks like the checks for cpu_online() and performance_pminfo[] would constrain CPU accesses, so:

Thanks.

Jan

#8 lore raw

PATCH 3/5 cpufreq/amd-cppc: move driver data into policy

In reply to Jan Beulich

Share space with the ACPI, powernow, and HWP drivers, avoiding a separate allocation for each CPU.

This then also reduces the concern over amd_cppc_cpufreq_cpu_init() being called for all CPUs, or a CPU going offline that's recorded in policy->cpu (which would result in accesses of per-CPU data of offline CPUs).

---
amd_cppc_cpufreq_target() (together with amd_cppc_write_request()) still requires policy->cpu to be online, though.

xen/arch/x86/acpi/cpufreq/amd-cppc.c +12 -96
@@ -31,81 +31,6 @@
 })
 
 /*
- * Field highest_perf, nominal_perf, lowest_nonlinear_perf, and lowest_perf
- * contain the values read from CPPC capability MSR. They represent the limits
- * of managed performance range as well as the dynamic capability, which may
- * change during processor operation
- * Field highest_perf represents highest performance, which is the absolute
- * maximum performance an individual processor may reach, assuming ideal
- * conditions. This performance level may not be sustainable for long
- * durations and may only be achievable if other platform components
- * are in a specific state; for example, it may require other processors be
- * in an idle state. This would be equivalent to the highest frequencies
- * supported by the processor.
- * Field nominal_perf represents maximum sustained performance level of the
- * processor, assuming ideal operating conditions. All cores/processors are
- * expected to be able to sustain their nominal performance state
- * simultaneously.
- * Field lowest_nonlinear_perf represents Lowest Nonlinear Performance, which
- * is the lowest performance level at which nonlinear power savings are
- * achieved. Above this threshold, lower performance levels should be
- * generally more energy efficient than higher performance levels. So in
- * traditional terms, this represents the P-state range of performance levels.
- * Field lowest_perf represents the absolute lowest performance level of the
- * platform. Selecting it may cause an efficiency penalty but should reduce
- * the instantaneous power consumption of the processor. So in traditional
- * terms, this represents the T-state range of performance levels.
- *
- * Field max_perf, min_perf, des_perf store the values for CPPC request MSR.
- * Software passes performance goals through these fields.
- * Field max_perf conveys the maximum performance level at which the platform
- * may run. And it may be set to any performance value in the range
- * [lowest_perf, highest_perf], inclusive.
- * Field min_perf conveys the minimum performance level at which the platform
- * may run. And it may be set to any performance value in the range
- * [lowest_perf, highest_perf], inclusive but must be less than or equal to
- * max_perf.
- * Field des_perf conveys performance level Xen governor is requesting. And it
- * may be set to any performance value in the range [min_perf, max_perf],
- * inclusive. In active mode, des_perf must be zero.
- * Field epp represents energy performance preference, which only has meaning
- * when active mode is enabled. The EPP is used in the CCLK DPM controller
- * to drive the frequency that a core is going to operate during short periods
- * of activity, called minimum active frequency, It could contatin a range of
- * values from 0 to 0xff. An EPP of zero sets the min active frequency to
- * maximum frequency, while an EPP of 0xff sets the min active frequency to
- * approxiately Idle frequency.
- */
-struct amd_cppc_drv_data
-{
-    const struct xen_processor_cppc *cppc_data;
-    union {
-        uint64_t raw;
-        struct {
-            unsigned int lowest_perf:8;
-            unsigned int lowest_nonlinear_perf:8;
-            unsigned int nominal_perf:8;
-            unsigned int highest_perf:8;
-            unsigned int :32;
-        };
-    } caps;
-    union {
-        uint64_t raw;
-        struct {
-            unsigned int max_perf:8;
-            unsigned int min_perf:8;
-            unsigned int des_perf:8;
-            unsigned int epp:8;
-            unsigned int :32;
-        };
-    } req;
-
-    int err;
-};
-
-static DEFINE_PER_CPU_READ_MOSTLY(struct amd_cppc_drv_data *,
-                                  amd_cppc_drv_data);
-/*
  * Core max frequency read from PstateDef as anchor point
  * for freq-to-perf transition
  */
@@ -279,11 +204,11 @@ static void cf_check amd_cppc_write_requ
     wrmsrl(MSR_AMD_CPPC_REQ, data->req.raw);
 }
 
-static void amd_cppc_write_request(unsigned int cpu, uint8_t min_perf,
-                                   uint8_t des_perf, uint8_t max_perf,
-                                   uint8_t epp)
+static void amd_cppc_write_request(struct cpufreq_policy *policy,
+                                   uint8_t min_perf, uint8_t des_perf,
+                                   uint8_t max_perf, uint8_t epp)
 {
-    struct amd_cppc_drv_data *data = per_cpu(amd_cppc_drv_data, cpu);
+    struct amd_cppc_drv_data *data = &policy->drv_data.amd_cppc;
     uint64_t prev = data->req.raw;
 
     data->req.min_perf = min_perf;
@@ -295,15 +220,15 @@ static void amd_cppc_write_request(unsig
     if ( prev == data->req.raw )
         return;
 
-    on_selected_cpus(cpumask_of(cpu), amd_cppc_write_request_msrs, data, 1);
+    on_selected_cpus(cpumask_of(policy->cpu), amd_cppc_write_request_msrs,
+                     data, 1);
 }
 
 static int cf_check amd_cppc_cpufreq_target(struct cpufreq_policy *policy,
                                             unsigned int target_freq,
                                             unsigned int relation)
 {
-    unsigned int cpu = policy->cpu;
-    const struct amd_cppc_drv_data *data = per_cpu(amd_cppc_drv_data, cpu);
+    const struct amd_cppc_drv_data *data = &policy->drv_data.amd_cppc;
     uint8_t des_perf;
     int res;
 
@@ -320,7 +245,7 @@ static int cf_check amd_cppc_cpufreq_tar
      * may actually cause an efficiency penalty, So when deciding the min_perf
      * value, we prefer lowest nonlinear performance over lowest performance.
      */
-    amd_cppc_write_request(policy->cpu, data->caps.lowest_nonlinear_perf,
+    amd_cppc_write_request(policy, data->caps.lowest_nonlinear_perf,
                            des_perf, data->caps.highest_perf,
                            /* Pre-defined BIOS value for passive mode */
                            per_cpu(epp_init, policy->cpu));
@@ -330,7 +255,7 @@ static int cf_check amd_cppc_cpufreq_tar
 static void cf_check amd_cppc_init_msrs(void *info)
 {
     struct cpufreq_policy *policy = info;
-    struct amd_cppc_drv_data *data = this_cpu(amd_cppc_drv_data);
+    struct amd_cppc_drv_data *data = &policy->drv_data.amd_cppc;
     uint64_t val;
     unsigned int min_freq = 0, nominal_freq = 0, max_freq;
 
@@ -431,24 +356,16 @@ static void amd_cppc_boost_init(struct c
 
 static int cf_check amd_cppc_cpufreq_cpu_exit(struct cpufreq_policy *policy)
 {
-    XVFREE(per_cpu(amd_cppc_drv_data, policy->cpu));
-
     return 0;
 }
 
 static int amd_cppc_cpufreq_init_perf(struct cpufreq_policy *policy)
 {
     unsigned int cpu = policy->cpu;
-    struct amd_cppc_drv_data *data;
-
-    data = xvzalloc(struct amd_cppc_drv_data);
-    if ( !data )
-        return -ENOMEM;
+    struct amd_cppc_drv_data *data = &policy->drv_data.amd_cppc;
 
     data->cppc_data = &processor_pminfo[cpu]->cppc_data;
 
-    per_cpu(amd_cppc_drv_data, cpu) = data;
-
     on_selected_cpus(cpumask_of(cpu), amd_cppc_init_msrs, policy, 1);
 
     /*
@@ -506,8 +423,7 @@ static void amd_cppc_prepare_policy(stru
                                     uint8_t *max_perf, uint8_t *min_perf,
                                     uint8_t *epp)
 {
-    const struct amd_cppc_drv_data *data = per_cpu(amd_cppc_drv_data,
-                                                   policy->cpu);
+    const struct amd_cppc_drv_data *data = &policy->drv_data.amd_cppc;
 
     /*
      * On default, set min_perf with lowest_nonlinear_perf, and max_perf
@@ -560,7 +476,7 @@ static int cf_check amd_cppc_epp_set_pol
 
     amd_cppc_prepare_policy(policy, &max_perf, &min_perf, &epp);
 
-    amd_cppc_write_request(policy->cpu, min_perf,
+    amd_cppc_write_request(policy, min_perf,
                            0 /* no des_perf in active mode */,
                            max_perf, epp);
     return 0;
xen/include/acpi/cpufreq/cpufreq.h +73 -0

85 lines not on this page · raw message

Reviewed-by on v1 3/5
#9 lore raw

RE: [PATCH 3/5] cpufreq/amd-cppc: move driver data into policy

[Public]

Quoted 8 lines

-----Original Message-----
From: Jan Beulich <[email protected]>
Sent: Thursday, January 22, 2026 5:43 PM
To: [email protected]
Cc: Andrew Cooper <[email protected]>; Roger Pau Monné <[email protected]>; Penny, Zheng <[email protected]>
Subject: [PATCH 3/5] cpufreq/amd-cppc: move driver data into policy

Quoted 8 lines

Share space with the ACPI, powernow, and HWP drivers, avoiding a separate allocation for each CPU.

This then also reduces the concern over amd_cppc_cpufreq_cpu_init() being called for all CPUs, or a CPU going offline that's recorded in policy->cpu (which would result in accesses of per-CPU data of offline CPUs).

Many thanks,
Penny Zheng

#10 lore raw

PATCH 4/5 cpufreq/amd-cppc: move epp_init into driver data

In reply to Jan Beulich

No reason for it to be a separate per-CPU item; it's connected to a struct cpufreq_policy instance just like other driver data.

This further reduces the concern over amd_cppc_cpufreq_cpu_init() being called for all CPUs, or a CPU going offline that's recorded in policy->cpu (which would result in accesses of per-CPU data of offline CPUs).

xen/arch/x86/acpi/cpufreq/amd-cppc.c +3 -4
@@ -35,7 +35,6 @@
  * for freq-to-perf transition
  */
 static DEFINE_PER_CPU_READ_MOSTLY(unsigned int, pxfreq_mhz);
-static DEFINE_PER_CPU_READ_MOSTLY(uint8_t, epp_init);
 #ifndef NDEBUG
 static bool __ro_after_init opt_active_mode;
 #else
@@ -248,7 +247,7 @@ static int cf_check amd_cppc_cpufreq_tar
     amd_cppc_write_request(policy, data->caps.lowest_nonlinear_perf,
                            des_perf, data->caps.highest_perf,
                            /* Pre-defined BIOS value for passive mode */
-                           per_cpu(epp_init, policy->cpu));
+                           data->epp_init);
     return 0;
 }
 
@@ -326,7 +325,7 @@ static void cf_check amd_cppc_init_msrs(
 
     /* Store pre-defined BIOS value for passive mode */
     rdmsrl(MSR_AMD_CPPC_REQ, val);
-    this_cpu(epp_init) = MASK_EXTR(val, AMD_CPPC_EPP_MASK);
+    data->epp_init = MASK_EXTR(val, AMD_CPPC_EPP_MASK);
 
     return;
 
@@ -465,7 +464,7 @@ static void amd_cppc_prepare_policy(stru
         break;
 
     default:
-        *epp = per_cpu(epp_init, policy->cpu);
+        *epp = data->epp_init;
         break;
     }
 }
xen/include/acpi/cpufreq/cpufreq.h +2 -0
@@ -143,6 +143,8 @@ struct amd_cppc_drv_data {
         };
     } req;
 
+    uint8_t epp_init;
+
     int err;
 };
 
Reviewed-by on v1 4/5
#11 lore raw

RE: [PATCH 4/5] cpufreq/amd-cppc: move epp_init into driver data

[Public]

Quoted 8 lines

-----Original Message-----
From: Jan Beulich <[email protected]>
Sent: Thursday, January 22, 2026 5:43 PM
To: [email protected]
Cc: Andrew Cooper <[email protected]>; Roger Pau Monné <[email protected]>; Penny, Zheng <[email protected]>
Subject: [PATCH 4/5] cpufreq/amd-cppc: move epp_init into driver data

Quoted 9 lines

No reason for it to be a separate per-CPU item; it's connected to a struct cpufreq_policy instance just like other driver data.

This further reduces the concern over amd_cppc_cpufreq_cpu_init() being called for all CPUs, or a CPU going offline that's recorded in policy->cpu (which would result in accesses of per-CPU data of offline CPUs).

Many thanks,
Penny Zheng

#12 lore raw

PATCH 5/5 cpufreq/amd-cppc: move pxfreq_mhz into driver data

In reply to Jan Beulich

No reason for it to be a separate per-CPU item; it's connected to a struct cpufreq_policy instance just like other driver data.

This also eliminates the concern over amd_cppc_cpufreq_cpu_init() being called for all CPUs, or a CPU going offline that's recorded in policy->cpu (which would result in accesses of per-CPU data of offline CPUs).

xen/arch/x86/acpi/cpufreq/amd-cppc.c +3 -8
@@ -30,11 +30,6 @@
         printk(XENLOG_DEBUG "AMD-CPPC: CPU%u " fmt, cpu, ## args);  \
 })
 
-/*
- * Core max frequency read from PstateDef as anchor point
- * for freq-to-perf transition
- */
-static DEFINE_PER_CPU_READ_MOSTLY(unsigned int, pxfreq_mhz);
 #ifndef NDEBUG
 static bool __ro_after_init opt_active_mode;
 #else
@@ -117,7 +112,7 @@ static int amd_cppc_khz_to_perf(const st
     {
         /* Read Processor Max Speed(MHz) as anchor point */
         mul = data->caps.highest_perf;
-        div = this_cpu(pxfreq_mhz);
+        div = data->pxfreq_mhz;
         if ( !div )
             return -EOPNOTSUPP;
     }
@@ -160,7 +155,7 @@ static int amd_get_cpc_freq(const struct
     }
 
     /* Read Processor Max Speed(MHz) as anchor point */
-    mul = this_cpu(pxfreq_mhz);
+    mul = data->pxfreq_mhz;
     if ( !mul )
         return -EOPNOTSUPP;
     div = data->caps.highest_perf;
@@ -287,7 +282,7 @@ static void cf_check amd_cppc_init_msrs(
     }
 
     amd_process_freq(&cpu_data[policy->cpu],
-                     NULL, NULL, &this_cpu(pxfreq_mhz));
+                     NULL, NULL, &data->pxfreq_mhz);
 
     data->err = amd_get_cpc_freq(data, data->cppc_data->cpc.lowest_mhz,
                                  data->caps.lowest_perf, &min_freq);
xen/include/acpi/cpufreq/cpufreq.h +6 -0
@@ -145,6 +145,12 @@ struct amd_cppc_drv_data {
 
     uint8_t epp_init;
 
+    /*
+     * Core max frequency read from PstateDef as anchor point
+     * for freq-to-perf transition
+     */
+    unsigned int pxfreq_mhz;
+
     int err;
 };
 
Reviewed-by on v1 5/5
#13 lore raw

RE: [PATCH 5/5] cpufreq/amd-cppc: move pxfreq_mhz into driver data

[Public]

Quoted 8 lines

-----Original Message-----
From: Jan Beulich <[email protected]>
Sent: Thursday, January 22, 2026 5:44 PM
To: [email protected]
Cc: Andrew Cooper <[email protected]>; Roger Pau Monné <[email protected]>; Penny, Zheng <[email protected]>
Subject: [PATCH 5/5] cpufreq/amd-cppc: move pxfreq_mhz into driver data

Quoted 9 lines

No reason for it to be a separate per-CPU item; it's connected to a struct cpufreq_policy instance just like other driver data.

This also eliminates the concern over amd_cppc_cpufreq_cpu_init() being called for all CPUs, or a CPU going offline that's recorded in policy->cpu (which would result in accesses of per-CPU data of offline CPUs).

Later, I'll rebase your commits and test them all on my board locally, and complement the Tested-by:. Since I guest upstream doesn't have boards with amd-cppc enabled in BIOS

Many thanks,
Penny Zheng