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