]> git.karo-electronics.de Git - karo-tx-linux.git/blob - arch/microblaze/kernel/dma.c
microblaze: Preliminary support for dma drivers
[karo-tx-linux.git] / arch / microblaze / kernel / dma.c
1 /*
2  * Copyright (C) 2009-2010 PetaLogix
3  * Copyright (C) 2006 Benjamin Herrenschmidt, IBM Corporation
4  *
5  * Provide default implementations of the DMA mapping callbacks for
6  * directly mapped busses.
7  */
8
9 #include <linux/device.h>
10 #include <linux/dma-mapping.h>
11 #include <linux/dma-debug.h>
12 #include <asm/bug.h>
13 #include <asm/cacheflush.h>
14
15 /*
16  * Generic direct DMA implementation
17  *
18  * This implementation supports a per-device offset that can be applied if
19  * the address at which memory is visible to devices is not 0. Platform code
20  * can set archdata.dma_data to an unsigned long holding the offset. By
21  * default the offset is PCI_DRAM_OFFSET.
22  */
23 static inline void __dma_sync_page(unsigned long paddr, unsigned long offset,
24                                 size_t size, enum dma_data_direction direction)
25 {
26         switch (direction) {
27         case DMA_TO_DEVICE:
28                 flush_dcache_range(paddr + offset, paddr + offset + size);
29                 break;
30         case DMA_FROM_DEVICE:
31                 invalidate_dcache_range(paddr + offset, paddr + offset + size);
32                 break;
33         default:
34                 BUG();
35         }
36 }
37
38 static unsigned long get_dma_direct_offset(struct device *dev)
39 {
40         if (dev)
41                 return (unsigned long)dev->archdata.dma_data;
42
43         return PCI_DRAM_OFFSET; /* FIXME Not sure if is correct */
44 }
45
46 void *dma_direct_alloc_coherent(struct device *dev, size_t size,
47                                 dma_addr_t *dma_handle, gfp_t flag)
48 {
49         void *ret;
50         struct page *page;
51         int node = dev_to_node(dev);
52
53         /* ignore region specifiers */
54         flag  &= ~(__GFP_HIGHMEM);
55
56         page = alloc_pages_node(node, flag, get_order(size));
57         if (page == NULL)
58                 return NULL;
59         ret = page_address(page);
60         memset(ret, 0, size);
61         *dma_handle = virt_to_phys(ret) + get_dma_direct_offset(dev);
62
63         return ret;
64 }
65
66 void dma_direct_free_coherent(struct device *dev, size_t size,
67                               void *vaddr, dma_addr_t dma_handle)
68 {
69         free_pages((unsigned long)vaddr, get_order(size));
70 }
71
72 static int dma_direct_map_sg(struct device *dev, struct scatterlist *sgl,
73                              int nents, enum dma_data_direction direction,
74                              struct dma_attrs *attrs)
75 {
76         struct scatterlist *sg;
77         int i;
78
79         /* FIXME this part of code is untested */
80         for_each_sg(sgl, sg, nents, i) {
81                 sg->dma_address = sg_phys(sg) + get_dma_direct_offset(dev);
82                 sg->dma_length = sg->length;
83                 __dma_sync_page(page_to_phys(sg_page(sg)), sg->offset,
84                                                         sg->length, direction);
85         }
86
87         return nents;
88 }
89
90 static void dma_direct_unmap_sg(struct device *dev, struct scatterlist *sg,
91                                 int nents, enum dma_data_direction direction,
92                                 struct dma_attrs *attrs)
93 {
94 }
95
96 static int dma_direct_dma_supported(struct device *dev, u64 mask)
97 {
98         return 1;
99 }
100
101 static inline dma_addr_t dma_direct_map_page(struct device *dev,
102                                              struct page *page,
103                                              unsigned long offset,
104                                              size_t size,
105                                              enum dma_data_direction direction,
106                                              struct dma_attrs *attrs)
107 {
108         BUG_ON(direction == DMA_NONE);
109         __dma_sync_page(page_to_phys(page), offset, size, direction);
110         return page_to_phys(page) + offset + get_dma_direct_offset(dev);
111 }
112
113 static inline void dma_direct_unmap_page(struct device *dev,
114                                          dma_addr_t dma_address,
115                                          size_t size,
116                                          enum dma_data_direction direction,
117                                          struct dma_attrs *attrs)
118 {
119 /* There is not necessary to do cache cleanup
120  *
121  * phys_to_virt is here because in __dma_sync_page is __virt_to_phys and
122  * dma_address is physical address
123  */
124         __dma_sync_page((void *)dma_address, 0 , size, direction);
125 }
126
127 struct dma_map_ops dma_direct_ops = {
128         .alloc_coherent = dma_direct_alloc_coherent,
129         .free_coherent  = dma_direct_free_coherent,
130         .map_sg         = dma_direct_map_sg,
131         .unmap_sg       = dma_direct_unmap_sg,
132         .dma_supported  = dma_direct_dma_supported,
133         .map_page       = dma_direct_map_page,
134         .unmap_page     = dma_direct_unmap_page,
135 };
136 EXPORT_SYMBOL(dma_direct_ops);
137
138 /* Number of entries preallocated for DMA-API debugging */
139 #define PREALLOC_DMA_DEBUG_ENTRIES (1 << 16)
140
141 static int __init dma_init(void)
142 {
143        dma_debug_init(PREALLOC_DMA_DEBUG_ENTRIES);
144
145        return 0;
146 }
147 fs_initcall(dma_init);