Rafael J. Wysocki | 0a0c516 | 2009-03-16 22:33:49 +0100 | [diff] [blame] | 1 | /* |
| 2 | * linux/kernel/irq/pm.c |
| 3 | * |
| 4 | * Copyright (C) 2009 Rafael J. Wysocki <rjw@sisk.pl>, Novell Inc. |
| 5 | * |
| 6 | * This file contains power management functions related to interrupts. |
| 7 | */ |
| 8 | |
| 9 | #include <linux/irq.h> |
| 10 | #include <linux/module.h> |
| 11 | #include <linux/interrupt.h> |
Ian Campbell | 9bab0b7 | 2011-10-03 15:37:00 +0100 | [diff] [blame] | 12 | #include <linux/syscore_ops.h> |
Rafael J. Wysocki | 0a0c516 | 2009-03-16 22:33:49 +0100 | [diff] [blame] | 13 | |
| 14 | #include "internals.h" |
| 15 | |
Thomas Gleixner | cab303b | 2014-08-28 11:44:31 +0200 | [diff] [blame] | 16 | /* |
| 17 | * Called from __setup_irq() with desc->lock held after @action has |
| 18 | * been installed in the action chain. |
| 19 | */ |
| 20 | void irq_pm_install_action(struct irq_desc *desc, struct irqaction *action) |
| 21 | { |
| 22 | desc->nr_actions++; |
| 23 | |
| 24 | if (action->flags & IRQF_FORCE_RESUME) |
| 25 | desc->force_resume_depth++; |
| 26 | |
| 27 | WARN_ON_ONCE(desc->force_resume_depth && |
| 28 | desc->force_resume_depth != desc->nr_actions); |
| 29 | |
| 30 | if (action->flags & IRQF_NO_SUSPEND) |
| 31 | desc->no_suspend_depth++; |
| 32 | |
| 33 | WARN_ON_ONCE(desc->no_suspend_depth && |
| 34 | desc->no_suspend_depth != desc->nr_actions); |
| 35 | } |
| 36 | |
| 37 | /* |
| 38 | * Called from __free_irq() with desc->lock held after @action has |
| 39 | * been removed from the action chain. |
| 40 | */ |
| 41 | void irq_pm_remove_action(struct irq_desc *desc, struct irqaction *action) |
| 42 | { |
| 43 | desc->nr_actions--; |
| 44 | |
| 45 | if (action->flags & IRQF_FORCE_RESUME) |
| 46 | desc->force_resume_depth--; |
| 47 | |
| 48 | if (action->flags & IRQF_NO_SUSPEND) |
| 49 | desc->no_suspend_depth--; |
| 50 | } |
| 51 | |
Thomas Gleixner | 8df2e02 | 2014-08-28 11:49:28 +0200 | [diff] [blame] | 52 | static void suspend_device_irq(struct irq_desc *desc, int irq) |
| 53 | { |
Thomas Gleixner | 5417de2 | 2014-08-28 15:48:59 +0200 | [diff] [blame] | 54 | if (!desc->action || desc->no_suspend_depth) |
Thomas Gleixner | 8df2e02 | 2014-08-28 11:49:28 +0200 | [diff] [blame] | 55 | return; |
| 56 | |
| 57 | desc->istate |= IRQS_SUSPENDED; |
| 58 | __disable_irq(desc, irq); |
Thomas Gleixner | 092fadd | 2014-08-28 16:49:43 +0200 | [diff] [blame^] | 59 | |
| 60 | /* |
| 61 | * Hardware which has no wakeup source configuration facility |
| 62 | * requires that the non wakeup interrupts are masked at the |
| 63 | * chip level. The chip implementation indicates that with |
| 64 | * IRQCHIP_MASK_ON_SUSPEND. |
| 65 | */ |
| 66 | if (irq_desc_get_chip(desc)->flags & IRQCHIP_MASK_ON_SUSPEND) |
| 67 | mask_irq(desc); |
Thomas Gleixner | 8df2e02 | 2014-08-28 11:49:28 +0200 | [diff] [blame] | 68 | } |
| 69 | |
Rafael J. Wysocki | 0a0c516 | 2009-03-16 22:33:49 +0100 | [diff] [blame] | 70 | /** |
| 71 | * suspend_device_irqs - disable all currently enabled interrupt lines |
| 72 | * |
Thomas Gleixner | 8df2e02 | 2014-08-28 11:49:28 +0200 | [diff] [blame] | 73 | * During system-wide suspend or hibernation device drivers need to be |
| 74 | * prevented from receiving interrupts and this function is provided |
| 75 | * for this purpose. |
| 76 | * |
| 77 | * So we disable all interrupts and mark them IRQS_SUSPENDED except |
| 78 | * for those which are unused and those which are marked as not |
| 79 | * suspendable via an interrupt request with the flag IRQF_NO_SUSPEND |
| 80 | * set. |
Rafael J. Wysocki | 0a0c516 | 2009-03-16 22:33:49 +0100 | [diff] [blame] | 81 | */ |
| 82 | void suspend_device_irqs(void) |
| 83 | { |
| 84 | struct irq_desc *desc; |
| 85 | int irq; |
| 86 | |
| 87 | for_each_irq_desc(irq, desc) { |
| 88 | unsigned long flags; |
| 89 | |
Thomas Gleixner | 239007b | 2009-11-17 16:46:45 +0100 | [diff] [blame] | 90 | raw_spin_lock_irqsave(&desc->lock, flags); |
Thomas Gleixner | 8df2e02 | 2014-08-28 11:49:28 +0200 | [diff] [blame] | 91 | suspend_device_irq(desc, irq); |
Thomas Gleixner | 239007b | 2009-11-17 16:46:45 +0100 | [diff] [blame] | 92 | raw_spin_unlock_irqrestore(&desc->lock, flags); |
Rafael J. Wysocki | 0a0c516 | 2009-03-16 22:33:49 +0100 | [diff] [blame] | 93 | } |
| 94 | |
| 95 | for_each_irq_desc(irq, desc) |
Thomas Gleixner | c531e83 | 2011-02-08 12:44:58 +0100 | [diff] [blame] | 96 | if (desc->istate & IRQS_SUSPENDED) |
Rafael J. Wysocki | 0a0c516 | 2009-03-16 22:33:49 +0100 | [diff] [blame] | 97 | synchronize_irq(irq); |
| 98 | } |
| 99 | EXPORT_SYMBOL_GPL(suspend_device_irqs); |
| 100 | |
Thomas Gleixner | 8df2e02 | 2014-08-28 11:49:28 +0200 | [diff] [blame] | 101 | static void resume_irq(struct irq_desc *desc, int irq) |
| 102 | { |
| 103 | if (desc->istate & IRQS_SUSPENDED) |
| 104 | goto resume; |
| 105 | |
Thomas Gleixner | 5417de2 | 2014-08-28 15:48:59 +0200 | [diff] [blame] | 106 | /* Force resume the interrupt? */ |
| 107 | if (!desc->force_resume_depth) |
Thomas Gleixner | 8df2e02 | 2014-08-28 11:49:28 +0200 | [diff] [blame] | 108 | return; |
| 109 | |
| 110 | /* Pretend that it got disabled ! */ |
| 111 | desc->depth++; |
| 112 | resume: |
| 113 | desc->istate &= ~IRQS_SUSPENDED; |
| 114 | __enable_irq(desc, irq); |
| 115 | } |
| 116 | |
Ian Campbell | 9bab0b7 | 2011-10-03 15:37:00 +0100 | [diff] [blame] | 117 | static void resume_irqs(bool want_early) |
Rafael J. Wysocki | 0a0c516 | 2009-03-16 22:33:49 +0100 | [diff] [blame] | 118 | { |
| 119 | struct irq_desc *desc; |
| 120 | int irq; |
| 121 | |
| 122 | for_each_irq_desc(irq, desc) { |
| 123 | unsigned long flags; |
Ian Campbell | 9bab0b7 | 2011-10-03 15:37:00 +0100 | [diff] [blame] | 124 | bool is_early = desc->action && |
| 125 | desc->action->flags & IRQF_EARLY_RESUME; |
| 126 | |
Laxman Dewangan | ac01810 | 2013-11-25 19:39:47 +0530 | [diff] [blame] | 127 | if (!is_early && want_early) |
Ian Campbell | 9bab0b7 | 2011-10-03 15:37:00 +0100 | [diff] [blame] | 128 | continue; |
Rafael J. Wysocki | 0a0c516 | 2009-03-16 22:33:49 +0100 | [diff] [blame] | 129 | |
Thomas Gleixner | 239007b | 2009-11-17 16:46:45 +0100 | [diff] [blame] | 130 | raw_spin_lock_irqsave(&desc->lock, flags); |
Thomas Gleixner | 8df2e02 | 2014-08-28 11:49:28 +0200 | [diff] [blame] | 131 | resume_irq(desc, irq); |
Thomas Gleixner | 239007b | 2009-11-17 16:46:45 +0100 | [diff] [blame] | 132 | raw_spin_unlock_irqrestore(&desc->lock, flags); |
Rafael J. Wysocki | 0a0c516 | 2009-03-16 22:33:49 +0100 | [diff] [blame] | 133 | } |
| 134 | } |
Ian Campbell | 9bab0b7 | 2011-10-03 15:37:00 +0100 | [diff] [blame] | 135 | |
| 136 | /** |
| 137 | * irq_pm_syscore_ops - enable interrupt lines early |
| 138 | * |
| 139 | * Enable all interrupt lines with %IRQF_EARLY_RESUME set. |
| 140 | */ |
| 141 | static void irq_pm_syscore_resume(void) |
| 142 | { |
| 143 | resume_irqs(true); |
| 144 | } |
| 145 | |
| 146 | static struct syscore_ops irq_pm_syscore_ops = { |
| 147 | .resume = irq_pm_syscore_resume, |
| 148 | }; |
| 149 | |
| 150 | static int __init irq_pm_init_ops(void) |
| 151 | { |
| 152 | register_syscore_ops(&irq_pm_syscore_ops); |
| 153 | return 0; |
| 154 | } |
| 155 | |
| 156 | device_initcall(irq_pm_init_ops); |
| 157 | |
| 158 | /** |
| 159 | * resume_device_irqs - enable interrupt lines disabled by suspend_device_irqs() |
| 160 | * |
| 161 | * Enable all non-%IRQF_EARLY_RESUME interrupt lines previously |
| 162 | * disabled by suspend_device_irqs() that have the IRQS_SUSPENDED flag |
| 163 | * set as well as those with %IRQF_FORCE_RESUME. |
| 164 | */ |
| 165 | void resume_device_irqs(void) |
| 166 | { |
| 167 | resume_irqs(false); |
| 168 | } |
Rafael J. Wysocki | 0a0c516 | 2009-03-16 22:33:49 +0100 | [diff] [blame] | 169 | EXPORT_SYMBOL_GPL(resume_device_irqs); |
| 170 | |
| 171 | /** |
| 172 | * check_wakeup_irqs - check if any wake-up interrupts are pending |
| 173 | */ |
| 174 | int check_wakeup_irqs(void) |
| 175 | { |
| 176 | struct irq_desc *desc; |
| 177 | int irq; |
| 178 | |
Thomas Gleixner | d209a69 | 2011-03-11 21:22:14 +0100 | [diff] [blame] | 179 | for_each_irq_desc(irq, desc) { |
Thomas Gleixner | 9c6079a | 2012-05-04 17:56:16 +0200 | [diff] [blame] | 180 | /* |
| 181 | * Only interrupts which are marked as wakeup source |
| 182 | * and have not been disabled before the suspend check |
| 183 | * can abort suspend. |
| 184 | */ |
Thomas Gleixner | d209a69 | 2011-03-11 21:22:14 +0100 | [diff] [blame] | 185 | if (irqd_is_wakeup_set(&desc->irq_data)) { |
Thomas Gleixner | 9c6079a | 2012-05-04 17:56:16 +0200 | [diff] [blame] | 186 | if (desc->depth == 1 && desc->istate & IRQS_PENDING) |
Thomas Gleixner | d209a69 | 2011-03-11 21:22:14 +0100 | [diff] [blame] | 187 | return -EBUSY; |
Thomas Gleixner | d209a69 | 2011-03-11 21:22:14 +0100 | [diff] [blame] | 188 | } |
Thomas Gleixner | d209a69 | 2011-03-11 21:22:14 +0100 | [diff] [blame] | 189 | } |
Rafael J. Wysocki | 0a0c516 | 2009-03-16 22:33:49 +0100 | [diff] [blame] | 190 | |
| 191 | return 0; |
| 192 | } |