blob: c53705a63df708052d879417ad394053d5d9a026 [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 Bonzini24addbc2013-04-10 17:49:04 +020017int dma_memory_set(DMAContext *dma, dma_addr_t addr, uint8_t c, dma_addr_t len)
David Gibsond86a77f2012-06-27 14:50:38 +100018{
Paolo Bonzini24addbc2013-04-10 17:49:04 +020019 AddressSpace *as = dma->as;
20
21 dma_barrier(dma, DMA_DIRECTION_FROM_DEVICE);
22
David Gibsond86a77f2012-06-27 14:50:38 +100023#define FILLBUF_SIZE 512
24 uint8_t fillbuf[FILLBUF_SIZE];
25 int l;
Paolo Bonzini24addbc2013-04-10 17:49:04 +020026 bool error = false;
David Gibsond86a77f2012-06-27 14:50:38 +100027
28 memset(fillbuf, c, FILLBUF_SIZE);
29 while (len > 0) {
30 l = len < FILLBUF_SIZE ? len : FILLBUF_SIZE;
Paolo Bonzini24addbc2013-04-10 17:49:04 +020031 error |= address_space_rw(as, addr, fillbuf, l, true);
Benjamin Herrenschmidtbc9b78d2012-08-14 17:41:47 +100032 len -= l;
33 addr += l;
David Gibsond86a77f2012-06-27 14:50:38 +100034 }
David Gibsone5332e62012-06-27 14:50:43 +100035
Paolo Bonzini24addbc2013-04-10 17:49:04 +020036 return error;
David Gibsond86a77f2012-06-27 14:50:38 +100037}
38
David Gibsonc65bcef2012-06-27 14:50:40 +100039void qemu_sglist_init(QEMUSGList *qsg, int alloc_hint, DMAContext *dma)
aliguori244ab902009-02-05 21:23:50 +000040{
Anthony Liguori7267c092011-08-20 22:09:37 -050041 qsg->sg = g_malloc(alloc_hint * sizeof(ScatterGatherEntry));
aliguori244ab902009-02-05 21:23:50 +000042 qsg->nsg = 0;
43 qsg->nalloc = alloc_hint;
44 qsg->size = 0;
David Gibsonc65bcef2012-06-27 14:50:40 +100045 qsg->dma = dma;
aliguori244ab902009-02-05 21:23:50 +000046}
47
David Gibsond3231182011-10-31 17:06:46 +110048void qemu_sglist_add(QEMUSGList *qsg, dma_addr_t base, dma_addr_t len)
aliguori244ab902009-02-05 21:23:50 +000049{
50 if (qsg->nsg == qsg->nalloc) {
51 qsg->nalloc = 2 * qsg->nalloc + 1;
Anthony Liguori7267c092011-08-20 22:09:37 -050052 qsg->sg = g_realloc(qsg->sg, qsg->nalloc * sizeof(ScatterGatherEntry));
aliguori244ab902009-02-05 21:23:50 +000053 }
54 qsg->sg[qsg->nsg].base = base;
55 qsg->sg[qsg->nsg].len = len;
56 qsg->size += len;
57 ++qsg->nsg;
58}
59
60void qemu_sglist_destroy(QEMUSGList *qsg)
61{
Anthony Liguori7267c092011-08-20 22:09:37 -050062 g_free(qsg->sg);
Jason Baronea8d82a2012-08-03 15:57:10 -040063 memset(qsg, 0, sizeof(*qsg));
aliguori244ab902009-02-05 21:23:50 +000064}
65
aliguori59a703e2009-02-05 21:23:58 +000066typedef struct {
aliguori37b78422009-03-20 18:26:16 +000067 BlockDriverAIOCB common;
aliguori59a703e2009-02-05 21:23:58 +000068 BlockDriverState *bs;
69 BlockDriverAIOCB *acb;
70 QEMUSGList *sg;
71 uint64_t sector_num;
David Gibson43cf8ae2012-03-27 13:42:23 +110072 DMADirection dir;
Paolo Bonzinic3adb5b2011-09-16 16:40:02 +020073 bool in_cancel;
aliguori59a703e2009-02-05 21:23:58 +000074 int sg_cur_index;
David Gibsond3231182011-10-31 17:06:46 +110075 dma_addr_t sg_cur_byte;
aliguori59a703e2009-02-05 21:23:58 +000076 QEMUIOVector iov;
77 QEMUBH *bh;
Christoph Hellwigcb144cc2011-05-19 10:57:59 +020078 DMAIOFunc *io_func;
aliguori37b78422009-03-20 18:26:16 +000079} DMAAIOCB;
aliguori59a703e2009-02-05 21:23:58 +000080
81static void dma_bdrv_cb(void *opaque, int ret);
82
83static void reschedule_dma(void *opaque)
84{
aliguori37b78422009-03-20 18:26:16 +000085 DMAAIOCB *dbs = (DMAAIOCB *)opaque;
aliguori59a703e2009-02-05 21:23:58 +000086
87 qemu_bh_delete(dbs->bh);
88 dbs->bh = NULL;
Paolo Bonzinic3adb5b2011-09-16 16:40:02 +020089 dma_bdrv_cb(dbs, 0);
aliguori59a703e2009-02-05 21:23:58 +000090}
91
92static void continue_after_map_failure(void *opaque)
93{
aliguori37b78422009-03-20 18:26:16 +000094 DMAAIOCB *dbs = (DMAAIOCB *)opaque;
aliguori59a703e2009-02-05 21:23:58 +000095
96 dbs->bh = qemu_bh_new(reschedule_dma, dbs);
97 qemu_bh_schedule(dbs->bh);
98}
99
aliguori7403b142009-03-28 16:11:25 +0000100static void dma_bdrv_unmap(DMAAIOCB *dbs)
aliguori59a703e2009-02-05 21:23:58 +0000101{
aliguori59a703e2009-02-05 21:23:58 +0000102 int i;
103
aliguori59a703e2009-02-05 21:23:58 +0000104 for (i = 0; i < dbs->iov.niov; ++i) {
David Gibsonc65bcef2012-06-27 14:50:40 +1000105 dma_memory_unmap(dbs->sg->dma, dbs->iov.iov[i].iov_base,
106 dbs->iov.iov[i].iov_len, dbs->dir,
107 dbs->iov.iov[i].iov_len);
aliguori59a703e2009-02-05 21:23:58 +0000108 }
Paolo Bonzinic3adb5b2011-09-16 16:40:02 +0200109 qemu_iovec_reset(&dbs->iov);
110}
111
112static void dma_complete(DMAAIOCB *dbs, int ret)
113{
Kevin Wolfc57c4652011-11-24 06:15:28 -0500114 trace_dma_complete(dbs, ret, dbs->common.cb);
115
Paolo Bonzinic3adb5b2011-09-16 16:40:02 +0200116 dma_bdrv_unmap(dbs);
117 if (dbs->common.cb) {
118 dbs->common.cb(dbs->common.opaque, ret);
119 }
120 qemu_iovec_destroy(&dbs->iov);
121 if (dbs->bh) {
122 qemu_bh_delete(dbs->bh);
123 dbs->bh = NULL;
124 }
125 if (!dbs->in_cancel) {
126 /* Requests may complete while dma_aio_cancel is in progress. In
127 * this case, the AIOCB should not be released because it is still
128 * referenced by dma_aio_cancel. */
129 qemu_aio_release(dbs);
130 }
aliguori7403b142009-03-28 16:11:25 +0000131}
132
blueswir1856ae5c2009-04-07 17:57:09 +0000133static void dma_bdrv_cb(void *opaque, int ret)
aliguori7403b142009-03-28 16:11:25 +0000134{
135 DMAAIOCB *dbs = (DMAAIOCB *)opaque;
David Gibsonc65bcef2012-06-27 14:50:40 +1000136 dma_addr_t cur_addr, cur_len;
aliguori7403b142009-03-28 16:11:25 +0000137 void *mem;
138
Kevin Wolfc57c4652011-11-24 06:15:28 -0500139 trace_dma_bdrv_cb(dbs, ret);
140
aliguori7403b142009-03-28 16:11:25 +0000141 dbs->acb = NULL;
142 dbs->sector_num += dbs->iov.size / 512;
143 dma_bdrv_unmap(dbs);
aliguori59a703e2009-02-05 21:23:58 +0000144
145 if (dbs->sg_cur_index == dbs->sg->nsg || ret < 0) {
Paolo Bonzinic3adb5b2011-09-16 16:40:02 +0200146 dma_complete(dbs, ret);
aliguori59a703e2009-02-05 21:23:58 +0000147 return;
148 }
149
150 while (dbs->sg_cur_index < dbs->sg->nsg) {
151 cur_addr = dbs->sg->sg[dbs->sg_cur_index].base + dbs->sg_cur_byte;
152 cur_len = dbs->sg->sg[dbs->sg_cur_index].len - dbs->sg_cur_byte;
David Gibsonc65bcef2012-06-27 14:50:40 +1000153 mem = dma_memory_map(dbs->sg->dma, cur_addr, &cur_len, dbs->dir);
aliguori59a703e2009-02-05 21:23:58 +0000154 if (!mem)
155 break;
156 qemu_iovec_add(&dbs->iov, mem, cur_len);
157 dbs->sg_cur_byte += cur_len;
158 if (dbs->sg_cur_byte == dbs->sg->sg[dbs->sg_cur_index].len) {
159 dbs->sg_cur_byte = 0;
160 ++dbs->sg_cur_index;
161 }
162 }
163
164 if (dbs->iov.size == 0) {
Kevin Wolfc57c4652011-11-24 06:15:28 -0500165 trace_dma_map_wait(dbs);
aliguori59a703e2009-02-05 21:23:58 +0000166 cpu_register_map_client(dbs, continue_after_map_failure);
167 return;
168 }
169
Christoph Hellwigcb144cc2011-05-19 10:57:59 +0200170 dbs->acb = dbs->io_func(dbs->bs, dbs->sector_num, &dbs->iov,
171 dbs->iov.size / 512, dma_bdrv_cb, dbs);
Paolo Bonzini6bee44e2011-11-14 17:50:52 +0100172 assert(dbs->acb);
aliguori59a703e2009-02-05 21:23:58 +0000173}
174
Christoph Hellwigc16b5a22009-05-25 12:37:32 +0200175static void dma_aio_cancel(BlockDriverAIOCB *acb)
176{
177 DMAAIOCB *dbs = container_of(acb, DMAAIOCB, common);
178
Kevin Wolfc57c4652011-11-24 06:15:28 -0500179 trace_dma_aio_cancel(dbs);
180
Christoph Hellwigc16b5a22009-05-25 12:37:32 +0200181 if (dbs->acb) {
Paolo Bonzinic3adb5b2011-09-16 16:40:02 +0200182 BlockDriverAIOCB *acb = dbs->acb;
183 dbs->acb = NULL;
184 dbs->in_cancel = true;
185 bdrv_aio_cancel(acb);
186 dbs->in_cancel = false;
Christoph Hellwigc16b5a22009-05-25 12:37:32 +0200187 }
Paolo Bonzinic3adb5b2011-09-16 16:40:02 +0200188 dbs->common.cb = NULL;
189 dma_complete(dbs, 0);
Christoph Hellwigc16b5a22009-05-25 12:37:32 +0200190}
191
Stefan Hajnoczid7331be2012-10-31 16:34:37 +0100192static const AIOCBInfo dma_aiocb_info = {
Christoph Hellwigc16b5a22009-05-25 12:37:32 +0200193 .aiocb_size = sizeof(DMAAIOCB),
194 .cancel = dma_aio_cancel,
195};
196
Christoph Hellwigcb144cc2011-05-19 10:57:59 +0200197BlockDriverAIOCB *dma_bdrv_io(
aliguori59a703e2009-02-05 21:23:58 +0000198 BlockDriverState *bs, QEMUSGList *sg, uint64_t sector_num,
Christoph Hellwigcb144cc2011-05-19 10:57:59 +0200199 DMAIOFunc *io_func, BlockDriverCompletionFunc *cb,
David Gibson43cf8ae2012-03-27 13:42:23 +1100200 void *opaque, DMADirection dir)
aliguori59a703e2009-02-05 21:23:58 +0000201{
Stefan Hajnoczid7331be2012-10-31 16:34:37 +0100202 DMAAIOCB *dbs = qemu_aio_get(&dma_aiocb_info, bs, cb, opaque);
aliguori59a703e2009-02-05 21:23:58 +0000203
David Gibson43cf8ae2012-03-27 13:42:23 +1100204 trace_dma_bdrv_io(dbs, bs, sector_num, (dir == DMA_DIRECTION_TO_DEVICE));
Kevin Wolfc57c4652011-11-24 06:15:28 -0500205
aliguori37b78422009-03-20 18:26:16 +0000206 dbs->acb = NULL;
aliguori59a703e2009-02-05 21:23:58 +0000207 dbs->bs = bs;
aliguori59a703e2009-02-05 21:23:58 +0000208 dbs->sg = sg;
209 dbs->sector_num = sector_num;
210 dbs->sg_cur_index = 0;
211 dbs->sg_cur_byte = 0;
David Gibson43cf8ae2012-03-27 13:42:23 +1100212 dbs->dir = dir;
Christoph Hellwigcb144cc2011-05-19 10:57:59 +0200213 dbs->io_func = io_func;
aliguori59a703e2009-02-05 21:23:58 +0000214 dbs->bh = NULL;
215 qemu_iovec_init(&dbs->iov, sg->nsg);
216 dma_bdrv_cb(dbs, 0);
aliguori37b78422009-03-20 18:26:16 +0000217 return &dbs->common;
aliguori59a703e2009-02-05 21:23:58 +0000218}
219
220
221BlockDriverAIOCB *dma_bdrv_read(BlockDriverState *bs,
222 QEMUSGList *sg, uint64_t sector,
223 void (*cb)(void *opaque, int ret), void *opaque)
224{
David Gibson43cf8ae2012-03-27 13:42:23 +1100225 return dma_bdrv_io(bs, sg, sector, bdrv_aio_readv, cb, opaque,
226 DMA_DIRECTION_FROM_DEVICE);
aliguori59a703e2009-02-05 21:23:58 +0000227}
228
229BlockDriverAIOCB *dma_bdrv_write(BlockDriverState *bs,
230 QEMUSGList *sg, uint64_t sector,
231 void (*cb)(void *opaque, int ret), void *opaque)
232{
David Gibson43cf8ae2012-03-27 13:42:23 +1100233 return dma_bdrv_io(bs, sg, sector, bdrv_aio_writev, cb, opaque,
234 DMA_DIRECTION_TO_DEVICE);
aliguori59a703e2009-02-05 21:23:58 +0000235}
Paolo Bonzini8171ee32011-07-06 08:02:14 +0200236
237
David Gibsonc65bcef2012-06-27 14:50:40 +1000238static uint64_t dma_buf_rw(uint8_t *ptr, int32_t len, QEMUSGList *sg,
239 DMADirection dir)
Paolo Bonzini8171ee32011-07-06 08:02:14 +0200240{
241 uint64_t resid;
242 int sg_cur_index;
243
244 resid = sg->size;
245 sg_cur_index = 0;
246 len = MIN(len, resid);
247 while (len > 0) {
248 ScatterGatherEntry entry = sg->sg[sg_cur_index++];
249 int32_t xfer = MIN(len, entry.len);
David Gibsonc65bcef2012-06-27 14:50:40 +1000250 dma_memory_rw(sg->dma, entry.base, ptr, xfer, dir);
Paolo Bonzini8171ee32011-07-06 08:02:14 +0200251 ptr += xfer;
252 len -= xfer;
253 resid -= xfer;
254 }
255
256 return resid;
257}
258
259uint64_t dma_buf_read(uint8_t *ptr, int32_t len, QEMUSGList *sg)
260{
David Gibsonc65bcef2012-06-27 14:50:40 +1000261 return dma_buf_rw(ptr, len, sg, DMA_DIRECTION_FROM_DEVICE);
Paolo Bonzini8171ee32011-07-06 08:02:14 +0200262}
263
264uint64_t dma_buf_write(uint8_t *ptr, int32_t len, QEMUSGList *sg)
265{
David Gibsonc65bcef2012-06-27 14:50:40 +1000266 return dma_buf_rw(ptr, len, sg, DMA_DIRECTION_TO_DEVICE);
Paolo Bonzini8171ee32011-07-06 08:02:14 +0200267}
Paolo Bonzini84a69352011-09-05 14:20:29 +0200268
269void dma_acct_start(BlockDriverState *bs, BlockAcctCookie *cookie,
270 QEMUSGList *sg, enum BlockAcctType type)
271{
272 bdrv_acct_start(bs, cookie, sg->size, type);
273}
David Gibsone5332e62012-06-27 14:50:43 +1000274
Paolo Bonzini24addbc2013-04-10 17:49:04 +0200275void dma_context_init(DMAContext *dma, AddressSpace *as)
David Gibsone5332e62012-06-27 14:50:43 +1000276{
277#ifdef DEBUG_IOMMU
Paolo Bonzini24addbc2013-04-10 17:49:04 +0200278 fprintf(stderr, "dma_context_init(%p -> %p)\n", dma, as);
David Gibsone5332e62012-06-27 14:50:43 +1000279#endif
Avi Kivityb90600e2012-10-03 16:42:37 +0200280 dma->as = as;
David Gibsone5332e62012-06-27 14:50:43 +1000281}