Home | History | Annotate | Line # | Download | only in dev
schizo.c revision 1.18
      1  1.18       mrg /*	$NetBSD: schizo.c,v 1.18 2011/03/20 20:43:34 mrg Exp $	*/
      2   1.1       mrg /*	$OpenBSD: schizo.c,v 1.55 2008/08/18 20:29:37 brad Exp $	*/
      3   1.1       mrg 
      4   1.1       mrg /*
      5   1.1       mrg  * Copyright (c) 2002 Jason L. Wright (jason (at) thought.net)
      6   1.1       mrg  * Copyright (c) 2003 Henric Jungheim
      7  1.17       mrg  * Copyright (c) 2008, 2009, 2010 Matthew R. Green
      8   1.1       mrg  * All rights reserved.
      9   1.1       mrg  *
     10   1.1       mrg  * Redistribution and use in source and binary forms, with or without
     11   1.1       mrg  * modification, are permitted provided that the following conditions
     12   1.1       mrg  * are met:
     13   1.1       mrg  * 1. Redistributions of source code must retain the above copyright
     14   1.1       mrg  *    notice, this list of conditions and the following disclaimer.
     15   1.1       mrg  * 2. Redistributions in binary form must reproduce the above copyright
     16   1.1       mrg  *    notice, this list of conditions and the following disclaimer in the
     17   1.1       mrg  *    documentation and/or other materials provided with the distribution.
     18   1.1       mrg  *
     19   1.1       mrg  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
     20   1.1       mrg  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
     21   1.1       mrg  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
     22   1.1       mrg  * DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT,
     23   1.1       mrg  * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
     24   1.1       mrg  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
     25   1.1       mrg  * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     26   1.1       mrg  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
     27   1.1       mrg  * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
     28   1.1       mrg  * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     29   1.1       mrg  * POSSIBILITY OF SUCH DAMAGE.
     30   1.1       mrg  */
     31   1.1       mrg 
     32   1.1       mrg #include <sys/param.h>
     33   1.1       mrg #include <sys/device.h>
     34   1.1       mrg #include <sys/errno.h>
     35   1.1       mrg #include <sys/extent.h>
     36   1.1       mrg #include <sys/malloc.h>
     37   1.1       mrg #include <sys/systm.h>
     38   1.1       mrg #include <sys/time.h>
     39   1.1       mrg #include <sys/reboot.h>
     40   1.1       mrg 
     41   1.1       mrg #define _SPARC_BUS_DMA_PRIVATE
     42   1.1       mrg #include <machine/bus.h>
     43   1.1       mrg #include <machine/autoconf.h>
     44   1.1       mrg #include <machine/psl.h>
     45   1.1       mrg 
     46   1.1       mrg #include <dev/pci/pcivar.h>
     47   1.1       mrg #include <dev/pci/pcireg.h>
     48   1.1       mrg 
     49   1.1       mrg #include <sparc64/dev/iommureg.h>
     50   1.1       mrg #include <sparc64/dev/iommuvar.h>
     51   1.1       mrg #include <sparc64/dev/schizoreg.h>
     52   1.1       mrg #include <sparc64/dev/schizovar.h>
     53   1.1       mrg #include <sparc64/sparc64/cache.h>
     54   1.1       mrg 
     55   1.1       mrg #ifdef DEBUG
     56   1.1       mrg #define SDB_PROM        0x01
     57   1.1       mrg #define SDB_BUSMAP      0x02
     58   1.1       mrg #define SDB_INTR        0x04
     59   1.2       mrg #define SDB_INTMAP      0x08
     60   1.2       mrg #define SDB_CONF        0x10
     61   1.7       mrg int schizo_debug = 0x0;
     62   1.1       mrg #define DPRINTF(l, s)   do { if (schizo_debug & l) printf s; } while (0)
     63   1.1       mrg #else
     64   1.1       mrg #define DPRINTF(l, s)
     65   1.1       mrg #endif
     66   1.1       mrg 
     67   1.1       mrg extern struct sparc_pci_chipset _sparc_pci_chipset;
     68   1.1       mrg 
     69   1.2       mrg static	int	schizo_match(struct device *, struct cfdata *, void *);
     70   1.2       mrg static	void	schizo_attach(struct device *, struct device *, void *);
     71   1.2       mrg static	int	schizo_print(void *aux, const char *p);
     72   1.2       mrg 
     73   1.2       mrg CFATTACH_DECL(schizo, sizeof(struct schizo_softc),
     74   1.2       mrg     schizo_match, schizo_attach, NULL, NULL);
     75   1.2       mrg 
     76   1.1       mrg void schizo_init_iommu(struct schizo_softc *, struct schizo_pbm *);
     77   1.1       mrg 
     78   1.1       mrg void schizo_set_intr(struct schizo_softc *, struct schizo_pbm *, int,
     79   1.2       mrg     int (*handler)(void *), void *, int, const char *);
     80   1.1       mrg int schizo_ue(void *);
     81   1.1       mrg int schizo_ce(void *);
     82   1.1       mrg int schizo_safari_error(void *);
     83   1.1       mrg int schizo_pci_error(void *);
     84   1.1       mrg 
     85   1.1       mrg pci_chipset_tag_t schizo_alloc_chipset(struct schizo_pbm *, int,
     86   1.1       mrg     pci_chipset_tag_t);
     87   1.1       mrg bus_space_tag_t schizo_alloc_mem_tag(struct schizo_pbm *);
     88   1.1       mrg bus_space_tag_t schizo_alloc_io_tag(struct schizo_pbm *);
     89   1.1       mrg bus_space_tag_t schizo_alloc_config_tag(struct schizo_pbm *);
     90   1.1       mrg bus_space_tag_t schizo_alloc_bus_tag(struct schizo_pbm *, const char *,
     91   1.2       mrg     int);
     92   1.1       mrg bus_dma_tag_t schizo_alloc_dma_tag(struct schizo_pbm *);
     93   1.1       mrg 
     94   1.1       mrg pcireg_t schizo_conf_read(pci_chipset_tag_t, pcitag_t, int);
     95   1.1       mrg void schizo_conf_write(pci_chipset_tag_t, pcitag_t, int, pcireg_t);
     96   1.1       mrg 
     97   1.2       mrg int schizo_bus_map(bus_space_tag_t t, bus_addr_t offset, bus_size_t size,
     98   1.2       mrg 	           int flags, vaddr_t unused, bus_space_handle_t *hp);
     99   1.2       mrg static paddr_t schizo_bus_mmap(bus_space_tag_t t, bus_addr_t paddr,
    100   1.2       mrg                                off_t off, int prot, int flags);
    101   1.2       mrg static void *schizo_intr_establish(bus_space_tag_t, int, int, int (*)(void *),
    102   1.2       mrg 	void *, void(*)(void));
    103  1.10       mrg static int schizo_pci_intr_map(struct pci_attach_args *, pci_intr_handle_t *);
    104   1.2       mrg static void *schizo_pci_intr_establish(pci_chipset_tag_t, pci_intr_handle_t,
    105   1.2       mrg                                        int, int (*)(void *), void *);
    106   1.4       mrg static int schizo_dmamap_create(bus_dma_tag_t, bus_size_t, int, bus_size_t,
    107   1.4       mrg 	bus_size_t, int, bus_dmamap_t *);
    108   1.1       mrg 
    109   1.1       mrg int
    110   1.2       mrg schizo_match(struct device *parent, struct cfdata *match, void *aux)
    111   1.1       mrg {
    112   1.1       mrg 	struct mainbus_attach_args *ma = aux;
    113   1.1       mrg 	char *str;
    114   1.1       mrg 
    115   1.1       mrg 	if (strcmp(ma->ma_name, "pci") != 0)
    116   1.1       mrg 		return (0);
    117   1.1       mrg 
    118   1.2       mrg 	str = prom_getpropstring(ma->ma_node, "model");
    119   1.1       mrg 	if (strcmp(str, "schizo") == 0)
    120   1.1       mrg 		return (1);
    121   1.1       mrg 
    122   1.2       mrg 	str = prom_getpropstring(ma->ma_node, "compatible");
    123   1.1       mrg 	if (strcmp(str, "pci108e,8001") == 0)
    124   1.1       mrg 		return (1);
    125   1.1       mrg 	if (strcmp(str, "pci108e,8002") == 0)		/* XMITS */
    126   1.1       mrg 		return (1);
    127   1.1       mrg 	if (strcmp(str, "pci108e,a801") == 0)		/* Tomatillo */
    128   1.1       mrg 		return (1);
    129   1.1       mrg 
    130   1.1       mrg 	return (0);
    131   1.1       mrg }
    132   1.1       mrg 
    133   1.1       mrg void
    134   1.1       mrg schizo_attach(struct device *parent, struct device *self, void *aux)
    135   1.1       mrg {
    136   1.1       mrg 	struct schizo_softc *sc = (struct schizo_softc *)self;
    137   1.1       mrg 	struct mainbus_attach_args *ma = aux;
    138  1.13       mrg 	struct schizo_pbm *pbm;
    139  1.15       mrg 	struct iommu_state *is;
    140  1.13       mrg 	struct pcibus_attach_args pba;
    141  1.13       mrg 	uint64_t reg, eccctrl;
    142  1.13       mrg 	int *busranges = NULL, nranges;
    143   1.1       mrg 	char *str;
    144  1.17       mrg 	bool no_sc;
    145   1.1       mrg 
    146  1.16       mrg 	aprint_normal(": addr %" PRIx64, ma->ma_reg[0].ur_paddr);
    147   1.2       mrg 	str = prom_getpropstring(ma->ma_node, "compatible");
    148   1.1       mrg 	if (strcmp(str, "pci108e,a801") == 0)
    149   1.1       mrg 		sc->sc_tomatillo = 1;
    150   1.1       mrg 
    151   1.1       mrg 	sc->sc_node = ma->ma_node;
    152   1.1       mrg 	sc->sc_dmat = ma->ma_dmatag;
    153   1.2       mrg 	sc->sc_bustag = ma->ma_bustag;
    154   1.1       mrg 
    155  1.13       mrg 	if (bus_space_map(sc->sc_bustag, ma->ma_reg[1].ur_paddr - 0x10000UL,
    156   1.2       mrg 	    sizeof(struct schizo_regs), 0,
    157   1.2       mrg 	    &sc->sc_ctrlh)) {
    158  1.16       mrg 		aprint_error(": failed to map registers\n");
    159   1.1       mrg 		return;
    160   1.1       mrg 	}
    161   1.1       mrg 
    162  1.10       mrg 	sc->sc_ign = INTIGN(ma->ma_upaid << INTMAP_IGN_SHIFT);
    163   1.6       mrg 
    164   1.1       mrg 	/* enable schizo ecc error interrupts */
    165   1.2       mrg 	eccctrl = schizo_read(sc, SCZ_ECCCTRL);
    166   1.2       mrg 	eccctrl |= SCZ_ECCCTRL_EE_INTEN |
    167   1.2       mrg 		   SCZ_ECCCTRL_UE_INTEN |
    168   1.2       mrg 		   SCZ_ECCCTRL_CE_INTEN;
    169   1.2       mrg 	schizo_write(sc, SCZ_ECCCTRL, eccctrl);
    170   1.1       mrg 
    171   1.1       mrg 	pbm = malloc(sizeof(*pbm), M_DEVBUF, M_NOWAIT | M_ZERO);
    172   1.1       mrg 	if (pbm == NULL)
    173   1.1       mrg 		panic("schizo: can't alloc schizo pbm");
    174   1.1       mrg 
    175   1.1       mrg 	pbm->sp_sc = sc;
    176   1.2       mrg 	pbm->sp_regt = sc->sc_bustag;
    177   1.1       mrg 
    178  1.13       mrg 	if ((ma->ma_reg[0].ur_paddr & 0x00700000) == 0x00600000)
    179   1.5       mrg 		pbm->sp_bus_a = 1;
    180   1.5       mrg 	else
    181   1.5       mrg 		pbm->sp_bus_a = 0;
    182   1.5       mrg 
    183  1.13       mrg 	/*
    184  1.13       mrg 	 * Map interrupt registers
    185  1.13       mrg 	 */
    186  1.13       mrg 	if (bus_space_map(sc->sc_bustag, ma->ma_reg[0].ur_paddr,
    187  1.13       mrg 			  ma->ma_reg[0].ur_len,
    188  1.13       mrg 			  BUS_SPACE_MAP_LINEAR, &pbm->sp_intrh)) {
    189  1.16       mrg 		aprint_error(": failed to interrupt map registers\n");
    190  1.13       mrg 		return;
    191  1.13       mrg 	}
    192  1.13       mrg 
    193   1.2       mrg 	if (prom_getprop(sc->sc_node, "ranges", sizeof(struct schizo_range),
    194   1.1       mrg 	    &pbm->sp_nrange, (void **)&pbm->sp_range))
    195   1.1       mrg 		panic("schizo: can't get ranges");
    196   1.1       mrg 
    197   1.2       mrg 	if (prom_getprop(sc->sc_node, "bus-range", sizeof(int), &nranges,
    198   1.1       mrg 	    (void **)&busranges))
    199   1.1       mrg 		panic("schizo: can't get bus-range");
    200   1.1       mrg 
    201  1.16       mrg 	aprint_normal(": \"%s\", version %d, ign %x, bus %c %d to %d\n",
    202   1.1       mrg 	    sc->sc_tomatillo ? "Tomatillo" : "Schizo",
    203   1.2       mrg 	    prom_getpropint(sc->sc_node, "version#", 0), sc->sc_ign,
    204   1.2       mrg 	    pbm->sp_bus_a ? 'A' : 'B', busranges[0], busranges[1]);
    205  1.16       mrg 	aprint_naive("\n");
    206   1.1       mrg 
    207   1.1       mrg 	if (bus_space_subregion(pbm->sp_regt, sc->sc_ctrlh,
    208   1.2       mrg 	    pbm->sp_bus_a ? offsetof(struct schizo_regs, pbm_a) :
    209   1.1       mrg 	    offsetof(struct schizo_regs, pbm_b),
    210   1.1       mrg 	    sizeof(struct schizo_pbm_regs),
    211   1.1       mrg 	    &pbm->sp_regh)) {
    212   1.1       mrg 		panic("schizo: unable to create PBM handle");
    213   1.1       mrg 	}
    214   1.1       mrg 
    215  1.15       mrg 	is = &pbm->sp_is;
    216  1.15       mrg 	pbm->sp_sb.sb_is = is;
    217  1.17       mrg 	no_sc = prom_getproplen(sc->sc_node, "no-streaming-cache") >= 0;
    218  1.17       mrg 	if (no_sc)
    219  1.17       mrg 		aprint_debug("%s: no streaming buffers\n", sc->sc_dv.dv_xname);
    220  1.17       mrg 	else {
    221  1.15       mrg 		vaddr_t va = (vaddr_t)&pbm->sp_flush[0x40];
    222  1.15       mrg 
    223  1.15       mrg 		/*
    224  1.15       mrg 		 * Initialize the strbuf_ctl.
    225  1.15       mrg 		 *
    226  1.15       mrg 		 * The flush sync buffer must be 64-byte aligned.
    227  1.15       mrg 		 */
    228  1.15       mrg 		is->is_sb[0] = &pbm->sp_sb;
    229  1.15       mrg 		is->is_sb[0]->sb_flush = (void *)(va & ~0x3f);
    230  1.15       mrg 
    231  1.15       mrg 		bus_space_subregion(pbm->sp_regt, pbm->sp_regh,
    232  1.15       mrg 			offsetof(struct schizo_pbm_regs, strbuf),
    233  1.15       mrg 			sizeof(struct iommu_strbuf), &is->is_sb[0]->sb_sb);
    234  1.15       mrg 	}
    235  1.15       mrg 
    236  1.16       mrg 	aprint_normal("%s: ", sc->sc_dv.dv_xname);
    237   1.1       mrg 	schizo_init_iommu(sc, pbm);
    238   1.1       mrg 
    239   1.1       mrg 	pbm->sp_memt = schizo_alloc_mem_tag(pbm);
    240   1.1       mrg 	pbm->sp_iot = schizo_alloc_io_tag(pbm);
    241   1.1       mrg 	pbm->sp_cfgt = schizo_alloc_config_tag(pbm);
    242   1.1       mrg 	pbm->sp_dmat = schizo_alloc_dma_tag(pbm);
    243   1.2       mrg 	pbm->sp_flags = (pbm->sp_memt ? PCI_FLAGS_MEM_ENABLED : 0) |
    244   1.2       mrg 		        (pbm->sp_iot ? PCI_FLAGS_IO_ENABLED : 0);
    245   1.1       mrg 
    246   1.1       mrg 	if (bus_space_map(pbm->sp_cfgt, 0, 0x1000000, 0, &pbm->sp_cfgh))
    247   1.1       mrg 		panic("schizo: could not map config space");
    248   1.1       mrg 
    249   1.1       mrg 	pbm->sp_pc = schizo_alloc_chipset(pbm, sc->sc_node,
    250   1.1       mrg 	    &_sparc_pci_chipset);
    251   1.3  nakayama 	pbm->sp_pc->spc_busmax = busranges[1];
    252   1.3  nakayama 	pbm->sp_pc->spc_busnode = malloc(sizeof(*pbm->sp_pc->spc_busnode),
    253   1.3  nakayama 	    M_DEVBUF, M_NOWAIT | M_ZERO);
    254   1.3  nakayama 	if (pbm->sp_pc->spc_busnode == NULL)
    255   1.3  nakayama 		panic("schizo: malloc busnode");
    256   1.1       mrg 
    257   1.1       mrg 	pba.pba_bus = busranges[0];
    258   1.1       mrg 	pba.pba_bridgetag = NULL;
    259   1.1       mrg 	pba.pba_pc = pbm->sp_pc;
    260   1.1       mrg 	pba.pba_flags = pbm->sp_flags;
    261   1.1       mrg 	pba.pba_dmat = pbm->sp_dmat;
    262   1.2       mrg 	pba.pba_dmat64 = NULL;	/* XXX */
    263   1.1       mrg 	pba.pba_memt = pbm->sp_memt;
    264   1.1       mrg 	pba.pba_iot = pbm->sp_iot;
    265   1.1       mrg 
    266   1.1       mrg 	free(busranges, M_DEVBUF);
    267   1.1       mrg 
    268   1.1       mrg 	schizo_pbm_write(pbm, SCZ_PCI_INTR_RETRY, 5);
    269   1.1       mrg 
    270   1.1       mrg 	/* clear out the bus errors */
    271   1.1       mrg 	schizo_pbm_write(pbm, SCZ_PCI_CTRL, schizo_pbm_read(pbm, SCZ_PCI_CTRL));
    272   1.1       mrg 	schizo_pbm_write(pbm, SCZ_PCI_AFSR, schizo_pbm_read(pbm, SCZ_PCI_AFSR));
    273   1.1       mrg 	schizo_cfg_write(pbm, PCI_COMMAND_STATUS_REG,
    274   1.1       mrg 	    schizo_cfg_read(pbm, PCI_COMMAND_STATUS_REG));
    275   1.1       mrg 
    276   1.1       mrg 	reg = schizo_pbm_read(pbm, SCZ_PCI_CTRL);
    277   1.1       mrg 	/* enable/disable error interrupts and arbiter */
    278   1.1       mrg 	reg |= SCZ_PCICTRL_EEN | SCZ_PCICTRL_MMU_INT | SCZ_PCICTRL_ARB;
    279   1.1       mrg 	reg &= ~SCZ_PCICTRL_SBH_INT;
    280   1.1       mrg 	schizo_pbm_write(pbm, SCZ_PCI_CTRL, reg);
    281   1.1       mrg 
    282   1.1       mrg 	reg = schizo_pbm_read(pbm, SCZ_PCI_DIAG);
    283   1.1       mrg 	reg &= ~(SCZ_PCIDIAG_D_RTRYARB | SCZ_PCIDIAG_D_RETRY |
    284   1.1       mrg 	    SCZ_PCIDIAG_D_INTSYNC);
    285   1.1       mrg 	schizo_pbm_write(pbm, SCZ_PCI_DIAG, reg);
    286   1.1       mrg 
    287   1.2       mrg 	if (pbm->sp_bus_a)
    288   1.1       mrg 		schizo_set_intr(sc, pbm, PIL_HIGH, schizo_pci_error,
    289   1.1       mrg 		   pbm, SCZ_PCIERR_A_INO, "pci_a");
    290   1.1       mrg 	else
    291   1.1       mrg 		schizo_set_intr(sc, pbm, PIL_HIGH, schizo_pci_error,
    292   1.1       mrg 		   pbm, SCZ_PCIERR_B_INO, "pci_b");
    293   1.1       mrg 
    294   1.1       mrg 	/* double mapped */
    295   1.1       mrg 	schizo_set_intr(sc, pbm, PIL_HIGH, schizo_ue, sc, SCZ_UE_INO,
    296   1.1       mrg 	    "ue");
    297   1.1       mrg 	schizo_set_intr(sc, pbm, PIL_HIGH, schizo_ce, sc, SCZ_CE_INO,
    298   1.1       mrg 	    "ce");
    299   1.1       mrg 	schizo_set_intr(sc, pbm, PIL_HIGH, schizo_safari_error, sc,
    300   1.1       mrg 	    SCZ_SERR_INO, "safari");
    301   1.1       mrg 
    302  1.17       mrg 	if (sc->sc_tomatillo) {
    303  1.17       mrg 		/*
    304  1.18       mrg 		 * Enable the IOCACHE.
    305  1.17       mrg 		 */
    306  1.17       mrg 		uint64_t iocache_csr;
    307  1.17       mrg 
    308  1.18       mrg 		iocache_csr = TOM_IOCACHE_CSR_WRT_PEN |
    309  1.18       mrg 			      (1 << TOM_IOCACHE_CSR_POFFSET_SHIFT) |
    310  1.18       mrg 			      TOM_IOCACHE_CSR_PEN_RDM |
    311  1.18       mrg 			      TOM_IOCACHE_CSR_PEN_ONE |
    312  1.18       mrg 			      TOM_IOCACHE_CSR_PEN_LINE;
    313  1.17       mrg 
    314  1.18       mrg 		schizo_pbm_write(pbm, SCZ_PCI_IOCACHE_CSR, iocache_csr);
    315  1.17       mrg 	}
    316  1.17       mrg 
    317   1.1       mrg 	config_found(&sc->sc_dv, &pba, schizo_print);
    318   1.1       mrg }
    319   1.1       mrg 
    320   1.1       mrg int
    321   1.1       mrg schizo_ue(void *vsc)
    322   1.1       mrg {
    323   1.1       mrg 	struct schizo_softc *sc = vsc;
    324   1.1       mrg 
    325   1.1       mrg 	panic("%s: uncorrectable error", sc->sc_dv.dv_xname);
    326   1.1       mrg 	return (1);
    327   1.1       mrg }
    328   1.1       mrg 
    329   1.1       mrg int
    330   1.1       mrg schizo_ce(void *vsc)
    331   1.1       mrg {
    332   1.1       mrg 	struct schizo_softc *sc = vsc;
    333   1.1       mrg 
    334   1.1       mrg 	panic("%s: correctable error", sc->sc_dv.dv_xname);
    335   1.1       mrg 	return (1);
    336   1.1       mrg }
    337   1.1       mrg 
    338   1.1       mrg int
    339   1.1       mrg schizo_pci_error(void *vpbm)
    340   1.1       mrg {
    341   1.1       mrg 	struct schizo_pbm *sp = vpbm;
    342   1.1       mrg 	struct schizo_softc *sc = sp->sp_sc;
    343   1.2       mrg 	u_int64_t afsr, afar, ctrl, tfar;
    344   1.1       mrg 	u_int32_t csr;
    345   1.2       mrg 	char bits[128];
    346   1.1       mrg 
    347   1.1       mrg 	afsr = schizo_pbm_read(sp, SCZ_PCI_AFSR);
    348   1.1       mrg 	afar = schizo_pbm_read(sp, SCZ_PCI_AFAR);
    349   1.1       mrg 	ctrl = schizo_pbm_read(sp, SCZ_PCI_CTRL);
    350   1.1       mrg 	csr = schizo_cfg_read(sp, PCI_COMMAND_STATUS_REG);
    351   1.1       mrg 
    352   1.1       mrg 	printf("%s: pci bus %c error\n", sc->sc_dv.dv_xname,
    353   1.1       mrg 	    sp->sp_bus_a ? 'A' : 'B');
    354   1.1       mrg 
    355   1.8  christos 	snprintb(bits, sizeof(bits), SCZ_PCIAFSR_BITS, afsr);
    356   1.8  christos 	printf("PCIAFSR=%s\n", bits);
    357  1.14  nakayama 	printf("PCIAFAR=%" PRIx64 "\n", afar);
    358   1.8  christos 	snprintb(bits, sizeof(bits), SCZ_PCICTRL_BITS, ctrl);
    359   1.8  christos 	printf("PCICTRL=%s\n", bits);
    360   1.2       mrg #ifdef PCI_COMMAND_STATUS_BITS
    361   1.8  christos 	snprintb(bits, sizeof(bits), PCI_COMMAND_STATUS_BITS, csr);
    362   1.8  christos 	printf("PCICSR=%s\n", bits);
    363   1.2       mrg #endif
    364   1.1       mrg 
    365   1.1       mrg 	if (ctrl & SCZ_PCICTRL_MMU_ERR) {
    366   1.1       mrg 		ctrl = schizo_pbm_read(sp, SCZ_PCI_IOMMU_CTRL);
    367  1.14  nakayama 		printf("IOMMUCTRL=%" PRIx64 "\n", ctrl);
    368   1.1       mrg 
    369   1.1       mrg 		if ((ctrl & TOM_IOMMU_ERR) == 0)
    370   1.1       mrg 			goto clear_error;
    371   1.1       mrg 
    372   1.1       mrg 		if (sc->sc_tomatillo) {
    373   1.1       mrg 			tfar = schizo_pbm_read(sp, TOM_PCI_IOMMU_TFAR);
    374  1.14  nakayama 			printf("IOMMUTFAR=%" PRIx64 "\n", tfar);
    375   1.1       mrg 		}
    376   1.1       mrg 
    377   1.1       mrg 		/* These are non-fatal if target abort was signalled. */
    378   1.1       mrg 		if ((ctrl & TOM_IOMMU_ERR_MASK) == TOM_IOMMU_INV_ERR ||
    379   1.1       mrg 		    ctrl & TOM_IOMMU_ILLTSBTBW_ERR ||
    380   1.1       mrg 		    ctrl & TOM_IOMMU_BADVA_ERR) {
    381   1.1       mrg 			if (csr & PCI_STATUS_TARGET_TARGET_ABORT) {
    382   1.1       mrg 				schizo_pbm_write(sp, SCZ_PCI_IOMMU_CTRL, ctrl);
    383   1.1       mrg 				goto clear_error;
    384   1.1       mrg 			}
    385   1.1       mrg 		}
    386   1.1       mrg 	}
    387   1.1       mrg 
    388   1.1       mrg 	panic("%s: fatal", sc->sc_dv.dv_xname);
    389   1.1       mrg 
    390   1.1       mrg  clear_error:
    391   1.1       mrg 	schizo_cfg_write(sp, PCI_COMMAND_STATUS_REG, csr);
    392   1.1       mrg 	schizo_pbm_write(sp, SCZ_PCI_CTRL, ctrl);
    393   1.1       mrg 	schizo_pbm_write(sp, SCZ_PCI_AFSR, afsr);
    394   1.1       mrg 	return (1);
    395   1.1       mrg }
    396   1.1       mrg 
    397   1.1       mrg int
    398   1.1       mrg schizo_safari_error(void *vsc)
    399   1.1       mrg {
    400   1.1       mrg 	struct schizo_softc *sc = vsc;
    401   1.1       mrg 
    402   1.1       mrg 	printf("%s: safari error\n", sc->sc_dv.dv_xname);
    403   1.1       mrg 
    404  1.14  nakayama 	printf("ERRLOG=%" PRIx64 "\n", schizo_read(sc, SCZ_SAFARI_ERRLOG));
    405  1.14  nakayama 	printf("UE_AFSR=%" PRIx64 "\n", schizo_read(sc, SCZ_UE_AFSR));
    406  1.14  nakayama 	printf("UE_AFAR=%" PRIx64 "\n", schizo_read(sc, SCZ_UE_AFAR));
    407  1.14  nakayama 	printf("CE_AFSR=%" PRIx64 "\n", schizo_read(sc, SCZ_CE_AFSR));
    408  1.14  nakayama 	printf("CE_AFAR=%" PRIx64 "\n", schizo_read(sc, SCZ_CE_AFAR));
    409   1.1       mrg 
    410   1.1       mrg 	panic("%s: fatal", sc->sc_dv.dv_xname);
    411   1.1       mrg 	return (1);
    412   1.1       mrg }
    413   1.1       mrg 
    414   1.1       mrg void
    415   1.1       mrg schizo_init_iommu(struct schizo_softc *sc, struct schizo_pbm *pbm)
    416   1.1       mrg {
    417   1.1       mrg 	struct iommu_state *is = &pbm->sp_is;
    418   1.1       mrg 	int *vdma = NULL, nitem, tsbsize = 7;
    419   1.1       mrg 	u_int32_t iobase = -1;
    420   1.1       mrg 	char *name;
    421   1.1       mrg 
    422   1.4       mrg 	/* punch in our copies */
    423   1.1       mrg 	is->is_bustag = pbm->sp_regt;
    424  1.15       mrg 	bus_space_subregion(is->is_bustag, pbm->sp_regh,
    425  1.15       mrg 		offsetof(struct schizo_pbm_regs, iommu),
    426  1.18       mrg 		sizeof(struct iommureg2),
    427  1.15       mrg 		&is->is_iommu);
    428   1.1       mrg 
    429   1.1       mrg 	/*
    430   1.1       mrg 	 * Separate the men from the boys.  If the `virtual-dma'
    431   1.1       mrg 	 * property exists, use it.
    432   1.1       mrg 	 */
    433   1.2       mrg 	if (!prom_getprop(sc->sc_node, "virtual-dma", sizeof(vdma), &nitem,
    434   1.1       mrg 	    (void **)&vdma)) {
    435   1.1       mrg 		/* Damn.  Gotta use these values. */
    436   1.1       mrg 		iobase = vdma[0];
    437   1.1       mrg #define	TSBCASE(x)	case 1 << ((x) + 23): tsbsize = (x); break
    438   1.1       mrg 		switch (vdma[1]) {
    439   1.1       mrg 			TSBCASE(1); TSBCASE(2); TSBCASE(3);
    440   1.1       mrg 			TSBCASE(4); TSBCASE(5); TSBCASE(6);
    441   1.1       mrg 		default:
    442   1.1       mrg 			printf("bogus tsb size %x, using 7\n", vdma[1]);
    443   1.1       mrg 			TSBCASE(7);
    444   1.1       mrg 		}
    445   1.1       mrg #undef TSBCASE
    446   1.2       mrg 		DPRINTF(SDB_BUSMAP, ("schizo_init_iommu: iobase=0x%x\n", iobase));
    447   1.1       mrg 		free(vdma, M_DEVBUF);
    448   1.1       mrg 	} else {
    449   1.2       mrg 		DPRINTF(SDB_BUSMAP, ("schizo_init_iommu: getprop failed, "
    450   1.1       mrg 		    "using iobase=0x%x, tsbsize=%d\n", iobase, tsbsize));
    451   1.1       mrg 	}
    452   1.1       mrg 
    453  1.15       mrg 	/* give us a nice name.. */
    454  1.15       mrg 	name = (char *)malloc(32, M_DEVBUF, M_NOWAIT);
    455  1.15       mrg 	if (name == NULL)
    456  1.15       mrg 		panic("couldn't malloc iommu name");
    457  1.15       mrg 	snprintf(name, 32, "%s dvma", sc->sc_dv.dv_xname);
    458  1.15       mrg 
    459   1.1       mrg 	iommu_init(name, is, tsbsize, iobase);
    460   1.1       mrg }
    461   1.1       mrg 
    462   1.1       mrg int
    463   1.1       mrg schizo_print(void *aux, const char *p)
    464   1.1       mrg {
    465   1.2       mrg 
    466   1.1       mrg 	if (p == NULL)
    467   1.1       mrg 		return (UNCONF);
    468   1.1       mrg 	return (QUIET);
    469   1.1       mrg }
    470   1.1       mrg 
    471   1.1       mrg pcireg_t
    472   1.1       mrg schizo_conf_read(pci_chipset_tag_t pc, pcitag_t tag, int reg)
    473   1.1       mrg {
    474   1.2       mrg 	struct schizo_pbm *sp = pc->cookie;
    475  1.11  nakayama 	pcireg_t val = (pcireg_t)~0;
    476   1.2       mrg 
    477   1.2       mrg 	DPRINTF(SDB_CONF, ("%s: tag %lx reg %x ", __func__, (long)tag, reg));
    478  1.11  nakayama 	if (PCITAG_NODE(tag) != -1)
    479  1.11  nakayama 		val = bus_space_read_4(sp->sp_cfgt, sp->sp_cfgh,
    480  1.11  nakayama 		    PCITAG_OFFSET(tag) + reg);
    481   1.2       mrg 	DPRINTF(SDB_CONF, (" returning %08x\n", (u_int)val));
    482   1.2       mrg 	return (val);
    483   1.1       mrg }
    484   1.1       mrg 
    485   1.1       mrg void
    486   1.1       mrg schizo_conf_write(pci_chipset_tag_t pc, pcitag_t tag, int reg, pcireg_t data)
    487   1.1       mrg {
    488   1.2       mrg 	struct schizo_pbm *sp = pc->cookie;
    489   1.2       mrg 
    490   1.2       mrg 	DPRINTF(SDB_CONF, ("%s: tag %lx; reg %x; data %x", __func__,
    491   1.2       mrg 		(long)tag, reg, (int)data));
    492  1.11  nakayama 
    493  1.11  nakayama 	/* If we don't know it, just punt it.  */
    494  1.11  nakayama 	if (PCITAG_NODE(tag) == -1) {
    495  1.11  nakayama 		DPRINTF(SDB_CONF, (" .. bad addr\n"));
    496  1.11  nakayama 		return;
    497  1.11  nakayama 	}
    498  1.11  nakayama 
    499   1.2       mrg         bus_space_write_4(sp->sp_cfgt, sp->sp_cfgh,
    500   1.1       mrg 	    PCITAG_OFFSET(tag) + reg, data);
    501   1.2       mrg 	DPRINTF(SDB_CONF, (" .. done\n"));
    502   1.1       mrg }
    503   1.1       mrg 
    504   1.1       mrg void
    505   1.1       mrg schizo_set_intr(struct schizo_softc *sc, struct schizo_pbm *pbm, int ipl,
    506   1.2       mrg     int (*handler)(void *), void *arg, int ino, const char *what)
    507   1.1       mrg {
    508   1.1       mrg 	struct intrhand *ih;
    509   1.2       mrg 	u_int64_t mapoff, clroff;
    510  1.13       mrg 	uintptr_t intrregs;
    511   1.2       mrg 
    512   1.6       mrg 	DPRINTF(SDB_INTR, ("%s: ino %x ign %x fn %p arg %p", __func__,
    513   1.6       mrg 	    ino, sc->sc_ign, handler, arg));
    514   1.6       mrg 
    515   1.2       mrg 	mapoff = offsetof(struct schizo_pbm_regs, imap[ino]);
    516   1.2       mrg 	clroff = offsetof(struct schizo_pbm_regs, iclr[ino]);
    517  1.12       mrg 	ino |= sc->sc_ign;
    518   1.1       mrg 
    519  1.14  nakayama 	DPRINTF(SDB_INTR, (" mapoff %" PRIx64 " clroff %" PRIx64 "\n",
    520  1.14  nakayama 	    mapoff, clroff));
    521   1.6       mrg 
    522   1.2       mrg 	ih = (struct intrhand *)
    523   1.2       mrg 		malloc(sizeof(struct intrhand), M_DEVBUF, M_NOWAIT);
    524   1.2       mrg 	if (ih == NULL)
    525   1.1       mrg 		return;
    526   1.2       mrg 	ih->ih_arg = arg;
    527  1.13       mrg 	intrregs = (uintptr_t)bus_space_vaddr(pbm->sp_regt, pbm->sp_intrh);
    528  1.14  nakayama 	ih->ih_map = (uint64_t *)(uintptr_t)(intrregs + mapoff);
    529  1.14  nakayama 	ih->ih_clr = (uint64_t *)(uintptr_t)(intrregs + clroff);
    530   1.2       mrg 	ih->ih_fun = handler;
    531   1.2       mrg 	ih->ih_pil = (1<<ipl);
    532   1.2       mrg 	ih->ih_number = INTVEC(schizo_pbm_read(pbm, mapoff));
    533   1.2       mrg 	intr_establish(ipl, ipl != IPL_VM, ih);
    534   1.2       mrg 
    535   1.2       mrg 	schizo_pbm_write(pbm, mapoff,
    536   1.2       mrg 	    ih->ih_number | INTMAP_V | (CPU_UPAID << INTMAP_TID_SHIFT));
    537   1.1       mrg }
    538   1.1       mrg 
    539   1.1       mrg bus_space_tag_t
    540   1.1       mrg schizo_alloc_mem_tag(struct schizo_pbm *sp)
    541   1.1       mrg {
    542   1.1       mrg 	return (schizo_alloc_bus_tag(sp, "mem",
    543   1.2       mrg 	    PCI_MEMORY_BUS_SPACE));
    544   1.1       mrg }
    545   1.1       mrg 
    546   1.1       mrg bus_space_tag_t
    547   1.1       mrg schizo_alloc_io_tag(struct schizo_pbm *sp)
    548   1.1       mrg {
    549   1.1       mrg 	return (schizo_alloc_bus_tag(sp, "io",
    550   1.2       mrg 	    PCI_IO_BUS_SPACE));
    551   1.1       mrg }
    552   1.1       mrg 
    553   1.1       mrg bus_space_tag_t
    554   1.1       mrg schizo_alloc_config_tag(struct schizo_pbm *sp)
    555   1.1       mrg {
    556   1.1       mrg 	return (schizo_alloc_bus_tag(sp, "cfg",
    557   1.2       mrg 	    PCI_CONFIG_BUS_SPACE));
    558   1.1       mrg }
    559   1.1       mrg 
    560   1.1       mrg bus_space_tag_t
    561   1.2       mrg schizo_alloc_bus_tag(struct schizo_pbm *pbm, const char *name, int type)
    562   1.1       mrg {
    563   1.1       mrg 	struct schizo_softc *sc = pbm->sp_sc;
    564   1.2       mrg 	bus_space_tag_t bt;
    565   1.1       mrg 
    566   1.2       mrg 	bt = (bus_space_tag_t) malloc(sizeof(struct sparc_bus_space_tag),
    567   1.2       mrg 		    M_DEVBUF, M_NOWAIT | M_ZERO);
    568   1.1       mrg 	if (bt == NULL)
    569   1.1       mrg 		panic("schizo: could not allocate bus tag");
    570   1.1       mrg 
    571   1.1       mrg 	bt->cookie = pbm;
    572   1.2       mrg 	bt->parent = sc->sc_bustag;
    573   1.2       mrg 	bt->type = type;
    574   1.1       mrg 	bt->sparc_bus_map = schizo_bus_map;
    575   1.1       mrg 	bt->sparc_bus_mmap = schizo_bus_mmap;
    576   1.1       mrg 	bt->sparc_intr_establish = schizo_intr_establish;
    577   1.1       mrg 	return (bt);
    578   1.1       mrg }
    579   1.1       mrg 
    580   1.1       mrg bus_dma_tag_t
    581   1.1       mrg schizo_alloc_dma_tag(struct schizo_pbm *pbm)
    582   1.1       mrg {
    583   1.1       mrg 	struct schizo_softc *sc = pbm->sp_sc;
    584   1.1       mrg 	bus_dma_tag_t dt, pdt = sc->sc_dmat;
    585   1.1       mrg 
    586   1.1       mrg 	dt = malloc(sizeof(*dt), M_DEVBUF, M_NOWAIT | M_ZERO);
    587   1.1       mrg 	if (dt == NULL)
    588   1.1       mrg 		panic("schizo: could not alloc dma tag");
    589   1.1       mrg 
    590   1.1       mrg 	dt->_cookie = pbm;
    591   1.1       mrg 	dt->_parent = pdt;
    592   1.2       mrg #define PCOPY(x)	dt->x = pdt->x
    593   1.4       mrg 	dt->_dmamap_create = schizo_dmamap_create;
    594   1.2       mrg 	PCOPY(_dmamap_destroy);
    595   1.2       mrg 	dt->_dmamap_load = iommu_dvmamap_load;
    596   1.2       mrg 	PCOPY(_dmamap_load_mbuf);
    597   1.2       mrg 	PCOPY(_dmamap_load_uio);
    598   1.2       mrg 	dt->_dmamap_load_raw = iommu_dvmamap_load_raw;
    599   1.2       mrg 	dt->_dmamap_unload = iommu_dvmamap_unload;
    600   1.2       mrg 	dt->_dmamap_sync = iommu_dvmamap_sync;
    601   1.2       mrg 	dt->_dmamem_alloc = iommu_dvmamem_alloc;
    602   1.2       mrg 	dt->_dmamem_free = iommu_dvmamem_free;
    603   1.2       mrg 	dt->_dmamem_map = iommu_dvmamem_map;
    604   1.2       mrg 	dt->_dmamem_unmap = iommu_dvmamem_unmap;
    605   1.2       mrg 	PCOPY(_dmamem_mmap);
    606   1.2       mrg #undef	PCOPY
    607   1.1       mrg 	return (dt);
    608   1.1       mrg }
    609   1.1       mrg 
    610   1.1       mrg pci_chipset_tag_t
    611   1.1       mrg schizo_alloc_chipset(struct schizo_pbm *pbm, int node, pci_chipset_tag_t pc)
    612   1.1       mrg {
    613   1.1       mrg 	pci_chipset_tag_t npc;
    614   1.1       mrg 
    615   1.1       mrg 	npc = malloc(sizeof *npc, M_DEVBUF, M_NOWAIT);
    616   1.1       mrg 	if (npc == NULL)
    617   1.1       mrg 		panic("schizo: could not allocate pci_chipset_tag_t");
    618   1.1       mrg 	memcpy(npc, pc, sizeof *pc);
    619   1.1       mrg 	npc->cookie = pbm;
    620   1.1       mrg 	npc->rootnode = node;
    621   1.2       mrg 	npc->spc_conf_read = schizo_conf_read;
    622   1.2       mrg 	npc->spc_conf_write = schizo_conf_write;
    623  1.10       mrg 	npc->spc_intr_map = schizo_pci_intr_map;
    624   1.2       mrg 	npc->spc_intr_establish = schizo_pci_intr_establish;
    625  1.17       mrg 	npc->spc_find_ino = NULL;
    626   1.1       mrg 	return (npc);
    627   1.1       mrg }
    628   1.1       mrg 
    629   1.1       mrg int
    630   1.2       mrg schizo_dmamap_create(bus_dma_tag_t t, bus_size_t size,
    631   1.1       mrg     int nsegments, bus_size_t maxsegsz, bus_size_t boundary, int flags,
    632   1.1       mrg     bus_dmamap_t *dmamp)
    633   1.1       mrg {
    634   1.2       mrg 	struct schizo_pbm *pbm = t->_cookie;
    635   1.4       mrg 	int error;
    636   1.1       mrg 
    637   1.4       mrg 	error = bus_dmamap_create(t->_parent, size, nsegments, maxsegsz,
    638   1.4       mrg 				  boundary, flags, dmamp);
    639   1.4       mrg 	if (error == 0)
    640   1.4       mrg 		(*dmamp)->_dm_cookie = &pbm->sp_sb;
    641   1.4       mrg 	return error;
    642   1.1       mrg }
    643   1.2       mrg 
    644   1.2       mrg static struct schizo_range *
    645   1.2       mrg get_schizorange(struct schizo_pbm *pbm, int ss)
    646   1.2       mrg {
    647   1.2       mrg 	int i;
    648   1.2       mrg 
    649   1.2       mrg 	for (i = 0; i < pbm->sp_nrange; i++) {
    650   1.2       mrg 		if (((pbm->sp_range[i].cspace >> 24) & 0x03) == ss)
    651   1.2       mrg 			return (&pbm->sp_range[i]);
    652   1.2       mrg 	}
    653   1.2       mrg 	/* not found */
    654   1.2       mrg 	return (NULL);
    655   1.2       mrg }
    656   1.1       mrg 
    657   1.1       mrg int
    658   1.2       mrg schizo_bus_map(bus_space_tag_t t, bus_addr_t offset, bus_size_t size,
    659   1.2       mrg 	       int flags, vaddr_t unused, bus_space_handle_t *hp)
    660   1.1       mrg {
    661   1.2       mrg 	bus_addr_t paddr;
    662   1.1       mrg 	struct schizo_pbm *pbm = t->cookie;
    663   1.2       mrg 	struct schizo_softc *sc = pbm->sp_sc;
    664   1.2       mrg 	struct schizo_range *sr;
    665   1.2       mrg 	int ss;
    666   1.1       mrg 
    667   1.1       mrg 	DPRINTF(SDB_BUSMAP, ("schizo_bus_map: type %d off %qx sz %qx flags %d",
    668   1.2       mrg 	    t->type,
    669   1.1       mrg 	    (unsigned long long)offset,
    670   1.1       mrg 	    (unsigned long long)size,
    671   1.1       mrg 	    flags));
    672   1.1       mrg 
    673   1.2       mrg 	ss = sparc_pci_childspace(t->type);
    674   1.2       mrg 	DPRINTF(SDB_BUSMAP, (" cspace %d\n", ss));
    675   1.1       mrg 
    676   1.2       mrg 	sr = get_schizorange(pbm, ss);
    677   1.2       mrg 	if (sr != NULL) {
    678   1.2       mrg 		paddr = BUS_ADDR(sr->phys_hi, sr->phys_lo + offset);
    679   1.2       mrg 		DPRINTF(SDB_BUSMAP, ("%s: mapping paddr "
    680   1.2       mrg 				     "space %lx offset %lx paddr %qx\n",
    681   1.2       mrg 			       __func__, (long)ss, (long)offset,
    682   1.2       mrg 			       (unsigned long long)paddr));
    683   1.2       mrg 		return ((*sc->sc_bustag->sparc_bus_map)(t, paddr, size,
    684   1.2       mrg 			flags, 0, hp));
    685   1.1       mrg 	}
    686   1.2       mrg 	DPRINTF(SDB_BUSMAP, ("%s: FAILED\n", __func__));
    687   1.1       mrg 	return (EINVAL);
    688   1.1       mrg }
    689   1.1       mrg 
    690   1.2       mrg static paddr_t
    691   1.2       mrg schizo_bus_mmap(bus_space_tag_t t, bus_addr_t paddr, off_t off, int prot,
    692   1.2       mrg 	int flags)
    693   1.1       mrg {
    694   1.1       mrg 	bus_addr_t offset = paddr;
    695   1.1       mrg 	struct schizo_pbm *pbm = t->cookie;
    696   1.2       mrg 	struct schizo_softc *sc = pbm->sp_sc;
    697   1.2       mrg 	struct schizo_range *sr;
    698   1.2       mrg 	int ss;
    699   1.1       mrg 
    700   1.2       mrg 	ss = sparc_pci_childspace(t->type);
    701   1.1       mrg 
    702   1.1       mrg 	DPRINTF(SDB_BUSMAP, ("schizo_bus_mmap: prot %d flags %d pa %qx\n",
    703   1.1       mrg 	    prot, flags, (unsigned long long)paddr));
    704   1.1       mrg 
    705   1.2       mrg 	sr = get_schizorange(pbm, ss);
    706   1.2       mrg 	if (sr != NULL) {
    707   1.2       mrg 		paddr = BUS_ADDR(sr->phys_hi, sr->phys_lo + offset);
    708   1.2       mrg 		DPRINTF(SDB_BUSMAP, ("%s: mapping paddr "
    709   1.2       mrg 				     "space %lx offset %lx paddr %qx\n",
    710   1.2       mrg 			       __func__, (long)ss, (long)offset,
    711   1.2       mrg 			       (unsigned long long)paddr));
    712   1.2       mrg 		return (bus_space_mmap(sc->sc_bustag, paddr, off,
    713   1.2       mrg 				       prot, flags));
    714   1.1       mrg 	}
    715   1.2       mrg 	DPRINTF(SDB_BUSMAP, ("%s: FAILED\n", __func__));
    716   1.1       mrg 	return (-1);
    717   1.1       mrg }
    718   1.1       mrg 
    719  1.10       mrg /*
    720  1.12       mrg  * Set the IGN for this schizo into the handle.
    721  1.10       mrg  */
    722  1.10       mrg int
    723  1.10       mrg schizo_pci_intr_map(struct pci_attach_args *pa, pci_intr_handle_t *ihp)
    724  1.10       mrg {
    725  1.10       mrg 	struct schizo_pbm *pbm = pa->pa_pc->cookie;
    726  1.10       mrg 	struct schizo_softc *sc = pbm->sp_sc;
    727  1.10       mrg 
    728  1.12       mrg 	*ihp |= sc->sc_ign;
    729  1.10       mrg 	DPRINTF(SDB_INTMAP, ("returning IGN adjusted to %x\n", *ihp));
    730  1.10       mrg 	return (0);
    731  1.10       mrg }
    732  1.10       mrg 
    733   1.2       mrg static void *
    734   1.2       mrg schizo_intr_establish(bus_space_tag_t t, int ihandle, int level,
    735   1.2       mrg 	int (*handler)(void *), void *arg, void (*fastvec)(void) /* ignored */)
    736   1.1       mrg {
    737   1.1       mrg 	struct schizo_pbm *pbm = t->cookie;
    738   1.1       mrg 	struct intrhand *ih = NULL;
    739   1.7       mrg 	uint64_t mapoff, clroff;
    740  1.13       mrg 	uintptr_t intrregs;
    741   1.7       mrg 	volatile uint64_t *intrmapptr = NULL, *intrclrptr = NULL;
    742   1.1       mrg 	int ino;
    743   1.5       mrg 	long vec;
    744   1.1       mrg 
    745   1.1       mrg 	vec = INTVEC(ihandle);
    746   1.1       mrg 	ino = INTINO(vec);
    747   1.1       mrg 
    748   1.5       mrg 	ih = malloc(sizeof *ih, M_DEVBUF, M_NOWAIT);
    749   1.5       mrg 	if (ih == NULL)
    750   1.5       mrg 		return (NULL);
    751   1.5       mrg 
    752   1.6       mrg 	DPRINTF(SDB_INTR, ("\n%s: ihandle %d level %d fn %p arg %p\n", __func__,
    753   1.2       mrg 	    ihandle, level, handler, arg));
    754   1.2       mrg 
    755   1.1       mrg 	if (level == IPL_NONE)
    756   1.1       mrg 		level = INTLEV(vec);
    757   1.1       mrg 	if (level == IPL_NONE) {
    758   1.1       mrg 		printf(": no IPL, setting IPL 2.\n");
    759   1.1       mrg 		level = 2;
    760   1.1       mrg 	}
    761   1.1       mrg 
    762   1.5       mrg 	mapoff = offsetof(struct schizo_pbm_regs, imap[ino]);
    763   1.5       mrg 	clroff = offsetof(struct schizo_pbm_regs, iclr[ino]);
    764   1.1       mrg 
    765  1.14  nakayama 	DPRINTF(SDB_INTR, ("%s: intr %x: %p mapoff %" PRIx64 " clroff %"
    766  1.14  nakayama 	    PRIx64 "\n", __func__, ino, intrlev[ino], mapoff, clroff));
    767   1.6       mrg 
    768  1.13       mrg 	intrregs = (uintptr_t)bus_space_vaddr(pbm->sp_regt, pbm->sp_intrh);
    769  1.14  nakayama 	intrmapptr = (uint64_t *)(uintptr_t)(intrregs + mapoff);
    770  1.14  nakayama 	intrclrptr = (uint64_t *)(uintptr_t)(intrregs + clroff);
    771   1.6       mrg 
    772   1.5       mrg 	if (INTIGN(vec) == 0)
    773  1.13       mrg 		ino |= schizo_pbm_readintr(pbm, mapoff) & INTMAP_IGN;
    774   1.5       mrg 	else
    775   1.5       mrg 		ino |= vec & INTMAP_IGN;
    776   1.1       mrg 
    777   1.5       mrg 	/* Register the map and clear intr registers */
    778   1.2       mrg 	ih->ih_map = intrmapptr;
    779   1.2       mrg 	ih->ih_clr = intrclrptr;
    780   1.2       mrg 
    781   1.2       mrg 	ih->ih_fun = handler;
    782   1.2       mrg 	ih->ih_arg = arg;
    783   1.2       mrg 	ih->ih_pil = level;
    784   1.6       mrg 	ih->ih_number = ino;
    785   1.2       mrg 
    786   1.5       mrg 	DPRINTF(SDB_INTR, (
    787   1.6       mrg 	    "; installing handler %p arg %p with inr %x pil %u\n",
    788   1.6       mrg 	    handler, arg, ino, (u_int)ih->ih_pil));
    789   1.5       mrg 
    790   1.2       mrg 	intr_establish(ih->ih_pil, level != IPL_VM, ih);
    791   1.1       mrg 
    792   1.5       mrg 	/*
    793   1.5       mrg 	 * Enable the interrupt now we have the handler installed.
    794   1.5       mrg 	 * Read the current value as we can't change it besides the
    795   1.5       mrg 	 * valid bit so so make sure only this bit is changed.
    796   1.5       mrg 	 */
    797   1.5       mrg 	if (intrmapptr) {
    798   1.2       mrg 		u_int64_t imap;
    799   1.1       mrg 
    800  1.13       mrg 		imap = schizo_pbm_readintr(pbm, mapoff);
    801   1.5       mrg 		DPRINTF(SDB_INTR, ("; read intrmap = %016qx",
    802   1.5       mrg 			(unsigned long long)imap));
    803   1.2       mrg 		imap |= INTMAP_V;
    804   1.5       mrg 		DPRINTF(SDB_INTR, ("; addr of intrmapptr = %p", intrmapptr));
    805   1.5       mrg 		DPRINTF(SDB_INTR, ("; writing intrmap = %016qx\n",
    806   1.5       mrg 			(unsigned long long)imap));
    807  1.13       mrg 		schizo_pbm_writeintr(pbm, mapoff, imap);
    808  1.13       mrg 		imap = schizo_pbm_readintr(pbm, mapoff);
    809   1.5       mrg 		DPRINTF(SDB_INTR, ("; reread intrmap = %016qx",
    810   1.5       mrg 			(unsigned long long)imap));
    811   1.2       mrg 		ih->ih_number |= imap & INTMAP_INR;
    812   1.1       mrg 	}
    813   1.5       mrg  	if (intrclrptr) {
    814   1.5       mrg  		/* set state to IDLE */
    815  1.13       mrg 		schizo_pbm_writeintr(pbm, clroff, 0);
    816   1.5       mrg  	}
    817   1.1       mrg 
    818   1.1       mrg 	return (ih);
    819   1.1       mrg }
    820   1.1       mrg 
    821   1.2       mrg static void *
    822   1.2       mrg schizo_pci_intr_establish(pci_chipset_tag_t pc, pci_intr_handle_t ih, int level,
    823   1.2       mrg 	int (*func)(void *), void *arg)
    824   1.2       mrg {
    825   1.2       mrg 	void *cookie;
    826   1.2       mrg 	struct schizo_pbm *pbm = (struct schizo_pbm *)pc->cookie;
    827   1.2       mrg 
    828   1.9       mrg 	DPRINTF(SDB_INTR, ("%s: ih %lx; level %d", __func__, (u_long)ih, level));
    829   1.2       mrg 	cookie = bus_intr_establish(pbm->sp_memt, ih, level, func, arg);
    830   1.2       mrg 
    831   1.2       mrg 	DPRINTF(SDB_INTR, ("; returning handle %p\n", cookie));
    832   1.2       mrg 	return (cookie);
    833   1.2       mrg }
    834