i7core_edac.c 43 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717
  1. /* Intel 7 core Memory Controller kernel module (Nehalem)
  2. *
  3. * This file may be distributed under the terms of the
  4. * GNU General Public License version 2 only.
  5. *
  6. * Copyright (c) 2009 by:
  7. * Mauro Carvalho Chehab <mchehab@redhat.com>
  8. *
  9. * Red Hat Inc. http://www.redhat.com
  10. *
  11. * Forked and adapted from the i5400_edac driver
  12. *
  13. * Based on the following public Intel datasheets:
  14. * Intel Core i7 Processor Extreme Edition and Intel Core i7 Processor
  15. * Datasheet, Volume 2:
  16. * http://download.intel.com/design/processor/datashts/320835.pdf
  17. * Intel Xeon Processor 5500 Series Datasheet Volume 2
  18. * http://www.intel.com/Assets/PDF/datasheet/321322.pdf
  19. * also available at:
  20. * http://www.arrownac.com/manufacturers/intel/s/nehalem/5500-datasheet-v2.pdf
  21. */
  22. #include <linux/module.h>
  23. #include <linux/init.h>
  24. #include <linux/pci.h>
  25. #include <linux/pci_ids.h>
  26. #include <linux/slab.h>
  27. #include <linux/edac.h>
  28. #include <linux/mmzone.h>
  29. #include <linux/edac_mce.h>
  30. #include <linux/spinlock.h>
  31. #include "edac_core.h"
  32. /* To use the new pci_[read/write]_config_qword instead of two dword */
  33. #define USE_QWORD 0
  34. /*
  35. * Alter this version for the module when modifications are made
  36. */
  37. #define I7CORE_REVISION " Ver: 1.0.0 " __DATE__
  38. #define EDAC_MOD_STR "i7core_edac"
  39. /* HACK: temporary, just to enable all logs, for now */
  40. #undef debugf0
  41. #define debugf0(fmt, arg...) edac_printk(KERN_INFO, "i7core", fmt, ##arg)
  42. /*
  43. * Debug macros
  44. */
  45. #define i7core_printk(level, fmt, arg...) \
  46. edac_printk(level, "i7core", fmt, ##arg)
  47. #define i7core_mc_printk(mci, level, fmt, arg...) \
  48. edac_mc_chipset_printk(mci, level, "i7core", fmt, ##arg)
  49. /*
  50. * i7core Memory Controller Registers
  51. */
  52. /* OFFSETS for Device 0 Function 0 */
  53. #define MC_CFG_CONTROL 0x90
  54. /* OFFSETS for Device 3 Function 0 */
  55. #define MC_CONTROL 0x48
  56. #define MC_STATUS 0x4c
  57. #define MC_MAX_DOD 0x64
  58. /*
  59. * OFFSETS for Device 3 Function 4, as inicated on Xeon 5500 datasheet:
  60. * http://www.arrownac.com/manufacturers/intel/s/nehalem/5500-datasheet-v2.pdf
  61. */
  62. #define MC_TEST_ERR_RCV1 0x60
  63. #define DIMM2_COR_ERR(r) ((r) & 0x7fff)
  64. #define MC_TEST_ERR_RCV0 0x64
  65. #define DIMM1_COR_ERR(r) (((r) >> 16) & 0x7fff)
  66. #define DIMM0_COR_ERR(r) ((r) & 0x7fff)
  67. /* OFFSETS for Devices 4,5 and 6 Function 0 */
  68. #define MC_CHANNEL_DIMM_INIT_PARAMS 0x58
  69. #define THREE_DIMMS_PRESENT (1 << 24)
  70. #define SINGLE_QUAD_RANK_PRESENT (1 << 23)
  71. #define QUAD_RANK_PRESENT (1 << 22)
  72. #define REGISTERED_DIMM (1 << 15)
  73. #define MC_CHANNEL_MAPPER 0x60
  74. #define RDLCH(r, ch) ((((r) >> (3 + (ch * 6))) & 0x07) - 1)
  75. #define WRLCH(r, ch) ((((r) >> (ch * 6)) & 0x07) - 1)
  76. #define MC_CHANNEL_RANK_PRESENT 0x7c
  77. #define RANK_PRESENT_MASK 0xffff
  78. #define MC_CHANNEL_ADDR_MATCH 0xf0
  79. #define MC_CHANNEL_ERROR_MASK 0xf8
  80. #define MC_CHANNEL_ERROR_INJECT 0xfc
  81. #define INJECT_ADDR_PARITY 0x10
  82. #define INJECT_ECC 0x08
  83. #define MASK_CACHELINE 0x06
  84. #define MASK_FULL_CACHELINE 0x06
  85. #define MASK_MSB32_CACHELINE 0x04
  86. #define MASK_LSB32_CACHELINE 0x02
  87. #define NO_MASK_CACHELINE 0x00
  88. #define REPEAT_EN 0x01
  89. /* OFFSETS for Devices 4,5 and 6 Function 1 */
  90. #define MC_DOD_CH_DIMM0 0x48
  91. #define MC_DOD_CH_DIMM1 0x4c
  92. #define MC_DOD_CH_DIMM2 0x50
  93. #define RANKOFFSET_MASK ((1 << 12) | (1 << 11) | (1 << 10))
  94. #define RANKOFFSET(x) ((x & RANKOFFSET_MASK) >> 10)
  95. #define DIMM_PRESENT_MASK (1 << 9)
  96. #define DIMM_PRESENT(x) (((x) & DIMM_PRESENT_MASK) >> 9)
  97. #define MC_DOD_NUMBANK_MASK ((1 << 8) | (1 << 7))
  98. #define MC_DOD_NUMBANK(x) (((x) & MC_DOD_NUMBANK_MASK) >> 7)
  99. #define MC_DOD_NUMRANK_MASK ((1 << 6) | (1 << 5))
  100. #define MC_DOD_NUMRANK(x) (((x) & MC_DOD_NUMRANK_MASK) >> 5)
  101. #define MC_DOD_NUMROW_MASK ((1 << 4) | (1 << 3) | (1 << 2))
  102. #define MC_DOD_NUMROW(x) (((x) & MC_DOD_NUMROW_MASK) >> 2)
  103. #define MC_DOD_NUMCOL_MASK 3
  104. #define MC_DOD_NUMCOL(x) ((x) & MC_DOD_NUMCOL_MASK)
  105. #define MC_RANK_PRESENT 0x7c
  106. #define MC_SAG_CH_0 0x80
  107. #define MC_SAG_CH_1 0x84
  108. #define MC_SAG_CH_2 0x88
  109. #define MC_SAG_CH_3 0x8c
  110. #define MC_SAG_CH_4 0x90
  111. #define MC_SAG_CH_5 0x94
  112. #define MC_SAG_CH_6 0x98
  113. #define MC_SAG_CH_7 0x9c
  114. #define MC_RIR_LIMIT_CH_0 0x40
  115. #define MC_RIR_LIMIT_CH_1 0x44
  116. #define MC_RIR_LIMIT_CH_2 0x48
  117. #define MC_RIR_LIMIT_CH_3 0x4C
  118. #define MC_RIR_LIMIT_CH_4 0x50
  119. #define MC_RIR_LIMIT_CH_5 0x54
  120. #define MC_RIR_LIMIT_CH_6 0x58
  121. #define MC_RIR_LIMIT_CH_7 0x5C
  122. #define MC_RIR_LIMIT_MASK ((1 << 10) - 1)
  123. #define MC_RIR_WAY_CH 0x80
  124. #define MC_RIR_WAY_OFFSET_MASK (((1 << 14) - 1) & ~0x7)
  125. #define MC_RIR_WAY_RANK_MASK 0x7
  126. /*
  127. * i7core structs
  128. */
  129. #define NUM_CHANS 3
  130. #define MAX_DIMMS 3 /* Max DIMMS per channel */
  131. #define NUM_SOCKETS 2 /* Max number of MC sockets */
  132. #define MAX_MCR_FUNC 4
  133. #define MAX_CHAN_FUNC 3
  134. struct i7core_info {
  135. u32 mc_control;
  136. u32 mc_status;
  137. u32 max_dod;
  138. u32 ch_map;
  139. };
  140. struct i7core_inject {
  141. int enable;
  142. u8 socket;
  143. u32 section;
  144. u32 type;
  145. u32 eccmask;
  146. /* Error address mask */
  147. int channel, dimm, rank, bank, page, col;
  148. };
  149. struct i7core_channel {
  150. u32 ranks;
  151. u32 dimms;
  152. };
  153. struct pci_id_descr {
  154. int dev;
  155. int func;
  156. int dev_id;
  157. struct pci_dev *pdev[NUM_SOCKETS];
  158. };
  159. struct i7core_pvt {
  160. struct pci_dev *pci_noncore[NUM_SOCKETS];
  161. struct pci_dev *pci_mcr[NUM_SOCKETS][MAX_MCR_FUNC + 1];
  162. struct pci_dev *pci_ch[NUM_SOCKETS][NUM_CHANS][MAX_CHAN_FUNC + 1];
  163. struct i7core_info info;
  164. struct i7core_inject inject;
  165. struct i7core_channel channel[NUM_SOCKETS][NUM_CHANS];
  166. int sockets; /* Number of sockets */
  167. int channels; /* Number of active channels */
  168. int ce_count_available[NUM_SOCKETS];
  169. /* ECC corrected errors counts per dimm */
  170. unsigned long ce_count[NUM_SOCKETS][MAX_DIMMS];
  171. int last_ce_count[NUM_SOCKETS][MAX_DIMMS];
  172. /* mcelog glue */
  173. struct edac_mce edac_mce;
  174. struct mce mce_entry[MCE_LOG_LEN];
  175. unsigned mce_count;
  176. spinlock_t mce_lock;
  177. };
  178. /* Device name and register DID (Device ID) */
  179. struct i7core_dev_info {
  180. const char *ctl_name; /* name for this device */
  181. u16 fsb_mapping_errors; /* DID for the branchmap,control */
  182. };
  183. #define PCI_DESCR(device, function, device_id) \
  184. .dev = (device), \
  185. .func = (function), \
  186. .dev_id = (device_id)
  187. struct pci_id_descr pci_devs[] = {
  188. /* Memory controller */
  189. { PCI_DESCR(3, 0, PCI_DEVICE_ID_INTEL_I7_MCR) },
  190. { PCI_DESCR(3, 1, PCI_DEVICE_ID_INTEL_I7_MC_TAD) },
  191. { PCI_DESCR(3, 2, PCI_DEVICE_ID_INTEL_I7_MC_RAS) }, /* if RDIMM is supported */
  192. { PCI_DESCR(3, 4, PCI_DEVICE_ID_INTEL_I7_MC_TEST) },
  193. /* Channel 0 */
  194. { PCI_DESCR(4, 0, PCI_DEVICE_ID_INTEL_I7_MC_CH0_CTRL) },
  195. { PCI_DESCR(4, 1, PCI_DEVICE_ID_INTEL_I7_MC_CH0_ADDR) },
  196. { PCI_DESCR(4, 2, PCI_DEVICE_ID_INTEL_I7_MC_CH0_RANK) },
  197. { PCI_DESCR(4, 3, PCI_DEVICE_ID_INTEL_I7_MC_CH0_TC) },
  198. /* Channel 1 */
  199. { PCI_DESCR(5, 0, PCI_DEVICE_ID_INTEL_I7_MC_CH1_CTRL) },
  200. { PCI_DESCR(5, 1, PCI_DEVICE_ID_INTEL_I7_MC_CH1_ADDR) },
  201. { PCI_DESCR(5, 2, PCI_DEVICE_ID_INTEL_I7_MC_CH1_RANK) },
  202. { PCI_DESCR(5, 3, PCI_DEVICE_ID_INTEL_I7_MC_CH1_TC) },
  203. /* Channel 2 */
  204. { PCI_DESCR(6, 0, PCI_DEVICE_ID_INTEL_I7_MC_CH2_CTRL) },
  205. { PCI_DESCR(6, 1, PCI_DEVICE_ID_INTEL_I7_MC_CH2_ADDR) },
  206. { PCI_DESCR(6, 2, PCI_DEVICE_ID_INTEL_I7_MC_CH2_RANK) },
  207. { PCI_DESCR(6, 3, PCI_DEVICE_ID_INTEL_I7_MC_CH2_TC) },
  208. /* Generic Non-core registers */
  209. /*
  210. * This is the PCI device on i7core and on Xeon 35xx (8086:2c41)
  211. * On Xeon 55xx, however, it has a different id (8086:2c40). So,
  212. * the probing code needs to test for the other address in case of
  213. * failure of this one
  214. */
  215. { PCI_DESCR(0, 0, PCI_DEVICE_ID_INTEL_I7_NOCORE) },
  216. };
  217. #define N_DEVS ARRAY_SIZE(pci_devs)
  218. /*
  219. * pci_device_id table for which devices we are looking for
  220. * This should match the first device at pci_devs table
  221. */
  222. static const struct pci_device_id i7core_pci_tbl[] __devinitdata = {
  223. {PCI_DEVICE(PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_X58_HUB_MGMT)},
  224. {0,} /* 0 terminated list. */
  225. };
  226. /* Table of devices attributes supported by this driver */
  227. static const struct i7core_dev_info i7core_devs[] = {
  228. {
  229. .ctl_name = "i7 Core",
  230. .fsb_mapping_errors = PCI_DEVICE_ID_INTEL_I7_MCR,
  231. },
  232. };
  233. static struct edac_pci_ctl_info *i7core_pci;
  234. /****************************************************************************
  235. Anciliary status routines
  236. ****************************************************************************/
  237. /* MC_CONTROL bits */
  238. #define CH_ACTIVE(pvt, ch) ((pvt)->info.mc_control & (1 << (8 + ch)))
  239. #define ECCx8(pvt) ((pvt)->info.mc_control & (1 << 1))
  240. /* MC_STATUS bits */
  241. #define ECC_ENABLED(pvt) ((pvt)->info.mc_status & (1 << 3))
  242. #define CH_DISABLED(pvt, ch) ((pvt)->info.mc_status & (1 << ch))
  243. /* MC_MAX_DOD read functions */
  244. static inline int numdimms(u32 dimms)
  245. {
  246. return (dimms & 0x3) + 1;
  247. }
  248. static inline int numrank(u32 rank)
  249. {
  250. static int ranks[4] = { 1, 2, 4, -EINVAL };
  251. return ranks[rank & 0x3];
  252. }
  253. static inline int numbank(u32 bank)
  254. {
  255. static int banks[4] = { 4, 8, 16, -EINVAL };
  256. return banks[bank & 0x3];
  257. }
  258. static inline int numrow(u32 row)
  259. {
  260. static int rows[8] = {
  261. 1 << 12, 1 << 13, 1 << 14, 1 << 15,
  262. 1 << 16, -EINVAL, -EINVAL, -EINVAL,
  263. };
  264. return rows[row & 0x7];
  265. }
  266. static inline int numcol(u32 col)
  267. {
  268. static int cols[8] = {
  269. 1 << 10, 1 << 11, 1 << 12, -EINVAL,
  270. };
  271. return cols[col & 0x3];
  272. }
  273. /****************************************************************************
  274. Memory check routines
  275. ****************************************************************************/
  276. static struct pci_dev *get_pdev_slot_func(u8 socket, unsigned slot,
  277. unsigned func)
  278. {
  279. int i;
  280. for (i = 0; i < N_DEVS; i++) {
  281. if (!pci_devs[i].pdev[socket])
  282. continue;
  283. if (PCI_SLOT(pci_devs[i].pdev[socket]->devfn) == slot &&
  284. PCI_FUNC(pci_devs[i].pdev[socket]->devfn) == func) {
  285. return pci_devs[i].pdev[socket];
  286. }
  287. }
  288. return NULL;
  289. }
  290. static int i7core_get_active_channels(u8 socket, unsigned *channels,
  291. unsigned *csrows)
  292. {
  293. struct pci_dev *pdev = NULL;
  294. int i, j;
  295. u32 status, control;
  296. *channels = 0;
  297. *csrows = 0;
  298. pdev = get_pdev_slot_func(socket, 3, 0);
  299. if (!pdev) {
  300. i7core_printk(KERN_ERR, "Couldn't find socket %d fn 3.0!!!\n",
  301. socket);
  302. return -ENODEV;
  303. }
  304. /* Device 3 function 0 reads */
  305. pci_read_config_dword(pdev, MC_STATUS, &status);
  306. pci_read_config_dword(pdev, MC_CONTROL, &control);
  307. for (i = 0; i < NUM_CHANS; i++) {
  308. u32 dimm_dod[3];
  309. /* Check if the channel is active */
  310. if (!(control & (1 << (8 + i))))
  311. continue;
  312. /* Check if the channel is disabled */
  313. if (status & (1 << i))
  314. continue;
  315. pdev = get_pdev_slot_func(socket, i + 4, 1);
  316. if (!pdev) {
  317. i7core_printk(KERN_ERR, "Couldn't find socket %d "
  318. "fn %d.%d!!!\n",
  319. socket, i + 4, 1);
  320. return -ENODEV;
  321. }
  322. /* Devices 4-6 function 1 */
  323. pci_read_config_dword(pdev,
  324. MC_DOD_CH_DIMM0, &dimm_dod[0]);
  325. pci_read_config_dword(pdev,
  326. MC_DOD_CH_DIMM1, &dimm_dod[1]);
  327. pci_read_config_dword(pdev,
  328. MC_DOD_CH_DIMM2, &dimm_dod[2]);
  329. (*channels)++;
  330. for (j = 0; j < 3; j++) {
  331. if (!DIMM_PRESENT(dimm_dod[j]))
  332. continue;
  333. (*csrows)++;
  334. }
  335. }
  336. debugf0("Number of active channels on socked %d: %d\n",
  337. socket, *channels);
  338. return 0;
  339. }
  340. static int get_dimm_config(struct mem_ctl_info *mci, int *csrow, u8 socket)
  341. {
  342. struct i7core_pvt *pvt = mci->pvt_info;
  343. struct csrow_info *csr;
  344. struct pci_dev *pdev;
  345. int i, j;
  346. unsigned long last_page = 0;
  347. enum edac_type mode;
  348. enum mem_type mtype;
  349. /* Get data from the MC register, function 0 */
  350. pdev = pvt->pci_mcr[socket][0];
  351. if (!pdev)
  352. return -ENODEV;
  353. /* Device 3 function 0 reads */
  354. pci_read_config_dword(pdev, MC_CONTROL, &pvt->info.mc_control);
  355. pci_read_config_dword(pdev, MC_STATUS, &pvt->info.mc_status);
  356. pci_read_config_dword(pdev, MC_MAX_DOD, &pvt->info.max_dod);
  357. pci_read_config_dword(pdev, MC_CHANNEL_MAPPER, &pvt->info.ch_map);
  358. debugf0("MC control=0x%08x status=0x%08x dod=0x%08x map=0x%08x\n",
  359. pvt->info.mc_control, pvt->info.mc_status,
  360. pvt->info.max_dod, pvt->info.ch_map);
  361. if (ECC_ENABLED(pvt)) {
  362. debugf0("ECC enabled with x%d SDCC\n", ECCx8(pvt) ? 8 : 4);
  363. if (ECCx8(pvt))
  364. mode = EDAC_S8ECD8ED;
  365. else
  366. mode = EDAC_S4ECD4ED;
  367. } else {
  368. debugf0("ECC disabled\n");
  369. mode = EDAC_NONE;
  370. }
  371. /* FIXME: need to handle the error codes */
  372. debugf0("DOD Max limits: DIMMS: %d, %d-ranked, %d-banked\n",
  373. numdimms(pvt->info.max_dod),
  374. numrank(pvt->info.max_dod >> 2),
  375. numbank(pvt->info.max_dod >> 4));
  376. debugf0("DOD Max rows x colums = 0x%x x 0x%x\n",
  377. numrow(pvt->info.max_dod >> 6),
  378. numcol(pvt->info.max_dod >> 9));
  379. debugf0("Memory channel configuration:\n");
  380. for (i = 0; i < NUM_CHANS; i++) {
  381. u32 data, dimm_dod[3], value[8];
  382. if (!CH_ACTIVE(pvt, i)) {
  383. debugf0("Channel %i is not active\n", i);
  384. continue;
  385. }
  386. if (CH_DISABLED(pvt, i)) {
  387. debugf0("Channel %i is disabled\n", i);
  388. continue;
  389. }
  390. /* Devices 4-6 function 0 */
  391. pci_read_config_dword(pvt->pci_ch[socket][i][0],
  392. MC_CHANNEL_DIMM_INIT_PARAMS, &data);
  393. pvt->channel[socket][i].ranks = (data & QUAD_RANK_PRESENT) ?
  394. 4 : 2;
  395. if (data & REGISTERED_DIMM)
  396. mtype = MEM_RDDR3;
  397. else
  398. mtype = MEM_DDR3;
  399. #if 0
  400. if (data & THREE_DIMMS_PRESENT)
  401. pvt->channel[i].dimms = 3;
  402. else if (data & SINGLE_QUAD_RANK_PRESENT)
  403. pvt->channel[i].dimms = 1;
  404. else
  405. pvt->channel[i].dimms = 2;
  406. #endif
  407. /* Devices 4-6 function 1 */
  408. pci_read_config_dword(pvt->pci_ch[socket][i][1],
  409. MC_DOD_CH_DIMM0, &dimm_dod[0]);
  410. pci_read_config_dword(pvt->pci_ch[socket][i][1],
  411. MC_DOD_CH_DIMM1, &dimm_dod[1]);
  412. pci_read_config_dword(pvt->pci_ch[socket][i][1],
  413. MC_DOD_CH_DIMM2, &dimm_dod[2]);
  414. debugf0("Ch%d phy rd%d, wr%d (0x%08x): "
  415. "%d ranks, %cDIMMs\n",
  416. i,
  417. RDLCH(pvt->info.ch_map, i), WRLCH(pvt->info.ch_map, i),
  418. data,
  419. pvt->channel[socket][i].ranks,
  420. (data & REGISTERED_DIMM) ? 'R' : 'U');
  421. for (j = 0; j < 3; j++) {
  422. u32 banks, ranks, rows, cols;
  423. u32 size, npages;
  424. if (!DIMM_PRESENT(dimm_dod[j]))
  425. continue;
  426. banks = numbank(MC_DOD_NUMBANK(dimm_dod[j]));
  427. ranks = numrank(MC_DOD_NUMRANK(dimm_dod[j]));
  428. rows = numrow(MC_DOD_NUMROW(dimm_dod[j]));
  429. cols = numcol(MC_DOD_NUMCOL(dimm_dod[j]));
  430. /* DDR3 has 8 I/O banks */
  431. size = (rows * cols * banks * ranks) >> (20 - 3);
  432. pvt->channel[socket][i].dimms++;
  433. debugf0("\tdimm %d (0x%08x) %d Mb offset: %x, "
  434. "numbank: %d,\n\t\t"
  435. "numrank: %d, numrow: %#x, numcol: %#x\n",
  436. j, dimm_dod[j], size,
  437. RANKOFFSET(dimm_dod[j]),
  438. banks, ranks, rows, cols);
  439. #if PAGE_SHIFT > 20
  440. npages = size >> (PAGE_SHIFT - 20);
  441. #else
  442. npages = size << (20 - PAGE_SHIFT);
  443. #endif
  444. csr = &mci->csrows[*csrow];
  445. csr->first_page = last_page + 1;
  446. last_page += npages;
  447. csr->last_page = last_page;
  448. csr->nr_pages = npages;
  449. csr->page_mask = 0;
  450. csr->grain = 8;
  451. csr->csrow_idx = *csrow;
  452. csr->nr_channels = 1;
  453. csr->channels[0].chan_idx = i;
  454. csr->channels[0].ce_count = 0;
  455. switch (banks) {
  456. case 4:
  457. csr->dtype = DEV_X4;
  458. break;
  459. case 8:
  460. csr->dtype = DEV_X8;
  461. break;
  462. case 16:
  463. csr->dtype = DEV_X16;
  464. break;
  465. default:
  466. csr->dtype = DEV_UNKNOWN;
  467. }
  468. csr->edac_mode = mode;
  469. csr->mtype = mtype;
  470. (*csrow)++;
  471. }
  472. pci_read_config_dword(pdev, MC_SAG_CH_0, &value[0]);
  473. pci_read_config_dword(pdev, MC_SAG_CH_1, &value[1]);
  474. pci_read_config_dword(pdev, MC_SAG_CH_2, &value[2]);
  475. pci_read_config_dword(pdev, MC_SAG_CH_3, &value[3]);
  476. pci_read_config_dword(pdev, MC_SAG_CH_4, &value[4]);
  477. pci_read_config_dword(pdev, MC_SAG_CH_5, &value[5]);
  478. pci_read_config_dword(pdev, MC_SAG_CH_6, &value[6]);
  479. pci_read_config_dword(pdev, MC_SAG_CH_7, &value[7]);
  480. debugf0("\t[%i] DIVBY3\tREMOVED\tOFFSET\n", i);
  481. for (j = 0; j < 8; j++)
  482. debugf0("\t\t%#x\t%#x\t%#x\n",
  483. (value[j] >> 27) & 0x1,
  484. (value[j] >> 24) & 0x7,
  485. (value[j] && ((1 << 24) - 1)));
  486. }
  487. return 0;
  488. }
  489. /****************************************************************************
  490. Error insertion routines
  491. ****************************************************************************/
  492. /* The i7core has independent error injection features per channel.
  493. However, to have a simpler code, we don't allow enabling error injection
  494. on more than one channel.
  495. Also, since a change at an inject parameter will be applied only at enable,
  496. we're disabling error injection on all write calls to the sysfs nodes that
  497. controls the error code injection.
  498. */
  499. static int disable_inject(struct mem_ctl_info *mci)
  500. {
  501. struct i7core_pvt *pvt = mci->pvt_info;
  502. pvt->inject.enable = 0;
  503. if (!pvt->pci_ch[pvt->inject.socket][pvt->inject.channel][0])
  504. return -ENODEV;
  505. pci_write_config_dword(pvt->pci_ch[pvt->inject.socket][pvt->inject.channel][0],
  506. MC_CHANNEL_ERROR_MASK, 0);
  507. return 0;
  508. }
  509. /*
  510. * i7core inject inject.socket
  511. *
  512. * accept and store error injection inject.socket value
  513. */
  514. static ssize_t i7core_inject_socket_store(struct mem_ctl_info *mci,
  515. const char *data, size_t count)
  516. {
  517. struct i7core_pvt *pvt = mci->pvt_info;
  518. unsigned long value;
  519. int rc;
  520. rc = strict_strtoul(data, 10, &value);
  521. if ((rc < 0) || (value > pvt->sockets))
  522. return 0;
  523. pvt->inject.section = (u32) value;
  524. return count;
  525. }
  526. static ssize_t i7core_inject_socket_show(struct mem_ctl_info *mci,
  527. char *data)
  528. {
  529. struct i7core_pvt *pvt = mci->pvt_info;
  530. return sprintf(data, "%d\n", pvt->inject.socket);
  531. }
  532. /*
  533. * i7core inject inject.section
  534. *
  535. * accept and store error injection inject.section value
  536. * bit 0 - refers to the lower 32-byte half cacheline
  537. * bit 1 - refers to the upper 32-byte half cacheline
  538. */
  539. static ssize_t i7core_inject_section_store(struct mem_ctl_info *mci,
  540. const char *data, size_t count)
  541. {
  542. struct i7core_pvt *pvt = mci->pvt_info;
  543. unsigned long value;
  544. int rc;
  545. if (pvt->inject.enable)
  546. disable_inject(mci);
  547. rc = strict_strtoul(data, 10, &value);
  548. if ((rc < 0) || (value > 3))
  549. return 0;
  550. pvt->inject.section = (u32) value;
  551. return count;
  552. }
  553. static ssize_t i7core_inject_section_show(struct mem_ctl_info *mci,
  554. char *data)
  555. {
  556. struct i7core_pvt *pvt = mci->pvt_info;
  557. return sprintf(data, "0x%08x\n", pvt->inject.section);
  558. }
  559. /*
  560. * i7core inject.type
  561. *
  562. * accept and store error injection inject.section value
  563. * bit 0 - repeat enable - Enable error repetition
  564. * bit 1 - inject ECC error
  565. * bit 2 - inject parity error
  566. */
  567. static ssize_t i7core_inject_type_store(struct mem_ctl_info *mci,
  568. const char *data, size_t count)
  569. {
  570. struct i7core_pvt *pvt = mci->pvt_info;
  571. unsigned long value;
  572. int rc;
  573. if (pvt->inject.enable)
  574. disable_inject(mci);
  575. rc = strict_strtoul(data, 10, &value);
  576. if ((rc < 0) || (value > 7))
  577. return 0;
  578. pvt->inject.type = (u32) value;
  579. return count;
  580. }
  581. static ssize_t i7core_inject_type_show(struct mem_ctl_info *mci,
  582. char *data)
  583. {
  584. struct i7core_pvt *pvt = mci->pvt_info;
  585. return sprintf(data, "0x%08x\n", pvt->inject.type);
  586. }
  587. /*
  588. * i7core_inject_inject.eccmask_store
  589. *
  590. * The type of error (UE/CE) will depend on the inject.eccmask value:
  591. * Any bits set to a 1 will flip the corresponding ECC bit
  592. * Correctable errors can be injected by flipping 1 bit or the bits within
  593. * a symbol pair (2 consecutive aligned 8-bit pairs - i.e. 7:0 and 15:8 or
  594. * 23:16 and 31:24). Flipping bits in two symbol pairs will cause an
  595. * uncorrectable error to be injected.
  596. */
  597. static ssize_t i7core_inject_eccmask_store(struct mem_ctl_info *mci,
  598. const char *data, size_t count)
  599. {
  600. struct i7core_pvt *pvt = mci->pvt_info;
  601. unsigned long value;
  602. int rc;
  603. if (pvt->inject.enable)
  604. disable_inject(mci);
  605. rc = strict_strtoul(data, 10, &value);
  606. if (rc < 0)
  607. return 0;
  608. pvt->inject.eccmask = (u32) value;
  609. return count;
  610. }
  611. static ssize_t i7core_inject_eccmask_show(struct mem_ctl_info *mci,
  612. char *data)
  613. {
  614. struct i7core_pvt *pvt = mci->pvt_info;
  615. return sprintf(data, "0x%08x\n", pvt->inject.eccmask);
  616. }
  617. /*
  618. * i7core_addrmatch
  619. *
  620. * The type of error (UE/CE) will depend on the inject.eccmask value:
  621. * Any bits set to a 1 will flip the corresponding ECC bit
  622. * Correctable errors can be injected by flipping 1 bit or the bits within
  623. * a symbol pair (2 consecutive aligned 8-bit pairs - i.e. 7:0 and 15:8 or
  624. * 23:16 and 31:24). Flipping bits in two symbol pairs will cause an
  625. * uncorrectable error to be injected.
  626. */
  627. static ssize_t i7core_inject_addrmatch_store(struct mem_ctl_info *mci,
  628. const char *data, size_t count)
  629. {
  630. struct i7core_pvt *pvt = mci->pvt_info;
  631. char *cmd, *val;
  632. long value;
  633. int rc;
  634. if (pvt->inject.enable)
  635. disable_inject(mci);
  636. do {
  637. cmd = strsep((char **) &data, ":");
  638. if (!cmd)
  639. break;
  640. val = strsep((char **) &data, " \n\t");
  641. if (!val)
  642. return cmd - data;
  643. if (!strcasecmp(val, "any"))
  644. value = -1;
  645. else {
  646. rc = strict_strtol(val, 10, &value);
  647. if ((rc < 0) || (value < 0))
  648. return cmd - data;
  649. }
  650. if (!strcasecmp(cmd, "channel")) {
  651. if (value < 3)
  652. pvt->inject.channel = value;
  653. else
  654. return cmd - data;
  655. } else if (!strcasecmp(cmd, "dimm")) {
  656. if (value < 4)
  657. pvt->inject.dimm = value;
  658. else
  659. return cmd - data;
  660. } else if (!strcasecmp(cmd, "rank")) {
  661. if (value < 4)
  662. pvt->inject.rank = value;
  663. else
  664. return cmd - data;
  665. } else if (!strcasecmp(cmd, "bank")) {
  666. if (value < 4)
  667. pvt->inject.bank = value;
  668. else
  669. return cmd - data;
  670. } else if (!strcasecmp(cmd, "page")) {
  671. if (value <= 0xffff)
  672. pvt->inject.page = value;
  673. else
  674. return cmd - data;
  675. } else if (!strcasecmp(cmd, "col") ||
  676. !strcasecmp(cmd, "column")) {
  677. if (value <= 0x3fff)
  678. pvt->inject.col = value;
  679. else
  680. return cmd - data;
  681. }
  682. } while (1);
  683. return count;
  684. }
  685. static ssize_t i7core_inject_addrmatch_show(struct mem_ctl_info *mci,
  686. char *data)
  687. {
  688. struct i7core_pvt *pvt = mci->pvt_info;
  689. char channel[4], dimm[4], bank[4], rank[4], page[7], col[7];
  690. if (pvt->inject.channel < 0)
  691. sprintf(channel, "any");
  692. else
  693. sprintf(channel, "%d", pvt->inject.channel);
  694. if (pvt->inject.dimm < 0)
  695. sprintf(dimm, "any");
  696. else
  697. sprintf(dimm, "%d", pvt->inject.dimm);
  698. if (pvt->inject.bank < 0)
  699. sprintf(bank, "any");
  700. else
  701. sprintf(bank, "%d", pvt->inject.bank);
  702. if (pvt->inject.rank < 0)
  703. sprintf(rank, "any");
  704. else
  705. sprintf(rank, "%d", pvt->inject.rank);
  706. if (pvt->inject.page < 0)
  707. sprintf(page, "any");
  708. else
  709. sprintf(page, "0x%04x", pvt->inject.page);
  710. if (pvt->inject.col < 0)
  711. sprintf(col, "any");
  712. else
  713. sprintf(col, "0x%04x", pvt->inject.col);
  714. return sprintf(data, "channel: %s\ndimm: %s\nbank: %s\n"
  715. "rank: %s\npage: %s\ncolumn: %s\n",
  716. channel, dimm, bank, rank, page, col);
  717. }
  718. /*
  719. * This routine prepares the Memory Controller for error injection.
  720. * The error will be injected when some process tries to write to the
  721. * memory that matches the given criteria.
  722. * The criteria can be set in terms of a mask where dimm, rank, bank, page
  723. * and col can be specified.
  724. * A -1 value for any of the mask items will make the MCU to ignore
  725. * that matching criteria for error injection.
  726. *
  727. * It should be noticed that the error will only happen after a write operation
  728. * on a memory that matches the condition. if REPEAT_EN is not enabled at
  729. * inject mask, then it will produce just one error. Otherwise, it will repeat
  730. * until the injectmask would be cleaned.
  731. *
  732. * FIXME: This routine assumes that MAXNUMDIMMS value of MC_MAX_DOD
  733. * is reliable enough to check if the MC is using the
  734. * three channels. However, this is not clear at the datasheet.
  735. */
  736. static ssize_t i7core_inject_enable_store(struct mem_ctl_info *mci,
  737. const char *data, size_t count)
  738. {
  739. struct i7core_pvt *pvt = mci->pvt_info;
  740. u32 injectmask;
  741. u64 mask = 0;
  742. int rc;
  743. long enable;
  744. if (!pvt->pci_ch[pvt->inject.socket][pvt->inject.channel][0])
  745. return 0;
  746. rc = strict_strtoul(data, 10, &enable);
  747. if ((rc < 0))
  748. return 0;
  749. if (enable) {
  750. pvt->inject.enable = 1;
  751. } else {
  752. disable_inject(mci);
  753. return count;
  754. }
  755. /* Sets pvt->inject.dimm mask */
  756. if (pvt->inject.dimm < 0)
  757. mask |= 1L << 41;
  758. else {
  759. if (pvt->channel[pvt->inject.socket][pvt->inject.channel].dimms > 2)
  760. mask |= (pvt->inject.dimm & 0x3L) << 35;
  761. else
  762. mask |= (pvt->inject.dimm & 0x1L) << 36;
  763. }
  764. /* Sets pvt->inject.rank mask */
  765. if (pvt->inject.rank < 0)
  766. mask |= 1L << 40;
  767. else {
  768. if (pvt->channel[pvt->inject.socket][pvt->inject.channel].dimms > 2)
  769. mask |= (pvt->inject.rank & 0x1L) << 34;
  770. else
  771. mask |= (pvt->inject.rank & 0x3L) << 34;
  772. }
  773. /* Sets pvt->inject.bank mask */
  774. if (pvt->inject.bank < 0)
  775. mask |= 1L << 39;
  776. else
  777. mask |= (pvt->inject.bank & 0x15L) << 30;
  778. /* Sets pvt->inject.page mask */
  779. if (pvt->inject.page < 0)
  780. mask |= 1L << 38;
  781. else
  782. mask |= (pvt->inject.page & 0xffffL) << 14;
  783. /* Sets pvt->inject.column mask */
  784. if (pvt->inject.col < 0)
  785. mask |= 1L << 37;
  786. else
  787. mask |= (pvt->inject.col & 0x3fffL);
  788. /* Unlock writes to registers */
  789. pci_write_config_dword(pvt->pci_noncore[pvt->inject.socket],
  790. MC_CFG_CONTROL, 0x2);
  791. msleep(100);
  792. /* Zeroes error count registers */
  793. pci_write_config_dword(pvt->pci_mcr[pvt->inject.socket][4],
  794. MC_TEST_ERR_RCV1, 0);
  795. pci_write_config_dword(pvt->pci_mcr[pvt->inject.socket][4],
  796. MC_TEST_ERR_RCV0, 0);
  797. pvt->ce_count_available[pvt->inject.socket] = 0;
  798. #if USE_QWORD
  799. pci_write_config_qword(pvt->pci_ch[pvt->inject.socket][pvt->inject.channel][0],
  800. MC_CHANNEL_ADDR_MATCH, mask);
  801. #else
  802. pci_write_config_dword(pvt->pci_ch[pvt->inject.socket][pvt->inject.channel][0],
  803. MC_CHANNEL_ADDR_MATCH, mask);
  804. pci_write_config_dword(pvt->pci_ch[pvt->inject.socket][pvt->inject.channel][0],
  805. MC_CHANNEL_ADDR_MATCH + 4, mask >> 32L);
  806. #endif
  807. #if 1
  808. #if USE_QWORD
  809. u64 rdmask;
  810. pci_read_config_qword(pvt->pci_ch[pvt->inject.socket][pvt->inject.channel][0],
  811. MC_CHANNEL_ADDR_MATCH, &rdmask);
  812. debugf0("Inject addr match write 0x%016llx, read: 0x%016llx\n",
  813. mask, rdmask);
  814. #else
  815. u32 rdmask1, rdmask2;
  816. pci_read_config_dword(pvt->pci_ch[pvt->inject.socket][pvt->inject.channel][0],
  817. MC_CHANNEL_ADDR_MATCH, &rdmask1);
  818. pci_read_config_dword(pvt->pci_ch[pvt->inject.socket][pvt->inject.channel][0],
  819. MC_CHANNEL_ADDR_MATCH + 4, &rdmask2);
  820. debugf0("Inject addr match write 0x%016llx, read: 0x%08x 0x%08x\n",
  821. mask, rdmask1, rdmask2);
  822. #endif
  823. #endif
  824. pci_write_config_dword(pvt->pci_ch[pvt->inject.socket][pvt->inject.channel][0],
  825. MC_CHANNEL_ERROR_MASK, pvt->inject.eccmask);
  826. /*
  827. * bit 0: REPEAT_EN
  828. * bits 1-2: MASK_HALF_CACHELINE
  829. * bit 3: INJECT_ECC
  830. * bit 4: INJECT_ADDR_PARITY
  831. */
  832. injectmask = (pvt->inject.type & 1) |
  833. (pvt->inject.section & 0x3) << 1 |
  834. (pvt->inject.type & 0x6) << (3 - 1);
  835. pci_write_config_dword(pvt->pci_ch[pvt->inject.socket][pvt->inject.channel][0],
  836. MC_CHANNEL_ERROR_MASK, injectmask);
  837. #if 0
  838. /* lock writes to registers */
  839. pci_write_config_dword(pvt->pci_noncore, MC_CFG_CONTROL, 0);
  840. #endif
  841. debugf0("Error inject addr match 0x%016llx, ecc 0x%08x,"
  842. " inject 0x%08x\n",
  843. mask, pvt->inject.eccmask, injectmask);
  844. return count;
  845. }
  846. static ssize_t i7core_inject_enable_show(struct mem_ctl_info *mci,
  847. char *data)
  848. {
  849. struct i7core_pvt *pvt = mci->pvt_info;
  850. u32 injectmask;
  851. pci_read_config_dword(pvt->pci_ch[pvt->inject.socket][pvt->inject.channel][0],
  852. MC_CHANNEL_ERROR_MASK, &injectmask);
  853. debugf0("Inject error read: 0x%018x\n", injectmask);
  854. if (injectmask & 0x0c)
  855. pvt->inject.enable = 1;
  856. return sprintf(data, "%d\n", pvt->inject.enable);
  857. }
  858. static ssize_t i7core_ce_regs_show(struct mem_ctl_info *mci, char *data)
  859. {
  860. unsigned i, count, total = 0;
  861. struct i7core_pvt *pvt = mci->pvt_info;
  862. for (i = 0; i < pvt->sockets; i++) {
  863. if (!pvt->ce_count_available[i])
  864. count = sprintf(data, "socket 0 data unavailable\n");
  865. else
  866. count = sprintf(data, "socket %d, dimm0: %lu\n"
  867. "dimm1: %lu\ndimm2: %lu\n",
  868. i,
  869. pvt->ce_count[i][0],
  870. pvt->ce_count[i][1],
  871. pvt->ce_count[i][2]);
  872. data += count;
  873. total += count;
  874. }
  875. return total;
  876. }
  877. /*
  878. * Sysfs struct
  879. */
  880. static struct mcidev_sysfs_attribute i7core_inj_attrs[] = {
  881. {
  882. .attr = {
  883. .name = "inject_socket",
  884. .mode = (S_IRUGO | S_IWUSR)
  885. },
  886. .show = i7core_inject_socket_show,
  887. .store = i7core_inject_socket_store,
  888. }, {
  889. .attr = {
  890. .name = "inject_section",
  891. .mode = (S_IRUGO | S_IWUSR)
  892. },
  893. .show = i7core_inject_section_show,
  894. .store = i7core_inject_section_store,
  895. }, {
  896. .attr = {
  897. .name = "inject_type",
  898. .mode = (S_IRUGO | S_IWUSR)
  899. },
  900. .show = i7core_inject_type_show,
  901. .store = i7core_inject_type_store,
  902. }, {
  903. .attr = {
  904. .name = "inject_eccmask",
  905. .mode = (S_IRUGO | S_IWUSR)
  906. },
  907. .show = i7core_inject_eccmask_show,
  908. .store = i7core_inject_eccmask_store,
  909. }, {
  910. .attr = {
  911. .name = "inject_addrmatch",
  912. .mode = (S_IRUGO | S_IWUSR)
  913. },
  914. .show = i7core_inject_addrmatch_show,
  915. .store = i7core_inject_addrmatch_store,
  916. }, {
  917. .attr = {
  918. .name = "inject_enable",
  919. .mode = (S_IRUGO | S_IWUSR)
  920. },
  921. .show = i7core_inject_enable_show,
  922. .store = i7core_inject_enable_store,
  923. }, {
  924. .attr = {
  925. .name = "corrected_error_counts",
  926. .mode = (S_IRUGO | S_IWUSR)
  927. },
  928. .show = i7core_ce_regs_show,
  929. .store = NULL,
  930. },
  931. };
  932. /****************************************************************************
  933. Device initialization routines: put/get, init/exit
  934. ****************************************************************************/
  935. /*
  936. * i7core_put_devices 'put' all the devices that we have
  937. * reserved via 'get'
  938. */
  939. static void i7core_put_devices(void)
  940. {
  941. int i, j;
  942. for (i = 0; i < NUM_SOCKETS; i++)
  943. for (j = 0; j < N_DEVS; j++)
  944. pci_dev_put(pci_devs[j].pdev[i]);
  945. }
  946. /*
  947. * i7core_get_devices Find and perform 'get' operation on the MCH's
  948. * device/functions we want to reference for this driver
  949. *
  950. * Need to 'get' device 16 func 1 and func 2
  951. */
  952. static int i7core_get_devices(void)
  953. {
  954. int rc, i;
  955. struct pci_dev *pdev = NULL;
  956. u8 bus = 0;
  957. u8 socket = 0;
  958. for (i = 0; i < N_DEVS; i++) {
  959. pdev = pci_get_device(PCI_VENDOR_ID_INTEL,
  960. pci_devs[i].dev_id, NULL);
  961. if (!pdev && !i) {
  962. pcibios_scan_specific_bus(254);
  963. pcibios_scan_specific_bus(255);
  964. pdev = pci_get_device(PCI_VENDOR_ID_INTEL,
  965. pci_devs[i].dev_id, NULL);
  966. }
  967. /*
  968. * On Xeon 55xx, the Intel Quckpath Arch Generic Non-core regs
  969. * is at addr 8086:2c40, instead of 8086:2c41. So, we need
  970. * to probe for the alternate address in case of failure
  971. */
  972. if (pci_devs[i].dev_id == PCI_DEVICE_ID_INTEL_I7_NOCORE
  973. && !pdev)
  974. pdev = pci_get_device(PCI_VENDOR_ID_INTEL,
  975. PCI_DEVICE_ID_INTEL_I7_NOCORE_ALT, NULL);
  976. if (likely(pdev)) {
  977. bus = pdev->bus->number;
  978. if (bus == 0x3f)
  979. socket = 0;
  980. else
  981. socket = 255 - bus;
  982. if (socket >= NUM_SOCKETS) {
  983. i7core_printk(KERN_ERR,
  984. "Found unexpected socket for "
  985. "dev %02x:%02x.%d PCI ID %04x:%04x\n",
  986. bus, pci_devs[i].dev, pci_devs[i].func,
  987. PCI_VENDOR_ID_INTEL, pci_devs[i].dev_id);
  988. rc = -ENODEV;
  989. goto error;
  990. }
  991. pci_devs[i].pdev[socket] = pdev;
  992. } else {
  993. i7core_printk(KERN_ERR,
  994. "Device not found: "
  995. "dev %02x:%02x.%d PCI ID %04x:%04x\n",
  996. bus, pci_devs[i].dev, pci_devs[i].func,
  997. PCI_VENDOR_ID_INTEL, pci_devs[i].dev_id);
  998. /* Dev 3 function 2 only exists on chips with RDIMMs */
  999. if ((pci_devs[i].dev == 3) && (pci_devs[i].func == 2))
  1000. continue;
  1001. /* End of list, leave */
  1002. rc = -ENODEV;
  1003. goto error;
  1004. }
  1005. /* Sanity check */
  1006. if (unlikely(PCI_SLOT(pdev->devfn) != pci_devs[i].dev ||
  1007. PCI_FUNC(pdev->devfn) != pci_devs[i].func)) {
  1008. i7core_printk(KERN_ERR,
  1009. "Device PCI ID %04x:%04x "
  1010. "has fn %d.%d instead of fn %d.%d\n",
  1011. PCI_VENDOR_ID_INTEL, pci_devs[i].dev_id,
  1012. PCI_SLOT(pdev->devfn), PCI_FUNC(pdev->devfn),
  1013. pci_devs[i].dev, pci_devs[i].func);
  1014. rc = -EINVAL;
  1015. goto error;
  1016. }
  1017. /* Be sure that the device is enabled */
  1018. rc = pci_enable_device(pdev);
  1019. if (unlikely(rc < 0)) {
  1020. i7core_printk(KERN_ERR,
  1021. "Couldn't enable PCI ID %04x:%04x "
  1022. "fn %d.%d\n",
  1023. PCI_VENDOR_ID_INTEL, pci_devs[i].dev_id,
  1024. PCI_SLOT(pdev->devfn), PCI_FUNC(pdev->devfn));
  1025. goto error;
  1026. }
  1027. i7core_printk(KERN_INFO,
  1028. "Registered socket %d "
  1029. "dev %02x:%02x.%d PCI ID %04x:%04x\n",
  1030. socket, bus, pci_devs[i].dev, pci_devs[i].func,
  1031. PCI_VENDOR_ID_INTEL, pci_devs[i].dev_id);
  1032. }
  1033. return 0;
  1034. error:
  1035. i7core_put_devices();
  1036. return -EINVAL;
  1037. }
  1038. static int mci_bind_devs(struct mem_ctl_info *mci)
  1039. {
  1040. struct i7core_pvt *pvt = mci->pvt_info;
  1041. struct pci_dev *pdev;
  1042. int i, j, func, slot;
  1043. for (i = 0; i < pvt->sockets; i++) {
  1044. for (j = 0; j < N_DEVS; j++) {
  1045. pdev = pci_devs[j].pdev[i];
  1046. if (!pdev)
  1047. continue;
  1048. func = PCI_FUNC(pdev->devfn);
  1049. slot = PCI_SLOT(pdev->devfn);
  1050. if (slot == 3) {
  1051. if (unlikely(func > MAX_MCR_FUNC))
  1052. goto error;
  1053. pvt->pci_mcr[i][func] = pdev;
  1054. } else if (likely(slot >= 4 && slot < 4 + NUM_CHANS)) {
  1055. if (unlikely(func > MAX_CHAN_FUNC))
  1056. goto error;
  1057. pvt->pci_ch[i][slot - 4][func] = pdev;
  1058. } else if (!slot && !func)
  1059. pvt->pci_noncore[i] = pdev;
  1060. else
  1061. goto error;
  1062. debugf0("Associated fn %d.%d, dev = %p, socket %d\n",
  1063. PCI_SLOT(pdev->devfn), PCI_FUNC(pdev->devfn),
  1064. pdev, i);
  1065. }
  1066. }
  1067. return 0;
  1068. error:
  1069. i7core_printk(KERN_ERR, "Device %d, function %d "
  1070. "is out of the expected range\n",
  1071. slot, func);
  1072. return -EINVAL;
  1073. }
  1074. /****************************************************************************
  1075. Error check routines
  1076. ****************************************************************************/
  1077. /* This function is based on the device 3 function 4 registers as described on:
  1078. * Intel Xeon Processor 5500 Series Datasheet Volume 2
  1079. * http://www.intel.com/Assets/PDF/datasheet/321322.pdf
  1080. * also available at:
  1081. * http://www.arrownac.com/manufacturers/intel/s/nehalem/5500-datasheet-v2.pdf
  1082. */
  1083. static void check_mc_test_err(struct mem_ctl_info *mci, u8 socket)
  1084. {
  1085. struct i7core_pvt *pvt = mci->pvt_info;
  1086. u32 rcv1, rcv0;
  1087. int new0, new1, new2;
  1088. if (!pvt->pci_mcr[socket][4]) {
  1089. debugf0("%s MCR registers not found\n",__func__);
  1090. return;
  1091. }
  1092. /* Corrected error reads */
  1093. pci_read_config_dword(pvt->pci_mcr[socket][4], MC_TEST_ERR_RCV1, &rcv1);
  1094. pci_read_config_dword(pvt->pci_mcr[socket][4], MC_TEST_ERR_RCV0, &rcv0);
  1095. /* Store the new values */
  1096. new2 = DIMM2_COR_ERR(rcv1);
  1097. new1 = DIMM1_COR_ERR(rcv0);
  1098. new0 = DIMM0_COR_ERR(rcv0);
  1099. #if 0
  1100. debugf2("%s CE rcv1=0x%08x rcv0=0x%08x, %d %d %d\n",
  1101. (pvt->ce_count_available ? "UPDATE" : "READ"),
  1102. rcv1, rcv0, new0, new1, new2);
  1103. #endif
  1104. /* Updates CE counters if it is not the first time here */
  1105. if (pvt->ce_count_available[socket]) {
  1106. /* Updates CE counters */
  1107. int add0, add1, add2;
  1108. add2 = new2 - pvt->last_ce_count[socket][2];
  1109. add1 = new1 - pvt->last_ce_count[socket][1];
  1110. add0 = new0 - pvt->last_ce_count[socket][0];
  1111. if (add2 < 0)
  1112. add2 += 0x7fff;
  1113. pvt->ce_count[socket][2] += add2;
  1114. if (add1 < 0)
  1115. add1 += 0x7fff;
  1116. pvt->ce_count[socket][1] += add1;
  1117. if (add0 < 0)
  1118. add0 += 0x7fff;
  1119. pvt->ce_count[socket][0] += add0;
  1120. } else
  1121. pvt->ce_count_available[socket] = 1;
  1122. /* Store the new values */
  1123. pvt->last_ce_count[socket][2] = new2;
  1124. pvt->last_ce_count[socket][1] = new1;
  1125. pvt->last_ce_count[socket][0] = new0;
  1126. }
  1127. /*
  1128. * According with tables E-11 and E-12 of chapter E.3.3 of Intel 64 and IA-32
  1129. * Architectures Software Developer’s Manual Volume 3B.
  1130. * Nehalem are defined as family 0x06, model 0x1a
  1131. *
  1132. * The MCA registers used here are the following ones:
  1133. * struct mce field MCA Register
  1134. * m->status MSR_IA32_MC8_STATUS
  1135. * m->addr MSR_IA32_MC8_ADDR
  1136. * m->misc MSR_IA32_MC8_MISC
  1137. * In the case of Nehalem, the error information is masked at .status and .misc
  1138. * fields
  1139. */
  1140. static void i7core_mce_output_error(struct mem_ctl_info *mci,
  1141. struct mce *m)
  1142. {
  1143. char *type;
  1144. char *err, *msg;
  1145. unsigned long error = m->status & 0x1ff0000l;
  1146. u32 core_err_cnt = (m->status >> 38) && 0x7fff;
  1147. u32 dimm = (m->misc >> 16) & 0x3;
  1148. u32 channel = (m->misc >> 18) & 0x3;
  1149. u32 syndrome = m->misc >> 32;
  1150. u32 errnum = find_first_bit(&error, 32);
  1151. if (m->mcgstatus & 1)
  1152. type = "FATAL";
  1153. else
  1154. type = "NON_FATAL";
  1155. switch (errnum) {
  1156. case 16:
  1157. err = "read ECC error";
  1158. break;
  1159. case 17:
  1160. err = "RAS ECC error";
  1161. break;
  1162. case 18:
  1163. err = "write parity error";
  1164. break;
  1165. case 19:
  1166. err = "redundacy loss";
  1167. break;
  1168. case 20:
  1169. err = "reserved";
  1170. break;
  1171. case 21:
  1172. err = "memory range error";
  1173. break;
  1174. case 22:
  1175. err = "RTID out of range";
  1176. break;
  1177. case 23:
  1178. err = "address parity error";
  1179. break;
  1180. case 24:
  1181. err = "byte enable parity error";
  1182. break;
  1183. default:
  1184. err = "unknown";
  1185. }
  1186. /* FIXME: should convert addr into bank and rank information */
  1187. msg = kasprintf(GFP_ATOMIC,
  1188. "%s (addr = 0x%08llx Dimm=%d, Channel=%d, "
  1189. "syndrome=0x%08x, count=%d Err=%d (%s))\n",
  1190. type, (long long) m->addr, dimm, channel,
  1191. syndrome, core_err_cnt,errnum, err);
  1192. debugf0("%s", msg);
  1193. /* Call the helper to output message */
  1194. edac_mc_handle_fbd_ue(mci, 0 /* FIXME: should be rank here */,
  1195. 0, 0 /* FIXME: should be channel here */, msg);
  1196. kfree(msg);
  1197. }
  1198. /*
  1199. * i7core_check_error Retrieve and process errors reported by the
  1200. * hardware. Called by the Core module.
  1201. */
  1202. static void i7core_check_error(struct mem_ctl_info *mci)
  1203. {
  1204. struct i7core_pvt *pvt = mci->pvt_info;
  1205. int i;
  1206. unsigned count = 0;
  1207. struct mce *m = NULL;
  1208. unsigned long flags;
  1209. debugf0(__FILE__ ": %s()\n", __func__);
  1210. /* Copy all mce errors into a temporary buffer */
  1211. spin_lock_irqsave(&pvt->mce_lock, flags);
  1212. if (pvt->mce_count) {
  1213. m = kmalloc(sizeof(*m) * pvt->mce_count, GFP_ATOMIC);
  1214. if (m) {
  1215. count = pvt->mce_count;
  1216. memcpy(m, &pvt->mce_entry, sizeof(*m) * count);
  1217. }
  1218. pvt->mce_count = 0;
  1219. }
  1220. spin_unlock_irqrestore(&pvt->mce_lock, flags);
  1221. /* proccess mcelog errors */
  1222. for (i = 0; i < count; i++)
  1223. i7core_mce_output_error(mci, &m[i]);
  1224. kfree(m);
  1225. /* check memory count errors */
  1226. for (i = 0; i < pvt->sockets; i++)
  1227. check_mc_test_err(mci, i);
  1228. }
  1229. /*
  1230. * i7core_mce_check_error Replicates mcelog routine to get errors
  1231. * This routine simply queues mcelog errors, and
  1232. * return. The error itself should be handled later
  1233. * by i7core_check_error.
  1234. */
  1235. static int i7core_mce_check_error(void *priv, struct mce *mce)
  1236. {
  1237. struct mem_ctl_info *mci = priv;
  1238. struct i7core_pvt *pvt = mci->pvt_info;
  1239. unsigned long flags;
  1240. debugf0(__FILE__ ": %s()\n", __func__);
  1241. /*
  1242. * Just let mcelog handle it if the error is
  1243. * outside the memory controller
  1244. */
  1245. if (((mce->status & 0xffff) >> 7) != 1)
  1246. return 0;
  1247. /* Bank 8 registers are the only ones that we know how to handle */
  1248. if (mce->bank != 8)
  1249. return 0;
  1250. spin_lock_irqsave(&pvt->mce_lock, flags);
  1251. if (pvt->mce_count < MCE_LOG_LEN) {
  1252. memcpy(&pvt->mce_entry[pvt->mce_count], mce, sizeof(*mce));
  1253. pvt->mce_count++;
  1254. }
  1255. spin_unlock_irqrestore(&pvt->mce_lock, flags);
  1256. /* Handle fatal errors immediately */
  1257. if (mce->mcgstatus & 1)
  1258. i7core_check_error(mci);
  1259. /* Advice mcelog that the error were handled */
  1260. return 1;
  1261. }
  1262. /*
  1263. * i7core_probe Probe for ONE instance of device to see if it is
  1264. * present.
  1265. * return:
  1266. * 0 for FOUND a device
  1267. * < 0 for error code
  1268. */
  1269. static int __devinit i7core_probe(struct pci_dev *pdev,
  1270. const struct pci_device_id *id)
  1271. {
  1272. struct mem_ctl_info *mci;
  1273. struct i7core_pvt *pvt;
  1274. int num_channels = 0;
  1275. int num_csrows = 0;
  1276. int csrow = 0;
  1277. int dev_idx = id->driver_data;
  1278. int rc, i;
  1279. u8 sockets;
  1280. if (unlikely(dev_idx >= ARRAY_SIZE(i7core_devs)))
  1281. return -EINVAL;
  1282. /* get the pci devices we want to reserve for our use */
  1283. rc = i7core_get_devices();
  1284. if (unlikely(rc < 0))
  1285. return rc;
  1286. sockets = 1;
  1287. for (i = NUM_SOCKETS - 1; i > 0; i--)
  1288. if (pci_devs[0].pdev[i]) {
  1289. sockets = i + 1;
  1290. break;
  1291. }
  1292. for (i = 0; i < sockets; i++) {
  1293. int channels;
  1294. int csrows;
  1295. /* Check the number of active and not disabled channels */
  1296. rc = i7core_get_active_channels(i, &channels, &csrows);
  1297. if (unlikely(rc < 0))
  1298. goto fail0;
  1299. num_channels += channels;
  1300. num_csrows += csrows;
  1301. }
  1302. /* allocate a new MC control structure */
  1303. mci = edac_mc_alloc(sizeof(*pvt), num_csrows, num_channels, 0);
  1304. if (unlikely(!mci)) {
  1305. rc = -ENOMEM;
  1306. goto fail0;
  1307. }
  1308. debugf0("MC: " __FILE__ ": %s(): mci = %p\n", __func__, mci);
  1309. mci->dev = &pdev->dev; /* record ptr to the generic device */
  1310. pvt = mci->pvt_info;
  1311. memset(pvt, 0, sizeof(*pvt));
  1312. pvt->sockets = sockets;
  1313. mci->mc_idx = 0;
  1314. /*
  1315. * FIXME: how to handle RDDR3 at MCI level? It is possible to have
  1316. * Mixed RDDR3/UDDR3 with Nehalem, provided that they are on different
  1317. * memory channels
  1318. */
  1319. mci->mtype_cap = MEM_FLAG_DDR3;
  1320. mci->edac_ctl_cap = EDAC_FLAG_NONE;
  1321. mci->edac_cap = EDAC_FLAG_NONE;
  1322. mci->mod_name = "i7core_edac.c";
  1323. mci->mod_ver = I7CORE_REVISION;
  1324. mci->ctl_name = i7core_devs[dev_idx].ctl_name;
  1325. mci->dev_name = pci_name(pdev);
  1326. mci->ctl_page_to_phys = NULL;
  1327. mci->mc_driver_sysfs_attributes = i7core_inj_attrs;
  1328. /* Set the function pointer to an actual operation function */
  1329. mci->edac_check = i7core_check_error;
  1330. /* Store pci devices at mci for faster access */
  1331. rc = mci_bind_devs(mci);
  1332. if (unlikely(rc < 0))
  1333. goto fail1;
  1334. /* Get dimm basic config */
  1335. for (i = 0; i < sockets; i++)
  1336. get_dimm_config(mci, &csrow, i);
  1337. /* add this new MC control structure to EDAC's list of MCs */
  1338. if (unlikely(edac_mc_add_mc(mci))) {
  1339. debugf0("MC: " __FILE__
  1340. ": %s(): failed edac_mc_add_mc()\n", __func__);
  1341. /* FIXME: perhaps some code should go here that disables error
  1342. * reporting if we just enabled it
  1343. */
  1344. rc = -EINVAL;
  1345. goto fail1;
  1346. }
  1347. /* allocating generic PCI control info */
  1348. i7core_pci = edac_pci_create_generic_ctl(&pdev->dev, EDAC_MOD_STR);
  1349. if (unlikely(!i7core_pci)) {
  1350. printk(KERN_WARNING
  1351. "%s(): Unable to create PCI control\n",
  1352. __func__);
  1353. printk(KERN_WARNING
  1354. "%s(): PCI error report via EDAC not setup\n",
  1355. __func__);
  1356. }
  1357. /* Default error mask is any memory */
  1358. pvt->inject.channel = 0;
  1359. pvt->inject.dimm = -1;
  1360. pvt->inject.rank = -1;
  1361. pvt->inject.bank = -1;
  1362. pvt->inject.page = -1;
  1363. pvt->inject.col = -1;
  1364. /* Registers on edac_mce in order to receive memory errors */
  1365. pvt->edac_mce.priv = mci;
  1366. pvt->edac_mce.check_error = i7core_mce_check_error;
  1367. spin_lock_init(&pvt->mce_lock);
  1368. rc = edac_mce_register(&pvt->edac_mce);
  1369. if (unlikely (rc < 0)) {
  1370. debugf0("MC: " __FILE__
  1371. ": %s(): failed edac_mce_register()\n", __func__);
  1372. goto fail1;
  1373. }
  1374. i7core_printk(KERN_INFO, "Driver loaded.\n");
  1375. return 0;
  1376. fail1:
  1377. edac_mc_free(mci);
  1378. fail0:
  1379. i7core_put_devices();
  1380. return rc;
  1381. }
  1382. /*
  1383. * i7core_remove destructor for one instance of device
  1384. *
  1385. */
  1386. static void __devexit i7core_remove(struct pci_dev *pdev)
  1387. {
  1388. struct mem_ctl_info *mci;
  1389. struct i7core_pvt *pvt;
  1390. debugf0(__FILE__ ": %s()\n", __func__);
  1391. if (i7core_pci)
  1392. edac_pci_release_generic_ctl(i7core_pci);
  1393. mci = edac_mc_del_mc(&pdev->dev);
  1394. if (!mci)
  1395. return;
  1396. /* Unregisters on edac_mce in order to receive memory errors */
  1397. pvt = mci->pvt_info;
  1398. edac_mce_unregister(&pvt->edac_mce);
  1399. /* retrieve references to resources, and free those resources */
  1400. i7core_put_devices();
  1401. edac_mc_free(mci);
  1402. }
  1403. MODULE_DEVICE_TABLE(pci, i7core_pci_tbl);
  1404. /*
  1405. * i7core_driver pci_driver structure for this module
  1406. *
  1407. */
  1408. static struct pci_driver i7core_driver = {
  1409. .name = "i7core_edac",
  1410. .probe = i7core_probe,
  1411. .remove = __devexit_p(i7core_remove),
  1412. .id_table = i7core_pci_tbl,
  1413. };
  1414. /*
  1415. * i7core_init Module entry function
  1416. * Try to initialize this module for its devices
  1417. */
  1418. static int __init i7core_init(void)
  1419. {
  1420. int pci_rc;
  1421. debugf2("MC: " __FILE__ ": %s()\n", __func__);
  1422. /* Ensure that the OPSTATE is set correctly for POLL or NMI */
  1423. opstate_init();
  1424. pci_rc = pci_register_driver(&i7core_driver);
  1425. return (pci_rc < 0) ? pci_rc : 0;
  1426. }
  1427. /*
  1428. * i7core_exit() Module exit function
  1429. * Unregister the driver
  1430. */
  1431. static void __exit i7core_exit(void)
  1432. {
  1433. debugf2("MC: " __FILE__ ": %s()\n", __func__);
  1434. pci_unregister_driver(&i7core_driver);
  1435. }
  1436. module_init(i7core_init);
  1437. module_exit(i7core_exit);
  1438. MODULE_LICENSE("GPL");
  1439. MODULE_AUTHOR("Mauro Carvalho Chehab <mchehab@redhat.com>");
  1440. MODULE_AUTHOR("Red Hat Inc. (http://www.redhat.com)");
  1441. MODULE_DESCRIPTION("MC Driver for Intel i7 Core memory controllers - "
  1442. I7CORE_REVISION);
  1443. module_param(edac_op_state, int, 0444);
  1444. MODULE_PARM_DESC(edac_op_state, "EDAC Error Reporting state: 0=Poll,1=NMI");