xref: /netbsd-src/sys/arch/sparc64/dev/psycho.c (revision 8b0f9554ff8762542c4defc4f70e1eb76fb508fa)
1 /*	$NetBSD: psycho.c,v 1.82 2007/12/03 14:13:18 rafal Exp $	*/
2 
3 /*
4  * Copyright (c) 2001, 2002 Eduardo E. Horvath
5  * Copyright (c) 1999, 2000 Matthew R. Green
6  * All rights reserved.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions
10  * are met:
11  * 1. Redistributions of source code must retain the above copyright
12  *    notice, this list of conditions and the following disclaimer.
13  * 2. Redistributions in binary form must reproduce the above copyright
14  *    notice, this list of conditions and the following disclaimer in the
15  *    documentation and/or other materials provided with the distribution.
16  * 3. The name of the author may not be used to endorse or promote products
17  *    derived from this software without specific prior written permission.
18  *
19  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
20  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
21  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
22  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
23  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
24  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
25  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
26  * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
27  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29  * SUCH DAMAGE.
30  */
31 
32 #include <sys/cdefs.h>
33 __KERNEL_RCSID(0, "$NetBSD: psycho.c,v 1.82 2007/12/03 14:13:18 rafal Exp $");
34 
35 #include "opt_ddb.h"
36 
37 /*
38  * Support for `psycho' and `psycho+' UPA to PCI bridge and
39  * UltraSPARC IIi and IIe `sabre' PCI controllers.
40  */
41 
42 #ifdef DEBUG
43 #define PDB_PROM	0x01
44 #define PDB_BUSMAP	0x02
45 #define PDB_INTR	0x04
46 int psycho_debug = 0x0;
47 #define DPRINTF(l, s)   do { if (psycho_debug & l) printf s; } while (0)
48 #else
49 #define DPRINTF(l, s)
50 #endif
51 
52 #include <sys/param.h>
53 #include <sys/device.h>
54 #include <sys/errno.h>
55 #include <sys/extent.h>
56 #include <sys/malloc.h>
57 #include <sys/systm.h>
58 #include <sys/time.h>
59 #include <sys/reboot.h>
60 
61 #include <uvm/uvm.h>
62 
63 #define _SPARC_BUS_DMA_PRIVATE
64 #include <machine/bus.h>
65 #include <machine/autoconf.h>
66 #include <machine/psl.h>
67 
68 #include <dev/pci/pcivar.h>
69 #include <dev/pci/pcireg.h>
70 #include <dev/sysmon/sysmon_taskq.h>
71 
72 #include <sparc64/dev/iommureg.h>
73 #include <sparc64/dev/iommuvar.h>
74 #include <sparc64/dev/psychoreg.h>
75 #include <sparc64/dev/psychovar.h>
76 #include <sparc64/sparc64/cache.h>
77 
78 #include "ioconf.h"
79 
80 static pci_chipset_tag_t psycho_alloc_chipset(struct psycho_pbm *, int,
81 	pci_chipset_tag_t);
82 static struct extent *psycho_alloc_extent(struct psycho_pbm *, int, int,
83 	const char *);
84 static void psycho_get_bus_range(int, int *);
85 static void psycho_get_ranges(int, struct psycho_ranges **, int *);
86 static void psycho_set_intr(struct psycho_softc *, int, void *, uint64_t *,
87 	uint64_t *);
88 
89 /* Interrupt handlers */
90 static int psycho_ue(void *);
91 static int psycho_ce(void *);
92 static int psycho_bus_a(void *);
93 static int psycho_bus_b(void *);
94 static int psycho_powerfail(void *);
95 static int psycho_wakeup(void *);
96 
97 
98 /* IOMMU support */
99 static void psycho_iommu_init(struct psycho_softc *, int);
100 
101 /*
102  * bus space and bus DMA support for UltraSPARC `psycho'.  note that most
103  * of the bus DMA support is provided by the iommu dvma controller.
104  */
105 static int get_childspace(int);
106 static struct psycho_ranges *get_psychorange(struct psycho_pbm *, int);
107 
108 static paddr_t psycho_bus_mmap(bus_space_tag_t, bus_addr_t, off_t, int, int);
109 static int _psycho_bus_map(bus_space_tag_t, bus_addr_t, bus_size_t, int,
110 	vaddr_t, bus_space_handle_t *);
111 static void *psycho_intr_establish(bus_space_tag_t, int, int, int (*)(void *),
112 	void *, void(*)(void));
113 
114 static int psycho_dmamap_load(bus_dma_tag_t, bus_dmamap_t, void *, bus_size_t,
115 	struct proc *, int);
116 static void psycho_dmamap_unload(bus_dma_tag_t, bus_dmamap_t);
117 static int psycho_dmamap_load_raw(bus_dma_tag_t, bus_dmamap_t,
118 	bus_dma_segment_t *, int, bus_size_t, int);
119 static void psycho_dmamap_sync(bus_dma_tag_t, bus_dmamap_t, bus_addr_t,
120 	bus_size_t, int);
121 int psycho_dmamem_alloc(bus_dma_tag_t, bus_size_t, bus_size_t, bus_size_t,
122 	bus_dma_segment_t *, int, int *, int);
123 void psycho_dmamem_free(bus_dma_tag_t, bus_dma_segment_t *, int);
124 int psycho_dmamem_map(bus_dma_tag_t, bus_dma_segment_t *, int, size_t,
125 	void **, int);
126 void psycho_dmamem_unmap(bus_dma_tag_t, void *, size_t);
127 
128 /* base pci_chipset */
129 extern struct sparc_pci_chipset _sparc_pci_chipset;
130 
131 /* power button handlers */
132 static void psycho_register_power_button(struct psycho_softc *sc);
133 static void psycho_power_button_pressed(void *arg);
134 
135 /*
136  * autoconfiguration
137  */
138 static	int	psycho_match(struct device *, struct cfdata *, void *);
139 static	void	psycho_attach(struct device *, struct device *, void *);
140 static	int	psycho_print(void *aux, const char *p);
141 
142 CFATTACH_DECL(psycho, sizeof(struct psycho_softc),
143     psycho_match, psycho_attach, NULL, NULL);
144 
145 /*
146  * "sabre" is the UltraSPARC IIi onboard UPA to PCI bridge.  It manages a
147  * single PCI bus and does not have a streaming buffer.  It often has an APB
148  * (advanced PCI bridge) connected to it, which was designed specifically for
149  * the IIi.  The APB let's the IIi handle two independednt PCI buses, and
150  * appears as two "simba"'s underneath the sabre.
151  *
152  * "psycho" and "psycho+" is a dual UPA to PCI bridge.  It sits on the UPA bus
153  * and manages two PCI buses.  "psycho" has two 64-bit 33 MHz buses, while
154  * "psycho+" controls both a 64-bit 33 MHz and a 64-bit 66 MHz PCI bus.  You
155  * will usually find a "psycho+" since I don't think the original "psycho"
156  * ever shipped, and if it did it would be in the U30.
157  *
158  * Each "psycho" PCI bus appears as a separate OFW node, but since they are
159  * both part of the same IC, they only have a single register space.  As such,
160  * they need to be configured together, even though the autoconfiguration will
161  * attach them separately.
162  *
163  * On UltraIIi machines, "sabre" itself usually takes pci0, with "simba" often
164  * as pci1 and pci2, although they have been implemented with other PCI bus
165  * numbers on some machines.
166  *
167  * On UltraII machines, there can be any number of "psycho+" ICs, each
168  * providing two PCI buses.
169  *
170  *
171  * XXXX The psycho/sabre node has an `interrupts' attribute.  They contain
172  * the values of the following interrupts in this order:
173  *
174  * PCI Bus Error	(30)
175  * DMA UE		(2e)
176  * DMA CE		(2f)
177  * Power Fail		(25)
178  *
179  * We really should attach handlers for each.
180  *
181  */
182 
183 #define	ROM_PCI_NAME		"pci"
184 
185 struct psycho_names {
186 	const char *p_name;
187 	int p_type;
188 } psycho_names[] = {
189 	{ "SUNW,psycho",        PSYCHO_MODE_PSYCHO      },
190 	{ "pci108e,8000",       PSYCHO_MODE_PSYCHO      },
191 	{ "SUNW,sabre",         PSYCHO_MODE_SABRE       },
192 	{ "pci108e,a000",       PSYCHO_MODE_SABRE       },
193 	{ "pci108e,a001",       PSYCHO_MODE_SABRE       },
194 	{ NULL, 0 }
195 };
196 
197 static	int
198 psycho_match(struct device *parent, struct cfdata *match, void *aux)
199 {
200 	struct mainbus_attach_args *ma = aux;
201 	char *model = prom_getpropstring(ma->ma_node, "model");
202 	int i;
203 
204 	/* match on a name of "pci" and a sabre or a psycho */
205 	if (strcmp(ma->ma_name, ROM_PCI_NAME) == 0) {
206 		for (i=0; psycho_names[i].p_name; i++)
207 			if (strcmp(model, psycho_names[i].p_name) == 0)
208 				return (1);
209 
210 		model = prom_getpropstring(ma->ma_node, "compatible");
211 		for (i=0; psycho_names[i].p_name; i++)
212 			if (strcmp(model, psycho_names[i].p_name) == 0)
213 				return (1);
214 	}
215 	return (0);
216 }
217 
218 #ifdef DEBUG
219 static void psycho_dump_intmap(struct psycho_softc *sc);
220 static void
221 psycho_dump_intmap(struct psycho_softc *sc)
222 {
223 	volatile uint64_t *intrmapptr = NULL;
224 
225 	printf("psycho_dump_intmap: OBIO\n");
226 
227 	for (intrmapptr = &sc->sc_regs->scsi_int_map;
228 	     intrmapptr < &sc->sc_regs->ue_int_map;
229 	     intrmapptr++)
230 		printf("%p: %llx\n", intrmapptr,
231 		    (unsigned long long)*intrmapptr);
232 
233 	printf("\tintmap:pci\n");
234 	for (intrmapptr = &sc->sc_regs->pcia_slot0_int;
235 	     intrmapptr <= &sc->sc_regs->pcib_slot3_int;
236 	     intrmapptr++)
237 		printf("%p: %llx\n", intrmapptr,
238 		    (unsigned long long)*intrmapptr);
239 
240 	printf("\tintmap:ffb\n");
241 	for (intrmapptr = &sc->sc_regs->ffb0_int_map;
242 	     intrmapptr <= &sc->sc_regs->ffb1_int_map;
243 	     intrmapptr++)
244 		printf("%p: %llx\n", intrmapptr,
245 		    (unsigned long long)*intrmapptr);
246 }
247 #endif
248 
249 /*
250  * SUNW,psycho initialisation ..
251  *	- find the per-psycho registers
252  *	- figure out the IGN.
253  *	- find our partner psycho
254  *	- configure ourselves
255  *	- bus range, bus,
256  *	- get interrupt-map and interrupt-map-mask
257  *	- setup the chipsets.
258  *	- if we're the first of the pair, initialise the IOMMU, otherwise
259  *	  just copy it's tags and addresses.
260  */
261 static	void
262 psycho_attach(struct device *parent, struct device *self, void *aux)
263 {
264 	struct psycho_softc *sc = (struct psycho_softc *)self;
265 	struct psycho_softc *osc = NULL;
266 	struct psycho_pbm *pp;
267 	struct pcibus_attach_args pba;
268 	struct mainbus_attach_args *ma = aux;
269 	struct psycho_ranges *pr;
270 	prop_dictionary_t dict;
271 	bus_space_handle_t bh;
272 	uint64_t csr, mem_base;
273 	int psycho_br[2], n, i;
274 	bus_space_handle_t pci_ctl;
275 	char *model = prom_getpropstring(ma->ma_node, "model");
276 
277 	printf("\n");
278 
279 	sc->sc_node = ma->ma_node;
280 	sc->sc_bustag = ma->ma_bustag;
281 	sc->sc_dmatag = ma->ma_dmatag;
282 
283 	/*
284 	 * Identify the device.
285 	 */
286 	for (i=0; psycho_names[i].p_name; i++)
287 		if (strcmp(model, psycho_names[i].p_name) == 0) {
288 			sc->sc_mode = psycho_names[i].p_type;
289 			goto found;
290 		}
291 
292 	model = prom_getpropstring(ma->ma_node, "compatible");
293 	for (i=0; psycho_names[i].p_name; i++)
294 		if (strcmp(model, psycho_names[i].p_name) == 0) {
295 			sc->sc_mode = psycho_names[i].p_type;
296 			goto found;
297 		}
298 
299 	panic("unknown psycho model %s", model);
300 found:
301 
302 	/*
303 	 * The psycho gets three register banks:
304 	 * (0) per-PBM configuration and status registers
305 	 * (1) per-PBM PCI configuration space, containing only the
306 	 *     PBM 256-byte PCI header
307 	 * (2) the shared psycho configuration registers (struct psychoreg)
308 	 */
309 
310 	/* Register layouts are different.  stuupid. */
311 	if (sc->sc_mode == PSYCHO_MODE_PSYCHO) {
312 		sc->sc_basepaddr = (paddr_t)ma->ma_reg[2].ur_paddr;
313 
314 		if (ma->ma_naddress > 2) {
315 			sparc_promaddr_to_handle(sc->sc_bustag,
316 				ma->ma_address[2], &sc->sc_bh);
317 			sparc_promaddr_to_handle(sc->sc_bustag,
318 				ma->ma_address[0], &pci_ctl);
319 
320 			sc->sc_regs = (struct psychoreg *)
321 				bus_space_vaddr(sc->sc_bustag, sc->sc_bh);
322 		} else if (ma->ma_nreg > 2) {
323 
324 			/* We need to map this in ourselves. */
325 			if (bus_space_map(sc->sc_bustag,
326 				ma->ma_reg[2].ur_paddr,
327 				ma->ma_reg[2].ur_len, BUS_SPACE_MAP_LINEAR,
328 				&sc->sc_bh))
329 				panic("psycho_attach: cannot map regs");
330 			sc->sc_regs = (struct psychoreg *)
331 				bus_space_vaddr(sc->sc_bustag, sc->sc_bh);
332 
333 			if (bus_space_map(sc->sc_bustag,
334 				ma->ma_reg[0].ur_paddr,
335 				ma->ma_reg[0].ur_len, BUS_SPACE_MAP_LINEAR,
336 				&pci_ctl))
337 				panic("psycho_attach: cannot map ctl");
338 		} else
339 			panic("psycho_attach: %d not enough registers",
340 				ma->ma_nreg);
341 
342 	} else {
343 		sc->sc_basepaddr = (paddr_t)ma->ma_reg[0].ur_paddr;
344 
345 		if (ma->ma_naddress) {
346 			sparc_promaddr_to_handle(sc->sc_bustag,
347 				ma->ma_address[0], &sc->sc_bh);
348 			sc->sc_regs = (struct psychoreg *)
349 				bus_space_vaddr(sc->sc_bustag, sc->sc_bh);
350 			bus_space_subregion(sc->sc_bustag, sc->sc_bh,
351 				offsetof(struct psychoreg,  psy_pcictl),
352 				sizeof(struct pci_ctl), &pci_ctl);
353 		} else if (ma->ma_nreg) {
354 
355 			/* We need to map this in ourselves. */
356 			if (bus_space_map(sc->sc_bustag,
357 				ma->ma_reg[0].ur_paddr,
358 				ma->ma_reg[0].ur_len, BUS_SPACE_MAP_LINEAR,
359 				&sc->sc_bh))
360 				panic("psycho_attach: cannot map regs");
361 			sc->sc_regs = (struct psychoreg *)
362 				bus_space_vaddr(sc->sc_bustag, sc->sc_bh);
363 
364 			bus_space_subregion(sc->sc_bustag, sc->sc_bh,
365 				offsetof(struct psychoreg,  psy_pcictl),
366 				sizeof(struct pci_ctl), &pci_ctl);
367 		} else
368 			panic("psycho_attach: %d not enough registers",
369 				ma->ma_nreg);
370 	}
371 
372 
373 	csr = bus_space_read_8(sc->sc_bustag, sc->sc_bh,
374 		offsetof(struct psychoreg, psy_csr));
375 	sc->sc_ign = 0x7c0; /* APB IGN is always 0x7c */
376 	if (sc->sc_mode == PSYCHO_MODE_PSYCHO)
377 		sc->sc_ign = PSYCHO_GCSR_IGN(csr) << 6;
378 
379 	printf("%s: impl %d, version %d: ign %x ",
380 		model, PSYCHO_GCSR_IMPL(csr), PSYCHO_GCSR_VERS(csr),
381 		sc->sc_ign);
382 	/*
383 	 * Match other psycho's that are already configured against
384 	 * the base physical address. This will be the same for a
385 	 * pair of devices that share register space.
386 	 */
387 	for (n = 0; n < psycho_cd.cd_ndevs; n++) {
388 
389 		struct psycho_softc *asc =
390 			(struct psycho_softc *)psycho_cd.cd_devs[n];
391 
392 		if (asc == NULL || asc == sc)
393 			/* This entry is not there or it is me */
394 			continue;
395 
396 		if (asc->sc_basepaddr != sc->sc_basepaddr)
397 			/* This is an unrelated psycho */
398 			continue;
399 
400 		/* Found partner */
401 		osc = asc;
402 		break;
403 	}
404 
405 
406 	/* Oh, dear.  OK, lets get started */
407 
408 	/*
409 	 * Setup the PCI control register
410 	 */
411 	csr = bus_space_read_8(sc->sc_bustag, pci_ctl,
412 		offsetof(struct pci_ctl, pci_csr));
413 	csr |= PCICTL_MRLM |
414 	       PCICTL_ARB_PARK |
415 	       PCICTL_ERRINTEN |
416 	       PCICTL_4ENABLE;
417 	csr &= ~(PCICTL_SERR |
418 		 PCICTL_CPU_PRIO |
419 		 PCICTL_ARB_PRIO |
420 		 PCICTL_RTRYWAIT);
421 	bus_space_write_8(sc->sc_bustag, pci_ctl,
422 		offsetof(struct pci_ctl, pci_csr), csr);
423 
424 
425 	/*
426 	 * Allocate our psycho_pbm
427 	 */
428 	pp = sc->sc_psycho_this = malloc(sizeof *pp, M_DEVBUF,
429 					 M_NOWAIT | M_ZERO);
430 	if (pp == NULL)
431 		panic("could not allocate psycho pbm");
432 
433 	pp->pp_sc = sc;
434 
435 	/* grab the psycho ranges */
436 	psycho_get_ranges(sc->sc_node, &pp->pp_range, &pp->pp_nrange);
437 
438 	/* get the bus-range for the psycho */
439 	psycho_get_bus_range(sc->sc_node, psycho_br);
440 
441 	pba.pba_bus = psycho_br[0];
442 	pba.pba_bridgetag = NULL;
443 	pp->pp_busmax = psycho_br[1];
444 
445 	printf("bus range %u to %u", psycho_br[0], psycho_br[1]);
446 	printf("; PCI bus %d", psycho_br[0]);
447 
448 	pp->pp_pcictl = pci_ctl;
449 
450 	/* allocate our tags */
451 	pp->pp_memt = psycho_alloc_mem_tag(pp);
452 	pp->pp_iot = psycho_alloc_io_tag(pp);
453 	pp->pp_dmat = psycho_alloc_dma_tag(pp);
454 	pp->pp_flags = (pp->pp_memt ? PCI_FLAGS_MEM_ENABLED : 0) |
455 		       (pp->pp_iot ? PCI_FLAGS_IO_ENABLED : 0);
456 
457 	/* allocate a chipset for this */
458 	pp->pp_pc = psycho_alloc_chipset(pp, sc->sc_node, &_sparc_pci_chipset);
459 
460 	/* setup the rest of the psycho pbm */
461 	pba.pba_pc = psycho_alloc_chipset(pp, sc->sc_node, pp->pp_pc);
462 
463 	switch((ma->ma_reg[0].ur_paddr) & 0xf000) {
464 	case 0x2000:
465 		pp->pp_id = PSYCHO_PBM_A;
466 		break;
467 	case 0x4000:
468 		pp->pp_id = PSYCHO_PBM_B;
469 		break;
470 	}
471 
472 	printf("\n");
473 
474 	/* allocate extents for free bus space */
475 	pp->pp_exmem = psycho_alloc_extent(pp, sc->sc_node, 0x02, "psycho mem");
476 	pp->pp_exio = psycho_alloc_extent(pp, sc->sc_node, 0x01, "psycho io");
477 
478 #ifdef DEBUG
479 	if (psycho_debug & PDB_INTR)
480 		psycho_dump_intmap(sc);
481 #endif
482 
483 	/*
484 	 * And finally, if we're a sabre or the first of a pair of psycho's to
485 	 * arrive here, start up the IOMMU and get a config space tag.
486 	 */
487 	if (osc == NULL) {
488 		uint64_t timeo;
489 
490 		/*
491 		 * Establish handlers for interesting interrupts....
492 		 *
493 		 * XXX We need to remember these and remove this to support
494 		 * hotplug on the UPA/FHC bus.
495 		 *
496 		 * XXX Not all controllers have these, but installing them
497 		 * is better than trying to sort through this mess.
498 		 */
499 		psycho_set_intr(sc, 15, psycho_ue,
500 			&sc->sc_regs->ue_int_map,
501 			&sc->sc_regs->ue_clr_int);
502 		psycho_set_intr(sc, 1, psycho_ce,
503 			&sc->sc_regs->ce_int_map,
504 			&sc->sc_regs->ce_clr_int);
505 		psycho_set_intr(sc, 15, psycho_bus_a,
506 			&sc->sc_regs->pciaerr_int_map,
507 			&sc->sc_regs->pciaerr_clr_int);
508 		psycho_set_intr(sc, 15, psycho_powerfail,
509 			&sc->sc_regs->power_int_map,
510 			&sc->sc_regs->power_clr_int);
511 		psycho_register_power_button(sc);
512 		if (sc->sc_mode != PSYCHO_MODE_SABRE) {
513 			/* sabre doesn't have these interrupts */
514 			psycho_set_intr(sc, 15, psycho_bus_b,
515 					&sc->sc_regs->pciberr_int_map,
516 					&sc->sc_regs->pciberr_clr_int);
517 			psycho_set_intr(sc, 1, psycho_wakeup,
518 					&sc->sc_regs->pwrmgt_int_map,
519 					&sc->sc_regs->pwrmgt_clr_int);
520 		}
521 
522 		/*
523 		 * Apparently a number of machines with psycho and psycho+
524 		 * controllers have interrupt latency issues.  We'll try
525 		 * setting the interrupt retry timeout to 0xff which gives us
526 		 * a retry of 3-6 usec (which is what sysio is set to) for the
527 		 * moment, which seems to help alleviate this problem.
528 		 */
529 		timeo = sc->sc_regs->intr_retry_timer;
530 		if (timeo > 0xfff) {
531 #ifdef DEBUG
532 			printf("decreasing interrupt retry timeout "
533 				"from %lx to 0xff\n", (long)timeo);
534 #endif
535 			sc->sc_regs->intr_retry_timer = 0xff;
536 		}
537 
538 		/*
539 		 * Allocate bus node, this contains a prom node per bus.
540 		 */
541 		pp->pp_busnode = malloc(sizeof(*pp->pp_busnode), M_DEVBUF,
542 					M_NOWAIT | M_ZERO);
543 		if (pp->pp_busnode == NULL)
544 			panic("psycho_attach: malloc pp->pp_busnode");
545 
546 		/*
547 		 * Setup IOMMU and PCI configuration if we're the first
548 		 * of a pair of psycho's to arrive here.
549 		 *
550 		 * We should calculate a TSB size based on amount of RAM
551 		 * and number of bus controllers and number an type of
552 		 * child devices.
553 		 *
554 		 * For the moment, 32KB should be more than enough.
555 		 */
556 		sc->sc_is = malloc(sizeof(struct iommu_state),
557 			M_DEVBUF, M_NOWAIT);
558 		if (sc->sc_is == NULL)
559 			panic("psycho_attach: malloc iommu_state");
560 
561 		/* Point the strbuf_ctl at the iommu_state */
562 		pp->pp_sb.sb_is = sc->sc_is;
563 
564 		sc->sc_is->is_sb[0] = sc->sc_is->is_sb[1] = NULL;
565 		if (prom_getproplen(sc->sc_node, "no-streaming-cache") < 0) {
566 			struct strbuf_ctl *sb = &pp->pp_sb;
567 			vaddr_t va = (vaddr_t)&pp->pp_flush[0x40];
568 
569 			/*
570 			 * Initialize the strbuf_ctl.
571 			 *
572 			 * The flush sync buffer must be 64-byte aligned.
573 			 */
574 			sb->sb_flush = (void *)(va & ~0x3f);
575 
576 			bus_space_subregion(sc->sc_bustag, pci_ctl,
577 				offsetof(struct pci_ctl, pci_strbuf),
578 				sizeof (struct iommu_strbuf), &sb->sb_sb);
579 
580 			/* Point our iommu at the strbuf_ctl */
581 			sc->sc_is->is_sb[0] = sb;
582 		}
583 
584 		psycho_iommu_init(sc, 2);
585 
586 		sc->sc_configtag = psycho_alloc_config_tag(sc->sc_psycho_this);
587 
588 		/*
589 		 * XXX This is a really ugly hack because PCI config space
590 		 * is explicitly handled with unmapped accesses.
591 		 */
592 		i = sc->sc_bustag->type;
593 		sc->sc_bustag->type = PCI_CONFIG_BUS_SPACE;
594 		if (bus_space_map(sc->sc_bustag, sc->sc_basepaddr + 0x01000000,
595 			0x01000000, 0, &bh))
596 			panic("could not map psycho PCI configuration space");
597 		sc->sc_bustag->type = i;
598 		sc->sc_configaddr = bh;
599 	} else {
600 		/* Share bus numbers with the pair of mine */
601 		pp->pp_busnode = osc->sc_psycho_this->pp_busnode;
602 
603 		/* Just copy IOMMU state, config tag and address */
604 		sc->sc_is = osc->sc_is;
605 		sc->sc_configtag = osc->sc_configtag;
606 		sc->sc_configaddr = osc->sc_configaddr;
607 
608 		/* Point the strbuf_ctl at the iommu_state */
609 		pp->pp_sb.sb_is = sc->sc_is;
610 
611 		if (prom_getproplen(sc->sc_node, "no-streaming-cache") < 0) {
612 			struct strbuf_ctl *sb = &pp->pp_sb;
613 			vaddr_t va = (vaddr_t)&pp->pp_flush[0x40];
614 
615 			/*
616 			 * Initialize the strbuf_ctl.
617 			 *
618 			 * The flush sync buffer must be 64-byte aligned.
619 			 */
620 			sb->sb_flush = (void *)(va & ~0x3f);
621 
622 			bus_space_subregion(sc->sc_bustag, pci_ctl,
623 				offsetof(struct pci_ctl, pci_strbuf),
624 				sizeof (struct iommu_strbuf), &sb->sb_sb);
625 
626 			/* Point our iommu at the strbuf_ctl */
627 			sc->sc_is->is_sb[1] = sb;
628 		}
629 		iommu_reset(sc->sc_is);
630 	}
631 
632 	dict = device_properties(self);
633 	pr = get_psychorange(pp, 2);	/* memory range */
634 #ifdef DEBUG
635 	printf("memory range: %08x %08x\n", pr->phys_hi, pr->phys_lo);
636 #endif
637 	mem_base = ((uint64_t)pr->phys_hi) << 32 | pr->phys_lo;
638 	prop_dictionary_set_uint64(dict, "mem_base", mem_base);
639 
640 	/*
641 	 * attach the pci.. note we pass PCI A tags, etc., for the sabre here.
642 	 */
643 	pba.pba_flags = sc->sc_psycho_this->pp_flags;
644 	pba.pba_dmat = sc->sc_psycho_this->pp_dmat;
645 	pba.pba_dmat64 = NULL;
646 	pba.pba_iot = sc->sc_psycho_this->pp_iot;
647 	pba.pba_memt = sc->sc_psycho_this->pp_memt;
648 
649 	config_found_ia(self, "pcibus", &pba, psycho_print);
650 }
651 
652 static	int
653 psycho_print(void *aux, const char *p)
654 {
655 
656 	if (p == NULL)
657 		return (UNCONF);
658 	return (QUIET);
659 }
660 
661 static void
662 psycho_set_intr(struct psycho_softc *sc, int ipl, void *handler,
663 	uint64_t *mapper, uint64_t *clearer)
664 {
665 	struct intrhand *ih;
666 
667 	ih = (struct intrhand *)malloc(sizeof(struct intrhand),
668 		M_DEVBUF, M_NOWAIT);
669 	ih->ih_arg = sc;
670 	ih->ih_map = mapper;
671 	ih->ih_clr = clearer;
672 	ih->ih_fun = handler;
673 	ih->ih_pil = (1<<ipl);
674 	ih->ih_number = INTVEC(*(ih->ih_map));
675 	intr_establish(ipl, ih);
676 	*(ih->ih_map) |= INTMAP_V|(CPU_UPAID << INTMAP_TID_SHIFT);
677 }
678 
679 /*
680  * power button handlers
681  */
682 static void
683 psycho_register_power_button(struct psycho_softc *sc)
684 {
685 	sysmon_task_queue_init();
686 
687 	sc->sc_powerpressed = 0;
688 	sc->sc_smcontext = malloc(sizeof(struct sysmon_pswitch), M_DEVBUF, 0);
689 	if (!sc->sc_smcontext) {
690 		printf("%s: could not allocate power button context\n",
691 		    sc->sc_dev.dv_xname);
692 		return;
693 	}
694 	memset(sc->sc_smcontext, 0, sizeof(struct sysmon_pswitch));
695 	sc->sc_smcontext->smpsw_name = sc->sc_dev.dv_xname;
696 	sc->sc_smcontext->smpsw_type = PSWITCH_TYPE_POWER;
697 	if (sysmon_pswitch_register(sc->sc_smcontext) != 0)
698 		printf("%s: unable to register power button with sysmon\n",
699 		    sc->sc_dev.dv_xname);
700 }
701 
702 static void
703 psycho_power_button_pressed(void *arg)
704 {
705 	struct psycho_softc *sc = arg;
706 
707 	sysmon_pswitch_event(sc->sc_smcontext, PSWITCH_EVENT_PRESSED);
708 	sc->sc_powerpressed = 0;
709 }
710 
711 /*
712  * PCI bus support
713  */
714 
715 /*
716  * allocate a PCI chipset tag and set it's cookie.
717  */
718 static pci_chipset_tag_t
719 psycho_alloc_chipset(struct psycho_pbm *pp, int node, pci_chipset_tag_t pc)
720 {
721 	pci_chipset_tag_t npc;
722 
723 	npc = malloc(sizeof *npc, M_DEVBUF, M_NOWAIT);
724 	if (npc == NULL)
725 		panic("could not allocate pci_chipset_tag_t");
726 	memcpy(npc, pc, sizeof *pc);
727 	npc->cookie = pp;
728 	npc->rootnode = node;
729 
730 	return (npc);
731 }
732 
733 /*
734  * create extent for free bus space, then allocate assigned regions.
735  */
736 static struct extent *
737 psycho_alloc_extent(struct psycho_pbm *pp, int node, int ss, const char *name)
738 {
739 	struct psycho_registers *pa = NULL;
740 	struct psycho_ranges *pr;
741 	struct extent *ex;
742 	bus_addr_t baddr, addr;
743 	bus_size_t bsize, size;
744 	int i, num;
745 
746 	/* get bus space size */
747 	pr = get_psychorange(pp, ss);
748 	if (pr == NULL) {
749 		printf("psycho_alloc_extent: get_psychorange failed\n");
750 		return NULL;
751 	}
752 	baddr = 0x00000000;
753 	bsize = BUS_ADDR(pr->size_hi, pr->size_lo);
754 
755 	/* get available lists */
756 	num = 0;
757 	if (prom_getprop(node, "available", sizeof(*pa), &num, &pa)) {
758 		printf("psycho_alloc_extent: prom_getprop failed\n");
759 		return NULL;
760 	}
761 
762 	/* create extent */
763 	ex = extent_create(name, baddr, bsize - baddr - 1, M_DEVBUF, 0, 0,
764 			   EX_NOWAIT);
765 	if (ex == NULL) {
766 		printf("psycho_alloc_extent: extent_create failed\n");
767 		goto ret;
768 	}
769 
770 	/* allocate assigned regions */
771 	for (i = 0; i < num; i++)
772 		if (((pa[i].phys_hi >> 24) & 0x03) == ss) {
773 			/* allocate bus space */
774 			addr = BUS_ADDR(pa[i].phys_mid, pa[i].phys_lo);
775 			size = BUS_ADDR(pa[i].size_hi, pa[i].size_lo);
776 			if (extent_alloc_region(ex, baddr, addr - baddr,
777 						EX_NOWAIT)) {
778 				printf("psycho_alloc_extent: "
779 				       "extent_alloc_region %" PRIx64 "-%"
780 				       PRIx64 " failed\n", baddr, addr);
781 				extent_destroy(ex);
782 				ex = NULL;
783 				goto ret;
784 			}
785 			baddr = addr + size;
786 		}
787 	/* allocate left region if available */
788 	if (baddr < bsize)
789 		if (extent_alloc_region(ex, baddr, bsize - baddr, EX_NOWAIT)) {
790 			printf("psycho_alloc_extent: extent_alloc_region %"
791 			       PRIx64 "-%" PRIx64 " failed\n", baddr, bsize);
792 			extent_destroy(ex);
793 			ex = NULL;
794 			goto ret;
795 		}
796 
797 #ifdef DEBUG
798 	/* print extent */
799 	extent_print(ex);
800 #endif
801 
802 ret:
803 	/* return extent */
804 	free(pa, M_DEVBUF);
805 	return ex;
806 }
807 
808 /*
809  * grovel the OBP for various psycho properties
810  */
811 static void
812 psycho_get_bus_range(int node, int *brp)
813 {
814 	int n, error;
815 
816 	n = 2;
817 	error = prom_getprop(node, "bus-range", sizeof(*brp), &n, &brp);
818 	if (error)
819 		panic("could not get psycho bus-range, error %d", error);
820 	if (n != 2)
821 		panic("broken psycho bus-range");
822 	DPRINTF(PDB_PROM, ("psycho debug: got `bus-range' for node %08x: %u - %u\n",
823 			   node, brp[0], brp[1]));
824 }
825 
826 static void
827 psycho_get_ranges(int node, struct psycho_ranges **rp, int *np)
828 {
829 
830 	if (prom_getprop(node, "ranges", sizeof(**rp), np, rp))
831 		panic("could not get psycho ranges");
832 	DPRINTF(PDB_PROM, ("psycho debug: got `ranges' for node %08x: %d entries\n", node, *np));
833 }
834 
835 /*
836  * Interrupt handlers.
837  */
838 
839 static int
840 psycho_ue(void *arg)
841 {
842 	struct psycho_softc *sc = (struct psycho_softc *)arg;
843 	struct psychoreg *regs = sc->sc_regs;
844 	long long afsr = regs->psy_ue_afsr;
845 	long long afar = regs->psy_ue_afar;
846 	long size = PAGE_SIZE<<(sc->sc_is->is_tsbsize);
847 	struct iommu_state *is = sc->sc_is;
848 	char bits[128];
849 
850 	/*
851 	 * It's uncorrectable.  Dump the regs and panic.
852 	 */
853 	printf("%s: uncorrectable DMA error AFAR %llx pa %llx AFSR %llx:\n%s\n",
854 		sc->sc_dev.dv_xname, afar,
855 		(long long)iommu_extract(is, (vaddr_t)afar), afsr,
856 		bitmask_snprintf(afsr, PSYCHO_UE_AFSR_BITS,
857 			bits, sizeof(bits)));
858 
859 	/* Sometimes the AFAR points to an IOTSB entry */
860 	if (afar >= is->is_ptsb && afar < is->is_ptsb + size) {
861 		printf("IOVA %llx IOTTE %llx\n",
862 			(long long)((afar - is->is_ptsb) * PAGE_SIZE + is->is_dvmabase),
863 			(long long)ldxa(afar, ASI_PHYS_CACHED));
864 	}
865 #ifdef DDB
866 	Debugger();
867 #endif
868 	regs->psy_ue_afar = 0;
869 	regs->psy_ue_afsr = 0;
870 	return (1);
871 }
872 static int
873 psycho_ce(void *arg)
874 {
875 	struct psycho_softc *sc = (struct psycho_softc *)arg;
876 	struct psychoreg *regs = sc->sc_regs;
877 
878 	/*
879 	 * It's correctable.  Dump the regs and continue.
880 	 */
881 
882 	printf("%s: correctable DMA error AFAR %llx AFSR %llx\n",
883 		sc->sc_dev.dv_xname,
884 		(long long)regs->psy_ce_afar, (long long)regs->psy_ce_afsr);
885 	return (1);
886 }
887 static int
888 psycho_bus_a(void *arg)
889 {
890 	struct psycho_softc *sc = (struct psycho_softc *)arg;
891 	struct psychoreg *regs = sc->sc_regs;
892 
893 	/*
894 	 * It's uncorrectable.  Dump the regs and panic.
895 	 */
896 
897 	panic("%s: PCI bus A error AFAR %llx AFSR %llx",
898 		sc->sc_dev.dv_xname,
899 		(long long)regs->psy_pcictl[0].pci_afar,
900 		(long long)regs->psy_pcictl[0].pci_afsr);
901 	return (1);
902 }
903 static int
904 psycho_bus_b(void *arg)
905 {
906 	struct psycho_softc *sc = (struct psycho_softc *)arg;
907 	struct psychoreg *regs = sc->sc_regs;
908 
909 	/*
910 	 * It's uncorrectable.  Dump the regs and panic.
911 	 */
912 
913 	panic("%s: PCI bus B error AFAR %llx AFSR %llx",
914 		sc->sc_dev.dv_xname,
915 		(long long)regs->psy_pcictl[0].pci_afar,
916 		(long long)regs->psy_pcictl[0].pci_afsr);
917 	return (1);
918 }
919 
920 static int
921 psycho_powerfail(void *arg)
922 {
923 	struct psycho_softc *sc = (struct psycho_softc *)arg;
924 
925 	/*
926 	 * We lost power. Queue a callback with thread context to
927 	 * handle all the real work.
928 	 */
929 	if (sc->sc_powerpressed == 0 && sc->sc_smcontext != NULL) {
930 		sc->sc_powerpressed = 1;
931 		sysmon_task_queue_sched(0, psycho_power_button_pressed, sc);
932 	}
933 	return (1);
934 }
935 
936 static
937 int psycho_wakeup(void *arg)
938 {
939 	struct psycho_softc *sc = (struct psycho_softc *)arg;
940 
941 	/*
942 	 * Gee, we don't really have a framework to deal with this
943 	 * properly.
944 	 */
945 	printf("%s: power management wakeup\n",	sc->sc_dev.dv_xname);
946 	return (1);
947 }
948 
949 
950 
951 /*
952  * initialise the IOMMU..
953  */
954 void
955 psycho_iommu_init(struct psycho_softc *sc, int tsbsize)
956 {
957 	char *name;
958 	struct iommu_state *is = sc->sc_is;
959 	uint32_t iobase = -1;
960 	int *vdma = NULL;
961 	int nitem;
962 
963 	/* punch in our copies */
964 	is->is_bustag = sc->sc_bustag;
965 	bus_space_subregion(sc->sc_bustag, sc->sc_bh,
966 		offsetof(struct psychoreg, psy_iommu),
967 		sizeof (struct iommureg),
968 		&is->is_iommu);
969 
970 	/*
971 	 * Separate the men from the boys.  Get the `virtual-dma'
972 	 * property for sabre and use that to make sure the damn
973 	 * iommu works.
974 	 *
975 	 * We could query the `#virtual-dma-size-cells' and
976 	 * `#virtual-dma-addr-cells' and DTRT, but I'm lazy.
977 	 */
978 	nitem = 0;
979 	if (!prom_getprop(sc->sc_node, "virtual-dma", sizeof(vdma), &nitem,
980 		&vdma)) {
981 		/* Damn.  Gotta use these values. */
982 		iobase = vdma[0];
983 #define	TSBCASE(x)	case 1<<((x)+23): tsbsize = (x); break
984 		switch (vdma[1]) {
985 			TSBCASE(1); TSBCASE(2); TSBCASE(3);
986 			TSBCASE(4); TSBCASE(5); TSBCASE(6);
987 		default:
988 			printf("bogus tsb size %x, using 7\n", vdma[1]);
989 			TSBCASE(7);
990 		}
991 #undef TSBCASE
992 	}
993 
994 	/* give us a nice name.. */
995 	name = (char *)malloc(32, M_DEVBUF, M_NOWAIT);
996 	if (name == 0)
997 		panic("couldn't malloc iommu name");
998 	snprintf(name, 32, "%s dvma", sc->sc_dev.dv_xname);
999 
1000 	iommu_init(name, is, tsbsize, iobase);
1001 }
1002 
1003 /*
1004  * below here is bus space and bus DMA support
1005  */
1006 bus_space_tag_t
1007 psycho_alloc_bus_tag(struct psycho_pbm *pp, int type)
1008 {
1009 	struct psycho_softc *sc = pp->pp_sc;
1010 	bus_space_tag_t bt;
1011 
1012 	bt = (bus_space_tag_t)
1013 		malloc(sizeof(struct sparc_bus_space_tag), M_DEVBUF, M_NOWAIT);
1014 	if (bt == NULL)
1015 		panic("could not allocate psycho bus tag");
1016 
1017 	memset(bt, 0, sizeof *bt);
1018 	bt->cookie = pp;
1019 	bt->parent = sc->sc_bustag;
1020 	bt->type = type;
1021 	bt->sparc_bus_map = _psycho_bus_map;
1022 	bt->sparc_bus_mmap = psycho_bus_mmap;
1023 	bt->sparc_intr_establish = psycho_intr_establish;
1024 	return (bt);
1025 }
1026 
1027 bus_dma_tag_t
1028 psycho_alloc_dma_tag(struct psycho_pbm *pp)
1029 {
1030 	struct psycho_softc *sc = pp->pp_sc;
1031 	bus_dma_tag_t dt, pdt = sc->sc_dmatag;
1032 
1033 	dt = (bus_dma_tag_t)
1034 		malloc(sizeof(struct sparc_bus_dma_tag), M_DEVBUF, M_NOWAIT);
1035 	if (dt == NULL)
1036 		panic("could not allocate psycho DMA tag");
1037 
1038 	memset(dt, 0, sizeof *dt);
1039 	dt->_cookie = pp;
1040 	dt->_parent = pdt;
1041 #define PCOPY(x)	dt->x = pdt->x
1042 	PCOPY(_dmamap_create);
1043 	PCOPY(_dmamap_destroy);
1044 	dt->_dmamap_load = psycho_dmamap_load;
1045 	PCOPY(_dmamap_load_mbuf);
1046 	PCOPY(_dmamap_load_uio);
1047 	dt->_dmamap_load_raw = psycho_dmamap_load_raw;
1048 	dt->_dmamap_unload = psycho_dmamap_unload;
1049 	dt->_dmamap_sync = psycho_dmamap_sync;
1050 	dt->_dmamem_alloc = psycho_dmamem_alloc;
1051 	dt->_dmamem_free = psycho_dmamem_free;
1052 	dt->_dmamem_map = psycho_dmamem_map;
1053 	dt->_dmamem_unmap = psycho_dmamem_unmap;
1054 	PCOPY(_dmamem_mmap);
1055 #undef	PCOPY
1056 	return (dt);
1057 }
1058 
1059 /*
1060  * bus space support.  <sparc64/dev/psychoreg.h> has a discussion about
1061  * PCI physical addresses.
1062  */
1063 
1064 static int
1065 get_childspace(int type)
1066 {
1067 	int ss;
1068 
1069 	switch (type) {
1070 	case PCI_CONFIG_BUS_SPACE:
1071 		ss = 0x00;
1072 		break;
1073 	case PCI_IO_BUS_SPACE:
1074 		ss = 0x01;
1075 		break;
1076 	case PCI_MEMORY_BUS_SPACE:
1077 		ss = 0x02;
1078 		break;
1079 #if 0
1080 	/* we don't do 64 bit memory space */
1081 	case PCI_MEMORY64_BUS_SPACE:
1082 		ss = 0x03;
1083 		break;
1084 #endif
1085 	default:
1086 		panic("get_childspace: unknown bus type");
1087 	}
1088 
1089 	return (ss);
1090 }
1091 
1092 static struct psycho_ranges *
1093 get_psychorange(struct psycho_pbm *pp, int ss)
1094 {
1095 	int i;
1096 
1097 	for (i = 0; i < pp->pp_nrange; i++) {
1098 		if (((pp->pp_range[i].cspace >> 24) & 0x03) == ss)
1099 			return (&pp->pp_range[i]);
1100 	}
1101 	/* not found */
1102 	return (NULL);
1103 }
1104 
1105 static int
1106 _psycho_bus_map(bus_space_tag_t t, bus_addr_t offset, bus_size_t size,
1107 	int flags, vaddr_t unused, bus_space_handle_t *hp)
1108 {
1109 	struct psycho_pbm *pp = t->cookie;
1110 	struct psycho_softc *sc = pp->pp_sc;
1111 	struct psycho_ranges *pr;
1112 	bus_addr_t paddr;
1113 	int ss;
1114 
1115 	DPRINTF(PDB_BUSMAP,
1116 		("_psycho_bus_map: type %d off %qx sz %qx flags %d",
1117 			t->type, (unsigned long long)offset,
1118 			(unsigned long long)size, flags));
1119 
1120 	ss = get_childspace(t->type);
1121 	DPRINTF(PDB_BUSMAP, (" cspace %d", ss));
1122 
1123 	pr = get_psychorange(pp, ss);
1124 	if (pr != NULL) {
1125 		paddr = BUS_ADDR(pr->phys_hi, pr->phys_lo + offset);
1126 		DPRINTF(PDB_BUSMAP, ("\n_psycho_bus_map: mapping paddr "
1127 				     "space %lx offset %lx paddr %qx\n",
1128 			       (long)ss, (long)offset,
1129 			       (unsigned long long)paddr));
1130 		return ((*sc->sc_bustag->sparc_bus_map)(t, paddr, size,
1131 			flags, 0, hp));
1132 	}
1133 	DPRINTF(PDB_BUSMAP, (" FAILED\n"));
1134 	return (EINVAL);
1135 }
1136 
1137 static paddr_t
1138 psycho_bus_mmap(bus_space_tag_t t, bus_addr_t paddr, off_t off, int prot,
1139 	int flags)
1140 {
1141 	bus_addr_t offset = paddr;
1142 	struct psycho_pbm *pp = t->cookie;
1143 	struct psycho_softc *sc = pp->pp_sc;
1144 	struct psycho_ranges *pr;
1145 	int ss;
1146 
1147 	ss = get_childspace(t->type);
1148 
1149 	DPRINTF(PDB_BUSMAP, ("_psycho_bus_mmap: prot %x flags %d pa %qx\n",
1150 		prot, flags, (unsigned long long)paddr));
1151 
1152 	pr = get_psychorange(pp, ss);
1153 	if (pr != NULL) {
1154 		paddr = BUS_ADDR(pr->phys_hi, pr->phys_lo + offset);
1155 		DPRINTF(PDB_BUSMAP, ("\n_psycho_bus_mmap: mapping paddr "
1156 				     "space %lx offset %lx paddr %qx\n",
1157 			       (long)ss, (long)offset,
1158 			       (unsigned long long)paddr));
1159 		return (bus_space_mmap(sc->sc_bustag, paddr, off,
1160 				       prot, flags));
1161 	}
1162 
1163 	return (-1);
1164 }
1165 
1166 /*
1167  * Get a PCI offset address from bus_space_handle_t.
1168  */
1169 bus_addr_t
1170 psycho_bus_offset(bus_space_tag_t t, bus_space_handle_t *hp)
1171 {
1172 	struct psycho_pbm *pp = t->cookie;
1173 	struct psycho_ranges *pr;
1174 	bus_addr_t addr, offset;
1175 	vaddr_t va;
1176 	int ss;
1177 
1178 	addr = hp->_ptr;
1179 	ss = get_childspace(t->type);
1180 	DPRINTF(PDB_BUSMAP, ("psycho_bus_offset: type %d addr %" PRIx64
1181 			     " cspace %d", t->type, addr, ss));
1182 
1183 	pr = get_psychorange(pp, ss);
1184 	if (pr != NULL) {
1185 		if (!PHYS_ASI(hp->_asi)) {
1186 			va = trunc_page((vaddr_t)addr);
1187 			if (pmap_extract(pmap_kernel(), va, &addr) == FALSE) {
1188 				DPRINTF(PDB_BUSMAP,
1189 					("\n pmap_extract FAILED\n"));
1190 				return (-1);
1191 			}
1192 			addr += hp->_ptr & PGOFSET;
1193 		}
1194 		offset = BUS_ADDR_PADDR(addr) - pr->phys_lo;
1195 		DPRINTF(PDB_BUSMAP, ("\npsycho_bus_offset: paddr %" PRIx64
1196 				     " offset %" PRIx64 "\n", addr, offset));
1197 		return (offset);
1198 	}
1199 	DPRINTF(PDB_BUSMAP, ("\n FAILED\n"));
1200 	return (-1);
1201 }
1202 
1203 
1204 /*
1205  * install an interrupt handler for a PCI device
1206  */
1207 void *
1208 psycho_intr_establish(bus_space_tag_t t, int ihandle, int level,
1209 	int (*handler)(void *), void *arg, void (*fastvec)(void) /* ignored */)
1210 {
1211 	struct psycho_pbm *pp = t->cookie;
1212 	struct psycho_softc *sc = pp->pp_sc;
1213 	struct intrhand *ih;
1214 	volatile uint64_t *intrmapptr = NULL, *intrclrptr = NULL;
1215 	int64_t imap = 0;
1216 	int ino;
1217 	long vec = INTVEC(ihandle);
1218 
1219 	ih = (struct intrhand *)
1220 		malloc(sizeof(struct intrhand), M_DEVBUF, M_NOWAIT);
1221 	if (ih == NULL)
1222 		return (NULL);
1223 
1224 	/*
1225 	 * Hunt through all the interrupt mapping regs to look for our
1226 	 * interrupt vector.
1227 	 *
1228 	 * XXX We only compare INOs rather than IGNs since the firmware may
1229 	 * not provide the IGN and the IGN is constant for all device on that
1230 	 * PCI controller.  This could cause problems for the FFB/external
1231 	 * interrupt which has a full vector that can be set arbitrarily.
1232 	 */
1233 
1234 	DPRINTF(PDB_INTR, ("\npsycho_intr_establish: ihandle %x vec %lx", ihandle, vec));
1235 	ino = INTINO(vec);
1236 	DPRINTF(PDB_INTR, (" ino %x", ino));
1237 
1238 	/* If the device didn't ask for an IPL, use the one encoded. */
1239 	if (level == IPL_NONE) level = INTLEV(vec);
1240 	/* If it still has no level, print a warning and assign IPL 2 */
1241 	if (level == IPL_NONE) {
1242 		printf("ERROR: no IPL, setting IPL 2.\n");
1243 		level = 2;
1244 	}
1245 
1246 	DPRINTF(PDB_INTR, ("\npsycho: intr %lx: %p\nHunting for IRQ...\n",
1247 	    (long)ino, intrlev[ino]));
1248 
1249  	/*
1250  	 * First look for PCI interrupts, otherwise the PCI A slot 0
1251  	 * INTA# interrupt might match an unused non-PCI (obio)
1252  	 * interrupt.
1253  	 */
1254 	for (intrmapptr = &sc->sc_regs->pcia_slot0_int,
1255 		     intrclrptr = &sc->sc_regs->pcia0_clr_int[0];
1256 	     intrmapptr <= &sc->sc_regs->pcib_slot3_int;
1257 	     intrmapptr++, intrclrptr += 4) {
1258 		if (sc->sc_mode == PSYCHO_MODE_PSYCHO &&
1259 		    (intrmapptr == &sc->sc_regs->pcia_slot2_int ||
1260 		     intrmapptr == &sc->sc_regs->pcia_slot3_int))
1261 			continue;
1262 		if (((*intrmapptr ^ vec) & 0x3c) == 0) {
1263 			intrclrptr += vec & 0x3;
1264 			goto found;
1265 		}
1266 	}
1267 
1268 	/* Now hunt thru obio. */
1269 	for (intrmapptr = &sc->sc_regs->scsi_int_map,
1270 		     intrclrptr = &sc->sc_regs->scsi_clr_int;
1271 	     intrmapptr < &sc->sc_regs->ue_int_map;
1272 	     intrmapptr++, intrclrptr++) {
1273 		if (INTINO(*intrmapptr) == ino)
1274 			goto found;
1275 	}
1276 
1277 	/* Finally check the two FFB slots */
1278 	intrclrptr = NULL; /* XXX? */
1279 	for (intrmapptr = &sc->sc_regs->ffb0_int_map;
1280 	     intrmapptr <= &sc->sc_regs->ffb1_int_map;
1281 	     intrmapptr++) {
1282 		if (INTVEC(*intrmapptr) == ino)
1283 			goto found;
1284 	}
1285 
1286 	printf("Cannot find interrupt vector %lx\n", vec);
1287 	return (NULL);
1288 
1289 found:
1290 	/* Register the map and clear intr registers */
1291 	ih->ih_map = intrmapptr;
1292 	ih->ih_clr = intrclrptr;
1293 
1294 	ih->ih_fun = handler;
1295 	ih->ih_arg = arg;
1296 	ih->ih_pil = level;
1297 	ih->ih_number = ino | sc->sc_ign;
1298 
1299 	DPRINTF(PDB_INTR, (
1300 	    "; installing handler %p arg %p with ino %u pil %u\n",
1301 	    handler, arg, (u_int)ino, (u_int)ih->ih_pil));
1302 
1303 	intr_establish(ih->ih_pil, ih);
1304 
1305 	/*
1306 	 * Enable the interrupt now we have the handler installed.
1307 	 * Read the current value as we can't change it besides the
1308 	 * valid bit so so make sure only this bit is changed.
1309 	 *
1310 	 * XXXX --- we really should use bus_space for this.
1311 	 */
1312 	if (intrmapptr) {
1313 		imap = *intrmapptr;
1314 		DPRINTF(PDB_INTR, ("; read intrmap = %016qx",
1315 			(unsigned long long)imap));
1316 
1317 		/* Enable the interrupt */
1318 		imap |= INTMAP_V|(CPU_UPAID << INTMAP_TID_SHIFT);
1319 		DPRINTF(PDB_INTR, ("; addr of intrmapptr = %p", intrmapptr));
1320 		DPRINTF(PDB_INTR, ("; writing intrmap = %016qx\n",
1321 			(unsigned long long)imap));
1322 		*intrmapptr = imap;
1323 		DPRINTF(PDB_INTR, ("; reread intrmap = %016qx",
1324 			(unsigned long long)(imap = *intrmapptr)));
1325 	}
1326  	if (intrclrptr) {
1327  		/* set state to IDLE */
1328  		*intrclrptr = 0;
1329  	}
1330 	return (ih);
1331 }
1332 
1333 /*
1334  * hooks into the iommu dvma calls.
1335  */
1336 int
1337 psycho_dmamap_load(bus_dma_tag_t t, bus_dmamap_t map, void *buf,
1338 	bus_size_t buflen, struct proc *p, int flags)
1339 {
1340 	struct psycho_pbm *pp = (struct psycho_pbm *)t->_cookie;
1341 
1342 	return (iommu_dvmamap_load(t, &pp->pp_sb, map, buf, buflen, p, flags));
1343 }
1344 
1345 void
1346 psycho_dmamap_unload(bus_dma_tag_t t, bus_dmamap_t map)
1347 {
1348 	struct psycho_pbm *pp = (struct psycho_pbm *)t->_cookie;
1349 
1350 	iommu_dvmamap_unload(t, &pp->pp_sb, map);
1351 }
1352 
1353 int
1354 psycho_dmamap_load_raw(bus_dma_tag_t t, bus_dmamap_t map,
1355 	bus_dma_segment_t *segs, int nsegs, bus_size_t size, int flags)
1356 {
1357 	struct psycho_pbm *pp = (struct psycho_pbm *)t->_cookie;
1358 
1359 	return (iommu_dvmamap_load_raw(t, &pp->pp_sb, map, segs, nsegs, flags, size));
1360 }
1361 
1362 void
1363 psycho_dmamap_sync(bus_dma_tag_t t, bus_dmamap_t map, bus_addr_t offset,
1364 	bus_size_t len, int ops)
1365 {
1366 	struct psycho_pbm *pp = (struct psycho_pbm *)t->_cookie;
1367 
1368 	if (ops & (BUS_DMASYNC_PREREAD|BUS_DMASYNC_PREWRITE)) {
1369 		/* Flush the CPU then the IOMMU */
1370 		bus_dmamap_sync(t->_parent, map, offset, len, ops);
1371 		iommu_dvmamap_sync(t, &pp->pp_sb, map, offset, len, ops);
1372 	}
1373 	if (ops & (BUS_DMASYNC_POSTREAD|BUS_DMASYNC_POSTWRITE)) {
1374 		/* Flush the IOMMU then the CPU */
1375 		iommu_dvmamap_sync(t, &pp->pp_sb, map, offset, len, ops);
1376 		bus_dmamap_sync(t->_parent, map, offset, len, ops);
1377 	}
1378 
1379 }
1380 
1381 int
1382 psycho_dmamem_alloc(bus_dma_tag_t t, bus_size_t size, bus_size_t alignment,
1383 	bus_size_t boundary, bus_dma_segment_t *segs, int nsegs, int *rsegs,
1384 	int flags)
1385 {
1386 	struct psycho_pbm *pp = (struct psycho_pbm *)t->_cookie;
1387 
1388 	return (iommu_dvmamem_alloc(t, &pp->pp_sb, size, alignment, boundary,
1389 	    segs, nsegs, rsegs, flags));
1390 }
1391 
1392 void
1393 psycho_dmamem_free(bus_dma_tag_t t, bus_dma_segment_t *segs, int nsegs)
1394 {
1395 	struct psycho_pbm *pp = (struct psycho_pbm *)t->_cookie;
1396 
1397 	iommu_dvmamem_free(t, &pp->pp_sb, segs, nsegs);
1398 }
1399 
1400 int
1401 psycho_dmamem_map(bus_dma_tag_t t, bus_dma_segment_t *segs, int nsegs,
1402 	size_t size, void **kvap, int flags)
1403 {
1404 	struct psycho_pbm *pp = (struct psycho_pbm *)t->_cookie;
1405 
1406 	return (iommu_dvmamem_map(t, &pp->pp_sb, segs, nsegs, size, kvap, flags));
1407 }
1408 
1409 void
1410 psycho_dmamem_unmap(bus_dma_tag_t t, void *kva, size_t size)
1411 {
1412 	struct psycho_pbm *pp = (struct psycho_pbm *)t->_cookie;
1413 
1414 	iommu_dvmamem_unmap(t, &pp->pp_sb, kva, size);
1415 }
1416