2 * sysfs.c - sysfs support
4 * (C) 2006-2007 Shaohua Li <shaohua.li@intel.com>
6 * This code is licenced under the GPL.
9 #include <linux/kernel.h>
10 #include <linux/cpuidle.h>
11 #include <linux/sysfs.h>
12 #include <linux/cpu.h>
16 static unsigned int sysfs_switch;
17 static int __init cpuidle_sysfs_setup(char *unused)
22 __setup("cpuidle_sysfs_switch", cpuidle_sysfs_setup);
24 static ssize_t show_available_governors(struct sys_device *dev, char *buf)
27 struct cpuidle_governor *tmp;
29 mutex_lock(&cpuidle_lock);
30 list_for_each_entry(tmp, &cpuidle_governors, governor_list) {
31 if (i >= (ssize_t) ((PAGE_SIZE/sizeof(char)) - CPUIDLE_NAME_LEN - 2))
33 i += scnprintf(&buf[i], CPUIDLE_NAME_LEN, "%s ", tmp->name);
37 i+= sprintf(&buf[i], "\n");
38 mutex_unlock(&cpuidle_lock);
42 static ssize_t show_current_driver(struct sys_device *dev, char *buf)
46 spin_lock(&cpuidle_driver_lock);
47 if (cpuidle_curr_driver)
48 ret = sprintf(buf, "%s\n", cpuidle_curr_driver->name);
50 ret = sprintf(buf, "none\n");
51 spin_unlock(&cpuidle_driver_lock);
56 static ssize_t show_current_governor(struct sys_device *dev, char *buf)
60 mutex_lock(&cpuidle_lock);
61 if (cpuidle_curr_governor)
62 ret = sprintf(buf, "%s\n", cpuidle_curr_governor->name);
64 ret = sprintf(buf, "none\n");
65 mutex_unlock(&cpuidle_lock);
70 static ssize_t store_current_governor(struct sys_device *dev,
71 const char *buf, size_t count)
73 char gov_name[CPUIDLE_NAME_LEN];
76 struct cpuidle_governor *gov;
78 if (!len || len >= sizeof(gov_name))
81 memcpy(gov_name, buf, len);
83 if (gov_name[len - 1] == '\n')
84 gov_name[--len] = '\0';
86 mutex_lock(&cpuidle_lock);
88 list_for_each_entry(gov, &cpuidle_governors, governor_list) {
89 if (strlen(gov->name) == len && !strcmp(gov->name, gov_name)) {
90 ret = cpuidle_switch_governor(gov);
95 mutex_unlock(&cpuidle_lock);
103 static SYSDEV_ATTR(current_driver, 0444, show_current_driver, NULL);
104 static SYSDEV_ATTR(current_governor_ro, 0444, show_current_governor, NULL);
106 static struct attribute *cpuclass_default_attrs[] = {
107 &attr_current_driver.attr,
108 &attr_current_governor_ro.attr,
112 static SYSDEV_ATTR(available_governors, 0444, show_available_governors, NULL);
113 static SYSDEV_ATTR(current_governor, 0644, show_current_governor,
114 store_current_governor);
116 static struct attribute *cpuclass_switch_attrs[] = {
117 &attr_available_governors.attr,
118 &attr_current_driver.attr,
119 &attr_current_governor.attr,
123 static struct attribute_group cpuclass_attr_group = {
124 .attrs = cpuclass_default_attrs,
129 * cpuidle_add_class_sysfs - add CPU global sysfs attributes
131 int cpuidle_add_class_sysfs(struct sysdev_class *cls)
134 cpuclass_attr_group.attrs = cpuclass_switch_attrs;
136 return sysfs_create_group(&cls->kset.kobj, &cpuclass_attr_group);
140 * cpuidle_remove_class_sysfs - remove CPU global sysfs attributes
142 void cpuidle_remove_class_sysfs(struct sysdev_class *cls)
144 sysfs_remove_group(&cls->kset.kobj, &cpuclass_attr_group);
147 struct cpuidle_attr {
148 struct attribute attr;
149 ssize_t (*show)(struct cpuidle_device *, char *);
150 ssize_t (*store)(struct cpuidle_device *, const char *, size_t count);
153 #define define_one_ro(_name, show) \
154 static struct cpuidle_attr attr_##_name = __ATTR(_name, 0444, show, NULL)
155 #define define_one_rw(_name, show, store) \
156 static struct cpuidle_attr attr_##_name = __ATTR(_name, 0644, show, store)
158 #define kobj_to_cpuidledev(k) container_of(k, struct cpuidle_device, kobj)
159 #define attr_to_cpuidleattr(a) container_of(a, struct cpuidle_attr, attr)
160 static ssize_t cpuidle_show(struct kobject * kobj, struct attribute * attr ,char * buf)
163 struct cpuidle_device *dev = kobj_to_cpuidledev(kobj);
164 struct cpuidle_attr * cattr = attr_to_cpuidleattr(attr);
167 mutex_lock(&cpuidle_lock);
168 ret = cattr->show(dev, buf);
169 mutex_unlock(&cpuidle_lock);
174 static ssize_t cpuidle_store(struct kobject * kobj, struct attribute * attr,
175 const char * buf, size_t count)
178 struct cpuidle_device *dev = kobj_to_cpuidledev(kobj);
179 struct cpuidle_attr * cattr = attr_to_cpuidleattr(attr);
182 mutex_lock(&cpuidle_lock);
183 ret = cattr->store(dev, buf, count);
184 mutex_unlock(&cpuidle_lock);
189 static struct sysfs_ops cpuidle_sysfs_ops = {
190 .show = cpuidle_show,
191 .store = cpuidle_store,
194 static void cpuidle_sysfs_release(struct kobject *kobj)
196 struct cpuidle_device *dev = kobj_to_cpuidledev(kobj);
198 complete(&dev->kobj_unregister);
201 static struct kobj_type ktype_cpuidle = {
202 .sysfs_ops = &cpuidle_sysfs_ops,
203 .release = cpuidle_sysfs_release,
206 struct cpuidle_state_attr {
207 struct attribute attr;
208 ssize_t (*show)(struct cpuidle_state *, char *);
209 ssize_t (*store)(struct cpuidle_state *, const char *, size_t);
212 #define define_one_state_ro(_name, show) \
213 static struct cpuidle_state_attr attr_##_name = __ATTR(_name, 0444, show, NULL)
215 #define define_show_state_function(_name) \
216 static ssize_t show_state_##_name(struct cpuidle_state *state, char *buf) \
218 return sprintf(buf, "%u\n", state->_name);\
221 #define define_show_state_ull_function(_name) \
222 static ssize_t show_state_##_name(struct cpuidle_state *state, char *buf) \
224 return sprintf(buf, "%llu\n", state->_name);\
227 #define define_show_state_str_function(_name) \
228 static ssize_t show_state_##_name(struct cpuidle_state *state, char *buf) \
230 if (state->_name[0] == '\0')\
231 return sprintf(buf, "<null>\n");\
232 return sprintf(buf, "%s\n", state->_name);\
235 define_show_state_function(exit_latency)
236 define_show_state_function(power_usage)
237 define_show_state_ull_function(usage)
238 define_show_state_ull_function(time)
239 define_show_state_str_function(name)
240 define_show_state_str_function(desc)
242 define_one_state_ro(name, show_state_name);
243 define_one_state_ro(desc, show_state_desc);
244 define_one_state_ro(latency, show_state_exit_latency);
245 define_one_state_ro(power, show_state_power_usage);
246 define_one_state_ro(usage, show_state_usage);
247 define_one_state_ro(time, show_state_time);
249 static struct attribute *cpuidle_state_default_attrs[] = {
259 #define kobj_to_state_obj(k) container_of(k, struct cpuidle_state_kobj, kobj)
260 #define kobj_to_state(k) (kobj_to_state_obj(k)->state)
261 #define attr_to_stateattr(a) container_of(a, struct cpuidle_state_attr, attr)
262 static ssize_t cpuidle_state_show(struct kobject * kobj,
263 struct attribute * attr ,char * buf)
266 struct cpuidle_state *state = kobj_to_state(kobj);
267 struct cpuidle_state_attr * cattr = attr_to_stateattr(attr);
270 ret = cattr->show(state, buf);
275 static struct sysfs_ops cpuidle_state_sysfs_ops = {
276 .show = cpuidle_state_show,
279 static void cpuidle_state_sysfs_release(struct kobject *kobj)
281 struct cpuidle_state_kobj *state_obj = kobj_to_state_obj(kobj);
283 complete(&state_obj->kobj_unregister);
286 static struct kobj_type ktype_state_cpuidle = {
287 .sysfs_ops = &cpuidle_state_sysfs_ops,
288 .default_attrs = cpuidle_state_default_attrs,
289 .release = cpuidle_state_sysfs_release,
292 static void inline cpuidle_free_state_kobj(struct cpuidle_device *device, int i)
294 kobject_put(&device->kobjs[i]->kobj);
295 wait_for_completion(&device->kobjs[i]->kobj_unregister);
296 kfree(device->kobjs[i]);
297 device->kobjs[i] = NULL;
301 * cpuidle_add_driver_sysfs - adds driver-specific sysfs attributes
302 * @device: the target device
304 int cpuidle_add_state_sysfs(struct cpuidle_device *device)
306 int i, ret = -ENOMEM;
307 struct cpuidle_state_kobj *kobj;
309 /* state statistics */
310 for (i = 0; i < device->state_count; i++) {
311 kobj = kzalloc(sizeof(struct cpuidle_state_kobj), GFP_KERNEL);
314 kobj->state = &device->states[i];
315 init_completion(&kobj->kobj_unregister);
317 ret = kobject_init_and_add(&kobj->kobj, &ktype_state_cpuidle, &device->kobj,
323 kobject_uevent(&kobj->kobj, KOBJ_ADD);
324 device->kobjs[i] = kobj;
330 for (i = i - 1; i >= 0; i--)
331 cpuidle_free_state_kobj(device, i);
336 * cpuidle_remove_driver_sysfs - removes driver-specific sysfs attributes
337 * @device: the target device
339 void cpuidle_remove_state_sysfs(struct cpuidle_device *device)
343 for (i = 0; i < device->state_count; i++)
344 cpuidle_free_state_kobj(device, i);
348 * cpuidle_add_sysfs - creates a sysfs instance for the target device
349 * @sysdev: the target device
351 int cpuidle_add_sysfs(struct sys_device *sysdev)
353 int cpu = sysdev->id;
354 struct cpuidle_device *dev;
357 dev = per_cpu(cpuidle_devices, cpu);
358 error = kobject_init_and_add(&dev->kobj, &ktype_cpuidle, &sysdev->kobj,
361 kobject_uevent(&dev->kobj, KOBJ_ADD);
366 * cpuidle_remove_sysfs - deletes a sysfs instance on the target device
367 * @sysdev: the target device
369 void cpuidle_remove_sysfs(struct sys_device *sysdev)
371 int cpu = sysdev->id;
372 struct cpuidle_device *dev;
374 dev = per_cpu(cpuidle_devices, cpu);
375 kobject_put(&dev->kobj);