blob: 4c9e529620f7841e7fb4a5c0ec10ada98cc524c1 [file] [log] [blame]
aliguori244ab902009-02-05 21:23:50 +00001/*
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#include "dma.h"
Kevin Wolfc57c4652011-11-24 06:15:28 -050011#include "trace.h"
aliguori244ab902009-02-05 21:23:50 +000012
David Gibsond86a77f2012-06-27 14:50:38 +100013int dma_memory_set(DMAContext *dma, dma_addr_t addr, uint8_t c, dma_addr_t len)
14{
15#define FILLBUF_SIZE 512
16 uint8_t fillbuf[FILLBUF_SIZE];
17 int l;
18
19 memset(fillbuf, c, FILLBUF_SIZE);
20 while (len > 0) {
21 l = len < FILLBUF_SIZE ? len : FILLBUF_SIZE;
22 cpu_physical_memory_rw(addr, fillbuf, l, true);
23 len -= len;
24 addr += len;
25 }
26 return 0;
27}
28
David Gibsonc65bcef2012-06-27 14:50:40 +100029void qemu_sglist_init(QEMUSGList *qsg, int alloc_hint, DMAContext *dma)
aliguori244ab902009-02-05 21:23:50 +000030{
Anthony Liguori7267c092011-08-20 22:09:37 -050031 qsg->sg = g_malloc(alloc_hint * sizeof(ScatterGatherEntry));
aliguori244ab902009-02-05 21:23:50 +000032 qsg->nsg = 0;
33 qsg->nalloc = alloc_hint;
34 qsg->size = 0;
David Gibsonc65bcef2012-06-27 14:50:40 +100035 qsg->dma = dma;
aliguori244ab902009-02-05 21:23:50 +000036}
37
David Gibsond3231182011-10-31 17:06:46 +110038void qemu_sglist_add(QEMUSGList *qsg, dma_addr_t base, dma_addr_t len)
aliguori244ab902009-02-05 21:23:50 +000039{
40 if (qsg->nsg == qsg->nalloc) {
41 qsg->nalloc = 2 * qsg->nalloc + 1;
Anthony Liguori7267c092011-08-20 22:09:37 -050042 qsg->sg = g_realloc(qsg->sg, qsg->nalloc * sizeof(ScatterGatherEntry));
aliguori244ab902009-02-05 21:23:50 +000043 }
44 qsg->sg[qsg->nsg].base = base;
45 qsg->sg[qsg->nsg].len = len;
46 qsg->size += len;
47 ++qsg->nsg;
48}
49
50void qemu_sglist_destroy(QEMUSGList *qsg)
51{
Anthony Liguori7267c092011-08-20 22:09:37 -050052 g_free(qsg->sg);
aliguori244ab902009-02-05 21:23:50 +000053}
54
aliguori59a703e2009-02-05 21:23:58 +000055typedef struct {
aliguori37b78422009-03-20 18:26:16 +000056 BlockDriverAIOCB common;
aliguori59a703e2009-02-05 21:23:58 +000057 BlockDriverState *bs;
58 BlockDriverAIOCB *acb;
59 QEMUSGList *sg;
60 uint64_t sector_num;
David Gibson43cf8ae2012-03-27 13:42:23 +110061 DMADirection dir;
Paolo Bonzinic3adb5b2011-09-16 16:40:02 +020062 bool in_cancel;
aliguori59a703e2009-02-05 21:23:58 +000063 int sg_cur_index;
David Gibsond3231182011-10-31 17:06:46 +110064 dma_addr_t sg_cur_byte;
aliguori59a703e2009-02-05 21:23:58 +000065 QEMUIOVector iov;
66 QEMUBH *bh;
Christoph Hellwigcb144cc2011-05-19 10:57:59 +020067 DMAIOFunc *io_func;
aliguori37b78422009-03-20 18:26:16 +000068} DMAAIOCB;
aliguori59a703e2009-02-05 21:23:58 +000069
70static void dma_bdrv_cb(void *opaque, int ret);
71
72static void reschedule_dma(void *opaque)
73{
aliguori37b78422009-03-20 18:26:16 +000074 DMAAIOCB *dbs = (DMAAIOCB *)opaque;
aliguori59a703e2009-02-05 21:23:58 +000075
76 qemu_bh_delete(dbs->bh);
77 dbs->bh = NULL;
Paolo Bonzinic3adb5b2011-09-16 16:40:02 +020078 dma_bdrv_cb(dbs, 0);
aliguori59a703e2009-02-05 21:23:58 +000079}
80
81static void continue_after_map_failure(void *opaque)
82{
aliguori37b78422009-03-20 18:26:16 +000083 DMAAIOCB *dbs = (DMAAIOCB *)opaque;
aliguori59a703e2009-02-05 21:23:58 +000084
85 dbs->bh = qemu_bh_new(reschedule_dma, dbs);
86 qemu_bh_schedule(dbs->bh);
87}
88
aliguori7403b142009-03-28 16:11:25 +000089static void dma_bdrv_unmap(DMAAIOCB *dbs)
aliguori59a703e2009-02-05 21:23:58 +000090{
aliguori59a703e2009-02-05 21:23:58 +000091 int i;
92
aliguori59a703e2009-02-05 21:23:58 +000093 for (i = 0; i < dbs->iov.niov; ++i) {
David Gibsonc65bcef2012-06-27 14:50:40 +100094 dma_memory_unmap(dbs->sg->dma, dbs->iov.iov[i].iov_base,
95 dbs->iov.iov[i].iov_len, dbs->dir,
96 dbs->iov.iov[i].iov_len);
aliguori59a703e2009-02-05 21:23:58 +000097 }
Paolo Bonzinic3adb5b2011-09-16 16:40:02 +020098 qemu_iovec_reset(&dbs->iov);
99}
100
101static void dma_complete(DMAAIOCB *dbs, int ret)
102{
Kevin Wolfc57c4652011-11-24 06:15:28 -0500103 trace_dma_complete(dbs, ret, dbs->common.cb);
104
Paolo Bonzinic3adb5b2011-09-16 16:40:02 +0200105 dma_bdrv_unmap(dbs);
106 if (dbs->common.cb) {
107 dbs->common.cb(dbs->common.opaque, ret);
108 }
109 qemu_iovec_destroy(&dbs->iov);
110 if (dbs->bh) {
111 qemu_bh_delete(dbs->bh);
112 dbs->bh = NULL;
113 }
114 if (!dbs->in_cancel) {
115 /* Requests may complete while dma_aio_cancel is in progress. In
116 * this case, the AIOCB should not be released because it is still
117 * referenced by dma_aio_cancel. */
118 qemu_aio_release(dbs);
119 }
aliguori7403b142009-03-28 16:11:25 +0000120}
121
blueswir1856ae5c2009-04-07 17:57:09 +0000122static void dma_bdrv_cb(void *opaque, int ret)
aliguori7403b142009-03-28 16:11:25 +0000123{
124 DMAAIOCB *dbs = (DMAAIOCB *)opaque;
David Gibsonc65bcef2012-06-27 14:50:40 +1000125 dma_addr_t cur_addr, cur_len;
aliguori7403b142009-03-28 16:11:25 +0000126 void *mem;
127
Kevin Wolfc57c4652011-11-24 06:15:28 -0500128 trace_dma_bdrv_cb(dbs, ret);
129
aliguori7403b142009-03-28 16:11:25 +0000130 dbs->acb = NULL;
131 dbs->sector_num += dbs->iov.size / 512;
132 dma_bdrv_unmap(dbs);
aliguori59a703e2009-02-05 21:23:58 +0000133
134 if (dbs->sg_cur_index == dbs->sg->nsg || ret < 0) {
Paolo Bonzinic3adb5b2011-09-16 16:40:02 +0200135 dma_complete(dbs, ret);
aliguori59a703e2009-02-05 21:23:58 +0000136 return;
137 }
138
139 while (dbs->sg_cur_index < dbs->sg->nsg) {
140 cur_addr = dbs->sg->sg[dbs->sg_cur_index].base + dbs->sg_cur_byte;
141 cur_len = dbs->sg->sg[dbs->sg_cur_index].len - dbs->sg_cur_byte;
David Gibsonc65bcef2012-06-27 14:50:40 +1000142 mem = dma_memory_map(dbs->sg->dma, cur_addr, &cur_len, dbs->dir);
aliguori59a703e2009-02-05 21:23:58 +0000143 if (!mem)
144 break;
145 qemu_iovec_add(&dbs->iov, mem, cur_len);
146 dbs->sg_cur_byte += cur_len;
147 if (dbs->sg_cur_byte == dbs->sg->sg[dbs->sg_cur_index].len) {
148 dbs->sg_cur_byte = 0;
149 ++dbs->sg_cur_index;
150 }
151 }
152
153 if (dbs->iov.size == 0) {
Kevin Wolfc57c4652011-11-24 06:15:28 -0500154 trace_dma_map_wait(dbs);
aliguori59a703e2009-02-05 21:23:58 +0000155 cpu_register_map_client(dbs, continue_after_map_failure);
156 return;
157 }
158
Christoph Hellwigcb144cc2011-05-19 10:57:59 +0200159 dbs->acb = dbs->io_func(dbs->bs, dbs->sector_num, &dbs->iov,
160 dbs->iov.size / 512, dma_bdrv_cb, dbs);
Paolo Bonzini6bee44e2011-11-14 17:50:52 +0100161 assert(dbs->acb);
aliguori59a703e2009-02-05 21:23:58 +0000162}
163
Christoph Hellwigc16b5a22009-05-25 12:37:32 +0200164static void dma_aio_cancel(BlockDriverAIOCB *acb)
165{
166 DMAAIOCB *dbs = container_of(acb, DMAAIOCB, common);
167
Kevin Wolfc57c4652011-11-24 06:15:28 -0500168 trace_dma_aio_cancel(dbs);
169
Christoph Hellwigc16b5a22009-05-25 12:37:32 +0200170 if (dbs->acb) {
Paolo Bonzinic3adb5b2011-09-16 16:40:02 +0200171 BlockDriverAIOCB *acb = dbs->acb;
172 dbs->acb = NULL;
173 dbs->in_cancel = true;
174 bdrv_aio_cancel(acb);
175 dbs->in_cancel = false;
Christoph Hellwigc16b5a22009-05-25 12:37:32 +0200176 }
Paolo Bonzinic3adb5b2011-09-16 16:40:02 +0200177 dbs->common.cb = NULL;
178 dma_complete(dbs, 0);
Christoph Hellwigc16b5a22009-05-25 12:37:32 +0200179}
180
181static AIOPool dma_aio_pool = {
182 .aiocb_size = sizeof(DMAAIOCB),
183 .cancel = dma_aio_cancel,
184};
185
Christoph Hellwigcb144cc2011-05-19 10:57:59 +0200186BlockDriverAIOCB *dma_bdrv_io(
aliguori59a703e2009-02-05 21:23:58 +0000187 BlockDriverState *bs, QEMUSGList *sg, uint64_t sector_num,
Christoph Hellwigcb144cc2011-05-19 10:57:59 +0200188 DMAIOFunc *io_func, BlockDriverCompletionFunc *cb,
David Gibson43cf8ae2012-03-27 13:42:23 +1100189 void *opaque, DMADirection dir)
aliguori59a703e2009-02-05 21:23:58 +0000190{
Christoph Hellwigcb144cc2011-05-19 10:57:59 +0200191 DMAAIOCB *dbs = qemu_aio_get(&dma_aio_pool, bs, cb, opaque);
aliguori59a703e2009-02-05 21:23:58 +0000192
David Gibson43cf8ae2012-03-27 13:42:23 +1100193 trace_dma_bdrv_io(dbs, bs, sector_num, (dir == DMA_DIRECTION_TO_DEVICE));
Kevin Wolfc57c4652011-11-24 06:15:28 -0500194
aliguori37b78422009-03-20 18:26:16 +0000195 dbs->acb = NULL;
aliguori59a703e2009-02-05 21:23:58 +0000196 dbs->bs = bs;
aliguori59a703e2009-02-05 21:23:58 +0000197 dbs->sg = sg;
198 dbs->sector_num = sector_num;
199 dbs->sg_cur_index = 0;
200 dbs->sg_cur_byte = 0;
David Gibson43cf8ae2012-03-27 13:42:23 +1100201 dbs->dir = dir;
Christoph Hellwigcb144cc2011-05-19 10:57:59 +0200202 dbs->io_func = io_func;
aliguori59a703e2009-02-05 21:23:58 +0000203 dbs->bh = NULL;
204 qemu_iovec_init(&dbs->iov, sg->nsg);
205 dma_bdrv_cb(dbs, 0);
aliguori37b78422009-03-20 18:26:16 +0000206 return &dbs->common;
aliguori59a703e2009-02-05 21:23:58 +0000207}
208
209
210BlockDriverAIOCB *dma_bdrv_read(BlockDriverState *bs,
211 QEMUSGList *sg, uint64_t sector,
212 void (*cb)(void *opaque, int ret), void *opaque)
213{
David Gibson43cf8ae2012-03-27 13:42:23 +1100214 return dma_bdrv_io(bs, sg, sector, bdrv_aio_readv, cb, opaque,
215 DMA_DIRECTION_FROM_DEVICE);
aliguori59a703e2009-02-05 21:23:58 +0000216}
217
218BlockDriverAIOCB *dma_bdrv_write(BlockDriverState *bs,
219 QEMUSGList *sg, uint64_t sector,
220 void (*cb)(void *opaque, int ret), void *opaque)
221{
David Gibson43cf8ae2012-03-27 13:42:23 +1100222 return dma_bdrv_io(bs, sg, sector, bdrv_aio_writev, cb, opaque,
223 DMA_DIRECTION_TO_DEVICE);
aliguori59a703e2009-02-05 21:23:58 +0000224}
Paolo Bonzini8171ee32011-07-06 08:02:14 +0200225
226
David Gibsonc65bcef2012-06-27 14:50:40 +1000227static uint64_t dma_buf_rw(uint8_t *ptr, int32_t len, QEMUSGList *sg,
228 DMADirection dir)
Paolo Bonzini8171ee32011-07-06 08:02:14 +0200229{
230 uint64_t resid;
231 int sg_cur_index;
232
233 resid = sg->size;
234 sg_cur_index = 0;
235 len = MIN(len, resid);
236 while (len > 0) {
237 ScatterGatherEntry entry = sg->sg[sg_cur_index++];
238 int32_t xfer = MIN(len, entry.len);
David Gibsonc65bcef2012-06-27 14:50:40 +1000239 dma_memory_rw(sg->dma, entry.base, ptr, xfer, dir);
Paolo Bonzini8171ee32011-07-06 08:02:14 +0200240 ptr += xfer;
241 len -= xfer;
242 resid -= xfer;
243 }
244
245 return resid;
246}
247
248uint64_t dma_buf_read(uint8_t *ptr, int32_t len, QEMUSGList *sg)
249{
David Gibsonc65bcef2012-06-27 14:50:40 +1000250 return dma_buf_rw(ptr, len, sg, DMA_DIRECTION_FROM_DEVICE);
Paolo Bonzini8171ee32011-07-06 08:02:14 +0200251}
252
253uint64_t dma_buf_write(uint8_t *ptr, int32_t len, QEMUSGList *sg)
254{
David Gibsonc65bcef2012-06-27 14:50:40 +1000255 return dma_buf_rw(ptr, len, sg, DMA_DIRECTION_TO_DEVICE);
Paolo Bonzini8171ee32011-07-06 08:02:14 +0200256}
Paolo Bonzini84a69352011-09-05 14:20:29 +0200257
258void dma_acct_start(BlockDriverState *bs, BlockAcctCookie *cookie,
259 QEMUSGList *sg, enum BlockAcctType type)
260{
261 bdrv_acct_start(bs, cookie, sg->size, type);
262}