blob: e47857f889b6d2a18dcf839249bae5f7af66f334 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/* pci-dma-nommu.c: Dynamic DMA mapping support for the FRV
2 *
3 * Copyright (C) 2004 Red Hat, Inc. All Rights Reserved.
David Woodhouse44d1b982008-06-05 22:46:18 -07004 * Written by David Woodhouse (dwmw2@infradead.org)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005 *
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.
10 */
11
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>
17#include <asm/io.h>
18
19#if 1
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
25#endif
26
27struct dma_alloc_record {
28 struct list_head list;
29 unsigned long ofs;
30 unsigned long len;
31};
32
33static DEFINE_SPINLOCK(dma_alloc_lock);
34static LIST_HEAD(dma_alloc_list);
35
Al Viroa5da7d32005-10-21 03:21:18 -040036void *dma_alloc_coherent(struct device *hwdev, size_t size, dma_addr_t *dma_handle, gfp_t gfp)
Linus Torvalds1da177e2005-04-16 15:20:36 -070037{
38 struct dma_alloc_record *new;
39 struct list_head *this = &dma_alloc_list;
40 unsigned long flags;
41 unsigned long start = DMA_SRAM_START;
42 unsigned long end;
43
44 if (!DMA_SRAM_START) {
45 printk("%s called without any DMA area reserved!\n", __func__);
46 return NULL;
47 }
48
49 new = kmalloc(sizeof (*new), GFP_ATOMIC);
50 if (!new)
51 return NULL;
52
53 /* Round up to a reasonable alignment */
54 new->len = (size + 31) & ~31;
55
56 spin_lock_irqsave(&dma_alloc_lock, flags);
57
58 list_for_each (this, &dma_alloc_list) {
59 struct dma_alloc_record *this_r = list_entry(this, struct dma_alloc_record, list);
60 end = this_r->ofs;
61
62 if (end - start >= size)
63 goto gotone;
64
65 start = this_r->ofs + this_r->len;
66 }
67 /* Reached end of list. */
68 end = DMA_SRAM_END;
69 this = &dma_alloc_list;
70
71 if (end - start >= size) {
72 gotone:
73 new->ofs = start;
74 list_add_tail(&new->list, this);
75 spin_unlock_irqrestore(&dma_alloc_lock, flags);
76
77 *dma_handle = start;
78 return (void *)start;
79 }
80
81 kfree(new);
82 spin_unlock_irqrestore(&dma_alloc_lock, flags);
83 return NULL;
84}
85
David Howells40234402006-01-08 01:01:19 -080086EXPORT_SYMBOL(dma_alloc_coherent);
87
Linus Torvalds1da177e2005-04-16 15:20:36 -070088void dma_free_coherent(struct device *hwdev, size_t size, void *vaddr, dma_addr_t dma_handle)
89{
90 struct dma_alloc_record *rec;
91 unsigned long flags;
92
93 spin_lock_irqsave(&dma_alloc_lock, flags);
94
95 list_for_each_entry(rec, &dma_alloc_list, list) {
96 if (rec->ofs == dma_handle) {
97 list_del(&rec->list);
98 kfree(rec);
99 spin_unlock_irqrestore(&dma_alloc_lock, flags);
100 return;
101 }
102 }
103 spin_unlock_irqrestore(&dma_alloc_lock, flags);
104 BUG();
105}
106
David Howells40234402006-01-08 01:01:19 -0800107EXPORT_SYMBOL(dma_free_coherent);
108
Linus Torvalds1da177e2005-04-16 15:20:36 -0700109dma_addr_t dma_map_single(struct device *dev, void *ptr, size_t size,
110 enum dma_data_direction direction)
111{
Stoyan Gaydarovdb5c4442009-06-11 13:05:04 +0100112 BUG_ON(direction == DMA_NONE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700113
114 frv_cache_wback_inv((unsigned long) ptr, (unsigned long) ptr + size);
115
116 return virt_to_bus(ptr);
117}
118
David Howells40234402006-01-08 01:01:19 -0800119EXPORT_SYMBOL(dma_map_single);
120
Linus Torvalds1da177e2005-04-16 15:20:36 -0700121int dma_map_sg(struct device *dev, struct scatterlist *sg, int nents,
122 enum dma_data_direction direction)
123{
124 int i;
125
126 for (i=0; i<nents; i++)
127 frv_cache_wback_inv(sg_dma_address(&sg[i]),
128 sg_dma_address(&sg[i]) + sg_dma_len(&sg[i]));
129
Stoyan Gaydarovdb5c4442009-06-11 13:05:04 +0100130 BUG_ON(direction == DMA_NONE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700131
132 return nents;
133}
David Howells40234402006-01-08 01:01:19 -0800134
135EXPORT_SYMBOL(dma_map_sg);
David Howellsc9af9562008-10-15 16:50:53 +0100136
David Howellsc9af9562008-10-15 16:50:53 +0100137dma_addr_t dma_map_page(struct device *dev, struct page *page, unsigned long offset,
138 size_t size, enum dma_data_direction direction)
139{
140 BUG_ON(direction == DMA_NONE);
141 flush_dcache_page(page);
142 return (dma_addr_t) page_to_phys(page) + offset;
143}
144
145EXPORT_SYMBOL(dma_map_page);