Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0
0002 /*
0003  * drivers/base/power/wakeup.c - System wakeup events framework
0004  *
0005  * Copyright (c) 2010 Rafael J. Wysocki <rjw@sisk.pl>, Novell Inc.
0006  */
0007 #define pr_fmt(fmt) "PM: " fmt
0008 
0009 #include <linux/device.h>
0010 #include <linux/slab.h>
0011 #include <linux/sched/signal.h>
0012 #include <linux/capability.h>
0013 #include <linux/export.h>
0014 #include <linux/suspend.h>
0015 #include <linux/seq_file.h>
0016 #include <linux/debugfs.h>
0017 #include <linux/pm_wakeirq.h>
0018 #include <trace/events/power.h>
0019 
0020 #include "power.h"
0021 
0022 #ifndef CONFIG_SUSPEND
0023 suspend_state_t pm_suspend_target_state;
0024 #define pm_suspend_target_state (PM_SUSPEND_ON)
0025 #endif
0026 
0027 #define list_for_each_entry_rcu_locked(pos, head, member) \
0028     list_for_each_entry_rcu(pos, head, member, \
0029         srcu_read_lock_held(&wakeup_srcu))
0030 /*
0031  * If set, the suspend/hibernate code will abort transitions to a sleep state
0032  * if wakeup events are registered during or immediately before the transition.
0033  */
0034 bool events_check_enabled __read_mostly;
0035 
0036 /* First wakeup IRQ seen by the kernel in the last cycle. */
0037 static unsigned int wakeup_irq[2] __read_mostly;
0038 static DEFINE_RAW_SPINLOCK(wakeup_irq_lock);
0039 
0040 /* If greater than 0 and the system is suspending, terminate the suspend. */
0041 static atomic_t pm_abort_suspend __read_mostly;
0042 
0043 /*
0044  * Combined counters of registered wakeup events and wakeup events in progress.
0045  * They need to be modified together atomically, so it's better to use one
0046  * atomic variable to hold them both.
0047  */
0048 static atomic_t combined_event_count = ATOMIC_INIT(0);
0049 
0050 #define IN_PROGRESS_BITS    (sizeof(int) * 4)
0051 #define MAX_IN_PROGRESS     ((1 << IN_PROGRESS_BITS) - 1)
0052 
0053 static void split_counters(unsigned int *cnt, unsigned int *inpr)
0054 {
0055     unsigned int comb = atomic_read(&combined_event_count);
0056 
0057     *cnt = (comb >> IN_PROGRESS_BITS);
0058     *inpr = comb & MAX_IN_PROGRESS;
0059 }
0060 
0061 /* A preserved old value of the events counter. */
0062 static unsigned int saved_count;
0063 
0064 static DEFINE_RAW_SPINLOCK(events_lock);
0065 
0066 static void pm_wakeup_timer_fn(struct timer_list *t);
0067 
0068 static LIST_HEAD(wakeup_sources);
0069 
0070 static DECLARE_WAIT_QUEUE_HEAD(wakeup_count_wait_queue);
0071 
0072 DEFINE_STATIC_SRCU(wakeup_srcu);
0073 
0074 static struct wakeup_source deleted_ws = {
0075     .name = "deleted",
0076     .lock =  __SPIN_LOCK_UNLOCKED(deleted_ws.lock),
0077 };
0078 
0079 static DEFINE_IDA(wakeup_ida);
0080 
0081 /**
0082  * wakeup_source_create - Create a struct wakeup_source object.
0083  * @name: Name of the new wakeup source.
0084  */
0085 struct wakeup_source *wakeup_source_create(const char *name)
0086 {
0087     struct wakeup_source *ws;
0088     const char *ws_name;
0089     int id;
0090 
0091     ws = kzalloc(sizeof(*ws), GFP_KERNEL);
0092     if (!ws)
0093         goto err_ws;
0094 
0095     ws_name = kstrdup_const(name, GFP_KERNEL);
0096     if (!ws_name)
0097         goto err_name;
0098     ws->name = ws_name;
0099 
0100     id = ida_alloc(&wakeup_ida, GFP_KERNEL);
0101     if (id < 0)
0102         goto err_id;
0103     ws->id = id;
0104 
0105     return ws;
0106 
0107 err_id:
0108     kfree_const(ws->name);
0109 err_name:
0110     kfree(ws);
0111 err_ws:
0112     return NULL;
0113 }
0114 EXPORT_SYMBOL_GPL(wakeup_source_create);
0115 
0116 /*
0117  * Record wakeup_source statistics being deleted into a dummy wakeup_source.
0118  */
0119 static void wakeup_source_record(struct wakeup_source *ws)
0120 {
0121     unsigned long flags;
0122 
0123     spin_lock_irqsave(&deleted_ws.lock, flags);
0124 
0125     if (ws->event_count) {
0126         deleted_ws.total_time =
0127             ktime_add(deleted_ws.total_time, ws->total_time);
0128         deleted_ws.prevent_sleep_time =
0129             ktime_add(deleted_ws.prevent_sleep_time,
0130                   ws->prevent_sleep_time);
0131         deleted_ws.max_time =
0132             ktime_compare(deleted_ws.max_time, ws->max_time) > 0 ?
0133                 deleted_ws.max_time : ws->max_time;
0134         deleted_ws.event_count += ws->event_count;
0135         deleted_ws.active_count += ws->active_count;
0136         deleted_ws.relax_count += ws->relax_count;
0137         deleted_ws.expire_count += ws->expire_count;
0138         deleted_ws.wakeup_count += ws->wakeup_count;
0139     }
0140 
0141     spin_unlock_irqrestore(&deleted_ws.lock, flags);
0142 }
0143 
0144 static void wakeup_source_free(struct wakeup_source *ws)
0145 {
0146     ida_free(&wakeup_ida, ws->id);
0147     kfree_const(ws->name);
0148     kfree(ws);
0149 }
0150 
0151 /**
0152  * wakeup_source_destroy - Destroy a struct wakeup_source object.
0153  * @ws: Wakeup source to destroy.
0154  *
0155  * Use only for wakeup source objects created with wakeup_source_create().
0156  */
0157 void wakeup_source_destroy(struct wakeup_source *ws)
0158 {
0159     if (!ws)
0160         return;
0161 
0162     __pm_relax(ws);
0163     wakeup_source_record(ws);
0164     wakeup_source_free(ws);
0165 }
0166 EXPORT_SYMBOL_GPL(wakeup_source_destroy);
0167 
0168 /**
0169  * wakeup_source_add - Add given object to the list of wakeup sources.
0170  * @ws: Wakeup source object to add to the list.
0171  */
0172 void wakeup_source_add(struct wakeup_source *ws)
0173 {
0174     unsigned long flags;
0175 
0176     if (WARN_ON(!ws))
0177         return;
0178 
0179     spin_lock_init(&ws->lock);
0180     timer_setup(&ws->timer, pm_wakeup_timer_fn, 0);
0181     ws->active = false;
0182 
0183     raw_spin_lock_irqsave(&events_lock, flags);
0184     list_add_rcu(&ws->entry, &wakeup_sources);
0185     raw_spin_unlock_irqrestore(&events_lock, flags);
0186 }
0187 EXPORT_SYMBOL_GPL(wakeup_source_add);
0188 
0189 /**
0190  * wakeup_source_remove - Remove given object from the wakeup sources list.
0191  * @ws: Wakeup source object to remove from the list.
0192  */
0193 void wakeup_source_remove(struct wakeup_source *ws)
0194 {
0195     unsigned long flags;
0196 
0197     if (WARN_ON(!ws))
0198         return;
0199 
0200     raw_spin_lock_irqsave(&events_lock, flags);
0201     list_del_rcu(&ws->entry);
0202     raw_spin_unlock_irqrestore(&events_lock, flags);
0203     synchronize_srcu(&wakeup_srcu);
0204 
0205     del_timer_sync(&ws->timer);
0206     /*
0207      * Clear timer.function to make wakeup_source_not_registered() treat
0208      * this wakeup source as not registered.
0209      */
0210     ws->timer.function = NULL;
0211 }
0212 EXPORT_SYMBOL_GPL(wakeup_source_remove);
0213 
0214 /**
0215  * wakeup_source_register - Create wakeup source and add it to the list.
0216  * @dev: Device this wakeup source is associated with (or NULL if virtual).
0217  * @name: Name of the wakeup source to register.
0218  */
0219 struct wakeup_source *wakeup_source_register(struct device *dev,
0220                          const char *name)
0221 {
0222     struct wakeup_source *ws;
0223     int ret;
0224 
0225     ws = wakeup_source_create(name);
0226     if (ws) {
0227         if (!dev || device_is_registered(dev)) {
0228             ret = wakeup_source_sysfs_add(dev, ws);
0229             if (ret) {
0230                 wakeup_source_free(ws);
0231                 return NULL;
0232             }
0233         }
0234         wakeup_source_add(ws);
0235     }
0236     return ws;
0237 }
0238 EXPORT_SYMBOL_GPL(wakeup_source_register);
0239 
0240 /**
0241  * wakeup_source_unregister - Remove wakeup source from the list and remove it.
0242  * @ws: Wakeup source object to unregister.
0243  */
0244 void wakeup_source_unregister(struct wakeup_source *ws)
0245 {
0246     if (ws) {
0247         wakeup_source_remove(ws);
0248         if (ws->dev)
0249             wakeup_source_sysfs_remove(ws);
0250 
0251         wakeup_source_destroy(ws);
0252     }
0253 }
0254 EXPORT_SYMBOL_GPL(wakeup_source_unregister);
0255 
0256 /**
0257  * wakeup_sources_read_lock - Lock wakeup source list for read.
0258  *
0259  * Returns an index of srcu lock for struct wakeup_srcu.
0260  * This index must be passed to the matching wakeup_sources_read_unlock().
0261  */
0262 int wakeup_sources_read_lock(void)
0263 {
0264     return srcu_read_lock(&wakeup_srcu);
0265 }
0266 EXPORT_SYMBOL_GPL(wakeup_sources_read_lock);
0267 
0268 /**
0269  * wakeup_sources_read_unlock - Unlock wakeup source list.
0270  * @idx: return value from corresponding wakeup_sources_read_lock()
0271  */
0272 void wakeup_sources_read_unlock(int idx)
0273 {
0274     srcu_read_unlock(&wakeup_srcu, idx);
0275 }
0276 EXPORT_SYMBOL_GPL(wakeup_sources_read_unlock);
0277 
0278 /**
0279  * wakeup_sources_walk_start - Begin a walk on wakeup source list
0280  *
0281  * Returns first object of the list of wakeup sources.
0282  *
0283  * Note that to be safe, wakeup sources list needs to be locked by calling
0284  * wakeup_source_read_lock() for this.
0285  */
0286 struct wakeup_source *wakeup_sources_walk_start(void)
0287 {
0288     struct list_head *ws_head = &wakeup_sources;
0289 
0290     return list_entry_rcu(ws_head->next, struct wakeup_source, entry);
0291 }
0292 EXPORT_SYMBOL_GPL(wakeup_sources_walk_start);
0293 
0294 /**
0295  * wakeup_sources_walk_next - Get next wakeup source from the list
0296  * @ws: Previous wakeup source object
0297  *
0298  * Note that to be safe, wakeup sources list needs to be locked by calling
0299  * wakeup_source_read_lock() for this.
0300  */
0301 struct wakeup_source *wakeup_sources_walk_next(struct wakeup_source *ws)
0302 {
0303     struct list_head *ws_head = &wakeup_sources;
0304 
0305     return list_next_or_null_rcu(ws_head, &ws->entry,
0306                 struct wakeup_source, entry);
0307 }
0308 EXPORT_SYMBOL_GPL(wakeup_sources_walk_next);
0309 
0310 /**
0311  * device_wakeup_attach - Attach a wakeup source object to a device object.
0312  * @dev: Device to handle.
0313  * @ws: Wakeup source object to attach to @dev.
0314  *
0315  * This causes @dev to be treated as a wakeup device.
0316  */
0317 static int device_wakeup_attach(struct device *dev, struct wakeup_source *ws)
0318 {
0319     spin_lock_irq(&dev->power.lock);
0320     if (dev->power.wakeup) {
0321         spin_unlock_irq(&dev->power.lock);
0322         return -EEXIST;
0323     }
0324     dev->power.wakeup = ws;
0325     if (dev->power.wakeirq)
0326         device_wakeup_attach_irq(dev, dev->power.wakeirq);
0327     spin_unlock_irq(&dev->power.lock);
0328     return 0;
0329 }
0330 
0331 /**
0332  * device_wakeup_enable - Enable given device to be a wakeup source.
0333  * @dev: Device to handle.
0334  *
0335  * Create a wakeup source object, register it and attach it to @dev.
0336  */
0337 int device_wakeup_enable(struct device *dev)
0338 {
0339     struct wakeup_source *ws;
0340     int ret;
0341 
0342     if (!dev || !dev->power.can_wakeup)
0343         return -EINVAL;
0344 
0345     if (pm_suspend_target_state != PM_SUSPEND_ON)
0346         dev_dbg(dev, "Suspicious %s() during system transition!\n", __func__);
0347 
0348     ws = wakeup_source_register(dev, dev_name(dev));
0349     if (!ws)
0350         return -ENOMEM;
0351 
0352     ret = device_wakeup_attach(dev, ws);
0353     if (ret)
0354         wakeup_source_unregister(ws);
0355 
0356     return ret;
0357 }
0358 EXPORT_SYMBOL_GPL(device_wakeup_enable);
0359 
0360 /**
0361  * device_wakeup_attach_irq - Attach a wakeirq to a wakeup source
0362  * @dev: Device to handle
0363  * @wakeirq: Device specific wakeirq entry
0364  *
0365  * Attach a device wakeirq to the wakeup source so the device
0366  * wake IRQ can be configured automatically for suspend and
0367  * resume.
0368  *
0369  * Call under the device's power.lock lock.
0370  */
0371 void device_wakeup_attach_irq(struct device *dev,
0372                  struct wake_irq *wakeirq)
0373 {
0374     struct wakeup_source *ws;
0375 
0376     ws = dev->power.wakeup;
0377     if (!ws)
0378         return;
0379 
0380     if (ws->wakeirq)
0381         dev_err(dev, "Leftover wakeup IRQ found, overriding\n");
0382 
0383     ws->wakeirq = wakeirq;
0384 }
0385 
0386 /**
0387  * device_wakeup_detach_irq - Detach a wakeirq from a wakeup source
0388  * @dev: Device to handle
0389  *
0390  * Removes a device wakeirq from the wakeup source.
0391  *
0392  * Call under the device's power.lock lock.
0393  */
0394 void device_wakeup_detach_irq(struct device *dev)
0395 {
0396     struct wakeup_source *ws;
0397 
0398     ws = dev->power.wakeup;
0399     if (ws)
0400         ws->wakeirq = NULL;
0401 }
0402 
0403 /**
0404  * device_wakeup_arm_wake_irqs -
0405  *
0406  * Iterates over the list of device wakeirqs to arm them.
0407  */
0408 void device_wakeup_arm_wake_irqs(void)
0409 {
0410     struct wakeup_source *ws;
0411     int srcuidx;
0412 
0413     srcuidx = srcu_read_lock(&wakeup_srcu);
0414     list_for_each_entry_rcu_locked(ws, &wakeup_sources, entry)
0415         dev_pm_arm_wake_irq(ws->wakeirq);
0416     srcu_read_unlock(&wakeup_srcu, srcuidx);
0417 }
0418 
0419 /**
0420  * device_wakeup_disarm_wake_irqs -
0421  *
0422  * Iterates over the list of device wakeirqs to disarm them.
0423  */
0424 void device_wakeup_disarm_wake_irqs(void)
0425 {
0426     struct wakeup_source *ws;
0427     int srcuidx;
0428 
0429     srcuidx = srcu_read_lock(&wakeup_srcu);
0430     list_for_each_entry_rcu_locked(ws, &wakeup_sources, entry)
0431         dev_pm_disarm_wake_irq(ws->wakeirq);
0432     srcu_read_unlock(&wakeup_srcu, srcuidx);
0433 }
0434 
0435 /**
0436  * device_wakeup_detach - Detach a device's wakeup source object from it.
0437  * @dev: Device to detach the wakeup source object from.
0438  *
0439  * After it returns, @dev will not be treated as a wakeup device any more.
0440  */
0441 static struct wakeup_source *device_wakeup_detach(struct device *dev)
0442 {
0443     struct wakeup_source *ws;
0444 
0445     spin_lock_irq(&dev->power.lock);
0446     ws = dev->power.wakeup;
0447     dev->power.wakeup = NULL;
0448     spin_unlock_irq(&dev->power.lock);
0449     return ws;
0450 }
0451 
0452 /**
0453  * device_wakeup_disable - Do not regard a device as a wakeup source any more.
0454  * @dev: Device to handle.
0455  *
0456  * Detach the @dev's wakeup source object from it, unregister this wakeup source
0457  * object and destroy it.
0458  */
0459 int device_wakeup_disable(struct device *dev)
0460 {
0461     struct wakeup_source *ws;
0462 
0463     if (!dev || !dev->power.can_wakeup)
0464         return -EINVAL;
0465 
0466     ws = device_wakeup_detach(dev);
0467     wakeup_source_unregister(ws);
0468     return 0;
0469 }
0470 EXPORT_SYMBOL_GPL(device_wakeup_disable);
0471 
0472 /**
0473  * device_set_wakeup_capable - Set/reset device wakeup capability flag.
0474  * @dev: Device to handle.
0475  * @capable: Whether or not @dev is capable of waking up the system from sleep.
0476  *
0477  * If @capable is set, set the @dev's power.can_wakeup flag and add its
0478  * wakeup-related attributes to sysfs.  Otherwise, unset the @dev's
0479  * power.can_wakeup flag and remove its wakeup-related attributes from sysfs.
0480  *
0481  * This function may sleep and it can't be called from any context where
0482  * sleeping is not allowed.
0483  */
0484 void device_set_wakeup_capable(struct device *dev, bool capable)
0485 {
0486     if (!!dev->power.can_wakeup == !!capable)
0487         return;
0488 
0489     dev->power.can_wakeup = capable;
0490     if (device_is_registered(dev) && !list_empty(&dev->power.entry)) {
0491         if (capable) {
0492             int ret = wakeup_sysfs_add(dev);
0493 
0494             if (ret)
0495                 dev_info(dev, "Wakeup sysfs attributes not added\n");
0496         } else {
0497             wakeup_sysfs_remove(dev);
0498         }
0499     }
0500 }
0501 EXPORT_SYMBOL_GPL(device_set_wakeup_capable);
0502 
0503 /**
0504  * device_set_wakeup_enable - Enable or disable a device to wake up the system.
0505  * @dev: Device to handle.
0506  * @enable: enable/disable flag
0507  */
0508 int device_set_wakeup_enable(struct device *dev, bool enable)
0509 {
0510     return enable ? device_wakeup_enable(dev) : device_wakeup_disable(dev);
0511 }
0512 EXPORT_SYMBOL_GPL(device_set_wakeup_enable);
0513 
0514 /**
0515  * wakeup_source_not_registered - validate the given wakeup source.
0516  * @ws: Wakeup source to be validated.
0517  */
0518 static bool wakeup_source_not_registered(struct wakeup_source *ws)
0519 {
0520     /*
0521      * Use timer struct to check if the given source is initialized
0522      * by wakeup_source_add.
0523      */
0524     return ws->timer.function != pm_wakeup_timer_fn;
0525 }
0526 
0527 /*
0528  * The functions below use the observation that each wakeup event starts a
0529  * period in which the system should not be suspended.  The moment this period
0530  * will end depends on how the wakeup event is going to be processed after being
0531  * detected and all of the possible cases can be divided into two distinct
0532  * groups.
0533  *
0534  * First, a wakeup event may be detected by the same functional unit that will
0535  * carry out the entire processing of it and possibly will pass it to user space
0536  * for further processing.  In that case the functional unit that has detected
0537  * the event may later "close" the "no suspend" period associated with it
0538  * directly as soon as it has been dealt with.  The pair of pm_stay_awake() and
0539  * pm_relax(), balanced with each other, is supposed to be used in such
0540  * situations.
0541  *
0542  * Second, a wakeup event may be detected by one functional unit and processed
0543  * by another one.  In that case the unit that has detected it cannot really
0544  * "close" the "no suspend" period associated with it, unless it knows in
0545  * advance what's going to happen to the event during processing.  This
0546  * knowledge, however, may not be available to it, so it can simply specify time
0547  * to wait before the system can be suspended and pass it as the second
0548  * argument of pm_wakeup_event().
0549  *
0550  * It is valid to call pm_relax() after pm_wakeup_event(), in which case the
0551  * "no suspend" period will be ended either by the pm_relax(), or by the timer
0552  * function executed when the timer expires, whichever comes first.
0553  */
0554 
0555 /**
0556  * wakeup_source_activate - Mark given wakeup source as active.
0557  * @ws: Wakeup source to handle.
0558  *
0559  * Update the @ws' statistics and, if @ws has just been activated, notify the PM
0560  * core of the event by incrementing the counter of the wakeup events being
0561  * processed.
0562  */
0563 static void wakeup_source_activate(struct wakeup_source *ws)
0564 {
0565     unsigned int cec;
0566 
0567     if (WARN_ONCE(wakeup_source_not_registered(ws),
0568             "unregistered wakeup source\n"))
0569         return;
0570 
0571     ws->active = true;
0572     ws->active_count++;
0573     ws->last_time = ktime_get();
0574     if (ws->autosleep_enabled)
0575         ws->start_prevent_time = ws->last_time;
0576 
0577     /* Increment the counter of events in progress. */
0578     cec = atomic_inc_return(&combined_event_count);
0579 
0580     trace_wakeup_source_activate(ws->name, cec);
0581 }
0582 
0583 /**
0584  * wakeup_source_report_event - Report wakeup event using the given source.
0585  * @ws: Wakeup source to report the event for.
0586  * @hard: If set, abort suspends in progress and wake up from suspend-to-idle.
0587  */
0588 static void wakeup_source_report_event(struct wakeup_source *ws, bool hard)
0589 {
0590     ws->event_count++;
0591     /* This is racy, but the counter is approximate anyway. */
0592     if (events_check_enabled)
0593         ws->wakeup_count++;
0594 
0595     if (!ws->active)
0596         wakeup_source_activate(ws);
0597 
0598     if (hard)
0599         pm_system_wakeup();
0600 }
0601 
0602 /**
0603  * __pm_stay_awake - Notify the PM core of a wakeup event.
0604  * @ws: Wakeup source object associated with the source of the event.
0605  *
0606  * It is safe to call this function from interrupt context.
0607  */
0608 void __pm_stay_awake(struct wakeup_source *ws)
0609 {
0610     unsigned long flags;
0611 
0612     if (!ws)
0613         return;
0614 
0615     spin_lock_irqsave(&ws->lock, flags);
0616 
0617     wakeup_source_report_event(ws, false);
0618     del_timer(&ws->timer);
0619     ws->timer_expires = 0;
0620 
0621     spin_unlock_irqrestore(&ws->lock, flags);
0622 }
0623 EXPORT_SYMBOL_GPL(__pm_stay_awake);
0624 
0625 /**
0626  * pm_stay_awake - Notify the PM core that a wakeup event is being processed.
0627  * @dev: Device the wakeup event is related to.
0628  *
0629  * Notify the PM core of a wakeup event (signaled by @dev) by calling
0630  * __pm_stay_awake for the @dev's wakeup source object.
0631  *
0632  * Call this function after detecting of a wakeup event if pm_relax() is going
0633  * to be called directly after processing the event (and possibly passing it to
0634  * user space for further processing).
0635  */
0636 void pm_stay_awake(struct device *dev)
0637 {
0638     unsigned long flags;
0639 
0640     if (!dev)
0641         return;
0642 
0643     spin_lock_irqsave(&dev->power.lock, flags);
0644     __pm_stay_awake(dev->power.wakeup);
0645     spin_unlock_irqrestore(&dev->power.lock, flags);
0646 }
0647 EXPORT_SYMBOL_GPL(pm_stay_awake);
0648 
0649 #ifdef CONFIG_PM_AUTOSLEEP
0650 static void update_prevent_sleep_time(struct wakeup_source *ws, ktime_t now)
0651 {
0652     ktime_t delta = ktime_sub(now, ws->start_prevent_time);
0653     ws->prevent_sleep_time = ktime_add(ws->prevent_sleep_time, delta);
0654 }
0655 #else
0656 static inline void update_prevent_sleep_time(struct wakeup_source *ws,
0657                          ktime_t now) {}
0658 #endif
0659 
0660 /**
0661  * wakeup_source_deactivate - Mark given wakeup source as inactive.
0662  * @ws: Wakeup source to handle.
0663  *
0664  * Update the @ws' statistics and notify the PM core that the wakeup source has
0665  * become inactive by decrementing the counter of wakeup events being processed
0666  * and incrementing the counter of registered wakeup events.
0667  */
0668 static void wakeup_source_deactivate(struct wakeup_source *ws)
0669 {
0670     unsigned int cnt, inpr, cec;
0671     ktime_t duration;
0672     ktime_t now;
0673 
0674     ws->relax_count++;
0675     /*
0676      * __pm_relax() may be called directly or from a timer function.
0677      * If it is called directly right after the timer function has been
0678      * started, but before the timer function calls __pm_relax(), it is
0679      * possible that __pm_stay_awake() will be called in the meantime and
0680      * will set ws->active.  Then, ws->active may be cleared immediately
0681      * by the __pm_relax() called from the timer function, but in such a
0682      * case ws->relax_count will be different from ws->active_count.
0683      */
0684     if (ws->relax_count != ws->active_count) {
0685         ws->relax_count--;
0686         return;
0687     }
0688 
0689     ws->active = false;
0690 
0691     now = ktime_get();
0692     duration = ktime_sub(now, ws->last_time);
0693     ws->total_time = ktime_add(ws->total_time, duration);
0694     if (ktime_to_ns(duration) > ktime_to_ns(ws->max_time))
0695         ws->max_time = duration;
0696 
0697     ws->last_time = now;
0698     del_timer(&ws->timer);
0699     ws->timer_expires = 0;
0700 
0701     if (ws->autosleep_enabled)
0702         update_prevent_sleep_time(ws, now);
0703 
0704     /*
0705      * Increment the counter of registered wakeup events and decrement the
0706      * counter of wakeup events in progress simultaneously.
0707      */
0708     cec = atomic_add_return(MAX_IN_PROGRESS, &combined_event_count);
0709     trace_wakeup_source_deactivate(ws->name, cec);
0710 
0711     split_counters(&cnt, &inpr);
0712     if (!inpr && waitqueue_active(&wakeup_count_wait_queue))
0713         wake_up(&wakeup_count_wait_queue);
0714 }
0715 
0716 /**
0717  * __pm_relax - Notify the PM core that processing of a wakeup event has ended.
0718  * @ws: Wakeup source object associated with the source of the event.
0719  *
0720  * Call this function for wakeup events whose processing started with calling
0721  * __pm_stay_awake().
0722  *
0723  * It is safe to call it from interrupt context.
0724  */
0725 void __pm_relax(struct wakeup_source *ws)
0726 {
0727     unsigned long flags;
0728 
0729     if (!ws)
0730         return;
0731 
0732     spin_lock_irqsave(&ws->lock, flags);
0733     if (ws->active)
0734         wakeup_source_deactivate(ws);
0735     spin_unlock_irqrestore(&ws->lock, flags);
0736 }
0737 EXPORT_SYMBOL_GPL(__pm_relax);
0738 
0739 /**
0740  * pm_relax - Notify the PM core that processing of a wakeup event has ended.
0741  * @dev: Device that signaled the event.
0742  *
0743  * Execute __pm_relax() for the @dev's wakeup source object.
0744  */
0745 void pm_relax(struct device *dev)
0746 {
0747     unsigned long flags;
0748 
0749     if (!dev)
0750         return;
0751 
0752     spin_lock_irqsave(&dev->power.lock, flags);
0753     __pm_relax(dev->power.wakeup);
0754     spin_unlock_irqrestore(&dev->power.lock, flags);
0755 }
0756 EXPORT_SYMBOL_GPL(pm_relax);
0757 
0758 /**
0759  * pm_wakeup_timer_fn - Delayed finalization of a wakeup event.
0760  * @t: timer list
0761  *
0762  * Call wakeup_source_deactivate() for the wakeup source whose address is stored
0763  * in @data if it is currently active and its timer has not been canceled and
0764  * the expiration time of the timer is not in future.
0765  */
0766 static void pm_wakeup_timer_fn(struct timer_list *t)
0767 {
0768     struct wakeup_source *ws = from_timer(ws, t, timer);
0769     unsigned long flags;
0770 
0771     spin_lock_irqsave(&ws->lock, flags);
0772 
0773     if (ws->active && ws->timer_expires
0774         && time_after_eq(jiffies, ws->timer_expires)) {
0775         wakeup_source_deactivate(ws);
0776         ws->expire_count++;
0777     }
0778 
0779     spin_unlock_irqrestore(&ws->lock, flags);
0780 }
0781 
0782 /**
0783  * pm_wakeup_ws_event - Notify the PM core of a wakeup event.
0784  * @ws: Wakeup source object associated with the event source.
0785  * @msec: Anticipated event processing time (in milliseconds).
0786  * @hard: If set, abort suspends in progress and wake up from suspend-to-idle.
0787  *
0788  * Notify the PM core of a wakeup event whose source is @ws that will take
0789  * approximately @msec milliseconds to be processed by the kernel.  If @ws is
0790  * not active, activate it.  If @msec is nonzero, set up the @ws' timer to
0791  * execute pm_wakeup_timer_fn() in future.
0792  *
0793  * It is safe to call this function from interrupt context.
0794  */
0795 void pm_wakeup_ws_event(struct wakeup_source *ws, unsigned int msec, bool hard)
0796 {
0797     unsigned long flags;
0798     unsigned long expires;
0799 
0800     if (!ws)
0801         return;
0802 
0803     spin_lock_irqsave(&ws->lock, flags);
0804 
0805     wakeup_source_report_event(ws, hard);
0806 
0807     if (!msec) {
0808         wakeup_source_deactivate(ws);
0809         goto unlock;
0810     }
0811 
0812     expires = jiffies + msecs_to_jiffies(msec);
0813     if (!expires)
0814         expires = 1;
0815 
0816     if (!ws->timer_expires || time_after(expires, ws->timer_expires)) {
0817         mod_timer(&ws->timer, expires);
0818         ws->timer_expires = expires;
0819     }
0820 
0821  unlock:
0822     spin_unlock_irqrestore(&ws->lock, flags);
0823 }
0824 EXPORT_SYMBOL_GPL(pm_wakeup_ws_event);
0825 
0826 /**
0827  * pm_wakeup_dev_event - Notify the PM core of a wakeup event.
0828  * @dev: Device the wakeup event is related to.
0829  * @msec: Anticipated event processing time (in milliseconds).
0830  * @hard: If set, abort suspends in progress and wake up from suspend-to-idle.
0831  *
0832  * Call pm_wakeup_ws_event() for the @dev's wakeup source object.
0833  */
0834 void pm_wakeup_dev_event(struct device *dev, unsigned int msec, bool hard)
0835 {
0836     unsigned long flags;
0837 
0838     if (!dev)
0839         return;
0840 
0841     spin_lock_irqsave(&dev->power.lock, flags);
0842     pm_wakeup_ws_event(dev->power.wakeup, msec, hard);
0843     spin_unlock_irqrestore(&dev->power.lock, flags);
0844 }
0845 EXPORT_SYMBOL_GPL(pm_wakeup_dev_event);
0846 
0847 void pm_print_active_wakeup_sources(void)
0848 {
0849     struct wakeup_source *ws;
0850     int srcuidx, active = 0;
0851     struct wakeup_source *last_activity_ws = NULL;
0852 
0853     srcuidx = srcu_read_lock(&wakeup_srcu);
0854     list_for_each_entry_rcu_locked(ws, &wakeup_sources, entry) {
0855         if (ws->active) {
0856             pm_pr_dbg("active wakeup source: %s\n", ws->name);
0857             active = 1;
0858         } else if (!active &&
0859                (!last_activity_ws ||
0860                 ktime_to_ns(ws->last_time) >
0861                 ktime_to_ns(last_activity_ws->last_time))) {
0862             last_activity_ws = ws;
0863         }
0864     }
0865 
0866     if (!active && last_activity_ws)
0867         pm_pr_dbg("last active wakeup source: %s\n",
0868             last_activity_ws->name);
0869     srcu_read_unlock(&wakeup_srcu, srcuidx);
0870 }
0871 EXPORT_SYMBOL_GPL(pm_print_active_wakeup_sources);
0872 
0873 /**
0874  * pm_wakeup_pending - Check if power transition in progress should be aborted.
0875  *
0876  * Compare the current number of registered wakeup events with its preserved
0877  * value from the past and return true if new wakeup events have been registered
0878  * since the old value was stored.  Also return true if the current number of
0879  * wakeup events being processed is different from zero.
0880  */
0881 bool pm_wakeup_pending(void)
0882 {
0883     unsigned long flags;
0884     bool ret = false;
0885 
0886     raw_spin_lock_irqsave(&events_lock, flags);
0887     if (events_check_enabled) {
0888         unsigned int cnt, inpr;
0889 
0890         split_counters(&cnt, &inpr);
0891         ret = (cnt != saved_count || inpr > 0);
0892         events_check_enabled = !ret;
0893     }
0894     raw_spin_unlock_irqrestore(&events_lock, flags);
0895 
0896     if (ret) {
0897         pm_pr_dbg("Wakeup pending, aborting suspend\n");
0898         pm_print_active_wakeup_sources();
0899     }
0900 
0901     return ret || atomic_read(&pm_abort_suspend) > 0;
0902 }
0903 EXPORT_SYMBOL_GPL(pm_wakeup_pending);
0904 
0905 void pm_system_wakeup(void)
0906 {
0907     atomic_inc(&pm_abort_suspend);
0908     s2idle_wake();
0909 }
0910 EXPORT_SYMBOL_GPL(pm_system_wakeup);
0911 
0912 void pm_system_cancel_wakeup(void)
0913 {
0914     atomic_dec_if_positive(&pm_abort_suspend);
0915 }
0916 
0917 void pm_wakeup_clear(unsigned int irq_number)
0918 {
0919     raw_spin_lock_irq(&wakeup_irq_lock);
0920 
0921     if (irq_number && wakeup_irq[0] == irq_number)
0922         wakeup_irq[0] = wakeup_irq[1];
0923     else
0924         wakeup_irq[0] = 0;
0925 
0926     wakeup_irq[1] = 0;
0927 
0928     raw_spin_unlock_irq(&wakeup_irq_lock);
0929 
0930     if (!irq_number)
0931         atomic_set(&pm_abort_suspend, 0);
0932 }
0933 
0934 void pm_system_irq_wakeup(unsigned int irq_number)
0935 {
0936     unsigned long flags;
0937 
0938     raw_spin_lock_irqsave(&wakeup_irq_lock, flags);
0939 
0940     if (wakeup_irq[0] == 0)
0941         wakeup_irq[0] = irq_number;
0942     else if (wakeup_irq[1] == 0)
0943         wakeup_irq[1] = irq_number;
0944     else
0945         irq_number = 0;
0946 
0947     raw_spin_unlock_irqrestore(&wakeup_irq_lock, flags);
0948 
0949     if (irq_number)
0950         pm_system_wakeup();
0951 }
0952 
0953 unsigned int pm_wakeup_irq(void)
0954 {
0955     return wakeup_irq[0];
0956 }
0957 
0958 /**
0959  * pm_get_wakeup_count - Read the number of registered wakeup events.
0960  * @count: Address to store the value at.
0961  * @block: Whether or not to block.
0962  *
0963  * Store the number of registered wakeup events at the address in @count.  If
0964  * @block is set, block until the current number of wakeup events being
0965  * processed is zero.
0966  *
0967  * Return 'false' if the current number of wakeup events being processed is
0968  * nonzero.  Otherwise return 'true'.
0969  */
0970 bool pm_get_wakeup_count(unsigned int *count, bool block)
0971 {
0972     unsigned int cnt, inpr;
0973 
0974     if (block) {
0975         DEFINE_WAIT(wait);
0976 
0977         for (;;) {
0978             prepare_to_wait(&wakeup_count_wait_queue, &wait,
0979                     TASK_INTERRUPTIBLE);
0980             split_counters(&cnt, &inpr);
0981             if (inpr == 0 || signal_pending(current))
0982                 break;
0983             pm_print_active_wakeup_sources();
0984             schedule();
0985         }
0986         finish_wait(&wakeup_count_wait_queue, &wait);
0987     }
0988 
0989     split_counters(&cnt, &inpr);
0990     *count = cnt;
0991     return !inpr;
0992 }
0993 
0994 /**
0995  * pm_save_wakeup_count - Save the current number of registered wakeup events.
0996  * @count: Value to compare with the current number of registered wakeup events.
0997  *
0998  * If @count is equal to the current number of registered wakeup events and the
0999  * current number of wakeup events being processed is zero, store @count as the
1000  * old number of registered wakeup events for pm_check_wakeup_events(), enable
1001  * wakeup events detection and return 'true'.  Otherwise disable wakeup events
1002  * detection and return 'false'.
1003  */
1004 bool pm_save_wakeup_count(unsigned int count)
1005 {
1006     unsigned int cnt, inpr;
1007     unsigned long flags;
1008 
1009     events_check_enabled = false;
1010     raw_spin_lock_irqsave(&events_lock, flags);
1011     split_counters(&cnt, &inpr);
1012     if (cnt == count && inpr == 0) {
1013         saved_count = count;
1014         events_check_enabled = true;
1015     }
1016     raw_spin_unlock_irqrestore(&events_lock, flags);
1017     return events_check_enabled;
1018 }
1019 
1020 #ifdef CONFIG_PM_AUTOSLEEP
1021 /**
1022  * pm_wakep_autosleep_enabled - Modify autosleep_enabled for all wakeup sources.
1023  * @set: Whether to set or to clear the autosleep_enabled flags.
1024  */
1025 void pm_wakep_autosleep_enabled(bool set)
1026 {
1027     struct wakeup_source *ws;
1028     ktime_t now = ktime_get();
1029     int srcuidx;
1030 
1031     srcuidx = srcu_read_lock(&wakeup_srcu);
1032     list_for_each_entry_rcu_locked(ws, &wakeup_sources, entry) {
1033         spin_lock_irq(&ws->lock);
1034         if (ws->autosleep_enabled != set) {
1035             ws->autosleep_enabled = set;
1036             if (ws->active) {
1037                 if (set)
1038                     ws->start_prevent_time = now;
1039                 else
1040                     update_prevent_sleep_time(ws, now);
1041             }
1042         }
1043         spin_unlock_irq(&ws->lock);
1044     }
1045     srcu_read_unlock(&wakeup_srcu, srcuidx);
1046 }
1047 #endif /* CONFIG_PM_AUTOSLEEP */
1048 
1049 /**
1050  * print_wakeup_source_stats - Print wakeup source statistics information.
1051  * @m: seq_file to print the statistics into.
1052  * @ws: Wakeup source object to print the statistics for.
1053  */
1054 static int print_wakeup_source_stats(struct seq_file *m,
1055                      struct wakeup_source *ws)
1056 {
1057     unsigned long flags;
1058     ktime_t total_time;
1059     ktime_t max_time;
1060     unsigned long active_count;
1061     ktime_t active_time;
1062     ktime_t prevent_sleep_time;
1063 
1064     spin_lock_irqsave(&ws->lock, flags);
1065 
1066     total_time = ws->total_time;
1067     max_time = ws->max_time;
1068     prevent_sleep_time = ws->prevent_sleep_time;
1069     active_count = ws->active_count;
1070     if (ws->active) {
1071         ktime_t now = ktime_get();
1072 
1073         active_time = ktime_sub(now, ws->last_time);
1074         total_time = ktime_add(total_time, active_time);
1075         if (active_time > max_time)
1076             max_time = active_time;
1077 
1078         if (ws->autosleep_enabled)
1079             prevent_sleep_time = ktime_add(prevent_sleep_time,
1080                 ktime_sub(now, ws->start_prevent_time));
1081     } else {
1082         active_time = 0;
1083     }
1084 
1085     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",
1086            ws->name, active_count, ws->event_count,
1087            ws->wakeup_count, ws->expire_count,
1088            ktime_to_ms(active_time), ktime_to_ms(total_time),
1089            ktime_to_ms(max_time), ktime_to_ms(ws->last_time),
1090            ktime_to_ms(prevent_sleep_time));
1091 
1092     spin_unlock_irqrestore(&ws->lock, flags);
1093 
1094     return 0;
1095 }
1096 
1097 static void *wakeup_sources_stats_seq_start(struct seq_file *m,
1098                     loff_t *pos)
1099 {
1100     struct wakeup_source *ws;
1101     loff_t n = *pos;
1102     int *srcuidx = m->private;
1103 
1104     if (n == 0) {
1105         seq_puts(m, "name\t\tactive_count\tevent_count\twakeup_count\t"
1106             "expire_count\tactive_since\ttotal_time\tmax_time\t"
1107             "last_change\tprevent_suspend_time\n");
1108     }
1109 
1110     *srcuidx = srcu_read_lock(&wakeup_srcu);
1111     list_for_each_entry_rcu_locked(ws, &wakeup_sources, entry) {
1112         if (n-- <= 0)
1113             return ws;
1114     }
1115 
1116     return NULL;
1117 }
1118 
1119 static void *wakeup_sources_stats_seq_next(struct seq_file *m,
1120                     void *v, loff_t *pos)
1121 {
1122     struct wakeup_source *ws = v;
1123     struct wakeup_source *next_ws = NULL;
1124 
1125     ++(*pos);
1126 
1127     list_for_each_entry_continue_rcu(ws, &wakeup_sources, entry) {
1128         next_ws = ws;
1129         break;
1130     }
1131 
1132     if (!next_ws)
1133         print_wakeup_source_stats(m, &deleted_ws);
1134 
1135     return next_ws;
1136 }
1137 
1138 static void wakeup_sources_stats_seq_stop(struct seq_file *m, void *v)
1139 {
1140     int *srcuidx = m->private;
1141 
1142     srcu_read_unlock(&wakeup_srcu, *srcuidx);
1143 }
1144 
1145 /**
1146  * wakeup_sources_stats_seq_show - Print wakeup sources statistics information.
1147  * @m: seq_file to print the statistics into.
1148  * @v: wakeup_source of each iteration
1149  */
1150 static int wakeup_sources_stats_seq_show(struct seq_file *m, void *v)
1151 {
1152     struct wakeup_source *ws = v;
1153 
1154     print_wakeup_source_stats(m, ws);
1155 
1156     return 0;
1157 }
1158 
1159 static const struct seq_operations wakeup_sources_stats_seq_ops = {
1160     .start = wakeup_sources_stats_seq_start,
1161     .next  = wakeup_sources_stats_seq_next,
1162     .stop  = wakeup_sources_stats_seq_stop,
1163     .show  = wakeup_sources_stats_seq_show,
1164 };
1165 
1166 static int wakeup_sources_stats_open(struct inode *inode, struct file *file)
1167 {
1168     return seq_open_private(file, &wakeup_sources_stats_seq_ops, sizeof(int));
1169 }
1170 
1171 static const struct file_operations wakeup_sources_stats_fops = {
1172     .owner = THIS_MODULE,
1173     .open = wakeup_sources_stats_open,
1174     .read = seq_read,
1175     .llseek = seq_lseek,
1176     .release = seq_release_private,
1177 };
1178 
1179 static int __init wakeup_sources_debugfs_init(void)
1180 {
1181     debugfs_create_file("wakeup_sources", 0444, NULL, NULL,
1182                 &wakeup_sources_stats_fops);
1183     return 0;
1184 }
1185 
1186 postcore_initcall(wakeup_sources_debugfs_init);