i7core_edac.c 64 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512
  1. /* Intel i7 core/Nehalem Memory Controller kernel module
  2. *
  3. * This driver supports the memory controllers found on the Intel
  4. * processor families i7core, i7core 7xx/8xx, i5core, Xeon 35xx,
  5. * Xeon 55xx and Xeon 56xx also known as Nehalem, Nehalem-EP, Lynnfield
  6. * and Westmere-EP.
  7. *
  8. * This file may be distributed under the terms of the
  9. * GNU General Public License version 2 only.
  10. *
  11. * Copyright (c) 2009-2010 by:
  12. * Mauro Carvalho Chehab <mchehab@redhat.com>
  13. *
  14. * Red Hat Inc. http://www.redhat.com
  15. *
  16. * Forked and adapted from the i5400_edac driver
  17. *
  18. * Based on the following public Intel datasheets:
  19. * Intel Core i7 Processor Extreme Edition and Intel Core i7 Processor
  20. * Datasheet, Volume 2:
  21. * http://download.intel.com/design/processor/datashts/320835.pdf
  22. * Intel Xeon Processor 5500 Series Datasheet Volume 2
  23. * http://www.intel.com/Assets/PDF/datasheet/321322.pdf
  24. * also available at:
  25. * http://www.arrownac.com/manufacturers/intel/s/nehalem/5500-datasheet-v2.pdf
  26. */
  27. #include <linux/module.h>
  28. #include <linux/init.h>
  29. #include <linux/pci.h>
  30. #include <linux/pci_ids.h>
  31. #include <linux/slab.h>
  32. #include <linux/delay.h>
  33. #include <linux/dmi.h>
  34. #include <linux/edac.h>
  35. #include <linux/mmzone.h>
  36. #include <linux/smp.h>
  37. #include <asm/mce.h>
  38. #include <asm/processor.h>
  39. #include <asm/div64.h>
  40. #include "edac_core.h"
  41. /* Static vars */
  42. static LIST_HEAD(i7core_edac_list);
  43. static DEFINE_MUTEX(i7core_edac_lock);
  44. static int probed;
  45. static int use_pci_fixup;
  46. module_param(use_pci_fixup, int, 0444);
  47. MODULE_PARM_DESC(use_pci_fixup, "Enable PCI fixup to seek for hidden devices");
  48. /*
  49. * This is used for Nehalem-EP and Nehalem-EX devices, where the non-core
  50. * registers start at bus 255, and are not reported by BIOS.
  51. * We currently find devices with only 2 sockets. In order to support more QPI
  52. * Quick Path Interconnect, just increment this number.
  53. */
  54. #define MAX_SOCKET_BUSES 2
  55. /*
  56. * Alter this version for the module when modifications are made
  57. */
  58. #define I7CORE_REVISION " Ver: 1.0.0"
  59. #define EDAC_MOD_STR "i7core_edac"
  60. /*
  61. * Debug macros
  62. */
  63. #define i7core_printk(level, fmt, arg...) \
  64. edac_printk(level, "i7core", fmt, ##arg)
  65. #define i7core_mc_printk(mci, level, fmt, arg...) \
  66. edac_mc_chipset_printk(mci, level, "i7core", fmt, ##arg)
  67. /*
  68. * i7core Memory Controller Registers
  69. */
  70. /* OFFSETS for Device 0 Function 0 */
  71. #define MC_CFG_CONTROL 0x90
  72. #define MC_CFG_UNLOCK 0x02
  73. #define MC_CFG_LOCK 0x00
  74. /* OFFSETS for Device 3 Function 0 */
  75. #define MC_CONTROL 0x48
  76. #define MC_STATUS 0x4c
  77. #define MC_MAX_DOD 0x64
  78. /*
  79. * OFFSETS for Device 3 Function 4, as inicated on Xeon 5500 datasheet:
  80. * http://www.arrownac.com/manufacturers/intel/s/nehalem/5500-datasheet-v2.pdf
  81. */
  82. #define MC_TEST_ERR_RCV1 0x60
  83. #define DIMM2_COR_ERR(r) ((r) & 0x7fff)
  84. #define MC_TEST_ERR_RCV0 0x64
  85. #define DIMM1_COR_ERR(r) (((r) >> 16) & 0x7fff)
  86. #define DIMM0_COR_ERR(r) ((r) & 0x7fff)
  87. /* OFFSETS for Device 3 Function 2, as inicated on Xeon 5500 datasheet */
  88. #define MC_SSRCONTROL 0x48
  89. #define SSR_MODE_DISABLE 0x00
  90. #define SSR_MODE_ENABLE 0x01
  91. #define SSR_MODE_MASK 0x03
  92. #define MC_SCRUB_CONTROL 0x4c
  93. #define STARTSCRUB (1 << 24)
  94. #define SCRUBINTERVAL_MASK 0xffffff
  95. #define MC_COR_ECC_CNT_0 0x80
  96. #define MC_COR_ECC_CNT_1 0x84
  97. #define MC_COR_ECC_CNT_2 0x88
  98. #define MC_COR_ECC_CNT_3 0x8c
  99. #define MC_COR_ECC_CNT_4 0x90
  100. #define MC_COR_ECC_CNT_5 0x94
  101. #define DIMM_TOP_COR_ERR(r) (((r) >> 16) & 0x7fff)
  102. #define DIMM_BOT_COR_ERR(r) ((r) & 0x7fff)
  103. /* OFFSETS for Devices 4,5 and 6 Function 0 */
  104. #define MC_CHANNEL_DIMM_INIT_PARAMS 0x58
  105. #define THREE_DIMMS_PRESENT (1 << 24)
  106. #define SINGLE_QUAD_RANK_PRESENT (1 << 23)
  107. #define QUAD_RANK_PRESENT (1 << 22)
  108. #define REGISTERED_DIMM (1 << 15)
  109. #define MC_CHANNEL_MAPPER 0x60
  110. #define RDLCH(r, ch) ((((r) >> (3 + (ch * 6))) & 0x07) - 1)
  111. #define WRLCH(r, ch) ((((r) >> (ch * 6)) & 0x07) - 1)
  112. #define MC_CHANNEL_RANK_PRESENT 0x7c
  113. #define RANK_PRESENT_MASK 0xffff
  114. #define MC_CHANNEL_ADDR_MATCH 0xf0
  115. #define MC_CHANNEL_ERROR_MASK 0xf8
  116. #define MC_CHANNEL_ERROR_INJECT 0xfc
  117. #define INJECT_ADDR_PARITY 0x10
  118. #define INJECT_ECC 0x08
  119. #define MASK_CACHELINE 0x06
  120. #define MASK_FULL_CACHELINE 0x06
  121. #define MASK_MSB32_CACHELINE 0x04
  122. #define MASK_LSB32_CACHELINE 0x02
  123. #define NO_MASK_CACHELINE 0x00
  124. #define REPEAT_EN 0x01
  125. /* OFFSETS for Devices 4,5 and 6 Function 1 */
  126. #define MC_DOD_CH_DIMM0 0x48
  127. #define MC_DOD_CH_DIMM1 0x4c
  128. #define MC_DOD_CH_DIMM2 0x50
  129. #define RANKOFFSET_MASK ((1 << 12) | (1 << 11) | (1 << 10))
  130. #define RANKOFFSET(x) ((x & RANKOFFSET_MASK) >> 10)
  131. #define DIMM_PRESENT_MASK (1 << 9)
  132. #define DIMM_PRESENT(x) (((x) & DIMM_PRESENT_MASK) >> 9)
  133. #define MC_DOD_NUMBANK_MASK ((1 << 8) | (1 << 7))
  134. #define MC_DOD_NUMBANK(x) (((x) & MC_DOD_NUMBANK_MASK) >> 7)
  135. #define MC_DOD_NUMRANK_MASK ((1 << 6) | (1 << 5))
  136. #define MC_DOD_NUMRANK(x) (((x) & MC_DOD_NUMRANK_MASK) >> 5)
  137. #define MC_DOD_NUMROW_MASK ((1 << 4) | (1 << 3) | (1 << 2))
  138. #define MC_DOD_NUMROW(x) (((x) & MC_DOD_NUMROW_MASK) >> 2)
  139. #define MC_DOD_NUMCOL_MASK 3
  140. #define MC_DOD_NUMCOL(x) ((x) & MC_DOD_NUMCOL_MASK)
  141. #define MC_RANK_PRESENT 0x7c
  142. #define MC_SAG_CH_0 0x80
  143. #define MC_SAG_CH_1 0x84
  144. #define MC_SAG_CH_2 0x88
  145. #define MC_SAG_CH_3 0x8c
  146. #define MC_SAG_CH_4 0x90
  147. #define MC_SAG_CH_5 0x94
  148. #define MC_SAG_CH_6 0x98
  149. #define MC_SAG_CH_7 0x9c
  150. #define MC_RIR_LIMIT_CH_0 0x40
  151. #define MC_RIR_LIMIT_CH_1 0x44
  152. #define MC_RIR_LIMIT_CH_2 0x48
  153. #define MC_RIR_LIMIT_CH_3 0x4C
  154. #define MC_RIR_LIMIT_CH_4 0x50
  155. #define MC_RIR_LIMIT_CH_5 0x54
  156. #define MC_RIR_LIMIT_CH_6 0x58
  157. #define MC_RIR_LIMIT_CH_7 0x5C
  158. #define MC_RIR_LIMIT_MASK ((1 << 10) - 1)
  159. #define MC_RIR_WAY_CH 0x80
  160. #define MC_RIR_WAY_OFFSET_MASK (((1 << 14) - 1) & ~0x7)
  161. #define MC_RIR_WAY_RANK_MASK 0x7
  162. /*
  163. * i7core structs
  164. */
  165. #define NUM_CHANS 3
  166. #define MAX_DIMMS 3 /* Max DIMMS per channel */
  167. #define MAX_MCR_FUNC 4
  168. #define MAX_CHAN_FUNC 3
  169. struct i7core_info {
  170. u32 mc_control;
  171. u32 mc_status;
  172. u32 max_dod;
  173. u32 ch_map;
  174. };
  175. struct i7core_inject {
  176. int enable;
  177. u32 section;
  178. u32 type;
  179. u32 eccmask;
  180. /* Error address mask */
  181. int channel, dimm, rank, bank, page, col;
  182. };
  183. struct i7core_channel {
  184. u32 ranks;
  185. u32 dimms;
  186. };
  187. struct pci_id_descr {
  188. int dev;
  189. int func;
  190. int dev_id;
  191. int optional;
  192. };
  193. struct pci_id_table {
  194. const struct pci_id_descr *descr;
  195. int n_devs;
  196. };
  197. struct i7core_dev {
  198. struct list_head list;
  199. u8 socket;
  200. struct pci_dev **pdev;
  201. int n_devs;
  202. struct mem_ctl_info *mci;
  203. };
  204. struct i7core_pvt {
  205. struct pci_dev *pci_noncore;
  206. struct pci_dev *pci_mcr[MAX_MCR_FUNC + 1];
  207. struct pci_dev *pci_ch[NUM_CHANS][MAX_CHAN_FUNC + 1];
  208. struct i7core_dev *i7core_dev;
  209. struct i7core_info info;
  210. struct i7core_inject inject;
  211. struct i7core_channel channel[NUM_CHANS];
  212. int ce_count_available;
  213. int csrow_map[NUM_CHANS][MAX_DIMMS];
  214. /* ECC corrected errors counts per udimm */
  215. unsigned long udimm_ce_count[MAX_DIMMS];
  216. int udimm_last_ce_count[MAX_DIMMS];
  217. /* ECC corrected errors counts per rdimm */
  218. unsigned long rdimm_ce_count[NUM_CHANS][MAX_DIMMS];
  219. int rdimm_last_ce_count[NUM_CHANS][MAX_DIMMS];
  220. bool is_registered, enable_scrub;
  221. /* Fifo double buffers */
  222. struct mce mce_entry[MCE_LOG_LEN];
  223. struct mce mce_outentry[MCE_LOG_LEN];
  224. /* Fifo in/out counters */
  225. unsigned mce_in, mce_out;
  226. /* Count indicator to show errors not got */
  227. unsigned mce_overrun;
  228. /* DCLK Frequency used for computing scrub rate */
  229. int dclk_freq;
  230. /* Struct to control EDAC polling */
  231. struct edac_pci_ctl_info *i7core_pci;
  232. };
  233. #define PCI_DESCR(device, function, device_id) \
  234. .dev = (device), \
  235. .func = (function), \
  236. .dev_id = (device_id)
  237. static const struct pci_id_descr pci_dev_descr_i7core_nehalem[] = {
  238. /* Memory controller */
  239. { PCI_DESCR(3, 0, PCI_DEVICE_ID_INTEL_I7_MCR) },
  240. { PCI_DESCR(3, 1, PCI_DEVICE_ID_INTEL_I7_MC_TAD) },
  241. /* Exists only for RDIMM */
  242. { PCI_DESCR(3, 2, PCI_DEVICE_ID_INTEL_I7_MC_RAS), .optional = 1 },
  243. { PCI_DESCR(3, 4, PCI_DEVICE_ID_INTEL_I7_MC_TEST) },
  244. /* Channel 0 */
  245. { PCI_DESCR(4, 0, PCI_DEVICE_ID_INTEL_I7_MC_CH0_CTRL) },
  246. { PCI_DESCR(4, 1, PCI_DEVICE_ID_INTEL_I7_MC_CH0_ADDR) },
  247. { PCI_DESCR(4, 2, PCI_DEVICE_ID_INTEL_I7_MC_CH0_RANK) },
  248. { PCI_DESCR(4, 3, PCI_DEVICE_ID_INTEL_I7_MC_CH0_TC) },
  249. /* Channel 1 */
  250. { PCI_DESCR(5, 0, PCI_DEVICE_ID_INTEL_I7_MC_CH1_CTRL) },
  251. { PCI_DESCR(5, 1, PCI_DEVICE_ID_INTEL_I7_MC_CH1_ADDR) },
  252. { PCI_DESCR(5, 2, PCI_DEVICE_ID_INTEL_I7_MC_CH1_RANK) },
  253. { PCI_DESCR(5, 3, PCI_DEVICE_ID_INTEL_I7_MC_CH1_TC) },
  254. /* Channel 2 */
  255. { PCI_DESCR(6, 0, PCI_DEVICE_ID_INTEL_I7_MC_CH2_CTRL) },
  256. { PCI_DESCR(6, 1, PCI_DEVICE_ID_INTEL_I7_MC_CH2_ADDR) },
  257. { PCI_DESCR(6, 2, PCI_DEVICE_ID_INTEL_I7_MC_CH2_RANK) },
  258. { PCI_DESCR(6, 3, PCI_DEVICE_ID_INTEL_I7_MC_CH2_TC) },
  259. /* Generic Non-core registers */
  260. /*
  261. * This is the PCI device on i7core and on Xeon 35xx (8086:2c41)
  262. * On Xeon 55xx, however, it has a different id (8086:2c40). So,
  263. * the probing code needs to test for the other address in case of
  264. * failure of this one
  265. */
  266. { PCI_DESCR(0, 0, PCI_DEVICE_ID_INTEL_I7_NONCORE) },
  267. };
  268. static const struct pci_id_descr pci_dev_descr_lynnfield[] = {
  269. { PCI_DESCR( 3, 0, PCI_DEVICE_ID_INTEL_LYNNFIELD_MCR) },
  270. { PCI_DESCR( 3, 1, PCI_DEVICE_ID_INTEL_LYNNFIELD_MC_TAD) },
  271. { PCI_DESCR( 3, 4, PCI_DEVICE_ID_INTEL_LYNNFIELD_MC_TEST) },
  272. { PCI_DESCR( 4, 0, PCI_DEVICE_ID_INTEL_LYNNFIELD_MC_CH0_CTRL) },
  273. { PCI_DESCR( 4, 1, PCI_DEVICE_ID_INTEL_LYNNFIELD_MC_CH0_ADDR) },
  274. { PCI_DESCR( 4, 2, PCI_DEVICE_ID_INTEL_LYNNFIELD_MC_CH0_RANK) },
  275. { PCI_DESCR( 4, 3, PCI_DEVICE_ID_INTEL_LYNNFIELD_MC_CH0_TC) },
  276. { PCI_DESCR( 5, 0, PCI_DEVICE_ID_INTEL_LYNNFIELD_MC_CH1_CTRL) },
  277. { PCI_DESCR( 5, 1, PCI_DEVICE_ID_INTEL_LYNNFIELD_MC_CH1_ADDR) },
  278. { PCI_DESCR( 5, 2, PCI_DEVICE_ID_INTEL_LYNNFIELD_MC_CH1_RANK) },
  279. { PCI_DESCR( 5, 3, PCI_DEVICE_ID_INTEL_LYNNFIELD_MC_CH1_TC) },
  280. /*
  281. * This is the PCI device has an alternate address on some
  282. * processors like Core i7 860
  283. */
  284. { PCI_DESCR( 0, 0, PCI_DEVICE_ID_INTEL_LYNNFIELD_NONCORE) },
  285. };
  286. static const struct pci_id_descr pci_dev_descr_i7core_westmere[] = {
  287. /* Memory controller */
  288. { PCI_DESCR(3, 0, PCI_DEVICE_ID_INTEL_LYNNFIELD_MCR_REV2) },
  289. { PCI_DESCR(3, 1, PCI_DEVICE_ID_INTEL_LYNNFIELD_MC_TAD_REV2) },
  290. /* Exists only for RDIMM */
  291. { PCI_DESCR(3, 2, PCI_DEVICE_ID_INTEL_LYNNFIELD_MC_RAS_REV2), .optional = 1 },
  292. { PCI_DESCR(3, 4, PCI_DEVICE_ID_INTEL_LYNNFIELD_MC_TEST_REV2) },
  293. /* Channel 0 */
  294. { PCI_DESCR(4, 0, PCI_DEVICE_ID_INTEL_LYNNFIELD_MC_CH0_CTRL_REV2) },
  295. { PCI_DESCR(4, 1, PCI_DEVICE_ID_INTEL_LYNNFIELD_MC_CH0_ADDR_REV2) },
  296. { PCI_DESCR(4, 2, PCI_DEVICE_ID_INTEL_LYNNFIELD_MC_CH0_RANK_REV2) },
  297. { PCI_DESCR(4, 3, PCI_DEVICE_ID_INTEL_LYNNFIELD_MC_CH0_TC_REV2) },
  298. /* Channel 1 */
  299. { PCI_DESCR(5, 0, PCI_DEVICE_ID_INTEL_LYNNFIELD_MC_CH1_CTRL_REV2) },
  300. { PCI_DESCR(5, 1, PCI_DEVICE_ID_INTEL_LYNNFIELD_MC_CH1_ADDR_REV2) },
  301. { PCI_DESCR(5, 2, PCI_DEVICE_ID_INTEL_LYNNFIELD_MC_CH1_RANK_REV2) },
  302. { PCI_DESCR(5, 3, PCI_DEVICE_ID_INTEL_LYNNFIELD_MC_CH1_TC_REV2) },
  303. /* Channel 2 */
  304. { PCI_DESCR(6, 0, PCI_DEVICE_ID_INTEL_LYNNFIELD_MC_CH2_CTRL_REV2) },
  305. { PCI_DESCR(6, 1, PCI_DEVICE_ID_INTEL_LYNNFIELD_MC_CH2_ADDR_REV2) },
  306. { PCI_DESCR(6, 2, PCI_DEVICE_ID_INTEL_LYNNFIELD_MC_CH2_RANK_REV2) },
  307. { PCI_DESCR(6, 3, PCI_DEVICE_ID_INTEL_LYNNFIELD_MC_CH2_TC_REV2) },
  308. /* Generic Non-core registers */
  309. { PCI_DESCR(0, 0, PCI_DEVICE_ID_INTEL_LYNNFIELD_NONCORE_REV2) },
  310. };
  311. #define PCI_ID_TABLE_ENTRY(A) { .descr=A, .n_devs = ARRAY_SIZE(A) }
  312. static const struct pci_id_table pci_dev_table[] = {
  313. PCI_ID_TABLE_ENTRY(pci_dev_descr_i7core_nehalem),
  314. PCI_ID_TABLE_ENTRY(pci_dev_descr_lynnfield),
  315. PCI_ID_TABLE_ENTRY(pci_dev_descr_i7core_westmere),
  316. {0,} /* 0 terminated list. */
  317. };
  318. /*
  319. * pci_device_id table for which devices we are looking for
  320. */
  321. static DEFINE_PCI_DEVICE_TABLE(i7core_pci_tbl) = {
  322. {PCI_DEVICE(PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_X58_HUB_MGMT)},
  323. {PCI_DEVICE(PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_LYNNFIELD_QPI_LINK0)},
  324. {0,} /* 0 terminated list. */
  325. };
  326. /****************************************************************************
  327. Anciliary status routines
  328. ****************************************************************************/
  329. /* MC_CONTROL bits */
  330. #define CH_ACTIVE(pvt, ch) ((pvt)->info.mc_control & (1 << (8 + ch)))
  331. #define ECCx8(pvt) ((pvt)->info.mc_control & (1 << 1))
  332. /* MC_STATUS bits */
  333. #define ECC_ENABLED(pvt) ((pvt)->info.mc_status & (1 << 4))
  334. #define CH_DISABLED(pvt, ch) ((pvt)->info.mc_status & (1 << ch))
  335. /* MC_MAX_DOD read functions */
  336. static inline int numdimms(u32 dimms)
  337. {
  338. return (dimms & 0x3) + 1;
  339. }
  340. static inline int numrank(u32 rank)
  341. {
  342. static int ranks[4] = { 1, 2, 4, -EINVAL };
  343. return ranks[rank & 0x3];
  344. }
  345. static inline int numbank(u32 bank)
  346. {
  347. static int banks[4] = { 4, 8, 16, -EINVAL };
  348. return banks[bank & 0x3];
  349. }
  350. static inline int numrow(u32 row)
  351. {
  352. static int rows[8] = {
  353. 1 << 12, 1 << 13, 1 << 14, 1 << 15,
  354. 1 << 16, -EINVAL, -EINVAL, -EINVAL,
  355. };
  356. return rows[row & 0x7];
  357. }
  358. static inline int numcol(u32 col)
  359. {
  360. static int cols[8] = {
  361. 1 << 10, 1 << 11, 1 << 12, -EINVAL,
  362. };
  363. return cols[col & 0x3];
  364. }
  365. static struct i7core_dev *get_i7core_dev(u8 socket)
  366. {
  367. struct i7core_dev *i7core_dev;
  368. list_for_each_entry(i7core_dev, &i7core_edac_list, list) {
  369. if (i7core_dev->socket == socket)
  370. return i7core_dev;
  371. }
  372. return NULL;
  373. }
  374. static struct i7core_dev *alloc_i7core_dev(u8 socket,
  375. const struct pci_id_table *table)
  376. {
  377. struct i7core_dev *i7core_dev;
  378. i7core_dev = kzalloc(sizeof(*i7core_dev), GFP_KERNEL);
  379. if (!i7core_dev)
  380. return NULL;
  381. i7core_dev->pdev = kzalloc(sizeof(*i7core_dev->pdev) * table->n_devs,
  382. GFP_KERNEL);
  383. if (!i7core_dev->pdev) {
  384. kfree(i7core_dev);
  385. return NULL;
  386. }
  387. i7core_dev->socket = socket;
  388. i7core_dev->n_devs = table->n_devs;
  389. list_add_tail(&i7core_dev->list, &i7core_edac_list);
  390. return i7core_dev;
  391. }
  392. static void free_i7core_dev(struct i7core_dev *i7core_dev)
  393. {
  394. list_del(&i7core_dev->list);
  395. kfree(i7core_dev->pdev);
  396. kfree(i7core_dev);
  397. }
  398. /****************************************************************************
  399. Memory check routines
  400. ****************************************************************************/
  401. static struct pci_dev *get_pdev_slot_func(u8 socket, unsigned slot,
  402. unsigned func)
  403. {
  404. struct i7core_dev *i7core_dev = get_i7core_dev(socket);
  405. int i;
  406. if (!i7core_dev)
  407. return NULL;
  408. for (i = 0; i < i7core_dev->n_devs; i++) {
  409. if (!i7core_dev->pdev[i])
  410. continue;
  411. if (PCI_SLOT(i7core_dev->pdev[i]->devfn) == slot &&
  412. PCI_FUNC(i7core_dev->pdev[i]->devfn) == func) {
  413. return i7core_dev->pdev[i];
  414. }
  415. }
  416. return NULL;
  417. }
  418. /**
  419. * i7core_get_active_channels() - gets the number of channels and csrows
  420. * @socket: Quick Path Interconnect socket
  421. * @channels: Number of channels that will be returned
  422. * @csrows: Number of csrows found
  423. *
  424. * Since EDAC core needs to know in advance the number of available channels
  425. * and csrows, in order to allocate memory for csrows/channels, it is needed
  426. * to run two similar steps. At the first step, implemented on this function,
  427. * it checks the number of csrows/channels present at one socket.
  428. * this is used in order to properly allocate the size of mci components.
  429. *
  430. * It should be noticed that none of the current available datasheets explain
  431. * or even mention how csrows are seen by the memory controller. So, we need
  432. * to add a fake description for csrows.
  433. * So, this driver is attributing one DIMM memory for one csrow.
  434. */
  435. static int i7core_get_active_channels(const u8 socket, unsigned *channels,
  436. unsigned *csrows)
  437. {
  438. struct pci_dev *pdev = NULL;
  439. int i, j;
  440. u32 status, control;
  441. *channels = 0;
  442. *csrows = 0;
  443. pdev = get_pdev_slot_func(socket, 3, 0);
  444. if (!pdev) {
  445. i7core_printk(KERN_ERR, "Couldn't find socket %d fn 3.0!!!\n",
  446. socket);
  447. return -ENODEV;
  448. }
  449. /* Device 3 function 0 reads */
  450. pci_read_config_dword(pdev, MC_STATUS, &status);
  451. pci_read_config_dword(pdev, MC_CONTROL, &control);
  452. for (i = 0; i < NUM_CHANS; i++) {
  453. u32 dimm_dod[3];
  454. /* Check if the channel is active */
  455. if (!(control & (1 << (8 + i))))
  456. continue;
  457. /* Check if the channel is disabled */
  458. if (status & (1 << i))
  459. continue;
  460. pdev = get_pdev_slot_func(socket, i + 4, 1);
  461. if (!pdev) {
  462. i7core_printk(KERN_ERR, "Couldn't find socket %d "
  463. "fn %d.%d!!!\n",
  464. socket, i + 4, 1);
  465. return -ENODEV;
  466. }
  467. /* Devices 4-6 function 1 */
  468. pci_read_config_dword(pdev,
  469. MC_DOD_CH_DIMM0, &dimm_dod[0]);
  470. pci_read_config_dword(pdev,
  471. MC_DOD_CH_DIMM1, &dimm_dod[1]);
  472. pci_read_config_dword(pdev,
  473. MC_DOD_CH_DIMM2, &dimm_dod[2]);
  474. (*channels)++;
  475. for (j = 0; j < 3; j++) {
  476. if (!DIMM_PRESENT(dimm_dod[j]))
  477. continue;
  478. (*csrows)++;
  479. }
  480. }
  481. debugf0("Number of active channels on socket %d: %d\n",
  482. socket, *channels);
  483. return 0;
  484. }
  485. static int get_dimm_config(const struct mem_ctl_info *mci)
  486. {
  487. struct i7core_pvt *pvt = mci->pvt_info;
  488. struct csrow_info *csr;
  489. struct pci_dev *pdev;
  490. int i, j;
  491. int csrow = 0;
  492. unsigned long last_page = 0;
  493. enum edac_type mode;
  494. enum mem_type mtype;
  495. /* Get data from the MC register, function 0 */
  496. pdev = pvt->pci_mcr[0];
  497. if (!pdev)
  498. return -ENODEV;
  499. /* Device 3 function 0 reads */
  500. pci_read_config_dword(pdev, MC_CONTROL, &pvt->info.mc_control);
  501. pci_read_config_dword(pdev, MC_STATUS, &pvt->info.mc_status);
  502. pci_read_config_dword(pdev, MC_MAX_DOD, &pvt->info.max_dod);
  503. pci_read_config_dword(pdev, MC_CHANNEL_MAPPER, &pvt->info.ch_map);
  504. debugf0("QPI %d control=0x%08x status=0x%08x dod=0x%08x map=0x%08x\n",
  505. pvt->i7core_dev->socket, pvt->info.mc_control, pvt->info.mc_status,
  506. pvt->info.max_dod, pvt->info.ch_map);
  507. if (ECC_ENABLED(pvt)) {
  508. debugf0("ECC enabled with x%d SDCC\n", ECCx8(pvt) ? 8 : 4);
  509. if (ECCx8(pvt))
  510. mode = EDAC_S8ECD8ED;
  511. else
  512. mode = EDAC_S4ECD4ED;
  513. } else {
  514. debugf0("ECC disabled\n");
  515. mode = EDAC_NONE;
  516. }
  517. /* FIXME: need to handle the error codes */
  518. debugf0("DOD Max limits: DIMMS: %d, %d-ranked, %d-banked "
  519. "x%x x 0x%x\n",
  520. numdimms(pvt->info.max_dod),
  521. numrank(pvt->info.max_dod >> 2),
  522. numbank(pvt->info.max_dod >> 4),
  523. numrow(pvt->info.max_dod >> 6),
  524. numcol(pvt->info.max_dod >> 9));
  525. for (i = 0; i < NUM_CHANS; i++) {
  526. u32 data, dimm_dod[3], value[8];
  527. if (!pvt->pci_ch[i][0])
  528. continue;
  529. if (!CH_ACTIVE(pvt, i)) {
  530. debugf0("Channel %i is not active\n", i);
  531. continue;
  532. }
  533. if (CH_DISABLED(pvt, i)) {
  534. debugf0("Channel %i is disabled\n", i);
  535. continue;
  536. }
  537. /* Devices 4-6 function 0 */
  538. pci_read_config_dword(pvt->pci_ch[i][0],
  539. MC_CHANNEL_DIMM_INIT_PARAMS, &data);
  540. pvt->channel[i].ranks = (data & QUAD_RANK_PRESENT) ?
  541. 4 : 2;
  542. if (data & REGISTERED_DIMM)
  543. mtype = MEM_RDDR3;
  544. else
  545. mtype = MEM_DDR3;
  546. #if 0
  547. if (data & THREE_DIMMS_PRESENT)
  548. pvt->channel[i].dimms = 3;
  549. else if (data & SINGLE_QUAD_RANK_PRESENT)
  550. pvt->channel[i].dimms = 1;
  551. else
  552. pvt->channel[i].dimms = 2;
  553. #endif
  554. /* Devices 4-6 function 1 */
  555. pci_read_config_dword(pvt->pci_ch[i][1],
  556. MC_DOD_CH_DIMM0, &dimm_dod[0]);
  557. pci_read_config_dword(pvt->pci_ch[i][1],
  558. MC_DOD_CH_DIMM1, &dimm_dod[1]);
  559. pci_read_config_dword(pvt->pci_ch[i][1],
  560. MC_DOD_CH_DIMM2, &dimm_dod[2]);
  561. debugf0("Ch%d phy rd%d, wr%d (0x%08x): "
  562. "%d ranks, %cDIMMs\n",
  563. i,
  564. RDLCH(pvt->info.ch_map, i), WRLCH(pvt->info.ch_map, i),
  565. data,
  566. pvt->channel[i].ranks,
  567. (data & REGISTERED_DIMM) ? 'R' : 'U');
  568. for (j = 0; j < 3; j++) {
  569. u32 banks, ranks, rows, cols;
  570. u32 size, npages;
  571. if (!DIMM_PRESENT(dimm_dod[j]))
  572. continue;
  573. banks = numbank(MC_DOD_NUMBANK(dimm_dod[j]));
  574. ranks = numrank(MC_DOD_NUMRANK(dimm_dod[j]));
  575. rows = numrow(MC_DOD_NUMROW(dimm_dod[j]));
  576. cols = numcol(MC_DOD_NUMCOL(dimm_dod[j]));
  577. /* DDR3 has 8 I/O banks */
  578. size = (rows * cols * banks * ranks) >> (20 - 3);
  579. pvt->channel[i].dimms++;
  580. debugf0("\tdimm %d %d Mb offset: %x, "
  581. "bank: %d, rank: %d, row: %#x, col: %#x\n",
  582. j, size,
  583. RANKOFFSET(dimm_dod[j]),
  584. banks, ranks, rows, cols);
  585. npages = MiB_TO_PAGES(size);
  586. csr = &mci->csrows[csrow];
  587. csr->first_page = last_page + 1;
  588. last_page += npages;
  589. csr->last_page = last_page;
  590. csr->nr_pages = npages;
  591. csr->page_mask = 0;
  592. csr->grain = 8;
  593. csr->csrow_idx = csrow;
  594. csr->nr_channels = 1;
  595. csr->channels[0].chan_idx = i;
  596. csr->channels[0].ce_count = 0;
  597. pvt->csrow_map[i][j] = csrow;
  598. switch (banks) {
  599. case 4:
  600. csr->dtype = DEV_X4;
  601. break;
  602. case 8:
  603. csr->dtype = DEV_X8;
  604. break;
  605. case 16:
  606. csr->dtype = DEV_X16;
  607. break;
  608. default:
  609. csr->dtype = DEV_UNKNOWN;
  610. }
  611. csr->edac_mode = mode;
  612. csr->mtype = mtype;
  613. snprintf(csr->channels[0].label,
  614. sizeof(csr->channels[0].label),
  615. "CPU#%uChannel#%u_DIMM#%u",
  616. pvt->i7core_dev->socket, i, j);
  617. csrow++;
  618. }
  619. pci_read_config_dword(pdev, MC_SAG_CH_0, &value[0]);
  620. pci_read_config_dword(pdev, MC_SAG_CH_1, &value[1]);
  621. pci_read_config_dword(pdev, MC_SAG_CH_2, &value[2]);
  622. pci_read_config_dword(pdev, MC_SAG_CH_3, &value[3]);
  623. pci_read_config_dword(pdev, MC_SAG_CH_4, &value[4]);
  624. pci_read_config_dword(pdev, MC_SAG_CH_5, &value[5]);
  625. pci_read_config_dword(pdev, MC_SAG_CH_6, &value[6]);
  626. pci_read_config_dword(pdev, MC_SAG_CH_7, &value[7]);
  627. debugf1("\t[%i] DIVBY3\tREMOVED\tOFFSET\n", i);
  628. for (j = 0; j < 8; j++)
  629. debugf1("\t\t%#x\t%#x\t%#x\n",
  630. (value[j] >> 27) & 0x1,
  631. (value[j] >> 24) & 0x7,
  632. (value[j] & ((1 << 24) - 1)));
  633. }
  634. return 0;
  635. }
  636. /****************************************************************************
  637. Error insertion routines
  638. ****************************************************************************/
  639. /* The i7core has independent error injection features per channel.
  640. However, to have a simpler code, we don't allow enabling error injection
  641. on more than one channel.
  642. Also, since a change at an inject parameter will be applied only at enable,
  643. we're disabling error injection on all write calls to the sysfs nodes that
  644. controls the error code injection.
  645. */
  646. static int disable_inject(const struct mem_ctl_info *mci)
  647. {
  648. struct i7core_pvt *pvt = mci->pvt_info;
  649. pvt->inject.enable = 0;
  650. if (!pvt->pci_ch[pvt->inject.channel][0])
  651. return -ENODEV;
  652. pci_write_config_dword(pvt->pci_ch[pvt->inject.channel][0],
  653. MC_CHANNEL_ERROR_INJECT, 0);
  654. return 0;
  655. }
  656. /*
  657. * i7core inject inject.section
  658. *
  659. * accept and store error injection inject.section value
  660. * bit 0 - refers to the lower 32-byte half cacheline
  661. * bit 1 - refers to the upper 32-byte half cacheline
  662. */
  663. static ssize_t i7core_inject_section_store(struct mem_ctl_info *mci,
  664. const char *data, size_t count)
  665. {
  666. struct i7core_pvt *pvt = mci->pvt_info;
  667. unsigned long value;
  668. int rc;
  669. if (pvt->inject.enable)
  670. disable_inject(mci);
  671. rc = strict_strtoul(data, 10, &value);
  672. if ((rc < 0) || (value > 3))
  673. return -EIO;
  674. pvt->inject.section = (u32) value;
  675. return count;
  676. }
  677. static ssize_t i7core_inject_section_show(struct mem_ctl_info *mci,
  678. char *data)
  679. {
  680. struct i7core_pvt *pvt = mci->pvt_info;
  681. return sprintf(data, "0x%08x\n", pvt->inject.section);
  682. }
  683. /*
  684. * i7core inject.type
  685. *
  686. * accept and store error injection inject.section value
  687. * bit 0 - repeat enable - Enable error repetition
  688. * bit 1 - inject ECC error
  689. * bit 2 - inject parity error
  690. */
  691. static ssize_t i7core_inject_type_store(struct mem_ctl_info *mci,
  692. const char *data, size_t count)
  693. {
  694. struct i7core_pvt *pvt = mci->pvt_info;
  695. unsigned long value;
  696. int rc;
  697. if (pvt->inject.enable)
  698. disable_inject(mci);
  699. rc = strict_strtoul(data, 10, &value);
  700. if ((rc < 0) || (value > 7))
  701. return -EIO;
  702. pvt->inject.type = (u32) value;
  703. return count;
  704. }
  705. static ssize_t i7core_inject_type_show(struct mem_ctl_info *mci,
  706. char *data)
  707. {
  708. struct i7core_pvt *pvt = mci->pvt_info;
  709. return sprintf(data, "0x%08x\n", pvt->inject.type);
  710. }
  711. /*
  712. * i7core_inject_inject.eccmask_store
  713. *
  714. * The type of error (UE/CE) will depend on the inject.eccmask value:
  715. * Any bits set to a 1 will flip the corresponding ECC bit
  716. * Correctable errors can be injected by flipping 1 bit or the bits within
  717. * a symbol pair (2 consecutive aligned 8-bit pairs - i.e. 7:0 and 15:8 or
  718. * 23:16 and 31:24). Flipping bits in two symbol pairs will cause an
  719. * uncorrectable error to be injected.
  720. */
  721. static ssize_t i7core_inject_eccmask_store(struct mem_ctl_info *mci,
  722. const char *data, size_t count)
  723. {
  724. struct i7core_pvt *pvt = mci->pvt_info;
  725. unsigned long value;
  726. int rc;
  727. if (pvt->inject.enable)
  728. disable_inject(mci);
  729. rc = strict_strtoul(data, 10, &value);
  730. if (rc < 0)
  731. return -EIO;
  732. pvt->inject.eccmask = (u32) value;
  733. return count;
  734. }
  735. static ssize_t i7core_inject_eccmask_show(struct mem_ctl_info *mci,
  736. char *data)
  737. {
  738. struct i7core_pvt *pvt = mci->pvt_info;
  739. return sprintf(data, "0x%08x\n", pvt->inject.eccmask);
  740. }
  741. /*
  742. * i7core_addrmatch
  743. *
  744. * The type of error (UE/CE) will depend on the inject.eccmask value:
  745. * Any bits set to a 1 will flip the corresponding ECC bit
  746. * Correctable errors can be injected by flipping 1 bit or the bits within
  747. * a symbol pair (2 consecutive aligned 8-bit pairs - i.e. 7:0 and 15:8 or
  748. * 23:16 and 31:24). Flipping bits in two symbol pairs will cause an
  749. * uncorrectable error to be injected.
  750. */
  751. #define DECLARE_ADDR_MATCH(param, limit) \
  752. static ssize_t i7core_inject_store_##param( \
  753. struct mem_ctl_info *mci, \
  754. const char *data, size_t count) \
  755. { \
  756. struct i7core_pvt *pvt; \
  757. long value; \
  758. int rc; \
  759. \
  760. debugf1("%s()\n", __func__); \
  761. pvt = mci->pvt_info; \
  762. \
  763. if (pvt->inject.enable) \
  764. disable_inject(mci); \
  765. \
  766. if (!strcasecmp(data, "any") || !strcasecmp(data, "any\n"))\
  767. value = -1; \
  768. else { \
  769. rc = strict_strtoul(data, 10, &value); \
  770. if ((rc < 0) || (value >= limit)) \
  771. return -EIO; \
  772. } \
  773. \
  774. pvt->inject.param = value; \
  775. \
  776. return count; \
  777. } \
  778. \
  779. static ssize_t i7core_inject_show_##param( \
  780. struct mem_ctl_info *mci, \
  781. char *data) \
  782. { \
  783. struct i7core_pvt *pvt; \
  784. \
  785. pvt = mci->pvt_info; \
  786. debugf1("%s() pvt=%p\n", __func__, pvt); \
  787. if (pvt->inject.param < 0) \
  788. return sprintf(data, "any\n"); \
  789. else \
  790. return sprintf(data, "%d\n", pvt->inject.param);\
  791. }
  792. #define ATTR_ADDR_MATCH(param) \
  793. { \
  794. .attr = { \
  795. .name = #param, \
  796. .mode = (S_IRUGO | S_IWUSR) \
  797. }, \
  798. .show = i7core_inject_show_##param, \
  799. .store = i7core_inject_store_##param, \
  800. }
  801. DECLARE_ADDR_MATCH(channel, 3);
  802. DECLARE_ADDR_MATCH(dimm, 3);
  803. DECLARE_ADDR_MATCH(rank, 4);
  804. DECLARE_ADDR_MATCH(bank, 32);
  805. DECLARE_ADDR_MATCH(page, 0x10000);
  806. DECLARE_ADDR_MATCH(col, 0x4000);
  807. static int write_and_test(struct pci_dev *dev, const int where, const u32 val)
  808. {
  809. u32 read;
  810. int count;
  811. debugf0("setting pci %02x:%02x.%x reg=%02x value=%08x\n",
  812. dev->bus->number, PCI_SLOT(dev->devfn), PCI_FUNC(dev->devfn),
  813. where, val);
  814. for (count = 0; count < 10; count++) {
  815. if (count)
  816. msleep(100);
  817. pci_write_config_dword(dev, where, val);
  818. pci_read_config_dword(dev, where, &read);
  819. if (read == val)
  820. return 0;
  821. }
  822. i7core_printk(KERN_ERR, "Error during set pci %02x:%02x.%x reg=%02x "
  823. "write=%08x. Read=%08x\n",
  824. dev->bus->number, PCI_SLOT(dev->devfn), PCI_FUNC(dev->devfn),
  825. where, val, read);
  826. return -EINVAL;
  827. }
  828. /*
  829. * This routine prepares the Memory Controller for error injection.
  830. * The error will be injected when some process tries to write to the
  831. * memory that matches the given criteria.
  832. * The criteria can be set in terms of a mask where dimm, rank, bank, page
  833. * and col can be specified.
  834. * A -1 value for any of the mask items will make the MCU to ignore
  835. * that matching criteria for error injection.
  836. *
  837. * It should be noticed that the error will only happen after a write operation
  838. * on a memory that matches the condition. if REPEAT_EN is not enabled at
  839. * inject mask, then it will produce just one error. Otherwise, it will repeat
  840. * until the injectmask would be cleaned.
  841. *
  842. * FIXME: This routine assumes that MAXNUMDIMMS value of MC_MAX_DOD
  843. * is reliable enough to check if the MC is using the
  844. * three channels. However, this is not clear at the datasheet.
  845. */
  846. static ssize_t i7core_inject_enable_store(struct mem_ctl_info *mci,
  847. const char *data, size_t count)
  848. {
  849. struct i7core_pvt *pvt = mci->pvt_info;
  850. u32 injectmask;
  851. u64 mask = 0;
  852. int rc;
  853. long enable;
  854. if (!pvt->pci_ch[pvt->inject.channel][0])
  855. return 0;
  856. rc = strict_strtoul(data, 10, &enable);
  857. if ((rc < 0))
  858. return 0;
  859. if (enable) {
  860. pvt->inject.enable = 1;
  861. } else {
  862. disable_inject(mci);
  863. return count;
  864. }
  865. /* Sets pvt->inject.dimm mask */
  866. if (pvt->inject.dimm < 0)
  867. mask |= 1LL << 41;
  868. else {
  869. if (pvt->channel[pvt->inject.channel].dimms > 2)
  870. mask |= (pvt->inject.dimm & 0x3LL) << 35;
  871. else
  872. mask |= (pvt->inject.dimm & 0x1LL) << 36;
  873. }
  874. /* Sets pvt->inject.rank mask */
  875. if (pvt->inject.rank < 0)
  876. mask |= 1LL << 40;
  877. else {
  878. if (pvt->channel[pvt->inject.channel].dimms > 2)
  879. mask |= (pvt->inject.rank & 0x1LL) << 34;
  880. else
  881. mask |= (pvt->inject.rank & 0x3LL) << 34;
  882. }
  883. /* Sets pvt->inject.bank mask */
  884. if (pvt->inject.bank < 0)
  885. mask |= 1LL << 39;
  886. else
  887. mask |= (pvt->inject.bank & 0x15LL) << 30;
  888. /* Sets pvt->inject.page mask */
  889. if (pvt->inject.page < 0)
  890. mask |= 1LL << 38;
  891. else
  892. mask |= (pvt->inject.page & 0xffff) << 14;
  893. /* Sets pvt->inject.column mask */
  894. if (pvt->inject.col < 0)
  895. mask |= 1LL << 37;
  896. else
  897. mask |= (pvt->inject.col & 0x3fff);
  898. /*
  899. * bit 0: REPEAT_EN
  900. * bits 1-2: MASK_HALF_CACHELINE
  901. * bit 3: INJECT_ECC
  902. * bit 4: INJECT_ADDR_PARITY
  903. */
  904. injectmask = (pvt->inject.type & 1) |
  905. (pvt->inject.section & 0x3) << 1 |
  906. (pvt->inject.type & 0x6) << (3 - 1);
  907. /* Unlock writes to registers - this register is write only */
  908. pci_write_config_dword(pvt->pci_noncore,
  909. MC_CFG_CONTROL, 0x2);
  910. write_and_test(pvt->pci_ch[pvt->inject.channel][0],
  911. MC_CHANNEL_ADDR_MATCH, mask);
  912. write_and_test(pvt->pci_ch[pvt->inject.channel][0],
  913. MC_CHANNEL_ADDR_MATCH + 4, mask >> 32L);
  914. write_and_test(pvt->pci_ch[pvt->inject.channel][0],
  915. MC_CHANNEL_ERROR_MASK, pvt->inject.eccmask);
  916. write_and_test(pvt->pci_ch[pvt->inject.channel][0],
  917. MC_CHANNEL_ERROR_INJECT, injectmask);
  918. /*
  919. * This is something undocumented, based on my tests
  920. * Without writing 8 to this register, errors aren't injected. Not sure
  921. * why.
  922. */
  923. pci_write_config_dword(pvt->pci_noncore,
  924. MC_CFG_CONTROL, 8);
  925. debugf0("Error inject addr match 0x%016llx, ecc 0x%08x,"
  926. " inject 0x%08x\n",
  927. mask, pvt->inject.eccmask, injectmask);
  928. return count;
  929. }
  930. static ssize_t i7core_inject_enable_show(struct mem_ctl_info *mci,
  931. char *data)
  932. {
  933. struct i7core_pvt *pvt = mci->pvt_info;
  934. u32 injectmask;
  935. if (!pvt->pci_ch[pvt->inject.channel][0])
  936. return 0;
  937. pci_read_config_dword(pvt->pci_ch[pvt->inject.channel][0],
  938. MC_CHANNEL_ERROR_INJECT, &injectmask);
  939. debugf0("Inject error read: 0x%018x\n", injectmask);
  940. if (injectmask & 0x0c)
  941. pvt->inject.enable = 1;
  942. return sprintf(data, "%d\n", pvt->inject.enable);
  943. }
  944. #define DECLARE_COUNTER(param) \
  945. static ssize_t i7core_show_counter_##param( \
  946. struct mem_ctl_info *mci, \
  947. char *data) \
  948. { \
  949. struct i7core_pvt *pvt = mci->pvt_info; \
  950. \
  951. debugf1("%s() \n", __func__); \
  952. if (!pvt->ce_count_available || (pvt->is_registered)) \
  953. return sprintf(data, "data unavailable\n"); \
  954. return sprintf(data, "%lu\n", \
  955. pvt->udimm_ce_count[param]); \
  956. }
  957. #define ATTR_COUNTER(param) \
  958. { \
  959. .attr = { \
  960. .name = __stringify(udimm##param), \
  961. .mode = (S_IRUGO | S_IWUSR) \
  962. }, \
  963. .show = i7core_show_counter_##param \
  964. }
  965. DECLARE_COUNTER(0);
  966. DECLARE_COUNTER(1);
  967. DECLARE_COUNTER(2);
  968. /*
  969. * Sysfs struct
  970. */
  971. static const struct mcidev_sysfs_attribute i7core_addrmatch_attrs[] = {
  972. ATTR_ADDR_MATCH(channel),
  973. ATTR_ADDR_MATCH(dimm),
  974. ATTR_ADDR_MATCH(rank),
  975. ATTR_ADDR_MATCH(bank),
  976. ATTR_ADDR_MATCH(page),
  977. ATTR_ADDR_MATCH(col),
  978. { } /* End of list */
  979. };
  980. static const struct mcidev_sysfs_group i7core_inject_addrmatch = {
  981. .name = "inject_addrmatch",
  982. .mcidev_attr = i7core_addrmatch_attrs,
  983. };
  984. static const struct mcidev_sysfs_attribute i7core_udimm_counters_attrs[] = {
  985. ATTR_COUNTER(0),
  986. ATTR_COUNTER(1),
  987. ATTR_COUNTER(2),
  988. { .attr = { .name = NULL } }
  989. };
  990. static const struct mcidev_sysfs_group i7core_udimm_counters = {
  991. .name = "all_channel_counts",
  992. .mcidev_attr = i7core_udimm_counters_attrs,
  993. };
  994. static const struct mcidev_sysfs_attribute i7core_sysfs_rdimm_attrs[] = {
  995. {
  996. .attr = {
  997. .name = "inject_section",
  998. .mode = (S_IRUGO | S_IWUSR)
  999. },
  1000. .show = i7core_inject_section_show,
  1001. .store = i7core_inject_section_store,
  1002. }, {
  1003. .attr = {
  1004. .name = "inject_type",
  1005. .mode = (S_IRUGO | S_IWUSR)
  1006. },
  1007. .show = i7core_inject_type_show,
  1008. .store = i7core_inject_type_store,
  1009. }, {
  1010. .attr = {
  1011. .name = "inject_eccmask",
  1012. .mode = (S_IRUGO | S_IWUSR)
  1013. },
  1014. .show = i7core_inject_eccmask_show,
  1015. .store = i7core_inject_eccmask_store,
  1016. }, {
  1017. .grp = &i7core_inject_addrmatch,
  1018. }, {
  1019. .attr = {
  1020. .name = "inject_enable",
  1021. .mode = (S_IRUGO | S_IWUSR)
  1022. },
  1023. .show = i7core_inject_enable_show,
  1024. .store = i7core_inject_enable_store,
  1025. },
  1026. { } /* End of list */
  1027. };
  1028. static const struct mcidev_sysfs_attribute i7core_sysfs_udimm_attrs[] = {
  1029. {
  1030. .attr = {
  1031. .name = "inject_section",
  1032. .mode = (S_IRUGO | S_IWUSR)
  1033. },
  1034. .show = i7core_inject_section_show,
  1035. .store = i7core_inject_section_store,
  1036. }, {
  1037. .attr = {
  1038. .name = "inject_type",
  1039. .mode = (S_IRUGO | S_IWUSR)
  1040. },
  1041. .show = i7core_inject_type_show,
  1042. .store = i7core_inject_type_store,
  1043. }, {
  1044. .attr = {
  1045. .name = "inject_eccmask",
  1046. .mode = (S_IRUGO | S_IWUSR)
  1047. },
  1048. .show = i7core_inject_eccmask_show,
  1049. .store = i7core_inject_eccmask_store,
  1050. }, {
  1051. .grp = &i7core_inject_addrmatch,
  1052. }, {
  1053. .attr = {
  1054. .name = "inject_enable",
  1055. .mode = (S_IRUGO | S_IWUSR)
  1056. },
  1057. .show = i7core_inject_enable_show,
  1058. .store = i7core_inject_enable_store,
  1059. }, {
  1060. .grp = &i7core_udimm_counters,
  1061. },
  1062. { } /* End of list */
  1063. };
  1064. /****************************************************************************
  1065. Device initialization routines: put/get, init/exit
  1066. ****************************************************************************/
  1067. /*
  1068. * i7core_put_all_devices 'put' all the devices that we have
  1069. * reserved via 'get'
  1070. */
  1071. static void i7core_put_devices(struct i7core_dev *i7core_dev)
  1072. {
  1073. int i;
  1074. debugf0(__FILE__ ": %s()\n", __func__);
  1075. for (i = 0; i < i7core_dev->n_devs; i++) {
  1076. struct pci_dev *pdev = i7core_dev->pdev[i];
  1077. if (!pdev)
  1078. continue;
  1079. debugf0("Removing dev %02x:%02x.%d\n",
  1080. pdev->bus->number,
  1081. PCI_SLOT(pdev->devfn), PCI_FUNC(pdev->devfn));
  1082. pci_dev_put(pdev);
  1083. }
  1084. }
  1085. static void i7core_put_all_devices(void)
  1086. {
  1087. struct i7core_dev *i7core_dev, *tmp;
  1088. list_for_each_entry_safe(i7core_dev, tmp, &i7core_edac_list, list) {
  1089. i7core_put_devices(i7core_dev);
  1090. free_i7core_dev(i7core_dev);
  1091. }
  1092. }
  1093. static void __init i7core_xeon_pci_fixup(const struct pci_id_table *table)
  1094. {
  1095. struct pci_dev *pdev = NULL;
  1096. int i;
  1097. /*
  1098. * On Xeon 55xx, the Intel Quick Path Arch Generic Non-core pci buses
  1099. * aren't announced by acpi. So, we need to use a legacy scan probing
  1100. * to detect them
  1101. */
  1102. while (table && table->descr) {
  1103. pdev = pci_get_device(PCI_VENDOR_ID_INTEL, table->descr[0].dev_id, NULL);
  1104. if (unlikely(!pdev)) {
  1105. for (i = 0; i < MAX_SOCKET_BUSES; i++)
  1106. pcibios_scan_specific_bus(255-i);
  1107. }
  1108. pci_dev_put(pdev);
  1109. table++;
  1110. }
  1111. }
  1112. static unsigned i7core_pci_lastbus(void)
  1113. {
  1114. int last_bus = 0, bus;
  1115. struct pci_bus *b = NULL;
  1116. while ((b = pci_find_next_bus(b)) != NULL) {
  1117. bus = b->number;
  1118. debugf0("Found bus %d\n", bus);
  1119. if (bus > last_bus)
  1120. last_bus = bus;
  1121. }
  1122. debugf0("Last bus %d\n", last_bus);
  1123. return last_bus;
  1124. }
  1125. /*
  1126. * i7core_get_all_devices Find and perform 'get' operation on the MCH's
  1127. * device/functions we want to reference for this driver
  1128. *
  1129. * Need to 'get' device 16 func 1 and func 2
  1130. */
  1131. static int i7core_get_onedevice(struct pci_dev **prev,
  1132. const struct pci_id_table *table,
  1133. const unsigned devno,
  1134. const unsigned last_bus)
  1135. {
  1136. struct i7core_dev *i7core_dev;
  1137. const struct pci_id_descr *dev_descr = &table->descr[devno];
  1138. struct pci_dev *pdev = NULL;
  1139. u8 bus = 0;
  1140. u8 socket = 0;
  1141. pdev = pci_get_device(PCI_VENDOR_ID_INTEL,
  1142. dev_descr->dev_id, *prev);
  1143. /*
  1144. * On Xeon 55xx, the Intel Quckpath Arch Generic Non-core regs
  1145. * is at addr 8086:2c40, instead of 8086:2c41. So, we need
  1146. * to probe for the alternate address in case of failure
  1147. */
  1148. if (dev_descr->dev_id == PCI_DEVICE_ID_INTEL_I7_NONCORE && !pdev) {
  1149. pci_dev_get(*prev); /* pci_get_device will put it */
  1150. pdev = pci_get_device(PCI_VENDOR_ID_INTEL,
  1151. PCI_DEVICE_ID_INTEL_I7_NONCORE_ALT, *prev);
  1152. }
  1153. if (dev_descr->dev_id == PCI_DEVICE_ID_INTEL_LYNNFIELD_NONCORE &&
  1154. !pdev) {
  1155. pci_dev_get(*prev); /* pci_get_device will put it */
  1156. pdev = pci_get_device(PCI_VENDOR_ID_INTEL,
  1157. PCI_DEVICE_ID_INTEL_LYNNFIELD_NONCORE_ALT,
  1158. *prev);
  1159. }
  1160. if (!pdev) {
  1161. if (*prev) {
  1162. *prev = pdev;
  1163. return 0;
  1164. }
  1165. if (dev_descr->optional)
  1166. return 0;
  1167. if (devno == 0)
  1168. return -ENODEV;
  1169. i7core_printk(KERN_INFO,
  1170. "Device not found: dev %02x.%d PCI ID %04x:%04x\n",
  1171. dev_descr->dev, dev_descr->func,
  1172. PCI_VENDOR_ID_INTEL, dev_descr->dev_id);
  1173. /* End of list, leave */
  1174. return -ENODEV;
  1175. }
  1176. bus = pdev->bus->number;
  1177. socket = last_bus - bus;
  1178. i7core_dev = get_i7core_dev(socket);
  1179. if (!i7core_dev) {
  1180. i7core_dev = alloc_i7core_dev(socket, table);
  1181. if (!i7core_dev) {
  1182. pci_dev_put(pdev);
  1183. return -ENOMEM;
  1184. }
  1185. }
  1186. if (i7core_dev->pdev[devno]) {
  1187. i7core_printk(KERN_ERR,
  1188. "Duplicated device for "
  1189. "dev %02x:%02x.%d PCI ID %04x:%04x\n",
  1190. bus, dev_descr->dev, dev_descr->func,
  1191. PCI_VENDOR_ID_INTEL, dev_descr->dev_id);
  1192. pci_dev_put(pdev);
  1193. return -ENODEV;
  1194. }
  1195. i7core_dev->pdev[devno] = pdev;
  1196. /* Sanity check */
  1197. if (unlikely(PCI_SLOT(pdev->devfn) != dev_descr->dev ||
  1198. PCI_FUNC(pdev->devfn) != dev_descr->func)) {
  1199. i7core_printk(KERN_ERR,
  1200. "Device PCI ID %04x:%04x "
  1201. "has dev %02x:%02x.%d instead of dev %02x:%02x.%d\n",
  1202. PCI_VENDOR_ID_INTEL, dev_descr->dev_id,
  1203. bus, PCI_SLOT(pdev->devfn), PCI_FUNC(pdev->devfn),
  1204. bus, dev_descr->dev, dev_descr->func);
  1205. return -ENODEV;
  1206. }
  1207. /* Be sure that the device is enabled */
  1208. if (unlikely(pci_enable_device(pdev) < 0)) {
  1209. i7core_printk(KERN_ERR,
  1210. "Couldn't enable "
  1211. "dev %02x:%02x.%d PCI ID %04x:%04x\n",
  1212. bus, dev_descr->dev, dev_descr->func,
  1213. PCI_VENDOR_ID_INTEL, dev_descr->dev_id);
  1214. return -ENODEV;
  1215. }
  1216. debugf0("Detected socket %d dev %02x:%02x.%d PCI ID %04x:%04x\n",
  1217. socket, bus, dev_descr->dev,
  1218. dev_descr->func,
  1219. PCI_VENDOR_ID_INTEL, dev_descr->dev_id);
  1220. /*
  1221. * As stated on drivers/pci/search.c, the reference count for
  1222. * @from is always decremented if it is not %NULL. So, as we need
  1223. * to get all devices up to null, we need to do a get for the device
  1224. */
  1225. pci_dev_get(pdev);
  1226. *prev = pdev;
  1227. return 0;
  1228. }
  1229. static int i7core_get_all_devices(void)
  1230. {
  1231. int i, rc, last_bus;
  1232. struct pci_dev *pdev = NULL;
  1233. const struct pci_id_table *table = pci_dev_table;
  1234. last_bus = i7core_pci_lastbus();
  1235. while (table && table->descr) {
  1236. for (i = 0; i < table->n_devs; i++) {
  1237. pdev = NULL;
  1238. do {
  1239. rc = i7core_get_onedevice(&pdev, table, i,
  1240. last_bus);
  1241. if (rc < 0) {
  1242. if (i == 0) {
  1243. i = table->n_devs;
  1244. break;
  1245. }
  1246. i7core_put_all_devices();
  1247. return -ENODEV;
  1248. }
  1249. } while (pdev);
  1250. }
  1251. table++;
  1252. }
  1253. return 0;
  1254. }
  1255. static int mci_bind_devs(struct mem_ctl_info *mci,
  1256. struct i7core_dev *i7core_dev)
  1257. {
  1258. struct i7core_pvt *pvt = mci->pvt_info;
  1259. struct pci_dev *pdev;
  1260. int i, func, slot;
  1261. char *family;
  1262. pvt->is_registered = false;
  1263. pvt->enable_scrub = false;
  1264. for (i = 0; i < i7core_dev->n_devs; i++) {
  1265. pdev = i7core_dev->pdev[i];
  1266. if (!pdev)
  1267. continue;
  1268. func = PCI_FUNC(pdev->devfn);
  1269. slot = PCI_SLOT(pdev->devfn);
  1270. if (slot == 3) {
  1271. if (unlikely(func > MAX_MCR_FUNC))
  1272. goto error;
  1273. pvt->pci_mcr[func] = pdev;
  1274. } else if (likely(slot >= 4 && slot < 4 + NUM_CHANS)) {
  1275. if (unlikely(func > MAX_CHAN_FUNC))
  1276. goto error;
  1277. pvt->pci_ch[slot - 4][func] = pdev;
  1278. } else if (!slot && !func) {
  1279. pvt->pci_noncore = pdev;
  1280. /* Detect the processor family */
  1281. switch (pdev->device) {
  1282. case PCI_DEVICE_ID_INTEL_I7_NONCORE:
  1283. family = "Xeon 35xx/ i7core";
  1284. pvt->enable_scrub = false;
  1285. break;
  1286. case PCI_DEVICE_ID_INTEL_LYNNFIELD_NONCORE_ALT:
  1287. family = "i7-800/i5-700";
  1288. pvt->enable_scrub = false;
  1289. break;
  1290. case PCI_DEVICE_ID_INTEL_LYNNFIELD_NONCORE:
  1291. family = "Xeon 34xx";
  1292. pvt->enable_scrub = false;
  1293. break;
  1294. case PCI_DEVICE_ID_INTEL_I7_NONCORE_ALT:
  1295. family = "Xeon 55xx";
  1296. pvt->enable_scrub = true;
  1297. break;
  1298. case PCI_DEVICE_ID_INTEL_LYNNFIELD_NONCORE_REV2:
  1299. family = "Xeon 56xx / i7-900";
  1300. pvt->enable_scrub = true;
  1301. break;
  1302. default:
  1303. family = "unknown";
  1304. pvt->enable_scrub = false;
  1305. }
  1306. debugf0("Detected a processor type %s\n", family);
  1307. } else
  1308. goto error;
  1309. debugf0("Associated fn %d.%d, dev = %p, socket %d\n",
  1310. PCI_SLOT(pdev->devfn), PCI_FUNC(pdev->devfn),
  1311. pdev, i7core_dev->socket);
  1312. if (PCI_SLOT(pdev->devfn) == 3 &&
  1313. PCI_FUNC(pdev->devfn) == 2)
  1314. pvt->is_registered = true;
  1315. }
  1316. return 0;
  1317. error:
  1318. i7core_printk(KERN_ERR, "Device %d, function %d "
  1319. "is out of the expected range\n",
  1320. slot, func);
  1321. return -EINVAL;
  1322. }
  1323. /****************************************************************************
  1324. Error check routines
  1325. ****************************************************************************/
  1326. static void i7core_rdimm_update_csrow(struct mem_ctl_info *mci,
  1327. const int chan,
  1328. const int dimm,
  1329. const int add)
  1330. {
  1331. char *msg;
  1332. struct i7core_pvt *pvt = mci->pvt_info;
  1333. int row = pvt->csrow_map[chan][dimm], i;
  1334. for (i = 0; i < add; i++) {
  1335. msg = kasprintf(GFP_KERNEL, "Corrected error "
  1336. "(Socket=%d channel=%d dimm=%d)",
  1337. pvt->i7core_dev->socket, chan, dimm);
  1338. edac_mc_handle_fbd_ce(mci, row, 0, msg);
  1339. kfree (msg);
  1340. }
  1341. }
  1342. static void i7core_rdimm_update_ce_count(struct mem_ctl_info *mci,
  1343. const int chan,
  1344. const int new0,
  1345. const int new1,
  1346. const int new2)
  1347. {
  1348. struct i7core_pvt *pvt = mci->pvt_info;
  1349. int add0 = 0, add1 = 0, add2 = 0;
  1350. /* Updates CE counters if it is not the first time here */
  1351. if (pvt->ce_count_available) {
  1352. /* Updates CE counters */
  1353. add2 = new2 - pvt->rdimm_last_ce_count[chan][2];
  1354. add1 = new1 - pvt->rdimm_last_ce_count[chan][1];
  1355. add0 = new0 - pvt->rdimm_last_ce_count[chan][0];
  1356. if (add2 < 0)
  1357. add2 += 0x7fff;
  1358. pvt->rdimm_ce_count[chan][2] += add2;
  1359. if (add1 < 0)
  1360. add1 += 0x7fff;
  1361. pvt->rdimm_ce_count[chan][1] += add1;
  1362. if (add0 < 0)
  1363. add0 += 0x7fff;
  1364. pvt->rdimm_ce_count[chan][0] += add0;
  1365. } else
  1366. pvt->ce_count_available = 1;
  1367. /* Store the new values */
  1368. pvt->rdimm_last_ce_count[chan][2] = new2;
  1369. pvt->rdimm_last_ce_count[chan][1] = new1;
  1370. pvt->rdimm_last_ce_count[chan][0] = new0;
  1371. /*updated the edac core */
  1372. if (add0 != 0)
  1373. i7core_rdimm_update_csrow(mci, chan, 0, add0);
  1374. if (add1 != 0)
  1375. i7core_rdimm_update_csrow(mci, chan, 1, add1);
  1376. if (add2 != 0)
  1377. i7core_rdimm_update_csrow(mci, chan, 2, add2);
  1378. }
  1379. static void i7core_rdimm_check_mc_ecc_err(struct mem_ctl_info *mci)
  1380. {
  1381. struct i7core_pvt *pvt = mci->pvt_info;
  1382. u32 rcv[3][2];
  1383. int i, new0, new1, new2;
  1384. /*Read DEV 3: FUN 2: MC_COR_ECC_CNT regs directly*/
  1385. pci_read_config_dword(pvt->pci_mcr[2], MC_COR_ECC_CNT_0,
  1386. &rcv[0][0]);
  1387. pci_read_config_dword(pvt->pci_mcr[2], MC_COR_ECC_CNT_1,
  1388. &rcv[0][1]);
  1389. pci_read_config_dword(pvt->pci_mcr[2], MC_COR_ECC_CNT_2,
  1390. &rcv[1][0]);
  1391. pci_read_config_dword(pvt->pci_mcr[2], MC_COR_ECC_CNT_3,
  1392. &rcv[1][1]);
  1393. pci_read_config_dword(pvt->pci_mcr[2], MC_COR_ECC_CNT_4,
  1394. &rcv[2][0]);
  1395. pci_read_config_dword(pvt->pci_mcr[2], MC_COR_ECC_CNT_5,
  1396. &rcv[2][1]);
  1397. for (i = 0 ; i < 3; i++) {
  1398. debugf3("MC_COR_ECC_CNT%d = 0x%x; MC_COR_ECC_CNT%d = 0x%x\n",
  1399. (i * 2), rcv[i][0], (i * 2) + 1, rcv[i][1]);
  1400. /*if the channel has 3 dimms*/
  1401. if (pvt->channel[i].dimms > 2) {
  1402. new0 = DIMM_BOT_COR_ERR(rcv[i][0]);
  1403. new1 = DIMM_TOP_COR_ERR(rcv[i][0]);
  1404. new2 = DIMM_BOT_COR_ERR(rcv[i][1]);
  1405. } else {
  1406. new0 = DIMM_TOP_COR_ERR(rcv[i][0]) +
  1407. DIMM_BOT_COR_ERR(rcv[i][0]);
  1408. new1 = DIMM_TOP_COR_ERR(rcv[i][1]) +
  1409. DIMM_BOT_COR_ERR(rcv[i][1]);
  1410. new2 = 0;
  1411. }
  1412. i7core_rdimm_update_ce_count(mci, i, new0, new1, new2);
  1413. }
  1414. }
  1415. /* This function is based on the device 3 function 4 registers as described on:
  1416. * Intel Xeon Processor 5500 Series Datasheet Volume 2
  1417. * http://www.intel.com/Assets/PDF/datasheet/321322.pdf
  1418. * also available at:
  1419. * http://www.arrownac.com/manufacturers/intel/s/nehalem/5500-datasheet-v2.pdf
  1420. */
  1421. static void i7core_udimm_check_mc_ecc_err(struct mem_ctl_info *mci)
  1422. {
  1423. struct i7core_pvt *pvt = mci->pvt_info;
  1424. u32 rcv1, rcv0;
  1425. int new0, new1, new2;
  1426. if (!pvt->pci_mcr[4]) {
  1427. debugf0("%s MCR registers not found\n", __func__);
  1428. return;
  1429. }
  1430. /* Corrected test errors */
  1431. pci_read_config_dword(pvt->pci_mcr[4], MC_TEST_ERR_RCV1, &rcv1);
  1432. pci_read_config_dword(pvt->pci_mcr[4], MC_TEST_ERR_RCV0, &rcv0);
  1433. /* Store the new values */
  1434. new2 = DIMM2_COR_ERR(rcv1);
  1435. new1 = DIMM1_COR_ERR(rcv0);
  1436. new0 = DIMM0_COR_ERR(rcv0);
  1437. /* Updates CE counters if it is not the first time here */
  1438. if (pvt->ce_count_available) {
  1439. /* Updates CE counters */
  1440. int add0, add1, add2;
  1441. add2 = new2 - pvt->udimm_last_ce_count[2];
  1442. add1 = new1 - pvt->udimm_last_ce_count[1];
  1443. add0 = new0 - pvt->udimm_last_ce_count[0];
  1444. if (add2 < 0)
  1445. add2 += 0x7fff;
  1446. pvt->udimm_ce_count[2] += add2;
  1447. if (add1 < 0)
  1448. add1 += 0x7fff;
  1449. pvt->udimm_ce_count[1] += add1;
  1450. if (add0 < 0)
  1451. add0 += 0x7fff;
  1452. pvt->udimm_ce_count[0] += add0;
  1453. if (add0 | add1 | add2)
  1454. i7core_printk(KERN_ERR, "New Corrected error(s): "
  1455. "dimm0: +%d, dimm1: +%d, dimm2 +%d\n",
  1456. add0, add1, add2);
  1457. } else
  1458. pvt->ce_count_available = 1;
  1459. /* Store the new values */
  1460. pvt->udimm_last_ce_count[2] = new2;
  1461. pvt->udimm_last_ce_count[1] = new1;
  1462. pvt->udimm_last_ce_count[0] = new0;
  1463. }
  1464. /*
  1465. * According with tables E-11 and E-12 of chapter E.3.3 of Intel 64 and IA-32
  1466. * Architectures Software Developer’s Manual Volume 3B.
  1467. * Nehalem are defined as family 0x06, model 0x1a
  1468. *
  1469. * The MCA registers used here are the following ones:
  1470. * struct mce field MCA Register
  1471. * m->status MSR_IA32_MC8_STATUS
  1472. * m->addr MSR_IA32_MC8_ADDR
  1473. * m->misc MSR_IA32_MC8_MISC
  1474. * In the case of Nehalem, the error information is masked at .status and .misc
  1475. * fields
  1476. */
  1477. static void i7core_mce_output_error(struct mem_ctl_info *mci,
  1478. const struct mce *m)
  1479. {
  1480. struct i7core_pvt *pvt = mci->pvt_info;
  1481. char *type, *optype, *err, *msg;
  1482. unsigned long error = m->status & 0x1ff0000l;
  1483. u32 optypenum = (m->status >> 4) & 0x07;
  1484. u32 core_err_cnt = (m->status >> 38) & 0x7fff;
  1485. u32 dimm = (m->misc >> 16) & 0x3;
  1486. u32 channel = (m->misc >> 18) & 0x3;
  1487. u32 syndrome = m->misc >> 32;
  1488. u32 errnum = find_first_bit(&error, 32);
  1489. int csrow;
  1490. if (m->mcgstatus & 1)
  1491. type = "FATAL";
  1492. else
  1493. type = "NON_FATAL";
  1494. switch (optypenum) {
  1495. case 0:
  1496. optype = "generic undef request";
  1497. break;
  1498. case 1:
  1499. optype = "read error";
  1500. break;
  1501. case 2:
  1502. optype = "write error";
  1503. break;
  1504. case 3:
  1505. optype = "addr/cmd error";
  1506. break;
  1507. case 4:
  1508. optype = "scrubbing error";
  1509. break;
  1510. default:
  1511. optype = "reserved";
  1512. break;
  1513. }
  1514. switch (errnum) {
  1515. case 16:
  1516. err = "read ECC error";
  1517. break;
  1518. case 17:
  1519. err = "RAS ECC error";
  1520. break;
  1521. case 18:
  1522. err = "write parity error";
  1523. break;
  1524. case 19:
  1525. err = "redundacy loss";
  1526. break;
  1527. case 20:
  1528. err = "reserved";
  1529. break;
  1530. case 21:
  1531. err = "memory range error";
  1532. break;
  1533. case 22:
  1534. err = "RTID out of range";
  1535. break;
  1536. case 23:
  1537. err = "address parity error";
  1538. break;
  1539. case 24:
  1540. err = "byte enable parity error";
  1541. break;
  1542. default:
  1543. err = "unknown";
  1544. }
  1545. /* FIXME: should convert addr into bank and rank information */
  1546. msg = kasprintf(GFP_ATOMIC,
  1547. "%s (addr = 0x%08llx, cpu=%d, Dimm=%d, Channel=%d, "
  1548. "syndrome=0x%08x, count=%d, Err=%08llx:%08llx (%s: %s))\n",
  1549. type, (long long) m->addr, m->cpu, dimm, channel,
  1550. syndrome, core_err_cnt, (long long)m->status,
  1551. (long long)m->misc, optype, err);
  1552. debugf0("%s", msg);
  1553. csrow = pvt->csrow_map[channel][dimm];
  1554. /* Call the helper to output message */
  1555. if (m->mcgstatus & 1)
  1556. edac_mc_handle_fbd_ue(mci, csrow, 0,
  1557. 0 /* FIXME: should be channel here */, msg);
  1558. else if (!pvt->is_registered)
  1559. edac_mc_handle_fbd_ce(mci, csrow,
  1560. 0 /* FIXME: should be channel here */, msg);
  1561. kfree(msg);
  1562. }
  1563. /*
  1564. * i7core_check_error Retrieve and process errors reported by the
  1565. * hardware. Called by the Core module.
  1566. */
  1567. static void i7core_check_error(struct mem_ctl_info *mci)
  1568. {
  1569. struct i7core_pvt *pvt = mci->pvt_info;
  1570. int i;
  1571. unsigned count = 0;
  1572. struct mce *m;
  1573. /*
  1574. * MCE first step: Copy all mce errors into a temporary buffer
  1575. * We use a double buffering here, to reduce the risk of
  1576. * losing an error.
  1577. */
  1578. smp_rmb();
  1579. count = (pvt->mce_out + MCE_LOG_LEN - pvt->mce_in)
  1580. % MCE_LOG_LEN;
  1581. if (!count)
  1582. goto check_ce_error;
  1583. m = pvt->mce_outentry;
  1584. if (pvt->mce_in + count > MCE_LOG_LEN) {
  1585. unsigned l = MCE_LOG_LEN - pvt->mce_in;
  1586. memcpy(m, &pvt->mce_entry[pvt->mce_in], sizeof(*m) * l);
  1587. smp_wmb();
  1588. pvt->mce_in = 0;
  1589. count -= l;
  1590. m += l;
  1591. }
  1592. memcpy(m, &pvt->mce_entry[pvt->mce_in], sizeof(*m) * count);
  1593. smp_wmb();
  1594. pvt->mce_in += count;
  1595. smp_rmb();
  1596. if (pvt->mce_overrun) {
  1597. i7core_printk(KERN_ERR, "Lost %d memory errors\n",
  1598. pvt->mce_overrun);
  1599. smp_wmb();
  1600. pvt->mce_overrun = 0;
  1601. }
  1602. /*
  1603. * MCE second step: parse errors and display
  1604. */
  1605. for (i = 0; i < count; i++)
  1606. i7core_mce_output_error(mci, &pvt->mce_outentry[i]);
  1607. /*
  1608. * Now, let's increment CE error counts
  1609. */
  1610. check_ce_error:
  1611. if (!pvt->is_registered)
  1612. i7core_udimm_check_mc_ecc_err(mci);
  1613. else
  1614. i7core_rdimm_check_mc_ecc_err(mci);
  1615. }
  1616. /*
  1617. * i7core_mce_check_error Replicates mcelog routine to get errors
  1618. * This routine simply queues mcelog errors, and
  1619. * return. The error itself should be handled later
  1620. * by i7core_check_error.
  1621. * WARNING: As this routine should be called at NMI time, extra care should
  1622. * be taken to avoid deadlocks, and to be as fast as possible.
  1623. */
  1624. static int i7core_mce_check_error(struct notifier_block *nb, unsigned long val,
  1625. void *data)
  1626. {
  1627. struct mce *mce = (struct mce *)data;
  1628. struct i7core_dev *i7_dev;
  1629. struct mem_ctl_info *mci;
  1630. struct i7core_pvt *pvt;
  1631. i7_dev = get_i7core_dev(mce->socketid);
  1632. if (!i7_dev)
  1633. return NOTIFY_BAD;
  1634. mci = i7_dev->mci;
  1635. pvt = mci->pvt_info;
  1636. /*
  1637. * Just let mcelog handle it if the error is
  1638. * outside the memory controller
  1639. */
  1640. if (((mce->status & 0xffff) >> 7) != 1)
  1641. return NOTIFY_DONE;
  1642. /* Bank 8 registers are the only ones that we know how to handle */
  1643. if (mce->bank != 8)
  1644. return NOTIFY_DONE;
  1645. smp_rmb();
  1646. if ((pvt->mce_out + 1) % MCE_LOG_LEN == pvt->mce_in) {
  1647. smp_wmb();
  1648. pvt->mce_overrun++;
  1649. return NOTIFY_DONE;
  1650. }
  1651. /* Copy memory error at the ringbuffer */
  1652. memcpy(&pvt->mce_entry[pvt->mce_out], mce, sizeof(*mce));
  1653. smp_wmb();
  1654. pvt->mce_out = (pvt->mce_out + 1) % MCE_LOG_LEN;
  1655. /* Handle fatal errors immediately */
  1656. if (mce->mcgstatus & 1)
  1657. i7core_check_error(mci);
  1658. /* Advise mcelog that the errors were handled */
  1659. return NOTIFY_STOP;
  1660. }
  1661. static struct notifier_block i7_mce_dec = {
  1662. .notifier_call = i7core_mce_check_error,
  1663. };
  1664. struct memdev_dmi_entry {
  1665. u8 type;
  1666. u8 length;
  1667. u16 handle;
  1668. u16 phys_mem_array_handle;
  1669. u16 mem_err_info_handle;
  1670. u16 total_width;
  1671. u16 data_width;
  1672. u16 size;
  1673. u8 form;
  1674. u8 device_set;
  1675. u8 device_locator;
  1676. u8 bank_locator;
  1677. u8 memory_type;
  1678. u16 type_detail;
  1679. u16 speed;
  1680. u8 manufacturer;
  1681. u8 serial_number;
  1682. u8 asset_tag;
  1683. u8 part_number;
  1684. u8 attributes;
  1685. u32 extended_size;
  1686. u16 conf_mem_clk_speed;
  1687. } __attribute__((__packed__));
  1688. /*
  1689. * Decode the DRAM Clock Frequency, be paranoid, make sure that all
  1690. * memory devices show the same speed, and if they don't then consider
  1691. * all speeds to be invalid.
  1692. */
  1693. static void decode_dclk(const struct dmi_header *dh, void *_dclk_freq)
  1694. {
  1695. int *dclk_freq = _dclk_freq;
  1696. u16 dmi_mem_clk_speed;
  1697. if (*dclk_freq == -1)
  1698. return;
  1699. if (dh->type == DMI_ENTRY_MEM_DEVICE) {
  1700. struct memdev_dmi_entry *memdev_dmi_entry =
  1701. (struct memdev_dmi_entry *)dh;
  1702. unsigned long conf_mem_clk_speed_offset =
  1703. (unsigned long)&memdev_dmi_entry->conf_mem_clk_speed -
  1704. (unsigned long)&memdev_dmi_entry->type;
  1705. unsigned long speed_offset =
  1706. (unsigned long)&memdev_dmi_entry->speed -
  1707. (unsigned long)&memdev_dmi_entry->type;
  1708. /* Check that a DIMM is present */
  1709. if (memdev_dmi_entry->size == 0)
  1710. return;
  1711. /*
  1712. * Pick the configured speed if it's available, otherwise
  1713. * pick the DIMM speed, or we don't have a speed.
  1714. */
  1715. if (memdev_dmi_entry->length > conf_mem_clk_speed_offset) {
  1716. dmi_mem_clk_speed =
  1717. memdev_dmi_entry->conf_mem_clk_speed;
  1718. } else if (memdev_dmi_entry->length > speed_offset) {
  1719. dmi_mem_clk_speed = memdev_dmi_entry->speed;
  1720. } else {
  1721. *dclk_freq = -1;
  1722. return;
  1723. }
  1724. if (*dclk_freq == 0) {
  1725. /* First pass, speed was 0 */
  1726. if (dmi_mem_clk_speed > 0) {
  1727. /* Set speed if a valid speed is read */
  1728. *dclk_freq = dmi_mem_clk_speed;
  1729. } else {
  1730. /* Otherwise we don't have a valid speed */
  1731. *dclk_freq = -1;
  1732. }
  1733. } else if (*dclk_freq > 0 &&
  1734. *dclk_freq != dmi_mem_clk_speed) {
  1735. /*
  1736. * If we have a speed, check that all DIMMS are the same
  1737. * speed, otherwise set the speed as invalid.
  1738. */
  1739. *dclk_freq = -1;
  1740. }
  1741. }
  1742. }
  1743. /*
  1744. * The default DCLK frequency is used as a fallback if we
  1745. * fail to find anything reliable in the DMI. The value
  1746. * is taken straight from the datasheet.
  1747. */
  1748. #define DEFAULT_DCLK_FREQ 800
  1749. static int get_dclk_freq(void)
  1750. {
  1751. int dclk_freq = 0;
  1752. dmi_walk(decode_dclk, (void *)&dclk_freq);
  1753. if (dclk_freq < 1)
  1754. return DEFAULT_DCLK_FREQ;
  1755. return dclk_freq;
  1756. }
  1757. /*
  1758. * set_sdram_scrub_rate This routine sets byte/sec bandwidth scrub rate
  1759. * to hardware according to SCRUBINTERVAL formula
  1760. * found in datasheet.
  1761. */
  1762. static int set_sdram_scrub_rate(struct mem_ctl_info *mci, u32 new_bw)
  1763. {
  1764. struct i7core_pvt *pvt = mci->pvt_info;
  1765. struct pci_dev *pdev;
  1766. u32 dw_scrub;
  1767. u32 dw_ssr;
  1768. /* Get data from the MC register, function 2 */
  1769. pdev = pvt->pci_mcr[2];
  1770. if (!pdev)
  1771. return -ENODEV;
  1772. pci_read_config_dword(pdev, MC_SCRUB_CONTROL, &dw_scrub);
  1773. if (new_bw == 0) {
  1774. /* Prepare to disable petrol scrub */
  1775. dw_scrub &= ~STARTSCRUB;
  1776. /* Stop the patrol scrub engine */
  1777. write_and_test(pdev, MC_SCRUB_CONTROL,
  1778. dw_scrub & ~SCRUBINTERVAL_MASK);
  1779. /* Get current status of scrub rate and set bit to disable */
  1780. pci_read_config_dword(pdev, MC_SSRCONTROL, &dw_ssr);
  1781. dw_ssr &= ~SSR_MODE_MASK;
  1782. dw_ssr |= SSR_MODE_DISABLE;
  1783. } else {
  1784. const int cache_line_size = 64;
  1785. const u32 freq_dclk_mhz = pvt->dclk_freq;
  1786. unsigned long long scrub_interval;
  1787. /*
  1788. * Translate the desired scrub rate to a register value and
  1789. * program the corresponding register value.
  1790. */
  1791. scrub_interval = (unsigned long long)freq_dclk_mhz *
  1792. cache_line_size * 1000000;
  1793. do_div(scrub_interval, new_bw);
  1794. if (!scrub_interval || scrub_interval > SCRUBINTERVAL_MASK)
  1795. return -EINVAL;
  1796. dw_scrub = SCRUBINTERVAL_MASK & scrub_interval;
  1797. /* Start the patrol scrub engine */
  1798. pci_write_config_dword(pdev, MC_SCRUB_CONTROL,
  1799. STARTSCRUB | dw_scrub);
  1800. /* Get current status of scrub rate and set bit to enable */
  1801. pci_read_config_dword(pdev, MC_SSRCONTROL, &dw_ssr);
  1802. dw_ssr &= ~SSR_MODE_MASK;
  1803. dw_ssr |= SSR_MODE_ENABLE;
  1804. }
  1805. /* Disable or enable scrubbing */
  1806. pci_write_config_dword(pdev, MC_SSRCONTROL, dw_ssr);
  1807. return new_bw;
  1808. }
  1809. /*
  1810. * get_sdram_scrub_rate This routine convert current scrub rate value
  1811. * into byte/sec bandwidth accourding to
  1812. * SCRUBINTERVAL formula found in datasheet.
  1813. */
  1814. static int get_sdram_scrub_rate(struct mem_ctl_info *mci)
  1815. {
  1816. struct i7core_pvt *pvt = mci->pvt_info;
  1817. struct pci_dev *pdev;
  1818. const u32 cache_line_size = 64;
  1819. const u32 freq_dclk_mhz = pvt->dclk_freq;
  1820. unsigned long long scrub_rate;
  1821. u32 scrubval;
  1822. /* Get data from the MC register, function 2 */
  1823. pdev = pvt->pci_mcr[2];
  1824. if (!pdev)
  1825. return -ENODEV;
  1826. /* Get current scrub control data */
  1827. pci_read_config_dword(pdev, MC_SCRUB_CONTROL, &scrubval);
  1828. /* Mask highest 8-bits to 0 */
  1829. scrubval &= SCRUBINTERVAL_MASK;
  1830. if (!scrubval)
  1831. return 0;
  1832. /* Calculate scrub rate value into byte/sec bandwidth */
  1833. scrub_rate = (unsigned long long)freq_dclk_mhz *
  1834. 1000000 * cache_line_size;
  1835. do_div(scrub_rate, scrubval);
  1836. return (int)scrub_rate;
  1837. }
  1838. static void enable_sdram_scrub_setting(struct mem_ctl_info *mci)
  1839. {
  1840. struct i7core_pvt *pvt = mci->pvt_info;
  1841. u32 pci_lock;
  1842. /* Unlock writes to pci registers */
  1843. pci_read_config_dword(pvt->pci_noncore, MC_CFG_CONTROL, &pci_lock);
  1844. pci_lock &= ~0x3;
  1845. pci_write_config_dword(pvt->pci_noncore, MC_CFG_CONTROL,
  1846. pci_lock | MC_CFG_UNLOCK);
  1847. mci->set_sdram_scrub_rate = set_sdram_scrub_rate;
  1848. mci->get_sdram_scrub_rate = get_sdram_scrub_rate;
  1849. }
  1850. static void disable_sdram_scrub_setting(struct mem_ctl_info *mci)
  1851. {
  1852. struct i7core_pvt *pvt = mci->pvt_info;
  1853. u32 pci_lock;
  1854. /* Lock writes to pci registers */
  1855. pci_read_config_dword(pvt->pci_noncore, MC_CFG_CONTROL, &pci_lock);
  1856. pci_lock &= ~0x3;
  1857. pci_write_config_dword(pvt->pci_noncore, MC_CFG_CONTROL,
  1858. pci_lock | MC_CFG_LOCK);
  1859. }
  1860. static void i7core_pci_ctl_create(struct i7core_pvt *pvt)
  1861. {
  1862. pvt->i7core_pci = edac_pci_create_generic_ctl(
  1863. &pvt->i7core_dev->pdev[0]->dev,
  1864. EDAC_MOD_STR);
  1865. if (unlikely(!pvt->i7core_pci))
  1866. i7core_printk(KERN_WARNING,
  1867. "Unable to setup PCI error report via EDAC\n");
  1868. }
  1869. static void i7core_pci_ctl_release(struct i7core_pvt *pvt)
  1870. {
  1871. if (likely(pvt->i7core_pci))
  1872. edac_pci_release_generic_ctl(pvt->i7core_pci);
  1873. else
  1874. i7core_printk(KERN_ERR,
  1875. "Couldn't find mem_ctl_info for socket %d\n",
  1876. pvt->i7core_dev->socket);
  1877. pvt->i7core_pci = NULL;
  1878. }
  1879. static void i7core_unregister_mci(struct i7core_dev *i7core_dev)
  1880. {
  1881. struct mem_ctl_info *mci = i7core_dev->mci;
  1882. struct i7core_pvt *pvt;
  1883. if (unlikely(!mci || !mci->pvt_info)) {
  1884. debugf0("MC: " __FILE__ ": %s(): dev = %p\n",
  1885. __func__, &i7core_dev->pdev[0]->dev);
  1886. i7core_printk(KERN_ERR, "Couldn't find mci handler\n");
  1887. return;
  1888. }
  1889. pvt = mci->pvt_info;
  1890. debugf0("MC: " __FILE__ ": %s(): mci = %p, dev = %p\n",
  1891. __func__, mci, &i7core_dev->pdev[0]->dev);
  1892. /* Disable scrubrate setting */
  1893. if (pvt->enable_scrub)
  1894. disable_sdram_scrub_setting(mci);
  1895. /* Disable EDAC polling */
  1896. i7core_pci_ctl_release(pvt);
  1897. /* Remove MC sysfs nodes */
  1898. edac_mc_del_mc(mci->dev);
  1899. debugf1("%s: free mci struct\n", mci->ctl_name);
  1900. kfree(mci->ctl_name);
  1901. edac_mc_free(mci);
  1902. i7core_dev->mci = NULL;
  1903. }
  1904. static int i7core_register_mci(struct i7core_dev *i7core_dev)
  1905. {
  1906. struct mem_ctl_info *mci;
  1907. struct i7core_pvt *pvt;
  1908. int rc, channels, csrows;
  1909. /* Check the number of active and not disabled channels */
  1910. rc = i7core_get_active_channels(i7core_dev->socket, &channels, &csrows);
  1911. if (unlikely(rc < 0))
  1912. return rc;
  1913. /* allocate a new MC control structure */
  1914. mci = edac_mc_alloc(sizeof(*pvt), csrows, channels, i7core_dev->socket);
  1915. if (unlikely(!mci))
  1916. return -ENOMEM;
  1917. debugf0("MC: " __FILE__ ": %s(): mci = %p, dev = %p\n",
  1918. __func__, mci, &i7core_dev->pdev[0]->dev);
  1919. pvt = mci->pvt_info;
  1920. memset(pvt, 0, sizeof(*pvt));
  1921. /* Associates i7core_dev and mci for future usage */
  1922. pvt->i7core_dev = i7core_dev;
  1923. i7core_dev->mci = mci;
  1924. /*
  1925. * FIXME: how to handle RDDR3 at MCI level? It is possible to have
  1926. * Mixed RDDR3/UDDR3 with Nehalem, provided that they are on different
  1927. * memory channels
  1928. */
  1929. mci->mtype_cap = MEM_FLAG_DDR3;
  1930. mci->edac_ctl_cap = EDAC_FLAG_NONE;
  1931. mci->edac_cap = EDAC_FLAG_NONE;
  1932. mci->mod_name = "i7core_edac.c";
  1933. mci->mod_ver = I7CORE_REVISION;
  1934. mci->ctl_name = kasprintf(GFP_KERNEL, "i7 core #%d",
  1935. i7core_dev->socket);
  1936. mci->dev_name = pci_name(i7core_dev->pdev[0]);
  1937. mci->ctl_page_to_phys = NULL;
  1938. /* Store pci devices at mci for faster access */
  1939. rc = mci_bind_devs(mci, i7core_dev);
  1940. if (unlikely(rc < 0))
  1941. goto fail0;
  1942. if (pvt->is_registered)
  1943. mci->mc_driver_sysfs_attributes = i7core_sysfs_rdimm_attrs;
  1944. else
  1945. mci->mc_driver_sysfs_attributes = i7core_sysfs_udimm_attrs;
  1946. /* Get dimm basic config */
  1947. get_dimm_config(mci);
  1948. /* record ptr to the generic device */
  1949. mci->dev = &i7core_dev->pdev[0]->dev;
  1950. /* Set the function pointer to an actual operation function */
  1951. mci->edac_check = i7core_check_error;
  1952. /* Enable scrubrate setting */
  1953. if (pvt->enable_scrub)
  1954. enable_sdram_scrub_setting(mci);
  1955. /* add this new MC control structure to EDAC's list of MCs */
  1956. if (unlikely(edac_mc_add_mc(mci))) {
  1957. debugf0("MC: " __FILE__
  1958. ": %s(): failed edac_mc_add_mc()\n", __func__);
  1959. /* FIXME: perhaps some code should go here that disables error
  1960. * reporting if we just enabled it
  1961. */
  1962. rc = -EINVAL;
  1963. goto fail0;
  1964. }
  1965. /* Default error mask is any memory */
  1966. pvt->inject.channel = 0;
  1967. pvt->inject.dimm = -1;
  1968. pvt->inject.rank = -1;
  1969. pvt->inject.bank = -1;
  1970. pvt->inject.page = -1;
  1971. pvt->inject.col = -1;
  1972. /* allocating generic PCI control info */
  1973. i7core_pci_ctl_create(pvt);
  1974. /* DCLK for scrub rate setting */
  1975. pvt->dclk_freq = get_dclk_freq();
  1976. return 0;
  1977. fail0:
  1978. kfree(mci->ctl_name);
  1979. edac_mc_free(mci);
  1980. i7core_dev->mci = NULL;
  1981. return rc;
  1982. }
  1983. /*
  1984. * i7core_probe Probe for ONE instance of device to see if it is
  1985. * present.
  1986. * return:
  1987. * 0 for FOUND a device
  1988. * < 0 for error code
  1989. */
  1990. static int __devinit i7core_probe(struct pci_dev *pdev,
  1991. const struct pci_device_id *id)
  1992. {
  1993. int rc, count = 0;
  1994. struct i7core_dev *i7core_dev;
  1995. /* get the pci devices we want to reserve for our use */
  1996. mutex_lock(&i7core_edac_lock);
  1997. /*
  1998. * All memory controllers are allocated at the first pass.
  1999. */
  2000. if (unlikely(probed >= 1)) {
  2001. mutex_unlock(&i7core_edac_lock);
  2002. return -ENODEV;
  2003. }
  2004. probed++;
  2005. rc = i7core_get_all_devices();
  2006. if (unlikely(rc < 0))
  2007. goto fail0;
  2008. list_for_each_entry(i7core_dev, &i7core_edac_list, list) {
  2009. count++;
  2010. rc = i7core_register_mci(i7core_dev);
  2011. if (unlikely(rc < 0))
  2012. goto fail1;
  2013. }
  2014. /*
  2015. * Nehalem-EX uses a different memory controller. However, as the
  2016. * memory controller is not visible on some Nehalem/Nehalem-EP, we
  2017. * need to indirectly probe via a X58 PCI device. The same devices
  2018. * are found on (some) Nehalem-EX. So, on those machines, the
  2019. * probe routine needs to return -ENODEV, as the actual Memory
  2020. * Controller registers won't be detected.
  2021. */
  2022. if (!count) {
  2023. rc = -ENODEV;
  2024. goto fail1;
  2025. }
  2026. i7core_printk(KERN_INFO,
  2027. "Driver loaded, %d memory controller(s) found.\n",
  2028. count);
  2029. mutex_unlock(&i7core_edac_lock);
  2030. return 0;
  2031. fail1:
  2032. list_for_each_entry(i7core_dev, &i7core_edac_list, list)
  2033. i7core_unregister_mci(i7core_dev);
  2034. i7core_put_all_devices();
  2035. fail0:
  2036. mutex_unlock(&i7core_edac_lock);
  2037. return rc;
  2038. }
  2039. /*
  2040. * i7core_remove destructor for one instance of device
  2041. *
  2042. */
  2043. static void __devexit i7core_remove(struct pci_dev *pdev)
  2044. {
  2045. struct i7core_dev *i7core_dev;
  2046. debugf0(__FILE__ ": %s()\n", __func__);
  2047. /*
  2048. * we have a trouble here: pdev value for removal will be wrong, since
  2049. * it will point to the X58 register used to detect that the machine
  2050. * is a Nehalem or upper design. However, due to the way several PCI
  2051. * devices are grouped together to provide MC functionality, we need
  2052. * to use a different method for releasing the devices
  2053. */
  2054. mutex_lock(&i7core_edac_lock);
  2055. if (unlikely(!probed)) {
  2056. mutex_unlock(&i7core_edac_lock);
  2057. return;
  2058. }
  2059. list_for_each_entry(i7core_dev, &i7core_edac_list, list)
  2060. i7core_unregister_mci(i7core_dev);
  2061. /* Release PCI resources */
  2062. i7core_put_all_devices();
  2063. probed--;
  2064. mutex_unlock(&i7core_edac_lock);
  2065. }
  2066. MODULE_DEVICE_TABLE(pci, i7core_pci_tbl);
  2067. /*
  2068. * i7core_driver pci_driver structure for this module
  2069. *
  2070. */
  2071. static struct pci_driver i7core_driver = {
  2072. .name = "i7core_edac",
  2073. .probe = i7core_probe,
  2074. .remove = __devexit_p(i7core_remove),
  2075. .id_table = i7core_pci_tbl,
  2076. };
  2077. /*
  2078. * i7core_init Module entry function
  2079. * Try to initialize this module for its devices
  2080. */
  2081. static int __init i7core_init(void)
  2082. {
  2083. int pci_rc;
  2084. debugf2("MC: " __FILE__ ": %s()\n", __func__);
  2085. /* Ensure that the OPSTATE is set correctly for POLL or NMI */
  2086. opstate_init();
  2087. if (use_pci_fixup)
  2088. i7core_xeon_pci_fixup(pci_dev_table);
  2089. pci_rc = pci_register_driver(&i7core_driver);
  2090. if (pci_rc >= 0) {
  2091. mce_register_decode_chain(&i7_mce_dec);
  2092. return 0;
  2093. }
  2094. i7core_printk(KERN_ERR, "Failed to register device with error %d.\n",
  2095. pci_rc);
  2096. return pci_rc;
  2097. }
  2098. /*
  2099. * i7core_exit() Module exit function
  2100. * Unregister the driver
  2101. */
  2102. static void __exit i7core_exit(void)
  2103. {
  2104. debugf2("MC: " __FILE__ ": %s()\n", __func__);
  2105. pci_unregister_driver(&i7core_driver);
  2106. mce_unregister_decode_chain(&i7_mce_dec);
  2107. }
  2108. module_init(i7core_init);
  2109. module_exit(i7core_exit);
  2110. MODULE_LICENSE("GPL");
  2111. MODULE_AUTHOR("Mauro Carvalho Chehab <mchehab@redhat.com>");
  2112. MODULE_AUTHOR("Red Hat Inc. (http://www.redhat.com)");
  2113. MODULE_DESCRIPTION("MC Driver for Intel i7 Core memory controllers - "
  2114. I7CORE_REVISION);
  2115. module_param(edac_op_state, int, 0444);
  2116. MODULE_PARM_DESC(edac_op_state, "EDAC Error Reporting state: 0=Poll,1=NMI");