governor_simpleondemand.c 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * linux/drivers/devfreq/governor_simpleondemand.c
  4. *
  5. * Copyright (C) 2011 Samsung Electronics
  6. * MyungJoo Ham <[email protected]>
  7. */
  8. #include <linux/errno.h>
  9. #include <linux/module.h>
  10. #include <linux/devfreq.h>
  11. #include <linux/math64.h>
  12. #include "governor.h"
  13. /* Default constants for DevFreq-Simple-Ondemand (DFSO) */
  14. #define DFSO_UPTHRESHOLD (90)
  15. #define DFSO_DOWNDIFFERENCTIAL (5)
  16. static int devfreq_simple_ondemand_func(struct devfreq *df,
  17. unsigned long *freq)
  18. {
  19. int err;
  20. struct devfreq_dev_status *stat;
  21. unsigned long long a, b;
  22. unsigned int dfso_upthreshold = DFSO_UPTHRESHOLD;
  23. unsigned int dfso_downdifferential = DFSO_DOWNDIFFERENCTIAL;
  24. struct devfreq_simple_ondemand_data *data = df->data;
  25. err = devfreq_update_stats(df);
  26. if (err)
  27. return err;
  28. stat = &df->last_status;
  29. if (data) {
  30. if (data->upthreshold)
  31. dfso_upthreshold = data->upthreshold;
  32. if (data->downdifferential)
  33. dfso_downdifferential = data->downdifferential;
  34. }
  35. if (dfso_upthreshold > 100 ||
  36. dfso_upthreshold < dfso_downdifferential)
  37. return -EINVAL;
  38. /* Assume MAX if it is going to be divided by zero */
  39. if (stat->total_time == 0) {
  40. *freq = DEVFREQ_MAX_FREQ;
  41. return 0;
  42. }
  43. /* Prevent overflow */
  44. if (stat->busy_time >= (1 << 24) || stat->total_time >= (1 << 24)) {
  45. stat->busy_time >>= 7;
  46. stat->total_time >>= 7;
  47. }
  48. /* Set MAX if it's busy enough */
  49. if (stat->busy_time * 100 >
  50. stat->total_time * dfso_upthreshold) {
  51. *freq = DEVFREQ_MAX_FREQ;
  52. return 0;
  53. }
  54. /* Set MAX if we do not know the initial frequency */
  55. if (stat->current_frequency == 0) {
  56. *freq = DEVFREQ_MAX_FREQ;
  57. return 0;
  58. }
  59. /* Keep the current frequency */
  60. if (stat->busy_time * 100 >
  61. stat->total_time * (dfso_upthreshold - dfso_downdifferential)) {
  62. *freq = stat->current_frequency;
  63. return 0;
  64. }
  65. /* Set the desired frequency based on the load */
  66. a = stat->busy_time;
  67. a *= stat->current_frequency;
  68. b = div_u64(a, stat->total_time);
  69. b *= 100;
  70. b = div_u64(b, (dfso_upthreshold - dfso_downdifferential / 2));
  71. *freq = (unsigned long) b;
  72. return 0;
  73. }
  74. static int devfreq_simple_ondemand_handler(struct devfreq *devfreq,
  75. unsigned int event, void *data)
  76. {
  77. switch (event) {
  78. case DEVFREQ_GOV_START:
  79. devfreq_monitor_start(devfreq);
  80. break;
  81. case DEVFREQ_GOV_STOP:
  82. devfreq_monitor_stop(devfreq);
  83. break;
  84. case DEVFREQ_GOV_UPDATE_INTERVAL:
  85. devfreq_update_interval(devfreq, (unsigned int *)data);
  86. break;
  87. case DEVFREQ_GOV_SUSPEND:
  88. devfreq_monitor_suspend(devfreq);
  89. break;
  90. case DEVFREQ_GOV_RESUME:
  91. devfreq_monitor_resume(devfreq);
  92. break;
  93. default:
  94. break;
  95. }
  96. return 0;
  97. }
  98. static struct devfreq_governor devfreq_simple_ondemand = {
  99. .name = DEVFREQ_GOV_SIMPLE_ONDEMAND,
  100. .attrs = DEVFREQ_GOV_ATTR_POLLING_INTERVAL
  101. | DEVFREQ_GOV_ATTR_TIMER,
  102. .get_target_freq = devfreq_simple_ondemand_func,
  103. .event_handler = devfreq_simple_ondemand_handler,
  104. };
  105. static int __init devfreq_simple_ondemand_init(void)
  106. {
  107. return devfreq_add_governor(&devfreq_simple_ondemand);
  108. }
  109. subsys_initcall(devfreq_simple_ondemand_init);
  110. static void __exit devfreq_simple_ondemand_exit(void)
  111. {
  112. int ret;
  113. ret = devfreq_remove_governor(&devfreq_simple_ondemand);
  114. if (ret)
  115. pr_err("%s: failed remove governor %d\n", __func__, ret);
  116. return;
  117. }
  118. module_exit(devfreq_simple_ondemand_exit);
  119. MODULE_LICENSE("GPL");