wakeup.c 31 KB

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