Home | History | Annotate | Line # | Download | only in fdt
fdt_intr.c revision 1.11.8.2
      1 /* $NetBSD: fdt_intr.c,v 1.11.8.2 2020/04/08 14:08:04 martin 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.11.8.2 2020/04/08 14:08:04 martin Exp $");
     31 
     32 #include <sys/param.h>
     33 #include <sys/atomic.h>
     34 #include <sys/bus.h>
     35 #include <sys/kmem.h>
     36 #include <sys/queue.h>
     37 #include <sys/mutex.h>
     38 #include <sys/condvar.h>
     39 
     40 #include <libfdt.h>
     41 #include <dev/fdt/fdtvar.h>
     42 #include <dev/fdt/fdt_private.h>
     43 
     44 struct fdtbus_interrupt_controller {
     45 	device_t ic_dev;
     46 	int ic_phandle;
     47 	const struct fdtbus_interrupt_controller_func *ic_funcs;
     48 
     49 	LIST_ENTRY(fdtbus_interrupt_controller) ic_next;
     50 };
     51 
     52 static LIST_HEAD(, fdtbus_interrupt_controller) fdtbus_interrupt_controllers =
     53     LIST_HEAD_INITIALIZER(fdtbus_interrupt_controllers);
     54 
     55 struct fdtbus_interrupt_cookie {
     56 	struct fdtbus_interrupt_controller *c_ic;
     57 	void *c_ih;
     58 
     59 	LIST_ENTRY(fdtbus_interrupt_cookie) c_next;
     60 	uint32_t c_refcnt;
     61 };
     62 
     63 static LIST_HEAD(, fdtbus_interrupt_cookie) fdtbus_interrupt_cookies =
     64     LIST_HEAD_INITIALIZER(fdtbus_interrupt_cookies);
     65 static kmutex_t fdtbus_interrupt_cookie_mutex;
     66 static kcondvar_t fdtbus_interrupt_cookie_wait;
     67 static bool fdtbus_interrupt_cookies_wanted;
     68 
     69 static const u_int *	get_specifier_by_index(int, int, int *);
     70 static const u_int *	get_specifier_from_map(int, const u_int *, int *);
     71 
     72 void
     73 fdtbus_intr_init(void)
     74 {
     75 
     76 	mutex_init(&fdtbus_interrupt_cookie_mutex, MUTEX_DEFAULT, IPL_HIGH);
     77 	cv_init(&fdtbus_interrupt_cookie_wait, "fdtintr");
     78 }
     79 
     80 /*
     81  * Find the interrupt controller for a given node. This function will either
     82  * return the phandle of the interrupt controller for this node, or the phandle
     83  * of a node containing an interrupt-map table that can be used to find the
     84  * real interrupt controller.
     85  */
     86 static int
     87 fdtbus_get_interrupt_parent(int phandle)
     88 {
     89 	int iparent = phandle;
     90 
     91 	do {
     92 		/*
     93 		 * If the node is an interrupt-controller, we are done. Note that
     94 		 * a node cannot be an interrupt-controller for itself, so we skip
     95 		 * the leaf node here.
     96 		 */
     97 		if (phandle != iparent && of_hasprop(iparent, "interrupt-controller"))
     98 			return iparent;
     99 
    100 		/*
    101 		 * If the node has an explicit interrupt-parent, follow the reference.
    102 		 */
    103 		if (of_hasprop(iparent, "interrupt-parent"))
    104 			return fdtbus_get_phandle(iparent, "interrupt-parent");
    105 
    106 		/*
    107 		 * If the node has an interrupt-map, use it. The caller is responsible
    108 		 * for parsing the interrupt-map and finding the real interrupt parent.
    109 		 */
    110 		if (phandle != iparent && of_hasprop(iparent, "interrupt-map"))
    111 			return iparent;
    112 
    113 		/*
    114 		 * Continue searching up the tree.
    115 		 */
    116 		iparent = OF_parent(iparent);
    117 	} while (iparent > 0);
    118 
    119 	return 0;
    120 }
    121 
    122 static struct fdtbus_interrupt_controller *
    123 fdtbus_get_interrupt_controller(int phandle)
    124 {
    125 	struct fdtbus_interrupt_controller * ic;
    126 	LIST_FOREACH(ic, &fdtbus_interrupt_controllers, ic_next) {
    127 		if (ic->ic_phandle == phandle)
    128 			return ic;
    129 	}
    130 	return NULL;
    131 }
    132 
    133 int
    134 fdtbus_register_interrupt_controller(device_t dev, int phandle,
    135     const struct fdtbus_interrupt_controller_func *funcs)
    136 {
    137 	struct fdtbus_interrupt_controller *ic;
    138 
    139 	ic = kmem_alloc(sizeof(*ic), KM_SLEEP);
    140 	ic->ic_dev = dev;
    141 	ic->ic_phandle = phandle;
    142 	ic->ic_funcs = funcs;
    143 
    144 	LIST_INSERT_HEAD(&fdtbus_interrupt_controllers, ic, ic_next);
    145 
    146 	return 0;
    147 }
    148 
    149 static struct fdtbus_interrupt_cookie *
    150 fdtbus_get_interrupt_cookie(void *cookie)
    151 {
    152 	struct fdtbus_interrupt_cookie *c;
    153 
    154 	mutex_enter(&fdtbus_interrupt_cookie_mutex);
    155 	LIST_FOREACH(c, &fdtbus_interrupt_cookies, c_next) {
    156 		if (c->c_ih == cookie) {
    157 			c->c_refcnt++;
    158 			KASSERT(c->c_refcnt > 0);
    159 			break;
    160 		}
    161 	}
    162 	mutex_exit(&fdtbus_interrupt_cookie_mutex);
    163 	return c;
    164 }
    165 
    166 static void
    167 fdtbus_put_interrupt_cookie(struct fdtbus_interrupt_cookie *c)
    168 {
    169 
    170 	mutex_enter(&fdtbus_interrupt_cookie_mutex);
    171 	KASSERT(c->c_refcnt > 0);
    172 	c->c_refcnt--;
    173 	if (fdtbus_interrupt_cookies_wanted) {
    174 		fdtbus_interrupt_cookies_wanted = false;
    175 		cv_signal(&fdtbus_interrupt_cookie_wait);
    176 	}
    177 	mutex_exit(&fdtbus_interrupt_cookie_mutex);
    178 }
    179 
    180 void *
    181 fdtbus_intr_establish(int phandle, u_int index, int ipl, int flags,
    182     int (*func)(void *), void *arg)
    183 {
    184 	const u_int *specifier;
    185 	int ihandle;
    186 
    187 	specifier = get_specifier_by_index(phandle, index, &ihandle);
    188 	if (specifier == NULL)
    189 		return NULL;
    190 
    191 	return fdtbus_intr_establish_raw(ihandle, specifier, ipl,
    192 	    flags, func, arg);
    193 }
    194 
    195 void *
    196 fdtbus_intr_establish_byname(int phandle, const char *name, int ipl,
    197     int flags, int (*func)(void *), void *arg)
    198 {
    199 	u_int index;
    200 	int err;
    201 
    202 	err = fdtbus_get_index(phandle, "interrupt-names", name, &index);
    203 	if (err != 0)
    204 		return NULL;
    205 
    206 	return fdtbus_intr_establish(phandle, index, ipl, flags, func, arg);
    207 }
    208 
    209 void *
    210 fdtbus_intr_establish_raw(int ihandle, const u_int *specifier, int ipl,
    211     int flags, int (*func)(void *), void *arg)
    212 {
    213 	struct fdtbus_interrupt_controller *ic;
    214 	struct fdtbus_interrupt_cookie *c;
    215 	void *ih;
    216 
    217 	ic = fdtbus_get_interrupt_controller(ihandle);
    218 	if (ic == NULL)
    219 		return NULL;
    220 
    221 	c = kmem_zalloc(sizeof(*c), KM_SLEEP);
    222 	c->c_ic = ic;
    223 	mutex_enter(&fdtbus_interrupt_cookie_mutex);
    224 	LIST_INSERT_HEAD(&fdtbus_interrupt_cookies, c, c_next);
    225 	mutex_exit(&fdtbus_interrupt_cookie_mutex);
    226 
    227 	/*
    228 	 * XXX This leaves a small window where the handler is registered
    229 	 * (and thus could be called) before the cookie on the list has a
    230 	 * valid lookup key (and thus can be found).  This will cause a
    231 	 * panic in fdt_intr_mask() if that is called from the handler before
    232 	 * this situation is resolved.  For now we just cross our fingers
    233 	 * and hope that the device won't actually interrupt until we return.
    234 	 */
    235 	ih = ic->ic_funcs->establish(ic->ic_dev, __UNCONST(specifier),
    236 	    ipl, flags, func, arg);
    237 	if (ih != NULL) {
    238 		atomic_store_release(&c->c_ih, ih);
    239 	} else {
    240 		mutex_enter(&fdtbus_interrupt_cookie_mutex);
    241 		LIST_REMOVE(c, c_next);
    242 		mutex_exit(&fdtbus_interrupt_cookie_mutex);
    243 		kmem_free(c, sizeof(*c));
    244 	}
    245 
    246 	return ih;
    247 }
    248 
    249 void
    250 fdtbus_intr_disestablish(int phandle, void *cookie)
    251 {
    252 	struct fdtbus_interrupt_cookie *c;
    253 
    254 	if ((c = fdtbus_get_interrupt_cookie(cookie)) == NULL) {
    255 		panic("%s: interrupt handle not valid", __func__);
    256 	}
    257 
    258 	const struct fdtbus_interrupt_controller *ic = c->c_ic;
    259 	ic->ic_funcs->disestablish(ic->ic_dev, cookie);
    260 
    261 	/*
    262 	 * Wait for any dangling references other than ours to
    263 	 * drain away.
    264 	 */
    265 	mutex_enter(&fdtbus_interrupt_cookie_mutex);
    266 	while (c->c_refcnt != 1) {
    267 		KASSERT(c->c_refcnt > 0);
    268 		fdtbus_interrupt_cookies_wanted = true;
    269 		cv_wait(&fdtbus_interrupt_cookie_wait,
    270 			&fdtbus_interrupt_cookie_mutex);
    271 	}
    272 	LIST_REMOVE(c, c_next);
    273 	mutex_exit(&fdtbus_interrupt_cookie_mutex);
    274 
    275 	kmem_free(c, sizeof(*c));
    276 }
    277 
    278 void
    279 fdtbus_intr_mask(int phandle, void *cookie)
    280 {
    281 	struct fdtbus_interrupt_cookie *c;
    282 
    283 	if ((c = fdtbus_get_interrupt_cookie(cookie)) == NULL) {
    284 		panic("%s: interrupt handle not valid", __func__);
    285 	}
    286 
    287 	struct fdtbus_interrupt_controller * const ic = c->c_ic;
    288 
    289 	if (ic->ic_funcs->mask == NULL) {
    290 		panic("%s: no 'mask' method for %s", __func__,
    291 		    device_xname(ic->ic_dev));
    292 	}
    293 
    294 	ic->ic_funcs->mask(ic->ic_dev, cookie);
    295 	fdtbus_put_interrupt_cookie(c);
    296 }
    297 
    298 void
    299 fdtbus_intr_unmask(int phandle, void *cookie)
    300 {
    301 	struct fdtbus_interrupt_cookie *c;
    302 
    303 	if ((c = fdtbus_get_interrupt_cookie(cookie)) == NULL) {
    304 		panic("%s: interrupt handle not valid", __func__);
    305 	}
    306 
    307 	struct fdtbus_interrupt_controller * const ic = c->c_ic;
    308 
    309 	if (ic->ic_funcs->unmask == NULL) {
    310 		panic("%s: no 'unmask' method for %s", __func__,
    311 		    device_xname(ic->ic_dev));
    312 	}
    313 
    314 	ic->ic_funcs->unmask(ic->ic_dev, cookie);
    315 	fdtbus_put_interrupt_cookie(c);
    316 }
    317 
    318 bool
    319 fdtbus_intr_str(int phandle, u_int index, char *buf, size_t buflen)
    320 {
    321 	const u_int *specifier;
    322 	int ihandle;
    323 
    324 	specifier = get_specifier_by_index(phandle, index, &ihandle);
    325 	if (specifier == NULL)
    326 		return false;
    327 
    328 	return fdtbus_intr_str_raw(ihandle, specifier, buf, buflen);
    329 }
    330 
    331 bool
    332 fdtbus_intr_str_raw(int ihandle, const u_int *specifier, char *buf, size_t buflen)
    333 {
    334 	struct fdtbus_interrupt_controller *ic;
    335 
    336 	ic = fdtbus_get_interrupt_controller(ihandle);
    337 	if (ic == NULL)
    338 		return false;
    339 
    340 	return ic->ic_funcs->intrstr(ic->ic_dev, __UNCONST(specifier), buf, buflen);
    341 }
    342 
    343 static int
    344 find_address_cells(int phandle)
    345 {
    346 	uint32_t cells;
    347 
    348 	if (of_getprop_uint32(phandle, "#address-cells", &cells) != 0)
    349 		cells = 2;
    350 
    351 	return cells;
    352 }
    353 
    354 static int
    355 find_interrupt_cells(int phandle)
    356 {
    357 	uint32_t cells;
    358 
    359 	while (phandle > 0) {
    360 		if (of_getprop_uint32(phandle, "#interrupt-cells", &cells) == 0)
    361 			return cells;
    362 		phandle = OF_parent(phandle);
    363 	}
    364 	return 0;
    365 }
    366 
    367 static const u_int *
    368 get_specifier_from_map(int phandle, const u_int *interrupt_spec, int *piphandle)
    369 {
    370 	const u_int *result = NULL;
    371 	int len, resid;
    372 
    373 	const u_int *data = fdtbus_get_prop(phandle, "interrupt-map", &len);
    374 	if (data == NULL || len <= 0)
    375 		return NULL;
    376 	resid = len;
    377 
    378 	/* child unit address: #address-cells prop of child bus node */
    379 	const int cua_cells = find_address_cells(phandle);
    380 	/* child interrupt specifier: #interrupt-cells of the nexus node */
    381 	const int cis_cells = find_interrupt_cells(phandle);
    382 
    383 	/* Offset (in cells) from map entry to child unit address specifier */
    384 	const u_int cua_off = 0;
    385 	/* Offset (in cells) from map entry to child interrupt specifier */
    386 	const u_int cis_off = cua_off + cua_cells;
    387 	/* Offset (in cells) from map entry to interrupt parent phandle */
    388 	const u_int ip_off = cis_off + cis_cells;
    389 	/* Offset (in cells) from map entry to parent unit specifier */
    390 	const u_int pus_off = ip_off + 1;
    391 
    392 	const u_int *p = (const u_int *)data;
    393 	while (resid > 0) {
    394 		/* Interrupt parent phandle */
    395 		const u_int iparent = fdtbus_get_phandle_from_native(be32toh(p[ip_off]));
    396 
    397 		/* parent unit specifier: #address-cells of the interrupt parent */
    398 		const u_int pus_cells = find_address_cells(iparent);
    399 		/* parent interrupt specifier: #interrupt-cells of the interrupt parent */
    400 		const u_int pis_cells = find_interrupt_cells(iparent);
    401 
    402 		/* Offset (in cells) from map entry to parent interrupt specifier */
    403 		const u_int pis_off = pus_off + pus_cells;
    404 
    405 #ifdef FDT_INTR_DEBUG
    406 		printf(" intr map (len %d):", pis_off + pis_cells);
    407 		for (int i = 0; i < pis_off + pis_cells; i++)
    408 			printf(" %08x", p[i]);
    409 		printf("\n");
    410 #endif
    411 
    412 		if (memcmp(&p[cis_off], interrupt_spec, cis_cells * 4) == 0) {
    413 #ifdef FDT_INTR_DEBUG
    414 			const int slen = pus_cells + pis_cells;
    415 			printf(" intr map match iparent %08x slen %d:", iparent, slen);
    416 			for (int i = 0; i < slen; i++)
    417 				printf(" %08x", p[pus_off + i]);
    418 			printf("\n");
    419 #endif
    420 			result = &p[pus_off];
    421 			*piphandle = iparent;
    422 			goto done;
    423 		}
    424 		/* Determine the length of the entry and skip that many
    425 		 * 32 bit words
    426 		 */
    427 		const u_int reclen = pis_off + pis_cells;
    428 		resid -= reclen * sizeof(u_int);
    429 		p += reclen;
    430 	}
    431 
    432 done:
    433 	return result;
    434 }
    435 
    436 static const u_int *
    437 get_specifier_by_index(int phandle, int pindex, int *piphandle)
    438 {
    439 	const u_int *node_specifier;
    440 	int interrupt_parent, interrupt_cells, len;
    441 
    442 	interrupt_parent = fdtbus_get_interrupt_parent(phandle);
    443 	if (interrupt_parent <= 0)
    444 		return NULL;
    445 
    446 	node_specifier = fdtbus_get_prop(phandle, "interrupts", &len);
    447 	if (node_specifier == NULL)
    448 		return NULL;
    449 
    450 	interrupt_cells = find_interrupt_cells(interrupt_parent);
    451 	if (interrupt_cells <= 0)
    452 		return NULL;
    453 
    454 	const u_int spec_length = len / 4;
    455 	const u_int nintr = spec_length / interrupt_cells;
    456 	if (pindex >= nintr)
    457 		return NULL;
    458 
    459 	node_specifier += (interrupt_cells * pindex);
    460 
    461 	if (of_hasprop(interrupt_parent, "interrupt-map"))
    462 		return get_specifier_from_map(interrupt_parent, node_specifier, piphandle);
    463 
    464 	*piphandle = interrupt_parent;
    465 
    466 	return node_specifier;
    467 }
    468