governor_simpleondemand.c 3.4 KB

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