nmi_int.c 11 KB

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