4 * Copyright (c) 2009 Red Hat
6 * This work is licensed under the terms of the GNU General Public License
7 * (GNU GPL), version 2 or later.
11 #include "block_int.h"
13 void qemu_sglist_init(QEMUSGList
*qsg
, int alloc_hint
)
15 qsg
->sg
= g_malloc(alloc_hint
* sizeof(ScatterGatherEntry
));
17 qsg
->nalloc
= alloc_hint
;
21 void qemu_sglist_add(QEMUSGList
*qsg
, target_phys_addr_t base
,
22 target_phys_addr_t len
)
24 if (qsg
->nsg
== qsg
->nalloc
) {
25 qsg
->nalloc
= 2 * qsg
->nalloc
+ 1;
26 qsg
->sg
= g_realloc(qsg
->sg
, qsg
->nalloc
* sizeof(ScatterGatherEntry
));
28 qsg
->sg
[qsg
->nsg
].base
= base
;
29 qsg
->sg
[qsg
->nsg
].len
= len
;
34 void qemu_sglist_destroy(QEMUSGList
*qsg
)
40 BlockDriverAIOCB common
;
42 BlockDriverAIOCB
*acb
;
47 target_phys_addr_t sg_cur_byte
;
53 static void dma_bdrv_cb(void *opaque
, int ret
);
55 static void reschedule_dma(void *opaque
)
57 DMAAIOCB
*dbs
= (DMAAIOCB
*)opaque
;
59 qemu_bh_delete(dbs
->bh
);
61 dma_bdrv_cb(opaque
, 0);
64 static void continue_after_map_failure(void *opaque
)
66 DMAAIOCB
*dbs
= (DMAAIOCB
*)opaque
;
68 dbs
->bh
= qemu_bh_new(reschedule_dma
, dbs
);
69 qemu_bh_schedule(dbs
->bh
);
72 static void dma_bdrv_unmap(DMAAIOCB
*dbs
)
76 for (i
= 0; i
< dbs
->iov
.niov
; ++i
) {
77 cpu_physical_memory_unmap(dbs
->iov
.iov
[i
].iov_base
,
78 dbs
->iov
.iov
[i
].iov_len
, !dbs
->is_write
,
79 dbs
->iov
.iov
[i
].iov_len
);
83 static void dma_bdrv_cb(void *opaque
, int ret
)
85 DMAAIOCB
*dbs
= (DMAAIOCB
*)opaque
;
86 target_phys_addr_t cur_addr
, cur_len
;
90 dbs
->sector_num
+= dbs
->iov
.size
/ 512;
92 qemu_iovec_reset(&dbs
->iov
);
94 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
);
101 while (dbs
->sg_cur_index
< dbs
->sg
->nsg
) {
102 cur_addr
= dbs
->sg
->sg
[dbs
->sg_cur_index
].base
+ dbs
->sg_cur_byte
;
103 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
->is_write
);
107 qemu_iovec_add(&dbs
->iov
, mem
, cur_len
);
108 dbs
->sg_cur_byte
+= cur_len
;
109 if (dbs
->sg_cur_byte
== dbs
->sg
->sg
[dbs
->sg_cur_index
].len
) {
110 dbs
->sg_cur_byte
= 0;
115 if (dbs
->iov
.size
== 0) {
116 cpu_register_map_client(dbs
, continue_after_map_failure
);
120 dbs
->acb
= dbs
->io_func(dbs
->bs
, dbs
->sector_num
, &dbs
->iov
,
121 dbs
->iov
.size
/ 512, dma_bdrv_cb
, dbs
);
124 qemu_iovec_destroy(&dbs
->iov
);
129 static void dma_aio_cancel(BlockDriverAIOCB
*acb
)
131 DMAAIOCB
*dbs
= container_of(acb
, DMAAIOCB
, common
);
134 bdrv_aio_cancel(dbs
->acb
);
138 static AIOPool dma_aio_pool
= {
139 .aiocb_size
= sizeof(DMAAIOCB
),
140 .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
, int is_write
)
148 DMAAIOCB
*dbs
= qemu_aio_get(&dma_aio_pool
, bs
, cb
, opaque
);
153 dbs
->sector_num
= sector_num
;
154 dbs
->sg_cur_index
= 0;
155 dbs
->sg_cur_byte
= 0;
156 dbs
->is_write
= is_write
;
157 dbs
->io_func
= io_func
;
159 qemu_iovec_init(&dbs
->iov
, sg
->nsg
);
162 qemu_aio_release(dbs
);
169 BlockDriverAIOCB
*dma_bdrv_read(BlockDriverState
*bs
,
170 QEMUSGList
*sg
, uint64_t sector
,
171 void (*cb
)(void *opaque
, int ret
), void *opaque
)
173 return dma_bdrv_io(bs
, sg
, sector
, bdrv_aio_readv
, cb
, opaque
, 0);
176 BlockDriverAIOCB
*dma_bdrv_write(BlockDriverState
*bs
,
177 QEMUSGList
*sg
, uint64_t sector
,
178 void (*cb
)(void *opaque
, int ret
), void *opaque
)
180 return dma_bdrv_io(bs
, sg
, sector
, bdrv_aio_writev
, cb
, opaque
, 1);