xref: /freebsd/sys/sys/intr.h (revision 4b01a7fa76ce5abd0ade631ac5566804ba657090)
1 /*-
2  * SPDX-License-Identifier: BSD-2-Clause
3  *
4  * Copyright (c) 2015-2016 Svatopluk Kraus
5  * Copyright (c) 2015-2016 Michal Meloun
6  * All rights reserved.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions
10  * are met:
11  * 1. Redistributions of source code must retain the above copyright
12  *    notice, this list of conditions and the following disclaimer.
13  * 2. Redistributions in binary form must reproduce the above copyright
14  *    notice, this list of conditions and the following disclaimer in the
15  *    documentation and/or other materials provided with the distribution.
16  *
17  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
18  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
21  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27  * SUCH DAMAGE.
28  */
29 
30 #ifndef _SYS_INTR_H_
31 #define _SYS_INTR_H_
32 #ifndef INTRNG
33 #error Need INTRNG for this file
34 #endif
35 
36 #include <sys/systm.h>
37 
38 #include <machine/intr.h>
39 
40 #define	INTR_IRQ_INVALID	0xFFFFFFFF
41 
42 #define INTR_ROOT_IRQ	0
43 
44 enum intr_map_data_type {
45 	INTR_MAP_DATA_ACPI = 0,
46 	INTR_MAP_DATA_FDT,
47 	INTR_MAP_DATA_GPIO,
48 	INTR_MAP_DATA_MSI,
49 
50 	/* Placeholders for platform specific types */
51 	INTR_MAP_DATA_PLAT_1 = 1000,
52 	INTR_MAP_DATA_PLAT_2,
53 	INTR_MAP_DATA_PLAT_3,
54 	INTR_MAP_DATA_PLAT_4,
55 	INTR_MAP_DATA_PLAT_5,
56 };
57 
58 struct intr_map_data {
59 	size_t			len;
60 	enum intr_map_data_type	type;
61 };
62 
63 struct intr_map_data_msi {
64 	struct intr_map_data	hdr;
65 	struct intr_irqsrc 	*isrc;
66 };
67 
68 #ifdef notyet
69 #define	INTR_SOLO	INTR_MD1
70 typedef int intr_irq_filter_t(void *arg, struct trapframe *tf);
71 #else
72 typedef int intr_irq_filter_t(void *arg);
73 #endif
74 typedef int intr_child_irq_filter_t(void *arg, uintptr_t irq);
75 
76 #define INTR_ISRC_NAMELEN	(MAXCOMLEN + 1)
77 
78 #define INTR_ISRCF_IPI		0x01	/* IPI interrupt */
79 #define INTR_ISRCF_PPI		0x02	/* PPI interrupt */
80 #define INTR_ISRCF_BOUND	0x04	/* bound to a CPU */
81 
82 struct intr_pic;
83 
84 /* Interrupt source definition. */
85 struct intr_irqsrc {
86 	device_t		isrc_dev;	/* where isrc is mapped */
87 	u_int			isrc_irq;	/* unique identificator */
88 	u_int			isrc_flags;
89 	char			isrc_name[INTR_ISRC_NAMELEN];
90 	cpuset_t		isrc_cpu;	/* on which CPUs is enabled */
91 	u_int			isrc_index;
92 	u_long *		isrc_count;
93 	u_int			isrc_handlers;
94 	struct intr_event *	isrc_event;
95 #ifdef INTR_SOLO
96 	intr_irq_filter_t *	isrc_filter;
97 	void *			isrc_arg;
98 #endif
99 	/* Used by MSI interrupts to store the iommu details */
100 	void *			isrc_iommu;
101 };
102 
103 /* Intr interface for PIC. */
104 int intr_isrc_deregister(struct intr_irqsrc *);
105 int intr_isrc_register(struct intr_irqsrc *, device_t, u_int, const char *, ...)
106     __printflike(4, 5);
107 
108 #ifdef SMP
109 bool intr_isrc_init_on_cpu(struct intr_irqsrc *isrc, u_int cpu);
110 #endif
111 
112 int intr_isrc_dispatch(struct intr_irqsrc *, struct trapframe *);
113 u_int intr_irq_next_cpu(u_int current_cpu, cpuset_t *cpumask);
114 
115 struct intr_pic *intr_pic_register(device_t, intptr_t);
116 int intr_pic_deregister(device_t, intptr_t);
117 int intr_pic_claim_root(device_t, intptr_t, intr_irq_filter_t *, void *,
118     uint32_t);
119 int intr_pic_add_handler(device_t, struct intr_pic *,
120     intr_child_irq_filter_t *, void *, uintptr_t, uintptr_t);
121 bool intr_is_per_cpu(struct resource *);
122 
123 device_t intr_irq_root_device(uint32_t);
124 
125 /* Intr interface for BUS. */
126 
127 int intr_activate_irq(device_t, struct resource *);
128 int intr_deactivate_irq(device_t, struct resource *);
129 
130 int intr_setup_irq(device_t, struct resource *, driver_filter_t, driver_intr_t,
131     void *, int, void **);
132 int intr_teardown_irq(device_t, struct resource *, void *);
133 
134 int intr_describe_irq(device_t, struct resource *, void *, const char *);
135 int intr_child_irq_handler(struct intr_pic *, uintptr_t);
136 
137 /* Intr resources  mapping. */
138 struct intr_map_data *intr_alloc_map_data(enum intr_map_data_type, size_t, int);
139 void intr_free_intr_map_data(struct intr_map_data *);
140 u_int intr_map_irq(device_t, intptr_t, struct intr_map_data *);
141 void intr_unmap_irq(u_int );
142 u_int intr_map_clone_irq(u_int );
143 
144 /* MSI/MSI-X handling */
145 int intr_msi_register(device_t, intptr_t);
146 int intr_alloc_msi(device_t, device_t, intptr_t, int, int, int *);
147 int intr_release_msi(device_t, device_t, intptr_t, int, int *);
148 int intr_map_msi(device_t, device_t, intptr_t, int, uint64_t *, uint32_t *);
149 int intr_alloc_msix(device_t, device_t, intptr_t, int *);
150 int intr_release_msix(device_t, device_t, intptr_t, int);
151 
152 #ifdef SMP
153 int intr_bind_irq(device_t, struct resource *, int);
154 
155 void intr_pic_init_secondary(void);
156 #endif
157 
158 extern u_int	intr_nirq;	/* number of IRQs on intrng platforms */
159 
160 /* Intr interface for IPIs. */
161 #ifdef SMP
162 typedef void intr_ipi_handler_t(void *);
163 
164 int intr_ipi_pic_register(device_t dev, u_int priority);
165 void intr_ipi_setup(u_int ipi, const char *name, intr_ipi_handler_t *hand,
166     void *arg);
167 void intr_ipi_send(cpuset_t cpus, u_int ipi);
168 void intr_ipi_dispatch(u_int ipi);
169 #endif
170 
171 /* Main interrupt handler called from asm on most archs except riscv. */
172 void intr_irq_handler(struct trapframe *tf, uint32_t rootnum);
173 
174 #endif	/* _SYS_INTR_H */
175