wakeup.c 30 KB

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