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