* Copyright (c) 2003
* Hidetoshi Shimokawa. All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* 3. All advertising materials mentioning features or use of this software
* must display the following acknowledgement:
*
* This product includes software developed by Hidetoshi Shimokawa.
*
* 4. Neither the name of the author nor the names of its contributors
* may be used to endorse or promote products derived from this software
* without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
*
*/
#include <OS.h>
#include <malloc.h>
#include "queue.h"
#include "fwglue.h"
#include "firewire.h"
#include "iec13213.h"
#include "firewirereg.h"
#include "fwdma.h"
#include "util.h"
#if 0
static void
fwdma_map_cb(void *arg, bus_dma_segment_t *segs, int nseg, int error)
{
bus_addr_t *baddr;
if (error)
printf("fwdma_map_cb: error=%d\n", error);
baddr = (bus_addr_t *)arg;
*baddr = segs->ds_addr;
}
void *
fwdma_malloc(struct firewire_comm *fc, int alignment, bus_size_t size,
struct fwdma_alloc *dma, int flag)
{
int err;
dma->v_addr = NULL;
err = bus_dma_tag_create(
fc->dmat,
alignment,
0,
BUS_SPACE_MAXADDR_32BIT,
BUS_SPACE_MAXADDR,
NULL, NULL,
size,
1,
BUS_SPACE_MAXSIZE_32BIT,
BUS_DMA_ALLOCNOW,
#if defined(__FreeBSD__) && __FreeBSD_version >= 501102
busdma_lock_mutex,
FW_GMTX(fc),
#endif
&dma->dma_tag);
if (err) {
printf("fwdma_malloc: failed(1)\n");
return(NULL);
}
err = bus_dmamem_alloc(dma->dma_tag, &dma->v_addr,
flag, &dma->dma_map);
if (err) {
printf("fwdma_malloc: failed(2)\n");
return(NULL);
}
bus_dmamap_load(dma->dma_tag, dma->dma_map, dma->v_addr,
size, fwdma_map_cb, &dma->bus_addr, 0);
return(dma->v_addr);
}
void
fwdma_free(struct firewire_comm *fc, struct fwdma_alloc *dma)
{
bus_dmamap_unload(dma->dma_tag, dma->dma_map);
bus_dmamem_free(dma->dma_tag, dma->v_addr, dma->dma_map);
bus_dma_tag_destroy(dma->dma_tag);
}
void *
fwdma_malloc_size(bus_dma_tag_t dmat, bus_dmamap_t *dmamap,
bus_size_t size, bus_addr_t *bus_addr, int flag)
{
void *v_addr;
if (bus_dmamem_alloc(dmat, &v_addr, flag, dmamap)) {
printf("fwdma_malloc_size: failed(1)\n");
return(NULL);
}
bus_dmamap_load(dmat, *dmamap, v_addr, size,
fwdma_map_cb, bus_addr, 0);
return(v_addr);
}
void
fwdma_free_size(bus_dma_tag_t dmat, bus_dmamap_t dmamap,
void *vaddr, bus_size_t size)
{
bus_dmamap_unload(dmat, dmamap);
bus_dmamem_free(dmat, vaddr, dmamap);
}
#endif
* Allocate multisegment dma buffers
* each segment size is eqaul to ssize except last segment.
*/
struct fwdma_alloc_multi *
fwdma_malloc_multiseg(int alignment,
int esize, int n)
{
struct fwdma_alloc_multi *am;
struct fwdma_seg *seg;
int ssize;
int nseg, i;
void *buf_virt, *buf_phy;
if (esize > B_PAGE_SIZE) {
esize = ssize = roundup2(esize, B_PAGE_SIZE);
nseg = n;
} else {
nseg = howmany(n, ssize / esize);*/
esize = roundup2(esize, alignment);
ssize = rounddown(B_PAGE_SIZE, esize);
nseg = howmany(n, ssize / esize);
}
am = (struct fwdma_alloc_multi *)malloc(sizeof(struct fwdma_alloc_multi)
+ sizeof(struct fwdma_seg)*nseg);
if (am == NULL) {
dprintf("fwdma_malloc_multiseg: malloc failed\n");
return(NULL);
}
am->ssize = ssize;
am->esize = esize;
am->nseg = nseg;
#if 0
if (bus_dma_tag_create(
fc->dmat,
alignment,
0,
BUS_SPACE_MAXADDR_32BIT,
BUS_SPACE_MAXADDR,
NULL, NULL,
ssize,
1,
BUS_SPACE_MAXSIZE_32BIT,
BUS_DMA_ALLOCNOW,
#if defined(__FreeBSD__) && __FreeBSD_version >= 501102
busdma_lock_mutex,
FW_GMTX(fc),
#endif
&am->dma_tag)) {
printf("fwdma_malloc_multiseg: tag_create failed\n");
free(am, M_FW);
return(NULL);
}
#endif
am->Area = alloc_mem(&buf_virt, &buf_phy, nseg*ssize, 0, "fw multi dma buf");
seg->v_addr = fwdma_malloc_size(am->dma_tag, &seg->dma_map,
ssize, &seg->bus_addr, flag);
if (seg->v_addr == NULL) {
printf("fwdma_malloc_multi: malloc_size failed %d\n",
am->nseg);
fwdma_free_multiseg(am);
return(NULL);
}
am->nseg++;
}*/
for(i = 0, seg=&am->seg[0]; i < nseg; i++){
seg->v_addr =(char *)buf_virt + (i * ssize);
seg->bus_addr = (bus_addr_t)((char *)buf_phy + (i * ssize));
seg++;
}
return(am);
}
void
fwdma_free_multiseg(struct fwdma_alloc_multi *am)
{
for (seg = &am->seg[0]; am->nseg --; seg ++) {
fwdma_free_size(am->dma_tag, seg->dma_map,
seg->v_addr, am->ssize);
}
bus_dma_tag_destroy(am->dma_tag);*/
delete_area(am->Area);
free(am);
}