]>
Commit | Line | Data |
---|---|---|
244ab90e AL |
1 | /* |
2 | * DMA helper functions | |
3 | * | |
4 | * Copyright (c) 2009 Red Hat | |
5 | * | |
6 | * This work is licensed under the terms of the GNU General Public License | |
7 | * (GNU GPL), version 2 or later. | |
8 | */ | |
9 | ||
10 | #ifndef DMA_H | |
11 | #define DMA_H | |
12 | ||
13 | #include <stdio.h> | |
022c62cb | 14 | #include "exec/memory.h" |
df32fd1c | 15 | #include "exec/address-spaces.h" |
1ad2134f | 16 | #include "hw/hw.h" |
737e150e | 17 | #include "block/block.h" |
9c17d615 | 18 | #include "sysemu/kvm.h" |
244ab90e | 19 | |
10dc8aef PB |
20 | typedef struct ScatterGatherEntry ScatterGatherEntry; |
21 | ||
43cf8ae6 DG |
22 | typedef enum { |
23 | DMA_DIRECTION_TO_DEVICE = 0, | |
24 | DMA_DIRECTION_FROM_DEVICE = 1, | |
25 | } DMADirection; | |
26 | ||
fead0c24 PB |
27 | struct QEMUSGList { |
28 | ScatterGatherEntry *sg; | |
29 | int nsg; | |
30 | int nalloc; | |
31 | size_t size; | |
f487b677 | 32 | DeviceState *dev; |
df32fd1c | 33 | AddressSpace *as; |
fead0c24 PB |
34 | }; |
35 | ||
4be403c8 | 36 | #ifndef CONFIG_USER_ONLY |
d9d1055e | 37 | |
e5332e63 DG |
38 | /* |
39 | * When an IOMMU is present, bus addresses become distinct from | |
40 | * CPU/memory physical addresses and may be a different size. Because | |
41 | * the IOVA size depends more on the bus than on the platform, we more | |
42 | * or less have to treat these as 64-bit always to cover all (or at | |
43 | * least most) cases. | |
44 | */ | |
45 | typedef uint64_t dma_addr_t; | |
46 | ||
47 | #define DMA_ADDR_BITS 64 | |
48 | #define DMA_ADDR_FMT "%" PRIx64 | |
49 | ||
df32fd1c | 50 | static inline void dma_barrier(AddressSpace *as, DMADirection dir) |
7a0bac4d BH |
51 | { |
52 | /* | |
53 | * This is called before DMA read and write operations | |
54 | * unless the _relaxed form is used and is responsible | |
55 | * for providing some sane ordering of accesses vs | |
56 | * concurrently running VCPUs. | |
57 | * | |
58 | * Users of map(), unmap() or lower level st/ld_* | |
59 | * operations are responsible for providing their own | |
60 | * ordering via barriers. | |
61 | * | |
62 | * This primitive implementation does a simple smp_mb() | |
63 | * before each operation which provides pretty much full | |
64 | * ordering. | |
65 | * | |
66 | * A smarter implementation can be devised if needed to | |
67 | * use lighter barriers based on the direction of the | |
68 | * transfer, the DMA context, etc... | |
69 | */ | |
70 | if (kvm_enabled()) { | |
71 | smp_mb(); | |
72 | } | |
73 | } | |
74 | ||
d86a77f8 DG |
75 | /* Checks that the given range of addresses is valid for DMA. This is |
76 | * useful for certain cases, but usually you should just use | |
77 | * dma_memory_{read,write}() and check for errors */ | |
df32fd1c | 78 | static inline bool dma_memory_valid(AddressSpace *as, |
e5332e63 DG |
79 | dma_addr_t addr, dma_addr_t len, |
80 | DMADirection dir) | |
d86a77f8 | 81 | { |
df32fd1c | 82 | return address_space_access_valid(as, addr, len, |
24addbc7 | 83 | dir == DMA_DIRECTION_FROM_DEVICE); |
d86a77f8 DG |
84 | } |
85 | ||
df32fd1c | 86 | static inline int dma_memory_rw_relaxed(AddressSpace *as, dma_addr_t addr, |
7a0bac4d BH |
87 | void *buf, dma_addr_t len, |
88 | DMADirection dir) | |
d86a77f8 | 89 | { |
df32fd1c | 90 | return address_space_rw(as, addr, buf, len, dir == DMA_DIRECTION_FROM_DEVICE); |
d86a77f8 DG |
91 | } |
92 | ||
df32fd1c | 93 | static inline int dma_memory_read_relaxed(AddressSpace *as, dma_addr_t addr, |
7a0bac4d BH |
94 | void *buf, dma_addr_t len) |
95 | { | |
df32fd1c | 96 | return dma_memory_rw_relaxed(as, addr, buf, len, DMA_DIRECTION_TO_DEVICE); |
7a0bac4d BH |
97 | } |
98 | ||
df32fd1c | 99 | static inline int dma_memory_write_relaxed(AddressSpace *as, dma_addr_t addr, |
7a0bac4d BH |
100 | const void *buf, dma_addr_t len) |
101 | { | |
df32fd1c | 102 | return dma_memory_rw_relaxed(as, addr, (void *)buf, len, |
7a0bac4d BH |
103 | DMA_DIRECTION_FROM_DEVICE); |
104 | } | |
105 | ||
df32fd1c | 106 | static inline int dma_memory_rw(AddressSpace *as, dma_addr_t addr, |
7a0bac4d BH |
107 | void *buf, dma_addr_t len, |
108 | DMADirection dir) | |
109 | { | |
df32fd1c | 110 | dma_barrier(as, dir); |
7a0bac4d | 111 | |
df32fd1c | 112 | return dma_memory_rw_relaxed(as, addr, buf, len, dir); |
7a0bac4d BH |
113 | } |
114 | ||
df32fd1c | 115 | static inline int dma_memory_read(AddressSpace *as, dma_addr_t addr, |
d86a77f8 DG |
116 | void *buf, dma_addr_t len) |
117 | { | |
df32fd1c | 118 | return dma_memory_rw(as, addr, buf, len, DMA_DIRECTION_TO_DEVICE); |
d86a77f8 DG |
119 | } |
120 | ||
df32fd1c | 121 | static inline int dma_memory_write(AddressSpace *as, dma_addr_t addr, |
d86a77f8 DG |
122 | const void *buf, dma_addr_t len) |
123 | { | |
df32fd1c | 124 | return dma_memory_rw(as, addr, (void *)buf, len, |
d86a77f8 DG |
125 | DMA_DIRECTION_FROM_DEVICE); |
126 | } | |
127 | ||
df32fd1c | 128 | int dma_memory_set(AddressSpace *as, dma_addr_t addr, uint8_t c, dma_addr_t len); |
d86a77f8 | 129 | |
df32fd1c | 130 | static inline void *dma_memory_map(AddressSpace *as, |
d86a77f8 DG |
131 | dma_addr_t addr, dma_addr_t *len, |
132 | DMADirection dir) | |
133 | { | |
24addbc7 PB |
134 | hwaddr xlen = *len; |
135 | void *p; | |
136 | ||
df32fd1c | 137 | p = address_space_map(as, addr, &xlen, dir == DMA_DIRECTION_FROM_DEVICE); |
24addbc7 PB |
138 | *len = xlen; |
139 | return p; | |
d86a77f8 DG |
140 | } |
141 | ||
df32fd1c | 142 | static inline void dma_memory_unmap(AddressSpace *as, |
d86a77f8 DG |
143 | void *buffer, dma_addr_t len, |
144 | DMADirection dir, dma_addr_t access_len) | |
145 | { | |
df32fd1c | 146 | address_space_unmap(as, buffer, (hwaddr)len, |
24addbc7 | 147 | dir == DMA_DIRECTION_FROM_DEVICE, access_len); |
d86a77f8 DG |
148 | } |
149 | ||
150 | #define DEFINE_LDST_DMA(_lname, _sname, _bits, _end) \ | |
df32fd1c | 151 | static inline uint##_bits##_t ld##_lname##_##_end##_dma(AddressSpace *as, \ |
d86a77f8 DG |
152 | dma_addr_t addr) \ |
153 | { \ | |
154 | uint##_bits##_t val; \ | |
df32fd1c | 155 | dma_memory_read(as, addr, &val, (_bits) / 8); \ |
d86a77f8 DG |
156 | return _end##_bits##_to_cpu(val); \ |
157 | } \ | |
df32fd1c | 158 | static inline void st##_sname##_##_end##_dma(AddressSpace *as, \ |
d86a77f8 DG |
159 | dma_addr_t addr, \ |
160 | uint##_bits##_t val) \ | |
161 | { \ | |
162 | val = cpu_to_##_end##_bits(val); \ | |
df32fd1c | 163 | dma_memory_write(as, addr, &val, (_bits) / 8); \ |
d86a77f8 DG |
164 | } |
165 | ||
df32fd1c | 166 | static inline uint8_t ldub_dma(AddressSpace *as, dma_addr_t addr) |
d86a77f8 DG |
167 | { |
168 | uint8_t val; | |
169 | ||
df32fd1c | 170 | dma_memory_read(as, addr, &val, 1); |
d86a77f8 DG |
171 | return val; |
172 | } | |
173 | ||
df32fd1c | 174 | static inline void stb_dma(AddressSpace *as, dma_addr_t addr, uint8_t val) |
d86a77f8 | 175 | { |
df32fd1c | 176 | dma_memory_write(as, addr, &val, 1); |
d86a77f8 DG |
177 | } |
178 | ||
179 | DEFINE_LDST_DMA(uw, w, 16, le); | |
180 | DEFINE_LDST_DMA(l, l, 32, le); | |
181 | DEFINE_LDST_DMA(q, q, 64, le); | |
182 | DEFINE_LDST_DMA(uw, w, 16, be); | |
183 | DEFINE_LDST_DMA(l, l, 32, be); | |
184 | DEFINE_LDST_DMA(q, q, 64, be); | |
185 | ||
186 | #undef DEFINE_LDST_DMA | |
187 | ||
10dc8aef | 188 | struct ScatterGatherEntry { |
d3231181 DG |
189 | dma_addr_t base; |
190 | dma_addr_t len; | |
10dc8aef | 191 | }; |
244ab90e | 192 | |
f487b677 PB |
193 | void qemu_sglist_init(QEMUSGList *qsg, DeviceState *dev, int alloc_hint, |
194 | AddressSpace *as); | |
d3231181 | 195 | void qemu_sglist_add(QEMUSGList *qsg, dma_addr_t base, dma_addr_t len); |
244ab90e | 196 | void qemu_sglist_destroy(QEMUSGList *qsg); |
10dc8aef | 197 | #endif |
244ab90e | 198 | |
cb144ccb CH |
199 | typedef BlockDriverAIOCB *DMAIOFunc(BlockDriverState *bs, int64_t sector_num, |
200 | QEMUIOVector *iov, int nb_sectors, | |
201 | BlockDriverCompletionFunc *cb, void *opaque); | |
202 | ||
203 | BlockDriverAIOCB *dma_bdrv_io(BlockDriverState *bs, | |
204 | QEMUSGList *sg, uint64_t sector_num, | |
205 | DMAIOFunc *io_func, BlockDriverCompletionFunc *cb, | |
43cf8ae6 | 206 | void *opaque, DMADirection dir); |
59a703eb AL |
207 | BlockDriverAIOCB *dma_bdrv_read(BlockDriverState *bs, |
208 | QEMUSGList *sg, uint64_t sector, | |
209 | BlockDriverCompletionFunc *cb, void *opaque); | |
210 | BlockDriverAIOCB *dma_bdrv_write(BlockDriverState *bs, | |
211 | QEMUSGList *sg, uint64_t sector, | |
212 | BlockDriverCompletionFunc *cb, void *opaque); | |
8171ee35 PB |
213 | uint64_t dma_buf_read(uint8_t *ptr, int32_t len, QEMUSGList *sg); |
214 | uint64_t dma_buf_write(uint8_t *ptr, int32_t len, QEMUSGList *sg); | |
215 | ||
84a69356 PB |
216 | void dma_acct_start(BlockDriverState *bs, BlockAcctCookie *cookie, |
217 | QEMUSGList *sg, enum BlockAcctType type); | |
218 | ||
244ab90e | 219 | #endif |