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/* Start Change Log
21 * 2001/10/27 : engebret : Created.
22 * End Change Log
23 */
24
25#ifndef _EEH_H
26#define _EEH_H
27
28struct pci_dev;
29
30#define IO_UNMAPPED_REGION_ID 0xaUL
31
32#define IO_TOKEN_TO_ADDR(token) ((((unsigned long)(token)) & 0xFFFFFFFF) | (0xEUL << 60))
33/* Flag bits encoded in the 3 unused function bits of devfn */
34#define EEH_TOKEN_DISABLED (1UL << 34UL)	/* eeh is disabled for this token */
35#define IS_EEH_TOKEN_DISABLED(token) ((unsigned long)(token) & EEH_TOKEN_DISABLED)
36
37#define EEH_STATE_OVERRIDE 1   /* IOA does not require eeh traps */
38#define EEH_STATE_FAILURE  16  /* */
39
40/* This is for profiling only */
41extern unsigned long eeh_total_mmio_ffs;
42
43extern int eeh_implemented;
44
45void eeh_init(void);
46static inline int is_eeh_implemented(void) { return eeh_implemented; }
47int eeh_get_state(unsigned long ea);
48unsigned long eeh_check_failure(void *token, unsigned long val);
49
50#define EEH_DISABLE		0
51#define EEH_ENABLE		1
52#define EEH_RELEASE_LOADSTORE	2
53#define EEH_RELEASE_DMA		3
54int eeh_set_option(struct pci_dev *dev, int options);
55
56/* Given a PCI device check if eeh should be configured or not.
57 * This may look at firmware properties and/or kernel cmdline options.
58 */
59int is_eeh_configured(struct pci_dev *dev);
60
61/* Generate an EEH token.
62 * The high nibble of the offset is cleared, otherwise bounds checking is performed.
63 * Use IO_TOKEN_TO_ADDR(token) to translate this token back to a mapped virtual addr.
64 * Do NOT do this to perform IO -- use the read/write macros!
65 */
66unsigned long eeh_token(unsigned long phb,
67			unsigned long bus,
68			unsigned long devfn,
69			unsigned long offset);
70
71extern void *memcpy(void *, const void *, unsigned long);
72extern void *memset(void *,int, unsigned long);
73
74/* EEH_POSSIBLE_ERROR() -- test for possible MMIO failure.
75 *
76 * Order this macro for performance.
77 * If EEH is off for a device and it is a memory BAR, ioremap will
78 * map it to the IOREGION.  In this case addr == vaddr and since these
79 * should be in registers we compare them first.  Next we check for
80 * all ones which is perhaps fastest as ~val.  Finally we weed out
81 * EEH disabled IO BARs.
82 *
83 * If this macro yields TRUE, the caller relays to eeh_check_failure()
84 * which does further tests out of line.
85 */
86/* #define EEH_POSSIBLE_ERROR(addr, vaddr, val) ((vaddr) != (addr) && ~(val) == 0 && !IS_EEH_TOKEN_DISABLED(addr)) */
87/* This version is rearranged to collect some profiling data */
88#define EEH_POSSIBLE_ERROR(addr, vaddr, val) (~(val) == 0 && (++eeh_total_mmio_ffs, (vaddr) != (addr) && !IS_EEH_TOKEN_DISABLED(addr)))
89
90/*
91 * MMIO read/write operations with EEH support.
92 *
93 * addr: 64b token of the form 0xA0PPBBDDyyyyyyyy
94 *       0xA0     : Unmapped MMIO region
95 *       PP       : PHB index (starting at zero)
96 *	 BB	  : PCI Bus number under given PHB
97 *	 DD	  : PCI devfn under given bus
98 *       yyyyyyyy : Virtual address offset
99 *
100 * An actual virtual address is produced from this token
101 * by masking into the form:
102 *   0xE0000000yyyyyyyy
103 */
104static inline u8 eeh_readb(void *addr) {
105	volatile u8 *vaddr = (volatile u8 *)IO_TOKEN_TO_ADDR(addr);
106	u8 val = in_8(vaddr);
107	if (EEH_POSSIBLE_ERROR(addr, vaddr, val))
108		return eeh_check_failure(addr, val);
109	return val;
110}
111static inline void eeh_writeb(u8 val, void *addr) {
112	volatile u8 *vaddr = (volatile u8 *)IO_TOKEN_TO_ADDR(addr);
113	out_8(vaddr, val);
114}
115static inline u16 eeh_readw(void *addr) {
116	volatile u16 *vaddr = (volatile u16 *)IO_TOKEN_TO_ADDR(addr);
117	u16 val = in_le16(vaddr);
118	if (EEH_POSSIBLE_ERROR(addr, vaddr, val))
119		return eeh_check_failure(addr, val);
120	return val;
121}
122static inline void eeh_writew(u16 val, void *addr) {
123	volatile u16 *vaddr = (volatile u16 *)IO_TOKEN_TO_ADDR(addr);
124	out_le16(vaddr, val);
125}
126static inline u32 eeh_readl(void *addr) {
127	volatile u32 *vaddr = (volatile u32 *)IO_TOKEN_TO_ADDR(addr);
128	u32 val = in_le32(vaddr);
129	if (EEH_POSSIBLE_ERROR(addr, vaddr, val))
130		return eeh_check_failure(addr, val);
131	return val;
132}
133static inline void eeh_writel(u32 val, void *addr) {
134	volatile u32 *vaddr = (volatile u32 *)IO_TOKEN_TO_ADDR(addr);
135	out_le32(vaddr, val);
136}
137
138static inline void eeh_memset_io(void *addr, int c, unsigned long n) {
139	void *vaddr = (void *)IO_TOKEN_TO_ADDR(addr);
140	memset(vaddr, c, n);
141}
142static inline void eeh_memcpy_fromio(void *dest, void *src, unsigned long n) {
143	void *vsrc = (void *)IO_TOKEN_TO_ADDR(src);
144	memcpy(dest, vsrc, n);
145	/* look for ffff's here at dest[n] */
146}
147static inline void eeh_memcpy_toio(void *dest, void *src, unsigned long n) {
148	void *vdest = (void *)IO_TOKEN_TO_ADDR(dest);
149	memcpy(vdest, src, n);
150}
151
152static inline void eeh_insb(volatile u8 *addr, void *buf, int n) {
153	volatile u8 *vaddr = (volatile u8 *)IO_TOKEN_TO_ADDR(addr);
154	_insb(vaddr, buf, n);
155	/* ToDo: look for ff's in buf[n] */
156}
157
158static inline void eeh_outsb(volatile u8 *addr, const void *buf, int n) {
159	volatile u8 *vaddr = (volatile u8 *)IO_TOKEN_TO_ADDR(addr);
160	_outsb(vaddr, buf, n);
161}
162
163static inline void eeh_insw_ns(volatile u16 *addr, void *buf, int n) {
164	volatile u16 *vaddr = (volatile u16 *)IO_TOKEN_TO_ADDR(addr);
165	_insw_ns(vaddr, buf, n);
166	/* ToDo: look for ffff's in buf[n] */
167}
168
169static inline void eeh_outsw_ns(volatile u16 *addr, const void *buf, int n) {
170	volatile u16 *vaddr = (volatile u16 *)IO_TOKEN_TO_ADDR(addr);
171	_outsw_ns(vaddr, buf, n);
172}
173
174static inline void eeh_insl_ns(volatile u32 *addr, void *buf, int n) {
175	volatile u32 *vaddr = (volatile u32 *)IO_TOKEN_TO_ADDR(addr);
176	_insl_ns(vaddr, buf, n);
177	/* ToDo: look for ffffffff's in buf[n] */
178}
179
180static inline void eeh_outsl_ns(volatile u32 *addr, const void *buf, int n) {
181	volatile u32 *vaddr = (volatile u32 *)IO_TOKEN_TO_ADDR(addr);
182	_outsl_ns(vaddr, buf, n);
183}
184
185
186#endif /* _EEH_H */
187