vdi: leave bounce buffering to block layer
[qemu.git] / dma-helpers.c
blob7fcc86dca92d272ec7d5b98adfeb0e4c4e544ae2
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 #include "dma.h"
11 #include "trace.h"
13 void qemu_sglist_init(QEMUSGList *qsg, int alloc_hint)
15 qsg->sg = g_malloc(alloc_hint * sizeof(ScatterGatherEntry));
16 qsg->nsg = 0;
17 qsg->nalloc = alloc_hint;
18 qsg->size = 0;
21 void qemu_sglist_add(QEMUSGList *qsg, dma_addr_t base, dma_addr_t len)
23 if (qsg->nsg == qsg->nalloc) {
24 qsg->nalloc = 2 * qsg->nalloc + 1;
25 qsg->sg = g_realloc(qsg->sg, qsg->nalloc * sizeof(ScatterGatherEntry));
27 qsg->sg[qsg->nsg].base = base;
28 qsg->sg[qsg->nsg].len = len;
29 qsg->size += len;
30 ++qsg->nsg;
33 void qemu_sglist_destroy(QEMUSGList *qsg)
35 g_free(qsg->sg);
38 typedef struct {
39 BlockDriverAIOCB common;
40 BlockDriverState *bs;
41 BlockDriverAIOCB *acb;
42 QEMUSGList *sg;
43 uint64_t sector_num;
44 bool to_dev;
45 bool in_cancel;
46 int sg_cur_index;
47 dma_addr_t sg_cur_byte;
48 QEMUIOVector iov;
49 QEMUBH *bh;
50 DMAIOFunc *io_func;
51 } DMAAIOCB;
53 static void dma_bdrv_cb(void *opaque, int ret);
55 static void reschedule_dma(void *opaque)
57 DMAAIOCB *dbs = (DMAAIOCB *)opaque;
59 qemu_bh_delete(dbs->bh);
60 dbs->bh = NULL;
61 dma_bdrv_cb(dbs, 0);
64 static void continue_after_map_failure(void *opaque)
66 DMAAIOCB *dbs = (DMAAIOCB *)opaque;
68 dbs->bh = qemu_bh_new(reschedule_dma, dbs);
69 qemu_bh_schedule(dbs->bh);
72 static void dma_bdrv_unmap(DMAAIOCB *dbs)
74 int i;
76 for (i = 0; i < dbs->iov.niov; ++i) {
77 cpu_physical_memory_unmap(dbs->iov.iov[i].iov_base,
78 dbs->iov.iov[i].iov_len, !dbs->to_dev,
79 dbs->iov.iov[i].iov_len);
81 qemu_iovec_reset(&dbs->iov);
84 static void dma_complete(DMAAIOCB *dbs, int ret)
86 trace_dma_complete(dbs, ret, dbs->common.cb);
88 dma_bdrv_unmap(dbs);
89 if (dbs->common.cb) {
90 dbs->common.cb(dbs->common.opaque, ret);
92 qemu_iovec_destroy(&dbs->iov);
93 if (dbs->bh) {
94 qemu_bh_delete(dbs->bh);
95 dbs->bh = NULL;
97 if (!dbs->in_cancel) {
98 /* Requests may complete while dma_aio_cancel is in progress. In
99 * this case, the AIOCB should not be released because it is still
100 * referenced by dma_aio_cancel. */
101 qemu_aio_release(dbs);
105 static void dma_bdrv_cb(void *opaque, int ret)
107 DMAAIOCB *dbs = (DMAAIOCB *)opaque;
108 target_phys_addr_t cur_addr, cur_len;
109 void *mem;
111 trace_dma_bdrv_cb(dbs, ret);
113 dbs->acb = NULL;
114 dbs->sector_num += dbs->iov.size / 512;
115 dma_bdrv_unmap(dbs);
117 if (dbs->sg_cur_index == dbs->sg->nsg || ret < 0) {
118 dma_complete(dbs, ret);
119 return;
122 while (dbs->sg_cur_index < dbs->sg->nsg) {
123 cur_addr = dbs->sg->sg[dbs->sg_cur_index].base + dbs->sg_cur_byte;
124 cur_len = dbs->sg->sg[dbs->sg_cur_index].len - dbs->sg_cur_byte;
125 mem = cpu_physical_memory_map(cur_addr, &cur_len, !dbs->to_dev);
126 if (!mem)
127 break;
128 qemu_iovec_add(&dbs->iov, mem, cur_len);
129 dbs->sg_cur_byte += cur_len;
130 if (dbs->sg_cur_byte == dbs->sg->sg[dbs->sg_cur_index].len) {
131 dbs->sg_cur_byte = 0;
132 ++dbs->sg_cur_index;
136 if (dbs->iov.size == 0) {
137 trace_dma_map_wait(dbs);
138 cpu_register_map_client(dbs, continue_after_map_failure);
139 return;
142 dbs->acb = dbs->io_func(dbs->bs, dbs->sector_num, &dbs->iov,
143 dbs->iov.size / 512, dma_bdrv_cb, dbs);
144 assert(dbs->acb);
147 static void dma_aio_cancel(BlockDriverAIOCB *acb)
149 DMAAIOCB *dbs = container_of(acb, DMAAIOCB, common);
151 trace_dma_aio_cancel(dbs);
153 if (dbs->acb) {
154 BlockDriverAIOCB *acb = dbs->acb;
155 dbs->acb = NULL;
156 dbs->in_cancel = true;
157 bdrv_aio_cancel(acb);
158 dbs->in_cancel = false;
160 dbs->common.cb = NULL;
161 dma_complete(dbs, 0);
164 static AIOPool dma_aio_pool = {
165 .aiocb_size = sizeof(DMAAIOCB),
166 .cancel = dma_aio_cancel,
169 BlockDriverAIOCB *dma_bdrv_io(
170 BlockDriverState *bs, QEMUSGList *sg, uint64_t sector_num,
171 DMAIOFunc *io_func, BlockDriverCompletionFunc *cb,
172 void *opaque, bool to_dev)
174 DMAAIOCB *dbs = qemu_aio_get(&dma_aio_pool, bs, cb, opaque);
176 trace_dma_bdrv_io(dbs, bs, sector_num, to_dev);
178 dbs->acb = NULL;
179 dbs->bs = bs;
180 dbs->sg = sg;
181 dbs->sector_num = sector_num;
182 dbs->sg_cur_index = 0;
183 dbs->sg_cur_byte = 0;
184 dbs->to_dev = to_dev;
185 dbs->io_func = io_func;
186 dbs->bh = NULL;
187 qemu_iovec_init(&dbs->iov, sg->nsg);
188 dma_bdrv_cb(dbs, 0);
189 return &dbs->common;
193 BlockDriverAIOCB *dma_bdrv_read(BlockDriverState *bs,
194 QEMUSGList *sg, uint64_t sector,
195 void (*cb)(void *opaque, int ret), void *opaque)
197 return dma_bdrv_io(bs, sg, sector, bdrv_aio_readv, cb, opaque, false);
200 BlockDriverAIOCB *dma_bdrv_write(BlockDriverState *bs,
201 QEMUSGList *sg, uint64_t sector,
202 void (*cb)(void *opaque, int ret), void *opaque)
204 return dma_bdrv_io(bs, sg, sector, bdrv_aio_writev, cb, opaque, true);
208 static uint64_t dma_buf_rw(uint8_t *ptr, int32_t len, QEMUSGList *sg, bool to_dev)
210 uint64_t resid;
211 int sg_cur_index;
213 resid = sg->size;
214 sg_cur_index = 0;
215 len = MIN(len, resid);
216 while (len > 0) {
217 ScatterGatherEntry entry = sg->sg[sg_cur_index++];
218 int32_t xfer = MIN(len, entry.len);
219 cpu_physical_memory_rw(entry.base, ptr, xfer, !to_dev);
220 ptr += xfer;
221 len -= xfer;
222 resid -= xfer;
225 return resid;
228 uint64_t dma_buf_read(uint8_t *ptr, int32_t len, QEMUSGList *sg)
230 return dma_buf_rw(ptr, len, sg, 0);
233 uint64_t dma_buf_write(uint8_t *ptr, int32_t len, QEMUSGList *sg)
235 return dma_buf_rw(ptr, len, sg, 1);
238 void dma_acct_start(BlockDriverState *bs, BlockAcctCookie *cookie,
239 QEMUSGList *sg, enum BlockAcctType type)
241 bdrv_acct_start(bs, cookie, sg->size, type);