Back to home page

LXR

 
 

    


0001 irq_domain interrupt number mapping library
0002 
0003 The current design of the Linux kernel uses a single large number
0004 space where each separate IRQ source is assigned a different number.
0005 This is simple when there is only one interrupt controller, but in
0006 systems with multiple interrupt controllers the kernel must ensure
0007 that each one gets assigned non-overlapping allocations of Linux
0008 IRQ numbers.
0009 
0010 The number of interrupt controllers registered as unique irqchips
0011 show a rising tendency: for example subdrivers of different kinds
0012 such as GPIO controllers avoid reimplementing identical callback
0013 mechanisms as the IRQ core system by modelling their interrupt
0014 handlers as irqchips, i.e. in effect cascading interrupt controllers.
0015 
0016 Here the interrupt number loose all kind of correspondence to
0017 hardware interrupt numbers: whereas in the past, IRQ numbers could
0018 be chosen so they matched the hardware IRQ line into the root
0019 interrupt controller (i.e. the component actually fireing the
0020 interrupt line to the CPU) nowadays this number is just a number.
0021 
0022 For this reason we need a mechanism to separate controller-local
0023 interrupt numbers, called hardware irq's, from Linux IRQ numbers.
0024 
0025 The irq_alloc_desc*() and irq_free_desc*() APIs provide allocation of
0026 irq numbers, but they don't provide any support for reverse mapping of
0027 the controller-local IRQ (hwirq) number into the Linux IRQ number
0028 space.
0029 
0030 The irq_domain library adds mapping between hwirq and IRQ numbers on
0031 top of the irq_alloc_desc*() API.  An irq_domain to manage mapping is
0032 preferred over interrupt controller drivers open coding their own
0033 reverse mapping scheme.
0034 
0035 irq_domain also implements translation from an abstract irq_fwspec
0036 structure to hwirq numbers (Device Tree and ACPI GSI so far), and can
0037 be easily extended to support other IRQ topology data sources.
0038 
0039 === irq_domain usage ===
0040 An interrupt controller driver creates and registers an irq_domain by
0041 calling one of the irq_domain_add_*() functions (each mapping method
0042 has a different allocator function, more on that later).  The function
0043 will return a pointer to the irq_domain on success.  The caller must
0044 provide the allocator function with an irq_domain_ops structure.
0045 
0046 In most cases, the irq_domain will begin empty without any mappings
0047 between hwirq and IRQ numbers.  Mappings are added to the irq_domain
0048 by calling irq_create_mapping() which accepts the irq_domain and a
0049 hwirq number as arguments.  If a mapping for the hwirq doesn't already
0050 exist then it will allocate a new Linux irq_desc, associate it with
0051 the hwirq, and call the .map() callback so the driver can perform any
0052 required hardware setup.
0053 
0054 When an interrupt is received, irq_find_mapping() function should
0055 be used to find the Linux IRQ number from the hwirq number.
0056 
0057 The irq_create_mapping() function must be called *atleast once*
0058 before any call to irq_find_mapping(), lest the descriptor will not
0059 be allocated.
0060 
0061 If the driver has the Linux IRQ number or the irq_data pointer, and
0062 needs to know the associated hwirq number (such as in the irq_chip
0063 callbacks) then it can be directly obtained from irq_data->hwirq.
0064 
0065 === Types of irq_domain mappings ===
0066 There are several mechanisms available for reverse mapping from hwirq
0067 to Linux irq, and each mechanism uses a different allocation function.
0068 Which reverse map type should be used depends on the use case.  Each
0069 of the reverse map types are described below:
0070 
0071 ==== Linear ====
0072 irq_domain_add_linear()
0073 irq_domain_create_linear()
0074 
0075 The linear reverse map maintains a fixed size table indexed by the
0076 hwirq number.  When a hwirq is mapped, an irq_desc is allocated for
0077 the hwirq, and the IRQ number is stored in the table.
0078 
0079 The Linear map is a good choice when the maximum number of hwirqs is
0080 fixed and a relatively small number (~ < 256).  The advantages of this
0081 map are fixed time lookup for IRQ numbers, and irq_descs are only
0082 allocated for in-use IRQs.  The disadvantage is that the table must be
0083 as large as the largest possible hwirq number.
0084 
0085 irq_domain_add_linear() and irq_domain_create_linear() are functionally
0086 equivalent, except for the first argument is different - the former
0087 accepts an Open Firmware specific 'struct device_node', while the latter
0088 accepts a more general abstraction 'struct fwnode_handle'.
0089 
0090 The majority of drivers should use the linear map.
0091 
0092 ==== Tree ====
0093 irq_domain_add_tree()
0094 irq_domain_create_tree()
0095 
0096 The irq_domain maintains a radix tree map from hwirq numbers to Linux
0097 IRQs.  When an hwirq is mapped, an irq_desc is allocated and the
0098 hwirq is used as the lookup key for the radix tree.
0099 
0100 The tree map is a good choice if the hwirq number can be very large
0101 since it doesn't need to allocate a table as large as the largest
0102 hwirq number.  The disadvantage is that hwirq to IRQ number lookup is
0103 dependent on how many entries are in the table.
0104 
0105 irq_domain_add_tree() and irq_domain_create_tree() are functionally
0106 equivalent, except for the first argument is different - the former
0107 accepts an Open Firmware specific 'struct device_node', while the latter
0108 accepts a more general abstraction 'struct fwnode_handle'.
0109 
0110 Very few drivers should need this mapping.
0111 
0112 ==== No Map ===-
0113 irq_domain_add_nomap()
0114 
0115 The No Map mapping is to be used when the hwirq number is
0116 programmable in the hardware.  In this case it is best to program the
0117 Linux IRQ number into the hardware itself so that no mapping is
0118 required.  Calling irq_create_direct_mapping() will allocate a Linux
0119 IRQ number and call the .map() callback so that driver can program the
0120 Linux IRQ number into the hardware.
0121 
0122 Most drivers cannot use this mapping.
0123 
0124 ==== Legacy ====
0125 irq_domain_add_simple()
0126 irq_domain_add_legacy()
0127 irq_domain_add_legacy_isa()
0128 
0129 The Legacy mapping is a special case for drivers that already have a
0130 range of irq_descs allocated for the hwirqs.  It is used when the
0131 driver cannot be immediately converted to use the linear mapping.  For
0132 example, many embedded system board support files use a set of #defines
0133 for IRQ numbers that are passed to struct device registrations.  In that
0134 case the Linux IRQ numbers cannot be dynamically assigned and the legacy
0135 mapping should be used.
0136 
0137 The legacy map assumes a contiguous range of IRQ numbers has already
0138 been allocated for the controller and that the IRQ number can be
0139 calculated by adding a fixed offset to the hwirq number, and
0140 visa-versa.  The disadvantage is that it requires the interrupt
0141 controller to manage IRQ allocations and it requires an irq_desc to be
0142 allocated for every hwirq, even if it is unused.
0143 
0144 The legacy map should only be used if fixed IRQ mappings must be
0145 supported.  For example, ISA controllers would use the legacy map for
0146 mapping Linux IRQs 0-15 so that existing ISA drivers get the correct IRQ
0147 numbers.
0148 
0149 Most users of legacy mappings should use irq_domain_add_simple() which
0150 will use a legacy domain only if an IRQ range is supplied by the
0151 system and will otherwise use a linear domain mapping. The semantics
0152 of this call are such that if an IRQ range is specified then
0153 descriptors will be allocated on-the-fly for it, and if no range is
0154 specified it will fall through to irq_domain_add_linear() which means
0155 *no* irq descriptors will be allocated.
0156 
0157 A typical use case for simple domains is where an irqchip provider
0158 is supporting both dynamic and static IRQ assignments.
0159 
0160 In order to avoid ending up in a situation where a linear domain is
0161 used and no descriptor gets allocated it is very important to make sure
0162 that the driver using the simple domain call irq_create_mapping()
0163 before any irq_find_mapping() since the latter will actually work
0164 for the static IRQ assignment case.
0165 
0166 ==== Hierarchy IRQ domain ====
0167 On some architectures, there may be multiple interrupt controllers
0168 involved in delivering an interrupt from the device to the target CPU.
0169 Let's look at a typical interrupt delivering path on x86 platforms:
0170 
0171 Device --> IOAPIC -> Interrupt remapping Controller -> Local APIC -> CPU
0172 
0173 There are three interrupt controllers involved:
0174 1) IOAPIC controller
0175 2) Interrupt remapping controller
0176 3) Local APIC controller
0177 
0178 To support such a hardware topology and make software architecture match
0179 hardware architecture, an irq_domain data structure is built for each
0180 interrupt controller and those irq_domains are organized into hierarchy.
0181 When building irq_domain hierarchy, the irq_domain near to the device is
0182 child and the irq_domain near to CPU is parent. So a hierarchy structure
0183 as below will be built for the example above.
0184         CPU Vector irq_domain (root irq_domain to manage CPU vectors)
0185                 ^
0186                 |
0187         Interrupt Remapping irq_domain (manage irq_remapping entries)
0188                 ^
0189                 |
0190         IOAPIC irq_domain (manage IOAPIC delivery entries/pins)
0191 
0192 There are four major interfaces to use hierarchy irq_domain:
0193 1) irq_domain_alloc_irqs(): allocate IRQ descriptors and interrupt
0194    controller related resources to deliver these interrupts.
0195 2) irq_domain_free_irqs(): free IRQ descriptors and interrupt controller
0196    related resources associated with these interrupts.
0197 3) irq_domain_activate_irq(): activate interrupt controller hardware to
0198    deliver the interrupt.
0199 4) irq_domain_deactivate_irq(): deactivate interrupt controller hardware
0200    to stop delivering the interrupt.
0201 
0202 Following changes are needed to support hierarchy irq_domain.
0203 1) a new field 'parent' is added to struct irq_domain; it's used to
0204    maintain irq_domain hierarchy information.
0205 2) a new field 'parent_data' is added to struct irq_data; it's used to
0206    build hierarchy irq_data to match hierarchy irq_domains. The irq_data
0207    is used to store irq_domain pointer and hardware irq number.
0208 3) new callbacks are added to struct irq_domain_ops to support hierarchy
0209    irq_domain operations.
0210 
0211 With support of hierarchy irq_domain and hierarchy irq_data ready, an
0212 irq_domain structure is built for each interrupt controller, and an
0213 irq_data structure is allocated for each irq_domain associated with an
0214 IRQ. Now we could go one step further to support stacked(hierarchy)
0215 irq_chip. That is, an irq_chip is associated with each irq_data along
0216 the hierarchy. A child irq_chip may implement a required action by
0217 itself or by cooperating with its parent irq_chip.
0218 
0219 With stacked irq_chip, interrupt controller driver only needs to deal
0220 with the hardware managed by itself and may ask for services from its
0221 parent irq_chip when needed. So we could achieve a much cleaner
0222 software architecture.
0223 
0224 For an interrupt controller driver to support hierarchy irq_domain, it
0225 needs to:
0226 1) Implement irq_domain_ops.alloc and irq_domain_ops.free
0227 2) Optionally implement irq_domain_ops.activate and
0228    irq_domain_ops.deactivate.
0229 3) Optionally implement an irq_chip to manage the interrupt controller
0230    hardware.
0231 4) No need to implement irq_domain_ops.map and irq_domain_ops.unmap,
0232    they are unused with hierarchy irq_domain.
0233 
0234 Hierarchy irq_domain may also be used to support other architectures,
0235 such as ARM, ARM64 etc.