Home | History | Annotate | Line # | Download | only in vsa
vsbus.c revision 1.43
      1  1.43  drochner /*	$NetBSD: vsbus.c,v 1.43 2003/06/18 08:58:42 drochner Exp $ */
      2   1.1     ragge /*
      3  1.20     ragge  * Copyright (c) 1996, 1999 Ludd, University of Lule}, Sweden.
      4   1.1     ragge  * All rights reserved.
      5   1.1     ragge  *
      6   1.1     ragge  * This code is derived from software contributed to Ludd by Bertram Barth.
      7   1.1     ragge  *
      8   1.1     ragge  * Redistribution and use in source and binary forms, with or without
      9   1.1     ragge  * modification, are permitted provided that the following conditions
     10   1.1     ragge  * are met:
     11   1.1     ragge  * 1. Redistributions of source code must retain the above copyright
     12   1.1     ragge  *    notice, this list of conditions and the following disclaimer.
     13   1.1     ragge  * 2. Redistributions in binary form must reproduce the above copyright
     14   1.1     ragge  *    notice, this list of conditions and the following disclaimer in the
     15   1.1     ragge  *    documentation and/or other materials provided with the distribution.
     16   1.1     ragge  * 3. All advertising materials mentioning features or use of this software
     17   1.1     ragge  *    must display the following acknowledgement:
     18   1.1     ragge  *	This product includes software developed at Ludd, University of
     19   1.1     ragge  *	Lule}, Sweden and its contributors.
     20   1.1     ragge  * 4. The name of the author may not be used to endorse or promote products
     21   1.1     ragge  *    derived from this software without specific prior written permission
     22   1.1     ragge  *
     23   1.1     ragge  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
     24   1.1     ragge  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
     25   1.1     ragge  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
     26   1.1     ragge  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
     27   1.1     ragge  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
     28   1.1     ragge  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
     29   1.1     ragge  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
     30   1.1     ragge  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
     31   1.1     ragge  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
     32   1.1     ragge  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     33   1.1     ragge  */
     34   1.1     ragge 
     35   1.1     ragge #include <sys/param.h>
     36   1.1     ragge #include <sys/systm.h>
     37   1.1     ragge #include <sys/buf.h>
     38   1.1     ragge #include <sys/conf.h>
     39   1.1     ragge #include <sys/file.h>
     40   1.1     ragge #include <sys/ioctl.h>
     41   1.1     ragge #include <sys/proc.h>
     42   1.1     ragge #include <sys/user.h>
     43   1.1     ragge #include <sys/device.h>
     44   1.1     ragge #include <sys/disklabel.h>
     45   1.1     ragge #include <sys/syslog.h>
     46   1.1     ragge #include <sys/stat.h>
     47   1.1     ragge 
     48  1.29       mrg #include <uvm/uvm_extern.h>
     49  1.20     ragge 
     50  1.27     ragge #define _VAX_BUS_DMA_PRIVATE
     51  1.17     ragge #include <machine/bus.h>
     52   1.1     ragge #include <machine/pte.h>
     53   1.1     ragge #include <machine/sid.h>
     54   1.1     ragge #include <machine/scb.h>
     55   1.1     ragge #include <machine/cpu.h>
     56   1.1     ragge #include <machine/trap.h>
     57   1.1     ragge #include <machine/nexus.h>
     58   1.1     ragge 
     59   1.1     ragge #include <machine/uvax.h>
     60   1.1     ragge #include <machine/ka410.h>
     61  1.12     ragge #include <machine/ka420.h>
     62   1.1     ragge #include <machine/ka43.h>
     63   1.1     ragge 
     64   1.1     ragge #include <machine/vsbus.h>
     65   1.1     ragge 
     66  1.10     ragge #include "ioconf.h"
     67  1.26      matt #include "opt_cputype.h"
     68   1.1     ragge 
     69  1.27     ragge int	vsbus_match(struct device *, struct cfdata *, void *);
     70  1.27     ragge void	vsbus_attach(struct device *, struct device *, void *);
     71  1.27     ragge int	vsbus_print(void *, const char *);
     72  1.27     ragge int	vsbus_search(struct device *, struct cfdata *, void *);
     73   1.1     ragge 
     74  1.21      matt static struct vax_bus_dma_tag vsbus_bus_dma_tag = {
     75  1.17     ragge 	0,
     76  1.17     ragge 	0,
     77  1.17     ragge 	0,
     78  1.17     ragge 	0,
     79  1.17     ragge 	0,
     80  1.17     ragge 	0,
     81  1.17     ragge 	_bus_dmamap_create,
     82  1.17     ragge 	_bus_dmamap_destroy,
     83  1.17     ragge 	_bus_dmamap_load,
     84  1.17     ragge 	_bus_dmamap_load_mbuf,
     85  1.17     ragge 	_bus_dmamap_load_uio,
     86  1.17     ragge 	_bus_dmamap_load_raw,
     87  1.17     ragge 	_bus_dmamap_unload,
     88  1.17     ragge 	_bus_dmamap_sync,
     89  1.17     ragge 	_bus_dmamem_alloc,
     90  1.17     ragge 	_bus_dmamem_free,
     91  1.17     ragge 	_bus_dmamem_map,
     92  1.17     ragge 	_bus_dmamem_unmap,
     93  1.17     ragge 	_bus_dmamem_mmap,
     94  1.17     ragge };
     95  1.17     ragge 
     96  1.21      matt extern struct vax_bus_space vax_mem_bus_space;
     97  1.27     ragge static SIMPLEQ_HEAD(, vsbus_dma) vsbus_dma;
     98  1.21      matt 
     99  1.38   thorpej CFATTACH_DECL(vsbus, sizeof(struct vsbus_softc),
    100  1.39   thorpej     vsbus_match, vsbus_attach, NULL, NULL);
    101   1.6     ragge 
    102   1.1     ragge int
    103   1.1     ragge vsbus_print(aux, name)
    104   1.1     ragge 	void *aux;
    105   1.2       cgd 	const char *name;
    106   1.1     ragge {
    107  1.10     ragge 	struct vsbus_attach_args *va = aux;
    108   1.1     ragge 
    109  1.40   thorpej 	aprint_normal(" csr 0x%lx vec %o ipl %x maskbit %d", va->va_paddr,
    110  1.16     ragge 	    va->va_cvec & 511, va->va_br, va->va_maskno - 1);
    111  1.16     ragge 	return(UNCONF);
    112   1.1     ragge }
    113   1.1     ragge 
    114   1.1     ragge int
    115   1.1     ragge vsbus_match(parent, cf, aux)
    116   1.1     ragge 	struct	device	*parent;
    117   1.9     ragge 	struct cfdata	*cf;
    118   1.1     ragge 	void	*aux;
    119   1.1     ragge {
    120  1.32      matt #if VAX53 || VAXANY
    121  1.31     ragge 	/* Kludge: VAX53 is... special */
    122  1.31     ragge 	if (vax_boardtype == VAX_BTYP_53 && (int)aux == 1)
    123  1.31     ragge 		return 1; /* Hack */
    124  1.31     ragge #endif
    125  1.18     ragge 	if (vax_bustype == VAX_VSBUS)
    126  1.18     ragge 		return 1;
    127  1.18     ragge 	return 0;
    128   1.1     ragge }
    129   1.1     ragge 
    130   1.1     ragge void
    131   1.1     ragge vsbus_attach(parent, self, aux)
    132   1.1     ragge 	struct	device	*parent, *self;
    133   1.1     ragge 	void	*aux;
    134   1.1     ragge {
    135  1.10     ragge 	struct	vsbus_softc *sc = (void *)self;
    136  1.27     ragge 	int dbase, dsize;
    137   1.1     ragge 
    138   1.4  christos 	printf("\n");
    139  1.14     ragge 
    140  1.21      matt 	sc->sc_dmatag = vsbus_bus_dma_tag;
    141  1.21      matt 
    142  1.19     ragge 	switch (vax_boardtype) {
    143  1.31     ragge #if VAX49 || VAX53
    144  1.31     ragge 	case VAX_BTYP_53:
    145  1.19     ragge 	case VAX_BTYP_49:
    146  1.24      matt 		sc->sc_vsregs = vax_map_physmem(0x25c00000, 1);
    147  1.24      matt 		sc->sc_intreq = (char *)sc->sc_vsregs + 12;
    148  1.24      matt 		sc->sc_intclr = (char *)sc->sc_vsregs + 12;
    149  1.24      matt 		sc->sc_intmsk = (char *)sc->sc_vsregs + 8;
    150  1.24      matt 		vsbus_dma_init(sc, 8192);
    151  1.19     ragge 		break;
    152  1.22      matt #endif
    153  1.19     ragge 
    154  1.22      matt #if VAX46 || VAX48
    155  1.21      matt 	case VAX_BTYP_48:
    156  1.21      matt 	case VAX_BTYP_46:
    157  1.24      matt 		sc->sc_vsregs = vax_map_physmem(VS_REGS, 1);
    158  1.24      matt 		sc->sc_intreq = (char *)sc->sc_vsregs + 15;
    159  1.24      matt 		sc->sc_intclr = (char *)sc->sc_vsregs + 15;
    160  1.24      matt 		sc->sc_intmsk = (char *)sc->sc_vsregs + 12;
    161  1.24      matt 		vsbus_dma_init(sc, 32768);
    162  1.22      matt #endif
    163  1.21      matt 
    164  1.19     ragge 	default:
    165  1.24      matt 		sc->sc_vsregs = vax_map_physmem(VS_REGS, 1);
    166  1.24      matt 		sc->sc_intreq = (char *)sc->sc_vsregs + 15;
    167  1.24      matt 		sc->sc_intclr = (char *)sc->sc_vsregs + 15;
    168  1.24      matt 		sc->sc_intmsk = (char *)sc->sc_vsregs + 12;
    169  1.27     ragge 		if (vax_boardtype == VAX_BTYP_410) {
    170  1.27     ragge 			dbase = KA410_DMA_BASE;
    171  1.27     ragge 			dsize = KA410_DMA_SIZE;
    172  1.27     ragge 		} else {
    173  1.27     ragge 			dbase = KA420_DMA_BASE;
    174  1.27     ragge 			dsize = KA420_DMA_SIZE;
    175  1.27     ragge 			*(char *)(sc->sc_vsregs + 0xe0) = 1; /* Big DMA */
    176  1.27     ragge 		}
    177  1.27     ragge 		sc->sc_dmasize = dsize;
    178  1.27     ragge 		sc->sc_dmaaddr = uvm_km_valloc(kernel_map, dsize);
    179  1.27     ragge 		ioaccess(sc->sc_dmaaddr, dbase, dsize/VAX_NBPG);
    180  1.19     ragge 		break;
    181  1.19     ragge 	}
    182  1.16     ragge 
    183  1.27     ragge 	SIMPLEQ_INIT(&vsbus_dma);
    184   1.1     ragge 	/*
    185  1.16     ragge 	 * First: find which interrupts we won't care about.
    186  1.16     ragge 	 * There are interrupts that interrupt on a periodic basic
    187  1.16     ragge 	 * that we don't want to interfere with the rest of the
    188  1.16     ragge 	 * interrupt probing.
    189   1.1     ragge 	 */
    190  1.19     ragge 	*sc->sc_intmsk = 0;
    191  1.19     ragge 	*sc->sc_intclr = 0xff;
    192  1.16     ragge 	DELAY(1000000); /* Wait a second */
    193  1.19     ragge 	sc->sc_mask = *sc->sc_intreq;
    194  1.16     ragge 	printf("%s: interrupt mask %x\n", self->dv_xname, sc->sc_mask);
    195   1.1     ragge 	/*
    196   1.1     ragge 	 * now check for all possible devices on this "bus"
    197   1.1     ragge 	 */
    198  1.16     ragge 	config_search(vsbus_search, self, NULL);
    199  1.13     ragge 
    200  1.16     ragge 	/* Autoconfig finished, enable interrupts */
    201  1.19     ragge 	*sc->sc_intmsk = ~sc->sc_mask;
    202   1.1     ragge }
    203   1.1     ragge 
    204  1.16     ragge int
    205  1.16     ragge vsbus_search(parent, cf, aux)
    206  1.16     ragge 	struct device *parent;
    207  1.16     ragge 	struct cfdata *cf;
    208  1.16     ragge 	void *aux;
    209   1.1     ragge {
    210  1.16     ragge 	struct	vsbus_softc *sc = (void *)parent;
    211  1.16     ragge 	struct	vsbus_attach_args va;
    212  1.16     ragge 	int i, vec, br;
    213  1.16     ragge 	u_char c;
    214   1.1     ragge 
    215  1.16     ragge 	va.va_paddr = cf->cf_loc[0];
    216  1.16     ragge 	va.va_addr = vax_map_physmem(va.va_paddr, 1);
    217  1.21      matt 	va.va_dmat = &sc->sc_dmatag;
    218  1.21      matt 	va.va_iot = &vax_mem_bus_space;
    219   1.1     ragge 
    220  1.19     ragge 	*sc->sc_intmsk = 0;
    221  1.19     ragge 	*sc->sc_intclr = 0xff;
    222  1.16     ragge 	scb_vecref(0, 0); /* Clear vector ref */
    223  1.16     ragge 
    224  1.36   thorpej 	i = config_match(parent, cf, &va);
    225  1.16     ragge 	vax_unmap_physmem(va.va_addr, 1);
    226  1.19     ragge 	c = *sc->sc_intreq & ~sc->sc_mask;
    227  1.19     ragge 	if (i == 0)
    228  1.19     ragge 		goto forgetit;
    229  1.16     ragge 	if (i > 10)
    230  1.16     ragge 		c = sc->sc_mask; /* Fooling interrupt */
    231  1.19     ragge 	else if (c == 0)
    232  1.16     ragge 		goto forgetit;
    233  1.16     ragge 
    234  1.19     ragge 	*sc->sc_intmsk = c;
    235  1.16     ragge 	DELAY(100);
    236  1.19     ragge 	*sc->sc_intmsk = 0;
    237  1.16     ragge 	va.va_maskno = ffs((u_int)c);
    238  1.16     ragge 	i = scb_vecref(&vec, &br);
    239  1.16     ragge 	if (i == 0)
    240  1.16     ragge 		goto fail;
    241  1.16     ragge 	if (vec == 0)
    242  1.16     ragge 		goto fail;
    243  1.16     ragge 
    244  1.16     ragge 	va.va_br = br;
    245  1.16     ragge 	va.va_cvec = vec;
    246  1.27     ragge 	va.va_dmaaddr = sc->sc_dmaaddr;
    247  1.27     ragge 	va.va_dmasize = sc->sc_dmasize;
    248  1.27     ragge 	*sc->sc_intmsk = c; /* Allow interrupts during attach */
    249  1.16     ragge 	config_attach(parent, cf, &va, vsbus_print);
    250  1.27     ragge 	*sc->sc_intmsk = 0;
    251  1.16     ragge 	return 0;
    252  1.16     ragge 
    253  1.16     ragge fail:
    254  1.31     ragge 	printf("%s%d at %s csr 0x%x %s\n",
    255  1.35   thorpej 	    cf->cf_name, cf->cf_unit, parent->dv_xname,
    256  1.16     ragge 	    cf->cf_loc[0], (i ? "zero vector" : "didn't interrupt"));
    257  1.16     ragge forgetit:
    258  1.16     ragge 	return 0;
    259   1.1     ragge }
    260   1.1     ragge 
    261  1.16     ragge /*
    262  1.16     ragge  * Sets a new interrupt mask. Returns the old one.
    263  1.16     ragge  * Works like spl functions.
    264  1.16     ragge  */
    265  1.16     ragge unsigned char
    266  1.16     ragge vsbus_setmask(mask)
    267  1.16     ragge 	unsigned char mask;
    268   1.1     ragge {
    269  1.25     ragge 	struct vsbus_softc *sc;
    270  1.16     ragge 	unsigned char ch;
    271   1.1     ragge 
    272  1.25     ragge 	if (vsbus_cd.cd_ndevs == 0)
    273  1.25     ragge 		return 0;
    274  1.25     ragge 	sc = vsbus_cd.cd_devs[0];
    275  1.25     ragge 
    276  1.19     ragge 	ch = *sc->sc_intmsk;
    277  1.19     ragge 	*sc->sc_intmsk = mask;
    278  1.16     ragge 	return ch;
    279   1.1     ragge }
    280   1.1     ragge 
    281  1.16     ragge /*
    282  1.16     ragge  * Clears the interrupts in mask.
    283  1.16     ragge  */
    284  1.10     ragge void
    285  1.16     ragge vsbus_clrintr(mask)
    286  1.16     ragge 	unsigned char mask;
    287   1.1     ragge {
    288  1.25     ragge 	struct vsbus_softc *sc;
    289  1.25     ragge 
    290  1.25     ragge 	if (vsbus_cd.cd_ndevs == 0)
    291  1.25     ragge 		return;
    292  1.25     ragge 	sc = vsbus_cd.cd_devs[0];
    293   1.1     ragge 
    294  1.19     ragge 	*sc->sc_intclr = mask;
    295   1.1     ragge }
    296   1.1     ragge 
    297   1.1     ragge /*
    298  1.20     ragge  * Copy data from/to a user process' space from the DMA area.
    299  1.20     ragge  * Use the physical memory directly.
    300   1.1     ragge  */
    301  1.20     ragge void
    302  1.27     ragge vsbus_copytoproc(struct proc *p, caddr_t from, caddr_t to, int len)
    303  1.20     ragge {
    304  1.20     ragge 	struct pte *pte;
    305  1.20     ragge 	paddr_t pa;
    306   1.1     ragge 
    307  1.30       chs 	if ((vaddr_t)to & KERNBASE) { /* In kernel space */
    308  1.27     ragge 		bcopy(from, to, len);
    309  1.27     ragge 		return;
    310  1.27     ragge 	}
    311  1.41   thorpej 	if ((long)to & 0x40000000)
    312  1.41   thorpej 		pte = &p->p_vmspace->vm_map.pmap->pm_p1br[((long)to & ~0x40000000)];
    313  1.41   thorpej 	else
    314  1.41   thorpej 		pte = &p->p_vmspace->vm_map.pmap->pm_p0br[(long)to];
    315  1.20     ragge 	if ((vaddr_t)to & PGOFSET) {
    316  1.30       chs 		int cz = round_page((vaddr_t)to) - (vaddr_t)to;
    317  1.20     ragge 
    318  1.42   thorpej 		pa = (pte->pg_pfn << VAX_PGSHIFT) | (PAGE_SIZE - cz) | KERNBASE;
    319  1.20     ragge 		bcopy(from, (caddr_t)pa, min(cz, len));
    320  1.20     ragge 		from += cz;
    321  1.20     ragge 		to += cz;
    322  1.20     ragge 		len -= cz;
    323  1.20     ragge 		pte += 8; /* XXX */
    324  1.20     ragge 	}
    325  1.20     ragge 	while (len > 0) {
    326  1.20     ragge 		pa = (pte->pg_pfn << VAX_PGSHIFT) | KERNBASE;
    327  1.42   thorpej 		bcopy(from, (caddr_t)pa, min(PAGE_SIZE, len));
    328  1.42   thorpej 		from += PAGE_SIZE;
    329  1.42   thorpej 		to += PAGE_SIZE;
    330  1.42   thorpej 		len -= PAGE_SIZE;
    331  1.20     ragge 		pte += 8; /* XXX */
    332  1.20     ragge 	}
    333  1.20     ragge }
    334   1.1     ragge 
    335  1.20     ragge void
    336  1.27     ragge vsbus_copyfromproc(struct proc *p, caddr_t from, caddr_t to, int len)
    337  1.20     ragge {
    338  1.20     ragge 	struct pte *pte;
    339  1.20     ragge 	paddr_t pa;
    340  1.20     ragge 
    341  1.30       chs 	if ((vaddr_t)from & KERNBASE) { /* In kernel space */
    342  1.27     ragge 		bcopy(from, to, len);
    343  1.27     ragge 		return;
    344  1.27     ragge 	}
    345  1.41   thorpej 	if ((long)to & 0x40000000)
    346  1.41   thorpej 		pte = &p->p_vmspace->vm_map.pmap->pm_p1br[((long)to & ~0x40000000)];
    347  1.41   thorpej 	else
    348  1.41   thorpej 		pte = &p->p_vmspace->vm_map.pmap->pm_p0br[(long)to];
    349  1.20     ragge 	if ((vaddr_t)from & PGOFSET) {
    350  1.30       chs 		int cz = round_page((vaddr_t)from) - (vaddr_t)from;
    351  1.20     ragge 
    352  1.42   thorpej 		pa = (pte->pg_pfn << VAX_PGSHIFT) | (PAGE_SIZE - cz) | KERNBASE;
    353  1.20     ragge 		bcopy((caddr_t)pa, to, min(cz, len));
    354  1.20     ragge 		from += cz;
    355  1.20     ragge 		to += cz;
    356  1.20     ragge 		len -= cz;
    357  1.20     ragge 		pte += 8; /* XXX */
    358  1.20     ragge 	}
    359  1.20     ragge 	while (len > 0) {
    360  1.20     ragge 		pa = (pte->pg_pfn << VAX_PGSHIFT) | KERNBASE;
    361  1.42   thorpej 		bcopy((caddr_t)pa, to, min(PAGE_SIZE, len));
    362  1.42   thorpej 		from += PAGE_SIZE;
    363  1.42   thorpej 		to += PAGE_SIZE;
    364  1.42   thorpej 		len -= PAGE_SIZE;
    365  1.20     ragge 		pte += 8; /* XXX */
    366  1.20     ragge 	}
    367  1.20     ragge }
    368  1.27     ragge 
    369  1.27     ragge /*
    370  1.27     ragge  * There can only be one user of the DMA area on VS2k/VS3100 at one
    371  1.27     ragge  * time, so keep track of it here.
    372  1.27     ragge  */
    373  1.27     ragge static int vsbus_active = 0;
    374  1.27     ragge 
    375  1.27     ragge void
    376  1.27     ragge vsbus_dma_start(struct vsbus_dma *vd)
    377  1.27     ragge {
    378  1.27     ragge 
    379  1.27     ragge 	SIMPLEQ_INSERT_TAIL(&vsbus_dma, vd, vd_q);
    380  1.27     ragge 
    381  1.27     ragge 	if (vsbus_active == 0)
    382  1.27     ragge 		vsbus_dma_intr();
    383  1.27     ragge }
    384  1.27     ragge 
    385  1.27     ragge void
    386  1.27     ragge vsbus_dma_intr(void)
    387  1.27     ragge {
    388  1.27     ragge 	struct vsbus_dma *vd;
    389  1.27     ragge 
    390  1.27     ragge 	vd = SIMPLEQ_FIRST(&vsbus_dma);
    391  1.27     ragge 	if (vd == NULL) {
    392  1.27     ragge 		vsbus_active = 0;
    393  1.27     ragge 		return;
    394  1.27     ragge 	}
    395  1.27     ragge 	vsbus_active = 1;
    396  1.33     lukem 	SIMPLEQ_REMOVE_HEAD(&vsbus_dma, vd_q);
    397  1.27     ragge 	(*vd->vd_go)(vd->vd_arg);
    398  1.27     ragge }
    399  1.27     ragge 
    400