gov_power_allocator.c 21 KB

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