Home | History | Annotate | Line # | Download | only in dev
      1  1.138    rillig /*	$NetBSD: psycho.c,v 1.138 2024/09/08 09:36:49 rillig 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.138    rillig __KERNEL_RCSID(0, "$NetBSD: psycho.c,v 1.138 2024/09/08 09:36:49 rillig Exp $");
     59    1.1       mrg 
     60    1.7       mrg #include "opt_ddb.h"
     61    1.7       mrg 
     62    1.1       mrg /*
     63   1.99  nakayama  * 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.92       mrg #define PDB_INTMAP	0x08
     72   1.92       mrg #define PDB_CONF	0x10
     73  1.114  macallan #define PDB_STICK	0x20
     74    1.3       mrg int psycho_debug = 0x0;
     75    1.1       mrg #define DPRINTF(l, s)   do { if (psycho_debug & l) printf s; } while (0)
     76    1.1       mrg #else
     77    1.1       mrg #define DPRINTF(l, s)
     78    1.1       mrg #endif
     79    1.1       mrg 
     80    1.1       mrg #include <sys/param.h>
     81    1.7       mrg #include <sys/device.h>
     82    1.7       mrg #include <sys/errno.h>
     83    1.1       mrg #include <sys/extent.h>
     84  1.131  nakayama #include <sys/malloc.h>
     85  1.129   thorpej #include <sys/kmem.h>
     86    1.7       mrg #include <sys/systm.h>
     87    1.1       mrg #include <sys/time.h>
     88   1.34       eeh #include <sys/reboot.h>
     89    1.1       mrg 
     90   1.58  nakayama #include <uvm/uvm.h>
     91   1.58  nakayama 
     92    1.1       mrg #define _SPARC_BUS_DMA_PRIVATE
     93  1.108    dyoung #include <sys/bus.h>
     94    1.1       mrg #include <machine/autoconf.h>
     95   1.18       eeh #include <machine/psl.h>
     96    1.1       mrg 
     97    1.1       mrg #include <dev/pci/pcivar.h>
     98    1.1       mrg #include <dev/pci/pcireg.h>
     99   1.60    martin #include <dev/sysmon/sysmon_taskq.h>
    100    1.1       mrg 
    101    1.1       mrg #include <sparc64/dev/iommureg.h>
    102    1.1       mrg #include <sparc64/dev/iommuvar.h>
    103    1.1       mrg #include <sparc64/dev/psychoreg.h>
    104    1.1       mrg #include <sparc64/dev/psychovar.h>
    105    1.1       mrg 
    106    1.8       mrg #include "ioconf.h"
    107    1.8       mrg 
    108   1.77       cdi static pci_chipset_tag_t psycho_alloc_chipset(struct psycho_pbm *, int,
    109   1.77       cdi 	pci_chipset_tag_t);
    110   1.77       cdi static struct extent *psycho_alloc_extent(struct psycho_pbm *, int, int,
    111   1.77       cdi 	const char *);
    112   1.77       cdi static void psycho_get_bus_range(int, int *);
    113  1.116       jdc static void psycho_fixup_bus_range(int, int *);
    114   1.77       cdi static void psycho_get_ranges(int, struct psycho_ranges **, int *);
    115   1.77       cdi static void psycho_set_intr(struct psycho_softc *, int, void *, uint64_t *,
    116   1.77       cdi 	uint64_t *);
    117   1.34       eeh 
    118   1.92       mrg /* chipset handlers */
    119   1.92       mrg static pcireg_t	psycho_pci_conf_read(pci_chipset_tag_t, pcitag_t, int);
    120   1.92       mrg static void	psycho_pci_conf_write(pci_chipset_tag_t, pcitag_t, int,
    121   1.92       mrg 				      pcireg_t);
    122   1.92       mrg static void	*psycho_pci_intr_establish(pci_chipset_tag_t,
    123   1.92       mrg 					   pci_intr_handle_t,
    124   1.92       mrg 					   int, int (*)(void *), void *);
    125  1.105    dyoung static int	psycho_pci_find_ino(const struct pci_attach_args *,
    126   1.92       mrg 				    pci_intr_handle_t *);
    127   1.92       mrg 
    128   1.34       eeh /* Interrupt handlers */
    129   1.77       cdi static int psycho_ue(void *);
    130   1.77       cdi static int psycho_ce(void *);
    131   1.77       cdi static int psycho_bus_a(void *);
    132   1.77       cdi static int psycho_bus_b(void *);
    133   1.77       cdi static int psycho_powerfail(void *);
    134   1.77       cdi static int psycho_wakeup(void *);
    135   1.34       eeh 
    136    1.1       mrg 
    137    1.1       mrg /* IOMMU support */
    138   1.77       cdi static void psycho_iommu_init(struct psycho_softc *, int);
    139    1.1       mrg 
    140    1.7       mrg /*
    141   1.61       wiz  * bus space and bus DMA support for UltraSPARC `psycho'.  note that most
    142   1.61       wiz  * of the bus DMA support is provided by the iommu dvma controller.
    143    1.7       mrg  */
    144   1.77       cdi static struct psycho_ranges *get_psychorange(struct psycho_pbm *, int);
    145   1.58  nakayama 
    146   1.77       cdi static paddr_t psycho_bus_mmap(bus_space_tag_t, bus_addr_t, off_t, int, int);
    147   1.77       cdi static int _psycho_bus_map(bus_space_tag_t, bus_addr_t, bus_size_t, int,
    148   1.77       cdi 	vaddr_t, bus_space_handle_t *);
    149   1.77       cdi static void *psycho_intr_establish(bus_space_tag_t, int, int, int (*)(void *),
    150   1.77       cdi 	void *, void(*)(void));
    151   1.77       cdi 
    152   1.91  nakayama static int psycho_dmamap_create(bus_dma_tag_t, bus_size_t, int, bus_size_t,
    153   1.91  nakayama 	bus_size_t, int, bus_dmamap_t *);
    154   1.90  nakayama static void psycho_sabre_dmamap_sync(bus_dma_tag_t, bus_dmamap_t, bus_addr_t,
    155   1.90  nakayama 	bus_size_t, int);
    156    1.7       mrg 
    157    1.7       mrg /* base pci_chipset */
    158    1.1       mrg extern struct sparc_pci_chipset _sparc_pci_chipset;
    159    1.1       mrg 
    160   1.60    martin /* power button handlers */
    161   1.60    martin static void psycho_register_power_button(struct psycho_softc *sc);
    162   1.60    martin static void psycho_power_button_pressed(void *arg);
    163   1.60    martin 
    164    1.1       mrg /*
    165    1.1       mrg  * autoconfiguration
    166    1.1       mrg  */
    167  1.107  christos static	int	psycho_match(device_t, cfdata_t, void *);
    168  1.107  christos static	void	psycho_attach(device_t, device_t, void *);
    169   1.77       cdi static	int	psycho_print(void *aux, const char *p);
    170    1.1       mrg 
    171  1.107  christos CFATTACH_DECL_NEW(psycho, sizeof(struct psycho_softc),
    172   1.55   thorpej     psycho_match, psycho_attach, NULL, NULL);
    173    1.1       mrg 
    174    1.1       mrg /*
    175   1.34       eeh  * "sabre" is the UltraSPARC IIi onboard UPA to PCI bridge.  It manages a
    176   1.34       eeh  * single PCI bus and does not have a streaming buffer.  It often has an APB
    177   1.34       eeh  * (advanced PCI bridge) connected to it, which was designed specifically for
    178  1.137    andvar  * the IIi.  The APB lets the IIi handle two independent PCI buses, and
    179   1.34       eeh  * appears as two "simba"'s underneath the sabre.
    180   1.34       eeh  *
    181   1.34       eeh  * "psycho" and "psycho+" is a dual UPA to PCI bridge.  It sits on the UPA bus
    182   1.79     lukem  * and manages two PCI buses.  "psycho" has two 64-bit 33 MHz buses, while
    183   1.79     lukem  * "psycho+" controls both a 64-bit 33 MHz and a 64-bit 66 MHz PCI bus.  You
    184   1.34       eeh  * will usually find a "psycho+" since I don't think the original "psycho"
    185   1.99  nakayama  * ever shipped, and if it did it would be in the U30.
    186   1.34       eeh  *
    187   1.34       eeh  * Each "psycho" PCI bus appears as a separate OFW node, but since they are
    188   1.34       eeh  * both part of the same IC, they only have a single register space.  As such,
    189   1.34       eeh  * they need to be configured together, even though the autoconfiguration will
    190   1.34       eeh  * attach them separately.
    191   1.34       eeh  *
    192   1.34       eeh  * On UltraIIi machines, "sabre" itself usually takes pci0, with "simba" often
    193   1.34       eeh  * as pci1 and pci2, although they have been implemented with other PCI bus
    194   1.34       eeh  * numbers on some machines.
    195   1.34       eeh  *
    196   1.34       eeh  * On UltraII machines, there can be any number of "psycho+" ICs, each
    197   1.99  nakayama  * providing two PCI buses.
    198   1.34       eeh  *
    199   1.34       eeh  *
    200   1.34       eeh  * XXXX The psycho/sabre node has an `interrupts' attribute.  They contain
    201   1.34       eeh  * the values of the following interrupts in this order:
    202    1.1       mrg  *
    203   1.34       eeh  * PCI Bus Error	(30)
    204   1.34       eeh  * DMA UE		(2e)
    205   1.34       eeh  * DMA CE		(2f)
    206   1.34       eeh  * Power Fail		(25)
    207   1.34       eeh  *
    208   1.34       eeh  * We really should attach handlers for each.
    209    1.1       mrg  *
    210    1.1       mrg  */
    211   1.35       eeh 
    212    1.1       mrg #define	ROM_PCI_NAME		"pci"
    213   1.35       eeh 
    214   1.35       eeh struct psycho_names {
    215   1.74  christos 	const char *p_name;
    216   1.35       eeh 	int p_type;
    217   1.35       eeh } psycho_names[] = {
    218   1.99  nakayama 	{ "SUNW,psycho",	PSYCHO_MODE_PSYCHO	},
    219   1.99  nakayama 	{ "pci108e,8000",	PSYCHO_MODE_PSYCHO	},
    220   1.99  nakayama 	{ "SUNW,sabre",		PSYCHO_MODE_SABRE	},
    221   1.99  nakayama 	{ "pci108e,a000",	PSYCHO_MODE_SABRE	},
    222   1.99  nakayama 	{ "pci108e,a001",	PSYCHO_MODE_SABRE	},
    223   1.35       eeh 	{ NULL, 0 }
    224   1.35       eeh };
    225    1.1       mrg 
    226  1.114  macallan struct psycho_softc *psycho0 = NULL;
    227  1.114  macallan 
    228    1.1       mrg static	int
    229  1.107  christos psycho_match(device_t parent, cfdata_t match, void *aux)
    230    1.1       mrg {
    231    1.1       mrg 	struct mainbus_attach_args *ma = aux;
    232  1.126    martin 	char *model;
    233   1.35       eeh 	int i;
    234    1.1       mrg 
    235  1.126    martin 	if (ma->ma_node == 0)
    236  1.126    martin 		return 0;	/* no OF node, can't be us */
    237  1.126    martin 
    238  1.126    martin 	model = prom_getpropstring(ma->ma_node, "model");
    239    1.1       mrg 	/* match on a name of "pci" and a sabre or a psycho */
    240   1.35       eeh 	if (strcmp(ma->ma_name, ROM_PCI_NAME) == 0) {
    241   1.35       eeh 		for (i=0; psycho_names[i].p_name; i++)
    242   1.35       eeh 			if (strcmp(model, psycho_names[i].p_name) == 0)
    243   1.35       eeh 				return (1);
    244   1.35       eeh 
    245   1.69        pk 		model = prom_getpropstring(ma->ma_node, "compatible");
    246   1.35       eeh 		for (i=0; psycho_names[i].p_name; i++)
    247   1.35       eeh 			if (strcmp(model, psycho_names[i].p_name) == 0)
    248   1.35       eeh 				return (1);
    249   1.35       eeh 	}
    250    1.1       mrg 	return (0);
    251    1.1       mrg }
    252    1.1       mrg 
    253   1.68    petrov #ifdef DEBUG
    254   1.68    petrov static void psycho_dump_intmap(struct psycho_softc *sc);
    255   1.68    petrov static void
    256   1.68    petrov psycho_dump_intmap(struct psycho_softc *sc)
    257   1.68    petrov {
    258   1.77       cdi 	volatile uint64_t *intrmapptr = NULL;
    259   1.68    petrov 
    260   1.68    petrov 	printf("psycho_dump_intmap: OBIO\n");
    261   1.68    petrov 
    262   1.68    petrov 	for (intrmapptr = &sc->sc_regs->scsi_int_map;
    263   1.68    petrov 	     intrmapptr < &sc->sc_regs->ue_int_map;
    264   1.68    petrov 	     intrmapptr++)
    265   1.71  nakayama 		printf("%p: %llx\n", intrmapptr,
    266   1.71  nakayama 		    (unsigned long long)*intrmapptr);
    267   1.68    petrov 
    268   1.68    petrov 	printf("\tintmap:pci\n");
    269   1.68    petrov 	for (intrmapptr = &sc->sc_regs->pcia_slot0_int;
    270   1.68    petrov 	     intrmapptr <= &sc->sc_regs->pcib_slot3_int;
    271   1.68    petrov 	     intrmapptr++)
    272   1.71  nakayama 		printf("%p: %llx\n", intrmapptr,
    273   1.71  nakayama 		    (unsigned long long)*intrmapptr);
    274   1.68    petrov 
    275   1.68    petrov 	printf("\tintmap:ffb\n");
    276   1.68    petrov 	for (intrmapptr = &sc->sc_regs->ffb0_int_map;
    277   1.68    petrov 	     intrmapptr <= &sc->sc_regs->ffb1_int_map;
    278   1.68    petrov 	     intrmapptr++)
    279   1.71  nakayama 		printf("%p: %llx\n", intrmapptr,
    280   1.71  nakayama 		    (unsigned long long)*intrmapptr);
    281   1.68    petrov }
    282   1.68    petrov #endif
    283   1.68    petrov 
    284   1.34       eeh /*
    285   1.34       eeh  * SUNW,psycho initialisation ..
    286   1.34       eeh  *	- find the per-psycho registers
    287   1.34       eeh  *	- figure out the IGN.
    288   1.34       eeh  *	- find our partner psycho
    289   1.34       eeh  *	- configure ourselves
    290   1.99  nakayama  *	- bus range, bus,
    291   1.34       eeh  *	- get interrupt-map and interrupt-map-mask
    292   1.34       eeh  *	- setup the chipsets.
    293   1.34       eeh  *	- if we're the first of the pair, initialise the IOMMU, otherwise
    294  1.120       snj  *	  just copy its tags and addresses.
    295   1.34       eeh  */
    296    1.1       mrg static	void
    297  1.107  christos psycho_attach(device_t parent, device_t self, void *aux)
    298    1.1       mrg {
    299  1.107  christos 	struct psycho_softc *sc = device_private(self);
    300   1.34       eeh 	struct psycho_softc *osc = NULL;
    301   1.34       eeh 	struct psycho_pbm *pp;
    302   1.47   thorpej 	struct pcibus_attach_args pba;
    303    1.1       mrg 	struct mainbus_attach_args *ma = aux;
    304   1.81  macallan 	struct psycho_ranges *pr;
    305   1.81  macallan 	prop_dictionary_t dict;
    306   1.34       eeh 	bus_space_handle_t bh;
    307   1.81  macallan 	uint64_t csr, mem_base;
    308   1.35       eeh 	int psycho_br[2], n, i;
    309   1.45       eeh 	bus_space_handle_t pci_ctl;
    310   1.69        pk 	char *model = prom_getpropstring(ma->ma_node, "model");
    311    1.1       mrg 
    312   1.84  jmcneill 	aprint_normal("\n");
    313    1.1       mrg 
    314  1.107  christos 	sc->sc_dev = self;
    315    1.1       mrg 	sc->sc_node = ma->ma_node;
    316    1.1       mrg 	sc->sc_bustag = ma->ma_bustag;
    317    1.1       mrg 	sc->sc_dmatag = ma->ma_dmatag;
    318  1.114  macallan 	sc->sc_last_stick = 0;
    319    1.1       mrg 
    320  1.114  macallan 	if (psycho0 == NULL)
    321  1.114  macallan 		psycho0 = sc;
    322  1.114  macallan 	DPRINTF(PDB_STICK, ("init psycho0 %lx\n", (long)sc));
    323    1.1       mrg 	/*
    324   1.45       eeh 	 * Identify the device.
    325    1.1       mrg 	 */
    326   1.35       eeh 	for (i=0; psycho_names[i].p_name; i++)
    327   1.35       eeh 		if (strcmp(model, psycho_names[i].p_name) == 0) {
    328   1.35       eeh 			sc->sc_mode = psycho_names[i].p_type;
    329   1.35       eeh 			goto found;
    330   1.35       eeh 		}
    331   1.35       eeh 
    332   1.69        pk 	model = prom_getpropstring(ma->ma_node, "compatible");
    333   1.35       eeh 	for (i=0; psycho_names[i].p_name; i++)
    334   1.35       eeh 		if (strcmp(model, psycho_names[i].p_name) == 0) {
    335   1.35       eeh 			sc->sc_mode = psycho_names[i].p_type;
    336   1.35       eeh 			goto found;
    337   1.35       eeh 		}
    338   1.34       eeh 
    339   1.35       eeh 	panic("unknown psycho model %s", model);
    340   1.35       eeh found:
    341    1.1       mrg 
    342    1.1       mrg 	/*
    343   1.22        pk 	 * The psycho gets three register banks:
    344   1.22        pk 	 * (0) per-PBM configuration and status registers
    345   1.22        pk 	 * (1) per-PBM PCI configuration space, containing only the
    346   1.22        pk 	 *     PBM 256-byte PCI header
    347   1.22        pk 	 * (2) the shared psycho configuration registers (struct psychoreg)
    348   1.22        pk 	 */
    349   1.34       eeh 
    350   1.34       eeh 	/* Register layouts are different.  stuupid. */
    351   1.34       eeh 	if (sc->sc_mode == PSYCHO_MODE_PSYCHO) {
    352   1.34       eeh 		sc->sc_basepaddr = (paddr_t)ma->ma_reg[2].ur_paddr;
    353   1.34       eeh 
    354   1.34       eeh 		if (ma->ma_naddress > 2) {
    355   1.45       eeh 			sparc_promaddr_to_handle(sc->sc_bustag,
    356   1.45       eeh 				ma->ma_address[2], &sc->sc_bh);
    357   1.45       eeh 			sparc_promaddr_to_handle(sc->sc_bustag,
    358   1.45       eeh 				ma->ma_address[0], &pci_ctl);
    359   1.45       eeh 
    360   1.34       eeh 			sc->sc_regs = (struct psychoreg *)
    361   1.45       eeh 				bus_space_vaddr(sc->sc_bustag, sc->sc_bh);
    362   1.34       eeh 		} else if (ma->ma_nreg > 2) {
    363   1.34       eeh 
    364   1.34       eeh 			/* We need to map this in ourselves. */
    365   1.44       eeh 			if (bus_space_map(sc->sc_bustag,
    366   1.34       eeh 				ma->ma_reg[2].ur_paddr,
    367   1.45       eeh 				ma->ma_reg[2].ur_len, BUS_SPACE_MAP_LINEAR,
    368   1.45       eeh 				&sc->sc_bh))
    369   1.34       eeh 				panic("psycho_attach: cannot map regs");
    370   1.45       eeh 			sc->sc_regs = (struct psychoreg *)
    371   1.45       eeh 				bus_space_vaddr(sc->sc_bustag, sc->sc_bh);
    372   1.34       eeh 
    373   1.44       eeh 			if (bus_space_map(sc->sc_bustag,
    374   1.34       eeh 				ma->ma_reg[0].ur_paddr,
    375   1.45       eeh 				ma->ma_reg[0].ur_len, BUS_SPACE_MAP_LINEAR,
    376   1.45       eeh 				&pci_ctl))
    377   1.34       eeh 				panic("psycho_attach: cannot map ctl");
    378   1.34       eeh 		} else
    379   1.34       eeh 			panic("psycho_attach: %d not enough registers",
    380   1.34       eeh 				ma->ma_nreg);
    381   1.68    petrov 
    382   1.34       eeh 	} else {
    383   1.34       eeh 		sc->sc_basepaddr = (paddr_t)ma->ma_reg[0].ur_paddr;
    384   1.34       eeh 
    385   1.34       eeh 		if (ma->ma_naddress) {
    386   1.45       eeh 			sparc_promaddr_to_handle(sc->sc_bustag,
    387   1.45       eeh 				ma->ma_address[0], &sc->sc_bh);
    388   1.34       eeh 			sc->sc_regs = (struct psychoreg *)
    389   1.45       eeh 				bus_space_vaddr(sc->sc_bustag, sc->sc_bh);
    390  1.114  macallan 
    391   1.45       eeh 			bus_space_subregion(sc->sc_bustag, sc->sc_bh,
    392   1.45       eeh 				offsetof(struct psychoreg,  psy_pcictl),
    393   1.45       eeh 				sizeof(struct pci_ctl), &pci_ctl);
    394   1.34       eeh 		} else if (ma->ma_nreg) {
    395   1.34       eeh 
    396   1.34       eeh 			/* We need to map this in ourselves. */
    397   1.44       eeh 			if (bus_space_map(sc->sc_bustag,
    398   1.34       eeh 				ma->ma_reg[0].ur_paddr,
    399   1.99  nakayama 				ma->ma_reg[0].ur_len, BUS_SPACE_MAP_LINEAR,
    400   1.45       eeh 				&sc->sc_bh))
    401   1.34       eeh 				panic("psycho_attach: cannot map regs");
    402   1.45       eeh 			sc->sc_regs = (struct psychoreg *)
    403   1.45       eeh 				bus_space_vaddr(sc->sc_bustag, sc->sc_bh);
    404   1.45       eeh 
    405   1.45       eeh 			bus_space_subregion(sc->sc_bustag, sc->sc_bh,
    406   1.45       eeh 				offsetof(struct psychoreg,  psy_pcictl),
    407   1.45       eeh 				sizeof(struct pci_ctl), &pci_ctl);
    408   1.34       eeh 		} else
    409   1.34       eeh 			panic("psycho_attach: %d not enough registers",
    410   1.34       eeh 				ma->ma_nreg);
    411   1.34       eeh 	}
    412   1.23        pk 
    413   1.99  nakayama 	csr = bus_space_read_8(sc->sc_bustag, sc->sc_bh,
    414   1.45       eeh 		offsetof(struct psychoreg, psy_csr));
    415   1.34       eeh 	sc->sc_ign = 0x7c0; /* APB IGN is always 0x7c */
    416   1.34       eeh 	if (sc->sc_mode == PSYCHO_MODE_PSYCHO)
    417   1.34       eeh 		sc->sc_ign = PSYCHO_GCSR_IGN(csr) << 6;
    418   1.24        pk 
    419   1.84  jmcneill 	aprint_normal_dev(self, "%s: impl %d, version %d: ign %x ",
    420   1.34       eeh 		model, PSYCHO_GCSR_IMPL(csr), PSYCHO_GCSR_VERS(csr),
    421   1.34       eeh 		sc->sc_ign);
    422   1.22        pk 	/*
    423   1.24        pk 	 * Match other psycho's that are already configured against
    424   1.24        pk 	 * the base physical address. This will be the same for a
    425   1.24        pk 	 * pair of devices that share register space.
    426    1.1       mrg 	 */
    427    1.3       mrg 	for (n = 0; n < psycho_cd.cd_ndevs; n++) {
    428    1.8       mrg 
    429   1.88    cegger 		struct psycho_softc *asc = device_lookup_private(&psycho_cd, n);
    430    1.3       mrg 
    431   1.24        pk 		if (asc == NULL || asc == sc)
    432   1.24        pk 			/* This entry is not there or it is me */
    433   1.24        pk 			continue;
    434   1.23        pk 
    435   1.24        pk 		if (asc->sc_basepaddr != sc->sc_basepaddr)
    436   1.24        pk 			/* This is an unrelated psycho */
    437    1.3       mrg 			continue;
    438    1.3       mrg 
    439   1.24        pk 		/* Found partner */
    440   1.24        pk 		osc = asc;
    441    1.8       mrg 		break;
    442    1.8       mrg 	}
    443    1.8       mrg 
    444    1.3       mrg 
    445    1.3       mrg 	/* Oh, dear.  OK, lets get started */
    446    1.3       mrg 
    447   1.24        pk 	/*
    448   1.24        pk 	 * Setup the PCI control register
    449   1.24        pk 	 */
    450   1.99  nakayama 	csr = bus_space_read_8(sc->sc_bustag, pci_ctl,
    451   1.45       eeh 		offsetof(struct pci_ctl, pci_csr));
    452    1.8       mrg 	csr |= PCICTL_MRLM |
    453    1.8       mrg 	       PCICTL_ARB_PARK |
    454    1.8       mrg 	       PCICTL_ERRINTEN |
    455    1.8       mrg 	       PCICTL_4ENABLE;
    456    1.8       mrg 	csr &= ~(PCICTL_SERR |
    457    1.8       mrg 		 PCICTL_CPU_PRIO |
    458    1.8       mrg 		 PCICTL_ARB_PRIO |
    459    1.8       mrg 		 PCICTL_RTRYWAIT);
    460   1.45       eeh 	bus_space_write_8(sc->sc_bustag, pci_ctl,
    461   1.45       eeh 		offsetof(struct pci_ctl, pci_csr), csr);
    462    1.8       mrg 
    463   1.24        pk 
    464   1.24        pk 	/*
    465   1.24        pk 	 * Allocate our psycho_pbm
    466   1.24        pk 	 */
    467  1.129   thorpej 	pp = sc->sc_psycho_this = kmem_zalloc(sizeof *pp, KM_SLEEP);
    468   1.22        pk 	pp->pp_sc = sc;
    469    1.8       mrg 
    470    1.8       mrg 	/* grab the psycho ranges */
    471   1.22        pk 	psycho_get_ranges(sc->sc_node, &pp->pp_range, &pp->pp_nrange);
    472    1.8       mrg 
    473    1.8       mrg 	/* get the bus-range for the psycho */
    474    1.8       mrg 	psycho_get_bus_range(sc->sc_node, psycho_br);
    475    1.8       mrg 
    476   1.47   thorpej 	pba.pba_bus = psycho_br[0];
    477   1.48       eeh 	pba.pba_bridgetag = NULL;
    478    1.8       mrg 
    479  1.116       jdc 	/* Fix up invalid 0x00-0xff bus-range, as found on SPARCle */
    480  1.116       jdc 	if (psycho_br[0] == 0 && psycho_br[1] == 0xff)
    481  1.116       jdc 		psycho_fixup_bus_range(sc->sc_node, psycho_br);
    482  1.116       jdc 
    483   1.84  jmcneill 	aprint_normal("bus range %u to %u", psycho_br[0], psycho_br[1]);
    484   1.84  jmcneill 	aprint_normal("; PCI bus %d", psycho_br[0]);
    485    1.8       mrg 
    486   1.99  nakayama 	pp->pp_pcictl = pci_ctl;
    487    1.8       mrg 
    488    1.8       mrg 	/* allocate our tags */
    489    1.8       mrg 	pp->pp_memt = psycho_alloc_mem_tag(pp);
    490    1.8       mrg 	pp->pp_iot = psycho_alloc_io_tag(pp);
    491    1.8       mrg 	pp->pp_dmat = psycho_alloc_dma_tag(pp);
    492  1.106    dyoung 	pp->pp_flags = (pp->pp_memt ? PCI_FLAGS_MEM_OKAY : 0) |
    493  1.106    dyoung 		       (pp->pp_iot ? PCI_FLAGS_IO_OKAY : 0);
    494    1.8       mrg 
    495    1.8       mrg 	/* allocate a chipset for this */
    496    1.8       mrg 	pp->pp_pc = psycho_alloc_chipset(pp, sc->sc_node, &_sparc_pci_chipset);
    497   1.93  nakayama 	pp->pp_pc->spc_busmax = psycho_br[1];
    498    1.8       mrg 
    499   1.68    petrov 	switch((ma->ma_reg[0].ur_paddr) & 0xf000) {
    500   1.68    petrov 	case 0x2000:
    501   1.68    petrov 		pp->pp_id = PSYCHO_PBM_A;
    502   1.68    petrov 		break;
    503   1.68    petrov 	case 0x4000:
    504   1.68    petrov 		pp->pp_id = PSYCHO_PBM_B;
    505   1.68    petrov 		break;
    506   1.68    petrov 	}
    507   1.68    petrov 
    508   1.84  jmcneill 	aprint_normal("\n");
    509    1.8       mrg 
    510   1.58  nakayama 	/* allocate extents for free bus space */
    511   1.58  nakayama 	pp->pp_exmem = psycho_alloc_extent(pp, sc->sc_node, 0x02, "psycho mem");
    512   1.58  nakayama 	pp->pp_exio = psycho_alloc_extent(pp, sc->sc_node, 0x01, "psycho io");
    513   1.58  nakayama 
    514   1.68    petrov #ifdef DEBUG
    515   1.68    petrov 	if (psycho_debug & PDB_INTR)
    516   1.68    petrov 		psycho_dump_intmap(sc);
    517   1.68    petrov #endif
    518   1.68    petrov 
    519    1.8       mrg 	/*
    520   1.34       eeh 	 * And finally, if we're a sabre or the first of a pair of psycho's to
    521   1.24        pk 	 * arrive here, start up the IOMMU and get a config space tag.
    522    1.8       mrg 	 */
    523   1.24        pk 	if (osc == NULL) {
    524   1.40       eeh 		uint64_t timeo;
    525   1.34       eeh 
    526   1.34       eeh 		/*
    527   1.34       eeh 		 * Establish handlers for interesting interrupts....
    528   1.34       eeh 		 *
    529   1.34       eeh 		 * XXX We need to remember these and remove this to support
    530   1.34       eeh 		 * hotplug on the UPA/FHC bus.
    531   1.34       eeh 		 *
    532   1.34       eeh 		 * XXX Not all controllers have these, but installing them
    533   1.34       eeh 		 * is better than trying to sort through this mess.
    534   1.34       eeh 		 */
    535   1.34       eeh 		psycho_set_intr(sc, 15, psycho_ue,
    536   1.99  nakayama 			&sc->sc_regs->ue_int_map,
    537   1.34       eeh 			&sc->sc_regs->ue_clr_int);
    538   1.34       eeh 		psycho_set_intr(sc, 1, psycho_ce,
    539   1.99  nakayama 			&sc->sc_regs->ce_int_map,
    540   1.34       eeh 			&sc->sc_regs->ce_clr_int);
    541   1.34       eeh 		psycho_set_intr(sc, 15, psycho_bus_a,
    542   1.99  nakayama 			&sc->sc_regs->pciaerr_int_map,
    543   1.34       eeh 			&sc->sc_regs->pciaerr_clr_int);
    544  1.102  nakayama 		/*
    545  1.102  nakayama 		 * Netra X1 may hang when the powerfail interrupt is enabled.
    546  1.102  nakayama 		 */
    547  1.102  nakayama 		if (strcmp(machine_model, "SUNW,UltraAX-i2") != 0) {
    548  1.102  nakayama 			psycho_set_intr(sc, 15, psycho_powerfail,
    549  1.102  nakayama 				&sc->sc_regs->power_int_map,
    550  1.102  nakayama 				&sc->sc_regs->power_clr_int);
    551  1.102  nakayama 			psycho_register_power_button(sc);
    552  1.102  nakayama 		}
    553   1.65    petrov 		if (sc->sc_mode != PSYCHO_MODE_SABRE) {
    554   1.78       wiz 			/* sabre doesn't have these interrupts */
    555   1.65    petrov 			psycho_set_intr(sc, 15, psycho_bus_b,
    556   1.99  nakayama 					&sc->sc_regs->pciberr_int_map,
    557   1.65    petrov 					&sc->sc_regs->pciberr_clr_int);
    558   1.65    petrov 			psycho_set_intr(sc, 1, psycho_wakeup,
    559   1.99  nakayama 					&sc->sc_regs->pwrmgt_int_map,
    560   1.65    petrov 					&sc->sc_regs->pwrmgt_clr_int);
    561   1.65    petrov 		}
    562   1.40       eeh 
    563   1.40       eeh 		/*
    564   1.40       eeh 		 * Apparently a number of machines with psycho and psycho+
    565   1.40       eeh 		 * controllers have interrupt latency issues.  We'll try
    566   1.40       eeh 		 * setting the interrupt retry timeout to 0xff which gives us
    567   1.40       eeh 		 * a retry of 3-6 usec (which is what sysio is set to) for the
    568   1.40       eeh 		 * moment, which seems to help alleviate this problem.
    569   1.40       eeh 		 */
    570   1.45       eeh 		timeo = sc->sc_regs->intr_retry_timer;
    571   1.40       eeh 		if (timeo > 0xfff) {
    572   1.40       eeh #ifdef DEBUG
    573   1.40       eeh 			printf("decreasing interrupt retry timeout "
    574   1.40       eeh 				"from %lx to 0xff\n", (long)timeo);
    575   1.40       eeh #endif
    576   1.45       eeh 			sc->sc_regs->intr_retry_timer = 0xff;
    577   1.40       eeh 		}
    578   1.34       eeh 
    579   1.13       eeh 		/*
    580   1.58  nakayama 		 * Allocate bus node, this contains a prom node per bus.
    581   1.58  nakayama 		 */
    582   1.92       mrg 		pp->pp_pc->spc_busnode =
    583  1.129   thorpej 		    kmem_zalloc(sizeof(*pp->pp_pc->spc_busnode), KM_SLEEP);
    584   1.58  nakayama 
    585   1.58  nakayama 		/*
    586   1.24        pk 		 * Setup IOMMU and PCI configuration if we're the first
    587   1.24        pk 		 * of a pair of psycho's to arrive here.
    588   1.24        pk 		 *
    589   1.13       eeh 		 * We should calculate a TSB size based on amount of RAM
    590  1.138    rillig 		 * and number of bus controllers and number and type of
    591   1.34       eeh 		 * child devices.
    592   1.13       eeh 		 *
    593   1.13       eeh 		 * For the moment, 32KB should be more than enough.
    594   1.13       eeh 		 */
    595  1.129   thorpej 		sc->sc_is = kmem_alloc(sizeof(struct iommu_state), KM_SLEEP);
    596   1.39       eeh 
    597   1.50       eeh 		/* Point the strbuf_ctl at the iommu_state */
    598   1.50       eeh 		pp->pp_sb.sb_is = sc->sc_is;
    599   1.39       eeh 
    600   1.51       eeh 		sc->sc_is->is_sb[0] = sc->sc_is->is_sb[1] = NULL;
    601   1.69        pk 		if (prom_getproplen(sc->sc_node, "no-streaming-cache") < 0) {
    602   1.50       eeh 			struct strbuf_ctl *sb = &pp->pp_sb;
    603   1.50       eeh 			vaddr_t va = (vaddr_t)&pp->pp_flush[0x40];
    604   1.50       eeh 
    605   1.50       eeh 			/*
    606   1.50       eeh 			 * Initialize the strbuf_ctl.
    607   1.99  nakayama 			 *
    608   1.50       eeh 			 * The flush sync buffer must be 64-byte aligned.
    609   1.50       eeh 			 */
    610   1.50       eeh 			sb->sb_flush = (void *)(va & ~0x3f);
    611   1.50       eeh 
    612   1.49       eeh 			bus_space_subregion(sc->sc_bustag, pci_ctl,
    613   1.45       eeh 				offsetof(struct pci_ctl, pci_strbuf),
    614   1.50       eeh 				sizeof (struct iommu_strbuf), &sb->sb_sb);
    615   1.50       eeh 
    616   1.50       eeh 			/* Point our iommu at the strbuf_ctl */
    617   1.50       eeh 			sc->sc_is->is_sb[0] = sb;
    618   1.45       eeh 		}
    619   1.39       eeh 
    620   1.13       eeh 		psycho_iommu_init(sc, 2);
    621    1.8       mrg 
    622    1.8       mrg 		sc->sc_configtag = psycho_alloc_config_tag(sc->sc_psycho_this);
    623   1.44       eeh 
    624   1.99  nakayama 		/*
    625   1.44       eeh 		 * XXX This is a really ugly hack because PCI config space
    626   1.44       eeh 		 * is explicitly handled with unmapped accesses.
    627   1.44       eeh 		 */
    628   1.44       eeh 		i = sc->sc_bustag->type;
    629   1.44       eeh 		sc->sc_bustag->type = PCI_CONFIG_BUS_SPACE;
    630   1.44       eeh 		if (bus_space_map(sc->sc_bustag, sc->sc_basepaddr + 0x01000000,
    631   1.58  nakayama 			0x01000000, 0, &bh))
    632   1.23        pk 			panic("could not map psycho PCI configuration space");
    633   1.44       eeh 		sc->sc_bustag->type = i;
    634   1.45       eeh 		sc->sc_configaddr = bh;
    635    1.8       mrg 	} else {
    636   1.58  nakayama 		/* Share bus numbers with the pair of mine */
    637   1.92       mrg 		pp->pp_pc->spc_busnode =
    638   1.92       mrg 		    osc->sc_psycho_this->pp_pc->spc_busnode;
    639   1.58  nakayama 
    640   1.24        pk 		/* Just copy IOMMU state, config tag and address */
    641   1.24        pk 		sc->sc_is = osc->sc_is;
    642    1.8       mrg 		sc->sc_configtag = osc->sc_configtag;
    643    1.8       mrg 		sc->sc_configaddr = osc->sc_configaddr;
    644   1.39       eeh 
    645   1.50       eeh 		/* Point the strbuf_ctl at the iommu_state */
    646   1.50       eeh 		pp->pp_sb.sb_is = sc->sc_is;
    647   1.50       eeh 
    648   1.69        pk 		if (prom_getproplen(sc->sc_node, "no-streaming-cache") < 0) {
    649   1.50       eeh 			struct strbuf_ctl *sb = &pp->pp_sb;
    650   1.50       eeh 			vaddr_t va = (vaddr_t)&pp->pp_flush[0x40];
    651   1.50       eeh 
    652   1.50       eeh 			/*
    653   1.50       eeh 			 * Initialize the strbuf_ctl.
    654   1.99  nakayama 			 *
    655   1.50       eeh 			 * The flush sync buffer must be 64-byte aligned.
    656   1.50       eeh 			 */
    657   1.50       eeh 			sb->sb_flush = (void *)(va & ~0x3f);
    658   1.50       eeh 
    659   1.49       eeh 			bus_space_subregion(sc->sc_bustag, pci_ctl,
    660   1.45       eeh 				offsetof(struct pci_ctl, pci_strbuf),
    661   1.50       eeh 				sizeof (struct iommu_strbuf), &sb->sb_sb);
    662   1.50       eeh 
    663   1.50       eeh 			/* Point our iommu at the strbuf_ctl */
    664   1.50       eeh 			sc->sc_is->is_sb[1] = sb;
    665   1.45       eeh 		}
    666   1.39       eeh 		iommu_reset(sc->sc_is);
    667    1.8       mrg 	}
    668   1.34       eeh 
    669   1.81  macallan 	dict = device_properties(self);
    670   1.81  macallan 	pr = get_psychorange(pp, 2);	/* memory range */
    671   1.81  macallan #ifdef DEBUG
    672   1.81  macallan 	printf("memory range: %08x %08x\n", pr->phys_hi, pr->phys_lo);
    673   1.81  macallan #endif
    674   1.81  macallan 	mem_base = ((uint64_t)pr->phys_hi) << 32 | pr->phys_lo;
    675   1.81  macallan 	prop_dictionary_set_uint64(dict, "mem_base", mem_base);
    676   1.81  macallan 
    677   1.34       eeh 	/*
    678   1.34       eeh 	 * attach the pci.. note we pass PCI A tags, etc., for the sabre here.
    679   1.34       eeh 	 */
    680   1.47   thorpej 	pba.pba_flags = sc->sc_psycho_this->pp_flags;
    681   1.47   thorpej 	pba.pba_dmat = sc->sc_psycho_this->pp_dmat;
    682   1.63      fvdl 	pba.pba_dmat64 = NULL;
    683   1.47   thorpej 	pba.pba_iot = sc->sc_psycho_this->pp_iot;
    684   1.47   thorpej 	pba.pba_memt = sc->sc_psycho_this->pp_memt;
    685   1.93  nakayama 	pba.pba_pc = pp->pp_pc;
    686   1.34       eeh 
    687  1.134   thorpej 	config_found(self, &pba, psycho_print,
    688  1.136   thorpej 	    CFARGS(.devhandle = device_handle(self)));
    689   1.34       eeh }
    690   1.34       eeh 
    691   1.34       eeh static	int
    692   1.77       cdi psycho_print(void *aux, const char *p)
    693   1.34       eeh {
    694   1.34       eeh 
    695   1.34       eeh 	if (p == NULL)
    696   1.34       eeh 		return (UNCONF);
    697   1.34       eeh 	return (QUIET);
    698   1.34       eeh }
    699   1.34       eeh 
    700   1.34       eeh static void
    701   1.77       cdi psycho_set_intr(struct psycho_softc *sc, int ipl, void *handler,
    702   1.77       cdi 	uint64_t *mapper, uint64_t *clearer)
    703   1.34       eeh {
    704   1.34       eeh 	struct intrhand *ih;
    705   1.34       eeh 
    706  1.124     palle 	ih = intrhand_alloc();
    707   1.34       eeh 	ih->ih_arg = sc;
    708   1.34       eeh 	ih->ih_map = mapper;
    709   1.34       eeh 	ih->ih_clr = clearer;
    710   1.34       eeh 	ih->ih_fun = handler;
    711  1.111  nakayama 	ih->ih_pil = ipl;
    712   1.34       eeh 	ih->ih_number = INTVEC(*(ih->ih_map));
    713  1.110       mrg 	ih->ih_pending = 0;
    714   1.86    martin 	intr_establish(ipl, ipl != IPL_VM, ih);
    715   1.76    martin 	*(ih->ih_map) |= INTMAP_V|(CPU_UPAID << INTMAP_TID_SHIFT);
    716    1.1       mrg }
    717    1.1       mrg 
    718    1.1       mrg /*
    719   1.60    martin  * power button handlers
    720   1.60    martin  */
    721   1.60    martin static void
    722   1.60    martin psycho_register_power_button(struct psycho_softc *sc)
    723   1.60    martin {
    724   1.60    martin 	sysmon_task_queue_init();
    725   1.60    martin 
    726   1.60    martin 	sc->sc_powerpressed = 0;
    727  1.129   thorpej 	sc->sc_smcontext = kmem_zalloc(sizeof(struct sysmon_pswitch), KM_SLEEP);
    728  1.107  christos 	sc->sc_smcontext->smpsw_name = device_xname(sc->sc_dev);
    729   1.60    martin 	sc->sc_smcontext->smpsw_type = PSWITCH_TYPE_POWER;
    730   1.60    martin 	if (sysmon_pswitch_register(sc->sc_smcontext) != 0)
    731  1.107  christos 		aprint_error_dev(sc->sc_dev, "unable to register power button with sysmon\n");
    732   1.60    martin }
    733   1.60    martin 
    734   1.60    martin static void
    735   1.60    martin psycho_power_button_pressed(void *arg)
    736   1.60    martin {
    737   1.60    martin 	struct psycho_softc *sc = arg;
    738   1.60    martin 
    739   1.60    martin 	sysmon_pswitch_event(sc->sc_smcontext, PSWITCH_EVENT_PRESSED);
    740   1.60    martin 	sc->sc_powerpressed = 0;
    741   1.60    martin }
    742   1.60    martin 
    743   1.60    martin /*
    744    1.1       mrg  * PCI bus support
    745    1.1       mrg  */
    746    1.1       mrg 
    747    1.1       mrg /*
    748  1.120       snj  * allocate a PCI chipset tag and set its cookie.
    749    1.1       mrg  */
    750    1.1       mrg static pci_chipset_tag_t
    751   1.77       cdi psycho_alloc_chipset(struct psycho_pbm *pp, int node, pci_chipset_tag_t pc)
    752    1.1       mrg {
    753    1.1       mrg 	pci_chipset_tag_t npc;
    754    1.1       mrg 
    755  1.129   thorpej 	npc = kmem_alloc(sizeof *npc, KM_SLEEP);
    756    1.1       mrg 	memcpy(npc, pc, sizeof *pc);
    757    1.1       mrg 	npc->cookie = pp;
    758   1.34       eeh 	npc->rootnode = node;
    759   1.92       mrg 	npc->spc_conf_read = psycho_pci_conf_read;
    760   1.92       mrg 	npc->spc_conf_write = psycho_pci_conf_write;
    761  1.100       mrg 	npc->spc_intr_map = NULL;
    762   1.92       mrg 	npc->spc_intr_establish = psycho_pci_intr_establish;
    763   1.92       mrg 	npc->spc_find_ino = psycho_pci_find_ino;
    764    1.1       mrg 
    765    1.1       mrg 	return (npc);
    766    1.1       mrg }
    767    1.1       mrg 
    768    1.1       mrg /*
    769   1.58  nakayama  * create extent for free bus space, then allocate assigned regions.
    770   1.58  nakayama  */
    771   1.58  nakayama static struct extent *
    772   1.77       cdi psycho_alloc_extent(struct psycho_pbm *pp, int node, int ss, const char *name)
    773   1.58  nakayama {
    774  1.131  nakayama 	struct psycho_registers *pa = NULL;
    775   1.58  nakayama 	struct psycho_ranges *pr;
    776   1.58  nakayama 	struct extent *ex;
    777   1.58  nakayama 	bus_addr_t baddr, addr;
    778   1.58  nakayama 	bus_size_t bsize, size;
    779   1.58  nakayama 	int i, num;
    780   1.58  nakayama 
    781   1.58  nakayama 	/* get bus space size */
    782   1.58  nakayama 	pr = get_psychorange(pp, ss);
    783   1.58  nakayama 	if (pr == NULL) {
    784   1.58  nakayama 		printf("psycho_alloc_extent: get_psychorange failed\n");
    785   1.58  nakayama 		return NULL;
    786   1.58  nakayama 	}
    787   1.58  nakayama 	baddr = 0x00000000;
    788   1.58  nakayama 	bsize = BUS_ADDR(pr->size_hi, pr->size_lo);
    789   1.58  nakayama 
    790   1.58  nakayama 	/* get available lists */
    791   1.70    martin 	num = 0;
    792   1.69        pk 	if (prom_getprop(node, "available", sizeof(*pa), &num, &pa)) {
    793   1.83       jdc 		printf("psycho_alloc_extent: no \"available\" property\n");
    794   1.58  nakayama 		return NULL;
    795   1.58  nakayama 	}
    796   1.58  nakayama 
    797   1.58  nakayama 	/* create extent */
    798  1.112      para 	ex = extent_create(name, baddr, bsize - baddr - 1, 0, 0, EX_NOWAIT);
    799   1.58  nakayama 	if (ex == NULL) {
    800   1.58  nakayama 		printf("psycho_alloc_extent: extent_create failed\n");
    801   1.58  nakayama 		goto ret;
    802   1.58  nakayama 	}
    803   1.58  nakayama 
    804   1.58  nakayama 	/* allocate assigned regions */
    805   1.58  nakayama 	for (i = 0; i < num; i++)
    806   1.58  nakayama 		if (((pa[i].phys_hi >> 24) & 0x03) == ss) {
    807   1.58  nakayama 			/* allocate bus space */
    808   1.58  nakayama 			addr = BUS_ADDR(pa[i].phys_mid, pa[i].phys_lo);
    809   1.58  nakayama 			size = BUS_ADDR(pa[i].size_hi, pa[i].size_lo);
    810   1.58  nakayama 			if (extent_alloc_region(ex, baddr, addr - baddr,
    811   1.58  nakayama 						EX_NOWAIT)) {
    812   1.58  nakayama 				printf("psycho_alloc_extent: "
    813   1.58  nakayama 				       "extent_alloc_region %" PRIx64 "-%"
    814   1.58  nakayama 				       PRIx64 " failed\n", baddr, addr);
    815   1.58  nakayama 				extent_destroy(ex);
    816   1.58  nakayama 				ex = NULL;
    817   1.58  nakayama 				goto ret;
    818   1.58  nakayama 			}
    819   1.58  nakayama 			baddr = addr + size;
    820   1.58  nakayama 		}
    821   1.58  nakayama 	/* allocate left region if available */
    822   1.58  nakayama 	if (baddr < bsize)
    823   1.58  nakayama 		if (extent_alloc_region(ex, baddr, bsize - baddr, EX_NOWAIT)) {
    824   1.58  nakayama 			printf("psycho_alloc_extent: extent_alloc_region %"
    825   1.58  nakayama 			       PRIx64 "-%" PRIx64 " failed\n", baddr, bsize);
    826   1.58  nakayama 			extent_destroy(ex);
    827   1.58  nakayama 			ex = NULL;
    828   1.58  nakayama 			goto ret;
    829   1.58  nakayama 		}
    830   1.58  nakayama 
    831   1.58  nakayama #ifdef DEBUG
    832   1.58  nakayama 	/* print extent */
    833   1.58  nakayama 	extent_print(ex);
    834   1.58  nakayama #endif
    835   1.58  nakayama 
    836   1.58  nakayama ret:
    837   1.58  nakayama 	/* return extent */
    838  1.131  nakayama 	free(pa, M_DEVBUF);
    839   1.58  nakayama 	return ex;
    840   1.58  nakayama }
    841   1.58  nakayama 
    842   1.58  nakayama /*
    843    1.1       mrg  * grovel the OBP for various psycho properties
    844    1.1       mrg  */
    845    1.1       mrg static void
    846   1.77       cdi psycho_get_bus_range(int node, int *brp)
    847    1.1       mrg {
    848   1.70    martin 	int n, error;
    849    1.1       mrg 
    850   1.72  nakayama 	n = 2;
    851   1.70    martin 	error = prom_getprop(node, "bus-range", sizeof(*brp), &n, &brp);
    852   1.70    martin 	if (error)
    853   1.70    martin 		panic("could not get psycho bus-range, error %d", error);
    854    1.1       mrg 	if (n != 2)
    855    1.1       mrg 		panic("broken psycho bus-range");
    856  1.122       mrg 	DPRINTF(PDB_PROM, ("%s: got `bus-range' for node %08x: %u - %u\n",
    857  1.122       mrg 			   __func__, node, brp[0], brp[1]));
    858    1.1       mrg }
    859    1.1       mrg 
    860    1.1       mrg static void
    861  1.116       jdc psycho_fixup_bus_range(int node0, int *brp0)
    862  1.116       jdc {
    863  1.116       jdc 	int node;
    864  1.116       jdc 	int len, busrange[2], *brp;
    865  1.116       jdc 
    866  1.116       jdc 	DPRINTF(PDB_PROM,
    867  1.122       mrg 	    ("%s: fixing up `bus-range' for node %08x: %u - %u\n",
    868  1.122       mrg 	    __func__, node0, brp0[0], brp0[1]));
    869  1.116       jdc 
    870  1.116       jdc 	/*
    871  1.116       jdc 	 * Check all nodes under this one and increase the bus range to
    872  1.116       jdc 	 * match.  Recurse through PCI-PCI bridges.  Cardbus bridges are
    873  1.116       jdc 	 * fixed up in pccbb_attach_hook().  Assumes that "bus-range" for
    874  1.116       jdc 	 * PCI-PCI bridges apart from this one is correct.
    875  1.116       jdc 	 */
    876  1.116       jdc 	brp0[1] = brp0[0];
    877  1.116       jdc 	node = prom_firstchild(node0);
    878  1.116       jdc 	for (node = ((node)); node; node = prom_nextsibling(node)) {
    879  1.116       jdc 		len = 2;
    880  1.116       jdc 		brp = busrange;
    881  1.116       jdc 		if (prom_getprop(node, "bus-range", sizeof(*brp),
    882  1.116       jdc 		    &len, &brp) != 0)
    883  1.116       jdc 			break;
    884  1.116       jdc 		if (len != 2)
    885  1.116       jdc 			break;
    886  1.116       jdc 		psycho_fixup_bus_range(node, busrange);
    887  1.116       jdc 		if (brp0[0] > busrange[0] && busrange[0] >= 0)
    888  1.116       jdc 			brp0[0] = busrange[0];
    889  1.116       jdc 		if (brp0[1] < busrange[1] && busrange[1] < 256)
    890  1.116       jdc 			brp0[1] = busrange[1];
    891  1.116       jdc 	}
    892  1.116       jdc 
    893  1.116       jdc 	DPRINTF(PDB_PROM,
    894  1.122       mrg 	    ("%s: fixed up `bus-range' for node %08x: %u - %u\n",
    895  1.122       mrg 	    __func__, node0, brp[0], brp[1]));
    896  1.116       jdc }
    897  1.116       jdc 
    898  1.116       jdc static void
    899   1.77       cdi psycho_get_ranges(int node, struct psycho_ranges **rp, int *np)
    900    1.1       mrg {
    901    1.1       mrg 
    902   1.69        pk 	if (prom_getprop(node, "ranges", sizeof(**rp), np, rp))
    903    1.1       mrg 		panic("could not get psycho ranges");
    904  1.122       mrg 	DPRINTF(PDB_PROM, ("%s: got `ranges' for node %08x: %d entries\n",
    905  1.122       mrg 			  __func__, node, *np));
    906    1.1       mrg }
    907    1.1       mrg 
    908   1.34       eeh /*
    909   1.34       eeh  * Interrupt handlers.
    910   1.34       eeh  */
    911   1.34       eeh 
    912   1.34       eeh static int
    913   1.77       cdi psycho_ue(void *arg)
    914   1.34       eeh {
    915   1.34       eeh 	struct psycho_softc *sc = (struct psycho_softc *)arg;
    916   1.34       eeh 	struct psychoreg *regs = sc->sc_regs;
    917   1.41       eeh 	struct iommu_state *is = sc->sc_is;
    918   1.98  nakayama 	uint64_t afsr = regs->psy_ue_afsr;
    919   1.98  nakayama 	uint64_t afar = regs->psy_ue_afar;
    920   1.98  nakayama 	psize_t size = PAGE_SIZE << is->is_tsbsize;
    921   1.36       eeh 	char bits[128];
    922   1.34       eeh 
    923   1.34       eeh 	/*
    924   1.34       eeh 	 * It's uncorrectable.  Dump the regs and panic.
    925   1.34       eeh 	 */
    926   1.95  christos 	snprintb(bits, sizeof(bits), PSYCHO_UE_AFSR_BITS, afsr);
    927  1.107  christos 	aprint_error_dev(sc->sc_dev,
    928   1.98  nakayama 	    "uncorrectable DMA error AFAR %" PRIx64 " AFSR %s\n", afar, bits);
    929   1.98  nakayama 
    930   1.41       eeh 	/* Sometimes the AFAR points to an IOTSB entry */
    931   1.41       eeh 	if (afar >= is->is_ptsb && afar < is->is_ptsb + size) {
    932  1.107  christos 		aprint_error_dev(sc->sc_dev,
    933   1.98  nakayama 		    "IOVA %" PRIx64 " IOTTE %" PRIx64 "\n",
    934   1.98  nakayama 		    (afar - is->is_ptsb) / sizeof(is->is_tsb[0]) * PAGE_SIZE
    935   1.98  nakayama 		    + is->is_dvmabase, ldxa(afar, ASI_PHYS_CACHED));
    936   1.41       eeh 	}
    937   1.43       chs #ifdef DDB
    938   1.41       eeh 	Debugger();
    939   1.43       chs #endif
    940   1.41       eeh 	regs->psy_ue_afar = 0;
    941   1.41       eeh 	regs->psy_ue_afsr = 0;
    942   1.34       eeh 	return (1);
    943   1.34       eeh }
    944   1.92       mrg 
    945   1.99  nakayama static int
    946   1.77       cdi psycho_ce(void *arg)
    947    1.1       mrg {
    948   1.34       eeh 	struct psycho_softc *sc = (struct psycho_softc *)arg;
    949   1.34       eeh 	struct psychoreg *regs = sc->sc_regs;
    950   1.34       eeh 
    951   1.34       eeh 	/*
    952   1.34       eeh 	 * It's correctable.  Dump the regs and continue.
    953   1.34       eeh 	 */
    954  1.107  christos 	aprint_error_dev(sc->sc_dev,
    955   1.98  nakayama 	    "correctable DMA error AFAR %" PRIx64 " AFSR %" PRIx64 "\n",
    956   1.98  nakayama 	    regs->psy_ce_afar, regs->psy_ce_afsr);
    957   1.34       eeh 	return (1);
    958    1.1       mrg }
    959   1.92       mrg 
    960   1.99  nakayama static int
    961   1.77       cdi psycho_bus_a(void *arg)
    962   1.34       eeh {
    963   1.34       eeh 	struct psycho_softc *sc = (struct psycho_softc *)arg;
    964   1.34       eeh 	struct psychoreg *regs = sc->sc_regs;
    965   1.34       eeh 
    966   1.34       eeh 	/*
    967   1.34       eeh 	 * It's uncorrectable.  Dump the regs and panic.
    968   1.34       eeh 	 */
    969    1.1       mrg 
    970   1.98  nakayama 	panic("%s: PCI bus A error AFAR %" PRIx64 " AFSR %" PRIx64,
    971  1.107  christos 	    device_xname(sc->sc_dev),
    972   1.98  nakayama 	    regs->psy_pcictl[0].pci_afar, regs->psy_pcictl[0].pci_afsr);
    973   1.34       eeh 	return (1);
    974   1.34       eeh }
    975   1.92       mrg 
    976   1.99  nakayama static int
    977   1.77       cdi psycho_bus_b(void *arg)
    978    1.1       mrg {
    979   1.34       eeh 	struct psycho_softc *sc = (struct psycho_softc *)arg;
    980   1.34       eeh 	struct psychoreg *regs = sc->sc_regs;
    981   1.34       eeh 
    982   1.34       eeh 	/*
    983   1.34       eeh 	 * It's uncorrectable.  Dump the regs and panic.
    984   1.34       eeh 	 */
    985    1.1       mrg 
    986   1.98  nakayama 	panic("%s: PCI bus B error AFAR %" PRIx64 " AFSR %" PRIx64,
    987  1.107  christos 	    device_xname(sc->sc_dev),
    988   1.98  nakayama 	    regs->psy_pcictl[0].pci_afar, regs->psy_pcictl[0].pci_afsr);
    989   1.34       eeh 	return (1);
    990    1.1       mrg }
    991   1.60    martin 
    992   1.99  nakayama static int
    993   1.77       cdi psycho_powerfail(void *arg)
    994   1.34       eeh {
    995   1.60    martin 	struct psycho_softc *sc = (struct psycho_softc *)arg;
    996    1.1       mrg 
    997   1.34       eeh 	/*
    998   1.60    martin 	 * We lost power. Queue a callback with thread context to
    999   1.60    martin 	 * handle all the real work.
   1000   1.34       eeh 	 */
   1001   1.60    martin 	if (sc->sc_powerpressed == 0 && sc->sc_smcontext != NULL) {
   1002   1.60    martin 		sc->sc_powerpressed = 1;
   1003   1.60    martin 		sysmon_task_queue_sched(0, psycho_power_button_pressed, sc);
   1004   1.60    martin 	}
   1005   1.34       eeh 	return (1);
   1006   1.34       eeh }
   1007   1.60    martin 
   1008   1.99  nakayama static
   1009   1.77       cdi int psycho_wakeup(void *arg)
   1010    1.1       mrg {
   1011   1.34       eeh 	struct psycho_softc *sc = (struct psycho_softc *)arg;
   1012    1.1       mrg 
   1013   1.34       eeh 	/*
   1014   1.34       eeh 	 * Gee, we don't really have a framework to deal with this
   1015   1.34       eeh 	 * properly.
   1016   1.34       eeh 	 */
   1017  1.107  christos 	aprint_error_dev(sc->sc_dev, "power management wakeup\n");
   1018   1.34       eeh 	return (1);
   1019    1.1       mrg }
   1020    1.1       mrg 
   1021   1.34       eeh 
   1022    1.1       mrg /*
   1023    1.1       mrg  * initialise the IOMMU..
   1024    1.1       mrg  */
   1025    1.1       mrg void
   1026   1.77       cdi psycho_iommu_init(struct psycho_softc *sc, int tsbsize)
   1027    1.1       mrg {
   1028    1.1       mrg 	char *name;
   1029   1.39       eeh 	struct iommu_state *is = sc->sc_is;
   1030   1.77       cdi 	uint32_t iobase = -1;
   1031   1.34       eeh 	int *vdma = NULL;
   1032   1.34       eeh 	int nitem;
   1033   1.24        pk 
   1034    1.1       mrg 	/* punch in our copies */
   1035   1.24        pk 	is->is_bustag = sc->sc_bustag;
   1036   1.45       eeh 	bus_space_subregion(sc->sc_bustag, sc->sc_bh,
   1037   1.99  nakayama 		offsetof(struct psychoreg, psy_iommu),
   1038  1.104       mrg 		sizeof (struct iommureg),
   1039   1.45       eeh 		&is->is_iommu);
   1040    1.1       mrg 
   1041   1.34       eeh 	/*
   1042   1.34       eeh 	 * Separate the men from the boys.  Get the `virtual-dma'
   1043   1.34       eeh 	 * property for sabre and use that to make sure the damn
   1044   1.34       eeh 	 * iommu works.
   1045   1.34       eeh 	 *
   1046   1.34       eeh 	 * We could query the `#virtual-dma-size-cells' and
   1047   1.34       eeh 	 * `#virtual-dma-addr-cells' and DTRT, but I'm lazy.
   1048   1.34       eeh 	 */
   1049   1.70    martin 	nitem = 0;
   1050   1.99  nakayama 	if (!prom_getprop(sc->sc_node, "virtual-dma", sizeof(vdma), &nitem,
   1051   1.66       mrg 		&vdma)) {
   1052   1.34       eeh 		/* Damn.  Gotta use these values. */
   1053   1.34       eeh 		iobase = vdma[0];
   1054   1.34       eeh #define	TSBCASE(x)	case 1<<((x)+23): tsbsize = (x); break
   1055   1.99  nakayama 		switch (vdma[1]) {
   1056  1.127       mrg 			TSBCASE(1);
   1057  1.127       mrg 			TSBCASE(2);
   1058  1.127       mrg 			TSBCASE(3);
   1059  1.127       mrg 			TSBCASE(4);
   1060  1.127       mrg 			TSBCASE(5);
   1061  1.127       mrg 			TSBCASE(6);
   1062  1.127       mrg 			TSBCASE(7);
   1063   1.99  nakayama 		default:
   1064   1.34       eeh 			printf("bogus tsb size %x, using 7\n", vdma[1]);
   1065  1.127       mrg 			tsbsize = 7;
   1066   1.34       eeh 		}
   1067   1.34       eeh #undef TSBCASE
   1068   1.34       eeh 	}
   1069   1.34       eeh 
   1070    1.1       mrg 	/* give us a nice name.. */
   1071  1.129   thorpej 	name = kmem_asprintf("%s dvma", device_xname(sc->sc_dev));
   1072    1.1       mrg 
   1073   1.34       eeh 	iommu_init(name, is, tsbsize, iobase);
   1074    1.7       mrg }
   1075    1.7       mrg 
   1076    1.7       mrg /*
   1077   1.61       wiz  * below here is bus space and bus DMA support
   1078    1.7       mrg  */
   1079    1.7       mrg bus_space_tag_t
   1080   1.77       cdi psycho_alloc_bus_tag(struct psycho_pbm *pp, int type)
   1081    1.7       mrg {
   1082    1.7       mrg 	struct psycho_softc *sc = pp->pp_sc;
   1083    1.7       mrg 	bus_space_tag_t bt;
   1084    1.7       mrg 
   1085  1.129   thorpej 	bt = kmem_zalloc(sizeof(*bt), KM_SLEEP);
   1086    1.7       mrg 	bt->cookie = pp;
   1087    1.7       mrg 	bt->parent = sc->sc_bustag;
   1088    1.7       mrg 	bt->type = type;
   1089    1.7       mrg 	bt->sparc_bus_map = _psycho_bus_map;
   1090    1.7       mrg 	bt->sparc_bus_mmap = psycho_bus_mmap;
   1091    1.7       mrg 	bt->sparc_intr_establish = psycho_intr_establish;
   1092    1.7       mrg 	return (bt);
   1093    1.7       mrg }
   1094    1.7       mrg 
   1095    1.7       mrg bus_dma_tag_t
   1096   1.77       cdi psycho_alloc_dma_tag(struct psycho_pbm *pp)
   1097    1.7       mrg {
   1098    1.7       mrg 	struct psycho_softc *sc = pp->pp_sc;
   1099    1.7       mrg 	bus_dma_tag_t dt, pdt = sc->sc_dmatag;
   1100    1.7       mrg 
   1101  1.129   thorpej 	dt = kmem_zalloc(sizeof(*dt), KM_SLEEP);
   1102    1.7       mrg 	dt->_cookie = pp;
   1103    1.7       mrg 	dt->_parent = pdt;
   1104    1.7       mrg #define PCOPY(x)	dt->x = pdt->x
   1105   1.91  nakayama 	dt->_dmamap_create = psycho_dmamap_create;
   1106    1.7       mrg 	PCOPY(_dmamap_destroy);
   1107   1.91  nakayama 	dt->_dmamap_load = iommu_dvmamap_load;
   1108    1.7       mrg 	PCOPY(_dmamap_load_mbuf);
   1109    1.7       mrg 	PCOPY(_dmamap_load_uio);
   1110   1.91  nakayama 	dt->_dmamap_load_raw = iommu_dvmamap_load_raw;
   1111   1.91  nakayama 	dt->_dmamap_unload = iommu_dvmamap_unload;
   1112   1.90  nakayama 	if (sc->sc_mode == PSYCHO_MODE_SABRE)
   1113   1.90  nakayama 		dt->_dmamap_sync = psycho_sabre_dmamap_sync;
   1114   1.90  nakayama 	else
   1115   1.91  nakayama 		dt->_dmamap_sync = iommu_dvmamap_sync;
   1116   1.91  nakayama 	dt->_dmamem_alloc = iommu_dvmamem_alloc;
   1117   1.91  nakayama 	dt->_dmamem_free = iommu_dvmamem_free;
   1118   1.91  nakayama 	dt->_dmamem_map = iommu_dvmamem_map;
   1119   1.91  nakayama 	dt->_dmamem_unmap = iommu_dvmamem_unmap;
   1120    1.7       mrg 	PCOPY(_dmamem_mmap);
   1121    1.7       mrg #undef	PCOPY
   1122    1.7       mrg 	return (dt);
   1123    1.7       mrg }
   1124    1.7       mrg 
   1125    1.7       mrg /*
   1126    1.7       mrg  * bus space support.  <sparc64/dev/psychoreg.h> has a discussion about
   1127    1.7       mrg  * PCI physical addresses.
   1128    1.7       mrg  */
   1129    1.7       mrg 
   1130   1.58  nakayama static struct psycho_ranges *
   1131   1.77       cdi get_psychorange(struct psycho_pbm *pp, int ss)
   1132   1.58  nakayama {
   1133   1.58  nakayama 	int i;
   1134   1.58  nakayama 
   1135   1.58  nakayama 	for (i = 0; i < pp->pp_nrange; i++) {
   1136   1.58  nakayama 		if (((pp->pp_range[i].cspace >> 24) & 0x03) == ss)
   1137   1.58  nakayama 			return (&pp->pp_range[i]);
   1138   1.58  nakayama 	}
   1139   1.58  nakayama 	/* not found */
   1140   1.58  nakayama 	return (NULL);
   1141   1.58  nakayama }
   1142   1.58  nakayama 
   1143    1.7       mrg static int
   1144   1.77       cdi _psycho_bus_map(bus_space_tag_t t, bus_addr_t offset, bus_size_t size,
   1145   1.77       cdi 	int flags, vaddr_t unused, bus_space_handle_t *hp)
   1146    1.7       mrg {
   1147    1.7       mrg 	struct psycho_pbm *pp = t->cookie;
   1148    1.7       mrg 	struct psycho_softc *sc = pp->pp_sc;
   1149   1.58  nakayama 	struct psycho_ranges *pr;
   1150   1.58  nakayama 	bus_addr_t paddr;
   1151   1.58  nakayama 	int ss;
   1152    1.7       mrg 
   1153   1.99  nakayama 	DPRINTF(PDB_BUSMAP,
   1154  1.122       mrg 		("%s: type %d off %qx sz %qx flags %d",
   1155  1.122       mrg 			__func__, t->type, (unsigned long long)offset,
   1156   1.44       eeh 			(unsigned long long)size, flags));
   1157    1.7       mrg 
   1158  1.125  macallan 	flags &= ~BUS_SPACE_MAP_PREFETCHABLE;
   1159  1.125  macallan 
   1160   1.94       mrg 	ss = sparc_pci_childspace(t->type);
   1161    1.7       mrg 	DPRINTF(PDB_BUSMAP, (" cspace %d", ss));
   1162    1.7       mrg 
   1163   1.58  nakayama 	pr = get_psychorange(pp, ss);
   1164   1.58  nakayama 	if (pr != NULL) {
   1165   1.58  nakayama 		paddr = BUS_ADDR(pr->phys_hi, pr->phys_lo + offset);
   1166  1.122       mrg 		DPRINTF(PDB_BUSMAP, ("\n%s: mapping paddr "
   1167   1.58  nakayama 				     "space %lx offset %lx paddr %qx\n",
   1168  1.122       mrg 			       __func__, (long)ss, (long)offset,
   1169   1.27      fvdl 			       (unsigned long long)paddr));
   1170   1.99  nakayama 		return ((*sc->sc_bustag->sparc_bus_map)(t, paddr, size,
   1171   1.44       eeh 			flags, 0, hp));
   1172    1.7       mrg 	}
   1173    1.7       mrg 	DPRINTF(PDB_BUSMAP, (" FAILED\n"));
   1174    1.7       mrg 	return (EINVAL);
   1175    1.7       mrg }
   1176    1.7       mrg 
   1177   1.37       eeh static paddr_t
   1178   1.77       cdi psycho_bus_mmap(bus_space_tag_t t, bus_addr_t paddr, off_t off, int prot,
   1179   1.77       cdi 	int flags)
   1180    1.7       mrg {
   1181    1.7       mrg 	bus_addr_t offset = paddr;
   1182    1.7       mrg 	struct psycho_pbm *pp = t->cookie;
   1183    1.7       mrg 	struct psycho_softc *sc = pp->pp_sc;
   1184   1.58  nakayama 	struct psycho_ranges *pr;
   1185   1.58  nakayama 	int ss;
   1186    1.7       mrg 
   1187  1.125  macallan 	flags &= ~BUS_SPACE_MAP_PREFETCHABLE;
   1188  1.125  macallan 
   1189   1.94       mrg 	ss = sparc_pci_childspace(t->type);
   1190    1.7       mrg 
   1191  1.123       mrg 	DPRINTF(PDB_BUSMAP, ("%s: prot %x flags %d busaddr %qx\n",
   1192  1.122       mrg 		__func__, prot, flags, (unsigned long long)paddr));
   1193    1.7       mrg 
   1194   1.58  nakayama 	pr = get_psychorange(pp, ss);
   1195   1.58  nakayama 	if (pr != NULL) {
   1196   1.58  nakayama 		paddr = BUS_ADDR(pr->phys_hi, pr->phys_lo + offset);
   1197  1.122       mrg 		DPRINTF(PDB_BUSMAP, ("%s: mapping paddr "
   1198   1.58  nakayama 				     "space %lx offset %lx paddr %qx\n",
   1199  1.122       mrg 			       __func__, (long)ss, (long)offset,
   1200   1.27      fvdl 			       (unsigned long long)paddr));
   1201   1.37       eeh 		return (bus_space_mmap(sc->sc_bustag, paddr, off,
   1202   1.37       eeh 				       prot, flags));
   1203    1.7       mrg 	}
   1204    1.7       mrg 
   1205   1.58  nakayama 	return (-1);
   1206   1.58  nakayama }
   1207   1.58  nakayama 
   1208   1.58  nakayama /*
   1209   1.58  nakayama  * Get a PCI offset address from bus_space_handle_t.
   1210   1.58  nakayama  */
   1211   1.58  nakayama bus_addr_t
   1212   1.77       cdi psycho_bus_offset(bus_space_tag_t t, bus_space_handle_t *hp)
   1213   1.58  nakayama {
   1214   1.58  nakayama 	struct psycho_pbm *pp = t->cookie;
   1215   1.58  nakayama 	struct psycho_ranges *pr;
   1216   1.58  nakayama 	bus_addr_t addr, offset;
   1217   1.58  nakayama 	vaddr_t va;
   1218   1.58  nakayama 	int ss;
   1219   1.58  nakayama 
   1220   1.58  nakayama 	addr = hp->_ptr;
   1221   1.94       mrg 	ss = sparc_pci_childspace(t->type);
   1222  1.122       mrg 	DPRINTF(PDB_BUSMAP, ("%s: type %d addr %" PRIx64" cspace %d",
   1223  1.122       mrg 			     __func__, t->type, addr, ss));
   1224   1.58  nakayama 
   1225   1.58  nakayama 	pr = get_psychorange(pp, ss);
   1226   1.58  nakayama 	if (pr != NULL) {
   1227   1.58  nakayama 		if (!PHYS_ASI(hp->_asi)) {
   1228   1.58  nakayama 			va = trunc_page((vaddr_t)addr);
   1229   1.58  nakayama 			if (pmap_extract(pmap_kernel(), va, &addr) == FALSE) {
   1230   1.58  nakayama 				DPRINTF(PDB_BUSMAP,
   1231  1.122       mrg 					("- pmap_extract FAILED\n"));
   1232   1.58  nakayama 				return (-1);
   1233   1.58  nakayama 			}
   1234   1.58  nakayama 			addr += hp->_ptr & PGOFSET;
   1235   1.58  nakayama 		}
   1236   1.58  nakayama 		offset = BUS_ADDR_PADDR(addr) - pr->phys_lo;
   1237  1.122       mrg 		DPRINTF(PDB_BUSMAP, ("- paddr %" PRIx64" offset %" PRIx64 "\n",
   1238  1.122       mrg 				    addr, offset));
   1239   1.58  nakayama 		return (offset);
   1240   1.58  nakayama 	}
   1241  1.122       mrg 	DPRINTF(PDB_BUSMAP, ("- FAILED\n"));
   1242    1.7       mrg 	return (-1);
   1243    1.7       mrg }
   1244    1.7       mrg 
   1245    1.7       mrg 
   1246    1.7       mrg /*
   1247    1.7       mrg  * install an interrupt handler for a PCI device
   1248    1.7       mrg  */
   1249    1.7       mrg void *
   1250   1.77       cdi psycho_intr_establish(bus_space_tag_t t, int ihandle, int level,
   1251   1.77       cdi 	int (*handler)(void *), void *arg, void (*fastvec)(void) /* ignored */)
   1252    1.7       mrg {
   1253    1.7       mrg 	struct psycho_pbm *pp = t->cookie;
   1254    1.7       mrg 	struct psycho_softc *sc = pp->pp_sc;
   1255    1.7       mrg 	struct intrhand *ih;
   1256   1.77       cdi 	volatile uint64_t *intrmapptr = NULL, *intrclrptr = NULL;
   1257   1.74  christos 	int64_t imap = 0;
   1258    1.7       mrg 	int ino;
   1259   1.99  nakayama 	long vec = INTVEC(ihandle);
   1260    1.7       mrg 
   1261  1.124     palle 	ih = intrhand_alloc();
   1262    1.7       mrg 
   1263  1.109  macallan 	ih->ih_ivec = ihandle;
   1264  1.109  macallan 
   1265   1.34       eeh 	/*
   1266   1.34       eeh 	 * Hunt through all the interrupt mapping regs to look for our
   1267   1.34       eeh 	 * interrupt vector.
   1268   1.34       eeh 	 *
   1269   1.34       eeh 	 * XXX We only compare INOs rather than IGNs since the firmware may
   1270   1.34       eeh 	 * not provide the IGN and the IGN is constant for all device on that
   1271   1.34       eeh 	 * PCI controller.  This could cause problems for the FFB/external
   1272   1.99  nakayama 	 * interrupt which has a full vector that can be set arbitrarily.
   1273   1.34       eeh 	 */
   1274   1.34       eeh 
   1275  1.122       mrg 	DPRINTF(PDB_INTR, ("%s: ihandle %x vec %lx", __func__, ihandle, vec));
   1276    1.7       mrg 	ino = INTINO(vec);
   1277  1.122       mrg 	DPRINTF(PDB_INTR, (" ino %x\n", ino));
   1278   1.34       eeh 
   1279   1.34       eeh 	/* If the device didn't ask for an IPL, use the one encoded. */
   1280   1.34       eeh 	if (level == IPL_NONE) level = INTLEV(vec);
   1281   1.34       eeh 	/* If it still has no level, print a warning and assign IPL 2 */
   1282   1.34       eeh 	if (level == IPL_NONE) {
   1283   1.34       eeh 		printf("ERROR: no IPL, setting IPL 2.\n");
   1284   1.34       eeh 		level = 2;
   1285   1.34       eeh 	}
   1286   1.34       eeh 
   1287  1.122       mrg 	DPRINTF(PDB_INTR, ("%s: intr %lx: %p\nHunting for IRQ...\n",
   1288  1.122       mrg 	    __func__, (long)ino, intrlev[ino]));
   1289    1.7       mrg 
   1290   1.99  nakayama  	/*
   1291   1.82     rafal  	 * First look for PCI interrupts, otherwise the PCI A slot 0
   1292   1.82     rafal  	 * INTA# interrupt might match an unused non-PCI (obio)
   1293   1.82     rafal  	 * interrupt.
   1294   1.82     rafal  	 */
   1295   1.56        pk 	for (intrmapptr = &sc->sc_regs->pcia_slot0_int,
   1296   1.56        pk 		     intrclrptr = &sc->sc_regs->pcia0_clr_int[0];
   1297   1.56        pk 	     intrmapptr <= &sc->sc_regs->pcib_slot3_int;
   1298   1.56        pk 	     intrmapptr++, intrclrptr += 4) {
   1299   1.68    petrov 		if (sc->sc_mode == PSYCHO_MODE_PSYCHO &&
   1300   1.68    petrov 		    (intrmapptr == &sc->sc_regs->pcia_slot2_int ||
   1301   1.68    petrov 		     intrmapptr == &sc->sc_regs->pcia_slot3_int))
   1302   1.68    petrov 			continue;
   1303   1.56        pk 		if (((*intrmapptr ^ vec) & 0x3c) == 0) {
   1304   1.56        pk 			intrclrptr += vec & 0x3;
   1305   1.56        pk 			goto found;
   1306   1.34       eeh 		}
   1307   1.56        pk 	}
   1308    1.7       mrg 
   1309   1.82     rafal 	/* Now hunt thru obio. */
   1310   1.82     rafal 	for (intrmapptr = &sc->sc_regs->scsi_int_map,
   1311   1.82     rafal 		     intrclrptr = &sc->sc_regs->scsi_clr_int;
   1312   1.82     rafal 	     intrmapptr < &sc->sc_regs->ue_int_map;
   1313   1.82     rafal 	     intrmapptr++, intrclrptr++) {
   1314   1.82     rafal 		if (INTINO(*intrmapptr) == ino)
   1315   1.82     rafal 			goto found;
   1316   1.82     rafal 	}
   1317   1.82     rafal 
   1318   1.56        pk 	/* Finally check the two FFB slots */
   1319   1.56        pk 	intrclrptr = NULL; /* XXX? */
   1320   1.56        pk 	for (intrmapptr = &sc->sc_regs->ffb0_int_map;
   1321   1.56        pk 	     intrmapptr <= &sc->sc_regs->ffb1_int_map;
   1322   1.56        pk 	     intrmapptr++) {
   1323   1.56        pk 		if (INTVEC(*intrmapptr) == ino)
   1324   1.56        pk 			goto found;
   1325   1.56        pk 	}
   1326   1.51       eeh 
   1327   1.56        pk 	printf("Cannot find interrupt vector %lx\n", vec);
   1328  1.129   thorpej 	kmem_free(ih, sizeof(*ih));
   1329   1.56        pk 	return (NULL);
   1330   1.51       eeh 
   1331   1.56        pk found:
   1332   1.56        pk 	/* Register the map and clear intr registers */
   1333   1.56        pk 	ih->ih_map = intrmapptr;
   1334   1.56        pk 	ih->ih_clr = intrclrptr;
   1335    1.7       mrg 
   1336    1.7       mrg 	ih->ih_fun = handler;
   1337    1.7       mrg 	ih->ih_arg = arg;
   1338   1.34       eeh 	ih->ih_pil = level;
   1339   1.24        pk 	ih->ih_number = ino | sc->sc_ign;
   1340  1.110       mrg 	ih->ih_pending = 0;
   1341   1.19        pk 
   1342   1.19        pk 	DPRINTF(PDB_INTR, (
   1343  1.122       mrg 	    "%s: installing handler %p arg %p with ino %u pil %u\n",
   1344  1.122       mrg 	    __func__, handler, arg, (u_int)ino, (u_int)ih->ih_pil));
   1345   1.19        pk 
   1346   1.86    martin 	intr_establish(ih->ih_pil, level != IPL_VM, ih);
   1347   1.34       eeh 
   1348   1.34       eeh 	/*
   1349   1.34       eeh 	 * Enable the interrupt now we have the handler installed.
   1350   1.34       eeh 	 * Read the current value as we can't change it besides the
   1351   1.34       eeh 	 * valid bit so so make sure only this bit is changed.
   1352   1.34       eeh 	 *
   1353   1.34       eeh 	 * XXXX --- we really should use bus_space for this.
   1354   1.34       eeh 	 */
   1355   1.34       eeh 	if (intrmapptr) {
   1356   1.74  christos 		imap = *intrmapptr;
   1357  1.122       mrg 		DPRINTF(PDB_INTR, ("%s: read intrmap = %016qx",
   1358  1.122       mrg 			__func__, (unsigned long long)imap));
   1359   1.34       eeh 
   1360   1.34       eeh 		/* Enable the interrupt */
   1361   1.76    martin 		imap |= INTMAP_V|(CPU_UPAID << INTMAP_TID_SHIFT);
   1362   1.34       eeh 		DPRINTF(PDB_INTR, ("; addr of intrmapptr = %p", intrmapptr));
   1363   1.34       eeh 		DPRINTF(PDB_INTR, ("; writing intrmap = %016qx\n",
   1364   1.74  christos 			(unsigned long long)imap));
   1365   1.74  christos 		*intrmapptr = imap;
   1366  1.122       mrg 		DPRINTF(PDB_INTR, ("; reread intrmap = %016qx\n",
   1367   1.74  christos 			(unsigned long long)(imap = *intrmapptr)));
   1368   1.34       eeh 	}
   1369   1.82     rafal  	if (intrclrptr) {
   1370   1.82     rafal  		/* set state to IDLE */
   1371   1.82     rafal  		*intrclrptr = 0;
   1372   1.82     rafal  	}
   1373    1.7       mrg 	return (ih);
   1374    1.7       mrg }
   1375    1.7       mrg 
   1376    1.7       mrg /*
   1377   1.92       mrg  * per-controller driver calls
   1378   1.92       mrg  */
   1379   1.92       mrg 
   1380   1.92       mrg /* assume we are mapped little-endian/side-effect */
   1381   1.92       mrg static pcireg_t
   1382   1.92       mrg psycho_pci_conf_read(pci_chipset_tag_t pc, pcitag_t tag, int reg)
   1383   1.92       mrg {
   1384   1.92       mrg 	struct psycho_pbm *pp = pc->cookie;
   1385   1.92       mrg 	struct psycho_softc *sc = pp->pp_sc;
   1386   1.92       mrg 	pcireg_t val = (pcireg_t)~0;
   1387  1.113  nakayama 	int s;
   1388   1.92       mrg 
   1389   1.99  nakayama 	DPRINTF(PDB_CONF, ("%s: tag %lx reg %x ", __func__,
   1390   1.92       mrg 		(long)tag, reg));
   1391  1.121   msaitoh 	if (PCITAG_NODE(tag) != -1 && (unsigned int)reg < PCI_CONF_SIZE) {
   1392   1.92       mrg 
   1393   1.92       mrg 		DPRINTF(PDB_CONF, ("asi=%x addr=%qx (offset=%x) ...",
   1394   1.92       mrg 			sc->sc_configaddr._asi,
   1395   1.99  nakayama 			(long long)(sc->sc_configaddr._ptr +
   1396   1.92       mrg 				PCITAG_OFFSET(tag) + reg),
   1397   1.92       mrg 			(int)PCITAG_OFFSET(tag) + reg));
   1398   1.92       mrg 
   1399  1.113  nakayama 		s = splhigh();
   1400  1.132       mrg 		struct cpu_info *ci = curcpu();
   1401  1.113  nakayama 		ci->ci_pci_probe = true;
   1402  1.113  nakayama 		membar_Sync();
   1403   1.92       mrg 		val = bus_space_read_4(sc->sc_configtag, sc->sc_configaddr,
   1404   1.92       mrg 			PCITAG_OFFSET(tag) + reg);
   1405  1.113  nakayama 		membar_Sync();
   1406  1.113  nakayama 		if (ci->ci_pci_fault)
   1407  1.113  nakayama 			val = (pcireg_t)~0;
   1408  1.113  nakayama 		ci->ci_pci_probe = ci->ci_pci_fault = false;
   1409  1.113  nakayama 		splx(s);
   1410   1.92       mrg 	}
   1411   1.92       mrg #ifdef DEBUG
   1412  1.122       mrg 	else DPRINTF(PDB_CONF, ("%s: bogus pcitag %x -", __func__,
   1413   1.92       mrg 		(int)PCITAG_OFFSET(tag)));
   1414   1.92       mrg #endif
   1415   1.92       mrg 	DPRINTF(PDB_CONF, (" returning %08x\n", (u_int)val));
   1416   1.92       mrg 
   1417   1.92       mrg 	return (val);
   1418   1.92       mrg }
   1419   1.92       mrg 
   1420   1.92       mrg static void
   1421   1.92       mrg psycho_pci_conf_write(pci_chipset_tag_t pc, pcitag_t tag, int reg, pcireg_t data)
   1422   1.92       mrg {
   1423   1.92       mrg 	struct psycho_pbm *pp = pc->cookie;
   1424   1.92       mrg 	struct psycho_softc *sc = pp->pp_sc;
   1425   1.92       mrg 
   1426   1.99  nakayama 	DPRINTF(PDB_CONF, ("%s: tag %lx; reg %x; data %x; ", __func__,
   1427   1.92       mrg 		(long)PCITAG_OFFSET(tag), reg, (int)data));
   1428   1.92       mrg 	DPRINTF(PDB_CONF, ("asi = %x; readaddr = %qx (offset = %x)\n",
   1429   1.92       mrg 		sc->sc_configaddr._asi,
   1430   1.99  nakayama 		(long long)(sc->sc_configaddr._ptr + PCITAG_OFFSET(tag) + reg),
   1431   1.92       mrg 		(int)PCITAG_OFFSET(tag) + reg));
   1432   1.92       mrg 
   1433   1.92       mrg 	/* If we don't know it, just punt it.  */
   1434   1.92       mrg 	if (PCITAG_NODE(tag) == -1) {
   1435   1.99  nakayama 		DPRINTF(PDB_CONF, ("%s: bad addr", __func__));
   1436   1.92       mrg 		return;
   1437   1.92       mrg 	}
   1438  1.121   msaitoh 
   1439  1.121   msaitoh 	if ((unsigned int)reg >= PCI_CONF_SIZE)
   1440  1.121   msaitoh 		return;
   1441  1.121   msaitoh 
   1442   1.99  nakayama 	bus_space_write_4(sc->sc_configtag, sc->sc_configaddr,
   1443   1.92       mrg 		PCITAG_OFFSET(tag) + reg, data);
   1444   1.92       mrg }
   1445   1.92       mrg 
   1446   1.92       mrg static void *
   1447   1.92       mrg psycho_pci_intr_establish(pci_chipset_tag_t pc, pci_intr_handle_t ih, int level,
   1448   1.92       mrg 	int (*func)(void *), void *arg)
   1449   1.92       mrg {
   1450   1.92       mrg 	void *cookie;
   1451   1.92       mrg 	struct psycho_pbm *pp = (struct psycho_pbm *)pc->cookie;
   1452   1.92       mrg 
   1453   1.96       mrg 	DPRINTF(PDB_INTR, ("%s: ih %lx; level %d", __func__, (u_long)ih, level));
   1454   1.92       mrg 	cookie = bus_intr_establish(pp->pp_memt, ih, level, func, arg);
   1455   1.92       mrg 
   1456   1.92       mrg 	DPRINTF(PDB_INTR, ("; returning handle %p\n", cookie));
   1457   1.92       mrg 	return (cookie);
   1458   1.92       mrg }
   1459   1.92       mrg 
   1460   1.92       mrg static int
   1461  1.105    dyoung psycho_pci_find_ino(const struct pci_attach_args *pa, pci_intr_handle_t *ihp)
   1462   1.92       mrg {
   1463   1.92       mrg 	struct psycho_pbm *pp = pa->pa_pc->cookie;
   1464   1.92       mrg 	struct psycho_softc *sc = pp->pp_sc;
   1465   1.92       mrg 	u_int bus;
   1466   1.92       mrg 	u_int dev;
   1467   1.92       mrg 	u_int pin;
   1468   1.92       mrg 
   1469   1.99  nakayama 	DPRINTF(PDB_INTMAP, ("%s: pa_tag: node %x, %d:%d:%d\n", __func__,
   1470   1.92       mrg 			      PCITAG_NODE(pa->pa_tag), (int)PCITAG_BUS(pa->pa_tag),
   1471   1.92       mrg 			      (int)PCITAG_DEV(pa->pa_tag),
   1472   1.92       mrg 			      (int)PCITAG_FUN(pa->pa_tag)));
   1473   1.92       mrg 	DPRINTF(PDB_INTMAP,
   1474   1.99  nakayama 		("%s: intrswiz %d, intrpin %d, intrline %d, rawintrpin %d\n", __func__,
   1475   1.92       mrg 		 pa->pa_intrswiz, pa->pa_intrpin, pa->pa_intrline, pa->pa_rawintrpin));
   1476   1.99  nakayama 	DPRINTF(PDB_INTMAP, ("%s: pa_intrtag: node %x, %d:%d:%d\n", __func__,
   1477   1.92       mrg 			      PCITAG_NODE(pa->pa_intrtag),
   1478   1.92       mrg 			      (int)PCITAG_BUS(pa->pa_intrtag),
   1479   1.92       mrg 			      (int)PCITAG_DEV(pa->pa_intrtag),
   1480   1.92       mrg 			      (int)PCITAG_FUN(pa->pa_intrtag)));
   1481   1.92       mrg 
   1482   1.92       mrg 	bus = (pp->pp_id == PSYCHO_PBM_B);
   1483   1.92       mrg 	/*
   1484   1.92       mrg 	 * If we are on a ppb, use the devno on the underlying bus when forming
   1485   1.92       mrg 	 * the ivec.
   1486   1.92       mrg 	 */
   1487   1.99  nakayama 	if (pa->pa_intrswiz != 0 && PCITAG_NODE(pa->pa_intrtag) != 0)
   1488   1.92       mrg 		dev = PCITAG_DEV(pa->pa_intrtag);
   1489   1.92       mrg 	else
   1490   1.92       mrg 		dev = pa->pa_device;
   1491   1.92       mrg 	dev--;
   1492   1.92       mrg 
   1493   1.92       mrg 	if (sc->sc_mode == PSYCHO_MODE_PSYCHO &&
   1494   1.92       mrg 	    pp->pp_id == PSYCHO_PBM_B)
   1495   1.92       mrg 		dev--;
   1496   1.92       mrg 
   1497   1.92       mrg 	pin = pa->pa_intrpin - 1;
   1498   1.99  nakayama 	DPRINTF(PDB_INTMAP, ("%s: mode %d, pbm %d, dev %d, pin %d\n", __func__,
   1499   1.92       mrg 	    sc->sc_mode, pp->pp_id, dev, pin));
   1500   1.92       mrg 
   1501   1.92       mrg 	*ihp = sc->sc_ign | ((bus << 4) & INTMAP_PCIBUS) |
   1502   1.92       mrg 	    ((dev << 2) & INTMAP_PCISLOT) | (pin & INTMAP_PCIINT);
   1503   1.92       mrg 
   1504   1.92       mrg 	return (0);
   1505   1.92       mrg }
   1506   1.92       mrg 
   1507   1.92       mrg /*
   1508    1.7       mrg  * hooks into the iommu dvma calls.
   1509    1.7       mrg  */
   1510   1.91  nakayama static int
   1511   1.91  nakayama psycho_dmamap_create(bus_dma_tag_t t, bus_size_t size, int nsegments,
   1512   1.91  nakayama 	bus_size_t maxsegsz, bus_size_t boundary, int flags,
   1513   1.91  nakayama 	bus_dmamap_t *dmamp)
   1514    1.7       mrg {
   1515    1.7       mrg 	struct psycho_pbm *pp = (struct psycho_pbm *)t->_cookie;
   1516   1.91  nakayama 	int error;
   1517    1.7       mrg 
   1518   1.91  nakayama 	error = bus_dmamap_create(t->_parent, size, nsegments, maxsegsz,
   1519   1.91  nakayama 				  boundary, flags, dmamp);
   1520   1.91  nakayama 	if (error == 0)
   1521   1.91  nakayama 		(*dmamp)->_dm_cookie = &pp->pp_sb;
   1522   1.91  nakayama 	return error;
   1523    1.1       mrg }
   1524   1.90  nakayama 
   1525   1.90  nakayama /*
   1526   1.90  nakayama  * UltraSPARC IIi and IIe have no streaming buffers, but have PCI DMA
   1527   1.90  nakayama  * Write Synchronization Register (see UltraSPARC-IIi User's Manual
   1528   1.90  nakayama  * section 19.3.0.5).  So use it to synchronize with the DMA writes.
   1529   1.90  nakayama  */
   1530   1.90  nakayama static void
   1531   1.90  nakayama psycho_sabre_dmamap_sync(bus_dma_tag_t t, bus_dmamap_t map, bus_addr_t offset,
   1532   1.90  nakayama 	bus_size_t len, int ops)
   1533   1.90  nakayama {
   1534  1.101  nakayama 	struct psycho_pbm *pp;
   1535  1.101  nakayama 	struct psycho_softc *sc;
   1536  1.101  nakayama 
   1537  1.101  nakayama 	/* If len is 0, then there is nothing to do. */
   1538  1.101  nakayama 	if (len == 0)
   1539  1.101  nakayama 		return;
   1540   1.90  nakayama 
   1541  1.101  nakayama 	if (ops & BUS_DMASYNC_POSTREAD) {
   1542  1.101  nakayama 		pp = (struct psycho_pbm *)t->_cookie;
   1543  1.101  nakayama 		sc = pp->pp_sc;
   1544   1.90  nakayama 		bus_space_read_8(sc->sc_bustag, sc->sc_bh,
   1545  1.101  nakayama 		    offsetof(struct psychoreg, pci_dma_write_sync));
   1546  1.101  nakayama 	}
   1547   1.90  nakayama 	bus_dmamap_sync(t->_parent, map, offset, len, ops);
   1548   1.90  nakayama }
   1549  1.114  macallan 
   1550  1.114  macallan /* US-IIe STICK support */
   1551  1.114  macallan 
   1552  1.115  nakayama uint64_t
   1553  1.114  macallan psycho_getstick(void)
   1554  1.114  macallan {
   1555  1.115  nakayama 	uint64_t stick;
   1556  1.114  macallan 
   1557  1.115  nakayama 	stick = bus_space_read_8(psycho0->sc_bustag, psycho0->sc_bh,
   1558  1.114  macallan 	    STICK_CNT_LOW) |
   1559  1.114  macallan 	    (bus_space_read_8(psycho0->sc_bustag, psycho0->sc_bh,
   1560  1.114  macallan 	    STICK_CNT_HIGH) & 0x7fffffff) << 32;
   1561  1.115  nakayama 	return stick;
   1562  1.114  macallan }
   1563  1.114  macallan 
   1564  1.118  nakayama uint32_t
   1565  1.118  nakayama psycho_getstick32(void)
   1566  1.118  nakayama {
   1567  1.118  nakayama 
   1568  1.118  nakayama 	return bus_space_read_8(psycho0->sc_bustag, psycho0->sc_bh,
   1569  1.118  nakayama 	    STICK_CNT_LOW);
   1570  1.118  nakayama }
   1571  1.118  nakayama 
   1572  1.114  macallan void
   1573  1.114  macallan psycho_setstick(long cnt)
   1574  1.114  macallan {
   1575  1.114  macallan 
   1576  1.114  macallan 	/*
   1577  1.114  macallan 	 * looks like we can't actually write the STICK counter, so instead we
   1578  1.114  macallan 	 * prepare sc_last_stick for the coming interrupt setup
   1579  1.114  macallan 	 */
   1580  1.114  macallan #if 0
   1581  1.114  macallan 	bus_space_write_8(psycho0->sc_bustag, psycho0->sc_bh,
   1582  1.114  macallan 	    STICK_CNT_HIGH, (cnt >> 32));
   1583  1.114  macallan 	bus_space_write_8(psycho0->sc_bustag, psycho0->sc_bh,
   1584  1.114  macallan 	    STICK_CNT_LOW, (uint32_t)(cnt & 0xffffffff));
   1585  1.114  macallan #endif
   1586  1.114  macallan 
   1587  1.114  macallan 	if (cnt == 0) {
   1588  1.114  macallan 		bus_space_write_8(psycho0->sc_bustag, psycho0->sc_bh,
   1589  1.114  macallan 		    STICK_CMP_HIGH, 0);
   1590  1.114  macallan 		bus_space_write_8(psycho0->sc_bustag, psycho0->sc_bh,
   1591  1.114  macallan 		    STICK_CMP_LOW, 0);
   1592  1.114  macallan 		psycho0->sc_last_stick = 0;
   1593  1.114  macallan 	}
   1594  1.114  macallan 
   1595  1.114  macallan 	psycho0->sc_last_stick = psycho_getstick();
   1596  1.122       mrg 	DPRINTF(PDB_STICK, ("%s: %ld\n", __func__, psycho0->sc_last_stick));
   1597  1.114  macallan }
   1598  1.114  macallan 
   1599  1.114  macallan void
   1600  1.114  macallan psycho_nextstick(long diff)
   1601  1.114  macallan {
   1602  1.114  macallan 	uint64_t cmp, now;
   1603  1.114  macallan 
   1604  1.114  macallan 	/*
   1605  1.114  macallan 	 * there is no way we'll ever overflow
   1606  1.114  macallan 	 * the counter is 63 bits wide, at 12MHz that's >24000 years
   1607  1.114  macallan 	 */
   1608  1.114  macallan 	now = psycho_getstick() + 1000;
   1609  1.114  macallan 	cmp = psycho0->sc_last_stick;
   1610  1.114  macallan 
   1611  1.114  macallan 	while (cmp < now)
   1612  1.114  macallan 		cmp += diff;
   1613  1.114  macallan 
   1614  1.114  macallan 	bus_space_write_8(psycho0->sc_bustag, psycho0->sc_bh,
   1615  1.114  macallan 	    STICK_CMP_HIGH, (cmp >> 32) & 0x7fffffff);
   1616  1.114  macallan 	bus_space_write_8(psycho0->sc_bustag, psycho0->sc_bh,
   1617  1.114  macallan 	    STICK_CMP_LOW, (cmp & 0xffffffff));
   1618  1.114  macallan 
   1619  1.114  macallan 	psycho0->sc_last_stick = cmp;
   1620  1.114  macallan }
   1621