fdt_intr.c revision 1.23 1 /* $NetBSD: fdt_intr.c,v 1.23 2019/11/16 21:53:38 mlelstv 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.23 2019/11/16 21:53:38 mlelstv 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 printf("%s: handle not found %u@%x\n",__func__,index,phandle);
143 return NULL;
144 }
145
146 return fdtbus_intr_establish_raw(ihandle, specifier, ipl,
147 flags, func, arg);
148 }
149
150 void *
151 fdtbus_intr_establish_byname(int phandle, const char *name, int ipl,
152 int flags, int (*func)(void *), void *arg)
153 {
154 u_int index;
155 int err;
156
157 err = fdtbus_get_index(phandle, "interrupt-names", name, &index);
158 if (err != 0)
159 return NULL;
160
161 return fdtbus_intr_establish(phandle, index, ipl, flags, func, arg);
162 }
163
164 void *
165 fdtbus_intr_establish_raw(int ihandle, const u_int *specifier, int ipl,
166 int flags, int (*func)(void *), void *arg)
167 {
168 struct fdtbus_interrupt_controller *ic;
169 struct fdtbus_interrupt_cookie *c;
170 void *ih;
171
172 ic = fdtbus_get_interrupt_controller(ihandle);
173 if (ic == NULL) {
174 printf("%s: ihandle %d is not a controller\n",__func__,ihandle);
175 return NULL;
176 }
177
178 ih = ic->ic_funcs->establish(ic->ic_dev, __UNCONST(specifier),
179 ipl, flags, func, arg);
180 if (ih != NULL) {
181 c = kmem_alloc(sizeof(*c), KM_SLEEP);
182 c->c_ic = ic;
183 c->c_ih = ih;
184 LIST_INSERT_HEAD(&fdtbus_interrupt_cookies, c, c_next);
185 }
186
187 return ih;
188 }
189
190 void
191 fdtbus_intr_disestablish(int phandle, void *cookie)
192 {
193 struct fdtbus_interrupt_controller *ic = NULL;
194 struct fdtbus_interrupt_cookie *c;
195
196 LIST_FOREACH(c, &fdtbus_interrupt_cookies, c_next) {
197 if (c->c_ih == cookie) {
198 ic = c->c_ic;
199 LIST_REMOVE(c, c_next);
200 kmem_free(c, sizeof(*c));
201 break;
202 }
203 }
204
205 if (ic == NULL)
206 panic("%s: interrupt handle not valid", __func__);
207
208 return ic->ic_funcs->disestablish(ic->ic_dev, cookie);
209 }
210
211 bool
212 fdtbus_intr_str(int phandle, u_int index, char *buf, size_t buflen)
213 {
214 const u_int *specifier;
215 int ihandle;
216
217 specifier = get_specifier_by_index(phandle, index, &ihandle);
218 if (specifier == NULL)
219 return false;
220
221 return fdtbus_intr_str_raw(ihandle, specifier, buf, buflen);
222 }
223
224 bool
225 fdtbus_intr_str_raw(int ihandle, const u_int *specifier, char *buf, size_t buflen)
226 {
227 struct fdtbus_interrupt_controller *ic;
228
229 ic = fdtbus_get_interrupt_controller(ihandle);
230 if (ic == NULL)
231 return false;
232
233 return ic->ic_funcs->intrstr(ic->ic_dev, __UNCONST(specifier), buf, buflen);
234 }
235
236 static int
237 find_address_cells(int phandle)
238 {
239 uint32_t cells;
240
241 if (of_getprop_uint32(phandle, "#address-cells", &cells) != 0)
242 cells = 2;
243
244 return cells;
245 }
246
247 static int
248 find_interrupt_cells(int phandle)
249 {
250 uint32_t cells;
251
252 while (phandle > 0) {
253 if (of_getprop_uint32(phandle, "#interrupt-cells", &cells) == 0)
254 return cells;
255 phandle = OF_parent(phandle);
256 }
257 return 0;
258 }
259
260 static const u_int *
261 get_specifier_from_map(int phandle, const u_int *interrupt_spec, int *piphandle)
262 {
263 const u_int *result = NULL;
264 int len, resid;
265
266 const u_int *data = fdtbus_get_prop(phandle, "interrupt-map", &len);
267 if (data == NULL || len <= 0)
268 return NULL;
269 resid = len;
270
271 /* child unit address: #address-cells prop of child bus node */
272 const int cua_cells = find_address_cells(phandle);
273 /* child interrupt specifier: #interrupt-cells of the nexus node */
274 const int cis_cells = find_interrupt_cells(phandle);
275
276 /* Offset (in cells) from map entry to child unit address specifier */
277 const u_int cua_off = 0;
278 /* Offset (in cells) from map entry to child interrupt specifier */
279 const u_int cis_off = cua_off + cua_cells;
280 /* Offset (in cells) from map entry to interrupt parent phandle */
281 const u_int ip_off = cis_off + cis_cells;
282 /* Offset (in cells) from map entry to parent unit specifier */
283 const u_int pus_off = ip_off + 1;
284
285 const u_int *p = (const u_int *)data;
286 while (resid > 0) {
287 /* Interrupt parent phandle */
288 const u_int iparent = fdtbus_get_phandle_from_native(be32toh(p[ip_off]));
289
290 /* parent unit specifier: #address-cells of the interrupt parent */
291 const u_int pus_cells = find_address_cells(iparent);
292 /* parent interrupt specifier: #interrupt-cells of the interrupt parent */
293 const u_int pis_cells = find_interrupt_cells(iparent);
294
295 /* Offset (in cells) from map entry to parent interrupt specifier */
296 const u_int pis_off = pus_off + pus_cells;
297
298 #ifdef FDT_INTR_DEBUG
299 printf(" intr map (len %d):", pis_off + pis_cells);
300 for (int i = 0; i < pis_off + pis_cells; i++)
301 printf(" %08x", p[i]);
302 printf("\n");
303 #endif
304
305 if (memcmp(&p[cis_off], interrupt_spec, cis_cells * 4) == 0) {
306 #ifdef FDT_INTR_DEBUG
307 const int slen = pus_cells + pis_cells;
308 printf(" intr map match iparent %08x slen %d:", iparent, slen);
309 for (int i = 0; i < slen; i++)
310 printf(" %08x", p[pus_off + i]);
311 printf("\n");
312 #endif
313 result = &p[pus_off];
314 *piphandle = iparent;
315 goto done;
316 }
317 /* Determine the length of the entry and skip that many
318 * 32 bit words
319 */
320 const u_int reclen = pis_off + pis_cells;
321 resid -= reclen * sizeof(u_int);
322 p += reclen;
323 }
324
325 done:
326 return result;
327 }
328
329
330 static const u_int *
331 get_specifier_from_extended(int phandle, int pindex, int *piphandle)
332 {
333 const u_int *result = NULL;
334 struct fdt_phandle_data data;
335
336 if (fdtbus_get_phandle_with_data(phandle, "interrupts-extended",
337 "#interrupt-cells", pindex, &data) == 0) {
338 *piphandle = data.phandle;
339 result = data.values;
340 }
341
342 return result;
343 }
344
345 static const u_int *
346 get_specifier_by_index(int phandle, int pindex, int *piphandle)
347 {
348 const u_int *node_specifier;
349 int interrupt_parent, interrupt_cells, len;
350
351 if (of_hasprop(phandle, "interrupts-extended"))
352 return get_specifier_from_extended(phandle, pindex, piphandle);
353
354 interrupt_parent = fdtbus_get_interrupt_parent(phandle);
355 if (interrupt_parent <= 0)
356 return NULL;
357
358 node_specifier = fdtbus_get_prop(phandle, "interrupts", &len);
359 if (node_specifier == NULL)
360 return NULL;
361
362 interrupt_cells = find_interrupt_cells(interrupt_parent);
363 if (interrupt_cells <= 0)
364 return NULL;
365
366 const u_int spec_length = len / 4;
367 const u_int nintr = spec_length / interrupt_cells;
368 if (pindex >= nintr)
369 return NULL;
370
371 node_specifier += (interrupt_cells * pindex);
372
373 if (of_hasprop(interrupt_parent, "interrupt-map"))
374 return get_specifier_from_map(interrupt_parent, node_specifier, piphandle);
375
376 *piphandle = interrupt_parent;
377
378 return node_specifier;
379 }
380