Path: blob/master/arch/frv/mb93090-mb00/pci-dma-nommu.c
10817 views
/* pci-dma-nommu.c: Dynamic DMA mapping support for the FRV1*2* Copyright (C) 2004 Red Hat, Inc. All Rights Reserved.3* Written by David Woodhouse ([email protected])4*5* This program is free software; you can redistribute it and/or6* modify it under the terms of the GNU General Public License7* as published by the Free Software Foundation; either version8* 2 of the License, or (at your option) any later version.9*/1011#include <linux/types.h>12#include <linux/slab.h>13#include <linux/dma-mapping.h>14#include <linux/list.h>15#include <linux/pci.h>16#include <asm/io.h>1718#if 119#define DMA_SRAM_START dma_coherent_mem_start20#define DMA_SRAM_END dma_coherent_mem_end21#else // Use video RAM on Matrox22#define DMA_SRAM_START 0xe890000023#define DMA_SRAM_END 0xe8a0000024#endif2526struct dma_alloc_record {27struct list_head list;28unsigned long ofs;29unsigned long len;30};3132static DEFINE_SPINLOCK(dma_alloc_lock);33static LIST_HEAD(dma_alloc_list);3435void *dma_alloc_coherent(struct device *hwdev, size_t size, dma_addr_t *dma_handle, gfp_t gfp)36{37struct dma_alloc_record *new;38struct list_head *this = &dma_alloc_list;39unsigned long flags;40unsigned long start = DMA_SRAM_START;41unsigned long end;4243if (!DMA_SRAM_START) {44printk("%s called without any DMA area reserved!\n", __func__);45return NULL;46}4748new = kmalloc(sizeof (*new), GFP_ATOMIC);49if (!new)50return NULL;5152/* Round up to a reasonable alignment */53new->len = (size + 31) & ~31;5455spin_lock_irqsave(&dma_alloc_lock, flags);5657list_for_each (this, &dma_alloc_list) {58struct dma_alloc_record *this_r = list_entry(this, struct dma_alloc_record, list);59end = this_r->ofs;6061if (end - start >= size)62goto gotone;6364start = this_r->ofs + this_r->len;65}66/* Reached end of list. */67end = DMA_SRAM_END;68this = &dma_alloc_list;6970if (end - start >= size) {71gotone:72new->ofs = start;73list_add_tail(&new->list, this);74spin_unlock_irqrestore(&dma_alloc_lock, flags);7576*dma_handle = start;77return (void *)start;78}7980kfree(new);81spin_unlock_irqrestore(&dma_alloc_lock, flags);82return NULL;83}8485EXPORT_SYMBOL(dma_alloc_coherent);8687void dma_free_coherent(struct device *hwdev, size_t size, void *vaddr, dma_addr_t dma_handle)88{89struct dma_alloc_record *rec;90unsigned long flags;9192spin_lock_irqsave(&dma_alloc_lock, flags);9394list_for_each_entry(rec, &dma_alloc_list, list) {95if (rec->ofs == dma_handle) {96list_del(&rec->list);97kfree(rec);98spin_unlock_irqrestore(&dma_alloc_lock, flags);99return;100}101}102spin_unlock_irqrestore(&dma_alloc_lock, flags);103BUG();104}105106EXPORT_SYMBOL(dma_free_coherent);107108dma_addr_t dma_map_single(struct device *dev, void *ptr, size_t size,109enum dma_data_direction direction)110{111BUG_ON(direction == DMA_NONE);112113frv_cache_wback_inv((unsigned long) ptr, (unsigned long) ptr + size);114115return virt_to_bus(ptr);116}117118EXPORT_SYMBOL(dma_map_single);119120int dma_map_sg(struct device *dev, struct scatterlist *sg, int nents,121enum dma_data_direction direction)122{123int i;124125for (i=0; i<nents; i++)126frv_cache_wback_inv(sg_dma_address(&sg[i]),127sg_dma_address(&sg[i]) + sg_dma_len(&sg[i]));128129BUG_ON(direction == DMA_NONE);130131return nents;132}133134EXPORT_SYMBOL(dma_map_sg);135136dma_addr_t dma_map_page(struct device *dev, struct page *page, unsigned long offset,137size_t size, enum dma_data_direction direction)138{139BUG_ON(direction == DMA_NONE);140flush_dcache_page(page);141return (dma_addr_t) page_to_phys(page) + offset;142}143144EXPORT_SYMBOL(dma_map_page);145146147