wakeup.c 23 KB

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