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