Home | History | Annotate | Line # | Download | only in common
shared_intr.c revision 1.23
      1  1.23  thorpej /* $NetBSD: shared_intr.c,v 1.23 2020/09/22 15:24:01 thorpej Exp $ */
      2   1.1      cgd 
      3   1.1      cgd /*
      4   1.1      cgd  * Copyright (c) 1996 Carnegie-Mellon University.
      5   1.1      cgd  * All rights reserved.
      6   1.1      cgd  *
      7   1.1      cgd  * Authors: Chris G. Demetriou
      8  1.21     matt  *
      9   1.1      cgd  * Permission to use, copy, modify and distribute this software and
     10   1.1      cgd  * its documentation is hereby granted, provided that both the copyright
     11   1.1      cgd  * notice and this permission notice appear in all copies of the
     12   1.1      cgd  * software, derivative works or modified versions, and any portions
     13   1.1      cgd  * thereof, and that both notices appear in supporting documentation.
     14  1.21     matt  *
     15  1.21     matt  * CARNEGIE MELLON ALLOWS FREE USE OF THIS SOFTWARE IN ITS "AS IS"
     16  1.21     matt  * CONDITION.  CARNEGIE MELLON DISCLAIMS ANY LIABILITY OF ANY KIND
     17   1.1      cgd  * FOR ANY DAMAGES WHATSOEVER RESULTING FROM THE USE OF THIS SOFTWARE.
     18  1.21     matt  *
     19   1.1      cgd  * Carnegie Mellon requests users of this software to return to
     20   1.1      cgd  *
     21   1.1      cgd  *  Software Distribution Coordinator  or  Software.Distribution (at) CS.CMU.EDU
     22   1.1      cgd  *  School of Computer Science
     23   1.1      cgd  *  Carnegie Mellon University
     24   1.1      cgd  *  Pittsburgh PA 15213-3890
     25   1.1      cgd  *
     26   1.1      cgd  * any improvements or extensions that they make and grant Carnegie the
     27   1.1      cgd  * rights to redistribute these changes.
     28   1.1      cgd  */
     29   1.1      cgd 
     30   1.1      cgd /*
     31   1.1      cgd  * Common shared-interrupt-line functionality.
     32   1.1      cgd  */
     33   1.2      cgd 
     34   1.3      cgd #include <sys/cdefs.h>			/* RCS ID & Copyright macro defns */
     35   1.3      cgd 
     36  1.23  thorpej __KERNEL_RCSID(0, "$NetBSD: shared_intr.c,v 1.23 2020/09/22 15:24:01 thorpej Exp $");
     37   1.1      cgd 
     38   1.1      cgd #include <sys/param.h>
     39   1.7  thorpej #include <sys/kernel.h>
     40   1.1      cgd #include <sys/systm.h>
     41   1.1      cgd #include <sys/malloc.h>
     42   1.1      cgd #include <sys/syslog.h>
     43   1.1      cgd #include <sys/queue.h>
     44  1.19       ad #include <sys/atomic.h>
     45  1.19       ad #include <sys/intr.h>
     46   1.1      cgd 
     47  1.20      dsl static const char *intr_typename(int);
     48   1.1      cgd 
     49   1.1      cgd static const char *
     50  1.14  thorpej intr_typename(int type)
     51   1.1      cgd {
     52   1.1      cgd 
     53   1.1      cgd 	switch (type) {
     54   1.1      cgd 	case IST_UNUSABLE:
     55   1.1      cgd 		return ("disabled");
     56   1.1      cgd 	case IST_NONE:
     57   1.1      cgd 		return ("none");
     58   1.1      cgd 	case IST_PULSE:
     59   1.1      cgd 		return ("pulsed");
     60   1.1      cgd 	case IST_EDGE:
     61   1.1      cgd 		return ("edge-triggered");
     62   1.1      cgd 	case IST_LEVEL:
     63   1.1      cgd 		return ("level-triggered");
     64   1.1      cgd 	}
     65   1.1      cgd 	panic("intr_typename: unknown type %d", type);
     66   1.1      cgd }
     67   1.1      cgd 
     68   1.1      cgd struct alpha_shared_intr *
     69  1.15  thorpej alpha_shared_intr_alloc(unsigned int n, unsigned int namesize)
     70   1.1      cgd {
     71   1.1      cgd 	struct alpha_shared_intr *intr;
     72   1.1      cgd 	unsigned int i;
     73   1.1      cgd 
     74   1.1      cgd 	intr = malloc(n * sizeof (struct alpha_shared_intr), M_DEVBUF,
     75  1.22      chs 	    M_WAITOK);
     76   1.1      cgd 	for (i = 0; i < n; i++) {
     77   1.1      cgd 		TAILQ_INIT(&intr[i].intr_q);
     78   1.1      cgd 		intr[i].intr_sharetype = IST_NONE;
     79   1.1      cgd 		intr[i].intr_dfltsharetype = IST_NONE;
     80   1.1      cgd 		intr[i].intr_nstrays = 0;
     81   1.1      cgd 		intr[i].intr_maxstrays = 5;
     82  1.11   mjacob 		intr[i].intr_private = NULL;
     83  1.15  thorpej 		if (namesize != 0) {
     84  1.15  thorpej 			intr[i].intr_string = malloc(namesize, M_DEVBUF,
     85  1.22      chs 			    M_WAITOK);
     86  1.15  thorpej 		} else
     87  1.15  thorpej 			intr[i].intr_string = NULL;
     88   1.1      cgd 	}
     89   1.1      cgd 
     90   1.1      cgd 	return (intr);
     91   1.1      cgd }
     92   1.1      cgd 
     93   1.1      cgd int
     94  1.14  thorpej alpha_shared_intr_dispatch(struct alpha_shared_intr *intr, unsigned int num)
     95   1.1      cgd {
     96   1.1      cgd 	struct alpha_shared_intrhand *ih;
     97   1.1      cgd 	int rv, handled;
     98   1.1      cgd 
     99  1.19       ad 	atomic_add_long(&intr[num].intr_evcnt.ev_count, 1);
    100  1.15  thorpej 
    101   1.1      cgd 	ih = intr[num].intr_q.tqh_first;
    102   1.1      cgd 	handled = 0;
    103   1.1      cgd 	while (ih != NULL) {
    104   1.1      cgd 
    105   1.1      cgd 		/*
    106   1.1      cgd 		 * The handler returns one of three values:
    107   1.1      cgd 		 *   0:	This interrupt wasn't for me.
    108   1.1      cgd 		 *   1: This interrupt was for me.
    109   1.1      cgd 		 *  -1: This interrupt might have been for me, but I can't say
    110   1.1      cgd 		 *      for sure.
    111   1.1      cgd 		 */
    112  1.23  thorpej 
    113  1.23  thorpej 		if (!ih->ih_mpsafe) {
    114  1.23  thorpej 			KERNEL_LOCK(1, NULL);
    115  1.23  thorpej 			rv = (*ih->ih_fn)(ih->ih_arg);
    116  1.23  thorpej 			KERNEL_UNLOCK_ONE(NULL);
    117  1.23  thorpej 		} else {
    118  1.23  thorpej 			rv = (*ih->ih_fn)(ih->ih_arg);
    119  1.23  thorpej 		}
    120   1.1      cgd 
    121   1.1      cgd 		handled = handled || (rv != 0);
    122   1.1      cgd 		ih = ih->ih_q.tqe_next;
    123   1.1      cgd 	}
    124   1.1      cgd 
    125   1.1      cgd 	return (handled);
    126   1.1      cgd }
    127   1.1      cgd 
    128   1.1      cgd void *
    129  1.14  thorpej alpha_shared_intr_establish(struct alpha_shared_intr *intr, unsigned int num,
    130  1.23  thorpej     int type, int level, int flags,
    131  1.23  thorpej     int (*fn)(void *), void *arg, const char *basename)
    132   1.1      cgd {
    133   1.1      cgd 	struct alpha_shared_intrhand *ih;
    134   1.1      cgd 
    135   1.1      cgd 	if (intr[num].intr_sharetype == IST_UNUSABLE) {
    136   1.1      cgd 		printf("alpha_shared_intr_establish: %s %d: unusable\n",
    137   1.1      cgd 		    basename, num);
    138   1.1      cgd 		return NULL;
    139   1.1      cgd 	}
    140   1.1      cgd 
    141  1.22      chs 	ih = malloc(sizeof *ih, M_DEVBUF, M_WAITOK);
    142   1.1      cgd #ifdef DIAGNOSTIC
    143   1.1      cgd 	if (type == IST_NONE)
    144   1.1      cgd 		panic("alpha_shared_intr_establish: bogus type");
    145   1.1      cgd #endif
    146   1.1      cgd 
    147   1.1      cgd 	switch (intr[num].intr_sharetype) {
    148   1.1      cgd 	case IST_EDGE:
    149   1.1      cgd 	case IST_LEVEL:
    150   1.1      cgd 		if (type == intr[num].intr_sharetype)
    151   1.1      cgd 			break;
    152   1.1      cgd 	case IST_PULSE:
    153   1.1      cgd 		if (type != IST_NONE) {
    154  1.10  thorpej 			if (intr[num].intr_q.tqh_first == NULL) {
    155   1.1      cgd 				printf("alpha_shared_intr_establish: %s %d: warning: using %s on %s\n",
    156   1.1      cgd 				    basename, num, intr_typename(type),
    157   1.1      cgd 				    intr_typename(intr[num].intr_sharetype));
    158   1.1      cgd 				type = intr[num].intr_sharetype;
    159   1.1      cgd 			} else {
    160   1.1      cgd 				panic("alpha_shared_intr_establish: %s %d: can't share %s with %s",
    161   1.1      cgd 				    basename, num, intr_typename(type),
    162   1.1      cgd 				    intr_typename(intr[num].intr_sharetype));
    163   1.1      cgd 			}
    164   1.1      cgd 		}
    165   1.1      cgd 		break;
    166   1.1      cgd 
    167   1.1      cgd 	case IST_NONE:
    168   1.1      cgd 		/* not currently used; safe */
    169   1.1      cgd 		break;
    170   1.1      cgd 	}
    171   1.1      cgd 
    172  1.13  thorpej 	ih->ih_intrhead = intr;
    173   1.1      cgd 	ih->ih_fn = fn;
    174   1.1      cgd 	ih->ih_arg = arg;
    175   1.1      cgd 	ih->ih_level = level;
    176   1.6  thorpej 	ih->ih_num = num;
    177  1.23  thorpej 	ih->ih_mpsafe = (flags & ALPHA_INTR_MPSAFE) != 0;
    178   1.1      cgd 
    179   1.1      cgd 	intr[num].intr_sharetype = type;
    180   1.1      cgd 	TAILQ_INSERT_TAIL(&intr[num].intr_q, ih, ih_q);
    181   1.1      cgd 
    182   1.1      cgd 	return (ih);
    183   1.6  thorpej }
    184   1.6  thorpej 
    185   1.6  thorpej void
    186  1.14  thorpej alpha_shared_intr_disestablish(struct alpha_shared_intr *intr, void *cookie,
    187  1.14  thorpej     const char *basename)
    188   1.6  thorpej {
    189   1.6  thorpej 	struct alpha_shared_intrhand *ih = cookie;
    190   1.6  thorpej 	unsigned int num = ih->ih_num;
    191   1.6  thorpej 
    192   1.6  thorpej 	/*
    193   1.6  thorpej 	 * Just remove it from the list and free the entry.  We let
    194   1.6  thorpej 	 * the caller deal with resetting the share type, if appropriate.
    195   1.6  thorpej 	 */
    196   1.6  thorpej 	TAILQ_REMOVE(&intr[num].intr_q, ih, ih_q);
    197   1.1      cgd }
    198   1.1      cgd 
    199   1.1      cgd int
    200  1.14  thorpej alpha_shared_intr_get_sharetype(struct alpha_shared_intr *intr,
    201  1.14  thorpej     unsigned int num)
    202   1.1      cgd {
    203   1.1      cgd 
    204   1.1      cgd 	return (intr[num].intr_sharetype);
    205   1.1      cgd }
    206   1.1      cgd 
    207   1.1      cgd int
    208  1.14  thorpej alpha_shared_intr_isactive(struct alpha_shared_intr *intr, unsigned int num)
    209   1.1      cgd {
    210   1.1      cgd 
    211   1.1      cgd 	return (intr[num].intr_q.tqh_first != NULL);
    212  1.16  thorpej }
    213  1.16  thorpej 
    214  1.16  thorpej int
    215  1.16  thorpej alpha_shared_intr_firstactive(struct alpha_shared_intr *intr, unsigned int num)
    216  1.16  thorpej {
    217  1.16  thorpej 
    218  1.16  thorpej 	return (intr[num].intr_q.tqh_first != NULL &&
    219  1.16  thorpej 		intr[num].intr_q.tqh_first->ih_q.tqe_next == NULL);
    220   1.1      cgd }
    221   1.1      cgd 
    222   1.1      cgd void
    223  1.14  thorpej alpha_shared_intr_set_dfltsharetype(struct alpha_shared_intr *intr,
    224  1.14  thorpej     unsigned int num, int newdfltsharetype)
    225   1.1      cgd {
    226   1.1      cgd 
    227   1.1      cgd #ifdef DIAGNOSTIC
    228   1.1      cgd 	if (alpha_shared_intr_isactive(intr, num))
    229   1.1      cgd 		panic("alpha_shared_intr_set_dfltsharetype on active intr");
    230   1.1      cgd #endif
    231   1.1      cgd 
    232   1.1      cgd 	intr[num].intr_dfltsharetype = newdfltsharetype;
    233   1.1      cgd 	intr[num].intr_sharetype = intr[num].intr_dfltsharetype;
    234   1.1      cgd }
    235   1.1      cgd 
    236   1.1      cgd void
    237  1.14  thorpej alpha_shared_intr_set_maxstrays(struct alpha_shared_intr *intr,
    238  1.14  thorpej     unsigned int num, int newmaxstrays)
    239   1.1      cgd {
    240  1.12   mjacob 	int s = splhigh();
    241   1.1      cgd 	intr[num].intr_maxstrays = newmaxstrays;
    242   1.1      cgd 	intr[num].intr_nstrays = 0;
    243  1.12   mjacob 	splx(s);
    244   1.1      cgd }
    245   1.1      cgd 
    246   1.1      cgd void
    247  1.17  thorpej alpha_shared_intr_reset_strays(struct alpha_shared_intr *intr,
    248  1.17  thorpej     unsigned int num)
    249  1.17  thorpej {
    250  1.17  thorpej 
    251  1.17  thorpej 	/*
    252  1.17  thorpej 	 * Don't bother blocking interrupts; this doesn't have to be
    253  1.17  thorpej 	 * precise, but it does need to be fast.
    254  1.17  thorpej 	 */
    255  1.17  thorpej 	intr[num].intr_nstrays = 0;
    256  1.17  thorpej }
    257  1.17  thorpej 
    258  1.17  thorpej void
    259  1.14  thorpej alpha_shared_intr_stray(struct alpha_shared_intr *intr, unsigned int num,
    260  1.14  thorpej     const char *basename)
    261   1.1      cgd {
    262   1.1      cgd 
    263   1.1      cgd 	intr[num].intr_nstrays++;
    264   1.5  thorpej 
    265   1.5  thorpej 	if (intr[num].intr_maxstrays == 0)
    266   1.5  thorpej 		return;
    267   1.5  thorpej 
    268   1.1      cgd 	if (intr[num].intr_nstrays <= intr[num].intr_maxstrays)
    269   1.1      cgd 		log(LOG_ERR, "stray %s %d%s\n", basename, num,
    270   1.1      cgd 		    intr[num].intr_nstrays >= intr[num].intr_maxstrays ?
    271   1.1      cgd 		      "; stopped logging" : "");
    272   1.8  thorpej }
    273   1.8  thorpej 
    274   1.8  thorpej void
    275  1.14  thorpej alpha_shared_intr_set_private(struct alpha_shared_intr *intr,
    276  1.14  thorpej     unsigned int num, void *v)
    277   1.8  thorpej {
    278   1.8  thorpej 
    279   1.8  thorpej 	intr[num].intr_private = v;
    280   1.8  thorpej }
    281   1.8  thorpej 
    282   1.8  thorpej void *
    283  1.14  thorpej alpha_shared_intr_get_private(struct alpha_shared_intr *intr,
    284  1.14  thorpej     unsigned int num)
    285   1.8  thorpej {
    286   1.8  thorpej 
    287   1.8  thorpej 	return (intr[num].intr_private);
    288  1.15  thorpej }
    289  1.15  thorpej 
    290  1.15  thorpej struct evcnt *
    291  1.15  thorpej alpha_shared_intr_evcnt(struct alpha_shared_intr *intr,
    292  1.15  thorpej     unsigned int num)
    293  1.15  thorpej {
    294  1.15  thorpej 
    295  1.15  thorpej 	return (&intr[num].intr_evcnt);
    296  1.15  thorpej }
    297  1.15  thorpej 
    298  1.15  thorpej char *
    299  1.15  thorpej alpha_shared_intr_string(struct alpha_shared_intr *intr,
    300  1.15  thorpej     unsigned int num)
    301  1.15  thorpej {
    302  1.15  thorpej 
    303  1.15  thorpej 	return (intr[num].intr_string);
    304   1.1      cgd }
    305