dma-helpers.c 6.7 KB
Newer Older
1 2 3 4 5 6 7 8 9
/*
 * DMA helper functions
 *
 * Copyright (c) 2009 Red Hat
 *
 * This work is licensed under the terms of the GNU General Public License
 * (GNU GPL), version 2 or later.
 */

10
#include "sysemu/dma.h"
K
Kevin Wolf 已提交
11
#include "trace.h"
12 13
#include "qemu/range.h"
#include "qemu/thread.h"
14
#include "qemu/main-loop.h"
15

16 17
/* #define DEBUG_IOMMU */

P
Paolo Bonzini 已提交
18
int dma_memory_set(AddressSpace *as, dma_addr_t addr, uint8_t c, dma_addr_t len)
19
{
P
Paolo Bonzini 已提交
20
    dma_barrier(as, DMA_DIRECTION_FROM_DEVICE);
21

22 23 24
#define FILLBUF_SIZE 512
    uint8_t fillbuf[FILLBUF_SIZE];
    int l;
25
    bool error = false;
26 27 28 29

    memset(fillbuf, c, FILLBUF_SIZE);
    while (len > 0) {
        l = len < FILLBUF_SIZE ? len : FILLBUF_SIZE;
30
        error |= address_space_rw(as, addr, fillbuf, l, true);
31 32
        len -= l;
        addr += l;
33
    }
34

35
    return error;
36 37
}

38 39
void qemu_sglist_init(QEMUSGList *qsg, DeviceState *dev, int alloc_hint,
                      AddressSpace *as)
40
{
41
    qsg->sg = g_malloc(alloc_hint * sizeof(ScatterGatherEntry));
42 43 44
    qsg->nsg = 0;
    qsg->nalloc = alloc_hint;
    qsg->size = 0;
P
Paolo Bonzini 已提交
45
    qsg->as = as;
46 47
    qsg->dev = dev;
    object_ref(OBJECT(dev));
48 49
}

50
void qemu_sglist_add(QEMUSGList *qsg, dma_addr_t base, dma_addr_t len)
51 52 53
{
    if (qsg->nsg == qsg->nalloc) {
        qsg->nalloc = 2 * qsg->nalloc + 1;
54
        qsg->sg = g_realloc(qsg->sg, qsg->nalloc * sizeof(ScatterGatherEntry));
55 56 57 58 59 60 61 62 63
    }
    qsg->sg[qsg->nsg].base = base;
    qsg->sg[qsg->nsg].len = len;
    qsg->size += len;
    ++qsg->nsg;
}

void qemu_sglist_destroy(QEMUSGList *qsg)
{
64
    object_unref(OBJECT(qsg->dev));
65
    g_free(qsg->sg);
66
    memset(qsg, 0, sizeof(*qsg));
67 68
}

69
typedef struct {
70
    BlockAIOCB common;
71
    BlockDriverState *bs;
72
    BlockAIOCB *acb;
73 74
    QEMUSGList *sg;
    uint64_t sector_num;
75
    DMADirection dir;
76
    int sg_cur_index;
77
    dma_addr_t sg_cur_byte;
78 79
    QEMUIOVector iov;
    QEMUBH *bh;
80
    DMAIOFunc *io_func;
81
} DMAAIOCB;
82 83 84 85 86

static void dma_bdrv_cb(void *opaque, int ret);

static void reschedule_dma(void *opaque)
{
87
    DMAAIOCB *dbs = (DMAAIOCB *)opaque;
88 89 90

    qemu_bh_delete(dbs->bh);
    dbs->bh = NULL;
91
    dma_bdrv_cb(dbs, 0);
92 93 94 95
}

static void continue_after_map_failure(void *opaque)
{
96
    DMAAIOCB *dbs = (DMAAIOCB *)opaque;
97 98 99 100 101

    dbs->bh = qemu_bh_new(reschedule_dma, dbs);
    qemu_bh_schedule(dbs->bh);
}

102
static void dma_bdrv_unmap(DMAAIOCB *dbs)
103 104 105 106
{
    int i;

    for (i = 0; i < dbs->iov.niov; ++i) {
P
Paolo Bonzini 已提交
107
        dma_memory_unmap(dbs->sg->as, dbs->iov.iov[i].iov_base,
108 109
                         dbs->iov.iov[i].iov_len, dbs->dir,
                         dbs->iov.iov[i].iov_len);
110
    }
111 112 113 114 115
    qemu_iovec_reset(&dbs->iov);
}

static void dma_complete(DMAAIOCB *dbs, int ret)
{
K
Kevin Wolf 已提交
116 117
    trace_dma_complete(dbs, ret, dbs->common.cb);

118 119 120 121 122 123 124 125 126
    dma_bdrv_unmap(dbs);
    if (dbs->common.cb) {
        dbs->common.cb(dbs->common.opaque, ret);
    }
    qemu_iovec_destroy(&dbs->iov);
    if (dbs->bh) {
        qemu_bh_delete(dbs->bh);
        dbs->bh = NULL;
    }
127
    qemu_aio_unref(dbs);
128 129
}

B
blueswir1 已提交
130
static void dma_bdrv_cb(void *opaque, int ret)
131 132
{
    DMAAIOCB *dbs = (DMAAIOCB *)opaque;
133
    dma_addr_t cur_addr, cur_len;
134 135
    void *mem;

K
Kevin Wolf 已提交
136 137
    trace_dma_bdrv_cb(dbs, ret);

138 139
    dbs->acb = NULL;
    dbs->sector_num += dbs->iov.size / 512;
140 141

    if (dbs->sg_cur_index == dbs->sg->nsg || ret < 0) {
142
        dma_complete(dbs, ret);
143 144
        return;
    }
145
    dma_bdrv_unmap(dbs);
146 147 148 149

    while (dbs->sg_cur_index < dbs->sg->nsg) {
        cur_addr = dbs->sg->sg[dbs->sg_cur_index].base + dbs->sg_cur_byte;
        cur_len = dbs->sg->sg[dbs->sg_cur_index].len - dbs->sg_cur_byte;
P
Paolo Bonzini 已提交
150
        mem = dma_memory_map(dbs->sg->as, cur_addr, &cur_len, dbs->dir);
151 152 153 154 155 156 157 158 159 160 161
        if (!mem)
            break;
        qemu_iovec_add(&dbs->iov, mem, cur_len);
        dbs->sg_cur_byte += cur_len;
        if (dbs->sg_cur_byte == dbs->sg->sg[dbs->sg_cur_index].len) {
            dbs->sg_cur_byte = 0;
            ++dbs->sg_cur_index;
        }
    }

    if (dbs->iov.size == 0) {
K
Kevin Wolf 已提交
162
        trace_dma_map_wait(dbs);
163 164 165 166
        cpu_register_map_client(dbs, continue_after_map_failure);
        return;
    }

K
Kevin Wolf 已提交
167 168 169 170
    if (dbs->iov.size & ~BDRV_SECTOR_MASK) {
        qemu_iovec_discard_back(&dbs->iov, dbs->iov.size & ~BDRV_SECTOR_MASK);
    }

171 172
    dbs->acb = dbs->io_func(dbs->bs, dbs->sector_num, &dbs->iov,
                            dbs->iov.size / 512, dma_bdrv_cb, dbs);
173
    assert(dbs->acb);
174 175
}

176
static void dma_aio_cancel(BlockAIOCB *acb)
177 178 179
{
    DMAAIOCB *dbs = container_of(acb, DMAAIOCB, common);

K
Kevin Wolf 已提交
180 181
    trace_dma_aio_cancel(dbs);

182
    if (dbs->acb) {
183
        bdrv_aio_cancel_async(dbs->acb);
184 185 186
    }
}

187

S
Stefan Hajnoczi 已提交
188
static const AIOCBInfo dma_aiocb_info = {
189
    .aiocb_size         = sizeof(DMAAIOCB),
190
    .cancel_async       = dma_aio_cancel,
191 192
};

193
BlockAIOCB *dma_bdrv_io(
194
    BlockDriverState *bs, QEMUSGList *sg, uint64_t sector_num,
195
    DMAIOFunc *io_func, BlockCompletionFunc *cb,
196
    void *opaque, DMADirection dir)
197
{
S
Stefan Hajnoczi 已提交
198
    DMAAIOCB *dbs = qemu_aio_get(&dma_aiocb_info, bs, cb, opaque);
199

200
    trace_dma_bdrv_io(dbs, bs, sector_num, (dir == DMA_DIRECTION_TO_DEVICE));
K
Kevin Wolf 已提交
201

202
    dbs->acb = NULL;
203 204 205 206 207
    dbs->bs = bs;
    dbs->sg = sg;
    dbs->sector_num = sector_num;
    dbs->sg_cur_index = 0;
    dbs->sg_cur_byte = 0;
208
    dbs->dir = dir;
209
    dbs->io_func = io_func;
210 211 212
    dbs->bh = NULL;
    qemu_iovec_init(&dbs->iov, sg->nsg);
    dma_bdrv_cb(dbs, 0);
213
    return &dbs->common;
214 215 216
}


217 218 219
BlockAIOCB *dma_bdrv_read(BlockDriverState *bs,
                          QEMUSGList *sg, uint64_t sector,
                          void (*cb)(void *opaque, int ret), void *opaque)
220
{
221 222
    return dma_bdrv_io(bs, sg, sector, bdrv_aio_readv, cb, opaque,
                       DMA_DIRECTION_FROM_DEVICE);
223 224
}

225 226 227
BlockAIOCB *dma_bdrv_write(BlockDriverState *bs,
                           QEMUSGList *sg, uint64_t sector,
                           void (*cb)(void *opaque, int ret), void *opaque)
228
{
229 230
    return dma_bdrv_io(bs, sg, sector, bdrv_aio_writev, cb, opaque,
                       DMA_DIRECTION_TO_DEVICE);
231
}
232 233


234 235
static uint64_t dma_buf_rw(uint8_t *ptr, int32_t len, QEMUSGList *sg,
                           DMADirection dir)
236 237 238 239 240 241 242 243 244 245
{
    uint64_t resid;
    int sg_cur_index;

    resid = sg->size;
    sg_cur_index = 0;
    len = MIN(len, resid);
    while (len > 0) {
        ScatterGatherEntry entry = sg->sg[sg_cur_index++];
        int32_t xfer = MIN(len, entry.len);
P
Paolo Bonzini 已提交
246
        dma_memory_rw(sg->as, entry.base, ptr, xfer, dir);
247 248 249 250 251 252 253 254 255 256
        ptr += xfer;
        len -= xfer;
        resid -= xfer;
    }

    return resid;
}

uint64_t dma_buf_read(uint8_t *ptr, int32_t len, QEMUSGList *sg)
{
257
    return dma_buf_rw(ptr, len, sg, DMA_DIRECTION_FROM_DEVICE);
258 259 260 261
}

uint64_t dma_buf_write(uint8_t *ptr, int32_t len, QEMUSGList *sg)
{
262
    return dma_buf_rw(ptr, len, sg, DMA_DIRECTION_TO_DEVICE);
263
}
264 265 266 267

void dma_acct_start(BlockDriverState *bs, BlockAcctCookie *cookie,
                    QEMUSGList *sg, enum BlockAcctType type)
{
268
    block_acct_start(bdrv_get_stats(bs), cookie, sg->size, type);
269
}