nmi_int.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693
  1. /**
  2. * @file nmi_int.c
  3. *
  4. * @remark Copyright 2002-2009 OProfile authors
  5. * @remark Read the file COPYING
  6. *
  7. * @author John Levon <levon@movementarian.org>
  8. * @author Robert Richter <robert.richter@amd.com>
  9. * @author Barry Kasindorf <barry.kasindorf@amd.com>
  10. * @author Jason Yeh <jason.yeh@amd.com>
  11. * @author Suravee Suthikulpanit <suravee.suthikulpanit@amd.com>
  12. */
  13. #include <linux/init.h>
  14. #include <linux/notifier.h>
  15. #include <linux/smp.h>
  16. #include <linux/oprofile.h>
  17. #include <linux/sysdev.h>
  18. #include <linux/slab.h>
  19. #include <linux/moduleparam.h>
  20. #include <linux/kdebug.h>
  21. #include <linux/cpu.h>
  22. #include <asm/nmi.h>
  23. #include <asm/msr.h>
  24. #include <asm/apic.h>
  25. #include "op_counter.h"
  26. #include "op_x86_model.h"
  27. static struct op_x86_model_spec *model;
  28. static DEFINE_PER_CPU(struct op_msrs, cpu_msrs);
  29. static DEFINE_PER_CPU(unsigned long, saved_lvtpc);
  30. /* 0 == registered but off, 1 == registered and on */
  31. static int nmi_enabled = 0;
  32. #ifdef CONFIG_OPROFILE_EVENT_MULTIPLEX
  33. extern atomic_t multiplex_counter;
  34. #endif
  35. struct op_counter_config counter_config[OP_MAX_COUNTER];
  36. /* common functions */
  37. u64 op_x86_get_ctrl(struct op_x86_model_spec const *model,
  38. struct op_counter_config *counter_config)
  39. {
  40. u64 val = 0;
  41. u16 event = (u16)counter_config->event;
  42. val |= ARCH_PERFMON_EVENTSEL_INT;
  43. val |= counter_config->user ? ARCH_PERFMON_EVENTSEL_USR : 0;
  44. val |= counter_config->kernel ? ARCH_PERFMON_EVENTSEL_OS : 0;
  45. val |= (counter_config->unit_mask & 0xFF) << 8;
  46. event &= model->event_mask ? model->event_mask : 0xFF;
  47. val |= event & 0xFF;
  48. val |= (event & 0x0F00) << 24;
  49. return val;
  50. }
  51. static int profile_exceptions_notify(struct notifier_block *self,
  52. unsigned long val, void *data)
  53. {
  54. struct die_args *args = (struct die_args *)data;
  55. int ret = NOTIFY_DONE;
  56. int cpu = smp_processor_id();
  57. switch (val) {
  58. case DIE_NMI:
  59. case DIE_NMI_IPI:
  60. model->check_ctrs(args->regs, &per_cpu(cpu_msrs, cpu));
  61. ret = NOTIFY_STOP;
  62. break;
  63. default:
  64. break;
  65. }
  66. return ret;
  67. }
  68. static void nmi_cpu_save_registers(struct op_msrs *msrs)
  69. {
  70. struct op_msr *counters = msrs->counters;
  71. struct op_msr *controls = msrs->controls;
  72. unsigned int i;
  73. for (i = 0; i < model->num_counters; ++i) {
  74. if (counters[i].addr)
  75. rdmsrl(counters[i].addr, counters[i].saved);
  76. }
  77. for (i = 0; i < model->num_controls; ++i) {
  78. if (controls[i].addr)
  79. rdmsrl(controls[i].addr, controls[i].saved);
  80. }
  81. }
  82. static void nmi_cpu_start(void *dummy)
  83. {
  84. struct op_msrs const *msrs = &__get_cpu_var(cpu_msrs);
  85. model->start(msrs);
  86. }
  87. static int nmi_start(void)
  88. {
  89. on_each_cpu(nmi_cpu_start, NULL, 1);
  90. return 0;
  91. }
  92. static void nmi_cpu_stop(void *dummy)
  93. {
  94. struct op_msrs const *msrs = &__get_cpu_var(cpu_msrs);
  95. model->stop(msrs);
  96. }
  97. static void nmi_stop(void)
  98. {
  99. on_each_cpu(nmi_cpu_stop, NULL, 1);
  100. }
  101. #ifdef CONFIG_OPROFILE_EVENT_MULTIPLEX
  102. static DEFINE_PER_CPU(int, switch_index);
  103. inline int op_x86_phys_to_virt(int phys)
  104. {
  105. return __get_cpu_var(switch_index) + phys;
  106. }
  107. static void nmi_shutdown_mux(void)
  108. {
  109. int i;
  110. for_each_possible_cpu(i) {
  111. kfree(per_cpu(cpu_msrs, i).multiplex);
  112. per_cpu(cpu_msrs, i).multiplex = NULL;
  113. per_cpu(switch_index, i) = 0;
  114. }
  115. }
  116. static int nmi_setup_mux(void)
  117. {
  118. size_t multiplex_size =
  119. sizeof(struct op_msr) * model->num_virt_counters;
  120. int i;
  121. for_each_possible_cpu(i) {
  122. per_cpu(cpu_msrs, i).multiplex =
  123. kmalloc(multiplex_size, GFP_KERNEL);
  124. if (!per_cpu(cpu_msrs, i).multiplex)
  125. return 0;
  126. }
  127. return 1;
  128. }
  129. static void nmi_cpu_setup_mux(int cpu, struct op_msrs const * const msrs)
  130. {
  131. int i;
  132. struct op_msr *multiplex = msrs->multiplex;
  133. for (i = 0; i < model->num_virt_counters; ++i) {
  134. if (counter_config[i].enabled) {
  135. multiplex[i].saved = -(u64)counter_config[i].count;
  136. } else {
  137. multiplex[i].addr = 0;
  138. multiplex[i].saved = 0;
  139. }
  140. }
  141. per_cpu(switch_index, cpu) = 0;
  142. }
  143. static void nmi_cpu_save_mpx_registers(struct op_msrs *msrs)
  144. {
  145. struct op_msr *multiplex = msrs->multiplex;
  146. int i;
  147. for (i = 0; i < model->num_counters; ++i) {
  148. int virt = op_x86_phys_to_virt(i);
  149. if (multiplex[virt].addr)
  150. rdmsrl(multiplex[virt].addr, multiplex[virt].saved);
  151. }
  152. }
  153. static void nmi_cpu_restore_mpx_registers(struct op_msrs *msrs)
  154. {
  155. struct op_msr *multiplex = msrs->multiplex;
  156. int i;
  157. for (i = 0; i < model->num_counters; ++i) {
  158. int virt = op_x86_phys_to_virt(i);
  159. if (multiplex[virt].addr)
  160. wrmsrl(multiplex[virt].addr, multiplex[virt].saved);
  161. }
  162. }
  163. static void nmi_cpu_switch(void *dummy)
  164. {
  165. int cpu = smp_processor_id();
  166. int si = per_cpu(switch_index, cpu);
  167. struct op_msrs *msrs = &per_cpu(cpu_msrs, cpu);
  168. nmi_cpu_stop(NULL);
  169. nmi_cpu_save_mpx_registers(msrs);
  170. /* move to next set */
  171. si += model->num_counters;
  172. if ((si > model->num_virt_counters) || (counter_config[si].count == 0))
  173. per_cpu(switch_index, cpu) = 0;
  174. else
  175. per_cpu(switch_index, cpu) = si;
  176. model->switch_ctrl(model, msrs);
  177. nmi_cpu_restore_mpx_registers(msrs);
  178. nmi_cpu_start(NULL);
  179. }
  180. /*
  181. * Quick check to see if multiplexing is necessary.
  182. * The check should be sufficient since counters are used
  183. * in ordre.
  184. */
  185. static int nmi_multiplex_on(void)
  186. {
  187. return counter_config[model->num_counters].count ? 0 : -EINVAL;
  188. }
  189. static int nmi_switch_event(void)
  190. {
  191. if (!model->switch_ctrl)
  192. return -ENOSYS; /* not implemented */
  193. if (nmi_multiplex_on() < 0)
  194. return -EINVAL; /* not necessary */
  195. on_each_cpu(nmi_cpu_switch, NULL, 1);
  196. atomic_inc(&multiplex_counter);
  197. return 0;
  198. }
  199. #else
  200. inline int op_x86_phys_to_virt(int phys) { return phys; }
  201. static inline void nmi_shutdown_mux(void) { }
  202. static inline int nmi_setup_mux(void) { return 1; }
  203. static inline void
  204. nmi_cpu_setup_mux(int cpu, struct op_msrs const * const msrs) { }
  205. #endif
  206. static void free_msrs(void)
  207. {
  208. int i;
  209. for_each_possible_cpu(i) {
  210. kfree(per_cpu(cpu_msrs, i).counters);
  211. per_cpu(cpu_msrs, i).counters = NULL;
  212. kfree(per_cpu(cpu_msrs, i).controls);
  213. per_cpu(cpu_msrs, i).controls = NULL;
  214. }
  215. }
  216. static int allocate_msrs(void)
  217. {
  218. size_t controls_size = sizeof(struct op_msr) * model->num_controls;
  219. size_t counters_size = sizeof(struct op_msr) * model->num_counters;
  220. int i;
  221. for_each_possible_cpu(i) {
  222. per_cpu(cpu_msrs, i).counters = kmalloc(counters_size,
  223. GFP_KERNEL);
  224. if (!per_cpu(cpu_msrs, i).counters)
  225. return 0;
  226. per_cpu(cpu_msrs, i).controls = kmalloc(controls_size,
  227. GFP_KERNEL);
  228. if (!per_cpu(cpu_msrs, i).controls)
  229. return 0;
  230. }
  231. return 1;
  232. }
  233. static void nmi_cpu_setup(void *dummy)
  234. {
  235. int cpu = smp_processor_id();
  236. struct op_msrs *msrs = &per_cpu(cpu_msrs, cpu);
  237. nmi_cpu_save_registers(msrs);
  238. spin_lock(&oprofilefs_lock);
  239. model->setup_ctrs(model, msrs);
  240. nmi_cpu_setup_mux(cpu, msrs);
  241. spin_unlock(&oprofilefs_lock);
  242. per_cpu(saved_lvtpc, cpu) = apic_read(APIC_LVTPC);
  243. apic_write(APIC_LVTPC, APIC_DM_NMI);
  244. }
  245. static struct notifier_block profile_exceptions_nb = {
  246. .notifier_call = profile_exceptions_notify,
  247. .next = NULL,
  248. .priority = 2
  249. };
  250. static int nmi_setup(void)
  251. {
  252. int err = 0;
  253. int cpu;
  254. if (!allocate_msrs())
  255. err = -ENOMEM;
  256. else if (!nmi_setup_mux())
  257. err = -ENOMEM;
  258. else
  259. err = register_die_notifier(&profile_exceptions_nb);
  260. if (err) {
  261. free_msrs();
  262. nmi_shutdown_mux();
  263. return err;
  264. }
  265. /* We need to serialize save and setup for HT because the subset
  266. * of msrs are distinct for save and setup operations
  267. */
  268. /* Assume saved/restored counters are the same on all CPUs */
  269. model->fill_in_addresses(&per_cpu(cpu_msrs, 0));
  270. for_each_possible_cpu(cpu) {
  271. if (cpu != 0) {
  272. memcpy(per_cpu(cpu_msrs, cpu).counters,
  273. per_cpu(cpu_msrs, 0).counters,
  274. sizeof(struct op_msr) * model->num_counters);
  275. memcpy(per_cpu(cpu_msrs, cpu).controls,
  276. per_cpu(cpu_msrs, 0).controls,
  277. sizeof(struct op_msr) * model->num_controls);
  278. #ifdef CONFIG_OPROFILE_EVENT_MULTIPLEX
  279. memcpy(per_cpu(cpu_msrs, cpu).multiplex,
  280. per_cpu(cpu_msrs, 0).multiplex,
  281. sizeof(struct op_msr) * model->num_virt_counters);
  282. #endif
  283. }
  284. }
  285. on_each_cpu(nmi_cpu_setup, NULL, 1);
  286. nmi_enabled = 1;
  287. return 0;
  288. }
  289. static void nmi_cpu_restore_registers(struct op_msrs *msrs)
  290. {
  291. struct op_msr *counters = msrs->counters;
  292. struct op_msr *controls = msrs->controls;
  293. unsigned int i;
  294. for (i = 0; i < model->num_controls; ++i) {
  295. if (controls[i].addr)
  296. wrmsrl(controls[i].addr, controls[i].saved);
  297. }
  298. for (i = 0; i < model->num_counters; ++i) {
  299. if (counters[i].addr)
  300. wrmsrl(counters[i].addr, counters[i].saved);
  301. }
  302. }
  303. static void nmi_cpu_shutdown(void *dummy)
  304. {
  305. unsigned int v;
  306. int cpu = smp_processor_id();
  307. struct op_msrs *msrs = &per_cpu(cpu_msrs, cpu);
  308. /* restoring APIC_LVTPC can trigger an apic error because the delivery
  309. * mode and vector nr combination can be illegal. That's by design: on
  310. * power on apic lvt contain a zero vector nr which are legal only for
  311. * NMI delivery mode. So inhibit apic err before restoring lvtpc
  312. */
  313. v = apic_read(APIC_LVTERR);
  314. apic_write(APIC_LVTERR, v | APIC_LVT_MASKED);
  315. apic_write(APIC_LVTPC, per_cpu(saved_lvtpc, cpu));
  316. apic_write(APIC_LVTERR, v);
  317. nmi_cpu_restore_registers(msrs);
  318. }
  319. static void nmi_shutdown(void)
  320. {
  321. struct op_msrs *msrs;
  322. nmi_enabled = 0;
  323. on_each_cpu(nmi_cpu_shutdown, NULL, 1);
  324. unregister_die_notifier(&profile_exceptions_nb);
  325. nmi_shutdown_mux();
  326. msrs = &get_cpu_var(cpu_msrs);
  327. model->shutdown(msrs);
  328. free_msrs();
  329. put_cpu_var(cpu_msrs);
  330. }
  331. static int nmi_create_files(struct super_block *sb, struct dentry *root)
  332. {
  333. unsigned int i;
  334. for (i = 0; i < model->num_virt_counters; ++i) {
  335. struct dentry *dir;
  336. char buf[4];
  337. #ifndef CONFIG_OPROFILE_EVENT_MULTIPLEX
  338. /* quick little hack to _not_ expose a counter if it is not
  339. * available for use. This should protect userspace app.
  340. * NOTE: assumes 1:1 mapping here (that counters are organized
  341. * sequentially in their struct assignment).
  342. */
  343. if (unlikely(!avail_to_resrv_perfctr_nmi_bit(i)))
  344. continue;
  345. #endif /* CONFIG_OPROFILE_EVENT_MULTIPLEX */
  346. snprintf(buf, sizeof(buf), "%d", i);
  347. dir = oprofilefs_mkdir(sb, root, buf);
  348. oprofilefs_create_ulong(sb, dir, "enabled", &counter_config[i].enabled);
  349. oprofilefs_create_ulong(sb, dir, "event", &counter_config[i].event);
  350. oprofilefs_create_ulong(sb, dir, "count", &counter_config[i].count);
  351. oprofilefs_create_ulong(sb, dir, "unit_mask", &counter_config[i].unit_mask);
  352. oprofilefs_create_ulong(sb, dir, "kernel", &counter_config[i].kernel);
  353. oprofilefs_create_ulong(sb, dir, "user", &counter_config[i].user);
  354. }
  355. return 0;
  356. }
  357. #ifdef CONFIG_SMP
  358. static int oprofile_cpu_notifier(struct notifier_block *b, unsigned long action,
  359. void *data)
  360. {
  361. int cpu = (unsigned long)data;
  362. switch (action) {
  363. case CPU_DOWN_FAILED:
  364. case CPU_ONLINE:
  365. smp_call_function_single(cpu, nmi_cpu_start, NULL, 0);
  366. break;
  367. case CPU_DOWN_PREPARE:
  368. smp_call_function_single(cpu, nmi_cpu_stop, NULL, 1);
  369. break;
  370. }
  371. return NOTIFY_DONE;
  372. }
  373. static struct notifier_block oprofile_cpu_nb = {
  374. .notifier_call = oprofile_cpu_notifier
  375. };
  376. #endif
  377. #ifdef CONFIG_PM
  378. static int nmi_suspend(struct sys_device *dev, pm_message_t state)
  379. {
  380. /* Only one CPU left, just stop that one */
  381. if (nmi_enabled == 1)
  382. nmi_cpu_stop(NULL);
  383. return 0;
  384. }
  385. static int nmi_resume(struct sys_device *dev)
  386. {
  387. if (nmi_enabled == 1)
  388. nmi_cpu_start(NULL);
  389. return 0;
  390. }
  391. static struct sysdev_class oprofile_sysclass = {
  392. .name = "oprofile",
  393. .resume = nmi_resume,
  394. .suspend = nmi_suspend,
  395. };
  396. static struct sys_device device_oprofile = {
  397. .id = 0,
  398. .cls = &oprofile_sysclass,
  399. };
  400. static int __init init_sysfs(void)
  401. {
  402. int error;
  403. error = sysdev_class_register(&oprofile_sysclass);
  404. if (!error)
  405. error = sysdev_register(&device_oprofile);
  406. return error;
  407. }
  408. static void exit_sysfs(void)
  409. {
  410. sysdev_unregister(&device_oprofile);
  411. sysdev_class_unregister(&oprofile_sysclass);
  412. }
  413. #else
  414. #define init_sysfs() do { } while (0)
  415. #define exit_sysfs() do { } while (0)
  416. #endif /* CONFIG_PM */
  417. static int __init p4_init(char **cpu_type)
  418. {
  419. __u8 cpu_model = boot_cpu_data.x86_model;
  420. if (cpu_model > 6 || cpu_model == 5)
  421. return 0;
  422. #ifndef CONFIG_SMP
  423. *cpu_type = "i386/p4";
  424. model = &op_p4_spec;
  425. return 1;
  426. #else
  427. switch (smp_num_siblings) {
  428. case 1:
  429. *cpu_type = "i386/p4";
  430. model = &op_p4_spec;
  431. return 1;
  432. case 2:
  433. *cpu_type = "i386/p4-ht";
  434. model = &op_p4_ht2_spec;
  435. return 1;
  436. }
  437. #endif
  438. printk(KERN_INFO "oprofile: P4 HyperThreading detected with > 2 threads\n");
  439. printk(KERN_INFO "oprofile: Reverting to timer mode.\n");
  440. return 0;
  441. }
  442. static int force_arch_perfmon;
  443. static int force_cpu_type(const char *str, struct kernel_param *kp)
  444. {
  445. if (!strcmp(str, "arch_perfmon")) {
  446. force_arch_perfmon = 1;
  447. printk(KERN_INFO "oprofile: forcing architectural perfmon\n");
  448. }
  449. return 0;
  450. }
  451. module_param_call(cpu_type, force_cpu_type, NULL, NULL, 0);
  452. static int __init ppro_init(char **cpu_type)
  453. {
  454. __u8 cpu_model = boot_cpu_data.x86_model;
  455. struct op_x86_model_spec *spec = &op_ppro_spec; /* default */
  456. if (force_arch_perfmon && cpu_has_arch_perfmon)
  457. return 0;
  458. switch (cpu_model) {
  459. case 0 ... 2:
  460. *cpu_type = "i386/ppro";
  461. break;
  462. case 3 ... 5:
  463. *cpu_type = "i386/pii";
  464. break;
  465. case 6 ... 8:
  466. case 10 ... 11:
  467. *cpu_type = "i386/piii";
  468. break;
  469. case 9:
  470. case 13:
  471. *cpu_type = "i386/p6_mobile";
  472. break;
  473. case 14:
  474. *cpu_type = "i386/core";
  475. break;
  476. case 15: case 23:
  477. *cpu_type = "i386/core_2";
  478. break;
  479. case 26:
  480. spec = &op_arch_perfmon_spec;
  481. *cpu_type = "i386/core_i7";
  482. break;
  483. case 28:
  484. *cpu_type = "i386/atom";
  485. break;
  486. default:
  487. /* Unknown */
  488. return 0;
  489. }
  490. model = spec;
  491. return 1;
  492. }
  493. /* in order to get sysfs right */
  494. static int using_nmi;
  495. int __init op_nmi_init(struct oprofile_operations *ops)
  496. {
  497. __u8 vendor = boot_cpu_data.x86_vendor;
  498. __u8 family = boot_cpu_data.x86;
  499. char *cpu_type = NULL;
  500. int ret = 0;
  501. if (!cpu_has_apic)
  502. return -ENODEV;
  503. switch (vendor) {
  504. case X86_VENDOR_AMD:
  505. /* Needs to be at least an Athlon (or hammer in 32bit mode) */
  506. switch (family) {
  507. case 6:
  508. cpu_type = "i386/athlon";
  509. break;
  510. case 0xf:
  511. /*
  512. * Actually it could be i386/hammer too, but
  513. * give user space an consistent name.
  514. */
  515. cpu_type = "x86-64/hammer";
  516. break;
  517. case 0x10:
  518. cpu_type = "x86-64/family10";
  519. break;
  520. case 0x11:
  521. cpu_type = "x86-64/family11h";
  522. break;
  523. default:
  524. return -ENODEV;
  525. }
  526. model = &op_amd_spec;
  527. break;
  528. case X86_VENDOR_INTEL:
  529. switch (family) {
  530. /* Pentium IV */
  531. case 0xf:
  532. p4_init(&cpu_type);
  533. break;
  534. /* A P6-class processor */
  535. case 6:
  536. ppro_init(&cpu_type);
  537. break;
  538. default:
  539. break;
  540. }
  541. if (cpu_type)
  542. break;
  543. if (!cpu_has_arch_perfmon)
  544. return -ENODEV;
  545. /* use arch perfmon as fallback */
  546. cpu_type = "i386/arch_perfmon";
  547. model = &op_arch_perfmon_spec;
  548. break;
  549. default:
  550. return -ENODEV;
  551. }
  552. #ifdef CONFIG_SMP
  553. register_cpu_notifier(&oprofile_cpu_nb);
  554. #endif
  555. /* default values, can be overwritten by model */
  556. ops->create_files = nmi_create_files;
  557. ops->setup = nmi_setup;
  558. ops->shutdown = nmi_shutdown;
  559. ops->start = nmi_start;
  560. ops->stop = nmi_stop;
  561. ops->cpu_type = cpu_type;
  562. #ifdef CONFIG_OPROFILE_EVENT_MULTIPLEX
  563. ops->switch_events = nmi_switch_event;
  564. #endif
  565. if (model->init)
  566. ret = model->init(ops);
  567. if (ret)
  568. return ret;
  569. init_sysfs();
  570. using_nmi = 1;
  571. printk(KERN_INFO "oprofile: using NMI interrupt.\n");
  572. return 0;
  573. }
  574. void op_nmi_exit(void)
  575. {
  576. if (using_nmi) {
  577. exit_sysfs();
  578. #ifdef CONFIG_SMP
  579. unregister_cpu_notifier(&oprofile_cpu_nb);
  580. #endif
  581. }
  582. if (model->exit)
  583. model->exit();
  584. }