perf_event_cpu.c 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322
  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, IRQF_NOBALANCING, "arm-pmu",
  102. cpu_pmu);
  103. if (err) {
  104. pr_err("unable to request IRQ%d for ARM PMU counters\n",
  105. irq);
  106. return err;
  107. }
  108. cpumask_set_cpu(i, &cpu_pmu->active_irqs);
  109. }
  110. return 0;
  111. }
  112. static void cpu_pmu_init(struct arm_pmu *cpu_pmu)
  113. {
  114. int cpu;
  115. for_each_possible_cpu(cpu) {
  116. struct pmu_hw_events *events = &per_cpu(cpu_hw_events, cpu);
  117. events->events = per_cpu(hw_events, cpu);
  118. events->used_mask = per_cpu(used_mask, cpu);
  119. raw_spin_lock_init(&events->pmu_lock);
  120. }
  121. cpu_pmu->get_hw_events = cpu_pmu_get_cpu_events;
  122. cpu_pmu->request_irq = cpu_pmu_request_irq;
  123. cpu_pmu->free_irq = cpu_pmu_free_irq;
  124. /* Ensure the PMU has sane values out of reset. */
  125. if (cpu_pmu->reset)
  126. on_each_cpu(cpu_pmu->reset, cpu_pmu, 1);
  127. }
  128. /*
  129. * PMU hardware loses all context when a CPU goes offline.
  130. * When a CPU is hotplugged back in, since some hardware registers are
  131. * UNKNOWN at reset, the PMU must be explicitly reset to avoid reading
  132. * junk values out of them.
  133. */
  134. static int __cpuinit cpu_pmu_notify(struct notifier_block *b,
  135. unsigned long action, void *hcpu)
  136. {
  137. if ((action & ~CPU_TASKS_FROZEN) != CPU_STARTING)
  138. return NOTIFY_DONE;
  139. if (cpu_pmu && cpu_pmu->reset)
  140. cpu_pmu->reset(cpu_pmu);
  141. else
  142. return NOTIFY_DONE;
  143. return NOTIFY_OK;
  144. }
  145. static struct notifier_block __cpuinitdata cpu_pmu_hotplug_notifier = {
  146. .notifier_call = cpu_pmu_notify,
  147. };
  148. /*
  149. * PMU platform driver and devicetree bindings.
  150. */
  151. static struct of_device_id cpu_pmu_of_device_ids[] = {
  152. {.compatible = "arm,cortex-a15-pmu", .data = armv7_a15_pmu_init},
  153. {.compatible = "arm,cortex-a9-pmu", .data = armv7_a9_pmu_init},
  154. {.compatible = "arm,cortex-a8-pmu", .data = armv7_a8_pmu_init},
  155. {.compatible = "arm,cortex-a7-pmu", .data = armv7_a7_pmu_init},
  156. {.compatible = "arm,cortex-a5-pmu", .data = armv7_a5_pmu_init},
  157. {.compatible = "arm,arm11mpcore-pmu", .data = armv6mpcore_pmu_init},
  158. {.compatible = "arm,arm1176-pmu", .data = armv6pmu_init},
  159. {.compatible = "arm,arm1136-pmu", .data = armv6pmu_init},
  160. {},
  161. };
  162. static struct platform_device_id cpu_pmu_plat_device_ids[] = {
  163. {.name = "arm-pmu"},
  164. {},
  165. };
  166. /*
  167. * CPU PMU identification and probing.
  168. */
  169. static int probe_current_pmu(struct arm_pmu *pmu)
  170. {
  171. int cpu = get_cpu();
  172. unsigned long implementor = read_cpuid_implementor();
  173. unsigned long part_number = read_cpuid_part_number();
  174. int ret = -ENODEV;
  175. pr_info("probing PMU on CPU %d\n", cpu);
  176. /* ARM Ltd CPUs. */
  177. if (implementor == ARM_CPU_IMP_ARM) {
  178. switch (part_number) {
  179. case ARM_CPU_PART_ARM1136:
  180. case ARM_CPU_PART_ARM1156:
  181. case ARM_CPU_PART_ARM1176:
  182. ret = armv6pmu_init(pmu);
  183. break;
  184. case ARM_CPU_PART_ARM11MPCORE:
  185. ret = armv6mpcore_pmu_init(pmu);
  186. break;
  187. case ARM_CPU_PART_CORTEX_A8:
  188. ret = armv7_a8_pmu_init(pmu);
  189. break;
  190. case ARM_CPU_PART_CORTEX_A9:
  191. ret = armv7_a9_pmu_init(pmu);
  192. break;
  193. case ARM_CPU_PART_CORTEX_A5:
  194. ret = armv7_a5_pmu_init(pmu);
  195. break;
  196. case ARM_CPU_PART_CORTEX_A15:
  197. ret = armv7_a15_pmu_init(pmu);
  198. break;
  199. case ARM_CPU_PART_CORTEX_A7:
  200. ret = armv7_a7_pmu_init(pmu);
  201. break;
  202. }
  203. /* Intel CPUs [xscale]. */
  204. } else if (implementor == ARM_CPU_IMP_INTEL) {
  205. switch (xscale_cpu_arch_version()) {
  206. case ARM_CPU_XSCALE_ARCH_V1:
  207. ret = xscale1pmu_init(pmu);
  208. break;
  209. case ARM_CPU_XSCALE_ARCH_V2:
  210. ret = xscale2pmu_init(pmu);
  211. break;
  212. }
  213. }
  214. put_cpu();
  215. return ret;
  216. }
  217. static int cpu_pmu_device_probe(struct platform_device *pdev)
  218. {
  219. const struct of_device_id *of_id;
  220. int (*init_fn)(struct arm_pmu *);
  221. struct device_node *node = pdev->dev.of_node;
  222. struct arm_pmu *pmu;
  223. int ret = -ENODEV;
  224. if (cpu_pmu) {
  225. pr_info("attempt to register multiple PMU devices!");
  226. return -ENOSPC;
  227. }
  228. pmu = kzalloc(sizeof(struct arm_pmu), GFP_KERNEL);
  229. if (!pmu) {
  230. pr_info("failed to allocate PMU device!");
  231. return -ENOMEM;
  232. }
  233. if (node && (of_id = of_match_node(cpu_pmu_of_device_ids, pdev->dev.of_node))) {
  234. init_fn = of_id->data;
  235. ret = init_fn(pmu);
  236. } else {
  237. ret = probe_current_pmu(pmu);
  238. }
  239. if (ret) {
  240. pr_info("failed to probe PMU!");
  241. goto out_free;
  242. }
  243. cpu_pmu = pmu;
  244. cpu_pmu->plat_device = pdev;
  245. cpu_pmu_init(cpu_pmu);
  246. ret = armpmu_register(cpu_pmu, PERF_TYPE_RAW);
  247. if (!ret)
  248. return 0;
  249. out_free:
  250. pr_info("failed to register PMU devices!");
  251. kfree(pmu);
  252. return ret;
  253. }
  254. static struct platform_driver cpu_pmu_driver = {
  255. .driver = {
  256. .name = "arm-pmu",
  257. .pm = &armpmu_dev_pm_ops,
  258. .of_match_table = cpu_pmu_of_device_ids,
  259. },
  260. .probe = cpu_pmu_device_probe,
  261. .id_table = cpu_pmu_plat_device_ids,
  262. };
  263. static int __init register_pmu_driver(void)
  264. {
  265. int err;
  266. err = register_cpu_notifier(&cpu_pmu_hotplug_notifier);
  267. if (err)
  268. return err;
  269. err = platform_driver_register(&cpu_pmu_driver);
  270. if (err)
  271. unregister_cpu_notifier(&cpu_pmu_hotplug_notifier);
  272. return err;
  273. }
  274. device_initcall(register_pmu_driver);