summaryrefslogtreecommitdiff
path: root/drivers/cpuquiet/driver.c
blob: d9dbea76994a4433c33744f0aafae51fef4b0314 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
/*
 * Copyright (c) 2012 NVIDIA CORPORATION.  All rights reserved.
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; version 2 of the License.
 *
 * This program is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for
 * more details.
 *
 * You should have received a copy of the GNU General Public License along
 * with this program; if not, write to the Free Software Foundation, Inc.,
 * 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
 *
 */

#include <linux/mutex.h>
#include <linux/module.h>
#include <linux/cpuquiet.h>
#include <linux/cpu.h>
#include <linux/jiffies.h>
#include <linux/slab.h>
#include <asm/cputime.h>

#include "cpuquiet.h"

struct cpuquiet_cpu_stat {
	cputime64_t time_up_total;
	u64 last_update;
	unsigned int up_down_count;
	struct kobject cpu_kobject;
};

struct cpu_attribute {
	struct attribute attr;
	enum { up_down_count, time_up_total } type;
};

static struct cpuquiet_driver *cpuquiet_curr_driver;
struct cpuquiet_cpu_stat *stats;

#define CPU_ATTRIBUTE(_name) \
	static struct cpu_attribute _name ## _attr = {			\
		.attr =  {.name = __stringify(_name), .mode = 0444 },	\
		.type	= _name,					\
}

CPU_ATTRIBUTE(up_down_count);
CPU_ATTRIBUTE(time_up_total);

static struct attribute *cpu_attributes[] = {
	&up_down_count_attr.attr,
	&time_up_total_attr.attr,
	NULL,
};

static void stats_update(struct cpuquiet_cpu_stat *stat, bool up)
{
	u64 cur_jiffies = get_jiffies_64();
	bool was_up = stat->up_down_count & 0x1;

	if (was_up)
		stat->time_up_total = cputime64_add(stat->time_up_total,
			cputime64_sub(cur_jiffies, stat->last_update));

	if (was_up != up)
		stat->up_down_count++;

	stat->last_update = cur_jiffies;
}

int cpuquiet_quiesence_cpu(unsigned int cpunumber)
{
	int err = -EPERM;

	if (cpuquiet_curr_driver && cpuquiet_curr_driver->quiesence_cpu)
		err = cpuquiet_curr_driver->quiesence_cpu(cpunumber);

	if (!err)
		stats_update(stats + cpunumber, 0);

	return err;
}
EXPORT_SYMBOL(cpuquiet_quiesence_cpu);

int cpuquiet_wake_cpu(unsigned int cpunumber)
{
	int err = -EPERM;

	if (cpuquiet_curr_driver && cpuquiet_curr_driver->wake_cpu)
		err = cpuquiet_curr_driver->wake_cpu(cpunumber);

	if (!err)
		stats_update(stats + cpunumber, 1);

	return err;
}
EXPORT_SYMBOL(cpuquiet_wake_cpu);

static ssize_t stats_sysfs_show(struct kobject *kobj,
			struct attribute *attr, char *buf)
{
	struct cpu_attribute *cattr =
		container_of(attr, struct cpu_attribute, attr);
	struct cpuquiet_cpu_stat *stat  =
		container_of(kobj, struct cpuquiet_cpu_stat, cpu_kobject);
	ssize_t len = 0;
	bool was_up = stat->up_down_count & 0x1;

	stats_update(stat, was_up);

	switch (cattr->type) {
	case up_down_count:
		len = sprintf(buf, "%u\n", stat->up_down_count);
		break;
	case time_up_total:
		len =  sprintf(buf, "%llu\n", stat->time_up_total);
		break;
	}

	return len;
}

static const struct sysfs_ops stats_sysfs_ops = {
	.show = stats_sysfs_show,
};

static struct kobj_type ktype_cpu_stats = {
	.sysfs_ops = &stats_sysfs_ops,
	.default_attrs = cpu_attributes,
};

int cpuquiet_register_driver(struct cpuquiet_driver *drv)
{
	int err = -EBUSY;
	unsigned int cpu;
	struct sys_device *sys_dev;
	u64 cur_jiffies;

	if (!drv)
		return -EINVAL;

	stats = kzalloc(nr_cpu_ids * sizeof(*stats), GFP_KERNEL);
	if (!stats)
		return -ENOMEM;

	for_each_possible_cpu(cpu) {
		cur_jiffies = get_jiffies_64();
		stats[cpu].last_update = cur_jiffies;
		if (cpu_online(cpu))
			stats[cpu].up_down_count = 1;
		sys_dev = get_cpu_sysdev(cpu);
		if (sys_dev) {
			cpuquiet_add_dev(sys_dev, cpu);
			cpuquiet_cpu_kobject_init(&stats[cpu].cpu_kobject,
					&ktype_cpu_stats, "stats", cpu);
		}
	}

	mutex_lock(&cpuquiet_lock);
	if (!cpuquiet_curr_driver) {
		err = 0;
		cpuquiet_curr_driver = drv;
		cpuquiet_switch_governor(cpuquiet_get_first_governor());
	}
	mutex_unlock(&cpuquiet_lock);

	return err;
}
EXPORT_SYMBOL(cpuquiet_register_driver);

struct cpuquiet_driver *cpuquiet_get_driver(void)
{
	return cpuquiet_curr_driver;
}

void cpuquiet_unregister_driver(struct cpuquiet_driver *drv)
{
	unsigned int cpu;

	if (drv != cpuquiet_curr_driver) {
		WARN(1, "invalid cpuquiet_unregister_driver(%s)\n",
			drv->name);
		return;
	}

	/* stop current governor first */
	cpuquiet_switch_governor(NULL);

	mutex_lock(&cpuquiet_lock);
	cpuquiet_curr_driver = NULL;

	for_each_possible_cpu(cpu) {
		kobject_put(&stats[cpu].cpu_kobject);
		cpuquiet_remove_dev(cpu);
	}

	mutex_unlock(&cpuquiet_lock);
}
EXPORT_SYMBOL(cpuquiet_unregister_driver);