patch-2_6_7-vs1_9_1_12
[linux-2.6.git] / include / asm-ppc64 / eeh.h
1 /* 
2  * eeh.h
3  * Copyright (C) 2001  Dave Engebretsen & Todd Inglett IBM Corporation.
4  *
5  * This program is free software; you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License as published by
7  * the Free Software Foundation; either version 2 of the License, or
8  * (at your option) any later version.
9  * 
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13  * GNU General Public License for more details.
14  * 
15  * You should have received a copy of the GNU General Public License
16  * along with this program; if not, write to the Free Software
17  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
18  */
19
20 #ifndef _PPC64_EEH_H
21 #define _PPC64_EEH_H
22
23 #include <linux/string.h>
24 #include <linux/init.h>
25
26 struct pci_dev;
27
28 /* I/O addresses are converted to EEH "tokens" such that a driver will cause
29  * a bad page fault if the address is used directly (i.e. these addresses are
30  * never actually mapped.  Translation between IO <-> EEH region is 1 to 1.
31  */
32 #define IO_TOKEN_TO_ADDR(token) \
33         (((unsigned long)(token) & ~(0xfUL << REGION_SHIFT)) | \
34         (IO_REGION_ID << REGION_SHIFT))
35
36 #define IO_ADDR_TO_TOKEN(addr) \
37         (((unsigned long)(addr) & ~(0xfUL << REGION_SHIFT)) | \
38         (EEH_REGION_ID << REGION_SHIFT))
39
40 /* Values for eeh_mode bits in device_node */
41 #define EEH_MODE_SUPPORTED      (1<<0)
42 #define EEH_MODE_NOCHECK        (1<<1)
43
44 extern void __init eeh_init(void);
45 unsigned long eeh_check_failure(void *token, unsigned long val);
46 void *eeh_ioremap(unsigned long addr, void *vaddr);
47 void __init pci_addr_cache_build(void);
48
49 /**
50  * eeh_add_device_early
51  * eeh_add_device_late
52  *
53  * Perform eeh initialization for devices added after boot.
54  * Call eeh_add_device_early before doing any i/o to the
55  * device (including config space i/o).  Call eeh_add_device_late
56  * to finish the eeh setup for this device.
57  */
58 void eeh_add_device_early(struct device_node *);
59 void eeh_add_device_late(struct pci_dev *);
60
61 /**
62  * eeh_remove_device - undo EEH setup for the indicated pci device
63  * @dev: pci device to be removed
64  *
65  * This routine should be when a device is removed from a running
66  * system (e.g. by hotplug or dlpar).
67  */
68 void eeh_remove_device(struct pci_dev *);
69
70 #define EEH_DISABLE             0
71 #define EEH_ENABLE              1
72 #define EEH_RELEASE_LOADSTORE   2
73 #define EEH_RELEASE_DMA         3
74 int eeh_set_option(struct pci_dev *dev, int options);
75
76 /*
77  * EEH_POSSIBLE_ERROR() -- test for possible MMIO failure.
78  *
79  * Order this macro for performance.
80  * If EEH is off for a device and it is a memory BAR, ioremap will
81  * map it to the IOREGION.  In this case addr == vaddr and since these
82  * should be in registers we compare them first.  Next we check for
83  * ff's which indicates a (very) possible failure.
84  *
85  * If this macro yields TRUE, the caller relays to eeh_check_failure()
86  * which does further tests out of line.
87  */
88 #define EEH_POSSIBLE_IO_ERROR(val, type)        ((val) == (type)~0)
89
90 /* The vaddr will equal the addr if EEH checking is disabled for
91  * this device.  This is because eeh_ioremap() will not have
92  * remapped to 0xA0, and thus both vaddr and addr will be 0xE0...
93  */
94 #define EEH_POSSIBLE_ERROR(addr, vaddr, val, type) \
95                 ((vaddr) != (addr) && EEH_POSSIBLE_IO_ERROR(val, type))
96
97 /* 
98  * MMIO read/write operations with EEH support.
99  */
100 static inline u8 eeh_readb(void *addr) {
101         volatile u8 *vaddr = (volatile u8 *)IO_TOKEN_TO_ADDR(addr);
102         u8 val = in_8(vaddr);
103         if (EEH_POSSIBLE_ERROR(addr, vaddr, val, u8))
104                 return eeh_check_failure(addr, val);
105         return val;
106 }
107 static inline void eeh_writeb(u8 val, void *addr) {
108         volatile u8 *vaddr = (volatile u8 *)IO_TOKEN_TO_ADDR(addr);
109         out_8(vaddr, val);
110 }
111
112 static inline u16 eeh_readw(void *addr) {
113         volatile u16 *vaddr = (volatile u16 *)IO_TOKEN_TO_ADDR(addr);
114         u16 val = in_le16(vaddr);
115         if (EEH_POSSIBLE_ERROR(addr, vaddr, val, u16))
116                 return eeh_check_failure(addr, val);
117         return val;
118 }
119 static inline void eeh_writew(u16 val, void *addr) {
120         volatile u16 *vaddr = (volatile u16 *)IO_TOKEN_TO_ADDR(addr);
121         out_le16(vaddr, val);
122 }
123 static inline u16 eeh_raw_readw(void *addr) {
124         volatile u16 *vaddr = (volatile u16 *)IO_TOKEN_TO_ADDR(addr);
125         u16 val = in_be16(vaddr);
126         if (EEH_POSSIBLE_ERROR(addr, vaddr, val, u16))
127                 return eeh_check_failure(addr, val);
128         return val;
129 }
130 static inline void eeh_raw_writew(u16 val, void *addr) {
131         volatile u16 *vaddr = (volatile u16 *)IO_TOKEN_TO_ADDR(addr);
132         out_be16(vaddr, val);
133 }
134
135 static inline u32 eeh_readl(void *addr) {
136         volatile u32 *vaddr = (volatile u32 *)IO_TOKEN_TO_ADDR(addr);
137         u32 val = in_le32(vaddr);
138         if (EEH_POSSIBLE_ERROR(addr, vaddr, val, u32))
139                 return eeh_check_failure(addr, val);
140         return val;
141 }
142 static inline void eeh_writel(u32 val, void *addr) {
143         volatile u32 *vaddr = (volatile u32 *)IO_TOKEN_TO_ADDR(addr);
144         out_le32(vaddr, val);
145 }
146 static inline u32 eeh_raw_readl(void *addr) {
147         volatile u32 *vaddr = (volatile u32 *)IO_TOKEN_TO_ADDR(addr);
148         u32 val = in_be32(vaddr);
149         if (EEH_POSSIBLE_ERROR(addr, vaddr, val, u32))
150                 return eeh_check_failure(addr, val);
151         return val;
152 }
153 static inline void eeh_raw_writel(u32 val, void *addr) {
154         volatile u32 *vaddr = (volatile u32 *)IO_TOKEN_TO_ADDR(addr);
155         out_be32(vaddr, val);
156 }
157
158 static inline u64 eeh_readq(void *addr) {
159         volatile u64 *vaddr = (volatile u64 *)IO_TOKEN_TO_ADDR(addr);
160         u64 val = in_le64(vaddr);
161         if (EEH_POSSIBLE_ERROR(addr, vaddr, val, u64))
162                 return eeh_check_failure(addr, val);
163         return val;
164 }
165 static inline void eeh_writeq(u64 val, void *addr) {
166         volatile u64 *vaddr = (volatile u64 *)IO_TOKEN_TO_ADDR(addr);
167         out_le64(vaddr, val);
168 }
169 static inline u64 eeh_raw_readq(void *addr) {
170         volatile u64 *vaddr = (volatile u64 *)IO_TOKEN_TO_ADDR(addr);
171         u64 val = in_be64(vaddr);
172         if (EEH_POSSIBLE_ERROR(addr, vaddr, val, u64))
173                 return eeh_check_failure(addr, val);
174         return val;
175 }
176 static inline void eeh_raw_writeq(u64 val, void *addr) {
177         volatile u64 *vaddr = (volatile u64 *)IO_TOKEN_TO_ADDR(addr);
178         out_be64(vaddr, val);
179 }
180
181 static inline void eeh_memset_io(void *addr, int c, unsigned long n) {
182         void *vaddr = (void *)IO_TOKEN_TO_ADDR(addr);
183         memset(vaddr, c, n);
184 }
185 static inline void eeh_memcpy_fromio(void *dest, void *src, unsigned long n) {
186         void *vsrc = (void *)IO_TOKEN_TO_ADDR(src);
187         memcpy(dest, vsrc, n);
188         /* Look for ffff's here at dest[n].  Assume that at least 4 bytes
189          * were copied. Check all four bytes.
190          */
191         if ((n >= 4) &&
192                 (EEH_POSSIBLE_ERROR(src, vsrc, (*((u32 *) dest+n-4)), u32))) {
193                 eeh_check_failure(src, (*((u32 *) dest+n-4)));
194         }
195 }
196
197 static inline void eeh_memcpy_toio(void *dest, void *src, unsigned long n) {
198         void *vdest = (void *)IO_TOKEN_TO_ADDR(dest);
199         memcpy(vdest, src, n);
200 }
201
202 #define MAX_ISA_PORT 0x10000
203 extern unsigned long io_page_mask;
204 #define _IO_IS_VALID(port) ((port) >= MAX_ISA_PORT || (1 << (port>>PAGE_SHIFT)) & io_page_mask)
205
206 static inline u8 eeh_inb(unsigned long port) {
207         u8 val;
208         if (!_IO_IS_VALID(port))
209                 return ~0;
210         val = in_8((u8 *)(port+pci_io_base));
211         if (EEH_POSSIBLE_IO_ERROR(val, u8))
212                 return eeh_check_failure((void*)(port), val);
213         return val;
214 }
215
216 static inline void eeh_outb(u8 val, unsigned long port) {
217         if (_IO_IS_VALID(port))
218                 out_8((u8 *)(port+pci_io_base), val);
219 }
220
221 static inline u16 eeh_inw(unsigned long port) {
222         u16 val;
223         if (!_IO_IS_VALID(port))
224                 return ~0;
225         val = in_le16((u16 *)(port+pci_io_base));
226         if (EEH_POSSIBLE_IO_ERROR(val, u16))
227                 return eeh_check_failure((void*)(port), val);
228         return val;
229 }
230
231 static inline void eeh_outw(u16 val, unsigned long port) {
232         if (_IO_IS_VALID(port))
233                 out_le16((u16 *)(port+pci_io_base), val);
234 }
235
236 static inline u32 eeh_inl(unsigned long port) {
237         u32 val;
238         if (!_IO_IS_VALID(port))
239                 return ~0;
240         val = in_le32((u32 *)(port+pci_io_base));
241         if (EEH_POSSIBLE_IO_ERROR(val, u32))
242                 return eeh_check_failure((void*)(port), val);
243         return val;
244 }
245
246 static inline void eeh_outl(u32 val, unsigned long port) {
247         if (_IO_IS_VALID(port))
248                 out_le32((u32 *)(port+pci_io_base), val);
249 }
250
251 /* in-string eeh macros */
252 static inline void eeh_insb(unsigned long port, void * buf, int ns) {
253         _insb((u8 *)(port+pci_io_base), buf, ns);
254         if (EEH_POSSIBLE_IO_ERROR((*(((u8*)buf)+ns-1)), u8))
255                 eeh_check_failure((void*)(port), *(u8*)buf);
256 }
257
258 static inline void eeh_insw_ns(unsigned long port, void * buf, int ns) {
259         _insw_ns((u16 *)(port+pci_io_base), buf, ns);
260         if (EEH_POSSIBLE_IO_ERROR((*(((u16*)buf)+ns-1)), u16))
261                 eeh_check_failure((void*)(port), *(u16*)buf);
262 }
263
264 static inline void eeh_insl_ns(unsigned long port, void * buf, int nl) {
265         _insl_ns((u32 *)(port+pci_io_base), buf, nl);
266         if (EEH_POSSIBLE_IO_ERROR((*(((u32*)buf)+nl-1)), u32))
267                 eeh_check_failure((void*)(port), *(u32*)buf);
268 }
269
270 #endif /* _PPC64_EEH_H */