fair_share.c 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  1. /*
  2. * fair_share.c - A simple weight based Thermal governor
  3. *
  4. * Copyright (C) 2012 Intel Corp
  5. * Copyright (C) 2012 Durgadoss R <durgadoss.r@intel.com>
  6. *
  7. * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  8. *
  9. * This program is free software; you can redistribute it and/or modify
  10. * it under the terms of the GNU General Public License as published by
  11. * the Free Software Foundation; version 2 of the License.
  12. *
  13. * This program is distributed in the hope that it will be useful, but
  14. * WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  16. * General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU General Public License along
  19. * with this program; if not, write to the Free Software Foundation, Inc.,
  20. * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
  21. *
  22. * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  23. */
  24. #include <linux/thermal.h>
  25. #include <trace/events/thermal.h>
  26. #include "thermal_core.h"
  27. /**
  28. * get_trip_level: - obtains the current trip level for a zone
  29. * @tz: thermal zone device
  30. */
  31. static int get_trip_level(struct thermal_zone_device *tz)
  32. {
  33. int count = 0;
  34. int trip_temp;
  35. enum thermal_trip_type trip_type;
  36. if (tz->trips == 0 || !tz->ops->get_trip_temp)
  37. return 0;
  38. for (count = 0; count < tz->trips; count++) {
  39. tz->ops->get_trip_temp(tz, count, &trip_temp);
  40. if (tz->temperature < trip_temp)
  41. break;
  42. }
  43. /*
  44. * count > 0 only if temperature is greater than first trip
  45. * point, in which case, trip_point = count - 1
  46. */
  47. if (count > 0) {
  48. tz->ops->get_trip_type(tz, count - 1, &trip_type);
  49. trace_thermal_zone_trip(tz, count - 1, trip_type);
  50. }
  51. return count;
  52. }
  53. static long get_target_state(struct thermal_zone_device *tz,
  54. struct thermal_cooling_device *cdev, int percentage, int level)
  55. {
  56. unsigned long max_state;
  57. cdev->ops->get_max_state(cdev, &max_state);
  58. return (long)(percentage * level * max_state) / (100 * tz->trips);
  59. }
  60. /**
  61. * fair_share_throttle - throttles devices associated with the given zone
  62. * @tz - thermal_zone_device
  63. *
  64. * Throttling Logic: This uses three parameters to calculate the new
  65. * throttle state of the cooling devices associated with the given zone.
  66. *
  67. * Parameters used for Throttling:
  68. * P1. max_state: Maximum throttle state exposed by the cooling device.
  69. * P2. percentage[i]/100:
  70. * How 'effective' the 'i'th device is, in cooling the given zone.
  71. * P3. cur_trip_level/max_no_of_trips:
  72. * This describes the extent to which the devices should be throttled.
  73. * We do not want to throttle too much when we trip a lower temperature,
  74. * whereas the throttling is at full swing if we trip critical levels.
  75. * (Heavily assumes the trip points are in ascending order)
  76. * new_state of cooling device = P3 * P2 * P1
  77. */
  78. static int fair_share_throttle(struct thermal_zone_device *tz, int trip)
  79. {
  80. struct thermal_instance *instance;
  81. int total_weight = 0;
  82. int total_instance = 0;
  83. int cur_trip_level = get_trip_level(tz);
  84. list_for_each_entry(instance, &tz->thermal_instances, tz_node) {
  85. if (instance->trip != trip)
  86. continue;
  87. total_weight += instance->weight;
  88. total_instance++;
  89. }
  90. list_for_each_entry(instance, &tz->thermal_instances, tz_node) {
  91. int percentage;
  92. struct thermal_cooling_device *cdev = instance->cdev;
  93. if (instance->trip != trip)
  94. continue;
  95. if (!total_weight)
  96. percentage = 100 / total_instance;
  97. else
  98. percentage = (instance->weight * 100) / total_weight;
  99. instance->target = get_target_state(tz, cdev, percentage,
  100. cur_trip_level);
  101. instance->cdev->updated = false;
  102. thermal_cdev_update(cdev);
  103. }
  104. return 0;
  105. }
  106. static struct thermal_governor thermal_gov_fair_share = {
  107. .name = "fair_share",
  108. .throttle = fair_share_throttle,
  109. };
  110. int thermal_gov_fair_share_register(void)
  111. {
  112. return thermal_register_governor(&thermal_gov_fair_share);
  113. }
  114. void thermal_gov_fair_share_unregister(void)
  115. {
  116. thermal_unregister_governor(&thermal_gov_fair_share);
  117. }