Home | History | Annotate | Line # | Download | only in vsa
vsbus_dma.c revision 1.8.2.2
      1  1.8.2.2  bouyer /* $NetBSD: vsbus_dma.c,v 1.8.2.2 2000/11/20 20:33:40 bouyer Exp $ */
      2  1.8.2.2  bouyer 
      3  1.8.2.2  bouyer /*-
      4  1.8.2.2  bouyer  * Copyright (c) 1997, 1998 The NetBSD Foundation, Inc.
      5  1.8.2.2  bouyer  * All rights reserved.
      6  1.8.2.2  bouyer  *
      7  1.8.2.2  bouyer  * This code is derived from software contributed to The NetBSD Foundation
      8  1.8.2.2  bouyer  * by Jason R. Thorpe of the Numerical Aerospace Simulation Facility,
      9  1.8.2.2  bouyer  * NASA Ames Research Center.
     10  1.8.2.2  bouyer  *
     11  1.8.2.2  bouyer  * Redistribution and use in source and binary forms, with or without
     12  1.8.2.2  bouyer  * modification, are permitted provided that the following conditions
     13  1.8.2.2  bouyer  * are met:
     14  1.8.2.2  bouyer  * 1. Redistributions of source code must retain the above copyright
     15  1.8.2.2  bouyer  *    notice, this list of conditions and the following disclaimer.
     16  1.8.2.2  bouyer  * 2. Redistributions in binary form must reproduce the above copyright
     17  1.8.2.2  bouyer  *    notice, this list of conditions and the following disclaimer in the
     18  1.8.2.2  bouyer  *    documentation and/or other materials provided with the distribution.
     19  1.8.2.2  bouyer  * 3. All advertising materials mentioning features or use of this software
     20  1.8.2.2  bouyer  *    must display the following acknowledgement:
     21  1.8.2.2  bouyer  *	This product includes software developed by the NetBSD
     22  1.8.2.2  bouyer  *	Foundation, Inc. and its contributors.
     23  1.8.2.2  bouyer  * 4. Neither the name of The NetBSD Foundation nor the names of its
     24  1.8.2.2  bouyer  *    contributors may be used to endorse or promote products derived
     25  1.8.2.2  bouyer  *    from this software without specific prior written permission.
     26  1.8.2.2  bouyer  *
     27  1.8.2.2  bouyer  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     28  1.8.2.2  bouyer  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     29  1.8.2.2  bouyer  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     30  1.8.2.2  bouyer  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     31  1.8.2.2  bouyer  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     32  1.8.2.2  bouyer  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     33  1.8.2.2  bouyer  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     34  1.8.2.2  bouyer  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     35  1.8.2.2  bouyer  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     36  1.8.2.2  bouyer  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     37  1.8.2.2  bouyer  * POSSIBILITY OF SUCH DAMAGE.
     38  1.8.2.2  bouyer  */
     39  1.8.2.2  bouyer 
     40  1.8.2.2  bouyer 
     41  1.8.2.2  bouyer #include <sys/param.h>
     42  1.8.2.2  bouyer #include <sys/systm.h>
     43  1.8.2.2  bouyer #include <sys/kernel.h>
     44  1.8.2.2  bouyer #include <sys/device.h>
     45  1.8.2.2  bouyer #include <sys/malloc.h>
     46  1.8.2.2  bouyer #include <uvm/uvm_extern.h>
     47  1.8.2.2  bouyer 
     48  1.8.2.2  bouyer #define _VAX_BUS_DMA_PRIVATE
     49  1.8.2.2  bouyer #include <machine/bus.h>
     50  1.8.2.2  bouyer #include <machine/cpu.h>
     51  1.8.2.2  bouyer #include <machine/sid.h>
     52  1.8.2.2  bouyer #include <machine/sgmap.h>
     53  1.8.2.2  bouyer #include <machine/vsbus.h>
     54  1.8.2.2  bouyer 
     55  1.8.2.2  bouyer static int vsbus_bus_dmamap_create_sgmap(bus_dma_tag_t, bus_size_t, int,
     56  1.8.2.2  bouyer 	    bus_size_t, bus_size_t, int, bus_dmamap_t *);
     57  1.8.2.2  bouyer 
     58  1.8.2.2  bouyer static void vsbus_bus_dmamap_destroy_sgmap(bus_dma_tag_t, bus_dmamap_t);
     59  1.8.2.2  bouyer 
     60  1.8.2.2  bouyer static int vsbus_bus_dmamap_load_sgmap(bus_dma_tag_t, bus_dmamap_t, void *,
     61  1.8.2.2  bouyer 	    bus_size_t, struct proc *, int);
     62  1.8.2.2  bouyer 
     63  1.8.2.2  bouyer static int vsbus_bus_dmamap_load_mbuf_sgmap(bus_dma_tag_t, bus_dmamap_t,
     64  1.8.2.2  bouyer 	    struct mbuf *, int);
     65  1.8.2.2  bouyer 
     66  1.8.2.2  bouyer static int vsbus_bus_dmamap_load_uio_sgmap(bus_dma_tag_t, bus_dmamap_t,
     67  1.8.2.2  bouyer 	    struct uio *, int);
     68  1.8.2.2  bouyer 
     69  1.8.2.2  bouyer static int vsbus_bus_dmamap_load_raw_sgmap(bus_dma_tag_t, bus_dmamap_t,
     70  1.8.2.2  bouyer 	    bus_dma_segment_t *, int, bus_size_t, int);
     71  1.8.2.2  bouyer 
     72  1.8.2.2  bouyer static void vsbus_bus_dmamap_unload_sgmap(bus_dma_tag_t, bus_dmamap_t);
     73  1.8.2.2  bouyer 
     74  1.8.2.2  bouyer static void vsbus_bus_dmamap_sync(bus_dma_tag_t, bus_dmamap_t, bus_addr_t,
     75  1.8.2.2  bouyer 	    bus_size_t, int);
     76  1.8.2.2  bouyer 
     77  1.8.2.2  bouyer void
     78  1.8.2.2  bouyer vsbus_dma_init(sc, ptecnt)
     79  1.8.2.2  bouyer 	struct vsbus_softc *sc;
     80  1.8.2.2  bouyer 	unsigned ptecnt;
     81  1.8.2.2  bouyer {
     82  1.8.2.2  bouyer 	bus_dma_tag_t t;
     83  1.8.2.2  bouyer 	bus_dma_segment_t segs[1];
     84  1.8.2.2  bouyer 	struct pte *pte;
     85  1.8.2.2  bouyer 	int nsegs, error;
     86  1.8.2.2  bouyer 	unsigned mapsize = ptecnt * sizeof(struct pte);
     87  1.8.2.2  bouyer 
     88  1.8.2.2  bouyer 	/*
     89  1.8.2.2  bouyer 	 * Initialize the DMA tag used for sgmap-mapped DMA.
     90  1.8.2.2  bouyer 	 */
     91  1.8.2.2  bouyer 	t = &sc->sc_dmatag;
     92  1.8.2.2  bouyer 	t->_cookie = sc;
     93  1.8.2.2  bouyer 	t->_wbase = 0;
     94  1.8.2.2  bouyer 	t->_wsize = ptecnt * VAX_NBPG;
     95  1.8.2.2  bouyer 	t->_boundary = 0;
     96  1.8.2.2  bouyer 	t->_sgmap = &sc->sc_sgmap;
     97  1.8.2.2  bouyer 	t->_dmamap_create = vsbus_bus_dmamap_create_sgmap;
     98  1.8.2.2  bouyer 	t->_dmamap_destroy = vsbus_bus_dmamap_destroy_sgmap;
     99  1.8.2.2  bouyer 	t->_dmamap_load = vsbus_bus_dmamap_load_sgmap;
    100  1.8.2.2  bouyer 	t->_dmamap_load_mbuf = vsbus_bus_dmamap_load_mbuf_sgmap;
    101  1.8.2.2  bouyer 	t->_dmamap_load_uio = vsbus_bus_dmamap_load_uio_sgmap;
    102  1.8.2.2  bouyer 	t->_dmamap_load_raw = vsbus_bus_dmamap_load_raw_sgmap;
    103  1.8.2.2  bouyer 	t->_dmamap_unload = vsbus_bus_dmamap_unload_sgmap;
    104  1.8.2.2  bouyer 	t->_dmamap_sync = vsbus_bus_dmamap_sync;
    105  1.8.2.2  bouyer 
    106  1.8.2.2  bouyer 	t->_dmamem_alloc = _bus_dmamem_alloc;
    107  1.8.2.2  bouyer 	t->_dmamem_free = _bus_dmamem_free;
    108  1.8.2.2  bouyer 	t->_dmamem_map = _bus_dmamem_map;
    109  1.8.2.2  bouyer 	t->_dmamem_unmap = _bus_dmamem_unmap;
    110  1.8.2.2  bouyer 	t->_dmamem_mmap = _bus_dmamem_mmap;
    111  1.8.2.2  bouyer 
    112  1.8.2.2  bouyer 	if (vax_boardtype == VAX_BTYP_46 || vax_boardtype == VAX_BTYP_48) {
    113  1.8.2.2  bouyer 		/*
    114  1.8.2.2  bouyer 		 * Allocate and map the VS4000 scatter gather map.
    115  1.8.2.2  bouyer 		 */
    116  1.8.2.2  bouyer 		error = bus_dmamem_alloc(t, mapsize, mapsize, mapsize,
    117  1.8.2.2  bouyer 		    segs, 1, &nsegs, BUS_DMA_NOWAIT);
    118  1.8.2.2  bouyer 		if (error) {
    119  1.8.2.2  bouyer 			panic("vsbus_dma_init: error allocating memory for "
    120  1.8.2.2  bouyer 			    "hw sgmap: error=%d", error);
    121  1.8.2.2  bouyer 		}
    122  1.8.2.2  bouyer 
    123  1.8.2.2  bouyer 		error = bus_dmamem_map(t, segs, nsegs, mapsize,
    124  1.8.2.2  bouyer 		   (caddr_t *) &pte, BUS_DMA_NOWAIT|BUS_DMA_COHERENT);
    125  1.8.2.2  bouyer 		if (error) {
    126  1.8.2.2  bouyer 			panic("vsbus_dma_init: error mapping memory for "
    127  1.8.2.2  bouyer 			    "hw sgmap: error=%d", error);
    128  1.8.2.2  bouyer 		}
    129  1.8.2.2  bouyer 		memset(pte, 0, mapsize);
    130  1.8.2.2  bouyer 		*(int *) (sc->sc_vsregs + 8) = segs->ds_addr;	/* set MAP BASE 0x2008008 */
    131  1.8.2.2  bouyer 	} else {
    132  1.8.2.2  bouyer 		sc->sc_sgmap.aps_flags |= SGMAP_KA49;
    133  1.8.2.2  bouyer 		pte = (struct pte *) vax_map_physmem(KA49_SCSIMAP, mapsize / VAX_NBPG);
    134  1.8.2.2  bouyer 		for (nsegs = ptecnt; nsegs > 0; ) {
    135  1.8.2.2  bouyer 			((u_int32_t *) pte)[--nsegs] = 0x88000000;
    136  1.8.2.2  bouyer 		}
    137  1.8.2.2  bouyer 		segs->ds_addr = KA49_SCSIMAP;
    138  1.8.2.2  bouyer 	}
    139  1.8.2.2  bouyer 	printf("%s: %uK entry DMA SGMAP at PA 0x%lx (VA %p)\n",
    140  1.8.2.2  bouyer 		sc->sc_dev.dv_xname, ptecnt / 1024, segs->ds_addr, pte);
    141  1.8.2.2  bouyer 
    142  1.8.2.2  bouyer 	/*
    143  1.8.2.2  bouyer 	 * Initialize the SGMAP.
    144  1.8.2.2  bouyer 	 */
    145  1.8.2.2  bouyer 	vax_sgmap_init(t, &sc->sc_sgmap, "vsbus_sgmap", t->_wbase, t->_wsize, pte, 0);
    146  1.8.2.2  bouyer 
    147  1.8.2.2  bouyer }
    148  1.8.2.2  bouyer 
    149  1.8.2.2  bouyer /*
    150  1.8.2.2  bouyer  * Create a VSBUS SGMAP-mapped DMA map.
    151  1.8.2.2  bouyer  */
    152  1.8.2.2  bouyer int
    153  1.8.2.2  bouyer vsbus_bus_dmamap_create_sgmap(t, size, nsegments, maxsegsz, boundary,
    154  1.8.2.2  bouyer     flags, dmamp)
    155  1.8.2.2  bouyer 	bus_dma_tag_t t;
    156  1.8.2.2  bouyer 	bus_size_t size;
    157  1.8.2.2  bouyer 	int nsegments;
    158  1.8.2.2  bouyer 	bus_size_t maxsegsz;
    159  1.8.2.2  bouyer 	bus_size_t boundary;
    160  1.8.2.2  bouyer 	int flags;
    161  1.8.2.2  bouyer 	bus_dmamap_t *dmamp;
    162  1.8.2.2  bouyer {
    163  1.8.2.2  bouyer 	bus_dmamap_t map;
    164  1.8.2.2  bouyer 	int error;
    165  1.8.2.2  bouyer 
    166  1.8.2.2  bouyer 	error = _bus_dmamap_create(t, size, nsegments, maxsegsz,
    167  1.8.2.2  bouyer 	    boundary, flags, dmamp);
    168  1.8.2.2  bouyer 	if (error)
    169  1.8.2.2  bouyer 		return (error);
    170  1.8.2.2  bouyer 
    171  1.8.2.2  bouyer 	map = *dmamp;
    172  1.8.2.2  bouyer 
    173  1.8.2.2  bouyer 	if (flags & BUS_DMA_ALLOCNOW) {
    174  1.8.2.2  bouyer 		error = vax_sgmap_alloc(map, vax_round_page(size),
    175  1.8.2.2  bouyer 		    t->_sgmap, flags);
    176  1.8.2.2  bouyer 		if (error)
    177  1.8.2.2  bouyer 			vsbus_bus_dmamap_destroy_sgmap(t, map);
    178  1.8.2.2  bouyer 	}
    179  1.8.2.2  bouyer 
    180  1.8.2.2  bouyer 	return (error);
    181  1.8.2.2  bouyer }
    182  1.8.2.2  bouyer 
    183  1.8.2.2  bouyer /*
    184  1.8.2.2  bouyer  * Destroy a VSBUS SGMAP-mapped DMA map.
    185  1.8.2.2  bouyer  */
    186  1.8.2.2  bouyer static void
    187  1.8.2.2  bouyer vsbus_bus_dmamap_destroy_sgmap(t, map)
    188  1.8.2.2  bouyer 	bus_dma_tag_t t;
    189  1.8.2.2  bouyer 	bus_dmamap_t map;
    190  1.8.2.2  bouyer {
    191  1.8.2.2  bouyer 
    192  1.8.2.2  bouyer 	if (map->_dm_flags & DMAMAP_HAS_SGMAP)
    193  1.8.2.2  bouyer 		vax_sgmap_free(map, t->_sgmap);
    194  1.8.2.2  bouyer 
    195  1.8.2.2  bouyer 	_bus_dmamap_destroy(t, map);
    196  1.8.2.2  bouyer }
    197  1.8.2.2  bouyer 
    198  1.8.2.2  bouyer /*
    199  1.8.2.2  bouyer  * Load a VSBUS SGMAP-mapped DMA map with a linear buffer.
    200  1.8.2.2  bouyer  */
    201  1.8.2.2  bouyer static int
    202  1.8.2.2  bouyer vsbus_bus_dmamap_load_sgmap(t, map, buf, buflen, p, flags)
    203  1.8.2.2  bouyer 	bus_dma_tag_t t;
    204  1.8.2.2  bouyer 	bus_dmamap_t map;
    205  1.8.2.2  bouyer 	void *buf;
    206  1.8.2.2  bouyer 	bus_size_t buflen;
    207  1.8.2.2  bouyer 	struct proc *p;
    208  1.8.2.2  bouyer 	int flags;
    209  1.8.2.2  bouyer {
    210  1.8.2.2  bouyer 	return vax_sgmap_load(t, map, buf, buflen, p, flags, t->_sgmap);
    211  1.8.2.2  bouyer }
    212  1.8.2.2  bouyer 
    213  1.8.2.2  bouyer /*
    214  1.8.2.2  bouyer  * Load a VSBUS SGMAP-mapped DMA map with an mbuf chain.
    215  1.8.2.2  bouyer  */
    216  1.8.2.2  bouyer static int
    217  1.8.2.2  bouyer vsbus_bus_dmamap_load_mbuf_sgmap(t, map, m, flags)
    218  1.8.2.2  bouyer 	bus_dma_tag_t t;
    219  1.8.2.2  bouyer 	bus_dmamap_t map;
    220  1.8.2.2  bouyer 	struct mbuf *m;
    221  1.8.2.2  bouyer 	int flags;
    222  1.8.2.2  bouyer {
    223  1.8.2.2  bouyer 	return vax_sgmap_load_mbuf(t, map, m, flags, t->_sgmap);
    224  1.8.2.2  bouyer }
    225  1.8.2.2  bouyer 
    226  1.8.2.2  bouyer /*
    227  1.8.2.2  bouyer  * Load a VSBUS SGMAP-mapped DMA map with a uio.
    228  1.8.2.2  bouyer  */
    229  1.8.2.2  bouyer static int
    230  1.8.2.2  bouyer vsbus_bus_dmamap_load_uio_sgmap(t, map, uio, flags)
    231  1.8.2.2  bouyer 	bus_dma_tag_t t;
    232  1.8.2.2  bouyer 	bus_dmamap_t map;
    233  1.8.2.2  bouyer 	struct uio *uio;
    234  1.8.2.2  bouyer 	int flags;
    235  1.8.2.2  bouyer {
    236  1.8.2.2  bouyer 	return vax_sgmap_load_uio(t, map, uio, flags, t->_sgmap);
    237  1.8.2.2  bouyer }
    238  1.8.2.2  bouyer 
    239  1.8.2.2  bouyer /*
    240  1.8.2.2  bouyer  * Load a VSBUS SGMAP-mapped DMA map with raw memory.
    241  1.8.2.2  bouyer  */
    242  1.8.2.2  bouyer static int
    243  1.8.2.2  bouyer vsbus_bus_dmamap_load_raw_sgmap(t, map, segs, nsegs, size, flags)
    244  1.8.2.2  bouyer 	bus_dma_tag_t t;
    245  1.8.2.2  bouyer 	bus_dmamap_t map;
    246  1.8.2.2  bouyer 	bus_dma_segment_t *segs;
    247  1.8.2.2  bouyer 	int nsegs;
    248  1.8.2.2  bouyer 	bus_size_t size;
    249  1.8.2.2  bouyer 	int flags;
    250  1.8.2.2  bouyer {
    251  1.8.2.2  bouyer 	return vax_sgmap_load_raw(t, map, segs, nsegs, size, flags, t->_sgmap);
    252  1.8.2.2  bouyer }
    253  1.8.2.2  bouyer 
    254  1.8.2.2  bouyer /*
    255  1.8.2.2  bouyer  * Unload a VSBUS DMA map.
    256  1.8.2.2  bouyer  */
    257  1.8.2.2  bouyer static void
    258  1.8.2.2  bouyer vsbus_bus_dmamap_unload_sgmap(t, map)
    259  1.8.2.2  bouyer 	bus_dma_tag_t t;
    260  1.8.2.2  bouyer 	bus_dmamap_t map;
    261  1.8.2.2  bouyer {
    262  1.8.2.2  bouyer 	/*
    263  1.8.2.2  bouyer 	 * Invalidate any SGMAP page table entries used by this
    264  1.8.2.2  bouyer 	 * mapping.
    265  1.8.2.2  bouyer 	 */
    266  1.8.2.2  bouyer 	vax_sgmap_unload(t, map, t->_sgmap);
    267  1.8.2.2  bouyer 
    268  1.8.2.2  bouyer 	/*
    269  1.8.2.2  bouyer 	 * Do the generic bits of the unload.
    270  1.8.2.2  bouyer 	 */
    271  1.8.2.2  bouyer 	_bus_dmamap_unload(t, map);
    272  1.8.2.2  bouyer }
    273  1.8.2.2  bouyer 
    274  1.8.2.2  bouyer /*
    275  1.8.2.2  bouyer  * Sync the bus map.
    276  1.8.2.2  bouyer  */
    277  1.8.2.2  bouyer static void
    278  1.8.2.2  bouyer vsbus_bus_dmamap_sync(tag, dmam, offset, len, ops)
    279  1.8.2.2  bouyer 	bus_dma_tag_t tag;
    280  1.8.2.2  bouyer 	bus_dmamap_t dmam;
    281  1.8.2.2  bouyer 	bus_addr_t offset;
    282  1.8.2.2  bouyer 	bus_size_t len;
    283  1.8.2.2  bouyer 	int ops;
    284  1.8.2.2  bouyer {
    285  1.8.2.2  bouyer 	/* not needed */
    286  1.8.2.2  bouyer }
    287