fdt_intr.c revision 1.13 1 /* $NetBSD: fdt_intr.c,v 1.13 2018/07/02 12:17:05 jmcneill Exp $ */
2
3 /*-
4 * Copyright (c) 2015-2018 Jared McNeill <jmcneill (at) invisible.ca>
5 * All rights reserved.
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 *
16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
17 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
18 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
19 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
20 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
21 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
22 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
23 * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
24 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26 * SUCH DAMAGE.
27 */
28
29 #include <sys/cdefs.h>
30 __KERNEL_RCSID(0, "$NetBSD: fdt_intr.c,v 1.13 2018/07/02 12:17:05 jmcneill Exp $");
31
32 #include <sys/param.h>
33 #include <sys/bus.h>
34 #include <sys/kmem.h>
35 #include <sys/queue.h>
36
37 #include <libfdt.h>
38 #include <dev/fdt/fdtvar.h>
39
40 struct fdtbus_interrupt_controller {
41 device_t ic_dev;
42 int ic_phandle;
43 const struct fdtbus_interrupt_controller_func *ic_funcs;
44
45 LIST_ENTRY(fdtbus_interrupt_controller) ic_next;
46 };
47
48 static LIST_HEAD(, fdtbus_interrupt_controller) fdtbus_interrupt_controllers =
49 LIST_HEAD_INITIALIZER(fdtbus_interrupt_controllers);
50
51 struct fdtbus_interrupt_cookie {
52 struct fdtbus_interrupt_controller *c_ic;
53 void *c_ih;
54 };
55
56 static u_int * get_specifier_by_index(int, int, int *);
57 static u_int * get_specifier_from_map(int, const u_int *, int *);
58
59 static int
60 fdtbus_get_interrupt_parent(int phandle)
61 {
62 u_int interrupt_parent;
63
64 while (phandle >= 0) {
65 if (of_getprop_uint32(phandle, "interrupt-parent",
66 &interrupt_parent) == 0) {
67 break;
68 }
69 if (phandle == 0) {
70 return -1;
71 }
72 phandle = OF_parent(phandle);
73 }
74 if (phandle < 0) {
75 return -1;
76 }
77
78 const void *data = fdtbus_get_data();
79 const int off = fdt_node_offset_by_phandle(data, interrupt_parent);
80 if (off < 0) {
81 return -1;
82 }
83
84 return fdtbus_offset2phandle(off);
85 }
86
87 static struct fdtbus_interrupt_controller *
88 fdtbus_get_interrupt_controller(int phandle)
89 {
90 struct fdtbus_interrupt_controller * ic;
91 LIST_FOREACH(ic, &fdtbus_interrupt_controllers, ic_next) {
92 if (ic->ic_phandle == phandle)
93 return ic;
94 }
95 return NULL;
96 }
97
98 int
99 fdtbus_register_interrupt_controller(device_t dev, int phandle,
100 const struct fdtbus_interrupt_controller_func *funcs)
101 {
102 struct fdtbus_interrupt_controller *ic;
103
104 ic = kmem_alloc(sizeof(*ic), KM_SLEEP);
105 ic->ic_dev = dev;
106 ic->ic_phandle = phandle;
107 ic->ic_funcs = funcs;
108
109 LIST_INSERT_HEAD(&fdtbus_interrupt_controllers, ic, ic_next);
110
111 return 0;
112 }
113
114 void *
115 fdtbus_intr_establish(int phandle, u_int index, int ipl, int flags,
116 int (*func)(void *), void *arg)
117 {
118 struct fdtbus_interrupt_controller *ic;
119 struct fdtbus_interrupt_cookie *c = NULL;
120 u_int *specifier;
121 int ihandle;
122 void *ih;
123
124 specifier = get_specifier_by_index(phandle, index, &ihandle);
125 if (specifier == NULL)
126 return NULL;
127
128 ic = fdtbus_get_interrupt_controller(ihandle);
129 if (ic == NULL)
130 return NULL;
131
132 ih = ic->ic_funcs->establish(ic->ic_dev, specifier,
133 ipl, flags, func, arg);
134 if (ih != NULL) {
135 c = kmem_alloc(sizeof(*c), KM_SLEEP);
136 c->c_ic = ic;
137 c->c_ih = ih;
138 }
139
140 return c;
141 }
142
143 void
144 fdtbus_intr_disestablish(int phandle, void *cookie)
145 {
146 struct fdtbus_interrupt_cookie *c = cookie;
147 struct fdtbus_interrupt_controller *ic = c->c_ic;
148 void *ih = c->c_ih;
149
150 return ic->ic_funcs->disestablish(ic->ic_dev, ih);
151 }
152
153 bool
154 fdtbus_intr_str(int phandle, u_int index, char *buf, size_t buflen)
155 {
156 struct fdtbus_interrupt_controller *ic;
157 u_int *specifier;
158 int ihandle;
159
160 specifier = get_specifier_by_index(phandle, index, &ihandle);
161
162 ic = fdtbus_get_interrupt_controller(ihandle);
163 if (ic == NULL)
164 return false;
165
166 return ic->ic_funcs->intrstr(ic->ic_dev, specifier, buf, buflen);
167 }
168
169 static int
170 find_address_cells(int phandle)
171 {
172 uint32_t cells;
173
174 if (of_getprop_uint32(phandle, "#address-cells", &cells) != 0)
175 cells = 2;
176
177 return cells;
178 }
179
180 static int
181 find_interrupt_cells(int phandle)
182 {
183 uint32_t cells;
184
185 while (phandle > 0) {
186 if (of_getprop_uint32(phandle, "#interrupt-cells", &cells) == 0)
187 return cells;
188 phandle = OF_parent(phandle);
189 }
190 return 0;
191 }
192
193 static u_int *
194 get_specifier_from_map(int phandle, const u_int *interrupt_spec, int *piphandle)
195 {
196 u_int *result = NULL;
197 int len, resid;
198
199 const u_int *data = fdtbus_get_prop(phandle, "interrupt-map", &len);
200 if (data == NULL || len <= 0)
201 return NULL;
202 resid = len;
203
204 /* child unit address: #address-cells prop of child bus node */
205 const int cua_cells = find_address_cells(phandle);
206 /* child interrupt specifier: #interrupt-cells of the nexus node */
207 const int cis_cells = find_interrupt_cells(phandle);
208
209 /* Offset (in cells) from map entry to child unit address specifier */
210 const u_int cua_off = 0;
211 /* Offset (in cells) from map entry to child interrupt specifier */
212 const u_int cis_off = cua_off + cua_cells;
213 /* Offset (in cells) from map entry to interrupt parent phandle */
214 const u_int ip_off = cis_off + cis_cells;
215 /* Offset (in cells) from map entry to parent unit specifier */
216 const u_int pus_off = ip_off + 1;
217
218 const u_int *p = (const u_int *)data;
219 while (resid > 0) {
220 /* Interrupt parent phandle */
221 const u_int iparent = fdtbus_get_phandle_from_native(be32toh(p[ip_off]));
222
223 /* parent unit specifier: #address-cells of the interrupt parent */
224 const u_int pus_cells = find_address_cells(iparent);
225 /* parent interrupt specifier: #interrupt-cells of the interrupt parent */
226 const u_int pis_cells = find_interrupt_cells(iparent);
227
228 /* Offset (in cells) from map entry to parent interrupt specifier */
229 const u_int pis_off = pus_off + pus_cells;
230
231 #ifdef FDT_INTR_DEBUG
232 printf(" intr map (len %d):", pis_off + pis_cells);
233 for (int i = 0; i < pis_off + pis_cells; i++)
234 printf(" %08x", p[i]);
235 printf("\n");
236 #endif
237
238 if (memcmp(&p[cis_off], interrupt_spec, cis_cells * 4) == 0) {
239 const int slen = pus_cells + pis_cells;
240 #ifdef FDT_INTR_DEBUG
241 printf(" intr map match iparent %08x slen %d:", iparent, slen);
242 for (int i = 0; i < slen; i++)
243 printf(" %08x", p[pus_off + i]);
244 printf("\n");
245 #endif
246 result = kmem_alloc(slen, KM_SLEEP);
247 memcpy(result, &p[pus_off], slen * 4);
248 *piphandle = iparent;
249 goto done;
250 }
251 /* Determine the length of the entry and skip that many
252 * 32 bit words
253 */
254 const u_int reclen = pis_off + pis_cells;
255 resid -= reclen * sizeof(u_int);
256 p += reclen;
257 }
258
259 done:
260 return result;
261 }
262
263 static u_int *
264 get_specifier_by_index(int phandle, int pindex, int *piphandle)
265 {
266 const u_int *node_specifier;
267 u_int *specifier;
268 int interrupt_parent, interrupt_cells, len;
269
270 interrupt_parent = fdtbus_get_interrupt_parent(phandle);
271 if (interrupt_parent <= 0)
272 return NULL;
273
274 node_specifier = fdtbus_get_prop(phandle, "interrupts", &len);
275 if (node_specifier == NULL)
276 return NULL;
277
278 interrupt_cells = find_interrupt_cells(interrupt_parent);
279 if (interrupt_cells <= 0)
280 return NULL;
281
282 const u_int spec_length = len / 4;
283 const u_int nintr = spec_length / interrupt_cells;
284 if (pindex >= nintr)
285 return NULL;
286
287 node_specifier += (interrupt_cells * pindex);
288
289 if (of_hasprop(interrupt_parent, "interrupt-map"))
290 return get_specifier_from_map(interrupt_parent, node_specifier, piphandle);
291
292 specifier = kmem_alloc(interrupt_cells * sizeof(u_int), KM_SLEEP);
293 memcpy(specifier, node_specifier, interrupt_cells * 4);
294
295 *piphandle = interrupt_parent;
296
297 return specifier;
298 }
299