This commit was manufactured by cvs2svn to create branch 'vserver'.
[linux-2.6.git] / lib / iomap.c
1 /*
2  * Implement the default iomap interfaces
3  */
4 #include <linux/pci.h>
5 #include <linux/module.h>
6 #include <asm/io.h>
7
8 /*
9  * Read/write from/to an (offsettable) iomem cookie. It might be a PIO
10  * access or a MMIO access, these functions don't care. The info is
11  * encoded in the hardware mapping set up by the mapping functions
12  * (or the cookie itself, depending on implementation and hw).
13  *
14  * The generic routines don't assume any hardware mappings, and just
15  * encode the PIO/MMIO as part of the cookie. They coldly assume that
16  * the MMIO IO mappings are not in the low address range.
17  *
18  * Architectures for which this is not true can't use this generic
19  * implementation and should do their own copy.
20  *
21  * We encode the physical PIO addresses (0-0xffff) into the
22  * pointer by offsetting them with a constant (0x10000) and
23  * assuming that all the low addresses are always PIO. That means
24  * we can do some sanity checks on the low bits, and don't
25  * need to just take things for granted.
26  */
27 #define PIO_OFFSET      0x10000UL
28 #define PIO_MASK        0x0ffffUL
29 #define PIO_RESERVED    0x40000UL
30
31 /*
32  * Ugly macros are a way of life.
33  */
34 #define VERIFY_PIO(port) BUG_ON((port & ~PIO_MASK) != PIO_OFFSET)
35
36 #define IO_COND(addr, is_pio, is_mmio) do {                     \
37         unsigned long port = (unsigned long __force)addr;       \
38         if (port < PIO_RESERVED) {                              \
39                 VERIFY_PIO(port);                               \
40                 port &= PIO_MASK;                               \
41                 is_pio;                                         \
42         } else {                                                \
43                 is_mmio;                                        \
44         }                                                       \
45 } while (0)
46
47 unsigned int fastcall ioread8(void __iomem *addr)
48 {
49         IO_COND(addr, return inb(port), return readb(addr));
50 }
51 unsigned int fastcall ioread16(void __iomem *addr)
52 {
53         IO_COND(addr, return inw(port), return readw(addr));
54 }
55 unsigned int fastcall ioread32(void __iomem *addr)
56 {
57         IO_COND(addr, return inl(port), return readl(addr));
58 }
59 EXPORT_SYMBOL(ioread8);
60 EXPORT_SYMBOL(ioread16);
61 EXPORT_SYMBOL(ioread32);
62
63 void fastcall iowrite8(u8 val, void __iomem *addr)
64 {
65         IO_COND(addr, outb(val,port), writeb(val, addr));
66 }
67 void fastcall iowrite16(u16 val, void __iomem *addr)
68 {
69         IO_COND(addr, outw(val,port), writew(val, addr));
70 }
71 void fastcall iowrite32(u32 val, void __iomem *addr)
72 {
73         IO_COND(addr, outl(val,port), writel(val, addr));
74 }
75 EXPORT_SYMBOL(iowrite8);
76 EXPORT_SYMBOL(iowrite16);
77 EXPORT_SYMBOL(iowrite32);
78
79 /*
80  * These are the "repeat MMIO read/write" functions.
81  * Note the "__raw" accesses, since we don't want to
82  * convert to CPU byte order. We write in "IO byte
83  * order" (we also don't have IO barriers).
84  */
85 static inline void mmio_insb(void __iomem *addr, u8 *dst, int count)
86 {
87         while (--count >= 0) {
88                 u8 data = __raw_readb(addr);
89                 *dst = data;
90                 dst++;
91         }
92 }
93 static inline void mmio_insw(void __iomem *addr, u16 *dst, int count)
94 {
95         while (--count >= 0) {
96                 u16 data = __raw_readw(addr);
97                 *dst = data;
98                 dst++;
99         }
100 }
101 static inline void mmio_insl(void __iomem *addr, u32 *dst, int count)
102 {
103         while (--count >= 0) {
104                 u32 data = __raw_readl(addr);
105                 *dst = data;
106                 dst++;
107         }
108 }
109
110 static inline void mmio_outsb(void __iomem *addr, const u8 *src, int count)
111 {
112         while (--count >= 0) {
113                 __raw_writeb(*src, addr);
114                 src++;
115         }
116 }
117 static inline void mmio_outsw(void __iomem *addr, const u16 *src, int count)
118 {
119         while (--count >= 0) {
120                 __raw_writew(*src, addr);
121                 src++;
122         }
123 }
124 static inline void mmio_outsl(void __iomem *addr, const u32 *src, int count)
125 {
126         while (--count >= 0) {
127                 __raw_writel(*src, addr);
128                 src++;
129         }
130 }
131
132 void fastcall ioread8_rep(void __iomem *addr, void *dst, unsigned long count)
133 {
134         IO_COND(addr, insb(port,dst,count), mmio_insb(addr, dst, count));
135 }
136 void fastcall ioread16_rep(void __iomem *addr, void *dst, unsigned long count)
137 {
138         IO_COND(addr, insw(port,dst,count), mmio_insw(addr, dst, count));
139 }
140 void fastcall ioread32_rep(void __iomem *addr, void *dst, unsigned long count)
141 {
142         IO_COND(addr, insl(port,dst,count), mmio_insl(addr, dst, count));
143 }
144 EXPORT_SYMBOL(ioread8_rep);
145 EXPORT_SYMBOL(ioread16_rep);
146 EXPORT_SYMBOL(ioread32_rep);
147
148 void fastcall iowrite8_rep(void __iomem *addr, const void *src, unsigned long count)
149 {
150         IO_COND(addr, outsb(port, src, count), mmio_outsb(addr, src, count));
151 }
152 void fastcall iowrite16_rep(void __iomem *addr, const void *src, unsigned long count)
153 {
154         IO_COND(addr, outsw(port, src, count), mmio_outsw(addr, src, count));
155 }
156 void fastcall iowrite32_rep(void __iomem *addr, const void *src, unsigned long count)
157 {
158         IO_COND(addr, outsl(port, src,count), mmio_outsl(addr, src, count));
159 }
160 EXPORT_SYMBOL(iowrite8_rep);
161 EXPORT_SYMBOL(iowrite16_rep);
162 EXPORT_SYMBOL(iowrite32_rep);
163
164 /* Create a virtual mapping cookie for an IO port range */
165 void __iomem *ioport_map(unsigned long port, unsigned int nr)
166 {
167         if (port > PIO_MASK)
168                 return NULL;
169         return (void __iomem *) (unsigned long) (port + PIO_OFFSET);
170 }
171
172 void ioport_unmap(void __iomem *addr)
173 {
174         /* Nothing to do */
175 }
176 EXPORT_SYMBOL(ioport_map);
177 EXPORT_SYMBOL(ioport_unmap);
178
179 /* Create a virtual mapping cookie for a PCI BAR (memory or IO) */
180 void __iomem *pci_iomap(struct pci_dev *dev, int bar, unsigned long maxlen)
181 {
182         unsigned long start = pci_resource_start(dev, bar);
183         unsigned long len = pci_resource_len(dev, bar);
184         unsigned long flags = pci_resource_flags(dev, bar);
185
186         if (!len || !start)
187                 return NULL;
188         if (maxlen && len > maxlen)
189                 len = maxlen;
190         if (flags & IORESOURCE_IO)
191                 return ioport_map(start, len);
192         if (flags & IORESOURCE_MEM) {
193                 if (flags & IORESOURCE_CACHEABLE)
194                         return ioremap(start, len);
195                 return ioremap_nocache(start, len);
196         }
197         /* What? */
198         return NULL;
199 }
200
201 void pci_iounmap(struct pci_dev *dev, void __iomem * addr)
202 {
203         IO_COND(addr, /* nothing */, iounmap(addr));
204 }
205 EXPORT_SYMBOL(pci_iomap);
206 EXPORT_SYMBOL(pci_iounmap);