Home | History | Annotate | Line # | Download | only in dev
octeon_cib.c revision 1.1.2.1
      1  1.1.2.1   thorpej /* $NetBSD: octeon_cib.c,v 1.1.2.1 2021/04/03 22:28:31 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.1.2.1   thorpej __KERNEL_RCSID(0, "$NetBSD: octeon_cib.c,v 1.1.2.1 2021/04/03 22:28:31 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.1.2.1   thorpej 		    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.1.2.1   thorpej static const struct device_compatible_entry compat_data[] = {
     94  1.1.2.1   thorpej 	{ .compat = "cavium,octeon-7130-cib" },
     95  1.1.2.1   thorpej 	DEVICE_COMPAT_EOL
     96      1.1  jmcneill };
     97      1.1  jmcneill 
     98      1.1  jmcneill static int
     99      1.1  jmcneill octeon_cib_match(device_t parent, cfdata_t cf, void *aux)
    100      1.1  jmcneill {
    101      1.1  jmcneill 	struct fdt_attach_args * const faa = aux;
    102      1.1  jmcneill 
    103  1.1.2.1   thorpej 	return of_compatible_match(faa->faa_phandle, compat_data);
    104      1.1  jmcneill }
    105      1.1  jmcneill 
    106      1.1  jmcneill static void
    107      1.1  jmcneill octeon_cib_attach(device_t parent, device_t self, void *aux)
    108      1.1  jmcneill {
    109      1.1  jmcneill 	struct octeon_cib_softc * const sc = device_private(self);
    110      1.1  jmcneill 	struct fdt_attach_args * const faa = aux;
    111      1.1  jmcneill 	const int phandle = faa->faa_phandle;
    112      1.1  jmcneill 	char intrstr[128];
    113      1.1  jmcneill 	bus_addr_t addr;
    114      1.1  jmcneill 	bus_size_t size;
    115      1.1  jmcneill 	u_int max_bits;
    116      1.1  jmcneill 	int error;
    117      1.1  jmcneill 	void *ih;
    118      1.1  jmcneill 
    119      1.1  jmcneill 	sc->sc_dev = self;
    120      1.1  jmcneill 	sc->sc_phandle = phandle;
    121      1.1  jmcneill 	sc->sc_bst = faa->faa_bst;
    122      1.1  jmcneill 
    123      1.1  jmcneill 	if (fdtbus_get_reg(phandle, 0, &addr, &size) != 0 ||
    124      1.1  jmcneill 	    bus_space_map(sc->sc_bst, addr, size, 0, &sc->sc_bsh_raw) != 0) {
    125      1.1  jmcneill 		aprint_error(": couldn't map RAW register\n");
    126      1.1  jmcneill 		return;
    127      1.1  jmcneill 	}
    128      1.1  jmcneill 	if (fdtbus_get_reg(phandle, 1, &addr, &size) != 0 ||
    129      1.1  jmcneill 	    bus_space_map(sc->sc_bst, addr, size, 0, &sc->sc_bsh_en) != 0) {
    130      1.1  jmcneill 		aprint_error(": couldn't map EN register\n");
    131      1.1  jmcneill 		return;
    132      1.1  jmcneill 	}
    133      1.1  jmcneill 
    134      1.1  jmcneill 	if (!fdtbus_intr_str(phandle, 0, intrstr, sizeof(intrstr))) {
    135      1.1  jmcneill 		aprint_error(": failed to decode interrupt\n");
    136      1.1  jmcneill 		return;
    137      1.1  jmcneill 	}
    138      1.1  jmcneill 
    139      1.1  jmcneill 	if (of_getprop_uint32(phandle, "cavium,max-bits", &max_bits) != 0) {
    140      1.1  jmcneill 		aprint_error(": missing 'cavium,max-bits' property\n");
    141      1.1  jmcneill 		return;
    142      1.1  jmcneill 	}
    143      1.1  jmcneill 	if (max_bits == 0 || max_bits > 64) {
    144      1.1  jmcneill 		aprint_error(": 'cavium,max-bits' value out of range\n");
    145      1.1  jmcneill 		return;
    146      1.1  jmcneill 	}
    147      1.1  jmcneill 
    148      1.1  jmcneill 	sc->sc_intr = kmem_zalloc(sizeof(*sc->sc_intr) * max_bits, KM_SLEEP);
    149      1.1  jmcneill 	sc->sc_nintr = max_bits;
    150      1.1  jmcneill 
    151      1.1  jmcneill 	error = fdtbus_register_interrupt_controller(self, phandle,
    152      1.1  jmcneill 	    &octeon_cib_funcs);
    153      1.1  jmcneill 	if (error != 0) {
    154      1.1  jmcneill 		aprint_error(": couldn't register with fdtbus: %d\n", error);
    155      1.1  jmcneill 		return;
    156      1.1  jmcneill 	}
    157      1.1  jmcneill 
    158      1.1  jmcneill 	aprint_naive("\n");
    159      1.1  jmcneill 	aprint_normal(": CIB\n");
    160      1.1  jmcneill 
    161      1.1  jmcneill 	CIB_WRITE_EN(sc, 0);
    162      1.1  jmcneill 	CIB_WRITE_RAW(sc, ~0ULL);
    163      1.1  jmcneill 
    164      1.1  jmcneill 	ih = fdtbus_intr_establish(phandle, 0, IPL_SCHED, FDT_INTR_MPSAFE,
    165      1.1  jmcneill 	    octeon_cib_intr, sc);
    166      1.1  jmcneill 	if (ih == NULL) {
    167      1.1  jmcneill 		aprint_error_dev(self, "couldn't establish interrupt on %s\n",
    168      1.1  jmcneill 		    intrstr);
    169      1.1  jmcneill 		return;
    170      1.1  jmcneill 	}
    171      1.1  jmcneill 	aprint_normal_dev(self, "interrupting on %s\n", intrstr);
    172      1.1  jmcneill }
    173      1.1  jmcneill 
    174      1.1  jmcneill static void *
    175      1.1  jmcneill octeon_cib_establish(device_t dev, u_int *specifier, int ipl, int flags,
    176  1.1.2.1   thorpej     int (*func)(void *), void *arg, const char *xname)
    177      1.1  jmcneill {
    178      1.1  jmcneill 	struct octeon_cib_softc * const sc = device_private(dev);
    179      1.1  jmcneill 	struct octeon_cib_intr *ih;
    180      1.1  jmcneill 	uint64_t val;
    181      1.1  jmcneill 
    182      1.1  jmcneill 	/* 1st cell is the bit number in the CIB* registers */
    183      1.1  jmcneill 	/* 2nd cell is the triggering setting */
    184      1.1  jmcneill 	const int bit = be32toh(specifier[0]);
    185      1.1  jmcneill 	const int type = (be32toh(specifier[1]) & 0x3) ? IST_EDGE : IST_LEVEL;
    186      1.1  jmcneill 
    187      1.1  jmcneill 	if (bit > sc->sc_nintr) {
    188      1.1  jmcneill 		aprint_error_dev(dev, "bit %d out of range\n", bit);
    189      1.1  jmcneill 		return NULL;
    190      1.1  jmcneill 	}
    191      1.1  jmcneill 
    192      1.1  jmcneill 	ih = &sc->sc_intr[bit];
    193      1.1  jmcneill 	ih->ih_mpsafe = (flags & FDT_INTR_MPSAFE) != 0;
    194      1.1  jmcneill 	ih->ih_type = type;
    195      1.1  jmcneill 	ih->ih_func = func;
    196      1.1  jmcneill 	ih->ih_arg = arg;
    197      1.1  jmcneill 	ih->ih_mask = __BIT(bit);
    198      1.1  jmcneill 
    199      1.1  jmcneill 	val = CIB_READ_EN(sc);
    200      1.1  jmcneill 	val |= ih->ih_mask;
    201      1.1  jmcneill 	CIB_WRITE_EN(sc, val);
    202      1.1  jmcneill 
    203      1.1  jmcneill 	return ih;
    204      1.1  jmcneill }
    205      1.1  jmcneill 
    206      1.1  jmcneill static void
    207      1.1  jmcneill octeon_cib_disestablish(device_t dev, void *ih_cookie)
    208      1.1  jmcneill {
    209      1.1  jmcneill 	struct octeon_cib_softc * const sc = device_private(dev);
    210      1.1  jmcneill 	struct octeon_cib_intr *ih = ih_cookie;
    211      1.1  jmcneill 	uint64_t val;
    212      1.1  jmcneill 
    213      1.1  jmcneill 	val = CIB_READ_EN(sc);
    214      1.1  jmcneill 	val &= ~ih->ih_mask;
    215      1.1  jmcneill 	CIB_WRITE_EN(sc, val);
    216      1.1  jmcneill }
    217      1.1  jmcneill 
    218      1.1  jmcneill static bool
    219      1.1  jmcneill octeon_cib_intrstr(device_t dev, u_int *specifier, char *buf,
    220      1.1  jmcneill     size_t buflen)
    221      1.1  jmcneill {
    222      1.1  jmcneill 	/* 1st cell is the bit number in the CIB* registers */
    223      1.1  jmcneill 	const int bit = be32toh(specifier[0]);
    224      1.1  jmcneill 
    225      1.1  jmcneill 	snprintf(buf, buflen, "%s intr %d", device_xname(dev), bit);
    226      1.1  jmcneill 
    227      1.1  jmcneill 	return true;
    228      1.1  jmcneill }
    229      1.1  jmcneill 
    230      1.1  jmcneill static int
    231      1.1  jmcneill octeon_cib_intr(void *priv)
    232      1.1  jmcneill {
    233      1.1  jmcneill 	struct octeon_cib_softc * const sc = priv;
    234      1.1  jmcneill 	struct octeon_cib_intr *ih;
    235      1.1  jmcneill 	uint64_t pend;
    236      1.1  jmcneill 	int n, rv = 0;
    237      1.1  jmcneill 
    238      1.1  jmcneill 	pend = CIB_READ_RAW(sc);
    239      1.1  jmcneill 	pend &= CIB_READ_EN(sc);
    240      1.1  jmcneill 
    241      1.1  jmcneill 	while ((n = ffs64(pend)) != 0) {
    242      1.1  jmcneill 		ih = &sc->sc_intr[n - 1];
    243      1.1  jmcneill 		KASSERT(ih->ih_mask == __BIT(n - 1));
    244      1.1  jmcneill 
    245      1.1  jmcneill 		if (ih->ih_type == IST_EDGE)
    246      1.1  jmcneill 			CIB_WRITE_RAW(sc, ih->ih_mask);	/* ack */
    247      1.1  jmcneill 
    248      1.1  jmcneill #ifdef MULTIPROCESSOR
    249      1.1  jmcneill 		if (!ih->ih_mpsafe) {
    250      1.1  jmcneill 			KERNEL_LOCK(1, NULL);
    251      1.1  jmcneill 			rv |= ih->ih_func(ih->ih_arg);
    252      1.1  jmcneill 			KERNEL_UNLOCK_ONE(NULL);
    253      1.1  jmcneill 		} else
    254      1.1  jmcneill #endif
    255      1.1  jmcneill 			rv |= ih->ih_func(ih->ih_arg);
    256      1.1  jmcneill 
    257      1.1  jmcneill 		pend &= ~ih->ih_mask;
    258      1.1  jmcneill 	}
    259      1.1  jmcneill 
    260      1.1  jmcneill 	return rv;
    261      1.1  jmcneill }
    262