perf_event_amd_ibs.c 6.0 KB

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