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