Merge branch 'powercap'
* powercap: PowerCap: Convert class code to use dev_groups PowerCap: Introduce Intel RAPL power capping driver bitops: Introduce BIT_ULL x86 / msr: add 64bit _on_cpu access functions PowerCap: Add to drivers Kconfig and Makefile PowerCap: Add class driver PowerCap: Documentation
This commit is contained in:
commit
ce6bceabae
|
@ -0,0 +1,152 @@
|
|||
What: /sys/class/powercap/
|
||||
Date: September 2013
|
||||
KernelVersion: 3.13
|
||||
Contact: linux-pm@vger.kernel.org
|
||||
Description:
|
||||
The powercap/ class sub directory belongs to the power cap
|
||||
subsystem. Refer to
|
||||
Documentation/power/powercap/powercap.txt for details.
|
||||
|
||||
What: /sys/class/powercap/<control type>
|
||||
Date: September 2013
|
||||
KernelVersion: 3.13
|
||||
Contact: linux-pm@vger.kernel.org
|
||||
Description:
|
||||
A <control type> is a unique name under /sys/class/powercap.
|
||||
Here <control type> determines how the power is going to be
|
||||
controlled. A <control type> can contain multiple power zones.
|
||||
|
||||
What: /sys/class/powercap/<control type>/enabled
|
||||
Date: September 2013
|
||||
KernelVersion: 3.13
|
||||
Contact: linux-pm@vger.kernel.org
|
||||
Description:
|
||||
This allows to enable/disable power capping for a "control type".
|
||||
This status affects every power zone using this "control_type.
|
||||
|
||||
What: /sys/class/powercap/<control type>/<power zone>
|
||||
Date: September 2013
|
||||
KernelVersion: 3.13
|
||||
Contact: linux-pm@vger.kernel.org
|
||||
Description:
|
||||
A power zone is a single or a collection of devices, which can
|
||||
be independently monitored and controlled. A power zone sysfs
|
||||
entry is qualified with the name of the <control type>.
|
||||
E.g. intel-rapl:0:1:1.
|
||||
|
||||
What: /sys/class/powercap/<control type>/<power zone>/<child power zone>
|
||||
Date: September 2013
|
||||
KernelVersion: 3.13
|
||||
Contact: linux-pm@vger.kernel.org
|
||||
Description:
|
||||
Power zones may be organized in a hierarchy in which child
|
||||
power zones provide monitoring and control for a subset of
|
||||
devices under the parent. For example, if there is a parent
|
||||
power zone for a whole CPU package, each CPU core in it can
|
||||
be a child power zone.
|
||||
|
||||
What: /sys/class/powercap/.../<power zone>/name
|
||||
Date: September 2013
|
||||
KernelVersion: 3.13
|
||||
Contact: linux-pm@vger.kernel.org
|
||||
Description:
|
||||
Specifies the name of this power zone.
|
||||
|
||||
What: /sys/class/powercap/.../<power zone>/energy_uj
|
||||
Date: September 2013
|
||||
KernelVersion: 3.13
|
||||
Contact: linux-pm@vger.kernel.org
|
||||
Description:
|
||||
Current energy counter in micro-joules. Write "0" to reset.
|
||||
If the counter can not be reset, then this attribute is
|
||||
read-only.
|
||||
|
||||
What: /sys/class/powercap/.../<power zone>/max_energy_range_uj
|
||||
Date: September 2013
|
||||
KernelVersion: 3.13
|
||||
Contact: linux-pm@vger.kernel.org
|
||||
Description:
|
||||
Range of the above energy counter in micro-joules.
|
||||
|
||||
|
||||
What: /sys/class/powercap/.../<power zone>/power_uw
|
||||
Date: September 2013
|
||||
KernelVersion: 3.13
|
||||
Contact: linux-pm@vger.kernel.org
|
||||
Description:
|
||||
Current power in micro-watts.
|
||||
|
||||
What: /sys/class/powercap/.../<power zone>/max_power_range_uw
|
||||
Date: September 2013
|
||||
KernelVersion: 3.13
|
||||
Contact: linux-pm@vger.kernel.org
|
||||
Description:
|
||||
Range of the above power value in micro-watts.
|
||||
|
||||
What: /sys/class/powercap/.../<power zone>/constraint_X_name
|
||||
Date: September 2013
|
||||
KernelVersion: 3.13
|
||||
Contact: linux-pm@vger.kernel.org
|
||||
Description:
|
||||
Each power zone can define one or more constraints. Each
|
||||
constraint can have an optional name. Here "X" can have values
|
||||
from 0 to max integer.
|
||||
|
||||
What: /sys/class/powercap/.../<power zone>/constraint_X_power_limit_uw
|
||||
Date: September 2013
|
||||
KernelVersion: 3.13
|
||||
Contact: linux-pm@vger.kernel.org
|
||||
Description:
|
||||
Power limit in micro-watts should be applicable for
|
||||
the time window specified by "constraint_X_time_window_us".
|
||||
Here "X" can have values from 0 to max integer.
|
||||
|
||||
What: /sys/class/powercap/.../<power zone>/constraint_X_time_window_us
|
||||
Date: September 2013
|
||||
KernelVersion: 3.13
|
||||
Contact: linux-pm@vger.kernel.org
|
||||
Description:
|
||||
Time window in micro seconds. This is used along with
|
||||
constraint_X_power_limit_uw to define a power constraint.
|
||||
Here "X" can have values from 0 to max integer.
|
||||
|
||||
|
||||
What: /sys/class/powercap/<control type>/.../constraint_X_max_power_uw
|
||||
Date: September 2013
|
||||
KernelVersion: 3.13
|
||||
Contact: linux-pm@vger.kernel.org
|
||||
Description:
|
||||
Maximum allowed power in micro watts for this constraint.
|
||||
Here "X" can have values from 0 to max integer.
|
||||
|
||||
What: /sys/class/powercap/<control type>/.../constraint_X_min_power_uw
|
||||
Date: September 2013
|
||||
KernelVersion: 3.13
|
||||
Contact: linux-pm@vger.kernel.org
|
||||
Description:
|
||||
Minimum allowed power in micro watts for this constraint.
|
||||
Here "X" can have values from 0 to max integer.
|
||||
|
||||
What: /sys/class/powercap/.../<power zone>/constraint_X_max_time_window_us
|
||||
Date: September 2013
|
||||
KernelVersion: 3.13
|
||||
Contact: linux-pm@vger.kernel.org
|
||||
Description:
|
||||
Maximum allowed time window in micro seconds for this
|
||||
constraint. Here "X" can have values from 0 to max integer.
|
||||
|
||||
What: /sys/class/powercap/.../<power zone>/constraint_X_min_time_window_us
|
||||
Date: September 2013
|
||||
KernelVersion: 3.13
|
||||
Contact: linux-pm@vger.kernel.org
|
||||
Description:
|
||||
Minimum allowed time window in micro seconds for this
|
||||
constraint. Here "X" can have values from 0 to max integer.
|
||||
|
||||
What: /sys/class/powercap/.../<power zone>/enabled
|
||||
Date: September 2013
|
||||
KernelVersion: 3.13
|
||||
Contact: linux-pm@vger.kernel.org
|
||||
Description
|
||||
This allows to enable/disable power capping at power zone level.
|
||||
This applies to current power zone and its children.
|
|
@ -0,0 +1,236 @@
|
|||
Power Capping Framework
|
||||
==================================
|
||||
|
||||
The power capping framework provides a consistent interface between the kernel
|
||||
and the user space that allows power capping drivers to expose the settings to
|
||||
user space in a uniform way.
|
||||
|
||||
Terminology
|
||||
=========================
|
||||
The framework exposes power capping devices to user space via sysfs in the
|
||||
form of a tree of objects. The objects at the root level of the tree represent
|
||||
'control types', which correspond to different methods of power capping. For
|
||||
example, the intel-rapl control type represents the Intel "Running Average
|
||||
Power Limit" (RAPL) technology, whereas the 'idle-injection' control type
|
||||
corresponds to the use of idle injection for controlling power.
|
||||
|
||||
Power zones represent different parts of the system, which can be controlled and
|
||||
monitored using the power capping method determined by the control type the
|
||||
given zone belongs to. They each contain attributes for monitoring power, as
|
||||
well as controls represented in the form of power constraints. If the parts of
|
||||
the system represented by different power zones are hierarchical (that is, one
|
||||
bigger part consists of multiple smaller parts that each have their own power
|
||||
controls), those power zones may also be organized in a hierarchy with one
|
||||
parent power zone containing multiple subzones and so on to reflect the power
|
||||
control topology of the system. In that case, it is possible to apply power
|
||||
capping to a set of devices together using the parent power zone and if more
|
||||
fine grained control is required, it can be applied through the subzones.
|
||||
|
||||
|
||||
Example sysfs interface tree:
|
||||
|
||||
/sys/devices/virtual/powercap
|
||||
??? intel-rapl
|
||||
??? intel-rapl:0
|
||||
? ??? constraint_0_name
|
||||
? ??? constraint_0_power_limit_uw
|
||||
? ??? constraint_0_time_window_us
|
||||
? ??? constraint_1_name
|
||||
? ??? constraint_1_power_limit_uw
|
||||
? ??? constraint_1_time_window_us
|
||||
? ??? device -> ../../intel-rapl
|
||||
? ??? energy_uj
|
||||
? ??? intel-rapl:0:0
|
||||
? ? ??? constraint_0_name
|
||||
? ? ??? constraint_0_power_limit_uw
|
||||
? ? ??? constraint_0_time_window_us
|
||||
? ? ??? constraint_1_name
|
||||
? ? ??? constraint_1_power_limit_uw
|
||||
? ? ??? constraint_1_time_window_us
|
||||
? ? ??? device -> ../../intel-rapl:0
|
||||
? ? ??? energy_uj
|
||||
? ? ??? max_energy_range_uj
|
||||
? ? ??? name
|
||||
? ? ??? enabled
|
||||
? ? ??? power
|
||||
? ? ? ??? async
|
||||
? ? ? []
|
||||
? ? ??? subsystem -> ../../../../../../class/power_cap
|
||||
? ? ??? uevent
|
||||
? ??? intel-rapl:0:1
|
||||
? ? ??? constraint_0_name
|
||||
? ? ??? constraint_0_power_limit_uw
|
||||
? ? ??? constraint_0_time_window_us
|
||||
? ? ??? constraint_1_name
|
||||
? ? ??? constraint_1_power_limit_uw
|
||||
? ? ??? constraint_1_time_window_us
|
||||
? ? ??? device -> ../../intel-rapl:0
|
||||
? ? ??? energy_uj
|
||||
? ? ??? max_energy_range_uj
|
||||
? ? ??? name
|
||||
? ? ??? enabled
|
||||
? ? ??? power
|
||||
? ? ? ??? async
|
||||
? ? ? []
|
||||
? ? ??? subsystem -> ../../../../../../class/power_cap
|
||||
? ? ??? uevent
|
||||
? ??? max_energy_range_uj
|
||||
? ??? max_power_range_uw
|
||||
? ??? name
|
||||
? ??? enabled
|
||||
? ??? power
|
||||
? ? ??? async
|
||||
? ? []
|
||||
? ??? subsystem -> ../../../../../class/power_cap
|
||||
? ??? enabled
|
||||
? ??? uevent
|
||||
??? intel-rapl:1
|
||||
? ??? constraint_0_name
|
||||
? ??? constraint_0_power_limit_uw
|
||||
? ??? constraint_0_time_window_us
|
||||
? ??? constraint_1_name
|
||||
? ??? constraint_1_power_limit_uw
|
||||
? ??? constraint_1_time_window_us
|
||||
? ??? device -> ../../intel-rapl
|
||||
? ??? energy_uj
|
||||
? ??? intel-rapl:1:0
|
||||
? ? ??? constraint_0_name
|
||||
? ? ??? constraint_0_power_limit_uw
|
||||
? ? ??? constraint_0_time_window_us
|
||||
? ? ??? constraint_1_name
|
||||
? ? ??? constraint_1_power_limit_uw
|
||||
? ? ??? constraint_1_time_window_us
|
||||
? ? ??? device -> ../../intel-rapl:1
|
||||
? ? ??? energy_uj
|
||||
? ? ??? max_energy_range_uj
|
||||
? ? ??? name
|
||||
? ? ??? enabled
|
||||
? ? ??? power
|
||||
? ? ? ??? async
|
||||
? ? ? []
|
||||
? ? ??? subsystem -> ../../../../../../class/power_cap
|
||||
? ? ??? uevent
|
||||
? ??? intel-rapl:1:1
|
||||
? ? ??? constraint_0_name
|
||||
? ? ??? constraint_0_power_limit_uw
|
||||
? ? ??? constraint_0_time_window_us
|
||||
? ? ??? constraint_1_name
|
||||
? ? ??? constraint_1_power_limit_uw
|
||||
? ? ??? constraint_1_time_window_us
|
||||
? ? ??? device -> ../../intel-rapl:1
|
||||
? ? ??? energy_uj
|
||||
? ? ??? max_energy_range_uj
|
||||
? ? ??? name
|
||||
? ? ??? enabled
|
||||
? ? ??? power
|
||||
? ? ? ??? async
|
||||
? ? ? []
|
||||
? ? ??? subsystem -> ../../../../../../class/power_cap
|
||||
? ? ??? uevent
|
||||
? ??? max_energy_range_uj
|
||||
? ??? max_power_range_uw
|
||||
? ??? name
|
||||
? ??? enabled
|
||||
? ??? power
|
||||
? ? ??? async
|
||||
? ? []
|
||||
? ??? subsystem -> ../../../../../class/power_cap
|
||||
? ??? uevent
|
||||
??? power
|
||||
? ??? async
|
||||
? []
|
||||
??? subsystem -> ../../../../class/power_cap
|
||||
??? enabled
|
||||
??? uevent
|
||||
|
||||
The above example illustrates a case in which the Intel RAPL technology,
|
||||
available in Intel® IA-64 and IA-32 Processor Architectures, is used. There is one
|
||||
control type called intel-rapl which contains two power zones, intel-rapl:0 and
|
||||
intel-rapl:1, representing CPU packages. Each of these power zones contains
|
||||
two subzones, intel-rapl:j:0 and intel-rapl:j:1 (j = 0, 1), representing the
|
||||
"core" and the "uncore" parts of the given CPU package, respectively. All of
|
||||
the zones and subzones contain energy monitoring attributes (energy_uj,
|
||||
max_energy_range_uj) and constraint attributes (constraint_*) allowing controls
|
||||
to be applied (the constraints in the 'package' power zones apply to the whole
|
||||
CPU packages and the subzone constraints only apply to the respective parts of
|
||||
the given package individually). Since Intel RAPL doesn't provide instantaneous
|
||||
power value, there is no power_uw attribute.
|
||||
|
||||
In addition to that, each power zone contains a name attribute, allowing the
|
||||
part of the system represented by that zone to be identified.
|
||||
For example:
|
||||
|
||||
cat /sys/class/power_cap/intel-rapl/intel-rapl:0/name
|
||||
package-0
|
||||
|
||||
The Intel RAPL technology allows two constraints, short term and long term,
|
||||
with two different time windows to be applied to each power zone. Thus for
|
||||
each zone there are 2 attributes representing the constraint names, 2 power
|
||||
limits and 2 attributes representing the sizes of the time windows. Such that,
|
||||
constraint_j_* attributes correspond to the jth constraint (j = 0,1).
|
||||
|
||||
For example:
|
||||
constraint_0_name
|
||||
constraint_0_power_limit_uw
|
||||
constraint_0_time_window_us
|
||||
constraint_1_name
|
||||
constraint_1_power_limit_uw
|
||||
constraint_1_time_window_us
|
||||
|
||||
Power Zone Attributes
|
||||
=================================
|
||||
Monitoring attributes
|
||||
----------------------
|
||||
|
||||
energy_uj (rw): Current energy counter in micro joules. Write "0" to reset.
|
||||
If the counter can not be reset, then this attribute is read only.
|
||||
|
||||
max_energy_range_uj (ro): Range of the above energy counter in micro-joules.
|
||||
|
||||
power_uw (ro): Current power in micro watts.
|
||||
|
||||
max_power_range_uw (ro): Range of the above power value in micro-watts.
|
||||
|
||||
name (ro): Name of this power zone.
|
||||
|
||||
It is possible that some domains have both power ranges and energy counter ranges;
|
||||
however, only one is mandatory.
|
||||
|
||||
Constraints
|
||||
----------------
|
||||
constraint_X_power_limit_uw (rw): Power limit in micro watts, which should be
|
||||
applicable for the time window specified by "constraint_X_time_window_us".
|
||||
|
||||
constraint_X_time_window_us (rw): Time window in micro seconds.
|
||||
|
||||
constraint_X_name (ro): An optional name of the constraint
|
||||
|
||||
constraint_X_max_power_uw(ro): Maximum allowed power in micro watts.
|
||||
|
||||
constraint_X_min_power_uw(ro): Minimum allowed power in micro watts.
|
||||
|
||||
constraint_X_max_time_window_us(ro): Maximum allowed time window in micro seconds.
|
||||
|
||||
constraint_X_min_time_window_us(ro): Minimum allowed time window in micro seconds.
|
||||
|
||||
Except power_limit_uw and time_window_us other fields are optional.
|
||||
|
||||
Common zone and control type attributes
|
||||
----------------------------------------
|
||||
enabled (rw): Enable/Disable controls at zone level or for all zones using
|
||||
a control type.
|
||||
|
||||
Power Cap Client Driver Interface
|
||||
==================================
|
||||
The API summary:
|
||||
|
||||
Call powercap_register_control_type() to register control type object.
|
||||
Call powercap_register_zone() to register a power zone (under a given
|
||||
control type), either as a top-level power zone or as a subzone of another
|
||||
power zone registered earlier.
|
||||
The number of constraints in a power zone and the corresponding callbacks have
|
||||
to be defined prior to calling powercap_register_zone() to register that zone.
|
||||
|
||||
To Free a power zone call powercap_unregister_zone().
|
||||
To free a control type object call powercap_unregister_control_type().
|
||||
Detailed API can be generated using kernel-doc on include/linux/powercap.h.
|
|
@ -218,10 +218,14 @@ void msrs_free(struct msr *msrs);
|
|||
#ifdef CONFIG_SMP
|
||||
int rdmsr_on_cpu(unsigned int cpu, u32 msr_no, u32 *l, u32 *h);
|
||||
int wrmsr_on_cpu(unsigned int cpu, u32 msr_no, u32 l, u32 h);
|
||||
int rdmsrl_on_cpu(unsigned int cpu, u32 msr_no, u64 *q);
|
||||
int wrmsrl_on_cpu(unsigned int cpu, u32 msr_no, u64 q);
|
||||
void rdmsr_on_cpus(const struct cpumask *mask, u32 msr_no, struct msr *msrs);
|
||||
void wrmsr_on_cpus(const struct cpumask *mask, u32 msr_no, struct msr *msrs);
|
||||
int rdmsr_safe_on_cpu(unsigned int cpu, u32 msr_no, u32 *l, u32 *h);
|
||||
int wrmsr_safe_on_cpu(unsigned int cpu, u32 msr_no, u32 l, u32 h);
|
||||
int rdmsrl_safe_on_cpu(unsigned int cpu, u32 msr_no, u64 *q);
|
||||
int wrmsrl_safe_on_cpu(unsigned int cpu, u32 msr_no, u64 q);
|
||||
int rdmsr_safe_regs_on_cpu(unsigned int cpu, u32 regs[8]);
|
||||
int wrmsr_safe_regs_on_cpu(unsigned int cpu, u32 regs[8]);
|
||||
#else /* CONFIG_SMP */
|
||||
|
@ -235,6 +239,16 @@ static inline int wrmsr_on_cpu(unsigned int cpu, u32 msr_no, u32 l, u32 h)
|
|||
wrmsr(msr_no, l, h);
|
||||
return 0;
|
||||
}
|
||||
static inline int rdmsrl_on_cpu(unsigned int cpu, u32 msr_no, u64 *q)
|
||||
{
|
||||
rdmsrl(msr_no, *q);
|
||||
return 0;
|
||||
}
|
||||
static inline int wrmsrl_on_cpu(unsigned int cpu, u32 msr_no, u64 q)
|
||||
{
|
||||
wrmsrl(msr_no, q);
|
||||
return 0;
|
||||
}
|
||||
static inline void rdmsr_on_cpus(const struct cpumask *m, u32 msr_no,
|
||||
struct msr *msrs)
|
||||
{
|
||||
|
@ -254,6 +268,14 @@ static inline int wrmsr_safe_on_cpu(unsigned int cpu, u32 msr_no, u32 l, u32 h)
|
|||
{
|
||||
return wrmsr_safe(msr_no, l, h);
|
||||
}
|
||||
static inline int rdmsrl_safe_on_cpu(unsigned int cpu, u32 msr_no, u64 *q)
|
||||
{
|
||||
return rdmsrl_safe(msr_no, q);
|
||||
}
|
||||
static inline int wrmsrl_safe_on_cpu(unsigned int cpu, u32 msr_no, u64 q)
|
||||
{
|
||||
return wrmsrl_safe(msr_no, q);
|
||||
}
|
||||
static inline int rdmsr_safe_regs_on_cpu(unsigned int cpu, u32 regs[8])
|
||||
{
|
||||
return rdmsr_safe_regs(regs);
|
||||
|
|
|
@ -47,6 +47,21 @@ int rdmsr_on_cpu(unsigned int cpu, u32 msr_no, u32 *l, u32 *h)
|
|||
}
|
||||
EXPORT_SYMBOL(rdmsr_on_cpu);
|
||||
|
||||
int rdmsrl_on_cpu(unsigned int cpu, u32 msr_no, u64 *q)
|
||||
{
|
||||
int err;
|
||||
struct msr_info rv;
|
||||
|
||||
memset(&rv, 0, sizeof(rv));
|
||||
|
||||
rv.msr_no = msr_no;
|
||||
err = smp_call_function_single(cpu, __rdmsr_on_cpu, &rv, 1);
|
||||
*q = rv.reg.q;
|
||||
|
||||
return err;
|
||||
}
|
||||
EXPORT_SYMBOL(rdmsrl_on_cpu);
|
||||
|
||||
int wrmsr_on_cpu(unsigned int cpu, u32 msr_no, u32 l, u32 h)
|
||||
{
|
||||
int err;
|
||||
|
@ -63,6 +78,22 @@ int wrmsr_on_cpu(unsigned int cpu, u32 msr_no, u32 l, u32 h)
|
|||
}
|
||||
EXPORT_SYMBOL(wrmsr_on_cpu);
|
||||
|
||||
int wrmsrl_on_cpu(unsigned int cpu, u32 msr_no, u64 q)
|
||||
{
|
||||
int err;
|
||||
struct msr_info rv;
|
||||
|
||||
memset(&rv, 0, sizeof(rv));
|
||||
|
||||
rv.msr_no = msr_no;
|
||||
rv.reg.q = q;
|
||||
|
||||
err = smp_call_function_single(cpu, __wrmsr_on_cpu, &rv, 1);
|
||||
|
||||
return err;
|
||||
}
|
||||
EXPORT_SYMBOL(wrmsrl_on_cpu);
|
||||
|
||||
static void __rwmsr_on_cpus(const struct cpumask *mask, u32 msr_no,
|
||||
struct msr *msrs,
|
||||
void (*msr_func) (void *info))
|
||||
|
@ -159,6 +190,37 @@ int wrmsr_safe_on_cpu(unsigned int cpu, u32 msr_no, u32 l, u32 h)
|
|||
}
|
||||
EXPORT_SYMBOL(wrmsr_safe_on_cpu);
|
||||
|
||||
int wrmsrl_safe_on_cpu(unsigned int cpu, u32 msr_no, u64 q)
|
||||
{
|
||||
int err;
|
||||
struct msr_info rv;
|
||||
|
||||
memset(&rv, 0, sizeof(rv));
|
||||
|
||||
rv.msr_no = msr_no;
|
||||
rv.reg.q = q;
|
||||
|
||||
err = smp_call_function_single(cpu, __wrmsr_safe_on_cpu, &rv, 1);
|
||||
|
||||
return err ? err : rv.err;
|
||||
}
|
||||
EXPORT_SYMBOL(wrmsrl_safe_on_cpu);
|
||||
|
||||
int rdmsrl_safe_on_cpu(unsigned int cpu, u32 msr_no, u64 *q)
|
||||
{
|
||||
int err;
|
||||
struct msr_info rv;
|
||||
|
||||
memset(&rv, 0, sizeof(rv));
|
||||
|
||||
rv.msr_no = msr_no;
|
||||
err = smp_call_function_single(cpu, __rdmsr_safe_on_cpu, &rv, 1);
|
||||
*q = rv.reg.q;
|
||||
|
||||
return err ? err : rv.err;
|
||||
}
|
||||
EXPORT_SYMBOL(rdmsrl_safe_on_cpu);
|
||||
|
||||
/*
|
||||
* These variants are significantly slower, but allows control over
|
||||
* the entire 32-bit GPR set.
|
||||
|
|
|
@ -166,4 +166,6 @@ source "drivers/reset/Kconfig"
|
|||
|
||||
source "drivers/fmc/Kconfig"
|
||||
|
||||
source "drivers/powercap/Kconfig"
|
||||
|
||||
endmenu
|
||||
|
|
|
@ -152,3 +152,4 @@ obj-$(CONFIG_VME_BUS) += vme/
|
|||
obj-$(CONFIG_IPACK_BUS) += ipack/
|
||||
obj-$(CONFIG_NTB) += ntb/
|
||||
obj-$(CONFIG_FMC) += fmc/
|
||||
obj-$(CONFIG_POWERCAP) += powercap/
|
||||
|
|
|
@ -0,0 +1,32 @@
|
|||
#
|
||||
# Generic power capping sysfs interface configuration
|
||||
#
|
||||
|
||||
menuconfig POWERCAP
|
||||
bool "Generic powercap sysfs driver"
|
||||
help
|
||||
The power capping sysfs interface allows kernel subsystems to expose power
|
||||
capping settings to user space in a consistent way. Usually, it consists
|
||||
of multiple control types that determine which settings may be exposed and
|
||||
power zones representing parts of the system that can be subject to power
|
||||
capping.
|
||||
|
||||
If you want this code to be compiled in, say Y here.
|
||||
|
||||
if POWERCAP
|
||||
# Client driver configurations go here.
|
||||
config INTEL_RAPL
|
||||
tristate "Intel RAPL Support"
|
||||
depends on X86
|
||||
default n
|
||||
---help---
|
||||
This enables support for the Intel Running Average Power Limit (RAPL)
|
||||
technology which allows power limits to be enforced and monitored on
|
||||
modern Intel processors (Sandy Bridge and later).
|
||||
|
||||
In RAPL, the platform level settings are divided into domains for
|
||||
fine grained control. These domains include processor package, DRAM
|
||||
controller, CPU core (Power Plance 0), graphics uncore (Power Plane
|
||||
1), etc.
|
||||
|
||||
endif
|
|
@ -0,0 +1,2 @@
|
|||
obj-$(CONFIG_POWERCAP) += powercap_sys.o
|
||||
obj-$(CONFIG_INTEL_RAPL) += intel_rapl.o
|
File diff suppressed because it is too large
Load Diff
|
@ -0,0 +1,685 @@
|
|||
/*
|
||||
* Power capping class
|
||||
* Copyright (c) 2013, Intel Corporation.
|
||||
*
|
||||
* This program is free software; you can redistribute it and/or modify it
|
||||
* under the terms and conditions of the GNU General Public License,
|
||||
* version 2, as published by the Free Software Foundation.
|
||||
*
|
||||
* This program is distributed in the hope it will be useful, but WITHOUT
|
||||
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
|
||||
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
|
||||
* more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License along with
|
||||
* this program; if not, write to the Free Software Foundation, Inc.
|
||||
*
|
||||
*/
|
||||
|
||||
#include <linux/module.h>
|
||||
#include <linux/device.h>
|
||||
#include <linux/err.h>
|
||||
#include <linux/slab.h>
|
||||
#include <linux/powercap.h>
|
||||
|
||||
#define to_powercap_zone(n) container_of(n, struct powercap_zone, dev)
|
||||
#define to_powercap_control_type(n) \
|
||||
container_of(n, struct powercap_control_type, dev)
|
||||
|
||||
/* Power zone show function */
|
||||
#define define_power_zone_show(_attr) \
|
||||
static ssize_t _attr##_show(struct device *dev, \
|
||||
struct device_attribute *dev_attr,\
|
||||
char *buf) \
|
||||
{ \
|
||||
u64 value; \
|
||||
ssize_t len = -EINVAL; \
|
||||
struct powercap_zone *power_zone = to_powercap_zone(dev); \
|
||||
\
|
||||
if (power_zone->ops->get_##_attr) { \
|
||||
if (!power_zone->ops->get_##_attr(power_zone, &value)) \
|
||||
len = sprintf(buf, "%lld\n", value); \
|
||||
} \
|
||||
\
|
||||
return len; \
|
||||
}
|
||||
|
||||
/* The only meaningful input is 0 (reset), others are silently ignored */
|
||||
#define define_power_zone_store(_attr) \
|
||||
static ssize_t _attr##_store(struct device *dev,\
|
||||
struct device_attribute *dev_attr, \
|
||||
const char *buf, size_t count) \
|
||||
{ \
|
||||
int err; \
|
||||
struct powercap_zone *power_zone = to_powercap_zone(dev); \
|
||||
u64 value; \
|
||||
\
|
||||
err = kstrtoull(buf, 10, &value); \
|
||||
if (err) \
|
||||
return -EINVAL; \
|
||||
if (value) \
|
||||
return count; \
|
||||
if (power_zone->ops->reset_##_attr) { \
|
||||
if (!power_zone->ops->reset_##_attr(power_zone)) \
|
||||
return count; \
|
||||
} \
|
||||
\
|
||||
return -EINVAL; \
|
||||
}
|
||||
|
||||
/* Power zone constraint show function */
|
||||
#define define_power_zone_constraint_show(_attr) \
|
||||
static ssize_t show_constraint_##_attr(struct device *dev, \
|
||||
struct device_attribute *dev_attr,\
|
||||
char *buf) \
|
||||
{ \
|
||||
u64 value; \
|
||||
ssize_t len = -ENODATA; \
|
||||
struct powercap_zone *power_zone = to_powercap_zone(dev); \
|
||||
int id; \
|
||||
struct powercap_zone_constraint *pconst;\
|
||||
\
|
||||
if (!sscanf(dev_attr->attr.name, "constraint_%d_", &id)) \
|
||||
return -EINVAL; \
|
||||
if (id >= power_zone->const_id_cnt) \
|
||||
return -EINVAL; \
|
||||
pconst = &power_zone->constraints[id]; \
|
||||
if (pconst && pconst->ops && pconst->ops->get_##_attr) { \
|
||||
if (!pconst->ops->get_##_attr(power_zone, id, &value)) \
|
||||
len = sprintf(buf, "%lld\n", value); \
|
||||
} \
|
||||
\
|
||||
return len; \
|
||||
}
|
||||
|
||||
/* Power zone constraint store function */
|
||||
#define define_power_zone_constraint_store(_attr) \
|
||||
static ssize_t store_constraint_##_attr(struct device *dev,\
|
||||
struct device_attribute *dev_attr, \
|
||||
const char *buf, size_t count) \
|
||||
{ \
|
||||
int err; \
|
||||
u64 value; \
|
||||
struct powercap_zone *power_zone = to_powercap_zone(dev); \
|
||||
int id; \
|
||||
struct powercap_zone_constraint *pconst;\
|
||||
\
|
||||
if (!sscanf(dev_attr->attr.name, "constraint_%d_", &id)) \
|
||||
return -EINVAL; \
|
||||
if (id >= power_zone->const_id_cnt) \
|
||||
return -EINVAL; \
|
||||
pconst = &power_zone->constraints[id]; \
|
||||
err = kstrtoull(buf, 10, &value); \
|
||||
if (err) \
|
||||
return -EINVAL; \
|
||||
if (pconst && pconst->ops && pconst->ops->set_##_attr) { \
|
||||
if (!pconst->ops->set_##_attr(power_zone, id, value)) \
|
||||
return count; \
|
||||
} \
|
||||
\
|
||||
return -ENODATA; \
|
||||
}
|
||||
|
||||
/* Power zone information callbacks */
|
||||
define_power_zone_show(power_uw);
|
||||
define_power_zone_show(max_power_range_uw);
|
||||
define_power_zone_show(energy_uj);
|
||||
define_power_zone_store(energy_uj);
|
||||
define_power_zone_show(max_energy_range_uj);
|
||||
|
||||
/* Power zone attributes */
|
||||
static DEVICE_ATTR_RO(max_power_range_uw);
|
||||
static DEVICE_ATTR_RO(power_uw);
|
||||
static DEVICE_ATTR_RO(max_energy_range_uj);
|
||||
static DEVICE_ATTR_RW(energy_uj);
|
||||
|
||||
/* Power zone constraint attributes callbacks */
|
||||
define_power_zone_constraint_show(power_limit_uw);
|
||||
define_power_zone_constraint_store(power_limit_uw);
|
||||
define_power_zone_constraint_show(time_window_us);
|
||||
define_power_zone_constraint_store(time_window_us);
|
||||
define_power_zone_constraint_show(max_power_uw);
|
||||
define_power_zone_constraint_show(min_power_uw);
|
||||
define_power_zone_constraint_show(max_time_window_us);
|
||||
define_power_zone_constraint_show(min_time_window_us);
|
||||
|
||||
/* For one time seeding of constraint device attributes */
|
||||
struct powercap_constraint_attr {
|
||||
struct device_attribute power_limit_attr;
|
||||
struct device_attribute time_window_attr;
|
||||
struct device_attribute max_power_attr;
|
||||
struct device_attribute min_power_attr;
|
||||
struct device_attribute max_time_window_attr;
|
||||
struct device_attribute min_time_window_attr;
|
||||
struct device_attribute name_attr;
|
||||
};
|
||||
|
||||
static struct powercap_constraint_attr
|
||||
constraint_attrs[MAX_CONSTRAINTS_PER_ZONE];
|
||||
|
||||
/* A list of powercap control_types */
|
||||
static LIST_HEAD(powercap_cntrl_list);
|
||||
/* Mutex to protect list of powercap control_types */
|
||||
static DEFINE_MUTEX(powercap_cntrl_list_lock);
|
||||
|
||||
#define POWERCAP_CONSTRAINT_NAME_LEN 30 /* Some limit to avoid overflow */
|
||||
static ssize_t show_constraint_name(struct device *dev,
|
||||
struct device_attribute *dev_attr,
|
||||
char *buf)
|
||||
{
|
||||
const char *name;
|
||||
struct powercap_zone *power_zone = to_powercap_zone(dev);
|
||||
int id;
|
||||
ssize_t len = -ENODATA;
|
||||
struct powercap_zone_constraint *pconst;
|
||||
|
||||
if (!sscanf(dev_attr->attr.name, "constraint_%d_", &id))
|
||||
return -EINVAL;
|
||||
if (id >= power_zone->const_id_cnt)
|
||||
return -EINVAL;
|
||||
pconst = &power_zone->constraints[id];
|
||||
|
||||
if (pconst && pconst->ops && pconst->ops->get_name) {
|
||||
name = pconst->ops->get_name(power_zone, id);
|
||||
if (name) {
|
||||
snprintf(buf, POWERCAP_CONSTRAINT_NAME_LEN,
|
||||
"%s\n", name);
|
||||
buf[POWERCAP_CONSTRAINT_NAME_LEN] = '\0';
|
||||
len = strlen(buf);
|
||||
}
|
||||
}
|
||||
|
||||
return len;
|
||||
}
|
||||
|
||||
static int create_constraint_attribute(int id, const char *name,
|
||||
int mode,
|
||||
struct device_attribute *dev_attr,
|
||||
ssize_t (*show)(struct device *,
|
||||
struct device_attribute *, char *),
|
||||
ssize_t (*store)(struct device *,
|
||||
struct device_attribute *,
|
||||
const char *, size_t)
|
||||
)
|
||||
{
|
||||
|
||||
dev_attr->attr.name = kasprintf(GFP_KERNEL, "constraint_%d_%s",
|
||||
id, name);
|
||||
if (!dev_attr->attr.name)
|
||||
return -ENOMEM;
|
||||
dev_attr->attr.mode = mode;
|
||||
dev_attr->show = show;
|
||||
dev_attr->store = store;
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
static void free_constraint_attributes(void)
|
||||
{
|
||||
int i;
|
||||
|
||||
for (i = 0; i < MAX_CONSTRAINTS_PER_ZONE; ++i) {
|
||||
kfree(constraint_attrs[i].power_limit_attr.attr.name);
|
||||
kfree(constraint_attrs[i].time_window_attr.attr.name);
|
||||
kfree(constraint_attrs[i].name_attr.attr.name);
|
||||
kfree(constraint_attrs[i].max_power_attr.attr.name);
|
||||
kfree(constraint_attrs[i].min_power_attr.attr.name);
|
||||
kfree(constraint_attrs[i].max_time_window_attr.attr.name);
|
||||
kfree(constraint_attrs[i].min_time_window_attr.attr.name);
|
||||
}
|
||||
}
|
||||
|
||||
static int seed_constraint_attributes(void)
|
||||
{
|
||||
int i;
|
||||
int ret;
|
||||
|
||||
for (i = 0; i < MAX_CONSTRAINTS_PER_ZONE; ++i) {
|
||||
ret = create_constraint_attribute(i, "power_limit_uw",
|
||||
S_IWUSR | S_IRUGO,
|
||||
&constraint_attrs[i].power_limit_attr,
|
||||
show_constraint_power_limit_uw,
|
||||
store_constraint_power_limit_uw);
|
||||
if (ret)
|
||||
goto err_alloc;
|
||||
ret = create_constraint_attribute(i, "time_window_us",
|
||||
S_IWUSR | S_IRUGO,
|
||||
&constraint_attrs[i].time_window_attr,
|
||||
show_constraint_time_window_us,
|
||||
store_constraint_time_window_us);
|
||||
if (ret)
|
||||
goto err_alloc;
|
||||
ret = create_constraint_attribute(i, "name", S_IRUGO,
|
||||
&constraint_attrs[i].name_attr,
|
||||
show_constraint_name,
|
||||
NULL);
|
||||
if (ret)
|
||||
goto err_alloc;
|
||||
ret = create_constraint_attribute(i, "max_power_uw", S_IRUGO,
|
||||
&constraint_attrs[i].max_power_attr,
|
||||
show_constraint_max_power_uw,
|
||||
NULL);
|
||||
if (ret)
|
||||
goto err_alloc;
|
||||
ret = create_constraint_attribute(i, "min_power_uw", S_IRUGO,
|
||||
&constraint_attrs[i].min_power_attr,
|
||||
show_constraint_min_power_uw,
|
||||
NULL);
|
||||
if (ret)
|
||||
goto err_alloc;
|
||||
ret = create_constraint_attribute(i, "max_time_window_us",
|
||||
S_IRUGO,
|
||||
&constraint_attrs[i].max_time_window_attr,
|
||||
show_constraint_max_time_window_us,
|
||||
NULL);
|
||||
if (ret)
|
||||
goto err_alloc;
|
||||
ret = create_constraint_attribute(i, "min_time_window_us",
|
||||
S_IRUGO,
|
||||
&constraint_attrs[i].min_time_window_attr,
|
||||
show_constraint_min_time_window_us,
|
||||
NULL);
|
||||
if (ret)
|
||||
goto err_alloc;
|
||||
|
||||
}
|
||||
|
||||
return 0;
|
||||
|
||||
err_alloc:
|
||||
free_constraint_attributes();
|
||||
|
||||
return ret;
|
||||
}
|
||||
|
||||
static int create_constraints(struct powercap_zone *power_zone,
|
||||
int nr_constraints,
|
||||
struct powercap_zone_constraint_ops *const_ops)
|
||||
{
|
||||
int i;
|
||||
int ret = 0;
|
||||
int count;
|
||||
struct powercap_zone_constraint *pconst;
|
||||
|
||||
if (!power_zone || !const_ops || !const_ops->get_power_limit_uw ||
|
||||
!const_ops->set_power_limit_uw ||
|
||||
!const_ops->get_time_window_us ||
|
||||
!const_ops->set_time_window_us)
|
||||
return -EINVAL;
|
||||
|
||||
count = power_zone->zone_attr_count;
|
||||
for (i = 0; i < nr_constraints; ++i) {
|
||||
pconst = &power_zone->constraints[i];
|
||||
pconst->ops = const_ops;
|
||||
pconst->id = power_zone->const_id_cnt;
|
||||
power_zone->const_id_cnt++;
|
||||
power_zone->zone_dev_attrs[count++] =
|
||||
&constraint_attrs[i].power_limit_attr.attr;
|
||||
power_zone->zone_dev_attrs[count++] =
|
||||
&constraint_attrs[i].time_window_attr.attr;
|
||||
if (pconst->ops->get_name)
|
||||
power_zone->zone_dev_attrs[count++] =
|
||||
&constraint_attrs[i].name_attr.attr;
|
||||
if (pconst->ops->get_max_power_uw)
|
||||
power_zone->zone_dev_attrs[count++] =
|
||||
&constraint_attrs[i].max_power_attr.attr;
|
||||
if (pconst->ops->get_min_power_uw)
|
||||
power_zone->zone_dev_attrs[count++] =
|
||||
&constraint_attrs[i].min_power_attr.attr;
|
||||
if (pconst->ops->get_max_time_window_us)
|
||||
power_zone->zone_dev_attrs[count++] =
|
||||
&constraint_attrs[i].max_time_window_attr.attr;
|
||||
if (pconst->ops->get_min_time_window_us)
|
||||
power_zone->zone_dev_attrs[count++] =
|
||||
&constraint_attrs[i].min_time_window_attr.attr;
|
||||
}
|
||||
power_zone->zone_attr_count = count;
|
||||
|
||||
return ret;
|
||||
}
|
||||
|
||||
static bool control_type_valid(void *control_type)
|
||||
{
|
||||
struct powercap_control_type *pos = NULL;
|
||||
bool found = false;
|
||||
|
||||
mutex_lock(&powercap_cntrl_list_lock);
|
||||
|
||||
list_for_each_entry(pos, &powercap_cntrl_list, node) {
|
||||
if (pos == control_type) {
|
||||
found = true;
|
||||
break;
|
||||
}
|
||||
}
|
||||
mutex_unlock(&powercap_cntrl_list_lock);
|
||||
|
||||
return found;
|
||||
}
|
||||
|
||||
static ssize_t name_show(struct device *dev,
|
||||
struct device_attribute *attr,
|
||||
char *buf)
|
||||
{
|
||||
struct powercap_zone *power_zone = to_powercap_zone(dev);
|
||||
|
||||
return sprintf(buf, "%s\n", power_zone->name);
|
||||
}
|
||||
|
||||
static DEVICE_ATTR_RO(name);
|
||||
|
||||
/* Create zone and attributes in sysfs */
|
||||
static void create_power_zone_common_attributes(
|
||||
struct powercap_zone *power_zone)
|
||||
{
|
||||
int count = 0;
|
||||
|
||||
power_zone->zone_dev_attrs[count++] = &dev_attr_name.attr;
|
||||
if (power_zone->ops->get_max_energy_range_uj)
|
||||
power_zone->zone_dev_attrs[count++] =
|
||||
&dev_attr_max_energy_range_uj.attr;
|
||||
if (power_zone->ops->get_energy_uj)
|
||||
power_zone->zone_dev_attrs[count++] =
|
||||
&dev_attr_energy_uj.attr;
|
||||
if (power_zone->ops->get_power_uw)
|
||||
power_zone->zone_dev_attrs[count++] =
|
||||
&dev_attr_power_uw.attr;
|
||||
if (power_zone->ops->get_max_power_range_uw)
|
||||
power_zone->zone_dev_attrs[count++] =
|
||||
&dev_attr_max_power_range_uw.attr;
|
||||
power_zone->zone_dev_attrs[count] = NULL;
|
||||
power_zone->zone_attr_count = count;
|
||||
}
|
||||
|
||||
static void powercap_release(struct device *dev)
|
||||
{
|
||||
bool allocated;
|
||||
|
||||
if (dev->parent) {
|
||||
struct powercap_zone *power_zone = to_powercap_zone(dev);
|
||||
|
||||
/* Store flag as the release() may free memory */
|
||||
allocated = power_zone->allocated;
|
||||
/* Remove id from parent idr struct */
|
||||
idr_remove(power_zone->parent_idr, power_zone->id);
|
||||
/* Destroy idrs allocated for this zone */
|
||||
idr_destroy(&power_zone->idr);
|
||||
kfree(power_zone->name);
|
||||
kfree(power_zone->zone_dev_attrs);
|
||||
kfree(power_zone->constraints);
|
||||
if (power_zone->ops->release)
|
||||
power_zone->ops->release(power_zone);
|
||||
if (allocated)
|
||||
kfree(power_zone);
|
||||
} else {
|
||||
struct powercap_control_type *control_type =
|
||||
to_powercap_control_type(dev);
|
||||
|
||||
/* Store flag as the release() may free memory */
|
||||
allocated = control_type->allocated;
|
||||
idr_destroy(&control_type->idr);
|
||||
mutex_destroy(&control_type->lock);
|
||||
if (control_type->ops && control_type->ops->release)
|
||||
control_type->ops->release(control_type);
|
||||
if (allocated)
|
||||
kfree(control_type);
|
||||
}
|
||||
}
|
||||
|
||||
static ssize_t enabled_show(struct device *dev,
|
||||
struct device_attribute *attr,
|
||||
char *buf)
|
||||
{
|
||||
bool mode = true;
|
||||
|
||||
/* Default is enabled */
|
||||
if (dev->parent) {
|
||||
struct powercap_zone *power_zone = to_powercap_zone(dev);
|
||||
if (power_zone->ops->get_enable)
|
||||
if (power_zone->ops->get_enable(power_zone, &mode))
|
||||
mode = false;
|
||||
} else {
|
||||
struct powercap_control_type *control_type =
|
||||
to_powercap_control_type(dev);
|
||||
if (control_type->ops && control_type->ops->get_enable)
|
||||
if (control_type->ops->get_enable(control_type, &mode))
|
||||
mode = false;
|
||||
}
|
||||
|
||||
return sprintf(buf, "%d\n", mode);
|
||||
}
|
||||
|
||||
static ssize_t enabled_store(struct device *dev,
|
||||
struct device_attribute *attr,
|
||||
const char *buf, size_t len)
|
||||
{
|
||||
bool mode;
|
||||
|
||||
if (strtobool(buf, &mode))
|
||||
return -EINVAL;
|
||||
if (dev->parent) {
|
||||
struct powercap_zone *power_zone = to_powercap_zone(dev);
|
||||
if (power_zone->ops->set_enable)
|
||||
if (!power_zone->ops->set_enable(power_zone, mode))
|
||||
return len;
|
||||
} else {
|
||||
struct powercap_control_type *control_type =
|
||||
to_powercap_control_type(dev);
|
||||
if (control_type->ops && control_type->ops->set_enable)
|
||||
if (!control_type->ops->set_enable(control_type, mode))
|
||||
return len;
|
||||
}
|
||||
|
||||
return -ENOSYS;
|
||||
}
|
||||
|
||||
static DEVICE_ATTR_RW(enabled);
|
||||
|
||||
static struct attribute *powercap_attrs[] = {
|
||||
&dev_attr_enabled.attr,
|
||||
NULL,
|
||||
};
|
||||
ATTRIBUTE_GROUPS(powercap);
|
||||
|
||||
static struct class powercap_class = {
|
||||
.name = "powercap",
|
||||
.dev_release = powercap_release,
|
||||
.dev_groups = powercap_groups,
|
||||
};
|
||||
|
||||
struct powercap_zone *powercap_register_zone(
|
||||
struct powercap_zone *power_zone,
|
||||
struct powercap_control_type *control_type,
|
||||
const char *name,
|
||||
struct powercap_zone *parent,
|
||||
const struct powercap_zone_ops *ops,
|
||||
int nr_constraints,
|
||||
struct powercap_zone_constraint_ops *const_ops)
|
||||
{
|
||||
int result;
|
||||
int nr_attrs;
|
||||
|
||||
if (!name || !control_type || !ops ||
|
||||
nr_constraints > MAX_CONSTRAINTS_PER_ZONE ||
|
||||
(!ops->get_energy_uj && !ops->get_power_uw) ||
|
||||
!control_type_valid(control_type))
|
||||
return ERR_PTR(-EINVAL);
|
||||
|
||||
if (power_zone) {
|
||||
if (!ops->release)
|
||||
return ERR_PTR(-EINVAL);
|
||||
memset(power_zone, 0, sizeof(*power_zone));
|
||||
} else {
|
||||
power_zone = kzalloc(sizeof(*power_zone), GFP_KERNEL);
|
||||
if (!power_zone)
|
||||
return ERR_PTR(-ENOMEM);
|
||||
power_zone->allocated = true;
|
||||
}
|
||||
power_zone->ops = ops;
|
||||
power_zone->control_type_inst = control_type;
|
||||
if (!parent) {
|
||||
power_zone->dev.parent = &control_type->dev;
|
||||
power_zone->parent_idr = &control_type->idr;
|
||||
} else {
|
||||
power_zone->dev.parent = &parent->dev;
|
||||
power_zone->parent_idr = &parent->idr;
|
||||
}
|
||||
power_zone->dev.class = &powercap_class;
|
||||
|
||||
mutex_lock(&control_type->lock);
|
||||
/* Using idr to get the unique id */
|
||||
result = idr_alloc(power_zone->parent_idr, NULL, 0, 0, GFP_KERNEL);
|
||||
if (result < 0)
|
||||
goto err_idr_alloc;
|
||||
|
||||
power_zone->id = result;
|
||||
idr_init(&power_zone->idr);
|
||||
power_zone->name = kstrdup(name, GFP_KERNEL);
|
||||
if (!power_zone->name)
|
||||
goto err_name_alloc;
|
||||
dev_set_name(&power_zone->dev, "%s:%x",
|
||||
dev_name(power_zone->dev.parent),
|
||||
power_zone->id);
|
||||
power_zone->constraints = kzalloc(sizeof(*power_zone->constraints) *
|
||||
nr_constraints, GFP_KERNEL);
|
||||
if (!power_zone->constraints)
|
||||
goto err_const_alloc;
|
||||
|
||||
nr_attrs = nr_constraints * POWERCAP_CONSTRAINTS_ATTRS +
|
||||
POWERCAP_ZONE_MAX_ATTRS + 1;
|
||||
power_zone->zone_dev_attrs = kzalloc(sizeof(void *) *
|
||||
nr_attrs, GFP_KERNEL);
|
||||
if (!power_zone->zone_dev_attrs)
|
||||
goto err_attr_alloc;
|
||||
create_power_zone_common_attributes(power_zone);
|
||||
result = create_constraints(power_zone, nr_constraints, const_ops);
|
||||
if (result)
|
||||
goto err_dev_ret;
|
||||
|
||||
power_zone->zone_dev_attrs[power_zone->zone_attr_count] = NULL;
|
||||
power_zone->dev_zone_attr_group.attrs = power_zone->zone_dev_attrs;
|
||||
power_zone->dev_attr_groups[0] = &power_zone->dev_zone_attr_group;
|
||||
power_zone->dev_attr_groups[1] = NULL;
|
||||
power_zone->dev.groups = power_zone->dev_attr_groups;
|
||||
result = device_register(&power_zone->dev);
|
||||
if (result)
|
||||
goto err_dev_ret;
|
||||
|
||||
control_type->nr_zones++;
|
||||
mutex_unlock(&control_type->lock);
|
||||
|
||||
return power_zone;
|
||||
|
||||
err_dev_ret:
|
||||
kfree(power_zone->zone_dev_attrs);
|
||||
err_attr_alloc:
|
||||
kfree(power_zone->constraints);
|
||||
err_const_alloc:
|
||||
kfree(power_zone->name);
|
||||
err_name_alloc:
|
||||
idr_remove(power_zone->parent_idr, power_zone->id);
|
||||
err_idr_alloc:
|
||||
if (power_zone->allocated)
|
||||
kfree(power_zone);
|
||||
mutex_unlock(&control_type->lock);
|
||||
|
||||
return ERR_PTR(result);
|
||||
}
|
||||
EXPORT_SYMBOL_GPL(powercap_register_zone);
|
||||
|
||||
int powercap_unregister_zone(struct powercap_control_type *control_type,
|
||||
struct powercap_zone *power_zone)
|
||||
{
|
||||
if (!power_zone || !control_type)
|
||||
return -EINVAL;
|
||||
|
||||
mutex_lock(&control_type->lock);
|
||||
control_type->nr_zones--;
|
||||
mutex_unlock(&control_type->lock);
|
||||
|
||||
device_unregister(&power_zone->dev);
|
||||
|
||||
return 0;
|
||||
}
|
||||
EXPORT_SYMBOL_GPL(powercap_unregister_zone);
|
||||
|
||||
struct powercap_control_type *powercap_register_control_type(
|
||||
struct powercap_control_type *control_type,
|
||||
const char *name,
|
||||
const struct powercap_control_type_ops *ops)
|
||||
{
|
||||
int result;
|
||||
|
||||
if (!name)
|
||||
return ERR_PTR(-EINVAL);
|
||||
if (control_type) {
|
||||
if (!ops || !ops->release)
|
||||
return ERR_PTR(-EINVAL);
|
||||
memset(control_type, 0, sizeof(*control_type));
|
||||
} else {
|
||||
control_type = kzalloc(sizeof(*control_type), GFP_KERNEL);
|
||||
if (!control_type)
|
||||
return ERR_PTR(-ENOMEM);
|
||||
control_type->allocated = true;
|
||||
}
|
||||
mutex_init(&control_type->lock);
|
||||
control_type->ops = ops;
|
||||
INIT_LIST_HEAD(&control_type->node);
|
||||
control_type->dev.class = &powercap_class;
|
||||
dev_set_name(&control_type->dev, name);
|
||||
result = device_register(&control_type->dev);
|
||||
if (result) {
|
||||
if (control_type->allocated)
|
||||
kfree(control_type);
|
||||
return ERR_PTR(result);
|
||||
}
|
||||
idr_init(&control_type->idr);
|
||||
|
||||
mutex_lock(&powercap_cntrl_list_lock);
|
||||
list_add_tail(&control_type->node, &powercap_cntrl_list);
|
||||
mutex_unlock(&powercap_cntrl_list_lock);
|
||||
|
||||
return control_type;
|
||||
}
|
||||
EXPORT_SYMBOL_GPL(powercap_register_control_type);
|
||||
|
||||
int powercap_unregister_control_type(struct powercap_control_type *control_type)
|
||||
{
|
||||
struct powercap_control_type *pos = NULL;
|
||||
|
||||
if (control_type->nr_zones) {
|
||||
dev_err(&control_type->dev, "Zones of this type still not freed\n");
|
||||
return -EINVAL;
|
||||
}
|
||||
mutex_lock(&powercap_cntrl_list_lock);
|
||||
list_for_each_entry(pos, &powercap_cntrl_list, node) {
|
||||
if (pos == control_type) {
|
||||
list_del(&control_type->node);
|
||||
mutex_unlock(&powercap_cntrl_list_lock);
|
||||
device_unregister(&control_type->dev);
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
mutex_unlock(&powercap_cntrl_list_lock);
|
||||
|
||||
return -ENODEV;
|
||||
}
|
||||
EXPORT_SYMBOL_GPL(powercap_unregister_control_type);
|
||||
|
||||
static int __init powercap_init(void)
|
||||
{
|
||||
int result = 0;
|
||||
|
||||
result = seed_constraint_attributes();
|
||||
if (result)
|
||||
return result;
|
||||
|
||||
result = class_register(&powercap_class);
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
device_initcall(powercap_init);
|
||||
|
||||
MODULE_DESCRIPTION("PowerCap sysfs Driver");
|
||||
MODULE_AUTHOR("Srinivas Pandruvada <srinivas.pandruvada@linux.intel.com>");
|
||||
MODULE_LICENSE("GPL v2");
|
|
@ -4,8 +4,11 @@
|
|||
|
||||
#ifdef __KERNEL__
|
||||
#define BIT(nr) (1UL << (nr))
|
||||
#define BIT_ULL(nr) (1ULL << (nr))
|
||||
#define BIT_MASK(nr) (1UL << ((nr) % BITS_PER_LONG))
|
||||
#define BIT_WORD(nr) ((nr) / BITS_PER_LONG)
|
||||
#define BIT_ULL_MASK(nr) (1ULL << ((nr) % BITS_PER_LONG_LONG))
|
||||
#define BIT_ULL_WORD(nr) ((nr) / BITS_PER_LONG_LONG)
|
||||
#define BITS_PER_BYTE 8
|
||||
#define BITS_TO_LONGS(nr) DIV_ROUND_UP(nr, BITS_PER_BYTE * sizeof(long))
|
||||
#endif
|
||||
|
|
|
@ -0,0 +1,325 @@
|
|||
/*
|
||||
* powercap.h: Data types and headers for sysfs power capping interface
|
||||
* Copyright (c) 2013, Intel Corporation.
|
||||
*
|
||||
* This program is free software; you can redistribute it and/or modify it
|
||||
* under the terms and conditions of the GNU General Public License,
|
||||
* version 2, as published by the Free Software Foundation.
|
||||
*
|
||||
* This program is distributed in the hope it will be useful, but WITHOUT
|
||||
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
|
||||
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
|
||||
* more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License along with
|
||||
* this program; if not, write to the Free Software Foundation, Inc.
|
||||
*
|
||||
*/
|
||||
|
||||
#ifndef __POWERCAP_H__
|
||||
#define __POWERCAP_H__
|
||||
|
||||
#include <linux/device.h>
|
||||
#include <linux/idr.h>
|
||||
|
||||
/*
|
||||
* A power cap class device can contain multiple powercap control_types.
|
||||
* Each control_type can have multiple power zones, which can be independently
|
||||
* controlled. Each power zone can have one or more constraints.
|
||||
*/
|
||||
|
||||
struct powercap_control_type;
|
||||
struct powercap_zone;
|
||||
struct powercap_zone_constraint;
|
||||
|
||||
/**
|
||||
* struct powercap_control_type_ops - Define control type callbacks
|
||||
* @set_enable: Enable/Disable whole control type.
|
||||
* Default is enabled. But this callback allows all zones
|
||||
* to be in disable state and remove any applied power
|
||||
* limits. If disabled power zone can only be monitored
|
||||
* not controlled.
|
||||
* @get_enable: get Enable/Disable status.
|
||||
* @release: Callback to inform that last reference to this
|
||||
* control type is closed. So it is safe to free data
|
||||
* structure associated with this control type.
|
||||
* This callback is mandatory if the client own memory
|
||||
* for the control type.
|
||||
*
|
||||
* This structure defines control type callbacks to be implemented by client
|
||||
* drivers
|
||||
*/
|
||||
struct powercap_control_type_ops {
|
||||
int (*set_enable) (struct powercap_control_type *, bool mode);
|
||||
int (*get_enable) (struct powercap_control_type *, bool *mode);
|
||||
int (*release) (struct powercap_control_type *);
|
||||
};
|
||||
|
||||
/**
|
||||
* struct powercap_control_type- Defines a powercap control_type
|
||||
* @name: name of control_type
|
||||
* @dev: device for this control_type
|
||||
* @idr: idr to have unique id for its child
|
||||
* @root_node: Root holding power zones for this control_type
|
||||
* @ops: Pointer to callback struct
|
||||
* @node_lock: mutex for control type
|
||||
* @allocated: This is possible that client owns the memory
|
||||
* used by this structure. In this case
|
||||
* this flag is set to false by framework to
|
||||
* prevent deallocation during release process.
|
||||
* Otherwise this flag is set to true.
|
||||
* @ctrl_inst: link to the control_type list
|
||||
*
|
||||
* Defines powercap control_type. This acts as a container for power
|
||||
* zones, which use same method to control power. E.g. RAPL, RAPL-PCI etc.
|
||||
* All fields are private and should not be used by client drivers.
|
||||
*/
|
||||
struct powercap_control_type {
|
||||
struct device dev;
|
||||
struct idr idr;
|
||||
int nr_zones;
|
||||
const struct powercap_control_type_ops *ops;
|
||||
struct mutex lock;
|
||||
bool allocated;
|
||||
struct list_head node;
|
||||
};
|
||||
|
||||
/**
|
||||
* struct powercap_zone_ops - Define power zone callbacks
|
||||
* @get_max_energy_range_uj: Get maximum range of energy counter in
|
||||
* micro-joules.
|
||||
* @get_energy_uj: Get current energy counter in micro-joules.
|
||||
* @reset_energy_uj: Reset micro-joules energy counter.
|
||||
* @get_max_power_range_uw: Get maximum range of power counter in
|
||||
* micro-watts.
|
||||
* @get_power_uw: Get current power counter in micro-watts.
|
||||
* @set_enable: Enable/Disable power zone controls.
|
||||
* Default is enabled.
|
||||
* @get_enable: get Enable/Disable status.
|
||||
* @release: Callback to inform that last reference to this
|
||||
* control type is closed. So it is safe to free
|
||||
* data structure associated with this
|
||||
* control type. Mandatory, if client driver owns
|
||||
* the power_zone memory.
|
||||
*
|
||||
* This structure defines zone callbacks to be implemented by client drivers.
|
||||
* Client drives can define both energy and power related callbacks. But at
|
||||
* the least one type (either power or energy) is mandatory. Client drivers
|
||||
* should handle mutual exclusion, if required in callbacks.
|
||||
*/
|
||||
struct powercap_zone_ops {
|
||||
int (*get_max_energy_range_uj) (struct powercap_zone *, u64 *);
|
||||
int (*get_energy_uj) (struct powercap_zone *, u64 *);
|
||||
int (*reset_energy_uj) (struct powercap_zone *);
|
||||
int (*get_max_power_range_uw) (struct powercap_zone *, u64 *);
|
||||
int (*get_power_uw) (struct powercap_zone *, u64 *);
|
||||
int (*set_enable) (struct powercap_zone *, bool mode);
|
||||
int (*get_enable) (struct powercap_zone *, bool *mode);
|
||||
int (*release) (struct powercap_zone *);
|
||||
};
|
||||
|
||||
#define POWERCAP_ZONE_MAX_ATTRS 6
|
||||
#define POWERCAP_CONSTRAINTS_ATTRS 8
|
||||
#define MAX_CONSTRAINTS_PER_ZONE 10
|
||||
/**
|
||||
* struct powercap_zone- Defines instance of a power cap zone
|
||||
* @id: Unique id
|
||||
* @name: Power zone name.
|
||||
* @control_type_inst: Control type instance for this zone.
|
||||
* @ops: Pointer to the zone operation structure.
|
||||
* @dev: Instance of a device.
|
||||
* @const_id_cnt: Number of constraint defined.
|
||||
* @idr: Instance to an idr entry for children zones.
|
||||
* @parent_idr: To remove reference from the parent idr.
|
||||
* @private_data: Private data pointer if any for this zone.
|
||||
* @zone_dev_attrs: Attributes associated with this device.
|
||||
* @zone_attr_count: Attribute count.
|
||||
* @dev_zone_attr_group: Attribute group for attributes.
|
||||
* @dev_attr_groups: Attribute group store to register with device.
|
||||
* @allocated: This is possible that client owns the memory
|
||||
* used by this structure. In this case
|
||||
* this flag is set to false by framework to
|
||||
* prevent deallocation during release process.
|
||||
* Otherwise this flag is set to true.
|
||||
* @constraint_ptr: List of constraints for this zone.
|
||||
*
|
||||
* This defines a power zone instance. The fields of this structure are
|
||||
* private, and should not be used by client drivers.
|
||||
*/
|
||||
struct powercap_zone {
|
||||
int id;
|
||||
char *name;
|
||||
void *control_type_inst;
|
||||
const struct powercap_zone_ops *ops;
|
||||
struct device dev;
|
||||
int const_id_cnt;
|
||||
struct idr idr;
|
||||
struct idr *parent_idr;
|
||||
void *private_data;
|
||||
struct attribute **zone_dev_attrs;
|
||||
int zone_attr_count;
|
||||
struct attribute_group dev_zone_attr_group;
|
||||
const struct attribute_group *dev_attr_groups[2]; /* 1 group + NULL */
|
||||
bool allocated;
|
||||
struct powercap_zone_constraint *constraints;
|
||||
};
|
||||
|
||||
/**
|
||||
* struct powercap_zone_constraint_ops - Define constraint callbacks
|
||||
* @set_power_limit_uw: Set power limit in micro-watts.
|
||||
* @get_power_limit_uw: Get power limit in micro-watts.
|
||||
* @set_time_window_us: Set time window in micro-seconds.
|
||||
* @get_time_window_us: Get time window in micro-seconds.
|
||||
* @get_max_power_uw: Get max power allowed in micro-watts.
|
||||
* @get_min_power_uw: Get min power allowed in micro-watts.
|
||||
* @get_max_time_window_us: Get max time window allowed in micro-seconds.
|
||||
* @get_min_time_window_us: Get min time window allowed in micro-seconds.
|
||||
* @get_name: Get the name of constraint
|
||||
*
|
||||
* This structure is used to define the constraint callbacks for the client
|
||||
* drivers. The following callbacks are mandatory and can't be NULL:
|
||||
* set_power_limit_uw
|
||||
* get_power_limit_uw
|
||||
* set_time_window_us
|
||||
* get_time_window_us
|
||||
* get_name
|
||||
* Client drivers should handle mutual exclusion, if required in callbacks.
|
||||
*/
|
||||
struct powercap_zone_constraint_ops {
|
||||
int (*set_power_limit_uw) (struct powercap_zone *, int, u64);
|
||||
int (*get_power_limit_uw) (struct powercap_zone *, int, u64 *);
|
||||
int (*set_time_window_us) (struct powercap_zone *, int, u64);
|
||||
int (*get_time_window_us) (struct powercap_zone *, int, u64 *);
|
||||
int (*get_max_power_uw) (struct powercap_zone *, int, u64 *);
|
||||
int (*get_min_power_uw) (struct powercap_zone *, int, u64 *);
|
||||
int (*get_max_time_window_us) (struct powercap_zone *, int, u64 *);
|
||||
int (*get_min_time_window_us) (struct powercap_zone *, int, u64 *);
|
||||
const char *(*get_name) (struct powercap_zone *, int);
|
||||
};
|
||||
|
||||
/**
|
||||
* struct powercap_zone_constraint- Defines instance of a constraint
|
||||
* @id: Instance Id of this constraint.
|
||||
* @power_zone: Pointer to the power zone for this constraint.
|
||||
* @ops: Pointer to the constraint callbacks.
|
||||
*
|
||||
* This defines a constraint instance.
|
||||
*/
|
||||
struct powercap_zone_constraint {
|
||||
int id;
|
||||
struct powercap_zone *power_zone;
|
||||
struct powercap_zone_constraint_ops *ops;
|
||||
};
|
||||
|
||||
|
||||
/* For clients to get their device pointer, may be used for dev_dbgs */
|
||||
#define POWERCAP_GET_DEV(power_zone) (&power_zone->dev)
|
||||
|
||||
/**
|
||||
* powercap_set_zone_data() - Set private data for a zone
|
||||
* @power_zone: A pointer to the valid zone instance.
|
||||
* @pdata: A pointer to the user private data.
|
||||
*
|
||||
* Allows client drivers to associate some private data to zone instance.
|
||||
*/
|
||||
static inline void powercap_set_zone_data(struct powercap_zone *power_zone,
|
||||
void *pdata)
|
||||
{
|
||||
if (power_zone)
|
||||
power_zone->private_data = pdata;
|
||||
}
|
||||
|
||||
/**
|
||||
* powercap_get_zone_data() - Get private data for a zone
|
||||
* @power_zone: A pointer to the valid zone instance.
|
||||
*
|
||||
* Allows client drivers to get private data associate with a zone,
|
||||
* using call to powercap_set_zone_data.
|
||||
*/
|
||||
static inline void *powercap_get_zone_data(struct powercap_zone *power_zone)
|
||||
{
|
||||
if (power_zone)
|
||||
return power_zone->private_data;
|
||||
return NULL;
|
||||
}
|
||||
|
||||
/**
|
||||
* powercap_register_control_type() - Register a control_type with framework
|
||||
* @control_type: Pointer to client allocated memory for the control type
|
||||
* structure storage. If this is NULL, powercap framework
|
||||
* will allocate memory and own it.
|
||||
* Advantage of this parameter is that client can embed
|
||||
* this data in its data structures and allocate in a
|
||||
* single call, preventing multiple allocations.
|
||||
* @control_type_name: The Name of this control_type, which will be shown
|
||||
* in the sysfs Interface.
|
||||
* @ops: Callbacks for control type. This parameter is optional.
|
||||
*
|
||||
* Used to create a control_type with the power capping class. Here control_type
|
||||
* can represent a type of technology, which can control a range of power zones.
|
||||
* For example a control_type can be RAPL (Running Average Power Limit)
|
||||
* Intel® 64 and IA-32 Processor Architectures. The name can be any string
|
||||
* which must be unique, otherwise this function returns NULL.
|
||||
* A pointer to the control_type instance is returned on success.
|
||||
*/
|
||||
struct powercap_control_type *powercap_register_control_type(
|
||||
struct powercap_control_type *control_type,
|
||||
const char *name,
|
||||
const struct powercap_control_type_ops *ops);
|
||||
|
||||
/**
|
||||
* powercap_unregister_control_type() - Unregister a control_type from framework
|
||||
* @instance: A pointer to the valid control_type instance.
|
||||
*
|
||||
* Used to unregister a control_type with the power capping class.
|
||||
* All power zones registered under this control type have to be unregistered
|
||||
* before calling this function, or it will fail with an error code.
|
||||
*/
|
||||
int powercap_unregister_control_type(struct powercap_control_type *instance);
|
||||
|
||||
/* Zone register/unregister API */
|
||||
|
||||
/**
|
||||
* powercap_register_zone() - Register a power zone
|
||||
* @power_zone: Pointer to client allocated memory for the power zone structure
|
||||
* storage. If this is NULL, powercap framework will allocate
|
||||
* memory and own it. Advantage of this parameter is that client
|
||||
* can embed this data in its data structures and allocate in a
|
||||
* single call, preventing multiple allocations.
|
||||
* @control_type: A control_type instance under which this zone operates.
|
||||
* @name: A name for this zone.
|
||||
* @parent: A pointer to the parent power zone instance if any or NULL
|
||||
* @ops: Pointer to zone operation callback structure.
|
||||
* @no_constraints: Number of constraints for this zone
|
||||
* @const_ops: Pointer to constraint callback structure
|
||||
*
|
||||
* Register a power zone under a given control type. A power zone must register
|
||||
* a pointer to a structure representing zone callbacks.
|
||||
* A power zone can be located under a parent power zone, in which case @parent
|
||||
* should point to it. Otherwise, if @parent is NULL, the new power zone will
|
||||
* be located directly under the given control type
|
||||
* For each power zone there may be a number of constraints that appear in the
|
||||
* sysfs under that zone as attributes with unique numeric IDs.
|
||||
* Returns pointer to the power_zone on success.
|
||||
*/
|
||||
struct powercap_zone *powercap_register_zone(
|
||||
struct powercap_zone *power_zone,
|
||||
struct powercap_control_type *control_type,
|
||||
const char *name,
|
||||
struct powercap_zone *parent,
|
||||
const struct powercap_zone_ops *ops,
|
||||
int nr_constraints,
|
||||
struct powercap_zone_constraint_ops *const_ops);
|
||||
|
||||
/**
|
||||
* powercap_unregister_zone() - Unregister a zone device
|
||||
* @control_type: A pointer to the valid instance of a control_type.
|
||||
* @power_zone: A pointer to the valid zone instance for a control_type
|
||||
*
|
||||
* Used to unregister a zone device for a control_type. Caller should
|
||||
* make sure that children for this zone are unregistered first.
|
||||
*/
|
||||
int powercap_unregister_zone(struct powercap_control_type *control_type,
|
||||
struct powercap_zone *power_zone);
|
||||
|
||||
#endif
|
Loading…
Reference in New Issue