Skip to content

Commit bed5703

Browse files
tmlindrafaeljw
authored andcommitted
PM / wakeirq: Fix dedicated wakeirq for drivers not using autosuspend
I noticed some wakeirq flakeyness with consumer drivers not using autosuspend. For drivers not using autosuspend, the wakeirq may never get unmasked in rpm_suspend() because of irq desc->depth. We are configuring dedicated wakeirqs to start with IRQ_NOAUTOEN as we naturally don't want them running until rpm_suspend() is called. However, when a consumer driver initially calls pm_runtime_get(), we now wrongly start with disable_irq_nosync() call on the dedicated wakeirq that is disabled to start with. This causes desc->depth to toggle between 1 and 2 instead of the usual 0 and 1. This can prevent enable_irq() from unmasking the wakeirq as that only happens at desc->depth 1. This does not necessarily show up with drivers using autosuspend as there is time for disable_irq_nosync() before rpm_suspend() gets called after the autosuspend timeout. Let's fix the issue by adding wirq->status that lazily gets set on the first rpm_suspend(). We also need PM runtime core private functions for dev_pm_enable_wake_irq_check() and dev_pm_disable_wake_irq_check() so we can enable the dedicated wakeirq on the first rpm_suspend(). While at it, let's also fix the comments for dev_pm_enable_wake_irq() and dev_pm_disable_wake_irq(). Those can still be used by the consumer drivers as needed because the IRQ core manages the interrupt usecount for us. Fixes: 4990d4f (PM / Wakeirq: Add automated device wake IRQ handling) Signed-off-by: Tony Lindgren <[email protected]> Signed-off-by: Rafael J. Wysocki <[email protected]>
1 parent 1d9174f commit bed5703

File tree

3 files changed

+88
-15
lines changed

3 files changed

+88
-15
lines changed

drivers/base/power/power.h

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,14 +21,22 @@ extern void pm_runtime_init(struct device *dev);
2121
extern void pm_runtime_reinit(struct device *dev);
2222
extern void pm_runtime_remove(struct device *dev);
2323

24+
#define WAKE_IRQ_DEDICATED_ALLOCATED BIT(0)
25+
#define WAKE_IRQ_DEDICATED_MANAGED BIT(1)
26+
#define WAKE_IRQ_DEDICATED_MASK (WAKE_IRQ_DEDICATED_ALLOCATED | \
27+
WAKE_IRQ_DEDICATED_MANAGED)
28+
2429
struct wake_irq {
2530
struct device *dev;
31+
unsigned int status;
2632
int irq;
27-
bool dedicated_irq:1;
2833
};
2934

3035
extern void dev_pm_arm_wake_irq(struct wake_irq *wirq);
3136
extern void dev_pm_disarm_wake_irq(struct wake_irq *wirq);
37+
extern void dev_pm_enable_wake_irq_check(struct device *dev,
38+
bool can_change_status);
39+
extern void dev_pm_disable_wake_irq_check(struct device *dev);
3240

3341
#ifdef CONFIG_PM_SLEEP
3442

@@ -104,6 +112,15 @@ static inline void dev_pm_disarm_wake_irq(struct wake_irq *wirq)
104112
{
105113
}
106114

115+
static inline void dev_pm_enable_wake_irq_check(struct device *dev,
116+
bool can_change_status)
117+
{
118+
}
119+
120+
static inline void dev_pm_disable_wake_irq_check(struct device *dev)
121+
{
122+
}
123+
107124
#endif
108125

109126
#ifdef CONFIG_PM_SLEEP

drivers/base/power/runtime.c

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -516,7 +516,7 @@ static int rpm_suspend(struct device *dev, int rpmflags)
516516

517517
callback = RPM_GET_CALLBACK(dev, runtime_suspend);
518518

519-
dev_pm_enable_wake_irq(dev);
519+
dev_pm_enable_wake_irq_check(dev, true);
520520
retval = rpm_callback(callback, dev);
521521
if (retval)
522522
goto fail;
@@ -555,7 +555,7 @@ static int rpm_suspend(struct device *dev, int rpmflags)
555555
return retval;
556556

557557
fail:
558-
dev_pm_disable_wake_irq(dev);
558+
dev_pm_disable_wake_irq_check(dev);
559559
__update_runtime_status(dev, RPM_ACTIVE);
560560
dev->power.deferred_resume = false;
561561
wake_up_all(&dev->power.wait_queue);
@@ -738,12 +738,12 @@ static int rpm_resume(struct device *dev, int rpmflags)
738738

739739
callback = RPM_GET_CALLBACK(dev, runtime_resume);
740740

741-
dev_pm_disable_wake_irq(dev);
741+
dev_pm_disable_wake_irq_check(dev);
742742
retval = rpm_callback(callback, dev);
743743
if (retval) {
744744
__update_runtime_status(dev, RPM_SUSPENDED);
745745
pm_runtime_cancel_pending(dev);
746-
dev_pm_enable_wake_irq(dev);
746+
dev_pm_enable_wake_irq_check(dev, false);
747747
} else {
748748
no_callback:
749749
__update_runtime_status(dev, RPM_ACTIVE);

drivers/base/power/wakeirq.c

Lines changed: 66 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -110,8 +110,10 @@ void dev_pm_clear_wake_irq(struct device *dev)
110110
dev->power.wakeirq = NULL;
111111
spin_unlock_irqrestore(&dev->power.lock, flags);
112112

113-
if (wirq->dedicated_irq)
113+
if (wirq->status & WAKE_IRQ_DEDICATED_ALLOCATED) {
114114
free_irq(wirq->irq, wirq);
115+
wirq->status &= ~WAKE_IRQ_DEDICATED_MASK;
116+
}
115117
kfree(wirq);
116118
}
117119
EXPORT_SYMBOL_GPL(dev_pm_clear_wake_irq);
@@ -179,7 +181,6 @@ int dev_pm_set_dedicated_wake_irq(struct device *dev, int irq)
179181

180182
wirq->dev = dev;
181183
wirq->irq = irq;
182-
wirq->dedicated_irq = true;
183184
irq_set_status_flags(irq, IRQ_NOAUTOEN);
184185

185186
/*
@@ -195,6 +196,8 @@ int dev_pm_set_dedicated_wake_irq(struct device *dev, int irq)
195196
if (err)
196197
goto err_free_irq;
197198

199+
wirq->status = WAKE_IRQ_DEDICATED_ALLOCATED;
200+
198201
return err;
199202

200203
err_free_irq:
@@ -210,9 +213,9 @@ EXPORT_SYMBOL_GPL(dev_pm_set_dedicated_wake_irq);
210213
* dev_pm_enable_wake_irq - Enable device wake-up interrupt
211214
* @dev: Device
212215
*
213-
* Called from the bus code or the device driver for
214-
* runtime_suspend() to enable the wake-up interrupt while
215-
* the device is running.
216+
* Optionally called from the bus code or the device driver for
217+
* runtime_resume() to override the PM runtime core managed wake-up
218+
* interrupt handling to enable the wake-up interrupt.
216219
*
217220
* Note that for runtime_suspend()) the wake-up interrupts
218221
* should be unconditionally enabled unlike for suspend()
@@ -222,7 +225,7 @@ void dev_pm_enable_wake_irq(struct device *dev)
222225
{
223226
struct wake_irq *wirq = dev->power.wakeirq;
224227

225-
if (wirq && wirq->dedicated_irq)
228+
if (wirq && (wirq->status & WAKE_IRQ_DEDICATED_ALLOCATED))
226229
enable_irq(wirq->irq);
227230
}
228231
EXPORT_SYMBOL_GPL(dev_pm_enable_wake_irq);
@@ -231,19 +234,72 @@ EXPORT_SYMBOL_GPL(dev_pm_enable_wake_irq);
231234
* dev_pm_disable_wake_irq - Disable device wake-up interrupt
232235
* @dev: Device
233236
*
234-
* Called from the bus code or the device driver for
235-
* runtime_resume() to disable the wake-up interrupt while
236-
* the device is running.
237+
* Optionally called from the bus code or the device driver for
238+
* runtime_suspend() to override the PM runtime core managed wake-up
239+
* interrupt handling to disable the wake-up interrupt.
237240
*/
238241
void dev_pm_disable_wake_irq(struct device *dev)
239242
{
240243
struct wake_irq *wirq = dev->power.wakeirq;
241244

242-
if (wirq && wirq->dedicated_irq)
245+
if (wirq && (wirq->status & WAKE_IRQ_DEDICATED_ALLOCATED))
243246
disable_irq_nosync(wirq->irq);
244247
}
245248
EXPORT_SYMBOL_GPL(dev_pm_disable_wake_irq);
246249

250+
/**
251+
* dev_pm_enable_wake_irq_check - Checks and enables wake-up interrupt
252+
* @dev: Device
253+
* @can_change_status: Can change wake-up interrupt status
254+
*
255+
* Enables wakeirq conditionally. We need to enable wake-up interrupt
256+
* lazily on the first rpm_suspend(). This is needed as the consumer device
257+
* starts in RPM_SUSPENDED state, and the the first pm_runtime_get() would
258+
* otherwise try to disable already disabled wakeirq. The wake-up interrupt
259+
* starts disabled with IRQ_NOAUTOEN set.
260+
*
261+
* Should be only called from rpm_suspend() and rpm_resume() path.
262+
* Caller must hold &dev->power.lock to change wirq->status
263+
*/
264+
void dev_pm_enable_wake_irq_check(struct device *dev,
265+
bool can_change_status)
266+
{
267+
struct wake_irq *wirq = dev->power.wakeirq;
268+
269+
if (!wirq || !((wirq->status & WAKE_IRQ_DEDICATED_MASK)))
270+
return;
271+
272+
if (likely(wirq->status & WAKE_IRQ_DEDICATED_MANAGED)) {
273+
goto enable;
274+
} else if (can_change_status) {
275+
wirq->status |= WAKE_IRQ_DEDICATED_MANAGED;
276+
goto enable;
277+
}
278+
279+
return;
280+
281+
enable:
282+
enable_irq(wirq->irq);
283+
}
284+
285+
/**
286+
* dev_pm_disable_wake_irq_check - Checks and disables wake-up interrupt
287+
* @dev: Device
288+
*
289+
* Disables wake-up interrupt conditionally based on status.
290+
* Should be only called from rpm_suspend() and rpm_resume() path.
291+
*/
292+
void dev_pm_disable_wake_irq_check(struct device *dev)
293+
{
294+
struct wake_irq *wirq = dev->power.wakeirq;
295+
296+
if (!wirq || !((wirq->status & WAKE_IRQ_DEDICATED_MASK)))
297+
return;
298+
299+
if (wirq->status & WAKE_IRQ_DEDICATED_MANAGED)
300+
disable_irq_nosync(wirq->irq);
301+
}
302+
247303
/**
248304
* dev_pm_arm_wake_irq - Arm device wake-up
249305
* @wirq: Device wake-up interrupt

0 commit comments

Comments
 (0)