nmi_int.c 9.2 KB

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