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 static AIOPool dma_aio_pool
;
15 void qemu_sglist_init(QEMUSGList
*qsg
, int alloc_hint
)
17 qsg
->sg
= qemu_malloc(alloc_hint
* sizeof(ScatterGatherEntry
));
19 qsg
->nalloc
= alloc_hint
;
23 void qemu_sglist_add(QEMUSGList
*qsg
, target_phys_addr_t base
,
24 target_phys_addr_t len
)
26 if (qsg
->nsg
== qsg
->nalloc
) {
27 qsg
->nalloc
= 2 * qsg
->nalloc
+ 1;
28 qsg
->sg
= qemu_realloc(qsg
->sg
, qsg
->nalloc
* sizeof(ScatterGatherEntry
));
30 qsg
->sg
[qsg
->nsg
].base
= base
;
31 qsg
->sg
[qsg
->nsg
].len
= len
;
36 void qemu_sglist_destroy(QEMUSGList
*qsg
)
42 BlockDriverAIOCB common
;
44 BlockDriverAIOCB
*acb
;
49 target_phys_addr_t sg_cur_byte
;
54 static void dma_bdrv_cb(void *opaque
, int ret
);
56 static void reschedule_dma(void *opaque
)
58 DMAAIOCB
*dbs
= (DMAAIOCB
*)opaque
;
60 qemu_bh_delete(dbs
->bh
);
62 dma_bdrv_cb(opaque
, 0);
65 static void continue_after_map_failure(void *opaque
)
67 DMAAIOCB
*dbs
= (DMAAIOCB
*)opaque
;
69 dbs
->bh
= qemu_bh_new(reschedule_dma
, dbs
);
70 qemu_bh_schedule(dbs
->bh
);
73 static void dma_bdrv_cb(void *opaque
, int ret
)
75 DMAAIOCB
*dbs
= (DMAAIOCB
*)opaque
;
76 target_phys_addr_t cur_addr
, cur_len
;
81 dbs
->sector_num
+= dbs
->iov
.size
/ 512;
82 for (i
= 0; i
< dbs
->iov
.niov
; ++i
) {
83 cpu_physical_memory_unmap(dbs
->iov
.iov
[i
].iov_base
,
84 dbs
->iov
.iov
[i
].iov_len
, !dbs
->is_write
,
85 dbs
->iov
.iov
[i
].iov_len
);
87 qemu_iovec_reset(&dbs
->iov
);
89 if (dbs
->sg_cur_index
== dbs
->sg
->nsg
|| ret
< 0) {
90 dbs
->common
.cb(dbs
->common
.opaque
, ret
);
91 qemu_iovec_destroy(&dbs
->iov
);
92 qemu_aio_release(dbs
);
96 while (dbs
->sg_cur_index
< dbs
->sg
->nsg
) {
97 cur_addr
= dbs
->sg
->sg
[dbs
->sg_cur_index
].base
+ dbs
->sg_cur_byte
;
98 cur_len
= dbs
->sg
->sg
[dbs
->sg_cur_index
].len
- dbs
->sg_cur_byte
;
99 mem
= cpu_physical_memory_map(cur_addr
, &cur_len
, !dbs
->is_write
);
102 qemu_iovec_add(&dbs
->iov
, mem
, cur_len
);
103 dbs
->sg_cur_byte
+= cur_len
;
104 if (dbs
->sg_cur_byte
== dbs
->sg
->sg
[dbs
->sg_cur_index
].len
) {
105 dbs
->sg_cur_byte
= 0;
110 if (dbs
->iov
.size
== 0) {
111 cpu_register_map_client(dbs
, continue_after_map_failure
);
116 dbs
->acb
= bdrv_aio_writev(dbs
->bs
, dbs
->sector_num
, &dbs
->iov
,
117 dbs
->iov
.size
/ 512, dma_bdrv_cb
, dbs
);
119 dbs
->acb
= bdrv_aio_readv(dbs
->bs
, dbs
->sector_num
, &dbs
->iov
,
120 dbs
->iov
.size
/ 512, dma_bdrv_cb
, dbs
);
124 static BlockDriverAIOCB
*dma_bdrv_io(
125 BlockDriverState
*bs
, QEMUSGList
*sg
, uint64_t sector_num
,
126 BlockDriverCompletionFunc
*cb
, void *opaque
,
129 DMAAIOCB
*dbs
= qemu_aio_get_pool(&dma_aio_pool
, bs
, cb
, opaque
);
134 dbs
->sector_num
= sector_num
;
135 dbs
->sg_cur_index
= 0;
136 dbs
->sg_cur_byte
= 0;
137 dbs
->is_write
= is_write
;
139 qemu_iovec_init(&dbs
->iov
, sg
->nsg
);
145 BlockDriverAIOCB
*dma_bdrv_read(BlockDriverState
*bs
,
146 QEMUSGList
*sg
, uint64_t sector
,
147 void (*cb
)(void *opaque
, int ret
), void *opaque
)
149 return dma_bdrv_io(bs
, sg
, sector
, cb
, opaque
, 0);
152 BlockDriverAIOCB
*dma_bdrv_write(BlockDriverState
*bs
,
153 QEMUSGList
*sg
, uint64_t sector
,
154 void (*cb
)(void *opaque
, int ret
), void *opaque
)
156 return dma_bdrv_io(bs
, sg
, sector
, cb
, opaque
, 1);
159 static void dma_aio_cancel(BlockDriverAIOCB
*acb
)
161 DMAAIOCB
*dbs
= container_of(acb
, DMAAIOCB
, common
);
164 bdrv_aio_cancel(dbs
->acb
);
168 void dma_helper_init(void)
170 aio_pool_init(&dma_aio_pool
, sizeof(DMAAIOCB
), dma_aio_cancel
);