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