fdt_intr.c revision 1.3.2.4 1 1.3.2.4 skrll /* $NetBSD: fdt_intr.c,v 1.3.2.4 2016/05/29 08:44:21 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.4 skrll __KERNEL_RCSID(0, "$NetBSD: fdt_intr.c,v 1.3.2.4 2016/05/29 08:44:21 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.2 skrll static struct fdtbus_interrupt_controller *fdtbus_ic = NULL;
48 1.3.2.2 skrll
49 1.3.2.3 skrll static bool has_interrupt_map(int phandle);
50 1.3.2.3 skrll static u_int *get_entry_from_map(int phandle, int pindex, u_int *spec);
51 1.3.2.3 skrll static u_int *get_specifier_by_index(int phandle, int pindex, u_int *spec);
52 1.3.2.3 skrll
53 1.3.2.2 skrll static int
54 1.3.2.2 skrll fdtbus_get_interrupt_parent(int phandle)
55 1.3.2.2 skrll {
56 1.3.2.2 skrll u_int interrupt_parent;
57 1.3.2.2 skrll
58 1.3.2.2 skrll while (phandle >= 0) {
59 1.3.2.2 skrll if (of_getprop_uint32(phandle, "interrupt-parent",
60 1.3.2.2 skrll &interrupt_parent) == 0) {
61 1.3.2.2 skrll break;
62 1.3.2.2 skrll }
63 1.3.2.2 skrll if (phandle == 0) {
64 1.3.2.2 skrll return -1;
65 1.3.2.2 skrll }
66 1.3.2.2 skrll phandle = OF_parent(phandle);
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
72 1.3.2.2 skrll const void *data = fdtbus_get_data();
73 1.3.2.2 skrll const int off = fdt_node_offset_by_phandle(data, interrupt_parent);
74 1.3.2.2 skrll if (off < 0) {
75 1.3.2.2 skrll return -1;
76 1.3.2.2 skrll }
77 1.3.2.2 skrll
78 1.3.2.2 skrll return fdtbus_offset2phandle(off);
79 1.3.2.2 skrll }
80 1.3.2.2 skrll
81 1.3.2.2 skrll static struct fdtbus_interrupt_controller *
82 1.3.2.3 skrll fdtbus_get_interrupt_controller_ic(int phandle)
83 1.3.2.2 skrll {
84 1.3.2.3 skrll struct fdtbus_interrupt_controller * ic;
85 1.3.2.3 skrll for (ic = fdtbus_ic; ic; ic = ic->ic_next) {
86 1.3.2.3 skrll if (ic->ic_phandle == phandle) {
87 1.3.2.3 skrll return ic;
88 1.3.2.3 skrll }
89 1.3.2.3 skrll }
90 1.3.2.3 skrll return NULL;
91 1.3.2.3 skrll }
92 1.3.2.2 skrll
93 1.3.2.3 skrll static struct fdtbus_interrupt_controller *
94 1.3.2.3 skrll fdtbus_get_interrupt_controller(int phandle)
95 1.3.2.3 skrll {
96 1.3.2.2 skrll const int ic_phandle = fdtbus_get_interrupt_parent(phandle);
97 1.3.2.2 skrll if (ic_phandle < 0) {
98 1.3.2.2 skrll return NULL;
99 1.3.2.2 skrll }
100 1.3.2.2 skrll
101 1.3.2.3 skrll return fdtbus_get_interrupt_controller_ic(ic_phandle);
102 1.3.2.2 skrll }
103 1.3.2.2 skrll
104 1.3.2.2 skrll int
105 1.3.2.2 skrll fdtbus_register_interrupt_controller(device_t dev, int phandle,
106 1.3.2.2 skrll const struct fdtbus_interrupt_controller_func *funcs)
107 1.3.2.2 skrll {
108 1.3.2.2 skrll struct fdtbus_interrupt_controller *ic;
109 1.3.2.2 skrll
110 1.3.2.2 skrll ic = kmem_alloc(sizeof(*ic), KM_SLEEP);
111 1.3.2.2 skrll ic->ic_dev = dev;
112 1.3.2.2 skrll ic->ic_phandle = phandle;
113 1.3.2.2 skrll ic->ic_funcs = funcs;
114 1.3.2.2 skrll
115 1.3.2.2 skrll ic->ic_next = fdtbus_ic;
116 1.3.2.2 skrll fdtbus_ic = ic;
117 1.3.2.2 skrll
118 1.3.2.2 skrll return 0;
119 1.3.2.2 skrll }
120 1.3.2.2 skrll
121 1.3.2.2 skrll void *
122 1.3.2.2 skrll fdtbus_intr_establish(int phandle, u_int index, int ipl, int flags,
123 1.3.2.2 skrll int (*func)(void *), void *arg)
124 1.3.2.2 skrll {
125 1.3.2.3 skrll void * result = NULL;
126 1.3.2.3 skrll u_int *specifier;
127 1.3.2.3 skrll u_int spec_length;
128 1.3.2.3 skrll int ihandle;
129 1.3.2.2 skrll struct fdtbus_interrupt_controller *ic;
130 1.3.2.2 skrll
131 1.3.2.3 skrll if (has_interrupt_map(phandle)) {
132 1.3.2.3 skrll specifier = get_entry_from_map(phandle, index, &spec_length);
133 1.3.2.3 skrll ihandle = be32toh(specifier[1]);
134 1.3.2.3 skrll ihandle = fdtbus_get_phandle_from_native(ihandle);
135 1.3.2.3 skrll specifier += 2;
136 1.3.2.3 skrll } else {
137 1.3.2.3 skrll specifier = get_specifier_by_index(phandle, index,
138 1.3.2.3 skrll &spec_length);
139 1.3.2.3 skrll ihandle = phandle;
140 1.3.2.3 skrll }
141 1.3.2.3 skrll if (specifier == NULL) {
142 1.3.2.3 skrll printf("%s: Unable to get specifier %d for phandle %d\n",
143 1.3.2.3 skrll __func__, index, phandle);
144 1.3.2.3 skrll goto done;
145 1.3.2.3 skrll }
146 1.3.2.3 skrll ic = fdtbus_get_interrupt_controller(ihandle);
147 1.3.2.3 skrll if (ic == NULL) {
148 1.3.2.3 skrll printf("%s: Unable to get interrupt controller for %d\n",
149 1.3.2.3 skrll __func__, ihandle);
150 1.3.2.3 skrll goto done;
151 1.3.2.3 skrll }
152 1.3.2.3 skrll result = ic->ic_funcs->establish(ic->ic_dev, specifier,
153 1.3.2.3 skrll ipl, flags, func, arg);
154 1.3.2.3 skrll done:
155 1.3.2.3 skrll if (has_interrupt_map(phandle))
156 1.3.2.3 skrll specifier -= 2;
157 1.3.2.3 skrll if (specifier && spec_length > 0)
158 1.3.2.3 skrll kmem_free(specifier, spec_length);
159 1.3.2.3 skrll return result;
160 1.3.2.2 skrll }
161 1.3.2.2 skrll
162 1.3.2.2 skrll void
163 1.3.2.2 skrll fdtbus_intr_disestablish(int phandle, void *ih)
164 1.3.2.2 skrll {
165 1.3.2.2 skrll struct fdtbus_interrupt_controller *ic;
166 1.3.2.2 skrll
167 1.3.2.2 skrll ic = fdtbus_get_interrupt_controller(phandle);
168 1.3.2.2 skrll KASSERT(ic != NULL);
169 1.3.2.2 skrll
170 1.3.2.2 skrll return ic->ic_funcs->disestablish(ic->ic_dev, ih);
171 1.3.2.2 skrll }
172 1.3.2.2 skrll
173 1.3.2.2 skrll bool
174 1.3.2.2 skrll fdtbus_intr_str(int phandle, u_int index, char *buf, size_t buflen)
175 1.3.2.2 skrll {
176 1.3.2.3 skrll bool result = false;
177 1.3.2.2 skrll struct fdtbus_interrupt_controller *ic;
178 1.3.2.3 skrll int ihandle;
179 1.3.2.3 skrll u_int *specifier;
180 1.3.2.3 skrll u_int spec_length;
181 1.3.2.3 skrll if (has_interrupt_map(phandle)) {
182 1.3.2.3 skrll specifier = get_entry_from_map(phandle, index,
183 1.3.2.3 skrll &spec_length);
184 1.3.2.3 skrll ihandle = be32toh(specifier[1]);
185 1.3.2.3 skrll ihandle = fdtbus_get_phandle_from_native(ihandle);
186 1.3.2.3 skrll specifier += 2;
187 1.3.2.3 skrll } else {
188 1.3.2.3 skrll ihandle = phandle;
189 1.3.2.3 skrll specifier = get_specifier_by_index(phandle, index,
190 1.3.2.3 skrll &spec_length);
191 1.3.2.3 skrll }
192 1.3.2.3 skrll if (specifier == NULL) {
193 1.3.2.3 skrll printf("%s: Unable to get specifier %d for phandle %d\n",
194 1.3.2.3 skrll __func__, index, phandle);
195 1.3.2.3 skrll goto done;
196 1.3.2.3 skrll }
197 1.3.2.3 skrll ic = fdtbus_get_interrupt_controller(ihandle);
198 1.3.2.3 skrll if (ic == NULL) {
199 1.3.2.3 skrll printf("%s: Unable to get interrupt controller for %d\n",
200 1.3.2.3 skrll __func__, ihandle);
201 1.3.2.3 skrll goto done;
202 1.3.2.3 skrll }
203 1.3.2.3 skrll result = ic->ic_funcs->intrstr(ic->ic_dev, specifier, buf, buflen);
204 1.3.2.3 skrll done:
205 1.3.2.3 skrll if (has_interrupt_map(phandle))
206 1.3.2.3 skrll specifier -= 2;
207 1.3.2.3 skrll if (specifier && spec_length > 0)
208 1.3.2.3 skrll kmem_free(specifier, spec_length);
209 1.3.2.3 skrll return result;
210 1.3.2.3 skrll }
211 1.3.2.2 skrll
212 1.3.2.3 skrll /*
213 1.3.2.3 skrll * Devices that have multiple interrupts, connected to two or more
214 1.3.2.3 skrll * interrupt sources use an interrupt map rather than a simple
215 1.3.2.3 skrll * interrupt parent to indicate which interrupt controller goes with
216 1.3.2.3 skrll * which map. The interrupt map is contained in the node describing
217 1.3.2.3 skrll * the first level parent and contains one entry per interrupt:
218 1.3.2.3 skrll * index -- the index of the entry in the map
219 1.3.2.3 skrll * &parent -- pointer to the node containing the actual interrupt parent
220 1.3.2.3 skrll * for the specific interrupt
221 1.3.2.3 skrll * [specifier 0 - specifier N-1] The N (usually 2 or 3) 32 bit words
222 1.3.2.3 skrll * that make up the specifier.
223 1.3.2.3 skrll *
224 1.3.2.3 skrll * returns true if the device phandle has an interrupt-parent that
225 1.3.2.3 skrll * contains an interrupt-map.
226 1.3.2.3 skrll */
227 1.3.2.3 skrll static bool
228 1.3.2.3 skrll has_interrupt_map(int phandle)
229 1.3.2.3 skrll {
230 1.3.2.3 skrll int ic_phandle;
231 1.3.2.3 skrll of_getprop_uint32(phandle, "interrupt-parent", &ic_phandle);
232 1.3.2.3 skrll ic_phandle = fdtbus_get_phandle_from_native(ic_phandle);
233 1.3.2.3 skrll if (ic_phandle <= 0)
234 1.3.2.2 skrll return false;
235 1.3.2.3 skrll int len = OF_getproplen(ic_phandle, "interrupt-map");
236 1.3.2.3 skrll if (len > 0)
237 1.3.2.3 skrll return true;
238 1.3.2.3 skrll return false;
239 1.3.2.3 skrll }
240 1.3.2.3 skrll
241 1.3.2.3 skrll /*
242 1.3.2.3 skrll * Walk the specifier map and return a pointer to the map entry
243 1.3.2.3 skrll * associated with pindex. Return null if there is no entry.
244 1.3.2.3 skrll *
245 1.3.2.3 skrll * Because the length of the specifier depends on the interrupt
246 1.3.2.3 skrll * controller, we need to repeatedly obtain interrupt-celss for
247 1.3.2.3 skrll * the controller for the current index.
248 1.3.2.3 skrll *
249 1.3.2.3 skrll */
250 1.3.2.3 skrll static u_int *
251 1.3.2.3 skrll get_entry_from_map(int phandle, int pindex, u_int *spec_length)
252 1.3.2.3 skrll {
253 1.3.2.3 skrll int intr_cells;
254 1.3.2.3 skrll int intr_parent;
255 1.3.2.3 skrll u_int *result = NULL;
256 1.3.2.3 skrll
257 1.3.2.3 skrll of_getprop_uint32(phandle, "#interrupt-cells", &intr_cells);
258 1.3.2.3 skrll of_getprop_uint32(phandle, "interrupt-parent", &intr_parent);
259 1.3.2.3 skrll
260 1.3.2.3 skrll intr_parent = fdtbus_get_phandle_from_native(intr_parent);
261 1.3.2.3 skrll int len = OF_getproplen(intr_parent, "interrupt-map");
262 1.3.2.3 skrll if (len <= 0) {
263 1.3.2.3 skrll printf("%s: no interrupt-map.\n", __func__);
264 1.3.2.3 skrll return NULL;
265 1.3.2.3 skrll }
266 1.3.2.3 skrll uint resid = len;
267 1.3.2.3 skrll char *data = kmem_alloc(len, KM_SLEEP);
268 1.3.2.3 skrll len = OF_getprop(intr_parent, "interrupt-map", data, len);
269 1.3.2.3 skrll if (len <= 0) {
270 1.3.2.3 skrll printf("%s: can't get property interrupt-map.\n", __func__);
271 1.3.2.3 skrll goto done;
272 1.3.2.3 skrll }
273 1.3.2.3 skrll u_int *p = (u_int *)data;
274 1.3.2.3 skrll
275 1.3.2.3 skrll while (resid > 0) {
276 1.3.2.3 skrll u_int index = be32toh(p[0]);
277 1.3.2.3 skrll const u_int parent = fdtbus_get_phandle_from_native(be32toh(p[intr_cells]));
278 1.3.2.3 skrll u_int pintr_cells;
279 1.3.2.3 skrll of_getprop_uint32(parent, "#interrupt-cells", &pintr_cells);
280 1.3.2.3 skrll if (index == pindex) {
281 1.3.2.3 skrll result = kmem_alloc((pintr_cells + 2) * sizeof(u_int),
282 1.3.2.3 skrll KM_SLEEP);
283 1.3.2.3 skrll *spec_length = (pintr_cells + 2) * sizeof (u_int);
284 1.3.2.3 skrll for (int i = 0; i < pintr_cells + 2; i++)
285 1.3.2.3 skrll result[i] = p[i];
286 1.3.2.3 skrll goto done;
287 1.3.2.3 skrll
288 1.3.2.3 skrll }
289 1.3.2.3 skrll /* Determine the length of the entry and skip that many
290 1.3.2.3 skrll * 32 bit words
291 1.3.2.3 skrll */
292 1.3.2.3 skrll const u_int reclen = (intr_cells + pintr_cells + 1);
293 1.3.2.3 skrll resid -= reclen * sizeof(u_int);
294 1.3.2.3 skrll p += reclen;
295 1.3.2.3 skrll }
296 1.3.2.3 skrll done:
297 1.3.2.3 skrll kmem_free(data, len);
298 1.3.2.3 skrll return result;
299 1.3.2.3 skrll }
300 1.3.2.3 skrll
301 1.3.2.3 skrll
302 1.3.2.3 skrll /*
303 1.3.2.3 skrll * Devices that don't connect to more than one interrupt source use
304 1.3.2.3 skrll * an array of specifiers. Find the specifier that matches pindex
305 1.3.2.3 skrll * and return a pointer to it.
306 1.3.2.3 skrll *
307 1.3.2.3 skrll */
308 1.3.2.3 skrll static u_int *get_specifier_by_index(int phandle, int pindex,
309 1.3.2.3 skrll u_int *spec_length)
310 1.3.2.3 skrll {
311 1.3.2.3 skrll u_int *specifiers;
312 1.3.2.3 skrll u_int *specifier;
313 1.3.2.3 skrll int interrupt_parent, interrupt_cells, len;
314 1.3.2.3 skrll
315 1.3.2.3 skrll interrupt_parent = fdtbus_get_interrupt_parent(phandle);
316 1.3.2.3 skrll if (interrupt_parent <= 0) {
317 1.3.2.3 skrll printf("%s: interrupt_parent sanity check failed\n", __func__);
318 1.3.2.3 skrll printf("%s: interrupt_parent = %d\n", __func__,
319 1.3.2.3 skrll interrupt_parent);
320 1.3.2.3 skrll return NULL;
321 1.3.2.3 skrll }
322 1.3.2.3 skrll
323 1.3.2.3 skrll len = OF_getprop(interrupt_parent, "#interrupt-cells",
324 1.3.2.3 skrll &interrupt_cells, sizeof(interrupt_cells));
325 1.3.2.3 skrll interrupt_cells = be32toh(interrupt_cells);
326 1.3.2.3 skrll if (len != sizeof(interrupt_cells) || interrupt_cells <= 0) {
327 1.3.2.3 skrll printf("%s: interrupt_cells sanity check failed\n", __func__);
328 1.3.2.3 skrll return NULL;
329 1.3.2.3 skrll }
330 1.3.2.3 skrll
331 1.3.2.3 skrll len = OF_getproplen(phandle, "interrupts");
332 1.3.2.3 skrll if (len <= 0) {
333 1.3.2.3 skrll printf("%s: Couldn't get property interrupts\n", __func__);
334 1.3.2.3 skrll return NULL;
335 1.3.2.3 skrll }
336 1.3.2.3 skrll
337 1.3.2.3 skrll const u_int nintr = len / interrupt_cells;
338 1.3.2.3 skrll
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.3 skrll specifiers = kmem_alloc(len, KM_SLEEP);
343 1.3.2.2 skrll
344 1.3.2.3 skrll if (OF_getprop(phandle, "interrupts", specifiers, len) != len) {
345 1.3.2.3 skrll kmem_free(specifiers, len);
346 1.3.2.3 skrll return NULL;
347 1.3.2.3 skrll }
348 1.3.2.3 skrll specifier = kmem_alloc(interrupt_cells * sizeof(u_int), KM_SLEEP);
349 1.3.2.3 skrll *spec_length = interrupt_cells * sizeof(u_int);
350 1.3.2.3 skrll for (int i = 0; i < interrupt_cells; i++)
351 1.3.2.4 skrll specifier[i] = specifiers[pindex * interrupt_cells + i];
352 1.3.2.3 skrll kmem_free(specifiers, len);
353 1.3.2.3 skrll return specifier;
354 1.3.2.2 skrll }
355