wakeup.c 28 KB

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