gov_fair_share.c 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  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 <durgadoss.r@intel.com>
  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->trips == 0 || !tz->ops->get_trip_temp)
  25. return 0;
  26. for (count = 0; count < tz->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. unsigned long max_state;
  45. cdev->ops->get_max_state(cdev, &max_state);
  46. return (long)(percentage * level * max_state) / (100 * tz->trips);
  47. }
  48. /**
  49. * fair_share_throttle - throttles devices associated with the given zone
  50. * @tz: thermal_zone_device
  51. * @trip: trip point index
  52. *
  53. * Throttling Logic: This uses three parameters to calculate the new
  54. * throttle state of the cooling devices associated with the given zone.
  55. *
  56. * Parameters used for Throttling:
  57. * P1. max_state: Maximum throttle state exposed by the cooling device.
  58. * P2. percentage[i]/100:
  59. * How 'effective' the 'i'th device is, in cooling the given zone.
  60. * P3. cur_trip_level/max_no_of_trips:
  61. * This describes the extent to which the devices should be throttled.
  62. * We do not want to throttle too much when we trip a lower temperature,
  63. * whereas the throttling is at full swing if we trip critical levels.
  64. * (Heavily assumes the trip points are in ascending order)
  65. * new_state of cooling device = P3 * P2 * P1
  66. */
  67. static int fair_share_throttle(struct thermal_zone_device *tz, int trip)
  68. {
  69. struct thermal_instance *instance;
  70. int total_weight = 0;
  71. int total_instance = 0;
  72. int cur_trip_level = get_trip_level(tz);
  73. mutex_lock(&tz->lock);
  74. list_for_each_entry(instance, &tz->thermal_instances, tz_node) {
  75. if (instance->trip != trip)
  76. continue;
  77. total_weight += instance->weight;
  78. total_instance++;
  79. }
  80. list_for_each_entry(instance, &tz->thermal_instances, tz_node) {
  81. int percentage;
  82. struct thermal_cooling_device *cdev = instance->cdev;
  83. if (instance->trip != trip)
  84. continue;
  85. if (!total_weight)
  86. percentage = 100 / total_instance;
  87. else
  88. percentage = (instance->weight * 100) / total_weight;
  89. instance->target = get_target_state(tz, cdev, percentage,
  90. cur_trip_level);
  91. mutex_lock(&instance->cdev->lock);
  92. instance->cdev->updated = false;
  93. mutex_unlock(&instance->cdev->lock);
  94. thermal_cdev_update(cdev);
  95. }
  96. mutex_unlock(&tz->lock);
  97. return 0;
  98. }
  99. static struct thermal_governor thermal_gov_fair_share = {
  100. .name = "fair_share",
  101. .throttle = fair_share_throttle,
  102. };
  103. THERMAL_GOVERNOR_DECLARE(thermal_gov_fair_share);