fdt_intr.c revision 1.11.4.2 1 1.11.4.2 jdolecek /* $NetBSD: fdt_intr.c,v 1.11.4.2 2017/12/03 11:37:01 jdolecek Exp $ */
2 1.11.4.2 jdolecek
3 1.11.4.2 jdolecek /*-
4 1.11.4.2 jdolecek * Copyright (c) 2015 Jared D. McNeill <jmcneill (at) invisible.ca>
5 1.11.4.2 jdolecek * All rights reserved.
6 1.11.4.2 jdolecek *
7 1.11.4.2 jdolecek * Redistribution and use in source and binary forms, with or without
8 1.11.4.2 jdolecek * modification, are permitted provided that the following conditions
9 1.11.4.2 jdolecek * are met:
10 1.11.4.2 jdolecek * 1. Redistributions of source code must retain the above copyright
11 1.11.4.2 jdolecek * notice, this list of conditions and the following disclaimer.
12 1.11.4.2 jdolecek * 2. Redistributions in binary form must reproduce the above copyright
13 1.11.4.2 jdolecek * notice, this list of conditions and the following disclaimer in the
14 1.11.4.2 jdolecek * documentation and/or other materials provided with the distribution.
15 1.11.4.2 jdolecek *
16 1.11.4.2 jdolecek * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
17 1.11.4.2 jdolecek * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
18 1.11.4.2 jdolecek * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
19 1.11.4.2 jdolecek * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
20 1.11.4.2 jdolecek * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
21 1.11.4.2 jdolecek * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
22 1.11.4.2 jdolecek * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
23 1.11.4.2 jdolecek * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
24 1.11.4.2 jdolecek * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25 1.11.4.2 jdolecek * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26 1.11.4.2 jdolecek * SUCH DAMAGE.
27 1.11.4.2 jdolecek */
28 1.11.4.2 jdolecek
29 1.11.4.2 jdolecek #include <sys/cdefs.h>
30 1.11.4.2 jdolecek __KERNEL_RCSID(0, "$NetBSD: fdt_intr.c,v 1.11.4.2 2017/12/03 11:37:01 jdolecek Exp $");
31 1.11.4.2 jdolecek
32 1.11.4.2 jdolecek #include <sys/param.h>
33 1.11.4.2 jdolecek #include <sys/bus.h>
34 1.11.4.2 jdolecek #include <sys/kmem.h>
35 1.11.4.2 jdolecek
36 1.11.4.2 jdolecek #include <libfdt.h>
37 1.11.4.2 jdolecek #include <dev/fdt/fdtvar.h>
38 1.11.4.2 jdolecek
39 1.11.4.2 jdolecek struct fdtbus_interrupt_controller {
40 1.11.4.2 jdolecek device_t ic_dev;
41 1.11.4.2 jdolecek int ic_phandle;
42 1.11.4.2 jdolecek const struct fdtbus_interrupt_controller_func *ic_funcs;
43 1.11.4.2 jdolecek
44 1.11.4.2 jdolecek struct fdtbus_interrupt_controller *ic_next;
45 1.11.4.2 jdolecek };
46 1.11.4.2 jdolecek
47 1.11.4.2 jdolecek struct fdtbus_interrupt_cookie {
48 1.11.4.2 jdolecek struct fdtbus_interrupt_controller *c_ic;
49 1.11.4.2 jdolecek void *c_ih;
50 1.11.4.2 jdolecek };
51 1.11.4.2 jdolecek
52 1.11.4.2 jdolecek static struct fdtbus_interrupt_controller *fdtbus_ic = NULL;
53 1.11.4.2 jdolecek
54 1.11.4.2 jdolecek static bool has_interrupt_map(int);
55 1.11.4.2 jdolecek static u_int * get_specifier_by_index(int, int, int *);
56 1.11.4.2 jdolecek static u_int * get_specifier_from_map(int, int, int *);
57 1.11.4.2 jdolecek
58 1.11.4.2 jdolecek static int
59 1.11.4.2 jdolecek fdtbus_get_interrupt_parent(int phandle)
60 1.11.4.2 jdolecek {
61 1.11.4.2 jdolecek u_int interrupt_parent;
62 1.11.4.2 jdolecek
63 1.11.4.2 jdolecek while (phandle >= 0) {
64 1.11.4.2 jdolecek if (of_getprop_uint32(phandle, "interrupt-parent",
65 1.11.4.2 jdolecek &interrupt_parent) == 0) {
66 1.11.4.2 jdolecek break;
67 1.11.4.2 jdolecek }
68 1.11.4.2 jdolecek if (phandle == 0) {
69 1.11.4.2 jdolecek return -1;
70 1.11.4.2 jdolecek }
71 1.11.4.2 jdolecek phandle = OF_parent(phandle);
72 1.11.4.2 jdolecek }
73 1.11.4.2 jdolecek if (phandle < 0) {
74 1.11.4.2 jdolecek return -1;
75 1.11.4.2 jdolecek }
76 1.11.4.2 jdolecek
77 1.11.4.2 jdolecek const void *data = fdtbus_get_data();
78 1.11.4.2 jdolecek const int off = fdt_node_offset_by_phandle(data, interrupt_parent);
79 1.11.4.2 jdolecek if (off < 0) {
80 1.11.4.2 jdolecek return -1;
81 1.11.4.2 jdolecek }
82 1.11.4.2 jdolecek
83 1.11.4.2 jdolecek return fdtbus_offset2phandle(off);
84 1.11.4.2 jdolecek }
85 1.11.4.2 jdolecek
86 1.11.4.2 jdolecek static struct fdtbus_interrupt_controller *
87 1.11.4.2 jdolecek fdtbus_get_interrupt_controller(int phandle)
88 1.11.4.2 jdolecek {
89 1.11.4.2 jdolecek struct fdtbus_interrupt_controller * ic;
90 1.11.4.2 jdolecek for (ic = fdtbus_ic; ic; ic = ic->ic_next) {
91 1.11.4.2 jdolecek if (ic->ic_phandle == phandle) {
92 1.11.4.2 jdolecek return ic;
93 1.11.4.2 jdolecek }
94 1.11.4.2 jdolecek }
95 1.11.4.2 jdolecek return NULL;
96 1.11.4.2 jdolecek }
97 1.11.4.2 jdolecek
98 1.11.4.2 jdolecek int
99 1.11.4.2 jdolecek fdtbus_register_interrupt_controller(device_t dev, int phandle,
100 1.11.4.2 jdolecek const struct fdtbus_interrupt_controller_func *funcs)
101 1.11.4.2 jdolecek {
102 1.11.4.2 jdolecek struct fdtbus_interrupt_controller *ic;
103 1.11.4.2 jdolecek
104 1.11.4.2 jdolecek ic = kmem_alloc(sizeof(*ic), KM_SLEEP);
105 1.11.4.2 jdolecek ic->ic_dev = dev;
106 1.11.4.2 jdolecek ic->ic_phandle = phandle;
107 1.11.4.2 jdolecek ic->ic_funcs = funcs;
108 1.11.4.2 jdolecek
109 1.11.4.2 jdolecek ic->ic_next = fdtbus_ic;
110 1.11.4.2 jdolecek fdtbus_ic = ic;
111 1.11.4.2 jdolecek
112 1.11.4.2 jdolecek return 0;
113 1.11.4.2 jdolecek }
114 1.11.4.2 jdolecek
115 1.11.4.2 jdolecek void *
116 1.11.4.2 jdolecek fdtbus_intr_establish(int phandle, u_int index, int ipl, int flags,
117 1.11.4.2 jdolecek int (*func)(void *), void *arg)
118 1.11.4.2 jdolecek {
119 1.11.4.2 jdolecek struct fdtbus_interrupt_controller *ic;
120 1.11.4.2 jdolecek struct fdtbus_interrupt_cookie *c = NULL;
121 1.11.4.2 jdolecek u_int *specifier;
122 1.11.4.2 jdolecek int ihandle;
123 1.11.4.2 jdolecek void *ih;
124 1.11.4.2 jdolecek
125 1.11.4.2 jdolecek specifier = get_specifier_by_index(phandle, index, &ihandle);
126 1.11.4.2 jdolecek if (specifier == NULL)
127 1.11.4.2 jdolecek return NULL;
128 1.11.4.2 jdolecek
129 1.11.4.2 jdolecek ic = fdtbus_get_interrupt_controller(ihandle);
130 1.11.4.2 jdolecek if (ic == NULL)
131 1.11.4.2 jdolecek return NULL;
132 1.11.4.2 jdolecek
133 1.11.4.2 jdolecek ih = ic->ic_funcs->establish(ic->ic_dev, specifier,
134 1.11.4.2 jdolecek ipl, flags, func, arg);
135 1.11.4.2 jdolecek if (ih != NULL) {
136 1.11.4.2 jdolecek c = kmem_alloc(sizeof(*c), KM_SLEEP);
137 1.11.4.2 jdolecek c->c_ic = ic;
138 1.11.4.2 jdolecek c->c_ih = ih;
139 1.11.4.2 jdolecek }
140 1.11.4.2 jdolecek
141 1.11.4.2 jdolecek return c;
142 1.11.4.2 jdolecek }
143 1.11.4.2 jdolecek
144 1.11.4.2 jdolecek void
145 1.11.4.2 jdolecek fdtbus_intr_disestablish(int phandle, void *cookie)
146 1.11.4.2 jdolecek {
147 1.11.4.2 jdolecek struct fdtbus_interrupt_cookie *c = cookie;
148 1.11.4.2 jdolecek struct fdtbus_interrupt_controller *ic = c->c_ic;
149 1.11.4.2 jdolecek void *ih = c->c_ih;
150 1.11.4.2 jdolecek
151 1.11.4.2 jdolecek return ic->ic_funcs->disestablish(ic->ic_dev, ih);
152 1.11.4.2 jdolecek }
153 1.11.4.2 jdolecek
154 1.11.4.2 jdolecek bool
155 1.11.4.2 jdolecek fdtbus_intr_str(int phandle, u_int index, char *buf, size_t buflen)
156 1.11.4.2 jdolecek {
157 1.11.4.2 jdolecek struct fdtbus_interrupt_controller *ic;
158 1.11.4.2 jdolecek u_int *specifier;
159 1.11.4.2 jdolecek int ihandle;
160 1.11.4.2 jdolecek
161 1.11.4.2 jdolecek specifier = get_specifier_by_index(phandle, index, &ihandle);
162 1.11.4.2 jdolecek
163 1.11.4.2 jdolecek ic = fdtbus_get_interrupt_controller(ihandle);
164 1.11.4.2 jdolecek if (ic == NULL)
165 1.11.4.2 jdolecek return false;
166 1.11.4.2 jdolecek
167 1.11.4.2 jdolecek return ic->ic_funcs->intrstr(ic->ic_dev, specifier, buf, buflen);
168 1.11.4.2 jdolecek }
169 1.11.4.2 jdolecek
170 1.11.4.2 jdolecek static int
171 1.11.4.2 jdolecek find_interrupt_map(int phandle)
172 1.11.4.2 jdolecek {
173 1.11.4.2 jdolecek while (phandle > 0) {
174 1.11.4.2 jdolecek if (of_hasprop(phandle, "interrupt-map"))
175 1.11.4.2 jdolecek return phandle;
176 1.11.4.2 jdolecek phandle = OF_parent(phandle);
177 1.11.4.2 jdolecek }
178 1.11.4.2 jdolecek return -1;
179 1.11.4.2 jdolecek }
180 1.11.4.2 jdolecek
181 1.11.4.2 jdolecek static int
182 1.11.4.2 jdolecek find_address_cells(int phandle)
183 1.11.4.2 jdolecek {
184 1.11.4.2 jdolecek uint32_t cells;
185 1.11.4.2 jdolecek
186 1.11.4.2 jdolecek if (of_getprop_uint32(phandle, "#address-cells", &cells) != 0)
187 1.11.4.2 jdolecek cells = 2;
188 1.11.4.2 jdolecek
189 1.11.4.2 jdolecek return cells;
190 1.11.4.2 jdolecek }
191 1.11.4.2 jdolecek
192 1.11.4.2 jdolecek static int
193 1.11.4.2 jdolecek find_interrupt_cells(int phandle)
194 1.11.4.2 jdolecek {
195 1.11.4.2 jdolecek uint32_t cells;
196 1.11.4.2 jdolecek
197 1.11.4.2 jdolecek while (phandle > 0) {
198 1.11.4.2 jdolecek if (of_getprop_uint32(phandle, "#interrupt-cells", &cells) == 0)
199 1.11.4.2 jdolecek return cells;
200 1.11.4.2 jdolecek phandle = OF_parent(phandle);
201 1.11.4.2 jdolecek }
202 1.11.4.2 jdolecek return 0;
203 1.11.4.2 jdolecek }
204 1.11.4.2 jdolecek
205 1.11.4.2 jdolecek static bool
206 1.11.4.2 jdolecek has_interrupt_map(int phandle)
207 1.11.4.2 jdolecek {
208 1.11.4.2 jdolecek return find_interrupt_map(OF_parent(phandle)) != -1;
209 1.11.4.2 jdolecek }
210 1.11.4.2 jdolecek
211 1.11.4.2 jdolecek static u_int *
212 1.11.4.2 jdolecek get_specifier_from_map(int phandle, int pindex, int *piphandle)
213 1.11.4.2 jdolecek {
214 1.11.4.2 jdolecek const u_int *node_specifier = NULL;
215 1.11.4.2 jdolecek u_int *result = NULL;
216 1.11.4.2 jdolecek int len, resid;
217 1.11.4.2 jdolecek
218 1.11.4.2 jdolecek const u_int interrupt_cells = find_interrupt_cells(phandle);
219 1.11.4.2 jdolecek if (interrupt_cells < 1)
220 1.11.4.2 jdolecek return NULL;
221 1.11.4.2 jdolecek
222 1.11.4.2 jdolecek node_specifier = fdt_getprop(fdtbus_get_data(), fdtbus_phandle2offset(phandle),
223 1.11.4.2 jdolecek "interrupts", &len);
224 1.11.4.2 jdolecek if (node_specifier == NULL)
225 1.11.4.2 jdolecek return NULL;
226 1.11.4.2 jdolecek
227 1.11.4.2 jdolecek const u_int spec_length = len / 4;
228 1.11.4.2 jdolecek const u_int nintr = spec_length / interrupt_cells;
229 1.11.4.2 jdolecek if (pindex >= nintr)
230 1.11.4.2 jdolecek return NULL;
231 1.11.4.2 jdolecek
232 1.11.4.2 jdolecek node_specifier += (interrupt_cells * pindex);
233 1.11.4.2 jdolecek
234 1.11.4.2 jdolecek const int nexus_phandle = find_interrupt_map(OF_parent(phandle));
235 1.11.4.2 jdolecek
236 1.11.4.2 jdolecek const u_int *data = fdt_getprop(fdtbus_get_data(), fdtbus_phandle2offset(nexus_phandle),
237 1.11.4.2 jdolecek "interrupt-map", &len);
238 1.11.4.2 jdolecek if (data == NULL || len <= 0) {
239 1.11.4.2 jdolecek printf("%s: can't get property interrupt-map.\n", __func__);
240 1.11.4.2 jdolecek return NULL;
241 1.11.4.2 jdolecek }
242 1.11.4.2 jdolecek resid = len;
243 1.11.4.2 jdolecek
244 1.11.4.2 jdolecek /* child unit address: #address-cells prop of child bus node */
245 1.11.4.2 jdolecek const int cua_cells = find_address_cells(nexus_phandle);
246 1.11.4.2 jdolecek /* child interrupt specifier: #interrupt-cells of the nexus node */
247 1.11.4.2 jdolecek const int cis_cells = find_interrupt_cells(nexus_phandle);
248 1.11.4.2 jdolecek
249 1.11.4.2 jdolecek /* Offset (in cells) from map entry to child unit address specifier */
250 1.11.4.2 jdolecek const u_int cua_off = 0;
251 1.11.4.2 jdolecek /* Offset (in cells) from map entry to child interrupt specifier */
252 1.11.4.2 jdolecek const u_int cis_off = cua_off + cua_cells;
253 1.11.4.2 jdolecek /* Offset (in cells) from map entry to interrupt parent phandle */
254 1.11.4.2 jdolecek const u_int ip_off = cis_off + cis_cells;
255 1.11.4.2 jdolecek /* Offset (in cells) from map entry to parent unit specifier */
256 1.11.4.2 jdolecek const u_int pus_off = ip_off + 1;
257 1.11.4.2 jdolecek
258 1.11.4.2 jdolecek #ifdef FDT_INTR_DEBUG
259 1.11.4.2 jdolecek printf("%s: phandle=%s nexus_phandle=%s\n", __func__,
260 1.11.4.2 jdolecek fdt_get_name(fdtbus_get_data(), fdtbus_phandle2offset(phandle), NULL),
261 1.11.4.2 jdolecek fdt_get_name(fdtbus_get_data(), fdtbus_phandle2offset(nexus_phandle), NULL));
262 1.11.4.2 jdolecek printf("cua_cells: %d, cis_cells: %d, ip_off = %d\n", cua_cells, cis_cells, ip_off);
263 1.11.4.2 jdolecek printf("searching for interrupt in map (data %p, len %d):", data, len);
264 1.11.4.2 jdolecek for (int i = 0; i < interrupt_cells; i++)
265 1.11.4.2 jdolecek printf(" %08x", node_specifier[i]);
266 1.11.4.2 jdolecek printf("\n");
267 1.11.4.2 jdolecek #endif
268 1.11.4.2 jdolecek
269 1.11.4.2 jdolecek const u_int *p = (const u_int *)data;
270 1.11.4.2 jdolecek while (resid > 0) {
271 1.11.4.2 jdolecek /* Interrupt parent phandle */
272 1.11.4.2 jdolecek const u_int iparent = fdtbus_get_phandle_from_native(be32toh(p[ip_off]));
273 1.11.4.2 jdolecek
274 1.11.4.2 jdolecek /* parent unit specifier: #address-cells of the interrupt parent */
275 1.11.4.2 jdolecek const u_int pus_cells = find_address_cells(iparent);
276 1.11.4.2 jdolecek /* parent interrupt specifier: #interrupt-cells of the interrupt parent */
277 1.11.4.2 jdolecek const u_int pis_cells = find_interrupt_cells(iparent);
278 1.11.4.2 jdolecek
279 1.11.4.2 jdolecek /* Offset (in cells) from map entry to parent interrupt specifier */
280 1.11.4.2 jdolecek const u_int pis_off = pus_off + pus_cells;
281 1.11.4.2 jdolecek
282 1.11.4.2 jdolecek #ifdef FDT_INTR_DEBUG
283 1.11.4.2 jdolecek printf(" intr map (len %d):", pis_off + pis_cells);
284 1.11.4.2 jdolecek for (int i = 0; i < pis_off + pis_cells; i++)
285 1.11.4.2 jdolecek printf(" %08x", p[i]);
286 1.11.4.2 jdolecek printf("\n");
287 1.11.4.2 jdolecek #endif
288 1.11.4.2 jdolecek
289 1.11.4.2 jdolecek if (cis_cells == interrupt_cells && memcmp(&p[cis_off], node_specifier, interrupt_cells * 4) == 0) {
290 1.11.4.2 jdolecek const int slen = pus_cells + pis_cells;
291 1.11.4.2 jdolecek #ifdef FDT_INTR_DEBUG
292 1.11.4.2 jdolecek printf(" intr map match iparent %08x slen %d:", iparent, slen);
293 1.11.4.2 jdolecek for (int i = 0; i < slen; i++)
294 1.11.4.2 jdolecek printf(" %08x", p[pus_off + i]);
295 1.11.4.2 jdolecek printf("\n");
296 1.11.4.2 jdolecek #endif
297 1.11.4.2 jdolecek result = kmem_alloc(slen, KM_SLEEP);
298 1.11.4.2 jdolecek memcpy(result, &p[pus_off], slen * 4);
299 1.11.4.2 jdolecek *piphandle = iparent;
300 1.11.4.2 jdolecek goto done;
301 1.11.4.2 jdolecek }
302 1.11.4.2 jdolecek /* Determine the length of the entry and skip that many
303 1.11.4.2 jdolecek * 32 bit words
304 1.11.4.2 jdolecek */
305 1.11.4.2 jdolecek const u_int reclen = pis_off + pis_cells;
306 1.11.4.2 jdolecek resid -= reclen * sizeof(u_int);
307 1.11.4.2 jdolecek p += reclen;
308 1.11.4.2 jdolecek }
309 1.11.4.2 jdolecek
310 1.11.4.2 jdolecek done:
311 1.11.4.2 jdolecek return result;
312 1.11.4.2 jdolecek }
313 1.11.4.2 jdolecek
314 1.11.4.2 jdolecek static u_int *
315 1.11.4.2 jdolecek get_specifier_by_index(int phandle, int pindex, int *piphandle)
316 1.11.4.2 jdolecek {
317 1.11.4.2 jdolecek const u_int *node_specifier;
318 1.11.4.2 jdolecek u_int *specifier;
319 1.11.4.2 jdolecek int interrupt_parent, interrupt_cells, len;
320 1.11.4.2 jdolecek
321 1.11.4.2 jdolecek if (has_interrupt_map(phandle))
322 1.11.4.2 jdolecek return get_specifier_from_map(phandle, pindex, piphandle);
323 1.11.4.2 jdolecek
324 1.11.4.2 jdolecek interrupt_parent = fdtbus_get_interrupt_parent(phandle);
325 1.11.4.2 jdolecek if (interrupt_parent <= 0)
326 1.11.4.2 jdolecek return NULL;
327 1.11.4.2 jdolecek
328 1.11.4.2 jdolecek interrupt_cells = find_interrupt_cells(interrupt_parent);
329 1.11.4.2 jdolecek if (interrupt_cells <= 0)
330 1.11.4.2 jdolecek return NULL;
331 1.11.4.2 jdolecek
332 1.11.4.2 jdolecek node_specifier = fdt_getprop(fdtbus_get_data(), fdtbus_phandle2offset(phandle),
333 1.11.4.2 jdolecek "interrupts", &len);
334 1.11.4.2 jdolecek if (node_specifier == NULL)
335 1.11.4.2 jdolecek return NULL;
336 1.11.4.2 jdolecek
337 1.11.4.2 jdolecek const u_int spec_length = len / 4;
338 1.11.4.2 jdolecek const u_int nintr = spec_length / interrupt_cells;
339 1.11.4.2 jdolecek if (pindex >= nintr)
340 1.11.4.2 jdolecek return NULL;
341 1.11.4.2 jdolecek
342 1.11.4.2 jdolecek node_specifier += (interrupt_cells * pindex);
343 1.11.4.2 jdolecek
344 1.11.4.2 jdolecek specifier = kmem_alloc(interrupt_cells * sizeof(u_int), KM_SLEEP);
345 1.11.4.2 jdolecek memcpy(specifier, node_specifier, interrupt_cells * 4);
346 1.11.4.2 jdolecek
347 1.11.4.2 jdolecek *piphandle = interrupt_parent;
348 1.11.4.2 jdolecek
349 1.11.4.2 jdolecek return specifier;
350 1.11.4.2 jdolecek }
351