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/processor.h>
  26. #include <asm/mce.h>
  27. #include "edac_core.h"
  28. /* Static vars */
  29. static LIST_HEAD(sbridge_edac_list);
  30. /*
  31. * Alter this version for the module when modifications are made
  32. */
  33. #define SBRIDGE_REVISION " Ver: 1.1.1 "
  34. #define EDAC_MOD_STR "sbridge_edac"
  35. /*
  36. * Debug macros
  37. */
  38. #define sbridge_printk(level, fmt, arg...) \
  39. edac_printk(level, "sbridge", fmt, ##arg)
  40. #define sbridge_mc_printk(mci, level, fmt, arg...) \
  41. edac_mc_chipset_printk(mci, level, "sbridge", fmt, ##arg)
  42. /*
  43. * Get a bit field at register value <v>, from bit <lo> to bit <hi>
  44. */
  45. #define GET_BITFIELD(v, lo, hi) \
  46. (((v) & GENMASK_ULL(hi, lo)) >> (lo))
  47. /* Devices 12 Function 6, Offsets 0x80 to 0xcc */
  48. static const u32 sbridge_dram_rule[] = {
  49. 0x80, 0x88, 0x90, 0x98, 0xa0,
  50. 0xa8, 0xb0, 0xb8, 0xc0, 0xc8,
  51. };
  52. static const u32 ibridge_dram_rule[] = {
  53. 0x60, 0x68, 0x70, 0x78, 0x80,
  54. 0x88, 0x90, 0x98, 0xa0, 0xa8,
  55. 0xb0, 0xb8, 0xc0, 0xc8, 0xd0,
  56. 0xd8, 0xe0, 0xe8, 0xf0, 0xf8,
  57. };
  58. static const u32 knl_dram_rule[] = {
  59. 0x60, 0x68, 0x70, 0x78, 0x80, /* 0-4 */
  60. 0x88, 0x90, 0x98, 0xa0, 0xa8, /* 5-9 */
  61. 0xb0, 0xb8, 0xc0, 0xc8, 0xd0, /* 10-14 */
  62. 0xd8, 0xe0, 0xe8, 0xf0, 0xf8, /* 15-19 */
  63. 0x100, 0x108, 0x110, 0x118, /* 20-23 */
  64. };
  65. #define DRAM_RULE_ENABLE(reg) GET_BITFIELD(reg, 0, 0)
  66. #define A7MODE(reg) GET_BITFIELD(reg, 26, 26)
  67. static char *show_dram_attr(u32 attr)
  68. {
  69. switch (attr) {
  70. case 0:
  71. return "DRAM";
  72. case 1:
  73. return "MMCFG";
  74. case 2:
  75. return "NXM";
  76. default:
  77. return "unknown";
  78. }
  79. }
  80. static const u32 sbridge_interleave_list[] = {
  81. 0x84, 0x8c, 0x94, 0x9c, 0xa4,
  82. 0xac, 0xb4, 0xbc, 0xc4, 0xcc,
  83. };
  84. static const u32 ibridge_interleave_list[] = {
  85. 0x64, 0x6c, 0x74, 0x7c, 0x84,
  86. 0x8c, 0x94, 0x9c, 0xa4, 0xac,
  87. 0xb4, 0xbc, 0xc4, 0xcc, 0xd4,
  88. 0xdc, 0xe4, 0xec, 0xf4, 0xfc,
  89. };
  90. static const u32 knl_interleave_list[] = {
  91. 0x64, 0x6c, 0x74, 0x7c, 0x84, /* 0-4 */
  92. 0x8c, 0x94, 0x9c, 0xa4, 0xac, /* 5-9 */
  93. 0xb4, 0xbc, 0xc4, 0xcc, 0xd4, /* 10-14 */
  94. 0xdc, 0xe4, 0xec, 0xf4, 0xfc, /* 15-19 */
  95. 0x104, 0x10c, 0x114, 0x11c, /* 20-23 */
  96. };
  97. struct interleave_pkg {
  98. unsigned char start;
  99. unsigned char end;
  100. };
  101. static const struct interleave_pkg sbridge_interleave_pkg[] = {
  102. { 0, 2 },
  103. { 3, 5 },
  104. { 8, 10 },
  105. { 11, 13 },
  106. { 16, 18 },
  107. { 19, 21 },
  108. { 24, 26 },
  109. { 27, 29 },
  110. };
  111. static const struct interleave_pkg ibridge_interleave_pkg[] = {
  112. { 0, 3 },
  113. { 4, 7 },
  114. { 8, 11 },
  115. { 12, 15 },
  116. { 16, 19 },
  117. { 20, 23 },
  118. { 24, 27 },
  119. { 28, 31 },
  120. };
  121. static inline int sad_pkg(const struct interleave_pkg *table, u32 reg,
  122. int interleave)
  123. {
  124. return GET_BITFIELD(reg, table[interleave].start,
  125. table[interleave].end);
  126. }
  127. /* Devices 12 Function 7 */
  128. #define TOLM 0x80
  129. #define TOHM 0x84
  130. #define HASWELL_TOLM 0xd0
  131. #define HASWELL_TOHM_0 0xd4
  132. #define HASWELL_TOHM_1 0xd8
  133. #define KNL_TOLM 0xd0
  134. #define KNL_TOHM_0 0xd4
  135. #define KNL_TOHM_1 0xd8
  136. #define GET_TOLM(reg) ((GET_BITFIELD(reg, 0, 3) << 28) | 0x3ffffff)
  137. #define GET_TOHM(reg) ((GET_BITFIELD(reg, 0, 20) << 25) | 0x3ffffff)
  138. /* Device 13 Function 6 */
  139. #define SAD_TARGET 0xf0
  140. #define SOURCE_ID(reg) GET_BITFIELD(reg, 9, 11)
  141. #define SOURCE_ID_KNL(reg) GET_BITFIELD(reg, 12, 14)
  142. #define SAD_CONTROL 0xf4
  143. /* Device 14 function 0 */
  144. static const u32 tad_dram_rule[] = {
  145. 0x40, 0x44, 0x48, 0x4c,
  146. 0x50, 0x54, 0x58, 0x5c,
  147. 0x60, 0x64, 0x68, 0x6c,
  148. };
  149. #define MAX_TAD ARRAY_SIZE(tad_dram_rule)
  150. #define TAD_LIMIT(reg) ((GET_BITFIELD(reg, 12, 31) << 26) | 0x3ffffff)
  151. #define TAD_SOCK(reg) GET_BITFIELD(reg, 10, 11)
  152. #define TAD_CH(reg) GET_BITFIELD(reg, 8, 9)
  153. #define TAD_TGT3(reg) GET_BITFIELD(reg, 6, 7)
  154. #define TAD_TGT2(reg) GET_BITFIELD(reg, 4, 5)
  155. #define TAD_TGT1(reg) GET_BITFIELD(reg, 2, 3)
  156. #define TAD_TGT0(reg) GET_BITFIELD(reg, 0, 1)
  157. /* Device 15, function 0 */
  158. #define MCMTR 0x7c
  159. #define KNL_MCMTR 0x624
  160. #define IS_ECC_ENABLED(mcmtr) GET_BITFIELD(mcmtr, 2, 2)
  161. #define IS_LOCKSTEP_ENABLED(mcmtr) GET_BITFIELD(mcmtr, 1, 1)
  162. #define IS_CLOSE_PG(mcmtr) GET_BITFIELD(mcmtr, 0, 0)
  163. /* Device 15, function 1 */
  164. #define RASENABLES 0xac
  165. #define IS_MIRROR_ENABLED(reg) GET_BITFIELD(reg, 0, 0)
  166. /* Device 15, functions 2-5 */
  167. static const int mtr_regs[] = {
  168. 0x80, 0x84, 0x88,
  169. };
  170. static const int knl_mtr_reg = 0xb60;
  171. #define RANK_DISABLE(mtr) GET_BITFIELD(mtr, 16, 19)
  172. #define IS_DIMM_PRESENT(mtr) GET_BITFIELD(mtr, 14, 14)
  173. #define RANK_CNT_BITS(mtr) GET_BITFIELD(mtr, 12, 13)
  174. #define RANK_WIDTH_BITS(mtr) GET_BITFIELD(mtr, 2, 4)
  175. #define COL_WIDTH_BITS(mtr) GET_BITFIELD(mtr, 0, 1)
  176. static const u32 tad_ch_nilv_offset[] = {
  177. 0x90, 0x94, 0x98, 0x9c,
  178. 0xa0, 0xa4, 0xa8, 0xac,
  179. 0xb0, 0xb4, 0xb8, 0xbc,
  180. };
  181. #define CHN_IDX_OFFSET(reg) GET_BITFIELD(reg, 28, 29)
  182. #define TAD_OFFSET(reg) (GET_BITFIELD(reg, 6, 25) << 26)
  183. static const u32 rir_way_limit[] = {
  184. 0x108, 0x10c, 0x110, 0x114, 0x118,
  185. };
  186. #define MAX_RIR_RANGES ARRAY_SIZE(rir_way_limit)
  187. #define IS_RIR_VALID(reg) GET_BITFIELD(reg, 31, 31)
  188. #define RIR_WAY(reg) GET_BITFIELD(reg, 28, 29)
  189. #define MAX_RIR_WAY 8
  190. static const u32 rir_offset[MAX_RIR_RANGES][MAX_RIR_WAY] = {
  191. { 0x120, 0x124, 0x128, 0x12c, 0x130, 0x134, 0x138, 0x13c },
  192. { 0x140, 0x144, 0x148, 0x14c, 0x150, 0x154, 0x158, 0x15c },
  193. { 0x160, 0x164, 0x168, 0x16c, 0x170, 0x174, 0x178, 0x17c },
  194. { 0x180, 0x184, 0x188, 0x18c, 0x190, 0x194, 0x198, 0x19c },
  195. { 0x1a0, 0x1a4, 0x1a8, 0x1ac, 0x1b0, 0x1b4, 0x1b8, 0x1bc },
  196. };
  197. #define RIR_RNK_TGT(type, reg) (((type) == BROADWELL) ? \
  198. GET_BITFIELD(reg, 20, 23) : GET_BITFIELD(reg, 16, 19))
  199. #define RIR_OFFSET(type, reg) (((type) == HASWELL || (type) == BROADWELL) ? \
  200. GET_BITFIELD(reg, 2, 15) : GET_BITFIELD(reg, 2, 14))
  201. /* Device 16, functions 2-7 */
  202. /*
  203. * FIXME: Implement the error count reads directly
  204. */
  205. static const u32 correrrcnt[] = {
  206. 0x104, 0x108, 0x10c, 0x110,
  207. };
  208. #define RANK_ODD_OV(reg) GET_BITFIELD(reg, 31, 31)
  209. #define RANK_ODD_ERR_CNT(reg) GET_BITFIELD(reg, 16, 30)
  210. #define RANK_EVEN_OV(reg) GET_BITFIELD(reg, 15, 15)
  211. #define RANK_EVEN_ERR_CNT(reg) GET_BITFIELD(reg, 0, 14)
  212. static const u32 correrrthrsld[] = {
  213. 0x11c, 0x120, 0x124, 0x128,
  214. };
  215. #define RANK_ODD_ERR_THRSLD(reg) GET_BITFIELD(reg, 16, 30)
  216. #define RANK_EVEN_ERR_THRSLD(reg) GET_BITFIELD(reg, 0, 14)
  217. /* Device 17, function 0 */
  218. #define SB_RANK_CFG_A 0x0328
  219. #define IB_RANK_CFG_A 0x0320
  220. /*
  221. * sbridge structs
  222. */
  223. #define NUM_CHANNELS 8 /* 2MC per socket, four chan per MC */
  224. #define MAX_DIMMS 3 /* Max DIMMS per channel */
  225. #define KNL_MAX_CHAS 38 /* KNL max num. of Cache Home Agents */
  226. #define KNL_MAX_CHANNELS 6 /* KNL max num. of PCI channels */
  227. #define KNL_MAX_EDCS 8 /* Embedded DRAM controllers */
  228. #define CHANNEL_UNSPECIFIED 0xf /* Intel IA32 SDM 15-14 */
  229. enum type {
  230. SANDY_BRIDGE,
  231. IVY_BRIDGE,
  232. HASWELL,
  233. BROADWELL,
  234. KNIGHTS_LANDING,
  235. };
  236. struct sbridge_pvt;
  237. struct sbridge_info {
  238. enum type type;
  239. u32 mcmtr;
  240. u32 rankcfgr;
  241. u64 (*get_tolm)(struct sbridge_pvt *pvt);
  242. u64 (*get_tohm)(struct sbridge_pvt *pvt);
  243. u64 (*rir_limit)(u32 reg);
  244. u64 (*sad_limit)(u32 reg);
  245. u32 (*interleave_mode)(u32 reg);
  246. char* (*show_interleave_mode)(u32 reg);
  247. u32 (*dram_attr)(u32 reg);
  248. const u32 *dram_rule;
  249. const u32 *interleave_list;
  250. const struct interleave_pkg *interleave_pkg;
  251. u8 max_sad;
  252. u8 max_interleave;
  253. u8 (*get_node_id)(struct sbridge_pvt *pvt);
  254. enum mem_type (*get_memory_type)(struct sbridge_pvt *pvt);
  255. enum dev_type (*get_width)(struct sbridge_pvt *pvt, u32 mtr);
  256. struct pci_dev *pci_vtd;
  257. };
  258. struct sbridge_channel {
  259. u32 ranks;
  260. u32 dimms;
  261. };
  262. struct pci_id_descr {
  263. int dev_id;
  264. int optional;
  265. };
  266. struct pci_id_table {
  267. const struct pci_id_descr *descr;
  268. int n_devs;
  269. enum type type;
  270. };
  271. struct sbridge_dev {
  272. struct list_head list;
  273. u8 bus, mc;
  274. u8 node_id, source_id;
  275. struct pci_dev **pdev;
  276. int n_devs;
  277. struct mem_ctl_info *mci;
  278. };
  279. struct knl_pvt {
  280. struct pci_dev *pci_cha[KNL_MAX_CHAS];
  281. struct pci_dev *pci_channel[KNL_MAX_CHANNELS];
  282. struct pci_dev *pci_mc0;
  283. struct pci_dev *pci_mc1;
  284. struct pci_dev *pci_mc0_misc;
  285. struct pci_dev *pci_mc1_misc;
  286. struct pci_dev *pci_mc_info; /* tolm, tohm */
  287. };
  288. struct sbridge_pvt {
  289. struct pci_dev *pci_ta, *pci_ddrio, *pci_ras;
  290. struct pci_dev *pci_sad0, *pci_sad1;
  291. struct pci_dev *pci_ha0, *pci_ha1;
  292. struct pci_dev *pci_br0, *pci_br1;
  293. struct pci_dev *pci_ha1_ta;
  294. struct pci_dev *pci_tad[NUM_CHANNELS];
  295. struct sbridge_dev *sbridge_dev;
  296. struct sbridge_info info;
  297. struct sbridge_channel channel[NUM_CHANNELS];
  298. /* Memory type detection */
  299. bool is_mirrored, is_lockstep, is_close_pg;
  300. bool is_chan_hash;
  301. /* Memory description */
  302. u64 tolm, tohm;
  303. struct knl_pvt knl;
  304. };
  305. #define PCI_DESCR(device_id, opt) \
  306. .dev_id = (device_id), \
  307. .optional = opt
  308. static const struct pci_id_descr pci_dev_descr_sbridge[] = {
  309. /* Processor Home Agent */
  310. { PCI_DESCR(PCI_DEVICE_ID_INTEL_SBRIDGE_IMC_HA0, 0) },
  311. /* Memory controller */
  312. { PCI_DESCR(PCI_DEVICE_ID_INTEL_SBRIDGE_IMC_TA, 0) },
  313. { PCI_DESCR(PCI_DEVICE_ID_INTEL_SBRIDGE_IMC_RAS, 0) },
  314. { PCI_DESCR(PCI_DEVICE_ID_INTEL_SBRIDGE_IMC_TAD0, 0) },
  315. { PCI_DESCR(PCI_DEVICE_ID_INTEL_SBRIDGE_IMC_TAD1, 0) },
  316. { PCI_DESCR(PCI_DEVICE_ID_INTEL_SBRIDGE_IMC_TAD2, 0) },
  317. { PCI_DESCR(PCI_DEVICE_ID_INTEL_SBRIDGE_IMC_TAD3, 0) },
  318. { PCI_DESCR(PCI_DEVICE_ID_INTEL_SBRIDGE_IMC_DDRIO, 1) },
  319. /* System Address Decoder */
  320. { PCI_DESCR(PCI_DEVICE_ID_INTEL_SBRIDGE_SAD0, 0) },
  321. { PCI_DESCR(PCI_DEVICE_ID_INTEL_SBRIDGE_SAD1, 0) },
  322. /* Broadcast Registers */
  323. { PCI_DESCR(PCI_DEVICE_ID_INTEL_SBRIDGE_BR, 0) },
  324. };
  325. #define PCI_ID_TABLE_ENTRY(A, T) { \
  326. .descr = A, \
  327. .n_devs = ARRAY_SIZE(A), \
  328. .type = T \
  329. }
  330. static const struct pci_id_table pci_dev_descr_sbridge_table[] = {
  331. PCI_ID_TABLE_ENTRY(pci_dev_descr_sbridge, SANDY_BRIDGE),
  332. {0,} /* 0 terminated list. */
  333. };
  334. /* This changes depending if 1HA or 2HA:
  335. * 1HA:
  336. * 0x0eb8 (17.0) is DDRIO0
  337. * 2HA:
  338. * 0x0ebc (17.4) is DDRIO0
  339. */
  340. #define PCI_DEVICE_ID_INTEL_IBRIDGE_IMC_1HA_DDRIO0 0x0eb8
  341. #define PCI_DEVICE_ID_INTEL_IBRIDGE_IMC_2HA_DDRIO0 0x0ebc
  342. /* pci ids */
  343. #define PCI_DEVICE_ID_INTEL_IBRIDGE_IMC_HA0 0x0ea0
  344. #define PCI_DEVICE_ID_INTEL_IBRIDGE_IMC_HA0_TA 0x0ea8
  345. #define PCI_DEVICE_ID_INTEL_IBRIDGE_IMC_HA0_RAS 0x0e71
  346. #define PCI_DEVICE_ID_INTEL_IBRIDGE_IMC_HA0_TAD0 0x0eaa
  347. #define PCI_DEVICE_ID_INTEL_IBRIDGE_IMC_HA0_TAD1 0x0eab
  348. #define PCI_DEVICE_ID_INTEL_IBRIDGE_IMC_HA0_TAD2 0x0eac
  349. #define PCI_DEVICE_ID_INTEL_IBRIDGE_IMC_HA0_TAD3 0x0ead
  350. #define PCI_DEVICE_ID_INTEL_IBRIDGE_SAD 0x0ec8
  351. #define PCI_DEVICE_ID_INTEL_IBRIDGE_BR0 0x0ec9
  352. #define PCI_DEVICE_ID_INTEL_IBRIDGE_BR1 0x0eca
  353. #define PCI_DEVICE_ID_INTEL_IBRIDGE_IMC_HA1 0x0e60
  354. #define PCI_DEVICE_ID_INTEL_IBRIDGE_IMC_HA1_TA 0x0e68
  355. #define PCI_DEVICE_ID_INTEL_IBRIDGE_IMC_HA1_RAS 0x0e79
  356. #define PCI_DEVICE_ID_INTEL_IBRIDGE_IMC_HA1_TAD0 0x0e6a
  357. #define PCI_DEVICE_ID_INTEL_IBRIDGE_IMC_HA1_TAD1 0x0e6b
  358. #define PCI_DEVICE_ID_INTEL_IBRIDGE_IMC_HA1_TAD2 0x0e6c
  359. #define PCI_DEVICE_ID_INTEL_IBRIDGE_IMC_HA1_TAD3 0x0e6d
  360. static const struct pci_id_descr pci_dev_descr_ibridge[] = {
  361. /* Processor Home Agent */
  362. { PCI_DESCR(PCI_DEVICE_ID_INTEL_IBRIDGE_IMC_HA0, 0) },
  363. /* Memory controller */
  364. { PCI_DESCR(PCI_DEVICE_ID_INTEL_IBRIDGE_IMC_HA0_TA, 0) },
  365. { PCI_DESCR(PCI_DEVICE_ID_INTEL_IBRIDGE_IMC_HA0_RAS, 0) },
  366. { PCI_DESCR(PCI_DEVICE_ID_INTEL_IBRIDGE_IMC_HA0_TAD0, 0) },
  367. { PCI_DESCR(PCI_DEVICE_ID_INTEL_IBRIDGE_IMC_HA0_TAD1, 0) },
  368. { PCI_DESCR(PCI_DEVICE_ID_INTEL_IBRIDGE_IMC_HA0_TAD2, 0) },
  369. { PCI_DESCR(PCI_DEVICE_ID_INTEL_IBRIDGE_IMC_HA0_TAD3, 0) },
  370. /* System Address Decoder */
  371. { PCI_DESCR(PCI_DEVICE_ID_INTEL_IBRIDGE_SAD, 0) },
  372. /* Broadcast Registers */
  373. { PCI_DESCR(PCI_DEVICE_ID_INTEL_IBRIDGE_BR0, 1) },
  374. { PCI_DESCR(PCI_DEVICE_ID_INTEL_IBRIDGE_BR1, 0) },
  375. /* Optional, mode 2HA */
  376. { PCI_DESCR(PCI_DEVICE_ID_INTEL_IBRIDGE_IMC_HA1, 1) },
  377. #if 0
  378. { PCI_DESCR(PCI_DEVICE_ID_INTEL_IBRIDGE_IMC_HA1_TA, 1) },
  379. { PCI_DESCR(PCI_DEVICE_ID_INTEL_IBRIDGE_IMC_HA1_RAS, 1) },
  380. #endif
  381. { PCI_DESCR(PCI_DEVICE_ID_INTEL_IBRIDGE_IMC_HA1_TAD0, 1) },
  382. { PCI_DESCR(PCI_DEVICE_ID_INTEL_IBRIDGE_IMC_HA1_TAD1, 1) },
  383. { PCI_DESCR(PCI_DEVICE_ID_INTEL_IBRIDGE_IMC_HA1_TAD2, 1) },
  384. { PCI_DESCR(PCI_DEVICE_ID_INTEL_IBRIDGE_IMC_HA1_TAD3, 1) },
  385. { PCI_DESCR(PCI_DEVICE_ID_INTEL_IBRIDGE_IMC_1HA_DDRIO0, 1) },
  386. { PCI_DESCR(PCI_DEVICE_ID_INTEL_IBRIDGE_IMC_2HA_DDRIO0, 1) },
  387. };
  388. static const struct pci_id_table pci_dev_descr_ibridge_table[] = {
  389. PCI_ID_TABLE_ENTRY(pci_dev_descr_ibridge, IVY_BRIDGE),
  390. {0,} /* 0 terminated list. */
  391. };
  392. /* Haswell support */
  393. /* EN processor:
  394. * - 1 IMC
  395. * - 3 DDR3 channels, 2 DPC per channel
  396. * EP processor:
  397. * - 1 or 2 IMC
  398. * - 4 DDR4 channels, 3 DPC per channel
  399. * EP 4S processor:
  400. * - 2 IMC
  401. * - 4 DDR4 channels, 3 DPC per channel
  402. * EX processor:
  403. * - 2 IMC
  404. * - each IMC interfaces with a SMI 2 channel
  405. * - each SMI channel interfaces with a scalable memory buffer
  406. * - each scalable memory buffer supports 4 DDR3/DDR4 channels, 3 DPC
  407. */
  408. #define HASWELL_DDRCRCLKCONTROLS 0xa10 /* Ditto on Broadwell */
  409. #define HASWELL_HASYSDEFEATURE2 0x84
  410. #define PCI_DEVICE_ID_INTEL_HASWELL_IMC_VTD_MISC 0x2f28
  411. #define PCI_DEVICE_ID_INTEL_HASWELL_IMC_HA0 0x2fa0
  412. #define PCI_DEVICE_ID_INTEL_HASWELL_IMC_HA1 0x2f60
  413. #define PCI_DEVICE_ID_INTEL_HASWELL_IMC_HA0_TA 0x2fa8
  414. #define PCI_DEVICE_ID_INTEL_HASWELL_IMC_HA0_THERMAL 0x2f71
  415. #define PCI_DEVICE_ID_INTEL_HASWELL_IMC_HA1_TA 0x2f68
  416. #define PCI_DEVICE_ID_INTEL_HASWELL_IMC_HA1_THERMAL 0x2f79
  417. #define PCI_DEVICE_ID_INTEL_HASWELL_IMC_CBO_SAD0 0x2ffc
  418. #define PCI_DEVICE_ID_INTEL_HASWELL_IMC_CBO_SAD1 0x2ffd
  419. #define PCI_DEVICE_ID_INTEL_HASWELL_IMC_HA0_TAD0 0x2faa
  420. #define PCI_DEVICE_ID_INTEL_HASWELL_IMC_HA0_TAD1 0x2fab
  421. #define PCI_DEVICE_ID_INTEL_HASWELL_IMC_HA0_TAD2 0x2fac
  422. #define PCI_DEVICE_ID_INTEL_HASWELL_IMC_HA0_TAD3 0x2fad
  423. #define PCI_DEVICE_ID_INTEL_HASWELL_IMC_HA1_TAD0 0x2f6a
  424. #define PCI_DEVICE_ID_INTEL_HASWELL_IMC_HA1_TAD1 0x2f6b
  425. #define PCI_DEVICE_ID_INTEL_HASWELL_IMC_HA1_TAD2 0x2f6c
  426. #define PCI_DEVICE_ID_INTEL_HASWELL_IMC_HA1_TAD3 0x2f6d
  427. #define PCI_DEVICE_ID_INTEL_HASWELL_IMC_DDRIO0 0x2fbd
  428. #define PCI_DEVICE_ID_INTEL_HASWELL_IMC_DDRIO1 0x2fbf
  429. #define PCI_DEVICE_ID_INTEL_HASWELL_IMC_DDRIO2 0x2fb9
  430. #define PCI_DEVICE_ID_INTEL_HASWELL_IMC_DDRIO3 0x2fbb
  431. static const struct pci_id_descr pci_dev_descr_haswell[] = {
  432. /* first item must be the HA */
  433. { PCI_DESCR(PCI_DEVICE_ID_INTEL_HASWELL_IMC_HA0, 0) },
  434. { PCI_DESCR(PCI_DEVICE_ID_INTEL_HASWELL_IMC_CBO_SAD0, 0) },
  435. { PCI_DESCR(PCI_DEVICE_ID_INTEL_HASWELL_IMC_CBO_SAD1, 0) },
  436. { PCI_DESCR(PCI_DEVICE_ID_INTEL_HASWELL_IMC_HA1, 1) },
  437. { PCI_DESCR(PCI_DEVICE_ID_INTEL_HASWELL_IMC_HA0_TA, 0) },
  438. { PCI_DESCR(PCI_DEVICE_ID_INTEL_HASWELL_IMC_HA0_THERMAL, 0) },
  439. { PCI_DESCR(PCI_DEVICE_ID_INTEL_HASWELL_IMC_HA0_TAD0, 0) },
  440. { PCI_DESCR(PCI_DEVICE_ID_INTEL_HASWELL_IMC_HA0_TAD1, 0) },
  441. { PCI_DESCR(PCI_DEVICE_ID_INTEL_HASWELL_IMC_HA0_TAD2, 1) },
  442. { PCI_DESCR(PCI_DEVICE_ID_INTEL_HASWELL_IMC_HA0_TAD3, 1) },
  443. { PCI_DESCR(PCI_DEVICE_ID_INTEL_HASWELL_IMC_DDRIO0, 1) },
  444. { PCI_DESCR(PCI_DEVICE_ID_INTEL_HASWELL_IMC_DDRIO1, 1) },
  445. { PCI_DESCR(PCI_DEVICE_ID_INTEL_HASWELL_IMC_DDRIO2, 1) },
  446. { PCI_DESCR(PCI_DEVICE_ID_INTEL_HASWELL_IMC_DDRIO3, 1) },
  447. { PCI_DESCR(PCI_DEVICE_ID_INTEL_HASWELL_IMC_HA1_TA, 1) },
  448. { PCI_DESCR(PCI_DEVICE_ID_INTEL_HASWELL_IMC_HA1_THERMAL, 1) },
  449. { PCI_DESCR(PCI_DEVICE_ID_INTEL_HASWELL_IMC_HA1_TAD0, 1) },
  450. { PCI_DESCR(PCI_DEVICE_ID_INTEL_HASWELL_IMC_HA1_TAD1, 1) },
  451. { PCI_DESCR(PCI_DEVICE_ID_INTEL_HASWELL_IMC_HA1_TAD2, 1) },
  452. { PCI_DESCR(PCI_DEVICE_ID_INTEL_HASWELL_IMC_HA1_TAD3, 1) },
  453. };
  454. static const struct pci_id_table pci_dev_descr_haswell_table[] = {
  455. PCI_ID_TABLE_ENTRY(pci_dev_descr_haswell, HASWELL),
  456. {0,} /* 0 terminated list. */
  457. };
  458. /* Knight's Landing Support */
  459. /*
  460. * KNL's memory channels are swizzled between memory controllers.
  461. * MC0 is mapped to CH3,4,5 and MC1 is mapped to CH0,1,2
  462. */
  463. #define knl_channel_remap(mc, chan) ((mc) ? (chan) : (chan) + 3)
  464. /* Memory controller, TAD tables, error injection - 2-8-0, 2-9-0 (2 of these) */
  465. #define PCI_DEVICE_ID_INTEL_KNL_IMC_MC 0x7840
  466. /* DRAM channel stuff; bank addrs, dimmmtr, etc.. 2-8-2 - 2-9-4 (6 of these) */
  467. #define PCI_DEVICE_ID_INTEL_KNL_IMC_CHANNEL 0x7843
  468. /* kdrwdbu TAD limits/offsets, MCMTR - 2-10-1, 2-11-1 (2 of these) */
  469. #define PCI_DEVICE_ID_INTEL_KNL_IMC_TA 0x7844
  470. /* CHA broadcast registers, dram rules - 1-29-0 (1 of these) */
  471. #define PCI_DEVICE_ID_INTEL_KNL_IMC_SAD0 0x782a
  472. /* SAD target - 1-29-1 (1 of these) */
  473. #define PCI_DEVICE_ID_INTEL_KNL_IMC_SAD1 0x782b
  474. /* Caching / Home Agent */
  475. #define PCI_DEVICE_ID_INTEL_KNL_IMC_CHA 0x782c
  476. /* Device with TOLM and TOHM, 0-5-0 (1 of these) */
  477. #define PCI_DEVICE_ID_INTEL_KNL_IMC_TOLHM 0x7810
  478. /*
  479. * KNL differs from SB, IB, and Haswell in that it has multiple
  480. * instances of the same device with the same device ID, so we handle that
  481. * by creating as many copies in the table as we expect to find.
  482. * (Like device ID must be grouped together.)
  483. */
  484. static const struct pci_id_descr pci_dev_descr_knl[] = {
  485. [0] = { PCI_DESCR(PCI_DEVICE_ID_INTEL_KNL_IMC_SAD0, 0) },
  486. [1] = { PCI_DESCR(PCI_DEVICE_ID_INTEL_KNL_IMC_SAD1, 0) },
  487. [2 ... 3] = { PCI_DESCR(PCI_DEVICE_ID_INTEL_KNL_IMC_MC, 0)},
  488. [4 ... 41] = { PCI_DESCR(PCI_DEVICE_ID_INTEL_KNL_IMC_CHA, 0) },
  489. [42 ... 47] = { PCI_DESCR(PCI_DEVICE_ID_INTEL_KNL_IMC_CHANNEL, 0) },
  490. [48] = { PCI_DESCR(PCI_DEVICE_ID_INTEL_KNL_IMC_TA, 0) },
  491. [49] = { PCI_DESCR(PCI_DEVICE_ID_INTEL_KNL_IMC_TOLHM, 0) },
  492. };
  493. static const struct pci_id_table pci_dev_descr_knl_table[] = {
  494. PCI_ID_TABLE_ENTRY(pci_dev_descr_knl, KNIGHTS_LANDING),
  495. {0,}
  496. };
  497. /*
  498. * Broadwell support
  499. *
  500. * DE processor:
  501. * - 1 IMC
  502. * - 2 DDR3 channels, 2 DPC per channel
  503. * EP processor:
  504. * - 1 or 2 IMC
  505. * - 4 DDR4 channels, 3 DPC per channel
  506. * EP 4S processor:
  507. * - 2 IMC
  508. * - 4 DDR4 channels, 3 DPC per channel
  509. * EX processor:
  510. * - 2 IMC
  511. * - each IMC interfaces with a SMI 2 channel
  512. * - each SMI channel interfaces with a scalable memory buffer
  513. * - each scalable memory buffer supports 4 DDR3/DDR4 channels, 3 DPC
  514. */
  515. #define PCI_DEVICE_ID_INTEL_BROADWELL_IMC_VTD_MISC 0x6f28
  516. #define PCI_DEVICE_ID_INTEL_BROADWELL_IMC_HA0 0x6fa0
  517. #define PCI_DEVICE_ID_INTEL_BROADWELL_IMC_HA1 0x6f60
  518. #define PCI_DEVICE_ID_INTEL_BROADWELL_IMC_HA0_TA 0x6fa8
  519. #define PCI_DEVICE_ID_INTEL_BROADWELL_IMC_HA0_THERMAL 0x6f71
  520. #define PCI_DEVICE_ID_INTEL_BROADWELL_IMC_HA1_TA 0x6f68
  521. #define PCI_DEVICE_ID_INTEL_BROADWELL_IMC_HA1_THERMAL 0x6f79
  522. #define PCI_DEVICE_ID_INTEL_BROADWELL_IMC_CBO_SAD0 0x6ffc
  523. #define PCI_DEVICE_ID_INTEL_BROADWELL_IMC_CBO_SAD1 0x6ffd
  524. #define PCI_DEVICE_ID_INTEL_BROADWELL_IMC_HA0_TAD0 0x6faa
  525. #define PCI_DEVICE_ID_INTEL_BROADWELL_IMC_HA0_TAD1 0x6fab
  526. #define PCI_DEVICE_ID_INTEL_BROADWELL_IMC_HA0_TAD2 0x6fac
  527. #define PCI_DEVICE_ID_INTEL_BROADWELL_IMC_HA0_TAD3 0x6fad
  528. #define PCI_DEVICE_ID_INTEL_BROADWELL_IMC_HA1_TAD0 0x6f6a
  529. #define PCI_DEVICE_ID_INTEL_BROADWELL_IMC_HA1_TAD1 0x6f6b
  530. #define PCI_DEVICE_ID_INTEL_BROADWELL_IMC_HA1_TAD2 0x6f6c
  531. #define PCI_DEVICE_ID_INTEL_BROADWELL_IMC_HA1_TAD3 0x6f6d
  532. #define PCI_DEVICE_ID_INTEL_BROADWELL_IMC_DDRIO0 0x6faf
  533. static const struct pci_id_descr pci_dev_descr_broadwell[] = {
  534. /* first item must be the HA */
  535. { PCI_DESCR(PCI_DEVICE_ID_INTEL_BROADWELL_IMC_HA0, 0) },
  536. { PCI_DESCR(PCI_DEVICE_ID_INTEL_BROADWELL_IMC_CBO_SAD0, 0) },
  537. { PCI_DESCR(PCI_DEVICE_ID_INTEL_BROADWELL_IMC_CBO_SAD1, 0) },
  538. { PCI_DESCR(PCI_DEVICE_ID_INTEL_BROADWELL_IMC_HA1, 1) },
  539. { PCI_DESCR(PCI_DEVICE_ID_INTEL_BROADWELL_IMC_HA0_TA, 0) },
  540. { PCI_DESCR(PCI_DEVICE_ID_INTEL_BROADWELL_IMC_HA0_THERMAL, 0) },
  541. { PCI_DESCR(PCI_DEVICE_ID_INTEL_BROADWELL_IMC_HA0_TAD0, 0) },
  542. { PCI_DESCR(PCI_DEVICE_ID_INTEL_BROADWELL_IMC_HA0_TAD1, 0) },
  543. { PCI_DESCR(PCI_DEVICE_ID_INTEL_BROADWELL_IMC_HA0_TAD2, 1) },
  544. { PCI_DESCR(PCI_DEVICE_ID_INTEL_BROADWELL_IMC_HA0_TAD3, 1) },
  545. { PCI_DESCR(PCI_DEVICE_ID_INTEL_BROADWELL_IMC_DDRIO0, 1) },
  546. { PCI_DESCR(PCI_DEVICE_ID_INTEL_BROADWELL_IMC_HA1_TA, 1) },
  547. { PCI_DESCR(PCI_DEVICE_ID_INTEL_BROADWELL_IMC_HA1_THERMAL, 1) },
  548. { PCI_DESCR(PCI_DEVICE_ID_INTEL_BROADWELL_IMC_HA1_TAD0, 1) },
  549. { PCI_DESCR(PCI_DEVICE_ID_INTEL_BROADWELL_IMC_HA1_TAD1, 1) },
  550. { PCI_DESCR(PCI_DEVICE_ID_INTEL_BROADWELL_IMC_HA1_TAD2, 1) },
  551. { PCI_DESCR(PCI_DEVICE_ID_INTEL_BROADWELL_IMC_HA1_TAD3, 1) },
  552. };
  553. static const struct pci_id_table pci_dev_descr_broadwell_table[] = {
  554. PCI_ID_TABLE_ENTRY(pci_dev_descr_broadwell, BROADWELL),
  555. {0,} /* 0 terminated list. */
  556. };
  557. /****************************************************************************
  558. Ancillary status routines
  559. ****************************************************************************/
  560. static inline int numrank(enum type type, u32 mtr)
  561. {
  562. int ranks = (1 << RANK_CNT_BITS(mtr));
  563. int max = 4;
  564. if (type == HASWELL || type == BROADWELL || type == KNIGHTS_LANDING)
  565. max = 8;
  566. if (ranks > max) {
  567. edac_dbg(0, "Invalid number of ranks: %d (max = %i) raw value = %x (%04x)\n",
  568. ranks, max, (unsigned int)RANK_CNT_BITS(mtr), mtr);
  569. return -EINVAL;
  570. }
  571. return ranks;
  572. }
  573. static inline int numrow(u32 mtr)
  574. {
  575. int rows = (RANK_WIDTH_BITS(mtr) + 12);
  576. if (rows < 13 || rows > 18) {
  577. edac_dbg(0, "Invalid number of rows: %d (should be between 14 and 17) raw value = %x (%04x)\n",
  578. rows, (unsigned int)RANK_WIDTH_BITS(mtr), mtr);
  579. return -EINVAL;
  580. }
  581. return 1 << rows;
  582. }
  583. static inline int numcol(u32 mtr)
  584. {
  585. int cols = (COL_WIDTH_BITS(mtr) + 10);
  586. if (cols > 12) {
  587. edac_dbg(0, "Invalid number of cols: %d (max = 4) raw value = %x (%04x)\n",
  588. cols, (unsigned int)COL_WIDTH_BITS(mtr), mtr);
  589. return -EINVAL;
  590. }
  591. return 1 << cols;
  592. }
  593. static struct sbridge_dev *get_sbridge_dev(u8 bus, int multi_bus)
  594. {
  595. struct sbridge_dev *sbridge_dev;
  596. /*
  597. * If we have devices scattered across several busses that pertain
  598. * to the same memory controller, we'll lump them all together.
  599. */
  600. if (multi_bus) {
  601. return list_first_entry_or_null(&sbridge_edac_list,
  602. struct sbridge_dev, list);
  603. }
  604. list_for_each_entry(sbridge_dev, &sbridge_edac_list, list) {
  605. if (sbridge_dev->bus == bus)
  606. return sbridge_dev;
  607. }
  608. return NULL;
  609. }
  610. static struct sbridge_dev *alloc_sbridge_dev(u8 bus,
  611. const struct pci_id_table *table)
  612. {
  613. struct sbridge_dev *sbridge_dev;
  614. sbridge_dev = kzalloc(sizeof(*sbridge_dev), GFP_KERNEL);
  615. if (!sbridge_dev)
  616. return NULL;
  617. sbridge_dev->pdev = kzalloc(sizeof(*sbridge_dev->pdev) * table->n_devs,
  618. GFP_KERNEL);
  619. if (!sbridge_dev->pdev) {
  620. kfree(sbridge_dev);
  621. return NULL;
  622. }
  623. sbridge_dev->bus = bus;
  624. sbridge_dev->n_devs = table->n_devs;
  625. list_add_tail(&sbridge_dev->list, &sbridge_edac_list);
  626. return sbridge_dev;
  627. }
  628. static void free_sbridge_dev(struct sbridge_dev *sbridge_dev)
  629. {
  630. list_del(&sbridge_dev->list);
  631. kfree(sbridge_dev->pdev);
  632. kfree(sbridge_dev);
  633. }
  634. static u64 sbridge_get_tolm(struct sbridge_pvt *pvt)
  635. {
  636. u32 reg;
  637. /* Address range is 32:28 */
  638. pci_read_config_dword(pvt->pci_sad1, TOLM, &reg);
  639. return GET_TOLM(reg);
  640. }
  641. static u64 sbridge_get_tohm(struct sbridge_pvt *pvt)
  642. {
  643. u32 reg;
  644. pci_read_config_dword(pvt->pci_sad1, TOHM, &reg);
  645. return GET_TOHM(reg);
  646. }
  647. static u64 ibridge_get_tolm(struct sbridge_pvt *pvt)
  648. {
  649. u32 reg;
  650. pci_read_config_dword(pvt->pci_br1, TOLM, &reg);
  651. return GET_TOLM(reg);
  652. }
  653. static u64 ibridge_get_tohm(struct sbridge_pvt *pvt)
  654. {
  655. u32 reg;
  656. pci_read_config_dword(pvt->pci_br1, TOHM, &reg);
  657. return GET_TOHM(reg);
  658. }
  659. static u64 rir_limit(u32 reg)
  660. {
  661. return ((u64)GET_BITFIELD(reg, 1, 10) << 29) | 0x1fffffff;
  662. }
  663. static u64 sad_limit(u32 reg)
  664. {
  665. return (GET_BITFIELD(reg, 6, 25) << 26) | 0x3ffffff;
  666. }
  667. static u32 interleave_mode(u32 reg)
  668. {
  669. return GET_BITFIELD(reg, 1, 1);
  670. }
  671. char *show_interleave_mode(u32 reg)
  672. {
  673. return interleave_mode(reg) ? "8:6" : "[8:6]XOR[18:16]";
  674. }
  675. static u32 dram_attr(u32 reg)
  676. {
  677. return GET_BITFIELD(reg, 2, 3);
  678. }
  679. static u64 knl_sad_limit(u32 reg)
  680. {
  681. return (GET_BITFIELD(reg, 7, 26) << 26) | 0x3ffffff;
  682. }
  683. static u32 knl_interleave_mode(u32 reg)
  684. {
  685. return GET_BITFIELD(reg, 1, 2);
  686. }
  687. static char *knl_show_interleave_mode(u32 reg)
  688. {
  689. char *s;
  690. switch (knl_interleave_mode(reg)) {
  691. case 0:
  692. s = "use address bits [8:6]";
  693. break;
  694. case 1:
  695. s = "use address bits [10:8]";
  696. break;
  697. case 2:
  698. s = "use address bits [14:12]";
  699. break;
  700. case 3:
  701. s = "use address bits [32:30]";
  702. break;
  703. default:
  704. WARN_ON(1);
  705. break;
  706. }
  707. return s;
  708. }
  709. static u32 dram_attr_knl(u32 reg)
  710. {
  711. return GET_BITFIELD(reg, 3, 4);
  712. }
  713. static enum mem_type get_memory_type(struct sbridge_pvt *pvt)
  714. {
  715. u32 reg;
  716. enum mem_type mtype;
  717. if (pvt->pci_ddrio) {
  718. pci_read_config_dword(pvt->pci_ddrio, pvt->info.rankcfgr,
  719. &reg);
  720. if (GET_BITFIELD(reg, 11, 11))
  721. /* FIXME: Can also be LRDIMM */
  722. mtype = MEM_RDDR3;
  723. else
  724. mtype = MEM_DDR3;
  725. } else
  726. mtype = MEM_UNKNOWN;
  727. return mtype;
  728. }
  729. static enum mem_type haswell_get_memory_type(struct sbridge_pvt *pvt)
  730. {
  731. u32 reg;
  732. bool registered = false;
  733. enum mem_type mtype = MEM_UNKNOWN;
  734. if (!pvt->pci_ddrio)
  735. goto out;
  736. pci_read_config_dword(pvt->pci_ddrio,
  737. HASWELL_DDRCRCLKCONTROLS, &reg);
  738. /* Is_Rdimm */
  739. if (GET_BITFIELD(reg, 16, 16))
  740. registered = true;
  741. pci_read_config_dword(pvt->pci_ta, MCMTR, &reg);
  742. if (GET_BITFIELD(reg, 14, 14)) {
  743. if (registered)
  744. mtype = MEM_RDDR4;
  745. else
  746. mtype = MEM_DDR4;
  747. } else {
  748. if (registered)
  749. mtype = MEM_RDDR3;
  750. else
  751. mtype = MEM_DDR3;
  752. }
  753. out:
  754. return mtype;
  755. }
  756. static enum dev_type knl_get_width(struct sbridge_pvt *pvt, u32 mtr)
  757. {
  758. /* for KNL value is fixed */
  759. return DEV_X16;
  760. }
  761. static enum dev_type sbridge_get_width(struct sbridge_pvt *pvt, u32 mtr)
  762. {
  763. /* there's no way to figure out */
  764. return DEV_UNKNOWN;
  765. }
  766. static enum dev_type __ibridge_get_width(u32 mtr)
  767. {
  768. enum dev_type type;
  769. switch (mtr) {
  770. case 3:
  771. type = DEV_UNKNOWN;
  772. break;
  773. case 2:
  774. type = DEV_X16;
  775. break;
  776. case 1:
  777. type = DEV_X8;
  778. break;
  779. case 0:
  780. type = DEV_X4;
  781. break;
  782. }
  783. return type;
  784. }
  785. static enum dev_type ibridge_get_width(struct sbridge_pvt *pvt, u32 mtr)
  786. {
  787. /*
  788. * ddr3_width on the documentation but also valid for DDR4 on
  789. * Haswell
  790. */
  791. return __ibridge_get_width(GET_BITFIELD(mtr, 7, 8));
  792. }
  793. static enum dev_type broadwell_get_width(struct sbridge_pvt *pvt, u32 mtr)
  794. {
  795. /* ddr3_width on the documentation but also valid for DDR4 */
  796. return __ibridge_get_width(GET_BITFIELD(mtr, 8, 9));
  797. }
  798. static enum mem_type knl_get_memory_type(struct sbridge_pvt *pvt)
  799. {
  800. /* DDR4 RDIMMS and LRDIMMS are supported */
  801. return MEM_RDDR4;
  802. }
  803. static u8 get_node_id(struct sbridge_pvt *pvt)
  804. {
  805. u32 reg;
  806. pci_read_config_dword(pvt->pci_br0, SAD_CONTROL, &reg);
  807. return GET_BITFIELD(reg, 0, 2);
  808. }
  809. static u8 haswell_get_node_id(struct sbridge_pvt *pvt)
  810. {
  811. u32 reg;
  812. pci_read_config_dword(pvt->pci_sad1, SAD_CONTROL, &reg);
  813. return GET_BITFIELD(reg, 0, 3);
  814. }
  815. static u8 knl_get_node_id(struct sbridge_pvt *pvt)
  816. {
  817. u32 reg;
  818. pci_read_config_dword(pvt->pci_sad1, SAD_CONTROL, &reg);
  819. return GET_BITFIELD(reg, 0, 2);
  820. }
  821. static u64 haswell_get_tolm(struct sbridge_pvt *pvt)
  822. {
  823. u32 reg;
  824. pci_read_config_dword(pvt->info.pci_vtd, HASWELL_TOLM, &reg);
  825. return (GET_BITFIELD(reg, 26, 31) << 26) | 0x3ffffff;
  826. }
  827. static u64 haswell_get_tohm(struct sbridge_pvt *pvt)
  828. {
  829. u64 rc;
  830. u32 reg;
  831. pci_read_config_dword(pvt->info.pci_vtd, HASWELL_TOHM_0, &reg);
  832. rc = GET_BITFIELD(reg, 26, 31);
  833. pci_read_config_dword(pvt->info.pci_vtd, HASWELL_TOHM_1, &reg);
  834. rc = ((reg << 6) | rc) << 26;
  835. return rc | 0x1ffffff;
  836. }
  837. static u64 knl_get_tolm(struct sbridge_pvt *pvt)
  838. {
  839. u32 reg;
  840. pci_read_config_dword(pvt->knl.pci_mc_info, KNL_TOLM, &reg);
  841. return (GET_BITFIELD(reg, 26, 31) << 26) | 0x3ffffff;
  842. }
  843. static u64 knl_get_tohm(struct sbridge_pvt *pvt)
  844. {
  845. u64 rc;
  846. u32 reg_lo, reg_hi;
  847. pci_read_config_dword(pvt->knl.pci_mc_info, KNL_TOHM_0, &reg_lo);
  848. pci_read_config_dword(pvt->knl.pci_mc_info, KNL_TOHM_1, &reg_hi);
  849. rc = ((u64)reg_hi << 32) | reg_lo;
  850. return rc | 0x3ffffff;
  851. }
  852. static u64 haswell_rir_limit(u32 reg)
  853. {
  854. return (((u64)GET_BITFIELD(reg, 1, 11) + 1) << 29) - 1;
  855. }
  856. static inline u8 sad_pkg_socket(u8 pkg)
  857. {
  858. /* on Ivy Bridge, nodeID is SASS, where A is HA and S is node id */
  859. return ((pkg >> 3) << 2) | (pkg & 0x3);
  860. }
  861. static inline u8 sad_pkg_ha(u8 pkg)
  862. {
  863. return (pkg >> 2) & 0x1;
  864. }
  865. static int haswell_chan_hash(int idx, u64 addr)
  866. {
  867. int i;
  868. /*
  869. * XOR even bits from 12:26 to bit0 of idx,
  870. * odd bits from 13:27 to bit1
  871. */
  872. for (i = 12; i < 28; i += 2)
  873. idx ^= (addr >> i) & 3;
  874. return idx;
  875. }
  876. /****************************************************************************
  877. Memory check routines
  878. ****************************************************************************/
  879. static struct pci_dev *get_pdev_same_bus(u8 bus, u32 id)
  880. {
  881. struct pci_dev *pdev = NULL;
  882. do {
  883. pdev = pci_get_device(PCI_VENDOR_ID_INTEL, id, pdev);
  884. if (pdev && pdev->bus->number == bus)
  885. break;
  886. } while (pdev);
  887. return pdev;
  888. }
  889. /**
  890. * check_if_ecc_is_active() - Checks if ECC is active
  891. * @bus: Device bus
  892. * @type: Memory controller type
  893. * returns: 0 in case ECC is active, -ENODEV if it can't be determined or
  894. * disabled
  895. */
  896. static int check_if_ecc_is_active(const u8 bus, enum type type)
  897. {
  898. struct pci_dev *pdev = NULL;
  899. u32 mcmtr, id;
  900. switch (type) {
  901. case IVY_BRIDGE:
  902. id = PCI_DEVICE_ID_INTEL_IBRIDGE_IMC_HA0_TA;
  903. break;
  904. case HASWELL:
  905. id = PCI_DEVICE_ID_INTEL_HASWELL_IMC_HA0_TA;
  906. break;
  907. case SANDY_BRIDGE:
  908. id = PCI_DEVICE_ID_INTEL_SBRIDGE_IMC_TA;
  909. break;
  910. case BROADWELL:
  911. id = PCI_DEVICE_ID_INTEL_BROADWELL_IMC_HA0_TA;
  912. break;
  913. case KNIGHTS_LANDING:
  914. /*
  915. * KNL doesn't group things by bus the same way
  916. * SB/IB/Haswell does.
  917. */
  918. id = PCI_DEVICE_ID_INTEL_KNL_IMC_TA;
  919. break;
  920. default:
  921. return -ENODEV;
  922. }
  923. if (type != KNIGHTS_LANDING)
  924. pdev = get_pdev_same_bus(bus, id);
  925. else
  926. pdev = pci_get_device(PCI_VENDOR_ID_INTEL, id, 0);
  927. if (!pdev) {
  928. sbridge_printk(KERN_ERR, "Couldn't find PCI device "
  929. "%04x:%04x! on bus %02d\n",
  930. PCI_VENDOR_ID_INTEL, id, bus);
  931. return -ENODEV;
  932. }
  933. pci_read_config_dword(pdev,
  934. type == KNIGHTS_LANDING ? KNL_MCMTR : MCMTR, &mcmtr);
  935. if (!IS_ECC_ENABLED(mcmtr)) {
  936. sbridge_printk(KERN_ERR, "ECC is disabled. Aborting\n");
  937. return -ENODEV;
  938. }
  939. return 0;
  940. }
  941. /* Low bits of TAD limit, and some metadata. */
  942. static const u32 knl_tad_dram_limit_lo[] = {
  943. 0x400, 0x500, 0x600, 0x700,
  944. 0x800, 0x900, 0xa00, 0xb00,
  945. };
  946. /* Low bits of TAD offset. */
  947. static const u32 knl_tad_dram_offset_lo[] = {
  948. 0x404, 0x504, 0x604, 0x704,
  949. 0x804, 0x904, 0xa04, 0xb04,
  950. };
  951. /* High 16 bits of TAD limit and offset. */
  952. static const u32 knl_tad_dram_hi[] = {
  953. 0x408, 0x508, 0x608, 0x708,
  954. 0x808, 0x908, 0xa08, 0xb08,
  955. };
  956. /* Number of ways a tad entry is interleaved. */
  957. static const u32 knl_tad_ways[] = {
  958. 8, 6, 4, 3, 2, 1,
  959. };
  960. /*
  961. * Retrieve the n'th Target Address Decode table entry
  962. * from the memory controller's TAD table.
  963. *
  964. * @pvt: driver private data
  965. * @entry: which entry you want to retrieve
  966. * @mc: which memory controller (0 or 1)
  967. * @offset: output tad range offset
  968. * @limit: output address of first byte above tad range
  969. * @ways: output number of interleave ways
  970. *
  971. * The offset value has curious semantics. It's a sort of running total
  972. * of the sizes of all the memory regions that aren't mapped in this
  973. * tad table.
  974. */
  975. static int knl_get_tad(const struct sbridge_pvt *pvt,
  976. const int entry,
  977. const int mc,
  978. u64 *offset,
  979. u64 *limit,
  980. int *ways)
  981. {
  982. u32 reg_limit_lo, reg_offset_lo, reg_hi;
  983. struct pci_dev *pci_mc;
  984. int way_id;
  985. switch (mc) {
  986. case 0:
  987. pci_mc = pvt->knl.pci_mc0;
  988. break;
  989. case 1:
  990. pci_mc = pvt->knl.pci_mc1;
  991. break;
  992. default:
  993. WARN_ON(1);
  994. return -EINVAL;
  995. }
  996. pci_read_config_dword(pci_mc,
  997. knl_tad_dram_limit_lo[entry], &reg_limit_lo);
  998. pci_read_config_dword(pci_mc,
  999. knl_tad_dram_offset_lo[entry], &reg_offset_lo);
  1000. pci_read_config_dword(pci_mc,
  1001. knl_tad_dram_hi[entry], &reg_hi);
  1002. /* Is this TAD entry enabled? */
  1003. if (!GET_BITFIELD(reg_limit_lo, 0, 0))
  1004. return -ENODEV;
  1005. way_id = GET_BITFIELD(reg_limit_lo, 3, 5);
  1006. if (way_id < ARRAY_SIZE(knl_tad_ways)) {
  1007. *ways = knl_tad_ways[way_id];
  1008. } else {
  1009. *ways = 0;
  1010. sbridge_printk(KERN_ERR,
  1011. "Unexpected value %d in mc_tad_limit_lo wayness field\n",
  1012. way_id);
  1013. return -ENODEV;
  1014. }
  1015. /*
  1016. * The least significant 6 bits of base and limit are truncated.
  1017. * For limit, we fill the missing bits with 1s.
  1018. */
  1019. *offset = ((u64) GET_BITFIELD(reg_offset_lo, 6, 31) << 6) |
  1020. ((u64) GET_BITFIELD(reg_hi, 0, 15) << 32);
  1021. *limit = ((u64) GET_BITFIELD(reg_limit_lo, 6, 31) << 6) | 63 |
  1022. ((u64) GET_BITFIELD(reg_hi, 16, 31) << 32);
  1023. return 0;
  1024. }
  1025. /* Determine which memory controller is responsible for a given channel. */
  1026. static int knl_channel_mc(int channel)
  1027. {
  1028. WARN_ON(channel < 0 || channel >= 6);
  1029. return channel < 3 ? 1 : 0;
  1030. }
  1031. /*
  1032. * Get the Nth entry from EDC_ROUTE_TABLE register.
  1033. * (This is the per-tile mapping of logical interleave targets to
  1034. * physical EDC modules.)
  1035. *
  1036. * entry 0: 0:2
  1037. * 1: 3:5
  1038. * 2: 6:8
  1039. * 3: 9:11
  1040. * 4: 12:14
  1041. * 5: 15:17
  1042. * 6: 18:20
  1043. * 7: 21:23
  1044. * reserved: 24:31
  1045. */
  1046. static u32 knl_get_edc_route(int entry, u32 reg)
  1047. {
  1048. WARN_ON(entry >= KNL_MAX_EDCS);
  1049. return GET_BITFIELD(reg, entry*3, (entry*3)+2);
  1050. }
  1051. /*
  1052. * Get the Nth entry from MC_ROUTE_TABLE register.
  1053. * (This is the per-tile mapping of logical interleave targets to
  1054. * physical DRAM channels modules.)
  1055. *
  1056. * entry 0: mc 0:2 channel 18:19
  1057. * 1: mc 3:5 channel 20:21
  1058. * 2: mc 6:8 channel 22:23
  1059. * 3: mc 9:11 channel 24:25
  1060. * 4: mc 12:14 channel 26:27
  1061. * 5: mc 15:17 channel 28:29
  1062. * reserved: 30:31
  1063. *
  1064. * Though we have 3 bits to identify the MC, we should only see
  1065. * the values 0 or 1.
  1066. */
  1067. static u32 knl_get_mc_route(int entry, u32 reg)
  1068. {
  1069. int mc, chan;
  1070. WARN_ON(entry >= KNL_MAX_CHANNELS);
  1071. mc = GET_BITFIELD(reg, entry*3, (entry*3)+2);
  1072. chan = GET_BITFIELD(reg, (entry*2) + 18, (entry*2) + 18 + 1);
  1073. return knl_channel_remap(mc, chan);
  1074. }
  1075. /*
  1076. * Render the EDC_ROUTE register in human-readable form.
  1077. * Output string s should be at least KNL_MAX_EDCS*2 bytes.
  1078. */
  1079. static void knl_show_edc_route(u32 reg, char *s)
  1080. {
  1081. int i;
  1082. for (i = 0; i < KNL_MAX_EDCS; i++) {
  1083. s[i*2] = knl_get_edc_route(i, reg) + '0';
  1084. s[i*2+1] = '-';
  1085. }
  1086. s[KNL_MAX_EDCS*2 - 1] = '\0';
  1087. }
  1088. /*
  1089. * Render the MC_ROUTE register in human-readable form.
  1090. * Output string s should be at least KNL_MAX_CHANNELS*2 bytes.
  1091. */
  1092. static void knl_show_mc_route(u32 reg, char *s)
  1093. {
  1094. int i;
  1095. for (i = 0; i < KNL_MAX_CHANNELS; i++) {
  1096. s[i*2] = knl_get_mc_route(i, reg) + '0';
  1097. s[i*2+1] = '-';
  1098. }
  1099. s[KNL_MAX_CHANNELS*2 - 1] = '\0';
  1100. }
  1101. #define KNL_EDC_ROUTE 0xb8
  1102. #define KNL_MC_ROUTE 0xb4
  1103. /* Is this dram rule backed by regular DRAM in flat mode? */
  1104. #define KNL_EDRAM(reg) GET_BITFIELD(reg, 29, 29)
  1105. /* Is this dram rule cached? */
  1106. #define KNL_CACHEABLE(reg) GET_BITFIELD(reg, 28, 28)
  1107. /* Is this rule backed by edc ? */
  1108. #define KNL_EDRAM_ONLY(reg) GET_BITFIELD(reg, 29, 29)
  1109. /* Is this rule backed by DRAM, cacheable in EDRAM? */
  1110. #define KNL_CACHEABLE(reg) GET_BITFIELD(reg, 28, 28)
  1111. /* Is this rule mod3? */
  1112. #define KNL_MOD3(reg) GET_BITFIELD(reg, 27, 27)
  1113. /*
  1114. * Figure out how big our RAM modules are.
  1115. *
  1116. * The DIMMMTR register in KNL doesn't tell us the size of the DIMMs, so we
  1117. * have to figure this out from the SAD rules, interleave lists, route tables,
  1118. * and TAD rules.
  1119. *
  1120. * SAD rules can have holes in them (e.g. the 3G-4G hole), so we have to
  1121. * inspect the TAD rules to figure out how large the SAD regions really are.
  1122. *
  1123. * When we know the real size of a SAD region and how many ways it's
  1124. * interleaved, we know the individual contribution of each channel to
  1125. * TAD is size/ways.
  1126. *
  1127. * Finally, we have to check whether each channel participates in each SAD
  1128. * region.
  1129. *
  1130. * Fortunately, KNL only supports one DIMM per channel, so once we know how
  1131. * much memory the channel uses, we know the DIMM is at least that large.
  1132. * (The BIOS might possibly choose not to map all available memory, in which
  1133. * case we will underreport the size of the DIMM.)
  1134. *
  1135. * In theory, we could try to determine the EDC sizes as well, but that would
  1136. * only work in flat mode, not in cache mode.
  1137. *
  1138. * @mc_sizes: Output sizes of channels (must have space for KNL_MAX_CHANNELS
  1139. * elements)
  1140. */
  1141. static int knl_get_dimm_capacity(struct sbridge_pvt *pvt, u64 *mc_sizes)
  1142. {
  1143. u64 sad_base, sad_size, sad_limit = 0;
  1144. u64 tad_base, tad_size, tad_limit, tad_deadspace, tad_livespace;
  1145. int sad_rule = 0;
  1146. int tad_rule = 0;
  1147. int intrlv_ways, tad_ways;
  1148. u32 first_pkg, pkg;
  1149. int i;
  1150. u64 sad_actual_size[2]; /* sad size accounting for holes, per mc */
  1151. u32 dram_rule, interleave_reg;
  1152. u32 mc_route_reg[KNL_MAX_CHAS];
  1153. u32 edc_route_reg[KNL_MAX_CHAS];
  1154. int edram_only;
  1155. char edc_route_string[KNL_MAX_EDCS*2];
  1156. char mc_route_string[KNL_MAX_CHANNELS*2];
  1157. int cur_reg_start;
  1158. int mc;
  1159. int channel;
  1160. int way;
  1161. int participants[KNL_MAX_CHANNELS];
  1162. int participant_count = 0;
  1163. for (i = 0; i < KNL_MAX_CHANNELS; i++)
  1164. mc_sizes[i] = 0;
  1165. /* Read the EDC route table in each CHA. */
  1166. cur_reg_start = 0;
  1167. for (i = 0; i < KNL_MAX_CHAS; i++) {
  1168. pci_read_config_dword(pvt->knl.pci_cha[i],
  1169. KNL_EDC_ROUTE, &edc_route_reg[i]);
  1170. if (i > 0 && edc_route_reg[i] != edc_route_reg[i-1]) {
  1171. knl_show_edc_route(edc_route_reg[i-1],
  1172. edc_route_string);
  1173. if (cur_reg_start == i-1)
  1174. edac_dbg(0, "edc route table for CHA %d: %s\n",
  1175. cur_reg_start, edc_route_string);
  1176. else
  1177. edac_dbg(0, "edc route table for CHA %d-%d: %s\n",
  1178. cur_reg_start, i-1, edc_route_string);
  1179. cur_reg_start = i;
  1180. }
  1181. }
  1182. knl_show_edc_route(edc_route_reg[i-1], edc_route_string);
  1183. if (cur_reg_start == i-1)
  1184. edac_dbg(0, "edc route table for CHA %d: %s\n",
  1185. cur_reg_start, edc_route_string);
  1186. else
  1187. edac_dbg(0, "edc route table for CHA %d-%d: %s\n",
  1188. cur_reg_start, i-1, edc_route_string);
  1189. /* Read the MC route table in each CHA. */
  1190. cur_reg_start = 0;
  1191. for (i = 0; i < KNL_MAX_CHAS; i++) {
  1192. pci_read_config_dword(pvt->knl.pci_cha[i],
  1193. KNL_MC_ROUTE, &mc_route_reg[i]);
  1194. if (i > 0 && mc_route_reg[i] != mc_route_reg[i-1]) {
  1195. knl_show_mc_route(mc_route_reg[i-1], mc_route_string);
  1196. if (cur_reg_start == i-1)
  1197. edac_dbg(0, "mc route table for CHA %d: %s\n",
  1198. cur_reg_start, mc_route_string);
  1199. else
  1200. edac_dbg(0, "mc route table for CHA %d-%d: %s\n",
  1201. cur_reg_start, i-1, mc_route_string);
  1202. cur_reg_start = i;
  1203. }
  1204. }
  1205. knl_show_mc_route(mc_route_reg[i-1], mc_route_string);
  1206. if (cur_reg_start == i-1)
  1207. edac_dbg(0, "mc route table for CHA %d: %s\n",
  1208. cur_reg_start, mc_route_string);
  1209. else
  1210. edac_dbg(0, "mc route table for CHA %d-%d: %s\n",
  1211. cur_reg_start, i-1, mc_route_string);
  1212. /* Process DRAM rules */
  1213. for (sad_rule = 0; sad_rule < pvt->info.max_sad; sad_rule++) {
  1214. /* previous limit becomes the new base */
  1215. sad_base = sad_limit;
  1216. pci_read_config_dword(pvt->pci_sad0,
  1217. pvt->info.dram_rule[sad_rule], &dram_rule);
  1218. if (!DRAM_RULE_ENABLE(dram_rule))
  1219. break;
  1220. edram_only = KNL_EDRAM_ONLY(dram_rule);
  1221. sad_limit = pvt->info.sad_limit(dram_rule)+1;
  1222. sad_size = sad_limit - sad_base;
  1223. pci_read_config_dword(pvt->pci_sad0,
  1224. pvt->info.interleave_list[sad_rule], &interleave_reg);
  1225. /*
  1226. * Find out how many ways this dram rule is interleaved.
  1227. * We stop when we see the first channel again.
  1228. */
  1229. first_pkg = sad_pkg(pvt->info.interleave_pkg,
  1230. interleave_reg, 0);
  1231. for (intrlv_ways = 1; intrlv_ways < 8; intrlv_ways++) {
  1232. pkg = sad_pkg(pvt->info.interleave_pkg,
  1233. interleave_reg, intrlv_ways);
  1234. if ((pkg & 0x8) == 0) {
  1235. /*
  1236. * 0 bit means memory is non-local,
  1237. * which KNL doesn't support
  1238. */
  1239. edac_dbg(0, "Unexpected interleave target %d\n",
  1240. pkg);
  1241. return -1;
  1242. }
  1243. if (pkg == first_pkg)
  1244. break;
  1245. }
  1246. if (KNL_MOD3(dram_rule))
  1247. intrlv_ways *= 3;
  1248. edac_dbg(3, "dram rule %d (base 0x%llx, limit 0x%llx), %d way interleave%s\n",
  1249. sad_rule,
  1250. sad_base,
  1251. sad_limit,
  1252. intrlv_ways,
  1253. edram_only ? ", EDRAM" : "");
  1254. /*
  1255. * Find out how big the SAD region really is by iterating
  1256. * over TAD tables (SAD regions may contain holes).
  1257. * Each memory controller might have a different TAD table, so
  1258. * we have to look at both.
  1259. *
  1260. * Livespace is the memory that's mapped in this TAD table,
  1261. * deadspace is the holes (this could be the MMIO hole, or it
  1262. * could be memory that's mapped by the other TAD table but
  1263. * not this one).
  1264. */
  1265. for (mc = 0; mc < 2; mc++) {
  1266. sad_actual_size[mc] = 0;
  1267. tad_livespace = 0;
  1268. for (tad_rule = 0;
  1269. tad_rule < ARRAY_SIZE(
  1270. knl_tad_dram_limit_lo);
  1271. tad_rule++) {
  1272. if (knl_get_tad(pvt,
  1273. tad_rule,
  1274. mc,
  1275. &tad_deadspace,
  1276. &tad_limit,
  1277. &tad_ways))
  1278. break;
  1279. tad_size = (tad_limit+1) -
  1280. (tad_livespace + tad_deadspace);
  1281. tad_livespace += tad_size;
  1282. tad_base = (tad_limit+1) - tad_size;
  1283. if (tad_base < sad_base) {
  1284. if (tad_limit > sad_base)
  1285. edac_dbg(0, "TAD region overlaps lower SAD boundary -- TAD tables may be configured incorrectly.\n");
  1286. } else if (tad_base < sad_limit) {
  1287. if (tad_limit+1 > sad_limit) {
  1288. edac_dbg(0, "TAD region overlaps upper SAD boundary -- TAD tables may be configured incorrectly.\n");
  1289. } else {
  1290. /* TAD region is completely inside SAD region */
  1291. edac_dbg(3, "TAD region %d 0x%llx - 0x%llx (%lld bytes) table%d\n",
  1292. tad_rule, tad_base,
  1293. tad_limit, tad_size,
  1294. mc);
  1295. sad_actual_size[mc] += tad_size;
  1296. }
  1297. }
  1298. tad_base = tad_limit+1;
  1299. }
  1300. }
  1301. for (mc = 0; mc < 2; mc++) {
  1302. edac_dbg(3, " total TAD DRAM footprint in table%d : 0x%llx (%lld bytes)\n",
  1303. mc, sad_actual_size[mc], sad_actual_size[mc]);
  1304. }
  1305. /* Ignore EDRAM rule */
  1306. if (edram_only)
  1307. continue;
  1308. /* Figure out which channels participate in interleave. */
  1309. for (channel = 0; channel < KNL_MAX_CHANNELS; channel++)
  1310. participants[channel] = 0;
  1311. /* For each channel, does at least one CHA have
  1312. * this channel mapped to the given target?
  1313. */
  1314. for (channel = 0; channel < KNL_MAX_CHANNELS; channel++) {
  1315. for (way = 0; way < intrlv_ways; way++) {
  1316. int target;
  1317. int cha;
  1318. if (KNL_MOD3(dram_rule))
  1319. target = way;
  1320. else
  1321. target = 0x7 & sad_pkg(
  1322. pvt->info.interleave_pkg, interleave_reg, way);
  1323. for (cha = 0; cha < KNL_MAX_CHAS; cha++) {
  1324. if (knl_get_mc_route(target,
  1325. mc_route_reg[cha]) == channel
  1326. && !participants[channel]) {
  1327. participant_count++;
  1328. participants[channel] = 1;
  1329. break;
  1330. }
  1331. }
  1332. }
  1333. }
  1334. if (participant_count != intrlv_ways)
  1335. edac_dbg(0, "participant_count (%d) != interleave_ways (%d): DIMM size may be incorrect\n",
  1336. participant_count, intrlv_ways);
  1337. for (channel = 0; channel < KNL_MAX_CHANNELS; channel++) {
  1338. mc = knl_channel_mc(channel);
  1339. if (participants[channel]) {
  1340. edac_dbg(4, "mc channel %d contributes %lld bytes via sad entry %d\n",
  1341. channel,
  1342. sad_actual_size[mc]/intrlv_ways,
  1343. sad_rule);
  1344. mc_sizes[channel] +=
  1345. sad_actual_size[mc]/intrlv_ways;
  1346. }
  1347. }
  1348. }
  1349. return 0;
  1350. }
  1351. static int get_dimm_config(struct mem_ctl_info *mci)
  1352. {
  1353. struct sbridge_pvt *pvt = mci->pvt_info;
  1354. struct dimm_info *dimm;
  1355. unsigned i, j, banks, ranks, rows, cols, npages;
  1356. u64 size;
  1357. u32 reg;
  1358. enum edac_type mode;
  1359. enum mem_type mtype;
  1360. int channels = pvt->info.type == KNIGHTS_LANDING ?
  1361. KNL_MAX_CHANNELS : NUM_CHANNELS;
  1362. u64 knl_mc_sizes[KNL_MAX_CHANNELS];
  1363. if (pvt->info.type == HASWELL || pvt->info.type == BROADWELL) {
  1364. pci_read_config_dword(pvt->pci_ha0, HASWELL_HASYSDEFEATURE2, &reg);
  1365. pvt->is_chan_hash = GET_BITFIELD(reg, 21, 21);
  1366. }
  1367. if (pvt->info.type == HASWELL || pvt->info.type == BROADWELL ||
  1368. pvt->info.type == KNIGHTS_LANDING)
  1369. pci_read_config_dword(pvt->pci_sad1, SAD_TARGET, &reg);
  1370. else
  1371. pci_read_config_dword(pvt->pci_br0, SAD_TARGET, &reg);
  1372. if (pvt->info.type == KNIGHTS_LANDING)
  1373. pvt->sbridge_dev->source_id = SOURCE_ID_KNL(reg);
  1374. else
  1375. pvt->sbridge_dev->source_id = SOURCE_ID(reg);
  1376. pvt->sbridge_dev->node_id = pvt->info.get_node_id(pvt);
  1377. edac_dbg(0, "mc#%d: Node ID: %d, source ID: %d\n",
  1378. pvt->sbridge_dev->mc,
  1379. pvt->sbridge_dev->node_id,
  1380. pvt->sbridge_dev->source_id);
  1381. /* KNL doesn't support mirroring or lockstep,
  1382. * and is always closed page
  1383. */
  1384. if (pvt->info.type == KNIGHTS_LANDING) {
  1385. mode = EDAC_S4ECD4ED;
  1386. pvt->is_mirrored = false;
  1387. if (knl_get_dimm_capacity(pvt, knl_mc_sizes) != 0)
  1388. return -1;
  1389. } else {
  1390. pci_read_config_dword(pvt->pci_ras, RASENABLES, &reg);
  1391. if (IS_MIRROR_ENABLED(reg)) {
  1392. edac_dbg(0, "Memory mirror is enabled\n");
  1393. pvt->is_mirrored = true;
  1394. } else {
  1395. edac_dbg(0, "Memory mirror is disabled\n");
  1396. pvt->is_mirrored = false;
  1397. }
  1398. pci_read_config_dword(pvt->pci_ta, MCMTR, &pvt->info.mcmtr);
  1399. if (IS_LOCKSTEP_ENABLED(pvt->info.mcmtr)) {
  1400. edac_dbg(0, "Lockstep is enabled\n");
  1401. mode = EDAC_S8ECD8ED;
  1402. pvt->is_lockstep = true;
  1403. } else {
  1404. edac_dbg(0, "Lockstep is disabled\n");
  1405. mode = EDAC_S4ECD4ED;
  1406. pvt->is_lockstep = false;
  1407. }
  1408. if (IS_CLOSE_PG(pvt->info.mcmtr)) {
  1409. edac_dbg(0, "address map is on closed page mode\n");
  1410. pvt->is_close_pg = true;
  1411. } else {
  1412. edac_dbg(0, "address map is on open page mode\n");
  1413. pvt->is_close_pg = false;
  1414. }
  1415. }
  1416. mtype = pvt->info.get_memory_type(pvt);
  1417. if (mtype == MEM_RDDR3 || mtype == MEM_RDDR4)
  1418. edac_dbg(0, "Memory is registered\n");
  1419. else if (mtype == MEM_UNKNOWN)
  1420. edac_dbg(0, "Cannot determine memory type\n");
  1421. else
  1422. edac_dbg(0, "Memory is unregistered\n");
  1423. if (mtype == MEM_DDR4 || mtype == MEM_RDDR4)
  1424. banks = 16;
  1425. else
  1426. banks = 8;
  1427. for (i = 0; i < channels; i++) {
  1428. u32 mtr;
  1429. int max_dimms_per_channel;
  1430. if (pvt->info.type == KNIGHTS_LANDING) {
  1431. max_dimms_per_channel = 1;
  1432. if (!pvt->knl.pci_channel[i])
  1433. continue;
  1434. } else {
  1435. max_dimms_per_channel = ARRAY_SIZE(mtr_regs);
  1436. if (!pvt->pci_tad[i])
  1437. continue;
  1438. }
  1439. for (j = 0; j < max_dimms_per_channel; j++) {
  1440. dimm = EDAC_DIMM_PTR(mci->layers, mci->dimms, mci->n_layers,
  1441. i, j, 0);
  1442. if (pvt->info.type == KNIGHTS_LANDING) {
  1443. pci_read_config_dword(pvt->knl.pci_channel[i],
  1444. knl_mtr_reg, &mtr);
  1445. } else {
  1446. pci_read_config_dword(pvt->pci_tad[i],
  1447. mtr_regs[j], &mtr);
  1448. }
  1449. edac_dbg(4, "Channel #%d MTR%d = %x\n", i, j, mtr);
  1450. if (IS_DIMM_PRESENT(mtr)) {
  1451. pvt->channel[i].dimms++;
  1452. ranks = numrank(pvt->info.type, mtr);
  1453. if (pvt->info.type == KNIGHTS_LANDING) {
  1454. /* For DDR4, this is fixed. */
  1455. cols = 1 << 10;
  1456. rows = knl_mc_sizes[i] /
  1457. ((u64) cols * ranks * banks * 8);
  1458. } else {
  1459. rows = numrow(mtr);
  1460. cols = numcol(mtr);
  1461. }
  1462. size = ((u64)rows * cols * banks * ranks) >> (20 - 3);
  1463. npages = MiB_TO_PAGES(size);
  1464. edac_dbg(0, "mc#%d: ha %d channel %d, dimm %d, %lld Mb (%d pages) bank: %d, rank: %d, row: %#x, col: %#x\n",
  1465. pvt->sbridge_dev->mc, i/4, i%4, j,
  1466. size, npages,
  1467. banks, ranks, rows, cols);
  1468. dimm->nr_pages = npages;
  1469. dimm->grain = 32;
  1470. dimm->dtype = pvt->info.get_width(pvt, mtr);
  1471. dimm->mtype = mtype;
  1472. dimm->edac_mode = mode;
  1473. snprintf(dimm->label, sizeof(dimm->label),
  1474. "CPU_SrcID#%u_Ha#%u_Chan#%u_DIMM#%u",
  1475. pvt->sbridge_dev->source_id, i/4, i%4, j);
  1476. }
  1477. }
  1478. }
  1479. return 0;
  1480. }
  1481. static void get_memory_layout(const struct mem_ctl_info *mci)
  1482. {
  1483. struct sbridge_pvt *pvt = mci->pvt_info;
  1484. int i, j, k, n_sads, n_tads, sad_interl;
  1485. u32 reg;
  1486. u64 limit, prv = 0;
  1487. u64 tmp_mb;
  1488. u32 gb, mb;
  1489. u32 rir_way;
  1490. /*
  1491. * Step 1) Get TOLM/TOHM ranges
  1492. */
  1493. pvt->tolm = pvt->info.get_tolm(pvt);
  1494. tmp_mb = (1 + pvt->tolm) >> 20;
  1495. gb = div_u64_rem(tmp_mb, 1024, &mb);
  1496. edac_dbg(0, "TOLM: %u.%03u GB (0x%016Lx)\n",
  1497. gb, (mb*1000)/1024, (u64)pvt->tolm);
  1498. /* Address range is already 45:25 */
  1499. pvt->tohm = pvt->info.get_tohm(pvt);
  1500. tmp_mb = (1 + pvt->tohm) >> 20;
  1501. gb = div_u64_rem(tmp_mb, 1024, &mb);
  1502. edac_dbg(0, "TOHM: %u.%03u GB (0x%016Lx)\n",
  1503. gb, (mb*1000)/1024, (u64)pvt->tohm);
  1504. /*
  1505. * Step 2) Get SAD range and SAD Interleave list
  1506. * TAD registers contain the interleave wayness. However, it
  1507. * seems simpler to just discover it indirectly, with the
  1508. * algorithm bellow.
  1509. */
  1510. prv = 0;
  1511. for (n_sads = 0; n_sads < pvt->info.max_sad; n_sads++) {
  1512. /* SAD_LIMIT Address range is 45:26 */
  1513. pci_read_config_dword(pvt->pci_sad0, pvt->info.dram_rule[n_sads],
  1514. &reg);
  1515. limit = pvt->info.sad_limit(reg);
  1516. if (!DRAM_RULE_ENABLE(reg))
  1517. continue;
  1518. if (limit <= prv)
  1519. break;
  1520. tmp_mb = (limit + 1) >> 20;
  1521. gb = div_u64_rem(tmp_mb, 1024, &mb);
  1522. edac_dbg(0, "SAD#%d %s up to %u.%03u GB (0x%016Lx) Interleave: %s reg=0x%08x\n",
  1523. n_sads,
  1524. show_dram_attr(pvt->info.dram_attr(reg)),
  1525. gb, (mb*1000)/1024,
  1526. ((u64)tmp_mb) << 20L,
  1527. pvt->info.show_interleave_mode(reg),
  1528. reg);
  1529. prv = limit;
  1530. pci_read_config_dword(pvt->pci_sad0, pvt->info.interleave_list[n_sads],
  1531. &reg);
  1532. sad_interl = sad_pkg(pvt->info.interleave_pkg, reg, 0);
  1533. for (j = 0; j < 8; j++) {
  1534. u32 pkg = sad_pkg(pvt->info.interleave_pkg, reg, j);
  1535. if (j > 0 && sad_interl == pkg)
  1536. break;
  1537. edac_dbg(0, "SAD#%d, interleave #%d: %d\n",
  1538. n_sads, j, pkg);
  1539. }
  1540. }
  1541. if (pvt->info.type == KNIGHTS_LANDING)
  1542. return;
  1543. /*
  1544. * Step 3) Get TAD range
  1545. */
  1546. prv = 0;
  1547. for (n_tads = 0; n_tads < MAX_TAD; n_tads++) {
  1548. pci_read_config_dword(pvt->pci_ha0, tad_dram_rule[n_tads],
  1549. &reg);
  1550. limit = TAD_LIMIT(reg);
  1551. if (limit <= prv)
  1552. break;
  1553. tmp_mb = (limit + 1) >> 20;
  1554. gb = div_u64_rem(tmp_mb, 1024, &mb);
  1555. 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",
  1556. n_tads, gb, (mb*1000)/1024,
  1557. ((u64)tmp_mb) << 20L,
  1558. (u32)(1 << TAD_SOCK(reg)),
  1559. (u32)TAD_CH(reg) + 1,
  1560. (u32)TAD_TGT0(reg),
  1561. (u32)TAD_TGT1(reg),
  1562. (u32)TAD_TGT2(reg),
  1563. (u32)TAD_TGT3(reg),
  1564. reg);
  1565. prv = limit;
  1566. }
  1567. /*
  1568. * Step 4) Get TAD offsets, per each channel
  1569. */
  1570. for (i = 0; i < NUM_CHANNELS; i++) {
  1571. if (!pvt->channel[i].dimms)
  1572. continue;
  1573. for (j = 0; j < n_tads; j++) {
  1574. pci_read_config_dword(pvt->pci_tad[i],
  1575. tad_ch_nilv_offset[j],
  1576. &reg);
  1577. tmp_mb = TAD_OFFSET(reg) >> 20;
  1578. gb = div_u64_rem(tmp_mb, 1024, &mb);
  1579. edac_dbg(0, "TAD CH#%d, offset #%d: %u.%03u GB (0x%016Lx), reg=0x%08x\n",
  1580. i, j,
  1581. gb, (mb*1000)/1024,
  1582. ((u64)tmp_mb) << 20L,
  1583. reg);
  1584. }
  1585. }
  1586. /*
  1587. * Step 6) Get RIR Wayness/Limit, per each channel
  1588. */
  1589. for (i = 0; i < NUM_CHANNELS; i++) {
  1590. if (!pvt->channel[i].dimms)
  1591. continue;
  1592. for (j = 0; j < MAX_RIR_RANGES; j++) {
  1593. pci_read_config_dword(pvt->pci_tad[i],
  1594. rir_way_limit[j],
  1595. &reg);
  1596. if (!IS_RIR_VALID(reg))
  1597. continue;
  1598. tmp_mb = pvt->info.rir_limit(reg) >> 20;
  1599. rir_way = 1 << RIR_WAY(reg);
  1600. gb = div_u64_rem(tmp_mb, 1024, &mb);
  1601. edac_dbg(0, "CH#%d RIR#%d, limit: %u.%03u GB (0x%016Lx), way: %d, reg=0x%08x\n",
  1602. i, j,
  1603. gb, (mb*1000)/1024,
  1604. ((u64)tmp_mb) << 20L,
  1605. rir_way,
  1606. reg);
  1607. for (k = 0; k < rir_way; k++) {
  1608. pci_read_config_dword(pvt->pci_tad[i],
  1609. rir_offset[j][k],
  1610. &reg);
  1611. tmp_mb = RIR_OFFSET(pvt->info.type, reg) << 6;
  1612. gb = div_u64_rem(tmp_mb, 1024, &mb);
  1613. edac_dbg(0, "CH#%d RIR#%d INTL#%d, offset %u.%03u GB (0x%016Lx), tgt: %d, reg=0x%08x\n",
  1614. i, j, k,
  1615. gb, (mb*1000)/1024,
  1616. ((u64)tmp_mb) << 20L,
  1617. (u32)RIR_RNK_TGT(pvt->info.type, reg),
  1618. reg);
  1619. }
  1620. }
  1621. }
  1622. }
  1623. static struct mem_ctl_info *get_mci_for_node_id(u8 node_id)
  1624. {
  1625. struct sbridge_dev *sbridge_dev;
  1626. list_for_each_entry(sbridge_dev, &sbridge_edac_list, list) {
  1627. if (sbridge_dev->node_id == node_id)
  1628. return sbridge_dev->mci;
  1629. }
  1630. return NULL;
  1631. }
  1632. static int get_memory_error_data(struct mem_ctl_info *mci,
  1633. u64 addr,
  1634. u8 *socket, u8 *ha,
  1635. long *channel_mask,
  1636. u8 *rank,
  1637. char **area_type, char *msg)
  1638. {
  1639. struct mem_ctl_info *new_mci;
  1640. struct sbridge_pvt *pvt = mci->pvt_info;
  1641. struct pci_dev *pci_ha;
  1642. int n_rir, n_sads, n_tads, sad_way, sck_xch;
  1643. int sad_interl, idx, base_ch;
  1644. int interleave_mode, shiftup = 0;
  1645. unsigned sad_interleave[pvt->info.max_interleave];
  1646. u32 reg, dram_rule;
  1647. u8 ch_way, sck_way, pkg, sad_ha = 0, ch_add = 0;
  1648. u32 tad_offset;
  1649. u32 rir_way;
  1650. u32 mb, gb;
  1651. u64 ch_addr, offset, limit = 0, prv = 0;
  1652. /*
  1653. * Step 0) Check if the address is at special memory ranges
  1654. * The check bellow is probably enough to fill all cases where
  1655. * the error is not inside a memory, except for the legacy
  1656. * range (e. g. VGA addresses). It is unlikely, however, that the
  1657. * memory controller would generate an error on that range.
  1658. */
  1659. if ((addr > (u64) pvt->tolm) && (addr < (1LL << 32))) {
  1660. sprintf(msg, "Error at TOLM area, on addr 0x%08Lx", addr);
  1661. return -EINVAL;
  1662. }
  1663. if (addr >= (u64)pvt->tohm) {
  1664. sprintf(msg, "Error at MMIOH area, on addr 0x%016Lx", addr);
  1665. return -EINVAL;
  1666. }
  1667. /*
  1668. * Step 1) Get socket
  1669. */
  1670. for (n_sads = 0; n_sads < pvt->info.max_sad; n_sads++) {
  1671. pci_read_config_dword(pvt->pci_sad0, pvt->info.dram_rule[n_sads],
  1672. &reg);
  1673. if (!DRAM_RULE_ENABLE(reg))
  1674. continue;
  1675. limit = pvt->info.sad_limit(reg);
  1676. if (limit <= prv) {
  1677. sprintf(msg, "Can't discover the memory socket");
  1678. return -EINVAL;
  1679. }
  1680. if (addr <= limit)
  1681. break;
  1682. prv = limit;
  1683. }
  1684. if (n_sads == pvt->info.max_sad) {
  1685. sprintf(msg, "Can't discover the memory socket");
  1686. return -EINVAL;
  1687. }
  1688. dram_rule = reg;
  1689. *area_type = show_dram_attr(pvt->info.dram_attr(dram_rule));
  1690. interleave_mode = pvt->info.interleave_mode(dram_rule);
  1691. pci_read_config_dword(pvt->pci_sad0, pvt->info.interleave_list[n_sads],
  1692. &reg);
  1693. if (pvt->info.type == SANDY_BRIDGE) {
  1694. sad_interl = sad_pkg(pvt->info.interleave_pkg, reg, 0);
  1695. for (sad_way = 0; sad_way < 8; sad_way++) {
  1696. u32 pkg = sad_pkg(pvt->info.interleave_pkg, reg, sad_way);
  1697. if (sad_way > 0 && sad_interl == pkg)
  1698. break;
  1699. sad_interleave[sad_way] = pkg;
  1700. edac_dbg(0, "SAD interleave #%d: %d\n",
  1701. sad_way, sad_interleave[sad_way]);
  1702. }
  1703. edac_dbg(0, "mc#%d: Error detected on SAD#%d: address 0x%016Lx < 0x%016Lx, Interleave [%d:6]%s\n",
  1704. pvt->sbridge_dev->mc,
  1705. n_sads,
  1706. addr,
  1707. limit,
  1708. sad_way + 7,
  1709. !interleave_mode ? "" : "XOR[18:16]");
  1710. if (interleave_mode)
  1711. idx = ((addr >> 6) ^ (addr >> 16)) & 7;
  1712. else
  1713. idx = (addr >> 6) & 7;
  1714. switch (sad_way) {
  1715. case 1:
  1716. idx = 0;
  1717. break;
  1718. case 2:
  1719. idx = idx & 1;
  1720. break;
  1721. case 4:
  1722. idx = idx & 3;
  1723. break;
  1724. case 8:
  1725. break;
  1726. default:
  1727. sprintf(msg, "Can't discover socket interleave");
  1728. return -EINVAL;
  1729. }
  1730. *socket = sad_interleave[idx];
  1731. edac_dbg(0, "SAD interleave index: %d (wayness %d) = CPU socket %d\n",
  1732. idx, sad_way, *socket);
  1733. } else if (pvt->info.type == HASWELL || pvt->info.type == BROADWELL) {
  1734. int bits, a7mode = A7MODE(dram_rule);
  1735. if (a7mode) {
  1736. /* A7 mode swaps P9 with P6 */
  1737. bits = GET_BITFIELD(addr, 7, 8) << 1;
  1738. bits |= GET_BITFIELD(addr, 9, 9);
  1739. } else
  1740. bits = GET_BITFIELD(addr, 6, 8);
  1741. if (interleave_mode == 0) {
  1742. /* interleave mode will XOR {8,7,6} with {18,17,16} */
  1743. idx = GET_BITFIELD(addr, 16, 18);
  1744. idx ^= bits;
  1745. } else
  1746. idx = bits;
  1747. pkg = sad_pkg(pvt->info.interleave_pkg, reg, idx);
  1748. *socket = sad_pkg_socket(pkg);
  1749. sad_ha = sad_pkg_ha(pkg);
  1750. if (sad_ha)
  1751. ch_add = 4;
  1752. if (a7mode) {
  1753. /* MCChanShiftUpEnable */
  1754. pci_read_config_dword(pvt->pci_ha0,
  1755. HASWELL_HASYSDEFEATURE2, &reg);
  1756. shiftup = GET_BITFIELD(reg, 22, 22);
  1757. }
  1758. edac_dbg(0, "SAD interleave package: %d = CPU socket %d, HA %i, shiftup: %i\n",
  1759. idx, *socket, sad_ha, shiftup);
  1760. } else {
  1761. /* Ivy Bridge's SAD mode doesn't support XOR interleave mode */
  1762. idx = (addr >> 6) & 7;
  1763. pkg = sad_pkg(pvt->info.interleave_pkg, reg, idx);
  1764. *socket = sad_pkg_socket(pkg);
  1765. sad_ha = sad_pkg_ha(pkg);
  1766. if (sad_ha)
  1767. ch_add = 4;
  1768. edac_dbg(0, "SAD interleave package: %d = CPU socket %d, HA %d\n",
  1769. idx, *socket, sad_ha);
  1770. }
  1771. *ha = sad_ha;
  1772. /*
  1773. * Move to the proper node structure, in order to access the
  1774. * right PCI registers
  1775. */
  1776. new_mci = get_mci_for_node_id(*socket);
  1777. if (!new_mci) {
  1778. sprintf(msg, "Struct for socket #%u wasn't initialized",
  1779. *socket);
  1780. return -EINVAL;
  1781. }
  1782. mci = new_mci;
  1783. pvt = mci->pvt_info;
  1784. /*
  1785. * Step 2) Get memory channel
  1786. */
  1787. prv = 0;
  1788. if (pvt->info.type == SANDY_BRIDGE)
  1789. pci_ha = pvt->pci_ha0;
  1790. else {
  1791. if (sad_ha)
  1792. pci_ha = pvt->pci_ha1;
  1793. else
  1794. pci_ha = pvt->pci_ha0;
  1795. }
  1796. for (n_tads = 0; n_tads < MAX_TAD; n_tads++) {
  1797. pci_read_config_dword(pci_ha, tad_dram_rule[n_tads], &reg);
  1798. limit = TAD_LIMIT(reg);
  1799. if (limit <= prv) {
  1800. sprintf(msg, "Can't discover the memory channel");
  1801. return -EINVAL;
  1802. }
  1803. if (addr <= limit)
  1804. break;
  1805. prv = limit;
  1806. }
  1807. if (n_tads == MAX_TAD) {
  1808. sprintf(msg, "Can't discover the memory channel");
  1809. return -EINVAL;
  1810. }
  1811. ch_way = TAD_CH(reg) + 1;
  1812. sck_way = TAD_SOCK(reg);
  1813. if (ch_way == 3)
  1814. idx = addr >> 6;
  1815. else {
  1816. idx = (addr >> (6 + sck_way + shiftup)) & 0x3;
  1817. if (pvt->is_chan_hash)
  1818. idx = haswell_chan_hash(idx, addr);
  1819. }
  1820. idx = idx % ch_way;
  1821. /*
  1822. * FIXME: Shouldn't we use CHN_IDX_OFFSET() here, when ch_way == 3 ???
  1823. */
  1824. switch (idx) {
  1825. case 0:
  1826. base_ch = TAD_TGT0(reg);
  1827. break;
  1828. case 1:
  1829. base_ch = TAD_TGT1(reg);
  1830. break;
  1831. case 2:
  1832. base_ch = TAD_TGT2(reg);
  1833. break;
  1834. case 3:
  1835. base_ch = TAD_TGT3(reg);
  1836. break;
  1837. default:
  1838. sprintf(msg, "Can't discover the TAD target");
  1839. return -EINVAL;
  1840. }
  1841. *channel_mask = 1 << base_ch;
  1842. pci_read_config_dword(pvt->pci_tad[ch_add + base_ch],
  1843. tad_ch_nilv_offset[n_tads],
  1844. &tad_offset);
  1845. if (pvt->is_mirrored) {
  1846. *channel_mask |= 1 << ((base_ch + 2) % 4);
  1847. switch(ch_way) {
  1848. case 2:
  1849. case 4:
  1850. sck_xch = (1 << sck_way) * (ch_way >> 1);
  1851. break;
  1852. default:
  1853. sprintf(msg, "Invalid mirror set. Can't decode addr");
  1854. return -EINVAL;
  1855. }
  1856. } else
  1857. sck_xch = (1 << sck_way) * ch_way;
  1858. if (pvt->is_lockstep)
  1859. *channel_mask |= 1 << ((base_ch + 1) % 4);
  1860. offset = TAD_OFFSET(tad_offset);
  1861. 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",
  1862. n_tads,
  1863. addr,
  1864. limit,
  1865. sck_way,
  1866. ch_way,
  1867. offset,
  1868. idx,
  1869. base_ch,
  1870. *channel_mask);
  1871. /* Calculate channel address */
  1872. /* Remove the TAD offset */
  1873. if (offset > addr) {
  1874. sprintf(msg, "Can't calculate ch addr: TAD offset 0x%08Lx is too high for addr 0x%08Lx!",
  1875. offset, addr);
  1876. return -EINVAL;
  1877. }
  1878. ch_addr = addr - offset;
  1879. ch_addr >>= (6 + shiftup);
  1880. ch_addr /= sck_xch;
  1881. ch_addr <<= (6 + shiftup);
  1882. ch_addr |= addr & ((1 << (6 + shiftup)) - 1);
  1883. /*
  1884. * Step 3) Decode rank
  1885. */
  1886. for (n_rir = 0; n_rir < MAX_RIR_RANGES; n_rir++) {
  1887. pci_read_config_dword(pvt->pci_tad[ch_add + base_ch],
  1888. rir_way_limit[n_rir],
  1889. &reg);
  1890. if (!IS_RIR_VALID(reg))
  1891. continue;
  1892. limit = pvt->info.rir_limit(reg);
  1893. gb = div_u64_rem(limit >> 20, 1024, &mb);
  1894. edac_dbg(0, "RIR#%d, limit: %u.%03u GB (0x%016Lx), way: %d\n",
  1895. n_rir,
  1896. gb, (mb*1000)/1024,
  1897. limit,
  1898. 1 << RIR_WAY(reg));
  1899. if (ch_addr <= limit)
  1900. break;
  1901. }
  1902. if (n_rir == MAX_RIR_RANGES) {
  1903. sprintf(msg, "Can't discover the memory rank for ch addr 0x%08Lx",
  1904. ch_addr);
  1905. return -EINVAL;
  1906. }
  1907. rir_way = RIR_WAY(reg);
  1908. if (pvt->is_close_pg)
  1909. idx = (ch_addr >> 6);
  1910. else
  1911. idx = (ch_addr >> 13); /* FIXME: Datasheet says to shift by 15 */
  1912. idx %= 1 << rir_way;
  1913. pci_read_config_dword(pvt->pci_tad[ch_add + base_ch],
  1914. rir_offset[n_rir][idx],
  1915. &reg);
  1916. *rank = RIR_RNK_TGT(pvt->info.type, reg);
  1917. edac_dbg(0, "RIR#%d: channel address 0x%08Lx < 0x%08Lx, RIR interleave %d, index %d\n",
  1918. n_rir,
  1919. ch_addr,
  1920. limit,
  1921. rir_way,
  1922. idx);
  1923. return 0;
  1924. }
  1925. /****************************************************************************
  1926. Device initialization routines: put/get, init/exit
  1927. ****************************************************************************/
  1928. /*
  1929. * sbridge_put_all_devices 'put' all the devices that we have
  1930. * reserved via 'get'
  1931. */
  1932. static void sbridge_put_devices(struct sbridge_dev *sbridge_dev)
  1933. {
  1934. int i;
  1935. edac_dbg(0, "\n");
  1936. for (i = 0; i < sbridge_dev->n_devs; i++) {
  1937. struct pci_dev *pdev = sbridge_dev->pdev[i];
  1938. if (!pdev)
  1939. continue;
  1940. edac_dbg(0, "Removing dev %02x:%02x.%d\n",
  1941. pdev->bus->number,
  1942. PCI_SLOT(pdev->devfn), PCI_FUNC(pdev->devfn));
  1943. pci_dev_put(pdev);
  1944. }
  1945. }
  1946. static void sbridge_put_all_devices(void)
  1947. {
  1948. struct sbridge_dev *sbridge_dev, *tmp;
  1949. list_for_each_entry_safe(sbridge_dev, tmp, &sbridge_edac_list, list) {
  1950. sbridge_put_devices(sbridge_dev);
  1951. free_sbridge_dev(sbridge_dev);
  1952. }
  1953. }
  1954. static int sbridge_get_onedevice(struct pci_dev **prev,
  1955. u8 *num_mc,
  1956. const struct pci_id_table *table,
  1957. const unsigned devno,
  1958. const int multi_bus)
  1959. {
  1960. struct sbridge_dev *sbridge_dev;
  1961. const struct pci_id_descr *dev_descr = &table->descr[devno];
  1962. struct pci_dev *pdev = NULL;
  1963. u8 bus = 0;
  1964. sbridge_printk(KERN_DEBUG,
  1965. "Seeking for: PCI ID %04x:%04x\n",
  1966. PCI_VENDOR_ID_INTEL, dev_descr->dev_id);
  1967. pdev = pci_get_device(PCI_VENDOR_ID_INTEL,
  1968. dev_descr->dev_id, *prev);
  1969. if (!pdev) {
  1970. if (*prev) {
  1971. *prev = pdev;
  1972. return 0;
  1973. }
  1974. if (dev_descr->optional)
  1975. return 0;
  1976. /* if the HA wasn't found */
  1977. if (devno == 0)
  1978. return -ENODEV;
  1979. sbridge_printk(KERN_INFO,
  1980. "Device not found: %04x:%04x\n",
  1981. PCI_VENDOR_ID_INTEL, dev_descr->dev_id);
  1982. /* End of list, leave */
  1983. return -ENODEV;
  1984. }
  1985. bus = pdev->bus->number;
  1986. sbridge_dev = get_sbridge_dev(bus, multi_bus);
  1987. if (!sbridge_dev) {
  1988. sbridge_dev = alloc_sbridge_dev(bus, table);
  1989. if (!sbridge_dev) {
  1990. pci_dev_put(pdev);
  1991. return -ENOMEM;
  1992. }
  1993. (*num_mc)++;
  1994. }
  1995. if (sbridge_dev->pdev[devno]) {
  1996. sbridge_printk(KERN_ERR,
  1997. "Duplicated device for %04x:%04x\n",
  1998. PCI_VENDOR_ID_INTEL, dev_descr->dev_id);
  1999. pci_dev_put(pdev);
  2000. return -ENODEV;
  2001. }
  2002. sbridge_dev->pdev[devno] = pdev;
  2003. /* Be sure that the device is enabled */
  2004. if (unlikely(pci_enable_device(pdev) < 0)) {
  2005. sbridge_printk(KERN_ERR,
  2006. "Couldn't enable %04x:%04x\n",
  2007. PCI_VENDOR_ID_INTEL, dev_descr->dev_id);
  2008. return -ENODEV;
  2009. }
  2010. edac_dbg(0, "Detected %04x:%04x\n",
  2011. PCI_VENDOR_ID_INTEL, dev_descr->dev_id);
  2012. /*
  2013. * As stated on drivers/pci/search.c, the reference count for
  2014. * @from is always decremented if it is not %NULL. So, as we need
  2015. * to get all devices up to null, we need to do a get for the device
  2016. */
  2017. pci_dev_get(pdev);
  2018. *prev = pdev;
  2019. return 0;
  2020. }
  2021. /*
  2022. * sbridge_get_all_devices - Find and perform 'get' operation on the MCH's
  2023. * devices we want to reference for this driver.
  2024. * @num_mc: pointer to the memory controllers count, to be incremented in case
  2025. * of success.
  2026. * @table: model specific table
  2027. *
  2028. * returns 0 in case of success or error code
  2029. */
  2030. static int sbridge_get_all_devices(u8 *num_mc,
  2031. const struct pci_id_table *table)
  2032. {
  2033. int i, rc;
  2034. struct pci_dev *pdev = NULL;
  2035. int allow_dups = 0;
  2036. int multi_bus = 0;
  2037. if (table->type == KNIGHTS_LANDING)
  2038. allow_dups = multi_bus = 1;
  2039. while (table && table->descr) {
  2040. for (i = 0; i < table->n_devs; i++) {
  2041. if (!allow_dups || i == 0 ||
  2042. table->descr[i].dev_id !=
  2043. table->descr[i-1].dev_id) {
  2044. pdev = NULL;
  2045. }
  2046. do {
  2047. rc = sbridge_get_onedevice(&pdev, num_mc,
  2048. table, i, multi_bus);
  2049. if (rc < 0) {
  2050. if (i == 0) {
  2051. i = table->n_devs;
  2052. break;
  2053. }
  2054. sbridge_put_all_devices();
  2055. return -ENODEV;
  2056. }
  2057. } while (pdev && !allow_dups);
  2058. }
  2059. table++;
  2060. }
  2061. return 0;
  2062. }
  2063. static int sbridge_mci_bind_devs(struct mem_ctl_info *mci,
  2064. struct sbridge_dev *sbridge_dev)
  2065. {
  2066. struct sbridge_pvt *pvt = mci->pvt_info;
  2067. struct pci_dev *pdev;
  2068. u8 saw_chan_mask = 0;
  2069. int i;
  2070. for (i = 0; i < sbridge_dev->n_devs; i++) {
  2071. pdev = sbridge_dev->pdev[i];
  2072. if (!pdev)
  2073. continue;
  2074. switch (pdev->device) {
  2075. case PCI_DEVICE_ID_INTEL_SBRIDGE_SAD0:
  2076. pvt->pci_sad0 = pdev;
  2077. break;
  2078. case PCI_DEVICE_ID_INTEL_SBRIDGE_SAD1:
  2079. pvt->pci_sad1 = pdev;
  2080. break;
  2081. case PCI_DEVICE_ID_INTEL_SBRIDGE_BR:
  2082. pvt->pci_br0 = pdev;
  2083. break;
  2084. case PCI_DEVICE_ID_INTEL_SBRIDGE_IMC_HA0:
  2085. pvt->pci_ha0 = pdev;
  2086. break;
  2087. case PCI_DEVICE_ID_INTEL_SBRIDGE_IMC_TA:
  2088. pvt->pci_ta = pdev;
  2089. break;
  2090. case PCI_DEVICE_ID_INTEL_SBRIDGE_IMC_RAS:
  2091. pvt->pci_ras = pdev;
  2092. break;
  2093. case PCI_DEVICE_ID_INTEL_SBRIDGE_IMC_TAD0:
  2094. case PCI_DEVICE_ID_INTEL_SBRIDGE_IMC_TAD1:
  2095. case PCI_DEVICE_ID_INTEL_SBRIDGE_IMC_TAD2:
  2096. case PCI_DEVICE_ID_INTEL_SBRIDGE_IMC_TAD3:
  2097. {
  2098. int id = pdev->device - PCI_DEVICE_ID_INTEL_SBRIDGE_IMC_TAD0;
  2099. pvt->pci_tad[id] = pdev;
  2100. saw_chan_mask |= 1 << id;
  2101. }
  2102. break;
  2103. case PCI_DEVICE_ID_INTEL_SBRIDGE_IMC_DDRIO:
  2104. pvt->pci_ddrio = pdev;
  2105. break;
  2106. default:
  2107. goto error;
  2108. }
  2109. edac_dbg(0, "Associated PCI %02x:%02x, bus %d with dev = %p\n",
  2110. pdev->vendor, pdev->device,
  2111. sbridge_dev->bus,
  2112. pdev);
  2113. }
  2114. /* Check if everything were registered */
  2115. if (!pvt->pci_sad0 || !pvt->pci_sad1 || !pvt->pci_ha0 ||
  2116. !pvt->pci_ras || !pvt->pci_ta)
  2117. goto enodev;
  2118. if (saw_chan_mask != 0x0f)
  2119. goto enodev;
  2120. return 0;
  2121. enodev:
  2122. sbridge_printk(KERN_ERR, "Some needed devices are missing\n");
  2123. return -ENODEV;
  2124. error:
  2125. sbridge_printk(KERN_ERR, "Unexpected device %02x:%02x\n",
  2126. PCI_VENDOR_ID_INTEL, pdev->device);
  2127. return -EINVAL;
  2128. }
  2129. static int ibridge_mci_bind_devs(struct mem_ctl_info *mci,
  2130. struct sbridge_dev *sbridge_dev)
  2131. {
  2132. struct sbridge_pvt *pvt = mci->pvt_info;
  2133. struct pci_dev *pdev;
  2134. u8 saw_chan_mask = 0;
  2135. int i;
  2136. for (i = 0; i < sbridge_dev->n_devs; i++) {
  2137. pdev = sbridge_dev->pdev[i];
  2138. if (!pdev)
  2139. continue;
  2140. switch (pdev->device) {
  2141. case PCI_DEVICE_ID_INTEL_IBRIDGE_IMC_HA0:
  2142. pvt->pci_ha0 = pdev;
  2143. break;
  2144. case PCI_DEVICE_ID_INTEL_IBRIDGE_IMC_HA0_TA:
  2145. pvt->pci_ta = pdev;
  2146. break;
  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(0x2d, pci_dev_descr_sbridge_table), /* SANDY_BRIDGE */
  2896. ICPU(0x3e, pci_dev_descr_ibridge_table), /* IVY_BRIDGE */
  2897. ICPU(0x3f, pci_dev_descr_haswell_table), /* HASWELL */
  2898. ICPU(0x4f, pci_dev_descr_broadwell_table), /* BROADWELL */
  2899. ICPU(0x56, pci_dev_descr_broadwell_table), /* BROADWELL-DE */
  2900. ICPU(0x57, pci_dev_descr_knl_table), /* KNIGHTS_LANDING */
  2901. { }
  2902. };
  2903. MODULE_DEVICE_TABLE(x86cpu, sbridge_cpuids);
  2904. /*
  2905. * sbridge_probe Get all devices and register memory controllers
  2906. * present.
  2907. * return:
  2908. * 0 for FOUND a device
  2909. * < 0 for error code
  2910. */
  2911. static int sbridge_probe(const struct x86_cpu_id *id)
  2912. {
  2913. int rc = -ENODEV;
  2914. u8 mc, num_mc = 0;
  2915. struct sbridge_dev *sbridge_dev;
  2916. struct pci_id_table *ptable = (struct pci_id_table *)id->driver_data;
  2917. /* get the pci devices we want to reserve for our use */
  2918. rc = sbridge_get_all_devices(&num_mc, ptable);
  2919. if (unlikely(rc < 0)) {
  2920. edac_dbg(0, "couldn't get all devices\n");
  2921. goto fail0;
  2922. }
  2923. mc = 0;
  2924. list_for_each_entry(sbridge_dev, &sbridge_edac_list, list) {
  2925. edac_dbg(0, "Registering MC#%d (%d of %d)\n",
  2926. mc, mc + 1, num_mc);
  2927. sbridge_dev->mc = mc++;
  2928. rc = sbridge_register_mci(sbridge_dev, ptable->type);
  2929. if (unlikely(rc < 0))
  2930. goto fail1;
  2931. }
  2932. sbridge_printk(KERN_INFO, "%s\n", SBRIDGE_REVISION);
  2933. return 0;
  2934. fail1:
  2935. list_for_each_entry(sbridge_dev, &sbridge_edac_list, list)
  2936. sbridge_unregister_mci(sbridge_dev);
  2937. sbridge_put_all_devices();
  2938. fail0:
  2939. return rc;
  2940. }
  2941. /*
  2942. * sbridge_remove cleanup
  2943. *
  2944. */
  2945. static void sbridge_remove(void)
  2946. {
  2947. struct sbridge_dev *sbridge_dev;
  2948. edac_dbg(0, "\n");
  2949. list_for_each_entry(sbridge_dev, &sbridge_edac_list, list)
  2950. sbridge_unregister_mci(sbridge_dev);
  2951. /* Release PCI resources */
  2952. sbridge_put_all_devices();
  2953. }
  2954. /*
  2955. * sbridge_init Module entry function
  2956. * Try to initialize this module for its devices
  2957. */
  2958. static int __init sbridge_init(void)
  2959. {
  2960. const struct x86_cpu_id *id;
  2961. int rc;
  2962. edac_dbg(2, "\n");
  2963. id = x86_match_cpu(sbridge_cpuids);
  2964. if (!id)
  2965. return -ENODEV;
  2966. /* Ensure that the OPSTATE is set correctly for POLL or NMI */
  2967. opstate_init();
  2968. rc = sbridge_probe(id);
  2969. if (rc >= 0) {
  2970. mce_register_decode_chain(&sbridge_mce_dec);
  2971. if (get_edac_report_status() == EDAC_REPORTING_DISABLED)
  2972. sbridge_printk(KERN_WARNING, "Loading driver, error reporting disabled.\n");
  2973. return 0;
  2974. }
  2975. sbridge_printk(KERN_ERR, "Failed to register device with error %d.\n",
  2976. rc);
  2977. return rc;
  2978. }
  2979. /*
  2980. * sbridge_exit() Module exit function
  2981. * Unregister the driver
  2982. */
  2983. static void __exit sbridge_exit(void)
  2984. {
  2985. edac_dbg(2, "\n");
  2986. sbridge_remove();
  2987. mce_unregister_decode_chain(&sbridge_mce_dec);
  2988. }
  2989. module_init(sbridge_init);
  2990. module_exit(sbridge_exit);
  2991. module_param(edac_op_state, int, 0444);
  2992. MODULE_PARM_DESC(edac_op_state, "EDAC Error Reporting state: 0=Poll,1=NMI");
  2993. MODULE_LICENSE("GPL");
  2994. MODULE_AUTHOR("Mauro Carvalho Chehab");
  2995. MODULE_AUTHOR("Red Hat Inc. (http://www.redhat.com)");
  2996. MODULE_DESCRIPTION("MC Driver for Intel Sandy Bridge and Ivy Bridge memory controllers - "
  2997. SBRIDGE_REVISION);