dtpm.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * Copyright 2020 Linaro Limited
  4. *
  5. * Author: Daniel Lezcano <daniel.lezcano@linaro.org>
  6. *
  7. * The powercap based Dynamic Thermal Power Management framework
  8. * provides to the userspace a consistent API to set the power limit
  9. * on some devices.
  10. *
  11. * DTPM defines the functions to create a tree of constraints. Each
  12. * parent node is a virtual description of the aggregation of the
  13. * children. It propagates the constraints set at its level to its
  14. * children and collect the children power information. The leaves of
  15. * the tree are the real devices which have the ability to get their
  16. * current power consumption and set their power limit.
  17. */
  18. #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
  19. #include <linux/dtpm.h>
  20. #include <linux/init.h>
  21. #include <linux/kernel.h>
  22. #include <linux/powercap.h>
  23. #include <linux/slab.h>
  24. #include <linux/mutex.h>
  25. #define DTPM_POWER_LIMIT_FLAG 0
  26. static const char *constraint_name[] = {
  27. "Instantaneous",
  28. };
  29. static DEFINE_MUTEX(dtpm_lock);
  30. static struct powercap_control_type *pct;
  31. static struct dtpm *root;
  32. static int get_time_window_us(struct powercap_zone *pcz, int cid, u64 *window)
  33. {
  34. return -ENOSYS;
  35. }
  36. static int set_time_window_us(struct powercap_zone *pcz, int cid, u64 window)
  37. {
  38. return -ENOSYS;
  39. }
  40. static int get_max_power_range_uw(struct powercap_zone *pcz, u64 *max_power_uw)
  41. {
  42. struct dtpm *dtpm = to_dtpm(pcz);
  43. mutex_lock(&dtpm_lock);
  44. *max_power_uw = dtpm->power_max - dtpm->power_min;
  45. mutex_unlock(&dtpm_lock);
  46. return 0;
  47. }
  48. static int __get_power_uw(struct dtpm *dtpm, u64 *power_uw)
  49. {
  50. struct dtpm *child;
  51. u64 power;
  52. int ret = 0;
  53. if (dtpm->ops) {
  54. *power_uw = dtpm->ops->get_power_uw(dtpm);
  55. return 0;
  56. }
  57. *power_uw = 0;
  58. list_for_each_entry(child, &dtpm->children, sibling) {
  59. ret = __get_power_uw(child, &power);
  60. if (ret)
  61. break;
  62. *power_uw += power;
  63. }
  64. return ret;
  65. }
  66. static int get_power_uw(struct powercap_zone *pcz, u64 *power_uw)
  67. {
  68. struct dtpm *dtpm = to_dtpm(pcz);
  69. int ret;
  70. mutex_lock(&dtpm_lock);
  71. ret = __get_power_uw(dtpm, power_uw);
  72. mutex_unlock(&dtpm_lock);
  73. return ret;
  74. }
  75. static void __dtpm_rebalance_weight(struct dtpm *dtpm)
  76. {
  77. struct dtpm *child;
  78. list_for_each_entry(child, &dtpm->children, sibling) {
  79. pr_debug("Setting weight '%d' for '%s'\n",
  80. child->weight, child->zone.name);
  81. child->weight = DIV64_U64_ROUND_CLOSEST(
  82. child->power_max * 1024, dtpm->power_max);
  83. __dtpm_rebalance_weight(child);
  84. }
  85. }
  86. static void __dtpm_sub_power(struct dtpm *dtpm)
  87. {
  88. struct dtpm *parent = dtpm->parent;
  89. while (parent) {
  90. parent->power_min -= dtpm->power_min;
  91. parent->power_max -= dtpm->power_max;
  92. parent->power_limit -= dtpm->power_limit;
  93. parent = parent->parent;
  94. }
  95. __dtpm_rebalance_weight(root);
  96. }
  97. static void __dtpm_add_power(struct dtpm *dtpm)
  98. {
  99. struct dtpm *parent = dtpm->parent;
  100. while (parent) {
  101. parent->power_min += dtpm->power_min;
  102. parent->power_max += dtpm->power_max;
  103. parent->power_limit += dtpm->power_limit;
  104. parent = parent->parent;
  105. }
  106. __dtpm_rebalance_weight(root);
  107. }
  108. /**
  109. * dtpm_update_power - Update the power on the dtpm
  110. * @dtpm: a pointer to a dtpm structure to update
  111. * @power_min: a u64 representing the new power_min value
  112. * @power_max: a u64 representing the new power_max value
  113. *
  114. * Function to update the power values of the dtpm node specified in
  115. * parameter. These new values will be propagated to the tree.
  116. *
  117. * Return: zero on success, -EINVAL if the values are inconsistent
  118. */
  119. int dtpm_update_power(struct dtpm *dtpm, u64 power_min, u64 power_max)
  120. {
  121. int ret = 0;
  122. mutex_lock(&dtpm_lock);
  123. if (power_min == dtpm->power_min && power_max == dtpm->power_max)
  124. goto unlock;
  125. if (power_max < power_min) {
  126. ret = -EINVAL;
  127. goto unlock;
  128. }
  129. __dtpm_sub_power(dtpm);
  130. dtpm->power_min = power_min;
  131. dtpm->power_max = power_max;
  132. if (!test_bit(DTPM_POWER_LIMIT_FLAG, &dtpm->flags))
  133. dtpm->power_limit = power_max;
  134. __dtpm_add_power(dtpm);
  135. unlock:
  136. mutex_unlock(&dtpm_lock);
  137. return ret;
  138. }
  139. /**
  140. * dtpm_release_zone - Cleanup when the node is released
  141. * @pcz: a pointer to a powercap_zone structure
  142. *
  143. * Do some housecleaning and update the weight on the tree. The
  144. * release will be denied if the node has children. This function must
  145. * be called by the specific release callback of the different
  146. * backends.
  147. *
  148. * Return: 0 on success, -EBUSY if there are children
  149. */
  150. int dtpm_release_zone(struct powercap_zone *pcz)
  151. {
  152. struct dtpm *dtpm = to_dtpm(pcz);
  153. struct dtpm *parent = dtpm->parent;
  154. mutex_lock(&dtpm_lock);
  155. if (!list_empty(&dtpm->children)) {
  156. mutex_unlock(&dtpm_lock);
  157. return -EBUSY;
  158. }
  159. if (parent)
  160. list_del(&dtpm->sibling);
  161. __dtpm_sub_power(dtpm);
  162. mutex_unlock(&dtpm_lock);
  163. if (dtpm->ops)
  164. dtpm->ops->release(dtpm);
  165. if (root == dtpm)
  166. root = NULL;
  167. kfree(dtpm);
  168. return 0;
  169. }
  170. static int __get_power_limit_uw(struct dtpm *dtpm, int cid, u64 *power_limit)
  171. {
  172. *power_limit = dtpm->power_limit;
  173. return 0;
  174. }
  175. static int get_power_limit_uw(struct powercap_zone *pcz,
  176. int cid, u64 *power_limit)
  177. {
  178. struct dtpm *dtpm = to_dtpm(pcz);
  179. int ret;
  180. mutex_lock(&dtpm_lock);
  181. ret = __get_power_limit_uw(dtpm, cid, power_limit);
  182. mutex_unlock(&dtpm_lock);
  183. return ret;
  184. }
  185. /*
  186. * Set the power limit on the nodes, the power limit is distributed
  187. * given the weight of the children.
  188. *
  189. * The dtpm node lock must be held when calling this function.
  190. */
  191. static int __set_power_limit_uw(struct dtpm *dtpm, int cid, u64 power_limit)
  192. {
  193. struct dtpm *child;
  194. int ret = 0;
  195. u64 power;
  196. /*
  197. * A max power limitation means we remove the power limit,
  198. * otherwise we set a constraint and flag the dtpm node.
  199. */
  200. if (power_limit == dtpm->power_max) {
  201. clear_bit(DTPM_POWER_LIMIT_FLAG, &dtpm->flags);
  202. } else {
  203. set_bit(DTPM_POWER_LIMIT_FLAG, &dtpm->flags);
  204. }
  205. pr_debug("Setting power limit for '%s': %llu uW\n",
  206. dtpm->zone.name, power_limit);
  207. /*
  208. * Only leaves of the dtpm tree has ops to get/set the power
  209. */
  210. if (dtpm->ops) {
  211. dtpm->power_limit = dtpm->ops->set_power_uw(dtpm, power_limit);
  212. } else {
  213. dtpm->power_limit = 0;
  214. list_for_each_entry(child, &dtpm->children, sibling) {
  215. /*
  216. * Integer division rounding will inevitably
  217. * lead to a different min or max value when
  218. * set several times. In order to restore the
  219. * initial value, we force the child's min or
  220. * max power every time if the constraint is
  221. * at the boundaries.
  222. */
  223. if (power_limit == dtpm->power_max) {
  224. power = child->power_max;
  225. } else if (power_limit == dtpm->power_min) {
  226. power = child->power_min;
  227. } else {
  228. power = DIV_ROUND_CLOSEST_ULL(
  229. power_limit * child->weight, 1024);
  230. }
  231. pr_debug("Setting power limit for '%s': %llu uW\n",
  232. child->zone.name, power);
  233. ret = __set_power_limit_uw(child, cid, power);
  234. if (!ret)
  235. ret = __get_power_limit_uw(child, cid, &power);
  236. if (ret)
  237. break;
  238. dtpm->power_limit += power;
  239. }
  240. }
  241. return ret;
  242. }
  243. static int set_power_limit_uw(struct powercap_zone *pcz,
  244. int cid, u64 power_limit)
  245. {
  246. struct dtpm *dtpm = to_dtpm(pcz);
  247. int ret;
  248. mutex_lock(&dtpm_lock);
  249. /*
  250. * Don't allow values outside of the power range previously
  251. * set when initializing the power numbers.
  252. */
  253. power_limit = clamp_val(power_limit, dtpm->power_min, dtpm->power_max);
  254. ret = __set_power_limit_uw(dtpm, cid, power_limit);
  255. pr_debug("%s: power limit: %llu uW, power max: %llu uW\n",
  256. dtpm->zone.name, dtpm->power_limit, dtpm->power_max);
  257. mutex_unlock(&dtpm_lock);
  258. return ret;
  259. }
  260. static const char *get_constraint_name(struct powercap_zone *pcz, int cid)
  261. {
  262. return constraint_name[cid];
  263. }
  264. static int get_max_power_uw(struct powercap_zone *pcz, int id, u64 *max_power)
  265. {
  266. struct dtpm *dtpm = to_dtpm(pcz);
  267. mutex_lock(&dtpm_lock);
  268. *max_power = dtpm->power_max;
  269. mutex_unlock(&dtpm_lock);
  270. return 0;
  271. }
  272. static struct powercap_zone_constraint_ops constraint_ops = {
  273. .set_power_limit_uw = set_power_limit_uw,
  274. .get_power_limit_uw = get_power_limit_uw,
  275. .set_time_window_us = set_time_window_us,
  276. .get_time_window_us = get_time_window_us,
  277. .get_max_power_uw = get_max_power_uw,
  278. .get_name = get_constraint_name,
  279. };
  280. static struct powercap_zone_ops zone_ops = {
  281. .get_max_power_range_uw = get_max_power_range_uw,
  282. .get_power_uw = get_power_uw,
  283. .release = dtpm_release_zone,
  284. };
  285. /**
  286. * dtpm_alloc - Allocate and initialize a dtpm struct
  287. * @name: a string specifying the name of the node
  288. *
  289. * Return: a struct dtpm pointer, NULL in case of error
  290. */
  291. struct dtpm *dtpm_alloc(struct dtpm_ops *ops)
  292. {
  293. struct dtpm *dtpm;
  294. dtpm = kzalloc(sizeof(*dtpm), GFP_KERNEL);
  295. if (dtpm) {
  296. INIT_LIST_HEAD(&dtpm->children);
  297. INIT_LIST_HEAD(&dtpm->sibling);
  298. dtpm->weight = 1024;
  299. dtpm->ops = ops;
  300. }
  301. return dtpm;
  302. }
  303. /**
  304. * dtpm_unregister - Unregister a dtpm node from the hierarchy tree
  305. * @dtpm: a pointer to a dtpm structure corresponding to the node to be removed
  306. *
  307. * Call the underlying powercap unregister function. That will call
  308. * the release callback of the powercap zone.
  309. */
  310. void dtpm_unregister(struct dtpm *dtpm)
  311. {
  312. powercap_unregister_zone(pct, &dtpm->zone);
  313. pr_info("Unregistered dtpm node '%s'\n", dtpm->zone.name);
  314. }
  315. /**
  316. * dtpm_register - Register a dtpm node in the hierarchy tree
  317. * @name: a string specifying the name of the node
  318. * @dtpm: a pointer to a dtpm structure corresponding to the new node
  319. * @parent: a pointer to a dtpm structure corresponding to the parent node
  320. *
  321. * Create a dtpm node in the tree. If no parent is specified, the node
  322. * is the root node of the hierarchy. If the root node already exists,
  323. * then the registration will fail. The powercap controller must be
  324. * initialized before calling this function.
  325. *
  326. * The dtpm structure must be initialized with the power numbers
  327. * before calling this function.
  328. *
  329. * Return: zero on success, a negative value in case of error:
  330. * -EAGAIN: the function is called before the framework is initialized.
  331. * -EBUSY: the root node is already inserted
  332. * -EINVAL: * there is no root node yet and @parent is specified
  333. * * no all ops are defined
  334. * * parent have ops which are reserved for leaves
  335. * Other negative values are reported back from the powercap framework
  336. */
  337. int dtpm_register(const char *name, struct dtpm *dtpm, struct dtpm *parent)
  338. {
  339. struct powercap_zone *pcz;
  340. if (!pct)
  341. return -EAGAIN;
  342. if (root && !parent)
  343. return -EBUSY;
  344. if (!root && parent)
  345. return -EINVAL;
  346. if (parent && parent->ops)
  347. return -EINVAL;
  348. if (!dtpm)
  349. return -EINVAL;
  350. if (dtpm->ops && !(dtpm->ops->set_power_uw &&
  351. dtpm->ops->get_power_uw &&
  352. dtpm->ops->release))
  353. return -EINVAL;
  354. pcz = powercap_register_zone(&dtpm->zone, pct, name,
  355. parent ? &parent->zone : NULL,
  356. &zone_ops, MAX_DTPM_CONSTRAINTS,
  357. &constraint_ops);
  358. if (IS_ERR(pcz))
  359. return PTR_ERR(pcz);
  360. mutex_lock(&dtpm_lock);
  361. if (parent) {
  362. list_add_tail(&dtpm->sibling, &parent->children);
  363. dtpm->parent = parent;
  364. } else {
  365. root = dtpm;
  366. }
  367. __dtpm_add_power(dtpm);
  368. pr_info("Registered dtpm node '%s' / %llu-%llu uW, \n",
  369. dtpm->zone.name, dtpm->power_min, dtpm->power_max);
  370. mutex_unlock(&dtpm_lock);
  371. return 0;
  372. }
  373. static int __init dtpm_init(void)
  374. {
  375. struct dtpm_descr **dtpm_descr;
  376. pct = powercap_register_control_type(NULL, "dtpm", NULL);
  377. if (IS_ERR(pct)) {
  378. pr_err("Failed to register control type\n");
  379. return PTR_ERR(pct);
  380. }
  381. for_each_dtpm_table(dtpm_descr)
  382. (*dtpm_descr)->init(*dtpm_descr);
  383. return 0;
  384. }
  385. late_initcall(dtpm_init);