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