nmi_int.c 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461
  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 struct op_msrs cpu_msrs[NR_CPUS];
  24. static unsigned long saved_lvtpc[NR_CPUS];
  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, &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 = &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(cpu_msrs[i].counters);
  117. cpu_msrs[i].counters = NULL;
  118. kfree(cpu_msrs[i].controls);
  119. 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. cpu_msrs[i].counters = kmalloc(counters_size, GFP_KERNEL);
  130. if (!cpu_msrs[i].counters) {
  131. success = 0;
  132. break;
  133. }
  134. cpu_msrs[i].controls = kmalloc(controls_size, GFP_KERNEL);
  135. if (!cpu_msrs[i].controls) {
  136. success = 0;
  137. break;
  138. }
  139. }
  140. if (!success)
  141. free_msrs();
  142. return success;
  143. }
  144. static void nmi_cpu_setup(void *dummy)
  145. {
  146. int cpu = smp_processor_id();
  147. struct op_msrs *msrs = &cpu_msrs[cpu];
  148. spin_lock(&oprofilefs_lock);
  149. model->setup_ctrs(msrs);
  150. spin_unlock(&oprofilefs_lock);
  151. saved_lvtpc[cpu] = apic_read(APIC_LVTPC);
  152. apic_write(APIC_LVTPC, APIC_DM_NMI);
  153. }
  154. static struct notifier_block profile_exceptions_nb = {
  155. .notifier_call = profile_exceptions_notify,
  156. .next = NULL,
  157. .priority = 0
  158. };
  159. static int nmi_setup(void)
  160. {
  161. int err = 0;
  162. int cpu;
  163. if (!allocate_msrs())
  164. return -ENOMEM;
  165. err = register_die_notifier(&profile_exceptions_nb);
  166. if (err) {
  167. free_msrs();
  168. return err;
  169. }
  170. /* We need to serialize save and setup for HT because the subset
  171. * of msrs are distinct for save and setup operations
  172. */
  173. /* Assume saved/restored counters are the same on all CPUs */
  174. model->fill_in_addresses(&cpu_msrs[0]);
  175. for_each_possible_cpu(cpu) {
  176. if (cpu != 0) {
  177. memcpy(cpu_msrs[cpu].counters, cpu_msrs[0].counters,
  178. sizeof(struct op_msr) * model->num_counters);
  179. memcpy(cpu_msrs[cpu].controls, cpu_msrs[0].controls,
  180. sizeof(struct op_msr) * model->num_controls);
  181. }
  182. }
  183. on_each_cpu(nmi_save_registers, NULL, 0, 1);
  184. on_each_cpu(nmi_cpu_setup, NULL, 0, 1);
  185. nmi_enabled = 1;
  186. return 0;
  187. }
  188. static void nmi_restore_registers(struct op_msrs *msrs)
  189. {
  190. unsigned int const nr_ctrs = model->num_counters;
  191. unsigned int const nr_ctrls = model->num_controls;
  192. struct op_msr *counters = msrs->counters;
  193. struct op_msr *controls = msrs->controls;
  194. unsigned int i;
  195. for (i = 0; i < nr_ctrls; ++i) {
  196. if (controls[i].addr) {
  197. wrmsr(controls[i].addr,
  198. controls[i].saved.low,
  199. controls[i].saved.high);
  200. }
  201. }
  202. for (i = 0; i < nr_ctrs; ++i) {
  203. if (counters[i].addr) {
  204. wrmsr(counters[i].addr,
  205. counters[i].saved.low,
  206. counters[i].saved.high);
  207. }
  208. }
  209. }
  210. static void nmi_cpu_shutdown(void *dummy)
  211. {
  212. unsigned int v;
  213. int cpu = smp_processor_id();
  214. struct op_msrs *msrs = &cpu_msrs[cpu];
  215. /* restoring APIC_LVTPC can trigger an apic error because the delivery
  216. * mode and vector nr combination can be illegal. That's by design: on
  217. * power on apic lvt contain a zero vector nr which are legal only for
  218. * NMI delivery mode. So inhibit apic err before restoring lvtpc
  219. */
  220. v = apic_read(APIC_LVTERR);
  221. apic_write(APIC_LVTERR, v | APIC_LVT_MASKED);
  222. apic_write(APIC_LVTPC, saved_lvtpc[cpu]);
  223. apic_write(APIC_LVTERR, v);
  224. nmi_restore_registers(msrs);
  225. }
  226. static void nmi_shutdown(void)
  227. {
  228. nmi_enabled = 0;
  229. on_each_cpu(nmi_cpu_shutdown, NULL, 0, 1);
  230. unregister_die_notifier(&profile_exceptions_nb);
  231. model->shutdown(cpu_msrs);
  232. free_msrs();
  233. }
  234. static void nmi_cpu_start(void *dummy)
  235. {
  236. struct op_msrs const *msrs = &cpu_msrs[smp_processor_id()];
  237. model->start(msrs);
  238. }
  239. static int nmi_start(void)
  240. {
  241. on_each_cpu(nmi_cpu_start, NULL, 0, 1);
  242. return 0;
  243. }
  244. static void nmi_cpu_stop(void *dummy)
  245. {
  246. struct op_msrs const *msrs = &cpu_msrs[smp_processor_id()];
  247. model->stop(msrs);
  248. }
  249. static void nmi_stop(void)
  250. {
  251. on_each_cpu(nmi_cpu_stop, NULL, 0, 1);
  252. }
  253. struct op_counter_config counter_config[OP_MAX_COUNTER];
  254. static int nmi_create_files(struct super_block *sb, struct dentry *root)
  255. {
  256. unsigned int i;
  257. for (i = 0; i < model->num_counters; ++i) {
  258. struct dentry *dir;
  259. char buf[4];
  260. /* quick little hack to _not_ expose a counter if it is not
  261. * available for use. This should protect userspace app.
  262. * NOTE: assumes 1:1 mapping here (that counters are organized
  263. * sequentially in their struct assignment).
  264. */
  265. if (unlikely(!avail_to_resrv_perfctr_nmi_bit(i)))
  266. continue;
  267. snprintf(buf, sizeof(buf), "%d", i);
  268. dir = oprofilefs_mkdir(sb, root, buf);
  269. oprofilefs_create_ulong(sb, dir, "enabled", &counter_config[i].enabled);
  270. oprofilefs_create_ulong(sb, dir, "event", &counter_config[i].event);
  271. oprofilefs_create_ulong(sb, dir, "count", &counter_config[i].count);
  272. oprofilefs_create_ulong(sb, dir, "unit_mask", &counter_config[i].unit_mask);
  273. oprofilefs_create_ulong(sb, dir, "kernel", &counter_config[i].kernel);
  274. oprofilefs_create_ulong(sb, dir, "user", &counter_config[i].user);
  275. }
  276. return 0;
  277. }
  278. static int p4force;
  279. module_param(p4force, int, 0);
  280. static int __init p4_init(char **cpu_type)
  281. {
  282. __u8 cpu_model = boot_cpu_data.x86_model;
  283. if (!p4force && (cpu_model > 6 || cpu_model == 5))
  284. return 0;
  285. #ifndef CONFIG_SMP
  286. *cpu_type = "i386/p4";
  287. model = &op_p4_spec;
  288. return 1;
  289. #else
  290. switch (smp_num_siblings) {
  291. case 1:
  292. *cpu_type = "i386/p4";
  293. model = &op_p4_spec;
  294. return 1;
  295. case 2:
  296. *cpu_type = "i386/p4-ht";
  297. model = &op_p4_ht2_spec;
  298. return 1;
  299. }
  300. #endif
  301. printk(KERN_INFO "oprofile: P4 HyperThreading detected with > 2 threads\n");
  302. printk(KERN_INFO "oprofile: Reverting to timer mode.\n");
  303. return 0;
  304. }
  305. static int __init ppro_init(char **cpu_type)
  306. {
  307. __u8 cpu_model = boot_cpu_data.x86_model;
  308. if (cpu_model == 14)
  309. *cpu_type = "i386/core";
  310. else if (cpu_model == 15 || cpu_model == 23)
  311. *cpu_type = "i386/core_2";
  312. else if (cpu_model > 0xd)
  313. return 0;
  314. else if (cpu_model == 9) {
  315. *cpu_type = "i386/p6_mobile";
  316. } else if (cpu_model > 5) {
  317. *cpu_type = "i386/piii";
  318. } else if (cpu_model > 2) {
  319. *cpu_type = "i386/pii";
  320. } else {
  321. *cpu_type = "i386/ppro";
  322. }
  323. model = &op_ppro_spec;
  324. return 1;
  325. }
  326. /* in order to get sysfs right */
  327. static int using_nmi;
  328. int __init op_nmi_init(struct oprofile_operations *ops)
  329. {
  330. __u8 vendor = boot_cpu_data.x86_vendor;
  331. __u8 family = boot_cpu_data.x86;
  332. char *cpu_type;
  333. if (!cpu_has_apic)
  334. return -ENODEV;
  335. switch (vendor) {
  336. case X86_VENDOR_AMD:
  337. /* Needs to be at least an Athlon (or hammer in 32bit mode) */
  338. switch (family) {
  339. default:
  340. return -ENODEV;
  341. case 6:
  342. model = &op_athlon_spec;
  343. cpu_type = "i386/athlon";
  344. break;
  345. case 0xf:
  346. model = &op_athlon_spec;
  347. /* Actually it could be i386/hammer too, but give
  348. user space an consistent name. */
  349. cpu_type = "x86-64/hammer";
  350. break;
  351. case 0x10:
  352. model = &op_athlon_spec;
  353. cpu_type = "x86-64/family10";
  354. break;
  355. }
  356. break;
  357. case X86_VENDOR_INTEL:
  358. switch (family) {
  359. /* Pentium IV */
  360. case 0xf:
  361. if (!p4_init(&cpu_type))
  362. return -ENODEV;
  363. break;
  364. /* A P6-class processor */
  365. case 6:
  366. if (!ppro_init(&cpu_type))
  367. return -ENODEV;
  368. break;
  369. default:
  370. return -ENODEV;
  371. }
  372. break;
  373. default:
  374. return -ENODEV;
  375. }
  376. init_sysfs();
  377. using_nmi = 1;
  378. ops->create_files = nmi_create_files;
  379. ops->setup = nmi_setup;
  380. ops->shutdown = nmi_shutdown;
  381. ops->start = nmi_start;
  382. ops->stop = nmi_stop;
  383. ops->cpu_type = cpu_type;
  384. printk(KERN_INFO "oprofile: using NMI interrupt.\n");
  385. return 0;
  386. }
  387. void op_nmi_exit(void)
  388. {
  389. if (using_nmi)
  390. exit_sysfs();
  391. }