xref: /kvm-unit-tests/lib/asm-generic/io.h (revision d3aacb4f57d05f74f2030dbe12e7dfd6aa1b273d)
1 #ifndef _ASM_GENERIC_IO_H_
2 #define _ASM_GENERIC_IO_H_
3 /*
4  * asm-generic/io.h
5  *  adapted from the Linux kernel's include/asm-generic/io.h
6  *  and arch/arm/include/asm/io.h
7  *
8  * Copyright (C) 2014, Red Hat Inc, Andrew Jones <drjones@redhat.com>
9  *
10  * This work is licensed under the terms of the GNU LGPL, version 2.
11  */
12 #include "libcflat.h"
13 #include "asm/page.h"
14 
15 #ifndef __raw_readb
16 static inline u8 __raw_readb(const volatile void *addr)
17 {
18 	return *(const volatile u8 *)addr;
19 }
20 #endif
21 
22 #ifndef __raw_readw
23 static inline u16 __raw_readw(const volatile void *addr)
24 {
25 	return *(const volatile u16 *)addr;
26 }
27 #endif
28 
29 #ifndef __raw_readl
30 static inline u32 __raw_readl(const volatile void *addr)
31 {
32 	return *(const volatile u32 *)addr;
33 }
34 #endif
35 
36 #ifndef __raw_readq
37 static inline u64 __raw_readq(const volatile void *addr)
38 {
39 	assert(sizeof(unsigned long) == sizeof(u64));
40 	return *(const volatile u64 *)addr;
41 }
42 #endif
43 
44 #ifndef __raw_writeb
45 static inline void __raw_writeb(u8 b, volatile void *addr)
46 {
47 	*(volatile u8 *)addr = b;
48 }
49 #endif
50 
51 #ifndef __raw_writew
52 static inline void __raw_writew(u16 b, volatile void *addr)
53 {
54 	*(volatile u16 *)addr = b;
55 }
56 #endif
57 
58 #ifndef __raw_writel
59 static inline void __raw_writel(u32 b, volatile void *addr)
60 {
61 	*(volatile u32 *)addr = b;
62 }
63 #endif
64 
65 #ifndef __raw_writeq
66 static inline void __raw_writeq(u64 b, volatile void *addr)
67 {
68 	assert(sizeof(unsigned long) == sizeof(u64));
69 	*(volatile u64 *)addr = b;
70 }
71 #endif
72 
73 #ifndef __bswap16
74 static inline u16 __bswap16(u16 x)
75 {
76 	return ((x >> 8) & 0xff) | ((x & 0xff) << 8);
77 }
78 #endif
79 
80 #ifndef __bswap32
81 static inline u32 __bswap32(u32 x)
82 {
83 	return ((x & 0xff000000) >> 24) | ((x & 0x00ff0000) >>  8) |
84 	       ((x & 0x0000ff00) <<  8) | ((x & 0x000000ff) << 24);
85 }
86 #endif
87 
88 #ifndef __bswap64
89 static inline u64 __bswap64(u64 x)
90 {
91 	return ((x & 0x00000000000000ffULL) << 56) |
92 	       ((x & 0x000000000000ff00ULL) << 40) |
93 	       ((x & 0x0000000000ff0000ULL) << 24) |
94 	       ((x & 0x00000000ff000000ULL) <<  8) |
95 	       ((x & 0x000000ff00000000ULL) >>  8) |
96 	       ((x & 0x0000ff0000000000ULL) >> 24) |
97 	       ((x & 0x00ff000000000000ULL) >> 40) |
98 	       ((x & 0xff00000000000000ULL) >> 56);
99 }
100 #endif
101 
102 #ifndef __cpu_is_be
103 #define __cpu_is_be() (0)
104 #endif
105 
106 #define le16_to_cpu(x) \
107 	({ u16 __r = __cpu_is_be() ? __bswap16(x) : (x); __r; })
108 #define cpu_to_le16 le16_to_cpu
109 
110 #define le32_to_cpu(x) \
111 	({ u32 __r = __cpu_is_be() ? __bswap32(x) : (x); __r; })
112 #define cpu_to_le32 le32_to_cpu
113 
114 #define le64_to_cpu(x) \
115 	({ u64 __r = __cpu_is_be() ? __bswap64(x) : (x); __r; })
116 #define cpu_to_le64 le64_to_cpu
117 
118 #define be16_to_cpu(x) \
119 	({ u16 __r = !__cpu_is_be() ? __bswap16(x) : (x); __r; })
120 #define cpu_to_be16 be16_to_cpu
121 
122 #define be32_to_cpu(x) \
123 	({ u32 __r = !__cpu_is_be() ? __bswap32(x) : (x); __r; })
124 #define cpu_to_be32 be32_to_cpu
125 
126 #define be64_to_cpu(x) \
127 	({ u64 __r = !__cpu_is_be() ? __bswap64(x) : (x); __r; })
128 #define cpu_to_be64 be64_to_cpu
129 
130 #ifndef rmb
131 #define rmb() do { } while (0)
132 #endif
133 #ifndef wmb
134 #define wmb() do { } while (0)
135 #endif
136 
137 #define readb(addr) \
138 	({ u8 __r = __raw_readb(addr); rmb(); __r; })
139 #define readw(addr) \
140 	({ u16 __r = le16_to_cpu(__raw_readw(addr)); rmb(); __r; })
141 #define readl(addr) \
142 	({ u32 __r = le32_to_cpu(__raw_readl(addr)); rmb(); __r; })
143 #define readq(addr) \
144 	({ u64 __r = le64_to_cpu(__raw_readq(addr)); rmb(); __r; })
145 
146 #define writeb(b, addr) \
147 	({ wmb(); __raw_writeb(b, addr); })
148 #define writew(b, addr) \
149 	({ wmb(); __raw_writew(cpu_to_le16(b), addr); })
150 #define writel(b, addr) \
151 	({ wmb(); __raw_writel(cpu_to_le32(b), addr); })
152 #define writeq(b, addr) \
153 	({ wmb(); __raw_writeq(cpu_to_le64(b), addr); })
154 
155 #ifndef ioremap
156 static inline void *ioremap(u64 phys_addr, size_t size __unused)
157 {
158 	assert(sizeof(long) == 8 || !(phys_addr >> 32));
159 	return (void *)(unsigned long)phys_addr;
160 }
161 #endif
162 
163 #ifndef virt_to_phys
164 static inline unsigned long virt_to_phys(volatile void *address)
165 {
166 	return __pa((unsigned long)address);
167 }
168 
169 static inline void *phys_to_virt(unsigned long address)
170 {
171 	return __va(address);
172 }
173 #endif
174 
175 #endif /* _ASM_GENERIC_IO_H_ */
176