mce_amd.c 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954
  1. /*
  2. * (c) 2005-2015 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 <asm/amd_nb.h>
  24. #include <asm/apic.h>
  25. #include <asm/idle.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. /*
  52. * OS is required to set the MCAX bit to acknowledge that it is now using the
  53. * new MSR ranges and new registers under each bank. It also means that the OS
  54. * will configure deferred errors in the new MCx_CONFIG register. If the bit is
  55. * not set, uncorrectable errors will cause a system panic.
  56. */
  57. #define SMCA_MCAX_EN_OFF 0x1
  58. static const char * const th_names[] = {
  59. "load_store",
  60. "insn_fetch",
  61. "combined_unit",
  62. "",
  63. "northbridge",
  64. "execution_unit",
  65. };
  66. static DEFINE_PER_CPU(struct threshold_bank **, threshold_banks);
  67. static DEFINE_PER_CPU(unsigned char, bank_map); /* see which banks are on */
  68. static void amd_threshold_interrupt(void);
  69. static void amd_deferred_error_interrupt(void);
  70. static void default_deferred_error_interrupt(void)
  71. {
  72. pr_err("Unexpected deferred interrupt at vector %x\n", DEFERRED_ERROR_VECTOR);
  73. }
  74. void (*deferred_error_int_vector)(void) = default_deferred_error_interrupt;
  75. /*
  76. * CPU Initialization
  77. */
  78. struct thresh_restart {
  79. struct threshold_block *b;
  80. int reset;
  81. int set_lvt_off;
  82. int lvt_off;
  83. u16 old_limit;
  84. };
  85. static inline bool is_shared_bank(int bank)
  86. {
  87. /*
  88. * Scalable MCA provides for only one core to have access to the MSRs of
  89. * a shared bank.
  90. */
  91. if (mce_flags.smca)
  92. return false;
  93. /* Bank 4 is for northbridge reporting and is thus shared */
  94. return (bank == 4);
  95. }
  96. static const char *bank4_names(const struct threshold_block *b)
  97. {
  98. switch (b->address) {
  99. /* MSR4_MISC0 */
  100. case 0x00000413:
  101. return "dram";
  102. case 0xc0000408:
  103. return "ht_links";
  104. case 0xc0000409:
  105. return "l3_cache";
  106. default:
  107. WARN(1, "Funny MSR: 0x%08x\n", b->address);
  108. return "";
  109. }
  110. };
  111. static bool lvt_interrupt_supported(unsigned int bank, u32 msr_high_bits)
  112. {
  113. /*
  114. * bank 4 supports APIC LVT interrupts implicitly since forever.
  115. */
  116. if (bank == 4)
  117. return true;
  118. /*
  119. * IntP: interrupt present; if this bit is set, the thresholding
  120. * bank can generate APIC LVT interrupts
  121. */
  122. return msr_high_bits & BIT(28);
  123. }
  124. static int lvt_off_valid(struct threshold_block *b, int apic, u32 lo, u32 hi)
  125. {
  126. int msr = (hi & MASK_LVTOFF_HI) >> 20;
  127. if (apic < 0) {
  128. pr_err(FW_BUG "cpu %d, failed to setup threshold interrupt "
  129. "for bank %d, block %d (MSR%08X=0x%x%08x)\n", b->cpu,
  130. b->bank, b->block, b->address, hi, lo);
  131. return 0;
  132. }
  133. if (apic != msr) {
  134. /*
  135. * On SMCA CPUs, LVT offset is programmed at a different MSR, and
  136. * the BIOS provides the value. The original field where LVT offset
  137. * was set is reserved. Return early here:
  138. */
  139. if (mce_flags.smca)
  140. return 0;
  141. pr_err(FW_BUG "cpu %d, invalid threshold interrupt offset %d "
  142. "for bank %d, block %d (MSR%08X=0x%x%08x)\n",
  143. b->cpu, apic, b->bank, b->block, b->address, hi, lo);
  144. return 0;
  145. }
  146. return 1;
  147. };
  148. /*
  149. * Called via smp_call_function_single(), must be called with correct
  150. * cpu affinity.
  151. */
  152. static void threshold_restart_bank(void *_tr)
  153. {
  154. struct thresh_restart *tr = _tr;
  155. u32 hi, lo;
  156. rdmsr(tr->b->address, lo, hi);
  157. if (tr->b->threshold_limit < (hi & THRESHOLD_MAX))
  158. tr->reset = 1; /* limit cannot be lower than err count */
  159. if (tr->reset) { /* reset err count and overflow bit */
  160. hi =
  161. (hi & ~(MASK_ERR_COUNT_HI | MASK_OVERFLOW_HI)) |
  162. (THRESHOLD_MAX - tr->b->threshold_limit);
  163. } else if (tr->old_limit) { /* change limit w/o reset */
  164. int new_count = (hi & THRESHOLD_MAX) +
  165. (tr->old_limit - tr->b->threshold_limit);
  166. hi = (hi & ~MASK_ERR_COUNT_HI) |
  167. (new_count & THRESHOLD_MAX);
  168. }
  169. /* clear IntType */
  170. hi &= ~MASK_INT_TYPE_HI;
  171. if (!tr->b->interrupt_capable)
  172. goto done;
  173. if (tr->set_lvt_off) {
  174. if (lvt_off_valid(tr->b, tr->lvt_off, lo, hi)) {
  175. /* set new lvt offset */
  176. hi &= ~MASK_LVTOFF_HI;
  177. hi |= tr->lvt_off << 20;
  178. }
  179. }
  180. if (tr->b->interrupt_enable)
  181. hi |= INT_TYPE_APIC;
  182. done:
  183. hi |= MASK_COUNT_EN_HI;
  184. wrmsr(tr->b->address, lo, hi);
  185. }
  186. static void mce_threshold_block_init(struct threshold_block *b, int offset)
  187. {
  188. struct thresh_restart tr = {
  189. .b = b,
  190. .set_lvt_off = 1,
  191. .lvt_off = offset,
  192. };
  193. b->threshold_limit = THRESHOLD_MAX;
  194. threshold_restart_bank(&tr);
  195. };
  196. static int setup_APIC_mce_threshold(int reserved, int new)
  197. {
  198. if (reserved < 0 && !setup_APIC_eilvt(new, THRESHOLD_APIC_VECTOR,
  199. APIC_EILVT_MSG_FIX, 0))
  200. return new;
  201. return reserved;
  202. }
  203. static int setup_APIC_deferred_error(int reserved, int new)
  204. {
  205. if (reserved < 0 && !setup_APIC_eilvt(new, DEFERRED_ERROR_VECTOR,
  206. APIC_EILVT_MSG_FIX, 0))
  207. return new;
  208. return reserved;
  209. }
  210. static void deferred_error_interrupt_enable(struct cpuinfo_x86 *c)
  211. {
  212. u32 low = 0, high = 0;
  213. int def_offset = -1, def_new;
  214. if (rdmsr_safe(MSR_CU_DEF_ERR, &low, &high))
  215. return;
  216. def_new = (low & MASK_DEF_LVTOFF) >> 4;
  217. if (!(low & MASK_DEF_LVTOFF)) {
  218. pr_err(FW_BUG "Your BIOS is not setting up LVT offset 0x2 for deferred error IRQs correctly.\n");
  219. def_new = DEF_LVT_OFF;
  220. low = (low & ~MASK_DEF_LVTOFF) | (DEF_LVT_OFF << 4);
  221. }
  222. def_offset = setup_APIC_deferred_error(def_offset, def_new);
  223. if ((def_offset == def_new) &&
  224. (deferred_error_int_vector != amd_deferred_error_interrupt))
  225. deferred_error_int_vector = amd_deferred_error_interrupt;
  226. low = (low & ~MASK_DEF_INT_TYPE) | DEF_INT_TYPE_APIC;
  227. wrmsr(MSR_CU_DEF_ERR, low, high);
  228. }
  229. static int
  230. prepare_threshold_block(unsigned int bank, unsigned int block, u32 addr,
  231. int offset, u32 misc_high)
  232. {
  233. unsigned int cpu = smp_processor_id();
  234. struct threshold_block b;
  235. int new;
  236. if (!block)
  237. per_cpu(bank_map, cpu) |= (1 << bank);
  238. memset(&b, 0, sizeof(b));
  239. b.cpu = cpu;
  240. b.bank = bank;
  241. b.block = block;
  242. b.address = addr;
  243. b.interrupt_capable = lvt_interrupt_supported(bank, misc_high);
  244. if (!b.interrupt_capable)
  245. goto done;
  246. b.interrupt_enable = 1;
  247. if (mce_flags.smca) {
  248. u32 smca_low, smca_high;
  249. u32 smca_addr = MSR_AMD64_SMCA_MCx_CONFIG(bank);
  250. if (!rdmsr_safe(smca_addr, &smca_low, &smca_high)) {
  251. smca_high |= SMCA_MCAX_EN_OFF;
  252. wrmsr(smca_addr, smca_low, smca_high);
  253. }
  254. /* Gather LVT offset for thresholding: */
  255. if (rdmsr_safe(MSR_CU_DEF_ERR, &smca_low, &smca_high))
  256. goto out;
  257. new = (smca_low & SMCA_THR_LVT_OFF) >> 12;
  258. } else {
  259. new = (misc_high & MASK_LVTOFF_HI) >> 20;
  260. }
  261. offset = setup_APIC_mce_threshold(offset, new);
  262. if ((offset == new) && (mce_threshold_vector != amd_threshold_interrupt))
  263. mce_threshold_vector = amd_threshold_interrupt;
  264. done:
  265. mce_threshold_block_init(&b, offset);
  266. out:
  267. return offset;
  268. }
  269. /* cpu init entry point, called from mce.c with preempt off */
  270. void mce_amd_feature_init(struct cpuinfo_x86 *c)
  271. {
  272. u32 low = 0, high = 0, address = 0;
  273. unsigned int bank, block;
  274. int offset = -1;
  275. for (bank = 0; bank < mca_cfg.banks; ++bank) {
  276. for (block = 0; block < NR_BLOCKS; ++block) {
  277. if (block == 0)
  278. address = MSR_IA32_MCx_MISC(bank);
  279. else if (block == 1) {
  280. address = (low & MASK_BLKPTR_LO) >> 21;
  281. if (!address)
  282. break;
  283. address += MCG_XBLK_ADDR;
  284. } else
  285. ++address;
  286. if (rdmsr_safe(address, &low, &high))
  287. break;
  288. if (!(high & MASK_VALID_HI))
  289. continue;
  290. if (!(high & MASK_CNTP_HI) ||
  291. (high & MASK_LOCKED_HI))
  292. continue;
  293. offset = prepare_threshold_block(bank, block, address, offset, high);
  294. }
  295. }
  296. if (mce_flags.succor)
  297. deferred_error_interrupt_enable(c);
  298. }
  299. static void __log_error(unsigned int bank, bool threshold_err, u64 misc)
  300. {
  301. struct mce m;
  302. u64 status;
  303. rdmsrl(MSR_IA32_MCx_STATUS(bank), status);
  304. if (!(status & MCI_STATUS_VAL))
  305. return;
  306. mce_setup(&m);
  307. m.status = status;
  308. m.bank = bank;
  309. if (threshold_err)
  310. m.misc = misc;
  311. if (m.status & MCI_STATUS_ADDRV)
  312. rdmsrl(MSR_IA32_MCx_ADDR(bank), m.addr);
  313. mce_log(&m);
  314. wrmsrl(MSR_IA32_MCx_STATUS(bank), 0);
  315. }
  316. static inline void __smp_deferred_error_interrupt(void)
  317. {
  318. inc_irq_stat(irq_deferred_error_count);
  319. deferred_error_int_vector();
  320. }
  321. asmlinkage __visible void smp_deferred_error_interrupt(void)
  322. {
  323. entering_irq();
  324. __smp_deferred_error_interrupt();
  325. exiting_ack_irq();
  326. }
  327. asmlinkage __visible void smp_trace_deferred_error_interrupt(void)
  328. {
  329. entering_irq();
  330. trace_deferred_error_apic_entry(DEFERRED_ERROR_VECTOR);
  331. __smp_deferred_error_interrupt();
  332. trace_deferred_error_apic_exit(DEFERRED_ERROR_VECTOR);
  333. exiting_ack_irq();
  334. }
  335. /* APIC interrupt handler for deferred errors */
  336. static void amd_deferred_error_interrupt(void)
  337. {
  338. u64 status;
  339. unsigned int bank;
  340. for (bank = 0; bank < mca_cfg.banks; ++bank) {
  341. rdmsrl(MSR_IA32_MCx_STATUS(bank), status);
  342. if (!(status & MCI_STATUS_VAL) ||
  343. !(status & MCI_STATUS_DEFERRED))
  344. continue;
  345. __log_error(bank, false, 0);
  346. break;
  347. }
  348. }
  349. /*
  350. * APIC Interrupt Handler
  351. */
  352. /*
  353. * threshold interrupt handler will service THRESHOLD_APIC_VECTOR.
  354. * the interrupt goes off when error_count reaches threshold_limit.
  355. * the handler will simply log mcelog w/ software defined bank number.
  356. */
  357. static void amd_threshold_interrupt(void)
  358. {
  359. u32 low = 0, high = 0, address = 0;
  360. int cpu = smp_processor_id();
  361. unsigned int bank, block;
  362. /* assume first bank caused it */
  363. for (bank = 0; bank < mca_cfg.banks; ++bank) {
  364. if (!(per_cpu(bank_map, cpu) & (1 << bank)))
  365. continue;
  366. for (block = 0; block < NR_BLOCKS; ++block) {
  367. if (block == 0) {
  368. address = MSR_IA32_MCx_MISC(bank);
  369. } else if (block == 1) {
  370. address = (low & MASK_BLKPTR_LO) >> 21;
  371. if (!address)
  372. break;
  373. address += MCG_XBLK_ADDR;
  374. } else {
  375. ++address;
  376. }
  377. if (rdmsr_safe(address, &low, &high))
  378. break;
  379. if (!(high & MASK_VALID_HI)) {
  380. if (block)
  381. continue;
  382. else
  383. break;
  384. }
  385. if (!(high & MASK_CNTP_HI) ||
  386. (high & MASK_LOCKED_HI))
  387. continue;
  388. /*
  389. * Log the machine check that caused the threshold
  390. * event.
  391. */
  392. if (high & MASK_OVERFLOW_HI)
  393. goto log;
  394. }
  395. }
  396. return;
  397. log:
  398. __log_error(bank, true, ((u64)high << 32) | low);
  399. }
  400. /*
  401. * Sysfs Interface
  402. */
  403. struct threshold_attr {
  404. struct attribute attr;
  405. ssize_t (*show) (struct threshold_block *, char *);
  406. ssize_t (*store) (struct threshold_block *, const char *, size_t count);
  407. };
  408. #define SHOW_FIELDS(name) \
  409. static ssize_t show_ ## name(struct threshold_block *b, char *buf) \
  410. { \
  411. return sprintf(buf, "%lu\n", (unsigned long) b->name); \
  412. }
  413. SHOW_FIELDS(interrupt_enable)
  414. SHOW_FIELDS(threshold_limit)
  415. static ssize_t
  416. store_interrupt_enable(struct threshold_block *b, const char *buf, size_t size)
  417. {
  418. struct thresh_restart tr;
  419. unsigned long new;
  420. if (!b->interrupt_capable)
  421. return -EINVAL;
  422. if (kstrtoul(buf, 0, &new) < 0)
  423. return -EINVAL;
  424. b->interrupt_enable = !!new;
  425. memset(&tr, 0, sizeof(tr));
  426. tr.b = b;
  427. smp_call_function_single(b->cpu, threshold_restart_bank, &tr, 1);
  428. return size;
  429. }
  430. static ssize_t
  431. store_threshold_limit(struct threshold_block *b, const char *buf, size_t size)
  432. {
  433. struct thresh_restart tr;
  434. unsigned long new;
  435. if (kstrtoul(buf, 0, &new) < 0)
  436. return -EINVAL;
  437. if (new > THRESHOLD_MAX)
  438. new = THRESHOLD_MAX;
  439. if (new < 1)
  440. new = 1;
  441. memset(&tr, 0, sizeof(tr));
  442. tr.old_limit = b->threshold_limit;
  443. b->threshold_limit = new;
  444. tr.b = b;
  445. smp_call_function_single(b->cpu, threshold_restart_bank, &tr, 1);
  446. return size;
  447. }
  448. static ssize_t show_error_count(struct threshold_block *b, char *buf)
  449. {
  450. u32 lo, hi;
  451. rdmsr_on_cpu(b->cpu, b->address, &lo, &hi);
  452. return sprintf(buf, "%u\n", ((hi & THRESHOLD_MAX) -
  453. (THRESHOLD_MAX - b->threshold_limit)));
  454. }
  455. static struct threshold_attr error_count = {
  456. .attr = {.name = __stringify(error_count), .mode = 0444 },
  457. .show = show_error_count,
  458. };
  459. #define RW_ATTR(val) \
  460. static struct threshold_attr val = { \
  461. .attr = {.name = __stringify(val), .mode = 0644 }, \
  462. .show = show_## val, \
  463. .store = store_## val, \
  464. };
  465. RW_ATTR(interrupt_enable);
  466. RW_ATTR(threshold_limit);
  467. static struct attribute *default_attrs[] = {
  468. &threshold_limit.attr,
  469. &error_count.attr,
  470. NULL, /* possibly interrupt_enable if supported, see below */
  471. NULL,
  472. };
  473. #define to_block(k) container_of(k, struct threshold_block, kobj)
  474. #define to_attr(a) container_of(a, struct threshold_attr, attr)
  475. static ssize_t show(struct kobject *kobj, struct attribute *attr, char *buf)
  476. {
  477. struct threshold_block *b = to_block(kobj);
  478. struct threshold_attr *a = to_attr(attr);
  479. ssize_t ret;
  480. ret = a->show ? a->show(b, buf) : -EIO;
  481. return ret;
  482. }
  483. static ssize_t store(struct kobject *kobj, struct attribute *attr,
  484. const char *buf, size_t count)
  485. {
  486. struct threshold_block *b = to_block(kobj);
  487. struct threshold_attr *a = to_attr(attr);
  488. ssize_t ret;
  489. ret = a->store ? a->store(b, buf, count) : -EIO;
  490. return ret;
  491. }
  492. static const struct sysfs_ops threshold_ops = {
  493. .show = show,
  494. .store = store,
  495. };
  496. static struct kobj_type threshold_ktype = {
  497. .sysfs_ops = &threshold_ops,
  498. .default_attrs = default_attrs,
  499. };
  500. static int allocate_threshold_blocks(unsigned int cpu, unsigned int bank,
  501. unsigned int block, u32 address)
  502. {
  503. struct threshold_block *b = NULL;
  504. u32 low, high;
  505. int err;
  506. if ((bank >= mca_cfg.banks) || (block >= NR_BLOCKS))
  507. return 0;
  508. if (rdmsr_safe_on_cpu(cpu, address, &low, &high))
  509. return 0;
  510. if (!(high & MASK_VALID_HI)) {
  511. if (block)
  512. goto recurse;
  513. else
  514. return 0;
  515. }
  516. if (!(high & MASK_CNTP_HI) ||
  517. (high & MASK_LOCKED_HI))
  518. goto recurse;
  519. b = kzalloc(sizeof(struct threshold_block), GFP_KERNEL);
  520. if (!b)
  521. return -ENOMEM;
  522. b->block = block;
  523. b->bank = bank;
  524. b->cpu = cpu;
  525. b->address = address;
  526. b->interrupt_enable = 0;
  527. b->interrupt_capable = lvt_interrupt_supported(bank, high);
  528. b->threshold_limit = THRESHOLD_MAX;
  529. if (b->interrupt_capable) {
  530. threshold_ktype.default_attrs[2] = &interrupt_enable.attr;
  531. b->interrupt_enable = 1;
  532. } else {
  533. threshold_ktype.default_attrs[2] = NULL;
  534. }
  535. INIT_LIST_HEAD(&b->miscj);
  536. if (per_cpu(threshold_banks, cpu)[bank]->blocks) {
  537. list_add(&b->miscj,
  538. &per_cpu(threshold_banks, cpu)[bank]->blocks->miscj);
  539. } else {
  540. per_cpu(threshold_banks, cpu)[bank]->blocks = b;
  541. }
  542. err = kobject_init_and_add(&b->kobj, &threshold_ktype,
  543. per_cpu(threshold_banks, cpu)[bank]->kobj,
  544. (bank == 4 ? bank4_names(b) : th_names[bank]));
  545. if (err)
  546. goto out_free;
  547. recurse:
  548. if (!block) {
  549. address = (low & MASK_BLKPTR_LO) >> 21;
  550. if (!address)
  551. return 0;
  552. address += MCG_XBLK_ADDR;
  553. } else {
  554. ++address;
  555. }
  556. err = allocate_threshold_blocks(cpu, bank, ++block, address);
  557. if (err)
  558. goto out_free;
  559. if (b)
  560. kobject_uevent(&b->kobj, KOBJ_ADD);
  561. return err;
  562. out_free:
  563. if (b) {
  564. kobject_put(&b->kobj);
  565. list_del(&b->miscj);
  566. kfree(b);
  567. }
  568. return err;
  569. }
  570. static int __threshold_add_blocks(struct threshold_bank *b)
  571. {
  572. struct list_head *head = &b->blocks->miscj;
  573. struct threshold_block *pos = NULL;
  574. struct threshold_block *tmp = NULL;
  575. int err = 0;
  576. err = kobject_add(&b->blocks->kobj, b->kobj, b->blocks->kobj.name);
  577. if (err)
  578. return err;
  579. list_for_each_entry_safe(pos, tmp, head, miscj) {
  580. err = kobject_add(&pos->kobj, b->kobj, pos->kobj.name);
  581. if (err) {
  582. list_for_each_entry_safe_reverse(pos, tmp, head, miscj)
  583. kobject_del(&pos->kobj);
  584. return err;
  585. }
  586. }
  587. return err;
  588. }
  589. static int threshold_create_bank(unsigned int cpu, unsigned int bank)
  590. {
  591. struct device *dev = per_cpu(mce_device, cpu);
  592. struct amd_northbridge *nb = NULL;
  593. struct threshold_bank *b = NULL;
  594. const char *name = th_names[bank];
  595. int err = 0;
  596. if (is_shared_bank(bank)) {
  597. nb = node_to_amd_nb(amd_get_nb_id(cpu));
  598. /* threshold descriptor already initialized on this node? */
  599. if (nb && nb->bank4) {
  600. /* yes, use it */
  601. b = nb->bank4;
  602. err = kobject_add(b->kobj, &dev->kobj, name);
  603. if (err)
  604. goto out;
  605. per_cpu(threshold_banks, cpu)[bank] = b;
  606. atomic_inc(&b->cpus);
  607. err = __threshold_add_blocks(b);
  608. goto out;
  609. }
  610. }
  611. b = kzalloc(sizeof(struct threshold_bank), GFP_KERNEL);
  612. if (!b) {
  613. err = -ENOMEM;
  614. goto out;
  615. }
  616. b->kobj = kobject_create_and_add(name, &dev->kobj);
  617. if (!b->kobj) {
  618. err = -EINVAL;
  619. goto out_free;
  620. }
  621. per_cpu(threshold_banks, cpu)[bank] = b;
  622. if (is_shared_bank(bank)) {
  623. atomic_set(&b->cpus, 1);
  624. /* nb is already initialized, see above */
  625. if (nb) {
  626. WARN_ON(nb->bank4);
  627. nb->bank4 = b;
  628. }
  629. }
  630. err = allocate_threshold_blocks(cpu, bank, 0, MSR_IA32_MCx_MISC(bank));
  631. if (!err)
  632. goto out;
  633. out_free:
  634. kfree(b);
  635. out:
  636. return err;
  637. }
  638. /* create dir/files for all valid threshold banks */
  639. static int threshold_create_device(unsigned int cpu)
  640. {
  641. unsigned int bank;
  642. struct threshold_bank **bp;
  643. int err = 0;
  644. bp = kzalloc(sizeof(struct threshold_bank *) * mca_cfg.banks,
  645. GFP_KERNEL);
  646. if (!bp)
  647. return -ENOMEM;
  648. per_cpu(threshold_banks, cpu) = bp;
  649. for (bank = 0; bank < mca_cfg.banks; ++bank) {
  650. if (!(per_cpu(bank_map, cpu) & (1 << bank)))
  651. continue;
  652. err = threshold_create_bank(cpu, bank);
  653. if (err)
  654. return err;
  655. }
  656. return err;
  657. }
  658. static void deallocate_threshold_block(unsigned int cpu,
  659. unsigned int bank)
  660. {
  661. struct threshold_block *pos = NULL;
  662. struct threshold_block *tmp = NULL;
  663. struct threshold_bank *head = per_cpu(threshold_banks, cpu)[bank];
  664. if (!head)
  665. return;
  666. list_for_each_entry_safe(pos, tmp, &head->blocks->miscj, miscj) {
  667. kobject_put(&pos->kobj);
  668. list_del(&pos->miscj);
  669. kfree(pos);
  670. }
  671. kfree(per_cpu(threshold_banks, cpu)[bank]->blocks);
  672. per_cpu(threshold_banks, cpu)[bank]->blocks = NULL;
  673. }
  674. static void __threshold_remove_blocks(struct threshold_bank *b)
  675. {
  676. struct threshold_block *pos = NULL;
  677. struct threshold_block *tmp = NULL;
  678. kobject_del(b->kobj);
  679. list_for_each_entry_safe(pos, tmp, &b->blocks->miscj, miscj)
  680. kobject_del(&pos->kobj);
  681. }
  682. static void threshold_remove_bank(unsigned int cpu, int bank)
  683. {
  684. struct amd_northbridge *nb;
  685. struct threshold_bank *b;
  686. b = per_cpu(threshold_banks, cpu)[bank];
  687. if (!b)
  688. return;
  689. if (!b->blocks)
  690. goto free_out;
  691. if (is_shared_bank(bank)) {
  692. if (!atomic_dec_and_test(&b->cpus)) {
  693. __threshold_remove_blocks(b);
  694. per_cpu(threshold_banks, cpu)[bank] = NULL;
  695. return;
  696. } else {
  697. /*
  698. * the last CPU on this node using the shared bank is
  699. * going away, remove that bank now.
  700. */
  701. nb = node_to_amd_nb(amd_get_nb_id(cpu));
  702. nb->bank4 = NULL;
  703. }
  704. }
  705. deallocate_threshold_block(cpu, bank);
  706. free_out:
  707. kobject_del(b->kobj);
  708. kobject_put(b->kobj);
  709. kfree(b);
  710. per_cpu(threshold_banks, cpu)[bank] = NULL;
  711. }
  712. static void threshold_remove_device(unsigned int cpu)
  713. {
  714. unsigned int bank;
  715. for (bank = 0; bank < mca_cfg.banks; ++bank) {
  716. if (!(per_cpu(bank_map, cpu) & (1 << bank)))
  717. continue;
  718. threshold_remove_bank(cpu, bank);
  719. }
  720. kfree(per_cpu(threshold_banks, cpu));
  721. }
  722. /* get notified when a cpu comes on/off */
  723. static void
  724. amd_64_threshold_cpu_callback(unsigned long action, unsigned int cpu)
  725. {
  726. switch (action) {
  727. case CPU_ONLINE:
  728. case CPU_ONLINE_FROZEN:
  729. threshold_create_device(cpu);
  730. break;
  731. case CPU_DEAD:
  732. case CPU_DEAD_FROZEN:
  733. threshold_remove_device(cpu);
  734. break;
  735. default:
  736. break;
  737. }
  738. }
  739. static __init int threshold_init_device(void)
  740. {
  741. unsigned lcpu = 0;
  742. /* to hit CPUs online before the notifier is up */
  743. for_each_online_cpu(lcpu) {
  744. int err = threshold_create_device(lcpu);
  745. if (err)
  746. return err;
  747. }
  748. threshold_cpu_callback = amd_64_threshold_cpu_callback;
  749. return 0;
  750. }
  751. /*
  752. * there are 3 funcs which need to be _initcalled in a logic sequence:
  753. * 1. xen_late_init_mcelog
  754. * 2. mcheck_init_device
  755. * 3. threshold_init_device
  756. *
  757. * xen_late_init_mcelog must register xen_mce_chrdev_device before
  758. * native mce_chrdev_device registration if running under xen platform;
  759. *
  760. * mcheck_init_device should be inited before threshold_init_device to
  761. * initialize mce_device, otherwise a NULL ptr dereference will cause panic.
  762. *
  763. * so we use following _initcalls
  764. * 1. device_initcall(xen_late_init_mcelog);
  765. * 2. device_initcall_sync(mcheck_init_device);
  766. * 3. late_initcall(threshold_init_device);
  767. *
  768. * when running under xen, the initcall order is 1,2,3;
  769. * on baremetal, we skip 1 and we do only 2 and 3.
  770. */
  771. late_initcall(threshold_init_device);