mce_amd.c 17 KB

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