xref: /openbmc/linux/drivers/edac/e7xxx_edac.c (revision 1ac731c529cd4d6adbce134754b51ff7d822b145)
1806c35f5SAlan Cox /*
2806c35f5SAlan Cox  * Intel e7xxx Memory Controller kernel module
3806c35f5SAlan Cox  * (C) 2003 Linux Networx (http://lnxi.com)
4806c35f5SAlan Cox  * This file may be distributed under the terms of the
5806c35f5SAlan Cox  * GNU General Public License.
6806c35f5SAlan Cox  *
7806c35f5SAlan Cox  * See "enum e7xxx_chips" below for supported chipsets
8806c35f5SAlan Cox  *
9806c35f5SAlan Cox  * Written by Thayne Harbaugh
10806c35f5SAlan Cox  * Based on work by Dan Hollis <goemon at anime dot net> and others.
11806c35f5SAlan Cox  *	http://www.anime.net/~goemon/linux-ecc/
12806c35f5SAlan Cox  *
1330ac4406SMauro Carvalho Chehab  * Datasheet:
1430ac4406SMauro Carvalho Chehab  *	http://www.intel.com/content/www/us/en/chipsets/e7501-chipset-memory-controller-hub-datasheet.html
1530ac4406SMauro Carvalho Chehab  *
16806c35f5SAlan Cox  * Contributors:
17806c35f5SAlan Cox  *	Eric Biederman (Linux Networx)
18806c35f5SAlan Cox  *	Tom Zimmerman (Linux Networx)
19806c35f5SAlan Cox  *	Jim Garlick (Lawrence Livermore National Labs)
20806c35f5SAlan Cox  *	Dave Peterson (Lawrence Livermore National Labs)
21806c35f5SAlan Cox  *	That One Guy (Some other place)
22806c35f5SAlan Cox  *	Wang Zhenyu (intel.com)
23806c35f5SAlan Cox  *
24806c35f5SAlan Cox  * $Id: edac_e7xxx.c,v 1.5.2.9 2005/10/05 00:43:44 dsp_llnl Exp $
25806c35f5SAlan Cox  *
26806c35f5SAlan Cox  */
27806c35f5SAlan Cox 
28806c35f5SAlan Cox #include <linux/module.h>
29806c35f5SAlan Cox #include <linux/init.h>
30806c35f5SAlan Cox #include <linux/pci.h>
31806c35f5SAlan Cox #include <linux/pci_ids.h>
32c0d12172SDave Jiang #include <linux/edac.h>
3378d88e8aSMauro Carvalho Chehab #include "edac_module.h"
34806c35f5SAlan Cox 
35929a40ecSDoug Thompson #define	EDAC_MOD_STR	"e7xxx_edac"
3637f04581SDoug Thompson 
37537fba28SDave Peterson #define e7xxx_printk(level, fmt, arg...) \
38537fba28SDave Peterson 	edac_printk(level, "e7xxx", fmt, ##arg)
39537fba28SDave Peterson 
40537fba28SDave Peterson #define e7xxx_mc_printk(mci, level, fmt, arg...) \
41537fba28SDave Peterson 	edac_mc_chipset_printk(mci, level, "e7xxx", fmt, ##arg)
42537fba28SDave Peterson 
43806c35f5SAlan Cox #ifndef PCI_DEVICE_ID_INTEL_7205_0
44806c35f5SAlan Cox #define PCI_DEVICE_ID_INTEL_7205_0	0x255d
45806c35f5SAlan Cox #endif				/* PCI_DEVICE_ID_INTEL_7205_0 */
46806c35f5SAlan Cox 
47806c35f5SAlan Cox #ifndef PCI_DEVICE_ID_INTEL_7205_1_ERR
48806c35f5SAlan Cox #define PCI_DEVICE_ID_INTEL_7205_1_ERR	0x2551
49806c35f5SAlan Cox #endif				/* PCI_DEVICE_ID_INTEL_7205_1_ERR */
50806c35f5SAlan Cox 
51806c35f5SAlan Cox #ifndef PCI_DEVICE_ID_INTEL_7500_0
52806c35f5SAlan Cox #define PCI_DEVICE_ID_INTEL_7500_0	0x2540
53806c35f5SAlan Cox #endif				/* PCI_DEVICE_ID_INTEL_7500_0 */
54806c35f5SAlan Cox 
55806c35f5SAlan Cox #ifndef PCI_DEVICE_ID_INTEL_7500_1_ERR
56806c35f5SAlan Cox #define PCI_DEVICE_ID_INTEL_7500_1_ERR	0x2541
57806c35f5SAlan Cox #endif				/* PCI_DEVICE_ID_INTEL_7500_1_ERR */
58806c35f5SAlan Cox 
59806c35f5SAlan Cox #ifndef PCI_DEVICE_ID_INTEL_7501_0
60806c35f5SAlan Cox #define PCI_DEVICE_ID_INTEL_7501_0	0x254c
61806c35f5SAlan Cox #endif				/* PCI_DEVICE_ID_INTEL_7501_0 */
62806c35f5SAlan Cox 
63806c35f5SAlan Cox #ifndef PCI_DEVICE_ID_INTEL_7501_1_ERR
64806c35f5SAlan Cox #define PCI_DEVICE_ID_INTEL_7501_1_ERR	0x2541
65806c35f5SAlan Cox #endif				/* PCI_DEVICE_ID_INTEL_7501_1_ERR */
66806c35f5SAlan Cox 
67806c35f5SAlan Cox #ifndef PCI_DEVICE_ID_INTEL_7505_0
68806c35f5SAlan Cox #define PCI_DEVICE_ID_INTEL_7505_0	0x2550
69806c35f5SAlan Cox #endif				/* PCI_DEVICE_ID_INTEL_7505_0 */
70806c35f5SAlan Cox 
71806c35f5SAlan Cox #ifndef PCI_DEVICE_ID_INTEL_7505_1_ERR
72806c35f5SAlan Cox #define PCI_DEVICE_ID_INTEL_7505_1_ERR	0x2551
73806c35f5SAlan Cox #endif				/* PCI_DEVICE_ID_INTEL_7505_1_ERR */
74806c35f5SAlan Cox 
75806c35f5SAlan Cox #define E7XXX_NR_CSROWS		8	/* number of csrows */
7630ac4406SMauro Carvalho Chehab #define E7XXX_NR_DIMMS		8	/* 2 channels, 4 dimms/channel */
77806c35f5SAlan Cox 
78806c35f5SAlan Cox /* E7XXX register addresses - device 0 function 0 */
79806c35f5SAlan Cox #define E7XXX_DRB		0x60	/* DRAM row boundary register (8b) */
80806c35f5SAlan Cox #define E7XXX_DRA		0x70	/* DRAM row attribute register (8b) */
81806c35f5SAlan Cox 					/*
82806c35f5SAlan Cox 					 * 31   Device width row 7 0=x8 1=x4
83806c35f5SAlan Cox 					 * 27   Device width row 6
84806c35f5SAlan Cox 					 * 23   Device width row 5
85806c35f5SAlan Cox 					 * 19   Device width row 4
86806c35f5SAlan Cox 					 * 15   Device width row 3
87806c35f5SAlan Cox 					 * 11   Device width row 2
88806c35f5SAlan Cox 					 *  7   Device width row 1
89806c35f5SAlan Cox 					 *  3   Device width row 0
90806c35f5SAlan Cox 					 */
91806c35f5SAlan Cox #define E7XXX_DRC		0x7C	/* DRAM controller mode reg (32b) */
92806c35f5SAlan Cox 					/*
93806c35f5SAlan Cox 					 * 22    Number channels 0=1,1=2
94806c35f5SAlan Cox 					 * 19:18 DRB Granularity 32/64MB
95806c35f5SAlan Cox 					 */
96806c35f5SAlan Cox #define E7XXX_TOLM		0xC4	/* DRAM top of low memory reg (16b) */
97806c35f5SAlan Cox #define E7XXX_REMAPBASE		0xC6	/* DRAM remap base address reg (16b) */
98806c35f5SAlan Cox #define E7XXX_REMAPLIMIT	0xC8	/* DRAM remap limit address reg (16b) */
99806c35f5SAlan Cox 
100806c35f5SAlan Cox /* E7XXX register addresses - device 0 function 1 */
101806c35f5SAlan Cox #define E7XXX_DRAM_FERR		0x80	/* DRAM first error register (8b) */
102806c35f5SAlan Cox #define E7XXX_DRAM_NERR		0x82	/* DRAM next error register (8b) */
103806c35f5SAlan Cox #define E7XXX_DRAM_CELOG_ADD	0xA0	/* DRAM first correctable memory */
104806c35f5SAlan Cox 					/*     error address register (32b) */
105806c35f5SAlan Cox 					/*
106806c35f5SAlan Cox 					 * 31:28 Reserved
107806c35f5SAlan Cox 					 * 27:6  CE address (4k block 33:12)
108806c35f5SAlan Cox 					 *  5:0  Reserved
109806c35f5SAlan Cox 					 */
110806c35f5SAlan Cox #define E7XXX_DRAM_UELOG_ADD	0xB0	/* DRAM first uncorrectable memory */
111806c35f5SAlan Cox 					/*     error address register (32b) */
112806c35f5SAlan Cox 					/*
113806c35f5SAlan Cox 					 * 31:28 Reserved
114806c35f5SAlan Cox 					 * 27:6  CE address (4k block 33:12)
115806c35f5SAlan Cox 					 *  5:0  Reserved
116806c35f5SAlan Cox 					 */
117806c35f5SAlan Cox #define E7XXX_DRAM_CELOG_SYNDROME 0xD0	/* DRAM first correctable memory */
118806c35f5SAlan Cox 					/*     error syndrome register (16b) */
119806c35f5SAlan Cox 
120806c35f5SAlan Cox enum e7xxx_chips {
121806c35f5SAlan Cox 	E7500 = 0,
122806c35f5SAlan Cox 	E7501,
123806c35f5SAlan Cox 	E7505,
124806c35f5SAlan Cox 	E7205,
125806c35f5SAlan Cox };
126806c35f5SAlan Cox 
127806c35f5SAlan Cox struct e7xxx_pvt {
128806c35f5SAlan Cox 	struct pci_dev *bridge_ck;
129806c35f5SAlan Cox 	u32 tolm;
130806c35f5SAlan Cox 	u32 remapbase;
131806c35f5SAlan Cox 	u32 remaplimit;
132806c35f5SAlan Cox 	const struct e7xxx_dev_info *dev_info;
133806c35f5SAlan Cox };
134806c35f5SAlan Cox 
135806c35f5SAlan Cox struct e7xxx_dev_info {
136806c35f5SAlan Cox 	u16 err_dev;
137806c35f5SAlan Cox 	const char *ctl_name;
138806c35f5SAlan Cox };
139806c35f5SAlan Cox 
140806c35f5SAlan Cox struct e7xxx_error_info {
141806c35f5SAlan Cox 	u8 dram_ferr;
142806c35f5SAlan Cox 	u8 dram_nerr;
143806c35f5SAlan Cox 	u32 dram_celog_add;
144806c35f5SAlan Cox 	u16 dram_celog_syndrome;
145806c35f5SAlan Cox 	u32 dram_uelog_add;
146806c35f5SAlan Cox };
147806c35f5SAlan Cox 
148456a2f95SDave Jiang static struct edac_pci_ctl_info *e7xxx_pci;
149456a2f95SDave Jiang 
150806c35f5SAlan Cox static const struct e7xxx_dev_info e7xxx_devs[] = {
151806c35f5SAlan Cox 	[E7500] = {
152806c35f5SAlan Cox 		.err_dev = PCI_DEVICE_ID_INTEL_7500_1_ERR,
153849a4c37SDave Jiang 		.ctl_name = "E7500"},
154806c35f5SAlan Cox 	[E7501] = {
155806c35f5SAlan Cox 		.err_dev = PCI_DEVICE_ID_INTEL_7501_1_ERR,
156849a4c37SDave Jiang 		.ctl_name = "E7501"},
157806c35f5SAlan Cox 	[E7505] = {
158806c35f5SAlan Cox 		.err_dev = PCI_DEVICE_ID_INTEL_7505_1_ERR,
159849a4c37SDave Jiang 		.ctl_name = "E7505"},
160806c35f5SAlan Cox 	[E7205] = {
161806c35f5SAlan Cox 		.err_dev = PCI_DEVICE_ID_INTEL_7205_1_ERR,
162849a4c37SDave Jiang 		.ctl_name = "E7205"},
163806c35f5SAlan Cox };
164806c35f5SAlan Cox 
165806c35f5SAlan Cox /* FIXME - is this valid for both SECDED and S4ECD4ED? */
e7xxx_find_channel(u16 syndrome)166806c35f5SAlan Cox static inline int e7xxx_find_channel(u16 syndrome)
167806c35f5SAlan Cox {
168956b9ba1SJoe Perches 	edac_dbg(3, "\n");
169806c35f5SAlan Cox 
170806c35f5SAlan Cox 	if ((syndrome & 0xff00) == 0)
171806c35f5SAlan Cox 		return 0;
172e7ecd891SDave Peterson 
173806c35f5SAlan Cox 	if ((syndrome & 0x00ff) == 0)
174806c35f5SAlan Cox 		return 1;
175e7ecd891SDave Peterson 
176806c35f5SAlan Cox 	if ((syndrome & 0xf000) == 0 || (syndrome & 0x0f00) == 0)
177806c35f5SAlan Cox 		return 0;
178e7ecd891SDave Peterson 
179806c35f5SAlan Cox 	return 1;
180806c35f5SAlan Cox }
181806c35f5SAlan Cox 
ctl_page_to_phys(struct mem_ctl_info * mci,unsigned long page)182e7ecd891SDave Peterson static unsigned long ctl_page_to_phys(struct mem_ctl_info *mci,
183e7ecd891SDave Peterson 				unsigned long page)
184806c35f5SAlan Cox {
185806c35f5SAlan Cox 	u32 remap;
186806c35f5SAlan Cox 	struct e7xxx_pvt *pvt = (struct e7xxx_pvt *)mci->pvt_info;
187806c35f5SAlan Cox 
188956b9ba1SJoe Perches 	edac_dbg(3, "\n");
189806c35f5SAlan Cox 
190806c35f5SAlan Cox 	if ((page < pvt->tolm) ||
191806c35f5SAlan Cox 		((page >= 0x100000) && (page < pvt->remapbase)))
192806c35f5SAlan Cox 		return page;
193e7ecd891SDave Peterson 
194806c35f5SAlan Cox 	remap = (page - pvt->tolm) + pvt->remapbase;
195e7ecd891SDave Peterson 
196806c35f5SAlan Cox 	if (remap < pvt->remaplimit)
197806c35f5SAlan Cox 		return remap;
198e7ecd891SDave Peterson 
199537fba28SDave Peterson 	e7xxx_printk(KERN_ERR, "Invalid page %lx - out of range\n", page);
200806c35f5SAlan Cox 	return pvt->tolm - 1;
201806c35f5SAlan Cox }
202806c35f5SAlan Cox 
process_ce(struct mem_ctl_info * mci,struct e7xxx_error_info * info)203849a4c37SDave Jiang static void process_ce(struct mem_ctl_info *mci, struct e7xxx_error_info *info)
204806c35f5SAlan Cox {
205806c35f5SAlan Cox 	u32 error_1b, page;
206806c35f5SAlan Cox 	u16 syndrome;
207806c35f5SAlan Cox 	int row;
208806c35f5SAlan Cox 	int channel;
209806c35f5SAlan Cox 
210956b9ba1SJoe Perches 	edac_dbg(3, "\n");
211806c35f5SAlan Cox 	/* read the error address */
212806c35f5SAlan Cox 	error_1b = info->dram_celog_add;
213806c35f5SAlan Cox 	/* FIXME - should use PAGE_SHIFT */
214806c35f5SAlan Cox 	page = error_1b >> 6;	/* convert the address to 4k page */
215806c35f5SAlan Cox 	/* read the syndrome */
216806c35f5SAlan Cox 	syndrome = info->dram_celog_syndrome;
217806c35f5SAlan Cox 	/* FIXME - check for -1 */
218806c35f5SAlan Cox 	row = edac_mc_find_csrow_by_page(mci, page);
219806c35f5SAlan Cox 	/* convert syndrome to channel */
220806c35f5SAlan Cox 	channel = e7xxx_find_channel(syndrome);
2219eb07a7fSMauro Carvalho Chehab 	edac_mc_handle_error(HW_EVENT_ERR_CORRECTED, mci, 1, page, 0, syndrome,
22203f7eae8SMauro Carvalho Chehab 			     row, channel, -1, "e7xxx CE", "");
223806c35f5SAlan Cox }
224806c35f5SAlan Cox 
process_ce_no_info(struct mem_ctl_info * mci)225806c35f5SAlan Cox static void process_ce_no_info(struct mem_ctl_info *mci)
226806c35f5SAlan Cox {
227956b9ba1SJoe Perches 	edac_dbg(3, "\n");
2288030122aSJason Baron 	edac_mc_handle_error(HW_EVENT_ERR_CORRECTED, mci, 1, 0, 0, 0, -1, -1, -1,
22903f7eae8SMauro Carvalho Chehab 			     "e7xxx CE log register overflow", "");
230806c35f5SAlan Cox }
231806c35f5SAlan Cox 
process_ue(struct mem_ctl_info * mci,struct e7xxx_error_info * info)232849a4c37SDave Jiang static void process_ue(struct mem_ctl_info *mci, struct e7xxx_error_info *info)
233806c35f5SAlan Cox {
234806c35f5SAlan Cox 	u32 error_2b, block_page;
235806c35f5SAlan Cox 	int row;
236806c35f5SAlan Cox 
237956b9ba1SJoe Perches 	edac_dbg(3, "\n");
238806c35f5SAlan Cox 	/* read the error address */
239806c35f5SAlan Cox 	error_2b = info->dram_uelog_add;
240806c35f5SAlan Cox 	/* FIXME - should use PAGE_SHIFT */
241806c35f5SAlan Cox 	block_page = error_2b >> 6;	/* convert to 4k address */
242806c35f5SAlan Cox 	row = edac_mc_find_csrow_by_page(mci, block_page);
24330ac4406SMauro Carvalho Chehab 
2449eb07a7fSMauro Carvalho Chehab 	edac_mc_handle_error(HW_EVENT_ERR_UNCORRECTED, mci, 1, block_page, 0, 0,
24503f7eae8SMauro Carvalho Chehab 			     row, -1, -1, "e7xxx UE", "");
246806c35f5SAlan Cox }
247806c35f5SAlan Cox 
process_ue_no_info(struct mem_ctl_info * mci)248806c35f5SAlan Cox static void process_ue_no_info(struct mem_ctl_info *mci)
249806c35f5SAlan Cox {
250956b9ba1SJoe Perches 	edac_dbg(3, "\n");
25130ac4406SMauro Carvalho Chehab 
2529eb07a7fSMauro Carvalho Chehab 	edac_mc_handle_error(HW_EVENT_ERR_UNCORRECTED, mci, 1, 0, 0, 0, -1, -1, -1,
25303f7eae8SMauro Carvalho Chehab 			     "e7xxx UE log register overflow", "");
254806c35f5SAlan Cox }
255806c35f5SAlan Cox 
e7xxx_get_error_info(struct mem_ctl_info * mci,struct e7xxx_error_info * info)256806c35f5SAlan Cox static void e7xxx_get_error_info(struct mem_ctl_info *mci,
257806c35f5SAlan Cox 				 struct e7xxx_error_info *info)
258806c35f5SAlan Cox {
259806c35f5SAlan Cox 	struct e7xxx_pvt *pvt;
260806c35f5SAlan Cox 
261806c35f5SAlan Cox 	pvt = (struct e7xxx_pvt *)mci->pvt_info;
262849a4c37SDave Jiang 	pci_read_config_byte(pvt->bridge_ck, E7XXX_DRAM_FERR, &info->dram_ferr);
263849a4c37SDave Jiang 	pci_read_config_byte(pvt->bridge_ck, E7XXX_DRAM_NERR, &info->dram_nerr);
264806c35f5SAlan Cox 
265806c35f5SAlan Cox 	if ((info->dram_ferr & 1) || (info->dram_nerr & 1)) {
266806c35f5SAlan Cox 		pci_read_config_dword(pvt->bridge_ck, E7XXX_DRAM_CELOG_ADD,
267806c35f5SAlan Cox 				&info->dram_celog_add);
268806c35f5SAlan Cox 		pci_read_config_word(pvt->bridge_ck,
269e7ecd891SDave Peterson 				E7XXX_DRAM_CELOG_SYNDROME,
270e7ecd891SDave Peterson 				&info->dram_celog_syndrome);
271806c35f5SAlan Cox 	}
272806c35f5SAlan Cox 
273806c35f5SAlan Cox 	if ((info->dram_ferr & 2) || (info->dram_nerr & 2))
274806c35f5SAlan Cox 		pci_read_config_dword(pvt->bridge_ck, E7XXX_DRAM_UELOG_ADD,
275806c35f5SAlan Cox 				&info->dram_uelog_add);
276806c35f5SAlan Cox 
277806c35f5SAlan Cox 	if (info->dram_ferr & 3)
278e7ecd891SDave Peterson 		pci_write_bits8(pvt->bridge_ck, E7XXX_DRAM_FERR, 0x03, 0x03);
279806c35f5SAlan Cox 
280806c35f5SAlan Cox 	if (info->dram_nerr & 3)
281e7ecd891SDave Peterson 		pci_write_bits8(pvt->bridge_ck, E7XXX_DRAM_NERR, 0x03, 0x03);
282806c35f5SAlan Cox }
283806c35f5SAlan Cox 
e7xxx_process_error_info(struct mem_ctl_info * mci,struct e7xxx_error_info * info,int handle_errors)284806c35f5SAlan Cox static int e7xxx_process_error_info(struct mem_ctl_info *mci,
285849a4c37SDave Jiang 				struct e7xxx_error_info *info,
286849a4c37SDave Jiang 				int handle_errors)
287806c35f5SAlan Cox {
288806c35f5SAlan Cox 	int error_found;
289806c35f5SAlan Cox 
290806c35f5SAlan Cox 	error_found = 0;
291806c35f5SAlan Cox 
292806c35f5SAlan Cox 	/* decode and report errors */
293806c35f5SAlan Cox 	if (info->dram_ferr & 1) {	/* check first error correctable */
294806c35f5SAlan Cox 		error_found = 1;
295806c35f5SAlan Cox 
296806c35f5SAlan Cox 		if (handle_errors)
297806c35f5SAlan Cox 			process_ce(mci, info);
298806c35f5SAlan Cox 	}
299806c35f5SAlan Cox 
300806c35f5SAlan Cox 	if (info->dram_ferr & 2) {	/* check first error uncorrectable */
301806c35f5SAlan Cox 		error_found = 1;
302806c35f5SAlan Cox 
303806c35f5SAlan Cox 		if (handle_errors)
304806c35f5SAlan Cox 			process_ue(mci, info);
305806c35f5SAlan Cox 	}
306806c35f5SAlan Cox 
307806c35f5SAlan Cox 	if (info->dram_nerr & 1) {	/* check next error correctable */
308806c35f5SAlan Cox 		error_found = 1;
309806c35f5SAlan Cox 
310806c35f5SAlan Cox 		if (handle_errors) {
311806c35f5SAlan Cox 			if (info->dram_ferr & 1)
312806c35f5SAlan Cox 				process_ce_no_info(mci);
313806c35f5SAlan Cox 			else
314806c35f5SAlan Cox 				process_ce(mci, info);
315806c35f5SAlan Cox 		}
316806c35f5SAlan Cox 	}
317806c35f5SAlan Cox 
318806c35f5SAlan Cox 	if (info->dram_nerr & 2) {	/* check next error uncorrectable */
319806c35f5SAlan Cox 		error_found = 1;
320806c35f5SAlan Cox 
321806c35f5SAlan Cox 		if (handle_errors) {
322806c35f5SAlan Cox 			if (info->dram_ferr & 2)
323806c35f5SAlan Cox 				process_ue_no_info(mci);
324806c35f5SAlan Cox 			else
325806c35f5SAlan Cox 				process_ue(mci, info);
326806c35f5SAlan Cox 		}
327806c35f5SAlan Cox 	}
328806c35f5SAlan Cox 
329806c35f5SAlan Cox 	return error_found;
330806c35f5SAlan Cox }
331806c35f5SAlan Cox 
e7xxx_check(struct mem_ctl_info * mci)332806c35f5SAlan Cox static void e7xxx_check(struct mem_ctl_info *mci)
333806c35f5SAlan Cox {
334806c35f5SAlan Cox 	struct e7xxx_error_info info;
335806c35f5SAlan Cox 
336806c35f5SAlan Cox 	e7xxx_get_error_info(mci, &info);
337806c35f5SAlan Cox 	e7xxx_process_error_info(mci, &info, 1);
338806c35f5SAlan Cox }
339806c35f5SAlan Cox 
34013189525SDoug Thompson /* Return 1 if dual channel mode is active.  Else return 0. */
dual_channel_active(u32 drc,int dev_idx)34113189525SDoug Thompson static inline int dual_channel_active(u32 drc, int dev_idx)
342806c35f5SAlan Cox {
34313189525SDoug Thompson 	return (dev_idx == E7501) ? ((drc >> 22) & 0x1) : 1;
34413189525SDoug Thompson }
345806c35f5SAlan Cox 
34613189525SDoug Thompson /* Return DRB granularity (0=32mb, 1=64mb). */
drb_granularity(u32 drc,int dev_idx)34713189525SDoug Thompson static inline int drb_granularity(u32 drc, int dev_idx)
34813189525SDoug Thompson {
349806c35f5SAlan Cox 	/* only e7501 can be single channel */
35013189525SDoug Thompson 	return (dev_idx == E7501) ? ((drc >> 18) & 0x3) : 1;
351806c35f5SAlan Cox }
352806c35f5SAlan Cox 
e7xxx_init_csrows(struct mem_ctl_info * mci,struct pci_dev * pdev,int dev_idx,u32 drc)35313189525SDoug Thompson static void e7xxx_init_csrows(struct mem_ctl_info *mci, struct pci_dev *pdev,
35413189525SDoug Thompson 			int dev_idx, u32 drc)
35513189525SDoug Thompson {
35613189525SDoug Thompson 	unsigned long last_cumul_size;
357084a4fccSMauro Carvalho Chehab 	int index, j;
35813189525SDoug Thompson 	u8 value;
359a895bf8bSMauro Carvalho Chehab 	u32 dra, cumul_size, nr_pages;
36013189525SDoug Thompson 	int drc_chan, drc_drbg, drc_ddim, mem_dev;
36113189525SDoug Thompson 	struct csrow_info *csrow;
362084a4fccSMauro Carvalho Chehab 	struct dimm_info *dimm;
363fd63312dSMauro Carvalho Chehab 	enum edac_type edac_mode;
364806c35f5SAlan Cox 
365806c35f5SAlan Cox 	pci_read_config_dword(pdev, E7XXX_DRA, &dra);
36613189525SDoug Thompson 	drc_chan = dual_channel_active(drc, dev_idx);
36713189525SDoug Thompson 	drc_drbg = drb_granularity(drc, dev_idx);
36813189525SDoug Thompson 	drc_ddim = (drc >> 20) & 0x3;
36913189525SDoug Thompson 	last_cumul_size = 0;
370806c35f5SAlan Cox 
37113189525SDoug Thompson 	/* The dram row boundary (DRB) reg values are boundary address
372806c35f5SAlan Cox 	 * for each DRAM row with a granularity of 32 or 64MB (single/dual
373806c35f5SAlan Cox 	 * channel operation).  DRB regs are cumulative; therefore DRB7 will
374806c35f5SAlan Cox 	 * contain the total memory contained in all eight rows.
375806c35f5SAlan Cox 	 */
37613189525SDoug Thompson 	for (index = 0; index < mci->nr_csrows; index++) {
377806c35f5SAlan Cox 		/* mem_dev 0=x8, 1=x4 */
37813189525SDoug Thompson 		mem_dev = (dra >> (index * 4 + 3)) & 0x1;
379de3910ebSMauro Carvalho Chehab 		csrow = mci->csrows[index];
380806c35f5SAlan Cox 
38137f04581SDoug Thompson 		pci_read_config_byte(pdev, E7XXX_DRB + index, &value);
382806c35f5SAlan Cox 		/* convert a 64 or 32 MiB DRB to a page size. */
383806c35f5SAlan Cox 		cumul_size = value << (25 + drc_drbg - PAGE_SHIFT);
384956b9ba1SJoe Perches 		edac_dbg(3, "(%d) cumul_size 0x%x\n", index, cumul_size);
385806c35f5SAlan Cox 		if (cumul_size == last_cumul_size)
386806c35f5SAlan Cox 			continue;	/* not populated */
387806c35f5SAlan Cox 
388806c35f5SAlan Cox 		csrow->first_page = last_cumul_size;
389806c35f5SAlan Cox 		csrow->last_page = cumul_size - 1;
390a895bf8bSMauro Carvalho Chehab 		nr_pages = cumul_size - last_cumul_size;
391806c35f5SAlan Cox 		last_cumul_size = cumul_size;
392084a4fccSMauro Carvalho Chehab 
393fd63312dSMauro Carvalho Chehab 		/*
394fd63312dSMauro Carvalho Chehab 		* if single channel or x8 devices then SECDED
395fd63312dSMauro Carvalho Chehab 		* if dual channel and x4 then S4ECD4ED
396fd63312dSMauro Carvalho Chehab 		*/
397fd63312dSMauro Carvalho Chehab 		if (drc_ddim) {
398fd63312dSMauro Carvalho Chehab 			if (drc_chan && mem_dev) {
399fd63312dSMauro Carvalho Chehab 				edac_mode = EDAC_S4ECD4ED;
400fd63312dSMauro Carvalho Chehab 				mci->edac_cap |= EDAC_FLAG_S4ECD4ED;
401fd63312dSMauro Carvalho Chehab 			} else {
402fd63312dSMauro Carvalho Chehab 				edac_mode = EDAC_SECDED;
403fd63312dSMauro Carvalho Chehab 				mci->edac_cap |= EDAC_FLAG_SECDED;
404fd63312dSMauro Carvalho Chehab 			}
405fd63312dSMauro Carvalho Chehab 		} else
406fd63312dSMauro Carvalho Chehab 			edac_mode = EDAC_NONE;
407fd63312dSMauro Carvalho Chehab 
408084a4fccSMauro Carvalho Chehab 		for (j = 0; j < drc_chan + 1; j++) {
409de3910ebSMauro Carvalho Chehab 			dimm = csrow->channels[j]->dimm;
410084a4fccSMauro Carvalho Chehab 
411a895bf8bSMauro Carvalho Chehab 			dimm->nr_pages = nr_pages / (drc_chan + 1);
412084a4fccSMauro Carvalho Chehab 			dimm->grain = 1 << 12;	/* 4KiB - resolution of CELOG */
413084a4fccSMauro Carvalho Chehab 			dimm->mtype = MEM_RDDR;	/* only one type supported */
414084a4fccSMauro Carvalho Chehab 			dimm->dtype = mem_dev ? DEV_X4 : DEV_X8;
415fd63312dSMauro Carvalho Chehab 			dimm->edac_mode = edac_mode;
416084a4fccSMauro Carvalho Chehab 		}
417806c35f5SAlan Cox 	}
41813189525SDoug Thompson }
419806c35f5SAlan Cox 
e7xxx_probe1(struct pci_dev * pdev,int dev_idx)42013189525SDoug Thompson static int e7xxx_probe1(struct pci_dev *pdev, int dev_idx)
42113189525SDoug Thompson {
42213189525SDoug Thompson 	u16 pci_data;
42313189525SDoug Thompson 	struct mem_ctl_info *mci = NULL;
42430ac4406SMauro Carvalho Chehab 	struct edac_mc_layer layers[2];
42513189525SDoug Thompson 	struct e7xxx_pvt *pvt = NULL;
42613189525SDoug Thompson 	u32 drc;
42713189525SDoug Thompson 	int drc_chan;
42813189525SDoug Thompson 	struct e7xxx_error_info discard;
42913189525SDoug Thompson 
430956b9ba1SJoe Perches 	edac_dbg(0, "mci\n");
431c0d12172SDave Jiang 
43213189525SDoug Thompson 	pci_read_config_dword(pdev, E7XXX_DRC, &drc);
43313189525SDoug Thompson 
43413189525SDoug Thompson 	drc_chan = dual_channel_active(drc, dev_idx);
43530ac4406SMauro Carvalho Chehab 	/*
43630ac4406SMauro Carvalho Chehab 	 * According with the datasheet, this device has a maximum of
43730ac4406SMauro Carvalho Chehab 	 * 4 DIMMS per channel, either single-rank or dual-rank. So, the
43830ac4406SMauro Carvalho Chehab 	 * total amount of dimms is 8 (E7XXX_NR_DIMMS).
43930ac4406SMauro Carvalho Chehab 	 * That means that the DIMM is mapped as CSROWs, and the channel
44030ac4406SMauro Carvalho Chehab 	 * will map the rank. So, an error to either channel should be
44130ac4406SMauro Carvalho Chehab 	 * attributed to the same dimm.
44230ac4406SMauro Carvalho Chehab 	 */
44330ac4406SMauro Carvalho Chehab 	layers[0].type = EDAC_MC_LAYER_CHIP_SELECT;
44430ac4406SMauro Carvalho Chehab 	layers[0].size = E7XXX_NR_CSROWS;
44530ac4406SMauro Carvalho Chehab 	layers[0].is_virt_csrow = true;
44630ac4406SMauro Carvalho Chehab 	layers[1].type = EDAC_MC_LAYER_CHANNEL;
44730ac4406SMauro Carvalho Chehab 	layers[1].size = drc_chan + 1;
44830ac4406SMauro Carvalho Chehab 	layers[1].is_virt_csrow = false;
449ca0907b9SMauro Carvalho Chehab 	mci = edac_mc_alloc(0, ARRAY_SIZE(layers), layers, sizeof(*pvt));
45013189525SDoug Thompson 	if (mci == NULL)
45113189525SDoug Thompson 		return -ENOMEM;
45213189525SDoug Thompson 
453956b9ba1SJoe Perches 	edac_dbg(3, "init mci\n");
45413189525SDoug Thompson 	mci->mtype_cap = MEM_FLAG_RDDR;
45513189525SDoug Thompson 	mci->edac_ctl_cap = EDAC_FLAG_NONE | EDAC_FLAG_SECDED |
45613189525SDoug Thompson 		EDAC_FLAG_S4ECD4ED;
45713189525SDoug Thompson 	/* FIXME - what if different memory types are in different csrows? */
45813189525SDoug Thompson 	mci->mod_name = EDAC_MOD_STR;
459fd687502SMauro Carvalho Chehab 	mci->pdev = &pdev->dev;
460956b9ba1SJoe Perches 	edac_dbg(3, "init pvt\n");
46113189525SDoug Thompson 	pvt = (struct e7xxx_pvt *)mci->pvt_info;
46213189525SDoug Thompson 	pvt->dev_info = &e7xxx_devs[dev_idx];
46313189525SDoug Thompson 	pvt->bridge_ck = pci_get_device(PCI_VENDOR_ID_INTEL,
464849a4c37SDave Jiang 					pvt->dev_info->err_dev, pvt->bridge_ck);
46513189525SDoug Thompson 
46613189525SDoug Thompson 	if (!pvt->bridge_ck) {
46713189525SDoug Thompson 		e7xxx_printk(KERN_ERR, "error reporting device not found:"
46813189525SDoug Thompson 			"vendor %x device 0x%x (broken BIOS?)\n",
46913189525SDoug Thompson 			PCI_VENDOR_ID_INTEL, e7xxx_devs[dev_idx].err_dev);
47013189525SDoug Thompson 		goto fail0;
47113189525SDoug Thompson 	}
47213189525SDoug Thompson 
473956b9ba1SJoe Perches 	edac_dbg(3, "more mci init\n");
47413189525SDoug Thompson 	mci->ctl_name = pvt->dev_info->ctl_name;
475c4192705SDave Jiang 	mci->dev_name = pci_name(pdev);
47613189525SDoug Thompson 	mci->edac_check = e7xxx_check;
47713189525SDoug Thompson 	mci->ctl_page_to_phys = ctl_page_to_phys;
47813189525SDoug Thompson 	e7xxx_init_csrows(mci, pdev, dev_idx, drc);
479806c35f5SAlan Cox 	mci->edac_cap |= EDAC_FLAG_NONE;
480956b9ba1SJoe Perches 	edac_dbg(3, "tolm, remapbase, remaplimit\n");
481806c35f5SAlan Cox 	/* load the top of low memory, remap base, and remap limit vars */
48237f04581SDoug Thompson 	pci_read_config_word(pdev, E7XXX_TOLM, &pci_data);
483806c35f5SAlan Cox 	pvt->tolm = ((u32) pci_data) << 4;
48437f04581SDoug Thompson 	pci_read_config_word(pdev, E7XXX_REMAPBASE, &pci_data);
485806c35f5SAlan Cox 	pvt->remapbase = ((u32) pci_data) << 14;
48637f04581SDoug Thompson 	pci_read_config_word(pdev, E7XXX_REMAPLIMIT, &pci_data);
487806c35f5SAlan Cox 	pvt->remaplimit = ((u32) pci_data) << 14;
488537fba28SDave Peterson 	e7xxx_printk(KERN_INFO,
489e7ecd891SDave Peterson 		"tolm = %x, remapbase = %x, remaplimit = %x\n", pvt->tolm,
490e7ecd891SDave Peterson 		pvt->remapbase, pvt->remaplimit);
491806c35f5SAlan Cox 
492806c35f5SAlan Cox 	/* clear any pending errors, or initial state bits */
493749ede57SDave Peterson 	e7xxx_get_error_info(mci, &discard);
494806c35f5SAlan Cox 
4952d7bbb91SDoug Thompson 	/* Here we assume that we will never see multiple instances of this
4962d7bbb91SDoug Thompson 	 * type of memory controller.  The ID is therefore hardcoded to 0.
4972d7bbb91SDoug Thompson 	 */
498b8f6f975SDoug Thompson 	if (edac_mc_add_mc(mci)) {
499956b9ba1SJoe Perches 		edac_dbg(3, "failed edac_mc_add_mc()\n");
50013189525SDoug Thompson 		goto fail1;
501806c35f5SAlan Cox 	}
502806c35f5SAlan Cox 
503456a2f95SDave Jiang 	/* allocating generic PCI control info */
504456a2f95SDave Jiang 	e7xxx_pci = edac_pci_create_generic_ctl(&pdev->dev, EDAC_MOD_STR);
505456a2f95SDave Jiang 	if (!e7xxx_pci) {
506456a2f95SDave Jiang 		printk(KERN_WARNING
507456a2f95SDave Jiang 			"%s(): Unable to create PCI control\n",
508456a2f95SDave Jiang 			__func__);
509456a2f95SDave Jiang 		printk(KERN_WARNING
510456a2f95SDave Jiang 			"%s(): PCI error report via EDAC not setup\n",
511456a2f95SDave Jiang 			__func__);
512456a2f95SDave Jiang 	}
513456a2f95SDave Jiang 
514806c35f5SAlan Cox 	/* get this far and it's successful */
515956b9ba1SJoe Perches 	edac_dbg(3, "success\n");
516806c35f5SAlan Cox 	return 0;
517806c35f5SAlan Cox 
51813189525SDoug Thompson fail1:
519806c35f5SAlan Cox 	pci_dev_put(pvt->bridge_ck);
520806c35f5SAlan Cox 
52113189525SDoug Thompson fail0:
52213189525SDoug Thompson 	edac_mc_free(mci);
52313189525SDoug Thompson 
52413189525SDoug Thompson 	return -ENODEV;
525806c35f5SAlan Cox }
526806c35f5SAlan Cox 
527806c35f5SAlan Cox /* returns count (>= 0), or negative on error */
e7xxx_init_one(struct pci_dev * pdev,const struct pci_device_id * ent)5289b3c6e85SGreg Kroah-Hartman static int e7xxx_init_one(struct pci_dev *pdev, const struct pci_device_id *ent)
529806c35f5SAlan Cox {
530956b9ba1SJoe Perches 	edac_dbg(0, "\n");
531806c35f5SAlan Cox 
532806c35f5SAlan Cox 	/* wake up and enable device */
533806c35f5SAlan Cox 	return pci_enable_device(pdev) ?
534806c35f5SAlan Cox 		-EIO : e7xxx_probe1(pdev, ent->driver_data);
535806c35f5SAlan Cox }
536806c35f5SAlan Cox 
e7xxx_remove_one(struct pci_dev * pdev)5379b3c6e85SGreg Kroah-Hartman static void e7xxx_remove_one(struct pci_dev *pdev)
538806c35f5SAlan Cox {
539806c35f5SAlan Cox 	struct mem_ctl_info *mci;
540806c35f5SAlan Cox 	struct e7xxx_pvt *pvt;
541806c35f5SAlan Cox 
542956b9ba1SJoe Perches 	edac_dbg(0, "\n");
543806c35f5SAlan Cox 
544456a2f95SDave Jiang 	if (e7xxx_pci)
545456a2f95SDave Jiang 		edac_pci_release_generic_ctl(e7xxx_pci);
546456a2f95SDave Jiang 
54737f04581SDoug Thompson 	if ((mci = edac_mc_del_mc(&pdev->dev)) == NULL)
54818dbc337SDave Peterson 		return;
54918dbc337SDave Peterson 
550806c35f5SAlan Cox 	pvt = (struct e7xxx_pvt *)mci->pvt_info;
551806c35f5SAlan Cox 	pci_dev_put(pvt->bridge_ck);
552806c35f5SAlan Cox 	edac_mc_free(mci);
553806c35f5SAlan Cox }
554806c35f5SAlan Cox 
555ba935f40SJingoo Han static const struct pci_device_id e7xxx_pci_tbl[] = {
556e7ecd891SDave Peterson 	{
557e7ecd891SDave Peterson 	 PCI_VEND_DEV(INTEL, 7205_0), PCI_ANY_ID, PCI_ANY_ID, 0, 0,
558849a4c37SDave Jiang 	 E7205},
559e7ecd891SDave Peterson 	{
560e7ecd891SDave Peterson 	 PCI_VEND_DEV(INTEL, 7500_0), PCI_ANY_ID, PCI_ANY_ID, 0, 0,
561849a4c37SDave Jiang 	 E7500},
562e7ecd891SDave Peterson 	{
563e7ecd891SDave Peterson 	 PCI_VEND_DEV(INTEL, 7501_0), PCI_ANY_ID, PCI_ANY_ID, 0, 0,
564849a4c37SDave Jiang 	 E7501},
565e7ecd891SDave Peterson 	{
566e7ecd891SDave Peterson 	 PCI_VEND_DEV(INTEL, 7505_0), PCI_ANY_ID, PCI_ANY_ID, 0, 0,
567849a4c37SDave Jiang 	 E7505},
568e7ecd891SDave Peterson 	{
569e7ecd891SDave Peterson 	 0,
570e7ecd891SDave Peterson 	 }			/* 0 terminated list. */
571806c35f5SAlan Cox };
572806c35f5SAlan Cox 
573806c35f5SAlan Cox MODULE_DEVICE_TABLE(pci, e7xxx_pci_tbl);
574806c35f5SAlan Cox 
575806c35f5SAlan Cox static struct pci_driver e7xxx_driver = {
576680cbbbbSDave Peterson 	.name = EDAC_MOD_STR,
577806c35f5SAlan Cox 	.probe = e7xxx_init_one,
5789b3c6e85SGreg Kroah-Hartman 	.remove = e7xxx_remove_one,
579806c35f5SAlan Cox 	.id_table = e7xxx_pci_tbl,
580806c35f5SAlan Cox };
581806c35f5SAlan Cox 
e7xxx_init(void)582da9bb1d2SAlan Cox static int __init e7xxx_init(void)
583806c35f5SAlan Cox {
584c3c52bceSHitoshi Mitake        /* Ensure that the OPSTATE is set correctly for POLL or NMI */
585c3c52bceSHitoshi Mitake        opstate_init();
586c3c52bceSHitoshi Mitake 
587806c35f5SAlan Cox 	return pci_register_driver(&e7xxx_driver);
588806c35f5SAlan Cox }
589806c35f5SAlan Cox 
e7xxx_exit(void)590806c35f5SAlan Cox static void __exit e7xxx_exit(void)
591806c35f5SAlan Cox {
592806c35f5SAlan Cox 	pci_unregister_driver(&e7xxx_driver);
593806c35f5SAlan Cox }
594806c35f5SAlan Cox 
595806c35f5SAlan Cox module_init(e7xxx_init);
596806c35f5SAlan Cox module_exit(e7xxx_exit);
597806c35f5SAlan Cox 
598806c35f5SAlan Cox MODULE_LICENSE("GPL");
599*371b27f2SBorislav Petkov (AMD) MODULE_AUTHOR("Linux Networx (http://lnxi.com) Thayne Harbaugh et al");
600806c35f5SAlan Cox MODULE_DESCRIPTION("MC support for Intel e7xxx memory controllers");
601c0d12172SDave Jiang module_param(edac_op_state, int, 0444);
602c0d12172SDave Jiang MODULE_PARM_DESC(edac_op_state, "EDAC Error Reporting state: 0=Poll,1=NMI");
603