Skip to content

Commit e4cb0c9

Browse files
committed
Merge branch 'pm-genirq' into acpi-pm
2 parents 905563f + 27f3d18 commit e4cb0c9

14 files changed

Lines changed: 415 additions & 117 deletions

File tree

Lines changed: 123 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,123 @@
1+
System Suspend and Device Interrupts
2+
3+
Copyright (C) 2014 Intel Corp.
4+
Author: Rafael J. Wysocki <rafael.j.wysocki@intel.com>
5+
6+
7+
Suspending and Resuming Device IRQs
8+
-----------------------------------
9+
10+
Device interrupt request lines (IRQs) are generally disabled during system
11+
suspend after the "late" phase of suspending devices (that is, after all of the
12+
->prepare, ->suspend and ->suspend_late callbacks have been executed for all
13+
devices). That is done by suspend_device_irqs().
14+
15+
The rationale for doing so is that after the "late" phase of device suspend
16+
there is no legitimate reason why any interrupts from suspended devices should
17+
trigger and if any devices have not been suspended properly yet, it is better to
18+
block interrupts from them anyway. Also, in the past we had problems with
19+
interrupt handlers for shared IRQs that device drivers implementing them were
20+
not prepared for interrupts triggering after their devices had been suspended.
21+
In some cases they would attempt to access, for example, memory address spaces
22+
of suspended devices and cause unpredictable behavior to ensue as a result.
23+
Unfortunately, such problems are very difficult to debug and the introduction
24+
of suspend_device_irqs(), along with the "noirq" phase of device suspend and
25+
resume, was the only practical way to mitigate them.
26+
27+
Device IRQs are re-enabled during system resume, right before the "early" phase
28+
of resuming devices (that is, before starting to execute ->resume_early
29+
callbacks for devices). The function doing that is resume_device_irqs().
30+
31+
32+
The IRQF_NO_SUSPEND Flag
33+
------------------------
34+
35+
There are interrupts that can legitimately trigger during the entire system
36+
suspend-resume cycle, including the "noirq" phases of suspending and resuming
37+
devices as well as during the time when nonboot CPUs are taken offline and
38+
brought back online. That applies to timer interrupts in the first place,
39+
but also to IPIs and to some other special-purpose interrupts.
40+
41+
The IRQF_NO_SUSPEND flag is used to indicate that to the IRQ subsystem when
42+
requesting a special-purpose interrupt. It causes suspend_device_irqs() to
43+
leave the corresponding IRQ enabled so as to allow the interrupt to work all
44+
the time as expected.
45+
46+
Note that the IRQF_NO_SUSPEND flag affects the entire IRQ and not just one
47+
user of it. Thus, if the IRQ is shared, all of the interrupt handlers installed
48+
for it will be executed as usual after suspend_device_irqs(), even if the
49+
IRQF_NO_SUSPEND flag was not passed to request_irq() (or equivalent) by some of
50+
the IRQ's users. For this reason, using IRQF_NO_SUSPEND and IRQF_SHARED at the
51+
same time should be avoided.
52+
53+
54+
System Wakeup Interrupts, enable_irq_wake() and disable_irq_wake()
55+
------------------------------------------------------------------
56+
57+
System wakeup interrupts generally need to be configured to wake up the system
58+
from sleep states, especially if they are used for different purposes (e.g. as
59+
I/O interrupts) in the working state.
60+
61+
That may involve turning on a special signal handling logic within the platform
62+
(such as an SoC) so that signals from a given line are routed in a different way
63+
during system sleep so as to trigger a system wakeup when needed. For example,
64+
the platform may include a dedicated interrupt controller used specifically for
65+
handling system wakeup events. Then, if a given interrupt line is supposed to
66+
wake up the system from sleep sates, the corresponding input of that interrupt
67+
controller needs to be enabled to receive signals from the line in question.
68+
After wakeup, it generally is better to disable that input to prevent the
69+
dedicated controller from triggering interrupts unnecessarily.
70+
71+
The IRQ subsystem provides two helper functions to be used by device drivers for
72+
those purposes. Namely, enable_irq_wake() turns on the platform's logic for
73+
handling the given IRQ as a system wakeup interrupt line and disable_irq_wake()
74+
turns that logic off.
75+
76+
Calling enable_irq_wake() causes suspend_device_irqs() to treat the given IRQ
77+
in a special way. Namely, the IRQ remains enabled, by on the first interrupt
78+
it will be disabled, marked as pending and "suspended" so that it will be
79+
re-enabled by resume_device_irqs() during the subsequent system resume. Also
80+
the PM core is notified about the event which casues the system suspend in
81+
progress to be aborted (that doesn't have to happen immediately, but at one
82+
of the points where the suspend thread looks for pending wakeup events).
83+
84+
This way every interrupt from a wakeup interrupt source will either cause the
85+
system suspend currently in progress to be aborted or wake up the system if
86+
already suspended. However, after suspend_device_irqs() interrupt handlers are
87+
not executed for system wakeup IRQs. They are only executed for IRQF_NO_SUSPEND
88+
IRQs at that time, but those IRQs should not be configured for system wakeup
89+
using enable_irq_wake().
90+
91+
92+
Interrupts and Suspend-to-Idle
93+
------------------------------
94+
95+
Suspend-to-idle (also known as the "freeze" sleep state) is a relatively new
96+
system sleep state that works by idling all of the processors and waiting for
97+
interrupts right after the "noirq" phase of suspending devices.
98+
99+
Of course, this means that all of the interrupts with the IRQF_NO_SUSPEND flag
100+
set will bring CPUs out of idle while in that state, but they will not cause the
101+
IRQ subsystem to trigger a system wakeup.
102+
103+
System wakeup interrupts, in turn, will trigger wakeup from suspend-to-idle in
104+
analogy with what they do in the full system suspend case. The only difference
105+
is that the wakeup from suspend-to-idle is signaled using the usual working
106+
state interrupt delivery mechanisms and doesn't require the platform to use
107+
any special interrupt handling logic for it to work.
108+
109+
110+
IRQF_NO_SUSPEND and enable_irq_wake()
111+
-------------------------------------
112+
113+
There are no valid reasons to use both enable_irq_wake() and the IRQF_NO_SUSPEND
114+
flag on the same IRQ.
115+
116+
First of all, if the IRQ is not shared, the rules for handling IRQF_NO_SUSPEND
117+
interrupts (interrupt handlers are invoked after suspend_device_irqs()) are
118+
directly at odds with the rules for handling system wakeup interrupts (interrupt
119+
handlers are not invoked after suspend_device_irqs()).
120+
121+
Second, both enable_irq_wake() and IRQF_NO_SUSPEND apply to entire IRQs and not
122+
to individual interrupt handlers, so sharing an IRQ between a system wakeup
123+
interrupt source and an IRQF_NO_SUSPEND interrupt source does not make sense.

‎arch/x86/kernel/apic/io_apic.c‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2623,6 +2623,7 @@ static struct irq_chip ioapic_chip __read_mostly = {
26232623
.irq_eoi = ack_apic_level,
26242624
.irq_set_affinity = native_ioapic_set_affinity,
26252625
.irq_retrigger = ioapic_retrigger_irq,
2626+
.flags = IRQCHIP_SKIP_SET_WAKE,
26262627
};
26272628

26282629
static inline void init_IO_APIC_traps(void)
@@ -3173,6 +3174,7 @@ static struct irq_chip msi_chip = {
31733174
.irq_ack = ack_apic_edge,
31743175
.irq_set_affinity = msi_set_affinity,
31753176
.irq_retrigger = ioapic_retrigger_irq,
3177+
.flags = IRQCHIP_SKIP_SET_WAKE,
31763178
};
31773179

31783180
int setup_msi_irq(struct pci_dev *dev, struct msi_desc *msidesc,
@@ -3271,6 +3273,7 @@ static struct irq_chip dmar_msi_type = {
32713273
.irq_ack = ack_apic_edge,
32723274
.irq_set_affinity = dmar_msi_set_affinity,
32733275
.irq_retrigger = ioapic_retrigger_irq,
3276+
.flags = IRQCHIP_SKIP_SET_WAKE,
32743277
};
32753278

32763279
int arch_setup_dmar_msi(unsigned int irq)
@@ -3321,6 +3324,7 @@ static struct irq_chip hpet_msi_type = {
33213324
.irq_ack = ack_apic_edge,
33223325
.irq_set_affinity = hpet_msi_set_affinity,
33233326
.irq_retrigger = ioapic_retrigger_irq,
3327+
.flags = IRQCHIP_SKIP_SET_WAKE,
33243328
};
33253329

33263330
int default_setup_hpet_msi(unsigned int irq, unsigned int id)
@@ -3384,6 +3388,7 @@ static struct irq_chip ht_irq_chip = {
33843388
.irq_ack = ack_apic_edge,
33853389
.irq_set_affinity = ht_set_affinity,
33863390
.irq_retrigger = ioapic_retrigger_irq,
3391+
.flags = IRQCHIP_SKIP_SET_WAKE,
33873392
};
33883393

33893394
int arch_setup_ht_irq(unsigned int irq, struct pci_dev *dev)

‎drivers/base/power/wakeup.c‎

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,9 @@
2424
*/
2525
bool events_check_enabled __read_mostly;
2626

27+
/* If set and the system is suspending, terminate the suspend. */
28+
static bool pm_abort_suspend __read_mostly;
29+
2730
/*
2831
* Combined counters of registered wakeup events and wakeup events in progress.
2932
* They need to be modified together atomically, so it's better to use one
@@ -719,7 +722,18 @@ bool pm_wakeup_pending(void)
719722
pm_print_active_wakeup_sources();
720723
}
721724

722-
return ret;
725+
return ret || pm_abort_suspend;
726+
}
727+
728+
void pm_system_wakeup(void)
729+
{
730+
pm_abort_suspend = true;
731+
freeze_wake();
732+
}
733+
734+
void pm_wakeup_clear(void)
735+
{
736+
pm_abort_suspend = false;
723737
}
724738

725739
/**

‎drivers/base/syscore.c‎

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
#include <linux/syscore_ops.h>
1010
#include <linux/mutex.h>
1111
#include <linux/module.h>
12-
#include <linux/interrupt.h>
12+
#include <linux/suspend.h>
1313
#include <trace/events/power.h>
1414

1515
static LIST_HEAD(syscore_ops_list);
@@ -54,9 +54,8 @@ int syscore_suspend(void)
5454
pr_debug("Checking wakeup interrupts\n");
5555

5656
/* Return error code if there are any wakeup interrupts pending. */
57-
ret = check_wakeup_irqs();
58-
if (ret)
59-
return ret;
57+
if (pm_wakeup_pending())
58+
return -EBUSY;
6059

6160
WARN_ONCE(!irqs_disabled(),
6261
"Interrupts enabled before system core suspend.\n");

‎drivers/pci/pcie/pme.c‎

Lines changed: 51 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -41,11 +41,17 @@ static int __init pcie_pme_setup(char *str)
4141
}
4242
__setup("pcie_pme=", pcie_pme_setup);
4343

44+
enum pme_suspend_level {
45+
PME_SUSPEND_NONE = 0,
46+
PME_SUSPEND_WAKEUP,
47+
PME_SUSPEND_NOIRQ,
48+
};
49+
4450
struct pcie_pme_service_data {
4551
spinlock_t lock;
4652
struct pcie_device *srv;
4753
struct work_struct work;
48-
bool noirq; /* Don't enable the PME interrupt used by this service. */
54+
enum pme_suspend_level suspend_level;
4955
};
5056

5157
/**
@@ -223,7 +229,7 @@ static void pcie_pme_work_fn(struct work_struct *work)
223229
spin_lock_irq(&data->lock);
224230

225231
for (;;) {
226-
if (data->noirq)
232+
if (data->suspend_level != PME_SUSPEND_NONE)
227233
break;
228234

229235
pcie_capability_read_dword(port, PCI_EXP_RTSTA, &rtsta);
@@ -250,7 +256,7 @@ static void pcie_pme_work_fn(struct work_struct *work)
250256
spin_lock_irq(&data->lock);
251257
}
252258

253-
if (!data->noirq)
259+
if (data->suspend_level == PME_SUSPEND_NONE)
254260
pcie_pme_interrupt_enable(port, true);
255261

256262
spin_unlock_irq(&data->lock);
@@ -367,6 +373,21 @@ static int pcie_pme_probe(struct pcie_device *srv)
367373
return ret;
368374
}
369375

376+
static bool pcie_pme_check_wakeup(struct pci_bus *bus)
377+
{
378+
struct pci_dev *dev;
379+
380+
if (!bus)
381+
return false;
382+
383+
list_for_each_entry(dev, &bus->devices, bus_list)
384+
if (device_may_wakeup(&dev->dev)
385+
|| pcie_pme_check_wakeup(dev->subordinate))
386+
return true;
387+
388+
return false;
389+
}
390+
370391
/**
371392
* pcie_pme_suspend - Suspend PCIe PME service device.
372393
* @srv: PCIe service device to suspend.
@@ -375,11 +396,26 @@ static int pcie_pme_suspend(struct pcie_device *srv)
375396
{
376397
struct pcie_pme_service_data *data = get_service_data(srv);
377398
struct pci_dev *port = srv->port;
399+
bool wakeup;
378400

401+
if (device_may_wakeup(&port->dev)) {
402+
wakeup = true;
403+
} else {
404+
down_read(&pci_bus_sem);
405+
wakeup = pcie_pme_check_wakeup(port->subordinate);
406+
up_read(&pci_bus_sem);
407+
}
379408
spin_lock_irq(&data->lock);
380-
pcie_pme_interrupt_enable(port, false);
381-
pcie_clear_root_pme_status(port);
382-
data->noirq = true;
409+
if (wakeup) {
410+
enable_irq_wake(srv->irq);
411+
data->suspend_level = PME_SUSPEND_WAKEUP;
412+
} else {
413+
struct pci_dev *port = srv->port;
414+
415+
pcie_pme_interrupt_enable(port, false);
416+
pcie_clear_root_pme_status(port);
417+
data->suspend_level = PME_SUSPEND_NOIRQ;
418+
}
383419
spin_unlock_irq(&data->lock);
384420

385421
synchronize_irq(srv->irq);
@@ -394,12 +430,17 @@ static int pcie_pme_suspend(struct pcie_device *srv)
394430
static int pcie_pme_resume(struct pcie_device *srv)
395431
{
396432
struct pcie_pme_service_data *data = get_service_data(srv);
397-
struct pci_dev *port = srv->port;
398433

399434
spin_lock_irq(&data->lock);
400-
data->noirq = false;
401-
pcie_clear_root_pme_status(port);
402-
pcie_pme_interrupt_enable(port, true);
435+
if (data->suspend_level == PME_SUSPEND_NOIRQ) {
436+
struct pci_dev *port = srv->port;
437+
438+
pcie_clear_root_pme_status(port);
439+
pcie_pme_interrupt_enable(port, true);
440+
} else {
441+
disable_irq_wake(srv->irq);
442+
}
443+
data->suspend_level = PME_SUSPEND_NONE;
403444
spin_unlock_irq(&data->lock);
404445

405446
return 0;

‎include/linux/interrupt.h‎

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -193,11 +193,6 @@ extern void irq_wake_thread(unsigned int irq, void *dev_id);
193193
/* The following three functions are for the core kernel use only. */
194194
extern void suspend_device_irqs(void);
195195
extern void resume_device_irqs(void);
196-
#ifdef CONFIG_PM_SLEEP
197-
extern int check_wakeup_irqs(void);
198-
#else
199-
static inline int check_wakeup_irqs(void) { return 0; }
200-
#endif
201196

202197
/**
203198
* struct irq_affinity_notify - context for notification of IRQ affinity changes

‎include/linux/irq.h‎

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -173,6 +173,7 @@ struct irq_data {
173173
* IRQD_IRQ_DISABLED - Disabled state of the interrupt
174174
* IRQD_IRQ_MASKED - Masked state of the interrupt
175175
* IRQD_IRQ_INPROGRESS - In progress state of the interrupt
176+
* IRQD_WAKEUP_ARMED - Wakeup mode armed
176177
*/
177178
enum {
178179
IRQD_TRIGGER_MASK = 0xf,
@@ -186,6 +187,7 @@ enum {
186187
IRQD_IRQ_DISABLED = (1 << 16),
187188
IRQD_IRQ_MASKED = (1 << 17),
188189
IRQD_IRQ_INPROGRESS = (1 << 18),
190+
IRQD_WAKEUP_ARMED = (1 << 19),
189191
};
190192

191193
static inline bool irqd_is_setaffinity_pending(struct irq_data *d)
@@ -257,6 +259,12 @@ static inline bool irqd_irq_inprogress(struct irq_data *d)
257259
return d->state_use_accessors & IRQD_IRQ_INPROGRESS;
258260
}
259261

262+
static inline bool irqd_is_wakeup_armed(struct irq_data *d)
263+
{
264+
return d->state_use_accessors & IRQD_WAKEUP_ARMED;
265+
}
266+
267+
260268
/*
261269
* Functions for chained handlers which can be enabled/disabled by the
262270
* standard disable_irq/enable_irq calls. Must be called with

‎include/linux/irqdesc.h‎

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,11 @@ struct irq_desc;
3636
* @threads_oneshot: bitfield to handle shared oneshot threads
3737
* @threads_active: number of irqaction threads currently running
3838
* @wait_for_threads: wait queue for sync_irq to wait for threaded handlers
39+
* @nr_actions: number of installed actions on this descriptor
40+
* @no_suspend_depth: number of irqactions on a irq descriptor with
41+
* IRQF_NO_SUSPEND set
42+
* @force_resume_depth: number of irqactions on a irq descriptor with
43+
* IRQF_FORCE_RESUME set
3944
* @dir: /proc/irq/ procfs entry
4045
* @name: flow handler name for /proc/interrupts output
4146
*/
@@ -68,6 +73,11 @@ struct irq_desc {
6873
unsigned long threads_oneshot;
6974
atomic_t threads_active;
7075
wait_queue_head_t wait_for_threads;
76+
#ifdef CONFIG_PM_SLEEP
77+
unsigned int nr_actions;
78+
unsigned int no_suspend_depth;
79+
unsigned int force_resume_depth;
80+
#endif
7181
#ifdef CONFIG_PROC_FS
7282
struct proc_dir_entry *dir;
7383
#endif

0 commit comments

Comments
 (0)