fair_share.c 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  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. #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
  25. #include <linux/module.h>
  26. #include <linux/thermal.h>
  27. #include "thermal_core.h"
  28. /**
  29. * get_trip_level: - obtains the current trip level for a zone
  30. * @tz: thermal zone device
  31. */
  32. static int get_trip_level(struct thermal_zone_device *tz)
  33. {
  34. int count = 0;
  35. unsigned long trip_temp;
  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. return count;
  44. }
  45. static long get_target_state(struct thermal_zone_device *tz,
  46. struct thermal_cooling_device *cdev, int weight, int level)
  47. {
  48. unsigned long max_state;
  49. cdev->ops->get_max_state(cdev, &max_state);
  50. return (long)(weight * level * max_state) / (100 * tz->trips);
  51. }
  52. /**
  53. * fair_share_throttle - throttles devices asscciated with the given zone
  54. * @tz - thermal_zone_device
  55. *
  56. * Throttling Logic: This uses three parameters to calculate the new
  57. * throttle state of the cooling devices associated with the given zone.
  58. *
  59. * Parameters used for Throttling:
  60. * P1. max_state: Maximum throttle state exposed by the cooling device.
  61. * P2. weight[i]/100:
  62. * How 'effective' the 'i'th device is, in cooling the given zone.
  63. * P3. cur_trip_level/max_no_of_trips:
  64. * This describes the extent to which the devices should be throttled.
  65. * We do not want to throttle too much when we trip a lower temperature,
  66. * whereas the throttling is at full swing if we trip critical levels.
  67. * (Heavily assumes the trip points are in ascending order)
  68. * new_state of cooling device = P3 * P2 * P1
  69. */
  70. static int fair_share_throttle(struct thermal_zone_device *tz, int trip)
  71. {
  72. const struct thermal_zone_params *tzp;
  73. struct thermal_cooling_device *cdev;
  74. struct thermal_instance *instance;
  75. int i;
  76. int cur_trip_level = get_trip_level(tz);
  77. if (!tz->tzp || !tz->tzp->tbp)
  78. return -EINVAL;
  79. tzp = tz->tzp;
  80. for (i = 0; i < tzp->num_tbps; i++) {
  81. if (!tzp->tbp[i].cdev)
  82. continue;
  83. cdev = tzp->tbp[i].cdev;
  84. instance = get_thermal_instance(tz, cdev, trip);
  85. if (!instance)
  86. continue;
  87. instance->target = get_target_state(tz, cdev,
  88. tzp->tbp[i].weight, cur_trip_level);
  89. instance->cdev->updated = false;
  90. thermal_cdev_update(cdev);
  91. }
  92. return 0;
  93. }
  94. static struct thermal_governor thermal_gov_fair_share = {
  95. .name = "fair_share",
  96. .throttle = fair_share_throttle,
  97. .owner = THIS_MODULE,
  98. };
  99. static int __init thermal_gov_fair_share_init(void)
  100. {
  101. return thermal_register_governor(&thermal_gov_fair_share);
  102. }
  103. static void __exit thermal_gov_fair_share_exit(void)
  104. {
  105. thermal_unregister_governor(&thermal_gov_fair_share);
  106. }
  107. /* This should load after thermal framework */
  108. fs_initcall(thermal_gov_fair_share_init);
  109. module_exit(thermal_gov_fair_share_exit);
  110. MODULE_AUTHOR("Durgadoss R");
  111. MODULE_DESCRIPTION("A simple weight based thermal throttling governor");
  112. MODULE_LICENSE("GPL");