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