nmi_int.c 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428
  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. rdmsr(counters[i].addr,
  79. counters[i].saved.low,
  80. counters[i].saved.high);
  81. }
  82. for (i = 0; i < nr_ctrls; ++i) {
  83. rdmsr(controls[i].addr,
  84. controls[i].saved.low,
  85. controls[i].saved.high);
  86. }
  87. }
  88. static void nmi_save_registers(void * dummy)
  89. {
  90. int cpu = smp_processor_id();
  91. struct op_msrs * msrs = &cpu_msrs[cpu];
  92. model->fill_in_addresses(msrs);
  93. nmi_cpu_save_registers(msrs);
  94. }
  95. static void free_msrs(void)
  96. {
  97. int i;
  98. for_each_possible_cpu(i) {
  99. kfree(cpu_msrs[i].counters);
  100. cpu_msrs[i].counters = NULL;
  101. kfree(cpu_msrs[i].controls);
  102. cpu_msrs[i].controls = NULL;
  103. }
  104. }
  105. static int allocate_msrs(void)
  106. {
  107. int success = 1;
  108. size_t controls_size = sizeof(struct op_msr) * model->num_controls;
  109. size_t counters_size = sizeof(struct op_msr) * model->num_counters;
  110. int i;
  111. for_each_online_cpu(i) {
  112. cpu_msrs[i].counters = kmalloc(counters_size, GFP_KERNEL);
  113. if (!cpu_msrs[i].counters) {
  114. success = 0;
  115. break;
  116. }
  117. cpu_msrs[i].controls = kmalloc(controls_size, GFP_KERNEL);
  118. if (!cpu_msrs[i].controls) {
  119. success = 0;
  120. break;
  121. }
  122. }
  123. if (!success)
  124. free_msrs();
  125. return success;
  126. }
  127. static void nmi_cpu_setup(void * dummy)
  128. {
  129. int cpu = smp_processor_id();
  130. struct op_msrs * msrs = &cpu_msrs[cpu];
  131. spin_lock(&oprofilefs_lock);
  132. model->setup_ctrs(msrs);
  133. spin_unlock(&oprofilefs_lock);
  134. saved_lvtpc[cpu] = apic_read(APIC_LVTPC);
  135. apic_write(APIC_LVTPC, APIC_DM_NMI);
  136. }
  137. static int nmi_setup(void)
  138. {
  139. if (!allocate_msrs())
  140. return -ENOMEM;
  141. /* We walk a thin line between law and rape here.
  142. * We need to be careful to install our NMI handler
  143. * without actually triggering any NMIs as this will
  144. * break the core code horrifically.
  145. */
  146. if (reserve_lapic_nmi() < 0) {
  147. free_msrs();
  148. return -EBUSY;
  149. }
  150. /* We need to serialize save and setup for HT because the subset
  151. * of msrs are distinct for save and setup operations
  152. */
  153. on_each_cpu(nmi_save_registers, NULL, 0, 1);
  154. on_each_cpu(nmi_cpu_setup, NULL, 0, 1);
  155. set_nmi_callback(nmi_callback);
  156. nmi_enabled = 1;
  157. return 0;
  158. }
  159. static void nmi_restore_registers(struct op_msrs * msrs)
  160. {
  161. unsigned int const nr_ctrs = model->num_counters;
  162. unsigned int const nr_ctrls = model->num_controls;
  163. struct op_msr * counters = msrs->counters;
  164. struct op_msr * controls = msrs->controls;
  165. unsigned int i;
  166. for (i = 0; i < nr_ctrls; ++i) {
  167. wrmsr(controls[i].addr,
  168. controls[i].saved.low,
  169. controls[i].saved.high);
  170. }
  171. for (i = 0; i < nr_ctrs; ++i) {
  172. wrmsr(counters[i].addr,
  173. counters[i].saved.low,
  174. counters[i].saved.high);
  175. }
  176. }
  177. static void nmi_cpu_shutdown(void * dummy)
  178. {
  179. unsigned int v;
  180. int cpu = smp_processor_id();
  181. struct op_msrs * msrs = &cpu_msrs[cpu];
  182. /* restoring APIC_LVTPC can trigger an apic error because the delivery
  183. * mode and vector nr combination can be illegal. That's by design: on
  184. * power on apic lvt contain a zero vector nr which are legal only for
  185. * NMI delivery mode. So inhibit apic err before restoring lvtpc
  186. */
  187. v = apic_read(APIC_LVTERR);
  188. apic_write(APIC_LVTERR, v | APIC_LVT_MASKED);
  189. apic_write(APIC_LVTPC, saved_lvtpc[cpu]);
  190. apic_write(APIC_LVTERR, v);
  191. nmi_restore_registers(msrs);
  192. }
  193. static void nmi_shutdown(void)
  194. {
  195. nmi_enabled = 0;
  196. on_each_cpu(nmi_cpu_shutdown, NULL, 0, 1);
  197. unset_nmi_callback();
  198. release_lapic_nmi();
  199. free_msrs();
  200. }
  201. static void nmi_cpu_start(void * dummy)
  202. {
  203. struct op_msrs const * msrs = &cpu_msrs[smp_processor_id()];
  204. model->start(msrs);
  205. }
  206. static int nmi_start(void)
  207. {
  208. on_each_cpu(nmi_cpu_start, NULL, 0, 1);
  209. return 0;
  210. }
  211. static void nmi_cpu_stop(void * dummy)
  212. {
  213. struct op_msrs const * msrs = &cpu_msrs[smp_processor_id()];
  214. model->stop(msrs);
  215. }
  216. static void nmi_stop(void)
  217. {
  218. on_each_cpu(nmi_cpu_stop, NULL, 0, 1);
  219. }
  220. struct op_counter_config counter_config[OP_MAX_COUNTER];
  221. static int nmi_create_files(struct super_block * sb, struct dentry * root)
  222. {
  223. unsigned int i;
  224. for (i = 0; i < model->num_counters; ++i) {
  225. struct dentry * dir;
  226. char buf[4];
  227. snprintf(buf, sizeof(buf), "%d", i);
  228. dir = oprofilefs_mkdir(sb, root, buf);
  229. oprofilefs_create_ulong(sb, dir, "enabled", &counter_config[i].enabled);
  230. oprofilefs_create_ulong(sb, dir, "event", &counter_config[i].event);
  231. oprofilefs_create_ulong(sb, dir, "count", &counter_config[i].count);
  232. oprofilefs_create_ulong(sb, dir, "unit_mask", &counter_config[i].unit_mask);
  233. oprofilefs_create_ulong(sb, dir, "kernel", &counter_config[i].kernel);
  234. oprofilefs_create_ulong(sb, dir, "user", &counter_config[i].user);
  235. }
  236. return 0;
  237. }
  238. static int p4force;
  239. module_param(p4force, int, 0);
  240. static int __init p4_init(char ** cpu_type)
  241. {
  242. __u8 cpu_model = boot_cpu_data.x86_model;
  243. if (!p4force && (cpu_model > 6 || cpu_model == 5))
  244. return 0;
  245. #ifndef CONFIG_SMP
  246. *cpu_type = "i386/p4";
  247. model = &op_p4_spec;
  248. return 1;
  249. #else
  250. switch (smp_num_siblings) {
  251. case 1:
  252. *cpu_type = "i386/p4";
  253. model = &op_p4_spec;
  254. return 1;
  255. case 2:
  256. *cpu_type = "i386/p4-ht";
  257. model = &op_p4_ht2_spec;
  258. return 1;
  259. }
  260. #endif
  261. printk(KERN_INFO "oprofile: P4 HyperThreading detected with > 2 threads\n");
  262. printk(KERN_INFO "oprofile: Reverting to timer mode.\n");
  263. return 0;
  264. }
  265. static int __init ppro_init(char ** cpu_type)
  266. {
  267. __u8 cpu_model = boot_cpu_data.x86_model;
  268. if (cpu_model == 14)
  269. *cpu_type = "i386/core";
  270. else if (cpu_model > 0xd)
  271. return 0;
  272. else if (cpu_model == 9) {
  273. *cpu_type = "i386/p6_mobile";
  274. } else if (cpu_model > 5) {
  275. *cpu_type = "i386/piii";
  276. } else if (cpu_model > 2) {
  277. *cpu_type = "i386/pii";
  278. } else {
  279. *cpu_type = "i386/ppro";
  280. }
  281. model = &op_ppro_spec;
  282. return 1;
  283. }
  284. /* in order to get driverfs right */
  285. static int using_nmi;
  286. int __init op_nmi_init(struct oprofile_operations *ops)
  287. {
  288. __u8 vendor = boot_cpu_data.x86_vendor;
  289. __u8 family = boot_cpu_data.x86;
  290. char *cpu_type;
  291. if (!cpu_has_apic)
  292. return -ENODEV;
  293. switch (vendor) {
  294. case X86_VENDOR_AMD:
  295. /* Needs to be at least an Athlon (or hammer in 32bit mode) */
  296. switch (family) {
  297. default:
  298. return -ENODEV;
  299. case 6:
  300. model = &op_athlon_spec;
  301. cpu_type = "i386/athlon";
  302. break;
  303. case 0xf:
  304. model = &op_athlon_spec;
  305. /* Actually it could be i386/hammer too, but give
  306. user space an consistent name. */
  307. cpu_type = "x86-64/hammer";
  308. break;
  309. }
  310. break;
  311. case X86_VENDOR_INTEL:
  312. switch (family) {
  313. /* Pentium IV */
  314. case 0xf:
  315. if (!p4_init(&cpu_type))
  316. return -ENODEV;
  317. break;
  318. /* A P6-class processor */
  319. case 6:
  320. if (!ppro_init(&cpu_type))
  321. return -ENODEV;
  322. break;
  323. default:
  324. return -ENODEV;
  325. }
  326. break;
  327. default:
  328. return -ENODEV;
  329. }
  330. init_driverfs();
  331. using_nmi = 1;
  332. ops->create_files = nmi_create_files;
  333. ops->setup = nmi_setup;
  334. ops->shutdown = nmi_shutdown;
  335. ops->start = nmi_start;
  336. ops->stop = nmi_stop;
  337. ops->cpu_type = cpu_type;
  338. printk(KERN_INFO "oprofile: using NMI interrupt.\n");
  339. return 0;
  340. }
  341. void op_nmi_exit(void)
  342. {
  343. if (using_nmi)
  344. exit_driverfs();
  345. }