xref: /openbmc/linux/drivers/edac/e7xxx_edac.c (revision b8f6f9755248026f21282e25cac49a1af698056c)
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  *
13806c35f5SAlan Cox  * Contributors:
14806c35f5SAlan Cox  *	Eric Biederman (Linux Networx)
15806c35f5SAlan Cox  *	Tom Zimmerman (Linux Networx)
16806c35f5SAlan Cox  *	Jim Garlick (Lawrence Livermore National Labs)
17806c35f5SAlan Cox  *	Dave Peterson (Lawrence Livermore National Labs)
18806c35f5SAlan Cox  *	That One Guy (Some other place)
19806c35f5SAlan Cox  *	Wang Zhenyu (intel.com)
20806c35f5SAlan Cox  *
21806c35f5SAlan Cox  * $Id: edac_e7xxx.c,v 1.5.2.9 2005/10/05 00:43:44 dsp_llnl Exp $
22806c35f5SAlan Cox  *
23806c35f5SAlan Cox  */
24806c35f5SAlan Cox 
25806c35f5SAlan Cox #include <linux/module.h>
26806c35f5SAlan Cox #include <linux/init.h>
27806c35f5SAlan Cox #include <linux/pci.h>
28806c35f5SAlan Cox #include <linux/pci_ids.h>
29806c35f5SAlan Cox #include <linux/slab.h>
30c0d12172SDave Jiang #include <linux/edac.h>
3120bcb7a8SDouglas Thompson #include "edac_core.h"
32806c35f5SAlan Cox 
3320bcb7a8SDouglas Thompson #define	E7XXX_REVISION " Ver: 2.0.2 " __DATE__
34929a40ecSDoug Thompson #define	EDAC_MOD_STR	"e7xxx_edac"
3537f04581SDoug Thompson 
36537fba28SDave Peterson #define e7xxx_printk(level, fmt, arg...) \
37537fba28SDave Peterson 	edac_printk(level, "e7xxx", fmt, ##arg)
38537fba28SDave Peterson 
39537fba28SDave Peterson #define e7xxx_mc_printk(mci, level, fmt, arg...) \
40537fba28SDave Peterson 	edac_mc_chipset_printk(mci, level, "e7xxx", fmt, ##arg)
41537fba28SDave Peterson 
42806c35f5SAlan Cox #ifndef PCI_DEVICE_ID_INTEL_7205_0
43806c35f5SAlan Cox #define PCI_DEVICE_ID_INTEL_7205_0	0x255d
44806c35f5SAlan Cox #endif				/* PCI_DEVICE_ID_INTEL_7205_0 */
45806c35f5SAlan Cox 
46806c35f5SAlan Cox #ifndef PCI_DEVICE_ID_INTEL_7205_1_ERR
47806c35f5SAlan Cox #define PCI_DEVICE_ID_INTEL_7205_1_ERR	0x2551
48806c35f5SAlan Cox #endif				/* PCI_DEVICE_ID_INTEL_7205_1_ERR */
49806c35f5SAlan Cox 
50806c35f5SAlan Cox #ifndef PCI_DEVICE_ID_INTEL_7500_0
51806c35f5SAlan Cox #define PCI_DEVICE_ID_INTEL_7500_0	0x2540
52806c35f5SAlan Cox #endif				/* PCI_DEVICE_ID_INTEL_7500_0 */
53806c35f5SAlan Cox 
54806c35f5SAlan Cox #ifndef PCI_DEVICE_ID_INTEL_7500_1_ERR
55806c35f5SAlan Cox #define PCI_DEVICE_ID_INTEL_7500_1_ERR	0x2541
56806c35f5SAlan Cox #endif				/* PCI_DEVICE_ID_INTEL_7500_1_ERR */
57806c35f5SAlan Cox 
58806c35f5SAlan Cox #ifndef PCI_DEVICE_ID_INTEL_7501_0
59806c35f5SAlan Cox #define PCI_DEVICE_ID_INTEL_7501_0	0x254c
60806c35f5SAlan Cox #endif				/* PCI_DEVICE_ID_INTEL_7501_0 */
61806c35f5SAlan Cox 
62806c35f5SAlan Cox #ifndef PCI_DEVICE_ID_INTEL_7501_1_ERR
63806c35f5SAlan Cox #define PCI_DEVICE_ID_INTEL_7501_1_ERR	0x2541
64806c35f5SAlan Cox #endif				/* PCI_DEVICE_ID_INTEL_7501_1_ERR */
65806c35f5SAlan Cox 
66806c35f5SAlan Cox #ifndef PCI_DEVICE_ID_INTEL_7505_0
67806c35f5SAlan Cox #define PCI_DEVICE_ID_INTEL_7505_0	0x2550
68806c35f5SAlan Cox #endif				/* PCI_DEVICE_ID_INTEL_7505_0 */
69806c35f5SAlan Cox 
70806c35f5SAlan Cox #ifndef PCI_DEVICE_ID_INTEL_7505_1_ERR
71806c35f5SAlan Cox #define PCI_DEVICE_ID_INTEL_7505_1_ERR	0x2551
72806c35f5SAlan Cox #endif				/* PCI_DEVICE_ID_INTEL_7505_1_ERR */
73806c35f5SAlan Cox 
74806c35f5SAlan Cox #define E7XXX_NR_CSROWS		8	/* number of csrows */
75806c35f5SAlan Cox #define E7XXX_NR_DIMMS		8	/* FIXME - is this correct? */
76806c35f5SAlan Cox 
77806c35f5SAlan Cox /* E7XXX register addresses - device 0 function 0 */
78806c35f5SAlan Cox #define E7XXX_DRB		0x60	/* DRAM row boundary register (8b) */
79806c35f5SAlan Cox #define E7XXX_DRA		0x70	/* DRAM row attribute register (8b) */
80806c35f5SAlan Cox 					/*
81806c35f5SAlan Cox 					 * 31   Device width row 7 0=x8 1=x4
82806c35f5SAlan Cox 					 * 27   Device width row 6
83806c35f5SAlan Cox 					 * 23   Device width row 5
84806c35f5SAlan Cox 					 * 19   Device width row 4
85806c35f5SAlan Cox 					 * 15   Device width row 3
86806c35f5SAlan Cox 					 * 11   Device width row 2
87806c35f5SAlan Cox 					 *  7   Device width row 1
88806c35f5SAlan Cox 					 *  3   Device width row 0
89806c35f5SAlan Cox 					 */
90806c35f5SAlan Cox #define E7XXX_DRC		0x7C	/* DRAM controller mode reg (32b) */
91806c35f5SAlan Cox 					/*
92806c35f5SAlan Cox 					 * 22    Number channels 0=1,1=2
93806c35f5SAlan Cox 					 * 19:18 DRB Granularity 32/64MB
94806c35f5SAlan Cox 					 */
95806c35f5SAlan Cox #define E7XXX_TOLM		0xC4	/* DRAM top of low memory reg (16b) */
96806c35f5SAlan Cox #define E7XXX_REMAPBASE		0xC6	/* DRAM remap base address reg (16b) */
97806c35f5SAlan Cox #define E7XXX_REMAPLIMIT	0xC8	/* DRAM remap limit address reg (16b) */
98806c35f5SAlan Cox 
99806c35f5SAlan Cox /* E7XXX register addresses - device 0 function 1 */
100806c35f5SAlan Cox #define E7XXX_DRAM_FERR		0x80	/* DRAM first error register (8b) */
101806c35f5SAlan Cox #define E7XXX_DRAM_NERR		0x82	/* DRAM next error register (8b) */
102806c35f5SAlan Cox #define E7XXX_DRAM_CELOG_ADD	0xA0	/* DRAM first correctable memory */
103806c35f5SAlan Cox 					/*     error address register (32b) */
104806c35f5SAlan Cox 					/*
105806c35f5SAlan Cox 					 * 31:28 Reserved
106806c35f5SAlan Cox 					 * 27:6  CE address (4k block 33:12)
107806c35f5SAlan Cox 					 *  5:0  Reserved
108806c35f5SAlan Cox 					 */
109806c35f5SAlan Cox #define E7XXX_DRAM_UELOG_ADD	0xB0	/* DRAM first uncorrectable memory */
110806c35f5SAlan Cox 					/*     error address register (32b) */
111806c35f5SAlan Cox 					/*
112806c35f5SAlan Cox 					 * 31:28 Reserved
113806c35f5SAlan Cox 					 * 27:6  CE address (4k block 33:12)
114806c35f5SAlan Cox 					 *  5:0  Reserved
115806c35f5SAlan Cox 					 */
116806c35f5SAlan Cox #define E7XXX_DRAM_CELOG_SYNDROME 0xD0	/* DRAM first correctable memory */
117806c35f5SAlan Cox 					/*     error syndrome register (16b) */
118806c35f5SAlan Cox 
119806c35f5SAlan Cox enum e7xxx_chips {
120806c35f5SAlan Cox 	E7500 = 0,
121806c35f5SAlan Cox 	E7501,
122806c35f5SAlan Cox 	E7505,
123806c35f5SAlan Cox 	E7205,
124806c35f5SAlan Cox };
125806c35f5SAlan Cox 
126806c35f5SAlan Cox struct e7xxx_pvt {
127806c35f5SAlan Cox 	struct pci_dev *bridge_ck;
128806c35f5SAlan Cox 	u32 tolm;
129806c35f5SAlan Cox 	u32 remapbase;
130806c35f5SAlan Cox 	u32 remaplimit;
131806c35f5SAlan Cox 	const struct e7xxx_dev_info *dev_info;
132806c35f5SAlan Cox };
133806c35f5SAlan Cox 
134806c35f5SAlan Cox struct e7xxx_dev_info {
135806c35f5SAlan Cox 	u16 err_dev;
136806c35f5SAlan Cox 	const char *ctl_name;
137806c35f5SAlan Cox };
138806c35f5SAlan Cox 
139806c35f5SAlan Cox struct e7xxx_error_info {
140806c35f5SAlan Cox 	u8 dram_ferr;
141806c35f5SAlan Cox 	u8 dram_nerr;
142806c35f5SAlan Cox 	u32 dram_celog_add;
143806c35f5SAlan Cox 	u16 dram_celog_syndrome;
144806c35f5SAlan Cox 	u32 dram_uelog_add;
145806c35f5SAlan Cox };
146806c35f5SAlan Cox 
147456a2f95SDave Jiang static struct edac_pci_ctl_info *e7xxx_pci;
148456a2f95SDave Jiang 
149806c35f5SAlan Cox static const struct e7xxx_dev_info e7xxx_devs[] = {
150806c35f5SAlan Cox 	[E7500] = {
151806c35f5SAlan Cox 		.err_dev = PCI_DEVICE_ID_INTEL_7500_1_ERR,
152849a4c37SDave Jiang 		.ctl_name = "E7500"},
153806c35f5SAlan Cox 	[E7501] = {
154806c35f5SAlan Cox 		.err_dev = PCI_DEVICE_ID_INTEL_7501_1_ERR,
155849a4c37SDave Jiang 		.ctl_name = "E7501"},
156806c35f5SAlan Cox 	[E7505] = {
157806c35f5SAlan Cox 		.err_dev = PCI_DEVICE_ID_INTEL_7505_1_ERR,
158849a4c37SDave Jiang 		.ctl_name = "E7505"},
159806c35f5SAlan Cox 	[E7205] = {
160806c35f5SAlan Cox 		.err_dev = PCI_DEVICE_ID_INTEL_7205_1_ERR,
161849a4c37SDave Jiang 		.ctl_name = "E7205"},
162806c35f5SAlan Cox };
163806c35f5SAlan Cox 
164806c35f5SAlan Cox /* FIXME - is this valid for both SECDED and S4ECD4ED? */
165806c35f5SAlan Cox static inline int e7xxx_find_channel(u16 syndrome)
166806c35f5SAlan Cox {
167537fba28SDave Peterson 	debugf3("%s()\n", __func__);
168806c35f5SAlan Cox 
169806c35f5SAlan Cox 	if ((syndrome & 0xff00) == 0)
170806c35f5SAlan Cox 		return 0;
171e7ecd891SDave Peterson 
172806c35f5SAlan Cox 	if ((syndrome & 0x00ff) == 0)
173806c35f5SAlan Cox 		return 1;
174e7ecd891SDave Peterson 
175806c35f5SAlan Cox 	if ((syndrome & 0xf000) == 0 || (syndrome & 0x0f00) == 0)
176806c35f5SAlan Cox 		return 0;
177e7ecd891SDave Peterson 
178806c35f5SAlan Cox 	return 1;
179806c35f5SAlan Cox }
180806c35f5SAlan Cox 
181e7ecd891SDave Peterson static unsigned long ctl_page_to_phys(struct mem_ctl_info *mci,
182e7ecd891SDave Peterson 				unsigned long page)
183806c35f5SAlan Cox {
184806c35f5SAlan Cox 	u32 remap;
185806c35f5SAlan Cox 	struct e7xxx_pvt *pvt = (struct e7xxx_pvt *)mci->pvt_info;
186806c35f5SAlan Cox 
187537fba28SDave Peterson 	debugf3("%s()\n", __func__);
188806c35f5SAlan Cox 
189806c35f5SAlan Cox 	if ((page < pvt->tolm) ||
190806c35f5SAlan Cox 		((page >= 0x100000) && (page < pvt->remapbase)))
191806c35f5SAlan Cox 		return page;
192e7ecd891SDave Peterson 
193806c35f5SAlan Cox 	remap = (page - pvt->tolm) + pvt->remapbase;
194e7ecd891SDave Peterson 
195806c35f5SAlan Cox 	if (remap < pvt->remaplimit)
196806c35f5SAlan Cox 		return remap;
197e7ecd891SDave Peterson 
198537fba28SDave Peterson 	e7xxx_printk(KERN_ERR, "Invalid page %lx - out of range\n", page);
199806c35f5SAlan Cox 	return pvt->tolm - 1;
200806c35f5SAlan Cox }
201806c35f5SAlan Cox 
202849a4c37SDave Jiang static void process_ce(struct mem_ctl_info *mci, struct e7xxx_error_info *info)
203806c35f5SAlan Cox {
204806c35f5SAlan Cox 	u32 error_1b, page;
205806c35f5SAlan Cox 	u16 syndrome;
206806c35f5SAlan Cox 	int row;
207806c35f5SAlan Cox 	int channel;
208806c35f5SAlan Cox 
209537fba28SDave Peterson 	debugf3("%s()\n", __func__);
210806c35f5SAlan Cox 	/* read the error address */
211806c35f5SAlan Cox 	error_1b = info->dram_celog_add;
212806c35f5SAlan Cox 	/* FIXME - should use PAGE_SHIFT */
213806c35f5SAlan Cox 	page = error_1b >> 6;	/* convert the address to 4k page */
214806c35f5SAlan Cox 	/* read the syndrome */
215806c35f5SAlan Cox 	syndrome = info->dram_celog_syndrome;
216806c35f5SAlan Cox 	/* FIXME - check for -1 */
217806c35f5SAlan Cox 	row = edac_mc_find_csrow_by_page(mci, page);
218806c35f5SAlan Cox 	/* convert syndrome to channel */
219806c35f5SAlan Cox 	channel = e7xxx_find_channel(syndrome);
220e7ecd891SDave Peterson 	edac_mc_handle_ce(mci, page, 0, syndrome, row, channel, "e7xxx CE");
221806c35f5SAlan Cox }
222806c35f5SAlan Cox 
223806c35f5SAlan Cox static void process_ce_no_info(struct mem_ctl_info *mci)
224806c35f5SAlan Cox {
225537fba28SDave Peterson 	debugf3("%s()\n", __func__);
226806c35f5SAlan Cox 	edac_mc_handle_ce_no_info(mci, "e7xxx CE log register overflow");
227806c35f5SAlan Cox }
228806c35f5SAlan Cox 
229849a4c37SDave Jiang static void process_ue(struct mem_ctl_info *mci, struct e7xxx_error_info *info)
230806c35f5SAlan Cox {
231806c35f5SAlan Cox 	u32 error_2b, block_page;
232806c35f5SAlan Cox 	int row;
233806c35f5SAlan Cox 
234537fba28SDave Peterson 	debugf3("%s()\n", __func__);
235806c35f5SAlan Cox 	/* read the error address */
236806c35f5SAlan Cox 	error_2b = info->dram_uelog_add;
237806c35f5SAlan Cox 	/* FIXME - should use PAGE_SHIFT */
238806c35f5SAlan Cox 	block_page = error_2b >> 6;	/* convert to 4k address */
239806c35f5SAlan Cox 	row = edac_mc_find_csrow_by_page(mci, block_page);
240806c35f5SAlan Cox 	edac_mc_handle_ue(mci, block_page, 0, row, "e7xxx UE");
241806c35f5SAlan Cox }
242806c35f5SAlan Cox 
243806c35f5SAlan Cox static void process_ue_no_info(struct mem_ctl_info *mci)
244806c35f5SAlan Cox {
245537fba28SDave Peterson 	debugf3("%s()\n", __func__);
246806c35f5SAlan Cox 	edac_mc_handle_ue_no_info(mci, "e7xxx UE log register overflow");
247806c35f5SAlan Cox }
248806c35f5SAlan Cox 
249806c35f5SAlan Cox static void e7xxx_get_error_info(struct mem_ctl_info *mci,
250806c35f5SAlan Cox 				 struct e7xxx_error_info *info)
251806c35f5SAlan Cox {
252806c35f5SAlan Cox 	struct e7xxx_pvt *pvt;
253806c35f5SAlan Cox 
254806c35f5SAlan Cox 	pvt = (struct e7xxx_pvt *)mci->pvt_info;
255849a4c37SDave Jiang 	pci_read_config_byte(pvt->bridge_ck, E7XXX_DRAM_FERR, &info->dram_ferr);
256849a4c37SDave Jiang 	pci_read_config_byte(pvt->bridge_ck, E7XXX_DRAM_NERR, &info->dram_nerr);
257806c35f5SAlan Cox 
258806c35f5SAlan Cox 	if ((info->dram_ferr & 1) || (info->dram_nerr & 1)) {
259806c35f5SAlan Cox 		pci_read_config_dword(pvt->bridge_ck, E7XXX_DRAM_CELOG_ADD,
260806c35f5SAlan Cox 				&info->dram_celog_add);
261806c35f5SAlan Cox 		pci_read_config_word(pvt->bridge_ck,
262e7ecd891SDave Peterson 				E7XXX_DRAM_CELOG_SYNDROME,
263e7ecd891SDave Peterson 				&info->dram_celog_syndrome);
264806c35f5SAlan Cox 	}
265806c35f5SAlan Cox 
266806c35f5SAlan Cox 	if ((info->dram_ferr & 2) || (info->dram_nerr & 2))
267806c35f5SAlan Cox 		pci_read_config_dword(pvt->bridge_ck, E7XXX_DRAM_UELOG_ADD,
268806c35f5SAlan Cox 				&info->dram_uelog_add);
269806c35f5SAlan Cox 
270806c35f5SAlan Cox 	if (info->dram_ferr & 3)
271e7ecd891SDave Peterson 		pci_write_bits8(pvt->bridge_ck, E7XXX_DRAM_FERR, 0x03, 0x03);
272806c35f5SAlan Cox 
273806c35f5SAlan Cox 	if (info->dram_nerr & 3)
274e7ecd891SDave Peterson 		pci_write_bits8(pvt->bridge_ck, E7XXX_DRAM_NERR, 0x03, 0x03);
275806c35f5SAlan Cox }
276806c35f5SAlan Cox 
277806c35f5SAlan Cox static int e7xxx_process_error_info(struct mem_ctl_info *mci,
278849a4c37SDave Jiang 				struct e7xxx_error_info *info,
279849a4c37SDave Jiang 				int handle_errors)
280806c35f5SAlan Cox {
281806c35f5SAlan Cox 	int error_found;
282806c35f5SAlan Cox 
283806c35f5SAlan Cox 	error_found = 0;
284806c35f5SAlan Cox 
285806c35f5SAlan Cox 	/* decode and report errors */
286806c35f5SAlan Cox 	if (info->dram_ferr & 1) {	/* check first error correctable */
287806c35f5SAlan Cox 		error_found = 1;
288806c35f5SAlan Cox 
289806c35f5SAlan Cox 		if (handle_errors)
290806c35f5SAlan Cox 			process_ce(mci, info);
291806c35f5SAlan Cox 	}
292806c35f5SAlan Cox 
293806c35f5SAlan Cox 	if (info->dram_ferr & 2) {	/* check first error uncorrectable */
294806c35f5SAlan Cox 		error_found = 1;
295806c35f5SAlan Cox 
296806c35f5SAlan Cox 		if (handle_errors)
297806c35f5SAlan Cox 			process_ue(mci, info);
298806c35f5SAlan Cox 	}
299806c35f5SAlan Cox 
300806c35f5SAlan Cox 	if (info->dram_nerr & 1) {	/* check next error correctable */
301806c35f5SAlan Cox 		error_found = 1;
302806c35f5SAlan Cox 
303806c35f5SAlan Cox 		if (handle_errors) {
304806c35f5SAlan Cox 			if (info->dram_ferr & 1)
305806c35f5SAlan Cox 				process_ce_no_info(mci);
306806c35f5SAlan Cox 			else
307806c35f5SAlan Cox 				process_ce(mci, info);
308806c35f5SAlan Cox 		}
309806c35f5SAlan Cox 	}
310806c35f5SAlan Cox 
311806c35f5SAlan Cox 	if (info->dram_nerr & 2) {	/* check next error uncorrectable */
312806c35f5SAlan Cox 		error_found = 1;
313806c35f5SAlan Cox 
314806c35f5SAlan Cox 		if (handle_errors) {
315806c35f5SAlan Cox 			if (info->dram_ferr & 2)
316806c35f5SAlan Cox 				process_ue_no_info(mci);
317806c35f5SAlan Cox 			else
318806c35f5SAlan Cox 				process_ue(mci, info);
319806c35f5SAlan Cox 		}
320806c35f5SAlan Cox 	}
321806c35f5SAlan Cox 
322806c35f5SAlan Cox 	return error_found;
323806c35f5SAlan Cox }
324806c35f5SAlan Cox 
325806c35f5SAlan Cox static void e7xxx_check(struct mem_ctl_info *mci)
326806c35f5SAlan Cox {
327806c35f5SAlan Cox 	struct e7xxx_error_info info;
328806c35f5SAlan Cox 
329537fba28SDave Peterson 	debugf3("%s()\n", __func__);
330806c35f5SAlan Cox 	e7xxx_get_error_info(mci, &info);
331806c35f5SAlan Cox 	e7xxx_process_error_info(mci, &info, 1);
332806c35f5SAlan Cox }
333806c35f5SAlan Cox 
33413189525SDoug Thompson /* Return 1 if dual channel mode is active.  Else return 0. */
33513189525SDoug Thompson static inline int dual_channel_active(u32 drc, int dev_idx)
336806c35f5SAlan Cox {
33713189525SDoug Thompson 	return (dev_idx == E7501) ? ((drc >> 22) & 0x1) : 1;
33813189525SDoug Thompson }
339806c35f5SAlan Cox 
34013189525SDoug Thompson /* Return DRB granularity (0=32mb, 1=64mb). */
34113189525SDoug Thompson static inline int drb_granularity(u32 drc, int dev_idx)
34213189525SDoug Thompson {
343806c35f5SAlan Cox 	/* only e7501 can be single channel */
34413189525SDoug Thompson 	return (dev_idx == E7501) ? ((drc >> 18) & 0x3) : 1;
345806c35f5SAlan Cox }
346806c35f5SAlan Cox 
34713189525SDoug Thompson static void e7xxx_init_csrows(struct mem_ctl_info *mci, struct pci_dev *pdev,
34813189525SDoug Thompson 			int dev_idx, u32 drc)
34913189525SDoug Thompson {
35013189525SDoug Thompson 	unsigned long last_cumul_size;
35113189525SDoug Thompson 	int index;
35213189525SDoug Thompson 	u8 value;
35313189525SDoug Thompson 	u32 dra, cumul_size;
35413189525SDoug Thompson 	int drc_chan, drc_drbg, drc_ddim, mem_dev;
35513189525SDoug Thompson 	struct csrow_info *csrow;
356806c35f5SAlan Cox 
357806c35f5SAlan Cox 	pci_read_config_dword(pdev, E7XXX_DRA, &dra);
35813189525SDoug Thompson 	drc_chan = dual_channel_active(drc, dev_idx);
35913189525SDoug Thompson 	drc_drbg = drb_granularity(drc, dev_idx);
36013189525SDoug Thompson 	drc_ddim = (drc >> 20) & 0x3;
36113189525SDoug Thompson 	last_cumul_size = 0;
362806c35f5SAlan Cox 
36313189525SDoug Thompson 	/* The dram row boundary (DRB) reg values are boundary address
364806c35f5SAlan Cox 	 * for each DRAM row with a granularity of 32 or 64MB (single/dual
365806c35f5SAlan Cox 	 * channel operation).  DRB regs are cumulative; therefore DRB7 will
366806c35f5SAlan Cox 	 * contain the total memory contained in all eight rows.
367806c35f5SAlan Cox 	 */
36813189525SDoug Thompson 	for (index = 0; index < mci->nr_csrows; index++) {
369806c35f5SAlan Cox 		/* mem_dev 0=x8, 1=x4 */
37013189525SDoug Thompson 		mem_dev = (dra >> (index * 4 + 3)) & 0x1;
37113189525SDoug Thompson 		csrow = &mci->csrows[index];
372806c35f5SAlan Cox 
37337f04581SDoug Thompson 		pci_read_config_byte(pdev, E7XXX_DRB + index, &value);
374806c35f5SAlan Cox 		/* convert a 64 or 32 MiB DRB to a page size. */
375806c35f5SAlan Cox 		cumul_size = value << (25 + drc_drbg - PAGE_SHIFT);
376537fba28SDave Peterson 		debugf3("%s(): (%d) cumul_size 0x%x\n", __func__, index,
377537fba28SDave Peterson 			cumul_size);
378806c35f5SAlan Cox 		if (cumul_size == last_cumul_size)
379806c35f5SAlan Cox 			continue;	/* not populated */
380806c35f5SAlan Cox 
381806c35f5SAlan Cox 		csrow->first_page = last_cumul_size;
382806c35f5SAlan Cox 		csrow->last_page = cumul_size - 1;
383806c35f5SAlan Cox 		csrow->nr_pages = cumul_size - last_cumul_size;
384806c35f5SAlan Cox 		last_cumul_size = cumul_size;
385806c35f5SAlan Cox 		csrow->grain = 1 << 12;	/* 4KiB - resolution of CELOG */
386806c35f5SAlan Cox 		csrow->mtype = MEM_RDDR;	/* only one type supported */
387806c35f5SAlan Cox 		csrow->dtype = mem_dev ? DEV_X4 : DEV_X8;
388806c35f5SAlan Cox 
389806c35f5SAlan Cox 		/*
390806c35f5SAlan Cox 		 * if single channel or x8 devices then SECDED
391806c35f5SAlan Cox 		 * if dual channel and x4 then S4ECD4ED
392806c35f5SAlan Cox 		 */
393806c35f5SAlan Cox 		if (drc_ddim) {
394806c35f5SAlan Cox 			if (drc_chan && mem_dev) {
395806c35f5SAlan Cox 				csrow->edac_mode = EDAC_S4ECD4ED;
396806c35f5SAlan Cox 				mci->edac_cap |= EDAC_FLAG_S4ECD4ED;
397806c35f5SAlan Cox 			} else {
398806c35f5SAlan Cox 				csrow->edac_mode = EDAC_SECDED;
399806c35f5SAlan Cox 				mci->edac_cap |= EDAC_FLAG_SECDED;
400806c35f5SAlan Cox 			}
401806c35f5SAlan Cox 		} else
402806c35f5SAlan Cox 			csrow->edac_mode = EDAC_NONE;
403806c35f5SAlan Cox 	}
40413189525SDoug Thompson }
405806c35f5SAlan Cox 
40613189525SDoug Thompson static int e7xxx_probe1(struct pci_dev *pdev, int dev_idx)
40713189525SDoug Thompson {
40813189525SDoug Thompson 	u16 pci_data;
40913189525SDoug Thompson 	struct mem_ctl_info *mci = NULL;
41013189525SDoug Thompson 	struct e7xxx_pvt *pvt = NULL;
41113189525SDoug Thompson 	u32 drc;
41213189525SDoug Thompson 	int drc_chan;
41313189525SDoug Thompson 	struct e7xxx_error_info discard;
41413189525SDoug Thompson 
41513189525SDoug Thompson 	debugf0("%s(): mci\n", __func__);
416c0d12172SDave Jiang 
417c0d12172SDave Jiang 	/* make sure error reporting method is sane */
418c0d12172SDave Jiang 	switch (edac_op_state) {
419c0d12172SDave Jiang 	case EDAC_OPSTATE_POLL:
420c0d12172SDave Jiang 	case EDAC_OPSTATE_NMI:
421c0d12172SDave Jiang 		break;
422c0d12172SDave Jiang 	default:
423c0d12172SDave Jiang 		edac_op_state = EDAC_OPSTATE_POLL;
424c0d12172SDave Jiang 		break;
425c0d12172SDave Jiang 	}
426c0d12172SDave Jiang 
42713189525SDoug Thompson 	pci_read_config_dword(pdev, E7XXX_DRC, &drc);
42813189525SDoug Thompson 
42913189525SDoug Thompson 	drc_chan = dual_channel_active(drc, dev_idx);
430*b8f6f975SDoug Thompson 	mci = edac_mc_alloc(sizeof(*pvt), E7XXX_NR_CSROWS, drc_chan + 1, 0);
43113189525SDoug Thompson 
43213189525SDoug Thompson 	if (mci == NULL)
43313189525SDoug Thompson 		return -ENOMEM;
43413189525SDoug Thompson 
43513189525SDoug Thompson 	debugf3("%s(): init mci\n", __func__);
43613189525SDoug Thompson 	mci->mtype_cap = MEM_FLAG_RDDR;
43713189525SDoug Thompson 	mci->edac_ctl_cap = EDAC_FLAG_NONE | EDAC_FLAG_SECDED |
43813189525SDoug Thompson 		EDAC_FLAG_S4ECD4ED;
43913189525SDoug Thompson 	/* FIXME - what if different memory types are in different csrows? */
44013189525SDoug Thompson 	mci->mod_name = EDAC_MOD_STR;
44113189525SDoug Thompson 	mci->mod_ver = E7XXX_REVISION;
44213189525SDoug Thompson 	mci->dev = &pdev->dev;
44313189525SDoug Thompson 	debugf3("%s(): init pvt\n", __func__);
44413189525SDoug Thompson 	pvt = (struct e7xxx_pvt *)mci->pvt_info;
44513189525SDoug Thompson 	pvt->dev_info = &e7xxx_devs[dev_idx];
44613189525SDoug Thompson 	pvt->bridge_ck = pci_get_device(PCI_VENDOR_ID_INTEL,
447849a4c37SDave Jiang 					pvt->dev_info->err_dev, pvt->bridge_ck);
44813189525SDoug Thompson 
44913189525SDoug Thompson 	if (!pvt->bridge_ck) {
45013189525SDoug Thompson 		e7xxx_printk(KERN_ERR, "error reporting device not found:"
45113189525SDoug Thompson 			"vendor %x device 0x%x (broken BIOS?)\n",
45213189525SDoug Thompson 			PCI_VENDOR_ID_INTEL, e7xxx_devs[dev_idx].err_dev);
45313189525SDoug Thompson 		goto fail0;
45413189525SDoug Thompson 	}
45513189525SDoug Thompson 
45613189525SDoug Thompson 	debugf3("%s(): more mci init\n", __func__);
45713189525SDoug Thompson 	mci->ctl_name = pvt->dev_info->ctl_name;
458c4192705SDave Jiang 	mci->dev_name = pci_name(pdev);
45913189525SDoug Thompson 	mci->edac_check = e7xxx_check;
46013189525SDoug Thompson 	mci->ctl_page_to_phys = ctl_page_to_phys;
46113189525SDoug Thompson 	e7xxx_init_csrows(mci, pdev, dev_idx, drc);
462806c35f5SAlan Cox 	mci->edac_cap |= EDAC_FLAG_NONE;
463537fba28SDave Peterson 	debugf3("%s(): tolm, remapbase, remaplimit\n", __func__);
464806c35f5SAlan Cox 	/* load the top of low memory, remap base, and remap limit vars */
46537f04581SDoug Thompson 	pci_read_config_word(pdev, E7XXX_TOLM, &pci_data);
466806c35f5SAlan Cox 	pvt->tolm = ((u32) pci_data) << 4;
46737f04581SDoug Thompson 	pci_read_config_word(pdev, E7XXX_REMAPBASE, &pci_data);
468806c35f5SAlan Cox 	pvt->remapbase = ((u32) pci_data) << 14;
46937f04581SDoug Thompson 	pci_read_config_word(pdev, E7XXX_REMAPLIMIT, &pci_data);
470806c35f5SAlan Cox 	pvt->remaplimit = ((u32) pci_data) << 14;
471537fba28SDave Peterson 	e7xxx_printk(KERN_INFO,
472e7ecd891SDave Peterson 		"tolm = %x, remapbase = %x, remaplimit = %x\n", pvt->tolm,
473e7ecd891SDave Peterson 		pvt->remapbase, pvt->remaplimit);
474806c35f5SAlan Cox 
475806c35f5SAlan Cox 	/* clear any pending errors, or initial state bits */
476749ede57SDave Peterson 	e7xxx_get_error_info(mci, &discard);
477806c35f5SAlan Cox 
4782d7bbb91SDoug Thompson 	/* Here we assume that we will never see multiple instances of this
4792d7bbb91SDoug Thompson 	 * type of memory controller.  The ID is therefore hardcoded to 0.
4802d7bbb91SDoug Thompson 	 */
481*b8f6f975SDoug Thompson 	if (edac_mc_add_mc(mci)) {
482537fba28SDave Peterson 		debugf3("%s(): failed edac_mc_add_mc()\n", __func__);
48313189525SDoug Thompson 		goto fail1;
484806c35f5SAlan Cox 	}
485806c35f5SAlan Cox 
486456a2f95SDave Jiang 	/* allocating generic PCI control info */
487456a2f95SDave Jiang 	e7xxx_pci = edac_pci_create_generic_ctl(&pdev->dev, EDAC_MOD_STR);
488456a2f95SDave Jiang 	if (!e7xxx_pci) {
489456a2f95SDave Jiang 		printk(KERN_WARNING
490456a2f95SDave Jiang 			"%s(): Unable to create PCI control\n",
491456a2f95SDave Jiang 			__func__);
492456a2f95SDave Jiang 		printk(KERN_WARNING
493456a2f95SDave Jiang 			"%s(): PCI error report via EDAC not setup\n",
494456a2f95SDave Jiang 			__func__);
495456a2f95SDave Jiang 	}
496456a2f95SDave Jiang 
497806c35f5SAlan Cox 	/* get this far and it's successful */
498537fba28SDave Peterson 	debugf3("%s(): success\n", __func__);
499806c35f5SAlan Cox 	return 0;
500806c35f5SAlan Cox 
50113189525SDoug Thompson fail1:
502806c35f5SAlan Cox 	pci_dev_put(pvt->bridge_ck);
503806c35f5SAlan Cox 
50413189525SDoug Thompson fail0:
50513189525SDoug Thompson 	edac_mc_free(mci);
50613189525SDoug Thompson 
50713189525SDoug Thompson 	return -ENODEV;
508806c35f5SAlan Cox }
509806c35f5SAlan Cox 
510806c35f5SAlan Cox /* returns count (>= 0), or negative on error */
511e7ecd891SDave Peterson static int __devinit e7xxx_init_one(struct pci_dev *pdev,
512e7ecd891SDave Peterson 				const struct pci_device_id *ent)
513806c35f5SAlan Cox {
514537fba28SDave Peterson 	debugf0("%s()\n", __func__);
515806c35f5SAlan Cox 
516806c35f5SAlan Cox 	/* wake up and enable device */
517806c35f5SAlan Cox 	return pci_enable_device(pdev) ?
518806c35f5SAlan Cox 		-EIO : e7xxx_probe1(pdev, ent->driver_data);
519806c35f5SAlan Cox }
520806c35f5SAlan Cox 
521806c35f5SAlan Cox static void __devexit e7xxx_remove_one(struct pci_dev *pdev)
522806c35f5SAlan Cox {
523806c35f5SAlan Cox 	struct mem_ctl_info *mci;
524806c35f5SAlan Cox 	struct e7xxx_pvt *pvt;
525806c35f5SAlan Cox 
526537fba28SDave Peterson 	debugf0("%s()\n", __func__);
527806c35f5SAlan Cox 
528456a2f95SDave Jiang 	if (e7xxx_pci)
529456a2f95SDave Jiang 		edac_pci_release_generic_ctl(e7xxx_pci);
530456a2f95SDave Jiang 
53137f04581SDoug Thompson 	if ((mci = edac_mc_del_mc(&pdev->dev)) == NULL)
53218dbc337SDave Peterson 		return;
53318dbc337SDave Peterson 
534806c35f5SAlan Cox 	pvt = (struct e7xxx_pvt *)mci->pvt_info;
535806c35f5SAlan Cox 	pci_dev_put(pvt->bridge_ck);
536806c35f5SAlan Cox 	edac_mc_free(mci);
537806c35f5SAlan Cox }
538806c35f5SAlan Cox 
539806c35f5SAlan Cox static const struct pci_device_id e7xxx_pci_tbl[] __devinitdata = {
540e7ecd891SDave Peterson 	{
541e7ecd891SDave Peterson 	 PCI_VEND_DEV(INTEL, 7205_0), PCI_ANY_ID, PCI_ANY_ID, 0, 0,
542849a4c37SDave Jiang 	 E7205},
543e7ecd891SDave Peterson 	{
544e7ecd891SDave Peterson 	 PCI_VEND_DEV(INTEL, 7500_0), PCI_ANY_ID, PCI_ANY_ID, 0, 0,
545849a4c37SDave Jiang 	 E7500},
546e7ecd891SDave Peterson 	{
547e7ecd891SDave Peterson 	 PCI_VEND_DEV(INTEL, 7501_0), PCI_ANY_ID, PCI_ANY_ID, 0, 0,
548849a4c37SDave Jiang 	 E7501},
549e7ecd891SDave Peterson 	{
550e7ecd891SDave Peterson 	 PCI_VEND_DEV(INTEL, 7505_0), PCI_ANY_ID, PCI_ANY_ID, 0, 0,
551849a4c37SDave Jiang 	 E7505},
552e7ecd891SDave Peterson 	{
553e7ecd891SDave Peterson 	 0,
554e7ecd891SDave Peterson 	 }			/* 0 terminated list. */
555806c35f5SAlan Cox };
556806c35f5SAlan Cox 
557806c35f5SAlan Cox MODULE_DEVICE_TABLE(pci, e7xxx_pci_tbl);
558806c35f5SAlan Cox 
559806c35f5SAlan Cox static struct pci_driver e7xxx_driver = {
560680cbbbbSDave Peterson 	.name = EDAC_MOD_STR,
561806c35f5SAlan Cox 	.probe = e7xxx_init_one,
562806c35f5SAlan Cox 	.remove = __devexit_p(e7xxx_remove_one),
563806c35f5SAlan Cox 	.id_table = e7xxx_pci_tbl,
564806c35f5SAlan Cox };
565806c35f5SAlan Cox 
566da9bb1d2SAlan Cox static int __init e7xxx_init(void)
567806c35f5SAlan Cox {
568806c35f5SAlan Cox 	return pci_register_driver(&e7xxx_driver);
569806c35f5SAlan Cox }
570806c35f5SAlan Cox 
571806c35f5SAlan Cox static void __exit e7xxx_exit(void)
572806c35f5SAlan Cox {
573806c35f5SAlan Cox 	pci_unregister_driver(&e7xxx_driver);
574806c35f5SAlan Cox }
575806c35f5SAlan Cox 
576806c35f5SAlan Cox module_init(e7xxx_init);
577806c35f5SAlan Cox module_exit(e7xxx_exit);
578806c35f5SAlan Cox 
579806c35f5SAlan Cox MODULE_LICENSE("GPL");
580806c35f5SAlan Cox MODULE_AUTHOR("Linux Networx (http://lnxi.com) Thayne Harbaugh et al\n"
581806c35f5SAlan Cox 		"Based on.work by Dan Hollis et al");
582806c35f5SAlan Cox MODULE_DESCRIPTION("MC support for Intel e7xxx memory controllers");
583c0d12172SDave Jiang module_param(edac_op_state, int, 0444);
584c0d12172SDave Jiang MODULE_PARM_DESC(edac_op_state, "EDAC Error Reporting state: 0=Poll,1=NMI");
585