Statistics
| Branch: | Revision:

root / include / sysemu / dma.h @ 24addbc7

History | View | Annotate | Download (7.6 kB)

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>
14
#include "exec/memory.h"
15
#include "hw/hw.h"
16
#include "block/block.h"
17
#include "sysemu/kvm.h"
18

    
19
typedef struct DMAContext DMAContext;
20
typedef struct ScatterGatherEntry ScatterGatherEntry;
21

    
22
typedef enum {
23
    DMA_DIRECTION_TO_DEVICE = 0,
24
    DMA_DIRECTION_FROM_DEVICE = 1,
25
} DMADirection;
26

    
27
struct QEMUSGList {
28
    ScatterGatherEntry *sg;
29
    int nsg;
30
    int nalloc;
31
    size_t size;
32
    DMAContext *dma;
33
};
34

    
35
#ifndef CONFIG_USER_ONLY
36

    
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
struct DMAContext {
50
    AddressSpace *as;
51
};
52

    
53
/* A global DMA context corresponding to the address_space_memory
54
 * AddressSpace, for sysbus devices which do DMA.
55
 */
56
extern DMAContext dma_context_memory;
57

    
58
static inline void dma_barrier(DMAContext *dma, DMADirection dir)
59
{
60
    /*
61
     * This is called before DMA read and write operations
62
     * unless the _relaxed form is used and is responsible
63
     * for providing some sane ordering of accesses vs
64
     * concurrently running VCPUs.
65
     *
66
     * Users of map(), unmap() or lower level st/ld_*
67
     * operations are responsible for providing their own
68
     * ordering via barriers.
69
     *
70
     * This primitive implementation does a simple smp_mb()
71
     * before each operation which provides pretty much full
72
     * ordering.
73
     *
74
     * A smarter implementation can be devised if needed to
75
     * use lighter barriers based on the direction of the
76
     * transfer, the DMA context, etc...
77
     */
78
    if (kvm_enabled()) {
79
        smp_mb();
80
    }
81
}
82

    
83
/* Checks that the given range of addresses is valid for DMA.  This is
84
 * useful for certain cases, but usually you should just use
85
 * dma_memory_{read,write}() and check for errors */
86
static inline bool dma_memory_valid(DMAContext *dma,
87
                                    dma_addr_t addr, dma_addr_t len,
88
                                    DMADirection dir)
89
{
90
    return address_space_access_valid(dma->as, addr, len,
91
                                      dir == DMA_DIRECTION_FROM_DEVICE);
92
}
93

    
94
static inline int dma_memory_rw_relaxed(DMAContext *dma, dma_addr_t addr,
95
                                        void *buf, dma_addr_t len,
96
                                        DMADirection dir)
97
{
98
    return address_space_rw(dma->as, addr, buf, len, dir == DMA_DIRECTION_FROM_DEVICE);
99
}
100

    
101
static inline int dma_memory_read_relaxed(DMAContext *dma, dma_addr_t addr,
102
                                          void *buf, dma_addr_t len)
103
{
104
    return dma_memory_rw_relaxed(dma, addr, buf, len, DMA_DIRECTION_TO_DEVICE);
105
}
106

    
107
static inline int dma_memory_write_relaxed(DMAContext *dma, dma_addr_t addr,
108
                                           const void *buf, dma_addr_t len)
109
{
110
    return dma_memory_rw_relaxed(dma, addr, (void *)buf, len,
111
                                 DMA_DIRECTION_FROM_DEVICE);
112
}
113

    
114
static inline int dma_memory_rw(DMAContext *dma, dma_addr_t addr,
115
                                void *buf, dma_addr_t len,
116
                                DMADirection dir)
117
{
118
    dma_barrier(dma, dir);
119

    
120
    return dma_memory_rw_relaxed(dma, addr, buf, len, dir);
121
}
122

    
123
static inline int dma_memory_read(DMAContext *dma, dma_addr_t addr,
124
                                  void *buf, dma_addr_t len)
125
{
126
    return dma_memory_rw(dma, addr, buf, len, DMA_DIRECTION_TO_DEVICE);
127
}
128

    
129
static inline int dma_memory_write(DMAContext *dma, dma_addr_t addr,
130
                                   const void *buf, dma_addr_t len)
131
{
132
    return dma_memory_rw(dma, addr, (void *)buf, len,
133
                         DMA_DIRECTION_FROM_DEVICE);
134
}
135

    
136
int dma_memory_set(DMAContext *dma, dma_addr_t addr, uint8_t c, dma_addr_t len);
137

    
138
static inline void *dma_memory_map(DMAContext *dma,
139
                                   dma_addr_t addr, dma_addr_t *len,
140
                                   DMADirection dir)
141
{
142
    hwaddr xlen = *len;
143
    void *p;
144

    
145
    p = address_space_map(dma->as, addr, &xlen, dir == DMA_DIRECTION_FROM_DEVICE);
146
    *len = xlen;
147
    return p;
148
}
149

    
150
static inline void dma_memory_unmap(DMAContext *dma,
151
                                    void *buffer, dma_addr_t len,
152
                                    DMADirection dir, dma_addr_t access_len)
153
{
154
    address_space_unmap(dma->as, buffer, (hwaddr)len,
155
                        dir == DMA_DIRECTION_FROM_DEVICE, access_len);
156
}
157

    
158
#define DEFINE_LDST_DMA(_lname, _sname, _bits, _end) \
159
    static inline uint##_bits##_t ld##_lname##_##_end##_dma(DMAContext *dma, \
160
                                                            dma_addr_t addr) \
161
    {                                                                   \
162
        uint##_bits##_t val;                                            \
163
        dma_memory_read(dma, addr, &val, (_bits) / 8);                  \
164
        return _end##_bits##_to_cpu(val);                               \
165
    }                                                                   \
166
    static inline void st##_sname##_##_end##_dma(DMAContext *dma,       \
167
                                                 dma_addr_t addr,       \
168
                                                 uint##_bits##_t val)   \
169
    {                                                                   \
170
        val = cpu_to_##_end##_bits(val);                                \
171
        dma_memory_write(dma, addr, &val, (_bits) / 8);                 \
172
    }
173

    
174
static inline uint8_t ldub_dma(DMAContext *dma, dma_addr_t addr)
175
{
176
    uint8_t val;
177

    
178
    dma_memory_read(dma, addr, &val, 1);
179
    return val;
180
}
181

    
182
static inline void stb_dma(DMAContext *dma, dma_addr_t addr, uint8_t val)
183
{
184
    dma_memory_write(dma, addr, &val, 1);
185
}
186

    
187
DEFINE_LDST_DMA(uw, w, 16, le);
188
DEFINE_LDST_DMA(l, l, 32, le);
189
DEFINE_LDST_DMA(q, q, 64, le);
190
DEFINE_LDST_DMA(uw, w, 16, be);
191
DEFINE_LDST_DMA(l, l, 32, be);
192
DEFINE_LDST_DMA(q, q, 64, be);
193

    
194
#undef DEFINE_LDST_DMA
195

    
196
void dma_context_init(DMAContext *dma, AddressSpace *as);
197

    
198
struct ScatterGatherEntry {
199
    dma_addr_t base;
200
    dma_addr_t len;
201
};
202

    
203
void qemu_sglist_init(QEMUSGList *qsg, int alloc_hint, DMAContext *dma);
204
void qemu_sglist_add(QEMUSGList *qsg, dma_addr_t base, dma_addr_t len);
205
void qemu_sglist_destroy(QEMUSGList *qsg);
206
#endif
207

    
208
typedef BlockDriverAIOCB *DMAIOFunc(BlockDriverState *bs, int64_t sector_num,
209
                                 QEMUIOVector *iov, int nb_sectors,
210
                                 BlockDriverCompletionFunc *cb, void *opaque);
211

    
212
BlockDriverAIOCB *dma_bdrv_io(BlockDriverState *bs,
213
                              QEMUSGList *sg, uint64_t sector_num,
214
                              DMAIOFunc *io_func, BlockDriverCompletionFunc *cb,
215
                              void *opaque, DMADirection dir);
216
BlockDriverAIOCB *dma_bdrv_read(BlockDriverState *bs,
217
                                QEMUSGList *sg, uint64_t sector,
218
                                BlockDriverCompletionFunc *cb, void *opaque);
219
BlockDriverAIOCB *dma_bdrv_write(BlockDriverState *bs,
220
                                 QEMUSGList *sg, uint64_t sector,
221
                                 BlockDriverCompletionFunc *cb, void *opaque);
222
uint64_t dma_buf_read(uint8_t *ptr, int32_t len, QEMUSGList *sg);
223
uint64_t dma_buf_write(uint8_t *ptr, int32_t len, QEMUSGList *sg);
224

    
225
void dma_acct_start(BlockDriverState *bs, BlockAcctCookie *cookie,
226
                    QEMUSGList *sg, enum BlockAcctType type);
227

    
228
#endif