suspend-and-interrupts.txt 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. System Suspend and Device Interrupts
  2. Copyright (C) 2014 Intel Corp.
  3. Author: Rafael J. Wysocki <rafael.j.wysocki@intel.com>
  4. Suspending and Resuming Device IRQs
  5. -----------------------------------
  6. Device interrupt request lines (IRQs) are generally disabled during system
  7. suspend after the "late" phase of suspending devices (that is, after all of the
  8. ->prepare, ->suspend and ->suspend_late callbacks have been executed for all
  9. devices). That is done by suspend_device_irqs().
  10. The rationale for doing so is that after the "late" phase of device suspend
  11. there is no legitimate reason why any interrupts from suspended devices should
  12. trigger and if any devices have not been suspended properly yet, it is better to
  13. block interrupts from them anyway. Also, in the past we had problems with
  14. interrupt handlers for shared IRQs that device drivers implementing them were
  15. not prepared for interrupts triggering after their devices had been suspended.
  16. In some cases they would attempt to access, for example, memory address spaces
  17. of suspended devices and cause unpredictable behavior to ensue as a result.
  18. Unfortunately, such problems are very difficult to debug and the introduction
  19. of suspend_device_irqs(), along with the "noirq" phase of device suspend and
  20. resume, was the only practical way to mitigate them.
  21. Device IRQs are re-enabled during system resume, right before the "early" phase
  22. of resuming devices (that is, before starting to execute ->resume_early
  23. callbacks for devices). The function doing that is resume_device_irqs().
  24. The IRQF_NO_SUSPEND Flag
  25. ------------------------
  26. There are interrupts that can legitimately trigger during the entire system
  27. suspend-resume cycle, including the "noirq" phases of suspending and resuming
  28. devices as well as during the time when nonboot CPUs are taken offline and
  29. brought back online. That applies to timer interrupts in the first place,
  30. but also to IPIs and to some other special-purpose interrupts.
  31. The IRQF_NO_SUSPEND flag is used to indicate that to the IRQ subsystem when
  32. requesting a special-purpose interrupt. It causes suspend_device_irqs() to
  33. leave the corresponding IRQ enabled so as to allow the interrupt to work all
  34. the time as expected.
  35. Note that the IRQF_NO_SUSPEND flag affects the entire IRQ and not just one
  36. user of it. Thus, if the IRQ is shared, all of the interrupt handlers installed
  37. for it will be executed as usual after suspend_device_irqs(), even if the
  38. IRQF_NO_SUSPEND flag was not passed to request_irq() (or equivalent) by some of
  39. the IRQ's users. For this reason, using IRQF_NO_SUSPEND and IRQF_SHARED at the
  40. same time should be avoided.
  41. System Wakeup Interrupts, enable_irq_wake() and disable_irq_wake()
  42. ------------------------------------------------------------------
  43. System wakeup interrupts generally need to be configured to wake up the system
  44. from sleep states, especially if they are used for different purposes (e.g. as
  45. I/O interrupts) in the working state.
  46. That may involve turning on a special signal handling logic within the platform
  47. (such as an SoC) so that signals from a given line are routed in a different way
  48. during system sleep so as to trigger a system wakeup when needed. For example,
  49. the platform may include a dedicated interrupt controller used specifically for
  50. handling system wakeup events. Then, if a given interrupt line is supposed to
  51. wake up the system from sleep sates, the corresponding input of that interrupt
  52. controller needs to be enabled to receive signals from the line in question.
  53. After wakeup, it generally is better to disable that input to prevent the
  54. dedicated controller from triggering interrupts unnecessarily.
  55. The IRQ subsystem provides two helper functions to be used by device drivers for
  56. those purposes. Namely, enable_irq_wake() turns on the platform's logic for
  57. handling the given IRQ as a system wakeup interrupt line and disable_irq_wake()
  58. turns that logic off.
  59. Calling enable_irq_wake() causes suspend_device_irqs() to treat the given IRQ
  60. in a special way. Namely, the IRQ remains enabled, by on the first interrupt
  61. it will be disabled, marked as pending and "suspended" so that it will be
  62. re-enabled by resume_device_irqs() during the subsequent system resume. Also
  63. the PM core is notified about the event which casues the system suspend in
  64. progress to be aborted (that doesn't have to happen immediately, but at one
  65. of the points where the suspend thread looks for pending wakeup events).
  66. This way every interrupt from a wakeup interrupt source will either cause the
  67. system suspend currently in progress to be aborted or wake up the system if
  68. already suspended. However, after suspend_device_irqs() interrupt handlers are
  69. not executed for system wakeup IRQs. They are only executed for IRQF_NO_SUSPEND
  70. IRQs at that time, but those IRQs should not be configured for system wakeup
  71. using enable_irq_wake().
  72. Interrupts and Suspend-to-Idle
  73. ------------------------------
  74. Suspend-to-idle (also known as the "freeze" sleep state) is a relatively new
  75. system sleep state that works by idling all of the processors and waiting for
  76. interrupts right after the "noirq" phase of suspending devices.
  77. Of course, this means that all of the interrupts with the IRQF_NO_SUSPEND flag
  78. set will bring CPUs out of idle while in that state, but they will not cause the
  79. IRQ subsystem to trigger a system wakeup.
  80. System wakeup interrupts, in turn, will trigger wakeup from suspend-to-idle in
  81. analogy with what they do in the full system suspend case. The only difference
  82. is that the wakeup from suspend-to-idle is signaled using the usual working
  83. state interrupt delivery mechanisms and doesn't require the platform to use
  84. any special interrupt handling logic for it to work.
  85. IRQF_NO_SUSPEND and enable_irq_wake()
  86. -------------------------------------
  87. There are no valid reasons to use both enable_irq_wake() and the IRQF_NO_SUSPEND
  88. flag on the same IRQ.
  89. First of all, if the IRQ is not shared, the rules for handling IRQF_NO_SUSPEND
  90. interrupts (interrupt handlers are invoked after suspend_device_irqs()) are
  91. directly at odds with the rules for handling system wakeup interrupts (interrupt
  92. handlers are not invoked after suspend_device_irqs()).
  93. Second, both enable_irq_wake() and IRQF_NO_SUSPEND apply to entire IRQs and not
  94. to individual interrupt handlers, so sharing an IRQ between a system wakeup
  95. interrupt source and an IRQF_NO_SUSPEND interrupt source does not make sense.