nmi_int.c 17 KB

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