nmi_int.c 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799
  1. /**
  2. * @file nmi_int.c
  3. *
  4. * @remark Copyright 2002-2009 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. * @author Barry Kasindorf <barry.kasindorf@amd.com>
  10. * @author Jason Yeh <jason.yeh@amd.com>
  11. * @author Suravee Suthikulpanit <suravee.suthikulpanit@amd.com>
  12. */
  13. #include <linux/init.h>
  14. #include <linux/notifier.h>
  15. #include <linux/smp.h>
  16. #include <linux/oprofile.h>
  17. #include <linux/sysdev.h>
  18. #include <linux/slab.h>
  19. #include <linux/moduleparam.h>
  20. #include <linux/kdebug.h>
  21. #include <linux/cpu.h>
  22. #include <asm/nmi.h>
  23. #include <asm/msr.h>
  24. #include <asm/apic.h>
  25. #include "op_counter.h"
  26. #include "op_x86_model.h"
  27. static struct op_x86_model_spec *model;
  28. static DEFINE_PER_CPU(struct op_msrs, cpu_msrs);
  29. static DEFINE_PER_CPU(unsigned long, saved_lvtpc);
  30. /* must be protected with get_online_cpus()/put_online_cpus(): */
  31. static int nmi_enabled;
  32. static int ctr_running;
  33. struct op_counter_config counter_config[OP_MAX_COUNTER];
  34. /* common functions */
  35. u64 op_x86_get_ctrl(struct op_x86_model_spec const *model,
  36. struct op_counter_config *counter_config)
  37. {
  38. u64 val = 0;
  39. u16 event = (u16)counter_config->event;
  40. val |= ARCH_PERFMON_EVENTSEL_INT;
  41. val |= counter_config->user ? ARCH_PERFMON_EVENTSEL_USR : 0;
  42. val |= counter_config->kernel ? ARCH_PERFMON_EVENTSEL_OS : 0;
  43. val |= (counter_config->unit_mask & 0xFF) << 8;
  44. event &= model->event_mask ? model->event_mask : 0xFF;
  45. val |= event & 0xFF;
  46. val |= (event & 0x0F00) << 24;
  47. return val;
  48. }
  49. static int profile_exceptions_notify(struct notifier_block *self,
  50. unsigned long val, void *data)
  51. {
  52. struct die_args *args = (struct die_args *)data;
  53. int ret = NOTIFY_DONE;
  54. switch (val) {
  55. case DIE_NMI:
  56. case DIE_NMI_IPI:
  57. if (ctr_running)
  58. model->check_ctrs(args->regs, &__get_cpu_var(cpu_msrs));
  59. else if (!nmi_enabled)
  60. break;
  61. else
  62. model->stop(&__get_cpu_var(cpu_msrs));
  63. ret = NOTIFY_STOP;
  64. break;
  65. default:
  66. break;
  67. }
  68. return ret;
  69. }
  70. static void nmi_cpu_save_registers(struct op_msrs *msrs)
  71. {
  72. struct op_msr *counters = msrs->counters;
  73. struct op_msr *controls = msrs->controls;
  74. unsigned int i;
  75. for (i = 0; i < model->num_counters; ++i) {
  76. if (counters[i].addr)
  77. rdmsrl(counters[i].addr, counters[i].saved);
  78. }
  79. for (i = 0; i < model->num_controls; ++i) {
  80. if (controls[i].addr)
  81. rdmsrl(controls[i].addr, controls[i].saved);
  82. }
  83. }
  84. static void nmi_cpu_start(void *dummy)
  85. {
  86. struct op_msrs const *msrs = &__get_cpu_var(cpu_msrs);
  87. if (!msrs->controls)
  88. WARN_ON_ONCE(1);
  89. else
  90. model->start(msrs);
  91. }
  92. static int nmi_start(void)
  93. {
  94. get_online_cpus();
  95. on_each_cpu(nmi_cpu_start, NULL, 1);
  96. ctr_running = 1;
  97. put_online_cpus();
  98. return 0;
  99. }
  100. static void nmi_cpu_stop(void *dummy)
  101. {
  102. struct op_msrs const *msrs = &__get_cpu_var(cpu_msrs);
  103. if (!msrs->controls)
  104. WARN_ON_ONCE(1);
  105. else
  106. model->stop(msrs);
  107. }
  108. static void nmi_stop(void)
  109. {
  110. get_online_cpus();
  111. on_each_cpu(nmi_cpu_stop, NULL, 1);
  112. ctr_running = 0;
  113. put_online_cpus();
  114. }
  115. #ifdef CONFIG_OPROFILE_EVENT_MULTIPLEX
  116. static DEFINE_PER_CPU(int, switch_index);
  117. static inline int has_mux(void)
  118. {
  119. return !!model->switch_ctrl;
  120. }
  121. inline int op_x86_phys_to_virt(int phys)
  122. {
  123. return __get_cpu_var(switch_index) + phys;
  124. }
  125. inline int op_x86_virt_to_phys(int virt)
  126. {
  127. return virt % model->num_counters;
  128. }
  129. static void nmi_shutdown_mux(void)
  130. {
  131. int i;
  132. if (!has_mux())
  133. return;
  134. for_each_possible_cpu(i) {
  135. kfree(per_cpu(cpu_msrs, i).multiplex);
  136. per_cpu(cpu_msrs, i).multiplex = NULL;
  137. per_cpu(switch_index, i) = 0;
  138. }
  139. }
  140. static int nmi_setup_mux(void)
  141. {
  142. size_t multiplex_size =
  143. sizeof(struct op_msr) * model->num_virt_counters;
  144. int i;
  145. if (!has_mux())
  146. return 1;
  147. for_each_possible_cpu(i) {
  148. per_cpu(cpu_msrs, i).multiplex =
  149. kzalloc(multiplex_size, GFP_KERNEL);
  150. if (!per_cpu(cpu_msrs, i).multiplex)
  151. return 0;
  152. }
  153. return 1;
  154. }
  155. static void nmi_cpu_setup_mux(int cpu, struct op_msrs const * const msrs)
  156. {
  157. int i;
  158. struct op_msr *multiplex = msrs->multiplex;
  159. if (!has_mux())
  160. return;
  161. for (i = 0; i < model->num_virt_counters; ++i) {
  162. if (counter_config[i].enabled) {
  163. multiplex[i].saved = -(u64)counter_config[i].count;
  164. } else {
  165. multiplex[i].saved = 0;
  166. }
  167. }
  168. per_cpu(switch_index, cpu) = 0;
  169. }
  170. static void nmi_cpu_save_mpx_registers(struct op_msrs *msrs)
  171. {
  172. struct op_msr *counters = msrs->counters;
  173. struct op_msr *multiplex = msrs->multiplex;
  174. int i;
  175. for (i = 0; i < model->num_counters; ++i) {
  176. int virt = op_x86_phys_to_virt(i);
  177. if (counters[i].addr)
  178. rdmsrl(counters[i].addr, multiplex[virt].saved);
  179. }
  180. }
  181. static void nmi_cpu_restore_mpx_registers(struct op_msrs *msrs)
  182. {
  183. struct op_msr *counters = msrs->counters;
  184. struct op_msr *multiplex = msrs->multiplex;
  185. int i;
  186. for (i = 0; i < model->num_counters; ++i) {
  187. int virt = op_x86_phys_to_virt(i);
  188. if (counters[i].addr)
  189. wrmsrl(counters[i].addr, multiplex[virt].saved);
  190. }
  191. }
  192. static void nmi_cpu_switch(void *dummy)
  193. {
  194. int cpu = smp_processor_id();
  195. int si = per_cpu(switch_index, cpu);
  196. struct op_msrs *msrs = &per_cpu(cpu_msrs, cpu);
  197. nmi_cpu_stop(NULL);
  198. nmi_cpu_save_mpx_registers(msrs);
  199. /* move to next set */
  200. si += model->num_counters;
  201. if ((si >= model->num_virt_counters) || (counter_config[si].count == 0))
  202. per_cpu(switch_index, cpu) = 0;
  203. else
  204. per_cpu(switch_index, cpu) = si;
  205. model->switch_ctrl(model, msrs);
  206. nmi_cpu_restore_mpx_registers(msrs);
  207. nmi_cpu_start(NULL);
  208. }
  209. /*
  210. * Quick check to see if multiplexing is necessary.
  211. * The check should be sufficient since counters are used
  212. * in ordre.
  213. */
  214. static int nmi_multiplex_on(void)
  215. {
  216. return counter_config[model->num_counters].count ? 0 : -EINVAL;
  217. }
  218. static int nmi_switch_event(void)
  219. {
  220. if (!has_mux())
  221. return -ENOSYS; /* not implemented */
  222. if (nmi_multiplex_on() < 0)
  223. return -EINVAL; /* not necessary */
  224. get_online_cpus();
  225. if (ctr_running)
  226. on_each_cpu(nmi_cpu_switch, NULL, 1);
  227. put_online_cpus();
  228. return 0;
  229. }
  230. static inline void mux_init(struct oprofile_operations *ops)
  231. {
  232. if (has_mux())
  233. ops->switch_events = nmi_switch_event;
  234. }
  235. static void mux_clone(int cpu)
  236. {
  237. if (!has_mux())
  238. return;
  239. memcpy(per_cpu(cpu_msrs, cpu).multiplex,
  240. per_cpu(cpu_msrs, 0).multiplex,
  241. sizeof(struct op_msr) * model->num_virt_counters);
  242. }
  243. #else
  244. inline int op_x86_phys_to_virt(int phys) { return phys; }
  245. inline int op_x86_virt_to_phys(int virt) { return virt; }
  246. static inline void nmi_shutdown_mux(void) { }
  247. static inline int nmi_setup_mux(void) { return 1; }
  248. static inline void
  249. nmi_cpu_setup_mux(int cpu, struct op_msrs const * const msrs) { }
  250. static inline void mux_init(struct oprofile_operations *ops) { }
  251. static void mux_clone(int cpu) { }
  252. #endif
  253. static void free_msrs(void)
  254. {
  255. int i;
  256. for_each_possible_cpu(i) {
  257. kfree(per_cpu(cpu_msrs, i).counters);
  258. per_cpu(cpu_msrs, i).counters = NULL;
  259. kfree(per_cpu(cpu_msrs, i).controls);
  260. per_cpu(cpu_msrs, i).controls = NULL;
  261. }
  262. nmi_shutdown_mux();
  263. }
  264. static int allocate_msrs(void)
  265. {
  266. size_t controls_size = sizeof(struct op_msr) * model->num_controls;
  267. size_t counters_size = sizeof(struct op_msr) * model->num_counters;
  268. int i;
  269. for_each_possible_cpu(i) {
  270. per_cpu(cpu_msrs, i).counters = kzalloc(counters_size,
  271. GFP_KERNEL);
  272. if (!per_cpu(cpu_msrs, i).counters)
  273. goto fail;
  274. per_cpu(cpu_msrs, i).controls = kzalloc(controls_size,
  275. GFP_KERNEL);
  276. if (!per_cpu(cpu_msrs, i).controls)
  277. goto fail;
  278. }
  279. if (!nmi_setup_mux())
  280. goto fail;
  281. return 1;
  282. fail:
  283. free_msrs();
  284. return 0;
  285. }
  286. static void nmi_cpu_setup(void *dummy)
  287. {
  288. int cpu = smp_processor_id();
  289. struct op_msrs *msrs = &per_cpu(cpu_msrs, cpu);
  290. nmi_cpu_save_registers(msrs);
  291. spin_lock(&oprofilefs_lock);
  292. model->setup_ctrs(model, msrs);
  293. nmi_cpu_setup_mux(cpu, msrs);
  294. spin_unlock(&oprofilefs_lock);
  295. per_cpu(saved_lvtpc, cpu) = apic_read(APIC_LVTPC);
  296. apic_write(APIC_LVTPC, APIC_DM_NMI);
  297. }
  298. static struct notifier_block profile_exceptions_nb = {
  299. .notifier_call = profile_exceptions_notify,
  300. .next = NULL,
  301. .priority = 2
  302. };
  303. static void nmi_cpu_restore_registers(struct op_msrs *msrs)
  304. {
  305. struct op_msr *counters = msrs->counters;
  306. struct op_msr *controls = msrs->controls;
  307. unsigned int i;
  308. for (i = 0; i < model->num_controls; ++i) {
  309. if (controls[i].addr)
  310. wrmsrl(controls[i].addr, controls[i].saved);
  311. }
  312. for (i = 0; i < model->num_counters; ++i) {
  313. if (counters[i].addr)
  314. wrmsrl(counters[i].addr, counters[i].saved);
  315. }
  316. }
  317. static void nmi_cpu_shutdown(void *dummy)
  318. {
  319. unsigned int v;
  320. int cpu = smp_processor_id();
  321. struct op_msrs *msrs = &per_cpu(cpu_msrs, cpu);
  322. /* restoring APIC_LVTPC can trigger an apic error because the delivery
  323. * mode and vector nr combination can be illegal. That's by design: on
  324. * power on apic lvt contain a zero vector nr which are legal only for
  325. * NMI delivery mode. So inhibit apic err before restoring lvtpc
  326. */
  327. v = apic_read(APIC_LVTERR);
  328. apic_write(APIC_LVTERR, v | APIC_LVT_MASKED);
  329. apic_write(APIC_LVTPC, per_cpu(saved_lvtpc, cpu));
  330. apic_write(APIC_LVTERR, v);
  331. nmi_cpu_restore_registers(msrs);
  332. if (model->cpu_down)
  333. model->cpu_down();
  334. }
  335. static void nmi_cpu_up(void *dummy)
  336. {
  337. if (nmi_enabled)
  338. nmi_cpu_setup(dummy);
  339. if (ctr_running)
  340. nmi_cpu_start(dummy);
  341. }
  342. static void nmi_cpu_down(void *dummy)
  343. {
  344. if (ctr_running)
  345. nmi_cpu_stop(dummy);
  346. if (nmi_enabled)
  347. nmi_cpu_shutdown(dummy);
  348. }
  349. static int nmi_create_files(struct super_block *sb, struct dentry *root)
  350. {
  351. unsigned int i;
  352. for (i = 0; i < model->num_virt_counters; ++i) {
  353. struct dentry *dir;
  354. char buf[4];
  355. /* quick little hack to _not_ expose a counter if it is not
  356. * available for use. This should protect userspace app.
  357. * NOTE: assumes 1:1 mapping here (that counters are organized
  358. * sequentially in their struct assignment).
  359. */
  360. if (!avail_to_resrv_perfctr_nmi_bit(op_x86_virt_to_phys(i)))
  361. continue;
  362. snprintf(buf, sizeof(buf), "%d", i);
  363. dir = oprofilefs_mkdir(sb, root, buf);
  364. oprofilefs_create_ulong(sb, dir, "enabled", &counter_config[i].enabled);
  365. oprofilefs_create_ulong(sb, dir, "event", &counter_config[i].event);
  366. oprofilefs_create_ulong(sb, dir, "count", &counter_config[i].count);
  367. oprofilefs_create_ulong(sb, dir, "unit_mask", &counter_config[i].unit_mask);
  368. oprofilefs_create_ulong(sb, dir, "kernel", &counter_config[i].kernel);
  369. oprofilefs_create_ulong(sb, dir, "user", &counter_config[i].user);
  370. }
  371. return 0;
  372. }
  373. static int oprofile_cpu_notifier(struct notifier_block *b, unsigned long action,
  374. void *data)
  375. {
  376. int cpu = (unsigned long)data;
  377. switch (action) {
  378. case CPU_DOWN_FAILED:
  379. case CPU_ONLINE:
  380. smp_call_function_single(cpu, nmi_cpu_up, NULL, 0);
  381. break;
  382. case CPU_DOWN_PREPARE:
  383. smp_call_function_single(cpu, nmi_cpu_down, NULL, 1);
  384. break;
  385. }
  386. return NOTIFY_DONE;
  387. }
  388. static struct notifier_block oprofile_cpu_nb = {
  389. .notifier_call = oprofile_cpu_notifier
  390. };
  391. static int nmi_setup(void)
  392. {
  393. int err = 0;
  394. int cpu;
  395. if (!allocate_msrs())
  396. return -ENOMEM;
  397. /* We need to serialize save and setup for HT because the subset
  398. * of msrs are distinct for save and setup operations
  399. */
  400. /* Assume saved/restored counters are the same on all CPUs */
  401. err = model->fill_in_addresses(&per_cpu(cpu_msrs, 0));
  402. if (err)
  403. goto fail;
  404. for_each_possible_cpu(cpu) {
  405. if (!cpu)
  406. continue;
  407. memcpy(per_cpu(cpu_msrs, cpu).counters,
  408. per_cpu(cpu_msrs, 0).counters,
  409. sizeof(struct op_msr) * model->num_counters);
  410. memcpy(per_cpu(cpu_msrs, cpu).controls,
  411. per_cpu(cpu_msrs, 0).controls,
  412. sizeof(struct op_msr) * model->num_controls);
  413. mux_clone(cpu);
  414. }
  415. nmi_enabled = 0;
  416. ctr_running = 0;
  417. barrier();
  418. err = register_die_notifier(&profile_exceptions_nb);
  419. if (err)
  420. goto fail;
  421. get_online_cpus();
  422. register_cpu_notifier(&oprofile_cpu_nb);
  423. on_each_cpu(nmi_cpu_setup, NULL, 1);
  424. nmi_enabled = 1;
  425. put_online_cpus();
  426. return 0;
  427. fail:
  428. free_msrs();
  429. return err;
  430. }
  431. static void nmi_shutdown(void)
  432. {
  433. struct op_msrs *msrs;
  434. get_online_cpus();
  435. unregister_cpu_notifier(&oprofile_cpu_nb);
  436. on_each_cpu(nmi_cpu_shutdown, NULL, 1);
  437. nmi_enabled = 0;
  438. ctr_running = 0;
  439. put_online_cpus();
  440. barrier();
  441. unregister_die_notifier(&profile_exceptions_nb);
  442. msrs = &get_cpu_var(cpu_msrs);
  443. model->shutdown(msrs);
  444. free_msrs();
  445. put_cpu_var(cpu_msrs);
  446. }
  447. #ifdef CONFIG_PM
  448. static int nmi_suspend(struct sys_device *dev, pm_message_t state)
  449. {
  450. /* Only one CPU left, just stop that one */
  451. if (nmi_enabled == 1)
  452. nmi_cpu_stop(NULL);
  453. return 0;
  454. }
  455. static int nmi_resume(struct sys_device *dev)
  456. {
  457. if (nmi_enabled == 1)
  458. nmi_cpu_start(NULL);
  459. return 0;
  460. }
  461. static struct sysdev_class oprofile_sysclass = {
  462. .name = "oprofile",
  463. .resume = nmi_resume,
  464. .suspend = nmi_suspend,
  465. };
  466. static struct sys_device device_oprofile = {
  467. .id = 0,
  468. .cls = &oprofile_sysclass,
  469. };
  470. static int __init init_sysfs(void)
  471. {
  472. int error;
  473. error = sysdev_class_register(&oprofile_sysclass);
  474. if (error)
  475. return error;
  476. error = sysdev_register(&device_oprofile);
  477. if (error)
  478. sysdev_class_unregister(&oprofile_sysclass);
  479. return error;
  480. }
  481. static void exit_sysfs(void)
  482. {
  483. sysdev_unregister(&device_oprofile);
  484. sysdev_class_unregister(&oprofile_sysclass);
  485. }
  486. #else
  487. static inline int init_sysfs(void) { return 0; }
  488. static inline void exit_sysfs(void) { }
  489. #endif /* CONFIG_PM */
  490. static int __init p4_init(char **cpu_type)
  491. {
  492. __u8 cpu_model = boot_cpu_data.x86_model;
  493. if (cpu_model > 6 || cpu_model == 5)
  494. return 0;
  495. #ifndef CONFIG_SMP
  496. *cpu_type = "i386/p4";
  497. model = &op_p4_spec;
  498. return 1;
  499. #else
  500. switch (smp_num_siblings) {
  501. case 1:
  502. *cpu_type = "i386/p4";
  503. model = &op_p4_spec;
  504. return 1;
  505. case 2:
  506. *cpu_type = "i386/p4-ht";
  507. model = &op_p4_ht2_spec;
  508. return 1;
  509. }
  510. #endif
  511. printk(KERN_INFO "oprofile: P4 HyperThreading detected with > 2 threads\n");
  512. printk(KERN_INFO "oprofile: Reverting to timer mode.\n");
  513. return 0;
  514. }
  515. static int force_arch_perfmon;
  516. static int force_cpu_type(const char *str, struct kernel_param *kp)
  517. {
  518. if (!strcmp(str, "arch_perfmon")) {
  519. force_arch_perfmon = 1;
  520. printk(KERN_INFO "oprofile: forcing architectural perfmon\n");
  521. }
  522. return 0;
  523. }
  524. module_param_call(cpu_type, force_cpu_type, NULL, NULL, 0);
  525. static int __init ppro_init(char **cpu_type)
  526. {
  527. __u8 cpu_model = boot_cpu_data.x86_model;
  528. struct op_x86_model_spec *spec = &op_ppro_spec; /* default */
  529. if (force_arch_perfmon && cpu_has_arch_perfmon)
  530. return 0;
  531. /*
  532. * Documentation on identifying Intel processors by CPU family
  533. * and model can be found in the Intel Software Developer's
  534. * Manuals (SDM):
  535. *
  536. * http://www.intel.com/products/processor/manuals/
  537. *
  538. * As of May 2010 the documentation for this was in the:
  539. * "Intel 64 and IA-32 Architectures Software Developer's
  540. * Manual Volume 3B: System Programming Guide", "Table B-1
  541. * CPUID Signature Values of DisplayFamily_DisplayModel".
  542. */
  543. switch (cpu_model) {
  544. case 0 ... 2:
  545. *cpu_type = "i386/ppro";
  546. break;
  547. case 3 ... 5:
  548. *cpu_type = "i386/pii";
  549. break;
  550. case 6 ... 8:
  551. case 10 ... 11:
  552. *cpu_type = "i386/piii";
  553. break;
  554. case 9:
  555. case 13:
  556. *cpu_type = "i386/p6_mobile";
  557. break;
  558. case 14:
  559. *cpu_type = "i386/core";
  560. break;
  561. case 15: case 23:
  562. *cpu_type = "i386/core_2";
  563. break;
  564. case 0x1a:
  565. case 0x1e:
  566. case 0x2e:
  567. spec = &op_arch_perfmon_spec;
  568. *cpu_type = "i386/core_i7";
  569. break;
  570. case 0x1c:
  571. *cpu_type = "i386/atom";
  572. break;
  573. default:
  574. /* Unknown */
  575. return 0;
  576. }
  577. model = spec;
  578. return 1;
  579. }
  580. /* in order to get sysfs right */
  581. static int using_nmi;
  582. int __init op_nmi_init(struct oprofile_operations *ops)
  583. {
  584. __u8 vendor = boot_cpu_data.x86_vendor;
  585. __u8 family = boot_cpu_data.x86;
  586. char *cpu_type = NULL;
  587. int ret = 0;
  588. using_nmi = 0;
  589. if (!cpu_has_apic)
  590. return -ENODEV;
  591. switch (vendor) {
  592. case X86_VENDOR_AMD:
  593. /* Needs to be at least an Athlon (or hammer in 32bit mode) */
  594. switch (family) {
  595. case 6:
  596. cpu_type = "i386/athlon";
  597. break;
  598. case 0xf:
  599. /*
  600. * Actually it could be i386/hammer too, but
  601. * give user space an consistent name.
  602. */
  603. cpu_type = "x86-64/hammer";
  604. break;
  605. case 0x10:
  606. cpu_type = "x86-64/family10";
  607. break;
  608. case 0x11:
  609. cpu_type = "x86-64/family11h";
  610. break;
  611. default:
  612. return -ENODEV;
  613. }
  614. model = &op_amd_spec;
  615. break;
  616. case X86_VENDOR_INTEL:
  617. switch (family) {
  618. /* Pentium IV */
  619. case 0xf:
  620. p4_init(&cpu_type);
  621. break;
  622. /* A P6-class processor */
  623. case 6:
  624. ppro_init(&cpu_type);
  625. break;
  626. default:
  627. break;
  628. }
  629. if (cpu_type)
  630. break;
  631. if (!cpu_has_arch_perfmon)
  632. return -ENODEV;
  633. /* use arch perfmon as fallback */
  634. cpu_type = "i386/arch_perfmon";
  635. model = &op_arch_perfmon_spec;
  636. break;
  637. default:
  638. return -ENODEV;
  639. }
  640. /* default values, can be overwritten by model */
  641. ops->create_files = nmi_create_files;
  642. ops->setup = nmi_setup;
  643. ops->shutdown = nmi_shutdown;
  644. ops->start = nmi_start;
  645. ops->stop = nmi_stop;
  646. ops->cpu_type = cpu_type;
  647. if (model->init)
  648. ret = model->init(ops);
  649. if (ret)
  650. return ret;
  651. if (!model->num_virt_counters)
  652. model->num_virt_counters = model->num_counters;
  653. mux_init(ops);
  654. ret = init_sysfs();
  655. if (ret)
  656. return ret;
  657. using_nmi = 1;
  658. printk(KERN_INFO "oprofile: using NMI interrupt.\n");
  659. return 0;
  660. }
  661. void op_nmi_exit(void)
  662. {
  663. if (using_nmi)
  664. exit_sysfs();
  665. }