blob: cea1de0161f18a164aebb84e275ad67d5899eda8 [file] [log] [blame]
Rafael J. Wysocki0a0c5162009-03-16 22:33:49 +01001/*
2 * linux/kernel/irq/pm.c
3 *
4 * Copyright (C) 2009 Rafael J. Wysocki <rjw@sisk.pl>, Novell Inc.
5 *
6 * This file contains power management functions related to interrupts.
7 */
8
9#include <linux/irq.h>
10#include <linux/module.h>
11#include <linux/interrupt.h>
Thomas Gleixner9ce7a252014-08-29 14:00:16 +020012#include <linux/suspend.h>
Ian Campbell9bab0b72011-10-03 15:37:00 +010013#include <linux/syscore_ops.h>
Rafael J. Wysocki0a0c5162009-03-16 22:33:49 +010014
15#include "internals.h"
16
Thomas Gleixner9ce7a252014-08-29 14:00:16 +020017bool irq_pm_check_wakeup(struct irq_desc *desc)
18{
19 if (irqd_is_wakeup_armed(&desc->irq_data)) {
20 irqd_clear(&desc->irq_data, IRQD_WAKEUP_ARMED);
21 desc->istate |= IRQS_SUSPENDED | IRQS_PENDING;
22 desc->depth++;
23 irq_disable(desc);
Alexandra Yatesa6f5f0d2015-09-15 10:32:46 -070024 pm_system_irq_wakeup(irq_desc_get_irq(desc));
Thomas Gleixner9ce7a252014-08-29 14:00:16 +020025 return true;
26 }
27 return false;
28}
29
Thomas Gleixnercab303b2014-08-28 11:44:31 +020030/*
31 * Called from __setup_irq() with desc->lock held after @action has
32 * been installed in the action chain.
33 */
34void irq_pm_install_action(struct irq_desc *desc, struct irqaction *action)
35{
36 desc->nr_actions++;
37
38 if (action->flags & IRQF_FORCE_RESUME)
39 desc->force_resume_depth++;
40
41 WARN_ON_ONCE(desc->force_resume_depth &&
42 desc->force_resume_depth != desc->nr_actions);
43
44 if (action->flags & IRQF_NO_SUSPEND)
45 desc->no_suspend_depth++;
Rafael J. Wysocki17f48032015-02-27 00:07:55 +010046 else if (action->flags & IRQF_COND_SUSPEND)
47 desc->cond_suspend_depth++;
Thomas Gleixnercab303b2014-08-28 11:44:31 +020048
49 WARN_ON_ONCE(desc->no_suspend_depth &&
Rafael J. Wysocki17f48032015-02-27 00:07:55 +010050 (desc->no_suspend_depth +
51 desc->cond_suspend_depth) != desc->nr_actions);
Thomas Gleixnercab303b2014-08-28 11:44:31 +020052}
53
54/*
55 * Called from __free_irq() with desc->lock held after @action has
56 * been removed from the action chain.
57 */
58void irq_pm_remove_action(struct irq_desc *desc, struct irqaction *action)
59{
60 desc->nr_actions--;
61
62 if (action->flags & IRQF_FORCE_RESUME)
63 desc->force_resume_depth--;
64
65 if (action->flags & IRQF_NO_SUSPEND)
66 desc->no_suspend_depth--;
Rafael J. Wysocki17f48032015-02-27 00:07:55 +010067 else if (action->flags & IRQF_COND_SUSPEND)
68 desc->cond_suspend_depth--;
Thomas Gleixnercab303b2014-08-28 11:44:31 +020069}
70
Jiang Liub80f5f32015-06-23 19:58:45 +020071static bool suspend_device_irq(struct irq_desc *desc)
Thomas Gleixner8df2e022014-08-28 11:49:28 +020072{
Grygorii Strashko4717f132015-11-10 11:58:12 +020073 if (!desc->action || irq_desc_is_chained(desc) ||
74 desc->no_suspend_depth)
Thomas Gleixnerc4df6062014-08-28 22:50:43 +020075 return false;
Thomas Gleixner8df2e022014-08-28 11:49:28 +020076
Thomas Gleixner9ce7a252014-08-29 14:00:16 +020077 if (irqd_is_wakeup_set(&desc->irq_data)) {
Thomas Gleixnerb76f1672014-08-29 13:54:09 +020078 irqd_set(&desc->irq_data, IRQD_WAKEUP_ARMED);
Thomas Gleixner9ce7a252014-08-29 14:00:16 +020079 /*
80 * We return true here to force the caller to issue
81 * synchronize_irq(). We need to make sure that the
82 * IRQD_WAKEUP_ARMED is visible before we return from
83 * suspend_device_irqs().
84 */
85 return true;
86 }
Thomas Gleixnerb76f1672014-08-29 13:54:09 +020087
Thomas Gleixner8df2e022014-08-28 11:49:28 +020088 desc->istate |= IRQS_SUSPENDED;
Jiang Liu79ff1cd2015-06-23 19:52:36 +020089 __disable_irq(desc);
Thomas Gleixner092fadd2014-08-28 16:49:43 +020090
91 /*
92 * Hardware which has no wakeup source configuration facility
93 * requires that the non wakeup interrupts are masked at the
94 * chip level. The chip implementation indicates that with
95 * IRQCHIP_MASK_ON_SUSPEND.
96 */
97 if (irq_desc_get_chip(desc)->flags & IRQCHIP_MASK_ON_SUSPEND)
98 mask_irq(desc);
Thomas Gleixnerc4df6062014-08-28 22:50:43 +020099 return true;
Thomas Gleixner8df2e022014-08-28 11:49:28 +0200100}
101
Rafael J. Wysocki0a0c5162009-03-16 22:33:49 +0100102/**
103 * suspend_device_irqs - disable all currently enabled interrupt lines
104 *
Thomas Gleixner8df2e022014-08-28 11:49:28 +0200105 * During system-wide suspend or hibernation device drivers need to be
106 * prevented from receiving interrupts and this function is provided
107 * for this purpose.
108 *
109 * So we disable all interrupts and mark them IRQS_SUSPENDED except
Thomas Gleixner9ce7a252014-08-29 14:00:16 +0200110 * for those which are unused, those which are marked as not
Thomas Gleixner8df2e022014-08-28 11:49:28 +0200111 * suspendable via an interrupt request with the flag IRQF_NO_SUSPEND
Thomas Gleixner9ce7a252014-08-29 14:00:16 +0200112 * set and those which are marked as active wakeup sources.
113 *
114 * The active wakeup sources are handled by the flow handler entry
115 * code which checks for the IRQD_WAKEUP_ARMED flag, suspends the
116 * interrupt and notifies the pm core about the wakeup.
Rafael J. Wysocki0a0c5162009-03-16 22:33:49 +0100117 */
118void suspend_device_irqs(void)
119{
120 struct irq_desc *desc;
121 int irq;
122
123 for_each_irq_desc(irq, desc) {
124 unsigned long flags;
Thomas Gleixnerc4df6062014-08-28 22:50:43 +0200125 bool sync;
Rafael J. Wysocki0a0c5162009-03-16 22:33:49 +0100126
NeilBrown3c646f22015-05-17 15:19:34 +1000127 if (irq_settings_is_nested_thread(desc))
128 continue;
Thomas Gleixner239007b2009-11-17 16:46:45 +0100129 raw_spin_lock_irqsave(&desc->lock, flags);
Jiang Liub80f5f32015-06-23 19:58:45 +0200130 sync = suspend_device_irq(desc);
Thomas Gleixner239007b2009-11-17 16:46:45 +0100131 raw_spin_unlock_irqrestore(&desc->lock, flags);
Rafael J. Wysocki0a0c5162009-03-16 22:33:49 +0100132
Thomas Gleixnerc4df6062014-08-28 22:50:43 +0200133 if (sync)
Rafael J. Wysocki0a0c5162009-03-16 22:33:49 +0100134 synchronize_irq(irq);
Thomas Gleixnerc4df6062014-08-28 22:50:43 +0200135 }
Rafael J. Wysocki0a0c5162009-03-16 22:33:49 +0100136}
137EXPORT_SYMBOL_GPL(suspend_device_irqs);
138
Jiang Liub80f5f32015-06-23 19:58:45 +0200139static void resume_irq(struct irq_desc *desc)
Thomas Gleixner8df2e022014-08-28 11:49:28 +0200140{
Thomas Gleixnerb76f1672014-08-29 13:54:09 +0200141 irqd_clear(&desc->irq_data, IRQD_WAKEUP_ARMED);
142
Thomas Gleixner8df2e022014-08-28 11:49:28 +0200143 if (desc->istate & IRQS_SUSPENDED)
144 goto resume;
145
Thomas Gleixner5417de22014-08-28 15:48:59 +0200146 /* Force resume the interrupt? */
147 if (!desc->force_resume_depth)
Thomas Gleixner8df2e022014-08-28 11:49:28 +0200148 return;
149
150 /* Pretend that it got disabled ! */
151 desc->depth++;
152resume:
153 desc->istate &= ~IRQS_SUSPENDED;
Jiang Liu79ff1cd2015-06-23 19:52:36 +0200154 __enable_irq(desc);
Thomas Gleixner8df2e022014-08-28 11:49:28 +0200155}
156
Ian Campbell9bab0b72011-10-03 15:37:00 +0100157static void resume_irqs(bool want_early)
Rafael J. Wysocki0a0c5162009-03-16 22:33:49 +0100158{
159 struct irq_desc *desc;
160 int irq;
161
162 for_each_irq_desc(irq, desc) {
163 unsigned long flags;
Ian Campbell9bab0b72011-10-03 15:37:00 +0100164 bool is_early = desc->action &&
165 desc->action->flags & IRQF_EARLY_RESUME;
166
Laxman Dewanganac018102013-11-25 19:39:47 +0530167 if (!is_early && want_early)
Ian Campbell9bab0b72011-10-03 15:37:00 +0100168 continue;
NeilBrown3c646f22015-05-17 15:19:34 +1000169 if (irq_settings_is_nested_thread(desc))
170 continue;
Rafael J. Wysocki0a0c5162009-03-16 22:33:49 +0100171
Thomas Gleixner239007b2009-11-17 16:46:45 +0100172 raw_spin_lock_irqsave(&desc->lock, flags);
Jiang Liub80f5f32015-06-23 19:58:45 +0200173 resume_irq(desc);
Thomas Gleixner239007b2009-11-17 16:46:45 +0100174 raw_spin_unlock_irqrestore(&desc->lock, flags);
Rafael J. Wysocki0a0c5162009-03-16 22:33:49 +0100175 }
176}
Ian Campbell9bab0b72011-10-03 15:37:00 +0100177
178/**
179 * irq_pm_syscore_ops - enable interrupt lines early
180 *
181 * Enable all interrupt lines with %IRQF_EARLY_RESUME set.
182 */
183static void irq_pm_syscore_resume(void)
184{
185 resume_irqs(true);
186}
187
188static struct syscore_ops irq_pm_syscore_ops = {
189 .resume = irq_pm_syscore_resume,
190};
191
192static int __init irq_pm_init_ops(void)
193{
194 register_syscore_ops(&irq_pm_syscore_ops);
195 return 0;
196}
197
198device_initcall(irq_pm_init_ops);
199
200/**
201 * resume_device_irqs - enable interrupt lines disabled by suspend_device_irqs()
202 *
203 * Enable all non-%IRQF_EARLY_RESUME interrupt lines previously
204 * disabled by suspend_device_irqs() that have the IRQS_SUSPENDED flag
205 * set as well as those with %IRQF_FORCE_RESUME.
206 */
207void resume_device_irqs(void)
208{
209 resume_irqs(false);
210}
Rafael J. Wysocki0a0c5162009-03-16 22:33:49 +0100211EXPORT_SYMBOL_GPL(resume_device_irqs);