xref: /netbsd-src/sys/arch/sparc64/dev/schizo.c (revision 7f21db1c0118155e0dd40b75182e30c589d9f63e)
1 /*	$NetBSD: schizo.c,v 1.13 2010/02/06 00:23:30 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@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 = 0x0;
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_iommu(struct schizo_softc *, struct schizo_pbm *);
76 
77 void schizo_set_intr(struct schizo_softc *, struct schizo_pbm *, int,
78     int (*handler)(void *), void *, int, const char *);
79 int schizo_ue(void *);
80 int schizo_ce(void *);
81 int schizo_safari_error(void *);
82 int schizo_pci_error(void *);
83 
84 pci_chipset_tag_t schizo_alloc_chipset(struct schizo_pbm *, int,
85     pci_chipset_tag_t);
86 bus_space_tag_t schizo_alloc_mem_tag(struct schizo_pbm *);
87 bus_space_tag_t schizo_alloc_io_tag(struct schizo_pbm *);
88 bus_space_tag_t schizo_alloc_config_tag(struct schizo_pbm *);
89 bus_space_tag_t schizo_alloc_bus_tag(struct schizo_pbm *, const char *,
90     int);
91 bus_dma_tag_t schizo_alloc_dma_tag(struct schizo_pbm *);
92 
93 pcireg_t schizo_conf_read(pci_chipset_tag_t, pcitag_t, int);
94 void schizo_conf_write(pci_chipset_tag_t, pcitag_t, int, pcireg_t);
95 
96 int schizo_bus_map(bus_space_tag_t t, bus_addr_t offset, bus_size_t size,
97 	           int flags, vaddr_t unused, bus_space_handle_t *hp);
98 static paddr_t schizo_bus_mmap(bus_space_tag_t t, bus_addr_t paddr,
99                                off_t off, int prot, int flags);
100 static void *schizo_intr_establish(bus_space_tag_t, int, int, int (*)(void *),
101 	void *, void(*)(void));
102 static int schizo_pci_intr_map(struct pci_attach_args *, pci_intr_handle_t *);
103 static void *schizo_pci_intr_establish(pci_chipset_tag_t, pci_intr_handle_t,
104                                        int, int (*)(void *), void *);
105 static int schizo_pci_find_ino(struct pci_attach_args *, pci_intr_handle_t *);
106 static int schizo_dmamap_create(bus_dma_tag_t, bus_size_t, int, bus_size_t,
107 	bus_size_t, int, bus_dmamap_t *);
108 
109 int
110 schizo_match(struct device *parent, struct cfdata *match, void *aux)
111 {
112 	struct mainbus_attach_args *ma = aux;
113 	char *str;
114 
115 	if (strcmp(ma->ma_name, "pci") != 0)
116 		return (0);
117 
118 	str = prom_getpropstring(ma->ma_node, "model");
119 	if (strcmp(str, "schizo") == 0)
120 		return (1);
121 
122 	str = prom_getpropstring(ma->ma_node, "compatible");
123 	if (strcmp(str, "pci108e,8001") == 0)
124 		return (1);
125 	if (strcmp(str, "pci108e,8002") == 0)		/* XMITS */
126 		return (1);
127 	if (strcmp(str, "pci108e,a801") == 0)		/* Tomatillo */
128 		return (1);
129 
130 	return (0);
131 }
132 
133 void
134 schizo_attach(struct device *parent, struct device *self, void *aux)
135 {
136 	struct schizo_softc *sc = (struct schizo_softc *)self;
137 	struct mainbus_attach_args *ma = aux;
138 	struct schizo_pbm *pbm;
139 	struct pcibus_attach_args pba;
140 	uint64_t reg, eccctrl;
141 	int *busranges = NULL, nranges;
142 	char *str;
143 
144 	printf(": addr %lx", ma->ma_reg[0].ur_paddr);
145 	str = prom_getpropstring(ma->ma_node, "compatible");
146 	if (strcmp(str, "pci108e,a801") == 0)
147 		sc->sc_tomatillo = 1;
148 
149 	sc->sc_node = ma->ma_node;
150 	sc->sc_dmat = ma->ma_dmatag;
151 	sc->sc_bustag = ma->ma_bustag;
152 
153 	if (bus_space_map(sc->sc_bustag, ma->ma_reg[1].ur_paddr - 0x10000UL,
154 	    sizeof(struct schizo_regs), 0,
155 	    &sc->sc_ctrlh)) {
156 		printf(": failed to map registers\n");
157 		return;
158 	}
159 
160 	sc->sc_ign = INTIGN(ma->ma_upaid << INTMAP_IGN_SHIFT);
161 
162 	/* enable schizo ecc error interrupts */
163 	eccctrl = schizo_read(sc, SCZ_ECCCTRL);
164 	eccctrl |= SCZ_ECCCTRL_EE_INTEN |
165 		   SCZ_ECCCTRL_UE_INTEN |
166 		   SCZ_ECCCTRL_CE_INTEN;
167 	schizo_write(sc, SCZ_ECCCTRL, eccctrl);
168 
169 	pbm = malloc(sizeof(*pbm), M_DEVBUF, M_NOWAIT | M_ZERO);
170 	if (pbm == NULL)
171 		panic("schizo: can't alloc schizo pbm");
172 
173 	pbm->sp_sc = sc;
174 	pbm->sp_regt = sc->sc_bustag;
175 
176 	if ((ma->ma_reg[0].ur_paddr & 0x00700000) == 0x00600000)
177 		pbm->sp_bus_a = 1;
178 	else
179 		pbm->sp_bus_a = 0;
180 
181 	/*
182 	 * Map interrupt registers
183 	 */
184 	if (bus_space_map(sc->sc_bustag, ma->ma_reg[0].ur_paddr,
185 			  ma->ma_reg[0].ur_len,
186 			  BUS_SPACE_MAP_LINEAR, &pbm->sp_intrh)) {
187 		printf(": failed to interrupt map registers\n");
188 		return;
189 	}
190 
191 	if (prom_getprop(sc->sc_node, "ranges", sizeof(struct schizo_range),
192 	    &pbm->sp_nrange, (void **)&pbm->sp_range))
193 		panic("schizo: can't get ranges");
194 
195 	if (prom_getprop(sc->sc_node, "bus-range", sizeof(int), &nranges,
196 	    (void **)&busranges))
197 		panic("schizo: can't get bus-range");
198 
199 	printf(": \"%s\", version %d, ign %x, bus %c %d to %d\n",
200 	    sc->sc_tomatillo ? "Tomatillo" : "Schizo",
201 	    prom_getpropint(sc->sc_node, "version#", 0), sc->sc_ign,
202 	    pbm->sp_bus_a ? 'A' : 'B', busranges[0], busranges[1]);
203 
204 	if (bus_space_subregion(pbm->sp_regt, sc->sc_ctrlh,
205 	    pbm->sp_bus_a ? offsetof(struct schizo_regs, pbm_a) :
206 	    offsetof(struct schizo_regs, pbm_b),
207 	    sizeof(struct schizo_pbm_regs),
208 	    &pbm->sp_regh)) {
209 		panic("schizo: unable to create PBM handle");
210 	}
211 
212 	printf("%s: ", sc->sc_dv.dv_xname);
213 	schizo_init_iommu(sc, pbm);
214 
215 	pbm->sp_memt = schizo_alloc_mem_tag(pbm);
216 	pbm->sp_iot = schizo_alloc_io_tag(pbm);
217 	pbm->sp_cfgt = schizo_alloc_config_tag(pbm);
218 	pbm->sp_dmat = schizo_alloc_dma_tag(pbm);
219 	pbm->sp_flags = (pbm->sp_memt ? PCI_FLAGS_MEM_ENABLED : 0) |
220 		        (pbm->sp_iot ? PCI_FLAGS_IO_ENABLED : 0);
221 
222 	if (bus_space_map(pbm->sp_cfgt, 0, 0x1000000, 0, &pbm->sp_cfgh))
223 		panic("schizo: could not map config space");
224 
225 	pbm->sp_pc = schizo_alloc_chipset(pbm, sc->sc_node,
226 	    &_sparc_pci_chipset);
227 	pbm->sp_pc->spc_busmax = busranges[1];
228 	pbm->sp_pc->spc_busnode = malloc(sizeof(*pbm->sp_pc->spc_busnode),
229 	    M_DEVBUF, M_NOWAIT | M_ZERO);
230 	if (pbm->sp_pc->spc_busnode == NULL)
231 		panic("schizo: malloc busnode");
232 
233 	pba.pba_bus = busranges[0];
234 	pba.pba_bridgetag = NULL;
235 	pba.pba_pc = pbm->sp_pc;
236 	pba.pba_flags = pbm->sp_flags;
237 	pba.pba_dmat = pbm->sp_dmat;
238 	pba.pba_dmat64 = NULL;	/* XXX */
239 	pba.pba_memt = pbm->sp_memt;
240 	pba.pba_iot = pbm->sp_iot;
241 
242 	free(busranges, M_DEVBUF);
243 
244 	schizo_pbm_write(pbm, SCZ_PCI_INTR_RETRY, 5);
245 
246 	/* clear out the bus errors */
247 	schizo_pbm_write(pbm, SCZ_PCI_CTRL, schizo_pbm_read(pbm, SCZ_PCI_CTRL));
248 	schizo_pbm_write(pbm, SCZ_PCI_AFSR, schizo_pbm_read(pbm, SCZ_PCI_AFSR));
249 	schizo_cfg_write(pbm, PCI_COMMAND_STATUS_REG,
250 	    schizo_cfg_read(pbm, PCI_COMMAND_STATUS_REG));
251 
252 	reg = schizo_pbm_read(pbm, SCZ_PCI_CTRL);
253 	/* enable/disable error interrupts and arbiter */
254 	reg |= SCZ_PCICTRL_EEN | SCZ_PCICTRL_MMU_INT | SCZ_PCICTRL_ARB;
255 	reg &= ~SCZ_PCICTRL_SBH_INT;
256 	schizo_pbm_write(pbm, SCZ_PCI_CTRL, reg);
257 
258 	reg = schizo_pbm_read(pbm, SCZ_PCI_DIAG);
259 	reg &= ~(SCZ_PCIDIAG_D_RTRYARB | SCZ_PCIDIAG_D_RETRY |
260 	    SCZ_PCIDIAG_D_INTSYNC);
261 	schizo_pbm_write(pbm, SCZ_PCI_DIAG, reg);
262 
263 	if (pbm->sp_bus_a)
264 		schizo_set_intr(sc, pbm, PIL_HIGH, schizo_pci_error,
265 		   pbm, SCZ_PCIERR_A_INO, "pci_a");
266 	else
267 		schizo_set_intr(sc, pbm, PIL_HIGH, schizo_pci_error,
268 		   pbm, SCZ_PCIERR_B_INO, "pci_b");
269 
270 	/* double mapped */
271 	schizo_set_intr(sc, pbm, PIL_HIGH, schizo_ue, sc, SCZ_UE_INO,
272 	    "ue");
273 	schizo_set_intr(sc, pbm, PIL_HIGH, schizo_ce, sc, SCZ_CE_INO,
274 	    "ce");
275 	schizo_set_intr(sc, pbm, PIL_HIGH, schizo_safari_error, sc,
276 	    SCZ_SERR_INO, "safari");
277 
278 	config_found(&sc->sc_dv, &pba, schizo_print);
279 }
280 
281 int
282 schizo_ue(void *vsc)
283 {
284 	struct schizo_softc *sc = vsc;
285 
286 	panic("%s: uncorrectable error", sc->sc_dv.dv_xname);
287 	return (1);
288 }
289 
290 int
291 schizo_ce(void *vsc)
292 {
293 	struct schizo_softc *sc = vsc;
294 
295 	panic("%s: correctable error", sc->sc_dv.dv_xname);
296 	return (1);
297 }
298 
299 int
300 schizo_pci_error(void *vpbm)
301 {
302 	struct schizo_pbm *sp = vpbm;
303 	struct schizo_softc *sc = sp->sp_sc;
304 	u_int64_t afsr, afar, ctrl, tfar;
305 	u_int32_t csr;
306 	char bits[128];
307 
308 	afsr = schizo_pbm_read(sp, SCZ_PCI_AFSR);
309 	afar = schizo_pbm_read(sp, SCZ_PCI_AFAR);
310 	ctrl = schizo_pbm_read(sp, SCZ_PCI_CTRL);
311 	csr = schizo_cfg_read(sp, PCI_COMMAND_STATUS_REG);
312 
313 	printf("%s: pci bus %c error\n", sc->sc_dv.dv_xname,
314 	    sp->sp_bus_a ? 'A' : 'B');
315 
316 	snprintb(bits, sizeof(bits), SCZ_PCIAFSR_BITS, afsr);
317 	printf("PCIAFSR=%s\n", bits);
318 	printf("PCIAFAR=%lx\n", afar);
319 	snprintb(bits, sizeof(bits), SCZ_PCICTRL_BITS, ctrl);
320 	printf("PCICTRL=%s\n", bits);
321 #ifdef PCI_COMMAND_STATUS_BITS
322 	snprintb(bits, sizeof(bits), PCI_COMMAND_STATUS_BITS, csr);
323 	printf("PCICSR=%s\n", bits);
324 #endif
325 
326 	if (ctrl & SCZ_PCICTRL_MMU_ERR) {
327 		ctrl = schizo_pbm_read(sp, SCZ_PCI_IOMMU_CTRL);
328 		printf("IOMMUCTRL=%lx\n", ctrl);
329 
330 		if ((ctrl & TOM_IOMMU_ERR) == 0)
331 			goto clear_error;
332 
333 		if (sc->sc_tomatillo) {
334 			tfar = schizo_pbm_read(sp, TOM_PCI_IOMMU_TFAR);
335 			printf("IOMMUTFAR=%lx\n", tfar);
336 		}
337 
338 		/* These are non-fatal if target abort was signalled. */
339 		if ((ctrl & TOM_IOMMU_ERR_MASK) == TOM_IOMMU_INV_ERR ||
340 		    ctrl & TOM_IOMMU_ILLTSBTBW_ERR ||
341 		    ctrl & TOM_IOMMU_BADVA_ERR) {
342 			if (csr & PCI_STATUS_TARGET_TARGET_ABORT) {
343 				schizo_pbm_write(sp, SCZ_PCI_IOMMU_CTRL, ctrl);
344 				goto clear_error;
345 			}
346 		}
347 	}
348 
349 	panic("%s: fatal", sc->sc_dv.dv_xname);
350 
351  clear_error:
352 	schizo_cfg_write(sp, PCI_COMMAND_STATUS_REG, csr);
353 	schizo_pbm_write(sp, SCZ_PCI_CTRL, ctrl);
354 	schizo_pbm_write(sp, SCZ_PCI_AFSR, afsr);
355 	return (1);
356 }
357 
358 int
359 schizo_safari_error(void *vsc)
360 {
361 	struct schizo_softc *sc = vsc;
362 
363 	printf("%s: safari error\n", sc->sc_dv.dv_xname);
364 
365 	printf("ERRLOG=%lx\n", schizo_read(sc, SCZ_SAFARI_ERRLOG));
366 	printf("UE_AFSR=%lx\n", schizo_read(sc, SCZ_UE_AFSR));
367 	printf("UE_AFAR=%lx\n", schizo_read(sc, SCZ_UE_AFAR));
368 	printf("CE_AFSR=%lx\n", schizo_read(sc, SCZ_CE_AFSR));
369 	printf("CE_AFAR=%lx\n", schizo_read(sc, SCZ_CE_AFAR));
370 
371 	panic("%s: fatal", sc->sc_dv.dv_xname);
372 	return (1);
373 }
374 
375 void
376 schizo_init_iommu(struct schizo_softc *sc, struct schizo_pbm *pbm)
377 {
378 	struct iommu_state *is = &pbm->sp_is;
379 	int *vdma = NULL, nitem, tsbsize = 7;
380 	u_int32_t iobase = -1;
381 	vaddr_t va;
382 	char *name;
383 
384 	if (prom_getproplen(sc->sc_node, "no-streaming-cache") < 0) {
385 	}
386 
387 	va = (vaddr_t)pbm->sp_flush[0x40];
388 
389 	/* punch in our copies */
390 	is->is_bustag = pbm->sp_regt;
391 	if (bus_space_subregion(is->is_bustag, pbm->sp_regh,
392 	    offsetof(struct schizo_pbm_regs, iommu),
393 	    sizeof(struct schizo_iommureg), &is->is_iommu)) {
394 		printf("schizo: unable to create streaming buffer handle\n");
395 		is->is_sb[0]->sb_flush = NULL;
396 	}
397 
398 	/* initialize our strbuf_ctl */
399 	is->is_sb[0] = &pbm->sp_sb;
400 	pbm->sp_sb.sb_is = is;
401 	is->is_sb[0]->sb_flush = (void *)(va & ~0x3f);
402 
403 	if (bus_space_subregion(is->is_bustag, pbm->sp_regh,
404 	    offsetof(struct schizo_pbm_regs, strbuf),
405 	    sizeof(struct iommu_strbuf), &is->is_sb[0]->sb_sb)) {
406 	}
407 
408 	name = (char *)malloc(32, M_DEVBUF, M_NOWAIT);
409 	if (name == NULL)
410 		panic("couldn't malloc iommu name");
411 	snprintf(name, 32, "%s dvma", sc->sc_dv.dv_xname);
412 
413 	/*
414 	 * Separate the men from the boys.  If the `virtual-dma'
415 	 * property exists, use it.
416 	 */
417 	if (!prom_getprop(sc->sc_node, "virtual-dma", sizeof(vdma), &nitem,
418 	    (void **)&vdma)) {
419 		/* Damn.  Gotta use these values. */
420 		iobase = vdma[0];
421 #define	TSBCASE(x)	case 1 << ((x) + 23): tsbsize = (x); break
422 		switch (vdma[1]) {
423 			TSBCASE(1); TSBCASE(2); TSBCASE(3);
424 			TSBCASE(4); TSBCASE(5); TSBCASE(6);
425 		default:
426 			printf("bogus tsb size %x, using 7\n", vdma[1]);
427 			TSBCASE(7);
428 		}
429 #undef TSBCASE
430 		DPRINTF(SDB_BUSMAP, ("schizo_init_iommu: iobase=0x%x\n", iobase));
431 		free(vdma, M_DEVBUF);
432 	} else {
433 		DPRINTF(SDB_BUSMAP, ("schizo_init_iommu: getprop failed, "
434 		    "using iobase=0x%x, tsbsize=%d\n", iobase, tsbsize));
435 	}
436 
437 	iommu_init(name, is, tsbsize, iobase);
438 }
439 
440 int
441 schizo_print(void *aux, const char *p)
442 {
443 
444 	if (p == NULL)
445 		return (UNCONF);
446 	return (QUIET);
447 }
448 
449 pcireg_t
450 schizo_conf_read(pci_chipset_tag_t pc, pcitag_t tag, int reg)
451 {
452 	struct schizo_pbm *sp = pc->cookie;
453 	pcireg_t val = (pcireg_t)~0;
454 
455 	DPRINTF(SDB_CONF, ("%s: tag %lx reg %x ", __func__, (long)tag, reg));
456 	if (PCITAG_NODE(tag) != -1)
457 		val = bus_space_read_4(sp->sp_cfgt, sp->sp_cfgh,
458 		    PCITAG_OFFSET(tag) + reg);
459 	DPRINTF(SDB_CONF, (" returning %08x\n", (u_int)val));
460 	return (val);
461 }
462 
463 void
464 schizo_conf_write(pci_chipset_tag_t pc, pcitag_t tag, int reg, pcireg_t data)
465 {
466 	struct schizo_pbm *sp = pc->cookie;
467 
468 	DPRINTF(SDB_CONF, ("%s: tag %lx; reg %x; data %x", __func__,
469 		(long)tag, reg, (int)data));
470 
471 	/* If we don't know it, just punt it.  */
472 	if (PCITAG_NODE(tag) == -1) {
473 		DPRINTF(SDB_CONF, (" .. bad addr\n"));
474 		return;
475 	}
476 
477         bus_space_write_4(sp->sp_cfgt, sp->sp_cfgh,
478 	    PCITAG_OFFSET(tag) + reg, data);
479 	DPRINTF(SDB_CONF, (" .. done\n"));
480 }
481 
482 void
483 schizo_set_intr(struct schizo_softc *sc, struct schizo_pbm *pbm, int ipl,
484     int (*handler)(void *), void *arg, int ino, const char *what)
485 {
486 	struct intrhand *ih;
487 	u_int64_t mapoff, clroff;
488 	uintptr_t intrregs;
489 
490 	DPRINTF(SDB_INTR, ("%s: ino %x ign %x fn %p arg %p", __func__,
491 	    ino, sc->sc_ign, handler, arg));
492 
493 	mapoff = offsetof(struct schizo_pbm_regs, imap[ino]);
494 	clroff = offsetof(struct schizo_pbm_regs, iclr[ino]);
495 	ino |= sc->sc_ign;
496 
497 	DPRINTF(SDB_INTR, (" mapoff %lx clroff %lx\n", mapoff, clroff));
498 
499 	ih = (struct intrhand *)
500 		malloc(sizeof(struct intrhand), M_DEVBUF, M_NOWAIT);
501 	if (ih == NULL)
502 		return;
503 	ih->ih_arg = arg;
504 	intrregs = (uintptr_t)bus_space_vaddr(pbm->sp_regt, pbm->sp_intrh);
505 	ih->ih_map = (uint64_t *)(intrregs + mapoff);
506 	ih->ih_clr = (uint64_t *)(intrregs + clroff);
507 	ih->ih_fun = handler;
508 	ih->ih_pil = (1<<ipl);
509 	ih->ih_number = INTVEC(schizo_pbm_read(pbm, mapoff));
510 	intr_establish(ipl, ipl != IPL_VM, ih);
511 
512 	schizo_pbm_write(pbm, mapoff,
513 	    ih->ih_number | INTMAP_V | (CPU_UPAID << INTMAP_TID_SHIFT));
514 }
515 
516 bus_space_tag_t
517 schizo_alloc_mem_tag(struct schizo_pbm *sp)
518 {
519 	return (schizo_alloc_bus_tag(sp, "mem",
520 	    PCI_MEMORY_BUS_SPACE));
521 }
522 
523 bus_space_tag_t
524 schizo_alloc_io_tag(struct schizo_pbm *sp)
525 {
526 	return (schizo_alloc_bus_tag(sp, "io",
527 	    PCI_IO_BUS_SPACE));
528 }
529 
530 bus_space_tag_t
531 schizo_alloc_config_tag(struct schizo_pbm *sp)
532 {
533 	return (schizo_alloc_bus_tag(sp, "cfg",
534 	    PCI_CONFIG_BUS_SPACE));
535 }
536 
537 bus_space_tag_t
538 schizo_alloc_bus_tag(struct schizo_pbm *pbm, const char *name, int type)
539 {
540 	struct schizo_softc *sc = pbm->sp_sc;
541 	bus_space_tag_t bt;
542 
543 	bt = (bus_space_tag_t) malloc(sizeof(struct sparc_bus_space_tag),
544 		    M_DEVBUF, M_NOWAIT | M_ZERO);
545 	if (bt == NULL)
546 		panic("schizo: could not allocate bus tag");
547 
548 	bt->cookie = pbm;
549 	bt->parent = sc->sc_bustag;
550 	bt->type = type;
551 	bt->sparc_bus_map = schizo_bus_map;
552 	bt->sparc_bus_mmap = schizo_bus_mmap;
553 	bt->sparc_intr_establish = schizo_intr_establish;
554 	return (bt);
555 }
556 
557 bus_dma_tag_t
558 schizo_alloc_dma_tag(struct schizo_pbm *pbm)
559 {
560 	struct schizo_softc *sc = pbm->sp_sc;
561 	bus_dma_tag_t dt, pdt = sc->sc_dmat;
562 
563 	dt = malloc(sizeof(*dt), M_DEVBUF, M_NOWAIT | M_ZERO);
564 	if (dt == NULL)
565 		panic("schizo: could not alloc dma tag");
566 
567 	dt->_cookie = pbm;
568 	dt->_parent = pdt;
569 #define PCOPY(x)	dt->x = pdt->x
570 	dt->_dmamap_create = schizo_dmamap_create;
571 	PCOPY(_dmamap_destroy);
572 	dt->_dmamap_load = iommu_dvmamap_load;
573 	PCOPY(_dmamap_load_mbuf);
574 	PCOPY(_dmamap_load_uio);
575 	dt->_dmamap_load_raw = iommu_dvmamap_load_raw;
576 	dt->_dmamap_unload = iommu_dvmamap_unload;
577 	dt->_dmamap_sync = iommu_dvmamap_sync;
578 	dt->_dmamem_alloc = iommu_dvmamem_alloc;
579 	dt->_dmamem_free = iommu_dvmamem_free;
580 	dt->_dmamem_map = iommu_dvmamem_map;
581 	dt->_dmamem_unmap = iommu_dvmamem_unmap;
582 	PCOPY(_dmamem_mmap);
583 #undef	PCOPY
584 	return (dt);
585 }
586 
587 pci_chipset_tag_t
588 schizo_alloc_chipset(struct schizo_pbm *pbm, int node, pci_chipset_tag_t pc)
589 {
590 	pci_chipset_tag_t npc;
591 
592 	npc = malloc(sizeof *npc, M_DEVBUF, M_NOWAIT);
593 	if (npc == NULL)
594 		panic("schizo: could not allocate pci_chipset_tag_t");
595 	memcpy(npc, pc, sizeof *pc);
596 	npc->cookie = pbm;
597 	npc->rootnode = node;
598 	npc->spc_conf_read = schizo_conf_read;
599 	npc->spc_conf_write = schizo_conf_write;
600 	npc->spc_intr_map = schizo_pci_intr_map;
601 	npc->spc_intr_establish = schizo_pci_intr_establish;
602 	npc->spc_find_ino = schizo_pci_find_ino;
603 	return (npc);
604 }
605 
606 int
607 schizo_dmamap_create(bus_dma_tag_t t, bus_size_t size,
608     int nsegments, bus_size_t maxsegsz, bus_size_t boundary, int flags,
609     bus_dmamap_t *dmamp)
610 {
611 	struct schizo_pbm *pbm = t->_cookie;
612 	int error;
613 
614 	error = bus_dmamap_create(t->_parent, size, nsegments, maxsegsz,
615 				  boundary, flags, dmamp);
616 	if (error == 0)
617 		(*dmamp)->_dm_cookie = &pbm->sp_sb;
618 	return error;
619 }
620 
621 static struct schizo_range *
622 get_schizorange(struct schizo_pbm *pbm, int ss)
623 {
624 	int i;
625 
626 	for (i = 0; i < pbm->sp_nrange; i++) {
627 		if (((pbm->sp_range[i].cspace >> 24) & 0x03) == ss)
628 			return (&pbm->sp_range[i]);
629 	}
630 	/* not found */
631 	return (NULL);
632 }
633 
634 int
635 schizo_bus_map(bus_space_tag_t t, bus_addr_t offset, bus_size_t size,
636 	       int flags, vaddr_t unused, bus_space_handle_t *hp)
637 {
638 	bus_addr_t paddr;
639 	struct schizo_pbm *pbm = t->cookie;
640 	struct schizo_softc *sc = pbm->sp_sc;
641 	struct schizo_range *sr;
642 	int ss;
643 
644 	DPRINTF(SDB_BUSMAP, ("schizo_bus_map: type %d off %qx sz %qx flags %d",
645 	    t->type,
646 	    (unsigned long long)offset,
647 	    (unsigned long long)size,
648 	    flags));
649 
650 	ss = sparc_pci_childspace(t->type);
651 	DPRINTF(SDB_BUSMAP, (" cspace %d\n", ss));
652 
653 	sr = get_schizorange(pbm, ss);
654 	if (sr != NULL) {
655 		paddr = BUS_ADDR(sr->phys_hi, sr->phys_lo + offset);
656 		DPRINTF(SDB_BUSMAP, ("%s: mapping paddr "
657 				     "space %lx offset %lx paddr %qx\n",
658 			       __func__, (long)ss, (long)offset,
659 			       (unsigned long long)paddr));
660 		return ((*sc->sc_bustag->sparc_bus_map)(t, paddr, size,
661 			flags, 0, hp));
662 	}
663 	DPRINTF(SDB_BUSMAP, ("%s: FAILED\n", __func__));
664 	return (EINVAL);
665 }
666 
667 static paddr_t
668 schizo_bus_mmap(bus_space_tag_t t, bus_addr_t paddr, off_t off, int prot,
669 	int flags)
670 {
671 	bus_addr_t offset = paddr;
672 	struct schizo_pbm *pbm = t->cookie;
673 	struct schizo_softc *sc = pbm->sp_sc;
674 	struct schizo_range *sr;
675 	int ss;
676 
677 	ss = sparc_pci_childspace(t->type);
678 
679 	DPRINTF(SDB_BUSMAP, ("schizo_bus_mmap: prot %d flags %d pa %qx\n",
680 	    prot, flags, (unsigned long long)paddr));
681 
682 	sr = get_schizorange(pbm, ss);
683 	if (sr != NULL) {
684 		paddr = BUS_ADDR(sr->phys_hi, sr->phys_lo + offset);
685 		DPRINTF(SDB_BUSMAP, ("%s: mapping paddr "
686 				     "space %lx offset %lx paddr %qx\n",
687 			       __func__, (long)ss, (long)offset,
688 			       (unsigned long long)paddr));
689 		return (bus_space_mmap(sc->sc_bustag, paddr, off,
690 				       prot, flags));
691 	}
692 	DPRINTF(SDB_BUSMAP, ("%s: FAILED\n", __func__));
693 	return (-1);
694 }
695 
696 /*
697  * Set the IGN for this schizo into the handle.
698  */
699 int
700 schizo_pci_intr_map(struct pci_attach_args *pa, pci_intr_handle_t *ihp)
701 {
702 	struct schizo_pbm *pbm = pa->pa_pc->cookie;
703 	struct schizo_softc *sc = pbm->sp_sc;
704 
705 	*ihp |= sc->sc_ign;
706 	DPRINTF(SDB_INTMAP, ("returning IGN adjusted to %x\n", *ihp));
707 	return (0);
708 }
709 
710 static void *
711 schizo_intr_establish(bus_space_tag_t t, int ihandle, int level,
712 	int (*handler)(void *), void *arg, void (*fastvec)(void) /* ignored */)
713 {
714 	struct schizo_pbm *pbm = t->cookie;
715 	struct intrhand *ih = NULL;
716 	uint64_t mapoff, clroff;
717 	uintptr_t intrregs;
718 	volatile uint64_t *intrmapptr = NULL, *intrclrptr = NULL;
719 	int ino;
720 	long vec;
721 
722 	vec = INTVEC(ihandle);
723 	ino = INTINO(vec);
724 
725 	ih = malloc(sizeof *ih, M_DEVBUF, M_NOWAIT);
726 	if (ih == NULL)
727 		return (NULL);
728 
729 	DPRINTF(SDB_INTR, ("\n%s: ihandle %d level %d fn %p arg %p\n", __func__,
730 	    ihandle, level, handler, arg));
731 
732 	if (level == IPL_NONE)
733 		level = INTLEV(vec);
734 	if (level == IPL_NONE) {
735 		printf(": no IPL, setting IPL 2.\n");
736 		level = 2;
737 	}
738 
739 	mapoff = offsetof(struct schizo_pbm_regs, imap[ino]);
740 	clroff = offsetof(struct schizo_pbm_regs, iclr[ino]);
741 
742 	DPRINTF(SDB_INTR, ("%s: intr %x: %p mapoff %lx clroff %lx\n",
743 	    __func__, ino, intrlev[ino], mapoff, clroff));
744 
745 	intrregs = (uintptr_t)bus_space_vaddr(pbm->sp_regt, pbm->sp_intrh);
746 	intrmapptr = (uint64_t *)(intrregs + mapoff);
747 	intrclrptr = (uint64_t *)(intrregs + clroff);
748 
749 	if (INTIGN(vec) == 0)
750 		ino |= schizo_pbm_readintr(pbm, mapoff) & INTMAP_IGN;
751 	else
752 		ino |= vec & INTMAP_IGN;
753 
754 	/* Register the map and clear intr registers */
755 	ih->ih_map = intrmapptr;
756 	ih->ih_clr = intrclrptr;
757 
758 	ih->ih_fun = handler;
759 	ih->ih_arg = arg;
760 	ih->ih_pil = level;
761 	ih->ih_number = ino;
762 
763 	DPRINTF(SDB_INTR, (
764 	    "; installing handler %p arg %p with inr %x pil %u\n",
765 	    handler, arg, ino, (u_int)ih->ih_pil));
766 
767 	intr_establish(ih->ih_pil, level != IPL_VM, ih);
768 
769 	/*
770 	 * Enable the interrupt now we have the handler installed.
771 	 * Read the current value as we can't change it besides the
772 	 * valid bit so so make sure only this bit is changed.
773 	 */
774 	if (intrmapptr) {
775 		u_int64_t imap;
776 
777 		imap = schizo_pbm_readintr(pbm, mapoff);
778 		DPRINTF(SDB_INTR, ("; read intrmap = %016qx",
779 			(unsigned long long)imap));
780 		imap |= INTMAP_V;
781 		DPRINTF(SDB_INTR, ("; addr of intrmapptr = %p", intrmapptr));
782 		DPRINTF(SDB_INTR, ("; writing intrmap = %016qx\n",
783 			(unsigned long long)imap));
784 		schizo_pbm_writeintr(pbm, mapoff, imap);
785 		imap = schizo_pbm_readintr(pbm, mapoff);
786 		DPRINTF(SDB_INTR, ("; reread intrmap = %016qx",
787 			(unsigned long long)imap));
788 		ih->ih_number |= imap & INTMAP_INR;
789 	}
790  	if (intrclrptr) {
791  		/* set state to IDLE */
792 		schizo_pbm_writeintr(pbm, clroff, 0);
793  	}
794 
795 	return (ih);
796 }
797 
798 static void *
799 schizo_pci_intr_establish(pci_chipset_tag_t pc, pci_intr_handle_t ih, int level,
800 	int (*func)(void *), void *arg)
801 {
802 	void *cookie;
803 	struct schizo_pbm *pbm = (struct schizo_pbm *)pc->cookie;
804 
805 	DPRINTF(SDB_INTR, ("%s: ih %lx; level %d", __func__, (u_long)ih, level));
806 	cookie = bus_intr_establish(pbm->sp_memt, ih, level, func, arg);
807 
808 	DPRINTF(SDB_INTR, ("; returning handle %p\n", cookie));
809 	return (cookie);
810 }
811 
812 static int
813 schizo_pci_find_ino(struct pci_attach_args *pa, pci_intr_handle_t *ihp)
814 {
815 #if 0
816 	struct schizo_pbm *pbm = pa->pa_pc->cookie;
817 	struct schizo_softc *sc = pbm->sp_sc;
818 	u_int bus;
819 	u_int dev;
820 	u_int pin;
821 #endif
822 
823 	DPRINTF(SDB_INTMAP, ("pci_find_ino: pa_tag: node %x, %d:%d:%d\n",
824 			      PCITAG_NODE(pa->pa_tag), (int)PCITAG_BUS(pa->pa_tag),
825 			      (int)PCITAG_DEV(pa->pa_tag),
826 			      (int)PCITAG_FUN(pa->pa_tag)));
827 	DPRINTF(SDB_INTMAP,
828 		("pci_find_ino: intrswiz %d, intrpin %d, intrline %d, rawintrpin %d\n",
829 		 pa->pa_intrswiz, pa->pa_intrpin, pa->pa_intrline, pa->pa_rawintrpin));
830 	DPRINTF(SDB_INTMAP, ("pci_find_ino: pa_intrtag: node %x, %d:%d:%d\n",
831 			      PCITAG_NODE(pa->pa_intrtag),
832 			      (int)PCITAG_BUS(pa->pa_intrtag),
833 			      (int)PCITAG_DEV(pa->pa_intrtag),
834 			      (int)PCITAG_FUN(pa->pa_intrtag)));
835 
836 #if 0
837 	bus = (pp->pp_id == PSYCHO_PBM_B);
838 	/*
839 	 * If we are on a ppb, use the devno on the underlying bus when forming
840 	 * the ivec.
841 	 */
842 	if (pa->pa_intrswiz != 0 && PCITAG_NODE(pa->pa_intrtag) != 0)
843 		dev = PCITAG_DEV(pa->pa_intrtag);
844 	else
845 		dev = pa->pa_device;
846 	dev--;
847 
848 	if (sc->sc_mode == PSYCHO_MODE_PSYCHO &&
849 	    pp->pp_id == PSYCHO_PBM_B)
850 		dev--;
851 
852 	pin = pa->pa_intrpin - 1;
853 	DPRINTF(SDB_INTMAP, ("pci_find_ino: mode %d, pbm %d, dev %d, pin %d\n",
854 	    sc->sc_mode, pp->pp_id, dev, pin));
855 
856 	*ihp = sc->sc_ign | ((bus << 4) & INTMAP_PCIBUS) |
857 	    ((dev << 2) & INTMAP_PCISLOT) | (pin & INTMAP_PCIINT);
858 #endif
859 
860 	return (0);
861 }
862