dtpm.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * Copyright 2020 Linaro Limited
  4. *
  5. * Author: Daniel Lezcano <[email protected]>
  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. #include <linux/of.h>
  26. #include "dtpm_subsys.h"
  27. #define DTPM_POWER_LIMIT_FLAG 0
  28. static const char *constraint_name[] = {
  29. "Instantaneous",
  30. };
  31. static DEFINE_MUTEX(dtpm_lock);
  32. static struct powercap_control_type *pct;
  33. static struct dtpm *root;
  34. static int get_time_window_us(struct powercap_zone *pcz, int cid, u64 *window)
  35. {
  36. return -ENOSYS;
  37. }
  38. static int set_time_window_us(struct powercap_zone *pcz, int cid, u64 window)
  39. {
  40. return -ENOSYS;
  41. }
  42. static int get_max_power_range_uw(struct powercap_zone *pcz, u64 *max_power_uw)
  43. {
  44. struct dtpm *dtpm = to_dtpm(pcz);
  45. *max_power_uw = dtpm->power_max - dtpm->power_min;
  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. return __get_power_uw(to_dtpm(pcz), power_uw);
  69. }
  70. static void __dtpm_rebalance_weight(struct dtpm *dtpm)
  71. {
  72. struct dtpm *child;
  73. list_for_each_entry(child, &dtpm->children, sibling) {
  74. pr_debug("Setting weight '%d' for '%s'\n",
  75. child->weight, child->zone.name);
  76. child->weight = DIV64_U64_ROUND_CLOSEST(
  77. child->power_max * 1024, dtpm->power_max);
  78. __dtpm_rebalance_weight(child);
  79. }
  80. }
  81. static void __dtpm_sub_power(struct dtpm *dtpm)
  82. {
  83. struct dtpm *parent = dtpm->parent;
  84. while (parent) {
  85. parent->power_min -= dtpm->power_min;
  86. parent->power_max -= dtpm->power_max;
  87. parent->power_limit -= dtpm->power_limit;
  88. parent = parent->parent;
  89. }
  90. }
  91. static void __dtpm_add_power(struct dtpm *dtpm)
  92. {
  93. struct dtpm *parent = dtpm->parent;
  94. while (parent) {
  95. parent->power_min += dtpm->power_min;
  96. parent->power_max += dtpm->power_max;
  97. parent->power_limit += dtpm->power_limit;
  98. parent = parent->parent;
  99. }
  100. }
  101. /**
  102. * dtpm_update_power - Update the power on the dtpm
  103. * @dtpm: a pointer to a dtpm structure to update
  104. *
  105. * Function to update the power values of the dtpm node specified in
  106. * parameter. These new values will be propagated to the tree.
  107. *
  108. * Return: zero on success, -EINVAL if the values are inconsistent
  109. */
  110. int dtpm_update_power(struct dtpm *dtpm)
  111. {
  112. int ret;
  113. __dtpm_sub_power(dtpm);
  114. ret = dtpm->ops->update_power_uw(dtpm);
  115. if (ret)
  116. pr_err("Failed to update power for '%s': %d\n",
  117. dtpm->zone.name, ret);
  118. if (!test_bit(DTPM_POWER_LIMIT_FLAG, &dtpm->flags))
  119. dtpm->power_limit = dtpm->power_max;
  120. __dtpm_add_power(dtpm);
  121. if (root)
  122. __dtpm_rebalance_weight(root);
  123. return ret;
  124. }
  125. /**
  126. * dtpm_release_zone - Cleanup when the node is released
  127. * @pcz: a pointer to a powercap_zone structure
  128. *
  129. * Do some housecleaning and update the weight on the tree. The
  130. * release will be denied if the node has children. This function must
  131. * be called by the specific release callback of the different
  132. * backends.
  133. *
  134. * Return: 0 on success, -EBUSY if there are children
  135. */
  136. int dtpm_release_zone(struct powercap_zone *pcz)
  137. {
  138. struct dtpm *dtpm = to_dtpm(pcz);
  139. struct dtpm *parent = dtpm->parent;
  140. if (!list_empty(&dtpm->children))
  141. return -EBUSY;
  142. if (parent)
  143. list_del(&dtpm->sibling);
  144. __dtpm_sub_power(dtpm);
  145. if (dtpm->ops)
  146. dtpm->ops->release(dtpm);
  147. else
  148. kfree(dtpm);
  149. return 0;
  150. }
  151. static int get_power_limit_uw(struct powercap_zone *pcz,
  152. int cid, u64 *power_limit)
  153. {
  154. *power_limit = to_dtpm(pcz)->power_limit;
  155. return 0;
  156. }
  157. /*
  158. * Set the power limit on the nodes, the power limit is distributed
  159. * given the weight of the children.
  160. *
  161. * The dtpm node lock must be held when calling this function.
  162. */
  163. static int __set_power_limit_uw(struct dtpm *dtpm, int cid, u64 power_limit)
  164. {
  165. struct dtpm *child;
  166. int ret = 0;
  167. u64 power;
  168. /*
  169. * A max power limitation means we remove the power limit,
  170. * otherwise we set a constraint and flag the dtpm node.
  171. */
  172. if (power_limit == dtpm->power_max) {
  173. clear_bit(DTPM_POWER_LIMIT_FLAG, &dtpm->flags);
  174. } else {
  175. set_bit(DTPM_POWER_LIMIT_FLAG, &dtpm->flags);
  176. }
  177. pr_debug("Setting power limit for '%s': %llu uW\n",
  178. dtpm->zone.name, power_limit);
  179. /*
  180. * Only leaves of the dtpm tree has ops to get/set the power
  181. */
  182. if (dtpm->ops) {
  183. dtpm->power_limit = dtpm->ops->set_power_uw(dtpm, power_limit);
  184. } else {
  185. dtpm->power_limit = 0;
  186. list_for_each_entry(child, &dtpm->children, sibling) {
  187. /*
  188. * Integer division rounding will inevitably
  189. * lead to a different min or max value when
  190. * set several times. In order to restore the
  191. * initial value, we force the child's min or
  192. * max power every time if the constraint is
  193. * at the boundaries.
  194. */
  195. if (power_limit == dtpm->power_max) {
  196. power = child->power_max;
  197. } else if (power_limit == dtpm->power_min) {
  198. power = child->power_min;
  199. } else {
  200. power = DIV_ROUND_CLOSEST_ULL(
  201. power_limit * child->weight, 1024);
  202. }
  203. pr_debug("Setting power limit for '%s': %llu uW\n",
  204. child->zone.name, power);
  205. ret = __set_power_limit_uw(child, cid, power);
  206. if (!ret)
  207. ret = get_power_limit_uw(&child->zone, cid, &power);
  208. if (ret)
  209. break;
  210. dtpm->power_limit += power;
  211. }
  212. }
  213. return ret;
  214. }
  215. static int set_power_limit_uw(struct powercap_zone *pcz,
  216. int cid, u64 power_limit)
  217. {
  218. struct dtpm *dtpm = to_dtpm(pcz);
  219. int ret;
  220. /*
  221. * Don't allow values outside of the power range previously
  222. * set when initializing the power numbers.
  223. */
  224. power_limit = clamp_val(power_limit, dtpm->power_min, dtpm->power_max);
  225. ret = __set_power_limit_uw(dtpm, cid, power_limit);
  226. pr_debug("%s: power limit: %llu uW, power max: %llu uW\n",
  227. dtpm->zone.name, dtpm->power_limit, dtpm->power_max);
  228. return ret;
  229. }
  230. static const char *get_constraint_name(struct powercap_zone *pcz, int cid)
  231. {
  232. return constraint_name[cid];
  233. }
  234. static int get_max_power_uw(struct powercap_zone *pcz, int id, u64 *max_power)
  235. {
  236. *max_power = to_dtpm(pcz)->power_max;
  237. return 0;
  238. }
  239. static struct powercap_zone_constraint_ops constraint_ops = {
  240. .set_power_limit_uw = set_power_limit_uw,
  241. .get_power_limit_uw = get_power_limit_uw,
  242. .set_time_window_us = set_time_window_us,
  243. .get_time_window_us = get_time_window_us,
  244. .get_max_power_uw = get_max_power_uw,
  245. .get_name = get_constraint_name,
  246. };
  247. static struct powercap_zone_ops zone_ops = {
  248. .get_max_power_range_uw = get_max_power_range_uw,
  249. .get_power_uw = get_power_uw,
  250. .release = dtpm_release_zone,
  251. };
  252. /**
  253. * dtpm_init - Allocate and initialize a dtpm struct
  254. * @dtpm: The dtpm struct pointer to be initialized
  255. * @ops: The dtpm device specific ops, NULL for a virtual node
  256. */
  257. void dtpm_init(struct dtpm *dtpm, struct dtpm_ops *ops)
  258. {
  259. if (dtpm) {
  260. INIT_LIST_HEAD(&dtpm->children);
  261. INIT_LIST_HEAD(&dtpm->sibling);
  262. dtpm->weight = 1024;
  263. dtpm->ops = ops;
  264. }
  265. }
  266. /**
  267. * dtpm_unregister - Unregister a dtpm node from the hierarchy tree
  268. * @dtpm: a pointer to a dtpm structure corresponding to the node to be removed
  269. *
  270. * Call the underlying powercap unregister function. That will call
  271. * the release callback of the powercap zone.
  272. */
  273. void dtpm_unregister(struct dtpm *dtpm)
  274. {
  275. powercap_unregister_zone(pct, &dtpm->zone);
  276. pr_debug("Unregistered dtpm node '%s'\n", dtpm->zone.name);
  277. }
  278. /**
  279. * dtpm_register - Register a dtpm node in the hierarchy tree
  280. * @name: a string specifying the name of the node
  281. * @dtpm: a pointer to a dtpm structure corresponding to the new node
  282. * @parent: a pointer to a dtpm structure corresponding to the parent node
  283. *
  284. * Create a dtpm node in the tree. If no parent is specified, the node
  285. * is the root node of the hierarchy. If the root node already exists,
  286. * then the registration will fail. The powercap controller must be
  287. * initialized before calling this function.
  288. *
  289. * The dtpm structure must be initialized with the power numbers
  290. * before calling this function.
  291. *
  292. * Return: zero on success, a negative value in case of error:
  293. * -EAGAIN: the function is called before the framework is initialized.
  294. * -EBUSY: the root node is already inserted
  295. * -EINVAL: * there is no root node yet and @parent is specified
  296. * * no all ops are defined
  297. * * parent have ops which are reserved for leaves
  298. * Other negative values are reported back from the powercap framework
  299. */
  300. int dtpm_register(const char *name, struct dtpm *dtpm, struct dtpm *parent)
  301. {
  302. struct powercap_zone *pcz;
  303. if (!pct)
  304. return -EAGAIN;
  305. if (root && !parent)
  306. return -EBUSY;
  307. if (!root && parent)
  308. return -EINVAL;
  309. if (parent && parent->ops)
  310. return -EINVAL;
  311. if (!dtpm)
  312. return -EINVAL;
  313. if (dtpm->ops && !(dtpm->ops->set_power_uw &&
  314. dtpm->ops->get_power_uw &&
  315. dtpm->ops->update_power_uw &&
  316. dtpm->ops->release))
  317. return -EINVAL;
  318. pcz = powercap_register_zone(&dtpm->zone, pct, name,
  319. parent ? &parent->zone : NULL,
  320. &zone_ops, MAX_DTPM_CONSTRAINTS,
  321. &constraint_ops);
  322. if (IS_ERR(pcz))
  323. return PTR_ERR(pcz);
  324. if (parent) {
  325. list_add_tail(&dtpm->sibling, &parent->children);
  326. dtpm->parent = parent;
  327. } else {
  328. root = dtpm;
  329. }
  330. if (dtpm->ops && !dtpm->ops->update_power_uw(dtpm)) {
  331. __dtpm_add_power(dtpm);
  332. dtpm->power_limit = dtpm->power_max;
  333. }
  334. pr_debug("Registered dtpm node '%s' / %llu-%llu uW, \n",
  335. dtpm->zone.name, dtpm->power_min, dtpm->power_max);
  336. return 0;
  337. }
  338. static struct dtpm *dtpm_setup_virtual(const struct dtpm_node *hierarchy,
  339. struct dtpm *parent)
  340. {
  341. struct dtpm *dtpm;
  342. int ret;
  343. dtpm = kzalloc(sizeof(*dtpm), GFP_KERNEL);
  344. if (!dtpm)
  345. return ERR_PTR(-ENOMEM);
  346. dtpm_init(dtpm, NULL);
  347. ret = dtpm_register(hierarchy->name, dtpm, parent);
  348. if (ret) {
  349. pr_err("Failed to register dtpm node '%s': %d\n",
  350. hierarchy->name, ret);
  351. kfree(dtpm);
  352. return ERR_PTR(ret);
  353. }
  354. return dtpm;
  355. }
  356. static struct dtpm *dtpm_setup_dt(const struct dtpm_node *hierarchy,
  357. struct dtpm *parent)
  358. {
  359. struct device_node *np;
  360. int i, ret;
  361. np = of_find_node_by_path(hierarchy->name);
  362. if (!np) {
  363. pr_err("Failed to find '%s'\n", hierarchy->name);
  364. return ERR_PTR(-ENXIO);
  365. }
  366. for (i = 0; i < ARRAY_SIZE(dtpm_subsys); i++) {
  367. if (!dtpm_subsys[i]->setup)
  368. continue;
  369. ret = dtpm_subsys[i]->setup(parent, np);
  370. if (ret) {
  371. pr_err("Failed to setup '%s': %d\n", dtpm_subsys[i]->name, ret);
  372. of_node_put(np);
  373. return ERR_PTR(ret);
  374. }
  375. }
  376. of_node_put(np);
  377. /*
  378. * By returning a NULL pointer, we let know the caller there
  379. * is no child for us as we are a leaf of the tree
  380. */
  381. return NULL;
  382. }
  383. typedef struct dtpm * (*dtpm_node_callback_t)(const struct dtpm_node *, struct dtpm *);
  384. static dtpm_node_callback_t dtpm_node_callback[] = {
  385. [DTPM_NODE_VIRTUAL] = dtpm_setup_virtual,
  386. [DTPM_NODE_DT] = dtpm_setup_dt,
  387. };
  388. static int dtpm_for_each_child(const struct dtpm_node *hierarchy,
  389. const struct dtpm_node *it, struct dtpm *parent)
  390. {
  391. struct dtpm *dtpm;
  392. int i, ret;
  393. for (i = 0; hierarchy[i].name; i++) {
  394. if (hierarchy[i].parent != it)
  395. continue;
  396. dtpm = dtpm_node_callback[hierarchy[i].type](&hierarchy[i], parent);
  397. /*
  398. * A NULL pointer means there is no children, hence we
  399. * continue without going deeper in the recursivity.
  400. */
  401. if (!dtpm)
  402. continue;
  403. /*
  404. * There are multiple reasons why the callback could
  405. * fail. The generic glue is abstracting the backend
  406. * and therefore it is not possible to report back or
  407. * take a decision based on the error. In any case,
  408. * if this call fails, it is not critical in the
  409. * hierarchy creation, we can assume the underlying
  410. * service is not found, so we continue without this
  411. * branch in the tree but with a warning to log the
  412. * information the node was not created.
  413. */
  414. if (IS_ERR(dtpm)) {
  415. pr_warn("Failed to create '%s' in the hierarchy\n",
  416. hierarchy[i].name);
  417. continue;
  418. }
  419. ret = dtpm_for_each_child(hierarchy, &hierarchy[i], dtpm);
  420. if (ret)
  421. return ret;
  422. }
  423. return 0;
  424. }
  425. /**
  426. * dtpm_create_hierarchy - Create the dtpm hierarchy
  427. * @hierarchy: An array of struct dtpm_node describing the hierarchy
  428. *
  429. * The function is called by the platform specific code with the
  430. * description of the different node in the hierarchy. It creates the
  431. * tree in the sysfs filesystem under the powercap dtpm entry.
  432. *
  433. * The expected tree has the format:
  434. *
  435. * struct dtpm_node hierarchy[] = {
  436. * [0] { .name = "topmost", type = DTPM_NODE_VIRTUAL },
  437. * [1] { .name = "package", .type = DTPM_NODE_VIRTUAL, .parent = &hierarchy[0] },
  438. * [2] { .name = "/cpus/cpu0", .type = DTPM_NODE_DT, .parent = &hierarchy[1] },
  439. * [3] { .name = "/cpus/cpu1", .type = DTPM_NODE_DT, .parent = &hierarchy[1] },
  440. * [4] { .name = "/cpus/cpu2", .type = DTPM_NODE_DT, .parent = &hierarchy[1] },
  441. * [5] { .name = "/cpus/cpu3", .type = DTPM_NODE_DT, .parent = &hierarchy[1] },
  442. * [6] { }
  443. * };
  444. *
  445. * The last element is always an empty one and marks the end of the
  446. * array.
  447. *
  448. * Return: zero on success, a negative value in case of error. Errors
  449. * are reported back from the underlying functions.
  450. */
  451. int dtpm_create_hierarchy(struct of_device_id *dtpm_match_table)
  452. {
  453. const struct of_device_id *match;
  454. const struct dtpm_node *hierarchy;
  455. struct device_node *np;
  456. int i, ret;
  457. mutex_lock(&dtpm_lock);
  458. if (pct) {
  459. ret = -EBUSY;
  460. goto out_unlock;
  461. }
  462. pct = powercap_register_control_type(NULL, "dtpm", NULL);
  463. if (IS_ERR(pct)) {
  464. pr_err("Failed to register control type\n");
  465. ret = PTR_ERR(pct);
  466. goto out_pct;
  467. }
  468. ret = -ENODEV;
  469. np = of_find_node_by_path("/");
  470. if (!np)
  471. goto out_err;
  472. match = of_match_node(dtpm_match_table, np);
  473. of_node_put(np);
  474. if (!match)
  475. goto out_err;
  476. hierarchy = match->data;
  477. if (!hierarchy) {
  478. ret = -EFAULT;
  479. goto out_err;
  480. }
  481. ret = dtpm_for_each_child(hierarchy, NULL, NULL);
  482. if (ret)
  483. goto out_err;
  484. for (i = 0; i < ARRAY_SIZE(dtpm_subsys); i++) {
  485. if (!dtpm_subsys[i]->init)
  486. continue;
  487. ret = dtpm_subsys[i]->init();
  488. if (ret)
  489. pr_info("Failed to initialize '%s': %d",
  490. dtpm_subsys[i]->name, ret);
  491. }
  492. mutex_unlock(&dtpm_lock);
  493. return 0;
  494. out_err:
  495. powercap_unregister_control_type(pct);
  496. out_pct:
  497. pct = NULL;
  498. out_unlock:
  499. mutex_unlock(&dtpm_lock);
  500. return ret;
  501. }
  502. EXPORT_SYMBOL_GPL(dtpm_create_hierarchy);
  503. static void __dtpm_destroy_hierarchy(struct dtpm *dtpm)
  504. {
  505. struct dtpm *child, *aux;
  506. list_for_each_entry_safe(child, aux, &dtpm->children, sibling)
  507. __dtpm_destroy_hierarchy(child);
  508. /*
  509. * At this point, we know all children were removed from the
  510. * recursive call before
  511. */
  512. dtpm_unregister(dtpm);
  513. }
  514. void dtpm_destroy_hierarchy(void)
  515. {
  516. int i;
  517. mutex_lock(&dtpm_lock);
  518. if (!pct)
  519. goto out_unlock;
  520. __dtpm_destroy_hierarchy(root);
  521. for (i = 0; i < ARRAY_SIZE(dtpm_subsys); i++) {
  522. if (!dtpm_subsys[i]->exit)
  523. continue;
  524. dtpm_subsys[i]->exit();
  525. }
  526. powercap_unregister_control_type(pct);
  527. pct = NULL;
  528. root = NULL;
  529. out_unlock:
  530. mutex_unlock(&dtpm_lock);
  531. }
  532. EXPORT_SYMBOL_GPL(dtpm_destroy_hierarchy);