/*-
* Copyright (c) 2003 Izumi Tsutsui. 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.
*
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``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 AUTHOR 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.
*/
/*-
* Copyright (C) 2000 Shuichiro URATA. 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. The name of the author may not be used to endorse or promote products
* derived from this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``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 AUTHOR 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.
*/
static int
jazz_bus_dmamap_alloc_sgmap(bus_dma_tag_t t, bus_dma_segment_t *segs,
int nsegs, bus_size_t boundary, int flags)
{
jazz_dma_pte_t *dmapte;
bus_addr_t addr;
bus_size_t off;
int i, npte;
for (i = 0; i < nsegs; i++) {
off = jazz_dma_page_offs(segs[i]._ds_paddr);
npte = jazz_dma_page_round(segs[i].ds_len + off) /
JAZZ_DMA_PAGE_SIZE;
dmapte = jazz_dmatlb_alloc(npte, boundary, flags, &addr);
if (dmapte == NULL)
return ENOMEM;
segs[i].ds_addr = addr + off;
static void
jazz_bus_dmamap_free_sgmap(bus_dma_tag_t t, bus_dma_segment_t *segs, int nsegs)
{
int i, npte;
bus_addr_t addr;
for (i = 0; i < nsegs; i++) {
addr = (segs[i].ds_addr - t->dma_offset) & JAZZ_DMA_PAGE_NUM;
npte = jazz_dma_page_round(segs[i].ds_len +
jazz_dma_page_offs(segs[i].ds_addr)) / JAZZ_DMA_PAGE_SIZE;
jazz_dmatlb_free(addr, npte);
}
}
/*
* function to create a DMA map. If BUS_DMA_ALLOCNOW is specified and
* nsegments is 1, allocate jazzdmatlb here, too.
*/
int
jazz_bus_dmamap_create(bus_dma_tag_t t, bus_size_t size, int nsegments,
bus_size_t maxsegsz, bus_size_t boundary, int flags, bus_dmamap_t *dmamp)
{
struct arc_bus_dmamap *map;
jazz_tlbmap_t tlbmap;
int error, npte;
if (nsegments > 1)
/*
* BUS_DMA_ALLOCNOW is allowed only with one segment for now.
* XXX needs re-think.
*/
flags &= ~BUS_DMA_ALLOCNOW;
/*
* function to destroy a DMA map. If BUS_DMA_ALLOCNOW is specified,
* free jazzdmatlb, too.
*/
void
jazz_bus_dmamap_destroy(bus_dma_tag_t t, bus_dmamap_t map)
{
if ((map->_dm_flags & BUS_DMA_ALLOCNOW) != 0) {
jazz_tlbmap_t tlbmap;
int npte;
/*
* function for loading a direct-mapped DMA map with a linear buffer.
*/
int
jazz_bus_dmamap_load(bus_dma_tag_t t, bus_dmamap_t map, void *buf,
bus_size_t buflen, struct proc *p, int flags)
{
int error;
if ((map->_dm_flags & BUS_DMA_ALLOCNOW) != 0) {
/* just use pre-allocated DMA TLB for the buffer */
jazz_tlbmap_t tlbmap;
bus_size_t off;
struct vmspace *vm;
if (p != NULL) {
vm = p->p_vmspace;
} else {
vm = vmspace_kernel();
}
tlbmap = (jazz_tlbmap_t)map->_dm_cookie;
off = jazz_dma_page_offs(buf);
jazz_dmatlb_map_va(vm, (vaddr_t)buf, buflen, tlbmap->ptebase);
/*
* Like jazz_bus_dmamap_load(), but for mbufs.
*/
int
jazz_bus_dmamap_load_mbuf(bus_dma_tag_t t, bus_dmamap_t map, struct mbuf *m0,
int flags)
{
int error;
if ((map->_dm_flags & BUS_DMA_ALLOCNOW) != 0)
/* BUS_DMA_ALLOCNOW is valid only for linear buffer. */
return ENODEV; /* XXX which errno is better? */
/*
* Like jazz_bus_dmamap_load(), but for uios.
*/
int
jazz_bus_dmamap_load_uio(bus_dma_tag_t t, bus_dmamap_t map, struct uio *uio,
int flags)
{
int error;
if ((map->_dm_flags & BUS_DMA_ALLOCNOW) != 0)
/* BUS_DMA_ALLOCNOW is valid only for linear buffer. */
return ENODEV; /* XXX which errno is better? */
/*
* Like _bus_dmamap_load(), but for raw memory.
*/
int
jazz_bus_dmamap_load_raw(bus_dma_tag_t t, bus_dmamap_t map,
bus_dma_segment_t *segs, int nsegs, bus_size_t size, int flags)
{
int error;
if ((map->_dm_flags & BUS_DMA_ALLOCNOW) != 0)
/* BUS_DMA_ALLOCNOW is valid only for linear buffer. */
return ENODEV; /* XXX which errno is better? */