1 /* SPDX-License-Identifier: GPL-2.0 */
3 * irq_domain - IRQ translation domains
5 * Translation infrastructure between hw and linux irq numbers. This is
6 * helpful for interrupt controllers to implement mapping between hardware
7 * irq numbers and the Linux irq number space.
9 * irq_domains also have hooks for translating device tree or other
10 * firmware interrupt representations into a hardware irq number that
11 * can be mapped back to a Linux irq number without any extra platform
14 * Interrupt controller "domain" data structure. This could be defined as a
15 * irq domain controller. That is, it handles the mapping between hardware
16 * and virtual interrupt numbers for a given interrupt domain. The domain
17 * structure is generally created by the PIC code for a given PIC instance
18 * (though a domain can cover more than one PIC if they have a flat number
19 * model). It's the domain callbacks that are responsible for setting the
20 * irq_chip on a given irq_desc after it's been mapped.
22 * The host code and data structures use a fwnode_handle pointer to
23 * identify the domain. In some cases, and in order to preserve source
24 * code compatibility, this fwnode pointer is "upgraded" to a DT
25 * device_node. For those firmware infrastructures that do not provide
26 * a unique identifier for an interrupt controller, the irq_domain
27 * code offers a fwnode allocator.
30 #ifndef _LINUX_IRQDOMAIN_H
31 #define _LINUX_IRQDOMAIN_H
33 #include <linux/types.h>
34 #include <linux/irqhandler.h>
36 #include <linux/mutex.h>
37 #include <linux/radix-tree.h>
47 struct irq_affinity_desc;
49 #define IRQ_DOMAIN_IRQ_SPEC_PARAMS 16
52 * struct irq_fwspec - generic IRQ specifier structure
54 * @fwnode: Pointer to a firmware-specific descriptor
55 * @param_count: Number of device-specific parameters
56 * @param: Device-specific parameters
58 * This structure, directly modeled after of_phandle_args, is used to
59 * pass a device-specific description of an interrupt.
62 struct fwnode_handle *fwnode;
64 u32 param[IRQ_DOMAIN_IRQ_SPEC_PARAMS];
67 /* Conversion function from of_phandle_args fields to fwspec */
68 void of_phandle_args_to_fwspec(struct device_node *np, const u32 *args,
69 unsigned int count, struct irq_fwspec *fwspec);
72 * Should several domains have the same device node, but serve
73 * different purposes (for example one domain is for PCI/MSI, and the
74 * other for wired IRQs), they can be distinguished using a
75 * bus-specific token. Most domains are expected to only carry
78 enum irq_domain_bus_token {
81 DOMAIN_BUS_GENERIC_MSI,
83 DOMAIN_BUS_PLATFORM_MSI,
86 DOMAIN_BUS_FSL_MC_MSI,
87 DOMAIN_BUS_TI_SCI_INTA_MSI,
93 * struct irq_domain_ops - Methods for irq_domain objects
94 * @match: Match an interrupt controller device node to a host, returns
96 * @map: Create or update a mapping between a virtual irq number and a hw
97 * irq number. This is called only once for a given mapping.
98 * @unmap: Dispose of such a mapping
99 * @xlate: Given a device tree node and interrupt specifier, decode
100 * the hardware irq number and linux irq type value.
102 * Functions below are provided by the driver and called whenever a new mapping
103 * is created or an old mapping is disposed. The driver can then proceed to
104 * whatever internal data structures management is required. It also needs
105 * to setup the irq_desc when returning from map().
107 struct irq_domain_ops {
108 int (*match)(struct irq_domain *d, struct device_node *node,
109 enum irq_domain_bus_token bus_token);
110 int (*select)(struct irq_domain *d, struct irq_fwspec *fwspec,
111 enum irq_domain_bus_token bus_token);
112 int (*map)(struct irq_domain *d, unsigned int virq, irq_hw_number_t hw);
113 void (*unmap)(struct irq_domain *d, unsigned int virq);
114 int (*xlate)(struct irq_domain *d, struct device_node *node,
115 const u32 *intspec, unsigned int intsize,
116 unsigned long *out_hwirq, unsigned int *out_type);
117 #ifdef CONFIG_IRQ_DOMAIN_HIERARCHY
118 /* extended V2 interfaces to support hierarchy irq_domains */
119 int (*alloc)(struct irq_domain *d, unsigned int virq,
120 unsigned int nr_irqs, void *arg);
121 void (*free)(struct irq_domain *d, unsigned int virq,
122 unsigned int nr_irqs);
123 int (*activate)(struct irq_domain *d, struct irq_data *irqd, bool reserve);
124 void (*deactivate)(struct irq_domain *d, struct irq_data *irq_data);
125 int (*translate)(struct irq_domain *d, struct irq_fwspec *fwspec,
126 unsigned long *out_hwirq, unsigned int *out_type);
128 #ifdef CONFIG_GENERIC_IRQ_DEBUGFS
129 void (*debug_show)(struct seq_file *m, struct irq_domain *d,
130 struct irq_data *irqd, int ind);
134 extern struct irq_domain_ops irq_generic_chip_ops;
136 struct irq_domain_chip_generic;
139 * struct irq_domain - Hardware interrupt number translation object
140 * @link: Element in global irq_domain list.
141 * @name: Name of interrupt domain
142 * @ops: pointer to irq_domain methods
143 * @host_data: private data pointer for use by owner. Not touched by irq_domain
145 * @flags: host per irq_domain flags
146 * @mapcount: The number of mapped interrupts
149 * @fwnode: Pointer to firmware node associated with the irq_domain. Pretty easy
150 * to swap it for the of_node via the irq_domain_get_of_node accessor
151 * @gc: Pointer to a list of generic chips. There is a helper function for
152 * setting up one or more generic chips for interrupt controllers
153 * drivers using the generic chip library which uses this pointer.
154 * @parent: Pointer to parent irq_domain to support hierarchy irq_domains
156 * Revmap data, used internally by irq_domain
157 * @revmap_size: Size of the linear map table @revmap[]
158 * @revmap_tree: Radix map tree for hwirqs that don't fit in the linear map
159 * @revmap_mutex: Lock for the revmap
160 * @revmap: Linear table of irq_data pointers
163 struct list_head link;
165 const struct irq_domain_ops *ops;
168 unsigned int mapcount;
171 struct fwnode_handle *fwnode;
172 enum irq_domain_bus_token bus_token;
173 struct irq_domain_chip_generic *gc;
174 #ifdef CONFIG_IRQ_DOMAIN_HIERARCHY
175 struct irq_domain *parent;
178 /* reverse map data. The linear map gets appended to the irq_domain */
179 irq_hw_number_t hwirq_max;
180 unsigned int revmap_size;
181 struct radix_tree_root revmap_tree;
182 struct mutex revmap_mutex;
183 struct irq_data __rcu *revmap[];
186 /* Irq domain flags */
188 /* Irq domain is hierarchical */
189 IRQ_DOMAIN_FLAG_HIERARCHY = (1 << 0),
191 /* Irq domain name was allocated in __irq_domain_add() */
192 IRQ_DOMAIN_NAME_ALLOCATED = (1 << 1),
194 /* Irq domain is an IPI domain with virq per cpu */
195 IRQ_DOMAIN_FLAG_IPI_PER_CPU = (1 << 2),
197 /* Irq domain is an IPI domain with single virq */
198 IRQ_DOMAIN_FLAG_IPI_SINGLE = (1 << 3),
200 /* Irq domain implements MSIs */
201 IRQ_DOMAIN_FLAG_MSI = (1 << 4),
203 /* Irq domain implements MSI remapping */
204 IRQ_DOMAIN_FLAG_MSI_REMAP = (1 << 5),
207 * Quirk to handle MSI implementations which do not provide
208 * masking. Currently known to affect x86, but partially
209 * handled in core code.
211 IRQ_DOMAIN_MSI_NOMASK_QUIRK = (1 << 6),
213 /* Irq domain doesn't translate anything */
214 IRQ_DOMAIN_FLAG_NO_MAP = (1 << 7),
217 * Flags starting from IRQ_DOMAIN_FLAG_NONCORE are reserved
218 * for implementation specific purposes and ignored by the
221 IRQ_DOMAIN_FLAG_NONCORE = (1 << 16),
224 static inline struct device_node *irq_domain_get_of_node(struct irq_domain *d)
226 return to_of_node(d->fwnode);
229 #ifdef CONFIG_IRQ_DOMAIN
230 struct fwnode_handle *__irq_domain_alloc_fwnode(unsigned int type, int id,
231 const char *name, phys_addr_t *pa);
235 IRQCHIP_FWNODE_NAMED,
236 IRQCHIP_FWNODE_NAMED_ID,
240 struct fwnode_handle *irq_domain_alloc_named_fwnode(const char *name)
242 return __irq_domain_alloc_fwnode(IRQCHIP_FWNODE_NAMED, 0, name, NULL);
246 struct fwnode_handle *irq_domain_alloc_named_id_fwnode(const char *name, int id)
248 return __irq_domain_alloc_fwnode(IRQCHIP_FWNODE_NAMED_ID, id, name,
252 static inline struct fwnode_handle *irq_domain_alloc_fwnode(phys_addr_t *pa)
254 return __irq_domain_alloc_fwnode(IRQCHIP_FWNODE_REAL, 0, NULL, pa);
257 void irq_domain_free_fwnode(struct fwnode_handle *fwnode);
258 struct irq_domain *__irq_domain_add(struct fwnode_handle *fwnode, unsigned int size,
259 irq_hw_number_t hwirq_max, int direct_max,
260 const struct irq_domain_ops *ops,
262 struct irq_domain *irq_domain_create_simple(struct fwnode_handle *fwnode,
264 unsigned int first_irq,
265 const struct irq_domain_ops *ops,
267 struct irq_domain *irq_domain_add_legacy(struct device_node *of_node,
269 unsigned int first_irq,
270 irq_hw_number_t first_hwirq,
271 const struct irq_domain_ops *ops,
273 struct irq_domain *irq_domain_create_legacy(struct fwnode_handle *fwnode,
275 unsigned int first_irq,
276 irq_hw_number_t first_hwirq,
277 const struct irq_domain_ops *ops,
279 extern struct irq_domain *irq_find_matching_fwspec(struct irq_fwspec *fwspec,
280 enum irq_domain_bus_token bus_token);
281 extern bool irq_domain_check_msi_remap(void);
282 extern void irq_set_default_host(struct irq_domain *host);
283 extern struct irq_domain *irq_get_default_host(void);
284 extern int irq_domain_alloc_descs(int virq, unsigned int nr_irqs,
285 irq_hw_number_t hwirq, int node,
286 const struct irq_affinity_desc *affinity);
288 static inline struct fwnode_handle *of_node_to_fwnode(struct device_node *node)
290 return node ? &node->fwnode : NULL;
293 extern const struct fwnode_operations irqchip_fwnode_ops;
295 static inline bool is_fwnode_irqchip(struct fwnode_handle *fwnode)
297 return fwnode && fwnode->ops == &irqchip_fwnode_ops;
300 extern void irq_domain_update_bus_token(struct irq_domain *domain,
301 enum irq_domain_bus_token bus_token);
304 struct irq_domain *irq_find_matching_fwnode(struct fwnode_handle *fwnode,
305 enum irq_domain_bus_token bus_token)
307 struct irq_fwspec fwspec = {
311 return irq_find_matching_fwspec(&fwspec, bus_token);
314 static inline struct irq_domain *irq_find_matching_host(struct device_node *node,
315 enum irq_domain_bus_token bus_token)
317 return irq_find_matching_fwnode(of_node_to_fwnode(node), bus_token);
320 static inline struct irq_domain *irq_find_host(struct device_node *node)
322 struct irq_domain *d;
324 d = irq_find_matching_host(node, DOMAIN_BUS_WIRED);
326 d = irq_find_matching_host(node, DOMAIN_BUS_ANY);
331 static inline struct irq_domain *irq_domain_add_simple(struct device_node *of_node,
333 unsigned int first_irq,
334 const struct irq_domain_ops *ops,
337 return irq_domain_create_simple(of_node_to_fwnode(of_node), size, first_irq, ops, host_data);
341 * irq_domain_add_linear() - Allocate and register a linear revmap irq_domain.
342 * @of_node: pointer to interrupt controller's device tree node.
343 * @size: Number of interrupts in the domain.
344 * @ops: map/unmap domain callbacks
345 * @host_data: Controller private data pointer
347 static inline struct irq_domain *irq_domain_add_linear(struct device_node *of_node,
349 const struct irq_domain_ops *ops,
352 return __irq_domain_add(of_node_to_fwnode(of_node), size, size, 0, ops, host_data);
355 #ifdef CONFIG_IRQ_DOMAIN_NOMAP
356 static inline struct irq_domain *irq_domain_add_nomap(struct device_node *of_node,
357 unsigned int max_irq,
358 const struct irq_domain_ops *ops,
361 return __irq_domain_add(of_node_to_fwnode(of_node), 0, max_irq, max_irq, ops, host_data);
364 extern unsigned int irq_create_direct_mapping(struct irq_domain *host);
367 static inline struct irq_domain *irq_domain_add_tree(struct device_node *of_node,
368 const struct irq_domain_ops *ops,
371 return __irq_domain_add(of_node_to_fwnode(of_node), 0, ~0, 0, ops, host_data);
374 static inline struct irq_domain *irq_domain_create_linear(struct fwnode_handle *fwnode,
376 const struct irq_domain_ops *ops,
379 return __irq_domain_add(fwnode, size, size, 0, ops, host_data);
382 static inline struct irq_domain *irq_domain_create_tree(struct fwnode_handle *fwnode,
383 const struct irq_domain_ops *ops,
386 return __irq_domain_add(fwnode, 0, ~0, 0, ops, host_data);
389 extern void irq_domain_remove(struct irq_domain *host);
391 extern int irq_domain_associate(struct irq_domain *domain, unsigned int irq,
392 irq_hw_number_t hwirq);
393 extern void irq_domain_associate_many(struct irq_domain *domain,
394 unsigned int irq_base,
395 irq_hw_number_t hwirq_base, int count);
397 extern unsigned int irq_create_mapping_affinity(struct irq_domain *host,
398 irq_hw_number_t hwirq,
399 const struct irq_affinity_desc *affinity);
400 extern unsigned int irq_create_fwspec_mapping(struct irq_fwspec *fwspec);
401 extern void irq_dispose_mapping(unsigned int virq);
403 static inline unsigned int irq_create_mapping(struct irq_domain *host,
404 irq_hw_number_t hwirq)
406 return irq_create_mapping_affinity(host, hwirq, NULL);
409 extern struct irq_desc *__irq_resolve_mapping(struct irq_domain *domain,
410 irq_hw_number_t hwirq,
413 static inline struct irq_desc *irq_resolve_mapping(struct irq_domain *domain,
414 irq_hw_number_t hwirq)
416 return __irq_resolve_mapping(domain, hwirq, NULL);
420 * irq_find_mapping() - Find a linux irq from a hw irq number.
421 * @domain: domain owning this hardware interrupt
422 * @hwirq: hardware irq number in that domain space
424 static inline unsigned int irq_find_mapping(struct irq_domain *domain,
425 irq_hw_number_t hwirq)
429 if (__irq_resolve_mapping(domain, hwirq, &irq))
435 static inline unsigned int irq_linear_revmap(struct irq_domain *domain,
436 irq_hw_number_t hwirq)
438 return irq_find_mapping(domain, hwirq);
441 extern const struct irq_domain_ops irq_domain_simple_ops;
443 /* stock xlate functions */
444 int irq_domain_xlate_onecell(struct irq_domain *d, struct device_node *ctrlr,
445 const u32 *intspec, unsigned int intsize,
446 irq_hw_number_t *out_hwirq, unsigned int *out_type);
447 int irq_domain_xlate_twocell(struct irq_domain *d, struct device_node *ctrlr,
448 const u32 *intspec, unsigned int intsize,
449 irq_hw_number_t *out_hwirq, unsigned int *out_type);
450 int irq_domain_xlate_onetwocell(struct irq_domain *d, struct device_node *ctrlr,
451 const u32 *intspec, unsigned int intsize,
452 irq_hw_number_t *out_hwirq, unsigned int *out_type);
454 int irq_domain_translate_twocell(struct irq_domain *d,
455 struct irq_fwspec *fwspec,
456 unsigned long *out_hwirq,
457 unsigned int *out_type);
459 int irq_domain_translate_onecell(struct irq_domain *d,
460 struct irq_fwspec *fwspec,
461 unsigned long *out_hwirq,
462 unsigned int *out_type);
465 int irq_reserve_ipi(struct irq_domain *domain, const struct cpumask *dest);
466 int irq_destroy_ipi(unsigned int irq, const struct cpumask *dest);
468 /* V2 interfaces to support hierarchy IRQ domains. */
469 extern struct irq_data *irq_domain_get_irq_data(struct irq_domain *domain,
471 extern void irq_domain_set_info(struct irq_domain *domain, unsigned int virq,
472 irq_hw_number_t hwirq, struct irq_chip *chip,
473 void *chip_data, irq_flow_handler_t handler,
474 void *handler_data, const char *handler_name);
475 extern void irq_domain_reset_irq_data(struct irq_data *irq_data);
476 #ifdef CONFIG_IRQ_DOMAIN_HIERARCHY
477 extern struct irq_domain *irq_domain_create_hierarchy(struct irq_domain *parent,
478 unsigned int flags, unsigned int size,
479 struct fwnode_handle *fwnode,
480 const struct irq_domain_ops *ops, void *host_data);
482 static inline struct irq_domain *irq_domain_add_hierarchy(struct irq_domain *parent,
485 struct device_node *node,
486 const struct irq_domain_ops *ops,
489 return irq_domain_create_hierarchy(parent, flags, size,
490 of_node_to_fwnode(node),
494 extern int __irq_domain_alloc_irqs(struct irq_domain *domain, int irq_base,
495 unsigned int nr_irqs, int node, void *arg,
497 const struct irq_affinity_desc *affinity);
498 extern void irq_domain_free_irqs(unsigned int virq, unsigned int nr_irqs);
499 extern int irq_domain_activate_irq(struct irq_data *irq_data, bool early);
500 extern void irq_domain_deactivate_irq(struct irq_data *irq_data);
502 static inline int irq_domain_alloc_irqs(struct irq_domain *domain,
503 unsigned int nr_irqs, int node, void *arg)
505 return __irq_domain_alloc_irqs(domain, -1, nr_irqs, node, arg, false,
509 extern int irq_domain_alloc_irqs_hierarchy(struct irq_domain *domain,
510 unsigned int irq_base,
511 unsigned int nr_irqs, void *arg);
512 extern int irq_domain_set_hwirq_and_chip(struct irq_domain *domain,
514 irq_hw_number_t hwirq,
515 struct irq_chip *chip,
517 extern void irq_domain_free_irqs_common(struct irq_domain *domain,
519 unsigned int nr_irqs);
520 extern void irq_domain_free_irqs_top(struct irq_domain *domain,
521 unsigned int virq, unsigned int nr_irqs);
523 extern int irq_domain_push_irq(struct irq_domain *domain, int virq, void *arg);
524 extern int irq_domain_pop_irq(struct irq_domain *domain, int virq);
526 extern int irq_domain_alloc_irqs_parent(struct irq_domain *domain,
527 unsigned int irq_base,
528 unsigned int nr_irqs, void *arg);
530 extern void irq_domain_free_irqs_parent(struct irq_domain *domain,
531 unsigned int irq_base,
532 unsigned int nr_irqs);
534 extern int irq_domain_disconnect_hierarchy(struct irq_domain *domain,
537 static inline bool irq_domain_is_hierarchy(struct irq_domain *domain)
539 return domain->flags & IRQ_DOMAIN_FLAG_HIERARCHY;
542 static inline bool irq_domain_is_ipi(struct irq_domain *domain)
544 return domain->flags &
545 (IRQ_DOMAIN_FLAG_IPI_PER_CPU | IRQ_DOMAIN_FLAG_IPI_SINGLE);
548 static inline bool irq_domain_is_ipi_per_cpu(struct irq_domain *domain)
550 return domain->flags & IRQ_DOMAIN_FLAG_IPI_PER_CPU;
553 static inline bool irq_domain_is_ipi_single(struct irq_domain *domain)
555 return domain->flags & IRQ_DOMAIN_FLAG_IPI_SINGLE;
558 static inline bool irq_domain_is_msi(struct irq_domain *domain)
560 return domain->flags & IRQ_DOMAIN_FLAG_MSI;
563 static inline bool irq_domain_is_msi_remap(struct irq_domain *domain)
565 return domain->flags & IRQ_DOMAIN_FLAG_MSI_REMAP;
568 extern bool irq_domain_hierarchical_is_msi_remap(struct irq_domain *domain);
570 #else /* CONFIG_IRQ_DOMAIN_HIERARCHY */
571 static inline int irq_domain_alloc_irqs(struct irq_domain *domain,
572 unsigned int nr_irqs, int node, void *arg)
577 static inline void irq_domain_free_irqs(unsigned int virq,
578 unsigned int nr_irqs) { }
580 static inline bool irq_domain_is_hierarchy(struct irq_domain *domain)
585 static inline bool irq_domain_is_ipi(struct irq_domain *domain)
590 static inline bool irq_domain_is_ipi_per_cpu(struct irq_domain *domain)
595 static inline bool irq_domain_is_ipi_single(struct irq_domain *domain)
600 static inline bool irq_domain_is_msi(struct irq_domain *domain)
605 static inline bool irq_domain_is_msi_remap(struct irq_domain *domain)
611 irq_domain_hierarchical_is_msi_remap(struct irq_domain *domain)
615 #endif /* CONFIG_IRQ_DOMAIN_HIERARCHY */
617 #else /* CONFIG_IRQ_DOMAIN */
618 static inline void irq_dispose_mapping(unsigned int virq) { }
619 static inline struct irq_domain *irq_find_matching_fwnode(
620 struct fwnode_handle *fwnode, enum irq_domain_bus_token bus_token)
624 static inline bool irq_domain_check_msi_remap(void)
628 #endif /* !CONFIG_IRQ_DOMAIN */
630 #endif /* _LINUX_IRQDOMAIN_H */