nmi_int.c 12 KB

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