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