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