blob: 499550fc231af3bcc268d9c9e4958bd0b5345f2e [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
Paolo Bonzini9c17d612012-12-17 18:20:04 +010010#include "sysemu/dma.h"
Kevin Wolfc57c4652011-11-24 06:15:28 -050011#include "trace.h"
Paolo Bonzini1de7afc2012-12-17 18:20:00 +010012#include "qemu/range.h"
13#include "qemu/thread.h"
aliguori244ab902009-02-05 21:23:50 +000014
David Gibsone5332e62012-06-27 14:50:43 +100015/* #define DEBUG_IOMMU */
16
Paolo Bonzinidf32fd12013-04-10 18:15:49 +020017int dma_memory_set(AddressSpace *as, dma_addr_t addr, uint8_t c, dma_addr_t len)
David Gibsond86a77f2012-06-27 14:50:38 +100018{
Paolo Bonzinidf32fd12013-04-10 18:15:49 +020019 dma_barrier(as, DMA_DIRECTION_FROM_DEVICE);
Paolo Bonzini24addbc2013-04-10 17:49:04 +020020
David Gibsond86a77f2012-06-27 14:50:38 +100021#define FILLBUF_SIZE 512
22 uint8_t fillbuf[FILLBUF_SIZE];
23 int l;
Paolo Bonzini24addbc2013-04-10 17:49:04 +020024 bool error = false;
David Gibsond86a77f2012-06-27 14:50:38 +100025
26 memset(fillbuf, c, FILLBUF_SIZE);
27 while (len > 0) {
28 l = len < FILLBUF_SIZE ? len : FILLBUF_SIZE;
Paolo Bonzini24addbc2013-04-10 17:49:04 +020029 error |= address_space_rw(as, addr, fillbuf, l, true);
Benjamin Herrenschmidtbc9b78d2012-08-14 17:41:47 +100030 len -= l;
31 addr += l;
David Gibsond86a77f2012-06-27 14:50:38 +100032 }
David Gibsone5332e62012-06-27 14:50:43 +100033
Paolo Bonzini24addbc2013-04-10 17:49:04 +020034 return error;
David Gibsond86a77f2012-06-27 14:50:38 +100035}
36
Paolo Bonzinif487b672013-06-03 14:17:19 +020037void qemu_sglist_init(QEMUSGList *qsg, DeviceState *dev, int alloc_hint,
38 AddressSpace *as)
aliguori244ab902009-02-05 21:23:50 +000039{
Anthony Liguori7267c092011-08-20 22:09:37 -050040 qsg->sg = g_malloc(alloc_hint * sizeof(ScatterGatherEntry));
aliguori244ab902009-02-05 21:23:50 +000041 qsg->nsg = 0;
42 qsg->nalloc = alloc_hint;
43 qsg->size = 0;
Paolo Bonzinidf32fd12013-04-10 18:15:49 +020044 qsg->as = as;
Paolo Bonzinif487b672013-06-03 14:17:19 +020045 qsg->dev = dev;
46 object_ref(OBJECT(dev));
aliguori244ab902009-02-05 21:23:50 +000047}
48
David Gibsond3231182011-10-31 17:06:46 +110049void qemu_sglist_add(QEMUSGList *qsg, dma_addr_t base, dma_addr_t len)
aliguori244ab902009-02-05 21:23:50 +000050{
51 if (qsg->nsg == qsg->nalloc) {
52 qsg->nalloc = 2 * qsg->nalloc + 1;
Anthony Liguori7267c092011-08-20 22:09:37 -050053 qsg->sg = g_realloc(qsg->sg, qsg->nalloc * sizeof(ScatterGatherEntry));
aliguori244ab902009-02-05 21:23:50 +000054 }
55 qsg->sg[qsg->nsg].base = base;
56 qsg->sg[qsg->nsg].len = len;
57 qsg->size += len;
58 ++qsg->nsg;
59}
60
61void qemu_sglist_destroy(QEMUSGList *qsg)
62{
Paolo Bonzinif487b672013-06-03 14:17:19 +020063 object_unref(OBJECT(qsg->dev));
Anthony Liguori7267c092011-08-20 22:09:37 -050064 g_free(qsg->sg);
Jason Baronea8d82a2012-08-03 15:57:10 -040065 memset(qsg, 0, sizeof(*qsg));
aliguori244ab902009-02-05 21:23:50 +000066}
67
aliguori59a703e2009-02-05 21:23:58 +000068typedef struct {
aliguori37b78422009-03-20 18:26:16 +000069 BlockDriverAIOCB common;
aliguori59a703e2009-02-05 21:23:58 +000070 BlockDriverState *bs;
71 BlockDriverAIOCB *acb;
72 QEMUSGList *sg;
73 uint64_t sector_num;
David Gibson43cf8ae2012-03-27 13:42:23 +110074 DMADirection dir;
Paolo Bonzinic3adb5b2011-09-16 16:40:02 +020075 bool in_cancel;
aliguori59a703e2009-02-05 21:23:58 +000076 int sg_cur_index;
David Gibsond3231182011-10-31 17:06:46 +110077 dma_addr_t sg_cur_byte;
aliguori59a703e2009-02-05 21:23:58 +000078 QEMUIOVector iov;
79 QEMUBH *bh;
Christoph Hellwigcb144cc2011-05-19 10:57:59 +020080 DMAIOFunc *io_func;
aliguori37b78422009-03-20 18:26:16 +000081} DMAAIOCB;
aliguori59a703e2009-02-05 21:23:58 +000082
83static void dma_bdrv_cb(void *opaque, int ret);
84
85static void reschedule_dma(void *opaque)
86{
aliguori37b78422009-03-20 18:26:16 +000087 DMAAIOCB *dbs = (DMAAIOCB *)opaque;
aliguori59a703e2009-02-05 21:23:58 +000088
89 qemu_bh_delete(dbs->bh);
90 dbs->bh = NULL;
Paolo Bonzinic3adb5b2011-09-16 16:40:02 +020091 dma_bdrv_cb(dbs, 0);
aliguori59a703e2009-02-05 21:23:58 +000092}
93
94static void continue_after_map_failure(void *opaque)
95{
aliguori37b78422009-03-20 18:26:16 +000096 DMAAIOCB *dbs = (DMAAIOCB *)opaque;
aliguori59a703e2009-02-05 21:23:58 +000097
98 dbs->bh = qemu_bh_new(reschedule_dma, dbs);
99 qemu_bh_schedule(dbs->bh);
100}
101
aliguori7403b142009-03-28 16:11:25 +0000102static void dma_bdrv_unmap(DMAAIOCB *dbs)
aliguori59a703e2009-02-05 21:23:58 +0000103{
aliguori59a703e2009-02-05 21:23:58 +0000104 int i;
105
aliguori59a703e2009-02-05 21:23:58 +0000106 for (i = 0; i < dbs->iov.niov; ++i) {
Paolo Bonzinidf32fd12013-04-10 18:15:49 +0200107 dma_memory_unmap(dbs->sg->as, dbs->iov.iov[i].iov_base,
David Gibsonc65bcef2012-06-27 14:50:40 +1000108 dbs->iov.iov[i].iov_len, dbs->dir,
109 dbs->iov.iov[i].iov_len);
aliguori59a703e2009-02-05 21:23:58 +0000110 }
Paolo Bonzinic3adb5b2011-09-16 16:40:02 +0200111 qemu_iovec_reset(&dbs->iov);
112}
113
114static void dma_complete(DMAAIOCB *dbs, int ret)
115{
Kevin Wolfc57c4652011-11-24 06:15:28 -0500116 trace_dma_complete(dbs, ret, dbs->common.cb);
117
Paolo Bonzinic3adb5b2011-09-16 16:40:02 +0200118 dma_bdrv_unmap(dbs);
119 if (dbs->common.cb) {
120 dbs->common.cb(dbs->common.opaque, ret);
121 }
122 qemu_iovec_destroy(&dbs->iov);
123 if (dbs->bh) {
124 qemu_bh_delete(dbs->bh);
125 dbs->bh = NULL;
126 }
127 if (!dbs->in_cancel) {
128 /* Requests may complete while dma_aio_cancel is in progress. In
129 * this case, the AIOCB should not be released because it is still
130 * referenced by dma_aio_cancel. */
131 qemu_aio_release(dbs);
132 }
aliguori7403b142009-03-28 16:11:25 +0000133}
134
blueswir1856ae5c2009-04-07 17:57:09 +0000135static void dma_bdrv_cb(void *opaque, int ret)
aliguori7403b142009-03-28 16:11:25 +0000136{
137 DMAAIOCB *dbs = (DMAAIOCB *)opaque;
David Gibsonc65bcef2012-06-27 14:50:40 +1000138 dma_addr_t cur_addr, cur_len;
aliguori7403b142009-03-28 16:11:25 +0000139 void *mem;
140
Kevin Wolfc57c4652011-11-24 06:15:28 -0500141 trace_dma_bdrv_cb(dbs, ret);
142
aliguori7403b142009-03-28 16:11:25 +0000143 dbs->acb = NULL;
144 dbs->sector_num += dbs->iov.size / 512;
145 dma_bdrv_unmap(dbs);
aliguori59a703e2009-02-05 21:23:58 +0000146
147 if (dbs->sg_cur_index == dbs->sg->nsg || ret < 0) {
Paolo Bonzinic3adb5b2011-09-16 16:40:02 +0200148 dma_complete(dbs, ret);
aliguori59a703e2009-02-05 21:23:58 +0000149 return;
150 }
151
152 while (dbs->sg_cur_index < dbs->sg->nsg) {
153 cur_addr = dbs->sg->sg[dbs->sg_cur_index].base + dbs->sg_cur_byte;
154 cur_len = dbs->sg->sg[dbs->sg_cur_index].len - dbs->sg_cur_byte;
Paolo Bonzinidf32fd12013-04-10 18:15:49 +0200155 mem = dma_memory_map(dbs->sg->as, cur_addr, &cur_len, dbs->dir);
aliguori59a703e2009-02-05 21:23:58 +0000156 if (!mem)
157 break;
158 qemu_iovec_add(&dbs->iov, mem, cur_len);
159 dbs->sg_cur_byte += cur_len;
160 if (dbs->sg_cur_byte == dbs->sg->sg[dbs->sg_cur_index].len) {
161 dbs->sg_cur_byte = 0;
162 ++dbs->sg_cur_index;
163 }
164 }
165
166 if (dbs->iov.size == 0) {
Kevin Wolfc57c4652011-11-24 06:15:28 -0500167 trace_dma_map_wait(dbs);
aliguori59a703e2009-02-05 21:23:58 +0000168 cpu_register_map_client(dbs, continue_after_map_failure);
169 return;
170 }
171
Christoph Hellwigcb144cc2011-05-19 10:57:59 +0200172 dbs->acb = dbs->io_func(dbs->bs, dbs->sector_num, &dbs->iov,
173 dbs->iov.size / 512, dma_bdrv_cb, dbs);
Paolo Bonzini6bee44e2011-11-14 17:50:52 +0100174 assert(dbs->acb);
aliguori59a703e2009-02-05 21:23:58 +0000175}
176
Christoph Hellwigc16b5a22009-05-25 12:37:32 +0200177static void dma_aio_cancel(BlockDriverAIOCB *acb)
178{
179 DMAAIOCB *dbs = container_of(acb, DMAAIOCB, common);
180
Kevin Wolfc57c4652011-11-24 06:15:28 -0500181 trace_dma_aio_cancel(dbs);
182
Christoph Hellwigc16b5a22009-05-25 12:37:32 +0200183 if (dbs->acb) {
Paolo Bonzinic3adb5b2011-09-16 16:40:02 +0200184 BlockDriverAIOCB *acb = dbs->acb;
185 dbs->acb = NULL;
186 dbs->in_cancel = true;
187 bdrv_aio_cancel(acb);
188 dbs->in_cancel = false;
Christoph Hellwigc16b5a22009-05-25 12:37:32 +0200189 }
Paolo Bonzinic3adb5b2011-09-16 16:40:02 +0200190 dbs->common.cb = NULL;
191 dma_complete(dbs, 0);
Christoph Hellwigc16b5a22009-05-25 12:37:32 +0200192}
193
Stefan Hajnoczid7331be2012-10-31 16:34:37 +0100194static const AIOCBInfo dma_aiocb_info = {
Christoph Hellwigc16b5a22009-05-25 12:37:32 +0200195 .aiocb_size = sizeof(DMAAIOCB),
196 .cancel = dma_aio_cancel,
197};
198
Christoph Hellwigcb144cc2011-05-19 10:57:59 +0200199BlockDriverAIOCB *dma_bdrv_io(
aliguori59a703e2009-02-05 21:23:58 +0000200 BlockDriverState *bs, QEMUSGList *sg, uint64_t sector_num,
Christoph Hellwigcb144cc2011-05-19 10:57:59 +0200201 DMAIOFunc *io_func, BlockDriverCompletionFunc *cb,
David Gibson43cf8ae2012-03-27 13:42:23 +1100202 void *opaque, DMADirection dir)
aliguori59a703e2009-02-05 21:23:58 +0000203{
Stefan Hajnoczid7331be2012-10-31 16:34:37 +0100204 DMAAIOCB *dbs = qemu_aio_get(&dma_aiocb_info, bs, cb, opaque);
aliguori59a703e2009-02-05 21:23:58 +0000205
David Gibson43cf8ae2012-03-27 13:42:23 +1100206 trace_dma_bdrv_io(dbs, bs, sector_num, (dir == DMA_DIRECTION_TO_DEVICE));
Kevin Wolfc57c4652011-11-24 06:15:28 -0500207
aliguori37b78422009-03-20 18:26:16 +0000208 dbs->acb = NULL;
aliguori59a703e2009-02-05 21:23:58 +0000209 dbs->bs = bs;
aliguori59a703e2009-02-05 21:23:58 +0000210 dbs->sg = sg;
211 dbs->sector_num = sector_num;
212 dbs->sg_cur_index = 0;
213 dbs->sg_cur_byte = 0;
David Gibson43cf8ae2012-03-27 13:42:23 +1100214 dbs->dir = dir;
Christoph Hellwigcb144cc2011-05-19 10:57:59 +0200215 dbs->io_func = io_func;
aliguori59a703e2009-02-05 21:23:58 +0000216 dbs->bh = NULL;
217 qemu_iovec_init(&dbs->iov, sg->nsg);
218 dma_bdrv_cb(dbs, 0);
aliguori37b78422009-03-20 18:26:16 +0000219 return &dbs->common;
aliguori59a703e2009-02-05 21:23:58 +0000220}
221
222
223BlockDriverAIOCB *dma_bdrv_read(BlockDriverState *bs,
224 QEMUSGList *sg, uint64_t sector,
225 void (*cb)(void *opaque, int ret), void *opaque)
226{
David Gibson43cf8ae2012-03-27 13:42:23 +1100227 return dma_bdrv_io(bs, sg, sector, bdrv_aio_readv, cb, opaque,
228 DMA_DIRECTION_FROM_DEVICE);
aliguori59a703e2009-02-05 21:23:58 +0000229}
230
231BlockDriverAIOCB *dma_bdrv_write(BlockDriverState *bs,
232 QEMUSGList *sg, uint64_t sector,
233 void (*cb)(void *opaque, int ret), void *opaque)
234{
David Gibson43cf8ae2012-03-27 13:42:23 +1100235 return dma_bdrv_io(bs, sg, sector, bdrv_aio_writev, cb, opaque,
236 DMA_DIRECTION_TO_DEVICE);
aliguori59a703e2009-02-05 21:23:58 +0000237}
Paolo Bonzini8171ee32011-07-06 08:02:14 +0200238
239
David Gibsonc65bcef2012-06-27 14:50:40 +1000240static uint64_t dma_buf_rw(uint8_t *ptr, int32_t len, QEMUSGList *sg,
241 DMADirection dir)
Paolo Bonzini8171ee32011-07-06 08:02:14 +0200242{
243 uint64_t resid;
244 int sg_cur_index;
245
246 resid = sg->size;
247 sg_cur_index = 0;
248 len = MIN(len, resid);
249 while (len > 0) {
250 ScatterGatherEntry entry = sg->sg[sg_cur_index++];
251 int32_t xfer = MIN(len, entry.len);
Paolo Bonzinidf32fd12013-04-10 18:15:49 +0200252 dma_memory_rw(sg->as, entry.base, ptr, xfer, dir);
Paolo Bonzini8171ee32011-07-06 08:02:14 +0200253 ptr += xfer;
254 len -= xfer;
255 resid -= xfer;
256 }
257
258 return resid;
259}
260
261uint64_t dma_buf_read(uint8_t *ptr, int32_t len, QEMUSGList *sg)
262{
David Gibsonc65bcef2012-06-27 14:50:40 +1000263 return dma_buf_rw(ptr, len, sg, DMA_DIRECTION_FROM_DEVICE);
Paolo Bonzini8171ee32011-07-06 08:02:14 +0200264}
265
266uint64_t dma_buf_write(uint8_t *ptr, int32_t len, QEMUSGList *sg)
267{
David Gibsonc65bcef2012-06-27 14:50:40 +1000268 return dma_buf_rw(ptr, len, sg, DMA_DIRECTION_TO_DEVICE);
Paolo Bonzini8171ee32011-07-06 08:02:14 +0200269}
Paolo Bonzini84a69352011-09-05 14:20:29 +0200270
271void dma_acct_start(BlockDriverState *bs, BlockAcctCookie *cookie,
272 QEMUSGList *sg, enum BlockAcctType type)
273{
274 bdrv_acct_start(bs, cookie, sg->size, type);
275}