nmi_int.c 17 KB

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