MAINTAINERS: Make section QOM cover hw/core/*bus.c as well
[qemu/armbru.git] / include / hw / virtio / virtio-access.h
blob6818a23a2d351858dd60e4b3b72b2e1c86b416da
1 /*
2 * Virtio Accessor Support: In case your target can change endian.
4 * Copyright IBM, Corp. 2013
6 * Authors:
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 "exec/hwaddr.h"
20 #include "hw/virtio/virtio.h"
21 #include "hw/virtio/virtio-bus.h"
23 #if defined(TARGET_PPC64) || defined(TARGET_ARM)
24 #define LEGACY_VIRTIO_IS_BIENDIAN 1
25 #endif
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. */
34 return false;
36 return true;
37 #else
38 return false;
39 #endif
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,
73 uint16_t value)
75 AddressSpace *dma_as = vdev->dma_as;
77 if (virtio_access_is_big_endian(vdev)) {
78 stw_be_phys(dma_as, pa, value);
79 } else {
80 stw_le_phys(dma_as, pa, value);
84 static inline void virtio_stl_phys(VirtIODevice *vdev, hwaddr pa,
85 uint32_t value)
87 AddressSpace *dma_as = vdev->dma_as;
89 if (virtio_access_is_big_endian(vdev)) {
90 stl_be_phys(dma_as, pa, value);
91 } else {
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)) {
99 stw_be_p(ptr, v);
100 } else {
101 stw_le_p(ptr, v);
105 static inline void virtio_stl_p(VirtIODevice *vdev, void *ptr, uint32_t v)
107 if (virtio_access_is_big_endian(vdev)) {
108 stl_be_p(ptr, v);
109 } else {
110 stl_le_p(ptr, v);
114 static inline void virtio_stq_p(VirtIODevice *vdev, void *ptr, uint64_t v)
116 if (virtio_access_is_big_endian(vdev)) {
117 stq_be_p(ptr, v);
118 } else {
119 stq_le_p(ptr, v);
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);
127 } else {
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);
136 } else {
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);
145 } else {
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);
154 #else
155 return virtio_access_is_big_endian(vdev) ? bswap16(s) : s;
156 #endif
159 static inline uint16_t virtio_lduw_phys_cached(VirtIODevice *vdev,
160 MemoryRegionCache *cache,
161 hwaddr pa)
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,
171 hwaddr pa)
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,
181 hwaddr pa)
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);
195 } else {
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);
206 } else {
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);
220 #else
221 return virtio_access_is_big_endian(vdev) ? bswap32(s) : s;
222 #endif
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);
234 #else
235 return virtio_access_is_big_endian(vdev) ? bswap64(s) : s;
236 #endif
239 static inline void virtio_tswap64s(VirtIODevice *vdev, uint64_t *s)
241 *s = virtio_tswap64(vdev, *s);
243 #endif /* QEMU_VIRTIO_ACCESS_H */