blob: e4ca35ad3338aef976269d7bbd97d871fdc18281 [file] [log] [blame]
Linas Vepstas172ca922005-11-03 18:50:04 -06001/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07002 * 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.
Linas Vepstas172ca922005-11-03 18:50:04 -06009 *
Linus Torvalds1da177e2005-04-16 15:20:36 -070010 * 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.
Linas Vepstas172ca922005-11-03 18:50:04 -060014 *
Linus Torvalds1da177e2005-04-16 15:20:36 -070015 * 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/config.h>
24#include <linux/init.h>
25#include <linux/list.h>
26#include <linux/string.h>
27
28struct pci_dev;
29struct device_node;
Linus Torvalds1da177e2005-04-16 15:20:36 -070030
31#ifdef CONFIG_EEH
32
David Woodhouse1e28a7d2005-11-17 00:44:03 +000033extern int eeh_subsystem_enabled;
34
Linus Torvalds1da177e2005-04-16 15:20:36 -070035/* Values for eeh_mode bits in device_node */
36#define EEH_MODE_SUPPORTED (1<<0)
37#define EEH_MODE_NOCHECK (1<<1)
38#define EEH_MODE_ISOLATED (1<<2)
39
Linas Vepstas172ca922005-11-03 18:50:04 -060040/* Max number of EEH freezes allowed before we consider the device
41 * to be permanently disabled. */
42#define EEH_MAX_ALLOWED_FREEZES 5
43
Linus Torvalds1da177e2005-04-16 15:20:36 -070044void __init eeh_init(void);
45unsigned long eeh_check_failure(const volatile void __iomem *token,
46 unsigned long val);
47int eeh_dn_check_failure(struct device_node *dn, struct pci_dev *dev);
48void __init pci_addr_cache_build(void);
49
50/**
51 * eeh_add_device_early
52 * eeh_add_device_late
53 *
54 * Perform eeh initialization for devices added after boot.
55 * Call eeh_add_device_early before doing any i/o to the
56 * device (including config space i/o). Call eeh_add_device_late
57 * to finish the eeh setup for this device.
58 */
59void eeh_add_device_early(struct device_node *);
Linas Vepstase2a296e2005-11-03 18:51:31 -060060void eeh_add_device_tree_early(struct device_node *);
Linus Torvalds1da177e2005-04-16 15:20:36 -070061void eeh_add_device_late(struct pci_dev *);
62
63/**
64 * eeh_remove_device - undo EEH setup for the indicated pci device
65 * @dev: pci device to be removed
66 *
Linas Vepstas172ca922005-11-03 18:50:04 -060067 * This routine should be called when a device is removed from
68 * a running system (e.g. by hotplug or dlpar). It unregisters
69 * the PCI device from the EEH subsystem. I/O errors affecting
70 * this device will no longer be detected after this call; thus,
71 * i/o errors affecting this slot may leave this device unusable.
Linus Torvalds1da177e2005-04-16 15:20:36 -070072 */
73void eeh_remove_device(struct pci_dev *);
74
Linus Torvalds1da177e2005-04-16 15:20:36 -070075/**
Linas Vepstase2a296e2005-11-03 18:51:31 -060076 * eeh_remove_device_recursive - undo EEH for device & children.
77 * @dev: pci device to be removed
78 *
79 * As above, this removes the device; it also removes child
80 * pci devices as well.
81 */
82void eeh_remove_bus_device(struct pci_dev *);
83
84/**
Linus Torvalds1da177e2005-04-16 15:20:36 -070085 * EEH_POSSIBLE_ERROR() -- test for possible MMIO failure.
86 *
87 * If this macro yields TRUE, the caller relays to eeh_check_failure()
88 * which does further tests out of line.
89 */
David Woodhouse1e28a7d2005-11-17 00:44:03 +000090#define EEH_POSSIBLE_ERROR(val, type) ((val) == (type)~0 && eeh_subsystem_enabled)
Linus Torvalds1da177e2005-04-16 15:20:36 -070091
92/*
93 * Reads from a device which has been isolated by EEH will return
94 * all 1s. This macro gives an all-1s value of the given size (in
95 * bytes: 1, 2, or 4) for comparing with the result of a read.
96 */
97#define EEH_IO_ERROR_VALUE(size) (~0U >> ((4 - (size)) * 8))
98
99#else /* !CONFIG_EEH */
100static inline void eeh_init(void) { }
101
102static inline unsigned long eeh_check_failure(const volatile void __iomem *token, unsigned long val)
103{
104 return val;
105}
106
107static inline int eeh_dn_check_failure(struct device_node *dn, struct pci_dev *dev)
108{
109 return 0;
110}
111
112static inline void pci_addr_cache_build(void) { }
113
114static inline void eeh_add_device_early(struct device_node *dn) { }
115
116static inline void eeh_add_device_late(struct pci_dev *dev) { }
117
118static inline void eeh_remove_device(struct pci_dev *dev) { }
119
120#define EEH_POSSIBLE_ERROR(val, type) (0)
121#define EEH_IO_ERROR_VALUE(size) (-1UL)
122#endif /* CONFIG_EEH */
123
Linas Vepstas172ca922005-11-03 18:50:04 -0600124/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700125 * MMIO read/write operations with EEH support.
126 */
127static inline u8 eeh_readb(const volatile void __iomem *addr)
128{
129 u8 val = in_8(addr);
130 if (EEH_POSSIBLE_ERROR(val, u8))
131 return eeh_check_failure(addr, val);
132 return val;
133}
134static inline void eeh_writeb(u8 val, volatile void __iomem *addr)
135{
136 out_8(addr, val);
137}
138
139static inline u16 eeh_readw(const volatile void __iomem *addr)
140{
141 u16 val = in_le16(addr);
142 if (EEH_POSSIBLE_ERROR(val, u16))
143 return eeh_check_failure(addr, val);
144 return val;
145}
146static inline void eeh_writew(u16 val, volatile void __iomem *addr)
147{
148 out_le16(addr, val);
149}
150static inline u16 eeh_raw_readw(const volatile void __iomem *addr)
151{
152 u16 val = in_be16(addr);
153 if (EEH_POSSIBLE_ERROR(val, u16))
154 return eeh_check_failure(addr, val);
155 return val;
156}
157static inline void eeh_raw_writew(u16 val, volatile void __iomem *addr) {
158 volatile u16 __iomem *vaddr = (volatile u16 __iomem *) addr;
159 out_be16(vaddr, val);
160}
161
162static inline u32 eeh_readl(const volatile void __iomem *addr)
163{
164 u32 val = in_le32(addr);
165 if (EEH_POSSIBLE_ERROR(val, u32))
166 return eeh_check_failure(addr, val);
167 return val;
168}
169static inline void eeh_writel(u32 val, volatile void __iomem *addr)
170{
171 out_le32(addr, val);
172}
173static inline u32 eeh_raw_readl(const volatile void __iomem *addr)
174{
175 u32 val = in_be32(addr);
176 if (EEH_POSSIBLE_ERROR(val, u32))
177 return eeh_check_failure(addr, val);
178 return val;
179}
180static inline void eeh_raw_writel(u32 val, volatile void __iomem *addr)
181{
182 out_be32(addr, val);
183}
184
185static inline u64 eeh_readq(const volatile void __iomem *addr)
186{
187 u64 val = in_le64(addr);
188 if (EEH_POSSIBLE_ERROR(val, u64))
189 return eeh_check_failure(addr, val);
190 return val;
191}
192static inline void eeh_writeq(u64 val, volatile void __iomem *addr)
193{
194 out_le64(addr, val);
195}
196static inline u64 eeh_raw_readq(const volatile void __iomem *addr)
197{
198 u64 val = in_be64(addr);
199 if (EEH_POSSIBLE_ERROR(val, u64))
200 return eeh_check_failure(addr, val);
201 return val;
202}
203static inline void eeh_raw_writeq(u64 val, volatile void __iomem *addr)
204{
205 out_be64(addr, val);
206}
207
208#define EEH_CHECK_ALIGN(v,a) \
209 ((((unsigned long)(v)) & ((a) - 1)) == 0)
210
211static inline void eeh_memset_io(volatile void __iomem *addr, int c,
212 unsigned long n)
213{
viro@ZenIV.linux.org.uk6c9afc62005-09-09 16:50:43 +0100214 void *p = (void __force *)addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700215 u32 lc = c;
216 lc |= lc << 8;
217 lc |= lc << 16;
218
viro@ZenIV.linux.org.uk6c9afc62005-09-09 16:50:43 +0100219 while(n && !EEH_CHECK_ALIGN(p, 4)) {
220 *((volatile u8 *)p) = c;
221 p++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700222 n--;
223 }
224 while(n >= 4) {
viro@ZenIV.linux.org.uk6c9afc62005-09-09 16:50:43 +0100225 *((volatile u32 *)p) = lc;
226 p += 4;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700227 n -= 4;
228 }
229 while(n) {
viro@ZenIV.linux.org.uk6c9afc62005-09-09 16:50:43 +0100230 *((volatile u8 *)p) = c;
231 p++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700232 n--;
233 }
234 __asm__ __volatile__ ("sync" : : : "memory");
235}
236static inline void eeh_memcpy_fromio(void *dest, const volatile void __iomem *src,
237 unsigned long n)
238{
239 void *vsrc = (void __force *) src;
240 void *destsave = dest;
241 unsigned long nsave = n;
242
243 while(n && (!EEH_CHECK_ALIGN(vsrc, 4) || !EEH_CHECK_ALIGN(dest, 4))) {
244 *((u8 *)dest) = *((volatile u8 *)vsrc);
245 __asm__ __volatile__ ("eieio" : : : "memory");
viro@ZenIV.linux.org.uk6c9afc62005-09-09 16:50:43 +0100246 vsrc++;
247 dest++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700248 n--;
249 }
250 while(n > 4) {
251 *((u32 *)dest) = *((volatile u32 *)vsrc);
252 __asm__ __volatile__ ("eieio" : : : "memory");
viro@ZenIV.linux.org.uk6c9afc62005-09-09 16:50:43 +0100253 vsrc += 4;
254 dest += 4;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700255 n -= 4;
256 }
257 while(n) {
258 *((u8 *)dest) = *((volatile u8 *)vsrc);
259 __asm__ __volatile__ ("eieio" : : : "memory");
viro@ZenIV.linux.org.uk6c9afc62005-09-09 16:50:43 +0100260 vsrc++;
261 dest++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700262 n--;
263 }
264 __asm__ __volatile__ ("sync" : : : "memory");
265
266 /* Look for ffff's here at dest[n]. Assume that at least 4 bytes
267 * were copied. Check all four bytes.
268 */
269 if ((nsave >= 4) &&
270 (EEH_POSSIBLE_ERROR((*((u32 *) destsave+nsave-4)), u32))) {
271 eeh_check_failure(src, (*((u32 *) destsave+nsave-4)));
272 }
273}
274
275static inline void eeh_memcpy_toio(volatile void __iomem *dest, const void *src,
276 unsigned long n)
277{
278 void *vdest = (void __force *) dest;
279
280 while(n && (!EEH_CHECK_ALIGN(vdest, 4) || !EEH_CHECK_ALIGN(src, 4))) {
281 *((volatile u8 *)vdest) = *((u8 *)src);
viro@ZenIV.linux.org.uk6c9afc62005-09-09 16:50:43 +0100282 src++;
283 vdest++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700284 n--;
285 }
286 while(n > 4) {
287 *((volatile u32 *)vdest) = *((volatile u32 *)src);
viro@ZenIV.linux.org.uk6c9afc62005-09-09 16:50:43 +0100288 src += 4;
289 vdest += 4;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700290 n-=4;
291 }
292 while(n) {
293 *((volatile u8 *)vdest) = *((u8 *)src);
viro@ZenIV.linux.org.uk6c9afc62005-09-09 16:50:43 +0100294 src++;
295 vdest++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700296 n--;
297 }
298 __asm__ __volatile__ ("sync" : : : "memory");
299}
300
301#undef EEH_CHECK_ALIGN
302
303static inline u8 eeh_inb(unsigned long port)
304{
305 u8 val;
306 if (!_IO_IS_VALID(port))
307 return ~0;
308 val = in_8((u8 __iomem *)(port+pci_io_base));
309 if (EEH_POSSIBLE_ERROR(val, u8))
310 return eeh_check_failure((void __iomem *)(port), val);
311 return val;
312}
313
314static inline void eeh_outb(u8 val, unsigned long port)
315{
316 if (_IO_IS_VALID(port))
317 out_8((u8 __iomem *)(port+pci_io_base), val);
318}
319
320static inline u16 eeh_inw(unsigned long port)
321{
322 u16 val;
323 if (!_IO_IS_VALID(port))
324 return ~0;
325 val = in_le16((u16 __iomem *)(port+pci_io_base));
326 if (EEH_POSSIBLE_ERROR(val, u16))
327 return eeh_check_failure((void __iomem *)(port), val);
328 return val;
329}
330
331static inline void eeh_outw(u16 val, unsigned long port)
332{
333 if (_IO_IS_VALID(port))
334 out_le16((u16 __iomem *)(port+pci_io_base), val);
335}
336
337static inline u32 eeh_inl(unsigned long port)
338{
339 u32 val;
340 if (!_IO_IS_VALID(port))
341 return ~0;
342 val = in_le32((u32 __iomem *)(port+pci_io_base));
343 if (EEH_POSSIBLE_ERROR(val, u32))
344 return eeh_check_failure((void __iomem *)(port), val);
345 return val;
346}
347
348static inline void eeh_outl(u32 val, unsigned long port)
349{
350 if (_IO_IS_VALID(port))
351 out_le32((u32 __iomem *)(port+pci_io_base), val);
352}
353
354/* in-string eeh macros */
355static inline void eeh_insb(unsigned long port, void * buf, int ns)
356{
357 _insb((u8 __iomem *)(port+pci_io_base), buf, ns);
358 if (EEH_POSSIBLE_ERROR((*(((u8*)buf)+ns-1)), u8))
359 eeh_check_failure((void __iomem *)(port), *(u8*)buf);
360}
361
362static inline void eeh_insw_ns(unsigned long port, void * buf, int ns)
363{
364 _insw_ns((u16 __iomem *)(port+pci_io_base), buf, ns);
365 if (EEH_POSSIBLE_ERROR((*(((u16*)buf)+ns-1)), u16))
366 eeh_check_failure((void __iomem *)(port), *(u16*)buf);
367}
368
369static inline void eeh_insl_ns(unsigned long port, void * buf, int nl)
370{
371 _insl_ns((u32 __iomem *)(port+pci_io_base), buf, nl);
372 if (EEH_POSSIBLE_ERROR((*(((u32*)buf)+nl-1)), u32))
373 eeh_check_failure((void __iomem *)(port), *(u32*)buf);
374}
375
376#endif /* _PPC64_EEH_H */