gov_fair_share.c 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * fair_share.c - A simple weight based Thermal governor
  4. *
  5. * Copyright (C) 2012 Intel Corp
  6. * Copyright (C) 2012 Durgadoss R <[email protected]>
  7. *
  8. * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  9. *
  10. * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  11. */
  12. #include <linux/thermal.h>
  13. #include <trace/events/thermal.h>
  14. #include "thermal_core.h"
  15. /**
  16. * get_trip_level: - obtains the current trip level for a zone
  17. * @tz: thermal zone device
  18. */
  19. static int get_trip_level(struct thermal_zone_device *tz)
  20. {
  21. int count = 0;
  22. int trip_temp;
  23. enum thermal_trip_type trip_type;
  24. if (tz->num_trips == 0 || !tz->ops->get_trip_temp)
  25. return 0;
  26. for (count = 0; count < tz->num_trips; count++) {
  27. tz->ops->get_trip_temp(tz, count, &trip_temp);
  28. if (tz->temperature < trip_temp)
  29. break;
  30. }
  31. /*
  32. * count > 0 only if temperature is greater than first trip
  33. * point, in which case, trip_point = count - 1
  34. */
  35. if (count > 0) {
  36. tz->ops->get_trip_type(tz, count - 1, &trip_type);
  37. trace_thermal_zone_trip(tz, count - 1, trip_type);
  38. }
  39. return count;
  40. }
  41. static long get_target_state(struct thermal_zone_device *tz,
  42. struct thermal_cooling_device *cdev, int percentage, int level)
  43. {
  44. return (long)(percentage * level * cdev->max_state) / (100 * tz->num_trips);
  45. }
  46. /**
  47. * fair_share_throttle - throttles devices associated with the given zone
  48. * @tz: thermal_zone_device
  49. * @trip: trip point index
  50. *
  51. * Throttling Logic: This uses three parameters to calculate the new
  52. * throttle state of the cooling devices associated with the given zone.
  53. *
  54. * Parameters used for Throttling:
  55. * P1. max_state: Maximum throttle state exposed by the cooling device.
  56. * P2. percentage[i]/100:
  57. * How 'effective' the 'i'th device is, in cooling the given zone.
  58. * P3. cur_trip_level/max_no_of_trips:
  59. * This describes the extent to which the devices should be throttled.
  60. * We do not want to throttle too much when we trip a lower temperature,
  61. * whereas the throttling is at full swing if we trip critical levels.
  62. * (Heavily assumes the trip points are in ascending order)
  63. * new_state of cooling device = P3 * P2 * P1
  64. */
  65. static int fair_share_throttle(struct thermal_zone_device *tz, int trip)
  66. {
  67. struct thermal_instance *instance;
  68. int total_weight = 0;
  69. int total_instance = 0;
  70. int cur_trip_level = get_trip_level(tz);
  71. lockdep_assert_held(&tz->lock);
  72. list_for_each_entry(instance, &tz->thermal_instances, tz_node) {
  73. if (instance->trip != trip)
  74. continue;
  75. total_weight += instance->weight;
  76. total_instance++;
  77. }
  78. list_for_each_entry(instance, &tz->thermal_instances, tz_node) {
  79. int percentage;
  80. struct thermal_cooling_device *cdev = instance->cdev;
  81. if (instance->trip != trip)
  82. continue;
  83. if (!total_weight)
  84. percentage = 100 / total_instance;
  85. else
  86. percentage = (instance->weight * 100) / total_weight;
  87. instance->target = get_target_state(tz, cdev, percentage,
  88. cur_trip_level);
  89. mutex_lock(&cdev->lock);
  90. __thermal_cdev_update(cdev);
  91. mutex_unlock(&cdev->lock);
  92. }
  93. return 0;
  94. }
  95. static struct thermal_governor thermal_gov_fair_share = {
  96. .name = "fair_share",
  97. .throttle = fair_share_throttle,
  98. };
  99. THERMAL_GOVERNOR_DECLARE(thermal_gov_fair_share);