Home | History | Annotate | Line # | Download | only in vsa
      1 /*	$NetBSD: vsbus.c,v 1.69 2024/02/17 17:41:43 tsutsui 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.69 2024/02/17 17:41:43 tsutsui 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 		}
    150 		sc->sc_dmasize = dsize;
    151 		sc->sc_dmaaddr = uvm_km_alloc(kernel_map, dsize, 0,
    152 		    UVM_KMF_VAONLY);
    153 		ioaccess(sc->sc_dmaaddr, dbase, dsize/VAX_NBPG);
    154 		break;
    155 	}
    156 
    157 	SIMPLEQ_INIT(&vsbus_dma);
    158 	/*
    159 	 * First: find which interrupts we won't care about.
    160 	 * There are interrupts that interrupt on a periodic basic
    161 	 * that we don't want to interfere with the rest of the
    162 	 * interrupt probing.
    163 	 */
    164 	*sc->sc_intmsk = 0;
    165 	*sc->sc_intclr = 0xff;
    166 	DELAY(1000000); /* Wait a second */
    167 	sc->sc_mask = *sc->sc_intreq;
    168 	aprint_normal_dev(self, "interrupt mask %x\n", sc->sc_mask);
    169 	/*
    170 	 * now check for all possible devices on this "bus"
    171 	 */
    172 	config_search(self, NULL,
    173 	    CFARGS(.search = vsbus_search));
    174 
    175 	/* Autoconfig finished, enable interrupts */
    176 	*sc->sc_intmsk = ~sc->sc_mask;
    177 }
    178 
    179 int
    180 vsbus_search(device_t parent, cfdata_t cf, const int *ldesc, void *aux)
    181 {
    182 	struct vsbus_softc *sc = device_private(parent);
    183 	struct vsbus_attach_args va;
    184 	int i, vec, br;
    185 	u_char c;
    186 
    187 	va.va_paddr = cf->cf_loc[VSBUSCF_CSR];
    188 	va.va_addr = vax_map_physmem(va.va_paddr, 1);
    189 	va.va_dmat = &sc->sc_dmatag;
    190 	va.va_memt = sc->sc_iot;
    191 
    192 	*sc->sc_intmsk = 0;
    193 	*sc->sc_intclr = 0xff;
    194 	scb_vecref(0, 0); /* Clear vector ref */
    195 
    196 	i = config_probe(parent, cf, &va);
    197 	vax_unmap_physmem(va.va_addr, 1);
    198 	c = *sc->sc_intreq & ~sc->sc_mask;
    199 	if (i == 0)
    200 		goto forgetit;
    201 	if (i > 10)
    202 		c = sc->sc_mask; /* Fooling interrupt */
    203 	else if (c == 0)
    204 		goto forgetit;
    205 
    206 	*sc->sc_intmsk = c;
    207 	DELAY(100);
    208 	*sc->sc_intmsk = 0;
    209 	va.va_maskno = ffs((u_int)c);
    210 	i = scb_vecref(&vec, &br);
    211 	if (i == 0)
    212 		goto fail;
    213 	if (vec == 0)
    214 		goto fail;
    215 
    216 	va.va_br = br;
    217 	va.va_cvec = vec;
    218 	va.va_dmaaddr = sc->sc_dmaaddr;
    219 	va.va_dmasize = sc->sc_dmasize;
    220 	*sc->sc_intmsk = c; /* Allow interrupts during attach */
    221 	config_attach(parent, cf, &va, vsbus_print, CFARGS_NONE);
    222 	*sc->sc_intmsk = 0;
    223 	return 0;
    224 
    225 fail:
    226 	printf("%s%d at %s csr 0x%x %s\n",
    227 	    cf->cf_name, cf->cf_unit, device_xname(parent),
    228 	    cf->cf_loc[VSBUSCF_CSR], (i ? "zero vector" : "didn't interrupt"));
    229 forgetit:
    230 	return 0;
    231 }
    232 
    233 /*
    234  * Sets a new interrupt mask. Returns the old one.
    235  * Works like spl functions.
    236  */
    237 unsigned char
    238 vsbus_setmask(int mask)
    239 {
    240 	struct vsbus_softc * const sc = device_lookup_private(&vsbus_cd, 0);
    241 	unsigned char ch;
    242 
    243 	if (sc == NULL)
    244 		return 0;
    245 
    246 	ch = *sc->sc_intmsk;
    247 	*sc->sc_intmsk = mask;
    248 	return ch;
    249 }
    250 
    251 /*
    252  * Clears the interrupts in mask.
    253  */
    254 void
    255 vsbus_clrintr(int mask)
    256 {
    257 	struct vsbus_softc * const sc = device_lookup_private(&vsbus_cd, 0);
    258 	if (sc == NULL)
    259 		return;
    260 
    261 	*sc->sc_intclr = mask;
    262 }
    263 
    264 #define puvtopte(va, pmap) \
    265 	(((vaddr_t)va < 0x40000000) ? \
    266 	&(((pmap)->pm_p0br)[PG_PFNUM(va)]) : \
    267 	&(((pmap)->pm_p1br)[PG_PFNUM(va)]))
    268 
    269 /*
    270  * Copy data from/to a user process' space from the DMA area.
    271  * Use the physical memory directly.
    272  */
    273 void
    274 vsbus_copytoproc(struct proc *p, void *fromv, void *tov, int len)
    275 {
    276 	char *from = fromv, *to = tov;
    277 	struct pmap *pm;
    278 	struct pte *pte;
    279 	paddr_t pa;
    280 
    281 	if ((vaddr_t)to & KERNBASE) { /* In kernel space */
    282 		memcpy(to, from, len);
    283 		return;
    284 	}
    285 
    286 #ifdef DIAGNOSTIC
    287 	if (p == NULL)
    288 		panic("vsbus_copytoproc: no proc");
    289 #endif
    290 
    291 	pm = p->p_vmspace->vm_map.pmap;
    292 	pte = puvtopte(trunc_page((vaddr_t)to), pm);
    293 	if ((vaddr_t)to & PGOFSET) {
    294 		int cz = round_page((vaddr_t)to) - (vaddr_t)to;
    295 
    296 		pa = (pte->pg_pfn << VAX_PGSHIFT) | (PAGE_SIZE - cz) | KERNBASE;
    297 		memcpy((void *)pa, from, uimin(cz, len));
    298 		from += cz;
    299 		to += cz;
    300 		len -= cz;
    301 		pte += 8; /* XXX */
    302 	}
    303 	while (len > 0) {
    304 		pa = (pte->pg_pfn << VAX_PGSHIFT) | KERNBASE;
    305 		memcpy((void *)pa, from, uimin(PAGE_SIZE, len));
    306 		from += PAGE_SIZE;
    307 		to += PAGE_SIZE;
    308 		len -= PAGE_SIZE;
    309 		pte += 8; /* XXX */
    310 	}
    311 }
    312 
    313 void
    314 vsbus_copyfromproc(struct proc *p, void *fromv, void *tov, int len)
    315 {
    316 	char *from = fromv, *to = tov;
    317 	struct pmap *pm;
    318 	struct pte *pte;
    319 	paddr_t pa;
    320 
    321 	if ((vaddr_t)from & KERNBASE) { /* In kernel space */
    322 		memcpy(to, from, len);
    323 		return;
    324 	}
    325 
    326 #ifdef DIAGNOSTIC
    327 	if (p == NULL)
    328 		panic("vsbus_copyfromproc: no proc");
    329 #endif
    330 
    331 	pm = p->p_vmspace->vm_map.pmap;
    332 	pte = puvtopte(trunc_page((vaddr_t)from), pm);
    333 	if ((vaddr_t)from & PGOFSET) {
    334 		int cz = round_page((vaddr_t)from) - (vaddr_t)from;
    335 
    336 		pa = (pte->pg_pfn << VAX_PGSHIFT) | (PAGE_SIZE - cz) | KERNBASE;
    337 		memcpy(to, (void *)pa, uimin(cz, len));
    338 		from += cz;
    339 		to += cz;
    340 		len -= cz;
    341 		pte += 8; /* XXX */
    342 	}
    343 	while (len > 0) {
    344 		pa = (pte->pg_pfn << VAX_PGSHIFT) | KERNBASE;
    345 		memcpy(to,  (void *)pa, uimin(PAGE_SIZE, len));
    346 		from += PAGE_SIZE;
    347 		to += PAGE_SIZE;
    348 		len -= PAGE_SIZE;
    349 		pte += 8; /* XXX */
    350 	}
    351 }
    352 
    353 /*
    354  * There can only be one user of the DMA area on VS2k/VS3100 at one
    355  * time, so keep track of it here.
    356  */
    357 static int vsbus_active = 0;
    358 
    359 void
    360 vsbus_dma_start(struct vsbus_dma *vd)
    361 {
    362 
    363 	SIMPLEQ_INSERT_TAIL(&vsbus_dma, vd, vd_q);
    364 
    365 	if (vsbus_active == 0)
    366 		vsbus_dma_intr();
    367 }
    368 
    369 void
    370 vsbus_dma_intr(void)
    371 {
    372 	struct vsbus_dma *vd;
    373 
    374 	vd = SIMPLEQ_FIRST(&vsbus_dma);
    375 	if (vd == NULL) {
    376 		vsbus_active = 0;
    377 		return;
    378 	}
    379 	vsbus_active = 1;
    380 	SIMPLEQ_REMOVE_HEAD(&vsbus_dma, vd_q);
    381 	(*vd->vd_go)(vd->vd_arg);
    382 }
    383 
    384