power_allocator.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539
  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 total_req_power, max_allocatable_power;
  203. u32 total_granted_power, power_range;
  204. int i, num_actors, total_weight, ret = 0;
  205. int trip_max_desired_temperature = params->trip_max_desired_temperature;
  206. mutex_lock(&tz->lock);
  207. num_actors = 0;
  208. total_weight = 0;
  209. list_for_each_entry(instance, &tz->thermal_instances, tz_node) {
  210. if ((instance->trip == trip_max_desired_temperature) &&
  211. cdev_is_power_actor(instance->cdev)) {
  212. num_actors++;
  213. total_weight += instance->weight;
  214. }
  215. }
  216. /*
  217. * We need to allocate three arrays of the same size:
  218. * req_power, max_power and granted_power. They are going to
  219. * be needed until this function returns. Allocate them all
  220. * in one go to simplify the allocation and deallocation
  221. * logic.
  222. */
  223. BUILD_BUG_ON(sizeof(*req_power) != sizeof(*max_power));
  224. BUILD_BUG_ON(sizeof(*req_power) != sizeof(*granted_power));
  225. BUILD_BUG_ON(sizeof(*req_power) != sizeof(*extra_actor_power));
  226. req_power = devm_kcalloc(&tz->device, num_actors * 4,
  227. sizeof(*req_power), GFP_KERNEL);
  228. if (!req_power) {
  229. ret = -ENOMEM;
  230. goto unlock;
  231. }
  232. max_power = &req_power[num_actors];
  233. granted_power = &req_power[2 * num_actors];
  234. extra_actor_power = &req_power[3 * num_actors];
  235. i = 0;
  236. total_req_power = 0;
  237. max_allocatable_power = 0;
  238. list_for_each_entry(instance, &tz->thermal_instances, tz_node) {
  239. int weight;
  240. struct thermal_cooling_device *cdev = instance->cdev;
  241. if (instance->trip != trip_max_desired_temperature)
  242. continue;
  243. if (!cdev_is_power_actor(cdev))
  244. continue;
  245. if (cdev->ops->get_requested_power(cdev, tz, &req_power[i]))
  246. continue;
  247. if (!total_weight)
  248. weight = 1 << FRAC_BITS;
  249. else
  250. weight = instance->weight;
  251. req_power[i] = frac_to_int(weight * req_power[i]);
  252. if (power_actor_get_max_power(cdev, tz, &max_power[i]))
  253. continue;
  254. total_req_power += req_power[i];
  255. max_allocatable_power += max_power[i];
  256. i++;
  257. }
  258. power_range = pid_controller(tz, current_temp, control_temp,
  259. max_allocatable_power);
  260. divvy_up_power(req_power, max_power, num_actors, total_req_power,
  261. power_range, granted_power, extra_actor_power);
  262. total_granted_power = 0;
  263. i = 0;
  264. list_for_each_entry(instance, &tz->thermal_instances, tz_node) {
  265. if (instance->trip != trip_max_desired_temperature)
  266. continue;
  267. if (!cdev_is_power_actor(instance->cdev))
  268. continue;
  269. power_actor_set_power(instance->cdev, instance,
  270. granted_power[i]);
  271. total_granted_power += granted_power[i];
  272. i++;
  273. }
  274. trace_thermal_power_allocator(tz, req_power, total_req_power,
  275. granted_power, total_granted_power,
  276. num_actors, power_range,
  277. max_allocatable_power, current_temp,
  278. control_temp - current_temp);
  279. devm_kfree(&tz->device, req_power);
  280. unlock:
  281. mutex_unlock(&tz->lock);
  282. return ret;
  283. }
  284. static int get_governor_trips(struct thermal_zone_device *tz,
  285. struct power_allocator_params *params)
  286. {
  287. int i, ret, last_passive;
  288. bool found_first_passive;
  289. found_first_passive = false;
  290. last_passive = -1;
  291. ret = -EINVAL;
  292. for (i = 0; i < tz->trips; i++) {
  293. enum thermal_trip_type type;
  294. ret = tz->ops->get_trip_type(tz, i, &type);
  295. if (ret)
  296. return ret;
  297. if (!found_first_passive) {
  298. if (type == THERMAL_TRIP_PASSIVE) {
  299. params->trip_switch_on = i;
  300. found_first_passive = true;
  301. }
  302. } else if (type == THERMAL_TRIP_PASSIVE) {
  303. last_passive = i;
  304. } else {
  305. break;
  306. }
  307. }
  308. if (last_passive != -1) {
  309. params->trip_max_desired_temperature = last_passive;
  310. ret = 0;
  311. } else {
  312. ret = -EINVAL;
  313. }
  314. return ret;
  315. }
  316. static void reset_pid_controller(struct power_allocator_params *params)
  317. {
  318. params->err_integral = 0;
  319. params->prev_err = 0;
  320. }
  321. static void allow_maximum_power(struct thermal_zone_device *tz)
  322. {
  323. struct thermal_instance *instance;
  324. struct power_allocator_params *params = tz->governor_data;
  325. list_for_each_entry(instance, &tz->thermal_instances, tz_node) {
  326. if ((instance->trip != params->trip_max_desired_temperature) ||
  327. (!cdev_is_power_actor(instance->cdev)))
  328. continue;
  329. instance->target = 0;
  330. instance->cdev->updated = false;
  331. thermal_cdev_update(instance->cdev);
  332. }
  333. }
  334. /**
  335. * power_allocator_bind() - bind the power_allocator governor to a thermal zone
  336. * @tz: thermal zone to bind it to
  337. *
  338. * Check that the thermal zone is valid for this governor, that is, it
  339. * has two thermal trips. If so, initialize the PID controller
  340. * parameters and bind it to the thermal zone.
  341. *
  342. * Return: 0 on success, -EINVAL if the trips were invalid or -ENOMEM
  343. * if we ran out of memory.
  344. */
  345. static int power_allocator_bind(struct thermal_zone_device *tz)
  346. {
  347. int ret;
  348. struct power_allocator_params *params;
  349. int switch_on_temp, control_temp;
  350. u32 temperature_threshold;
  351. if (!tz->tzp || !tz->tzp->sustainable_power) {
  352. dev_err(&tz->device,
  353. "power_allocator: missing sustainable_power\n");
  354. return -EINVAL;
  355. }
  356. params = devm_kzalloc(&tz->device, sizeof(*params), GFP_KERNEL);
  357. if (!params)
  358. return -ENOMEM;
  359. ret = get_governor_trips(tz, params);
  360. if (ret) {
  361. dev_err(&tz->device,
  362. "thermal zone %s has wrong trip setup for power allocator\n",
  363. tz->type);
  364. goto free;
  365. }
  366. ret = tz->ops->get_trip_temp(tz, params->trip_switch_on,
  367. &switch_on_temp);
  368. if (ret)
  369. goto free;
  370. ret = tz->ops->get_trip_temp(tz, params->trip_max_desired_temperature,
  371. &control_temp);
  372. if (ret)
  373. goto free;
  374. temperature_threshold = control_temp - switch_on_temp;
  375. tz->tzp->k_po = tz->tzp->k_po ?:
  376. int_to_frac(tz->tzp->sustainable_power) / temperature_threshold;
  377. tz->tzp->k_pu = tz->tzp->k_pu ?:
  378. int_to_frac(2 * tz->tzp->sustainable_power) /
  379. temperature_threshold;
  380. tz->tzp->k_i = tz->tzp->k_i ?: int_to_frac(10) / 1000;
  381. /*
  382. * The default for k_d and integral_cutoff is 0, so we can
  383. * leave them as they are.
  384. */
  385. reset_pid_controller(params);
  386. tz->governor_data = params;
  387. return 0;
  388. free:
  389. devm_kfree(&tz->device, params);
  390. return ret;
  391. }
  392. static void power_allocator_unbind(struct thermal_zone_device *tz)
  393. {
  394. dev_dbg(&tz->device, "Unbinding from thermal zone %d\n", tz->id);
  395. devm_kfree(&tz->device, tz->governor_data);
  396. tz->governor_data = NULL;
  397. }
  398. static int power_allocator_throttle(struct thermal_zone_device *tz, int trip)
  399. {
  400. int ret;
  401. int switch_on_temp, control_temp, current_temp;
  402. struct power_allocator_params *params = tz->governor_data;
  403. /*
  404. * We get called for every trip point but we only need to do
  405. * our calculations once
  406. */
  407. if (trip != params->trip_max_desired_temperature)
  408. return 0;
  409. ret = thermal_zone_get_temp(tz, &current_temp);
  410. if (ret) {
  411. dev_warn(&tz->device, "Failed to get temperature: %d\n", ret);
  412. return ret;
  413. }
  414. ret = tz->ops->get_trip_temp(tz, params->trip_switch_on,
  415. &switch_on_temp);
  416. if (ret) {
  417. dev_warn(&tz->device,
  418. "Failed to get switch on temperature: %d\n", ret);
  419. return ret;
  420. }
  421. if (current_temp < switch_on_temp) {
  422. tz->passive = 0;
  423. reset_pid_controller(params);
  424. allow_maximum_power(tz);
  425. return 0;
  426. }
  427. tz->passive = 1;
  428. ret = tz->ops->get_trip_temp(tz, params->trip_max_desired_temperature,
  429. &control_temp);
  430. if (ret) {
  431. dev_warn(&tz->device,
  432. "Failed to get the maximum desired temperature: %d\n",
  433. ret);
  434. return ret;
  435. }
  436. return allocate_power(tz, current_temp, control_temp);
  437. }
  438. static struct thermal_governor thermal_gov_power_allocator = {
  439. .name = "power_allocator",
  440. .bind_to_tz = power_allocator_bind,
  441. .unbind_from_tz = power_allocator_unbind,
  442. .throttle = power_allocator_throttle,
  443. };
  444. int thermal_gov_power_allocator_register(void)
  445. {
  446. return thermal_register_governor(&thermal_gov_power_allocator);
  447. }
  448. void thermal_gov_power_allocator_unregister(void)
  449. {
  450. thermal_unregister_governor(&thermal_gov_power_allocator);
  451. }