governor_simpleondemand.c 3.5 KB

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