xref: /openbmc/linux/drivers/edac/i82875p_edac.c (revision f30795fb)
10d88a10eSAlan Cox /*
20d88a10eSAlan Cox  * Intel D82875P Memory Controller kernel module
30d88a10eSAlan Cox  * (C) 2003 Linux Networx (http://lnxi.com)
40d88a10eSAlan Cox  * This file may be distributed under the terms of the
50d88a10eSAlan Cox  * GNU General Public License.
60d88a10eSAlan Cox  *
70d88a10eSAlan Cox  * Written by Thayne Harbaugh
80d88a10eSAlan Cox  * Contributors:
90d88a10eSAlan Cox  *	Wang Zhenyu at intel.com
100d88a10eSAlan Cox  *
110d88a10eSAlan Cox  * $Id: edac_i82875p.c,v 1.5.2.11 2005/10/05 00:43:44 dsp_llnl Exp $
120d88a10eSAlan Cox  *
130d88a10eSAlan Cox  * Note: E7210 appears same as D82875P - zhenyu.z.wang at intel.com
140d88a10eSAlan Cox  */
150d88a10eSAlan Cox 
160d88a10eSAlan Cox #include <linux/module.h>
170d88a10eSAlan Cox #include <linux/init.h>
180d88a10eSAlan Cox #include <linux/pci.h>
190d88a10eSAlan Cox #include <linux/pci_ids.h>
20c3c52bceSHitoshi Mitake #include <linux/edac.h>
2178d88e8aSMauro Carvalho Chehab #include "edac_module.h"
220d88a10eSAlan Cox 
23929a40ecSDoug Thompson #define EDAC_MOD_STR		"i82875p_edac"
2437f04581SDoug Thompson 
25537fba28SDave Peterson #define i82875p_printk(level, fmt, arg...) \
26537fba28SDave Peterson 	edac_printk(level, "i82875p", fmt, ##arg)
27537fba28SDave Peterson 
28537fba28SDave Peterson #define i82875p_mc_printk(mci, level, fmt, arg...) \
29537fba28SDave Peterson 	edac_mc_chipset_printk(mci, level, "i82875p", fmt, ##arg)
30537fba28SDave Peterson 
310d88a10eSAlan Cox #ifndef PCI_DEVICE_ID_INTEL_82875_0
320d88a10eSAlan Cox #define PCI_DEVICE_ID_INTEL_82875_0	0x2578
330d88a10eSAlan Cox #endif				/* PCI_DEVICE_ID_INTEL_82875_0 */
340d88a10eSAlan Cox 
350d88a10eSAlan Cox #ifndef PCI_DEVICE_ID_INTEL_82875_6
360d88a10eSAlan Cox #define PCI_DEVICE_ID_INTEL_82875_6	0x257e
370d88a10eSAlan Cox #endif				/* PCI_DEVICE_ID_INTEL_82875_6 */
380d88a10eSAlan Cox 
390d88a10eSAlan Cox /* four csrows in dual channel, eight in single channel */
400a8a9ac9SMauro Carvalho Chehab #define I82875P_NR_DIMMS		8
410a8a9ac9SMauro Carvalho Chehab #define I82875P_NR_CSROWS(nr_chans)	(I82875P_NR_DIMMS / (nr_chans))
420d88a10eSAlan Cox 
430d88a10eSAlan Cox /* Intel 82875p register addresses - device 0 function 0 - DRAM Controller */
440d88a10eSAlan Cox #define I82875P_EAP		0x58	/* Error Address Pointer (32b)
450d88a10eSAlan Cox 					 *
460d88a10eSAlan Cox 					 * 31:12 block address
470d88a10eSAlan Cox 					 * 11:0  reserved
480d88a10eSAlan Cox 					 */
490d88a10eSAlan Cox 
500d88a10eSAlan Cox #define I82875P_DERRSYN		0x5c	/* DRAM Error Syndrome (8b)
510d88a10eSAlan Cox 					 *
520d88a10eSAlan Cox 					 *  7:0  DRAM ECC Syndrome
530d88a10eSAlan Cox 					 */
540d88a10eSAlan Cox 
550d88a10eSAlan Cox #define I82875P_DES		0x5d	/* DRAM Error Status (8b)
560d88a10eSAlan Cox 					 *
570d88a10eSAlan Cox 					 *  7:1  reserved
580d88a10eSAlan Cox 					 *  0    Error channel 0/1
590d88a10eSAlan Cox 					 */
600d88a10eSAlan Cox 
610d88a10eSAlan Cox #define I82875P_ERRSTS		0xc8	/* Error Status Register (16b)
620d88a10eSAlan Cox 					 *
630d88a10eSAlan Cox 					 * 15:10 reserved
640d88a10eSAlan Cox 					 *  9    non-DRAM lock error (ndlock)
650d88a10eSAlan Cox 					 *  8    Sftwr Generated SMI
660d88a10eSAlan Cox 					 *  7    ECC UE
670d88a10eSAlan Cox 					 *  6    reserved
680d88a10eSAlan Cox 					 *  5    MCH detects unimplemented cycle
690d88a10eSAlan Cox 					 *  4    AGP access outside GA
700d88a10eSAlan Cox 					 *  3    Invalid AGP access
710d88a10eSAlan Cox 					 *  2    Invalid GA translation table
720d88a10eSAlan Cox 					 *  1    Unsupported AGP command
730d88a10eSAlan Cox 					 *  0    ECC CE
740d88a10eSAlan Cox 					 */
750d88a10eSAlan Cox 
760d88a10eSAlan Cox #define I82875P_ERRCMD		0xca	/* Error Command (16b)
770d88a10eSAlan Cox 					 *
780d88a10eSAlan Cox 					 * 15:10 reserved
790d88a10eSAlan Cox 					 *  9    SERR on non-DRAM lock
800d88a10eSAlan Cox 					 *  8    SERR on ECC UE
810d88a10eSAlan Cox 					 *  7    SERR on ECC CE
820d88a10eSAlan Cox 					 *  6    target abort on high exception
830d88a10eSAlan Cox 					 *  5    detect unimplemented cyc
840d88a10eSAlan Cox 					 *  4    AGP access outside of GA
850d88a10eSAlan Cox 					 *  3    SERR on invalid AGP access
860d88a10eSAlan Cox 					 *  2    invalid translation table
870d88a10eSAlan Cox 					 *  1    SERR on unsupported AGP command
880d88a10eSAlan Cox 					 *  0    reserved
890d88a10eSAlan Cox 					 */
900d88a10eSAlan Cox 
910d88a10eSAlan Cox /* Intel 82875p register addresses - device 6 function 0 - DRAM Controller */
920d88a10eSAlan Cox #define I82875P_PCICMD6		0x04	/* PCI Command Register (16b)
930d88a10eSAlan Cox 					 *
940d88a10eSAlan Cox 					 * 15:10 reserved
950d88a10eSAlan Cox 					 *  9    fast back-to-back - ro 0
960d88a10eSAlan Cox 					 *  8    SERR enable - ro 0
970d88a10eSAlan Cox 					 *  7    addr/data stepping - ro 0
980d88a10eSAlan Cox 					 *  6    parity err enable - ro 0
990d88a10eSAlan Cox 					 *  5    VGA palette snoop - ro 0
1000d88a10eSAlan Cox 					 *  4    mem wr & invalidate - ro 0
1010d88a10eSAlan Cox 					 *  3    special cycle - ro 0
1020d88a10eSAlan Cox 					 *  2    bus master - ro 0
1030d88a10eSAlan Cox 					 *  1    mem access dev6 - 0(dis),1(en)
1040d88a10eSAlan Cox 					 *  0    IO access dev3 - 0(dis),1(en)
1050d88a10eSAlan Cox 					 */
1060d88a10eSAlan Cox 
1070d88a10eSAlan Cox #define I82875P_BAR6		0x10	/* Mem Delays Base ADDR Reg (32b)
1080d88a10eSAlan Cox 					 *
1090d88a10eSAlan Cox 					 * 31:12 mem base addr [31:12]
1100d88a10eSAlan Cox 					 * 11:4  address mask - ro 0
1110d88a10eSAlan Cox 					 *  3    prefetchable - ro 0(non),1(pre)
1120d88a10eSAlan Cox 					 *  2:1  mem type - ro 0
1130d88a10eSAlan Cox 					 *  0    mem space - ro 0
1140d88a10eSAlan Cox 					 */
1150d88a10eSAlan Cox 
1160d88a10eSAlan Cox /* Intel 82875p MMIO register space - device 0 function 0 - MMR space */
1170d88a10eSAlan Cox 
1180d88a10eSAlan Cox #define I82875P_DRB_SHIFT 26	/* 64MiB grain */
1190d88a10eSAlan Cox #define I82875P_DRB		0x00	/* DRAM Row Boundary (8b x 8)
1200d88a10eSAlan Cox 					 *
1210d88a10eSAlan Cox 					 *  7    reserved
1220d88a10eSAlan Cox 					 *  6:0  64MiB row boundary addr
1230d88a10eSAlan Cox 					 */
1240d88a10eSAlan Cox 
1250d88a10eSAlan Cox #define I82875P_DRA		0x10	/* DRAM Row Attribute (4b x 8)
1260d88a10eSAlan Cox 					 *
1270d88a10eSAlan Cox 					 *  7    reserved
1280d88a10eSAlan Cox 					 *  6:4  row attr row 1
1290d88a10eSAlan Cox 					 *  3    reserved
1300d88a10eSAlan Cox 					 *  2:0  row attr row 0
1310d88a10eSAlan Cox 					 *
1320d88a10eSAlan Cox 					 * 000 =  4KiB
1330d88a10eSAlan Cox 					 * 001 =  8KiB
1340d88a10eSAlan Cox 					 * 010 = 16KiB
1350d88a10eSAlan Cox 					 * 011 = 32KiB
1360d88a10eSAlan Cox 					 */
1370d88a10eSAlan Cox 
1380d88a10eSAlan Cox #define I82875P_DRC		0x68	/* DRAM Controller Mode (32b)
1390d88a10eSAlan Cox 					 *
1400d88a10eSAlan Cox 					 * 31:30 reserved
1410d88a10eSAlan Cox 					 * 29    init complete
1420d88a10eSAlan Cox 					 * 28:23 reserved
1430d88a10eSAlan Cox 					 * 22:21 nr chan 00=1,01=2
1440d88a10eSAlan Cox 					 * 20    reserved
1450d88a10eSAlan Cox 					 * 19:18 Data Integ Mode 00=none,01=ecc
1460d88a10eSAlan Cox 					 * 17:11 reserved
1470d88a10eSAlan Cox 					 * 10:8  refresh mode
1480d88a10eSAlan Cox 					 *  7    reserved
1490d88a10eSAlan Cox 					 *  6:4  mode select
1500d88a10eSAlan Cox 					 *  3:2  reserved
1510d88a10eSAlan Cox 					 *  1:0  DRAM type 01=DDR
1520d88a10eSAlan Cox 					 */
1530d88a10eSAlan Cox 
1540d88a10eSAlan Cox enum i82875p_chips {
1550d88a10eSAlan Cox 	I82875P = 0,
1560d88a10eSAlan Cox };
1570d88a10eSAlan Cox 
1580d88a10eSAlan Cox struct i82875p_pvt {
1590d88a10eSAlan Cox 	struct pci_dev *ovrfl_pdev;
1606d57348dSAl Viro 	void __iomem *ovrfl_window;
1610d88a10eSAlan Cox };
1620d88a10eSAlan Cox 
1630d88a10eSAlan Cox struct i82875p_dev_info {
1640d88a10eSAlan Cox 	const char *ctl_name;
1650d88a10eSAlan Cox };
1660d88a10eSAlan Cox 
1670d88a10eSAlan Cox struct i82875p_error_info {
1680d88a10eSAlan Cox 	u16 errsts;
1690d88a10eSAlan Cox 	u32 eap;
1700d88a10eSAlan Cox 	u8 des;
1710d88a10eSAlan Cox 	u8 derrsyn;
1720d88a10eSAlan Cox 	u16 errsts2;
1730d88a10eSAlan Cox };
1740d88a10eSAlan Cox 
1750d88a10eSAlan Cox static const struct i82875p_dev_info i82875p_devs[] = {
1760d88a10eSAlan Cox 	[I82875P] = {
177466b71d5SDave Jiang 		.ctl_name = "i82875p"},
1780d88a10eSAlan Cox };
1790d88a10eSAlan Cox 
180f044091cSDouglas Thompson static struct pci_dev *mci_pdev;	/* init dev: in case that AGP code has
181e7ecd891SDave Peterson 					 * already registered driver
182e7ecd891SDave Peterson 					 */
183e7ecd891SDave Peterson 
184456a2f95SDave Jiang static struct edac_pci_ctl_info *i82875p_pci;
185456a2f95SDave Jiang 
i82875p_get_error_info(struct mem_ctl_info * mci,struct i82875p_error_info * info)1860d88a10eSAlan Cox static void i82875p_get_error_info(struct mem_ctl_info *mci,
1870d88a10eSAlan Cox 				struct i82875p_error_info *info)
1880d88a10eSAlan Cox {
18937f04581SDoug Thompson 	struct pci_dev *pdev;
19037f04581SDoug Thompson 
191fd687502SMauro Carvalho Chehab 	pdev = to_pci_dev(mci->pdev);
19237f04581SDoug Thompson 
1930d88a10eSAlan Cox 	/*
1940d88a10eSAlan Cox 	 * This is a mess because there is no atomic way to read all the
1950d88a10eSAlan Cox 	 * registers at once and the registers can transition from CE being
1960d88a10eSAlan Cox 	 * overwritten by UE.
1970d88a10eSAlan Cox 	 */
19837f04581SDoug Thompson 	pci_read_config_word(pdev, I82875P_ERRSTS, &info->errsts);
199654ede20SJason Uhlenkott 
200654ede20SJason Uhlenkott 	if (!(info->errsts & 0x0081))
201654ede20SJason Uhlenkott 		return;
202654ede20SJason Uhlenkott 
20337f04581SDoug Thompson 	pci_read_config_dword(pdev, I82875P_EAP, &info->eap);
20437f04581SDoug Thompson 	pci_read_config_byte(pdev, I82875P_DES, &info->des);
20537f04581SDoug Thompson 	pci_read_config_byte(pdev, I82875P_DERRSYN, &info->derrsyn);
20637f04581SDoug Thompson 	pci_read_config_word(pdev, I82875P_ERRSTS, &info->errsts2);
2070d88a10eSAlan Cox 
2080d88a10eSAlan Cox 	/*
2090d88a10eSAlan Cox 	 * If the error is the same then we can for both reads then
2100d88a10eSAlan Cox 	 * the first set of reads is valid.  If there is a change then
2110d88a10eSAlan Cox 	 * there is a CE no info and the second set of reads is valid
2120d88a10eSAlan Cox 	 * and should be UE info.
2130d88a10eSAlan Cox 	 */
2140d88a10eSAlan Cox 	if ((info->errsts ^ info->errsts2) & 0x0081) {
21537f04581SDoug Thompson 		pci_read_config_dword(pdev, I82875P_EAP, &info->eap);
21637f04581SDoug Thompson 		pci_read_config_byte(pdev, I82875P_DES, &info->des);
217466b71d5SDave Jiang 		pci_read_config_byte(pdev, I82875P_DERRSYN, &info->derrsyn);
2180d88a10eSAlan Cox 	}
219654ede20SJason Uhlenkott 
220654ede20SJason Uhlenkott 	pci_write_bits16(pdev, I82875P_ERRSTS, 0x0081, 0x0081);
2210d88a10eSAlan Cox }
2220d88a10eSAlan Cox 
i82875p_process_error_info(struct mem_ctl_info * mci,struct i82875p_error_info * info,int handle_errors)2230d88a10eSAlan Cox static int i82875p_process_error_info(struct mem_ctl_info *mci,
224466b71d5SDave Jiang 				struct i82875p_error_info *info,
225466b71d5SDave Jiang 				int handle_errors)
2260d88a10eSAlan Cox {
2270d88a10eSAlan Cox 	int row, multi_chan;
2280d88a10eSAlan Cox 
229de3910ebSMauro Carvalho Chehab 	multi_chan = mci->csrows[0]->nr_channels - 1;
2300d88a10eSAlan Cox 
231654ede20SJason Uhlenkott 	if (!(info->errsts & 0x0081))
2320d88a10eSAlan Cox 		return 0;
2330d88a10eSAlan Cox 
2340d88a10eSAlan Cox 	if (!handle_errors)
2350d88a10eSAlan Cox 		return 1;
2360d88a10eSAlan Cox 
2370d88a10eSAlan Cox 	if ((info->errsts ^ info->errsts2) & 0x0081) {
2389eb07a7fSMauro Carvalho Chehab 		edac_mc_handle_error(HW_EVENT_ERR_UNCORRECTED, mci, 1, 0, 0, 0,
2390a8a9ac9SMauro Carvalho Chehab 				     -1, -1, -1,
24003f7eae8SMauro Carvalho Chehab 				     "UE overwrote CE", "");
2410d88a10eSAlan Cox 		info->errsts = info->errsts2;
2420d88a10eSAlan Cox 	}
2430d88a10eSAlan Cox 
2440d88a10eSAlan Cox 	info->eap >>= PAGE_SHIFT;
2450d88a10eSAlan Cox 	row = edac_mc_find_csrow_by_page(mci, info->eap);
2460d88a10eSAlan Cox 
2470d88a10eSAlan Cox 	if (info->errsts & 0x0080)
2489eb07a7fSMauro Carvalho Chehab 		edac_mc_handle_error(HW_EVENT_ERR_UNCORRECTED, mci, 1,
2490a8a9ac9SMauro Carvalho Chehab 				     info->eap, 0, 0,
2500a8a9ac9SMauro Carvalho Chehab 				     row, -1, -1,
25103f7eae8SMauro Carvalho Chehab 				     "i82875p UE", "");
2520d88a10eSAlan Cox 	else
2539eb07a7fSMauro Carvalho Chehab 		edac_mc_handle_error(HW_EVENT_ERR_CORRECTED, mci, 1,
2540a8a9ac9SMauro Carvalho Chehab 				     info->eap, 0, info->derrsyn,
2550a8a9ac9SMauro Carvalho Chehab 				     row, multi_chan ? (info->des & 0x1) : 0,
25603f7eae8SMauro Carvalho Chehab 				     -1, "i82875p CE", "");
2570d88a10eSAlan Cox 
2580d88a10eSAlan Cox 	return 1;
2590d88a10eSAlan Cox }
2600d88a10eSAlan Cox 
i82875p_check(struct mem_ctl_info * mci)2610d88a10eSAlan Cox static void i82875p_check(struct mem_ctl_info *mci)
2620d88a10eSAlan Cox {
2630d88a10eSAlan Cox 	struct i82875p_error_info info;
2640d88a10eSAlan Cox 
2650d88a10eSAlan Cox 	i82875p_get_error_info(mci, &info);
2660d88a10eSAlan Cox 	i82875p_process_error_info(mci, &info, 1);
2670d88a10eSAlan Cox }
2680d88a10eSAlan Cox 
26913189525SDoug Thompson /* Return 0 on success or 1 on failure. */
i82875p_setup_overfl_dev(struct pci_dev * pdev,struct pci_dev ** ovrfl_pdev,void __iomem ** ovrfl_window)27013189525SDoug Thompson static int i82875p_setup_overfl_dev(struct pci_dev *pdev,
271466b71d5SDave Jiang 				struct pci_dev **ovrfl_pdev,
272466b71d5SDave Jiang 				void __iomem **ovrfl_window)
2730d88a10eSAlan Cox {
27413189525SDoug Thompson 	struct pci_dev *dev;
27513189525SDoug Thompson 	void __iomem *window;
2760d88a10eSAlan Cox 
27713189525SDoug Thompson 	*ovrfl_pdev = NULL;
27813189525SDoug Thompson 	*ovrfl_window = NULL;
27913189525SDoug Thompson 	dev = pci_get_device(PCI_VEND_DEV(INTEL, 82875_6), NULL);
2800d88a10eSAlan Cox 
28113189525SDoug Thompson 	if (dev == NULL) {
28213189525SDoug Thompson 		/* Intel tells BIOS developers to hide device 6 which
2830d88a10eSAlan Cox 		 * configures the overflow device access containing
2840d88a10eSAlan Cox 		 * the DRBs - this is where we expose device 6.
2850d88a10eSAlan Cox 		 * http://www.x86-secret.com/articles/tweak/pat/patsecrets-2.htm
2860d88a10eSAlan Cox 		 */
2870d88a10eSAlan Cox 		pci_write_bits8(pdev, 0xf4, 0x2, 0x2);
28813189525SDoug Thompson 		dev = pci_scan_single_device(pdev->bus, PCI_DEVFN(6, 0));
289e7ecd891SDave Peterson 
29013189525SDoug Thompson 		if (dev == NULL)
29113189525SDoug Thompson 			return 1;
29262456726SJohn Feeney 
293307d1144SJarkko Lavinen 		pci_bus_assign_resources(dev->bus);
294617b4157SBjorn Helgaas 		pci_bus_add_device(dev);
2950d88a10eSAlan Cox 	}
296e7ecd891SDave Peterson 
29713189525SDoug Thompson 	*ovrfl_pdev = dev;
29813189525SDoug Thompson 
29913189525SDoug Thompson 	if (pci_enable_device(dev)) {
30013189525SDoug Thompson 		i82875p_printk(KERN_ERR, "%s(): Failed to enable overflow "
30113189525SDoug Thompson 			"device\n", __func__);
30213189525SDoug Thompson 		return 1;
3030d88a10eSAlan Cox 	}
3040d88a10eSAlan Cox 
30513189525SDoug Thompson 	if (pci_request_regions(dev, pci_name(dev))) {
3060d88a10eSAlan Cox #ifdef CORRECT_BIOS
307637beb69SDave Peterson 		goto fail0;
3080d88a10eSAlan Cox #endif
3090d88a10eSAlan Cox 	}
310e7ecd891SDave Peterson 
3110d88a10eSAlan Cox 	/* cache is irrelevant for PCI bus reads/writes */
3121dca00bdSArjan van de Ven 	window = pci_ioremap_bar(dev, 0);
31313189525SDoug Thompson 	if (window == NULL) {
314537fba28SDave Peterson 		i82875p_printk(KERN_ERR, "%s(): Failed to ioremap bar6\n",
315537fba28SDave Peterson 			__func__);
316637beb69SDave Peterson 		goto fail1;
3170d88a10eSAlan Cox 	}
3180d88a10eSAlan Cox 
31913189525SDoug Thompson 	*ovrfl_window = window;
32013189525SDoug Thompson 	return 0;
3210d88a10eSAlan Cox 
32213189525SDoug Thompson fail1:
32313189525SDoug Thompson 	pci_release_regions(dev);
3240d88a10eSAlan Cox 
32513189525SDoug Thompson #ifdef CORRECT_BIOS
32613189525SDoug Thompson fail0:
32713189525SDoug Thompson 	pci_disable_device(dev);
32813189525SDoug Thompson #endif
32913189525SDoug Thompson 	/* NOTE: the ovrfl proc entry and pci_dev are intentionally left */
33013189525SDoug Thompson 	return 1;
3310d88a10eSAlan Cox }
3320d88a10eSAlan Cox 
33313189525SDoug Thompson /* Return 1 if dual channel mode is active.  Else return 0. */
dual_channel_active(u32 drc)33413189525SDoug Thompson static inline int dual_channel_active(u32 drc)
33513189525SDoug Thompson {
33613189525SDoug Thompson 	return (drc >> 21) & 0x1;
33713189525SDoug Thompson }
3380d88a10eSAlan Cox 
i82875p_init_csrows(struct mem_ctl_info * mci,struct pci_dev * pdev,void __iomem * ovrfl_window,u32 drc)33913189525SDoug Thompson static void i82875p_init_csrows(struct mem_ctl_info *mci,
340466b71d5SDave Jiang 				struct pci_dev *pdev,
341466b71d5SDave Jiang 				void __iomem * ovrfl_window, u32 drc)
34213189525SDoug Thompson {
34313189525SDoug Thompson 	struct csrow_info *csrow;
344084a4fccSMauro Carvalho Chehab 	struct dimm_info *dimm;
345084a4fccSMauro Carvalho Chehab 	unsigned nr_chans = dual_channel_active(drc) + 1;
34613189525SDoug Thompson 	unsigned long last_cumul_size;
34713189525SDoug Thompson 	u8 value;
34813189525SDoug Thompson 	u32 drc_ddim;		/* DRAM Data Integrity Mode 0=none,2=edac */
349a895bf8bSMauro Carvalho Chehab 	u32 cumul_size, nr_pages;
350084a4fccSMauro Carvalho Chehab 	int index, j;
35113189525SDoug Thompson 
35213189525SDoug Thompson 	drc_ddim = (drc >> 18) & 0x1;
35313189525SDoug Thompson 	last_cumul_size = 0;
35413189525SDoug Thompson 
35513189525SDoug Thompson 	/* The dram row boundary (DRB) reg values are boundary address
3560d88a10eSAlan Cox 	 * for each DRAM row with a granularity of 32 or 64MB (single/dual
3570d88a10eSAlan Cox 	 * channel operation).  DRB regs are cumulative; therefore DRB7 will
3580d88a10eSAlan Cox 	 * contain the total memory contained in all eight rows.
3590d88a10eSAlan Cox 	 */
36013189525SDoug Thompson 
36113189525SDoug Thompson 	for (index = 0; index < mci->nr_csrows; index++) {
362de3910ebSMauro Carvalho Chehab 		csrow = mci->csrows[index];
3630d88a10eSAlan Cox 
3640d88a10eSAlan Cox 		value = readb(ovrfl_window + I82875P_DRB + index);
3650d88a10eSAlan Cox 		cumul_size = value << (I82875P_DRB_SHIFT - PAGE_SHIFT);
366956b9ba1SJoe Perches 		edac_dbg(3, "(%d) cumul_size 0x%x\n", index, cumul_size);
3670d88a10eSAlan Cox 		if (cumul_size == last_cumul_size)
3680d88a10eSAlan Cox 			continue;	/* not populated */
3690d88a10eSAlan Cox 
3700d88a10eSAlan Cox 		csrow->first_page = last_cumul_size;
3710d88a10eSAlan Cox 		csrow->last_page = cumul_size - 1;
372a895bf8bSMauro Carvalho Chehab 		nr_pages = cumul_size - last_cumul_size;
3730d88a10eSAlan Cox 		last_cumul_size = cumul_size;
374084a4fccSMauro Carvalho Chehab 
375084a4fccSMauro Carvalho Chehab 		for (j = 0; j < nr_chans; j++) {
376de3910ebSMauro Carvalho Chehab 			dimm = csrow->channels[j]->dimm;
377084a4fccSMauro Carvalho Chehab 
378a895bf8bSMauro Carvalho Chehab 			dimm->nr_pages = nr_pages / nr_chans;
379084a4fccSMauro Carvalho Chehab 			dimm->grain = 1 << 12;	/* I82875P_EAP has 4KiB reolution */
380084a4fccSMauro Carvalho Chehab 			dimm->mtype = MEM_DDR;
381084a4fccSMauro Carvalho Chehab 			dimm->dtype = DEV_UNKNOWN;
382084a4fccSMauro Carvalho Chehab 			dimm->edac_mode = drc_ddim ? EDAC_SECDED : EDAC_NONE;
383084a4fccSMauro Carvalho Chehab 		}
3840d88a10eSAlan Cox 	}
38513189525SDoug Thompson }
3860d88a10eSAlan Cox 
i82875p_probe1(struct pci_dev * pdev,int dev_idx)38713189525SDoug Thompson static int i82875p_probe1(struct pci_dev *pdev, int dev_idx)
38813189525SDoug Thompson {
38913189525SDoug Thompson 	int rc = -ENODEV;
39013189525SDoug Thompson 	struct mem_ctl_info *mci;
3910a8a9ac9SMauro Carvalho Chehab 	struct edac_mc_layer layers[2];
39213189525SDoug Thompson 	struct i82875p_pvt *pvt;
39313189525SDoug Thompson 	struct pci_dev *ovrfl_pdev;
39413189525SDoug Thompson 	void __iomem *ovrfl_window;
39513189525SDoug Thompson 	u32 drc;
39613189525SDoug Thompson 	u32 nr_chans;
39713189525SDoug Thompson 	struct i82875p_error_info discard;
39813189525SDoug Thompson 
399956b9ba1SJoe Perches 	edac_dbg(0, "\n");
400c3c52bceSHitoshi Mitake 
40113189525SDoug Thompson 	if (i82875p_setup_overfl_dev(pdev, &ovrfl_pdev, &ovrfl_window))
40213189525SDoug Thompson 		return -ENODEV;
40313189525SDoug Thompson 	drc = readl(ovrfl_window + I82875P_DRC);
40413189525SDoug Thompson 	nr_chans = dual_channel_active(drc) + 1;
40513189525SDoug Thompson 
4060a8a9ac9SMauro Carvalho Chehab 	layers[0].type = EDAC_MC_LAYER_CHIP_SELECT;
4070a8a9ac9SMauro Carvalho Chehab 	layers[0].size = I82875P_NR_CSROWS(nr_chans);
4080a8a9ac9SMauro Carvalho Chehab 	layers[0].is_virt_csrow = true;
4090a8a9ac9SMauro Carvalho Chehab 	layers[1].type = EDAC_MC_LAYER_CHANNEL;
4100a8a9ac9SMauro Carvalho Chehab 	layers[1].size = nr_chans;
4110a8a9ac9SMauro Carvalho Chehab 	layers[1].is_virt_csrow = false;
412ca0907b9SMauro Carvalho Chehab 	mci = edac_mc_alloc(0, ARRAY_SIZE(layers), layers, sizeof(*pvt));
41313189525SDoug Thompson 	if (!mci) {
41413189525SDoug Thompson 		rc = -ENOMEM;
41513189525SDoug Thompson 		goto fail0;
41613189525SDoug Thompson 	}
41713189525SDoug Thompson 
418956b9ba1SJoe Perches 	edac_dbg(3, "init mci\n");
419fd687502SMauro Carvalho Chehab 	mci->pdev = &pdev->dev;
42013189525SDoug Thompson 	mci->mtype_cap = MEM_FLAG_DDR;
42113189525SDoug Thompson 	mci->edac_ctl_cap = EDAC_FLAG_NONE | EDAC_FLAG_SECDED;
42213189525SDoug Thompson 	mci->edac_cap = EDAC_FLAG_UNKNOWN;
42313189525SDoug Thompson 	mci->mod_name = EDAC_MOD_STR;
42413189525SDoug Thompson 	mci->ctl_name = i82875p_devs[dev_idx].ctl_name;
425c4192705SDave Jiang 	mci->dev_name = pci_name(pdev);
42613189525SDoug Thompson 	mci->edac_check = i82875p_check;
42713189525SDoug Thompson 	mci->ctl_page_to_phys = NULL;
428956b9ba1SJoe Perches 	edac_dbg(3, "init pvt\n");
42913189525SDoug Thompson 	pvt = (struct i82875p_pvt *)mci->pvt_info;
43013189525SDoug Thompson 	pvt->ovrfl_pdev = ovrfl_pdev;
43113189525SDoug Thompson 	pvt->ovrfl_window = ovrfl_window;
43213189525SDoug Thompson 	i82875p_init_csrows(mci, pdev, ovrfl_window, drc);
433749ede57SDave Peterson 	i82875p_get_error_info(mci, &discard);	/* clear counters */
4340d88a10eSAlan Cox 
4352d7bbb91SDoug Thompson 	/* Here we assume that we will never see multiple instances of this
4362d7bbb91SDoug Thompson 	 * type of memory controller.  The ID is therefore hardcoded to 0.
4372d7bbb91SDoug Thompson 	 */
438b8f6f975SDoug Thompson 	if (edac_mc_add_mc(mci)) {
439956b9ba1SJoe Perches 		edac_dbg(3, "failed edac_mc_add_mc()\n");
44013189525SDoug Thompson 		goto fail1;
4410d88a10eSAlan Cox 	}
4420d88a10eSAlan Cox 
443456a2f95SDave Jiang 	/* allocating generic PCI control info */
444456a2f95SDave Jiang 	i82875p_pci = edac_pci_create_generic_ctl(&pdev->dev, EDAC_MOD_STR);
445456a2f95SDave Jiang 	if (!i82875p_pci) {
446456a2f95SDave Jiang 		printk(KERN_WARNING
447456a2f95SDave Jiang 			"%s(): Unable to create PCI control\n",
448456a2f95SDave Jiang 			__func__);
449456a2f95SDave Jiang 		printk(KERN_WARNING
450456a2f95SDave Jiang 			"%s(): PCI error report via EDAC not setup\n",
451456a2f95SDave Jiang 			__func__);
452456a2f95SDave Jiang 	}
453456a2f95SDave Jiang 
4540d88a10eSAlan Cox 	/* get this far and it's successful */
455956b9ba1SJoe Perches 	edac_dbg(3, "success\n");
4560d88a10eSAlan Cox 	return 0;
4570d88a10eSAlan Cox 
45813189525SDoug Thompson fail1:
4590d88a10eSAlan Cox 	edac_mc_free(mci);
4600d88a10eSAlan Cox 
46113189525SDoug Thompson fail0:
4620d88a10eSAlan Cox 	iounmap(ovrfl_window);
4630d88a10eSAlan Cox 	pci_release_regions(ovrfl_pdev);
4640d88a10eSAlan Cox 
465637beb69SDave Peterson 	pci_disable_device(ovrfl_pdev);
4660d88a10eSAlan Cox 	/* NOTE: the ovrfl proc entry and pci_dev are intentionally left */
4670d88a10eSAlan Cox 	return rc;
4680d88a10eSAlan Cox }
4690d88a10eSAlan Cox 
4700d88a10eSAlan Cox /* returns count (>= 0), or negative on error */
i82875p_init_one(struct pci_dev * pdev,const struct pci_device_id * ent)4719b3c6e85SGreg Kroah-Hartman static int i82875p_init_one(struct pci_dev *pdev,
4720d88a10eSAlan Cox 			    const struct pci_device_id *ent)
4730d88a10eSAlan Cox {
4740d88a10eSAlan Cox 	int rc;
4750d88a10eSAlan Cox 
476956b9ba1SJoe Perches 	edac_dbg(0, "\n");
477537fba28SDave Peterson 	i82875p_printk(KERN_INFO, "i82875p init one\n");
478e7ecd891SDave Peterson 
4790d88a10eSAlan Cox 	if (pci_enable_device(pdev) < 0)
4800d88a10eSAlan Cox 		return -EIO;
481e7ecd891SDave Peterson 
4820d88a10eSAlan Cox 	rc = i82875p_probe1(pdev, ent->driver_data);
483e7ecd891SDave Peterson 
4840d88a10eSAlan Cox 	if (mci_pdev == NULL)
4850d88a10eSAlan Cox 		mci_pdev = pci_dev_get(pdev);
486e7ecd891SDave Peterson 
4870d88a10eSAlan Cox 	return rc;
4880d88a10eSAlan Cox }
4890d88a10eSAlan Cox 
i82875p_remove_one(struct pci_dev * pdev)4909b3c6e85SGreg Kroah-Hartman static void i82875p_remove_one(struct pci_dev *pdev)
4910d88a10eSAlan Cox {
4920d88a10eSAlan Cox 	struct mem_ctl_info *mci;
4930d88a10eSAlan Cox 	struct i82875p_pvt *pvt = NULL;
4940d88a10eSAlan Cox 
495956b9ba1SJoe Perches 	edac_dbg(0, "\n");
4960d88a10eSAlan Cox 
497456a2f95SDave Jiang 	if (i82875p_pci)
498456a2f95SDave Jiang 		edac_pci_release_generic_ctl(i82875p_pci);
499456a2f95SDave Jiang 
50037f04581SDoug Thompson 	if ((mci = edac_mc_del_mc(&pdev->dev)) == NULL)
5010d88a10eSAlan Cox 		return;
5020d88a10eSAlan Cox 
5030d88a10eSAlan Cox 	pvt = (struct i82875p_pvt *)mci->pvt_info;
504e7ecd891SDave Peterson 
5050d88a10eSAlan Cox 	if (pvt->ovrfl_window)
5060d88a10eSAlan Cox 		iounmap(pvt->ovrfl_window);
5070d88a10eSAlan Cox 
5080d88a10eSAlan Cox 	if (pvt->ovrfl_pdev) {
5090d88a10eSAlan Cox #ifdef CORRECT_BIOS
5100d88a10eSAlan Cox 		pci_release_regions(pvt->ovrfl_pdev);
5110d88a10eSAlan Cox #endif				/*CORRECT_BIOS */
5120d88a10eSAlan Cox 		pci_disable_device(pvt->ovrfl_pdev);
5130d88a10eSAlan Cox 		pci_dev_put(pvt->ovrfl_pdev);
5140d88a10eSAlan Cox 	}
5150d88a10eSAlan Cox 
5160d88a10eSAlan Cox 	edac_mc_free(mci);
5170d88a10eSAlan Cox }
5180d88a10eSAlan Cox 
519ba935f40SJingoo Han static const struct pci_device_id i82875p_pci_tbl[] = {
520e7ecd891SDave Peterson 	{
521e7ecd891SDave Peterson 	 PCI_VEND_DEV(INTEL, 82875_0), PCI_ANY_ID, PCI_ANY_ID, 0, 0,
522466b71d5SDave Jiang 	 I82875P},
523e7ecd891SDave Peterson 	{
524e7ecd891SDave Peterson 	 0,
525e7ecd891SDave Peterson 	 }			/* 0 terminated list. */
5260d88a10eSAlan Cox };
5270d88a10eSAlan Cox 
5280d88a10eSAlan Cox MODULE_DEVICE_TABLE(pci, i82875p_pci_tbl);
5290d88a10eSAlan Cox 
5300d88a10eSAlan Cox static struct pci_driver i82875p_driver = {
531680cbbbbSDave Peterson 	.name = EDAC_MOD_STR,
5320d88a10eSAlan Cox 	.probe = i82875p_init_one,
5339b3c6e85SGreg Kroah-Hartman 	.remove = i82875p_remove_one,
5340d88a10eSAlan Cox 	.id_table = i82875p_pci_tbl,
5350d88a10eSAlan Cox };
5360d88a10eSAlan Cox 
i82875p_init(void)537da9bb1d2SAlan Cox static int __init i82875p_init(void)
5380d88a10eSAlan Cox {
5390d88a10eSAlan Cox 	int pci_rc;
5400d88a10eSAlan Cox 
541956b9ba1SJoe Perches 	edac_dbg(3, "\n");
542c3c52bceSHitoshi Mitake 
543c3c52bceSHitoshi Mitake        /* Ensure that the OPSTATE is set correctly for POLL or NMI */
544c3c52bceSHitoshi Mitake        opstate_init();
545c3c52bceSHitoshi Mitake 
5460d88a10eSAlan Cox 	pci_rc = pci_register_driver(&i82875p_driver);
547e7ecd891SDave Peterson 
5480d88a10eSAlan Cox 	if (pci_rc < 0)
549637beb69SDave Peterson 		goto fail0;
550e7ecd891SDave Peterson 
5510d88a10eSAlan Cox 	if (mci_pdev == NULL) {
552e7ecd891SDave Peterson 		mci_pdev = pci_get_device(PCI_VENDOR_ID_INTEL,
5530d88a10eSAlan Cox 					PCI_DEVICE_ID_INTEL_82875_0, NULL);
554e7ecd891SDave Peterson 
5550d88a10eSAlan Cox 		if (!mci_pdev) {
556956b9ba1SJoe Perches 			edac_dbg(0, "875p pci_get_device fail\n");
557637beb69SDave Peterson 			pci_rc = -ENODEV;
558637beb69SDave Peterson 			goto fail1;
5590d88a10eSAlan Cox 		}
560e7ecd891SDave Peterson 
5610d88a10eSAlan Cox 		pci_rc = i82875p_init_one(mci_pdev, i82875p_pci_tbl);
562e7ecd891SDave Peterson 
5630d88a10eSAlan Cox 		if (pci_rc < 0) {
564956b9ba1SJoe Perches 			edac_dbg(0, "875p init fail\n");
565637beb69SDave Peterson 			pci_rc = -ENODEV;
566637beb69SDave Peterson 			goto fail1;
5670d88a10eSAlan Cox 		}
5680d88a10eSAlan Cox 	}
569e7ecd891SDave Peterson 
5700d88a10eSAlan Cox 	return 0;
571637beb69SDave Peterson 
572637beb69SDave Peterson fail1:
573637beb69SDave Peterson 	pci_unregister_driver(&i82875p_driver);
574637beb69SDave Peterson 
575637beb69SDave Peterson fail0:
576637beb69SDave Peterson 	pci_dev_put(mci_pdev);
577637beb69SDave Peterson 	return pci_rc;
5780d88a10eSAlan Cox }
5790d88a10eSAlan Cox 
i82875p_exit(void)5800d88a10eSAlan Cox static void __exit i82875p_exit(void)
5810d88a10eSAlan Cox {
582956b9ba1SJoe Perches 	edac_dbg(3, "\n");
5830d88a10eSAlan Cox 
5840d88a10eSAlan Cox 	i82875p_remove_one(mci_pdev);
5850d88a10eSAlan Cox 	pci_dev_put(mci_pdev);
58609a81269SJarkko Lavinen 
58709a81269SJarkko Lavinen 	pci_unregister_driver(&i82875p_driver);
58809a81269SJarkko Lavinen 
5890d88a10eSAlan Cox }
5900d88a10eSAlan Cox 
5910d88a10eSAlan Cox module_init(i82875p_init);
5920d88a10eSAlan Cox module_exit(i82875p_exit);
5930d88a10eSAlan Cox 
5940d88a10eSAlan Cox MODULE_LICENSE("GPL");
5950d88a10eSAlan Cox MODULE_AUTHOR("Linux Networx (http://lnxi.com) Thayne Harbaugh");
5960d88a10eSAlan Cox MODULE_DESCRIPTION("MC support for Intel 82875 memory hub controllers");
597c3c52bceSHitoshi Mitake 
598c3c52bceSHitoshi Mitake module_param(edac_op_state, int, 0444);
599c3c52bceSHitoshi Mitake MODULE_PARM_DESC(edac_op_state, "EDAC Error Reporting state: 0=Poll,1=NMI");
600