Home | History | Annotate | Line # | Download | only in pci
pci.c revision 1.27
      1  1.27       cgd /*	$NetBSD: pci.c,v 1.27 1997/04/10 23:12:22 cgd Exp $	*/
      2   1.3       cgd 
      3   1.1   mycroft /*
      4  1.27       cgd  * Copyright (c) 1995, 1996, 1997
      5  1.27       cgd  *     Christopher G. Demetriou.  All rights reserved.
      6   1.1   mycroft  * Copyright (c) 1994 Charles Hannum.  All rights reserved.
      7   1.1   mycroft  *
      8   1.1   mycroft  * Redistribution and use in source and binary forms, with or without
      9   1.1   mycroft  * modification, are permitted provided that the following conditions
     10   1.1   mycroft  * are met:
     11   1.1   mycroft  * 1. Redistributions of source code must retain the above copyright
     12   1.1   mycroft  *    notice, this list of conditions and the following disclaimer.
     13   1.1   mycroft  * 2. Redistributions in binary form must reproduce the above copyright
     14   1.1   mycroft  *    notice, this list of conditions and the following disclaimer in the
     15   1.1   mycroft  *    documentation and/or other materials provided with the distribution.
     16   1.1   mycroft  * 3. All advertising materials mentioning features or use of this software
     17   1.1   mycroft  *    must display the following acknowledgement:
     18   1.1   mycroft  *	This product includes software developed by Charles Hannum.
     19   1.1   mycroft  * 4. The name of the author may not be used to endorse or promote products
     20   1.1   mycroft  *    derived from this software without specific prior written permission.
     21   1.1   mycroft  *
     22   1.1   mycroft  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
     23   1.1   mycroft  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
     24   1.1   mycroft  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
     25   1.1   mycroft  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
     26   1.1   mycroft  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
     27   1.1   mycroft  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
     28   1.1   mycroft  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
     29   1.1   mycroft  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
     30   1.1   mycroft  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
     31   1.1   mycroft  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     32   1.1   mycroft  */
     33   1.1   mycroft 
     34   1.1   mycroft /*
     35  1.10       cgd  * PCI bus autoconfiguration.
     36   1.1   mycroft  */
     37   1.1   mycroft 
     38   1.1   mycroft #include <sys/param.h>
     39  1.10       cgd #include <sys/systm.h>
     40   1.1   mycroft #include <sys/device.h>
     41   1.1   mycroft 
     42  1.10       cgd #include <dev/pci/pcireg.h>
     43   1.7       cgd #include <dev/pci/pcivar.h>
     44  1.10       cgd 
     45  1.26       cgd #ifdef __BROKEN_INDIRECT_CONFIG
     46  1.10       cgd int pcimatch __P((struct device *, void *, void *));
     47  1.26       cgd #else
     48  1.26       cgd int pcimatch __P((struct device *, struct cfdata *, void *));
     49  1.26       cgd #endif
     50  1.10       cgd void pciattach __P((struct device *, struct device *, void *));
     51  1.10       cgd 
     52  1.16   thorpej struct cfattach pci_ca = {
     53  1.16   thorpej 	sizeof(struct device), pcimatch, pciattach
     54  1.16   thorpej };
     55  1.16   thorpej 
     56  1.16   thorpej struct cfdriver pci_cd = {
     57  1.16   thorpej 	NULL, "pci", DV_DULL
     58  1.10       cgd };
     59  1.10       cgd 
     60  1.21       cgd int	pciprint __P((void *, const char *));
     61  1.26       cgd #ifdef __BROKEN_INDIRECT_CONFIG
     62  1.10       cgd int	pcisubmatch __P((struct device *, void *, void *));
     63  1.26       cgd #else
     64  1.26       cgd int	pcisubmatch __P((struct device *, struct cfdata *, void *));
     65  1.26       cgd #endif
     66  1.10       cgd 
     67  1.25       cgd /*
     68  1.25       cgd  * Callback so that ISA/EISA bridges can attach their child busses
     69  1.25       cgd  * after PCI configuration is done.
     70  1.25       cgd  *
     71  1.25       cgd  * This works because:
     72  1.25       cgd  *	(1) there can be at most one ISA/EISA bridge per PCI bus, and
     73  1.25       cgd  *	(2) any ISA/EISA bridges must be attached to primary PCI
     74  1.25       cgd  *	    busses (i.e. bus zero).
     75  1.25       cgd  *
     76  1.25       cgd  * That boils down to: there can only be one of these outstanding
     77  1.25       cgd  * at a time, it is cleared when configuring PCI bus 0 before any
     78  1.25       cgd  * subdevices have been found, and it is run after all subdevices
     79  1.25       cgd  * of PCI bus 0 have been found.
     80  1.25       cgd  *
     81  1.25       cgd  * This is needed because there are some (legacy) PCI devices which
     82  1.25       cgd  * can show up as ISA/EISA devices as well (the prime example of which
     83  1.25       cgd  * are VGA controllers).  If you attach ISA from a PCI-ISA/EISA bridge,
     84  1.25       cgd  * and the bridge is seen before the video board is, the board can show
     85  1.25       cgd  * up as an ISA device, and that can (bogusly) complicate the PCI device's
     86  1.25       cgd  * attach code, or make the PCI device not be properly attached at all.
     87  1.25       cgd  */
     88  1.25       cgd static void	(*pci_isa_bridge_callback) __P((void *));
     89  1.25       cgd static void	*pci_isa_bridge_callback_arg;
     90  1.25       cgd 
     91  1.10       cgd int
     92  1.26       cgd #ifdef __BROKEN_INDIRECT_CONFIG
     93  1.10       cgd pcimatch(parent, match, aux)
     94  1.26       cgd #else
     95  1.26       cgd pcimatch(parent, cf, aux)
     96  1.26       cgd #endif
     97  1.10       cgd 	struct device *parent;
     98  1.26       cgd #ifdef __BROKEN_INDIRECT_CONFIG
     99  1.26       cgd 	void *match;
    100  1.26       cgd #else
    101  1.26       cgd 	struct cfdata *cf;
    102  1.26       cgd #endif
    103  1.26       cgd 	void *aux;
    104  1.10       cgd {
    105  1.26       cgd #ifdef __BROKEN_INDIRECT_CONFIG
    106  1.10       cgd 	struct cfdata *cf = match;
    107  1.26       cgd #endif
    108  1.10       cgd 	struct pcibus_attach_args *pba = aux;
    109  1.10       cgd 
    110  1.10       cgd 	if (strcmp(pba->pba_busname, cf->cf_driver->cd_name))
    111  1.10       cgd 		return (0);
    112  1.10       cgd 
    113  1.10       cgd 	/* Check the locators */
    114  1.14       cgd 	if (cf->pcibuscf_bus != PCIBUS_UNK_BUS &&
    115  1.14       cgd 	    cf->pcibuscf_bus != pba->pba_bus)
    116  1.10       cgd 		return (0);
    117  1.10       cgd 
    118  1.10       cgd 	/* sanity */
    119  1.10       cgd 	if (pba->pba_bus < 0 || pba->pba_bus > 255)
    120  1.10       cgd 		return (0);
    121  1.10       cgd 
    122  1.10       cgd 	/*
    123  1.10       cgd 	 * XXX check other (hardware?) indicators
    124  1.10       cgd 	 */
    125  1.10       cgd 
    126  1.10       cgd 	return 1;
    127  1.10       cgd }
    128  1.10       cgd 
    129  1.10       cgd void
    130  1.10       cgd pciattach(parent, self, aux)
    131  1.10       cgd 	struct device *parent, *self;
    132  1.10       cgd 	void *aux;
    133  1.10       cgd {
    134  1.10       cgd 	struct pcibus_attach_args *pba = aux;
    135  1.24   thorpej 	bus_space_tag_t iot, memt;
    136  1.18       cgd 	pci_chipset_tag_t pc;
    137  1.18       cgd 	int bus, device, maxndevs, function, nfunctions;
    138  1.10       cgd 
    139  1.18       cgd 	pci_attach_hook(parent, self, pba);
    140  1.23  christos 	printf("\n");
    141  1.10       cgd 
    142  1.24   thorpej 	iot = pba->pba_iot;
    143  1.24   thorpej 	memt = pba->pba_memt;
    144  1.18       cgd 	pc = pba->pba_pc;
    145  1.18       cgd 	bus = pba->pba_bus;
    146  1.18       cgd 	maxndevs = pci_bus_maxdevs(pc, bus);
    147  1.18       cgd 
    148  1.25       cgd 	if (bus == 0)
    149  1.25       cgd 		pci_isa_bridge_callback = NULL;
    150  1.25       cgd 
    151  1.18       cgd 	for (device = 0; device < maxndevs; device++) {
    152  1.10       cgd 		pcitag_t tag;
    153  1.27       cgd 		pcireg_t id, class, intr, bhlcr, csr;
    154  1.10       cgd 		struct pci_attach_args pa;
    155  1.19  christos 		int pin;
    156  1.10       cgd 
    157  1.18       cgd 		tag = pci_make_tag(pc, bus, device, 0);
    158  1.18       cgd 		id = pci_conf_read(pc, tag, PCI_ID_REG);
    159  1.10       cgd 		if (id == 0 || id == 0xffffffff)
    160  1.10       cgd 			continue;
    161  1.10       cgd 
    162  1.18       cgd 		bhlcr = pci_conf_read(pc, tag, PCI_BHLC_REG);
    163  1.17       cgd 		nfunctions = PCI_HDRTYPE_MULTIFN(bhlcr) ? 8 : 1;
    164  1.10       cgd 
    165  1.10       cgd 		for (function = 0; function < nfunctions; function++) {
    166  1.18       cgd 			tag = pci_make_tag(pc, bus, device, function);
    167  1.18       cgd 			id = pci_conf_read(pc, tag, PCI_ID_REG);
    168  1.10       cgd 			if (id == 0 || id == 0xffffffff)
    169  1.10       cgd 				continue;
    170  1.27       cgd 			csr = pci_conf_read(pc, tag, PCI_COMMAND_STATUS_REG);
    171  1.18       cgd 			class = pci_conf_read(pc, tag, PCI_CLASS_REG);
    172  1.18       cgd 			intr = pci_conf_read(pc, tag, PCI_INTERRUPT_REG);
    173  1.10       cgd 
    174  1.24   thorpej 			pa.pa_iot = iot;
    175  1.24   thorpej 			pa.pa_memt = memt;
    176  1.18       cgd 			pa.pa_pc = pc;
    177  1.10       cgd 			pa.pa_device = device;
    178  1.10       cgd 			pa.pa_function = function;
    179  1.10       cgd 			pa.pa_tag = tag;
    180  1.10       cgd 			pa.pa_id = id;
    181  1.10       cgd 			pa.pa_class = class;
    182  1.10       cgd 
    183  1.27       cgd 			/* set up memory and I/O enable flags as appropriate */
    184  1.27       cgd 			pa.pa_flags = 0;
    185  1.27       cgd 			if ((pba->pba_flags & PCI_FLAGS_IO_ENABLED) &&
    186  1.27       cgd 			    (csr & PCI_COMMAND_IO_ENABLE))
    187  1.27       cgd 				pa.pa_flags |= PCI_FLAGS_IO_ENABLED;
    188  1.27       cgd 			if ((pba->pba_flags & PCI_FLAGS_MEM_ENABLED) &&
    189  1.27       cgd 			    (csr & PCI_COMMAND_MEM_ENABLE))
    190  1.27       cgd 				pa.pa_flags |= PCI_FLAGS_MEM_ENABLED;
    191  1.27       cgd 
    192  1.18       cgd 			if (bus == 0) {
    193  1.18       cgd 				pa.pa_intrswiz = 0;
    194  1.18       cgd 				pa.pa_intrtag = tag;
    195  1.18       cgd 			} else {
    196  1.18       cgd 				pa.pa_intrswiz = pba->pba_intrswiz + device;
    197  1.18       cgd 				pa.pa_intrtag = pba->pba_intrtag;
    198  1.18       cgd 			}
    199  1.18       cgd 			pin = PCI_INTERRUPT_PIN(intr);
    200  1.18       cgd 			if (pin == PCI_INTERRUPT_PIN_NONE) {
    201  1.18       cgd 				/* no interrupt */
    202  1.18       cgd 				pa.pa_intrpin = 0;
    203  1.18       cgd 			} else {
    204  1.18       cgd 				/*
    205  1.18       cgd 				 * swizzle it based on the number of
    206  1.18       cgd 				 * busses we're behind and our device
    207  1.18       cgd 				 * number.
    208  1.18       cgd 				 */
    209  1.18       cgd 				pa.pa_intrpin =			/* XXX */
    210  1.18       cgd 				    ((pin + pa.pa_intrswiz - 1) % 4) + 1;
    211  1.18       cgd 			}
    212  1.18       cgd 			pa.pa_intrline = PCI_INTERRUPT_LINE(intr);
    213  1.18       cgd 
    214  1.10       cgd 			config_found_sm(self, &pa, pciprint, pcisubmatch);
    215  1.10       cgd 		}
    216  1.10       cgd 	}
    217  1.25       cgd 
    218  1.25       cgd 	if (bus == 0 && pci_isa_bridge_callback != NULL)
    219  1.25       cgd 		(*pci_isa_bridge_callback)(pci_isa_bridge_callback_arg);
    220  1.10       cgd }
    221   1.1   mycroft 
    222   1.1   mycroft int
    223  1.10       cgd pciprint(aux, pnp)
    224   1.1   mycroft 	void *aux;
    225  1.21       cgd 	const char *pnp;
    226   1.1   mycroft {
    227   1.1   mycroft 	register struct pci_attach_args *pa = aux;
    228  1.10       cgd 	char devinfo[256];
    229   1.1   mycroft 
    230  1.10       cgd 	if (pnp) {
    231  1.10       cgd 		pci_devinfo(pa->pa_id, pa->pa_class, 1, devinfo);
    232  1.23  christos 		printf("%s at %s", devinfo, pnp);
    233  1.10       cgd 	}
    234  1.23  christos 	printf(" dev %d function %d", pa->pa_device, pa->pa_function);
    235  1.27       cgd #if 0
    236  1.27       cgd 	printf(" (%si/o, %smem)",
    237  1.27       cgd 	    pa->pa_flags & PCI_FLAGS_IO_ENABLED ? "" : "no ",
    238  1.27       cgd 	    pa->pa_flags & PCI_FLAGS_MEM_ENABLED ? "" : "no ");
    239  1.27       cgd #endif
    240   1.6   mycroft 	return (UNCONF);
    241   1.6   mycroft }
    242   1.6   mycroft 
    243   1.6   mycroft int
    244  1.26       cgd #ifdef __BROKEN_INDIRECT_CONFIG
    245   1.6   mycroft pcisubmatch(parent, match, aux)
    246  1.26       cgd #else
    247  1.26       cgd pcisubmatch(parent, cf, aux)
    248  1.26       cgd #endif
    249   1.6   mycroft 	struct device *parent;
    250  1.26       cgd #ifdef __BROKEN_INDIRECT_CONFIG
    251  1.26       cgd 	void *match;
    252  1.26       cgd #else
    253  1.26       cgd 	struct cfdata *cf;
    254  1.26       cgd #endif
    255  1.26       cgd 	void *aux;
    256   1.6   mycroft {
    257  1.26       cgd #ifdef __BROKEN_INDIRECT_CONFIG
    258   1.6   mycroft 	struct cfdata *cf = match;
    259  1.26       cgd #endif
    260   1.6   mycroft 	struct pci_attach_args *pa = aux;
    261   1.6   mycroft 
    262  1.14       cgd 	if (cf->pcicf_dev != PCI_UNK_DEV &&
    263  1.14       cgd 	    cf->pcicf_dev != pa->pa_device)
    264   1.6   mycroft 		return 0;
    265  1.14       cgd 	if (cf->pcicf_function != PCI_UNK_FUNCTION &&
    266  1.14       cgd 	    cf->pcicf_function != pa->pa_function)
    267   1.6   mycroft 		return 0;
    268  1.26       cgd 	return ((*cf->cf_attach->ca_match)(parent, cf, aux));
    269  1.18       cgd }
    270  1.18       cgd 
    271  1.18       cgd int
    272  1.18       cgd pci_io_find(pc, pcitag, reg, iobasep, iosizep)
    273  1.18       cgd 	pci_chipset_tag_t pc;
    274  1.18       cgd 	pcitag_t pcitag;
    275  1.18       cgd 	int reg;
    276  1.24   thorpej 	bus_addr_t *iobasep;
    277  1.24   thorpej 	bus_size_t *iosizep;
    278  1.18       cgd {
    279  1.18       cgd 	pcireg_t addrdata, sizedata;
    280  1.18       cgd 	int s;
    281  1.18       cgd 
    282  1.18       cgd 	if (reg < PCI_MAPREG_START || reg >= PCI_MAPREG_END || (reg & 3))
    283  1.18       cgd 		panic("pci_io_find: bad request");
    284  1.18       cgd 
    285  1.18       cgd 	/* XXX?
    286  1.18       cgd 	 * Section 6.2.5.1, `Address Maps', tells us that:
    287  1.18       cgd 	 *
    288  1.18       cgd 	 * 1) The builtin software should have already mapped the device in a
    289  1.18       cgd 	 * reasonable way.
    290  1.18       cgd 	 *
    291  1.18       cgd 	 * 2) A device which wants 2^n bytes of memory will hardwire the bottom
    292  1.18       cgd 	 * n bits of the address to 0.  As recommended, we write all 1s and see
    293  1.18       cgd 	 * what we get back.
    294  1.18       cgd 	 */
    295  1.18       cgd 	addrdata = pci_conf_read(pc, pcitag, reg);
    296  1.18       cgd 
    297  1.18       cgd 	s = splhigh();
    298  1.18       cgd 	pci_conf_write(pc, pcitag, reg, 0xffffffff);
    299  1.18       cgd 	sizedata = pci_conf_read(pc, pcitag, reg);
    300  1.18       cgd 	pci_conf_write(pc, pcitag, reg, addrdata);
    301  1.18       cgd 	splx(s);
    302  1.18       cgd 
    303  1.18       cgd 	if (PCI_MAPREG_TYPE(addrdata) != PCI_MAPREG_TYPE_IO)
    304  1.18       cgd 		panic("pci_io_find: not an I/O region");
    305  1.18       cgd 
    306  1.18       cgd 	if (iobasep != NULL)
    307  1.18       cgd 		*iobasep = PCI_MAPREG_IO_ADDR(addrdata);
    308  1.18       cgd 	if (iosizep != NULL)
    309  1.20   mycroft 		*iosizep = PCI_MAPREG_IO_SIZE(sizedata);
    310  1.18       cgd 
    311  1.18       cgd 	return (0);
    312  1.18       cgd }
    313  1.18       cgd 
    314  1.18       cgd int
    315  1.18       cgd pci_mem_find(pc, pcitag, reg, membasep, memsizep, cacheablep)
    316  1.18       cgd 	pci_chipset_tag_t pc;
    317  1.18       cgd 	pcitag_t pcitag;
    318  1.18       cgd 	int reg;
    319  1.24   thorpej 	bus_addr_t *membasep;
    320  1.24   thorpej 	bus_size_t *memsizep;
    321  1.18       cgd 	int *cacheablep;
    322  1.18       cgd {
    323  1.18       cgd 	pcireg_t addrdata, sizedata;
    324  1.18       cgd 	int s;
    325  1.18       cgd 
    326  1.18       cgd 	if (reg < PCI_MAPREG_START || reg >= PCI_MAPREG_END || (reg & 3))
    327  1.18       cgd 		panic("pci_find_mem: bad request");
    328  1.18       cgd 
    329  1.18       cgd 	/*
    330  1.18       cgd 	 * Section 6.2.5.1, `Address Maps', tells us that:
    331  1.18       cgd 	 *
    332  1.18       cgd 	 * 1) The builtin software should have already mapped the device in a
    333  1.18       cgd 	 * reasonable way.
    334  1.18       cgd 	 *
    335  1.18       cgd 	 * 2) A device which wants 2^n bytes of memory will hardwire the bottom
    336  1.18       cgd 	 * n bits of the address to 0.  As recommended, we write all 1s and see
    337  1.18       cgd 	 * what we get back.
    338  1.18       cgd 	 */
    339  1.18       cgd 	addrdata = pci_conf_read(pc, pcitag, reg);
    340  1.18       cgd 
    341  1.18       cgd 	s = splhigh();
    342  1.18       cgd 	pci_conf_write(pc, pcitag, reg, 0xffffffff);
    343  1.18       cgd 	sizedata = pci_conf_read(pc, pcitag, reg);
    344  1.18       cgd 	pci_conf_write(pc, pcitag, reg, addrdata);
    345  1.18       cgd 	splx(s);
    346  1.18       cgd 
    347  1.18       cgd 	if (PCI_MAPREG_TYPE(addrdata) == PCI_MAPREG_TYPE_IO)
    348  1.18       cgd 		panic("pci_find_mem: I/O region");
    349  1.18       cgd 
    350  1.18       cgd 	switch (PCI_MAPREG_MEM_TYPE(addrdata)) {
    351  1.18       cgd 	case PCI_MAPREG_MEM_TYPE_32BIT:
    352  1.18       cgd 	case PCI_MAPREG_MEM_TYPE_32BIT_1M:
    353  1.18       cgd 		break;
    354  1.18       cgd 	case PCI_MAPREG_MEM_TYPE_64BIT:
    355  1.23  christos /* XXX */	printf("pci_find_mem: 64-bit region\n");
    356  1.18       cgd /* XXX */	return (1);
    357  1.18       cgd 	default:
    358  1.23  christos 		printf("pci_find_mem: reserved region type\n");
    359  1.18       cgd 		return (1);
    360  1.18       cgd 	}
    361  1.18       cgd 
    362  1.18       cgd 	if (membasep != NULL)
    363  1.18       cgd 		*membasep = PCI_MAPREG_MEM_ADDR(addrdata);	/* PCI addr */
    364  1.18       cgd 	if (memsizep != NULL)
    365  1.20   mycroft 		*memsizep = PCI_MAPREG_MEM_SIZE(sizedata);
    366  1.18       cgd 	if (cacheablep != NULL)
    367  1.18       cgd 		*cacheablep = PCI_MAPREG_MEM_CACHEABLE(addrdata);
    368  1.18       cgd 
    369  1.18       cgd 	return 0;
    370  1.25       cgd }
    371  1.25       cgd 
    372  1.25       cgd void
    373  1.25       cgd set_pci_isa_bridge_callback(fn, arg)
    374  1.25       cgd 	void (*fn) __P((void *));
    375  1.25       cgd 	void *arg;
    376  1.25       cgd {
    377  1.25       cgd 
    378  1.25       cgd 	if (pci_isa_bridge_callback != NULL)
    379  1.25       cgd 		panic("set_pci_isa_bridge_callback");
    380  1.25       cgd 	pci_isa_bridge_callback = fn;
    381  1.25       cgd 	pci_isa_bridge_callback_arg = arg;
    382   1.1   mycroft }
    383