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