Home | History | Annotate | Line # | Download | only in pci
pcibios.c revision 1.8
      1  1.8      uch /*	$NetBSD: pcibios.c,v 1.8 2002/01/22 15:07:27 uch Exp $	*/
      2  1.1  thorpej 
      3  1.1  thorpej /*-
      4  1.1  thorpej  * Copyright (c) 1999 The NetBSD Foundation, Inc.
      5  1.1  thorpej  * All rights reserved.
      6  1.1  thorpej  *
      7  1.1  thorpej  * This code is derived from software contributed to The NetBSD Foundation
      8  1.1  thorpej  * by Jason R. Thorpe of the Numerical Aerospace Simulation Facility,
      9  1.1  thorpej  * NASA Ames Research Center.
     10  1.1  thorpej  *
     11  1.1  thorpej  * Redistribution and use in source and binary forms, with or without
     12  1.1  thorpej  * modification, are permitted provided that the following conditions
     13  1.1  thorpej  * are met:
     14  1.1  thorpej  * 1. Redistributions of source code must retain the above copyright
     15  1.1  thorpej  *    notice, this list of conditions and the following disclaimer.
     16  1.1  thorpej  * 2. Redistributions in binary form must reproduce the above copyright
     17  1.1  thorpej  *    notice, this list of conditions and the following disclaimer in the
     18  1.1  thorpej  *    documentation and/or other materials provided with the distribution.
     19  1.1  thorpej  * 3. All advertising materials mentioning features or use of this software
     20  1.1  thorpej  *    must display the following acknowledgement:
     21  1.1  thorpej  *	This product includes software developed by the NetBSD
     22  1.1  thorpej  *	Foundation, Inc. and its contributors.
     23  1.1  thorpej  * 4. Neither the name of The NetBSD Foundation nor the names of its
     24  1.1  thorpej  *    contributors may be used to endorse or promote products derived
     25  1.1  thorpej  *    from this software without specific prior written permission.
     26  1.1  thorpej  *
     27  1.1  thorpej  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     28  1.1  thorpej  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     29  1.1  thorpej  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     30  1.1  thorpej  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     31  1.1  thorpej  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     32  1.1  thorpej  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     33  1.1  thorpej  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     34  1.1  thorpej  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     35  1.1  thorpej  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     36  1.1  thorpej  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     37  1.1  thorpej  * POSSIBILITY OF SUCH DAMAGE.
     38  1.1  thorpej  */
     39  1.1  thorpej 
     40  1.1  thorpej /*
     41  1.1  thorpej  * Copyright (c) 1999, by UCHIYAMA Yasushi
     42  1.1  thorpej  * All rights reserved.
     43  1.1  thorpej  *
     44  1.1  thorpej  * Redistribution and use in source and binary forms, with or without
     45  1.1  thorpej  * modification, are permitted provided that the following conditions
     46  1.1  thorpej  * are met:
     47  1.1  thorpej  * 1. Redistributions of source code must retain the above copyright
     48  1.1  thorpej  *    notice, this list of conditions and the following disclaimer.
     49  1.1  thorpej  * 2. The name of the developer may NOT be used to endorse or promote products
     50  1.1  thorpej  *    derived from this software without specific prior written permission.
     51  1.1  thorpej  *
     52  1.1  thorpej  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
     53  1.1  thorpej  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     54  1.1  thorpej  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     55  1.1  thorpej  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
     56  1.1  thorpej  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     57  1.1  thorpej  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     58  1.1  thorpej  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     59  1.1  thorpej  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     60  1.1  thorpej  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     61  1.1  thorpej  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     62  1.1  thorpej  * SUCH DAMAGE.
     63  1.1  thorpej  */
     64  1.1  thorpej 
     65  1.1  thorpej /*
     66  1.1  thorpej  * Interface to the PCI BIOS and PCI Interrupt Routing table.
     67  1.1  thorpej  */
     68  1.7    lukem 
     69  1.7    lukem #include <sys/cdefs.h>
     70  1.8      uch __KERNEL_RCSID(0, "$NetBSD: pcibios.c,v 1.8 2002/01/22 15:07:27 uch Exp $");
     71  1.1  thorpej 
     72  1.1  thorpej #include "opt_pcibios.h"
     73  1.1  thorpej 
     74  1.1  thorpej #include <sys/param.h>
     75  1.1  thorpej #include <sys/systm.h>
     76  1.1  thorpej #include <sys/device.h>
     77  1.1  thorpej #include <sys/malloc.h>
     78  1.1  thorpej 
     79  1.1  thorpej #include <dev/isa/isareg.h>
     80  1.1  thorpej #include <machine/isa_machdep.h>
     81  1.1  thorpej 
     82  1.1  thorpej #include <dev/pci/pcireg.h>
     83  1.1  thorpej #include <dev/pci/pcivar.h>
     84  1.3      uch #include <dev/pci/pcidevs.h>
     85  1.1  thorpej 
     86  1.1  thorpej #include <i386/pci/pcibios.h>
     87  1.1  thorpej #ifdef PCIBIOS_INTR_FIXUP
     88  1.1  thorpej #include <i386/pci/pci_intr_fixup.h>
     89  1.1  thorpej #endif
     90  1.2  thorpej #ifdef PCIBIOS_BUS_FIXUP
     91  1.2  thorpej #include <i386/pci/pci_bus_fixup.h>
     92  1.2  thorpej #endif
     93  1.3      uch #ifdef PCIBIOS_ADDR_FIXUP
     94  1.3      uch #include <i386/pci/pci_addr_fixup.h>
     95  1.3      uch #endif
     96  1.1  thorpej 
     97  1.1  thorpej #include <machine/bios32.h>
     98  1.1  thorpej 
     99  1.4     soda #ifdef PCIBIOSVERBOSE
    100  1.4     soda int	pcibiosverbose = 1;
    101  1.4     soda #endif
    102  1.4     soda 
    103  1.1  thorpej int pcibios_present;
    104  1.1  thorpej 
    105  1.1  thorpej struct pcibios_pir_header pcibios_pir_header;
    106  1.1  thorpej struct pcibios_intr_routing *pcibios_pir_table;
    107  1.1  thorpej int pcibios_pir_table_nentries;
    108  1.1  thorpej int pcibios_max_bus;
    109  1.1  thorpej 
    110  1.1  thorpej struct bios32_entry pcibios_entry;
    111  1.1  thorpej 
    112  1.1  thorpej void	pcibios_pir_init __P((void));
    113  1.1  thorpej 
    114  1.1  thorpej int	pcibios_get_status __P((u_int32_t *, u_int32_t *, u_int32_t *,
    115  1.1  thorpej 	    u_int32_t *, u_int32_t *, u_int32_t *, u_int32_t *));
    116  1.1  thorpej int	pcibios_get_intr_routing __P((struct pcibios_intr_routing *,
    117  1.1  thorpej 	    int *, u_int16_t *));
    118  1.1  thorpej 
    119  1.1  thorpej int	pcibios_return_code __P((u_int16_t, const char *));
    120  1.1  thorpej 
    121  1.1  thorpej void	pcibios_print_exclirq __P((void));
    122  1.1  thorpej #ifdef PCIINTR_DEBUG
    123  1.1  thorpej void	pcibios_print_pir_table __P((void));
    124  1.1  thorpej #endif
    125  1.1  thorpej 
    126  1.1  thorpej #define	PCI_IRQ_TABLE_START	0xf0000
    127  1.1  thorpej #define	PCI_IRQ_TABLE_END	0xfffff
    128  1.1  thorpej 
    129  1.8      uch static void pci_bridge_hook(pci_chipset_tag_t, pcitag_t, void *);
    130  1.8      uch struct pci_bridge_hook_arg {
    131  1.8      uch 	void (*func)(pci_chipset_tag_t, pcitag_t, void *);
    132  1.8      uch 	void *arg;
    133  1.8      uch };
    134  1.8      uch 
    135  1.1  thorpej void
    136  1.1  thorpej pcibios_init()
    137  1.1  thorpej {
    138  1.1  thorpej 	struct bios32_entry_info ei;
    139  1.1  thorpej 	u_int32_t rev_maj, rev_min, mech1, mech2, scmech1, scmech2;
    140  1.1  thorpej 
    141  1.1  thorpej 	if (bios32_service(BIOS32_MAKESIG('$', 'P', 'C', 'I'),
    142  1.1  thorpej 	    &pcibios_entry, &ei) == 0) {
    143  1.1  thorpej 		/*
    144  1.1  thorpej 		 * No PCI BIOS found; will fall back on old
    145  1.1  thorpej 		 * mechanism.
    146  1.1  thorpej 		 */
    147  1.1  thorpej 		return;
    148  1.1  thorpej 	}
    149  1.1  thorpej 
    150  1.1  thorpej 	/*
    151  1.1  thorpej 	 * We've located the PCI BIOS service; get some information
    152  1.1  thorpej 	 * about it.
    153  1.1  thorpej 	 */
    154  1.1  thorpej 	if (pcibios_get_status(&rev_maj, &rev_min, &mech1, &mech2,
    155  1.1  thorpej 	    &scmech1, &scmech2, &pcibios_max_bus) != PCIBIOS_SUCCESS) {
    156  1.1  thorpej 		/*
    157  1.1  thorpej 		 * We can't use the PCI BIOS; will fall back on old
    158  1.1  thorpej 		 * mechanism.
    159  1.1  thorpej 		 */
    160  1.1  thorpej 		return;
    161  1.1  thorpej 	}
    162  1.1  thorpej 
    163  1.1  thorpej 	printf("PCI BIOS rev. %d.%d found at 0x%lx\n", rev_maj, rev_min >> 4,
    164  1.1  thorpej 	    ei.bei_entry);
    165  1.1  thorpej #ifdef PCIBIOSVERBOSE
    166  1.1  thorpej 	printf("pcibios: config mechanism %s%s, special cycles %s%s, "
    167  1.1  thorpej 	    "last bus %d\n",
    168  1.1  thorpej 	    mech1 ? "[1]" : "[x]",
    169  1.1  thorpej 	    mech2 ? "[2]" : "[x]",
    170  1.1  thorpej 	    scmech1 ? "[1]" : "[x]",
    171  1.1  thorpej 	    scmech2 ? "[2]" : "[x]",
    172  1.1  thorpej 	    pcibios_max_bus);
    173  1.1  thorpej 
    174  1.1  thorpej #endif
    175  1.1  thorpej 
    176  1.1  thorpej 	/*
    177  1.1  thorpej 	 * The PCI BIOS tells us the config mechanism; fill it in now
    178  1.1  thorpej 	 * so that pci_mode_detect() doesn't have to look for it.
    179  1.1  thorpej 	 */
    180  1.1  thorpej 	pci_mode = mech1 ? 1 : 2;
    181  1.1  thorpej 
    182  1.1  thorpej 	pcibios_present = 1;
    183  1.1  thorpej 
    184  1.1  thorpej 	/*
    185  1.1  thorpej 	 * Find the PCI IRQ Routing table.
    186  1.1  thorpej 	 */
    187  1.1  thorpej 	pcibios_pir_init();
    188  1.1  thorpej 
    189  1.1  thorpej #ifdef PCIBIOS_INTR_FIXUP
    190  1.1  thorpej 	if (pcibios_pir_table != NULL) {
    191  1.1  thorpej 		int rv;
    192  1.1  thorpej 		u_int16_t pciirq;
    193  1.1  thorpej 
    194  1.1  thorpej 		/*
    195  1.1  thorpej 		 * Fixup interrupt routing.
    196  1.1  thorpej 		 */
    197  1.1  thorpej 		rv = pci_intr_fixup(NULL, I386_BUS_SPACE_IO, &pciirq);
    198  1.1  thorpej 		switch (rv) {
    199  1.1  thorpej 		case -1:
    200  1.1  thorpej 			/* Non-fatal error. */
    201  1.1  thorpej 			printf("Warning: unable to fix up PCI interrupt "
    202  1.1  thorpej 			    "routing\n");
    203  1.1  thorpej 			break;
    204  1.1  thorpej 
    205  1.1  thorpej 		case 1:
    206  1.1  thorpej 			/* Fatal error. */
    207  1.1  thorpej 			panic("pcibios_init: interrupt fixup failed");
    208  1.1  thorpej 			break;
    209  1.1  thorpej 		}
    210  1.1  thorpej 
    211  1.1  thorpej 		/*
    212  1.1  thorpej 		 * XXX Clear `pciirq' from the ISA interrupt allocation
    213  1.1  thorpej 		 * XXX mask.
    214  1.1  thorpej 		 */
    215  1.1  thorpej 	}
    216  1.2  thorpej #endif
    217  1.2  thorpej 
    218  1.2  thorpej #ifdef PCIBIOS_BUS_FIXUP
    219  1.2  thorpej 	pcibios_max_bus = pci_bus_fixup(NULL, 0);
    220  1.2  thorpej #ifdef PCIBIOSVERBOSE
    221  1.2  thorpej 	printf("PCI bus #%d is the last bus\n", pcibios_max_bus);
    222  1.2  thorpej #endif
    223  1.1  thorpej #endif
    224  1.3      uch 
    225  1.3      uch #ifdef PCIBIOS_ADDR_FIXUP
    226  1.5      uch 	pci_addr_fixup(NULL, pcibios_max_bus);
    227  1.3      uch #endif
    228  1.1  thorpej }
    229  1.1  thorpej 
    230  1.1  thorpej void
    231  1.1  thorpej pcibios_pir_init()
    232  1.1  thorpej {
    233  1.1  thorpej 	char devinfo[256];
    234  1.1  thorpej 	paddr_t pa;
    235  1.1  thorpej 	caddr_t p;
    236  1.1  thorpej 	unsigned char cksum;
    237  1.1  thorpej 	u_int16_t tablesize;
    238  1.1  thorpej 	u_int8_t rev_maj, rev_min;
    239  1.1  thorpej 	int i;
    240  1.1  thorpej 
    241  1.1  thorpej 	for (pa = PCI_IRQ_TABLE_START; pa < PCI_IRQ_TABLE_END; pa += 16) {
    242  1.1  thorpej 		p = (caddr_t)ISA_HOLE_VADDR(pa);
    243  1.1  thorpej 		if (*(int *)p != BIOS32_MAKESIG('$', 'P', 'I', 'R'))
    244  1.1  thorpej 			continue;
    245  1.1  thorpej 
    246  1.1  thorpej 		rev_min = *(p + 4);
    247  1.1  thorpej 		rev_maj = *(p + 5);
    248  1.1  thorpej 		tablesize = *(u_int16_t *)(p + 6);
    249  1.1  thorpej 
    250  1.1  thorpej 		cksum = 0;
    251  1.1  thorpej 		for (i = 0; i < tablesize; i++)
    252  1.1  thorpej 			cksum += *(unsigned char *)(p + i);
    253  1.1  thorpej 
    254  1.1  thorpej 		printf("PCI IRQ Routing Table rev. %d.%d found at 0x%lx, "
    255  1.1  thorpej 		    "size %d bytes (%d entries)\n", rev_maj, rev_min, pa,
    256  1.1  thorpej 		    tablesize, (tablesize - 32) / 16);
    257  1.1  thorpej 
    258  1.1  thorpej 		if (cksum != 0) {
    259  1.1  thorpej 			printf("pcibios_pir_init: bad IRQ table checksum\n");
    260  1.1  thorpej 			continue;
    261  1.1  thorpej 		}
    262  1.1  thorpej 
    263  1.1  thorpej 		if (tablesize < 32 || (tablesize % 16) != 0) {
    264  1.1  thorpej 			printf("pcibios_pir_init: bad IRQ table size\n");
    265  1.1  thorpej 			continue;
    266  1.1  thorpej 		}
    267  1.1  thorpej 
    268  1.1  thorpej 		if (rev_maj != 1 || rev_min != 0) {
    269  1.1  thorpej 			printf("pcibios_pir_init: unsupported IRQ table "
    270  1.1  thorpej 			    "version\n");
    271  1.1  thorpej 			continue;
    272  1.1  thorpej 		}
    273  1.1  thorpej 
    274  1.1  thorpej 		/*
    275  1.1  thorpej 		 * We can handle this table!  Make a copy of it.
    276  1.1  thorpej 		 */
    277  1.1  thorpej 		memcpy(&pcibios_pir_header, p, 32);
    278  1.1  thorpej 		pcibios_pir_table = malloc(tablesize - 32, M_DEVBUF,
    279  1.1  thorpej 		    M_NOWAIT);
    280  1.1  thorpej 		if (pcibios_pir_table == NULL) {
    281  1.1  thorpej 			printf("pcibios_pir_init: no memory for $PIR\n");
    282  1.1  thorpej 			return;
    283  1.1  thorpej 		}
    284  1.1  thorpej 		memcpy(pcibios_pir_table, p + 32, tablesize - 32);
    285  1.1  thorpej 		pcibios_pir_table_nentries = (tablesize - 32) / 16;
    286  1.1  thorpej 
    287  1.1  thorpej 		printf("PCI Interrupt Router at %03d:%02d:%01d",
    288  1.1  thorpej 		    pcibios_pir_header.router_bus,
    289  1.4     soda 		    PIR_DEVFUNC_DEVICE(pcibios_pir_header.router_devfunc),
    290  1.4     soda 		    PIR_DEVFUNC_FUNCTION(pcibios_pir_header.router_devfunc));
    291  1.1  thorpej 		if (pcibios_pir_header.compat_router != 0) {
    292  1.1  thorpej 			pci_devinfo(pcibios_pir_header.compat_router, 0, 0,
    293  1.1  thorpej 			    devinfo);
    294  1.1  thorpej 			printf(" (%s)", devinfo);
    295  1.1  thorpej 		}
    296  1.1  thorpej 		printf("\n");
    297  1.1  thorpej 		pcibios_print_exclirq();
    298  1.1  thorpej #ifdef PCIINTR_DEBUG
    299  1.1  thorpej 		pcibios_print_pir_table();
    300  1.1  thorpej #endif
    301  1.1  thorpej 		return;
    302  1.1  thorpej 	}
    303  1.1  thorpej 
    304  1.1  thorpej 	/*
    305  1.1  thorpej 	 * If there was no PIR table found, try using the PCI BIOS
    306  1.1  thorpej 	 * Get Interrupt Routing call.
    307  1.1  thorpej 	 *
    308  1.1  thorpej 	 * XXX The interface to this call sucks; just allocate enough
    309  1.1  thorpej 	 * XXX room for 32 entries.
    310  1.1  thorpej 	 */
    311  1.1  thorpej 	pcibios_pir_table_nentries = 32;
    312  1.1  thorpej 	pcibios_pir_table = malloc(pcibios_pir_table_nentries *
    313  1.1  thorpej 	    sizeof(*pcibios_pir_table), M_DEVBUF, M_NOWAIT);
    314  1.1  thorpej 	if (pcibios_pir_table == NULL) {
    315  1.1  thorpej 		printf("pcibios_pir_init: no memory for $PIR\n");
    316  1.1  thorpej 		return;
    317  1.1  thorpej 	}
    318  1.1  thorpej 	if (pcibios_get_intr_routing(pcibios_pir_table,
    319  1.1  thorpej 	    &pcibios_pir_table_nentries,
    320  1.1  thorpej 	    &pcibios_pir_header.exclusive_irq) != PCIBIOS_SUCCESS) {
    321  1.1  thorpej 		printf("No PCI IRQ Routing information available.\n");
    322  1.1  thorpej 		free(pcibios_pir_table, M_DEVBUF);
    323  1.1  thorpej 		pcibios_pir_table = NULL;
    324  1.1  thorpej 		pcibios_pir_table_nentries = 0;
    325  1.1  thorpej 		return;
    326  1.1  thorpej 	}
    327  1.1  thorpej 	printf("PCI BIOS has %d Interrupt Routing table entries\n",
    328  1.1  thorpej 	    pcibios_pir_table_nentries);
    329  1.1  thorpej 	pcibios_print_exclirq();
    330  1.1  thorpej #ifdef PCIINTR_DEBUG
    331  1.1  thorpej 	pcibios_print_pir_table();
    332  1.1  thorpej #endif
    333  1.1  thorpej }
    334  1.1  thorpej 
    335  1.1  thorpej int
    336  1.1  thorpej pcibios_get_status(rev_maj, rev_min, mech1, mech2, scmech1, scmech2, maxbus)
    337  1.1  thorpej 	u_int32_t *rev_maj, *rev_min, *mech1, *mech2, *scmech1, *scmech2,
    338  1.1  thorpej 	    *maxbus;
    339  1.1  thorpej {
    340  1.1  thorpej 	u_int16_t ax, bx, cx;
    341  1.1  thorpej 	u_int32_t edx;
    342  1.1  thorpej 	int rv;
    343  1.1  thorpej 
    344  1.1  thorpej 	__asm __volatile("lcall (%%edi)					; \
    345  1.1  thorpej 			jc 1f						; \
    346  1.1  thorpej 			xor %%ah, %%ah					; \
    347  1.1  thorpej 		1:"
    348  1.1  thorpej 		: "=a" (ax), "=b" (bx), "=c" (cx), "=d" (edx)
    349  1.1  thorpej 		: "0" (0xb101), "D" (&pcibios_entry));
    350  1.1  thorpej 
    351  1.1  thorpej 	rv = pcibios_return_code(ax, "pcibios_get_status");
    352  1.1  thorpej 	if (rv != PCIBIOS_SUCCESS)
    353  1.1  thorpej 		return (rv);
    354  1.1  thorpej 
    355  1.1  thorpej 	if (edx != BIOS32_MAKESIG('P', 'C', 'I', ' '))
    356  1.1  thorpej 		return (PCIBIOS_SERVICE_NOT_PRESENT);	/* XXX */
    357  1.1  thorpej 
    358  1.1  thorpej 	/*
    359  1.1  thorpej 	 * Fill in the various pieces if info we're looking for.
    360  1.1  thorpej 	 */
    361  1.1  thorpej 	*mech1 = ax & 1;
    362  1.1  thorpej 	*mech2 = ax & (1 << 1);
    363  1.1  thorpej 	*scmech1 = ax & (1 << 4);
    364  1.1  thorpej 	*scmech2 = ax & (1 << 5);
    365  1.1  thorpej 	*rev_maj = (bx >> 8) & 0xff;
    366  1.1  thorpej 	*rev_min = bx & 0xff;
    367  1.1  thorpej 	*maxbus = cx & 0xff;
    368  1.1  thorpej 
    369  1.1  thorpej 	return (PCIBIOS_SUCCESS);
    370  1.1  thorpej }
    371  1.1  thorpej 
    372  1.1  thorpej int
    373  1.1  thorpej pcibios_get_intr_routing(table, nentries, exclirq)
    374  1.1  thorpej 	struct pcibios_intr_routing *table;
    375  1.1  thorpej 	int *nentries;
    376  1.1  thorpej 	u_int16_t *exclirq;
    377  1.1  thorpej {
    378  1.1  thorpej 	u_int16_t ax, bx;
    379  1.1  thorpej 	int rv;
    380  1.1  thorpej 	struct {
    381  1.1  thorpej 		u_int16_t size;
    382  1.1  thorpej 		caddr_t offset;
    383  1.1  thorpej 		u_int16_t segment;
    384  1.1  thorpej 	} __attribute__((__packed__)) args;
    385  1.1  thorpej 
    386  1.1  thorpej 	args.size = *nentries * sizeof(*table);
    387  1.1  thorpej 	args.offset = (caddr_t)table;
    388  1.1  thorpej 	args.segment = GSEL(GDATA_SEL, SEL_KPL);
    389  1.1  thorpej 
    390  1.1  thorpej 	memset(table, 0, args.size);
    391  1.1  thorpej 
    392  1.1  thorpej 	__asm __volatile("lcall (%%esi)					; \
    393  1.1  thorpej 			jc 1f						; \
    394  1.1  thorpej 			xor %%ah, %%ah					; \
    395  1.1  thorpej 		1:	movw %w2, %%ds					; \
    396  1.1  thorpej 			movw %w2, %%es"
    397  1.1  thorpej 		: "=a" (ax), "=b" (bx)
    398  1.1  thorpej 		: "r" GSEL(GDATA_SEL, SEL_KPL), "0" (0xb10e), "1" (0),
    399  1.1  thorpej 		  "D" (&args), "S" (&pcibios_entry));
    400  1.1  thorpej 
    401  1.1  thorpej 	rv = pcibios_return_code(ax, "pcibios_get_intr_routing");
    402  1.1  thorpej 	if (rv != PCIBIOS_SUCCESS)
    403  1.1  thorpej 		return (rv);
    404  1.1  thorpej 
    405  1.1  thorpej 	*nentries = args.size / sizeof(*table);
    406  1.1  thorpej 	*exclirq = bx;
    407  1.1  thorpej 
    408  1.1  thorpej 	return (PCIBIOS_SUCCESS);
    409  1.1  thorpej }
    410  1.1  thorpej 
    411  1.1  thorpej int
    412  1.1  thorpej pcibios_return_code(ax, func)
    413  1.1  thorpej 	u_int16_t ax;
    414  1.1  thorpej 	const char *func;
    415  1.1  thorpej {
    416  1.1  thorpej 	const char *errstr;
    417  1.1  thorpej 	int rv = ax >> 8;
    418  1.1  thorpej 
    419  1.1  thorpej 	switch (rv) {
    420  1.1  thorpej 	case PCIBIOS_SUCCESS:
    421  1.1  thorpej 		return (PCIBIOS_SUCCESS);
    422  1.1  thorpej 
    423  1.1  thorpej 	case PCIBIOS_SERVICE_NOT_PRESENT:
    424  1.1  thorpej 		errstr = "service not present";
    425  1.1  thorpej 		break;
    426  1.1  thorpej 
    427  1.1  thorpej 	case PCIBIOS_FUNCTION_NOT_SUPPORTED:
    428  1.1  thorpej 		errstr = "function not supported";
    429  1.1  thorpej 		break;
    430  1.1  thorpej 
    431  1.1  thorpej 	case PCIBIOS_BAD_VENDOR_ID:
    432  1.1  thorpej 		errstr = "bad vendor ID";
    433  1.1  thorpej 		break;
    434  1.1  thorpej 
    435  1.1  thorpej 	case PCIBIOS_DEVICE_NOT_FOUND:
    436  1.1  thorpej 		errstr = "device not found";
    437  1.1  thorpej 		break;
    438  1.1  thorpej 
    439  1.1  thorpej 	case PCIBIOS_BAD_REGISTER_NUMBER:
    440  1.1  thorpej 		errstr = "bad register number";
    441  1.1  thorpej 		break;
    442  1.1  thorpej 
    443  1.1  thorpej 	case PCIBIOS_SET_FAILED:
    444  1.1  thorpej 		errstr = "set failed";
    445  1.1  thorpej 		break;
    446  1.1  thorpej 
    447  1.1  thorpej 	case PCIBIOS_BUFFER_TOO_SMALL:
    448  1.1  thorpej 		errstr = "buffer too small";
    449  1.1  thorpej 		break;
    450  1.1  thorpej 
    451  1.1  thorpej 	default:
    452  1.1  thorpej 		printf("%s: unknown return code 0x%x\n", func, rv);
    453  1.1  thorpej 		return (rv);
    454  1.1  thorpej 	}
    455  1.1  thorpej 
    456  1.1  thorpej 	printf("%s: %s\n", func, errstr);
    457  1.1  thorpej 	return (rv);
    458  1.1  thorpej }
    459  1.1  thorpej 
    460  1.1  thorpej void
    461  1.1  thorpej pcibios_print_exclirq()
    462  1.1  thorpej {
    463  1.1  thorpej 	int i;
    464  1.1  thorpej 
    465  1.1  thorpej 	if (pcibios_pir_header.exclusive_irq) {
    466  1.1  thorpej 		printf("PCI Exclusive IRQs:");
    467  1.1  thorpej 		for (i = 0; i < 16; i++) {
    468  1.1  thorpej 			if (pcibios_pir_header.exclusive_irq & (1 << i))
    469  1.1  thorpej 				printf(" %d", i);
    470  1.1  thorpej 		}
    471  1.1  thorpej 		printf("\n");
    472  1.1  thorpej 	}
    473  1.1  thorpej }
    474  1.1  thorpej 
    475  1.1  thorpej #ifdef PCIINTR_DEBUG
    476  1.1  thorpej void
    477  1.1  thorpej pcibios_print_pir_table()
    478  1.1  thorpej {
    479  1.1  thorpej 	int i, j;
    480  1.1  thorpej 
    481  1.1  thorpej 	for (i = 0; i < pcibios_pir_table_nentries; i++) {
    482  1.1  thorpej 		printf("PIR Entry %d:\n", i);
    483  1.1  thorpej 		printf("\tBus: %d  Device: %d\n",
    484  1.1  thorpej 		    pcibios_pir_table[i].bus,
    485  1.4     soda 		    PIR_DEVFUNC_DEVICE(pcibios_pir_table[i].device));
    486  1.1  thorpej 		for (j = 0; j < 4; j++) {
    487  1.1  thorpej 			printf("\t\tINT%c: link 0x%02x bitmap 0x%04x\n",
    488  1.1  thorpej 			    'A' + j,
    489  1.1  thorpej 			    pcibios_pir_table[i].linkmap[j].link,
    490  1.1  thorpej 			    pcibios_pir_table[i].linkmap[j].bitmap);
    491  1.1  thorpej 		}
    492  1.1  thorpej 	}
    493  1.1  thorpej }
    494  1.1  thorpej #endif
    495  1.3      uch 
    496  1.6      mcr void
    497  1.6      mcr pci_device_foreach(pc, maxbus, func, context)
    498  1.6      mcr 	pci_chipset_tag_t pc;
    499  1.6      mcr 	int maxbus;
    500  1.6      mcr 	void (*func) __P((pci_chipset_tag_t, pcitag_t, void *));
    501  1.6      mcr 	void *context;
    502  1.6      mcr {
    503  1.6      mcr   pci_device_foreach_min(pc, 0, maxbus, func, context);
    504  1.6      mcr }
    505  1.6      mcr 
    506  1.3      uch void
    507  1.6      mcr pci_device_foreach_min(pc, minbus, maxbus, func, context)
    508  1.3      uch 	pci_chipset_tag_t pc;
    509  1.6      mcr 	int minbus;
    510  1.3      uch 	int maxbus;
    511  1.6      mcr 	void (*func) __P((pci_chipset_tag_t, pcitag_t, void *));
    512  1.6      mcr 	void *context;
    513  1.3      uch {
    514  1.3      uch 	const struct pci_quirkdata *qd;
    515  1.3      uch 	int bus, device, function, maxdevs, nfuncs;
    516  1.3      uch 	pcireg_t id, bhlcr;
    517  1.3      uch 	pcitag_t tag;
    518  1.3      uch 
    519  1.6      mcr 	for (bus = minbus; bus <= maxbus; bus++) {
    520  1.3      uch 		maxdevs = pci_bus_maxdevs(pc, bus);
    521  1.3      uch 		for (device = 0; device < maxdevs; device++) {
    522  1.3      uch 			tag = pci_make_tag(pc, bus, device, 0);
    523  1.3      uch 			id = pci_conf_read(pc, tag, PCI_ID_REG);
    524  1.3      uch 
    525  1.3      uch 			/* Invalid vendor ID value? */
    526  1.3      uch 			if (PCI_VENDOR(id) == PCI_VENDOR_INVALID)
    527  1.3      uch 				continue;
    528  1.3      uch 			/* XXX Not invalid, but we've done this ~forever. */
    529  1.3      uch 			if (PCI_VENDOR(id) == 0)
    530  1.3      uch 				continue;
    531  1.3      uch 
    532  1.3      uch 			qd = pci_lookup_quirkdata(PCI_VENDOR(id),
    533  1.3      uch 			    PCI_PRODUCT(id));
    534  1.3      uch 
    535  1.3      uch 			bhlcr = pci_conf_read(pc, tag, PCI_BHLC_REG);
    536  1.3      uch 			if (PCI_HDRTYPE_MULTIFN(bhlcr) ||
    537  1.3      uch 			    (qd != NULL &&
    538  1.3      uch 			     (qd->quirks & PCI_QUIRK_MULTIFUNCTION) != 0))
    539  1.3      uch 				nfuncs = 8;
    540  1.3      uch 			else
    541  1.3      uch 				nfuncs = 1;
    542  1.3      uch 
    543  1.3      uch 			for (function = 0; function < nfuncs; function++) {
    544  1.3      uch 				tag = pci_make_tag(pc, bus, device, function);
    545  1.3      uch 				id = pci_conf_read(pc, tag, PCI_ID_REG);
    546  1.3      uch 
    547  1.3      uch 				/* Invalid vendor ID value? */
    548  1.3      uch 				if (PCI_VENDOR(id) == PCI_VENDOR_INVALID)
    549  1.3      uch 					continue;
    550  1.3      uch 				/*
    551  1.3      uch 				 * XXX Not invalid, but we've done this
    552  1.3      uch 				 * ~forever.
    553  1.3      uch 				 */
    554  1.3      uch 				if (PCI_VENDOR(id) == 0)
    555  1.3      uch 					continue;
    556  1.6      mcr 				(*func)(pc, tag, context);
    557  1.3      uch 			}
    558  1.3      uch 		}
    559  1.8      uch 	}
    560  1.8      uch }
    561  1.8      uch 
    562  1.8      uch void
    563  1.8      uch pci_bridge_foreach(pci_chipset_tag_t pc, int minbus, int maxbus,
    564  1.8      uch     void (*func)(pci_chipset_tag_t, pcitag_t, void *), void *ctx)
    565  1.8      uch {
    566  1.8      uch 	struct pci_bridge_hook_arg bridge_hook;
    567  1.8      uch 
    568  1.8      uch 	bridge_hook.func = func;
    569  1.8      uch 	bridge_hook.arg = ctx;
    570  1.8      uch 
    571  1.8      uch 	pci_device_foreach_min(pc, minbus, maxbus, pci_bridge_hook,
    572  1.8      uch 	    &bridge_hook);
    573  1.8      uch }
    574  1.8      uch 
    575  1.8      uch void
    576  1.8      uch pci_bridge_hook(pci_chipset_tag_t pc, pcitag_t tag, void *ctx)
    577  1.8      uch {
    578  1.8      uch 	struct pci_bridge_hook_arg *bridge_hook = (void *)ctx;
    579  1.8      uch 	pcireg_t reg;
    580  1.8      uch 
    581  1.8      uch 	reg = pci_conf_read(pc, tag, PCI_CLASS_REG);
    582  1.8      uch 	if (PCI_CLASS(reg) == PCI_CLASS_BRIDGE &&
    583  1.8      uch 	    (PCI_SUBCLASS(reg) == PCI_SUBCLASS_BRIDGE_PCI ||
    584  1.8      uch 		PCI_SUBCLASS(reg) == PCI_SUBCLASS_BRIDGE_CARDBUS)) {
    585  1.8      uch 		(*bridge_hook->func)(pc, tag, bridge_hook->arg);
    586  1.3      uch 	}
    587  1.3      uch }
    588