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