Home | History | Annotate | Line # | Download | only in fdt
fdt_intr.c revision 1.8
      1  1.8  jmcneill /* $NetBSD: fdt_intr.c,v 1.8 2016/05/25 12:43:08 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.8  jmcneill __KERNEL_RCSID(0, "$NetBSD: fdt_intr.c,v 1.8 2016/05/25 12:43:08 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_entry_from_map(int phandle, int pindex, u_int *spec);
     51  1.6     marty static u_int *get_specifier_by_index(int phandle, int pindex, u_int *spec);
     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.7     marty 	void * result = NULL;
    126  1.4     marty 	u_int *specifier;
    127  1.7     marty 	u_int spec_length;
    128  1.4     marty 	int ihandle;
    129  1.1  jmcneill 	struct fdtbus_interrupt_controller *ic;
    130  1.1  jmcneill 
    131  1.4     marty 	if (has_interrupt_map(phandle)) {
    132  1.7     marty 		specifier = get_entry_from_map(phandle, index, &spec_length);
    133  1.4     marty 		ihandle = be32toh(specifier[1]);
    134  1.4     marty 		ihandle = fdtbus_get_phandle_from_native(ihandle);
    135  1.4     marty 		specifier += 2;
    136  1.4     marty 	} else {
    137  1.7     marty 		specifier = get_specifier_by_index(phandle, index,
    138  1.7     marty 						   &spec_length);
    139  1.4     marty 		ihandle = phandle;
    140  1.4     marty 	}
    141  1.6     marty 	if (specifier == NULL) {
    142  1.6     marty 		printf("%s: Unable to get specifier %d for phandle %d\n",
    143  1.6     marty 		       __func__, index, phandle);
    144  1.7     marty 		goto done;
    145  1.6     marty 	}
    146  1.4     marty 	ic = fdtbus_get_interrupt_controller(ihandle);
    147  1.6     marty 	if (ic == NULL) {
    148  1.6     marty 		printf("%s: Unable to get interrupt controller for %d\n",
    149  1.6     marty 		       __func__, ihandle);
    150  1.7     marty 		goto done;
    151  1.6     marty 	}
    152  1.7     marty 	result = ic->ic_funcs->establish(ic->ic_dev, specifier,
    153  1.4     marty 					       ipl, flags, func, arg);
    154  1.7     marty done:
    155  1.7     marty 	if (has_interrupt_map(phandle))
    156  1.7     marty 	    specifier -= 2;
    157  1.7     marty 	if (specifier && spec_length > 0)
    158  1.7     marty 		kmem_free(specifier, spec_length);
    159  1.7     marty 	return result;
    160  1.1  jmcneill }
    161  1.1  jmcneill 
    162  1.1  jmcneill void
    163  1.1  jmcneill fdtbus_intr_disestablish(int phandle, void *ih)
    164  1.1  jmcneill {
    165  1.1  jmcneill 	struct fdtbus_interrupt_controller *ic;
    166  1.1  jmcneill 
    167  1.1  jmcneill 	ic = fdtbus_get_interrupt_controller(phandle);
    168  1.1  jmcneill 	KASSERT(ic != NULL);
    169  1.1  jmcneill 
    170  1.1  jmcneill 	return ic->ic_funcs->disestablish(ic->ic_dev, ih);
    171  1.1  jmcneill }
    172  1.1  jmcneill 
    173  1.1  jmcneill bool
    174  1.1  jmcneill fdtbus_intr_str(int phandle, u_int index, char *buf, size_t buflen)
    175  1.1  jmcneill {
    176  1.7     marty 	bool result = false;
    177  1.1  jmcneill 	struct fdtbus_interrupt_controller *ic;
    178  1.4     marty 	int ihandle;
    179  1.4     marty 	u_int *specifier;
    180  1.7     marty 	u_int spec_length;
    181  1.4     marty 	if (has_interrupt_map(phandle)) {
    182  1.7     marty 		specifier = get_entry_from_map(phandle, index,
    183  1.7     marty 			&spec_length);
    184  1.4     marty 		ihandle = be32toh(specifier[1]);
    185  1.4     marty 		ihandle = fdtbus_get_phandle_from_native(ihandle);
    186  1.6     marty 		specifier += 2;
    187  1.4     marty 	} else {
    188  1.4     marty 		ihandle = phandle;
    189  1.7     marty 		specifier = get_specifier_by_index(phandle, index,
    190  1.7     marty 						   &spec_length);
    191  1.6     marty 	}
    192  1.6     marty 	if (specifier == NULL) {
    193  1.6     marty 		printf("%s: Unable to get specifier %d for phandle %d\n",
    194  1.6     marty 		       __func__, index, phandle);
    195  1.7     marty 		goto done;
    196  1.4     marty 	}
    197  1.4     marty 	ic = fdtbus_get_interrupt_controller(ihandle);
    198  1.6     marty 	if (ic == NULL) {
    199  1.6     marty 		printf("%s: Unable to get interrupt controller for %d\n",
    200  1.6     marty 		       __func__, ihandle);
    201  1.7     marty 		goto done;
    202  1.6     marty 	}
    203  1.7     marty 	result = ic->ic_funcs->intrstr(ic->ic_dev, specifier, buf, buflen);
    204  1.7     marty done:
    205  1.7     marty 	if (has_interrupt_map(phandle))
    206  1.7     marty 	    specifier -= 2;
    207  1.7     marty 	if (specifier && spec_length > 0)
    208  1.7     marty 		kmem_free(specifier, spec_length);
    209  1.7     marty 	return result;
    210  1.4     marty }
    211  1.1  jmcneill 
    212  1.4     marty /*
    213  1.4     marty  * Devices that have multiple interrupts, connected to two or more
    214  1.4     marty  * interrupt sources use an interrupt map rather than a simple
    215  1.4     marty  * interrupt parent to indicate which interrupt controller goes with
    216  1.4     marty  * which map.  The interrupt map is contained in the node describing
    217  1.4     marty  * the first level parent and contains one entry per interrupt:
    218  1.4     marty  *   index -- the index of the entry in the map
    219  1.4     marty  *   &parent -- pointer to the node containing the actual interrupt parent
    220  1.4     marty  *              for the specific interrupt
    221  1.4     marty  *  [specifier 0 - specifier N-1] The N (usually 2 or 3) 32 bit words
    222  1.4     marty  *              that make up the specifier.
    223  1.4     marty  *
    224  1.4     marty  * returns true if the device phandle has an interrupt-parent that
    225  1.4     marty  * contains an interrupt-map.
    226  1.4     marty  */
    227  1.4     marty static bool
    228  1.4     marty has_interrupt_map(int phandle)
    229  1.4     marty {
    230  1.4     marty 	int ic_phandle;
    231  1.4     marty 	of_getprop_uint32(phandle, "interrupt-parent", &ic_phandle);
    232  1.4     marty 	ic_phandle = fdtbus_get_phandle_from_native(ic_phandle);
    233  1.4     marty 	if (ic_phandle <= 0)
    234  1.4     marty 		return false;
    235  1.4     marty 	int len = OF_getproplen(ic_phandle, "interrupt-map");
    236  1.4     marty 	if (len > 0)
    237  1.4     marty 		return true;
    238  1.4     marty 	return false;
    239  1.4     marty }
    240  1.4     marty 
    241  1.4     marty /*
    242  1.4     marty  * Walk the specifier map and return a pointer to the map entry
    243  1.4     marty  * associated with pindex.  Return null if there is no entry.
    244  1.4     marty  *
    245  1.4     marty  * Because the length of the specifier depends on the interrupt
    246  1.4     marty  * controller, we need to repeatedly obtain interrupt-celss for
    247  1.4     marty  * the controller for the current index.
    248  1.4     marty  *
    249  1.4     marty  */
    250  1.4     marty static u_int *
    251  1.7     marty get_entry_from_map(int phandle, int pindex, u_int *spec_length)
    252  1.4     marty {
    253  1.4     marty 	int intr_cells;
    254  1.4     marty 	int intr_parent;
    255  1.6     marty 	u_int *result = NULL;
    256  1.4     marty 
    257  1.4     marty 	of_getprop_uint32(phandle, "#interrupt-cells", &intr_cells);
    258  1.4     marty 	of_getprop_uint32(phandle, "interrupt-parent", &intr_parent);
    259  1.4     marty 
    260  1.4     marty 	intr_parent = fdtbus_get_phandle_from_native(intr_parent);
    261  1.4     marty 	int len = OF_getproplen(intr_parent, "interrupt-map");
    262  1.4     marty 	if (len <= 0) {
    263  1.6     marty 		printf("%s: no interrupt-map.\n", __func__);
    264  1.4     marty 		return NULL;
    265  1.4     marty 	}
    266  1.4     marty 	uint resid = len;
    267  1.4     marty 	char *data = kmem_alloc(len, KM_SLEEP);
    268  1.4     marty 	len = OF_getprop(intr_parent, "interrupt-map", data, len);
    269  1.4     marty 	if (len <= 0) {
    270  1.6     marty 		printf("%s:  can't get property interrupt-map.\n", __func__);
    271  1.6     marty 		goto done;
    272  1.4     marty 	}
    273  1.4     marty 	u_int *p = (u_int *)data;
    274  1.4     marty 
    275  1.4     marty 	while (resid > 0) {
    276  1.4     marty 		u_int index = be32toh(p[0]);
    277  1.6     marty 		const u_int parent = fdtbus_get_phandle_from_native(be32toh(p[intr_cells]));
    278  1.6     marty 		u_int pintr_cells;
    279  1.6     marty 		of_getprop_uint32(parent, "#interrupt-cells", &pintr_cells);
    280  1.4     marty 		if (index == pindex) {
    281  1.7     marty 			result = kmem_alloc((pintr_cells + 2) * sizeof(u_int),
    282  1.7     marty 					    KM_SLEEP);
    283  1.7     marty 			*spec_length = (pintr_cells + 2) * sizeof (u_int);
    284  1.7     marty 			for (int i = 0; i < pintr_cells + 2; i++)
    285  1.7     marty 				result[i] =  p[i];
    286  1.6     marty 			goto done;
    287  1.4     marty 
    288  1.4     marty 		}
    289  1.4     marty 		/* Determine the length of the entry and skip that many
    290  1.4     marty 		 * 32 bit words
    291  1.4     marty 		 */
    292  1.4     marty 		const u_int reclen = (intr_cells + pintr_cells + 1);
    293  1.4     marty 		resid -= reclen * sizeof(u_int);
    294  1.4     marty 		p += reclen;
    295  1.4     marty 	}
    296  1.6     marty done:
    297  1.6     marty 	kmem_free(data, len);
    298  1.6     marty 	return result;
    299  1.4     marty }
    300  1.4     marty 
    301  1.4     marty 
    302  1.4     marty /*
    303  1.4     marty  * Devices that don't connect to more than one interrupt source use
    304  1.4     marty  * an array of specifiers.  Find the specifier that matches pindex
    305  1.4     marty  * and return a pointer to it.
    306  1.4     marty  *
    307  1.4     marty  */
    308  1.7     marty static u_int *get_specifier_by_index(int phandle, int pindex,
    309  1.7     marty 				     u_int *spec_length)
    310  1.4     marty {
    311  1.4     marty 	u_int *specifiers;
    312  1.7     marty 	u_int *specifier;
    313  1.4     marty 	int interrupt_parent, interrupt_cells, len;
    314  1.4     marty 
    315  1.6     marty 	interrupt_parent = fdtbus_get_interrupt_parent(phandle);
    316  1.6     marty 	if (interrupt_parent <= 0) {
    317  1.4     marty 		printf("%s: interrupt_parent sanity check failed\n", __func__);
    318  1.6     marty 		printf("%s: interrupt_parent = %d\n", __func__,
    319  1.6     marty 		       interrupt_parent);
    320  1.4     marty 		return NULL;
    321  1.4     marty 	}
    322  1.4     marty 
    323  1.4     marty 	len = OF_getprop(interrupt_parent, "#interrupt-cells",
    324  1.4     marty 			 &interrupt_cells,  sizeof(interrupt_cells));
    325  1.4     marty 	interrupt_cells = be32toh(interrupt_cells);
    326  1.4     marty 	if (len != sizeof(interrupt_cells) || interrupt_cells <= 0) {
    327  1.6     marty 		printf("%s: interrupt_cells sanity check failed\n", __func__);
    328  1.4     marty 		return NULL;
    329  1.4     marty 	}
    330  1.4     marty 
    331  1.4     marty 	len = OF_getproplen(phandle, "interrupts");
    332  1.4     marty 	if (len <= 0) {
    333  1.4     marty 		printf("%s: Couldn't get property interrupts\n", __func__);
    334  1.4     marty 		return NULL;
    335  1.4     marty 	}
    336  1.4     marty 
    337  1.4     marty 	const u_int nintr = len / interrupt_cells;
    338  1.4     marty 
    339  1.4     marty 	if (pindex >= nintr)
    340  1.4     marty 		return NULL;
    341  1.4     marty 
    342  1.4     marty 	specifiers = kmem_alloc(len, KM_SLEEP);
    343  1.4     marty 
    344  1.4     marty 	if (OF_getprop(phandle, "interrupts", specifiers, len) != len) {
    345  1.4     marty 		kmem_free(specifiers, len);
    346  1.4     marty 		return NULL;
    347  1.4     marty 	}
    348  1.7     marty 	specifier = kmem_alloc(interrupt_cells * sizeof(u_int), KM_SLEEP);
    349  1.7     marty 	*spec_length = interrupt_cells * sizeof(u_int);
    350  1.6     marty 	for (int i = 0; i < interrupt_cells; i++)
    351  1.8  jmcneill 		specifier[i] = specifiers[pindex * interrupt_cells + i];
    352  1.6     marty 	kmem_free(specifiers, len);
    353  1.6     marty 	return specifier;
    354  1.1  jmcneill }
    355