Home | History | Annotate | Line # | Download | only in pci
pci.c revision 1.31.2.2
      1  1.31.2.2  thorpej /*	$NetBSD: pci.c,v 1.31.2.2 1998/01/31 00:37:40 thorpej Exp $	*/
      2  1.31.2.2  thorpej 
      3  1.31.2.2  thorpej /*
      4  1.31.2.2  thorpej  * Copyright (c) 1995, 1996, 1997
      5  1.31.2.2  thorpej  *     Christopher G. Demetriou.  All rights reserved.
      6  1.31.2.2  thorpej  * Copyright (c) 1994 Charles Hannum.  All rights reserved.
      7  1.31.2.2  thorpej  *
      8  1.31.2.2  thorpej  * Redistribution and use in source and binary forms, with or without
      9  1.31.2.2  thorpej  * modification, are permitted provided that the following conditions
     10  1.31.2.2  thorpej  * are met:
     11  1.31.2.2  thorpej  * 1. Redistributions of source code must retain the above copyright
     12  1.31.2.2  thorpej  *    notice, this list of conditions and the following disclaimer.
     13  1.31.2.2  thorpej  * 2. Redistributions in binary form must reproduce the above copyright
     14  1.31.2.2  thorpej  *    notice, this list of conditions and the following disclaimer in the
     15  1.31.2.2  thorpej  *    documentation and/or other materials provided with the distribution.
     16  1.31.2.2  thorpej  * 3. All advertising materials mentioning features or use of this software
     17  1.31.2.2  thorpej  *    must display the following acknowledgement:
     18  1.31.2.2  thorpej  *	This product includes software developed by Charles Hannum.
     19  1.31.2.2  thorpej  * 4. The name of the author may not be used to endorse or promote products
     20  1.31.2.2  thorpej  *    derived from this software without specific prior written permission.
     21  1.31.2.2  thorpej  *
     22  1.31.2.2  thorpej  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
     23  1.31.2.2  thorpej  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
     24  1.31.2.2  thorpej  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
     25  1.31.2.2  thorpej  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
     26  1.31.2.2  thorpej  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
     27  1.31.2.2  thorpej  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
     28  1.31.2.2  thorpej  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
     29  1.31.2.2  thorpej  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
     30  1.31.2.2  thorpej  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
     31  1.31.2.2  thorpej  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     32  1.31.2.2  thorpej  */
     33  1.31.2.2  thorpej 
     34  1.31.2.2  thorpej /*
     35  1.31.2.2  thorpej  * PCI bus autoconfiguration.
     36  1.31.2.2  thorpej  */
     37  1.31.2.2  thorpej 
     38  1.31.2.2  thorpej #include <sys/param.h>
     39  1.31.2.2  thorpej #include <sys/systm.h>
     40  1.31.2.2  thorpej #include <sys/device.h>
     41  1.31.2.2  thorpej 
     42  1.31.2.2  thorpej #include <dev/pci/pcireg.h>
     43  1.31.2.2  thorpej #include <dev/pci/pcivar.h>
     44  1.31.2.2  thorpej 
     45  1.31.2.2  thorpej #ifdef __BROKEN_INDIRECT_CONFIG
     46  1.31.2.2  thorpej int pcimatch __P((struct device *, void *, void *));
     47  1.31.2.2  thorpej #else
     48  1.31.2.2  thorpej int pcimatch __P((struct device *, struct cfdata *, void *));
     49  1.31.2.2  thorpej #endif
     50  1.31.2.2  thorpej void pciattach __P((struct device *, struct device *, void *));
     51  1.31.2.2  thorpej 
     52  1.31.2.2  thorpej struct cfattach pci_ca = {
     53  1.31.2.2  thorpej 	sizeof(struct device), pcimatch, pciattach
     54  1.31.2.2  thorpej };
     55  1.31.2.2  thorpej 
     56  1.31.2.2  thorpej int	pciprint __P((void *, const char *));
     57  1.31.2.2  thorpej #ifdef __BROKEN_INDIRECT_CONFIG
     58  1.31.2.2  thorpej int	pcisubmatch __P((struct device *, void *, void *));
     59  1.31.2.2  thorpej #else
     60  1.31.2.2  thorpej int	pcisubmatch __P((struct device *, struct cfdata *, void *));
     61  1.31.2.2  thorpej #endif
     62  1.31.2.2  thorpej 
     63  1.31.2.2  thorpej /*
     64  1.31.2.2  thorpej  * Callback so that ISA/EISA bridges can attach their child busses
     65  1.31.2.2  thorpej  * after PCI configuration is done.
     66  1.31.2.2  thorpej  *
     67  1.31.2.2  thorpej  * This works because:
     68  1.31.2.2  thorpej  *	(1) there can be at most one ISA/EISA bridge per PCI bus, and
     69  1.31.2.2  thorpej  *	(2) any ISA/EISA bridges must be attached to primary PCI
     70  1.31.2.2  thorpej  *	    busses (i.e. bus zero).
     71  1.31.2.2  thorpej  *
     72  1.31.2.2  thorpej  * That boils down to: there can only be one of these outstanding
     73  1.31.2.2  thorpej  * at a time, it is cleared when configuring PCI bus 0 before any
     74  1.31.2.2  thorpej  * subdevices have been found, and it is run after all subdevices
     75  1.31.2.2  thorpej  * of PCI bus 0 have been found.
     76  1.31.2.2  thorpej  *
     77  1.31.2.2  thorpej  * This is needed because there are some (legacy) PCI devices which
     78  1.31.2.2  thorpej  * can show up as ISA/EISA devices as well (the prime example of which
     79  1.31.2.2  thorpej  * are VGA controllers).  If you attach ISA from a PCI-ISA/EISA bridge,
     80  1.31.2.2  thorpej  * and the bridge is seen before the video board is, the board can show
     81  1.31.2.2  thorpej  * up as an ISA device, and that can (bogusly) complicate the PCI device's
     82  1.31.2.2  thorpej  * attach code, or make the PCI device not be properly attached at all.
     83  1.31.2.2  thorpej  */
     84  1.31.2.2  thorpej static void	(*pci_isa_bridge_callback) __P((void *));
     85  1.31.2.2  thorpej static void	*pci_isa_bridge_callback_arg;
     86  1.31.2.2  thorpej 
     87  1.31.2.2  thorpej int
     88  1.31.2.2  thorpej #ifdef __BROKEN_INDIRECT_CONFIG
     89  1.31.2.2  thorpej pcimatch(parent, match, aux)
     90  1.31.2.2  thorpej #else
     91  1.31.2.2  thorpej pcimatch(parent, cf, aux)
     92  1.31.2.2  thorpej #endif
     93  1.31.2.2  thorpej 	struct device *parent;
     94  1.31.2.2  thorpej #ifdef __BROKEN_INDIRECT_CONFIG
     95  1.31.2.2  thorpej 	void *match;
     96  1.31.2.2  thorpej #else
     97  1.31.2.2  thorpej 	struct cfdata *cf;
     98  1.31.2.2  thorpej #endif
     99  1.31.2.2  thorpej 	void *aux;
    100  1.31.2.2  thorpej {
    101  1.31.2.2  thorpej #ifdef __BROKEN_INDIRECT_CONFIG
    102  1.31.2.2  thorpej 	struct cfdata *cf = match;
    103  1.31.2.2  thorpej #endif
    104  1.31.2.2  thorpej 	struct pcibus_attach_args *pba = aux;
    105  1.31.2.2  thorpej 
    106  1.31.2.2  thorpej 	if (strcmp(pba->pba_busname, cf->cf_driver->cd_name))
    107  1.31.2.2  thorpej 		return (0);
    108  1.31.2.2  thorpej 
    109  1.31.2.2  thorpej 	/* Check the locators */
    110  1.31.2.2  thorpej 	if (cf->pcibuscf_bus != PCIBUS_UNK_BUS &&
    111  1.31.2.2  thorpej 	    cf->pcibuscf_bus != pba->pba_bus)
    112  1.31.2.2  thorpej 		return (0);
    113  1.31.2.2  thorpej 
    114  1.31.2.2  thorpej 	/* sanity */
    115  1.31.2.2  thorpej 	if (pba->pba_bus < 0 || pba->pba_bus > 255)
    116  1.31.2.2  thorpej 		return (0);
    117  1.31.2.2  thorpej 
    118  1.31.2.2  thorpej 	/*
    119  1.31.2.2  thorpej 	 * XXX check other (hardware?) indicators
    120  1.31.2.2  thorpej 	 */
    121  1.31.2.2  thorpej 
    122  1.31.2.2  thorpej 	return 1;
    123  1.31.2.2  thorpej }
    124  1.31.2.2  thorpej 
    125  1.31.2.2  thorpej void
    126  1.31.2.2  thorpej pciattach(parent, self, aux)
    127  1.31.2.2  thorpej 	struct device *parent, *self;
    128  1.31.2.2  thorpej 	void *aux;
    129  1.31.2.2  thorpej {
    130  1.31.2.2  thorpej 	struct pcibus_attach_args *pba = aux;
    131  1.31.2.2  thorpej 	bus_space_tag_t iot, memt;
    132  1.31.2.2  thorpej 	pci_chipset_tag_t pc;
    133  1.31.2.2  thorpej 	int bus, device, maxndevs, function, nfunctions;
    134  1.31.2.2  thorpej 	int io_enabled, mem_enabled;
    135  1.31.2.2  thorpej 
    136  1.31.2.2  thorpej 	pci_attach_hook(parent, self, pba);
    137  1.31.2.2  thorpej 	printf("\n");
    138  1.31.2.2  thorpej 
    139  1.31.2.2  thorpej 	io_enabled = (pba->pba_flags & PCI_FLAGS_IO_ENABLED);
    140  1.31.2.2  thorpej 	mem_enabled = (pba->pba_flags & PCI_FLAGS_MEM_ENABLED);
    141  1.31.2.2  thorpej 
    142  1.31.2.2  thorpej 	if (io_enabled == 0 && mem_enabled == 0) {
    143  1.31.2.2  thorpej 		printf("%s: no spaces enabled!\n", self->dv_xname);
    144  1.31.2.2  thorpej 		return;
    145  1.31.2.2  thorpej 	}
    146  1.31.2.2  thorpej 
    147  1.31.2.2  thorpej 	printf("%s: ", self->dv_xname);
    148  1.31.2.2  thorpej 	if (io_enabled)
    149  1.31.2.2  thorpej 		printf("i/o enabled");
    150  1.31.2.2  thorpej 	if (mem_enabled) {
    151  1.31.2.2  thorpej 		if (io_enabled)
    152  1.31.2.2  thorpej 			printf(", ");
    153  1.31.2.2  thorpej 		printf("memory enabled");
    154  1.31.2.2  thorpej 	}
    155  1.31.2.2  thorpej 	printf("\n");
    156  1.31.2.2  thorpej 
    157  1.31.2.2  thorpej 	iot = pba->pba_iot;
    158  1.31.2.2  thorpej 	memt = pba->pba_memt;
    159  1.31.2.2  thorpej 	pc = pba->pba_pc;
    160  1.31.2.2  thorpej 	bus = pba->pba_bus;
    161  1.31.2.2  thorpej 	maxndevs = pci_bus_maxdevs(pc, bus);
    162  1.31.2.2  thorpej 
    163  1.31.2.2  thorpej 	if (bus == 0)
    164  1.31.2.2  thorpej 		pci_isa_bridge_callback = NULL;
    165  1.31.2.2  thorpej 
    166  1.31.2.2  thorpej 	for (device = 0; device < maxndevs; device++) {
    167  1.31.2.2  thorpej 		pcitag_t tag;
    168  1.31.2.2  thorpej 		pcireg_t id, class, intr, bhlcr, csr;
    169  1.31.2.2  thorpej 		struct pci_attach_args pa;
    170  1.31.2.2  thorpej 		int pin;
    171  1.31.2.2  thorpej 
    172  1.31.2.2  thorpej 		tag = pci_make_tag(pc, bus, device, 0);
    173  1.31.2.2  thorpej 		id = pci_conf_read(pc, tag, PCI_ID_REG);
    174  1.31.2.2  thorpej 		if (id == 0 || id == 0xffffffff)
    175  1.31.2.2  thorpej 			continue;
    176  1.31.2.2  thorpej 
    177  1.31.2.2  thorpej 		bhlcr = pci_conf_read(pc, tag, PCI_BHLC_REG);
    178  1.31.2.2  thorpej 		nfunctions = PCI_HDRTYPE_MULTIFN(bhlcr) ? 8 : 1;
    179  1.31.2.2  thorpej 
    180  1.31.2.2  thorpej 		for (function = 0; function < nfunctions; function++) {
    181  1.31.2.2  thorpej 			tag = pci_make_tag(pc, bus, device, function);
    182  1.31.2.2  thorpej 			id = pci_conf_read(pc, tag, PCI_ID_REG);
    183  1.31.2.2  thorpej 			if (id == 0 || id == 0xffffffff)
    184  1.31.2.2  thorpej 				continue;
    185  1.31.2.2  thorpej 			csr = pci_conf_read(pc, tag, PCI_COMMAND_STATUS_REG);
    186  1.31.2.2  thorpej 			class = pci_conf_read(pc, tag, PCI_CLASS_REG);
    187  1.31.2.2  thorpej 			intr = pci_conf_read(pc, tag, PCI_INTERRUPT_REG);
    188  1.31.2.2  thorpej 
    189  1.31.2.2  thorpej 			pa.pa_iot = iot;
    190  1.31.2.2  thorpej 			pa.pa_memt = memt;
    191  1.31.2.2  thorpej 			pa.pa_dmat = pba->pba_dmat;
    192  1.31.2.2  thorpej 			pa.pa_pc = pc;
    193  1.31.2.2  thorpej 			pa.pa_device = device;
    194  1.31.2.2  thorpej 			pa.pa_function = function;
    195  1.31.2.2  thorpej 			pa.pa_tag = tag;
    196  1.31.2.2  thorpej 			pa.pa_id = id;
    197  1.31.2.2  thorpej 			pa.pa_class = class;
    198  1.31.2.2  thorpej 
    199  1.31.2.2  thorpej 			/* set up memory and I/O enable flags as appropriate */
    200  1.31.2.2  thorpej 			pa.pa_flags = 0;
    201  1.31.2.2  thorpej 			if ((pba->pba_flags & PCI_FLAGS_IO_ENABLED) &&
    202  1.31.2.2  thorpej 			    (csr & PCI_COMMAND_IO_ENABLE))
    203  1.31.2.2  thorpej 				pa.pa_flags |= PCI_FLAGS_IO_ENABLED;
    204  1.31.2.2  thorpej 			if ((pba->pba_flags & PCI_FLAGS_MEM_ENABLED) &&
    205  1.31.2.2  thorpej 			    (csr & PCI_COMMAND_MEM_ENABLE))
    206  1.31.2.2  thorpej 				pa.pa_flags |= PCI_FLAGS_MEM_ENABLED;
    207  1.31.2.2  thorpej 
    208  1.31.2.2  thorpej 			if (bus == 0) {
    209  1.31.2.2  thorpej 				pa.pa_intrswiz = 0;
    210  1.31.2.2  thorpej 				pa.pa_intrtag = tag;
    211  1.31.2.2  thorpej 			} else {
    212  1.31.2.2  thorpej 				pa.pa_intrswiz = pba->pba_intrswiz + device;
    213  1.31.2.2  thorpej 				pa.pa_intrtag = pba->pba_intrtag;
    214  1.31.2.2  thorpej 			}
    215  1.31.2.2  thorpej 			pin = PCI_INTERRUPT_PIN(intr);
    216  1.31.2.2  thorpej 			if (pin == PCI_INTERRUPT_PIN_NONE) {
    217  1.31.2.2  thorpej 				/* no interrupt */
    218  1.31.2.2  thorpej 				pa.pa_intrpin = 0;
    219  1.31.2.2  thorpej 			} else {
    220  1.31.2.2  thorpej 				/*
    221  1.31.2.2  thorpej 				 * swizzle it based on the number of
    222  1.31.2.2  thorpej 				 * busses we're behind and our device
    223  1.31.2.2  thorpej 				 * number.
    224  1.31.2.2  thorpej 				 */
    225  1.31.2.2  thorpej 				pa.pa_intrpin =			/* XXX */
    226  1.31.2.2  thorpej 				    ((pin + pa.pa_intrswiz - 1) % 4) + 1;
    227  1.31.2.2  thorpej 			}
    228  1.31.2.2  thorpej 			pa.pa_intrline = PCI_INTERRUPT_LINE(intr);
    229  1.31.2.2  thorpej 
    230  1.31.2.2  thorpej 			config_found_sm(self, &pa, pciprint, pcisubmatch);
    231  1.31.2.2  thorpej 		}
    232  1.31.2.2  thorpej 	}
    233  1.31.2.2  thorpej 
    234  1.31.2.2  thorpej 	if (bus == 0 && pci_isa_bridge_callback != NULL)
    235  1.31.2.2  thorpej 		(*pci_isa_bridge_callback)(pci_isa_bridge_callback_arg);
    236  1.31.2.2  thorpej }
    237  1.31.2.2  thorpej 
    238  1.31.2.2  thorpej int
    239  1.31.2.2  thorpej pciprint(aux, pnp)
    240  1.31.2.2  thorpej 	void *aux;
    241  1.31.2.2  thorpej 	const char *pnp;
    242  1.31.2.2  thorpej {
    243  1.31.2.2  thorpej 	register struct pci_attach_args *pa = aux;
    244  1.31.2.2  thorpej 	char devinfo[256];
    245  1.31.2.2  thorpej 
    246  1.31.2.2  thorpej 	if (pnp) {
    247  1.31.2.2  thorpej 		pci_devinfo(pa->pa_id, pa->pa_class, 1, devinfo);
    248  1.31.2.2  thorpej 		printf("%s at %s", devinfo, pnp);
    249  1.31.2.2  thorpej 	}
    250  1.31.2.2  thorpej 	printf(" dev %d function %d", pa->pa_device, pa->pa_function);
    251  1.31.2.2  thorpej #if 0
    252  1.31.2.2  thorpej 	printf(" (%si/o, %smem)",
    253  1.31.2.2  thorpej 	    pa->pa_flags & PCI_FLAGS_IO_ENABLED ? "" : "no ",
    254  1.31.2.2  thorpej 	    pa->pa_flags & PCI_FLAGS_MEM_ENABLED ? "" : "no ");
    255  1.31.2.2  thorpej #endif
    256  1.31.2.2  thorpej 	return (UNCONF);
    257  1.31.2.2  thorpej }
    258  1.31.2.2  thorpej 
    259  1.31.2.2  thorpej int
    260  1.31.2.2  thorpej #ifdef __BROKEN_INDIRECT_CONFIG
    261  1.31.2.2  thorpej pcisubmatch(parent, match, aux)
    262  1.31.2.2  thorpej #else
    263  1.31.2.2  thorpej pcisubmatch(parent, cf, aux)
    264  1.31.2.2  thorpej #endif
    265  1.31.2.2  thorpej 	struct device *parent;
    266  1.31.2.2  thorpej #ifdef __BROKEN_INDIRECT_CONFIG
    267  1.31.2.2  thorpej 	void *match;
    268  1.31.2.2  thorpej #else
    269  1.31.2.2  thorpej 	struct cfdata *cf;
    270  1.31.2.2  thorpej #endif
    271  1.31.2.2  thorpej 	void *aux;
    272  1.31.2.2  thorpej {
    273  1.31.2.2  thorpej #ifdef __BROKEN_INDIRECT_CONFIG
    274  1.31.2.2  thorpej 	struct cfdata *cf = match;
    275  1.31.2.2  thorpej #endif
    276  1.31.2.2  thorpej 	struct pci_attach_args *pa = aux;
    277  1.31.2.2  thorpej 
    278  1.31.2.2  thorpej 	if (cf->pcicf_dev != PCI_UNK_DEV &&
    279  1.31.2.2  thorpej 	    cf->pcicf_dev != pa->pa_device)
    280  1.31.2.2  thorpej 		return 0;
    281  1.31.2.2  thorpej 	if (cf->pcicf_function != PCI_UNK_FUNCTION &&
    282  1.31.2.2  thorpej 	    cf->pcicf_function != pa->pa_function)
    283  1.31.2.2  thorpej 		return 0;
    284  1.31.2.2  thorpej 	return ((*cf->cf_attach->ca_match)(parent, cf, aux));
    285  1.31.2.2  thorpej }
    286  1.31.2.2  thorpej 
    287  1.31.2.2  thorpej void
    288  1.31.2.2  thorpej set_pci_isa_bridge_callback(fn, arg)
    289  1.31.2.2  thorpej 	void (*fn) __P((void *));
    290  1.31.2.2  thorpej 	void *arg;
    291  1.31.2.2  thorpej {
    292  1.31.2.2  thorpej 
    293  1.31.2.2  thorpej 	if (pci_isa_bridge_callback != NULL)
    294  1.31.2.2  thorpej 		panic("set_pci_isa_bridge_callback");
    295  1.31.2.2  thorpej 	pci_isa_bridge_callback = fn;
    296  1.31.2.2  thorpej 	pci_isa_bridge_callback_arg = arg;
    297  1.31.2.2  thorpej }
    298