governor_simpleondemand.c 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  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. u32 *flag)
  22. {
  23. struct devfreq_dev_status stat;
  24. int err = df->profile->get_dev_status(df->dev.parent, &stat);
  25. unsigned long long a, b;
  26. unsigned int dfso_upthreshold = DFSO_UPTHRESHOLD;
  27. unsigned int dfso_downdifferential = DFSO_DOWNDIFFERENCTIAL;
  28. struct devfreq_simple_ondemand_data *data = df->data;
  29. unsigned long max = (df->max_freq) ? df->max_freq : UINT_MAX;
  30. unsigned long min = (df->min_freq) ? df->min_freq : 0;
  31. if (err)
  32. return err;
  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. /* Prevent overflow */
  43. if (stat.busy_time >= (1 << 24) || stat.total_time >= (1 << 24)) {
  44. stat.busy_time >>= 7;
  45. stat.total_time >>= 7;
  46. }
  47. if (data && data->simple_scaling) {
  48. if (stat.busy_time * 100 >
  49. stat.total_time * dfso_upthreshold)
  50. *freq = max;
  51. else if (stat.busy_time * 100 <
  52. stat.total_time * dfso_downdifferential)
  53. *freq = min;
  54. else
  55. *freq = df->previous_freq;
  56. return 0;
  57. }
  58. /* Assume MAX if it is going to be divided by zero */
  59. if (stat.total_time == 0) {
  60. *freq = max;
  61. return 0;
  62. }
  63. /* Set MAX if it's busy enough */
  64. if (stat.busy_time * 100 >
  65. stat.total_time * dfso_upthreshold) {
  66. *freq = max;
  67. return 0;
  68. }
  69. /* Set MAX if we do not know the initial frequency */
  70. if (stat.current_frequency == 0) {
  71. *freq = max;
  72. return 0;
  73. }
  74. /* Keep the current frequency */
  75. if (stat.busy_time * 100 >
  76. stat.total_time * (dfso_upthreshold - dfso_downdifferential)) {
  77. *freq = stat.current_frequency;
  78. return 0;
  79. }
  80. /* Set the desired frequency based on the load */
  81. a = stat.busy_time;
  82. a *= stat.current_frequency;
  83. b = div_u64(a, stat.total_time);
  84. b *= 100;
  85. b = div_u64(b, (dfso_upthreshold - dfso_downdifferential / 2));
  86. *freq = (unsigned long) b;
  87. if (df->min_freq && *freq < df->min_freq)
  88. *freq = df->min_freq;
  89. if (df->max_freq && *freq > df->max_freq)
  90. *freq = df->max_freq;
  91. return 0;
  92. }
  93. static int devfreq_simple_ondemand_handler(struct devfreq *devfreq,
  94. unsigned int event, void *data)
  95. {
  96. switch (event) {
  97. case DEVFREQ_GOV_START:
  98. devfreq_monitor_start(devfreq);
  99. break;
  100. case DEVFREQ_GOV_STOP:
  101. devfreq_monitor_stop(devfreq);
  102. break;
  103. case DEVFREQ_GOV_INTERVAL:
  104. devfreq_interval_update(devfreq, (unsigned int *)data);
  105. break;
  106. case DEVFREQ_GOV_SUSPEND:
  107. devfreq_monitor_suspend(devfreq);
  108. break;
  109. case DEVFREQ_GOV_RESUME:
  110. devfreq_monitor_resume(devfreq);
  111. break;
  112. default:
  113. break;
  114. }
  115. return 0;
  116. }
  117. static struct devfreq_governor devfreq_simple_ondemand = {
  118. .name = "simple_ondemand",
  119. .get_target_freq = devfreq_simple_ondemand_func,
  120. .event_handler = devfreq_simple_ondemand_handler,
  121. };
  122. static int __init devfreq_simple_ondemand_init(void)
  123. {
  124. return devfreq_add_governor(&devfreq_simple_ondemand);
  125. }
  126. subsys_initcall(devfreq_simple_ondemand_init);
  127. static void __exit devfreq_simple_ondemand_exit(void)
  128. {
  129. int ret;
  130. ret = devfreq_remove_governor(&devfreq_simple_ondemand);
  131. if (ret)
  132. pr_err("%s: failed remove governor %d\n", __func__, ret);
  133. return;
  134. }
  135. module_exit(devfreq_simple_ondemand_exit);
  136. MODULE_LICENSE("GPL");