wakeup.c 25 KB

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