Home | History | Annotate | Line # | Download | only in pci
pcibios.c revision 1.25
      1 /*	$NetBSD: pcibios.c,v 1.25 2005/06/21 08:19:26 sekiya Exp $	*/
      2 
      3 /*-
      4  * Copyright (c) 1999 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) 1999, by UCHIYAMA Yasushi
     42  * All rights reserved.
     43  *
     44  * Redistribution and use in source and binary forms, with or without
     45  * modification, are permitted provided that the following conditions
     46  * are met:
     47  * 1. Redistributions of source code must retain the above copyright
     48  *    notice, this list of conditions and the following disclaimer.
     49  * 2. The name of the developer may NOT be used to endorse or promote products
     50  *    derived from this software without specific prior written permission.
     51  *
     52  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
     53  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     54  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     55  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
     56  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     57  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     58  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     59  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     60  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     61  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     62  * SUCH DAMAGE.
     63  */
     64 
     65 /*
     66  * Interface to the PCI BIOS and PCI Interrupt Routing table.
     67  */
     68 
     69 #include <sys/cdefs.h>
     70 __KERNEL_RCSID(0, "$NetBSD: pcibios.c,v 1.25 2005/06/21 08:19:26 sekiya Exp $");
     71 
     72 #include "opt_pcibios.h"
     73 #include "opt_pcifixup.h"
     74 
     75 #include <sys/param.h>
     76 #include <sys/systm.h>
     77 #include <sys/device.h>
     78 #include <sys/malloc.h>
     79 
     80 #include <dev/isa/isareg.h>
     81 #include <machine/isa_machdep.h>
     82 
     83 #include <dev/pci/pcireg.h>
     84 #include <dev/pci/pcivar.h>
     85 #include <dev/pci/pcidevs.h>
     86 
     87 #include <i386/pci/pcibios.h>
     88 #ifdef PCI_INTR_FIXUP
     89 #include <i386/pci/pci_intr_fixup.h>
     90 #endif
     91 #ifdef PCI_BUS_FIXUP
     92 #include <i386/pci/pci_bus_fixup.h>
     93 #endif
     94 #ifdef PCI_ADDR_FIXUP
     95 #include <i386/pci/pci_addr_fixup.h>
     96 #endif
     97 
     98 #include <machine/bios32.h>
     99 
    100 #ifdef PCIBIOSVERBOSE
    101 int	pcibiosverbose = 1;
    102 #endif
    103 
    104 int pcibios_present;
    105 
    106 struct pcibios_pir_header pcibios_pir_header;
    107 struct pcibios_intr_routing *pcibios_pir_table;
    108 int pcibios_pir_table_nentries;
    109 int pcibios_max_bus;
    110 
    111 struct bios32_entry pcibios_entry;
    112 
    113 void	pcibios_pir_init(void);
    114 
    115 int	pcibios_get_status(u_int32_t *, u_int32_t *, u_int32_t *,
    116 	    u_int32_t *, u_int32_t *, u_int32_t *, u_int32_t *);
    117 int	pcibios_get_intr_routing(struct pcibios_intr_routing *,
    118 	    int *, u_int16_t *);
    119 
    120 int	pcibios_return_code(u_int16_t, const char *);
    121 
    122 void	pcibios_print_exclirq(void);
    123 
    124 #ifdef PCIBIOS_LIBRETTO_FIXUP
    125 /* for Libretto L2/L3 hack */
    126 static void	pcibios_fixup_pir_table(void);
    127 static void	pcibios_fixup_pir_table_mask(struct pcibios_linkmap *);
    128 
    129 struct pcibios_linkmap pir_mask[] = {
    130 	{ 2,	0x0040 },
    131 	{ 7,	0x0080 },
    132 	{ 8,	0x0020 },
    133 	{ 0,	0x0000 }
    134 };
    135 #endif
    136 
    137 #ifdef PCIBIOS_SHARP_MM20_FIXUP
    138 static void pcibios_mm20_fixup(void);
    139 #endif
    140 
    141 #ifdef PCIINTR_DEBUG
    142 void	pcibios_print_pir_table(void);
    143 #endif
    144 
    145 #define	PCI_IRQ_TABLE_START	0xf0000
    146 #define	PCI_IRQ_TABLE_END	0xfffff
    147 
    148 void
    149 pcibios_init(void)
    150 {
    151 	struct bios32_entry_info ei;
    152 	u_int32_t rev_maj, rev_min, mech1, mech2, scmech1, scmech2;
    153 
    154 #if defined(PCI_ADDR_FIXUP)
    155 	/*
    156 	 * Initialize pointers used by rbus routines here.  That way, if
    157 	 * PCIBIOS initialization fails, the rbus code doesn't break
    158 	 * spectacularly when PCI_ADDR_FIXUP is defined.
    159 	 */
    160 
    161 	pciaddr.extent_port = NULL;
    162 	pciaddr.extent_mem = NULL;
    163 #endif
    164 
    165 	if (bios32_service(BIOS32_MAKESIG('$', 'P', 'C', 'I'),
    166 	    &pcibios_entry, &ei) == 0) {
    167 		/*
    168 		 * No PCI BIOS found; will fall back on old
    169 		 * mechanism.
    170 		 */
    171 		return;
    172 	}
    173 
    174 	/*
    175 	 * We've located the PCI BIOS service; get some information
    176 	 * about it.
    177 	 */
    178 	if (pcibios_get_status(&rev_maj, &rev_min, &mech1, &mech2,
    179 	    &scmech1, &scmech2, &pcibios_max_bus) != PCIBIOS_SUCCESS) {
    180 		/*
    181 		 * We can't use the PCI BIOS; will fall back on old
    182 		 * mechanism.
    183 		 */
    184 		return;
    185 	}
    186 
    187 	printf("PCI BIOS rev. %d.%d found at 0x%lx\n", rev_maj, rev_min >> 4,
    188 	    ei.bei_entry);
    189 #ifdef PCIBIOSVERBOSE
    190 	printf("pcibios: config mechanism %s%s, special cycles %s%s, "
    191 	    "last bus %d\n",
    192 	    mech1 ? "[1]" : "[x]",
    193 	    mech2 ? "[2]" : "[x]",
    194 	    scmech1 ? "[1]" : "[x]",
    195 	    scmech2 ? "[2]" : "[x]",
    196 	    pcibios_max_bus);
    197 
    198 #endif
    199 
    200 	/*
    201 	 * The PCI BIOS tells us the config mechanism; fill it in now
    202 	 * so that pci_mode_detect() doesn't have to look for it.
    203 	 */
    204 	pci_mode = mech1 ? 1 : 2;
    205 
    206 	pcibios_present = 1;
    207 
    208 	/*
    209 	 * Find the PCI IRQ Routing table.
    210 	 */
    211 	pcibios_pir_init();
    212 
    213 #ifdef PCI_INTR_FIXUP
    214 	if (pcibios_pir_table != NULL) {
    215 		int rv;
    216 		u_int16_t pciirq;
    217 
    218 		/*
    219 		 * Fixup interrupt routing.
    220 		 */
    221 		rv = pci_intr_fixup(NULL, X86_BUS_SPACE_IO, &pciirq);
    222 		switch (rv) {
    223 		case -1:
    224 			/* Non-fatal error. */
    225 			printf("Warning: unable to fix up PCI interrupt "
    226 			    "routing\n");
    227 			break;
    228 
    229 		case 1:
    230 			/* Fatal error. */
    231 			panic("pcibios_init: interrupt fixup failed");
    232 			break;
    233 		}
    234 
    235 		/*
    236 		 * XXX Clear `pciirq' from the ISA interrupt allocation
    237 		 * XXX mask.
    238 		 */
    239 	}
    240 #endif
    241 
    242 #ifdef PCI_BUS_FIXUP
    243 	pcibios_max_bus = pci_bus_fixup(NULL, 0);
    244 #ifdef PCIBIOSVERBOSE
    245 	printf("PCI bus #%d is the last bus\n", pcibios_max_bus);
    246 #endif
    247 #endif
    248 
    249 #ifdef PCI_ADDR_FIXUP
    250 	pci_addr_fixup(NULL, pcibios_max_bus);
    251 #endif
    252 }
    253 
    254 void
    255 pcibios_pir_init(void)
    256 {
    257 	char *devinfo;
    258 	paddr_t pa;
    259 	caddr_t p;
    260 	unsigned char cksum;
    261 	u_int16_t tablesize;
    262 	u_int8_t rev_maj, rev_min;
    263 	int i;
    264 
    265 	for (pa = PCI_IRQ_TABLE_START; pa < PCI_IRQ_TABLE_END; pa += 16) {
    266 		p = (caddr_t)ISA_HOLE_VADDR(pa);
    267 		if (*(int *)p != BIOS32_MAKESIG('$', 'P', 'I', 'R')) {
    268 			/*
    269 			 * XXX: Some laptops (Toshiba/Libretto L series)
    270 			 * use _PIR instead of $PIR. So we try that too.
    271 			 */
    272 			if (*(int *)p != BIOS32_MAKESIG('_', 'P', 'I', 'R'))
    273 				continue;
    274 		}
    275 
    276 		rev_min = *(p + 4);
    277 		rev_maj = *(p + 5);
    278 		tablesize = *(u_int16_t *)(p + 6);
    279 
    280 		cksum = 0;
    281 		for (i = 0; i < tablesize; i++)
    282 			cksum += *(unsigned char *)(p + i);
    283 
    284 		printf("PCI IRQ Routing Table rev. %d.%d found at 0x%lx, "
    285 		    "size %d bytes (%d entries)\n", rev_maj, rev_min, pa,
    286 		    tablesize, (tablesize - 32) / 16);
    287 
    288 		if (cksum != 0) {
    289 			printf("pcibios_pir_init: bad IRQ table checksum\n");
    290 			continue;
    291 		}
    292 
    293 		if (tablesize < 32 || (tablesize % 16) != 0) {
    294 			printf("pcibios_pir_init: bad IRQ table size\n");
    295 			continue;
    296 		}
    297 
    298 		if (rev_maj != 1 || rev_min != 0) {
    299 			printf("pcibios_pir_init: unsupported IRQ table "
    300 			    "version\n");
    301 			continue;
    302 		}
    303 
    304 		/*
    305 		 * We can handle this table!  Make a copy of it.
    306 		 */
    307 		memcpy(&pcibios_pir_header, p, 32);
    308 		pcibios_pir_table = malloc(tablesize - 32, M_DEVBUF,
    309 		    M_NOWAIT);
    310 		if (pcibios_pir_table == NULL) {
    311 			printf("pcibios_pir_init: no memory for $PIR\n");
    312 			return;
    313 		}
    314 		memcpy(pcibios_pir_table, p + 32, tablesize - 32);
    315 		pcibios_pir_table_nentries = (tablesize - 32) / 16;
    316 
    317 		printf("PCI Interrupt Router at %03d:%02d:%01d",
    318 		    pcibios_pir_header.router_bus,
    319 		    PIR_DEVFUNC_DEVICE(pcibios_pir_header.router_devfunc),
    320 		    PIR_DEVFUNC_FUNCTION(pcibios_pir_header.router_devfunc));
    321 		if (pcibios_pir_header.compat_router != 0) {
    322 			devinfo = malloc(256, M_DEVBUF, M_NOWAIT);
    323 			if (devinfo) {
    324 				pci_devinfo(pcibios_pir_header.compat_router,
    325 				    0, 0, devinfo, 256);
    326 				printf(" (%s compatible)", devinfo);
    327 				free(devinfo, M_DEVBUF);
    328 			}
    329 		}
    330 		printf("\n");
    331 		pcibios_print_exclirq();
    332 
    333 #ifdef PCIBIOS_LIBRETTO_FIXUP
    334 		/* for Libretto L2/L3 hack */
    335 		pcibios_fixup_pir_table();
    336 #endif
    337 #ifdef PCIBIOS_SHARP_MM20_FIXUP
    338 		pcibios_mm20_fixup();
    339 #endif
    340 #ifdef PCIINTR_DEBUG
    341 		pcibios_print_pir_table();
    342 #endif
    343 		return;
    344 	}
    345 
    346 	/*
    347 	 * If there was no PIR table found, try using the PCI BIOS
    348 	 * Get Interrupt Routing call.
    349 	 *
    350 	 * XXX The interface to this call sucks; just allocate enough
    351 	 * XXX room for 32 entries.
    352 	 */
    353 	pcibios_pir_table_nentries = 32;
    354 	pcibios_pir_table = malloc(pcibios_pir_table_nentries *
    355 	    sizeof(*pcibios_pir_table), M_DEVBUF, M_NOWAIT);
    356 	if (pcibios_pir_table == NULL) {
    357 		printf("pcibios_pir_init: no memory for $PIR\n");
    358 		return;
    359 	}
    360 	if (pcibios_get_intr_routing(pcibios_pir_table,
    361 	    &pcibios_pir_table_nentries,
    362 	    &pcibios_pir_header.exclusive_irq) != PCIBIOS_SUCCESS) {
    363 		printf("No PCI IRQ Routing information available.\n");
    364 		free(pcibios_pir_table, M_DEVBUF);
    365 		pcibios_pir_table = NULL;
    366 		pcibios_pir_table_nentries = 0;
    367 		return;
    368 	}
    369 	printf("PCI BIOS has %d Interrupt Routing table entries\n",
    370 	    pcibios_pir_table_nentries);
    371 	pcibios_print_exclirq();
    372 
    373 #ifdef PCIBIOS_LIBRETTO_FIXUP
    374 	/* for Libretto L2/L3 hack */
    375 	pcibios_fixup_pir_table();
    376 #endif
    377 #ifdef PCIBIOS_SHARP_MM20_FIXUP
    378 	pcibios_mm20_fixup();
    379 #endif
    380 #ifdef PCIINTR_DEBUG
    381 	pcibios_print_pir_table();
    382 #endif
    383 }
    384 
    385 int
    386 pcibios_get_status(u_int32_t *rev_maj, u_int32_t *rev_min,
    387     u_int32_t *mech1, u_int32_t *mech2, u_int32_t *scmech1, u_int32_t *scmech2,
    388     u_int32_t *maxbus)
    389 {
    390 	u_int16_t ax, bx, cx;
    391 	u_int32_t edx;
    392 	int rv;
    393 
    394 	__asm __volatile("lcall *(%%edi)				; \
    395 			jc 1f						; \
    396 			xor %%ah, %%ah					; \
    397 		1:"
    398 		: "=a" (ax), "=b" (bx), "=c" (cx), "=d" (edx)
    399 		: "0" (0xb101), "D" (&pcibios_entry));
    400 
    401 	rv = pcibios_return_code(ax, "pcibios_get_status");
    402 	if (rv != PCIBIOS_SUCCESS)
    403 		return (rv);
    404 
    405 	if (edx != BIOS32_MAKESIG('P', 'C', 'I', ' '))
    406 		return (PCIBIOS_SERVICE_NOT_PRESENT);	/* XXX */
    407 
    408 	/*
    409 	 * Fill in the various pieces if info we're looking for.
    410 	 */
    411 	*mech1 = ax & 1;
    412 	*mech2 = ax & (1 << 1);
    413 	*scmech1 = ax & (1 << 4);
    414 	*scmech2 = ax & (1 << 5);
    415 	*rev_maj = (bx >> 8) & 0xff;
    416 	*rev_min = bx & 0xff;
    417 	*maxbus = cx & 0xff;
    418 
    419 	return (PCIBIOS_SUCCESS);
    420 }
    421 
    422 int
    423 pcibios_get_intr_routing(struct pcibios_intr_routing *table,
    424     int *nentries, u_int16_t *exclirq)
    425 {
    426 	u_int16_t ax, bx;
    427 	int rv;
    428 	struct {
    429 		u_int16_t size;
    430 		caddr_t offset;
    431 		u_int16_t segment;
    432 	} __attribute__((__packed__)) args;
    433 
    434 	args.size = *nentries * sizeof(*table);
    435 	args.offset = (caddr_t)table;
    436 	args.segment = GSEL(GDATA_SEL, SEL_KPL);
    437 
    438 	memset(table, 0, args.size);
    439 
    440 	__asm __volatile("lcall *(%%esi)				; \
    441 			jc 1f						; \
    442 			xor %%ah, %%ah					; \
    443 		1:	movw %w2, %%ds					; \
    444 			movw %w2, %%es"
    445 		: "=a" (ax), "=b" (bx)
    446 		: "r" GSEL(GDATA_SEL, SEL_KPL), "0" (0xb10e), "1" (0),
    447 		  "D" (&args), "S" (&pcibios_entry));
    448 
    449 	rv = pcibios_return_code(ax, "pcibios_get_intr_routing");
    450 	if (rv != PCIBIOS_SUCCESS)
    451 		return (rv);
    452 
    453 	*nentries = args.size / sizeof(*table);
    454 	*exclirq = bx;
    455 
    456 	return (PCIBIOS_SUCCESS);
    457 }
    458 
    459 int
    460 pcibios_return_code(u_int16_t ax, const char *func)
    461 {
    462 	const char *errstr;
    463 	int rv = ax >> 8;
    464 
    465 	switch (rv) {
    466 	case PCIBIOS_SUCCESS:
    467 		return (PCIBIOS_SUCCESS);
    468 
    469 	case PCIBIOS_SERVICE_NOT_PRESENT:
    470 		errstr = "service not present";
    471 		break;
    472 
    473 	case PCIBIOS_FUNCTION_NOT_SUPPORTED:
    474 		errstr = "function not supported";
    475 		break;
    476 
    477 	case PCIBIOS_BAD_VENDOR_ID:
    478 		errstr = "bad vendor ID";
    479 		break;
    480 
    481 	case PCIBIOS_DEVICE_NOT_FOUND:
    482 		errstr = "device not found";
    483 		break;
    484 
    485 	case PCIBIOS_BAD_REGISTER_NUMBER:
    486 		errstr = "bad register number";
    487 		break;
    488 
    489 	case PCIBIOS_SET_FAILED:
    490 		errstr = "set failed";
    491 		break;
    492 
    493 	case PCIBIOS_BUFFER_TOO_SMALL:
    494 		errstr = "buffer too small";
    495 		break;
    496 
    497 	default:
    498 		printf("%s: unknown return code 0x%x\n", func, rv);
    499 		return (rv);
    500 	}
    501 
    502 	printf("%s: %s\n", func, errstr);
    503 	return (rv);
    504 }
    505 
    506 void
    507 pcibios_print_exclirq(void)
    508 {
    509 	int i;
    510 
    511 	if (pcibios_pir_header.exclusive_irq) {
    512 		printf("PCI Exclusive IRQs:");
    513 		for (i = 0; i < 16; i++) {
    514 			if (pcibios_pir_header.exclusive_irq & (1 << i))
    515 				printf(" %d", i);
    516 		}
    517 		printf("\n");
    518 	}
    519 }
    520 
    521 #ifdef PCIBIOS_LIBRETTO_FIXUP
    522 /* for Libretto L2/L3 hack */
    523 static void
    524 pcibios_fixup_pir_table(void)
    525 {
    526 	struct pcibios_linkmap *m;
    527 
    528 	for (m = pir_mask; m->link != 0; m++)
    529 		pcibios_fixup_pir_table_mask(m);
    530 }
    531 
    532 void
    533 pcibios_fixup_pir_table_mask(struct pcibios_linkmap *mask)
    534 {
    535 	int i, j;
    536 
    537 	for (i = 0; i < pcibios_pir_table_nentries; i++) {
    538 		for (j = 0; j < 4; j++) {
    539 			if (pcibios_pir_table[i].linkmap[j].link == mask->link) {
    540 				pcibios_pir_table[i].linkmap[j].bitmap
    541 				    &= mask->bitmap;
    542 			}
    543 		}
    544 	}
    545 }
    546 #endif
    547 
    548 #ifdef PCIINTR_DEBUG
    549 void
    550 pcibios_print_pir_table(void)
    551 {
    552 	int i, j;
    553 
    554 	for (i = 0; i < pcibios_pir_table_nentries; i++) {
    555 		printf("PIR Entry %d:\n", i);
    556 		printf("\tBus: %d  Device: %d\n",
    557 		    pcibios_pir_table[i].bus,
    558 		    PIR_DEVFUNC_DEVICE(pcibios_pir_table[i].device));
    559 		for (j = 0; j < 4; j++) {
    560 			printf("\t\tINT%c: link 0x%02x bitmap 0x%04x\n",
    561 			    'A' + j,
    562 			    pcibios_pir_table[i].linkmap[j].link,
    563 			    pcibios_pir_table[i].linkmap[j].bitmap);
    564 		}
    565 	}
    566 }
    567 #endif
    568 
    569 #ifdef PCIBIOS_SHARP_MM20_FIXUP
    570 /*
    571  * This is a gross hack to get the interrupt from the EHCI controller
    572  * working on a Sharp MM20.  The BIOS is just incredibly buggy.
    573  *
    574  * The story thus far:
    575  * The modern way to route the interrupt is to use ACPI.  But using
    576  * ACPI fails with an error message about an uninitialized local
    577  * variable in the AML code.  (It works in Windows, but fails in NetBSD
    578  * and Linux.)
    579  *
    580  * The second attempt is to use PCI Interrupt Routing table.  But this
    581  * fails because the table does not contain any information about the
    582  * interrupt from the EHCI controller.  This is probably due to the fact
    583  * that the table is compatible with ALi M1543, but the MM20 has an ALi M1563.
    584  * The M1563 has additional interrupt lines.  The ali1543.c code also
    585  * cannot handle the M1653's extended interrupts.  And fixing this is
    586  * difficult since getting a data sheet from ALi requires signing an NDA.
    587  *
    588  * The third attempt is to use a BIOS call to route the interrupt
    589  * (as FreeBSD does) with manually generated information.  But the BIOS call
    590  * fails because the BIOS code is not quite position independent.  It makes
    591  * some assumption about where the code segment register points.
    592  *
    593  * So the solution is to use the third attempt, but with a patched version
    594  * of the BIOS.
    595  *    -- lennart (at) augustsson.net
    596  */
    597 
    598 #define	BIOS32_START	0xe0000
    599 #define	BIOS32_SIZE	0x20000
    600 
    601 static char pcibios_shadow[BIOS32_SIZE];
    602 static struct bios32_entry pcibios_entry_shadow;
    603 
    604 /*
    605  * Copy BIOS and zap offending instruction.
    606  * The bad instruction is
    607  *    mov    %cs:0x63c(%ebx),%ah
    608  * NetBSD does not have the code segment set up for this to work.
    609  * Using the value 0xff for the table entry seems to work.
    610  * The replacement is
    611  *    mov $0xff,%ah; nop; nop; nop; nop; nop
    612  */
    613 static void
    614 pcibios_copy_bios(void)
    615 {
    616 	u_int8_t *bad_instr;
    617 
    618 	memcpy(pcibios_shadow, ISA_HOLE_VADDR(BIOS32_START), BIOS32_SIZE);
    619 	pcibios_entry_shadow = pcibios_entry;
    620 	pcibios_entry_shadow.offset =
    621 	    (void*)((u_long)pcibios_shadow +
    622 		    (u_long)pcibios_entry.offset -
    623 		    (u_long)ISA_HOLE_VADDR(BIOS32_START));
    624 
    625 	bad_instr = (u_int8_t *)pcibios_entry_shadow.offset + 0x499;
    626 	if (*bad_instr != 0x2e)
    627 		panic("bad bios");
    628 	bad_instr[0] = 0xb4; bad_instr[1] = 0xff; /* mov $0xff,%ah */
    629 	bad_instr[2] = 0x90;		/* nop */
    630 	bad_instr[3] = 0x90;		/* nop */
    631 	bad_instr[4] = 0x90;		/* nop */
    632 	bad_instr[5] = 0x90;		/* nop */
    633 	bad_instr[6] = 0x90;		/* nop */
    634 }
    635 
    636 /*
    637  * Call BIOS to route an interrupt.
    638  * The PCI device is identified by bus,device,func.
    639  * The interrupt is on pin PIN (A-D) and interrupt IRQ.
    640  * BIOS knows the magic for the interrupt controller.
    641  */
    642 static int
    643 pcibios_biosroute(int bus, int device, int func, int pin, int irq)
    644 {
    645 	u_int16_t ax, bx, cx;
    646 	int rv;
    647 
    648 	printf("pcibios_biosroute: b,d,f=%d,%d,%d pin=%x irq=%d\n",
    649 	       bus, device, func, pin+0xa, irq);
    650 
    651 	bx = (bus << 8) | (device << 3) | func;
    652 	cx = (irq << 8) | (0xa + pin);
    653 
    654 	__asm __volatile("lcall *(%%esi)				; \
    655 			jc 1f						; \
    656 			xor %%ah, %%ah					; \
    657 		1:	movw %w1, %%ds					; \
    658 			movw %w1, %%es"
    659 			 : "=a" (ax)
    660 			 : "r" GSEL(GDATA_SEL, SEL_KPL), "0" (0xb10f),
    661 			   "b" (bx), "c" (cx),
    662 		           "S" (&pcibios_entry_shadow));
    663 
    664 	rv = pcibios_return_code(ax, "pcibios_biosroute");
    665 
    666 	return rv;
    667 }
    668 
    669 #define MM20_PCI_BUS 0
    670 #define MM20_PCI_EHCI_DEV 15
    671 #define MM20_PCI_EHCI_FUNC 3
    672 #define MM20_PCI_EHCI_PIN 3
    673 #define MM20_PCI_EHCI_INTR 11
    674 #define MM20_PCI_ISA_DEV 3
    675 #define MM20_PCI_ISA_FUNC 0
    676 
    677 static void
    678 pcibios_mm20_fixup(void)
    679 {
    680 	pci_chipset_tag_t pc;
    681 	pcitag_t tag;
    682 
    683 	/* Copy BIOS */
    684 	pcibios_copy_bios();
    685 	/* Route the interrupt for the EHCI controller. */
    686 	(void)pcibios_biosroute(MM20_PCI_BUS,
    687 				MM20_PCI_EHCI_DEV,
    688 				MM20_PCI_EHCI_FUNC,
    689 				MM20_PCI_EHCI_PIN,
    690 				MM20_PCI_EHCI_INTR);
    691 
    692 	/* Fake some tags. */
    693 	pc = NULL;
    694 	tag = pci_make_tag(pc, MM20_PCI_BUS, MM20_PCI_EHCI_DEV,
    695 			   MM20_PCI_EHCI_FUNC);
    696 	/* Set interrupt register in EHCI controller */
    697 	pci_conf_write(pc, tag, 0x3c, 0x50000400 + MM20_PCI_EHCI_INTR);
    698 	tag = pci_make_tag(pc, MM20_PCI_BUS, MM20_PCI_ISA_DEV,
    699 			   MM20_PCI_ISA_FUNC);
    700 	/* Set some unknown registers in the ISA bridge. */
    701 	pci_conf_write(pc, tag, 0x58, 0xd87f5300);
    702 	pci_conf_write(pc, tag, 0x74, 0x00000009);
    703 }
    704 
    705 #endif /* PCIBIOS_SHARP_MM20_FIXUP */
    706