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