3 * Copyright (C) 2001 Dave Engebretsen & Todd Inglett IBM Corporation.
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.
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.
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
23 #include <linux/string.h>
24 #include <linux/init.h>
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.
32 #define IO_TOKEN_TO_ADDR(token) \
33 (((unsigned long)(token) & ~(0xfUL << REGION_SHIFT)) | \
34 (IO_REGION_ID << REGION_SHIFT))
36 #define IO_ADDR_TO_TOKEN(addr) \
37 (((unsigned long)(addr) & ~(0xfUL << REGION_SHIFT)) | \
38 (EEH_REGION_ID << REGION_SHIFT))
40 /* Values for eeh_mode bits in device_node */
41 #define EEH_MODE_SUPPORTED (1<<0)
42 #define EEH_MODE_NOCHECK (1<<1)
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);
50 * eeh_add_device - perform EEH initialization for the indicated pci device
51 * @dev: pci device for which to set up EEH
53 * This routine can be used to perform EEH initialization for PCI
54 * devices that were added after system boot (e.g. hotplug, dlpar).
55 * Whether this actually enables EEH or not for this device depends
56 * on the type of the device, on earlier boot command-line
59 void eeh_add_device(struct pci_dev *);
62 * eeh_remove_device - undo EEH setup for the indicated pci device
63 * @dev: pci device to be removed
65 * This routine should be when a device is removed from a running
66 * system (e.g. by hotplug or dlpar).
68 void eeh_remove_device(struct pci_dev *);
72 #define EEH_RELEASE_LOADSTORE 2
73 #define EEH_RELEASE_DMA 3
74 int eeh_set_option(struct pci_dev *dev, int options);
77 * EEH_POSSIBLE_ERROR() -- test for possible MMIO failure.
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.
85 * If this macro yields TRUE, the caller relays to eeh_check_failure()
86 * which does further tests out of line.
88 #define EEH_POSSIBLE_IO_ERROR(val, type) ((val) == (type)~0)
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...
94 #define EEH_POSSIBLE_ERROR(addr, vaddr, val, type) \
95 ((vaddr) != (addr) && EEH_POSSIBLE_IO_ERROR(val, type))
98 * MMIO read/write operations with EEH support.
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);
107 static inline void eeh_writeb(u8 val, void *addr) {
108 volatile u8 *vaddr = (volatile u8 *)IO_TOKEN_TO_ADDR(addr);
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);
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);
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);
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);
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);
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);
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);
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);
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);
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);
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);
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);
181 static inline void eeh_memset_io(void *addr, int c, unsigned long n) {
182 void *vaddr = (void *)IO_TOKEN_TO_ADDR(addr);
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.
192 (EEH_POSSIBLE_ERROR(src, vsrc, (*((u32 *) dest+n-4)), u32))) {
193 eeh_check_failure(src, (*((u32 *) dest+n-4)));
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);
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)
206 static inline u8 eeh_inb(unsigned long port) {
208 if (!_IO_IS_VALID(port))
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);
216 static inline void eeh_outb(u8 val, unsigned long port) {
217 if (_IO_IS_VALID(port))
218 return out_8((u8 *)(port+pci_io_base), val);
221 static inline u16 eeh_inw(unsigned long port) {
223 if (!_IO_IS_VALID(port))
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);
231 static inline void eeh_outw(u16 val, unsigned long port) {
232 if (_IO_IS_VALID(port))
233 return out_le16((u16 *)(port+pci_io_base), val);
236 static inline u32 eeh_inl(unsigned long port) {
238 if (!_IO_IS_VALID(port))
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);
246 static inline void eeh_outl(u32 val, unsigned long port) {
247 if (_IO_IS_VALID(port))
248 return out_le32((u32 *)(port+pci_io_base), val);
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);
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);
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);
270 #endif /* _PPC64_EEH_H */