aliguori | 244ab90 | 2009-02-05 21:23:50 +0000 | [diff] [blame] | 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" |
aliguori | 59a703e | 2009-02-05 21:23:58 +0000 | [diff] [blame] | 11 | #include "block_int.h" |
Kevin Wolf | c57c465 | 2011-11-24 06:15:28 -0500 | [diff] [blame] | 12 | #include "trace.h" |
aliguori | 244ab90 | 2009-02-05 21:23:50 +0000 | [diff] [blame] | 13 | |
| 14 | void qemu_sglist_init(QEMUSGList *qsg, int alloc_hint) |
| 15 | { |
Anthony Liguori | 7267c09 | 2011-08-20 22:09:37 -0500 | [diff] [blame] | 16 | qsg->sg = g_malloc(alloc_hint * sizeof(ScatterGatherEntry)); |
aliguori | 244ab90 | 2009-02-05 21:23:50 +0000 | [diff] [blame] | 17 | qsg->nsg = 0; |
| 18 | qsg->nalloc = alloc_hint; |
| 19 | qsg->size = 0; |
| 20 | } |
| 21 | |
David Gibson | d323118 | 2011-10-31 17:06:46 +1100 | [diff] [blame] | 22 | void qemu_sglist_add(QEMUSGList *qsg, dma_addr_t base, dma_addr_t len) |
aliguori | 244ab90 | 2009-02-05 21:23:50 +0000 | [diff] [blame] | 23 | { |
| 24 | if (qsg->nsg == qsg->nalloc) { |
| 25 | qsg->nalloc = 2 * qsg->nalloc + 1; |
Anthony Liguori | 7267c09 | 2011-08-20 22:09:37 -0500 | [diff] [blame] | 26 | qsg->sg = g_realloc(qsg->sg, qsg->nalloc * sizeof(ScatterGatherEntry)); |
aliguori | 244ab90 | 2009-02-05 21:23:50 +0000 | [diff] [blame] | 27 | } |
| 28 | qsg->sg[qsg->nsg].base = base; |
| 29 | qsg->sg[qsg->nsg].len = len; |
| 30 | qsg->size += len; |
| 31 | ++qsg->nsg; |
| 32 | } |
| 33 | |
| 34 | void qemu_sglist_destroy(QEMUSGList *qsg) |
| 35 | { |
Anthony Liguori | 7267c09 | 2011-08-20 22:09:37 -0500 | [diff] [blame] | 36 | g_free(qsg->sg); |
aliguori | 244ab90 | 2009-02-05 21:23:50 +0000 | [diff] [blame] | 37 | } |
| 38 | |
aliguori | 59a703e | 2009-02-05 21:23:58 +0000 | [diff] [blame] | 39 | typedef struct { |
aliguori | 37b7842 | 2009-03-20 18:26:16 +0000 | [diff] [blame] | 40 | BlockDriverAIOCB common; |
aliguori | 59a703e | 2009-02-05 21:23:58 +0000 | [diff] [blame] | 41 | BlockDriverState *bs; |
| 42 | BlockDriverAIOCB *acb; |
| 43 | QEMUSGList *sg; |
| 44 | uint64_t sector_num; |
Paolo Bonzini | bbca72c | 2011-09-16 16:40:00 +0200 | [diff] [blame] | 45 | bool to_dev; |
Paolo Bonzini | c3adb5b | 2011-09-16 16:40:02 +0200 | [diff] [blame] | 46 | bool in_cancel; |
aliguori | 59a703e | 2009-02-05 21:23:58 +0000 | [diff] [blame] | 47 | int sg_cur_index; |
David Gibson | d323118 | 2011-10-31 17:06:46 +1100 | [diff] [blame] | 48 | dma_addr_t sg_cur_byte; |
aliguori | 59a703e | 2009-02-05 21:23:58 +0000 | [diff] [blame] | 49 | QEMUIOVector iov; |
| 50 | QEMUBH *bh; |
Christoph Hellwig | cb144cc | 2011-05-19 10:57:59 +0200 | [diff] [blame] | 51 | DMAIOFunc *io_func; |
aliguori | 37b7842 | 2009-03-20 18:26:16 +0000 | [diff] [blame] | 52 | } DMAAIOCB; |
aliguori | 59a703e | 2009-02-05 21:23:58 +0000 | [diff] [blame] | 53 | |
| 54 | static void dma_bdrv_cb(void *opaque, int ret); |
| 55 | |
| 56 | static void reschedule_dma(void *opaque) |
| 57 | { |
aliguori | 37b7842 | 2009-03-20 18:26:16 +0000 | [diff] [blame] | 58 | DMAAIOCB *dbs = (DMAAIOCB *)opaque; |
aliguori | 59a703e | 2009-02-05 21:23:58 +0000 | [diff] [blame] | 59 | |
| 60 | qemu_bh_delete(dbs->bh); |
| 61 | dbs->bh = NULL; |
Paolo Bonzini | c3adb5b | 2011-09-16 16:40:02 +0200 | [diff] [blame] | 62 | dma_bdrv_cb(dbs, 0); |
aliguori | 59a703e | 2009-02-05 21:23:58 +0000 | [diff] [blame] | 63 | } |
| 64 | |
| 65 | static void continue_after_map_failure(void *opaque) |
| 66 | { |
aliguori | 37b7842 | 2009-03-20 18:26:16 +0000 | [diff] [blame] | 67 | DMAAIOCB *dbs = (DMAAIOCB *)opaque; |
aliguori | 59a703e | 2009-02-05 21:23:58 +0000 | [diff] [blame] | 68 | |
| 69 | dbs->bh = qemu_bh_new(reschedule_dma, dbs); |
| 70 | qemu_bh_schedule(dbs->bh); |
| 71 | } |
| 72 | |
aliguori | 7403b14 | 2009-03-28 16:11:25 +0000 | [diff] [blame] | 73 | static void dma_bdrv_unmap(DMAAIOCB *dbs) |
aliguori | 59a703e | 2009-02-05 21:23:58 +0000 | [diff] [blame] | 74 | { |
aliguori | 59a703e | 2009-02-05 21:23:58 +0000 | [diff] [blame] | 75 | int i; |
| 76 | |
aliguori | 59a703e | 2009-02-05 21:23:58 +0000 | [diff] [blame] | 77 | for (i = 0; i < dbs->iov.niov; ++i) { |
| 78 | cpu_physical_memory_unmap(dbs->iov.iov[i].iov_base, |
Paolo Bonzini | bbca72c | 2011-09-16 16:40:00 +0200 | [diff] [blame] | 79 | dbs->iov.iov[i].iov_len, !dbs->to_dev, |
aliguori | 59a703e | 2009-02-05 21:23:58 +0000 | [diff] [blame] | 80 | dbs->iov.iov[i].iov_len); |
| 81 | } |
Paolo Bonzini | c3adb5b | 2011-09-16 16:40:02 +0200 | [diff] [blame] | 82 | qemu_iovec_reset(&dbs->iov); |
| 83 | } |
| 84 | |
| 85 | static void dma_complete(DMAAIOCB *dbs, int ret) |
| 86 | { |
Kevin Wolf | c57c465 | 2011-11-24 06:15:28 -0500 | [diff] [blame] | 87 | trace_dma_complete(dbs, ret, dbs->common.cb); |
| 88 | |
Paolo Bonzini | c3adb5b | 2011-09-16 16:40:02 +0200 | [diff] [blame] | 89 | dma_bdrv_unmap(dbs); |
| 90 | if (dbs->common.cb) { |
| 91 | dbs->common.cb(dbs->common.opaque, ret); |
| 92 | } |
| 93 | qemu_iovec_destroy(&dbs->iov); |
| 94 | if (dbs->bh) { |
| 95 | qemu_bh_delete(dbs->bh); |
| 96 | dbs->bh = NULL; |
| 97 | } |
| 98 | if (!dbs->in_cancel) { |
| 99 | /* Requests may complete while dma_aio_cancel is in progress. In |
| 100 | * this case, the AIOCB should not be released because it is still |
| 101 | * referenced by dma_aio_cancel. */ |
| 102 | qemu_aio_release(dbs); |
| 103 | } |
aliguori | 7403b14 | 2009-03-28 16:11:25 +0000 | [diff] [blame] | 104 | } |
| 105 | |
blueswir1 | 856ae5c | 2009-04-07 17:57:09 +0000 | [diff] [blame] | 106 | static void dma_bdrv_cb(void *opaque, int ret) |
aliguori | 7403b14 | 2009-03-28 16:11:25 +0000 | [diff] [blame] | 107 | { |
| 108 | DMAAIOCB *dbs = (DMAAIOCB *)opaque; |
Anthony Liguori | c227f09 | 2009-10-01 16:12:16 -0500 | [diff] [blame] | 109 | target_phys_addr_t cur_addr, cur_len; |
aliguori | 7403b14 | 2009-03-28 16:11:25 +0000 | [diff] [blame] | 110 | void *mem; |
| 111 | |
Kevin Wolf | c57c465 | 2011-11-24 06:15:28 -0500 | [diff] [blame] | 112 | trace_dma_bdrv_cb(dbs, ret); |
| 113 | |
aliguori | 7403b14 | 2009-03-28 16:11:25 +0000 | [diff] [blame] | 114 | dbs->acb = NULL; |
| 115 | dbs->sector_num += dbs->iov.size / 512; |
| 116 | dma_bdrv_unmap(dbs); |
aliguori | 59a703e | 2009-02-05 21:23:58 +0000 | [diff] [blame] | 117 | |
| 118 | if (dbs->sg_cur_index == dbs->sg->nsg || ret < 0) { |
Paolo Bonzini | c3adb5b | 2011-09-16 16:40:02 +0200 | [diff] [blame] | 119 | dma_complete(dbs, ret); |
aliguori | 59a703e | 2009-02-05 21:23:58 +0000 | [diff] [blame] | 120 | return; |
| 121 | } |
| 122 | |
| 123 | while (dbs->sg_cur_index < dbs->sg->nsg) { |
| 124 | cur_addr = dbs->sg->sg[dbs->sg_cur_index].base + dbs->sg_cur_byte; |
| 125 | cur_len = dbs->sg->sg[dbs->sg_cur_index].len - dbs->sg_cur_byte; |
Paolo Bonzini | bbca72c | 2011-09-16 16:40:00 +0200 | [diff] [blame] | 126 | mem = cpu_physical_memory_map(cur_addr, &cur_len, !dbs->to_dev); |
aliguori | 59a703e | 2009-02-05 21:23:58 +0000 | [diff] [blame] | 127 | if (!mem) |
| 128 | break; |
| 129 | qemu_iovec_add(&dbs->iov, mem, cur_len); |
| 130 | dbs->sg_cur_byte += cur_len; |
| 131 | if (dbs->sg_cur_byte == dbs->sg->sg[dbs->sg_cur_index].len) { |
| 132 | dbs->sg_cur_byte = 0; |
| 133 | ++dbs->sg_cur_index; |
| 134 | } |
| 135 | } |
| 136 | |
| 137 | if (dbs->iov.size == 0) { |
Kevin Wolf | c57c465 | 2011-11-24 06:15:28 -0500 | [diff] [blame] | 138 | trace_dma_map_wait(dbs); |
aliguori | 59a703e | 2009-02-05 21:23:58 +0000 | [diff] [blame] | 139 | cpu_register_map_client(dbs, continue_after_map_failure); |
| 140 | return; |
| 141 | } |
| 142 | |
Christoph Hellwig | cb144cc | 2011-05-19 10:57:59 +0200 | [diff] [blame] | 143 | dbs->acb = dbs->io_func(dbs->bs, dbs->sector_num, &dbs->iov, |
| 144 | dbs->iov.size / 512, dma_bdrv_cb, dbs); |
Paolo Bonzini | 6bee44e | 2011-11-14 17:50:52 +0100 | [diff] [blame] | 145 | assert(dbs->acb); |
aliguori | 59a703e | 2009-02-05 21:23:58 +0000 | [diff] [blame] | 146 | } |
| 147 | |
Christoph Hellwig | c16b5a2 | 2009-05-25 12:37:32 +0200 | [diff] [blame] | 148 | static void dma_aio_cancel(BlockDriverAIOCB *acb) |
| 149 | { |
| 150 | DMAAIOCB *dbs = container_of(acb, DMAAIOCB, common); |
| 151 | |
Kevin Wolf | c57c465 | 2011-11-24 06:15:28 -0500 | [diff] [blame] | 152 | trace_dma_aio_cancel(dbs); |
| 153 | |
Christoph Hellwig | c16b5a2 | 2009-05-25 12:37:32 +0200 | [diff] [blame] | 154 | if (dbs->acb) { |
Paolo Bonzini | c3adb5b | 2011-09-16 16:40:02 +0200 | [diff] [blame] | 155 | BlockDriverAIOCB *acb = dbs->acb; |
| 156 | dbs->acb = NULL; |
| 157 | dbs->in_cancel = true; |
| 158 | bdrv_aio_cancel(acb); |
| 159 | dbs->in_cancel = false; |
Christoph Hellwig | c16b5a2 | 2009-05-25 12:37:32 +0200 | [diff] [blame] | 160 | } |
Paolo Bonzini | c3adb5b | 2011-09-16 16:40:02 +0200 | [diff] [blame] | 161 | dbs->common.cb = NULL; |
| 162 | dma_complete(dbs, 0); |
Christoph Hellwig | c16b5a2 | 2009-05-25 12:37:32 +0200 | [diff] [blame] | 163 | } |
| 164 | |
| 165 | static AIOPool dma_aio_pool = { |
| 166 | .aiocb_size = sizeof(DMAAIOCB), |
| 167 | .cancel = dma_aio_cancel, |
| 168 | }; |
| 169 | |
Christoph Hellwig | cb144cc | 2011-05-19 10:57:59 +0200 | [diff] [blame] | 170 | BlockDriverAIOCB *dma_bdrv_io( |
aliguori | 59a703e | 2009-02-05 21:23:58 +0000 | [diff] [blame] | 171 | BlockDriverState *bs, QEMUSGList *sg, uint64_t sector_num, |
Christoph Hellwig | cb144cc | 2011-05-19 10:57:59 +0200 | [diff] [blame] | 172 | DMAIOFunc *io_func, BlockDriverCompletionFunc *cb, |
Paolo Bonzini | bbca72c | 2011-09-16 16:40:00 +0200 | [diff] [blame] | 173 | void *opaque, bool to_dev) |
aliguori | 59a703e | 2009-02-05 21:23:58 +0000 | [diff] [blame] | 174 | { |
Christoph Hellwig | cb144cc | 2011-05-19 10:57:59 +0200 | [diff] [blame] | 175 | DMAAIOCB *dbs = qemu_aio_get(&dma_aio_pool, bs, cb, opaque); |
aliguori | 59a703e | 2009-02-05 21:23:58 +0000 | [diff] [blame] | 176 | |
Kevin Wolf | c57c465 | 2011-11-24 06:15:28 -0500 | [diff] [blame] | 177 | trace_dma_bdrv_io(dbs, bs, sector_num, to_dev); |
| 178 | |
aliguori | 37b7842 | 2009-03-20 18:26:16 +0000 | [diff] [blame] | 179 | dbs->acb = NULL; |
aliguori | 59a703e | 2009-02-05 21:23:58 +0000 | [diff] [blame] | 180 | dbs->bs = bs; |
aliguori | 59a703e | 2009-02-05 21:23:58 +0000 | [diff] [blame] | 181 | dbs->sg = sg; |
| 182 | dbs->sector_num = sector_num; |
| 183 | dbs->sg_cur_index = 0; |
| 184 | dbs->sg_cur_byte = 0; |
Paolo Bonzini | bbca72c | 2011-09-16 16:40:00 +0200 | [diff] [blame] | 185 | dbs->to_dev = to_dev; |
Christoph Hellwig | cb144cc | 2011-05-19 10:57:59 +0200 | [diff] [blame] | 186 | dbs->io_func = io_func; |
aliguori | 59a703e | 2009-02-05 21:23:58 +0000 | [diff] [blame] | 187 | dbs->bh = NULL; |
| 188 | qemu_iovec_init(&dbs->iov, sg->nsg); |
| 189 | dma_bdrv_cb(dbs, 0); |
aliguori | 37b7842 | 2009-03-20 18:26:16 +0000 | [diff] [blame] | 190 | return &dbs->common; |
aliguori | 59a703e | 2009-02-05 21:23:58 +0000 | [diff] [blame] | 191 | } |
| 192 | |
| 193 | |
| 194 | BlockDriverAIOCB *dma_bdrv_read(BlockDriverState *bs, |
| 195 | QEMUSGList *sg, uint64_t sector, |
| 196 | void (*cb)(void *opaque, int ret), void *opaque) |
| 197 | { |
Paolo Bonzini | bbca72c | 2011-09-16 16:40:00 +0200 | [diff] [blame] | 198 | return dma_bdrv_io(bs, sg, sector, bdrv_aio_readv, cb, opaque, false); |
aliguori | 59a703e | 2009-02-05 21:23:58 +0000 | [diff] [blame] | 199 | } |
| 200 | |
| 201 | BlockDriverAIOCB *dma_bdrv_write(BlockDriverState *bs, |
| 202 | QEMUSGList *sg, uint64_t sector, |
| 203 | void (*cb)(void *opaque, int ret), void *opaque) |
| 204 | { |
Paolo Bonzini | bbca72c | 2011-09-16 16:40:00 +0200 | [diff] [blame] | 205 | return dma_bdrv_io(bs, sg, sector, bdrv_aio_writev, cb, opaque, true); |
aliguori | 59a703e | 2009-02-05 21:23:58 +0000 | [diff] [blame] | 206 | } |