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