wakeup.c 33 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * drivers/base/power/wakeup.c - System wakeup events framework
  4. *
  5. * Copyright (c) 2010 Rafael J. Wysocki <rjw@sisk.pl>, Novell Inc.
  6. */
  7. #define pr_fmt(fmt) "PM: " fmt
  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 <linux/irq.h>
  18. #include <linux/irqdesc.h>
  19. #include <linux/wakeup_reason.h>
  20. #include <trace/events/power.h>
  21. #include "power.h"
  22. #ifndef CONFIG_SUSPEND
  23. suspend_state_t pm_suspend_target_state;
  24. #define pm_suspend_target_state (PM_SUSPEND_ON)
  25. #endif
  26. #define list_for_each_entry_rcu_locked(pos, head, member) \
  27. list_for_each_entry_rcu(pos, head, member, \
  28. srcu_read_lock_held(&wakeup_srcu))
  29. /*
  30. * If set, the suspend/hibernate code will abort transitions to a sleep state
  31. * if wakeup events are registered during or immediately before the transition.
  32. */
  33. bool events_check_enabled __read_mostly;
  34. /* First wakeup IRQ seen by the kernel in the last cycle. */
  35. static unsigned int wakeup_irq[2] __read_mostly;
  36. static DEFINE_RAW_SPINLOCK(wakeup_irq_lock);
  37. /* If greater than 0 and the system is suspending, terminate the suspend. */
  38. static atomic_t pm_abort_suspend __read_mostly;
  39. /*
  40. * Combined counters of registered wakeup events and wakeup events in progress.
  41. * They need to be modified together atomically, so it's better to use one
  42. * atomic variable to hold them both.
  43. */
  44. static atomic_t combined_event_count = ATOMIC_INIT(0);
  45. #define IN_PROGRESS_BITS (sizeof(int) * 4)
  46. #define MAX_IN_PROGRESS ((1 << IN_PROGRESS_BITS) - 1)
  47. static void split_counters(unsigned int *cnt, unsigned int *inpr)
  48. {
  49. unsigned int comb = atomic_read(&combined_event_count);
  50. *cnt = (comb >> IN_PROGRESS_BITS);
  51. *inpr = comb & MAX_IN_PROGRESS;
  52. }
  53. /* A preserved old value of the events counter. */
  54. static unsigned int saved_count;
  55. static DEFINE_RAW_SPINLOCK(events_lock);
  56. static void pm_wakeup_timer_fn(struct timer_list *t);
  57. static LIST_HEAD(wakeup_sources);
  58. static DECLARE_WAIT_QUEUE_HEAD(wakeup_count_wait_queue);
  59. DEFINE_STATIC_SRCU(wakeup_srcu);
  60. static struct wakeup_source deleted_ws = {
  61. .name = "deleted",
  62. .lock = __SPIN_LOCK_UNLOCKED(deleted_ws.lock),
  63. };
  64. static DEFINE_IDA(wakeup_ida);
  65. /**
  66. * wakeup_source_create - Create a struct wakeup_source object.
  67. * @name: Name of the new wakeup source.
  68. */
  69. struct wakeup_source *wakeup_source_create(const char *name)
  70. {
  71. struct wakeup_source *ws;
  72. const char *ws_name;
  73. int id;
  74. ws = kzalloc(sizeof(*ws), GFP_KERNEL);
  75. if (!ws)
  76. goto err_ws;
  77. ws_name = kstrdup_const(name, GFP_KERNEL);
  78. if (!ws_name)
  79. goto err_name;
  80. ws->name = ws_name;
  81. id = ida_alloc(&wakeup_ida, GFP_KERNEL);
  82. if (id < 0)
  83. goto err_id;
  84. ws->id = id;
  85. return ws;
  86. err_id:
  87. kfree_const(ws->name);
  88. err_name:
  89. kfree(ws);
  90. err_ws:
  91. return NULL;
  92. }
  93. EXPORT_SYMBOL_GPL(wakeup_source_create);
  94. /*
  95. * Record wakeup_source statistics being deleted into a dummy wakeup_source.
  96. */
  97. static void wakeup_source_record(struct wakeup_source *ws)
  98. {
  99. unsigned long flags;
  100. spin_lock_irqsave(&deleted_ws.lock, flags);
  101. if (ws->event_count) {
  102. deleted_ws.total_time =
  103. ktime_add(deleted_ws.total_time, ws->total_time);
  104. deleted_ws.prevent_sleep_time =
  105. ktime_add(deleted_ws.prevent_sleep_time,
  106. ws->prevent_sleep_time);
  107. deleted_ws.max_time =
  108. ktime_compare(deleted_ws.max_time, ws->max_time) > 0 ?
  109. deleted_ws.max_time : ws->max_time;
  110. deleted_ws.event_count += ws->event_count;
  111. deleted_ws.active_count += ws->active_count;
  112. deleted_ws.relax_count += ws->relax_count;
  113. deleted_ws.expire_count += ws->expire_count;
  114. deleted_ws.wakeup_count += ws->wakeup_count;
  115. }
  116. spin_unlock_irqrestore(&deleted_ws.lock, flags);
  117. }
  118. static void wakeup_source_free(struct wakeup_source *ws)
  119. {
  120. ida_free(&wakeup_ida, ws->id);
  121. kfree_const(ws->name);
  122. kfree(ws);
  123. }
  124. /**
  125. * wakeup_source_destroy - Destroy a struct wakeup_source object.
  126. * @ws: Wakeup source to destroy.
  127. *
  128. * Use only for wakeup source objects created with wakeup_source_create().
  129. */
  130. void wakeup_source_destroy(struct wakeup_source *ws)
  131. {
  132. if (!ws)
  133. return;
  134. __pm_relax(ws);
  135. wakeup_source_record(ws);
  136. wakeup_source_free(ws);
  137. }
  138. EXPORT_SYMBOL_GPL(wakeup_source_destroy);
  139. /**
  140. * wakeup_source_add - Add given object to the list of wakeup sources.
  141. * @ws: Wakeup source object to add to the list.
  142. */
  143. void wakeup_source_add(struct wakeup_source *ws)
  144. {
  145. unsigned long flags;
  146. if (WARN_ON(!ws))
  147. return;
  148. spin_lock_init(&ws->lock);
  149. timer_setup(&ws->timer, pm_wakeup_timer_fn, 0);
  150. ws->active = false;
  151. raw_spin_lock_irqsave(&events_lock, flags);
  152. list_add_rcu(&ws->entry, &wakeup_sources);
  153. raw_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. raw_spin_lock_irqsave(&events_lock, flags);
  166. list_del_rcu(&ws->entry);
  167. raw_spin_unlock_irqrestore(&events_lock, flags);
  168. synchronize_srcu(&wakeup_srcu);
  169. del_timer_sync(&ws->timer);
  170. /*
  171. * Clear timer.function to make wakeup_source_not_registered() treat
  172. * this wakeup source as not registered.
  173. */
  174. ws->timer.function = NULL;
  175. }
  176. EXPORT_SYMBOL_GPL(wakeup_source_remove);
  177. /**
  178. * wakeup_source_register - Create wakeup source and add it to the list.
  179. * @dev: Device this wakeup source is associated with (or NULL if virtual).
  180. * @name: Name of the wakeup source to register.
  181. */
  182. struct wakeup_source *wakeup_source_register(struct device *dev,
  183. const char *name)
  184. {
  185. struct wakeup_source *ws;
  186. int ret;
  187. ws = wakeup_source_create(name);
  188. if (ws) {
  189. if (!dev || device_is_registered(dev)) {
  190. ret = wakeup_source_sysfs_add(dev, ws);
  191. if (ret) {
  192. wakeup_source_free(ws);
  193. return NULL;
  194. }
  195. }
  196. wakeup_source_add(ws);
  197. }
  198. return ws;
  199. }
  200. EXPORT_SYMBOL_GPL(wakeup_source_register);
  201. /**
  202. * wakeup_source_unregister - Remove wakeup source from the list and remove it.
  203. * @ws: Wakeup source object to unregister.
  204. */
  205. void wakeup_source_unregister(struct wakeup_source *ws)
  206. {
  207. if (ws) {
  208. wakeup_source_remove(ws);
  209. if (ws->dev)
  210. wakeup_source_sysfs_remove(ws);
  211. wakeup_source_destroy(ws);
  212. }
  213. }
  214. EXPORT_SYMBOL_GPL(wakeup_source_unregister);
  215. /**
  216. * wakeup_sources_read_lock - Lock wakeup source list for read.
  217. *
  218. * Returns an index of srcu lock for struct wakeup_srcu.
  219. * This index must be passed to the matching wakeup_sources_read_unlock().
  220. */
  221. int wakeup_sources_read_lock(void)
  222. {
  223. return srcu_read_lock(&wakeup_srcu);
  224. }
  225. EXPORT_SYMBOL_GPL(wakeup_sources_read_lock);
  226. /**
  227. * wakeup_sources_read_unlock - Unlock wakeup source list.
  228. * @idx: return value from corresponding wakeup_sources_read_lock()
  229. */
  230. void wakeup_sources_read_unlock(int idx)
  231. {
  232. srcu_read_unlock(&wakeup_srcu, idx);
  233. }
  234. EXPORT_SYMBOL_GPL(wakeup_sources_read_unlock);
  235. /**
  236. * wakeup_sources_walk_start - Begin a walk on wakeup source list
  237. *
  238. * Returns first object of the list of wakeup sources.
  239. *
  240. * Note that to be safe, wakeup sources list needs to be locked by calling
  241. * wakeup_source_read_lock() for this.
  242. */
  243. struct wakeup_source *wakeup_sources_walk_start(void)
  244. {
  245. struct list_head *ws_head = &wakeup_sources;
  246. return list_entry_rcu(ws_head->next, struct wakeup_source, entry);
  247. }
  248. EXPORT_SYMBOL_GPL(wakeup_sources_walk_start);
  249. /**
  250. * wakeup_sources_walk_next - Get next wakeup source from the list
  251. * @ws: Previous wakeup source object
  252. *
  253. * Note that to be safe, wakeup sources list needs to be locked by calling
  254. * wakeup_source_read_lock() for this.
  255. */
  256. struct wakeup_source *wakeup_sources_walk_next(struct wakeup_source *ws)
  257. {
  258. struct list_head *ws_head = &wakeup_sources;
  259. return list_next_or_null_rcu(ws_head, &ws->entry,
  260. struct wakeup_source, entry);
  261. }
  262. EXPORT_SYMBOL_GPL(wakeup_sources_walk_next);
  263. /**
  264. * device_wakeup_attach - Attach a wakeup source object to a device object.
  265. * @dev: Device to handle.
  266. * @ws: Wakeup source object to attach to @dev.
  267. *
  268. * This causes @dev to be treated as a wakeup device.
  269. */
  270. static int device_wakeup_attach(struct device *dev, struct wakeup_source *ws)
  271. {
  272. spin_lock_irq(&dev->power.lock);
  273. if (dev->power.wakeup) {
  274. spin_unlock_irq(&dev->power.lock);
  275. return -EEXIST;
  276. }
  277. dev->power.wakeup = ws;
  278. if (dev->power.wakeirq)
  279. device_wakeup_attach_irq(dev, dev->power.wakeirq);
  280. spin_unlock_irq(&dev->power.lock);
  281. return 0;
  282. }
  283. /**
  284. * device_wakeup_enable - Enable given device to be a wakeup source.
  285. * @dev: Device to handle.
  286. *
  287. * Create a wakeup source object, register it and attach it to @dev.
  288. */
  289. int device_wakeup_enable(struct device *dev)
  290. {
  291. struct wakeup_source *ws;
  292. int ret;
  293. if (!dev || !dev->power.can_wakeup)
  294. return -EINVAL;
  295. if (pm_suspend_target_state != PM_SUSPEND_ON)
  296. dev_dbg(dev, "Suspicious %s() during system transition!\n", __func__);
  297. ws = wakeup_source_register(dev, dev_name(dev));
  298. if (!ws)
  299. return -ENOMEM;
  300. ret = device_wakeup_attach(dev, ws);
  301. if (ret)
  302. wakeup_source_unregister(ws);
  303. return ret;
  304. }
  305. EXPORT_SYMBOL_GPL(device_wakeup_enable);
  306. /**
  307. * device_wakeup_attach_irq - Attach a wakeirq to a wakeup source
  308. * @dev: Device to handle
  309. * @wakeirq: Device specific wakeirq entry
  310. *
  311. * Attach a device wakeirq to the wakeup source so the device
  312. * wake IRQ can be configured automatically for suspend and
  313. * resume.
  314. *
  315. * Call under the device's power.lock lock.
  316. */
  317. void device_wakeup_attach_irq(struct device *dev,
  318. struct wake_irq *wakeirq)
  319. {
  320. struct wakeup_source *ws;
  321. ws = dev->power.wakeup;
  322. if (!ws)
  323. return;
  324. if (ws->wakeirq)
  325. dev_err(dev, "Leftover wakeup IRQ found, overriding\n");
  326. ws->wakeirq = wakeirq;
  327. }
  328. /**
  329. * device_wakeup_detach_irq - Detach a wakeirq from a wakeup source
  330. * @dev: Device to handle
  331. *
  332. * Removes a device wakeirq from the wakeup source.
  333. *
  334. * Call under the device's power.lock lock.
  335. */
  336. void device_wakeup_detach_irq(struct device *dev)
  337. {
  338. struct wakeup_source *ws;
  339. ws = dev->power.wakeup;
  340. if (ws)
  341. ws->wakeirq = NULL;
  342. }
  343. /**
  344. * device_wakeup_arm_wake_irqs(void)
  345. *
  346. * Itereates over the list of device wakeirqs to arm them.
  347. */
  348. void device_wakeup_arm_wake_irqs(void)
  349. {
  350. struct wakeup_source *ws;
  351. int srcuidx;
  352. srcuidx = srcu_read_lock(&wakeup_srcu);
  353. list_for_each_entry_rcu_locked(ws, &wakeup_sources, entry)
  354. dev_pm_arm_wake_irq(ws->wakeirq);
  355. srcu_read_unlock(&wakeup_srcu, srcuidx);
  356. }
  357. /**
  358. * device_wakeup_disarm_wake_irqs(void)
  359. *
  360. * Itereates over the list of device wakeirqs to disarm them.
  361. */
  362. void device_wakeup_disarm_wake_irqs(void)
  363. {
  364. struct wakeup_source *ws;
  365. int srcuidx;
  366. srcuidx = srcu_read_lock(&wakeup_srcu);
  367. list_for_each_entry_rcu_locked(ws, &wakeup_sources, entry)
  368. dev_pm_disarm_wake_irq(ws->wakeirq);
  369. srcu_read_unlock(&wakeup_srcu, srcuidx);
  370. }
  371. /**
  372. * device_wakeup_detach - Detach a device's wakeup source object from it.
  373. * @dev: Device to detach the wakeup source object from.
  374. *
  375. * After it returns, @dev will not be treated as a wakeup device any more.
  376. */
  377. static struct wakeup_source *device_wakeup_detach(struct device *dev)
  378. {
  379. struct wakeup_source *ws;
  380. spin_lock_irq(&dev->power.lock);
  381. ws = dev->power.wakeup;
  382. dev->power.wakeup = NULL;
  383. spin_unlock_irq(&dev->power.lock);
  384. return ws;
  385. }
  386. /**
  387. * device_wakeup_disable - Do not regard a device as a wakeup source any more.
  388. * @dev: Device to handle.
  389. *
  390. * Detach the @dev's wakeup source object from it, unregister this wakeup source
  391. * object and destroy it.
  392. */
  393. int device_wakeup_disable(struct device *dev)
  394. {
  395. struct wakeup_source *ws;
  396. if (!dev || !dev->power.can_wakeup)
  397. return -EINVAL;
  398. ws = device_wakeup_detach(dev);
  399. wakeup_source_unregister(ws);
  400. return 0;
  401. }
  402. EXPORT_SYMBOL_GPL(device_wakeup_disable);
  403. /**
  404. * device_set_wakeup_capable - Set/reset device wakeup capability flag.
  405. * @dev: Device to handle.
  406. * @capable: Whether or not @dev is capable of waking up the system from sleep.
  407. *
  408. * If @capable is set, set the @dev's power.can_wakeup flag and add its
  409. * wakeup-related attributes to sysfs. Otherwise, unset the @dev's
  410. * power.can_wakeup flag and remove its wakeup-related attributes from sysfs.
  411. *
  412. * This function may sleep and it can't be called from any context where
  413. * sleeping is not allowed.
  414. */
  415. void device_set_wakeup_capable(struct device *dev, bool capable)
  416. {
  417. if (!!dev->power.can_wakeup == !!capable)
  418. return;
  419. dev->power.can_wakeup = capable;
  420. if (device_is_registered(dev) && !list_empty(&dev->power.entry)) {
  421. if (capable) {
  422. int ret = wakeup_sysfs_add(dev);
  423. if (ret)
  424. dev_info(dev, "Wakeup sysfs attributes not added\n");
  425. } else {
  426. wakeup_sysfs_remove(dev);
  427. }
  428. }
  429. }
  430. EXPORT_SYMBOL_GPL(device_set_wakeup_capable);
  431. /**
  432. * device_init_wakeup - Device wakeup initialization.
  433. * @dev: Device to handle.
  434. * @enable: Whether or not to enable @dev as a wakeup device.
  435. *
  436. * By default, most devices should leave wakeup disabled. The exceptions are
  437. * devices that everyone expects to be wakeup sources: keyboards, power buttons,
  438. * possibly network interfaces, etc. Also, devices that don't generate their
  439. * own wakeup requests but merely forward requests from one bus to another
  440. * (like PCI bridges) should have wakeup enabled by default.
  441. */
  442. int device_init_wakeup(struct device *dev, bool enable)
  443. {
  444. int ret = 0;
  445. if (!dev)
  446. return -EINVAL;
  447. if (enable) {
  448. device_set_wakeup_capable(dev, true);
  449. ret = device_wakeup_enable(dev);
  450. } else {
  451. device_wakeup_disable(dev);
  452. device_set_wakeup_capable(dev, false);
  453. }
  454. return ret;
  455. }
  456. EXPORT_SYMBOL_GPL(device_init_wakeup);
  457. /**
  458. * device_set_wakeup_enable - Enable or disable a device to wake up the system.
  459. * @dev: Device to handle.
  460. */
  461. int device_set_wakeup_enable(struct device *dev, bool enable)
  462. {
  463. return enable ? device_wakeup_enable(dev) : device_wakeup_disable(dev);
  464. }
  465. EXPORT_SYMBOL_GPL(device_set_wakeup_enable);
  466. /**
  467. * wakeup_source_not_registered - validate the given wakeup source.
  468. * @ws: Wakeup source to be validated.
  469. */
  470. static bool wakeup_source_not_registered(struct wakeup_source *ws)
  471. {
  472. /*
  473. * Use timer struct to check if the given source is initialized
  474. * by wakeup_source_add.
  475. */
  476. return ws->timer.function != pm_wakeup_timer_fn;
  477. }
  478. /*
  479. * The functions below use the observation that each wakeup event starts a
  480. * period in which the system should not be suspended. The moment this period
  481. * will end depends on how the wakeup event is going to be processed after being
  482. * detected and all of the possible cases can be divided into two distinct
  483. * groups.
  484. *
  485. * First, a wakeup event may be detected by the same functional unit that will
  486. * carry out the entire processing of it and possibly will pass it to user space
  487. * for further processing. In that case the functional unit that has detected
  488. * the event may later "close" the "no suspend" period associated with it
  489. * directly as soon as it has been dealt with. The pair of pm_stay_awake() and
  490. * pm_relax(), balanced with each other, is supposed to be used in such
  491. * situations.
  492. *
  493. * Second, a wakeup event may be detected by one functional unit and processed
  494. * by another one. In that case the unit that has detected it cannot really
  495. * "close" the "no suspend" period associated with it, unless it knows in
  496. * advance what's going to happen to the event during processing. This
  497. * knowledge, however, may not be available to it, so it can simply specify time
  498. * to wait before the system can be suspended and pass it as the second
  499. * argument of pm_wakeup_event().
  500. *
  501. * It is valid to call pm_relax() after pm_wakeup_event(), in which case the
  502. * "no suspend" period will be ended either by the pm_relax(), or by the timer
  503. * function executed when the timer expires, whichever comes first.
  504. */
  505. /**
  506. * wakup_source_activate - Mark given wakeup source as active.
  507. * @ws: Wakeup source to handle.
  508. *
  509. * Update the @ws' statistics and, if @ws has just been activated, notify the PM
  510. * core of the event by incrementing the counter of of wakeup events being
  511. * processed.
  512. */
  513. static void wakeup_source_activate(struct wakeup_source *ws)
  514. {
  515. unsigned int cec;
  516. if (WARN_ONCE(wakeup_source_not_registered(ws),
  517. "unregistered wakeup source\n"))
  518. return;
  519. ws->active = true;
  520. ws->active_count++;
  521. ws->last_time = ktime_get();
  522. if (ws->autosleep_enabled)
  523. ws->start_prevent_time = ws->last_time;
  524. /* Increment the counter of events in progress. */
  525. cec = atomic_inc_return(&combined_event_count);
  526. trace_wakeup_source_activate(ws->name, cec);
  527. }
  528. /**
  529. * wakeup_source_report_event - Report wakeup event using the given source.
  530. * @ws: Wakeup source to report the event for.
  531. * @hard: If set, abort suspends in progress and wake up from suspend-to-idle.
  532. */
  533. static void wakeup_source_report_event(struct wakeup_source *ws, bool hard)
  534. {
  535. ws->event_count++;
  536. /* This is racy, but the counter is approximate anyway. */
  537. if (events_check_enabled)
  538. ws->wakeup_count++;
  539. if (!ws->active)
  540. wakeup_source_activate(ws);
  541. if (hard)
  542. pm_system_wakeup();
  543. }
  544. /**
  545. * __pm_stay_awake - Notify the PM core of a wakeup event.
  546. * @ws: Wakeup source object associated with the source of the event.
  547. *
  548. * It is safe to call this function from interrupt context.
  549. */
  550. void __pm_stay_awake(struct wakeup_source *ws)
  551. {
  552. unsigned long flags;
  553. if (!ws)
  554. return;
  555. spin_lock_irqsave(&ws->lock, flags);
  556. wakeup_source_report_event(ws, false);
  557. del_timer(&ws->timer);
  558. ws->timer_expires = 0;
  559. spin_unlock_irqrestore(&ws->lock, flags);
  560. }
  561. EXPORT_SYMBOL_GPL(__pm_stay_awake);
  562. /**
  563. * pm_stay_awake - Notify the PM core that a wakeup event is being processed.
  564. * @dev: Device the wakeup event is related to.
  565. *
  566. * Notify the PM core of a wakeup event (signaled by @dev) by calling
  567. * __pm_stay_awake for the @dev's wakeup source object.
  568. *
  569. * Call this function after detecting of a wakeup event if pm_relax() is going
  570. * to be called directly after processing the event (and possibly passing it to
  571. * user space for further processing).
  572. */
  573. void pm_stay_awake(struct device *dev)
  574. {
  575. unsigned long flags;
  576. if (!dev)
  577. return;
  578. spin_lock_irqsave(&dev->power.lock, flags);
  579. __pm_stay_awake(dev->power.wakeup);
  580. spin_unlock_irqrestore(&dev->power.lock, flags);
  581. }
  582. EXPORT_SYMBOL_GPL(pm_stay_awake);
  583. #ifdef CONFIG_PM_AUTOSLEEP
  584. static void update_prevent_sleep_time(struct wakeup_source *ws, ktime_t now)
  585. {
  586. ktime_t delta = ktime_sub(now, ws->start_prevent_time);
  587. ws->prevent_sleep_time = ktime_add(ws->prevent_sleep_time, delta);
  588. }
  589. #else
  590. static inline void update_prevent_sleep_time(struct wakeup_source *ws,
  591. ktime_t now) {}
  592. #endif
  593. /**
  594. * wakup_source_deactivate - Mark given wakeup source as inactive.
  595. * @ws: Wakeup source to handle.
  596. *
  597. * Update the @ws' statistics and notify the PM core that the wakeup source has
  598. * become inactive by decrementing the counter of wakeup events being processed
  599. * and incrementing the counter of registered wakeup events.
  600. */
  601. static void wakeup_source_deactivate(struct wakeup_source *ws)
  602. {
  603. unsigned int cnt, inpr, cec;
  604. ktime_t duration;
  605. ktime_t now;
  606. ws->relax_count++;
  607. /*
  608. * __pm_relax() may be called directly or from a timer function.
  609. * If it is called directly right after the timer function has been
  610. * started, but before the timer function calls __pm_relax(), it is
  611. * possible that __pm_stay_awake() will be called in the meantime and
  612. * will set ws->active. Then, ws->active may be cleared immediately
  613. * by the __pm_relax() called from the timer function, but in such a
  614. * case ws->relax_count will be different from ws->active_count.
  615. */
  616. if (ws->relax_count != ws->active_count) {
  617. ws->relax_count--;
  618. return;
  619. }
  620. ws->active = false;
  621. now = ktime_get();
  622. duration = ktime_sub(now, ws->last_time);
  623. ws->total_time = ktime_add(ws->total_time, duration);
  624. if (ktime_to_ns(duration) > ktime_to_ns(ws->max_time))
  625. ws->max_time = duration;
  626. ws->last_time = now;
  627. del_timer(&ws->timer);
  628. ws->timer_expires = 0;
  629. if (ws->autosleep_enabled)
  630. update_prevent_sleep_time(ws, now);
  631. /*
  632. * Increment the counter of registered wakeup events and decrement the
  633. * couter of wakeup events in progress simultaneously.
  634. */
  635. cec = atomic_add_return(MAX_IN_PROGRESS, &combined_event_count);
  636. trace_wakeup_source_deactivate(ws->name, cec);
  637. split_counters(&cnt, &inpr);
  638. if (!inpr && waitqueue_active(&wakeup_count_wait_queue))
  639. wake_up(&wakeup_count_wait_queue);
  640. }
  641. /**
  642. * __pm_relax - Notify the PM core that processing of a wakeup event has ended.
  643. * @ws: Wakeup source object associated with the source of the event.
  644. *
  645. * Call this function for wakeup events whose processing started with calling
  646. * __pm_stay_awake().
  647. *
  648. * It is safe to call it from interrupt context.
  649. */
  650. void __pm_relax(struct wakeup_source *ws)
  651. {
  652. unsigned long flags;
  653. if (!ws)
  654. return;
  655. spin_lock_irqsave(&ws->lock, flags);
  656. if (ws->active)
  657. wakeup_source_deactivate(ws);
  658. spin_unlock_irqrestore(&ws->lock, flags);
  659. }
  660. EXPORT_SYMBOL_GPL(__pm_relax);
  661. /**
  662. * pm_relax - Notify the PM core that processing of a wakeup event has ended.
  663. * @dev: Device that signaled the event.
  664. *
  665. * Execute __pm_relax() for the @dev's wakeup source object.
  666. */
  667. void pm_relax(struct device *dev)
  668. {
  669. unsigned long flags;
  670. if (!dev)
  671. return;
  672. spin_lock_irqsave(&dev->power.lock, flags);
  673. __pm_relax(dev->power.wakeup);
  674. spin_unlock_irqrestore(&dev->power.lock, flags);
  675. }
  676. EXPORT_SYMBOL_GPL(pm_relax);
  677. /**
  678. * pm_wakeup_timer_fn - Delayed finalization of a wakeup event.
  679. * @data: Address of the wakeup source object associated with the event source.
  680. *
  681. * Call wakeup_source_deactivate() for the wakeup source whose address is stored
  682. * in @data if it is currently active and its timer has not been canceled and
  683. * the expiration time of the timer is not in future.
  684. */
  685. static void pm_wakeup_timer_fn(struct timer_list *t)
  686. {
  687. struct wakeup_source *ws = from_timer(ws, t, timer);
  688. unsigned long flags;
  689. spin_lock_irqsave(&ws->lock, flags);
  690. if (ws->active && ws->timer_expires
  691. && time_after_eq(jiffies, ws->timer_expires)) {
  692. wakeup_source_deactivate(ws);
  693. ws->expire_count++;
  694. }
  695. spin_unlock_irqrestore(&ws->lock, flags);
  696. }
  697. /**
  698. * pm_wakeup_ws_event - Notify the PM core of a wakeup event.
  699. * @ws: Wakeup source object associated with the event source.
  700. * @msec: Anticipated event processing time (in milliseconds).
  701. * @hard: If set, abort suspends in progress and wake up from suspend-to-idle.
  702. *
  703. * Notify the PM core of a wakeup event whose source is @ws that will take
  704. * approximately @msec milliseconds to be processed by the kernel. If @ws is
  705. * not active, activate it. If @msec is nonzero, set up the @ws' timer to
  706. * execute pm_wakeup_timer_fn() in future.
  707. *
  708. * It is safe to call this function from interrupt context.
  709. */
  710. void pm_wakeup_ws_event(struct wakeup_source *ws, unsigned int msec, bool hard)
  711. {
  712. unsigned long flags;
  713. unsigned long expires;
  714. if (!ws)
  715. return;
  716. spin_lock_irqsave(&ws->lock, flags);
  717. wakeup_source_report_event(ws, hard);
  718. if (!msec) {
  719. wakeup_source_deactivate(ws);
  720. goto unlock;
  721. }
  722. expires = jiffies + msecs_to_jiffies(msec);
  723. if (!expires)
  724. expires = 1;
  725. if (!ws->timer_expires || time_after(expires, ws->timer_expires)) {
  726. mod_timer(&ws->timer, expires);
  727. ws->timer_expires = expires;
  728. }
  729. unlock:
  730. spin_unlock_irqrestore(&ws->lock, flags);
  731. }
  732. EXPORT_SYMBOL_GPL(pm_wakeup_ws_event);
  733. /**
  734. * pm_wakeup_dev_event - Notify the PM core of a wakeup event.
  735. * @dev: Device the wakeup event is related to.
  736. * @msec: Anticipated event processing time (in milliseconds).
  737. * @hard: If set, abort suspends in progress and wake up from suspend-to-idle.
  738. *
  739. * Call pm_wakeup_ws_event() for the @dev's wakeup source object.
  740. */
  741. void pm_wakeup_dev_event(struct device *dev, unsigned int msec, bool hard)
  742. {
  743. unsigned long flags;
  744. if (!dev)
  745. return;
  746. spin_lock_irqsave(&dev->power.lock, flags);
  747. pm_wakeup_ws_event(dev->power.wakeup, msec, hard);
  748. spin_unlock_irqrestore(&dev->power.lock, flags);
  749. }
  750. EXPORT_SYMBOL_GPL(pm_wakeup_dev_event);
  751. void pm_get_active_wakeup_sources(char *pending_wakeup_source, size_t max)
  752. {
  753. struct wakeup_source *ws, *last_active_ws = NULL;
  754. int len = 0;
  755. bool active = false;
  756. rcu_read_lock();
  757. list_for_each_entry_rcu(ws, &wakeup_sources, entry) {
  758. if (ws->active && len < max) {
  759. if (!active)
  760. len += scnprintf(pending_wakeup_source, max,
  761. "Pending Wakeup Sources: ");
  762. len += scnprintf(pending_wakeup_source + len, max - len,
  763. "%s ", ws->name);
  764. active = true;
  765. } else if (!active &&
  766. (!last_active_ws ||
  767. ktime_to_ns(ws->last_time) >
  768. ktime_to_ns(last_active_ws->last_time))) {
  769. last_active_ws = ws;
  770. }
  771. }
  772. if (!active && last_active_ws) {
  773. scnprintf(pending_wakeup_source, max,
  774. "Last active Wakeup Source: %s",
  775. last_active_ws->name);
  776. }
  777. rcu_read_unlock();
  778. }
  779. EXPORT_SYMBOL_GPL(pm_get_active_wakeup_sources);
  780. void pm_print_active_wakeup_sources(void)
  781. {
  782. struct wakeup_source *ws;
  783. int srcuidx, active = 0;
  784. struct wakeup_source *last_activity_ws = NULL;
  785. srcuidx = srcu_read_lock(&wakeup_srcu);
  786. list_for_each_entry_rcu_locked(ws, &wakeup_sources, entry) {
  787. if (ws->active) {
  788. pm_pr_dbg("active wakeup source: %s\n", ws->name);
  789. active = 1;
  790. } else if (!active &&
  791. (!last_activity_ws ||
  792. ktime_to_ns(ws->last_time) >
  793. ktime_to_ns(last_activity_ws->last_time))) {
  794. last_activity_ws = ws;
  795. }
  796. }
  797. if (!active && last_activity_ws)
  798. pm_pr_dbg("last active wakeup source: %s\n",
  799. last_activity_ws->name);
  800. srcu_read_unlock(&wakeup_srcu, srcuidx);
  801. }
  802. EXPORT_SYMBOL_GPL(pm_print_active_wakeup_sources);
  803. /**
  804. * pm_wakeup_pending - Check if power transition in progress should be aborted.
  805. *
  806. * Compare the current number of registered wakeup events with its preserved
  807. * value from the past and return true if new wakeup events have been registered
  808. * since the old value was stored. Also return true if the current number of
  809. * wakeup events being processed is different from zero.
  810. */
  811. bool pm_wakeup_pending(void)
  812. {
  813. unsigned long flags;
  814. bool ret = false;
  815. char suspend_abort[MAX_SUSPEND_ABORT_LEN];
  816. raw_spin_lock_irqsave(&events_lock, flags);
  817. if (events_check_enabled) {
  818. unsigned int cnt, inpr;
  819. split_counters(&cnt, &inpr);
  820. ret = (cnt != saved_count || inpr > 0);
  821. events_check_enabled = !ret;
  822. }
  823. raw_spin_unlock_irqrestore(&events_lock, flags);
  824. if (ret) {
  825. pm_pr_dbg("Wakeup pending, aborting suspend\n");
  826. pm_print_active_wakeup_sources();
  827. pm_get_active_wakeup_sources(suspend_abort,
  828. MAX_SUSPEND_ABORT_LEN);
  829. log_suspend_abort_reason(suspend_abort);
  830. pr_info("PM: %s\n", suspend_abort);
  831. }
  832. return ret || atomic_read(&pm_abort_suspend) > 0;
  833. }
  834. void pm_system_wakeup(void)
  835. {
  836. atomic_inc(&pm_abort_suspend);
  837. s2idle_wake();
  838. }
  839. EXPORT_SYMBOL_GPL(pm_system_wakeup);
  840. void pm_system_cancel_wakeup(void)
  841. {
  842. atomic_dec_if_positive(&pm_abort_suspend);
  843. }
  844. void pm_wakeup_clear(unsigned int irq_number)
  845. {
  846. raw_spin_lock_irq(&wakeup_irq_lock);
  847. if (irq_number && wakeup_irq[0] == irq_number)
  848. wakeup_irq[0] = wakeup_irq[1];
  849. else
  850. wakeup_irq[0] = 0;
  851. wakeup_irq[1] = 0;
  852. raw_spin_unlock_irq(&wakeup_irq_lock);
  853. if (!irq_number)
  854. atomic_set(&pm_abort_suspend, 0);
  855. }
  856. void pm_system_irq_wakeup(unsigned int irq_number)
  857. {
  858. unsigned long flags;
  859. raw_spin_lock_irqsave(&wakeup_irq_lock, flags);
  860. if (wakeup_irq[0] == 0)
  861. wakeup_irq[0] = irq_number;
  862. else if (wakeup_irq[1] == 0)
  863. wakeup_irq[1] = irq_number;
  864. else
  865. irq_number = 0;
  866. raw_spin_unlock_irqrestore(&wakeup_irq_lock, flags);
  867. if (irq_number) {
  868. struct irq_desc *desc;
  869. const char *name = "null";
  870. desc = irq_to_desc(irq_number);
  871. if (desc == NULL)
  872. name = "stray irq";
  873. else if (desc->action && desc->action->name)
  874. name = desc->action->name;
  875. log_irq_wakeup_reason(irq_number);
  876. pr_warn("%s: %d triggered %s\n", __func__, irq_number, name);
  877. pm_system_wakeup();
  878. }
  879. }
  880. unsigned int pm_wakeup_irq(void)
  881. {
  882. return wakeup_irq[0];
  883. }
  884. /**
  885. * pm_get_wakeup_count - Read the number of registered wakeup events.
  886. * @count: Address to store the value at.
  887. * @block: Whether or not to block.
  888. *
  889. * Store the number of registered wakeup events at the address in @count. If
  890. * @block is set, block until the current number of wakeup events being
  891. * processed is zero.
  892. *
  893. * Return 'false' if the current number of wakeup events being processed is
  894. * nonzero. Otherwise return 'true'.
  895. */
  896. bool pm_get_wakeup_count(unsigned int *count, bool block)
  897. {
  898. unsigned int cnt, inpr;
  899. if (block) {
  900. DEFINE_WAIT(wait);
  901. for (;;) {
  902. prepare_to_wait(&wakeup_count_wait_queue, &wait,
  903. TASK_INTERRUPTIBLE);
  904. split_counters(&cnt, &inpr);
  905. if (inpr == 0 || signal_pending(current))
  906. break;
  907. pm_print_active_wakeup_sources();
  908. schedule();
  909. }
  910. finish_wait(&wakeup_count_wait_queue, &wait);
  911. }
  912. split_counters(&cnt, &inpr);
  913. *count = cnt;
  914. return !inpr;
  915. }
  916. /**
  917. * pm_save_wakeup_count - Save the current number of registered wakeup events.
  918. * @count: Value to compare with the current number of registered wakeup events.
  919. *
  920. * If @count is equal to the current number of registered wakeup events and the
  921. * current number of wakeup events being processed is zero, store @count as the
  922. * old number of registered wakeup events for pm_check_wakeup_events(), enable
  923. * wakeup events detection and return 'true'. Otherwise disable wakeup events
  924. * detection and return 'false'.
  925. */
  926. bool pm_save_wakeup_count(unsigned int count)
  927. {
  928. unsigned int cnt, inpr;
  929. unsigned long flags;
  930. events_check_enabled = false;
  931. raw_spin_lock_irqsave(&events_lock, flags);
  932. split_counters(&cnt, &inpr);
  933. if (cnt == count && inpr == 0) {
  934. saved_count = count;
  935. events_check_enabled = true;
  936. }
  937. raw_spin_unlock_irqrestore(&events_lock, flags);
  938. return events_check_enabled;
  939. }
  940. #ifdef CONFIG_PM_AUTOSLEEP
  941. /**
  942. * pm_wakep_autosleep_enabled - Modify autosleep_enabled for all wakeup sources.
  943. * @enabled: Whether to set or to clear the autosleep_enabled flags.
  944. */
  945. void pm_wakep_autosleep_enabled(bool set)
  946. {
  947. struct wakeup_source *ws;
  948. ktime_t now = ktime_get();
  949. int srcuidx;
  950. srcuidx = srcu_read_lock(&wakeup_srcu);
  951. list_for_each_entry_rcu_locked(ws, &wakeup_sources, entry) {
  952. spin_lock_irq(&ws->lock);
  953. if (ws->autosleep_enabled != set) {
  954. ws->autosleep_enabled = set;
  955. if (ws->active) {
  956. if (set)
  957. ws->start_prevent_time = now;
  958. else
  959. update_prevent_sleep_time(ws, now);
  960. }
  961. }
  962. spin_unlock_irq(&ws->lock);
  963. }
  964. srcu_read_unlock(&wakeup_srcu, srcuidx);
  965. }
  966. #endif /* CONFIG_PM_AUTOSLEEP */
  967. /**
  968. * print_wakeup_source_stats - Print wakeup source statistics information.
  969. * @m: seq_file to print the statistics into.
  970. * @ws: Wakeup source object to print the statistics for.
  971. */
  972. static int print_wakeup_source_stats(struct seq_file *m,
  973. struct wakeup_source *ws)
  974. {
  975. unsigned long flags;
  976. ktime_t total_time;
  977. ktime_t max_time;
  978. unsigned long active_count;
  979. ktime_t active_time;
  980. ktime_t prevent_sleep_time;
  981. spin_lock_irqsave(&ws->lock, flags);
  982. total_time = ws->total_time;
  983. max_time = ws->max_time;
  984. prevent_sleep_time = ws->prevent_sleep_time;
  985. active_count = ws->active_count;
  986. if (ws->active) {
  987. ktime_t now = ktime_get();
  988. active_time = ktime_sub(now, ws->last_time);
  989. total_time = ktime_add(total_time, active_time);
  990. if (active_time > max_time)
  991. max_time = active_time;
  992. if (ws->autosleep_enabled)
  993. prevent_sleep_time = ktime_add(prevent_sleep_time,
  994. ktime_sub(now, ws->start_prevent_time));
  995. } else {
  996. active_time = 0;
  997. }
  998. 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",
  999. ws->name, active_count, ws->event_count,
  1000. ws->wakeup_count, ws->expire_count,
  1001. ktime_to_ms(active_time), ktime_to_ms(total_time),
  1002. ktime_to_ms(max_time), ktime_to_ms(ws->last_time),
  1003. ktime_to_ms(prevent_sleep_time));
  1004. spin_unlock_irqrestore(&ws->lock, flags);
  1005. return 0;
  1006. }
  1007. static void *wakeup_sources_stats_seq_start(struct seq_file *m,
  1008. loff_t *pos)
  1009. {
  1010. struct wakeup_source *ws;
  1011. loff_t n = *pos;
  1012. int *srcuidx = m->private;
  1013. if (n == 0) {
  1014. seq_puts(m, "name\t\tactive_count\tevent_count\twakeup_count\t"
  1015. "expire_count\tactive_since\ttotal_time\tmax_time\t"
  1016. "last_change\tprevent_suspend_time\n");
  1017. }
  1018. *srcuidx = srcu_read_lock(&wakeup_srcu);
  1019. list_for_each_entry_rcu_locked(ws, &wakeup_sources, entry) {
  1020. if (n-- <= 0)
  1021. return ws;
  1022. }
  1023. return NULL;
  1024. }
  1025. static void *wakeup_sources_stats_seq_next(struct seq_file *m,
  1026. void *v, loff_t *pos)
  1027. {
  1028. struct wakeup_source *ws = v;
  1029. struct wakeup_source *next_ws = NULL;
  1030. ++(*pos);
  1031. list_for_each_entry_continue_rcu(ws, &wakeup_sources, entry) {
  1032. next_ws = ws;
  1033. break;
  1034. }
  1035. if (!next_ws)
  1036. print_wakeup_source_stats(m, &deleted_ws);
  1037. return next_ws;
  1038. }
  1039. static void wakeup_sources_stats_seq_stop(struct seq_file *m, void *v)
  1040. {
  1041. int *srcuidx = m->private;
  1042. srcu_read_unlock(&wakeup_srcu, *srcuidx);
  1043. }
  1044. /**
  1045. * wakeup_sources_stats_seq_show - Print wakeup sources statistics information.
  1046. * @m: seq_file to print the statistics into.
  1047. * @v: wakeup_source of each iteration
  1048. */
  1049. static int wakeup_sources_stats_seq_show(struct seq_file *m, void *v)
  1050. {
  1051. struct wakeup_source *ws = v;
  1052. print_wakeup_source_stats(m, ws);
  1053. return 0;
  1054. }
  1055. static const struct seq_operations wakeup_sources_stats_seq_ops = {
  1056. .start = wakeup_sources_stats_seq_start,
  1057. .next = wakeup_sources_stats_seq_next,
  1058. .stop = wakeup_sources_stats_seq_stop,
  1059. .show = wakeup_sources_stats_seq_show,
  1060. };
  1061. static int wakeup_sources_stats_open(struct inode *inode, struct file *file)
  1062. {
  1063. return seq_open_private(file, &wakeup_sources_stats_seq_ops, sizeof(int));
  1064. }
  1065. static const struct file_operations wakeup_sources_stats_fops = {
  1066. .owner = THIS_MODULE,
  1067. .open = wakeup_sources_stats_open,
  1068. .read = seq_read,
  1069. .llseek = seq_lseek,
  1070. .release = seq_release_private,
  1071. };
  1072. static int __init wakeup_sources_debugfs_init(void)
  1073. {
  1074. debugfs_create_file("wakeup_sources", S_IRUGO, NULL, NULL,
  1075. &wakeup_sources_stats_fops);
  1076. return 0;
  1077. }
  1078. postcore_initcall(wakeup_sources_debugfs_init);