fdt_intr.c revision 1.21 1 /* $NetBSD: fdt_intr.c,v 1.21 2019/02/27 17:01:57 jakllsch 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.21 2019/02/27 17:01:57 jakllsch 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 LIST_ENTRY(fdtbus_interrupt_cookie) c_next;
56 };
57
58 static LIST_HEAD(, fdtbus_interrupt_cookie) fdtbus_interrupt_cookies =
59 LIST_HEAD_INITIALIZER(fdtbus_interrupt_cookies);
60
61 static const u_int * get_specifier_by_index(int, int, int *);
62 static const u_int * get_specifier_from_map(int, const u_int *, int *);
63
64 /*
65 * Find the interrupt controller for a given node. This function will either
66 * return the phandle of the interrupt controller for this node, or the phandle
67 * of a node containing an interrupt-map table that can be used to find the
68 * real interrupt controller.
69 */
70 static int
71 fdtbus_get_interrupt_parent(int phandle)
72 {
73 int iparent = phandle;
74
75 do {
76 /*
77 * If the node is an interrupt-controller, we are done. Note that
78 * a node cannot be an interrupt-controller for itself, so we skip
79 * the leaf node here.
80 */
81 if (phandle != iparent && of_hasprop(iparent, "interrupt-controller"))
82 return iparent;
83
84 /*
85 * If the node has an explicit interrupt-parent, follow the reference.
86 */
87 if (of_hasprop(iparent, "interrupt-parent"))
88 return fdtbus_get_phandle(iparent, "interrupt-parent");
89
90 /*
91 * If the node has an interrupt-map, use it. The caller is responsible
92 * for parsing the interrupt-map and finding the real interrupt parent.
93 */
94 if (phandle != iparent && of_hasprop(iparent, "interrupt-map"))
95 return iparent;
96
97 /*
98 * Continue searching up the tree.
99 */
100 iparent = OF_parent(iparent);
101 } while (iparent > 0);
102
103 return 0;
104 }
105
106 static struct fdtbus_interrupt_controller *
107 fdtbus_get_interrupt_controller(int phandle)
108 {
109 struct fdtbus_interrupt_controller * ic;
110 LIST_FOREACH(ic, &fdtbus_interrupt_controllers, ic_next) {
111 if (ic->ic_phandle == phandle)
112 return ic;
113 }
114 return NULL;
115 }
116
117 int
118 fdtbus_register_interrupt_controller(device_t dev, int phandle,
119 const struct fdtbus_interrupt_controller_func *funcs)
120 {
121 struct fdtbus_interrupt_controller *ic;
122
123 ic = kmem_alloc(sizeof(*ic), KM_SLEEP);
124 ic->ic_dev = dev;
125 ic->ic_phandle = phandle;
126 ic->ic_funcs = funcs;
127
128 LIST_INSERT_HEAD(&fdtbus_interrupt_controllers, ic, ic_next);
129
130 return 0;
131 }
132
133 void *
134 fdtbus_intr_establish(int phandle, u_int index, int ipl, int flags,
135 int (*func)(void *), void *arg)
136 {
137 const u_int *specifier;
138 int ihandle;
139
140 specifier = get_specifier_by_index(phandle, index, &ihandle);
141 if (specifier == NULL)
142 return NULL;
143
144 return fdtbus_intr_establish_raw(ihandle, specifier, ipl,
145 flags, func, arg);
146 }
147
148 void *
149 fdtbus_intr_establish_byname(int phandle, const char *name, int ipl,
150 int flags, int (*func)(void *), void *arg)
151 {
152 u_int index;
153 int err;
154
155 err = fdtbus_get_index(phandle, "interrupt-names", name, &index);
156 if (err != 0)
157 return NULL;
158
159 return fdtbus_intr_establish(phandle, index, ipl, flags, func, arg);
160 }
161
162 void *
163 fdtbus_intr_establish_raw(int ihandle, const u_int *specifier, int ipl,
164 int flags, int (*func)(void *), void *arg)
165 {
166 struct fdtbus_interrupt_controller *ic;
167 struct fdtbus_interrupt_cookie *c;
168 void *ih;
169
170 ic = fdtbus_get_interrupt_controller(ihandle);
171 if (ic == NULL)
172 return NULL;
173
174 ih = ic->ic_funcs->establish(ic->ic_dev, __UNCONST(specifier),
175 ipl, flags, func, arg);
176 if (ih != NULL) {
177 c = kmem_alloc(sizeof(*c), KM_SLEEP);
178 c->c_ic = ic;
179 c->c_ih = ih;
180 LIST_INSERT_HEAD(&fdtbus_interrupt_cookies, c, c_next);
181 }
182
183 return ih;
184 }
185
186 void
187 fdtbus_intr_disestablish(int phandle, void *cookie)
188 {
189 struct fdtbus_interrupt_controller *ic = NULL;
190 struct fdtbus_interrupt_cookie *c;
191
192 LIST_FOREACH(c, &fdtbus_interrupt_cookies, c_next) {
193 if (c->c_ih == cookie) {
194 ic = c->c_ic;
195 LIST_REMOVE(c, c_next);
196 kmem_free(c, sizeof(*c));
197 break;
198 }
199 }
200
201 if (ic == NULL)
202 panic("%s: interrupt handle not valid", __func__);
203
204 return ic->ic_funcs->disestablish(ic->ic_dev, cookie);
205 }
206
207 bool
208 fdtbus_intr_str(int phandle, u_int index, char *buf, size_t buflen)
209 {
210 const u_int *specifier;
211 int ihandle;
212
213 specifier = get_specifier_by_index(phandle, index, &ihandle);
214 if (specifier == NULL)
215 return false;
216
217 return fdtbus_intr_str_raw(ihandle, specifier, buf, buflen);
218 }
219
220 bool
221 fdtbus_intr_str_raw(int ihandle, const u_int *specifier, char *buf, size_t buflen)
222 {
223 struct fdtbus_interrupt_controller *ic;
224
225 ic = fdtbus_get_interrupt_controller(ihandle);
226 if (ic == NULL)
227 return false;
228
229 return ic->ic_funcs->intrstr(ic->ic_dev, __UNCONST(specifier), buf, buflen);
230 }
231
232 static int
233 find_address_cells(int phandle)
234 {
235 uint32_t cells;
236
237 if (of_getprop_uint32(phandle, "#address-cells", &cells) != 0)
238 cells = 2;
239
240 return cells;
241 }
242
243 static int
244 find_interrupt_cells(int phandle)
245 {
246 uint32_t cells;
247
248 while (phandle > 0) {
249 if (of_getprop_uint32(phandle, "#interrupt-cells", &cells) == 0)
250 return cells;
251 phandle = OF_parent(phandle);
252 }
253 return 0;
254 }
255
256 static const u_int *
257 get_specifier_from_map(int phandle, const u_int *interrupt_spec, int *piphandle)
258 {
259 const u_int *result = NULL;
260 int len, resid;
261
262 const u_int *data = fdtbus_get_prop(phandle, "interrupt-map", &len);
263 if (data == NULL || len <= 0)
264 return NULL;
265 resid = len;
266
267 /* child unit address: #address-cells prop of child bus node */
268 const int cua_cells = find_address_cells(phandle);
269 /* child interrupt specifier: #interrupt-cells of the nexus node */
270 const int cis_cells = find_interrupt_cells(phandle);
271
272 /* Offset (in cells) from map entry to child unit address specifier */
273 const u_int cua_off = 0;
274 /* Offset (in cells) from map entry to child interrupt specifier */
275 const u_int cis_off = cua_off + cua_cells;
276 /* Offset (in cells) from map entry to interrupt parent phandle */
277 const u_int ip_off = cis_off + cis_cells;
278 /* Offset (in cells) from map entry to parent unit specifier */
279 const u_int pus_off = ip_off + 1;
280
281 const u_int *p = (const u_int *)data;
282 while (resid > 0) {
283 /* Interrupt parent phandle */
284 const u_int iparent = fdtbus_get_phandle_from_native(be32toh(p[ip_off]));
285
286 /* parent unit specifier: #address-cells of the interrupt parent */
287 const u_int pus_cells = find_address_cells(iparent);
288 /* parent interrupt specifier: #interrupt-cells of the interrupt parent */
289 const u_int pis_cells = find_interrupt_cells(iparent);
290
291 /* Offset (in cells) from map entry to parent interrupt specifier */
292 const u_int pis_off = pus_off + pus_cells;
293
294 #ifdef FDT_INTR_DEBUG
295 printf(" intr map (len %d):", pis_off + pis_cells);
296 for (int i = 0; i < pis_off + pis_cells; i++)
297 printf(" %08x", p[i]);
298 printf("\n");
299 #endif
300
301 if (memcmp(&p[cis_off], interrupt_spec, cis_cells * 4) == 0) {
302 #ifdef FDT_INTR_DEBUG
303 const int slen = pus_cells + pis_cells;
304 printf(" intr map match iparent %08x slen %d:", iparent, slen);
305 for (int i = 0; i < slen; i++)
306 printf(" %08x", p[pus_off + i]);
307 printf("\n");
308 #endif
309 result = &p[pus_off];
310 *piphandle = iparent;
311 goto done;
312 }
313 /* Determine the length of the entry and skip that many
314 * 32 bit words
315 */
316 const u_int reclen = pis_off + pis_cells;
317 resid -= reclen * sizeof(u_int);
318 p += reclen;
319 }
320
321 done:
322 return result;
323 }
324
325 static const u_int *
326 get_specifier_by_index(int phandle, int pindex, int *piphandle)
327 {
328 const u_int *node_specifier;
329 int interrupt_parent, interrupt_cells, len;
330
331 interrupt_parent = fdtbus_get_interrupt_parent(phandle);
332 if (interrupt_parent <= 0)
333 return NULL;
334
335 node_specifier = fdtbus_get_prop(phandle, "interrupts", &len);
336 if (node_specifier == NULL)
337 return NULL;
338
339 interrupt_cells = find_interrupt_cells(interrupt_parent);
340 if (interrupt_cells <= 0)
341 return NULL;
342
343 const u_int spec_length = len / 4;
344 const u_int nintr = spec_length / interrupt_cells;
345 if (pindex >= nintr)
346 return NULL;
347
348 node_specifier += (interrupt_cells * pindex);
349
350 if (of_hasprop(interrupt_parent, "interrupt-map"))
351 return get_specifier_from_map(interrupt_parent, node_specifier, piphandle);
352
353 *piphandle = interrupt_parent;
354
355 return node_specifier;
356 }
357