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