vserver 1.9.3
[linux-2.6.git] / include / asm-i386 / io.h
1 #ifndef _ASM_IO_H
2 #define _ASM_IO_H
3
4 #include <linux/config.h>
5 #include <linux/string.h>
6 #include <linux/compiler.h>
7
8 /*
9  * This file contains the definitions for the x86 IO instructions
10  * inb/inw/inl/outb/outw/outl and the "string versions" of the same
11  * (insb/insw/insl/outsb/outsw/outsl). You can also use "pausing"
12  * versions of the single-IO instructions (inb_p/inw_p/..).
13  *
14  * This file is not meant to be obfuscating: it's just complicated
15  * to (a) handle it all in a way that makes gcc able to optimize it
16  * as well as possible and (b) trying to avoid writing the same thing
17  * over and over again with slight variations and possibly making a
18  * mistake somewhere.
19  */
20
21 /*
22  * Thanks to James van Artsdalen for a better timing-fix than
23  * the two short jumps: using outb's to a nonexistent port seems
24  * to guarantee better timings even on fast machines.
25  *
26  * On the other hand, I'd like to be sure of a non-existent port:
27  * I feel a bit unsafe about using 0x80 (should be safe, though)
28  *
29  *              Linus
30  */
31
32  /*
33   *  Bit simplified and optimized by Jan Hubicka
34   *  Support of BIGMEM added by Gerhard Wichert, Siemens AG, July 1999.
35   *
36   *  isa_memset_io, isa_memcpy_fromio, isa_memcpy_toio added,
37   *  isa_read[wl] and isa_write[wl] fixed
38   *  - Arnaldo Carvalho de Melo <acme@conectiva.com.br>
39   */
40
41 #define IO_SPACE_LIMIT 0xffff
42
43 #define XQUAD_PORTIO_BASE 0xfe400000
44 #define XQUAD_PORTIO_QUAD 0x40000  /* 256k per quad. */
45
46 #ifdef __KERNEL__
47
48 #include <asm-generic/iomap.h>
49
50 #include <linux/vmalloc.h>
51
52 /**
53  *      virt_to_phys    -       map virtual addresses to physical
54  *      @address: address to remap
55  *
56  *      The returned physical address is the physical (CPU) mapping for
57  *      the memory address given. It is only valid to use this function on
58  *      addresses directly mapped or allocated via kmalloc. 
59  *
60  *      This function does not give bus mappings for DMA transfers. In
61  *      almost all conceivable cases a device driver should not be using
62  *      this function
63  */
64  
65 static inline unsigned long virt_to_phys(volatile void * address)
66 {
67         return __pa(address);
68 }
69
70 /**
71  *      phys_to_virt    -       map physical address to virtual
72  *      @address: address to remap
73  *
74  *      The returned virtual address is a current CPU mapping for
75  *      the memory address given. It is only valid to use this function on
76  *      addresses that have a kernel mapping
77  *
78  *      This function does not handle bus mappings for DMA transfers. In
79  *      almost all conceivable cases a device driver should not be using
80  *      this function
81  */
82
83 static inline void * phys_to_virt(unsigned long address)
84 {
85         return __va(address);
86 }
87
88 /*
89  * Change "struct page" to physical address.
90  */
91 #define page_to_phys(page)    ((dma_addr_t)page_to_pfn(page) << PAGE_SHIFT)
92
93 extern void __iomem * __ioremap(unsigned long offset, unsigned long size, unsigned long flags);
94
95 /**
96  * ioremap     -   map bus memory into CPU space
97  * @offset:    bus address of the memory
98  * @size:      size of the resource to map
99  *
100  * ioremap performs a platform specific sequence of operations to
101  * make bus memory CPU accessible via the readb/readw/readl/writeb/
102  * writew/writel functions and the other mmio helpers. The returned
103  * address is not guaranteed to be usable directly as a virtual
104  * address. 
105  */
106
107 static inline void __iomem * ioremap(unsigned long offset, unsigned long size)
108 {
109         return __ioremap(offset, size, 0);
110 }
111
112 extern void __iomem * ioremap_nocache(unsigned long offset, unsigned long size);
113 extern void iounmap(volatile void __iomem *addr);
114
115 /*
116  * bt_ioremap() and bt_iounmap() are for temporary early boot-time
117  * mappings, before the real ioremap() is functional.
118  * A boot-time mapping is currently limited to at most 16 pages.
119  */
120 extern void *bt_ioremap(unsigned long offset, unsigned long size);
121 extern void bt_iounmap(void *addr, unsigned long size);
122
123 /*
124  * ISA I/O bus memory addresses are 1:1 with the physical address.
125  */
126 #define isa_virt_to_bus virt_to_phys
127 #define isa_page_to_bus page_to_phys
128 #define isa_bus_to_virt phys_to_virt
129
130 /*
131  * However PCI ones are not necessarily 1:1 and therefore these interfaces
132  * are forbidden in portable PCI drivers.
133  *
134  * Allow them on x86 for legacy drivers, though.
135  */
136 #define virt_to_bus virt_to_phys
137 #define bus_to_virt phys_to_virt
138
139 /*
140  * readX/writeX() are used to access memory mapped devices. On some
141  * architectures the memory mapped IO stuff needs to be accessed
142  * differently. On the x86 architecture, we just read/write the
143  * memory location directly.
144  */
145
146 static inline unsigned char readb(const volatile void __iomem *addr)
147 {
148         return *(volatile unsigned char __force *) addr;
149 }
150 static inline unsigned short readw(const volatile void __iomem *addr)
151 {
152         return *(volatile unsigned short __force *) addr;
153 }
154 static inline unsigned int readl(const volatile void __iomem *addr)
155 {
156         return *(volatile unsigned int __force *) addr;
157 }
158 #define readb_relaxed(addr) readb(addr)
159 #define readw_relaxed(addr) readw(addr)
160 #define readl_relaxed(addr) readl(addr)
161 #define __raw_readb readb
162 #define __raw_readw readw
163 #define __raw_readl readl
164
165 static inline void writeb(unsigned char b, volatile void __iomem *addr)
166 {
167         *(volatile unsigned char __force *) addr = b;
168 }
169 static inline void writew(unsigned short b, volatile void __iomem *addr)
170 {
171         *(volatile unsigned short __force *) addr = b;
172 }
173 static inline void writel(unsigned int b, volatile void __iomem *addr)
174 {
175         *(volatile unsigned int __force *) addr = b;
176 }
177 #define __raw_writeb writeb
178 #define __raw_writew writew
179 #define __raw_writel writel
180
181 static inline void memset_io(volatile void __iomem *addr, unsigned char val, int count)
182 {
183         memset((void __force *) addr, val, count);
184 }
185 static inline void memcpy_fromio(void *dst, volatile void __iomem *src, int count)
186 {
187         __memcpy(dst, (void __force *) src, count);
188 }
189 static inline void memcpy_toio(volatile void __iomem *dst, const void *src, int count)
190 {
191         __memcpy((void __force *) dst, src, count);
192 }
193
194 /*
195  * ISA space is 'always mapped' on a typical x86 system, no need to
196  * explicitly ioremap() it. The fact that the ISA IO space is mapped
197  * to PAGE_OFFSET is pure coincidence - it does not mean ISA values
198  * are physical addresses. The following constant pointer can be
199  * used as the IO-area pointer (it can be iounmapped as well, so the
200  * analogy with PCI is quite large):
201  */
202 #define __ISA_IO_base ((char __iomem *)(PAGE_OFFSET))
203
204 #define isa_readb(a) readb(__ISA_IO_base + (a))
205 #define isa_readw(a) readw(__ISA_IO_base + (a))
206 #define isa_readl(a) readl(__ISA_IO_base + (a))
207 #define isa_writeb(b,a) writeb(b,__ISA_IO_base + (a))
208 #define isa_writew(w,a) writew(w,__ISA_IO_base + (a))
209 #define isa_writel(l,a) writel(l,__ISA_IO_base + (a))
210 #define isa_memset_io(a,b,c)            memset_io(__ISA_IO_base + (a),(b),(c))
211 #define isa_memcpy_fromio(a,b,c)        memcpy_fromio((a),__ISA_IO_base + (b),(c))
212 #define isa_memcpy_toio(a,b,c)          memcpy_toio(__ISA_IO_base + (a),(b),(c))
213
214
215 /*
216  * Again, i386 does not require mem IO specific function.
217  */
218
219 #define eth_io_copy_and_sum(a,b,c,d)            eth_copy_and_sum((a),(void __force *)(b),(c),(d))
220 #define isa_eth_io_copy_and_sum(a,b,c,d)        eth_copy_and_sum((a),(void __force *)(__ISA_IO_base + (b)),(c),(d))
221
222 /**
223  *      check_signature         -       find BIOS signatures
224  *      @io_addr: mmio address to check 
225  *      @signature:  signature block
226  *      @length: length of signature
227  *
228  *      Perform a signature comparison with the mmio address io_addr. This
229  *      address should have been obtained by ioremap.
230  *      Returns 1 on a match.
231  */
232  
233 static inline int check_signature(volatile void __iomem * io_addr,
234         const unsigned char *signature, int length)
235 {
236         int retval = 0;
237         do {
238                 if (readb(io_addr) != *signature)
239                         goto out;
240                 io_addr++;
241                 signature++;
242                 length--;
243         } while (length);
244         retval = 1;
245 out:
246         return retval;
247 }
248
249 /**
250  *      isa_check_signature             -       find BIOS signatures
251  *      @io_addr: mmio address to check 
252  *      @signature:  signature block
253  *      @length: length of signature
254  *
255  *      Perform a signature comparison with the ISA mmio address io_addr.
256  *      Returns 1 on a match.
257  *
258  *      This function is deprecated. New drivers should use ioremap and
259  *      check_signature.
260  */
261  
262
263 static inline int isa_check_signature(unsigned long io_addr,
264         const unsigned char *signature, int length)
265 {
266         int retval = 0;
267         do {
268                 if (isa_readb(io_addr) != *signature)
269                         goto out;
270                 io_addr++;
271                 signature++;
272                 length--;
273         } while (length);
274         retval = 1;
275 out:
276         return retval;
277 }
278
279 /*
280  *      Cache management
281  *
282  *      This needed for two cases
283  *      1. Out of order aware processors
284  *      2. Accidentally out of order processors (PPro errata #51)
285  */
286  
287 #if defined(CONFIG_X86_OOSTORE) || defined(CONFIG_X86_PPRO_FENCE)
288
289 static inline void flush_write_buffers(void)
290 {
291         __asm__ __volatile__ ("lock; addl $0,0(%%esp)": : :"memory");
292 }
293
294 #define dma_cache_inv(_start,_size)             flush_write_buffers()
295 #define dma_cache_wback(_start,_size)           flush_write_buffers()
296 #define dma_cache_wback_inv(_start,_size)       flush_write_buffers()
297
298 #else
299
300 /* Nothing to do */
301
302 #define dma_cache_inv(_start,_size)             do { } while (0)
303 #define dma_cache_wback(_start,_size)           do { } while (0)
304 #define dma_cache_wback_inv(_start,_size)       do { } while (0)
305 #define flush_write_buffers()
306
307 #endif
308
309 #endif /* __KERNEL__ */
310
311 #ifdef SLOW_IO_BY_JUMPING
312 #define __SLOW_DOWN_IO "jmp 1f; 1: jmp 1f; 1:"
313 #else
314 #define __SLOW_DOWN_IO "outb %%al,$0x80;"
315 #endif
316
317 static inline void slow_down_io(void) {
318         __asm__ __volatile__(
319                 __SLOW_DOWN_IO
320 #ifdef REALLY_SLOW_IO
321                 __SLOW_DOWN_IO __SLOW_DOWN_IO __SLOW_DOWN_IO
322 #endif
323                 : : );
324 }
325
326 #ifdef CONFIG_X86_NUMAQ
327 extern void *xquad_portio;    /* Where the IO area was mapped */
328 #define XQUAD_PORT_ADDR(port, quad) (xquad_portio + (XQUAD_PORTIO_QUAD*quad) + port)
329 #define __BUILDIO(bwl,bw,type) \
330 static inline void out##bwl##_quad(unsigned type value, int port, int quad) { \
331         if (xquad_portio) \
332                 write##bwl(value, XQUAD_PORT_ADDR(port, quad)); \
333         else \
334                 out##bwl##_local(value, port); \
335 } \
336 static inline void out##bwl(unsigned type value, int port) { \
337         out##bwl##_quad(value, port, 0); \
338 } \
339 static inline unsigned type in##bwl##_quad(int port, int quad) { \
340         if (xquad_portio) \
341                 return read##bwl(XQUAD_PORT_ADDR(port, quad)); \
342         else \
343                 return in##bwl##_local(port); \
344 } \
345 static inline unsigned type in##bwl(int port) { \
346         return in##bwl##_quad(port, 0); \
347 }
348 #else
349 #define __BUILDIO(bwl,bw,type) \
350 static inline void out##bwl(unsigned type value, int port) { \
351         out##bwl##_local(value, port); \
352 } \
353 static inline unsigned type in##bwl(int port) { \
354         return in##bwl##_local(port); \
355 }
356 #endif
357
358
359 #define BUILDIO(bwl,bw,type) \
360 static inline void out##bwl##_local(unsigned type value, int port) { \
361         __asm__ __volatile__("out" #bwl " %" #bw "0, %w1" : : "a"(value), "Nd"(port)); \
362 } \
363 static inline unsigned type in##bwl##_local(int port) { \
364         unsigned type value; \
365         __asm__ __volatile__("in" #bwl " %w1, %" #bw "0" : "=a"(value) : "Nd"(port)); \
366         return value; \
367 } \
368 static inline void out##bwl##_local_p(unsigned type value, int port) { \
369         out##bwl##_local(value, port); \
370         slow_down_io(); \
371 } \
372 static inline unsigned type in##bwl##_local_p(int port) { \
373         unsigned type value = in##bwl##_local(port); \
374         slow_down_io(); \
375         return value; \
376 } \
377 __BUILDIO(bwl,bw,type) \
378 static inline void out##bwl##_p(unsigned type value, int port) { \
379         out##bwl(value, port); \
380         slow_down_io(); \
381 } \
382 static inline unsigned type in##bwl##_p(int port) { \
383         unsigned type value = in##bwl(port); \
384         slow_down_io(); \
385         return value; \
386 } \
387 static inline void outs##bwl(int port, const void *addr, unsigned long count) { \
388         __asm__ __volatile__("rep; outs" #bwl : "+S"(addr), "+c"(count) : "d"(port)); \
389 } \
390 static inline void ins##bwl(int port, void *addr, unsigned long count) { \
391         __asm__ __volatile__("rep; ins" #bwl : "+D"(addr), "+c"(count) : "d"(port)); \
392 }
393
394 BUILDIO(b,b,char)
395 BUILDIO(w,w,short)
396 BUILDIO(l,,int)
397
398 #endif