Home | History | Annotate | Line # | Download | only in pci
pci.c revision 1.18
      1 /*	$NetBSD: pci.c,v 1.18 1996/03/27 04:08:24 cgd Exp $	*/
      2 
      3 /*
      4  * Copyright (c) 1995, 1996 Christopher G. Demetriou.  All rights reserved.
      5  * Copyright (c) 1994 Charles Hannum.  All rights reserved.
      6  *
      7  * Redistribution and use in source and binary forms, with or without
      8  * modification, are permitted provided that the following conditions
      9  * are met:
     10  * 1. Redistributions of source code must retain the above copyright
     11  *    notice, this list of conditions and the following disclaimer.
     12  * 2. Redistributions in binary form must reproduce the above copyright
     13  *    notice, this list of conditions and the following disclaimer in the
     14  *    documentation and/or other materials provided with the distribution.
     15  * 3. All advertising materials mentioning features or use of this software
     16  *    must display the following acknowledgement:
     17  *	This product includes software developed by Charles Hannum.
     18  * 4. The name of the author may not be used to endorse or promote products
     19  *    derived from this software without specific prior written permission.
     20  *
     21  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
     22  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
     23  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
     24  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
     25  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
     26  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
     27  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
     28  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
     29  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
     30  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     31  */
     32 
     33 /*
     34  * PCI bus autoconfiguration.
     35  */
     36 
     37 #include <sys/param.h>
     38 #include <sys/systm.h>
     39 #include <sys/device.h>
     40 
     41 #include <dev/pci/pcireg.h>
     42 #include <dev/pci/pcivar.h>
     43 
     44 int pcimatch __P((struct device *, void *, void *));
     45 void pciattach __P((struct device *, struct device *, void *));
     46 
     47 struct cfattach pci_ca = {
     48 	sizeof(struct device), pcimatch, pciattach
     49 };
     50 
     51 struct cfdriver pci_cd = {
     52 	NULL, "pci", DV_DULL
     53 };
     54 
     55 int	pciprint __P((void *, char *));
     56 int	pcisubmatch __P((struct device *, void *, void *));
     57 
     58 int
     59 pcimatch(parent, match, aux)
     60 	struct device *parent;
     61 	void *match, *aux;
     62 {
     63 	struct cfdata *cf = match;
     64 	struct pcibus_attach_args *pba = aux;
     65 
     66 	if (strcmp(pba->pba_busname, cf->cf_driver->cd_name))
     67 		return (0);
     68 
     69 	/* Check the locators */
     70 	if (cf->pcibuscf_bus != PCIBUS_UNK_BUS &&
     71 	    cf->pcibuscf_bus != pba->pba_bus)
     72 		return (0);
     73 
     74 	/* sanity */
     75 	if (pba->pba_bus < 0 || pba->pba_bus > 255)
     76 		return (0);
     77 
     78 	/*
     79 	 * XXX check other (hardware?) indicators
     80 	 */
     81 
     82 	return 1;
     83 }
     84 
     85 void
     86 pciattach(parent, self, aux)
     87 	struct device *parent, *self;
     88 	void *aux;
     89 {
     90 	struct pcibus_attach_args *pba = aux;
     91 	bus_chipset_tag_t bc;
     92 	pci_chipset_tag_t pc;
     93 	int bus, device, maxndevs, function, nfunctions;
     94 
     95 	pci_attach_hook(parent, self, pba);
     96 	printf("\n");
     97 
     98 	bc = pba->pba_bc;
     99 	pc = pba->pba_pc;
    100 	bus = pba->pba_bus;
    101 	maxndevs = pci_bus_maxdevs(pc, bus);
    102 
    103 	for (device = 0; device < maxndevs; device++) {
    104 		pcitag_t tag;
    105 		pcireg_t id, class, intr, bhlcr;
    106 		struct pci_attach_args pa;
    107 		struct cfdata *cf;
    108 		int supported, pin;
    109 
    110 		tag = pci_make_tag(pc, bus, device, 0);
    111 		id = pci_conf_read(pc, tag, PCI_ID_REG);
    112 		if (id == 0 || id == 0xffffffff)
    113 			continue;
    114 
    115 		bhlcr = pci_conf_read(pc, tag, PCI_BHLC_REG);
    116 		nfunctions = PCI_HDRTYPE_MULTIFN(bhlcr) ? 8 : 1;
    117 
    118 		for (function = 0; function < nfunctions; function++) {
    119 			tag = pci_make_tag(pc, bus, device, function);
    120 			id = pci_conf_read(pc, tag, PCI_ID_REG);
    121 			if (id == 0 || id == 0xffffffff)
    122 				continue;
    123 			class = pci_conf_read(pc, tag, PCI_CLASS_REG);
    124 			intr = pci_conf_read(pc, tag, PCI_INTERRUPT_REG);
    125 
    126 			pa.pa_bc = bc;
    127 			pa.pa_pc = pc;
    128 			pa.pa_device = device;
    129 			pa.pa_function = function;
    130 			pa.pa_tag = tag;
    131 			pa.pa_id = id;
    132 			pa.pa_class = class;
    133 
    134 			if (bus == 0) {
    135 				pa.pa_intrswiz = 0;
    136 				pa.pa_intrtag = tag;
    137 			} else {
    138 				pa.pa_intrswiz = pba->pba_intrswiz + device;
    139 				pa.pa_intrtag = pba->pba_intrtag;
    140 			}
    141 			pin = PCI_INTERRUPT_PIN(intr);
    142 			if (pin == PCI_INTERRUPT_PIN_NONE) {
    143 				/* no interrupt */
    144 				pa.pa_intrpin = 0;
    145 			} else {
    146 				/*
    147 				 * swizzle it based on the number of
    148 				 * busses we're behind and our device
    149 				 * number.
    150 				 */
    151 				pa.pa_intrpin =			/* XXX */
    152 				    ((pin + pa.pa_intrswiz - 1) % 4) + 1;
    153 			}
    154 			pa.pa_intrline = PCI_INTERRUPT_LINE(intr);
    155 
    156 			config_found_sm(self, &pa, pciprint, pcisubmatch);
    157 		}
    158 	}
    159 }
    160 
    161 int
    162 pciprint(aux, pnp)
    163 	void *aux;
    164 	char *pnp;
    165 {
    166 	register struct pci_attach_args *pa = aux;
    167 	char devinfo[256];
    168 
    169 	if (pnp) {
    170 		pci_devinfo(pa->pa_id, pa->pa_class, 1, devinfo);
    171 		printf("%s at %s", devinfo, pnp);
    172 	}
    173 	printf(" dev %d function %d", pa->pa_device, pa->pa_function);
    174 	return (UNCONF);
    175 }
    176 
    177 int
    178 pcisubmatch(parent, match, aux)
    179 	struct device *parent;
    180 	void *match, *aux;
    181 {
    182 	struct cfdata *cf = match;
    183 	struct pci_attach_args *pa = aux;
    184 
    185 	if (cf->pcicf_dev != PCI_UNK_DEV &&
    186 	    cf->pcicf_dev != pa->pa_device)
    187 		return 0;
    188 	if (cf->pcicf_function != PCI_UNK_FUNCTION &&
    189 	    cf->pcicf_function != pa->pa_function)
    190 		return 0;
    191 	return ((*cf->cf_attach->ca_match)(parent, match, aux));
    192 }
    193 
    194 int
    195 pci_io_find(pc, pcitag, reg, iobasep, iosizep)
    196 	pci_chipset_tag_t pc;
    197 	pcitag_t pcitag;
    198 	int reg;
    199 	bus_io_addr_t *iobasep;
    200 	bus_io_size_t *iosizep;
    201 {
    202 	pcireg_t addrdata, sizedata;
    203 	int s;
    204 
    205 	if (reg < PCI_MAPREG_START || reg >= PCI_MAPREG_END || (reg & 3))
    206 		panic("pci_io_find: bad request");
    207 
    208 	/* XXX?
    209 	 * Section 6.2.5.1, `Address Maps', tells us that:
    210 	 *
    211 	 * 1) The builtin software should have already mapped the device in a
    212 	 * reasonable way.
    213 	 *
    214 	 * 2) A device which wants 2^n bytes of memory will hardwire the bottom
    215 	 * n bits of the address to 0.  As recommended, we write all 1s and see
    216 	 * what we get back.
    217 	 */
    218 	addrdata = pci_conf_read(pc, pcitag, reg);
    219 
    220 	s = splhigh();
    221 	pci_conf_write(pc, pcitag, reg, 0xffffffff);
    222 	sizedata = pci_conf_read(pc, pcitag, reg);
    223 	pci_conf_write(pc, pcitag, reg, addrdata);
    224 	splx(s);
    225 
    226 	if (PCI_MAPREG_TYPE(addrdata) != PCI_MAPREG_TYPE_IO)
    227 		panic("pci_io_find: not an I/O region");
    228 
    229 	if (iobasep != NULL)
    230 		*iobasep = PCI_MAPREG_IO_ADDR(addrdata);
    231 	if (iosizep != NULL)
    232 		*iosizep = ~PCI_MAPREG_IO_ADDR(sizedata) + 1;
    233 
    234 	return (0);
    235 }
    236 
    237 int
    238 pci_mem_find(pc, pcitag, reg, membasep, memsizep, cacheablep)
    239 	pci_chipset_tag_t pc;
    240 	pcitag_t pcitag;
    241 	int reg;
    242 	bus_mem_addr_t *membasep;
    243 	bus_mem_size_t *memsizep;
    244 	int *cacheablep;
    245 {
    246 	pcireg_t addrdata, sizedata;
    247 	int s;
    248 
    249 	if (reg < PCI_MAPREG_START || reg >= PCI_MAPREG_END || (reg & 3))
    250 		panic("pci_find_mem: bad request");
    251 
    252 	/*
    253 	 * Section 6.2.5.1, `Address Maps', tells us that:
    254 	 *
    255 	 * 1) The builtin software should have already mapped the device in a
    256 	 * reasonable way.
    257 	 *
    258 	 * 2) A device which wants 2^n bytes of memory will hardwire the bottom
    259 	 * n bits of the address to 0.  As recommended, we write all 1s and see
    260 	 * what we get back.
    261 	 */
    262 	addrdata = pci_conf_read(pc, pcitag, reg);
    263 
    264 	s = splhigh();
    265 	pci_conf_write(pc, pcitag, reg, 0xffffffff);
    266 	sizedata = pci_conf_read(pc, pcitag, reg);
    267 	pci_conf_write(pc, pcitag, reg, addrdata);
    268 	splx(s);
    269 
    270 	if (PCI_MAPREG_TYPE(addrdata) == PCI_MAPREG_TYPE_IO)
    271 		panic("pci_find_mem: I/O region");
    272 
    273 	switch (PCI_MAPREG_MEM_TYPE(addrdata)) {
    274 	case PCI_MAPREG_MEM_TYPE_32BIT:
    275 	case PCI_MAPREG_MEM_TYPE_32BIT_1M:
    276 		break;
    277 	case PCI_MAPREG_MEM_TYPE_64BIT:
    278 /* XXX */	printf("pci_find_mem: 64-bit region\n");
    279 /* XXX */	return (1);
    280 	default:
    281 		printf("pci_find_mem: reserved region type\n");
    282 		return (1);
    283 	}
    284 
    285 	if (membasep != NULL)
    286 		*membasep = PCI_MAPREG_MEM_ADDR(addrdata);	/* PCI addr */
    287 	if (memsizep != NULL)
    288 		*memsizep = ~PCI_MAPREG_MEM_ADDR(sizedata) + 1;
    289 	if (cacheablep != NULL)
    290 		*cacheablep = PCI_MAPREG_MEM_CACHEABLE(addrdata);
    291 
    292 	return 0;
    293 }
    294