wakeup.c 30 KB

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