Commit | Line | Data |
---|---|---|
0a0c5168 RW |
1 | /* |
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> | |
9bab0b7f | 12 | #include <linux/syscore_ops.h> |
0a0c5168 RW |
13 | |
14 | #include "internals.h" | |
15 | ||
cab303be TG |
16 | /* |
17 | * Called from __setup_irq() with desc->lock held after @action has | |
18 | * been installed in the action chain. | |
19 | */ | |
20 | void irq_pm_install_action(struct irq_desc *desc, struct irqaction *action) | |
21 | { | |
22 | desc->nr_actions++; | |
23 | ||
24 | if (action->flags & IRQF_FORCE_RESUME) | |
25 | desc->force_resume_depth++; | |
26 | ||
27 | WARN_ON_ONCE(desc->force_resume_depth && | |
28 | desc->force_resume_depth != desc->nr_actions); | |
29 | ||
30 | if (action->flags & IRQF_NO_SUSPEND) | |
31 | desc->no_suspend_depth++; | |
32 | ||
33 | WARN_ON_ONCE(desc->no_suspend_depth && | |
34 | desc->no_suspend_depth != desc->nr_actions); | |
35 | } | |
36 | ||
37 | /* | |
38 | * Called from __free_irq() with desc->lock held after @action has | |
39 | * been removed from the action chain. | |
40 | */ | |
41 | void irq_pm_remove_action(struct irq_desc *desc, struct irqaction *action) | |
42 | { | |
43 | desc->nr_actions--; | |
44 | ||
45 | if (action->flags & IRQF_FORCE_RESUME) | |
46 | desc->force_resume_depth--; | |
47 | ||
48 | if (action->flags & IRQF_NO_SUSPEND) | |
49 | desc->no_suspend_depth--; | |
50 | } | |
51 | ||
c4df606c | 52 | static bool suspend_device_irq(struct irq_desc *desc, int irq) |
8df2e02c | 53 | { |
5417de22 | 54 | if (!desc->action || desc->no_suspend_depth) |
c4df606c | 55 | return false; |
8df2e02c | 56 | |
b76f1674 TG |
57 | if (irqd_is_wakeup_set(&desc->irq_data)) |
58 | irqd_set(&desc->irq_data, IRQD_WAKEUP_ARMED); | |
59 | ||
8df2e02c TG |
60 | desc->istate |= IRQS_SUSPENDED; |
61 | __disable_irq(desc, irq); | |
092fadd5 TG |
62 | |
63 | /* | |
64 | * Hardware which has no wakeup source configuration facility | |
65 | * requires that the non wakeup interrupts are masked at the | |
66 | * chip level. The chip implementation indicates that with | |
67 | * IRQCHIP_MASK_ON_SUSPEND. | |
68 | */ | |
69 | if (irq_desc_get_chip(desc)->flags & IRQCHIP_MASK_ON_SUSPEND) | |
70 | mask_irq(desc); | |
c4df606c | 71 | return true; |
8df2e02c TG |
72 | } |
73 | ||
0a0c5168 RW |
74 | /** |
75 | * suspend_device_irqs - disable all currently enabled interrupt lines | |
76 | * | |
8df2e02c TG |
77 | * During system-wide suspend or hibernation device drivers need to be |
78 | * prevented from receiving interrupts and this function is provided | |
79 | * for this purpose. | |
80 | * | |
81 | * So we disable all interrupts and mark them IRQS_SUSPENDED except | |
82 | * for those which are unused and those which are marked as not | |
83 | * suspendable via an interrupt request with the flag IRQF_NO_SUSPEND | |
84 | * set. | |
0a0c5168 RW |
85 | */ |
86 | void suspend_device_irqs(void) | |
87 | { | |
88 | struct irq_desc *desc; | |
89 | int irq; | |
90 | ||
91 | for_each_irq_desc(irq, desc) { | |
92 | unsigned long flags; | |
c4df606c | 93 | bool sync; |
0a0c5168 | 94 | |
239007b8 | 95 | raw_spin_lock_irqsave(&desc->lock, flags); |
c4df606c | 96 | sync = suspend_device_irq(desc, irq); |
239007b8 | 97 | raw_spin_unlock_irqrestore(&desc->lock, flags); |
0a0c5168 | 98 | |
c4df606c | 99 | if (sync) |
0a0c5168 | 100 | synchronize_irq(irq); |
c4df606c | 101 | } |
0a0c5168 RW |
102 | } |
103 | EXPORT_SYMBOL_GPL(suspend_device_irqs); | |
104 | ||
8df2e02c TG |
105 | static void resume_irq(struct irq_desc *desc, int irq) |
106 | { | |
b76f1674 TG |
107 | irqd_clear(&desc->irq_data, IRQD_WAKEUP_ARMED); |
108 | ||
8df2e02c TG |
109 | if (desc->istate & IRQS_SUSPENDED) |
110 | goto resume; | |
111 | ||
5417de22 TG |
112 | /* Force resume the interrupt? */ |
113 | if (!desc->force_resume_depth) | |
8df2e02c TG |
114 | return; |
115 | ||
116 | /* Pretend that it got disabled ! */ | |
117 | desc->depth++; | |
118 | resume: | |
119 | desc->istate &= ~IRQS_SUSPENDED; | |
120 | __enable_irq(desc, irq); | |
121 | } | |
122 | ||
9bab0b7f | 123 | static void resume_irqs(bool want_early) |
0a0c5168 RW |
124 | { |
125 | struct irq_desc *desc; | |
126 | int irq; | |
127 | ||
128 | for_each_irq_desc(irq, desc) { | |
129 | unsigned long flags; | |
9bab0b7f IC |
130 | bool is_early = desc->action && |
131 | desc->action->flags & IRQF_EARLY_RESUME; | |
132 | ||
ac01810c | 133 | if (!is_early && want_early) |
9bab0b7f | 134 | continue; |
0a0c5168 | 135 | |
239007b8 | 136 | raw_spin_lock_irqsave(&desc->lock, flags); |
8df2e02c | 137 | resume_irq(desc, irq); |
239007b8 | 138 | raw_spin_unlock_irqrestore(&desc->lock, flags); |
0a0c5168 RW |
139 | } |
140 | } | |
9bab0b7f IC |
141 | |
142 | /** | |
143 | * irq_pm_syscore_ops - enable interrupt lines early | |
144 | * | |
145 | * Enable all interrupt lines with %IRQF_EARLY_RESUME set. | |
146 | */ | |
147 | static void irq_pm_syscore_resume(void) | |
148 | { | |
149 | resume_irqs(true); | |
150 | } | |
151 | ||
152 | static struct syscore_ops irq_pm_syscore_ops = { | |
153 | .resume = irq_pm_syscore_resume, | |
154 | }; | |
155 | ||
156 | static int __init irq_pm_init_ops(void) | |
157 | { | |
158 | register_syscore_ops(&irq_pm_syscore_ops); | |
159 | return 0; | |
160 | } | |
161 | ||
162 | device_initcall(irq_pm_init_ops); | |
163 | ||
164 | /** | |
165 | * resume_device_irqs - enable interrupt lines disabled by suspend_device_irqs() | |
166 | * | |
167 | * Enable all non-%IRQF_EARLY_RESUME interrupt lines previously | |
168 | * disabled by suspend_device_irqs() that have the IRQS_SUSPENDED flag | |
169 | * set as well as those with %IRQF_FORCE_RESUME. | |
170 | */ | |
171 | void resume_device_irqs(void) | |
172 | { | |
173 | resume_irqs(false); | |
174 | } | |
0a0c5168 RW |
175 | EXPORT_SYMBOL_GPL(resume_device_irqs); |
176 | ||
177 | /** | |
178 | * check_wakeup_irqs - check if any wake-up interrupts are pending | |
179 | */ | |
180 | int check_wakeup_irqs(void) | |
181 | { | |
182 | struct irq_desc *desc; | |
183 | int irq; | |
184 | ||
d209a699 | 185 | for_each_irq_desc(irq, desc) { |
9c6079aa TG |
186 | /* |
187 | * Only interrupts which are marked as wakeup source | |
188 | * and have not been disabled before the suspend check | |
189 | * can abort suspend. | |
190 | */ | |
d209a699 | 191 | if (irqd_is_wakeup_set(&desc->irq_data)) { |
9c6079aa | 192 | if (desc->depth == 1 && desc->istate & IRQS_PENDING) |
d209a699 | 193 | return -EBUSY; |
d209a699 | 194 | } |
d209a699 | 195 | } |
0a0c5168 RW |
196 | |
197 | return 0; | |
198 | } |