nmi_int.c 12 KB

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