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 | #ifndef DMA_H |
| 11 | #define DMA_H |
| 12 | |
| 13 | #include <stdio.h> |
Avi Kivity | b90600e | 2012-10-03 16:42:37 +0200 | [diff] [blame] | 14 | #include "memory.h" |
Paul Brook | 1ad2134 | 2009-05-19 16:17:58 +0100 | [diff] [blame] | 15 | #include "hw/hw.h" |
aliguori | 59a703e | 2009-02-05 21:23:58 +0000 | [diff] [blame] | 16 | #include "block.h" |
Benjamin Herrenschmidt | 7a0bac4 | 2012-06-27 14:50:47 +1000 | [diff] [blame] | 17 | #include "kvm.h" |
aliguori | 244ab90 | 2009-02-05 21:23:50 +0000 | [diff] [blame] | 18 | |
David Gibson | d86a77f | 2012-06-27 14:50:38 +1000 | [diff] [blame] | 19 | typedef struct DMAContext DMAContext; |
Paolo Bonzini | 10dc8ae | 2011-09-16 16:40:01 +0200 | [diff] [blame] | 20 | typedef struct ScatterGatherEntry ScatterGatherEntry; |
| 21 | |
David Gibson | 43cf8ae | 2012-03-27 13:42:23 +1100 | [diff] [blame] | 22 | typedef enum { |
| 23 | DMA_DIRECTION_TO_DEVICE = 0, |
| 24 | DMA_DIRECTION_FROM_DEVICE = 1, |
| 25 | } DMADirection; |
| 26 | |
Paolo Bonzini | fead0c2 | 2011-11-09 16:58:30 +0100 | [diff] [blame] | 27 | struct QEMUSGList { |
| 28 | ScatterGatherEntry *sg; |
| 29 | int nsg; |
| 30 | int nalloc; |
| 31 | size_t size; |
David Gibson | c65bcef | 2012-06-27 14:50:40 +1000 | [diff] [blame] | 32 | DMAContext *dma; |
Paolo Bonzini | fead0c2 | 2011-11-09 16:58:30 +0100 | [diff] [blame] | 33 | }; |
| 34 | |
Avi Kivity | 4be403c | 2012-10-04 12:36:04 +0200 | [diff] [blame] | 35 | #ifndef CONFIG_USER_ONLY |
David Gibson | d9d1055 | 2011-10-31 17:06:45 +1100 | [diff] [blame] | 36 | |
David Gibson | e5332e6 | 2012-06-27 14:50:43 +1000 | [diff] [blame] | 37 | /* |
| 38 | * When an IOMMU is present, bus addresses become distinct from |
| 39 | * CPU/memory physical addresses and may be a different size. Because |
| 40 | * the IOVA size depends more on the bus than on the platform, we more |
| 41 | * or less have to treat these as 64-bit always to cover all (or at |
| 42 | * least most) cases. |
| 43 | */ |
| 44 | typedef uint64_t dma_addr_t; |
| 45 | |
| 46 | #define DMA_ADDR_BITS 64 |
| 47 | #define DMA_ADDR_FMT "%" PRIx64 |
| 48 | |
| 49 | typedef int DMATranslateFunc(DMAContext *dma, |
| 50 | dma_addr_t addr, |
Avi Kivity | a8170e5 | 2012-10-23 12:30:10 +0200 | [diff] [blame] | 51 | hwaddr *paddr, |
| 52 | hwaddr *len, |
David Gibson | e5332e6 | 2012-06-27 14:50:43 +1000 | [diff] [blame] | 53 | DMADirection dir); |
| 54 | typedef void* DMAMapFunc(DMAContext *dma, |
| 55 | dma_addr_t addr, |
| 56 | dma_addr_t *len, |
| 57 | DMADirection dir); |
| 58 | typedef void DMAUnmapFunc(DMAContext *dma, |
| 59 | void *buffer, |
| 60 | dma_addr_t len, |
| 61 | DMADirection dir, |
| 62 | dma_addr_t access_len); |
| 63 | |
| 64 | struct DMAContext { |
Avi Kivity | b90600e | 2012-10-03 16:42:37 +0200 | [diff] [blame] | 65 | AddressSpace *as; |
David Gibson | e5332e6 | 2012-06-27 14:50:43 +1000 | [diff] [blame] | 66 | DMATranslateFunc *translate; |
| 67 | DMAMapFunc *map; |
| 68 | DMAUnmapFunc *unmap; |
| 69 | }; |
| 70 | |
Peter Maydell | 9e11908 | 2012-10-29 11:34:32 +1000 | [diff] [blame] | 71 | /* A global DMA context corresponding to the address_space_memory |
| 72 | * AddressSpace, for sysbus devices which do DMA. |
| 73 | */ |
| 74 | extern DMAContext dma_context_memory; |
| 75 | |
Benjamin Herrenschmidt | 7a0bac4 | 2012-06-27 14:50:47 +1000 | [diff] [blame] | 76 | static inline void dma_barrier(DMAContext *dma, DMADirection dir) |
| 77 | { |
| 78 | /* |
| 79 | * This is called before DMA read and write operations |
| 80 | * unless the _relaxed form is used and is responsible |
| 81 | * for providing some sane ordering of accesses vs |
| 82 | * concurrently running VCPUs. |
| 83 | * |
| 84 | * Users of map(), unmap() or lower level st/ld_* |
| 85 | * operations are responsible for providing their own |
| 86 | * ordering via barriers. |
| 87 | * |
| 88 | * This primitive implementation does a simple smp_mb() |
| 89 | * before each operation which provides pretty much full |
| 90 | * ordering. |
| 91 | * |
| 92 | * A smarter implementation can be devised if needed to |
| 93 | * use lighter barriers based on the direction of the |
| 94 | * transfer, the DMA context, etc... |
| 95 | */ |
| 96 | if (kvm_enabled()) { |
| 97 | smp_mb(); |
| 98 | } |
| 99 | } |
| 100 | |
David Gibson | e5332e6 | 2012-06-27 14:50:43 +1000 | [diff] [blame] | 101 | static inline bool dma_has_iommu(DMAContext *dma) |
| 102 | { |
Avi Kivity | b90600e | 2012-10-03 16:42:37 +0200 | [diff] [blame] | 103 | return dma && dma->translate; |
David Gibson | e5332e6 | 2012-06-27 14:50:43 +1000 | [diff] [blame] | 104 | } |
David Gibson | d9d1055 | 2011-10-31 17:06:45 +1100 | [diff] [blame] | 105 | |
David Gibson | d86a77f | 2012-06-27 14:50:38 +1000 | [diff] [blame] | 106 | /* Checks that the given range of addresses is valid for DMA. This is |
| 107 | * useful for certain cases, but usually you should just use |
| 108 | * dma_memory_{read,write}() and check for errors */ |
David Gibson | e5332e6 | 2012-06-27 14:50:43 +1000 | [diff] [blame] | 109 | bool iommu_dma_memory_valid(DMAContext *dma, dma_addr_t addr, dma_addr_t len, |
| 110 | DMADirection dir); |
| 111 | static inline bool dma_memory_valid(DMAContext *dma, |
| 112 | dma_addr_t addr, dma_addr_t len, |
| 113 | DMADirection dir) |
David Gibson | d86a77f | 2012-06-27 14:50:38 +1000 | [diff] [blame] | 114 | { |
David Gibson | e5332e6 | 2012-06-27 14:50:43 +1000 | [diff] [blame] | 115 | if (!dma_has_iommu(dma)) { |
| 116 | return true; |
| 117 | } else { |
| 118 | return iommu_dma_memory_valid(dma, addr, len, dir); |
| 119 | } |
David Gibson | d86a77f | 2012-06-27 14:50:38 +1000 | [diff] [blame] | 120 | } |
| 121 | |
David Gibson | e5332e6 | 2012-06-27 14:50:43 +1000 | [diff] [blame] | 122 | int iommu_dma_memory_rw(DMAContext *dma, dma_addr_t addr, |
| 123 | void *buf, dma_addr_t len, DMADirection dir); |
Benjamin Herrenschmidt | 7a0bac4 | 2012-06-27 14:50:47 +1000 | [diff] [blame] | 124 | static inline int dma_memory_rw_relaxed(DMAContext *dma, dma_addr_t addr, |
| 125 | void *buf, dma_addr_t len, |
| 126 | DMADirection dir) |
David Gibson | d86a77f | 2012-06-27 14:50:38 +1000 | [diff] [blame] | 127 | { |
David Gibson | e5332e6 | 2012-06-27 14:50:43 +1000 | [diff] [blame] | 128 | if (!dma_has_iommu(dma)) { |
| 129 | /* Fast-path for no IOMMU */ |
Avi Kivity | b90600e | 2012-10-03 16:42:37 +0200 | [diff] [blame] | 130 | address_space_rw(dma->as, addr, buf, len, dir == DMA_DIRECTION_FROM_DEVICE); |
David Gibson | e5332e6 | 2012-06-27 14:50:43 +1000 | [diff] [blame] | 131 | return 0; |
| 132 | } else { |
| 133 | return iommu_dma_memory_rw(dma, addr, buf, len, dir); |
| 134 | } |
David Gibson | d86a77f | 2012-06-27 14:50:38 +1000 | [diff] [blame] | 135 | } |
| 136 | |
Benjamin Herrenschmidt | 7a0bac4 | 2012-06-27 14:50:47 +1000 | [diff] [blame] | 137 | static inline int dma_memory_read_relaxed(DMAContext *dma, dma_addr_t addr, |
| 138 | void *buf, dma_addr_t len) |
| 139 | { |
| 140 | return dma_memory_rw_relaxed(dma, addr, buf, len, DMA_DIRECTION_TO_DEVICE); |
| 141 | } |
| 142 | |
| 143 | static inline int dma_memory_write_relaxed(DMAContext *dma, dma_addr_t addr, |
| 144 | const void *buf, dma_addr_t len) |
| 145 | { |
| 146 | return dma_memory_rw_relaxed(dma, addr, (void *)buf, len, |
| 147 | DMA_DIRECTION_FROM_DEVICE); |
| 148 | } |
| 149 | |
| 150 | static inline int dma_memory_rw(DMAContext *dma, dma_addr_t addr, |
| 151 | void *buf, dma_addr_t len, |
| 152 | DMADirection dir) |
| 153 | { |
| 154 | dma_barrier(dma, dir); |
| 155 | |
| 156 | return dma_memory_rw_relaxed(dma, addr, buf, len, dir); |
| 157 | } |
| 158 | |
David Gibson | d86a77f | 2012-06-27 14:50:38 +1000 | [diff] [blame] | 159 | static inline int dma_memory_read(DMAContext *dma, dma_addr_t addr, |
| 160 | void *buf, dma_addr_t len) |
| 161 | { |
| 162 | return dma_memory_rw(dma, addr, buf, len, DMA_DIRECTION_TO_DEVICE); |
| 163 | } |
| 164 | |
| 165 | static inline int dma_memory_write(DMAContext *dma, dma_addr_t addr, |
| 166 | const void *buf, dma_addr_t len) |
| 167 | { |
| 168 | return dma_memory_rw(dma, addr, (void *)buf, len, |
| 169 | DMA_DIRECTION_FROM_DEVICE); |
| 170 | } |
| 171 | |
David Gibson | e5332e6 | 2012-06-27 14:50:43 +1000 | [diff] [blame] | 172 | int iommu_dma_memory_set(DMAContext *dma, dma_addr_t addr, uint8_t c, |
| 173 | dma_addr_t len); |
| 174 | |
David Gibson | d86a77f | 2012-06-27 14:50:38 +1000 | [diff] [blame] | 175 | int dma_memory_set(DMAContext *dma, dma_addr_t addr, uint8_t c, dma_addr_t len); |
| 176 | |
David Gibson | e5332e6 | 2012-06-27 14:50:43 +1000 | [diff] [blame] | 177 | void *iommu_dma_memory_map(DMAContext *dma, |
| 178 | dma_addr_t addr, dma_addr_t *len, |
| 179 | DMADirection dir); |
David Gibson | d86a77f | 2012-06-27 14:50:38 +1000 | [diff] [blame] | 180 | static inline void *dma_memory_map(DMAContext *dma, |
| 181 | dma_addr_t addr, dma_addr_t *len, |
| 182 | DMADirection dir) |
| 183 | { |
David Gibson | e5332e6 | 2012-06-27 14:50:43 +1000 | [diff] [blame] | 184 | if (!dma_has_iommu(dma)) { |
Avi Kivity | a8170e5 | 2012-10-23 12:30:10 +0200 | [diff] [blame] | 185 | hwaddr xlen = *len; |
David Gibson | e5332e6 | 2012-06-27 14:50:43 +1000 | [diff] [blame] | 186 | void *p; |
David Gibson | d86a77f | 2012-06-27 14:50:38 +1000 | [diff] [blame] | 187 | |
Avi Kivity | b90600e | 2012-10-03 16:42:37 +0200 | [diff] [blame] | 188 | p = address_space_map(dma->as, addr, &xlen, dir == DMA_DIRECTION_FROM_DEVICE); |
David Gibson | e5332e6 | 2012-06-27 14:50:43 +1000 | [diff] [blame] | 189 | *len = xlen; |
| 190 | return p; |
| 191 | } else { |
| 192 | return iommu_dma_memory_map(dma, addr, len, dir); |
| 193 | } |
David Gibson | d86a77f | 2012-06-27 14:50:38 +1000 | [diff] [blame] | 194 | } |
| 195 | |
David Gibson | e5332e6 | 2012-06-27 14:50:43 +1000 | [diff] [blame] | 196 | void iommu_dma_memory_unmap(DMAContext *dma, |
| 197 | void *buffer, dma_addr_t len, |
| 198 | DMADirection dir, dma_addr_t access_len); |
David Gibson | d86a77f | 2012-06-27 14:50:38 +1000 | [diff] [blame] | 199 | static inline void dma_memory_unmap(DMAContext *dma, |
| 200 | void *buffer, dma_addr_t len, |
| 201 | DMADirection dir, dma_addr_t access_len) |
| 202 | { |
David Gibson | e5332e6 | 2012-06-27 14:50:43 +1000 | [diff] [blame] | 203 | if (!dma_has_iommu(dma)) { |
Avi Kivity | a8170e5 | 2012-10-23 12:30:10 +0200 | [diff] [blame] | 204 | address_space_unmap(dma->as, buffer, (hwaddr)len, |
Avi Kivity | b90600e | 2012-10-03 16:42:37 +0200 | [diff] [blame] | 205 | dir == DMA_DIRECTION_FROM_DEVICE, access_len); |
David Gibson | e5332e6 | 2012-06-27 14:50:43 +1000 | [diff] [blame] | 206 | } else { |
| 207 | iommu_dma_memory_unmap(dma, buffer, len, dir, access_len); |
| 208 | } |
David Gibson | d86a77f | 2012-06-27 14:50:38 +1000 | [diff] [blame] | 209 | } |
| 210 | |
| 211 | #define DEFINE_LDST_DMA(_lname, _sname, _bits, _end) \ |
| 212 | static inline uint##_bits##_t ld##_lname##_##_end##_dma(DMAContext *dma, \ |
| 213 | dma_addr_t addr) \ |
| 214 | { \ |
| 215 | uint##_bits##_t val; \ |
| 216 | dma_memory_read(dma, addr, &val, (_bits) / 8); \ |
| 217 | return _end##_bits##_to_cpu(val); \ |
| 218 | } \ |
| 219 | static inline void st##_sname##_##_end##_dma(DMAContext *dma, \ |
| 220 | dma_addr_t addr, \ |
| 221 | uint##_bits##_t val) \ |
| 222 | { \ |
| 223 | val = cpu_to_##_end##_bits(val); \ |
| 224 | dma_memory_write(dma, addr, &val, (_bits) / 8); \ |
| 225 | } |
| 226 | |
| 227 | static inline uint8_t ldub_dma(DMAContext *dma, dma_addr_t addr) |
| 228 | { |
| 229 | uint8_t val; |
| 230 | |
| 231 | dma_memory_read(dma, addr, &val, 1); |
| 232 | return val; |
| 233 | } |
| 234 | |
| 235 | static inline void stb_dma(DMAContext *dma, dma_addr_t addr, uint8_t val) |
| 236 | { |
| 237 | dma_memory_write(dma, addr, &val, 1); |
| 238 | } |
| 239 | |
| 240 | DEFINE_LDST_DMA(uw, w, 16, le); |
| 241 | DEFINE_LDST_DMA(l, l, 32, le); |
| 242 | DEFINE_LDST_DMA(q, q, 64, le); |
| 243 | DEFINE_LDST_DMA(uw, w, 16, be); |
| 244 | DEFINE_LDST_DMA(l, l, 32, be); |
| 245 | DEFINE_LDST_DMA(q, q, 64, be); |
| 246 | |
| 247 | #undef DEFINE_LDST_DMA |
| 248 | |
Avi Kivity | b90600e | 2012-10-03 16:42:37 +0200 | [diff] [blame] | 249 | void dma_context_init(DMAContext *dma, AddressSpace *as, DMATranslateFunc translate, |
David Gibson | e5332e6 | 2012-06-27 14:50:43 +1000 | [diff] [blame] | 250 | DMAMapFunc map, DMAUnmapFunc unmap); |
| 251 | |
Paolo Bonzini | 10dc8ae | 2011-09-16 16:40:01 +0200 | [diff] [blame] | 252 | struct ScatterGatherEntry { |
David Gibson | d323118 | 2011-10-31 17:06:46 +1100 | [diff] [blame] | 253 | dma_addr_t base; |
| 254 | dma_addr_t len; |
Paolo Bonzini | 10dc8ae | 2011-09-16 16:40:01 +0200 | [diff] [blame] | 255 | }; |
aliguori | 244ab90 | 2009-02-05 21:23:50 +0000 | [diff] [blame] | 256 | |
David Gibson | c65bcef | 2012-06-27 14:50:40 +1000 | [diff] [blame] | 257 | void qemu_sglist_init(QEMUSGList *qsg, int alloc_hint, DMAContext *dma); |
David Gibson | d323118 | 2011-10-31 17:06:46 +1100 | [diff] [blame] | 258 | 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] | 259 | void qemu_sglist_destroy(QEMUSGList *qsg); |
Paolo Bonzini | 10dc8ae | 2011-09-16 16:40:01 +0200 | [diff] [blame] | 260 | #endif |
aliguori | 244ab90 | 2009-02-05 21:23:50 +0000 | [diff] [blame] | 261 | |
Christoph Hellwig | cb144cc | 2011-05-19 10:57:59 +0200 | [diff] [blame] | 262 | typedef BlockDriverAIOCB *DMAIOFunc(BlockDriverState *bs, int64_t sector_num, |
| 263 | QEMUIOVector *iov, int nb_sectors, |
| 264 | BlockDriverCompletionFunc *cb, void *opaque); |
| 265 | |
| 266 | BlockDriverAIOCB *dma_bdrv_io(BlockDriverState *bs, |
| 267 | QEMUSGList *sg, uint64_t sector_num, |
| 268 | DMAIOFunc *io_func, BlockDriverCompletionFunc *cb, |
David Gibson | 43cf8ae | 2012-03-27 13:42:23 +1100 | [diff] [blame] | 269 | void *opaque, DMADirection dir); |
aliguori | 59a703e | 2009-02-05 21:23:58 +0000 | [diff] [blame] | 270 | BlockDriverAIOCB *dma_bdrv_read(BlockDriverState *bs, |
| 271 | QEMUSGList *sg, uint64_t sector, |
| 272 | BlockDriverCompletionFunc *cb, void *opaque); |
| 273 | BlockDriverAIOCB *dma_bdrv_write(BlockDriverState *bs, |
| 274 | QEMUSGList *sg, uint64_t sector, |
| 275 | BlockDriverCompletionFunc *cb, void *opaque); |
Paolo Bonzini | 8171ee3 | 2011-07-06 08:02:14 +0200 | [diff] [blame] | 276 | uint64_t dma_buf_read(uint8_t *ptr, int32_t len, QEMUSGList *sg); |
| 277 | uint64_t dma_buf_write(uint8_t *ptr, int32_t len, QEMUSGList *sg); |
| 278 | |
Paolo Bonzini | 84a6935 | 2011-09-05 14:20:29 +0200 | [diff] [blame] | 279 | void dma_acct_start(BlockDriverState *bs, BlockAcctCookie *cookie, |
| 280 | QEMUSGList *sg, enum BlockAcctType type); |
| 281 | |
aliguori | 244ab90 | 2009-02-05 21:23:50 +0000 | [diff] [blame] | 282 | #endif |