wakeup.c 32 KB

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