Home | History | Annotate | Line # | Download | only in pci
pci_machdep.c revision 1.29
      1  1.29  garbled /*	$NetBSD: pci_machdep.c,v 1.29 2007/03/20 17:16:33 garbled Exp $	*/
      2   1.1   nonaka 
      3   1.1   nonaka /*
      4   1.1   nonaka  * Copyright (c) 1996 Christopher G. Demetriou.  All rights reserved.
      5   1.1   nonaka  * Copyright (c) 1994 Charles M. Hannum.  All rights reserved.
      6   1.1   nonaka  *
      7   1.1   nonaka  * Redistribution and use in source and binary forms, with or without
      8   1.1   nonaka  * modification, are permitted provided that the following conditions
      9   1.1   nonaka  * are met:
     10   1.1   nonaka  * 1. Redistributions of source code must retain the above copyright
     11   1.1   nonaka  *    notice, this list of conditions and the following disclaimer.
     12   1.1   nonaka  * 2. Redistributions in binary form must reproduce the above copyright
     13   1.1   nonaka  *    notice, this list of conditions and the following disclaimer in the
     14   1.1   nonaka  *    documentation and/or other materials provided with the distribution.
     15   1.1   nonaka  * 3. All advertising materials mentioning features or use of this software
     16   1.1   nonaka  *    must display the following acknowledgement:
     17   1.1   nonaka  *	This product includes software developed by Charles M. Hannum.
     18   1.1   nonaka  * 4. The name of the author may not be used to endorse or promote products
     19   1.1   nonaka  *    derived from this software without specific prior written permission.
     20   1.1   nonaka  *
     21   1.1   nonaka  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
     22   1.1   nonaka  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
     23   1.1   nonaka  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
     24   1.1   nonaka  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
     25   1.1   nonaka  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
     26   1.1   nonaka  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
     27   1.1   nonaka  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
     28   1.1   nonaka  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
     29   1.1   nonaka  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
     30   1.1   nonaka  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     31   1.1   nonaka  */
     32   1.1   nonaka 
     33   1.1   nonaka /*
     34   1.1   nonaka  * Machine-specific functions for PCI autoconfiguration.
     35   1.1   nonaka  *
     36   1.1   nonaka  * On PCs, there are two methods of generating PCI configuration cycles.
     37   1.1   nonaka  * We try to detect the appropriate mechanism for this machine and set
     38   1.1   nonaka  * up a few function pointers to access the correct method directly.
     39   1.1   nonaka  */
     40   1.1   nonaka 
     41  1.18    lukem #include <sys/cdefs.h>
     42  1.29  garbled __KERNEL_RCSID(0, "$NetBSD: pci_machdep.c,v 1.29 2007/03/20 17:16:33 garbled Exp $");
     43  1.18    lukem 
     44   1.1   nonaka #include <sys/types.h>
     45   1.1   nonaka #include <sys/param.h>
     46   1.1   nonaka #include <sys/time.h>
     47   1.1   nonaka #include <sys/systm.h>
     48   1.1   nonaka #include <sys/errno.h>
     49  1.15   kleink #include <sys/extent.h>
     50   1.1   nonaka #include <sys/device.h>
     51  1.27  garbled #include <sys/malloc.h>
     52   1.1   nonaka 
     53   1.6      mrg #include <uvm/uvm_extern.h>
     54   1.1   nonaka 
     55  1.10   nonaka #define _POWERPC_BUS_DMA_PRIVATE
     56   1.1   nonaka #include <machine/bus.h>
     57   1.1   nonaka #include <machine/intr.h>
     58  1.13   nonaka #include <machine/platform.h>
     59  1.24  garbled #include <machine/pnp.h>
     60   1.1   nonaka 
     61   1.1   nonaka #include <dev/isa/isavar.h>
     62  1.15   kleink 
     63   1.1   nonaka #include <dev/pci/pcivar.h>
     64   1.1   nonaka #include <dev/pci/pcireg.h>
     65   1.1   nonaka #include <dev/pci/pcidevs.h>
     66  1.15   kleink #include <dev/pci/pciconf.h>
     67  1.13   nonaka 
     68   1.1   nonaka /*
     69   1.1   nonaka  * PCI doesn't have any special needs; just use the generic versions
     70   1.1   nonaka  * of these functions.
     71   1.1   nonaka  */
     72  1.10   nonaka struct powerpc_bus_dma_tag pci_bus_dma_tag = {
     73   1.1   nonaka 	0,			/* _bounce_thresh */
     74   1.1   nonaka 	_bus_dmamap_create,
     75   1.1   nonaka 	_bus_dmamap_destroy,
     76   1.1   nonaka 	_bus_dmamap_load,
     77   1.1   nonaka 	_bus_dmamap_load_mbuf,
     78   1.1   nonaka 	_bus_dmamap_load_uio,
     79   1.1   nonaka 	_bus_dmamap_load_raw,
     80   1.1   nonaka 	_bus_dmamap_unload,
     81   1.1   nonaka 	NULL,			/* _dmamap_sync */
     82   1.1   nonaka 	_bus_dmamem_alloc,
     83   1.1   nonaka 	_bus_dmamem_free,
     84   1.1   nonaka 	_bus_dmamem_map,
     85   1.1   nonaka 	_bus_dmamem_unmap,
     86   1.1   nonaka 	_bus_dmamem_mmap,
     87   1.1   nonaka };
     88   1.1   nonaka 
     89  1.27  garbled /* 0 == direct 1 == indirect */
     90  1.27  garbled int prep_pci_config_mode = 1;
     91  1.27  garbled extern struct prep_pci_chipset *prep_pct;
     92  1.27  garbled 
     93  1.24  garbled void
     94  1.24  garbled prep_pci_get_chipset_tag(pci_chipset_tag_t pc)
     95  1.24  garbled {
     96  1.24  garbled 	int i;
     97  1.24  garbled 
     98  1.24  garbled 	i = pci_chipset_tag_type();
     99  1.24  garbled 
    100  1.27  garbled 	if (i == PCIBridgeIndirect || i == PCIBridgeRS6K) {
    101  1.27  garbled 		prep_pci_config_mode = 1;
    102  1.24  garbled 		prep_pci_get_chipset_tag_indirect(pc);
    103  1.27  garbled 	} else if (i == PCIBridgeDirect) {
    104  1.24  garbled 		prep_pci_get_chipset_tag_direct(pc);
    105  1.27  garbled 		prep_pci_config_mode = 0;
    106  1.27  garbled 	} else
    107  1.24  garbled 		panic("Unknown PCI chipset tag configuration method");
    108  1.24  garbled }
    109  1.24  garbled 
    110   1.1   nonaka int
    111  1.27  garbled prep_pci_bus_maxdevs(pci_chipset_tag_t pc, int busno)
    112   1.1   nonaka {
    113  1.27  garbled 	struct prep_pci_chipset_businfo *pbi;
    114  1.27  garbled 	prop_object_t busmax;
    115  1.27  garbled 
    116  1.27  garbled 	pbi = SIMPLEQ_FIRST(&prep_pct->pc_pbi);
    117  1.27  garbled 	while (busno--)
    118  1.27  garbled 		pbi = SIMPLEQ_NEXT(pbi, next);
    119  1.27  garbled 	if (pbi == NULL)
    120  1.27  garbled 		return 32;
    121  1.27  garbled 
    122  1.27  garbled 	busmax = prop_dictionary_get(pbi->pbi_properties,
    123  1.27  garbled 	    "prep-pcibus-maxdevices");
    124  1.27  garbled 	if (busmax == NULL)
    125  1.27  garbled 		return 32;
    126  1.27  garbled 	else
    127  1.27  garbled 		return prop_number_integer_value(busmax);
    128   1.1   nonaka 
    129  1.27  garbled 	return 32;
    130   1.1   nonaka }
    131   1.1   nonaka 
    132   1.1   nonaka int
    133  1.15   kleink prep_pci_intr_map(struct pci_attach_args *pa, pci_intr_handle_t *ihp)
    134   1.1   nonaka {
    135  1.27  garbled 	struct prep_pci_chipset_businfo *pbi;
    136  1.27  garbled 	prop_dictionary_t dict, devsub;
    137  1.27  garbled 	prop_object_t pinsub;
    138  1.27  garbled 	prop_number_t pbus;
    139  1.27  garbled 	int busno, bus, pin, line, swiz, dev;
    140  1.27  garbled 	char key[20];
    141  1.27  garbled 
    142  1.27  garbled 	pin = pa->pa_intrpin;
    143  1.27  garbled 	line = pa->pa_intrline;
    144  1.27  garbled 	bus = busno = pa->pa_bus;
    145  1.27  garbled 	swiz = pa->pa_intrswiz;
    146  1.27  garbled 	dev = pa->pa_device;
    147  1.27  garbled 
    148  1.27  garbled 	pbi = SIMPLEQ_FIRST(&prep_pct->pc_pbi);
    149  1.27  garbled 	while (busno--)
    150  1.27  garbled 		pbi = SIMPLEQ_NEXT(pbi, next);
    151  1.27  garbled 	KASSERT(pbi != NULL);
    152  1.27  garbled 
    153  1.27  garbled 	dict = prop_dictionary_get(pbi->pbi_properties, "prep-pci-intrmap");
    154  1.27  garbled 	if (dict == NULL) {
    155  1.27  garbled 		/* We have a non-PReP bus.  now it gets hard */
    156  1.27  garbled 		pbus = prop_dictionary_get(pbi->pbi_properties,
    157  1.27  garbled 		    "prep-pcibus-parent");
    158  1.27  garbled 		if (pbus == NULL)
    159  1.27  garbled 			goto bad;
    160  1.27  garbled 		busno = prop_number_integer_value(pbus);
    161  1.27  garbled 		pbus = prop_dictionary_get(pbi->pbi_properties,
    162  1.27  garbled 		    "prep-pcibus-rawdevnum");
    163  1.27  garbled 		dev = prop_number_integer_value(pbus);
    164  1.27  garbled 
    165  1.27  garbled 		/* now that we know the parent bus, we need to find it's pbi */
    166  1.27  garbled 		pbi = SIMPLEQ_FIRST(&prep_pct->pc_pbi);
    167  1.27  garbled 		while (busno--)
    168  1.27  garbled 			pbi = SIMPLEQ_NEXT(pbi, next);
    169  1.27  garbled 		KASSERT(pbi != NULL);
    170  1.27  garbled 
    171  1.27  garbled 		/* set the pin to the rawpin */
    172  1.27  garbled 		pin = pa->pa_rawintrpin;
    173  1.27  garbled 		/* now we have the pbi, ask for dict again */
    174  1.27  garbled 		dict = prop_dictionary_get(pbi->pbi_properties,
    175  1.27  garbled 		    "prep-pci-intrmap");
    176  1.27  garbled 		if (dict == NULL)
    177  1.27  garbled 			goto bad;
    178  1.27  garbled 	}
    179   1.1   nonaka 
    180  1.27  garbled 	/* No IRQ used. */
    181  1.27  garbled 	if (pin == 0)
    182   1.1   nonaka 		goto bad;
    183   1.1   nonaka 	if (pin > 4) {
    184   1.1   nonaka 		printf("pci_intr_map: bad interrupt pin %d\n", pin);
    185   1.1   nonaka 		goto bad;
    186   1.1   nonaka 	}
    187   1.1   nonaka 
    188  1.27  garbled 	sprintf(key, "devfunc-%d", dev);
    189  1.27  garbled 	devsub = prop_dictionary_get(dict, key);
    190  1.27  garbled 	if (devsub == NULL)
    191  1.27  garbled 		goto bad;
    192  1.27  garbled 	sprintf(key, "pin-%c", 'A' + (pin-1));
    193  1.27  garbled 	pinsub = prop_dictionary_get(devsub, key);
    194  1.27  garbled 	if (pinsub == NULL)
    195  1.27  garbled 		goto bad;
    196  1.27  garbled 	line = prop_number_integer_value(pinsub);
    197  1.27  garbled 
    198   1.1   nonaka 	/*
    199   1.1   nonaka 	* Section 6.2.4, `Miscellaneous Functions', says that 255 means
    200   1.1   nonaka 	* `unknown' or `no connection' on a PC.  We assume that a device with
    201   1.1   nonaka 	* `no connection' either doesn't have an interrupt (in which case the
    202   1.1   nonaka 	* pin number should be 0, and would have been noticed above), or
    203   1.1   nonaka 	* wasn't configured by the BIOS (in which case we punt, since there's
    204   1.1   nonaka 	* no real way we can know how the interrupt lines are mapped in the
    205   1.1   nonaka 	* hardware).
    206   1.1   nonaka 	*
    207   1.1   nonaka 	* XXX
    208   1.1   nonaka 	* Since IRQ 0 is only used by the clock, and we can't actually be sure
    209   1.1   nonaka 	* that the BIOS did its job, we also recognize that as meaning that
    210   1.1   nonaka 	* the BIOS has not configured the device.
    211   1.1   nonaka 	*/
    212   1.1   nonaka 	if (line == 0 || line == 255) {
    213   1.1   nonaka 		printf("pci_intr_map: no mapping for pin %c\n", '@' + pin);
    214   1.1   nonaka 		goto bad;
    215   1.1   nonaka 	} else {
    216   1.1   nonaka 		if (line >= ICU_LEN) {
    217   1.1   nonaka 			printf("pci_intr_map: bad interrupt line %d\n", line);
    218   1.1   nonaka 			goto bad;
    219   1.1   nonaka 		}
    220   1.1   nonaka 		if (line == IRQ_SLAVE) {
    221   1.1   nonaka 			printf("pci_intr_map: changed line 2 to line 9\n");
    222   1.1   nonaka 			line = 9;
    223   1.1   nonaka 		}
    224   1.1   nonaka 	}
    225   1.1   nonaka 
    226   1.1   nonaka 	*ihp = line;
    227   1.1   nonaka 	return 0;
    228   1.1   nonaka 
    229   1.1   nonaka bad:
    230   1.1   nonaka 	*ihp = -1;
    231   1.1   nonaka 	return 1;
    232   1.1   nonaka }
    233   1.1   nonaka 
    234   1.1   nonaka const char *
    235  1.15   kleink prep_pci_intr_string(void *v, pci_intr_handle_t ih)
    236   1.1   nonaka {
    237   1.1   nonaka 	static char irqstr[8];		/* 4 + 2 + NULL + sanity */
    238   1.1   nonaka 
    239   1.1   nonaka 	if (ih == 0 || ih >= ICU_LEN || ih == IRQ_SLAVE)
    240  1.17   provos 		panic("pci_intr_string: bogus handle 0x%x", ih);
    241   1.1   nonaka 
    242   1.1   nonaka 	sprintf(irqstr, "irq %d", ih);
    243   1.1   nonaka 	return (irqstr);
    244   1.1   nonaka 
    245   1.3      cgd }
    246   1.3      cgd 
    247   1.3      cgd const struct evcnt *
    248  1.15   kleink prep_pci_intr_evcnt(void *v, pci_intr_handle_t ih)
    249   1.3      cgd {
    250   1.3      cgd 
    251   1.3      cgd 	/* XXX for now, no evcnt parent reported */
    252   1.3      cgd 	return NULL;
    253   1.1   nonaka }
    254   1.1   nonaka 
    255   1.1   nonaka void *
    256  1.15   kleink prep_pci_intr_establish(void *v, pci_intr_handle_t ih, int level,
    257  1.13   nonaka     int (*func)(void *), void *arg)
    258   1.1   nonaka {
    259   1.1   nonaka 
    260   1.1   nonaka 	if (ih == 0 || ih >= ICU_LEN || ih == IRQ_SLAVE)
    261  1.17   provos 		panic("pci_intr_establish: bogus handle 0x%x", ih);
    262   1.1   nonaka 
    263   1.1   nonaka 	return isa_intr_establish(NULL, ih, IST_LEVEL, level, func, arg);
    264   1.1   nonaka }
    265   1.1   nonaka 
    266   1.1   nonaka void
    267  1.15   kleink prep_pci_intr_disestablish(void *v, void *cookie)
    268   1.1   nonaka {
    269   1.1   nonaka 
    270   1.9    lukem 	isa_intr_disestablish(NULL, cookie);
    271  1.13   nonaka }
    272  1.13   nonaka 
    273  1.13   nonaka void
    274  1.15   kleink prep_pci_conf_interrupt(void *v, int bus, int dev, int pin,
    275  1.13   nonaka     int swiz, int *iline)
    276  1.13   nonaka {
    277  1.27  garbled 	/* do nothing */
    278  1.27  garbled }
    279  1.13   nonaka 
    280  1.27  garbled extern pcitag_t prep_pci_direct_make_tag(void *, int, int, int);
    281  1.27  garbled extern pcitag_t prep_pci_indirect_make_tag(void *, int, int, int);
    282  1.27  garbled extern pcireg_t prep_pci_direct_conf_read(void *, pcitag_t, int);
    283  1.27  garbled extern pcireg_t prep_pci_indirect_conf_read(void *, pcitag_t, int);
    284  1.15   kleink 
    285  1.15   kleink int
    286  1.27  garbled prep_pci_conf_hook(pci_chipset_tag_t pct, int bus, int dev, int func,
    287  1.27  garbled 	pcireg_t id)
    288  1.15   kleink {
    289  1.27  garbled 	struct prep_pci_chipset_businfo *pbi;
    290  1.27  garbled 	prop_number_t bmax, pbus;
    291  1.27  garbled 	pcitag_t tag;
    292  1.27  garbled 	pcireg_t class;
    293  1.15   kleink 
    294  1.15   kleink 	/*
    295  1.15   kleink 	 * The P9100 board found in some IBM machines cannot be
    296  1.15   kleink 	 * over-configured.
    297  1.15   kleink 	 */
    298  1.15   kleink 	if (PCI_VENDOR(id) == PCI_VENDOR_WEITEK &&
    299  1.15   kleink 	    PCI_PRODUCT(id) == PCI_PRODUCT_WEITEK_P9100)
    300  1.15   kleink 		return 0;
    301  1.15   kleink 
    302  1.25      snj 	/* We have already mapped the MPIC2 if we have one, so leave it
    303  1.23  garbled 	   alone */
    304  1.23  garbled 	if (PCI_VENDOR(id) == PCI_VENDOR_IBM &&
    305  1.23  garbled 	    PCI_PRODUCT(id) == PCI_PRODUCT_IBM_MPIC2)
    306  1.23  garbled 		return 0;
    307  1.23  garbled 
    308  1.28  garbled 	if (PCI_VENDOR(id) == PCI_VENDOR_IBM &&
    309  1.28  garbled 	    PCI_PRODUCT(id) == PCI_PRODUCT_IBM_MPIC)
    310  1.28  garbled 		return 0;
    311  1.28  garbled 
    312  1.27  garbled 	if (PCI_VENDOR(id) == PCI_VENDOR_INTEL &&
    313  1.27  garbled 	    PCI_PRODUCT(id) == PCI_PRODUCT_INTEL_PCEB)
    314  1.27  garbled 		return 0;
    315  1.27  garbled 
    316  1.29  garbled 	if (PCI_VENDOR(id) == PCI_VENDOR_MOT &&
    317  1.29  garbled 	    PCI_PRODUCT(id) == PCI_PRODUCT_MOT_RAVEN)
    318  1.29  garbled 		return (PCI_CONF_ALL & ~PCI_CONF_MAP_MEM);
    319  1.29  garbled 
    320  1.27  garbled 	/* NOTE, all device specific stuff must be above this line */
    321  1.27  garbled 	/* don't do this on the primary host bridge */
    322  1.27  garbled 	if (bus == 0 && dev == 0 && func == 0)
    323  1.27  garbled 		return PCI_CONF_DEFAULT;
    324  1.27  garbled 
    325  1.27  garbled 	if (prep_pci_config_mode) {
    326  1.27  garbled 		tag = prep_pci_indirect_make_tag(pct, bus, dev, func);
    327  1.27  garbled 		class = prep_pci_indirect_conf_read(pct, tag,
    328  1.27  garbled 		    PCI_CLASS_REG);
    329  1.27  garbled 	} else {
    330  1.27  garbled 		tag = prep_pci_direct_make_tag(pct, bus, dev, func);
    331  1.27  garbled 		class = prep_pci_direct_conf_read(pct, tag,
    332  1.27  garbled 		    PCI_CLASS_REG);
    333  1.27  garbled 	}
    334  1.27  garbled 
    335  1.27  garbled 	/*
    336  1.27  garbled 	 * PCI bridges have special needs.  We need to discover where they
    337  1.27  garbled 	 * came from, and wire them appropriately.
    338  1.27  garbled 	 */
    339  1.27  garbled 	if (PCI_CLASS(class) == PCI_CLASS_BRIDGE &&
    340  1.27  garbled 	    PCI_SUBCLASS(class) == PCI_SUBCLASS_BRIDGE_PCI) {
    341  1.27  garbled 		pbi = malloc(sizeof(struct prep_pci_chipset_businfo), M_DEVBUF,
    342  1.27  garbled 		    M_NOWAIT);
    343  1.27  garbled 		KASSERT(pbi != NULL);
    344  1.27  garbled 		pbi->pbi_properties = prop_dictionary_create();
    345  1.27  garbled 		KASSERT(pbi->pbi_properties != NULL);
    346  1.27  garbled 		setup_pciintr_map(pbi, bus, dev, func);
    347  1.27  garbled 
    348  1.27  garbled 		/* record the parent bus, and the parent device number */
    349  1.27  garbled 		pbus = prop_number_create_integer(bus);
    350  1.27  garbled 		prop_dictionary_set(pbi->pbi_properties, "prep-pcibus-parent",
    351  1.27  garbled 		    pbus);
    352  1.27  garbled 		prop_object_release(pbus);
    353  1.27  garbled 		pbus = prop_number_create_integer(dev);
    354  1.27  garbled 		prop_dictionary_set(pbi->pbi_properties,
    355  1.27  garbled 		    "prep-pcibus-rawdevnum", pbus);
    356  1.27  garbled 		prop_object_release(pbus);
    357  1.27  garbled 
    358  1.27  garbled 		/* now look for bus quirks */
    359  1.27  garbled 
    360  1.27  garbled 		if (PCI_VENDOR(id) == PCI_VENDOR_DEC &&
    361  1.27  garbled 		    PCI_PRODUCT(id) == PCI_PRODUCT_DEC_21154) {
    362  1.27  garbled 			bmax = prop_number_create_integer(8);
    363  1.27  garbled 			KASSERT(bmax != NULL);
    364  1.27  garbled 			prop_dictionary_set(pbi->pbi_properties,
    365  1.27  garbled 			    "prep-pcibus-maxdevices", bmax);
    366  1.27  garbled 			prop_object_release(bmax);
    367  1.27  garbled 		}
    368  1.27  garbled 
    369  1.27  garbled 		SIMPLEQ_INSERT_TAIL(&prep_pct->pc_pbi, pbi, next);
    370  1.27  garbled 	}
    371  1.27  garbled 
    372  1.22  gdamore 	return (PCI_CONF_DEFAULT);
    373   1.1   nonaka }
    374