1*d206c345Sthorpej /* $NetBSD: pci_machdep.c,v 1.44 2020/11/21 15:59:53 thorpej Exp $ */
237eb9eebSnonaka
337eb9eebSnonaka /*
437eb9eebSnonaka * Copyright (c) 1996 Christopher G. Demetriou. All rights reserved.
537eb9eebSnonaka * Copyright (c) 1994 Charles M. Hannum. All rights reserved.
637eb9eebSnonaka *
737eb9eebSnonaka * Redistribution and use in source and binary forms, with or without
837eb9eebSnonaka * modification, are permitted provided that the following conditions
937eb9eebSnonaka * are met:
1037eb9eebSnonaka * 1. Redistributions of source code must retain the above copyright
1137eb9eebSnonaka * notice, this list of conditions and the following disclaimer.
1237eb9eebSnonaka * 2. Redistributions in binary form must reproduce the above copyright
1337eb9eebSnonaka * notice, this list of conditions and the following disclaimer in the
1437eb9eebSnonaka * documentation and/or other materials provided with the distribution.
1537eb9eebSnonaka * 3. All advertising materials mentioning features or use of this software
1637eb9eebSnonaka * must display the following acknowledgement:
1737eb9eebSnonaka * This product includes software developed by Charles M. Hannum.
1837eb9eebSnonaka * 4. The name of the author may not be used to endorse or promote products
1937eb9eebSnonaka * derived from this software without specific prior written permission.
2037eb9eebSnonaka *
2137eb9eebSnonaka * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
2237eb9eebSnonaka * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
2337eb9eebSnonaka * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
2437eb9eebSnonaka * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
2537eb9eebSnonaka * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
2637eb9eebSnonaka * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
2737eb9eebSnonaka * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
2837eb9eebSnonaka * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
2937eb9eebSnonaka * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
3037eb9eebSnonaka * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
3137eb9eebSnonaka */
3237eb9eebSnonaka
3337eb9eebSnonaka /*
3437eb9eebSnonaka * Machine-specific functions for PCI autoconfiguration.
3537eb9eebSnonaka *
3637eb9eebSnonaka * On PCs, there are two methods of generating PCI configuration cycles.
3737eb9eebSnonaka * We try to detect the appropriate mechanism for this machine and set
3837eb9eebSnonaka * up a few function pointers to access the correct method directly.
3937eb9eebSnonaka */
4037eb9eebSnonaka
41ed517291Slukem #include <sys/cdefs.h>
42*d206c345Sthorpej __KERNEL_RCSID(0, "$NetBSD: pci_machdep.c,v 1.44 2020/11/21 15:59:53 thorpej Exp $");
43ed517291Slukem
4437eb9eebSnonaka #include <sys/types.h>
4537eb9eebSnonaka #include <sys/param.h>
4637eb9eebSnonaka #include <sys/time.h>
4737eb9eebSnonaka #include <sys/systm.h>
4837eb9eebSnonaka #include <sys/errno.h>
495410bbdbSkleink #include <sys/extent.h>
5037eb9eebSnonaka #include <sys/device.h>
51*d206c345Sthorpej #include <sys/kmem.h>
5237eb9eebSnonaka
53fa00dc59Smrg #include <uvm/uvm_extern.h>
5437eb9eebSnonaka
5580e152f0Snonaka #define _POWERPC_BUS_DMA_PRIVATE
565a5fb3acSdyoung #include <sys/bus.h>
5737eb9eebSnonaka #include <machine/intr.h>
58992a1d0bSnonaka #include <machine/platform.h>
591dd8b987Sgarbled #include <machine/pnp.h>
6037eb9eebSnonaka
6137eb9eebSnonaka #include <dev/isa/isavar.h>
625410bbdbSkleink
6337eb9eebSnonaka #include <dev/pci/pcivar.h>
6437eb9eebSnonaka #include <dev/pci/pcireg.h>
6537eb9eebSnonaka #include <dev/pci/pcidevs.h>
665410bbdbSkleink #include <dev/pci/pciconf.h>
67992a1d0bSnonaka
6876847dcbSgarbled /* 0 == direct 1 == indirect */
6976847dcbSgarbled int prep_pci_config_mode = 1;
70d974db0aSgarbled extern struct genppc_pci_chipset *genppc_pct;
71d974db0aSgarbled extern u_int32_t prep_pci_baseaddr;
72d974db0aSgarbled extern u_int32_t prep_pci_basedata;
73d974db0aSgarbled
74d974db0aSgarbled static void
prep_pci_get_chipset_tag_indirect(pci_chipset_tag_t pc)75d974db0aSgarbled prep_pci_get_chipset_tag_indirect(pci_chipset_tag_t pc)
76d974db0aSgarbled {
77d974db0aSgarbled
78d974db0aSgarbled pc->pc_conf_v = (void *)pc;
79d974db0aSgarbled
80d974db0aSgarbled pc->pc_attach_hook = genppc_pci_indirect_attach_hook;
81d974db0aSgarbled pc->pc_bus_maxdevs = prep_pci_bus_maxdevs;
82d974db0aSgarbled pc->pc_make_tag = genppc_pci_indirect_make_tag;
83d974db0aSgarbled pc->pc_conf_read = genppc_pci_indirect_conf_read;
84d974db0aSgarbled pc->pc_conf_write = genppc_pci_indirect_conf_write;
85d974db0aSgarbled
86d974db0aSgarbled pc->pc_intr_v = (void *)pc;
87d974db0aSgarbled
88d974db0aSgarbled pc->pc_intr_map = prep_pci_intr_map;
89d974db0aSgarbled pc->pc_intr_string = genppc_pci_intr_string;
90d974db0aSgarbled pc->pc_intr_evcnt = genppc_pci_intr_evcnt;
91d974db0aSgarbled pc->pc_intr_establish = genppc_pci_intr_establish;
92d974db0aSgarbled pc->pc_intr_disestablish = genppc_pci_intr_disestablish;
93dd2488a8Smatt pc->pc_intr_setattr = genppc_pci_intr_setattr;
94e4a54b41Snonaka pc->pc_intr_type = genppc_pci_intr_type;
95e4a54b41Snonaka pc->pc_intr_alloc = genppc_pci_intr_alloc;
96e4a54b41Snonaka pc->pc_intr_release = genppc_pci_intr_release;
97e4a54b41Snonaka pc->pc_intx_alloc = genppc_pci_intx_alloc;
98d974db0aSgarbled
996ace03d3Smatt pc->pc_msi_v = (void *)pc;
1006ace03d3Smatt genppc_pci_chipset_msi_init(pc);
1016ace03d3Smatt
102e4a54b41Snonaka pc->pc_msix_v = (void *)pc;
103e4a54b41Snonaka genppc_pci_chipset_msix_init(pc);
104e4a54b41Snonaka
105d974db0aSgarbled pc->pc_conf_interrupt = genppc_pci_conf_interrupt;
106d974db0aSgarbled pc->pc_decompose_tag = genppc_pci_indirect_decompose_tag;
107d974db0aSgarbled pc->pc_conf_hook = prep_pci_conf_hook;
108d974db0aSgarbled
1094a40b014Smatt pc->pc_addr = mapiodev(prep_pci_baseaddr, 4, false);
1104a40b014Smatt pc->pc_data = mapiodev(prep_pci_basedata, 4, false);
111d974db0aSgarbled pc->pc_bus = 0;
112d974db0aSgarbled pc->pc_node = 0;
113d974db0aSgarbled pc->pc_memt = 0;
114d974db0aSgarbled pc->pc_iot = 0;
115d974db0aSgarbled }
11676847dcbSgarbled
1171dd8b987Sgarbled void
prep_pci_get_chipset_tag(pci_chipset_tag_t pc)1181dd8b987Sgarbled prep_pci_get_chipset_tag(pci_chipset_tag_t pc)
1191dd8b987Sgarbled {
1201dd8b987Sgarbled int i;
1211dd8b987Sgarbled
1221dd8b987Sgarbled i = pci_chipset_tag_type();
1231dd8b987Sgarbled
12476847dcbSgarbled if (i == PCIBridgeIndirect || i == PCIBridgeRS6K) {
12576847dcbSgarbled prep_pci_config_mode = 1;
1261dd8b987Sgarbled prep_pci_get_chipset_tag_indirect(pc);
12776847dcbSgarbled } else if (i == PCIBridgeDirect) {
1281dd8b987Sgarbled prep_pci_get_chipset_tag_direct(pc);
12976847dcbSgarbled prep_pci_config_mode = 0;
13076847dcbSgarbled } else
1311dd8b987Sgarbled panic("Unknown PCI chipset tag configuration method");
1321dd8b987Sgarbled }
1331dd8b987Sgarbled
13437eb9eebSnonaka int
prep_pci_bus_maxdevs(void * v,int busno)1354f2934afSmatt prep_pci_bus_maxdevs(void *v, int busno)
13637eb9eebSnonaka {
137d974db0aSgarbled struct genppc_pci_chipset_businfo *pbi;
13876847dcbSgarbled prop_object_t busmax;
13937eb9eebSnonaka
140d974db0aSgarbled pbi = SIMPLEQ_FIRST(&genppc_pct->pc_pbi);
14176847dcbSgarbled while (busno--)
14276847dcbSgarbled pbi = SIMPLEQ_NEXT(pbi, next);
14376847dcbSgarbled if (pbi == NULL)
14476847dcbSgarbled return 32;
14576847dcbSgarbled
14676847dcbSgarbled busmax = prop_dictionary_get(pbi->pbi_properties,
14776847dcbSgarbled "prep-pcibus-maxdevices");
14876847dcbSgarbled if (busmax == NULL)
14976847dcbSgarbled return 32;
15076847dcbSgarbled else
15176847dcbSgarbled return prop_number_integer_value(busmax);
15276847dcbSgarbled
15376847dcbSgarbled return 32;
15437eb9eebSnonaka }
15537eb9eebSnonaka
15637eb9eebSnonaka int
prep_pci_intr_map(const struct pci_attach_args * pa,pci_intr_handle_t * ihp)157d3e53912Sdyoung prep_pci_intr_map(const struct pci_attach_args *pa, pci_intr_handle_t *ihp)
15837eb9eebSnonaka {
159d974db0aSgarbled struct genppc_pci_chipset_businfo *pbi;
16076847dcbSgarbled prop_dictionary_t dict, devsub;
16176847dcbSgarbled prop_object_t pinsub;
16276847dcbSgarbled prop_number_t pbus;
163fd97fa0bSmrg int busno, pin, line, dev, origdev, i;
16476847dcbSgarbled char key[20];
16537eb9eebSnonaka
16676847dcbSgarbled pin = pa->pa_intrpin;
16776847dcbSgarbled line = pa->pa_intrline;
168fd97fa0bSmrg busno = pa->pa_bus;
169ae2d0d18Sgarbled origdev = dev = pa->pa_device;
170bbcca898Sgarbled i = 0;
17176847dcbSgarbled
172d974db0aSgarbled pbi = SIMPLEQ_FIRST(&genppc_pct->pc_pbi);
17376847dcbSgarbled while (busno--)
17476847dcbSgarbled pbi = SIMPLEQ_NEXT(pbi, next);
17576847dcbSgarbled KASSERT(pbi != NULL);
17676847dcbSgarbled
17776847dcbSgarbled dict = prop_dictionary_get(pbi->pbi_properties, "prep-pci-intrmap");
178ae2d0d18Sgarbled
179bbcca898Sgarbled if (dict != NULL)
180bbcca898Sgarbled i = prop_dictionary_count(dict);
181bbcca898Sgarbled
182bbcca898Sgarbled if (dict == NULL || i == 0) {
18376847dcbSgarbled /* We have a non-PReP bus. now it gets hard */
18476847dcbSgarbled pbus = prop_dictionary_get(pbi->pbi_properties,
18576847dcbSgarbled "prep-pcibus-parent");
18676847dcbSgarbled if (pbus == NULL)
18776847dcbSgarbled goto bad;
18876847dcbSgarbled busno = prop_number_integer_value(pbus);
18976847dcbSgarbled pbus = prop_dictionary_get(pbi->pbi_properties,
19076847dcbSgarbled "prep-pcibus-rawdevnum");
19176847dcbSgarbled dev = prop_number_integer_value(pbus);
19276847dcbSgarbled
193f0a7346dSsnj /* now that we know the parent bus, we need to find its pbi */
194d974db0aSgarbled pbi = SIMPLEQ_FIRST(&genppc_pct->pc_pbi);
19576847dcbSgarbled while (busno--)
19676847dcbSgarbled pbi = SIMPLEQ_NEXT(pbi, next);
19776847dcbSgarbled KASSERT(pbi != NULL);
19876847dcbSgarbled
199ae2d0d18Sgarbled /* swizzle the pin */
200ae2d0d18Sgarbled pin = ((pin + origdev - 1) & 3) + 1;
201ae2d0d18Sgarbled
20276847dcbSgarbled /* now we have the pbi, ask for dict again */
20376847dcbSgarbled dict = prop_dictionary_get(pbi->pbi_properties,
20476847dcbSgarbled "prep-pci-intrmap");
20576847dcbSgarbled if (dict == NULL)
20637eb9eebSnonaka goto bad;
20737eb9eebSnonaka }
20837eb9eebSnonaka
20976847dcbSgarbled /* No IRQ used. */
21076847dcbSgarbled if (pin == 0)
21176847dcbSgarbled goto bad;
21237eb9eebSnonaka if (pin > 4) {
213d974db0aSgarbled aprint_error("pci_intr_map: bad interrupt pin %d\n", pin);
21437eb9eebSnonaka goto bad;
21537eb9eebSnonaka }
21637eb9eebSnonaka
2178d7b7627Schristos snprintf(key, sizeof(key), "devfunc-%d", dev);
21876847dcbSgarbled devsub = prop_dictionary_get(dict, key);
21976847dcbSgarbled if (devsub == NULL)
22076847dcbSgarbled goto bad;
2218d7b7627Schristos snprintf(key, sizeof(key), "pin-%c", 'A' + (pin-1));
22276847dcbSgarbled pinsub = prop_dictionary_get(devsub, key);
22376847dcbSgarbled if (pinsub == NULL)
22476847dcbSgarbled goto bad;
22576847dcbSgarbled line = prop_number_integer_value(pinsub);
22676847dcbSgarbled
22737eb9eebSnonaka /*
22837eb9eebSnonaka * Section 6.2.4, `Miscellaneous Functions', says that 255 means
22937eb9eebSnonaka * `unknown' or `no connection' on a PC. We assume that a device with
23037eb9eebSnonaka * `no connection' either doesn't have an interrupt (in which case the
23137eb9eebSnonaka * pin number should be 0, and would have been noticed above), or
23237eb9eebSnonaka * wasn't configured by the BIOS (in which case we punt, since there's
23337eb9eebSnonaka * no real way we can know how the interrupt lines are mapped in the
23437eb9eebSnonaka * hardware).
23537eb9eebSnonaka *
23637eb9eebSnonaka * XXX
23737eb9eebSnonaka * Since IRQ 0 is only used by the clock, and we can't actually be sure
23837eb9eebSnonaka * that the BIOS did its job, we also recognize that as meaning that
23937eb9eebSnonaka * the BIOS has not configured the device.
24037eb9eebSnonaka */
24137eb9eebSnonaka if (line == 0 || line == 255) {
242d974db0aSgarbled aprint_error("pci_intr_map: no mapping for pin %c\n",
243d974db0aSgarbled '@' + pin);
24437eb9eebSnonaka goto bad;
24537eb9eebSnonaka } else {
24637eb9eebSnonaka if (line >= ICU_LEN) {
247d974db0aSgarbled aprint_error("pci_intr_map: bad interrupt line %d\n",
248d974db0aSgarbled line);
24937eb9eebSnonaka goto bad;
25037eb9eebSnonaka }
25137eb9eebSnonaka if (line == IRQ_SLAVE) {
252d974db0aSgarbled aprint_verbose("pci_intr_map: changed line 2 to line 9\n");
25337eb9eebSnonaka line = 9;
25437eb9eebSnonaka }
25537eb9eebSnonaka }
25637eb9eebSnonaka
25737eb9eebSnonaka *ihp = line;
25837eb9eebSnonaka return 0;
25937eb9eebSnonaka
26037eb9eebSnonaka bad:
26137eb9eebSnonaka *ihp = -1;
26237eb9eebSnonaka return 1;
26337eb9eebSnonaka }
26437eb9eebSnonaka
26576847dcbSgarbled extern pcitag_t prep_pci_direct_make_tag(void *, int, int, int);
266d974db0aSgarbled extern pcitag_t genppc_pci_indirect_make_tag(void *, int, int, int);
26776847dcbSgarbled extern pcireg_t prep_pci_direct_conf_read(void *, pcitag_t, int);
268d974db0aSgarbled extern pcireg_t genppc_pci_indirect_conf_read(void *, pcitag_t, int);
26976847dcbSgarbled
2705410bbdbSkleink int
prep_pci_conf_hook(void * v,int bus,int dev,int func,pcireg_t id)271dd2488a8Smatt prep_pci_conf_hook(void *v, int bus, int dev, int func, pcireg_t id)
2725410bbdbSkleink {
273dd2488a8Smatt pci_chipset_tag_t pc = v;
274d974db0aSgarbled struct genppc_pci_chipset_businfo *pbi;
27576847dcbSgarbled prop_number_t bmax, pbus;
27676847dcbSgarbled pcitag_t tag;
27776847dcbSgarbled pcireg_t class;
2785410bbdbSkleink
2795410bbdbSkleink /*
2805410bbdbSkleink * The P9100 board found in some IBM machines cannot be
2815410bbdbSkleink * over-configured.
2825410bbdbSkleink */
2835410bbdbSkleink if (PCI_VENDOR(id) == PCI_VENDOR_WEITEK &&
2845410bbdbSkleink PCI_PRODUCT(id) == PCI_PRODUCT_WEITEK_P9100)
2855410bbdbSkleink return 0;
2865410bbdbSkleink
287d97db384Ssnj /* We have already mapped the MPIC2 if we have one, so leave it
288b19f0d31Sgarbled alone */
289b19f0d31Sgarbled if (PCI_VENDOR(id) == PCI_VENDOR_IBM &&
290b19f0d31Sgarbled PCI_PRODUCT(id) == PCI_PRODUCT_IBM_MPIC2)
291b19f0d31Sgarbled return 0;
292b19f0d31Sgarbled
29330532444Sgarbled if (PCI_VENDOR(id) == PCI_VENDOR_IBM &&
29430532444Sgarbled PCI_PRODUCT(id) == PCI_PRODUCT_IBM_MPIC)
29530532444Sgarbled return 0;
29630532444Sgarbled
29776847dcbSgarbled if (PCI_VENDOR(id) == PCI_VENDOR_INTEL &&
29876847dcbSgarbled PCI_PRODUCT(id) == PCI_PRODUCT_INTEL_PCEB)
29976847dcbSgarbled return 0;
30076847dcbSgarbled
3018e09750dSgarbled if (PCI_VENDOR(id) == PCI_VENDOR_MOT &&
3028e09750dSgarbled PCI_PRODUCT(id) == PCI_PRODUCT_MOT_RAVEN)
3038e09750dSgarbled return (PCI_CONF_ALL & ~PCI_CONF_MAP_MEM);
3048e09750dSgarbled
30576847dcbSgarbled /* NOTE, all device specific stuff must be above this line */
30676847dcbSgarbled /* don't do this on the primary host bridge */
30776847dcbSgarbled if (bus == 0 && dev == 0 && func == 0)
30876847dcbSgarbled return PCI_CONF_DEFAULT;
30976847dcbSgarbled
31076847dcbSgarbled if (prep_pci_config_mode) {
311dd2488a8Smatt tag = genppc_pci_indirect_make_tag(pc, bus, dev, func);
312dd2488a8Smatt class = genppc_pci_indirect_conf_read(pc, tag,
31376847dcbSgarbled PCI_CLASS_REG);
31476847dcbSgarbled } else {
315dd2488a8Smatt tag = prep_pci_direct_make_tag(pc, bus, dev, func);
316dd2488a8Smatt class = prep_pci_direct_conf_read(pc, tag,
31776847dcbSgarbled PCI_CLASS_REG);
31876847dcbSgarbled }
31976847dcbSgarbled
32076847dcbSgarbled /*
32176847dcbSgarbled * PCI bridges have special needs. We need to discover where they
32276847dcbSgarbled * came from, and wire them appropriately.
32376847dcbSgarbled */
32476847dcbSgarbled if (PCI_CLASS(class) == PCI_CLASS_BRIDGE &&
32576847dcbSgarbled PCI_SUBCLASS(class) == PCI_SUBCLASS_BRIDGE_PCI) {
326*d206c345Sthorpej pbi = kmem_alloc(sizeof(struct genppc_pci_chipset_businfo),
327*d206c345Sthorpej KM_SLEEP);
32876847dcbSgarbled pbi->pbi_properties = prop_dictionary_create();
32976847dcbSgarbled KASSERT(pbi->pbi_properties != NULL);
33076847dcbSgarbled setup_pciintr_map(pbi, bus, dev, func);
33176847dcbSgarbled
33276847dcbSgarbled /* record the parent bus, and the parent device number */
33376847dcbSgarbled pbus = prop_number_create_integer(bus);
33476847dcbSgarbled prop_dictionary_set(pbi->pbi_properties, "prep-pcibus-parent",
33576847dcbSgarbled pbus);
33676847dcbSgarbled prop_object_release(pbus);
33776847dcbSgarbled pbus = prop_number_create_integer(dev);
33876847dcbSgarbled prop_dictionary_set(pbi->pbi_properties,
33976847dcbSgarbled "prep-pcibus-rawdevnum", pbus);
34076847dcbSgarbled prop_object_release(pbus);
34176847dcbSgarbled
34276847dcbSgarbled /* now look for bus quirks */
34376847dcbSgarbled
34476847dcbSgarbled if (PCI_VENDOR(id) == PCI_VENDOR_DEC &&
34576847dcbSgarbled PCI_PRODUCT(id) == PCI_PRODUCT_DEC_21154) {
34676847dcbSgarbled bmax = prop_number_create_integer(8);
34776847dcbSgarbled KASSERT(bmax != NULL);
34876847dcbSgarbled prop_dictionary_set(pbi->pbi_properties,
34976847dcbSgarbled "prep-pcibus-maxdevices", bmax);
35076847dcbSgarbled prop_object_release(bmax);
35176847dcbSgarbled }
35276847dcbSgarbled
353d974db0aSgarbled SIMPLEQ_INSERT_TAIL(&genppc_pct->pc_pbi, pbi, next);
35476847dcbSgarbled }
35576847dcbSgarbled
35684308388Sgdamore return (PCI_CONF_DEFAULT);
3575410bbdbSkleink }
358