blob: 0236ec2cd654b34bb9fb1e42d416138f63619b10 [file] [log] [blame]
Viresh Kumar4471a342012-10-26 00:47:42 +02001/*
2 * drivers/cpufreq/cpufreq_governor.h
3 *
4 * Header file for CPUFreq governors common code
5 *
6 * Copyright (C) 2001 Russell King
7 * (C) 2003 Venkatesh Pallipadi <venkatesh.pallipadi@intel.com>.
8 * (C) 2003 Jun Nakajima <jun.nakajima@intel.com>
9 * (C) 2009 Alexander Clouter <alex@digriz.org.uk>
10 * (c) 2012 Viresh Kumar <viresh.kumar@linaro.org>
11 *
12 * This program is free software; you can redistribute it and/or modify
13 * it under the terms of the GNU General Public License version 2 as
14 * published by the Free Software Foundation.
15 */
16
Borislav Petkovbeb0ff32013-04-02 12:26:15 +000017#ifndef _CPUFREQ_GOVERNOR_H
18#define _CPUFREQ_GOVERNOR_H
Viresh Kumar4471a342012-10-26 00:47:42 +020019
Rafael J. Wysocki2dd3e722015-12-08 21:44:05 +010020#include <linux/atomic.h>
Rafael J. Wysocki9be4fd22016-02-10 16:53:50 +010021#include <linux/irq_work.h>
Viresh Kumar4471a342012-10-26 00:47:42 +020022#include <linux/cpufreq.h>
Ingo Molnar55687da2017-02-08 18:51:31 +010023#include <linux/sched/cpufreq.h>
Viresh Kumar5ff0a262013-08-06 22:53:03 +053024#include <linux/kernel_stat.h>
25#include <linux/module.h>
Viresh Kumar4471a342012-10-26 00:47:42 +020026#include <linux/mutex.h>
Viresh Kumar4471a342012-10-26 00:47:42 +020027
Viresh Kumar4471a342012-10-26 00:47:42 +020028/* Ondemand Sampling types */
29enum {OD_NORMAL_SAMPLE, OD_SUB_SAMPLE};
30
Viresh Kumar4471a342012-10-26 00:47:42 +020031/*
32 * Abbreviations:
33 * dbs: used as a shortform for demand based switching It helps to keep variable
34 * names smaller, simpler
35 * cdbs: common dbs
Namhyung Kime5dde922013-02-28 05:38:00 +000036 * od_*: On-demand governor
Viresh Kumar4471a342012-10-26 00:47:42 +020037 * cs_*: Conservative governor
38 */
39
Rafael J. Wysockibc505472016-02-07 16:24:26 +010040/* Governor demand based switching data (per-policy or global). */
41struct dbs_data {
Rafael J. Wysocki0dd3c1d2016-03-22 02:47:51 +010042 struct gov_attr_set attr_set;
Rafael J. Wysockibc505472016-02-07 16:24:26 +010043 void *tuners;
Viresh Kumarff4b1782016-02-09 09:01:32 +053044 unsigned int min_sampling_rate;
45 unsigned int ignore_nice_load;
46 unsigned int sampling_rate;
47 unsigned int sampling_down_factor;
48 unsigned int up_threshold;
Rafael J. Wysocki8847e032016-02-18 02:20:13 +010049 unsigned int io_is_busy;
Rafael J. Wysockibc505472016-02-07 16:24:26 +010050};
51
Rafael J. Wysocki0dd3c1d2016-03-22 02:47:51 +010052static inline struct dbs_data *to_dbs_data(struct gov_attr_set *attr_set)
53{
54 return container_of(attr_set, struct dbs_data, attr_set);
55}
56
Viresh Kumarc4435632016-02-09 09:01:33 +053057#define gov_show_one(_gov, file_name) \
58static ssize_t show_##file_name \
Rafael J. Wysocki0dd3c1d2016-03-22 02:47:51 +010059(struct gov_attr_set *attr_set, char *buf) \
Viresh Kumarc4435632016-02-09 09:01:33 +053060{ \
Rafael J. Wysocki0dd3c1d2016-03-22 02:47:51 +010061 struct dbs_data *dbs_data = to_dbs_data(attr_set); \
Viresh Kumarc4435632016-02-09 09:01:33 +053062 struct _gov##_dbs_tuners *tuners = dbs_data->tuners; \
63 return sprintf(buf, "%u\n", tuners->file_name); \
64}
65
66#define gov_show_one_common(file_name) \
67static ssize_t show_##file_name \
Rafael J. Wysocki0dd3c1d2016-03-22 02:47:51 +010068(struct gov_attr_set *attr_set, char *buf) \
Viresh Kumarc4435632016-02-09 09:01:33 +053069{ \
Rafael J. Wysocki0dd3c1d2016-03-22 02:47:51 +010070 struct dbs_data *dbs_data = to_dbs_data(attr_set); \
Viresh Kumarc4435632016-02-09 09:01:33 +053071 return sprintf(buf, "%u\n", dbs_data->file_name); \
72}
73
74#define gov_attr_ro(_name) \
75static struct governor_attr _name = \
76__ATTR(_name, 0444, show_##_name, NULL)
77
78#define gov_attr_rw(_name) \
79static struct governor_attr _name = \
80__ATTR(_name, 0644, show_##_name, store_##_name)
81
Viresh Kumar44152cb2015-07-18 11:30:59 +053082/* Common to all CPUs of a policy */
Rafael J. Wysockie40e7b22016-02-10 17:07:44 +010083struct policy_dbs_info {
Viresh Kumar44152cb2015-07-18 11:30:59 +053084 struct cpufreq_policy *policy;
85 /*
Viresh Kumar70f43e52015-12-09 07:34:42 +053086 * Per policy mutex that serializes load evaluation from limit-change
87 * and work-handler.
Viresh Kumar44152cb2015-07-18 11:30:59 +053088 */
Viresh Kumar26f0dbc2016-11-08 11:06:33 +053089 struct mutex update_mutex;
Viresh Kumar70f43e52015-12-09 07:34:42 +053090
Rafael J. Wysocki9be4fd22016-02-10 16:53:50 +010091 u64 last_sample_time;
92 s64 sample_delay_ns;
Rafael J. Wysocki686cc632016-02-08 23:41:10 +010093 atomic_t work_count;
Rafael J. Wysocki9be4fd22016-02-10 16:53:50 +010094 struct irq_work irq_work;
Viresh Kumar70f43e52015-12-09 07:34:42 +053095 struct work_struct work;
Rafael J. Wysockibc505472016-02-07 16:24:26 +010096 /* dbs_data may be shared between multiple policy objects */
97 struct dbs_data *dbs_data;
Viresh Kumarc54df072016-02-10 11:00:25 +053098 struct list_head list;
Rafael J. Wysocki57dc3bc2016-02-15 02:20:51 +010099 /* Multiplier for increasing sample delay temporarily. */
100 unsigned int rate_mult;
Stratos Karafotis00bfe052016-11-16 19:26:29 +0200101 unsigned int idle_periods; /* For conservative */
Rafael J. Wysockie4db2812016-02-15 02:13:42 +0100102 /* Status indicators */
103 bool is_shared; /* This object is used by multiple CPUs */
104 bool work_in_progress; /* Work is being queued up or in progress */
Viresh Kumar44152cb2015-07-18 11:30:59 +0530105};
106
Rafael J. Wysockie40e7b22016-02-10 17:07:44 +0100107static inline void gov_update_sample_delay(struct policy_dbs_info *policy_dbs,
Rafael J. Wysocki9be4fd22016-02-10 16:53:50 +0100108 unsigned int delay_us)
109{
Rafael J. Wysockie40e7b22016-02-10 17:07:44 +0100110 policy_dbs->sample_delay_ns = delay_us * NSEC_PER_USEC;
Rafael J. Wysocki9be4fd22016-02-10 16:53:50 +0100111}
112
Viresh Kumar4471a342012-10-26 00:47:42 +0200113/* Per cpu structures */
Viresh Kumar875b8502015-06-19 17:18:03 +0530114struct cpu_dbs_info {
Viresh Kumar1e7586a2012-10-26 00:51:21 +0200115 u64 prev_cpu_idle;
Rafael J. Wysockib4f4b4b2016-04-28 01:19:03 +0200116 u64 prev_update_time;
Viresh Kumar1e7586a2012-10-26 00:51:21 +0200117 u64 prev_cpu_nice;
Srivatsa S. Bhat18b46ab2014-06-08 02:11:43 +0530118 /*
Viresh Kumarc8ae4812014-06-09 14:21:24 +0530119 * Used to keep track of load in the previous interval. However, when
120 * explicitly set to zero, it is used as a flag to ensure that we copy
121 * the previous load to the current interval only once, upon the first
122 * wake-up from idle.
Srivatsa S. Bhat18b46ab2014-06-08 02:11:43 +0530123 */
Viresh Kumarc8ae4812014-06-09 14:21:24 +0530124 unsigned int prev_load;
Rafael J. Wysocki9be4fd22016-02-10 16:53:50 +0100125 struct update_util_data update_util;
Rafael J. Wysockie40e7b22016-02-10 17:07:44 +0100126 struct policy_dbs_info *policy_dbs;
Viresh Kumar4471a342012-10-26 00:47:42 +0200127};
128
Stratos Karafotisc4afc412013-08-26 21:42:21 +0300129/* Common Governor data across policies */
Rafael J. Wysocki7bdad342016-02-07 16:05:07 +0100130struct dbs_governor {
Rafael J. Wysockiaf926182016-02-05 03:16:08 +0100131 struct cpufreq_governor gov;
Viresh Kumarc4435632016-02-09 09:01:33 +0530132 struct kobj_type kobj_type;
Viresh Kumar4471a342012-10-26 00:47:42 +0200133
Viresh Kumar0b981e72013-10-02 14:13:18 +0530134 /*
135 * Common data for platforms that don't set
136 * CPUFREQ_HAVE_GOVERNOR_PER_POLICY
137 */
Viresh Kumar4d5dcc42013-03-27 15:58:58 +0000138 struct dbs_data *gdbs_data;
Viresh Kumar4471a342012-10-26 00:47:42 +0200139
Viresh Kumar26f0dbc2016-11-08 11:06:33 +0530140 unsigned int (*gov_dbs_update)(struct cpufreq_policy *policy);
Rafael J. Wysocki7d5a9952016-02-18 18:40:14 +0100141 struct policy_dbs_info *(*alloc)(void);
142 void (*free)(struct policy_dbs_info *policy_dbs);
Rafael J. Wysocki9a15fb22016-05-18 22:59:49 +0200143 int (*init)(struct dbs_data *dbs_data);
144 void (*exit)(struct dbs_data *dbs_data);
Rafael J. Wysocki702c9e52016-02-18 02:21:21 +0100145 void (*start)(struct cpufreq_policy *policy);
Viresh Kumar4471a342012-10-26 00:47:42 +0200146};
147
Rafael J. Wysockiea59ee0d2016-02-07 16:09:51 +0100148static inline struct dbs_governor *dbs_governor_of(struct cpufreq_policy *policy)
149{
150 return container_of(policy->governor, struct dbs_governor, gov);
151}
152
Rafael J. Wysockie7888922016-06-02 23:24:15 +0200153/* Governor callback routines */
154int cpufreq_dbs_governor_init(struct cpufreq_policy *policy);
155void cpufreq_dbs_governor_exit(struct cpufreq_policy *policy);
156int cpufreq_dbs_governor_start(struct cpufreq_policy *policy);
157void cpufreq_dbs_governor_stop(struct cpufreq_policy *policy);
158void cpufreq_dbs_governor_limits(struct cpufreq_policy *policy);
159
160#define CPUFREQ_DBS_GOVERNOR_INITIALIZER(_name_) \
161 { \
162 .name = _name_, \
163 .max_transition_latency = TRANSITION_LATENCY_LIMIT, \
164 .owner = THIS_MODULE, \
165 .init = cpufreq_dbs_governor_init, \
166 .exit = cpufreq_dbs_governor_exit, \
167 .start = cpufreq_dbs_governor_start, \
168 .stop = cpufreq_dbs_governor_stop, \
169 .limits = cpufreq_dbs_governor_limits, \
170 }
171
Rafael J. Wysocki8434dad2016-02-18 02:22:42 +0100172/* Governor specific operations */
Viresh Kumar4471a342012-10-26 00:47:42 +0200173struct od_ops {
Viresh Kumar4471a342012-10-26 00:47:42 +0200174 unsigned int (*powersave_bias_target)(struct cpufreq_policy *policy,
175 unsigned int freq_next, unsigned int relation);
Viresh Kumar4471a342012-10-26 00:47:42 +0200176};
177
Rafael J. Wysocki4cccf752016-02-15 02:19:31 +0100178unsigned int dbs_update(struct cpufreq_policy *policy);
Jacob Shinfb308092013-04-02 09:56:56 -0500179void od_register_powersave_bias_handler(unsigned int (*f)
180 (struct cpufreq_policy *, unsigned int, unsigned int),
181 unsigned int powersave_bias);
182void od_unregister_powersave_bias_handler(void);
Rafael J. Wysocki0dd3c1d2016-03-22 02:47:51 +0100183ssize_t store_sampling_rate(struct gov_attr_set *attr_set, const char *buf,
Viresh Kumaraded3872016-02-11 17:31:15 +0530184 size_t count);
Rafael J. Wysocki8c8f77f2016-02-21 00:51:27 +0100185void gov_update_cpu_data(struct dbs_data *dbs_data);
Borislav Petkovbeb0ff32013-04-02 12:26:15 +0000186#endif /* _CPUFREQ_GOVERNOR_H */