power_allocator.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544
  1. /*
  2. * A power allocator to manage temperature
  3. *
  4. * Copyright (C) 2014 ARM Ltd.
  5. *
  6. * This program is free software; you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License version 2 as
  8. * published by the Free Software Foundation.
  9. *
  10. * This program is distributed "as is" WITHOUT ANY WARRANTY of any
  11. * kind, whether express or implied; without even the implied warranty
  12. * of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. */
  15. #define pr_fmt(fmt) "Power allocator: " fmt
  16. #include <linux/rculist.h>
  17. #include <linux/slab.h>
  18. #include <linux/thermal.h>
  19. #define CREATE_TRACE_POINTS
  20. #include <trace/events/thermal_power_allocator.h>
  21. #include "thermal_core.h"
  22. #define FRAC_BITS 10
  23. #define int_to_frac(x) ((x) << FRAC_BITS)
  24. #define frac_to_int(x) ((x) >> FRAC_BITS)
  25. /**
  26. * mul_frac() - multiply two fixed-point numbers
  27. * @x: first multiplicand
  28. * @y: second multiplicand
  29. *
  30. * Return: the result of multiplying two fixed-point numbers. The
  31. * result is also a fixed-point number.
  32. */
  33. static inline s64 mul_frac(s64 x, s64 y)
  34. {
  35. return (x * y) >> FRAC_BITS;
  36. }
  37. /**
  38. * div_frac() - divide two fixed-point numbers
  39. * @x: the dividend
  40. * @y: the divisor
  41. *
  42. * Return: the result of dividing two fixed-point numbers. The
  43. * result is also a fixed-point number.
  44. */
  45. static inline s64 div_frac(s64 x, s64 y)
  46. {
  47. return div_s64(x << FRAC_BITS, y);
  48. }
  49. /**
  50. * struct power_allocator_params - parameters for the power allocator governor
  51. * @err_integral: accumulated error in the PID controller.
  52. * @prev_err: error in the previous iteration of the PID controller.
  53. * Used to calculate the derivative term.
  54. * @trip_switch_on: first passive trip point of the thermal zone. The
  55. * governor switches on when this trip point is crossed.
  56. * @trip_max_desired_temperature: last passive trip point of the thermal
  57. * zone. The temperature we are
  58. * controlling for.
  59. */
  60. struct power_allocator_params {
  61. s64 err_integral;
  62. s32 prev_err;
  63. int trip_switch_on;
  64. int trip_max_desired_temperature;
  65. };
  66. /**
  67. * pid_controller() - PID controller
  68. * @tz: thermal zone we are operating in
  69. * @current_temp: the current temperature in millicelsius
  70. * @control_temp: the target temperature in millicelsius
  71. * @max_allocatable_power: maximum allocatable power for this thermal zone
  72. *
  73. * This PID controller increases the available power budget so that the
  74. * temperature of the thermal zone gets as close as possible to
  75. * @control_temp and limits the power if it exceeds it. k_po is the
  76. * proportional term when we are overshooting, k_pu is the
  77. * proportional term when we are undershooting. integral_cutoff is a
  78. * threshold below which we stop accumulating the error. The
  79. * accumulated error is only valid if the requested power will make
  80. * the system warmer. If the system is mostly idle, there's no point
  81. * in accumulating positive error.
  82. *
  83. * Return: The power budget for the next period.
  84. */
  85. static u32 pid_controller(struct thermal_zone_device *tz,
  86. int current_temp,
  87. int control_temp,
  88. u32 max_allocatable_power)
  89. {
  90. s64 p, i, d, power_range;
  91. s32 err, max_power_frac;
  92. struct power_allocator_params *params = tz->governor_data;
  93. max_power_frac = int_to_frac(max_allocatable_power);
  94. err = control_temp - current_temp;
  95. err = int_to_frac(err);
  96. /* Calculate the proportional term */
  97. p = mul_frac(err < 0 ? tz->tzp->k_po : tz->tzp->k_pu, err);
  98. /*
  99. * Calculate the integral term
  100. *
  101. * if the error is less than cut off allow integration (but
  102. * the integral is limited to max power)
  103. */
  104. i = mul_frac(tz->tzp->k_i, params->err_integral);
  105. if (err < int_to_frac(tz->tzp->integral_cutoff)) {
  106. s64 i_next = i + mul_frac(tz->tzp->k_i, err);
  107. if (abs64(i_next) < max_power_frac) {
  108. i = i_next;
  109. params->err_integral += err;
  110. }
  111. }
  112. /*
  113. * Calculate the derivative term
  114. *
  115. * We do err - prev_err, so with a positive k_d, a decreasing
  116. * error (i.e. driving closer to the line) results in less
  117. * power being applied, slowing down the controller)
  118. */
  119. d = mul_frac(tz->tzp->k_d, err - params->prev_err);
  120. d = div_frac(d, tz->passive_delay);
  121. params->prev_err = err;
  122. power_range = p + i + d;
  123. /* feed-forward the known sustainable dissipatable power */
  124. power_range = tz->tzp->sustainable_power + frac_to_int(power_range);
  125. power_range = clamp(power_range, (s64)0, (s64)max_allocatable_power);
  126. trace_thermal_power_allocator_pid(tz, frac_to_int(err),
  127. frac_to_int(params->err_integral),
  128. frac_to_int(p), frac_to_int(i),
  129. frac_to_int(d), power_range);
  130. return power_range;
  131. }
  132. /**
  133. * divvy_up_power() - divvy the allocated power between the actors
  134. * @req_power: each actor's requested power
  135. * @max_power: each actor's maximum available power
  136. * @num_actors: size of the @req_power, @max_power and @granted_power's array
  137. * @total_req_power: sum of @req_power
  138. * @power_range: total allocated power
  139. * @granted_power: output array: each actor's granted power
  140. * @extra_actor_power: an appropriately sized array to be used in the
  141. * function as temporary storage of the extra power given
  142. * to the actors
  143. *
  144. * This function divides the total allocated power (@power_range)
  145. * fairly between the actors. It first tries to give each actor a
  146. * share of the @power_range according to how much power it requested
  147. * compared to the rest of the actors. For example, if only one actor
  148. * requests power, then it receives all the @power_range. If
  149. * three actors each requests 1mW, each receives a third of the
  150. * @power_range.
  151. *
  152. * If any actor received more than their maximum power, then that
  153. * surplus is re-divvied among the actors based on how far they are
  154. * from their respective maximums.
  155. *
  156. * Granted power for each actor is written to @granted_power, which
  157. * should've been allocated by the calling function.
  158. */
  159. static void divvy_up_power(u32 *req_power, u32 *max_power, int num_actors,
  160. u32 total_req_power, u32 power_range,
  161. u32 *granted_power, u32 *extra_actor_power)
  162. {
  163. u32 extra_power, capped_extra_power;
  164. int i;
  165. /*
  166. * Prevent division by 0 if none of the actors request power.
  167. */
  168. if (!total_req_power)
  169. total_req_power = 1;
  170. capped_extra_power = 0;
  171. extra_power = 0;
  172. for (i = 0; i < num_actors; i++) {
  173. u64 req_range = req_power[i] * power_range;
  174. granted_power[i] = DIV_ROUND_CLOSEST_ULL(req_range,
  175. total_req_power);
  176. if (granted_power[i] > max_power[i]) {
  177. extra_power += granted_power[i] - max_power[i];
  178. granted_power[i] = max_power[i];
  179. }
  180. extra_actor_power[i] = max_power[i] - granted_power[i];
  181. capped_extra_power += extra_actor_power[i];
  182. }
  183. if (!extra_power)
  184. return;
  185. /*
  186. * Re-divvy the reclaimed extra among actors based on
  187. * how far they are from the max
  188. */
  189. extra_power = min(extra_power, capped_extra_power);
  190. if (capped_extra_power > 0)
  191. for (i = 0; i < num_actors; i++)
  192. granted_power[i] += (extra_actor_power[i] *
  193. extra_power) / capped_extra_power;
  194. }
  195. static int allocate_power(struct thermal_zone_device *tz,
  196. int current_temp,
  197. int control_temp)
  198. {
  199. struct thermal_instance *instance;
  200. struct power_allocator_params *params = tz->governor_data;
  201. u32 *req_power, *max_power, *granted_power, *extra_actor_power;
  202. u32 *weighted_req_power;
  203. u32 total_req_power, max_allocatable_power, total_weighted_req_power;
  204. u32 total_granted_power, power_range;
  205. int i, num_actors, total_weight, ret = 0;
  206. int trip_max_desired_temperature = params->trip_max_desired_temperature;
  207. mutex_lock(&tz->lock);
  208. num_actors = 0;
  209. total_weight = 0;
  210. list_for_each_entry(instance, &tz->thermal_instances, tz_node) {
  211. if ((instance->trip == trip_max_desired_temperature) &&
  212. cdev_is_power_actor(instance->cdev)) {
  213. num_actors++;
  214. total_weight += instance->weight;
  215. }
  216. }
  217. /*
  218. * We need to allocate five arrays of the same size:
  219. * req_power, max_power, granted_power, extra_actor_power and
  220. * weighted_req_power. They are going to be needed until this
  221. * function returns. Allocate them all in one go to simplify
  222. * the allocation and deallocation logic.
  223. */
  224. BUILD_BUG_ON(sizeof(*req_power) != sizeof(*max_power));
  225. BUILD_BUG_ON(sizeof(*req_power) != sizeof(*granted_power));
  226. BUILD_BUG_ON(sizeof(*req_power) != sizeof(*extra_actor_power));
  227. BUILD_BUG_ON(sizeof(*req_power) != sizeof(*weighted_req_power));
  228. req_power = kcalloc(num_actors * 5, sizeof(*req_power), GFP_KERNEL);
  229. if (!req_power) {
  230. ret = -ENOMEM;
  231. goto unlock;
  232. }
  233. max_power = &req_power[num_actors];
  234. granted_power = &req_power[2 * num_actors];
  235. extra_actor_power = &req_power[3 * num_actors];
  236. weighted_req_power = &req_power[4 * num_actors];
  237. i = 0;
  238. total_weighted_req_power = 0;
  239. total_req_power = 0;
  240. max_allocatable_power = 0;
  241. list_for_each_entry(instance, &tz->thermal_instances, tz_node) {
  242. int weight;
  243. struct thermal_cooling_device *cdev = instance->cdev;
  244. if (instance->trip != trip_max_desired_temperature)
  245. continue;
  246. if (!cdev_is_power_actor(cdev))
  247. continue;
  248. if (cdev->ops->get_requested_power(cdev, tz, &req_power[i]))
  249. continue;
  250. if (!total_weight)
  251. weight = 1 << FRAC_BITS;
  252. else
  253. weight = instance->weight;
  254. weighted_req_power[i] = frac_to_int(weight * req_power[i]);
  255. if (power_actor_get_max_power(cdev, tz, &max_power[i]))
  256. continue;
  257. total_req_power += req_power[i];
  258. max_allocatable_power += max_power[i];
  259. total_weighted_req_power += weighted_req_power[i];
  260. i++;
  261. }
  262. power_range = pid_controller(tz, current_temp, control_temp,
  263. max_allocatable_power);
  264. divvy_up_power(weighted_req_power, max_power, num_actors,
  265. total_weighted_req_power, power_range, granted_power,
  266. extra_actor_power);
  267. total_granted_power = 0;
  268. i = 0;
  269. list_for_each_entry(instance, &tz->thermal_instances, tz_node) {
  270. if (instance->trip != trip_max_desired_temperature)
  271. continue;
  272. if (!cdev_is_power_actor(instance->cdev))
  273. continue;
  274. power_actor_set_power(instance->cdev, instance,
  275. granted_power[i]);
  276. total_granted_power += granted_power[i];
  277. i++;
  278. }
  279. trace_thermal_power_allocator(tz, req_power, total_req_power,
  280. granted_power, total_granted_power,
  281. num_actors, power_range,
  282. max_allocatable_power, current_temp,
  283. control_temp - current_temp);
  284. kfree(req_power);
  285. unlock:
  286. mutex_unlock(&tz->lock);
  287. return ret;
  288. }
  289. static int get_governor_trips(struct thermal_zone_device *tz,
  290. struct power_allocator_params *params)
  291. {
  292. int i, ret, last_passive;
  293. bool found_first_passive;
  294. found_first_passive = false;
  295. last_passive = -1;
  296. ret = -EINVAL;
  297. for (i = 0; i < tz->trips; i++) {
  298. enum thermal_trip_type type;
  299. ret = tz->ops->get_trip_type(tz, i, &type);
  300. if (ret)
  301. return ret;
  302. if (!found_first_passive) {
  303. if (type == THERMAL_TRIP_PASSIVE) {
  304. params->trip_switch_on = i;
  305. found_first_passive = true;
  306. }
  307. } else if (type == THERMAL_TRIP_PASSIVE) {
  308. last_passive = i;
  309. } else {
  310. break;
  311. }
  312. }
  313. if (last_passive != -1) {
  314. params->trip_max_desired_temperature = last_passive;
  315. ret = 0;
  316. } else {
  317. ret = -EINVAL;
  318. }
  319. return ret;
  320. }
  321. static void reset_pid_controller(struct power_allocator_params *params)
  322. {
  323. params->err_integral = 0;
  324. params->prev_err = 0;
  325. }
  326. static void allow_maximum_power(struct thermal_zone_device *tz)
  327. {
  328. struct thermal_instance *instance;
  329. struct power_allocator_params *params = tz->governor_data;
  330. list_for_each_entry(instance, &tz->thermal_instances, tz_node) {
  331. if ((instance->trip != params->trip_max_desired_temperature) ||
  332. (!cdev_is_power_actor(instance->cdev)))
  333. continue;
  334. instance->target = 0;
  335. instance->cdev->updated = false;
  336. thermal_cdev_update(instance->cdev);
  337. }
  338. }
  339. /**
  340. * power_allocator_bind() - bind the power_allocator governor to a thermal zone
  341. * @tz: thermal zone to bind it to
  342. *
  343. * Check that the thermal zone is valid for this governor, that is, it
  344. * has two thermal trips. If so, initialize the PID controller
  345. * parameters and bind it to the thermal zone.
  346. *
  347. * Return: 0 on success, -EINVAL if the trips were invalid or -ENOMEM
  348. * if we ran out of memory.
  349. */
  350. static int power_allocator_bind(struct thermal_zone_device *tz)
  351. {
  352. int ret;
  353. struct power_allocator_params *params;
  354. int switch_on_temp, control_temp;
  355. u32 temperature_threshold;
  356. if (!tz->tzp || !tz->tzp->sustainable_power) {
  357. dev_err(&tz->device,
  358. "power_allocator: missing sustainable_power\n");
  359. return -EINVAL;
  360. }
  361. params = kzalloc(sizeof(*params), GFP_KERNEL);
  362. if (!params)
  363. return -ENOMEM;
  364. ret = get_governor_trips(tz, params);
  365. if (ret) {
  366. dev_err(&tz->device,
  367. "thermal zone %s has wrong trip setup for power allocator\n",
  368. tz->type);
  369. goto free;
  370. }
  371. ret = tz->ops->get_trip_temp(tz, params->trip_switch_on,
  372. &switch_on_temp);
  373. if (ret)
  374. goto free;
  375. ret = tz->ops->get_trip_temp(tz, params->trip_max_desired_temperature,
  376. &control_temp);
  377. if (ret)
  378. goto free;
  379. temperature_threshold = control_temp - switch_on_temp;
  380. tz->tzp->k_po = tz->tzp->k_po ?:
  381. int_to_frac(tz->tzp->sustainable_power) / temperature_threshold;
  382. tz->tzp->k_pu = tz->tzp->k_pu ?:
  383. int_to_frac(2 * tz->tzp->sustainable_power) /
  384. temperature_threshold;
  385. tz->tzp->k_i = tz->tzp->k_i ?: int_to_frac(10) / 1000;
  386. /*
  387. * The default for k_d and integral_cutoff is 0, so we can
  388. * leave them as they are.
  389. */
  390. reset_pid_controller(params);
  391. tz->governor_data = params;
  392. return 0;
  393. free:
  394. kfree(params);
  395. return ret;
  396. }
  397. static void power_allocator_unbind(struct thermal_zone_device *tz)
  398. {
  399. dev_dbg(&tz->device, "Unbinding from thermal zone %d\n", tz->id);
  400. kfree(tz->governor_data);
  401. tz->governor_data = NULL;
  402. }
  403. static int power_allocator_throttle(struct thermal_zone_device *tz, int trip)
  404. {
  405. int ret;
  406. int switch_on_temp, control_temp, current_temp;
  407. struct power_allocator_params *params = tz->governor_data;
  408. /*
  409. * We get called for every trip point but we only need to do
  410. * our calculations once
  411. */
  412. if (trip != params->trip_max_desired_temperature)
  413. return 0;
  414. ret = thermal_zone_get_temp(tz, &current_temp);
  415. if (ret) {
  416. dev_warn(&tz->device, "Failed to get temperature: %d\n", ret);
  417. return ret;
  418. }
  419. ret = tz->ops->get_trip_temp(tz, params->trip_switch_on,
  420. &switch_on_temp);
  421. if (ret) {
  422. dev_warn(&tz->device,
  423. "Failed to get switch on temperature: %d\n", ret);
  424. return ret;
  425. }
  426. if (current_temp < switch_on_temp) {
  427. tz->passive = 0;
  428. reset_pid_controller(params);
  429. allow_maximum_power(tz);
  430. return 0;
  431. }
  432. tz->passive = 1;
  433. ret = tz->ops->get_trip_temp(tz, params->trip_max_desired_temperature,
  434. &control_temp);
  435. if (ret) {
  436. dev_warn(&tz->device,
  437. "Failed to get the maximum desired temperature: %d\n",
  438. ret);
  439. return ret;
  440. }
  441. return allocate_power(tz, current_temp, control_temp);
  442. }
  443. static struct thermal_governor thermal_gov_power_allocator = {
  444. .name = "power_allocator",
  445. .bind_to_tz = power_allocator_bind,
  446. .unbind_from_tz = power_allocator_unbind,
  447. .throttle = power_allocator_throttle,
  448. };
  449. int thermal_gov_power_allocator_register(void)
  450. {
  451. return thermal_register_governor(&thermal_gov_power_allocator);
  452. }
  453. void thermal_gov_power_allocator_unregister(void)
  454. {
  455. thermal_unregister_governor(&thermal_gov_power_allocator);
  456. }