wakeup.c 28 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070
  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/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. /*
  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 greater than 0 and the system is suspending, terminate the suspend. */
  27. static atomic_t 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. ws->active = true;
  454. ws->active_count++;
  455. ws->last_time = ktime_get();
  456. if (ws->autosleep_enabled)
  457. ws->start_prevent_time = ws->last_time;
  458. /* Increment the counter of events in progress. */
  459. cec = atomic_inc_return(&combined_event_count);
  460. trace_wakeup_source_activate(ws->name, cec);
  461. }
  462. /**
  463. * wakeup_source_report_event - Report wakeup event using the given source.
  464. * @ws: Wakeup source to report the event for.
  465. * @hard: If set, abort suspends in progress and wake up from suspend-to-idle.
  466. */
  467. static void wakeup_source_report_event(struct wakeup_source *ws, bool hard)
  468. {
  469. ws->event_count++;
  470. /* This is racy, but the counter is approximate anyway. */
  471. if (events_check_enabled)
  472. ws->wakeup_count++;
  473. if (!ws->active)
  474. wakeup_source_activate(ws);
  475. if (hard)
  476. pm_system_wakeup();
  477. }
  478. /**
  479. * __pm_stay_awake - Notify the PM core of a wakeup event.
  480. * @ws: Wakeup source object associated with the source of the event.
  481. *
  482. * It is safe to call this function from interrupt context.
  483. */
  484. void __pm_stay_awake(struct wakeup_source *ws)
  485. {
  486. unsigned long flags;
  487. if (!ws)
  488. return;
  489. spin_lock_irqsave(&ws->lock, flags);
  490. wakeup_source_report_event(ws, false);
  491. del_timer(&ws->timer);
  492. ws->timer_expires = 0;
  493. spin_unlock_irqrestore(&ws->lock, flags);
  494. }
  495. EXPORT_SYMBOL_GPL(__pm_stay_awake);
  496. /**
  497. * pm_stay_awake - Notify the PM core that a wakeup event is being processed.
  498. * @dev: Device the wakeup event is related to.
  499. *
  500. * Notify the PM core of a wakeup event (signaled by @dev) by calling
  501. * __pm_stay_awake for the @dev's wakeup source object.
  502. *
  503. * Call this function after detecting of a wakeup event if pm_relax() is going
  504. * to be called directly after processing the event (and possibly passing it to
  505. * user space for further processing).
  506. */
  507. void pm_stay_awake(struct device *dev)
  508. {
  509. unsigned long flags;
  510. if (!dev)
  511. return;
  512. spin_lock_irqsave(&dev->power.lock, flags);
  513. __pm_stay_awake(dev->power.wakeup);
  514. spin_unlock_irqrestore(&dev->power.lock, flags);
  515. }
  516. EXPORT_SYMBOL_GPL(pm_stay_awake);
  517. #ifdef CONFIG_PM_AUTOSLEEP
  518. static void update_prevent_sleep_time(struct wakeup_source *ws, ktime_t now)
  519. {
  520. ktime_t delta = ktime_sub(now, ws->start_prevent_time);
  521. ws->prevent_sleep_time = ktime_add(ws->prevent_sleep_time, delta);
  522. }
  523. #else
  524. static inline void update_prevent_sleep_time(struct wakeup_source *ws,
  525. ktime_t now) {}
  526. #endif
  527. /**
  528. * wakup_source_deactivate - Mark given wakeup source as inactive.
  529. * @ws: Wakeup source to handle.
  530. *
  531. * Update the @ws' statistics and notify the PM core that the wakeup source has
  532. * become inactive by decrementing the counter of wakeup events being processed
  533. * and incrementing the counter of registered wakeup events.
  534. */
  535. static void wakeup_source_deactivate(struct wakeup_source *ws)
  536. {
  537. unsigned int cnt, inpr, cec;
  538. ktime_t duration;
  539. ktime_t now;
  540. ws->relax_count++;
  541. /*
  542. * __pm_relax() may be called directly or from a timer function.
  543. * If it is called directly right after the timer function has been
  544. * started, but before the timer function calls __pm_relax(), it is
  545. * possible that __pm_stay_awake() will be called in the meantime and
  546. * will set ws->active. Then, ws->active may be cleared immediately
  547. * by the __pm_relax() called from the timer function, but in such a
  548. * case ws->relax_count will be different from ws->active_count.
  549. */
  550. if (ws->relax_count != ws->active_count) {
  551. ws->relax_count--;
  552. return;
  553. }
  554. ws->active = false;
  555. now = ktime_get();
  556. duration = ktime_sub(now, ws->last_time);
  557. ws->total_time = ktime_add(ws->total_time, duration);
  558. if (ktime_to_ns(duration) > ktime_to_ns(ws->max_time))
  559. ws->max_time = duration;
  560. ws->last_time = now;
  561. del_timer(&ws->timer);
  562. ws->timer_expires = 0;
  563. if (ws->autosleep_enabled)
  564. update_prevent_sleep_time(ws, now);
  565. /*
  566. * Increment the counter of registered wakeup events and decrement the
  567. * couter of wakeup events in progress simultaneously.
  568. */
  569. cec = atomic_add_return(MAX_IN_PROGRESS, &combined_event_count);
  570. trace_wakeup_source_deactivate(ws->name, cec);
  571. split_counters(&cnt, &inpr);
  572. if (!inpr && waitqueue_active(&wakeup_count_wait_queue))
  573. wake_up(&wakeup_count_wait_queue);
  574. }
  575. /**
  576. * __pm_relax - Notify the PM core that processing of a wakeup event has ended.
  577. * @ws: Wakeup source object associated with the source of the event.
  578. *
  579. * Call this function for wakeup events whose processing started with calling
  580. * __pm_stay_awake().
  581. *
  582. * It is safe to call it from interrupt context.
  583. */
  584. void __pm_relax(struct wakeup_source *ws)
  585. {
  586. unsigned long flags;
  587. if (!ws)
  588. return;
  589. spin_lock_irqsave(&ws->lock, flags);
  590. if (ws->active)
  591. wakeup_source_deactivate(ws);
  592. spin_unlock_irqrestore(&ws->lock, flags);
  593. }
  594. EXPORT_SYMBOL_GPL(__pm_relax);
  595. /**
  596. * pm_relax - Notify the PM core that processing of a wakeup event has ended.
  597. * @dev: Device that signaled the event.
  598. *
  599. * Execute __pm_relax() for the @dev's wakeup source object.
  600. */
  601. void pm_relax(struct device *dev)
  602. {
  603. unsigned long flags;
  604. if (!dev)
  605. return;
  606. spin_lock_irqsave(&dev->power.lock, flags);
  607. __pm_relax(dev->power.wakeup);
  608. spin_unlock_irqrestore(&dev->power.lock, flags);
  609. }
  610. EXPORT_SYMBOL_GPL(pm_relax);
  611. /**
  612. * pm_wakeup_timer_fn - Delayed finalization of a wakeup event.
  613. * @data: Address of the wakeup source object associated with the event source.
  614. *
  615. * Call wakeup_source_deactivate() for the wakeup source whose address is stored
  616. * in @data if it is currently active and its timer has not been canceled and
  617. * the expiration time of the timer is not in future.
  618. */
  619. static void pm_wakeup_timer_fn(unsigned long data)
  620. {
  621. struct wakeup_source *ws = (struct wakeup_source *)data;
  622. unsigned long flags;
  623. spin_lock_irqsave(&ws->lock, flags);
  624. if (ws->active && ws->timer_expires
  625. && time_after_eq(jiffies, ws->timer_expires)) {
  626. wakeup_source_deactivate(ws);
  627. ws->expire_count++;
  628. }
  629. spin_unlock_irqrestore(&ws->lock, flags);
  630. }
  631. /**
  632. * pm_wakeup_ws_event - Notify the PM core of a wakeup event.
  633. * @ws: Wakeup source object associated with the event source.
  634. * @msec: Anticipated event processing time (in milliseconds).
  635. * @hard: If set, abort suspends in progress and wake up from suspend-to-idle.
  636. *
  637. * Notify the PM core of a wakeup event whose source is @ws that will take
  638. * approximately @msec milliseconds to be processed by the kernel. If @ws is
  639. * not active, activate it. If @msec is nonzero, set up the @ws' timer to
  640. * execute pm_wakeup_timer_fn() in future.
  641. *
  642. * It is safe to call this function from interrupt context.
  643. */
  644. void pm_wakeup_ws_event(struct wakeup_source *ws, unsigned int msec, bool hard)
  645. {
  646. unsigned long flags;
  647. unsigned long expires;
  648. if (!ws)
  649. return;
  650. spin_lock_irqsave(&ws->lock, flags);
  651. wakeup_source_report_event(ws, hard);
  652. if (!msec) {
  653. wakeup_source_deactivate(ws);
  654. goto unlock;
  655. }
  656. expires = jiffies + msecs_to_jiffies(msec);
  657. if (!expires)
  658. expires = 1;
  659. if (!ws->timer_expires || time_after(expires, ws->timer_expires)) {
  660. mod_timer(&ws->timer, expires);
  661. ws->timer_expires = expires;
  662. }
  663. unlock:
  664. spin_unlock_irqrestore(&ws->lock, flags);
  665. }
  666. EXPORT_SYMBOL_GPL(pm_wakeup_ws_event);
  667. /**
  668. * pm_wakeup_event - Notify the PM core of a wakeup event.
  669. * @dev: Device the wakeup event is related to.
  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. * Call pm_wakeup_ws_event() for the @dev's wakeup source object.
  674. */
  675. void pm_wakeup_dev_event(struct device *dev, unsigned int msec, bool hard)
  676. {
  677. unsigned long flags;
  678. if (!dev)
  679. return;
  680. spin_lock_irqsave(&dev->power.lock, flags);
  681. pm_wakeup_ws_event(dev->power.wakeup, msec, hard);
  682. spin_unlock_irqrestore(&dev->power.lock, flags);
  683. }
  684. EXPORT_SYMBOL_GPL(pm_wakeup_dev_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 || atomic_read(&pm_abort_suspend) > 0;
  733. }
  734. void pm_system_wakeup(void)
  735. {
  736. atomic_inc(&pm_abort_suspend);
  737. freeze_wake();
  738. }
  739. EXPORT_SYMBOL_GPL(pm_system_wakeup);
  740. void pm_system_cancel_wakeup(void)
  741. {
  742. atomic_dec(&pm_abort_suspend);
  743. }
  744. void pm_wakeup_clear(bool reset)
  745. {
  746. pm_wakeup_irq = 0;
  747. if (reset)
  748. atomic_set(&pm_abort_suspend, 0);
  749. }
  750. void pm_system_irq_wakeup(unsigned int irq_number)
  751. {
  752. if (pm_wakeup_irq == 0) {
  753. pm_wakeup_irq = irq_number;
  754. pm_system_wakeup();
  755. }
  756. }
  757. /**
  758. * pm_get_wakeup_count - Read the number of registered wakeup events.
  759. * @count: Address to store the value at.
  760. * @block: Whether or not to block.
  761. *
  762. * Store the number of registered wakeup events at the address in @count. If
  763. * @block is set, block until the current number of wakeup events being
  764. * processed is zero.
  765. *
  766. * Return 'false' if the current number of wakeup events being processed is
  767. * nonzero. Otherwise return 'true'.
  768. */
  769. bool pm_get_wakeup_count(unsigned int *count, bool block)
  770. {
  771. unsigned int cnt, inpr;
  772. if (block) {
  773. DEFINE_WAIT(wait);
  774. for (;;) {
  775. prepare_to_wait(&wakeup_count_wait_queue, &wait,
  776. TASK_INTERRUPTIBLE);
  777. split_counters(&cnt, &inpr);
  778. if (inpr == 0 || signal_pending(current))
  779. break;
  780. pm_print_active_wakeup_sources();
  781. schedule();
  782. }
  783. finish_wait(&wakeup_count_wait_queue, &wait);
  784. }
  785. split_counters(&cnt, &inpr);
  786. *count = cnt;
  787. return !inpr;
  788. }
  789. /**
  790. * pm_save_wakeup_count - Save the current number of registered wakeup events.
  791. * @count: Value to compare with the current number of registered wakeup events.
  792. *
  793. * If @count is equal to the current number of registered wakeup events and the
  794. * current number of wakeup events being processed is zero, store @count as the
  795. * old number of registered wakeup events for pm_check_wakeup_events(), enable
  796. * wakeup events detection and return 'true'. Otherwise disable wakeup events
  797. * detection and return 'false'.
  798. */
  799. bool pm_save_wakeup_count(unsigned int count)
  800. {
  801. unsigned int cnt, inpr;
  802. unsigned long flags;
  803. events_check_enabled = false;
  804. spin_lock_irqsave(&events_lock, flags);
  805. split_counters(&cnt, &inpr);
  806. if (cnt == count && inpr == 0) {
  807. saved_count = count;
  808. events_check_enabled = true;
  809. }
  810. spin_unlock_irqrestore(&events_lock, flags);
  811. return events_check_enabled;
  812. }
  813. #ifdef CONFIG_PM_AUTOSLEEP
  814. /**
  815. * pm_wakep_autosleep_enabled - Modify autosleep_enabled for all wakeup sources.
  816. * @enabled: Whether to set or to clear the autosleep_enabled flags.
  817. */
  818. void pm_wakep_autosleep_enabled(bool set)
  819. {
  820. struct wakeup_source *ws;
  821. ktime_t now = ktime_get();
  822. rcu_read_lock();
  823. list_for_each_entry_rcu(ws, &wakeup_sources, entry) {
  824. spin_lock_irq(&ws->lock);
  825. if (ws->autosleep_enabled != set) {
  826. ws->autosleep_enabled = set;
  827. if (ws->active) {
  828. if (set)
  829. ws->start_prevent_time = now;
  830. else
  831. update_prevent_sleep_time(ws, now);
  832. }
  833. }
  834. spin_unlock_irq(&ws->lock);
  835. }
  836. rcu_read_unlock();
  837. }
  838. #endif /* CONFIG_PM_AUTOSLEEP */
  839. static struct dentry *wakeup_sources_stats_dentry;
  840. /**
  841. * print_wakeup_source_stats - Print wakeup source statistics information.
  842. * @m: seq_file to print the statistics into.
  843. * @ws: Wakeup source object to print the statistics for.
  844. */
  845. static int print_wakeup_source_stats(struct seq_file *m,
  846. struct wakeup_source *ws)
  847. {
  848. unsigned long flags;
  849. ktime_t total_time;
  850. ktime_t max_time;
  851. unsigned long active_count;
  852. ktime_t active_time;
  853. ktime_t prevent_sleep_time;
  854. spin_lock_irqsave(&ws->lock, flags);
  855. total_time = ws->total_time;
  856. max_time = ws->max_time;
  857. prevent_sleep_time = ws->prevent_sleep_time;
  858. active_count = ws->active_count;
  859. if (ws->active) {
  860. ktime_t now = ktime_get();
  861. active_time = ktime_sub(now, ws->last_time);
  862. total_time = ktime_add(total_time, active_time);
  863. if (active_time > max_time)
  864. max_time = active_time;
  865. if (ws->autosleep_enabled)
  866. prevent_sleep_time = ktime_add(prevent_sleep_time,
  867. ktime_sub(now, ws->start_prevent_time));
  868. } else {
  869. active_time = 0;
  870. }
  871. 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",
  872. ws->name, active_count, ws->event_count,
  873. ws->wakeup_count, ws->expire_count,
  874. ktime_to_ms(active_time), ktime_to_ms(total_time),
  875. ktime_to_ms(max_time), ktime_to_ms(ws->last_time),
  876. ktime_to_ms(prevent_sleep_time));
  877. spin_unlock_irqrestore(&ws->lock, flags);
  878. return 0;
  879. }
  880. /**
  881. * wakeup_sources_stats_show - Print wakeup sources statistics information.
  882. * @m: seq_file to print the statistics into.
  883. */
  884. static int wakeup_sources_stats_show(struct seq_file *m, void *unused)
  885. {
  886. struct wakeup_source *ws;
  887. seq_puts(m, "name\t\tactive_count\tevent_count\twakeup_count\t"
  888. "expire_count\tactive_since\ttotal_time\tmax_time\t"
  889. "last_change\tprevent_suspend_time\n");
  890. rcu_read_lock();
  891. list_for_each_entry_rcu(ws, &wakeup_sources, entry)
  892. print_wakeup_source_stats(m, ws);
  893. rcu_read_unlock();
  894. print_wakeup_source_stats(m, &deleted_ws);
  895. return 0;
  896. }
  897. static int wakeup_sources_stats_open(struct inode *inode, struct file *file)
  898. {
  899. return single_open(file, wakeup_sources_stats_show, NULL);
  900. }
  901. static const struct file_operations wakeup_sources_stats_fops = {
  902. .owner = THIS_MODULE,
  903. .open = wakeup_sources_stats_open,
  904. .read = seq_read,
  905. .llseek = seq_lseek,
  906. .release = single_release,
  907. };
  908. static int __init wakeup_sources_debugfs_init(void)
  909. {
  910. wakeup_sources_stats_dentry = debugfs_create_file("wakeup_sources",
  911. S_IRUGO, NULL, NULL, &wakeup_sources_stats_fops);
  912. return 0;
  913. }
  914. postcore_initcall(wakeup_sources_debugfs_init);