Home | History | Annotate | Line # | Download | only in jazz
bus_dma_jazz.c revision 1.2
      1 /*	$NetBSD: bus_dma_jazz.c,v 1.2 2000/06/26 14:20:32 mrg Exp $	*/
      2 
      3 /*-
      4  * Copyright (C) 2000 Shuichiro URATA.  All rights reserved.
      5  *
      6  * Redistribution and use in source and binary forms, with or without
      7  * modification, are permitted provided that the following conditions
      8  * are met:
      9  * 1. Redistributions of source code must retain the above copyright
     10  *    notice, this list of conditions and the following disclaimer.
     11  * 2. Redistributions in binary form must reproduce the above copyright
     12  *    notice, this list of conditions and the following disclaimer in the
     13  *    documentation and/or other materials provided with the distribution.
     14  * 3. The name of the author may not be used to endorse or promote products
     15  *    derived from this software without specific prior written permission.
     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
     26  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     27  */
     28 
     29 #include <sys/param.h>
     30 #include <sys/systm.h>
     31 #include <sys/mbuf.h>
     32 #include <sys/device.h>
     33 
     34 #include <vm/vm.h>
     35 
     36 #include <uvm/uvm_extern.h>
     37 
     38 #define _ARC_BUS_DMA_PRIVATE
     39 #include <machine/bus.h>
     40 
     41 #include <arc/jazz/jazzdmatlbreg.h>
     42 #include <arc/jazz/jazzdmatlbvar.h>
     43 
     44 static int	jazz_bus_dmamap_alloc_sgmap __P((bus_dma_tag_t,
     45 		    bus_dma_segment_t *, int, bus_size_t, struct proc *, int));
     46 static void	jazz_bus_dmamap_free_sgmap __P((bus_dma_tag_t,
     47 		    bus_dma_segment_t *, int));
     48 
     49 int	jazz_bus_dmamap_load __P((bus_dma_tag_t, bus_dmamap_t, void *,
     50 	    bus_size_t, struct proc *, int));
     51 int	jazz_bus_dmamap_load_mbuf __P((bus_dma_tag_t, bus_dmamap_t,
     52 	    struct mbuf *, int));
     53 int	jazz_bus_dmamap_load_uio __P((bus_dma_tag_t, bus_dmamap_t,
     54 	    struct uio *, int));
     55 int	jazz_bus_dmamap_load_raw __P((bus_dma_tag_t, bus_dmamap_t,
     56 	    bus_dma_segment_t *, int, bus_size_t, int));
     57 void	jazz_bus_dmamap_unload __P((bus_dma_tag_t, bus_dmamap_t));
     58 void	jazz_mips1_bus_dmamap_sync __P((bus_dma_tag_t, bus_dmamap_t,
     59 	    bus_addr_t, bus_size_t, int));
     60 void	jazz_mips3_bus_dmamap_sync __P((bus_dma_tag_t, bus_dmamap_t,
     61 	    bus_addr_t, bus_size_t, int));
     62 
     63 void
     64 jazz_bus_dma_tag_init(t)
     65 	bus_dma_tag_t t;
     66 {
     67 	_bus_dma_tag_init(t);
     68 
     69 	t->_dmamap_load = jazz_bus_dmamap_load;
     70 	t->_dmamap_load_mbuf = jazz_bus_dmamap_load_mbuf;
     71 	t->_dmamap_load_uio = jazz_bus_dmamap_load_uio;
     72 	t->_dmamap_load_raw = jazz_bus_dmamap_load_raw;
     73 	t->_dmamap_unload = jazz_bus_dmamap_unload;
     74 #if defined(MIPS1) && defined(MIPS3)
     75 	t->_dmamap_sync = (CPUISMIPS3) ?
     76 	    jazz_mips3_bus_dmamap_sync : jazz_mips1_bus_dmamap_sync;
     77 #elif defined(MIPS1)
     78 	t->_dmamap_sync = jazz_mips1_bus_dmamap_sync;
     79 #elif defined(MIPS3)
     80 	t->_dmamap_sync = jazz_mips3_bus_dmamap_sync;
     81 #else
     82 #error neither MIPS1 nor MIPS3 is defined
     83 #endif
     84 	t->_dmamem_alloc = _bus_dmamem_alloc;
     85 	t->_dmamem_free = _bus_dmamem_free;
     86 }
     87 
     88 static int
     89 jazz_bus_dmamap_alloc_sgmap(t, segs, nsegs, boundary, p, flags)
     90 	bus_dma_tag_t t;
     91 	bus_dma_segment_t *segs;
     92 	int nsegs;
     93 	bus_size_t boundary;
     94 	struct proc *p;
     95 	int flags;
     96 {
     97 	jazz_dma_pte_t *dmapte;
     98 	bus_addr_t addr;
     99 	bus_size_t off;
    100 	int i, npte;
    101 
    102 	for (i = 0; i < nsegs; i++) {
    103 		off = jazz_dma_page_offs(segs[i]._ds_paddr);
    104 		npte = jazz_dma_page_round(segs[i].ds_len + off) /
    105 		    JAZZ_DMA_PAGE_SIZE;
    106 		dmapte = jazz_dmatlb_alloc(npte, boundary, flags, &addr);
    107 		if (dmapte == NULL)
    108 			return (ENOMEM);
    109 		segs[i].ds_addr = addr + off;
    110 
    111 		jazz_dmatlb_map_pa(segs[i]._ds_paddr, segs[i].ds_len, dmapte);
    112 	}
    113 	return (0);
    114 }
    115 
    116 static void
    117 jazz_bus_dmamap_free_sgmap(t, segs, nsegs)
    118 	bus_dma_tag_t t;
    119 	bus_dma_segment_t *segs;
    120 	int nsegs;
    121 {
    122 	int i, npte;
    123 	bus_addr_t addr;
    124 
    125 	for (i = 0; i < nsegs; i++) {
    126 		addr = (segs[i].ds_addr - t->dma_offset) & JAZZ_DMA_PAGE_NUM;
    127 		npte =  jazz_dma_page_round(segs[i].ds_len +
    128 		    jazz_dma_page_offs(segs[i].ds_addr)) / JAZZ_DMA_PAGE_SIZE;
    129 		jazz_dmatlb_free(addr, npte);
    130 	}
    131 }
    132 
    133 /*
    134  * function for loading a direct-mapped DMA map with a linear buffer.
    135  */
    136 int
    137 jazz_bus_dmamap_load(t, map, buf, buflen, p, flags)
    138 	bus_dma_tag_t t;
    139 	bus_dmamap_t map;
    140 	void *buf;
    141 	bus_size_t buflen;
    142 	struct proc *p;
    143 	int flags;
    144 {
    145 	int error = _bus_dmamap_load(t, map, buf, buflen, p, flags);
    146 
    147 	if (error == 0) {
    148 		error = jazz_bus_dmamap_alloc_sgmap(t, map->dm_segs,
    149 		    map->dm_nsegs, map->_dm_boundary, p, flags);
    150 	}
    151 	return (error);
    152 }
    153 
    154 /*
    155  * Like jazz_bus_dmamap_load(), but for mbufs.
    156  */
    157 int
    158 jazz_bus_dmamap_load_mbuf(t, map, m0, flags)
    159 	bus_dma_tag_t t;
    160 	bus_dmamap_t map;
    161 	struct mbuf *m0;
    162 	int flags;
    163 {
    164 	int error = _bus_dmamap_load_mbuf(t, map, m0, flags);
    165 
    166 	if (error == 0) {
    167 		error = jazz_bus_dmamap_alloc_sgmap(t, map->dm_segs,
    168 		    map->dm_nsegs, map->_dm_boundary, NULL, flags);
    169 	}
    170 	return (error);
    171 }
    172 
    173 /*
    174  * Like jazz_bus_dmamap_load(), but for uios.
    175  */
    176 int
    177 jazz_bus_dmamap_load_uio(t, map, uio, flags)
    178 	bus_dma_tag_t t;
    179 	bus_dmamap_t map;
    180 	struct uio *uio;
    181 	int flags;
    182 {
    183 	int error = jazz_bus_dmamap_load_uio(t, map, uio, flags);
    184 
    185 	if (error == 0) {
    186 		error = jazz_bus_dmamap_alloc_sgmap(t, map->dm_segs,
    187 		    map->dm_nsegs, map->_dm_boundary,
    188 		    uio->uio_segflg == UIO_USERSPACE ? uio->uio_procp : NULL,
    189 		    flags);
    190 	}
    191 	return (error);
    192 }
    193 
    194 /*
    195  * Like _bus_dmamap_load(), but for raw memory.
    196  */
    197 int
    198 jazz_bus_dmamap_load_raw(t, map, segs, nsegs, size, flags)
    199 	bus_dma_tag_t t;
    200 	bus_dmamap_t map;
    201 	bus_dma_segment_t *segs;
    202 	int nsegs;
    203 	bus_size_t size;
    204 	int flags;
    205 {
    206 	int error = _bus_dmamap_load_raw(t, map, segs, nsegs, size, flags);
    207 
    208 	if (error == 0) {
    209 		error = jazz_bus_dmamap_alloc_sgmap(t, map->dm_segs,
    210 		    map->dm_nsegs, map->_dm_boundary, NULL, flags);
    211 	}
    212 	return (error);
    213 }
    214 
    215 /*
    216  * unload a DMA map.
    217  */
    218 void
    219 jazz_bus_dmamap_unload(t, map)
    220 	bus_dma_tag_t t;
    221 	bus_dmamap_t map;
    222 {
    223 	jazz_bus_dmamap_free_sgmap(t, map->dm_segs, map->dm_nsegs);
    224 	_bus_dmamap_unload(t, map);
    225 }
    226 
    227 #ifdef MIPS1
    228 /*
    229  * Function for MIPS1 DMA map synchronization.
    230  */
    231 void
    232 jazz_mips1_bus_dmamap_sync(t, map, offset, len, ops)
    233 	bus_dma_tag_t t;
    234 	bus_dmamap_t map;
    235 	bus_addr_t offset;
    236 	bus_size_t len;
    237 	int ops;
    238 {
    239 	/* Flush DMA TLB */
    240 	if ((ops & (BUS_DMASYNC_PREREAD|BUS_DMASYNC_PREWRITE)) != 0)
    241 		jazz_dmatlb_flush();
    242 
    243 	return (_mips1_bus_dmamap_sync(t, map, offset, len, ops));
    244 }
    245 #endif /* MIPS1 */
    246 
    247 #ifdef MIPS3
    248 /*
    249  * Function for MIPS3 DMA map synchronization.
    250  */
    251 void
    252 jazz_mips3_bus_dmamap_sync(t, map, offset, len, ops)
    253 	bus_dma_tag_t t;
    254 	bus_dmamap_t map;
    255 	bus_addr_t offset;
    256 	bus_size_t len;
    257 	int ops;
    258 {
    259 	/* Flush DMA TLB */
    260 	if ((ops & (BUS_DMASYNC_PREREAD|BUS_DMASYNC_PREWRITE)) != 0)
    261 		jazz_dmatlb_flush();
    262 
    263 	return (_mips3_bus_dmamap_sync(t, map, offset, len, ops));
    264 }
    265 #endif /* MIPS3 */
    266