Home | History | Annotate | Line # | Download | only in fdt
fdt_intr.c revision 1.3.2.5
      1 /* $NetBSD: fdt_intr.c,v 1.3.2.5 2017/08/28 17:52:02 skrll Exp $ */
      2 
      3 /*-
      4  * Copyright (c) 2015 Jared D. 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.3.2.5 2017/08/28 17:52:02 skrll Exp $");
     31 
     32 #include <sys/param.h>
     33 #include <sys/bus.h>
     34 #include <sys/kmem.h>
     35 
     36 #include <libfdt.h>
     37 #include <dev/fdt/fdtvar.h>
     38 
     39 struct fdtbus_interrupt_controller {
     40 	device_t ic_dev;
     41 	int ic_phandle;
     42 	const struct fdtbus_interrupt_controller_func *ic_funcs;
     43 
     44 	struct fdtbus_interrupt_controller *ic_next;
     45 };
     46 
     47 struct fdtbus_interrupt_cookie {
     48 	struct fdtbus_interrupt_controller *c_ic;
     49 	void *c_ih;
     50 };
     51 
     52 static struct fdtbus_interrupt_controller *fdtbus_ic = NULL;
     53 
     54 static bool	has_interrupt_map(int);
     55 static u_int *	get_specifier_by_index(int, int, int *);
     56 static u_int *	get_specifier_from_map(int, int, int *);
     57 
     58 static int
     59 fdtbus_get_interrupt_parent(int phandle)
     60 {
     61 	u_int interrupt_parent;
     62 
     63 	while (phandle >= 0) {
     64 		if (of_getprop_uint32(phandle, "interrupt-parent",
     65 		    &interrupt_parent) == 0) {
     66 			break;
     67 		}
     68 		if (phandle == 0) {
     69 			return -1;
     70 		}
     71 		phandle = OF_parent(phandle);
     72 	}
     73 	if (phandle < 0) {
     74 		return -1;
     75 	}
     76 
     77 	const void *data = fdtbus_get_data();
     78 	const int off = fdt_node_offset_by_phandle(data, interrupt_parent);
     79 	if (off < 0) {
     80 		return -1;
     81 	}
     82 
     83 	return fdtbus_offset2phandle(off);
     84 }
     85 
     86 static struct fdtbus_interrupt_controller *
     87 fdtbus_get_interrupt_controller(int phandle)
     88 {
     89 	struct fdtbus_interrupt_controller * ic;
     90 	for (ic = fdtbus_ic; ic; ic = ic->ic_next) {
     91 		if (ic->ic_phandle == phandle) {
     92 			return ic;
     93 		}
     94 	}
     95 	return NULL;
     96 }
     97 
     98 int
     99 fdtbus_register_interrupt_controller(device_t dev, int phandle,
    100     const struct fdtbus_interrupt_controller_func *funcs)
    101 {
    102 	struct fdtbus_interrupt_controller *ic;
    103 
    104 	ic = kmem_alloc(sizeof(*ic), KM_SLEEP);
    105 	ic->ic_dev = dev;
    106 	ic->ic_phandle = phandle;
    107 	ic->ic_funcs = funcs;
    108 
    109 	ic->ic_next = fdtbus_ic;
    110 	fdtbus_ic = ic;
    111 
    112 	return 0;
    113 }
    114 
    115 void *
    116 fdtbus_intr_establish(int phandle, u_int index, int ipl, int flags,
    117     int (*func)(void *), void *arg)
    118 {
    119 	struct fdtbus_interrupt_controller *ic;
    120 	struct fdtbus_interrupt_cookie *c = NULL;
    121 	u_int *specifier;
    122 	int ihandle;
    123 	void *ih;
    124 
    125 	specifier = get_specifier_by_index(phandle, index, &ihandle);
    126 	if (specifier == NULL)
    127 		return NULL;
    128 
    129 	ic = fdtbus_get_interrupt_controller(ihandle);
    130 	if (ic == NULL)
    131 		return NULL;
    132 
    133 	ih = ic->ic_funcs->establish(ic->ic_dev, specifier,
    134 	    ipl, flags, func, arg);
    135 	if (ih != NULL) {
    136 		c = kmem_alloc(sizeof(*c), KM_SLEEP);
    137 		c->c_ic = ic;
    138 		c->c_ih = ih;
    139 	}
    140 
    141 	return c;
    142 }
    143 
    144 void
    145 fdtbus_intr_disestablish(int phandle, void *cookie)
    146 {
    147 	struct fdtbus_interrupt_cookie *c = cookie;
    148 	struct fdtbus_interrupt_controller *ic = c->c_ic;
    149 	void *ih = c->c_ih;
    150 
    151 	return ic->ic_funcs->disestablish(ic->ic_dev, ih);
    152 }
    153 
    154 bool
    155 fdtbus_intr_str(int phandle, u_int index, char *buf, size_t buflen)
    156 {
    157 	struct fdtbus_interrupt_controller *ic;
    158 	u_int *specifier;
    159 	int ihandle;
    160 
    161 	specifier = get_specifier_by_index(phandle, index, &ihandle);
    162 
    163 	ic = fdtbus_get_interrupt_controller(ihandle);
    164 	if (ic == NULL)
    165 		return false;
    166 
    167 	return ic->ic_funcs->intrstr(ic->ic_dev, specifier, buf, buflen);
    168 }
    169 
    170 static int
    171 find_interrupt_map(int phandle)
    172 {
    173 	while (phandle > 0) {
    174 		if (of_hasprop(phandle, "interrupt-map"))
    175 			return phandle;
    176 		phandle = OF_parent(phandle);
    177 	}
    178 	return -1;
    179 }
    180 
    181 static int
    182 find_address_cells(int phandle)
    183 {
    184 	uint32_t cells;
    185 
    186 	if (of_getprop_uint32(phandle, "#address-cells", &cells) != 0)
    187 		cells = 2;
    188 
    189 	return cells;
    190 }
    191 
    192 static int
    193 find_interrupt_cells(int phandle)
    194 {
    195 	uint32_t cells;
    196 
    197 	while (phandle > 0) {
    198 		if (of_getprop_uint32(phandle, "#interrupt-cells", &cells) == 0)
    199 			return cells;
    200 		phandle = OF_parent(phandle);
    201 	}
    202 	return 0;
    203 }
    204 
    205 static bool
    206 has_interrupt_map(int phandle)
    207 {
    208 	return find_interrupt_map(OF_parent(phandle)) != -1;
    209 }
    210 
    211 static u_int *
    212 get_specifier_from_map(int phandle, int pindex, int *piphandle)
    213 {
    214 	const u_int *node_specifier = NULL;
    215 	u_int *result = NULL;
    216 	int len, resid;
    217 
    218 	const u_int interrupt_cells = find_interrupt_cells(phandle);
    219 	if (interrupt_cells < 1)
    220 		return NULL;
    221 
    222 	node_specifier = fdt_getprop(fdtbus_get_data(), fdtbus_phandle2offset(phandle),
    223 	    "interrupts", &len);
    224 	if (node_specifier == NULL)
    225 		return NULL;
    226 
    227 	const u_int spec_length = len / 4;
    228 	const u_int nintr = spec_length / interrupt_cells;
    229 	if (pindex >= nintr)
    230 		return NULL;
    231 
    232 	node_specifier += (interrupt_cells * pindex);
    233 
    234 	const int nexus_phandle = find_interrupt_map(OF_parent(phandle));
    235 
    236 	const u_int *data = fdt_getprop(fdtbus_get_data(), fdtbus_phandle2offset(nexus_phandle),
    237 	    "interrupt-map", &len);
    238 	if (data == NULL || len <= 0) {
    239 		printf("%s: can't get property interrupt-map.\n", __func__);
    240 		return NULL;
    241 	}
    242 	resid = len;
    243 
    244 	/* child unit address: #address-cells prop of child bus node */
    245 	const int cua_cells = find_address_cells(nexus_phandle);
    246 	/* child interrupt specifier: #interrupt-cells of the nexus node */
    247 	const int cis_cells = find_interrupt_cells(nexus_phandle);
    248 
    249 	/* Offset (in cells) from map entry to child unit address specifier */
    250 	const u_int cua_off = 0;
    251 	/* Offset (in cells) from map entry to child interrupt specifier */
    252 	const u_int cis_off = cua_off + cua_cells;
    253 	/* Offset (in cells) from map entry to interrupt parent phandle */
    254 	const u_int ip_off = cis_off + cis_cells;
    255 	/* Offset (in cells) from map entry to parent unit specifier */
    256 	const u_int pus_off = ip_off + 1;
    257 
    258 #ifdef FDT_INTR_DEBUG
    259 	printf("%s: phandle=%s nexus_phandle=%s\n", __func__,
    260 	    fdt_get_name(fdtbus_get_data(), fdtbus_phandle2offset(phandle), NULL),
    261 	    fdt_get_name(fdtbus_get_data(), fdtbus_phandle2offset(nexus_phandle), NULL));
    262 	printf("cua_cells: %d, cis_cells: %d, ip_off = %d\n", cua_cells, cis_cells, ip_off);
    263 	printf("searching for interrupt in map (data %p, len %d):", data, len);
    264 	for (int i = 0; i < interrupt_cells; i++)
    265 		printf(" %08x", node_specifier[i]);
    266 	printf("\n");
    267 #endif
    268 
    269 	const u_int *p = (const u_int *)data;
    270 	while (resid > 0) {
    271 		/* Interrupt parent phandle */
    272 		const u_int iparent = fdtbus_get_phandle_from_native(be32toh(p[ip_off]));
    273 
    274 		/* parent unit specifier: #address-cells of the interrupt parent */
    275 		const u_int pus_cells = find_address_cells(iparent);
    276 		/* parent interrupt specifier: #interrupt-cells of the interrupt parent */
    277 		const u_int pis_cells = find_interrupt_cells(iparent);
    278 
    279 		/* Offset (in cells) from map entry to parent interrupt specifier */
    280 		const u_int pis_off = pus_off + pus_cells;
    281 
    282 #ifdef FDT_INTR_DEBUG
    283 		printf(" intr map (len %d):", pis_off + pis_cells);
    284 		for (int i = 0; i < pis_off + pis_cells; i++)
    285 			printf(" %08x", p[i]);
    286 		printf("\n");
    287 #endif
    288 
    289 		if (cis_cells == interrupt_cells && memcmp(&p[cis_off], node_specifier, interrupt_cells * 4) == 0) {
    290 			const int slen = pus_cells + pis_cells;
    291 #ifdef FDT_INTR_DEBUG
    292 			printf(" intr map match iparent %08x slen %d:", iparent, slen);
    293 			for (int i = 0; i < slen; i++)
    294 				printf(" %08x", p[pus_off + i]);
    295 			printf("\n");
    296 #endif
    297 			result = kmem_alloc(slen, KM_SLEEP);
    298 			memcpy(result, &p[pus_off], slen * 4);
    299 			*piphandle = iparent;
    300 			goto done;
    301 		}
    302 		/* Determine the length of the entry and skip that many
    303 		 * 32 bit words
    304 		 */
    305 		const u_int reclen = pis_off + pis_cells;
    306 		resid -= reclen * sizeof(u_int);
    307 		p += reclen;
    308 	}
    309 
    310 done:
    311 	return result;
    312 }
    313 
    314 static u_int *
    315 get_specifier_by_index(int phandle, int pindex, int *piphandle)
    316 {
    317 	const u_int *node_specifier;
    318 	u_int *specifier;
    319 	int interrupt_parent, interrupt_cells, len;
    320 
    321 	if (has_interrupt_map(phandle))
    322 		return get_specifier_from_map(phandle, pindex, piphandle);
    323 
    324 	interrupt_parent = fdtbus_get_interrupt_parent(phandle);
    325 	if (interrupt_parent <= 0)
    326 		return NULL;
    327 
    328 	interrupt_cells = find_interrupt_cells(interrupt_parent);
    329 	if (interrupt_cells <= 0)
    330 		return NULL;
    331 
    332 	node_specifier = fdt_getprop(fdtbus_get_data(), fdtbus_phandle2offset(phandle),
    333 	    "interrupts", &len);
    334 	if (node_specifier == NULL)
    335 		return NULL;
    336 
    337 	const u_int spec_length = len / 4;
    338 	const u_int nintr = spec_length / interrupt_cells;
    339 	if (pindex >= nintr)
    340 		return NULL;
    341 
    342 	node_specifier += (interrupt_cells * pindex);
    343 
    344 	specifier = kmem_alloc(interrupt_cells * sizeof(u_int), KM_SLEEP);
    345 	memcpy(specifier, node_specifier, interrupt_cells * 4);
    346 
    347 	*piphandle = interrupt_parent;
    348 
    349 	return specifier;
    350 }
    351