Home | History | Annotate | Line # | Download | only in dev
octeon_cib.c revision 1.3
      1  1.3   thorpej /* $NetBSD: octeon_cib.c,v 1.3 2021/01/18 02:35:49 thorpej Exp $ */
      2  1.1  jmcneill 
      3  1.1  jmcneill /*-
      4  1.1  jmcneill  * Copyright (c) 2020 Jared D. McNeill <jmcneill (at) invisible.ca>
      5  1.1  jmcneill  * All rights reserved.
      6  1.1  jmcneill  *
      7  1.1  jmcneill  * Redistribution and use in source and binary forms, with or without
      8  1.1  jmcneill  * modification, are permitted provided that the following conditions
      9  1.1  jmcneill  * are met:
     10  1.1  jmcneill  * 1. Redistributions of source code must retain the above copyright
     11  1.1  jmcneill  *    notice, this list of conditions and the following disclaimer.
     12  1.1  jmcneill  * 2. Redistributions in binary form must reproduce the above copyright
     13  1.1  jmcneill  *    notice, this list of conditions and the following disclaimer in the
     14  1.1  jmcneill  *    documentation and/or other materials provided with the distribution.
     15  1.1  jmcneill  *
     16  1.1  jmcneill  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
     17  1.1  jmcneill  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
     18  1.1  jmcneill  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
     19  1.1  jmcneill  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
     20  1.1  jmcneill  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
     21  1.1  jmcneill  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
     22  1.1  jmcneill  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
     23  1.1  jmcneill  * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
     24  1.1  jmcneill  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     25  1.1  jmcneill  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     26  1.1  jmcneill  * SUCH DAMAGE.
     27  1.1  jmcneill  */
     28  1.1  jmcneill 
     29  1.1  jmcneill #include "opt_multiprocessor.h"
     30  1.1  jmcneill 
     31  1.1  jmcneill #include <sys/cdefs.h>
     32  1.3   thorpej __KERNEL_RCSID(0, "$NetBSD: octeon_cib.c,v 1.3 2021/01/18 02:35:49 thorpej Exp $");
     33  1.1  jmcneill 
     34  1.1  jmcneill #include <sys/param.h>
     35  1.1  jmcneill #include <sys/bus.h>
     36  1.1  jmcneill #include <sys/device.h>
     37  1.1  jmcneill #include <sys/intr.h>
     38  1.1  jmcneill #include <sys/systm.h>
     39  1.1  jmcneill #include <sys/kernel.h>
     40  1.1  jmcneill #include <sys/kmem.h>
     41  1.1  jmcneill 
     42  1.1  jmcneill #include <dev/fdt/fdtvar.h>
     43  1.1  jmcneill 
     44  1.1  jmcneill #include <arch/mips/cavium/octeonvar.h>
     45  1.1  jmcneill 
     46  1.1  jmcneill static int	octeon_cib_match(device_t, cfdata_t, void *);
     47  1.1  jmcneill static void	octeon_cib_attach(device_t, device_t, void *);
     48  1.1  jmcneill 
     49  1.1  jmcneill static void *	octeon_cib_establish(device_t, u_int *, int, int,
     50  1.2  jmcneill 		    int (*)(void *), void *, const char *);
     51  1.1  jmcneill static void	octeon_cib_disestablish(device_t, void *);
     52  1.1  jmcneill static bool	octeon_cib_intrstr(device_t, u_int *, char *, size_t);
     53  1.1  jmcneill 
     54  1.1  jmcneill static int	octeon_cib_intr(void *);
     55  1.1  jmcneill 
     56  1.1  jmcneill struct fdtbus_interrupt_controller_func octeon_cib_funcs = {
     57  1.1  jmcneill 	.establish = octeon_cib_establish,
     58  1.1  jmcneill 	.disestablish = octeon_cib_disestablish,
     59  1.1  jmcneill 	.intrstr = octeon_cib_intrstr
     60  1.1  jmcneill };
     61  1.1  jmcneill 
     62  1.1  jmcneill struct octeon_cib_intr {
     63  1.1  jmcneill 	bool			ih_mpsafe;
     64  1.1  jmcneill 	int			ih_type;
     65  1.1  jmcneill 	int			(*ih_func)(void *);
     66  1.1  jmcneill 	void			*ih_arg;
     67  1.1  jmcneill 	uint64_t		ih_mask;
     68  1.1  jmcneill };
     69  1.1  jmcneill 
     70  1.1  jmcneill struct octeon_cib_softc {
     71  1.1  jmcneill 	device_t		sc_dev;
     72  1.1  jmcneill 	int			sc_phandle;
     73  1.1  jmcneill 	bus_space_tag_t		sc_bst;
     74  1.1  jmcneill 	bus_space_handle_t	sc_bsh_raw;
     75  1.1  jmcneill 	bus_space_handle_t	sc_bsh_en;
     76  1.1  jmcneill 
     77  1.1  jmcneill 	struct octeon_cib_intr	*sc_intr;
     78  1.1  jmcneill 	u_int			sc_nintr;
     79  1.1  jmcneill };
     80  1.1  jmcneill 
     81  1.1  jmcneill #define	CIB_READ_RAW(sc)		\
     82  1.1  jmcneill 	bus_space_read_8((sc)->sc_bst, (sc)->sc_bsh_raw, 0)
     83  1.1  jmcneill #define	CIB_WRITE_RAW(sc, val)		\
     84  1.1  jmcneill 	bus_space_write_8((sc)->sc_bst, (sc)->sc_bsh_raw, 0, (val))
     85  1.1  jmcneill #define	CIB_READ_EN(sc)			\
     86  1.1  jmcneill 	bus_space_read_8((sc)->sc_bst, (sc)->sc_bsh_en, 0)
     87  1.1  jmcneill #define	CIB_WRITE_EN(sc, val)		\
     88  1.1  jmcneill 	bus_space_write_8((sc)->sc_bst, (sc)->sc_bsh_en, 0, (val))
     89  1.1  jmcneill 
     90  1.1  jmcneill CFATTACH_DECL_NEW(octcib, sizeof(struct octeon_cib_softc),
     91  1.1  jmcneill 	octeon_cib_match, octeon_cib_attach, NULL, NULL);
     92  1.1  jmcneill 
     93  1.3   thorpej static const struct device_compatible_entry compat_data[] = {
     94  1.3   thorpej 	{ .compat = "cavium,octeon-7130-cib" },
     95  1.3   thorpej 
     96  1.3   thorpej 	{ 0 }
     97  1.1  jmcneill };
     98  1.1  jmcneill 
     99  1.1  jmcneill static int
    100  1.1  jmcneill octeon_cib_match(device_t parent, cfdata_t cf, void *aux)
    101  1.1  jmcneill {
    102  1.1  jmcneill 	struct fdt_attach_args * const faa = aux;
    103  1.1  jmcneill 
    104  1.1  jmcneill 	return of_match_compat_data(faa->faa_phandle, compat_data);
    105  1.1  jmcneill }
    106  1.1  jmcneill 
    107  1.1  jmcneill static void
    108  1.1  jmcneill octeon_cib_attach(device_t parent, device_t self, void *aux)
    109  1.1  jmcneill {
    110  1.1  jmcneill 	struct octeon_cib_softc * const sc = device_private(self);
    111  1.1  jmcneill 	struct fdt_attach_args * const faa = aux;
    112  1.1  jmcneill 	const int phandle = faa->faa_phandle;
    113  1.1  jmcneill 	char intrstr[128];
    114  1.1  jmcneill 	bus_addr_t addr;
    115  1.1  jmcneill 	bus_size_t size;
    116  1.1  jmcneill 	u_int max_bits;
    117  1.1  jmcneill 	int error;
    118  1.1  jmcneill 	void *ih;
    119  1.1  jmcneill 
    120  1.1  jmcneill 	sc->sc_dev = self;
    121  1.1  jmcneill 	sc->sc_phandle = phandle;
    122  1.1  jmcneill 	sc->sc_bst = faa->faa_bst;
    123  1.1  jmcneill 
    124  1.1  jmcneill 	if (fdtbus_get_reg(phandle, 0, &addr, &size) != 0 ||
    125  1.1  jmcneill 	    bus_space_map(sc->sc_bst, addr, size, 0, &sc->sc_bsh_raw) != 0) {
    126  1.1  jmcneill 		aprint_error(": couldn't map RAW register\n");
    127  1.1  jmcneill 		return;
    128  1.1  jmcneill 	}
    129  1.1  jmcneill 	if (fdtbus_get_reg(phandle, 1, &addr, &size) != 0 ||
    130  1.1  jmcneill 	    bus_space_map(sc->sc_bst, addr, size, 0, &sc->sc_bsh_en) != 0) {
    131  1.1  jmcneill 		aprint_error(": couldn't map EN register\n");
    132  1.1  jmcneill 		return;
    133  1.1  jmcneill 	}
    134  1.1  jmcneill 
    135  1.1  jmcneill 	if (!fdtbus_intr_str(phandle, 0, intrstr, sizeof(intrstr))) {
    136  1.1  jmcneill 		aprint_error(": failed to decode interrupt\n");
    137  1.1  jmcneill 		return;
    138  1.1  jmcneill 	}
    139  1.1  jmcneill 
    140  1.1  jmcneill 	if (of_getprop_uint32(phandle, "cavium,max-bits", &max_bits) != 0) {
    141  1.1  jmcneill 		aprint_error(": missing 'cavium,max-bits' property\n");
    142  1.1  jmcneill 		return;
    143  1.1  jmcneill 	}
    144  1.1  jmcneill 	if (max_bits == 0 || max_bits > 64) {
    145  1.1  jmcneill 		aprint_error(": 'cavium,max-bits' value out of range\n");
    146  1.1  jmcneill 		return;
    147  1.1  jmcneill 	}
    148  1.1  jmcneill 
    149  1.1  jmcneill 	sc->sc_intr = kmem_zalloc(sizeof(*sc->sc_intr) * max_bits, KM_SLEEP);
    150  1.1  jmcneill 	sc->sc_nintr = max_bits;
    151  1.1  jmcneill 
    152  1.1  jmcneill 	error = fdtbus_register_interrupt_controller(self, phandle,
    153  1.1  jmcneill 	    &octeon_cib_funcs);
    154  1.1  jmcneill 	if (error != 0) {
    155  1.1  jmcneill 		aprint_error(": couldn't register with fdtbus: %d\n", error);
    156  1.1  jmcneill 		return;
    157  1.1  jmcneill 	}
    158  1.1  jmcneill 
    159  1.1  jmcneill 	aprint_naive("\n");
    160  1.1  jmcneill 	aprint_normal(": CIB\n");
    161  1.1  jmcneill 
    162  1.1  jmcneill 	CIB_WRITE_EN(sc, 0);
    163  1.1  jmcneill 	CIB_WRITE_RAW(sc, ~0ULL);
    164  1.1  jmcneill 
    165  1.1  jmcneill 	ih = fdtbus_intr_establish(phandle, 0, IPL_SCHED, FDT_INTR_MPSAFE,
    166  1.1  jmcneill 	    octeon_cib_intr, sc);
    167  1.1  jmcneill 	if (ih == NULL) {
    168  1.1  jmcneill 		aprint_error_dev(self, "couldn't establish interrupt on %s\n",
    169  1.1  jmcneill 		    intrstr);
    170  1.1  jmcneill 		return;
    171  1.1  jmcneill 	}
    172  1.1  jmcneill 	aprint_normal_dev(self, "interrupting on %s\n", intrstr);
    173  1.1  jmcneill }
    174  1.1  jmcneill 
    175  1.1  jmcneill static void *
    176  1.1  jmcneill octeon_cib_establish(device_t dev, u_int *specifier, int ipl, int flags,
    177  1.2  jmcneill     int (*func)(void *), void *arg, const char *xname)
    178  1.1  jmcneill {
    179  1.1  jmcneill 	struct octeon_cib_softc * const sc = device_private(dev);
    180  1.1  jmcneill 	struct octeon_cib_intr *ih;
    181  1.1  jmcneill 	uint64_t val;
    182  1.1  jmcneill 
    183  1.1  jmcneill 	/* 1st cell is the bit number in the CIB* registers */
    184  1.1  jmcneill 	/* 2nd cell is the triggering setting */
    185  1.1  jmcneill 	const int bit = be32toh(specifier[0]);
    186  1.1  jmcneill 	const int type = (be32toh(specifier[1]) & 0x3) ? IST_EDGE : IST_LEVEL;
    187  1.1  jmcneill 
    188  1.1  jmcneill 	if (bit > sc->sc_nintr) {
    189  1.1  jmcneill 		aprint_error_dev(dev, "bit %d out of range\n", bit);
    190  1.1  jmcneill 		return NULL;
    191  1.1  jmcneill 	}
    192  1.1  jmcneill 
    193  1.1  jmcneill 	ih = &sc->sc_intr[bit];
    194  1.1  jmcneill 	ih->ih_mpsafe = (flags & FDT_INTR_MPSAFE) != 0;
    195  1.1  jmcneill 	ih->ih_type = type;
    196  1.1  jmcneill 	ih->ih_func = func;
    197  1.1  jmcneill 	ih->ih_arg = arg;
    198  1.1  jmcneill 	ih->ih_mask = __BIT(bit);
    199  1.1  jmcneill 
    200  1.1  jmcneill 	val = CIB_READ_EN(sc);
    201  1.1  jmcneill 	val |= ih->ih_mask;
    202  1.1  jmcneill 	CIB_WRITE_EN(sc, val);
    203  1.1  jmcneill 
    204  1.1  jmcneill 	return ih;
    205  1.1  jmcneill }
    206  1.1  jmcneill 
    207  1.1  jmcneill static void
    208  1.1  jmcneill octeon_cib_disestablish(device_t dev, void *ih_cookie)
    209  1.1  jmcneill {
    210  1.1  jmcneill 	struct octeon_cib_softc * const sc = device_private(dev);
    211  1.1  jmcneill 	struct octeon_cib_intr *ih = ih_cookie;
    212  1.1  jmcneill 	uint64_t val;
    213  1.1  jmcneill 
    214  1.1  jmcneill 	val = CIB_READ_EN(sc);
    215  1.1  jmcneill 	val &= ~ih->ih_mask;
    216  1.1  jmcneill 	CIB_WRITE_EN(sc, val);
    217  1.1  jmcneill }
    218  1.1  jmcneill 
    219  1.1  jmcneill static bool
    220  1.1  jmcneill octeon_cib_intrstr(device_t dev, u_int *specifier, char *buf,
    221  1.1  jmcneill     size_t buflen)
    222  1.1  jmcneill {
    223  1.1  jmcneill 	/* 1st cell is the bit number in the CIB* registers */
    224  1.1  jmcneill 	const int bit = be32toh(specifier[0]);
    225  1.1  jmcneill 
    226  1.1  jmcneill 	snprintf(buf, buflen, "%s intr %d", device_xname(dev), bit);
    227  1.1  jmcneill 
    228  1.1  jmcneill 	return true;
    229  1.1  jmcneill }
    230  1.1  jmcneill 
    231  1.1  jmcneill static int
    232  1.1  jmcneill octeon_cib_intr(void *priv)
    233  1.1  jmcneill {
    234  1.1  jmcneill 	struct octeon_cib_softc * const sc = priv;
    235  1.1  jmcneill 	struct octeon_cib_intr *ih;
    236  1.1  jmcneill 	uint64_t pend;
    237  1.1  jmcneill 	int n, rv = 0;
    238  1.1  jmcneill 
    239  1.1  jmcneill 	pend = CIB_READ_RAW(sc);
    240  1.1  jmcneill 	pend &= CIB_READ_EN(sc);
    241  1.1  jmcneill 
    242  1.1  jmcneill 	while ((n = ffs64(pend)) != 0) {
    243  1.1  jmcneill 		ih = &sc->sc_intr[n - 1];
    244  1.1  jmcneill 		KASSERT(ih->ih_mask == __BIT(n - 1));
    245  1.1  jmcneill 
    246  1.1  jmcneill 		if (ih->ih_type == IST_EDGE)
    247  1.1  jmcneill 			CIB_WRITE_RAW(sc, ih->ih_mask);	/* ack */
    248  1.1  jmcneill 
    249  1.1  jmcneill #ifdef MULTIPROCESSOR
    250  1.1  jmcneill 		if (!ih->ih_mpsafe) {
    251  1.1  jmcneill 			KERNEL_LOCK(1, NULL);
    252  1.1  jmcneill 			rv |= ih->ih_func(ih->ih_arg);
    253  1.1  jmcneill 			KERNEL_UNLOCK_ONE(NULL);
    254  1.1  jmcneill 		} else
    255  1.1  jmcneill #endif
    256  1.1  jmcneill 			rv |= ih->ih_func(ih->ih_arg);
    257  1.1  jmcneill 
    258  1.1  jmcneill 		pend &= ~ih->ih_mask;
    259  1.1  jmcneill 	}
    260  1.1  jmcneill 
    261  1.1  jmcneill 	return rv;
    262  1.1  jmcneill }
    263