1 /* SPDX-License-Identifier: GPL-2.0-only */
3 * powercap.h: Data types and headers for sysfs power capping interface
4 * Copyright (c) 2013, Intel Corporation.
10 #include <linux/device.h>
11 #include <linux/idr.h>
14 * A power cap class device can contain multiple powercap control_types.
15 * Each control_type can have multiple power zones, which can be independently
16 * controlled. Each power zone can have one or more constraints.
19 struct powercap_control_type
;
21 struct powercap_zone_constraint
;
24 * struct powercap_control_type_ops - Define control type callbacks
25 * @set_enable: Enable/Disable whole control type.
26 * Default is enabled. But this callback allows all zones
27 * to be in disable state and remove any applied power
28 * limits. If disabled power zone can only be monitored
30 * @get_enable: get Enable/Disable status.
31 * @release: Callback to inform that last reference to this
32 * control type is closed. So it is safe to free data
33 * structure associated with this control type.
34 * This callback is mandatory if the client own memory
35 * for the control type.
37 * This structure defines control type callbacks to be implemented by client
40 struct powercap_control_type_ops
{
41 int (*set_enable
) (struct powercap_control_type
*, bool mode
);
42 int (*get_enable
) (struct powercap_control_type
*, bool *mode
);
43 int (*release
) (struct powercap_control_type
*);
47 * struct powercap_control_type - Defines a powercap control_type
48 * @dev: device for this control_type
49 * @idr: idr to have unique id for its child
50 * @nr_zones: counter for number of zones of this type
51 * @ops: Pointer to callback struct
52 * @lock: mutex for control type
53 * @allocated: This is possible that client owns the memory
54 * used by this structure. In this case
55 * this flag is set to false by framework to
56 * prevent deallocation during release process.
57 * Otherwise this flag is set to true.
58 * @node: linked-list node
60 * Defines powercap control_type. This acts as a container for power
61 * zones, which use same method to control power. E.g. RAPL, RAPL-PCI etc.
62 * All fields are private and should not be used by client drivers.
64 struct powercap_control_type
{
68 const struct powercap_control_type_ops
*ops
;
71 struct list_head node
;
75 * struct powercap_zone_ops - Define power zone callbacks
76 * @get_max_energy_range_uj: Get maximum range of energy counter in
78 * @get_energy_uj: Get current energy counter in micro-joules.
79 * @reset_energy_uj: Reset micro-joules energy counter.
80 * @get_max_power_range_uw: Get maximum range of power counter in
82 * @get_power_uw: Get current power counter in micro-watts.
83 * @set_enable: Enable/Disable power zone controls.
85 * @get_enable: get Enable/Disable status.
86 * @release: Callback to inform that last reference to this
87 * control type is closed. So it is safe to free
88 * data structure associated with this
89 * control type. Mandatory, if client driver owns
90 * the power_zone memory.
92 * This structure defines zone callbacks to be implemented by client drivers.
93 * Client drives can define both energy and power related callbacks. But at
94 * the least one type (either power or energy) is mandatory. Client drivers
95 * should handle mutual exclusion, if required in callbacks.
97 struct powercap_zone_ops
{
98 int (*get_max_energy_range_uj
) (struct powercap_zone
*, u64
*);
99 int (*get_energy_uj
) (struct powercap_zone
*, u64
*);
100 int (*reset_energy_uj
) (struct powercap_zone
*);
101 int (*get_max_power_range_uw
) (struct powercap_zone
*, u64
*);
102 int (*get_power_uw
) (struct powercap_zone
*, u64
*);
103 int (*set_enable
) (struct powercap_zone
*, bool mode
);
104 int (*get_enable
) (struct powercap_zone
*, bool *mode
);
105 int (*release
) (struct powercap_zone
*);
108 #define POWERCAP_ZONE_MAX_ATTRS 6
109 #define POWERCAP_CONSTRAINTS_ATTRS 8
110 #define MAX_CONSTRAINTS_PER_ZONE 10
112 * struct powercap_zone- Defines instance of a power cap zone
114 * @name: Power zone name.
115 * @control_type_inst: Control type instance for this zone.
116 * @ops: Pointer to the zone operation structure.
117 * @dev: Instance of a device.
118 * @const_id_cnt: Number of constraint defined.
119 * @idr: Instance to an idr entry for children zones.
120 * @parent_idr: To remove reference from the parent idr.
121 * @private_data: Private data pointer if any for this zone.
122 * @zone_dev_attrs: Attributes associated with this device.
123 * @zone_attr_count: Attribute count.
124 * @dev_zone_attr_group: Attribute group for attributes.
125 * @dev_attr_groups: Attribute group store to register with device.
126 * @allocated: This is possible that client owns the memory
127 * used by this structure. In this case
128 * this flag is set to false by framework to
129 * prevent deallocation during release process.
130 * Otherwise this flag is set to true.
131 * @constraints: List of constraints for this zone.
133 * This defines a power zone instance. The fields of this structure are
134 * private, and should not be used by client drivers.
136 struct powercap_zone
{
139 void *control_type_inst
;
140 const struct powercap_zone_ops
*ops
;
144 struct idr
*parent_idr
;
146 struct attribute
**zone_dev_attrs
;
148 struct attribute_group dev_zone_attr_group
;
149 const struct attribute_group
*dev_attr_groups
[2]; /* 1 group + NULL */
151 struct powercap_zone_constraint
*constraints
;
155 * struct powercap_zone_constraint_ops - Define constraint callbacks
156 * @set_power_limit_uw: Set power limit in micro-watts.
157 * @get_power_limit_uw: Get power limit in micro-watts.
158 * @set_time_window_us: Set time window in micro-seconds.
159 * @get_time_window_us: Get time window in micro-seconds.
160 * @get_max_power_uw: Get max power allowed in micro-watts.
161 * @get_min_power_uw: Get min power allowed in micro-watts.
162 * @get_max_time_window_us: Get max time window allowed in micro-seconds.
163 * @get_min_time_window_us: Get min time window allowed in micro-seconds.
164 * @get_name: Get the name of constraint
166 * This structure is used to define the constraint callbacks for the client
167 * drivers. The following callbacks are mandatory and can't be NULL:
173 * Client drivers should handle mutual exclusion, if required in callbacks.
175 struct powercap_zone_constraint_ops
{
176 int (*set_power_limit_uw
) (struct powercap_zone
*, int, u64
);
177 int (*get_power_limit_uw
) (struct powercap_zone
*, int, u64
*);
178 int (*set_time_window_us
) (struct powercap_zone
*, int, u64
);
179 int (*get_time_window_us
) (struct powercap_zone
*, int, u64
*);
180 int (*get_max_power_uw
) (struct powercap_zone
*, int, u64
*);
181 int (*get_min_power_uw
) (struct powercap_zone
*, int, u64
*);
182 int (*get_max_time_window_us
) (struct powercap_zone
*, int, u64
*);
183 int (*get_min_time_window_us
) (struct powercap_zone
*, int, u64
*);
184 const char *(*get_name
) (struct powercap_zone
*, int);
188 * struct powercap_zone_constraint- Defines instance of a constraint
189 * @id: Instance Id of this constraint.
190 * @power_zone: Pointer to the power zone for this constraint.
191 * @ops: Pointer to the constraint callbacks.
193 * This defines a constraint instance.
195 struct powercap_zone_constraint
{
197 struct powercap_zone
*power_zone
;
198 const struct powercap_zone_constraint_ops
*ops
;
202 /* For clients to get their device pointer, may be used for dev_dbgs */
203 #define POWERCAP_GET_DEV(power_zone) (&power_zone->dev)
206 * powercap_set_zone_data() - Set private data for a zone
207 * @power_zone: A pointer to the valid zone instance.
208 * @pdata: A pointer to the user private data.
210 * Allows client drivers to associate some private data to zone instance.
212 static inline void powercap_set_zone_data(struct powercap_zone
*power_zone
,
216 power_zone
->private_data
= pdata
;
220 * powercap_get_zone_data() - Get private data for a zone
221 * @power_zone: A pointer to the valid zone instance.
223 * Allows client drivers to get private data associate with a zone,
224 * using call to powercap_set_zone_data.
226 static inline void *powercap_get_zone_data(struct powercap_zone
*power_zone
)
229 return power_zone
->private_data
;
234 * powercap_register_control_type() - Register a control_type with framework
235 * @control_type: Pointer to client allocated memory for the control type
236 * structure storage. If this is NULL, powercap framework
237 * will allocate memory and own it.
238 * Advantage of this parameter is that client can embed
239 * this data in its data structures and allocate in a
240 * single call, preventing multiple allocations.
241 * @control_type_name: The Name of this control_type, which will be shown
242 * in the sysfs Interface.
243 * @ops: Callbacks for control type. This parameter is optional.
245 * Used to create a control_type with the power capping class. Here control_type
246 * can represent a type of technology, which can control a range of power zones.
247 * For example a control_type can be RAPL (Running Average Power Limit)
248 * IntelĀ® 64 and IA-32 Processor Architectures. The name can be any string
249 * which must be unique, otherwise this function returns NULL.
250 * A pointer to the control_type instance is returned on success.
252 struct powercap_control_type
*powercap_register_control_type(
253 struct powercap_control_type
*control_type
,
255 const struct powercap_control_type_ops
*ops
);
258 * powercap_unregister_control_type() - Unregister a control_type from framework
259 * @instance: A pointer to the valid control_type instance.
261 * Used to unregister a control_type with the power capping class.
262 * All power zones registered under this control type have to be unregistered
263 * before calling this function, or it will fail with an error code.
265 int powercap_unregister_control_type(struct powercap_control_type
*instance
);
267 /* Zone register/unregister API */
270 * powercap_register_zone() - Register a power zone
271 * @power_zone: Pointer to client allocated memory for the power zone structure
272 * storage. If this is NULL, powercap framework will allocate
273 * memory and own it. Advantage of this parameter is that client
274 * can embed this data in its data structures and allocate in a
275 * single call, preventing multiple allocations.
276 * @control_type: A control_type instance under which this zone operates.
277 * @name: A name for this zone.
278 * @parent: A pointer to the parent power zone instance if any or NULL
279 * @ops: Pointer to zone operation callback structure.
280 * @no_constraints: Number of constraints for this zone
281 * @const_ops: Pointer to constraint callback structure
283 * Register a power zone under a given control type. A power zone must register
284 * a pointer to a structure representing zone callbacks.
285 * A power zone can be located under a parent power zone, in which case @parent
286 * should point to it. Otherwise, if @parent is NULL, the new power zone will
287 * be located directly under the given control type
288 * For each power zone there may be a number of constraints that appear in the
289 * sysfs under that zone as attributes with unique numeric IDs.
290 * Returns pointer to the power_zone on success.
292 struct powercap_zone
*powercap_register_zone(
293 struct powercap_zone
*power_zone
,
294 struct powercap_control_type
*control_type
,
296 struct powercap_zone
*parent
,
297 const struct powercap_zone_ops
*ops
,
299 const struct powercap_zone_constraint_ops
*const_ops
);
302 * powercap_unregister_zone() - Unregister a zone device
303 * @control_type: A pointer to the valid instance of a control_type.
304 * @power_zone: A pointer to the valid zone instance for a control_type
306 * Used to unregister a zone device for a control_type. Caller should
307 * make sure that children for this zone are unregistered first.
309 int powercap_unregister_zone(struct powercap_control_type
*control_type
,
310 struct powercap_zone
*power_zone
);