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