]> git.proxmox.com Git - mirror_qemu.git/blame - dma-helpers.c
pseries: Fix qdev.id handling in the VIO bus code
[mirror_qemu.git] / dma-helpers.c
CommitLineData
244ab90e
AL
1/*
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
10#include "dma.h"
59a703eb 11#include "block_int.h"
244ab90e
AL
12
13void qemu_sglist_init(QEMUSGList *qsg, int alloc_hint)
14{
7267c094 15 qsg->sg = g_malloc(alloc_hint * sizeof(ScatterGatherEntry));
244ab90e
AL
16 qsg->nsg = 0;
17 qsg->nalloc = alloc_hint;
18 qsg->size = 0;
19}
20
d3231181 21void qemu_sglist_add(QEMUSGList *qsg, dma_addr_t base, dma_addr_t len)
244ab90e
AL
22{
23 if (qsg->nsg == qsg->nalloc) {
24 qsg->nalloc = 2 * qsg->nalloc + 1;
7267c094 25 qsg->sg = g_realloc(qsg->sg, qsg->nalloc * sizeof(ScatterGatherEntry));
244ab90e
AL
26 }
27 qsg->sg[qsg->nsg].base = base;
28 qsg->sg[qsg->nsg].len = len;
29 qsg->size += len;
30 ++qsg->nsg;
31}
32
33void qemu_sglist_destroy(QEMUSGList *qsg)
34{
7267c094 35 g_free(qsg->sg);
244ab90e
AL
36}
37
59a703eb 38typedef struct {
37b7842c 39 BlockDriverAIOCB common;
59a703eb
AL
40 BlockDriverState *bs;
41 BlockDriverAIOCB *acb;
42 QEMUSGList *sg;
43 uint64_t sector_num;
bbca72c6 44 bool to_dev;
c3adb5b9 45 bool in_cancel;
59a703eb 46 int sg_cur_index;
d3231181 47 dma_addr_t sg_cur_byte;
59a703eb
AL
48 QEMUIOVector iov;
49 QEMUBH *bh;
cb144ccb 50 DMAIOFunc *io_func;
37b7842c 51} DMAAIOCB;
59a703eb
AL
52
53static void dma_bdrv_cb(void *opaque, int ret);
54
55static void reschedule_dma(void *opaque)
56{
37b7842c 57 DMAAIOCB *dbs = (DMAAIOCB *)opaque;
59a703eb
AL
58
59 qemu_bh_delete(dbs->bh);
60 dbs->bh = NULL;
c3adb5b9 61 dma_bdrv_cb(dbs, 0);
59a703eb
AL
62}
63
64static void continue_after_map_failure(void *opaque)
65{
37b7842c 66 DMAAIOCB *dbs = (DMAAIOCB *)opaque;
59a703eb
AL
67
68 dbs->bh = qemu_bh_new(reschedule_dma, dbs);
69 qemu_bh_schedule(dbs->bh);
70}
71
7403b14e 72static void dma_bdrv_unmap(DMAAIOCB *dbs)
59a703eb 73{
59a703eb
AL
74 int i;
75
59a703eb
AL
76 for (i = 0; i < dbs->iov.niov; ++i) {
77 cpu_physical_memory_unmap(dbs->iov.iov[i].iov_base,
bbca72c6 78 dbs->iov.iov[i].iov_len, !dbs->to_dev,
59a703eb
AL
79 dbs->iov.iov[i].iov_len);
80 }
c3adb5b9
PB
81 qemu_iovec_reset(&dbs->iov);
82}
83
84static void dma_complete(DMAAIOCB *dbs, int ret)
85{
86 dma_bdrv_unmap(dbs);
87 if (dbs->common.cb) {
88 dbs->common.cb(dbs->common.opaque, ret);
89 }
90 qemu_iovec_destroy(&dbs->iov);
91 if (dbs->bh) {
92 qemu_bh_delete(dbs->bh);
93 dbs->bh = NULL;
94 }
95 if (!dbs->in_cancel) {
96 /* Requests may complete while dma_aio_cancel is in progress. In
97 * this case, the AIOCB should not be released because it is still
98 * referenced by dma_aio_cancel. */
99 qemu_aio_release(dbs);
100 }
7403b14e
AL
101}
102
856ae5c3 103static void dma_bdrv_cb(void *opaque, int ret)
7403b14e
AL
104{
105 DMAAIOCB *dbs = (DMAAIOCB *)opaque;
c227f099 106 target_phys_addr_t cur_addr, cur_len;
7403b14e
AL
107 void *mem;
108
109 dbs->acb = NULL;
110 dbs->sector_num += dbs->iov.size / 512;
111 dma_bdrv_unmap(dbs);
59a703eb
AL
112
113 if (dbs->sg_cur_index == dbs->sg->nsg || ret < 0) {
c3adb5b9 114 dma_complete(dbs, ret);
59a703eb
AL
115 return;
116 }
117
118 while (dbs->sg_cur_index < dbs->sg->nsg) {
119 cur_addr = dbs->sg->sg[dbs->sg_cur_index].base + dbs->sg_cur_byte;
120 cur_len = dbs->sg->sg[dbs->sg_cur_index].len - dbs->sg_cur_byte;
bbca72c6 121 mem = cpu_physical_memory_map(cur_addr, &cur_len, !dbs->to_dev);
59a703eb
AL
122 if (!mem)
123 break;
124 qemu_iovec_add(&dbs->iov, mem, cur_len);
125 dbs->sg_cur_byte += cur_len;
126 if (dbs->sg_cur_byte == dbs->sg->sg[dbs->sg_cur_index].len) {
127 dbs->sg_cur_byte = 0;
128 ++dbs->sg_cur_index;
129 }
130 }
131
132 if (dbs->iov.size == 0) {
133 cpu_register_map_client(dbs, continue_after_map_failure);
134 return;
135 }
136
cb144ccb
CH
137 dbs->acb = dbs->io_func(dbs->bs, dbs->sector_num, &dbs->iov,
138 dbs->iov.size / 512, dma_bdrv_cb, dbs);
7403b14e 139 if (!dbs->acb) {
c3adb5b9 140 dma_complete(dbs, -EIO);
7403b14e 141 }
59a703eb
AL
142}
143
c16b5a2c
CH
144static void dma_aio_cancel(BlockDriverAIOCB *acb)
145{
146 DMAAIOCB *dbs = container_of(acb, DMAAIOCB, common);
147
148 if (dbs->acb) {
c3adb5b9
PB
149 BlockDriverAIOCB *acb = dbs->acb;
150 dbs->acb = NULL;
151 dbs->in_cancel = true;
152 bdrv_aio_cancel(acb);
153 dbs->in_cancel = false;
c16b5a2c 154 }
c3adb5b9
PB
155 dbs->common.cb = NULL;
156 dma_complete(dbs, 0);
c16b5a2c
CH
157}
158
159static AIOPool dma_aio_pool = {
160 .aiocb_size = sizeof(DMAAIOCB),
161 .cancel = dma_aio_cancel,
162};
163
cb144ccb 164BlockDriverAIOCB *dma_bdrv_io(
59a703eb 165 BlockDriverState *bs, QEMUSGList *sg, uint64_t sector_num,
cb144ccb 166 DMAIOFunc *io_func, BlockDriverCompletionFunc *cb,
bbca72c6 167 void *opaque, bool to_dev)
59a703eb 168{
cb144ccb 169 DMAAIOCB *dbs = qemu_aio_get(&dma_aio_pool, bs, cb, opaque);
59a703eb 170
37b7842c 171 dbs->acb = NULL;
59a703eb 172 dbs->bs = bs;
59a703eb
AL
173 dbs->sg = sg;
174 dbs->sector_num = sector_num;
175 dbs->sg_cur_index = 0;
176 dbs->sg_cur_byte = 0;
bbca72c6 177 dbs->to_dev = to_dev;
cb144ccb 178 dbs->io_func = io_func;
59a703eb
AL
179 dbs->bh = NULL;
180 qemu_iovec_init(&dbs->iov, sg->nsg);
181 dma_bdrv_cb(dbs, 0);
37b7842c 182 return &dbs->common;
59a703eb
AL
183}
184
185
186BlockDriverAIOCB *dma_bdrv_read(BlockDriverState *bs,
187 QEMUSGList *sg, uint64_t sector,
188 void (*cb)(void *opaque, int ret), void *opaque)
189{
bbca72c6 190 return dma_bdrv_io(bs, sg, sector, bdrv_aio_readv, cb, opaque, false);
59a703eb
AL
191}
192
193BlockDriverAIOCB *dma_bdrv_write(BlockDriverState *bs,
194 QEMUSGList *sg, uint64_t sector,
195 void (*cb)(void *opaque, int ret), void *opaque)
196{
bbca72c6 197 return dma_bdrv_io(bs, sg, sector, bdrv_aio_writev, cb, opaque, true);
59a703eb 198}