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