Home | History | Annotate | Line # | Download | only in atheros
ar_intr.c revision 1.1
      1  1.1  matt /* $NetBSD: ar_intr.c,v 1.1 2011/07/07 05:06:44 matt Exp $ */
      2  1.1  matt /*
      3  1.1  matt  * Copyright (c) 2006 Urbana-Champaign Independent Media Center.
      4  1.1  matt  * Copyright (c) 2006 Garrett D'Amore.
      5  1.1  matt  * All rights reserved.
      6  1.1  matt  *
      7  1.1  matt  * This code was written by Garrett D'Amore for the Champaign-Urbana
      8  1.1  matt  * Community Wireless Network Project.
      9  1.1  matt  *
     10  1.1  matt  * Redistribution and use in source and binary forms, with or
     11  1.1  matt  * without modification, are permitted provided that the following
     12  1.1  matt  * conditions are met:
     13  1.1  matt  * 1. Redistributions of source code must retain the above copyright
     14  1.1  matt  *    notice, this list of conditions and the following disclaimer.
     15  1.1  matt  * 2. Redistributions in binary form must reproduce the above
     16  1.1  matt  *    copyright notice, this list of conditions and the following
     17  1.1  matt  *    disclaimer in the documentation and/or other materials provided
     18  1.1  matt  *    with the distribution.
     19  1.1  matt  * 3. All advertising materials mentioning features or use of this
     20  1.1  matt  *    software must display the following acknowledgements:
     21  1.1  matt  *      This product includes software developed by the Urbana-Champaign
     22  1.1  matt  *      Independent Media Center.
     23  1.1  matt  *	This product includes software developed by Garrett D'Amore.
     24  1.1  matt  * 4. Urbana-Champaign Independent Media Center's name and Garrett
     25  1.1  matt  *    D'Amore's name may not be used to endorse or promote products
     26  1.1  matt  *    derived from this software without specific prior written permission.
     27  1.1  matt  *
     28  1.1  matt  * THIS SOFTWARE IS PROVIDED BY THE URBANA-CHAMPAIGN INDEPENDENT
     29  1.1  matt  * MEDIA CENTER AND GARRETT D'AMORE ``AS IS'' AND ANY EXPRESS OR
     30  1.1  matt  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
     31  1.1  matt  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     32  1.1  matt  * ARE DISCLAIMED.  IN NO EVENT SHALL THE URBANA-CHAMPAIGN INDEPENDENT
     33  1.1  matt  * MEDIA CENTER OR GARRETT D'AMORE BE LIABLE FOR ANY DIRECT, INDIRECT,
     34  1.1  matt  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
     35  1.1  matt  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
     36  1.1  matt  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
     37  1.1  matt  * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
     38  1.1  matt  * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     39  1.1  matt  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
     40  1.1  matt  * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     41  1.1  matt  */
     42  1.1  matt 
     43  1.1  matt #include <sys/cdefs.h>
     44  1.1  matt __KERNEL_RCSID(0, "$NetBSD: ar_intr.c,v 1.1 2011/07/07 05:06:44 matt Exp $");
     45  1.1  matt 
     46  1.1  matt #define __INTR_PRIVATE
     47  1.1  matt 
     48  1.1  matt #include <sys/param.h>
     49  1.1  matt #include <sys/queue.h>
     50  1.1  matt #include <sys/malloc.h>
     51  1.1  matt #include <sys/systm.h>
     52  1.1  matt #include <sys/device.h>
     53  1.1  matt #include <sys/kernel.h>
     54  1.1  matt 
     55  1.1  matt #include <machine/bus.h>
     56  1.1  matt #include <machine/intr.h>
     57  1.1  matt 
     58  1.1  matt #include <mips/locore.h>
     59  1.1  matt #include <mips/atheros/include/platform.h>
     60  1.1  matt 
     61  1.1  matt #define	REGVAL(x)	*((volatile uint32_t *)(MIPS_PHYS_TO_KSEG1((x))))
     62  1.1  matt 
     63  1.1  matt /*
     64  1.1  matt  * Only MISC interrupts are easily masked at the interrupt controller.
     65  1.1  matt  * The others have to be masked at the source.
     66  1.1  matt  */
     67  1.1  matt 
     68  1.1  matt #define	NINTRS	7	/* MIPS INT2-INT4 (7 is clock interrupt) */
     69  1.1  matt #define	NIRQS	32	/* bits in Miscellaneous Interrupt Status Register */
     70  1.1  matt 
     71  1.1  matt struct atheros_intrhand {
     72  1.1  matt 	LIST_ENTRY(atheros_intrhand) ih_q;
     73  1.1  matt 	int (*ih_func)(void *);
     74  1.1  matt 	void *ih_arg;
     75  1.1  matt 	int ih_irq;
     76  1.1  matt };
     77  1.1  matt 
     78  1.1  matt struct atheros_intr {
     79  1.1  matt 	LIST_HEAD(, atheros_intrhand) intr_qh;
     80  1.1  matt 	struct evcnt	intr_count;
     81  1.1  matt };
     82  1.1  matt 
     83  1.1  matt static struct atheros_intr cpu_intrs[NINTRS];
     84  1.1  matt static struct atheros_intr misc_intrs[NIRQS];
     85  1.1  matt 
     86  1.1  matt static uint32_t
     87  1.1  matt misc_intstat_get(void)
     88  1.1  matt {
     89  1.1  matt 	return REGVAL(platformsw->apsw_misc_intstat);
     90  1.1  matt }
     91  1.1  matt 
     92  1.1  matt static void
     93  1.1  matt misc_intstat_put(uint32_t v)
     94  1.1  matt {
     95  1.1  matt 	REGVAL(platformsw->apsw_misc_intstat) = v;
     96  1.1  matt }
     97  1.1  matt 
     98  1.1  matt static uint32_t
     99  1.1  matt misc_intmask_get(void)
    100  1.1  matt {
    101  1.1  matt 	return REGVAL(platformsw->apsw_misc_intmask);
    102  1.1  matt }
    103  1.1  matt 
    104  1.1  matt static void
    105  1.1  matt misc_intmask_put(uint32_t v)
    106  1.1  matt {
    107  1.1  matt 	REGVAL(platformsw->apsw_misc_intmask) = v;
    108  1.1  matt }
    109  1.1  matt 
    110  1.1  matt 
    111  1.1  matt static void *
    112  1.1  matt genath_cpu_intr_establish(int intr, int (*func)(void *), void *arg)
    113  1.1  matt {
    114  1.1  matt 	struct atheros_intrhand	*ih;
    115  1.1  matt 
    116  1.1  matt 	if ((ih = malloc(sizeof(*ih), M_DEVBUF, M_NOWAIT)) == NULL)
    117  1.1  matt 		return NULL;
    118  1.1  matt 
    119  1.1  matt 	ih->ih_func = func;
    120  1.1  matt 	ih->ih_arg = arg;
    121  1.1  matt 	ih->ih_irq = intr;
    122  1.1  matt 
    123  1.1  matt 	if (ih == NULL)
    124  1.1  matt 		return NULL;
    125  1.1  matt 
    126  1.1  matt 	const int s = splhigh();
    127  1.1  matt 
    128  1.1  matt 	LIST_INSERT_HEAD(&cpu_intrs[intr].intr_qh, ih, ih_q);
    129  1.1  matt 
    130  1.1  matt 	/*
    131  1.1  matt 	 * The MIPS CPU interrupts are enabled at boot time, so they
    132  1.1  matt 	 * should pretty much always be ready to go.
    133  1.1  matt 	 */
    134  1.1  matt 
    135  1.1  matt 	splx(s);
    136  1.1  matt 	return (ih);
    137  1.1  matt }
    138  1.1  matt 
    139  1.1  matt static void
    140  1.1  matt genath_cpu_intr_disestablish(void *arg)
    141  1.1  matt {
    142  1.1  matt 	struct atheros_intrhand	* const ih = arg;
    143  1.1  matt 
    144  1.1  matt 	const int s = splhigh();
    145  1.1  matt 
    146  1.1  matt 	LIST_REMOVE(ih, ih_q);
    147  1.1  matt 
    148  1.1  matt 	splx(s);
    149  1.1  matt 	free(ih, M_DEVBUF);
    150  1.1  matt }
    151  1.1  matt 
    152  1.1  matt static void *
    153  1.1  matt genath_misc_intr_establish(int irq, int (*func)(void *), void *arg)
    154  1.1  matt {
    155  1.1  matt 	struct atheros_intr * const intr = &misc_intrs[irq];
    156  1.1  matt 	struct atheros_intrhand	*ih;
    157  1.1  matt 	bool first;
    158  1.1  matt 	int s;
    159  1.1  matt 
    160  1.1  matt 
    161  1.1  matt 	if ((ih = malloc(sizeof(*ih), M_DEVBUF, M_NOWAIT)) == NULL)
    162  1.1  matt 		return NULL;
    163  1.1  matt 
    164  1.1  matt 	ih->ih_func = func;
    165  1.1  matt 	ih->ih_arg = arg;
    166  1.1  matt 	ih->ih_irq = irq;
    167  1.1  matt 
    168  1.1  matt 	s = splhigh();
    169  1.1  matt 
    170  1.1  matt 	first = LIST_EMPTY(&intr->intr_qh);
    171  1.1  matt 
    172  1.1  matt 	LIST_INSERT_HEAD(&intr->intr_qh, ih, ih_q);
    173  1.1  matt 
    174  1.1  matt 	if (first) {
    175  1.1  matt 		const uint32_t mask = misc_intmask_get() | __BIT(irq);
    176  1.1  matt 		misc_intmask_put(mask);
    177  1.1  matt 		(void) misc_intmask_get();	/* flush wbuffer */
    178  1.1  matt 	}
    179  1.1  matt 
    180  1.1  matt 	splx(s);
    181  1.1  matt 
    182  1.1  matt 	return ih;
    183  1.1  matt }
    184  1.1  matt 
    185  1.1  matt static void
    186  1.1  matt genath_misc_intr_disestablish(void *arg)
    187  1.1  matt {
    188  1.1  matt 	struct atheros_intrhand	*ih = arg;
    189  1.1  matt 	struct atheros_intr * const intr = &misc_intrs[ih->ih_irq];
    190  1.1  matt 
    191  1.1  matt 	const int s = splhigh();
    192  1.1  matt 
    193  1.1  matt 	LIST_REMOVE(ih, ih_q);
    194  1.1  matt 	if (LIST_EMPTY(&intr->intr_qh)) {
    195  1.1  matt 		const uint32_t mask = misc_intmask_get() & ~__BIT(ih->ih_irq);
    196  1.1  matt 		misc_intmask_put(mask);
    197  1.1  matt 		(void) misc_intmask_get();	/* flush wbuffer */
    198  1.1  matt 	}
    199  1.1  matt 
    200  1.1  matt 	splx(s);
    201  1.1  matt 	free(ih, M_DEVBUF);
    202  1.1  matt }
    203  1.1  matt 
    204  1.1  matt 
    205  1.1  matt static int
    206  1.1  matt genath_misc_intr(void *arg)
    207  1.1  matt {
    208  1.1  matt 	uint32_t		isr;
    209  1.1  matt 	uint32_t		mask;
    210  1.1  matt 	int			rv = 0;
    211  1.1  matt 	struct atheros_intr	*intr = arg;
    212  1.1  matt 
    213  1.1  matt 	isr = misc_intstat_get();
    214  1.1  matt 	mask = misc_intmask_get();
    215  1.1  matt 
    216  1.1  matt 	misc_intstat_put(isr & ~mask);
    217  1.1  matt 
    218  1.1  matt 	isr &= mask;
    219  1.1  matt 	while (isr != 0) {
    220  1.1  matt 		struct atheros_intrhand	*ih;
    221  1.1  matt 		int index = 31 - __builtin_clz(isr & -isr); /* ffs */
    222  1.1  matt 		intr += index;
    223  1.1  matt 
    224  1.1  matt 		intr->intr_count.ev_count++;
    225  1.1  matt 		LIST_FOREACH(ih, &intr->intr_qh, ih_q) {
    226  1.1  matt 			rv |= (*ih->ih_func)(ih->ih_arg);
    227  1.1  matt 		}
    228  1.1  matt 		isr >>= index + 1;
    229  1.1  matt 		intr++;
    230  1.1  matt 	}
    231  1.1  matt 
    232  1.1  matt 	return rv;
    233  1.1  matt }
    234  1.1  matt 
    235  1.1  matt static void
    236  1.1  matt genath_iointr(int cpl, vaddr_t pc, uint32_t ipending)
    237  1.1  matt {
    238  1.1  matt 	struct atheros_intr *intr = &cpu_intrs[NINTRS-1];
    239  1.1  matt 
    240  1.1  matt 	/* move ipending to the most significant bits */
    241  1.1  matt 	ipending *= __BIT(31) / (MIPS_INT_MASK_0 << (NINTRS-1));
    242  1.1  matt 	while (ipending != 0) {
    243  1.1  matt 		struct atheros_intrhand	*ih;
    244  1.1  matt 		int index = __builtin_clz(ipending);
    245  1.1  matt 
    246  1.1  matt 		intr -= index;
    247  1.1  matt 		ipending <<= index;
    248  1.1  matt 		KASSERT(ipending & __BIT(31));
    249  1.1  matt 		KASSERT(intr >= cpu_intrs);
    250  1.1  matt 
    251  1.1  matt 		intr->intr_count.ev_count++;
    252  1.1  matt 		LIST_FOREACH(ih, &intr->intr_qh, ih_q) {
    253  1.1  matt 			(*ih->ih_func)(ih->ih_arg);
    254  1.1  matt 		}
    255  1.1  matt 		ipending <<= 1;
    256  1.1  matt 		intr--;
    257  1.1  matt 	}
    258  1.1  matt }
    259  1.1  matt 
    260  1.1  matt static void
    261  1.1  matt genath_intr_init(void)
    262  1.1  matt {
    263  1.1  matt 	const struct atheros_platformsw * const apsw = platformsw;
    264  1.1  matt 
    265  1.1  matt 	KASSERT(apsw->apsw_ipl_sr_map != NULL);
    266  1.1  matt 	ipl_sr_map = *apsw->apsw_ipl_sr_map;
    267  1.1  matt 
    268  1.1  matt 	for (size_t i = 0; i < apsw->apsw_cpu_nintrs; i++) {
    269  1.1  matt 		if (apsw->apsw_cpu_intrnames[i] != NULL) {
    270  1.1  matt 			LIST_INIT(&cpu_intrs[i].intr_qh);
    271  1.1  matt 			evcnt_attach_dynamic(&cpu_intrs[i].intr_count,
    272  1.1  matt 			    EVCNT_TYPE_INTR, NULL, "cpu",
    273  1.1  matt 			    apsw->apsw_cpu_intrnames[i]);
    274  1.1  matt 		}
    275  1.1  matt 	}
    276  1.1  matt 
    277  1.1  matt 	for (size_t i = 0; i < apsw->apsw_misc_nintrs; i++) {
    278  1.1  matt 		if (apsw->apsw_misc_intrnames[i] != NULL) {
    279  1.1  matt 			LIST_INIT(&misc_intrs[i].intr_qh);
    280  1.1  matt 			evcnt_attach_dynamic(&misc_intrs[i].intr_count,
    281  1.1  matt 			    EVCNT_TYPE_INTR, NULL, "misc",
    282  1.1  matt 			    apsw->apsw_misc_intrnames[i]);
    283  1.1  matt 		}
    284  1.1  matt 	}
    285  1.1  matt 
    286  1.1  matt 	/* make sure we start without any misc interrupts enabled */
    287  1.1  matt 	(void) misc_intstat_get();
    288  1.1  matt 	misc_intmask_put(0);
    289  1.1  matt 	misc_intstat_put(0);
    290  1.1  matt 
    291  1.1  matt 	/* make sure we register the MISC interrupt handler */
    292  1.1  matt 	genath_cpu_intr_establish(apsw->apsw_cpuirq_misc,
    293  1.1  matt 	    genath_misc_intr, misc_intrs);
    294  1.1  matt }
    295  1.1  matt 
    296  1.1  matt 
    297  1.1  matt const struct atheros_intrsw atheros_intrsw = {
    298  1.1  matt 	.aisw_init = genath_intr_init,
    299  1.1  matt 	.aisw_cpu_establish = genath_cpu_intr_establish,
    300  1.1  matt 	.aisw_cpu_disestablish = genath_cpu_intr_disestablish,
    301  1.1  matt 	.aisw_misc_establish = genath_misc_intr_establish,
    302  1.1  matt 	.aisw_misc_disestablish = genath_misc_intr_disestablish,
    303  1.1  matt 	.aisw_iointr = genath_iointr,
    304  1.1  matt };
    305  1.1  matt 
    306