Home | History | Annotate | Line # | Download | only in tc
tc_dma.c revision 1.1.2.1
      1 /* $NetBSD: tc_dma.c,v 1.1.2.1 1997/06/03 23:34:23 thorpej Exp $ */
      2 
      3 /*-
      4  * Copyright (c) 1997 The NetBSD Foundation, Inc.
      5  * All rights reserved.
      6  *
      7  * This code is derived from software contributed to The NetBSD Foundation
      8  * by Jason R. Thorpe of the Numerical Aerospace Simulation Facility,
      9  * NASA Ames Research Center.
     10  *
     11  * Redistribution and use in source and binary forms, with or without
     12  * modification, are permitted provided that the following conditions
     13  * are met:
     14  * 1. Redistributions of source code must retain the above copyright
     15  *    notice, this list of conditions and the following disclaimer.
     16  * 2. Redistributions in binary form must reproduce the above copyright
     17  *    notice, this list of conditions and the following disclaimer in the
     18  *    documentation and/or other materials provided with the distribution.
     19  * 3. All advertising materials mentioning features or use of this software
     20  *    must display the following acknowledgement:
     21  *	This product includes software developed by the NetBSD
     22  *	Foundation, Inc. and its contributors.
     23  * 4. Neither the name of The NetBSD Foundation nor the names of its
     24  *    contributors may be used to endorse or promote products derived
     25  *    from this software without specific prior written permission.
     26  *
     27  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     28  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     29  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     30  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     31  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     32  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     33  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     34  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     35  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     36  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     37  * POSSIBILITY OF SUCH DAMAGE.
     38  */
     39 
     40 #include <machine/options.h>		/* Config options headers */
     41 #include <sys/cdefs.h>			/* RCS ID & Copyright macro defns */
     42 
     43 __KERNEL_RCSID(0, "$NetBSD: tc_dma.c,v 1.1.2.1 1997/06/03 23:34:23 thorpej Exp $");
     44 
     45 #include <sys/param.h>
     46 #include <sys/systm.h>
     47 #include <sys/kernel.h>
     48 #include <sys/device.h>
     49 #include <sys/malloc.h>
     50 #include <vm/vm.h>
     51 
     52 #define _ALPHA_BUS_DMA_PRIVATE
     53 #include <machine/bus.h>
     54 #include <machine/rpb.h>
     55 
     56 #include <dev/tc/tcreg.h>
     57 #include <dev/tc/tcvar.h>
     58 #include <alpha/tc/tc_sgmap.h>
     59 
     60 extern int cputype;
     61 
     62 bus_dma_tag_t tc_dma_get_tag __P((int));
     63 
     64 #ifdef DEC_3000_500
     65 int	tc_bus_dmamap_create_sgmap __P((bus_dma_tag_t, bus_size_t, int,
     66 	    bus_size_t, bus_size_t, int, bus_dmamap_t *));
     67 void	tc_bus_dmamap_destroy_sgmap __P((bus_dma_tag_t, bus_dmamap_t));
     68 int	tc_bus_dmamap_load_sgmap __P((bus_dma_tag_t, bus_dmamap_t, void *,
     69 	    bus_size_t, struct proc *, int));
     70 int	tc_bus_dmamap_load_mbuf_sgmap __P((bus_dma_tag_t, bus_dmamap_t,
     71 	    struct mbuf *, int));
     72 int	tc_bus_dmamap_load_uio_sgmap __P((bus_dma_tag_t, bus_dmamap_t,
     73 	    struct uio *, int));
     74 int	tc_bus_dmamap_load_raw_sgmap __P((bus_dma_tag_t, bus_dmamap_t,
     75 	    bus_dma_segment_t *, int, bus_size_t, int));
     76 void	tc_bus_dmamap_unload_sgmap __P((bus_dma_tag_t, bus_dmamap_t));
     77 #endif /* DEC_3000_500 */
     78 
     79 int	tc_bus_dmamap_load_direct __P((bus_dma_tag_t, bus_dmamap_t, void *,
     80 	    bus_size_t, struct proc *, int));
     81 int	tc_bus_dmamap_load_mbuf_direct __P((bus_dma_tag_t, bus_dmamap_t,
     82 	    struct mbuf *, int));
     83 int	tc_bus_dmamap_load_uio_direct __P((bus_dma_tag_t, bus_dmamap_t,
     84 	    struct uio *, int));
     85 int	tc_bus_dmamap_load_raw_direct __P((bus_dma_tag_t, bus_dmamap_t,
     86 	    bus_dma_segment_t *, int, bus_size_t, int));
     87 
     88 
     89 struct alpha_bus_dma_tag tc_dmat_direct = {
     90 	NULL,				/* _cookie */
     91 	NULL,				/* _get_tag */
     92 	_bus_dmamap_create,
     93 	_bus_dmamap_destroy,
     94 	tc_bus_dmamap_load_direct,
     95 	tc_bus_dmamap_load_mbuf_direct,
     96 	tc_bus_dmamap_load_uio_direct,
     97 	tc_bus_dmamap_load_raw_direct,
     98 	_bus_dmamap_unload,
     99 	NULL,				/* _dmamap_sync */
    100 	_bus_dmamem_alloc,
    101 	_bus_dmamem_free,
    102 	_bus_dmamem_map,
    103 	_bus_dmamem_unmap,
    104 	_bus_dmamem_mmap,
    105 };
    106 
    107 #ifdef DEC_3000_500
    108 struct alpha_bus_dma_tag tc_dmat_sgmap = {
    109 	NULL,				/* _cookie */
    110 	NULL,				/* _get_tag */
    111 	tc_bus_dmamap_create_sgmap,
    112 	tc_bus_dmamap_destroy_sgmap,
    113 	tc_bus_dmamap_load_sgmap,
    114 	tc_bus_dmamap_load_mbuf_sgmap,
    115 	tc_bus_dmamap_load_uio_sgmap,
    116 	tc_bus_dmamap_load_raw_sgmap,
    117 	tc_bus_dmamap_unload_sgmap,
    118 	NULL,				/* _dmamap_sync */
    119 	_bus_dmamem_alloc,
    120 	_bus_dmamem_free,
    121 	_bus_dmamem_map,
    122 	_bus_dmamem_unmap,
    123 	_bus_dmamem_mmap,
    124 };
    125 
    126 struct tc_dma_slot_info {
    127 	struct alpha_sgmap tdsi_sgmap;		/* sgmap for slot */
    128 	struct alpha_bus_dma_tag tdsi_dmat;	/* dma tag for slot */
    129 };
    130 struct tc_dma_slot_info *tc_dma_slot_info;
    131 #endif /* DEC_3000_500 */
    132 
    133 void
    134 tc_dma_init(nslots)
    135 	int nslots;
    136 {
    137 
    138 #ifdef DEC_3000_500
    139 	if (cputype == ST_DEC_3000_500) {
    140 		size_t sisize;
    141 		int i;
    142 
    143 		/* Allocate per-slot DMA info. */
    144 		sisize = nslots * sizeof(struct tc_dma_slot_info);
    145 		tc_dma_slot_info = malloc(sisize, M_DEVBUF, M_NOWAIT);
    146 		if (tc_dma_slot_info == NULL)
    147 			panic("tc_dma_init: can't allocate per-slot DMA info");
    148 		bzero(tc_dma_slot_info, sisize);
    149 
    150 		/* Default all slots to direct-mapped. */
    151 		for (i = 0; i < nslots; i++)
    152 			bcopy(&tc_dmat_direct, &tc_dma_slot_info[i].tdsi_dmat,
    153 			    sizeof(tc_dma_slot_info[i].tdsi_dmat));
    154 	}
    155 #endif /* DEC_3000_500 */
    156 
    157 	/* XXX XXX BEGIN XXX XXX */
    158 	{							/* XXX */
    159 		extern vm_offset_t alpha_XXX_dmamap_or;		/* XXX */
    160 		alpha_XXX_dmamap_or = 0;			/* XXX */
    161 	}							/* XXX */
    162 	/* XXX XXX END XXX XXX */
    163 }
    164 
    165 /*
    166  * Return the DMA tag for the given slot.
    167  */
    168 bus_dma_tag_t
    169 tc_dma_get_tag(slot)
    170 	int slot;
    171 {
    172 	bus_dma_tag_t t;
    173 
    174 	switch (cputype) {
    175 #ifdef DEC_3000_500
    176 	case ST_DEC_3000_500:
    177 		t = &tc_dma_slot_info[slot].tdsi_dmat;
    178 		break;
    179 #endif /* DEC_3000_500 */
    180 
    181 #ifdef DEC_3000_300
    182 	case ST_DEC_3000_300:
    183 		/* Pelican doesn't have SGMAPs. */
    184 		t = &tc_dmat_direct;
    185 		break;
    186 #endif /* ST_DEC_3000_300 */
    187 
    188 	default:
    189 		panic("tc_dma_get_tag: bad cputype");
    190 	}
    191 
    192 	return (t);
    193 }
    194 
    195 #ifdef DEC_3000_500
    196 /*
    197  * Create a TurboChannel SGMAP-mapped DMA map.
    198  */
    199 int
    200 tc_bus_dmamap_create_sgmap(t, size, nsegments, maxsegsz, boundary,
    201     flags, dmamp)
    202 	bus_dma_tag_t t;
    203 	bus_size_t size;
    204 	int nsegments;
    205 	bus_size_t maxsegsz;
    206 	bus_size_t boundary;
    207 	int flags;
    208 	bus_dmamap_t *dmamp;
    209 {
    210 	struct tc_dma_slot_info *tdsi = t->_cookie;
    211 	struct alpha_sgmap_cookie *a;
    212 	bus_dmamap_t map;
    213 	int error;
    214 
    215 	error = _bus_dmamap_create(t, size, nsegments, maxsegsz,
    216 	    boundary, flags, dmamp);
    217 	if (error)
    218 		return (error);
    219 
    220 	map = *dmamp;
    221 
    222 	a = malloc(sizeof(struct alpha_sgmap_cookie), M_DEVBUF,
    223 	    (flags & BUS_DMA_NOWAIT) ? M_NOWAIT : M_WAITOK);
    224 	if (a == NULL) {
    225 		_bus_dmamap_destroy(t, map);
    226 		return (ENOMEM);
    227 	}
    228 	bzero(a, sizeof(struct alpha_sgmap_cookie));
    229 	map->_dm_sgcookie = a;
    230 
    231 	if (flags & BUS_DMA_ALLOCNOW) {
    232 		error = alpha_sgmap_alloc(map, round_page(size),
    233 		    &tdsi->tdsi_sgmap, flags);
    234 		if (error)
    235 			tc_bus_dmamap_destroy_sgmap(t, map);
    236 	}
    237 
    238 	return (error);
    239 }
    240 
    241 /*
    242  * Destroy a TurboChannel SGMAP-mapped DMA map.
    243  */
    244 void
    245 tc_bus_dmamap_destroy_sgmap(t, map)
    246 	bus_dma_tag_t t;
    247 	bus_dmamap_t map;
    248 {
    249 	struct tc_dma_slot_info *tdsi = t->_cookie;
    250 	struct alpha_sgmap_cookie *a = map->_dm_sgcookie;
    251 
    252 	if (a->apdc_flags & APDC_HAS_SGMAP)
    253 		alpha_sgmap_free(&tdsi->tdsi_sgmap, a);
    254 
    255 	free(a, M_DEVBUF);
    256 	_bus_dmamap_destroy(t, map);
    257 }
    258 
    259 /*
    260  * Load a TurboChannel direct-mapped DMA map with a linear buffer.
    261  */
    262 int
    263 tc_bus_dmamap_load_direct(t, map, buf, buflen, p, flags)
    264 	bus_dma_tag_t t;
    265 	bus_dmamap_t map;
    266 	void *buf;
    267 	bus_size_t buflen;
    268 	struct proc *p;
    269 	int flags;
    270 {
    271 
    272 	return (_bus_dmamap_load_direct_common(t, map, buf, buflen, p,
    273 	    flags, 0));
    274 }
    275 
    276 /*
    277  * Load a TurboChannel SGMAP-mapped DMA map with a linear buffer.
    278  */
    279 int
    280 tc_bus_dmamap_load_sgmap(t, map, buf, buflen, p, flags)
    281 	bus_dma_tag_t t;
    282 	bus_dmamap_t map;
    283 	void *buf;
    284 	bus_size_t buflen;
    285 	struct proc *p;
    286 	int flags;
    287 {
    288 	struct tc_dma_slot_info *tdsi = t->_cookie;
    289 
    290 	return (tc_sgmap_load(t, map, buf, buflen, p, flags,
    291 	    &tdsi->tdsi_sgmap));
    292 }
    293 
    294 /*
    295  * Load a TurboChannel direct-mapped DMA map with an mbuf chain.
    296  */
    297 int
    298 tc_bus_dmamap_load_mbuf_direct(t, map, m, flags)
    299 	bus_dma_tag_t t;
    300 	bus_dmamap_t map;
    301 	struct mbuf *m;
    302 	int flags;
    303 {
    304 
    305 	return (_bus_dmamap_load_mbuf_direct_common(t, map, m,
    306 	    flags, 0));
    307 }
    308 
    309 /*
    310  * Load a TurboChannel SGMAP-mapped DMA map with an mbuf chain.
    311  */
    312 int
    313 tc_bus_dmamap_load_mbuf_sgmap(t, map, m, flags)
    314 	bus_dma_tag_t t;
    315 	bus_dmamap_t map;
    316 	struct mbuf *m;
    317 	int flags;
    318 {
    319 	struct tc_dma_slot_info *tdsi = t->_cookie;
    320 
    321 	return (tc_sgmap_load_mbuf(t, map, m, flags, &tdsi->tdsi_sgmap));
    322 }
    323 
    324 /*
    325  * Load a TurboChannel direct-mapped DMA map with a uio.
    326  */
    327 int
    328 tc_bus_dmamap_load_uio_direct(t, map, uio, flags)
    329 	bus_dma_tag_t t;
    330 	bus_dmamap_t map;
    331 	struct uio *uio;
    332 	int flags;
    333 {
    334 
    335 	return (_bus_dmamap_load_uio_direct_common(t, map, uio,
    336 	    flags, 0));
    337 }
    338 
    339 /*
    340  * Load a TurboChannel SGMAP-mapped DMA map with a uio.
    341  */
    342 int
    343 tc_bus_dmamap_load_uio_sgmap(t, map, uio, flags)
    344 	bus_dma_tag_t t;
    345 	bus_dmamap_t map;
    346 	struct uio *uio;
    347 	int flags;
    348 {
    349 	struct tc_dma_slot_info *tdsi = t->_cookie;
    350 
    351 	return (tc_sgmap_load_uio(t, map, uio, flags, &tdsi->tdsi_sgmap));
    352 }
    353 
    354 /*
    355  * Load a TurboChannel direct-mapped DMA map with raw memory.
    356  */
    357 int
    358 tc_bus_dmamap_load_raw_direct(t, map, segs, nsegs, size, flags)
    359 	bus_dma_tag_t t;
    360 	bus_dmamap_t map;
    361 	bus_dma_segment_t *segs;
    362 	int nsegs;
    363 	bus_size_t size;
    364 	int flags;
    365 {
    366 
    367 	return (_bus_dmamap_load_raw_direct_common(t, map, segs, nsegs,
    368 	    size, flags, 0));
    369 }
    370 
    371 /*
    372  * Load a TurboChannel SGMAP-mapped DMA map with raw memory.
    373  */
    374 int
    375 tc_bus_dmamap_load_raw_sgmap(t, map, segs, nsegs, size, flags)
    376 	bus_dma_tag_t t;
    377 	bus_dmamap_t map;
    378 	bus_dma_segment_t *segs;
    379 	int nsegs;
    380 	bus_size_t size;
    381 	int flags;
    382 {
    383 	struct tc_dma_slot_info *tdsi = t->_cookie;
    384 
    385 	return (tc_sgmap_load_raw(t, map, segs, nsegs, size, flags,
    386 	    &tdsi->tdsi_sgmap));
    387 }
    388 
    389 /*
    390  * Unload a TurboChannel DMA map.
    391  */
    392 void
    393 tc_bus_dmamap_unload_sgmap(t, map)
    394 	bus_dma_tag_t t;
    395 	bus_dmamap_t map;
    396 {
    397 	struct tc_dma_slot_info *tdsi = t->_cookie;
    398 
    399 	/*
    400 	 * Invalidate any SGMAP page table entries used by this
    401 	 * mapping.
    402 	 */
    403 	tc_sgmap_unload(t, map, &tdsi->tdsi_sgmap);
    404 
    405 	/*
    406 	 * Do the generic bits of the unload.
    407 	 */
    408 	_bus_dmamap_unload(t, map);
    409 }
    410 #endif /* DEC_3000_500 */
    411