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/export.h>
15 #include <linux/dma-mapping.h>
16 #include <linux/list.h>
17 #include <linux/pci.h>
21 #define DMA_SRAM_START dma_coherent_mem_start
22 #define DMA_SRAM_END dma_coherent_mem_end
23 #else // Use video RAM on Matrox
24 #define DMA_SRAM_START 0xe8900000
25 #define DMA_SRAM_END 0xe8a00000
28 struct dma_alloc_record
{
29 struct list_head list
;
34 static DEFINE_SPINLOCK(dma_alloc_lock
);
35 static LIST_HEAD(dma_alloc_list
);
37 static void *frv_dma_alloc(struct device
*hwdev
, size_t size
, dma_addr_t
*dma_handle
,
38 gfp_t gfp
, unsigned long attrs
)
40 struct dma_alloc_record
*new;
41 struct list_head
*this = &dma_alloc_list
;
43 unsigned long start
= DMA_SRAM_START
;
46 if (!DMA_SRAM_START
) {
47 printk("%s called without any DMA area reserved!\n", __func__
);
51 new = kmalloc(sizeof (*new), GFP_ATOMIC
);
55 /* Round up to a reasonable alignment */
56 new->len
= (size
+ 31) & ~31;
58 spin_lock_irqsave(&dma_alloc_lock
, flags
);
60 list_for_each (this, &dma_alloc_list
) {
61 struct dma_alloc_record
*this_r
= list_entry(this, struct dma_alloc_record
, list
);
64 if (end
- start
>= size
)
67 start
= this_r
->ofs
+ this_r
->len
;
69 /* Reached end of list. */
71 this = &dma_alloc_list
;
73 if (end
- start
>= size
) {
76 list_add_tail(&new->list
, this);
77 spin_unlock_irqrestore(&dma_alloc_lock
, flags
);
84 spin_unlock_irqrestore(&dma_alloc_lock
, flags
);
88 static void frv_dma_free(struct device
*hwdev
, size_t size
, void *vaddr
,
89 dma_addr_t dma_handle
, unsigned long attrs
)
91 struct dma_alloc_record
*rec
;
94 spin_lock_irqsave(&dma_alloc_lock
, flags
);
96 list_for_each_entry(rec
, &dma_alloc_list
, list
) {
97 if (rec
->ofs
== dma_handle
) {
100 spin_unlock_irqrestore(&dma_alloc_lock
, flags
);
104 spin_unlock_irqrestore(&dma_alloc_lock
, flags
);
108 static int frv_dma_map_sg(struct device
*dev
, struct scatterlist
*sglist
,
109 int nents
, enum dma_data_direction direction
,
112 struct scatterlist
*sg
;
115 BUG_ON(direction
== DMA_NONE
);
117 if (attrs
& DMA_ATTR_SKIP_CPU_SYNC
)
120 for_each_sg(sglist
, sg
, nents
, i
) {
121 frv_cache_wback_inv(sg_dma_address(sg
),
122 sg_dma_address(sg
) + sg_dma_len(sg
));
128 static dma_addr_t
frv_dma_map_page(struct device
*dev
, struct page
*page
,
129 unsigned long offset
, size_t size
,
130 enum dma_data_direction direction
, unsigned long attrs
)
132 BUG_ON(direction
== DMA_NONE
);
134 if (!(attrs
& DMA_ATTR_SKIP_CPU_SYNC
))
135 flush_dcache_page(page
);
137 return (dma_addr_t
) page_to_phys(page
) + offset
;
140 static void frv_dma_sync_single_for_device(struct device
*dev
,
141 dma_addr_t dma_handle
, size_t size
,
142 enum dma_data_direction direction
)
144 flush_write_buffers();
147 static void frv_dma_sync_sg_for_device(struct device
*dev
,
148 struct scatterlist
*sg
, int nelems
,
149 enum dma_data_direction direction
)
151 flush_write_buffers();
155 static int frv_dma_supported(struct device
*dev
, u64 mask
)
158 * we fall back to GFP_DMA when the mask isn't all 1s,
159 * so we can't guarantee allocations that must be
160 * within a tighter range than GFP_DMA..
162 if (mask
< 0x00ffffff)
167 const struct dma_map_ops frv_dma_ops
= {
168 .alloc
= frv_dma_alloc
,
169 .free
= frv_dma_free
,
170 .map_page
= frv_dma_map_page
,
171 .map_sg
= frv_dma_map_sg
,
172 .sync_single_for_device
= frv_dma_sync_single_for_device
,
173 .sync_sg_for_device
= frv_dma_sync_sg_for_device
,
174 .dma_supported
= frv_dma_supported
,
176 EXPORT_SYMBOL(frv_dma_ops
);