governor_simpleondemand.c 3.9 KB

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