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