mce_amd.c 20 KB

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