mce_amd.c 35 KB

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