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.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. 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. if (device_is_registered(dev) && !list_empty(&dev->power.entry)) {
  354. if (capable) {
  355. if (wakeup_sysfs_add(dev))
  356. return;
  357. } else {
  358. wakeup_sysfs_remove(dev);
  359. }
  360. }
  361. dev->power.can_wakeup = capable;
  362. }
  363. EXPORT_SYMBOL_GPL(device_set_wakeup_capable);
  364. /**
  365. * device_init_wakeup - Device wakeup initialization.
  366. * @dev: Device to handle.
  367. * @enable: Whether or not to enable @dev as a wakeup device.
  368. *
  369. * By default, most devices should leave wakeup disabled. The exceptions are
  370. * devices that everyone expects to be wakeup sources: keyboards, power buttons,
  371. * possibly network interfaces, etc. Also, devices that don't generate their
  372. * own wakeup requests but merely forward requests from one bus to another
  373. * (like PCI bridges) should have wakeup enabled by default.
  374. */
  375. int device_init_wakeup(struct device *dev, bool enable)
  376. {
  377. int ret = 0;
  378. if (!dev)
  379. return -EINVAL;
  380. if (enable) {
  381. device_set_wakeup_capable(dev, true);
  382. ret = device_wakeup_enable(dev);
  383. } else {
  384. if (dev->power.can_wakeup)
  385. device_wakeup_disable(dev);
  386. device_set_wakeup_capable(dev, false);
  387. }
  388. return ret;
  389. }
  390. EXPORT_SYMBOL_GPL(device_init_wakeup);
  391. /**
  392. * device_set_wakeup_enable - Enable or disable a device to wake up the system.
  393. * @dev: Device to handle.
  394. */
  395. int device_set_wakeup_enable(struct device *dev, bool enable)
  396. {
  397. if (!dev || !dev->power.can_wakeup)
  398. return -EINVAL;
  399. return enable ? device_wakeup_enable(dev) : device_wakeup_disable(dev);
  400. }
  401. EXPORT_SYMBOL_GPL(device_set_wakeup_enable);
  402. /**
  403. * wakeup_source_not_registered - validate the given wakeup source.
  404. * @ws: Wakeup source to be validated.
  405. */
  406. static bool wakeup_source_not_registered(struct wakeup_source *ws)
  407. {
  408. /*
  409. * Use timer struct to check if the given source is initialized
  410. * by wakeup_source_add.
  411. */
  412. return ws->timer.function != pm_wakeup_timer_fn ||
  413. ws->timer.data != (unsigned long)ws;
  414. }
  415. /*
  416. * The functions below use the observation that each wakeup event starts a
  417. * period in which the system should not be suspended. The moment this period
  418. * will end depends on how the wakeup event is going to be processed after being
  419. * detected and all of the possible cases can be divided into two distinct
  420. * groups.
  421. *
  422. * First, a wakeup event may be detected by the same functional unit that will
  423. * carry out the entire processing of it and possibly will pass it to user space
  424. * for further processing. In that case the functional unit that has detected
  425. * the event may later "close" the "no suspend" period associated with it
  426. * directly as soon as it has been dealt with. The pair of pm_stay_awake() and
  427. * pm_relax(), balanced with each other, is supposed to be used in such
  428. * situations.
  429. *
  430. * Second, a wakeup event may be detected by one functional unit and processed
  431. * by another one. In that case the unit that has detected it cannot really
  432. * "close" the "no suspend" period associated with it, unless it knows in
  433. * advance what's going to happen to the event during processing. This
  434. * knowledge, however, may not be available to it, so it can simply specify time
  435. * to wait before the system can be suspended and pass it as the second
  436. * argument of pm_wakeup_event().
  437. *
  438. * It is valid to call pm_relax() after pm_wakeup_event(), in which case the
  439. * "no suspend" period will be ended either by the pm_relax(), or by the timer
  440. * function executed when the timer expires, whichever comes first.
  441. */
  442. /**
  443. * wakup_source_activate - Mark given wakeup source as active.
  444. * @ws: Wakeup source to handle.
  445. *
  446. * Update the @ws' statistics and, if @ws has just been activated, notify the PM
  447. * core of the event by incrementing the counter of of wakeup events being
  448. * processed.
  449. */
  450. static void wakeup_source_activate(struct wakeup_source *ws)
  451. {
  452. unsigned int cec;
  453. if (WARN_ONCE(wakeup_source_not_registered(ws),
  454. "unregistered wakeup source\n"))
  455. return;
  456. /*
  457. * active wakeup source should bring the system
  458. * out of PM_SUSPEND_FREEZE state
  459. */
  460. freeze_wake();
  461. ws->active = true;
  462. ws->active_count++;
  463. ws->last_time = ktime_get();
  464. if (ws->autosleep_enabled)
  465. ws->start_prevent_time = ws->last_time;
  466. /* Increment the counter of events in progress. */
  467. cec = atomic_inc_return(&combined_event_count);
  468. trace_wakeup_source_activate(ws->name, cec);
  469. }
  470. /**
  471. * wakeup_source_report_event - Report wakeup event using the given source.
  472. * @ws: Wakeup source to report the event for.
  473. */
  474. static void wakeup_source_report_event(struct wakeup_source *ws)
  475. {
  476. ws->event_count++;
  477. /* This is racy, but the counter is approximate anyway. */
  478. if (events_check_enabled)
  479. ws->wakeup_count++;
  480. if (!ws->active)
  481. wakeup_source_activate(ws);
  482. }
  483. /**
  484. * __pm_stay_awake - Notify the PM core of a wakeup event.
  485. * @ws: Wakeup source object associated with the source of the event.
  486. *
  487. * It is safe to call this function from interrupt context.
  488. */
  489. void __pm_stay_awake(struct wakeup_source *ws)
  490. {
  491. unsigned long flags;
  492. if (!ws)
  493. return;
  494. spin_lock_irqsave(&ws->lock, flags);
  495. wakeup_source_report_event(ws);
  496. del_timer(&ws->timer);
  497. ws->timer_expires = 0;
  498. spin_unlock_irqrestore(&ws->lock, flags);
  499. }
  500. EXPORT_SYMBOL_GPL(__pm_stay_awake);
  501. /**
  502. * pm_stay_awake - Notify the PM core that a wakeup event is being processed.
  503. * @dev: Device the wakeup event is related to.
  504. *
  505. * Notify the PM core of a wakeup event (signaled by @dev) by calling
  506. * __pm_stay_awake for the @dev's wakeup source object.
  507. *
  508. * Call this function after detecting of a wakeup event if pm_relax() is going
  509. * to be called directly after processing the event (and possibly passing it to
  510. * user space for further processing).
  511. */
  512. void pm_stay_awake(struct device *dev)
  513. {
  514. unsigned long flags;
  515. if (!dev)
  516. return;
  517. spin_lock_irqsave(&dev->power.lock, flags);
  518. __pm_stay_awake(dev->power.wakeup);
  519. spin_unlock_irqrestore(&dev->power.lock, flags);
  520. }
  521. EXPORT_SYMBOL_GPL(pm_stay_awake);
  522. #ifdef CONFIG_PM_AUTOSLEEP
  523. static void update_prevent_sleep_time(struct wakeup_source *ws, ktime_t now)
  524. {
  525. ktime_t delta = ktime_sub(now, ws->start_prevent_time);
  526. ws->prevent_sleep_time = ktime_add(ws->prevent_sleep_time, delta);
  527. }
  528. #else
  529. static inline void update_prevent_sleep_time(struct wakeup_source *ws,
  530. ktime_t now) {}
  531. #endif
  532. /**
  533. * wakup_source_deactivate - Mark given wakeup source as inactive.
  534. * @ws: Wakeup source to handle.
  535. *
  536. * Update the @ws' statistics and notify the PM core that the wakeup source has
  537. * become inactive by decrementing the counter of wakeup events being processed
  538. * and incrementing the counter of registered wakeup events.
  539. */
  540. static void wakeup_source_deactivate(struct wakeup_source *ws)
  541. {
  542. unsigned int cnt, inpr, cec;
  543. ktime_t duration;
  544. ktime_t now;
  545. ws->relax_count++;
  546. /*
  547. * __pm_relax() may be called directly or from a timer function.
  548. * If it is called directly right after the timer function has been
  549. * started, but before the timer function calls __pm_relax(), it is
  550. * possible that __pm_stay_awake() will be called in the meantime and
  551. * will set ws->active. Then, ws->active may be cleared immediately
  552. * by the __pm_relax() called from the timer function, but in such a
  553. * case ws->relax_count will be different from ws->active_count.
  554. */
  555. if (ws->relax_count != ws->active_count) {
  556. ws->relax_count--;
  557. return;
  558. }
  559. ws->active = false;
  560. now = ktime_get();
  561. duration = ktime_sub(now, ws->last_time);
  562. ws->total_time = ktime_add(ws->total_time, duration);
  563. if (ktime_to_ns(duration) > ktime_to_ns(ws->max_time))
  564. ws->max_time = duration;
  565. ws->last_time = now;
  566. del_timer(&ws->timer);
  567. ws->timer_expires = 0;
  568. if (ws->autosleep_enabled)
  569. update_prevent_sleep_time(ws, now);
  570. /*
  571. * Increment the counter of registered wakeup events and decrement the
  572. * couter of wakeup events in progress simultaneously.
  573. */
  574. cec = atomic_add_return(MAX_IN_PROGRESS, &combined_event_count);
  575. trace_wakeup_source_deactivate(ws->name, cec);
  576. split_counters(&cnt, &inpr);
  577. if (!inpr && waitqueue_active(&wakeup_count_wait_queue))
  578. wake_up(&wakeup_count_wait_queue);
  579. }
  580. /**
  581. * __pm_relax - Notify the PM core that processing of a wakeup event has ended.
  582. * @ws: Wakeup source object associated with the source of the event.
  583. *
  584. * Call this function for wakeup events whose processing started with calling
  585. * __pm_stay_awake().
  586. *
  587. * It is safe to call it from interrupt context.
  588. */
  589. void __pm_relax(struct wakeup_source *ws)
  590. {
  591. unsigned long flags;
  592. if (!ws)
  593. return;
  594. spin_lock_irqsave(&ws->lock, flags);
  595. if (ws->active)
  596. wakeup_source_deactivate(ws);
  597. spin_unlock_irqrestore(&ws->lock, flags);
  598. }
  599. EXPORT_SYMBOL_GPL(__pm_relax);
  600. /**
  601. * pm_relax - Notify the PM core that processing of a wakeup event has ended.
  602. * @dev: Device that signaled the event.
  603. *
  604. * Execute __pm_relax() for the @dev's wakeup source object.
  605. */
  606. void pm_relax(struct device *dev)
  607. {
  608. unsigned long flags;
  609. if (!dev)
  610. return;
  611. spin_lock_irqsave(&dev->power.lock, flags);
  612. __pm_relax(dev->power.wakeup);
  613. spin_unlock_irqrestore(&dev->power.lock, flags);
  614. }
  615. EXPORT_SYMBOL_GPL(pm_relax);
  616. /**
  617. * pm_wakeup_timer_fn - Delayed finalization of a wakeup event.
  618. * @data: Address of the wakeup source object associated with the event source.
  619. *
  620. * Call wakeup_source_deactivate() for the wakeup source whose address is stored
  621. * in @data if it is currently active and its timer has not been canceled and
  622. * the expiration time of the timer is not in future.
  623. */
  624. static void pm_wakeup_timer_fn(unsigned long data)
  625. {
  626. struct wakeup_source *ws = (struct wakeup_source *)data;
  627. unsigned long flags;
  628. spin_lock_irqsave(&ws->lock, flags);
  629. if (ws->active && ws->timer_expires
  630. && time_after_eq(jiffies, ws->timer_expires)) {
  631. wakeup_source_deactivate(ws);
  632. ws->expire_count++;
  633. }
  634. spin_unlock_irqrestore(&ws->lock, flags);
  635. }
  636. /**
  637. * __pm_wakeup_event - Notify the PM core of a wakeup event.
  638. * @ws: Wakeup source object associated with the event source.
  639. * @msec: Anticipated event processing time (in milliseconds).
  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_event(struct wakeup_source *ws, unsigned int msec)
  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);
  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_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. *
  676. * Call __pm_wakeup_event() for the @dev's wakeup source object.
  677. */
  678. void pm_wakeup_event(struct device *dev, unsigned int msec)
  679. {
  680. unsigned long flags;
  681. if (!dev)
  682. return;
  683. spin_lock_irqsave(&dev->power.lock, flags);
  684. __pm_wakeup_event(dev->power.wakeup, msec);
  685. spin_unlock_irqrestore(&dev->power.lock, flags);
  686. }
  687. EXPORT_SYMBOL_GPL(pm_wakeup_event);
  688. void pm_print_active_wakeup_sources(void)
  689. {
  690. struct wakeup_source *ws;
  691. int srcuidx, active = 0;
  692. struct wakeup_source *last_activity_ws = NULL;
  693. srcuidx = srcu_read_lock(&wakeup_srcu);
  694. list_for_each_entry_rcu(ws, &wakeup_sources, entry) {
  695. if (ws->active) {
  696. pr_info("active wakeup source: %s\n", ws->name);
  697. active = 1;
  698. } else if (!active &&
  699. (!last_activity_ws ||
  700. ktime_to_ns(ws->last_time) >
  701. ktime_to_ns(last_activity_ws->last_time))) {
  702. last_activity_ws = ws;
  703. }
  704. }
  705. if (!active && last_activity_ws)
  706. pr_info("last active wakeup source: %s\n",
  707. last_activity_ws->name);
  708. srcu_read_unlock(&wakeup_srcu, srcuidx);
  709. }
  710. EXPORT_SYMBOL_GPL(pm_print_active_wakeup_sources);
  711. /**
  712. * pm_wakeup_pending - Check if power transition in progress should be aborted.
  713. *
  714. * Compare the current number of registered wakeup events with its preserved
  715. * value from the past and return true if new wakeup events have been registered
  716. * since the old value was stored. Also return true if the current number of
  717. * wakeup events being processed is different from zero.
  718. */
  719. bool pm_wakeup_pending(void)
  720. {
  721. unsigned long flags;
  722. bool ret = false;
  723. spin_lock_irqsave(&events_lock, flags);
  724. if (events_check_enabled) {
  725. unsigned int cnt, inpr;
  726. split_counters(&cnt, &inpr);
  727. ret = (cnt != saved_count || inpr > 0);
  728. events_check_enabled = !ret;
  729. }
  730. spin_unlock_irqrestore(&events_lock, flags);
  731. if (ret) {
  732. pr_info("PM: Wakeup pending, aborting suspend\n");
  733. pm_print_active_wakeup_sources();
  734. }
  735. return ret || pm_abort_suspend;
  736. }
  737. void pm_system_wakeup(void)
  738. {
  739. pm_abort_suspend = true;
  740. freeze_wake();
  741. }
  742. EXPORT_SYMBOL_GPL(pm_system_wakeup);
  743. void pm_wakeup_clear(void)
  744. {
  745. pm_abort_suspend = false;
  746. pm_wakeup_irq = 0;
  747. }
  748. void pm_system_irq_wakeup(unsigned int irq_number)
  749. {
  750. if (pm_wakeup_irq == 0) {
  751. pm_wakeup_irq = irq_number;
  752. pm_system_wakeup();
  753. }
  754. }
  755. /**
  756. * pm_get_wakeup_count - Read the number of registered wakeup events.
  757. * @count: Address to store the value at.
  758. * @block: Whether or not to block.
  759. *
  760. * Store the number of registered wakeup events at the address in @count. If
  761. * @block is set, block until the current number of wakeup events being
  762. * processed is zero.
  763. *
  764. * Return 'false' if the current number of wakeup events being processed is
  765. * nonzero. Otherwise return 'true'.
  766. */
  767. bool pm_get_wakeup_count(unsigned int *count, bool block)
  768. {
  769. unsigned int cnt, inpr;
  770. if (block) {
  771. DEFINE_WAIT(wait);
  772. for (;;) {
  773. prepare_to_wait(&wakeup_count_wait_queue, &wait,
  774. TASK_INTERRUPTIBLE);
  775. split_counters(&cnt, &inpr);
  776. if (inpr == 0 || signal_pending(current))
  777. break;
  778. schedule();
  779. }
  780. finish_wait(&wakeup_count_wait_queue, &wait);
  781. }
  782. split_counters(&cnt, &inpr);
  783. *count = cnt;
  784. return !inpr;
  785. }
  786. /**
  787. * pm_save_wakeup_count - Save the current number of registered wakeup events.
  788. * @count: Value to compare with the current number of registered wakeup events.
  789. *
  790. * If @count is equal to the current number of registered wakeup events and the
  791. * current number of wakeup events being processed is zero, store @count as the
  792. * old number of registered wakeup events for pm_check_wakeup_events(), enable
  793. * wakeup events detection and return 'true'. Otherwise disable wakeup events
  794. * detection and return 'false'.
  795. */
  796. bool pm_save_wakeup_count(unsigned int count)
  797. {
  798. unsigned int cnt, inpr;
  799. unsigned long flags;
  800. events_check_enabled = false;
  801. spin_lock_irqsave(&events_lock, flags);
  802. split_counters(&cnt, &inpr);
  803. if (cnt == count && inpr == 0) {
  804. saved_count = count;
  805. events_check_enabled = true;
  806. }
  807. spin_unlock_irqrestore(&events_lock, flags);
  808. return events_check_enabled;
  809. }
  810. #ifdef CONFIG_PM_AUTOSLEEP
  811. /**
  812. * pm_wakep_autosleep_enabled - Modify autosleep_enabled for all wakeup sources.
  813. * @enabled: Whether to set or to clear the autosleep_enabled flags.
  814. */
  815. void pm_wakep_autosleep_enabled(bool set)
  816. {
  817. struct wakeup_source *ws;
  818. ktime_t now = ktime_get();
  819. int srcuidx;
  820. srcuidx = srcu_read_lock(&wakeup_srcu);
  821. list_for_each_entry_rcu(ws, &wakeup_sources, entry) {
  822. spin_lock_irq(&ws->lock);
  823. if (ws->autosleep_enabled != set) {
  824. ws->autosleep_enabled = set;
  825. if (ws->active) {
  826. if (set)
  827. ws->start_prevent_time = now;
  828. else
  829. update_prevent_sleep_time(ws, now);
  830. }
  831. }
  832. spin_unlock_irq(&ws->lock);
  833. }
  834. srcu_read_unlock(&wakeup_srcu, srcuidx);
  835. }
  836. #endif /* CONFIG_PM_AUTOSLEEP */
  837. static struct dentry *wakeup_sources_stats_dentry;
  838. /**
  839. * print_wakeup_source_stats - Print wakeup source statistics information.
  840. * @m: seq_file to print the statistics into.
  841. * @ws: Wakeup source object to print the statistics for.
  842. */
  843. static int print_wakeup_source_stats(struct seq_file *m,
  844. struct wakeup_source *ws)
  845. {
  846. unsigned long flags;
  847. ktime_t total_time;
  848. ktime_t max_time;
  849. unsigned long active_count;
  850. ktime_t active_time;
  851. ktime_t prevent_sleep_time;
  852. spin_lock_irqsave(&ws->lock, flags);
  853. total_time = ws->total_time;
  854. max_time = ws->max_time;
  855. prevent_sleep_time = ws->prevent_sleep_time;
  856. active_count = ws->active_count;
  857. if (ws->active) {
  858. ktime_t now = ktime_get();
  859. active_time = ktime_sub(now, ws->last_time);
  860. total_time = ktime_add(total_time, active_time);
  861. if (active_time.tv64 > max_time.tv64)
  862. max_time = active_time;
  863. if (ws->autosleep_enabled)
  864. prevent_sleep_time = ktime_add(prevent_sleep_time,
  865. ktime_sub(now, ws->start_prevent_time));
  866. } else {
  867. active_time = ktime_set(0, 0);
  868. }
  869. 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",
  870. ws->name, active_count, ws->event_count,
  871. ws->wakeup_count, ws->expire_count,
  872. ktime_to_ms(active_time), ktime_to_ms(total_time),
  873. ktime_to_ms(max_time), ktime_to_ms(ws->last_time),
  874. ktime_to_ms(prevent_sleep_time));
  875. spin_unlock_irqrestore(&ws->lock, flags);
  876. return 0;
  877. }
  878. /**
  879. * wakeup_sources_stats_show - Print wakeup sources statistics information.
  880. * @m: seq_file to print the statistics into.
  881. */
  882. static int wakeup_sources_stats_show(struct seq_file *m, void *unused)
  883. {
  884. struct wakeup_source *ws;
  885. int srcuidx;
  886. seq_puts(m, "name\t\tactive_count\tevent_count\twakeup_count\t"
  887. "expire_count\tactive_since\ttotal_time\tmax_time\t"
  888. "last_change\tprevent_suspend_time\n");
  889. srcuidx = srcu_read_lock(&wakeup_srcu);
  890. list_for_each_entry_rcu(ws, &wakeup_sources, entry)
  891. print_wakeup_source_stats(m, ws);
  892. srcu_read_unlock(&wakeup_srcu, srcuidx);
  893. print_wakeup_source_stats(m, &deleted_ws);
  894. return 0;
  895. }
  896. static int wakeup_sources_stats_open(struct inode *inode, struct file *file)
  897. {
  898. return single_open(file, wakeup_sources_stats_show, NULL);
  899. }
  900. static const struct file_operations wakeup_sources_stats_fops = {
  901. .owner = THIS_MODULE,
  902. .open = wakeup_sources_stats_open,
  903. .read = seq_read,
  904. .llseek = seq_lseek,
  905. .release = single_release,
  906. };
  907. static int __init wakeup_sources_debugfs_init(void)
  908. {
  909. wakeup_sources_stats_dentry = debugfs_create_file("wakeup_sources",
  910. S_IRUGO, NULL, NULL, &wakeup_sources_stats_fops);
  911. return 0;
  912. }
  913. postcore_initcall(wakeup_sources_debugfs_init);