wakeup.c 29 KB

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