Home | History | Annotate | Line # | Download | only in fdt
fdt_intr.c revision 1.2
      1 /* $NetBSD: fdt_intr.c,v 1.2 2015/12/16 12:17:45 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.2 2015/12/16 12:17:45 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 int
     50 fdtbus_get_interrupt_parent(int phandle)
     51 {
     52 	u_int interrupt_parent;
     53 	int len;
     54 
     55 	while (phandle >= 0) {
     56 		len = OF_getprop(phandle, "interrupt-parent",
     57 		    &interrupt_parent, sizeof(interrupt_parent));
     58 		if (len == sizeof(interrupt_parent)) {
     59 			break;
     60 		}
     61 		if (phandle == 0) {
     62 			return -1;
     63 		}
     64 		phandle = OF_parent(phandle);
     65 	}
     66 	if (phandle < 0) {
     67 		return -1;
     68 	}
     69 
     70 	interrupt_parent = fdt32_to_cpu(interrupt_parent);
     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(int phandle)
     83 {
     84 	struct fdtbus_interrupt_controller *ic;
     85 
     86 	const int ic_phandle = fdtbus_get_interrupt_parent(phandle);
     87 	if (ic_phandle < 0) {
     88 		return NULL;
     89 	}
     90 
     91 	for (ic = fdtbus_ic; ic; ic = ic->ic_next) {
     92 		if (ic->ic_phandle == ic_phandle) {
     93 			return ic;
     94 		}
     95 	}
     96 
     97 	return NULL;
     98 }
     99 
    100 int
    101 fdtbus_register_interrupt_controller(device_t dev, int phandle,
    102     const struct fdtbus_interrupt_controller_func *funcs)
    103 {
    104 	struct fdtbus_interrupt_controller *ic;
    105 
    106 	ic = kmem_alloc(sizeof(*ic), KM_SLEEP);
    107 	ic->ic_dev = dev;
    108 	ic->ic_phandle = phandle;
    109 	ic->ic_funcs = funcs;
    110 
    111 	ic->ic_next = fdtbus_ic;
    112 	fdtbus_ic = ic;
    113 
    114 	return 0;
    115 }
    116 
    117 void *
    118 fdtbus_intr_establish(int phandle, u_int index, int ipl, int flags,
    119     int (*func)(void *), void *arg)
    120 {
    121 	struct fdtbus_interrupt_controller *ic;
    122 
    123 	ic = fdtbus_get_interrupt_controller(phandle);
    124 	if (ic == NULL)
    125 		return NULL;
    126 
    127 	return ic->ic_funcs->establish(ic->ic_dev, phandle, index, ipl,
    128 	    flags, func, arg);
    129 }
    130 
    131 void
    132 fdtbus_intr_disestablish(int phandle, void *ih)
    133 {
    134 	struct fdtbus_interrupt_controller *ic;
    135 
    136 	ic = fdtbus_get_interrupt_controller(phandle);
    137 	KASSERT(ic != NULL);
    138 
    139 	return ic->ic_funcs->disestablish(ic->ic_dev, ih);
    140 }
    141 
    142 bool
    143 fdtbus_intr_str(int phandle, u_int index, char *buf, size_t buflen)
    144 {
    145 	struct fdtbus_interrupt_controller *ic;
    146 
    147 	ic = fdtbus_get_interrupt_controller(phandle);
    148 	if (ic == NULL)
    149 		return false;
    150 
    151 	return ic->ic_funcs->intrstr(ic->ic_dev, phandle, index, buf, buflen);
    152 }
    153