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