perf_event_cpu.c 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319
  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 && 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 cpuid = read_cpuid_id();
  173. unsigned long implementor = (cpuid & 0xFF000000) >> 24;
  174. unsigned long part_number = (cpuid & 0xFFF0);
  175. int ret = -ENODEV;
  176. pr_info("probing PMU on CPU %d\n", cpu);
  177. /* ARM Ltd CPUs. */
  178. if (0x41 == implementor) {
  179. switch (part_number) {
  180. case 0xB360: /* ARM1136 */
  181. case 0xB560: /* ARM1156 */
  182. case 0xB760: /* ARM1176 */
  183. ret = armv6pmu_init(pmu);
  184. break;
  185. case 0xB020: /* ARM11mpcore */
  186. ret = armv6mpcore_pmu_init(pmu);
  187. break;
  188. case 0xC080: /* Cortex-A8 */
  189. ret = armv7_a8_pmu_init(pmu);
  190. break;
  191. case 0xC090: /* Cortex-A9 */
  192. ret = armv7_a9_pmu_init(pmu);
  193. break;
  194. case 0xC050: /* Cortex-A5 */
  195. ret = armv7_a5_pmu_init(pmu);
  196. break;
  197. case 0xC0F0: /* Cortex-A15 */
  198. ret = armv7_a15_pmu_init(pmu);
  199. break;
  200. case 0xC070: /* Cortex-A7 */
  201. ret = armv7_a7_pmu_init(pmu);
  202. break;
  203. }
  204. /* Intel CPUs [xscale]. */
  205. } else if (0x69 == implementor) {
  206. part_number = (cpuid >> 13) & 0x7;
  207. switch (part_number) {
  208. case 1:
  209. ret = xscale1pmu_init(pmu);
  210. break;
  211. case 2:
  212. ret = xscale2pmu_init(pmu);
  213. break;
  214. }
  215. }
  216. put_cpu();
  217. return ret;
  218. }
  219. static int cpu_pmu_device_probe(struct platform_device *pdev)
  220. {
  221. const struct of_device_id *of_id;
  222. int (*init_fn)(struct arm_pmu *);
  223. struct device_node *node = pdev->dev.of_node;
  224. struct arm_pmu *pmu;
  225. int ret = -ENODEV;
  226. if (cpu_pmu) {
  227. pr_info("attempt to register multiple PMU devices!");
  228. return -ENOSPC;
  229. }
  230. pmu = kzalloc(sizeof(struct arm_pmu), GFP_KERNEL);
  231. if (!pmu) {
  232. pr_info("failed to allocate PMU device!");
  233. return -ENOMEM;
  234. }
  235. if (node && (of_id = of_match_node(cpu_pmu_of_device_ids, pdev->dev.of_node))) {
  236. init_fn = of_id->data;
  237. ret = init_fn(pmu);
  238. } else {
  239. ret = probe_current_pmu(pmu);
  240. }
  241. if (ret) {
  242. pr_info("failed to register PMU devices!");
  243. kfree(pmu);
  244. return ret;
  245. }
  246. cpu_pmu = pmu;
  247. cpu_pmu->plat_device = pdev;
  248. cpu_pmu_init(cpu_pmu);
  249. armpmu_register(cpu_pmu, PERF_TYPE_RAW);
  250. return 0;
  251. }
  252. static struct platform_driver cpu_pmu_driver = {
  253. .driver = {
  254. .name = "arm-pmu",
  255. .pm = &armpmu_dev_pm_ops,
  256. .of_match_table = cpu_pmu_of_device_ids,
  257. },
  258. .probe = cpu_pmu_device_probe,
  259. .id_table = cpu_pmu_plat_device_ids,
  260. };
  261. static int __init register_pmu_driver(void)
  262. {
  263. int err;
  264. err = register_cpu_notifier(&cpu_pmu_hotplug_notifier);
  265. if (err)
  266. return err;
  267. err = platform_driver_register(&cpu_pmu_driver);
  268. if (err)
  269. unregister_cpu_notifier(&cpu_pmu_hotplug_notifier);
  270. return err;
  271. }
  272. device_initcall(register_pmu_driver);