.. | .. |
---|
18 | 18 | |
---|
19 | 19 | #include "cpuidle.h" |
---|
20 | 20 | |
---|
21 | | -static unsigned int sysfs_switch; |
---|
22 | | -static int __init cpuidle_sysfs_setup(char *unused) |
---|
23 | | -{ |
---|
24 | | - sysfs_switch = 1; |
---|
25 | | - return 1; |
---|
26 | | -} |
---|
27 | | -__setup("cpuidle_sysfs_switch", cpuidle_sysfs_setup); |
---|
28 | | - |
---|
29 | 21 | static ssize_t show_available_governors(struct device *dev, |
---|
30 | 22 | struct device_attribute *attr, |
---|
31 | 23 | char *buf) |
---|
.. | .. |
---|
35 | 27 | |
---|
36 | 28 | mutex_lock(&cpuidle_lock); |
---|
37 | 29 | list_for_each_entry(tmp, &cpuidle_governors, governor_list) { |
---|
38 | | - if (i >= (ssize_t) ((PAGE_SIZE/sizeof(char)) - |
---|
39 | | - CPUIDLE_NAME_LEN - 2)) |
---|
| 30 | + if (i >= (ssize_t) (PAGE_SIZE - (CPUIDLE_NAME_LEN + 2))) |
---|
40 | 31 | goto out; |
---|
41 | | - i += scnprintf(&buf[i], CPUIDLE_NAME_LEN, "%s ", tmp->name); |
---|
| 32 | + |
---|
| 33 | + i += scnprintf(&buf[i], CPUIDLE_NAME_LEN + 1, "%s ", tmp->name); |
---|
42 | 34 | } |
---|
43 | 35 | |
---|
44 | 36 | out: |
---|
.. | .. |
---|
85 | 77 | struct device_attribute *attr, |
---|
86 | 78 | const char *buf, size_t count) |
---|
87 | 79 | { |
---|
88 | | - char gov_name[CPUIDLE_NAME_LEN]; |
---|
89 | | - int ret = -EINVAL; |
---|
90 | | - size_t len = count; |
---|
| 80 | + char gov_name[CPUIDLE_NAME_LEN + 1]; |
---|
| 81 | + int ret; |
---|
91 | 82 | struct cpuidle_governor *gov; |
---|
92 | 83 | |
---|
93 | | - if (!len || len >= sizeof(gov_name)) |
---|
| 84 | + ret = sscanf(buf, "%" __stringify(CPUIDLE_NAME_LEN) "s", gov_name); |
---|
| 85 | + if (ret != 1) |
---|
94 | 86 | return -EINVAL; |
---|
95 | 87 | |
---|
96 | | - memcpy(gov_name, buf, len); |
---|
97 | | - gov_name[len] = '\0'; |
---|
98 | | - if (gov_name[len - 1] == '\n') |
---|
99 | | - gov_name[--len] = '\0'; |
---|
100 | | - |
---|
101 | 88 | mutex_lock(&cpuidle_lock); |
---|
102 | | - |
---|
| 89 | + ret = -EINVAL; |
---|
103 | 90 | list_for_each_entry(gov, &cpuidle_governors, governor_list) { |
---|
104 | | - if (strlen(gov->name) == len && !strcmp(gov->name, gov_name)) { |
---|
| 91 | + if (!strncmp(gov->name, gov_name, CPUIDLE_NAME_LEN)) { |
---|
105 | 92 | ret = cpuidle_switch_governor(gov); |
---|
106 | 93 | break; |
---|
107 | 94 | } |
---|
108 | 95 | } |
---|
109 | | - |
---|
110 | 96 | mutex_unlock(&cpuidle_lock); |
---|
111 | 97 | |
---|
112 | | - if (ret) |
---|
113 | | - return ret; |
---|
114 | | - else |
---|
115 | | - return count; |
---|
| 98 | + return ret ? ret : count; |
---|
116 | 99 | } |
---|
117 | 100 | |
---|
| 101 | +static DEVICE_ATTR(available_governors, 0444, show_available_governors, NULL); |
---|
118 | 102 | static DEVICE_ATTR(current_driver, 0444, show_current_driver, NULL); |
---|
| 103 | +static DEVICE_ATTR(current_governor, 0644, show_current_governor, |
---|
| 104 | + store_current_governor); |
---|
119 | 105 | static DEVICE_ATTR(current_governor_ro, 0444, show_current_governor, NULL); |
---|
120 | 106 | |
---|
121 | | -static struct attribute *cpuidle_default_attrs[] = { |
---|
| 107 | +static struct attribute *cpuidle_attrs[] = { |
---|
| 108 | + &dev_attr_available_governors.attr, |
---|
122 | 109 | &dev_attr_current_driver.attr, |
---|
| 110 | + &dev_attr_current_governor.attr, |
---|
123 | 111 | &dev_attr_current_governor_ro.attr, |
---|
124 | 112 | NULL |
---|
125 | 113 | }; |
---|
126 | 114 | |
---|
127 | | -static DEVICE_ATTR(available_governors, 0444, show_available_governors, NULL); |
---|
128 | | -static DEVICE_ATTR(current_governor, 0644, show_current_governor, |
---|
129 | | - store_current_governor); |
---|
130 | | - |
---|
131 | | -static struct attribute *cpuidle_switch_attrs[] = { |
---|
132 | | - &dev_attr_available_governors.attr, |
---|
133 | | - &dev_attr_current_driver.attr, |
---|
134 | | - &dev_attr_current_governor.attr, |
---|
135 | | - NULL |
---|
136 | | -}; |
---|
137 | | - |
---|
138 | 115 | static struct attribute_group cpuidle_attr_group = { |
---|
139 | | - .attrs = cpuidle_default_attrs, |
---|
| 116 | + .attrs = cpuidle_attrs, |
---|
140 | 117 | .name = "cpuidle", |
---|
141 | 118 | }; |
---|
142 | 119 | |
---|
143 | 120 | /** |
---|
144 | 121 | * cpuidle_add_interface - add CPU global sysfs attributes |
---|
| 122 | + * @dev: the target device |
---|
145 | 123 | */ |
---|
146 | 124 | int cpuidle_add_interface(struct device *dev) |
---|
147 | 125 | { |
---|
148 | | - if (sysfs_switch) |
---|
149 | | - cpuidle_attr_group.attrs = cpuidle_switch_attrs; |
---|
150 | | - |
---|
151 | 126 | return sysfs_create_group(&dev->kobj, &cpuidle_attr_group); |
---|
152 | 127 | } |
---|
153 | 128 | |
---|
154 | 129 | /** |
---|
155 | 130 | * cpuidle_remove_interface - remove CPU global sysfs attributes |
---|
| 131 | + * @dev: the target device |
---|
156 | 132 | */ |
---|
157 | 133 | void cpuidle_remove_interface(struct device *dev) |
---|
158 | 134 | { |
---|
.. | .. |
---|
164 | 140 | ssize_t (*show)(struct cpuidle_device *, char *); |
---|
165 | 141 | ssize_t (*store)(struct cpuidle_device *, const char *, size_t count); |
---|
166 | 142 | }; |
---|
167 | | - |
---|
168 | | -#define define_one_ro(_name, show) \ |
---|
169 | | - static struct cpuidle_attr attr_##_name = __ATTR(_name, 0444, show, NULL) |
---|
170 | | -#define define_one_rw(_name, show, store) \ |
---|
171 | | - static struct cpuidle_attr attr_##_name = __ATTR(_name, 0644, show, store) |
---|
172 | 143 | |
---|
173 | 144 | #define attr_to_cpuidleattr(a) container_of(a, struct cpuidle_attr, attr) |
---|
174 | 145 | |
---|
.. | .. |
---|
255 | 226 | return sprintf(buf, "%u\n", state->_name);\ |
---|
256 | 227 | } |
---|
257 | 228 | |
---|
258 | | -#define define_store_state_ull_function(_name) \ |
---|
259 | | -static ssize_t store_state_##_name(struct cpuidle_state *state, \ |
---|
260 | | - struct cpuidle_state_usage *state_usage, \ |
---|
261 | | - const char *buf, size_t size) \ |
---|
262 | | -{ \ |
---|
263 | | - unsigned long long value; \ |
---|
264 | | - int err; \ |
---|
265 | | - if (!capable(CAP_SYS_ADMIN)) \ |
---|
266 | | - return -EPERM; \ |
---|
267 | | - err = kstrtoull(buf, 0, &value); \ |
---|
268 | | - if (err) \ |
---|
269 | | - return err; \ |
---|
270 | | - if (value) \ |
---|
271 | | - state_usage->_name = 1; \ |
---|
272 | | - else \ |
---|
273 | | - state_usage->_name = 0; \ |
---|
274 | | - return size; \ |
---|
275 | | -} |
---|
276 | | - |
---|
277 | 229 | #define define_show_state_ull_function(_name) \ |
---|
278 | 230 | static ssize_t show_state_##_name(struct cpuidle_state *state, \ |
---|
279 | 231 | struct cpuidle_state_usage *state_usage, \ |
---|
.. | .. |
---|
292 | 244 | return sprintf(buf, "%s\n", state->_name);\ |
---|
293 | 245 | } |
---|
294 | 246 | |
---|
295 | | -define_show_state_function(exit_latency) |
---|
296 | | -define_show_state_function(target_residency) |
---|
| 247 | +#define define_show_state_time_function(_name) \ |
---|
| 248 | +static ssize_t show_state_##_name(struct cpuidle_state *state, \ |
---|
| 249 | + struct cpuidle_state_usage *state_usage, \ |
---|
| 250 | + char *buf) \ |
---|
| 251 | +{ \ |
---|
| 252 | + return sprintf(buf, "%llu\n", ktime_to_us(state->_name##_ns)); \ |
---|
| 253 | +} |
---|
| 254 | + |
---|
| 255 | +define_show_state_time_function(exit_latency) |
---|
| 256 | +define_show_state_time_function(target_residency) |
---|
297 | 257 | define_show_state_function(power_usage) |
---|
298 | 258 | define_show_state_ull_function(usage) |
---|
299 | | -define_show_state_ull_function(time) |
---|
| 259 | +define_show_state_ull_function(rejected) |
---|
300 | 260 | define_show_state_str_function(name) |
---|
301 | 261 | define_show_state_str_function(desc) |
---|
302 | | -define_show_state_ull_function(disable) |
---|
303 | | -define_store_state_ull_function(disable) |
---|
| 262 | +define_show_state_ull_function(above) |
---|
| 263 | +define_show_state_ull_function(below) |
---|
| 264 | + |
---|
| 265 | +static ssize_t show_state_time(struct cpuidle_state *state, |
---|
| 266 | + struct cpuidle_state_usage *state_usage, |
---|
| 267 | + char *buf) |
---|
| 268 | +{ |
---|
| 269 | + return sprintf(buf, "%llu\n", ktime_to_us(state_usage->time_ns)); |
---|
| 270 | +} |
---|
| 271 | + |
---|
| 272 | +static ssize_t show_state_disable(struct cpuidle_state *state, |
---|
| 273 | + struct cpuidle_state_usage *state_usage, |
---|
| 274 | + char *buf) |
---|
| 275 | +{ |
---|
| 276 | + return sprintf(buf, "%llu\n", |
---|
| 277 | + state_usage->disable & CPUIDLE_STATE_DISABLED_BY_USER); |
---|
| 278 | +} |
---|
| 279 | + |
---|
| 280 | +static ssize_t store_state_disable(struct cpuidle_state *state, |
---|
| 281 | + struct cpuidle_state_usage *state_usage, |
---|
| 282 | + const char *buf, size_t size) |
---|
| 283 | +{ |
---|
| 284 | + unsigned int value; |
---|
| 285 | + int err; |
---|
| 286 | + |
---|
| 287 | + if (!capable(CAP_SYS_ADMIN)) |
---|
| 288 | + return -EPERM; |
---|
| 289 | + |
---|
| 290 | + err = kstrtouint(buf, 0, &value); |
---|
| 291 | + if (err) |
---|
| 292 | + return err; |
---|
| 293 | + |
---|
| 294 | + if (value) |
---|
| 295 | + state_usage->disable |= CPUIDLE_STATE_DISABLED_BY_USER; |
---|
| 296 | + else |
---|
| 297 | + state_usage->disable &= ~CPUIDLE_STATE_DISABLED_BY_USER; |
---|
| 298 | + |
---|
| 299 | + return size; |
---|
| 300 | +} |
---|
| 301 | + |
---|
| 302 | +static ssize_t show_state_default_status(struct cpuidle_state *state, |
---|
| 303 | + struct cpuidle_state_usage *state_usage, |
---|
| 304 | + char *buf) |
---|
| 305 | +{ |
---|
| 306 | + return sprintf(buf, "%s\n", |
---|
| 307 | + state->flags & CPUIDLE_FLAG_OFF ? "disabled" : "enabled"); |
---|
| 308 | +} |
---|
304 | 309 | |
---|
305 | 310 | define_one_state_ro(name, show_state_name); |
---|
306 | 311 | define_one_state_ro(desc, show_state_desc); |
---|
.. | .. |
---|
308 | 313 | define_one_state_ro(residency, show_state_target_residency); |
---|
309 | 314 | define_one_state_ro(power, show_state_power_usage); |
---|
310 | 315 | define_one_state_ro(usage, show_state_usage); |
---|
| 316 | +define_one_state_ro(rejected, show_state_rejected); |
---|
311 | 317 | define_one_state_ro(time, show_state_time); |
---|
312 | 318 | define_one_state_rw(disable, show_state_disable, store_state_disable); |
---|
| 319 | +define_one_state_ro(above, show_state_above); |
---|
| 320 | +define_one_state_ro(below, show_state_below); |
---|
| 321 | +define_one_state_ro(default_status, show_state_default_status); |
---|
313 | 322 | |
---|
314 | 323 | static struct attribute *cpuidle_state_default_attrs[] = { |
---|
315 | 324 | &attr_name.attr, |
---|
.. | .. |
---|
318 | 327 | &attr_residency.attr, |
---|
319 | 328 | &attr_power.attr, |
---|
320 | 329 | &attr_usage.attr, |
---|
| 330 | + &attr_rejected.attr, |
---|
321 | 331 | &attr_time.attr, |
---|
322 | 332 | &attr_disable.attr, |
---|
| 333 | + &attr_above.attr, |
---|
| 334 | + &attr_below.attr, |
---|
| 335 | + &attr_default_status.attr, |
---|
323 | 336 | NULL |
---|
324 | 337 | }; |
---|
325 | 338 | |
---|
.. | .. |
---|
328 | 341 | struct cpuidle_state_usage *state_usage; |
---|
329 | 342 | struct completion kobj_unregister; |
---|
330 | 343 | struct kobject kobj; |
---|
| 344 | + struct cpuidle_device *device; |
---|
331 | 345 | }; |
---|
332 | 346 | |
---|
333 | 347 | #ifdef CONFIG_SUSPEND |
---|
.. | .. |
---|
385 | 399 | #define kobj_to_state_obj(k) container_of(k, struct cpuidle_state_kobj, kobj) |
---|
386 | 400 | #define kobj_to_state(k) (kobj_to_state_obj(k)->state) |
---|
387 | 401 | #define kobj_to_state_usage(k) (kobj_to_state_obj(k)->state_usage) |
---|
| 402 | +#define kobj_to_device(k) (kobj_to_state_obj(k)->device) |
---|
388 | 403 | #define attr_to_stateattr(a) container_of(a, struct cpuidle_state_attr, attr) |
---|
389 | 404 | |
---|
390 | 405 | static ssize_t cpuidle_state_show(struct kobject *kobj, struct attribute *attr, |
---|
391 | | - char * buf) |
---|
| 406 | + char *buf) |
---|
392 | 407 | { |
---|
393 | 408 | int ret = -EIO; |
---|
394 | 409 | struct cpuidle_state *state = kobj_to_state(kobj); |
---|
395 | 410 | struct cpuidle_state_usage *state_usage = kobj_to_state_usage(kobj); |
---|
396 | | - struct cpuidle_state_attr * cattr = attr_to_stateattr(attr); |
---|
| 411 | + struct cpuidle_state_attr *cattr = attr_to_stateattr(attr); |
---|
397 | 412 | |
---|
398 | 413 | if (cattr->show) |
---|
399 | 414 | ret = cattr->show(state, state_usage, buf); |
---|
.. | .. |
---|
408 | 423 | struct cpuidle_state *state = kobj_to_state(kobj); |
---|
409 | 424 | struct cpuidle_state_usage *state_usage = kobj_to_state_usage(kobj); |
---|
410 | 425 | struct cpuidle_state_attr *cattr = attr_to_stateattr(attr); |
---|
| 426 | + struct cpuidle_device *dev = kobj_to_device(kobj); |
---|
411 | 427 | |
---|
412 | 428 | if (cattr->store) |
---|
413 | 429 | ret = cattr->store(state, state_usage, buf, size); |
---|
| 430 | + |
---|
| 431 | + /* reset poll time cache */ |
---|
| 432 | + dev->poll_limit_ns = 0; |
---|
414 | 433 | |
---|
415 | 434 | return ret; |
---|
416 | 435 | } |
---|
.. | .. |
---|
462 | 481 | } |
---|
463 | 482 | kobj->state = &drv->states[i]; |
---|
464 | 483 | kobj->state_usage = &device->states_usage[i]; |
---|
| 484 | + kobj->device = device; |
---|
465 | 485 | init_completion(&kobj->kobj_unregister); |
---|
466 | 486 | |
---|
467 | 487 | ret = kobject_init_and_add(&kobj->kobj, &ktype_state_cpuidle, |
---|
.. | .. |
---|
580 | 600 | |
---|
581 | 601 | /** |
---|
582 | 602 | * cpuidle_add_driver_sysfs - adds the driver name sysfs attribute |
---|
583 | | - * @device: the target device |
---|
| 603 | + * @dev: the target device |
---|
584 | 604 | */ |
---|
585 | 605 | static int cpuidle_add_driver_sysfs(struct cpuidle_device *dev) |
---|
586 | 606 | { |
---|
.. | .. |
---|
612 | 632 | |
---|
613 | 633 | /** |
---|
614 | 634 | * cpuidle_remove_driver_sysfs - removes the driver name sysfs attribute |
---|
615 | | - * @device: the target device |
---|
| 635 | + * @dev: the target device |
---|
616 | 636 | */ |
---|
617 | 637 | static void cpuidle_remove_driver_sysfs(struct cpuidle_device *dev) |
---|
618 | 638 | { |
---|