Blame view

drivers/devfreq/governor_simpleondemand.c 3.22 KB
d2912cb15   Thomas Gleixner   treewide: Replace...
1
  // SPDX-License-Identifier: GPL-2.0-only
ce26c5bb9   MyungJoo Ham   PM / devfreq: Add...
2
3
4
5
6
  /*
   *  linux/drivers/devfreq/governor_simpleondemand.c
   *
   *  Copyright (C) 2011 Samsung Electronics
   *	MyungJoo Ham <myungjoo.ham@samsung.com>
ce26c5bb9   MyungJoo Ham   PM / devfreq: Add...
7
8
9
   */
  
  #include <linux/errno.h>
eff607fdb   Nishanth Menon   PM / devfreq: gov...
10
  #include <linux/module.h>
ce26c5bb9   MyungJoo Ham   PM / devfreq: Add...
11
12
  #include <linux/devfreq.h>
  #include <linux/math64.h>
7e6fdd4ba   Rajagopal Venkat   PM / devfreq: Cor...
13
  #include "governor.h"
ce26c5bb9   MyungJoo Ham   PM / devfreq: Add...
14
15
16
17
18
19
20
  
  /* Default constants for DevFreq-Simple-Ondemand (DFSO) */
  #define DFSO_UPTHRESHOLD	(90)
  #define DFSO_DOWNDIFFERENCTIAL	(5)
  static int devfreq_simple_ondemand_func(struct devfreq *df,
  					unsigned long *freq)
  {
08e75e754   Javi Merino   PM / devfreq: cac...
21
22
  	int err;
  	struct devfreq_dev_status *stat;
ce26c5bb9   MyungJoo Ham   PM / devfreq: Add...
23
24
25
26
  	unsigned long long a, b;
  	unsigned int dfso_upthreshold = DFSO_UPTHRESHOLD;
  	unsigned int dfso_downdifferential = DFSO_DOWNDIFFERENCTIAL;
  	struct devfreq_simple_ondemand_data *data = df->data;
08e75e754   Javi Merino   PM / devfreq: cac...
27
  	err = devfreq_update_stats(df);
ce26c5bb9   MyungJoo Ham   PM / devfreq: Add...
28
29
  	if (err)
  		return err;
08e75e754   Javi Merino   PM / devfreq: cac...
30
  	stat = &df->last_status;
ce26c5bb9   MyungJoo Ham   PM / devfreq: Add...
31
32
33
34
35
36
37
38
39
40
41
  	if (data) {
  		if (data->upthreshold)
  			dfso_upthreshold = data->upthreshold;
  		if (data->downdifferential)
  			dfso_downdifferential = data->downdifferential;
  	}
  	if (dfso_upthreshold > 100 ||
  	    dfso_upthreshold < dfso_downdifferential)
  		return -EINVAL;
  
  	/* Assume MAX if it is going to be divided by zero */
08e75e754   Javi Merino   PM / devfreq: cac...
42
  	if (stat->total_time == 0) {
6ff66e2a0   Matthias Kaehlcke   PM / devfreq: Don...
43
  		*freq = DEVFREQ_MAX_FREQ;
ce26c5bb9   MyungJoo Ham   PM / devfreq: Add...
44
45
46
47
  		return 0;
  	}
  
  	/* Prevent overflow */
08e75e754   Javi Merino   PM / devfreq: cac...
48
49
50
  	if (stat->busy_time >= (1 << 24) || stat->total_time >= (1 << 24)) {
  		stat->busy_time >>= 7;
  		stat->total_time >>= 7;
ce26c5bb9   MyungJoo Ham   PM / devfreq: Add...
51
52
53
  	}
  
  	/* Set MAX if it's busy enough */
08e75e754   Javi Merino   PM / devfreq: cac...
54
55
  	if (stat->busy_time * 100 >
  	    stat->total_time * dfso_upthreshold) {
6ff66e2a0   Matthias Kaehlcke   PM / devfreq: Don...
56
  		*freq = DEVFREQ_MAX_FREQ;
ce26c5bb9   MyungJoo Ham   PM / devfreq: Add...
57
58
59
60
  		return 0;
  	}
  
  	/* Set MAX if we do not know the initial frequency */
08e75e754   Javi Merino   PM / devfreq: cac...
61
  	if (stat->current_frequency == 0) {
6ff66e2a0   Matthias Kaehlcke   PM / devfreq: Don...
62
  		*freq = DEVFREQ_MAX_FREQ;
ce26c5bb9   MyungJoo Ham   PM / devfreq: Add...
63
64
65
66
  		return 0;
  	}
  
  	/* Keep the current frequency */
08e75e754   Javi Merino   PM / devfreq: cac...
67
68
69
  	if (stat->busy_time * 100 >
  	    stat->total_time * (dfso_upthreshold - dfso_downdifferential)) {
  		*freq = stat->current_frequency;
ce26c5bb9   MyungJoo Ham   PM / devfreq: Add...
70
71
72
73
  		return 0;
  	}
  
  	/* Set the desired frequency based on the load */
08e75e754   Javi Merino   PM / devfreq: cac...
74
75
76
  	a = stat->busy_time;
  	a *= stat->current_frequency;
  	b = div_u64(a, stat->total_time);
ce26c5bb9   MyungJoo Ham   PM / devfreq: Add...
77
78
79
80
81
82
  	b *= 100;
  	b = div_u64(b, (dfso_upthreshold - dfso_downdifferential / 2));
  	*freq = (unsigned long) b;
  
  	return 0;
  }
7e6fdd4ba   Rajagopal Venkat   PM / devfreq: Cor...
83
84
85
86
87
88
89
90
91
92
93
  static int devfreq_simple_ondemand_handler(struct devfreq *devfreq,
  				unsigned int event, void *data)
  {
  	switch (event) {
  	case DEVFREQ_GOV_START:
  		devfreq_monitor_start(devfreq);
  		break;
  
  	case DEVFREQ_GOV_STOP:
  		devfreq_monitor_stop(devfreq);
  		break;
3a1ec2e8d   Chanwoo Choi   PM / devfreq: Cha...
94
95
  	case DEVFREQ_GOV_UPDATE_INTERVAL:
  		devfreq_update_interval(devfreq, (unsigned int *)data);
7e6fdd4ba   Rajagopal Venkat   PM / devfreq: Cor...
96
  		break;
206c30cfe   Rajagopal Venkat   PM / devfreq: Add...
97
98
99
100
101
102
103
104
  
  	case DEVFREQ_GOV_SUSPEND:
  		devfreq_monitor_suspend(devfreq);
  		break;
  
  	case DEVFREQ_GOV_RESUME:
  		devfreq_monitor_resume(devfreq);
  		break;
7e6fdd4ba   Rajagopal Venkat   PM / devfreq: Cor...
105
106
107
108
109
110
  	default:
  		break;
  	}
  
  	return 0;
  }
1b5c1be2c   Nishanth Menon   PM / devfreq: map...
111
  static struct devfreq_governor devfreq_simple_ondemand = {
aa7c352f9   Chanwoo Choi   PM / devfreq: Def...
112
  	.name = DEVFREQ_GOV_SIMPLE_ONDEMAND,
ce26c5bb9   MyungJoo Ham   PM / devfreq: Add...
113
  	.get_target_freq = devfreq_simple_ondemand_func,
7e6fdd4ba   Rajagopal Venkat   PM / devfreq: Cor...
114
  	.event_handler = devfreq_simple_ondemand_handler,
ce26c5bb9   MyungJoo Ham   PM / devfreq: Add...
115
  };
83116e66a   Nishanth Menon   PM / devfreq: reg...
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
  
  static int __init devfreq_simple_ondemand_init(void)
  {
  	return devfreq_add_governor(&devfreq_simple_ondemand);
  }
  subsys_initcall(devfreq_simple_ondemand_init);
  
  static void __exit devfreq_simple_ondemand_exit(void)
  {
  	int ret;
  
  	ret = devfreq_remove_governor(&devfreq_simple_ondemand);
  	if (ret)
  		pr_err("%s: failed remove governor %d
  ", __func__, ret);
  
  	return;
  }
  module_exit(devfreq_simple_ondemand_exit);
eff607fdb   Nishanth Menon   PM / devfreq: gov...
135
  MODULE_LICENSE("GPL");