Merge commit 'kumar/kumar-dma'
[linux-2.6] / arch / powerpc / kernel / dma.c
1 /*
2  * Copyright (C) 2006 Benjamin Herrenschmidt, IBM Corporation
3  *
4  * Provide default implementations of the DMA mapping callbacks for
5  * directly mapped busses.
6  */
7
8 #include <linux/device.h>
9 #include <linux/dma-mapping.h>
10 #include <asm/bug.h>
11 #include <asm/abs_addr.h>
12
13 /*
14  * Generic direct DMA implementation
15  *
16  * This implementation supports a per-device offset that can be applied if
17  * the address at which memory is visible to devices is not 0. Platform code
18  * can set archdata.dma_data to an unsigned long holding the offset. By
19  * default the offset is PCI_DRAM_OFFSET.
20  */
21
22 static unsigned long get_dma_direct_offset(struct device *dev)
23 {
24         if (dev)
25                 return (unsigned long)dev->archdata.dma_data;
26
27         return PCI_DRAM_OFFSET;
28 }
29
30 void *dma_direct_alloc_coherent(struct device *dev, size_t size,
31                                 dma_addr_t *dma_handle, gfp_t flag)
32 {
33 #ifdef CONFIG_NOT_COHERENT_CACHE
34         return __dma_alloc_coherent(size, dma_handle, flag);
35 #else
36         struct page *page;
37         void *ret;
38         int node = dev_to_node(dev);
39
40         /* ignore region specifiers */
41         flag  &= ~(__GFP_HIGHMEM);
42
43         page = alloc_pages_node(node, flag, get_order(size));
44         if (page == NULL)
45                 return NULL;
46         ret = page_address(page);
47         memset(ret, 0, size);
48         *dma_handle = virt_to_abs(ret) + get_dma_direct_offset(dev);
49
50         return ret;
51 #endif
52 }
53
54 void dma_direct_free_coherent(struct device *dev, size_t size,
55                               void *vaddr, dma_addr_t dma_handle)
56 {
57 #ifdef CONFIG_NOT_COHERENT_CACHE
58         __dma_free_coherent(size, vaddr);
59 #else
60         free_pages((unsigned long)vaddr, get_order(size));
61 #endif
62 }
63
64 static int dma_direct_map_sg(struct device *dev, struct scatterlist *sgl,
65                              int nents, enum dma_data_direction direction,
66                              struct dma_attrs *attrs)
67 {
68         struct scatterlist *sg;
69         int i;
70
71         for_each_sg(sgl, sg, nents, i) {
72                 sg->dma_address = sg_phys(sg) + get_dma_direct_offset(dev);
73                 sg->dma_length = sg->length;
74         }
75
76         return nents;
77 }
78
79 static void dma_direct_unmap_sg(struct device *dev, struct scatterlist *sg,
80                                 int nents, enum dma_data_direction direction,
81                                 struct dma_attrs *attrs)
82 {
83 }
84
85 static int dma_direct_dma_supported(struct device *dev, u64 mask)
86 {
87 #ifdef CONFIG_PPC64
88         /* Could be improved to check for memory though it better be
89          * done via some global so platforms can set the limit in case
90          * they have limited DMA windows
91          */
92         return mask >= DMA_32BIT_MASK;
93 #else
94         return 1;
95 #endif
96 }
97
98 static inline dma_addr_t dma_direct_map_page(struct device *dev,
99                                              struct page *page,
100                                              unsigned long offset,
101                                              size_t size,
102                                              enum dma_data_direction dir,
103                                              struct dma_attrs *attrs)
104 {
105         BUG_ON(dir == DMA_NONE);
106         __dma_sync_page(page, offset, size, dir);
107         return page_to_phys(page) + offset + get_dma_direct_offset(dev);
108 }
109
110 static inline void dma_direct_unmap_page(struct device *dev,
111                                          dma_addr_t dma_address,
112                                          size_t size,
113                                          enum dma_data_direction direction,
114                                          struct dma_attrs *attrs)
115 {
116 }
117
118 struct dma_mapping_ops dma_direct_ops = {
119         .alloc_coherent = dma_direct_alloc_coherent,
120         .free_coherent  = dma_direct_free_coherent,
121         .map_sg         = dma_direct_map_sg,
122         .unmap_sg       = dma_direct_unmap_sg,
123         .dma_supported  = dma_direct_dma_supported,
124         .map_page       = dma_direct_map_page,
125         .unmap_page     = dma_direct_unmap_page,
126 };
127 EXPORT_SYMBOL(dma_direct_ops);