1*4882a593Smuzhiyun==================================== 2*4882a593SmuzhiyunSystem Suspend and Device Interrupts 3*4882a593Smuzhiyun==================================== 4*4882a593Smuzhiyun 5*4882a593SmuzhiyunCopyright (C) 2014 Intel Corp. 6*4882a593SmuzhiyunAuthor: Rafael J. Wysocki <rafael.j.wysocki@intel.com> 7*4882a593Smuzhiyun 8*4882a593Smuzhiyun 9*4882a593SmuzhiyunSuspending and Resuming Device IRQs 10*4882a593Smuzhiyun----------------------------------- 11*4882a593Smuzhiyun 12*4882a593SmuzhiyunDevice interrupt request lines (IRQs) are generally disabled during system 13*4882a593Smuzhiyunsuspend after the "late" phase of suspending devices (that is, after all of the 14*4882a593Smuzhiyun->prepare, ->suspend and ->suspend_late callbacks have been executed for all 15*4882a593Smuzhiyundevices). That is done by suspend_device_irqs(). 16*4882a593Smuzhiyun 17*4882a593SmuzhiyunThe rationale for doing so is that after the "late" phase of device suspend 18*4882a593Smuzhiyunthere is no legitimate reason why any interrupts from suspended devices should 19*4882a593Smuzhiyuntrigger and if any devices have not been suspended properly yet, it is better to 20*4882a593Smuzhiyunblock interrupts from them anyway. Also, in the past we had problems with 21*4882a593Smuzhiyuninterrupt handlers for shared IRQs that device drivers implementing them were 22*4882a593Smuzhiyunnot prepared for interrupts triggering after their devices had been suspended. 23*4882a593SmuzhiyunIn some cases they would attempt to access, for example, memory address spaces 24*4882a593Smuzhiyunof suspended devices and cause unpredictable behavior to ensue as a result. 25*4882a593SmuzhiyunUnfortunately, such problems are very difficult to debug and the introduction 26*4882a593Smuzhiyunof suspend_device_irqs(), along with the "noirq" phase of device suspend and 27*4882a593Smuzhiyunresume, was the only practical way to mitigate them. 28*4882a593Smuzhiyun 29*4882a593SmuzhiyunDevice IRQs are re-enabled during system resume, right before the "early" phase 30*4882a593Smuzhiyunof resuming devices (that is, before starting to execute ->resume_early 31*4882a593Smuzhiyuncallbacks for devices). The function doing that is resume_device_irqs(). 32*4882a593Smuzhiyun 33*4882a593Smuzhiyun 34*4882a593SmuzhiyunThe IRQF_NO_SUSPEND Flag 35*4882a593Smuzhiyun------------------------ 36*4882a593Smuzhiyun 37*4882a593SmuzhiyunThere are interrupts that can legitimately trigger during the entire system 38*4882a593Smuzhiyunsuspend-resume cycle, including the "noirq" phases of suspending and resuming 39*4882a593Smuzhiyundevices as well as during the time when nonboot CPUs are taken offline and 40*4882a593Smuzhiyunbrought back online. That applies to timer interrupts in the first place, 41*4882a593Smuzhiyunbut also to IPIs and to some other special-purpose interrupts. 42*4882a593Smuzhiyun 43*4882a593SmuzhiyunThe IRQF_NO_SUSPEND flag is used to indicate that to the IRQ subsystem when 44*4882a593Smuzhiyunrequesting a special-purpose interrupt. It causes suspend_device_irqs() to 45*4882a593Smuzhiyunleave the corresponding IRQ enabled so as to allow the interrupt to work as 46*4882a593Smuzhiyunexpected during the suspend-resume cycle, but does not guarantee that the 47*4882a593Smuzhiyuninterrupt will wake the system from a suspended state -- for such cases it is 48*4882a593Smuzhiyunnecessary to use enable_irq_wake(). 49*4882a593Smuzhiyun 50*4882a593SmuzhiyunNote that the IRQF_NO_SUSPEND flag affects the entire IRQ and not just one 51*4882a593Smuzhiyunuser of it. Thus, if the IRQ is shared, all of the interrupt handlers installed 52*4882a593Smuzhiyunfor it will be executed as usual after suspend_device_irqs(), even if the 53*4882a593SmuzhiyunIRQF_NO_SUSPEND flag was not passed to request_irq() (or equivalent) by some of 54*4882a593Smuzhiyunthe IRQ's users. For this reason, using IRQF_NO_SUSPEND and IRQF_SHARED at the 55*4882a593Smuzhiyunsame time should be avoided. 56*4882a593Smuzhiyun 57*4882a593Smuzhiyun 58*4882a593SmuzhiyunSystem Wakeup Interrupts, enable_irq_wake() and disable_irq_wake() 59*4882a593Smuzhiyun------------------------------------------------------------------ 60*4882a593Smuzhiyun 61*4882a593SmuzhiyunSystem wakeup interrupts generally need to be configured to wake up the system 62*4882a593Smuzhiyunfrom sleep states, especially if they are used for different purposes (e.g. as 63*4882a593SmuzhiyunI/O interrupts) in the working state. 64*4882a593Smuzhiyun 65*4882a593SmuzhiyunThat may involve turning on a special signal handling logic within the platform 66*4882a593Smuzhiyun(such as an SoC) so that signals from a given line are routed in a different way 67*4882a593Smuzhiyunduring system sleep so as to trigger a system wakeup when needed. For example, 68*4882a593Smuzhiyunthe platform may include a dedicated interrupt controller used specifically for 69*4882a593Smuzhiyunhandling system wakeup events. Then, if a given interrupt line is supposed to 70*4882a593Smuzhiyunwake up the system from sleep sates, the corresponding input of that interrupt 71*4882a593Smuzhiyuncontroller needs to be enabled to receive signals from the line in question. 72*4882a593SmuzhiyunAfter wakeup, it generally is better to disable that input to prevent the 73*4882a593Smuzhiyundedicated controller from triggering interrupts unnecessarily. 74*4882a593Smuzhiyun 75*4882a593SmuzhiyunThe IRQ subsystem provides two helper functions to be used by device drivers for 76*4882a593Smuzhiyunthose purposes. Namely, enable_irq_wake() turns on the platform's logic for 77*4882a593Smuzhiyunhandling the given IRQ as a system wakeup interrupt line and disable_irq_wake() 78*4882a593Smuzhiyunturns that logic off. 79*4882a593Smuzhiyun 80*4882a593SmuzhiyunCalling enable_irq_wake() causes suspend_device_irqs() to treat the given IRQ 81*4882a593Smuzhiyunin a special way. Namely, the IRQ remains enabled, by on the first interrupt 82*4882a593Smuzhiyunit will be disabled, marked as pending and "suspended" so that it will be 83*4882a593Smuzhiyunre-enabled by resume_device_irqs() during the subsequent system resume. Also 84*4882a593Smuzhiyunthe PM core is notified about the event which causes the system suspend in 85*4882a593Smuzhiyunprogress to be aborted (that doesn't have to happen immediately, but at one 86*4882a593Smuzhiyunof the points where the suspend thread looks for pending wakeup events). 87*4882a593Smuzhiyun 88*4882a593SmuzhiyunThis way every interrupt from a wakeup interrupt source will either cause the 89*4882a593Smuzhiyunsystem suspend currently in progress to be aborted or wake up the system if 90*4882a593Smuzhiyunalready suspended. However, after suspend_device_irqs() interrupt handlers are 91*4882a593Smuzhiyunnot executed for system wakeup IRQs. They are only executed for IRQF_NO_SUSPEND 92*4882a593SmuzhiyunIRQs at that time, but those IRQs should not be configured for system wakeup 93*4882a593Smuzhiyunusing enable_irq_wake(). 94*4882a593Smuzhiyun 95*4882a593Smuzhiyun 96*4882a593SmuzhiyunInterrupts and Suspend-to-Idle 97*4882a593Smuzhiyun------------------------------ 98*4882a593Smuzhiyun 99*4882a593SmuzhiyunSuspend-to-idle (also known as the "freeze" sleep state) is a relatively new 100*4882a593Smuzhiyunsystem sleep state that works by idling all of the processors and waiting for 101*4882a593Smuzhiyuninterrupts right after the "noirq" phase of suspending devices. 102*4882a593Smuzhiyun 103*4882a593SmuzhiyunOf course, this means that all of the interrupts with the IRQF_NO_SUSPEND flag 104*4882a593Smuzhiyunset will bring CPUs out of idle while in that state, but they will not cause the 105*4882a593SmuzhiyunIRQ subsystem to trigger a system wakeup. 106*4882a593Smuzhiyun 107*4882a593SmuzhiyunSystem wakeup interrupts, in turn, will trigger wakeup from suspend-to-idle in 108*4882a593Smuzhiyunanalogy with what they do in the full system suspend case. The only difference 109*4882a593Smuzhiyunis that the wakeup from suspend-to-idle is signaled using the usual working 110*4882a593Smuzhiyunstate interrupt delivery mechanisms and doesn't require the platform to use 111*4882a593Smuzhiyunany special interrupt handling logic for it to work. 112*4882a593Smuzhiyun 113*4882a593Smuzhiyun 114*4882a593SmuzhiyunIRQF_NO_SUSPEND and enable_irq_wake() 115*4882a593Smuzhiyun------------------------------------- 116*4882a593Smuzhiyun 117*4882a593SmuzhiyunThere are very few valid reasons to use both enable_irq_wake() and the 118*4882a593SmuzhiyunIRQF_NO_SUSPEND flag on the same IRQ, and it is never valid to use both for the 119*4882a593Smuzhiyunsame device. 120*4882a593Smuzhiyun 121*4882a593SmuzhiyunFirst of all, if the IRQ is not shared, the rules for handling IRQF_NO_SUSPEND 122*4882a593Smuzhiyuninterrupts (interrupt handlers are invoked after suspend_device_irqs()) are 123*4882a593Smuzhiyundirectly at odds with the rules for handling system wakeup interrupts (interrupt 124*4882a593Smuzhiyunhandlers are not invoked after suspend_device_irqs()). 125*4882a593Smuzhiyun 126*4882a593SmuzhiyunSecond, both enable_irq_wake() and IRQF_NO_SUSPEND apply to entire IRQs and not 127*4882a593Smuzhiyunto individual interrupt handlers, so sharing an IRQ between a system wakeup 128*4882a593Smuzhiyuninterrupt source and an IRQF_NO_SUSPEND interrupt source does not generally 129*4882a593Smuzhiyunmake sense. 130*4882a593Smuzhiyun 131*4882a593SmuzhiyunIn rare cases an IRQ can be shared between a wakeup device driver and an 132*4882a593SmuzhiyunIRQF_NO_SUSPEND user. In order for this to be safe, the wakeup device driver 133*4882a593Smuzhiyunmust be able to discern spurious IRQs from genuine wakeup events (signalling 134*4882a593Smuzhiyunthe latter to the core with pm_system_wakeup()), must use enable_irq_wake() to 135*4882a593Smuzhiyunensure that the IRQ will function as a wakeup source, and must request the IRQ 136*4882a593Smuzhiyunwith IRQF_COND_SUSPEND to tell the core that it meets these requirements. If 137*4882a593Smuzhiyunthese requirements are not met, it is not valid to use IRQF_COND_SUSPEND. 138