Merge remote-tracking branch 'kwolf/for-anthony' into staging
[qemu.git] / dma-helpers.c
blob86d2d0a9970bae0f8895d309a738f7cc0b3ff950
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 "block_int.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, target_phys_addr_t base,
22 target_phys_addr_t len)
24 if (qsg->nsg == qsg->nalloc) {
25 qsg->nalloc = 2 * qsg->nalloc + 1;
26 qsg->sg = g_realloc(qsg->sg, qsg->nalloc * sizeof(ScatterGatherEntry));
28 qsg->sg[qsg->nsg].base = base;
29 qsg->sg[qsg->nsg].len = len;
30 qsg->size += len;
31 ++qsg->nsg;
34 void qemu_sglist_destroy(QEMUSGList *qsg)
36 g_free(qsg->sg);
39 typedef struct {
40 BlockDriverAIOCB common;
41 BlockDriverState *bs;
42 BlockDriverAIOCB *acb;
43 QEMUSGList *sg;
44 uint64_t sector_num;
45 bool to_dev;
46 bool in_cancel;
47 int sg_cur_index;
48 target_phys_addr_t sg_cur_byte;
49 QEMUIOVector iov;
50 QEMUBH *bh;
51 DMAIOFunc *io_func;
52 } DMAAIOCB;
54 static void dma_bdrv_cb(void *opaque, int ret);
56 static void reschedule_dma(void *opaque)
58 DMAAIOCB *dbs = (DMAAIOCB *)opaque;
60 qemu_bh_delete(dbs->bh);
61 dbs->bh = NULL;
62 dma_bdrv_cb(dbs, 0);
65 static void continue_after_map_failure(void *opaque)
67 DMAAIOCB *dbs = (DMAAIOCB *)opaque;
69 dbs->bh = qemu_bh_new(reschedule_dma, dbs);
70 qemu_bh_schedule(dbs->bh);
73 static void dma_bdrv_unmap(DMAAIOCB *dbs)
75 int i;
77 for (i = 0; i < dbs->iov.niov; ++i) {
78 cpu_physical_memory_unmap(dbs->iov.iov[i].iov_base,
79 dbs->iov.iov[i].iov_len, !dbs->to_dev,
80 dbs->iov.iov[i].iov_len);
82 qemu_iovec_reset(&dbs->iov);
85 static void dma_complete(DMAAIOCB *dbs, int ret)
87 dma_bdrv_unmap(dbs);
88 if (dbs->common.cb) {
89 dbs->common.cb(dbs->common.opaque, ret);
91 qemu_iovec_destroy(&dbs->iov);
92 if (dbs->bh) {
93 qemu_bh_delete(dbs->bh);
94 dbs->bh = NULL;
96 if (!dbs->in_cancel) {
97 /* Requests may complete while dma_aio_cancel is in progress. In
98 * this case, the AIOCB should not be released because it is still
99 * referenced by dma_aio_cancel. */
100 qemu_aio_release(dbs);
104 static void dma_bdrv_cb(void *opaque, int ret)
106 DMAAIOCB *dbs = (DMAAIOCB *)opaque;
107 target_phys_addr_t cur_addr, cur_len;
108 void *mem;
110 dbs->acb = NULL;
111 dbs->sector_num += dbs->iov.size / 512;
112 dma_bdrv_unmap(dbs);
114 if (dbs->sg_cur_index == dbs->sg->nsg || ret < 0) {
115 dma_complete(dbs, ret);
116 return;
119 while (dbs->sg_cur_index < dbs->sg->nsg) {
120 cur_addr = dbs->sg->sg[dbs->sg_cur_index].base + dbs->sg_cur_byte;
121 cur_len = dbs->sg->sg[dbs->sg_cur_index].len - dbs->sg_cur_byte;
122 mem = cpu_physical_memory_map(cur_addr, &cur_len, !dbs->to_dev);
123 if (!mem)
124 break;
125 qemu_iovec_add(&dbs->iov, mem, cur_len);
126 dbs->sg_cur_byte += cur_len;
127 if (dbs->sg_cur_byte == dbs->sg->sg[dbs->sg_cur_index].len) {
128 dbs->sg_cur_byte = 0;
129 ++dbs->sg_cur_index;
133 if (dbs->iov.size == 0) {
134 cpu_register_map_client(dbs, continue_after_map_failure);
135 return;
138 dbs->acb = dbs->io_func(dbs->bs, dbs->sector_num, &dbs->iov,
139 dbs->iov.size / 512, dma_bdrv_cb, dbs);
140 if (!dbs->acb) {
141 dma_complete(dbs, -EIO);
145 static void dma_aio_cancel(BlockDriverAIOCB *acb)
147 DMAAIOCB *dbs = container_of(acb, DMAAIOCB, common);
149 if (dbs->acb) {
150 BlockDriverAIOCB *acb = dbs->acb;
151 dbs->acb = NULL;
152 dbs->in_cancel = true;
153 bdrv_aio_cancel(acb);
154 dbs->in_cancel = false;
156 dbs->common.cb = NULL;
157 dma_complete(dbs, 0);
160 static AIOPool dma_aio_pool = {
161 .aiocb_size = sizeof(DMAAIOCB),
162 .cancel = dma_aio_cancel,
165 BlockDriverAIOCB *dma_bdrv_io(
166 BlockDriverState *bs, QEMUSGList *sg, uint64_t sector_num,
167 DMAIOFunc *io_func, BlockDriverCompletionFunc *cb,
168 void *opaque, bool to_dev)
170 DMAAIOCB *dbs = qemu_aio_get(&dma_aio_pool, bs, cb, opaque);
172 dbs->acb = NULL;
173 dbs->bs = bs;
174 dbs->sg = sg;
175 dbs->sector_num = sector_num;
176 dbs->sg_cur_index = 0;
177 dbs->sg_cur_byte = 0;
178 dbs->to_dev = to_dev;
179 dbs->io_func = io_func;
180 dbs->bh = NULL;
181 qemu_iovec_init(&dbs->iov, sg->nsg);
182 dma_bdrv_cb(dbs, 0);
183 return &dbs->common;
187 BlockDriverAIOCB *dma_bdrv_read(BlockDriverState *bs,
188 QEMUSGList *sg, uint64_t sector,
189 void (*cb)(void *opaque, int ret), void *opaque)
191 return dma_bdrv_io(bs, sg, sector, bdrv_aio_readv, cb, opaque, false);
194 BlockDriverAIOCB *dma_bdrv_write(BlockDriverState *bs,
195 QEMUSGList *sg, uint64_t sector,
196 void (*cb)(void *opaque, int ret), void *opaque)
198 return dma_bdrv_io(bs, sg, sector, bdrv_aio_writev, cb, opaque, true);