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 | |
Paolo Bonzini | 9c17d61 | 2012-12-17 18:20:04 +0100 | [diff] [blame] | 10 | #include "sysemu/dma.h" |
Kevin Wolf | c57c465 | 2011-11-24 06:15:28 -0500 | [diff] [blame] | 11 | #include "trace.h" |
Paolo Bonzini | 1de7afc | 2012-12-17 18:20:00 +0100 | [diff] [blame] | 12 | #include "qemu/range.h" |
| 13 | #include "qemu/thread.h" |
aliguori | 244ab90 | 2009-02-05 21:23:50 +0000 | [diff] [blame] | 14 | |
David Gibson | e5332e6 | 2012-06-27 14:50:43 +1000 | [diff] [blame] | 15 | /* #define DEBUG_IOMMU */ |
| 16 | |
Avi Kivity | b90600e | 2012-10-03 16:42:37 +0200 | [diff] [blame] | 17 | static void do_dma_memory_set(AddressSpace *as, |
| 18 | dma_addr_t addr, uint8_t c, dma_addr_t len) |
David Gibson | d86a77f | 2012-06-27 14:50:38 +1000 | [diff] [blame] | 19 | { |
| 20 | #define FILLBUF_SIZE 512 |
| 21 | uint8_t fillbuf[FILLBUF_SIZE]; |
| 22 | int l; |
| 23 | |
| 24 | memset(fillbuf, c, FILLBUF_SIZE); |
| 25 | while (len > 0) { |
| 26 | l = len < FILLBUF_SIZE ? len : FILLBUF_SIZE; |
Avi Kivity | b90600e | 2012-10-03 16:42:37 +0200 | [diff] [blame] | 27 | address_space_rw(as, addr, fillbuf, l, true); |
Benjamin Herrenschmidt | bc9b78d | 2012-08-14 17:41:47 +1000 | [diff] [blame] | 28 | len -= l; |
| 29 | addr += l; |
David Gibson | d86a77f | 2012-06-27 14:50:38 +1000 | [diff] [blame] | 30 | } |
David Gibson | e5332e6 | 2012-06-27 14:50:43 +1000 | [diff] [blame] | 31 | } |
| 32 | |
| 33 | int dma_memory_set(DMAContext *dma, dma_addr_t addr, uint8_t c, dma_addr_t len) |
| 34 | { |
Benjamin Herrenschmidt | 7a0bac4 | 2012-06-27 14:50:47 +1000 | [diff] [blame] | 35 | dma_barrier(dma, DMA_DIRECTION_FROM_DEVICE); |
| 36 | |
David Gibson | e5332e6 | 2012-06-27 14:50:43 +1000 | [diff] [blame] | 37 | if (dma_has_iommu(dma)) { |
| 38 | return iommu_dma_memory_set(dma, addr, c, len); |
| 39 | } |
Avi Kivity | b90600e | 2012-10-03 16:42:37 +0200 | [diff] [blame] | 40 | do_dma_memory_set(dma->as, addr, c, len); |
David Gibson | e5332e6 | 2012-06-27 14:50:43 +1000 | [diff] [blame] | 41 | |
David Gibson | d86a77f | 2012-06-27 14:50:38 +1000 | [diff] [blame] | 42 | return 0; |
| 43 | } |
| 44 | |
David Gibson | c65bcef | 2012-06-27 14:50:40 +1000 | [diff] [blame] | 45 | void qemu_sglist_init(QEMUSGList *qsg, int alloc_hint, DMAContext *dma) |
aliguori | 244ab90 | 2009-02-05 21:23:50 +0000 | [diff] [blame] | 46 | { |
Anthony Liguori | 7267c09 | 2011-08-20 22:09:37 -0500 | [diff] [blame] | 47 | qsg->sg = g_malloc(alloc_hint * sizeof(ScatterGatherEntry)); |
aliguori | 244ab90 | 2009-02-05 21:23:50 +0000 | [diff] [blame] | 48 | qsg->nsg = 0; |
| 49 | qsg->nalloc = alloc_hint; |
| 50 | qsg->size = 0; |
David Gibson | c65bcef | 2012-06-27 14:50:40 +1000 | [diff] [blame] | 51 | qsg->dma = dma; |
aliguori | 244ab90 | 2009-02-05 21:23:50 +0000 | [diff] [blame] | 52 | } |
| 53 | |
David Gibson | d323118 | 2011-10-31 17:06:46 +1100 | [diff] [blame] | 54 | 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] | 55 | { |
| 56 | if (qsg->nsg == qsg->nalloc) { |
| 57 | qsg->nalloc = 2 * qsg->nalloc + 1; |
Anthony Liguori | 7267c09 | 2011-08-20 22:09:37 -0500 | [diff] [blame] | 58 | qsg->sg = g_realloc(qsg->sg, qsg->nalloc * sizeof(ScatterGatherEntry)); |
aliguori | 244ab90 | 2009-02-05 21:23:50 +0000 | [diff] [blame] | 59 | } |
| 60 | qsg->sg[qsg->nsg].base = base; |
| 61 | qsg->sg[qsg->nsg].len = len; |
| 62 | qsg->size += len; |
| 63 | ++qsg->nsg; |
| 64 | } |
| 65 | |
| 66 | void qemu_sglist_destroy(QEMUSGList *qsg) |
| 67 | { |
Anthony Liguori | 7267c09 | 2011-08-20 22:09:37 -0500 | [diff] [blame] | 68 | g_free(qsg->sg); |
Jason Baron | ea8d82a | 2012-08-03 15:57:10 -0400 | [diff] [blame] | 69 | memset(qsg, 0, sizeof(*qsg)); |
aliguori | 244ab90 | 2009-02-05 21:23:50 +0000 | [diff] [blame] | 70 | } |
| 71 | |
aliguori | 59a703e | 2009-02-05 21:23:58 +0000 | [diff] [blame] | 72 | typedef struct { |
aliguori | 37b7842 | 2009-03-20 18:26:16 +0000 | [diff] [blame] | 73 | BlockDriverAIOCB common; |
aliguori | 59a703e | 2009-02-05 21:23:58 +0000 | [diff] [blame] | 74 | BlockDriverState *bs; |
| 75 | BlockDriverAIOCB *acb; |
| 76 | QEMUSGList *sg; |
| 77 | uint64_t sector_num; |
David Gibson | 43cf8ae | 2012-03-27 13:42:23 +1100 | [diff] [blame] | 78 | DMADirection dir; |
Paolo Bonzini | c3adb5b | 2011-09-16 16:40:02 +0200 | [diff] [blame] | 79 | bool in_cancel; |
aliguori | 59a703e | 2009-02-05 21:23:58 +0000 | [diff] [blame] | 80 | int sg_cur_index; |
David Gibson | d323118 | 2011-10-31 17:06:46 +1100 | [diff] [blame] | 81 | dma_addr_t sg_cur_byte; |
aliguori | 59a703e | 2009-02-05 21:23:58 +0000 | [diff] [blame] | 82 | QEMUIOVector iov; |
| 83 | QEMUBH *bh; |
Christoph Hellwig | cb144cc | 2011-05-19 10:57:59 +0200 | [diff] [blame] | 84 | DMAIOFunc *io_func; |
aliguori | 37b7842 | 2009-03-20 18:26:16 +0000 | [diff] [blame] | 85 | } DMAAIOCB; |
aliguori | 59a703e | 2009-02-05 21:23:58 +0000 | [diff] [blame] | 86 | |
| 87 | static void dma_bdrv_cb(void *opaque, int ret); |
| 88 | |
| 89 | static void reschedule_dma(void *opaque) |
| 90 | { |
aliguori | 37b7842 | 2009-03-20 18:26:16 +0000 | [diff] [blame] | 91 | DMAAIOCB *dbs = (DMAAIOCB *)opaque; |
aliguori | 59a703e | 2009-02-05 21:23:58 +0000 | [diff] [blame] | 92 | |
| 93 | qemu_bh_delete(dbs->bh); |
| 94 | dbs->bh = NULL; |
Paolo Bonzini | c3adb5b | 2011-09-16 16:40:02 +0200 | [diff] [blame] | 95 | dma_bdrv_cb(dbs, 0); |
aliguori | 59a703e | 2009-02-05 21:23:58 +0000 | [diff] [blame] | 96 | } |
| 97 | |
| 98 | static void continue_after_map_failure(void *opaque) |
| 99 | { |
aliguori | 37b7842 | 2009-03-20 18:26:16 +0000 | [diff] [blame] | 100 | DMAAIOCB *dbs = (DMAAIOCB *)opaque; |
aliguori | 59a703e | 2009-02-05 21:23:58 +0000 | [diff] [blame] | 101 | |
| 102 | dbs->bh = qemu_bh_new(reschedule_dma, dbs); |
| 103 | qemu_bh_schedule(dbs->bh); |
| 104 | } |
| 105 | |
aliguori | 7403b14 | 2009-03-28 16:11:25 +0000 | [diff] [blame] | 106 | static void dma_bdrv_unmap(DMAAIOCB *dbs) |
aliguori | 59a703e | 2009-02-05 21:23:58 +0000 | [diff] [blame] | 107 | { |
aliguori | 59a703e | 2009-02-05 21:23:58 +0000 | [diff] [blame] | 108 | int i; |
| 109 | |
aliguori | 59a703e | 2009-02-05 21:23:58 +0000 | [diff] [blame] | 110 | for (i = 0; i < dbs->iov.niov; ++i) { |
David Gibson | c65bcef | 2012-06-27 14:50:40 +1000 | [diff] [blame] | 111 | dma_memory_unmap(dbs->sg->dma, dbs->iov.iov[i].iov_base, |
| 112 | dbs->iov.iov[i].iov_len, dbs->dir, |
| 113 | dbs->iov.iov[i].iov_len); |
aliguori | 59a703e | 2009-02-05 21:23:58 +0000 | [diff] [blame] | 114 | } |
Paolo Bonzini | c3adb5b | 2011-09-16 16:40:02 +0200 | [diff] [blame] | 115 | qemu_iovec_reset(&dbs->iov); |
| 116 | } |
| 117 | |
| 118 | static void dma_complete(DMAAIOCB *dbs, int ret) |
| 119 | { |
Kevin Wolf | c57c465 | 2011-11-24 06:15:28 -0500 | [diff] [blame] | 120 | trace_dma_complete(dbs, ret, dbs->common.cb); |
| 121 | |
Paolo Bonzini | c3adb5b | 2011-09-16 16:40:02 +0200 | [diff] [blame] | 122 | dma_bdrv_unmap(dbs); |
| 123 | if (dbs->common.cb) { |
| 124 | dbs->common.cb(dbs->common.opaque, ret); |
| 125 | } |
| 126 | qemu_iovec_destroy(&dbs->iov); |
| 127 | if (dbs->bh) { |
| 128 | qemu_bh_delete(dbs->bh); |
| 129 | dbs->bh = NULL; |
| 130 | } |
| 131 | if (!dbs->in_cancel) { |
| 132 | /* Requests may complete while dma_aio_cancel is in progress. In |
| 133 | * this case, the AIOCB should not be released because it is still |
| 134 | * referenced by dma_aio_cancel. */ |
| 135 | qemu_aio_release(dbs); |
| 136 | } |
aliguori | 7403b14 | 2009-03-28 16:11:25 +0000 | [diff] [blame] | 137 | } |
| 138 | |
blueswir1 | 856ae5c | 2009-04-07 17:57:09 +0000 | [diff] [blame] | 139 | static void dma_bdrv_cb(void *opaque, int ret) |
aliguori | 7403b14 | 2009-03-28 16:11:25 +0000 | [diff] [blame] | 140 | { |
| 141 | DMAAIOCB *dbs = (DMAAIOCB *)opaque; |
David Gibson | c65bcef | 2012-06-27 14:50:40 +1000 | [diff] [blame] | 142 | dma_addr_t cur_addr, cur_len; |
aliguori | 7403b14 | 2009-03-28 16:11:25 +0000 | [diff] [blame] | 143 | void *mem; |
| 144 | |
Kevin Wolf | c57c465 | 2011-11-24 06:15:28 -0500 | [diff] [blame] | 145 | trace_dma_bdrv_cb(dbs, ret); |
| 146 | |
aliguori | 7403b14 | 2009-03-28 16:11:25 +0000 | [diff] [blame] | 147 | dbs->acb = NULL; |
| 148 | dbs->sector_num += dbs->iov.size / 512; |
| 149 | dma_bdrv_unmap(dbs); |
aliguori | 59a703e | 2009-02-05 21:23:58 +0000 | [diff] [blame] | 150 | |
| 151 | if (dbs->sg_cur_index == dbs->sg->nsg || ret < 0) { |
Paolo Bonzini | c3adb5b | 2011-09-16 16:40:02 +0200 | [diff] [blame] | 152 | dma_complete(dbs, ret); |
aliguori | 59a703e | 2009-02-05 21:23:58 +0000 | [diff] [blame] | 153 | return; |
| 154 | } |
| 155 | |
| 156 | while (dbs->sg_cur_index < dbs->sg->nsg) { |
| 157 | cur_addr = dbs->sg->sg[dbs->sg_cur_index].base + dbs->sg_cur_byte; |
| 158 | cur_len = dbs->sg->sg[dbs->sg_cur_index].len - dbs->sg_cur_byte; |
David Gibson | c65bcef | 2012-06-27 14:50:40 +1000 | [diff] [blame] | 159 | mem = dma_memory_map(dbs->sg->dma, cur_addr, &cur_len, dbs->dir); |
aliguori | 59a703e | 2009-02-05 21:23:58 +0000 | [diff] [blame] | 160 | if (!mem) |
| 161 | break; |
| 162 | qemu_iovec_add(&dbs->iov, mem, cur_len); |
| 163 | dbs->sg_cur_byte += cur_len; |
| 164 | if (dbs->sg_cur_byte == dbs->sg->sg[dbs->sg_cur_index].len) { |
| 165 | dbs->sg_cur_byte = 0; |
| 166 | ++dbs->sg_cur_index; |
| 167 | } |
| 168 | } |
| 169 | |
| 170 | if (dbs->iov.size == 0) { |
Kevin Wolf | c57c465 | 2011-11-24 06:15:28 -0500 | [diff] [blame] | 171 | trace_dma_map_wait(dbs); |
aliguori | 59a703e | 2009-02-05 21:23:58 +0000 | [diff] [blame] | 172 | cpu_register_map_client(dbs, continue_after_map_failure); |
| 173 | return; |
| 174 | } |
| 175 | |
Christoph Hellwig | cb144cc | 2011-05-19 10:57:59 +0200 | [diff] [blame] | 176 | dbs->acb = dbs->io_func(dbs->bs, dbs->sector_num, &dbs->iov, |
| 177 | dbs->iov.size / 512, dma_bdrv_cb, dbs); |
Paolo Bonzini | 6bee44e | 2011-11-14 17:50:52 +0100 | [diff] [blame] | 178 | assert(dbs->acb); |
aliguori | 59a703e | 2009-02-05 21:23:58 +0000 | [diff] [blame] | 179 | } |
| 180 | |
Christoph Hellwig | c16b5a2 | 2009-05-25 12:37:32 +0200 | [diff] [blame] | 181 | static void dma_aio_cancel(BlockDriverAIOCB *acb) |
| 182 | { |
| 183 | DMAAIOCB *dbs = container_of(acb, DMAAIOCB, common); |
| 184 | |
Kevin Wolf | c57c465 | 2011-11-24 06:15:28 -0500 | [diff] [blame] | 185 | trace_dma_aio_cancel(dbs); |
| 186 | |
Christoph Hellwig | c16b5a2 | 2009-05-25 12:37:32 +0200 | [diff] [blame] | 187 | if (dbs->acb) { |
Paolo Bonzini | c3adb5b | 2011-09-16 16:40:02 +0200 | [diff] [blame] | 188 | BlockDriverAIOCB *acb = dbs->acb; |
| 189 | dbs->acb = NULL; |
| 190 | dbs->in_cancel = true; |
| 191 | bdrv_aio_cancel(acb); |
| 192 | dbs->in_cancel = false; |
Christoph Hellwig | c16b5a2 | 2009-05-25 12:37:32 +0200 | [diff] [blame] | 193 | } |
Paolo Bonzini | c3adb5b | 2011-09-16 16:40:02 +0200 | [diff] [blame] | 194 | dbs->common.cb = NULL; |
| 195 | dma_complete(dbs, 0); |
Christoph Hellwig | c16b5a2 | 2009-05-25 12:37:32 +0200 | [diff] [blame] | 196 | } |
| 197 | |
Stefan Hajnoczi | d7331be | 2012-10-31 16:34:37 +0100 | [diff] [blame] | 198 | static const AIOCBInfo dma_aiocb_info = { |
Christoph Hellwig | c16b5a2 | 2009-05-25 12:37:32 +0200 | [diff] [blame] | 199 | .aiocb_size = sizeof(DMAAIOCB), |
| 200 | .cancel = dma_aio_cancel, |
| 201 | }; |
| 202 | |
Christoph Hellwig | cb144cc | 2011-05-19 10:57:59 +0200 | [diff] [blame] | 203 | BlockDriverAIOCB *dma_bdrv_io( |
aliguori | 59a703e | 2009-02-05 21:23:58 +0000 | [diff] [blame] | 204 | BlockDriverState *bs, QEMUSGList *sg, uint64_t sector_num, |
Christoph Hellwig | cb144cc | 2011-05-19 10:57:59 +0200 | [diff] [blame] | 205 | DMAIOFunc *io_func, BlockDriverCompletionFunc *cb, |
David Gibson | 43cf8ae | 2012-03-27 13:42:23 +1100 | [diff] [blame] | 206 | void *opaque, DMADirection dir) |
aliguori | 59a703e | 2009-02-05 21:23:58 +0000 | [diff] [blame] | 207 | { |
Stefan Hajnoczi | d7331be | 2012-10-31 16:34:37 +0100 | [diff] [blame] | 208 | DMAAIOCB *dbs = qemu_aio_get(&dma_aiocb_info, bs, cb, opaque); |
aliguori | 59a703e | 2009-02-05 21:23:58 +0000 | [diff] [blame] | 209 | |
David Gibson | 43cf8ae | 2012-03-27 13:42:23 +1100 | [diff] [blame] | 210 | trace_dma_bdrv_io(dbs, bs, sector_num, (dir == DMA_DIRECTION_TO_DEVICE)); |
Kevin Wolf | c57c465 | 2011-11-24 06:15:28 -0500 | [diff] [blame] | 211 | |
aliguori | 37b7842 | 2009-03-20 18:26:16 +0000 | [diff] [blame] | 212 | dbs->acb = NULL; |
aliguori | 59a703e | 2009-02-05 21:23:58 +0000 | [diff] [blame] | 213 | dbs->bs = bs; |
aliguori | 59a703e | 2009-02-05 21:23:58 +0000 | [diff] [blame] | 214 | dbs->sg = sg; |
| 215 | dbs->sector_num = sector_num; |
| 216 | dbs->sg_cur_index = 0; |
| 217 | dbs->sg_cur_byte = 0; |
David Gibson | 43cf8ae | 2012-03-27 13:42:23 +1100 | [diff] [blame] | 218 | dbs->dir = dir; |
Christoph Hellwig | cb144cc | 2011-05-19 10:57:59 +0200 | [diff] [blame] | 219 | dbs->io_func = io_func; |
aliguori | 59a703e | 2009-02-05 21:23:58 +0000 | [diff] [blame] | 220 | dbs->bh = NULL; |
| 221 | qemu_iovec_init(&dbs->iov, sg->nsg); |
| 222 | dma_bdrv_cb(dbs, 0); |
aliguori | 37b7842 | 2009-03-20 18:26:16 +0000 | [diff] [blame] | 223 | return &dbs->common; |
aliguori | 59a703e | 2009-02-05 21:23:58 +0000 | [diff] [blame] | 224 | } |
| 225 | |
| 226 | |
| 227 | BlockDriverAIOCB *dma_bdrv_read(BlockDriverState *bs, |
| 228 | QEMUSGList *sg, uint64_t sector, |
| 229 | void (*cb)(void *opaque, int ret), void *opaque) |
| 230 | { |
David Gibson | 43cf8ae | 2012-03-27 13:42:23 +1100 | [diff] [blame] | 231 | return dma_bdrv_io(bs, sg, sector, bdrv_aio_readv, cb, opaque, |
| 232 | DMA_DIRECTION_FROM_DEVICE); |
aliguori | 59a703e | 2009-02-05 21:23:58 +0000 | [diff] [blame] | 233 | } |
| 234 | |
| 235 | BlockDriverAIOCB *dma_bdrv_write(BlockDriverState *bs, |
| 236 | QEMUSGList *sg, uint64_t sector, |
| 237 | void (*cb)(void *opaque, int ret), void *opaque) |
| 238 | { |
David Gibson | 43cf8ae | 2012-03-27 13:42:23 +1100 | [diff] [blame] | 239 | return dma_bdrv_io(bs, sg, sector, bdrv_aio_writev, cb, opaque, |
| 240 | DMA_DIRECTION_TO_DEVICE); |
aliguori | 59a703e | 2009-02-05 21:23:58 +0000 | [diff] [blame] | 241 | } |
Paolo Bonzini | 8171ee3 | 2011-07-06 08:02:14 +0200 | [diff] [blame] | 242 | |
| 243 | |
David Gibson | c65bcef | 2012-06-27 14:50:40 +1000 | [diff] [blame] | 244 | static uint64_t dma_buf_rw(uint8_t *ptr, int32_t len, QEMUSGList *sg, |
| 245 | DMADirection dir) |
Paolo Bonzini | 8171ee3 | 2011-07-06 08:02:14 +0200 | [diff] [blame] | 246 | { |
| 247 | uint64_t resid; |
| 248 | int sg_cur_index; |
| 249 | |
| 250 | resid = sg->size; |
| 251 | sg_cur_index = 0; |
| 252 | len = MIN(len, resid); |
| 253 | while (len > 0) { |
| 254 | ScatterGatherEntry entry = sg->sg[sg_cur_index++]; |
| 255 | int32_t xfer = MIN(len, entry.len); |
David Gibson | c65bcef | 2012-06-27 14:50:40 +1000 | [diff] [blame] | 256 | dma_memory_rw(sg->dma, entry.base, ptr, xfer, dir); |
Paolo Bonzini | 8171ee3 | 2011-07-06 08:02:14 +0200 | [diff] [blame] | 257 | ptr += xfer; |
| 258 | len -= xfer; |
| 259 | resid -= xfer; |
| 260 | } |
| 261 | |
| 262 | return resid; |
| 263 | } |
| 264 | |
| 265 | uint64_t dma_buf_read(uint8_t *ptr, int32_t len, QEMUSGList *sg) |
| 266 | { |
David Gibson | c65bcef | 2012-06-27 14:50:40 +1000 | [diff] [blame] | 267 | return dma_buf_rw(ptr, len, sg, DMA_DIRECTION_FROM_DEVICE); |
Paolo Bonzini | 8171ee3 | 2011-07-06 08:02:14 +0200 | [diff] [blame] | 268 | } |
| 269 | |
| 270 | uint64_t dma_buf_write(uint8_t *ptr, int32_t len, QEMUSGList *sg) |
| 271 | { |
David Gibson | c65bcef | 2012-06-27 14:50:40 +1000 | [diff] [blame] | 272 | return dma_buf_rw(ptr, len, sg, DMA_DIRECTION_TO_DEVICE); |
Paolo Bonzini | 8171ee3 | 2011-07-06 08:02:14 +0200 | [diff] [blame] | 273 | } |
Paolo Bonzini | 84a6935 | 2011-09-05 14:20:29 +0200 | [diff] [blame] | 274 | |
| 275 | void dma_acct_start(BlockDriverState *bs, BlockAcctCookie *cookie, |
| 276 | QEMUSGList *sg, enum BlockAcctType type) |
| 277 | { |
| 278 | bdrv_acct_start(bs, cookie, sg->size, type); |
| 279 | } |
David Gibson | e5332e6 | 2012-06-27 14:50:43 +1000 | [diff] [blame] | 280 | |
| 281 | bool iommu_dma_memory_valid(DMAContext *dma, dma_addr_t addr, dma_addr_t len, |
| 282 | DMADirection dir) |
| 283 | { |
Avi Kivity | a8170e5 | 2012-10-23 12:30:10 +0200 | [diff] [blame] | 284 | hwaddr paddr, plen; |
David Gibson | e5332e6 | 2012-06-27 14:50:43 +1000 | [diff] [blame] | 285 | |
| 286 | #ifdef DEBUG_IOMMU |
| 287 | fprintf(stderr, "dma_memory_check context=%p addr=0x" DMA_ADDR_FMT |
| 288 | " len=0x" DMA_ADDR_FMT " dir=%d\n", dma, addr, len, dir); |
| 289 | #endif |
| 290 | |
| 291 | while (len) { |
| 292 | if (dma->translate(dma, addr, &paddr, &plen, dir) != 0) { |
| 293 | return false; |
| 294 | } |
| 295 | |
| 296 | /* The translation might be valid for larger regions. */ |
| 297 | if (plen > len) { |
| 298 | plen = len; |
| 299 | } |
| 300 | |
| 301 | len -= plen; |
| 302 | addr += plen; |
| 303 | } |
| 304 | |
| 305 | return true; |
| 306 | } |
| 307 | |
| 308 | int iommu_dma_memory_rw(DMAContext *dma, dma_addr_t addr, |
| 309 | void *buf, dma_addr_t len, DMADirection dir) |
| 310 | { |
Avi Kivity | a8170e5 | 2012-10-23 12:30:10 +0200 | [diff] [blame] | 311 | hwaddr paddr, plen; |
David Gibson | e5332e6 | 2012-06-27 14:50:43 +1000 | [diff] [blame] | 312 | int err; |
| 313 | |
| 314 | #ifdef DEBUG_IOMMU |
| 315 | fprintf(stderr, "dma_memory_rw context=%p addr=0x" DMA_ADDR_FMT " len=0x" |
| 316 | DMA_ADDR_FMT " dir=%d\n", dma, addr, len, dir); |
| 317 | #endif |
| 318 | |
| 319 | while (len) { |
| 320 | err = dma->translate(dma, addr, &paddr, &plen, dir); |
| 321 | if (err) { |
| 322 | /* |
| 323 | * In case of failure on reads from the guest, we clean the |
| 324 | * destination buffer so that a device that doesn't test |
| 325 | * for errors will not expose qemu internal memory. |
| 326 | */ |
| 327 | memset(buf, 0, len); |
| 328 | return -1; |
| 329 | } |
| 330 | |
| 331 | /* The translation might be valid for larger regions. */ |
| 332 | if (plen > len) { |
| 333 | plen = len; |
| 334 | } |
| 335 | |
Avi Kivity | b90600e | 2012-10-03 16:42:37 +0200 | [diff] [blame] | 336 | address_space_rw(dma->as, paddr, buf, plen, dir == DMA_DIRECTION_FROM_DEVICE); |
David Gibson | e5332e6 | 2012-06-27 14:50:43 +1000 | [diff] [blame] | 337 | |
| 338 | len -= plen; |
| 339 | addr += plen; |
| 340 | buf += plen; |
| 341 | } |
| 342 | |
| 343 | return 0; |
| 344 | } |
| 345 | |
| 346 | int iommu_dma_memory_set(DMAContext *dma, dma_addr_t addr, uint8_t c, |
| 347 | dma_addr_t len) |
| 348 | { |
Avi Kivity | a8170e5 | 2012-10-23 12:30:10 +0200 | [diff] [blame] | 349 | hwaddr paddr, plen; |
David Gibson | e5332e6 | 2012-06-27 14:50:43 +1000 | [diff] [blame] | 350 | int err; |
| 351 | |
| 352 | #ifdef DEBUG_IOMMU |
| 353 | fprintf(stderr, "dma_memory_set context=%p addr=0x" DMA_ADDR_FMT |
| 354 | " len=0x" DMA_ADDR_FMT "\n", dma, addr, len); |
| 355 | #endif |
| 356 | |
| 357 | while (len) { |
| 358 | err = dma->translate(dma, addr, &paddr, &plen, |
| 359 | DMA_DIRECTION_FROM_DEVICE); |
| 360 | if (err) { |
| 361 | return err; |
| 362 | } |
| 363 | |
| 364 | /* The translation might be valid for larger regions. */ |
| 365 | if (plen > len) { |
| 366 | plen = len; |
| 367 | } |
| 368 | |
Avi Kivity | b90600e | 2012-10-03 16:42:37 +0200 | [diff] [blame] | 369 | do_dma_memory_set(dma->as, paddr, c, plen); |
David Gibson | e5332e6 | 2012-06-27 14:50:43 +1000 | [diff] [blame] | 370 | |
| 371 | len -= plen; |
| 372 | addr += plen; |
| 373 | } |
| 374 | |
| 375 | return 0; |
| 376 | } |
| 377 | |
Avi Kivity | b90600e | 2012-10-03 16:42:37 +0200 | [diff] [blame] | 378 | void dma_context_init(DMAContext *dma, AddressSpace *as, DMATranslateFunc translate, |
David Gibson | e5332e6 | 2012-06-27 14:50:43 +1000 | [diff] [blame] | 379 | DMAMapFunc map, DMAUnmapFunc unmap) |
| 380 | { |
| 381 | #ifdef DEBUG_IOMMU |
| 382 | fprintf(stderr, "dma_context_init(%p, %p, %p, %p)\n", |
| 383 | dma, translate, map, unmap); |
| 384 | #endif |
Avi Kivity | b90600e | 2012-10-03 16:42:37 +0200 | [diff] [blame] | 385 | dma->as = as; |
David Gibson | e5332e6 | 2012-06-27 14:50:43 +1000 | [diff] [blame] | 386 | dma->translate = translate; |
| 387 | dma->map = map; |
| 388 | dma->unmap = unmap; |
| 389 | } |
| 390 | |
| 391 | void *iommu_dma_memory_map(DMAContext *dma, dma_addr_t addr, dma_addr_t *len, |
| 392 | DMADirection dir) |
| 393 | { |
| 394 | int err; |
Avi Kivity | a8170e5 | 2012-10-23 12:30:10 +0200 | [diff] [blame] | 395 | hwaddr paddr, plen; |
David Gibson | e5332e6 | 2012-06-27 14:50:43 +1000 | [diff] [blame] | 396 | void *buf; |
| 397 | |
| 398 | if (dma->map) { |
| 399 | return dma->map(dma, addr, len, dir); |
| 400 | } |
| 401 | |
| 402 | plen = *len; |
| 403 | err = dma->translate(dma, addr, &paddr, &plen, dir); |
| 404 | if (err) { |
| 405 | return NULL; |
| 406 | } |
| 407 | |
| 408 | /* |
| 409 | * If this is true, the virtual region is contiguous, |
| 410 | * but the translated physical region isn't. We just |
Avi Kivity | b90600e | 2012-10-03 16:42:37 +0200 | [diff] [blame] | 411 | * clamp *len, much like address_space_map() does. |
David Gibson | e5332e6 | 2012-06-27 14:50:43 +1000 | [diff] [blame] | 412 | */ |
| 413 | if (plen < *len) { |
| 414 | *len = plen; |
| 415 | } |
| 416 | |
Avi Kivity | b90600e | 2012-10-03 16:42:37 +0200 | [diff] [blame] | 417 | buf = address_space_map(dma->as, paddr, &plen, dir == DMA_DIRECTION_FROM_DEVICE); |
David Gibson | e5332e6 | 2012-06-27 14:50:43 +1000 | [diff] [blame] | 418 | *len = plen; |
| 419 | |
| 420 | return buf; |
| 421 | } |
| 422 | |
| 423 | void iommu_dma_memory_unmap(DMAContext *dma, void *buffer, dma_addr_t len, |
| 424 | DMADirection dir, dma_addr_t access_len) |
| 425 | { |
| 426 | if (dma->unmap) { |
| 427 | dma->unmap(dma, buffer, len, dir, access_len); |
| 428 | return; |
| 429 | } |
| 430 | |
Avi Kivity | b90600e | 2012-10-03 16:42:37 +0200 | [diff] [blame] | 431 | address_space_unmap(dma->as, buffer, len, dir == DMA_DIRECTION_FROM_DEVICE, |
| 432 | access_len); |
David Gibson | e5332e6 | 2012-06-27 14:50:43 +1000 | [diff] [blame] | 433 | |
| 434 | } |