1 /* pci-dma-nommu.c: Dynamic DMA mapping support for the FRV
3 * Copyright (C) 2004 Red Hat, Inc. All Rights Reserved.
4 * Written by David Woodhouse (dwmw2@infradead.org)
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU General Public License
8 * as published by the Free Software Foundation; either version
9 * 2 of the License, or (at your option) any later version.
12 #include <linux/types.h>
13 #include <linux/slab.h>
14 #include <linux/dma-mapping.h>
15 #include <linux/list.h>
16 #include <linux/pci.h>
20 #define DMA_SRAM_START dma_coherent_mem_start
21 #define DMA_SRAM_END dma_coherent_mem_end
22 #else // Use video RAM on Matrox
23 #define DMA_SRAM_START 0xe8900000
24 #define DMA_SRAM_END 0xe8a00000
27 struct dma_alloc_record {
28 struct list_head list;
33 static DEFINE_SPINLOCK(dma_alloc_lock);
34 static LIST_HEAD(dma_alloc_list);
36 void *dma_alloc_coherent(struct device *hwdev, size_t size, dma_addr_t *dma_handle, gfp_t gfp)
38 struct dma_alloc_record *new;
39 struct list_head *this = &dma_alloc_list;
41 unsigned long start = DMA_SRAM_START;
44 if (!DMA_SRAM_START) {
45 printk("%s called without any DMA area reserved!\n", __func__);
49 new = kmalloc(sizeof (*new), GFP_ATOMIC);
53 /* Round up to a reasonable alignment */
54 new->len = (size + 31) & ~31;
56 spin_lock_irqsave(&dma_alloc_lock, flags);
58 list_for_each (this, &dma_alloc_list) {
59 struct dma_alloc_record *this_r = list_entry(this, struct dma_alloc_record, list);
62 if (end - start >= size)
65 start = this_r->ofs + this_r->len;
67 /* Reached end of list. */
69 this = &dma_alloc_list;
71 if (end - start >= size) {
74 list_add_tail(&new->list, this);
75 spin_unlock_irqrestore(&dma_alloc_lock, flags);
82 spin_unlock_irqrestore(&dma_alloc_lock, flags);
86 EXPORT_SYMBOL(dma_alloc_coherent);
88 void dma_free_coherent(struct device *hwdev, size_t size, void *vaddr, dma_addr_t dma_handle)
90 struct dma_alloc_record *rec;
93 spin_lock_irqsave(&dma_alloc_lock, flags);
95 list_for_each_entry(rec, &dma_alloc_list, list) {
96 if (rec->ofs == dma_handle) {
99 spin_unlock_irqrestore(&dma_alloc_lock, flags);
103 spin_unlock_irqrestore(&dma_alloc_lock, flags);
107 EXPORT_SYMBOL(dma_free_coherent);
110 * Map a single buffer of the indicated size for DMA in streaming mode.
111 * The 32-bit bus address to use is returned.
113 * Once the device is given the dma address, the device owns this memory
114 * until either dma_unmap_single or pci_dma_sync_single is performed.
116 dma_addr_t dma_map_single(struct device *dev, void *ptr, size_t size,
117 enum dma_data_direction direction)
119 BUG_ON(direction == DMA_NONE);
121 frv_cache_wback_inv((unsigned long) ptr, (unsigned long) ptr + size);
123 return virt_to_bus(ptr);
126 EXPORT_SYMBOL(dma_map_single);
129 * Map a set of buffers described by scatterlist in streaming
130 * mode for DMA. This is the scather-gather version of the
131 * above dma_map_single interface. Here the scatter gather list
132 * elements are each tagged with the appropriate dma address
133 * and length. They are obtained via sg_dma_{address,length}(SG).
135 * NOTE: An implementation may be able to use a smaller number of
136 * DMA address/length pairs than there are SG table elements.
137 * (for example via virtual mapping capabilities)
138 * The routine returns the number of addr/length pairs actually
139 * used, at most nents.
141 * Device ownership issues as mentioned above for dma_map_single are
144 int dma_map_sg(struct device *dev, struct scatterlist *sg, int nents,
145 enum dma_data_direction direction)
149 for (i=0; i<nents; i++)
150 frv_cache_wback_inv(sg_dma_address(&sg[i]),
151 sg_dma_address(&sg[i]) + sg_dma_len(&sg[i]));
153 BUG_ON(direction == DMA_NONE);
158 EXPORT_SYMBOL(dma_map_sg);
161 * Map a single page of the indicated size for DMA in streaming mode.
162 * The 32-bit bus address to use is returned.
164 * Device ownership issues as mentioned above for dma_map_single are
167 dma_addr_t dma_map_page(struct device *dev, struct page *page, unsigned long offset,
168 size_t size, enum dma_data_direction direction)
170 BUG_ON(direction == DMA_NONE);
171 flush_dcache_page(page);
172 return (dma_addr_t) page_to_phys(page) + offset;
175 EXPORT_SYMBOL(dma_map_page);