MyungJoo Ham | ce26c5b | 2011-10-02 00:19:34 +0200 | [diff] [blame] | 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 | |
| 12 | #include <linux/errno.h> |
Nishanth Menon | eff607f | 2012-10-29 15:01:46 -0500 | [diff] [blame] | 13 | #include <linux/module.h> |
MyungJoo Ham | ce26c5b | 2011-10-02 00:19:34 +0200 | [diff] [blame] | 14 | #include <linux/devfreq.h> |
| 15 | #include <linux/math64.h> |
Rajagopal Venkat | 7e6fdd4 | 2012-10-26 01:50:09 +0200 | [diff] [blame] | 16 | #include "governor.h" |
MyungJoo Ham | ce26c5b | 2011-10-02 00:19:34 +0200 | [diff] [blame] | 17 | |
| 18 | /* Default constants for DevFreq-Simple-Ondemand (DFSO) */ |
| 19 | #define DFSO_UPTHRESHOLD (90) |
| 20 | #define DFSO_DOWNDIFFERENCTIAL (5) |
| 21 | static int devfreq_simple_ondemand_func(struct devfreq *df, |
| 22 | unsigned long *freq) |
| 23 | { |
Javi Merino | 08e75e7 | 2015-08-14 18:56:56 +0100 | [diff] [blame] | 24 | int err; |
| 25 | struct devfreq_dev_status *stat; |
MyungJoo Ham | ce26c5b | 2011-10-02 00:19:34 +0200 | [diff] [blame] | 26 | unsigned long long a, b; |
| 27 | unsigned int dfso_upthreshold = DFSO_UPTHRESHOLD; |
| 28 | unsigned int dfso_downdifferential = DFSO_DOWNDIFFERENCTIAL; |
| 29 | struct devfreq_simple_ondemand_data *data = df->data; |
MyungJoo Ham | 6530b9de | 2011-12-09 16:42:19 +0900 | [diff] [blame] | 30 | unsigned long max = (df->max_freq) ? df->max_freq : UINT_MAX; |
Sahitya Tummala | 4b18548 | 2013-10-21 11:27:13 +0530 | [diff] [blame^] | 31 | unsigned long min = (df->min_freq) ? df->min_freq : 0; |
MyungJoo Ham | ce26c5b | 2011-10-02 00:19:34 +0200 | [diff] [blame] | 32 | |
Javi Merino | 08e75e7 | 2015-08-14 18:56:56 +0100 | [diff] [blame] | 33 | err = devfreq_update_stats(df); |
MyungJoo Ham | ce26c5b | 2011-10-02 00:19:34 +0200 | [diff] [blame] | 34 | if (err) |
| 35 | return err; |
| 36 | |
Javi Merino | 08e75e7 | 2015-08-14 18:56:56 +0100 | [diff] [blame] | 37 | stat = &df->last_status; |
| 38 | |
MyungJoo Ham | ce26c5b | 2011-10-02 00:19:34 +0200 | [diff] [blame] | 39 | if (data) { |
| 40 | if (data->upthreshold) |
| 41 | dfso_upthreshold = data->upthreshold; |
| 42 | if (data->downdifferential) |
| 43 | dfso_downdifferential = data->downdifferential; |
| 44 | } |
| 45 | if (dfso_upthreshold > 100 || |
| 46 | dfso_upthreshold < dfso_downdifferential) |
| 47 | return -EINVAL; |
| 48 | |
MyungJoo Ham | ce26c5b | 2011-10-02 00:19:34 +0200 | [diff] [blame] | 49 | /* Prevent overflow */ |
Javi Merino | 08e75e7 | 2015-08-14 18:56:56 +0100 | [diff] [blame] | 50 | if (stat->busy_time >= (1 << 24) || stat->total_time >= (1 << 24)) { |
| 51 | stat->busy_time >>= 7; |
| 52 | stat->total_time >>= 7; |
MyungJoo Ham | ce26c5b | 2011-10-02 00:19:34 +0200 | [diff] [blame] | 53 | } |
| 54 | |
Sahitya Tummala | 4b18548 | 2013-10-21 11:27:13 +0530 | [diff] [blame^] | 55 | if (data && data->simple_scaling) { |
| 56 | if (stat->busy_time * 100 > |
| 57 | stat->total_time * dfso_upthreshold) |
| 58 | *freq = max; |
| 59 | else if (stat->busy_time * 100 < |
| 60 | stat->total_time * dfso_downdifferential) |
| 61 | *freq = min; |
| 62 | else |
| 63 | *freq = df->previous_freq; |
| 64 | return 0; |
| 65 | } |
| 66 | |
| 67 | /* Assume MAX if it is going to be divided by zero */ |
| 68 | if (stat->total_time == 0) { |
| 69 | *freq = max; |
| 70 | return 0; |
| 71 | } |
| 72 | |
MyungJoo Ham | ce26c5b | 2011-10-02 00:19:34 +0200 | [diff] [blame] | 73 | /* Set MAX if it's busy enough */ |
Javi Merino | 08e75e7 | 2015-08-14 18:56:56 +0100 | [diff] [blame] | 74 | if (stat->busy_time * 100 > |
| 75 | stat->total_time * dfso_upthreshold) { |
MyungJoo Ham | 6530b9de | 2011-12-09 16:42:19 +0900 | [diff] [blame] | 76 | *freq = max; |
MyungJoo Ham | ce26c5b | 2011-10-02 00:19:34 +0200 | [diff] [blame] | 77 | return 0; |
| 78 | } |
| 79 | |
| 80 | /* Set MAX if we do not know the initial frequency */ |
Javi Merino | 08e75e7 | 2015-08-14 18:56:56 +0100 | [diff] [blame] | 81 | if (stat->current_frequency == 0) { |
MyungJoo Ham | 6530b9de | 2011-12-09 16:42:19 +0900 | [diff] [blame] | 82 | *freq = max; |
MyungJoo Ham | ce26c5b | 2011-10-02 00:19:34 +0200 | [diff] [blame] | 83 | return 0; |
| 84 | } |
| 85 | |
| 86 | /* Keep the current frequency */ |
Javi Merino | 08e75e7 | 2015-08-14 18:56:56 +0100 | [diff] [blame] | 87 | if (stat->busy_time * 100 > |
| 88 | stat->total_time * (dfso_upthreshold - dfso_downdifferential)) { |
| 89 | *freq = stat->current_frequency; |
MyungJoo Ham | ce26c5b | 2011-10-02 00:19:34 +0200 | [diff] [blame] | 90 | return 0; |
| 91 | } |
| 92 | |
| 93 | /* Set the desired frequency based on the load */ |
Javi Merino | 08e75e7 | 2015-08-14 18:56:56 +0100 | [diff] [blame] | 94 | a = stat->busy_time; |
| 95 | a *= stat->current_frequency; |
| 96 | b = div_u64(a, stat->total_time); |
MyungJoo Ham | ce26c5b | 2011-10-02 00:19:34 +0200 | [diff] [blame] | 97 | b *= 100; |
| 98 | b = div_u64(b, (dfso_upthreshold - dfso_downdifferential / 2)); |
| 99 | *freq = (unsigned long) b; |
| 100 | |
MyungJoo Ham | 6530b9de | 2011-12-09 16:42:19 +0900 | [diff] [blame] | 101 | if (df->min_freq && *freq < df->min_freq) |
| 102 | *freq = df->min_freq; |
| 103 | if (df->max_freq && *freq > df->max_freq) |
| 104 | *freq = df->max_freq; |
| 105 | |
MyungJoo Ham | ce26c5b | 2011-10-02 00:19:34 +0200 | [diff] [blame] | 106 | return 0; |
| 107 | } |
| 108 | |
Rajagopal Venkat | 7e6fdd4 | 2012-10-26 01:50:09 +0200 | [diff] [blame] | 109 | static int devfreq_simple_ondemand_handler(struct devfreq *devfreq, |
| 110 | unsigned int event, void *data) |
| 111 | { |
| 112 | switch (event) { |
| 113 | case DEVFREQ_GOV_START: |
| 114 | devfreq_monitor_start(devfreq); |
| 115 | break; |
| 116 | |
| 117 | case DEVFREQ_GOV_STOP: |
| 118 | devfreq_monitor_stop(devfreq); |
| 119 | break; |
| 120 | |
| 121 | case DEVFREQ_GOV_INTERVAL: |
| 122 | devfreq_interval_update(devfreq, (unsigned int *)data); |
| 123 | break; |
Rajagopal Venkat | 206c30c | 2012-10-26 01:50:18 +0200 | [diff] [blame] | 124 | |
| 125 | case DEVFREQ_GOV_SUSPEND: |
| 126 | devfreq_monitor_suspend(devfreq); |
| 127 | break; |
| 128 | |
| 129 | case DEVFREQ_GOV_RESUME: |
| 130 | devfreq_monitor_resume(devfreq); |
| 131 | break; |
| 132 | |
Rajagopal Venkat | 7e6fdd4 | 2012-10-26 01:50:09 +0200 | [diff] [blame] | 133 | default: |
| 134 | break; |
| 135 | } |
| 136 | |
| 137 | return 0; |
| 138 | } |
| 139 | |
Nishanth Menon | 1b5c1be | 2012-10-29 15:01:45 -0500 | [diff] [blame] | 140 | static struct devfreq_governor devfreq_simple_ondemand = { |
MyungJoo Ham | ce26c5b | 2011-10-02 00:19:34 +0200 | [diff] [blame] | 141 | .name = "simple_ondemand", |
| 142 | .get_target_freq = devfreq_simple_ondemand_func, |
Rajagopal Venkat | 7e6fdd4 | 2012-10-26 01:50:09 +0200 | [diff] [blame] | 143 | .event_handler = devfreq_simple_ondemand_handler, |
MyungJoo Ham | ce26c5b | 2011-10-02 00:19:34 +0200 | [diff] [blame] | 144 | }; |
Nishanth Menon | 83116e6 | 2012-10-29 15:01:44 -0500 | [diff] [blame] | 145 | |
| 146 | static int __init devfreq_simple_ondemand_init(void) |
| 147 | { |
| 148 | return devfreq_add_governor(&devfreq_simple_ondemand); |
| 149 | } |
| 150 | subsys_initcall(devfreq_simple_ondemand_init); |
| 151 | |
| 152 | static void __exit devfreq_simple_ondemand_exit(void) |
| 153 | { |
| 154 | int ret; |
| 155 | |
| 156 | ret = devfreq_remove_governor(&devfreq_simple_ondemand); |
| 157 | if (ret) |
| 158 | pr_err("%s: failed remove governor %d\n", __func__, ret); |
| 159 | |
| 160 | return; |
| 161 | } |
| 162 | module_exit(devfreq_simple_ondemand_exit); |
Nishanth Menon | eff607f | 2012-10-29 15:01:46 -0500 | [diff] [blame] | 163 | MODULE_LICENSE("GPL"); |