blob: 8463f5def0f596fb3424f6e2a25bb775460e05c7 [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 ignore_nice_load;
45 unsigned int sampling_rate;
46 unsigned int sampling_down_factor;
47 unsigned int up_threshold;
Rafael J. Wysocki8847e032016-02-18 02:20:13 +010048 unsigned int io_is_busy;
Rafael J. Wysockibc505472016-02-07 16:24:26 +010049};
50
Rafael J. Wysocki0dd3c1d2016-03-22 02:47:51 +010051static inline struct dbs_data *to_dbs_data(struct gov_attr_set *attr_set)
52{
53 return container_of(attr_set, struct dbs_data, attr_set);
54}
55
Viresh Kumarc4435632016-02-09 09:01:33 +053056#define gov_show_one(_gov, file_name) \
57static ssize_t show_##file_name \
Rafael J. Wysocki0dd3c1d2016-03-22 02:47:51 +010058(struct gov_attr_set *attr_set, char *buf) \
Viresh Kumarc4435632016-02-09 09:01:33 +053059{ \
Rafael J. Wysocki0dd3c1d2016-03-22 02:47:51 +010060 struct dbs_data *dbs_data = to_dbs_data(attr_set); \
Viresh Kumarc4435632016-02-09 09:01:33 +053061 struct _gov##_dbs_tuners *tuners = dbs_data->tuners; \
62 return sprintf(buf, "%u\n", tuners->file_name); \
63}
64
65#define gov_show_one_common(file_name) \
66static ssize_t show_##file_name \
Rafael J. Wysocki0dd3c1d2016-03-22 02:47:51 +010067(struct gov_attr_set *attr_set, char *buf) \
Viresh Kumarc4435632016-02-09 09:01:33 +053068{ \
Rafael J. Wysocki0dd3c1d2016-03-22 02:47:51 +010069 struct dbs_data *dbs_data = to_dbs_data(attr_set); \
Viresh Kumarc4435632016-02-09 09:01:33 +053070 return sprintf(buf, "%u\n", dbs_data->file_name); \
71}
72
73#define gov_attr_ro(_name) \
74static struct governor_attr _name = \
75__ATTR(_name, 0444, show_##_name, NULL)
76
77#define gov_attr_rw(_name) \
78static struct governor_attr _name = \
79__ATTR(_name, 0644, show_##_name, store_##_name)
80
Viresh Kumar44152cb2015-07-18 11:30:59 +053081/* Common to all CPUs of a policy */
Rafael J. Wysockie40e7b22016-02-10 17:07:44 +010082struct policy_dbs_info {
Viresh Kumar44152cb2015-07-18 11:30:59 +053083 struct cpufreq_policy *policy;
84 /*
Viresh Kumar70f43e52015-12-09 07:34:42 +053085 * Per policy mutex that serializes load evaluation from limit-change
86 * and work-handler.
Viresh Kumar44152cb2015-07-18 11:30:59 +053087 */
Viresh Kumar26f0dbc2016-11-08 11:06:33 +053088 struct mutex update_mutex;
Viresh Kumar70f43e52015-12-09 07:34:42 +053089
Rafael J. Wysocki9be4fd22016-02-10 16:53:50 +010090 u64 last_sample_time;
91 s64 sample_delay_ns;
Rafael J. Wysocki686cc632016-02-08 23:41:10 +010092 atomic_t work_count;
Rafael J. Wysocki9be4fd22016-02-10 16:53:50 +010093 struct irq_work irq_work;
Viresh Kumar70f43e52015-12-09 07:34:42 +053094 struct work_struct work;
Rafael J. Wysockibc505472016-02-07 16:24:26 +010095 /* dbs_data may be shared between multiple policy objects */
96 struct dbs_data *dbs_data;
Viresh Kumarc54df072016-02-10 11:00:25 +053097 struct list_head list;
Rafael J. Wysocki57dc3bc2016-02-15 02:20:51 +010098 /* Multiplier for increasing sample delay temporarily. */
99 unsigned int rate_mult;
Stratos Karafotis00bfe052016-11-16 19:26:29 +0200100 unsigned int idle_periods; /* For conservative */
Rafael J. Wysockie4db2812016-02-15 02:13:42 +0100101 /* Status indicators */
102 bool is_shared; /* This object is used by multiple CPUs */
103 bool work_in_progress; /* Work is being queued up or in progress */
Viresh Kumar44152cb2015-07-18 11:30:59 +0530104};
105
Rafael J. Wysockie40e7b22016-02-10 17:07:44 +0100106static inline void gov_update_sample_delay(struct policy_dbs_info *policy_dbs,
Rafael J. Wysocki9be4fd22016-02-10 16:53:50 +0100107 unsigned int delay_us)
108{
Rafael J. Wysockie40e7b22016-02-10 17:07:44 +0100109 policy_dbs->sample_delay_ns = delay_us * NSEC_PER_USEC;
Rafael J. Wysocki9be4fd22016-02-10 16:53:50 +0100110}
111
Viresh Kumar4471a342012-10-26 00:47:42 +0200112/* Per cpu structures */
Viresh Kumar875b8502015-06-19 17:18:03 +0530113struct cpu_dbs_info {
Viresh Kumar1e7586a2012-10-26 00:51:21 +0200114 u64 prev_cpu_idle;
Rafael J. Wysockib4f4b4b2016-04-28 01:19:03 +0200115 u64 prev_update_time;
Viresh Kumar1e7586a2012-10-26 00:51:21 +0200116 u64 prev_cpu_nice;
Srivatsa S. Bhat18b46ab2014-06-08 02:11:43 +0530117 /*
Viresh Kumarc8ae4812014-06-09 14:21:24 +0530118 * Used to keep track of load in the previous interval. However, when
119 * explicitly set to zero, it is used as a flag to ensure that we copy
120 * the previous load to the current interval only once, upon the first
121 * wake-up from idle.
Srivatsa S. Bhat18b46ab2014-06-08 02:11:43 +0530122 */
Viresh Kumarc8ae4812014-06-09 14:21:24 +0530123 unsigned int prev_load;
Rafael J. Wysocki9be4fd22016-02-10 16:53:50 +0100124 struct update_util_data update_util;
Rafael J. Wysockie40e7b22016-02-10 17:07:44 +0100125 struct policy_dbs_info *policy_dbs;
Viresh Kumar4471a342012-10-26 00:47:42 +0200126};
127
Stratos Karafotisc4afc412013-08-26 21:42:21 +0300128/* Common Governor data across policies */
Rafael J. Wysocki7bdad342016-02-07 16:05:07 +0100129struct dbs_governor {
Rafael J. Wysockiaf926182016-02-05 03:16:08 +0100130 struct cpufreq_governor gov;
Viresh Kumarc4435632016-02-09 09:01:33 +0530131 struct kobj_type kobj_type;
Viresh Kumar4471a342012-10-26 00:47:42 +0200132
Viresh Kumar0b981e72013-10-02 14:13:18 +0530133 /*
134 * Common data for platforms that don't set
135 * CPUFREQ_HAVE_GOVERNOR_PER_POLICY
136 */
Viresh Kumar4d5dcc42013-03-27 15:58:58 +0000137 struct dbs_data *gdbs_data;
Viresh Kumar4471a342012-10-26 00:47:42 +0200138
Viresh Kumar26f0dbc2016-11-08 11:06:33 +0530139 unsigned int (*gov_dbs_update)(struct cpufreq_policy *policy);
Rafael J. Wysocki7d5a9952016-02-18 18:40:14 +0100140 struct policy_dbs_info *(*alloc)(void);
141 void (*free)(struct policy_dbs_info *policy_dbs);
Rafael J. Wysocki9a15fb22016-05-18 22:59:49 +0200142 int (*init)(struct dbs_data *dbs_data);
143 void (*exit)(struct dbs_data *dbs_data);
Rafael J. Wysocki702c9e52016-02-18 02:21:21 +0100144 void (*start)(struct cpufreq_policy *policy);
Viresh Kumar4471a342012-10-26 00:47:42 +0200145};
146
Rafael J. Wysockiea59ee0d2016-02-07 16:09:51 +0100147static inline struct dbs_governor *dbs_governor_of(struct cpufreq_policy *policy)
148{
149 return container_of(policy->governor, struct dbs_governor, gov);
150}
151
Rafael J. Wysockie7888922016-06-02 23:24:15 +0200152/* Governor callback routines */
153int cpufreq_dbs_governor_init(struct cpufreq_policy *policy);
154void cpufreq_dbs_governor_exit(struct cpufreq_policy *policy);
155int cpufreq_dbs_governor_start(struct cpufreq_policy *policy);
156void cpufreq_dbs_governor_stop(struct cpufreq_policy *policy);
157void cpufreq_dbs_governor_limits(struct cpufreq_policy *policy);
158
159#define CPUFREQ_DBS_GOVERNOR_INITIALIZER(_name_) \
160 { \
161 .name = _name_, \
Viresh Kumared4676e2017-07-19 15:42:46 +0530162 .dynamic_switching = true, \
Rafael J. Wysockie7888922016-06-02 23:24:15 +0200163 .owner = THIS_MODULE, \
164 .init = cpufreq_dbs_governor_init, \
165 .exit = cpufreq_dbs_governor_exit, \
166 .start = cpufreq_dbs_governor_start, \
167 .stop = cpufreq_dbs_governor_stop, \
168 .limits = cpufreq_dbs_governor_limits, \
169 }
170
Rafael J. Wysocki8434dad2016-02-18 02:22:42 +0100171/* Governor specific operations */
Viresh Kumar4471a342012-10-26 00:47:42 +0200172struct od_ops {
Viresh Kumar4471a342012-10-26 00:47:42 +0200173 unsigned int (*powersave_bias_target)(struct cpufreq_policy *policy,
174 unsigned int freq_next, unsigned int relation);
Viresh Kumar4471a342012-10-26 00:47:42 +0200175};
176
Rafael J. Wysocki4cccf752016-02-15 02:19:31 +0100177unsigned int dbs_update(struct cpufreq_policy *policy);
Jacob Shinfb308092013-04-02 09:56:56 -0500178void od_register_powersave_bias_handler(unsigned int (*f)
179 (struct cpufreq_policy *, unsigned int, unsigned int),
180 unsigned int powersave_bias);
181void od_unregister_powersave_bias_handler(void);
Rafael J. Wysocki0dd3c1d2016-03-22 02:47:51 +0100182ssize_t store_sampling_rate(struct gov_attr_set *attr_set, const char *buf,
Viresh Kumaraded3872016-02-11 17:31:15 +0530183 size_t count);
Rafael J. Wysocki8c8f77f2016-02-21 00:51:27 +0100184void gov_update_cpu_data(struct dbs_data *dbs_data);
Borislav Petkovbeb0ff32013-04-02 12:26:15 +0000185#endif /* _CPUFREQ_GOVERNOR_H */