blob: ec04dc1e2527ae9ae387ec9b5b3a7689b3274cf9 [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 Simek6bd55f02012-12-27 10:40:38 +010014#include <linux/bug.h>
Michal Simekccfe27d2010-01-14 11:21:02 +010015
Michal Simek1be53e02010-03-11 14:15:48 +010016#define NOT_COHERENT_CACHE
17
18static void *dma_direct_alloc_coherent(struct device *dev, size_t size,
Andrzej Pietrasiewicz988624e2012-03-27 14:56:04 +020019 dma_addr_t *dma_handle, gfp_t flag,
Krzysztof Kozlowski00085f12016-08-03 13:46:00 -070020 unsigned long attrs)
Michal Simekccfe27d2010-01-14 11:21:02 +010021{
Michal Simek1be53e02010-03-11 14:15:48 +010022#ifdef NOT_COHERENT_CACHE
23 return consistent_alloc(flag, size, dma_handle);
24#else
Michal Simekccfe27d2010-01-14 11:21:02 +010025 void *ret;
26 struct page *page;
27 int node = dev_to_node(dev);
28
29 /* ignore region specifiers */
30 flag &= ~(__GFP_HIGHMEM);
31
32 page = alloc_pages_node(node, flag, get_order(size));
33 if (page == NULL)
34 return NULL;
35 ret = page_address(page);
36 memset(ret, 0, size);
Michal Simek193bca52014-05-16 13:37:02 +020037 *dma_handle = virt_to_phys(ret);
Michal Simekccfe27d2010-01-14 11:21:02 +010038
39 return ret;
Michal Simek1be53e02010-03-11 14:15:48 +010040#endif
Michal Simekccfe27d2010-01-14 11:21:02 +010041}
42
Michal Simek1be53e02010-03-11 14:15:48 +010043static void dma_direct_free_coherent(struct device *dev, size_t size,
Andrzej Pietrasiewicz988624e2012-03-27 14:56:04 +020044 void *vaddr, dma_addr_t dma_handle,
Krzysztof Kozlowski00085f12016-08-03 13:46:00 -070045 unsigned long attrs)
Michal Simekccfe27d2010-01-14 11:21:02 +010046{
Michal Simek1be53e02010-03-11 14:15:48 +010047#ifdef NOT_COHERENT_CACHE
Michal Simekf1525762010-04-10 17:34:06 +020048 consistent_free(size, vaddr);
Michal Simek1be53e02010-03-11 14:15:48 +010049#else
Michal Simekccfe27d2010-01-14 11:21:02 +010050 free_pages((unsigned long)vaddr, get_order(size));
Michal Simek1be53e02010-03-11 14:15:48 +010051#endif
Michal Simekccfe27d2010-01-14 11:21:02 +010052}
53
54static int dma_direct_map_sg(struct device *dev, struct scatterlist *sgl,
55 int nents, enum dma_data_direction direction,
Krzysztof Kozlowski00085f12016-08-03 13:46:00 -070056 unsigned long attrs)
Michal Simekccfe27d2010-01-14 11:21:02 +010057{
58 struct scatterlist *sg;
59 int i;
60
Michal Simekd79f3b02010-02-08 12:13:10 +010061 /* FIXME this part of code is untested */
Michal Simekccfe27d2010-01-14 11:21:02 +010062 for_each_sg(sgl, sg, nents, i) {
Michal Simek193bca52014-05-16 13:37:02 +020063 sg->dma_address = sg_phys(sg);
Dan Williams3e6110f2015-12-15 12:54:06 -080064 __dma_sync(page_to_phys(sg_page(sg)) + sg->offset,
65 sg->length, direction);
Michal Simekccfe27d2010-01-14 11:21:02 +010066 }
67
68 return nents;
69}
70
Michal Simekccfe27d2010-01-14 11:21:02 +010071static int dma_direct_dma_supported(struct device *dev, u64 mask)
72{
73 return 1;
74}
75
76static inline dma_addr_t dma_direct_map_page(struct device *dev,
77 struct page *page,
78 unsigned long offset,
79 size_t size,
Michal Simek2549edd2010-01-20 14:36:24 +010080 enum dma_data_direction direction,
Krzysztof Kozlowski00085f12016-08-03 13:46:00 -070081 unsigned long attrs)
Michal Simekccfe27d2010-01-14 11:21:02 +010082{
Eli Billauercf560c12011-09-11 22:43:06 +030083 __dma_sync(page_to_phys(page) + offset, size, direction);
Michal Simek193bca52014-05-16 13:37:02 +020084 return page_to_phys(page) + offset;
Michal Simekccfe27d2010-01-14 11:21:02 +010085}
86
87static inline void dma_direct_unmap_page(struct device *dev,
88 dma_addr_t dma_address,
89 size_t size,
90 enum dma_data_direction direction,
Krzysztof Kozlowski00085f12016-08-03 13:46:00 -070091 unsigned long attrs)
Michal Simekccfe27d2010-01-14 11:21:02 +010092{
Michal Simekd79f3b02010-02-08 12:13:10 +010093/* There is not necessary to do cache cleanup
94 *
95 * phys_to_virt is here because in __dma_sync_page is __virt_to_phys and
96 * dma_address is physical address
97 */
Eli Billauercf560c12011-09-11 22:43:06 +030098 __dma_sync(dma_address, size, direction);
Michal Simekccfe27d2010-01-14 11:21:02 +010099}
100
Eli Billauer0fb2a6f2011-09-11 22:43:07 +0300101static inline void
102dma_direct_sync_single_for_cpu(struct device *dev,
103 dma_addr_t dma_handle, size_t size,
104 enum dma_data_direction direction)
105{
106 /*
107 * It's pointless to flush the cache as the memory segment
108 * is given to the CPU
109 */
110
111 if (direction == DMA_FROM_DEVICE)
112 __dma_sync(dma_handle, size, direction);
113}
114
115static inline void
116dma_direct_sync_single_for_device(struct device *dev,
117 dma_addr_t dma_handle, size_t size,
118 enum dma_data_direction direction)
119{
120 /*
121 * It's pointless to invalidate the cache if the device isn't
122 * supposed to write to the relevant region
123 */
124
125 if (direction == DMA_TO_DEVICE)
126 __dma_sync(dma_handle, size, direction);
127}
128
129static inline void
130dma_direct_sync_sg_for_cpu(struct device *dev,
131 struct scatterlist *sgl, int nents,
132 enum dma_data_direction direction)
133{
134 struct scatterlist *sg;
135 int i;
136
137 /* FIXME this part of code is untested */
138 if (direction == DMA_FROM_DEVICE)
139 for_each_sg(sgl, sg, nents, i)
140 __dma_sync(sg->dma_address, sg->length, direction);
141}
142
143static inline void
144dma_direct_sync_sg_for_device(struct device *dev,
145 struct scatterlist *sgl, int nents,
146 enum dma_data_direction direction)
147{
148 struct scatterlist *sg;
149 int i;
150
151 /* FIXME this part of code is untested */
152 if (direction == DMA_TO_DEVICE)
153 for_each_sg(sgl, sg, nents, i)
154 __dma_sync(sg->dma_address, sg->length, direction);
155}
156
Michal Simek55ae2f32015-06-05 10:35:31 +0200157static
Lars-Peter Clausen3a8e3262014-12-03 16:07:28 +0100158int dma_direct_mmap_coherent(struct device *dev, struct vm_area_struct *vma,
159 void *cpu_addr, dma_addr_t handle, size_t size,
Krzysztof Kozlowski00085f12016-08-03 13:46:00 -0700160 unsigned long attrs)
Lars-Peter Clausen3a8e3262014-12-03 16:07:28 +0100161{
162#ifdef CONFIG_MMU
163 unsigned long user_count = (vma->vm_end - vma->vm_start) >> PAGE_SHIFT;
164 unsigned long count = PAGE_ALIGN(size) >> PAGE_SHIFT;
165 unsigned long off = vma->vm_pgoff;
166 unsigned long pfn;
167
168 if (off >= count || user_count > (count - off))
169 return -ENXIO;
170
171#ifdef NOT_COHERENT_CACHE
172 vma->vm_page_prot = pgprot_noncached(vma->vm_page_prot);
173 pfn = consistent_virt_to_pfn(cpu_addr);
174#else
175 pfn = virt_to_pfn(cpu_addr);
176#endif
177 return remap_pfn_range(vma, vma->vm_start, pfn + off,
178 vma->vm_end - vma->vm_start, vma->vm_page_prot);
179#else
180 return -ENXIO;
181#endif
182}
183
Michal Simekccfe27d2010-01-14 11:21:02 +0100184struct dma_map_ops dma_direct_ops = {
Andrzej Pietrasiewicz988624e2012-03-27 14:56:04 +0200185 .alloc = dma_direct_alloc_coherent,
186 .free = dma_direct_free_coherent,
Lars-Peter Clausen3a8e3262014-12-03 16:07:28 +0100187 .mmap = dma_direct_mmap_coherent,
Michal Simekccfe27d2010-01-14 11:21:02 +0100188 .map_sg = dma_direct_map_sg,
Michal Simekccfe27d2010-01-14 11:21:02 +0100189 .dma_supported = dma_direct_dma_supported,
190 .map_page = dma_direct_map_page,
191 .unmap_page = dma_direct_unmap_page,
Eli Billauer0fb2a6f2011-09-11 22:43:07 +0300192 .sync_single_for_cpu = dma_direct_sync_single_for_cpu,
193 .sync_single_for_device = dma_direct_sync_single_for_device,
194 .sync_sg_for_cpu = dma_direct_sync_sg_for_cpu,
195 .sync_sg_for_device = dma_direct_sync_sg_for_device,
Michal Simekccfe27d2010-01-14 11:21:02 +0100196};
197EXPORT_SYMBOL(dma_direct_ops);
198
199/* Number of entries preallocated for DMA-API debugging */
200#define PREALLOC_DMA_DEBUG_ENTRIES (1 << 16)
201
202static int __init dma_init(void)
203{
Michal Simek6bd55f02012-12-27 10:40:38 +0100204 dma_debug_init(PREALLOC_DMA_DEBUG_ENTRIES);
Michal Simekccfe27d2010-01-14 11:21:02 +0100205
Michal Simek6bd55f02012-12-27 10:40:38 +0100206 return 0;
Michal Simekccfe27d2010-01-14 11:21:02 +0100207}
208fs_initcall(dma_init);