xref: /openbmc/linux/drivers/edac/x38_edac.c (revision e2acc357ee9e77e543861b841aa47fc62c528543)
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>
17df8bc08cSHitoshi Mitake #include "edac_core.h"
18df8bc08cSHitoshi Mitake 
19df8bc08cSHitoshi Mitake #define X38_REVISION		"1.1"
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 
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 */
106df8bc08cSHitoshi Mitake 		debugf0("In single channel mode.\n");
107df8bc08cSHitoshi Mitake 		x38_channel_num = 1;
108df8bc08cSHitoshi Mitake 	} else {
109df8bc08cSHitoshi Mitake 		debugf0("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 
116df8bc08cSHitoshi Mitake static unsigned long eccerrlog_syndrome(u64 log)
117df8bc08cSHitoshi Mitake {
118df8bc08cSHitoshi Mitake 	return (log & X38_ECCERRLOG_SYNDROME_BITS) >> 16;
119df8bc08cSHitoshi Mitake }
120df8bc08cSHitoshi Mitake 
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 
150df8bc08cSHitoshi Mitake static void x38_clear_error_info(struct mem_ctl_info *mci)
151df8bc08cSHitoshi Mitake {
152df8bc08cSHitoshi Mitake 	struct pci_dev *pdev;
153df8bc08cSHitoshi Mitake 
154df8bc08cSHitoshi Mitake 	pdev = to_pci_dev(mci->dev);
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 
164df8bc08cSHitoshi Mitake static u64 x38_readq(const void __iomem *addr)
165df8bc08cSHitoshi Mitake {
166df8bc08cSHitoshi Mitake 	return readl(addr) | (((u64)readl(addr + 4)) << 32);
167df8bc08cSHitoshi Mitake }
168df8bc08cSHitoshi Mitake 
169df8bc08cSHitoshi Mitake static void x38_get_and_clear_error_info(struct mem_ctl_info *mci,
170df8bc08cSHitoshi Mitake 				 struct x38_error_info *info)
171df8bc08cSHitoshi Mitake {
172df8bc08cSHitoshi Mitake 	struct pci_dev *pdev;
173df8bc08cSHitoshi Mitake 	void __iomem *window = mci->pvt_info;
174df8bc08cSHitoshi Mitake 
175df8bc08cSHitoshi Mitake 	pdev = to_pci_dev(mci->dev);
176df8bc08cSHitoshi Mitake 
177df8bc08cSHitoshi Mitake 	/*
178df8bc08cSHitoshi Mitake 	 * This is a mess because there is no atomic way to read all the
179df8bc08cSHitoshi Mitake 	 * registers at once and the registers can transition from CE being
180df8bc08cSHitoshi Mitake 	 * overwritten by UE.
181df8bc08cSHitoshi Mitake 	 */
182df8bc08cSHitoshi Mitake 	pci_read_config_word(pdev, X38_ERRSTS, &info->errsts);
183df8bc08cSHitoshi Mitake 	if (!(info->errsts & X38_ERRSTS_BITS))
184df8bc08cSHitoshi Mitake 		return;
185df8bc08cSHitoshi Mitake 
186df8bc08cSHitoshi Mitake 	info->eccerrlog[0] = x38_readq(window + X38_C0ECCERRLOG);
187df8bc08cSHitoshi Mitake 	if (x38_channel_num == 2)
188df8bc08cSHitoshi Mitake 		info->eccerrlog[1] = x38_readq(window + X38_C1ECCERRLOG);
189df8bc08cSHitoshi Mitake 
190df8bc08cSHitoshi Mitake 	pci_read_config_word(pdev, X38_ERRSTS, &info->errsts2);
191df8bc08cSHitoshi Mitake 
192df8bc08cSHitoshi Mitake 	/*
193df8bc08cSHitoshi Mitake 	 * If the error is the same for both reads then the first set
194df8bc08cSHitoshi Mitake 	 * of reads is valid.  If there is a change then there is a CE
195df8bc08cSHitoshi Mitake 	 * with no info and the second set of reads is valid and
196df8bc08cSHitoshi Mitake 	 * should be UE info.
197df8bc08cSHitoshi Mitake 	 */
198df8bc08cSHitoshi Mitake 	if ((info->errsts ^ info->errsts2) & X38_ERRSTS_BITS) {
199df8bc08cSHitoshi Mitake 		info->eccerrlog[0] = x38_readq(window + X38_C0ECCERRLOG);
200df8bc08cSHitoshi Mitake 		if (x38_channel_num == 2)
201df8bc08cSHitoshi Mitake 			info->eccerrlog[1] =
202df8bc08cSHitoshi Mitake 				x38_readq(window + X38_C1ECCERRLOG);
203df8bc08cSHitoshi Mitake 	}
204df8bc08cSHitoshi Mitake 
205df8bc08cSHitoshi Mitake 	x38_clear_error_info(mci);
206df8bc08cSHitoshi Mitake }
207df8bc08cSHitoshi Mitake 
208df8bc08cSHitoshi Mitake static void x38_process_error_info(struct mem_ctl_info *mci,
209df8bc08cSHitoshi Mitake 				struct x38_error_info *info)
210df8bc08cSHitoshi Mitake {
211df8bc08cSHitoshi Mitake 	int channel;
212df8bc08cSHitoshi Mitake 	u64 log;
213df8bc08cSHitoshi Mitake 
214df8bc08cSHitoshi Mitake 	if (!(info->errsts & X38_ERRSTS_BITS))
215df8bc08cSHitoshi Mitake 		return;
216df8bc08cSHitoshi Mitake 
217df8bc08cSHitoshi Mitake 	if ((info->errsts ^ info->errsts2) & X38_ERRSTS_BITS) {
218*e2acc357SMauro Carvalho Chehab 		edac_mc_handle_error(HW_EVENT_ERR_UNCORRECTED, mci, 0, 0, 0,
219*e2acc357SMauro Carvalho Chehab 				     -1, -1, -1,
220*e2acc357SMauro Carvalho Chehab 				     "UE overwrote CE", "", NULL);
221df8bc08cSHitoshi Mitake 		info->errsts = info->errsts2;
222df8bc08cSHitoshi Mitake 	}
223df8bc08cSHitoshi Mitake 
224df8bc08cSHitoshi Mitake 	for (channel = 0; channel < x38_channel_num; channel++) {
225df8bc08cSHitoshi Mitake 		log = info->eccerrlog[channel];
226df8bc08cSHitoshi Mitake 		if (log & X38_ECCERRLOG_UE) {
227*e2acc357SMauro Carvalho Chehab 			edac_mc_handle_error(HW_EVENT_ERR_UNCORRECTED, mci,
228*e2acc357SMauro Carvalho Chehab 					     0, 0, 0,
229*e2acc357SMauro Carvalho Chehab 					     eccerrlog_row(channel, log),
230*e2acc357SMauro Carvalho Chehab 					     -1, -1,
231*e2acc357SMauro Carvalho Chehab 					     "x38 UE", "", NULL);
232df8bc08cSHitoshi Mitake 		} else if (log & X38_ECCERRLOG_CE) {
233*e2acc357SMauro Carvalho Chehab 			edac_mc_handle_error(HW_EVENT_ERR_CORRECTED, mci,
234*e2acc357SMauro Carvalho Chehab 					     0, 0, eccerrlog_syndrome(log),
235*e2acc357SMauro Carvalho Chehab 					     eccerrlog_row(channel, log),
236*e2acc357SMauro Carvalho Chehab 					     -1, -1,
237*e2acc357SMauro Carvalho Chehab 					     "x38 CE", "", NULL);
238df8bc08cSHitoshi Mitake 		}
239df8bc08cSHitoshi Mitake 	}
240df8bc08cSHitoshi Mitake }
241df8bc08cSHitoshi Mitake 
242df8bc08cSHitoshi Mitake static void x38_check(struct mem_ctl_info *mci)
243df8bc08cSHitoshi Mitake {
244df8bc08cSHitoshi Mitake 	struct x38_error_info info;
245df8bc08cSHitoshi Mitake 
246df8bc08cSHitoshi Mitake 	debugf1("MC%d: %s()\n", mci->mc_idx, __func__);
247df8bc08cSHitoshi Mitake 	x38_get_and_clear_error_info(mci, &info);
248df8bc08cSHitoshi Mitake 	x38_process_error_info(mci, &info);
249df8bc08cSHitoshi Mitake }
250df8bc08cSHitoshi Mitake 
251df8bc08cSHitoshi Mitake 
252df8bc08cSHitoshi Mitake void __iomem *x38_map_mchbar(struct pci_dev *pdev)
253df8bc08cSHitoshi Mitake {
254df8bc08cSHitoshi Mitake 	union {
255df8bc08cSHitoshi Mitake 		u64 mchbar;
256df8bc08cSHitoshi Mitake 		struct {
257df8bc08cSHitoshi Mitake 			u32 mchbar_low;
258df8bc08cSHitoshi Mitake 			u32 mchbar_high;
259df8bc08cSHitoshi Mitake 		};
260df8bc08cSHitoshi Mitake 	} u;
261df8bc08cSHitoshi Mitake 	void __iomem *window;
262df8bc08cSHitoshi Mitake 
263df8bc08cSHitoshi Mitake 	pci_read_config_dword(pdev, X38_MCHBAR_LOW, &u.mchbar_low);
264df8bc08cSHitoshi Mitake 	pci_write_config_dword(pdev, X38_MCHBAR_LOW, u.mchbar_low | 0x1);
265df8bc08cSHitoshi Mitake 	pci_read_config_dword(pdev, X38_MCHBAR_HIGH, &u.mchbar_high);
266df8bc08cSHitoshi Mitake 	u.mchbar &= X38_MCHBAR_MASK;
267df8bc08cSHitoshi Mitake 
268df8bc08cSHitoshi Mitake 	if (u.mchbar != (resource_size_t)u.mchbar) {
269df8bc08cSHitoshi Mitake 		printk(KERN_ERR
270df8bc08cSHitoshi Mitake 			"x38: mmio space beyond accessible range (0x%llx)\n",
271df8bc08cSHitoshi Mitake 			(unsigned long long)u.mchbar);
272df8bc08cSHitoshi Mitake 		return NULL;
273df8bc08cSHitoshi Mitake 	}
274df8bc08cSHitoshi Mitake 
275df8bc08cSHitoshi Mitake 	window = ioremap_nocache(u.mchbar, X38_MMR_WINDOW_SIZE);
276df8bc08cSHitoshi Mitake 	if (!window)
277df8bc08cSHitoshi Mitake 		printk(KERN_ERR "x38: cannot map mmio space at 0x%llx\n",
278df8bc08cSHitoshi Mitake 			(unsigned long long)u.mchbar);
279df8bc08cSHitoshi Mitake 
280df8bc08cSHitoshi Mitake 	return window;
281df8bc08cSHitoshi Mitake }
282df8bc08cSHitoshi Mitake 
283df8bc08cSHitoshi Mitake 
284df8bc08cSHitoshi Mitake static void x38_get_drbs(void __iomem *window,
285df8bc08cSHitoshi Mitake 			u16 drbs[X38_CHANNELS][X38_RANKS_PER_CHANNEL])
286df8bc08cSHitoshi Mitake {
287df8bc08cSHitoshi Mitake 	int i;
288df8bc08cSHitoshi Mitake 
289df8bc08cSHitoshi Mitake 	for (i = 0; i < X38_RANKS_PER_CHANNEL; i++) {
290df8bc08cSHitoshi Mitake 		drbs[0][i] = readw(window + X38_C0DRB + 2*i) & X38_DRB_MASK;
291df8bc08cSHitoshi Mitake 		drbs[1][i] = readw(window + X38_C1DRB + 2*i) & X38_DRB_MASK;
292df8bc08cSHitoshi Mitake 	}
293df8bc08cSHitoshi Mitake }
294df8bc08cSHitoshi Mitake 
295df8bc08cSHitoshi Mitake static bool x38_is_stacked(struct pci_dev *pdev,
296df8bc08cSHitoshi Mitake 			u16 drbs[X38_CHANNELS][X38_RANKS_PER_CHANNEL])
297df8bc08cSHitoshi Mitake {
298df8bc08cSHitoshi Mitake 	u16 tom;
299df8bc08cSHitoshi Mitake 
300df8bc08cSHitoshi Mitake 	pci_read_config_word(pdev, X38_TOM, &tom);
301df8bc08cSHitoshi Mitake 	tom &= X38_TOM_MASK;
302df8bc08cSHitoshi Mitake 
303df8bc08cSHitoshi Mitake 	return drbs[X38_CHANNELS - 1][X38_RANKS_PER_CHANNEL - 1] == tom;
304df8bc08cSHitoshi Mitake }
305df8bc08cSHitoshi Mitake 
306df8bc08cSHitoshi Mitake static unsigned long drb_to_nr_pages(
307df8bc08cSHitoshi Mitake 			u16 drbs[X38_CHANNELS][X38_RANKS_PER_CHANNEL],
308df8bc08cSHitoshi Mitake 			bool stacked, int channel, int rank)
309df8bc08cSHitoshi Mitake {
310df8bc08cSHitoshi Mitake 	int n;
311df8bc08cSHitoshi Mitake 
312df8bc08cSHitoshi Mitake 	n = drbs[channel][rank];
313df8bc08cSHitoshi Mitake 	if (rank > 0)
314df8bc08cSHitoshi Mitake 		n -= drbs[channel][rank - 1];
315df8bc08cSHitoshi Mitake 	if (stacked && (channel == 1) && drbs[channel][rank] ==
316df8bc08cSHitoshi Mitake 				drbs[channel][X38_RANKS_PER_CHANNEL - 1]) {
317df8bc08cSHitoshi Mitake 		n -= drbs[0][X38_RANKS_PER_CHANNEL - 1];
318df8bc08cSHitoshi Mitake 	}
319df8bc08cSHitoshi Mitake 
320df8bc08cSHitoshi Mitake 	n <<= (X38_DRB_SHIFT - PAGE_SHIFT);
321df8bc08cSHitoshi Mitake 	return n;
322df8bc08cSHitoshi Mitake }
323df8bc08cSHitoshi Mitake 
324df8bc08cSHitoshi Mitake static int x38_probe1(struct pci_dev *pdev, int dev_idx)
325df8bc08cSHitoshi Mitake {
326df8bc08cSHitoshi Mitake 	int rc;
327084a4fccSMauro Carvalho Chehab 	int i, j;
328df8bc08cSHitoshi Mitake 	struct mem_ctl_info *mci = NULL;
329*e2acc357SMauro Carvalho Chehab 	struct edac_mc_layer layers[2];
330df8bc08cSHitoshi Mitake 	u16 drbs[X38_CHANNELS][X38_RANKS_PER_CHANNEL];
331df8bc08cSHitoshi Mitake 	bool stacked;
332df8bc08cSHitoshi Mitake 	void __iomem *window;
333df8bc08cSHitoshi Mitake 
334df8bc08cSHitoshi Mitake 	debugf0("MC: %s()\n", __func__);
335df8bc08cSHitoshi Mitake 
336df8bc08cSHitoshi Mitake 	window = x38_map_mchbar(pdev);
337df8bc08cSHitoshi Mitake 	if (!window)
338df8bc08cSHitoshi Mitake 		return -ENODEV;
339df8bc08cSHitoshi Mitake 
340df8bc08cSHitoshi Mitake 	x38_get_drbs(window, drbs);
341df8bc08cSHitoshi Mitake 
342df8bc08cSHitoshi Mitake 	how_many_channel(pdev);
343df8bc08cSHitoshi Mitake 
344df8bc08cSHitoshi Mitake 	/* FIXME: unconventional pvt_info usage */
345*e2acc357SMauro Carvalho Chehab 	layers[0].type = EDAC_MC_LAYER_CHIP_SELECT;
346*e2acc357SMauro Carvalho Chehab 	layers[0].size = X38_RANKS;
347*e2acc357SMauro Carvalho Chehab 	layers[0].is_virt_csrow = true;
348*e2acc357SMauro Carvalho Chehab 	layers[1].type = EDAC_MC_LAYER_CHANNEL;
349*e2acc357SMauro Carvalho Chehab 	layers[1].size = x38_channel_num;
350*e2acc357SMauro Carvalho Chehab 	layers[1].is_virt_csrow = false;
351*e2acc357SMauro Carvalho Chehab 	mci = new_edac_mc_alloc(0, ARRAY_SIZE(layers), layers, 0);
352df8bc08cSHitoshi Mitake 	if (!mci)
353df8bc08cSHitoshi Mitake 		return -ENOMEM;
354df8bc08cSHitoshi Mitake 
355df8bc08cSHitoshi Mitake 	debugf3("MC: %s(): init mci\n", __func__);
356df8bc08cSHitoshi Mitake 
357df8bc08cSHitoshi Mitake 	mci->dev = &pdev->dev;
358df8bc08cSHitoshi Mitake 	mci->mtype_cap = MEM_FLAG_DDR2;
359df8bc08cSHitoshi Mitake 
360df8bc08cSHitoshi Mitake 	mci->edac_ctl_cap = EDAC_FLAG_SECDED;
361df8bc08cSHitoshi Mitake 	mci->edac_cap = EDAC_FLAG_SECDED;
362df8bc08cSHitoshi Mitake 
363df8bc08cSHitoshi Mitake 	mci->mod_name = EDAC_MOD_STR;
364df8bc08cSHitoshi Mitake 	mci->mod_ver = X38_REVISION;
365df8bc08cSHitoshi Mitake 	mci->ctl_name = x38_devs[dev_idx].ctl_name;
366df8bc08cSHitoshi Mitake 	mci->dev_name = pci_name(pdev);
367df8bc08cSHitoshi Mitake 	mci->edac_check = x38_check;
368df8bc08cSHitoshi Mitake 	mci->ctl_page_to_phys = NULL;
369df8bc08cSHitoshi Mitake 	mci->pvt_info = window;
370df8bc08cSHitoshi Mitake 
371df8bc08cSHitoshi Mitake 	stacked = x38_is_stacked(pdev, drbs);
372df8bc08cSHitoshi Mitake 
373df8bc08cSHitoshi Mitake 	/*
374df8bc08cSHitoshi Mitake 	 * The dram rank boundary (DRB) reg values are boundary addresses
375df8bc08cSHitoshi Mitake 	 * for each DRAM rank with a granularity of 64MB.  DRB regs are
376df8bc08cSHitoshi Mitake 	 * cumulative; the last one will contain the total memory
377df8bc08cSHitoshi Mitake 	 * contained in all ranks.
378df8bc08cSHitoshi Mitake 	 */
379df8bc08cSHitoshi Mitake 	for (i = 0; i < mci->nr_csrows; i++) {
380df8bc08cSHitoshi Mitake 		unsigned long nr_pages;
381df8bc08cSHitoshi Mitake 		struct csrow_info *csrow = &mci->csrows[i];
382df8bc08cSHitoshi Mitake 
383df8bc08cSHitoshi Mitake 		nr_pages = drb_to_nr_pages(drbs, stacked,
384df8bc08cSHitoshi Mitake 			i / X38_RANKS_PER_CHANNEL,
385df8bc08cSHitoshi Mitake 			i % X38_RANKS_PER_CHANNEL);
386df8bc08cSHitoshi Mitake 
387084a4fccSMauro Carvalho Chehab 		if (nr_pages == 0)
388df8bc08cSHitoshi Mitake 			continue;
389df8bc08cSHitoshi Mitake 
390084a4fccSMauro Carvalho Chehab 		for (j = 0; j < x38_channel_num; j++) {
391084a4fccSMauro Carvalho Chehab 			struct dimm_info *dimm = csrow->channels[j].dimm;
392a895bf8bSMauro Carvalho Chehab 
393a895bf8bSMauro Carvalho Chehab 			dimm->nr_pages = nr_pages / x38_channel_num;
394084a4fccSMauro Carvalho Chehab 			dimm->grain = nr_pages << PAGE_SHIFT;
395084a4fccSMauro Carvalho Chehab 			dimm->mtype = MEM_DDR2;
396084a4fccSMauro Carvalho Chehab 			dimm->dtype = DEV_UNKNOWN;
397084a4fccSMauro Carvalho Chehab 			dimm->edac_mode = EDAC_UNKNOWN;
398084a4fccSMauro Carvalho Chehab 		}
399df8bc08cSHitoshi Mitake 	}
400df8bc08cSHitoshi Mitake 
401df8bc08cSHitoshi Mitake 	x38_clear_error_info(mci);
402df8bc08cSHitoshi Mitake 
403df8bc08cSHitoshi Mitake 	rc = -ENODEV;
404df8bc08cSHitoshi Mitake 	if (edac_mc_add_mc(mci)) {
405df8bc08cSHitoshi Mitake 		debugf3("MC: %s(): failed edac_mc_add_mc()\n", __func__);
406df8bc08cSHitoshi Mitake 		goto fail;
407df8bc08cSHitoshi Mitake 	}
408df8bc08cSHitoshi Mitake 
409df8bc08cSHitoshi Mitake 	/* get this far and it's successful */
410df8bc08cSHitoshi Mitake 	debugf3("MC: %s(): success\n", __func__);
411df8bc08cSHitoshi Mitake 	return 0;
412df8bc08cSHitoshi Mitake 
413df8bc08cSHitoshi Mitake fail:
414df8bc08cSHitoshi Mitake 	iounmap(window);
415df8bc08cSHitoshi Mitake 	if (mci)
416df8bc08cSHitoshi Mitake 		edac_mc_free(mci);
417df8bc08cSHitoshi Mitake 
418df8bc08cSHitoshi Mitake 	return rc;
419df8bc08cSHitoshi Mitake }
420df8bc08cSHitoshi Mitake 
421df8bc08cSHitoshi Mitake static int __devinit x38_init_one(struct pci_dev *pdev,
422df8bc08cSHitoshi Mitake 				const struct pci_device_id *ent)
423df8bc08cSHitoshi Mitake {
424df8bc08cSHitoshi Mitake 	int rc;
425df8bc08cSHitoshi Mitake 
426df8bc08cSHitoshi Mitake 	debugf0("MC: %s()\n", __func__);
427df8bc08cSHitoshi Mitake 
428df8bc08cSHitoshi Mitake 	if (pci_enable_device(pdev) < 0)
429df8bc08cSHitoshi Mitake 		return -EIO;
430df8bc08cSHitoshi Mitake 
431df8bc08cSHitoshi Mitake 	rc = x38_probe1(pdev, ent->driver_data);
432df8bc08cSHitoshi Mitake 	if (!mci_pdev)
433df8bc08cSHitoshi Mitake 		mci_pdev = pci_dev_get(pdev);
434df8bc08cSHitoshi Mitake 
435df8bc08cSHitoshi Mitake 	return rc;
436df8bc08cSHitoshi Mitake }
437df8bc08cSHitoshi Mitake 
438df8bc08cSHitoshi Mitake static void __devexit x38_remove_one(struct pci_dev *pdev)
439df8bc08cSHitoshi Mitake {
440df8bc08cSHitoshi Mitake 	struct mem_ctl_info *mci;
441df8bc08cSHitoshi Mitake 
442df8bc08cSHitoshi Mitake 	debugf0("%s()\n", __func__);
443df8bc08cSHitoshi Mitake 
444df8bc08cSHitoshi Mitake 	mci = edac_mc_del_mc(&pdev->dev);
445df8bc08cSHitoshi Mitake 	if (!mci)
446df8bc08cSHitoshi Mitake 		return;
447df8bc08cSHitoshi Mitake 
448df8bc08cSHitoshi Mitake 	iounmap(mci->pvt_info);
449df8bc08cSHitoshi Mitake 
450df8bc08cSHitoshi Mitake 	edac_mc_free(mci);
451df8bc08cSHitoshi Mitake }
452df8bc08cSHitoshi Mitake 
45336c46f31SLionel Debroux static DEFINE_PCI_DEVICE_TABLE(x38_pci_tbl) = {
454df8bc08cSHitoshi Mitake 	{
455df8bc08cSHitoshi Mitake 	 PCI_VEND_DEV(INTEL, X38_HB), PCI_ANY_ID, PCI_ANY_ID, 0, 0,
456df8bc08cSHitoshi Mitake 	 X38},
457df8bc08cSHitoshi Mitake 	{
458df8bc08cSHitoshi Mitake 	 0,
459df8bc08cSHitoshi Mitake 	 }			/* 0 terminated list. */
460df8bc08cSHitoshi Mitake };
461df8bc08cSHitoshi Mitake 
462df8bc08cSHitoshi Mitake MODULE_DEVICE_TABLE(pci, x38_pci_tbl);
463df8bc08cSHitoshi Mitake 
464df8bc08cSHitoshi Mitake static struct pci_driver x38_driver = {
465df8bc08cSHitoshi Mitake 	.name = EDAC_MOD_STR,
466df8bc08cSHitoshi Mitake 	.probe = x38_init_one,
467df8bc08cSHitoshi Mitake 	.remove = __devexit_p(x38_remove_one),
468df8bc08cSHitoshi Mitake 	.id_table = x38_pci_tbl,
469df8bc08cSHitoshi Mitake };
470df8bc08cSHitoshi Mitake 
471df8bc08cSHitoshi Mitake static int __init x38_init(void)
472df8bc08cSHitoshi Mitake {
473df8bc08cSHitoshi Mitake 	int pci_rc;
474df8bc08cSHitoshi Mitake 
475df8bc08cSHitoshi Mitake 	debugf3("MC: %s()\n", __func__);
476df8bc08cSHitoshi Mitake 
477df8bc08cSHitoshi Mitake 	/* Ensure that the OPSTATE is set correctly for POLL or NMI */
478df8bc08cSHitoshi Mitake 	opstate_init();
479df8bc08cSHitoshi Mitake 
480df8bc08cSHitoshi Mitake 	pci_rc = pci_register_driver(&x38_driver);
481df8bc08cSHitoshi Mitake 	if (pci_rc < 0)
482df8bc08cSHitoshi Mitake 		goto fail0;
483df8bc08cSHitoshi Mitake 
484df8bc08cSHitoshi Mitake 	if (!mci_pdev) {
485df8bc08cSHitoshi Mitake 		x38_registered = 0;
486df8bc08cSHitoshi Mitake 		mci_pdev = pci_get_device(PCI_VENDOR_ID_INTEL,
487df8bc08cSHitoshi Mitake 					PCI_DEVICE_ID_INTEL_X38_HB, NULL);
488df8bc08cSHitoshi Mitake 		if (!mci_pdev) {
489df8bc08cSHitoshi Mitake 			debugf0("x38 pci_get_device fail\n");
490df8bc08cSHitoshi Mitake 			pci_rc = -ENODEV;
491df8bc08cSHitoshi Mitake 			goto fail1;
492df8bc08cSHitoshi Mitake 		}
493df8bc08cSHitoshi Mitake 
494df8bc08cSHitoshi Mitake 		pci_rc = x38_init_one(mci_pdev, x38_pci_tbl);
495df8bc08cSHitoshi Mitake 		if (pci_rc < 0) {
496df8bc08cSHitoshi Mitake 			debugf0("x38 init fail\n");
497df8bc08cSHitoshi Mitake 			pci_rc = -ENODEV;
498df8bc08cSHitoshi Mitake 			goto fail1;
499df8bc08cSHitoshi Mitake 		}
500df8bc08cSHitoshi Mitake 	}
501df8bc08cSHitoshi Mitake 
502df8bc08cSHitoshi Mitake 	return 0;
503df8bc08cSHitoshi Mitake 
504df8bc08cSHitoshi Mitake fail1:
505df8bc08cSHitoshi Mitake 	pci_unregister_driver(&x38_driver);
506df8bc08cSHitoshi Mitake 
507df8bc08cSHitoshi Mitake fail0:
508df8bc08cSHitoshi Mitake 	if (mci_pdev)
509df8bc08cSHitoshi Mitake 		pci_dev_put(mci_pdev);
510df8bc08cSHitoshi Mitake 
511df8bc08cSHitoshi Mitake 	return pci_rc;
512df8bc08cSHitoshi Mitake }
513df8bc08cSHitoshi Mitake 
514df8bc08cSHitoshi Mitake static void __exit x38_exit(void)
515df8bc08cSHitoshi Mitake {
516df8bc08cSHitoshi Mitake 	debugf3("MC: %s()\n", __func__);
517df8bc08cSHitoshi Mitake 
518df8bc08cSHitoshi Mitake 	pci_unregister_driver(&x38_driver);
519df8bc08cSHitoshi Mitake 	if (!x38_registered) {
520df8bc08cSHitoshi Mitake 		x38_remove_one(mci_pdev);
521df8bc08cSHitoshi Mitake 		pci_dev_put(mci_pdev);
522df8bc08cSHitoshi Mitake 	}
523df8bc08cSHitoshi Mitake }
524df8bc08cSHitoshi Mitake 
525df8bc08cSHitoshi Mitake module_init(x38_init);
526df8bc08cSHitoshi Mitake module_exit(x38_exit);
527df8bc08cSHitoshi Mitake 
528df8bc08cSHitoshi Mitake MODULE_LICENSE("GPL");
529df8bc08cSHitoshi Mitake MODULE_AUTHOR("Cluster Computing, Inc. Hitoshi Mitake");
530df8bc08cSHitoshi Mitake MODULE_DESCRIPTION("MC support for Intel X38 memory hub controllers");
531df8bc08cSHitoshi Mitake 
532df8bc08cSHitoshi Mitake module_param(edac_op_state, int, 0444);
533df8bc08cSHitoshi Mitake MODULE_PARM_DESC(edac_op_state, "EDAC Error Reporting state: 0=Poll,1=NMI");
534