blob: 34eb214b6d57a4597727f7d21c8791cc38faa276 [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>
Viresh Kumar5ff0a262013-08-06 22:53:03 +053023#include <linux/kernel_stat.h>
24#include <linux/module.h>
Viresh Kumar4471a342012-10-26 00:47:42 +020025#include <linux/mutex.h>
Viresh Kumar4471a342012-10-26 00:47:42 +020026
Viresh Kumar4471a342012-10-26 00:47:42 +020027/* Ondemand Sampling types */
28enum {OD_NORMAL_SAMPLE, OD_SUB_SAMPLE};
29
Viresh Kumar4471a342012-10-26 00:47:42 +020030/*
31 * Abbreviations:
32 * dbs: used as a shortform for demand based switching It helps to keep variable
33 * names smaller, simpler
34 * cdbs: common dbs
Namhyung Kime5dde922013-02-28 05:38:00 +000035 * od_*: On-demand governor
Viresh Kumar4471a342012-10-26 00:47:42 +020036 * cs_*: Conservative governor
37 */
38
Rafael J. Wysockibc505472016-02-07 16:24:26 +010039/* Governor demand based switching data (per-policy or global). */
40struct dbs_data {
Rafael J. Wysocki0dd3c1d2016-03-22 02:47:51 +010041 struct gov_attr_set attr_set;
Rafael J. Wysockibc505472016-02-07 16:24:26 +010042 void *tuners;
Viresh Kumarff4b1782016-02-09 09:01:32 +053043 unsigned int min_sampling_rate;
44 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 */
88 struct mutex timer_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;
Rafael J. Wysockie4db2812016-02-15 02:13:42 +0100100 /* Status indicators */
101 bool is_shared; /* This object is used by multiple CPUs */
102 bool work_in_progress; /* Work is being queued up or in progress */
Viresh Kumar44152cb2015-07-18 11:30:59 +0530103};
104
Rafael J. Wysockie40e7b22016-02-10 17:07:44 +0100105static inline void gov_update_sample_delay(struct policy_dbs_info *policy_dbs,
Rafael J. Wysocki9be4fd22016-02-10 16:53:50 +0100106 unsigned int delay_us)
107{
Rafael J. Wysockie40e7b22016-02-10 17:07:44 +0100108 policy_dbs->sample_delay_ns = delay_us * NSEC_PER_USEC;
Rafael J. Wysocki9be4fd22016-02-10 16:53:50 +0100109}
110
Viresh Kumar4471a342012-10-26 00:47:42 +0200111/* Per cpu structures */
Viresh Kumar875b8502015-06-19 17:18:03 +0530112struct cpu_dbs_info {
Viresh Kumar1e7586a2012-10-26 00:51:21 +0200113 u64 prev_cpu_idle;
Rafael J. Wysockib4f4b4b2016-04-28 01:19:03 +0200114 u64 prev_update_time;
Viresh Kumar1e7586a2012-10-26 00:51:21 +0200115 u64 prev_cpu_nice;
Srivatsa S. Bhat18b46ab2014-06-08 02:11:43 +0530116 /*
Viresh Kumarc8ae4812014-06-09 14:21:24 +0530117 * Used to keep track of load in the previous interval. However, when
118 * explicitly set to zero, it is used as a flag to ensure that we copy
119 * the previous load to the current interval only once, upon the first
120 * wake-up from idle.
Srivatsa S. Bhat18b46ab2014-06-08 02:11:43 +0530121 */
Viresh Kumarc8ae4812014-06-09 14:21:24 +0530122 unsigned int prev_load;
Rafael J. Wysocki9be4fd22016-02-10 16:53:50 +0100123 struct update_util_data update_util;
Rafael J. Wysockie40e7b22016-02-10 17:07:44 +0100124 struct policy_dbs_info *policy_dbs;
Viresh Kumar4471a342012-10-26 00:47:42 +0200125};
126
Stratos Karafotisc4afc412013-08-26 21:42:21 +0300127/* Common Governor data across policies */
Rafael J. Wysocki7bdad342016-02-07 16:05:07 +0100128struct dbs_governor {
Rafael J. Wysockiaf926182016-02-05 03:16:08 +0100129 struct cpufreq_governor gov;
Viresh Kumarc4435632016-02-09 09:01:33 +0530130 struct kobj_type kobj_type;
Viresh Kumar4471a342012-10-26 00:47:42 +0200131
Viresh Kumar0b981e72013-10-02 14:13:18 +0530132 /*
133 * Common data for platforms that don't set
134 * CPUFREQ_HAVE_GOVERNOR_PER_POLICY
135 */
Viresh Kumar4d5dcc42013-03-27 15:58:58 +0000136 struct dbs_data *gdbs_data;
Viresh Kumar4471a342012-10-26 00:47:42 +0200137
Rafael J. Wysocki9be4fd22016-02-10 16:53:50 +0100138 unsigned int (*gov_dbs_timer)(struct cpufreq_policy *policy);
Rafael J. Wysocki7d5a9952016-02-18 18:40:14 +0100139 struct policy_dbs_info *(*alloc)(void);
140 void (*free)(struct policy_dbs_info *policy_dbs);
Viresh Kumar8e0484d2015-06-03 15:57:11 +0530141 int (*init)(struct dbs_data *dbs_data, bool notify);
142 void (*exit)(struct dbs_data *dbs_data, bool notify);
Rafael J. Wysocki702c9e52016-02-18 02:21:21 +0100143 void (*start)(struct cpufreq_policy *policy);
Viresh Kumar4471a342012-10-26 00:47:42 +0200144};
145
Rafael J. Wysockiea59ee0d2016-02-07 16:09:51 +0100146static inline struct dbs_governor *dbs_governor_of(struct cpufreq_policy *policy)
147{
148 return container_of(policy->governor, struct dbs_governor, gov);
149}
150
Rafael J. Wysocki8434dad2016-02-18 02:22:42 +0100151/* Governor specific operations */
Viresh Kumar4471a342012-10-26 00:47:42 +0200152struct od_ops {
Viresh Kumar4471a342012-10-26 00:47:42 +0200153 unsigned int (*powersave_bias_target)(struct cpufreq_policy *policy,
154 unsigned int freq_next, unsigned int relation);
Viresh Kumar4471a342012-10-26 00:47:42 +0200155};
156
Rafael J. Wysocki4cccf752016-02-15 02:19:31 +0100157unsigned int dbs_update(struct cpufreq_policy *policy);
Rafael J. Wysocki906a6e52016-02-07 16:07:51 +0100158int cpufreq_governor_dbs(struct cpufreq_policy *policy, unsigned int event);
Jacob Shinfb308092013-04-02 09:56:56 -0500159void od_register_powersave_bias_handler(unsigned int (*f)
160 (struct cpufreq_policy *, unsigned int, unsigned int),
161 unsigned int powersave_bias);
162void od_unregister_powersave_bias_handler(void);
Rafael J. Wysocki0dd3c1d2016-03-22 02:47:51 +0100163ssize_t store_sampling_rate(struct gov_attr_set *attr_set, const char *buf,
Viresh Kumaraded3872016-02-11 17:31:15 +0530164 size_t count);
Rafael J. Wysocki8c8f77f2016-02-21 00:51:27 +0100165void gov_update_cpu_data(struct dbs_data *dbs_data);
Borislav Petkovbeb0ff32013-04-02 12:26:15 +0000166#endif /* _CPUFREQ_GOVERNOR_H */