governor_simpleondemand.c 3.5 KB

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