perf_event_cpu.c 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323
  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 <asm/cputype.h>
  28. #include <asm/irq_regs.h>
  29. #include <asm/pmu.h>
  30. /* Set at runtime when we know what CPU type we are. */
  31. static struct arm_pmu *cpu_pmu;
  32. static DEFINE_PER_CPU(struct perf_event * [ARMPMU_MAX_HWEVENTS], hw_events);
  33. static DEFINE_PER_CPU(unsigned long [BITS_TO_LONGS(ARMPMU_MAX_HWEVENTS)], used_mask);
  34. static DEFINE_PER_CPU(struct pmu_hw_events, cpu_hw_events);
  35. /*
  36. * Despite the names, these two functions are CPU-specific and are used
  37. * by the OProfile/perf code.
  38. */
  39. const char *perf_pmu_name(void)
  40. {
  41. if (!cpu_pmu)
  42. return NULL;
  43. return cpu_pmu->name;
  44. }
  45. EXPORT_SYMBOL_GPL(perf_pmu_name);
  46. int perf_num_counters(void)
  47. {
  48. int max_events = 0;
  49. if (cpu_pmu != NULL)
  50. max_events = cpu_pmu->num_events;
  51. return max_events;
  52. }
  53. EXPORT_SYMBOL_GPL(perf_num_counters);
  54. /* Include the PMU-specific implementations. */
  55. #include "perf_event_xscale.c"
  56. #include "perf_event_v6.c"
  57. #include "perf_event_v7.c"
  58. static struct pmu_hw_events *cpu_pmu_get_cpu_events(void)
  59. {
  60. return &__get_cpu_var(cpu_hw_events);
  61. }
  62. static void cpu_pmu_free_irq(struct arm_pmu *cpu_pmu)
  63. {
  64. int i, irq, irqs;
  65. struct platform_device *pmu_device = cpu_pmu->plat_device;
  66. irqs = min(pmu_device->num_resources, num_possible_cpus());
  67. for (i = 0; i < irqs; ++i) {
  68. if (!cpumask_test_and_clear_cpu(i, &cpu_pmu->active_irqs))
  69. continue;
  70. irq = platform_get_irq(pmu_device, i);
  71. if (irq >= 0)
  72. free_irq(irq, cpu_pmu);
  73. }
  74. }
  75. static int cpu_pmu_request_irq(struct arm_pmu *cpu_pmu, irq_handler_t handler)
  76. {
  77. int i, err, irq, irqs;
  78. struct platform_device *pmu_device = cpu_pmu->plat_device;
  79. if (!pmu_device)
  80. return -ENODEV;
  81. irqs = min(pmu_device->num_resources, num_possible_cpus());
  82. if (irqs < 1) {
  83. pr_err("no irqs for PMUs defined\n");
  84. return -ENODEV;
  85. }
  86. for (i = 0; i < irqs; ++i) {
  87. err = 0;
  88. irq = platform_get_irq(pmu_device, i);
  89. if (irq < 0)
  90. continue;
  91. /*
  92. * If we have a single PMU interrupt that we can't shift,
  93. * assume that we're running on a uniprocessor machine and
  94. * continue. Otherwise, continue without this interrupt.
  95. */
  96. if (irq_set_affinity(irq, cpumask_of(i)) && irqs > 1) {
  97. pr_warning("unable to set irq affinity (irq=%d, cpu=%u)\n",
  98. irq, i);
  99. continue;
  100. }
  101. err = request_irq(irq, handler,
  102. IRQF_NOBALANCING | IRQF_NO_THREAD, "arm-pmu",
  103. cpu_pmu);
  104. if (err) {
  105. pr_err("unable to request IRQ%d for ARM PMU counters\n",
  106. irq);
  107. return err;
  108. }
  109. cpumask_set_cpu(i, &cpu_pmu->active_irqs);
  110. }
  111. return 0;
  112. }
  113. static void cpu_pmu_init(struct arm_pmu *cpu_pmu)
  114. {
  115. int cpu;
  116. for_each_possible_cpu(cpu) {
  117. struct pmu_hw_events *events = &per_cpu(cpu_hw_events, cpu);
  118. events->events = per_cpu(hw_events, cpu);
  119. events->used_mask = per_cpu(used_mask, cpu);
  120. raw_spin_lock_init(&events->pmu_lock);
  121. }
  122. cpu_pmu->get_hw_events = cpu_pmu_get_cpu_events;
  123. cpu_pmu->request_irq = cpu_pmu_request_irq;
  124. cpu_pmu->free_irq = cpu_pmu_free_irq;
  125. /* Ensure the PMU has sane values out of reset. */
  126. if (cpu_pmu->reset)
  127. on_each_cpu(cpu_pmu->reset, cpu_pmu, 1);
  128. }
  129. /*
  130. * PMU hardware loses all context when a CPU goes offline.
  131. * When a CPU is hotplugged back in, since some hardware registers are
  132. * UNKNOWN at reset, the PMU must be explicitly reset to avoid reading
  133. * junk values out of them.
  134. */
  135. static int cpu_pmu_notify(struct notifier_block *b, unsigned long action,
  136. void *hcpu)
  137. {
  138. if ((action & ~CPU_TASKS_FROZEN) != CPU_STARTING)
  139. return NOTIFY_DONE;
  140. if (cpu_pmu && cpu_pmu->reset)
  141. cpu_pmu->reset(cpu_pmu);
  142. else
  143. return NOTIFY_DONE;
  144. return NOTIFY_OK;
  145. }
  146. static struct notifier_block cpu_pmu_hotplug_notifier = {
  147. .notifier_call = cpu_pmu_notify,
  148. };
  149. /*
  150. * PMU platform driver and devicetree bindings.
  151. */
  152. static struct of_device_id cpu_pmu_of_device_ids[] = {
  153. {.compatible = "arm,cortex-a15-pmu", .data = armv7_a15_pmu_init},
  154. {.compatible = "arm,cortex-a9-pmu", .data = armv7_a9_pmu_init},
  155. {.compatible = "arm,cortex-a8-pmu", .data = armv7_a8_pmu_init},
  156. {.compatible = "arm,cortex-a7-pmu", .data = armv7_a7_pmu_init},
  157. {.compatible = "arm,cortex-a5-pmu", .data = armv7_a5_pmu_init},
  158. {.compatible = "arm,arm11mpcore-pmu", .data = armv6mpcore_pmu_init},
  159. {.compatible = "arm,arm1176-pmu", .data = armv6pmu_init},
  160. {.compatible = "arm,arm1136-pmu", .data = armv6pmu_init},
  161. {},
  162. };
  163. static struct platform_device_id cpu_pmu_plat_device_ids[] = {
  164. {.name = "arm-pmu"},
  165. {},
  166. };
  167. /*
  168. * CPU PMU identification and probing.
  169. */
  170. static int probe_current_pmu(struct arm_pmu *pmu)
  171. {
  172. int cpu = get_cpu();
  173. unsigned long implementor = read_cpuid_implementor();
  174. unsigned long part_number = read_cpuid_part_number();
  175. int ret = -ENODEV;
  176. pr_info("probing PMU on CPU %d\n", cpu);
  177. /* ARM Ltd CPUs. */
  178. if (implementor == ARM_CPU_IMP_ARM) {
  179. switch (part_number) {
  180. case ARM_CPU_PART_ARM1136:
  181. case ARM_CPU_PART_ARM1156:
  182. case ARM_CPU_PART_ARM1176:
  183. ret = armv6pmu_init(pmu);
  184. break;
  185. case ARM_CPU_PART_ARM11MPCORE:
  186. ret = armv6mpcore_pmu_init(pmu);
  187. break;
  188. case ARM_CPU_PART_CORTEX_A8:
  189. ret = armv7_a8_pmu_init(pmu);
  190. break;
  191. case ARM_CPU_PART_CORTEX_A9:
  192. ret = armv7_a9_pmu_init(pmu);
  193. break;
  194. case ARM_CPU_PART_CORTEX_A5:
  195. ret = armv7_a5_pmu_init(pmu);
  196. break;
  197. case ARM_CPU_PART_CORTEX_A15:
  198. ret = armv7_a15_pmu_init(pmu);
  199. break;
  200. case ARM_CPU_PART_CORTEX_A7:
  201. ret = armv7_a7_pmu_init(pmu);
  202. break;
  203. }
  204. /* Intel CPUs [xscale]. */
  205. } else if (implementor == ARM_CPU_IMP_INTEL) {
  206. switch (xscale_cpu_arch_version()) {
  207. case ARM_CPU_XSCALE_ARCH_V1:
  208. ret = xscale1pmu_init(pmu);
  209. break;
  210. case ARM_CPU_XSCALE_ARCH_V2:
  211. ret = xscale2pmu_init(pmu);
  212. break;
  213. }
  214. }
  215. put_cpu();
  216. return ret;
  217. }
  218. static int cpu_pmu_device_probe(struct platform_device *pdev)
  219. {
  220. const struct of_device_id *of_id;
  221. int (*init_fn)(struct arm_pmu *);
  222. struct device_node *node = pdev->dev.of_node;
  223. struct arm_pmu *pmu;
  224. int ret = -ENODEV;
  225. if (cpu_pmu) {
  226. pr_info("attempt to register multiple PMU devices!");
  227. return -ENOSPC;
  228. }
  229. pmu = kzalloc(sizeof(struct arm_pmu), GFP_KERNEL);
  230. if (!pmu) {
  231. pr_info("failed to allocate PMU device!");
  232. return -ENOMEM;
  233. }
  234. if (node && (of_id = of_match_node(cpu_pmu_of_device_ids, pdev->dev.of_node))) {
  235. init_fn = of_id->data;
  236. ret = init_fn(pmu);
  237. } else {
  238. ret = probe_current_pmu(pmu);
  239. }
  240. if (ret) {
  241. pr_info("failed to probe PMU!");
  242. goto out_free;
  243. }
  244. cpu_pmu = pmu;
  245. cpu_pmu->plat_device = pdev;
  246. cpu_pmu_init(cpu_pmu);
  247. ret = armpmu_register(cpu_pmu, PERF_TYPE_RAW);
  248. if (!ret)
  249. return 0;
  250. out_free:
  251. pr_info("failed to register PMU devices!");
  252. kfree(pmu);
  253. return ret;
  254. }
  255. static struct platform_driver cpu_pmu_driver = {
  256. .driver = {
  257. .name = "arm-pmu",
  258. .pm = &armpmu_dev_pm_ops,
  259. .of_match_table = cpu_pmu_of_device_ids,
  260. },
  261. .probe = cpu_pmu_device_probe,
  262. .id_table = cpu_pmu_plat_device_ids,
  263. };
  264. static int __init register_pmu_driver(void)
  265. {
  266. int err;
  267. err = register_cpu_notifier(&cpu_pmu_hotplug_notifier);
  268. if (err)
  269. return err;
  270. err = platform_driver_register(&cpu_pmu_driver);
  271. if (err)
  272. unregister_cpu_notifier(&cpu_pmu_hotplug_notifier);
  273. return err;
  274. }
  275. device_initcall(register_pmu_driver);