mce_amd.c 33 KB

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