wakeup.c 24 KB

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