Home | History | Annotate | Line # | Download | only in fdt
fdt_intr.c revision 1.22
      1  1.22   hkenken /* $NetBSD: fdt_intr.c,v 1.22 2019/06/14 11:08:18 hkenken Exp $ */
      2   1.1  jmcneill 
      3   1.1  jmcneill /*-
      4  1.13  jmcneill  * 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.22   hkenken __KERNEL_RCSID(0, "$NetBSD: fdt_intr.c,v 1.22 2019/06/14 11:08:18 hkenken 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.12  jmcneill #include <sys/queue.h>
     36   1.1  jmcneill 
     37   1.1  jmcneill #include <libfdt.h>
     38   1.1  jmcneill #include <dev/fdt/fdtvar.h>
     39   1.1  jmcneill 
     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.12  jmcneill 	LIST_ENTRY(fdtbus_interrupt_controller) ic_next;
     46   1.1  jmcneill };
     47   1.1  jmcneill 
     48  1.12  jmcneill static LIST_HEAD(, fdtbus_interrupt_controller) fdtbus_interrupt_controllers =
     49  1.12  jmcneill     LIST_HEAD_INITIALIZER(fdtbus_interrupt_controllers);
     50  1.12  jmcneill 
     51  1.11  jmcneill struct fdtbus_interrupt_cookie {
     52  1.11  jmcneill 	struct fdtbus_interrupt_controller *c_ic;
     53  1.11  jmcneill 	void *c_ih;
     54  1.16  jmcneill 
     55  1.16  jmcneill 	LIST_ENTRY(fdtbus_interrupt_cookie) c_next;
     56  1.11  jmcneill };
     57  1.11  jmcneill 
     58  1.16  jmcneill static LIST_HEAD(, fdtbus_interrupt_cookie) fdtbus_interrupt_cookies =
     59  1.16  jmcneill     LIST_HEAD_INITIALIZER(fdtbus_interrupt_cookies);
     60  1.16  jmcneill 
     61  1.17  jmcneill static const u_int *	get_specifier_by_index(int, int, int *);
     62  1.17  jmcneill static const u_int *	get_specifier_from_map(int, const u_int *, int *);
     63   1.4     marty 
     64  1.15  jmcneill /*
     65  1.15  jmcneill  * Find the interrupt controller for a given node. This function will either
     66  1.15  jmcneill  * return the phandle of the interrupt controller for this node, or the phandle
     67  1.15  jmcneill  * of a node containing an interrupt-map table that can be used to find the
     68  1.15  jmcneill  * real interrupt controller.
     69  1.15  jmcneill  */
     70   1.1  jmcneill static int
     71   1.1  jmcneill fdtbus_get_interrupt_parent(int phandle)
     72   1.1  jmcneill {
     73  1.14  jmcneill 	int iparent = phandle;
     74   1.1  jmcneill 
     75  1.14  jmcneill 	do {
     76  1.15  jmcneill 		/*
     77  1.15  jmcneill 		 * If the node is an interrupt-controller, we are done. Note that
     78  1.15  jmcneill 		 * a node cannot be an interrupt-controller for itself, so we skip
     79  1.15  jmcneill 		 * the leaf node here.
     80  1.15  jmcneill 		 */
     81  1.15  jmcneill 		if (phandle != iparent && of_hasprop(iparent, "interrupt-controller"))
     82  1.15  jmcneill 			return iparent;
     83  1.15  jmcneill 
     84  1.15  jmcneill 		/*
     85  1.15  jmcneill 		 * If the node has an explicit interrupt-parent, follow the reference.
     86  1.15  jmcneill 		 */
     87  1.14  jmcneill 		if (of_hasprop(iparent, "interrupt-parent"))
     88  1.14  jmcneill 			return fdtbus_get_phandle(iparent, "interrupt-parent");
     89  1.15  jmcneill 
     90  1.15  jmcneill 		/*
     91  1.15  jmcneill 		 * If the node has an interrupt-map, use it. The caller is responsible
     92  1.15  jmcneill 		 * for parsing the interrupt-map and finding the real interrupt parent.
     93  1.15  jmcneill 		 */
     94  1.20  jakllsch 		if (phandle != iparent && of_hasprop(iparent, "interrupt-map"))
     95  1.14  jmcneill 			return iparent;
     96  1.15  jmcneill 
     97  1.15  jmcneill 		/*
     98  1.15  jmcneill 		 * Continue searching up the tree.
     99  1.15  jmcneill 		 */
    100  1.14  jmcneill 		iparent = OF_parent(iparent);
    101  1.14  jmcneill 	} while (iparent > 0);
    102   1.1  jmcneill 
    103  1.14  jmcneill 	return 0;
    104   1.1  jmcneill }
    105   1.1  jmcneill 
    106   1.1  jmcneill static struct fdtbus_interrupt_controller *
    107  1.11  jmcneill fdtbus_get_interrupt_controller(int phandle)
    108   1.4     marty {
    109   1.4     marty 	struct fdtbus_interrupt_controller * ic;
    110  1.12  jmcneill 	LIST_FOREACH(ic, &fdtbus_interrupt_controllers, ic_next) {
    111  1.12  jmcneill 		if (ic->ic_phandle == phandle)
    112   1.4     marty 			return ic;
    113   1.4     marty 	}
    114   1.4     marty 	return NULL;
    115   1.4     marty }
    116   1.4     marty 
    117   1.1  jmcneill int
    118   1.1  jmcneill fdtbus_register_interrupt_controller(device_t dev, int phandle,
    119   1.1  jmcneill     const struct fdtbus_interrupt_controller_func *funcs)
    120   1.1  jmcneill {
    121   1.1  jmcneill 	struct fdtbus_interrupt_controller *ic;
    122   1.1  jmcneill 
    123   1.1  jmcneill 	ic = kmem_alloc(sizeof(*ic), KM_SLEEP);
    124   1.1  jmcneill 	ic->ic_dev = dev;
    125   1.1  jmcneill 	ic->ic_phandle = phandle;
    126   1.1  jmcneill 	ic->ic_funcs = funcs;
    127   1.1  jmcneill 
    128  1.12  jmcneill 	LIST_INSERT_HEAD(&fdtbus_interrupt_controllers, ic, ic_next);
    129   1.1  jmcneill 
    130   1.1  jmcneill 	return 0;
    131   1.1  jmcneill }
    132   1.1  jmcneill 
    133   1.1  jmcneill void *
    134   1.1  jmcneill fdtbus_intr_establish(int phandle, u_int index, int ipl, int flags,
    135   1.1  jmcneill     int (*func)(void *), void *arg)
    136   1.1  jmcneill {
    137  1.17  jmcneill 	const u_int *specifier;
    138  1.10  jmcneill 	int ihandle;
    139   1.1  jmcneill 
    140  1.10  jmcneill 	specifier = get_specifier_by_index(phandle, index, &ihandle);
    141  1.10  jmcneill 	if (specifier == NULL)
    142  1.10  jmcneill 		return NULL;
    143   1.9  jmcneill 
    144  1.18  jmcneill 	return fdtbus_intr_establish_raw(ihandle, specifier, ipl,
    145  1.18  jmcneill 	    flags, func, arg);
    146  1.18  jmcneill }
    147  1.18  jmcneill 
    148  1.18  jmcneill void *
    149  1.21  jakllsch fdtbus_intr_establish_byname(int phandle, const char *name, int ipl,
    150  1.21  jakllsch     int flags, int (*func)(void *), void *arg)
    151  1.21  jakllsch {
    152  1.21  jakllsch 	u_int index;
    153  1.21  jakllsch 	int err;
    154  1.21  jakllsch 
    155  1.21  jakllsch 	err = fdtbus_get_index(phandle, "interrupt-names", name, &index);
    156  1.21  jakllsch 	if (err != 0)
    157  1.21  jakllsch 		return NULL;
    158  1.21  jakllsch 
    159  1.21  jakllsch 	return fdtbus_intr_establish(phandle, index, ipl, flags, func, arg);
    160  1.21  jakllsch }
    161  1.21  jakllsch 
    162  1.21  jakllsch void *
    163  1.18  jmcneill fdtbus_intr_establish_raw(int ihandle, const u_int *specifier, int ipl,
    164  1.18  jmcneill     int flags, int (*func)(void *), void *arg)
    165  1.18  jmcneill {
    166  1.18  jmcneill 	struct fdtbus_interrupt_controller *ic;
    167  1.18  jmcneill 	struct fdtbus_interrupt_cookie *c;
    168  1.18  jmcneill 	void *ih;
    169  1.18  jmcneill 
    170   1.4     marty 	ic = fdtbus_get_interrupt_controller(ihandle);
    171   1.9  jmcneill 	if (ic == NULL)
    172   1.9  jmcneill 		return NULL;
    173   1.9  jmcneill 
    174  1.17  jmcneill 	ih = ic->ic_funcs->establish(ic->ic_dev, __UNCONST(specifier),
    175   1.9  jmcneill 	    ipl, flags, func, arg);
    176  1.11  jmcneill 	if (ih != NULL) {
    177  1.11  jmcneill 		c = kmem_alloc(sizeof(*c), KM_SLEEP);
    178  1.11  jmcneill 		c->c_ic = ic;
    179  1.11  jmcneill 		c->c_ih = ih;
    180  1.16  jmcneill 		LIST_INSERT_HEAD(&fdtbus_interrupt_cookies, c, c_next);
    181  1.11  jmcneill 	}
    182  1.11  jmcneill 
    183  1.16  jmcneill 	return ih;
    184   1.1  jmcneill }
    185   1.1  jmcneill 
    186   1.1  jmcneill void
    187  1.11  jmcneill fdtbus_intr_disestablish(int phandle, void *cookie)
    188   1.1  jmcneill {
    189  1.16  jmcneill 	struct fdtbus_interrupt_controller *ic = NULL;
    190  1.16  jmcneill 	struct fdtbus_interrupt_cookie *c;
    191  1.16  jmcneill 
    192  1.16  jmcneill 	LIST_FOREACH(c, &fdtbus_interrupt_cookies, c_next) {
    193  1.16  jmcneill 		if (c->c_ih == cookie) {
    194  1.16  jmcneill 			ic = c->c_ic;
    195  1.16  jmcneill 			LIST_REMOVE(c, c_next);
    196  1.16  jmcneill 			kmem_free(c, sizeof(*c));
    197  1.16  jmcneill 			break;
    198  1.16  jmcneill 		}
    199  1.16  jmcneill 	}
    200  1.16  jmcneill 
    201  1.19     skrll 	if (ic == NULL)
    202  1.16  jmcneill 		panic("%s: interrupt handle not valid", __func__);
    203   1.1  jmcneill 
    204  1.16  jmcneill 	return ic->ic_funcs->disestablish(ic->ic_dev, cookie);
    205   1.1  jmcneill }
    206   1.1  jmcneill 
    207   1.1  jmcneill bool
    208   1.1  jmcneill fdtbus_intr_str(int phandle, u_int index, char *buf, size_t buflen)
    209   1.1  jmcneill {
    210  1.17  jmcneill 	const u_int *specifier;
    211  1.10  jmcneill 	int ihandle;
    212   1.9  jmcneill 
    213  1.10  jmcneill 	specifier = get_specifier_by_index(phandle, index, &ihandle);
    214  1.18  jmcneill 	if (specifier == NULL)
    215  1.18  jmcneill 		return false;
    216  1.18  jmcneill 
    217  1.18  jmcneill 	return fdtbus_intr_str_raw(ihandle, specifier, buf, buflen);
    218  1.18  jmcneill }
    219  1.18  jmcneill 
    220  1.18  jmcneill bool
    221  1.18  jmcneill fdtbus_intr_str_raw(int ihandle, const u_int *specifier, char *buf, size_t buflen)
    222  1.18  jmcneill {
    223  1.18  jmcneill 	struct fdtbus_interrupt_controller *ic;
    224   1.9  jmcneill 
    225   1.9  jmcneill 	ic = fdtbus_get_interrupt_controller(ihandle);
    226   1.9  jmcneill 	if (ic == NULL)
    227   1.9  jmcneill 		return false;
    228   1.9  jmcneill 
    229  1.17  jmcneill 	return ic->ic_funcs->intrstr(ic->ic_dev, __UNCONST(specifier), buf, buflen);
    230   1.9  jmcneill }
    231   1.9  jmcneill 
    232   1.9  jmcneill static int
    233   1.9  jmcneill find_address_cells(int phandle)
    234   1.9  jmcneill {
    235   1.9  jmcneill 	uint32_t cells;
    236   1.9  jmcneill 
    237   1.9  jmcneill 	if (of_getprop_uint32(phandle, "#address-cells", &cells) != 0)
    238   1.9  jmcneill 		cells = 2;
    239   1.9  jmcneill 
    240   1.9  jmcneill 	return cells;
    241   1.9  jmcneill }
    242   1.9  jmcneill 
    243   1.9  jmcneill static int
    244   1.9  jmcneill find_interrupt_cells(int phandle)
    245   1.9  jmcneill {
    246   1.9  jmcneill 	uint32_t cells;
    247   1.9  jmcneill 
    248   1.9  jmcneill 	while (phandle > 0) {
    249   1.9  jmcneill 		if (of_getprop_uint32(phandle, "#interrupt-cells", &cells) == 0)
    250   1.9  jmcneill 			return cells;
    251   1.9  jmcneill 		phandle = OF_parent(phandle);
    252   1.6     marty 	}
    253   1.9  jmcneill 	return 0;
    254   1.4     marty }
    255   1.1  jmcneill 
    256  1.17  jmcneill static const u_int *
    257  1.13  jmcneill get_specifier_from_map(int phandle, const u_int *interrupt_spec, int *piphandle)
    258   1.4     marty {
    259  1.17  jmcneill 	const u_int *result = NULL;
    260  1.10  jmcneill 	int len, resid;
    261   1.4     marty 
    262  1.13  jmcneill 	const u_int *data = fdtbus_get_prop(phandle, "interrupt-map", &len);
    263  1.13  jmcneill 	if (data == NULL || len <= 0)
    264   1.4     marty 		return NULL;
    265  1.10  jmcneill 	resid = len;
    266   1.9  jmcneill 
    267   1.9  jmcneill 	/* child unit address: #address-cells prop of child bus node */
    268  1.13  jmcneill 	const int cua_cells = find_address_cells(phandle);
    269   1.9  jmcneill 	/* child interrupt specifier: #interrupt-cells of the nexus node */
    270  1.13  jmcneill 	const int cis_cells = find_interrupt_cells(phandle);
    271   1.9  jmcneill 
    272   1.9  jmcneill 	/* Offset (in cells) from map entry to child unit address specifier */
    273   1.9  jmcneill 	const u_int cua_off = 0;
    274   1.9  jmcneill 	/* Offset (in cells) from map entry to child interrupt specifier */
    275   1.9  jmcneill 	const u_int cis_off = cua_off + cua_cells;
    276   1.9  jmcneill 	/* Offset (in cells) from map entry to interrupt parent phandle */
    277   1.9  jmcneill 	const u_int ip_off = cis_off + cis_cells;
    278   1.9  jmcneill 	/* Offset (in cells) from map entry to parent unit specifier */
    279   1.9  jmcneill 	const u_int pus_off = ip_off + 1;
    280   1.9  jmcneill 
    281  1.10  jmcneill 	const u_int *p = (const u_int *)data;
    282   1.9  jmcneill 	while (resid > 0) {
    283   1.9  jmcneill 		/* Interrupt parent phandle */
    284   1.9  jmcneill 		const u_int iparent = fdtbus_get_phandle_from_native(be32toh(p[ip_off]));
    285   1.4     marty 
    286   1.9  jmcneill 		/* parent unit specifier: #address-cells of the interrupt parent */
    287   1.9  jmcneill 		const u_int pus_cells = find_address_cells(iparent);
    288   1.9  jmcneill 		/* parent interrupt specifier: #interrupt-cells of the interrupt parent */
    289   1.9  jmcneill 		const u_int pis_cells = find_interrupt_cells(iparent);
    290   1.9  jmcneill 
    291   1.9  jmcneill 		/* Offset (in cells) from map entry to parent interrupt specifier */
    292   1.9  jmcneill 		const u_int pis_off = pus_off + pus_cells;
    293   1.9  jmcneill 
    294  1.10  jmcneill #ifdef FDT_INTR_DEBUG
    295  1.10  jmcneill 		printf(" intr map (len %d):", pis_off + pis_cells);
    296  1.10  jmcneill 		for (int i = 0; i < pis_off + pis_cells; i++)
    297  1.10  jmcneill 			printf(" %08x", p[i]);
    298  1.10  jmcneill 		printf("\n");
    299  1.10  jmcneill #endif
    300  1.10  jmcneill 
    301  1.13  jmcneill 		if (memcmp(&p[cis_off], interrupt_spec, cis_cells * 4) == 0) {
    302  1.17  jmcneill #ifdef FDT_INTR_DEBUG
    303   1.9  jmcneill 			const int slen = pus_cells + pis_cells;
    304   1.9  jmcneill 			printf(" intr map match iparent %08x slen %d:", iparent, slen);
    305   1.9  jmcneill 			for (int i = 0; i < slen; i++)
    306   1.9  jmcneill 				printf(" %08x", p[pus_off + i]);
    307   1.9  jmcneill 			printf("\n");
    308   1.9  jmcneill #endif
    309  1.17  jmcneill 			result = &p[pus_off];
    310   1.9  jmcneill 			*piphandle = iparent;
    311   1.6     marty 			goto done;
    312   1.4     marty 		}
    313   1.4     marty 		/* Determine the length of the entry and skip that many
    314   1.4     marty 		 * 32 bit words
    315   1.4     marty 		 */
    316   1.9  jmcneill 		const u_int reclen = pis_off + pis_cells;
    317   1.4     marty 		resid -= reclen * sizeof(u_int);
    318   1.4     marty 		p += reclen;
    319   1.4     marty 	}
    320   1.9  jmcneill 
    321   1.6     marty done:
    322   1.6     marty 	return result;
    323   1.4     marty }
    324   1.4     marty 
    325  1.22   hkenken 
    326  1.22   hkenken static const u_int *
    327  1.22   hkenken get_specifier_from_extended(int phandle, int pindex, int *piphandle)
    328  1.22   hkenken {
    329  1.22   hkenken 	const u_int *result = NULL;
    330  1.22   hkenken 	struct fdt_phandle_data data;
    331  1.22   hkenken 
    332  1.22   hkenken 	if (fdtbus_get_phandle_with_data(phandle, "interrupts-extended",
    333  1.22   hkenken 		"#interrupt-cells", pindex, &data) == 0) {
    334  1.22   hkenken 		*piphandle = data.phandle;
    335  1.22   hkenken 		result = data.values;
    336  1.22   hkenken 	}
    337  1.22   hkenken 
    338  1.22   hkenken 	return result;
    339  1.22   hkenken }
    340  1.22   hkenken 
    341  1.17  jmcneill static const u_int *
    342  1.10  jmcneill get_specifier_by_index(int phandle, int pindex, int *piphandle)
    343   1.4     marty {
    344  1.10  jmcneill 	const u_int *node_specifier;
    345   1.4     marty 	int interrupt_parent, interrupt_cells, len;
    346   1.4     marty 
    347  1.22   hkenken 	if (of_hasprop(phandle, "interrupts-extended"))
    348  1.22   hkenken 		return get_specifier_from_extended(phandle, pindex, piphandle);
    349  1.22   hkenken 
    350   1.6     marty 	interrupt_parent = fdtbus_get_interrupt_parent(phandle);
    351   1.9  jmcneill 	if (interrupt_parent <= 0)
    352   1.4     marty 		return NULL;
    353   1.4     marty 
    354  1.13  jmcneill 	node_specifier = fdtbus_get_prop(phandle, "interrupts", &len);
    355  1.13  jmcneill 	if (node_specifier == NULL)
    356  1.13  jmcneill 		return NULL;
    357  1.13  jmcneill 
    358   1.9  jmcneill 	interrupt_cells = find_interrupt_cells(interrupt_parent);
    359   1.9  jmcneill 	if (interrupt_cells <= 0)
    360   1.4     marty 		return NULL;
    361   1.4     marty 
    362  1.10  jmcneill 	const u_int spec_length = len / 4;
    363  1.10  jmcneill 	const u_int nintr = spec_length / interrupt_cells;
    364   1.4     marty 	if (pindex >= nintr)
    365   1.4     marty 		return NULL;
    366   1.4     marty 
    367  1.10  jmcneill 	node_specifier += (interrupt_cells * pindex);
    368   1.4     marty 
    369  1.13  jmcneill 	if (of_hasprop(interrupt_parent, "interrupt-map"))
    370  1.13  jmcneill 		return get_specifier_from_map(interrupt_parent, node_specifier, piphandle);
    371  1.13  jmcneill 
    372  1.10  jmcneill 	*piphandle = interrupt_parent;
    373  1.10  jmcneill 
    374  1.17  jmcneill 	return node_specifier;
    375   1.1  jmcneill }
    376