ppc: Avoid AREG0 for misc helpers
[qemu/agraf.git] / dma-helpers.c
blob7971a89c144b7f192b15a5b9691009e58dcc4a41
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 DMADirection dir;
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,
79 dbs->dir != DMA_DIRECTION_TO_DEVICE,
80 dbs->iov.iov[i].iov_len);
82 qemu_iovec_reset(&dbs->iov);
85 static void dma_complete(DMAAIOCB *dbs, int ret)
87 trace_dma_complete(dbs, ret, dbs->common.cb);
89 dma_bdrv_unmap(dbs);
90 if (dbs->common.cb) {
91 dbs->common.cb(dbs->common.opaque, ret);
93 qemu_iovec_destroy(&dbs->iov);
94 if (dbs->bh) {
95 qemu_bh_delete(dbs->bh);
96 dbs->bh = NULL;
98 if (!dbs->in_cancel) {
99 /* Requests may complete while dma_aio_cancel is in progress. In
100 * this case, the AIOCB should not be released because it is still
101 * referenced by dma_aio_cancel. */
102 qemu_aio_release(dbs);
106 static void dma_bdrv_cb(void *opaque, int ret)
108 DMAAIOCB *dbs = (DMAAIOCB *)opaque;
109 target_phys_addr_t cur_addr, cur_len;
110 void *mem;
112 trace_dma_bdrv_cb(dbs, ret);
114 dbs->acb = NULL;
115 dbs->sector_num += dbs->iov.size / 512;
116 dma_bdrv_unmap(dbs);
118 if (dbs->sg_cur_index == dbs->sg->nsg || ret < 0) {
119 dma_complete(dbs, ret);
120 return;
123 while (dbs->sg_cur_index < dbs->sg->nsg) {
124 cur_addr = dbs->sg->sg[dbs->sg_cur_index].base + dbs->sg_cur_byte;
125 cur_len = dbs->sg->sg[dbs->sg_cur_index].len - dbs->sg_cur_byte;
126 mem = cpu_physical_memory_map(cur_addr, &cur_len,
127 dbs->dir != DMA_DIRECTION_TO_DEVICE);
128 if (!mem)
129 break;
130 qemu_iovec_add(&dbs->iov, mem, cur_len);
131 dbs->sg_cur_byte += cur_len;
132 if (dbs->sg_cur_byte == dbs->sg->sg[dbs->sg_cur_index].len) {
133 dbs->sg_cur_byte = 0;
134 ++dbs->sg_cur_index;
138 if (dbs->iov.size == 0) {
139 trace_dma_map_wait(dbs);
140 cpu_register_map_client(dbs, continue_after_map_failure);
141 return;
144 dbs->acb = dbs->io_func(dbs->bs, dbs->sector_num, &dbs->iov,
145 dbs->iov.size / 512, dma_bdrv_cb, dbs);
146 assert(dbs->acb);
149 static void dma_aio_cancel(BlockDriverAIOCB *acb)
151 DMAAIOCB *dbs = container_of(acb, DMAAIOCB, common);
153 trace_dma_aio_cancel(dbs);
155 if (dbs->acb) {
156 BlockDriverAIOCB *acb = dbs->acb;
157 dbs->acb = NULL;
158 dbs->in_cancel = true;
159 bdrv_aio_cancel(acb);
160 dbs->in_cancel = false;
162 dbs->common.cb = NULL;
163 dma_complete(dbs, 0);
166 static AIOPool dma_aio_pool = {
167 .aiocb_size = sizeof(DMAAIOCB),
168 .cancel = dma_aio_cancel,
171 BlockDriverAIOCB *dma_bdrv_io(
172 BlockDriverState *bs, QEMUSGList *sg, uint64_t sector_num,
173 DMAIOFunc *io_func, BlockDriverCompletionFunc *cb,
174 void *opaque, DMADirection dir)
176 DMAAIOCB *dbs = qemu_aio_get(&dma_aio_pool, bs, cb, opaque);
178 trace_dma_bdrv_io(dbs, bs, sector_num, (dir == DMA_DIRECTION_TO_DEVICE));
180 dbs->acb = NULL;
181 dbs->bs = bs;
182 dbs->sg = sg;
183 dbs->sector_num = sector_num;
184 dbs->sg_cur_index = 0;
185 dbs->sg_cur_byte = 0;
186 dbs->dir = dir;
187 dbs->io_func = io_func;
188 dbs->bh = NULL;
189 qemu_iovec_init(&dbs->iov, sg->nsg);
190 dma_bdrv_cb(dbs, 0);
191 return &dbs->common;
195 BlockDriverAIOCB *dma_bdrv_read(BlockDriverState *bs,
196 QEMUSGList *sg, uint64_t sector,
197 void (*cb)(void *opaque, int ret), void *opaque)
199 return dma_bdrv_io(bs, sg, sector, bdrv_aio_readv, cb, opaque,
200 DMA_DIRECTION_FROM_DEVICE);
203 BlockDriverAIOCB *dma_bdrv_write(BlockDriverState *bs,
204 QEMUSGList *sg, uint64_t sector,
205 void (*cb)(void *opaque, int ret), void *opaque)
207 return dma_bdrv_io(bs, sg, sector, bdrv_aio_writev, cb, opaque,
208 DMA_DIRECTION_TO_DEVICE);
212 static uint64_t dma_buf_rw(uint8_t *ptr, int32_t len, QEMUSGList *sg, bool to_dev)
214 uint64_t resid;
215 int sg_cur_index;
217 resid = sg->size;
218 sg_cur_index = 0;
219 len = MIN(len, resid);
220 while (len > 0) {
221 ScatterGatherEntry entry = sg->sg[sg_cur_index++];
222 int32_t xfer = MIN(len, entry.len);
223 cpu_physical_memory_rw(entry.base, ptr, xfer, !to_dev);
224 ptr += xfer;
225 len -= xfer;
226 resid -= xfer;
229 return resid;
232 uint64_t dma_buf_read(uint8_t *ptr, int32_t len, QEMUSGList *sg)
234 return dma_buf_rw(ptr, len, sg, 0);
237 uint64_t dma_buf_write(uint8_t *ptr, int32_t len, QEMUSGList *sg)
239 return dma_buf_rw(ptr, len, sg, 1);
242 void dma_acct_start(BlockDriverState *bs, BlockAcctCookie *cookie,
243 QEMUSGList *sg, enum BlockAcctType type)
245 bdrv_acct_start(bs, cookie, sg->size, type);