Home | History | Annotate | Line # | Download | only in dev
psycho.c revision 1.35
      1 /*	$NetBSD: psycho.c,v 1.35 2001/09/10 16:17:06 eeh 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  * 3. The name of the author may not be used to endorse or promote products
     16  *    derived from this software without specific prior written permission.
     17  *
     18  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
     19  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
     20  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
     21  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
     22  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
     23  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
     24  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
     25  * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
     26  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     27  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     28  * SUCH DAMAGE.
     29  */
     30 
     31 #include "opt_ddb.h"
     32 
     33 /*
     34  * Support for `psycho' and `psycho+' UPA to PCI bridge and
     35  * UltraSPARC IIi and IIe `sabre' PCI controllers.
     36  */
     37 
     38 #undef DEBUG
     39 #define DEBUG
     40 
     41 #ifdef DEBUG
     42 #define PDB_PROM	0x01
     43 #define PDB_BUSMAP	0x02
     44 #define PDB_INTR	0x04
     45 int psycho_debug = 0x0;
     46 #define DPRINTF(l, s)   do { if (psycho_debug & l) printf s; } while (0)
     47 #else
     48 #define DPRINTF(l, s)
     49 #endif
     50 
     51 #include <sys/param.h>
     52 #include <sys/device.h>
     53 #include <sys/errno.h>
     54 #include <sys/extent.h>
     55 #include <sys/malloc.h>
     56 #include <sys/systm.h>
     57 #include <sys/time.h>
     58 #include <sys/reboot.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 
     68 #include <sparc64/dev/iommureg.h>
     69 #include <sparc64/dev/iommuvar.h>
     70 #include <sparc64/dev/psychoreg.h>
     71 #include <sparc64/dev/psychovar.h>
     72 #include <sparc64/sparc64/cache.h>
     73 
     74 #include "ioconf.h"
     75 
     76 static pci_chipset_tag_t psycho_alloc_chipset __P((struct psycho_pbm *, int,
     77 						   pci_chipset_tag_t));
     78 static void psycho_get_bus_range __P((int, int *));
     79 static void psycho_get_ranges __P((int, struct psycho_ranges **, int *));
     80 static void psycho_set_intr __P((struct psycho_softc *, int, void *,
     81 	u_int64_t *, u_int64_t *));
     82 
     83 /* Interrupt handlers */
     84 static int psycho_ue __P((void *));
     85 static int psycho_ce __P((void *));
     86 static int psycho_bus_a __P((void *));
     87 static int psycho_bus_b __P((void *));
     88 static int psycho_powerfail __P((void *));
     89 static int psycho_wakeup __P((void *));
     90 
     91 
     92 /* IOMMU support */
     93 static void psycho_iommu_init __P((struct psycho_softc *, int));
     94 
     95 /*
     96  * bus space and bus dma support for UltraSPARC `psycho'.  note that most
     97  * of the bus dma support is provided by the iommu dvma controller.
     98  */
     99 static int psycho_bus_mmap __P((bus_space_tag_t, bus_type_t, bus_addr_t,
    100 				int, bus_space_handle_t *));
    101 static int _psycho_bus_map __P((bus_space_tag_t, bus_type_t, bus_addr_t,
    102 				bus_size_t, int, vaddr_t,
    103 				bus_space_handle_t *));
    104 static void *psycho_intr_establish __P((bus_space_tag_t, int, int, int,
    105 				int (*) __P((void *)), void *));
    106 
    107 static int psycho_dmamap_load __P((bus_dma_tag_t, bus_dmamap_t, void *,
    108 				   bus_size_t, struct proc *, int));
    109 static void psycho_dmamap_unload __P((bus_dma_tag_t, bus_dmamap_t));
    110 static int psycho_dmamap_load_raw __P((bus_dma_tag_t, bus_dmamap_t,
    111 		    bus_dma_segment_t *, int, bus_size_t, int));
    112 static void psycho_dmamap_sync __P((bus_dma_tag_t, bus_dmamap_t, bus_addr_t,
    113 				    bus_size_t, int));
    114 int psycho_dmamem_alloc __P((bus_dma_tag_t, bus_size_t, bus_size_t, bus_size_t,
    115 			     bus_dma_segment_t *, int, int *, int));
    116 void psycho_dmamem_free __P((bus_dma_tag_t, bus_dma_segment_t *, int));
    117 int psycho_dmamem_map __P((bus_dma_tag_t, bus_dma_segment_t *, int, size_t,
    118 			   caddr_t *, int));
    119 void psycho_dmamem_unmap __P((bus_dma_tag_t, caddr_t, size_t));
    120 
    121 /* base pci_chipset */
    122 extern struct sparc_pci_chipset _sparc_pci_chipset;
    123 
    124 /*
    125  * autoconfiguration
    126  */
    127 static	int	psycho_match __P((struct device *, struct cfdata *, void *));
    128 static	void	psycho_attach __P((struct device *, struct device *, void *));
    129 static	int	psycho_print __P((void *aux, const char *p));
    130 
    131 struct cfattach psycho_ca = {
    132         sizeof(struct psycho_softc), psycho_match, psycho_attach
    133 };
    134 
    135 /*
    136  * "sabre" is the UltraSPARC IIi onboard UPA to PCI bridge.  It manages a
    137  * single PCI bus and does not have a streaming buffer.  It often has an APB
    138  * (advanced PCI bridge) connected to it, which was designed specifically for
    139  * the IIi.  The APB let's the IIi handle two independednt PCI buses, and
    140  * appears as two "simba"'s underneath the sabre.
    141  *
    142  * "psycho" and "psycho+" is a dual UPA to PCI bridge.  It sits on the UPA bus
    143  * and manages two PCI buses.  "psycho" has two 64-bit 33MHz buses, while
    144  * "psycho+" controls both a 64-bit 33Mhz and a 64-bit 66Mhz PCI bus.  You
    145  * will usually find a "psycho+" since I don't think the original "psycho"
    146  * ever shipped, and if it did it would be in the U30.
    147  *
    148  * Each "psycho" PCI bus appears as a separate OFW node, but since they are
    149  * both part of the same IC, they only have a single register space.  As such,
    150  * they need to be configured together, even though the autoconfiguration will
    151  * attach them separately.
    152  *
    153  * On UltraIIi machines, "sabre" itself usually takes pci0, with "simba" often
    154  * as pci1 and pci2, although they have been implemented with other PCI bus
    155  * numbers on some machines.
    156  *
    157  * On UltraII machines, there can be any number of "psycho+" ICs, each
    158  * providing two PCI buses.
    159  *
    160  *
    161  * XXXX The psycho/sabre node has an `interrupts' attribute.  They contain
    162  * the values of the following interrupts in this order:
    163  *
    164  * PCI Bus Error	(30)
    165  * DMA UE		(2e)
    166  * DMA CE		(2f)
    167  * Power Fail		(25)
    168  *
    169  * We really should attach handlers for each.
    170  *
    171  */
    172 
    173 #define	ROM_PCI_NAME		"pci"
    174 
    175 struct psycho_names {
    176 	char *p_name;
    177 	int p_type;
    178 } psycho_names[] = {
    179 	{ "SUNW,psycho",        PSYCHO_MODE_PSYCHO      },
    180 	{ "pci108e,8000",       PSYCHO_MODE_PSYCHO      },
    181 	{ "SUNW,sabre",         PSYCHO_MODE_SABRE       },
    182 	{ "pci108e,a000",       PSYCHO_MODE_SABRE       },
    183 	{ "pci108e,a001",       PSYCHO_MODE_SABRE       },
    184 	{ NULL, 0 }
    185 };
    186 
    187 static	int
    188 psycho_match(parent, match, aux)
    189 	struct device	*parent;
    190 	struct cfdata	*match;
    191 	void		*aux;
    192 {
    193 	struct mainbus_attach_args *ma = aux;
    194 	char *model = getpropstring(ma->ma_node, "model");
    195 	int i;
    196 
    197 	/* match on a name of "pci" and a sabre or a psycho */
    198 	if (strcmp(ma->ma_name, ROM_PCI_NAME) == 0) {
    199 		for (i=0; psycho_names[i].p_name; i++)
    200 			if (strcmp(model, psycho_names[i].p_name) == 0)
    201 				return (1);
    202 
    203 		model = getpropstring(ma->ma_node, "compatible");
    204 		for (i=0; psycho_names[i].p_name; i++)
    205 			if (strcmp(model, psycho_names[i].p_name) == 0)
    206 				return (1);
    207 	}
    208 	return (0);
    209 }
    210 
    211 /*
    212  * SUNW,psycho initialisation ..
    213  *	- find the per-psycho registers
    214  *	- figure out the IGN.
    215  *	- find our partner psycho
    216  *	- configure ourselves
    217  *	- bus range, bus,
    218  *	- get interrupt-map and interrupt-map-mask
    219  *	- setup the chipsets.
    220  *	- if we're the first of the pair, initialise the IOMMU, otherwise
    221  *	  just copy it's tags and addresses.
    222  */
    223 static	void
    224 psycho_attach(parent, self, aux)
    225 	struct device *parent, *self;
    226 	void *aux;
    227 {
    228 	struct psycho_softc *sc = (struct psycho_softc *)self;
    229 	struct psycho_softc *osc = NULL;
    230 	struct psycho_pbm *pp;
    231 	struct pcibus_attach_args pba;
    232 	struct mainbus_attach_args *ma = aux;
    233 	bus_space_handle_t bh;
    234 	u_int64_t csr;
    235 	int psycho_br[2], n, i;
    236 	struct pci_ctl *pci_ctl;
    237 	char *model = getpropstring(ma->ma_node, "model");
    238 
    239 	printf("\n");
    240 
    241 	sc->sc_node = ma->ma_node;
    242 	sc->sc_bustag = ma->ma_bustag;
    243 	sc->sc_dmatag = ma->ma_dmatag;
    244 
    245 	/*
    246 	 * call the model-specific initialisation routine.
    247 	 */
    248 	for (i=0; psycho_names[i].p_name; i++)
    249 		if (strcmp(model, psycho_names[i].p_name) == 0) {
    250 			sc->sc_mode = psycho_names[i].p_type;
    251 			goto found;
    252 		}
    253 
    254 	model = getpropstring(ma->ma_node, "compatible");
    255 	for (i=0; psycho_names[i].p_name; i++)
    256 		if (strcmp(model, psycho_names[i].p_name) == 0) {
    257 			sc->sc_mode = psycho_names[i].p_type;
    258 			goto found;
    259 		}
    260 
    261 	panic("unknown psycho model %s", model);
    262 found:
    263 
    264 	/*
    265 	 * The psycho gets three register banks:
    266 	 * (0) per-PBM configuration and status registers
    267 	 * (1) per-PBM PCI configuration space, containing only the
    268 	 *     PBM 256-byte PCI header
    269 	 * (2) the shared psycho configuration registers (struct psychoreg)
    270 	 *
    271 	 * XXX use the prom address for the psycho registers?  we do so far.
    272 	 */
    273 
    274 	/* Register layouts are different.  stuupid. */
    275 	if (sc->sc_mode == PSYCHO_MODE_PSYCHO) {
    276 		sc->sc_basepaddr = (paddr_t)ma->ma_reg[2].ur_paddr;
    277 
    278 		if (ma->ma_naddress > 2) {
    279 			sc->sc_regs = (struct psychoreg *)
    280 				(u_long)ma->ma_address[2];
    281 			pci_ctl = (struct pci_ctl *)
    282 				(u_long)ma->ma_address[0];
    283 		} else if (ma->ma_nreg > 2) {
    284 			bus_space_handle_t handle;
    285 
    286 			/* We need to map this in ourselves. */
    287 			if (bus_space_map2(sc->sc_bustag, 0,
    288 				ma->ma_reg[2].ur_paddr,
    289 				ma->ma_reg[2].ur_len, 0, NULL, &handle))
    290 				panic("psycho_attach: cannot map regs");
    291 			sc->sc_regs = (struct psychoreg *)(u_long)handle;
    292 
    293 			if (bus_space_map2(sc->sc_bustag, 0,
    294 				ma->ma_reg[0].ur_paddr,
    295 				ma->ma_reg[0].ur_len, 0, NULL, &handle))
    296 				panic("psycho_attach: cannot map ctl");
    297 /* XXX -- this is lost but never unmapped */
    298 			pci_ctl = (struct pci_ctl *)(u_long)handle;
    299 
    300 		} else
    301 			panic("psycho_attach: %d not enough registers",
    302 				ma->ma_nreg);
    303 	} else {
    304 		sc->sc_basepaddr = (paddr_t)ma->ma_reg[0].ur_paddr;
    305 
    306 		if (ma->ma_naddress) {
    307 			sc->sc_regs = (struct psychoreg *)
    308 				(u_long)ma->ma_address[0];
    309 			pci_ctl = (struct pci_ctl *)&sc->sc_regs->psy_pcictl[0];
    310 		} else if (ma->ma_nreg) {
    311 			bus_space_handle_t handle;
    312 
    313 			/* We need to map this in ourselves. */
    314 			if (bus_space_map2(sc->sc_bustag, 0,
    315 				ma->ma_reg[0].ur_paddr,
    316 				ma->ma_reg[0].ur_len, 0, NULL, &handle))
    317 				panic("psycho_attach: cannot map regs");
    318 			sc->sc_regs = (struct psychoreg *)(u_long)handle;
    319 			pci_ctl = (struct pci_ctl *)&sc->sc_regs->psy_pcictl[0];
    320 		} else
    321 			panic("psycho_attach: %d not enough registers",
    322 				ma->ma_nreg);
    323 	}
    324 
    325 	csr = sc->sc_regs->psy_csr;
    326 	sc->sc_ign = 0x7c0; /* APB IGN is always 0x7c */
    327 	if (sc->sc_mode == PSYCHO_MODE_PSYCHO)
    328 		sc->sc_ign = PSYCHO_GCSR_IGN(csr) << 6;
    329 
    330 	printf("%s: impl %d, version %d: ign %x ",
    331 		model, PSYCHO_GCSR_IMPL(csr), PSYCHO_GCSR_VERS(csr),
    332 		sc->sc_ign);
    333 	/*
    334 	 * Match other psycho's that are already configured against
    335 	 * the base physical address. This will be the same for a
    336 	 * pair of devices that share register space.
    337 	 */
    338 	for (n = 0; n < psycho_cd.cd_ndevs; n++) {
    339 
    340 		struct psycho_softc *asc =
    341 			(struct psycho_softc *)psycho_cd.cd_devs[n];
    342 
    343 		if (asc == NULL || asc == sc)
    344 			/* This entry is not there or it is me */
    345 			continue;
    346 
    347 		if (asc->sc_basepaddr != sc->sc_basepaddr)
    348 			/* This is an unrelated psycho */
    349 			continue;
    350 
    351 		/* Found partner */
    352 		osc = asc;
    353 		break;
    354 	}
    355 
    356 
    357 	/* Oh, dear.  OK, lets get started */
    358 
    359 	/*
    360 	 * Setup the PCI control register
    361 	 */
    362 	csr = bus_space_read_8(sc->sc_bustag,
    363 			(bus_space_handle_t)(u_long)&pci_ctl->pci_csr, 0);
    364 	csr |= PCICTL_MRLM |
    365 	       PCICTL_ARB_PARK |
    366 	       PCICTL_ERRINTEN |
    367 	       PCICTL_4ENABLE;
    368 	csr &= ~(PCICTL_SERR |
    369 		 PCICTL_CPU_PRIO |
    370 		 PCICTL_ARB_PRIO |
    371 		 PCICTL_RTRYWAIT);
    372 	bus_space_write_8(sc->sc_bustag,
    373 			(bus_space_handle_t)(u_long)&pci_ctl->pci_csr, 0, csr);
    374 
    375 
    376 	/*
    377 	 * Allocate our psycho_pbm
    378 	 */
    379 	pp = sc->sc_psycho_this = malloc(sizeof *pp, M_DEVBUF, M_NOWAIT);
    380 	if (pp == NULL)
    381 		panic("could not allocate psycho pbm");
    382 
    383 	memset(pp, 0, sizeof *pp);
    384 
    385 	pp->pp_sc = sc;
    386 
    387 	/* grab the psycho ranges */
    388 	psycho_get_ranges(sc->sc_node, &pp->pp_range, &pp->pp_nrange);
    389 
    390 	/* get the bus-range for the psycho */
    391 	psycho_get_bus_range(sc->sc_node, psycho_br);
    392 
    393 	pba.pba_bus = psycho_br[0];
    394 
    395 	printf("bus range %u to %u", psycho_br[0], psycho_br[1]);
    396 	printf("; PCI bus %d", psycho_br[0]);
    397 
    398 	pp->pp_pcictl = &sc->sc_regs->psy_pcictl[0];
    399 
    400 	/* allocate our tags */
    401 	pp->pp_memt = psycho_alloc_mem_tag(pp);
    402 	pp->pp_iot = psycho_alloc_io_tag(pp);
    403 	pp->pp_dmat = psycho_alloc_dma_tag(pp);
    404 	pp->pp_flags = (pp->pp_memt ? PCI_FLAGS_MEM_ENABLED : 0) |
    405 		       (pp->pp_iot ? PCI_FLAGS_IO_ENABLED : 0);
    406 
    407 	/* allocate a chipset for this */
    408 	pp->pp_pc = psycho_alloc_chipset(pp, sc->sc_node, &_sparc_pci_chipset);
    409 
    410 	/* setup the rest of the psycho pbm */
    411 	pba.pba_pc = psycho_alloc_chipset(pp, sc->sc_node, pp->pp_pc);
    412 
    413 	printf("\n");
    414 
    415 	/*
    416 	 * And finally, if we're a sabre or the first of a pair of psycho's to
    417 	 * arrive here, start up the IOMMU and get a config space tag.
    418 	 */
    419 	if (osc == NULL) {
    420 
    421 		/*
    422 		 * Establish handlers for interesting interrupts....
    423 		 *
    424 		 * XXX We need to remember these and remove this to support
    425 		 * hotplug on the UPA/FHC bus.
    426 		 *
    427 		 * XXX Not all controllers have these, but installing them
    428 		 * is better than trying to sort through this mess.
    429 		 */
    430 		psycho_set_intr(sc, 15, psycho_ue,
    431 			&sc->sc_regs->ue_int_map,
    432 			&sc->sc_regs->ue_clr_int);
    433 		psycho_set_intr(sc, 1, psycho_ce,
    434 			&sc->sc_regs->ce_int_map,
    435 			&sc->sc_regs->ce_clr_int);
    436 		psycho_set_intr(sc, 15, psycho_bus_a,
    437 			&sc->sc_regs->pciaerr_int_map,
    438 			&sc->sc_regs->pciaerr_clr_int);
    439 		psycho_set_intr(sc, 15, psycho_bus_b,
    440 			&sc->sc_regs->pciberr_int_map,
    441 			&sc->sc_regs->pciberr_clr_int);
    442 		psycho_set_intr(sc, 15, psycho_powerfail,
    443 			&sc->sc_regs->power_int_map,
    444 			&sc->sc_regs->power_clr_int);
    445 		psycho_set_intr(sc, 1, psycho_wakeup,
    446 			&sc->sc_regs->pwrmgt_int_map,
    447 			&sc->sc_regs->pwrmgt_clr_int);
    448 
    449 		/*
    450 		 * Setup IOMMU and PCI configuration if we're the first
    451 		 * of a pair of psycho's to arrive here.
    452 		 *
    453 		 * We should calculate a TSB size based on amount of RAM
    454 		 * and number of bus controllers and number an type of
    455 		 * child devices.
    456 		 *
    457 		 * For the moment, 32KB should be more than enough.
    458 		 */
    459 		psycho_iommu_init(sc, 2);
    460 
    461 		sc->sc_configtag = psycho_alloc_config_tag(sc->sc_psycho_this);
    462 		if (bus_space_map2(sc->sc_bustag,
    463 				  PCI_CONFIG_BUS_SPACE,
    464 				  sc->sc_basepaddr + 0x01000000,
    465 				  0x0100000,
    466 				  0,
    467 				  0,
    468 				  &bh))
    469 			panic("could not map psycho PCI configuration space");
    470 		sc->sc_configaddr = (off_t)bh;
    471 	} else {
    472 		/* Just copy IOMMU state, config tag and address */
    473 		sc->sc_is = osc->sc_is;
    474 		sc->sc_configtag = osc->sc_configtag;
    475 		sc->sc_configaddr = osc->sc_configaddr;
    476 	}
    477 
    478 	/*
    479 	 * attach the pci.. note we pass PCI A tags, etc., for the sabre here.
    480 	 */
    481 	pba.pba_busname = "pci";
    482 	pba.pba_flags = sc->sc_psycho_this->pp_flags;
    483 	pba.pba_dmat = sc->sc_psycho_this->pp_dmat;
    484 	pba.pba_iot = sc->sc_psycho_this->pp_iot;
    485 	pba.pba_memt = sc->sc_psycho_this->pp_memt;
    486 
    487 	config_found(self, &pba, psycho_print);
    488 }
    489 
    490 static	int
    491 psycho_print(aux, p)
    492 	void *aux;
    493 	const char *p;
    494 {
    495 
    496 	if (p == NULL)
    497 		return (UNCONF);
    498 	return (QUIET);
    499 }
    500 
    501 static void
    502 psycho_set_intr(sc, ipl, handler, mapper, clearer)
    503 	struct psycho_softc *sc;
    504 	int ipl;
    505 	void *handler;
    506 	u_int64_t *mapper;
    507 	u_int64_t *clearer;
    508 {
    509 	struct intrhand *ih;
    510 
    511 	ih = (struct intrhand *)malloc(sizeof(struct intrhand),
    512 		M_DEVBUF, M_NOWAIT);
    513 	ih->ih_arg = sc;
    514 	ih->ih_map = mapper;
    515 	ih->ih_clr = clearer;
    516 	ih->ih_fun = handler;
    517 	ih->ih_pil = (1<<ipl);
    518 	ih->ih_number = INTVEC(*(ih->ih_map));
    519 	intr_establish(ipl, ih);
    520 	*(ih->ih_map) |= INTMAP_V;
    521 }
    522 
    523 /*
    524  * PCI bus support
    525  */
    526 
    527 /*
    528  * allocate a PCI chipset tag and set it's cookie.
    529  */
    530 static pci_chipset_tag_t
    531 psycho_alloc_chipset(pp, node, pc)
    532 	struct psycho_pbm *pp;
    533 	int node;
    534 	pci_chipset_tag_t pc;
    535 {
    536 	pci_chipset_tag_t npc;
    537 
    538 	npc = malloc(sizeof *npc, M_DEVBUF, M_NOWAIT);
    539 	if (npc == NULL)
    540 		panic("could not allocate pci_chipset_tag_t");
    541 	memcpy(npc, pc, sizeof *pc);
    542 	npc->cookie = pp;
    543 	npc->rootnode = node;
    544 	npc->curnode = node;
    545 
    546 	return (npc);
    547 }
    548 
    549 /*
    550  * grovel the OBP for various psycho properties
    551  */
    552 static void
    553 psycho_get_bus_range(node, brp)
    554 	int node;
    555 	int *brp;
    556 {
    557 	int n;
    558 
    559 	if (getprop(node, "bus-range", sizeof(*brp), &n, (void **)&brp))
    560 		panic("could not get psycho bus-range");
    561 	if (n != 2)
    562 		panic("broken psycho bus-range");
    563 	DPRINTF(PDB_PROM, ("psycho debug: got `bus-range' for node %08x: %u - %u\n", node, brp[0], brp[1]));
    564 }
    565 
    566 static void
    567 psycho_get_ranges(node, rp, np)
    568 	int node;
    569 	struct psycho_ranges **rp;
    570 	int *np;
    571 {
    572 
    573 	if (getprop(node, "ranges", sizeof(**rp), np, (void **)rp))
    574 		panic("could not get psycho ranges");
    575 	DPRINTF(PDB_PROM, ("psycho debug: got `ranges' for node %08x: %d entries\n", node, *np));
    576 }
    577 
    578 /*
    579  * Interrupt handlers.
    580  */
    581 
    582 static int
    583 psycho_ue(arg)
    584 	void *arg;
    585 {
    586 	struct psycho_softc *sc = (struct psycho_softc *)arg;
    587 	struct psychoreg *regs = sc->sc_regs;
    588 
    589 	/*
    590 	 * It's uncorrectable.  Dump the regs and panic.
    591 	 */
    592 
    593 	panic("%s: uncorrectable DMA error AFAR %llx AFSR %llx\n",
    594 		sc->sc_dev.dv_xname,
    595 		(long long)regs->psy_ue_afar, (long long)regs->psy_ue_afsr);
    596 	return (1);
    597 }
    598 static int
    599 psycho_ce(arg)
    600 	void *arg;
    601 {
    602 	struct psycho_softc *sc = (struct psycho_softc *)arg;
    603 	struct psychoreg *regs = sc->sc_regs;
    604 
    605 	/*
    606 	 * It's correctable.  Dump the regs and continue.
    607 	 */
    608 
    609 	printf("%s: correctable DMA error AFAR %llx AFSR %llx\n",
    610 		sc->sc_dev.dv_xname,
    611 		(long long)regs->psy_ce_afar, (long long)regs->psy_ce_afsr);
    612 	return (1);
    613 }
    614 static int
    615 psycho_bus_a(arg)
    616 	void *arg;
    617 {
    618 	struct psycho_softc *sc = (struct psycho_softc *)arg;
    619 	struct psychoreg *regs = sc->sc_regs;
    620 
    621 	/*
    622 	 * It's uncorrectable.  Dump the regs and panic.
    623 	 */
    624 
    625 	panic("%s: PCI bus A error AFAR %llx AFSR %llx\n",
    626 		sc->sc_dev.dv_xname,
    627 		(long long)regs->psy_pcictl[0].pci_afar,
    628 		(long long)regs->psy_pcictl[0].pci_afsr);
    629 	return (1);
    630 }
    631 static int
    632 psycho_bus_b(arg)
    633 	void *arg;
    634 {
    635 	struct psycho_softc *sc = (struct psycho_softc *)arg;
    636 	struct psychoreg *regs = sc->sc_regs;
    637 
    638 	/*
    639 	 * It's uncorrectable.  Dump the regs and panic.
    640 	 */
    641 
    642 	panic("%s: PCI bus B error AFAR %llx AFSR %llx\n",
    643 		sc->sc_dev.dv_xname,
    644 		(long long)regs->psy_pcictl[0].pci_afar,
    645 		(long long)regs->psy_pcictl[0].pci_afsr);
    646 	return (1);
    647 }
    648 static int
    649 psycho_powerfail(arg)
    650 	void *arg;
    651 {
    652 
    653 	/*
    654 	 * We lost power.  Try to shut down NOW.
    655 	 */
    656 	printf("Power Failure Detected: Shutting down NOW.\n");
    657 	cpu_reboot(RB_POWERDOWN|RB_HALT, NULL);
    658 	return (1);
    659 }
    660 static
    661 int psycho_wakeup(arg)
    662 	void *arg;
    663 {
    664 	struct psycho_softc *sc = (struct psycho_softc *)arg;
    665 
    666 	/*
    667 	 * Gee, we don't really have a framework to deal with this
    668 	 * properly.
    669 	 */
    670 	printf("%s: power management wakeup\n",	sc->sc_dev.dv_xname);
    671 	return (1);
    672 }
    673 
    674 
    675 
    676 /*
    677  * initialise the IOMMU..
    678  */
    679 void
    680 psycho_iommu_init(sc, tsbsize)
    681 	struct psycho_softc *sc;
    682 	int tsbsize;
    683 {
    684 	char *name;
    685 	struct iommu_state *is;
    686 	u_int32_t iobase = -1;
    687 	int *vdma = NULL;
    688 	int nitem;
    689 
    690 	is = malloc(sizeof(struct iommu_state), M_DEVBUF, M_NOWAIT);
    691 	if (is == NULL)
    692 		panic("psycho_iommu_init: malloc is");
    693 
    694 	sc->sc_is = is;
    695 
    696 	/* punch in our copies */
    697 	is->is_bustag = sc->sc_bustag;
    698 	is->is_iommu = &sc->sc_regs->psy_iommu;
    699 
    700 	if (getproplen(sc->sc_node, "no-streaming-cache") < 0)
    701 		is->is_sb = 0;
    702 	else
    703 		is->is_sb = &sc->sc_regs->psy_iommu_strbuf;
    704 
    705 	/*
    706 	 * Separate the men from the boys.  Get the `virtual-dma'
    707 	 * property for sabre and use that to make sure the damn
    708 	 * iommu works.
    709 	 *
    710 	 * We could query the `#virtual-dma-size-cells' and
    711 	 * `#virtual-dma-addr-cells' and DTRT, but I'm lazy.
    712 	 */
    713 	if (!getprop(sc->sc_node, "virtual-dma", sizeof(vdma), &nitem,
    714 		(void **)&vdma)) {
    715 		/* Damn.  Gotta use these values. */
    716 		iobase = vdma[0];
    717 #define	TSBCASE(x)	case 1<<((x)+23): tsbsize = (x); break
    718 		switch (vdma[1]) {
    719 			TSBCASE(1); TSBCASE(2); TSBCASE(3);
    720 			TSBCASE(4); TSBCASE(5); TSBCASE(6);
    721 		default:
    722 			printf("bogus tsb size %x, using 7\n", vdma[1]);
    723 			TSBCASE(7);
    724 		}
    725 #undef TSBCASE
    726 	}
    727 
    728 	/* give us a nice name.. */
    729 	name = (char *)malloc(32, M_DEVBUF, M_NOWAIT);
    730 	if (name == 0)
    731 		panic("couldn't malloc iommu name");
    732 	snprintf(name, 32, "%s dvma", sc->sc_dev.dv_xname);
    733 
    734 	iommu_init(name, is, tsbsize, iobase);
    735 }
    736 
    737 /*
    738  * below here is bus space and bus dma support
    739  */
    740 bus_space_tag_t
    741 psycho_alloc_bus_tag(pp, type)
    742 	struct psycho_pbm *pp;
    743 	int type;
    744 {
    745 	struct psycho_softc *sc = pp->pp_sc;
    746 	bus_space_tag_t bt;
    747 
    748 	bt = (bus_space_tag_t)
    749 		malloc(sizeof(struct sparc_bus_space_tag), M_DEVBUF, M_NOWAIT);
    750 	if (bt == NULL)
    751 		panic("could not allocate psycho bus tag");
    752 
    753 	bzero(bt, sizeof *bt);
    754 	bt->cookie = pp;
    755 	bt->parent = sc->sc_bustag;
    756 	bt->type = type;
    757 	bt->sparc_bus_map = _psycho_bus_map;
    758 	bt->sparc_bus_mmap = psycho_bus_mmap;
    759 	bt->sparc_intr_establish = psycho_intr_establish;
    760 	return (bt);
    761 }
    762 
    763 bus_dma_tag_t
    764 psycho_alloc_dma_tag(pp)
    765 	struct psycho_pbm *pp;
    766 {
    767 	struct psycho_softc *sc = pp->pp_sc;
    768 	bus_dma_tag_t dt, pdt = sc->sc_dmatag;
    769 
    770 	dt = (bus_dma_tag_t)
    771 		malloc(sizeof(struct sparc_bus_dma_tag), M_DEVBUF, M_NOWAIT);
    772 	if (dt == NULL)
    773 		panic("could not allocate psycho dma tag");
    774 
    775 	bzero(dt, sizeof *dt);
    776 	dt->_cookie = pp;
    777 	dt->_parent = pdt;
    778 #define PCOPY(x)	dt->x = pdt->x
    779 	PCOPY(_dmamap_create);
    780 	PCOPY(_dmamap_destroy);
    781 	dt->_dmamap_load = psycho_dmamap_load;
    782 	PCOPY(_dmamap_load_mbuf);
    783 	PCOPY(_dmamap_load_uio);
    784 	dt->_dmamap_load_raw = psycho_dmamap_load_raw;
    785 	dt->_dmamap_unload = psycho_dmamap_unload;
    786 	dt->_dmamap_sync = psycho_dmamap_sync;
    787 	dt->_dmamem_alloc = psycho_dmamem_alloc;
    788 	dt->_dmamem_free = psycho_dmamem_free;
    789 	dt->_dmamem_map = psycho_dmamem_map;
    790 	dt->_dmamem_unmap = psycho_dmamem_unmap;
    791 	PCOPY(_dmamem_mmap);
    792 #undef	PCOPY
    793 	return (dt);
    794 }
    795 
    796 /*
    797  * bus space support.  <sparc64/dev/psychoreg.h> has a discussion about
    798  * PCI physical addresses.
    799  */
    800 
    801 static int get_childspace __P((int));
    802 
    803 static int
    804 get_childspace(type)
    805 	int type;
    806 {
    807 	int ss;
    808 
    809 	switch (type) {
    810 	case PCI_CONFIG_BUS_SPACE:
    811 		ss = 0x00;
    812 		break;
    813 	case PCI_IO_BUS_SPACE:
    814 		ss = 0x01;
    815 		break;
    816 	case PCI_MEMORY_BUS_SPACE:
    817 		ss = 0x02;
    818 		break;
    819 #if 0
    820 	/* we don't do 64 bit memory space */
    821 	case PCI_MEMORY64_BUS_SPACE:
    822 		ss = 0x03;
    823 		break;
    824 #endif
    825 	default:
    826 		panic("get_childspace: unknown bus type");
    827 	}
    828 
    829 	return (ss);
    830 }
    831 
    832 static int
    833 _psycho_bus_map(t, btype, offset, size, flags, vaddr, hp)
    834 	bus_space_tag_t t;
    835 	bus_type_t btype;
    836 	bus_addr_t offset;
    837 	bus_size_t size;
    838 	int	flags;
    839 	vaddr_t vaddr;
    840 	bus_space_handle_t *hp;
    841 {
    842 	struct psycho_pbm *pp = t->cookie;
    843 	struct psycho_softc *sc = pp->pp_sc;
    844 	int i, ss;
    845 
    846 	DPRINTF(PDB_BUSMAP, ("_psycho_bus_map: type %d off %qx sz %qx flags %d va %p", t->type, (unsigned long long)offset, (unsigned long long)size, flags,
    847 	    (void *)vaddr));
    848 
    849 	ss = get_childspace(t->type);
    850 	DPRINTF(PDB_BUSMAP, (" cspace %d", ss));
    851 
    852 	for (i = 0; i < pp->pp_nrange; i++) {
    853 		bus_addr_t paddr;
    854 
    855 		if (((pp->pp_range[i].cspace >> 24) & 0x03) != ss)
    856 			continue;
    857 
    858 		paddr = pp->pp_range[i].phys_lo + offset;
    859 		paddr |= ((bus_addr_t)pp->pp_range[i].phys_hi<<32);
    860 		DPRINTF(PDB_BUSMAP, ("\n_psycho_bus_map: mapping paddr space %lx offset %lx paddr %qx\n",
    861 			       (long)ss, (long)offset,
    862 			       (unsigned long long)paddr));
    863 		return (bus_space_map2(sc->sc_bustag, t->type, paddr,
    864 					size, flags, vaddr, hp));
    865 	}
    866 	DPRINTF(PDB_BUSMAP, (" FAILED\n"));
    867 	return (EINVAL);
    868 }
    869 
    870 static int
    871 psycho_bus_mmap(t, btype, paddr, flags, hp)
    872 	bus_space_tag_t t;
    873 	bus_type_t btype;
    874 	bus_addr_t paddr;
    875 	int flags;
    876 	bus_space_handle_t *hp;
    877 {
    878 	bus_addr_t offset = paddr;
    879 	struct psycho_pbm *pp = t->cookie;
    880 	struct psycho_softc *sc = pp->pp_sc;
    881 	int i, ss;
    882 
    883 	ss = get_childspace(t->type);
    884 
    885 	DPRINTF(PDB_BUSMAP, ("_psycho_bus_mmap: type %d flags %d pa %qx\n", btype, flags, (unsigned long long)paddr));
    886 
    887 	for (i = 0; i < pp->pp_nrange; i++) {
    888 		bus_addr_t paddr;
    889 
    890 		if (((pp->pp_range[i].cspace >> 24) & 0x03) != ss)
    891 			continue;
    892 
    893 		paddr = pp->pp_range[i].phys_lo + offset;
    894 		paddr |= ((bus_addr_t)pp->pp_range[i].phys_hi<<32);
    895 		DPRINTF(PDB_BUSMAP, ("\n_psycho_bus_mmap: mapping paddr space %lx offset %lx paddr %qx\n",
    896 			       (long)ss, (long)offset,
    897 			       (unsigned long long)paddr));
    898 		return (bus_space_mmap(sc->sc_bustag, 0, paddr,
    899 				       flags, hp));
    900 	}
    901 
    902 	return (-1);
    903 }
    904 
    905 
    906 /*
    907  * install an interrupt handler for a PCI device
    908  */
    909 void *
    910 psycho_intr_establish(t, ihandle, level, flags, handler, arg)
    911 	bus_space_tag_t t;
    912 	int ihandle;
    913 	int level;
    914 	int flags;
    915 	int (*handler) __P((void *));
    916 	void *arg;
    917 {
    918 	struct psycho_pbm *pp = t->cookie;
    919 	struct psycho_softc *sc = pp->pp_sc;
    920 	struct intrhand *ih;
    921 	volatile u_int64_t *intrmapptr = NULL, *intrclrptr = NULL;
    922 	int64_t intrmap = 0;
    923 	int ino;
    924 	long vec = INTVEC(ihandle);
    925 
    926 	ih = (struct intrhand *)
    927 		malloc(sizeof(struct intrhand), M_DEVBUF, M_NOWAIT);
    928 	if (ih == NULL)
    929 		return (NULL);
    930 
    931 	/*
    932 	 * Hunt through all the interrupt mapping regs to look for our
    933 	 * interrupt vector.
    934 	 *
    935 	 * XXX We only compare INOs rather than IGNs since the firmware may
    936 	 * not provide the IGN and the IGN is constant for all device on that
    937 	 * PCI controller.  This could cause problems for the FFB/external
    938 	 * interrupt which has a full vector that can be set arbitrarily.
    939 	 */
    940 
    941 
    942 	DPRINTF(PDB_INTR, ("\npsycho_intr_establish: ihandle %x vec %lx", ihandle, vec));
    943 	ino = INTINO(vec);
    944 	DPRINTF(PDB_INTR, (" ino %x", ino));
    945 
    946 	/* If the device didn't ask for an IPL, use the one encoded. */
    947 	if (level == IPL_NONE) level = INTLEV(vec);
    948 	/* If it still has no level, print a warning and assign IPL 2 */
    949 	if (level == IPL_NONE) {
    950 		printf("ERROR: no IPL, setting IPL 2.\n");
    951 		level = 2;
    952 	}
    953 
    954 	if ((flags & BUS_INTR_ESTABLISH_SOFTINTR) == 0) {
    955 
    956 		DPRINTF(PDB_INTR, ("\npsycho: intr %lx: %p\nHunting for IRQ...\n",
    957 		    (long)ino, intrlev[ino]));
    958 
    959 		/* Hunt thru obio first */
    960 		for (intrmapptr = &sc->sc_regs->scsi_int_map,
    961 			     intrclrptr = &sc->sc_regs->scsi_clr_int;
    962 		     intrmapptr <= &sc->sc_regs->ffb1_int_map;
    963 		     intrmapptr++, intrclrptr++) {
    964 			if (INTINO(*intrmapptr) == ino)
    965 				goto found;
    966 		}
    967 
    968 		/* Now do PCI interrupts */
    969 		for (intrmapptr = &sc->sc_regs->pcia_slot0_int,
    970 			     intrclrptr = &sc->sc_regs->pcia0_clr_int[0];
    971 		     intrmapptr <= &sc->sc_regs->pcib_slot3_int;
    972 		     intrmapptr++, intrclrptr += 4) {
    973 			if (((*intrmapptr ^ vec) & 0x3c) == 0) {
    974 				intrclrptr += vec & 0x3;
    975 				goto found;
    976 			}
    977 		}
    978 		printf("Cannot find interrupt vector %lx\n", vec);
    979 		return (NULL);
    980 
    981 	found:
    982 		/* Register the map and clear intr registers */
    983 		ih->ih_map = intrmapptr;
    984 		ih->ih_clr = intrclrptr;
    985 	}
    986 #ifdef NOT_DEBUG
    987 	if (psycho_debug & PDB_INTR) {
    988 		long i;
    989 
    990 		for (i = 0; i < 500000000; i++)
    991 			continue;
    992 	}
    993 #endif
    994 
    995 	ih->ih_fun = handler;
    996 	ih->ih_arg = arg;
    997 	ih->ih_pil = level;
    998 	ih->ih_number = ino | sc->sc_ign;
    999 
   1000 	DPRINTF(PDB_INTR, (
   1001 	    "; installing handler %p arg %p with ino %u pil %u\n",
   1002 	    handler, arg, (u_int)ino, (u_int)ih->ih_pil));
   1003 
   1004 	intr_establish(ih->ih_pil, ih);
   1005 
   1006 	/*
   1007 	 * Enable the interrupt now we have the handler installed.
   1008 	 * Read the current value as we can't change it besides the
   1009 	 * valid bit so so make sure only this bit is changed.
   1010 	 *
   1011 	 * XXXX --- we really should use bus_space for this.
   1012 	 */
   1013 	if (intrmapptr) {
   1014 		intrmap = *intrmapptr;
   1015 		DPRINTF(PDB_INTR, ("; read intrmap = %016qx",
   1016 			(unsigned long long)intrmap));
   1017 
   1018 		/* Enable the interrupt */
   1019 		intrmap |= INTMAP_V;
   1020 		DPRINTF(PDB_INTR, ("; addr of intrmapptr = %p", intrmapptr));
   1021 		DPRINTF(PDB_INTR, ("; writing intrmap = %016qx\n",
   1022 			(unsigned long long)intrmap));
   1023 		*intrmapptr = intrmap;
   1024 		DPRINTF(PDB_INTR, ("; reread intrmap = %016qx",
   1025 			(unsigned long long)(intrmap = *intrmapptr)));
   1026 	}
   1027 	return (ih);
   1028 }
   1029 
   1030 /*
   1031  * hooks into the iommu dvma calls.
   1032  */
   1033 int
   1034 psycho_dmamap_load(t, map, buf, buflen, p, flags)
   1035 	bus_dma_tag_t t;
   1036 	bus_dmamap_t map;
   1037 	void *buf;
   1038 	bus_size_t buflen;
   1039 	struct proc *p;
   1040 	int flags;
   1041 {
   1042 	struct psycho_pbm *pp = (struct psycho_pbm *)t->_cookie;
   1043 	struct psycho_softc *sc = pp->pp_sc;
   1044 
   1045 	return (iommu_dvmamap_load(t, sc->sc_is, map, buf, buflen, p, flags));
   1046 }
   1047 
   1048 void
   1049 psycho_dmamap_unload(t, map)
   1050 	bus_dma_tag_t t;
   1051 	bus_dmamap_t map;
   1052 {
   1053 	struct psycho_pbm *pp = (struct psycho_pbm *)t->_cookie;
   1054 	struct psycho_softc *sc = pp->pp_sc;
   1055 
   1056 	iommu_dvmamap_unload(t, sc->sc_is, map);
   1057 }
   1058 
   1059 int
   1060 psycho_dmamap_load_raw(t, map, segs, nsegs, size, flags)
   1061 	bus_dma_tag_t t;
   1062 	bus_dmamap_t map;
   1063 	bus_dma_segment_t *segs;
   1064 	int nsegs;
   1065 	bus_size_t size;
   1066 	int flags;
   1067 {
   1068 	struct psycho_pbm *pp = (struct psycho_pbm *)t->_cookie;
   1069 	struct psycho_softc *sc = pp->pp_sc;
   1070 
   1071 	return (iommu_dvmamap_load_raw(t, sc->sc_is, map, segs, nsegs, flags, size));
   1072 }
   1073 
   1074 void
   1075 psycho_dmamap_sync(t, map, offset, len, ops)
   1076 	bus_dma_tag_t t;
   1077 	bus_dmamap_t map;
   1078 	bus_addr_t offset;
   1079 	bus_size_t len;
   1080 	int ops;
   1081 {
   1082 	struct psycho_pbm *pp = (struct psycho_pbm *)t->_cookie;
   1083 	struct psycho_softc *sc = pp->pp_sc;
   1084 
   1085 	if (ops & (BUS_DMASYNC_PREREAD|BUS_DMASYNC_PREWRITE)) {
   1086 		/* Flush the CPU then the IOMMU */
   1087 		bus_dmamap_sync(t->_parent, map, offset, len, ops);
   1088 		iommu_dvmamap_sync(t, sc->sc_is, map, offset, len, ops);
   1089 	}
   1090 	if (ops & (BUS_DMASYNC_POSTREAD|BUS_DMASYNC_POSTWRITE)) {
   1091 		/* Flush the IOMMU then the CPU */
   1092 		iommu_dvmamap_sync(t, sc->sc_is, map, offset, len, ops);
   1093 		bus_dmamap_sync(t->_parent, map, offset, len, ops);
   1094 	}
   1095 
   1096 }
   1097 
   1098 int
   1099 psycho_dmamem_alloc(t, size, alignment, boundary, segs, nsegs, rsegs, flags)
   1100 	bus_dma_tag_t t;
   1101 	bus_size_t size;
   1102 	bus_size_t alignment;
   1103 	bus_size_t boundary;
   1104 	bus_dma_segment_t *segs;
   1105 	int nsegs;
   1106 	int *rsegs;
   1107 	int flags;
   1108 {
   1109 	struct psycho_pbm *pp = (struct psycho_pbm *)t->_cookie;
   1110 	struct psycho_softc *sc = pp->pp_sc;
   1111 
   1112 	return (iommu_dvmamem_alloc(t, sc->sc_is, size, alignment, boundary,
   1113 	    segs, nsegs, rsegs, flags));
   1114 }
   1115 
   1116 void
   1117 psycho_dmamem_free(t, segs, nsegs)
   1118 	bus_dma_tag_t t;
   1119 	bus_dma_segment_t *segs;
   1120 	int nsegs;
   1121 {
   1122 	struct psycho_pbm *pp = (struct psycho_pbm *)t->_cookie;
   1123 	struct psycho_softc *sc = pp->pp_sc;
   1124 
   1125 	iommu_dvmamem_free(t, sc->sc_is, segs, nsegs);
   1126 }
   1127 
   1128 int
   1129 psycho_dmamem_map(t, segs, nsegs, size, kvap, flags)
   1130 	bus_dma_tag_t t;
   1131 	bus_dma_segment_t *segs;
   1132 	int nsegs;
   1133 	size_t size;
   1134 	caddr_t *kvap;
   1135 	int flags;
   1136 {
   1137 	struct psycho_pbm *pp = (struct psycho_pbm *)t->_cookie;
   1138 	struct psycho_softc *sc = pp->pp_sc;
   1139 
   1140 	return (iommu_dvmamem_map(t, sc->sc_is, segs, nsegs, size, kvap, flags));
   1141 }
   1142 
   1143 void
   1144 psycho_dmamem_unmap(t, kva, size)
   1145 	bus_dma_tag_t t;
   1146 	caddr_t kva;
   1147 	size_t size;
   1148 {
   1149 	struct psycho_pbm *pp = (struct psycho_pbm *)t->_cookie;
   1150 	struct psycho_softc *sc = pp->pp_sc;
   1151 
   1152 	iommu_dvmamem_unmap(t, sc->sc_is, kva, size);
   1153 }
   1154