amd.c 33 KB

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