wakeup.c 21 KB

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