Home | History | Annotate | Line # | Download | only in fdt
fdt_intr.c revision 1.9
      1  1.9  jmcneill /* $NetBSD: fdt_intr.c,v 1.9 2017/06/02 00:55:26 jmcneill 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.9  jmcneill __KERNEL_RCSID(0, "$NetBSD: fdt_intr.c,v 1.9 2017/06/02 00:55:26 jmcneill 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.6     marty static u_int *get_specifier_by_index(int phandle, int pindex, u_int *spec);
     51  1.9  jmcneill static u_int *get_specifier_from_map(int phandle, u_int *spec, u_int spec_len, int *piphandle);
     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.9  jmcneill 	struct fdtbus_interrupt_controller *ic;
    126  1.9  jmcneill 	int ihandle = phandle;
    127  1.4     marty 	u_int *specifier;
    128  1.7     marty 	u_int spec_length;
    129  1.1  jmcneill 
    130  1.9  jmcneill 	specifier = get_specifier_by_index(phandle, index, &spec_length);
    131  1.9  jmcneill 	if (has_interrupt_map(phandle))
    132  1.9  jmcneill 		specifier = get_specifier_from_map(phandle, specifier,
    133  1.9  jmcneill 		    spec_length, &ihandle);
    134  1.9  jmcneill 
    135  1.4     marty 	ic = fdtbus_get_interrupt_controller(ihandle);
    136  1.9  jmcneill 	if (ic == NULL)
    137  1.9  jmcneill 		return NULL;
    138  1.9  jmcneill 
    139  1.9  jmcneill 	return ic->ic_funcs->establish(ic->ic_dev, specifier,
    140  1.9  jmcneill 	    ipl, flags, func, arg);
    141  1.1  jmcneill }
    142  1.1  jmcneill 
    143  1.1  jmcneill void
    144  1.1  jmcneill fdtbus_intr_disestablish(int phandle, void *ih)
    145  1.1  jmcneill {
    146  1.1  jmcneill 	struct fdtbus_interrupt_controller *ic;
    147  1.1  jmcneill 
    148  1.1  jmcneill 	ic = fdtbus_get_interrupt_controller(phandle);
    149  1.1  jmcneill 	KASSERT(ic != NULL);
    150  1.1  jmcneill 
    151  1.1  jmcneill 	return ic->ic_funcs->disestablish(ic->ic_dev, ih);
    152  1.1  jmcneill }
    153  1.1  jmcneill 
    154  1.1  jmcneill bool
    155  1.1  jmcneill fdtbus_intr_str(int phandle, u_int index, char *buf, size_t buflen)
    156  1.1  jmcneill {
    157  1.1  jmcneill 	struct fdtbus_interrupt_controller *ic;
    158  1.9  jmcneill 	int ihandle = phandle;
    159  1.4     marty 	u_int *specifier;
    160  1.7     marty 	u_int spec_length;
    161  1.9  jmcneill 
    162  1.9  jmcneill 	specifier = get_specifier_by_index(phandle, index, &spec_length);
    163  1.9  jmcneill 	if (has_interrupt_map(phandle))
    164  1.9  jmcneill 		specifier = get_specifier_from_map(phandle, specifier,
    165  1.9  jmcneill 		    spec_length, &ihandle);
    166  1.9  jmcneill 
    167  1.9  jmcneill 	ic = fdtbus_get_interrupt_controller(ihandle);
    168  1.9  jmcneill 	if (ic == NULL)
    169  1.9  jmcneill 		return false;
    170  1.9  jmcneill 
    171  1.9  jmcneill 	return ic->ic_funcs->intrstr(ic->ic_dev, specifier, buf, buflen);
    172  1.9  jmcneill }
    173  1.9  jmcneill 
    174  1.9  jmcneill static int
    175  1.9  jmcneill find_interrupt_map(int phandle)
    176  1.9  jmcneill {
    177  1.9  jmcneill 	while (phandle > 0) {
    178  1.9  jmcneill 		if (of_hasprop(phandle, "interrupt-map"))
    179  1.9  jmcneill 			return phandle;
    180  1.9  jmcneill 		phandle = OF_parent(phandle);
    181  1.4     marty 	}
    182  1.9  jmcneill 	return -1;
    183  1.9  jmcneill }
    184  1.9  jmcneill 
    185  1.9  jmcneill static int
    186  1.9  jmcneill find_address_cells(int phandle)
    187  1.9  jmcneill {
    188  1.9  jmcneill 	uint32_t cells;
    189  1.9  jmcneill 
    190  1.9  jmcneill 	if (of_getprop_uint32(phandle, "#address-cells", &cells) != 0)
    191  1.9  jmcneill 		cells = 2;
    192  1.9  jmcneill 
    193  1.9  jmcneill 	return cells;
    194  1.9  jmcneill }
    195  1.9  jmcneill 
    196  1.9  jmcneill static int
    197  1.9  jmcneill find_interrupt_cells(int phandle)
    198  1.9  jmcneill {
    199  1.9  jmcneill 	uint32_t cells;
    200  1.9  jmcneill 
    201  1.9  jmcneill 	while (phandle > 0) {
    202  1.9  jmcneill 		if (of_getprop_uint32(phandle, "#interrupt-cells", &cells) == 0)
    203  1.9  jmcneill 			return cells;
    204  1.9  jmcneill 		phandle = OF_parent(phandle);
    205  1.6     marty 	}
    206  1.9  jmcneill 	return 0;
    207  1.4     marty }
    208  1.1  jmcneill 
    209  1.4     marty static bool
    210  1.4     marty has_interrupt_map(int phandle)
    211  1.4     marty {
    212  1.9  jmcneill 	return find_interrupt_map(OF_parent(phandle)) != -1;
    213  1.4     marty }
    214  1.4     marty 
    215  1.4     marty static u_int *
    216  1.9  jmcneill get_specifier_from_map(int phandle, u_int *specifier, u_int spec_length, int *piphandle)
    217  1.4     marty {
    218  1.6     marty 	u_int *result = NULL;
    219  1.4     marty 
    220  1.9  jmcneill 	const int nexus_phandle = find_interrupt_map(phandle);
    221  1.4     marty 
    222  1.9  jmcneill 	int len = OF_getproplen(nexus_phandle, "interrupt-map");
    223  1.4     marty 	if (len <= 0) {
    224  1.6     marty 		printf("%s: no interrupt-map.\n", __func__);
    225  1.4     marty 		return NULL;
    226  1.4     marty 	}
    227  1.9  jmcneill 	int resid = len;
    228  1.4     marty 	char *data = kmem_alloc(len, KM_SLEEP);
    229  1.9  jmcneill 	len = OF_getprop(nexus_phandle, "interrupt-map", data, len);
    230  1.4     marty 	if (len <= 0) {
    231  1.9  jmcneill 		printf("%s: can't get property interrupt-map.\n", __func__);
    232  1.6     marty 		goto done;
    233  1.4     marty 	}
    234  1.9  jmcneill 
    235  1.9  jmcneill 	/* child unit address: #address-cells prop of child bus node */
    236  1.9  jmcneill 	const int cua_cells = find_address_cells(nexus_phandle);
    237  1.9  jmcneill 	/* child interrupt specifier: #interrupt-cells of the nexus node */
    238  1.9  jmcneill 	const int cis_cells = find_interrupt_cells(nexus_phandle);
    239  1.9  jmcneill 
    240  1.9  jmcneill 	/* Offset (in cells) from map entry to child unit address specifier */
    241  1.9  jmcneill 	const u_int cua_off = 0;
    242  1.9  jmcneill 	/* Offset (in cells) from map entry to child interrupt specifier */
    243  1.9  jmcneill 	const u_int cis_off = cua_off + cua_cells;
    244  1.9  jmcneill 	/* Offset (in cells) from map entry to interrupt parent phandle */
    245  1.9  jmcneill 	const u_int ip_off = cis_off + cis_cells;
    246  1.9  jmcneill 	/* Offset (in cells) from map entry to parent unit specifier */
    247  1.9  jmcneill 	const u_int pus_off = ip_off + 1;
    248  1.9  jmcneill 
    249  1.9  jmcneill #ifdef FDT_INTR_DEBUG
    250  1.9  jmcneill 	printf("cua_cells: %d, cis_cells: %d, ip_off = %d\n", cua_cells, cis_cells, ip_off);
    251  1.9  jmcneill 	printf("searching for interrupt in map:");
    252  1.9  jmcneill 	for (int i = 0; i < spec_length; i++)
    253  1.9  jmcneill 		printf(" %08x", specifier[i]);
    254  1.9  jmcneill 	printf("\n");
    255  1.9  jmcneill #endif
    256  1.9  jmcneill 
    257  1.4     marty 	u_int *p = (u_int *)data;
    258  1.9  jmcneill 	while (resid > 0) {
    259  1.9  jmcneill 		/* Interrupt parent phandle */
    260  1.9  jmcneill 		const u_int iparent = fdtbus_get_phandle_from_native(be32toh(p[ip_off]));
    261  1.4     marty 
    262  1.9  jmcneill 		/* parent unit specifier: #address-cells of the interrupt parent */
    263  1.9  jmcneill 		const u_int pus_cells = find_address_cells(iparent);
    264  1.9  jmcneill 		/* parent interrupt specifier: #interrupt-cells of the interrupt parent */
    265  1.9  jmcneill 		const u_int pis_cells = find_interrupt_cells(iparent);
    266  1.9  jmcneill 
    267  1.9  jmcneill 		/* Offset (in cells) from map entry to parent interrupt specifier */
    268  1.9  jmcneill 		const u_int pis_off = pus_off + pus_cells;
    269  1.9  jmcneill 
    270  1.9  jmcneill 		if (cis_cells == spec_length && memcmp(&p[cis_off], specifier, spec_length * 4) == 0) {
    271  1.9  jmcneill 			const int slen = pus_cells + pis_cells;
    272  1.9  jmcneill #ifdef FDT_INTR_DEBUG
    273  1.9  jmcneill 			printf(" intr map match iparent %08x slen %d:", iparent, slen);
    274  1.9  jmcneill 			for (int i = 0; i < slen; i++)
    275  1.9  jmcneill 				printf(" %08x", p[pus_off + i]);
    276  1.9  jmcneill 			printf("\n");
    277  1.9  jmcneill #endif
    278  1.9  jmcneill 			result = kmem_alloc(slen, KM_SLEEP);
    279  1.9  jmcneill 			memcpy(result, &p[pus_off], slen * 4);
    280  1.9  jmcneill 			*piphandle = iparent;
    281  1.6     marty 			goto done;
    282  1.4     marty 		}
    283  1.4     marty 		/* Determine the length of the entry and skip that many
    284  1.4     marty 		 * 32 bit words
    285  1.4     marty 		 */
    286  1.9  jmcneill 		const u_int reclen = pis_off + pis_cells;
    287  1.4     marty 		resid -= reclen * sizeof(u_int);
    288  1.4     marty 		p += reclen;
    289  1.4     marty 	}
    290  1.9  jmcneill 
    291  1.6     marty done:
    292  1.6     marty 	kmem_free(data, len);
    293  1.6     marty 	return result;
    294  1.4     marty }
    295  1.4     marty 
    296  1.9  jmcneill static u_int *
    297  1.9  jmcneill get_specifier_by_index(int phandle, int pindex, u_int *spec_length)
    298  1.4     marty {
    299  1.4     marty 	u_int *specifiers;
    300  1.7     marty 	u_int *specifier;
    301  1.4     marty 	int interrupt_parent, interrupt_cells, len;
    302  1.4     marty 
    303  1.6     marty 	interrupt_parent = fdtbus_get_interrupt_parent(phandle);
    304  1.9  jmcneill 	if (interrupt_parent <= 0)
    305  1.4     marty 		return NULL;
    306  1.4     marty 
    307  1.9  jmcneill 	interrupt_cells = find_interrupt_cells(interrupt_parent);
    308  1.9  jmcneill 	if (interrupt_cells <= 0)
    309  1.4     marty 		return NULL;
    310  1.4     marty 
    311  1.4     marty 	len = OF_getproplen(phandle, "interrupts");
    312  1.9  jmcneill 	if (len <= 0)
    313  1.4     marty 		return NULL;
    314  1.4     marty 
    315  1.4     marty 	const u_int nintr = len / interrupt_cells;
    316  1.4     marty 
    317  1.4     marty 	if (pindex >= nintr)
    318  1.4     marty 		return NULL;
    319  1.4     marty 
    320  1.4     marty 	specifiers = kmem_alloc(len, KM_SLEEP);
    321  1.4     marty 
    322  1.4     marty 	if (OF_getprop(phandle, "interrupts", specifiers, len) != len) {
    323  1.4     marty 		kmem_free(specifiers, len);
    324  1.4     marty 		return NULL;
    325  1.4     marty 	}
    326  1.7     marty 	specifier = kmem_alloc(interrupt_cells * sizeof(u_int), KM_SLEEP);
    327  1.9  jmcneill 	*spec_length = interrupt_cells;
    328  1.6     marty 	for (int i = 0; i < interrupt_cells; i++)
    329  1.8  jmcneill 		specifier[i] = specifiers[pindex * interrupt_cells + i];
    330  1.6     marty 	kmem_free(specifiers, len);
    331  1.6     marty 	return specifier;
    332  1.1  jmcneill }
    333