Home | History | Annotate | Line # | Download | only in pci
pci_kn20aa.c revision 1.19
      1 /*	$NetBSD: pci_kn20aa.c,v 1.19 1996/10/23 04:12:28 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 void	kn20aa_disable_intr __P((int irq));
     90 struct kn20aa_intrhand *kn20aa_attach_intr __P((struct kn20aa_intrchain *,
     91 			    int, int (*) (void *), void *));
     92 
     93 void
     94 pci_kn20aa_pickintr(ccp)
     95 	struct cia_config *ccp;
     96 {
     97 	int i;
     98 	struct kn20aa_intrhand *nintrhand;
     99 	bus_space_tag_t iot = ccp->cc_iot;
    100 	pci_chipset_tag_t pc = &ccp->cc_pc;
    101 
    102 	for (i = 0; i < KN20AA_MAX_IRQ; i++)
    103 		TAILQ_INIT(&kn20aa_pci_intrs[i]);
    104 
    105         pc->pc_intr_v = ccp;
    106         pc->pc_intr_map = dec_kn20aa_intr_map;
    107         pc->pc_intr_string = dec_kn20aa_intr_string;
    108         pc->pc_intr_establish = dec_kn20aa_intr_establish;
    109         pc->pc_intr_disestablish = dec_kn20aa_intr_disestablish;
    110 
    111 #if NSIO
    112 	sio_intr_setup(iot);
    113 #endif
    114 
    115 	set_iointr(kn20aa_iointr);
    116 
    117 #if NSIO
    118 	kn20aa_enable_intr(KN20AA_PCEB_IRQ);
    119 #if 0 /* XXX init PCEB interrupt handler? */
    120 	kn20aa_attach_intr(&kn20aa_pci_intrs[KN20AA_PCEB_IRQ], ???, ???, ???);
    121 #endif
    122 #endif
    123 }
    124 
    125 int
    126 dec_kn20aa_intr_map(ccv, bustag, buspin, line, ihp)
    127         void *ccv;
    128         pcitag_t bustag;
    129         int buspin, line;
    130         pci_intr_handle_t *ihp;
    131 {
    132 	struct cia_config *ccp = ccv;
    133 	pci_chipset_tag_t pc = &ccp->cc_pc;
    134 	int device;
    135 	int kn20aa_irq;
    136 	void *ih;
    137 
    138         if (buspin == 0) {
    139                 /* No IRQ used. */
    140                 return 1;
    141         }
    142         if (buspin > 4) {
    143                 printf("pci_map_int: bad interrupt pin %d\n", buspin);
    144                 return 1;
    145         }
    146 
    147 	/*
    148 	 * Slot->interrupt translation.  Appears to work, though it
    149 	 * may not hold up forever.
    150 	 *
    151 	 * The DEC engineers who did this hardware obviously engaged
    152 	 * in random drug testing.
    153 	 */
    154 	pci_decompose_tag(pc, bustag, NULL, &device, NULL);
    155 	switch (device) {
    156 	case 11:
    157 	case 12:
    158 		kn20aa_irq = ((device - 11) + 0) * 4;
    159 		break;
    160 
    161 	case 7:
    162 		kn20aa_irq = 8;
    163 		break;
    164 
    165 	case 9:
    166 		kn20aa_irq = 12;
    167 		break;
    168 
    169 	case 6:					/* 21040 on AlphaStation 500 */
    170 		kn20aa_irq = 13;
    171 		break;
    172 
    173 	case 8:
    174 		kn20aa_irq = 16;
    175 		break;
    176 
    177 	default:
    178 #ifdef KN20AA_BOGUS_IRQ_FROB
    179 		*ihp = 0xdeadbeef;
    180 		printf("\n\n BOGUS INTERRUPT MAPPING: dev %d, pin %d\n",
    181 		    device, buspin);
    182 		return (0);
    183 #endif
    184 		panic("pci_kn20aa_map_int: invalid device number %d\n",
    185 		    device);
    186 	}
    187 
    188 	kn20aa_irq += buspin - 1;
    189 	if (kn20aa_irq > KN20AA_MAX_IRQ)
    190 		panic("pci_kn20aa_map_int: kn20aa_irq too large (%d)\n",
    191 		    kn20aa_irq);
    192 
    193 	*ihp = kn20aa_irq;
    194 	return (0);
    195 }
    196 
    197 const char *
    198 dec_kn20aa_intr_string(ccv, ih)
    199 	void *ccv;
    200 	pci_intr_handle_t ih;
    201 {
    202 	struct cia_config *ccp = ccv;
    203         static char irqstr[15];          /* 11 + 2 + NULL + sanity */
    204 
    205 #ifdef KN20AA_BOGUS_IRQ_FROB
    206 	if (ih == 0xdeadbeef) {
    207 		sprintf(irqstr, "BOGUS");
    208 		return (irqstr);
    209 	}
    210 #endif
    211         if (ih > KN20AA_MAX_IRQ)
    212                 panic("dec_kn20aa_a50_intr_string: bogus kn20aa IRQ 0x%x\n",
    213 		    ih);
    214 
    215         sprintf(irqstr, "kn20aa irq %d", ih);
    216         return (irqstr);
    217 }
    218 
    219 void *
    220 dec_kn20aa_intr_establish(ccv, ih, level, func, arg)
    221         void *ccv, *arg;
    222         pci_intr_handle_t ih;
    223         int level;
    224         int (*func) __P((void *));
    225 {
    226         struct cia_config *ccp = ccv;
    227 	void *cookie;
    228 
    229 #ifdef KN20AA_BOGUS_IRQ_FROB
    230 	if (ih == 0xdeadbeef) {
    231 		int i;
    232 		char chars[10];
    233 
    234 		printf("dec_kn20aa_intr_establish: BOGUS IRQ\n");
    235 		do {
    236 			printf("IRQ to enable? ");
    237 			getstr(chars, 10);
    238 			i = atoi(chars);
    239 		} while (i < 0 || i > 32);
    240 		printf("ENABLING IRQ %d\n", i);
    241 		kn20aa_enable_intr(i);
    242 		return ((void *)0xbabefacedeadbeef);
    243 	}
    244 #endif
    245         if (ih > KN20AA_MAX_IRQ)
    246                 panic("dec_kn20aa_intr_establish: bogus kn20aa IRQ 0x%x\n",
    247 		    ih);
    248 
    249 	cookie = kn20aa_attach_intr(&kn20aa_pci_intrs[ih], level, func, arg);
    250 	kn20aa_enable_intr(ih);
    251 	return (cookie);
    252 }
    253 
    254 void
    255 dec_kn20aa_intr_disestablish(ccv, cookie)
    256         void *ccv, *cookie;
    257 {
    258 	struct cia_config *ccp = ccv;
    259 
    260 	panic("dec_kn20aa_intr_disestablish not implemented"); /* XXX */
    261 }
    262 
    263 /*
    264  * caught a stray interrupt; notify if not too many seen already.
    265  */
    266 void
    267 kn20aa_pci_strayintr(irq)
    268 	int irq;
    269 {
    270 
    271 	kn20aa_pci_strayintrcnt[irq]++;
    272 	if (kn20aa_pci_strayintrcnt[irq] == PCI_STRAY_MAX)
    273 		kn20aa_disable_intr(irq);
    274 
    275 	log(LOG_ERR, "stray kn20aa irq %d\n", irq);
    276 	if (kn20aa_pci_strayintrcnt[irq] == PCI_STRAY_MAX)
    277 		log(LOG_ERR, "disabling interrupts on kn20aa irq %d\n", irq);
    278 }
    279 
    280 void
    281 kn20aa_iointr(framep, vec)
    282 	void *framep;
    283 	unsigned long vec;
    284 {
    285 	struct kn20aa_intrhand *ih;
    286 	int irq, handled;
    287 
    288 	if (vec >= 0x900) {
    289 		if (vec >= 0x900 + (KN20AA_MAX_IRQ << 4))
    290 			panic("kn20aa_iointr: vec 0x%x out of range\n", vec);
    291 		irq = (vec - 0x900) >> 4;
    292 
    293 #ifdef EVCNT_COUNTERS
    294 		kn20aa_intr_evcnt.ev_count++;
    295 #else
    296 		if (KN20AA_MAX_IRQ != INTRCNT_KN20AA_IRQ_LEN)
    297 			panic("kn20aa interrupt counter sizes inconsistent");
    298 		intrcnt[INTRCNT_KN20AA_IRQ + irq]++;
    299 #endif
    300 
    301 		for (ih = kn20aa_pci_intrs[irq].tqh_first, handled = 0;
    302 		    ih != NULL; ih = ih->ih_q.tqe_next) {
    303 			int rv;
    304 
    305 			rv = (*ih->ih_fun)(ih->ih_arg);
    306 
    307 			ih->ih_count++;
    308 			handled = handled || (rv != 0);
    309 		}
    310 		if (!handled)
    311 			kn20aa_pci_strayintr(irq);
    312 		return;
    313 	}
    314 	if (vec >= 0x800) {
    315 #if NSIO
    316 		sio_iointr(framep, vec);
    317 #endif
    318 		return;
    319 	}
    320 	panic("kn20aa_iointr: weird vec 0x%x\n", vec);
    321 }
    322 
    323 void
    324 kn20aa_enable_intr(irq)
    325 	int irq;
    326 {
    327 
    328 	/*
    329 	 * From disassembling small bits of the OSF/1 kernel:
    330 	 * the following appears to enable a given interrupt request.
    331 	 * "blech."  I'd give valuable body parts for better docs or
    332 	 * for a good decompiler.
    333 	 */
    334 	alpha_mb();
    335 	REGVAL(0x8780000000L + 0x40L) |= (1 << irq);	/* XXX */
    336 	alpha_mb();
    337 }
    338 
    339 void
    340 kn20aa_disable_intr(irq)
    341 	int irq;
    342 {
    343 
    344 	alpha_mb();
    345 	REGVAL(0x8780000000L + 0x40L) &= ~(1 << irq);	/* XXX */
    346 	alpha_mb();
    347 }
    348 
    349 struct kn20aa_intrhand *
    350 kn20aa_attach_intr(chain, level, func, arg)
    351 	struct kn20aa_intrchain *chain;
    352 	int level;
    353 	int (*func) __P((void *));
    354 	void *arg;
    355 {
    356 	struct kn20aa_intrhand *nintrhand;
    357 
    358 	nintrhand = (struct kn20aa_intrhand *)
    359 	    malloc(sizeof *nintrhand, M_DEVBUF, M_WAITOK);
    360 
    361         nintrhand->ih_fun = func;
    362         nintrhand->ih_arg = arg;
    363         nintrhand->ih_count = 0;
    364         nintrhand->ih_level = level;
    365 	TAILQ_INSERT_TAIL(chain, nintrhand, ih_q);
    366 
    367 	return (nintrhand);
    368 }
    369