nmi_int.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516
  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 <linux/kdebug.h>
  17. #include <linux/cpu.h>
  18. #include <asm/nmi.h>
  19. #include <asm/msr.h>
  20. #include <asm/apic.h>
  21. #include "op_counter.h"
  22. #include "op_x86_model.h"
  23. static struct op_x86_model_spec const *model;
  24. static DEFINE_PER_CPU(struct op_msrs, cpu_msrs);
  25. static DEFINE_PER_CPU(unsigned long, saved_lvtpc);
  26. static int nmi_start(void);
  27. static void nmi_stop(void);
  28. static void nmi_cpu_start(void *dummy);
  29. static void nmi_cpu_stop(void *dummy);
  30. /* 0 == registered but off, 1 == registered and on */
  31. static int nmi_enabled = 0;
  32. #ifdef CONFIG_SMP
  33. static int oprofile_cpu_notifier(struct notifier_block *b, unsigned long action,
  34. void *data)
  35. {
  36. int cpu = (unsigned long)data;
  37. switch (action) {
  38. case CPU_DOWN_FAILED:
  39. case CPU_ONLINE:
  40. smp_call_function_single(cpu, nmi_cpu_start, NULL, 0);
  41. break;
  42. case CPU_DOWN_PREPARE:
  43. smp_call_function_single(cpu, nmi_cpu_stop, NULL, 1);
  44. break;
  45. }
  46. return NOTIFY_DONE;
  47. }
  48. static struct notifier_block oprofile_cpu_nb = {
  49. .notifier_call = oprofile_cpu_notifier
  50. };
  51. #endif
  52. #ifdef CONFIG_PM
  53. static int nmi_suspend(struct sys_device *dev, pm_message_t state)
  54. {
  55. /* Only one CPU left, just stop that one */
  56. if (nmi_enabled == 1)
  57. nmi_cpu_stop(NULL);
  58. return 0;
  59. }
  60. static int nmi_resume(struct sys_device *dev)
  61. {
  62. if (nmi_enabled == 1)
  63. nmi_cpu_start(NULL);
  64. return 0;
  65. }
  66. static struct sysdev_class oprofile_sysclass = {
  67. .name = "oprofile",
  68. .resume = nmi_resume,
  69. .suspend = nmi_suspend,
  70. };
  71. static struct sys_device device_oprofile = {
  72. .id = 0,
  73. .cls = &oprofile_sysclass,
  74. };
  75. static int __init init_sysfs(void)
  76. {
  77. int error;
  78. error = sysdev_class_register(&oprofile_sysclass);
  79. if (!error)
  80. error = sysdev_register(&device_oprofile);
  81. return error;
  82. }
  83. static void exit_sysfs(void)
  84. {
  85. sysdev_unregister(&device_oprofile);
  86. sysdev_class_unregister(&oprofile_sysclass);
  87. }
  88. #else
  89. #define init_sysfs() do { } while (0)
  90. #define exit_sysfs() do { } while (0)
  91. #endif /* CONFIG_PM */
  92. static int profile_exceptions_notify(struct notifier_block *self,
  93. unsigned long val, void *data)
  94. {
  95. struct die_args *args = (struct die_args *)data;
  96. int ret = NOTIFY_DONE;
  97. int cpu = smp_processor_id();
  98. switch (val) {
  99. case DIE_NMI:
  100. if (model->check_ctrs(args->regs, &per_cpu(cpu_msrs, cpu)))
  101. ret = NOTIFY_STOP;
  102. break;
  103. default:
  104. break;
  105. }
  106. return ret;
  107. }
  108. static void nmi_cpu_save_registers(struct op_msrs *msrs)
  109. {
  110. unsigned int const nr_ctrs = model->num_counters;
  111. unsigned int const nr_ctrls = model->num_controls;
  112. struct op_msr *counters = msrs->counters;
  113. struct op_msr *controls = msrs->controls;
  114. unsigned int i;
  115. for (i = 0; i < nr_ctrs; ++i) {
  116. if (counters[i].addr) {
  117. rdmsr(counters[i].addr,
  118. counters[i].saved.low,
  119. counters[i].saved.high);
  120. }
  121. }
  122. for (i = 0; i < nr_ctrls; ++i) {
  123. if (controls[i].addr) {
  124. rdmsr(controls[i].addr,
  125. controls[i].saved.low,
  126. controls[i].saved.high);
  127. }
  128. }
  129. }
  130. static void nmi_save_registers(void *dummy)
  131. {
  132. int cpu = smp_processor_id();
  133. struct op_msrs *msrs = &per_cpu(cpu_msrs, cpu);
  134. nmi_cpu_save_registers(msrs);
  135. }
  136. static void free_msrs(void)
  137. {
  138. int i;
  139. for_each_possible_cpu(i) {
  140. kfree(per_cpu(cpu_msrs, i).counters);
  141. per_cpu(cpu_msrs, i).counters = NULL;
  142. kfree(per_cpu(cpu_msrs, i).controls);
  143. per_cpu(cpu_msrs, i).controls = NULL;
  144. }
  145. }
  146. static int allocate_msrs(void)
  147. {
  148. int success = 1;
  149. size_t controls_size = sizeof(struct op_msr) * model->num_controls;
  150. size_t counters_size = sizeof(struct op_msr) * model->num_counters;
  151. int i;
  152. for_each_possible_cpu(i) {
  153. per_cpu(cpu_msrs, i).counters = kmalloc(counters_size,
  154. GFP_KERNEL);
  155. if (!per_cpu(cpu_msrs, i).counters) {
  156. success = 0;
  157. break;
  158. }
  159. per_cpu(cpu_msrs, i).controls = kmalloc(controls_size,
  160. GFP_KERNEL);
  161. if (!per_cpu(cpu_msrs, i).controls) {
  162. success = 0;
  163. break;
  164. }
  165. }
  166. if (!success)
  167. free_msrs();
  168. return success;
  169. }
  170. static void nmi_cpu_setup(void *dummy)
  171. {
  172. int cpu = smp_processor_id();
  173. struct op_msrs *msrs = &per_cpu(cpu_msrs, cpu);
  174. spin_lock(&oprofilefs_lock);
  175. model->setup_ctrs(msrs);
  176. spin_unlock(&oprofilefs_lock);
  177. per_cpu(saved_lvtpc, cpu) = apic_read(APIC_LVTPC);
  178. apic_write(APIC_LVTPC, APIC_DM_NMI);
  179. }
  180. static struct notifier_block profile_exceptions_nb = {
  181. .notifier_call = profile_exceptions_notify,
  182. .next = NULL,
  183. .priority = 0
  184. };
  185. static int nmi_setup(void)
  186. {
  187. int err = 0;
  188. int cpu;
  189. if (!allocate_msrs())
  190. return -ENOMEM;
  191. err = register_die_notifier(&profile_exceptions_nb);
  192. if (err) {
  193. free_msrs();
  194. return err;
  195. }
  196. /* We need to serialize save and setup for HT because the subset
  197. * of msrs are distinct for save and setup operations
  198. */
  199. /* Assume saved/restored counters are the same on all CPUs */
  200. model->fill_in_addresses(&per_cpu(cpu_msrs, 0));
  201. for_each_possible_cpu(cpu) {
  202. if (cpu != 0) {
  203. memcpy(per_cpu(cpu_msrs, cpu).counters,
  204. per_cpu(cpu_msrs, 0).counters,
  205. sizeof(struct op_msr) * model->num_counters);
  206. memcpy(per_cpu(cpu_msrs, cpu).controls,
  207. per_cpu(cpu_msrs, 0).controls,
  208. sizeof(struct op_msr) * model->num_controls);
  209. }
  210. }
  211. on_each_cpu(nmi_save_registers, NULL, 1);
  212. on_each_cpu(nmi_cpu_setup, NULL, 1);
  213. nmi_enabled = 1;
  214. return 0;
  215. }
  216. static void nmi_restore_registers(struct op_msrs *msrs)
  217. {
  218. unsigned int const nr_ctrs = model->num_counters;
  219. unsigned int const nr_ctrls = model->num_controls;
  220. struct op_msr *counters = msrs->counters;
  221. struct op_msr *controls = msrs->controls;
  222. unsigned int i;
  223. for (i = 0; i < nr_ctrls; ++i) {
  224. if (controls[i].addr) {
  225. wrmsr(controls[i].addr,
  226. controls[i].saved.low,
  227. controls[i].saved.high);
  228. }
  229. }
  230. for (i = 0; i < nr_ctrs; ++i) {
  231. if (counters[i].addr) {
  232. wrmsr(counters[i].addr,
  233. counters[i].saved.low,
  234. counters[i].saved.high);
  235. }
  236. }
  237. }
  238. static void nmi_cpu_shutdown(void *dummy)
  239. {
  240. unsigned int v;
  241. int cpu = smp_processor_id();
  242. struct op_msrs *msrs = &__get_cpu_var(cpu_msrs);
  243. /* restoring APIC_LVTPC can trigger an apic error because the delivery
  244. * mode and vector nr combination can be illegal. That's by design: on
  245. * power on apic lvt contain a zero vector nr which are legal only for
  246. * NMI delivery mode. So inhibit apic err before restoring lvtpc
  247. */
  248. v = apic_read(APIC_LVTERR);
  249. apic_write(APIC_LVTERR, v | APIC_LVT_MASKED);
  250. apic_write(APIC_LVTPC, per_cpu(saved_lvtpc, cpu));
  251. apic_write(APIC_LVTERR, v);
  252. nmi_restore_registers(msrs);
  253. }
  254. static void nmi_shutdown(void)
  255. {
  256. struct op_msrs *msrs;
  257. nmi_enabled = 0;
  258. on_each_cpu(nmi_cpu_shutdown, NULL, 1);
  259. unregister_die_notifier(&profile_exceptions_nb);
  260. msrs = &get_cpu_var(cpu_msrs);
  261. model->shutdown(msrs);
  262. free_msrs();
  263. put_cpu_var(cpu_msrs);
  264. }
  265. static void nmi_cpu_start(void *dummy)
  266. {
  267. struct op_msrs const *msrs = &__get_cpu_var(cpu_msrs);
  268. model->start(msrs);
  269. }
  270. static int nmi_start(void)
  271. {
  272. on_each_cpu(nmi_cpu_start, NULL, 1);
  273. return 0;
  274. }
  275. static void nmi_cpu_stop(void *dummy)
  276. {
  277. struct op_msrs const *msrs = &__get_cpu_var(cpu_msrs);
  278. model->stop(msrs);
  279. }
  280. static void nmi_stop(void)
  281. {
  282. on_each_cpu(nmi_cpu_stop, NULL, 1);
  283. }
  284. struct op_counter_config counter_config[OP_MAX_COUNTER];
  285. static int nmi_create_files(struct super_block *sb, struct dentry *root)
  286. {
  287. unsigned int i;
  288. for (i = 0; i < model->num_counters; ++i) {
  289. struct dentry *dir;
  290. char buf[4];
  291. /* quick little hack to _not_ expose a counter if it is not
  292. * available for use. This should protect userspace app.
  293. * NOTE: assumes 1:1 mapping here (that counters are organized
  294. * sequentially in their struct assignment).
  295. */
  296. if (unlikely(!avail_to_resrv_perfctr_nmi_bit(i)))
  297. continue;
  298. snprintf(buf, sizeof(buf), "%d", i);
  299. dir = oprofilefs_mkdir(sb, root, buf);
  300. oprofilefs_create_ulong(sb, dir, "enabled", &counter_config[i].enabled);
  301. oprofilefs_create_ulong(sb, dir, "event", &counter_config[i].event);
  302. oprofilefs_create_ulong(sb, dir, "count", &counter_config[i].count);
  303. oprofilefs_create_ulong(sb, dir, "unit_mask", &counter_config[i].unit_mask);
  304. oprofilefs_create_ulong(sb, dir, "kernel", &counter_config[i].kernel);
  305. oprofilefs_create_ulong(sb, dir, "user", &counter_config[i].user);
  306. }
  307. return 0;
  308. }
  309. static int p4force;
  310. module_param(p4force, int, 0);
  311. static int __init p4_init(char **cpu_type)
  312. {
  313. __u8 cpu_model = boot_cpu_data.x86_model;
  314. if (!p4force && (cpu_model > 6 || cpu_model == 5))
  315. return 0;
  316. #ifndef CONFIG_SMP
  317. *cpu_type = "i386/p4";
  318. model = &op_p4_spec;
  319. return 1;
  320. #else
  321. switch (smp_num_siblings) {
  322. case 1:
  323. *cpu_type = "i386/p4";
  324. model = &op_p4_spec;
  325. return 1;
  326. case 2:
  327. *cpu_type = "i386/p4-ht";
  328. model = &op_p4_ht2_spec;
  329. return 1;
  330. }
  331. #endif
  332. printk(KERN_INFO "oprofile: P4 HyperThreading detected with > 2 threads\n");
  333. printk(KERN_INFO "oprofile: Reverting to timer mode.\n");
  334. return 0;
  335. }
  336. static int __init ppro_init(char **cpu_type)
  337. {
  338. __u8 cpu_model = boot_cpu_data.x86_model;
  339. switch (cpu_model) {
  340. case 0 ... 2:
  341. *cpu_type = "i386/ppro";
  342. break;
  343. case 3 ... 5:
  344. *cpu_type = "i386/pii";
  345. break;
  346. case 6 ... 8:
  347. *cpu_type = "i386/piii";
  348. break;
  349. case 9:
  350. *cpu_type = "i386/p6_mobile";
  351. break;
  352. case 10 ... 13:
  353. *cpu_type = "i386/p6";
  354. break;
  355. case 14:
  356. *cpu_type = "i386/core";
  357. break;
  358. case 15: case 23:
  359. *cpu_type = "i386/core_2";
  360. break;
  361. case 26:
  362. *cpu_type = "i386/core_2";
  363. break;
  364. default:
  365. /* Unknown */
  366. return 0;
  367. }
  368. model = &op_ppro_spec;
  369. return 1;
  370. }
  371. /* in order to get sysfs right */
  372. static int using_nmi;
  373. int __init op_nmi_init(struct oprofile_operations *ops)
  374. {
  375. __u8 vendor = boot_cpu_data.x86_vendor;
  376. __u8 family = boot_cpu_data.x86;
  377. char *cpu_type;
  378. if (!cpu_has_apic)
  379. return -ENODEV;
  380. switch (vendor) {
  381. case X86_VENDOR_AMD:
  382. /* Needs to be at least an Athlon (or hammer in 32bit mode) */
  383. switch (family) {
  384. default:
  385. return -ENODEV;
  386. case 6:
  387. model = &op_athlon_spec;
  388. cpu_type = "i386/athlon";
  389. break;
  390. case 0xf:
  391. model = &op_athlon_spec;
  392. /* Actually it could be i386/hammer too, but give
  393. user space an consistent name. */
  394. cpu_type = "x86-64/hammer";
  395. break;
  396. case 0x10:
  397. model = &op_athlon_spec;
  398. cpu_type = "x86-64/family10";
  399. break;
  400. }
  401. break;
  402. case X86_VENDOR_INTEL:
  403. switch (family) {
  404. /* Pentium IV */
  405. case 0xf:
  406. if (!p4_init(&cpu_type))
  407. return -ENODEV;
  408. break;
  409. /* A P6-class processor */
  410. case 6:
  411. if (!ppro_init(&cpu_type))
  412. return -ENODEV;
  413. break;
  414. default:
  415. return -ENODEV;
  416. }
  417. break;
  418. default:
  419. return -ENODEV;
  420. }
  421. init_sysfs();
  422. #ifdef CONFIG_SMP
  423. register_cpu_notifier(&oprofile_cpu_nb);
  424. #endif
  425. using_nmi = 1;
  426. ops->create_files = nmi_create_files;
  427. ops->setup = nmi_setup;
  428. ops->shutdown = nmi_shutdown;
  429. ops->start = nmi_start;
  430. ops->stop = nmi_stop;
  431. ops->cpu_type = cpu_type;
  432. printk(KERN_INFO "oprofile: using NMI interrupt.\n");
  433. return 0;
  434. }
  435. void op_nmi_exit(void)
  436. {
  437. if (using_nmi) {
  438. exit_sysfs();
  439. #ifdef CONFIG_SMP
  440. unregister_cpu_notifier(&oprofile_cpu_nb);
  441. #endif
  442. }
  443. }