wakeup.c 25 KB

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