4 #include <linux/config.h>
5 #include <linux/string.h>
6 #include <linux/compiler.h>
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/..).
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
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.
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)
33 * Bit simplified and optimized by Jan Hubicka
34 * Support of BIGMEM added by Gerhard Wichert, Siemens AG, July 1999.
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>
41 #define IO_SPACE_LIMIT 0xffff
43 #define XQUAD_PORTIO_BASE 0xfe400000
44 #define XQUAD_PORTIO_QUAD 0x40000 /* 256k per quad. */
48 #include <asm-generic/iomap.h>
50 #include <linux/vmalloc.h>
51 #include <asm/fixmap.h>
54 * Convert a physical pointer to a virtual kernel pointer for /dev/mem
57 #define xlate_dev_mem_ptr(p) __va(p)
60 * Convert a virtual cached pointer to an uncached pointer
62 #define xlate_dev_kmem_ptr(p) p
65 * virt_to_phys - map virtual addresses to physical
66 * @address: address to remap
68 * The returned physical address is the physical (CPU) mapping for
69 * the memory address given. It is only valid to use this function on
70 * addresses directly mapped or allocated via kmalloc.
72 * This function does not give bus mappings for DMA transfers. In
73 * almost all conceivable cases a device driver should not be using
77 static inline unsigned long virt_to_phys(volatile void * address)
83 * phys_to_virt - map physical address to virtual
84 * @address: address to remap
86 * The returned virtual address is a current CPU mapping for
87 * the memory address given. It is only valid to use this function on
88 * addresses that have a kernel mapping
90 * This function does not handle bus mappings for DMA transfers. In
91 * almost all conceivable cases a device driver should not be using
95 static inline void * phys_to_virt(unsigned long address)
101 * Change "struct page" to physical address.
103 #define page_to_pseudophys(page) ((dma_addr_t)page_to_pfn(page) << PAGE_SHIFT)
104 #define page_to_phys(page) (phys_to_machine(page_to_pseudophys(page)))
105 #define page_to_bus(page) (phys_to_machine(page_to_pseudophys(page)))
107 #define bio_to_pseudophys(bio) (page_to_pseudophys(bio_page((bio))) + \
108 (unsigned long) bio_offset((bio)))
109 #define bvec_to_pseudophys(bv) (page_to_pseudophys((bv)->bv_page) + \
110 (unsigned long) (bv)->bv_offset)
112 #define BIOVEC_PHYS_MERGEABLE(vec1, vec2) \
113 (((bvec_to_phys((vec1)) + (vec1)->bv_len) == bvec_to_phys((vec2))) && \
114 ((bvec_to_pseudophys((vec1)) + (vec1)->bv_len) == \
115 bvec_to_pseudophys((vec2))))
117 extern void __iomem * __ioremap(unsigned long offset, unsigned long size, unsigned long flags);
120 * ioremap - map bus memory into CPU space
121 * @offset: bus address of the memory
122 * @size: size of the resource to map
124 * ioremap performs a platform specific sequence of operations to
125 * make bus memory CPU accessible via the readb/readw/readl/writeb/
126 * writew/writel functions and the other mmio helpers. The returned
127 * address is not guaranteed to be usable directly as a virtual
131 static inline void __iomem * ioremap(unsigned long offset, unsigned long size)
133 return __ioremap(offset, size, 0);
136 extern void __iomem * ioremap_nocache(unsigned long offset, unsigned long size);
137 extern void iounmap(volatile void __iomem *addr);
140 * bt_ioremap() and bt_iounmap() are for temporary early boot-time
141 * mappings, before the real ioremap() is functional.
142 * A boot-time mapping is currently limited to at most 16 pages.
144 extern void *bt_ioremap(unsigned long offset, unsigned long size);
145 extern void bt_iounmap(void *addr, unsigned long size);
147 /* Use early IO mappings for DMI because it's initialized early */
148 #define dmi_ioremap bt_ioremap
149 #define dmi_iounmap bt_iounmap
150 #define dmi_alloc alloc_bootmem
153 * ISA I/O bus memory addresses are 1:1 with the physical address.
155 #define isa_virt_to_bus(_x) isa_virt_to_bus_is_UNSUPPORTED->x
156 #define isa_page_to_bus(_x) isa_page_to_bus_is_UNSUPPORTED->x
157 #define isa_bus_to_virt(_x) (void *)(__fix_to_virt(FIX_ISAMAP_BEGIN) + (_x))
160 * However PCI ones are not necessarily 1:1 and therefore these interfaces
161 * are forbidden in portable PCI drivers.
163 * Allow them on x86 for legacy drivers, though.
165 #define virt_to_bus(_x) phys_to_machine(__pa(_x))
166 #define bus_to_virt(_x) __va(machine_to_phys(_x))
169 * readX/writeX() are used to access memory mapped devices. On some
170 * architectures the memory mapped IO stuff needs to be accessed
171 * differently. On the x86 architecture, we just read/write the
172 * memory location directly.
175 static inline unsigned char readb(const volatile void __iomem *addr)
177 return *(volatile unsigned char __force *) addr;
179 static inline unsigned short readw(const volatile void __iomem *addr)
181 return *(volatile unsigned short __force *) addr;
183 static inline unsigned int readl(const volatile void __iomem *addr)
185 return *(volatile unsigned int __force *) addr;
187 #define readb_relaxed(addr) readb(addr)
188 #define readw_relaxed(addr) readw(addr)
189 #define readl_relaxed(addr) readl(addr)
190 #define __raw_readb readb
191 #define __raw_readw readw
192 #define __raw_readl readl
194 static inline void writeb(unsigned char b, volatile void __iomem *addr)
196 *(volatile unsigned char __force *) addr = b;
198 static inline void writew(unsigned short b, volatile void __iomem *addr)
200 *(volatile unsigned short __force *) addr = b;
202 static inline void writel(unsigned int b, volatile void __iomem *addr)
204 *(volatile unsigned int __force *) addr = b;
206 #define __raw_writeb writeb
207 #define __raw_writew writew
208 #define __raw_writel writel
212 static inline void memset_io(volatile void __iomem *addr, unsigned char val, int count)
214 memset((void __force *) addr, val, count);
216 static inline void memcpy_fromio(void *dst, const volatile void __iomem *src, int count)
218 __memcpy(dst, (void __force *) src, count);
220 static inline void memcpy_toio(volatile void __iomem *dst, const void *src, int count)
222 __memcpy((void __force *) dst, src, count);
226 * ISA space is 'always mapped' on a typical x86 system, no need to
227 * explicitly ioremap() it. The fact that the ISA IO space is mapped
228 * to PAGE_OFFSET is pure coincidence - it does not mean ISA values
229 * are physical addresses. The following constant pointer can be
230 * used as the IO-area pointer (it can be iounmapped as well, so the
231 * analogy with PCI is quite large):
233 #define __ISA_IO_base ((char __iomem *)(fix_to_virt(FIX_ISAMAP_BEGIN)))
236 * Again, i386 does not require mem IO specific function.
239 #define eth_io_copy_and_sum(a,b,c,d) eth_copy_and_sum((a),(void __force *)(b),(c),(d))
242 * check_signature - find BIOS signatures
243 * @io_addr: mmio address to check
244 * @signature: signature block
245 * @length: length of signature
247 * Perform a signature comparison with the mmio address io_addr. This
248 * address should have been obtained by ioremap.
249 * Returns 1 on a match.
252 static inline int check_signature(volatile void __iomem * io_addr,
253 const unsigned char *signature, int length)
257 if (readb(io_addr) != *signature)
271 * This needed for two cases
272 * 1. Out of order aware processors
273 * 2. Accidentally out of order processors (PPro errata #51)
276 #if defined(CONFIG_X86_OOSTORE) || defined(CONFIG_X86_PPRO_FENCE)
278 static inline void flush_write_buffers(void)
280 __asm__ __volatile__ ("lock; addl $0,0(%%esp)": : :"memory");
283 #define dma_cache_inv(_start,_size) flush_write_buffers()
284 #define dma_cache_wback(_start,_size) flush_write_buffers()
285 #define dma_cache_wback_inv(_start,_size) flush_write_buffers()
291 #define dma_cache_inv(_start,_size) do { } while (0)
292 #define dma_cache_wback(_start,_size) do { } while (0)
293 #define dma_cache_wback_inv(_start,_size) do { } while (0)
294 #define flush_write_buffers()
298 #endif /* __KERNEL__ */
300 #ifdef SLOW_IO_BY_JUMPING
301 #define __SLOW_DOWN_IO "jmp 1f; 1: jmp 1f; 1:"
303 #define __SLOW_DOWN_IO "outb %%al,$0x80;"
306 static inline void slow_down_io(void) {
307 __asm__ __volatile__(
309 #ifdef REALLY_SLOW_IO
310 __SLOW_DOWN_IO __SLOW_DOWN_IO __SLOW_DOWN_IO
315 #ifdef CONFIG_X86_NUMAQ
316 extern void *xquad_portio; /* Where the IO area was mapped */
317 #define XQUAD_PORT_ADDR(port, quad) (xquad_portio + (XQUAD_PORTIO_QUAD*quad) + port)
318 #define __BUILDIO(bwl,bw,type) \
319 static inline void out##bwl##_quad(unsigned type value, int port, int quad) { \
321 write##bwl(value, XQUAD_PORT_ADDR(port, quad)); \
323 out##bwl##_local(value, port); \
325 static inline void out##bwl(unsigned type value, int port) { \
326 out##bwl##_quad(value, port, 0); \
328 static inline unsigned type in##bwl##_quad(int port, int quad) { \
330 return read##bwl(XQUAD_PORT_ADDR(port, quad)); \
332 return in##bwl##_local(port); \
334 static inline unsigned type in##bwl(int port) { \
335 return in##bwl##_quad(port, 0); \
338 #define __BUILDIO(bwl,bw,type) \
339 static inline void out##bwl(unsigned type value, int port) { \
340 out##bwl##_local(value, port); \
342 static inline unsigned type in##bwl(int port) { \
343 return in##bwl##_local(port); \
348 #define BUILDIO(bwl,bw,type) \
349 static inline void out##bwl##_local(unsigned type value, int port) { \
350 __asm__ __volatile__("out" #bwl " %" #bw "0, %w1" : : "a"(value), "Nd"(port)); \
352 static inline unsigned type in##bwl##_local(int port) { \
353 unsigned type value; \
354 __asm__ __volatile__("in" #bwl " %w1, %" #bw "0" : "=a"(value) : "Nd"(port)); \
357 static inline void out##bwl##_local_p(unsigned type value, int port) { \
358 out##bwl##_local(value, port); \
361 static inline unsigned type in##bwl##_local_p(int port) { \
362 unsigned type value = in##bwl##_local(port); \
366 __BUILDIO(bwl,bw,type) \
367 static inline void out##bwl##_p(unsigned type value, int port) { \
368 out##bwl(value, port); \
371 static inline unsigned type in##bwl##_p(int port) { \
372 unsigned type value = in##bwl(port); \
376 static inline void outs##bwl(int port, const void *addr, unsigned long count) { \
377 __asm__ __volatile__("rep; outs" #bwl : "+S"(addr), "+c"(count) : "d"(port)); \
379 static inline void ins##bwl(int port, void *addr, unsigned long count) { \
380 __asm__ __volatile__("rep; ins" #bwl : "+D"(addr), "+c"(count) : "d"(port)); \
387 /* We will be supplying our own /dev/mem implementation */
388 #define ARCH_HAS_DEV_MEM