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