irq-domain.rst 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270
  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_*() functions (each mapping method
  37. has a different allocator function, more on that later). The function
  38. will return a pointer to the irq_domain on success. The caller must
  39. 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. When an interrupt is received, irq_find_mapping() function should
  48. be used to find the Linux IRQ number from the hwirq number.
  49. The irq_create_mapping() function must be called *atleast once*
  50. before any call to irq_find_mapping(), lest the descriptor will not
  51. be allocated.
  52. If the driver has the Linux IRQ number or the irq_data pointer, and
  53. needs to know the associated hwirq number (such as in the irq_chip
  54. callbacks) then it can be directly obtained from irq_data->hwirq.
  55. Types of irq_domain mappings
  56. ============================
  57. There are several mechanisms available for reverse mapping from hwirq
  58. to Linux irq, and each mechanism uses a different allocation function.
  59. Which reverse map type should be used depends on the use case. Each
  60. of the reverse map types are described below:
  61. Linear
  62. ------
  63. ::
  64. irq_domain_add_linear()
  65. irq_domain_create_linear()
  66. The linear reverse map maintains a fixed size table indexed by the
  67. hwirq number. When a hwirq is mapped, an irq_desc is allocated for
  68. the hwirq, and the IRQ number is stored in the table.
  69. The Linear map is a good choice when the maximum number of hwirqs is
  70. fixed and a relatively small number (~ < 256). The advantages of this
  71. map are fixed time lookup for IRQ numbers, and irq_descs are only
  72. allocated for in-use IRQs. The disadvantage is that the table must be
  73. as large as the largest possible hwirq number.
  74. irq_domain_add_linear() and irq_domain_create_linear() are functionally
  75. equivalent, except for the first argument is different - the former
  76. accepts an Open Firmware specific 'struct device_node', while the latter
  77. accepts a more general abstraction 'struct fwnode_handle'.
  78. The majority of drivers should use the linear map.
  79. Tree
  80. ----
  81. ::
  82. irq_domain_add_tree()
  83. irq_domain_create_tree()
  84. The irq_domain maintains a radix tree map from hwirq numbers to Linux
  85. IRQs. When an hwirq is mapped, an irq_desc is allocated and the
  86. hwirq is used as the lookup key for the radix tree.
  87. The tree map is a good choice if the hwirq number can be very large
  88. since it doesn't need to allocate a table as large as the largest
  89. hwirq number. The disadvantage is that hwirq to IRQ number lookup is
  90. dependent on how many entries are in the table.
  91. irq_domain_add_tree() and irq_domain_create_tree() are functionally
  92. equivalent, except for the first argument is different - the former
  93. accepts an Open Firmware specific 'struct device_node', while the latter
  94. accepts a more general abstraction 'struct fwnode_handle'.
  95. Very few drivers should need this mapping.
  96. No Map
  97. ------
  98. ::
  99. irq_domain_add_nomap()
  100. The No Map mapping is to be used when the hwirq number is
  101. programmable in the hardware. In this case it is best to program the
  102. Linux IRQ number into the hardware itself so that no mapping is
  103. required. Calling irq_create_direct_mapping() will allocate a Linux
  104. IRQ number and call the .map() callback so that driver can program the
  105. Linux IRQ number into the hardware.
  106. Most drivers cannot use this mapping.
  107. Legacy
  108. ------
  109. ::
  110. irq_domain_add_simple()
  111. irq_domain_add_legacy()
  112. irq_domain_add_legacy_isa()
  113. The Legacy mapping is a special case for drivers that already have a
  114. range of irq_descs allocated for the hwirqs. It is used when the
  115. driver cannot be immediately converted to use the linear mapping. For
  116. example, many embedded system board support files use a set of #defines
  117. for IRQ numbers that are passed to struct device registrations. In that
  118. case the Linux IRQ numbers cannot be dynamically assigned and the legacy
  119. mapping should be used.
  120. The legacy map assumes a contiguous range of IRQ numbers has already
  121. been allocated for the controller and that the IRQ number can be
  122. calculated by adding a fixed offset to the hwirq number, and
  123. visa-versa. The disadvantage is that it requires the interrupt
  124. controller to manage IRQ allocations and it requires an irq_desc to be
  125. allocated for every hwirq, even if it is unused.
  126. The legacy map should only be used if fixed IRQ mappings must be
  127. supported. For example, ISA controllers would use the legacy map for
  128. mapping Linux IRQs 0-15 so that existing ISA drivers get the correct IRQ
  129. numbers.
  130. Most users of legacy mappings should use irq_domain_add_simple() which
  131. will use a legacy domain only if an IRQ range is supplied by the
  132. system and will otherwise use a linear domain mapping. The semantics
  133. of this call are such that if an IRQ range is specified then
  134. descriptors will be allocated on-the-fly for it, and if no range is
  135. specified it will fall through to irq_domain_add_linear() which means
  136. *no* irq descriptors will be allocated.
  137. A typical use case for simple domains is where an irqchip provider
  138. is supporting both dynamic and static IRQ assignments.
  139. In order to avoid ending up in a situation where a linear domain is
  140. used and no descriptor gets allocated it is very important to make sure
  141. that the driver using the simple domain call irq_create_mapping()
  142. before any irq_find_mapping() since the latter will actually work
  143. for the static IRQ assignment case.
  144. Hierarchy IRQ domain
  145. --------------------
  146. On some architectures, there may be multiple interrupt controllers
  147. involved in delivering an interrupt from the device to the target CPU.
  148. Let's look at a typical interrupt delivering path on x86 platforms::
  149. Device --> IOAPIC -> Interrupt remapping Controller -> Local APIC -> CPU
  150. There are three interrupt controllers involved:
  151. 1) IOAPIC controller
  152. 2) Interrupt remapping controller
  153. 3) Local APIC controller
  154. To support such a hardware topology and make software architecture match
  155. hardware architecture, an irq_domain data structure is built for each
  156. interrupt controller and those irq_domains are organized into hierarchy.
  157. When building irq_domain hierarchy, the irq_domain near to the device is
  158. child and the irq_domain near to CPU is parent. So a hierarchy structure
  159. as below will be built for the example above::
  160. CPU Vector irq_domain (root irq_domain to manage CPU vectors)
  161. ^
  162. |
  163. Interrupt Remapping irq_domain (manage irq_remapping entries)
  164. ^
  165. |
  166. IOAPIC irq_domain (manage IOAPIC delivery entries/pins)
  167. There are four major interfaces to use hierarchy irq_domain:
  168. 1) irq_domain_alloc_irqs(): allocate IRQ descriptors and interrupt
  169. controller related resources to deliver these interrupts.
  170. 2) irq_domain_free_irqs(): free IRQ descriptors and interrupt controller
  171. related resources associated with these interrupts.
  172. 3) irq_domain_activate_irq(): activate interrupt controller hardware to
  173. deliver the interrupt.
  174. 4) irq_domain_deactivate_irq(): deactivate interrupt controller hardware
  175. to stop delivering the interrupt.
  176. Following changes are needed to support hierarchy irq_domain:
  177. 1) a new field 'parent' is added to struct irq_domain; it's used to
  178. maintain irq_domain hierarchy information.
  179. 2) a new field 'parent_data' is added to struct irq_data; it's used to
  180. build hierarchy irq_data to match hierarchy irq_domains. The irq_data
  181. is used to store irq_domain pointer and hardware irq number.
  182. 3) new callbacks are added to struct irq_domain_ops to support hierarchy
  183. irq_domain operations.
  184. With support of hierarchy irq_domain and hierarchy irq_data ready, an
  185. irq_domain structure is built for each interrupt controller, and an
  186. irq_data structure is allocated for each irq_domain associated with an
  187. IRQ. Now we could go one step further to support stacked(hierarchy)
  188. irq_chip. That is, an irq_chip is associated with each irq_data along
  189. the hierarchy. A child irq_chip may implement a required action by
  190. itself or by cooperating with its parent irq_chip.
  191. With stacked irq_chip, interrupt controller driver only needs to deal
  192. with the hardware managed by itself and may ask for services from its
  193. parent irq_chip when needed. So we could achieve a much cleaner
  194. software architecture.
  195. For an interrupt controller driver to support hierarchy irq_domain, it
  196. needs to:
  197. 1) Implement irq_domain_ops.alloc and irq_domain_ops.free
  198. 2) Optionally implement irq_domain_ops.activate and
  199. irq_domain_ops.deactivate.
  200. 3) Optionally implement an irq_chip to manage the interrupt controller
  201. hardware.
  202. 4) No need to implement irq_domain_ops.map and irq_domain_ops.unmap,
  203. they are unused with hierarchy irq_domain.
  204. Hierarchy irq_domain is in no way x86 specific, and is heavily used to
  205. support other architectures, such as ARM, ARM64 etc.
  206. Debugging
  207. =========
  208. Most of the internals of the IRQ subsystem are exposed in debugfs by
  209. turning CONFIG_GENERIC_IRQ_DEBUGFS on.