nmi_int.c 12 KB

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