nmi_int.c 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804
  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 |= (u64)(event & 0x0F00) << 24;
  51. return val;
  52. }
  53. static int profile_exceptions_notify(unsigned int val, struct pt_regs *regs)
  54. {
  55. if (ctr_running)
  56. model->check_ctrs(regs, this_cpu_ptr(&cpu_msrs));
  57. else if (!nmi_enabled)
  58. return NMI_DONE;
  59. else
  60. model->stop(this_cpu_ptr(&cpu_msrs));
  61. return NMI_HANDLED;
  62. }
  63. static void nmi_cpu_save_registers(struct op_msrs *msrs)
  64. {
  65. struct op_msr *counters = msrs->counters;
  66. struct op_msr *controls = msrs->controls;
  67. unsigned int i;
  68. for (i = 0; i < model->num_counters; ++i) {
  69. if (counters[i].addr)
  70. rdmsrl(counters[i].addr, counters[i].saved);
  71. }
  72. for (i = 0; i < model->num_controls; ++i) {
  73. if (controls[i].addr)
  74. rdmsrl(controls[i].addr, controls[i].saved);
  75. }
  76. }
  77. static void nmi_cpu_start(void *dummy)
  78. {
  79. struct op_msrs const *msrs = this_cpu_ptr(&cpu_msrs);
  80. if (!msrs->controls)
  81. WARN_ON_ONCE(1);
  82. else
  83. model->start(msrs);
  84. }
  85. static int nmi_start(void)
  86. {
  87. get_online_cpus();
  88. ctr_running = 1;
  89. /* make ctr_running visible to the nmi handler: */
  90. smp_mb();
  91. on_each_cpu(nmi_cpu_start, NULL, 1);
  92. put_online_cpus();
  93. return 0;
  94. }
  95. static void nmi_cpu_stop(void *dummy)
  96. {
  97. struct op_msrs const *msrs = this_cpu_ptr(&cpu_msrs);
  98. if (!msrs->controls)
  99. WARN_ON_ONCE(1);
  100. else
  101. model->stop(msrs);
  102. }
  103. static void nmi_stop(void)
  104. {
  105. get_online_cpus();
  106. on_each_cpu(nmi_cpu_stop, NULL, 1);
  107. ctr_running = 0;
  108. put_online_cpus();
  109. }
  110. #ifdef CONFIG_OPROFILE_EVENT_MULTIPLEX
  111. static DEFINE_PER_CPU(int, switch_index);
  112. static inline int has_mux(void)
  113. {
  114. return !!model->switch_ctrl;
  115. }
  116. inline int op_x86_phys_to_virt(int phys)
  117. {
  118. return __this_cpu_read(switch_index) + phys;
  119. }
  120. inline int op_x86_virt_to_phys(int virt)
  121. {
  122. return virt % model->num_counters;
  123. }
  124. static void nmi_shutdown_mux(void)
  125. {
  126. int i;
  127. if (!has_mux())
  128. return;
  129. for_each_possible_cpu(i) {
  130. kfree(per_cpu(cpu_msrs, i).multiplex);
  131. per_cpu(cpu_msrs, i).multiplex = NULL;
  132. per_cpu(switch_index, i) = 0;
  133. }
  134. }
  135. static int nmi_setup_mux(void)
  136. {
  137. size_t multiplex_size =
  138. sizeof(struct op_msr) * model->num_virt_counters;
  139. int i;
  140. if (!has_mux())
  141. return 1;
  142. for_each_possible_cpu(i) {
  143. per_cpu(cpu_msrs, i).multiplex =
  144. kzalloc(multiplex_size, GFP_KERNEL);
  145. if (!per_cpu(cpu_msrs, i).multiplex)
  146. return 0;
  147. }
  148. return 1;
  149. }
  150. static void nmi_cpu_setup_mux(int cpu, struct op_msrs const * const msrs)
  151. {
  152. int i;
  153. struct op_msr *multiplex = msrs->multiplex;
  154. if (!has_mux())
  155. return;
  156. for (i = 0; i < model->num_virt_counters; ++i) {
  157. if (counter_config[i].enabled) {
  158. multiplex[i].saved = -(u64)counter_config[i].count;
  159. } else {
  160. multiplex[i].saved = 0;
  161. }
  162. }
  163. per_cpu(switch_index, cpu) = 0;
  164. }
  165. static void nmi_cpu_save_mpx_registers(struct op_msrs *msrs)
  166. {
  167. struct op_msr *counters = msrs->counters;
  168. struct op_msr *multiplex = msrs->multiplex;
  169. int i;
  170. for (i = 0; i < model->num_counters; ++i) {
  171. int virt = op_x86_phys_to_virt(i);
  172. if (counters[i].addr)
  173. rdmsrl(counters[i].addr, multiplex[virt].saved);
  174. }
  175. }
  176. static void nmi_cpu_restore_mpx_registers(struct op_msrs *msrs)
  177. {
  178. struct op_msr *counters = msrs->counters;
  179. struct op_msr *multiplex = msrs->multiplex;
  180. int i;
  181. for (i = 0; i < model->num_counters; ++i) {
  182. int virt = op_x86_phys_to_virt(i);
  183. if (counters[i].addr)
  184. wrmsrl(counters[i].addr, multiplex[virt].saved);
  185. }
  186. }
  187. static void nmi_cpu_switch(void *dummy)
  188. {
  189. int cpu = smp_processor_id();
  190. int si = per_cpu(switch_index, cpu);
  191. struct op_msrs *msrs = &per_cpu(cpu_msrs, cpu);
  192. nmi_cpu_stop(NULL);
  193. nmi_cpu_save_mpx_registers(msrs);
  194. /* move to next set */
  195. si += model->num_counters;
  196. if ((si >= model->num_virt_counters) || (counter_config[si].count == 0))
  197. per_cpu(switch_index, cpu) = 0;
  198. else
  199. per_cpu(switch_index, cpu) = si;
  200. model->switch_ctrl(model, msrs);
  201. nmi_cpu_restore_mpx_registers(msrs);
  202. nmi_cpu_start(NULL);
  203. }
  204. /*
  205. * Quick check to see if multiplexing is necessary.
  206. * The check should be sufficient since counters are used
  207. * in ordre.
  208. */
  209. static int nmi_multiplex_on(void)
  210. {
  211. return counter_config[model->num_counters].count ? 0 : -EINVAL;
  212. }
  213. static int nmi_switch_event(void)
  214. {
  215. if (!has_mux())
  216. return -ENOSYS; /* not implemented */
  217. if (nmi_multiplex_on() < 0)
  218. return -EINVAL; /* not necessary */
  219. get_online_cpus();
  220. if (ctr_running)
  221. on_each_cpu(nmi_cpu_switch, NULL, 1);
  222. put_online_cpus();
  223. return 0;
  224. }
  225. static inline void mux_init(struct oprofile_operations *ops)
  226. {
  227. if (has_mux())
  228. ops->switch_events = nmi_switch_event;
  229. }
  230. static void mux_clone(int cpu)
  231. {
  232. if (!has_mux())
  233. return;
  234. memcpy(per_cpu(cpu_msrs, cpu).multiplex,
  235. per_cpu(cpu_msrs, 0).multiplex,
  236. sizeof(struct op_msr) * model->num_virt_counters);
  237. }
  238. #else
  239. inline int op_x86_phys_to_virt(int phys) { return phys; }
  240. inline int op_x86_virt_to_phys(int virt) { return virt; }
  241. static inline void nmi_shutdown_mux(void) { }
  242. static inline int nmi_setup_mux(void) { return 1; }
  243. static inline void
  244. nmi_cpu_setup_mux(int cpu, struct op_msrs const * const msrs) { }
  245. static inline void mux_init(struct oprofile_operations *ops) { }
  246. static void mux_clone(int cpu) { }
  247. #endif
  248. static void free_msrs(void)
  249. {
  250. int i;
  251. for_each_possible_cpu(i) {
  252. kfree(per_cpu(cpu_msrs, i).counters);
  253. per_cpu(cpu_msrs, i).counters = NULL;
  254. kfree(per_cpu(cpu_msrs, i).controls);
  255. per_cpu(cpu_msrs, i).controls = NULL;
  256. }
  257. nmi_shutdown_mux();
  258. }
  259. static int allocate_msrs(void)
  260. {
  261. size_t controls_size = sizeof(struct op_msr) * model->num_controls;
  262. size_t counters_size = sizeof(struct op_msr) * model->num_counters;
  263. int i;
  264. for_each_possible_cpu(i) {
  265. per_cpu(cpu_msrs, i).counters = kzalloc(counters_size,
  266. GFP_KERNEL);
  267. if (!per_cpu(cpu_msrs, i).counters)
  268. goto fail;
  269. per_cpu(cpu_msrs, i).controls = kzalloc(controls_size,
  270. GFP_KERNEL);
  271. if (!per_cpu(cpu_msrs, i).controls)
  272. goto fail;
  273. }
  274. if (!nmi_setup_mux())
  275. goto fail;
  276. return 1;
  277. fail:
  278. free_msrs();
  279. return 0;
  280. }
  281. static void nmi_cpu_setup(void *dummy)
  282. {
  283. int cpu = smp_processor_id();
  284. struct op_msrs *msrs = &per_cpu(cpu_msrs, cpu);
  285. nmi_cpu_save_registers(msrs);
  286. raw_spin_lock(&oprofilefs_lock);
  287. model->setup_ctrs(model, msrs);
  288. nmi_cpu_setup_mux(cpu, msrs);
  289. raw_spin_unlock(&oprofilefs_lock);
  290. per_cpu(saved_lvtpc, cpu) = apic_read(APIC_LVTPC);
  291. apic_write(APIC_LVTPC, APIC_DM_NMI);
  292. }
  293. static void nmi_cpu_restore_registers(struct op_msrs *msrs)
  294. {
  295. struct op_msr *counters = msrs->counters;
  296. struct op_msr *controls = msrs->controls;
  297. unsigned int i;
  298. for (i = 0; i < model->num_controls; ++i) {
  299. if (controls[i].addr)
  300. wrmsrl(controls[i].addr, controls[i].saved);
  301. }
  302. for (i = 0; i < model->num_counters; ++i) {
  303. if (counters[i].addr)
  304. wrmsrl(counters[i].addr, counters[i].saved);
  305. }
  306. }
  307. static void nmi_cpu_shutdown(void *dummy)
  308. {
  309. unsigned int v;
  310. int cpu = smp_processor_id();
  311. struct op_msrs *msrs = &per_cpu(cpu_msrs, cpu);
  312. /* restoring APIC_LVTPC can trigger an apic error because the delivery
  313. * mode and vector nr combination can be illegal. That's by design: on
  314. * power on apic lvt contain a zero vector nr which are legal only for
  315. * NMI delivery mode. So inhibit apic err before restoring lvtpc
  316. */
  317. v = apic_read(APIC_LVTERR);
  318. apic_write(APIC_LVTERR, v | APIC_LVT_MASKED);
  319. apic_write(APIC_LVTPC, per_cpu(saved_lvtpc, cpu));
  320. apic_write(APIC_LVTERR, v);
  321. nmi_cpu_restore_registers(msrs);
  322. }
  323. static void nmi_cpu_up(void *dummy)
  324. {
  325. if (nmi_enabled)
  326. nmi_cpu_setup(dummy);
  327. if (ctr_running)
  328. nmi_cpu_start(dummy);
  329. }
  330. static void nmi_cpu_down(void *dummy)
  331. {
  332. if (ctr_running)
  333. nmi_cpu_stop(dummy);
  334. if (nmi_enabled)
  335. nmi_cpu_shutdown(dummy);
  336. }
  337. static int nmi_create_files(struct dentry *root)
  338. {
  339. unsigned int i;
  340. for (i = 0; i < model->num_virt_counters; ++i) {
  341. struct dentry *dir;
  342. char buf[4];
  343. /* quick little hack to _not_ expose a counter if it is not
  344. * available for use. This should protect userspace app.
  345. * NOTE: assumes 1:1 mapping here (that counters are organized
  346. * sequentially in their struct assignment).
  347. */
  348. if (!avail_to_resrv_perfctr_nmi_bit(op_x86_virt_to_phys(i)))
  349. continue;
  350. snprintf(buf, sizeof(buf), "%d", i);
  351. dir = oprofilefs_mkdir(root, buf);
  352. oprofilefs_create_ulong(dir, "enabled", &counter_config[i].enabled);
  353. oprofilefs_create_ulong(dir, "event", &counter_config[i].event);
  354. oprofilefs_create_ulong(dir, "count", &counter_config[i].count);
  355. oprofilefs_create_ulong(dir, "unit_mask", &counter_config[i].unit_mask);
  356. oprofilefs_create_ulong(dir, "kernel", &counter_config[i].kernel);
  357. oprofilefs_create_ulong(dir, "user", &counter_config[i].user);
  358. oprofilefs_create_ulong(dir, "extra", &counter_config[i].extra);
  359. }
  360. return 0;
  361. }
  362. static int oprofile_cpu_notifier(struct notifier_block *b, unsigned long action,
  363. void *data)
  364. {
  365. int cpu = (unsigned long)data;
  366. switch (action & ~CPU_TASKS_FROZEN) {
  367. case CPU_DOWN_FAILED:
  368. case CPU_ONLINE:
  369. smp_call_function_single(cpu, nmi_cpu_up, NULL, 0);
  370. break;
  371. case CPU_DOWN_PREPARE:
  372. smp_call_function_single(cpu, nmi_cpu_down, NULL, 1);
  373. break;
  374. }
  375. return NOTIFY_DONE;
  376. }
  377. static struct notifier_block oprofile_cpu_nb = {
  378. .notifier_call = oprofile_cpu_notifier
  379. };
  380. static int nmi_setup(void)
  381. {
  382. int err = 0;
  383. int cpu;
  384. if (!allocate_msrs())
  385. return -ENOMEM;
  386. /* We need to serialize save and setup for HT because the subset
  387. * of msrs are distinct for save and setup operations
  388. */
  389. /* Assume saved/restored counters are the same on all CPUs */
  390. err = model->fill_in_addresses(&per_cpu(cpu_msrs, 0));
  391. if (err)
  392. goto fail;
  393. for_each_possible_cpu(cpu) {
  394. if (!IS_ENABLED(CONFIG_SMP) || !cpu)
  395. continue;
  396. memcpy(per_cpu(cpu_msrs, cpu).counters,
  397. per_cpu(cpu_msrs, 0).counters,
  398. sizeof(struct op_msr) * model->num_counters);
  399. memcpy(per_cpu(cpu_msrs, cpu).controls,
  400. per_cpu(cpu_msrs, 0).controls,
  401. sizeof(struct op_msr) * model->num_controls);
  402. mux_clone(cpu);
  403. }
  404. nmi_enabled = 0;
  405. ctr_running = 0;
  406. /* make variables visible to the nmi handler: */
  407. smp_mb();
  408. err = register_nmi_handler(NMI_LOCAL, profile_exceptions_notify,
  409. 0, "oprofile");
  410. if (err)
  411. goto fail;
  412. cpu_notifier_register_begin();
  413. /* Use get/put_online_cpus() to protect 'nmi_enabled' */
  414. get_online_cpus();
  415. nmi_enabled = 1;
  416. /* make nmi_enabled visible to the nmi handler: */
  417. smp_mb();
  418. on_each_cpu(nmi_cpu_setup, NULL, 1);
  419. __register_cpu_notifier(&oprofile_cpu_nb);
  420. put_online_cpus();
  421. cpu_notifier_register_done();
  422. return 0;
  423. fail:
  424. free_msrs();
  425. return err;
  426. }
  427. static void nmi_shutdown(void)
  428. {
  429. struct op_msrs *msrs;
  430. cpu_notifier_register_begin();
  431. /* Use get/put_online_cpus() to protect 'nmi_enabled' & 'ctr_running' */
  432. get_online_cpus();
  433. on_each_cpu(nmi_cpu_shutdown, NULL, 1);
  434. nmi_enabled = 0;
  435. ctr_running = 0;
  436. __unregister_cpu_notifier(&oprofile_cpu_nb);
  437. put_online_cpus();
  438. cpu_notifier_register_done();
  439. /* make variables visible to the nmi handler: */
  440. smp_mb();
  441. unregister_nmi_handler(NMI_LOCAL, "oprofile");
  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(void)
  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 void nmi_resume(void)
  456. {
  457. if (nmi_enabled == 1)
  458. nmi_cpu_start(NULL);
  459. }
  460. static struct syscore_ops oprofile_syscore_ops = {
  461. .resume = nmi_resume,
  462. .suspend = nmi_suspend,
  463. };
  464. static void __init init_suspend_resume(void)
  465. {
  466. register_syscore_ops(&oprofile_syscore_ops);
  467. }
  468. static void exit_suspend_resume(void)
  469. {
  470. unregister_syscore_ops(&oprofile_syscore_ops);
  471. }
  472. #else
  473. static inline void init_suspend_resume(void) { }
  474. static inline void exit_suspend_resume(void) { }
  475. #endif /* CONFIG_PM */
  476. static int __init p4_init(char **cpu_type)
  477. {
  478. __u8 cpu_model = boot_cpu_data.x86_model;
  479. if (cpu_model > 6 || cpu_model == 5)
  480. return 0;
  481. #ifndef CONFIG_SMP
  482. *cpu_type = "i386/p4";
  483. model = &op_p4_spec;
  484. return 1;
  485. #else
  486. switch (smp_num_siblings) {
  487. case 1:
  488. *cpu_type = "i386/p4";
  489. model = &op_p4_spec;
  490. return 1;
  491. case 2:
  492. *cpu_type = "i386/p4-ht";
  493. model = &op_p4_ht2_spec;
  494. return 1;
  495. }
  496. #endif
  497. printk(KERN_INFO "oprofile: P4 HyperThreading detected with > 2 threads\n");
  498. printk(KERN_INFO "oprofile: Reverting to timer mode.\n");
  499. return 0;
  500. }
  501. enum __force_cpu_type {
  502. reserved = 0, /* do not force */
  503. timer,
  504. arch_perfmon,
  505. };
  506. static int force_cpu_type;
  507. static int set_cpu_type(const char *str, struct kernel_param *kp)
  508. {
  509. if (!strcmp(str, "timer")) {
  510. force_cpu_type = timer;
  511. printk(KERN_INFO "oprofile: forcing NMI timer mode\n");
  512. } else if (!strcmp(str, "arch_perfmon")) {
  513. force_cpu_type = arch_perfmon;
  514. printk(KERN_INFO "oprofile: forcing architectural perfmon\n");
  515. } else {
  516. force_cpu_type = 0;
  517. }
  518. return 0;
  519. }
  520. module_param_call(cpu_type, set_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_cpu_type == arch_perfmon && boot_cpu_has(X86_FEATURE_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 (!boot_cpu_has(X86_FEATURE_APIC))
  586. return -ENODEV;
  587. if (force_cpu_type == timer)
  588. return -ENODEV;
  589. switch (vendor) {
  590. case X86_VENDOR_AMD:
  591. /* Needs to be at least an Athlon (or hammer in 32bit mode) */
  592. switch (family) {
  593. case 6:
  594. cpu_type = "i386/athlon";
  595. break;
  596. case 0xf:
  597. /*
  598. * Actually it could be i386/hammer too, but
  599. * give user space an consistent name.
  600. */
  601. cpu_type = "x86-64/hammer";
  602. break;
  603. case 0x10:
  604. cpu_type = "x86-64/family10";
  605. break;
  606. case 0x11:
  607. cpu_type = "x86-64/family11h";
  608. break;
  609. case 0x12:
  610. cpu_type = "x86-64/family12h";
  611. break;
  612. case 0x14:
  613. cpu_type = "x86-64/family14h";
  614. break;
  615. case 0x15:
  616. cpu_type = "x86-64/family15h";
  617. break;
  618. default:
  619. return -ENODEV;
  620. }
  621. model = &op_amd_spec;
  622. break;
  623. case X86_VENDOR_INTEL:
  624. switch (family) {
  625. /* Pentium IV */
  626. case 0xf:
  627. p4_init(&cpu_type);
  628. break;
  629. /* A P6-class processor */
  630. case 6:
  631. ppro_init(&cpu_type);
  632. break;
  633. default:
  634. break;
  635. }
  636. if (cpu_type)
  637. break;
  638. if (!boot_cpu_has(X86_FEATURE_ARCH_PERFMON))
  639. return -ENODEV;
  640. /* use arch perfmon as fallback */
  641. cpu_type = "i386/arch_perfmon";
  642. model = &op_arch_perfmon_spec;
  643. break;
  644. default:
  645. return -ENODEV;
  646. }
  647. /* default values, can be overwritten by model */
  648. ops->create_files = nmi_create_files;
  649. ops->setup = nmi_setup;
  650. ops->shutdown = nmi_shutdown;
  651. ops->start = nmi_start;
  652. ops->stop = nmi_stop;
  653. ops->cpu_type = cpu_type;
  654. if (model->init)
  655. ret = model->init(ops);
  656. if (ret)
  657. return ret;
  658. if (!model->num_virt_counters)
  659. model->num_virt_counters = model->num_counters;
  660. mux_init(ops);
  661. init_suspend_resume();
  662. printk(KERN_INFO "oprofile: using NMI interrupt.\n");
  663. return 0;
  664. }
  665. void op_nmi_exit(void)
  666. {
  667. exit_suspend_resume();
  668. }