Home | History | Annotate | Line # | Download | only in pci
sio_pic.c revision 1.23.8.1
      1 /* $NetBSD: sio_pic.c,v 1.23.8.1 1999/08/02 19:35:20 thorpej Exp $ */
      2 
      3 /*-
      4  * Copyright (c) 1998 The NetBSD Foundation, Inc.
      5  * All rights reserved.
      6  *
      7  * This code is derived from software contributed to The NetBSD Foundation
      8  * by Jason R. Thorpe of the Numerical Aerospace Simulation Facility,
      9  * NASA Ames Research Center.
     10  *
     11  * Redistribution and use in source and binary forms, with or without
     12  * modification, are permitted provided that the following conditions
     13  * are met:
     14  * 1. Redistributions of source code must retain the above copyright
     15  *    notice, this list of conditions and the following disclaimer.
     16  * 2. Redistributions in binary form must reproduce the above copyright
     17  *    notice, this list of conditions and the following disclaimer in the
     18  *    documentation and/or other materials provided with the distribution.
     19  * 3. All advertising materials mentioning features or use of this software
     20  *    must display the following acknowledgement:
     21  *	This product includes software developed by the NetBSD
     22  *	Foundation, Inc. and its contributors.
     23  * 4. Neither the name of The NetBSD Foundation nor the names of its
     24  *    contributors may be used to endorse or promote products derived
     25  *    from this software without specific prior written permission.
     26  *
     27  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     28  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     29  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     30  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     31  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     32  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     33  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     34  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     35  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     36  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     37  * POSSIBILITY OF SUCH DAMAGE.
     38  */
     39 
     40 /*
     41  * Copyright (c) 1995, 1996 Carnegie-Mellon University.
     42  * All rights reserved.
     43  *
     44  * Author: Chris G. Demetriou
     45  *
     46  * Permission to use, copy, modify and distribute this software and
     47  * its documentation is hereby granted, provided that both the copyright
     48  * notice and this permission notice appear in all copies of the
     49  * software, derivative works or modified versions, and any portions
     50  * thereof, and that both notices appear in supporting documentation.
     51  *
     52  * CARNEGIE MELLON ALLOWS FREE USE OF THIS SOFTWARE IN ITS "AS IS"
     53  * CONDITION.  CARNEGIE MELLON DISCLAIMS ANY LIABILITY OF ANY KIND
     54  * FOR ANY DAMAGES WHATSOEVER RESULTING FROM THE USE OF THIS SOFTWARE.
     55  *
     56  * Carnegie Mellon requests users of this software to return to
     57  *
     58  *  Software Distribution Coordinator  or  Software.Distribution (at) CS.CMU.EDU
     59  *  School of Computer Science
     60  *  Carnegie Mellon University
     61  *  Pittsburgh PA 15213-3890
     62  *
     63  * any improvements or extensions that they make and grant Carnegie the
     64  * rights to redistribute these changes.
     65  */
     66 
     67 #include <sys/cdefs.h>			/* RCS ID & Copyright macro defns */
     68 
     69 __KERNEL_RCSID(0, "$NetBSD: sio_pic.c,v 1.23.8.1 1999/08/02 19:35:20 thorpej Exp $");
     70 
     71 #include <sys/param.h>
     72 #include <sys/systm.h>
     73 #include <sys/device.h>
     74 #include <sys/malloc.h>
     75 #include <sys/syslog.h>
     76 
     77 #include <machine/intr.h>
     78 #include <machine/bus.h>
     79 
     80 #include <dev/pci/pcireg.h>
     81 #include <dev/pci/pcivar.h>
     82 #include <dev/pci/pcidevs.h>
     83 
     84 #include <dev/isa/isareg.h>
     85 #include <dev/isa/isavar.h>
     86 #include <alpha/pci/siovar.h>
     87 
     88 #ifndef EVCNT_COUNTERS
     89 #include <machine/intrcnt.h>
     90 #endif
     91 
     92 #include "sio.h"
     93 
     94 /*
     95  * To add to the long history of wonderful PROM console traits,
     96  * AlphaStation PROMs don't reset themselves completely on boot!
     97  * Therefore, if an interrupt was turned on when the kernel was
     98  * started, we're not going to EVER turn it off...  I don't know
     99  * what will happen if new interrupts (that the PROM console doesn't
    100  * want) are turned on.  I'll burn that bridge when I come to it.
    101  */
    102 #define	BROKEN_PROM_CONSOLE
    103 
    104 /*
    105  * Private functions and variables.
    106  */
    107 
    108 bus_space_tag_t sio_iot;
    109 pci_chipset_tag_t sio_pc;
    110 bus_space_handle_t sio_ioh_icu1, sio_ioh_icu2, sio_ioh_elcr;
    111 
    112 #define	ICU_LEN		16		/* number of ISA IRQs */
    113 
    114 static struct alpha_shared_intr *sio_intr;
    115 #ifdef EVCNT_COUNTERS
    116 struct evcnt sio_intr_evcnt;
    117 #endif
    118 
    119 #ifndef STRAY_MAX
    120 #ifdef BROKEN_PROM_CONSOLE
    121 /*
    122  * If prom console is broken, because initial interrupt settings
    123  * must be kept, there's no way to escape stray interrupts.
    124  */
    125 #define	STRAY_MAX	0
    126 #else
    127 #define	STRAY_MAX	5
    128 #endif
    129 #endif
    130 
    131 #ifdef BROKEN_PROM_CONSOLE
    132 /*
    133  * If prom console is broken, must remember the initial interrupt
    134  * settings and enforce them.  WHEE!
    135  */
    136 u_int8_t initial_ocw1[2];
    137 u_int8_t initial_elcr[2];
    138 #define	INITIALLY_ENABLED(irq) \
    139 	    ((initial_ocw1[(irq) / 8] & (1 << ((irq) % 8))) == 0)
    140 #define	INITIALLY_LEVEL_TRIGGERED(irq) \
    141 	    ((initial_elcr[(irq) / 8] & (1 << ((irq) % 8))) != 0)
    142 #else
    143 #define	INITIALLY_ENABLED(irq)		((irq) == 2 ? 1 : 0)
    144 #define	INITIALLY_LEVEL_TRIGGERED(irq)	0
    145 #endif
    146 
    147 void		sio_setirqstat __P((int, int, int));
    148 
    149 u_int8_t	(*sio_read_elcr) __P((int));
    150 void		(*sio_write_elcr) __P((int, u_int8_t));
    151 static void	specific_eoi __P((int));
    152 
    153 /******************** i82378 SIO ELCR functions ********************/
    154 
    155 int		i82378_setup_elcr __P((void));
    156 u_int8_t	i82378_read_elcr __P((int));
    157 void		i82378_write_elcr __P((int, u_int8_t));
    158 
    159 int
    160 i82378_setup_elcr()
    161 {
    162 	int rv;
    163 
    164 	/*
    165 	 * We could probe configuration space to see that there's
    166 	 * actually an SIO present, but we are using this as a
    167 	 * fall-back in case nothing else matches.
    168 	 */
    169 
    170 	rv = bus_space_map(sio_iot, 0x4d0, 2, 0, &sio_ioh_elcr);
    171 
    172 	if (rv == 0) {
    173 		sio_read_elcr = i82378_read_elcr;
    174 		sio_write_elcr = i82378_write_elcr;
    175 	}
    176 
    177 	return (rv);
    178 }
    179 
    180 u_int8_t
    181 i82378_read_elcr(elcr)
    182 	int elcr;
    183 {
    184 
    185 	return (bus_space_read_1(sio_iot, sio_ioh_elcr, elcr));
    186 }
    187 
    188 void
    189 i82378_write_elcr(elcr, val)
    190 	int elcr;
    191 	u_int8_t val;
    192 {
    193 
    194 	bus_space_write_1(sio_iot, sio_ioh_elcr, elcr, val);
    195 }
    196 
    197 /******************** Cypress CY82C693 ELCR functions ********************/
    198 
    199 int		cy82c693_setup_elcr __P((void));
    200 u_int8_t	cy82c693_read_elcr __P((int));
    201 void		cy82c693_write_elcr __P((int, u_int8_t));
    202 
    203 int
    204 cy82c693_setup_elcr()
    205 {
    206 	int device, maxndevs;
    207 	pcitag_t tag;
    208 	pcireg_t id;
    209 
    210 	/*
    211 	 * Search PCI configuration space for a Cypress CY82C693.
    212 	 *
    213 	 * Note we can make some assumptions about our bus number
    214 	 * here, because:
    215 	 *
    216 	 *	(1) there can be at most one ISA/EISA bridge per PCI bus, and
    217 	 *
    218 	 *	(2) any ISA/EISA bridges must be attached to primary PCI
    219 	 *	    busses (i.e. bus zero).
    220 	 */
    221 
    222 	maxndevs = pci_bus_maxdevs(sio_pc, 0);
    223 
    224 	for (device = 0; device < maxndevs; device++) {
    225 		tag = pci_make_tag(sio_pc, 0, device, 0);
    226 		id = pci_conf_read(sio_pc, tag, PCI_ID_REG);
    227 
    228 		/* Invalid vendor ID value? */
    229 		if (PCI_VENDOR(id) == PCI_VENDOR_INVALID)
    230 			continue;
    231 		/* XXX Not invalid, but we've done this ~forever. */
    232 		if (PCI_VENDOR(id) == 0)
    233 			continue;
    234 
    235 		if (PCI_VENDOR(id) != PCI_VENDOR_CONTAQ ||
    236 		    PCI_PRODUCT(id) != PCI_PRODUCT_CONTAQ_82C693)
    237 			continue;
    238 
    239 		/*
    240 		 * Found one!
    241 		 */
    242 
    243 #if 0
    244 		printf("cy82c693_setup_elcr: found 82C693 at device %d\n",
    245 		    device);
    246 #endif
    247 
    248 		/*
    249 		 * The CY82C693's ELCR registers are accessed indirectly
    250 		 * via (IO_ICU1 + 2) (address) and (IO_ICU1 + 3) (data).
    251 		 */
    252 		sio_ioh_elcr = sio_ioh_icu1;
    253 
    254 		sio_read_elcr = cy82c693_read_elcr;
    255 		sio_write_elcr = cy82c693_write_elcr;
    256 
    257 		return (0);
    258 	}
    259 
    260 	/*
    261 	 * Didn't find a CY82C693.
    262 	 */
    263 	return (ENODEV);
    264 }
    265 
    266 u_int8_t
    267 cy82c693_read_elcr(elcr)
    268 	int elcr;
    269 {
    270 
    271 	bus_space_write_1(sio_iot, sio_ioh_elcr, 0x02, 0x03 + elcr);
    272 	return (bus_space_read_1(sio_iot, sio_ioh_elcr, 0x03));
    273 }
    274 
    275 void
    276 cy82c693_write_elcr(elcr, val)
    277 	int elcr;
    278 	u_int8_t val;
    279 {
    280 
    281 	bus_space_write_1(sio_iot, sio_ioh_elcr, 0x02, 0x03 + elcr);
    282 	bus_space_write_1(sio_iot, sio_ioh_elcr, 0x03, val);
    283 }
    284 
    285 /******************** ELCR access function configuration ********************/
    286 
    287 /*
    288  * Put the Intel SIO at the end, so we fall back on it if we don't
    289  * find anything else.  If any of the non-Intel functions find a
    290  * matching device, but are unable to map it for whatever reason,
    291  * they should panic.
    292  */
    293 
    294 int (*sio_elcr_setup_funcs[]) __P((void)) = {
    295 	cy82c693_setup_elcr,
    296 	i82378_setup_elcr,
    297 	NULL,
    298 };
    299 
    300 /******************** Shared SIO/Cypress functions ********************/
    301 
    302 void
    303 sio_setirqstat(irq, enabled, type)
    304 	int irq, enabled;
    305 	int type;
    306 {
    307 	u_int8_t ocw1[2], elcr[2];
    308 	int icu, bit;
    309 
    310 #if 0
    311 	printf("sio_setirqstat: irq %d: %s, %s\n", irq,
    312 	    enabled ? "enabled" : "disabled", isa_intr_typename(type));
    313 #endif
    314 
    315 	icu = irq / 8;
    316 	bit = irq % 8;
    317 
    318 	ocw1[0] = bus_space_read_1(sio_iot, sio_ioh_icu1, 1);
    319 	ocw1[1] = bus_space_read_1(sio_iot, sio_ioh_icu2, 1);
    320 	elcr[0] = (*sio_read_elcr)(0);				/* XXX */
    321 	elcr[1] = (*sio_read_elcr)(1);				/* XXX */
    322 
    323 	/*
    324 	 * interrupt enable: set bit to mask (disable) interrupt.
    325 	 */
    326 	if (enabled)
    327 		ocw1[icu] &= ~(1 << bit);
    328 	else
    329 		ocw1[icu] |= 1 << bit;
    330 
    331 	/*
    332 	 * interrupt type select: set bit to get level-triggered.
    333 	 */
    334 	if (type == IST_LEVEL)
    335 		elcr[icu] |= 1 << bit;
    336 	else
    337 		elcr[icu] &= ~(1 << bit);
    338 
    339 #ifdef not_here
    340 	/* see the init function... */
    341 	ocw1[0] &= ~0x04;		/* always enable IRQ2 on first PIC */
    342 	elcr[0] &= ~0x07;		/* IRQ[0-2] must be edge-triggered */
    343 	elcr[1] &= ~0x21;		/* IRQ[13,8] must be edge-triggered */
    344 #endif
    345 
    346 #ifdef BROKEN_PROM_CONSOLE
    347 	/*
    348 	 * make sure that the initially clear bits (unmasked interrupts)
    349 	 * are never set, and that the initially-level-triggered
    350 	 * intrrupts always remain level-triggered, to keep the prom happy.
    351 	 */
    352 	if ((ocw1[0] & ~initial_ocw1[0]) != 0 ||
    353 	    (ocw1[1] & ~initial_ocw1[1]) != 0 ||
    354 	    (elcr[0] & initial_elcr[0]) != initial_elcr[0] ||
    355 	    (elcr[1] & initial_elcr[1]) != initial_elcr[1]) {
    356 		printf("sio_sis: initial: ocw = (%2x,%2x), elcr = (%2x,%2x)\n",
    357 		    initial_ocw1[0], initial_ocw1[1],
    358 		    initial_elcr[0], initial_elcr[1]);
    359 		printf("         current: ocw = (%2x,%2x), elcr = (%2x,%2x)\n",
    360 		    ocw1[0], ocw1[1], elcr[0], elcr[1]);
    361 		panic("sio_setirqstat: hosed");
    362 	}
    363 #endif
    364 
    365 	bus_space_write_1(sio_iot, sio_ioh_icu1, 1, ocw1[0]);
    366 	bus_space_write_1(sio_iot, sio_ioh_icu2, 1, ocw1[1]);
    367 	(*sio_write_elcr)(0, elcr[0]);				/* XXX */
    368 	(*sio_write_elcr)(1, elcr[1]);				/* XXX */
    369 }
    370 
    371 void
    372 sio_intr_setup(pc, iot)
    373 	pci_chipset_tag_t pc;
    374 	bus_space_tag_t iot;
    375 {
    376 	int i;
    377 
    378 	sio_iot = iot;
    379 	sio_pc = pc;
    380 
    381 	if (bus_space_map(sio_iot, IO_ICU1, IO_ICUSIZE, 0, &sio_ioh_icu1) ||
    382 	    bus_space_map(sio_iot, IO_ICU2, IO_ICUSIZE, 0, &sio_ioh_icu2))
    383 		panic("sio_intr_setup: can't map ICU I/O ports");
    384 
    385 	for (i = 0; sio_elcr_setup_funcs[i] != NULL; i++)
    386 		if ((*sio_elcr_setup_funcs[i])() == 0)
    387 			break;
    388 	if (sio_elcr_setup_funcs[i] == NULL)
    389 		panic("sio_intr_setup: can't map ELCR");
    390 
    391 #ifdef BROKEN_PROM_CONSOLE
    392 	/*
    393 	 * Remember the initial values, because the prom is stupid.
    394 	 */
    395 	initial_ocw1[0] = bus_space_read_1(sio_iot, sio_ioh_icu1, 1);
    396 	initial_ocw1[1] = bus_space_read_1(sio_iot, sio_ioh_icu2, 1);
    397 	initial_elcr[0] = (*sio_read_elcr)(0);			/* XXX */
    398 	initial_elcr[1] = (*sio_read_elcr)(1);			/* XXX */
    399 #if 0
    400 	printf("initial_ocw1[0] = 0x%x\n", initial_ocw1[0]);
    401 	printf("initial_ocw1[1] = 0x%x\n", initial_ocw1[1]);
    402 	printf("initial_elcr[0] = 0x%x\n", initial_elcr[0]);
    403 	printf("initial_elcr[1] = 0x%x\n", initial_elcr[1]);
    404 #endif
    405 #endif
    406 
    407 	sio_intr = alpha_shared_intr_alloc(ICU_LEN);
    408 
    409 	/*
    410 	 * set up initial values for interrupt enables.
    411 	 */
    412 	for (i = 0; i < ICU_LEN; i++) {
    413 		alpha_shared_intr_set_maxstrays(sio_intr, i, STRAY_MAX);
    414 
    415 		switch (i) {
    416 		case 0:
    417 		case 1:
    418 		case 8:
    419 		case 13:
    420 			/*
    421 			 * IRQs 0, 1, 8, and 13 must always be
    422 			 * edge-triggered.
    423 			 */
    424 			if (INITIALLY_LEVEL_TRIGGERED(i))
    425 				printf("sio_intr_setup: %d LT!\n", i);
    426 			sio_setirqstat(i, INITIALLY_ENABLED(i), IST_EDGE);
    427 			alpha_shared_intr_set_dfltsharetype(sio_intr, i,
    428 			    IST_EDGE);
    429 
    430 			specific_eoi(i);
    431 			break;
    432 
    433 		case 2:
    434 			/*
    435 			 * IRQ 2 must be edge-triggered, and should be
    436 			 * enabled (otherwise IRQs 8-15 are ignored).
    437 			 */
    438 			if (INITIALLY_LEVEL_TRIGGERED(i))
    439 				printf("sio_intr_setup: %d LT!\n", i);
    440 			if (!INITIALLY_ENABLED(i))
    441 				printf("sio_intr_setup: %d not enabled!\n", i);
    442 			sio_setirqstat(i, 1, IST_EDGE);
    443 			alpha_shared_intr_set_dfltsharetype(sio_intr, i,
    444 			    IST_UNUSABLE);
    445 			break;
    446 
    447 		default:
    448 			/*
    449 			 * Otherwise, disable the IRQ and set its
    450 			 * type to (effectively) "unknown."
    451 			 */
    452 			sio_setirqstat(i, INITIALLY_ENABLED(i),
    453 			    INITIALLY_LEVEL_TRIGGERED(i) ? IST_LEVEL :
    454 				IST_NONE);
    455 			alpha_shared_intr_set_dfltsharetype(sio_intr, i,
    456 			    INITIALLY_LEVEL_TRIGGERED(i) ? IST_LEVEL :
    457                                 IST_NONE);
    458 			specific_eoi(i);
    459 			break;
    460 		}
    461 	}
    462 }
    463 
    464 const char *
    465 sio_intr_string(v, irq)
    466 	void *v;
    467 	int irq;
    468 {
    469 	static char irqstr[12];		/* 8 + 2 + NULL + sanity */
    470 
    471 	if (irq == 0 || irq >= ICU_LEN || irq == 2)
    472 		panic("sio_intr_string: bogus isa irq 0x%x\n", irq);
    473 
    474 	sprintf(irqstr, "isa irq %d", irq);
    475 	return (irqstr);
    476 }
    477 
    478 void *
    479 sio_intr_establish(v, irq, type, level, fn, arg)
    480 	void *v, *arg;
    481         int irq;
    482         int type;
    483         int level;
    484         int (*fn)(void *);
    485 {
    486 	void *cookie;
    487 
    488 	if (irq > ICU_LEN || type == IST_NONE)
    489 		panic("sio_intr_establish: bogus irq or type");
    490 
    491 	cookie = alpha_shared_intr_establish(sio_intr, irq, type, level, fn,
    492 	    arg, "isa irq");
    493 
    494 	if (cookie)
    495 		sio_setirqstat(irq, alpha_shared_intr_isactive(sio_intr, irq),
    496 		    alpha_shared_intr_get_sharetype(sio_intr, irq));
    497 
    498 	return (cookie);
    499 }
    500 
    501 void
    502 sio_intr_disestablish(v, cookie)
    503 	void *v;
    504 	void *cookie;
    505 {
    506 	struct alpha_shared_intrhand *ih = cookie;
    507 	int s, ist, irq = ih->ih_num;
    508 
    509 	s = splhigh();
    510 
    511 	/* Remove it from the link. */
    512 	alpha_shared_intr_disestablish(sio_intr, cookie, "isa irq");
    513 
    514 	/*
    515 	 * Decide if we should disable the interrupt.  We must ensure
    516 	 * that:
    517 	 *
    518 	 *	- An initially-enabled interrupt is never disabled.
    519 	 *	- An initially-LT interrupt is never untyped.
    520 	 */
    521 	if (alpha_shared_intr_isactive(sio_intr, irq) == 0) {
    522 		/*
    523 		 * IRQs 0, 1, 8, and 13 must always be edge-triggered
    524 		 * (see setup).
    525 		 */
    526 		switch (irq) {
    527 		case 0:
    528 		case 1:
    529 		case 8:
    530 		case 13:
    531 			/*
    532 			 * If the interrupt was initially level-triggered
    533 			 * a warning was printed in setup.
    534 			 */
    535 			ist = IST_EDGE;
    536 			break;
    537 
    538 		default:
    539 			ist = INITIALLY_LEVEL_TRIGGERED(irq) ?
    540 			    IST_LEVEL : IST_NONE;
    541 			break;
    542 		}
    543 		sio_setirqstat(irq, INITIALLY_ENABLED(irq), ist);
    544 		alpha_shared_intr_set_dfltsharetype(sio_intr, irq, ist);
    545 	}
    546 
    547 	splx(s);
    548 }
    549 
    550 void
    551 sio_iointr(framep, vec)
    552 	void *framep;
    553 	unsigned long vec;
    554 {
    555 	int irq;
    556 
    557 	irq = (vec - 0x800) >> 4;
    558 #ifdef DIAGNOSTIC
    559 	if (irq > ICU_LEN || irq < 0)
    560 		panic("sio_iointr: irq out of range (%d)", irq);
    561 #endif
    562 
    563 #ifdef EVCNT_COUNTERS
    564 	sio_intr_evcnt.ev_count++;
    565 #else
    566 #ifdef DEBUG
    567 	if (ICU_LEN != INTRCNT_ISA_IRQ_LEN)
    568 		panic("sio interrupt counter sizes inconsistent");
    569 #endif
    570 	intrcnt[INTRCNT_ISA_IRQ + irq]++;
    571 #endif
    572 
    573 	if (!alpha_shared_intr_dispatch(sio_intr, irq))
    574 		alpha_shared_intr_stray(sio_intr, irq, "isa irq");
    575 
    576 	/*
    577 	 * Some versions of the machines which use the SIO
    578 	 * (or is it some PALcode revisions on those machines?)
    579 	 * require the non-specific EOI to be fed to the PIC(s)
    580 	 * by the interrupt handler.
    581 	 */
    582 	specific_eoi(irq);
    583 }
    584 
    585 #define	LEGAL_IRQ(x)	((x) >= 0 && (x) < ICU_LEN && (x) != 2)
    586 
    587 int
    588 sio_intr_alloc(v, mask, type, irq)
    589 	void *v;
    590 	int mask;
    591 	int type;
    592 	int *irq;
    593 {
    594 	int i, tmp, bestirq, count;
    595 	struct alpha_shared_intrhand **p, *q;
    596 
    597 	if (type == IST_NONE)
    598 		panic("intr_alloc: bogus type");
    599 
    600 	bestirq = -1;
    601 	count = -1;
    602 
    603 	/* some interrupts should never be dynamically allocated */
    604 	mask &= 0xdef8;
    605 
    606 	/*
    607 	 * XXX some interrupts will be used later (6 for fdc, 12 for pms).
    608 	 * the right answer is to do "breadth-first" searching of devices.
    609 	 */
    610 	mask &= 0xefbf;
    611 
    612 	for (i = 0; i < ICU_LEN; i++) {
    613 		if (LEGAL_IRQ(i) == 0 || (mask & (1<<i)) == 0)
    614 			continue;
    615 
    616 		switch(sio_intr[i].intr_sharetype) {
    617 		case IST_NONE:
    618 			/*
    619 			 * if nothing's using the irq, just return it
    620 			 */
    621 			*irq = i;
    622 			return (0);
    623 
    624 		case IST_EDGE:
    625 		case IST_LEVEL:
    626 			if (type != sio_intr[i].intr_sharetype)
    627 				continue;
    628 			/*
    629 			 * if the irq is shareable, count the number of other
    630 			 * handlers, and if it's smaller than the last irq like
    631 			 * this, remember it
    632 			 *
    633 			 * XXX We should probably also consider the
    634 			 * interrupt level and stick IPL_TTY with other
    635 			 * IPL_TTY, etc.
    636 			 */
    637 			for (p = &TAILQ_FIRST(&sio_intr[i].intr_q), tmp = 0;
    638 			     (q = *p) != NULL; p = &TAILQ_NEXT(q, ih_q), tmp++)
    639 				;
    640 			if ((bestirq == -1) || (count > tmp)) {
    641 				bestirq = i;
    642 				count = tmp;
    643 			}
    644 			break;
    645 
    646 		case IST_PULSE:
    647 			/* this just isn't shareable */
    648 			continue;
    649 		}
    650 	}
    651 
    652 	if (bestirq == -1)
    653 		return (1);
    654 
    655 	*irq = bestirq;
    656 
    657 	return (0);
    658 }
    659 
    660 static void
    661 specific_eoi(irq)
    662 	int irq;
    663 {
    664 	if (irq > 7)
    665 		bus_space_write_1(sio_iot,
    666 		    sio_ioh_icu2, 0, 0x20 | (irq & 0x07));	/* XXX */
    667 	bus_space_write_1(sio_iot, sio_ioh_icu1, 0, 0x20 | (irq > 7 ? 2 : irq));
    668 }
    669