1 /* SPDX-License-Identifier: GPL-2.0+ */
2 /*
3  * Copyright (c) 2011 The Chromium OS Authors.
4  */
5 
6 #ifndef __SANDBOX_ASM_IO_H
7 #define __SANDBOX_ASM_IO_H
8 
9 enum sandboxio_size_t {
10 	SB_SIZE_8,
11 	SB_SIZE_16,
12 	SB_SIZE_32,
13 	SB_SIZE_64,
14 };
15 
16 void *phys_to_virt(phys_addr_t paddr);
17 #define phys_to_virt phys_to_virt
18 
19 phys_addr_t virt_to_phys(void *vaddr);
20 #define virt_to_phys virt_to_phys
21 
22 void *map_physmem(phys_addr_t paddr, unsigned long len, unsigned long flags);
23 #define map_physmem map_physmem
24 
25 /*
26  * Take down a mapping set up by map_physmem().
27  */
28 void unmap_physmem(const void *vaddr, unsigned long flags);
29 #define unmap_physmem unmap_physmem
30 
31 #include <asm-generic/io.h>
32 
33 /* For sandbox, we want addresses to point into our RAM buffer */
map_sysmem(phys_addr_t paddr,unsigned long len)34 static inline void *map_sysmem(phys_addr_t paddr, unsigned long len)
35 {
36 	return map_physmem(paddr, len, MAP_WRBACK);
37 }
38 
39 /* Remove a previous mapping */
unmap_sysmem(const void * vaddr)40 static inline void unmap_sysmem(const void *vaddr)
41 {
42 	unmap_physmem(vaddr, MAP_WRBACK);
43 }
44 
45 /* Map from a pointer to our RAM buffer */
46 phys_addr_t map_to_sysmem(const void *ptr);
47 
48 unsigned int sandbox_read(const void *addr, enum sandboxio_size_t size);
49 void sandbox_write(void *addr, unsigned int val, enum sandboxio_size_t size);
50 
51 #define readb(addr) sandbox_read((const void *)addr, SB_SIZE_8)
52 #define readw(addr) sandbox_read((const void *)addr, SB_SIZE_16)
53 #define readl(addr) sandbox_read((const void *)addr, SB_SIZE_32)
54 #ifdef CONFIG_SANDBOX64
55 #define readq(addr) sandbox_read((const void *)addr, SB_SIZE_64)
56 #endif
57 #define writeb(v, addr) sandbox_write((void *)addr, v, SB_SIZE_8)
58 #define writew(v, addr) sandbox_write((void *)addr, v, SB_SIZE_16)
59 #define writel(v, addr) sandbox_write((void *)addr, v, SB_SIZE_32)
60 #ifdef CONFIG_SANDBOX64
61 #define writeq(v, addr) sandbox_write((void *)addr, v, SB_SIZE_64)
62 #endif
63 
64 /*
65  * Clear and set bits in one shot. These macros can be used to clear and
66  * set multiple bits in a register using a single call. These macros can
67  * also be used to set a multiple-bit bit pattern using a mask, by
68  * specifying the mask in the 'clear' parameter and the new bit pattern
69  * in the 'set' parameter.
70  */
71 
72 #define out_arch(type,endian,a,v)	write##type(cpu_to_##endian(v),a)
73 #define in_arch(type,endian,a)		endian##_to_cpu(read##type(a))
74 
75 #define out_le64(a,v)	out_arch(q,le64,a,v)
76 #define out_le32(a,v)	out_arch(l,le32,a,v)
77 #define out_le16(a,v)	out_arch(w,le16,a,v)
78 
79 #define in_le64(a)	in_arch(q,le64,a)
80 #define in_le32(a)	in_arch(l,le32,a)
81 #define in_le16(a)	in_arch(w,le16,a)
82 
83 #define out_be32(a,v)	out_arch(l,be32,a,v)
84 #define out_be16(a,v)	out_arch(w,be16,a,v)
85 
86 #define in_be32(a)	in_arch(l,be32,a)
87 #define in_be16(a)	in_arch(w,be16,a)
88 
89 #define out_8(a,v)	writeb(v,a)
90 #define in_8(a)		readb(a)
91 
92 #define clrbits(type, addr, clear) \
93 	out_##type((addr), in_##type(addr) & ~(clear))
94 
95 #define setbits(type, addr, set) \
96 	out_##type((addr), in_##type(addr) | (set))
97 
98 #define clrsetbits(type, addr, clear, set) \
99 	out_##type((addr), (in_##type(addr) & ~(clear)) | (set))
100 
101 #define clrbits_be32(addr, clear) clrbits(be32, addr, clear)
102 #define setbits_be32(addr, set) setbits(be32, addr, set)
103 #define clrsetbits_be32(addr, clear, set) clrsetbits(be32, addr, clear, set)
104 
105 #define clrbits_le32(addr, clear) clrbits(le32, addr, clear)
106 #define setbits_le32(addr, set) setbits(le32, addr, set)
107 #define clrsetbits_le32(addr, clear, set) clrsetbits(le32, addr, clear, set)
108 
109 #define clrbits_be16(addr, clear) clrbits(be16, addr, clear)
110 #define setbits_be16(addr, set) setbits(be16, addr, set)
111 #define clrsetbits_be16(addr, clear, set) clrsetbits(be16, addr, clear, set)
112 
113 #define clrbits_le16(addr, clear) clrbits(le16, addr, clear)
114 #define setbits_le16(addr, set) setbits(le16, addr, set)
115 #define clrsetbits_le16(addr, clear, set) clrsetbits(le16, addr, clear, set)
116 
117 #define clrbits_8(addr, clear) clrbits(8, addr, clear)
118 #define setbits_8(addr, set) setbits(8, addr, set)
119 #define clrsetbits_8(addr, clear, set) clrsetbits(8, addr, clear, set)
120 
121 /* I/O access functions */
122 int _inl(unsigned int addr);
123 int _inw(unsigned int addr);
124 int _inb(unsigned int addr);
125 
126 void _outl(unsigned int value, unsigned int addr);
127 void _outw(unsigned int value, unsigned int addr);
128 void _outb(unsigned int value, unsigned int addr);
129 
130 #define inb(port)	_inb((uintptr_t)(port))
131 #define inw(port)	_inw((uintptr_t)(port))
132 #define inl(port)	_inl((uintptr_t)(port))
133 
134 #define outb(val, port)	_outb(val, (uintptr_t)(port))
135 #define outw(val, port)	_outw(val, (uintptr_t)(port))
136 #define outl(val, port)	_outl(val, (uintptr_t)(port))
137 
138 #define out_arch(type,endian,a,v)	write##type(cpu_to_##endian(v),a)
139 #define in_arch(type,endian,a)		endian##_to_cpu(read##type(a))
140 
141 #define out_le32(a,v)	out_arch(l,le32,a,v)
142 #define out_le16(a,v)	out_arch(w,le16,a,v)
143 
144 #define in_le32(a)	in_arch(l,le32,a)
145 #define in_le16(a)	in_arch(w,le16,a)
146 
147 #define out_be32(a,v)	out_arch(l,be32,a,v)
148 #define out_be16(a,v)	out_arch(w,be16,a,v)
149 
150 #define in_be32(a)	in_arch(l,be32,a)
151 #define in_be16(a)	in_arch(w,be16,a)
152 
153 #define out_8(a,v)	writeb(v,a)
154 #define in_8(a)		readb(a)
155 
156 #define clrbits(type, addr, clear) \
157 	out_##type((addr), in_##type(addr) & ~(clear))
158 
159 #define setbits(type, addr, set) \
160 	out_##type((addr), in_##type(addr) | (set))
161 
162 #define clrsetbits(type, addr, clear, set) \
163 	out_##type((addr), (in_##type(addr) & ~(clear)) | (set))
164 
165 #define clrbits_be32(addr, clear) clrbits(be32, addr, clear)
166 #define setbits_be32(addr, set) setbits(be32, addr, set)
167 #define clrsetbits_be32(addr, clear, set) clrsetbits(be32, addr, clear, set)
168 
169 #define clrbits_le32(addr, clear) clrbits(le32, addr, clear)
170 #define setbits_le32(addr, set) setbits(le32, addr, set)
171 #define clrsetbits_le32(addr, clear, set) clrsetbits(le32, addr, clear, set)
172 
173 #define clrbits_be16(addr, clear) clrbits(be16, addr, clear)
174 #define setbits_be16(addr, set) setbits(be16, addr, set)
175 #define clrsetbits_be16(addr, clear, set) clrsetbits(be16, addr, clear, set)
176 
177 #define clrbits_le16(addr, clear) clrbits(le16, addr, clear)
178 #define setbits_le16(addr, set) setbits(le16, addr, set)
179 #define clrsetbits_le16(addr, clear, set) clrsetbits(le16, addr, clear, set)
180 
181 #define clrbits_8(addr, clear) clrbits(8, addr, clear)
182 #define setbits_8(addr, set) setbits(8, addr, set)
183 #define clrsetbits_8(addr, clear, set) clrsetbits(8, addr, clear, set)
184 
_insw(volatile u16 * port,void * buf,int ns)185 static inline void _insw(volatile u16 *port, void *buf, int ns)
186 {
187 }
188 
_outsw(volatile u16 * port,const void * buf,int ns)189 static inline void _outsw(volatile u16 *port, const void *buf, int ns)
190 {
191 }
192 
memset_io(volatile void * addr,unsigned char val,int count)193 static inline void memset_io(volatile void *addr, unsigned char val, int count)
194 {
195 }
196 
memcpy_fromio(void * dst,const volatile void * src,int count)197 static inline void memcpy_fromio(void *dst, const volatile void *src, int count)
198 {
199 }
200 
memcpy_toio(volatile void * dst,const void * src,int count)201 static inline void memcpy_toio(volatile void *dst, const void *src, int count)
202 {
203 }
204 
205 #define insw(port, buf, ns)		_insw((u16 *)port, buf, ns)
206 #define outsw(port, buf, ns)		_outsw((u16 *)port, buf, ns)
207 
208 /* IO space accessors */
209 #define clrio(type, addr, clear) \
210 	out##type(in##type(addr) & ~(clear), (addr))
211 
212 #define setio(type, addr, set) \
213 	out##type(in##type(addr) | (set), (addr))
214 
215 #define clrsetio(type, addr, clear, set) \
216 	out##type((in##type(addr) & ~(clear)) | (set), (addr))
217 
218 #define clrio_32(addr, clear) clrio(l, addr, clear)
219 #define clrio_16(addr, clear) clrio(w, addr, clear)
220 #define clrio_8(addr, clear) clrio(b, addr, clear)
221 
222 #define setio_32(addr, set) setio(l, addr, set)
223 #define setio_16(addr, set) setio(w, addr, set)
224 #define setio_8(addr, set) setio(b, addr, set)
225 
226 #define clrsetio_32(addr, clear, set) clrsetio(l, addr, clear, set)
227 #define clrsetio_16(addr, clear, set) clrsetio(w, addr, clear, set)
228 #define clrsetio_8(addr, clear, set) clrsetio(b, addr, clear, set)
229 
230 #include <iotrace.h>
231 #include <asm/types.h>
232 
233 #endif
234