wakeup.c 28 KB

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