dma-helpers.c 4.6 KB
Newer Older
1 2 3 4 5 6 7 8 9 10
/*
 * 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.
 */

#include "dma.h"
11
#include "block_int.h"
12 13 14

void qemu_sglist_init(QEMUSGList *qsg, int alloc_hint)
{
15
    qsg->sg = g_malloc(alloc_hint * sizeof(ScatterGatherEntry));
16 17 18 19 20
    qsg->nsg = 0;
    qsg->nalloc = alloc_hint;
    qsg->size = 0;
}

A
Anthony Liguori 已提交
21 22
void qemu_sglist_add(QEMUSGList *qsg, target_phys_addr_t base,
                     target_phys_addr_t len)
23 24 25
{
    if (qsg->nsg == qsg->nalloc) {
        qsg->nalloc = 2 * qsg->nalloc + 1;
26
        qsg->sg = g_realloc(qsg->sg, qsg->nalloc * sizeof(ScatterGatherEntry));
27 28 29 30 31 32 33 34 35
    }
    qsg->sg[qsg->nsg].base = base;
    qsg->sg[qsg->nsg].len = len;
    qsg->size += len;
    ++qsg->nsg;
}

void qemu_sglist_destroy(QEMUSGList *qsg)
{
36
    g_free(qsg->sg);
37 38
}

39
typedef struct {
40
    BlockDriverAIOCB common;
41 42 43 44
    BlockDriverState *bs;
    BlockDriverAIOCB *acb;
    QEMUSGList *sg;
    uint64_t sector_num;
45
    bool to_dev;
46
    int sg_cur_index;
A
Anthony Liguori 已提交
47
    target_phys_addr_t sg_cur_byte;
48 49
    QEMUIOVector iov;
    QEMUBH *bh;
50
    DMAIOFunc *io_func;
51
} DMAAIOCB;
52 53 54 55 56

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

static void reschedule_dma(void *opaque)
{
57
    DMAAIOCB *dbs = (DMAAIOCB *)opaque;
58 59 60 61 62 63 64 65

    qemu_bh_delete(dbs->bh);
    dbs->bh = NULL;
    dma_bdrv_cb(opaque, 0);
}

static void continue_after_map_failure(void *opaque)
{
66
    DMAAIOCB *dbs = (DMAAIOCB *)opaque;
67 68 69 70 71

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

72
static void dma_bdrv_unmap(DMAAIOCB *dbs)
73 74 75 76 77
{
    int i;

    for (i = 0; i < dbs->iov.niov; ++i) {
        cpu_physical_memory_unmap(dbs->iov.iov[i].iov_base,
78
                                  dbs->iov.iov[i].iov_len, !dbs->to_dev,
79 80
                                  dbs->iov.iov[i].iov_len);
    }
81 82
}

B
blueswir1 已提交
83
static void dma_bdrv_cb(void *opaque, int ret)
84 85
{
    DMAAIOCB *dbs = (DMAAIOCB *)opaque;
A
Anthony Liguori 已提交
86
    target_phys_addr_t cur_addr, cur_len;
87 88 89 90 91
    void *mem;

    dbs->acb = NULL;
    dbs->sector_num += dbs->iov.size / 512;
    dma_bdrv_unmap(dbs);
92 93 94
    qemu_iovec_reset(&dbs->iov);

    if (dbs->sg_cur_index == dbs->sg->nsg || ret < 0) {
95
        dbs->common.cb(dbs->common.opaque, ret);
96
        qemu_iovec_destroy(&dbs->iov);
97
        qemu_aio_release(dbs);
98 99 100 101 102 103
        return;
    }

    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;
104
        mem = cpu_physical_memory_map(cur_addr, &cur_len, !dbs->to_dev);
105 106 107 108 109 110 111 112 113 114 115 116 117 118 119
        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) {
        cpu_register_map_client(dbs, continue_after_map_failure);
        return;
    }

120 121
    dbs->acb = dbs->io_func(dbs->bs, dbs->sector_num, &dbs->iov,
                            dbs->iov.size / 512, dma_bdrv_cb, dbs);
122 123 124 125 126
    if (!dbs->acb) {
        dma_bdrv_unmap(dbs);
        qemu_iovec_destroy(&dbs->iov);
        return;
    }
127 128
}

129 130 131 132 133 134 135 136 137 138 139 140 141 142
static void dma_aio_cancel(BlockDriverAIOCB *acb)
{
    DMAAIOCB *dbs = container_of(acb, DMAAIOCB, common);

    if (dbs->acb) {
        bdrv_aio_cancel(dbs->acb);
    }
}

static AIOPool dma_aio_pool = {
    .aiocb_size         = sizeof(DMAAIOCB),
    .cancel             = dma_aio_cancel,
};

143
BlockDriverAIOCB *dma_bdrv_io(
144
    BlockDriverState *bs, QEMUSGList *sg, uint64_t sector_num,
145
    DMAIOFunc *io_func, BlockDriverCompletionFunc *cb,
146
    void *opaque, bool to_dev)
147
{
148
    DMAAIOCB *dbs = qemu_aio_get(&dma_aio_pool, bs, cb, opaque);
149

150
    dbs->acb = NULL;
151 152 153 154 155
    dbs->bs = bs;
    dbs->sg = sg;
    dbs->sector_num = sector_num;
    dbs->sg_cur_index = 0;
    dbs->sg_cur_byte = 0;
156
    dbs->to_dev = to_dev;
157
    dbs->io_func = io_func;
158 159 160
    dbs->bh = NULL;
    qemu_iovec_init(&dbs->iov, sg->nsg);
    dma_bdrv_cb(dbs, 0);
161 162 163 164
    if (!dbs->acb) {
        qemu_aio_release(dbs);
        return NULL;
    }
165
    return &dbs->common;
166 167 168 169 170 171 172
}


BlockDriverAIOCB *dma_bdrv_read(BlockDriverState *bs,
                                QEMUSGList *sg, uint64_t sector,
                                void (*cb)(void *opaque, int ret), void *opaque)
{
173
    return dma_bdrv_io(bs, sg, sector, bdrv_aio_readv, cb, opaque, false);
174 175 176 177 178 179
}

BlockDriverAIOCB *dma_bdrv_write(BlockDriverState *bs,
                                 QEMUSGList *sg, uint64_t sector,
                                 void (*cb)(void *opaque, int ret), void *opaque)
{
180
    return dma_bdrv_io(bs, sg, sector, bdrv_aio_writev, cb, opaque, true);
181
}