power_allocator.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669
  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 INVALID_TRIP -1
  23. #define FRAC_BITS 10
  24. #define int_to_frac(x) ((x) << FRAC_BITS)
  25. #define frac_to_int(x) ((x) >> FRAC_BITS)
  26. /**
  27. * mul_frac() - multiply two fixed-point numbers
  28. * @x: first multiplicand
  29. * @y: second multiplicand
  30. *
  31. * Return: the result of multiplying two fixed-point numbers. The
  32. * result is also a fixed-point number.
  33. */
  34. static inline s64 mul_frac(s64 x, s64 y)
  35. {
  36. return (x * y) >> FRAC_BITS;
  37. }
  38. /**
  39. * div_frac() - divide two fixed-point numbers
  40. * @x: the dividend
  41. * @y: the divisor
  42. *
  43. * Return: the result of dividing two fixed-point numbers. The
  44. * result is also a fixed-point number.
  45. */
  46. static inline s64 div_frac(s64 x, s64 y)
  47. {
  48. return div_s64(x << FRAC_BITS, y);
  49. }
  50. /**
  51. * struct power_allocator_params - parameters for the power allocator governor
  52. * @allocated_tzp: whether we have allocated tzp for this thermal zone and
  53. * it needs to be freed on unbind
  54. * @err_integral: accumulated error in the PID controller.
  55. * @prev_err: error in the previous iteration of the PID controller.
  56. * Used to calculate the derivative term.
  57. * @trip_switch_on: first passive trip point of the thermal zone. The
  58. * governor switches on when this trip point is crossed.
  59. * If the thermal zone only has one passive trip point,
  60. * @trip_switch_on should be INVALID_TRIP.
  61. * @trip_max_desired_temperature: last passive trip point of the thermal
  62. * zone. The temperature we are
  63. * controlling for.
  64. */
  65. struct power_allocator_params {
  66. bool allocated_tzp;
  67. s64 err_integral;
  68. s32 prev_err;
  69. int trip_switch_on;
  70. int trip_max_desired_temperature;
  71. };
  72. /**
  73. * estimate_sustainable_power() - Estimate the sustainable power of a thermal zone
  74. * @tz: thermal zone we are operating in
  75. *
  76. * For thermal zones that don't provide a sustainable_power in their
  77. * thermal_zone_params, estimate one. Calculate it using the minimum
  78. * power of all the cooling devices as that gives a valid value that
  79. * can give some degree of functionality. For optimal performance of
  80. * this governor, provide a sustainable_power in the thermal zone's
  81. * thermal_zone_params.
  82. */
  83. static u32 estimate_sustainable_power(struct thermal_zone_device *tz)
  84. {
  85. u32 sustainable_power = 0;
  86. struct thermal_instance *instance;
  87. struct power_allocator_params *params = tz->governor_data;
  88. list_for_each_entry(instance, &tz->thermal_instances, tz_node) {
  89. struct thermal_cooling_device *cdev = instance->cdev;
  90. u32 min_power;
  91. if (instance->trip != params->trip_max_desired_temperature)
  92. continue;
  93. if (power_actor_get_min_power(cdev, tz, &min_power))
  94. continue;
  95. sustainable_power += min_power;
  96. }
  97. return sustainable_power;
  98. }
  99. /**
  100. * estimate_pid_constants() - Estimate the constants for the PID controller
  101. * @tz: thermal zone for which to estimate the constants
  102. * @sustainable_power: sustainable power for the thermal zone
  103. * @trip_switch_on: trip point number for the switch on temperature
  104. * @control_temp: target temperature for the power allocator governor
  105. * @force: whether to force the update of the constants
  106. *
  107. * This function is used to update the estimation of the PID
  108. * controller constants in struct thermal_zone_parameters.
  109. * Sustainable power is provided in case it was estimated. The
  110. * estimated sustainable_power should not be stored in the
  111. * thermal_zone_parameters so it has to be passed explicitly to this
  112. * function.
  113. *
  114. * If @force is not set, the values in the thermal zone's parameters
  115. * are preserved if they are not zero. If @force is set, the values
  116. * in thermal zone's parameters are overwritten.
  117. */
  118. static void estimate_pid_constants(struct thermal_zone_device *tz,
  119. u32 sustainable_power, int trip_switch_on,
  120. int control_temp, bool force)
  121. {
  122. int ret;
  123. int switch_on_temp;
  124. u32 temperature_threshold;
  125. ret = tz->ops->get_trip_temp(tz, trip_switch_on, &switch_on_temp);
  126. if (ret)
  127. switch_on_temp = 0;
  128. temperature_threshold = control_temp - switch_on_temp;
  129. /*
  130. * estimate_pid_constants() tries to find appropriate default
  131. * values for thermal zones that don't provide them. If a
  132. * system integrator has configured a thermal zone with two
  133. * passive trip points at the same temperature, that person
  134. * hasn't put any effort to set up the thermal zone properly
  135. * so just give up.
  136. */
  137. if (!temperature_threshold)
  138. return;
  139. if (!tz->tzp->k_po || force)
  140. tz->tzp->k_po = int_to_frac(sustainable_power) /
  141. temperature_threshold;
  142. if (!tz->tzp->k_pu || force)
  143. tz->tzp->k_pu = int_to_frac(2 * sustainable_power) /
  144. temperature_threshold;
  145. if (!tz->tzp->k_i || force)
  146. tz->tzp->k_i = int_to_frac(10) / 1000;
  147. /*
  148. * The default for k_d and integral_cutoff is 0, so we can
  149. * leave them as they are.
  150. */
  151. }
  152. /**
  153. * pid_controller() - PID controller
  154. * @tz: thermal zone we are operating in
  155. * @current_temp: the current temperature in millicelsius
  156. * @control_temp: the target temperature in millicelsius
  157. * @max_allocatable_power: maximum allocatable power for this thermal zone
  158. *
  159. * This PID controller increases the available power budget so that the
  160. * temperature of the thermal zone gets as close as possible to
  161. * @control_temp and limits the power if it exceeds it. k_po is the
  162. * proportional term when we are overshooting, k_pu is the
  163. * proportional term when we are undershooting. integral_cutoff is a
  164. * threshold below which we stop accumulating the error. The
  165. * accumulated error is only valid if the requested power will make
  166. * the system warmer. If the system is mostly idle, there's no point
  167. * in accumulating positive error.
  168. *
  169. * Return: The power budget for the next period.
  170. */
  171. static u32 pid_controller(struct thermal_zone_device *tz,
  172. int current_temp,
  173. int control_temp,
  174. u32 max_allocatable_power)
  175. {
  176. s64 p, i, d, power_range;
  177. s32 err, max_power_frac;
  178. u32 sustainable_power;
  179. struct power_allocator_params *params = tz->governor_data;
  180. max_power_frac = int_to_frac(max_allocatable_power);
  181. if (tz->tzp->sustainable_power) {
  182. sustainable_power = tz->tzp->sustainable_power;
  183. } else {
  184. sustainable_power = estimate_sustainable_power(tz);
  185. estimate_pid_constants(tz, sustainable_power,
  186. params->trip_switch_on, control_temp,
  187. true);
  188. }
  189. err = control_temp - current_temp;
  190. err = int_to_frac(err);
  191. /* Calculate the proportional term */
  192. p = mul_frac(err < 0 ? tz->tzp->k_po : tz->tzp->k_pu, err);
  193. /*
  194. * Calculate the integral term
  195. *
  196. * if the error is less than cut off allow integration (but
  197. * the integral is limited to max power)
  198. */
  199. i = mul_frac(tz->tzp->k_i, params->err_integral);
  200. if (err < int_to_frac(tz->tzp->integral_cutoff)) {
  201. s64 i_next = i + mul_frac(tz->tzp->k_i, err);
  202. if (abs64(i_next) < max_power_frac) {
  203. i = i_next;
  204. params->err_integral += err;
  205. }
  206. }
  207. /*
  208. * Calculate the derivative term
  209. *
  210. * We do err - prev_err, so with a positive k_d, a decreasing
  211. * error (i.e. driving closer to the line) results in less
  212. * power being applied, slowing down the controller)
  213. */
  214. d = mul_frac(tz->tzp->k_d, err - params->prev_err);
  215. d = div_frac(d, tz->passive_delay);
  216. params->prev_err = err;
  217. power_range = p + i + d;
  218. /* feed-forward the known sustainable dissipatable power */
  219. power_range = sustainable_power + frac_to_int(power_range);
  220. power_range = clamp(power_range, (s64)0, (s64)max_allocatable_power);
  221. trace_thermal_power_allocator_pid(tz, frac_to_int(err),
  222. frac_to_int(params->err_integral),
  223. frac_to_int(p), frac_to_int(i),
  224. frac_to_int(d), power_range);
  225. return power_range;
  226. }
  227. /**
  228. * divvy_up_power() - divvy the allocated power between the actors
  229. * @req_power: each actor's requested power
  230. * @max_power: each actor's maximum available power
  231. * @num_actors: size of the @req_power, @max_power and @granted_power's array
  232. * @total_req_power: sum of @req_power
  233. * @power_range: total allocated power
  234. * @granted_power: output array: each actor's granted power
  235. * @extra_actor_power: an appropriately sized array to be used in the
  236. * function as temporary storage of the extra power given
  237. * to the actors
  238. *
  239. * This function divides the total allocated power (@power_range)
  240. * fairly between the actors. It first tries to give each actor a
  241. * share of the @power_range according to how much power it requested
  242. * compared to the rest of the actors. For example, if only one actor
  243. * requests power, then it receives all the @power_range. If
  244. * three actors each requests 1mW, each receives a third of the
  245. * @power_range.
  246. *
  247. * If any actor received more than their maximum power, then that
  248. * surplus is re-divvied among the actors based on how far they are
  249. * from their respective maximums.
  250. *
  251. * Granted power for each actor is written to @granted_power, which
  252. * should've been allocated by the calling function.
  253. */
  254. static void divvy_up_power(u32 *req_power, u32 *max_power, int num_actors,
  255. u32 total_req_power, u32 power_range,
  256. u32 *granted_power, u32 *extra_actor_power)
  257. {
  258. u32 extra_power, capped_extra_power;
  259. int i;
  260. /*
  261. * Prevent division by 0 if none of the actors request power.
  262. */
  263. if (!total_req_power)
  264. total_req_power = 1;
  265. capped_extra_power = 0;
  266. extra_power = 0;
  267. for (i = 0; i < num_actors; i++) {
  268. u64 req_range = req_power[i] * power_range;
  269. granted_power[i] = DIV_ROUND_CLOSEST_ULL(req_range,
  270. total_req_power);
  271. if (granted_power[i] > max_power[i]) {
  272. extra_power += granted_power[i] - max_power[i];
  273. granted_power[i] = max_power[i];
  274. }
  275. extra_actor_power[i] = max_power[i] - granted_power[i];
  276. capped_extra_power += extra_actor_power[i];
  277. }
  278. if (!extra_power)
  279. return;
  280. /*
  281. * Re-divvy the reclaimed extra among actors based on
  282. * how far they are from the max
  283. */
  284. extra_power = min(extra_power, capped_extra_power);
  285. if (capped_extra_power > 0)
  286. for (i = 0; i < num_actors; i++)
  287. granted_power[i] += (extra_actor_power[i] *
  288. extra_power) / capped_extra_power;
  289. }
  290. static int allocate_power(struct thermal_zone_device *tz,
  291. int current_temp,
  292. int control_temp)
  293. {
  294. struct thermal_instance *instance;
  295. struct power_allocator_params *params = tz->governor_data;
  296. u32 *req_power, *max_power, *granted_power, *extra_actor_power;
  297. u32 *weighted_req_power;
  298. u32 total_req_power, max_allocatable_power, total_weighted_req_power;
  299. u32 total_granted_power, power_range;
  300. int i, num_actors, total_weight, ret = 0;
  301. int trip_max_desired_temperature = params->trip_max_desired_temperature;
  302. mutex_lock(&tz->lock);
  303. num_actors = 0;
  304. total_weight = 0;
  305. list_for_each_entry(instance, &tz->thermal_instances, tz_node) {
  306. if ((instance->trip == trip_max_desired_temperature) &&
  307. cdev_is_power_actor(instance->cdev)) {
  308. num_actors++;
  309. total_weight += instance->weight;
  310. }
  311. }
  312. if (!num_actors) {
  313. ret = -ENODEV;
  314. goto unlock;
  315. }
  316. /*
  317. * We need to allocate five arrays of the same size:
  318. * req_power, max_power, granted_power, extra_actor_power and
  319. * weighted_req_power. They are going to be needed until this
  320. * function returns. Allocate them all in one go to simplify
  321. * the allocation and deallocation logic.
  322. */
  323. BUILD_BUG_ON(sizeof(*req_power) != sizeof(*max_power));
  324. BUILD_BUG_ON(sizeof(*req_power) != sizeof(*granted_power));
  325. BUILD_BUG_ON(sizeof(*req_power) != sizeof(*extra_actor_power));
  326. BUILD_BUG_ON(sizeof(*req_power) != sizeof(*weighted_req_power));
  327. req_power = kcalloc(num_actors * 5, sizeof(*req_power), GFP_KERNEL);
  328. if (!req_power) {
  329. ret = -ENOMEM;
  330. goto unlock;
  331. }
  332. max_power = &req_power[num_actors];
  333. granted_power = &req_power[2 * num_actors];
  334. extra_actor_power = &req_power[3 * num_actors];
  335. weighted_req_power = &req_power[4 * num_actors];
  336. i = 0;
  337. total_weighted_req_power = 0;
  338. total_req_power = 0;
  339. max_allocatable_power = 0;
  340. list_for_each_entry(instance, &tz->thermal_instances, tz_node) {
  341. int weight;
  342. struct thermal_cooling_device *cdev = instance->cdev;
  343. if (instance->trip != trip_max_desired_temperature)
  344. continue;
  345. if (!cdev_is_power_actor(cdev))
  346. continue;
  347. if (cdev->ops->get_requested_power(cdev, tz, &req_power[i]))
  348. continue;
  349. if (!total_weight)
  350. weight = 1 << FRAC_BITS;
  351. else
  352. weight = instance->weight;
  353. weighted_req_power[i] = frac_to_int(weight * req_power[i]);
  354. if (power_actor_get_max_power(cdev, tz, &max_power[i]))
  355. continue;
  356. total_req_power += req_power[i];
  357. max_allocatable_power += max_power[i];
  358. total_weighted_req_power += weighted_req_power[i];
  359. i++;
  360. }
  361. power_range = pid_controller(tz, current_temp, control_temp,
  362. max_allocatable_power);
  363. divvy_up_power(weighted_req_power, max_power, num_actors,
  364. total_weighted_req_power, power_range, granted_power,
  365. extra_actor_power);
  366. total_granted_power = 0;
  367. i = 0;
  368. list_for_each_entry(instance, &tz->thermal_instances, tz_node) {
  369. if (instance->trip != trip_max_desired_temperature)
  370. continue;
  371. if (!cdev_is_power_actor(instance->cdev))
  372. continue;
  373. power_actor_set_power(instance->cdev, instance,
  374. granted_power[i]);
  375. total_granted_power += granted_power[i];
  376. i++;
  377. }
  378. trace_thermal_power_allocator(tz, req_power, total_req_power,
  379. granted_power, total_granted_power,
  380. num_actors, power_range,
  381. max_allocatable_power, current_temp,
  382. control_temp - current_temp);
  383. kfree(req_power);
  384. unlock:
  385. mutex_unlock(&tz->lock);
  386. return ret;
  387. }
  388. /**
  389. * get_governor_trips() - get the number of the two trip points that are key for this governor
  390. * @tz: thermal zone to operate on
  391. * @params: pointer to private data for this governor
  392. *
  393. * The power allocator governor works optimally with two trips points:
  394. * a "switch on" trip point and a "maximum desired temperature". These
  395. * are defined as the first and last passive trip points.
  396. *
  397. * If there is only one trip point, then that's considered to be the
  398. * "maximum desired temperature" trip point and the governor is always
  399. * on. If there are no passive or active trip points, then the
  400. * governor won't do anything. In fact, its throttle function
  401. * won't be called at all.
  402. */
  403. static void get_governor_trips(struct thermal_zone_device *tz,
  404. struct power_allocator_params *params)
  405. {
  406. int i, last_active, last_passive;
  407. bool found_first_passive;
  408. found_first_passive = false;
  409. last_active = INVALID_TRIP;
  410. last_passive = INVALID_TRIP;
  411. for (i = 0; i < tz->trips; i++) {
  412. enum thermal_trip_type type;
  413. int ret;
  414. ret = tz->ops->get_trip_type(tz, i, &type);
  415. if (ret) {
  416. dev_warn(&tz->device,
  417. "Failed to get trip point %d type: %d\n", i,
  418. ret);
  419. continue;
  420. }
  421. if (type == THERMAL_TRIP_PASSIVE) {
  422. if (!found_first_passive) {
  423. params->trip_switch_on = i;
  424. found_first_passive = true;
  425. } else {
  426. last_passive = i;
  427. }
  428. } else if (type == THERMAL_TRIP_ACTIVE) {
  429. last_active = i;
  430. } else {
  431. break;
  432. }
  433. }
  434. if (last_passive != INVALID_TRIP) {
  435. params->trip_max_desired_temperature = last_passive;
  436. } else if (found_first_passive) {
  437. params->trip_max_desired_temperature = params->trip_switch_on;
  438. params->trip_switch_on = INVALID_TRIP;
  439. } else {
  440. params->trip_switch_on = INVALID_TRIP;
  441. params->trip_max_desired_temperature = last_active;
  442. }
  443. }
  444. static void reset_pid_controller(struct power_allocator_params *params)
  445. {
  446. params->err_integral = 0;
  447. params->prev_err = 0;
  448. }
  449. static void allow_maximum_power(struct thermal_zone_device *tz)
  450. {
  451. struct thermal_instance *instance;
  452. struct power_allocator_params *params = tz->governor_data;
  453. list_for_each_entry(instance, &tz->thermal_instances, tz_node) {
  454. if ((instance->trip != params->trip_max_desired_temperature) ||
  455. (!cdev_is_power_actor(instance->cdev)))
  456. continue;
  457. instance->target = 0;
  458. instance->cdev->updated = false;
  459. thermal_cdev_update(instance->cdev);
  460. }
  461. }
  462. /**
  463. * power_allocator_bind() - bind the power_allocator governor to a thermal zone
  464. * @tz: thermal zone to bind it to
  465. *
  466. * Initialize the PID controller parameters and bind it to the thermal
  467. * zone.
  468. *
  469. * Return: 0 on success, or -ENOMEM if we ran out of memory.
  470. */
  471. static int power_allocator_bind(struct thermal_zone_device *tz)
  472. {
  473. int ret;
  474. struct power_allocator_params *params;
  475. int control_temp;
  476. params = kzalloc(sizeof(*params), GFP_KERNEL);
  477. if (!params)
  478. return -ENOMEM;
  479. if (!tz->tzp) {
  480. tz->tzp = kzalloc(sizeof(*tz->tzp), GFP_KERNEL);
  481. if (!tz->tzp) {
  482. ret = -ENOMEM;
  483. goto free_params;
  484. }
  485. params->allocated_tzp = true;
  486. }
  487. if (!tz->tzp->sustainable_power)
  488. dev_warn(&tz->device, "power_allocator: sustainable_power will be estimated\n");
  489. get_governor_trips(tz, params);
  490. if (tz->trips > 0) {
  491. ret = tz->ops->get_trip_temp(tz,
  492. params->trip_max_desired_temperature,
  493. &control_temp);
  494. if (!ret)
  495. estimate_pid_constants(tz, tz->tzp->sustainable_power,
  496. params->trip_switch_on,
  497. control_temp, false);
  498. }
  499. reset_pid_controller(params);
  500. tz->governor_data = params;
  501. return 0;
  502. free_params:
  503. kfree(params);
  504. return ret;
  505. }
  506. static void power_allocator_unbind(struct thermal_zone_device *tz)
  507. {
  508. struct power_allocator_params *params = tz->governor_data;
  509. dev_dbg(&tz->device, "Unbinding from thermal zone %d\n", tz->id);
  510. if (params->allocated_tzp) {
  511. kfree(tz->tzp);
  512. tz->tzp = NULL;
  513. }
  514. kfree(tz->governor_data);
  515. tz->governor_data = NULL;
  516. }
  517. static int power_allocator_throttle(struct thermal_zone_device *tz, int trip)
  518. {
  519. int ret;
  520. int switch_on_temp, control_temp, current_temp;
  521. struct power_allocator_params *params = tz->governor_data;
  522. /*
  523. * We get called for every trip point but we only need to do
  524. * our calculations once
  525. */
  526. if (trip != params->trip_max_desired_temperature)
  527. return 0;
  528. ret = thermal_zone_get_temp(tz, &current_temp);
  529. if (ret) {
  530. dev_warn(&tz->device, "Failed to get temperature: %d\n", ret);
  531. return ret;
  532. }
  533. ret = tz->ops->get_trip_temp(tz, params->trip_switch_on,
  534. &switch_on_temp);
  535. if (!ret && (current_temp < switch_on_temp)) {
  536. tz->passive = 0;
  537. reset_pid_controller(params);
  538. allow_maximum_power(tz);
  539. return 0;
  540. }
  541. tz->passive = 1;
  542. ret = tz->ops->get_trip_temp(tz, params->trip_max_desired_temperature,
  543. &control_temp);
  544. if (ret) {
  545. dev_warn(&tz->device,
  546. "Failed to get the maximum desired temperature: %d\n",
  547. ret);
  548. return ret;
  549. }
  550. return allocate_power(tz, current_temp, control_temp);
  551. }
  552. static struct thermal_governor thermal_gov_power_allocator = {
  553. .name = "power_allocator",
  554. .bind_to_tz = power_allocator_bind,
  555. .unbind_from_tz = power_allocator_unbind,
  556. .throttle = power_allocator_throttle,
  557. };
  558. int thermal_gov_power_allocator_register(void)
  559. {
  560. return thermal_register_governor(&thermal_gov_power_allocator);
  561. }
  562. void thermal_gov_power_allocator_unregister(void)
  563. {
  564. thermal_unregister_governor(&thermal_gov_power_allocator);
  565. }