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