Home | History | Annotate | Line # | Download | only in common
shared_intr.c revision 1.9
      1  1.9  thorpej /* $NetBSD: shared_intr.c,v 1.9 1999/12/07 21:36:16 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.1      cgd  *
      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.1      cgd  *
     15  1.1      cgd  * CARNEGIE MELLON ALLOWS FREE USE OF THIS SOFTWARE IN ITS "AS IS"
     16  1.1      cgd  * 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.1      cgd  *
     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.9  thorpej __KERNEL_RCSID(0, "$NetBSD: shared_intr.c,v 1.9 1999/12/07 21:36:16 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.1      cgd 
     45  1.1      cgd #include <machine/intr.h>
     46  1.1      cgd 
     47  1.1      cgd static const char *intr_typename __P((int));
     48  1.1      cgd 
     49  1.1      cgd static const char *
     50  1.1      cgd intr_typename(type)
     51  1.1      cgd 	int type;
     52  1.1      cgd {
     53  1.1      cgd 
     54  1.1      cgd 	switch (type) {
     55  1.1      cgd 	case IST_UNUSABLE:
     56  1.1      cgd 		return ("disabled");
     57  1.1      cgd 	case IST_NONE:
     58  1.1      cgd 		return ("none");
     59  1.1      cgd 	case IST_PULSE:
     60  1.1      cgd 		return ("pulsed");
     61  1.1      cgd 	case IST_EDGE:
     62  1.1      cgd 		return ("edge-triggered");
     63  1.1      cgd 	case IST_LEVEL:
     64  1.1      cgd 		return ("level-triggered");
     65  1.1      cgd 	}
     66  1.1      cgd 	panic("intr_typename: unknown type %d", type);
     67  1.1      cgd }
     68  1.1      cgd 
     69  1.1      cgd struct alpha_shared_intr *
     70  1.1      cgd alpha_shared_intr_alloc(n)
     71  1.1      cgd 	unsigned int n;
     72  1.1      cgd {
     73  1.1      cgd 	struct alpha_shared_intr *intr;
     74  1.1      cgd 	unsigned int i;
     75  1.1      cgd 
     76  1.1      cgd 	intr = malloc(n * sizeof (struct alpha_shared_intr), M_DEVBUF,
     77  1.1      cgd 	    cold ? M_NOWAIT : M_WAITOK);
     78  1.1      cgd 	if (intr == NULL)
     79  1.1      cgd 		panic("alpha_shared_intr_alloc: couldn't malloc intr");
     80  1.1      cgd 
     81  1.1      cgd 	for (i = 0; i < n; i++) {
     82  1.1      cgd 		TAILQ_INIT(&intr[i].intr_q);
     83  1.1      cgd 		intr[i].intr_sharetype = IST_NONE;
     84  1.1      cgd 		intr[i].intr_dfltsharetype = IST_NONE;
     85  1.1      cgd 		intr[i].intr_nstrays = 0;
     86  1.1      cgd 		intr[i].intr_maxstrays = 5;
     87  1.1      cgd 	}
     88  1.1      cgd 
     89  1.1      cgd 	return (intr);
     90  1.1      cgd }
     91  1.1      cgd 
     92  1.1      cgd int
     93  1.1      cgd alpha_shared_intr_dispatch(intr, num)
     94  1.1      cgd 	struct alpha_shared_intr *intr;
     95  1.1      cgd 	unsigned int num;
     96  1.1      cgd {
     97  1.1      cgd 	struct alpha_shared_intrhand *ih;
     98  1.1      cgd 	int rv, handled;
     99  1.1      cgd 
    100  1.1      cgd 	ih = intr[num].intr_q.tqh_first;
    101  1.1      cgd 	handled = 0;
    102  1.1      cgd 	while (ih != NULL) {
    103  1.1      cgd 
    104  1.1      cgd 		/*
    105  1.1      cgd 		 * The handler returns one of three values:
    106  1.1      cgd 		 *   0:	This interrupt wasn't for me.
    107  1.1      cgd 		 *   1: This interrupt was for me.
    108  1.1      cgd 		 *  -1: This interrupt might have been for me, but I can't say
    109  1.1      cgd 		 *      for sure.
    110  1.1      cgd 		 */
    111  1.1      cgd 		rv = (*ih->ih_fn)(ih->ih_arg);
    112  1.1      cgd 
    113  1.1      cgd 		handled = handled || (rv != 0);
    114  1.1      cgd 		ih = ih->ih_q.tqe_next;
    115  1.1      cgd 	}
    116  1.1      cgd 
    117  1.1      cgd 	return (handled);
    118  1.1      cgd }
    119  1.1      cgd 
    120  1.1      cgd void *
    121  1.1      cgd alpha_shared_intr_establish(intr, num, type, level, fn, arg, basename)
    122  1.1      cgd 	struct alpha_shared_intr *intr;
    123  1.1      cgd 	unsigned int num;
    124  1.1      cgd 	int type, level;
    125  1.1      cgd 	int (*fn) __P((void *));
    126  1.1      cgd 	void *arg;
    127  1.1      cgd 	const char *basename;
    128  1.1      cgd {
    129  1.1      cgd 	struct alpha_shared_intrhand *ih;
    130  1.1      cgd 
    131  1.1      cgd 	if (intr[num].intr_sharetype == IST_UNUSABLE) {
    132  1.1      cgd 		printf("alpha_shared_intr_establish: %s %d: unusable\n",
    133  1.1      cgd 		    basename, num);
    134  1.1      cgd 		return NULL;
    135  1.1      cgd 	}
    136  1.1      cgd 
    137  1.1      cgd 	/* no point in sleeping unless someone can free memory. */
    138  1.1      cgd 	ih = malloc(sizeof *ih, M_DEVBUF, cold ? M_NOWAIT : M_WAITOK);
    139  1.1      cgd 	if (ih == NULL)
    140  1.1      cgd 		panic("alpha_shared_intr_establish: can't malloc intrhand");
    141  1.1      cgd 
    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.9  thorpej 			if (intr[num].intr_q.tqh_first == NULL &&
    155  1.9  thorpej 			    type != intr[num].intr_sharetype) {
    156  1.1      cgd 				printf("alpha_shared_intr_establish: %s %d: warning: using %s on %s\n",
    157  1.1      cgd 				    basename, num, intr_typename(type),
    158  1.1      cgd 				    intr_typename(intr[num].intr_sharetype));
    159  1.1      cgd 				type = intr[num].intr_sharetype;
    160  1.1      cgd 			} else {
    161  1.1      cgd 				panic("alpha_shared_intr_establish: %s %d: can't share %s with %s",
    162  1.1      cgd 				    basename, num, intr_typename(type),
    163  1.1      cgd 				    intr_typename(intr[num].intr_sharetype));
    164  1.1      cgd 			}
    165  1.1      cgd 		}
    166  1.1      cgd 		break;
    167  1.1      cgd 
    168  1.1      cgd 	case IST_NONE:
    169  1.1      cgd 		/* not currently used; safe */
    170  1.1      cgd 		break;
    171  1.1      cgd 	}
    172  1.1      cgd 
    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.1      cgd 
    178  1.1      cgd 	intr[num].intr_sharetype = type;
    179  1.1      cgd 	TAILQ_INSERT_TAIL(&intr[num].intr_q, ih, ih_q);
    180  1.1      cgd 
    181  1.1      cgd 	return (ih);
    182  1.6  thorpej }
    183  1.6  thorpej 
    184  1.6  thorpej void
    185  1.6  thorpej alpha_shared_intr_disestablish(intr, cookie, basename)
    186  1.6  thorpej 	struct alpha_shared_intr *intr;
    187  1.6  thorpej 	void *cookie;
    188  1.6  thorpej 	const char *basename;
    189  1.6  thorpej {
    190  1.6  thorpej 	struct alpha_shared_intrhand *ih = cookie;
    191  1.6  thorpej 	unsigned int num = ih->ih_num;
    192  1.6  thorpej 
    193  1.6  thorpej 	/*
    194  1.6  thorpej 	 * Just remove it from the list and free the entry.  We let
    195  1.6  thorpej 	 * the caller deal with resetting the share type, if appropriate.
    196  1.6  thorpej 	 */
    197  1.6  thorpej 	TAILQ_REMOVE(&intr[num].intr_q, ih, ih_q);
    198  1.1      cgd }
    199  1.1      cgd 
    200  1.1      cgd int
    201  1.1      cgd alpha_shared_intr_get_sharetype(intr, num)
    202  1.1      cgd 	struct alpha_shared_intr *intr;
    203  1.1      cgd 	unsigned int num;
    204  1.1      cgd {
    205  1.1      cgd 
    206  1.1      cgd 	return (intr[num].intr_sharetype);
    207  1.1      cgd }
    208  1.1      cgd 
    209  1.1      cgd int
    210  1.1      cgd alpha_shared_intr_isactive(intr, num)
    211  1.1      cgd 	struct alpha_shared_intr *intr;
    212  1.1      cgd 	unsigned int num;
    213  1.1      cgd {
    214  1.1      cgd 
    215  1.1      cgd 	return (intr[num].intr_q.tqh_first != NULL);
    216  1.1      cgd }
    217  1.1      cgd 
    218  1.1      cgd void
    219  1.1      cgd alpha_shared_intr_set_dfltsharetype(intr, num, newdfltsharetype)
    220  1.1      cgd 	struct alpha_shared_intr *intr;
    221  1.1      cgd 	unsigned int num;
    222  1.1      cgd 	int newdfltsharetype;
    223  1.1      cgd {
    224  1.1      cgd 
    225  1.1      cgd #ifdef DIAGNOSTIC
    226  1.1      cgd 	if (alpha_shared_intr_isactive(intr, num))
    227  1.1      cgd 		panic("alpha_shared_intr_set_dfltsharetype on active intr");
    228  1.1      cgd #endif
    229  1.1      cgd 
    230  1.1      cgd 	intr[num].intr_dfltsharetype = newdfltsharetype;
    231  1.1      cgd 	intr[num].intr_sharetype = intr[num].intr_dfltsharetype;
    232  1.1      cgd }
    233  1.1      cgd 
    234  1.1      cgd void
    235  1.1      cgd alpha_shared_intr_set_maxstrays(intr, num, newmaxstrays)
    236  1.1      cgd 	struct alpha_shared_intr *intr;
    237  1.1      cgd 	unsigned int num;
    238  1.1      cgd 	int newmaxstrays;
    239  1.1      cgd {
    240  1.1      cgd 
    241  1.1      cgd #ifdef DIAGNOSTIC
    242  1.1      cgd 	if (alpha_shared_intr_isactive(intr, num))
    243  1.1      cgd 		panic("alpha_shared_intr_set_maxstrays on active intr");
    244  1.1      cgd #endif
    245  1.1      cgd 
    246  1.1      cgd 	intr[num].intr_maxstrays = newmaxstrays;
    247  1.1      cgd 	intr[num].intr_nstrays = 0;
    248  1.1      cgd }
    249  1.1      cgd 
    250  1.1      cgd void
    251  1.1      cgd alpha_shared_intr_stray(intr, num, basename)
    252  1.1      cgd 	struct alpha_shared_intr *intr;
    253  1.1      cgd 	unsigned int num;
    254  1.1      cgd 	const char *basename;
    255  1.1      cgd {
    256  1.1      cgd 
    257  1.1      cgd 	intr[num].intr_nstrays++;
    258  1.5  thorpej 
    259  1.5  thorpej 	if (intr[num].intr_maxstrays == 0)
    260  1.5  thorpej 		return;
    261  1.5  thorpej 
    262  1.1      cgd 	if (intr[num].intr_nstrays <= intr[num].intr_maxstrays)
    263  1.1      cgd 		log(LOG_ERR, "stray %s %d%s\n", basename, num,
    264  1.1      cgd 		    intr[num].intr_nstrays >= intr[num].intr_maxstrays ?
    265  1.1      cgd 		      "; stopped logging" : "");
    266  1.8  thorpej }
    267  1.8  thorpej 
    268  1.8  thorpej void
    269  1.8  thorpej alpha_shared_intr_set_private(intr, num, v)
    270  1.8  thorpej 	struct alpha_shared_intr *intr;
    271  1.8  thorpej 	unsigned int num;
    272  1.8  thorpej 	void *v;
    273  1.8  thorpej {
    274  1.8  thorpej 
    275  1.8  thorpej 	intr[num].intr_private = v;
    276  1.8  thorpej }
    277  1.8  thorpej 
    278  1.8  thorpej void *
    279  1.8  thorpej alpha_shared_intr_get_private(intr, num)
    280  1.8  thorpej 	struct alpha_shared_intr *intr;
    281  1.8  thorpej 	unsigned int num;
    282  1.8  thorpej {
    283  1.8  thorpej 
    284  1.8  thorpej 	return (intr[num].intr_private);
    285  1.1      cgd }
    286