152a65ff5SThomas Gleixner // SPDX-License-Identifier: GPL-2.0
20a0c5168SRafael J. Wysocki /*
30a0c5168SRafael J. Wysocki * Copyright (C) 2009 Rafael J. Wysocki <rjw@sisk.pl>, Novell Inc.
40a0c5168SRafael J. Wysocki *
50a0c5168SRafael J. Wysocki * This file contains power management functions related to interrupts.
60a0c5168SRafael J. Wysocki */
70a0c5168SRafael J. Wysocki
80a0c5168SRafael J. Wysocki #include <linux/irq.h>
90a0c5168SRafael J. Wysocki #include <linux/module.h>
100a0c5168SRafael J. Wysocki #include <linux/interrupt.h>
119ce7a258SThomas Gleixner #include <linux/suspend.h>
129bab0b7fSIan Campbell #include <linux/syscore_ops.h>
130a0c5168SRafael J. Wysocki
140a0c5168SRafael J. Wysocki #include "internals.h"
150a0c5168SRafael J. Wysocki
irq_pm_check_wakeup(struct irq_desc * desc)169ce7a258SThomas Gleixner bool irq_pm_check_wakeup(struct irq_desc *desc)
179ce7a258SThomas Gleixner {
189ce7a258SThomas Gleixner if (irqd_is_wakeup_armed(&desc->irq_data)) {
199ce7a258SThomas Gleixner irqd_clear(&desc->irq_data, IRQD_WAKEUP_ARMED);
209ce7a258SThomas Gleixner desc->istate |= IRQS_SUSPENDED | IRQS_PENDING;
219ce7a258SThomas Gleixner desc->depth++;
229ce7a258SThomas Gleixner irq_disable(desc);
23a6f5f0ddSAlexandra Yates pm_system_irq_wakeup(irq_desc_get_irq(desc));
249ce7a258SThomas Gleixner return true;
259ce7a258SThomas Gleixner }
269ce7a258SThomas Gleixner return false;
279ce7a258SThomas Gleixner }
289ce7a258SThomas Gleixner
29cab303beSThomas Gleixner /*
30cab303beSThomas Gleixner * Called from __setup_irq() with desc->lock held after @action has
31cab303beSThomas Gleixner * been installed in the action chain.
32cab303beSThomas Gleixner */
irq_pm_install_action(struct irq_desc * desc,struct irqaction * action)33cab303beSThomas Gleixner void irq_pm_install_action(struct irq_desc *desc, struct irqaction *action)
34cab303beSThomas Gleixner {
35cab303beSThomas Gleixner desc->nr_actions++;
36cab303beSThomas Gleixner
37cab303beSThomas Gleixner if (action->flags & IRQF_FORCE_RESUME)
38cab303beSThomas Gleixner desc->force_resume_depth++;
39cab303beSThomas Gleixner
40cab303beSThomas Gleixner WARN_ON_ONCE(desc->force_resume_depth &&
41cab303beSThomas Gleixner desc->force_resume_depth != desc->nr_actions);
42cab303beSThomas Gleixner
43cab303beSThomas Gleixner if (action->flags & IRQF_NO_SUSPEND)
44cab303beSThomas Gleixner desc->no_suspend_depth++;
4517f48034SRafael J. Wysocki else if (action->flags & IRQF_COND_SUSPEND)
4617f48034SRafael J. Wysocki desc->cond_suspend_depth++;
47cab303beSThomas Gleixner
48cab303beSThomas Gleixner WARN_ON_ONCE(desc->no_suspend_depth &&
4917f48034SRafael J. Wysocki (desc->no_suspend_depth +
5017f48034SRafael J. Wysocki desc->cond_suspend_depth) != desc->nr_actions);
51cab303beSThomas Gleixner }
52cab303beSThomas Gleixner
53cab303beSThomas Gleixner /*
54cab303beSThomas Gleixner * Called from __free_irq() with desc->lock held after @action has
55cab303beSThomas Gleixner * been removed from the action chain.
56cab303beSThomas Gleixner */
irq_pm_remove_action(struct irq_desc * desc,struct irqaction * action)57cab303beSThomas Gleixner void irq_pm_remove_action(struct irq_desc *desc, struct irqaction *action)
58cab303beSThomas Gleixner {
59cab303beSThomas Gleixner desc->nr_actions--;
60cab303beSThomas Gleixner
61cab303beSThomas Gleixner if (action->flags & IRQF_FORCE_RESUME)
62cab303beSThomas Gleixner desc->force_resume_depth--;
63cab303beSThomas Gleixner
64cab303beSThomas Gleixner if (action->flags & IRQF_NO_SUSPEND)
65cab303beSThomas Gleixner desc->no_suspend_depth--;
6617f48034SRafael J. Wysocki else if (action->flags & IRQF_COND_SUSPEND)
6717f48034SRafael J. Wysocki desc->cond_suspend_depth--;
68cab303beSThomas Gleixner }
69cab303beSThomas Gleixner
suspend_device_irq(struct irq_desc * desc)70b80f5f3fSJiang Liu static bool suspend_device_irq(struct irq_desc *desc)
718df2e02cSThomas Gleixner {
7290428a8eSMaulik Shah unsigned long chipflags = irq_desc_get_chip(desc)->flags;
7390428a8eSMaulik Shah struct irq_data *irqd = &desc->irq_data;
7490428a8eSMaulik Shah
754717f133SGrygorii Strashko if (!desc->action || irq_desc_is_chained(desc) ||
764717f133SGrygorii Strashko desc->no_suspend_depth)
77c4df606cSThomas Gleixner return false;
788df2e02cSThomas Gleixner
7990428a8eSMaulik Shah if (irqd_is_wakeup_set(irqd)) {
8090428a8eSMaulik Shah irqd_set(irqd, IRQD_WAKEUP_ARMED);
8190428a8eSMaulik Shah
8290428a8eSMaulik Shah if ((chipflags & IRQCHIP_ENABLE_WAKEUP_ON_SUSPEND) &&
8390428a8eSMaulik Shah irqd_irq_disabled(irqd)) {
8490428a8eSMaulik Shah /*
8590428a8eSMaulik Shah * Interrupt marked for wakeup is in disabled state.
8690428a8eSMaulik Shah * Enable interrupt here to unmask/enable in irqchip
8790428a8eSMaulik Shah * to be able to resume with such interrupts.
8890428a8eSMaulik Shah */
8990428a8eSMaulik Shah __enable_irq(desc);
9090428a8eSMaulik Shah irqd_set(irqd, IRQD_IRQ_ENABLED_ON_SUSPEND);
9190428a8eSMaulik Shah }
929ce7a258SThomas Gleixner /*
939ce7a258SThomas Gleixner * We return true here to force the caller to issue
949ce7a258SThomas Gleixner * synchronize_irq(). We need to make sure that the
959ce7a258SThomas Gleixner * IRQD_WAKEUP_ARMED is visible before we return from
969ce7a258SThomas Gleixner * suspend_device_irqs().
979ce7a258SThomas Gleixner */
989ce7a258SThomas Gleixner return true;
999ce7a258SThomas Gleixner }
100b76f1674SThomas Gleixner
1018df2e02cSThomas Gleixner desc->istate |= IRQS_SUSPENDED;
10279ff1cdaSJiang Liu __disable_irq(desc);
103092fadd5SThomas Gleixner
104092fadd5SThomas Gleixner /*
105092fadd5SThomas Gleixner * Hardware which has no wakeup source configuration facility
106092fadd5SThomas Gleixner * requires that the non wakeup interrupts are masked at the
107092fadd5SThomas Gleixner * chip level. The chip implementation indicates that with
108092fadd5SThomas Gleixner * IRQCHIP_MASK_ON_SUSPEND.
109092fadd5SThomas Gleixner */
11090428a8eSMaulik Shah if (chipflags & IRQCHIP_MASK_ON_SUSPEND)
111092fadd5SThomas Gleixner mask_irq(desc);
112c4df606cSThomas Gleixner return true;
1138df2e02cSThomas Gleixner }
1148df2e02cSThomas Gleixner
1150a0c5168SRafael J. Wysocki /**
1160a0c5168SRafael J. Wysocki * suspend_device_irqs - disable all currently enabled interrupt lines
1170a0c5168SRafael J. Wysocki *
1188df2e02cSThomas Gleixner * During system-wide suspend or hibernation device drivers need to be
1198df2e02cSThomas Gleixner * prevented from receiving interrupts and this function is provided
1208df2e02cSThomas Gleixner * for this purpose.
1218df2e02cSThomas Gleixner *
1228df2e02cSThomas Gleixner * So we disable all interrupts and mark them IRQS_SUSPENDED except
1239ce7a258SThomas Gleixner * for those which are unused, those which are marked as not
1248df2e02cSThomas Gleixner * suspendable via an interrupt request with the flag IRQF_NO_SUSPEND
1259ce7a258SThomas Gleixner * set and those which are marked as active wakeup sources.
1269ce7a258SThomas Gleixner *
1279ce7a258SThomas Gleixner * The active wakeup sources are handled by the flow handler entry
1289ce7a258SThomas Gleixner * code which checks for the IRQD_WAKEUP_ARMED flag, suspends the
1299ce7a258SThomas Gleixner * interrupt and notifies the pm core about the wakeup.
1300a0c5168SRafael J. Wysocki */
suspend_device_irqs(void)1310a0c5168SRafael J. Wysocki void suspend_device_irqs(void)
1320a0c5168SRafael J. Wysocki {
1330a0c5168SRafael J. Wysocki struct irq_desc *desc;
1340a0c5168SRafael J. Wysocki int irq;
1350a0c5168SRafael J. Wysocki
1360a0c5168SRafael J. Wysocki for_each_irq_desc(irq, desc) {
1370a0c5168SRafael J. Wysocki unsigned long flags;
138c4df606cSThomas Gleixner bool sync;
1390a0c5168SRafael J. Wysocki
1403c646f2cSNeilBrown if (irq_settings_is_nested_thread(desc))
1413c646f2cSNeilBrown continue;
142239007b8SThomas Gleixner raw_spin_lock_irqsave(&desc->lock, flags);
143b80f5f3fSJiang Liu sync = suspend_device_irq(desc);
144239007b8SThomas Gleixner raw_spin_unlock_irqrestore(&desc->lock, flags);
1450a0c5168SRafael J. Wysocki
146c4df606cSThomas Gleixner if (sync)
1470a0c5168SRafael J. Wysocki synchronize_irq(irq);
1480a0c5168SRafael J. Wysocki }
149c4df606cSThomas Gleixner }
1500a0c5168SRafael J. Wysocki
resume_irq(struct irq_desc * desc)151b80f5f3fSJiang Liu static void resume_irq(struct irq_desc *desc)
1528df2e02cSThomas Gleixner {
15390428a8eSMaulik Shah struct irq_data *irqd = &desc->irq_data;
15490428a8eSMaulik Shah
15590428a8eSMaulik Shah irqd_clear(irqd, IRQD_WAKEUP_ARMED);
15690428a8eSMaulik Shah
15790428a8eSMaulik Shah if (irqd_is_enabled_on_suspend(irqd)) {
15890428a8eSMaulik Shah /*
15990428a8eSMaulik Shah * Interrupt marked for wakeup was enabled during suspend
16090428a8eSMaulik Shah * entry. Disable such interrupts to restore them back to
16190428a8eSMaulik Shah * original state.
16290428a8eSMaulik Shah */
16390428a8eSMaulik Shah __disable_irq(desc);
16490428a8eSMaulik Shah irqd_clear(irqd, IRQD_IRQ_ENABLED_ON_SUSPEND);
16590428a8eSMaulik Shah }
166b76f1674SThomas Gleixner
1678df2e02cSThomas Gleixner if (desc->istate & IRQS_SUSPENDED)
1688df2e02cSThomas Gleixner goto resume;
1698df2e02cSThomas Gleixner
1705417de22SThomas Gleixner /* Force resume the interrupt? */
1715417de22SThomas Gleixner if (!desc->force_resume_depth)
1728df2e02cSThomas Gleixner return;
1738df2e02cSThomas Gleixner
1748df2e02cSThomas Gleixner /* Pretend that it got disabled ! */
1758df2e02cSThomas Gleixner desc->depth++;
176a696712cSJuergen Gross irq_state_set_disabled(desc);
177a696712cSJuergen Gross irq_state_set_masked(desc);
1788df2e02cSThomas Gleixner resume:
1798df2e02cSThomas Gleixner desc->istate &= ~IRQS_SUSPENDED;
18079ff1cdaSJiang Liu __enable_irq(desc);
1818df2e02cSThomas Gleixner }
1828df2e02cSThomas Gleixner
resume_irqs(bool want_early)1839bab0b7fSIan Campbell static void resume_irqs(bool want_early)
1840a0c5168SRafael J. Wysocki {
1850a0c5168SRafael J. Wysocki struct irq_desc *desc;
1860a0c5168SRafael J. Wysocki int irq;
1870a0c5168SRafael J. Wysocki
1880a0c5168SRafael J. Wysocki for_each_irq_desc(irq, desc) {
1890a0c5168SRafael J. Wysocki unsigned long flags;
1909bab0b7fSIan Campbell bool is_early = desc->action &&
1919bab0b7fSIan Campbell desc->action->flags & IRQF_EARLY_RESUME;
1929bab0b7fSIan Campbell
193ac01810cSLaxman Dewangan if (!is_early && want_early)
1949bab0b7fSIan Campbell continue;
1953c646f2cSNeilBrown if (irq_settings_is_nested_thread(desc))
1963c646f2cSNeilBrown continue;
1970a0c5168SRafael J. Wysocki
198239007b8SThomas Gleixner raw_spin_lock_irqsave(&desc->lock, flags);
199b80f5f3fSJiang Liu resume_irq(desc);
200239007b8SThomas Gleixner raw_spin_unlock_irqrestore(&desc->lock, flags);
2010a0c5168SRafael J. Wysocki }
2020a0c5168SRafael J. Wysocki }
2039bab0b7fSIan Campbell
2049bab0b7fSIan Campbell /**
2053a79bc63SRafael J. Wysocki * rearm_wake_irq - rearm a wakeup interrupt line after signaling wakeup
2063a79bc63SRafael J. Wysocki * @irq: Interrupt to rearm
2073a79bc63SRafael J. Wysocki */
rearm_wake_irq(unsigned int irq)2083a79bc63SRafael J. Wysocki void rearm_wake_irq(unsigned int irq)
2093a79bc63SRafael J. Wysocki {
2103a79bc63SRafael J. Wysocki unsigned long flags;
2113a79bc63SRafael J. Wysocki struct irq_desc *desc = irq_get_desc_buslock(irq, &flags, IRQ_GET_DESC_CHECK_GLOBAL);
2123a79bc63SRafael J. Wysocki
213e27b1636SGuenter Roeck if (!desc)
2143a79bc63SRafael J. Wysocki return;
2153a79bc63SRafael J. Wysocki
216e27b1636SGuenter Roeck if (!(desc->istate & IRQS_SUSPENDED) ||
217e27b1636SGuenter Roeck !irqd_is_wakeup_set(&desc->irq_data))
218e27b1636SGuenter Roeck goto unlock;
219e27b1636SGuenter Roeck
2203a79bc63SRafael J. Wysocki desc->istate &= ~IRQS_SUSPENDED;
2213a79bc63SRafael J. Wysocki irqd_set(&desc->irq_data, IRQD_WAKEUP_ARMED);
2223a79bc63SRafael J. Wysocki __enable_irq(desc);
2233a79bc63SRafael J. Wysocki
224e27b1636SGuenter Roeck unlock:
2253a79bc63SRafael J. Wysocki irq_put_desc_busunlock(desc, flags);
2263a79bc63SRafael J. Wysocki }
2273a79bc63SRafael J. Wysocki
2283a79bc63SRafael J. Wysocki /**
229*3b35e7e6SRandy Dunlap * irq_pm_syscore_resume - enable interrupt lines early
2309bab0b7fSIan Campbell *
2319bab0b7fSIan Campbell * Enable all interrupt lines with %IRQF_EARLY_RESUME set.
2329bab0b7fSIan Campbell */
irq_pm_syscore_resume(void)2339bab0b7fSIan Campbell static void irq_pm_syscore_resume(void)
2349bab0b7fSIan Campbell {
2359bab0b7fSIan Campbell resume_irqs(true);
2369bab0b7fSIan Campbell }
2379bab0b7fSIan Campbell
2389bab0b7fSIan Campbell static struct syscore_ops irq_pm_syscore_ops = {
2399bab0b7fSIan Campbell .resume = irq_pm_syscore_resume,
2409bab0b7fSIan Campbell };
2419bab0b7fSIan Campbell
irq_pm_init_ops(void)2429bab0b7fSIan Campbell static int __init irq_pm_init_ops(void)
2439bab0b7fSIan Campbell {
2449bab0b7fSIan Campbell register_syscore_ops(&irq_pm_syscore_ops);
2459bab0b7fSIan Campbell return 0;
2469bab0b7fSIan Campbell }
2479bab0b7fSIan Campbell
2489bab0b7fSIan Campbell device_initcall(irq_pm_init_ops);
2499bab0b7fSIan Campbell
2509bab0b7fSIan Campbell /**
2519bab0b7fSIan Campbell * resume_device_irqs - enable interrupt lines disabled by suspend_device_irqs()
2529bab0b7fSIan Campbell *
2539bab0b7fSIan Campbell * Enable all non-%IRQF_EARLY_RESUME interrupt lines previously
2549bab0b7fSIan Campbell * disabled by suspend_device_irqs() that have the IRQS_SUSPENDED flag
2559bab0b7fSIan Campbell * set as well as those with %IRQF_FORCE_RESUME.
2569bab0b7fSIan Campbell */
resume_device_irqs(void)2579bab0b7fSIan Campbell void resume_device_irqs(void)
2589bab0b7fSIan Campbell {
2599bab0b7fSIan Campbell resume_irqs(false);
2609bab0b7fSIan Campbell }
261