Home | History | Annotate | Line # | Download | only in vsa
vsbus.c revision 1.54.4.2
      1  1.54.4.2      yamt /*	$NetBSD: vsbus.c,v 1.54.4.2 2010/03/11 15:03:06 yamt 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.44     lukem 
     35      1.44     lukem #include <sys/cdefs.h>
     36  1.54.4.2      yamt __KERNEL_RCSID(0, "$NetBSD: vsbus.c,v 1.54.4.2 2010/03/11 15:03:06 yamt Exp $");
     37       1.1     ragge 
     38       1.1     ragge #include <sys/param.h>
     39       1.1     ragge #include <sys/systm.h>
     40       1.1     ragge #include <sys/buf.h>
     41       1.1     ragge #include <sys/conf.h>
     42       1.1     ragge #include <sys/file.h>
     43       1.1     ragge #include <sys/ioctl.h>
     44       1.1     ragge #include <sys/proc.h>
     45       1.1     ragge #include <sys/device.h>
     46       1.1     ragge #include <sys/disklabel.h>
     47       1.1     ragge #include <sys/syslog.h>
     48       1.1     ragge #include <sys/stat.h>
     49       1.1     ragge 
     50      1.29       mrg #include <uvm/uvm_extern.h>
     51      1.20     ragge 
     52      1.27     ragge #define _VAX_BUS_DMA_PRIVATE
     53      1.17     ragge #include <machine/bus.h>
     54       1.1     ragge #include <machine/pte.h>
     55       1.1     ragge #include <machine/sid.h>
     56       1.1     ragge #include <machine/scb.h>
     57       1.1     ragge #include <machine/cpu.h>
     58       1.1     ragge #include <machine/trap.h>
     59       1.1     ragge #include <machine/nexus.h>
     60       1.1     ragge 
     61       1.1     ragge #include <machine/uvax.h>
     62       1.1     ragge #include <machine/ka410.h>
     63      1.12     ragge #include <machine/ka420.h>
     64       1.1     ragge #include <machine/ka43.h>
     65       1.1     ragge 
     66      1.54      matt #include <machine/mainbus.h>
     67       1.1     ragge #include <machine/vsbus.h>
     68       1.1     ragge 
     69      1.10     ragge #include "ioconf.h"
     70      1.46       chs #include "locators.h"
     71      1.26      matt #include "opt_cputype.h"
     72       1.1     ragge 
     73      1.54      matt static int	vsbus_match(device_t, cfdata_t, void *);
     74      1.54      matt static void	vsbus_attach(device_t, device_t, void *);
     75      1.54      matt static int	vsbus_print(void *, const char *);
     76      1.54      matt static int	vsbus_search(device_t, cfdata_t, const int *, void *);
     77       1.1     ragge 
     78      1.27     ragge static SIMPLEQ_HEAD(, vsbus_dma) vsbus_dma;
     79      1.21      matt 
     80      1.54      matt CFATTACH_DECL_NEW(vsbus, sizeof(struct vsbus_softc),
     81      1.39   thorpej     vsbus_match, vsbus_attach, NULL, NULL);
     82       1.6     ragge 
     83      1.54      matt static struct vax_bus_dma_tag vsbus_bus_dma_tag = {
     84      1.54      matt 	._dmamap_create		= _bus_dmamap_create,
     85      1.54      matt 	._dmamap_destroy	= _bus_dmamap_destroy,
     86      1.54      matt 	._dmamap_load		= _bus_dmamap_load,
     87      1.54      matt 	._dmamap_load_mbuf	= _bus_dmamap_load_mbuf,
     88      1.54      matt 	._dmamap_load_uio	= _bus_dmamap_load_uio,
     89      1.54      matt 	._dmamap_load_raw	= _bus_dmamap_load_raw,
     90      1.54      matt 	._dmamap_unload		= _bus_dmamap_unload,
     91      1.54      matt 	._dmamap_sync		= _bus_dmamap_sync,
     92      1.54      matt 	._dmamem_alloc		= _bus_dmamem_alloc,
     93      1.54      matt 	._dmamem_free		= _bus_dmamem_free,
     94      1.54      matt 	._dmamem_map		= _bus_dmamem_map,
     95      1.54      matt 	._dmamem_unmap		= _bus_dmamem_unmap,
     96      1.54      matt 	._dmamem_mmap		= _bus_dmamem_mmap,
     97      1.54      matt };
     98      1.54      matt 
     99       1.1     ragge int
    100      1.54      matt vsbus_print(void *aux, const char *name)
    101       1.1     ragge {
    102      1.54      matt 	struct vsbus_attach_args * const va = aux;
    103       1.1     ragge 
    104      1.40   thorpej 	aprint_normal(" csr 0x%lx vec %o ipl %x maskbit %d", va->va_paddr,
    105      1.16     ragge 	    va->va_cvec & 511, va->va_br, va->va_maskno - 1);
    106      1.54      matt 	return UNCONF;
    107       1.1     ragge }
    108       1.1     ragge 
    109       1.1     ragge int
    110      1.54      matt vsbus_match(device_t parent, cfdata_t cf, void *aux)
    111       1.1     ragge {
    112      1.54      matt 	struct mainbus_attach_args * const ma = aux;
    113      1.54      matt 
    114      1.54      matt 	return !strcmp("vsbus", ma->ma_type);
    115       1.1     ragge }
    116       1.1     ragge 
    117       1.1     ragge void
    118      1.54      matt vsbus_attach(device_t parent, device_t self, void *aux)
    119       1.1     ragge {
    120      1.54      matt 	struct mainbus_attach_args * const ma = aux;
    121      1.54      matt 	struct vsbus_softc *sc = device_private(self);
    122      1.27     ragge 	int dbase, dsize;
    123       1.1     ragge 
    124      1.54      matt 	aprint_normal("\n");
    125      1.14     ragge 
    126      1.54      matt 	sc->sc_dev = self;
    127      1.54      matt 	sc->sc_iot = ma->ma_iot;
    128      1.21      matt 	sc->sc_dmatag = vsbus_bus_dma_tag;
    129      1.21      matt 
    130      1.19     ragge 	switch (vax_boardtype) {
    131      1.31     ragge #if VAX49 || VAX53
    132      1.31     ragge 	case VAX_BTYP_53:
    133      1.19     ragge 	case VAX_BTYP_49:
    134      1.24      matt 		sc->sc_vsregs = vax_map_physmem(0x25c00000, 1);
    135      1.24      matt 		sc->sc_intreq = (char *)sc->sc_vsregs + 12;
    136      1.24      matt 		sc->sc_intclr = (char *)sc->sc_vsregs + 12;
    137      1.24      matt 		sc->sc_intmsk = (char *)sc->sc_vsregs + 8;
    138      1.24      matt 		vsbus_dma_init(sc, 8192);
    139      1.19     ragge 		break;
    140      1.22      matt #endif
    141      1.19     ragge 
    142      1.22      matt #if VAX46 || VAX48
    143      1.21      matt 	case VAX_BTYP_48:
    144      1.21      matt 	case VAX_BTYP_46:
    145      1.24      matt 		sc->sc_vsregs = vax_map_physmem(VS_REGS, 1);
    146      1.24      matt 		sc->sc_intreq = (char *)sc->sc_vsregs + 15;
    147      1.24      matt 		sc->sc_intclr = (char *)sc->sc_vsregs + 15;
    148      1.24      matt 		sc->sc_intmsk = (char *)sc->sc_vsregs + 12;
    149      1.24      matt 		vsbus_dma_init(sc, 32768);
    150      1.22      matt #endif
    151      1.21      matt 
    152      1.19     ragge 	default:
    153      1.24      matt 		sc->sc_vsregs = vax_map_physmem(VS_REGS, 1);
    154      1.24      matt 		sc->sc_intreq = (char *)sc->sc_vsregs + 15;
    155      1.24      matt 		sc->sc_intclr = (char *)sc->sc_vsregs + 15;
    156      1.24      matt 		sc->sc_intmsk = (char *)sc->sc_vsregs + 12;
    157      1.27     ragge 		if (vax_boardtype == VAX_BTYP_410) {
    158      1.27     ragge 			dbase = KA410_DMA_BASE;
    159      1.27     ragge 			dsize = KA410_DMA_SIZE;
    160      1.27     ragge 		} else {
    161      1.27     ragge 			dbase = KA420_DMA_BASE;
    162      1.27     ragge 			dsize = KA420_DMA_SIZE;
    163      1.27     ragge 			*(char *)(sc->sc_vsregs + 0xe0) = 1; /* Big DMA */
    164      1.27     ragge 		}
    165      1.27     ragge 		sc->sc_dmasize = dsize;
    166      1.47      yamt 		sc->sc_dmaaddr = uvm_km_alloc(kernel_map, dsize, 0,
    167      1.47      yamt 		    UVM_KMF_VAONLY);
    168      1.27     ragge 		ioaccess(sc->sc_dmaaddr, dbase, dsize/VAX_NBPG);
    169      1.19     ragge 		break;
    170      1.19     ragge 	}
    171      1.16     ragge 
    172      1.27     ragge 	SIMPLEQ_INIT(&vsbus_dma);
    173       1.1     ragge 	/*
    174      1.16     ragge 	 * First: find which interrupts we won't care about.
    175      1.16     ragge 	 * There are interrupts that interrupt on a periodic basic
    176      1.16     ragge 	 * that we don't want to interfere with the rest of the
    177      1.16     ragge 	 * interrupt probing.
    178       1.1     ragge 	 */
    179      1.19     ragge 	*sc->sc_intmsk = 0;
    180      1.19     ragge 	*sc->sc_intclr = 0xff;
    181      1.16     ragge 	DELAY(1000000); /* Wait a second */
    182      1.19     ragge 	sc->sc_mask = *sc->sc_intreq;
    183      1.54      matt 	aprint_normal_dev(self, "interrupt mask %x\n", sc->sc_mask);
    184       1.1     ragge 	/*
    185       1.1     ragge 	 * now check for all possible devices on this "bus"
    186       1.1     ragge 	 */
    187      1.48  drochner 	config_search_ia(vsbus_search, self, "vsbus", NULL);
    188      1.13     ragge 
    189      1.16     ragge 	/* Autoconfig finished, enable interrupts */
    190      1.19     ragge 	*sc->sc_intmsk = ~sc->sc_mask;
    191       1.1     ragge }
    192       1.1     ragge 
    193      1.16     ragge int
    194      1.54      matt vsbus_search(device_t parent, cfdata_t cf, const int *ldesc, void *aux)
    195       1.1     ragge {
    196      1.54      matt 	struct vsbus_softc *sc = device_private(parent);
    197      1.54      matt 	struct vsbus_attach_args va;
    198      1.16     ragge 	int i, vec, br;
    199      1.16     ragge 	u_char c;
    200       1.1     ragge 
    201      1.46       chs 	va.va_paddr = cf->cf_loc[VSBUSCF_CSR];
    202      1.16     ragge 	va.va_addr = vax_map_physmem(va.va_paddr, 1);
    203      1.21      matt 	va.va_dmat = &sc->sc_dmatag;
    204      1.54      matt 	va.va_memt = sc->sc_iot;
    205       1.1     ragge 
    206      1.19     ragge 	*sc->sc_intmsk = 0;
    207      1.19     ragge 	*sc->sc_intclr = 0xff;
    208      1.16     ragge 	scb_vecref(0, 0); /* Clear vector ref */
    209      1.16     ragge 
    210      1.36   thorpej 	i = config_match(parent, cf, &va);
    211      1.16     ragge 	vax_unmap_physmem(va.va_addr, 1);
    212      1.19     ragge 	c = *sc->sc_intreq & ~sc->sc_mask;
    213      1.19     ragge 	if (i == 0)
    214      1.19     ragge 		goto forgetit;
    215      1.16     ragge 	if (i > 10)
    216      1.16     ragge 		c = sc->sc_mask; /* Fooling interrupt */
    217      1.19     ragge 	else if (c == 0)
    218      1.16     ragge 		goto forgetit;
    219      1.16     ragge 
    220      1.19     ragge 	*sc->sc_intmsk = c;
    221      1.16     ragge 	DELAY(100);
    222      1.19     ragge 	*sc->sc_intmsk = 0;
    223      1.16     ragge 	va.va_maskno = ffs((u_int)c);
    224      1.16     ragge 	i = scb_vecref(&vec, &br);
    225      1.16     ragge 	if (i == 0)
    226      1.16     ragge 		goto fail;
    227      1.16     ragge 	if (vec == 0)
    228      1.16     ragge 		goto fail;
    229      1.16     ragge 
    230      1.16     ragge 	va.va_br = br;
    231      1.16     ragge 	va.va_cvec = vec;
    232      1.27     ragge 	va.va_dmaaddr = sc->sc_dmaaddr;
    233      1.27     ragge 	va.va_dmasize = sc->sc_dmasize;
    234      1.27     ragge 	*sc->sc_intmsk = c; /* Allow interrupts during attach */
    235      1.16     ragge 	config_attach(parent, cf, &va, vsbus_print);
    236      1.27     ragge 	*sc->sc_intmsk = 0;
    237      1.16     ragge 	return 0;
    238      1.16     ragge 
    239      1.16     ragge fail:
    240      1.31     ragge 	printf("%s%d at %s csr 0x%x %s\n",
    241      1.54      matt 	    cf->cf_name, cf->cf_unit, device_xname(parent),
    242      1.46       chs 	    cf->cf_loc[VSBUSCF_CSR], (i ? "zero vector" : "didn't interrupt"));
    243      1.16     ragge forgetit:
    244      1.16     ragge 	return 0;
    245       1.1     ragge }
    246       1.1     ragge 
    247      1.16     ragge /*
    248      1.16     ragge  * Sets a new interrupt mask. Returns the old one.
    249      1.16     ragge  * Works like spl functions.
    250      1.16     ragge  */
    251      1.16     ragge unsigned char
    252      1.54      matt vsbus_setmask(int mask)
    253       1.1     ragge {
    254      1.54      matt 	struct vsbus_softc * const sc = device_lookup_private(&vsbus_cd, 0);
    255      1.16     ragge 	unsigned char ch;
    256       1.1     ragge 
    257      1.54      matt 	if (sc == NULL)
    258      1.25     ragge 		return 0;
    259      1.25     ragge 
    260      1.19     ragge 	ch = *sc->sc_intmsk;
    261      1.19     ragge 	*sc->sc_intmsk = mask;
    262      1.16     ragge 	return ch;
    263       1.1     ragge }
    264       1.1     ragge 
    265      1.16     ragge /*
    266      1.16     ragge  * Clears the interrupts in mask.
    267      1.16     ragge  */
    268      1.10     ragge void
    269      1.54      matt vsbus_clrintr(int mask)
    270       1.1     ragge {
    271      1.54      matt 	struct vsbus_softc * const sc = device_lookup_private(&vsbus_cd, 0);
    272      1.54      matt 	if (sc == NULL)
    273      1.25     ragge 		return;
    274       1.1     ragge 
    275      1.19     ragge 	*sc->sc_intclr = mask;
    276       1.1     ragge }
    277       1.1     ragge 
    278       1.1     ragge /*
    279      1.20     ragge  * Copy data from/to a user process' space from the DMA area.
    280      1.20     ragge  * Use the physical memory directly.
    281       1.1     ragge  */
    282      1.20     ragge void
    283      1.52  christos vsbus_copytoproc(struct proc *p, void *fromv, void *tov, int len)
    284      1.20     ragge {
    285      1.52  christos 	char *from = fromv, *to = tov;
    286      1.20     ragge 	struct pte *pte;
    287      1.20     ragge 	paddr_t pa;
    288       1.1     ragge 
    289      1.30       chs 	if ((vaddr_t)to & KERNBASE) { /* In kernel space */
    290  1.54.4.2      yamt 		memcpy(to, from, len);
    291      1.27     ragge 		return;
    292      1.27     ragge 	}
    293      1.45     ragge 
    294      1.45     ragge #ifdef DIAGNOSTIC
    295      1.45     ragge 	if (p == NULL)
    296      1.45     ragge 		panic("vsbus_copytoproc: no proc");
    297      1.45     ragge #endif
    298      1.45     ragge 
    299      1.45     ragge 	if ((vaddr_t)to & 0x40000000)
    300      1.45     ragge 		pte = &p->p_vmspace->vm_map.pmap->pm_p1br[vax_btop((vaddr_t)to & ~0x40000000)];
    301      1.41   thorpej 	else
    302      1.45     ragge 		pte = &p->p_vmspace->vm_map.pmap->pm_p0br[vax_btop((vaddr_t)to)];
    303      1.20     ragge 	if ((vaddr_t)to & PGOFSET) {
    304      1.30       chs 		int cz = round_page((vaddr_t)to) - (vaddr_t)to;
    305      1.20     ragge 
    306      1.42   thorpej 		pa = (pte->pg_pfn << VAX_PGSHIFT) | (PAGE_SIZE - cz) | KERNBASE;
    307  1.54.4.1      yamt 		memcpy((void *)pa, from, min(cz, len));
    308      1.20     ragge 		from += cz;
    309      1.20     ragge 		to += cz;
    310      1.20     ragge 		len -= cz;
    311      1.20     ragge 		pte += 8; /* XXX */
    312      1.20     ragge 	}
    313      1.20     ragge 	while (len > 0) {
    314      1.20     ragge 		pa = (pte->pg_pfn << VAX_PGSHIFT) | KERNBASE;
    315  1.54.4.1      yamt 		memcpy((void *)pa, from, min(PAGE_SIZE, len));
    316      1.42   thorpej 		from += PAGE_SIZE;
    317      1.42   thorpej 		to += PAGE_SIZE;
    318      1.42   thorpej 		len -= PAGE_SIZE;
    319      1.20     ragge 		pte += 8; /* XXX */
    320      1.20     ragge 	}
    321      1.20     ragge }
    322       1.1     ragge 
    323      1.20     ragge void
    324      1.52  christos vsbus_copyfromproc(struct proc *p, void *fromv, void *tov, int len)
    325      1.20     ragge {
    326      1.52  christos 	char *from = fromv, *to = tov;
    327      1.20     ragge 	struct pte *pte;
    328      1.20     ragge 	paddr_t pa;
    329      1.20     ragge 
    330      1.30       chs 	if ((vaddr_t)from & KERNBASE) { /* In kernel space */
    331  1.54.4.2      yamt 		memcpy(to, from, len);
    332      1.27     ragge 		return;
    333      1.27     ragge 	}
    334      1.45     ragge 
    335      1.45     ragge #ifdef DIAGNOSTIC
    336      1.45     ragge 	if (p == NULL)
    337      1.45     ragge 		panic("vsbus_copyfromproc: no proc");
    338      1.45     ragge #endif
    339      1.45     ragge 
    340      1.45     ragge 	if ((vaddr_t)from & 0x40000000)
    341      1.45     ragge 		pte = &p->p_vmspace->vm_map.pmap->pm_p1br[vax_btop((vaddr_t)from & ~0x40000000)];
    342      1.41   thorpej 	else
    343      1.45     ragge 		pte = &p->p_vmspace->vm_map.pmap->pm_p0br[vax_btop((vaddr_t)from)];
    344      1.20     ragge 	if ((vaddr_t)from & PGOFSET) {
    345      1.30       chs 		int cz = round_page((vaddr_t)from) - (vaddr_t)from;
    346      1.20     ragge 
    347      1.42   thorpej 		pa = (pte->pg_pfn << VAX_PGSHIFT) | (PAGE_SIZE - cz) | KERNBASE;
    348  1.54.4.1      yamt 		memcpy(to, (void *)pa, min(cz, len));
    349      1.20     ragge 		from += cz;
    350      1.20     ragge 		to += cz;
    351      1.20     ragge 		len -= cz;
    352      1.20     ragge 		pte += 8; /* XXX */
    353      1.20     ragge 	}
    354      1.20     ragge 	while (len > 0) {
    355      1.20     ragge 		pa = (pte->pg_pfn << VAX_PGSHIFT) | KERNBASE;
    356  1.54.4.1      yamt 		memcpy(to,  (void *)pa, min(PAGE_SIZE, len));
    357      1.42   thorpej 		from += PAGE_SIZE;
    358      1.42   thorpej 		to += PAGE_SIZE;
    359      1.42   thorpej 		len -= PAGE_SIZE;
    360      1.20     ragge 		pte += 8; /* XXX */
    361      1.20     ragge 	}
    362      1.20     ragge }
    363      1.27     ragge 
    364      1.27     ragge /*
    365      1.27     ragge  * There can only be one user of the DMA area on VS2k/VS3100 at one
    366      1.27     ragge  * time, so keep track of it here.
    367      1.27     ragge  */
    368      1.27     ragge static int vsbus_active = 0;
    369      1.27     ragge 
    370      1.27     ragge void
    371      1.27     ragge vsbus_dma_start(struct vsbus_dma *vd)
    372      1.27     ragge {
    373      1.27     ragge 
    374      1.27     ragge 	SIMPLEQ_INSERT_TAIL(&vsbus_dma, vd, vd_q);
    375      1.27     ragge 
    376      1.27     ragge 	if (vsbus_active == 0)
    377      1.27     ragge 		vsbus_dma_intr();
    378      1.27     ragge }
    379      1.27     ragge 
    380      1.27     ragge void
    381      1.27     ragge vsbus_dma_intr(void)
    382      1.27     ragge {
    383      1.27     ragge 	struct vsbus_dma *vd;
    384      1.27     ragge 
    385      1.27     ragge 	vd = SIMPLEQ_FIRST(&vsbus_dma);
    386      1.27     ragge 	if (vd == NULL) {
    387      1.27     ragge 		vsbus_active = 0;
    388      1.27     ragge 		return;
    389      1.27     ragge 	}
    390      1.27     ragge 	vsbus_active = 1;
    391      1.33     lukem 	SIMPLEQ_REMOVE_HEAD(&vsbus_dma, vd_q);
    392      1.27     ragge 	(*vd->vd_go)(vd->vd_arg);
    393      1.27     ragge }
    394      1.27     ragge 
    395