sb_edac.c 91 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499
  1. /* Intel Sandy Bridge -EN/-EP/-EX Memory Controller kernel module
  2. *
  3. * This driver supports the memory controllers found on the Intel
  4. * processor family Sandy Bridge.
  5. *
  6. * This file may be distributed under the terms of the
  7. * GNU General Public License version 2 only.
  8. *
  9. * Copyright (c) 2011 by:
  10. * Mauro Carvalho Chehab
  11. */
  12. #include <linux/module.h>
  13. #include <linux/init.h>
  14. #include <linux/pci.h>
  15. #include <linux/pci_ids.h>
  16. #include <linux/slab.h>
  17. #include <linux/delay.h>
  18. #include <linux/edac.h>
  19. #include <linux/mmzone.h>
  20. #include <linux/smp.h>
  21. #include <linux/bitmap.h>
  22. #include <linux/math64.h>
  23. #include <linux/mod_devicetable.h>
  24. #include <asm/cpu_device_id.h>
  25. #include <asm/intel-family.h>
  26. #include <asm/processor.h>
  27. #include <asm/mce.h>
  28. #include "edac_module.h"
  29. /* Static vars */
  30. static LIST_HEAD(sbridge_edac_list);
  31. /*
  32. * Alter this version for the module when modifications are made
  33. */
  34. #define SBRIDGE_REVISION " Ver: 1.1.1 "
  35. #define EDAC_MOD_STR "sbridge_edac"
  36. /*
  37. * Debug macros
  38. */
  39. #define sbridge_printk(level, fmt, arg...) \
  40. edac_printk(level, "sbridge", fmt, ##arg)
  41. #define sbridge_mc_printk(mci, level, fmt, arg...) \
  42. edac_mc_chipset_printk(mci, level, "sbridge", fmt, ##arg)
  43. /*
  44. * Get a bit field at register value <v>, from bit <lo> to bit <hi>
  45. */
  46. #define GET_BITFIELD(v, lo, hi) \
  47. (((v) & GENMASK_ULL(hi, lo)) >> (lo))
  48. /* Devices 12 Function 6, Offsets 0x80 to 0xcc */
  49. static const u32 sbridge_dram_rule[] = {
  50. 0x80, 0x88, 0x90, 0x98, 0xa0,
  51. 0xa8, 0xb0, 0xb8, 0xc0, 0xc8,
  52. };
  53. static const u32 ibridge_dram_rule[] = {
  54. 0x60, 0x68, 0x70, 0x78, 0x80,
  55. 0x88, 0x90, 0x98, 0xa0, 0xa8,
  56. 0xb0, 0xb8, 0xc0, 0xc8, 0xd0,
  57. 0xd8, 0xe0, 0xe8, 0xf0, 0xf8,
  58. };
  59. static const u32 knl_dram_rule[] = {
  60. 0x60, 0x68, 0x70, 0x78, 0x80, /* 0-4 */
  61. 0x88, 0x90, 0x98, 0xa0, 0xa8, /* 5-9 */
  62. 0xb0, 0xb8, 0xc0, 0xc8, 0xd0, /* 10-14 */
  63. 0xd8, 0xe0, 0xe8, 0xf0, 0xf8, /* 15-19 */
  64. 0x100, 0x108, 0x110, 0x118, /* 20-23 */
  65. };
  66. #define DRAM_RULE_ENABLE(reg) GET_BITFIELD(reg, 0, 0)
  67. #define A7MODE(reg) GET_BITFIELD(reg, 26, 26)
  68. static char *show_dram_attr(u32 attr)
  69. {
  70. switch (attr) {
  71. case 0:
  72. return "DRAM";
  73. case 1:
  74. return "MMCFG";
  75. case 2:
  76. return "NXM";
  77. default:
  78. return "unknown";
  79. }
  80. }
  81. static const u32 sbridge_interleave_list[] = {
  82. 0x84, 0x8c, 0x94, 0x9c, 0xa4,
  83. 0xac, 0xb4, 0xbc, 0xc4, 0xcc,
  84. };
  85. static const u32 ibridge_interleave_list[] = {
  86. 0x64, 0x6c, 0x74, 0x7c, 0x84,
  87. 0x8c, 0x94, 0x9c, 0xa4, 0xac,
  88. 0xb4, 0xbc, 0xc4, 0xcc, 0xd4,
  89. 0xdc, 0xe4, 0xec, 0xf4, 0xfc,
  90. };
  91. static const u32 knl_interleave_list[] = {
  92. 0x64, 0x6c, 0x74, 0x7c, 0x84, /* 0-4 */
  93. 0x8c, 0x94, 0x9c, 0xa4, 0xac, /* 5-9 */
  94. 0xb4, 0xbc, 0xc4, 0xcc, 0xd4, /* 10-14 */
  95. 0xdc, 0xe4, 0xec, 0xf4, 0xfc, /* 15-19 */
  96. 0x104, 0x10c, 0x114, 0x11c, /* 20-23 */
  97. };
  98. struct interleave_pkg {
  99. unsigned char start;
  100. unsigned char end;
  101. };
  102. static const struct interleave_pkg sbridge_interleave_pkg[] = {
  103. { 0, 2 },
  104. { 3, 5 },
  105. { 8, 10 },
  106. { 11, 13 },
  107. { 16, 18 },
  108. { 19, 21 },
  109. { 24, 26 },
  110. { 27, 29 },
  111. };
  112. static const struct interleave_pkg ibridge_interleave_pkg[] = {
  113. { 0, 3 },
  114. { 4, 7 },
  115. { 8, 11 },
  116. { 12, 15 },
  117. { 16, 19 },
  118. { 20, 23 },
  119. { 24, 27 },
  120. { 28, 31 },
  121. };
  122. static inline int sad_pkg(const struct interleave_pkg *table, u32 reg,
  123. int interleave)
  124. {
  125. return GET_BITFIELD(reg, table[interleave].start,
  126. table[interleave].end);
  127. }
  128. /* Devices 12 Function 7 */
  129. #define TOLM 0x80
  130. #define TOHM 0x84
  131. #define HASWELL_TOLM 0xd0
  132. #define HASWELL_TOHM_0 0xd4
  133. #define HASWELL_TOHM_1 0xd8
  134. #define KNL_TOLM 0xd0
  135. #define KNL_TOHM_0 0xd4
  136. #define KNL_TOHM_1 0xd8
  137. #define GET_TOLM(reg) ((GET_BITFIELD(reg, 0, 3) << 28) | 0x3ffffff)
  138. #define GET_TOHM(reg) ((GET_BITFIELD(reg, 0, 20) << 25) | 0x3ffffff)
  139. /* Device 13 Function 6 */
  140. #define SAD_TARGET 0xf0
  141. #define SOURCE_ID(reg) GET_BITFIELD(reg, 9, 11)
  142. #define SOURCE_ID_KNL(reg) GET_BITFIELD(reg, 12, 14)
  143. #define SAD_CONTROL 0xf4
  144. /* Device 14 function 0 */
  145. static const u32 tad_dram_rule[] = {
  146. 0x40, 0x44, 0x48, 0x4c,
  147. 0x50, 0x54, 0x58, 0x5c,
  148. 0x60, 0x64, 0x68, 0x6c,
  149. };
  150. #define MAX_TAD ARRAY_SIZE(tad_dram_rule)
  151. #define TAD_LIMIT(reg) ((GET_BITFIELD(reg, 12, 31) << 26) | 0x3ffffff)
  152. #define TAD_SOCK(reg) GET_BITFIELD(reg, 10, 11)
  153. #define TAD_CH(reg) GET_BITFIELD(reg, 8, 9)
  154. #define TAD_TGT3(reg) GET_BITFIELD(reg, 6, 7)
  155. #define TAD_TGT2(reg) GET_BITFIELD(reg, 4, 5)
  156. #define TAD_TGT1(reg) GET_BITFIELD(reg, 2, 3)
  157. #define TAD_TGT0(reg) GET_BITFIELD(reg, 0, 1)
  158. /* Device 15, function 0 */
  159. #define MCMTR 0x7c
  160. #define KNL_MCMTR 0x624
  161. #define IS_ECC_ENABLED(mcmtr) GET_BITFIELD(mcmtr, 2, 2)
  162. #define IS_LOCKSTEP_ENABLED(mcmtr) GET_BITFIELD(mcmtr, 1, 1)
  163. #define IS_CLOSE_PG(mcmtr) GET_BITFIELD(mcmtr, 0, 0)
  164. /* Device 15, function 1 */
  165. #define RASENABLES 0xac
  166. #define IS_MIRROR_ENABLED(reg) GET_BITFIELD(reg, 0, 0)
  167. /* Device 15, functions 2-5 */
  168. static const int mtr_regs[] = {
  169. 0x80, 0x84, 0x88,
  170. };
  171. static const int knl_mtr_reg = 0xb60;
  172. #define RANK_DISABLE(mtr) GET_BITFIELD(mtr, 16, 19)
  173. #define IS_DIMM_PRESENT(mtr) GET_BITFIELD(mtr, 14, 14)
  174. #define RANK_CNT_BITS(mtr) GET_BITFIELD(mtr, 12, 13)
  175. #define RANK_WIDTH_BITS(mtr) GET_BITFIELD(mtr, 2, 4)
  176. #define COL_WIDTH_BITS(mtr) GET_BITFIELD(mtr, 0, 1)
  177. static const u32 tad_ch_nilv_offset[] = {
  178. 0x90, 0x94, 0x98, 0x9c,
  179. 0xa0, 0xa4, 0xa8, 0xac,
  180. 0xb0, 0xb4, 0xb8, 0xbc,
  181. };
  182. #define CHN_IDX_OFFSET(reg) GET_BITFIELD(reg, 28, 29)
  183. #define TAD_OFFSET(reg) (GET_BITFIELD(reg, 6, 25) << 26)
  184. static const u32 rir_way_limit[] = {
  185. 0x108, 0x10c, 0x110, 0x114, 0x118,
  186. };
  187. #define MAX_RIR_RANGES ARRAY_SIZE(rir_way_limit)
  188. #define IS_RIR_VALID(reg) GET_BITFIELD(reg, 31, 31)
  189. #define RIR_WAY(reg) GET_BITFIELD(reg, 28, 29)
  190. #define MAX_RIR_WAY 8
  191. static const u32 rir_offset[MAX_RIR_RANGES][MAX_RIR_WAY] = {
  192. { 0x120, 0x124, 0x128, 0x12c, 0x130, 0x134, 0x138, 0x13c },
  193. { 0x140, 0x144, 0x148, 0x14c, 0x150, 0x154, 0x158, 0x15c },
  194. { 0x160, 0x164, 0x168, 0x16c, 0x170, 0x174, 0x178, 0x17c },
  195. { 0x180, 0x184, 0x188, 0x18c, 0x190, 0x194, 0x198, 0x19c },
  196. { 0x1a0, 0x1a4, 0x1a8, 0x1ac, 0x1b0, 0x1b4, 0x1b8, 0x1bc },
  197. };
  198. #define RIR_RNK_TGT(type, reg) (((type) == BROADWELL) ? \
  199. GET_BITFIELD(reg, 20, 23) : GET_BITFIELD(reg, 16, 19))
  200. #define RIR_OFFSET(type, reg) (((type) == HASWELL || (type) == BROADWELL) ? \
  201. GET_BITFIELD(reg, 2, 15) : GET_BITFIELD(reg, 2, 14))
  202. /* Device 16, functions 2-7 */
  203. /*
  204. * FIXME: Implement the error count reads directly
  205. */
  206. static const u32 correrrcnt[] = {
  207. 0x104, 0x108, 0x10c, 0x110,
  208. };
  209. #define RANK_ODD_OV(reg) GET_BITFIELD(reg, 31, 31)
  210. #define RANK_ODD_ERR_CNT(reg) GET_BITFIELD(reg, 16, 30)
  211. #define RANK_EVEN_OV(reg) GET_BITFIELD(reg, 15, 15)
  212. #define RANK_EVEN_ERR_CNT(reg) GET_BITFIELD(reg, 0, 14)
  213. static const u32 correrrthrsld[] = {
  214. 0x11c, 0x120, 0x124, 0x128,
  215. };
  216. #define RANK_ODD_ERR_THRSLD(reg) GET_BITFIELD(reg, 16, 30)
  217. #define RANK_EVEN_ERR_THRSLD(reg) GET_BITFIELD(reg, 0, 14)
  218. /* Device 17, function 0 */
  219. #define SB_RANK_CFG_A 0x0328
  220. #define IB_RANK_CFG_A 0x0320
  221. /*
  222. * sbridge structs
  223. */
  224. #define NUM_CHANNELS 8 /* 2MC per socket, four chan per MC */
  225. #define MAX_DIMMS 3 /* Max DIMMS per channel */
  226. #define KNL_MAX_CHAS 38 /* KNL max num. of Cache Home Agents */
  227. #define KNL_MAX_CHANNELS 6 /* KNL max num. of PCI channels */
  228. #define KNL_MAX_EDCS 8 /* Embedded DRAM controllers */
  229. #define CHANNEL_UNSPECIFIED 0xf /* Intel IA32 SDM 15-14 */
  230. enum type {
  231. SANDY_BRIDGE,
  232. IVY_BRIDGE,
  233. HASWELL,
  234. BROADWELL,
  235. KNIGHTS_LANDING,
  236. };
  237. struct sbridge_pvt;
  238. struct sbridge_info {
  239. enum type type;
  240. u32 mcmtr;
  241. u32 rankcfgr;
  242. u64 (*get_tolm)(struct sbridge_pvt *pvt);
  243. u64 (*get_tohm)(struct sbridge_pvt *pvt);
  244. u64 (*rir_limit)(u32 reg);
  245. u64 (*sad_limit)(u32 reg);
  246. u32 (*interleave_mode)(u32 reg);
  247. char* (*show_interleave_mode)(u32 reg);
  248. u32 (*dram_attr)(u32 reg);
  249. const u32 *dram_rule;
  250. const u32 *interleave_list;
  251. const struct interleave_pkg *interleave_pkg;
  252. u8 max_sad;
  253. u8 max_interleave;
  254. u8 (*get_node_id)(struct sbridge_pvt *pvt);
  255. enum mem_type (*get_memory_type)(struct sbridge_pvt *pvt);
  256. enum dev_type (*get_width)(struct sbridge_pvt *pvt, u32 mtr);
  257. struct pci_dev *pci_vtd;
  258. };
  259. struct sbridge_channel {
  260. u32 ranks;
  261. u32 dimms;
  262. };
  263. struct pci_id_descr {
  264. int dev_id;
  265. int optional;
  266. };
  267. struct pci_id_table {
  268. const struct pci_id_descr *descr;
  269. int n_devs;
  270. enum type type;
  271. };
  272. struct sbridge_dev {
  273. struct list_head list;
  274. u8 bus, mc;
  275. u8 node_id, source_id;
  276. struct pci_dev **pdev;
  277. int n_devs;
  278. struct mem_ctl_info *mci;
  279. };
  280. struct knl_pvt {
  281. struct pci_dev *pci_cha[KNL_MAX_CHAS];
  282. struct pci_dev *pci_channel[KNL_MAX_CHANNELS];
  283. struct pci_dev *pci_mc0;
  284. struct pci_dev *pci_mc1;
  285. struct pci_dev *pci_mc0_misc;
  286. struct pci_dev *pci_mc1_misc;
  287. struct pci_dev *pci_mc_info; /* tolm, tohm */
  288. };
  289. struct sbridge_pvt {
  290. struct pci_dev *pci_ta, *pci_ddrio, *pci_ras;
  291. struct pci_dev *pci_sad0, *pci_sad1;
  292. struct pci_dev *pci_ha0, *pci_ha1;
  293. struct pci_dev *pci_br0, *pci_br1;
  294. struct pci_dev *pci_ha1_ta;
  295. struct pci_dev *pci_tad[NUM_CHANNELS];
  296. struct sbridge_dev *sbridge_dev;
  297. struct sbridge_info info;
  298. struct sbridge_channel channel[NUM_CHANNELS];
  299. /* Memory type detection */
  300. bool is_mirrored, is_lockstep, is_close_pg;
  301. bool is_chan_hash;
  302. /* Memory description */
  303. u64 tolm, tohm;
  304. struct knl_pvt knl;
  305. };
  306. #define PCI_DESCR(device_id, opt) \
  307. .dev_id = (device_id), \
  308. .optional = opt
  309. static const struct pci_id_descr pci_dev_descr_sbridge[] = {
  310. /* Processor Home Agent */
  311. { PCI_DESCR(PCI_DEVICE_ID_INTEL_SBRIDGE_IMC_HA0, 0) },
  312. /* Memory controller */
  313. { PCI_DESCR(PCI_DEVICE_ID_INTEL_SBRIDGE_IMC_TA, 0) },
  314. { PCI_DESCR(PCI_DEVICE_ID_INTEL_SBRIDGE_IMC_RAS, 0) },
  315. { PCI_DESCR(PCI_DEVICE_ID_INTEL_SBRIDGE_IMC_TAD0, 0) },
  316. { PCI_DESCR(PCI_DEVICE_ID_INTEL_SBRIDGE_IMC_TAD1, 0) },
  317. { PCI_DESCR(PCI_DEVICE_ID_INTEL_SBRIDGE_IMC_TAD2, 0) },
  318. { PCI_DESCR(PCI_DEVICE_ID_INTEL_SBRIDGE_IMC_TAD3, 0) },
  319. { PCI_DESCR(PCI_DEVICE_ID_INTEL_SBRIDGE_IMC_DDRIO, 1) },
  320. /* System Address Decoder */
  321. { PCI_DESCR(PCI_DEVICE_ID_INTEL_SBRIDGE_SAD0, 0) },
  322. { PCI_DESCR(PCI_DEVICE_ID_INTEL_SBRIDGE_SAD1, 0) },
  323. /* Broadcast Registers */
  324. { PCI_DESCR(PCI_DEVICE_ID_INTEL_SBRIDGE_BR, 0) },
  325. };
  326. #define PCI_ID_TABLE_ENTRY(A, T) { \
  327. .descr = A, \
  328. .n_devs = ARRAY_SIZE(A), \
  329. .type = T \
  330. }
  331. static const struct pci_id_table pci_dev_descr_sbridge_table[] = {
  332. PCI_ID_TABLE_ENTRY(pci_dev_descr_sbridge, SANDY_BRIDGE),
  333. {0,} /* 0 terminated list. */
  334. };
  335. /* This changes depending if 1HA or 2HA:
  336. * 1HA:
  337. * 0x0eb8 (17.0) is DDRIO0
  338. * 2HA:
  339. * 0x0ebc (17.4) is DDRIO0
  340. */
  341. #define PCI_DEVICE_ID_INTEL_IBRIDGE_IMC_1HA_DDRIO0 0x0eb8
  342. #define PCI_DEVICE_ID_INTEL_IBRIDGE_IMC_2HA_DDRIO0 0x0ebc
  343. /* pci ids */
  344. #define PCI_DEVICE_ID_INTEL_IBRIDGE_IMC_HA0 0x0ea0
  345. #define PCI_DEVICE_ID_INTEL_IBRIDGE_IMC_HA0_TA 0x0ea8
  346. #define PCI_DEVICE_ID_INTEL_IBRIDGE_IMC_HA0_RAS 0x0e71
  347. #define PCI_DEVICE_ID_INTEL_IBRIDGE_IMC_HA0_TAD0 0x0eaa
  348. #define PCI_DEVICE_ID_INTEL_IBRIDGE_IMC_HA0_TAD1 0x0eab
  349. #define PCI_DEVICE_ID_INTEL_IBRIDGE_IMC_HA0_TAD2 0x0eac
  350. #define PCI_DEVICE_ID_INTEL_IBRIDGE_IMC_HA0_TAD3 0x0ead
  351. #define PCI_DEVICE_ID_INTEL_IBRIDGE_SAD 0x0ec8
  352. #define PCI_DEVICE_ID_INTEL_IBRIDGE_BR0 0x0ec9
  353. #define PCI_DEVICE_ID_INTEL_IBRIDGE_BR1 0x0eca
  354. #define PCI_DEVICE_ID_INTEL_IBRIDGE_IMC_HA1 0x0e60
  355. #define PCI_DEVICE_ID_INTEL_IBRIDGE_IMC_HA1_TA 0x0e68
  356. #define PCI_DEVICE_ID_INTEL_IBRIDGE_IMC_HA1_RAS 0x0e79
  357. #define PCI_DEVICE_ID_INTEL_IBRIDGE_IMC_HA1_TAD0 0x0e6a
  358. #define PCI_DEVICE_ID_INTEL_IBRIDGE_IMC_HA1_TAD1 0x0e6b
  359. #define PCI_DEVICE_ID_INTEL_IBRIDGE_IMC_HA1_TAD2 0x0e6c
  360. #define PCI_DEVICE_ID_INTEL_IBRIDGE_IMC_HA1_TAD3 0x0e6d
  361. static const struct pci_id_descr pci_dev_descr_ibridge[] = {
  362. /* Processor Home Agent */
  363. { PCI_DESCR(PCI_DEVICE_ID_INTEL_IBRIDGE_IMC_HA0, 0) },
  364. /* Memory controller */
  365. { PCI_DESCR(PCI_DEVICE_ID_INTEL_IBRIDGE_IMC_HA0_TA, 0) },
  366. { PCI_DESCR(PCI_DEVICE_ID_INTEL_IBRIDGE_IMC_HA0_RAS, 0) },
  367. { PCI_DESCR(PCI_DEVICE_ID_INTEL_IBRIDGE_IMC_HA0_TAD0, 0) },
  368. { PCI_DESCR(PCI_DEVICE_ID_INTEL_IBRIDGE_IMC_HA0_TAD1, 0) },
  369. { PCI_DESCR(PCI_DEVICE_ID_INTEL_IBRIDGE_IMC_HA0_TAD2, 0) },
  370. { PCI_DESCR(PCI_DEVICE_ID_INTEL_IBRIDGE_IMC_HA0_TAD3, 0) },
  371. /* System Address Decoder */
  372. { PCI_DESCR(PCI_DEVICE_ID_INTEL_IBRIDGE_SAD, 0) },
  373. /* Broadcast Registers */
  374. { PCI_DESCR(PCI_DEVICE_ID_INTEL_IBRIDGE_BR0, 1) },
  375. { PCI_DESCR(PCI_DEVICE_ID_INTEL_IBRIDGE_BR1, 0) },
  376. /* Optional, mode 2HA */
  377. { PCI_DESCR(PCI_DEVICE_ID_INTEL_IBRIDGE_IMC_HA1, 1) },
  378. #if 0
  379. { PCI_DESCR(PCI_DEVICE_ID_INTEL_IBRIDGE_IMC_HA1_TA, 1) },
  380. { PCI_DESCR(PCI_DEVICE_ID_INTEL_IBRIDGE_IMC_HA1_RAS, 1) },
  381. #endif
  382. { PCI_DESCR(PCI_DEVICE_ID_INTEL_IBRIDGE_IMC_HA1_TAD0, 1) },
  383. { PCI_DESCR(PCI_DEVICE_ID_INTEL_IBRIDGE_IMC_HA1_TAD1, 1) },
  384. { PCI_DESCR(PCI_DEVICE_ID_INTEL_IBRIDGE_IMC_HA1_TAD2, 1) },
  385. { PCI_DESCR(PCI_DEVICE_ID_INTEL_IBRIDGE_IMC_HA1_TAD3, 1) },
  386. { PCI_DESCR(PCI_DEVICE_ID_INTEL_IBRIDGE_IMC_1HA_DDRIO0, 1) },
  387. { PCI_DESCR(PCI_DEVICE_ID_INTEL_IBRIDGE_IMC_2HA_DDRIO0, 1) },
  388. };
  389. static const struct pci_id_table pci_dev_descr_ibridge_table[] = {
  390. PCI_ID_TABLE_ENTRY(pci_dev_descr_ibridge, IVY_BRIDGE),
  391. {0,} /* 0 terminated list. */
  392. };
  393. /* Haswell support */
  394. /* EN processor:
  395. * - 1 IMC
  396. * - 3 DDR3 channels, 2 DPC per channel
  397. * EP processor:
  398. * - 1 or 2 IMC
  399. * - 4 DDR4 channels, 3 DPC per channel
  400. * EP 4S processor:
  401. * - 2 IMC
  402. * - 4 DDR4 channels, 3 DPC per channel
  403. * EX processor:
  404. * - 2 IMC
  405. * - each IMC interfaces with a SMI 2 channel
  406. * - each SMI channel interfaces with a scalable memory buffer
  407. * - each scalable memory buffer supports 4 DDR3/DDR4 channels, 3 DPC
  408. */
  409. #define HASWELL_DDRCRCLKCONTROLS 0xa10 /* Ditto on Broadwell */
  410. #define HASWELL_HASYSDEFEATURE2 0x84
  411. #define PCI_DEVICE_ID_INTEL_HASWELL_IMC_VTD_MISC 0x2f28
  412. #define PCI_DEVICE_ID_INTEL_HASWELL_IMC_HA0 0x2fa0
  413. #define PCI_DEVICE_ID_INTEL_HASWELL_IMC_HA1 0x2f60
  414. #define PCI_DEVICE_ID_INTEL_HASWELL_IMC_HA0_TA 0x2fa8
  415. #define PCI_DEVICE_ID_INTEL_HASWELL_IMC_HA0_THERMAL 0x2f71
  416. #define PCI_DEVICE_ID_INTEL_HASWELL_IMC_HA1_TA 0x2f68
  417. #define PCI_DEVICE_ID_INTEL_HASWELL_IMC_HA1_THERMAL 0x2f79
  418. #define PCI_DEVICE_ID_INTEL_HASWELL_IMC_CBO_SAD0 0x2ffc
  419. #define PCI_DEVICE_ID_INTEL_HASWELL_IMC_CBO_SAD1 0x2ffd
  420. #define PCI_DEVICE_ID_INTEL_HASWELL_IMC_HA0_TAD0 0x2faa
  421. #define PCI_DEVICE_ID_INTEL_HASWELL_IMC_HA0_TAD1 0x2fab
  422. #define PCI_DEVICE_ID_INTEL_HASWELL_IMC_HA0_TAD2 0x2fac
  423. #define PCI_DEVICE_ID_INTEL_HASWELL_IMC_HA0_TAD3 0x2fad
  424. #define PCI_DEVICE_ID_INTEL_HASWELL_IMC_HA1_TAD0 0x2f6a
  425. #define PCI_DEVICE_ID_INTEL_HASWELL_IMC_HA1_TAD1 0x2f6b
  426. #define PCI_DEVICE_ID_INTEL_HASWELL_IMC_HA1_TAD2 0x2f6c
  427. #define PCI_DEVICE_ID_INTEL_HASWELL_IMC_HA1_TAD3 0x2f6d
  428. #define PCI_DEVICE_ID_INTEL_HASWELL_IMC_DDRIO0 0x2fbd
  429. #define PCI_DEVICE_ID_INTEL_HASWELL_IMC_DDRIO1 0x2fbf
  430. #define PCI_DEVICE_ID_INTEL_HASWELL_IMC_DDRIO2 0x2fb9
  431. #define PCI_DEVICE_ID_INTEL_HASWELL_IMC_DDRIO3 0x2fbb
  432. static const struct pci_id_descr pci_dev_descr_haswell[] = {
  433. /* first item must be the HA */
  434. { PCI_DESCR(PCI_DEVICE_ID_INTEL_HASWELL_IMC_HA0, 0) },
  435. { PCI_DESCR(PCI_DEVICE_ID_INTEL_HASWELL_IMC_CBO_SAD0, 0) },
  436. { PCI_DESCR(PCI_DEVICE_ID_INTEL_HASWELL_IMC_CBO_SAD1, 0) },
  437. { PCI_DESCR(PCI_DEVICE_ID_INTEL_HASWELL_IMC_HA1, 1) },
  438. { PCI_DESCR(PCI_DEVICE_ID_INTEL_HASWELL_IMC_HA0_TA, 0) },
  439. { PCI_DESCR(PCI_DEVICE_ID_INTEL_HASWELL_IMC_HA0_THERMAL, 0) },
  440. { PCI_DESCR(PCI_DEVICE_ID_INTEL_HASWELL_IMC_HA0_TAD0, 0) },
  441. { PCI_DESCR(PCI_DEVICE_ID_INTEL_HASWELL_IMC_HA0_TAD1, 0) },
  442. { PCI_DESCR(PCI_DEVICE_ID_INTEL_HASWELL_IMC_HA0_TAD2, 1) },
  443. { PCI_DESCR(PCI_DEVICE_ID_INTEL_HASWELL_IMC_HA0_TAD3, 1) },
  444. { PCI_DESCR(PCI_DEVICE_ID_INTEL_HASWELL_IMC_DDRIO0, 1) },
  445. { PCI_DESCR(PCI_DEVICE_ID_INTEL_HASWELL_IMC_DDRIO1, 1) },
  446. { PCI_DESCR(PCI_DEVICE_ID_INTEL_HASWELL_IMC_DDRIO2, 1) },
  447. { PCI_DESCR(PCI_DEVICE_ID_INTEL_HASWELL_IMC_DDRIO3, 1) },
  448. { PCI_DESCR(PCI_DEVICE_ID_INTEL_HASWELL_IMC_HA1_TA, 1) },
  449. { PCI_DESCR(PCI_DEVICE_ID_INTEL_HASWELL_IMC_HA1_THERMAL, 1) },
  450. { PCI_DESCR(PCI_DEVICE_ID_INTEL_HASWELL_IMC_HA1_TAD0, 1) },
  451. { PCI_DESCR(PCI_DEVICE_ID_INTEL_HASWELL_IMC_HA1_TAD1, 1) },
  452. { PCI_DESCR(PCI_DEVICE_ID_INTEL_HASWELL_IMC_HA1_TAD2, 1) },
  453. { PCI_DESCR(PCI_DEVICE_ID_INTEL_HASWELL_IMC_HA1_TAD3, 1) },
  454. };
  455. static const struct pci_id_table pci_dev_descr_haswell_table[] = {
  456. PCI_ID_TABLE_ENTRY(pci_dev_descr_haswell, HASWELL),
  457. {0,} /* 0 terminated list. */
  458. };
  459. /* Knight's Landing Support */
  460. /*
  461. * KNL's memory channels are swizzled between memory controllers.
  462. * MC0 is mapped to CH3,4,5 and MC1 is mapped to CH0,1,2
  463. */
  464. #define knl_channel_remap(mc, chan) ((mc) ? (chan) : (chan) + 3)
  465. /* Memory controller, TAD tables, error injection - 2-8-0, 2-9-0 (2 of these) */
  466. #define PCI_DEVICE_ID_INTEL_KNL_IMC_MC 0x7840
  467. /* DRAM channel stuff; bank addrs, dimmmtr, etc.. 2-8-2 - 2-9-4 (6 of these) */
  468. #define PCI_DEVICE_ID_INTEL_KNL_IMC_CHANNEL 0x7843
  469. /* kdrwdbu TAD limits/offsets, MCMTR - 2-10-1, 2-11-1 (2 of these) */
  470. #define PCI_DEVICE_ID_INTEL_KNL_IMC_TA 0x7844
  471. /* CHA broadcast registers, dram rules - 1-29-0 (1 of these) */
  472. #define PCI_DEVICE_ID_INTEL_KNL_IMC_SAD0 0x782a
  473. /* SAD target - 1-29-1 (1 of these) */
  474. #define PCI_DEVICE_ID_INTEL_KNL_IMC_SAD1 0x782b
  475. /* Caching / Home Agent */
  476. #define PCI_DEVICE_ID_INTEL_KNL_IMC_CHA 0x782c
  477. /* Device with TOLM and TOHM, 0-5-0 (1 of these) */
  478. #define PCI_DEVICE_ID_INTEL_KNL_IMC_TOLHM 0x7810
  479. /*
  480. * KNL differs from SB, IB, and Haswell in that it has multiple
  481. * instances of the same device with the same device ID, so we handle that
  482. * by creating as many copies in the table as we expect to find.
  483. * (Like device ID must be grouped together.)
  484. */
  485. static const struct pci_id_descr pci_dev_descr_knl[] = {
  486. [0] = { PCI_DESCR(PCI_DEVICE_ID_INTEL_KNL_IMC_SAD0, 0) },
  487. [1] = { PCI_DESCR(PCI_DEVICE_ID_INTEL_KNL_IMC_SAD1, 0) },
  488. [2 ... 3] = { PCI_DESCR(PCI_DEVICE_ID_INTEL_KNL_IMC_MC, 0)},
  489. [4 ... 41] = { PCI_DESCR(PCI_DEVICE_ID_INTEL_KNL_IMC_CHA, 0) },
  490. [42 ... 47] = { PCI_DESCR(PCI_DEVICE_ID_INTEL_KNL_IMC_CHANNEL, 0) },
  491. [48] = { PCI_DESCR(PCI_DEVICE_ID_INTEL_KNL_IMC_TA, 0) },
  492. [49] = { PCI_DESCR(PCI_DEVICE_ID_INTEL_KNL_IMC_TOLHM, 0) },
  493. };
  494. static const struct pci_id_table pci_dev_descr_knl_table[] = {
  495. PCI_ID_TABLE_ENTRY(pci_dev_descr_knl, KNIGHTS_LANDING),
  496. {0,}
  497. };
  498. /*
  499. * Broadwell support
  500. *
  501. * DE processor:
  502. * - 1 IMC
  503. * - 2 DDR3 channels, 2 DPC per channel
  504. * EP processor:
  505. * - 1 or 2 IMC
  506. * - 4 DDR4 channels, 3 DPC per channel
  507. * EP 4S processor:
  508. * - 2 IMC
  509. * - 4 DDR4 channels, 3 DPC per channel
  510. * EX processor:
  511. * - 2 IMC
  512. * - each IMC interfaces with a SMI 2 channel
  513. * - each SMI channel interfaces with a scalable memory buffer
  514. * - each scalable memory buffer supports 4 DDR3/DDR4 channels, 3 DPC
  515. */
  516. #define PCI_DEVICE_ID_INTEL_BROADWELL_IMC_VTD_MISC 0x6f28
  517. #define PCI_DEVICE_ID_INTEL_BROADWELL_IMC_HA0 0x6fa0
  518. #define PCI_DEVICE_ID_INTEL_BROADWELL_IMC_HA1 0x6f60
  519. #define PCI_DEVICE_ID_INTEL_BROADWELL_IMC_HA0_TA 0x6fa8
  520. #define PCI_DEVICE_ID_INTEL_BROADWELL_IMC_HA0_THERMAL 0x6f71
  521. #define PCI_DEVICE_ID_INTEL_BROADWELL_IMC_HA1_TA 0x6f68
  522. #define PCI_DEVICE_ID_INTEL_BROADWELL_IMC_HA1_THERMAL 0x6f79
  523. #define PCI_DEVICE_ID_INTEL_BROADWELL_IMC_CBO_SAD0 0x6ffc
  524. #define PCI_DEVICE_ID_INTEL_BROADWELL_IMC_CBO_SAD1 0x6ffd
  525. #define PCI_DEVICE_ID_INTEL_BROADWELL_IMC_HA0_TAD0 0x6faa
  526. #define PCI_DEVICE_ID_INTEL_BROADWELL_IMC_HA0_TAD1 0x6fab
  527. #define PCI_DEVICE_ID_INTEL_BROADWELL_IMC_HA0_TAD2 0x6fac
  528. #define PCI_DEVICE_ID_INTEL_BROADWELL_IMC_HA0_TAD3 0x6fad
  529. #define PCI_DEVICE_ID_INTEL_BROADWELL_IMC_HA1_TAD0 0x6f6a
  530. #define PCI_DEVICE_ID_INTEL_BROADWELL_IMC_HA1_TAD1 0x6f6b
  531. #define PCI_DEVICE_ID_INTEL_BROADWELL_IMC_HA1_TAD2 0x6f6c
  532. #define PCI_DEVICE_ID_INTEL_BROADWELL_IMC_HA1_TAD3 0x6f6d
  533. #define PCI_DEVICE_ID_INTEL_BROADWELL_IMC_DDRIO0 0x6faf
  534. static const struct pci_id_descr pci_dev_descr_broadwell[] = {
  535. /* first item must be the HA */
  536. { PCI_DESCR(PCI_DEVICE_ID_INTEL_BROADWELL_IMC_HA0, 0) },
  537. { PCI_DESCR(PCI_DEVICE_ID_INTEL_BROADWELL_IMC_CBO_SAD0, 0) },
  538. { PCI_DESCR(PCI_DEVICE_ID_INTEL_BROADWELL_IMC_CBO_SAD1, 0) },
  539. { PCI_DESCR(PCI_DEVICE_ID_INTEL_BROADWELL_IMC_HA1, 1) },
  540. { PCI_DESCR(PCI_DEVICE_ID_INTEL_BROADWELL_IMC_HA0_TA, 0) },
  541. { PCI_DESCR(PCI_DEVICE_ID_INTEL_BROADWELL_IMC_HA0_THERMAL, 0) },
  542. { PCI_DESCR(PCI_DEVICE_ID_INTEL_BROADWELL_IMC_HA0_TAD0, 0) },
  543. { PCI_DESCR(PCI_DEVICE_ID_INTEL_BROADWELL_IMC_HA0_TAD1, 0) },
  544. { PCI_DESCR(PCI_DEVICE_ID_INTEL_BROADWELL_IMC_HA0_TAD2, 1) },
  545. { PCI_DESCR(PCI_DEVICE_ID_INTEL_BROADWELL_IMC_HA0_TAD3, 1) },
  546. { PCI_DESCR(PCI_DEVICE_ID_INTEL_BROADWELL_IMC_DDRIO0, 1) },
  547. { PCI_DESCR(PCI_DEVICE_ID_INTEL_BROADWELL_IMC_HA1_TA, 1) },
  548. { PCI_DESCR(PCI_DEVICE_ID_INTEL_BROADWELL_IMC_HA1_THERMAL, 1) },
  549. { PCI_DESCR(PCI_DEVICE_ID_INTEL_BROADWELL_IMC_HA1_TAD0, 1) },
  550. { PCI_DESCR(PCI_DEVICE_ID_INTEL_BROADWELL_IMC_HA1_TAD1, 1) },
  551. { PCI_DESCR(PCI_DEVICE_ID_INTEL_BROADWELL_IMC_HA1_TAD2, 1) },
  552. { PCI_DESCR(PCI_DEVICE_ID_INTEL_BROADWELL_IMC_HA1_TAD3, 1) },
  553. };
  554. static const struct pci_id_table pci_dev_descr_broadwell_table[] = {
  555. PCI_ID_TABLE_ENTRY(pci_dev_descr_broadwell, BROADWELL),
  556. {0,} /* 0 terminated list. */
  557. };
  558. /****************************************************************************
  559. Ancillary status routines
  560. ****************************************************************************/
  561. static inline int numrank(enum type type, u32 mtr)
  562. {
  563. int ranks = (1 << RANK_CNT_BITS(mtr));
  564. int max = 4;
  565. if (type == HASWELL || type == BROADWELL || type == KNIGHTS_LANDING)
  566. max = 8;
  567. if (ranks > max) {
  568. edac_dbg(0, "Invalid number of ranks: %d (max = %i) raw value = %x (%04x)\n",
  569. ranks, max, (unsigned int)RANK_CNT_BITS(mtr), mtr);
  570. return -EINVAL;
  571. }
  572. return ranks;
  573. }
  574. static inline int numrow(u32 mtr)
  575. {
  576. int rows = (RANK_WIDTH_BITS(mtr) + 12);
  577. if (rows < 13 || rows > 18) {
  578. edac_dbg(0, "Invalid number of rows: %d (should be between 14 and 17) raw value = %x (%04x)\n",
  579. rows, (unsigned int)RANK_WIDTH_BITS(mtr), mtr);
  580. return -EINVAL;
  581. }
  582. return 1 << rows;
  583. }
  584. static inline int numcol(u32 mtr)
  585. {
  586. int cols = (COL_WIDTH_BITS(mtr) + 10);
  587. if (cols > 12) {
  588. edac_dbg(0, "Invalid number of cols: %d (max = 4) raw value = %x (%04x)\n",
  589. cols, (unsigned int)COL_WIDTH_BITS(mtr), mtr);
  590. return -EINVAL;
  591. }
  592. return 1 << cols;
  593. }
  594. static struct sbridge_dev *get_sbridge_dev(u8 bus, int multi_bus)
  595. {
  596. struct sbridge_dev *sbridge_dev;
  597. /*
  598. * If we have devices scattered across several busses that pertain
  599. * to the same memory controller, we'll lump them all together.
  600. */
  601. if (multi_bus) {
  602. return list_first_entry_or_null(&sbridge_edac_list,
  603. struct sbridge_dev, list);
  604. }
  605. list_for_each_entry(sbridge_dev, &sbridge_edac_list, list) {
  606. if (sbridge_dev->bus == bus)
  607. return sbridge_dev;
  608. }
  609. return NULL;
  610. }
  611. static struct sbridge_dev *alloc_sbridge_dev(u8 bus,
  612. const struct pci_id_table *table)
  613. {
  614. struct sbridge_dev *sbridge_dev;
  615. sbridge_dev = kzalloc(sizeof(*sbridge_dev), GFP_KERNEL);
  616. if (!sbridge_dev)
  617. return NULL;
  618. sbridge_dev->pdev = kzalloc(sizeof(*sbridge_dev->pdev) * table->n_devs,
  619. GFP_KERNEL);
  620. if (!sbridge_dev->pdev) {
  621. kfree(sbridge_dev);
  622. return NULL;
  623. }
  624. sbridge_dev->bus = bus;
  625. sbridge_dev->n_devs = table->n_devs;
  626. list_add_tail(&sbridge_dev->list, &sbridge_edac_list);
  627. return sbridge_dev;
  628. }
  629. static void free_sbridge_dev(struct sbridge_dev *sbridge_dev)
  630. {
  631. list_del(&sbridge_dev->list);
  632. kfree(sbridge_dev->pdev);
  633. kfree(sbridge_dev);
  634. }
  635. static u64 sbridge_get_tolm(struct sbridge_pvt *pvt)
  636. {
  637. u32 reg;
  638. /* Address range is 32:28 */
  639. pci_read_config_dword(pvt->pci_sad1, TOLM, &reg);
  640. return GET_TOLM(reg);
  641. }
  642. static u64 sbridge_get_tohm(struct sbridge_pvt *pvt)
  643. {
  644. u32 reg;
  645. pci_read_config_dword(pvt->pci_sad1, TOHM, &reg);
  646. return GET_TOHM(reg);
  647. }
  648. static u64 ibridge_get_tolm(struct sbridge_pvt *pvt)
  649. {
  650. u32 reg;
  651. pci_read_config_dword(pvt->pci_br1, TOLM, &reg);
  652. return GET_TOLM(reg);
  653. }
  654. static u64 ibridge_get_tohm(struct sbridge_pvt *pvt)
  655. {
  656. u32 reg;
  657. pci_read_config_dword(pvt->pci_br1, TOHM, &reg);
  658. return GET_TOHM(reg);
  659. }
  660. static u64 rir_limit(u32 reg)
  661. {
  662. return ((u64)GET_BITFIELD(reg, 1, 10) << 29) | 0x1fffffff;
  663. }
  664. static u64 sad_limit(u32 reg)
  665. {
  666. return (GET_BITFIELD(reg, 6, 25) << 26) | 0x3ffffff;
  667. }
  668. static u32 interleave_mode(u32 reg)
  669. {
  670. return GET_BITFIELD(reg, 1, 1);
  671. }
  672. char *show_interleave_mode(u32 reg)
  673. {
  674. return interleave_mode(reg) ? "8:6" : "[8:6]XOR[18:16]";
  675. }
  676. static u32 dram_attr(u32 reg)
  677. {
  678. return GET_BITFIELD(reg, 2, 3);
  679. }
  680. static u64 knl_sad_limit(u32 reg)
  681. {
  682. return (GET_BITFIELD(reg, 7, 26) << 26) | 0x3ffffff;
  683. }
  684. static u32 knl_interleave_mode(u32 reg)
  685. {
  686. return GET_BITFIELD(reg, 1, 2);
  687. }
  688. static char *knl_show_interleave_mode(u32 reg)
  689. {
  690. char *s;
  691. switch (knl_interleave_mode(reg)) {
  692. case 0:
  693. s = "use address bits [8:6]";
  694. break;
  695. case 1:
  696. s = "use address bits [10:8]";
  697. break;
  698. case 2:
  699. s = "use address bits [14:12]";
  700. break;
  701. case 3:
  702. s = "use address bits [32:30]";
  703. break;
  704. default:
  705. WARN_ON(1);
  706. break;
  707. }
  708. return s;
  709. }
  710. static u32 dram_attr_knl(u32 reg)
  711. {
  712. return GET_BITFIELD(reg, 3, 4);
  713. }
  714. static enum mem_type get_memory_type(struct sbridge_pvt *pvt)
  715. {
  716. u32 reg;
  717. enum mem_type mtype;
  718. if (pvt->pci_ddrio) {
  719. pci_read_config_dword(pvt->pci_ddrio, pvt->info.rankcfgr,
  720. &reg);
  721. if (GET_BITFIELD(reg, 11, 11))
  722. /* FIXME: Can also be LRDIMM */
  723. mtype = MEM_RDDR3;
  724. else
  725. mtype = MEM_DDR3;
  726. } else
  727. mtype = MEM_UNKNOWN;
  728. return mtype;
  729. }
  730. static enum mem_type haswell_get_memory_type(struct sbridge_pvt *pvt)
  731. {
  732. u32 reg;
  733. bool registered = false;
  734. enum mem_type mtype = MEM_UNKNOWN;
  735. if (!pvt->pci_ddrio)
  736. goto out;
  737. pci_read_config_dword(pvt->pci_ddrio,
  738. HASWELL_DDRCRCLKCONTROLS, &reg);
  739. /* Is_Rdimm */
  740. if (GET_BITFIELD(reg, 16, 16))
  741. registered = true;
  742. pci_read_config_dword(pvt->pci_ta, MCMTR, &reg);
  743. if (GET_BITFIELD(reg, 14, 14)) {
  744. if (registered)
  745. mtype = MEM_RDDR4;
  746. else
  747. mtype = MEM_DDR4;
  748. } else {
  749. if (registered)
  750. mtype = MEM_RDDR3;
  751. else
  752. mtype = MEM_DDR3;
  753. }
  754. out:
  755. return mtype;
  756. }
  757. static enum dev_type knl_get_width(struct sbridge_pvt *pvt, u32 mtr)
  758. {
  759. /* for KNL value is fixed */
  760. return DEV_X16;
  761. }
  762. static enum dev_type sbridge_get_width(struct sbridge_pvt *pvt, u32 mtr)
  763. {
  764. /* there's no way to figure out */
  765. return DEV_UNKNOWN;
  766. }
  767. static enum dev_type __ibridge_get_width(u32 mtr)
  768. {
  769. enum dev_type type;
  770. switch (mtr) {
  771. case 3:
  772. type = DEV_UNKNOWN;
  773. break;
  774. case 2:
  775. type = DEV_X16;
  776. break;
  777. case 1:
  778. type = DEV_X8;
  779. break;
  780. case 0:
  781. type = DEV_X4;
  782. break;
  783. }
  784. return type;
  785. }
  786. static enum dev_type ibridge_get_width(struct sbridge_pvt *pvt, u32 mtr)
  787. {
  788. /*
  789. * ddr3_width on the documentation but also valid for DDR4 on
  790. * Haswell
  791. */
  792. return __ibridge_get_width(GET_BITFIELD(mtr, 7, 8));
  793. }
  794. static enum dev_type broadwell_get_width(struct sbridge_pvt *pvt, u32 mtr)
  795. {
  796. /* ddr3_width on the documentation but also valid for DDR4 */
  797. return __ibridge_get_width(GET_BITFIELD(mtr, 8, 9));
  798. }
  799. static enum mem_type knl_get_memory_type(struct sbridge_pvt *pvt)
  800. {
  801. /* DDR4 RDIMMS and LRDIMMS are supported */
  802. return MEM_RDDR4;
  803. }
  804. static u8 get_node_id(struct sbridge_pvt *pvt)
  805. {
  806. u32 reg;
  807. pci_read_config_dword(pvt->pci_br0, SAD_CONTROL, &reg);
  808. return GET_BITFIELD(reg, 0, 2);
  809. }
  810. static u8 haswell_get_node_id(struct sbridge_pvt *pvt)
  811. {
  812. u32 reg;
  813. pci_read_config_dword(pvt->pci_sad1, SAD_CONTROL, &reg);
  814. return GET_BITFIELD(reg, 0, 3);
  815. }
  816. static u8 knl_get_node_id(struct sbridge_pvt *pvt)
  817. {
  818. u32 reg;
  819. pci_read_config_dword(pvt->pci_sad1, SAD_CONTROL, &reg);
  820. return GET_BITFIELD(reg, 0, 2);
  821. }
  822. static u64 haswell_get_tolm(struct sbridge_pvt *pvt)
  823. {
  824. u32 reg;
  825. pci_read_config_dword(pvt->info.pci_vtd, HASWELL_TOLM, &reg);
  826. return (GET_BITFIELD(reg, 26, 31) << 26) | 0x3ffffff;
  827. }
  828. static u64 haswell_get_tohm(struct sbridge_pvt *pvt)
  829. {
  830. u64 rc;
  831. u32 reg;
  832. pci_read_config_dword(pvt->info.pci_vtd, HASWELL_TOHM_0, &reg);
  833. rc = GET_BITFIELD(reg, 26, 31);
  834. pci_read_config_dword(pvt->info.pci_vtd, HASWELL_TOHM_1, &reg);
  835. rc = ((reg << 6) | rc) << 26;
  836. return rc | 0x1ffffff;
  837. }
  838. static u64 knl_get_tolm(struct sbridge_pvt *pvt)
  839. {
  840. u32 reg;
  841. pci_read_config_dword(pvt->knl.pci_mc_info, KNL_TOLM, &reg);
  842. return (GET_BITFIELD(reg, 26, 31) << 26) | 0x3ffffff;
  843. }
  844. static u64 knl_get_tohm(struct sbridge_pvt *pvt)
  845. {
  846. u64 rc;
  847. u32 reg_lo, reg_hi;
  848. pci_read_config_dword(pvt->knl.pci_mc_info, KNL_TOHM_0, &reg_lo);
  849. pci_read_config_dword(pvt->knl.pci_mc_info, KNL_TOHM_1, &reg_hi);
  850. rc = ((u64)reg_hi << 32) | reg_lo;
  851. return rc | 0x3ffffff;
  852. }
  853. static u64 haswell_rir_limit(u32 reg)
  854. {
  855. return (((u64)GET_BITFIELD(reg, 1, 11) + 1) << 29) - 1;
  856. }
  857. static inline u8 sad_pkg_socket(u8 pkg)
  858. {
  859. /* on Ivy Bridge, nodeID is SASS, where A is HA and S is node id */
  860. return ((pkg >> 3) << 2) | (pkg & 0x3);
  861. }
  862. static inline u8 sad_pkg_ha(u8 pkg)
  863. {
  864. return (pkg >> 2) & 0x1;
  865. }
  866. static int haswell_chan_hash(int idx, u64 addr)
  867. {
  868. int i;
  869. /*
  870. * XOR even bits from 12:26 to bit0 of idx,
  871. * odd bits from 13:27 to bit1
  872. */
  873. for (i = 12; i < 28; i += 2)
  874. idx ^= (addr >> i) & 3;
  875. return idx;
  876. }
  877. /****************************************************************************
  878. Memory check routines
  879. ****************************************************************************/
  880. static struct pci_dev *get_pdev_same_bus(u8 bus, u32 id)
  881. {
  882. struct pci_dev *pdev = NULL;
  883. do {
  884. pdev = pci_get_device(PCI_VENDOR_ID_INTEL, id, pdev);
  885. if (pdev && pdev->bus->number == bus)
  886. break;
  887. } while (pdev);
  888. return pdev;
  889. }
  890. /**
  891. * check_if_ecc_is_active() - Checks if ECC is active
  892. * @bus: Device bus
  893. * @type: Memory controller type
  894. * returns: 0 in case ECC is active, -ENODEV if it can't be determined or
  895. * disabled
  896. */
  897. static int check_if_ecc_is_active(const u8 bus, enum type type)
  898. {
  899. struct pci_dev *pdev = NULL;
  900. u32 mcmtr, id;
  901. switch (type) {
  902. case IVY_BRIDGE:
  903. id = PCI_DEVICE_ID_INTEL_IBRIDGE_IMC_HA0_TA;
  904. break;
  905. case HASWELL:
  906. id = PCI_DEVICE_ID_INTEL_HASWELL_IMC_HA0_TA;
  907. break;
  908. case SANDY_BRIDGE:
  909. id = PCI_DEVICE_ID_INTEL_SBRIDGE_IMC_TA;
  910. break;
  911. case BROADWELL:
  912. id = PCI_DEVICE_ID_INTEL_BROADWELL_IMC_HA0_TA;
  913. break;
  914. case KNIGHTS_LANDING:
  915. /*
  916. * KNL doesn't group things by bus the same way
  917. * SB/IB/Haswell does.
  918. */
  919. id = PCI_DEVICE_ID_INTEL_KNL_IMC_TA;
  920. break;
  921. default:
  922. return -ENODEV;
  923. }
  924. if (type != KNIGHTS_LANDING)
  925. pdev = get_pdev_same_bus(bus, id);
  926. else
  927. pdev = pci_get_device(PCI_VENDOR_ID_INTEL, id, 0);
  928. if (!pdev) {
  929. sbridge_printk(KERN_ERR, "Couldn't find PCI device "
  930. "%04x:%04x! on bus %02d\n",
  931. PCI_VENDOR_ID_INTEL, id, bus);
  932. return -ENODEV;
  933. }
  934. pci_read_config_dword(pdev,
  935. type == KNIGHTS_LANDING ? KNL_MCMTR : MCMTR, &mcmtr);
  936. if (!IS_ECC_ENABLED(mcmtr)) {
  937. sbridge_printk(KERN_ERR, "ECC is disabled. Aborting\n");
  938. return -ENODEV;
  939. }
  940. return 0;
  941. }
  942. /* Low bits of TAD limit, and some metadata. */
  943. static const u32 knl_tad_dram_limit_lo[] = {
  944. 0x400, 0x500, 0x600, 0x700,
  945. 0x800, 0x900, 0xa00, 0xb00,
  946. };
  947. /* Low bits of TAD offset. */
  948. static const u32 knl_tad_dram_offset_lo[] = {
  949. 0x404, 0x504, 0x604, 0x704,
  950. 0x804, 0x904, 0xa04, 0xb04,
  951. };
  952. /* High 16 bits of TAD limit and offset. */
  953. static const u32 knl_tad_dram_hi[] = {
  954. 0x408, 0x508, 0x608, 0x708,
  955. 0x808, 0x908, 0xa08, 0xb08,
  956. };
  957. /* Number of ways a tad entry is interleaved. */
  958. static const u32 knl_tad_ways[] = {
  959. 8, 6, 4, 3, 2, 1,
  960. };
  961. /*
  962. * Retrieve the n'th Target Address Decode table entry
  963. * from the memory controller's TAD table.
  964. *
  965. * @pvt: driver private data
  966. * @entry: which entry you want to retrieve
  967. * @mc: which memory controller (0 or 1)
  968. * @offset: output tad range offset
  969. * @limit: output address of first byte above tad range
  970. * @ways: output number of interleave ways
  971. *
  972. * The offset value has curious semantics. It's a sort of running total
  973. * of the sizes of all the memory regions that aren't mapped in this
  974. * tad table.
  975. */
  976. static int knl_get_tad(const struct sbridge_pvt *pvt,
  977. const int entry,
  978. const int mc,
  979. u64 *offset,
  980. u64 *limit,
  981. int *ways)
  982. {
  983. u32 reg_limit_lo, reg_offset_lo, reg_hi;
  984. struct pci_dev *pci_mc;
  985. int way_id;
  986. switch (mc) {
  987. case 0:
  988. pci_mc = pvt->knl.pci_mc0;
  989. break;
  990. case 1:
  991. pci_mc = pvt->knl.pci_mc1;
  992. break;
  993. default:
  994. WARN_ON(1);
  995. return -EINVAL;
  996. }
  997. pci_read_config_dword(pci_mc,
  998. knl_tad_dram_limit_lo[entry], &reg_limit_lo);
  999. pci_read_config_dword(pci_mc,
  1000. knl_tad_dram_offset_lo[entry], &reg_offset_lo);
  1001. pci_read_config_dword(pci_mc,
  1002. knl_tad_dram_hi[entry], &reg_hi);
  1003. /* Is this TAD entry enabled? */
  1004. if (!GET_BITFIELD(reg_limit_lo, 0, 0))
  1005. return -ENODEV;
  1006. way_id = GET_BITFIELD(reg_limit_lo, 3, 5);
  1007. if (way_id < ARRAY_SIZE(knl_tad_ways)) {
  1008. *ways = knl_tad_ways[way_id];
  1009. } else {
  1010. *ways = 0;
  1011. sbridge_printk(KERN_ERR,
  1012. "Unexpected value %d in mc_tad_limit_lo wayness field\n",
  1013. way_id);
  1014. return -ENODEV;
  1015. }
  1016. /*
  1017. * The least significant 6 bits of base and limit are truncated.
  1018. * For limit, we fill the missing bits with 1s.
  1019. */
  1020. *offset = ((u64) GET_BITFIELD(reg_offset_lo, 6, 31) << 6) |
  1021. ((u64) GET_BITFIELD(reg_hi, 0, 15) << 32);
  1022. *limit = ((u64) GET_BITFIELD(reg_limit_lo, 6, 31) << 6) | 63 |
  1023. ((u64) GET_BITFIELD(reg_hi, 16, 31) << 32);
  1024. return 0;
  1025. }
  1026. /* Determine which memory controller is responsible for a given channel. */
  1027. static int knl_channel_mc(int channel)
  1028. {
  1029. WARN_ON(channel < 0 || channel >= 6);
  1030. return channel < 3 ? 1 : 0;
  1031. }
  1032. /*
  1033. * Get the Nth entry from EDC_ROUTE_TABLE register.
  1034. * (This is the per-tile mapping of logical interleave targets to
  1035. * physical EDC modules.)
  1036. *
  1037. * entry 0: 0:2
  1038. * 1: 3:5
  1039. * 2: 6:8
  1040. * 3: 9:11
  1041. * 4: 12:14
  1042. * 5: 15:17
  1043. * 6: 18:20
  1044. * 7: 21:23
  1045. * reserved: 24:31
  1046. */
  1047. static u32 knl_get_edc_route(int entry, u32 reg)
  1048. {
  1049. WARN_ON(entry >= KNL_MAX_EDCS);
  1050. return GET_BITFIELD(reg, entry*3, (entry*3)+2);
  1051. }
  1052. /*
  1053. * Get the Nth entry from MC_ROUTE_TABLE register.
  1054. * (This is the per-tile mapping of logical interleave targets to
  1055. * physical DRAM channels modules.)
  1056. *
  1057. * entry 0: mc 0:2 channel 18:19
  1058. * 1: mc 3:5 channel 20:21
  1059. * 2: mc 6:8 channel 22:23
  1060. * 3: mc 9:11 channel 24:25
  1061. * 4: mc 12:14 channel 26:27
  1062. * 5: mc 15:17 channel 28:29
  1063. * reserved: 30:31
  1064. *
  1065. * Though we have 3 bits to identify the MC, we should only see
  1066. * the values 0 or 1.
  1067. */
  1068. static u32 knl_get_mc_route(int entry, u32 reg)
  1069. {
  1070. int mc, chan;
  1071. WARN_ON(entry >= KNL_MAX_CHANNELS);
  1072. mc = GET_BITFIELD(reg, entry*3, (entry*3)+2);
  1073. chan = GET_BITFIELD(reg, (entry*2) + 18, (entry*2) + 18 + 1);
  1074. return knl_channel_remap(mc, chan);
  1075. }
  1076. /*
  1077. * Render the EDC_ROUTE register in human-readable form.
  1078. * Output string s should be at least KNL_MAX_EDCS*2 bytes.
  1079. */
  1080. static void knl_show_edc_route(u32 reg, char *s)
  1081. {
  1082. int i;
  1083. for (i = 0; i < KNL_MAX_EDCS; i++) {
  1084. s[i*2] = knl_get_edc_route(i, reg) + '0';
  1085. s[i*2+1] = '-';
  1086. }
  1087. s[KNL_MAX_EDCS*2 - 1] = '\0';
  1088. }
  1089. /*
  1090. * Render the MC_ROUTE register in human-readable form.
  1091. * Output string s should be at least KNL_MAX_CHANNELS*2 bytes.
  1092. */
  1093. static void knl_show_mc_route(u32 reg, char *s)
  1094. {
  1095. int i;
  1096. for (i = 0; i < KNL_MAX_CHANNELS; i++) {
  1097. s[i*2] = knl_get_mc_route(i, reg) + '0';
  1098. s[i*2+1] = '-';
  1099. }
  1100. s[KNL_MAX_CHANNELS*2 - 1] = '\0';
  1101. }
  1102. #define KNL_EDC_ROUTE 0xb8
  1103. #define KNL_MC_ROUTE 0xb4
  1104. /* Is this dram rule backed by regular DRAM in flat mode? */
  1105. #define KNL_EDRAM(reg) GET_BITFIELD(reg, 29, 29)
  1106. /* Is this dram rule cached? */
  1107. #define KNL_CACHEABLE(reg) GET_BITFIELD(reg, 28, 28)
  1108. /* Is this rule backed by edc ? */
  1109. #define KNL_EDRAM_ONLY(reg) GET_BITFIELD(reg, 29, 29)
  1110. /* Is this rule backed by DRAM, cacheable in EDRAM? */
  1111. #define KNL_CACHEABLE(reg) GET_BITFIELD(reg, 28, 28)
  1112. /* Is this rule mod3? */
  1113. #define KNL_MOD3(reg) GET_BITFIELD(reg, 27, 27)
  1114. /*
  1115. * Figure out how big our RAM modules are.
  1116. *
  1117. * The DIMMMTR register in KNL doesn't tell us the size of the DIMMs, so we
  1118. * have to figure this out from the SAD rules, interleave lists, route tables,
  1119. * and TAD rules.
  1120. *
  1121. * SAD rules can have holes in them (e.g. the 3G-4G hole), so we have to
  1122. * inspect the TAD rules to figure out how large the SAD regions really are.
  1123. *
  1124. * When we know the real size of a SAD region and how many ways it's
  1125. * interleaved, we know the individual contribution of each channel to
  1126. * TAD is size/ways.
  1127. *
  1128. * Finally, we have to check whether each channel participates in each SAD
  1129. * region.
  1130. *
  1131. * Fortunately, KNL only supports one DIMM per channel, so once we know how
  1132. * much memory the channel uses, we know the DIMM is at least that large.
  1133. * (The BIOS might possibly choose not to map all available memory, in which
  1134. * case we will underreport the size of the DIMM.)
  1135. *
  1136. * In theory, we could try to determine the EDC sizes as well, but that would
  1137. * only work in flat mode, not in cache mode.
  1138. *
  1139. * @mc_sizes: Output sizes of channels (must have space for KNL_MAX_CHANNELS
  1140. * elements)
  1141. */
  1142. static int knl_get_dimm_capacity(struct sbridge_pvt *pvt, u64 *mc_sizes)
  1143. {
  1144. u64 sad_base, sad_size, sad_limit = 0;
  1145. u64 tad_base, tad_size, tad_limit, tad_deadspace, tad_livespace;
  1146. int sad_rule = 0;
  1147. int tad_rule = 0;
  1148. int intrlv_ways, tad_ways;
  1149. u32 first_pkg, pkg;
  1150. int i;
  1151. u64 sad_actual_size[2]; /* sad size accounting for holes, per mc */
  1152. u32 dram_rule, interleave_reg;
  1153. u32 mc_route_reg[KNL_MAX_CHAS];
  1154. u32 edc_route_reg[KNL_MAX_CHAS];
  1155. int edram_only;
  1156. char edc_route_string[KNL_MAX_EDCS*2];
  1157. char mc_route_string[KNL_MAX_CHANNELS*2];
  1158. int cur_reg_start;
  1159. int mc;
  1160. int channel;
  1161. int way;
  1162. int participants[KNL_MAX_CHANNELS];
  1163. int participant_count = 0;
  1164. for (i = 0; i < KNL_MAX_CHANNELS; i++)
  1165. mc_sizes[i] = 0;
  1166. /* Read the EDC route table in each CHA. */
  1167. cur_reg_start = 0;
  1168. for (i = 0; i < KNL_MAX_CHAS; i++) {
  1169. pci_read_config_dword(pvt->knl.pci_cha[i],
  1170. KNL_EDC_ROUTE, &edc_route_reg[i]);
  1171. if (i > 0 && edc_route_reg[i] != edc_route_reg[i-1]) {
  1172. knl_show_edc_route(edc_route_reg[i-1],
  1173. edc_route_string);
  1174. if (cur_reg_start == i-1)
  1175. edac_dbg(0, "edc route table for CHA %d: %s\n",
  1176. cur_reg_start, edc_route_string);
  1177. else
  1178. edac_dbg(0, "edc route table for CHA %d-%d: %s\n",
  1179. cur_reg_start, i-1, edc_route_string);
  1180. cur_reg_start = i;
  1181. }
  1182. }
  1183. knl_show_edc_route(edc_route_reg[i-1], edc_route_string);
  1184. if (cur_reg_start == i-1)
  1185. edac_dbg(0, "edc route table for CHA %d: %s\n",
  1186. cur_reg_start, edc_route_string);
  1187. else
  1188. edac_dbg(0, "edc route table for CHA %d-%d: %s\n",
  1189. cur_reg_start, i-1, edc_route_string);
  1190. /* Read the MC route table in each CHA. */
  1191. cur_reg_start = 0;
  1192. for (i = 0; i < KNL_MAX_CHAS; i++) {
  1193. pci_read_config_dword(pvt->knl.pci_cha[i],
  1194. KNL_MC_ROUTE, &mc_route_reg[i]);
  1195. if (i > 0 && mc_route_reg[i] != mc_route_reg[i-1]) {
  1196. knl_show_mc_route(mc_route_reg[i-1], mc_route_string);
  1197. if (cur_reg_start == i-1)
  1198. edac_dbg(0, "mc route table for CHA %d: %s\n",
  1199. cur_reg_start, mc_route_string);
  1200. else
  1201. edac_dbg(0, "mc route table for CHA %d-%d: %s\n",
  1202. cur_reg_start, i-1, mc_route_string);
  1203. cur_reg_start = i;
  1204. }
  1205. }
  1206. knl_show_mc_route(mc_route_reg[i-1], mc_route_string);
  1207. if (cur_reg_start == i-1)
  1208. edac_dbg(0, "mc route table for CHA %d: %s\n",
  1209. cur_reg_start, mc_route_string);
  1210. else
  1211. edac_dbg(0, "mc route table for CHA %d-%d: %s\n",
  1212. cur_reg_start, i-1, mc_route_string);
  1213. /* Process DRAM rules */
  1214. for (sad_rule = 0; sad_rule < pvt->info.max_sad; sad_rule++) {
  1215. /* previous limit becomes the new base */
  1216. sad_base = sad_limit;
  1217. pci_read_config_dword(pvt->pci_sad0,
  1218. pvt->info.dram_rule[sad_rule], &dram_rule);
  1219. if (!DRAM_RULE_ENABLE(dram_rule))
  1220. break;
  1221. edram_only = KNL_EDRAM_ONLY(dram_rule);
  1222. sad_limit = pvt->info.sad_limit(dram_rule)+1;
  1223. sad_size = sad_limit - sad_base;
  1224. pci_read_config_dword(pvt->pci_sad0,
  1225. pvt->info.interleave_list[sad_rule], &interleave_reg);
  1226. /*
  1227. * Find out how many ways this dram rule is interleaved.
  1228. * We stop when we see the first channel again.
  1229. */
  1230. first_pkg = sad_pkg(pvt->info.interleave_pkg,
  1231. interleave_reg, 0);
  1232. for (intrlv_ways = 1; intrlv_ways < 8; intrlv_ways++) {
  1233. pkg = sad_pkg(pvt->info.interleave_pkg,
  1234. interleave_reg, intrlv_ways);
  1235. if ((pkg & 0x8) == 0) {
  1236. /*
  1237. * 0 bit means memory is non-local,
  1238. * which KNL doesn't support
  1239. */
  1240. edac_dbg(0, "Unexpected interleave target %d\n",
  1241. pkg);
  1242. return -1;
  1243. }
  1244. if (pkg == first_pkg)
  1245. break;
  1246. }
  1247. if (KNL_MOD3(dram_rule))
  1248. intrlv_ways *= 3;
  1249. edac_dbg(3, "dram rule %d (base 0x%llx, limit 0x%llx), %d way interleave%s\n",
  1250. sad_rule,
  1251. sad_base,
  1252. sad_limit,
  1253. intrlv_ways,
  1254. edram_only ? ", EDRAM" : "");
  1255. /*
  1256. * Find out how big the SAD region really is by iterating
  1257. * over TAD tables (SAD regions may contain holes).
  1258. * Each memory controller might have a different TAD table, so
  1259. * we have to look at both.
  1260. *
  1261. * Livespace is the memory that's mapped in this TAD table,
  1262. * deadspace is the holes (this could be the MMIO hole, or it
  1263. * could be memory that's mapped by the other TAD table but
  1264. * not this one).
  1265. */
  1266. for (mc = 0; mc < 2; mc++) {
  1267. sad_actual_size[mc] = 0;
  1268. tad_livespace = 0;
  1269. for (tad_rule = 0;
  1270. tad_rule < ARRAY_SIZE(
  1271. knl_tad_dram_limit_lo);
  1272. tad_rule++) {
  1273. if (knl_get_tad(pvt,
  1274. tad_rule,
  1275. mc,
  1276. &tad_deadspace,
  1277. &tad_limit,
  1278. &tad_ways))
  1279. break;
  1280. tad_size = (tad_limit+1) -
  1281. (tad_livespace + tad_deadspace);
  1282. tad_livespace += tad_size;
  1283. tad_base = (tad_limit+1) - tad_size;
  1284. if (tad_base < sad_base) {
  1285. if (tad_limit > sad_base)
  1286. edac_dbg(0, "TAD region overlaps lower SAD boundary -- TAD tables may be configured incorrectly.\n");
  1287. } else if (tad_base < sad_limit) {
  1288. if (tad_limit+1 > sad_limit) {
  1289. edac_dbg(0, "TAD region overlaps upper SAD boundary -- TAD tables may be configured incorrectly.\n");
  1290. } else {
  1291. /* TAD region is completely inside SAD region */
  1292. edac_dbg(3, "TAD region %d 0x%llx - 0x%llx (%lld bytes) table%d\n",
  1293. tad_rule, tad_base,
  1294. tad_limit, tad_size,
  1295. mc);
  1296. sad_actual_size[mc] += tad_size;
  1297. }
  1298. }
  1299. tad_base = tad_limit+1;
  1300. }
  1301. }
  1302. for (mc = 0; mc < 2; mc++) {
  1303. edac_dbg(3, " total TAD DRAM footprint in table%d : 0x%llx (%lld bytes)\n",
  1304. mc, sad_actual_size[mc], sad_actual_size[mc]);
  1305. }
  1306. /* Ignore EDRAM rule */
  1307. if (edram_only)
  1308. continue;
  1309. /* Figure out which channels participate in interleave. */
  1310. for (channel = 0; channel < KNL_MAX_CHANNELS; channel++)
  1311. participants[channel] = 0;
  1312. /* For each channel, does at least one CHA have
  1313. * this channel mapped to the given target?
  1314. */
  1315. for (channel = 0; channel < KNL_MAX_CHANNELS; channel++) {
  1316. for (way = 0; way < intrlv_ways; way++) {
  1317. int target;
  1318. int cha;
  1319. if (KNL_MOD3(dram_rule))
  1320. target = way;
  1321. else
  1322. target = 0x7 & sad_pkg(
  1323. pvt->info.interleave_pkg, interleave_reg, way);
  1324. for (cha = 0; cha < KNL_MAX_CHAS; cha++) {
  1325. if (knl_get_mc_route(target,
  1326. mc_route_reg[cha]) == channel
  1327. && !participants[channel]) {
  1328. participant_count++;
  1329. participants[channel] = 1;
  1330. break;
  1331. }
  1332. }
  1333. }
  1334. }
  1335. if (participant_count != intrlv_ways)
  1336. edac_dbg(0, "participant_count (%d) != interleave_ways (%d): DIMM size may be incorrect\n",
  1337. participant_count, intrlv_ways);
  1338. for (channel = 0; channel < KNL_MAX_CHANNELS; channel++) {
  1339. mc = knl_channel_mc(channel);
  1340. if (participants[channel]) {
  1341. edac_dbg(4, "mc channel %d contributes %lld bytes via sad entry %d\n",
  1342. channel,
  1343. sad_actual_size[mc]/intrlv_ways,
  1344. sad_rule);
  1345. mc_sizes[channel] +=
  1346. sad_actual_size[mc]/intrlv_ways;
  1347. }
  1348. }
  1349. }
  1350. return 0;
  1351. }
  1352. static int get_dimm_config(struct mem_ctl_info *mci)
  1353. {
  1354. struct sbridge_pvt *pvt = mci->pvt_info;
  1355. struct dimm_info *dimm;
  1356. unsigned i, j, banks, ranks, rows, cols, npages;
  1357. u64 size;
  1358. u32 reg;
  1359. enum edac_type mode;
  1360. enum mem_type mtype;
  1361. int channels = pvt->info.type == KNIGHTS_LANDING ?
  1362. KNL_MAX_CHANNELS : NUM_CHANNELS;
  1363. u64 knl_mc_sizes[KNL_MAX_CHANNELS];
  1364. if (pvt->info.type == HASWELL || pvt->info.type == BROADWELL) {
  1365. pci_read_config_dword(pvt->pci_ha0, HASWELL_HASYSDEFEATURE2, &reg);
  1366. pvt->is_chan_hash = GET_BITFIELD(reg, 21, 21);
  1367. }
  1368. if (pvt->info.type == HASWELL || pvt->info.type == BROADWELL ||
  1369. pvt->info.type == KNIGHTS_LANDING)
  1370. pci_read_config_dword(pvt->pci_sad1, SAD_TARGET, &reg);
  1371. else
  1372. pci_read_config_dword(pvt->pci_br0, SAD_TARGET, &reg);
  1373. if (pvt->info.type == KNIGHTS_LANDING)
  1374. pvt->sbridge_dev->source_id = SOURCE_ID_KNL(reg);
  1375. else
  1376. pvt->sbridge_dev->source_id = SOURCE_ID(reg);
  1377. pvt->sbridge_dev->node_id = pvt->info.get_node_id(pvt);
  1378. edac_dbg(0, "mc#%d: Node ID: %d, source ID: %d\n",
  1379. pvt->sbridge_dev->mc,
  1380. pvt->sbridge_dev->node_id,
  1381. pvt->sbridge_dev->source_id);
  1382. /* KNL doesn't support mirroring or lockstep,
  1383. * and is always closed page
  1384. */
  1385. if (pvt->info.type == KNIGHTS_LANDING) {
  1386. mode = EDAC_S4ECD4ED;
  1387. pvt->is_mirrored = false;
  1388. if (knl_get_dimm_capacity(pvt, knl_mc_sizes) != 0)
  1389. return -1;
  1390. } else {
  1391. pci_read_config_dword(pvt->pci_ras, RASENABLES, &reg);
  1392. if (IS_MIRROR_ENABLED(reg)) {
  1393. edac_dbg(0, "Memory mirror is enabled\n");
  1394. pvt->is_mirrored = true;
  1395. } else {
  1396. edac_dbg(0, "Memory mirror is disabled\n");
  1397. pvt->is_mirrored = false;
  1398. }
  1399. pci_read_config_dword(pvt->pci_ta, MCMTR, &pvt->info.mcmtr);
  1400. if (IS_LOCKSTEP_ENABLED(pvt->info.mcmtr)) {
  1401. edac_dbg(0, "Lockstep is enabled\n");
  1402. mode = EDAC_S8ECD8ED;
  1403. pvt->is_lockstep = true;
  1404. } else {
  1405. edac_dbg(0, "Lockstep is disabled\n");
  1406. mode = EDAC_S4ECD4ED;
  1407. pvt->is_lockstep = false;
  1408. }
  1409. if (IS_CLOSE_PG(pvt->info.mcmtr)) {
  1410. edac_dbg(0, "address map is on closed page mode\n");
  1411. pvt->is_close_pg = true;
  1412. } else {
  1413. edac_dbg(0, "address map is on open page mode\n");
  1414. pvt->is_close_pg = false;
  1415. }
  1416. }
  1417. mtype = pvt->info.get_memory_type(pvt);
  1418. if (mtype == MEM_RDDR3 || mtype == MEM_RDDR4)
  1419. edac_dbg(0, "Memory is registered\n");
  1420. else if (mtype == MEM_UNKNOWN)
  1421. edac_dbg(0, "Cannot determine memory type\n");
  1422. else
  1423. edac_dbg(0, "Memory is unregistered\n");
  1424. if (mtype == MEM_DDR4 || mtype == MEM_RDDR4)
  1425. banks = 16;
  1426. else
  1427. banks = 8;
  1428. for (i = 0; i < channels; i++) {
  1429. u32 mtr;
  1430. int max_dimms_per_channel;
  1431. if (pvt->info.type == KNIGHTS_LANDING) {
  1432. max_dimms_per_channel = 1;
  1433. if (!pvt->knl.pci_channel[i])
  1434. continue;
  1435. } else {
  1436. max_dimms_per_channel = ARRAY_SIZE(mtr_regs);
  1437. if (!pvt->pci_tad[i])
  1438. continue;
  1439. }
  1440. for (j = 0; j < max_dimms_per_channel; j++) {
  1441. dimm = EDAC_DIMM_PTR(mci->layers, mci->dimms, mci->n_layers,
  1442. i, j, 0);
  1443. if (pvt->info.type == KNIGHTS_LANDING) {
  1444. pci_read_config_dword(pvt->knl.pci_channel[i],
  1445. knl_mtr_reg, &mtr);
  1446. } else {
  1447. pci_read_config_dword(pvt->pci_tad[i],
  1448. mtr_regs[j], &mtr);
  1449. }
  1450. edac_dbg(4, "Channel #%d MTR%d = %x\n", i, j, mtr);
  1451. if (IS_DIMM_PRESENT(mtr)) {
  1452. pvt->channel[i].dimms++;
  1453. ranks = numrank(pvt->info.type, mtr);
  1454. if (pvt->info.type == KNIGHTS_LANDING) {
  1455. /* For DDR4, this is fixed. */
  1456. cols = 1 << 10;
  1457. rows = knl_mc_sizes[i] /
  1458. ((u64) cols * ranks * banks * 8);
  1459. } else {
  1460. rows = numrow(mtr);
  1461. cols = numcol(mtr);
  1462. }
  1463. size = ((u64)rows * cols * banks * ranks) >> (20 - 3);
  1464. npages = MiB_TO_PAGES(size);
  1465. edac_dbg(0, "mc#%d: ha %d channel %d, dimm %d, %lld Mb (%d pages) bank: %d, rank: %d, row: %#x, col: %#x\n",
  1466. pvt->sbridge_dev->mc, i/4, i%4, j,
  1467. size, npages,
  1468. banks, ranks, rows, cols);
  1469. dimm->nr_pages = npages;
  1470. dimm->grain = 32;
  1471. dimm->dtype = pvt->info.get_width(pvt, mtr);
  1472. dimm->mtype = mtype;
  1473. dimm->edac_mode = mode;
  1474. snprintf(dimm->label, sizeof(dimm->label),
  1475. "CPU_SrcID#%u_Ha#%u_Chan#%u_DIMM#%u",
  1476. pvt->sbridge_dev->source_id, i/4, i%4, j);
  1477. }
  1478. }
  1479. }
  1480. return 0;
  1481. }
  1482. static void get_memory_layout(const struct mem_ctl_info *mci)
  1483. {
  1484. struct sbridge_pvt *pvt = mci->pvt_info;
  1485. int i, j, k, n_sads, n_tads, sad_interl;
  1486. u32 reg;
  1487. u64 limit, prv = 0;
  1488. u64 tmp_mb;
  1489. u32 gb, mb;
  1490. u32 rir_way;
  1491. /*
  1492. * Step 1) Get TOLM/TOHM ranges
  1493. */
  1494. pvt->tolm = pvt->info.get_tolm(pvt);
  1495. tmp_mb = (1 + pvt->tolm) >> 20;
  1496. gb = div_u64_rem(tmp_mb, 1024, &mb);
  1497. edac_dbg(0, "TOLM: %u.%03u GB (0x%016Lx)\n",
  1498. gb, (mb*1000)/1024, (u64)pvt->tolm);
  1499. /* Address range is already 45:25 */
  1500. pvt->tohm = pvt->info.get_tohm(pvt);
  1501. tmp_mb = (1 + pvt->tohm) >> 20;
  1502. gb = div_u64_rem(tmp_mb, 1024, &mb);
  1503. edac_dbg(0, "TOHM: %u.%03u GB (0x%016Lx)\n",
  1504. gb, (mb*1000)/1024, (u64)pvt->tohm);
  1505. /*
  1506. * Step 2) Get SAD range and SAD Interleave list
  1507. * TAD registers contain the interleave wayness. However, it
  1508. * seems simpler to just discover it indirectly, with the
  1509. * algorithm bellow.
  1510. */
  1511. prv = 0;
  1512. for (n_sads = 0; n_sads < pvt->info.max_sad; n_sads++) {
  1513. /* SAD_LIMIT Address range is 45:26 */
  1514. pci_read_config_dword(pvt->pci_sad0, pvt->info.dram_rule[n_sads],
  1515. &reg);
  1516. limit = pvt->info.sad_limit(reg);
  1517. if (!DRAM_RULE_ENABLE(reg))
  1518. continue;
  1519. if (limit <= prv)
  1520. break;
  1521. tmp_mb = (limit + 1) >> 20;
  1522. gb = div_u64_rem(tmp_mb, 1024, &mb);
  1523. edac_dbg(0, "SAD#%d %s up to %u.%03u GB (0x%016Lx) Interleave: %s reg=0x%08x\n",
  1524. n_sads,
  1525. show_dram_attr(pvt->info.dram_attr(reg)),
  1526. gb, (mb*1000)/1024,
  1527. ((u64)tmp_mb) << 20L,
  1528. pvt->info.show_interleave_mode(reg),
  1529. reg);
  1530. prv = limit;
  1531. pci_read_config_dword(pvt->pci_sad0, pvt->info.interleave_list[n_sads],
  1532. &reg);
  1533. sad_interl = sad_pkg(pvt->info.interleave_pkg, reg, 0);
  1534. for (j = 0; j < 8; j++) {
  1535. u32 pkg = sad_pkg(pvt->info.interleave_pkg, reg, j);
  1536. if (j > 0 && sad_interl == pkg)
  1537. break;
  1538. edac_dbg(0, "SAD#%d, interleave #%d: %d\n",
  1539. n_sads, j, pkg);
  1540. }
  1541. }
  1542. if (pvt->info.type == KNIGHTS_LANDING)
  1543. return;
  1544. /*
  1545. * Step 3) Get TAD range
  1546. */
  1547. prv = 0;
  1548. for (n_tads = 0; n_tads < MAX_TAD; n_tads++) {
  1549. pci_read_config_dword(pvt->pci_ha0, tad_dram_rule[n_tads],
  1550. &reg);
  1551. limit = TAD_LIMIT(reg);
  1552. if (limit <= prv)
  1553. break;
  1554. tmp_mb = (limit + 1) >> 20;
  1555. gb = div_u64_rem(tmp_mb, 1024, &mb);
  1556. edac_dbg(0, "TAD#%d: up to %u.%03u GB (0x%016Lx), socket interleave %d, memory interleave %d, TGT: %d, %d, %d, %d, reg=0x%08x\n",
  1557. n_tads, gb, (mb*1000)/1024,
  1558. ((u64)tmp_mb) << 20L,
  1559. (u32)(1 << TAD_SOCK(reg)),
  1560. (u32)TAD_CH(reg) + 1,
  1561. (u32)TAD_TGT0(reg),
  1562. (u32)TAD_TGT1(reg),
  1563. (u32)TAD_TGT2(reg),
  1564. (u32)TAD_TGT3(reg),
  1565. reg);
  1566. prv = limit;
  1567. }
  1568. /*
  1569. * Step 4) Get TAD offsets, per each channel
  1570. */
  1571. for (i = 0; i < NUM_CHANNELS; i++) {
  1572. if (!pvt->channel[i].dimms)
  1573. continue;
  1574. for (j = 0; j < n_tads; j++) {
  1575. pci_read_config_dword(pvt->pci_tad[i],
  1576. tad_ch_nilv_offset[j],
  1577. &reg);
  1578. tmp_mb = TAD_OFFSET(reg) >> 20;
  1579. gb = div_u64_rem(tmp_mb, 1024, &mb);
  1580. edac_dbg(0, "TAD CH#%d, offset #%d: %u.%03u GB (0x%016Lx), reg=0x%08x\n",
  1581. i, j,
  1582. gb, (mb*1000)/1024,
  1583. ((u64)tmp_mb) << 20L,
  1584. reg);
  1585. }
  1586. }
  1587. /*
  1588. * Step 6) Get RIR Wayness/Limit, per each channel
  1589. */
  1590. for (i = 0; i < NUM_CHANNELS; i++) {
  1591. if (!pvt->channel[i].dimms)
  1592. continue;
  1593. for (j = 0; j < MAX_RIR_RANGES; j++) {
  1594. pci_read_config_dword(pvt->pci_tad[i],
  1595. rir_way_limit[j],
  1596. &reg);
  1597. if (!IS_RIR_VALID(reg))
  1598. continue;
  1599. tmp_mb = pvt->info.rir_limit(reg) >> 20;
  1600. rir_way = 1 << RIR_WAY(reg);
  1601. gb = div_u64_rem(tmp_mb, 1024, &mb);
  1602. edac_dbg(0, "CH#%d RIR#%d, limit: %u.%03u GB (0x%016Lx), way: %d, reg=0x%08x\n",
  1603. i, j,
  1604. gb, (mb*1000)/1024,
  1605. ((u64)tmp_mb) << 20L,
  1606. rir_way,
  1607. reg);
  1608. for (k = 0; k < rir_way; k++) {
  1609. pci_read_config_dword(pvt->pci_tad[i],
  1610. rir_offset[j][k],
  1611. &reg);
  1612. tmp_mb = RIR_OFFSET(pvt->info.type, reg) << 6;
  1613. gb = div_u64_rem(tmp_mb, 1024, &mb);
  1614. edac_dbg(0, "CH#%d RIR#%d INTL#%d, offset %u.%03u GB (0x%016Lx), tgt: %d, reg=0x%08x\n",
  1615. i, j, k,
  1616. gb, (mb*1000)/1024,
  1617. ((u64)tmp_mb) << 20L,
  1618. (u32)RIR_RNK_TGT(pvt->info.type, reg),
  1619. reg);
  1620. }
  1621. }
  1622. }
  1623. }
  1624. static struct mem_ctl_info *get_mci_for_node_id(u8 node_id)
  1625. {
  1626. struct sbridge_dev *sbridge_dev;
  1627. list_for_each_entry(sbridge_dev, &sbridge_edac_list, list) {
  1628. if (sbridge_dev->node_id == node_id)
  1629. return sbridge_dev->mci;
  1630. }
  1631. return NULL;
  1632. }
  1633. static int get_memory_error_data(struct mem_ctl_info *mci,
  1634. u64 addr,
  1635. u8 *socket, u8 *ha,
  1636. long *channel_mask,
  1637. u8 *rank,
  1638. char **area_type, char *msg)
  1639. {
  1640. struct mem_ctl_info *new_mci;
  1641. struct sbridge_pvt *pvt = mci->pvt_info;
  1642. struct pci_dev *pci_ha;
  1643. int n_rir, n_sads, n_tads, sad_way, sck_xch;
  1644. int sad_interl, idx, base_ch;
  1645. int interleave_mode, shiftup = 0;
  1646. unsigned sad_interleave[pvt->info.max_interleave];
  1647. u32 reg, dram_rule;
  1648. u8 ch_way, sck_way, pkg, sad_ha = 0, ch_add = 0;
  1649. u32 tad_offset;
  1650. u32 rir_way;
  1651. u32 mb, gb;
  1652. u64 ch_addr, offset, limit = 0, prv = 0;
  1653. /*
  1654. * Step 0) Check if the address is at special memory ranges
  1655. * The check bellow is probably enough to fill all cases where
  1656. * the error is not inside a memory, except for the legacy
  1657. * range (e. g. VGA addresses). It is unlikely, however, that the
  1658. * memory controller would generate an error on that range.
  1659. */
  1660. if ((addr > (u64) pvt->tolm) && (addr < (1LL << 32))) {
  1661. sprintf(msg, "Error at TOLM area, on addr 0x%08Lx", addr);
  1662. return -EINVAL;
  1663. }
  1664. if (addr >= (u64)pvt->tohm) {
  1665. sprintf(msg, "Error at MMIOH area, on addr 0x%016Lx", addr);
  1666. return -EINVAL;
  1667. }
  1668. /*
  1669. * Step 1) Get socket
  1670. */
  1671. for (n_sads = 0; n_sads < pvt->info.max_sad; n_sads++) {
  1672. pci_read_config_dword(pvt->pci_sad0, pvt->info.dram_rule[n_sads],
  1673. &reg);
  1674. if (!DRAM_RULE_ENABLE(reg))
  1675. continue;
  1676. limit = pvt->info.sad_limit(reg);
  1677. if (limit <= prv) {
  1678. sprintf(msg, "Can't discover the memory socket");
  1679. return -EINVAL;
  1680. }
  1681. if (addr <= limit)
  1682. break;
  1683. prv = limit;
  1684. }
  1685. if (n_sads == pvt->info.max_sad) {
  1686. sprintf(msg, "Can't discover the memory socket");
  1687. return -EINVAL;
  1688. }
  1689. dram_rule = reg;
  1690. *area_type = show_dram_attr(pvt->info.dram_attr(dram_rule));
  1691. interleave_mode = pvt->info.interleave_mode(dram_rule);
  1692. pci_read_config_dword(pvt->pci_sad0, pvt->info.interleave_list[n_sads],
  1693. &reg);
  1694. if (pvt->info.type == SANDY_BRIDGE) {
  1695. sad_interl = sad_pkg(pvt->info.interleave_pkg, reg, 0);
  1696. for (sad_way = 0; sad_way < 8; sad_way++) {
  1697. u32 pkg = sad_pkg(pvt->info.interleave_pkg, reg, sad_way);
  1698. if (sad_way > 0 && sad_interl == pkg)
  1699. break;
  1700. sad_interleave[sad_way] = pkg;
  1701. edac_dbg(0, "SAD interleave #%d: %d\n",
  1702. sad_way, sad_interleave[sad_way]);
  1703. }
  1704. edac_dbg(0, "mc#%d: Error detected on SAD#%d: address 0x%016Lx < 0x%016Lx, Interleave [%d:6]%s\n",
  1705. pvt->sbridge_dev->mc,
  1706. n_sads,
  1707. addr,
  1708. limit,
  1709. sad_way + 7,
  1710. !interleave_mode ? "" : "XOR[18:16]");
  1711. if (interleave_mode)
  1712. idx = ((addr >> 6) ^ (addr >> 16)) & 7;
  1713. else
  1714. idx = (addr >> 6) & 7;
  1715. switch (sad_way) {
  1716. case 1:
  1717. idx = 0;
  1718. break;
  1719. case 2:
  1720. idx = idx & 1;
  1721. break;
  1722. case 4:
  1723. idx = idx & 3;
  1724. break;
  1725. case 8:
  1726. break;
  1727. default:
  1728. sprintf(msg, "Can't discover socket interleave");
  1729. return -EINVAL;
  1730. }
  1731. *socket = sad_interleave[idx];
  1732. edac_dbg(0, "SAD interleave index: %d (wayness %d) = CPU socket %d\n",
  1733. idx, sad_way, *socket);
  1734. } else if (pvt->info.type == HASWELL || pvt->info.type == BROADWELL) {
  1735. int bits, a7mode = A7MODE(dram_rule);
  1736. if (a7mode) {
  1737. /* A7 mode swaps P9 with P6 */
  1738. bits = GET_BITFIELD(addr, 7, 8) << 1;
  1739. bits |= GET_BITFIELD(addr, 9, 9);
  1740. } else
  1741. bits = GET_BITFIELD(addr, 6, 8);
  1742. if (interleave_mode == 0) {
  1743. /* interleave mode will XOR {8,7,6} with {18,17,16} */
  1744. idx = GET_BITFIELD(addr, 16, 18);
  1745. idx ^= bits;
  1746. } else
  1747. idx = bits;
  1748. pkg = sad_pkg(pvt->info.interleave_pkg, reg, idx);
  1749. *socket = sad_pkg_socket(pkg);
  1750. sad_ha = sad_pkg_ha(pkg);
  1751. if (sad_ha)
  1752. ch_add = 4;
  1753. if (a7mode) {
  1754. /* MCChanShiftUpEnable */
  1755. pci_read_config_dword(pvt->pci_ha0,
  1756. HASWELL_HASYSDEFEATURE2, &reg);
  1757. shiftup = GET_BITFIELD(reg, 22, 22);
  1758. }
  1759. edac_dbg(0, "SAD interleave package: %d = CPU socket %d, HA %i, shiftup: %i\n",
  1760. idx, *socket, sad_ha, shiftup);
  1761. } else {
  1762. /* Ivy Bridge's SAD mode doesn't support XOR interleave mode */
  1763. idx = (addr >> 6) & 7;
  1764. pkg = sad_pkg(pvt->info.interleave_pkg, reg, idx);
  1765. *socket = sad_pkg_socket(pkg);
  1766. sad_ha = sad_pkg_ha(pkg);
  1767. if (sad_ha)
  1768. ch_add = 4;
  1769. edac_dbg(0, "SAD interleave package: %d = CPU socket %d, HA %d\n",
  1770. idx, *socket, sad_ha);
  1771. }
  1772. *ha = sad_ha;
  1773. /*
  1774. * Move to the proper node structure, in order to access the
  1775. * right PCI registers
  1776. */
  1777. new_mci = get_mci_for_node_id(*socket);
  1778. if (!new_mci) {
  1779. sprintf(msg, "Struct for socket #%u wasn't initialized",
  1780. *socket);
  1781. return -EINVAL;
  1782. }
  1783. mci = new_mci;
  1784. pvt = mci->pvt_info;
  1785. /*
  1786. * Step 2) Get memory channel
  1787. */
  1788. prv = 0;
  1789. if (pvt->info.type == SANDY_BRIDGE)
  1790. pci_ha = pvt->pci_ha0;
  1791. else {
  1792. if (sad_ha)
  1793. pci_ha = pvt->pci_ha1;
  1794. else
  1795. pci_ha = pvt->pci_ha0;
  1796. }
  1797. for (n_tads = 0; n_tads < MAX_TAD; n_tads++) {
  1798. pci_read_config_dword(pci_ha, tad_dram_rule[n_tads], &reg);
  1799. limit = TAD_LIMIT(reg);
  1800. if (limit <= prv) {
  1801. sprintf(msg, "Can't discover the memory channel");
  1802. return -EINVAL;
  1803. }
  1804. if (addr <= limit)
  1805. break;
  1806. prv = limit;
  1807. }
  1808. if (n_tads == MAX_TAD) {
  1809. sprintf(msg, "Can't discover the memory channel");
  1810. return -EINVAL;
  1811. }
  1812. ch_way = TAD_CH(reg) + 1;
  1813. sck_way = TAD_SOCK(reg);
  1814. if (ch_way == 3)
  1815. idx = addr >> 6;
  1816. else {
  1817. idx = (addr >> (6 + sck_way + shiftup)) & 0x3;
  1818. if (pvt->is_chan_hash)
  1819. idx = haswell_chan_hash(idx, addr);
  1820. }
  1821. idx = idx % ch_way;
  1822. /*
  1823. * FIXME: Shouldn't we use CHN_IDX_OFFSET() here, when ch_way == 3 ???
  1824. */
  1825. switch (idx) {
  1826. case 0:
  1827. base_ch = TAD_TGT0(reg);
  1828. break;
  1829. case 1:
  1830. base_ch = TAD_TGT1(reg);
  1831. break;
  1832. case 2:
  1833. base_ch = TAD_TGT2(reg);
  1834. break;
  1835. case 3:
  1836. base_ch = TAD_TGT3(reg);
  1837. break;
  1838. default:
  1839. sprintf(msg, "Can't discover the TAD target");
  1840. return -EINVAL;
  1841. }
  1842. *channel_mask = 1 << base_ch;
  1843. pci_read_config_dword(pvt->pci_tad[ch_add + base_ch],
  1844. tad_ch_nilv_offset[n_tads],
  1845. &tad_offset);
  1846. if (pvt->is_mirrored) {
  1847. *channel_mask |= 1 << ((base_ch + 2) % 4);
  1848. switch(ch_way) {
  1849. case 2:
  1850. case 4:
  1851. sck_xch = (1 << sck_way) * (ch_way >> 1);
  1852. break;
  1853. default:
  1854. sprintf(msg, "Invalid mirror set. Can't decode addr");
  1855. return -EINVAL;
  1856. }
  1857. } else
  1858. sck_xch = (1 << sck_way) * ch_way;
  1859. if (pvt->is_lockstep)
  1860. *channel_mask |= 1 << ((base_ch + 1) % 4);
  1861. offset = TAD_OFFSET(tad_offset);
  1862. edac_dbg(0, "TAD#%d: address 0x%016Lx < 0x%016Lx, socket interleave %d, channel interleave %d (offset 0x%08Lx), index %d, base ch: %d, ch mask: 0x%02lx\n",
  1863. n_tads,
  1864. addr,
  1865. limit,
  1866. sck_way,
  1867. ch_way,
  1868. offset,
  1869. idx,
  1870. base_ch,
  1871. *channel_mask);
  1872. /* Calculate channel address */
  1873. /* Remove the TAD offset */
  1874. if (offset > addr) {
  1875. sprintf(msg, "Can't calculate ch addr: TAD offset 0x%08Lx is too high for addr 0x%08Lx!",
  1876. offset, addr);
  1877. return -EINVAL;
  1878. }
  1879. ch_addr = addr - offset;
  1880. ch_addr >>= (6 + shiftup);
  1881. ch_addr /= sck_xch;
  1882. ch_addr <<= (6 + shiftup);
  1883. ch_addr |= addr & ((1 << (6 + shiftup)) - 1);
  1884. /*
  1885. * Step 3) Decode rank
  1886. */
  1887. for (n_rir = 0; n_rir < MAX_RIR_RANGES; n_rir++) {
  1888. pci_read_config_dword(pvt->pci_tad[ch_add + base_ch],
  1889. rir_way_limit[n_rir],
  1890. &reg);
  1891. if (!IS_RIR_VALID(reg))
  1892. continue;
  1893. limit = pvt->info.rir_limit(reg);
  1894. gb = div_u64_rem(limit >> 20, 1024, &mb);
  1895. edac_dbg(0, "RIR#%d, limit: %u.%03u GB (0x%016Lx), way: %d\n",
  1896. n_rir,
  1897. gb, (mb*1000)/1024,
  1898. limit,
  1899. 1 << RIR_WAY(reg));
  1900. if (ch_addr <= limit)
  1901. break;
  1902. }
  1903. if (n_rir == MAX_RIR_RANGES) {
  1904. sprintf(msg, "Can't discover the memory rank for ch addr 0x%08Lx",
  1905. ch_addr);
  1906. return -EINVAL;
  1907. }
  1908. rir_way = RIR_WAY(reg);
  1909. if (pvt->is_close_pg)
  1910. idx = (ch_addr >> 6);
  1911. else
  1912. idx = (ch_addr >> 13); /* FIXME: Datasheet says to shift by 15 */
  1913. idx %= 1 << rir_way;
  1914. pci_read_config_dword(pvt->pci_tad[ch_add + base_ch],
  1915. rir_offset[n_rir][idx],
  1916. &reg);
  1917. *rank = RIR_RNK_TGT(pvt->info.type, reg);
  1918. edac_dbg(0, "RIR#%d: channel address 0x%08Lx < 0x%08Lx, RIR interleave %d, index %d\n",
  1919. n_rir,
  1920. ch_addr,
  1921. limit,
  1922. rir_way,
  1923. idx);
  1924. return 0;
  1925. }
  1926. /****************************************************************************
  1927. Device initialization routines: put/get, init/exit
  1928. ****************************************************************************/
  1929. /*
  1930. * sbridge_put_all_devices 'put' all the devices that we have
  1931. * reserved via 'get'
  1932. */
  1933. static void sbridge_put_devices(struct sbridge_dev *sbridge_dev)
  1934. {
  1935. int i;
  1936. edac_dbg(0, "\n");
  1937. for (i = 0; i < sbridge_dev->n_devs; i++) {
  1938. struct pci_dev *pdev = sbridge_dev->pdev[i];
  1939. if (!pdev)
  1940. continue;
  1941. edac_dbg(0, "Removing dev %02x:%02x.%d\n",
  1942. pdev->bus->number,
  1943. PCI_SLOT(pdev->devfn), PCI_FUNC(pdev->devfn));
  1944. pci_dev_put(pdev);
  1945. }
  1946. }
  1947. static void sbridge_put_all_devices(void)
  1948. {
  1949. struct sbridge_dev *sbridge_dev, *tmp;
  1950. list_for_each_entry_safe(sbridge_dev, tmp, &sbridge_edac_list, list) {
  1951. sbridge_put_devices(sbridge_dev);
  1952. free_sbridge_dev(sbridge_dev);
  1953. }
  1954. }
  1955. static int sbridge_get_onedevice(struct pci_dev **prev,
  1956. u8 *num_mc,
  1957. const struct pci_id_table *table,
  1958. const unsigned devno,
  1959. const int multi_bus)
  1960. {
  1961. struct sbridge_dev *sbridge_dev;
  1962. const struct pci_id_descr *dev_descr = &table->descr[devno];
  1963. struct pci_dev *pdev = NULL;
  1964. u8 bus = 0;
  1965. sbridge_printk(KERN_DEBUG,
  1966. "Seeking for: PCI ID %04x:%04x\n",
  1967. PCI_VENDOR_ID_INTEL, dev_descr->dev_id);
  1968. pdev = pci_get_device(PCI_VENDOR_ID_INTEL,
  1969. dev_descr->dev_id, *prev);
  1970. if (!pdev) {
  1971. if (*prev) {
  1972. *prev = pdev;
  1973. return 0;
  1974. }
  1975. if (dev_descr->optional)
  1976. return 0;
  1977. /* if the HA wasn't found */
  1978. if (devno == 0)
  1979. return -ENODEV;
  1980. sbridge_printk(KERN_INFO,
  1981. "Device not found: %04x:%04x\n",
  1982. PCI_VENDOR_ID_INTEL, dev_descr->dev_id);
  1983. /* End of list, leave */
  1984. return -ENODEV;
  1985. }
  1986. bus = pdev->bus->number;
  1987. sbridge_dev = get_sbridge_dev(bus, multi_bus);
  1988. if (!sbridge_dev) {
  1989. sbridge_dev = alloc_sbridge_dev(bus, table);
  1990. if (!sbridge_dev) {
  1991. pci_dev_put(pdev);
  1992. return -ENOMEM;
  1993. }
  1994. (*num_mc)++;
  1995. }
  1996. if (sbridge_dev->pdev[devno]) {
  1997. sbridge_printk(KERN_ERR,
  1998. "Duplicated device for %04x:%04x\n",
  1999. PCI_VENDOR_ID_INTEL, dev_descr->dev_id);
  2000. pci_dev_put(pdev);
  2001. return -ENODEV;
  2002. }
  2003. sbridge_dev->pdev[devno] = pdev;
  2004. /* Be sure that the device is enabled */
  2005. if (unlikely(pci_enable_device(pdev) < 0)) {
  2006. sbridge_printk(KERN_ERR,
  2007. "Couldn't enable %04x:%04x\n",
  2008. PCI_VENDOR_ID_INTEL, dev_descr->dev_id);
  2009. return -ENODEV;
  2010. }
  2011. edac_dbg(0, "Detected %04x:%04x\n",
  2012. PCI_VENDOR_ID_INTEL, dev_descr->dev_id);
  2013. /*
  2014. * As stated on drivers/pci/search.c, the reference count for
  2015. * @from is always decremented if it is not %NULL. So, as we need
  2016. * to get all devices up to null, we need to do a get for the device
  2017. */
  2018. pci_dev_get(pdev);
  2019. *prev = pdev;
  2020. return 0;
  2021. }
  2022. /*
  2023. * sbridge_get_all_devices - Find and perform 'get' operation on the MCH's
  2024. * devices we want to reference for this driver.
  2025. * @num_mc: pointer to the memory controllers count, to be incremented in case
  2026. * of success.
  2027. * @table: model specific table
  2028. *
  2029. * returns 0 in case of success or error code
  2030. */
  2031. static int sbridge_get_all_devices(u8 *num_mc,
  2032. const struct pci_id_table *table)
  2033. {
  2034. int i, rc;
  2035. struct pci_dev *pdev = NULL;
  2036. int allow_dups = 0;
  2037. int multi_bus = 0;
  2038. if (table->type == KNIGHTS_LANDING)
  2039. allow_dups = multi_bus = 1;
  2040. while (table && table->descr) {
  2041. for (i = 0; i < table->n_devs; i++) {
  2042. if (!allow_dups || i == 0 ||
  2043. table->descr[i].dev_id !=
  2044. table->descr[i-1].dev_id) {
  2045. pdev = NULL;
  2046. }
  2047. do {
  2048. rc = sbridge_get_onedevice(&pdev, num_mc,
  2049. table, i, multi_bus);
  2050. if (rc < 0) {
  2051. if (i == 0) {
  2052. i = table->n_devs;
  2053. break;
  2054. }
  2055. sbridge_put_all_devices();
  2056. return -ENODEV;
  2057. }
  2058. } while (pdev && !allow_dups);
  2059. }
  2060. table++;
  2061. }
  2062. return 0;
  2063. }
  2064. static int sbridge_mci_bind_devs(struct mem_ctl_info *mci,
  2065. struct sbridge_dev *sbridge_dev)
  2066. {
  2067. struct sbridge_pvt *pvt = mci->pvt_info;
  2068. struct pci_dev *pdev;
  2069. u8 saw_chan_mask = 0;
  2070. int i;
  2071. for (i = 0; i < sbridge_dev->n_devs; i++) {
  2072. pdev = sbridge_dev->pdev[i];
  2073. if (!pdev)
  2074. continue;
  2075. switch (pdev->device) {
  2076. case PCI_DEVICE_ID_INTEL_SBRIDGE_SAD0:
  2077. pvt->pci_sad0 = pdev;
  2078. break;
  2079. case PCI_DEVICE_ID_INTEL_SBRIDGE_SAD1:
  2080. pvt->pci_sad1 = pdev;
  2081. break;
  2082. case PCI_DEVICE_ID_INTEL_SBRIDGE_BR:
  2083. pvt->pci_br0 = pdev;
  2084. break;
  2085. case PCI_DEVICE_ID_INTEL_SBRIDGE_IMC_HA0:
  2086. pvt->pci_ha0 = pdev;
  2087. break;
  2088. case PCI_DEVICE_ID_INTEL_SBRIDGE_IMC_TA:
  2089. pvt->pci_ta = pdev;
  2090. break;
  2091. case PCI_DEVICE_ID_INTEL_SBRIDGE_IMC_RAS:
  2092. pvt->pci_ras = pdev;
  2093. break;
  2094. case PCI_DEVICE_ID_INTEL_SBRIDGE_IMC_TAD0:
  2095. case PCI_DEVICE_ID_INTEL_SBRIDGE_IMC_TAD1:
  2096. case PCI_DEVICE_ID_INTEL_SBRIDGE_IMC_TAD2:
  2097. case PCI_DEVICE_ID_INTEL_SBRIDGE_IMC_TAD3:
  2098. {
  2099. int id = pdev->device - PCI_DEVICE_ID_INTEL_SBRIDGE_IMC_TAD0;
  2100. pvt->pci_tad[id] = pdev;
  2101. saw_chan_mask |= 1 << id;
  2102. }
  2103. break;
  2104. case PCI_DEVICE_ID_INTEL_SBRIDGE_IMC_DDRIO:
  2105. pvt->pci_ddrio = pdev;
  2106. break;
  2107. default:
  2108. goto error;
  2109. }
  2110. edac_dbg(0, "Associated PCI %02x:%02x, bus %d with dev = %p\n",
  2111. pdev->vendor, pdev->device,
  2112. sbridge_dev->bus,
  2113. pdev);
  2114. }
  2115. /* Check if everything were registered */
  2116. if (!pvt->pci_sad0 || !pvt->pci_sad1 || !pvt->pci_ha0 ||
  2117. !pvt->pci_ras || !pvt->pci_ta)
  2118. goto enodev;
  2119. if (saw_chan_mask != 0x0f)
  2120. goto enodev;
  2121. return 0;
  2122. enodev:
  2123. sbridge_printk(KERN_ERR, "Some needed devices are missing\n");
  2124. return -ENODEV;
  2125. error:
  2126. sbridge_printk(KERN_ERR, "Unexpected device %02x:%02x\n",
  2127. PCI_VENDOR_ID_INTEL, pdev->device);
  2128. return -EINVAL;
  2129. }
  2130. static int ibridge_mci_bind_devs(struct mem_ctl_info *mci,
  2131. struct sbridge_dev *sbridge_dev)
  2132. {
  2133. struct sbridge_pvt *pvt = mci->pvt_info;
  2134. struct pci_dev *pdev;
  2135. u8 saw_chan_mask = 0;
  2136. int i;
  2137. for (i = 0; i < sbridge_dev->n_devs; i++) {
  2138. pdev = sbridge_dev->pdev[i];
  2139. if (!pdev)
  2140. continue;
  2141. switch (pdev->device) {
  2142. case PCI_DEVICE_ID_INTEL_IBRIDGE_IMC_HA0:
  2143. pvt->pci_ha0 = pdev;
  2144. break;
  2145. case PCI_DEVICE_ID_INTEL_IBRIDGE_IMC_HA0_TA:
  2146. pvt->pci_ta = pdev;
  2147. case PCI_DEVICE_ID_INTEL_IBRIDGE_IMC_HA0_RAS:
  2148. pvt->pci_ras = pdev;
  2149. break;
  2150. case PCI_DEVICE_ID_INTEL_IBRIDGE_IMC_HA0_TAD0:
  2151. case PCI_DEVICE_ID_INTEL_IBRIDGE_IMC_HA0_TAD1:
  2152. case PCI_DEVICE_ID_INTEL_IBRIDGE_IMC_HA0_TAD2:
  2153. case PCI_DEVICE_ID_INTEL_IBRIDGE_IMC_HA0_TAD3:
  2154. {
  2155. int id = pdev->device - PCI_DEVICE_ID_INTEL_IBRIDGE_IMC_HA0_TAD0;
  2156. pvt->pci_tad[id] = pdev;
  2157. saw_chan_mask |= 1 << id;
  2158. }
  2159. break;
  2160. case PCI_DEVICE_ID_INTEL_IBRIDGE_IMC_2HA_DDRIO0:
  2161. pvt->pci_ddrio = pdev;
  2162. break;
  2163. case PCI_DEVICE_ID_INTEL_IBRIDGE_IMC_1HA_DDRIO0:
  2164. pvt->pci_ddrio = pdev;
  2165. break;
  2166. case PCI_DEVICE_ID_INTEL_IBRIDGE_SAD:
  2167. pvt->pci_sad0 = pdev;
  2168. break;
  2169. case PCI_DEVICE_ID_INTEL_IBRIDGE_BR0:
  2170. pvt->pci_br0 = pdev;
  2171. break;
  2172. case PCI_DEVICE_ID_INTEL_IBRIDGE_BR1:
  2173. pvt->pci_br1 = pdev;
  2174. break;
  2175. case PCI_DEVICE_ID_INTEL_IBRIDGE_IMC_HA1:
  2176. pvt->pci_ha1 = pdev;
  2177. break;
  2178. case PCI_DEVICE_ID_INTEL_IBRIDGE_IMC_HA1_TAD0:
  2179. case PCI_DEVICE_ID_INTEL_IBRIDGE_IMC_HA1_TAD1:
  2180. case PCI_DEVICE_ID_INTEL_IBRIDGE_IMC_HA1_TAD2:
  2181. case PCI_DEVICE_ID_INTEL_IBRIDGE_IMC_HA1_TAD3:
  2182. {
  2183. int id = pdev->device - PCI_DEVICE_ID_INTEL_IBRIDGE_IMC_HA1_TAD0 + 4;
  2184. pvt->pci_tad[id] = pdev;
  2185. saw_chan_mask |= 1 << id;
  2186. }
  2187. break;
  2188. default:
  2189. goto error;
  2190. }
  2191. edac_dbg(0, "Associated PCI %02x.%02d.%d with dev = %p\n",
  2192. sbridge_dev->bus,
  2193. PCI_SLOT(pdev->devfn), PCI_FUNC(pdev->devfn),
  2194. pdev);
  2195. }
  2196. /* Check if everything were registered */
  2197. if (!pvt->pci_sad0 || !pvt->pci_ha0 || !pvt->pci_br0 ||
  2198. !pvt->pci_br1 || !pvt->pci_ras || !pvt->pci_ta)
  2199. goto enodev;
  2200. if (saw_chan_mask != 0x0f && /* -EN */
  2201. saw_chan_mask != 0x33 && /* -EP */
  2202. saw_chan_mask != 0xff) /* -EX */
  2203. goto enodev;
  2204. return 0;
  2205. enodev:
  2206. sbridge_printk(KERN_ERR, "Some needed devices are missing\n");
  2207. return -ENODEV;
  2208. error:
  2209. sbridge_printk(KERN_ERR,
  2210. "Unexpected device %02x:%02x\n", PCI_VENDOR_ID_INTEL,
  2211. pdev->device);
  2212. return -EINVAL;
  2213. }
  2214. static int haswell_mci_bind_devs(struct mem_ctl_info *mci,
  2215. struct sbridge_dev *sbridge_dev)
  2216. {
  2217. struct sbridge_pvt *pvt = mci->pvt_info;
  2218. struct pci_dev *pdev;
  2219. u8 saw_chan_mask = 0;
  2220. int i;
  2221. /* there's only one device per system; not tied to any bus */
  2222. if (pvt->info.pci_vtd == NULL)
  2223. /* result will be checked later */
  2224. pvt->info.pci_vtd = pci_get_device(PCI_VENDOR_ID_INTEL,
  2225. PCI_DEVICE_ID_INTEL_HASWELL_IMC_VTD_MISC,
  2226. NULL);
  2227. for (i = 0; i < sbridge_dev->n_devs; i++) {
  2228. pdev = sbridge_dev->pdev[i];
  2229. if (!pdev)
  2230. continue;
  2231. switch (pdev->device) {
  2232. case PCI_DEVICE_ID_INTEL_HASWELL_IMC_CBO_SAD0:
  2233. pvt->pci_sad0 = pdev;
  2234. break;
  2235. case PCI_DEVICE_ID_INTEL_HASWELL_IMC_CBO_SAD1:
  2236. pvt->pci_sad1 = pdev;
  2237. break;
  2238. case PCI_DEVICE_ID_INTEL_HASWELL_IMC_HA0:
  2239. pvt->pci_ha0 = pdev;
  2240. break;
  2241. case PCI_DEVICE_ID_INTEL_HASWELL_IMC_HA0_TA:
  2242. pvt->pci_ta = pdev;
  2243. break;
  2244. case PCI_DEVICE_ID_INTEL_HASWELL_IMC_HA0_THERMAL:
  2245. pvt->pci_ras = pdev;
  2246. break;
  2247. case PCI_DEVICE_ID_INTEL_HASWELL_IMC_HA0_TAD0:
  2248. case PCI_DEVICE_ID_INTEL_HASWELL_IMC_HA0_TAD1:
  2249. case PCI_DEVICE_ID_INTEL_HASWELL_IMC_HA0_TAD2:
  2250. case PCI_DEVICE_ID_INTEL_HASWELL_IMC_HA0_TAD3:
  2251. {
  2252. int id = pdev->device - PCI_DEVICE_ID_INTEL_HASWELL_IMC_HA0_TAD0;
  2253. pvt->pci_tad[id] = pdev;
  2254. saw_chan_mask |= 1 << id;
  2255. }
  2256. break;
  2257. case PCI_DEVICE_ID_INTEL_HASWELL_IMC_HA1_TAD0:
  2258. case PCI_DEVICE_ID_INTEL_HASWELL_IMC_HA1_TAD1:
  2259. case PCI_DEVICE_ID_INTEL_HASWELL_IMC_HA1_TAD2:
  2260. case PCI_DEVICE_ID_INTEL_HASWELL_IMC_HA1_TAD3:
  2261. {
  2262. int id = pdev->device - PCI_DEVICE_ID_INTEL_HASWELL_IMC_HA1_TAD0 + 4;
  2263. pvt->pci_tad[id] = pdev;
  2264. saw_chan_mask |= 1 << id;
  2265. }
  2266. break;
  2267. case PCI_DEVICE_ID_INTEL_HASWELL_IMC_DDRIO0:
  2268. case PCI_DEVICE_ID_INTEL_HASWELL_IMC_DDRIO1:
  2269. case PCI_DEVICE_ID_INTEL_HASWELL_IMC_DDRIO2:
  2270. case PCI_DEVICE_ID_INTEL_HASWELL_IMC_DDRIO3:
  2271. if (!pvt->pci_ddrio)
  2272. pvt->pci_ddrio = pdev;
  2273. break;
  2274. case PCI_DEVICE_ID_INTEL_HASWELL_IMC_HA1:
  2275. pvt->pci_ha1 = pdev;
  2276. break;
  2277. case PCI_DEVICE_ID_INTEL_HASWELL_IMC_HA1_TA:
  2278. pvt->pci_ha1_ta = pdev;
  2279. break;
  2280. default:
  2281. break;
  2282. }
  2283. edac_dbg(0, "Associated PCI %02x.%02d.%d with dev = %p\n",
  2284. sbridge_dev->bus,
  2285. PCI_SLOT(pdev->devfn), PCI_FUNC(pdev->devfn),
  2286. pdev);
  2287. }
  2288. /* Check if everything were registered */
  2289. if (!pvt->pci_sad0 || !pvt->pci_ha0 || !pvt->pci_sad1 ||
  2290. !pvt->pci_ras || !pvt->pci_ta || !pvt->info.pci_vtd)
  2291. goto enodev;
  2292. if (saw_chan_mask != 0x0f && /* -EN */
  2293. saw_chan_mask != 0x33 && /* -EP */
  2294. saw_chan_mask != 0xff) /* -EX */
  2295. goto enodev;
  2296. return 0;
  2297. enodev:
  2298. sbridge_printk(KERN_ERR, "Some needed devices are missing\n");
  2299. return -ENODEV;
  2300. }
  2301. static int broadwell_mci_bind_devs(struct mem_ctl_info *mci,
  2302. struct sbridge_dev *sbridge_dev)
  2303. {
  2304. struct sbridge_pvt *pvt = mci->pvt_info;
  2305. struct pci_dev *pdev;
  2306. u8 saw_chan_mask = 0;
  2307. int i;
  2308. /* there's only one device per system; not tied to any bus */
  2309. if (pvt->info.pci_vtd == NULL)
  2310. /* result will be checked later */
  2311. pvt->info.pci_vtd = pci_get_device(PCI_VENDOR_ID_INTEL,
  2312. PCI_DEVICE_ID_INTEL_BROADWELL_IMC_VTD_MISC,
  2313. NULL);
  2314. for (i = 0; i < sbridge_dev->n_devs; i++) {
  2315. pdev = sbridge_dev->pdev[i];
  2316. if (!pdev)
  2317. continue;
  2318. switch (pdev->device) {
  2319. case PCI_DEVICE_ID_INTEL_BROADWELL_IMC_CBO_SAD0:
  2320. pvt->pci_sad0 = pdev;
  2321. break;
  2322. case PCI_DEVICE_ID_INTEL_BROADWELL_IMC_CBO_SAD1:
  2323. pvt->pci_sad1 = pdev;
  2324. break;
  2325. case PCI_DEVICE_ID_INTEL_BROADWELL_IMC_HA0:
  2326. pvt->pci_ha0 = pdev;
  2327. break;
  2328. case PCI_DEVICE_ID_INTEL_BROADWELL_IMC_HA0_TA:
  2329. pvt->pci_ta = pdev;
  2330. break;
  2331. case PCI_DEVICE_ID_INTEL_BROADWELL_IMC_HA0_THERMAL:
  2332. pvt->pci_ras = pdev;
  2333. break;
  2334. case PCI_DEVICE_ID_INTEL_BROADWELL_IMC_HA0_TAD0:
  2335. case PCI_DEVICE_ID_INTEL_BROADWELL_IMC_HA0_TAD1:
  2336. case PCI_DEVICE_ID_INTEL_BROADWELL_IMC_HA0_TAD2:
  2337. case PCI_DEVICE_ID_INTEL_BROADWELL_IMC_HA0_TAD3:
  2338. {
  2339. int id = pdev->device - PCI_DEVICE_ID_INTEL_BROADWELL_IMC_HA0_TAD0;
  2340. pvt->pci_tad[id] = pdev;
  2341. saw_chan_mask |= 1 << id;
  2342. }
  2343. break;
  2344. case PCI_DEVICE_ID_INTEL_BROADWELL_IMC_HA1_TAD0:
  2345. case PCI_DEVICE_ID_INTEL_BROADWELL_IMC_HA1_TAD1:
  2346. case PCI_DEVICE_ID_INTEL_BROADWELL_IMC_HA1_TAD2:
  2347. case PCI_DEVICE_ID_INTEL_BROADWELL_IMC_HA1_TAD3:
  2348. {
  2349. int id = pdev->device - PCI_DEVICE_ID_INTEL_BROADWELL_IMC_HA1_TAD0 + 4;
  2350. pvt->pci_tad[id] = pdev;
  2351. saw_chan_mask |= 1 << id;
  2352. }
  2353. break;
  2354. case PCI_DEVICE_ID_INTEL_BROADWELL_IMC_DDRIO0:
  2355. pvt->pci_ddrio = pdev;
  2356. break;
  2357. case PCI_DEVICE_ID_INTEL_BROADWELL_IMC_HA1:
  2358. pvt->pci_ha1 = pdev;
  2359. break;
  2360. case PCI_DEVICE_ID_INTEL_BROADWELL_IMC_HA1_TA:
  2361. pvt->pci_ha1_ta = pdev;
  2362. break;
  2363. default:
  2364. break;
  2365. }
  2366. edac_dbg(0, "Associated PCI %02x.%02d.%d with dev = %p\n",
  2367. sbridge_dev->bus,
  2368. PCI_SLOT(pdev->devfn), PCI_FUNC(pdev->devfn),
  2369. pdev);
  2370. }
  2371. /* Check if everything were registered */
  2372. if (!pvt->pci_sad0 || !pvt->pci_ha0 || !pvt->pci_sad1 ||
  2373. !pvt->pci_ras || !pvt->pci_ta || !pvt->info.pci_vtd)
  2374. goto enodev;
  2375. if (saw_chan_mask != 0x0f && /* -EN */
  2376. saw_chan_mask != 0x33 && /* -EP */
  2377. saw_chan_mask != 0xff) /* -EX */
  2378. goto enodev;
  2379. return 0;
  2380. enodev:
  2381. sbridge_printk(KERN_ERR, "Some needed devices are missing\n");
  2382. return -ENODEV;
  2383. }
  2384. static int knl_mci_bind_devs(struct mem_ctl_info *mci,
  2385. struct sbridge_dev *sbridge_dev)
  2386. {
  2387. struct sbridge_pvt *pvt = mci->pvt_info;
  2388. struct pci_dev *pdev;
  2389. int dev, func;
  2390. int i;
  2391. int devidx;
  2392. for (i = 0; i < sbridge_dev->n_devs; i++) {
  2393. pdev = sbridge_dev->pdev[i];
  2394. if (!pdev)
  2395. continue;
  2396. /* Extract PCI device and function. */
  2397. dev = (pdev->devfn >> 3) & 0x1f;
  2398. func = pdev->devfn & 0x7;
  2399. switch (pdev->device) {
  2400. case PCI_DEVICE_ID_INTEL_KNL_IMC_MC:
  2401. if (dev == 8)
  2402. pvt->knl.pci_mc0 = pdev;
  2403. else if (dev == 9)
  2404. pvt->knl.pci_mc1 = pdev;
  2405. else {
  2406. sbridge_printk(KERN_ERR,
  2407. "Memory controller in unexpected place! (dev %d, fn %d)\n",
  2408. dev, func);
  2409. continue;
  2410. }
  2411. break;
  2412. case PCI_DEVICE_ID_INTEL_KNL_IMC_SAD0:
  2413. pvt->pci_sad0 = pdev;
  2414. break;
  2415. case PCI_DEVICE_ID_INTEL_KNL_IMC_SAD1:
  2416. pvt->pci_sad1 = pdev;
  2417. break;
  2418. case PCI_DEVICE_ID_INTEL_KNL_IMC_CHA:
  2419. /* There are one of these per tile, and range from
  2420. * 1.14.0 to 1.18.5.
  2421. */
  2422. devidx = ((dev-14)*8)+func;
  2423. if (devidx < 0 || devidx >= KNL_MAX_CHAS) {
  2424. sbridge_printk(KERN_ERR,
  2425. "Caching and Home Agent in unexpected place! (dev %d, fn %d)\n",
  2426. dev, func);
  2427. continue;
  2428. }
  2429. WARN_ON(pvt->knl.pci_cha[devidx] != NULL);
  2430. pvt->knl.pci_cha[devidx] = pdev;
  2431. break;
  2432. case PCI_DEVICE_ID_INTEL_KNL_IMC_CHANNEL:
  2433. devidx = -1;
  2434. /*
  2435. * MC0 channels 0-2 are device 9 function 2-4,
  2436. * MC1 channels 3-5 are device 8 function 2-4.
  2437. */
  2438. if (dev == 9)
  2439. devidx = func-2;
  2440. else if (dev == 8)
  2441. devidx = 3 + (func-2);
  2442. if (devidx < 0 || devidx >= KNL_MAX_CHANNELS) {
  2443. sbridge_printk(KERN_ERR,
  2444. "DRAM Channel Registers in unexpected place! (dev %d, fn %d)\n",
  2445. dev, func);
  2446. continue;
  2447. }
  2448. WARN_ON(pvt->knl.pci_channel[devidx] != NULL);
  2449. pvt->knl.pci_channel[devidx] = pdev;
  2450. break;
  2451. case PCI_DEVICE_ID_INTEL_KNL_IMC_TOLHM:
  2452. pvt->knl.pci_mc_info = pdev;
  2453. break;
  2454. case PCI_DEVICE_ID_INTEL_KNL_IMC_TA:
  2455. pvt->pci_ta = pdev;
  2456. break;
  2457. default:
  2458. sbridge_printk(KERN_ERR, "Unexpected device %d\n",
  2459. pdev->device);
  2460. break;
  2461. }
  2462. }
  2463. if (!pvt->knl.pci_mc0 || !pvt->knl.pci_mc1 ||
  2464. !pvt->pci_sad0 || !pvt->pci_sad1 ||
  2465. !pvt->pci_ta) {
  2466. goto enodev;
  2467. }
  2468. for (i = 0; i < KNL_MAX_CHANNELS; i++) {
  2469. if (!pvt->knl.pci_channel[i]) {
  2470. sbridge_printk(KERN_ERR, "Missing channel %d\n", i);
  2471. goto enodev;
  2472. }
  2473. }
  2474. for (i = 0; i < KNL_MAX_CHAS; i++) {
  2475. if (!pvt->knl.pci_cha[i]) {
  2476. sbridge_printk(KERN_ERR, "Missing CHA %d\n", i);
  2477. goto enodev;
  2478. }
  2479. }
  2480. return 0;
  2481. enodev:
  2482. sbridge_printk(KERN_ERR, "Some needed devices are missing\n");
  2483. return -ENODEV;
  2484. }
  2485. /****************************************************************************
  2486. Error check routines
  2487. ****************************************************************************/
  2488. /*
  2489. * While Sandy Bridge has error count registers, SMI BIOS read values from
  2490. * and resets the counters. So, they are not reliable for the OS to read
  2491. * from them. So, we have no option but to just trust on whatever MCE is
  2492. * telling us about the errors.
  2493. */
  2494. static void sbridge_mce_output_error(struct mem_ctl_info *mci,
  2495. const struct mce *m)
  2496. {
  2497. struct mem_ctl_info *new_mci;
  2498. struct sbridge_pvt *pvt = mci->pvt_info;
  2499. enum hw_event_mc_err_type tp_event;
  2500. char *type, *optype, msg[256];
  2501. bool ripv = GET_BITFIELD(m->mcgstatus, 0, 0);
  2502. bool overflow = GET_BITFIELD(m->status, 62, 62);
  2503. bool uncorrected_error = GET_BITFIELD(m->status, 61, 61);
  2504. bool recoverable;
  2505. u32 core_err_cnt = GET_BITFIELD(m->status, 38, 52);
  2506. u32 mscod = GET_BITFIELD(m->status, 16, 31);
  2507. u32 errcode = GET_BITFIELD(m->status, 0, 15);
  2508. u32 channel = GET_BITFIELD(m->status, 0, 3);
  2509. u32 optypenum = GET_BITFIELD(m->status, 4, 6);
  2510. long channel_mask, first_channel;
  2511. u8 rank, socket, ha;
  2512. int rc, dimm;
  2513. char *area_type = NULL;
  2514. if (pvt->info.type != SANDY_BRIDGE)
  2515. recoverable = true;
  2516. else
  2517. recoverable = GET_BITFIELD(m->status, 56, 56);
  2518. if (uncorrected_error) {
  2519. if (ripv) {
  2520. type = "FATAL";
  2521. tp_event = HW_EVENT_ERR_FATAL;
  2522. } else {
  2523. type = "NON_FATAL";
  2524. tp_event = HW_EVENT_ERR_UNCORRECTED;
  2525. }
  2526. } else {
  2527. type = "CORRECTED";
  2528. tp_event = HW_EVENT_ERR_CORRECTED;
  2529. }
  2530. /*
  2531. * According with Table 15-9 of the Intel Architecture spec vol 3A,
  2532. * memory errors should fit in this mask:
  2533. * 000f 0000 1mmm cccc (binary)
  2534. * where:
  2535. * f = Correction Report Filtering Bit. If 1, subsequent errors
  2536. * won't be shown
  2537. * mmm = error type
  2538. * cccc = channel
  2539. * If the mask doesn't match, report an error to the parsing logic
  2540. */
  2541. if (! ((errcode & 0xef80) == 0x80)) {
  2542. optype = "Can't parse: it is not a mem";
  2543. } else {
  2544. switch (optypenum) {
  2545. case 0:
  2546. optype = "generic undef request error";
  2547. break;
  2548. case 1:
  2549. optype = "memory read error";
  2550. break;
  2551. case 2:
  2552. optype = "memory write error";
  2553. break;
  2554. case 3:
  2555. optype = "addr/cmd error";
  2556. break;
  2557. case 4:
  2558. optype = "memory scrubbing error";
  2559. break;
  2560. default:
  2561. optype = "reserved";
  2562. break;
  2563. }
  2564. }
  2565. /* Only decode errors with an valid address (ADDRV) */
  2566. if (!GET_BITFIELD(m->status, 58, 58))
  2567. return;
  2568. if (pvt->info.type == KNIGHTS_LANDING) {
  2569. if (channel == 14) {
  2570. edac_dbg(0, "%s%s err_code:%04x:%04x EDRAM bank %d\n",
  2571. overflow ? " OVERFLOW" : "",
  2572. (uncorrected_error && recoverable)
  2573. ? " recoverable" : "",
  2574. mscod, errcode,
  2575. m->bank);
  2576. } else {
  2577. char A = *("A");
  2578. /*
  2579. * Reported channel is in range 0-2, so we can't map it
  2580. * back to mc. To figure out mc we check machine check
  2581. * bank register that reported this error.
  2582. * bank15 means mc0 and bank16 means mc1.
  2583. */
  2584. channel = knl_channel_remap(m->bank == 16, channel);
  2585. channel_mask = 1 << channel;
  2586. snprintf(msg, sizeof(msg),
  2587. "%s%s err_code:%04x:%04x channel:%d (DIMM_%c)",
  2588. overflow ? " OVERFLOW" : "",
  2589. (uncorrected_error && recoverable)
  2590. ? " recoverable" : " ",
  2591. mscod, errcode, channel, A + channel);
  2592. edac_mc_handle_error(tp_event, mci, core_err_cnt,
  2593. m->addr >> PAGE_SHIFT, m->addr & ~PAGE_MASK, 0,
  2594. channel, 0, -1,
  2595. optype, msg);
  2596. }
  2597. return;
  2598. } else {
  2599. rc = get_memory_error_data(mci, m->addr, &socket, &ha,
  2600. &channel_mask, &rank, &area_type, msg);
  2601. }
  2602. if (rc < 0)
  2603. goto err_parsing;
  2604. new_mci = get_mci_for_node_id(socket);
  2605. if (!new_mci) {
  2606. strcpy(msg, "Error: socket got corrupted!");
  2607. goto err_parsing;
  2608. }
  2609. mci = new_mci;
  2610. pvt = mci->pvt_info;
  2611. first_channel = find_first_bit(&channel_mask, NUM_CHANNELS);
  2612. if (rank < 4)
  2613. dimm = 0;
  2614. else if (rank < 8)
  2615. dimm = 1;
  2616. else
  2617. dimm = 2;
  2618. /*
  2619. * FIXME: On some memory configurations (mirror, lockstep), the
  2620. * Memory Controller can't point the error to a single DIMM. The
  2621. * EDAC core should be handling the channel mask, in order to point
  2622. * to the group of dimm's where the error may be happening.
  2623. */
  2624. if (!pvt->is_lockstep && !pvt->is_mirrored && !pvt->is_close_pg)
  2625. channel = first_channel;
  2626. snprintf(msg, sizeof(msg),
  2627. "%s%s area:%s err_code:%04x:%04x socket:%d ha:%d channel_mask:%ld rank:%d",
  2628. overflow ? " OVERFLOW" : "",
  2629. (uncorrected_error && recoverable) ? " recoverable" : "",
  2630. area_type,
  2631. mscod, errcode,
  2632. socket, ha,
  2633. channel_mask,
  2634. rank);
  2635. edac_dbg(0, "%s\n", msg);
  2636. /* FIXME: need support for channel mask */
  2637. if (channel == CHANNEL_UNSPECIFIED)
  2638. channel = -1;
  2639. /* Call the helper to output message */
  2640. edac_mc_handle_error(tp_event, mci, core_err_cnt,
  2641. m->addr >> PAGE_SHIFT, m->addr & ~PAGE_MASK, 0,
  2642. 4*ha+channel, dimm, -1,
  2643. optype, msg);
  2644. return;
  2645. err_parsing:
  2646. edac_mc_handle_error(tp_event, mci, core_err_cnt, 0, 0, 0,
  2647. -1, -1, -1,
  2648. msg, "");
  2649. }
  2650. /*
  2651. * Check that logging is enabled and that this is the right type
  2652. * of error for us to handle.
  2653. */
  2654. static int sbridge_mce_check_error(struct notifier_block *nb, unsigned long val,
  2655. void *data)
  2656. {
  2657. struct mce *mce = (struct mce *)data;
  2658. struct mem_ctl_info *mci;
  2659. struct sbridge_pvt *pvt;
  2660. char *type;
  2661. if (get_edac_report_status() == EDAC_REPORTING_DISABLED)
  2662. return NOTIFY_DONE;
  2663. mci = get_mci_for_node_id(mce->socketid);
  2664. if (!mci)
  2665. return NOTIFY_DONE;
  2666. pvt = mci->pvt_info;
  2667. /*
  2668. * Just let mcelog handle it if the error is
  2669. * outside the memory controller. A memory error
  2670. * is indicated by bit 7 = 1 and bits = 8-11,13-15 = 0.
  2671. * bit 12 has an special meaning.
  2672. */
  2673. if ((mce->status & 0xefff) >> 7 != 1)
  2674. return NOTIFY_DONE;
  2675. if (mce->mcgstatus & MCG_STATUS_MCIP)
  2676. type = "Exception";
  2677. else
  2678. type = "Event";
  2679. sbridge_mc_printk(mci, KERN_DEBUG, "HANDLING MCE MEMORY ERROR\n");
  2680. sbridge_mc_printk(mci, KERN_DEBUG, "CPU %d: Machine Check %s: %Lx "
  2681. "Bank %d: %016Lx\n", mce->extcpu, type,
  2682. mce->mcgstatus, mce->bank, mce->status);
  2683. sbridge_mc_printk(mci, KERN_DEBUG, "TSC %llx ", mce->tsc);
  2684. sbridge_mc_printk(mci, KERN_DEBUG, "ADDR %llx ", mce->addr);
  2685. sbridge_mc_printk(mci, KERN_DEBUG, "MISC %llx ", mce->misc);
  2686. sbridge_mc_printk(mci, KERN_DEBUG, "PROCESSOR %u:%x TIME %llu SOCKET "
  2687. "%u APIC %x\n", mce->cpuvendor, mce->cpuid,
  2688. mce->time, mce->socketid, mce->apicid);
  2689. sbridge_mce_output_error(mci, mce);
  2690. /* Advice mcelog that the error were handled */
  2691. return NOTIFY_STOP;
  2692. }
  2693. static struct notifier_block sbridge_mce_dec = {
  2694. .notifier_call = sbridge_mce_check_error,
  2695. };
  2696. /****************************************************************************
  2697. EDAC register/unregister logic
  2698. ****************************************************************************/
  2699. static void sbridge_unregister_mci(struct sbridge_dev *sbridge_dev)
  2700. {
  2701. struct mem_ctl_info *mci = sbridge_dev->mci;
  2702. struct sbridge_pvt *pvt;
  2703. if (unlikely(!mci || !mci->pvt_info)) {
  2704. edac_dbg(0, "MC: dev = %p\n", &sbridge_dev->pdev[0]->dev);
  2705. sbridge_printk(KERN_ERR, "Couldn't find mci handler\n");
  2706. return;
  2707. }
  2708. pvt = mci->pvt_info;
  2709. edac_dbg(0, "MC: mci = %p, dev = %p\n",
  2710. mci, &sbridge_dev->pdev[0]->dev);
  2711. /* Remove MC sysfs nodes */
  2712. edac_mc_del_mc(mci->pdev);
  2713. edac_dbg(1, "%s: free mci struct\n", mci->ctl_name);
  2714. kfree(mci->ctl_name);
  2715. edac_mc_free(mci);
  2716. sbridge_dev->mci = NULL;
  2717. }
  2718. static int sbridge_register_mci(struct sbridge_dev *sbridge_dev, enum type type)
  2719. {
  2720. struct mem_ctl_info *mci;
  2721. struct edac_mc_layer layers[2];
  2722. struct sbridge_pvt *pvt;
  2723. struct pci_dev *pdev = sbridge_dev->pdev[0];
  2724. int rc;
  2725. /* Check the number of active and not disabled channels */
  2726. rc = check_if_ecc_is_active(sbridge_dev->bus, type);
  2727. if (unlikely(rc < 0))
  2728. return rc;
  2729. /* allocate a new MC control structure */
  2730. layers[0].type = EDAC_MC_LAYER_CHANNEL;
  2731. layers[0].size = type == KNIGHTS_LANDING ?
  2732. KNL_MAX_CHANNELS : NUM_CHANNELS;
  2733. layers[0].is_virt_csrow = false;
  2734. layers[1].type = EDAC_MC_LAYER_SLOT;
  2735. layers[1].size = type == KNIGHTS_LANDING ? 1 : MAX_DIMMS;
  2736. layers[1].is_virt_csrow = true;
  2737. mci = edac_mc_alloc(sbridge_dev->mc, ARRAY_SIZE(layers), layers,
  2738. sizeof(*pvt));
  2739. if (unlikely(!mci))
  2740. return -ENOMEM;
  2741. edac_dbg(0, "MC: mci = %p, dev = %p\n",
  2742. mci, &pdev->dev);
  2743. pvt = mci->pvt_info;
  2744. memset(pvt, 0, sizeof(*pvt));
  2745. /* Associate sbridge_dev and mci for future usage */
  2746. pvt->sbridge_dev = sbridge_dev;
  2747. sbridge_dev->mci = mci;
  2748. mci->mtype_cap = type == KNIGHTS_LANDING ?
  2749. MEM_FLAG_DDR4 : MEM_FLAG_DDR3;
  2750. mci->edac_ctl_cap = EDAC_FLAG_NONE;
  2751. mci->edac_cap = EDAC_FLAG_NONE;
  2752. mci->mod_name = "sbridge_edac.c";
  2753. mci->mod_ver = SBRIDGE_REVISION;
  2754. mci->dev_name = pci_name(pdev);
  2755. mci->ctl_page_to_phys = NULL;
  2756. pvt->info.type = type;
  2757. switch (type) {
  2758. case IVY_BRIDGE:
  2759. pvt->info.rankcfgr = IB_RANK_CFG_A;
  2760. pvt->info.get_tolm = ibridge_get_tolm;
  2761. pvt->info.get_tohm = ibridge_get_tohm;
  2762. pvt->info.dram_rule = ibridge_dram_rule;
  2763. pvt->info.get_memory_type = get_memory_type;
  2764. pvt->info.get_node_id = get_node_id;
  2765. pvt->info.rir_limit = rir_limit;
  2766. pvt->info.sad_limit = sad_limit;
  2767. pvt->info.interleave_mode = interleave_mode;
  2768. pvt->info.show_interleave_mode = show_interleave_mode;
  2769. pvt->info.dram_attr = dram_attr;
  2770. pvt->info.max_sad = ARRAY_SIZE(ibridge_dram_rule);
  2771. pvt->info.interleave_list = ibridge_interleave_list;
  2772. pvt->info.max_interleave = ARRAY_SIZE(ibridge_interleave_list);
  2773. pvt->info.interleave_pkg = ibridge_interleave_pkg;
  2774. pvt->info.get_width = ibridge_get_width;
  2775. mci->ctl_name = kasprintf(GFP_KERNEL, "Ivy Bridge Socket#%d", mci->mc_idx);
  2776. /* Store pci devices at mci for faster access */
  2777. rc = ibridge_mci_bind_devs(mci, sbridge_dev);
  2778. if (unlikely(rc < 0))
  2779. goto fail0;
  2780. break;
  2781. case SANDY_BRIDGE:
  2782. pvt->info.rankcfgr = SB_RANK_CFG_A;
  2783. pvt->info.get_tolm = sbridge_get_tolm;
  2784. pvt->info.get_tohm = sbridge_get_tohm;
  2785. pvt->info.dram_rule = sbridge_dram_rule;
  2786. pvt->info.get_memory_type = get_memory_type;
  2787. pvt->info.get_node_id = get_node_id;
  2788. pvt->info.rir_limit = rir_limit;
  2789. pvt->info.sad_limit = sad_limit;
  2790. pvt->info.interleave_mode = interleave_mode;
  2791. pvt->info.show_interleave_mode = show_interleave_mode;
  2792. pvt->info.dram_attr = dram_attr;
  2793. pvt->info.max_sad = ARRAY_SIZE(sbridge_dram_rule);
  2794. pvt->info.interleave_list = sbridge_interleave_list;
  2795. pvt->info.max_interleave = ARRAY_SIZE(sbridge_interleave_list);
  2796. pvt->info.interleave_pkg = sbridge_interleave_pkg;
  2797. pvt->info.get_width = sbridge_get_width;
  2798. mci->ctl_name = kasprintf(GFP_KERNEL, "Sandy Bridge Socket#%d", mci->mc_idx);
  2799. /* Store pci devices at mci for faster access */
  2800. rc = sbridge_mci_bind_devs(mci, sbridge_dev);
  2801. if (unlikely(rc < 0))
  2802. goto fail0;
  2803. break;
  2804. case HASWELL:
  2805. /* rankcfgr isn't used */
  2806. pvt->info.get_tolm = haswell_get_tolm;
  2807. pvt->info.get_tohm = haswell_get_tohm;
  2808. pvt->info.dram_rule = ibridge_dram_rule;
  2809. pvt->info.get_memory_type = haswell_get_memory_type;
  2810. pvt->info.get_node_id = haswell_get_node_id;
  2811. pvt->info.rir_limit = haswell_rir_limit;
  2812. pvt->info.sad_limit = sad_limit;
  2813. pvt->info.interleave_mode = interleave_mode;
  2814. pvt->info.show_interleave_mode = show_interleave_mode;
  2815. pvt->info.dram_attr = dram_attr;
  2816. pvt->info.max_sad = ARRAY_SIZE(ibridge_dram_rule);
  2817. pvt->info.interleave_list = ibridge_interleave_list;
  2818. pvt->info.max_interleave = ARRAY_SIZE(ibridge_interleave_list);
  2819. pvt->info.interleave_pkg = ibridge_interleave_pkg;
  2820. pvt->info.get_width = ibridge_get_width;
  2821. mci->ctl_name = kasprintf(GFP_KERNEL, "Haswell Socket#%d", mci->mc_idx);
  2822. /* Store pci devices at mci for faster access */
  2823. rc = haswell_mci_bind_devs(mci, sbridge_dev);
  2824. if (unlikely(rc < 0))
  2825. goto fail0;
  2826. break;
  2827. case BROADWELL:
  2828. /* rankcfgr isn't used */
  2829. pvt->info.get_tolm = haswell_get_tolm;
  2830. pvt->info.get_tohm = haswell_get_tohm;
  2831. pvt->info.dram_rule = ibridge_dram_rule;
  2832. pvt->info.get_memory_type = haswell_get_memory_type;
  2833. pvt->info.get_node_id = haswell_get_node_id;
  2834. pvt->info.rir_limit = haswell_rir_limit;
  2835. pvt->info.sad_limit = sad_limit;
  2836. pvt->info.interleave_mode = interleave_mode;
  2837. pvt->info.show_interleave_mode = show_interleave_mode;
  2838. pvt->info.dram_attr = dram_attr;
  2839. pvt->info.max_sad = ARRAY_SIZE(ibridge_dram_rule);
  2840. pvt->info.interleave_list = ibridge_interleave_list;
  2841. pvt->info.max_interleave = ARRAY_SIZE(ibridge_interleave_list);
  2842. pvt->info.interleave_pkg = ibridge_interleave_pkg;
  2843. pvt->info.get_width = broadwell_get_width;
  2844. mci->ctl_name = kasprintf(GFP_KERNEL, "Broadwell Socket#%d", mci->mc_idx);
  2845. /* Store pci devices at mci for faster access */
  2846. rc = broadwell_mci_bind_devs(mci, sbridge_dev);
  2847. if (unlikely(rc < 0))
  2848. goto fail0;
  2849. break;
  2850. case KNIGHTS_LANDING:
  2851. /* pvt->info.rankcfgr == ??? */
  2852. pvt->info.get_tolm = knl_get_tolm;
  2853. pvt->info.get_tohm = knl_get_tohm;
  2854. pvt->info.dram_rule = knl_dram_rule;
  2855. pvt->info.get_memory_type = knl_get_memory_type;
  2856. pvt->info.get_node_id = knl_get_node_id;
  2857. pvt->info.rir_limit = NULL;
  2858. pvt->info.sad_limit = knl_sad_limit;
  2859. pvt->info.interleave_mode = knl_interleave_mode;
  2860. pvt->info.show_interleave_mode = knl_show_interleave_mode;
  2861. pvt->info.dram_attr = dram_attr_knl;
  2862. pvt->info.max_sad = ARRAY_SIZE(knl_dram_rule);
  2863. pvt->info.interleave_list = knl_interleave_list;
  2864. pvt->info.max_interleave = ARRAY_SIZE(knl_interleave_list);
  2865. pvt->info.interleave_pkg = ibridge_interleave_pkg;
  2866. pvt->info.get_width = knl_get_width;
  2867. mci->ctl_name = kasprintf(GFP_KERNEL,
  2868. "Knights Landing Socket#%d", mci->mc_idx);
  2869. rc = knl_mci_bind_devs(mci, sbridge_dev);
  2870. if (unlikely(rc < 0))
  2871. goto fail0;
  2872. break;
  2873. }
  2874. /* Get dimm basic config and the memory layout */
  2875. get_dimm_config(mci);
  2876. get_memory_layout(mci);
  2877. /* record ptr to the generic device */
  2878. mci->pdev = &pdev->dev;
  2879. /* add this new MC control structure to EDAC's list of MCs */
  2880. if (unlikely(edac_mc_add_mc(mci))) {
  2881. edac_dbg(0, "MC: failed edac_mc_add_mc()\n");
  2882. rc = -EINVAL;
  2883. goto fail0;
  2884. }
  2885. return 0;
  2886. fail0:
  2887. kfree(mci->ctl_name);
  2888. edac_mc_free(mci);
  2889. sbridge_dev->mci = NULL;
  2890. return rc;
  2891. }
  2892. #define ICPU(model, table) \
  2893. { X86_VENDOR_INTEL, 6, model, 0, (unsigned long)&table }
  2894. static const struct x86_cpu_id sbridge_cpuids[] = {
  2895. ICPU(INTEL_FAM6_SANDYBRIDGE_X, pci_dev_descr_sbridge_table),
  2896. ICPU(INTEL_FAM6_IVYBRIDGE_X, pci_dev_descr_ibridge_table),
  2897. ICPU(INTEL_FAM6_HASWELL_X, pci_dev_descr_haswell_table),
  2898. ICPU(INTEL_FAM6_BROADWELL_X, pci_dev_descr_broadwell_table),
  2899. ICPU(INTEL_FAM6_BROADWELL_XEON_D, pci_dev_descr_broadwell_table),
  2900. ICPU(INTEL_FAM6_XEON_PHI_KNL, pci_dev_descr_knl_table),
  2901. ICPU(INTEL_FAM6_XEON_PHI_KNM, pci_dev_descr_knl_table),
  2902. { }
  2903. };
  2904. MODULE_DEVICE_TABLE(x86cpu, sbridge_cpuids);
  2905. /*
  2906. * sbridge_probe Get all devices and register memory controllers
  2907. * present.
  2908. * return:
  2909. * 0 for FOUND a device
  2910. * < 0 for error code
  2911. */
  2912. static int sbridge_probe(const struct x86_cpu_id *id)
  2913. {
  2914. int rc = -ENODEV;
  2915. u8 mc, num_mc = 0;
  2916. struct sbridge_dev *sbridge_dev;
  2917. struct pci_id_table *ptable = (struct pci_id_table *)id->driver_data;
  2918. /* get the pci devices we want to reserve for our use */
  2919. rc = sbridge_get_all_devices(&num_mc, ptable);
  2920. if (unlikely(rc < 0)) {
  2921. edac_dbg(0, "couldn't get all devices\n");
  2922. goto fail0;
  2923. }
  2924. mc = 0;
  2925. list_for_each_entry(sbridge_dev, &sbridge_edac_list, list) {
  2926. edac_dbg(0, "Registering MC#%d (%d of %d)\n",
  2927. mc, mc + 1, num_mc);
  2928. sbridge_dev->mc = mc++;
  2929. rc = sbridge_register_mci(sbridge_dev, ptable->type);
  2930. if (unlikely(rc < 0))
  2931. goto fail1;
  2932. }
  2933. sbridge_printk(KERN_INFO, "%s\n", SBRIDGE_REVISION);
  2934. return 0;
  2935. fail1:
  2936. list_for_each_entry(sbridge_dev, &sbridge_edac_list, list)
  2937. sbridge_unregister_mci(sbridge_dev);
  2938. sbridge_put_all_devices();
  2939. fail0:
  2940. return rc;
  2941. }
  2942. /*
  2943. * sbridge_remove cleanup
  2944. *
  2945. */
  2946. static void sbridge_remove(void)
  2947. {
  2948. struct sbridge_dev *sbridge_dev;
  2949. edac_dbg(0, "\n");
  2950. list_for_each_entry(sbridge_dev, &sbridge_edac_list, list)
  2951. sbridge_unregister_mci(sbridge_dev);
  2952. /* Release PCI resources */
  2953. sbridge_put_all_devices();
  2954. }
  2955. /*
  2956. * sbridge_init Module entry function
  2957. * Try to initialize this module for its devices
  2958. */
  2959. static int __init sbridge_init(void)
  2960. {
  2961. const struct x86_cpu_id *id;
  2962. int rc;
  2963. edac_dbg(2, "\n");
  2964. id = x86_match_cpu(sbridge_cpuids);
  2965. if (!id)
  2966. return -ENODEV;
  2967. /* Ensure that the OPSTATE is set correctly for POLL or NMI */
  2968. opstate_init();
  2969. rc = sbridge_probe(id);
  2970. if (rc >= 0) {
  2971. mce_register_decode_chain(&sbridge_mce_dec);
  2972. if (get_edac_report_status() == EDAC_REPORTING_DISABLED)
  2973. sbridge_printk(KERN_WARNING, "Loading driver, error reporting disabled.\n");
  2974. return 0;
  2975. }
  2976. sbridge_printk(KERN_ERR, "Failed to register device with error %d.\n",
  2977. rc);
  2978. return rc;
  2979. }
  2980. /*
  2981. * sbridge_exit() Module exit function
  2982. * Unregister the driver
  2983. */
  2984. static void __exit sbridge_exit(void)
  2985. {
  2986. edac_dbg(2, "\n");
  2987. sbridge_remove();
  2988. mce_unregister_decode_chain(&sbridge_mce_dec);
  2989. }
  2990. module_init(sbridge_init);
  2991. module_exit(sbridge_exit);
  2992. module_param(edac_op_state, int, 0444);
  2993. MODULE_PARM_DESC(edac_op_state, "EDAC Error Reporting state: 0=Poll,1=NMI");
  2994. MODULE_LICENSE("GPL");
  2995. MODULE_AUTHOR("Mauro Carvalho Chehab");
  2996. MODULE_AUTHOR("Red Hat Inc. (http://www.redhat.com)");
  2997. MODULE_DESCRIPTION("MC Driver for Intel Sandy Bridge and Ivy Bridge memory controllers - "
  2998. SBRIDGE_REVISION);