irq-domain.rst 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297
  1. ===============================================
  2. The irq_domain interrupt number mapping library
  3. ===============================================
  4. The current design of the Linux kernel uses a single large number
  5. space where each separate IRQ source is assigned a different number.
  6. This is simple when there is only one interrupt controller, but in
  7. systems with multiple interrupt controllers the kernel must ensure
  8. that each one gets assigned non-overlapping allocations of Linux
  9. IRQ numbers.
  10. The number of interrupt controllers registered as unique irqchips
  11. show a rising tendency: for example subdrivers of different kinds
  12. such as GPIO controllers avoid reimplementing identical callback
  13. mechanisms as the IRQ core system by modelling their interrupt
  14. handlers as irqchips, i.e. in effect cascading interrupt controllers.
  15. Here the interrupt number loose all kind of correspondence to
  16. hardware interrupt numbers: whereas in the past, IRQ numbers could
  17. be chosen so they matched the hardware IRQ line into the root
  18. interrupt controller (i.e. the component actually fireing the
  19. interrupt line to the CPU) nowadays this number is just a number.
  20. For this reason we need a mechanism to separate controller-local
  21. interrupt numbers, called hardware irq's, from Linux IRQ numbers.
  22. The irq_alloc_desc*() and irq_free_desc*() APIs provide allocation of
  23. irq numbers, but they don't provide any support for reverse mapping of
  24. the controller-local IRQ (hwirq) number into the Linux IRQ number
  25. space.
  26. The irq_domain library adds mapping between hwirq and IRQ numbers on
  27. top of the irq_alloc_desc*() API. An irq_domain to manage mapping is
  28. preferred over interrupt controller drivers open coding their own
  29. reverse mapping scheme.
  30. irq_domain also implements translation from an abstract irq_fwspec
  31. structure to hwirq numbers (Device Tree and ACPI GSI so far), and can
  32. be easily extended to support other IRQ topology data sources.
  33. irq_domain usage
  34. ================
  35. An interrupt controller driver creates and registers an irq_domain by
  36. calling one of the irq_domain_add_*() or irq_domain_create_*() functions
  37. (each mapping method has a different allocator function, more on that later).
  38. The function will return a pointer to the irq_domain on success. The caller
  39. must provide the allocator function with an irq_domain_ops structure.
  40. In most cases, the irq_domain will begin empty without any mappings
  41. between hwirq and IRQ numbers. Mappings are added to the irq_domain
  42. by calling irq_create_mapping() which accepts the irq_domain and a
  43. hwirq number as arguments. If a mapping for the hwirq doesn't already
  44. exist then it will allocate a new Linux irq_desc, associate it with
  45. the hwirq, and call the .map() callback so the driver can perform any
  46. required hardware setup.
  47. Once a mapping has been established, it can be retrieved or used via a
  48. variety of methods:
  49. - irq_resolve_mapping() returns a pointer to the irq_desc structure
  50. for a given domain and hwirq number, and NULL if there was no
  51. mapping.
  52. - irq_find_mapping() returns a Linux IRQ number for a given domain and
  53. hwirq number, and 0 if there was no mapping
  54. - irq_linear_revmap() is now identical to irq_find_mapping(), and is
  55. deprecated
  56. - generic_handle_domain_irq() handles an interrupt described by a
  57. domain and a hwirq number
  58. Note that irq domain lookups must happen in contexts that are
  59. compatible with a RCU read-side critical section.
  60. The irq_create_mapping() function must be called *at least once*
  61. before any call to irq_find_mapping(), lest the descriptor will not
  62. be allocated.
  63. If the driver has the Linux IRQ number or the irq_data pointer, and
  64. needs to know the associated hwirq number (such as in the irq_chip
  65. callbacks) then it can be directly obtained from irq_data->hwirq.
  66. Types of irq_domain mappings
  67. ============================
  68. There are several mechanisms available for reverse mapping from hwirq
  69. to Linux irq, and each mechanism uses a different allocation function.
  70. Which reverse map type should be used depends on the use case. Each
  71. of the reverse map types are described below:
  72. Linear
  73. ------
  74. ::
  75. irq_domain_add_linear()
  76. irq_domain_create_linear()
  77. The linear reverse map maintains a fixed size table indexed by the
  78. hwirq number. When a hwirq is mapped, an irq_desc is allocated for
  79. the hwirq, and the IRQ number is stored in the table.
  80. The Linear map is a good choice when the maximum number of hwirqs is
  81. fixed and a relatively small number (~ < 256). The advantages of this
  82. map are fixed time lookup for IRQ numbers, and irq_descs are only
  83. allocated for in-use IRQs. The disadvantage is that the table must be
  84. as large as the largest possible hwirq number.
  85. irq_domain_add_linear() and irq_domain_create_linear() are functionally
  86. equivalent, except for the first argument is different - the former
  87. accepts an Open Firmware specific 'struct device_node', while the latter
  88. accepts a more general abstraction 'struct fwnode_handle'.
  89. The majority of drivers should use the linear map.
  90. Tree
  91. ----
  92. ::
  93. irq_domain_add_tree()
  94. irq_domain_create_tree()
  95. The irq_domain maintains a radix tree map from hwirq numbers to Linux
  96. IRQs. When an hwirq is mapped, an irq_desc is allocated and the
  97. hwirq is used as the lookup key for the radix tree.
  98. The tree map is a good choice if the hwirq number can be very large
  99. since it doesn't need to allocate a table as large as the largest
  100. hwirq number. The disadvantage is that hwirq to IRQ number lookup is
  101. dependent on how many entries are in the table.
  102. irq_domain_add_tree() and irq_domain_create_tree() are functionally
  103. equivalent, except for the first argument is different - the former
  104. accepts an Open Firmware specific 'struct device_node', while the latter
  105. accepts a more general abstraction 'struct fwnode_handle'.
  106. Very few drivers should need this mapping.
  107. No Map
  108. ------
  109. ::
  110. irq_domain_add_nomap()
  111. The No Map mapping is to be used when the hwirq number is
  112. programmable in the hardware. In this case it is best to program the
  113. Linux IRQ number into the hardware itself so that no mapping is
  114. required. Calling irq_create_direct_mapping() will allocate a Linux
  115. IRQ number and call the .map() callback so that driver can program the
  116. Linux IRQ number into the hardware.
  117. Most drivers cannot use this mapping, and it is now gated on the
  118. CONFIG_IRQ_DOMAIN_NOMAP option. Please refrain from introducing new
  119. users of this API.
  120. Legacy
  121. ------
  122. ::
  123. irq_domain_add_simple()
  124. irq_domain_add_legacy()
  125. irq_domain_create_simple()
  126. irq_domain_create_legacy()
  127. The Legacy mapping is a special case for drivers that already have a
  128. range of irq_descs allocated for the hwirqs. It is used when the
  129. driver cannot be immediately converted to use the linear mapping. For
  130. example, many embedded system board support files use a set of #defines
  131. for IRQ numbers that are passed to struct device registrations. In that
  132. case the Linux IRQ numbers cannot be dynamically assigned and the legacy
  133. mapping should be used.
  134. As the name implies, the \*_legacy() functions are deprecated and only
  135. exist to ease the support of ancient platforms. No new users should be
  136. added. Same goes for the \*_simple() functions when their use results
  137. in the legacy behaviour.
  138. The legacy map assumes a contiguous range of IRQ numbers has already
  139. been allocated for the controller and that the IRQ number can be
  140. calculated by adding a fixed offset to the hwirq number, and
  141. visa-versa. The disadvantage is that it requires the interrupt
  142. controller to manage IRQ allocations and it requires an irq_desc to be
  143. allocated for every hwirq, even if it is unused.
  144. The legacy map should only be used if fixed IRQ mappings must be
  145. supported. For example, ISA controllers would use the legacy map for
  146. mapping Linux IRQs 0-15 so that existing ISA drivers get the correct IRQ
  147. numbers.
  148. Most users of legacy mappings should use irq_domain_add_simple() or
  149. irq_domain_create_simple() which will use a legacy domain only if an IRQ range
  150. is supplied by the system and will otherwise use a linear domain mapping.
  151. The semantics of this call are such that if an IRQ range is specified then
  152. descriptors will be allocated on-the-fly for it, and if no range is
  153. specified it will fall through to irq_domain_add_linear() or
  154. irq_domain_create_linear() which means *no* irq descriptors will be allocated.
  155. A typical use case for simple domains is where an irqchip provider
  156. is supporting both dynamic and static IRQ assignments.
  157. In order to avoid ending up in a situation where a linear domain is
  158. used and no descriptor gets allocated it is very important to make sure
  159. that the driver using the simple domain call irq_create_mapping()
  160. before any irq_find_mapping() since the latter will actually work
  161. for the static IRQ assignment case.
  162. irq_domain_add_simple() and irq_domain_create_simple() as well as
  163. irq_domain_add_legacy() and irq_domain_create_legacy() are functionally
  164. equivalent, except for the first argument is different - the former
  165. accepts an Open Firmware specific 'struct device_node', while the latter
  166. accepts a more general abstraction 'struct fwnode_handle'.
  167. Hierarchy IRQ domain
  168. --------------------
  169. On some architectures, there may be multiple interrupt controllers
  170. involved in delivering an interrupt from the device to the target CPU.
  171. Let's look at a typical interrupt delivering path on x86 platforms::
  172. Device --> IOAPIC -> Interrupt remapping Controller -> Local APIC -> CPU
  173. There are three interrupt controllers involved:
  174. 1) IOAPIC controller
  175. 2) Interrupt remapping controller
  176. 3) Local APIC controller
  177. To support such a hardware topology and make software architecture match
  178. hardware architecture, an irq_domain data structure is built for each
  179. interrupt controller and those irq_domains are organized into hierarchy.
  180. When building irq_domain hierarchy, the irq_domain near to the device is
  181. child and the irq_domain near to CPU is parent. So a hierarchy structure
  182. as below will be built for the example above::
  183. CPU Vector irq_domain (root irq_domain to manage CPU vectors)
  184. ^
  185. |
  186. Interrupt Remapping irq_domain (manage irq_remapping entries)
  187. ^
  188. |
  189. IOAPIC irq_domain (manage IOAPIC delivery entries/pins)
  190. There are four major interfaces to use hierarchy irq_domain:
  191. 1) irq_domain_alloc_irqs(): allocate IRQ descriptors and interrupt
  192. controller related resources to deliver these interrupts.
  193. 2) irq_domain_free_irqs(): free IRQ descriptors and interrupt controller
  194. related resources associated with these interrupts.
  195. 3) irq_domain_activate_irq(): activate interrupt controller hardware to
  196. deliver the interrupt.
  197. 4) irq_domain_deactivate_irq(): deactivate interrupt controller hardware
  198. to stop delivering the interrupt.
  199. Following changes are needed to support hierarchy irq_domain:
  200. 1) a new field 'parent' is added to struct irq_domain; it's used to
  201. maintain irq_domain hierarchy information.
  202. 2) a new field 'parent_data' is added to struct irq_data; it's used to
  203. build hierarchy irq_data to match hierarchy irq_domains. The irq_data
  204. is used to store irq_domain pointer and hardware irq number.
  205. 3) new callbacks are added to struct irq_domain_ops to support hierarchy
  206. irq_domain operations.
  207. With support of hierarchy irq_domain and hierarchy irq_data ready, an
  208. irq_domain structure is built for each interrupt controller, and an
  209. irq_data structure is allocated for each irq_domain associated with an
  210. IRQ. Now we could go one step further to support stacked(hierarchy)
  211. irq_chip. That is, an irq_chip is associated with each irq_data along
  212. the hierarchy. A child irq_chip may implement a required action by
  213. itself or by cooperating with its parent irq_chip.
  214. With stacked irq_chip, interrupt controller driver only needs to deal
  215. with the hardware managed by itself and may ask for services from its
  216. parent irq_chip when needed. So we could achieve a much cleaner
  217. software architecture.
  218. For an interrupt controller driver to support hierarchy irq_domain, it
  219. needs to:
  220. 1) Implement irq_domain_ops.alloc and irq_domain_ops.free
  221. 2) Optionally implement irq_domain_ops.activate and
  222. irq_domain_ops.deactivate.
  223. 3) Optionally implement an irq_chip to manage the interrupt controller
  224. hardware.
  225. 4) No need to implement irq_domain_ops.map and irq_domain_ops.unmap,
  226. they are unused with hierarchy irq_domain.
  227. Hierarchy irq_domain is in no way x86 specific, and is heavily used to
  228. support other architectures, such as ARM, ARM64 etc.
  229. Debugging
  230. =========
  231. Most of the internals of the IRQ subsystem are exposed in debugfs by
  232. turning CONFIG_GENERIC_IRQ_DEBUGFS on.