blob: b159b8a847d69144d0a74753dca4d315bb80a7fd [file] [log] [blame]
Michal Simekccfe27d2010-01-14 11:21:02 +01001/*
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>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090011#include <linux/gfp.h>
Michal Simekccfe27d2010-01-14 11:21:02 +010012#include <linux/dma-debug.h>
Paul Gortmaker66421a62011-09-22 11:22:55 -040013#include <linux/export.h>
Michal Simekccfe27d2010-01-14 11:21:02 +010014#include <asm/bug.h>
Michal Simek2549edd2010-01-20 14:36:24 +010015#include <asm/cacheflush.h>
Michal Simekccfe27d2010-01-14 11:21:02 +010016
17/*
18 * Generic direct DMA implementation
19 *
20 * This implementation supports a per-device offset that can be applied if
21 * the address at which memory is visible to devices is not 0. Platform code
22 * can set archdata.dma_data to an unsigned long holding the offset. By
23 * default the offset is PCI_DRAM_OFFSET.
24 */
Michal Simekdcbae4b2010-02-09 09:25:08 +010025static inline void __dma_sync_page(unsigned long paddr, unsigned long offset,
Michal Simek2549edd2010-01-20 14:36:24 +010026 size_t size, enum dma_data_direction direction)
27{
Michal Simek2549edd2010-01-20 14:36:24 +010028 switch (direction) {
29 case DMA_TO_DEVICE:
Michal Simek5323c482011-02-10 08:37:49 +010030 case DMA_BIDIRECTIONAL:
Michal Simekdcbae4b2010-02-09 09:25:08 +010031 flush_dcache_range(paddr + offset, paddr + offset + size);
Michal Simek2549edd2010-01-20 14:36:24 +010032 break;
33 case DMA_FROM_DEVICE:
Michal Simekdcbae4b2010-02-09 09:25:08 +010034 invalidate_dcache_range(paddr + offset, paddr + offset + size);
Michal Simek2549edd2010-01-20 14:36:24 +010035 break;
36 default:
37 BUG();
38 }
39}
40
Michal Simekccfe27d2010-01-14 11:21:02 +010041static unsigned long get_dma_direct_offset(struct device *dev)
42{
Michal Simek78ebfa82010-03-23 15:37:02 +010043 if (likely(dev))
Michal Simekccfe27d2010-01-14 11:21:02 +010044 return (unsigned long)dev->archdata.dma_data;
45
46 return PCI_DRAM_OFFSET; /* FIXME Not sure if is correct */
47}
48
Michal Simek1be53e02010-03-11 14:15:48 +010049#define NOT_COHERENT_CACHE
50
51static void *dma_direct_alloc_coherent(struct device *dev, size_t size,
Michal Simekccfe27d2010-01-14 11:21:02 +010052 dma_addr_t *dma_handle, gfp_t flag)
53{
Michal Simek1be53e02010-03-11 14:15:48 +010054#ifdef NOT_COHERENT_CACHE
55 return consistent_alloc(flag, size, dma_handle);
56#else
Michal Simekccfe27d2010-01-14 11:21:02 +010057 void *ret;
58 struct page *page;
59 int node = dev_to_node(dev);
60
61 /* ignore region specifiers */
62 flag &= ~(__GFP_HIGHMEM);
63
64 page = alloc_pages_node(node, flag, get_order(size));
65 if (page == NULL)
66 return NULL;
67 ret = page_address(page);
68 memset(ret, 0, size);
69 *dma_handle = virt_to_phys(ret) + get_dma_direct_offset(dev);
70
71 return ret;
Michal Simek1be53e02010-03-11 14:15:48 +010072#endif
Michal Simekccfe27d2010-01-14 11:21:02 +010073}
74
Michal Simek1be53e02010-03-11 14:15:48 +010075static void dma_direct_free_coherent(struct device *dev, size_t size,
Michal Simekccfe27d2010-01-14 11:21:02 +010076 void *vaddr, dma_addr_t dma_handle)
77{
Michal Simek1be53e02010-03-11 14:15:48 +010078#ifdef NOT_COHERENT_CACHE
Michal Simekf1525762010-04-10 17:34:06 +020079 consistent_free(size, vaddr);
Michal Simek1be53e02010-03-11 14:15:48 +010080#else
Michal Simekccfe27d2010-01-14 11:21:02 +010081 free_pages((unsigned long)vaddr, get_order(size));
Michal Simek1be53e02010-03-11 14:15:48 +010082#endif
Michal Simekccfe27d2010-01-14 11:21:02 +010083}
84
85static int dma_direct_map_sg(struct device *dev, struct scatterlist *sgl,
86 int nents, enum dma_data_direction direction,
87 struct dma_attrs *attrs)
88{
89 struct scatterlist *sg;
90 int i;
91
Michal Simekd79f3b02010-02-08 12:13:10 +010092 /* FIXME this part of code is untested */
Michal Simekccfe27d2010-01-14 11:21:02 +010093 for_each_sg(sgl, sg, nents, i) {
94 sg->dma_address = sg_phys(sg) + get_dma_direct_offset(dev);
Michal Simekd79f3b02010-02-08 12:13:10 +010095 __dma_sync_page(page_to_phys(sg_page(sg)), sg->offset,
96 sg->length, direction);
Michal Simekccfe27d2010-01-14 11:21:02 +010097 }
98
99 return nents;
100}
101
102static void dma_direct_unmap_sg(struct device *dev, struct scatterlist *sg,
103 int nents, enum dma_data_direction direction,
104 struct dma_attrs *attrs)
105{
106}
107
108static int dma_direct_dma_supported(struct device *dev, u64 mask)
109{
110 return 1;
111}
112
113static inline dma_addr_t dma_direct_map_page(struct device *dev,
114 struct page *page,
115 unsigned long offset,
116 size_t size,
Michal Simek2549edd2010-01-20 14:36:24 +0100117 enum dma_data_direction direction,
Michal Simekccfe27d2010-01-14 11:21:02 +0100118 struct dma_attrs *attrs)
119{
Michal Simekd79f3b02010-02-08 12:13:10 +0100120 __dma_sync_page(page_to_phys(page), offset, size, direction);
Michal Simekccfe27d2010-01-14 11:21:02 +0100121 return page_to_phys(page) + offset + get_dma_direct_offset(dev);
122}
123
124static inline void dma_direct_unmap_page(struct device *dev,
125 dma_addr_t dma_address,
126 size_t size,
127 enum dma_data_direction direction,
128 struct dma_attrs *attrs)
129{
Michal Simekd79f3b02010-02-08 12:13:10 +0100130/* There is not necessary to do cache cleanup
131 *
132 * phys_to_virt is here because in __dma_sync_page is __virt_to_phys and
133 * dma_address is physical address
134 */
Michal Simek1be53e02010-03-11 14:15:48 +0100135 __dma_sync_page(dma_address, 0 , size, direction);
Michal Simekccfe27d2010-01-14 11:21:02 +0100136}
137
138struct dma_map_ops dma_direct_ops = {
139 .alloc_coherent = dma_direct_alloc_coherent,
140 .free_coherent = dma_direct_free_coherent,
141 .map_sg = dma_direct_map_sg,
142 .unmap_sg = dma_direct_unmap_sg,
143 .dma_supported = dma_direct_dma_supported,
144 .map_page = dma_direct_map_page,
145 .unmap_page = dma_direct_unmap_page,
146};
147EXPORT_SYMBOL(dma_direct_ops);
148
149/* Number of entries preallocated for DMA-API debugging */
150#define PREALLOC_DMA_DEBUG_ENTRIES (1 << 16)
151
152static int __init dma_init(void)
153{
154 dma_debug_init(PREALLOC_DMA_DEBUG_ENTRIES);
155
156 return 0;
157}
158fs_initcall(dma_init);