nmi_int.c 10 KB

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