Home | History | Annotate | Line # | Download | only in fdt
fdt_intr.c revision 1.11.8.2
      1  1.11.8.2    martin /* $NetBSD: fdt_intr.c,v 1.11.8.2 2020/04/08 14:08:04 martin Exp $ */
      2       1.1  jmcneill 
      3       1.1  jmcneill /*-
      4  1.11.8.1  christos  * Copyright (c) 2015-2018 Jared 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.11.8.2    martin __KERNEL_RCSID(0, "$NetBSD: fdt_intr.c,v 1.11.8.2 2020/04/08 14:08:04 martin Exp $");
     31       1.1  jmcneill 
     32       1.1  jmcneill #include <sys/param.h>
     33  1.11.8.2    martin #include <sys/atomic.h>
     34       1.1  jmcneill #include <sys/bus.h>
     35       1.1  jmcneill #include <sys/kmem.h>
     36  1.11.8.1  christos #include <sys/queue.h>
     37  1.11.8.2    martin #include <sys/mutex.h>
     38  1.11.8.2    martin #include <sys/condvar.h>
     39       1.1  jmcneill 
     40       1.1  jmcneill #include <libfdt.h>
     41       1.1  jmcneill #include <dev/fdt/fdtvar.h>
     42  1.11.8.2    martin #include <dev/fdt/fdt_private.h>
     43       1.1  jmcneill 
     44       1.1  jmcneill struct fdtbus_interrupt_controller {
     45       1.1  jmcneill 	device_t ic_dev;
     46       1.1  jmcneill 	int ic_phandle;
     47       1.1  jmcneill 	const struct fdtbus_interrupt_controller_func *ic_funcs;
     48       1.1  jmcneill 
     49  1.11.8.1  christos 	LIST_ENTRY(fdtbus_interrupt_controller) ic_next;
     50       1.1  jmcneill };
     51       1.1  jmcneill 
     52  1.11.8.1  christos static LIST_HEAD(, fdtbus_interrupt_controller) fdtbus_interrupt_controllers =
     53  1.11.8.1  christos     LIST_HEAD_INITIALIZER(fdtbus_interrupt_controllers);
     54  1.11.8.1  christos 
     55      1.11  jmcneill struct fdtbus_interrupt_cookie {
     56      1.11  jmcneill 	struct fdtbus_interrupt_controller *c_ic;
     57      1.11  jmcneill 	void *c_ih;
     58  1.11.8.1  christos 
     59  1.11.8.1  christos 	LIST_ENTRY(fdtbus_interrupt_cookie) c_next;
     60  1.11.8.2    martin 	uint32_t c_refcnt;
     61      1.11  jmcneill };
     62      1.11  jmcneill 
     63  1.11.8.1  christos static LIST_HEAD(, fdtbus_interrupt_cookie) fdtbus_interrupt_cookies =
     64  1.11.8.1  christos     LIST_HEAD_INITIALIZER(fdtbus_interrupt_cookies);
     65  1.11.8.2    martin static kmutex_t fdtbus_interrupt_cookie_mutex;
     66  1.11.8.2    martin static kcondvar_t fdtbus_interrupt_cookie_wait;
     67  1.11.8.2    martin static bool fdtbus_interrupt_cookies_wanted;
     68       1.1  jmcneill 
     69  1.11.8.1  christos static const u_int *	get_specifier_by_index(int, int, int *);
     70  1.11.8.1  christos static const u_int *	get_specifier_from_map(int, const u_int *, int *);
     71       1.4     marty 
     72  1.11.8.2    martin void
     73  1.11.8.2    martin fdtbus_intr_init(void)
     74  1.11.8.2    martin {
     75  1.11.8.2    martin 
     76  1.11.8.2    martin 	mutex_init(&fdtbus_interrupt_cookie_mutex, MUTEX_DEFAULT, IPL_HIGH);
     77  1.11.8.2    martin 	cv_init(&fdtbus_interrupt_cookie_wait, "fdtintr");
     78  1.11.8.2    martin }
     79  1.11.8.2    martin 
     80  1.11.8.1  christos /*
     81  1.11.8.1  christos  * Find the interrupt controller for a given node. This function will either
     82  1.11.8.1  christos  * return the phandle of the interrupt controller for this node, or the phandle
     83  1.11.8.1  christos  * of a node containing an interrupt-map table that can be used to find the
     84  1.11.8.1  christos  * real interrupt controller.
     85  1.11.8.1  christos  */
     86       1.1  jmcneill static int
     87       1.1  jmcneill fdtbus_get_interrupt_parent(int phandle)
     88       1.1  jmcneill {
     89  1.11.8.1  christos 	int iparent = phandle;
     90       1.1  jmcneill 
     91  1.11.8.1  christos 	do {
     92  1.11.8.1  christos 		/*
     93  1.11.8.1  christos 		 * If the node is an interrupt-controller, we are done. Note that
     94  1.11.8.1  christos 		 * a node cannot be an interrupt-controller for itself, so we skip
     95  1.11.8.1  christos 		 * the leaf node here.
     96  1.11.8.1  christos 		 */
     97  1.11.8.1  christos 		if (phandle != iparent && of_hasprop(iparent, "interrupt-controller"))
     98  1.11.8.1  christos 			return iparent;
     99       1.1  jmcneill 
    100  1.11.8.1  christos 		/*
    101  1.11.8.1  christos 		 * If the node has an explicit interrupt-parent, follow the reference.
    102  1.11.8.1  christos 		 */
    103  1.11.8.1  christos 		if (of_hasprop(iparent, "interrupt-parent"))
    104  1.11.8.1  christos 			return fdtbus_get_phandle(iparent, "interrupt-parent");
    105  1.11.8.1  christos 
    106  1.11.8.1  christos 		/*
    107  1.11.8.1  christos 		 * If the node has an interrupt-map, use it. The caller is responsible
    108  1.11.8.1  christos 		 * for parsing the interrupt-map and finding the real interrupt parent.
    109  1.11.8.1  christos 		 */
    110  1.11.8.1  christos 		if (phandle != iparent && of_hasprop(iparent, "interrupt-map"))
    111  1.11.8.1  christos 			return iparent;
    112       1.1  jmcneill 
    113  1.11.8.1  christos 		/*
    114  1.11.8.1  christos 		 * Continue searching up the tree.
    115  1.11.8.1  christos 		 */
    116  1.11.8.1  christos 		iparent = OF_parent(iparent);
    117  1.11.8.1  christos 	} while (iparent > 0);
    118  1.11.8.1  christos 
    119  1.11.8.1  christos 	return 0;
    120       1.1  jmcneill }
    121       1.1  jmcneill 
    122       1.1  jmcneill static struct fdtbus_interrupt_controller *
    123      1.11  jmcneill fdtbus_get_interrupt_controller(int phandle)
    124       1.4     marty {
    125       1.4     marty 	struct fdtbus_interrupt_controller * ic;
    126  1.11.8.1  christos 	LIST_FOREACH(ic, &fdtbus_interrupt_controllers, ic_next) {
    127  1.11.8.1  christos 		if (ic->ic_phandle == phandle)
    128       1.4     marty 			return ic;
    129       1.4     marty 	}
    130       1.4     marty 	return NULL;
    131       1.4     marty }
    132       1.4     marty 
    133       1.1  jmcneill int
    134       1.1  jmcneill fdtbus_register_interrupt_controller(device_t dev, int phandle,
    135       1.1  jmcneill     const struct fdtbus_interrupt_controller_func *funcs)
    136       1.1  jmcneill {
    137       1.1  jmcneill 	struct fdtbus_interrupt_controller *ic;
    138       1.1  jmcneill 
    139       1.1  jmcneill 	ic = kmem_alloc(sizeof(*ic), KM_SLEEP);
    140       1.1  jmcneill 	ic->ic_dev = dev;
    141       1.1  jmcneill 	ic->ic_phandle = phandle;
    142       1.1  jmcneill 	ic->ic_funcs = funcs;
    143       1.1  jmcneill 
    144  1.11.8.1  christos 	LIST_INSERT_HEAD(&fdtbus_interrupt_controllers, ic, ic_next);
    145       1.1  jmcneill 
    146       1.1  jmcneill 	return 0;
    147       1.1  jmcneill }
    148       1.1  jmcneill 
    149  1.11.8.2    martin static struct fdtbus_interrupt_cookie *
    150  1.11.8.2    martin fdtbus_get_interrupt_cookie(void *cookie)
    151  1.11.8.2    martin {
    152  1.11.8.2    martin 	struct fdtbus_interrupt_cookie *c;
    153  1.11.8.2    martin 
    154  1.11.8.2    martin 	mutex_enter(&fdtbus_interrupt_cookie_mutex);
    155  1.11.8.2    martin 	LIST_FOREACH(c, &fdtbus_interrupt_cookies, c_next) {
    156  1.11.8.2    martin 		if (c->c_ih == cookie) {
    157  1.11.8.2    martin 			c->c_refcnt++;
    158  1.11.8.2    martin 			KASSERT(c->c_refcnt > 0);
    159  1.11.8.2    martin 			break;
    160  1.11.8.2    martin 		}
    161  1.11.8.2    martin 	}
    162  1.11.8.2    martin 	mutex_exit(&fdtbus_interrupt_cookie_mutex);
    163  1.11.8.2    martin 	return c;
    164  1.11.8.2    martin }
    165  1.11.8.2    martin 
    166  1.11.8.2    martin static void
    167  1.11.8.2    martin fdtbus_put_interrupt_cookie(struct fdtbus_interrupt_cookie *c)
    168  1.11.8.2    martin {
    169  1.11.8.2    martin 
    170  1.11.8.2    martin 	mutex_enter(&fdtbus_interrupt_cookie_mutex);
    171  1.11.8.2    martin 	KASSERT(c->c_refcnt > 0);
    172  1.11.8.2    martin 	c->c_refcnt--;
    173  1.11.8.2    martin 	if (fdtbus_interrupt_cookies_wanted) {
    174  1.11.8.2    martin 		fdtbus_interrupt_cookies_wanted = false;
    175  1.11.8.2    martin 		cv_signal(&fdtbus_interrupt_cookie_wait);
    176  1.11.8.2    martin 	}
    177  1.11.8.2    martin 	mutex_exit(&fdtbus_interrupt_cookie_mutex);
    178  1.11.8.2    martin }
    179  1.11.8.2    martin 
    180       1.1  jmcneill void *
    181       1.1  jmcneill fdtbus_intr_establish(int phandle, u_int index, int ipl, int flags,
    182       1.1  jmcneill     int (*func)(void *), void *arg)
    183       1.1  jmcneill {
    184  1.11.8.1  christos 	const u_int *specifier;
    185      1.10  jmcneill 	int ihandle;
    186       1.1  jmcneill 
    187      1.10  jmcneill 	specifier = get_specifier_by_index(phandle, index, &ihandle);
    188      1.10  jmcneill 	if (specifier == NULL)
    189      1.10  jmcneill 		return NULL;
    190       1.9  jmcneill 
    191  1.11.8.1  christos 	return fdtbus_intr_establish_raw(ihandle, specifier, ipl,
    192  1.11.8.1  christos 	    flags, func, arg);
    193  1.11.8.1  christos }
    194  1.11.8.1  christos 
    195  1.11.8.1  christos void *
    196  1.11.8.1  christos fdtbus_intr_establish_byname(int phandle, const char *name, int ipl,
    197  1.11.8.1  christos     int flags, int (*func)(void *), void *arg)
    198  1.11.8.1  christos {
    199  1.11.8.1  christos 	u_int index;
    200  1.11.8.1  christos 	int err;
    201  1.11.8.1  christos 
    202  1.11.8.1  christos 	err = fdtbus_get_index(phandle, "interrupt-names", name, &index);
    203  1.11.8.1  christos 	if (err != 0)
    204  1.11.8.1  christos 		return NULL;
    205  1.11.8.1  christos 
    206  1.11.8.1  christos 	return fdtbus_intr_establish(phandle, index, ipl, flags, func, arg);
    207  1.11.8.1  christos }
    208  1.11.8.1  christos 
    209  1.11.8.1  christos void *
    210  1.11.8.1  christos fdtbus_intr_establish_raw(int ihandle, const u_int *specifier, int ipl,
    211  1.11.8.1  christos     int flags, int (*func)(void *), void *arg)
    212  1.11.8.1  christos {
    213  1.11.8.1  christos 	struct fdtbus_interrupt_controller *ic;
    214  1.11.8.1  christos 	struct fdtbus_interrupt_cookie *c;
    215  1.11.8.1  christos 	void *ih;
    216  1.11.8.1  christos 
    217       1.4     marty 	ic = fdtbus_get_interrupt_controller(ihandle);
    218       1.9  jmcneill 	if (ic == NULL)
    219       1.9  jmcneill 		return NULL;
    220       1.9  jmcneill 
    221  1.11.8.2    martin 	c = kmem_zalloc(sizeof(*c), KM_SLEEP);
    222  1.11.8.2    martin 	c->c_ic = ic;
    223  1.11.8.2    martin 	mutex_enter(&fdtbus_interrupt_cookie_mutex);
    224  1.11.8.2    martin 	LIST_INSERT_HEAD(&fdtbus_interrupt_cookies, c, c_next);
    225  1.11.8.2    martin 	mutex_exit(&fdtbus_interrupt_cookie_mutex);
    226  1.11.8.2    martin 
    227  1.11.8.2    martin 	/*
    228  1.11.8.2    martin 	 * XXX This leaves a small window where the handler is registered
    229  1.11.8.2    martin 	 * (and thus could be called) before the cookie on the list has a
    230  1.11.8.2    martin 	 * valid lookup key (and thus can be found).  This will cause a
    231  1.11.8.2    martin 	 * panic in fdt_intr_mask() if that is called from the handler before
    232  1.11.8.2    martin 	 * this situation is resolved.  For now we just cross our fingers
    233  1.11.8.2    martin 	 * and hope that the device won't actually interrupt until we return.
    234  1.11.8.2    martin 	 */
    235  1.11.8.1  christos 	ih = ic->ic_funcs->establish(ic->ic_dev, __UNCONST(specifier),
    236       1.9  jmcneill 	    ipl, flags, func, arg);
    237      1.11  jmcneill 	if (ih != NULL) {
    238  1.11.8.2    martin 		atomic_store_release(&c->c_ih, ih);
    239  1.11.8.2    martin 	} else {
    240  1.11.8.2    martin 		mutex_enter(&fdtbus_interrupt_cookie_mutex);
    241  1.11.8.2    martin 		LIST_REMOVE(c, c_next);
    242  1.11.8.2    martin 		mutex_exit(&fdtbus_interrupt_cookie_mutex);
    243  1.11.8.2    martin 		kmem_free(c, sizeof(*c));
    244      1.11  jmcneill 	}
    245      1.11  jmcneill 
    246  1.11.8.1  christos 	return ih;
    247       1.1  jmcneill }
    248       1.1  jmcneill 
    249       1.1  jmcneill void
    250      1.11  jmcneill fdtbus_intr_disestablish(int phandle, void *cookie)
    251       1.1  jmcneill {
    252  1.11.8.1  christos 	struct fdtbus_interrupt_cookie *c;
    253  1.11.8.1  christos 
    254  1.11.8.2    martin 	if ((c = fdtbus_get_interrupt_cookie(cookie)) == NULL) {
    255  1.11.8.2    martin 		panic("%s: interrupt handle not valid", __func__);
    256  1.11.8.1  christos 	}
    257       1.1  jmcneill 
    258  1.11.8.2    martin 	const struct fdtbus_interrupt_controller *ic = c->c_ic;
    259  1.11.8.2    martin 	ic->ic_funcs->disestablish(ic->ic_dev, cookie);
    260  1.11.8.2    martin 
    261  1.11.8.2    martin 	/*
    262  1.11.8.2    martin 	 * Wait for any dangling references other than ours to
    263  1.11.8.2    martin 	 * drain away.
    264  1.11.8.2    martin 	 */
    265  1.11.8.2    martin 	mutex_enter(&fdtbus_interrupt_cookie_mutex);
    266  1.11.8.2    martin 	while (c->c_refcnt != 1) {
    267  1.11.8.2    martin 		KASSERT(c->c_refcnt > 0);
    268  1.11.8.2    martin 		fdtbus_interrupt_cookies_wanted = true;
    269  1.11.8.2    martin 		cv_wait(&fdtbus_interrupt_cookie_wait,
    270  1.11.8.2    martin 			&fdtbus_interrupt_cookie_mutex);
    271  1.11.8.2    martin 	}
    272  1.11.8.2    martin 	LIST_REMOVE(c, c_next);
    273  1.11.8.2    martin 	mutex_exit(&fdtbus_interrupt_cookie_mutex);
    274  1.11.8.2    martin 
    275  1.11.8.2    martin 	kmem_free(c, sizeof(*c));
    276  1.11.8.2    martin }
    277  1.11.8.2    martin 
    278  1.11.8.2    martin void
    279  1.11.8.2    martin fdtbus_intr_mask(int phandle, void *cookie)
    280  1.11.8.2    martin {
    281  1.11.8.2    martin 	struct fdtbus_interrupt_cookie *c;
    282  1.11.8.2    martin 
    283  1.11.8.2    martin 	if ((c = fdtbus_get_interrupt_cookie(cookie)) == NULL) {
    284  1.11.8.1  christos 		panic("%s: interrupt handle not valid", __func__);
    285  1.11.8.2    martin 	}
    286  1.11.8.2    martin 
    287  1.11.8.2    martin 	struct fdtbus_interrupt_controller * const ic = c->c_ic;
    288  1.11.8.2    martin 
    289  1.11.8.2    martin 	if (ic->ic_funcs->mask == NULL) {
    290  1.11.8.2    martin 		panic("%s: no 'mask' method for %s", __func__,
    291  1.11.8.2    martin 		    device_xname(ic->ic_dev));
    292  1.11.8.2    martin 	}
    293  1.11.8.2    martin 
    294  1.11.8.2    martin 	ic->ic_funcs->mask(ic->ic_dev, cookie);
    295  1.11.8.2    martin 	fdtbus_put_interrupt_cookie(c);
    296  1.11.8.2    martin }
    297  1.11.8.2    martin 
    298  1.11.8.2    martin void
    299  1.11.8.2    martin fdtbus_intr_unmask(int phandle, void *cookie)
    300  1.11.8.2    martin {
    301  1.11.8.2    martin 	struct fdtbus_interrupt_cookie *c;
    302  1.11.8.2    martin 
    303  1.11.8.2    martin 	if ((c = fdtbus_get_interrupt_cookie(cookie)) == NULL) {
    304  1.11.8.2    martin 		panic("%s: interrupt handle not valid", __func__);
    305  1.11.8.2    martin 	}
    306  1.11.8.2    martin 
    307  1.11.8.2    martin 	struct fdtbus_interrupt_controller * const ic = c->c_ic;
    308  1.11.8.2    martin 
    309  1.11.8.2    martin 	if (ic->ic_funcs->unmask == NULL) {
    310  1.11.8.2    martin 		panic("%s: no 'unmask' method for %s", __func__,
    311  1.11.8.2    martin 		    device_xname(ic->ic_dev));
    312  1.11.8.2    martin 	}
    313  1.11.8.1  christos 
    314  1.11.8.2    martin 	ic->ic_funcs->unmask(ic->ic_dev, cookie);
    315  1.11.8.2    martin 	fdtbus_put_interrupt_cookie(c);
    316       1.1  jmcneill }
    317       1.1  jmcneill 
    318       1.1  jmcneill bool
    319       1.1  jmcneill fdtbus_intr_str(int phandle, u_int index, char *buf, size_t buflen)
    320       1.1  jmcneill {
    321  1.11.8.1  christos 	const u_int *specifier;
    322      1.10  jmcneill 	int ihandle;
    323       1.9  jmcneill 
    324      1.10  jmcneill 	specifier = get_specifier_by_index(phandle, index, &ihandle);
    325  1.11.8.1  christos 	if (specifier == NULL)
    326       1.9  jmcneill 		return false;
    327       1.9  jmcneill 
    328  1.11.8.1  christos 	return fdtbus_intr_str_raw(ihandle, specifier, buf, buflen);
    329       1.9  jmcneill }
    330       1.9  jmcneill 
    331  1.11.8.1  christos bool
    332  1.11.8.1  christos fdtbus_intr_str_raw(int ihandle, const u_int *specifier, char *buf, size_t buflen)
    333       1.9  jmcneill {
    334  1.11.8.1  christos 	struct fdtbus_interrupt_controller *ic;
    335  1.11.8.1  christos 
    336  1.11.8.1  christos 	ic = fdtbus_get_interrupt_controller(ihandle);
    337  1.11.8.1  christos 	if (ic == NULL)
    338  1.11.8.1  christos 		return false;
    339  1.11.8.1  christos 
    340  1.11.8.1  christos 	return ic->ic_funcs->intrstr(ic->ic_dev, __UNCONST(specifier), buf, buflen);
    341       1.9  jmcneill }
    342       1.9  jmcneill 
    343       1.9  jmcneill static int
    344       1.9  jmcneill find_address_cells(int phandle)
    345       1.9  jmcneill {
    346       1.9  jmcneill 	uint32_t cells;
    347       1.9  jmcneill 
    348       1.9  jmcneill 	if (of_getprop_uint32(phandle, "#address-cells", &cells) != 0)
    349       1.9  jmcneill 		cells = 2;
    350       1.9  jmcneill 
    351       1.9  jmcneill 	return cells;
    352       1.9  jmcneill }
    353       1.9  jmcneill 
    354       1.9  jmcneill static int
    355       1.9  jmcneill find_interrupt_cells(int phandle)
    356       1.9  jmcneill {
    357       1.9  jmcneill 	uint32_t cells;
    358       1.9  jmcneill 
    359       1.9  jmcneill 	while (phandle > 0) {
    360       1.9  jmcneill 		if (of_getprop_uint32(phandle, "#interrupt-cells", &cells) == 0)
    361       1.9  jmcneill 			return cells;
    362       1.9  jmcneill 		phandle = OF_parent(phandle);
    363       1.6     marty 	}
    364       1.9  jmcneill 	return 0;
    365       1.4     marty }
    366       1.1  jmcneill 
    367  1.11.8.1  christos static const u_int *
    368  1.11.8.1  christos get_specifier_from_map(int phandle, const u_int *interrupt_spec, int *piphandle)
    369       1.4     marty {
    370  1.11.8.1  christos 	const u_int *result = NULL;
    371      1.10  jmcneill 	int len, resid;
    372       1.4     marty 
    373  1.11.8.1  christos 	const u_int *data = fdtbus_get_prop(phandle, "interrupt-map", &len);
    374  1.11.8.1  christos 	if (data == NULL || len <= 0)
    375       1.4     marty 		return NULL;
    376      1.10  jmcneill 	resid = len;
    377       1.9  jmcneill 
    378       1.9  jmcneill 	/* child unit address: #address-cells prop of child bus node */
    379  1.11.8.1  christos 	const int cua_cells = find_address_cells(phandle);
    380       1.9  jmcneill 	/* child interrupt specifier: #interrupt-cells of the nexus node */
    381  1.11.8.1  christos 	const int cis_cells = find_interrupt_cells(phandle);
    382       1.9  jmcneill 
    383       1.9  jmcneill 	/* Offset (in cells) from map entry to child unit address specifier */
    384       1.9  jmcneill 	const u_int cua_off = 0;
    385       1.9  jmcneill 	/* Offset (in cells) from map entry to child interrupt specifier */
    386       1.9  jmcneill 	const u_int cis_off = cua_off + cua_cells;
    387       1.9  jmcneill 	/* Offset (in cells) from map entry to interrupt parent phandle */
    388       1.9  jmcneill 	const u_int ip_off = cis_off + cis_cells;
    389       1.9  jmcneill 	/* Offset (in cells) from map entry to parent unit specifier */
    390       1.9  jmcneill 	const u_int pus_off = ip_off + 1;
    391       1.9  jmcneill 
    392      1.10  jmcneill 	const u_int *p = (const u_int *)data;
    393       1.9  jmcneill 	while (resid > 0) {
    394       1.9  jmcneill 		/* Interrupt parent phandle */
    395       1.9  jmcneill 		const u_int iparent = fdtbus_get_phandle_from_native(be32toh(p[ip_off]));
    396       1.4     marty 
    397       1.9  jmcneill 		/* parent unit specifier: #address-cells of the interrupt parent */
    398       1.9  jmcneill 		const u_int pus_cells = find_address_cells(iparent);
    399       1.9  jmcneill 		/* parent interrupt specifier: #interrupt-cells of the interrupt parent */
    400       1.9  jmcneill 		const u_int pis_cells = find_interrupt_cells(iparent);
    401       1.9  jmcneill 
    402       1.9  jmcneill 		/* Offset (in cells) from map entry to parent interrupt specifier */
    403       1.9  jmcneill 		const u_int pis_off = pus_off + pus_cells;
    404       1.9  jmcneill 
    405      1.10  jmcneill #ifdef FDT_INTR_DEBUG
    406      1.10  jmcneill 		printf(" intr map (len %d):", pis_off + pis_cells);
    407      1.10  jmcneill 		for (int i = 0; i < pis_off + pis_cells; i++)
    408      1.10  jmcneill 			printf(" %08x", p[i]);
    409      1.10  jmcneill 		printf("\n");
    410      1.10  jmcneill #endif
    411      1.10  jmcneill 
    412  1.11.8.1  christos 		if (memcmp(&p[cis_off], interrupt_spec, cis_cells * 4) == 0) {
    413       1.9  jmcneill #ifdef FDT_INTR_DEBUG
    414  1.11.8.1  christos 			const int slen = pus_cells + pis_cells;
    415       1.9  jmcneill 			printf(" intr map match iparent %08x slen %d:", iparent, slen);
    416       1.9  jmcneill 			for (int i = 0; i < slen; i++)
    417       1.9  jmcneill 				printf(" %08x", p[pus_off + i]);
    418       1.9  jmcneill 			printf("\n");
    419       1.9  jmcneill #endif
    420  1.11.8.1  christos 			result = &p[pus_off];
    421       1.9  jmcneill 			*piphandle = iparent;
    422       1.6     marty 			goto done;
    423       1.4     marty 		}
    424       1.4     marty 		/* Determine the length of the entry and skip that many
    425       1.4     marty 		 * 32 bit words
    426       1.4     marty 		 */
    427       1.9  jmcneill 		const u_int reclen = pis_off + pis_cells;
    428       1.4     marty 		resid -= reclen * sizeof(u_int);
    429       1.4     marty 		p += reclen;
    430       1.4     marty 	}
    431       1.9  jmcneill 
    432       1.6     marty done:
    433       1.6     marty 	return result;
    434       1.4     marty }
    435       1.4     marty 
    436  1.11.8.1  christos static const u_int *
    437      1.10  jmcneill get_specifier_by_index(int phandle, int pindex, int *piphandle)
    438       1.4     marty {
    439      1.10  jmcneill 	const u_int *node_specifier;
    440       1.4     marty 	int interrupt_parent, interrupt_cells, len;
    441       1.4     marty 
    442       1.6     marty 	interrupt_parent = fdtbus_get_interrupt_parent(phandle);
    443       1.9  jmcneill 	if (interrupt_parent <= 0)
    444       1.4     marty 		return NULL;
    445       1.4     marty 
    446  1.11.8.1  christos 	node_specifier = fdtbus_get_prop(phandle, "interrupts", &len);
    447  1.11.8.1  christos 	if (node_specifier == NULL)
    448       1.4     marty 		return NULL;
    449       1.4     marty 
    450  1.11.8.1  christos 	interrupt_cells = find_interrupt_cells(interrupt_parent);
    451  1.11.8.1  christos 	if (interrupt_cells <= 0)
    452       1.4     marty 		return NULL;
    453       1.4     marty 
    454      1.10  jmcneill 	const u_int spec_length = len / 4;
    455      1.10  jmcneill 	const u_int nintr = spec_length / interrupt_cells;
    456       1.4     marty 	if (pindex >= nintr)
    457       1.4     marty 		return NULL;
    458       1.4     marty 
    459      1.10  jmcneill 	node_specifier += (interrupt_cells * pindex);
    460       1.4     marty 
    461  1.11.8.1  christos 	if (of_hasprop(interrupt_parent, "interrupt-map"))
    462  1.11.8.1  christos 		return get_specifier_from_map(interrupt_parent, node_specifier, piphandle);
    463      1.10  jmcneill 
    464      1.10  jmcneill 	*piphandle = interrupt_parent;
    465      1.10  jmcneill 
    466  1.11.8.1  christos 	return node_specifier;
    467       1.1  jmcneill }
    468