Thomas Gleixner | d2912cb | 2019-06-04 10:11:33 +0200 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0-only |
MyungJoo Ham | ce26c5b | 2011-10-02 00:19:34 +0200 | [diff] [blame] | 2 | /* |
| 3 | * linux/drivers/devfreq/governor_simpleondemand.c |
| 4 | * |
| 5 | * Copyright (C) 2011 Samsung Electronics |
| 6 | * MyungJoo Ham <myungjoo.ham@samsung.com> |
MyungJoo Ham | ce26c5b | 2011-10-02 00:19:34 +0200 | [diff] [blame] | 7 | */ |
| 8 | |
| 9 | #include <linux/errno.h> |
Nishanth Menon | eff607f | 2012-10-29 15:01:46 -0500 | [diff] [blame] | 10 | #include <linux/module.h> |
MyungJoo Ham | ce26c5b | 2011-10-02 00:19:34 +0200 | [diff] [blame] | 11 | #include <linux/devfreq.h> |
| 12 | #include <linux/math64.h> |
Rajagopal Venkat | 7e6fdd4 | 2012-10-26 01:50:09 +0200 | [diff] [blame] | 13 | #include "governor.h" |
MyungJoo Ham | ce26c5b | 2011-10-02 00:19:34 +0200 | [diff] [blame] | 14 | |
| 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 | { |
Javi Merino | 08e75e7 | 2015-08-14 18:56:56 +0100 | [diff] [blame] | 21 | int err; |
| 22 | struct devfreq_dev_status *stat; |
MyungJoo Ham | ce26c5b | 2011-10-02 00:19:34 +0200 | [diff] [blame] | 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 | |
Javi Merino | 08e75e7 | 2015-08-14 18:56:56 +0100 | [diff] [blame] | 28 | err = devfreq_update_stats(df); |
MyungJoo Ham | ce26c5b | 2011-10-02 00:19:34 +0200 | [diff] [blame] | 29 | if (err) |
| 30 | return err; |
| 31 | |
Javi Merino | 08e75e7 | 2015-08-14 18:56:56 +0100 | [diff] [blame] | 32 | stat = &df->last_status; |
| 33 | |
MyungJoo Ham | ce26c5b | 2011-10-02 00:19:34 +0200 | [diff] [blame] | 34 | if (data) { |
| 35 | if (data->upthreshold) |
| 36 | dfso_upthreshold = data->upthreshold; |
| 37 | if (data->downdifferential) |
| 38 | dfso_downdifferential = data->downdifferential; |
| 39 | } |
| 40 | if (dfso_upthreshold > 100 || |
| 41 | dfso_upthreshold < dfso_downdifferential) |
| 42 | return -EINVAL; |
| 43 | |
| 44 | /* Assume MAX if it is going to be divided by zero */ |
Javi Merino | 08e75e7 | 2015-08-14 18:56:56 +0100 | [diff] [blame] | 45 | if (stat->total_time == 0) { |
Matthias Kaehlcke | 6ff66e2 | 2018-08-03 13:05:10 -0700 | [diff] [blame] | 46 | *freq = DEVFREQ_MAX_FREQ; |
MyungJoo Ham | ce26c5b | 2011-10-02 00:19:34 +0200 | [diff] [blame] | 47 | return 0; |
| 48 | } |
| 49 | |
| 50 | /* Prevent overflow */ |
Javi Merino | 08e75e7 | 2015-08-14 18:56:56 +0100 | [diff] [blame] | 51 | if (stat->busy_time >= (1 << 24) || stat->total_time >= (1 << 24)) { |
| 52 | stat->busy_time >>= 7; |
| 53 | stat->total_time >>= 7; |
MyungJoo Ham | ce26c5b | 2011-10-02 00:19:34 +0200 | [diff] [blame] | 54 | } |
| 55 | |
| 56 | /* Set MAX if it's busy enough */ |
Javi Merino | 08e75e7 | 2015-08-14 18:56:56 +0100 | [diff] [blame] | 57 | if (stat->busy_time * 100 > |
| 58 | stat->total_time * dfso_upthreshold) { |
Matthias Kaehlcke | 6ff66e2 | 2018-08-03 13:05:10 -0700 | [diff] [blame] | 59 | *freq = DEVFREQ_MAX_FREQ; |
MyungJoo Ham | ce26c5b | 2011-10-02 00:19:34 +0200 | [diff] [blame] | 60 | return 0; |
| 61 | } |
| 62 | |
| 63 | /* Set MAX if we do not know the initial frequency */ |
Javi Merino | 08e75e7 | 2015-08-14 18:56:56 +0100 | [diff] [blame] | 64 | if (stat->current_frequency == 0) { |
Matthias Kaehlcke | 6ff66e2 | 2018-08-03 13:05:10 -0700 | [diff] [blame] | 65 | *freq = DEVFREQ_MAX_FREQ; |
MyungJoo Ham | ce26c5b | 2011-10-02 00:19:34 +0200 | [diff] [blame] | 66 | return 0; |
| 67 | } |
| 68 | |
| 69 | /* Keep the current frequency */ |
Javi Merino | 08e75e7 | 2015-08-14 18:56:56 +0100 | [diff] [blame] | 70 | if (stat->busy_time * 100 > |
| 71 | stat->total_time * (dfso_upthreshold - dfso_downdifferential)) { |
| 72 | *freq = stat->current_frequency; |
MyungJoo Ham | ce26c5b | 2011-10-02 00:19:34 +0200 | [diff] [blame] | 73 | return 0; |
| 74 | } |
| 75 | |
| 76 | /* Set the desired frequency based on the load */ |
Javi Merino | 08e75e7 | 2015-08-14 18:56:56 +0100 | [diff] [blame] | 77 | a = stat->busy_time; |
| 78 | a *= stat->current_frequency; |
| 79 | b = div_u64(a, stat->total_time); |
MyungJoo Ham | ce26c5b | 2011-10-02 00:19:34 +0200 | [diff] [blame] | 80 | b *= 100; |
| 81 | b = div_u64(b, (dfso_upthreshold - dfso_downdifferential / 2)); |
| 82 | *freq = (unsigned long) b; |
| 83 | |
MyungJoo Ham | ce26c5b | 2011-10-02 00:19:34 +0200 | [diff] [blame] | 84 | return 0; |
| 85 | } |
| 86 | |
Rajagopal Venkat | 7e6fdd4 | 2012-10-26 01:50:09 +0200 | [diff] [blame] | 87 | static int devfreq_simple_ondemand_handler(struct devfreq *devfreq, |
| 88 | unsigned int event, void *data) |
| 89 | { |
| 90 | switch (event) { |
| 91 | case DEVFREQ_GOV_START: |
| 92 | devfreq_monitor_start(devfreq); |
| 93 | break; |
| 94 | |
| 95 | case DEVFREQ_GOV_STOP: |
| 96 | devfreq_monitor_stop(devfreq); |
| 97 | break; |
| 98 | |
Chanwoo Choi | 3a1ec2e | 2020-01-29 13:24:18 +0900 | [diff] [blame] | 99 | case DEVFREQ_GOV_UPDATE_INTERVAL: |
| 100 | devfreq_update_interval(devfreq, (unsigned int *)data); |
Rajagopal Venkat | 7e6fdd4 | 2012-10-26 01:50:09 +0200 | [diff] [blame] | 101 | break; |
Rajagopal Venkat | 206c30c | 2012-10-26 01:50:18 +0200 | [diff] [blame] | 102 | |
| 103 | case DEVFREQ_GOV_SUSPEND: |
| 104 | devfreq_monitor_suspend(devfreq); |
| 105 | break; |
| 106 | |
| 107 | case DEVFREQ_GOV_RESUME: |
| 108 | devfreq_monitor_resume(devfreq); |
| 109 | break; |
| 110 | |
Rajagopal Venkat | 7e6fdd4 | 2012-10-26 01:50:09 +0200 | [diff] [blame] | 111 | default: |
| 112 | break; |
| 113 | } |
| 114 | |
| 115 | return 0; |
| 116 | } |
| 117 | |
Nishanth Menon | 1b5c1be | 2012-10-29 15:01:45 -0500 | [diff] [blame] | 118 | static struct devfreq_governor devfreq_simple_ondemand = { |
Chanwoo Choi | aa7c352 | 2017-10-23 10:32:12 +0900 | [diff] [blame] | 119 | .name = DEVFREQ_GOV_SIMPLE_ONDEMAND, |
MyungJoo Ham | ce26c5b | 2011-10-02 00:19:34 +0200 | [diff] [blame] | 120 | .get_target_freq = devfreq_simple_ondemand_func, |
Rajagopal Venkat | 7e6fdd4 | 2012-10-26 01:50:09 +0200 | [diff] [blame] | 121 | .event_handler = devfreq_simple_ondemand_handler, |
MyungJoo Ham | ce26c5b | 2011-10-02 00:19:34 +0200 | [diff] [blame] | 122 | }; |
Nishanth Menon | 83116e6 | 2012-10-29 15:01:44 -0500 | [diff] [blame] | 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 | |
| 130 | static void __exit devfreq_simple_ondemand_exit(void) |
| 131 | { |
| 132 | int ret; |
| 133 | |
| 134 | ret = devfreq_remove_governor(&devfreq_simple_ondemand); |
| 135 | if (ret) |
| 136 | pr_err("%s: failed remove governor %d\n", __func__, ret); |
| 137 | |
| 138 | return; |
| 139 | } |
| 140 | module_exit(devfreq_simple_ondemand_exit); |
Nishanth Menon | eff607f | 2012-10-29 15:01:46 -0500 | [diff] [blame] | 141 | MODULE_LICENSE("GPL"); |