blob: 0ba9c8b8ee743e960eed3db8e5d9108d7c5eeadc [file] [log] [blame]
Len Brown4f86d3a2007-10-03 18:58:00 -04001/*
2 * sysfs.c - sysfs support
3 *
4 * (C) 2006-2007 Shaohua Li <shaohua.li@intel.com>
5 *
6 * This code is licenced under the GPL.
7 */
8
9#include <linux/kernel.h>
10#include <linux/cpuidle.h>
11#include <linux/sysfs.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090012#include <linux/slab.h>
Len Brown4f86d3a2007-10-03 18:58:00 -040013#include <linux/cpu.h>
14
15#include "cpuidle.h"
16
17static unsigned int sysfs_switch;
18static int __init cpuidle_sysfs_setup(char *unused)
19{
20 sysfs_switch = 1;
21 return 1;
22}
23__setup("cpuidle_sysfs_switch", cpuidle_sysfs_setup);
24
Rabin Vincent66198f32008-08-12 15:08:45 -070025static ssize_t show_available_governors(struct sysdev_class *class,
Andi Kleenc9be0a32010-01-05 12:47:58 +010026 struct sysdev_class_attribute *attr,
Rabin Vincent66198f32008-08-12 15:08:45 -070027 char *buf)
Len Brown4f86d3a2007-10-03 18:58:00 -040028{
29 ssize_t i = 0;
30 struct cpuidle_governor *tmp;
31
32 mutex_lock(&cpuidle_lock);
33 list_for_each_entry(tmp, &cpuidle_governors, governor_list) {
34 if (i >= (ssize_t) ((PAGE_SIZE/sizeof(char)) - CPUIDLE_NAME_LEN - 2))
35 goto out;
36 i += scnprintf(&buf[i], CPUIDLE_NAME_LEN, "%s ", tmp->name);
37 }
38
39out:
40 i+= sprintf(&buf[i], "\n");
41 mutex_unlock(&cpuidle_lock);
42 return i;
43}
44
Rabin Vincent66198f32008-08-12 15:08:45 -070045static ssize_t show_current_driver(struct sysdev_class *class,
Andi Kleenc9be0a32010-01-05 12:47:58 +010046 struct sysdev_class_attribute *attr,
Rabin Vincent66198f32008-08-12 15:08:45 -070047 char *buf)
Len Brown4f86d3a2007-10-03 18:58:00 -040048{
49 ssize_t ret;
50
51 spin_lock(&cpuidle_driver_lock);
52 if (cpuidle_curr_driver)
53 ret = sprintf(buf, "%s\n", cpuidle_curr_driver->name);
54 else
55 ret = sprintf(buf, "none\n");
56 spin_unlock(&cpuidle_driver_lock);
57
58 return ret;
59}
60
Rabin Vincent66198f32008-08-12 15:08:45 -070061static ssize_t show_current_governor(struct sysdev_class *class,
Andi Kleenc9be0a32010-01-05 12:47:58 +010062 struct sysdev_class_attribute *attr,
Rabin Vincent66198f32008-08-12 15:08:45 -070063 char *buf)
Len Brown4f86d3a2007-10-03 18:58:00 -040064{
65 ssize_t ret;
66
67 mutex_lock(&cpuidle_lock);
68 if (cpuidle_curr_governor)
69 ret = sprintf(buf, "%s\n", cpuidle_curr_governor->name);
70 else
71 ret = sprintf(buf, "none\n");
72 mutex_unlock(&cpuidle_lock);
73
74 return ret;
75}
76
Rabin Vincent66198f32008-08-12 15:08:45 -070077static ssize_t store_current_governor(struct sysdev_class *class,
Andi Kleenc9be0a32010-01-05 12:47:58 +010078 struct sysdev_class_attribute *attr,
Rabin Vincent66198f32008-08-12 15:08:45 -070079 const char *buf, size_t count)
Len Brown4f86d3a2007-10-03 18:58:00 -040080{
81 char gov_name[CPUIDLE_NAME_LEN];
82 int ret = -EINVAL;
83 size_t len = count;
84 struct cpuidle_governor *gov;
85
86 if (!len || len >= sizeof(gov_name))
87 return -EINVAL;
88
89 memcpy(gov_name, buf, len);
90 gov_name[len] = '\0';
91 if (gov_name[len - 1] == '\n')
92 gov_name[--len] = '\0';
93
94 mutex_lock(&cpuidle_lock);
95
96 list_for_each_entry(gov, &cpuidle_governors, governor_list) {
97 if (strlen(gov->name) == len && !strcmp(gov->name, gov_name)) {
98 ret = cpuidle_switch_governor(gov);
99 break;
100 }
101 }
102
103 mutex_unlock(&cpuidle_lock);
104
105 if (ret)
106 return ret;
107 else
108 return count;
109}
110
Rabin Vincent66198f32008-08-12 15:08:45 -0700111static SYSDEV_CLASS_ATTR(current_driver, 0444, show_current_driver, NULL);
112static SYSDEV_CLASS_ATTR(current_governor_ro, 0444, show_current_governor,
113 NULL);
Len Brown4f86d3a2007-10-03 18:58:00 -0400114
115static struct attribute *cpuclass_default_attrs[] = {
116 &attr_current_driver.attr,
117 &attr_current_governor_ro.attr,
118 NULL
119};
120
Rabin Vincent66198f32008-08-12 15:08:45 -0700121static SYSDEV_CLASS_ATTR(available_governors, 0444, show_available_governors,
122 NULL);
123static SYSDEV_CLASS_ATTR(current_governor, 0644, show_current_governor,
124 store_current_governor);
Len Brown4f86d3a2007-10-03 18:58:00 -0400125
126static struct attribute *cpuclass_switch_attrs[] = {
127 &attr_available_governors.attr,
128 &attr_current_driver.attr,
129 &attr_current_governor.attr,
130 NULL
131};
132
133static struct attribute_group cpuclass_attr_group = {
134 .attrs = cpuclass_default_attrs,
135 .name = "cpuidle",
136};
137
138/**
139 * cpuidle_add_class_sysfs - add CPU global sysfs attributes
140 */
141int cpuidle_add_class_sysfs(struct sysdev_class *cls)
142{
143 if (sysfs_switch)
144 cpuclass_attr_group.attrs = cpuclass_switch_attrs;
145
146 return sysfs_create_group(&cls->kset.kobj, &cpuclass_attr_group);
147}
148
149/**
150 * cpuidle_remove_class_sysfs - remove CPU global sysfs attributes
151 */
152void cpuidle_remove_class_sysfs(struct sysdev_class *cls)
153{
154 sysfs_remove_group(&cls->kset.kobj, &cpuclass_attr_group);
155}
156
157struct cpuidle_attr {
158 struct attribute attr;
159 ssize_t (*show)(struct cpuidle_device *, char *);
160 ssize_t (*store)(struct cpuidle_device *, const char *, size_t count);
161};
162
163#define define_one_ro(_name, show) \
164 static struct cpuidle_attr attr_##_name = __ATTR(_name, 0444, show, NULL)
165#define define_one_rw(_name, show, store) \
166 static struct cpuidle_attr attr_##_name = __ATTR(_name, 0644, show, store)
167
168#define kobj_to_cpuidledev(k) container_of(k, struct cpuidle_device, kobj)
169#define attr_to_cpuidleattr(a) container_of(a, struct cpuidle_attr, attr)
170static ssize_t cpuidle_show(struct kobject * kobj, struct attribute * attr ,char * buf)
171{
172 int ret = -EIO;
173 struct cpuidle_device *dev = kobj_to_cpuidledev(kobj);
174 struct cpuidle_attr * cattr = attr_to_cpuidleattr(attr);
175
176 if (cattr->show) {
177 mutex_lock(&cpuidle_lock);
178 ret = cattr->show(dev, buf);
179 mutex_unlock(&cpuidle_lock);
180 }
181 return ret;
182}
183
184static ssize_t cpuidle_store(struct kobject * kobj, struct attribute * attr,
185 const char * buf, size_t count)
186{
187 int ret = -EIO;
188 struct cpuidle_device *dev = kobj_to_cpuidledev(kobj);
189 struct cpuidle_attr * cattr = attr_to_cpuidleattr(attr);
190
191 if (cattr->store) {
192 mutex_lock(&cpuidle_lock);
193 ret = cattr->store(dev, buf, count);
194 mutex_unlock(&cpuidle_lock);
195 }
196 return ret;
197}
198
Emese Revfy52cf25d2010-01-19 02:58:23 +0100199static const struct sysfs_ops cpuidle_sysfs_ops = {
Len Brown4f86d3a2007-10-03 18:58:00 -0400200 .show = cpuidle_show,
201 .store = cpuidle_store,
202};
203
204static void cpuidle_sysfs_release(struct kobject *kobj)
205{
206 struct cpuidle_device *dev = kobj_to_cpuidledev(kobj);
207
208 complete(&dev->kobj_unregister);
209}
210
211static struct kobj_type ktype_cpuidle = {
212 .sysfs_ops = &cpuidle_sysfs_ops,
213 .release = cpuidle_sysfs_release,
214};
215
216struct cpuidle_state_attr {
217 struct attribute attr;
218 ssize_t (*show)(struct cpuidle_state *, char *);
219 ssize_t (*store)(struct cpuidle_state *, const char *, size_t);
220};
221
222#define define_one_state_ro(_name, show) \
223static struct cpuidle_state_attr attr_##_name = __ATTR(_name, 0444, show, NULL)
224
225#define define_show_state_function(_name) \
226static ssize_t show_state_##_name(struct cpuidle_state *state, char *buf) \
227{ \
228 return sprintf(buf, "%u\n", state->_name);\
229}
230
Yi Yang8b78cf62008-02-25 08:46:12 +0800231#define define_show_state_ull_function(_name) \
232static ssize_t show_state_##_name(struct cpuidle_state *state, char *buf) \
233{ \
234 return sprintf(buf, "%llu\n", state->_name);\
235}
236
Venkatesh Pallipadi4fcb2fc2008-02-11 17:46:31 -0800237#define define_show_state_str_function(_name) \
238static ssize_t show_state_##_name(struct cpuidle_state *state, char *buf) \
239{ \
240 if (state->_name[0] == '\0')\
241 return sprintf(buf, "<null>\n");\
242 return sprintf(buf, "%s\n", state->_name);\
Len Brown4f86d3a2007-10-03 18:58:00 -0400243}
244
245define_show_state_function(exit_latency)
246define_show_state_function(power_usage)
Yi Yang8b78cf62008-02-25 08:46:12 +0800247define_show_state_ull_function(usage)
248define_show_state_ull_function(time)
Venkatesh Pallipadi4fcb2fc2008-02-11 17:46:31 -0800249define_show_state_str_function(name)
250define_show_state_str_function(desc)
251
Len Brown4f86d3a2007-10-03 18:58:00 -0400252define_one_state_ro(name, show_state_name);
Venkatesh Pallipadi4fcb2fc2008-02-11 17:46:31 -0800253define_one_state_ro(desc, show_state_desc);
Len Brown4f86d3a2007-10-03 18:58:00 -0400254define_one_state_ro(latency, show_state_exit_latency);
255define_one_state_ro(power, show_state_power_usage);
256define_one_state_ro(usage, show_state_usage);
257define_one_state_ro(time, show_state_time);
258
259static struct attribute *cpuidle_state_default_attrs[] = {
260 &attr_name.attr,
Venkatesh Pallipadi4fcb2fc2008-02-11 17:46:31 -0800261 &attr_desc.attr,
Len Brown4f86d3a2007-10-03 18:58:00 -0400262 &attr_latency.attr,
263 &attr_power.attr,
264 &attr_usage.attr,
265 &attr_time.attr,
266 NULL
267};
268
269#define kobj_to_state_obj(k) container_of(k, struct cpuidle_state_kobj, kobj)
270#define kobj_to_state(k) (kobj_to_state_obj(k)->state)
271#define attr_to_stateattr(a) container_of(a, struct cpuidle_state_attr, attr)
272static ssize_t cpuidle_state_show(struct kobject * kobj,
273 struct attribute * attr ,char * buf)
274{
275 int ret = -EIO;
276 struct cpuidle_state *state = kobj_to_state(kobj);
277 struct cpuidle_state_attr * cattr = attr_to_stateattr(attr);
278
279 if (cattr->show)
280 ret = cattr->show(state, buf);
281
282 return ret;
283}
284
Emese Revfy52cf25d2010-01-19 02:58:23 +0100285static const struct sysfs_ops cpuidle_state_sysfs_ops = {
Len Brown4f86d3a2007-10-03 18:58:00 -0400286 .show = cpuidle_state_show,
287};
288
289static void cpuidle_state_sysfs_release(struct kobject *kobj)
290{
291 struct cpuidle_state_kobj *state_obj = kobj_to_state_obj(kobj);
292
293 complete(&state_obj->kobj_unregister);
294}
295
296static struct kobj_type ktype_state_cpuidle = {
297 .sysfs_ops = &cpuidle_state_sysfs_ops,
298 .default_attrs = cpuidle_state_default_attrs,
299 .release = cpuidle_state_sysfs_release,
300};
301
302static void inline cpuidle_free_state_kobj(struct cpuidle_device *device, int i)
303{
Greg Kroah-Hartmanc10997f2007-12-20 08:13:05 -0800304 kobject_put(&device->kobjs[i]->kobj);
Len Brown4f86d3a2007-10-03 18:58:00 -0400305 wait_for_completion(&device->kobjs[i]->kobj_unregister);
306 kfree(device->kobjs[i]);
307 device->kobjs[i] = NULL;
308}
309
310/**
311 * cpuidle_add_driver_sysfs - adds driver-specific sysfs attributes
312 * @device: the target device
313 */
314int cpuidle_add_state_sysfs(struct cpuidle_device *device)
315{
316 int i, ret = -ENOMEM;
317 struct cpuidle_state_kobj *kobj;
318
319 /* state statistics */
320 for (i = 0; i < device->state_count; i++) {
321 kobj = kzalloc(sizeof(struct cpuidle_state_kobj), GFP_KERNEL);
322 if (!kobj)
323 goto error_state;
324 kobj->state = &device->states[i];
325 init_completion(&kobj->kobj_unregister);
326
Greg Kroah-Hartman94f57f32007-12-17 15:54:39 -0400327 ret = kobject_init_and_add(&kobj->kobj, &ktype_state_cpuidle, &device->kobj,
328 "state%d", i);
Len Brown4f86d3a2007-10-03 18:58:00 -0400329 if (ret) {
330 kfree(kobj);
331 goto error_state;
332 }
Greg Kroah-Hartman94f57f32007-12-17 15:54:39 -0400333 kobject_uevent(&kobj->kobj, KOBJ_ADD);
Len Brown4f86d3a2007-10-03 18:58:00 -0400334 device->kobjs[i] = kobj;
335 }
336
337 return 0;
338
339error_state:
340 for (i = i - 1; i >= 0; i--)
341 cpuidle_free_state_kobj(device, i);
342 return ret;
343}
344
345/**
346 * cpuidle_remove_driver_sysfs - removes driver-specific sysfs attributes
347 * @device: the target device
348 */
349void cpuidle_remove_state_sysfs(struct cpuidle_device *device)
350{
351 int i;
352
353 for (i = 0; i < device->state_count; i++)
354 cpuidle_free_state_kobj(device, i);
355}
356
357/**
358 * cpuidle_add_sysfs - creates a sysfs instance for the target device
359 * @sysdev: the target device
360 */
361int cpuidle_add_sysfs(struct sys_device *sysdev)
362{
363 int cpu = sysdev->id;
364 struct cpuidle_device *dev;
Greg Kroah-Hartman94f57f32007-12-17 15:54:39 -0400365 int error;
Len Brown4f86d3a2007-10-03 18:58:00 -0400366
367 dev = per_cpu(cpuidle_devices, cpu);
Greg Kroah-Hartman94f57f32007-12-17 15:54:39 -0400368 error = kobject_init_and_add(&dev->kobj, &ktype_cpuidle, &sysdev->kobj,
369 "cpuidle");
370 if (!error)
371 kobject_uevent(&dev->kobj, KOBJ_ADD);
372 return error;
Len Brown4f86d3a2007-10-03 18:58:00 -0400373}
374
375/**
376 * cpuidle_remove_sysfs - deletes a sysfs instance on the target device
377 * @sysdev: the target device
378 */
379void cpuidle_remove_sysfs(struct sys_device *sysdev)
380{
381 int cpu = sysdev->id;
382 struct cpuidle_device *dev;
383
384 dev = per_cpu(cpuidle_devices, cpu);
Greg Kroah-Hartmanc10997f2007-12-20 08:13:05 -0800385 kobject_put(&dev->kobj);
Len Brown4f86d3a2007-10-03 18:58:00 -0400386}