nmi_int.c 8.6 KB

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