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