mce_amd.c 33 KB

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