perf_event_amd_ibs.c 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339
  1. /*
  2. * Performance events - AMD IBS
  3. *
  4. * Copyright (C) 2011 Advanced Micro Devices, Inc., Robert Richter
  5. *
  6. * For licencing details see kernel-base/COPYING
  7. */
  8. #include <linux/perf_event.h>
  9. #include <linux/module.h>
  10. #include <linux/pci.h>
  11. #include <linux/syscore_ops.h>
  12. #include <asm/apic.h>
  13. static u32 ibs_caps;
  14. #if defined(CONFIG_PERF_EVENTS) && defined(CONFIG_CPU_SUP_AMD)
  15. static struct pmu perf_ibs;
  16. static int perf_ibs_init(struct perf_event *event)
  17. {
  18. if (perf_ibs.type != event->attr.type)
  19. return -ENOENT;
  20. return 0;
  21. }
  22. static int perf_ibs_add(struct perf_event *event, int flags)
  23. {
  24. return 0;
  25. }
  26. static void perf_ibs_del(struct perf_event *event, int flags)
  27. {
  28. }
  29. static struct pmu perf_ibs = {
  30. .event_init= perf_ibs_init,
  31. .add= perf_ibs_add,
  32. .del= perf_ibs_del,
  33. };
  34. static __init int perf_event_ibs_init(void)
  35. {
  36. if (!ibs_caps)
  37. return -ENODEV; /* ibs not supported by the cpu */
  38. perf_pmu_register(&perf_ibs, "ibs", -1);
  39. printk(KERN_INFO "perf: AMD IBS detected (0x%08x)\n", ibs_caps);
  40. return 0;
  41. }
  42. #else /* defined(CONFIG_PERF_EVENTS) && defined(CONFIG_CPU_SUP_AMD) */
  43. static __init int perf_event_ibs_init(void) { return 0; }
  44. #endif
  45. /* IBS - apic initialization, for perf and oprofile */
  46. static __init u32 __get_ibs_caps(void)
  47. {
  48. u32 caps;
  49. unsigned int max_level;
  50. if (!boot_cpu_has(X86_FEATURE_IBS))
  51. return 0;
  52. /* check IBS cpuid feature flags */
  53. max_level = cpuid_eax(0x80000000);
  54. if (max_level < IBS_CPUID_FEATURES)
  55. return IBS_CAPS_DEFAULT;
  56. caps = cpuid_eax(IBS_CPUID_FEATURES);
  57. if (!(caps & IBS_CAPS_AVAIL))
  58. /* cpuid flags not valid */
  59. return IBS_CAPS_DEFAULT;
  60. return caps;
  61. }
  62. u32 get_ibs_caps(void)
  63. {
  64. return ibs_caps;
  65. }
  66. EXPORT_SYMBOL(get_ibs_caps);
  67. static inline int get_eilvt(int offset)
  68. {
  69. return !setup_APIC_eilvt(offset, 0, APIC_EILVT_MSG_NMI, 1);
  70. }
  71. static inline int put_eilvt(int offset)
  72. {
  73. return !setup_APIC_eilvt(offset, 0, 0, 1);
  74. }
  75. /*
  76. * Check and reserve APIC extended interrupt LVT offset for IBS if available.
  77. */
  78. static inline int ibs_eilvt_valid(void)
  79. {
  80. int offset;
  81. u64 val;
  82. int valid = 0;
  83. preempt_disable();
  84. rdmsrl(MSR_AMD64_IBSCTL, val);
  85. offset = val & IBSCTL_LVT_OFFSET_MASK;
  86. if (!(val & IBSCTL_LVT_OFFSET_VALID)) {
  87. pr_err(FW_BUG "cpu %d, invalid IBS interrupt offset %d (MSR%08X=0x%016llx)\n",
  88. smp_processor_id(), offset, MSR_AMD64_IBSCTL, val);
  89. goto out;
  90. }
  91. if (!get_eilvt(offset)) {
  92. pr_err(FW_BUG "cpu %d, IBS interrupt offset %d not available (MSR%08X=0x%016llx)\n",
  93. smp_processor_id(), offset, MSR_AMD64_IBSCTL, val);
  94. goto out;
  95. }
  96. valid = 1;
  97. out:
  98. preempt_enable();
  99. return valid;
  100. }
  101. static int setup_ibs_ctl(int ibs_eilvt_off)
  102. {
  103. struct pci_dev *cpu_cfg;
  104. int nodes;
  105. u32 value = 0;
  106. nodes = 0;
  107. cpu_cfg = NULL;
  108. do {
  109. cpu_cfg = pci_get_device(PCI_VENDOR_ID_AMD,
  110. PCI_DEVICE_ID_AMD_10H_NB_MISC,
  111. cpu_cfg);
  112. if (!cpu_cfg)
  113. break;
  114. ++nodes;
  115. pci_write_config_dword(cpu_cfg, IBSCTL, ibs_eilvt_off
  116. | IBSCTL_LVT_OFFSET_VALID);
  117. pci_read_config_dword(cpu_cfg, IBSCTL, &value);
  118. if (value != (ibs_eilvt_off | IBSCTL_LVT_OFFSET_VALID)) {
  119. pci_dev_put(cpu_cfg);
  120. printk(KERN_DEBUG "Failed to setup IBS LVT offset, "
  121. "IBSCTL = 0x%08x\n", value);
  122. return -EINVAL;
  123. }
  124. } while (1);
  125. if (!nodes) {
  126. printk(KERN_DEBUG "No CPU node configured for IBS\n");
  127. return -ENODEV;
  128. }
  129. return 0;
  130. }
  131. /*
  132. * This runs only on the current cpu. We try to find an LVT offset and
  133. * setup the local APIC. For this we must disable preemption. On
  134. * success we initialize all nodes with this offset. This updates then
  135. * the offset in the IBS_CTL per-node msr. The per-core APIC setup of
  136. * the IBS interrupt vector is handled by perf_ibs_cpu_notifier that
  137. * is using the new offset.
  138. */
  139. static int force_ibs_eilvt_setup(void)
  140. {
  141. int offset;
  142. int ret;
  143. preempt_disable();
  144. /* find the next free available EILVT entry, skip offset 0 */
  145. for (offset = 1; offset < APIC_EILVT_NR_MAX; offset++) {
  146. if (get_eilvt(offset))
  147. break;
  148. }
  149. preempt_enable();
  150. if (offset == APIC_EILVT_NR_MAX) {
  151. printk(KERN_DEBUG "No EILVT entry available\n");
  152. return -EBUSY;
  153. }
  154. ret = setup_ibs_ctl(offset);
  155. if (ret)
  156. goto out;
  157. if (!ibs_eilvt_valid()) {
  158. ret = -EFAULT;
  159. goto out;
  160. }
  161. pr_info("IBS: LVT offset %d assigned\n", offset);
  162. return 0;
  163. out:
  164. preempt_disable();
  165. put_eilvt(offset);
  166. preempt_enable();
  167. return ret;
  168. }
  169. static void ibs_eilvt_setup(void)
  170. {
  171. /*
  172. * Force LVT offset assignment for family 10h: The offsets are
  173. * not assigned by the BIOS for this family, so the OS is
  174. * responsible for doing it. If the OS assignment fails, fall
  175. * back to BIOS settings and try to setup this.
  176. */
  177. if (boot_cpu_data.x86 == 0x10)
  178. force_ibs_eilvt_setup();
  179. }
  180. static inline int get_ibs_lvt_offset(void)
  181. {
  182. u64 val;
  183. rdmsrl(MSR_AMD64_IBSCTL, val);
  184. if (!(val & IBSCTL_LVT_OFFSET_VALID))
  185. return -EINVAL;
  186. return val & IBSCTL_LVT_OFFSET_MASK;
  187. }
  188. static void setup_APIC_ibs(void *dummy)
  189. {
  190. int offset;
  191. offset = get_ibs_lvt_offset();
  192. if (offset < 0)
  193. goto failed;
  194. if (!setup_APIC_eilvt(offset, 0, APIC_EILVT_MSG_NMI, 0))
  195. return;
  196. failed:
  197. pr_warn("perf: IBS APIC setup failed on cpu #%d\n",
  198. smp_processor_id());
  199. }
  200. static void clear_APIC_ibs(void *dummy)
  201. {
  202. int offset;
  203. offset = get_ibs_lvt_offset();
  204. if (offset >= 0)
  205. setup_APIC_eilvt(offset, 0, APIC_EILVT_MSG_FIX, 1);
  206. }
  207. #ifdef CONFIG_PM
  208. static int perf_ibs_suspend(void)
  209. {
  210. clear_APIC_ibs(NULL);
  211. return 0;
  212. }
  213. static void perf_ibs_resume(void)
  214. {
  215. ibs_eilvt_setup();
  216. setup_APIC_ibs(NULL);
  217. }
  218. static struct syscore_ops perf_ibs_syscore_ops = {
  219. .resume = perf_ibs_resume,
  220. .suspend = perf_ibs_suspend,
  221. };
  222. static void perf_ibs_pm_init(void)
  223. {
  224. register_syscore_ops(&perf_ibs_syscore_ops);
  225. }
  226. #else
  227. static inline void perf_ibs_pm_init(void) { }
  228. #endif
  229. static int __cpuinit
  230. perf_ibs_cpu_notifier(struct notifier_block *self, unsigned long action, void *hcpu)
  231. {
  232. switch (action & ~CPU_TASKS_FROZEN) {
  233. case CPU_STARTING:
  234. setup_APIC_ibs(NULL);
  235. break;
  236. case CPU_DYING:
  237. clear_APIC_ibs(NULL);
  238. break;
  239. default:
  240. break;
  241. }
  242. return NOTIFY_OK;
  243. }
  244. static __init int amd_ibs_init(void)
  245. {
  246. u32 caps;
  247. int ret = -EINVAL;
  248. caps = __get_ibs_caps();
  249. if (!caps)
  250. return -ENODEV; /* ibs not supported by the cpu */
  251. ibs_eilvt_setup();
  252. if (!ibs_eilvt_valid())
  253. goto out;
  254. perf_ibs_pm_init();
  255. get_online_cpus();
  256. ibs_caps = caps;
  257. /* make ibs_caps visible to other cpus: */
  258. smp_mb();
  259. perf_cpu_notifier(perf_ibs_cpu_notifier);
  260. smp_call_function(setup_APIC_ibs, NULL, 1);
  261. put_online_cpus();
  262. ret = perf_event_ibs_init();
  263. out:
  264. if (ret)
  265. pr_err("Failed to setup IBS, %d\n", ret);
  266. return ret;
  267. }
  268. /* Since we need the pci subsystem to init ibs we can't do this earlier: */
  269. device_initcall(amd_ibs_init);