amd.c 34 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * (c) 2005-2016 Advanced Micro Devices, Inc.
  4. *
  5. * Written by Jacob Shin - AMD, Inc.
  6. * Maintained by: Borislav Petkov <bp@alien8.de>
  7. *
  8. * All MC4_MISCi registers are shared between cores on a node.
  9. */
  10. #include <linux/interrupt.h>
  11. #include <linux/notifier.h>
  12. #include <linux/kobject.h>
  13. #include <linux/percpu.h>
  14. #include <linux/errno.h>
  15. #include <linux/sched.h>
  16. #include <linux/sysfs.h>
  17. #include <linux/slab.h>
  18. #include <linux/init.h>
  19. #include <linux/cpu.h>
  20. #include <linux/smp.h>
  21. #include <linux/string.h>
  22. #include <asm/amd_nb.h>
  23. #include <asm/traps.h>
  24. #include <asm/apic.h>
  25. #include <asm/mce.h>
  26. #include <asm/msr.h>
  27. #include <asm/trace/irq_vectors.h>
  28. #include "internal.h"
  29. #define NR_BLOCKS 5
  30. #define THRESHOLD_MAX 0xFFF
  31. #define INT_TYPE_APIC 0x00020000
  32. #define MASK_VALID_HI 0x80000000
  33. #define MASK_CNTP_HI 0x40000000
  34. #define MASK_LOCKED_HI 0x20000000
  35. #define MASK_LVTOFF_HI 0x00F00000
  36. #define MASK_COUNT_EN_HI 0x00080000
  37. #define MASK_INT_TYPE_HI 0x00060000
  38. #define MASK_OVERFLOW_HI 0x00010000
  39. #define MASK_ERR_COUNT_HI 0x00000FFF
  40. #define MASK_BLKPTR_LO 0xFF000000
  41. #define MCG_XBLK_ADDR 0xC0000400
  42. /* Deferred error settings */
  43. #define MSR_CU_DEF_ERR 0xC0000410
  44. #define MASK_DEF_LVTOFF 0x000000F0
  45. #define MASK_DEF_INT_TYPE 0x00000006
  46. #define DEF_LVT_OFF 0x2
  47. #define DEF_INT_TYPE_APIC 0x2
  48. /* Scalable MCA: */
  49. /* Threshold LVT offset is at MSR0xC0000410[15:12] */
  50. #define SMCA_THR_LVT_OFF 0xF000
  51. static bool thresholding_irq_en;
  52. static const char * const th_names[] = {
  53. "load_store",
  54. "insn_fetch",
  55. "combined_unit",
  56. "decode_unit",
  57. "northbridge",
  58. "execution_unit",
  59. };
  60. static const char * const smca_umc_block_names[] = {
  61. "dram_ecc",
  62. "misc_umc"
  63. };
  64. #define HWID_MCATYPE(hwid, mcatype) (((hwid) << 16) | (mcatype))
  65. struct smca_hwid {
  66. unsigned int bank_type; /* Use with smca_bank_types for easy indexing. */
  67. u32 hwid_mcatype; /* (hwid,mcatype) tuple */
  68. };
  69. struct smca_bank {
  70. const struct smca_hwid *hwid;
  71. u32 id; /* Value of MCA_IPID[InstanceId]. */
  72. u8 sysfs_id; /* Value used for sysfs name. */
  73. };
  74. static DEFINE_PER_CPU_READ_MOSTLY(struct smca_bank[MAX_NR_BANKS], smca_banks);
  75. static DEFINE_PER_CPU_READ_MOSTLY(u8[N_SMCA_BANK_TYPES], smca_bank_counts);
  76. static const char * const smca_names[] = {
  77. [SMCA_LS ... SMCA_LS_V2] = "load_store",
  78. [SMCA_IF] = "insn_fetch",
  79. [SMCA_L2_CACHE] = "l2_cache",
  80. [SMCA_DE] = "decode_unit",
  81. [SMCA_RESERVED] = "reserved",
  82. [SMCA_EX] = "execution_unit",
  83. [SMCA_FP] = "floating_point",
  84. [SMCA_L3_CACHE] = "l3_cache",
  85. [SMCA_CS ... SMCA_CS_V2] = "coherent_slave",
  86. [SMCA_PIE] = "pie",
  87. /* UMC v2 is separate because both of them can exist in a single system. */
  88. [SMCA_UMC] = "umc",
  89. [SMCA_UMC_V2] = "umc_v2",
  90. [SMCA_MA_LLC] = "ma_llc",
  91. [SMCA_PB] = "param_block",
  92. [SMCA_PSP ... SMCA_PSP_V2] = "psp",
  93. [SMCA_SMU ... SMCA_SMU_V2] = "smu",
  94. [SMCA_MP5] = "mp5",
  95. [SMCA_MPDMA] = "mpdma",
  96. [SMCA_NBIO] = "nbio",
  97. [SMCA_PCIE ... SMCA_PCIE_V2] = "pcie",
  98. [SMCA_XGMI_PCS] = "xgmi_pcs",
  99. [SMCA_NBIF] = "nbif",
  100. [SMCA_SHUB] = "shub",
  101. [SMCA_SATA] = "sata",
  102. [SMCA_USB] = "usb",
  103. [SMCA_USR_DP] = "usr_dp",
  104. [SMCA_USR_CP] = "usr_cp",
  105. [SMCA_GMI_PCS] = "gmi_pcs",
  106. [SMCA_XGMI_PHY] = "xgmi_phy",
  107. [SMCA_WAFL_PHY] = "wafl_phy",
  108. [SMCA_GMI_PHY] = "gmi_phy",
  109. };
  110. static const char *smca_get_name(enum smca_bank_types t)
  111. {
  112. if (t >= N_SMCA_BANK_TYPES)
  113. return NULL;
  114. return smca_names[t];
  115. }
  116. enum smca_bank_types smca_get_bank_type(unsigned int cpu, unsigned int bank)
  117. {
  118. struct smca_bank *b;
  119. if (bank >= MAX_NR_BANKS)
  120. return N_SMCA_BANK_TYPES;
  121. b = &per_cpu(smca_banks, cpu)[bank];
  122. if (!b->hwid)
  123. return N_SMCA_BANK_TYPES;
  124. return b->hwid->bank_type;
  125. }
  126. EXPORT_SYMBOL_GPL(smca_get_bank_type);
  127. static const struct smca_hwid smca_hwid_mcatypes[] = {
  128. /* { bank_type, hwid_mcatype } */
  129. /* Reserved type */
  130. { SMCA_RESERVED, HWID_MCATYPE(0x00, 0x0) },
  131. /* ZN Core (HWID=0xB0) MCA types */
  132. { SMCA_LS, HWID_MCATYPE(0xB0, 0x0) },
  133. { SMCA_LS_V2, HWID_MCATYPE(0xB0, 0x10) },
  134. { SMCA_IF, HWID_MCATYPE(0xB0, 0x1) },
  135. { SMCA_L2_CACHE, HWID_MCATYPE(0xB0, 0x2) },
  136. { SMCA_DE, HWID_MCATYPE(0xB0, 0x3) },
  137. /* HWID 0xB0 MCATYPE 0x4 is Reserved */
  138. { SMCA_EX, HWID_MCATYPE(0xB0, 0x5) },
  139. { SMCA_FP, HWID_MCATYPE(0xB0, 0x6) },
  140. { SMCA_L3_CACHE, HWID_MCATYPE(0xB0, 0x7) },
  141. /* Data Fabric MCA types */
  142. { SMCA_CS, HWID_MCATYPE(0x2E, 0x0) },
  143. { SMCA_PIE, HWID_MCATYPE(0x2E, 0x1) },
  144. { SMCA_CS_V2, HWID_MCATYPE(0x2E, 0x2) },
  145. { SMCA_MA_LLC, HWID_MCATYPE(0x2E, 0x4) },
  146. /* Unified Memory Controller MCA type */
  147. { SMCA_UMC, HWID_MCATYPE(0x96, 0x0) },
  148. { SMCA_UMC_V2, HWID_MCATYPE(0x96, 0x1) },
  149. /* Parameter Block MCA type */
  150. { SMCA_PB, HWID_MCATYPE(0x05, 0x0) },
  151. /* Platform Security Processor MCA type */
  152. { SMCA_PSP, HWID_MCATYPE(0xFF, 0x0) },
  153. { SMCA_PSP_V2, HWID_MCATYPE(0xFF, 0x1) },
  154. /* System Management Unit MCA type */
  155. { SMCA_SMU, HWID_MCATYPE(0x01, 0x0) },
  156. { SMCA_SMU_V2, HWID_MCATYPE(0x01, 0x1) },
  157. /* Microprocessor 5 Unit MCA type */
  158. { SMCA_MP5, HWID_MCATYPE(0x01, 0x2) },
  159. /* MPDMA MCA type */
  160. { SMCA_MPDMA, HWID_MCATYPE(0x01, 0x3) },
  161. /* Northbridge IO Unit MCA type */
  162. { SMCA_NBIO, HWID_MCATYPE(0x18, 0x0) },
  163. /* PCI Express Unit MCA type */
  164. { SMCA_PCIE, HWID_MCATYPE(0x46, 0x0) },
  165. { SMCA_PCIE_V2, HWID_MCATYPE(0x46, 0x1) },
  166. { SMCA_XGMI_PCS, HWID_MCATYPE(0x50, 0x0) },
  167. { SMCA_NBIF, HWID_MCATYPE(0x6C, 0x0) },
  168. { SMCA_SHUB, HWID_MCATYPE(0x80, 0x0) },
  169. { SMCA_SATA, HWID_MCATYPE(0xA8, 0x0) },
  170. { SMCA_USB, HWID_MCATYPE(0xAA, 0x0) },
  171. { SMCA_USR_DP, HWID_MCATYPE(0x170, 0x0) },
  172. { SMCA_USR_CP, HWID_MCATYPE(0x180, 0x0) },
  173. { SMCA_GMI_PCS, HWID_MCATYPE(0x241, 0x0) },
  174. { SMCA_XGMI_PHY, HWID_MCATYPE(0x259, 0x0) },
  175. { SMCA_WAFL_PHY, HWID_MCATYPE(0x267, 0x0) },
  176. { SMCA_GMI_PHY, HWID_MCATYPE(0x269, 0x0) },
  177. };
  178. /*
  179. * In SMCA enabled processors, we can have multiple banks for a given IP type.
  180. * So to define a unique name for each bank, we use a temp c-string to append
  181. * the MCA_IPID[InstanceId] to type's name in get_name().
  182. *
  183. * InstanceId is 32 bits which is 8 characters. Make sure MAX_MCATYPE_NAME_LEN
  184. * is greater than 8 plus 1 (for underscore) plus length of longest type name.
  185. */
  186. #define MAX_MCATYPE_NAME_LEN 30
  187. static char buf_mcatype[MAX_MCATYPE_NAME_LEN];
  188. static DEFINE_PER_CPU(struct threshold_bank **, threshold_banks);
  189. /*
  190. * A list of the banks enabled on each logical CPU. Controls which respective
  191. * descriptors to initialize later in mce_threshold_create_device().
  192. */
  193. static DEFINE_PER_CPU(u64, bank_map);
  194. /* Map of banks that have more than MCA_MISC0 available. */
  195. static DEFINE_PER_CPU(u64, smca_misc_banks_map);
  196. static void amd_threshold_interrupt(void);
  197. static void amd_deferred_error_interrupt(void);
  198. static void default_deferred_error_interrupt(void)
  199. {
  200. pr_err("Unexpected deferred interrupt at vector %x\n", DEFERRED_ERROR_VECTOR);
  201. }
  202. void (*deferred_error_int_vector)(void) = default_deferred_error_interrupt;
  203. static void smca_set_misc_banks_map(unsigned int bank, unsigned int cpu)
  204. {
  205. u32 low, high;
  206. /*
  207. * For SMCA enabled processors, BLKPTR field of the first MISC register
  208. * (MCx_MISC0) indicates presence of additional MISC regs set (MISC1-4).
  209. */
  210. if (rdmsr_safe(MSR_AMD64_SMCA_MCx_CONFIG(bank), &low, &high))
  211. return;
  212. if (!(low & MCI_CONFIG_MCAX))
  213. return;
  214. if (rdmsr_safe(MSR_AMD64_SMCA_MCx_MISC(bank), &low, &high))
  215. return;
  216. if (low & MASK_BLKPTR_LO)
  217. per_cpu(smca_misc_banks_map, cpu) |= BIT_ULL(bank);
  218. }
  219. static void smca_configure(unsigned int bank, unsigned int cpu)
  220. {
  221. u8 *bank_counts = this_cpu_ptr(smca_bank_counts);
  222. const struct smca_hwid *s_hwid;
  223. unsigned int i, hwid_mcatype;
  224. u32 high, low;
  225. u32 smca_config = MSR_AMD64_SMCA_MCx_CONFIG(bank);
  226. /* Set appropriate bits in MCA_CONFIG */
  227. if (!rdmsr_safe(smca_config, &low, &high)) {
  228. /*
  229. * OS is required to set the MCAX bit to acknowledge that it is
  230. * now using the new MSR ranges and new registers under each
  231. * bank. It also means that the OS will configure deferred
  232. * errors in the new MCx_CONFIG register. If the bit is not set,
  233. * uncorrectable errors will cause a system panic.
  234. *
  235. * MCA_CONFIG[MCAX] is bit 32 (0 in the high portion of the MSR.)
  236. */
  237. high |= BIT(0);
  238. /*
  239. * SMCA sets the Deferred Error Interrupt type per bank.
  240. *
  241. * MCA_CONFIG[DeferredIntTypeSupported] is bit 5, and tells us
  242. * if the DeferredIntType bit field is available.
  243. *
  244. * MCA_CONFIG[DeferredIntType] is bits [38:37] ([6:5] in the
  245. * high portion of the MSR). OS should set this to 0x1 to enable
  246. * APIC based interrupt. First, check that no interrupt has been
  247. * set.
  248. */
  249. if ((low & BIT(5)) && !((high >> 5) & 0x3))
  250. high |= BIT(5);
  251. this_cpu_ptr(mce_banks_array)[bank].lsb_in_status = !!(low & BIT(8));
  252. wrmsr(smca_config, low, high);
  253. }
  254. smca_set_misc_banks_map(bank, cpu);
  255. if (rdmsr_safe(MSR_AMD64_SMCA_MCx_IPID(bank), &low, &high)) {
  256. pr_warn("Failed to read MCA_IPID for bank %d\n", bank);
  257. return;
  258. }
  259. hwid_mcatype = HWID_MCATYPE(high & MCI_IPID_HWID,
  260. (high & MCI_IPID_MCATYPE) >> 16);
  261. for (i = 0; i < ARRAY_SIZE(smca_hwid_mcatypes); i++) {
  262. s_hwid = &smca_hwid_mcatypes[i];
  263. if (hwid_mcatype == s_hwid->hwid_mcatype) {
  264. this_cpu_ptr(smca_banks)[bank].hwid = s_hwid;
  265. this_cpu_ptr(smca_banks)[bank].id = low;
  266. this_cpu_ptr(smca_banks)[bank].sysfs_id = bank_counts[s_hwid->bank_type]++;
  267. break;
  268. }
  269. }
  270. }
  271. struct thresh_restart {
  272. struct threshold_block *b;
  273. int set_lvt_off;
  274. int lvt_off;
  275. u16 old_limit;
  276. };
  277. static inline bool is_shared_bank(int bank)
  278. {
  279. /*
  280. * Scalable MCA provides for only one core to have access to the MSRs of
  281. * a shared bank.
  282. */
  283. if (mce_flags.smca)
  284. return false;
  285. /* Bank 4 is for northbridge reporting and is thus shared */
  286. return (bank == 4);
  287. }
  288. static const char *bank4_names(const struct threshold_block *b)
  289. {
  290. switch (b->address) {
  291. /* MSR4_MISC0 */
  292. case 0x00000413:
  293. return "dram";
  294. case 0xc0000408:
  295. return "ht_links";
  296. case 0xc0000409:
  297. return "l3_cache";
  298. default:
  299. WARN(1, "Funny MSR: 0x%08x\n", b->address);
  300. return "";
  301. }
  302. };
  303. static bool lvt_interrupt_supported(unsigned int bank, u32 msr_high_bits)
  304. {
  305. /*
  306. * bank 4 supports APIC LVT interrupts implicitly since forever.
  307. */
  308. if (bank == 4)
  309. return true;
  310. /*
  311. * IntP: interrupt present; if this bit is set, the thresholding
  312. * bank can generate APIC LVT interrupts
  313. */
  314. return msr_high_bits & BIT(28);
  315. }
  316. static int lvt_off_valid(struct threshold_block *b, int apic, u32 lo, u32 hi)
  317. {
  318. int msr = (hi & MASK_LVTOFF_HI) >> 20;
  319. if (apic < 0) {
  320. pr_err(FW_BUG "cpu %d, failed to setup threshold interrupt "
  321. "for bank %d, block %d (MSR%08X=0x%x%08x)\n", b->cpu,
  322. b->bank, b->block, b->address, hi, lo);
  323. return 0;
  324. }
  325. if (apic != msr) {
  326. /*
  327. * On SMCA CPUs, LVT offset is programmed at a different MSR, and
  328. * the BIOS provides the value. The original field where LVT offset
  329. * was set is reserved. Return early here:
  330. */
  331. if (mce_flags.smca)
  332. return 0;
  333. pr_err(FW_BUG "cpu %d, invalid threshold interrupt offset %d "
  334. "for bank %d, block %d (MSR%08X=0x%x%08x)\n",
  335. b->cpu, apic, b->bank, b->block, b->address, hi, lo);
  336. return 0;
  337. }
  338. return 1;
  339. };
  340. /* Reprogram MCx_MISC MSR behind this threshold bank. */
  341. static void threshold_restart_bank(void *_tr)
  342. {
  343. struct thresh_restart *tr = _tr;
  344. u32 hi, lo;
  345. /* sysfs write might race against an offline operation */
  346. if (!this_cpu_read(threshold_banks) && !tr->set_lvt_off)
  347. return;
  348. rdmsr(tr->b->address, lo, hi);
  349. /*
  350. * Reset error count and overflow bit.
  351. * This is done during init or after handling an interrupt.
  352. */
  353. if (hi & MASK_OVERFLOW_HI || tr->set_lvt_off) {
  354. hi &= ~(MASK_ERR_COUNT_HI | MASK_OVERFLOW_HI);
  355. hi |= THRESHOLD_MAX - tr->b->threshold_limit;
  356. } else if (tr->old_limit) { /* change limit w/o reset */
  357. int new_count = (hi & THRESHOLD_MAX) +
  358. (tr->old_limit - tr->b->threshold_limit);
  359. hi = (hi & ~MASK_ERR_COUNT_HI) |
  360. (new_count & THRESHOLD_MAX);
  361. }
  362. /* clear IntType */
  363. hi &= ~MASK_INT_TYPE_HI;
  364. if (!tr->b->interrupt_capable)
  365. goto done;
  366. if (tr->set_lvt_off) {
  367. if (lvt_off_valid(tr->b, tr->lvt_off, lo, hi)) {
  368. /* set new lvt offset */
  369. hi &= ~MASK_LVTOFF_HI;
  370. hi |= tr->lvt_off << 20;
  371. }
  372. }
  373. if (tr->b->interrupt_enable)
  374. hi |= INT_TYPE_APIC;
  375. done:
  376. hi |= MASK_COUNT_EN_HI;
  377. wrmsr(tr->b->address, lo, hi);
  378. }
  379. static void mce_threshold_block_init(struct threshold_block *b, int offset)
  380. {
  381. struct thresh_restart tr = {
  382. .b = b,
  383. .set_lvt_off = 1,
  384. .lvt_off = offset,
  385. };
  386. b->threshold_limit = THRESHOLD_MAX;
  387. threshold_restart_bank(&tr);
  388. };
  389. static int setup_APIC_mce_threshold(int reserved, int new)
  390. {
  391. if (reserved < 0 && !setup_APIC_eilvt(new, THRESHOLD_APIC_VECTOR,
  392. APIC_EILVT_MSG_FIX, 0))
  393. return new;
  394. return reserved;
  395. }
  396. static int setup_APIC_deferred_error(int reserved, int new)
  397. {
  398. if (reserved < 0 && !setup_APIC_eilvt(new, DEFERRED_ERROR_VECTOR,
  399. APIC_EILVT_MSG_FIX, 0))
  400. return new;
  401. return reserved;
  402. }
  403. static void deferred_error_interrupt_enable(struct cpuinfo_x86 *c)
  404. {
  405. u32 low = 0, high = 0;
  406. int def_offset = -1, def_new;
  407. if (rdmsr_safe(MSR_CU_DEF_ERR, &low, &high))
  408. return;
  409. def_new = (low & MASK_DEF_LVTOFF) >> 4;
  410. if (!(low & MASK_DEF_LVTOFF)) {
  411. pr_err(FW_BUG "Your BIOS is not setting up LVT offset 0x2 for deferred error IRQs correctly.\n");
  412. def_new = DEF_LVT_OFF;
  413. low = (low & ~MASK_DEF_LVTOFF) | (DEF_LVT_OFF << 4);
  414. }
  415. def_offset = setup_APIC_deferred_error(def_offset, def_new);
  416. if ((def_offset == def_new) &&
  417. (deferred_error_int_vector != amd_deferred_error_interrupt))
  418. deferred_error_int_vector = amd_deferred_error_interrupt;
  419. if (!mce_flags.smca)
  420. low = (low & ~MASK_DEF_INT_TYPE) | DEF_INT_TYPE_APIC;
  421. wrmsr(MSR_CU_DEF_ERR, low, high);
  422. }
  423. static u32 smca_get_block_address(unsigned int bank, unsigned int block,
  424. unsigned int cpu)
  425. {
  426. if (!block)
  427. return MSR_AMD64_SMCA_MCx_MISC(bank);
  428. if (!(per_cpu(smca_misc_banks_map, cpu) & BIT_ULL(bank)))
  429. return 0;
  430. return MSR_AMD64_SMCA_MCx_MISCy(bank, block - 1);
  431. }
  432. static u32 get_block_address(u32 current_addr, u32 low, u32 high,
  433. unsigned int bank, unsigned int block,
  434. unsigned int cpu)
  435. {
  436. u32 addr = 0, offset = 0;
  437. if ((bank >= per_cpu(mce_num_banks, cpu)) || (block >= NR_BLOCKS))
  438. return addr;
  439. if (mce_flags.smca)
  440. return smca_get_block_address(bank, block, cpu);
  441. /* Fall back to method we used for older processors: */
  442. switch (block) {
  443. case 0:
  444. addr = mca_msr_reg(bank, MCA_MISC);
  445. break;
  446. case 1:
  447. offset = ((low & MASK_BLKPTR_LO) >> 21);
  448. if (offset)
  449. addr = MCG_XBLK_ADDR + offset;
  450. break;
  451. default:
  452. addr = ++current_addr;
  453. }
  454. return addr;
  455. }
  456. static int
  457. prepare_threshold_block(unsigned int bank, unsigned int block, u32 addr,
  458. int offset, u32 misc_high)
  459. {
  460. unsigned int cpu = smp_processor_id();
  461. u32 smca_low, smca_high;
  462. struct threshold_block b;
  463. int new;
  464. if (!block)
  465. per_cpu(bank_map, cpu) |= BIT_ULL(bank);
  466. memset(&b, 0, sizeof(b));
  467. b.cpu = cpu;
  468. b.bank = bank;
  469. b.block = block;
  470. b.address = addr;
  471. b.interrupt_capable = lvt_interrupt_supported(bank, misc_high);
  472. if (!b.interrupt_capable)
  473. goto done;
  474. b.interrupt_enable = 1;
  475. if (!mce_flags.smca) {
  476. new = (misc_high & MASK_LVTOFF_HI) >> 20;
  477. goto set_offset;
  478. }
  479. /* Gather LVT offset for thresholding: */
  480. if (rdmsr_safe(MSR_CU_DEF_ERR, &smca_low, &smca_high))
  481. goto out;
  482. new = (smca_low & SMCA_THR_LVT_OFF) >> 12;
  483. set_offset:
  484. offset = setup_APIC_mce_threshold(offset, new);
  485. if (offset == new)
  486. thresholding_irq_en = true;
  487. done:
  488. mce_threshold_block_init(&b, offset);
  489. out:
  490. return offset;
  491. }
  492. bool amd_filter_mce(struct mce *m)
  493. {
  494. enum smca_bank_types bank_type = smca_get_bank_type(m->extcpu, m->bank);
  495. struct cpuinfo_x86 *c = &boot_cpu_data;
  496. /* See Family 17h Models 10h-2Fh Erratum #1114. */
  497. if (c->x86 == 0x17 &&
  498. c->x86_model >= 0x10 && c->x86_model <= 0x2F &&
  499. bank_type == SMCA_IF && XEC(m->status, 0x3f) == 10)
  500. return true;
  501. /* NB GART TLB error reporting is disabled by default. */
  502. if (c->x86 < 0x17) {
  503. if (m->bank == 4 && XEC(m->status, 0x1f) == 0x5)
  504. return true;
  505. }
  506. return false;
  507. }
  508. /*
  509. * Turn off thresholding banks for the following conditions:
  510. * - MC4_MISC thresholding is not supported on Family 0x15.
  511. * - Prevent possible spurious interrupts from the IF bank on Family 0x17
  512. * Models 0x10-0x2F due to Erratum #1114.
  513. */
  514. static void disable_err_thresholding(struct cpuinfo_x86 *c, unsigned int bank)
  515. {
  516. int i, num_msrs;
  517. u64 hwcr;
  518. bool need_toggle;
  519. u32 msrs[NR_BLOCKS];
  520. if (c->x86 == 0x15 && bank == 4) {
  521. msrs[0] = 0x00000413; /* MC4_MISC0 */
  522. msrs[1] = 0xc0000408; /* MC4_MISC1 */
  523. num_msrs = 2;
  524. } else if (c->x86 == 0x17 &&
  525. (c->x86_model >= 0x10 && c->x86_model <= 0x2F)) {
  526. if (smca_get_bank_type(smp_processor_id(), bank) != SMCA_IF)
  527. return;
  528. msrs[0] = MSR_AMD64_SMCA_MCx_MISC(bank);
  529. num_msrs = 1;
  530. } else {
  531. return;
  532. }
  533. rdmsrl(MSR_K7_HWCR, hwcr);
  534. /* McStatusWrEn has to be set */
  535. need_toggle = !(hwcr & BIT(18));
  536. if (need_toggle)
  537. wrmsrl(MSR_K7_HWCR, hwcr | BIT(18));
  538. /* Clear CntP bit safely */
  539. for (i = 0; i < num_msrs; i++)
  540. msr_clear_bit(msrs[i], 62);
  541. /* restore old settings */
  542. if (need_toggle)
  543. wrmsrl(MSR_K7_HWCR, hwcr);
  544. }
  545. /* cpu init entry point, called from mce.c with preempt off */
  546. void mce_amd_feature_init(struct cpuinfo_x86 *c)
  547. {
  548. unsigned int bank, block, cpu = smp_processor_id();
  549. u32 low = 0, high = 0, address = 0;
  550. int offset = -1;
  551. for (bank = 0; bank < this_cpu_read(mce_num_banks); ++bank) {
  552. if (mce_flags.smca)
  553. smca_configure(bank, cpu);
  554. disable_err_thresholding(c, bank);
  555. for (block = 0; block < NR_BLOCKS; ++block) {
  556. address = get_block_address(address, low, high, bank, block, cpu);
  557. if (!address)
  558. break;
  559. if (rdmsr_safe(address, &low, &high))
  560. break;
  561. if (!(high & MASK_VALID_HI))
  562. continue;
  563. if (!(high & MASK_CNTP_HI) ||
  564. (high & MASK_LOCKED_HI))
  565. continue;
  566. offset = prepare_threshold_block(bank, block, address, offset, high);
  567. }
  568. }
  569. if (mce_flags.succor)
  570. deferred_error_interrupt_enable(c);
  571. }
  572. /*
  573. * DRAM ECC errors are reported in the Northbridge (bank 4) with
  574. * Extended Error Code 8.
  575. */
  576. static bool legacy_mce_is_memory_error(struct mce *m)
  577. {
  578. return m->bank == 4 && XEC(m->status, 0x1f) == 8;
  579. }
  580. /*
  581. * DRAM ECC errors are reported in Unified Memory Controllers with
  582. * Extended Error Code 0.
  583. */
  584. static bool smca_mce_is_memory_error(struct mce *m)
  585. {
  586. enum smca_bank_types bank_type;
  587. if (XEC(m->status, 0x3f))
  588. return false;
  589. bank_type = smca_get_bank_type(m->extcpu, m->bank);
  590. return bank_type == SMCA_UMC || bank_type == SMCA_UMC_V2;
  591. }
  592. bool amd_mce_is_memory_error(struct mce *m)
  593. {
  594. if (mce_flags.smca)
  595. return smca_mce_is_memory_error(m);
  596. else
  597. return legacy_mce_is_memory_error(m);
  598. }
  599. /*
  600. * AMD systems do not have an explicit indicator that the value in MCA_ADDR is
  601. * a system physical address. Therefore, individual cases need to be detected.
  602. * Future cases and checks will be added as needed.
  603. *
  604. * 1) General case
  605. * a) Assume address is not usable.
  606. * 2) Poison errors
  607. * a) Indicated by MCA_STATUS[43]: poison. Defined for all banks except legacy
  608. * northbridge (bank 4).
  609. * b) Refers to poison consumption in the core. Does not include "no action",
  610. * "action optional", or "deferred" error severities.
  611. * c) Will include a usable address so that immediate action can be taken.
  612. * 3) Northbridge DRAM ECC errors
  613. * a) Reported in legacy bank 4 with extended error code (XEC) 8.
  614. * b) MCA_STATUS[43] is *not* defined as poison in legacy bank 4. Therefore,
  615. * this bit should not be checked.
  616. *
  617. * NOTE: SMCA UMC memory errors fall into case #1.
  618. */
  619. bool amd_mce_usable_address(struct mce *m)
  620. {
  621. /* Check special northbridge case 3) first. */
  622. if (!mce_flags.smca) {
  623. if (legacy_mce_is_memory_error(m))
  624. return true;
  625. else if (m->bank == 4)
  626. return false;
  627. }
  628. /* Check poison bit for all other bank types. */
  629. if (m->status & MCI_STATUS_POISON)
  630. return true;
  631. /* Assume address is not usable for all others. */
  632. return false;
  633. }
  634. static void __log_error(unsigned int bank, u64 status, u64 addr, u64 misc)
  635. {
  636. struct mce m;
  637. mce_prep_record(&m);
  638. m.status = status;
  639. m.misc = misc;
  640. m.bank = bank;
  641. m.tsc = rdtsc();
  642. if (m.status & MCI_STATUS_ADDRV) {
  643. m.addr = addr;
  644. smca_extract_err_addr(&m);
  645. }
  646. if (mce_flags.smca) {
  647. rdmsrl(MSR_AMD64_SMCA_MCx_IPID(bank), m.ipid);
  648. if (m.status & MCI_STATUS_SYNDV)
  649. rdmsrl(MSR_AMD64_SMCA_MCx_SYND(bank), m.synd);
  650. }
  651. mce_log(&m);
  652. }
  653. DEFINE_IDTENTRY_SYSVEC(sysvec_deferred_error)
  654. {
  655. trace_deferred_error_apic_entry(DEFERRED_ERROR_VECTOR);
  656. inc_irq_stat(irq_deferred_error_count);
  657. deferred_error_int_vector();
  658. trace_deferred_error_apic_exit(DEFERRED_ERROR_VECTOR);
  659. apic_eoi();
  660. }
  661. /*
  662. * Returns true if the logged error is deferred. False, otherwise.
  663. */
  664. static inline bool
  665. _log_error_bank(unsigned int bank, u32 msr_stat, u32 msr_addr, u64 misc)
  666. {
  667. u64 status, addr = 0;
  668. rdmsrl(msr_stat, status);
  669. if (!(status & MCI_STATUS_VAL))
  670. return false;
  671. if (status & MCI_STATUS_ADDRV)
  672. rdmsrl(msr_addr, addr);
  673. __log_error(bank, status, addr, misc);
  674. wrmsrl(msr_stat, 0);
  675. return status & MCI_STATUS_DEFERRED;
  676. }
  677. static bool _log_error_deferred(unsigned int bank, u32 misc)
  678. {
  679. if (!_log_error_bank(bank, mca_msr_reg(bank, MCA_STATUS),
  680. mca_msr_reg(bank, MCA_ADDR), misc))
  681. return false;
  682. /*
  683. * Non-SMCA systems don't have MCA_DESTAT/MCA_DEADDR registers.
  684. * Return true here to avoid accessing these registers.
  685. */
  686. if (!mce_flags.smca)
  687. return true;
  688. /* Clear MCA_DESTAT if the deferred error was logged from MCA_STATUS. */
  689. wrmsrl(MSR_AMD64_SMCA_MCx_DESTAT(bank), 0);
  690. return true;
  691. }
  692. /*
  693. * We have three scenarios for checking for Deferred errors:
  694. *
  695. * 1) Non-SMCA systems check MCA_STATUS and log error if found.
  696. * 2) SMCA systems check MCA_STATUS. If error is found then log it and also
  697. * clear MCA_DESTAT.
  698. * 3) SMCA systems check MCA_DESTAT, if error was not found in MCA_STATUS, and
  699. * log it.
  700. */
  701. static void log_error_deferred(unsigned int bank)
  702. {
  703. if (_log_error_deferred(bank, 0))
  704. return;
  705. /*
  706. * Only deferred errors are logged in MCA_DE{STAT,ADDR} so just check
  707. * for a valid error.
  708. */
  709. _log_error_bank(bank, MSR_AMD64_SMCA_MCx_DESTAT(bank),
  710. MSR_AMD64_SMCA_MCx_DEADDR(bank), 0);
  711. }
  712. /* APIC interrupt handler for deferred errors */
  713. static void amd_deferred_error_interrupt(void)
  714. {
  715. unsigned int bank;
  716. for (bank = 0; bank < this_cpu_read(mce_num_banks); ++bank)
  717. log_error_deferred(bank);
  718. }
  719. static void log_error_thresholding(unsigned int bank, u64 misc)
  720. {
  721. _log_error_deferred(bank, misc);
  722. }
  723. static void log_and_reset_block(struct threshold_block *block)
  724. {
  725. struct thresh_restart tr;
  726. u32 low = 0, high = 0;
  727. if (!block)
  728. return;
  729. if (rdmsr_safe(block->address, &low, &high))
  730. return;
  731. if (!(high & MASK_OVERFLOW_HI))
  732. return;
  733. /* Log the MCE which caused the threshold event. */
  734. log_error_thresholding(block->bank, ((u64)high << 32) | low);
  735. /* Reset threshold block after logging error. */
  736. memset(&tr, 0, sizeof(tr));
  737. tr.b = block;
  738. threshold_restart_bank(&tr);
  739. }
  740. /*
  741. * Threshold interrupt handler will service THRESHOLD_APIC_VECTOR. The interrupt
  742. * goes off when error_count reaches threshold_limit.
  743. */
  744. static void amd_threshold_interrupt(void)
  745. {
  746. struct threshold_block *first_block = NULL, *block = NULL, *tmp = NULL;
  747. struct threshold_bank **bp = this_cpu_read(threshold_banks);
  748. unsigned int bank, cpu = smp_processor_id();
  749. /*
  750. * Validate that the threshold bank has been initialized already. The
  751. * handler is installed at boot time, but on a hotplug event the
  752. * interrupt might fire before the data has been initialized.
  753. */
  754. if (!bp)
  755. return;
  756. for (bank = 0; bank < this_cpu_read(mce_num_banks); ++bank) {
  757. if (!(per_cpu(bank_map, cpu) & BIT_ULL(bank)))
  758. continue;
  759. first_block = bp[bank]->blocks;
  760. if (!first_block)
  761. continue;
  762. /*
  763. * The first block is also the head of the list. Check it first
  764. * before iterating over the rest.
  765. */
  766. log_and_reset_block(first_block);
  767. list_for_each_entry_safe(block, tmp, &first_block->miscj, miscj)
  768. log_and_reset_block(block);
  769. }
  770. }
  771. /*
  772. * Sysfs Interface
  773. */
  774. struct threshold_attr {
  775. struct attribute attr;
  776. ssize_t (*show) (struct threshold_block *, char *);
  777. ssize_t (*store) (struct threshold_block *, const char *, size_t count);
  778. };
  779. #define SHOW_FIELDS(name) \
  780. static ssize_t show_ ## name(struct threshold_block *b, char *buf) \
  781. { \
  782. return sprintf(buf, "%lu\n", (unsigned long) b->name); \
  783. }
  784. SHOW_FIELDS(interrupt_enable)
  785. SHOW_FIELDS(threshold_limit)
  786. static ssize_t
  787. store_interrupt_enable(struct threshold_block *b, const char *buf, size_t size)
  788. {
  789. struct thresh_restart tr;
  790. unsigned long new;
  791. if (!b->interrupt_capable)
  792. return -EINVAL;
  793. if (kstrtoul(buf, 0, &new) < 0)
  794. return -EINVAL;
  795. b->interrupt_enable = !!new;
  796. memset(&tr, 0, sizeof(tr));
  797. tr.b = b;
  798. if (smp_call_function_single(b->cpu, threshold_restart_bank, &tr, 1))
  799. return -ENODEV;
  800. return size;
  801. }
  802. static ssize_t
  803. store_threshold_limit(struct threshold_block *b, const char *buf, size_t size)
  804. {
  805. struct thresh_restart tr;
  806. unsigned long new;
  807. if (kstrtoul(buf, 0, &new) < 0)
  808. return -EINVAL;
  809. if (new > THRESHOLD_MAX)
  810. new = THRESHOLD_MAX;
  811. if (new < 1)
  812. new = 1;
  813. memset(&tr, 0, sizeof(tr));
  814. tr.old_limit = b->threshold_limit;
  815. b->threshold_limit = new;
  816. tr.b = b;
  817. if (smp_call_function_single(b->cpu, threshold_restart_bank, &tr, 1))
  818. return -ENODEV;
  819. return size;
  820. }
  821. static ssize_t show_error_count(struct threshold_block *b, char *buf)
  822. {
  823. u32 lo, hi;
  824. /* CPU might be offline by now */
  825. if (rdmsr_on_cpu(b->cpu, b->address, &lo, &hi))
  826. return -ENODEV;
  827. return sprintf(buf, "%u\n", ((hi & THRESHOLD_MAX) -
  828. (THRESHOLD_MAX - b->threshold_limit)));
  829. }
  830. static struct threshold_attr error_count = {
  831. .attr = {.name = __stringify(error_count), .mode = 0444 },
  832. .show = show_error_count,
  833. };
  834. #define RW_ATTR(val) \
  835. static struct threshold_attr val = { \
  836. .attr = {.name = __stringify(val), .mode = 0644 }, \
  837. .show = show_## val, \
  838. .store = store_## val, \
  839. };
  840. RW_ATTR(interrupt_enable);
  841. RW_ATTR(threshold_limit);
  842. static struct attribute *default_attrs[] = {
  843. &threshold_limit.attr,
  844. &error_count.attr,
  845. NULL, /* possibly interrupt_enable if supported, see below */
  846. NULL,
  847. };
  848. ATTRIBUTE_GROUPS(default);
  849. #define to_block(k) container_of(k, struct threshold_block, kobj)
  850. #define to_attr(a) container_of(a, struct threshold_attr, attr)
  851. static ssize_t show(struct kobject *kobj, struct attribute *attr, char *buf)
  852. {
  853. struct threshold_block *b = to_block(kobj);
  854. struct threshold_attr *a = to_attr(attr);
  855. ssize_t ret;
  856. ret = a->show ? a->show(b, buf) : -EIO;
  857. return ret;
  858. }
  859. static ssize_t store(struct kobject *kobj, struct attribute *attr,
  860. const char *buf, size_t count)
  861. {
  862. struct threshold_block *b = to_block(kobj);
  863. struct threshold_attr *a = to_attr(attr);
  864. ssize_t ret;
  865. ret = a->store ? a->store(b, buf, count) : -EIO;
  866. return ret;
  867. }
  868. static const struct sysfs_ops threshold_ops = {
  869. .show = show,
  870. .store = store,
  871. };
  872. static void threshold_block_release(struct kobject *kobj);
  873. static const struct kobj_type threshold_ktype = {
  874. .sysfs_ops = &threshold_ops,
  875. .default_groups = default_groups,
  876. .release = threshold_block_release,
  877. };
  878. static const char *get_name(unsigned int cpu, unsigned int bank, struct threshold_block *b)
  879. {
  880. enum smca_bank_types bank_type;
  881. if (!mce_flags.smca) {
  882. if (b && bank == 4)
  883. return bank4_names(b);
  884. return th_names[bank];
  885. }
  886. bank_type = smca_get_bank_type(cpu, bank);
  887. if (b && (bank_type == SMCA_UMC || bank_type == SMCA_UMC_V2)) {
  888. if (b->block < ARRAY_SIZE(smca_umc_block_names))
  889. return smca_umc_block_names[b->block];
  890. }
  891. if (b && b->block) {
  892. snprintf(buf_mcatype, MAX_MCATYPE_NAME_LEN, "th_block_%u", b->block);
  893. return buf_mcatype;
  894. }
  895. if (bank_type >= N_SMCA_BANK_TYPES) {
  896. snprintf(buf_mcatype, MAX_MCATYPE_NAME_LEN, "th_bank_%u", bank);
  897. return buf_mcatype;
  898. }
  899. if (per_cpu(smca_bank_counts, cpu)[bank_type] == 1)
  900. return smca_get_name(bank_type);
  901. snprintf(buf_mcatype, MAX_MCATYPE_NAME_LEN,
  902. "%s_%u", smca_get_name(bank_type),
  903. per_cpu(smca_banks, cpu)[bank].sysfs_id);
  904. return buf_mcatype;
  905. }
  906. static int allocate_threshold_blocks(unsigned int cpu, struct threshold_bank *tb,
  907. unsigned int bank, unsigned int block,
  908. u32 address)
  909. {
  910. struct threshold_block *b = NULL;
  911. u32 low, high;
  912. int err;
  913. if ((bank >= this_cpu_read(mce_num_banks)) || (block >= NR_BLOCKS))
  914. return 0;
  915. if (rdmsr_safe(address, &low, &high))
  916. return 0;
  917. if (!(high & MASK_VALID_HI)) {
  918. if (block)
  919. goto recurse;
  920. else
  921. return 0;
  922. }
  923. if (!(high & MASK_CNTP_HI) ||
  924. (high & MASK_LOCKED_HI))
  925. goto recurse;
  926. b = kzalloc(sizeof(struct threshold_block), GFP_KERNEL);
  927. if (!b)
  928. return -ENOMEM;
  929. b->block = block;
  930. b->bank = bank;
  931. b->cpu = cpu;
  932. b->address = address;
  933. b->interrupt_enable = 0;
  934. b->interrupt_capable = lvt_interrupt_supported(bank, high);
  935. b->threshold_limit = THRESHOLD_MAX;
  936. if (b->interrupt_capable) {
  937. default_attrs[2] = &interrupt_enable.attr;
  938. b->interrupt_enable = 1;
  939. } else {
  940. default_attrs[2] = NULL;
  941. }
  942. INIT_LIST_HEAD(&b->miscj);
  943. /* This is safe as @tb is not visible yet */
  944. if (tb->blocks)
  945. list_add(&b->miscj, &tb->blocks->miscj);
  946. else
  947. tb->blocks = b;
  948. err = kobject_init_and_add(&b->kobj, &threshold_ktype, tb->kobj, get_name(cpu, bank, b));
  949. if (err)
  950. goto out_free;
  951. recurse:
  952. address = get_block_address(address, low, high, bank, ++block, cpu);
  953. if (!address)
  954. return 0;
  955. err = allocate_threshold_blocks(cpu, tb, bank, block, address);
  956. if (err)
  957. goto out_free;
  958. if (b)
  959. kobject_uevent(&b->kobj, KOBJ_ADD);
  960. return 0;
  961. out_free:
  962. if (b) {
  963. list_del(&b->miscj);
  964. kobject_put(&b->kobj);
  965. }
  966. return err;
  967. }
  968. static int __threshold_add_blocks(struct threshold_bank *b)
  969. {
  970. struct list_head *head = &b->blocks->miscj;
  971. struct threshold_block *pos = NULL;
  972. struct threshold_block *tmp = NULL;
  973. int err = 0;
  974. err = kobject_add(&b->blocks->kobj, b->kobj, b->blocks->kobj.name);
  975. if (err)
  976. return err;
  977. list_for_each_entry_safe(pos, tmp, head, miscj) {
  978. err = kobject_add(&pos->kobj, b->kobj, pos->kobj.name);
  979. if (err) {
  980. list_for_each_entry_safe_reverse(pos, tmp, head, miscj)
  981. kobject_del(&pos->kobj);
  982. return err;
  983. }
  984. }
  985. return err;
  986. }
  987. static int threshold_create_bank(struct threshold_bank **bp, unsigned int cpu,
  988. unsigned int bank)
  989. {
  990. struct device *dev = this_cpu_read(mce_device);
  991. struct amd_northbridge *nb = NULL;
  992. struct threshold_bank *b = NULL;
  993. const char *name = get_name(cpu, bank, NULL);
  994. int err = 0;
  995. if (!dev)
  996. return -ENODEV;
  997. if (is_shared_bank(bank)) {
  998. nb = node_to_amd_nb(topology_amd_node_id(cpu));
  999. /* threshold descriptor already initialized on this node? */
  1000. if (nb && nb->bank4) {
  1001. /* yes, use it */
  1002. b = nb->bank4;
  1003. err = kobject_add(b->kobj, &dev->kobj, name);
  1004. if (err)
  1005. goto out;
  1006. bp[bank] = b;
  1007. refcount_inc(&b->cpus);
  1008. err = __threshold_add_blocks(b);
  1009. goto out;
  1010. }
  1011. }
  1012. b = kzalloc(sizeof(struct threshold_bank), GFP_KERNEL);
  1013. if (!b) {
  1014. err = -ENOMEM;
  1015. goto out;
  1016. }
  1017. /* Associate the bank with the per-CPU MCE device */
  1018. b->kobj = kobject_create_and_add(name, &dev->kobj);
  1019. if (!b->kobj) {
  1020. err = -EINVAL;
  1021. goto out_free;
  1022. }
  1023. if (is_shared_bank(bank)) {
  1024. b->shared = 1;
  1025. refcount_set(&b->cpus, 1);
  1026. /* nb is already initialized, see above */
  1027. if (nb) {
  1028. WARN_ON(nb->bank4);
  1029. nb->bank4 = b;
  1030. }
  1031. }
  1032. err = allocate_threshold_blocks(cpu, b, bank, 0, mca_msr_reg(bank, MCA_MISC));
  1033. if (err)
  1034. goto out_kobj;
  1035. bp[bank] = b;
  1036. return 0;
  1037. out_kobj:
  1038. kobject_put(b->kobj);
  1039. out_free:
  1040. kfree(b);
  1041. out:
  1042. return err;
  1043. }
  1044. static void threshold_block_release(struct kobject *kobj)
  1045. {
  1046. kfree(to_block(kobj));
  1047. }
  1048. static void deallocate_threshold_blocks(struct threshold_bank *bank)
  1049. {
  1050. struct threshold_block *pos, *tmp;
  1051. list_for_each_entry_safe(pos, tmp, &bank->blocks->miscj, miscj) {
  1052. list_del(&pos->miscj);
  1053. kobject_put(&pos->kobj);
  1054. }
  1055. kobject_put(&bank->blocks->kobj);
  1056. }
  1057. static void __threshold_remove_blocks(struct threshold_bank *b)
  1058. {
  1059. struct threshold_block *pos = NULL;
  1060. struct threshold_block *tmp = NULL;
  1061. kobject_put(b->kobj);
  1062. list_for_each_entry_safe(pos, tmp, &b->blocks->miscj, miscj)
  1063. kobject_put(b->kobj);
  1064. }
  1065. static void threshold_remove_bank(struct threshold_bank *bank)
  1066. {
  1067. struct amd_northbridge *nb;
  1068. if (!bank->blocks)
  1069. goto out_free;
  1070. if (!bank->shared)
  1071. goto out_dealloc;
  1072. if (!refcount_dec_and_test(&bank->cpus)) {
  1073. __threshold_remove_blocks(bank);
  1074. return;
  1075. } else {
  1076. /*
  1077. * The last CPU on this node using the shared bank is going
  1078. * away, remove that bank now.
  1079. */
  1080. nb = node_to_amd_nb(topology_amd_node_id(smp_processor_id()));
  1081. nb->bank4 = NULL;
  1082. }
  1083. out_dealloc:
  1084. deallocate_threshold_blocks(bank);
  1085. out_free:
  1086. kobject_put(bank->kobj);
  1087. kfree(bank);
  1088. }
  1089. static void __threshold_remove_device(struct threshold_bank **bp)
  1090. {
  1091. unsigned int bank, numbanks = this_cpu_read(mce_num_banks);
  1092. for (bank = 0; bank < numbanks; bank++) {
  1093. if (!bp[bank])
  1094. continue;
  1095. threshold_remove_bank(bp[bank]);
  1096. bp[bank] = NULL;
  1097. }
  1098. kfree(bp);
  1099. }
  1100. int mce_threshold_remove_device(unsigned int cpu)
  1101. {
  1102. struct threshold_bank **bp = this_cpu_read(threshold_banks);
  1103. if (!bp)
  1104. return 0;
  1105. /*
  1106. * Clear the pointer before cleaning up, so that the interrupt won't
  1107. * touch anything of this.
  1108. */
  1109. this_cpu_write(threshold_banks, NULL);
  1110. __threshold_remove_device(bp);
  1111. return 0;
  1112. }
  1113. /**
  1114. * mce_threshold_create_device - Create the per-CPU MCE threshold device
  1115. * @cpu: The plugged in CPU
  1116. *
  1117. * Create directories and files for all valid threshold banks.
  1118. *
  1119. * This is invoked from the CPU hotplug callback which was installed in
  1120. * mcheck_init_device(). The invocation happens in context of the hotplug
  1121. * thread running on @cpu. The callback is invoked on all CPUs which are
  1122. * online when the callback is installed or during a real hotplug event.
  1123. */
  1124. int mce_threshold_create_device(unsigned int cpu)
  1125. {
  1126. unsigned int numbanks, bank;
  1127. struct threshold_bank **bp;
  1128. int err;
  1129. if (!mce_flags.amd_threshold)
  1130. return 0;
  1131. bp = this_cpu_read(threshold_banks);
  1132. if (bp)
  1133. return 0;
  1134. numbanks = this_cpu_read(mce_num_banks);
  1135. bp = kcalloc(numbanks, sizeof(*bp), GFP_KERNEL);
  1136. if (!bp)
  1137. return -ENOMEM;
  1138. for (bank = 0; bank < numbanks; ++bank) {
  1139. if (!(this_cpu_read(bank_map) & BIT_ULL(bank)))
  1140. continue;
  1141. err = threshold_create_bank(bp, cpu, bank);
  1142. if (err) {
  1143. __threshold_remove_device(bp);
  1144. return err;
  1145. }
  1146. }
  1147. this_cpu_write(threshold_banks, bp);
  1148. if (thresholding_irq_en)
  1149. mce_threshold_vector = amd_threshold_interrupt;
  1150. return 0;
  1151. }