blob: 5afba47478c2bb70086ac31f387e3345e7d64b44 [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 Bonzinidf32fd12013-04-10 18:15:49 +020037void qemu_sglist_init(QEMUSGList *qsg, int alloc_hint, AddressSpace *as)
aliguori244ab902009-02-05 21:23:50 +000038{
Anthony Liguori7267c092011-08-20 22:09:37 -050039 qsg->sg = g_malloc(alloc_hint * sizeof(ScatterGatherEntry));
aliguori244ab902009-02-05 21:23:50 +000040 qsg->nsg = 0;
41 qsg->nalloc = alloc_hint;
42 qsg->size = 0;
Paolo Bonzinidf32fd12013-04-10 18:15:49 +020043 qsg->as = as;
aliguori244ab902009-02-05 21:23:50 +000044}
45
David Gibsond3231182011-10-31 17:06:46 +110046void qemu_sglist_add(QEMUSGList *qsg, dma_addr_t base, dma_addr_t len)
aliguori244ab902009-02-05 21:23:50 +000047{
48 if (qsg->nsg == qsg->nalloc) {
49 qsg->nalloc = 2 * qsg->nalloc + 1;
Anthony Liguori7267c092011-08-20 22:09:37 -050050 qsg->sg = g_realloc(qsg->sg, qsg->nalloc * sizeof(ScatterGatherEntry));
aliguori244ab902009-02-05 21:23:50 +000051 }
52 qsg->sg[qsg->nsg].base = base;
53 qsg->sg[qsg->nsg].len = len;
54 qsg->size += len;
55 ++qsg->nsg;
56}
57
58void qemu_sglist_destroy(QEMUSGList *qsg)
59{
Anthony Liguori7267c092011-08-20 22:09:37 -050060 g_free(qsg->sg);
Jason Baronea8d82a2012-08-03 15:57:10 -040061 memset(qsg, 0, sizeof(*qsg));
aliguori244ab902009-02-05 21:23:50 +000062}
63
aliguori59a703e2009-02-05 21:23:58 +000064typedef struct {
aliguori37b78422009-03-20 18:26:16 +000065 BlockDriverAIOCB common;
aliguori59a703e2009-02-05 21:23:58 +000066 BlockDriverState *bs;
67 BlockDriverAIOCB *acb;
68 QEMUSGList *sg;
69 uint64_t sector_num;
David Gibson43cf8ae2012-03-27 13:42:23 +110070 DMADirection dir;
Paolo Bonzinic3adb5b2011-09-16 16:40:02 +020071 bool in_cancel;
aliguori59a703e2009-02-05 21:23:58 +000072 int sg_cur_index;
David Gibsond3231182011-10-31 17:06:46 +110073 dma_addr_t sg_cur_byte;
aliguori59a703e2009-02-05 21:23:58 +000074 QEMUIOVector iov;
75 QEMUBH *bh;
Christoph Hellwigcb144cc2011-05-19 10:57:59 +020076 DMAIOFunc *io_func;
aliguori37b78422009-03-20 18:26:16 +000077} DMAAIOCB;
aliguori59a703e2009-02-05 21:23:58 +000078
79static void dma_bdrv_cb(void *opaque, int ret);
80
81static void reschedule_dma(void *opaque)
82{
aliguori37b78422009-03-20 18:26:16 +000083 DMAAIOCB *dbs = (DMAAIOCB *)opaque;
aliguori59a703e2009-02-05 21:23:58 +000084
85 qemu_bh_delete(dbs->bh);
86 dbs->bh = NULL;
Paolo Bonzinic3adb5b2011-09-16 16:40:02 +020087 dma_bdrv_cb(dbs, 0);
aliguori59a703e2009-02-05 21:23:58 +000088}
89
90static void continue_after_map_failure(void *opaque)
91{
aliguori37b78422009-03-20 18:26:16 +000092 DMAAIOCB *dbs = (DMAAIOCB *)opaque;
aliguori59a703e2009-02-05 21:23:58 +000093
94 dbs->bh = qemu_bh_new(reschedule_dma, dbs);
95 qemu_bh_schedule(dbs->bh);
96}
97
aliguori7403b142009-03-28 16:11:25 +000098static void dma_bdrv_unmap(DMAAIOCB *dbs)
aliguori59a703e2009-02-05 21:23:58 +000099{
aliguori59a703e2009-02-05 21:23:58 +0000100 int i;
101
aliguori59a703e2009-02-05 21:23:58 +0000102 for (i = 0; i < dbs->iov.niov; ++i) {
Paolo Bonzinidf32fd12013-04-10 18:15:49 +0200103 dma_memory_unmap(dbs->sg->as, dbs->iov.iov[i].iov_base,
David Gibsonc65bcef2012-06-27 14:50:40 +1000104 dbs->iov.iov[i].iov_len, dbs->dir,
105 dbs->iov.iov[i].iov_len);
aliguori59a703e2009-02-05 21:23:58 +0000106 }
Paolo Bonzinic3adb5b2011-09-16 16:40:02 +0200107 qemu_iovec_reset(&dbs->iov);
108}
109
110static void dma_complete(DMAAIOCB *dbs, int ret)
111{
Kevin Wolfc57c4652011-11-24 06:15:28 -0500112 trace_dma_complete(dbs, ret, dbs->common.cb);
113
Paolo Bonzinic3adb5b2011-09-16 16:40:02 +0200114 dma_bdrv_unmap(dbs);
115 if (dbs->common.cb) {
116 dbs->common.cb(dbs->common.opaque, ret);
117 }
118 qemu_iovec_destroy(&dbs->iov);
119 if (dbs->bh) {
120 qemu_bh_delete(dbs->bh);
121 dbs->bh = NULL;
122 }
123 if (!dbs->in_cancel) {
124 /* Requests may complete while dma_aio_cancel is in progress. In
125 * this case, the AIOCB should not be released because it is still
126 * referenced by dma_aio_cancel. */
127 qemu_aio_release(dbs);
128 }
aliguori7403b142009-03-28 16:11:25 +0000129}
130
blueswir1856ae5c2009-04-07 17:57:09 +0000131static void dma_bdrv_cb(void *opaque, int ret)
aliguori7403b142009-03-28 16:11:25 +0000132{
133 DMAAIOCB *dbs = (DMAAIOCB *)opaque;
David Gibsonc65bcef2012-06-27 14:50:40 +1000134 dma_addr_t cur_addr, cur_len;
aliguori7403b142009-03-28 16:11:25 +0000135 void *mem;
136
Kevin Wolfc57c4652011-11-24 06:15:28 -0500137 trace_dma_bdrv_cb(dbs, ret);
138
aliguori7403b142009-03-28 16:11:25 +0000139 dbs->acb = NULL;
140 dbs->sector_num += dbs->iov.size / 512;
141 dma_bdrv_unmap(dbs);
aliguori59a703e2009-02-05 21:23:58 +0000142
143 if (dbs->sg_cur_index == dbs->sg->nsg || ret < 0) {
Paolo Bonzinic3adb5b2011-09-16 16:40:02 +0200144 dma_complete(dbs, ret);
aliguori59a703e2009-02-05 21:23:58 +0000145 return;
146 }
147
148 while (dbs->sg_cur_index < dbs->sg->nsg) {
149 cur_addr = dbs->sg->sg[dbs->sg_cur_index].base + dbs->sg_cur_byte;
150 cur_len = dbs->sg->sg[dbs->sg_cur_index].len - dbs->sg_cur_byte;
Paolo Bonzinidf32fd12013-04-10 18:15:49 +0200151 mem = dma_memory_map(dbs->sg->as, cur_addr, &cur_len, dbs->dir);
aliguori59a703e2009-02-05 21:23:58 +0000152 if (!mem)
153 break;
154 qemu_iovec_add(&dbs->iov, mem, cur_len);
155 dbs->sg_cur_byte += cur_len;
156 if (dbs->sg_cur_byte == dbs->sg->sg[dbs->sg_cur_index].len) {
157 dbs->sg_cur_byte = 0;
158 ++dbs->sg_cur_index;
159 }
160 }
161
162 if (dbs->iov.size == 0) {
Kevin Wolfc57c4652011-11-24 06:15:28 -0500163 trace_dma_map_wait(dbs);
aliguori59a703e2009-02-05 21:23:58 +0000164 cpu_register_map_client(dbs, continue_after_map_failure);
165 return;
166 }
167
Christoph Hellwigcb144cc2011-05-19 10:57:59 +0200168 dbs->acb = dbs->io_func(dbs->bs, dbs->sector_num, &dbs->iov,
169 dbs->iov.size / 512, dma_bdrv_cb, dbs);
Paolo Bonzini6bee44e2011-11-14 17:50:52 +0100170 assert(dbs->acb);
aliguori59a703e2009-02-05 21:23:58 +0000171}
172
Christoph Hellwigc16b5a22009-05-25 12:37:32 +0200173static void dma_aio_cancel(BlockDriverAIOCB *acb)
174{
175 DMAAIOCB *dbs = container_of(acb, DMAAIOCB, common);
176
Kevin Wolfc57c4652011-11-24 06:15:28 -0500177 trace_dma_aio_cancel(dbs);
178
Christoph Hellwigc16b5a22009-05-25 12:37:32 +0200179 if (dbs->acb) {
Paolo Bonzinic3adb5b2011-09-16 16:40:02 +0200180 BlockDriverAIOCB *acb = dbs->acb;
181 dbs->acb = NULL;
182 dbs->in_cancel = true;
183 bdrv_aio_cancel(acb);
184 dbs->in_cancel = false;
Christoph Hellwigc16b5a22009-05-25 12:37:32 +0200185 }
Paolo Bonzinic3adb5b2011-09-16 16:40:02 +0200186 dbs->common.cb = NULL;
187 dma_complete(dbs, 0);
Christoph Hellwigc16b5a22009-05-25 12:37:32 +0200188}
189
Stefan Hajnoczid7331be2012-10-31 16:34:37 +0100190static const AIOCBInfo dma_aiocb_info = {
Christoph Hellwigc16b5a22009-05-25 12:37:32 +0200191 .aiocb_size = sizeof(DMAAIOCB),
192 .cancel = dma_aio_cancel,
193};
194
Christoph Hellwigcb144cc2011-05-19 10:57:59 +0200195BlockDriverAIOCB *dma_bdrv_io(
aliguori59a703e2009-02-05 21:23:58 +0000196 BlockDriverState *bs, QEMUSGList *sg, uint64_t sector_num,
Christoph Hellwigcb144cc2011-05-19 10:57:59 +0200197 DMAIOFunc *io_func, BlockDriverCompletionFunc *cb,
David Gibson43cf8ae2012-03-27 13:42:23 +1100198 void *opaque, DMADirection dir)
aliguori59a703e2009-02-05 21:23:58 +0000199{
Stefan Hajnoczid7331be2012-10-31 16:34:37 +0100200 DMAAIOCB *dbs = qemu_aio_get(&dma_aiocb_info, bs, cb, opaque);
aliguori59a703e2009-02-05 21:23:58 +0000201
David Gibson43cf8ae2012-03-27 13:42:23 +1100202 trace_dma_bdrv_io(dbs, bs, sector_num, (dir == DMA_DIRECTION_TO_DEVICE));
Kevin Wolfc57c4652011-11-24 06:15:28 -0500203
aliguori37b78422009-03-20 18:26:16 +0000204 dbs->acb = NULL;
aliguori59a703e2009-02-05 21:23:58 +0000205 dbs->bs = bs;
aliguori59a703e2009-02-05 21:23:58 +0000206 dbs->sg = sg;
207 dbs->sector_num = sector_num;
208 dbs->sg_cur_index = 0;
209 dbs->sg_cur_byte = 0;
David Gibson43cf8ae2012-03-27 13:42:23 +1100210 dbs->dir = dir;
Christoph Hellwigcb144cc2011-05-19 10:57:59 +0200211 dbs->io_func = io_func;
aliguori59a703e2009-02-05 21:23:58 +0000212 dbs->bh = NULL;
213 qemu_iovec_init(&dbs->iov, sg->nsg);
214 dma_bdrv_cb(dbs, 0);
aliguori37b78422009-03-20 18:26:16 +0000215 return &dbs->common;
aliguori59a703e2009-02-05 21:23:58 +0000216}
217
218
219BlockDriverAIOCB *dma_bdrv_read(BlockDriverState *bs,
220 QEMUSGList *sg, uint64_t sector,
221 void (*cb)(void *opaque, int ret), void *opaque)
222{
David Gibson43cf8ae2012-03-27 13:42:23 +1100223 return dma_bdrv_io(bs, sg, sector, bdrv_aio_readv, cb, opaque,
224 DMA_DIRECTION_FROM_DEVICE);
aliguori59a703e2009-02-05 21:23:58 +0000225}
226
227BlockDriverAIOCB *dma_bdrv_write(BlockDriverState *bs,
228 QEMUSGList *sg, uint64_t sector,
229 void (*cb)(void *opaque, int ret), void *opaque)
230{
David Gibson43cf8ae2012-03-27 13:42:23 +1100231 return dma_bdrv_io(bs, sg, sector, bdrv_aio_writev, cb, opaque,
232 DMA_DIRECTION_TO_DEVICE);
aliguori59a703e2009-02-05 21:23:58 +0000233}
Paolo Bonzini8171ee32011-07-06 08:02:14 +0200234
235
David Gibsonc65bcef2012-06-27 14:50:40 +1000236static uint64_t dma_buf_rw(uint8_t *ptr, int32_t len, QEMUSGList *sg,
237 DMADirection dir)
Paolo Bonzini8171ee32011-07-06 08:02:14 +0200238{
239 uint64_t resid;
240 int sg_cur_index;
241
242 resid = sg->size;
243 sg_cur_index = 0;
244 len = MIN(len, resid);
245 while (len > 0) {
246 ScatterGatherEntry entry = sg->sg[sg_cur_index++];
247 int32_t xfer = MIN(len, entry.len);
Paolo Bonzinidf32fd12013-04-10 18:15:49 +0200248 dma_memory_rw(sg->as, entry.base, ptr, xfer, dir);
Paolo Bonzini8171ee32011-07-06 08:02:14 +0200249 ptr += xfer;
250 len -= xfer;
251 resid -= xfer;
252 }
253
254 return resid;
255}
256
257uint64_t dma_buf_read(uint8_t *ptr, int32_t len, QEMUSGList *sg)
258{
David Gibsonc65bcef2012-06-27 14:50:40 +1000259 return dma_buf_rw(ptr, len, sg, DMA_DIRECTION_FROM_DEVICE);
Paolo Bonzini8171ee32011-07-06 08:02:14 +0200260}
261
262uint64_t dma_buf_write(uint8_t *ptr, int32_t len, QEMUSGList *sg)
263{
David Gibsonc65bcef2012-06-27 14:50:40 +1000264 return dma_buf_rw(ptr, len, sg, DMA_DIRECTION_TO_DEVICE);
Paolo Bonzini8171ee32011-07-06 08:02:14 +0200265}
Paolo Bonzini84a69352011-09-05 14:20:29 +0200266
267void dma_acct_start(BlockDriverState *bs, BlockAcctCookie *cookie,
268 QEMUSGList *sg, enum BlockAcctType type)
269{
270 bdrv_acct_start(bs, cookie, sg->size, type);
271}