1 1.6 kiyohara /* $NetBSD: fwdma.h,v 1.6 2010/03/29 03:05:27 kiyohara Exp $ */ 2 1.1 kiyohara /*- 3 1.1 kiyohara * Copyright (C) 2003 4 1.1 kiyohara * Hidetoshi Shimokawa. All rights reserved. 5 1.6 kiyohara * 6 1.1 kiyohara * Redistribution and use in source and binary forms, with or without 7 1.1 kiyohara * modification, are permitted provided that the following conditions 8 1.1 kiyohara * are met: 9 1.1 kiyohara * 1. Redistributions of source code must retain the above copyright 10 1.1 kiyohara * notice, this list of conditions and the following disclaimer. 11 1.1 kiyohara * 2. Redistributions in binary form must reproduce the above copyright 12 1.1 kiyohara * notice, this list of conditions and the following disclaimer in the 13 1.1 kiyohara * documentation and/or other materials provided with the distribution. 14 1.1 kiyohara * 3. All advertising materials mentioning features or use of this software 15 1.1 kiyohara * must display the following acknowledgement: 16 1.1 kiyohara * 17 1.1 kiyohara * This product includes software developed by Hidetoshi Shimokawa. 18 1.1 kiyohara * 19 1.1 kiyohara * 4. Neither the name of the author nor the names of its contributors 20 1.1 kiyohara * may be used to endorse or promote products derived from this software 21 1.1 kiyohara * without specific prior written permission. 22 1.6 kiyohara * 23 1.1 kiyohara * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 24 1.1 kiyohara * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 25 1.1 kiyohara * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 26 1.1 kiyohara * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 27 1.1 kiyohara * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 28 1.1 kiyohara * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 29 1.1 kiyohara * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 30 1.1 kiyohara * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 31 1.1 kiyohara * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 32 1.1 kiyohara * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 33 1.1 kiyohara * SUCH DAMAGE. 34 1.6 kiyohara * 35 1.1 kiyohara * $FreeBSD: /repoman/r/ncvs/src/sys/dev/firewire/fwdma.h,v 1.3 2005/01/06 01:42:41 imp Exp $ 36 1.1 kiyohara */ 37 1.1 kiyohara 38 1.6 kiyohara #ifndef _FWDMA_H_ 39 1.6 kiyohara #define _FWDMA_H_ 40 1.6 kiyohara 41 1.1 kiyohara struct fwdma_alloc { 42 1.6 kiyohara bus_dma_tag_t dma_tag; 43 1.1 kiyohara bus_dmamap_t dma_map; 44 1.1 kiyohara void * v_addr; 45 1.1 kiyohara bus_addr_t bus_addr; 46 1.1 kiyohara }; 47 1.1 kiyohara 48 1.1 kiyohara struct fwdma_seg { 49 1.1 kiyohara bus_dmamap_t dma_map; 50 1.1 kiyohara void * v_addr; 51 1.1 kiyohara bus_addr_t bus_addr; 52 1.1 kiyohara }; 53 1.1 kiyohara 54 1.1 kiyohara struct fwdma_alloc_multi { 55 1.1 kiyohara bus_size_t ssize; 56 1.1 kiyohara bus_size_t esize; 57 1.1 kiyohara int nseg; 58 1.6 kiyohara bus_dma_tag_t dma_tag; 59 1.1 kiyohara struct fwdma_seg seg[0]; 60 1.1 kiyohara }; 61 1.1 kiyohara 62 1.4 perry static __inline void * 63 1.1 kiyohara fwdma_v_addr(struct fwdma_alloc_multi *am, int index) 64 1.1 kiyohara { 65 1.1 kiyohara bus_size_t ssize = am->ssize; 66 1.1 kiyohara int offset = am->esize * index; 67 1.1 kiyohara 68 1.6 kiyohara return (char *)am->seg[offset / ssize].v_addr + (offset % ssize); 69 1.1 kiyohara } 70 1.1 kiyohara 71 1.4 perry static __inline bus_addr_t 72 1.1 kiyohara fwdma_bus_addr(struct fwdma_alloc_multi *am, int index) 73 1.1 kiyohara { 74 1.1 kiyohara bus_size_t ssize = am->ssize; 75 1.1 kiyohara int offset = am->esize * index; 76 1.1 kiyohara 77 1.6 kiyohara return am->seg[offset / ssize].bus_addr + (offset % ssize); 78 1.1 kiyohara } 79 1.1 kiyohara 80 1.4 perry static __inline void 81 1.6 kiyohara fwdma_sync_multiseg(struct fwdma_alloc_multi *am, int start, int end, int op) 82 1.1 kiyohara { 83 1.1 kiyohara struct fwdma_seg *seg, *eseg; 84 1.6 kiyohara bus_addr_t off, eoff; 85 1.1 kiyohara 86 1.6 kiyohara off = (am->esize * start) % am->ssize; 87 1.6 kiyohara eoff = (am->esize * end) % am->ssize; 88 1.1 kiyohara seg = &am->seg[am->esize * start / am->ssize]; 89 1.1 kiyohara eseg = &am->seg[am->esize * end / am->ssize]; 90 1.6 kiyohara 91 1.6 kiyohara if (start > end) { 92 1.6 kiyohara for (; seg < &am->seg[am->nseg]; seg++) { 93 1.6 kiyohara bus_dmamap_sync(am->dma_tag, seg->dma_map, 94 1.6 kiyohara off, seg->dma_map->dm_mapsize - off, op); 95 1.6 kiyohara off = 0; 96 1.6 kiyohara } 97 1.6 kiyohara seg = am->seg; 98 1.6 kiyohara } 99 1.6 kiyohara for (; seg < eseg; seg++) { 100 1.6 kiyohara bus_dmamap_sync(am->dma_tag, seg->dma_map, 101 1.6 kiyohara off, seg->dma_map->dm_mapsize - off, op); 102 1.6 kiyohara off = 0; 103 1.6 kiyohara } 104 1.6 kiyohara bus_dmamap_sync(am->dma_tag, seg->dma_map, 105 1.6 kiyohara off, eoff - off + am->esize, op); 106 1.1 kiyohara } 107 1.1 kiyohara 108 1.4 perry static __inline void 109 1.6 kiyohara fwdma_sync_multiseg_all(struct fwdma_alloc_multi *am, int op) 110 1.1 kiyohara { 111 1.1 kiyohara struct fwdma_seg *seg; 112 1.1 kiyohara int i; 113 1.1 kiyohara 114 1.6 kiyohara seg = am->seg; 115 1.1 kiyohara for (i = 0; i < am->nseg; i++, seg++) 116 1.6 kiyohara bus_dmamap_sync(am->dma_tag, seg->dma_map, 117 1.6 kiyohara 0, seg->dma_map->dm_mapsize, op); 118 1.1 kiyohara } 119 1.1 kiyohara 120 1.6 kiyohara void *fwdma_malloc(device_t, bus_dma_tag_t, bus_dmamap_t *, bus_size_t, int, 121 1.6 kiyohara int); 122 1.6 kiyohara void fwdma_free(bus_dma_tag_t, bus_dmamap_t, void *); 123 1.6 kiyohara void *fwdma_alloc_setup(device_t, bus_dma_tag_t, bus_size_t, 124 1.6 kiyohara struct fwdma_alloc *, int, int); 125 1.1 kiyohara struct fwdma_alloc_multi *fwdma_malloc_multiseg(struct firewire_comm *, 126 1.6 kiyohara int, int, int, int); 127 1.1 kiyohara void fwdma_free_multiseg(struct fwdma_alloc_multi *); 128 1.1 kiyohara 129 1.6 kiyohara #endif /* _FWDMA_H_ */ 130