]> git.proxmox.com Git - mirror_qemu.git/blame - softmmu/dma-helpers.c
Merge remote-tracking branch 'remotes/hdeller/tags/hppa-updates-pull-request' into...
[mirror_qemu.git] / softmmu / dma-helpers.c
CommitLineData
244ab90e
AL
1/*
2 * DMA helper functions
3 *
bb755f52 4 * Copyright (c) 2009,2020 Red Hat
244ab90e
AL
5 *
6 * This work is licensed under the terms of the GNU General Public License
7 * (GNU GPL), version 2 or later.
8 */
9
d38ea87a 10#include "qemu/osdep.h"
4be74634 11#include "sysemu/block-backend.h"
9c17d615 12#include "sysemu/dma.h"
243af022 13#include "trace/trace-root.h"
1de7afc9 14#include "qemu/thread.h"
6a1751b7 15#include "qemu/main-loop.h"
740b1759 16#include "sysemu/cpu-timers.h"
5fb0a6b5 17#include "qemu/range.h"
244ab90e 18
e5332e63
DG
19/* #define DEBUG_IOMMU */
20
bb755f52 21MemTxResult dma_memory_set(AddressSpace *as, dma_addr_t addr,
7a36e42d 22 uint8_t c, dma_addr_t len, MemTxAttrs attrs)
d86a77f8 23{
df32fd1c 24 dma_barrier(as, DMA_DIRECTION_FROM_DEVICE);
24addbc7 25
75f01c68 26 return address_space_set(as, addr, c, len, attrs);
d86a77f8
DG
27}
28
f487b677
PB
29void qemu_sglist_init(QEMUSGList *qsg, DeviceState *dev, int alloc_hint,
30 AddressSpace *as)
244ab90e 31{
7267c094 32 qsg->sg = g_malloc(alloc_hint * sizeof(ScatterGatherEntry));
244ab90e
AL
33 qsg->nsg = 0;
34 qsg->nalloc = alloc_hint;
35 qsg->size = 0;
df32fd1c 36 qsg->as = as;
f487b677
PB
37 qsg->dev = dev;
38 object_ref(OBJECT(dev));
244ab90e
AL
39}
40
d3231181 41void qemu_sglist_add(QEMUSGList *qsg, dma_addr_t base, dma_addr_t len)
244ab90e
AL
42{
43 if (qsg->nsg == qsg->nalloc) {
44 qsg->nalloc = 2 * qsg->nalloc + 1;
7267c094 45 qsg->sg = g_realloc(qsg->sg, qsg->nalloc * sizeof(ScatterGatherEntry));
244ab90e
AL
46 }
47 qsg->sg[qsg->nsg].base = base;
48 qsg->sg[qsg->nsg].len = len;
49 qsg->size += len;
50 ++qsg->nsg;
51}
52
53void qemu_sglist_destroy(QEMUSGList *qsg)
54{
f487b677 55 object_unref(OBJECT(qsg->dev));
7267c094 56 g_free(qsg->sg);
ea8d82a1 57 memset(qsg, 0, sizeof(*qsg));
244ab90e
AL
58}
59
59a703eb 60typedef struct {
7c84b1b8 61 BlockAIOCB common;
8a8e63eb 62 AioContext *ctx;
7c84b1b8 63 BlockAIOCB *acb;
59a703eb 64 QEMUSGList *sg;
99868af3 65 uint32_t align;
d4f510eb 66 uint64_t offset;
43cf8ae6 67 DMADirection dir;
59a703eb 68 int sg_cur_index;
d3231181 69 dma_addr_t sg_cur_byte;
59a703eb
AL
70 QEMUIOVector iov;
71 QEMUBH *bh;
cb144ccb 72 DMAIOFunc *io_func;
8a8e63eb 73 void *io_func_opaque;
37b7842c 74} DMAAIOCB;
59a703eb 75
4be74634 76static void dma_blk_cb(void *opaque, int ret);
59a703eb
AL
77
78static void reschedule_dma(void *opaque)
79{
37b7842c 80 DMAAIOCB *dbs = (DMAAIOCB *)opaque;
59a703eb 81
539343c0 82 assert(!dbs->acb && dbs->bh);
59a703eb
AL
83 qemu_bh_delete(dbs->bh);
84 dbs->bh = NULL;
4be74634 85 dma_blk_cb(dbs, 0);
59a703eb
AL
86}
87
4be74634 88static void dma_blk_unmap(DMAAIOCB *dbs)
59a703eb 89{
59a703eb
AL
90 int i;
91
59a703eb 92 for (i = 0; i < dbs->iov.niov; ++i) {
df32fd1c 93 dma_memory_unmap(dbs->sg->as, dbs->iov.iov[i].iov_base,
c65bcef3
DG
94 dbs->iov.iov[i].iov_len, dbs->dir,
95 dbs->iov.iov[i].iov_len);
59a703eb 96 }
c3adb5b9
PB
97 qemu_iovec_reset(&dbs->iov);
98}
99
100static void dma_complete(DMAAIOCB *dbs, int ret)
101{
c57c4658
KW
102 trace_dma_complete(dbs, ret, dbs->common.cb);
103
539343c0 104 assert(!dbs->acb && !dbs->bh);
4be74634 105 dma_blk_unmap(dbs);
c3adb5b9
PB
106 if (dbs->common.cb) {
107 dbs->common.cb(dbs->common.opaque, ret);
108 }
109 qemu_iovec_destroy(&dbs->iov);
8007429a 110 qemu_aio_unref(dbs);
7403b14e
AL
111}
112
4be74634 113static void dma_blk_cb(void *opaque, int ret)
7403b14e
AL
114{
115 DMAAIOCB *dbs = (DMAAIOCB *)opaque;
c65bcef3 116 dma_addr_t cur_addr, cur_len;
7403b14e
AL
117 void *mem;
118
4be74634 119 trace_dma_blk_cb(dbs, ret);
c57c4658 120
7403b14e 121 dbs->acb = NULL;
d4f510eb 122 dbs->offset += dbs->iov.size;
59a703eb
AL
123
124 if (dbs->sg_cur_index == dbs->sg->nsg || ret < 0) {
c3adb5b9 125 dma_complete(dbs, ret);
59a703eb
AL
126 return;
127 }
4be74634 128 dma_blk_unmap(dbs);
59a703eb
AL
129
130 while (dbs->sg_cur_index < dbs->sg->nsg) {
131 cur_addr = dbs->sg->sg[dbs->sg_cur_index].base + dbs->sg_cur_byte;
132 cur_len = dbs->sg->sg[dbs->sg_cur_index].len - dbs->sg_cur_byte;
a1d4b0a3
PMD
133 mem = dma_memory_map(dbs->sg->as, cur_addr, &cur_len, dbs->dir,
134 MEMTXATTRS_UNSPECIFIED);
5fb0a6b5
PD
135 /*
136 * Make reads deterministic in icount mode. Windows sometimes issues
137 * disk read requests with overlapping SGs. It leads
138 * to non-determinism, because resulting buffer contents may be mixed
139 * from several sectors. This code splits all SGs into several
140 * groups. SGs in every group do not overlap.
141 */
740b1759 142 if (mem && icount_enabled() && dbs->dir == DMA_DIRECTION_FROM_DEVICE) {
5fb0a6b5
PD
143 int i;
144 for (i = 0 ; i < dbs->iov.niov ; ++i) {
145 if (ranges_overlap((intptr_t)dbs->iov.iov[i].iov_base,
146 dbs->iov.iov[i].iov_len, (intptr_t)mem,
147 cur_len)) {
148 dma_memory_unmap(dbs->sg->as, mem, cur_len,
149 dbs->dir, cur_len);
150 mem = NULL;
151 break;
152 }
153 }
154 }
59a703eb
AL
155 if (!mem)
156 break;
157 qemu_iovec_add(&dbs->iov, mem, cur_len);
158 dbs->sg_cur_byte += cur_len;
159 if (dbs->sg_cur_byte == dbs->sg->sg[dbs->sg_cur_index].len) {
160 dbs->sg_cur_byte = 0;
161 ++dbs->sg_cur_index;
162 }
163 }
164
165 if (dbs->iov.size == 0) {
c57c4658 166 trace_dma_map_wait(dbs);
8a8e63eb 167 dbs->bh = aio_bh_new(dbs->ctx, reschedule_dma, dbs);
e95205e1 168 cpu_register_map_client(dbs->bh);
59a703eb
AL
169 return;
170 }
171
99868af3
MCA
172 if (!QEMU_IS_ALIGNED(dbs->iov.size, dbs->align)) {
173 qemu_iovec_discard_back(&dbs->iov,
174 QEMU_ALIGN_DOWN(dbs->iov.size, dbs->align));
58f423fb
KW
175 }
176
1919631e 177 aio_context_acquire(dbs->ctx);
8a8e63eb
PB
178 dbs->acb = dbs->io_func(dbs->offset, &dbs->iov,
179 dma_blk_cb, dbs, dbs->io_func_opaque);
1919631e 180 aio_context_release(dbs->ctx);
6bee44ea 181 assert(dbs->acb);
59a703eb
AL
182}
183
7c84b1b8 184static void dma_aio_cancel(BlockAIOCB *acb)
c16b5a2c
CH
185{
186 DMAAIOCB *dbs = container_of(acb, DMAAIOCB, common);
187
c57c4658
KW
188 trace_dma_aio_cancel(dbs);
189
539343c0 190 assert(!(dbs->acb && dbs->bh));
c16b5a2c 191 if (dbs->acb) {
539343c0 192 /* This will invoke dma_blk_cb. */
4be74634 193 blk_aio_cancel_async(dbs->acb);
539343c0 194 return;
c16b5a2c 195 }
539343c0 196
e95205e1
FZ
197 if (dbs->bh) {
198 cpu_unregister_map_client(dbs->bh);
199 qemu_bh_delete(dbs->bh);
200 dbs->bh = NULL;
201 }
539343c0
PB
202 if (dbs->common.cb) {
203 dbs->common.cb(dbs->common.opaque, -ECANCELED);
204 }
c16b5a2c
CH
205}
206
5fa78b2a
SH
207static AioContext *dma_get_aio_context(BlockAIOCB *acb)
208{
209 DMAAIOCB *dbs = container_of(acb, DMAAIOCB, common);
210
211 return dbs->ctx;
212}
9bb9da46 213
d7331bed 214static const AIOCBInfo dma_aiocb_info = {
c16b5a2c 215 .aiocb_size = sizeof(DMAAIOCB),
9bb9da46 216 .cancel_async = dma_aio_cancel,
5fa78b2a 217 .get_aio_context = dma_get_aio_context,
c16b5a2c
CH
218};
219
8a8e63eb 220BlockAIOCB *dma_blk_io(AioContext *ctx,
99868af3 221 QEMUSGList *sg, uint64_t offset, uint32_t align,
8a8e63eb
PB
222 DMAIOFunc *io_func, void *io_func_opaque,
223 BlockCompletionFunc *cb,
43cf8ae6 224 void *opaque, DMADirection dir)
59a703eb 225{
8a8e63eb 226 DMAAIOCB *dbs = qemu_aio_get(&dma_aiocb_info, NULL, cb, opaque);
59a703eb 227
8a8e63eb 228 trace_dma_blk_io(dbs, io_func_opaque, offset, (dir == DMA_DIRECTION_TO_DEVICE));
c57c4658 229
37b7842c 230 dbs->acb = NULL;
59a703eb 231 dbs->sg = sg;
8a8e63eb 232 dbs->ctx = ctx;
cbe0ed62 233 dbs->offset = offset;
99868af3 234 dbs->align = align;
59a703eb
AL
235 dbs->sg_cur_index = 0;
236 dbs->sg_cur_byte = 0;
43cf8ae6 237 dbs->dir = dir;
cb144ccb 238 dbs->io_func = io_func;
8a8e63eb 239 dbs->io_func_opaque = io_func_opaque;
59a703eb
AL
240 dbs->bh = NULL;
241 qemu_iovec_init(&dbs->iov, sg->nsg);
4be74634 242 dma_blk_cb(dbs, 0);
37b7842c 243 return &dbs->common;
59a703eb
AL
244}
245
246
8a8e63eb
PB
247static
248BlockAIOCB *dma_blk_read_io_func(int64_t offset, QEMUIOVector *iov,
249 BlockCompletionFunc *cb, void *cb_opaque,
250 void *opaque)
251{
252 BlockBackend *blk = opaque;
253 return blk_aio_preadv(blk, offset, iov, 0, cb, cb_opaque);
254}
255
4be74634 256BlockAIOCB *dma_blk_read(BlockBackend *blk,
99868af3 257 QEMUSGList *sg, uint64_t offset, uint32_t align,
4be74634 258 void (*cb)(void *opaque, int ret), void *opaque)
59a703eb 259{
99868af3
MCA
260 return dma_blk_io(blk_get_aio_context(blk), sg, offset, align,
261 dma_blk_read_io_func, blk, cb, opaque,
4be74634 262 DMA_DIRECTION_FROM_DEVICE);
59a703eb
AL
263}
264
8a8e63eb
PB
265static
266BlockAIOCB *dma_blk_write_io_func(int64_t offset, QEMUIOVector *iov,
267 BlockCompletionFunc *cb, void *cb_opaque,
268 void *opaque)
269{
270 BlockBackend *blk = opaque;
271 return blk_aio_pwritev(blk, offset, iov, 0, cb, cb_opaque);
272}
273
4be74634 274BlockAIOCB *dma_blk_write(BlockBackend *blk,
99868af3 275 QEMUSGList *sg, uint64_t offset, uint32_t align,
4be74634 276 void (*cb)(void *opaque, int ret), void *opaque)
59a703eb 277{
99868af3
MCA
278 return dma_blk_io(blk_get_aio_context(blk), sg, offset, align,
279 dma_blk_write_io_func, blk, cb, opaque,
4be74634 280 DMA_DIRECTION_TO_DEVICE);
59a703eb 281}
8171ee35
PB
282
283
bfa30f39 284static MemTxResult dma_buf_rw(void *buf, dma_addr_t len, dma_addr_t *residual,
292e1314
PMD
285 QEMUSGList *sg, DMADirection dir,
286 MemTxAttrs attrs)
8171ee35 287{
c0ee1527 288 uint8_t *ptr = buf;
bfa30f39 289 dma_addr_t xresidual;
8171ee35 290 int sg_cur_index;
292e1314 291 MemTxResult res = MEMTX_OK;
8171ee35 292
5f412602 293 xresidual = sg->size;
8171ee35 294 sg_cur_index = 0;
5f412602 295 len = MIN(len, xresidual);
8171ee35
PB
296 while (len > 0) {
297 ScatterGatherEntry entry = sg->sg[sg_cur_index++];
bfa30f39 298 dma_addr_t xfer = MIN(len, entry.len);
292e1314 299 res |= dma_memory_rw(sg->as, entry.base, ptr, xfer, dir, attrs);
8171ee35
PB
300 ptr += xfer;
301 len -= xfer;
5f412602 302 xresidual -= xfer;
8171ee35
PB
303 }
304
5f412602
PMD
305 if (residual) {
306 *residual = xresidual;
292e1314
PMD
307 }
308 return res;
8171ee35
PB
309}
310
f02b664a 311MemTxResult dma_buf_read(void *ptr, dma_addr_t len, dma_addr_t *residual,
bfa30f39 312 QEMUSGList *sg, MemTxAttrs attrs)
8171ee35 313{
f02b664a 314 return dma_buf_rw(ptr, len, residual, sg, DMA_DIRECTION_FROM_DEVICE, attrs);
8171ee35
PB
315}
316
f02b664a 317MemTxResult dma_buf_write(void *ptr, dma_addr_t len, dma_addr_t *residual,
bfa30f39 318 QEMUSGList *sg, MemTxAttrs attrs)
8171ee35 319{
f02b664a 320 return dma_buf_rw(ptr, len, residual, sg, DMA_DIRECTION_TO_DEVICE, attrs);
8171ee35 321}
84a69356 322
4be74634 323void dma_acct_start(BlockBackend *blk, BlockAcctCookie *cookie,
84a69356
PB
324 QEMUSGList *sg, enum BlockAcctType type)
325{
4be74634 326 block_acct_start(blk_get_stats(blk), cookie, sg->size, type);
84a69356 327}
f14fb6c2
EA
328
329uint64_t dma_aligned_pow2_mask(uint64_t start, uint64_t end, int max_addr_bits)
330{
331 uint64_t max_mask = UINT64_MAX, addr_mask = end - start;
332 uint64_t alignment_mask, size_mask;
333
334 if (max_addr_bits != 64) {
335 max_mask = (1ULL << max_addr_bits) - 1;
336 }
337
338 alignment_mask = start ? (start & -start) - 1 : max_mask;
339 alignment_mask = MIN(alignment_mask, max_mask);
340 size_mask = MIN(addr_mask, max_mask);
341
342 if (alignment_mask <= size_mask) {
343 /* Increase the alignment of start */
344 return alignment_mask;
345 } else {
346 /* Find the largest page mask from size */
347 if (addr_mask == UINT64_MAX) {
348 return UINT64_MAX;
349 }
350 return (1ULL << (63 - clz64(addr_mask + 1))) - 1;
351 }
352}
353