blob: 2d28ba939d8edc71c693442b4464076f840ea5ff [file] [log] [blame]
Vineet Gupta1162b072013-01-18 15:12:20 +05301/*
2 * DMA Mapping glue for ARC
3 *
4 * Copyright (C) 2004, 2007-2010, 2011-2012 Synopsys, Inc. (www.synopsys.com)
5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License version 2 as
8 * published by the Free Software Foundation.
9 */
10
11#ifndef ASM_ARC_DMA_MAPPING_H
12#define ASM_ARC_DMA_MAPPING_H
13
14#include <asm-generic/dma-coherent.h>
15#include <asm/cacheflush.h>
Vineet Guptafc7943d2013-01-22 16:53:57 +053016
Vineet Gupta1162b072013-01-18 15:12:20 +053017void *dma_alloc_noncoherent(struct device *dev, size_t size,
18 dma_addr_t *dma_handle, gfp_t gfp);
19
20void dma_free_noncoherent(struct device *dev, size_t size, void *vaddr,
21 dma_addr_t dma_handle);
22
23void *dma_alloc_coherent(struct device *dev, size_t size,
24 dma_addr_t *dma_handle, gfp_t gfp);
25
26void dma_free_coherent(struct device *dev, size_t size, void *kvaddr,
27 dma_addr_t dma_handle);
28
29/* drivers/base/dma-mapping.c */
30extern int dma_common_mmap(struct device *dev, struct vm_area_struct *vma,
31 void *cpu_addr, dma_addr_t dma_addr, size_t size);
32extern int dma_common_get_sgtable(struct device *dev, struct sg_table *sgt,
33 void *cpu_addr, dma_addr_t dma_addr,
34 size_t size);
35
36#define dma_mmap_coherent(d, v, c, h, s) dma_common_mmap(d, v, c, h, s)
37#define dma_get_sgtable(d, t, v, h, s) dma_common_get_sgtable(d, t, v, h, s)
38
39/*
40 * streaming DMA Mapping API...
41 * CPU accesses page via normal paddr, thus needs to explicitly made
42 * consistent before each use
43 */
44
45static inline void __inline_dma_cache_sync(unsigned long paddr, size_t size,
46 enum dma_data_direction dir)
47{
48 switch (dir) {
49 case DMA_FROM_DEVICE:
50 dma_cache_inv(paddr, size);
51 break;
52 case DMA_TO_DEVICE:
53 dma_cache_wback(paddr, size);
54 break;
55 case DMA_BIDIRECTIONAL:
56 dma_cache_wback_inv(paddr, size);
57 break;
58 default:
59 pr_err("Invalid DMA dir [%d] for OP @ %lx\n", dir, paddr);
60 }
61}
62
63void __arc_dma_cache_sync(unsigned long paddr, size_t size,
64 enum dma_data_direction dir);
65
66#define _dma_cache_sync(addr, sz, dir) \
67do { \
68 if (__builtin_constant_p(dir)) \
69 __inline_dma_cache_sync(addr, sz, dir); \
70 else \
71 __arc_dma_cache_sync(addr, sz, dir); \
72} \
73while (0);
74
75static inline dma_addr_t
76dma_map_single(struct device *dev, void *cpu_addr, size_t size,
77 enum dma_data_direction dir)
78{
79 _dma_cache_sync((unsigned long)cpu_addr, size, dir);
Vineet Gupta454bfda2015-04-16 21:04:49 +053080 return (dma_addr_t)cpu_addr;
Vineet Gupta1162b072013-01-18 15:12:20 +053081}
82
83static inline void
84dma_unmap_single(struct device *dev, dma_addr_t dma_addr,
85 size_t size, enum dma_data_direction dir)
86{
87}
88
89static inline dma_addr_t
90dma_map_page(struct device *dev, struct page *page,
91 unsigned long offset, size_t size,
92 enum dma_data_direction dir)
93{
94 unsigned long paddr = page_to_phys(page) + offset;
95 return dma_map_single(dev, (void *)paddr, size, dir);
96}
97
98static inline void
99dma_unmap_page(struct device *dev, dma_addr_t dma_handle,
100 size_t size, enum dma_data_direction dir)
101{
102}
103
104static inline int
105dma_map_sg(struct device *dev, struct scatterlist *sg,
106 int nents, enum dma_data_direction dir)
107{
108 struct scatterlist *s;
109 int i;
110
111 for_each_sg(sg, s, nents, i)
Pierrick Hascoet2105fd52013-03-18 17:04:45 +0100112 s->dma_address = dma_map_page(dev, sg_page(s), s->offset,
Vineet Gupta1162b072013-01-18 15:12:20 +0530113 s->length, dir);
114
115 return nents;
116}
117
118static inline void
119dma_unmap_sg(struct device *dev, struct scatterlist *sg,
120 int nents, enum dma_data_direction dir)
121{
122 struct scatterlist *s;
123 int i;
124
125 for_each_sg(sg, s, nents, i)
126 dma_unmap_page(dev, sg_dma_address(s), sg_dma_len(s), dir);
127}
128
129static inline void
130dma_sync_single_for_cpu(struct device *dev, dma_addr_t dma_handle,
131 size_t size, enum dma_data_direction dir)
132{
Vineet Gupta454bfda2015-04-16 21:04:49 +0530133 _dma_cache_sync(dma_handle, size, DMA_FROM_DEVICE);
Vineet Gupta1162b072013-01-18 15:12:20 +0530134}
135
136static inline void
137dma_sync_single_for_device(struct device *dev, dma_addr_t dma_handle,
138 size_t size, enum dma_data_direction dir)
139{
Vineet Gupta454bfda2015-04-16 21:04:49 +0530140 _dma_cache_sync(dma_handle, size, DMA_TO_DEVICE);
Vineet Gupta1162b072013-01-18 15:12:20 +0530141}
142
143static inline void
144dma_sync_single_range_for_cpu(struct device *dev, dma_addr_t dma_handle,
145 unsigned long offset, size_t size,
146 enum dma_data_direction direction)
147{
Vineet Gupta454bfda2015-04-16 21:04:49 +0530148 _dma_cache_sync(dma_handle + offset, size, DMA_FROM_DEVICE);
Vineet Gupta1162b072013-01-18 15:12:20 +0530149}
150
151static inline void
152dma_sync_single_range_for_device(struct device *dev, dma_addr_t dma_handle,
153 unsigned long offset, size_t size,
154 enum dma_data_direction direction)
155{
Vineet Gupta454bfda2015-04-16 21:04:49 +0530156 _dma_cache_sync(dma_handle + offset, size, DMA_TO_DEVICE);
Vineet Gupta1162b072013-01-18 15:12:20 +0530157}
158
159static inline void
Akinobu Mita92ed9322015-06-30 14:58:30 -0700160dma_sync_sg_for_cpu(struct device *dev, struct scatterlist *sglist, int nelems,
Vineet Gupta1162b072013-01-18 15:12:20 +0530161 enum dma_data_direction dir)
162{
163 int i;
Akinobu Mita92ed9322015-06-30 14:58:30 -0700164 struct scatterlist *sg;
Vineet Gupta1162b072013-01-18 15:12:20 +0530165
Akinobu Mita92ed9322015-06-30 14:58:30 -0700166 for_each_sg(sglist, sg, nelems, i)
Vineet Gupta1162b072013-01-18 15:12:20 +0530167 _dma_cache_sync((unsigned int)sg_virt(sg), sg->length, dir);
168}
169
170static inline void
Akinobu Mita92ed9322015-06-30 14:58:30 -0700171dma_sync_sg_for_device(struct device *dev, struct scatterlist *sglist,
172 int nelems, enum dma_data_direction dir)
Vineet Gupta1162b072013-01-18 15:12:20 +0530173{
174 int i;
Akinobu Mita92ed9322015-06-30 14:58:30 -0700175 struct scatterlist *sg;
Vineet Gupta1162b072013-01-18 15:12:20 +0530176
Akinobu Mita92ed9322015-06-30 14:58:30 -0700177 for_each_sg(sglist, sg, nelems, i)
Vineet Gupta1162b072013-01-18 15:12:20 +0530178 _dma_cache_sync((unsigned int)sg_virt(sg), sg->length, dir);
179}
180
181static inline int dma_supported(struct device *dev, u64 dma_mask)
182{
183 /* Support 32 bit DMA mask exclusively */
184 return dma_mask == DMA_BIT_MASK(32);
185}
186
187static inline int dma_mapping_error(struct device *dev, dma_addr_t dma_addr)
188{
189 return 0;
190}
191
192static inline int dma_set_mask(struct device *dev, u64 dma_mask)
193{
194 if (!dev->dma_mask || !dma_supported(dev, dma_mask))
195 return -EIO;
196
197 *dev->dma_mask = dma_mask;
198
199 return 0;
200}
201
202#endif