xref: /linux/drivers/edac/x38_edac.c (revision 78d88e8a3d738f1ce508cd24b525d2e6cdfda1c1)
1df8bc08cSHitoshi Mitake /*
2df8bc08cSHitoshi Mitake  * Intel X38 Memory Controller kernel module
3df8bc08cSHitoshi Mitake  * Copyright (C) 2008 Cluster Computing, Inc.
4df8bc08cSHitoshi Mitake  *
5df8bc08cSHitoshi Mitake  * This file may be distributed under the terms of the
6df8bc08cSHitoshi Mitake  * GNU General Public License.
7df8bc08cSHitoshi Mitake  *
8df8bc08cSHitoshi Mitake  * This file is based on i3200_edac.c
9df8bc08cSHitoshi Mitake  *
10df8bc08cSHitoshi Mitake  */
11df8bc08cSHitoshi Mitake 
12df8bc08cSHitoshi Mitake #include <linux/module.h>
13df8bc08cSHitoshi Mitake #include <linux/init.h>
14df8bc08cSHitoshi Mitake #include <linux/pci.h>
15df8bc08cSHitoshi Mitake #include <linux/pci_ids.h>
16df8bc08cSHitoshi Mitake #include <linux/edac.h>
17a21e98ceSJason Baron 
182f8e2c87SChristoph Hellwig #include <linux/io-64-nonatomic-lo-hi.h>
19*78d88e8aSMauro Carvalho Chehab #include "edac_module.h"
20df8bc08cSHitoshi Mitake 
21df8bc08cSHitoshi Mitake #define X38_REVISION		"1.1"
22df8bc08cSHitoshi Mitake 
23df8bc08cSHitoshi Mitake #define EDAC_MOD_STR		"x38_edac"
24df8bc08cSHitoshi Mitake 
25df8bc08cSHitoshi Mitake #define PCI_DEVICE_ID_INTEL_X38_HB	0x29e0
26df8bc08cSHitoshi Mitake 
27df8bc08cSHitoshi Mitake #define X38_RANKS		8
28df8bc08cSHitoshi Mitake #define X38_RANKS_PER_CHANNEL	4
29df8bc08cSHitoshi Mitake #define X38_CHANNELS		2
30df8bc08cSHitoshi Mitake 
31df8bc08cSHitoshi Mitake /* Intel X38 register addresses - device 0 function 0 - DRAM Controller */
32df8bc08cSHitoshi Mitake 
33df8bc08cSHitoshi Mitake #define X38_MCHBAR_LOW	0x48	/* MCH Memory Mapped Register BAR */
343d768213SLu Zhihe #define X38_MCHBAR_HIGH	0x4c
35df8bc08cSHitoshi Mitake #define X38_MCHBAR_MASK	0xfffffc000ULL	/* bits 35:14 */
36df8bc08cSHitoshi Mitake #define X38_MMR_WINDOW_SIZE	16384
37df8bc08cSHitoshi Mitake 
38df8bc08cSHitoshi Mitake #define X38_TOM	0xa0	/* Top of Memory (16b)
39df8bc08cSHitoshi Mitake 				 *
40df8bc08cSHitoshi Mitake 				 * 15:10 reserved
41df8bc08cSHitoshi Mitake 				 *  9:0  total populated physical memory
42df8bc08cSHitoshi Mitake 				 */
43df8bc08cSHitoshi Mitake #define X38_TOM_MASK	0x3ff	/* bits 9:0 */
44df8bc08cSHitoshi Mitake #define X38_TOM_SHIFT 26	/* 64MiB grain */
45df8bc08cSHitoshi Mitake 
46df8bc08cSHitoshi Mitake #define X38_ERRSTS	0xc8	/* Error Status Register (16b)
47df8bc08cSHitoshi Mitake 				 *
48df8bc08cSHitoshi Mitake 				 * 15    reserved
49df8bc08cSHitoshi Mitake 				 * 14    Isochronous TBWRR Run Behind FIFO Full
50df8bc08cSHitoshi Mitake 				 *       (ITCV)
51df8bc08cSHitoshi Mitake 				 * 13    Isochronous TBWRR Run Behind FIFO Put
52df8bc08cSHitoshi Mitake 				 *       (ITSTV)
53df8bc08cSHitoshi Mitake 				 * 12    reserved
54df8bc08cSHitoshi Mitake 				 * 11    MCH Thermal Sensor Event
55df8bc08cSHitoshi Mitake 				 *       for SMI/SCI/SERR (GTSE)
56df8bc08cSHitoshi Mitake 				 * 10    reserved
57df8bc08cSHitoshi Mitake 				 *  9    LOCK to non-DRAM Memory Flag (LCKF)
58df8bc08cSHitoshi Mitake 				 *  8    reserved
59df8bc08cSHitoshi Mitake 				 *  7    DRAM Throttle Flag (DTF)
60df8bc08cSHitoshi Mitake 				 *  6:2  reserved
61df8bc08cSHitoshi Mitake 				 *  1    Multi-bit DRAM ECC Error Flag (DMERR)
62df8bc08cSHitoshi Mitake 				 *  0    Single-bit DRAM ECC Error Flag (DSERR)
63df8bc08cSHitoshi Mitake 				 */
64df8bc08cSHitoshi Mitake #define X38_ERRSTS_UE		0x0002
65df8bc08cSHitoshi Mitake #define X38_ERRSTS_CE		0x0001
66df8bc08cSHitoshi Mitake #define X38_ERRSTS_BITS	(X38_ERRSTS_UE | X38_ERRSTS_CE)
67df8bc08cSHitoshi Mitake 
68df8bc08cSHitoshi Mitake 
69df8bc08cSHitoshi Mitake /* Intel  MMIO register space - device 0 function 0 - MMR space */
70df8bc08cSHitoshi Mitake 
71df8bc08cSHitoshi Mitake #define X38_C0DRB	0x200	/* Channel 0 DRAM Rank Boundary (16b x 4)
72df8bc08cSHitoshi Mitake 				 *
73df8bc08cSHitoshi Mitake 				 * 15:10 reserved
74df8bc08cSHitoshi Mitake 				 *  9:0  Channel 0 DRAM Rank Boundary Address
75df8bc08cSHitoshi Mitake 				 */
76df8bc08cSHitoshi Mitake #define X38_C1DRB	0x600	/* Channel 1 DRAM Rank Boundary (16b x 4) */
77df8bc08cSHitoshi Mitake #define X38_DRB_MASK	0x3ff	/* bits 9:0 */
78df8bc08cSHitoshi Mitake #define X38_DRB_SHIFT 26	/* 64MiB grain */
79df8bc08cSHitoshi Mitake 
80df8bc08cSHitoshi Mitake #define X38_C0ECCERRLOG 0x280	/* Channel 0 ECC Error Log (64b)
81df8bc08cSHitoshi Mitake 				 *
82df8bc08cSHitoshi Mitake 				 * 63:48 Error Column Address (ERRCOL)
83df8bc08cSHitoshi Mitake 				 * 47:32 Error Row Address (ERRROW)
84df8bc08cSHitoshi Mitake 				 * 31:29 Error Bank Address (ERRBANK)
85df8bc08cSHitoshi Mitake 				 * 28:27 Error Rank Address (ERRRANK)
86df8bc08cSHitoshi Mitake 				 * 26:24 reserved
87df8bc08cSHitoshi Mitake 				 * 23:16 Error Syndrome (ERRSYND)
88df8bc08cSHitoshi Mitake 				 * 15: 2 reserved
89df8bc08cSHitoshi Mitake 				 *    1  Multiple Bit Error Status (MERRSTS)
90df8bc08cSHitoshi Mitake 				 *    0  Correctable Error Status (CERRSTS)
91df8bc08cSHitoshi Mitake 				 */
92df8bc08cSHitoshi Mitake #define X38_C1ECCERRLOG 0x680	/* Channel 1 ECC Error Log (64b) */
93df8bc08cSHitoshi Mitake #define X38_ECCERRLOG_CE	0x1
94df8bc08cSHitoshi Mitake #define X38_ECCERRLOG_UE	0x2
95df8bc08cSHitoshi Mitake #define X38_ECCERRLOG_RANK_BITS	0x18000000
96df8bc08cSHitoshi Mitake #define X38_ECCERRLOG_SYNDROME_BITS	0xff0000
97df8bc08cSHitoshi Mitake 
98df8bc08cSHitoshi Mitake #define X38_CAPID0 0xe0	/* see P.94 of spec for details */
99df8bc08cSHitoshi Mitake 
100df8bc08cSHitoshi Mitake static int x38_channel_num;
101df8bc08cSHitoshi Mitake 
102df8bc08cSHitoshi Mitake static int how_many_channel(struct pci_dev *pdev)
103df8bc08cSHitoshi Mitake {
104df8bc08cSHitoshi Mitake 	unsigned char capid0_8b; /* 8th byte of CAPID0 */
105df8bc08cSHitoshi Mitake 
106df8bc08cSHitoshi Mitake 	pci_read_config_byte(pdev, X38_CAPID0 + 8, &capid0_8b);
107df8bc08cSHitoshi Mitake 	if (capid0_8b & 0x20) {	/* check DCD: Dual Channel Disable */
108956b9ba1SJoe Perches 		edac_dbg(0, "In single channel mode\n");
109df8bc08cSHitoshi Mitake 		x38_channel_num = 1;
110df8bc08cSHitoshi Mitake 	} else {
111956b9ba1SJoe Perches 		edac_dbg(0, "In dual channel mode\n");
112df8bc08cSHitoshi Mitake 		x38_channel_num = 2;
113df8bc08cSHitoshi Mitake 	}
114df8bc08cSHitoshi Mitake 
115df8bc08cSHitoshi Mitake 	return x38_channel_num;
116df8bc08cSHitoshi Mitake }
117df8bc08cSHitoshi Mitake 
118df8bc08cSHitoshi Mitake static unsigned long eccerrlog_syndrome(u64 log)
119df8bc08cSHitoshi Mitake {
120df8bc08cSHitoshi Mitake 	return (log & X38_ECCERRLOG_SYNDROME_BITS) >> 16;
121df8bc08cSHitoshi Mitake }
122df8bc08cSHitoshi Mitake 
123df8bc08cSHitoshi Mitake static int eccerrlog_row(int channel, u64 log)
124df8bc08cSHitoshi Mitake {
125df8bc08cSHitoshi Mitake 	return ((log & X38_ECCERRLOG_RANK_BITS) >> 27) |
126df8bc08cSHitoshi Mitake 		(channel * X38_RANKS_PER_CHANNEL);
127df8bc08cSHitoshi Mitake }
128df8bc08cSHitoshi Mitake 
129df8bc08cSHitoshi Mitake enum x38_chips {
130df8bc08cSHitoshi Mitake 	X38 = 0,
131df8bc08cSHitoshi Mitake };
132df8bc08cSHitoshi Mitake 
133df8bc08cSHitoshi Mitake struct x38_dev_info {
134df8bc08cSHitoshi Mitake 	const char *ctl_name;
135df8bc08cSHitoshi Mitake };
136df8bc08cSHitoshi Mitake 
137df8bc08cSHitoshi Mitake struct x38_error_info {
138df8bc08cSHitoshi Mitake 	u16 errsts;
139df8bc08cSHitoshi Mitake 	u16 errsts2;
140df8bc08cSHitoshi Mitake 	u64 eccerrlog[X38_CHANNELS];
141df8bc08cSHitoshi Mitake };
142df8bc08cSHitoshi Mitake 
143df8bc08cSHitoshi Mitake static const struct x38_dev_info x38_devs[] = {
144df8bc08cSHitoshi Mitake 	[X38] = {
145df8bc08cSHitoshi Mitake 		.ctl_name = "x38"},
146df8bc08cSHitoshi Mitake };
147df8bc08cSHitoshi Mitake 
148df8bc08cSHitoshi Mitake static struct pci_dev *mci_pdev;
149df8bc08cSHitoshi Mitake static int x38_registered = 1;
150df8bc08cSHitoshi Mitake 
151df8bc08cSHitoshi Mitake 
152df8bc08cSHitoshi Mitake static void x38_clear_error_info(struct mem_ctl_info *mci)
153df8bc08cSHitoshi Mitake {
154df8bc08cSHitoshi Mitake 	struct pci_dev *pdev;
155df8bc08cSHitoshi Mitake 
156fd687502SMauro Carvalho Chehab 	pdev = to_pci_dev(mci->pdev);
157df8bc08cSHitoshi Mitake 
158df8bc08cSHitoshi Mitake 	/*
159df8bc08cSHitoshi Mitake 	 * Clear any error bits.
160df8bc08cSHitoshi Mitake 	 * (Yes, we really clear bits by writing 1 to them.)
161df8bc08cSHitoshi Mitake 	 */
162df8bc08cSHitoshi Mitake 	pci_write_bits16(pdev, X38_ERRSTS, X38_ERRSTS_BITS,
163df8bc08cSHitoshi Mitake 			 X38_ERRSTS_BITS);
164df8bc08cSHitoshi Mitake }
165df8bc08cSHitoshi Mitake 
166df8bc08cSHitoshi Mitake static void x38_get_and_clear_error_info(struct mem_ctl_info *mci,
167df8bc08cSHitoshi Mitake 				 struct x38_error_info *info)
168df8bc08cSHitoshi Mitake {
169df8bc08cSHitoshi Mitake 	struct pci_dev *pdev;
170df8bc08cSHitoshi Mitake 	void __iomem *window = mci->pvt_info;
171df8bc08cSHitoshi Mitake 
172fd687502SMauro Carvalho Chehab 	pdev = to_pci_dev(mci->pdev);
173df8bc08cSHitoshi Mitake 
174df8bc08cSHitoshi Mitake 	/*
175df8bc08cSHitoshi Mitake 	 * This is a mess because there is no atomic way to read all the
176df8bc08cSHitoshi Mitake 	 * registers at once and the registers can transition from CE being
177df8bc08cSHitoshi Mitake 	 * overwritten by UE.
178df8bc08cSHitoshi Mitake 	 */
179df8bc08cSHitoshi Mitake 	pci_read_config_word(pdev, X38_ERRSTS, &info->errsts);
180df8bc08cSHitoshi Mitake 	if (!(info->errsts & X38_ERRSTS_BITS))
181df8bc08cSHitoshi Mitake 		return;
182df8bc08cSHitoshi Mitake 
183a21e98ceSJason Baron 	info->eccerrlog[0] = lo_hi_readq(window + X38_C0ECCERRLOG);
184df8bc08cSHitoshi Mitake 	if (x38_channel_num == 2)
185a21e98ceSJason Baron 		info->eccerrlog[1] = lo_hi_readq(window + X38_C1ECCERRLOG);
186df8bc08cSHitoshi Mitake 
187df8bc08cSHitoshi Mitake 	pci_read_config_word(pdev, X38_ERRSTS, &info->errsts2);
188df8bc08cSHitoshi Mitake 
189df8bc08cSHitoshi Mitake 	/*
190df8bc08cSHitoshi Mitake 	 * If the error is the same for both reads then the first set
191df8bc08cSHitoshi Mitake 	 * of reads is valid.  If there is a change then there is a CE
192df8bc08cSHitoshi Mitake 	 * with no info and the second set of reads is valid and
193df8bc08cSHitoshi Mitake 	 * should be UE info.
194df8bc08cSHitoshi Mitake 	 */
195df8bc08cSHitoshi Mitake 	if ((info->errsts ^ info->errsts2) & X38_ERRSTS_BITS) {
196a21e98ceSJason Baron 		info->eccerrlog[0] = lo_hi_readq(window + X38_C0ECCERRLOG);
197df8bc08cSHitoshi Mitake 		if (x38_channel_num == 2)
198df8bc08cSHitoshi Mitake 			info->eccerrlog[1] =
199a21e98ceSJason Baron 				lo_hi_readq(window + X38_C1ECCERRLOG);
200df8bc08cSHitoshi Mitake 	}
201df8bc08cSHitoshi Mitake 
202df8bc08cSHitoshi Mitake 	x38_clear_error_info(mci);
203df8bc08cSHitoshi Mitake }
204df8bc08cSHitoshi Mitake 
205df8bc08cSHitoshi Mitake static void x38_process_error_info(struct mem_ctl_info *mci,
206df8bc08cSHitoshi Mitake 				struct x38_error_info *info)
207df8bc08cSHitoshi Mitake {
208df8bc08cSHitoshi Mitake 	int channel;
209df8bc08cSHitoshi Mitake 	u64 log;
210df8bc08cSHitoshi Mitake 
211df8bc08cSHitoshi Mitake 	if (!(info->errsts & X38_ERRSTS_BITS))
212df8bc08cSHitoshi Mitake 		return;
213df8bc08cSHitoshi Mitake 
214df8bc08cSHitoshi Mitake 	if ((info->errsts ^ info->errsts2) & X38_ERRSTS_BITS) {
2159eb07a7fSMauro Carvalho Chehab 		edac_mc_handle_error(HW_EVENT_ERR_UNCORRECTED, mci, 1, 0, 0, 0,
216e2acc357SMauro Carvalho Chehab 				     -1, -1, -1,
21703f7eae8SMauro Carvalho Chehab 				     "UE overwrote CE", "");
218df8bc08cSHitoshi Mitake 		info->errsts = info->errsts2;
219df8bc08cSHitoshi Mitake 	}
220df8bc08cSHitoshi Mitake 
221df8bc08cSHitoshi Mitake 	for (channel = 0; channel < x38_channel_num; channel++) {
222df8bc08cSHitoshi Mitake 		log = info->eccerrlog[channel];
223df8bc08cSHitoshi Mitake 		if (log & X38_ECCERRLOG_UE) {
2249eb07a7fSMauro Carvalho Chehab 			edac_mc_handle_error(HW_EVENT_ERR_UNCORRECTED, mci, 1,
225e2acc357SMauro Carvalho Chehab 					     0, 0, 0,
226e2acc357SMauro Carvalho Chehab 					     eccerrlog_row(channel, log),
227e2acc357SMauro Carvalho Chehab 					     -1, -1,
22803f7eae8SMauro Carvalho Chehab 					     "x38 UE", "");
229df8bc08cSHitoshi Mitake 		} else if (log & X38_ECCERRLOG_CE) {
2309eb07a7fSMauro Carvalho Chehab 			edac_mc_handle_error(HW_EVENT_ERR_CORRECTED, mci, 1,
231e2acc357SMauro Carvalho Chehab 					     0, 0, eccerrlog_syndrome(log),
232e2acc357SMauro Carvalho Chehab 					     eccerrlog_row(channel, log),
233e2acc357SMauro Carvalho Chehab 					     -1, -1,
23403f7eae8SMauro Carvalho Chehab 					     "x38 CE", "");
235df8bc08cSHitoshi Mitake 		}
236df8bc08cSHitoshi Mitake 	}
237df8bc08cSHitoshi Mitake }
238df8bc08cSHitoshi Mitake 
239df8bc08cSHitoshi Mitake static void x38_check(struct mem_ctl_info *mci)
240df8bc08cSHitoshi Mitake {
241df8bc08cSHitoshi Mitake 	struct x38_error_info info;
242df8bc08cSHitoshi Mitake 
243956b9ba1SJoe Perches 	edac_dbg(1, "MC%d\n", mci->mc_idx);
244df8bc08cSHitoshi Mitake 	x38_get_and_clear_error_info(mci, &info);
245df8bc08cSHitoshi Mitake 	x38_process_error_info(mci, &info);
246df8bc08cSHitoshi Mitake }
247df8bc08cSHitoshi Mitake 
248e0d391abSJingoo Han static void __iomem *x38_map_mchbar(struct pci_dev *pdev)
249df8bc08cSHitoshi Mitake {
250df8bc08cSHitoshi Mitake 	union {
251df8bc08cSHitoshi Mitake 		u64 mchbar;
252df8bc08cSHitoshi Mitake 		struct {
253df8bc08cSHitoshi Mitake 			u32 mchbar_low;
254df8bc08cSHitoshi Mitake 			u32 mchbar_high;
255df8bc08cSHitoshi Mitake 		};
256df8bc08cSHitoshi Mitake 	} u;
257df8bc08cSHitoshi Mitake 	void __iomem *window;
258df8bc08cSHitoshi Mitake 
259df8bc08cSHitoshi Mitake 	pci_read_config_dword(pdev, X38_MCHBAR_LOW, &u.mchbar_low);
260df8bc08cSHitoshi Mitake 	pci_write_config_dword(pdev, X38_MCHBAR_LOW, u.mchbar_low | 0x1);
261df8bc08cSHitoshi Mitake 	pci_read_config_dword(pdev, X38_MCHBAR_HIGH, &u.mchbar_high);
262df8bc08cSHitoshi Mitake 	u.mchbar &= X38_MCHBAR_MASK;
263df8bc08cSHitoshi Mitake 
264df8bc08cSHitoshi Mitake 	if (u.mchbar != (resource_size_t)u.mchbar) {
265df8bc08cSHitoshi Mitake 		printk(KERN_ERR
266df8bc08cSHitoshi Mitake 			"x38: mmio space beyond accessible range (0x%llx)\n",
267df8bc08cSHitoshi Mitake 			(unsigned long long)u.mchbar);
268df8bc08cSHitoshi Mitake 		return NULL;
269df8bc08cSHitoshi Mitake 	}
270df8bc08cSHitoshi Mitake 
271df8bc08cSHitoshi Mitake 	window = ioremap_nocache(u.mchbar, X38_MMR_WINDOW_SIZE);
272df8bc08cSHitoshi Mitake 	if (!window)
273df8bc08cSHitoshi Mitake 		printk(KERN_ERR "x38: cannot map mmio space at 0x%llx\n",
274df8bc08cSHitoshi Mitake 			(unsigned long long)u.mchbar);
275df8bc08cSHitoshi Mitake 
276df8bc08cSHitoshi Mitake 	return window;
277df8bc08cSHitoshi Mitake }
278df8bc08cSHitoshi Mitake 
279df8bc08cSHitoshi Mitake 
280df8bc08cSHitoshi Mitake static void x38_get_drbs(void __iomem *window,
281df8bc08cSHitoshi Mitake 			u16 drbs[X38_CHANNELS][X38_RANKS_PER_CHANNEL])
282df8bc08cSHitoshi Mitake {
283df8bc08cSHitoshi Mitake 	int i;
284df8bc08cSHitoshi Mitake 
285df8bc08cSHitoshi Mitake 	for (i = 0; i < X38_RANKS_PER_CHANNEL; i++) {
286df8bc08cSHitoshi Mitake 		drbs[0][i] = readw(window + X38_C0DRB + 2*i) & X38_DRB_MASK;
287df8bc08cSHitoshi Mitake 		drbs[1][i] = readw(window + X38_C1DRB + 2*i) & X38_DRB_MASK;
288df8bc08cSHitoshi Mitake 	}
289df8bc08cSHitoshi Mitake }
290df8bc08cSHitoshi Mitake 
291df8bc08cSHitoshi Mitake static bool x38_is_stacked(struct pci_dev *pdev,
292df8bc08cSHitoshi Mitake 			u16 drbs[X38_CHANNELS][X38_RANKS_PER_CHANNEL])
293df8bc08cSHitoshi Mitake {
294df8bc08cSHitoshi Mitake 	u16 tom;
295df8bc08cSHitoshi Mitake 
296df8bc08cSHitoshi Mitake 	pci_read_config_word(pdev, X38_TOM, &tom);
297df8bc08cSHitoshi Mitake 	tom &= X38_TOM_MASK;
298df8bc08cSHitoshi Mitake 
299df8bc08cSHitoshi Mitake 	return drbs[X38_CHANNELS - 1][X38_RANKS_PER_CHANNEL - 1] == tom;
300df8bc08cSHitoshi Mitake }
301df8bc08cSHitoshi Mitake 
302df8bc08cSHitoshi Mitake static unsigned long drb_to_nr_pages(
303df8bc08cSHitoshi Mitake 			u16 drbs[X38_CHANNELS][X38_RANKS_PER_CHANNEL],
304df8bc08cSHitoshi Mitake 			bool stacked, int channel, int rank)
305df8bc08cSHitoshi Mitake {
306df8bc08cSHitoshi Mitake 	int n;
307df8bc08cSHitoshi Mitake 
308df8bc08cSHitoshi Mitake 	n = drbs[channel][rank];
309df8bc08cSHitoshi Mitake 	if (rank > 0)
310df8bc08cSHitoshi Mitake 		n -= drbs[channel][rank - 1];
311df8bc08cSHitoshi Mitake 	if (stacked && (channel == 1) && drbs[channel][rank] ==
312df8bc08cSHitoshi Mitake 				drbs[channel][X38_RANKS_PER_CHANNEL - 1]) {
313df8bc08cSHitoshi Mitake 		n -= drbs[0][X38_RANKS_PER_CHANNEL - 1];
314df8bc08cSHitoshi Mitake 	}
315df8bc08cSHitoshi Mitake 
316df8bc08cSHitoshi Mitake 	n <<= (X38_DRB_SHIFT - PAGE_SHIFT);
317df8bc08cSHitoshi Mitake 	return n;
318df8bc08cSHitoshi Mitake }
319df8bc08cSHitoshi Mitake 
320df8bc08cSHitoshi Mitake static int x38_probe1(struct pci_dev *pdev, int dev_idx)
321df8bc08cSHitoshi Mitake {
322df8bc08cSHitoshi Mitake 	int rc;
323084a4fccSMauro Carvalho Chehab 	int i, j;
324df8bc08cSHitoshi Mitake 	struct mem_ctl_info *mci = NULL;
325e2acc357SMauro Carvalho Chehab 	struct edac_mc_layer layers[2];
326df8bc08cSHitoshi Mitake 	u16 drbs[X38_CHANNELS][X38_RANKS_PER_CHANNEL];
327df8bc08cSHitoshi Mitake 	bool stacked;
328df8bc08cSHitoshi Mitake 	void __iomem *window;
329df8bc08cSHitoshi Mitake 
330956b9ba1SJoe Perches 	edac_dbg(0, "MC:\n");
331df8bc08cSHitoshi Mitake 
332df8bc08cSHitoshi Mitake 	window = x38_map_mchbar(pdev);
333df8bc08cSHitoshi Mitake 	if (!window)
334df8bc08cSHitoshi Mitake 		return -ENODEV;
335df8bc08cSHitoshi Mitake 
336df8bc08cSHitoshi Mitake 	x38_get_drbs(window, drbs);
337df8bc08cSHitoshi Mitake 
338df8bc08cSHitoshi Mitake 	how_many_channel(pdev);
339df8bc08cSHitoshi Mitake 
340df8bc08cSHitoshi Mitake 	/* FIXME: unconventional pvt_info usage */
341e2acc357SMauro Carvalho Chehab 	layers[0].type = EDAC_MC_LAYER_CHIP_SELECT;
342e2acc357SMauro Carvalho Chehab 	layers[0].size = X38_RANKS;
343e2acc357SMauro Carvalho Chehab 	layers[0].is_virt_csrow = true;
344e2acc357SMauro Carvalho Chehab 	layers[1].type = EDAC_MC_LAYER_CHANNEL;
345e2acc357SMauro Carvalho Chehab 	layers[1].size = x38_channel_num;
346e2acc357SMauro Carvalho Chehab 	layers[1].is_virt_csrow = false;
347ca0907b9SMauro Carvalho Chehab 	mci = edac_mc_alloc(0, ARRAY_SIZE(layers), layers, 0);
348df8bc08cSHitoshi Mitake 	if (!mci)
349df8bc08cSHitoshi Mitake 		return -ENOMEM;
350df8bc08cSHitoshi Mitake 
351956b9ba1SJoe Perches 	edac_dbg(3, "MC: init mci\n");
352df8bc08cSHitoshi Mitake 
353fd687502SMauro Carvalho Chehab 	mci->pdev = &pdev->dev;
354df8bc08cSHitoshi Mitake 	mci->mtype_cap = MEM_FLAG_DDR2;
355df8bc08cSHitoshi Mitake 
356df8bc08cSHitoshi Mitake 	mci->edac_ctl_cap = EDAC_FLAG_SECDED;
357df8bc08cSHitoshi Mitake 	mci->edac_cap = EDAC_FLAG_SECDED;
358df8bc08cSHitoshi Mitake 
359df8bc08cSHitoshi Mitake 	mci->mod_name = EDAC_MOD_STR;
360df8bc08cSHitoshi Mitake 	mci->mod_ver = X38_REVISION;
361df8bc08cSHitoshi Mitake 	mci->ctl_name = x38_devs[dev_idx].ctl_name;
362df8bc08cSHitoshi Mitake 	mci->dev_name = pci_name(pdev);
363df8bc08cSHitoshi Mitake 	mci->edac_check = x38_check;
364df8bc08cSHitoshi Mitake 	mci->ctl_page_to_phys = NULL;
365df8bc08cSHitoshi Mitake 	mci->pvt_info = window;
366df8bc08cSHitoshi Mitake 
367df8bc08cSHitoshi Mitake 	stacked = x38_is_stacked(pdev, drbs);
368df8bc08cSHitoshi Mitake 
369df8bc08cSHitoshi Mitake 	/*
370df8bc08cSHitoshi Mitake 	 * The dram rank boundary (DRB) reg values are boundary addresses
371df8bc08cSHitoshi Mitake 	 * for each DRAM rank with a granularity of 64MB.  DRB regs are
372df8bc08cSHitoshi Mitake 	 * cumulative; the last one will contain the total memory
373df8bc08cSHitoshi Mitake 	 * contained in all ranks.
374df8bc08cSHitoshi Mitake 	 */
375df8bc08cSHitoshi Mitake 	for (i = 0; i < mci->nr_csrows; i++) {
376df8bc08cSHitoshi Mitake 		unsigned long nr_pages;
377de3910ebSMauro Carvalho Chehab 		struct csrow_info *csrow = mci->csrows[i];
378df8bc08cSHitoshi Mitake 
379df8bc08cSHitoshi Mitake 		nr_pages = drb_to_nr_pages(drbs, stacked,
380df8bc08cSHitoshi Mitake 			i / X38_RANKS_PER_CHANNEL,
381df8bc08cSHitoshi Mitake 			i % X38_RANKS_PER_CHANNEL);
382df8bc08cSHitoshi Mitake 
383084a4fccSMauro Carvalho Chehab 		if (nr_pages == 0)
384df8bc08cSHitoshi Mitake 			continue;
385df8bc08cSHitoshi Mitake 
386084a4fccSMauro Carvalho Chehab 		for (j = 0; j < x38_channel_num; j++) {
387de3910ebSMauro Carvalho Chehab 			struct dimm_info *dimm = csrow->channels[j]->dimm;
388a895bf8bSMauro Carvalho Chehab 
389a895bf8bSMauro Carvalho Chehab 			dimm->nr_pages = nr_pages / x38_channel_num;
390084a4fccSMauro Carvalho Chehab 			dimm->grain = nr_pages << PAGE_SHIFT;
391084a4fccSMauro Carvalho Chehab 			dimm->mtype = MEM_DDR2;
392084a4fccSMauro Carvalho Chehab 			dimm->dtype = DEV_UNKNOWN;
393084a4fccSMauro Carvalho Chehab 			dimm->edac_mode = EDAC_UNKNOWN;
394084a4fccSMauro Carvalho Chehab 		}
395df8bc08cSHitoshi Mitake 	}
396df8bc08cSHitoshi Mitake 
397df8bc08cSHitoshi Mitake 	x38_clear_error_info(mci);
398df8bc08cSHitoshi Mitake 
399df8bc08cSHitoshi Mitake 	rc = -ENODEV;
400df8bc08cSHitoshi Mitake 	if (edac_mc_add_mc(mci)) {
401956b9ba1SJoe Perches 		edac_dbg(3, "MC: failed edac_mc_add_mc()\n");
402df8bc08cSHitoshi Mitake 		goto fail;
403df8bc08cSHitoshi Mitake 	}
404df8bc08cSHitoshi Mitake 
405df8bc08cSHitoshi Mitake 	/* get this far and it's successful */
406956b9ba1SJoe Perches 	edac_dbg(3, "MC: success\n");
407df8bc08cSHitoshi Mitake 	return 0;
408df8bc08cSHitoshi Mitake 
409df8bc08cSHitoshi Mitake fail:
410df8bc08cSHitoshi Mitake 	iounmap(window);
411df8bc08cSHitoshi Mitake 	if (mci)
412df8bc08cSHitoshi Mitake 		edac_mc_free(mci);
413df8bc08cSHitoshi Mitake 
414df8bc08cSHitoshi Mitake 	return rc;
415df8bc08cSHitoshi Mitake }
416df8bc08cSHitoshi Mitake 
4179b3c6e85SGreg Kroah-Hartman static int x38_init_one(struct pci_dev *pdev, const struct pci_device_id *ent)
418df8bc08cSHitoshi Mitake {
419df8bc08cSHitoshi Mitake 	int rc;
420df8bc08cSHitoshi Mitake 
421956b9ba1SJoe Perches 	edac_dbg(0, "MC:\n");
422df8bc08cSHitoshi Mitake 
423df8bc08cSHitoshi Mitake 	if (pci_enable_device(pdev) < 0)
424df8bc08cSHitoshi Mitake 		return -EIO;
425df8bc08cSHitoshi Mitake 
426df8bc08cSHitoshi Mitake 	rc = x38_probe1(pdev, ent->driver_data);
427df8bc08cSHitoshi Mitake 	if (!mci_pdev)
428df8bc08cSHitoshi Mitake 		mci_pdev = pci_dev_get(pdev);
429df8bc08cSHitoshi Mitake 
430df8bc08cSHitoshi Mitake 	return rc;
431df8bc08cSHitoshi Mitake }
432df8bc08cSHitoshi Mitake 
4339b3c6e85SGreg Kroah-Hartman static void x38_remove_one(struct pci_dev *pdev)
434df8bc08cSHitoshi Mitake {
435df8bc08cSHitoshi Mitake 	struct mem_ctl_info *mci;
436df8bc08cSHitoshi Mitake 
437956b9ba1SJoe Perches 	edac_dbg(0, "\n");
438df8bc08cSHitoshi Mitake 
439df8bc08cSHitoshi Mitake 	mci = edac_mc_del_mc(&pdev->dev);
440df8bc08cSHitoshi Mitake 	if (!mci)
441df8bc08cSHitoshi Mitake 		return;
442df8bc08cSHitoshi Mitake 
443df8bc08cSHitoshi Mitake 	iounmap(mci->pvt_info);
444df8bc08cSHitoshi Mitake 
445df8bc08cSHitoshi Mitake 	edac_mc_free(mci);
446df8bc08cSHitoshi Mitake }
447df8bc08cSHitoshi Mitake 
448ba935f40SJingoo Han static const struct pci_device_id x38_pci_tbl[] = {
449df8bc08cSHitoshi Mitake 	{
450df8bc08cSHitoshi Mitake 	 PCI_VEND_DEV(INTEL, X38_HB), PCI_ANY_ID, PCI_ANY_ID, 0, 0,
451df8bc08cSHitoshi Mitake 	 X38},
452df8bc08cSHitoshi Mitake 	{
453df8bc08cSHitoshi Mitake 	 0,
454df8bc08cSHitoshi Mitake 	 }			/* 0 terminated list. */
455df8bc08cSHitoshi Mitake };
456df8bc08cSHitoshi Mitake 
457df8bc08cSHitoshi Mitake MODULE_DEVICE_TABLE(pci, x38_pci_tbl);
458df8bc08cSHitoshi Mitake 
459df8bc08cSHitoshi Mitake static struct pci_driver x38_driver = {
460df8bc08cSHitoshi Mitake 	.name = EDAC_MOD_STR,
461df8bc08cSHitoshi Mitake 	.probe = x38_init_one,
4629b3c6e85SGreg Kroah-Hartman 	.remove = x38_remove_one,
463df8bc08cSHitoshi Mitake 	.id_table = x38_pci_tbl,
464df8bc08cSHitoshi Mitake };
465df8bc08cSHitoshi Mitake 
466df8bc08cSHitoshi Mitake static int __init x38_init(void)
467df8bc08cSHitoshi Mitake {
468df8bc08cSHitoshi Mitake 	int pci_rc;
469df8bc08cSHitoshi Mitake 
470956b9ba1SJoe Perches 	edac_dbg(3, "MC:\n");
471df8bc08cSHitoshi Mitake 
472df8bc08cSHitoshi Mitake 	/* Ensure that the OPSTATE is set correctly for POLL or NMI */
473df8bc08cSHitoshi Mitake 	opstate_init();
474df8bc08cSHitoshi Mitake 
475df8bc08cSHitoshi Mitake 	pci_rc = pci_register_driver(&x38_driver);
476df8bc08cSHitoshi Mitake 	if (pci_rc < 0)
477df8bc08cSHitoshi Mitake 		goto fail0;
478df8bc08cSHitoshi Mitake 
479df8bc08cSHitoshi Mitake 	if (!mci_pdev) {
480df8bc08cSHitoshi Mitake 		x38_registered = 0;
481df8bc08cSHitoshi Mitake 		mci_pdev = pci_get_device(PCI_VENDOR_ID_INTEL,
482df8bc08cSHitoshi Mitake 					PCI_DEVICE_ID_INTEL_X38_HB, NULL);
483df8bc08cSHitoshi Mitake 		if (!mci_pdev) {
484956b9ba1SJoe Perches 			edac_dbg(0, "x38 pci_get_device fail\n");
485df8bc08cSHitoshi Mitake 			pci_rc = -ENODEV;
486df8bc08cSHitoshi Mitake 			goto fail1;
487df8bc08cSHitoshi Mitake 		}
488df8bc08cSHitoshi Mitake 
489df8bc08cSHitoshi Mitake 		pci_rc = x38_init_one(mci_pdev, x38_pci_tbl);
490df8bc08cSHitoshi Mitake 		if (pci_rc < 0) {
491956b9ba1SJoe Perches 			edac_dbg(0, "x38 init fail\n");
492df8bc08cSHitoshi Mitake 			pci_rc = -ENODEV;
493df8bc08cSHitoshi Mitake 			goto fail1;
494df8bc08cSHitoshi Mitake 		}
495df8bc08cSHitoshi Mitake 	}
496df8bc08cSHitoshi Mitake 
497df8bc08cSHitoshi Mitake 	return 0;
498df8bc08cSHitoshi Mitake 
499df8bc08cSHitoshi Mitake fail1:
500df8bc08cSHitoshi Mitake 	pci_unregister_driver(&x38_driver);
501df8bc08cSHitoshi Mitake 
502df8bc08cSHitoshi Mitake fail0:
503df8bc08cSHitoshi Mitake 	pci_dev_put(mci_pdev);
504df8bc08cSHitoshi Mitake 
505df8bc08cSHitoshi Mitake 	return pci_rc;
506df8bc08cSHitoshi Mitake }
507df8bc08cSHitoshi Mitake 
508df8bc08cSHitoshi Mitake static void __exit x38_exit(void)
509df8bc08cSHitoshi Mitake {
510956b9ba1SJoe Perches 	edac_dbg(3, "MC:\n");
511df8bc08cSHitoshi Mitake 
512df8bc08cSHitoshi Mitake 	pci_unregister_driver(&x38_driver);
513df8bc08cSHitoshi Mitake 	if (!x38_registered) {
514df8bc08cSHitoshi Mitake 		x38_remove_one(mci_pdev);
515df8bc08cSHitoshi Mitake 		pci_dev_put(mci_pdev);
516df8bc08cSHitoshi Mitake 	}
517df8bc08cSHitoshi Mitake }
518df8bc08cSHitoshi Mitake 
519df8bc08cSHitoshi Mitake module_init(x38_init);
520df8bc08cSHitoshi Mitake module_exit(x38_exit);
521df8bc08cSHitoshi Mitake 
522df8bc08cSHitoshi Mitake MODULE_LICENSE("GPL");
523df8bc08cSHitoshi Mitake MODULE_AUTHOR("Cluster Computing, Inc. Hitoshi Mitake");
524df8bc08cSHitoshi Mitake MODULE_DESCRIPTION("MC support for Intel X38 memory hub controllers");
525df8bc08cSHitoshi Mitake 
526df8bc08cSHitoshi Mitake module_param(edac_op_state, int, 0444);
527df8bc08cSHitoshi Mitake MODULE_PARM_DESC(edac_op_state, "EDAC Error Reporting state: 0=Poll,1=NMI");
528