wakeup.c 28 KB

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