1*78624466Sjmcneill /* $NetBSD: fdt_intr.c,v 1.30 2021/11/07 17:13:53 jmcneill Exp $ */
27d3ef0b6Sjmcneill
37d3ef0b6Sjmcneill /*-
41b7f74d6Sjmcneill * Copyright (c) 2015-2018 Jared McNeill <jmcneill@invisible.ca>
57d3ef0b6Sjmcneill * All rights reserved.
67d3ef0b6Sjmcneill *
77d3ef0b6Sjmcneill * Redistribution and use in source and binary forms, with or without
87d3ef0b6Sjmcneill * modification, are permitted provided that the following conditions
97d3ef0b6Sjmcneill * are met:
107d3ef0b6Sjmcneill * 1. Redistributions of source code must retain the above copyright
117d3ef0b6Sjmcneill * notice, this list of conditions and the following disclaimer.
127d3ef0b6Sjmcneill * 2. Redistributions in binary form must reproduce the above copyright
137d3ef0b6Sjmcneill * notice, this list of conditions and the following disclaimer in the
147d3ef0b6Sjmcneill * documentation and/or other materials provided with the distribution.
157d3ef0b6Sjmcneill *
167d3ef0b6Sjmcneill * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
177d3ef0b6Sjmcneill * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
187d3ef0b6Sjmcneill * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
197d3ef0b6Sjmcneill * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
207d3ef0b6Sjmcneill * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
217d3ef0b6Sjmcneill * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
227d3ef0b6Sjmcneill * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
237d3ef0b6Sjmcneill * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
247d3ef0b6Sjmcneill * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
257d3ef0b6Sjmcneill * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
267d3ef0b6Sjmcneill * SUCH DAMAGE.
277d3ef0b6Sjmcneill */
287d3ef0b6Sjmcneill
297d3ef0b6Sjmcneill #include <sys/cdefs.h>
30*78624466Sjmcneill __KERNEL_RCSID(0, "$NetBSD: fdt_intr.c,v 1.30 2021/11/07 17:13:53 jmcneill Exp $");
317d3ef0b6Sjmcneill
327d3ef0b6Sjmcneill #include <sys/param.h>
3386758bd6Sthorpej #include <sys/atomic.h>
347d3ef0b6Sjmcneill #include <sys/bus.h>
357d3ef0b6Sjmcneill #include <sys/kmem.h>
36233c5d85Sjmcneill #include <sys/queue.h>
3786758bd6Sthorpej #include <sys/mutex.h>
3886758bd6Sthorpej #include <sys/condvar.h>
397d3ef0b6Sjmcneill
407d3ef0b6Sjmcneill #include <libfdt.h>
417d3ef0b6Sjmcneill #include <dev/fdt/fdtvar.h>
4286758bd6Sthorpej #include <dev/fdt/fdt_private.h>
437d3ef0b6Sjmcneill
447d3ef0b6Sjmcneill struct fdtbus_interrupt_controller {
457d3ef0b6Sjmcneill device_t ic_dev;
467d3ef0b6Sjmcneill int ic_phandle;
477d3ef0b6Sjmcneill const struct fdtbus_interrupt_controller_func *ic_funcs;
487d3ef0b6Sjmcneill
49233c5d85Sjmcneill LIST_ENTRY(fdtbus_interrupt_controller) ic_next;
507d3ef0b6Sjmcneill };
517d3ef0b6Sjmcneill
52233c5d85Sjmcneill static LIST_HEAD(, fdtbus_interrupt_controller) fdtbus_interrupt_controllers =
53233c5d85Sjmcneill LIST_HEAD_INITIALIZER(fdtbus_interrupt_controllers);
54233c5d85Sjmcneill
55bc538cbaSjmcneill struct fdtbus_interrupt_cookie {
56bc538cbaSjmcneill struct fdtbus_interrupt_controller *c_ic;
57bc538cbaSjmcneill void *c_ih;
58ac8ac232Sjmcneill
59ac8ac232Sjmcneill LIST_ENTRY(fdtbus_interrupt_cookie) c_next;
6086758bd6Sthorpej uint32_t c_refcnt;
61bc538cbaSjmcneill };
62bc538cbaSjmcneill
63ac8ac232Sjmcneill static LIST_HEAD(, fdtbus_interrupt_cookie) fdtbus_interrupt_cookies =
64ac8ac232Sjmcneill LIST_HEAD_INITIALIZER(fdtbus_interrupt_cookies);
6586758bd6Sthorpej static kmutex_t fdtbus_interrupt_cookie_mutex;
6686758bd6Sthorpej static kcondvar_t fdtbus_interrupt_cookie_wait;
6786758bd6Sthorpej static bool fdtbus_interrupt_cookies_wanted;
68ac8ac232Sjmcneill
69e3ea5e48Sjmcneill static const u_int * get_specifier_by_index(int, int, int *);
70e3ea5e48Sjmcneill static const u_int * get_specifier_from_map(int, const u_int *, int *);
715cba6278Smarty
7286758bd6Sthorpej void
fdtbus_intr_init(void)7386758bd6Sthorpej fdtbus_intr_init(void)
7486758bd6Sthorpej {
7586758bd6Sthorpej
763e913d35Sskrll mutex_init(&fdtbus_interrupt_cookie_mutex, MUTEX_DEFAULT, IPL_HIGH);
7786758bd6Sthorpej cv_init(&fdtbus_interrupt_cookie_wait, "fdtintr");
7886758bd6Sthorpej }
7986758bd6Sthorpej
8036bbde02Sjmcneill /*
8136bbde02Sjmcneill * Find the interrupt controller for a given node. This function will either
8236bbde02Sjmcneill * return the phandle of the interrupt controller for this node, or the phandle
8336bbde02Sjmcneill * of a node containing an interrupt-map table that can be used to find the
8436bbde02Sjmcneill * real interrupt controller.
8536bbde02Sjmcneill */
867d3ef0b6Sjmcneill static int
fdtbus_get_interrupt_parent(int phandle)877d3ef0b6Sjmcneill fdtbus_get_interrupt_parent(int phandle)
887d3ef0b6Sjmcneill {
89c0539982Sjmcneill int iparent = phandle;
907d3ef0b6Sjmcneill
91c0539982Sjmcneill do {
9236bbde02Sjmcneill /*
9336bbde02Sjmcneill * If the node is an interrupt-controller, we are done. Note that
9436bbde02Sjmcneill * a node cannot be an interrupt-controller for itself, so we skip
9536bbde02Sjmcneill * the leaf node here.
9636bbde02Sjmcneill */
9736bbde02Sjmcneill if (phandle != iparent && of_hasprop(iparent, "interrupt-controller"))
9836bbde02Sjmcneill return iparent;
9936bbde02Sjmcneill
10036bbde02Sjmcneill /*
10136bbde02Sjmcneill * If the node has an explicit interrupt-parent, follow the reference.
10236bbde02Sjmcneill */
103c0539982Sjmcneill if (of_hasprop(iparent, "interrupt-parent"))
104c0539982Sjmcneill return fdtbus_get_phandle(iparent, "interrupt-parent");
10536bbde02Sjmcneill
10636bbde02Sjmcneill /*
10736bbde02Sjmcneill * If the node has an interrupt-map, use it. The caller is responsible
10836bbde02Sjmcneill * for parsing the interrupt-map and finding the real interrupt parent.
10936bbde02Sjmcneill */
1102fb1e9d4Sjakllsch if (phandle != iparent && of_hasprop(iparent, "interrupt-map"))
111c0539982Sjmcneill return iparent;
11236bbde02Sjmcneill
11336bbde02Sjmcneill /*
11436bbde02Sjmcneill * Continue searching up the tree.
11536bbde02Sjmcneill */
116c0539982Sjmcneill iparent = OF_parent(iparent);
117c0539982Sjmcneill } while (iparent > 0);
1187d3ef0b6Sjmcneill
119c0539982Sjmcneill return 0;
1207d3ef0b6Sjmcneill }
1217d3ef0b6Sjmcneill
1227d3ef0b6Sjmcneill static struct fdtbus_interrupt_controller *
fdtbus_get_interrupt_controller(int phandle)123bc538cbaSjmcneill fdtbus_get_interrupt_controller(int phandle)
1247d3ef0b6Sjmcneill {
1257d3ef0b6Sjmcneill struct fdtbus_interrupt_controller * ic;
126233c5d85Sjmcneill LIST_FOREACH(ic, &fdtbus_interrupt_controllers, ic_next) {
127233c5d85Sjmcneill if (ic->ic_phandle == phandle)
1285cba6278Smarty return ic;
1295cba6278Smarty }
1305cba6278Smarty return NULL;
1315cba6278Smarty }
1327d3ef0b6Sjmcneill
1337d3ef0b6Sjmcneill int
fdtbus_register_interrupt_controller(device_t dev,int phandle,const struct fdtbus_interrupt_controller_func * funcs)1347d3ef0b6Sjmcneill fdtbus_register_interrupt_controller(device_t dev, int phandle,
1357d3ef0b6Sjmcneill const struct fdtbus_interrupt_controller_func *funcs)
1367d3ef0b6Sjmcneill {
1377d3ef0b6Sjmcneill struct fdtbus_interrupt_controller *ic;
1387d3ef0b6Sjmcneill
1397d3ef0b6Sjmcneill ic = kmem_alloc(sizeof(*ic), KM_SLEEP);
1407d3ef0b6Sjmcneill ic->ic_dev = dev;
1417d3ef0b6Sjmcneill ic->ic_phandle = phandle;
1427d3ef0b6Sjmcneill ic->ic_funcs = funcs;
1437d3ef0b6Sjmcneill
144233c5d85Sjmcneill LIST_INSERT_HEAD(&fdtbus_interrupt_controllers, ic, ic_next);
1457d3ef0b6Sjmcneill
1467d3ef0b6Sjmcneill return 0;
1477d3ef0b6Sjmcneill }
1487d3ef0b6Sjmcneill
14986758bd6Sthorpej static struct fdtbus_interrupt_cookie *
fdtbus_get_interrupt_cookie(void * cookie)15086758bd6Sthorpej fdtbus_get_interrupt_cookie(void *cookie)
15186758bd6Sthorpej {
15286758bd6Sthorpej struct fdtbus_interrupt_cookie *c;
15386758bd6Sthorpej
15486758bd6Sthorpej mutex_enter(&fdtbus_interrupt_cookie_mutex);
15586758bd6Sthorpej LIST_FOREACH(c, &fdtbus_interrupt_cookies, c_next) {
15686758bd6Sthorpej if (c->c_ih == cookie) {
15786758bd6Sthorpej c->c_refcnt++;
15886758bd6Sthorpej KASSERT(c->c_refcnt > 0);
15986758bd6Sthorpej break;
16086758bd6Sthorpej }
16186758bd6Sthorpej }
16286758bd6Sthorpej mutex_exit(&fdtbus_interrupt_cookie_mutex);
16386758bd6Sthorpej return c;
16486758bd6Sthorpej }
16586758bd6Sthorpej
16686758bd6Sthorpej static void
fdtbus_put_interrupt_cookie(struct fdtbus_interrupt_cookie * c)16786758bd6Sthorpej fdtbus_put_interrupt_cookie(struct fdtbus_interrupt_cookie *c)
16886758bd6Sthorpej {
16986758bd6Sthorpej
17086758bd6Sthorpej mutex_enter(&fdtbus_interrupt_cookie_mutex);
17186758bd6Sthorpej KASSERT(c->c_refcnt > 0);
17286758bd6Sthorpej c->c_refcnt--;
17386758bd6Sthorpej if (fdtbus_interrupt_cookies_wanted) {
17486758bd6Sthorpej fdtbus_interrupt_cookies_wanted = false;
17586758bd6Sthorpej cv_signal(&fdtbus_interrupt_cookie_wait);
17686758bd6Sthorpej }
17786758bd6Sthorpej mutex_exit(&fdtbus_interrupt_cookie_mutex);
17886758bd6Sthorpej }
17986758bd6Sthorpej
180*78624466Sjmcneill int
fdtbus_intr_parent(int phandle)181*78624466Sjmcneill fdtbus_intr_parent(int phandle)
182*78624466Sjmcneill {
183*78624466Sjmcneill return fdtbus_get_interrupt_parent(phandle);
184*78624466Sjmcneill }
185*78624466Sjmcneill
1867d3ef0b6Sjmcneill void *
fdtbus_intr_establish(int phandle,u_int index,int ipl,int flags,int (* func)(void *),void * arg)1877d3ef0b6Sjmcneill fdtbus_intr_establish(int phandle, u_int index, int ipl, int flags,
1887d3ef0b6Sjmcneill int (*func)(void *), void *arg)
1897d3ef0b6Sjmcneill {
190d7f617c3Sryo return fdtbus_intr_establish_xname(phandle, index, ipl, flags,
191d7f617c3Sryo func, arg, NULL);
192d7f617c3Sryo }
193d7f617c3Sryo
194d7f617c3Sryo void *
fdtbus_intr_establish_xname(int phandle,u_int index,int ipl,int flags,int (* func)(void *),void * arg,const char * xname)195d7f617c3Sryo fdtbus_intr_establish_xname(int phandle, u_int index, int ipl, int flags,
196d7f617c3Sryo int (*func)(void *), void *arg, const char *xname)
197d7f617c3Sryo {
198e3ea5e48Sjmcneill const u_int *specifier;
199e9d96837Sjmcneill int ihandle;
2007d3ef0b6Sjmcneill
201e9d96837Sjmcneill specifier = get_specifier_by_index(phandle, index, &ihandle);
202dd6be231Sjmcneill if (specifier == NULL)
203e9d96837Sjmcneill return NULL;
20435bef7bdSjmcneill
205f092d0caSjmcneill return fdtbus_intr_establish_raw(ihandle, specifier, ipl,
206d7f617c3Sryo flags, func, arg, xname);
207f092d0caSjmcneill }
208f092d0caSjmcneill
209f092d0caSjmcneill void *
fdtbus_intr_establish_byname(int phandle,const char * name,int ipl,int flags,int (* func)(void *),void * arg,const char * xname)21080b4d1d9Sjakllsch fdtbus_intr_establish_byname(int phandle, const char *name, int ipl,
211c15a9cefSjmcneill int flags, int (*func)(void *), void *arg, const char *xname)
21280b4d1d9Sjakllsch {
21380b4d1d9Sjakllsch u_int index;
21480b4d1d9Sjakllsch int err;
21580b4d1d9Sjakllsch
21680b4d1d9Sjakllsch err = fdtbus_get_index(phandle, "interrupt-names", name, &index);
21780b4d1d9Sjakllsch if (err != 0)
21880b4d1d9Sjakllsch return NULL;
21980b4d1d9Sjakllsch
220c15a9cefSjmcneill return fdtbus_intr_establish_xname(phandle, index, ipl, flags, func,
221c15a9cefSjmcneill arg, xname);
22280b4d1d9Sjakllsch }
22380b4d1d9Sjakllsch
22480b4d1d9Sjakllsch void *
fdtbus_intr_establish_raw(int ihandle,const u_int * specifier,int ipl,int flags,int (* func)(void *),void * arg,const char * xname)225f092d0caSjmcneill fdtbus_intr_establish_raw(int ihandle, const u_int *specifier, int ipl,
226d7f617c3Sryo int flags, int (*func)(void *), void *arg, const char *xname)
227f092d0caSjmcneill {
228f092d0caSjmcneill struct fdtbus_interrupt_controller *ic;
229f092d0caSjmcneill struct fdtbus_interrupt_cookie *c;
230f092d0caSjmcneill void *ih;
231f092d0caSjmcneill
23235bef7bdSjmcneill ic = fdtbus_get_interrupt_controller(ihandle);
233bbba80e6Smlelstv if (ic == NULL) {
234bbba80e6Smlelstv printf("%s: ihandle %d is not a controller\n",__func__,ihandle);
23535bef7bdSjmcneill return NULL;
236bbba80e6Smlelstv }
23735bef7bdSjmcneill
23886758bd6Sthorpej c = kmem_zalloc(sizeof(*c), KM_SLEEP);
23986758bd6Sthorpej c->c_ic = ic;
24086758bd6Sthorpej mutex_enter(&fdtbus_interrupt_cookie_mutex);
24186758bd6Sthorpej LIST_INSERT_HEAD(&fdtbus_interrupt_cookies, c, c_next);
24286758bd6Sthorpej mutex_exit(&fdtbus_interrupt_cookie_mutex);
24386758bd6Sthorpej
24486758bd6Sthorpej /*
24586758bd6Sthorpej * XXX This leaves a small window where the handler is registered
24686758bd6Sthorpej * (and thus could be called) before the cookie on the list has a
24786758bd6Sthorpej * valid lookup key (and thus can be found). This will cause a
24886758bd6Sthorpej * panic in fdt_intr_mask() if that is called from the handler before
24986758bd6Sthorpej * this situation is resolved. For now we just cross our fingers
25086758bd6Sthorpej * and hope that the device won't actually interrupt until we return.
25186758bd6Sthorpej */
252e3ea5e48Sjmcneill ih = ic->ic_funcs->establish(ic->ic_dev, __UNCONST(specifier),
253d7f617c3Sryo ipl, flags, func, arg, xname);
254bc538cbaSjmcneill if (ih != NULL) {
25586758bd6Sthorpej atomic_store_release(&c->c_ih, ih);
25686758bd6Sthorpej } else {
25786758bd6Sthorpej mutex_enter(&fdtbus_interrupt_cookie_mutex);
25886758bd6Sthorpej LIST_REMOVE(c, c_next);
25986758bd6Sthorpej mutex_exit(&fdtbus_interrupt_cookie_mutex);
26086758bd6Sthorpej kmem_free(c, sizeof(*c));
261bc538cbaSjmcneill }
262bc538cbaSjmcneill
263ac8ac232Sjmcneill return ih;
2647d3ef0b6Sjmcneill }
2657d3ef0b6Sjmcneill
2667d3ef0b6Sjmcneill void
fdtbus_intr_disestablish(int phandle,void * cookie)267bc538cbaSjmcneill fdtbus_intr_disestablish(int phandle, void *cookie)
2687d3ef0b6Sjmcneill {
269ac8ac232Sjmcneill struct fdtbus_interrupt_cookie *c;
2707d3ef0b6Sjmcneill
27186758bd6Sthorpej if ((c = fdtbus_get_interrupt_cookie(cookie)) == NULL) {
272ac8ac232Sjmcneill panic("%s: interrupt handle not valid", __func__);
27386758bd6Sthorpej }
274ac8ac232Sjmcneill
27586758bd6Sthorpej const struct fdtbus_interrupt_controller *ic = c->c_ic;
27686758bd6Sthorpej ic->ic_funcs->disestablish(ic->ic_dev, cookie);
27786758bd6Sthorpej
27886758bd6Sthorpej /*
27986758bd6Sthorpej * Wait for any dangling references other than ours to
28086758bd6Sthorpej * drain away.
28186758bd6Sthorpej */
28286758bd6Sthorpej mutex_enter(&fdtbus_interrupt_cookie_mutex);
28386758bd6Sthorpej while (c->c_refcnt != 1) {
28486758bd6Sthorpej KASSERT(c->c_refcnt > 0);
28586758bd6Sthorpej fdtbus_interrupt_cookies_wanted = true;
28686758bd6Sthorpej cv_wait(&fdtbus_interrupt_cookie_wait,
28786758bd6Sthorpej &fdtbus_interrupt_cookie_mutex);
28886758bd6Sthorpej }
28986758bd6Sthorpej LIST_REMOVE(c, c_next);
29086758bd6Sthorpej mutex_exit(&fdtbus_interrupt_cookie_mutex);
29186758bd6Sthorpej
29286758bd6Sthorpej kmem_free(c, sizeof(*c));
29386758bd6Sthorpej }
29486758bd6Sthorpej
29586758bd6Sthorpej void
fdtbus_intr_mask(int phandle,void * cookie)29686758bd6Sthorpej fdtbus_intr_mask(int phandle, void *cookie)
29786758bd6Sthorpej {
29886758bd6Sthorpej struct fdtbus_interrupt_cookie *c;
29986758bd6Sthorpej
30086758bd6Sthorpej if ((c = fdtbus_get_interrupt_cookie(cookie)) == NULL) {
30186758bd6Sthorpej panic("%s: interrupt handle not valid", __func__);
30286758bd6Sthorpej }
30386758bd6Sthorpej
30486758bd6Sthorpej struct fdtbus_interrupt_controller * const ic = c->c_ic;
30586758bd6Sthorpej
30686758bd6Sthorpej if (ic->ic_funcs->mask == NULL) {
30786758bd6Sthorpej panic("%s: no 'mask' method for %s", __func__,
30886758bd6Sthorpej device_xname(ic->ic_dev));
30986758bd6Sthorpej }
31086758bd6Sthorpej
31186758bd6Sthorpej ic->ic_funcs->mask(ic->ic_dev, cookie);
31286758bd6Sthorpej fdtbus_put_interrupt_cookie(c);
31386758bd6Sthorpej }
31486758bd6Sthorpej
31586758bd6Sthorpej void
fdtbus_intr_unmask(int phandle,void * cookie)31686758bd6Sthorpej fdtbus_intr_unmask(int phandle, void *cookie)
31786758bd6Sthorpej {
31886758bd6Sthorpej struct fdtbus_interrupt_cookie *c;
31986758bd6Sthorpej
32086758bd6Sthorpej if ((c = fdtbus_get_interrupt_cookie(cookie)) == NULL) {
32186758bd6Sthorpej panic("%s: interrupt handle not valid", __func__);
32286758bd6Sthorpej }
32386758bd6Sthorpej
32486758bd6Sthorpej struct fdtbus_interrupt_controller * const ic = c->c_ic;
32586758bd6Sthorpej
32686758bd6Sthorpej if (ic->ic_funcs->unmask == NULL) {
32786758bd6Sthorpej panic("%s: no 'unmask' method for %s", __func__,
32886758bd6Sthorpej device_xname(ic->ic_dev));
32986758bd6Sthorpej }
33086758bd6Sthorpej
33186758bd6Sthorpej ic->ic_funcs->unmask(ic->ic_dev, cookie);
33286758bd6Sthorpej fdtbus_put_interrupt_cookie(c);
3337d3ef0b6Sjmcneill }
3347d3ef0b6Sjmcneill
3357d3ef0b6Sjmcneill bool
fdtbus_intr_str(int phandle,u_int index,char * buf,size_t buflen)3367d3ef0b6Sjmcneill fdtbus_intr_str(int phandle, u_int index, char *buf, size_t buflen)
3377d3ef0b6Sjmcneill {
338e3ea5e48Sjmcneill const u_int *specifier;
339e9d96837Sjmcneill int ihandle;
34035bef7bdSjmcneill
341e9d96837Sjmcneill specifier = get_specifier_by_index(phandle, index, &ihandle);
342f092d0caSjmcneill if (specifier == NULL)
343f092d0caSjmcneill return false;
344f092d0caSjmcneill
345f092d0caSjmcneill return fdtbus_intr_str_raw(ihandle, specifier, buf, buflen);
346f092d0caSjmcneill }
347f092d0caSjmcneill
348f092d0caSjmcneill bool
fdtbus_intr_str_raw(int ihandle,const u_int * specifier,char * buf,size_t buflen)349f092d0caSjmcneill fdtbus_intr_str_raw(int ihandle, const u_int *specifier, char *buf, size_t buflen)
350f092d0caSjmcneill {
351f092d0caSjmcneill struct fdtbus_interrupt_controller *ic;
35235bef7bdSjmcneill
35335bef7bdSjmcneill ic = fdtbus_get_interrupt_controller(ihandle);
35435bef7bdSjmcneill if (ic == NULL)
35535bef7bdSjmcneill return false;
35635bef7bdSjmcneill
357e3ea5e48Sjmcneill return ic->ic_funcs->intrstr(ic->ic_dev, __UNCONST(specifier), buf, buflen);
3585cba6278Smarty }
3597d3ef0b6Sjmcneill
36035bef7bdSjmcneill static int
find_address_cells(int phandle)36135bef7bdSjmcneill find_address_cells(int phandle)
36235bef7bdSjmcneill {
36335bef7bdSjmcneill uint32_t cells;
36435bef7bdSjmcneill
36535bef7bdSjmcneill if (of_getprop_uint32(phandle, "#address-cells", &cells) != 0)
36635bef7bdSjmcneill cells = 2;
36735bef7bdSjmcneill
36835bef7bdSjmcneill return cells;
36935bef7bdSjmcneill }
37035bef7bdSjmcneill
37135bef7bdSjmcneill static int
find_interrupt_cells(int phandle)37235bef7bdSjmcneill find_interrupt_cells(int phandle)
37335bef7bdSjmcneill {
37435bef7bdSjmcneill uint32_t cells;
37535bef7bdSjmcneill
37635bef7bdSjmcneill while (phandle > 0) {
37735bef7bdSjmcneill if (of_getprop_uint32(phandle, "#interrupt-cells", &cells) == 0)
37835bef7bdSjmcneill return cells;
37935bef7bdSjmcneill phandle = OF_parent(phandle);
38035bef7bdSjmcneill }
38135bef7bdSjmcneill return 0;
38235bef7bdSjmcneill }
38335bef7bdSjmcneill
384e3ea5e48Sjmcneill static const u_int *
get_specifier_from_map(int phandle,const u_int * interrupt_spec,int * piphandle)3851b7f74d6Sjmcneill get_specifier_from_map(int phandle, const u_int *interrupt_spec, int *piphandle)
3865cba6278Smarty {
387e3ea5e48Sjmcneill const u_int *result = NULL;
388e9d96837Sjmcneill int len, resid;
3895cba6278Smarty
3901b7f74d6Sjmcneill const u_int *data = fdtbus_get_prop(phandle, "interrupt-map", &len);
3911b7f74d6Sjmcneill if (data == NULL || len <= 0)
392e9d96837Sjmcneill return NULL;
393e9d96837Sjmcneill resid = len;
39435bef7bdSjmcneill
39535bef7bdSjmcneill /* child unit address: #address-cells prop of child bus node */
3961b7f74d6Sjmcneill const int cua_cells = find_address_cells(phandle);
39735bef7bdSjmcneill /* child interrupt specifier: #interrupt-cells of the nexus node */
3981b7f74d6Sjmcneill const int cis_cells = find_interrupt_cells(phandle);
39935bef7bdSjmcneill
40035bef7bdSjmcneill /* Offset (in cells) from map entry to child unit address specifier */
40135bef7bdSjmcneill const u_int cua_off = 0;
40235bef7bdSjmcneill /* Offset (in cells) from map entry to child interrupt specifier */
40335bef7bdSjmcneill const u_int cis_off = cua_off + cua_cells;
40435bef7bdSjmcneill /* Offset (in cells) from map entry to interrupt parent phandle */
40535bef7bdSjmcneill const u_int ip_off = cis_off + cis_cells;
40635bef7bdSjmcneill /* Offset (in cells) from map entry to parent unit specifier */
40735bef7bdSjmcneill const u_int pus_off = ip_off + 1;
40835bef7bdSjmcneill
409e9d96837Sjmcneill const u_int *p = (const u_int *)data;
4105cba6278Smarty while (resid > 0) {
41135bef7bdSjmcneill /* Interrupt parent phandle */
41235bef7bdSjmcneill const u_int iparent = fdtbus_get_phandle_from_native(be32toh(p[ip_off]));
4135cba6278Smarty
41435bef7bdSjmcneill /* parent unit specifier: #address-cells of the interrupt parent */
41535bef7bdSjmcneill const u_int pus_cells = find_address_cells(iparent);
41635bef7bdSjmcneill /* parent interrupt specifier: #interrupt-cells of the interrupt parent */
41735bef7bdSjmcneill const u_int pis_cells = find_interrupt_cells(iparent);
41835bef7bdSjmcneill
41935bef7bdSjmcneill /* Offset (in cells) from map entry to parent interrupt specifier */
42035bef7bdSjmcneill const u_int pis_off = pus_off + pus_cells;
42135bef7bdSjmcneill
422e9d96837Sjmcneill #ifdef FDT_INTR_DEBUG
423e9d96837Sjmcneill printf(" intr map (len %d):", pis_off + pis_cells);
424e9d96837Sjmcneill for (int i = 0; i < pis_off + pis_cells; i++)
425e9d96837Sjmcneill printf(" %08x", p[i]);
426e9d96837Sjmcneill printf("\n");
427e9d96837Sjmcneill #endif
428e9d96837Sjmcneill
4291b7f74d6Sjmcneill if (memcmp(&p[cis_off], interrupt_spec, cis_cells * 4) == 0) {
43035bef7bdSjmcneill #ifdef FDT_INTR_DEBUG
431e3ea5e48Sjmcneill const int slen = pus_cells + pis_cells;
43235bef7bdSjmcneill printf(" intr map match iparent %08x slen %d:", iparent, slen);
43335bef7bdSjmcneill for (int i = 0; i < slen; i++)
43435bef7bdSjmcneill printf(" %08x", p[pus_off + i]);
43535bef7bdSjmcneill printf("\n");
43635bef7bdSjmcneill #endif
437e3ea5e48Sjmcneill result = &p[pus_off];
43835bef7bdSjmcneill *piphandle = iparent;
43935bef7bdSjmcneill goto done;
4405cba6278Smarty }
4415cba6278Smarty /* Determine the length of the entry and skip that many
4425cba6278Smarty * 32 bit words
4435cba6278Smarty */
44435bef7bdSjmcneill const u_int reclen = pis_off + pis_cells;
4455cba6278Smarty resid -= reclen * sizeof(u_int);
4465cba6278Smarty p += reclen;
4475cba6278Smarty }
44835bef7bdSjmcneill
449788e2131Smarty done:
450788e2131Smarty return result;
4515cba6278Smarty }
4525cba6278Smarty
453c62fc3ecShkenken
454c62fc3ecShkenken static const u_int *
get_specifier_from_extended(int phandle,int pindex,int * piphandle)455c62fc3ecShkenken get_specifier_from_extended(int phandle, int pindex, int *piphandle)
456c62fc3ecShkenken {
457c62fc3ecShkenken const u_int *result = NULL;
458c62fc3ecShkenken struct fdt_phandle_data data;
459c62fc3ecShkenken
460c62fc3ecShkenken if (fdtbus_get_phandle_with_data(phandle, "interrupts-extended",
461c62fc3ecShkenken "#interrupt-cells", pindex, &data) == 0) {
462c62fc3ecShkenken *piphandle = data.phandle;
463c62fc3ecShkenken result = data.values;
464c62fc3ecShkenken }
465c62fc3ecShkenken
466c62fc3ecShkenken return result;
467c62fc3ecShkenken }
468c62fc3ecShkenken
469e3ea5e48Sjmcneill static const u_int *
get_specifier_by_index(int phandle,int pindex,int * piphandle)470e9d96837Sjmcneill get_specifier_by_index(int phandle, int pindex, int *piphandle)
4715cba6278Smarty {
472e9d96837Sjmcneill const u_int *node_specifier;
4735cba6278Smarty int interrupt_parent, interrupt_cells, len;
4745cba6278Smarty
475c62fc3ecShkenken if (of_hasprop(phandle, "interrupts-extended"))
476c62fc3ecShkenken return get_specifier_from_extended(phandle, pindex, piphandle);
477c62fc3ecShkenken
478788e2131Smarty interrupt_parent = fdtbus_get_interrupt_parent(phandle);
47935bef7bdSjmcneill if (interrupt_parent <= 0)
4805cba6278Smarty return NULL;
4815cba6278Smarty
4821b7f74d6Sjmcneill node_specifier = fdtbus_get_prop(phandle, "interrupts", &len);
4831b7f74d6Sjmcneill if (node_specifier == NULL)
4845cba6278Smarty return NULL;
4855cba6278Smarty
4861b7f74d6Sjmcneill interrupt_cells = find_interrupt_cells(interrupt_parent);
4871b7f74d6Sjmcneill if (interrupt_cells <= 0)
4885cba6278Smarty return NULL;
4895cba6278Smarty
490e9d96837Sjmcneill const u_int spec_length = len / 4;
491e9d96837Sjmcneill const u_int nintr = spec_length / interrupt_cells;
4925cba6278Smarty if (pindex >= nintr)
4935cba6278Smarty return NULL;
4945cba6278Smarty
495e9d96837Sjmcneill node_specifier += (interrupt_cells * pindex);
4965cba6278Smarty
4971b7f74d6Sjmcneill if (of_hasprop(interrupt_parent, "interrupt-map"))
4981b7f74d6Sjmcneill return get_specifier_from_map(interrupt_parent, node_specifier, piphandle);
4991b7f74d6Sjmcneill
500e9d96837Sjmcneill *piphandle = interrupt_parent;
501e9d96837Sjmcneill
502e3ea5e48Sjmcneill return node_specifier;
5037d3ef0b6Sjmcneill }
504