blob: 1b314e1df0289036928b630229bbe2d87cf4acaf [file] [log] [blame]
Thomas Gleixnerd2912cb2019-06-04 10:11:33 +02001// SPDX-License-Identifier: GPL-2.0-only
MyungJoo Hamce26c5b2011-10-02 00:19:34 +02002/*
3 * linux/drivers/devfreq/governor_simpleondemand.c
4 *
5 * Copyright (C) 2011 Samsung Electronics
6 * MyungJoo Ham <myungjoo.ham@samsung.com>
MyungJoo Hamce26c5b2011-10-02 00:19:34 +02007 */
8
9#include <linux/errno.h>
Nishanth Menoneff607f2012-10-29 15:01:46 -050010#include <linux/module.h>
MyungJoo Hamce26c5b2011-10-02 00:19:34 +020011#include <linux/devfreq.h>
12#include <linux/math64.h>
Rajagopal Venkat7e6fdd42012-10-26 01:50:09 +020013#include "governor.h"
MyungJoo Hamce26c5b2011-10-02 00:19:34 +020014
15/* Default constants for DevFreq-Simple-Ondemand (DFSO) */
16#define DFSO_UPTHRESHOLD (90)
17#define DFSO_DOWNDIFFERENCTIAL (5)
18static int devfreq_simple_ondemand_func(struct devfreq *df,
19 unsigned long *freq)
20{
Javi Merino08e75e72015-08-14 18:56:56 +010021 int err;
22 struct devfreq_dev_status *stat;
MyungJoo Hamce26c5b2011-10-02 00:19:34 +020023 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 Merino08e75e72015-08-14 18:56:56 +010028 err = devfreq_update_stats(df);
MyungJoo Hamce26c5b2011-10-02 00:19:34 +020029 if (err)
30 return err;
31
Javi Merino08e75e72015-08-14 18:56:56 +010032 stat = &df->last_status;
33
MyungJoo Hamce26c5b2011-10-02 00:19:34 +020034 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 Merino08e75e72015-08-14 18:56:56 +010045 if (stat->total_time == 0) {
Matthias Kaehlcke6ff66e22018-08-03 13:05:10 -070046 *freq = DEVFREQ_MAX_FREQ;
MyungJoo Hamce26c5b2011-10-02 00:19:34 +020047 return 0;
48 }
49
50 /* Prevent overflow */
Javi Merino08e75e72015-08-14 18:56:56 +010051 if (stat->busy_time >= (1 << 24) || stat->total_time >= (1 << 24)) {
52 stat->busy_time >>= 7;
53 stat->total_time >>= 7;
MyungJoo Hamce26c5b2011-10-02 00:19:34 +020054 }
55
56 /* Set MAX if it's busy enough */
Javi Merino08e75e72015-08-14 18:56:56 +010057 if (stat->busy_time * 100 >
58 stat->total_time * dfso_upthreshold) {
Matthias Kaehlcke6ff66e22018-08-03 13:05:10 -070059 *freq = DEVFREQ_MAX_FREQ;
MyungJoo Hamce26c5b2011-10-02 00:19:34 +020060 return 0;
61 }
62
63 /* Set MAX if we do not know the initial frequency */
Javi Merino08e75e72015-08-14 18:56:56 +010064 if (stat->current_frequency == 0) {
Matthias Kaehlcke6ff66e22018-08-03 13:05:10 -070065 *freq = DEVFREQ_MAX_FREQ;
MyungJoo Hamce26c5b2011-10-02 00:19:34 +020066 return 0;
67 }
68
69 /* Keep the current frequency */
Javi Merino08e75e72015-08-14 18:56:56 +010070 if (stat->busy_time * 100 >
71 stat->total_time * (dfso_upthreshold - dfso_downdifferential)) {
72 *freq = stat->current_frequency;
MyungJoo Hamce26c5b2011-10-02 00:19:34 +020073 return 0;
74 }
75
76 /* Set the desired frequency based on the load */
Javi Merino08e75e72015-08-14 18:56:56 +010077 a = stat->busy_time;
78 a *= stat->current_frequency;
79 b = div_u64(a, stat->total_time);
MyungJoo Hamce26c5b2011-10-02 00:19:34 +020080 b *= 100;
81 b = div_u64(b, (dfso_upthreshold - dfso_downdifferential / 2));
82 *freq = (unsigned long) b;
83
MyungJoo Hamce26c5b2011-10-02 00:19:34 +020084 return 0;
85}
86
Rajagopal Venkat7e6fdd42012-10-26 01:50:09 +020087static 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 Choi3a1ec2e2020-01-29 13:24:18 +090099 case DEVFREQ_GOV_UPDATE_INTERVAL:
100 devfreq_update_interval(devfreq, (unsigned int *)data);
Rajagopal Venkat7e6fdd42012-10-26 01:50:09 +0200101 break;
Rajagopal Venkat206c30c2012-10-26 01:50:18 +0200102
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 Venkat7e6fdd42012-10-26 01:50:09 +0200111 default:
112 break;
113 }
114
115 return 0;
116}
117
Nishanth Menon1b5c1be2012-10-29 15:01:45 -0500118static struct devfreq_governor devfreq_simple_ondemand = {
Chanwoo Choiaa7c3522017-10-23 10:32:12 +0900119 .name = DEVFREQ_GOV_SIMPLE_ONDEMAND,
MyungJoo Hamce26c5b2011-10-02 00:19:34 +0200120 .get_target_freq = devfreq_simple_ondemand_func,
Rajagopal Venkat7e6fdd42012-10-26 01:50:09 +0200121 .event_handler = devfreq_simple_ondemand_handler,
MyungJoo Hamce26c5b2011-10-02 00:19:34 +0200122};
Nishanth Menon83116e62012-10-29 15:01:44 -0500123
124static int __init devfreq_simple_ondemand_init(void)
125{
126 return devfreq_add_governor(&devfreq_simple_ondemand);
127}
128subsys_initcall(devfreq_simple_ondemand_init);
129
130static 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}
140module_exit(devfreq_simple_ondemand_exit);
Nishanth Menoneff607f2012-10-29 15:01:46 -0500141MODULE_LICENSE("GPL");