summaryrefslogtreecommitdiffstats
blob: 5ff0dd07d8a3b93aebe7e974b10255d88f510c0f (plain) (blame)
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
/*
 * Copyright (C) 2012 Texas Instruments, Inc
 *
 * This program is free software; you can redistribute it and/or modify it
 * under the terms of the GNU General Public License version 2 as published by
 * the Free Software Foundation.
 *
 * 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, see <http://www.gnu.org/licenses/>.
 */

#include <linux/sysfs.h>
#include "sgxfreq.h"


static int userspace_start(struct sgxfreq_sgx_data *data);
static void userspace_stop(void);
static void userspace_sgx_clk_on(void);
static void userspace_sgx_clk_off(void);
static void userspace_sgx_active(void);
static void userspace_sgx_idle(void);


static struct sgxfreq_governor userspace_gov = {
	.name =	"userspace",
	.gov_start = userspace_start,
	.gov_stop = userspace_stop,
	.sgx_clk_on = userspace_sgx_clk_on,
	.sgx_clk_off = userspace_sgx_clk_off,
	.sgx_active = userspace_sgx_active,
	.sgx_idle = userspace_sgx_idle,
};


static struct userspace_data {
	unsigned long freq_user; /* in KHz */
	struct mutex mutex;
} usd;


/*********************** begin sysfs interface ***********************/

extern struct kobject *sgxfreq_kobj;


static ssize_t show_frequency_set(struct device *dev, struct device_attribute *attr,
			    char *buf)
{
	return sprintf(buf, "%lu\n", usd.freq_user);
}


static ssize_t store_frequency_set(struct device *dev,
	struct device_attribute *attr, const char *buf, size_t count)
{
	int ret;
	unsigned long freq;

	ret = sscanf(buf, "%lu", &freq);
	if (ret != 1)
		return -EINVAL;

	mutex_lock(&odd.mutex);

	if (freq > sgxfreq_get_freq_max())
		freq = sgxfreq_get_freq_max();
	usd.freq_user = sgxfreq_set_freq_request(freq);
	trace_printk("USERSPACE: new freq=%luHz.\n", usd.freq_user);

	mutex_unlock(&odd.mutex);

	return count;
}


static DEVICE_ATTR(frequency_set, 0644,
	show_frequency_set, store_frequency_set);


static struct attribute *userspace_attributes[] = {
	&dev_attr_frequency_set.attr,
	NULL
};


static struct attribute_group userspace_attr_group = {
	.attrs = userspace_attributes,
	.name = "userspace",
};

/************************ end sysfs interface ************************/


int userspace_init(void)
{
	int ret;

	mutex_init(&odd.mutex);

	ret = sgxfreq_register_governor(&userspace_gov);
	if (ret)
		return ret;
	return 0;
}


int userspace_deinit(void)
{
	return 0;
}


static int userspace_start(struct sgxfreq_sgx_data *data)
{
	int ret;

	usd.freq_user = sgxfreq_get_freq();

	ret = sysfs_create_group(sgxfreq_kobj, &userspace_attr_group);
	if (ret)
		return ret;

	trace_printk("USERSPACE: started.\n");

	return 0;
}


static void userspace_stop(void)
{
	usd.freq_user = sgxfreq_set_freq_request(sgxfreq_get_freq_min());
	sysfs_remove_group(sgxfreq_kobj, &userspace_attr_group);

	trace_printk("USERSPACE: stopped.\n");
}


static void userspace_sgx_clk_on(void)
{
	mutex_lock(&ood.mutex);

	sgxfreq_set_freq_request(usd.freq_user);

	mutex_unlock(&ood.mutex);
}


static void userspace_sgx_clk_off(void)
{
	mutex_lock(&ood.mutex);

	sgxfreq_set_freq_request(sgxfreq_get_freq_min());

	mutex_unlock(&ood.mutex);
}


static void userspace_sgx_active(void)
{
	mutex_lock(&aid.mutex);

	sgxfreq_set_freq_request(usd.freq_user);

	mutex_unlock(&aid.mutex);
}


static void userspace_sgx_idle(void)
{
	mutex_lock(&aid.mutex);

	sgxfreq_set_freq_request(sgxfreq_get_freq_min());

	mutex_unlock(&aid.mutex);
}