Skip to content

Commit d7ca7d1

Browse files
Kuppuswamy Sathyanarayananrafaeljw
authored andcommitted
powercap: intel_rapl: Allow interface drivers to configure rapl_defaults
RAPL default settings vary across different RAPL interfaces (MSR, TPMI, MMIO). Currently, these defaults are stored in the common RAPL driver, which requires interface-specific handling logic and makes the common layer unnecessarily complex. There is no strong reason for the common code to own these defaults, since they are inherently interface-specific. To prepare for moving default configuration into the individual interface drivers, 1. Move struct rapl_defaults into a shared header so that interface drivers can directly populate their own default settings. 2. Change the @defaults field in struct rapl_if_priv from void * to const struct rapl_defaults * to improve type safety and readability and update the common driver to use the typed defaults structure. 3. Update all internal getter functions and local pointers to use const struct rapl_defaults * to maintain const-correctness. 4. Rename and export the common helper functions (check_unit, set_floor_freq, compute_time_window) so interface drivers may reuse or override them as appropriate. No functional changes. This is a preparatory refactoring to allow interface drivers to supply their own RAPL default settings. Co-developed-by: Zhang Rui <rui.zhang@intel.com> Signed-off-by: Zhang Rui <rui.zhang@intel.com> Signed-off-by: Kuppuswamy Sathyanarayanan <sathyanarayanan.kuppuswamy@linux.intel.com> Acked-by: Srinivas Pandruvada <srinivas.pandruvada@linux.intel.com> Link: https://patch.msgid.link/20260212233044.329790-9-sathyanarayanan.kuppuswamy@linux.intel.com Signed-off-by: Rafael J. Wysocki <rafael.j.wysocki@intel.com>
1 parent 90503f9 commit d7ca7d1

2 files changed

Lines changed: 43 additions & 38 deletions

File tree

drivers/powercap/intel_rapl_common.c

Lines changed: 28 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -221,20 +221,10 @@ static int get_pl_prim(struct rapl_domain *rd, int pl, enum pl_prims prim)
221221
#define power_zone_to_rapl_domain(_zone) \
222222
container_of(_zone, struct rapl_domain, power_zone)
223223

224-
struct rapl_defaults {
225-
u8 floor_freq_reg_addr;
226-
int (*check_unit)(struct rapl_domain *rd);
227-
void (*set_floor_freq)(struct rapl_domain *rd, bool mode);
228-
u64 (*compute_time_window)(struct rapl_domain *rd, u64 val,
229-
bool to_raw);
230-
unsigned int dram_domain_energy_unit;
231-
unsigned int psys_domain_energy_unit;
232-
bool spr_psys_bits;
233-
};
234-
static struct rapl_defaults *defaults_msr;
224+
static const struct rapl_defaults *defaults_msr;
235225
static const struct rapl_defaults defaults_tpmi;
236226

237-
static struct rapl_defaults *get_defaults(struct rapl_package *rp)
227+
static const struct rapl_defaults *get_defaults(struct rapl_package *rp)
238228
{
239229
return rp->priv->defaults;
240230
}
@@ -351,7 +341,7 @@ static int find_nr_power_limit(struct rapl_domain *rd)
351341
static int set_domain_enable(struct powercap_zone *power_zone, bool mode)
352342
{
353343
struct rapl_domain *rd = power_zone_to_rapl_domain(power_zone);
354-
struct rapl_defaults *defaults = get_defaults(rd->rp);
344+
const struct rapl_defaults *defaults = get_defaults(rd->rp);
355345
u64 val;
356346
int ret;
357347

@@ -640,7 +630,7 @@ static u64 rapl_unit_xlate(struct rapl_domain *rd, enum unit_type type,
640630
u64 value, int to_raw)
641631
{
642632
u64 units = 1;
643-
struct rapl_defaults *defaults = get_defaults(rd->rp);
633+
const struct rapl_defaults *defaults = get_defaults(rd->rp);
644634
u64 scale = 1;
645635

646636
switch (type) {
@@ -785,11 +775,11 @@ static int rapl_config(struct rapl_package *rp)
785775
/* MMIO I/F shares the same register layout as MSR registers */
786776
case RAPL_IF_MMIO:
787777
case RAPL_IF_MSR:
788-
rp->priv->defaults = (void *)defaults_msr;
778+
rp->priv->defaults = defaults_msr;
789779
rp->priv->rpi = (void *)rpi_msr;
790780
break;
791781
case RAPL_IF_TPMI:
792-
rp->priv->defaults = (void *)&defaults_tpmi;
782+
rp->priv->defaults = &defaults_tpmi;
793783
rp->priv->rpi = (void *)rpi_tpmi;
794784
break;
795785
default:
@@ -806,7 +796,7 @@ static int rapl_config(struct rapl_package *rp)
806796
static enum rapl_primitives
807797
prim_fixups(struct rapl_domain *rd, enum rapl_primitives prim)
808798
{
809-
struct rapl_defaults *defaults = get_defaults(rd->rp);
799+
const struct rapl_defaults *defaults = get_defaults(rd->rp);
810800

811801
if (!defaults->spr_psys_bits)
812802
return prim;
@@ -951,7 +941,7 @@ static int rapl_write_pl_data(struct rapl_domain *rd, int pl,
951941
* power unit : microWatts : Represented in milliWatts by default
952942
* time unit : microseconds: Represented in seconds by default
953943
*/
954-
static int rapl_check_unit_core(struct rapl_domain *rd)
944+
int rapl_default_check_unit(struct rapl_domain *rd)
955945
{
956946
struct reg_action ra;
957947
u32 value;
@@ -978,6 +968,7 @@ static int rapl_check_unit_core(struct rapl_domain *rd)
978968

979969
return 0;
980970
}
971+
EXPORT_SYMBOL_NS_GPL(rapl_default_check_unit, "INTEL_RAPL");
981972

982973
static int rapl_check_unit_atom(struct rapl_domain *rd)
983974
{
@@ -1071,7 +1062,7 @@ static void package_power_limit_irq_restore(struct rapl_package *rp)
10711062
wrmsr_safe(MSR_IA32_PACKAGE_THERM_INTERRUPT, l, h);
10721063
}
10731064

1074-
static void set_floor_freq_default(struct rapl_domain *rd, bool mode)
1065+
void rapl_default_set_floor_freq(struct rapl_domain *rd, bool mode)
10751066
{
10761067
int i;
10771068

@@ -1085,11 +1076,12 @@ static void set_floor_freq_default(struct rapl_domain *rd, bool mode)
10851076
rapl_write_pl_data(rd, i, PL_CLAMP, mode);
10861077
}
10871078
}
1079+
EXPORT_SYMBOL_NS_GPL(rapl_default_set_floor_freq, "INTEL_RAPL");
10881080

10891081
static void set_floor_freq_atom(struct rapl_domain *rd, bool enable)
10901082
{
10911083
static u32 power_ctrl_orig_val;
1092-
struct rapl_defaults *defaults = get_defaults(rd->rp);
1084+
const struct rapl_defaults *defaults = get_defaults(rd->rp);
10931085
u32 mdata;
10941086

10951087
if (!defaults->floor_freq_reg_addr) {
@@ -1110,8 +1102,7 @@ static void set_floor_freq_atom(struct rapl_domain *rd, bool enable)
11101102
defaults->floor_freq_reg_addr, mdata);
11111103
}
11121104

1113-
static u64 rapl_compute_time_window_core(struct rapl_domain *rd, u64 value,
1114-
bool to_raw)
1105+
u64 rapl_default_compute_time_window(struct rapl_domain *rd, u64 value, bool to_raw)
11151106
{
11161107
u64 f, y; /* fraction and exp. used for time unit */
11171108

@@ -1142,6 +1133,7 @@ static u64 rapl_compute_time_window_core(struct rapl_domain *rd, u64 value,
11421133
}
11431134
return value;
11441135
}
1136+
EXPORT_SYMBOL_NS_GPL(rapl_default_compute_time_window, "INTEL_RAPL");
11451137

11461138
static u64 rapl_compute_time_window_atom(struct rapl_domain *rd, u64 value,
11471139
bool to_raw)
@@ -1187,28 +1179,28 @@ static int rapl_check_unit_tpmi(struct rapl_domain *rd)
11871179
static const struct rapl_defaults defaults_tpmi = {
11881180
.check_unit = rapl_check_unit_tpmi,
11891181
/* Reuse existing logic, ignore the PL_CLAMP failures and enable all Power Limits */
1190-
.set_floor_freq = set_floor_freq_default,
1191-
.compute_time_window = rapl_compute_time_window_core,
1182+
.set_floor_freq = rapl_default_set_floor_freq,
1183+
.compute_time_window = rapl_default_compute_time_window,
11921184
};
11931185

11941186
static const struct rapl_defaults rapl_defaults_core = {
11951187
.floor_freq_reg_addr = 0,
1196-
.check_unit = rapl_check_unit_core,
1197-
.set_floor_freq = set_floor_freq_default,
1198-
.compute_time_window = rapl_compute_time_window_core,
1188+
.check_unit = rapl_default_check_unit,
1189+
.set_floor_freq = rapl_default_set_floor_freq,
1190+
.compute_time_window = rapl_default_compute_time_window,
11991191
};
12001192

12011193
static const struct rapl_defaults rapl_defaults_hsw_server = {
1202-
.check_unit = rapl_check_unit_core,
1203-
.set_floor_freq = set_floor_freq_default,
1204-
.compute_time_window = rapl_compute_time_window_core,
1194+
.check_unit = rapl_default_check_unit,
1195+
.set_floor_freq = rapl_default_set_floor_freq,
1196+
.compute_time_window = rapl_default_compute_time_window,
12051197
.dram_domain_energy_unit = 15300,
12061198
};
12071199

12081200
static const struct rapl_defaults rapl_defaults_spr_server = {
1209-
.check_unit = rapl_check_unit_core,
1210-
.set_floor_freq = set_floor_freq_default,
1211-
.compute_time_window = rapl_compute_time_window_core,
1201+
.check_unit = rapl_default_check_unit,
1202+
.set_floor_freq = rapl_default_set_floor_freq,
1203+
.compute_time_window = rapl_default_compute_time_window,
12121204
.psys_domain_energy_unit = NANOJOULE_PER_JOULE,
12131205
.spr_psys_bits = true,
12141206
};
@@ -1242,7 +1234,7 @@ static const struct rapl_defaults rapl_defaults_cht = {
12421234
};
12431235

12441236
static const struct rapl_defaults rapl_defaults_amd = {
1245-
.check_unit = rapl_check_unit_core,
1237+
.check_unit = rapl_default_check_unit,
12461238
};
12471239

12481240
static const struct x86_cpu_id rapl_ids[] __initconst = {
@@ -1448,7 +1440,7 @@ static int rapl_check_domain(int domain, struct rapl_package *rp)
14481440
*/
14491441
static int rapl_get_domain_unit(struct rapl_domain *rd)
14501442
{
1451-
struct rapl_defaults *defaults = get_defaults(rd->rp);
1443+
const struct rapl_defaults *defaults = get_defaults(rd->rp);
14521444
int ret;
14531445

14541446
if (!rd->regs[RAPL_DOMAIN_REG_UNIT].val) {
@@ -2341,7 +2333,7 @@ static int __init rapl_init(void)
23412333

23422334
id = x86_match_cpu(rapl_ids);
23432335
if (id) {
2344-
defaults_msr = (struct rapl_defaults *)id->driver_data;
2336+
defaults_msr = (const struct rapl_defaults *)id->driver_data;
23452337

23462338
rapl_msr_platdev = platform_device_alloc("intel_rapl_msr", 0);
23472339
if (!rapl_msr_platdev)

include/linux/intel_rapl.h

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -128,6 +128,16 @@ struct reg_action {
128128
int err;
129129
};
130130

131+
struct rapl_defaults {
132+
u8 floor_freq_reg_addr;
133+
int (*check_unit)(struct rapl_domain *rd);
134+
void (*set_floor_freq)(struct rapl_domain *rd, bool mode);
135+
u64 (*compute_time_window)(struct rapl_domain *rd, u64 val, bool to_raw);
136+
unsigned int dram_domain_energy_unit;
137+
unsigned int psys_domain_energy_unit;
138+
bool spr_psys_bits;
139+
};
140+
131141
/**
132142
* struct rapl_if_priv: private data for different RAPL interfaces
133143
* @control_type: Each RAPL interface must have its own powercap
@@ -142,7 +152,7 @@ struct reg_action {
142152
* registers.
143153
* @write_raw: Callback for writing RAPL interface specific
144154
* registers.
145-
* @defaults: internal pointer to interface default settings
155+
* @defaults: pointer to default settings
146156
* @rpi: internal pointer to interface primitive info
147157
*/
148158
struct rapl_if_priv {
@@ -154,7 +164,7 @@ struct rapl_if_priv {
154164
int limits[RAPL_DOMAIN_MAX];
155165
int (*read_raw)(int id, struct reg_action *ra, bool pmu_ctx);
156166
int (*write_raw)(int id, struct reg_action *ra);
157-
void *defaults;
167+
const struct rapl_defaults *defaults;
158168
void *rpi;
159169
};
160170

@@ -211,6 +221,9 @@ void rapl_remove_package_cpuslocked(struct rapl_package *rp);
211221
struct rapl_package *rapl_find_package_domain(int id, struct rapl_if_priv *priv, bool id_is_cpu);
212222
struct rapl_package *rapl_add_package(int id, struct rapl_if_priv *priv, bool id_is_cpu);
213223
void rapl_remove_package(struct rapl_package *rp);
224+
int rapl_default_check_unit(struct rapl_domain *rd);
225+
void rapl_default_set_floor_freq(struct rapl_domain *rd, bool mode);
226+
u64 rapl_default_compute_time_window(struct rapl_domain *rd, u64 value, bool to_raw);
214227

215228
#ifdef CONFIG_PERF_EVENTS
216229
int rapl_package_add_pmu(struct rapl_package *rp);

0 commit comments

Comments
 (0)