nmi_int.c 12 KB

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