2020-03-08 11:25:36 +03:00
|
|
|
/* $NetBSD: fdt_intr.c,v 1.26 2020/03/08 08:25:36 skrll Exp $ */
|
2015-12-16 15:03:44 +03:00
|
|
|
|
|
|
|
/*-
|
2018-07-02 15:17:05 +03:00
|
|
|
* Copyright (c) 2015-2018 Jared McNeill <jmcneill@invisible.ca>
|
2015-12-16 15:03:44 +03:00
|
|
|
* All rights reserved.
|
|
|
|
*
|
|
|
|
* Redistribution and use in source and binary forms, with or without
|
|
|
|
* modification, are permitted provided that the following conditions
|
|
|
|
* are met:
|
|
|
|
* 1. Redistributions of source code must retain the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer.
|
|
|
|
* 2. Redistributions in binary form must reproduce the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer in the
|
|
|
|
* documentation and/or other materials provided with the distribution.
|
|
|
|
*
|
|
|
|
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
|
|
|
|
* IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
|
|
|
|
* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
|
|
|
|
* IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
|
|
|
|
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
|
|
|
|
* BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
|
|
|
|
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
|
|
|
|
* AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
|
|
|
|
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
|
|
|
|
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
|
|
|
|
* SUCH DAMAGE.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <sys/cdefs.h>
|
2020-03-08 11:25:36 +03:00
|
|
|
__KERNEL_RCSID(0, "$NetBSD: fdt_intr.c,v 1.26 2020/03/08 08:25:36 skrll Exp $");
|
2015-12-16 15:03:44 +03:00
|
|
|
|
|
|
|
#include <sys/param.h>
|
2020-02-16 23:28:18 +03:00
|
|
|
#include <sys/atomic.h>
|
2015-12-16 15:03:44 +03:00
|
|
|
#include <sys/bus.h>
|
|
|
|
#include <sys/kmem.h>
|
2018-06-30 23:34:43 +03:00
|
|
|
#include <sys/queue.h>
|
2020-02-16 23:28:18 +03:00
|
|
|
#include <sys/mutex.h>
|
|
|
|
#include <sys/condvar.h>
|
2015-12-16 15:03:44 +03:00
|
|
|
|
|
|
|
#include <libfdt.h>
|
|
|
|
#include <dev/fdt/fdtvar.h>
|
2020-02-16 23:28:18 +03:00
|
|
|
#include <dev/fdt/fdt_private.h>
|
2015-12-16 15:03:44 +03:00
|
|
|
|
|
|
|
struct fdtbus_interrupt_controller {
|
|
|
|
device_t ic_dev;
|
|
|
|
int ic_phandle;
|
|
|
|
const struct fdtbus_interrupt_controller_func *ic_funcs;
|
|
|
|
|
2018-06-30 23:34:43 +03:00
|
|
|
LIST_ENTRY(fdtbus_interrupt_controller) ic_next;
|
2015-12-16 15:03:44 +03:00
|
|
|
};
|
|
|
|
|
2018-06-30 23:34:43 +03:00
|
|
|
static LIST_HEAD(, fdtbus_interrupt_controller) fdtbus_interrupt_controllers =
|
|
|
|
LIST_HEAD_INITIALIZER(fdtbus_interrupt_controllers);
|
|
|
|
|
2017-06-11 15:56:36 +03:00
|
|
|
struct fdtbus_interrupt_cookie {
|
|
|
|
struct fdtbus_interrupt_controller *c_ic;
|
|
|
|
void *c_ih;
|
2018-07-15 16:24:05 +03:00
|
|
|
|
|
|
|
LIST_ENTRY(fdtbus_interrupt_cookie) c_next;
|
2020-02-16 23:28:18 +03:00
|
|
|
uint32_t c_refcnt;
|
2017-06-11 15:56:36 +03:00
|
|
|
};
|
|
|
|
|
2018-07-15 16:24:05 +03:00
|
|
|
static LIST_HEAD(, fdtbus_interrupt_cookie) fdtbus_interrupt_cookies =
|
|
|
|
LIST_HEAD_INITIALIZER(fdtbus_interrupt_cookies);
|
2020-02-16 23:28:18 +03:00
|
|
|
static kmutex_t fdtbus_interrupt_cookie_mutex;
|
|
|
|
static kcondvar_t fdtbus_interrupt_cookie_wait;
|
|
|
|
static bool fdtbus_interrupt_cookies_wanted;
|
2018-07-15 16:24:05 +03:00
|
|
|
|
2018-07-15 19:59:16 +03:00
|
|
|
static const u_int * get_specifier_by_index(int, int, int *);
|
|
|
|
static const u_int * get_specifier_from_map(int, const u_int *, int *);
|
2016-01-06 00:53:48 +03:00
|
|
|
|
2020-02-16 23:28:18 +03:00
|
|
|
void
|
|
|
|
fdtbus_intr_init(void)
|
|
|
|
{
|
|
|
|
|
2020-03-08 11:25:36 +03:00
|
|
|
mutex_init(&fdtbus_interrupt_cookie_mutex, MUTEX_DEFAULT, IPL_HIGH);
|
2020-02-16 23:28:18 +03:00
|
|
|
cv_init(&fdtbus_interrupt_cookie_wait, "fdtintr");
|
|
|
|
}
|
|
|
|
|
2018-07-02 20:50:02 +03:00
|
|
|
/*
|
|
|
|
* Find the interrupt controller for a given node. This function will either
|
|
|
|
* return the phandle of the interrupt controller for this node, or the phandle
|
|
|
|
* of a node containing an interrupt-map table that can be used to find the
|
|
|
|
* real interrupt controller.
|
|
|
|
*/
|
2015-12-16 15:03:44 +03:00
|
|
|
static int
|
|
|
|
fdtbus_get_interrupt_parent(int phandle)
|
|
|
|
{
|
2018-07-02 19:06:50 +03:00
|
|
|
int iparent = phandle;
|
2015-12-16 15:03:44 +03:00
|
|
|
|
2018-07-02 19:06:50 +03:00
|
|
|
do {
|
2018-07-02 20:50:02 +03:00
|
|
|
/*
|
|
|
|
* If the node is an interrupt-controller, we are done. Note that
|
|
|
|
* a node cannot be an interrupt-controller for itself, so we skip
|
|
|
|
* the leaf node here.
|
|
|
|
*/
|
|
|
|
if (phandle != iparent && of_hasprop(iparent, "interrupt-controller"))
|
|
|
|
return iparent;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* If the node has an explicit interrupt-parent, follow the reference.
|
|
|
|
*/
|
2018-07-02 19:06:50 +03:00
|
|
|
if (of_hasprop(iparent, "interrupt-parent"))
|
|
|
|
return fdtbus_get_phandle(iparent, "interrupt-parent");
|
2018-07-02 20:50:02 +03:00
|
|
|
|
|
|
|
/*
|
|
|
|
* If the node has an interrupt-map, use it. The caller is responsible
|
|
|
|
* for parsing the interrupt-map and finding the real interrupt parent.
|
|
|
|
*/
|
2018-11-13 04:06:37 +03:00
|
|
|
if (phandle != iparent && of_hasprop(iparent, "interrupt-map"))
|
2018-07-02 19:06:50 +03:00
|
|
|
return iparent;
|
2018-07-02 20:50:02 +03:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Continue searching up the tree.
|
|
|
|
*/
|
2018-07-02 19:06:50 +03:00
|
|
|
iparent = OF_parent(iparent);
|
|
|
|
} while (iparent > 0);
|
2015-12-16 15:03:44 +03:00
|
|
|
|
2018-07-02 19:06:50 +03:00
|
|
|
return 0;
|
2015-12-16 15:03:44 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
static struct fdtbus_interrupt_controller *
|
2017-06-11 15:56:36 +03:00
|
|
|
fdtbus_get_interrupt_controller(int phandle)
|
2015-12-16 15:03:44 +03:00
|
|
|
{
|
2016-01-06 00:53:48 +03:00
|
|
|
struct fdtbus_interrupt_controller * ic;
|
2018-06-30 23:34:43 +03:00
|
|
|
LIST_FOREACH(ic, &fdtbus_interrupt_controllers, ic_next) {
|
|
|
|
if (ic->ic_phandle == phandle)
|
2016-01-06 00:53:48 +03:00
|
|
|
return ic;
|
|
|
|
}
|
|
|
|
return NULL;
|
|
|
|
}
|
2015-12-16 15:03:44 +03:00
|
|
|
|
|
|
|
int
|
|
|
|
fdtbus_register_interrupt_controller(device_t dev, int phandle,
|
|
|
|
const struct fdtbus_interrupt_controller_func *funcs)
|
|
|
|
{
|
|
|
|
struct fdtbus_interrupt_controller *ic;
|
|
|
|
|
|
|
|
ic = kmem_alloc(sizeof(*ic), KM_SLEEP);
|
|
|
|
ic->ic_dev = dev;
|
|
|
|
ic->ic_phandle = phandle;
|
|
|
|
ic->ic_funcs = funcs;
|
|
|
|
|
2018-06-30 23:34:43 +03:00
|
|
|
LIST_INSERT_HEAD(&fdtbus_interrupt_controllers, ic, ic_next);
|
2015-12-16 15:03:44 +03:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2020-02-16 23:28:18 +03:00
|
|
|
static struct fdtbus_interrupt_cookie *
|
|
|
|
fdtbus_get_interrupt_cookie(void *cookie)
|
|
|
|
{
|
|
|
|
struct fdtbus_interrupt_cookie *c;
|
|
|
|
|
|
|
|
mutex_enter(&fdtbus_interrupt_cookie_mutex);
|
|
|
|
LIST_FOREACH(c, &fdtbus_interrupt_cookies, c_next) {
|
|
|
|
if (c->c_ih == cookie) {
|
|
|
|
c->c_refcnt++;
|
|
|
|
KASSERT(c->c_refcnt > 0);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
mutex_exit(&fdtbus_interrupt_cookie_mutex);
|
|
|
|
return c;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
fdtbus_put_interrupt_cookie(struct fdtbus_interrupt_cookie *c)
|
|
|
|
{
|
|
|
|
|
|
|
|
mutex_enter(&fdtbus_interrupt_cookie_mutex);
|
|
|
|
KASSERT(c->c_refcnt > 0);
|
|
|
|
c->c_refcnt--;
|
|
|
|
if (fdtbus_interrupt_cookies_wanted) {
|
|
|
|
fdtbus_interrupt_cookies_wanted = false;
|
|
|
|
cv_signal(&fdtbus_interrupt_cookie_wait);
|
|
|
|
}
|
|
|
|
mutex_exit(&fdtbus_interrupt_cookie_mutex);
|
|
|
|
}
|
|
|
|
|
2015-12-16 15:03:44 +03:00
|
|
|
void *
|
|
|
|
fdtbus_intr_establish(int phandle, u_int index, int ipl, int flags,
|
|
|
|
int (*func)(void *), void *arg)
|
|
|
|
{
|
2018-07-15 19:59:16 +03:00
|
|
|
const u_int *specifier;
|
2017-06-02 16:12:33 +03:00
|
|
|
int ihandle;
|
2015-12-16 15:03:44 +03:00
|
|
|
|
2017-06-02 16:12:33 +03:00
|
|
|
specifier = get_specifier_by_index(phandle, index, &ihandle);
|
2019-12-31 23:47:05 +03:00
|
|
|
if (specifier == NULL)
|
2017-06-02 16:12:33 +03:00
|
|
|
return NULL;
|
2017-06-02 03:55:26 +03:00
|
|
|
|
2018-09-07 01:54:05 +03:00
|
|
|
return fdtbus_intr_establish_raw(ihandle, specifier, ipl,
|
|
|
|
flags, func, arg);
|
|
|
|
}
|
|
|
|
|
2019-02-27 20:01:57 +03:00
|
|
|
void *
|
|
|
|
fdtbus_intr_establish_byname(int phandle, const char *name, int ipl,
|
|
|
|
int flags, int (*func)(void *), void *arg)
|
|
|
|
{
|
|
|
|
u_int index;
|
|
|
|
int err;
|
|
|
|
|
|
|
|
err = fdtbus_get_index(phandle, "interrupt-names", name, &index);
|
|
|
|
if (err != 0)
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
return fdtbus_intr_establish(phandle, index, ipl, flags, func, arg);
|
|
|
|
}
|
|
|
|
|
2018-09-07 01:54:05 +03:00
|
|
|
void *
|
|
|
|
fdtbus_intr_establish_raw(int ihandle, const u_int *specifier, int ipl,
|
|
|
|
int flags, int (*func)(void *), void *arg)
|
|
|
|
{
|
|
|
|
struct fdtbus_interrupt_controller *ic;
|
|
|
|
struct fdtbus_interrupt_cookie *c;
|
|
|
|
void *ih;
|
|
|
|
|
2017-06-02 03:55:26 +03:00
|
|
|
ic = fdtbus_get_interrupt_controller(ihandle);
|
2019-11-17 00:53:38 +03:00
|
|
|
if (ic == NULL) {
|
|
|
|
printf("%s: ihandle %d is not a controller\n",__func__,ihandle);
|
2017-06-02 03:55:26 +03:00
|
|
|
return NULL;
|
2019-11-17 00:53:38 +03:00
|
|
|
}
|
2017-06-02 03:55:26 +03:00
|
|
|
|
2020-02-16 23:28:18 +03:00
|
|
|
c = kmem_zalloc(sizeof(*c), KM_SLEEP);
|
|
|
|
c->c_ic = ic;
|
|
|
|
mutex_enter(&fdtbus_interrupt_cookie_mutex);
|
|
|
|
LIST_INSERT_HEAD(&fdtbus_interrupt_cookies, c, c_next);
|
|
|
|
mutex_exit(&fdtbus_interrupt_cookie_mutex);
|
|
|
|
|
|
|
|
/*
|
|
|
|
* XXX This leaves a small window where the handler is registered
|
|
|
|
* (and thus could be called) before the cookie on the list has a
|
|
|
|
* valid lookup key (and thus can be found). This will cause a
|
|
|
|
* panic in fdt_intr_mask() if that is called from the handler before
|
|
|
|
* this situation is resolved. For now we just cross our fingers
|
|
|
|
* and hope that the device won't actually interrupt until we return.
|
|
|
|
*/
|
2018-07-15 19:59:16 +03:00
|
|
|
ih = ic->ic_funcs->establish(ic->ic_dev, __UNCONST(specifier),
|
2017-06-02 03:55:26 +03:00
|
|
|
ipl, flags, func, arg);
|
2017-06-11 15:56:36 +03:00
|
|
|
if (ih != NULL) {
|
2020-02-16 23:28:18 +03:00
|
|
|
atomic_store_release(&c->c_ih, ih);
|
|
|
|
} else {
|
|
|
|
mutex_enter(&fdtbus_interrupt_cookie_mutex);
|
|
|
|
LIST_REMOVE(c, c_next);
|
|
|
|
mutex_exit(&fdtbus_interrupt_cookie_mutex);
|
|
|
|
kmem_free(c, sizeof(*c));
|
2017-06-11 15:56:36 +03:00
|
|
|
}
|
|
|
|
|
2018-07-15 16:24:05 +03:00
|
|
|
return ih;
|
2015-12-16 15:03:44 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2017-06-11 15:56:36 +03:00
|
|
|
fdtbus_intr_disestablish(int phandle, void *cookie)
|
2015-12-16 15:03:44 +03:00
|
|
|
{
|
2018-07-15 16:24:05 +03:00
|
|
|
struct fdtbus_interrupt_cookie *c;
|
|
|
|
|
2020-02-16 23:28:18 +03:00
|
|
|
if ((c = fdtbus_get_interrupt_cookie(cookie)) == NULL) {
|
|
|
|
panic("%s: interrupt handle not valid", __func__);
|
2018-07-15 16:24:05 +03:00
|
|
|
}
|
|
|
|
|
2020-02-16 23:28:18 +03:00
|
|
|
const struct fdtbus_interrupt_controller *ic = c->c_ic;
|
|
|
|
ic->ic_funcs->disestablish(ic->ic_dev, cookie);
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Wait for any dangling references other than ours to
|
|
|
|
* drain away.
|
|
|
|
*/
|
|
|
|
mutex_enter(&fdtbus_interrupt_cookie_mutex);
|
|
|
|
while (c->c_refcnt != 1) {
|
|
|
|
KASSERT(c->c_refcnt > 0);
|
|
|
|
fdtbus_interrupt_cookies_wanted = true;
|
|
|
|
cv_wait(&fdtbus_interrupt_cookie_wait,
|
|
|
|
&fdtbus_interrupt_cookie_mutex);
|
|
|
|
}
|
|
|
|
LIST_REMOVE(c, c_next);
|
|
|
|
mutex_exit(&fdtbus_interrupt_cookie_mutex);
|
|
|
|
|
|
|
|
kmem_free(c, sizeof(*c));
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
fdtbus_intr_mask(int phandle, void *cookie)
|
|
|
|
{
|
|
|
|
struct fdtbus_interrupt_cookie *c;
|
|
|
|
|
|
|
|
if ((c = fdtbus_get_interrupt_cookie(cookie)) == NULL) {
|
|
|
|
panic("%s: interrupt handle not valid", __func__);
|
|
|
|
}
|
|
|
|
|
|
|
|
struct fdtbus_interrupt_controller * const ic = c->c_ic;
|
|
|
|
|
|
|
|
if (ic->ic_funcs->mask == NULL) {
|
|
|
|
panic("%s: no 'mask' method for %s", __func__,
|
|
|
|
device_xname(ic->ic_dev));
|
|
|
|
}
|
|
|
|
|
|
|
|
ic->ic_funcs->mask(ic->ic_dev, cookie);
|
|
|
|
fdtbus_put_interrupt_cookie(c);
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
fdtbus_intr_unmask(int phandle, void *cookie)
|
|
|
|
{
|
|
|
|
struct fdtbus_interrupt_cookie *c;
|
|
|
|
|
|
|
|
if ((c = fdtbus_get_interrupt_cookie(cookie)) == NULL) {
|
2018-07-15 16:24:05 +03:00
|
|
|
panic("%s: interrupt handle not valid", __func__);
|
2020-02-16 23:28:18 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
struct fdtbus_interrupt_controller * const ic = c->c_ic;
|
|
|
|
|
|
|
|
if (ic->ic_funcs->unmask == NULL) {
|
|
|
|
panic("%s: no 'unmask' method for %s", __func__,
|
|
|
|
device_xname(ic->ic_dev));
|
|
|
|
}
|
2015-12-16 15:03:44 +03:00
|
|
|
|
2020-02-16 23:28:18 +03:00
|
|
|
ic->ic_funcs->unmask(ic->ic_dev, cookie);
|
|
|
|
fdtbus_put_interrupt_cookie(c);
|
2015-12-16 15:03:44 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
|
|
|
fdtbus_intr_str(int phandle, u_int index, char *buf, size_t buflen)
|
|
|
|
{
|
2018-07-15 19:59:16 +03:00
|
|
|
const u_int *specifier;
|
2017-06-02 16:12:33 +03:00
|
|
|
int ihandle;
|
2017-06-02 03:55:26 +03:00
|
|
|
|
2017-06-02 16:12:33 +03:00
|
|
|
specifier = get_specifier_by_index(phandle, index, &ihandle);
|
2018-09-07 01:54:05 +03:00
|
|
|
if (specifier == NULL)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
return fdtbus_intr_str_raw(ihandle, specifier, buf, buflen);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
|
|
|
fdtbus_intr_str_raw(int ihandle, const u_int *specifier, char *buf, size_t buflen)
|
|
|
|
{
|
|
|
|
struct fdtbus_interrupt_controller *ic;
|
2017-06-02 03:55:26 +03:00
|
|
|
|
2016-01-06 00:53:48 +03:00
|
|
|
ic = fdtbus_get_interrupt_controller(ihandle);
|
2017-06-02 03:55:26 +03:00
|
|
|
if (ic == NULL)
|
|
|
|
return false;
|
|
|
|
|
2018-07-15 19:59:16 +03:00
|
|
|
return ic->ic_funcs->intrstr(ic->ic_dev, __UNCONST(specifier), buf, buflen);
|
2017-06-02 03:55:26 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
|
|
|
find_address_cells(int phandle)
|
|
|
|
{
|
|
|
|
uint32_t cells;
|
|
|
|
|
|
|
|
if (of_getprop_uint32(phandle, "#address-cells", &cells) != 0)
|
|
|
|
cells = 2;
|
|
|
|
|
|
|
|
return cells;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
|
|
|
find_interrupt_cells(int phandle)
|
|
|
|
{
|
|
|
|
uint32_t cells;
|
|
|
|
|
|
|
|
while (phandle > 0) {
|
|
|
|
if (of_getprop_uint32(phandle, "#interrupt-cells", &cells) == 0)
|
|
|
|
return cells;
|
|
|
|
phandle = OF_parent(phandle);
|
|
|
|
}
|
|
|
|
return 0;
|
2016-01-06 00:53:48 +03:00
|
|
|
}
|
2015-12-16 15:03:44 +03:00
|
|
|
|
2018-07-15 19:59:16 +03:00
|
|
|
static const u_int *
|
2018-07-02 15:17:05 +03:00
|
|
|
get_specifier_from_map(int phandle, const u_int *interrupt_spec, int *piphandle)
|
2016-01-06 00:53:48 +03:00
|
|
|
{
|
2018-07-15 19:59:16 +03:00
|
|
|
const u_int *result = NULL;
|
2017-06-02 16:12:33 +03:00
|
|
|
int len, resid;
|
2016-01-06 00:53:48 +03:00
|
|
|
|
2018-07-02 15:17:05 +03:00
|
|
|
const u_int *data = fdtbus_get_prop(phandle, "interrupt-map", &len);
|
|
|
|
if (data == NULL || len <= 0)
|
2017-06-02 16:12:33 +03:00
|
|
|
return NULL;
|
|
|
|
resid = len;
|
2016-01-06 00:53:48 +03:00
|
|
|
|
2017-06-02 03:55:26 +03:00
|
|
|
/* child unit address: #address-cells prop of child bus node */
|
2018-07-02 15:17:05 +03:00
|
|
|
const int cua_cells = find_address_cells(phandle);
|
2017-06-02 03:55:26 +03:00
|
|
|
/* child interrupt specifier: #interrupt-cells of the nexus node */
|
2018-07-02 15:17:05 +03:00
|
|
|
const int cis_cells = find_interrupt_cells(phandle);
|
2017-06-02 03:55:26 +03:00
|
|
|
|
|
|
|
/* Offset (in cells) from map entry to child unit address specifier */
|
|
|
|
const u_int cua_off = 0;
|
|
|
|
/* Offset (in cells) from map entry to child interrupt specifier */
|
|
|
|
const u_int cis_off = cua_off + cua_cells;
|
|
|
|
/* Offset (in cells) from map entry to interrupt parent phandle */
|
|
|
|
const u_int ip_off = cis_off + cis_cells;
|
|
|
|
/* Offset (in cells) from map entry to parent unit specifier */
|
|
|
|
const u_int pus_off = ip_off + 1;
|
|
|
|
|
2017-06-02 16:12:33 +03:00
|
|
|
const u_int *p = (const u_int *)data;
|
2016-01-06 00:53:48 +03:00
|
|
|
while (resid > 0) {
|
2017-06-02 03:55:26 +03:00
|
|
|
/* Interrupt parent phandle */
|
|
|
|
const u_int iparent = fdtbus_get_phandle_from_native(be32toh(p[ip_off]));
|
|
|
|
|
|
|
|
/* parent unit specifier: #address-cells of the interrupt parent */
|
|
|
|
const u_int pus_cells = find_address_cells(iparent);
|
|
|
|
/* parent interrupt specifier: #interrupt-cells of the interrupt parent */
|
|
|
|
const u_int pis_cells = find_interrupt_cells(iparent);
|
|
|
|
|
|
|
|
/* Offset (in cells) from map entry to parent interrupt specifier */
|
|
|
|
const u_int pis_off = pus_off + pus_cells;
|
|
|
|
|
2017-06-02 16:12:33 +03:00
|
|
|
#ifdef FDT_INTR_DEBUG
|
|
|
|
printf(" intr map (len %d):", pis_off + pis_cells);
|
|
|
|
for (int i = 0; i < pis_off + pis_cells; i++)
|
|
|
|
printf(" %08x", p[i]);
|
|
|
|
printf("\n");
|
|
|
|
#endif
|
|
|
|
|
2018-07-02 15:17:05 +03:00
|
|
|
if (memcmp(&p[cis_off], interrupt_spec, cis_cells * 4) == 0) {
|
2017-06-02 03:55:26 +03:00
|
|
|
#ifdef FDT_INTR_DEBUG
|
2018-07-15 19:59:16 +03:00
|
|
|
const int slen = pus_cells + pis_cells;
|
2017-06-02 03:55:26 +03:00
|
|
|
printf(" intr map match iparent %08x slen %d:", iparent, slen);
|
|
|
|
for (int i = 0; i < slen; i++)
|
|
|
|
printf(" %08x", p[pus_off + i]);
|
|
|
|
printf("\n");
|
|
|
|
#endif
|
2018-07-15 19:59:16 +03:00
|
|
|
result = &p[pus_off];
|
2017-06-02 03:55:26 +03:00
|
|
|
*piphandle = iparent;
|
2016-01-07 07:26:44 +03:00
|
|
|
goto done;
|
2016-01-06 00:53:48 +03:00
|
|
|
}
|
|
|
|
/* Determine the length of the entry and skip that many
|
|
|
|
* 32 bit words
|
|
|
|
*/
|
2017-06-02 03:55:26 +03:00
|
|
|
const u_int reclen = pis_off + pis_cells;
|
2016-01-06 00:53:48 +03:00
|
|
|
resid -= reclen * sizeof(u_int);
|
|
|
|
p += reclen;
|
|
|
|
}
|
2017-06-02 03:55:26 +03:00
|
|
|
|
2016-01-07 07:26:44 +03:00
|
|
|
done:
|
|
|
|
return result;
|
2016-01-06 00:53:48 +03:00
|
|
|
}
|
|
|
|
|
2019-06-14 14:08:18 +03:00
|
|
|
|
|
|
|
static const u_int *
|
|
|
|
get_specifier_from_extended(int phandle, int pindex, int *piphandle)
|
|
|
|
{
|
|
|
|
const u_int *result = NULL;
|
|
|
|
struct fdt_phandle_data data;
|
|
|
|
|
|
|
|
if (fdtbus_get_phandle_with_data(phandle, "interrupts-extended",
|
|
|
|
"#interrupt-cells", pindex, &data) == 0) {
|
|
|
|
*piphandle = data.phandle;
|
|
|
|
result = data.values;
|
|
|
|
}
|
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2018-07-15 19:59:16 +03:00
|
|
|
static const u_int *
|
2017-06-02 16:12:33 +03:00
|
|
|
get_specifier_by_index(int phandle, int pindex, int *piphandle)
|
2016-01-06 00:53:48 +03:00
|
|
|
{
|
2017-06-02 16:12:33 +03:00
|
|
|
const u_int *node_specifier;
|
2016-01-06 00:53:48 +03:00
|
|
|
int interrupt_parent, interrupt_cells, len;
|
|
|
|
|
2019-06-14 14:08:18 +03:00
|
|
|
if (of_hasprop(phandle, "interrupts-extended"))
|
|
|
|
return get_specifier_from_extended(phandle, pindex, piphandle);
|
|
|
|
|
2016-01-07 07:26:44 +03:00
|
|
|
interrupt_parent = fdtbus_get_interrupt_parent(phandle);
|
2017-06-02 03:55:26 +03:00
|
|
|
if (interrupt_parent <= 0)
|
2016-01-06 00:53:48 +03:00
|
|
|
return NULL;
|
|
|
|
|
2018-07-02 15:17:05 +03:00
|
|
|
node_specifier = fdtbus_get_prop(phandle, "interrupts", &len);
|
|
|
|
if (node_specifier == NULL)
|
2016-01-06 00:53:48 +03:00
|
|
|
return NULL;
|
|
|
|
|
2018-07-02 15:17:05 +03:00
|
|
|
interrupt_cells = find_interrupt_cells(interrupt_parent);
|
|
|
|
if (interrupt_cells <= 0)
|
2016-01-06 00:53:48 +03:00
|
|
|
return NULL;
|
|
|
|
|
2017-06-02 16:12:33 +03:00
|
|
|
const u_int spec_length = len / 4;
|
|
|
|
const u_int nintr = spec_length / interrupt_cells;
|
2016-01-06 00:53:48 +03:00
|
|
|
if (pindex >= nintr)
|
|
|
|
return NULL;
|
|
|
|
|
2017-06-02 16:12:33 +03:00
|
|
|
node_specifier += (interrupt_cells * pindex);
|
2016-01-06 00:53:48 +03:00
|
|
|
|
2018-07-02 15:17:05 +03:00
|
|
|
if (of_hasprop(interrupt_parent, "interrupt-map"))
|
|
|
|
return get_specifier_from_map(interrupt_parent, node_specifier, piphandle);
|
|
|
|
|
2017-06-02 16:12:33 +03:00
|
|
|
*piphandle = interrupt_parent;
|
|
|
|
|
2018-07-15 19:59:16 +03:00
|
|
|
return node_specifier;
|
2015-12-16 15:03:44 +03:00
|
|
|
}
|