gov_power_allocator.c 18 KB

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