Home | History | Annotate | Line # | Download | only in fdt
fdt_intr.c revision 1.14
      1 /* $NetBSD: fdt_intr.c,v 1.14 2018/07/02 16:06:50 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.14 2018/07/02 16:06:50 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 static int
     60 fdtbus_get_interrupt_parent(int phandle)
     61 {
     62 	int iparent = phandle;
     63 
     64 	do {
     65 		if (of_hasprop(iparent, "interrupt-parent"))
     66 			return fdtbus_get_phandle(iparent, "interrupt-parent");
     67 		else if (of_hasprop(iparent, "interrupt-controller"))
     68 			return iparent;
     69 		else if (of_hasprop(iparent, "interrupt-map"))
     70 			return iparent;
     71 		iparent = OF_parent(iparent);
     72 	} while (iparent > 0);
     73 
     74 	return 0;
     75 }
     76 
     77 static struct fdtbus_interrupt_controller *
     78 fdtbus_get_interrupt_controller(int phandle)
     79 {
     80 	struct fdtbus_interrupt_controller * ic;
     81 	LIST_FOREACH(ic, &fdtbus_interrupt_controllers, ic_next) {
     82 		if (ic->ic_phandle == phandle)
     83 			return ic;
     84 	}
     85 	return NULL;
     86 }
     87 
     88 int
     89 fdtbus_register_interrupt_controller(device_t dev, int phandle,
     90     const struct fdtbus_interrupt_controller_func *funcs)
     91 {
     92 	struct fdtbus_interrupt_controller *ic;
     93 
     94 	ic = kmem_alloc(sizeof(*ic), KM_SLEEP);
     95 	ic->ic_dev = dev;
     96 	ic->ic_phandle = phandle;
     97 	ic->ic_funcs = funcs;
     98 
     99 	LIST_INSERT_HEAD(&fdtbus_interrupt_controllers, ic, ic_next);
    100 
    101 	return 0;
    102 }
    103 
    104 void *
    105 fdtbus_intr_establish(int phandle, u_int index, int ipl, int flags,
    106     int (*func)(void *), void *arg)
    107 {
    108 	struct fdtbus_interrupt_controller *ic;
    109 	struct fdtbus_interrupt_cookie *c = NULL;
    110 	u_int *specifier;
    111 	int ihandle;
    112 	void *ih;
    113 
    114 	specifier = get_specifier_by_index(phandle, index, &ihandle);
    115 	if (specifier == NULL)
    116 		return NULL;
    117 
    118 	ic = fdtbus_get_interrupt_controller(ihandle);
    119 	if (ic == NULL)
    120 		return NULL;
    121 
    122 	ih = ic->ic_funcs->establish(ic->ic_dev, specifier,
    123 	    ipl, flags, func, arg);
    124 	if (ih != NULL) {
    125 		c = kmem_alloc(sizeof(*c), KM_SLEEP);
    126 		c->c_ic = ic;
    127 		c->c_ih = ih;
    128 	}
    129 
    130 	return c;
    131 }
    132 
    133 void
    134 fdtbus_intr_disestablish(int phandle, void *cookie)
    135 {
    136 	struct fdtbus_interrupt_cookie *c = cookie;
    137 	struct fdtbus_interrupt_controller *ic = c->c_ic;
    138 	void *ih = c->c_ih;
    139 
    140 	return ic->ic_funcs->disestablish(ic->ic_dev, ih);
    141 }
    142 
    143 bool
    144 fdtbus_intr_str(int phandle, u_int index, char *buf, size_t buflen)
    145 {
    146 	struct fdtbus_interrupt_controller *ic;
    147 	u_int *specifier;
    148 	int ihandle;
    149 
    150 	specifier = get_specifier_by_index(phandle, index, &ihandle);
    151 
    152 	ic = fdtbus_get_interrupt_controller(ihandle);
    153 	if (ic == NULL)
    154 		return false;
    155 
    156 	return ic->ic_funcs->intrstr(ic->ic_dev, specifier, buf, buflen);
    157 }
    158 
    159 static int
    160 find_address_cells(int phandle)
    161 {
    162 	uint32_t cells;
    163 
    164 	if (of_getprop_uint32(phandle, "#address-cells", &cells) != 0)
    165 		cells = 2;
    166 
    167 	return cells;
    168 }
    169 
    170 static int
    171 find_interrupt_cells(int phandle)
    172 {
    173 	uint32_t cells;
    174 
    175 	while (phandle > 0) {
    176 		if (of_getprop_uint32(phandle, "#interrupt-cells", &cells) == 0)
    177 			return cells;
    178 		phandle = OF_parent(phandle);
    179 	}
    180 	return 0;
    181 }
    182 
    183 static u_int *
    184 get_specifier_from_map(int phandle, const u_int *interrupt_spec, int *piphandle)
    185 {
    186 	u_int *result = NULL;
    187 	int len, resid;
    188 
    189 	const u_int *data = fdtbus_get_prop(phandle, "interrupt-map", &len);
    190 	if (data == NULL || len <= 0)
    191 		return NULL;
    192 	resid = len;
    193 
    194 	/* child unit address: #address-cells prop of child bus node */
    195 	const int cua_cells = find_address_cells(phandle);
    196 	/* child interrupt specifier: #interrupt-cells of the nexus node */
    197 	const int cis_cells = find_interrupt_cells(phandle);
    198 
    199 	/* Offset (in cells) from map entry to child unit address specifier */
    200 	const u_int cua_off = 0;
    201 	/* Offset (in cells) from map entry to child interrupt specifier */
    202 	const u_int cis_off = cua_off + cua_cells;
    203 	/* Offset (in cells) from map entry to interrupt parent phandle */
    204 	const u_int ip_off = cis_off + cis_cells;
    205 	/* Offset (in cells) from map entry to parent unit specifier */
    206 	const u_int pus_off = ip_off + 1;
    207 
    208 	const u_int *p = (const u_int *)data;
    209 	while (resid > 0) {
    210 		/* Interrupt parent phandle */
    211 		const u_int iparent = fdtbus_get_phandle_from_native(be32toh(p[ip_off]));
    212 
    213 		/* parent unit specifier: #address-cells of the interrupt parent */
    214 		const u_int pus_cells = find_address_cells(iparent);
    215 		/* parent interrupt specifier: #interrupt-cells of the interrupt parent */
    216 		const u_int pis_cells = find_interrupt_cells(iparent);
    217 
    218 		/* Offset (in cells) from map entry to parent interrupt specifier */
    219 		const u_int pis_off = pus_off + pus_cells;
    220 
    221 #ifdef FDT_INTR_DEBUG
    222 		printf(" intr map (len %d):", pis_off + pis_cells);
    223 		for (int i = 0; i < pis_off + pis_cells; i++)
    224 			printf(" %08x", p[i]);
    225 		printf("\n");
    226 #endif
    227 
    228 		if (memcmp(&p[cis_off], interrupt_spec, cis_cells * 4) == 0) {
    229 			const int slen = pus_cells + pis_cells;
    230 #ifdef FDT_INTR_DEBUG
    231 			printf(" intr map match iparent %08x slen %d:", iparent, slen);
    232 			for (int i = 0; i < slen; i++)
    233 				printf(" %08x", p[pus_off + i]);
    234 			printf("\n");
    235 #endif
    236 			result = kmem_alloc(slen, KM_SLEEP);
    237 			memcpy(result, &p[pus_off], slen * 4);
    238 			*piphandle = iparent;
    239 			goto done;
    240 		}
    241 		/* Determine the length of the entry and skip that many
    242 		 * 32 bit words
    243 		 */
    244 		const u_int reclen = pis_off + pis_cells;
    245 		resid -= reclen * sizeof(u_int);
    246 		p += reclen;
    247 	}
    248 
    249 done:
    250 	return result;
    251 }
    252 
    253 static u_int *
    254 get_specifier_by_index(int phandle, int pindex, int *piphandle)
    255 {
    256 	const u_int *node_specifier;
    257 	u_int *specifier;
    258 	int interrupt_parent, interrupt_cells, len;
    259 
    260 	interrupt_parent = fdtbus_get_interrupt_parent(phandle);
    261 	if (interrupt_parent <= 0)
    262 		return NULL;
    263 
    264 	node_specifier = fdtbus_get_prop(phandle, "interrupts", &len);
    265 	if (node_specifier == NULL)
    266 		return NULL;
    267 
    268 	interrupt_cells = find_interrupt_cells(interrupt_parent);
    269 	if (interrupt_cells <= 0)
    270 		return NULL;
    271 
    272 	const u_int spec_length = len / 4;
    273 	const u_int nintr = spec_length / interrupt_cells;
    274 	if (pindex >= nintr)
    275 		return NULL;
    276 
    277 	node_specifier += (interrupt_cells * pindex);
    278 
    279 	if (of_hasprop(interrupt_parent, "interrupt-map"))
    280 		return get_specifier_from_map(interrupt_parent, node_specifier, piphandle);
    281 
    282 	specifier = kmem_alloc(interrupt_cells * sizeof(u_int), KM_SLEEP);
    283 	memcpy(specifier, node_specifier, interrupt_cells * 4);
    284 
    285 	*piphandle = interrupt_parent;
    286 
    287 	return specifier;
    288 }
    289