Home | History | Annotate | Line # | Download | only in pci
      1  1.44   thorpej /*	$NetBSD: pci_machdep.c,v 1.44 2020/11/21 15:59:53 thorpej 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.44   thorpej __KERNEL_RCSID(0, "$NetBSD: pci_machdep.c,v 1.44 2020/11/21 15:59:53 thorpej 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.44   thorpej #include <sys/kmem.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.37    dyoung #include <sys/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.27   garbled /* 0 == direct 1 == indirect */
     69  1.27   garbled int prep_pci_config_mode = 1;
     70  1.32   garbled extern struct genppc_pci_chipset *genppc_pct;
     71  1.32   garbled extern u_int32_t prep_pci_baseaddr;
     72  1.32   garbled extern u_int32_t prep_pci_basedata;
     73  1.32   garbled 
     74  1.32   garbled static void
     75  1.32   garbled prep_pci_get_chipset_tag_indirect(pci_chipset_tag_t pc)
     76  1.32   garbled {
     77  1.32   garbled 
     78  1.32   garbled 	pc->pc_conf_v = (void *)pc;
     79  1.32   garbled 
     80  1.32   garbled 	pc->pc_attach_hook = genppc_pci_indirect_attach_hook;
     81  1.32   garbled 	pc->pc_bus_maxdevs = prep_pci_bus_maxdevs;
     82  1.32   garbled 	pc->pc_make_tag = genppc_pci_indirect_make_tag;
     83  1.32   garbled 	pc->pc_conf_read = genppc_pci_indirect_conf_read;
     84  1.32   garbled 	pc->pc_conf_write = genppc_pci_indirect_conf_write;
     85  1.32   garbled 
     86  1.32   garbled 	pc->pc_intr_v = (void *)pc;
     87  1.32   garbled 
     88  1.32   garbled 	pc->pc_intr_map = prep_pci_intr_map;
     89  1.32   garbled 	pc->pc_intr_string = genppc_pci_intr_string;
     90  1.32   garbled 	pc->pc_intr_evcnt = genppc_pci_intr_evcnt;
     91  1.32   garbled 	pc->pc_intr_establish = genppc_pci_intr_establish;
     92  1.32   garbled 	pc->pc_intr_disestablish = genppc_pci_intr_disestablish;
     93  1.35      matt 	pc->pc_intr_setattr = genppc_pci_intr_setattr;
     94  1.42    nonaka 	pc->pc_intr_type = genppc_pci_intr_type;
     95  1.42    nonaka 	pc->pc_intr_alloc = genppc_pci_intr_alloc;
     96  1.42    nonaka 	pc->pc_intr_release = genppc_pci_intr_release;
     97  1.42    nonaka 	pc->pc_intx_alloc = genppc_pci_intx_alloc;
     98  1.32   garbled 
     99  1.38      matt 	pc->pc_msi_v = (void *)pc;
    100  1.38      matt 	genppc_pci_chipset_msi_init(pc);
    101  1.38      matt 
    102  1.42    nonaka 	pc->pc_msix_v = (void *)pc;
    103  1.42    nonaka 	genppc_pci_chipset_msix_init(pc);
    104  1.42    nonaka 
    105  1.32   garbled 	pc->pc_conf_interrupt = genppc_pci_conf_interrupt;
    106  1.32   garbled 	pc->pc_decompose_tag = genppc_pci_indirect_decompose_tag;
    107  1.32   garbled 	pc->pc_conf_hook = prep_pci_conf_hook;
    108  1.32   garbled 
    109  1.36      matt 	pc->pc_addr = mapiodev(prep_pci_baseaddr, 4, false);
    110  1.36      matt 	pc->pc_data = mapiodev(prep_pci_basedata, 4, false);
    111  1.32   garbled 	pc->pc_bus = 0;
    112  1.32   garbled 	pc->pc_node = 0;
    113  1.32   garbled 	pc->pc_memt = 0;
    114  1.32   garbled 	pc->pc_iot = 0;
    115  1.32   garbled }
    116  1.27   garbled 
    117  1.24   garbled void
    118  1.24   garbled prep_pci_get_chipset_tag(pci_chipset_tag_t pc)
    119  1.24   garbled {
    120  1.24   garbled 	int i;
    121  1.24   garbled 
    122  1.24   garbled 	i = pci_chipset_tag_type();
    123  1.24   garbled 
    124  1.27   garbled 	if (i == PCIBridgeIndirect || i == PCIBridgeRS6K) {
    125  1.27   garbled 		prep_pci_config_mode = 1;
    126  1.24   garbled 		prep_pci_get_chipset_tag_indirect(pc);
    127  1.27   garbled 	} else if (i == PCIBridgeDirect) {
    128  1.24   garbled 		prep_pci_get_chipset_tag_direct(pc);
    129  1.27   garbled 		prep_pci_config_mode = 0;
    130  1.27   garbled 	} else
    131  1.24   garbled 		panic("Unknown PCI chipset tag configuration method");
    132  1.24   garbled }
    133  1.24   garbled 
    134   1.1    nonaka int
    135  1.33      matt prep_pci_bus_maxdevs(void *v, int busno)
    136   1.1    nonaka {
    137  1.32   garbled 	struct genppc_pci_chipset_businfo *pbi;
    138  1.27   garbled 	prop_object_t busmax;
    139  1.27   garbled 
    140  1.32   garbled 	pbi = SIMPLEQ_FIRST(&genppc_pct->pc_pbi);
    141  1.27   garbled 	while (busno--)
    142  1.27   garbled 		pbi = SIMPLEQ_NEXT(pbi, next);
    143  1.27   garbled 	if (pbi == NULL)
    144  1.27   garbled 		return 32;
    145  1.27   garbled 
    146  1.27   garbled 	busmax = prop_dictionary_get(pbi->pbi_properties,
    147  1.27   garbled 	    "prep-pcibus-maxdevices");
    148  1.27   garbled 	if (busmax == NULL)
    149  1.27   garbled 		return 32;
    150  1.27   garbled 	else
    151  1.27   garbled 		return prop_number_integer_value(busmax);
    152   1.1    nonaka 
    153  1.27   garbled 	return 32;
    154   1.1    nonaka }
    155   1.1    nonaka 
    156   1.1    nonaka int
    157  1.34    dyoung prep_pci_intr_map(const struct pci_attach_args *pa, pci_intr_handle_t *ihp)
    158   1.1    nonaka {
    159  1.32   garbled 	struct genppc_pci_chipset_businfo *pbi;
    160  1.27   garbled 	prop_dictionary_t dict, devsub;
    161  1.27   garbled 	prop_object_t pinsub;
    162  1.27   garbled 	prop_number_t pbus;
    163  1.40       mrg 	int busno, pin, line, dev, origdev, i;
    164  1.27   garbled 	char key[20];
    165  1.27   garbled 
    166  1.27   garbled 	pin = pa->pa_intrpin;
    167  1.27   garbled 	line = pa->pa_intrline;
    168  1.40       mrg 	busno = pa->pa_bus;
    169  1.31   garbled 	origdev = dev = pa->pa_device;
    170  1.30   garbled 	i = 0;
    171  1.27   garbled 
    172  1.32   garbled 	pbi = SIMPLEQ_FIRST(&genppc_pct->pc_pbi);
    173  1.27   garbled 	while (busno--)
    174  1.27   garbled 		pbi = SIMPLEQ_NEXT(pbi, next);
    175  1.27   garbled 	KASSERT(pbi != NULL);
    176  1.27   garbled 
    177  1.27   garbled 	dict = prop_dictionary_get(pbi->pbi_properties, "prep-pci-intrmap");
    178  1.31   garbled 
    179  1.30   garbled 	if (dict != NULL)
    180  1.30   garbled 		i = prop_dictionary_count(dict);
    181  1.30   garbled 
    182  1.30   garbled 	if (dict == NULL || i == 0) {
    183  1.27   garbled 		/* We have a non-PReP bus.  now it gets hard */
    184  1.27   garbled 		pbus = prop_dictionary_get(pbi->pbi_properties,
    185  1.27   garbled 		    "prep-pcibus-parent");
    186  1.27   garbled 		if (pbus == NULL)
    187  1.27   garbled 			goto bad;
    188  1.27   garbled 		busno = prop_number_integer_value(pbus);
    189  1.27   garbled 		pbus = prop_dictionary_get(pbi->pbi_properties,
    190  1.27   garbled 		    "prep-pcibus-rawdevnum");
    191  1.27   garbled 		dev = prop_number_integer_value(pbus);
    192  1.27   garbled 
    193  1.41       snj 		/* now that we know the parent bus, we need to find its pbi */
    194  1.32   garbled 		pbi = SIMPLEQ_FIRST(&genppc_pct->pc_pbi);
    195  1.27   garbled 		while (busno--)
    196  1.27   garbled 			pbi = SIMPLEQ_NEXT(pbi, next);
    197  1.27   garbled 		KASSERT(pbi != NULL);
    198  1.27   garbled 
    199  1.31   garbled 		/* swizzle the pin */
    200  1.31   garbled 		pin = ((pin + origdev - 1) & 3) + 1;
    201  1.31   garbled 
    202  1.27   garbled 		/* now we have the pbi, ask for dict again */
    203  1.27   garbled 		dict = prop_dictionary_get(pbi->pbi_properties,
    204  1.27   garbled 		    "prep-pci-intrmap");
    205  1.27   garbled 		if (dict == NULL)
    206  1.27   garbled 			goto bad;
    207  1.27   garbled 	}
    208   1.1    nonaka 
    209  1.27   garbled 	/* No IRQ used. */
    210  1.27   garbled 	if (pin == 0)
    211   1.1    nonaka 		goto bad;
    212   1.1    nonaka 	if (pin > 4) {
    213  1.32   garbled 		aprint_error("pci_intr_map: bad interrupt pin %d\n", pin);
    214   1.1    nonaka 		goto bad;
    215   1.1    nonaka 	}
    216   1.1    nonaka 
    217  1.39  christos 	snprintf(key, sizeof(key), "devfunc-%d", dev);
    218  1.27   garbled 	devsub = prop_dictionary_get(dict, key);
    219  1.27   garbled 	if (devsub == NULL)
    220  1.27   garbled 		goto bad;
    221  1.39  christos 	snprintf(key, sizeof(key), "pin-%c", 'A' + (pin-1));
    222  1.27   garbled 	pinsub = prop_dictionary_get(devsub, key);
    223  1.27   garbled 	if (pinsub == NULL)
    224  1.27   garbled 		goto bad;
    225  1.27   garbled 	line = prop_number_integer_value(pinsub);
    226  1.27   garbled 
    227   1.1    nonaka 	/*
    228   1.1    nonaka 	* Section 6.2.4, `Miscellaneous Functions', says that 255 means
    229   1.1    nonaka 	* `unknown' or `no connection' on a PC.  We assume that a device with
    230   1.1    nonaka 	* `no connection' either doesn't have an interrupt (in which case the
    231   1.1    nonaka 	* pin number should be 0, and would have been noticed above), or
    232   1.1    nonaka 	* wasn't configured by the BIOS (in which case we punt, since there's
    233   1.1    nonaka 	* no real way we can know how the interrupt lines are mapped in the
    234   1.1    nonaka 	* hardware).
    235   1.1    nonaka 	*
    236   1.1    nonaka 	* XXX
    237   1.1    nonaka 	* Since IRQ 0 is only used by the clock, and we can't actually be sure
    238   1.1    nonaka 	* that the BIOS did its job, we also recognize that as meaning that
    239   1.1    nonaka 	* the BIOS has not configured the device.
    240   1.1    nonaka 	*/
    241   1.1    nonaka 	if (line == 0 || line == 255) {
    242  1.32   garbled 		aprint_error("pci_intr_map: no mapping for pin %c\n",
    243  1.32   garbled 		    '@' + pin);
    244   1.1    nonaka 		goto bad;
    245   1.1    nonaka 	} else {
    246   1.1    nonaka 		if (line >= ICU_LEN) {
    247  1.32   garbled 			aprint_error("pci_intr_map: bad interrupt line %d\n",
    248  1.32   garbled 			    line);
    249   1.1    nonaka 			goto bad;
    250   1.1    nonaka 		}
    251   1.1    nonaka 		if (line == IRQ_SLAVE) {
    252  1.32   garbled 			aprint_verbose("pci_intr_map: changed line 2 to line 9\n");
    253   1.1    nonaka 			line = 9;
    254   1.1    nonaka 		}
    255   1.1    nonaka 	}
    256   1.1    nonaka 
    257   1.1    nonaka 	*ihp = line;
    258   1.1    nonaka 	return 0;
    259   1.1    nonaka 
    260   1.1    nonaka bad:
    261   1.1    nonaka 	*ihp = -1;
    262   1.1    nonaka 	return 1;
    263   1.1    nonaka }
    264   1.1    nonaka 
    265  1.27   garbled extern pcitag_t prep_pci_direct_make_tag(void *, int, int, int);
    266  1.32   garbled extern pcitag_t genppc_pci_indirect_make_tag(void *, int, int, int);
    267  1.27   garbled extern pcireg_t prep_pci_direct_conf_read(void *, pcitag_t, int);
    268  1.32   garbled extern pcireg_t genppc_pci_indirect_conf_read(void *, pcitag_t, int);
    269  1.15    kleink 
    270  1.15    kleink int
    271  1.35      matt prep_pci_conf_hook(void *v, int bus, int dev, int func, pcireg_t id)
    272  1.15    kleink {
    273  1.35      matt 	pci_chipset_tag_t pc = v;
    274  1.32   garbled 	struct genppc_pci_chipset_businfo *pbi;
    275  1.27   garbled 	prop_number_t bmax, pbus;
    276  1.27   garbled 	pcitag_t tag;
    277  1.27   garbled 	pcireg_t class;
    278  1.15    kleink 
    279  1.15    kleink 	/*
    280  1.15    kleink 	 * The P9100 board found in some IBM machines cannot be
    281  1.15    kleink 	 * over-configured.
    282  1.15    kleink 	 */
    283  1.15    kleink 	if (PCI_VENDOR(id) == PCI_VENDOR_WEITEK &&
    284  1.15    kleink 	    PCI_PRODUCT(id) == PCI_PRODUCT_WEITEK_P9100)
    285  1.15    kleink 		return 0;
    286  1.15    kleink 
    287  1.25       snj 	/* We have already mapped the MPIC2 if we have one, so leave it
    288  1.23   garbled 	   alone */
    289  1.23   garbled 	if (PCI_VENDOR(id) == PCI_VENDOR_IBM &&
    290  1.23   garbled 	    PCI_PRODUCT(id) == PCI_PRODUCT_IBM_MPIC2)
    291  1.23   garbled 		return 0;
    292  1.23   garbled 
    293  1.28   garbled 	if (PCI_VENDOR(id) == PCI_VENDOR_IBM &&
    294  1.28   garbled 	    PCI_PRODUCT(id) == PCI_PRODUCT_IBM_MPIC)
    295  1.28   garbled 		return 0;
    296  1.28   garbled 
    297  1.27   garbled 	if (PCI_VENDOR(id) == PCI_VENDOR_INTEL &&
    298  1.27   garbled 	    PCI_PRODUCT(id) == PCI_PRODUCT_INTEL_PCEB)
    299  1.27   garbled 		return 0;
    300  1.27   garbled 
    301  1.29   garbled 	if (PCI_VENDOR(id) == PCI_VENDOR_MOT &&
    302  1.29   garbled 	    PCI_PRODUCT(id) == PCI_PRODUCT_MOT_RAVEN)
    303  1.29   garbled 		return (PCI_CONF_ALL & ~PCI_CONF_MAP_MEM);
    304  1.29   garbled 
    305  1.27   garbled 	/* NOTE, all device specific stuff must be above this line */
    306  1.27   garbled 	/* don't do this on the primary host bridge */
    307  1.27   garbled 	if (bus == 0 && dev == 0 && func == 0)
    308  1.27   garbled 		return PCI_CONF_DEFAULT;
    309  1.27   garbled 
    310  1.27   garbled 	if (prep_pci_config_mode) {
    311  1.35      matt 		tag = genppc_pci_indirect_make_tag(pc, bus, dev, func);
    312  1.35      matt 		class = genppc_pci_indirect_conf_read(pc, tag,
    313  1.27   garbled 		    PCI_CLASS_REG);
    314  1.27   garbled 	} else {
    315  1.35      matt 		tag = prep_pci_direct_make_tag(pc, bus, dev, func);
    316  1.35      matt 		class = prep_pci_direct_conf_read(pc, tag,
    317  1.27   garbled 		    PCI_CLASS_REG);
    318  1.27   garbled 	}
    319  1.27   garbled 
    320  1.27   garbled 	/*
    321  1.27   garbled 	 * PCI bridges have special needs.  We need to discover where they
    322  1.27   garbled 	 * came from, and wire them appropriately.
    323  1.27   garbled 	 */
    324  1.27   garbled 	if (PCI_CLASS(class) == PCI_CLASS_BRIDGE &&
    325  1.27   garbled 	    PCI_SUBCLASS(class) == PCI_SUBCLASS_BRIDGE_PCI) {
    326  1.44   thorpej 		pbi = kmem_alloc(sizeof(struct genppc_pci_chipset_businfo),
    327  1.44   thorpej 		    KM_SLEEP);
    328  1.27   garbled 		pbi->pbi_properties = prop_dictionary_create();
    329  1.27   garbled 		KASSERT(pbi->pbi_properties != NULL);
    330  1.27   garbled 		setup_pciintr_map(pbi, bus, dev, func);
    331  1.27   garbled 
    332  1.27   garbled 		/* record the parent bus, and the parent device number */
    333  1.27   garbled 		pbus = prop_number_create_integer(bus);
    334  1.27   garbled 		prop_dictionary_set(pbi->pbi_properties, "prep-pcibus-parent",
    335  1.27   garbled 		    pbus);
    336  1.27   garbled 		prop_object_release(pbus);
    337  1.27   garbled 		pbus = prop_number_create_integer(dev);
    338  1.27   garbled 		prop_dictionary_set(pbi->pbi_properties,
    339  1.27   garbled 		    "prep-pcibus-rawdevnum", pbus);
    340  1.27   garbled 		prop_object_release(pbus);
    341  1.27   garbled 
    342  1.27   garbled 		/* now look for bus quirks */
    343  1.27   garbled 
    344  1.27   garbled 		if (PCI_VENDOR(id) == PCI_VENDOR_DEC &&
    345  1.27   garbled 		    PCI_PRODUCT(id) == PCI_PRODUCT_DEC_21154) {
    346  1.27   garbled 			bmax = prop_number_create_integer(8);
    347  1.27   garbled 			KASSERT(bmax != NULL);
    348  1.27   garbled 			prop_dictionary_set(pbi->pbi_properties,
    349  1.27   garbled 			    "prep-pcibus-maxdevices", bmax);
    350  1.27   garbled 			prop_object_release(bmax);
    351  1.27   garbled 		}
    352  1.27   garbled 
    353  1.32   garbled 		SIMPLEQ_INSERT_TAIL(&genppc_pct->pc_pbi, pbi, next);
    354  1.27   garbled 	}
    355  1.27   garbled 
    356  1.22   gdamore 	return (PCI_CONF_DEFAULT);
    357   1.1    nonaka }
    358