nmi_int.c 8.6 KB

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