mce_amd.c 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791
  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 * const bank4_names(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;
  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_MC0_MISC + bank * 4;
  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. int new = (high & MASK_LVTOFF_HI) >> 20;
  209. offset = setup_APIC_mce(offset, new);
  210. }
  211. mce_threshold_block_init(&b, offset);
  212. mce_threshold_vector = amd_threshold_interrupt;
  213. }
  214. }
  215. }
  216. /*
  217. * APIC Interrupt Handler
  218. */
  219. /*
  220. * threshold interrupt handler will service THRESHOLD_APIC_VECTOR.
  221. * the interrupt goes off when error_count reaches threshold_limit.
  222. * the handler will simply log mcelog w/ software defined bank number.
  223. */
  224. static void amd_threshold_interrupt(void)
  225. {
  226. u32 low = 0, high = 0, address = 0;
  227. unsigned int bank, block;
  228. struct mce m;
  229. mce_setup(&m);
  230. /* assume first bank caused it */
  231. for (bank = 0; bank < mca_cfg.banks; ++bank) {
  232. if (!(per_cpu(bank_map, m.cpu) & (1 << bank)))
  233. continue;
  234. for (block = 0; block < NR_BLOCKS; ++block) {
  235. if (block == 0) {
  236. address = MSR_IA32_MC0_MISC + bank * 4;
  237. } else if (block == 1) {
  238. address = (low & MASK_BLKPTR_LO) >> 21;
  239. if (!address)
  240. break;
  241. address += MCG_XBLK_ADDR;
  242. } else {
  243. ++address;
  244. }
  245. if (rdmsr_safe(address, &low, &high))
  246. break;
  247. if (!(high & MASK_VALID_HI)) {
  248. if (block)
  249. continue;
  250. else
  251. break;
  252. }
  253. if (!(high & MASK_CNTP_HI) ||
  254. (high & MASK_LOCKED_HI))
  255. continue;
  256. /*
  257. * Log the machine check that caused the threshold
  258. * event.
  259. */
  260. machine_check_poll(MCP_TIMESTAMP,
  261. &__get_cpu_var(mce_poll_banks));
  262. if (high & MASK_OVERFLOW_HI) {
  263. rdmsrl(address, m.misc);
  264. rdmsrl(MSR_IA32_MC0_STATUS + bank * 4,
  265. m.status);
  266. m.bank = K8_MCE_THRESHOLD_BASE
  267. + bank * NR_BLOCKS
  268. + block;
  269. mce_log(&m);
  270. return;
  271. }
  272. }
  273. }
  274. }
  275. /*
  276. * Sysfs Interface
  277. */
  278. struct threshold_attr {
  279. struct attribute attr;
  280. ssize_t (*show) (struct threshold_block *, char *);
  281. ssize_t (*store) (struct threshold_block *, const char *, size_t count);
  282. };
  283. #define SHOW_FIELDS(name) \
  284. static ssize_t show_ ## name(struct threshold_block *b, char *buf) \
  285. { \
  286. return sprintf(buf, "%lu\n", (unsigned long) b->name); \
  287. }
  288. SHOW_FIELDS(interrupt_enable)
  289. SHOW_FIELDS(threshold_limit)
  290. static ssize_t
  291. store_interrupt_enable(struct threshold_block *b, const char *buf, size_t size)
  292. {
  293. struct thresh_restart tr;
  294. unsigned long new;
  295. if (!b->interrupt_capable)
  296. return -EINVAL;
  297. if (strict_strtoul(buf, 0, &new) < 0)
  298. return -EINVAL;
  299. b->interrupt_enable = !!new;
  300. memset(&tr, 0, sizeof(tr));
  301. tr.b = b;
  302. smp_call_function_single(b->cpu, threshold_restart_bank, &tr, 1);
  303. return size;
  304. }
  305. static ssize_t
  306. store_threshold_limit(struct threshold_block *b, const char *buf, size_t size)
  307. {
  308. struct thresh_restart tr;
  309. unsigned long new;
  310. if (strict_strtoul(buf, 0, &new) < 0)
  311. return -EINVAL;
  312. if (new > THRESHOLD_MAX)
  313. new = THRESHOLD_MAX;
  314. if (new < 1)
  315. new = 1;
  316. memset(&tr, 0, sizeof(tr));
  317. tr.old_limit = b->threshold_limit;
  318. b->threshold_limit = new;
  319. tr.b = b;
  320. smp_call_function_single(b->cpu, threshold_restart_bank, &tr, 1);
  321. return size;
  322. }
  323. static ssize_t show_error_count(struct threshold_block *b, char *buf)
  324. {
  325. u32 lo, hi;
  326. rdmsr_on_cpu(b->cpu, b->address, &lo, &hi);
  327. return sprintf(buf, "%u\n", ((hi & THRESHOLD_MAX) -
  328. (THRESHOLD_MAX - b->threshold_limit)));
  329. }
  330. static struct threshold_attr error_count = {
  331. .attr = {.name = __stringify(error_count), .mode = 0444 },
  332. .show = show_error_count,
  333. };
  334. #define RW_ATTR(val) \
  335. static struct threshold_attr val = { \
  336. .attr = {.name = __stringify(val), .mode = 0644 }, \
  337. .show = show_## val, \
  338. .store = store_## val, \
  339. };
  340. RW_ATTR(interrupt_enable);
  341. RW_ATTR(threshold_limit);
  342. static struct attribute *default_attrs[] = {
  343. &threshold_limit.attr,
  344. &error_count.attr,
  345. NULL, /* possibly interrupt_enable if supported, see below */
  346. NULL,
  347. };
  348. #define to_block(k) container_of(k, struct threshold_block, kobj)
  349. #define to_attr(a) container_of(a, struct threshold_attr, attr)
  350. static ssize_t show(struct kobject *kobj, struct attribute *attr, char *buf)
  351. {
  352. struct threshold_block *b = to_block(kobj);
  353. struct threshold_attr *a = to_attr(attr);
  354. ssize_t ret;
  355. ret = a->show ? a->show(b, buf) : -EIO;
  356. return ret;
  357. }
  358. static ssize_t store(struct kobject *kobj, struct attribute *attr,
  359. const char *buf, size_t count)
  360. {
  361. struct threshold_block *b = to_block(kobj);
  362. struct threshold_attr *a = to_attr(attr);
  363. ssize_t ret;
  364. ret = a->store ? a->store(b, buf, count) : -EIO;
  365. return ret;
  366. }
  367. static const struct sysfs_ops threshold_ops = {
  368. .show = show,
  369. .store = store,
  370. };
  371. static struct kobj_type threshold_ktype = {
  372. .sysfs_ops = &threshold_ops,
  373. .default_attrs = default_attrs,
  374. };
  375. static __cpuinit int allocate_threshold_blocks(unsigned int cpu,
  376. unsigned int bank,
  377. unsigned int block,
  378. u32 address)
  379. {
  380. struct threshold_block *b = NULL;
  381. u32 low, high;
  382. int err;
  383. if ((bank >= mca_cfg.banks) || (block >= NR_BLOCKS))
  384. return 0;
  385. if (rdmsr_safe_on_cpu(cpu, address, &low, &high))
  386. return 0;
  387. if (!(high & MASK_VALID_HI)) {
  388. if (block)
  389. goto recurse;
  390. else
  391. return 0;
  392. }
  393. if (!(high & MASK_CNTP_HI) ||
  394. (high & MASK_LOCKED_HI))
  395. goto recurse;
  396. b = kzalloc(sizeof(struct threshold_block), GFP_KERNEL);
  397. if (!b)
  398. return -ENOMEM;
  399. b->block = block;
  400. b->bank = bank;
  401. b->cpu = cpu;
  402. b->address = address;
  403. b->interrupt_enable = 0;
  404. b->interrupt_capable = lvt_interrupt_supported(bank, high);
  405. b->threshold_limit = THRESHOLD_MAX;
  406. if (b->interrupt_capable)
  407. threshold_ktype.default_attrs[2] = &interrupt_enable.attr;
  408. else
  409. threshold_ktype.default_attrs[2] = NULL;
  410. INIT_LIST_HEAD(&b->miscj);
  411. if (per_cpu(threshold_banks, cpu)[bank]->blocks) {
  412. list_add(&b->miscj,
  413. &per_cpu(threshold_banks, cpu)[bank]->blocks->miscj);
  414. } else {
  415. per_cpu(threshold_banks, cpu)[bank]->blocks = b;
  416. }
  417. err = kobject_init_and_add(&b->kobj, &threshold_ktype,
  418. per_cpu(threshold_banks, cpu)[bank]->kobj,
  419. (bank == 4 ? bank4_names(b) : th_names[bank]));
  420. if (err)
  421. goto out_free;
  422. recurse:
  423. if (!block) {
  424. address = (low & MASK_BLKPTR_LO) >> 21;
  425. if (!address)
  426. return 0;
  427. address += MCG_XBLK_ADDR;
  428. } else {
  429. ++address;
  430. }
  431. err = allocate_threshold_blocks(cpu, bank, ++block, address);
  432. if (err)
  433. goto out_free;
  434. if (b)
  435. kobject_uevent(&b->kobj, KOBJ_ADD);
  436. return err;
  437. out_free:
  438. if (b) {
  439. kobject_put(&b->kobj);
  440. list_del(&b->miscj);
  441. kfree(b);
  442. }
  443. return err;
  444. }
  445. static __cpuinit int __threshold_add_blocks(struct threshold_bank *b)
  446. {
  447. struct list_head *head = &b->blocks->miscj;
  448. struct threshold_block *pos = NULL;
  449. struct threshold_block *tmp = NULL;
  450. int err = 0;
  451. err = kobject_add(&b->blocks->kobj, b->kobj, b->blocks->kobj.name);
  452. if (err)
  453. return err;
  454. list_for_each_entry_safe(pos, tmp, head, miscj) {
  455. err = kobject_add(&pos->kobj, b->kobj, pos->kobj.name);
  456. if (err) {
  457. list_for_each_entry_safe_reverse(pos, tmp, head, miscj)
  458. kobject_del(&pos->kobj);
  459. return err;
  460. }
  461. }
  462. return err;
  463. }
  464. static __cpuinit int threshold_create_bank(unsigned int cpu, unsigned int bank)
  465. {
  466. struct device *dev = per_cpu(mce_device, cpu);
  467. struct amd_northbridge *nb = NULL;
  468. struct threshold_bank *b = NULL;
  469. const char *name = th_names[bank];
  470. int err = 0;
  471. if (is_shared_bank(bank)) {
  472. nb = node_to_amd_nb(amd_get_nb_id(cpu));
  473. /* threshold descriptor already initialized on this node? */
  474. if (nb && nb->bank4) {
  475. /* yes, use it */
  476. b = nb->bank4;
  477. err = kobject_add(b->kobj, &dev->kobj, name);
  478. if (err)
  479. goto out;
  480. per_cpu(threshold_banks, cpu)[bank] = b;
  481. atomic_inc(&b->cpus);
  482. err = __threshold_add_blocks(b);
  483. goto out;
  484. }
  485. }
  486. b = kzalloc(sizeof(struct threshold_bank), GFP_KERNEL);
  487. if (!b) {
  488. err = -ENOMEM;
  489. goto out;
  490. }
  491. b->kobj = kobject_create_and_add(name, &dev->kobj);
  492. if (!b->kobj) {
  493. err = -EINVAL;
  494. goto out_free;
  495. }
  496. per_cpu(threshold_banks, cpu)[bank] = b;
  497. if (is_shared_bank(bank)) {
  498. atomic_set(&b->cpus, 1);
  499. /* nb is already initialized, see above */
  500. if (nb) {
  501. WARN_ON(nb->bank4);
  502. nb->bank4 = b;
  503. }
  504. }
  505. err = allocate_threshold_blocks(cpu, bank, 0,
  506. MSR_IA32_MC0_MISC + bank * 4);
  507. if (!err)
  508. goto out;
  509. out_free:
  510. kfree(b);
  511. out:
  512. return err;
  513. }
  514. /* create dir/files for all valid threshold banks */
  515. static __cpuinit int threshold_create_device(unsigned int cpu)
  516. {
  517. unsigned int bank;
  518. struct threshold_bank **bp;
  519. int err = 0;
  520. bp = kzalloc(sizeof(struct threshold_bank *) * mca_cfg.banks,
  521. GFP_KERNEL);
  522. if (!bp)
  523. return -ENOMEM;
  524. per_cpu(threshold_banks, cpu) = bp;
  525. for (bank = 0; bank < mca_cfg.banks; ++bank) {
  526. if (!(per_cpu(bank_map, cpu) & (1 << bank)))
  527. continue;
  528. err = threshold_create_bank(cpu, bank);
  529. if (err)
  530. return err;
  531. }
  532. return err;
  533. }
  534. static void deallocate_threshold_block(unsigned int cpu,
  535. unsigned int bank)
  536. {
  537. struct threshold_block *pos = NULL;
  538. struct threshold_block *tmp = NULL;
  539. struct threshold_bank *head = per_cpu(threshold_banks, cpu)[bank];
  540. if (!head)
  541. return;
  542. list_for_each_entry_safe(pos, tmp, &head->blocks->miscj, miscj) {
  543. kobject_put(&pos->kobj);
  544. list_del(&pos->miscj);
  545. kfree(pos);
  546. }
  547. kfree(per_cpu(threshold_banks, cpu)[bank]->blocks);
  548. per_cpu(threshold_banks, cpu)[bank]->blocks = NULL;
  549. }
  550. static void __threshold_remove_blocks(struct threshold_bank *b)
  551. {
  552. struct threshold_block *pos = NULL;
  553. struct threshold_block *tmp = NULL;
  554. kobject_del(b->kobj);
  555. list_for_each_entry_safe(pos, tmp, &b->blocks->miscj, miscj)
  556. kobject_del(&pos->kobj);
  557. }
  558. static void threshold_remove_bank(unsigned int cpu, int bank)
  559. {
  560. struct amd_northbridge *nb;
  561. struct threshold_bank *b;
  562. b = per_cpu(threshold_banks, cpu)[bank];
  563. if (!b)
  564. return;
  565. if (!b->blocks)
  566. goto free_out;
  567. if (is_shared_bank(bank)) {
  568. if (!atomic_dec_and_test(&b->cpus)) {
  569. __threshold_remove_blocks(b);
  570. per_cpu(threshold_banks, cpu)[bank] = NULL;
  571. return;
  572. } else {
  573. /*
  574. * the last CPU on this node using the shared bank is
  575. * going away, remove that bank now.
  576. */
  577. nb = node_to_amd_nb(amd_get_nb_id(cpu));
  578. nb->bank4 = NULL;
  579. }
  580. }
  581. deallocate_threshold_block(cpu, bank);
  582. free_out:
  583. kobject_del(b->kobj);
  584. kobject_put(b->kobj);
  585. kfree(b);
  586. per_cpu(threshold_banks, cpu)[bank] = NULL;
  587. }
  588. static void threshold_remove_device(unsigned int cpu)
  589. {
  590. unsigned int bank;
  591. for (bank = 0; bank < mca_cfg.banks; ++bank) {
  592. if (!(per_cpu(bank_map, cpu) & (1 << bank)))
  593. continue;
  594. threshold_remove_bank(cpu, bank);
  595. }
  596. kfree(per_cpu(threshold_banks, cpu));
  597. }
  598. /* get notified when a cpu comes on/off */
  599. static void __cpuinit
  600. amd_64_threshold_cpu_callback(unsigned long action, unsigned int cpu)
  601. {
  602. switch (action) {
  603. case CPU_ONLINE:
  604. case CPU_ONLINE_FROZEN:
  605. threshold_create_device(cpu);
  606. break;
  607. case CPU_DEAD:
  608. case CPU_DEAD_FROZEN:
  609. threshold_remove_device(cpu);
  610. break;
  611. default:
  612. break;
  613. }
  614. }
  615. static __init int threshold_init_device(void)
  616. {
  617. unsigned lcpu = 0;
  618. /* to hit CPUs online before the notifier is up */
  619. for_each_online_cpu(lcpu) {
  620. int err = threshold_create_device(lcpu);
  621. if (err)
  622. return err;
  623. }
  624. threshold_cpu_callback = amd_64_threshold_cpu_callback;
  625. return 0;
  626. }
  627. /*
  628. * there are 3 funcs which need to be _initcalled in a logic sequence:
  629. * 1. xen_late_init_mcelog
  630. * 2. mcheck_init_device
  631. * 3. threshold_init_device
  632. *
  633. * xen_late_init_mcelog must register xen_mce_chrdev_device before
  634. * native mce_chrdev_device registration if running under xen platform;
  635. *
  636. * mcheck_init_device should be inited before threshold_init_device to
  637. * initialize mce_device, otherwise a NULL ptr dereference will cause panic.
  638. *
  639. * so we use following _initcalls
  640. * 1. device_initcall(xen_late_init_mcelog);
  641. * 2. device_initcall_sync(mcheck_init_device);
  642. * 3. late_initcall(threshold_init_device);
  643. *
  644. * when running under xen, the initcall order is 1,2,3;
  645. * on baremetal, we skip 1 and we do only 2 and 3.
  646. */
  647. late_initcall(threshold_init_device);