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> |
Tejun Heo | 5a0e3ad | 2010-03-24 17:04:11 +0900 | [diff] [blame] | 12 | #include <linux/slab.h> |
Len Brown | 4f86d3a | 2007-10-03 18:58:00 -0400 | [diff] [blame] | 13 | #include <linux/cpu.h> |
ShuoX Liu | 3a53396 | 2012-03-28 15:19:11 -0700 | [diff] [blame] | 14 | #include <linux/capability.h> |
Daniel Lezcano | 8f3e995 | 2012-10-31 01:09:02 +0100 | [diff] [blame] | 15 | #include <linux/device.h> |
Len Brown | 4f86d3a | 2007-10-03 18:58:00 -0400 | [diff] [blame] | 16 | |
| 17 | #include "cpuidle.h" |
| 18 | |
| 19 | static unsigned int sysfs_switch; |
| 20 | static int __init cpuidle_sysfs_setup(char *unused) |
| 21 | { |
| 22 | sysfs_switch = 1; |
| 23 | return 1; |
| 24 | } |
| 25 | __setup("cpuidle_sysfs_switch", cpuidle_sysfs_setup); |
| 26 | |
Kay Sievers | 8a25a2f | 2011-12-21 14:29:42 -0800 | [diff] [blame] | 27 | static ssize_t show_available_governors(struct device *dev, |
| 28 | struct device_attribute *attr, |
Rabin Vincent | 66198f3 | 2008-08-12 15:08:45 -0700 | [diff] [blame] | 29 | char *buf) |
Len Brown | 4f86d3a | 2007-10-03 18:58:00 -0400 | [diff] [blame] | 30 | { |
| 31 | ssize_t i = 0; |
| 32 | struct cpuidle_governor *tmp; |
| 33 | |
| 34 | mutex_lock(&cpuidle_lock); |
| 35 | list_for_each_entry(tmp, &cpuidle_governors, governor_list) { |
| 36 | if (i >= (ssize_t) ((PAGE_SIZE/sizeof(char)) - CPUIDLE_NAME_LEN - 2)) |
| 37 | goto out; |
| 38 | i += scnprintf(&buf[i], CPUIDLE_NAME_LEN, "%s ", tmp->name); |
| 39 | } |
| 40 | |
| 41 | out: |
| 42 | i+= sprintf(&buf[i], "\n"); |
| 43 | mutex_unlock(&cpuidle_lock); |
| 44 | return i; |
| 45 | } |
| 46 | |
Kay Sievers | 8a25a2f | 2011-12-21 14:29:42 -0800 | [diff] [blame] | 47 | static ssize_t show_current_driver(struct device *dev, |
| 48 | struct device_attribute *attr, |
Rabin Vincent | 66198f3 | 2008-08-12 15:08:45 -0700 | [diff] [blame] | 49 | char *buf) |
Len Brown | 4f86d3a | 2007-10-03 18:58:00 -0400 | [diff] [blame] | 50 | { |
| 51 | ssize_t ret; |
Len Brown | 752138d | 2010-05-22 16:57:26 -0400 | [diff] [blame] | 52 | struct cpuidle_driver *cpuidle_driver = cpuidle_get_driver(); |
Len Brown | 4f86d3a | 2007-10-03 18:58:00 -0400 | [diff] [blame] | 53 | |
| 54 | spin_lock(&cpuidle_driver_lock); |
Len Brown | 752138d | 2010-05-22 16:57:26 -0400 | [diff] [blame] | 55 | if (cpuidle_driver) |
| 56 | ret = sprintf(buf, "%s\n", cpuidle_driver->name); |
Len Brown | 4f86d3a | 2007-10-03 18:58:00 -0400 | [diff] [blame] | 57 | else |
| 58 | ret = sprintf(buf, "none\n"); |
| 59 | spin_unlock(&cpuidle_driver_lock); |
| 60 | |
| 61 | return ret; |
| 62 | } |
| 63 | |
Kay Sievers | 8a25a2f | 2011-12-21 14:29:42 -0800 | [diff] [blame] | 64 | static ssize_t show_current_governor(struct device *dev, |
| 65 | struct device_attribute *attr, |
Rabin Vincent | 66198f3 | 2008-08-12 15:08:45 -0700 | [diff] [blame] | 66 | char *buf) |
Len Brown | 4f86d3a | 2007-10-03 18:58:00 -0400 | [diff] [blame] | 67 | { |
| 68 | ssize_t ret; |
| 69 | |
| 70 | mutex_lock(&cpuidle_lock); |
| 71 | if (cpuidle_curr_governor) |
| 72 | ret = sprintf(buf, "%s\n", cpuidle_curr_governor->name); |
| 73 | else |
| 74 | ret = sprintf(buf, "none\n"); |
| 75 | mutex_unlock(&cpuidle_lock); |
| 76 | |
| 77 | return ret; |
| 78 | } |
| 79 | |
Kay Sievers | 8a25a2f | 2011-12-21 14:29:42 -0800 | [diff] [blame] | 80 | static ssize_t store_current_governor(struct device *dev, |
| 81 | struct device_attribute *attr, |
Rabin Vincent | 66198f3 | 2008-08-12 15:08:45 -0700 | [diff] [blame] | 82 | const char *buf, size_t count) |
Len Brown | 4f86d3a | 2007-10-03 18:58:00 -0400 | [diff] [blame] | 83 | { |
| 84 | char gov_name[CPUIDLE_NAME_LEN]; |
| 85 | int ret = -EINVAL; |
| 86 | size_t len = count; |
| 87 | struct cpuidle_governor *gov; |
| 88 | |
| 89 | if (!len || len >= sizeof(gov_name)) |
| 90 | return -EINVAL; |
| 91 | |
| 92 | memcpy(gov_name, buf, len); |
| 93 | gov_name[len] = '\0'; |
| 94 | if (gov_name[len - 1] == '\n') |
| 95 | gov_name[--len] = '\0'; |
| 96 | |
| 97 | mutex_lock(&cpuidle_lock); |
| 98 | |
| 99 | list_for_each_entry(gov, &cpuidle_governors, governor_list) { |
| 100 | if (strlen(gov->name) == len && !strcmp(gov->name, gov_name)) { |
| 101 | ret = cpuidle_switch_governor(gov); |
| 102 | break; |
| 103 | } |
| 104 | } |
| 105 | |
| 106 | mutex_unlock(&cpuidle_lock); |
| 107 | |
| 108 | if (ret) |
| 109 | return ret; |
| 110 | else |
| 111 | return count; |
| 112 | } |
| 113 | |
Kay Sievers | 8a25a2f | 2011-12-21 14:29:42 -0800 | [diff] [blame] | 114 | static DEVICE_ATTR(current_driver, 0444, show_current_driver, NULL); |
| 115 | static DEVICE_ATTR(current_governor_ro, 0444, show_current_governor, NULL); |
Len Brown | 4f86d3a | 2007-10-03 18:58:00 -0400 | [diff] [blame] | 116 | |
Kay Sievers | 8a25a2f | 2011-12-21 14:29:42 -0800 | [diff] [blame] | 117 | static struct attribute *cpuidle_default_attrs[] = { |
| 118 | &dev_attr_current_driver.attr, |
| 119 | &dev_attr_current_governor_ro.attr, |
Len Brown | 4f86d3a | 2007-10-03 18:58:00 -0400 | [diff] [blame] | 120 | NULL |
| 121 | }; |
| 122 | |
Kay Sievers | 8a25a2f | 2011-12-21 14:29:42 -0800 | [diff] [blame] | 123 | static DEVICE_ATTR(available_governors, 0444, show_available_governors, NULL); |
| 124 | static DEVICE_ATTR(current_governor, 0644, show_current_governor, |
| 125 | store_current_governor); |
Len Brown | 4f86d3a | 2007-10-03 18:58:00 -0400 | [diff] [blame] | 126 | |
Kay Sievers | 8a25a2f | 2011-12-21 14:29:42 -0800 | [diff] [blame] | 127 | static struct attribute *cpuidle_switch_attrs[] = { |
| 128 | &dev_attr_available_governors.attr, |
| 129 | &dev_attr_current_driver.attr, |
| 130 | &dev_attr_current_governor.attr, |
Len Brown | 4f86d3a | 2007-10-03 18:58:00 -0400 | [diff] [blame] | 131 | NULL |
| 132 | }; |
| 133 | |
Kay Sievers | 8a25a2f | 2011-12-21 14:29:42 -0800 | [diff] [blame] | 134 | static struct attribute_group cpuidle_attr_group = { |
| 135 | .attrs = cpuidle_default_attrs, |
Len Brown | 4f86d3a | 2007-10-03 18:58:00 -0400 | [diff] [blame] | 136 | .name = "cpuidle", |
| 137 | }; |
| 138 | |
| 139 | /** |
Kay Sievers | 8a25a2f | 2011-12-21 14:29:42 -0800 | [diff] [blame] | 140 | * cpuidle_add_interface - add CPU global sysfs attributes |
Len Brown | 4f86d3a | 2007-10-03 18:58:00 -0400 | [diff] [blame] | 141 | */ |
Kay Sievers | 8a25a2f | 2011-12-21 14:29:42 -0800 | [diff] [blame] | 142 | int cpuidle_add_interface(struct device *dev) |
Len Brown | 4f86d3a | 2007-10-03 18:58:00 -0400 | [diff] [blame] | 143 | { |
| 144 | if (sysfs_switch) |
Kay Sievers | 8a25a2f | 2011-12-21 14:29:42 -0800 | [diff] [blame] | 145 | cpuidle_attr_group.attrs = cpuidle_switch_attrs; |
Len Brown | 4f86d3a | 2007-10-03 18:58:00 -0400 | [diff] [blame] | 146 | |
Kay Sievers | 8a25a2f | 2011-12-21 14:29:42 -0800 | [diff] [blame] | 147 | return sysfs_create_group(&dev->kobj, &cpuidle_attr_group); |
Len Brown | 4f86d3a | 2007-10-03 18:58:00 -0400 | [diff] [blame] | 148 | } |
| 149 | |
| 150 | /** |
Kay Sievers | 8a25a2f | 2011-12-21 14:29:42 -0800 | [diff] [blame] | 151 | * cpuidle_remove_interface - remove CPU global sysfs attributes |
Len Brown | 4f86d3a | 2007-10-03 18:58:00 -0400 | [diff] [blame] | 152 | */ |
Kay Sievers | 8a25a2f | 2011-12-21 14:29:42 -0800 | [diff] [blame] | 153 | void cpuidle_remove_interface(struct device *dev) |
Len Brown | 4f86d3a | 2007-10-03 18:58:00 -0400 | [diff] [blame] | 154 | { |
Kay Sievers | 8a25a2f | 2011-12-21 14:29:42 -0800 | [diff] [blame] | 155 | sysfs_remove_group(&dev->kobj, &cpuidle_attr_group); |
Len Brown | 4f86d3a | 2007-10-03 18:58:00 -0400 | [diff] [blame] | 156 | } |
| 157 | |
| 158 | struct cpuidle_attr { |
| 159 | struct attribute attr; |
| 160 | ssize_t (*show)(struct cpuidle_device *, char *); |
| 161 | ssize_t (*store)(struct cpuidle_device *, const char *, size_t count); |
| 162 | }; |
| 163 | |
| 164 | #define define_one_ro(_name, show) \ |
| 165 | static struct cpuidle_attr attr_##_name = __ATTR(_name, 0444, show, NULL) |
| 166 | #define define_one_rw(_name, show, store) \ |
| 167 | static struct cpuidle_attr attr_##_name = __ATTR(_name, 0644, show, store) |
| 168 | |
| 169 | #define kobj_to_cpuidledev(k) container_of(k, struct cpuidle_device, kobj) |
| 170 | #define attr_to_cpuidleattr(a) container_of(a, struct cpuidle_attr, attr) |
| 171 | static ssize_t cpuidle_show(struct kobject * kobj, struct attribute * attr ,char * buf) |
| 172 | { |
| 173 | int ret = -EIO; |
| 174 | struct cpuidle_device *dev = kobj_to_cpuidledev(kobj); |
| 175 | struct cpuidle_attr * cattr = attr_to_cpuidleattr(attr); |
| 176 | |
| 177 | if (cattr->show) { |
| 178 | mutex_lock(&cpuidle_lock); |
| 179 | ret = cattr->show(dev, buf); |
| 180 | mutex_unlock(&cpuidle_lock); |
| 181 | } |
| 182 | return ret; |
| 183 | } |
| 184 | |
| 185 | static ssize_t cpuidle_store(struct kobject * kobj, struct attribute * attr, |
| 186 | const char * buf, size_t count) |
| 187 | { |
| 188 | int ret = -EIO; |
| 189 | struct cpuidle_device *dev = kobj_to_cpuidledev(kobj); |
| 190 | struct cpuidle_attr * cattr = attr_to_cpuidleattr(attr); |
| 191 | |
| 192 | if (cattr->store) { |
| 193 | mutex_lock(&cpuidle_lock); |
| 194 | ret = cattr->store(dev, buf, count); |
| 195 | mutex_unlock(&cpuidle_lock); |
| 196 | } |
| 197 | return ret; |
| 198 | } |
| 199 | |
Emese Revfy | 52cf25d | 2010-01-19 02:58:23 +0100 | [diff] [blame] | 200 | static const struct sysfs_ops cpuidle_sysfs_ops = { |
Len Brown | 4f86d3a | 2007-10-03 18:58:00 -0400 | [diff] [blame] | 201 | .show = cpuidle_show, |
| 202 | .store = cpuidle_store, |
| 203 | }; |
| 204 | |
| 205 | static void cpuidle_sysfs_release(struct kobject *kobj) |
| 206 | { |
| 207 | struct cpuidle_device *dev = kobj_to_cpuidledev(kobj); |
| 208 | |
| 209 | complete(&dev->kobj_unregister); |
| 210 | } |
| 211 | |
| 212 | static struct kobj_type ktype_cpuidle = { |
| 213 | .sysfs_ops = &cpuidle_sysfs_ops, |
| 214 | .release = cpuidle_sysfs_release, |
| 215 | }; |
| 216 | |
| 217 | struct cpuidle_state_attr { |
| 218 | struct attribute attr; |
Deepthi Dharwar | 4202735 | 2011-10-28 16:20:33 +0530 | [diff] [blame] | 219 | ssize_t (*show)(struct cpuidle_state *, \ |
| 220 | struct cpuidle_state_usage *, char *); |
ShuoX Liu | dc7fd27 | 2012-07-03 19:05:31 +0200 | [diff] [blame] | 221 | ssize_t (*store)(struct cpuidle_state *, \ |
| 222 | struct cpuidle_state_usage *, const char *, size_t); |
Len Brown | 4f86d3a | 2007-10-03 18:58:00 -0400 | [diff] [blame] | 223 | }; |
| 224 | |
| 225 | #define define_one_state_ro(_name, show) \ |
| 226 | static struct cpuidle_state_attr attr_##_name = __ATTR(_name, 0444, show, NULL) |
| 227 | |
ShuoX Liu | 3a53396 | 2012-03-28 15:19:11 -0700 | [diff] [blame] | 228 | #define define_one_state_rw(_name, show, store) \ |
| 229 | static struct cpuidle_state_attr attr_##_name = __ATTR(_name, 0644, show, store) |
| 230 | |
Len Brown | 4f86d3a | 2007-10-03 18:58:00 -0400 | [diff] [blame] | 231 | #define define_show_state_function(_name) \ |
Deepthi Dharwar | 4202735 | 2011-10-28 16:20:33 +0530 | [diff] [blame] | 232 | static ssize_t show_state_##_name(struct cpuidle_state *state, \ |
| 233 | struct cpuidle_state_usage *state_usage, char *buf) \ |
Len Brown | 4f86d3a | 2007-10-03 18:58:00 -0400 | [diff] [blame] | 234 | { \ |
| 235 | return sprintf(buf, "%u\n", state->_name);\ |
| 236 | } |
| 237 | |
ShuoX Liu | dc7fd27 | 2012-07-03 19:05:31 +0200 | [diff] [blame] | 238 | #define define_store_state_ull_function(_name) \ |
ShuoX Liu | 3a53396 | 2012-03-28 15:19:11 -0700 | [diff] [blame] | 239 | static ssize_t store_state_##_name(struct cpuidle_state *state, \ |
ShuoX Liu | dc7fd27 | 2012-07-03 19:05:31 +0200 | [diff] [blame] | 240 | struct cpuidle_state_usage *state_usage, \ |
ShuoX Liu | 3a53396 | 2012-03-28 15:19:11 -0700 | [diff] [blame] | 241 | const char *buf, size_t size) \ |
| 242 | { \ |
ShuoX Liu | dc7fd27 | 2012-07-03 19:05:31 +0200 | [diff] [blame] | 243 | unsigned long long value; \ |
ShuoX Liu | 3a53396 | 2012-03-28 15:19:11 -0700 | [diff] [blame] | 244 | int err; \ |
| 245 | if (!capable(CAP_SYS_ADMIN)) \ |
| 246 | return -EPERM; \ |
ShuoX Liu | dc7fd27 | 2012-07-03 19:05:31 +0200 | [diff] [blame] | 247 | err = kstrtoull(buf, 0, &value); \ |
ShuoX Liu | 3a53396 | 2012-03-28 15:19:11 -0700 | [diff] [blame] | 248 | if (err) \ |
| 249 | return err; \ |
| 250 | if (value) \ |
ShuoX Liu | dc7fd27 | 2012-07-03 19:05:31 +0200 | [diff] [blame] | 251 | state_usage->_name = 1; \ |
ShuoX Liu | 3a53396 | 2012-03-28 15:19:11 -0700 | [diff] [blame] | 252 | else \ |
ShuoX Liu | dc7fd27 | 2012-07-03 19:05:31 +0200 | [diff] [blame] | 253 | state_usage->_name = 0; \ |
ShuoX Liu | 3a53396 | 2012-03-28 15:19:11 -0700 | [diff] [blame] | 254 | return size; \ |
| 255 | } |
| 256 | |
Yi Yang | 8b78cf6 | 2008-02-25 08:46:12 +0800 | [diff] [blame] | 257 | #define define_show_state_ull_function(_name) \ |
Deepthi Dharwar | 4202735 | 2011-10-28 16:20:33 +0530 | [diff] [blame] | 258 | static ssize_t show_state_##_name(struct cpuidle_state *state, \ |
| 259 | struct cpuidle_state_usage *state_usage, char *buf) \ |
Yi Yang | 8b78cf6 | 2008-02-25 08:46:12 +0800 | [diff] [blame] | 260 | { \ |
Deepthi Dharwar | 4202735 | 2011-10-28 16:20:33 +0530 | [diff] [blame] | 261 | return sprintf(buf, "%llu\n", state_usage->_name);\ |
Yi Yang | 8b78cf6 | 2008-02-25 08:46:12 +0800 | [diff] [blame] | 262 | } |
| 263 | |
Venkatesh Pallipadi | 4fcb2fc | 2008-02-11 17:46:31 -0800 | [diff] [blame] | 264 | #define define_show_state_str_function(_name) \ |
Deepthi Dharwar | 4202735 | 2011-10-28 16:20:33 +0530 | [diff] [blame] | 265 | static ssize_t show_state_##_name(struct cpuidle_state *state, \ |
| 266 | struct cpuidle_state_usage *state_usage, char *buf) \ |
Venkatesh Pallipadi | 4fcb2fc | 2008-02-11 17:46:31 -0800 | [diff] [blame] | 267 | { \ |
| 268 | if (state->_name[0] == '\0')\ |
| 269 | return sprintf(buf, "<null>\n");\ |
| 270 | return sprintf(buf, "%s\n", state->_name);\ |
Len Brown | 4f86d3a | 2007-10-03 18:58:00 -0400 | [diff] [blame] | 271 | } |
| 272 | |
| 273 | define_show_state_function(exit_latency) |
| 274 | define_show_state_function(power_usage) |
Yi Yang | 8b78cf6 | 2008-02-25 08:46:12 +0800 | [diff] [blame] | 275 | define_show_state_ull_function(usage) |
| 276 | define_show_state_ull_function(time) |
Venkatesh Pallipadi | 4fcb2fc | 2008-02-11 17:46:31 -0800 | [diff] [blame] | 277 | define_show_state_str_function(name) |
| 278 | define_show_state_str_function(desc) |
ShuoX Liu | dc7fd27 | 2012-07-03 19:05:31 +0200 | [diff] [blame] | 279 | define_show_state_ull_function(disable) |
| 280 | define_store_state_ull_function(disable) |
Venkatesh Pallipadi | 4fcb2fc | 2008-02-11 17:46:31 -0800 | [diff] [blame] | 281 | |
Len Brown | 4f86d3a | 2007-10-03 18:58:00 -0400 | [diff] [blame] | 282 | define_one_state_ro(name, show_state_name); |
Venkatesh Pallipadi | 4fcb2fc | 2008-02-11 17:46:31 -0800 | [diff] [blame] | 283 | define_one_state_ro(desc, show_state_desc); |
Len Brown | 4f86d3a | 2007-10-03 18:58:00 -0400 | [diff] [blame] | 284 | define_one_state_ro(latency, show_state_exit_latency); |
| 285 | define_one_state_ro(power, show_state_power_usage); |
| 286 | define_one_state_ro(usage, show_state_usage); |
| 287 | define_one_state_ro(time, show_state_time); |
ShuoX Liu | 3a53396 | 2012-03-28 15:19:11 -0700 | [diff] [blame] | 288 | define_one_state_rw(disable, show_state_disable, store_state_disable); |
Len Brown | 4f86d3a | 2007-10-03 18:58:00 -0400 | [diff] [blame] | 289 | |
| 290 | static struct attribute *cpuidle_state_default_attrs[] = { |
| 291 | &attr_name.attr, |
Venkatesh Pallipadi | 4fcb2fc | 2008-02-11 17:46:31 -0800 | [diff] [blame] | 292 | &attr_desc.attr, |
Len Brown | 4f86d3a | 2007-10-03 18:58:00 -0400 | [diff] [blame] | 293 | &attr_latency.attr, |
| 294 | &attr_power.attr, |
| 295 | &attr_usage.attr, |
| 296 | &attr_time.attr, |
ShuoX Liu | 3a53396 | 2012-03-28 15:19:11 -0700 | [diff] [blame] | 297 | &attr_disable.attr, |
Len Brown | 4f86d3a | 2007-10-03 18:58:00 -0400 | [diff] [blame] | 298 | NULL |
| 299 | }; |
| 300 | |
Daniel Lezcano | 349631e | 2012-10-31 01:05:16 +0100 | [diff] [blame] | 301 | struct cpuidle_state_kobj { |
| 302 | struct cpuidle_state *state; |
| 303 | struct cpuidle_state_usage *state_usage; |
| 304 | struct completion kobj_unregister; |
| 305 | struct kobject kobj; |
| 306 | }; |
| 307 | |
Len Brown | 4f86d3a | 2007-10-03 18:58:00 -0400 | [diff] [blame] | 308 | #define kobj_to_state_obj(k) container_of(k, struct cpuidle_state_kobj, kobj) |
| 309 | #define kobj_to_state(k) (kobj_to_state_obj(k)->state) |
Deepthi Dharwar | 4202735 | 2011-10-28 16:20:33 +0530 | [diff] [blame] | 310 | #define kobj_to_state_usage(k) (kobj_to_state_obj(k)->state_usage) |
Len Brown | 4f86d3a | 2007-10-03 18:58:00 -0400 | [diff] [blame] | 311 | #define attr_to_stateattr(a) container_of(a, struct cpuidle_state_attr, attr) |
| 312 | static ssize_t cpuidle_state_show(struct kobject * kobj, |
| 313 | struct attribute * attr ,char * buf) |
| 314 | { |
| 315 | int ret = -EIO; |
| 316 | struct cpuidle_state *state = kobj_to_state(kobj); |
Deepthi Dharwar | 4202735 | 2011-10-28 16:20:33 +0530 | [diff] [blame] | 317 | struct cpuidle_state_usage *state_usage = kobj_to_state_usage(kobj); |
Len Brown | 4f86d3a | 2007-10-03 18:58:00 -0400 | [diff] [blame] | 318 | struct cpuidle_state_attr * cattr = attr_to_stateattr(attr); |
| 319 | |
| 320 | if (cattr->show) |
Deepthi Dharwar | 4202735 | 2011-10-28 16:20:33 +0530 | [diff] [blame] | 321 | ret = cattr->show(state, state_usage, buf); |
Len Brown | 4f86d3a | 2007-10-03 18:58:00 -0400 | [diff] [blame] | 322 | |
| 323 | return ret; |
| 324 | } |
| 325 | |
ShuoX Liu | 3a53396 | 2012-03-28 15:19:11 -0700 | [diff] [blame] | 326 | static ssize_t cpuidle_state_store(struct kobject *kobj, |
| 327 | struct attribute *attr, const char *buf, size_t size) |
| 328 | { |
| 329 | int ret = -EIO; |
| 330 | struct cpuidle_state *state = kobj_to_state(kobj); |
ShuoX Liu | dc7fd27 | 2012-07-03 19:05:31 +0200 | [diff] [blame] | 331 | struct cpuidle_state_usage *state_usage = kobj_to_state_usage(kobj); |
ShuoX Liu | 3a53396 | 2012-03-28 15:19:11 -0700 | [diff] [blame] | 332 | struct cpuidle_state_attr *cattr = attr_to_stateattr(attr); |
| 333 | |
| 334 | if (cattr->store) |
ShuoX Liu | dc7fd27 | 2012-07-03 19:05:31 +0200 | [diff] [blame] | 335 | ret = cattr->store(state, state_usage, buf, size); |
ShuoX Liu | 3a53396 | 2012-03-28 15:19:11 -0700 | [diff] [blame] | 336 | |
| 337 | return ret; |
| 338 | } |
| 339 | |
Emese Revfy | 52cf25d | 2010-01-19 02:58:23 +0100 | [diff] [blame] | 340 | static const struct sysfs_ops cpuidle_state_sysfs_ops = { |
Len Brown | 4f86d3a | 2007-10-03 18:58:00 -0400 | [diff] [blame] | 341 | .show = cpuidle_state_show, |
ShuoX Liu | 3a53396 | 2012-03-28 15:19:11 -0700 | [diff] [blame] | 342 | .store = cpuidle_state_store, |
Len Brown | 4f86d3a | 2007-10-03 18:58:00 -0400 | [diff] [blame] | 343 | }; |
| 344 | |
| 345 | static void cpuidle_state_sysfs_release(struct kobject *kobj) |
| 346 | { |
| 347 | struct cpuidle_state_kobj *state_obj = kobj_to_state_obj(kobj); |
| 348 | |
| 349 | complete(&state_obj->kobj_unregister); |
| 350 | } |
| 351 | |
| 352 | static struct kobj_type ktype_state_cpuidle = { |
| 353 | .sysfs_ops = &cpuidle_state_sysfs_ops, |
| 354 | .default_attrs = cpuidle_state_default_attrs, |
| 355 | .release = cpuidle_state_sysfs_release, |
| 356 | }; |
| 357 | |
Jesper Juhl | 42b16b3 | 2011-01-17 00:09:38 +0100 | [diff] [blame] | 358 | static inline void cpuidle_free_state_kobj(struct cpuidle_device *device, int i) |
Len Brown | 4f86d3a | 2007-10-03 18:58:00 -0400 | [diff] [blame] | 359 | { |
Greg Kroah-Hartman | c10997f | 2007-12-20 08:13:05 -0800 | [diff] [blame] | 360 | kobject_put(&device->kobjs[i]->kobj); |
Len Brown | 4f86d3a | 2007-10-03 18:58:00 -0400 | [diff] [blame] | 361 | wait_for_completion(&device->kobjs[i]->kobj_unregister); |
| 362 | kfree(device->kobjs[i]); |
| 363 | device->kobjs[i] = NULL; |
| 364 | } |
| 365 | |
| 366 | /** |
Daniel Lezcano | bf4d1b5 | 2012-10-31 16:44:48 +0000 | [diff] [blame] | 367 | * cpuidle_add_state_sysfs - adds cpuidle states sysfs attributes |
Len Brown | 4f86d3a | 2007-10-03 18:58:00 -0400 | [diff] [blame] | 368 | * @device: the target device |
| 369 | */ |
Daniel Lezcano | bf4d1b5 | 2012-10-31 16:44:48 +0000 | [diff] [blame] | 370 | static int cpuidle_add_state_sysfs(struct cpuidle_device *device) |
Len Brown | 4f86d3a | 2007-10-03 18:58:00 -0400 | [diff] [blame] | 371 | { |
| 372 | int i, ret = -ENOMEM; |
| 373 | struct cpuidle_state_kobj *kobj; |
Daniel Lezcano | bf4d1b5 | 2012-10-31 16:44:48 +0000 | [diff] [blame] | 374 | struct cpuidle_driver *drv = cpuidle_get_cpu_driver(device); |
Len Brown | 4f86d3a | 2007-10-03 18:58:00 -0400 | [diff] [blame] | 375 | |
| 376 | /* state statistics */ |
Krzysztof Mazur | 392370e | 2013-01-11 23:20:09 +0100 | [diff] [blame] | 377 | for (i = 0; i < device->state_count; i++) { |
Len Brown | 4f86d3a | 2007-10-03 18:58:00 -0400 | [diff] [blame] | 378 | kobj = kzalloc(sizeof(struct cpuidle_state_kobj), GFP_KERNEL); |
| 379 | if (!kobj) |
| 380 | goto error_state; |
Deepthi Dharwar | 46bcfad | 2011-10-28 16:20:42 +0530 | [diff] [blame] | 381 | kobj->state = &drv->states[i]; |
Deepthi Dharwar | 4202735 | 2011-10-28 16:20:33 +0530 | [diff] [blame] | 382 | kobj->state_usage = &device->states_usage[i]; |
Len Brown | 4f86d3a | 2007-10-03 18:58:00 -0400 | [diff] [blame] | 383 | init_completion(&kobj->kobj_unregister); |
| 384 | |
Daniel Lezcano | e45a00d | 2012-10-26 12:26:32 +0200 | [diff] [blame] | 385 | ret = kobject_init_and_add(&kobj->kobj, &ktype_state_cpuidle, |
| 386 | &device->kobj, "state%d", i); |
Len Brown | 4f86d3a | 2007-10-03 18:58:00 -0400 | [diff] [blame] | 387 | if (ret) { |
| 388 | kfree(kobj); |
| 389 | goto error_state; |
| 390 | } |
Greg Kroah-Hartman | 94f57f3 | 2007-12-17 15:54:39 -0400 | [diff] [blame] | 391 | kobject_uevent(&kobj->kobj, KOBJ_ADD); |
Len Brown | 4f86d3a | 2007-10-03 18:58:00 -0400 | [diff] [blame] | 392 | device->kobjs[i] = kobj; |
| 393 | } |
| 394 | |
| 395 | return 0; |
| 396 | |
| 397 | error_state: |
| 398 | for (i = i - 1; i >= 0; i--) |
| 399 | cpuidle_free_state_kobj(device, i); |
| 400 | return ret; |
| 401 | } |
| 402 | |
| 403 | /** |
Daniel Lezcano | bf4d1b5 | 2012-10-31 16:44:48 +0000 | [diff] [blame] | 404 | * cpuidle_remove_driver_sysfs - removes the cpuidle states sysfs attributes |
Len Brown | 4f86d3a | 2007-10-03 18:58:00 -0400 | [diff] [blame] | 405 | * @device: the target device |
| 406 | */ |
Daniel Lezcano | bf4d1b5 | 2012-10-31 16:44:48 +0000 | [diff] [blame] | 407 | static void cpuidle_remove_state_sysfs(struct cpuidle_device *device) |
Len Brown | 4f86d3a | 2007-10-03 18:58:00 -0400 | [diff] [blame] | 408 | { |
| 409 | int i; |
| 410 | |
| 411 | for (i = 0; i < device->state_count; i++) |
| 412 | cpuidle_free_state_kobj(device, i); |
| 413 | } |
| 414 | |
Daniel Lezcano | bf4d1b5 | 2012-10-31 16:44:48 +0000 | [diff] [blame] | 415 | #ifdef CONFIG_CPU_IDLE_MULTIPLE_DRIVERS |
| 416 | #define kobj_to_driver_kobj(k) container_of(k, struct cpuidle_driver_kobj, kobj) |
| 417 | #define attr_to_driver_attr(a) container_of(a, struct cpuidle_driver_attr, attr) |
| 418 | |
| 419 | #define define_one_driver_ro(_name, show) \ |
| 420 | static struct cpuidle_driver_attr attr_driver_##_name = \ |
| 421 | __ATTR(_name, 0644, show, NULL) |
| 422 | |
| 423 | struct cpuidle_driver_kobj { |
| 424 | struct cpuidle_driver *drv; |
| 425 | struct completion kobj_unregister; |
| 426 | struct kobject kobj; |
| 427 | }; |
| 428 | |
| 429 | struct cpuidle_driver_attr { |
| 430 | struct attribute attr; |
| 431 | ssize_t (*show)(struct cpuidle_driver *, char *); |
| 432 | ssize_t (*store)(struct cpuidle_driver *, const char *, size_t); |
| 433 | }; |
| 434 | |
| 435 | static ssize_t show_driver_name(struct cpuidle_driver *drv, char *buf) |
| 436 | { |
| 437 | ssize_t ret; |
| 438 | |
| 439 | spin_lock(&cpuidle_driver_lock); |
| 440 | ret = sprintf(buf, "%s\n", drv ? drv->name : "none"); |
| 441 | spin_unlock(&cpuidle_driver_lock); |
| 442 | |
| 443 | return ret; |
| 444 | } |
| 445 | |
| 446 | static void cpuidle_driver_sysfs_release(struct kobject *kobj) |
| 447 | { |
| 448 | struct cpuidle_driver_kobj *driver_kobj = kobj_to_driver_kobj(kobj); |
| 449 | complete(&driver_kobj->kobj_unregister); |
| 450 | } |
| 451 | |
| 452 | static ssize_t cpuidle_driver_show(struct kobject *kobj, struct attribute * attr, |
| 453 | char * buf) |
| 454 | { |
| 455 | int ret = -EIO; |
| 456 | struct cpuidle_driver_kobj *driver_kobj = kobj_to_driver_kobj(kobj); |
| 457 | struct cpuidle_driver_attr *dattr = attr_to_driver_attr(attr); |
| 458 | |
| 459 | if (dattr->show) |
| 460 | ret = dattr->show(driver_kobj->drv, buf); |
| 461 | |
| 462 | return ret; |
| 463 | } |
| 464 | |
| 465 | static ssize_t cpuidle_driver_store(struct kobject *kobj, struct attribute *attr, |
| 466 | const char *buf, size_t size) |
| 467 | { |
| 468 | int ret = -EIO; |
| 469 | struct cpuidle_driver_kobj *driver_kobj = kobj_to_driver_kobj(kobj); |
| 470 | struct cpuidle_driver_attr *dattr = attr_to_driver_attr(attr); |
| 471 | |
| 472 | if (dattr->store) |
| 473 | ret = dattr->store(driver_kobj->drv, buf, size); |
| 474 | |
| 475 | return ret; |
| 476 | } |
| 477 | |
| 478 | define_one_driver_ro(name, show_driver_name); |
| 479 | |
| 480 | static const struct sysfs_ops cpuidle_driver_sysfs_ops = { |
| 481 | .show = cpuidle_driver_show, |
| 482 | .store = cpuidle_driver_store, |
| 483 | }; |
| 484 | |
| 485 | static struct attribute *cpuidle_driver_default_attrs[] = { |
| 486 | &attr_driver_name.attr, |
| 487 | NULL |
| 488 | }; |
| 489 | |
| 490 | static struct kobj_type ktype_driver_cpuidle = { |
| 491 | .sysfs_ops = &cpuidle_driver_sysfs_ops, |
| 492 | .default_attrs = cpuidle_driver_default_attrs, |
| 493 | .release = cpuidle_driver_sysfs_release, |
| 494 | }; |
| 495 | |
| 496 | /** |
| 497 | * cpuidle_add_driver_sysfs - adds the driver name sysfs attribute |
| 498 | * @device: the target device |
| 499 | */ |
| 500 | static int cpuidle_add_driver_sysfs(struct cpuidle_device *dev) |
| 501 | { |
| 502 | struct cpuidle_driver_kobj *kdrv; |
| 503 | struct cpuidle_driver *drv = cpuidle_get_cpu_driver(dev); |
| 504 | int ret; |
| 505 | |
| 506 | kdrv = kzalloc(sizeof(*kdrv), GFP_KERNEL); |
| 507 | if (!kdrv) |
| 508 | return -ENOMEM; |
| 509 | |
| 510 | kdrv->drv = drv; |
| 511 | init_completion(&kdrv->kobj_unregister); |
| 512 | |
| 513 | ret = kobject_init_and_add(&kdrv->kobj, &ktype_driver_cpuidle, |
| 514 | &dev->kobj, "driver"); |
| 515 | if (ret) { |
| 516 | kfree(kdrv); |
| 517 | return ret; |
| 518 | } |
| 519 | |
| 520 | kobject_uevent(&kdrv->kobj, KOBJ_ADD); |
| 521 | dev->kobj_driver = kdrv; |
| 522 | |
| 523 | return ret; |
| 524 | } |
| 525 | |
| 526 | /** |
| 527 | * cpuidle_remove_driver_sysfs - removes the driver name sysfs attribute |
| 528 | * @device: the target device |
| 529 | */ |
| 530 | static void cpuidle_remove_driver_sysfs(struct cpuidle_device *dev) |
| 531 | { |
| 532 | struct cpuidle_driver_kobj *kdrv = dev->kobj_driver; |
| 533 | kobject_put(&kdrv->kobj); |
| 534 | wait_for_completion(&kdrv->kobj_unregister); |
| 535 | kfree(kdrv); |
| 536 | } |
| 537 | #else |
| 538 | static inline int cpuidle_add_driver_sysfs(struct cpuidle_device *dev) |
| 539 | { |
| 540 | return 0; |
| 541 | } |
| 542 | |
| 543 | static inline void cpuidle_remove_driver_sysfs(struct cpuidle_device *dev) |
| 544 | { |
| 545 | ; |
| 546 | } |
| 547 | #endif |
| 548 | |
| 549 | /** |
| 550 | * cpuidle_add_device_sysfs - adds device specific sysfs attributes |
| 551 | * @device: the target device |
| 552 | */ |
| 553 | int cpuidle_add_device_sysfs(struct cpuidle_device *device) |
| 554 | { |
| 555 | int ret; |
| 556 | |
| 557 | ret = cpuidle_add_state_sysfs(device); |
| 558 | if (ret) |
| 559 | return ret; |
| 560 | |
| 561 | ret = cpuidle_add_driver_sysfs(device); |
| 562 | if (ret) |
| 563 | cpuidle_remove_state_sysfs(device); |
| 564 | return ret; |
| 565 | } |
| 566 | |
| 567 | /** |
| 568 | * cpuidle_remove_device_sysfs : removes device specific sysfs attributes |
| 569 | * @device : the target device |
| 570 | */ |
| 571 | void cpuidle_remove_device_sysfs(struct cpuidle_device *device) |
| 572 | { |
| 573 | cpuidle_remove_driver_sysfs(device); |
| 574 | cpuidle_remove_state_sysfs(device); |
| 575 | } |
| 576 | |
Len Brown | 4f86d3a | 2007-10-03 18:58:00 -0400 | [diff] [blame] | 577 | /** |
| 578 | * cpuidle_add_sysfs - creates a sysfs instance for the target device |
Kay Sievers | 8a25a2f | 2011-12-21 14:29:42 -0800 | [diff] [blame] | 579 | * @dev: the target device |
Len Brown | 4f86d3a | 2007-10-03 18:58:00 -0400 | [diff] [blame] | 580 | */ |
Daniel Lezcano | 1aef40e | 2012-10-26 12:26:24 +0200 | [diff] [blame] | 581 | int cpuidle_add_sysfs(struct cpuidle_device *dev) |
Len Brown | 4f86d3a | 2007-10-03 18:58:00 -0400 | [diff] [blame] | 582 | { |
Daniel Lezcano | 1aef40e | 2012-10-26 12:26:24 +0200 | [diff] [blame] | 583 | struct device *cpu_dev = get_cpu_device((unsigned long)dev->cpu); |
Greg Kroah-Hartman | 94f57f3 | 2007-12-17 15:54:39 -0400 | [diff] [blame] | 584 | int error; |
Len Brown | 4f86d3a | 2007-10-03 18:58:00 -0400 | [diff] [blame] | 585 | |
Daniel Lezcano | e45a00d | 2012-10-26 12:26:32 +0200 | [diff] [blame] | 586 | init_completion(&dev->kobj_unregister); |
| 587 | |
Kay Sievers | 8a25a2f | 2011-12-21 14:29:42 -0800 | [diff] [blame] | 588 | error = kobject_init_and_add(&dev->kobj, &ktype_cpuidle, &cpu_dev->kobj, |
Greg Kroah-Hartman | 94f57f3 | 2007-12-17 15:54:39 -0400 | [diff] [blame] | 589 | "cpuidle"); |
| 590 | if (!error) |
| 591 | kobject_uevent(&dev->kobj, KOBJ_ADD); |
| 592 | return error; |
Len Brown | 4f86d3a | 2007-10-03 18:58:00 -0400 | [diff] [blame] | 593 | } |
| 594 | |
| 595 | /** |
| 596 | * cpuidle_remove_sysfs - deletes a sysfs instance on the target device |
Kay Sievers | 8a25a2f | 2011-12-21 14:29:42 -0800 | [diff] [blame] | 597 | * @dev: the target device |
Len Brown | 4f86d3a | 2007-10-03 18:58:00 -0400 | [diff] [blame] | 598 | */ |
Daniel Lezcano | 1aef40e | 2012-10-26 12:26:24 +0200 | [diff] [blame] | 599 | void cpuidle_remove_sysfs(struct cpuidle_device *dev) |
Len Brown | 4f86d3a | 2007-10-03 18:58:00 -0400 | [diff] [blame] | 600 | { |
Greg Kroah-Hartman | c10997f | 2007-12-20 08:13:05 -0800 | [diff] [blame] | 601 | kobject_put(&dev->kobj); |
Daniel Lezcano | e45a00d | 2012-10-26 12:26:32 +0200 | [diff] [blame] | 602 | wait_for_completion(&dev->kobj_unregister); |
Len Brown | 4f86d3a | 2007-10-03 18:58:00 -0400 | [diff] [blame] | 603 | } |