powercap.h 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326
  1. /*
  2. * powercap.h: Data types and headers for sysfs power capping interface
  3. * Copyright (c) 2013, Intel Corporation.
  4. *
  5. * This program is free software; you can redistribute it and/or modify it
  6. * under the terms and conditions of the GNU General Public License,
  7. * version 2, as published by the Free Software Foundation.
  8. *
  9. * This program is distributed in the hope it will be useful, but WITHOUT
  10. * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  11. * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
  12. * more details.
  13. *
  14. * You should have received a copy of the GNU General Public License along with
  15. * this program; if not, write to the Free Software Foundation, Inc.
  16. *
  17. */
  18. #ifndef __POWERCAP_H__
  19. #define __POWERCAP_H__
  20. #include <linux/device.h>
  21. #include <linux/idr.h>
  22. /*
  23. * A power cap class device can contain multiple powercap control_types.
  24. * Each control_type can have multiple power zones, which can be independently
  25. * controlled. Each power zone can have one or more constraints.
  26. */
  27. struct powercap_control_type;
  28. struct powercap_zone;
  29. struct powercap_zone_constraint;
  30. /**
  31. * struct powercap_control_type_ops - Define control type callbacks
  32. * @set_enable: Enable/Disable whole control type.
  33. * Default is enabled. But this callback allows all zones
  34. * to be in disable state and remove any applied power
  35. * limits. If disabled power zone can only be monitored
  36. * not controlled.
  37. * @get_enable: get Enable/Disable status.
  38. * @release: Callback to inform that last reference to this
  39. * control type is closed. So it is safe to free data
  40. * structure associated with this control type.
  41. * This callback is mandatory if the client own memory
  42. * for the control type.
  43. *
  44. * This structure defines control type callbacks to be implemented by client
  45. * drivers
  46. */
  47. struct powercap_control_type_ops {
  48. int (*set_enable) (struct powercap_control_type *, bool mode);
  49. int (*get_enable) (struct powercap_control_type *, bool *mode);
  50. int (*release) (struct powercap_control_type *);
  51. };
  52. /**
  53. * struct powercap_control_type- Defines a powercap control_type
  54. * @name: name of control_type
  55. * @dev: device for this control_type
  56. * @idr: idr to have unique id for its child
  57. * @root_node: Root holding power zones for this control_type
  58. * @ops: Pointer to callback struct
  59. * @node_lock: mutex for control type
  60. * @allocated: This is possible that client owns the memory
  61. * used by this structure. In this case
  62. * this flag is set to false by framework to
  63. * prevent deallocation during release process.
  64. * Otherwise this flag is set to true.
  65. * @ctrl_inst: link to the control_type list
  66. *
  67. * Defines powercap control_type. This acts as a container for power
  68. * zones, which use same method to control power. E.g. RAPL, RAPL-PCI etc.
  69. * All fields are private and should not be used by client drivers.
  70. */
  71. struct powercap_control_type {
  72. struct device dev;
  73. struct idr idr;
  74. int nr_zones;
  75. const struct powercap_control_type_ops *ops;
  76. struct mutex lock;
  77. bool allocated;
  78. struct list_head node;
  79. };
  80. /**
  81. * struct powercap_zone_ops - Define power zone callbacks
  82. * @get_max_energy_range_uj: Get maximum range of energy counter in
  83. * micro-joules.
  84. * @get_energy_uj: Get current energy counter in micro-joules.
  85. * @reset_energy_uj: Reset micro-joules energy counter.
  86. * @get_max_power_range_uw: Get maximum range of power counter in
  87. * micro-watts.
  88. * @get_power_uw: Get current power counter in micro-watts.
  89. * @set_enable: Enable/Disable power zone controls.
  90. * Default is enabled.
  91. * @get_enable: get Enable/Disable status.
  92. * @release: Callback to inform that last reference to this
  93. * control type is closed. So it is safe to free
  94. * data structure associated with this
  95. * control type. Mandatory, if client driver owns
  96. * the power_zone memory.
  97. *
  98. * This structure defines zone callbacks to be implemented by client drivers.
  99. * Client drives can define both energy and power related callbacks. But at
  100. * the least one type (either power or energy) is mandatory. Client drivers
  101. * should handle mutual exclusion, if required in callbacks.
  102. */
  103. struct powercap_zone_ops {
  104. int (*get_max_energy_range_uj) (struct powercap_zone *, u64 *);
  105. int (*get_energy_uj) (struct powercap_zone *, u64 *);
  106. int (*reset_energy_uj) (struct powercap_zone *);
  107. int (*get_max_power_range_uw) (struct powercap_zone *, u64 *);
  108. int (*get_power_uw) (struct powercap_zone *, u64 *);
  109. int (*set_enable) (struct powercap_zone *, bool mode);
  110. int (*get_enable) (struct powercap_zone *, bool *mode);
  111. int (*release) (struct powercap_zone *);
  112. };
  113. #define POWERCAP_ZONE_MAX_ATTRS 6
  114. #define POWERCAP_CONSTRAINTS_ATTRS 8
  115. #define MAX_CONSTRAINTS_PER_ZONE 10
  116. /**
  117. * struct powercap_zone- Defines instance of a power cap zone
  118. * @id: Unique id
  119. * @name: Power zone name.
  120. * @control_type_inst: Control type instance for this zone.
  121. * @ops: Pointer to the zone operation structure.
  122. * @dev: Instance of a device.
  123. * @const_id_cnt: Number of constraint defined.
  124. * @idr: Instance to an idr entry for children zones.
  125. * @parent_idr: To remove reference from the parent idr.
  126. * @private_data: Private data pointer if any for this zone.
  127. * @zone_dev_attrs: Attributes associated with this device.
  128. * @zone_attr_count: Attribute count.
  129. * @dev_zone_attr_group: Attribute group for attributes.
  130. * @dev_attr_groups: Attribute group store to register with device.
  131. * @allocated: This is possible that client owns the memory
  132. * used by this structure. In this case
  133. * this flag is set to false by framework to
  134. * prevent deallocation during release process.
  135. * Otherwise this flag is set to true.
  136. * @constraint_ptr: List of constraints for this zone.
  137. *
  138. * This defines a power zone instance. The fields of this structure are
  139. * private, and should not be used by client drivers.
  140. */
  141. struct powercap_zone {
  142. int id;
  143. char *name;
  144. void *control_type_inst;
  145. const struct powercap_zone_ops *ops;
  146. struct device dev;
  147. int const_id_cnt;
  148. struct idr idr;
  149. struct idr *parent_idr;
  150. void *private_data;
  151. struct attribute **zone_dev_attrs;
  152. int zone_attr_count;
  153. struct attribute_group dev_zone_attr_group;
  154. const struct attribute_group *dev_attr_groups[2]; /* 1 group + NULL */
  155. bool allocated;
  156. struct powercap_zone_constraint *constraints;
  157. };
  158. /**
  159. * struct powercap_zone_constraint_ops - Define constraint callbacks
  160. * @set_power_limit_uw: Set power limit in micro-watts.
  161. * @get_power_limit_uw: Get power limit in micro-watts.
  162. * @set_time_window_us: Set time window in micro-seconds.
  163. * @get_time_window_us: Get time window in micro-seconds.
  164. * @get_max_power_uw: Get max power allowed in micro-watts.
  165. * @get_min_power_uw: Get min power allowed in micro-watts.
  166. * @get_max_time_window_us: Get max time window allowed in micro-seconds.
  167. * @get_min_time_window_us: Get min time window allowed in micro-seconds.
  168. * @get_name: Get the name of constraint
  169. *
  170. * This structure is used to define the constraint callbacks for the client
  171. * drivers. The following callbacks are mandatory and can't be NULL:
  172. * set_power_limit_uw
  173. * get_power_limit_uw
  174. * set_time_window_us
  175. * get_time_window_us
  176. * get_name
  177. * Client drivers should handle mutual exclusion, if required in callbacks.
  178. */
  179. struct powercap_zone_constraint_ops {
  180. int (*set_power_limit_uw) (struct powercap_zone *, int, u64);
  181. int (*get_power_limit_uw) (struct powercap_zone *, int, u64 *);
  182. int (*set_time_window_us) (struct powercap_zone *, int, u64);
  183. int (*get_time_window_us) (struct powercap_zone *, int, u64 *);
  184. int (*get_max_power_uw) (struct powercap_zone *, int, u64 *);
  185. int (*get_min_power_uw) (struct powercap_zone *, int, u64 *);
  186. int (*get_max_time_window_us) (struct powercap_zone *, int, u64 *);
  187. int (*get_min_time_window_us) (struct powercap_zone *, int, u64 *);
  188. const char *(*get_name) (struct powercap_zone *, int);
  189. };
  190. /**
  191. * struct powercap_zone_constraint- Defines instance of a constraint
  192. * @id: Instance Id of this constraint.
  193. * @power_zone: Pointer to the power zone for this constraint.
  194. * @ops: Pointer to the constraint callbacks.
  195. *
  196. * This defines a constraint instance.
  197. */
  198. struct powercap_zone_constraint {
  199. int id;
  200. struct powercap_zone *power_zone;
  201. const struct powercap_zone_constraint_ops *ops;
  202. };
  203. /* For clients to get their device pointer, may be used for dev_dbgs */
  204. #define POWERCAP_GET_DEV(power_zone) (&power_zone->dev)
  205. /**
  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.
  209. *
  210. * Allows client drivers to associate some private data to zone instance.
  211. */
  212. static inline void powercap_set_zone_data(struct powercap_zone *power_zone,
  213. void *pdata)
  214. {
  215. if (power_zone)
  216. power_zone->private_data = pdata;
  217. }
  218. /**
  219. * powercap_get_zone_data() - Get private data for a zone
  220. * @power_zone: A pointer to the valid zone instance.
  221. *
  222. * Allows client drivers to get private data associate with a zone,
  223. * using call to powercap_set_zone_data.
  224. */
  225. static inline void *powercap_get_zone_data(struct powercap_zone *power_zone)
  226. {
  227. if (power_zone)
  228. return power_zone->private_data;
  229. return NULL;
  230. }
  231. /**
  232. * powercap_register_control_type() - Register a control_type with framework
  233. * @control_type: Pointer to client allocated memory for the control type
  234. * structure storage. If this is NULL, powercap framework
  235. * will allocate memory and own it.
  236. * Advantage of this parameter is that client can embed
  237. * this data in its data structures and allocate in a
  238. * single call, preventing multiple allocations.
  239. * @control_type_name: The Name of this control_type, which will be shown
  240. * in the sysfs Interface.
  241. * @ops: Callbacks for control type. This parameter is optional.
  242. *
  243. * Used to create a control_type with the power capping class. Here control_type
  244. * can represent a type of technology, which can control a range of power zones.
  245. * For example a control_type can be RAPL (Running Average Power Limit)
  246. * Intel® 64 and IA-32 Processor Architectures. The name can be any string
  247. * which must be unique, otherwise this function returns NULL.
  248. * A pointer to the control_type instance is returned on success.
  249. */
  250. struct powercap_control_type *powercap_register_control_type(
  251. struct powercap_control_type *control_type,
  252. const char *name,
  253. const struct powercap_control_type_ops *ops);
  254. /**
  255. * powercap_unregister_control_type() - Unregister a control_type from framework
  256. * @instance: A pointer to the valid control_type instance.
  257. *
  258. * Used to unregister a control_type with the power capping class.
  259. * All power zones registered under this control type have to be unregistered
  260. * before calling this function, or it will fail with an error code.
  261. */
  262. int powercap_unregister_control_type(struct powercap_control_type *instance);
  263. /* Zone register/unregister API */
  264. /**
  265. * powercap_register_zone() - Register a power zone
  266. * @power_zone: Pointer to client allocated memory for the power zone structure
  267. * storage. If this is NULL, powercap framework will allocate
  268. * memory and own it. Advantage of this parameter is that client
  269. * can embed this data in its data structures and allocate in a
  270. * single call, preventing multiple allocations.
  271. * @control_type: A control_type instance under which this zone operates.
  272. * @name: A name for this zone.
  273. * @parent: A pointer to the parent power zone instance if any or NULL
  274. * @ops: Pointer to zone operation callback structure.
  275. * @no_constraints: Number of constraints for this zone
  276. * @const_ops: Pointer to constraint callback structure
  277. *
  278. * Register a power zone under a given control type. A power zone must register
  279. * a pointer to a structure representing zone callbacks.
  280. * A power zone can be located under a parent power zone, in which case @parent
  281. * should point to it. Otherwise, if @parent is NULL, the new power zone will
  282. * be located directly under the given control type
  283. * For each power zone there may be a number of constraints that appear in the
  284. * sysfs under that zone as attributes with unique numeric IDs.
  285. * Returns pointer to the power_zone on success.
  286. */
  287. struct powercap_zone *powercap_register_zone(
  288. struct powercap_zone *power_zone,
  289. struct powercap_control_type *control_type,
  290. const char *name,
  291. struct powercap_zone *parent,
  292. const struct powercap_zone_ops *ops,
  293. int nr_constraints,
  294. const struct powercap_zone_constraint_ops *const_ops);
  295. /**
  296. * powercap_unregister_zone() - Unregister a zone device
  297. * @control_type: A pointer to the valid instance of a control_type.
  298. * @power_zone: A pointer to the valid zone instance for a control_type
  299. *
  300. * Used to unregister a zone device for a control_type. Caller should
  301. * make sure that children for this zone are unregistered first.
  302. */
  303. int powercap_unregister_zone(struct powercap_control_type *control_type,
  304. struct powercap_zone *power_zone);
  305. #endif