1 // SPDX-License-Identifier: GPL-2.0 2 /* 3 * Copyright (C) 2009 Rafael J. Wysocki <rjw@sisk.pl>, Novell Inc. 4 * 5 * This file contains power management functions related to interrupts. 6 */ 7 8 #include <linux/irq.h> 9 #include <linux/module.h> 10 #include <linux/interrupt.h> 11 #include <linux/suspend.h> 12 #include <linux/syscore_ops.h> 13 14 #include "internals.h" 15 16 void irq_pm_handle_wakeup(struct irq_desc *desc) 17 { 18 irqd_clear(&desc->irq_data, IRQD_WAKEUP_ARMED); 19 desc->istate |= IRQS_SUSPENDED | IRQS_PENDING; 20 desc->depth++; 21 irq_disable(desc); 22 pm_system_irq_wakeup(irq_desc_get_irq(desc)); 23 } 24 25 /* 26 * Called from __setup_irq() with desc->lock held after @action has 27 * been installed in the action chain. 28 */ 29 void irq_pm_install_action(struct irq_desc *desc, struct irqaction *action) 30 { 31 desc->nr_actions++; 32 33 if (action->flags & IRQF_FORCE_RESUME) 34 desc->force_resume_depth++; 35 36 WARN_ON_ONCE(desc->force_resume_depth && 37 desc->force_resume_depth != desc->nr_actions); 38 39 if (action->flags & IRQF_NO_SUSPEND) 40 desc->no_suspend_depth++; 41 else if (action->flags & IRQF_COND_SUSPEND) 42 desc->cond_suspend_depth++; 43 44 WARN_ON_ONCE(desc->no_suspend_depth && 45 (desc->no_suspend_depth + desc->cond_suspend_depth) != desc->nr_actions); 46 } 47 48 /* 49 * Called from __free_irq() with desc->lock held after @action has 50 * been removed from the action chain. 51 */ 52 void irq_pm_remove_action(struct irq_desc *desc, struct irqaction *action) 53 { 54 desc->nr_actions--; 55 56 if (action->flags & IRQF_FORCE_RESUME) 57 desc->force_resume_depth--; 58 59 if (action->flags & IRQF_NO_SUSPEND) 60 desc->no_suspend_depth--; 61 else if (action->flags & IRQF_COND_SUSPEND) 62 desc->cond_suspend_depth--; 63 } 64 65 static bool suspend_device_irq(struct irq_desc *desc) 66 { 67 unsigned long chipflags = irq_desc_get_chip(desc)->flags; 68 struct irq_data *irqd = &desc->irq_data; 69 70 if (!desc->action || irq_desc_is_chained(desc) || 71 desc->no_suspend_depth) 72 return false; 73 74 if (irqd_is_wakeup_set(irqd)) { 75 irqd_set(irqd, IRQD_WAKEUP_ARMED); 76 77 if ((chipflags & IRQCHIP_ENABLE_WAKEUP_ON_SUSPEND) && 78 irqd_irq_disabled(irqd)) { 79 /* 80 * Interrupt marked for wakeup is in disabled state. 81 * Enable interrupt here to unmask/enable in irqchip 82 * to be able to resume with such interrupts. 83 */ 84 __enable_irq(desc); 85 irqd_set(irqd, IRQD_IRQ_ENABLED_ON_SUSPEND); 86 } 87 /* 88 * We return true here to force the caller to issue 89 * synchronize_irq(). We need to make sure that the 90 * IRQD_WAKEUP_ARMED is visible before we return from 91 * suspend_device_irqs(). 92 */ 93 return true; 94 } 95 96 desc->istate |= IRQS_SUSPENDED; 97 __disable_irq(desc); 98 99 /* 100 * Hardware which has no wakeup source configuration facility 101 * requires that the non wakeup interrupts are masked at the 102 * chip level. The chip implementation indicates that with 103 * IRQCHIP_MASK_ON_SUSPEND. 104 */ 105 if (chipflags & IRQCHIP_MASK_ON_SUSPEND) 106 mask_irq(desc); 107 return true; 108 } 109 110 /** 111 * suspend_device_irqs - disable all currently enabled interrupt lines 112 * 113 * During system-wide suspend or hibernation device drivers need to be 114 * prevented from receiving interrupts and this function is provided 115 * for this purpose. 116 * 117 * So we disable all interrupts and mark them IRQS_SUSPENDED except 118 * for those which are unused, those which are marked as not 119 * suspendable via an interrupt request with the flag IRQF_NO_SUSPEND 120 * set and those which are marked as active wakeup sources. 121 * 122 * The active wakeup sources are handled by the flow handler entry 123 * code which checks for the IRQD_WAKEUP_ARMED flag, suspends the 124 * interrupt and notifies the pm core about the wakeup. 125 */ 126 void suspend_device_irqs(void) 127 { 128 struct irq_desc *desc; 129 int irq; 130 131 for_each_irq_desc(irq, desc) { 132 bool sync; 133 134 if (irq_settings_is_nested_thread(desc)) 135 continue; 136 scoped_guard(raw_spinlock_irqsave, &desc->lock) 137 sync = suspend_device_irq(desc); 138 139 if (sync) 140 synchronize_irq(irq); 141 } 142 } 143 144 static void resume_irq(struct irq_desc *desc) 145 { 146 struct irq_data *irqd = &desc->irq_data; 147 148 irqd_clear(irqd, IRQD_WAKEUP_ARMED); 149 150 if (irqd_is_enabled_on_suspend(irqd)) { 151 /* 152 * Interrupt marked for wakeup was enabled during suspend 153 * entry. Disable such interrupts to restore them back to 154 * original state. 155 */ 156 __disable_irq(desc); 157 irqd_clear(irqd, IRQD_IRQ_ENABLED_ON_SUSPEND); 158 } 159 160 if (desc->istate & IRQS_SUSPENDED) 161 goto resume; 162 163 /* Force resume the interrupt? */ 164 if (!desc->force_resume_depth) 165 return; 166 167 /* Pretend that it got disabled ! */ 168 desc->depth++; 169 irq_state_set_disabled(desc); 170 irq_state_set_masked(desc); 171 resume: 172 desc->istate &= ~IRQS_SUSPENDED; 173 __enable_irq(desc); 174 } 175 176 static void resume_irqs(bool want_early) 177 { 178 struct irq_desc *desc; 179 int irq; 180 181 for_each_irq_desc(irq, desc) { 182 bool is_early = desc->action && desc->action->flags & IRQF_EARLY_RESUME; 183 184 if (!is_early && want_early) 185 continue; 186 if (irq_settings_is_nested_thread(desc)) 187 continue; 188 189 guard(raw_spinlock_irqsave)(&desc->lock); 190 resume_irq(desc); 191 } 192 } 193 194 /** 195 * rearm_wake_irq - rearm a wakeup interrupt line after signaling wakeup 196 * @irq: Interrupt to rearm 197 */ 198 void rearm_wake_irq(unsigned int irq) 199 { 200 scoped_irqdesc_get_and_buslock(irq, IRQ_GET_DESC_CHECK_GLOBAL) { 201 struct irq_desc *desc = scoped_irqdesc; 202 203 if (!(desc->istate & IRQS_SUSPENDED) || !irqd_is_wakeup_set(&desc->irq_data)) 204 return; 205 206 desc->istate &= ~IRQS_SUSPENDED; 207 irqd_set(&desc->irq_data, IRQD_WAKEUP_ARMED); 208 __enable_irq(desc); 209 } 210 } 211 212 /** 213 * irq_pm_syscore_resume - enable interrupt lines early 214 * 215 * Enable all interrupt lines with %IRQF_EARLY_RESUME set. 216 */ 217 static void irq_pm_syscore_resume(void) 218 { 219 resume_irqs(true); 220 } 221 222 static struct syscore_ops irq_pm_syscore_ops = { 223 .resume = irq_pm_syscore_resume, 224 }; 225 226 static int __init irq_pm_init_ops(void) 227 { 228 register_syscore_ops(&irq_pm_syscore_ops); 229 return 0; 230 } 231 232 device_initcall(irq_pm_init_ops); 233 234 /** 235 * resume_device_irqs - enable interrupt lines disabled by suspend_device_irqs() 236 * 237 * Enable all non-%IRQF_EARLY_RESUME interrupt lines previously 238 * disabled by suspend_device_irqs() that have the IRQS_SUSPENDED flag 239 * set as well as those with %IRQF_FORCE_RESUME. 240 */ 241 void resume_device_irqs(void) 242 { 243 resume_irqs(false); 244 } 245