4 * Copyright (c) 2009 Red Hat
6 * This work is licensed under the terms of the GNU General Public License
7 * (GNU GPL), version 2 or later.
14 #include "exec/memory.h"
16 #include "block/block.h"
17 #include "sysemu/kvm.h"
19 typedef struct DMAContext DMAContext
;
20 typedef struct ScatterGatherEntry ScatterGatherEntry
;
23 DMA_DIRECTION_TO_DEVICE
= 0,
24 DMA_DIRECTION_FROM_DEVICE
= 1,
28 ScatterGatherEntry
*sg
;
35 #ifndef CONFIG_USER_ONLY
38 * When an IOMMU is present, bus addresses become distinct from
39 * CPU/memory physical addresses and may be a different size. Because
40 * the IOVA size depends more on the bus than on the platform, we more
41 * or less have to treat these as 64-bit always to cover all (or at
44 typedef uint64_t dma_addr_t
;
46 #define DMA_ADDR_BITS 64
47 #define DMA_ADDR_FMT "%" PRIx64
49 typedef int DMATranslateFunc(DMAContext
*dma
,
54 typedef void* DMAMapFunc(DMAContext
*dma
,
58 typedef void DMAUnmapFunc(DMAContext
*dma
,
62 dma_addr_t access_len
);
66 DMATranslateFunc
*translate
;
71 /* A global DMA context corresponding to the address_space_memory
72 * AddressSpace, for sysbus devices which do DMA.
74 extern DMAContext dma_context_memory
;
76 static inline void dma_barrier(DMAContext
*dma
, DMADirection dir
)
79 * This is called before DMA read and write operations
80 * unless the _relaxed form is used and is responsible
81 * for providing some sane ordering of accesses vs
82 * concurrently running VCPUs.
84 * Users of map(), unmap() or lower level st/ld_*
85 * operations are responsible for providing their own
86 * ordering via barriers.
88 * This primitive implementation does a simple smp_mb()
89 * before each operation which provides pretty much full
92 * A smarter implementation can be devised if needed to
93 * use lighter barriers based on the direction of the
94 * transfer, the DMA context, etc...
101 static inline bool dma_has_iommu(DMAContext
*dma
)
103 return dma
&& dma
->translate
;
106 /* Checks that the given range of addresses is valid for DMA. This is
107 * useful for certain cases, but usually you should just use
108 * dma_memory_{read,write}() and check for errors */
109 bool iommu_dma_memory_valid(DMAContext
*dma
, dma_addr_t addr
, dma_addr_t len
,
111 static inline bool dma_memory_valid(DMAContext
*dma
,
112 dma_addr_t addr
, dma_addr_t len
,
115 if (!dma_has_iommu(dma
)) {
118 return iommu_dma_memory_valid(dma
, addr
, len
, dir
);
122 int iommu_dma_memory_rw(DMAContext
*dma
, dma_addr_t addr
,
123 void *buf
, dma_addr_t len
, DMADirection dir
);
124 static inline int dma_memory_rw_relaxed(DMAContext
*dma
, dma_addr_t addr
,
125 void *buf
, dma_addr_t len
,
128 if (!dma_has_iommu(dma
)) {
129 /* Fast-path for no IOMMU */
130 address_space_rw(dma
->as
, addr
, buf
, len
, dir
== DMA_DIRECTION_FROM_DEVICE
);
133 return iommu_dma_memory_rw(dma
, addr
, buf
, len
, dir
);
137 static inline int dma_memory_read_relaxed(DMAContext
*dma
, dma_addr_t addr
,
138 void *buf
, dma_addr_t len
)
140 return dma_memory_rw_relaxed(dma
, addr
, buf
, len
, DMA_DIRECTION_TO_DEVICE
);
143 static inline int dma_memory_write_relaxed(DMAContext
*dma
, dma_addr_t addr
,
144 const void *buf
, dma_addr_t len
)
146 return dma_memory_rw_relaxed(dma
, addr
, (void *)buf
, len
,
147 DMA_DIRECTION_FROM_DEVICE
);
150 static inline int dma_memory_rw(DMAContext
*dma
, dma_addr_t addr
,
151 void *buf
, dma_addr_t len
,
154 dma_barrier(dma
, dir
);
156 return dma_memory_rw_relaxed(dma
, addr
, buf
, len
, dir
);
159 static inline int dma_memory_read(DMAContext
*dma
, dma_addr_t addr
,
160 void *buf
, dma_addr_t len
)
162 return dma_memory_rw(dma
, addr
, buf
, len
, DMA_DIRECTION_TO_DEVICE
);
165 static inline int dma_memory_write(DMAContext
*dma
, dma_addr_t addr
,
166 const void *buf
, dma_addr_t len
)
168 return dma_memory_rw(dma
, addr
, (void *)buf
, len
,
169 DMA_DIRECTION_FROM_DEVICE
);
172 int iommu_dma_memory_set(DMAContext
*dma
, dma_addr_t addr
, uint8_t c
,
175 int dma_memory_set(DMAContext
*dma
, dma_addr_t addr
, uint8_t c
, dma_addr_t len
);
177 void *iommu_dma_memory_map(DMAContext
*dma
,
178 dma_addr_t addr
, dma_addr_t
*len
,
180 static inline void *dma_memory_map(DMAContext
*dma
,
181 dma_addr_t addr
, dma_addr_t
*len
,
184 if (!dma_has_iommu(dma
)) {
188 p
= address_space_map(dma
->as
, addr
, &xlen
, dir
== DMA_DIRECTION_FROM_DEVICE
);
192 return iommu_dma_memory_map(dma
, addr
, len
, dir
);
196 void iommu_dma_memory_unmap(DMAContext
*dma
,
197 void *buffer
, dma_addr_t len
,
198 DMADirection dir
, dma_addr_t access_len
);
199 static inline void dma_memory_unmap(DMAContext
*dma
,
200 void *buffer
, dma_addr_t len
,
201 DMADirection dir
, dma_addr_t access_len
)
203 if (!dma_has_iommu(dma
)) {
204 address_space_unmap(dma
->as
, buffer
, (hwaddr
)len
,
205 dir
== DMA_DIRECTION_FROM_DEVICE
, access_len
);
207 iommu_dma_memory_unmap(dma
, buffer
, len
, dir
, access_len
);
211 #define DEFINE_LDST_DMA(_lname, _sname, _bits, _end) \
212 static inline uint##_bits##_t ld##_lname##_##_end##_dma(DMAContext *dma, \
215 uint##_bits##_t val; \
216 dma_memory_read(dma, addr, &val, (_bits) / 8); \
217 return _end##_bits##_to_cpu(val); \
219 static inline void st##_sname##_##_end##_dma(DMAContext *dma, \
221 uint##_bits##_t val) \
223 val = cpu_to_##_end##_bits(val); \
224 dma_memory_write(dma, addr, &val, (_bits) / 8); \
227 static inline uint8_t ldub_dma(DMAContext
*dma
, dma_addr_t addr
)
231 dma_memory_read(dma
, addr
, &val
, 1);
235 static inline void stb_dma(DMAContext
*dma
, dma_addr_t addr
, uint8_t val
)
237 dma_memory_write(dma
, addr
, &val
, 1);
240 DEFINE_LDST_DMA(uw
, w
, 16, le
);
241 DEFINE_LDST_DMA(l
, l
, 32, le
);
242 DEFINE_LDST_DMA(q
, q
, 64, le
);
243 DEFINE_LDST_DMA(uw
, w
, 16, be
);
244 DEFINE_LDST_DMA(l
, l
, 32, be
);
245 DEFINE_LDST_DMA(q
, q
, 64, be
);
247 #undef DEFINE_LDST_DMA
249 void dma_context_init(DMAContext
*dma
, AddressSpace
*as
, DMATranslateFunc translate
,
250 DMAMapFunc map
, DMAUnmapFunc unmap
);
252 struct ScatterGatherEntry
{
257 void qemu_sglist_init(QEMUSGList
*qsg
, int alloc_hint
, DMAContext
*dma
);
258 void qemu_sglist_add(QEMUSGList
*qsg
, dma_addr_t base
, dma_addr_t len
);
259 void qemu_sglist_destroy(QEMUSGList
*qsg
);
262 typedef BlockDriverAIOCB
*DMAIOFunc(BlockDriverState
*bs
, int64_t sector_num
,
263 QEMUIOVector
*iov
, int nb_sectors
,
264 BlockDriverCompletionFunc
*cb
, void *opaque
);
266 BlockDriverAIOCB
*dma_bdrv_io(BlockDriverState
*bs
,
267 QEMUSGList
*sg
, uint64_t sector_num
,
268 DMAIOFunc
*io_func
, BlockDriverCompletionFunc
*cb
,
269 void *opaque
, DMADirection dir
);
270 BlockDriverAIOCB
*dma_bdrv_read(BlockDriverState
*bs
,
271 QEMUSGList
*sg
, uint64_t sector
,
272 BlockDriverCompletionFunc
*cb
, void *opaque
);
273 BlockDriverAIOCB
*dma_bdrv_write(BlockDriverState
*bs
,
274 QEMUSGList
*sg
, uint64_t sector
,
275 BlockDriverCompletionFunc
*cb
, void *opaque
);
276 uint64_t dma_buf_read(uint8_t *ptr
, int32_t len
, QEMUSGList
*sg
);
277 uint64_t dma_buf_write(uint8_t *ptr
, int32_t len
, QEMUSGList
*sg
);
279 void dma_acct_start(BlockDriverState
*bs
, BlockAcctCookie
*cookie
,
280 QEMUSGList
*sg
, enum BlockAcctType type
);