mce_amd.c 34 KB

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