master
h 119 lines 2.76 KB
Raw
1 /*
2 * Virtio Accessor Support: In case your target can change endian.
3 *
4 * Copyright IBM, Corp. 2013
5 *
6 * Authors:
7 * Rusty Russell <rusty@au.ibm.com>
8 *
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.
13 *
14 */
15
16 #ifndef QEMU_VIRTIO_ACCESS_H
17 #define QEMU_VIRTIO_ACCESS_H
18
19 #include "exec/hwaddr.h"
20 #include "system/memory_cached.h"
21 #include "hw/virtio/virtio.h"
22 #include "hw/virtio/virtio-bus.h"
23
24 static inline void virtio_stw_p(VirtIODevice *vdev, void *ptr, uint16_t v)
25 {
26 if (virtio_vdev_is_big_endian(vdev)) {
27 stw_be_p(ptr, v);
28 } else {
29 stw_le_p(ptr, v);
30 }
31 }
32
33 static inline void virtio_stl_p(VirtIODevice *vdev, void *ptr, uint32_t v)
34 {
35 if (virtio_vdev_is_big_endian(vdev)) {
36 stl_be_p(ptr, v);
37 } else {
38 stl_le_p(ptr, v);
39 }
40 }
41
42 static inline void virtio_stq_p(VirtIODevice *vdev, void *ptr, uint64_t v)
43 {
44 if (virtio_vdev_is_big_endian(vdev)) {
45 stq_be_p(ptr, v);
46 } else {
47 stq_le_p(ptr, v);
48 }
49 }
50
51 static inline int virtio_lduw_p(VirtIODevice *vdev, const void *ptr)
52 {
53 if (virtio_vdev_is_big_endian(vdev)) {
54 return lduw_be_p(ptr);
55 } else {
56 return lduw_le_p(ptr);
57 }
58 }
59
60 static inline int virtio_ldl_p(VirtIODevice *vdev, const void *ptr)
61 {
62 if (virtio_vdev_is_big_endian(vdev)) {
63 return ldl_be_p(ptr);
64 } else {
65 return ldl_le_p(ptr);
66 }
67 }
68
69 static inline uint64_t virtio_ldq_p(VirtIODevice *vdev, const void *ptr)
70 {
71 if (virtio_vdev_is_big_endian(vdev)) {
72 return ldq_be_p(ptr);
73 } else {
74 return ldq_le_p(ptr);
75 }
76 }
77
78 static inline uint16_t virtio_tswap16(VirtIODevice *vdev, uint16_t s)
79 {
80 #if HOST_BIG_ENDIAN
81 return virtio_vdev_is_big_endian(vdev) ? s : bswap16(s);
82 #else
83 return virtio_vdev_is_big_endian(vdev) ? bswap16(s) : s;
84 #endif
85 }
86
87 static inline void virtio_tswap16s(VirtIODevice *vdev, uint16_t *s)
88 {
89 *s = virtio_tswap16(vdev, *s);
90 }
91
92 static inline uint32_t virtio_tswap32(VirtIODevice *vdev, uint32_t s)
93 {
94 #if HOST_BIG_ENDIAN
95 return virtio_vdev_is_big_endian(vdev) ? s : bswap32(s);
96 #else
97 return virtio_vdev_is_big_endian(vdev) ? bswap32(s) : s;
98 #endif
99 }
100
101 static inline void virtio_tswap32s(VirtIODevice *vdev, uint32_t *s)
102 {
103 *s = virtio_tswap32(vdev, *s);
104 }
105
106 static inline uint64_t virtio_tswap64(VirtIODevice *vdev, uint64_t s)
107 {
108 #if HOST_BIG_ENDIAN
109 return virtio_vdev_is_big_endian(vdev) ? s : bswap64(s);
110 #else
111 return virtio_vdev_is_big_endian(vdev) ? bswap64(s) : s;
112 #endif
113 }
114
115 static inline void virtio_tswap64s(VirtIODevice *vdev, uint64_t *s)
116 {
117 *s = virtio_tswap64(vdev, *s);
118 }
119 #endif /* QEMU_VIRTIO_ACCESS_H */