wakeup.c 29 KB

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