nmi_int.c 10 KB

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