gov_power_allocator.c 22 KB

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