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