perf_event_cpu.c 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363
  1. /*
  2. * This program is free software; you can redistribute it and/or modify
  3. * it under the terms of the GNU General Public License version 2 as
  4. * published by the Free Software Foundation.
  5. *
  6. * This program is distributed in the hope that it will be useful,
  7. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  8. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  9. * GNU General Public License for more details.
  10. *
  11. * You should have received a copy of the GNU General Public License
  12. * along with this program; if not, write to the Free Software
  13. * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
  14. *
  15. * Copyright (C) 2012 ARM Limited
  16. *
  17. * Author: Will Deacon <will.deacon@arm.com>
  18. */
  19. #define pr_fmt(fmt) "CPU PMU: " fmt
  20. #include <linux/bitmap.h>
  21. #include <linux/export.h>
  22. #include <linux/kernel.h>
  23. #include <linux/of.h>
  24. #include <linux/platform_device.h>
  25. #include <linux/slab.h>
  26. #include <linux/spinlock.h>
  27. #include <linux/irq.h>
  28. #include <linux/irqdesc.h>
  29. #include <asm/cputype.h>
  30. #include <asm/irq_regs.h>
  31. #include <asm/pmu.h>
  32. /* Set at runtime when we know what CPU type we are. */
  33. static struct arm_pmu *cpu_pmu;
  34. static DEFINE_PER_CPU(struct arm_pmu *, percpu_pmu);
  35. static DEFINE_PER_CPU(struct perf_event * [ARMPMU_MAX_HWEVENTS], hw_events);
  36. static DEFINE_PER_CPU(unsigned long [BITS_TO_LONGS(ARMPMU_MAX_HWEVENTS)], used_mask);
  37. static DEFINE_PER_CPU(struct pmu_hw_events, cpu_hw_events);
  38. /*
  39. * Despite the names, these two functions are CPU-specific and are used
  40. * by the OProfile/perf code.
  41. */
  42. const char *perf_pmu_name(void)
  43. {
  44. if (!cpu_pmu)
  45. return NULL;
  46. return cpu_pmu->name;
  47. }
  48. EXPORT_SYMBOL_GPL(perf_pmu_name);
  49. int perf_num_counters(void)
  50. {
  51. int max_events = 0;
  52. if (cpu_pmu != NULL)
  53. max_events = cpu_pmu->num_events;
  54. return max_events;
  55. }
  56. EXPORT_SYMBOL_GPL(perf_num_counters);
  57. /* Include the PMU-specific implementations. */
  58. #include "perf_event_xscale.c"
  59. #include "perf_event_v6.c"
  60. #include "perf_event_v7.c"
  61. static struct pmu_hw_events *cpu_pmu_get_cpu_events(void)
  62. {
  63. return this_cpu_ptr(&cpu_hw_events);
  64. }
  65. static void cpu_pmu_enable_percpu_irq(void *data)
  66. {
  67. struct arm_pmu *cpu_pmu = data;
  68. struct platform_device *pmu_device = cpu_pmu->plat_device;
  69. int irq = platform_get_irq(pmu_device, 0);
  70. enable_percpu_irq(irq, IRQ_TYPE_NONE);
  71. cpumask_set_cpu(smp_processor_id(), &cpu_pmu->active_irqs);
  72. }
  73. static void cpu_pmu_disable_percpu_irq(void *data)
  74. {
  75. struct arm_pmu *cpu_pmu = data;
  76. struct platform_device *pmu_device = cpu_pmu->plat_device;
  77. int irq = platform_get_irq(pmu_device, 0);
  78. cpumask_clear_cpu(smp_processor_id(), &cpu_pmu->active_irqs);
  79. disable_percpu_irq(irq);
  80. }
  81. static void cpu_pmu_free_irq(struct arm_pmu *cpu_pmu)
  82. {
  83. int i, irq, irqs;
  84. struct platform_device *pmu_device = cpu_pmu->plat_device;
  85. irqs = min(pmu_device->num_resources, num_possible_cpus());
  86. irq = platform_get_irq(pmu_device, 0);
  87. if (irq >= 0 && irq_is_percpu(irq)) {
  88. on_each_cpu(cpu_pmu_disable_percpu_irq, cpu_pmu, 1);
  89. free_percpu_irq(irq, &percpu_pmu);
  90. } else {
  91. for (i = 0; i < irqs; ++i) {
  92. if (!cpumask_test_and_clear_cpu(i, &cpu_pmu->active_irqs))
  93. continue;
  94. irq = platform_get_irq(pmu_device, i);
  95. if (irq >= 0)
  96. free_irq(irq, cpu_pmu);
  97. }
  98. }
  99. }
  100. static int cpu_pmu_request_irq(struct arm_pmu *cpu_pmu, irq_handler_t handler)
  101. {
  102. int i, err, irq, irqs;
  103. struct platform_device *pmu_device = cpu_pmu->plat_device;
  104. if (!pmu_device)
  105. return -ENODEV;
  106. irqs = min(pmu_device->num_resources, num_possible_cpus());
  107. if (irqs < 1) {
  108. printk_once("perf/ARM: No irqs for PMU defined, sampling events not supported\n");
  109. return 0;
  110. }
  111. irq = platform_get_irq(pmu_device, 0);
  112. if (irq >= 0 && irq_is_percpu(irq)) {
  113. err = request_percpu_irq(irq, handler, "arm-pmu", &percpu_pmu);
  114. if (err) {
  115. pr_err("unable to request IRQ%d for ARM PMU counters\n",
  116. irq);
  117. return err;
  118. }
  119. on_each_cpu(cpu_pmu_enable_percpu_irq, cpu_pmu, 1);
  120. } else {
  121. for (i = 0; i < irqs; ++i) {
  122. err = 0;
  123. irq = platform_get_irq(pmu_device, i);
  124. if (irq < 0)
  125. continue;
  126. /*
  127. * If we have a single PMU interrupt that we can't shift,
  128. * assume that we're running on a uniprocessor machine and
  129. * continue. Otherwise, continue without this interrupt.
  130. */
  131. if (irq_set_affinity(irq, cpumask_of(i)) && irqs > 1) {
  132. pr_warning("unable to set irq affinity (irq=%d, cpu=%u)\n",
  133. irq, i);
  134. continue;
  135. }
  136. err = request_irq(irq, handler,
  137. IRQF_NOBALANCING | IRQF_NO_THREAD, "arm-pmu",
  138. cpu_pmu);
  139. if (err) {
  140. pr_err("unable to request IRQ%d for ARM PMU counters\n",
  141. irq);
  142. return err;
  143. }
  144. cpumask_set_cpu(i, &cpu_pmu->active_irqs);
  145. }
  146. }
  147. return 0;
  148. }
  149. static void cpu_pmu_init(struct arm_pmu *cpu_pmu)
  150. {
  151. int cpu;
  152. for_each_possible_cpu(cpu) {
  153. struct pmu_hw_events *events = &per_cpu(cpu_hw_events, cpu);
  154. events->events = per_cpu(hw_events, cpu);
  155. events->used_mask = per_cpu(used_mask, cpu);
  156. raw_spin_lock_init(&events->pmu_lock);
  157. per_cpu(percpu_pmu, cpu) = cpu_pmu;
  158. }
  159. cpu_pmu->get_hw_events = cpu_pmu_get_cpu_events;
  160. cpu_pmu->request_irq = cpu_pmu_request_irq;
  161. cpu_pmu->free_irq = cpu_pmu_free_irq;
  162. /* Ensure the PMU has sane values out of reset. */
  163. if (cpu_pmu->reset)
  164. on_each_cpu(cpu_pmu->reset, cpu_pmu, 1);
  165. /* If no interrupts available, set the corresponding capability flag */
  166. if (!platform_get_irq(cpu_pmu->plat_device, 0))
  167. cpu_pmu->pmu.capabilities |= PERF_PMU_CAP_NO_INTERRUPT;
  168. }
  169. /*
  170. * PMU hardware loses all context when a CPU goes offline.
  171. * When a CPU is hotplugged back in, since some hardware registers are
  172. * UNKNOWN at reset, the PMU must be explicitly reset to avoid reading
  173. * junk values out of them.
  174. */
  175. static int cpu_pmu_notify(struct notifier_block *b, unsigned long action,
  176. void *hcpu)
  177. {
  178. if ((action & ~CPU_TASKS_FROZEN) != CPU_STARTING)
  179. return NOTIFY_DONE;
  180. if (cpu_pmu && cpu_pmu->reset)
  181. cpu_pmu->reset(cpu_pmu);
  182. else
  183. return NOTIFY_DONE;
  184. return NOTIFY_OK;
  185. }
  186. static struct notifier_block cpu_pmu_hotplug_notifier = {
  187. .notifier_call = cpu_pmu_notify,
  188. };
  189. /*
  190. * PMU platform driver and devicetree bindings.
  191. */
  192. static struct of_device_id cpu_pmu_of_device_ids[] = {
  193. {.compatible = "arm,cortex-a17-pmu", .data = armv7_a17_pmu_init},
  194. {.compatible = "arm,cortex-a15-pmu", .data = armv7_a15_pmu_init},
  195. {.compatible = "arm,cortex-a12-pmu", .data = armv7_a12_pmu_init},
  196. {.compatible = "arm,cortex-a9-pmu", .data = armv7_a9_pmu_init},
  197. {.compatible = "arm,cortex-a8-pmu", .data = armv7_a8_pmu_init},
  198. {.compatible = "arm,cortex-a7-pmu", .data = armv7_a7_pmu_init},
  199. {.compatible = "arm,cortex-a5-pmu", .data = armv7_a5_pmu_init},
  200. {.compatible = "arm,arm11mpcore-pmu", .data = armv6mpcore_pmu_init},
  201. {.compatible = "arm,arm1176-pmu", .data = armv6pmu_init},
  202. {.compatible = "arm,arm1136-pmu", .data = armv6pmu_init},
  203. {.compatible = "qcom,krait-pmu", .data = krait_pmu_init},
  204. {},
  205. };
  206. static struct platform_device_id cpu_pmu_plat_device_ids[] = {
  207. {.name = "arm-pmu"},
  208. {},
  209. };
  210. /*
  211. * CPU PMU identification and probing.
  212. */
  213. static int probe_current_pmu(struct arm_pmu *pmu)
  214. {
  215. int cpu = get_cpu();
  216. unsigned long implementor = read_cpuid_implementor();
  217. unsigned long part_number = read_cpuid_part_number();
  218. int ret = -ENODEV;
  219. pr_info("probing PMU on CPU %d\n", cpu);
  220. /* ARM Ltd CPUs. */
  221. if (implementor == ARM_CPU_IMP_ARM) {
  222. switch (part_number) {
  223. case ARM_CPU_PART_ARM1136:
  224. case ARM_CPU_PART_ARM1156:
  225. case ARM_CPU_PART_ARM1176:
  226. ret = armv6pmu_init(pmu);
  227. break;
  228. case ARM_CPU_PART_ARM11MPCORE:
  229. ret = armv6mpcore_pmu_init(pmu);
  230. break;
  231. case ARM_CPU_PART_CORTEX_A8:
  232. ret = armv7_a8_pmu_init(pmu);
  233. break;
  234. case ARM_CPU_PART_CORTEX_A9:
  235. ret = armv7_a9_pmu_init(pmu);
  236. break;
  237. }
  238. /* Intel CPUs [xscale]. */
  239. } else if (implementor == ARM_CPU_IMP_INTEL) {
  240. switch (xscale_cpu_arch_version()) {
  241. case ARM_CPU_XSCALE_ARCH_V1:
  242. ret = xscale1pmu_init(pmu);
  243. break;
  244. case ARM_CPU_XSCALE_ARCH_V2:
  245. ret = xscale2pmu_init(pmu);
  246. break;
  247. }
  248. }
  249. put_cpu();
  250. return ret;
  251. }
  252. static int cpu_pmu_device_probe(struct platform_device *pdev)
  253. {
  254. const struct of_device_id *of_id;
  255. const int (*init_fn)(struct arm_pmu *);
  256. struct device_node *node = pdev->dev.of_node;
  257. struct arm_pmu *pmu;
  258. int ret = -ENODEV;
  259. if (cpu_pmu) {
  260. pr_info("attempt to register multiple PMU devices!");
  261. return -ENOSPC;
  262. }
  263. pmu = kzalloc(sizeof(struct arm_pmu), GFP_KERNEL);
  264. if (!pmu) {
  265. pr_info("failed to allocate PMU device!");
  266. return -ENOMEM;
  267. }
  268. cpu_pmu = pmu;
  269. cpu_pmu->plat_device = pdev;
  270. if (node && (of_id = of_match_node(cpu_pmu_of_device_ids, pdev->dev.of_node))) {
  271. init_fn = of_id->data;
  272. ret = init_fn(pmu);
  273. } else {
  274. ret = probe_current_pmu(pmu);
  275. }
  276. if (ret) {
  277. pr_info("failed to probe PMU!");
  278. goto out_free;
  279. }
  280. cpu_pmu_init(cpu_pmu);
  281. ret = armpmu_register(cpu_pmu, PERF_TYPE_RAW);
  282. if (!ret)
  283. return 0;
  284. out_free:
  285. pr_info("failed to register PMU devices!");
  286. kfree(pmu);
  287. return ret;
  288. }
  289. static struct platform_driver cpu_pmu_driver = {
  290. .driver = {
  291. .name = "arm-pmu",
  292. .pm = &armpmu_dev_pm_ops,
  293. .of_match_table = cpu_pmu_of_device_ids,
  294. },
  295. .probe = cpu_pmu_device_probe,
  296. .id_table = cpu_pmu_plat_device_ids,
  297. };
  298. static int __init register_pmu_driver(void)
  299. {
  300. int err;
  301. err = register_cpu_notifier(&cpu_pmu_hotplug_notifier);
  302. if (err)
  303. return err;
  304. err = platform_driver_register(&cpu_pmu_driver);
  305. if (err)
  306. unregister_cpu_notifier(&cpu_pmu_hotplug_notifier);
  307. return err;
  308. }
  309. device_initcall(register_pmu_driver);