1 | #ifndef ETHERBOOT_IO_H
|
---|
2 | #define ETHERBOOT_IO_H
|
---|
3 |
|
---|
4 |
|
---|
5 | /* Amount of relocation etherboot is experiencing */
|
---|
6 | extern unsigned long virt_offset;
|
---|
7 |
|
---|
8 | /* Don't require identity mapped physical memory,
|
---|
9 | * osloader.c is the only valid user at the moment.
|
---|
10 | */
|
---|
11 | static inline unsigned long virt_to_phys(volatile const void *virt_addr)
|
---|
12 | {
|
---|
13 | return ((unsigned long)virt_addr) + virt_offset;
|
---|
14 | }
|
---|
15 |
|
---|
16 | static inline void *phys_to_virt(unsigned long phys_addr)
|
---|
17 | {
|
---|
18 | return (void *)(phys_addr - virt_offset);
|
---|
19 | }
|
---|
20 |
|
---|
21 | /* virt_to_bus converts an addresss inside of etherboot [_start, _end]
|
---|
22 | * into a memory access cards can use.
|
---|
23 | */
|
---|
24 | #define virt_to_bus virt_to_phys
|
---|
25 |
|
---|
26 |
|
---|
27 | /* bus_to_virt reverses virt_to_bus, the address must be output
|
---|
28 | * from virt_to_bus to be valid. This function does not work on
|
---|
29 | * all bus addresses.
|
---|
30 | */
|
---|
31 | #define bus_to_virt phys_to_virt
|
---|
32 |
|
---|
33 | /* ioremap converts a random 32bit bus address into something
|
---|
34 | * etherboot can access.
|
---|
35 | */
|
---|
36 | static inline void *ioremap(unsigned long bus_addr, unsigned long length __unused)
|
---|
37 | {
|
---|
38 | return bus_to_virt(bus_addr);
|
---|
39 | }
|
---|
40 |
|
---|
41 | /* iounmap cleans up anything ioremap had to setup */
|
---|
42 | static inline void iounmap(void *virt_addr __unused)
|
---|
43 | {
|
---|
44 | return;
|
---|
45 | }
|
---|
46 |
|
---|
47 | /*
|
---|
48 | * This file contains the definitions for the x86 IO instructions
|
---|
49 | * inb/inw/inl/outb/outw/outl and the "string versions" of the same
|
---|
50 | * (insb/insw/insl/outsb/outsw/outsl). You can also use "pausing"
|
---|
51 | * versions of the single-IO instructions (inb_p/inw_p/..).
|
---|
52 | *
|
---|
53 | * This file is not meant to be obfuscating: it's just complicated
|
---|
54 | * to (a) handle it all in a way that makes gcc able to optimize it
|
---|
55 | * as well as possible and (b) trying to avoid writing the same thing
|
---|
56 | * over and over again with slight variations and possibly making a
|
---|
57 | * mistake somewhere.
|
---|
58 | */
|
---|
59 |
|
---|
60 | /*
|
---|
61 | * Thanks to James van Artsdalen for a better timing-fix than
|
---|
62 | * the two short jumps: using outb's to a nonexistent port seems
|
---|
63 | * to guarantee better timings even on fast machines.
|
---|
64 | *
|
---|
65 | * On the other hand, I'd like to be sure of a non-existent port:
|
---|
66 | * I feel a bit unsafe about using 0x80 (should be safe, though)
|
---|
67 | *
|
---|
68 | * Linus
|
---|
69 | */
|
---|
70 |
|
---|
71 | #ifdef SLOW_IO_BY_JUMPING
|
---|
72 | #define __SLOW_DOWN_IO __asm__ __volatile__("jmp 1f\n1:\tjmp 1f\n1:")
|
---|
73 | #else
|
---|
74 | #define __SLOW_DOWN_IO __asm__ __volatile__("outb %al,$0x80")
|
---|
75 | #endif
|
---|
76 |
|
---|
77 | #ifdef REALLY_SLOW_IO
|
---|
78 | #define SLOW_DOWN_IO { __SLOW_DOWN_IO; __SLOW_DOWN_IO; __SLOW_DOWN_IO; __SLOW_DOWN_IO; }
|
---|
79 | #else
|
---|
80 | #define SLOW_DOWN_IO __SLOW_DOWN_IO
|
---|
81 | #endif
|
---|
82 |
|
---|
83 | /*
|
---|
84 | * readX/writeX() are used to access memory mapped devices. On some
|
---|
85 | * architectures the memory mapped IO stuff needs to be accessed
|
---|
86 | * differently. On the x86 architecture, we just read/write the
|
---|
87 | * memory location directly.
|
---|
88 | */
|
---|
89 | #define readb(addr) (*(volatile unsigned char *) (addr))
|
---|
90 | #define readw(addr) (*(volatile unsigned short *) (addr))
|
---|
91 | #define readl(addr) (*(volatile unsigned int *) (addr))
|
---|
92 |
|
---|
93 | #define writeb(b,addr) ((*(volatile unsigned char *) (addr)) = (b))
|
---|
94 | #define writew(b,addr) ((*(volatile unsigned short *) (addr)) = (b))
|
---|
95 | #define writel(b,addr) ((*(volatile unsigned int *) (addr)) = (b))
|
---|
96 |
|
---|
97 | #define memcpy_fromio(a,b,c) memcpy((a),(void *)(b),(c))
|
---|
98 | #define memcpy_toio(a,b,c) memcpy((void *)(a),(b),(c))
|
---|
99 |
|
---|
100 | /*
|
---|
101 | * Force strict CPU ordering.
|
---|
102 | * And yes, this is required on UP too when we're talking
|
---|
103 | * to devices.
|
---|
104 | *
|
---|
105 | * For now, "wmb()" doesn't actually do anything, as all
|
---|
106 | * Intel CPU's follow what Intel calls a *Processor Order*,
|
---|
107 | * in which all writes are seen in the program order even
|
---|
108 | * outside the CPU.
|
---|
109 | *
|
---|
110 | * I expect future Intel CPU's to have a weaker ordering,
|
---|
111 | * but I'd also expect them to finally get their act together
|
---|
112 | * and add some real memory barriers if so.
|
---|
113 | *
|
---|
114 | * Some non intel clones support out of order store. wmb() ceases to be a
|
---|
115 | * nop for these.
|
---|
116 | */
|
---|
117 |
|
---|
118 | #define mb() __asm__ __volatile__ ("lock; addl $0,0(%%esp)": : :"memory")
|
---|
119 | #define rmb() mb()
|
---|
120 | #define wmb() mb();
|
---|
121 |
|
---|
122 |
|
---|
123 | /*
|
---|
124 | * Talk about misusing macros..
|
---|
125 | */
|
---|
126 |
|
---|
127 | #define __OUT1(s,x) \
|
---|
128 | extern void __out##s(unsigned x value, unsigned short port); \
|
---|
129 | extern inline void __out##s(unsigned x value, unsigned short port) {
|
---|
130 |
|
---|
131 | #define __OUT2(s,s1,s2) \
|
---|
132 | __asm__ __volatile__ ("out" #s " %" s1 "0,%" s2 "1"
|
---|
133 |
|
---|
134 | #define __OUT(s,s1,x) \
|
---|
135 | __OUT1(s,x) __OUT2(s,s1,"w") : : "a" (value), "d" (port)); } \
|
---|
136 | __OUT1(s##c,x) __OUT2(s,s1,"") : : "a" (value), "id" (port)); } \
|
---|
137 | __OUT1(s##_p,x) __OUT2(s,s1,"w") : : "a" (value), "d" (port)); SLOW_DOWN_IO; } \
|
---|
138 | __OUT1(s##c_p,x) __OUT2(s,s1,"") : : "a" (value), "id" (port)); SLOW_DOWN_IO; }
|
---|
139 |
|
---|
140 | #define __IN1(s,x) \
|
---|
141 | extern unsigned x __in##s(unsigned short port); \
|
---|
142 | extern inline unsigned x __in##s(unsigned short port) { unsigned x _v;
|
---|
143 |
|
---|
144 | #define __IN2(s,s1,s2) \
|
---|
145 | __asm__ __volatile__ ("in" #s " %" s2 "1,%" s1 "0"
|
---|
146 |
|
---|
147 | #define __IN(s,s1,x,i...) \
|
---|
148 | __IN1(s,x) __IN2(s,s1,"w") : "=a" (_v) : "d" (port) ,##i ); return _v; } \
|
---|
149 | __IN1(s##c,x) __IN2(s,s1,"") : "=a" (_v) : "id" (port) ,##i ); return _v; } \
|
---|
150 | __IN1(s##_p,x) __IN2(s,s1,"w") : "=a" (_v) : "d" (port) ,##i ); SLOW_DOWN_IO; return _v; } \
|
---|
151 | __IN1(s##c_p,x) __IN2(s,s1,"") : "=a" (_v) : "id" (port) ,##i ); SLOW_DOWN_IO; return _v; }
|
---|
152 |
|
---|
153 | #define __INS(s) \
|
---|
154 | extern void ins##s(unsigned short port, void * addr, unsigned long count); \
|
---|
155 | extern inline void ins##s(unsigned short port, void * addr, unsigned long count) \
|
---|
156 | { __asm__ __volatile__ ("cld ; rep ; ins" #s \
|
---|
157 | : "=D" (addr), "=c" (count) : "d" (port),"0" (addr),"1" (count)); }
|
---|
158 |
|
---|
159 | #define __OUTS(s) \
|
---|
160 | extern void outs##s(unsigned short port, const void * addr, unsigned long count); \
|
---|
161 | extern inline void outs##s(unsigned short port, const void * addr, unsigned long count) \
|
---|
162 | { __asm__ __volatile__ ("cld ; rep ; outs" #s \
|
---|
163 | : "=S" (addr), "=c" (count) : "d" (port),"0" (addr),"1" (count)); }
|
---|
164 |
|
---|
165 | __IN(b,"", char)
|
---|
166 | __IN(w,"",short)
|
---|
167 | __IN(l,"", long)
|
---|
168 |
|
---|
169 | __OUT(b,"b",char)
|
---|
170 | __OUT(w,"w",short)
|
---|
171 | __OUT(l,,int)
|
---|
172 |
|
---|
173 | __INS(b)
|
---|
174 | __INS(w)
|
---|
175 | __INS(l)
|
---|
176 |
|
---|
177 | __OUTS(b)
|
---|
178 | __OUTS(w)
|
---|
179 | __OUTS(l)
|
---|
180 |
|
---|
181 | /*
|
---|
182 | * Note that due to the way __builtin_constant_p() works, you
|
---|
183 | * - can't use it inside a inline function (it will never be true)
|
---|
184 | * - you don't have to worry about side effects within the __builtin..
|
---|
185 | */
|
---|
186 | #define outb(val,port) \
|
---|
187 | ((__builtin_constant_p((port)) && (port) < 256) ? \
|
---|
188 | __outbc((val),(port)) : \
|
---|
189 | __outb((val),(port)))
|
---|
190 |
|
---|
191 | #define inb(port) \
|
---|
192 | ((__builtin_constant_p((port)) && (port) < 256) ? \
|
---|
193 | __inbc(port) : \
|
---|
194 | __inb(port))
|
---|
195 |
|
---|
196 | #define outb_p(val,port) \
|
---|
197 | ((__builtin_constant_p((port)) && (port) < 256) ? \
|
---|
198 | __outbc_p((val),(port)) : \
|
---|
199 | __outb_p((val),(port)))
|
---|
200 |
|
---|
201 | #define inb_p(port) \
|
---|
202 | ((__builtin_constant_p((port)) && (port) < 256) ? \
|
---|
203 | __inbc_p(port) : \
|
---|
204 | __inb_p(port))
|
---|
205 |
|
---|
206 | #define outw(val,port) \
|
---|
207 | ((__builtin_constant_p((port)) && (port) < 256) ? \
|
---|
208 | __outwc((val),(port)) : \
|
---|
209 | __outw((val),(port)))
|
---|
210 |
|
---|
211 | #define inw(port) \
|
---|
212 | ((__builtin_constant_p((port)) && (port) < 256) ? \
|
---|
213 | __inwc(port) : \
|
---|
214 | __inw(port))
|
---|
215 |
|
---|
216 | #define outw_p(val,port) \
|
---|
217 | ((__builtin_constant_p((port)) && (port) < 256) ? \
|
---|
218 | __outwc_p((val),(port)) : \
|
---|
219 | __outw_p((val),(port)))
|
---|
220 |
|
---|
221 | #define inw_p(port) \
|
---|
222 | ((__builtin_constant_p((port)) && (port) < 256) ? \
|
---|
223 | __inwc_p(port) : \
|
---|
224 | __inw_p(port))
|
---|
225 |
|
---|
226 | #define outl(val,port) \
|
---|
227 | ((__builtin_constant_p((port)) && (port) < 256) ? \
|
---|
228 | __outlc((val),(port)) : \
|
---|
229 | __outl((val),(port)))
|
---|
230 |
|
---|
231 | #define inl(port) \
|
---|
232 | ((__builtin_constant_p((port)) && (port) < 256) ? \
|
---|
233 | __inlc(port) : \
|
---|
234 | __inl(port))
|
---|
235 |
|
---|
236 | #define outl_p(val,port) \
|
---|
237 | ((__builtin_constant_p((port)) && (port) < 256) ? \
|
---|
238 | __outlc_p((val),(port)) : \
|
---|
239 | __outl_p((val),(port)))
|
---|
240 |
|
---|
241 | #define inl_p(port) \
|
---|
242 | ((__builtin_constant_p((port)) && (port) < 256) ? \
|
---|
243 | __inlc_p(port) : \
|
---|
244 | __inl_p(port))
|
---|
245 |
|
---|
246 | #endif /* ETHERBOOT_IO_H */
|
---|