mce_amd.c 17 KB

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