blob: a21b3dc9825a3f7a619f1bd91a8ec2a826673f93 [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>
Ian Campbell9bab0b72011-10-03 15:37:00 +010012#include <linux/syscore_ops.h>
Rafael J. Wysocki0a0c5162009-03-16 22:33:49 +010013
14#include "internals.h"
15
Thomas Gleixnercab303b2014-08-28 11:44:31 +020016/*
17 * Called from __setup_irq() with desc->lock held after @action has
18 * been installed in the action chain.
19 */
20void 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 */
41void 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
Thomas Gleixner8df2e022014-08-28 11:49:28 +020052static void suspend_device_irq(struct irq_desc *desc, int irq)
53{
Thomas Gleixner5417de22014-08-28 15:48:59 +020054 if (!desc->action || desc->no_suspend_depth)
Thomas Gleixner8df2e022014-08-28 11:49:28 +020055 return;
56
57 desc->istate |= IRQS_SUSPENDED;
58 __disable_irq(desc, irq);
Thomas Gleixner092fadd2014-08-28 16:49:43 +020059
60 /*
61 * Hardware which has no wakeup source configuration facility
62 * requires that the non wakeup interrupts are masked at the
63 * chip level. The chip implementation indicates that with
64 * IRQCHIP_MASK_ON_SUSPEND.
65 */
66 if (irq_desc_get_chip(desc)->flags & IRQCHIP_MASK_ON_SUSPEND)
67 mask_irq(desc);
Thomas Gleixner8df2e022014-08-28 11:49:28 +020068}
69
Rafael J. Wysocki0a0c5162009-03-16 22:33:49 +010070/**
71 * suspend_device_irqs - disable all currently enabled interrupt lines
72 *
Thomas Gleixner8df2e022014-08-28 11:49:28 +020073 * During system-wide suspend or hibernation device drivers need to be
74 * prevented from receiving interrupts and this function is provided
75 * for this purpose.
76 *
77 * So we disable all interrupts and mark them IRQS_SUSPENDED except
78 * for those which are unused and those which are marked as not
79 * suspendable via an interrupt request with the flag IRQF_NO_SUSPEND
80 * set.
Rafael J. Wysocki0a0c5162009-03-16 22:33:49 +010081 */
82void suspend_device_irqs(void)
83{
84 struct irq_desc *desc;
85 int irq;
86
87 for_each_irq_desc(irq, desc) {
88 unsigned long flags;
89
Thomas Gleixner239007b2009-11-17 16:46:45 +010090 raw_spin_lock_irqsave(&desc->lock, flags);
Thomas Gleixner8df2e022014-08-28 11:49:28 +020091 suspend_device_irq(desc, irq);
Thomas Gleixner239007b2009-11-17 16:46:45 +010092 raw_spin_unlock_irqrestore(&desc->lock, flags);
Rafael J. Wysocki0a0c5162009-03-16 22:33:49 +010093 }
94
95 for_each_irq_desc(irq, desc)
Thomas Gleixnerc531e832011-02-08 12:44:58 +010096 if (desc->istate & IRQS_SUSPENDED)
Rafael J. Wysocki0a0c5162009-03-16 22:33:49 +010097 synchronize_irq(irq);
98}
99EXPORT_SYMBOL_GPL(suspend_device_irqs);
100
Thomas Gleixner8df2e022014-08-28 11:49:28 +0200101static void resume_irq(struct irq_desc *desc, int irq)
102{
103 if (desc->istate & IRQS_SUSPENDED)
104 goto resume;
105
Thomas Gleixner5417de22014-08-28 15:48:59 +0200106 /* Force resume the interrupt? */
107 if (!desc->force_resume_depth)
Thomas Gleixner8df2e022014-08-28 11:49:28 +0200108 return;
109
110 /* Pretend that it got disabled ! */
111 desc->depth++;
112resume:
113 desc->istate &= ~IRQS_SUSPENDED;
114 __enable_irq(desc, irq);
115}
116
Ian Campbell9bab0b72011-10-03 15:37:00 +0100117static void resume_irqs(bool want_early)
Rafael J. Wysocki0a0c5162009-03-16 22:33:49 +0100118{
119 struct irq_desc *desc;
120 int irq;
121
122 for_each_irq_desc(irq, desc) {
123 unsigned long flags;
Ian Campbell9bab0b72011-10-03 15:37:00 +0100124 bool is_early = desc->action &&
125 desc->action->flags & IRQF_EARLY_RESUME;
126
Laxman Dewanganac018102013-11-25 19:39:47 +0530127 if (!is_early && want_early)
Ian Campbell9bab0b72011-10-03 15:37:00 +0100128 continue;
Rafael J. Wysocki0a0c5162009-03-16 22:33:49 +0100129
Thomas Gleixner239007b2009-11-17 16:46:45 +0100130 raw_spin_lock_irqsave(&desc->lock, flags);
Thomas Gleixner8df2e022014-08-28 11:49:28 +0200131 resume_irq(desc, irq);
Thomas Gleixner239007b2009-11-17 16:46:45 +0100132 raw_spin_unlock_irqrestore(&desc->lock, flags);
Rafael J. Wysocki0a0c5162009-03-16 22:33:49 +0100133 }
134}
Ian Campbell9bab0b72011-10-03 15:37:00 +0100135
136/**
137 * irq_pm_syscore_ops - enable interrupt lines early
138 *
139 * Enable all interrupt lines with %IRQF_EARLY_RESUME set.
140 */
141static void irq_pm_syscore_resume(void)
142{
143 resume_irqs(true);
144}
145
146static struct syscore_ops irq_pm_syscore_ops = {
147 .resume = irq_pm_syscore_resume,
148};
149
150static int __init irq_pm_init_ops(void)
151{
152 register_syscore_ops(&irq_pm_syscore_ops);
153 return 0;
154}
155
156device_initcall(irq_pm_init_ops);
157
158/**
159 * resume_device_irqs - enable interrupt lines disabled by suspend_device_irqs()
160 *
161 * Enable all non-%IRQF_EARLY_RESUME interrupt lines previously
162 * disabled by suspend_device_irqs() that have the IRQS_SUSPENDED flag
163 * set as well as those with %IRQF_FORCE_RESUME.
164 */
165void resume_device_irqs(void)
166{
167 resume_irqs(false);
168}
Rafael J. Wysocki0a0c5162009-03-16 22:33:49 +0100169EXPORT_SYMBOL_GPL(resume_device_irqs);
170
171/**
172 * check_wakeup_irqs - check if any wake-up interrupts are pending
173 */
174int check_wakeup_irqs(void)
175{
176 struct irq_desc *desc;
177 int irq;
178
Thomas Gleixnerd209a692011-03-11 21:22:14 +0100179 for_each_irq_desc(irq, desc) {
Thomas Gleixner9c6079a2012-05-04 17:56:16 +0200180 /*
181 * Only interrupts which are marked as wakeup source
182 * and have not been disabled before the suspend check
183 * can abort suspend.
184 */
Thomas Gleixnerd209a692011-03-11 21:22:14 +0100185 if (irqd_is_wakeup_set(&desc->irq_data)) {
Thomas Gleixner9c6079a2012-05-04 17:56:16 +0200186 if (desc->depth == 1 && desc->istate & IRQS_PENDING)
Thomas Gleixnerd209a692011-03-11 21:22:14 +0100187 return -EBUSY;
Thomas Gleixnerd209a692011-03-11 21:22:14 +0100188 }
Thomas Gleixnerd209a692011-03-11 21:22:14 +0100189 }
Rafael J. Wysocki0a0c5162009-03-16 22:33:49 +0100190
191 return 0;
192}