yuezonghe | 824eb0c | 2024-06-27 02:32:26 -0700 | [diff] [blame] | 1 | /* |
| 2 | * drivers/base/power/wakeup.c - System wakeup events framework |
| 3 | * |
| 4 | * Copyright (c) 2010 Rafael J. Wysocki <rjw@sisk.pl>, Novell Inc. |
| 5 | * |
| 6 | * This file is released under the GPLv2. |
| 7 | */ |
| 8 | |
| 9 | #include <linux/device.h> |
| 10 | #include <linux/slab.h> |
| 11 | #include <linux/sched.h> |
| 12 | #include <linux/capability.h> |
| 13 | #include <linux/export.h> |
| 14 | #include <linux/suspend.h> |
| 15 | #include <linux/seq_file.h> |
| 16 | #include <linux/debugfs.h> |
| 17 | #include <trace/events/power.h> |
| 18 | |
| 19 | #include "power.h" |
| 20 | |
| 21 | /* |
| 22 | * If set, the suspend/hibernate code will abort transitions to a sleep state |
| 23 | * if wakeup events are registered during or immediately before the transition. |
| 24 | */ |
| 25 | bool events_check_enabled __read_mostly; |
| 26 | |
| 27 | /* |
| 28 | * Combined counters of registered wakeup events and wakeup events in progress. |
| 29 | * They need to be modified together atomically, so it's better to use one |
| 30 | * atomic variable to hold them both. |
| 31 | */ |
| 32 | static atomic_t combined_event_count = ATOMIC_INIT(0); |
| 33 | |
| 34 | #define IN_PROGRESS_BITS (sizeof(int) * 4) |
| 35 | #define MAX_IN_PROGRESS ((1 << IN_PROGRESS_BITS) - 1) |
| 36 | |
| 37 | static void split_counters(unsigned int *cnt, unsigned int *inpr) |
| 38 | { |
| 39 | unsigned int comb = atomic_read(&combined_event_count); |
| 40 | |
| 41 | *cnt = (comb >> IN_PROGRESS_BITS); |
| 42 | *inpr = comb & MAX_IN_PROGRESS; |
| 43 | } |
| 44 | |
| 45 | /* A preserved old value of the events counter. */ |
| 46 | static unsigned int saved_count; |
| 47 | |
| 48 | static DEFINE_SPINLOCK(events_lock); |
| 49 | |
| 50 | static void pm_wakeup_timer_fn(unsigned long data); |
| 51 | |
| 52 | static LIST_HEAD(wakeup_sources); |
| 53 | |
| 54 | static DECLARE_WAIT_QUEUE_HEAD(wakeup_count_wait_queue); |
| 55 | |
| 56 | |
| 57 | volatile int wakelock_current_event_num = 0; |
| 58 | |
| 59 | |
| 60 | /******************************************************************* |
| 61 | *********** for trace lasted 1000 wakelock operations ********** |
| 62 | ******************************************************************* |
| 63 | */ |
| 64 | #define ZX_TRACE_WAKESRC |
| 65 | |
| 66 | #ifdef ZX_TRACE_WAKESRC |
| 67 | /*#pragma GCC optimize("O0")*/ |
| 68 | |
| 69 | extern unsigned int test_timer_read( void ); |
| 70 | |
| 71 | #define TRACE_WAKESRC_COUNT 50 |
| 72 | |
| 73 | typedef struct |
| 74 | { |
| 75 | struct wakeup_source * wakesrc_trace; |
| 76 | unsigned int wakesrc_time; |
| 77 | }wakesrc_trace_t; |
| 78 | |
| 79 | volatile wakesrc_trace_t wakesrc_lock_view[TRACE_WAKESRC_COUNT]; |
| 80 | volatile unsigned int wakesrc_lock_index = 0; |
| 81 | volatile wakesrc_trace_t wakesrc_unlock_view[TRACE_WAKESRC_COUNT]; |
| 82 | volatile unsigned int wakesrc_unlock_index = 0; |
| 83 | |
| 84 | void trace_lock_wakesrc(struct wakeup_source *ws) |
| 85 | { |
| 86 | unsigned long msr; |
| 87 | |
| 88 | if(!strncmp(ws->name, "nand", 4)) |
| 89 | return; |
| 90 | |
| 91 | |
| 92 | local_irq_save(msr); |
| 93 | |
| 94 | wakesrc_lock_view[wakesrc_lock_index].wakesrc_trace = ws; |
| 95 | wakesrc_lock_view[wakesrc_lock_index].wakesrc_time = test_timer_read(); |
| 96 | wakesrc_lock_index++; |
| 97 | if(wakesrc_lock_index==TRACE_WAKESRC_COUNT) |
| 98 | wakesrc_lock_index=0; |
| 99 | |
| 100 | local_irq_restore(msr); |
| 101 | |
| 102 | } |
| 103 | |
| 104 | void trace_unlock_wakesrc(struct wakeup_source *ws) |
| 105 | { |
| 106 | unsigned long msr; |
| 107 | |
| 108 | if(!strncmp(ws->name, "nand", 4)) |
| 109 | return; |
| 110 | |
| 111 | local_irq_save(msr); |
| 112 | |
| 113 | wakesrc_unlock_view[wakesrc_unlock_index].wakesrc_trace = ws; |
| 114 | wakesrc_unlock_view[wakesrc_unlock_index].wakesrc_time = test_timer_read(); |
| 115 | wakesrc_unlock_index++; |
| 116 | if(wakesrc_unlock_index==TRACE_WAKESRC_COUNT) |
| 117 | wakesrc_unlock_index=0; |
| 118 | local_irq_restore(msr); |
| 119 | |
| 120 | } |
| 121 | #else |
| 122 | void trace_lock_wakesrc(struct wakeup_source *ws){} |
| 123 | void trace_unlock_wakesrc(struct wakeup_source *ws){} |
| 124 | #endif |
| 125 | |
| 126 | #ifdef CONFIG_ZX_PM_SUSPEND |
| 127 | extern void zx_pm_suspend(void); |
| 128 | |
| 129 | #define PM_WS_RECORD_COUNT 50 |
| 130 | static unsigned int pm_ws_count = 0; |
| 131 | struct wakeup_source *pm_ws_record[PM_WS_RECORD_COUNT]; |
| 132 | |
| 133 | static char debug_wakeup_buf[200]; |
| 134 | void pm_debug_wakelocks(void) |
| 135 | { |
| 136 | char *str = debug_wakeup_buf; |
| 137 | struct wakeup_source *ws; |
| 138 | |
| 139 | rcu_read_lock(); |
| 140 | |
| 141 | *str = 0; |
| 142 | |
| 143 | list_for_each_entry_rcu(ws, &wakeup_sources, entry) |
| 144 | { |
| 145 | if (ws->active) |
| 146 | { |
| 147 | str += scnprintf(str, 200, "%s ", ws->name); |
| 148 | } |
| 149 | } |
| 150 | |
| 151 | printk("[WAKELOCK] %s \n", debug_wakeup_buf); |
| 152 | |
| 153 | rcu_read_unlock(); |
| 154 | } |
| 155 | |
| 156 | static void pm_record_ws(struct wakeup_source *ws) |
| 157 | { |
| 158 | unsigned long msr; |
| 159 | local_irq_save(msr); |
| 160 | |
| 161 | if(pm_ws_count == PM_WS_RECORD_COUNT){ |
| 162 | local_irq_restore(msr); |
| 163 | return; |
| 164 | } |
| 165 | |
| 166 | pm_ws_record[pm_ws_count] = ws; |
| 167 | pm_ws_count++; |
| 168 | local_irq_restore(msr); |
| 169 | } |
| 170 | #else |
| 171 | void pm_debug_wakelocks(void){} |
| 172 | static void pm_record_ws(struct wakeup_source *ws){} |
| 173 | static void zx_pm_suspend(void){} |
| 174 | #endif |
| 175 | |
| 176 | /** |
| 177 | * wakeup_source_prepare - Prepare a new wakeup source for initialization. |
| 178 | * @ws: Wakeup source to prepare. |
| 179 | * @name: Pointer to the name of the new wakeup source. |
| 180 | * |
| 181 | * Callers must ensure that the @name string won't be freed when @ws is still in |
| 182 | * use. |
| 183 | */ |
| 184 | void wakeup_source_prepare(struct wakeup_source *ws, const char *name) |
| 185 | { |
| 186 | if (ws) { |
| 187 | memset(ws, 0, sizeof(*ws)); |
| 188 | ws->name = name; |
| 189 | } |
| 190 | } |
| 191 | EXPORT_SYMBOL_GPL(wakeup_source_prepare); |
| 192 | |
| 193 | /** |
| 194 | * wakeup_source_create - Create a struct wakeup_source object. |
| 195 | * @name: Name of the new wakeup source. |
| 196 | */ |
| 197 | struct wakeup_source *wakeup_source_create(const char *name) |
| 198 | { |
| 199 | struct wakeup_source *ws; |
| 200 | |
| 201 | ws = kmalloc(sizeof(*ws), GFP_KERNEL); |
| 202 | if (!ws) |
| 203 | return NULL; |
| 204 | |
| 205 | wakeup_source_prepare(ws, name ? kstrdup(name, GFP_KERNEL) : NULL); |
| 206 | return ws; |
| 207 | } |
| 208 | EXPORT_SYMBOL_GPL(wakeup_source_create); |
| 209 | |
| 210 | /** |
| 211 | * wakeup_source_drop - Prepare a struct wakeup_source object for destruction. |
| 212 | * @ws: Wakeup source to prepare for destruction. |
| 213 | * |
| 214 | * Callers must ensure that __pm_stay_awake() or __pm_wakeup_event() will never |
| 215 | * be run in parallel with this function for the same wakeup source object. |
| 216 | */ |
| 217 | void wakeup_source_drop(struct wakeup_source *ws) |
| 218 | { |
| 219 | if (!ws) |
| 220 | return; |
| 221 | |
| 222 | del_timer_sync(&ws->timer); |
| 223 | __pm_relax(ws); |
| 224 | } |
| 225 | EXPORT_SYMBOL_GPL(wakeup_source_drop); |
| 226 | |
| 227 | /** |
| 228 | * wakeup_source_destroy - Destroy a struct wakeup_source object. |
| 229 | * @ws: Wakeup source to destroy. |
| 230 | * |
| 231 | * Use only for wakeup source objects created with wakeup_source_create(). |
| 232 | */ |
| 233 | void wakeup_source_destroy(struct wakeup_source *ws) |
| 234 | { |
| 235 | if (!ws) |
| 236 | return; |
| 237 | |
| 238 | wakeup_source_drop(ws); |
| 239 | kfree(ws->name); |
| 240 | kfree(ws); |
| 241 | } |
| 242 | EXPORT_SYMBOL_GPL(wakeup_source_destroy); |
| 243 | |
| 244 | /** |
| 245 | * wakeup_source_add - Add given object to the list of wakeup sources. |
| 246 | * @ws: Wakeup source object to add to the list. |
| 247 | */ |
| 248 | void wakeup_source_add(struct wakeup_source *ws) |
| 249 | { |
| 250 | unsigned long flags; |
| 251 | |
| 252 | if (WARN_ON(!ws)) |
| 253 | return; |
| 254 | |
| 255 | spin_lock_init(&ws->lock); |
| 256 | setup_timer(&ws->timer, pm_wakeup_timer_fn, (unsigned long)ws); |
| 257 | ws->active = false; |
| 258 | ws->last_time = ktime_get(); |
| 259 | |
| 260 | spin_lock_irqsave(&events_lock, flags); |
| 261 | list_add_rcu(&ws->entry, &wakeup_sources); |
| 262 | pm_record_ws(ws); |
| 263 | spin_unlock_irqrestore(&events_lock, flags); |
| 264 | } |
| 265 | EXPORT_SYMBOL_GPL(wakeup_source_add); |
| 266 | |
| 267 | /** |
| 268 | * wakeup_source_remove - Remove given object from the wakeup sources list. |
| 269 | * @ws: Wakeup source object to remove from the list. |
| 270 | */ |
| 271 | void wakeup_source_remove(struct wakeup_source *ws) |
| 272 | { |
| 273 | unsigned long flags; |
| 274 | |
| 275 | if (WARN_ON(!ws)) |
| 276 | return; |
| 277 | |
| 278 | spin_lock_irqsave(&events_lock, flags); |
| 279 | list_del_rcu(&ws->entry); |
| 280 | spin_unlock_irqrestore(&events_lock, flags); |
| 281 | synchronize_rcu(); |
| 282 | } |
| 283 | EXPORT_SYMBOL_GPL(wakeup_source_remove); |
| 284 | |
| 285 | /** |
| 286 | * wakeup_source_register - Create wakeup source and add it to the list. |
| 287 | * @name: Name of the wakeup source to register. |
| 288 | */ |
| 289 | struct wakeup_source *wakeup_source_register(const char *name) |
| 290 | { |
| 291 | struct wakeup_source *ws; |
| 292 | |
| 293 | ws = wakeup_source_create(name); |
| 294 | if (ws) |
| 295 | wakeup_source_add(ws); |
| 296 | |
| 297 | return ws; |
| 298 | } |
| 299 | EXPORT_SYMBOL_GPL(wakeup_source_register); |
| 300 | |
| 301 | /** |
| 302 | * wakeup_source_unregister - Remove wakeup source from the list and remove it. |
| 303 | * @ws: Wakeup source object to unregister. |
| 304 | */ |
| 305 | void wakeup_source_unregister(struct wakeup_source *ws) |
| 306 | { |
| 307 | if (ws) { |
| 308 | wakeup_source_remove(ws); |
| 309 | wakeup_source_destroy(ws); |
| 310 | } |
| 311 | } |
| 312 | EXPORT_SYMBOL_GPL(wakeup_source_unregister); |
| 313 | |
| 314 | /** |
| 315 | * device_wakeup_attach - Attach a wakeup source object to a device object. |
| 316 | * @dev: Device to handle. |
| 317 | * @ws: Wakeup source object to attach to @dev. |
| 318 | * |
| 319 | * This causes @dev to be treated as a wakeup device. |
| 320 | */ |
| 321 | static int device_wakeup_attach(struct device *dev, struct wakeup_source *ws) |
| 322 | { |
| 323 | spin_lock_irq(&dev->power.lock); |
| 324 | if (dev->power.wakeup) { |
| 325 | spin_unlock_irq(&dev->power.lock); |
| 326 | return -EEXIST; |
| 327 | } |
| 328 | dev->power.wakeup = ws; |
| 329 | spin_unlock_irq(&dev->power.lock); |
| 330 | return 0; |
| 331 | } |
| 332 | |
| 333 | /** |
| 334 | * device_wakeup_enable - Enable given device to be a wakeup source. |
| 335 | * @dev: Device to handle. |
| 336 | * |
| 337 | * Create a wakeup source object, register it and attach it to @dev. |
| 338 | */ |
| 339 | int device_wakeup_enable(struct device *dev) |
| 340 | { |
| 341 | struct wakeup_source *ws; |
| 342 | int ret; |
| 343 | |
| 344 | if (!dev || !dev->power.can_wakeup) |
| 345 | return -EINVAL; |
| 346 | |
| 347 | ws = wakeup_source_register(dev_name(dev)); |
| 348 | if (!ws) |
| 349 | return -ENOMEM; |
| 350 | |
| 351 | ret = device_wakeup_attach(dev, ws); |
| 352 | if (ret) |
| 353 | wakeup_source_unregister(ws); |
| 354 | |
| 355 | return ret; |
| 356 | } |
| 357 | EXPORT_SYMBOL_GPL(device_wakeup_enable); |
| 358 | |
| 359 | /** |
| 360 | * device_wakeup_detach - Detach a device's wakeup source object from it. |
| 361 | * @dev: Device to detach the wakeup source object from. |
| 362 | * |
| 363 | * After it returns, @dev will not be treated as a wakeup device any more. |
| 364 | */ |
| 365 | static struct wakeup_source *device_wakeup_detach(struct device *dev) |
| 366 | { |
| 367 | struct wakeup_source *ws; |
| 368 | |
| 369 | spin_lock_irq(&dev->power.lock); |
| 370 | ws = dev->power.wakeup; |
| 371 | dev->power.wakeup = NULL; |
| 372 | spin_unlock_irq(&dev->power.lock); |
| 373 | return ws; |
| 374 | } |
| 375 | |
| 376 | /** |
| 377 | * device_wakeup_disable - Do not regard a device as a wakeup source any more. |
| 378 | * @dev: Device to handle. |
| 379 | * |
| 380 | * Detach the @dev's wakeup source object from it, unregister this wakeup source |
| 381 | * object and destroy it. |
| 382 | */ |
| 383 | int device_wakeup_disable(struct device *dev) |
| 384 | { |
| 385 | struct wakeup_source *ws; |
| 386 | |
| 387 | if (!dev || !dev->power.can_wakeup) |
| 388 | return -EINVAL; |
| 389 | |
| 390 | ws = device_wakeup_detach(dev); |
| 391 | if (ws) |
| 392 | wakeup_source_unregister(ws); |
| 393 | |
| 394 | return 0; |
| 395 | } |
| 396 | EXPORT_SYMBOL_GPL(device_wakeup_disable); |
| 397 | |
| 398 | /** |
| 399 | * device_set_wakeup_capable - Set/reset device wakeup capability flag. |
| 400 | * @dev: Device to handle. |
| 401 | * @capable: Whether or not @dev is capable of waking up the system from sleep. |
| 402 | * |
| 403 | * If @capable is set, set the @dev's power.can_wakeup flag and add its |
| 404 | * wakeup-related attributes to sysfs. Otherwise, unset the @dev's |
| 405 | * power.can_wakeup flag and remove its wakeup-related attributes from sysfs. |
| 406 | * |
| 407 | * This function may sleep and it can't be called from any context where |
| 408 | * sleeping is not allowed. |
| 409 | */ |
| 410 | void device_set_wakeup_capable(struct device *dev, bool capable) |
| 411 | { |
| 412 | if (!!dev->power.can_wakeup == !!capable) |
| 413 | return; |
| 414 | |
| 415 | if (device_is_registered(dev) && !list_empty(&dev->power.entry)) { |
| 416 | if (capable) { |
| 417 | if (wakeup_sysfs_add(dev)) |
| 418 | return; |
| 419 | } else { |
| 420 | wakeup_sysfs_remove(dev); |
| 421 | } |
| 422 | } |
| 423 | dev->power.can_wakeup = capable; |
| 424 | } |
| 425 | EXPORT_SYMBOL_GPL(device_set_wakeup_capable); |
| 426 | |
| 427 | /** |
| 428 | * device_init_wakeup - Device wakeup initialization. |
| 429 | * @dev: Device to handle. |
| 430 | * @enable: Whether or not to enable @dev as a wakeup device. |
| 431 | * |
| 432 | * By default, most devices should leave wakeup disabled. The exceptions are |
| 433 | * devices that everyone expects to be wakeup sources: keyboards, power buttons, |
| 434 | * possibly network interfaces, etc. Also, devices that don't generate their |
| 435 | * own wakeup requests but merely forward requests from one bus to another |
| 436 | * (like PCI bridges) should have wakeup enabled by default. |
| 437 | */ |
| 438 | int device_init_wakeup(struct device *dev, bool enable) |
| 439 | { |
| 440 | int ret = 0; |
| 441 | |
| 442 | if (enable) { |
| 443 | device_set_wakeup_capable(dev, true); |
| 444 | ret = device_wakeup_enable(dev); |
| 445 | } else { |
| 446 | device_set_wakeup_capable(dev, false); |
| 447 | } |
| 448 | |
| 449 | return ret; |
| 450 | } |
| 451 | EXPORT_SYMBOL_GPL(device_init_wakeup); |
| 452 | |
| 453 | /** |
| 454 | * device_set_wakeup_enable - Enable or disable a device to wake up the system. |
| 455 | * @dev: Device to handle. |
| 456 | */ |
| 457 | int device_set_wakeup_enable(struct device *dev, bool enable) |
| 458 | { |
| 459 | if (!dev || !dev->power.can_wakeup) |
| 460 | return -EINVAL; |
| 461 | |
| 462 | return enable ? device_wakeup_enable(dev) : device_wakeup_disable(dev); |
| 463 | } |
| 464 | EXPORT_SYMBOL_GPL(device_set_wakeup_enable); |
| 465 | |
| 466 | /* |
| 467 | * The functions below use the observation that each wakeup event starts a |
| 468 | * period in which the system should not be suspended. The moment this period |
| 469 | * will end depends on how the wakeup event is going to be processed after being |
| 470 | * detected and all of the possible cases can be divided into two distinct |
| 471 | * groups. |
| 472 | * |
| 473 | * First, a wakeup event may be detected by the same functional unit that will |
| 474 | * carry out the entire processing of it and possibly will pass it to user space |
| 475 | * for further processing. In that case the functional unit that has detected |
| 476 | * the event may later "close" the "no suspend" period associated with it |
| 477 | * directly as soon as it has been dealt with. The pair of pm_stay_awake() and |
| 478 | * pm_relax(), balanced with each other, is supposed to be used in such |
| 479 | * situations. |
| 480 | * |
| 481 | * Second, a wakeup event may be detected by one functional unit and processed |
| 482 | * by another one. In that case the unit that has detected it cannot really |
| 483 | * "close" the "no suspend" period associated with it, unless it knows in |
| 484 | * advance what's going to happen to the event during processing. This |
| 485 | * knowledge, however, may not be available to it, so it can simply specify time |
| 486 | * to wait before the system can be suspended and pass it as the second |
| 487 | * argument of pm_wakeup_event(). |
| 488 | * |
| 489 | * It is valid to call pm_relax() after pm_wakeup_event(), in which case the |
| 490 | * "no suspend" period will be ended either by the pm_relax(), or by the timer |
| 491 | * function executed when the timer expires, whichever comes first. |
| 492 | */ |
| 493 | |
| 494 | /** |
| 495 | * wakup_source_activate - Mark given wakeup source as active. |
| 496 | * @ws: Wakeup source to handle. |
| 497 | * |
| 498 | * Update the @ws' statistics and, if @ws has just been activated, notify the PM |
| 499 | * core of the event by incrementing the counter of of wakeup events being |
| 500 | * processed. |
| 501 | */ |
| 502 | static void wakeup_source_activate(struct wakeup_source *ws) |
| 503 | { |
| 504 | unsigned int cec; |
| 505 | |
| 506 | ws->active = true; |
| 507 | ws->active_count++; |
| 508 | ws->last_time = ktime_get(); |
| 509 | if (ws->autosleep_enabled) |
| 510 | ws->start_prevent_time = ws->last_time; |
| 511 | |
| 512 | /* Increment the counter of events in progress. */ |
| 513 | cec = atomic_inc_return(&combined_event_count); |
| 514 | |
| 515 | trace_wakeup_source_activate(ws->name, cec); |
| 516 | |
| 517 | trace_lock_wakesrc(ws); |
| 518 | } |
| 519 | |
| 520 | /** |
| 521 | * wakeup_source_report_event - Report wakeup event using the given source. |
| 522 | * @ws: Wakeup source to report the event for. |
| 523 | */ |
| 524 | static void wakeup_source_report_event(struct wakeup_source *ws) |
| 525 | { |
| 526 | ws->event_count++; |
| 527 | /* This is racy, but the counter is approximate anyway. */ |
| 528 | if (events_check_enabled) |
| 529 | ws->wakeup_count++; |
| 530 | |
| 531 | if (!ws->active) |
| 532 | { |
| 533 | wakelock_current_event_num++; |
| 534 | wakeup_source_activate(ws); |
| 535 | } |
| 536 | } |
| 537 | |
| 538 | /** |
| 539 | * __pm_stay_awake - Notify the PM core of a wakeup event. |
| 540 | * @ws: Wakeup source object associated with the source of the event. |
| 541 | * |
| 542 | * It is safe to call this function from interrupt context. |
| 543 | */ |
| 544 | void __pm_stay_awake(struct wakeup_source *ws) |
| 545 | { |
| 546 | unsigned long flags; |
| 547 | |
| 548 | if (!ws) |
| 549 | return; |
| 550 | |
| 551 | spin_lock_irqsave(&ws->lock, flags); |
| 552 | |
| 553 | wakeup_source_report_event(ws); |
| 554 | del_timer(&ws->timer); |
| 555 | ws->timer_expires = 0; |
| 556 | |
| 557 | spin_unlock_irqrestore(&ws->lock, flags); |
| 558 | } |
| 559 | EXPORT_SYMBOL_GPL(__pm_stay_awake); |
| 560 | |
| 561 | /** |
| 562 | * pm_stay_awake - Notify the PM core that a wakeup event is being processed. |
| 563 | * @dev: Device the wakeup event is related to. |
| 564 | * |
| 565 | * Notify the PM core of a wakeup event (signaled by @dev) by calling |
| 566 | * __pm_stay_awake for the @dev's wakeup source object. |
| 567 | * |
| 568 | * Call this function after detecting of a wakeup event if pm_relax() is going |
| 569 | * to be called directly after processing the event (and possibly passing it to |
| 570 | * user space for further processing). |
| 571 | */ |
| 572 | void pm_stay_awake(struct device *dev) |
| 573 | { |
| 574 | unsigned long flags; |
| 575 | |
| 576 | if (!dev) |
| 577 | return; |
| 578 | |
| 579 | spin_lock_irqsave(&dev->power.lock, flags); |
| 580 | __pm_stay_awake(dev->power.wakeup); |
| 581 | spin_unlock_irqrestore(&dev->power.lock, flags); |
| 582 | } |
| 583 | EXPORT_SYMBOL_GPL(pm_stay_awake); |
| 584 | |
| 585 | #ifdef CONFIG_PM_AUTOSLEEP |
| 586 | static void update_prevent_sleep_time(struct wakeup_source *ws, ktime_t now) |
| 587 | { |
| 588 | ktime_t delta = ktime_sub(now, ws->start_prevent_time); |
| 589 | ws->prevent_sleep_time = ktime_add(ws->prevent_sleep_time, delta); |
| 590 | } |
| 591 | #else |
| 592 | static inline void update_prevent_sleep_time(struct wakeup_source *ws, |
| 593 | ktime_t now) {} |
| 594 | #endif |
| 595 | |
| 596 | /** |
| 597 | * wakup_source_deactivate - Mark given wakeup source as inactive. |
| 598 | * @ws: Wakeup source to handle. |
| 599 | * |
| 600 | * Update the @ws' statistics and notify the PM core that the wakeup source has |
| 601 | * become inactive by decrementing the counter of wakeup events being processed |
| 602 | * and incrementing the counter of registered wakeup events. |
| 603 | */ |
| 604 | static void wakeup_source_deactivate(struct wakeup_source *ws) |
| 605 | { |
| 606 | unsigned int cnt, inpr, cec; |
| 607 | ktime_t duration; |
| 608 | ktime_t now; |
| 609 | |
| 610 | ws->relax_count++; |
| 611 | /* |
| 612 | * __pm_relax() may be called directly or from a timer function. |
| 613 | * If it is called directly right after the timer function has been |
| 614 | * started, but before the timer function calls __pm_relax(), it is |
| 615 | * possible that __pm_stay_awake() will be called in the meantime and |
| 616 | * will set ws->active. Then, ws->active may be cleared immediately |
| 617 | * by the __pm_relax() called from the timer function, but in such a |
| 618 | * case ws->relax_count will be different from ws->active_count. |
| 619 | */ |
| 620 | if (ws->relax_count != ws->active_count) { |
| 621 | ws->relax_count--; |
| 622 | |
| 623 | return; |
| 624 | } |
| 625 | |
| 626 | ws->active = false; |
| 627 | |
| 628 | now = ktime_get(); |
| 629 | duration = ktime_sub(now, ws->last_time); |
| 630 | ws->total_time = ktime_add(ws->total_time, duration); |
| 631 | if (ktime_to_ns(duration) > ktime_to_ns(ws->max_time)) |
| 632 | ws->max_time = duration; |
| 633 | |
| 634 | ws->last_time = now; |
| 635 | del_timer(&ws->timer); |
| 636 | ws->timer_expires = 0; |
| 637 | |
| 638 | if (ws->autosleep_enabled) |
| 639 | update_prevent_sleep_time(ws, now); |
| 640 | |
| 641 | /* |
| 642 | * Increment the counter of registered wakeup events and decrement the |
| 643 | * couter of wakeup events in progress simultaneously. |
| 644 | */ |
| 645 | cec = atomic_add_return(MAX_IN_PROGRESS, &combined_event_count); |
| 646 | trace_wakeup_source_deactivate(ws->name, cec); |
| 647 | |
| 648 | split_counters(&cnt, &inpr); |
| 649 | if (!inpr && waitqueue_active(&wakeup_count_wait_queue)) |
| 650 | wake_up(&wakeup_count_wait_queue); |
| 651 | |
| 652 | zx_pm_suspend(); |
| 653 | trace_unlock_wakesrc(ws); |
| 654 | } |
| 655 | |
| 656 | /** |
| 657 | * __pm_relax - Notify the PM core that processing of a wakeup event has ended. |
| 658 | * @ws: Wakeup source object associated with the source of the event. |
| 659 | * |
| 660 | * Call this function for wakeup events whose processing started with calling |
| 661 | * __pm_stay_awake(). |
| 662 | * |
| 663 | * It is safe to call it from interrupt context. |
| 664 | */ |
| 665 | void __pm_relax(struct wakeup_source *ws) |
| 666 | { |
| 667 | unsigned long flags; |
| 668 | |
| 669 | if (!ws) |
| 670 | return; |
| 671 | |
| 672 | spin_lock_irqsave(&ws->lock, flags); |
| 673 | if (ws->active) |
| 674 | wakeup_source_deactivate(ws); |
| 675 | spin_unlock_irqrestore(&ws->lock, flags); |
| 676 | } |
| 677 | EXPORT_SYMBOL_GPL(__pm_relax); |
| 678 | |
| 679 | /** |
| 680 | * pm_relax - Notify the PM core that processing of a wakeup event has ended. |
| 681 | * @dev: Device that signaled the event. |
| 682 | * |
| 683 | * Execute __pm_relax() for the @dev's wakeup source object. |
| 684 | */ |
| 685 | void pm_relax(struct device *dev) |
| 686 | { |
| 687 | unsigned long flags; |
| 688 | |
| 689 | if (!dev) |
| 690 | return; |
| 691 | |
| 692 | spin_lock_irqsave(&dev->power.lock, flags); |
| 693 | __pm_relax(dev->power.wakeup); |
| 694 | spin_unlock_irqrestore(&dev->power.lock, flags); |
| 695 | } |
| 696 | EXPORT_SYMBOL_GPL(pm_relax); |
| 697 | |
| 698 | /** |
| 699 | * pm_wakeup_timer_fn - Delayed finalization of a wakeup event. |
| 700 | * @data: Address of the wakeup source object associated with the event source. |
| 701 | * |
| 702 | * Call wakeup_source_deactivate() for the wakeup source whose address is stored |
| 703 | * in @data if it is currently active and its timer has not been canceled and |
| 704 | * the expiration time of the timer is not in future. |
| 705 | */ |
| 706 | static void pm_wakeup_timer_fn(unsigned long data) |
| 707 | { |
| 708 | struct wakeup_source *ws = (struct wakeup_source *)data; |
| 709 | unsigned long flags; |
| 710 | |
| 711 | spin_lock_irqsave(&ws->lock, flags); |
| 712 | |
| 713 | if (ws->active && ws->timer_expires |
| 714 | && time_after_eq(jiffies, ws->timer_expires)) { |
| 715 | wakeup_source_deactivate(ws); |
| 716 | ws->expire_count++; |
| 717 | } |
| 718 | |
| 719 | spin_unlock_irqrestore(&ws->lock, flags); |
| 720 | } |
| 721 | |
| 722 | /** |
| 723 | * __pm_wakeup_event - Notify the PM core of a wakeup event. |
| 724 | * @ws: Wakeup source object associated with the event source. |
| 725 | * @msec: Anticipated event processing time (in milliseconds). |
| 726 | * |
| 727 | * Notify the PM core of a wakeup event whose source is @ws that will take |
| 728 | * approximately @msec milliseconds to be processed by the kernel. If @ws is |
| 729 | * not active, activate it. If @msec is nonzero, set up the @ws' timer to |
| 730 | * execute pm_wakeup_timer_fn() in future. |
| 731 | * |
| 732 | * It is safe to call this function from interrupt context. |
| 733 | */ |
| 734 | void __pm_wakeup_event(struct wakeup_source *ws, unsigned int msec) |
| 735 | { |
| 736 | unsigned long flags; |
| 737 | unsigned long expires; |
| 738 | |
| 739 | if (!ws) |
| 740 | return; |
| 741 | |
| 742 | spin_lock_irqsave(&ws->lock, flags); |
| 743 | |
| 744 | wakeup_source_report_event(ws); |
| 745 | |
| 746 | if (!msec) { |
| 747 | wakeup_source_deactivate(ws); |
| 748 | goto unlock; |
| 749 | } |
| 750 | |
| 751 | expires = jiffies + msecs_to_jiffies(msec); |
| 752 | if (!expires) |
| 753 | expires = 1; |
| 754 | |
| 755 | if (!ws->timer_expires || time_after(expires, ws->timer_expires)) { |
| 756 | mod_timer(&ws->timer, expires); |
| 757 | ws->timer_expires = expires; |
| 758 | } |
| 759 | |
| 760 | unlock: |
| 761 | spin_unlock_irqrestore(&ws->lock, flags); |
| 762 | } |
| 763 | EXPORT_SYMBOL_GPL(__pm_wakeup_event); |
| 764 | |
| 765 | |
| 766 | /** |
| 767 | * pm_wakeup_event - Notify the PM core of a wakeup event. |
| 768 | * @dev: Device the wakeup event is related to. |
| 769 | * @msec: Anticipated event processing time (in milliseconds). |
| 770 | * |
| 771 | * Call __pm_wakeup_event() for the @dev's wakeup source object. |
| 772 | */ |
| 773 | void pm_wakeup_event(struct device *dev, unsigned int msec) |
| 774 | { |
| 775 | unsigned long flags; |
| 776 | |
| 777 | if (!dev) |
| 778 | return; |
| 779 | |
| 780 | spin_lock_irqsave(&dev->power.lock, flags); |
| 781 | __pm_wakeup_event(dev->power.wakeup, msec); |
| 782 | spin_unlock_irqrestore(&dev->power.lock, flags); |
| 783 | } |
| 784 | EXPORT_SYMBOL_GPL(pm_wakeup_event); |
| 785 | |
| 786 | static void print_active_wakeup_sources(void) |
| 787 | { |
| 788 | struct wakeup_source *ws; |
| 789 | int active = 0; |
| 790 | struct wakeup_source *last_activity_ws = NULL; |
| 791 | |
| 792 | rcu_read_lock(); |
| 793 | list_for_each_entry_rcu(ws, &wakeup_sources, entry) { |
| 794 | if (ws->active) { |
| 795 | pr_info("active wakeup source: %s\n", ws->name); |
| 796 | active = 1; |
| 797 | } else if (!active && |
| 798 | (!last_activity_ws || |
| 799 | ktime_to_ns(ws->last_time) > |
| 800 | ktime_to_ns(last_activity_ws->last_time))) { |
| 801 | last_activity_ws = ws; |
| 802 | } |
| 803 | } |
| 804 | |
| 805 | if (!active && last_activity_ws) |
| 806 | pr_info("last active wakeup source: %s\n", |
| 807 | last_activity_ws->name); |
| 808 | rcu_read_unlock(); |
| 809 | } |
| 810 | |
| 811 | /** |
| 812 | * pm_wakeup_pending - Check if power transition in progress should be aborted. |
| 813 | * |
| 814 | * Compare the current number of registered wakeup events with its preserved |
| 815 | * value from the past and return true if new wakeup events have been registered |
| 816 | * since the old value was stored. Also return true if the current number of |
| 817 | * wakeup events being processed is different from zero. |
| 818 | */ |
| 819 | bool pm_wakeup_pending(void) |
| 820 | { |
| 821 | unsigned long flags; |
| 822 | bool ret = false; |
| 823 | |
| 824 | spin_lock_irqsave(&events_lock, flags); |
| 825 | if (events_check_enabled) { |
| 826 | unsigned int cnt, inpr; |
| 827 | |
| 828 | split_counters(&cnt, &inpr); |
| 829 | ret = (cnt != saved_count || inpr > 0); |
| 830 | events_check_enabled = !ret; |
| 831 | } |
| 832 | spin_unlock_irqrestore(&events_lock, flags); |
| 833 | |
| 834 | if (ret) |
| 835 | print_active_wakeup_sources(); |
| 836 | |
| 837 | return ret; |
| 838 | } |
| 839 | |
| 840 | /** |
| 841 | * pm_get_wakeup_count - Read the number of registered wakeup events. |
| 842 | * @count: Address to store the value at. |
| 843 | * @block: Whether or not to block. |
| 844 | * |
| 845 | * Store the number of registered wakeup events at the address in @count. If |
| 846 | * @block is set, block until the current number of wakeup events being |
| 847 | * processed is zero. |
| 848 | * |
| 849 | * Return 'false' if the current number of wakeup events being processed is |
| 850 | * nonzero. Otherwise return 'true'. |
| 851 | */ |
| 852 | bool pm_get_wakeup_count(unsigned int *count, bool block) |
| 853 | { |
| 854 | unsigned int cnt, inpr; |
| 855 | |
| 856 | if (block) { |
| 857 | DEFINE_WAIT(wait); |
| 858 | |
| 859 | for (;;) { |
| 860 | prepare_to_wait(&wakeup_count_wait_queue, &wait, |
| 861 | TASK_INTERRUPTIBLE); |
| 862 | split_counters(&cnt, &inpr); |
| 863 | if (inpr == 0 || signal_pending(current)) |
| 864 | break; |
| 865 | |
| 866 | schedule(); |
| 867 | } |
| 868 | finish_wait(&wakeup_count_wait_queue, &wait); |
| 869 | } |
| 870 | |
| 871 | split_counters(&cnt, &inpr); |
| 872 | *count = cnt; |
| 873 | return !inpr; |
| 874 | } |
| 875 | |
| 876 | /** |
| 877 | * pm_save_wakeup_count - Save the current number of registered wakeup events. |
| 878 | * @count: Value to compare with the current number of registered wakeup events. |
| 879 | * |
| 880 | * If @count is equal to the current number of registered wakeup events and the |
| 881 | * current number of wakeup events being processed is zero, store @count as the |
| 882 | * old number of registered wakeup events for pm_check_wakeup_events(), enable |
| 883 | * wakeup events detection and return 'true'. Otherwise disable wakeup events |
| 884 | * detection and return 'false'. |
| 885 | */ |
| 886 | bool pm_save_wakeup_count(unsigned int count) |
| 887 | { |
| 888 | unsigned int cnt, inpr; |
| 889 | unsigned long flags; |
| 890 | |
| 891 | events_check_enabled = false; |
| 892 | spin_lock_irqsave(&events_lock, flags); |
| 893 | split_counters(&cnt, &inpr); |
| 894 | if (cnt == count && inpr == 0) { |
| 895 | saved_count = count; |
| 896 | events_check_enabled = true; |
| 897 | } |
| 898 | spin_unlock_irqrestore(&events_lock, flags); |
| 899 | return events_check_enabled; |
| 900 | } |
| 901 | |
| 902 | #ifdef CONFIG_PM_AUTOSLEEP |
| 903 | /** |
| 904 | * pm_wakep_autosleep_enabled - Modify autosleep_enabled for all wakeup sources. |
| 905 | * @enabled: Whether to set or to clear the autosleep_enabled flags. |
| 906 | */ |
| 907 | void pm_wakep_autosleep_enabled(bool set) |
| 908 | { |
| 909 | struct wakeup_source *ws; |
| 910 | ktime_t now = ktime_get(); |
| 911 | |
| 912 | rcu_read_lock(); |
| 913 | list_for_each_entry_rcu(ws, &wakeup_sources, entry) { |
| 914 | spin_lock_irq(&ws->lock); |
| 915 | if (ws->autosleep_enabled != set) { |
| 916 | ws->autosleep_enabled = set; |
| 917 | if (ws->active) { |
| 918 | if (set) |
| 919 | ws->start_prevent_time = now; |
| 920 | else |
| 921 | update_prevent_sleep_time(ws, now); |
| 922 | } |
| 923 | } |
| 924 | spin_unlock_irq(&ws->lock); |
| 925 | } |
| 926 | rcu_read_unlock(); |
| 927 | } |
| 928 | #endif /* CONFIG_PM_AUTOSLEEP */ |
| 929 | |
| 930 | static struct dentry *wakeup_sources_stats_dentry; |
| 931 | |
| 932 | /** |
| 933 | * print_wakeup_source_stats - Print wakeup source statistics information. |
| 934 | * @m: seq_file to print the statistics into. |
| 935 | * @ws: Wakeup source object to print the statistics for. |
| 936 | */ |
| 937 | static int print_wakeup_source_stats(struct seq_file *m, |
| 938 | struct wakeup_source *ws) |
| 939 | { |
| 940 | unsigned long flags; |
| 941 | ktime_t total_time; |
| 942 | ktime_t max_time; |
| 943 | unsigned long active_count; |
| 944 | ktime_t active_time; |
| 945 | ktime_t prevent_sleep_time; |
| 946 | int ret; |
| 947 | |
| 948 | spin_lock_irqsave(&ws->lock, flags); |
| 949 | |
| 950 | total_time = ws->total_time; |
| 951 | max_time = ws->max_time; |
| 952 | prevent_sleep_time = ws->prevent_sleep_time; |
| 953 | active_count = ws->active_count; |
| 954 | if (ws->active) { |
| 955 | ktime_t now = ktime_get(); |
| 956 | |
| 957 | active_time = ktime_sub(now, ws->last_time); |
| 958 | total_time = ktime_add(total_time, active_time); |
| 959 | if (active_time.tv64 > max_time.tv64) |
| 960 | max_time = active_time; |
| 961 | |
| 962 | if (ws->autosleep_enabled) |
| 963 | prevent_sleep_time = ktime_add(prevent_sleep_time, |
| 964 | ktime_sub(now, ws->start_prevent_time)); |
| 965 | } else { |
| 966 | active_time = ktime_set(0, 0); |
| 967 | } |
| 968 | |
| 969 | ret = seq_printf(m, "%-12s\t%lu\t\t%lu\t\t%lu\t\t%lu\t\t" |
| 970 | "%lld\t\t%lld\t\t%lld\t\t%lld\t\t%lld\n", |
| 971 | ws->name, active_count, ws->event_count, |
| 972 | ws->wakeup_count, ws->expire_count, |
| 973 | ktime_to_ms(active_time), ktime_to_ms(total_time), |
| 974 | ktime_to_ms(max_time), ktime_to_ms(ws->last_time), |
| 975 | ktime_to_ms(prevent_sleep_time)); |
| 976 | |
| 977 | spin_unlock_irqrestore(&ws->lock, flags); |
| 978 | |
| 979 | return ret; |
| 980 | } |
| 981 | |
| 982 | /** |
| 983 | * wakeup_sources_stats_show - Print wakeup sources statistics information. |
| 984 | * @m: seq_file to print the statistics into. |
| 985 | */ |
| 986 | static int wakeup_sources_stats_show(struct seq_file *m, void *unused) |
| 987 | { |
| 988 | struct wakeup_source *ws; |
| 989 | |
| 990 | seq_puts(m, "name\t\tactive_count\tevent_count\twakeup_count\t" |
| 991 | "expire_count\tactive_since\ttotal_time\tmax_time\t" |
| 992 | "last_change\tprevent_suspend_time\n"); |
| 993 | |
| 994 | rcu_read_lock(); |
| 995 | list_for_each_entry_rcu(ws, &wakeup_sources, entry) |
| 996 | print_wakeup_source_stats(m, ws); |
| 997 | rcu_read_unlock(); |
| 998 | |
| 999 | return 0; |
| 1000 | } |
| 1001 | |
| 1002 | static int wakeup_sources_stats_open(struct inode *inode, struct file *file) |
| 1003 | { |
| 1004 | return single_open(file, wakeup_sources_stats_show, NULL); |
| 1005 | } |
| 1006 | |
| 1007 | static const struct file_operations wakeup_sources_stats_fops = { |
| 1008 | .owner = THIS_MODULE, |
| 1009 | .open = wakeup_sources_stats_open, |
| 1010 | .read = seq_read, |
| 1011 | .llseek = seq_lseek, |
| 1012 | .release = single_release, |
| 1013 | }; |
| 1014 | |
| 1015 | static int __init wakeup_sources_debugfs_init(void) |
| 1016 | { |
| 1017 | wakeup_sources_stats_dentry = debugfs_create_file("wakeup_sources", |
| 1018 | S_IRUGO, NULL, NULL, &wakeup_sources_stats_fops); |
| 1019 | return 0; |
| 1020 | } |
| 1021 | |
| 1022 | postcore_initcall(wakeup_sources_debugfs_init); |