mce_amd.c 34 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420
  1. /*
  2. * (c) 2005-2016 Advanced Micro Devices, Inc.
  3. * Your use of this code is subject to the terms and conditions of the
  4. * GNU general public license version 2. See "COPYING" or
  5. * http://www.gnu.org/licenses/gpl.html
  6. *
  7. * Written by Jacob Shin - AMD, Inc.
  8. * Maintained by: Borislav Petkov <bp@alien8.de>
  9. *
  10. * All MC4_MISCi registers are shared between cores on a node.
  11. */
  12. #include <linux/interrupt.h>
  13. #include <linux/notifier.h>
  14. #include <linux/kobject.h>
  15. #include <linux/percpu.h>
  16. #include <linux/errno.h>
  17. #include <linux/sched.h>
  18. #include <linux/sysfs.h>
  19. #include <linux/slab.h>
  20. #include <linux/init.h>
  21. #include <linux/cpu.h>
  22. #include <linux/smp.h>
  23. #include <linux/string.h>
  24. #include <asm/amd_nb.h>
  25. #include <asm/apic.h>
  26. #include <asm/mce.h>
  27. #include <asm/msr.h>
  28. #include <asm/trace/irq_vectors.h>
  29. #include "mce-internal.h"
  30. #define NR_BLOCKS 5
  31. #define THRESHOLD_MAX 0xFFF
  32. #define INT_TYPE_APIC 0x00020000
  33. #define MASK_VALID_HI 0x80000000
  34. #define MASK_CNTP_HI 0x40000000
  35. #define MASK_LOCKED_HI 0x20000000
  36. #define MASK_LVTOFF_HI 0x00F00000
  37. #define MASK_COUNT_EN_HI 0x00080000
  38. #define MASK_INT_TYPE_HI 0x00060000
  39. #define MASK_OVERFLOW_HI 0x00010000
  40. #define MASK_ERR_COUNT_HI 0x00000FFF
  41. #define MASK_BLKPTR_LO 0xFF000000
  42. #define MCG_XBLK_ADDR 0xC0000400
  43. /* Deferred error settings */
  44. #define MSR_CU_DEF_ERR 0xC0000410
  45. #define MASK_DEF_LVTOFF 0x000000F0
  46. #define MASK_DEF_INT_TYPE 0x00000006
  47. #define DEF_LVT_OFF 0x2
  48. #define DEF_INT_TYPE_APIC 0x2
  49. /* Scalable MCA: */
  50. /* Threshold LVT offset is at MSR0xC0000410[15:12] */
  51. #define SMCA_THR_LVT_OFF 0xF000
  52. static bool thresholding_en;
  53. static const char * const th_names[] = {
  54. "load_store",
  55. "insn_fetch",
  56. "combined_unit",
  57. "decode_unit",
  58. "northbridge",
  59. "execution_unit",
  60. };
  61. static const char * const smca_umc_block_names[] = {
  62. "dram_ecc",
  63. "misc_umc"
  64. };
  65. struct smca_bank_name {
  66. const char *name; /* Short name for sysfs */
  67. const char *long_name; /* Long name for pretty-printing */
  68. };
  69. static struct smca_bank_name smca_names[] = {
  70. [SMCA_LS] = { "load_store", "Load Store Unit" },
  71. [SMCA_IF] = { "insn_fetch", "Instruction Fetch Unit" },
  72. [SMCA_L2_CACHE] = { "l2_cache", "L2 Cache" },
  73. [SMCA_DE] = { "decode_unit", "Decode Unit" },
  74. [SMCA_EX] = { "execution_unit", "Execution Unit" },
  75. [SMCA_FP] = { "floating_point", "Floating Point Unit" },
  76. [SMCA_L3_CACHE] = { "l3_cache", "L3 Cache" },
  77. [SMCA_CS] = { "coherent_slave", "Coherent Slave" },
  78. [SMCA_PIE] = { "pie", "Power, Interrupts, etc." },
  79. [SMCA_UMC] = { "umc", "Unified Memory Controller" },
  80. [SMCA_PB] = { "param_block", "Parameter Block" },
  81. [SMCA_PSP] = { "psp", "Platform Security Processor" },
  82. [SMCA_SMU] = { "smu", "System Management Unit" },
  83. };
  84. const char *smca_get_name(enum smca_bank_types t)
  85. {
  86. if (t >= N_SMCA_BANK_TYPES)
  87. return NULL;
  88. return smca_names[t].name;
  89. }
  90. const char *smca_get_long_name(enum smca_bank_types t)
  91. {
  92. if (t >= N_SMCA_BANK_TYPES)
  93. return NULL;
  94. return smca_names[t].long_name;
  95. }
  96. EXPORT_SYMBOL_GPL(smca_get_long_name);
  97. static enum smca_bank_types smca_get_bank_type(struct mce *m)
  98. {
  99. struct smca_bank *b;
  100. if (m->bank >= N_SMCA_BANK_TYPES)
  101. return N_SMCA_BANK_TYPES;
  102. b = &smca_banks[m->bank];
  103. if (!b->hwid)
  104. return N_SMCA_BANK_TYPES;
  105. return b->hwid->bank_type;
  106. }
  107. static struct smca_hwid smca_hwid_mcatypes[] = {
  108. /* { bank_type, hwid_mcatype, xec_bitmap } */
  109. /* ZN Core (HWID=0xB0) MCA types */
  110. { SMCA_LS, HWID_MCATYPE(0xB0, 0x0), 0x1FFFEF },
  111. { SMCA_IF, HWID_MCATYPE(0xB0, 0x1), 0x3FFF },
  112. { SMCA_L2_CACHE, HWID_MCATYPE(0xB0, 0x2), 0xF },
  113. { SMCA_DE, HWID_MCATYPE(0xB0, 0x3), 0x1FF },
  114. /* HWID 0xB0 MCATYPE 0x4 is Reserved */
  115. { SMCA_EX, HWID_MCATYPE(0xB0, 0x5), 0x7FF },
  116. { SMCA_FP, HWID_MCATYPE(0xB0, 0x6), 0x7F },
  117. { SMCA_L3_CACHE, HWID_MCATYPE(0xB0, 0x7), 0xFF },
  118. /* Data Fabric MCA types */
  119. { SMCA_CS, HWID_MCATYPE(0x2E, 0x0), 0x1FF },
  120. { SMCA_PIE, HWID_MCATYPE(0x2E, 0x1), 0xF },
  121. /* Unified Memory Controller MCA type */
  122. { SMCA_UMC, HWID_MCATYPE(0x96, 0x0), 0x3F },
  123. /* Parameter Block MCA type */
  124. { SMCA_PB, HWID_MCATYPE(0x05, 0x0), 0x1 },
  125. /* Platform Security Processor MCA type */
  126. { SMCA_PSP, HWID_MCATYPE(0xFF, 0x0), 0x1 },
  127. /* System Management Unit MCA type */
  128. { SMCA_SMU, HWID_MCATYPE(0x01, 0x0), 0x1 },
  129. };
  130. struct smca_bank smca_banks[MAX_NR_BANKS];
  131. EXPORT_SYMBOL_GPL(smca_banks);
  132. /*
  133. * In SMCA enabled processors, we can have multiple banks for a given IP type.
  134. * So to define a unique name for each bank, we use a temp c-string to append
  135. * the MCA_IPID[InstanceId] to type's name in get_name().
  136. *
  137. * InstanceId is 32 bits which is 8 characters. Make sure MAX_MCATYPE_NAME_LEN
  138. * is greater than 8 plus 1 (for underscore) plus length of longest type name.
  139. */
  140. #define MAX_MCATYPE_NAME_LEN 30
  141. static char buf_mcatype[MAX_MCATYPE_NAME_LEN];
  142. static DEFINE_PER_CPU(struct threshold_bank **, threshold_banks);
  143. static DEFINE_PER_CPU(unsigned int, bank_map); /* see which banks are on */
  144. static void amd_threshold_interrupt(void);
  145. static void amd_deferred_error_interrupt(void);
  146. static void default_deferred_error_interrupt(void)
  147. {
  148. pr_err("Unexpected deferred interrupt at vector %x\n", DEFERRED_ERROR_VECTOR);
  149. }
  150. void (*deferred_error_int_vector)(void) = default_deferred_error_interrupt;
  151. static void smca_configure(unsigned int bank, unsigned int cpu)
  152. {
  153. unsigned int i, hwid_mcatype;
  154. struct smca_hwid *s_hwid;
  155. u32 high, low;
  156. u32 smca_config = MSR_AMD64_SMCA_MCx_CONFIG(bank);
  157. /* Set appropriate bits in MCA_CONFIG */
  158. if (!rdmsr_safe(smca_config, &low, &high)) {
  159. /*
  160. * OS is required to set the MCAX bit to acknowledge that it is
  161. * now using the new MSR ranges and new registers under each
  162. * bank. It also means that the OS will configure deferred
  163. * errors in the new MCx_CONFIG register. If the bit is not set,
  164. * uncorrectable errors will cause a system panic.
  165. *
  166. * MCA_CONFIG[MCAX] is bit 32 (0 in the high portion of the MSR.)
  167. */
  168. high |= BIT(0);
  169. /*
  170. * SMCA sets the Deferred Error Interrupt type per bank.
  171. *
  172. * MCA_CONFIG[DeferredIntTypeSupported] is bit 5, and tells us
  173. * if the DeferredIntType bit field is available.
  174. *
  175. * MCA_CONFIG[DeferredIntType] is bits [38:37] ([6:5] in the
  176. * high portion of the MSR). OS should set this to 0x1 to enable
  177. * APIC based interrupt. First, check that no interrupt has been
  178. * set.
  179. */
  180. if ((low & BIT(5)) && !((high >> 5) & 0x3))
  181. high |= BIT(5);
  182. wrmsr(smca_config, low, high);
  183. }
  184. /* Return early if this bank was already initialized. */
  185. if (smca_banks[bank].hwid)
  186. return;
  187. if (rdmsr_safe_on_cpu(cpu, MSR_AMD64_SMCA_MCx_IPID(bank), &low, &high)) {
  188. pr_warn("Failed to read MCA_IPID for bank %d\n", bank);
  189. return;
  190. }
  191. hwid_mcatype = HWID_MCATYPE(high & MCI_IPID_HWID,
  192. (high & MCI_IPID_MCATYPE) >> 16);
  193. for (i = 0; i < ARRAY_SIZE(smca_hwid_mcatypes); i++) {
  194. s_hwid = &smca_hwid_mcatypes[i];
  195. if (hwid_mcatype == s_hwid->hwid_mcatype) {
  196. smca_banks[bank].hwid = s_hwid;
  197. smca_banks[bank].id = low;
  198. smca_banks[bank].sysfs_id = s_hwid->count++;
  199. break;
  200. }
  201. }
  202. }
  203. struct thresh_restart {
  204. struct threshold_block *b;
  205. int reset;
  206. int set_lvt_off;
  207. int lvt_off;
  208. u16 old_limit;
  209. };
  210. static inline bool is_shared_bank(int bank)
  211. {
  212. /*
  213. * Scalable MCA provides for only one core to have access to the MSRs of
  214. * a shared bank.
  215. */
  216. if (mce_flags.smca)
  217. return false;
  218. /* Bank 4 is for northbridge reporting and is thus shared */
  219. return (bank == 4);
  220. }
  221. static const char *bank4_names(const struct threshold_block *b)
  222. {
  223. switch (b->address) {
  224. /* MSR4_MISC0 */
  225. case 0x00000413:
  226. return "dram";
  227. case 0xc0000408:
  228. return "ht_links";
  229. case 0xc0000409:
  230. return "l3_cache";
  231. default:
  232. WARN(1, "Funny MSR: 0x%08x\n", b->address);
  233. return "";
  234. }
  235. };
  236. static bool lvt_interrupt_supported(unsigned int bank, u32 msr_high_bits)
  237. {
  238. /*
  239. * bank 4 supports APIC LVT interrupts implicitly since forever.
  240. */
  241. if (bank == 4)
  242. return true;
  243. /*
  244. * IntP: interrupt present; if this bit is set, the thresholding
  245. * bank can generate APIC LVT interrupts
  246. */
  247. return msr_high_bits & BIT(28);
  248. }
  249. static int lvt_off_valid(struct threshold_block *b, int apic, u32 lo, u32 hi)
  250. {
  251. int msr = (hi & MASK_LVTOFF_HI) >> 20;
  252. if (apic < 0) {
  253. pr_err(FW_BUG "cpu %d, failed to setup threshold interrupt "
  254. "for bank %d, block %d (MSR%08X=0x%x%08x)\n", b->cpu,
  255. b->bank, b->block, b->address, hi, lo);
  256. return 0;
  257. }
  258. if (apic != msr) {
  259. /*
  260. * On SMCA CPUs, LVT offset is programmed at a different MSR, and
  261. * the BIOS provides the value. The original field where LVT offset
  262. * was set is reserved. Return early here:
  263. */
  264. if (mce_flags.smca)
  265. return 0;
  266. pr_err(FW_BUG "cpu %d, invalid threshold interrupt offset %d "
  267. "for bank %d, block %d (MSR%08X=0x%x%08x)\n",
  268. b->cpu, apic, b->bank, b->block, b->address, hi, lo);
  269. return 0;
  270. }
  271. return 1;
  272. };
  273. /* Reprogram MCx_MISC MSR behind this threshold bank. */
  274. static void threshold_restart_bank(void *_tr)
  275. {
  276. struct thresh_restart *tr = _tr;
  277. u32 hi, lo;
  278. rdmsr(tr->b->address, lo, hi);
  279. if (tr->b->threshold_limit < (hi & THRESHOLD_MAX))
  280. tr->reset = 1; /* limit cannot be lower than err count */
  281. if (tr->reset) { /* reset err count and overflow bit */
  282. hi =
  283. (hi & ~(MASK_ERR_COUNT_HI | MASK_OVERFLOW_HI)) |
  284. (THRESHOLD_MAX - tr->b->threshold_limit);
  285. } else if (tr->old_limit) { /* change limit w/o reset */
  286. int new_count = (hi & THRESHOLD_MAX) +
  287. (tr->old_limit - tr->b->threshold_limit);
  288. hi = (hi & ~MASK_ERR_COUNT_HI) |
  289. (new_count & THRESHOLD_MAX);
  290. }
  291. /* clear IntType */
  292. hi &= ~MASK_INT_TYPE_HI;
  293. if (!tr->b->interrupt_capable)
  294. goto done;
  295. if (tr->set_lvt_off) {
  296. if (lvt_off_valid(tr->b, tr->lvt_off, lo, hi)) {
  297. /* set new lvt offset */
  298. hi &= ~MASK_LVTOFF_HI;
  299. hi |= tr->lvt_off << 20;
  300. }
  301. }
  302. if (tr->b->interrupt_enable)
  303. hi |= INT_TYPE_APIC;
  304. done:
  305. hi |= MASK_COUNT_EN_HI;
  306. wrmsr(tr->b->address, lo, hi);
  307. }
  308. static void mce_threshold_block_init(struct threshold_block *b, int offset)
  309. {
  310. struct thresh_restart tr = {
  311. .b = b,
  312. .set_lvt_off = 1,
  313. .lvt_off = offset,
  314. };
  315. b->threshold_limit = THRESHOLD_MAX;
  316. threshold_restart_bank(&tr);
  317. };
  318. static int setup_APIC_mce_threshold(int reserved, int new)
  319. {
  320. if (reserved < 0 && !setup_APIC_eilvt(new, THRESHOLD_APIC_VECTOR,
  321. APIC_EILVT_MSG_FIX, 0))
  322. return new;
  323. return reserved;
  324. }
  325. static int setup_APIC_deferred_error(int reserved, int new)
  326. {
  327. if (reserved < 0 && !setup_APIC_eilvt(new, DEFERRED_ERROR_VECTOR,
  328. APIC_EILVT_MSG_FIX, 0))
  329. return new;
  330. return reserved;
  331. }
  332. static void deferred_error_interrupt_enable(struct cpuinfo_x86 *c)
  333. {
  334. u32 low = 0, high = 0;
  335. int def_offset = -1, def_new;
  336. if (rdmsr_safe(MSR_CU_DEF_ERR, &low, &high))
  337. return;
  338. def_new = (low & MASK_DEF_LVTOFF) >> 4;
  339. if (!(low & MASK_DEF_LVTOFF)) {
  340. pr_err(FW_BUG "Your BIOS is not setting up LVT offset 0x2 for deferred error IRQs correctly.\n");
  341. def_new = DEF_LVT_OFF;
  342. low = (low & ~MASK_DEF_LVTOFF) | (DEF_LVT_OFF << 4);
  343. }
  344. def_offset = setup_APIC_deferred_error(def_offset, def_new);
  345. if ((def_offset == def_new) &&
  346. (deferred_error_int_vector != amd_deferred_error_interrupt))
  347. deferred_error_int_vector = amd_deferred_error_interrupt;
  348. if (!mce_flags.smca)
  349. low = (low & ~MASK_DEF_INT_TYPE) | DEF_INT_TYPE_APIC;
  350. wrmsr(MSR_CU_DEF_ERR, low, high);
  351. }
  352. static u32 get_block_address(unsigned int cpu, u32 current_addr, u32 low, u32 high,
  353. unsigned int bank, unsigned int block)
  354. {
  355. u32 addr = 0, offset = 0;
  356. if (mce_flags.smca) {
  357. if (!block) {
  358. addr = MSR_AMD64_SMCA_MCx_MISC(bank);
  359. } else {
  360. /*
  361. * For SMCA enabled processors, BLKPTR field of the
  362. * first MISC register (MCx_MISC0) indicates presence of
  363. * additional MISC register set (MISC1-4).
  364. */
  365. u32 low, high;
  366. if (rdmsr_safe_on_cpu(cpu, MSR_AMD64_SMCA_MCx_CONFIG(bank), &low, &high))
  367. return addr;
  368. if (!(low & MCI_CONFIG_MCAX))
  369. return addr;
  370. if (!rdmsr_safe_on_cpu(cpu, MSR_AMD64_SMCA_MCx_MISC(bank), &low, &high) &&
  371. (low & MASK_BLKPTR_LO))
  372. addr = MSR_AMD64_SMCA_MCx_MISCy(bank, block - 1);
  373. }
  374. return addr;
  375. }
  376. /* Fall back to method we used for older processors: */
  377. switch (block) {
  378. case 0:
  379. addr = msr_ops.misc(bank);
  380. break;
  381. case 1:
  382. offset = ((low & MASK_BLKPTR_LO) >> 21);
  383. if (offset)
  384. addr = MCG_XBLK_ADDR + offset;
  385. break;
  386. default:
  387. addr = ++current_addr;
  388. }
  389. return addr;
  390. }
  391. static int
  392. prepare_threshold_block(unsigned int bank, unsigned int block, u32 addr,
  393. int offset, u32 misc_high)
  394. {
  395. unsigned int cpu = smp_processor_id();
  396. u32 smca_low, smca_high;
  397. struct threshold_block b;
  398. int new;
  399. if (!block)
  400. per_cpu(bank_map, cpu) |= (1 << bank);
  401. memset(&b, 0, sizeof(b));
  402. b.cpu = cpu;
  403. b.bank = bank;
  404. b.block = block;
  405. b.address = addr;
  406. b.interrupt_capable = lvt_interrupt_supported(bank, misc_high);
  407. if (!b.interrupt_capable)
  408. goto done;
  409. b.interrupt_enable = 1;
  410. if (!mce_flags.smca) {
  411. new = (misc_high & MASK_LVTOFF_HI) >> 20;
  412. goto set_offset;
  413. }
  414. /* Gather LVT offset for thresholding: */
  415. if (rdmsr_safe(MSR_CU_DEF_ERR, &smca_low, &smca_high))
  416. goto out;
  417. new = (smca_low & SMCA_THR_LVT_OFF) >> 12;
  418. set_offset:
  419. offset = setup_APIC_mce_threshold(offset, new);
  420. if ((offset == new) && (mce_threshold_vector != amd_threshold_interrupt))
  421. mce_threshold_vector = amd_threshold_interrupt;
  422. done:
  423. mce_threshold_block_init(&b, offset);
  424. out:
  425. return offset;
  426. }
  427. /* cpu init entry point, called from mce.c with preempt off */
  428. void mce_amd_feature_init(struct cpuinfo_x86 *c)
  429. {
  430. u32 low = 0, high = 0, address = 0;
  431. unsigned int bank, block, cpu = smp_processor_id();
  432. int offset = -1;
  433. for (bank = 0; bank < mca_cfg.banks; ++bank) {
  434. if (mce_flags.smca)
  435. smca_configure(bank, cpu);
  436. for (block = 0; block < NR_BLOCKS; ++block) {
  437. address = get_block_address(cpu, address, low, high, bank, block);
  438. if (!address)
  439. break;
  440. if (rdmsr_safe(address, &low, &high))
  441. break;
  442. if (!(high & MASK_VALID_HI))
  443. continue;
  444. if (!(high & MASK_CNTP_HI) ||
  445. (high & MASK_LOCKED_HI))
  446. continue;
  447. offset = prepare_threshold_block(bank, block, address, offset, high);
  448. }
  449. }
  450. if (mce_flags.succor)
  451. deferred_error_interrupt_enable(c);
  452. }
  453. int umc_normaddr_to_sysaddr(u64 norm_addr, u16 nid, u8 umc, u64 *sys_addr)
  454. {
  455. u64 dram_base_addr, dram_limit_addr, dram_hole_base;
  456. /* We start from the normalized address */
  457. u64 ret_addr = norm_addr;
  458. u32 tmp;
  459. u8 die_id_shift, die_id_mask, socket_id_shift, socket_id_mask;
  460. u8 intlv_num_dies, intlv_num_chan, intlv_num_sockets;
  461. u8 intlv_addr_sel, intlv_addr_bit;
  462. u8 num_intlv_bits, hashed_bit;
  463. u8 lgcy_mmio_hole_en, base = 0;
  464. u8 cs_mask, cs_id = 0;
  465. bool hash_enabled = false;
  466. /* Read D18F0x1B4 (DramOffset), check if base 1 is used. */
  467. if (amd_df_indirect_read(nid, 0, 0x1B4, umc, &tmp))
  468. goto out_err;
  469. /* Remove HiAddrOffset from normalized address, if enabled: */
  470. if (tmp & BIT(0)) {
  471. u64 hi_addr_offset = (tmp & GENMASK_ULL(31, 20)) << 8;
  472. if (norm_addr >= hi_addr_offset) {
  473. ret_addr -= hi_addr_offset;
  474. base = 1;
  475. }
  476. }
  477. /* Read D18F0x110 (DramBaseAddress). */
  478. if (amd_df_indirect_read(nid, 0, 0x110 + (8 * base), umc, &tmp))
  479. goto out_err;
  480. /* Check if address range is valid. */
  481. if (!(tmp & BIT(0))) {
  482. pr_err("%s: Invalid DramBaseAddress range: 0x%x.\n",
  483. __func__, tmp);
  484. goto out_err;
  485. }
  486. lgcy_mmio_hole_en = tmp & BIT(1);
  487. intlv_num_chan = (tmp >> 4) & 0xF;
  488. intlv_addr_sel = (tmp >> 8) & 0x7;
  489. dram_base_addr = (tmp & GENMASK_ULL(31, 12)) << 16;
  490. /* {0, 1, 2, 3} map to address bits {8, 9, 10, 11} respectively */
  491. if (intlv_addr_sel > 3) {
  492. pr_err("%s: Invalid interleave address select %d.\n",
  493. __func__, intlv_addr_sel);
  494. goto out_err;
  495. }
  496. /* Read D18F0x114 (DramLimitAddress). */
  497. if (amd_df_indirect_read(nid, 0, 0x114 + (8 * base), umc, &tmp))
  498. goto out_err;
  499. intlv_num_sockets = (tmp >> 8) & 0x1;
  500. intlv_num_dies = (tmp >> 10) & 0x3;
  501. dram_limit_addr = ((tmp & GENMASK_ULL(31, 12)) << 16) | GENMASK_ULL(27, 0);
  502. intlv_addr_bit = intlv_addr_sel + 8;
  503. /* Re-use intlv_num_chan by setting it equal to log2(#channels) */
  504. switch (intlv_num_chan) {
  505. case 0: intlv_num_chan = 0; break;
  506. case 1: intlv_num_chan = 1; break;
  507. case 3: intlv_num_chan = 2; break;
  508. case 5: intlv_num_chan = 3; break;
  509. case 7: intlv_num_chan = 4; break;
  510. case 8: intlv_num_chan = 1;
  511. hash_enabled = true;
  512. break;
  513. default:
  514. pr_err("%s: Invalid number of interleaved channels %d.\n",
  515. __func__, intlv_num_chan);
  516. goto out_err;
  517. }
  518. num_intlv_bits = intlv_num_chan;
  519. if (intlv_num_dies > 2) {
  520. pr_err("%s: Invalid number of interleaved nodes/dies %d.\n",
  521. __func__, intlv_num_dies);
  522. goto out_err;
  523. }
  524. num_intlv_bits += intlv_num_dies;
  525. /* Add a bit if sockets are interleaved. */
  526. num_intlv_bits += intlv_num_sockets;
  527. /* Assert num_intlv_bits <= 4 */
  528. if (num_intlv_bits > 4) {
  529. pr_err("%s: Invalid interleave bits %d.\n",
  530. __func__, num_intlv_bits);
  531. goto out_err;
  532. }
  533. if (num_intlv_bits > 0) {
  534. u64 temp_addr_x, temp_addr_i, temp_addr_y;
  535. u8 die_id_bit, sock_id_bit, cs_fabric_id;
  536. /*
  537. * Read FabricBlockInstanceInformation3_CS[BlockFabricID].
  538. * This is the fabric id for this coherent slave. Use
  539. * umc/channel# as instance id of the coherent slave
  540. * for FICAA.
  541. */
  542. if (amd_df_indirect_read(nid, 0, 0x50, umc, &tmp))
  543. goto out_err;
  544. cs_fabric_id = (tmp >> 8) & 0xFF;
  545. die_id_bit = 0;
  546. /* If interleaved over more than 1 channel: */
  547. if (intlv_num_chan) {
  548. die_id_bit = intlv_num_chan;
  549. cs_mask = (1 << die_id_bit) - 1;
  550. cs_id = cs_fabric_id & cs_mask;
  551. }
  552. sock_id_bit = die_id_bit;
  553. /* Read D18F1x208 (SystemFabricIdMask). */
  554. if (intlv_num_dies || intlv_num_sockets)
  555. if (amd_df_indirect_read(nid, 1, 0x208, umc, &tmp))
  556. goto out_err;
  557. /* If interleaved over more than 1 die. */
  558. if (intlv_num_dies) {
  559. sock_id_bit = die_id_bit + intlv_num_dies;
  560. die_id_shift = (tmp >> 24) & 0xF;
  561. die_id_mask = (tmp >> 8) & 0xFF;
  562. cs_id |= ((cs_fabric_id & die_id_mask) >> die_id_shift) << die_id_bit;
  563. }
  564. /* If interleaved over more than 1 socket. */
  565. if (intlv_num_sockets) {
  566. socket_id_shift = (tmp >> 28) & 0xF;
  567. socket_id_mask = (tmp >> 16) & 0xFF;
  568. cs_id |= ((cs_fabric_id & socket_id_mask) >> socket_id_shift) << sock_id_bit;
  569. }
  570. /*
  571. * The pre-interleaved address consists of XXXXXXIIIYYYYY
  572. * where III is the ID for this CS, and XXXXXXYYYYY are the
  573. * address bits from the post-interleaved address.
  574. * "num_intlv_bits" has been calculated to tell us how many "I"
  575. * bits there are. "intlv_addr_bit" tells us how many "Y" bits
  576. * there are (where "I" starts).
  577. */
  578. temp_addr_y = ret_addr & GENMASK_ULL(intlv_addr_bit-1, 0);
  579. temp_addr_i = (cs_id << intlv_addr_bit);
  580. temp_addr_x = (ret_addr & GENMASK_ULL(63, intlv_addr_bit)) << num_intlv_bits;
  581. ret_addr = temp_addr_x | temp_addr_i | temp_addr_y;
  582. }
  583. /* Add dram base address */
  584. ret_addr += dram_base_addr;
  585. /* If legacy MMIO hole enabled */
  586. if (lgcy_mmio_hole_en) {
  587. if (amd_df_indirect_read(nid, 0, 0x104, umc, &tmp))
  588. goto out_err;
  589. dram_hole_base = tmp & GENMASK(31, 24);
  590. if (ret_addr >= dram_hole_base)
  591. ret_addr += (BIT_ULL(32) - dram_hole_base);
  592. }
  593. if (hash_enabled) {
  594. /* Save some parentheses and grab ls-bit at the end. */
  595. hashed_bit = (ret_addr >> 12) ^
  596. (ret_addr >> 18) ^
  597. (ret_addr >> 21) ^
  598. (ret_addr >> 30) ^
  599. cs_id;
  600. hashed_bit &= BIT(0);
  601. if (hashed_bit != ((ret_addr >> intlv_addr_bit) & BIT(0)))
  602. ret_addr ^= BIT(intlv_addr_bit);
  603. }
  604. /* Is calculated system address is above DRAM limit address? */
  605. if (ret_addr > dram_limit_addr)
  606. goto out_err;
  607. *sys_addr = ret_addr;
  608. return 0;
  609. out_err:
  610. return -EINVAL;
  611. }
  612. EXPORT_SYMBOL_GPL(umc_normaddr_to_sysaddr);
  613. bool amd_mce_is_memory_error(struct mce *m)
  614. {
  615. /* ErrCodeExt[20:16] */
  616. u8 xec = (m->status >> 16) & 0x1f;
  617. if (mce_flags.smca)
  618. return smca_get_bank_type(m) == SMCA_UMC && xec == 0x0;
  619. return m->bank == 4 && xec == 0x8;
  620. }
  621. static void __log_error(unsigned int bank, u64 status, u64 addr, u64 misc)
  622. {
  623. struct mce m;
  624. mce_setup(&m);
  625. m.status = status;
  626. m.misc = misc;
  627. m.bank = bank;
  628. m.tsc = rdtsc();
  629. if (m.status & MCI_STATUS_ADDRV) {
  630. m.addr = addr;
  631. /*
  632. * Extract [55:<lsb>] where lsb is the least significant
  633. * *valid* bit of the address bits.
  634. */
  635. if (mce_flags.smca) {
  636. u8 lsb = (m.addr >> 56) & 0x3f;
  637. m.addr &= GENMASK_ULL(55, lsb);
  638. }
  639. }
  640. if (mce_flags.smca) {
  641. rdmsrl(MSR_AMD64_SMCA_MCx_IPID(bank), m.ipid);
  642. if (m.status & MCI_STATUS_SYNDV)
  643. rdmsrl(MSR_AMD64_SMCA_MCx_SYND(bank), m.synd);
  644. }
  645. mce_log(&m);
  646. }
  647. asmlinkage __visible void __irq_entry smp_deferred_error_interrupt(void)
  648. {
  649. entering_irq();
  650. trace_deferred_error_apic_entry(DEFERRED_ERROR_VECTOR);
  651. inc_irq_stat(irq_deferred_error_count);
  652. deferred_error_int_vector();
  653. trace_deferred_error_apic_exit(DEFERRED_ERROR_VECTOR);
  654. exiting_ack_irq();
  655. }
  656. /*
  657. * Returns true if the logged error is deferred. False, otherwise.
  658. */
  659. static inline bool
  660. _log_error_bank(unsigned int bank, u32 msr_stat, u32 msr_addr, u64 misc)
  661. {
  662. u64 status, addr = 0;
  663. rdmsrl(msr_stat, status);
  664. if (!(status & MCI_STATUS_VAL))
  665. return false;
  666. if (status & MCI_STATUS_ADDRV)
  667. rdmsrl(msr_addr, addr);
  668. __log_error(bank, status, addr, misc);
  669. wrmsrl(msr_stat, 0);
  670. return status & MCI_STATUS_DEFERRED;
  671. }
  672. /*
  673. * We have three scenarios for checking for Deferred errors:
  674. *
  675. * 1) Non-SMCA systems check MCA_STATUS and log error if found.
  676. * 2) SMCA systems check MCA_STATUS. If error is found then log it and also
  677. * clear MCA_DESTAT.
  678. * 3) SMCA systems check MCA_DESTAT, if error was not found in MCA_STATUS, and
  679. * log it.
  680. */
  681. static void log_error_deferred(unsigned int bank)
  682. {
  683. bool defrd;
  684. defrd = _log_error_bank(bank, msr_ops.status(bank),
  685. msr_ops.addr(bank), 0);
  686. if (!mce_flags.smca)
  687. return;
  688. /* Clear MCA_DESTAT if we logged the deferred error from MCA_STATUS. */
  689. if (defrd) {
  690. wrmsrl(MSR_AMD64_SMCA_MCx_DESTAT(bank), 0);
  691. return;
  692. }
  693. /*
  694. * Only deferred errors are logged in MCA_DE{STAT,ADDR} so just check
  695. * for a valid error.
  696. */
  697. _log_error_bank(bank, MSR_AMD64_SMCA_MCx_DESTAT(bank),
  698. MSR_AMD64_SMCA_MCx_DEADDR(bank), 0);
  699. }
  700. /* APIC interrupt handler for deferred errors */
  701. static void amd_deferred_error_interrupt(void)
  702. {
  703. unsigned int bank;
  704. for (bank = 0; bank < mca_cfg.banks; ++bank)
  705. log_error_deferred(bank);
  706. }
  707. static void log_error_thresholding(unsigned int bank, u64 misc)
  708. {
  709. _log_error_bank(bank, msr_ops.status(bank), msr_ops.addr(bank), misc);
  710. }
  711. static void log_and_reset_block(struct threshold_block *block)
  712. {
  713. struct thresh_restart tr;
  714. u32 low = 0, high = 0;
  715. if (!block)
  716. return;
  717. if (rdmsr_safe(block->address, &low, &high))
  718. return;
  719. if (!(high & MASK_OVERFLOW_HI))
  720. return;
  721. /* Log the MCE which caused the threshold event. */
  722. log_error_thresholding(block->bank, ((u64)high << 32) | low);
  723. /* Reset threshold block after logging error. */
  724. memset(&tr, 0, sizeof(tr));
  725. tr.b = block;
  726. threshold_restart_bank(&tr);
  727. }
  728. /*
  729. * Threshold interrupt handler will service THRESHOLD_APIC_VECTOR. The interrupt
  730. * goes off when error_count reaches threshold_limit.
  731. */
  732. static void amd_threshold_interrupt(void)
  733. {
  734. struct threshold_block *first_block = NULL, *block = NULL, *tmp = NULL;
  735. unsigned int bank, cpu = smp_processor_id();
  736. for (bank = 0; bank < mca_cfg.banks; ++bank) {
  737. if (!(per_cpu(bank_map, cpu) & (1 << bank)))
  738. continue;
  739. first_block = per_cpu(threshold_banks, cpu)[bank]->blocks;
  740. if (!first_block)
  741. continue;
  742. /*
  743. * The first block is also the head of the list. Check it first
  744. * before iterating over the rest.
  745. */
  746. log_and_reset_block(first_block);
  747. list_for_each_entry_safe(block, tmp, &first_block->miscj, miscj)
  748. log_and_reset_block(block);
  749. }
  750. }
  751. /*
  752. * Sysfs Interface
  753. */
  754. struct threshold_attr {
  755. struct attribute attr;
  756. ssize_t (*show) (struct threshold_block *, char *);
  757. ssize_t (*store) (struct threshold_block *, const char *, size_t count);
  758. };
  759. #define SHOW_FIELDS(name) \
  760. static ssize_t show_ ## name(struct threshold_block *b, char *buf) \
  761. { \
  762. return sprintf(buf, "%lu\n", (unsigned long) b->name); \
  763. }
  764. SHOW_FIELDS(interrupt_enable)
  765. SHOW_FIELDS(threshold_limit)
  766. static ssize_t
  767. store_interrupt_enable(struct threshold_block *b, const char *buf, size_t size)
  768. {
  769. struct thresh_restart tr;
  770. unsigned long new;
  771. if (!b->interrupt_capable)
  772. return -EINVAL;
  773. if (kstrtoul(buf, 0, &new) < 0)
  774. return -EINVAL;
  775. b->interrupt_enable = !!new;
  776. memset(&tr, 0, sizeof(tr));
  777. tr.b = b;
  778. smp_call_function_single(b->cpu, threshold_restart_bank, &tr, 1);
  779. return size;
  780. }
  781. static ssize_t
  782. store_threshold_limit(struct threshold_block *b, const char *buf, size_t size)
  783. {
  784. struct thresh_restart tr;
  785. unsigned long new;
  786. if (kstrtoul(buf, 0, &new) < 0)
  787. return -EINVAL;
  788. if (new > THRESHOLD_MAX)
  789. new = THRESHOLD_MAX;
  790. if (new < 1)
  791. new = 1;
  792. memset(&tr, 0, sizeof(tr));
  793. tr.old_limit = b->threshold_limit;
  794. b->threshold_limit = new;
  795. tr.b = b;
  796. smp_call_function_single(b->cpu, threshold_restart_bank, &tr, 1);
  797. return size;
  798. }
  799. static ssize_t show_error_count(struct threshold_block *b, char *buf)
  800. {
  801. u32 lo, hi;
  802. rdmsr_on_cpu(b->cpu, b->address, &lo, &hi);
  803. return sprintf(buf, "%u\n", ((hi & THRESHOLD_MAX) -
  804. (THRESHOLD_MAX - b->threshold_limit)));
  805. }
  806. static struct threshold_attr error_count = {
  807. .attr = {.name = __stringify(error_count), .mode = 0444 },
  808. .show = show_error_count,
  809. };
  810. #define RW_ATTR(val) \
  811. static struct threshold_attr val = { \
  812. .attr = {.name = __stringify(val), .mode = 0644 }, \
  813. .show = show_## val, \
  814. .store = store_## val, \
  815. };
  816. RW_ATTR(interrupt_enable);
  817. RW_ATTR(threshold_limit);
  818. static struct attribute *default_attrs[] = {
  819. &threshold_limit.attr,
  820. &error_count.attr,
  821. NULL, /* possibly interrupt_enable if supported, see below */
  822. NULL,
  823. };
  824. #define to_block(k) container_of(k, struct threshold_block, kobj)
  825. #define to_attr(a) container_of(a, struct threshold_attr, attr)
  826. static ssize_t show(struct kobject *kobj, struct attribute *attr, char *buf)
  827. {
  828. struct threshold_block *b = to_block(kobj);
  829. struct threshold_attr *a = to_attr(attr);
  830. ssize_t ret;
  831. ret = a->show ? a->show(b, buf) : -EIO;
  832. return ret;
  833. }
  834. static ssize_t store(struct kobject *kobj, struct attribute *attr,
  835. const char *buf, size_t count)
  836. {
  837. struct threshold_block *b = to_block(kobj);
  838. struct threshold_attr *a = to_attr(attr);
  839. ssize_t ret;
  840. ret = a->store ? a->store(b, buf, count) : -EIO;
  841. return ret;
  842. }
  843. static const struct sysfs_ops threshold_ops = {
  844. .show = show,
  845. .store = store,
  846. };
  847. static struct kobj_type threshold_ktype = {
  848. .sysfs_ops = &threshold_ops,
  849. .default_attrs = default_attrs,
  850. };
  851. static const char *get_name(unsigned int bank, struct threshold_block *b)
  852. {
  853. unsigned int bank_type;
  854. if (!mce_flags.smca) {
  855. if (b && bank == 4)
  856. return bank4_names(b);
  857. return th_names[bank];
  858. }
  859. if (!smca_banks[bank].hwid)
  860. return NULL;
  861. bank_type = smca_banks[bank].hwid->bank_type;
  862. if (b && bank_type == SMCA_UMC) {
  863. if (b->block < ARRAY_SIZE(smca_umc_block_names))
  864. return smca_umc_block_names[b->block];
  865. return NULL;
  866. }
  867. if (smca_banks[bank].hwid->count == 1)
  868. return smca_get_name(bank_type);
  869. snprintf(buf_mcatype, MAX_MCATYPE_NAME_LEN,
  870. "%s_%x", smca_get_name(bank_type),
  871. smca_banks[bank].sysfs_id);
  872. return buf_mcatype;
  873. }
  874. static int allocate_threshold_blocks(unsigned int cpu, unsigned int bank,
  875. unsigned int block, u32 address)
  876. {
  877. struct threshold_block *b = NULL;
  878. u32 low, high;
  879. int err;
  880. if ((bank >= mca_cfg.banks) || (block >= NR_BLOCKS))
  881. return 0;
  882. if (rdmsr_safe_on_cpu(cpu, address, &low, &high))
  883. return 0;
  884. if (!(high & MASK_VALID_HI)) {
  885. if (block)
  886. goto recurse;
  887. else
  888. return 0;
  889. }
  890. if (!(high & MASK_CNTP_HI) ||
  891. (high & MASK_LOCKED_HI))
  892. goto recurse;
  893. b = kzalloc(sizeof(struct threshold_block), GFP_KERNEL);
  894. if (!b)
  895. return -ENOMEM;
  896. b->block = block;
  897. b->bank = bank;
  898. b->cpu = cpu;
  899. b->address = address;
  900. b->interrupt_enable = 0;
  901. b->interrupt_capable = lvt_interrupt_supported(bank, high);
  902. b->threshold_limit = THRESHOLD_MAX;
  903. if (b->interrupt_capable) {
  904. threshold_ktype.default_attrs[2] = &interrupt_enable.attr;
  905. b->interrupt_enable = 1;
  906. } else {
  907. threshold_ktype.default_attrs[2] = NULL;
  908. }
  909. INIT_LIST_HEAD(&b->miscj);
  910. if (per_cpu(threshold_banks, cpu)[bank]->blocks) {
  911. list_add(&b->miscj,
  912. &per_cpu(threshold_banks, cpu)[bank]->blocks->miscj);
  913. } else {
  914. per_cpu(threshold_banks, cpu)[bank]->blocks = b;
  915. }
  916. err = kobject_init_and_add(&b->kobj, &threshold_ktype,
  917. per_cpu(threshold_banks, cpu)[bank]->kobj,
  918. get_name(bank, b));
  919. if (err)
  920. goto out_free;
  921. recurse:
  922. address = get_block_address(cpu, address, low, high, bank, ++block);
  923. if (!address)
  924. return 0;
  925. err = allocate_threshold_blocks(cpu, bank, block, address);
  926. if (err)
  927. goto out_free;
  928. if (b)
  929. kobject_uevent(&b->kobj, KOBJ_ADD);
  930. return err;
  931. out_free:
  932. if (b) {
  933. kobject_put(&b->kobj);
  934. list_del(&b->miscj);
  935. kfree(b);
  936. }
  937. return err;
  938. }
  939. static int __threshold_add_blocks(struct threshold_bank *b)
  940. {
  941. struct list_head *head = &b->blocks->miscj;
  942. struct threshold_block *pos = NULL;
  943. struct threshold_block *tmp = NULL;
  944. int err = 0;
  945. err = kobject_add(&b->blocks->kobj, b->kobj, b->blocks->kobj.name);
  946. if (err)
  947. return err;
  948. list_for_each_entry_safe(pos, tmp, head, miscj) {
  949. err = kobject_add(&pos->kobj, b->kobj, pos->kobj.name);
  950. if (err) {
  951. list_for_each_entry_safe_reverse(pos, tmp, head, miscj)
  952. kobject_del(&pos->kobj);
  953. return err;
  954. }
  955. }
  956. return err;
  957. }
  958. static int threshold_create_bank(unsigned int cpu, unsigned int bank)
  959. {
  960. struct device *dev = per_cpu(mce_device, cpu);
  961. struct amd_northbridge *nb = NULL;
  962. struct threshold_bank *b = NULL;
  963. const char *name = get_name(bank, NULL);
  964. int err = 0;
  965. if (!dev)
  966. return -ENODEV;
  967. if (is_shared_bank(bank)) {
  968. nb = node_to_amd_nb(amd_get_nb_id(cpu));
  969. /* threshold descriptor already initialized on this node? */
  970. if (nb && nb->bank4) {
  971. /* yes, use it */
  972. b = nb->bank4;
  973. err = kobject_add(b->kobj, &dev->kobj, name);
  974. if (err)
  975. goto out;
  976. per_cpu(threshold_banks, cpu)[bank] = b;
  977. refcount_inc(&b->cpus);
  978. err = __threshold_add_blocks(b);
  979. goto out;
  980. }
  981. }
  982. b = kzalloc(sizeof(struct threshold_bank), GFP_KERNEL);
  983. if (!b) {
  984. err = -ENOMEM;
  985. goto out;
  986. }
  987. b->kobj = kobject_create_and_add(name, &dev->kobj);
  988. if (!b->kobj) {
  989. err = -EINVAL;
  990. goto out_free;
  991. }
  992. per_cpu(threshold_banks, cpu)[bank] = b;
  993. if (is_shared_bank(bank)) {
  994. refcount_set(&b->cpus, 1);
  995. /* nb is already initialized, see above */
  996. if (nb) {
  997. WARN_ON(nb->bank4);
  998. nb->bank4 = b;
  999. }
  1000. }
  1001. err = allocate_threshold_blocks(cpu, bank, 0, msr_ops.misc(bank));
  1002. if (!err)
  1003. goto out;
  1004. out_free:
  1005. kfree(b);
  1006. out:
  1007. return err;
  1008. }
  1009. static void deallocate_threshold_block(unsigned int cpu,
  1010. unsigned int bank)
  1011. {
  1012. struct threshold_block *pos = NULL;
  1013. struct threshold_block *tmp = NULL;
  1014. struct threshold_bank *head = per_cpu(threshold_banks, cpu)[bank];
  1015. if (!head)
  1016. return;
  1017. list_for_each_entry_safe(pos, tmp, &head->blocks->miscj, miscj) {
  1018. kobject_put(&pos->kobj);
  1019. list_del(&pos->miscj);
  1020. kfree(pos);
  1021. }
  1022. kfree(per_cpu(threshold_banks, cpu)[bank]->blocks);
  1023. per_cpu(threshold_banks, cpu)[bank]->blocks = NULL;
  1024. }
  1025. static void __threshold_remove_blocks(struct threshold_bank *b)
  1026. {
  1027. struct threshold_block *pos = NULL;
  1028. struct threshold_block *tmp = NULL;
  1029. kobject_del(b->kobj);
  1030. list_for_each_entry_safe(pos, tmp, &b->blocks->miscj, miscj)
  1031. kobject_del(&pos->kobj);
  1032. }
  1033. static void threshold_remove_bank(unsigned int cpu, int bank)
  1034. {
  1035. struct amd_northbridge *nb;
  1036. struct threshold_bank *b;
  1037. b = per_cpu(threshold_banks, cpu)[bank];
  1038. if (!b)
  1039. return;
  1040. if (!b->blocks)
  1041. goto free_out;
  1042. if (is_shared_bank(bank)) {
  1043. if (!refcount_dec_and_test(&b->cpus)) {
  1044. __threshold_remove_blocks(b);
  1045. per_cpu(threshold_banks, cpu)[bank] = NULL;
  1046. return;
  1047. } else {
  1048. /*
  1049. * the last CPU on this node using the shared bank is
  1050. * going away, remove that bank now.
  1051. */
  1052. nb = node_to_amd_nb(amd_get_nb_id(cpu));
  1053. nb->bank4 = NULL;
  1054. }
  1055. }
  1056. deallocate_threshold_block(cpu, bank);
  1057. free_out:
  1058. kobject_del(b->kobj);
  1059. kobject_put(b->kobj);
  1060. kfree(b);
  1061. per_cpu(threshold_banks, cpu)[bank] = NULL;
  1062. }
  1063. int mce_threshold_remove_device(unsigned int cpu)
  1064. {
  1065. unsigned int bank;
  1066. if (!thresholding_en)
  1067. return 0;
  1068. for (bank = 0; bank < mca_cfg.banks; ++bank) {
  1069. if (!(per_cpu(bank_map, cpu) & (1 << bank)))
  1070. continue;
  1071. threshold_remove_bank(cpu, bank);
  1072. }
  1073. kfree(per_cpu(threshold_banks, cpu));
  1074. per_cpu(threshold_banks, cpu) = NULL;
  1075. return 0;
  1076. }
  1077. /* create dir/files for all valid threshold banks */
  1078. int mce_threshold_create_device(unsigned int cpu)
  1079. {
  1080. unsigned int bank;
  1081. struct threshold_bank **bp;
  1082. int err = 0;
  1083. if (!thresholding_en)
  1084. return 0;
  1085. bp = per_cpu(threshold_banks, cpu);
  1086. if (bp)
  1087. return 0;
  1088. bp = kzalloc(sizeof(struct threshold_bank *) * mca_cfg.banks,
  1089. GFP_KERNEL);
  1090. if (!bp)
  1091. return -ENOMEM;
  1092. per_cpu(threshold_banks, cpu) = bp;
  1093. for (bank = 0; bank < mca_cfg.banks; ++bank) {
  1094. if (!(per_cpu(bank_map, cpu) & (1 << bank)))
  1095. continue;
  1096. err = threshold_create_bank(cpu, bank);
  1097. if (err)
  1098. goto err;
  1099. }
  1100. return err;
  1101. err:
  1102. mce_threshold_remove_device(cpu);
  1103. return err;
  1104. }
  1105. static __init int threshold_init_device(void)
  1106. {
  1107. unsigned lcpu = 0;
  1108. if (mce_threshold_vector == amd_threshold_interrupt)
  1109. thresholding_en = true;
  1110. /* to hit CPUs online before the notifier is up */
  1111. for_each_online_cpu(lcpu) {
  1112. int err = mce_threshold_create_device(lcpu);
  1113. if (err)
  1114. return err;
  1115. }
  1116. return 0;
  1117. }
  1118. /*
  1119. * there are 3 funcs which need to be _initcalled in a logic sequence:
  1120. * 1. xen_late_init_mcelog
  1121. * 2. mcheck_init_device
  1122. * 3. threshold_init_device
  1123. *
  1124. * xen_late_init_mcelog must register xen_mce_chrdev_device before
  1125. * native mce_chrdev_device registration if running under xen platform;
  1126. *
  1127. * mcheck_init_device should be inited before threshold_init_device to
  1128. * initialize mce_device, otherwise a NULL ptr dereference will cause panic.
  1129. *
  1130. * so we use following _initcalls
  1131. * 1. device_initcall(xen_late_init_mcelog);
  1132. * 2. device_initcall_sync(mcheck_init_device);
  1133. * 3. late_initcall(threshold_init_device);
  1134. *
  1135. * when running under xen, the initcall order is 1,2,3;
  1136. * on baremetal, we skip 1 and we do only 2 and 3.
  1137. */
  1138. late_initcall(threshold_init_device);