power_allocator.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661
  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. * @control_temp: the target temperature in millicelsius
  156. * @max_allocatable_power: maximum allocatable power for this thermal zone
  157. *
  158. * This PID controller increases the available power budget so that the
  159. * temperature of the thermal zone gets as close as possible to
  160. * @control_temp and limits the power if it exceeds it. k_po is the
  161. * proportional term when we are overshooting, k_pu is the
  162. * proportional term when we are undershooting. integral_cutoff is a
  163. * threshold below which we stop accumulating the error. The
  164. * accumulated error is only valid if the requested power will make
  165. * the system warmer. If the system is mostly idle, there's no point
  166. * in accumulating positive error.
  167. *
  168. * Return: The power budget for the next period.
  169. */
  170. static u32 pid_controller(struct thermal_zone_device *tz,
  171. int control_temp,
  172. u32 max_allocatable_power)
  173. {
  174. s64 p, i, d, power_range;
  175. s32 err, max_power_frac;
  176. u32 sustainable_power;
  177. struct power_allocator_params *params = tz->governor_data;
  178. max_power_frac = int_to_frac(max_allocatable_power);
  179. if (tz->tzp->sustainable_power) {
  180. sustainable_power = tz->tzp->sustainable_power;
  181. } else {
  182. sustainable_power = estimate_sustainable_power(tz);
  183. estimate_pid_constants(tz, sustainable_power,
  184. params->trip_switch_on, control_temp,
  185. true);
  186. }
  187. err = control_temp - tz->temperature;
  188. err = int_to_frac(err);
  189. /* Calculate the proportional term */
  190. p = mul_frac(err < 0 ? tz->tzp->k_po : tz->tzp->k_pu, err);
  191. /*
  192. * Calculate the integral term
  193. *
  194. * if the error is less than cut off allow integration (but
  195. * the integral is limited to max power)
  196. */
  197. i = mul_frac(tz->tzp->k_i, params->err_integral);
  198. if (err < int_to_frac(tz->tzp->integral_cutoff)) {
  199. s64 i_next = i + mul_frac(tz->tzp->k_i, err);
  200. if (abs(i_next) < max_power_frac) {
  201. i = i_next;
  202. params->err_integral += err;
  203. }
  204. }
  205. /*
  206. * Calculate the derivative term
  207. *
  208. * We do err - prev_err, so with a positive k_d, a decreasing
  209. * error (i.e. driving closer to the line) results in less
  210. * power being applied, slowing down the controller)
  211. */
  212. d = mul_frac(tz->tzp->k_d, err - params->prev_err);
  213. d = div_frac(d, tz->passive_delay);
  214. params->prev_err = err;
  215. power_range = p + i + d;
  216. /* feed-forward the known sustainable dissipatable power */
  217. power_range = sustainable_power + frac_to_int(power_range);
  218. power_range = clamp(power_range, (s64)0, (s64)max_allocatable_power);
  219. trace_thermal_power_allocator_pid(tz, frac_to_int(err),
  220. frac_to_int(params->err_integral),
  221. frac_to_int(p), frac_to_int(i),
  222. frac_to_int(d), power_range);
  223. return power_range;
  224. }
  225. /**
  226. * divvy_up_power() - divvy the allocated power between the actors
  227. * @req_power: each actor's requested power
  228. * @max_power: each actor's maximum available power
  229. * @num_actors: size of the @req_power, @max_power and @granted_power's array
  230. * @total_req_power: sum of @req_power
  231. * @power_range: total allocated power
  232. * @granted_power: output array: each actor's granted power
  233. * @extra_actor_power: an appropriately sized array to be used in the
  234. * function as temporary storage of the extra power given
  235. * to the actors
  236. *
  237. * This function divides the total allocated power (@power_range)
  238. * fairly between the actors. It first tries to give each actor a
  239. * share of the @power_range according to how much power it requested
  240. * compared to the rest of the actors. For example, if only one actor
  241. * requests power, then it receives all the @power_range. If
  242. * three actors each requests 1mW, each receives a third of the
  243. * @power_range.
  244. *
  245. * If any actor received more than their maximum power, then that
  246. * surplus is re-divvied among the actors based on how far they are
  247. * from their respective maximums.
  248. *
  249. * Granted power for each actor is written to @granted_power, which
  250. * should've been allocated by the calling function.
  251. */
  252. static void divvy_up_power(u32 *req_power, u32 *max_power, int num_actors,
  253. u32 total_req_power, u32 power_range,
  254. u32 *granted_power, u32 *extra_actor_power)
  255. {
  256. u32 extra_power, capped_extra_power;
  257. int i;
  258. /*
  259. * Prevent division by 0 if none of the actors request power.
  260. */
  261. if (!total_req_power)
  262. total_req_power = 1;
  263. capped_extra_power = 0;
  264. extra_power = 0;
  265. for (i = 0; i < num_actors; i++) {
  266. u64 req_range = req_power[i] * power_range;
  267. granted_power[i] = DIV_ROUND_CLOSEST_ULL(req_range,
  268. total_req_power);
  269. if (granted_power[i] > max_power[i]) {
  270. extra_power += granted_power[i] - max_power[i];
  271. granted_power[i] = max_power[i];
  272. }
  273. extra_actor_power[i] = max_power[i] - granted_power[i];
  274. capped_extra_power += extra_actor_power[i];
  275. }
  276. if (!extra_power)
  277. return;
  278. /*
  279. * Re-divvy the reclaimed extra among actors based on
  280. * how far they are from the max
  281. */
  282. extra_power = min(extra_power, capped_extra_power);
  283. if (capped_extra_power > 0)
  284. for (i = 0; i < num_actors; i++)
  285. granted_power[i] += (extra_actor_power[i] *
  286. extra_power) / capped_extra_power;
  287. }
  288. static int allocate_power(struct thermal_zone_device *tz,
  289. int control_temp)
  290. {
  291. struct thermal_instance *instance;
  292. struct power_allocator_params *params = tz->governor_data;
  293. u32 *req_power, *max_power, *granted_power, *extra_actor_power;
  294. u32 *weighted_req_power;
  295. u32 total_req_power, max_allocatable_power, total_weighted_req_power;
  296. u32 total_granted_power, power_range;
  297. int i, num_actors, total_weight, ret = 0;
  298. int trip_max_desired_temperature = params->trip_max_desired_temperature;
  299. mutex_lock(&tz->lock);
  300. num_actors = 0;
  301. total_weight = 0;
  302. list_for_each_entry(instance, &tz->thermal_instances, tz_node) {
  303. if ((instance->trip == trip_max_desired_temperature) &&
  304. cdev_is_power_actor(instance->cdev)) {
  305. num_actors++;
  306. total_weight += instance->weight;
  307. }
  308. }
  309. if (!num_actors) {
  310. ret = -ENODEV;
  311. goto unlock;
  312. }
  313. /*
  314. * We need to allocate five arrays of the same size:
  315. * req_power, max_power, granted_power, extra_actor_power and
  316. * weighted_req_power. They are going to be needed until this
  317. * function returns. Allocate them all in one go to simplify
  318. * the allocation and deallocation logic.
  319. */
  320. BUILD_BUG_ON(sizeof(*req_power) != sizeof(*max_power));
  321. BUILD_BUG_ON(sizeof(*req_power) != sizeof(*granted_power));
  322. BUILD_BUG_ON(sizeof(*req_power) != sizeof(*extra_actor_power));
  323. BUILD_BUG_ON(sizeof(*req_power) != sizeof(*weighted_req_power));
  324. req_power = kcalloc(num_actors * 5, sizeof(*req_power), GFP_KERNEL);
  325. if (!req_power) {
  326. ret = -ENOMEM;
  327. goto unlock;
  328. }
  329. max_power = &req_power[num_actors];
  330. granted_power = &req_power[2 * num_actors];
  331. extra_actor_power = &req_power[3 * num_actors];
  332. weighted_req_power = &req_power[4 * num_actors];
  333. i = 0;
  334. total_weighted_req_power = 0;
  335. total_req_power = 0;
  336. max_allocatable_power = 0;
  337. list_for_each_entry(instance, &tz->thermal_instances, tz_node) {
  338. int weight;
  339. struct thermal_cooling_device *cdev = instance->cdev;
  340. if (instance->trip != trip_max_desired_temperature)
  341. continue;
  342. if (!cdev_is_power_actor(cdev))
  343. continue;
  344. if (cdev->ops->get_requested_power(cdev, tz, &req_power[i]))
  345. continue;
  346. if (!total_weight)
  347. weight = 1 << FRAC_BITS;
  348. else
  349. weight = instance->weight;
  350. weighted_req_power[i] = frac_to_int(weight * req_power[i]);
  351. if (power_actor_get_max_power(cdev, tz, &max_power[i]))
  352. continue;
  353. total_req_power += req_power[i];
  354. max_allocatable_power += max_power[i];
  355. total_weighted_req_power += weighted_req_power[i];
  356. i++;
  357. }
  358. power_range = pid_controller(tz, control_temp, max_allocatable_power);
  359. divvy_up_power(weighted_req_power, max_power, num_actors,
  360. total_weighted_req_power, power_range, granted_power,
  361. extra_actor_power);
  362. total_granted_power = 0;
  363. i = 0;
  364. list_for_each_entry(instance, &tz->thermal_instances, tz_node) {
  365. if (instance->trip != trip_max_desired_temperature)
  366. continue;
  367. if (!cdev_is_power_actor(instance->cdev))
  368. continue;
  369. power_actor_set_power(instance->cdev, instance,
  370. granted_power[i]);
  371. total_granted_power += granted_power[i];
  372. i++;
  373. }
  374. trace_thermal_power_allocator(tz, req_power, total_req_power,
  375. granted_power, total_granted_power,
  376. num_actors, power_range,
  377. max_allocatable_power, tz->temperature,
  378. control_temp - tz->temperature);
  379. kfree(req_power);
  380. unlock:
  381. mutex_unlock(&tz->lock);
  382. return ret;
  383. }
  384. /**
  385. * get_governor_trips() - get the number of the two trip points that are key for this governor
  386. * @tz: thermal zone to operate on
  387. * @params: pointer to private data for this governor
  388. *
  389. * The power allocator governor works optimally with two trips points:
  390. * a "switch on" trip point and a "maximum desired temperature". These
  391. * are defined as the first and last passive trip points.
  392. *
  393. * If there is only one trip point, then that's considered to be the
  394. * "maximum desired temperature" trip point and the governor is always
  395. * on. If there are no passive or active trip points, then the
  396. * governor won't do anything. In fact, its throttle function
  397. * won't be called at all.
  398. */
  399. static void get_governor_trips(struct thermal_zone_device *tz,
  400. struct power_allocator_params *params)
  401. {
  402. int i, last_active, last_passive;
  403. bool found_first_passive;
  404. found_first_passive = false;
  405. last_active = INVALID_TRIP;
  406. last_passive = INVALID_TRIP;
  407. for (i = 0; i < tz->trips; i++) {
  408. enum thermal_trip_type type;
  409. int ret;
  410. ret = tz->ops->get_trip_type(tz, i, &type);
  411. if (ret) {
  412. dev_warn(&tz->device,
  413. "Failed to get trip point %d type: %d\n", i,
  414. ret);
  415. continue;
  416. }
  417. if (type == THERMAL_TRIP_PASSIVE) {
  418. if (!found_first_passive) {
  419. params->trip_switch_on = i;
  420. found_first_passive = true;
  421. } else {
  422. last_passive = i;
  423. }
  424. } else if (type == THERMAL_TRIP_ACTIVE) {
  425. last_active = i;
  426. } else {
  427. break;
  428. }
  429. }
  430. if (last_passive != INVALID_TRIP) {
  431. params->trip_max_desired_temperature = last_passive;
  432. } else if (found_first_passive) {
  433. params->trip_max_desired_temperature = params->trip_switch_on;
  434. params->trip_switch_on = INVALID_TRIP;
  435. } else {
  436. params->trip_switch_on = INVALID_TRIP;
  437. params->trip_max_desired_temperature = last_active;
  438. }
  439. }
  440. static void reset_pid_controller(struct power_allocator_params *params)
  441. {
  442. params->err_integral = 0;
  443. params->prev_err = 0;
  444. }
  445. static void allow_maximum_power(struct thermal_zone_device *tz)
  446. {
  447. struct thermal_instance *instance;
  448. struct power_allocator_params *params = tz->governor_data;
  449. mutex_lock(&tz->lock);
  450. list_for_each_entry(instance, &tz->thermal_instances, tz_node) {
  451. if ((instance->trip != params->trip_max_desired_temperature) ||
  452. (!cdev_is_power_actor(instance->cdev)))
  453. continue;
  454. instance->target = 0;
  455. instance->cdev->updated = false;
  456. thermal_cdev_update(instance->cdev);
  457. }
  458. mutex_unlock(&tz->lock);
  459. }
  460. /**
  461. * power_allocator_bind() - bind the power_allocator governor to a thermal zone
  462. * @tz: thermal zone to bind it to
  463. *
  464. * Initialize the PID controller parameters and bind it to the thermal
  465. * zone.
  466. *
  467. * Return: 0 on success, or -ENOMEM if we ran out of memory.
  468. */
  469. static int power_allocator_bind(struct thermal_zone_device *tz)
  470. {
  471. int ret;
  472. struct power_allocator_params *params;
  473. int control_temp;
  474. params = kzalloc(sizeof(*params), GFP_KERNEL);
  475. if (!params)
  476. return -ENOMEM;
  477. if (!tz->tzp) {
  478. tz->tzp = kzalloc(sizeof(*tz->tzp), GFP_KERNEL);
  479. if (!tz->tzp) {
  480. ret = -ENOMEM;
  481. goto free_params;
  482. }
  483. params->allocated_tzp = true;
  484. }
  485. if (!tz->tzp->sustainable_power)
  486. dev_warn(&tz->device, "power_allocator: sustainable_power will be estimated\n");
  487. get_governor_trips(tz, params);
  488. if (tz->trips > 0) {
  489. ret = tz->ops->get_trip_temp(tz,
  490. params->trip_max_desired_temperature,
  491. &control_temp);
  492. if (!ret)
  493. estimate_pid_constants(tz, tz->tzp->sustainable_power,
  494. params->trip_switch_on,
  495. control_temp, false);
  496. }
  497. reset_pid_controller(params);
  498. tz->governor_data = params;
  499. return 0;
  500. free_params:
  501. kfree(params);
  502. return ret;
  503. }
  504. static void power_allocator_unbind(struct thermal_zone_device *tz)
  505. {
  506. struct power_allocator_params *params = tz->governor_data;
  507. dev_dbg(&tz->device, "Unbinding from thermal zone %d\n", tz->id);
  508. if (params->allocated_tzp) {
  509. kfree(tz->tzp);
  510. tz->tzp = NULL;
  511. }
  512. kfree(tz->governor_data);
  513. tz->governor_data = NULL;
  514. }
  515. static int power_allocator_throttle(struct thermal_zone_device *tz, int trip)
  516. {
  517. int ret;
  518. int switch_on_temp, control_temp;
  519. struct power_allocator_params *params = tz->governor_data;
  520. /*
  521. * We get called for every trip point but we only need to do
  522. * our calculations once
  523. */
  524. if (trip != params->trip_max_desired_temperature)
  525. return 0;
  526. ret = tz->ops->get_trip_temp(tz, params->trip_switch_on,
  527. &switch_on_temp);
  528. if (!ret && (tz->temperature < switch_on_temp)) {
  529. tz->passive = 0;
  530. reset_pid_controller(params);
  531. allow_maximum_power(tz);
  532. return 0;
  533. }
  534. tz->passive = 1;
  535. ret = tz->ops->get_trip_temp(tz, params->trip_max_desired_temperature,
  536. &control_temp);
  537. if (ret) {
  538. dev_warn(&tz->device,
  539. "Failed to get the maximum desired temperature: %d\n",
  540. ret);
  541. return ret;
  542. }
  543. return allocate_power(tz, control_temp);
  544. }
  545. static struct thermal_governor thermal_gov_power_allocator = {
  546. .name = "power_allocator",
  547. .bind_to_tz = power_allocator_bind,
  548. .unbind_from_tz = power_allocator_unbind,
  549. .throttle = power_allocator_throttle,
  550. };
  551. int thermal_gov_power_allocator_register(void)
  552. {
  553. return thermal_register_governor(&thermal_gov_power_allocator);
  554. }
  555. void thermal_gov_power_allocator_unregister(void)
  556. {
  557. thermal_unregister_governor(&thermal_gov_power_allocator);
  558. }