nmi_int.c 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460
  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 <asm/kdebug.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. set_kset_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_driverfs(void)
  52. {
  53. int error;
  54. if (!(error = sysdev_class_register(&oprofile_sysclass)))
  55. error = sysdev_register(&device_oprofile);
  56. return error;
  57. }
  58. static void exit_driverfs(void)
  59. {
  60. sysdev_unregister(&device_oprofile);
  61. sysdev_class_unregister(&oprofile_sysclass);
  62. }
  63. #else
  64. #define init_driverfs() do { } while (0)
  65. #define exit_driverfs() do { } while (0)
  66. #endif /* CONFIG_PM */
  67. static int profile_exceptions_notify(struct notifier_block *self,
  68. unsigned long val, void *data)
  69. {
  70. struct die_args *args = (struct die_args *)data;
  71. int ret = NOTIFY_DONE;
  72. int cpu = smp_processor_id();
  73. switch(val) {
  74. case DIE_NMI:
  75. if (model->check_ctrs(args->regs, &cpu_msrs[cpu]))
  76. ret = NOTIFY_STOP;
  77. break;
  78. default:
  79. break;
  80. }
  81. return ret;
  82. }
  83. static void nmi_cpu_save_registers(struct op_msrs * msrs)
  84. {
  85. unsigned int const nr_ctrs = model->num_counters;
  86. unsigned int const nr_ctrls = model->num_controls;
  87. struct op_msr * counters = msrs->counters;
  88. struct op_msr * controls = msrs->controls;
  89. unsigned int i;
  90. for (i = 0; i < nr_ctrs; ++i) {
  91. if (counters[i].addr){
  92. rdmsr(counters[i].addr,
  93. counters[i].saved.low,
  94. counters[i].saved.high);
  95. }
  96. }
  97. for (i = 0; i < nr_ctrls; ++i) {
  98. if (controls[i].addr){
  99. rdmsr(controls[i].addr,
  100. controls[i].saved.low,
  101. controls[i].saved.high);
  102. }
  103. }
  104. }
  105. static void nmi_save_registers(void * dummy)
  106. {
  107. int cpu = smp_processor_id();
  108. struct op_msrs * msrs = &cpu_msrs[cpu];
  109. model->fill_in_addresses(msrs);
  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_online_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. if (!allocate_msrs())
  163. return -ENOMEM;
  164. if ((err = register_die_notifier(&profile_exceptions_nb))){
  165. free_msrs();
  166. return err;
  167. }
  168. /* We need to serialize save and setup for HT because the subset
  169. * of msrs are distinct for save and setup operations
  170. */
  171. on_each_cpu(nmi_save_registers, NULL, 0, 1);
  172. on_each_cpu(nmi_cpu_setup, NULL, 0, 1);
  173. nmi_enabled = 1;
  174. return 0;
  175. }
  176. static void nmi_restore_registers(struct op_msrs * msrs)
  177. {
  178. unsigned int const nr_ctrs = model->num_counters;
  179. unsigned int const nr_ctrls = model->num_controls;
  180. struct op_msr * counters = msrs->counters;
  181. struct op_msr * controls = msrs->controls;
  182. unsigned int i;
  183. for (i = 0; i < nr_ctrls; ++i) {
  184. if (controls[i].addr){
  185. wrmsr(controls[i].addr,
  186. controls[i].saved.low,
  187. controls[i].saved.high);
  188. }
  189. }
  190. for (i = 0; i < nr_ctrs; ++i) {
  191. if (counters[i].addr){
  192. wrmsr(counters[i].addr,
  193. counters[i].saved.low,
  194. counters[i].saved.high);
  195. }
  196. }
  197. }
  198. static void nmi_cpu_shutdown(void * dummy)
  199. {
  200. unsigned int v;
  201. int cpu = smp_processor_id();
  202. struct op_msrs * msrs = &cpu_msrs[cpu];
  203. /* restoring APIC_LVTPC can trigger an apic error because the delivery
  204. * mode and vector nr combination can be illegal. That's by design: on
  205. * power on apic lvt contain a zero vector nr which are legal only for
  206. * NMI delivery mode. So inhibit apic err before restoring lvtpc
  207. */
  208. v = apic_read(APIC_LVTERR);
  209. apic_write(APIC_LVTERR, v | APIC_LVT_MASKED);
  210. apic_write(APIC_LVTPC, saved_lvtpc[cpu]);
  211. apic_write(APIC_LVTERR, v);
  212. nmi_restore_registers(msrs);
  213. model->shutdown(msrs);
  214. }
  215. static void nmi_shutdown(void)
  216. {
  217. nmi_enabled = 0;
  218. on_each_cpu(nmi_cpu_shutdown, NULL, 0, 1);
  219. unregister_die_notifier(&profile_exceptions_nb);
  220. free_msrs();
  221. }
  222. static void nmi_cpu_start(void * dummy)
  223. {
  224. struct op_msrs const * msrs = &cpu_msrs[smp_processor_id()];
  225. model->start(msrs);
  226. }
  227. static int nmi_start(void)
  228. {
  229. on_each_cpu(nmi_cpu_start, NULL, 0, 1);
  230. return 0;
  231. }
  232. static void nmi_cpu_stop(void * dummy)
  233. {
  234. struct op_msrs const * msrs = &cpu_msrs[smp_processor_id()];
  235. model->stop(msrs);
  236. }
  237. static void nmi_stop(void)
  238. {
  239. on_each_cpu(nmi_cpu_stop, NULL, 0, 1);
  240. }
  241. struct op_counter_config counter_config[OP_MAX_COUNTER];
  242. static int nmi_create_files(struct super_block * sb, struct dentry * root)
  243. {
  244. unsigned int i;
  245. for (i = 0; i < model->num_counters; ++i) {
  246. struct dentry * dir;
  247. char buf[4];
  248. /* quick little hack to _not_ expose a counter if it is not
  249. * available for use. This should protect userspace app.
  250. * NOTE: assumes 1:1 mapping here (that counters are organized
  251. * sequentially in their struct assignment).
  252. */
  253. if (unlikely(!avail_to_resrv_perfctr_nmi_bit(i)))
  254. continue;
  255. snprintf(buf, sizeof(buf), "%d", i);
  256. dir = oprofilefs_mkdir(sb, root, buf);
  257. oprofilefs_create_ulong(sb, dir, "enabled", &counter_config[i].enabled);
  258. oprofilefs_create_ulong(sb, dir, "event", &counter_config[i].event);
  259. oprofilefs_create_ulong(sb, dir, "count", &counter_config[i].count);
  260. oprofilefs_create_ulong(sb, dir, "unit_mask", &counter_config[i].unit_mask);
  261. oprofilefs_create_ulong(sb, dir, "kernel", &counter_config[i].kernel);
  262. oprofilefs_create_ulong(sb, dir, "user", &counter_config[i].user);
  263. }
  264. return 0;
  265. }
  266. static int p4force;
  267. module_param(p4force, int, 0);
  268. static int __init p4_init(char ** cpu_type)
  269. {
  270. __u8 cpu_model = boot_cpu_data.x86_model;
  271. if (!p4force && (cpu_model > 6 || cpu_model == 5))
  272. return 0;
  273. #ifndef CONFIG_SMP
  274. *cpu_type = "i386/p4";
  275. model = &op_p4_spec;
  276. return 1;
  277. #else
  278. switch (smp_num_siblings) {
  279. case 1:
  280. *cpu_type = "i386/p4";
  281. model = &op_p4_spec;
  282. return 1;
  283. case 2:
  284. *cpu_type = "i386/p4-ht";
  285. model = &op_p4_ht2_spec;
  286. return 1;
  287. }
  288. #endif
  289. printk(KERN_INFO "oprofile: P4 HyperThreading detected with > 2 threads\n");
  290. printk(KERN_INFO "oprofile: Reverting to timer mode.\n");
  291. return 0;
  292. }
  293. static int __init ppro_init(char ** cpu_type)
  294. {
  295. __u8 cpu_model = boot_cpu_data.x86_model;
  296. if (cpu_model == 14)
  297. *cpu_type = "i386/core";
  298. else if (cpu_model == 15)
  299. *cpu_type = "i386/core_2";
  300. else if (cpu_model > 0xd)
  301. return 0;
  302. else if (cpu_model == 9) {
  303. *cpu_type = "i386/p6_mobile";
  304. } else if (cpu_model > 5) {
  305. *cpu_type = "i386/piii";
  306. } else if (cpu_model > 2) {
  307. *cpu_type = "i386/pii";
  308. } else {
  309. *cpu_type = "i386/ppro";
  310. }
  311. model = &op_ppro_spec;
  312. return 1;
  313. }
  314. /* in order to get driverfs right */
  315. static int using_nmi;
  316. int __init op_nmi_init(struct oprofile_operations *ops)
  317. {
  318. __u8 vendor = boot_cpu_data.x86_vendor;
  319. __u8 family = boot_cpu_data.x86;
  320. char *cpu_type;
  321. if (!cpu_has_apic)
  322. return -ENODEV;
  323. switch (vendor) {
  324. case X86_VENDOR_AMD:
  325. /* Needs to be at least an Athlon (or hammer in 32bit mode) */
  326. switch (family) {
  327. default:
  328. return -ENODEV;
  329. case 6:
  330. model = &op_athlon_spec;
  331. cpu_type = "i386/athlon";
  332. break;
  333. case 0xf:
  334. model = &op_athlon_spec;
  335. /* Actually it could be i386/hammer too, but give
  336. user space an consistent name. */
  337. cpu_type = "x86-64/hammer";
  338. break;
  339. }
  340. break;
  341. case X86_VENDOR_INTEL:
  342. switch (family) {
  343. /* Pentium IV */
  344. case 0xf:
  345. if (!p4_init(&cpu_type))
  346. return -ENODEV;
  347. break;
  348. /* A P6-class processor */
  349. case 6:
  350. if (!ppro_init(&cpu_type))
  351. return -ENODEV;
  352. break;
  353. default:
  354. return -ENODEV;
  355. }
  356. break;
  357. default:
  358. return -ENODEV;
  359. }
  360. init_driverfs();
  361. using_nmi = 1;
  362. ops->create_files = nmi_create_files;
  363. ops->setup = nmi_setup;
  364. ops->shutdown = nmi_shutdown;
  365. ops->start = nmi_start;
  366. ops->stop = nmi_stop;
  367. ops->cpu_type = cpu_type;
  368. printk(KERN_INFO "oprofile: using NMI interrupt.\n");
  369. return 0;
  370. }
  371. void op_nmi_exit(void)
  372. {
  373. if (using_nmi)
  374. exit_driverfs();
  375. }