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