Len Brown | 4f86d3a | 2007-10-03 18:58:00 -0400 | [diff] [blame] | 1 | /* |
| 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> |
| 12 | #include <linux/cpu.h> |
| 13 | |
| 14 | #include "cpuidle.h" |
| 15 | |
| 16 | static unsigned int sysfs_switch; |
| 17 | static int __init cpuidle_sysfs_setup(char *unused) |
| 18 | { |
| 19 | sysfs_switch = 1; |
| 20 | return 1; |
| 21 | } |
| 22 | __setup("cpuidle_sysfs_switch", cpuidle_sysfs_setup); |
| 23 | |
| 24 | static ssize_t show_available_governors(struct sys_device *dev, char *buf) |
| 25 | { |
| 26 | ssize_t i = 0; |
| 27 | struct cpuidle_governor *tmp; |
| 28 | |
| 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)) |
| 32 | goto out; |
| 33 | i += scnprintf(&buf[i], CPUIDLE_NAME_LEN, "%s ", tmp->name); |
| 34 | } |
| 35 | |
| 36 | out: |
| 37 | i+= sprintf(&buf[i], "\n"); |
| 38 | mutex_unlock(&cpuidle_lock); |
| 39 | return i; |
| 40 | } |
| 41 | |
| 42 | static ssize_t show_current_driver(struct sys_device *dev, char *buf) |
| 43 | { |
| 44 | ssize_t ret; |
| 45 | |
| 46 | spin_lock(&cpuidle_driver_lock); |
| 47 | if (cpuidle_curr_driver) |
| 48 | ret = sprintf(buf, "%s\n", cpuidle_curr_driver->name); |
| 49 | else |
| 50 | ret = sprintf(buf, "none\n"); |
| 51 | spin_unlock(&cpuidle_driver_lock); |
| 52 | |
| 53 | return ret; |
| 54 | } |
| 55 | |
| 56 | static ssize_t show_current_governor(struct sys_device *dev, char *buf) |
| 57 | { |
| 58 | ssize_t ret; |
| 59 | |
| 60 | mutex_lock(&cpuidle_lock); |
| 61 | if (cpuidle_curr_governor) |
| 62 | ret = sprintf(buf, "%s\n", cpuidle_curr_governor->name); |
| 63 | else |
| 64 | ret = sprintf(buf, "none\n"); |
| 65 | mutex_unlock(&cpuidle_lock); |
| 66 | |
| 67 | return ret; |
| 68 | } |
| 69 | |
| 70 | static ssize_t store_current_governor(struct sys_device *dev, |
| 71 | const char *buf, size_t count) |
| 72 | { |
| 73 | char gov_name[CPUIDLE_NAME_LEN]; |
| 74 | int ret = -EINVAL; |
| 75 | size_t len = count; |
| 76 | struct cpuidle_governor *gov; |
| 77 | |
| 78 | if (!len || len >= sizeof(gov_name)) |
| 79 | return -EINVAL; |
| 80 | |
| 81 | memcpy(gov_name, buf, len); |
| 82 | gov_name[len] = '\0'; |
| 83 | if (gov_name[len - 1] == '\n') |
| 84 | gov_name[--len] = '\0'; |
| 85 | |
| 86 | mutex_lock(&cpuidle_lock); |
| 87 | |
| 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); |
| 91 | break; |
| 92 | } |
| 93 | } |
| 94 | |
| 95 | mutex_unlock(&cpuidle_lock); |
| 96 | |
| 97 | if (ret) |
| 98 | return ret; |
| 99 | else |
| 100 | return count; |
| 101 | } |
| 102 | |
| 103 | static SYSDEV_ATTR(current_driver, 0444, show_current_driver, NULL); |
| 104 | static SYSDEV_ATTR(current_governor_ro, 0444, show_current_governor, NULL); |
| 105 | |
| 106 | static struct attribute *cpuclass_default_attrs[] = { |
| 107 | &attr_current_driver.attr, |
| 108 | &attr_current_governor_ro.attr, |
| 109 | NULL |
| 110 | }; |
| 111 | |
| 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); |
| 115 | |
| 116 | static struct attribute *cpuclass_switch_attrs[] = { |
| 117 | &attr_available_governors.attr, |
| 118 | &attr_current_driver.attr, |
| 119 | &attr_current_governor.attr, |
| 120 | NULL |
| 121 | }; |
| 122 | |
| 123 | static struct attribute_group cpuclass_attr_group = { |
| 124 | .attrs = cpuclass_default_attrs, |
| 125 | .name = "cpuidle", |
| 126 | }; |
| 127 | |
| 128 | /** |
| 129 | * cpuidle_add_class_sysfs - add CPU global sysfs attributes |
| 130 | */ |
| 131 | int cpuidle_add_class_sysfs(struct sysdev_class *cls) |
| 132 | { |
| 133 | if (sysfs_switch) |
| 134 | cpuclass_attr_group.attrs = cpuclass_switch_attrs; |
| 135 | |
| 136 | return sysfs_create_group(&cls->kset.kobj, &cpuclass_attr_group); |
| 137 | } |
| 138 | |
| 139 | /** |
| 140 | * cpuidle_remove_class_sysfs - remove CPU global sysfs attributes |
| 141 | */ |
| 142 | void cpuidle_remove_class_sysfs(struct sysdev_class *cls) |
| 143 | { |
| 144 | sysfs_remove_group(&cls->kset.kobj, &cpuclass_attr_group); |
| 145 | } |
| 146 | |
| 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); |
| 151 | }; |
| 152 | |
| 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) |
| 157 | |
| 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) |
| 161 | { |
| 162 | int ret = -EIO; |
| 163 | struct cpuidle_device *dev = kobj_to_cpuidledev(kobj); |
| 164 | struct cpuidle_attr * cattr = attr_to_cpuidleattr(attr); |
| 165 | |
| 166 | if (cattr->show) { |
| 167 | mutex_lock(&cpuidle_lock); |
| 168 | ret = cattr->show(dev, buf); |
| 169 | mutex_unlock(&cpuidle_lock); |
| 170 | } |
| 171 | return ret; |
| 172 | } |
| 173 | |
| 174 | static ssize_t cpuidle_store(struct kobject * kobj, struct attribute * attr, |
| 175 | const char * buf, size_t count) |
| 176 | { |
| 177 | int ret = -EIO; |
| 178 | struct cpuidle_device *dev = kobj_to_cpuidledev(kobj); |
| 179 | struct cpuidle_attr * cattr = attr_to_cpuidleattr(attr); |
| 180 | |
| 181 | if (cattr->store) { |
| 182 | mutex_lock(&cpuidle_lock); |
| 183 | ret = cattr->store(dev, buf, count); |
| 184 | mutex_unlock(&cpuidle_lock); |
| 185 | } |
| 186 | return ret; |
| 187 | } |
| 188 | |
| 189 | static struct sysfs_ops cpuidle_sysfs_ops = { |
| 190 | .show = cpuidle_show, |
| 191 | .store = cpuidle_store, |
| 192 | }; |
| 193 | |
| 194 | static void cpuidle_sysfs_release(struct kobject *kobj) |
| 195 | { |
| 196 | struct cpuidle_device *dev = kobj_to_cpuidledev(kobj); |
| 197 | |
| 198 | complete(&dev->kobj_unregister); |
| 199 | } |
| 200 | |
| 201 | static struct kobj_type ktype_cpuidle = { |
| 202 | .sysfs_ops = &cpuidle_sysfs_ops, |
| 203 | .release = cpuidle_sysfs_release, |
| 204 | }; |
| 205 | |
| 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); |
| 210 | }; |
| 211 | |
| 212 | #define define_one_state_ro(_name, show) \ |
| 213 | static struct cpuidle_state_attr attr_##_name = __ATTR(_name, 0444, show, NULL) |
| 214 | |
| 215 | #define define_show_state_function(_name) \ |
| 216 | static ssize_t show_state_##_name(struct cpuidle_state *state, char *buf) \ |
| 217 | { \ |
| 218 | return sprintf(buf, "%u\n", state->_name);\ |
| 219 | } |
| 220 | |
Venkatesh Pallipadi | 4fcb2fc | 2008-02-11 17:46:31 -0800 | [diff] [blame] | 221 | #define define_show_state_str_function(_name) \ |
| 222 | static ssize_t show_state_##_name(struct cpuidle_state *state, char *buf) \ |
| 223 | { \ |
| 224 | if (state->_name[0] == '\0')\ |
| 225 | return sprintf(buf, "<null>\n");\ |
| 226 | return sprintf(buf, "%s\n", state->_name);\ |
Len Brown | 4f86d3a | 2007-10-03 18:58:00 -0400 | [diff] [blame] | 227 | } |
| 228 | |
| 229 | define_show_state_function(exit_latency) |
| 230 | define_show_state_function(power_usage) |
| 231 | define_show_state_function(usage) |
| 232 | define_show_state_function(time) |
Venkatesh Pallipadi | 4fcb2fc | 2008-02-11 17:46:31 -0800 | [diff] [blame] | 233 | define_show_state_str_function(name) |
| 234 | define_show_state_str_function(desc) |
| 235 | |
Len Brown | 4f86d3a | 2007-10-03 18:58:00 -0400 | [diff] [blame] | 236 | define_one_state_ro(name, show_state_name); |
Venkatesh Pallipadi | 4fcb2fc | 2008-02-11 17:46:31 -0800 | [diff] [blame] | 237 | define_one_state_ro(desc, show_state_desc); |
Len Brown | 4f86d3a | 2007-10-03 18:58:00 -0400 | [diff] [blame] | 238 | define_one_state_ro(latency, show_state_exit_latency); |
| 239 | define_one_state_ro(power, show_state_power_usage); |
| 240 | define_one_state_ro(usage, show_state_usage); |
| 241 | define_one_state_ro(time, show_state_time); |
| 242 | |
| 243 | static struct attribute *cpuidle_state_default_attrs[] = { |
| 244 | &attr_name.attr, |
Venkatesh Pallipadi | 4fcb2fc | 2008-02-11 17:46:31 -0800 | [diff] [blame] | 245 | &attr_desc.attr, |
Len Brown | 4f86d3a | 2007-10-03 18:58:00 -0400 | [diff] [blame] | 246 | &attr_latency.attr, |
| 247 | &attr_power.attr, |
| 248 | &attr_usage.attr, |
| 249 | &attr_time.attr, |
| 250 | NULL |
| 251 | }; |
| 252 | |
| 253 | #define kobj_to_state_obj(k) container_of(k, struct cpuidle_state_kobj, kobj) |
| 254 | #define kobj_to_state(k) (kobj_to_state_obj(k)->state) |
| 255 | #define attr_to_stateattr(a) container_of(a, struct cpuidle_state_attr, attr) |
| 256 | static ssize_t cpuidle_state_show(struct kobject * kobj, |
| 257 | struct attribute * attr ,char * buf) |
| 258 | { |
| 259 | int ret = -EIO; |
| 260 | struct cpuidle_state *state = kobj_to_state(kobj); |
| 261 | struct cpuidle_state_attr * cattr = attr_to_stateattr(attr); |
| 262 | |
| 263 | if (cattr->show) |
| 264 | ret = cattr->show(state, buf); |
| 265 | |
| 266 | return ret; |
| 267 | } |
| 268 | |
| 269 | static struct sysfs_ops cpuidle_state_sysfs_ops = { |
| 270 | .show = cpuidle_state_show, |
| 271 | }; |
| 272 | |
| 273 | static void cpuidle_state_sysfs_release(struct kobject *kobj) |
| 274 | { |
| 275 | struct cpuidle_state_kobj *state_obj = kobj_to_state_obj(kobj); |
| 276 | |
| 277 | complete(&state_obj->kobj_unregister); |
| 278 | } |
| 279 | |
| 280 | static struct kobj_type ktype_state_cpuidle = { |
| 281 | .sysfs_ops = &cpuidle_state_sysfs_ops, |
| 282 | .default_attrs = cpuidle_state_default_attrs, |
| 283 | .release = cpuidle_state_sysfs_release, |
| 284 | }; |
| 285 | |
| 286 | static void inline cpuidle_free_state_kobj(struct cpuidle_device *device, int i) |
| 287 | { |
Greg Kroah-Hartman | c10997f | 2007-12-20 08:13:05 -0800 | [diff] [blame] | 288 | kobject_put(&device->kobjs[i]->kobj); |
Len Brown | 4f86d3a | 2007-10-03 18:58:00 -0400 | [diff] [blame] | 289 | wait_for_completion(&device->kobjs[i]->kobj_unregister); |
| 290 | kfree(device->kobjs[i]); |
| 291 | device->kobjs[i] = NULL; |
| 292 | } |
| 293 | |
| 294 | /** |
| 295 | * cpuidle_add_driver_sysfs - adds driver-specific sysfs attributes |
| 296 | * @device: the target device |
| 297 | */ |
| 298 | int cpuidle_add_state_sysfs(struct cpuidle_device *device) |
| 299 | { |
| 300 | int i, ret = -ENOMEM; |
| 301 | struct cpuidle_state_kobj *kobj; |
| 302 | |
| 303 | /* state statistics */ |
| 304 | for (i = 0; i < device->state_count; i++) { |
| 305 | kobj = kzalloc(sizeof(struct cpuidle_state_kobj), GFP_KERNEL); |
| 306 | if (!kobj) |
| 307 | goto error_state; |
| 308 | kobj->state = &device->states[i]; |
| 309 | init_completion(&kobj->kobj_unregister); |
| 310 | |
Greg Kroah-Hartman | 94f57f3 | 2007-12-17 15:54:39 -0400 | [diff] [blame] | 311 | ret = kobject_init_and_add(&kobj->kobj, &ktype_state_cpuidle, &device->kobj, |
| 312 | "state%d", i); |
Len Brown | 4f86d3a | 2007-10-03 18:58:00 -0400 | [diff] [blame] | 313 | if (ret) { |
| 314 | kfree(kobj); |
| 315 | goto error_state; |
| 316 | } |
Greg Kroah-Hartman | 94f57f3 | 2007-12-17 15:54:39 -0400 | [diff] [blame] | 317 | kobject_uevent(&kobj->kobj, KOBJ_ADD); |
Len Brown | 4f86d3a | 2007-10-03 18:58:00 -0400 | [diff] [blame] | 318 | device->kobjs[i] = kobj; |
| 319 | } |
| 320 | |
| 321 | return 0; |
| 322 | |
| 323 | error_state: |
| 324 | for (i = i - 1; i >= 0; i--) |
| 325 | cpuidle_free_state_kobj(device, i); |
| 326 | return ret; |
| 327 | } |
| 328 | |
| 329 | /** |
| 330 | * cpuidle_remove_driver_sysfs - removes driver-specific sysfs attributes |
| 331 | * @device: the target device |
| 332 | */ |
| 333 | void cpuidle_remove_state_sysfs(struct cpuidle_device *device) |
| 334 | { |
| 335 | int i; |
| 336 | |
| 337 | for (i = 0; i < device->state_count; i++) |
| 338 | cpuidle_free_state_kobj(device, i); |
| 339 | } |
| 340 | |
| 341 | /** |
| 342 | * cpuidle_add_sysfs - creates a sysfs instance for the target device |
| 343 | * @sysdev: the target device |
| 344 | */ |
| 345 | int cpuidle_add_sysfs(struct sys_device *sysdev) |
| 346 | { |
| 347 | int cpu = sysdev->id; |
| 348 | struct cpuidle_device *dev; |
Greg Kroah-Hartman | 94f57f3 | 2007-12-17 15:54:39 -0400 | [diff] [blame] | 349 | int error; |
Len Brown | 4f86d3a | 2007-10-03 18:58:00 -0400 | [diff] [blame] | 350 | |
| 351 | dev = per_cpu(cpuidle_devices, cpu); |
Greg Kroah-Hartman | 94f57f3 | 2007-12-17 15:54:39 -0400 | [diff] [blame] | 352 | error = kobject_init_and_add(&dev->kobj, &ktype_cpuidle, &sysdev->kobj, |
| 353 | "cpuidle"); |
| 354 | if (!error) |
| 355 | kobject_uevent(&dev->kobj, KOBJ_ADD); |
| 356 | return error; |
Len Brown | 4f86d3a | 2007-10-03 18:58:00 -0400 | [diff] [blame] | 357 | } |
| 358 | |
| 359 | /** |
| 360 | * cpuidle_remove_sysfs - deletes a sysfs instance on the target device |
| 361 | * @sysdev: the target device |
| 362 | */ |
| 363 | void cpuidle_remove_sysfs(struct sys_device *sysdev) |
| 364 | { |
| 365 | int cpu = sysdev->id; |
| 366 | struct cpuidle_device *dev; |
| 367 | |
| 368 | dev = per_cpu(cpuidle_devices, cpu); |
Greg Kroah-Hartman | c10997f | 2007-12-20 08:13:05 -0800 | [diff] [blame] | 369 | kobject_put(&dev->kobj); |
Len Brown | 4f86d3a | 2007-10-03 18:58:00 -0400 | [diff] [blame] | 370 | } |