wakeup.c 28.6 KB
Newer Older
1 2 3 4 5 6 7 8 9 10
/*
 * drivers/base/power/wakeup.c - System wakeup events framework
 *
 * Copyright (c) 2010 Rafael J. Wysocki <rjw@sisk.pl>, Novell Inc.
 *
 * This file is released under the GPLv2.
 */

#include <linux/device.h>
#include <linux/slab.h>
11
#include <linux/sched/signal.h>
12
#include <linux/capability.h>
13
#include <linux/export.h>
14
#include <linux/suspend.h>
15 16
#include <linux/seq_file.h>
#include <linux/debugfs.h>
17
#include <linux/pm_wakeirq.h>
18
#include <trace/events/power.h>
19 20 21

#include "power.h"

22 23 24 25
/*
 * If set, the suspend/hibernate code will abort transitions to a sleep state
 * if wakeup events are registered during or immediately before the transition.
 */
26
bool events_check_enabled __read_mostly;
27

28 29 30
/* First wakeup IRQ seen by the kernel in the last cycle. */
unsigned int pm_wakeup_irq __read_mostly;

31 32
/* If greater than 0 and the system is suspending, terminate the suspend. */
static atomic_t pm_abort_suspend __read_mostly;
33

34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52
/*
 * Combined counters of registered wakeup events and wakeup events in progress.
 * They need to be modified together atomically, so it's better to use one
 * atomic variable to hold them both.
 */
static atomic_t combined_event_count = ATOMIC_INIT(0);

#define IN_PROGRESS_BITS	(sizeof(int) * 4)
#define MAX_IN_PROGRESS		((1 << IN_PROGRESS_BITS) - 1)

static void split_counters(unsigned int *cnt, unsigned int *inpr)
{
	unsigned int comb = atomic_read(&combined_event_count);

	*cnt = (comb >> IN_PROGRESS_BITS);
	*inpr = comb & MAX_IN_PROGRESS;
}

/* A preserved old value of the events counter. */
53
static unsigned int saved_count;
54 55 56

static DEFINE_SPINLOCK(events_lock);

57 58
static void pm_wakeup_timer_fn(unsigned long data);

59 60
static LIST_HEAD(wakeup_sources);

61 62
static DECLARE_WAIT_QUEUE_HEAD(wakeup_count_wait_queue);

T
Thomas Gleixner 已提交
63 64
DEFINE_STATIC_SRCU(wakeup_srcu);

65 66 67 68 69
static struct wakeup_source deleted_ws = {
	.name = "deleted",
	.lock =  __SPIN_LOCK_UNLOCKED(deleted_ws.lock),
};

70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86
/**
 * wakeup_source_prepare - Prepare a new wakeup source for initialization.
 * @ws: Wakeup source to prepare.
 * @name: Pointer to the name of the new wakeup source.
 *
 * Callers must ensure that the @name string won't be freed when @ws is still in
 * use.
 */
void wakeup_source_prepare(struct wakeup_source *ws, const char *name)
{
	if (ws) {
		memset(ws, 0, sizeof(*ws));
		ws->name = name;
	}
}
EXPORT_SYMBOL_GPL(wakeup_source_prepare);

87 88 89 90 91 92 93 94
/**
 * wakeup_source_create - Create a struct wakeup_source object.
 * @name: Name of the new wakeup source.
 */
struct wakeup_source *wakeup_source_create(const char *name)
{
	struct wakeup_source *ws;

95
	ws = kmalloc(sizeof(*ws), GFP_KERNEL);
96 97 98
	if (!ws)
		return NULL;

99
	wakeup_source_prepare(ws, name ? kstrdup_const(name, GFP_KERNEL) : NULL);
100 101 102 103 104
	return ws;
}
EXPORT_SYMBOL_GPL(wakeup_source_create);

/**
105 106
 * wakeup_source_drop - Prepare a struct wakeup_source object for destruction.
 * @ws: Wakeup source to prepare for destruction.
107 108 109
 *
 * Callers must ensure that __pm_stay_awake() or __pm_wakeup_event() will never
 * be run in parallel with this function for the same wakeup source object.
110
 */
111
void wakeup_source_drop(struct wakeup_source *ws)
112 113 114 115
{
	if (!ws)
		return;

116 117
	del_timer_sync(&ws->timer);
	__pm_relax(ws);
118 119 120
}
EXPORT_SYMBOL_GPL(wakeup_source_drop);

121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148
/*
 * Record wakeup_source statistics being deleted into a dummy wakeup_source.
 */
static void wakeup_source_record(struct wakeup_source *ws)
{
	unsigned long flags;

	spin_lock_irqsave(&deleted_ws.lock, flags);

	if (ws->event_count) {
		deleted_ws.total_time =
			ktime_add(deleted_ws.total_time, ws->total_time);
		deleted_ws.prevent_sleep_time =
			ktime_add(deleted_ws.prevent_sleep_time,
				  ws->prevent_sleep_time);
		deleted_ws.max_time =
			ktime_compare(deleted_ws.max_time, ws->max_time) > 0 ?
				deleted_ws.max_time : ws->max_time;
		deleted_ws.event_count += ws->event_count;
		deleted_ws.active_count += ws->active_count;
		deleted_ws.relax_count += ws->relax_count;
		deleted_ws.expire_count += ws->expire_count;
		deleted_ws.wakeup_count += ws->wakeup_count;
	}

	spin_unlock_irqrestore(&deleted_ws.lock, flags);
}

149 150 151 152 153 154 155 156 157 158 159 160
/**
 * wakeup_source_destroy - Destroy a struct wakeup_source object.
 * @ws: Wakeup source to destroy.
 *
 * Use only for wakeup source objects created with wakeup_source_create().
 */
void wakeup_source_destroy(struct wakeup_source *ws)
{
	if (!ws)
		return;

	wakeup_source_drop(ws);
161
	wakeup_source_record(ws);
162
	kfree_const(ws->name);
163 164 165 166 167 168 169 170 171 172
	kfree(ws);
}
EXPORT_SYMBOL_GPL(wakeup_source_destroy);

/**
 * wakeup_source_add - Add given object to the list of wakeup sources.
 * @ws: Wakeup source object to add to the list.
 */
void wakeup_source_add(struct wakeup_source *ws)
{
173 174
	unsigned long flags;

175 176 177
	if (WARN_ON(!ws))
		return;

178
	spin_lock_init(&ws->lock);
179 180
	setup_timer(&ws->timer, pm_wakeup_timer_fn, (unsigned long)ws);
	ws->active = false;
181
	ws->last_time = ktime_get();
182

183
	spin_lock_irqsave(&events_lock, flags);
184
	list_add_rcu(&ws->entry, &wakeup_sources);
185
	spin_unlock_irqrestore(&events_lock, flags);
186 187 188 189 190 191 192 193 194
}
EXPORT_SYMBOL_GPL(wakeup_source_add);

/**
 * wakeup_source_remove - Remove given object from the wakeup sources list.
 * @ws: Wakeup source object to remove from the list.
 */
void wakeup_source_remove(struct wakeup_source *ws)
{
195 196
	unsigned long flags;

197 198 199
	if (WARN_ON(!ws))
		return;

200
	spin_lock_irqsave(&events_lock, flags);
201
	list_del_rcu(&ws->entry);
202
	spin_unlock_irqrestore(&events_lock, flags);
T
Thomas Gleixner 已提交
203
	synchronize_srcu(&wakeup_srcu);
204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228
}
EXPORT_SYMBOL_GPL(wakeup_source_remove);

/**
 * wakeup_source_register - Create wakeup source and add it to the list.
 * @name: Name of the wakeup source to register.
 */
struct wakeup_source *wakeup_source_register(const char *name)
{
	struct wakeup_source *ws;

	ws = wakeup_source_create(name);
	if (ws)
		wakeup_source_add(ws);

	return ws;
}
EXPORT_SYMBOL_GPL(wakeup_source_register);

/**
 * wakeup_source_unregister - Remove wakeup source from the list and remove it.
 * @ws: Wakeup source object to unregister.
 */
void wakeup_source_unregister(struct wakeup_source *ws)
{
229 230 231 232
	if (ws) {
		wakeup_source_remove(ws);
		wakeup_source_destroy(ws);
	}
233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250
}
EXPORT_SYMBOL_GPL(wakeup_source_unregister);

/**
 * device_wakeup_attach - Attach a wakeup source object to a device object.
 * @dev: Device to handle.
 * @ws: Wakeup source object to attach to @dev.
 *
 * This causes @dev to be treated as a wakeup device.
 */
static int device_wakeup_attach(struct device *dev, struct wakeup_source *ws)
{
	spin_lock_irq(&dev->power.lock);
	if (dev->power.wakeup) {
		spin_unlock_irq(&dev->power.lock);
		return -EEXIST;
	}
	dev->power.wakeup = ws;
251 252
	if (dev->power.wakeirq)
		device_wakeup_attach_irq(dev, dev->power.wakeirq);
253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282
	spin_unlock_irq(&dev->power.lock);
	return 0;
}

/**
 * device_wakeup_enable - Enable given device to be a wakeup source.
 * @dev: Device to handle.
 *
 * Create a wakeup source object, register it and attach it to @dev.
 */
int device_wakeup_enable(struct device *dev)
{
	struct wakeup_source *ws;
	int ret;

	if (!dev || !dev->power.can_wakeup)
		return -EINVAL;

	ws = wakeup_source_register(dev_name(dev));
	if (!ws)
		return -ENOMEM;

	ret = device_wakeup_attach(dev, ws);
	if (ret)
		wakeup_source_unregister(ws);

	return ret;
}
EXPORT_SYMBOL_GPL(device_wakeup_enable);

283 284 285 286 287 288 289 290
/**
 * device_wakeup_attach_irq - Attach a wakeirq to a wakeup source
 * @dev: Device to handle
 * @wakeirq: Device specific wakeirq entry
 *
 * Attach a device wakeirq to the wakeup source so the device
 * wake IRQ can be configured automatically for suspend and
 * resume.
291 292
 *
 * Call under the device's power.lock lock.
293 294 295 296 297 298 299 300 301
 */
int device_wakeup_attach_irq(struct device *dev,
			     struct wake_irq *wakeirq)
{
	struct wakeup_source *ws;

	ws = dev->power.wakeup;
	if (!ws) {
		dev_err(dev, "forgot to call call device_init_wakeup?\n");
302
		return -EINVAL;
303 304
	}

305 306
	if (ws->wakeirq)
		return -EEXIST;
307 308

	ws->wakeirq = wakeirq;
309
	return 0;
310 311 312 313 314 315 316
}

/**
 * device_wakeup_detach_irq - Detach a wakeirq from a wakeup source
 * @dev: Device to handle
 *
 * Removes a device wakeirq from the wakeup source.
317 318
 *
 * Call under the device's power.lock lock.
319 320 321 322 323 324
 */
void device_wakeup_detach_irq(struct device *dev)
{
	struct wakeup_source *ws;

	ws = dev->power.wakeup;
325 326
	if (ws)
		ws->wakeirq = NULL;
327 328 329 330 331 332 333 334 335 336
}

/**
 * device_wakeup_arm_wake_irqs(void)
 *
 * Itereates over the list of device wakeirqs to arm them.
 */
void device_wakeup_arm_wake_irqs(void)
{
	struct wakeup_source *ws;
T
Thomas Gleixner 已提交
337
	int srcuidx;
338

T
Thomas Gleixner 已提交
339
	srcuidx = srcu_read_lock(&wakeup_srcu);
340 341
	list_for_each_entry_rcu(ws, &wakeup_sources, entry)
		dev_pm_arm_wake_irq(ws->wakeirq);
T
Thomas Gleixner 已提交
342
	srcu_read_unlock(&wakeup_srcu, srcuidx);
343 344 345 346 347 348 349 350 351 352
}

/**
 * device_wakeup_disarm_wake_irqs(void)
 *
 * Itereates over the list of device wakeirqs to disarm them.
 */
void device_wakeup_disarm_wake_irqs(void)
{
	struct wakeup_source *ws;
T
Thomas Gleixner 已提交
353
	int srcuidx;
354

T
Thomas Gleixner 已提交
355
	srcuidx = srcu_read_lock(&wakeup_srcu);
356 357
	list_for_each_entry_rcu(ws, &wakeup_sources, entry)
		dev_pm_disarm_wake_irq(ws->wakeirq);
T
Thomas Gleixner 已提交
358
	srcu_read_unlock(&wakeup_srcu, srcuidx);
359 360
}

361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392
/**
 * device_wakeup_detach - Detach a device's wakeup source object from it.
 * @dev: Device to detach the wakeup source object from.
 *
 * After it returns, @dev will not be treated as a wakeup device any more.
 */
static struct wakeup_source *device_wakeup_detach(struct device *dev)
{
	struct wakeup_source *ws;

	spin_lock_irq(&dev->power.lock);
	ws = dev->power.wakeup;
	dev->power.wakeup = NULL;
	spin_unlock_irq(&dev->power.lock);
	return ws;
}

/**
 * device_wakeup_disable - Do not regard a device as a wakeup source any more.
 * @dev: Device to handle.
 *
 * Detach the @dev's wakeup source object from it, unregister this wakeup source
 * object and destroy it.
 */
int device_wakeup_disable(struct device *dev)
{
	struct wakeup_source *ws;

	if (!dev || !dev->power.can_wakeup)
		return -EINVAL;

	ws = device_wakeup_detach(dev);
393
	wakeup_source_unregister(ws);
394 395 396 397
	return 0;
}
EXPORT_SYMBOL_GPL(device_wakeup_disable);

398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414
/**
 * device_set_wakeup_capable - Set/reset device wakeup capability flag.
 * @dev: Device to handle.
 * @capable: Whether or not @dev is capable of waking up the system from sleep.
 *
 * If @capable is set, set the @dev's power.can_wakeup flag and add its
 * wakeup-related attributes to sysfs.  Otherwise, unset the @dev's
 * power.can_wakeup flag and remove its wakeup-related attributes from sysfs.
 *
 * This function may sleep and it can't be called from any context where
 * sleeping is not allowed.
 */
void device_set_wakeup_capable(struct device *dev, bool capable)
{
	if (!!dev->power.can_wakeup == !!capable)
		return;

415
	dev->power.can_wakeup = capable;
416
	if (device_is_registered(dev) && !list_empty(&dev->power.entry)) {
417
		if (capable) {
418 419 420 421
			int ret = wakeup_sysfs_add(dev);

			if (ret)
				dev_info(dev, "Wakeup sysfs attributes not added\n");
422 423 424 425 426 427 428
		} else {
			wakeup_sysfs_remove(dev);
		}
	}
}
EXPORT_SYMBOL_GPL(device_set_wakeup_capable);

429 430 431 432 433 434 435
/**
 * device_init_wakeup - Device wakeup initialization.
 * @dev: Device to handle.
 * @enable: Whether or not to enable @dev as a wakeup device.
 *
 * By default, most devices should leave wakeup disabled.  The exceptions are
 * devices that everyone expects to be wakeup sources: keyboards, power buttons,
436 437 438
 * possibly network interfaces, etc.  Also, devices that don't generate their
 * own wakeup requests but merely forward requests from one bus to another
 * (like PCI bridges) should have wakeup enabled by default.
439 440 441 442 443
 */
int device_init_wakeup(struct device *dev, bool enable)
{
	int ret = 0;

444 445 446
	if (!dev)
		return -EINVAL;

447 448 449 450
	if (enable) {
		device_set_wakeup_capable(dev, true);
		ret = device_wakeup_enable(dev);
	} else {
451 452 453
		if (dev->power.can_wakeup)
			device_wakeup_disable(dev);

454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472
		device_set_wakeup_capable(dev, false);
	}

	return ret;
}
EXPORT_SYMBOL_GPL(device_init_wakeup);

/**
 * device_set_wakeup_enable - Enable or disable a device to wake up the system.
 * @dev: Device to handle.
 */
int device_set_wakeup_enable(struct device *dev, bool enable)
{
	if (!dev || !dev->power.can_wakeup)
		return -EINVAL;

	return enable ? device_wakeup_enable(dev) : device_wakeup_disable(dev);
}
EXPORT_SYMBOL_GPL(device_set_wakeup_enable);
473

474 475 476 477 478 479 480 481 482 483 484 485 486 487
/**
 * wakeup_source_not_registered - validate the given wakeup source.
 * @ws: Wakeup source to be validated.
 */
static bool wakeup_source_not_registered(struct wakeup_source *ws)
{
	/*
	 * Use timer struct to check if the given source is initialized
	 * by wakeup_source_add.
	 */
	return ws->timer.function != pm_wakeup_timer_fn ||
		   ws->timer.data != (unsigned long)ws;
}

488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509
/*
 * The functions below use the observation that each wakeup event starts a
 * period in which the system should not be suspended.  The moment this period
 * will end depends on how the wakeup event is going to be processed after being
 * detected and all of the possible cases can be divided into two distinct
 * groups.
 *
 * First, a wakeup event may be detected by the same functional unit that will
 * carry out the entire processing of it and possibly will pass it to user space
 * for further processing.  In that case the functional unit that has detected
 * the event may later "close" the "no suspend" period associated with it
 * directly as soon as it has been dealt with.  The pair of pm_stay_awake() and
 * pm_relax(), balanced with each other, is supposed to be used in such
 * situations.
 *
 * Second, a wakeup event may be detected by one functional unit and processed
 * by another one.  In that case the unit that has detected it cannot really
 * "close" the "no suspend" period associated with it, unless it knows in
 * advance what's going to happen to the event during processing.  This
 * knowledge, however, may not be available to it, so it can simply specify time
 * to wait before the system can be suspended and pass it as the second
 * argument of pm_wakeup_event().
510 511 512 513
 *
 * It is valid to call pm_relax() after pm_wakeup_event(), in which case the
 * "no suspend" period will be ended either by the pm_relax(), or by the timer
 * function executed when the timer expires, whichever comes first.
514 515
 */

516 517 518 519 520 521 522 523
/**
 * wakup_source_activate - Mark given wakeup source as active.
 * @ws: Wakeup source to handle.
 *
 * Update the @ws' statistics and, if @ws has just been activated, notify the PM
 * core of the event by incrementing the counter of of wakeup events being
 * processed.
 */
524
static void wakeup_source_activate(struct wakeup_source *ws)
525
{
526 527
	unsigned int cec;

528 529 530 531
	if (WARN_ONCE(wakeup_source_not_registered(ws),
			"unregistered wakeup source\n"))
		return;

532 533 534
	ws->active = true;
	ws->active_count++;
	ws->last_time = ktime_get();
535 536
	if (ws->autosleep_enabled)
		ws->start_prevent_time = ws->last_time;
537

538
	/* Increment the counter of events in progress. */
539 540 541
	cec = atomic_inc_return(&combined_event_count);

	trace_wakeup_source_activate(ws->name, cec);
542 543
}

544 545 546
/**
 * wakeup_source_report_event - Report wakeup event using the given source.
 * @ws: Wakeup source to report the event for.
547
 * @hard: If set, abort suspends in progress and wake up from suspend-to-idle.
548
 */
549
static void wakeup_source_report_event(struct wakeup_source *ws, bool hard)
550 551 552 553 554 555 556
{
	ws->event_count++;
	/* This is racy, but the counter is approximate anyway. */
	if (events_check_enabled)
		ws->wakeup_count++;

	if (!ws->active)
557 558 559 560
		wakeup_source_activate(ws);

	if (hard)
		pm_system_wakeup();
561 562
}

563 564 565 566 567 568 569 570 571 572 573 574 575 576
/**
 * __pm_stay_awake - Notify the PM core of a wakeup event.
 * @ws: Wakeup source object associated with the source of the event.
 *
 * It is safe to call this function from interrupt context.
 */
void __pm_stay_awake(struct wakeup_source *ws)
{
	unsigned long flags;

	if (!ws)
		return;

	spin_lock_irqsave(&ws->lock, flags);
577

578
	wakeup_source_report_event(ws, false);
579 580 581
	del_timer(&ws->timer);
	ws->timer_expires = 0;

582 583 584 585
	spin_unlock_irqrestore(&ws->lock, flags);
}
EXPORT_SYMBOL_GPL(__pm_stay_awake);

586 587 588 589
/**
 * pm_stay_awake - Notify the PM core that a wakeup event is being processed.
 * @dev: Device the wakeup event is related to.
 *
590 591
 * Notify the PM core of a wakeup event (signaled by @dev) by calling
 * __pm_stay_awake for the @dev's wakeup source object.
592 593 594 595 596 597 598 599 600
 *
 * Call this function after detecting of a wakeup event if pm_relax() is going
 * to be called directly after processing the event (and possibly passing it to
 * user space for further processing).
 */
void pm_stay_awake(struct device *dev)
{
	unsigned long flags;

601 602
	if (!dev)
		return;
603

604 605 606
	spin_lock_irqsave(&dev->power.lock, flags);
	__pm_stay_awake(dev->power.wakeup);
	spin_unlock_irqrestore(&dev->power.lock, flags);
607
}
608
EXPORT_SYMBOL_GPL(pm_stay_awake);
609

610 611 612 613 614 615 616 617 618 619 620
#ifdef CONFIG_PM_AUTOSLEEP
static void update_prevent_sleep_time(struct wakeup_source *ws, ktime_t now)
{
	ktime_t delta = ktime_sub(now, ws->start_prevent_time);
	ws->prevent_sleep_time = ktime_add(ws->prevent_sleep_time, delta);
}
#else
static inline void update_prevent_sleep_time(struct wakeup_source *ws,
					     ktime_t now) {}
#endif

621
/**
622 623
 * wakup_source_deactivate - Mark given wakeup source as inactive.
 * @ws: Wakeup source to handle.
624
 *
625 626 627 628 629 630
 * Update the @ws' statistics and notify the PM core that the wakeup source has
 * become inactive by decrementing the counter of wakeup events being processed
 * and incrementing the counter of registered wakeup events.
 */
static void wakeup_source_deactivate(struct wakeup_source *ws)
{
631
	unsigned int cnt, inpr, cec;
632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657
	ktime_t duration;
	ktime_t now;

	ws->relax_count++;
	/*
	 * __pm_relax() may be called directly or from a timer function.
	 * If it is called directly right after the timer function has been
	 * started, but before the timer function calls __pm_relax(), it is
	 * possible that __pm_stay_awake() will be called in the meantime and
	 * will set ws->active.  Then, ws->active may be cleared immediately
	 * by the __pm_relax() called from the timer function, but in such a
	 * case ws->relax_count will be different from ws->active_count.
	 */
	if (ws->relax_count != ws->active_count) {
		ws->relax_count--;
		return;
	}

	ws->active = false;

	now = ktime_get();
	duration = ktime_sub(now, ws->last_time);
	ws->total_time = ktime_add(ws->total_time, duration);
	if (ktime_to_ns(duration) > ktime_to_ns(ws->max_time))
		ws->max_time = duration;

658
	ws->last_time = now;
659
	del_timer(&ws->timer);
660
	ws->timer_expires = 0;
661

662 663 664
	if (ws->autosleep_enabled)
		update_prevent_sleep_time(ws, now);

665
	/*
666 667
	 * Increment the counter of registered wakeup events and decrement the
	 * couter of wakeup events in progress simultaneously.
668
	 */
669 670
	cec = atomic_add_return(MAX_IN_PROGRESS, &combined_event_count);
	trace_wakeup_source_deactivate(ws->name, cec);
671 672 673 674

	split_counters(&cnt, &inpr);
	if (!inpr && waitqueue_active(&wakeup_count_wait_queue))
		wake_up(&wakeup_count_wait_queue);
675 676 677 678 679
}

/**
 * __pm_relax - Notify the PM core that processing of a wakeup event has ended.
 * @ws: Wakeup source object associated with the source of the event.
680 681
 *
 * Call this function for wakeup events whose processing started with calling
682
 * __pm_stay_awake().
683 684 685
 *
 * It is safe to call it from interrupt context.
 */
686
void __pm_relax(struct wakeup_source *ws)
687 688 689
{
	unsigned long flags;

690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715
	if (!ws)
		return;

	spin_lock_irqsave(&ws->lock, flags);
	if (ws->active)
		wakeup_source_deactivate(ws);
	spin_unlock_irqrestore(&ws->lock, flags);
}
EXPORT_SYMBOL_GPL(__pm_relax);

/**
 * pm_relax - Notify the PM core that processing of a wakeup event has ended.
 * @dev: Device that signaled the event.
 *
 * Execute __pm_relax() for the @dev's wakeup source object.
 */
void pm_relax(struct device *dev)
{
	unsigned long flags;

	if (!dev)
		return;

	spin_lock_irqsave(&dev->power.lock, flags);
	__pm_relax(dev->power.wakeup);
	spin_unlock_irqrestore(&dev->power.lock, flags);
716
}
717
EXPORT_SYMBOL_GPL(pm_relax);
718 719

/**
720
 * pm_wakeup_timer_fn - Delayed finalization of a wakeup event.
721
 * @data: Address of the wakeup source object associated with the event source.
722
 *
723 724 725
 * Call wakeup_source_deactivate() for the wakeup source whose address is stored
 * in @data if it is currently active and its timer has not been canceled and
 * the expiration time of the timer is not in future.
726
 */
727
static void pm_wakeup_timer_fn(unsigned long data)
728
{
729 730 731 732 733 734
	struct wakeup_source *ws = (struct wakeup_source *)data;
	unsigned long flags;

	spin_lock_irqsave(&ws->lock, flags);

	if (ws->active && ws->timer_expires
735
	    && time_after_eq(jiffies, ws->timer_expires)) {
736
		wakeup_source_deactivate(ws);
737 738
		ws->expire_count++;
	}
739 740

	spin_unlock_irqrestore(&ws->lock, flags);
741 742 743
}

/**
744
 * pm_wakeup_ws_event - Notify the PM core of a wakeup event.
745 746
 * @ws: Wakeup source object associated with the event source.
 * @msec: Anticipated event processing time (in milliseconds).
747
 * @hard: If set, abort suspends in progress and wake up from suspend-to-idle.
748 749 750 751 752 753 754 755
 *
 * Notify the PM core of a wakeup event whose source is @ws that will take
 * approximately @msec milliseconds to be processed by the kernel.  If @ws is
 * not active, activate it.  If @msec is nonzero, set up the @ws' timer to
 * execute pm_wakeup_timer_fn() in future.
 *
 * It is safe to call this function from interrupt context.
 */
756
void pm_wakeup_ws_event(struct wakeup_source *ws, unsigned int msec, bool hard)
757
{
758
	unsigned long flags;
759
	unsigned long expires;
760

761 762 763 764 765
	if (!ws)
		return;

	spin_lock_irqsave(&ws->lock, flags);

766
	wakeup_source_report_event(ws, hard);
767 768 769 770

	if (!msec) {
		wakeup_source_deactivate(ws);
		goto unlock;
771
	}
772 773 774 775 776

	expires = jiffies + msecs_to_jiffies(msec);
	if (!expires)
		expires = 1;

777
	if (!ws->timer_expires || time_after(expires, ws->timer_expires)) {
778 779 780 781 782 783
		mod_timer(&ws->timer, expires);
		ws->timer_expires = expires;
	}

 unlock:
	spin_unlock_irqrestore(&ws->lock, flags);
784
}
785
EXPORT_SYMBOL_GPL(pm_wakeup_ws_event);
786 787 788 789 790

/**
 * pm_wakeup_event - Notify the PM core of a wakeup event.
 * @dev: Device the wakeup event is related to.
 * @msec: Anticipated event processing time (in milliseconds).
791
 * @hard: If set, abort suspends in progress and wake up from suspend-to-idle.
792
 *
793
 * Call pm_wakeup_ws_event() for the @dev's wakeup source object.
794
 */
795
void pm_wakeup_dev_event(struct device *dev, unsigned int msec, bool hard)
796 797 798
{
	unsigned long flags;

799 800
	if (!dev)
		return;
801

802
	spin_lock_irqsave(&dev->power.lock, flags);
803
	pm_wakeup_ws_event(dev->power.wakeup, msec, hard);
804 805
	spin_unlock_irqrestore(&dev->power.lock, flags);
}
806
EXPORT_SYMBOL_GPL(pm_wakeup_dev_event);
807

808
void pm_print_active_wakeup_sources(void)
809 810
{
	struct wakeup_source *ws;
T
Thomas Gleixner 已提交
811
	int srcuidx, active = 0;
812 813
	struct wakeup_source *last_activity_ws = NULL;

T
Thomas Gleixner 已提交
814
	srcuidx = srcu_read_lock(&wakeup_srcu);
815 816
	list_for_each_entry_rcu(ws, &wakeup_sources, entry) {
		if (ws->active) {
817
			pr_debug("active wakeup source: %s\n", ws->name);
818 819 820 821 822 823 824 825 826 827
			active = 1;
		} else if (!active &&
			   (!last_activity_ws ||
			    ktime_to_ns(ws->last_time) >
			    ktime_to_ns(last_activity_ws->last_time))) {
			last_activity_ws = ws;
		}
	}

	if (!active && last_activity_ws)
828
		pr_debug("last active wakeup source: %s\n",
829
			last_activity_ws->name);
T
Thomas Gleixner 已提交
830
	srcu_read_unlock(&wakeup_srcu, srcuidx);
831
}
832
EXPORT_SYMBOL_GPL(pm_print_active_wakeup_sources);
833

834
/**
835
 * pm_wakeup_pending - Check if power transition in progress should be aborted.
836 837
 *
 * Compare the current number of registered wakeup events with its preserved
838 839 840
 * value from the past and return true if new wakeup events have been registered
 * since the old value was stored.  Also return true if the current number of
 * wakeup events being processed is different from zero.
841
 */
842
bool pm_wakeup_pending(void)
843 844
{
	unsigned long flags;
845
	bool ret = false;
846 847 848

	spin_lock_irqsave(&events_lock, flags);
	if (events_check_enabled) {
849 850 851 852
		unsigned int cnt, inpr;

		split_counters(&cnt, &inpr);
		ret = (cnt != saved_count || inpr > 0);
853
		events_check_enabled = !ret;
854 855
	}
	spin_unlock_irqrestore(&events_lock, flags);
856

857 858
	if (ret) {
		pr_info("PM: Wakeup pending, aborting suspend\n");
859
		pm_print_active_wakeup_sources();
860
	}
861

862
	return ret || atomic_read(&pm_abort_suspend) > 0;
863 864 865 866
}

void pm_system_wakeup(void)
{
867
	atomic_inc(&pm_abort_suspend);
868
	s2idle_wake();
869
}
870
EXPORT_SYMBOL_GPL(pm_system_wakeup);
871

872 873 874 875 876 877
void pm_system_cancel_wakeup(void)
{
	atomic_dec(&pm_abort_suspend);
}

void pm_wakeup_clear(bool reset)
878
{
879
	pm_wakeup_irq = 0;
880 881
	if (reset)
		atomic_set(&pm_abort_suspend, 0);
882 883 884 885 886 887 888 889
}

void pm_system_irq_wakeup(unsigned int irq_number)
{
	if (pm_wakeup_irq == 0) {
		pm_wakeup_irq = irq_number;
		pm_system_wakeup();
	}
890 891 892 893 894
}

/**
 * pm_get_wakeup_count - Read the number of registered wakeup events.
 * @count: Address to store the value at.
895
 * @block: Whether or not to block.
896
 *
897 898 899
 * Store the number of registered wakeup events at the address in @count.  If
 * @block is set, block until the current number of wakeup events being
 * processed is zero.
900
 *
901 902
 * Return 'false' if the current number of wakeup events being processed is
 * nonzero.  Otherwise return 'true'.
903
 */
904
bool pm_get_wakeup_count(unsigned int *count, bool block)
905
{
906
	unsigned int cnt, inpr;
907

908 909 910 911 912 913 914 915 916
	if (block) {
		DEFINE_WAIT(wait);

		for (;;) {
			prepare_to_wait(&wakeup_count_wait_queue, &wait,
					TASK_INTERRUPTIBLE);
			split_counters(&cnt, &inpr);
			if (inpr == 0 || signal_pending(current))
				break;
917
			pm_print_active_wakeup_sources();
918 919 920
			schedule();
		}
		finish_wait(&wakeup_count_wait_queue, &wait);
921
	}
922

923 924 925
	split_counters(&cnt, &inpr);
	*count = cnt;
	return !inpr;
926 927 928 929 930 931 932 933
}

/**
 * pm_save_wakeup_count - Save the current number of registered wakeup events.
 * @count: Value to compare with the current number of registered wakeup events.
 *
 * If @count is equal to the current number of registered wakeup events and the
 * current number of wakeup events being processed is zero, store @count as the
934 935 936
 * old number of registered wakeup events for pm_check_wakeup_events(), enable
 * wakeup events detection and return 'true'.  Otherwise disable wakeup events
 * detection and return 'false'.
937
 */
938
bool pm_save_wakeup_count(unsigned int count)
939
{
940
	unsigned int cnt, inpr;
941
	unsigned long flags;
942

943
	events_check_enabled = false;
944
	spin_lock_irqsave(&events_lock, flags);
945 946
	split_counters(&cnt, &inpr);
	if (cnt == count && inpr == 0) {
947
		saved_count = count;
948 949
		events_check_enabled = true;
	}
950
	spin_unlock_irqrestore(&events_lock, flags);
951
	return events_check_enabled;
952
}
953

954 955 956 957 958 959 960 961 962
#ifdef CONFIG_PM_AUTOSLEEP
/**
 * pm_wakep_autosleep_enabled - Modify autosleep_enabled for all wakeup sources.
 * @enabled: Whether to set or to clear the autosleep_enabled flags.
 */
void pm_wakep_autosleep_enabled(bool set)
{
	struct wakeup_source *ws;
	ktime_t now = ktime_get();
T
Thomas Gleixner 已提交
963
	int srcuidx;
964

T
Thomas Gleixner 已提交
965
	srcuidx = srcu_read_lock(&wakeup_srcu);
966 967 968 969 970 971 972 973 974 975 976 977 978
	list_for_each_entry_rcu(ws, &wakeup_sources, entry) {
		spin_lock_irq(&ws->lock);
		if (ws->autosleep_enabled != set) {
			ws->autosleep_enabled = set;
			if (ws->active) {
				if (set)
					ws->start_prevent_time = now;
				else
					update_prevent_sleep_time(ws, now);
			}
		}
		spin_unlock_irq(&ws->lock);
	}
T
Thomas Gleixner 已提交
979
	srcu_read_unlock(&wakeup_srcu, srcuidx);
980 981 982
}
#endif /* CONFIG_PM_AUTOSLEEP */

983 984 985 986 987 988 989 990 991 992 993 994 995 996 997
static struct dentry *wakeup_sources_stats_dentry;

/**
 * print_wakeup_source_stats - Print wakeup source statistics information.
 * @m: seq_file to print the statistics into.
 * @ws: Wakeup source object to print the statistics for.
 */
static int print_wakeup_source_stats(struct seq_file *m,
				     struct wakeup_source *ws)
{
	unsigned long flags;
	ktime_t total_time;
	ktime_t max_time;
	unsigned long active_count;
	ktime_t active_time;
998
	ktime_t prevent_sleep_time;
999 1000 1001 1002 1003

	spin_lock_irqsave(&ws->lock, flags);

	total_time = ws->total_time;
	max_time = ws->max_time;
1004
	prevent_sleep_time = ws->prevent_sleep_time;
1005 1006
	active_count = ws->active_count;
	if (ws->active) {
1007 1008 1009
		ktime_t now = ktime_get();

		active_time = ktime_sub(now, ws->last_time);
1010
		total_time = ktime_add(total_time, active_time);
T
Thomas Gleixner 已提交
1011
		if (active_time > max_time)
1012
			max_time = active_time;
1013 1014 1015 1016

		if (ws->autosleep_enabled)
			prevent_sleep_time = ktime_add(prevent_sleep_time,
				ktime_sub(now, ws->start_prevent_time));
1017
	} else {
T
Thomas Gleixner 已提交
1018
		active_time = 0;
1019 1020
	}

1021 1022 1023 1024 1025 1026
	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",
		   ws->name, active_count, ws->event_count,
		   ws->wakeup_count, ws->expire_count,
		   ktime_to_ms(active_time), ktime_to_ms(total_time),
		   ktime_to_ms(max_time), ktime_to_ms(ws->last_time),
		   ktime_to_ms(prevent_sleep_time));
1027 1028 1029

	spin_unlock_irqrestore(&ws->lock, flags);

1030
	return 0;
1031 1032 1033 1034 1035 1036 1037 1038 1039
}

/**
 * wakeup_sources_stats_show - Print wakeup sources statistics information.
 * @m: seq_file to print the statistics into.
 */
static int wakeup_sources_stats_show(struct seq_file *m, void *unused)
{
	struct wakeup_source *ws;
T
Thomas Gleixner 已提交
1040
	int srcuidx;
1041

1042 1043
	seq_puts(m, "name\t\tactive_count\tevent_count\twakeup_count\t"
		"expire_count\tactive_since\ttotal_time\tmax_time\t"
1044
		"last_change\tprevent_suspend_time\n");
1045

T
Thomas Gleixner 已提交
1046
	srcuidx = srcu_read_lock(&wakeup_srcu);
1047 1048
	list_for_each_entry_rcu(ws, &wakeup_sources, entry)
		print_wakeup_source_stats(m, ws);
T
Thomas Gleixner 已提交
1049
	srcu_read_unlock(&wakeup_srcu, srcuidx);
1050

1051 1052
	print_wakeup_source_stats(m, &deleted_ws);

1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076
	return 0;
}

static int wakeup_sources_stats_open(struct inode *inode, struct file *file)
{
	return single_open(file, wakeup_sources_stats_show, NULL);
}

static const struct file_operations wakeup_sources_stats_fops = {
	.owner = THIS_MODULE,
	.open = wakeup_sources_stats_open,
	.read = seq_read,
	.llseek = seq_lseek,
	.release = single_release,
};

static int __init wakeup_sources_debugfs_init(void)
{
	wakeup_sources_stats_dentry = debugfs_create_file("wakeup_sources",
			S_IRUGO, NULL, NULL, &wakeup_sources_stats_fops);
	return 0;
}

postcore_initcall(wakeup_sources_debugfs_init);