Home | History | Annotate | Line # | Download | only in fdt
fdt_intr.c revision 1.15
      1 /* $NetBSD: fdt_intr.c,v 1.15 2018/07/02 17:50:02 jmcneill Exp $ */
      2 
      3 /*-
      4  * Copyright (c) 2015-2018 Jared McNeill <jmcneill (at) invisible.ca>
      5  * All rights reserved.
      6  *
      7  * Redistribution and use in source and binary forms, with or without
      8  * modification, are permitted provided that the following conditions
      9  * are met:
     10  * 1. Redistributions of source code must retain the above copyright
     11  *    notice, this list of conditions and the following disclaimer.
     12  * 2. Redistributions in binary form must reproduce the above copyright
     13  *    notice, this list of conditions and the following disclaimer in the
     14  *    documentation and/or other materials provided with the distribution.
     15  *
     16  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
     17  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
     18  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
     19  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
     20  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
     21  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
     22  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
     23  * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
     24  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     25  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     26  * SUCH DAMAGE.
     27  */
     28 
     29 #include <sys/cdefs.h>
     30 __KERNEL_RCSID(0, "$NetBSD: fdt_intr.c,v 1.15 2018/07/02 17:50:02 jmcneill Exp $");
     31 
     32 #include <sys/param.h>
     33 #include <sys/bus.h>
     34 #include <sys/kmem.h>
     35 #include <sys/queue.h>
     36 
     37 #include <libfdt.h>
     38 #include <dev/fdt/fdtvar.h>
     39 
     40 struct fdtbus_interrupt_controller {
     41 	device_t ic_dev;
     42 	int ic_phandle;
     43 	const struct fdtbus_interrupt_controller_func *ic_funcs;
     44 
     45 	LIST_ENTRY(fdtbus_interrupt_controller) ic_next;
     46 };
     47 
     48 static LIST_HEAD(, fdtbus_interrupt_controller) fdtbus_interrupt_controllers =
     49     LIST_HEAD_INITIALIZER(fdtbus_interrupt_controllers);
     50 
     51 struct fdtbus_interrupt_cookie {
     52 	struct fdtbus_interrupt_controller *c_ic;
     53 	void *c_ih;
     54 };
     55 
     56 static u_int *	get_specifier_by_index(int, int, int *);
     57 static u_int *	get_specifier_from_map(int, const u_int *, int *);
     58 
     59 /*
     60  * Find the interrupt controller for a given node. This function will either
     61  * return the phandle of the interrupt controller for this node, or the phandle
     62  * of a node containing an interrupt-map table that can be used to find the
     63  * real interrupt controller.
     64  */
     65 static int
     66 fdtbus_get_interrupt_parent(int phandle)
     67 {
     68 	int iparent = phandle;
     69 
     70 	do {
     71 		/*
     72 		 * If the node is an interrupt-controller, we are done. Note that
     73 		 * a node cannot be an interrupt-controller for itself, so we skip
     74 		 * the leaf node here.
     75 		 */
     76 		if (phandle != iparent && of_hasprop(iparent, "interrupt-controller"))
     77 			return iparent;
     78 
     79 		/*
     80 		 * If the node has an explicit interrupt-parent, follow the reference.
     81 		 */
     82 		if (of_hasprop(iparent, "interrupt-parent"))
     83 			return fdtbus_get_phandle(iparent, "interrupt-parent");
     84 
     85 		/*
     86 		 * If the node has an interrupt-map, use it. The caller is responsible
     87 		 * for parsing the interrupt-map and finding the real interrupt parent.
     88 		 */
     89 		if (of_hasprop(iparent, "interrupt-map"))
     90 			return iparent;
     91 
     92 		/*
     93 		 * Continue searching up the tree.
     94 		 */
     95 		iparent = OF_parent(iparent);
     96 	} while (iparent > 0);
     97 
     98 	return 0;
     99 }
    100 
    101 static struct fdtbus_interrupt_controller *
    102 fdtbus_get_interrupt_controller(int phandle)
    103 {
    104 	struct fdtbus_interrupt_controller * ic;
    105 	LIST_FOREACH(ic, &fdtbus_interrupt_controllers, ic_next) {
    106 		if (ic->ic_phandle == phandle)
    107 			return ic;
    108 	}
    109 	return NULL;
    110 }
    111 
    112 int
    113 fdtbus_register_interrupt_controller(device_t dev, int phandle,
    114     const struct fdtbus_interrupt_controller_func *funcs)
    115 {
    116 	struct fdtbus_interrupt_controller *ic;
    117 
    118 	ic = kmem_alloc(sizeof(*ic), KM_SLEEP);
    119 	ic->ic_dev = dev;
    120 	ic->ic_phandle = phandle;
    121 	ic->ic_funcs = funcs;
    122 
    123 	LIST_INSERT_HEAD(&fdtbus_interrupt_controllers, ic, ic_next);
    124 
    125 	return 0;
    126 }
    127 
    128 void *
    129 fdtbus_intr_establish(int phandle, u_int index, int ipl, int flags,
    130     int (*func)(void *), void *arg)
    131 {
    132 	struct fdtbus_interrupt_controller *ic;
    133 	struct fdtbus_interrupt_cookie *c = NULL;
    134 	u_int *specifier;
    135 	int ihandle;
    136 	void *ih;
    137 
    138 	specifier = get_specifier_by_index(phandle, index, &ihandle);
    139 	if (specifier == NULL)
    140 		return NULL;
    141 
    142 	ic = fdtbus_get_interrupt_controller(ihandle);
    143 	if (ic == NULL)
    144 		return NULL;
    145 
    146 	ih = ic->ic_funcs->establish(ic->ic_dev, specifier,
    147 	    ipl, flags, func, arg);
    148 	if (ih != NULL) {
    149 		c = kmem_alloc(sizeof(*c), KM_SLEEP);
    150 		c->c_ic = ic;
    151 		c->c_ih = ih;
    152 	}
    153 
    154 	return c;
    155 }
    156 
    157 void
    158 fdtbus_intr_disestablish(int phandle, void *cookie)
    159 {
    160 	struct fdtbus_interrupt_cookie *c = cookie;
    161 	struct fdtbus_interrupt_controller *ic = c->c_ic;
    162 	void *ih = c->c_ih;
    163 
    164 	return ic->ic_funcs->disestablish(ic->ic_dev, ih);
    165 }
    166 
    167 bool
    168 fdtbus_intr_str(int phandle, u_int index, char *buf, size_t buflen)
    169 {
    170 	struct fdtbus_interrupt_controller *ic;
    171 	u_int *specifier;
    172 	int ihandle;
    173 
    174 	specifier = get_specifier_by_index(phandle, index, &ihandle);
    175 
    176 	ic = fdtbus_get_interrupt_controller(ihandle);
    177 	if (ic == NULL)
    178 		return false;
    179 
    180 	return ic->ic_funcs->intrstr(ic->ic_dev, specifier, buf, buflen);
    181 }
    182 
    183 static int
    184 find_address_cells(int phandle)
    185 {
    186 	uint32_t cells;
    187 
    188 	if (of_getprop_uint32(phandle, "#address-cells", &cells) != 0)
    189 		cells = 2;
    190 
    191 	return cells;
    192 }
    193 
    194 static int
    195 find_interrupt_cells(int phandle)
    196 {
    197 	uint32_t cells;
    198 
    199 	while (phandle > 0) {
    200 		if (of_getprop_uint32(phandle, "#interrupt-cells", &cells) == 0)
    201 			return cells;
    202 		phandle = OF_parent(phandle);
    203 	}
    204 	return 0;
    205 }
    206 
    207 static u_int *
    208 get_specifier_from_map(int phandle, const u_int *interrupt_spec, int *piphandle)
    209 {
    210 	u_int *result = NULL;
    211 	int len, resid;
    212 
    213 	const u_int *data = fdtbus_get_prop(phandle, "interrupt-map", &len);
    214 	if (data == NULL || len <= 0)
    215 		return NULL;
    216 	resid = len;
    217 
    218 	/* child unit address: #address-cells prop of child bus node */
    219 	const int cua_cells = find_address_cells(phandle);
    220 	/* child interrupt specifier: #interrupt-cells of the nexus node */
    221 	const int cis_cells = find_interrupt_cells(phandle);
    222 
    223 	/* Offset (in cells) from map entry to child unit address specifier */
    224 	const u_int cua_off = 0;
    225 	/* Offset (in cells) from map entry to child interrupt specifier */
    226 	const u_int cis_off = cua_off + cua_cells;
    227 	/* Offset (in cells) from map entry to interrupt parent phandle */
    228 	const u_int ip_off = cis_off + cis_cells;
    229 	/* Offset (in cells) from map entry to parent unit specifier */
    230 	const u_int pus_off = ip_off + 1;
    231 
    232 	const u_int *p = (const u_int *)data;
    233 	while (resid > 0) {
    234 		/* Interrupt parent phandle */
    235 		const u_int iparent = fdtbus_get_phandle_from_native(be32toh(p[ip_off]));
    236 
    237 		/* parent unit specifier: #address-cells of the interrupt parent */
    238 		const u_int pus_cells = find_address_cells(iparent);
    239 		/* parent interrupt specifier: #interrupt-cells of the interrupt parent */
    240 		const u_int pis_cells = find_interrupt_cells(iparent);
    241 
    242 		/* Offset (in cells) from map entry to parent interrupt specifier */
    243 		const u_int pis_off = pus_off + pus_cells;
    244 
    245 #ifdef FDT_INTR_DEBUG
    246 		printf(" intr map (len %d):", pis_off + pis_cells);
    247 		for (int i = 0; i < pis_off + pis_cells; i++)
    248 			printf(" %08x", p[i]);
    249 		printf("\n");
    250 #endif
    251 
    252 		if (memcmp(&p[cis_off], interrupt_spec, cis_cells * 4) == 0) {
    253 			const int slen = pus_cells + pis_cells;
    254 #ifdef FDT_INTR_DEBUG
    255 			printf(" intr map match iparent %08x slen %d:", iparent, slen);
    256 			for (int i = 0; i < slen; i++)
    257 				printf(" %08x", p[pus_off + i]);
    258 			printf("\n");
    259 #endif
    260 			result = kmem_alloc(slen, KM_SLEEP);
    261 			memcpy(result, &p[pus_off], slen * 4);
    262 			*piphandle = iparent;
    263 			goto done;
    264 		}
    265 		/* Determine the length of the entry and skip that many
    266 		 * 32 bit words
    267 		 */
    268 		const u_int reclen = pis_off + pis_cells;
    269 		resid -= reclen * sizeof(u_int);
    270 		p += reclen;
    271 	}
    272 
    273 done:
    274 	return result;
    275 }
    276 
    277 static u_int *
    278 get_specifier_by_index(int phandle, int pindex, int *piphandle)
    279 {
    280 	const u_int *node_specifier;
    281 	u_int *specifier;
    282 	int interrupt_parent, interrupt_cells, len;
    283 
    284 	interrupt_parent = fdtbus_get_interrupt_parent(phandle);
    285 	if (interrupt_parent <= 0)
    286 		return NULL;
    287 
    288 	node_specifier = fdtbus_get_prop(phandle, "interrupts", &len);
    289 	if (node_specifier == NULL)
    290 		return NULL;
    291 
    292 	interrupt_cells = find_interrupt_cells(interrupt_parent);
    293 	if (interrupt_cells <= 0)
    294 		return NULL;
    295 
    296 	const u_int spec_length = len / 4;
    297 	const u_int nintr = spec_length / interrupt_cells;
    298 	if (pindex >= nintr)
    299 		return NULL;
    300 
    301 	node_specifier += (interrupt_cells * pindex);
    302 
    303 	if (of_hasprop(interrupt_parent, "interrupt-map"))
    304 		return get_specifier_from_map(interrupt_parent, node_specifier, piphandle);
    305 
    306 	specifier = kmem_alloc(interrupt_cells * sizeof(u_int), KM_SLEEP);
    307 	memcpy(specifier, node_specifier, interrupt_cells * 4);
    308 
    309 	*piphandle = interrupt_parent;
    310 
    311 	return specifier;
    312 }
    313