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