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