block: Rename BlockDriverAIOCB* to BlockAIOCB*
[qemu/armbru.git] / include / sysemu / dma.h
blob1bc22234f1b2bab89dcfe7a514aeeef3dbb3fd03
1 /*
2 * DMA helper functions
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.
8 */
10 #ifndef DMA_H
11 #define DMA_H
13 #include <stdio.h>
14 #include "exec/memory.h"
15 #include "exec/address-spaces.h"
16 #include "hw/hw.h"
17 #include "block/block.h"
18 #include "block/accounting.h"
19 #include "sysemu/kvm.h"
21 typedef struct ScatterGatherEntry ScatterGatherEntry;
23 typedef enum {
24 DMA_DIRECTION_TO_DEVICE = 0,
25 DMA_DIRECTION_FROM_DEVICE = 1,
26 } DMADirection;
28 struct QEMUSGList {
29 ScatterGatherEntry *sg;
30 int nsg;
31 int nalloc;
32 size_t size;
33 DeviceState *dev;
34 AddressSpace *as;
37 #ifndef CONFIG_USER_ONLY
40 * When an IOMMU is present, bus addresses become distinct from
41 * CPU/memory physical addresses and may be a different size. Because
42 * the IOVA size depends more on the bus than on the platform, we more
43 * or less have to treat these as 64-bit always to cover all (or at
44 * least most) cases.
46 typedef uint64_t dma_addr_t;
48 #define DMA_ADDR_BITS 64
49 #define DMA_ADDR_FMT "%" PRIx64
51 static inline void dma_barrier(AddressSpace *as, DMADirection dir)
54 * This is called before DMA read and write operations
55 * unless the _relaxed form is used and is responsible
56 * for providing some sane ordering of accesses vs
57 * concurrently running VCPUs.
59 * Users of map(), unmap() or lower level st/ld_*
60 * operations are responsible for providing their own
61 * ordering via barriers.
63 * This primitive implementation does a simple smp_mb()
64 * before each operation which provides pretty much full
65 * ordering.
67 * A smarter implementation can be devised if needed to
68 * use lighter barriers based on the direction of the
69 * transfer, the DMA context, etc...
71 if (kvm_enabled()) {
72 smp_mb();
76 /* Checks that the given range of addresses is valid for DMA. This is
77 * useful for certain cases, but usually you should just use
78 * dma_memory_{read,write}() and check for errors */
79 static inline bool dma_memory_valid(AddressSpace *as,
80 dma_addr_t addr, dma_addr_t len,
81 DMADirection dir)
83 return address_space_access_valid(as, addr, len,
84 dir == DMA_DIRECTION_FROM_DEVICE);
87 static inline int dma_memory_rw_relaxed(AddressSpace *as, dma_addr_t addr,
88 void *buf, dma_addr_t len,
89 DMADirection dir)
91 return address_space_rw(as, addr, buf, len, dir == DMA_DIRECTION_FROM_DEVICE);
94 static inline int dma_memory_read_relaxed(AddressSpace *as, dma_addr_t addr,
95 void *buf, dma_addr_t len)
97 return dma_memory_rw_relaxed(as, addr, buf, len, DMA_DIRECTION_TO_DEVICE);
100 static inline int dma_memory_write_relaxed(AddressSpace *as, dma_addr_t addr,
101 const void *buf, dma_addr_t len)
103 return dma_memory_rw_relaxed(as, addr, (void *)buf, len,
104 DMA_DIRECTION_FROM_DEVICE);
107 static inline int dma_memory_rw(AddressSpace *as, dma_addr_t addr,
108 void *buf, dma_addr_t len,
109 DMADirection dir)
111 dma_barrier(as, dir);
113 return dma_memory_rw_relaxed(as, addr, buf, len, dir);
116 static inline int dma_memory_read(AddressSpace *as, dma_addr_t addr,
117 void *buf, dma_addr_t len)
119 return dma_memory_rw(as, addr, buf, len, DMA_DIRECTION_TO_DEVICE);
122 static inline int dma_memory_write(AddressSpace *as, dma_addr_t addr,
123 const void *buf, dma_addr_t len)
125 return dma_memory_rw(as, addr, (void *)buf, len,
126 DMA_DIRECTION_FROM_DEVICE);
129 int dma_memory_set(AddressSpace *as, dma_addr_t addr, uint8_t c, dma_addr_t len);
131 static inline void *dma_memory_map(AddressSpace *as,
132 dma_addr_t addr, dma_addr_t *len,
133 DMADirection dir)
135 hwaddr xlen = *len;
136 void *p;
138 p = address_space_map(as, addr, &xlen, dir == DMA_DIRECTION_FROM_DEVICE);
139 *len = xlen;
140 return p;
143 static inline void dma_memory_unmap(AddressSpace *as,
144 void *buffer, dma_addr_t len,
145 DMADirection dir, dma_addr_t access_len)
147 address_space_unmap(as, buffer, (hwaddr)len,
148 dir == DMA_DIRECTION_FROM_DEVICE, access_len);
151 #define DEFINE_LDST_DMA(_lname, _sname, _bits, _end) \
152 static inline uint##_bits##_t ld##_lname##_##_end##_dma(AddressSpace *as, \
153 dma_addr_t addr) \
155 uint##_bits##_t val; \
156 dma_memory_read(as, addr, &val, (_bits) / 8); \
157 return _end##_bits##_to_cpu(val); \
159 static inline void st##_sname##_##_end##_dma(AddressSpace *as, \
160 dma_addr_t addr, \
161 uint##_bits##_t val) \
163 val = cpu_to_##_end##_bits(val); \
164 dma_memory_write(as, addr, &val, (_bits) / 8); \
167 static inline uint8_t ldub_dma(AddressSpace *as, dma_addr_t addr)
169 uint8_t val;
171 dma_memory_read(as, addr, &val, 1);
172 return val;
175 static inline void stb_dma(AddressSpace *as, dma_addr_t addr, uint8_t val)
177 dma_memory_write(as, addr, &val, 1);
180 DEFINE_LDST_DMA(uw, w, 16, le);
181 DEFINE_LDST_DMA(l, l, 32, le);
182 DEFINE_LDST_DMA(q, q, 64, le);
183 DEFINE_LDST_DMA(uw, w, 16, be);
184 DEFINE_LDST_DMA(l, l, 32, be);
185 DEFINE_LDST_DMA(q, q, 64, be);
187 #undef DEFINE_LDST_DMA
189 struct ScatterGatherEntry {
190 dma_addr_t base;
191 dma_addr_t len;
194 void qemu_sglist_init(QEMUSGList *qsg, DeviceState *dev, int alloc_hint,
195 AddressSpace *as);
196 void qemu_sglist_add(QEMUSGList *qsg, dma_addr_t base, dma_addr_t len);
197 void qemu_sglist_destroy(QEMUSGList *qsg);
198 #endif
200 typedef BlockAIOCB *DMAIOFunc(BlockDriverState *bs, int64_t sector_num,
201 QEMUIOVector *iov, int nb_sectors,
202 BlockDriverCompletionFunc *cb, void *opaque);
204 BlockAIOCB *dma_bdrv_io(BlockDriverState *bs,
205 QEMUSGList *sg, uint64_t sector_num,
206 DMAIOFunc *io_func, BlockDriverCompletionFunc *cb,
207 void *opaque, DMADirection dir);
208 BlockAIOCB *dma_bdrv_read(BlockDriverState *bs,
209 QEMUSGList *sg, uint64_t sector,
210 BlockDriverCompletionFunc *cb, void *opaque);
211 BlockAIOCB *dma_bdrv_write(BlockDriverState *bs,
212 QEMUSGList *sg, uint64_t sector,
213 BlockDriverCompletionFunc *cb, void *opaque);
214 uint64_t dma_buf_read(uint8_t *ptr, int32_t len, QEMUSGList *sg);
215 uint64_t dma_buf_write(uint8_t *ptr, int32_t len, QEMUSGList *sg);
217 void dma_acct_start(BlockDriverState *bs, BlockAcctCookie *cookie,
218 QEMUSGList *sg, enum BlockAcctType type);
220 #endif