wakeup.c 30 KB

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