nmi_int.c 17 KB

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