governor_simpleondemand.c 2.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  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. /* Default constants for DevFreq-Simple-Ondemand (DFSO) */
  15. #define DFSO_UPTHRESHOLD (90)
  16. #define DFSO_DOWNDIFFERENCTIAL (5)
  17. static int devfreq_simple_ondemand_func(struct devfreq *df,
  18. unsigned long *freq)
  19. {
  20. struct devfreq_dev_status stat;
  21. int err = df->profile->get_dev_status(df->dev.parent, &stat);
  22. unsigned long long a, b;
  23. unsigned int dfso_upthreshold = DFSO_UPTHRESHOLD;
  24. unsigned int dfso_downdifferential = DFSO_DOWNDIFFERENCTIAL;
  25. struct devfreq_simple_ondemand_data *data = df->data;
  26. if (err)
  27. return err;
  28. if (data) {
  29. if (data->upthreshold)
  30. dfso_upthreshold = data->upthreshold;
  31. if (data->downdifferential)
  32. dfso_downdifferential = data->downdifferential;
  33. }
  34. if (dfso_upthreshold > 100 ||
  35. dfso_upthreshold < dfso_downdifferential)
  36. return -EINVAL;
  37. /* Assume MAX if it is going to be divided by zero */
  38. if (stat.total_time == 0) {
  39. *freq = UINT_MAX;
  40. return 0;
  41. }
  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. /* Set MAX if it's busy enough */
  48. if (stat.busy_time * 100 >
  49. stat.total_time * dfso_upthreshold) {
  50. *freq = UINT_MAX;
  51. return 0;
  52. }
  53. /* Set MAX if we do not know the initial frequency */
  54. if (stat.current_frequency == 0) {
  55. *freq = UINT_MAX;
  56. return 0;
  57. }
  58. /* Keep the current frequency */
  59. if (stat.busy_time * 100 >
  60. stat.total_time * (dfso_upthreshold - dfso_downdifferential)) {
  61. *freq = stat.current_frequency;
  62. return 0;
  63. }
  64. /* Set the desired frequency based on the load */
  65. a = stat.busy_time;
  66. a *= stat.current_frequency;
  67. b = div_u64(a, stat.total_time);
  68. b *= 100;
  69. b = div_u64(b, (dfso_upthreshold - dfso_downdifferential / 2));
  70. *freq = (unsigned long) b;
  71. return 0;
  72. }
  73. const struct devfreq_governor devfreq_simple_ondemand = {
  74. .name = "simple_ondemand",
  75. .get_target_freq = devfreq_simple_ondemand_func,
  76. };