2 * Virtio Accessor Support: In case your target can change endian.
4 * Copyright IBM, Corp. 2013
7 * Rusty Russell <rusty@au.ibm.com>
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation, either version 2 of the License, or
12 * (at your option) any later version.
16 #ifndef QEMU_VIRTIO_ACCESS_H
17 #define QEMU_VIRTIO_ACCESS_H
19 #include "hw/virtio/virtio.h"
20 #include "hw/virtio/virtio-bus.h"
21 #include "exec/address-spaces.h"
23 #if defined(TARGET_PPC64) || defined(TARGET_ARM)
24 #define LEGACY_VIRTIO_IS_BIENDIAN 1
27 static inline bool virtio_access_is_big_endian(VirtIODevice
*vdev
)
29 #if defined(LEGACY_VIRTIO_IS_BIENDIAN)
30 return virtio_is_big_endian(vdev
);
31 #elif defined(TARGET_WORDS_BIGENDIAN)
32 if (virtio_vdev_has_feature(vdev
, VIRTIO_F_VERSION_1
)) {
33 /* Devices conforming to VIRTIO 1.0 or later are always LE. */
42 static inline uint16_t virtio_lduw_phys(VirtIODevice
*vdev
, hwaddr pa
)
44 AddressSpace
*dma_as
= vdev
->dma_as
;
46 if (virtio_access_is_big_endian(vdev
)) {
47 return lduw_be_phys(dma_as
, pa
);
49 return lduw_le_phys(dma_as
, pa
);
52 static inline uint32_t virtio_ldl_phys(VirtIODevice
*vdev
, hwaddr pa
)
54 AddressSpace
*dma_as
= vdev
->dma_as
;
56 if (virtio_access_is_big_endian(vdev
)) {
57 return ldl_be_phys(dma_as
, pa
);
59 return ldl_le_phys(dma_as
, pa
);
62 static inline uint64_t virtio_ldq_phys(VirtIODevice
*vdev
, hwaddr pa
)
64 AddressSpace
*dma_as
= vdev
->dma_as
;
66 if (virtio_access_is_big_endian(vdev
)) {
67 return ldq_be_phys(dma_as
, pa
);
69 return ldq_le_phys(dma_as
, pa
);
72 static inline void virtio_stw_phys(VirtIODevice
*vdev
, hwaddr pa
,
75 AddressSpace
*dma_as
= vdev
->dma_as
;
77 if (virtio_access_is_big_endian(vdev
)) {
78 stw_be_phys(dma_as
, pa
, value
);
80 stw_le_phys(dma_as
, pa
, value
);
84 static inline void virtio_stl_phys(VirtIODevice
*vdev
, hwaddr pa
,
87 AddressSpace
*dma_as
= vdev
->dma_as
;
89 if (virtio_access_is_big_endian(vdev
)) {
90 stl_be_phys(dma_as
, pa
, value
);
92 stl_le_phys(dma_as
, pa
, value
);
96 static inline void virtio_stw_p(VirtIODevice
*vdev
, void *ptr
, uint16_t v
)
98 if (virtio_access_is_big_endian(vdev
)) {
105 static inline void virtio_stl_p(VirtIODevice
*vdev
, void *ptr
, uint32_t v
)
107 if (virtio_access_is_big_endian(vdev
)) {
114 static inline void virtio_stq_p(VirtIODevice
*vdev
, void *ptr
, uint64_t v
)
116 if (virtio_access_is_big_endian(vdev
)) {
123 static inline int virtio_lduw_p(VirtIODevice
*vdev
, const void *ptr
)
125 if (virtio_access_is_big_endian(vdev
)) {
126 return lduw_be_p(ptr
);
128 return lduw_le_p(ptr
);
132 static inline int virtio_ldl_p(VirtIODevice
*vdev
, const void *ptr
)
134 if (virtio_access_is_big_endian(vdev
)) {
135 return ldl_be_p(ptr
);
137 return ldl_le_p(ptr
);
141 static inline uint64_t virtio_ldq_p(VirtIODevice
*vdev
, const void *ptr
)
143 if (virtio_access_is_big_endian(vdev
)) {
144 return ldq_be_p(ptr
);
146 return ldq_le_p(ptr
);
150 static inline uint16_t virtio_tswap16(VirtIODevice
*vdev
, uint16_t s
)
152 #ifdef HOST_WORDS_BIGENDIAN
153 return virtio_access_is_big_endian(vdev
) ? s
: bswap16(s
);
155 return virtio_access_is_big_endian(vdev
) ? bswap16(s
) : s
;
159 static inline uint16_t virtio_lduw_phys_cached(VirtIODevice
*vdev
,
160 MemoryRegionCache
*cache
,
163 if (virtio_access_is_big_endian(vdev
)) {
164 return lduw_be_phys_cached(cache
, pa
);
166 return lduw_le_phys_cached(cache
, pa
);
169 static inline uint32_t virtio_ldl_phys_cached(VirtIODevice
*vdev
,
170 MemoryRegionCache
*cache
,
173 if (virtio_access_is_big_endian(vdev
)) {
174 return ldl_be_phys_cached(cache
, pa
);
176 return ldl_le_phys_cached(cache
, pa
);
179 static inline uint64_t virtio_ldq_phys_cached(VirtIODevice
*vdev
,
180 MemoryRegionCache
*cache
,
183 if (virtio_access_is_big_endian(vdev
)) {
184 return ldq_be_phys_cached(cache
, pa
);
186 return ldq_le_phys_cached(cache
, pa
);
189 static inline void virtio_stw_phys_cached(VirtIODevice
*vdev
,
190 MemoryRegionCache
*cache
,
191 hwaddr pa
, uint16_t value
)
193 if (virtio_access_is_big_endian(vdev
)) {
194 stw_be_phys_cached(cache
, pa
, value
);
196 stw_le_phys_cached(cache
, pa
, value
);
200 static inline void virtio_stl_phys_cached(VirtIODevice
*vdev
,
201 MemoryRegionCache
*cache
,
202 hwaddr pa
, uint32_t value
)
204 if (virtio_access_is_big_endian(vdev
)) {
205 stl_be_phys_cached(cache
, pa
, value
);
207 stl_le_phys_cached(cache
, pa
, value
);
211 static inline void virtio_tswap16s(VirtIODevice
*vdev
, uint16_t *s
)
213 *s
= virtio_tswap16(vdev
, *s
);
216 static inline uint32_t virtio_tswap32(VirtIODevice
*vdev
, uint32_t s
)
218 #ifdef HOST_WORDS_BIGENDIAN
219 return virtio_access_is_big_endian(vdev
) ? s
: bswap32(s
);
221 return virtio_access_is_big_endian(vdev
) ? bswap32(s
) : s
;
225 static inline void virtio_tswap32s(VirtIODevice
*vdev
, uint32_t *s
)
227 *s
= virtio_tswap32(vdev
, *s
);
230 static inline uint64_t virtio_tswap64(VirtIODevice
*vdev
, uint64_t s
)
232 #ifdef HOST_WORDS_BIGENDIAN
233 return virtio_access_is_big_endian(vdev
) ? s
: bswap64(s
);
235 return virtio_access_is_big_endian(vdev
) ? bswap64(s
) : s
;
239 static inline void virtio_tswap64s(VirtIODevice
*vdev
, uint64_t *s
)
241 *s
= virtio_tswap64(vdev
, *s
);
243 #endif /* QEMU_VIRTIO_ACCESS_H */