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