1*90d7b449Sthorpej /* $NetBSD: pyro.c,v 1.26 2022/01/21 19:14:14 thorpej Exp $ */
2a1b3c3f0Smrg /* from: $OpenBSD: pyro.c,v 1.20 2010/12/05 15:15:14 kettenis Exp $ */
35b208c27Smrg
45b208c27Smrg /*
55b208c27Smrg * Copyright (c) 2002 Jason L. Wright (jason@thought.net)
65b208c27Smrg * Copyright (c) 2003 Henric Jungheim
75b208c27Smrg * Copyright (c) 2007 Mark Kettenis
8a1b3c3f0Smrg * Copyright (c) 2011 Matthew R. Green
95b208c27Smrg * All rights reserved.
105b208c27Smrg *
115b208c27Smrg * Redistribution and use in source and binary forms, with or without
125b208c27Smrg * modification, are permitted provided that the following conditions
135b208c27Smrg * are met:
145b208c27Smrg * 1. Redistributions of source code must retain the above copyright
155b208c27Smrg * notice, this list of conditions and the following disclaimer.
165b208c27Smrg * 2. Redistributions in binary form must reproduce the above copyright
175b208c27Smrg * notice, this list of conditions and the following disclaimer in the
185b208c27Smrg * documentation and/or other materials provided with the distribution.
195b208c27Smrg *
205b208c27Smrg * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
215b208c27Smrg * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
225b208c27Smrg * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
235b208c27Smrg * DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT,
245b208c27Smrg * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
255b208c27Smrg * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
265b208c27Smrg * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
275b208c27Smrg * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
285b208c27Smrg * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
295b208c27Smrg * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
305b208c27Smrg * POSSIBILITY OF SUCH DAMAGE.
315b208c27Smrg */
325b208c27Smrg
33f72d1cdaSmrg #include <sys/cdefs.h>
34*90d7b449Sthorpej __KERNEL_RCSID(0, "$NetBSD: pyro.c,v 1.26 2022/01/21 19:14:14 thorpej Exp $");
35f72d1cdaSmrg
365b208c27Smrg #include <sys/param.h>
375b208c27Smrg #include <sys/device.h>
385b208c27Smrg #include <sys/errno.h>
395b208c27Smrg #include <sys/malloc.h>
40fc256c4aSthorpej #include <sys/kmem.h>
415b208c27Smrg #include <sys/systm.h>
425b208c27Smrg
435b208c27Smrg #define _SPARC_BUS_DMA_PRIVATE
44b6584574Sdyoung #include <sys/bus.h>
455b208c27Smrg #include <machine/autoconf.h>
465b208c27Smrg
475b208c27Smrg #ifdef DDB
485b208c27Smrg #include <machine/db_machdep.h>
495b208c27Smrg #endif
505b208c27Smrg
515b208c27Smrg #include <dev/pci/pcivar.h>
525b208c27Smrg #include <dev/pci/pcireg.h>
535b208c27Smrg
545b208c27Smrg #include <sparc64/dev/iommureg.h>
555b208c27Smrg #include <sparc64/dev/iommuvar.h>
565b208c27Smrg #include <sparc64/dev/pyrovar.h>
575b208c27Smrg
585b208c27Smrg #ifdef DEBUG
595b208c27Smrg #define PDB_PROM 0x01
605b208c27Smrg #define PDB_BUSMAP 0x02
615b208c27Smrg #define PDB_INTR 0x04
625b208c27Smrg #define PDB_CONF 0x08
63cc564476Smrg int pyro_debug = 0x0;
645b208c27Smrg #define DPRINTF(l, s) do { if (pyro_debug & l) printf s; } while (0)
655b208c27Smrg #else
665b208c27Smrg #define DPRINTF(l, s)
675b208c27Smrg #endif
685b208c27Smrg
695b208c27Smrg #define FIRE_RESET_GEN 0x7010
705b208c27Smrg
715b208c27Smrg #define FIRE_RESET_GEN_XIR 0x0000000000000002L
725b208c27Smrg
735b208c27Smrg #define FIRE_INTRMAP_INT_CNTRL_NUM_MASK 0x000003c0
745b208c27Smrg #define FIRE_INTRMAP_INT_CNTRL_NUM0 0x00000040
755b208c27Smrg #define FIRE_INTRMAP_INT_CNTRL_NUM1 0x00000080
765b208c27Smrg #define FIRE_INTRMAP_INT_CNTRL_NUM2 0x00000100
775b208c27Smrg #define FIRE_INTRMAP_INT_CNTRL_NUM3 0x00000200
785b208c27Smrg #define FIRE_INTRMAP_T_JPID_SHIFT 26
795b208c27Smrg #define FIRE_INTRMAP_T_JPID_MASK 0x7c000000
805b208c27Smrg
815b208c27Smrg #define OBERON_INTRMAP_T_DESTID_SHIFT 21
825b208c27Smrg #define OBERON_INTRMAP_T_DESTID_MASK 0x7fe00000
835b208c27Smrg
845b208c27Smrg extern struct sparc_pci_chipset _sparc_pci_chipset;
855b208c27Smrg
8640babd3eSchristos int pyro_match(device_t, cfdata_t, void *);
8740babd3eSchristos void pyro_attach(device_t, device_t, void *);
88a1b3c3f0Smrg int pyro_print(void *, const char *);
89a1b3c3f0Smrg
900cdde954Schristos CFATTACH_DECL_NEW(pyro, sizeof(struct pyro_softc),
91a1b3c3f0Smrg pyro_match, pyro_attach, NULL, NULL);
92a1b3c3f0Smrg
935b208c27Smrg void pyro_init(struct pyro_softc *, int);
945b208c27Smrg void pyro_init_iommu(struct pyro_softc *, struct pyro_pbm *);
955b208c27Smrg
965b208c27Smrg pci_chipset_tag_t pyro_alloc_chipset(struct pyro_pbm *, int,
975b208c27Smrg pci_chipset_tag_t);
985b208c27Smrg bus_space_tag_t pyro_alloc_mem_tag(struct pyro_pbm *);
995b208c27Smrg bus_space_tag_t pyro_alloc_io_tag(struct pyro_pbm *);
1005b208c27Smrg bus_space_tag_t pyro_alloc_config_tag(struct pyro_pbm *);
101a1b3c3f0Smrg bus_space_tag_t pyro_alloc_bus_tag(struct pyro_pbm *, const char *, int);
1025b208c27Smrg bus_dma_tag_t pyro_alloc_dma_tag(struct pyro_pbm *);
1035b208c27Smrg
104a1b3c3f0Smrg #if 0
1055b208c27Smrg int pyro_conf_size(pci_chipset_tag_t, pcitag_t);
106a1b3c3f0Smrg #endif
1075b208c27Smrg pcireg_t pyro_conf_read(pci_chipset_tag_t, pcitag_t, int);
1085b208c27Smrg void pyro_conf_write(pci_chipset_tag_t, pcitag_t, int, pcireg_t);
1095b208c27Smrg
110a1b3c3f0Smrg static void * pyro_pci_intr_establish(pci_chipset_tag_t pc,
111a1b3c3f0Smrg pci_intr_handle_t ih, int level,
112a1b3c3f0Smrg int (*func)(void *), void *arg);
1135b208c27Smrg
114d3e53912Sdyoung int pyro_intr_map(const struct pci_attach_args *, pci_intr_handle_t *);
115a1b3c3f0Smrg int pyro_bus_map(bus_space_tag_t, bus_addr_t,
116a1b3c3f0Smrg bus_size_t, int, vaddr_t, bus_space_handle_t *);
117a1b3c3f0Smrg paddr_t pyro_bus_mmap(bus_space_tag_t, bus_addr_t, off_t,
118a1b3c3f0Smrg int, int);
119a1b3c3f0Smrg void *pyro_intr_establish(bus_space_tag_t, int, int,
120a1b3c3f0Smrg int (*)(void *), void *, void (*)(void));
121a1b3c3f0Smrg
122a1b3c3f0Smrg int pyro_dmamap_create(bus_dma_tag_t, bus_size_t, int,
1235b208c27Smrg bus_size_t, bus_size_t, int, bus_dmamap_t *);
1245b208c27Smrg
1255b208c27Smrg int
pyro_match(device_t parent,cfdata_t match,void * aux)126cbab9cadSchs pyro_match(device_t parent, cfdata_t match, void *aux)
1275b208c27Smrg {
1285b208c27Smrg struct mainbus_attach_args *ma = aux;
1295b208c27Smrg char *str;
1305b208c27Smrg
1315b208c27Smrg if (strcmp(ma->ma_name, "pci") != 0)
1325b208c27Smrg return (0);
1335b208c27Smrg
134a1b3c3f0Smrg str = prom_getpropstring(ma->ma_node, "compatible");
1355b208c27Smrg if (strcmp(str, "pciex108e,80f0") == 0 ||
1365b208c27Smrg strcmp(str, "pciex108e,80f8") == 0)
1375b208c27Smrg return (1);
1385b208c27Smrg
1395b208c27Smrg return (0);
1405b208c27Smrg }
1415b208c27Smrg
1425b208c27Smrg void
pyro_attach(device_t parent,device_t self,void * aux)143cbab9cadSchs pyro_attach(device_t parent, device_t self, void *aux)
1445b208c27Smrg {
14540babd3eSchristos struct pyro_softc *sc = device_private(self);
1465b208c27Smrg struct mainbus_attach_args *ma = aux;
1475b208c27Smrg char *str;
1485b208c27Smrg int busa;
1495b208c27Smrg
15040babd3eSchristos sc->sc_dev = self;
1515b208c27Smrg sc->sc_node = ma->ma_node;
1525b208c27Smrg sc->sc_dmat = ma->ma_dmatag;
153a1b3c3f0Smrg sc->sc_bustag = ma->ma_bustag;
1545b208c27Smrg sc->sc_csr = ma->ma_reg[0].ur_paddr;
1555b208c27Smrg sc->sc_xbc = ma->ma_reg[1].ur_paddr;
1565b208c27Smrg sc->sc_ign = INTIGN(ma->ma_upaid << INTMAP_IGN_SHIFT);
1575b208c27Smrg
1585b208c27Smrg if ((ma->ma_reg[0].ur_paddr & 0x00700000) == 0x00600000)
1595b208c27Smrg busa = 1;
1605b208c27Smrg else
1615b208c27Smrg busa = 0;
1625b208c27Smrg
163a1b3c3f0Smrg if (bus_space_map(sc->sc_bustag, sc->sc_csr,
164a1b3c3f0Smrg ma->ma_reg[0].ur_len, BUS_SPACE_MAP_LINEAR, &sc->sc_csrh)) {
1655b208c27Smrg printf(": failed to map csr registers\n");
1665b208c27Smrg return;
1675b208c27Smrg }
1685b208c27Smrg
169a1b3c3f0Smrg if (bus_space_map(sc->sc_bustag, sc->sc_xbc,
1705b208c27Smrg ma->ma_reg[1].ur_len, 0, &sc->sc_xbch)) {
1715b208c27Smrg printf(": failed to map xbc registers\n");
1725b208c27Smrg return;
1735b208c27Smrg }
1745b208c27Smrg
175a1b3c3f0Smrg str = prom_getpropstring(ma->ma_node, "compatible");
1765b208c27Smrg if (strcmp(str, "pciex108e,80f8") == 0)
1775b208c27Smrg sc->sc_oberon = 1;
1785b208c27Smrg
1795b208c27Smrg pyro_init(sc, busa);
1805b208c27Smrg }
1815b208c27Smrg
1825b208c27Smrg void
pyro_init(struct pyro_softc * sc,int busa)1835b208c27Smrg pyro_init(struct pyro_softc *sc, int busa)
1845b208c27Smrg {
1855b208c27Smrg struct pyro_pbm *pbm;
1865b208c27Smrg struct pcibus_attach_args pba;
1875b208c27Smrg int *busranges = NULL, nranges;
1885b208c27Smrg
189fc256c4aSthorpej pbm = kmem_zalloc(sizeof(*pbm), KM_SLEEP);
1905b208c27Smrg pbm->pp_sc = sc;
1915b208c27Smrg pbm->pp_bus_a = busa;
1925b208c27Smrg
193a1b3c3f0Smrg if (prom_getprop(sc->sc_node, "ranges", sizeof(struct pyro_range),
1945b208c27Smrg &pbm->pp_nrange, (void **)&pbm->pp_range))
1955b208c27Smrg panic("pyro: can't get ranges");
1965b208c27Smrg
197a1b3c3f0Smrg if (prom_getprop(sc->sc_node, "bus-range", sizeof(int), &nranges,
1985b208c27Smrg (void **)&busranges))
1995b208c27Smrg panic("pyro: can't get bus-range");
2005b208c27Smrg
2015b208c27Smrg printf(": \"%s\", rev %d, ign %x, bus %c %d to %d\n",
2025b208c27Smrg sc->sc_oberon ? "Oberon" : "Fire",
203a1b3c3f0Smrg prom_getpropint(sc->sc_node, "module-revision#", 0), sc->sc_ign,
2045b208c27Smrg busa ? 'A' : 'B', busranges[0], busranges[1]);
2055b208c27Smrg
20640babd3eSchristos printf("%s: ", device_xname(sc->sc_dev));
2075b208c27Smrg pyro_init_iommu(sc, pbm);
2085b208c27Smrg
2095b208c27Smrg pbm->pp_memt = pyro_alloc_mem_tag(pbm);
2105b208c27Smrg pbm->pp_iot = pyro_alloc_io_tag(pbm);
2115b208c27Smrg pbm->pp_cfgt = pyro_alloc_config_tag(pbm);
2125b208c27Smrg pbm->pp_dmat = pyro_alloc_dma_tag(pbm);
213a6b2b839Sdyoung pbm->pp_flags = (pbm->pp_memt ? PCI_FLAGS_MEM_OKAY : 0) |
214a6b2b839Sdyoung (pbm->pp_iot ? PCI_FLAGS_IO_OKAY : 0);
2155b208c27Smrg
2165b208c27Smrg if (bus_space_map(pbm->pp_cfgt, 0, 0x10000000, 0, &pbm->pp_cfgh))
2175b208c27Smrg panic("pyro: can't map config space");
2185b208c27Smrg
2195b208c27Smrg pbm->pp_pc = pyro_alloc_chipset(pbm, sc->sc_node, &_sparc_pci_chipset);
220a1b3c3f0Smrg pbm->pp_pc->spc_busmax = busranges[1];
221fc256c4aSthorpej pbm->pp_pc->spc_busnode = kmem_zalloc(sizeof(*pbm->pp_pc->spc_busnode),
222fc256c4aSthorpej KM_SLEEP);
2235b208c27Smrg
224a1b3c3f0Smrg #if 0
2255b208c27Smrg pbm->pp_pc->bustag = pbm->pp_cfgt;
2265b208c27Smrg pbm->pp_pc->bushandle = pbm->pp_cfgh;
227a1b3c3f0Smrg #endif
2285b208c27Smrg
2295b208c27Smrg bzero(&pba, sizeof(pba));
2305b208c27Smrg pba.pba_bus = busranges[0];
2315b208c27Smrg pba.pba_pc = pbm->pp_pc;
2325b208c27Smrg pba.pba_flags = pbm->pp_flags;
2335b208c27Smrg pba.pba_dmat = pbm->pp_dmat;
234a1b3c3f0Smrg pba.pba_dmat64 = NULL; /* XXX */
2355b208c27Smrg pba.pba_memt = pbm->pp_memt;
2365b208c27Smrg pba.pba_iot = pbm->pp_iot;
2375b208c27Smrg
2385b208c27Smrg free(busranges, M_DEVBUF);
2395b208c27Smrg
24065c738d1Sthorpej config_found(sc->sc_dev, &pba, pyro_print,
241*90d7b449Sthorpej CFARGS(.devhandle = device_handle(sc->sc_dev)));
2425b208c27Smrg }
2435b208c27Smrg
2445b208c27Smrg void
pyro_init_iommu(struct pyro_softc * sc,struct pyro_pbm * pbm)2455b208c27Smrg pyro_init_iommu(struct pyro_softc *sc, struct pyro_pbm *pbm)
2465b208c27Smrg {
2475b208c27Smrg struct iommu_state *is = &pbm->pp_is;
2485b208c27Smrg int tsbsize = 7;
2495b208c27Smrg u_int32_t iobase = -1;
2505b208c27Smrg char *name;
2515b208c27Smrg
252a1b3c3f0Smrg pbm->pp_sb.sb_is = is;
253a1b3c3f0Smrg is->is_bustag = sc->sc_bustag;
2545b208c27Smrg
2555b208c27Smrg if (bus_space_subregion(is->is_bustag, sc->sc_csrh,
2565b208c27Smrg 0x40000, 0x100, &is->is_iommu)) {
2575b208c27Smrg panic("pyro: unable to create iommu handle");
2585b208c27Smrg }
2595b208c27Smrg
2609117b6a1Smrg /* We have no STC. */
2619117b6a1Smrg is->is_sb[0] = NULL;
2625b208c27Smrg
263fc256c4aSthorpej name = kmem_asprintf("%s dvma", device_xname(sc->sc_dev));
2645b208c27Smrg
2659117b6a1Smrg /* Tell iommu how to set the TSB size. */
2669117b6a1Smrg is->is_flags = IOMMU_TSBSIZE_IN_PTSB;
2679117b6a1Smrg
2685b208c27Smrg /* On Oberon, we need to flush the cache. */
2695b208c27Smrg if (sc->sc_oberon)
2705b208c27Smrg is->is_flags |= IOMMU_FLUSH_CACHE;
2715b208c27Smrg
2725b208c27Smrg iommu_init(name, is, tsbsize, iobase);
2735b208c27Smrg }
2745b208c27Smrg
2755b208c27Smrg int
pyro_print(void * aux,const char * p)2765b208c27Smrg pyro_print(void *aux, const char *p)
2775b208c27Smrg {
2785b208c27Smrg if (p == NULL)
2795b208c27Smrg return (UNCONF);
2805b208c27Smrg return (QUIET);
2815b208c27Smrg }
2825b208c27Smrg
2835b208c27Smrg pcireg_t
pyro_conf_read(pci_chipset_tag_t pc,pcitag_t tag,int reg)2845b208c27Smrg pyro_conf_read(pci_chipset_tag_t pc, pcitag_t tag, int reg)
2855b208c27Smrg {
286a1b3c3f0Smrg struct pyro_pbm *pp = pc->cookie;
287a1b3c3f0Smrg pcireg_t val = (pcireg_t)~0;
288cd7b58e0Snakayama int s;
289a1b3c3f0Smrg
290a1b3c3f0Smrg DPRINTF(PDB_CONF, ("%s: tag %lx reg %x ", __func__, (long)tag, reg));
291605f564fSmsaitoh if (PCITAG_NODE(tag) != -1 && (unsigned int)reg < PCI_CONF_SIZE) {
292cd7b58e0Snakayama s = splhigh();
29344f9c0cbSmrg struct cpu_info *ci = curcpu();
294cd7b58e0Snakayama ci->ci_pci_probe = true;
295cd7b58e0Snakayama membar_Sync();
296a1b3c3f0Smrg val = bus_space_read_4(pp->pp_cfgt, pp->pp_cfgh,
297a1b3c3f0Smrg (PCITAG_OFFSET(tag) << 4) + reg);
298cd7b58e0Snakayama membar_Sync();
299cd7b58e0Snakayama if (ci->ci_pci_fault)
300cd7b58e0Snakayama val = (pcireg_t)~0;
301cd7b58e0Snakayama ci->ci_pci_probe = ci->ci_pci_fault = false;
302cd7b58e0Snakayama splx(s);
303cd7b58e0Snakayama }
304a1b3c3f0Smrg DPRINTF(PDB_CONF, (" returning %08x\n", (u_int)val));
305a1b3c3f0Smrg return (val);
3065b208c27Smrg }
3075b208c27Smrg
3085b208c27Smrg void
pyro_conf_write(pci_chipset_tag_t pc,pcitag_t tag,int reg,pcireg_t data)3095b208c27Smrg pyro_conf_write(pci_chipset_tag_t pc, pcitag_t tag, int reg, pcireg_t data)
3105b208c27Smrg {
311a1b3c3f0Smrg struct pyro_pbm *pp = pc->cookie;
312a1b3c3f0Smrg
313a1b3c3f0Smrg DPRINTF(PDB_CONF, ("%s: tag %lx; reg %x; data %x", __func__,
314a1b3c3f0Smrg (long)tag, reg, (int)data));
315a1b3c3f0Smrg
316a1b3c3f0Smrg /* If we don't know it, just punt it. */
317a1b3c3f0Smrg if (PCITAG_NODE(tag) == -1) {
318a1b3c3f0Smrg DPRINTF(PDB_CONF, (" .. bad addr\n"));
319a1b3c3f0Smrg return;
320a1b3c3f0Smrg }
321a1b3c3f0Smrg
322605f564fSmsaitoh if ((unsigned int)reg >= PCI_CONF_SIZE)
323605f564fSmsaitoh return;
324605f564fSmsaitoh
325a1b3c3f0Smrg bus_space_write_4(pp->pp_cfgt, pp->pp_cfgh,
3265b208c27Smrg (PCITAG_OFFSET(tag) << 4) + reg, data);
327a1b3c3f0Smrg DPRINTF(PDB_CONF, (" .. done\n"));
3285b208c27Smrg }
3295b208c27Smrg
3305b208c27Smrg /*
3315b208c27Smrg * Bus-specific interrupt mapping
3325b208c27Smrg */
3335b208c27Smrg int
pyro_intr_map(const struct pci_attach_args * pa,pci_intr_handle_t * ihp)334d3e53912Sdyoung pyro_intr_map(const struct pci_attach_args *pa, pci_intr_handle_t *ihp)
3355b208c27Smrg {
3365b208c27Smrg struct pyro_pbm *pp = pa->pa_pc->cookie;
3375b208c27Smrg struct pyro_softc *sc = pp->pp_sc;
3385b208c27Smrg u_int dev;
3395b208c27Smrg
3405b208c27Smrg if (*ihp != (pci_intr_handle_t)-1) {
3415b208c27Smrg *ihp |= sc->sc_ign;
3429117b6a1Smrg DPRINTF(PDB_INTR, ("%s: not -1 -> ih %lx\n", __func__, (u_long)*ihp));
3435b208c27Smrg return (0);
3445b208c27Smrg }
3455b208c27Smrg
3465b208c27Smrg /*
3475b208c27Smrg * We didn't find a PROM mapping for this interrupt. Try to
3485b208c27Smrg * construct one ourselves based on the swizzled interrupt pin
3495b208c27Smrg * and the interrupt mapping for PCI slots documented in the
3505b208c27Smrg * UltraSPARC-IIi User's Manual.
3515b208c27Smrg */
3525b208c27Smrg
353a1b3c3f0Smrg if (pa->pa_intrpin == 0) {
3549117b6a1Smrg DPRINTF(PDB_INTR, ("%s: no intrpen\n", __func__));
3555b208c27Smrg return (-1);
356a1b3c3f0Smrg }
3575b208c27Smrg
3585b208c27Smrg /*
3595b208c27Smrg * This deserves some documentation. Should anyone
3605b208c27Smrg * have anything official looking, please speak up.
3615b208c27Smrg */
3625b208c27Smrg dev = pa->pa_device - 1;
3635b208c27Smrg
3645b208c27Smrg *ihp = (pa->pa_intrpin - 1) & INTMAP_PCIINT;
3655b208c27Smrg *ihp |= (dev << 2) & INTMAP_PCISLOT;
3665b208c27Smrg *ihp |= sc->sc_ign;
3675b208c27Smrg
3689117b6a1Smrg DPRINTF(PDB_INTR, ("%s: weird hack -> ih %lx\n", __func__, (u_long)*ihp));
3695b208c27Smrg return (0);
3705b208c27Smrg }
3715b208c27Smrg
3725b208c27Smrg bus_space_tag_t
pyro_alloc_mem_tag(struct pyro_pbm * pp)3735b208c27Smrg pyro_alloc_mem_tag(struct pyro_pbm *pp)
3745b208c27Smrg {
375a1b3c3f0Smrg return (pyro_alloc_bus_tag(pp, "mem", PCI_MEMORY_BUS_SPACE));
3765b208c27Smrg }
3775b208c27Smrg
3785b208c27Smrg bus_space_tag_t
pyro_alloc_io_tag(struct pyro_pbm * pp)3795b208c27Smrg pyro_alloc_io_tag(struct pyro_pbm *pp)
3805b208c27Smrg {
381a1b3c3f0Smrg return (pyro_alloc_bus_tag(pp, "io", PCI_IO_BUS_SPACE));
3825b208c27Smrg }
3835b208c27Smrg
3845b208c27Smrg bus_space_tag_t
pyro_alloc_config_tag(struct pyro_pbm * pp)3855b208c27Smrg pyro_alloc_config_tag(struct pyro_pbm *pp)
3865b208c27Smrg {
387a1b3c3f0Smrg return (pyro_alloc_bus_tag(pp, "cfg", PCI_CONFIG_BUS_SPACE));
3885b208c27Smrg }
3895b208c27Smrg
3905b208c27Smrg bus_space_tag_t
pyro_alloc_bus_tag(struct pyro_pbm * pbm,const char * name,int type)391a1b3c3f0Smrg pyro_alloc_bus_tag(struct pyro_pbm *pbm, const char *name, int type)
3925b208c27Smrg {
3935b208c27Smrg struct pyro_softc *sc = pbm->pp_sc;
3945b208c27Smrg struct sparc_bus_space_tag *bt;
3955b208c27Smrg
396fc256c4aSthorpej bt = kmem_zalloc(sizeof(*bt), KM_SLEEP);
3975b208c27Smrg
398a1b3c3f0Smrg #if 0
3995b208c27Smrg snprintf(bt->name, sizeof(bt->name), "%s-pbm_%s(%d/%2.2x)",
40040babd3eSchristos device_xname(sc->sc_dev), name, ss, asi);
401a1b3c3f0Smrg #endif
4025b208c27Smrg
4035b208c27Smrg bt->cookie = pbm;
404a1b3c3f0Smrg bt->parent = sc->sc_bustag;
405a1b3c3f0Smrg bt->type = type;
406a1b3c3f0Smrg bt->sparc_bus_map = pyro_bus_map;
407a1b3c3f0Smrg bt->sparc_bus_mmap = pyro_bus_mmap;
408a1b3c3f0Smrg bt->sparc_intr_establish = pyro_intr_establish;
4095b208c27Smrg return (bt);
4105b208c27Smrg }
4115b208c27Smrg
4125b208c27Smrg bus_dma_tag_t
pyro_alloc_dma_tag(struct pyro_pbm * pbm)4135b208c27Smrg pyro_alloc_dma_tag(struct pyro_pbm *pbm)
4145b208c27Smrg {
4155b208c27Smrg struct pyro_softc *sc = pbm->pp_sc;
4165b208c27Smrg bus_dma_tag_t dt, pdt = sc->sc_dmat;
4175b208c27Smrg
418fc256c4aSthorpej dt = kmem_zalloc(sizeof(*dt), KM_SLEEP);
4195b208c27Smrg dt->_cookie = pbm;
4205b208c27Smrg dt->_parent = pdt;
421a1b3c3f0Smrg #define PCOPY(x) dt->x = pdt->x
4225b208c27Smrg dt->_dmamap_create = pyro_dmamap_create;
423a1b3c3f0Smrg PCOPY(_dmamap_destroy);
4245b208c27Smrg dt->_dmamap_load = iommu_dvmamap_load;
425a1b3c3f0Smrg PCOPY(_dmamap_load_mbuf);
426a1b3c3f0Smrg PCOPY(_dmamap_load_uio);
4275b208c27Smrg dt->_dmamap_load_raw = iommu_dvmamap_load_raw;
4285b208c27Smrg dt->_dmamap_unload = iommu_dvmamap_unload;
4295b208c27Smrg dt->_dmamap_sync = iommu_dvmamap_sync;
4305b208c27Smrg dt->_dmamem_alloc = iommu_dvmamem_alloc;
4315b208c27Smrg dt->_dmamem_free = iommu_dvmamem_free;
432a1b3c3f0Smrg dt->_dmamem_map = iommu_dvmamem_map;
433a1b3c3f0Smrg dt->_dmamem_unmap = iommu_dvmamem_unmap;
434a1b3c3f0Smrg PCOPY(_dmamem_mmap);
435a1b3c3f0Smrg #undef PCOPY
4365b208c27Smrg return (dt);
4375b208c27Smrg }
4385b208c27Smrg
4395b208c27Smrg pci_chipset_tag_t
pyro_alloc_chipset(struct pyro_pbm * pbm,int node,pci_chipset_tag_t pc)4405b208c27Smrg pyro_alloc_chipset(struct pyro_pbm *pbm, int node, pci_chipset_tag_t pc)
4415b208c27Smrg {
4425b208c27Smrg pci_chipset_tag_t npc;
4435b208c27Smrg
444fc256c4aSthorpej npc = kmem_alloc(sizeof *npc, KM_SLEEP);
4455b208c27Smrg memcpy(npc, pc, sizeof *pc);
4465b208c27Smrg npc->cookie = pbm;
4475b208c27Smrg npc->rootnode = node;
448a1b3c3f0Smrg npc->spc_conf_read = pyro_conf_read;
449a1b3c3f0Smrg npc->spc_conf_write = pyro_conf_write;
450a1b3c3f0Smrg npc->spc_intr_map = pyro_intr_map;
451a1b3c3f0Smrg npc->spc_intr_establish = pyro_pci_intr_establish;
452a1b3c3f0Smrg npc->spc_find_ino = NULL;
4535b208c27Smrg return (npc);
4545b208c27Smrg }
4555b208c27Smrg
4565b208c27Smrg int
pyro_dmamap_create(bus_dma_tag_t t,bus_size_t size,int nsegments,bus_size_t maxsegsz,bus_size_t boundary,int flags,bus_dmamap_t * dmamp)457a1b3c3f0Smrg pyro_dmamap_create(bus_dma_tag_t t, bus_size_t size,
4585b208c27Smrg int nsegments, bus_size_t maxsegsz, bus_size_t boundary, int flags,
4595b208c27Smrg bus_dmamap_t *dmamp)
4605b208c27Smrg {
461a1b3c3f0Smrg struct pyro_pbm *pbm = t->_cookie;
462a1b3c3f0Smrg int error;
4635b208c27Smrg
464a1b3c3f0Smrg error = bus_dmamap_create(t->_parent, size, nsegments, maxsegsz,
465a1b3c3f0Smrg boundary, flags, dmamp);
466a1b3c3f0Smrg if (error == 0)
467a1b3c3f0Smrg (*dmamp)->_dm_cookie = &pbm->pp_sb;
468a1b3c3f0Smrg return error;
4695b208c27Smrg }
4705b208c27Smrg
4715b208c27Smrg int
pyro_bus_map(bus_space_tag_t t,bus_addr_t offset,bus_size_t size,int flags,vaddr_t unused,bus_space_handle_t * hp)472a1b3c3f0Smrg pyro_bus_map(bus_space_tag_t t, bus_addr_t offset,
473a1b3c3f0Smrg bus_size_t size, int flags, vaddr_t unused, bus_space_handle_t *hp)
4745b208c27Smrg {
4755b208c27Smrg struct pyro_pbm *pbm = t->cookie;
476a1b3c3f0Smrg struct pyro_softc *sc = pbm->pp_sc;
4775b208c27Smrg int i, ss;
4785b208c27Smrg
479a1b3c3f0Smrg DPRINTF(PDB_BUSMAP, ("pyro_bus_map: type %d off %qx sz %qx flags %d",
480a1b3c3f0Smrg t->type,
4815b208c27Smrg (unsigned long long)offset,
4825b208c27Smrg (unsigned long long)size,
4835b208c27Smrg flags));
4845b208c27Smrg
48516321ec1Smacallan /*
48616321ec1Smacallan * BUS_SPACE_MAP_PREFETCHABLE causes hard hangs on schizo, so weed it
487f93d6526Smaya * out for now until someone can verify whether it works on pyro
48816321ec1Smacallan */
48916321ec1Smacallan flags &= ~BUS_SPACE_MAP_PREFETCHABLE;
49016321ec1Smacallan
491a1b3c3f0Smrg ss = sparc_pci_childspace(t->type);
4925b208c27Smrg DPRINTF(PDB_BUSMAP, (" cspace %d", ss));
4935b208c27Smrg
4945b208c27Smrg if (t->parent == 0 || t->parent->sparc_bus_map == 0) {
4955b208c27Smrg printf("\n_pyro_bus_map: invalid parent");
4965b208c27Smrg return (EINVAL);
4975b208c27Smrg }
4985b208c27Smrg
4995b208c27Smrg for (i = 0; i < pbm->pp_nrange; i++) {
5005b208c27Smrg bus_addr_t paddr;
501a1b3c3f0Smrg struct pyro_range *pr = &pbm->pp_range[i];
5025b208c27Smrg
503a1b3c3f0Smrg if (((pr->cspace >> 24) & 0x03) != ss)
5045b208c27Smrg continue;
5055b208c27Smrg
506a1b3c3f0Smrg paddr = BUS_ADDR(pr->phys_hi, pr->phys_lo + offset);
507a1b3c3f0Smrg return ((*sc->sc_bustag->sparc_bus_map)(t, paddr, size,
508a1b3c3f0Smrg flags, 0, hp));
5095b208c27Smrg }
5105b208c27Smrg
5115b208c27Smrg return (EINVAL);
5125b208c27Smrg }
5135b208c27Smrg
5145b208c27Smrg paddr_t
pyro_bus_mmap(bus_space_tag_t t,bus_addr_t paddr,off_t off,int prot,int flags)515a1b3c3f0Smrg pyro_bus_mmap(bus_space_tag_t t, bus_addr_t paddr,
5165b208c27Smrg off_t off, int prot, int flags)
5175b208c27Smrg {
5185b208c27Smrg bus_addr_t offset = paddr;
5195b208c27Smrg struct pyro_pbm *pbm = t->cookie;
520a1b3c3f0Smrg struct pyro_softc *sc = pbm->pp_sc;
5215b208c27Smrg int i, ss;
5225b208c27Smrg
52316321ec1Smacallan /*
52416321ec1Smacallan * BUS_SPACE_MAP_PREFETCHABLE causes hard hangs on schizo, so weed it
525f93d6526Smaya * out for now until someone can verify whether it works on pyro
52616321ec1Smacallan */
52716321ec1Smacallan flags &= ~BUS_SPACE_MAP_PREFETCHABLE;
52816321ec1Smacallan
529a1b3c3f0Smrg ss = sparc_pci_childspace(t->type);
5305b208c27Smrg
531a1b3c3f0Smrg DPRINTF(PDB_BUSMAP, ("pyro_bus_mmap: prot %d flags %d pa %qx\n",
5325b208c27Smrg prot, flags, (unsigned long long)paddr));
5335b208c27Smrg
5345b208c27Smrg if (t->parent == 0 || t->parent->sparc_bus_mmap == 0) {
5355b208c27Smrg printf("\n_pyro_bus_mmap: invalid parent");
5365b208c27Smrg return (-1);
5375b208c27Smrg }
5385b208c27Smrg
5395b208c27Smrg for (i = 0; i < pbm->pp_nrange; i++) {
540a1b3c3f0Smrg struct pyro_range *pr = &pbm->pp_range[i];
5415b208c27Smrg
542a1b3c3f0Smrg if (((pr->cspace >> 24) & 0x03) != ss)
5435b208c27Smrg continue;
5445b208c27Smrg
545a1b3c3f0Smrg paddr = BUS_ADDR(pr->phys_hi, pr->phys_lo + offset);
546a1b3c3f0Smrg return (bus_space_mmap(sc->sc_bustag, paddr, off,
547a1b3c3f0Smrg prot, flags));
5485b208c27Smrg }
5495b208c27Smrg
5505b208c27Smrg return (-1);
5515b208c27Smrg }
5525b208c27Smrg
5535b208c27Smrg void *
pyro_intr_establish(bus_space_tag_t t,int ihandle,int level,int (* handler)(void *),void * arg,void (* fastvec)(void))554a1b3c3f0Smrg pyro_intr_establish(bus_space_tag_t t, int ihandle, int level,
555a1b3c3f0Smrg int (*handler)(void *), void *arg, void (*fastvec)(void) /* ignored */)
5565b208c27Smrg {
5575b208c27Smrg struct pyro_pbm *pbm = t->cookie;
5585b208c27Smrg struct pyro_softc *sc = pbm->pp_sc;
5595b208c27Smrg struct intrhand *ih = NULL;
5605b208c27Smrg volatile u_int64_t *intrmapptr = NULL, *intrclrptr = NULL;
5619117b6a1Smrg u_int64_t *imapbase, *iclrbase;
5625b208c27Smrg int ino;
5635b208c27Smrg
5645b208c27Smrg ino = INTINO(ihandle);
565ed7e2948Smrg DPRINTF(PDB_INTR, ("%s: ih %lx; level %d ino %#x", __func__, (u_long)ihandle, level, ino));
5665b208c27Smrg
5675b208c27Smrg if (level == IPL_NONE)
5685b208c27Smrg level = INTLEV(ihandle);
5695b208c27Smrg if (level == IPL_NONE) {
5705b208c27Smrg printf(": no IPL, setting IPL 2.\n");
5715b208c27Smrg level = 2;
5725b208c27Smrg }
5735b208c27Smrg
5749117b6a1Smrg imapbase = (uint64_t *)((uintptr_t)bus_space_vaddr(sc->sc_bustag, sc->sc_csrh) + 0x1000);
5759117b6a1Smrg iclrbase = (uint64_t *)((uintptr_t)bus_space_vaddr(sc->sc_bustag, sc->sc_csrh) + 0x1400);
5769117b6a1Smrg intrmapptr = &imapbase[ino];
5779117b6a1Smrg intrclrptr = &iclrbase[ino];
578ed7e2948Smrg DPRINTF(PDB_INTR, (" mapptr %p clrptr %p\n", intrmapptr, intrclrptr));
579ed7e2948Smrg
5805b208c27Smrg ino |= INTVEC(ihandle);
5815b208c27Smrg
5827820dc41Spalle ih = intrhand_alloc();
5835b208c27Smrg
584a1b3c3f0Smrg /* Register the map and clear intr registers */
585a1b3c3f0Smrg ih->ih_map = intrmapptr;
586a1b3c3f0Smrg ih->ih_clr = intrclrptr;
587a1b3c3f0Smrg
588ed7e2948Smrg ih->ih_ivec = ihandle;
589a1b3c3f0Smrg ih->ih_fun = handler;
590a1b3c3f0Smrg ih->ih_arg = arg;
591a1b3c3f0Smrg ih->ih_pil = level;
592a1b3c3f0Smrg ih->ih_number = ino;
593ed7e2948Smrg ih->ih_pending = 0;
594a1b3c3f0Smrg
595a1b3c3f0Smrg intr_establish(ih->ih_pil, level != IPL_VM, ih);
5965b208c27Smrg
5975b208c27Smrg if (intrmapptr != NULL) {
5989117b6a1Smrg u_int64_t imap;
5995b208c27Smrg
6009117b6a1Smrg imap = *intrmapptr;
6019117b6a1Smrg DPRINTF(PDB_INTR, ("%s: read intrmap = %016qx", __func__,
6029117b6a1Smrg (unsigned long long)imap));
6039117b6a1Smrg imap &= ~FIRE_INTRMAP_INT_CNTRL_NUM_MASK;
6049117b6a1Smrg imap |= FIRE_INTRMAP_INT_CNTRL_NUM0;
6059117b6a1Smrg DPRINTF(PDB_INTR, ("; set intr group intrmap = %016qx",
6069117b6a1Smrg (unsigned long long)imap));
6075b208c27Smrg if (sc->sc_oberon) {
6089117b6a1Smrg imap &= ~OBERON_INTRMAP_T_DESTID_MASK;
6099117b6a1Smrg imap |= CPU_JUPITERID <<
6105b208c27Smrg OBERON_INTRMAP_T_DESTID_SHIFT;
6115b208c27Smrg } else {
6129117b6a1Smrg imap &= ~FIRE_INTRMAP_T_JPID_MASK;
6139117b6a1Smrg imap |= CPU_UPAID << FIRE_INTRMAP_T_JPID_SHIFT;
6145b208c27Smrg }
6159117b6a1Smrg DPRINTF(PDB_INTR, ("; set cpuid num intrmap = %016qx",
6169117b6a1Smrg (unsigned long long)imap));
6179117b6a1Smrg imap |= INTMAP_V;
6189117b6a1Smrg *intrmapptr = imap;
6199117b6a1Smrg DPRINTF(PDB_INTR, ("; writing intrmap = %016qx",
6209117b6a1Smrg (unsigned long long)imap));
6219117b6a1Smrg imap = *intrmapptr;
6229117b6a1Smrg ih->ih_number |= imap & INTMAP_INR;
623ed7e2948Smrg DPRINTF(PDB_INTR, ("; reread intrmap = %016qx, "
624ed7e2948Smrg "set ih_number to %x\n",
625ed7e2948Smrg (unsigned long long)imap, ih->ih_number));
6265b208c27Smrg }
627a1b3c3f0Smrg if (intrclrptr) {
628a1b3c3f0Smrg /* set state to IDLE */
629a1b3c3f0Smrg *intrclrptr = 0;
630a1b3c3f0Smrg }
6315b208c27Smrg
6325b208c27Smrg return (ih);
6335b208c27Smrg }
6345b208c27Smrg
635a1b3c3f0Smrg static void *
pyro_pci_intr_establish(pci_chipset_tag_t pc,pci_intr_handle_t ih,int level,int (* func)(void *),void * arg)636a1b3c3f0Smrg pyro_pci_intr_establish(pci_chipset_tag_t pc, pci_intr_handle_t ih, int level,
637a1b3c3f0Smrg int (*func)(void *), void *arg)
638a1b3c3f0Smrg {
639a1b3c3f0Smrg void *cookie;
640a1b3c3f0Smrg struct pyro_pbm *pbm = (struct pyro_pbm *)pc->cookie;
641a1b3c3f0Smrg
6429117b6a1Smrg DPRINTF(PDB_INTR, ("%s: ih %lx; level %d\n", __func__, (u_long)ih, level));
643a1b3c3f0Smrg cookie = bus_intr_establish(pbm->pp_memt, ih, level, func, arg);
644a1b3c3f0Smrg
6459117b6a1Smrg DPRINTF(PDB_INTR, ("%s: returning handle %p\n", __func__, cookie));
646a1b3c3f0Smrg return (cookie);
647a1b3c3f0Smrg }
648