Home | History | Annotate | Line # | Download | only in pci
pci_kn20aa.c revision 1.7
      1 /*	$NetBSD: pci_kn20aa.c,v 1.7 1996/07/14 04:08:56 cgd Exp $	*/
      2 
      3 /*
      4  * Copyright (c) 1995, 1996 Carnegie-Mellon University.
      5  * All rights reserved.
      6  *
      7  * Author: Chris G. Demetriou
      8  *
      9  * Permission to use, copy, modify and distribute this software and
     10  * its documentation is hereby granted, provided that both the copyright
     11  * notice and this permission notice appear in all copies of the
     12  * software, derivative works or modified versions, and any portions
     13  * thereof, and that both notices appear in supporting documentation.
     14  *
     15  * CARNEGIE MELLON ALLOWS FREE USE OF THIS SOFTWARE IN ITS "AS IS"
     16  * CONDITION.  CARNEGIE MELLON DISCLAIMS ANY LIABILITY OF ANY KIND
     17  * FOR ANY DAMAGES WHATSOEVER RESULTING FROM THE USE OF THIS SOFTWARE.
     18  *
     19  * Carnegie Mellon requests users of this software to return to
     20  *
     21  *  Software Distribution Coordinator  or  Software.Distribution (at) CS.CMU.EDU
     22  *  School of Computer Science
     23  *  Carnegie Mellon University
     24  *  Pittsburgh PA 15213-3890
     25  *
     26  * any improvements or extensions that they make and grant Carnegie the
     27  * rights to redistribute these changes.
     28  */
     29 
     30 #include <sys/types.h>
     31 #include <sys/param.h>
     32 #include <sys/time.h>
     33 #include <sys/systm.h>
     34 #include <sys/errno.h>
     35 #include <sys/malloc.h>
     36 #include <sys/device.h>
     37 #include <sys/syslog.h>
     38 
     39 #include <vm/vm.h>
     40 
     41 #include <machine/autoconf.h>
     42 
     43 #include <dev/pci/pcireg.h>
     44 #include <dev/pci/pcivar.h>
     45 
     46 #include <alpha/pci/ciareg.h>
     47 #include <alpha/pci/ciavar.h>
     48 
     49 #include <alpha/pci/pci_kn20aa.h>
     50 
     51 #ifndef EVCNT_COUNTERS
     52 #include <machine/intrcnt.h>
     53 #endif
     54 
     55 #include "sio.h"
     56 #if NSIO
     57 #include <alpha/pci/siovar.h>
     58 #endif
     59 
     60 int	dec_kn20aa_intr_map __P((void *, pcitag_t, int, int,
     61 	    pci_intr_handle_t *));
     62 const char *dec_kn20aa_intr_string __P((void *, pci_intr_handle_t));
     63 void	*dec_kn20aa_intr_establish __P((void *, pci_intr_handle_t,
     64 	    int, int (*func)(void *), void *));
     65 void	dec_kn20aa_intr_disestablish __P((void *, void *));
     66 
     67 #define	KN20AA_PCEB_IRQ	31
     68 #define	KN20AA_MAX_IRQ	32
     69 #define	PCI_STRAY_MAX	5
     70 
     71 struct kn20aa_intrhand {
     72 	TAILQ_ENTRY(kn20aa_intrhand) ih_q;
     73         int     (*ih_fun)();
     74         void    *ih_arg;
     75         u_long  ih_count;
     76         int     ih_level;
     77 };
     78 TAILQ_HEAD(kn20aa_intrchain, kn20aa_intrhand);
     79 
     80 struct kn20aa_intrchain kn20aa_pci_intrs[KN20AA_MAX_IRQ];
     81 int	kn20aa_pci_strayintrcnt[KN20AA_MAX_IRQ];
     82 #ifdef EVCNT_COUNTERS
     83 struct evcnt kn20aa_intr_evcnt;
     84 #endif
     85 
     86 void	kn20aa_pci_strayintr __P((int irq));
     87 void	kn20aa_iointr __P((void *framep, unsigned long vec));
     88 void	kn20aa_enable_intr __P((int irq));
     89 struct kn20aa_intrhand *kn20aa_attach_intr __P((struct kn20aa_intrchain *,
     90 			    int, int (*) (void *), void *));
     91 
     92 void
     93 pci_kn20aa_pickintr(ccp)
     94 	struct cia_config *ccp;
     95 {
     96 	int i;
     97 	struct kn20aa_intrhand *nintrhand;
     98 	bus_chipset_tag_t bc = &ccp->cc_bc;
     99 	pci_chipset_tag_t pc = &ccp->cc_pc;
    100 
    101 	for (i = 0; i < KN20AA_MAX_IRQ; i++)
    102 		TAILQ_INIT(&kn20aa_pci_intrs[i]);
    103 
    104         pc->pc_intr_v = ccp;
    105         pc->pc_intr_map = dec_kn20aa_intr_map;
    106         pc->pc_intr_string = dec_kn20aa_intr_string;
    107         pc->pc_intr_establish = dec_kn20aa_intr_establish;
    108         pc->pc_intr_disestablish = dec_kn20aa_intr_disestablish;
    109 
    110 #if NSIO
    111 	sio_intr_setup(bc);
    112 #endif
    113 
    114 	set_iointr(kn20aa_iointr);
    115 
    116 #if NSIO
    117 	kn20aa_enable_intr(KN20AA_PCEB_IRQ);
    118 #if 0 /* XXX init PCEB interrupt handler? */
    119 	kn20aa_attach_intr(&kn20aa_pci_intrs[KN20AA_PCEB_IRQ], ???, ???, ???);
    120 #endif
    121 #endif
    122 }
    123 
    124 int
    125 dec_kn20aa_intr_map(ccv, bustag, buspin, line, ihp)
    126         void *ccv;
    127         pcitag_t bustag;
    128         int buspin, line;
    129         pci_intr_handle_t *ihp;
    130 {
    131 	struct cia_config *ccp = ccv;
    132 	pci_chipset_tag_t pc = &ccp->cc_pc;
    133 	int device;
    134 	int kn20aa_slot, kn20aa_irq;
    135 	void *ih;
    136 
    137         if (buspin == 0) {
    138                 /* No IRQ used. */
    139                 return 0;
    140         }
    141         if (buspin > 4) {
    142                 printf("pci_map_int: bad interrupt pin %d\n", buspin);
    143                 return NULL;
    144         }
    145 
    146 	/*
    147 	 * Slot->interrupt translation.  Appears to work, though it
    148 	 * may not hold up forever.
    149 	 *
    150 	 * The DEC engineers who did this hardware obviously engaged
    151 	 * in random drug testing.
    152 	 */
    153 	pci_decompose_tag(pc, bustag, NULL, &device, NULL);
    154 	switch (device) {
    155 	case 11:
    156 	case 12:
    157 		kn20aa_slot = (device - 11) + 0;
    158 		break;
    159 
    160 	case 7:
    161 		kn20aa_slot = 2;
    162 		break;
    163 
    164 	case 8:
    165 		kn20aa_slot = 4;
    166 		break;
    167 
    168 	case 9:
    169 		kn20aa_slot = 3;
    170 		break;
    171 
    172 	default:
    173 		panic("pci_kn20aa_map_int: invalid device number %d\n",
    174 		    device);
    175 	}
    176 
    177 	kn20aa_irq = (kn20aa_slot * 4) + buspin - 1;
    178 	if (kn20aa_irq > KN20AA_MAX_IRQ)
    179 		panic("pci_kn20aa_map_int: kn20aa_irq too large (%d)\n",
    180 		    kn20aa_irq);
    181 
    182 	*ihp = kn20aa_irq;
    183 }
    184 
    185 const char *
    186 dec_kn20aa_intr_string(ccv, ih)
    187 	void *ccv;
    188 	pci_intr_handle_t ih;
    189 {
    190 	struct cia_config *ccp = ccv;
    191         static char irqstr[15];          /* 11 + 2 + NULL + sanity */
    192 
    193         if (ih > KN20AA_MAX_IRQ)
    194                 panic("dec_kn20aa_a50_intr_string: bogus kn20aa IRQ 0x%x\n",
    195 		    ih);
    196 
    197         sprintf(irqstr, "kn20aa irq %d", ih);
    198         return (irqstr);
    199 }
    200 
    201 void *
    202 dec_kn20aa_intr_establish(ccv, ih, level, func, arg)
    203         void *ccv, *arg;
    204         pci_intr_handle_t ih;
    205         int level;
    206         int (*func) __P((void *));
    207 {
    208         struct cia_config *ccp = ccv;
    209 	void *cookie;
    210 
    211         if (ih > KN20AA_MAX_IRQ)
    212                 panic("dec_kn20aa_intr_establish: bogus kn20aa IRQ 0x%x\n",
    213 		    ih);
    214 
    215 	cookie = kn20aa_attach_intr(&kn20aa_pci_intrs[ih], level, func, arg);
    216 	kn20aa_enable_intr(ih);
    217 	return (cookie);
    218 }
    219 
    220 void
    221 dec_kn20aa_intr_disestablish(ccv, cookie)
    222         void *ccv, *cookie;
    223 {
    224 	struct cia_config *ccp = ccv;
    225 
    226 	panic("dec_kn20aa_intr_disestablish not implemented"); /* XXX */
    227 }
    228 
    229 /*
    230  * caught a stray interrupt; notify if not too many seen already.
    231  */
    232 void
    233 kn20aa_pci_strayintr(irq)
    234 	int irq;
    235 {
    236 
    237 	if (++kn20aa_pci_strayintrcnt[irq] <= PCI_STRAY_MAX)
    238 		log(LOG_ERR, "stray PCI interrupt %d%s\n", irq,
    239 		    kn20aa_pci_strayintrcnt[irq] >= PCI_STRAY_MAX ?
    240 		    "; stopped logging" : "");
    241 }
    242 
    243 void
    244 kn20aa_iointr(framep, vec)
    245 	void *framep;
    246 	unsigned long vec;
    247 {
    248 	struct kn20aa_intrhand *ih;
    249 	int irq, handled;
    250 
    251 	if (vec >= 0x900) {
    252 		if (vec >= 0x900 + (KN20AA_MAX_IRQ << 4))
    253 			panic("kn20aa_iointr: vec 0x%x out of range\n", vec);
    254 		irq = (vec - 0x900) >> 4;
    255 
    256 #ifdef EVCNT_COUNTERS
    257 		kn20aa_intr_evcnt.ev_count++;
    258 #else
    259 		if (KN20AA_MAX_IRQ != INTRCNT_KN20AA_IRQ_LEN)
    260 			panic("kn20aa interrupt counter sizes inconsistent");
    261 		intrcnt[INTRCNT_KN20AA_IRQ + irq]++;
    262 #endif
    263 
    264 		for (ih = kn20aa_pci_intrs[irq].tqh_first, handled = 0;
    265 		    ih != NULL; ih = ih->ih_q.tqe_next) {
    266 			int rv;
    267 
    268 			rv = (*ih->ih_fun)(ih->ih_arg);
    269 
    270 			ih->ih_count++;
    271 			handled = handled || (rv != 0);
    272 		}
    273 		if (!handled)
    274 			kn20aa_pci_strayintr(irq);
    275 		return;
    276 	}
    277 	if (vec >= 0x800) {
    278 #if NSIO
    279 		sio_iointr(framep, vec);
    280 #endif
    281 		return;
    282 	}
    283 	panic("kn20aa_iointr: weird vec 0x%x\n", vec);
    284 }
    285 
    286 void
    287 kn20aa_enable_intr(irq)
    288 	int irq;
    289 {
    290 
    291 	/*
    292 	 * From disassembling the OSF/1 source code:
    293 	 * the following appears to enable a given interrupt request.
    294 	 * "blech."  I'd give valuable body parts for better docs or
    295 	 * for a good decompiler.
    296 	 */
    297 	alpha_mb();
    298 	REGVAL(0x8780000000L + 0x40L) |= (1 << irq);	/* XXX */
    299 	alpha_mb();
    300 }
    301 
    302 struct kn20aa_intrhand *
    303 kn20aa_attach_intr(chain, level, func, arg)
    304 	struct kn20aa_intrchain *chain;
    305 	int level;
    306 	int (*func) __P((void *));
    307 	void *arg;
    308 {
    309 	struct kn20aa_intrhand *nintrhand;
    310 
    311 	nintrhand = (struct kn20aa_intrhand *)
    312 	    malloc(sizeof *nintrhand, M_DEVBUF, M_WAITOK);
    313 
    314         nintrhand->ih_fun = func;
    315         nintrhand->ih_arg = arg;
    316         nintrhand->ih_count = 0;
    317         nintrhand->ih_level = level;
    318 	TAILQ_INSERT_TAIL(chain, nintrhand, ih_q);
    319 
    320 	return (nintrhand);
    321 }
    322