arm_pmu_platform.c 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * platform_device probing code for ARM performance counters.
  4. *
  5. * Copyright (C) 2009 picoChip Designs, Ltd., Jamie Iles
  6. * Copyright (C) 2010 ARM Ltd., Will Deacon <will.deacon@arm.com>
  7. */
  8. #define pr_fmt(fmt) "hw perfevents: " fmt
  9. #define dev_fmt pr_fmt
  10. #include <linux/bug.h>
  11. #include <linux/cpumask.h>
  12. #include <linux/device.h>
  13. #include <linux/errno.h>
  14. #include <linux/irq.h>
  15. #include <linux/irqdesc.h>
  16. #include <linux/kconfig.h>
  17. #include <linux/of.h>
  18. #include <linux/of_device.h>
  19. #include <linux/percpu.h>
  20. #include <linux/perf/arm_pmu.h>
  21. #include <linux/platform_device.h>
  22. #include <linux/printk.h>
  23. #include <linux/smp.h>
  24. static int probe_current_pmu(struct arm_pmu *pmu,
  25. const struct pmu_probe_info *info)
  26. {
  27. int cpu = get_cpu();
  28. unsigned int cpuid = read_cpuid_id();
  29. int ret = -ENODEV;
  30. pr_info("probing PMU on CPU %d\n", cpu);
  31. for (; info->init != NULL; info++) {
  32. if ((cpuid & info->mask) != info->cpuid)
  33. continue;
  34. ret = info->init(pmu);
  35. break;
  36. }
  37. put_cpu();
  38. return ret;
  39. }
  40. static int pmu_parse_percpu_irq(struct arm_pmu *pmu, int irq)
  41. {
  42. int cpu, ret;
  43. struct pmu_hw_events __percpu *hw_events = pmu->hw_events;
  44. ret = irq_get_percpu_devid_partition(irq, &pmu->supported_cpus);
  45. if (ret)
  46. return ret;
  47. for_each_cpu(cpu, &pmu->supported_cpus)
  48. per_cpu(hw_events->irq, cpu) = irq;
  49. return 0;
  50. }
  51. static bool pmu_has_irq_affinity(struct device_node *node)
  52. {
  53. return !!of_find_property(node, "interrupt-affinity", NULL);
  54. }
  55. static int pmu_parse_irq_affinity(struct device_node *node, int i)
  56. {
  57. struct device_node *dn;
  58. int cpu;
  59. /*
  60. * If we don't have an interrupt-affinity property, we guess irq
  61. * affinity matches our logical CPU order, as we used to assume.
  62. * This is fragile, so we'll warn in pmu_parse_irqs().
  63. */
  64. if (!pmu_has_irq_affinity(node))
  65. return i;
  66. dn = of_parse_phandle(node, "interrupt-affinity", i);
  67. if (!dn) {
  68. pr_warn("failed to parse interrupt-affinity[%d] for %pOFn\n",
  69. i, node);
  70. return -EINVAL;
  71. }
  72. cpu = of_cpu_node_to_id(dn);
  73. if (cpu < 0) {
  74. pr_warn("failed to find logical CPU for %pOFn\n", dn);
  75. cpu = nr_cpu_ids;
  76. }
  77. of_node_put(dn);
  78. return cpu;
  79. }
  80. static int pmu_parse_irqs(struct arm_pmu *pmu)
  81. {
  82. int i = 0, num_irqs;
  83. struct platform_device *pdev = pmu->plat_device;
  84. struct pmu_hw_events __percpu *hw_events = pmu->hw_events;
  85. num_irqs = platform_irq_count(pdev);
  86. if (num_irqs < 0)
  87. return dev_err_probe(&pdev->dev, num_irqs, "unable to count PMU IRQs\n");
  88. /*
  89. * In this case we have no idea which CPUs are covered by the PMU.
  90. * To match our prior behaviour, we assume all CPUs in this case.
  91. */
  92. if (num_irqs == 0) {
  93. pr_warn("no irqs for PMU, sampling events not supported\n");
  94. pmu->pmu.capabilities |= PERF_PMU_CAP_NO_INTERRUPT;
  95. cpumask_setall(&pmu->supported_cpus);
  96. return 0;
  97. }
  98. if (num_irqs == 1) {
  99. int irq = platform_get_irq(pdev, 0);
  100. if (irq && irq_is_percpu_devid(irq))
  101. return pmu_parse_percpu_irq(pmu, irq);
  102. }
  103. if (nr_cpu_ids != 1 && !pmu_has_irq_affinity(pdev->dev.of_node)) {
  104. pr_warn("no interrupt-affinity property for %pOF, guessing.\n",
  105. pdev->dev.of_node);
  106. }
  107. for (i = 0; i < num_irqs; i++) {
  108. int cpu, irq;
  109. irq = platform_get_irq(pdev, i);
  110. if (WARN_ON(irq <= 0))
  111. continue;
  112. if (irq_is_percpu_devid(irq)) {
  113. pr_warn("multiple PPIs or mismatched SPI/PPI detected\n");
  114. return -EINVAL;
  115. }
  116. cpu = pmu_parse_irq_affinity(pdev->dev.of_node, i);
  117. if (cpu < 0)
  118. return cpu;
  119. if (cpu >= nr_cpu_ids)
  120. continue;
  121. if (per_cpu(hw_events->irq, cpu)) {
  122. pr_warn("multiple PMU IRQs for the same CPU detected\n");
  123. return -EINVAL;
  124. }
  125. per_cpu(hw_events->irq, cpu) = irq;
  126. cpumask_set_cpu(cpu, &pmu->supported_cpus);
  127. }
  128. return 0;
  129. }
  130. static int armpmu_request_irqs(struct arm_pmu *armpmu)
  131. {
  132. struct pmu_hw_events __percpu *hw_events = armpmu->hw_events;
  133. int cpu, err = 0;
  134. for_each_cpu(cpu, &armpmu->supported_cpus) {
  135. int irq = per_cpu(hw_events->irq, cpu);
  136. if (!irq)
  137. continue;
  138. err = armpmu_request_irq(irq, cpu);
  139. if (err)
  140. break;
  141. }
  142. return err;
  143. }
  144. static void armpmu_free_irqs(struct arm_pmu *armpmu)
  145. {
  146. int cpu;
  147. struct pmu_hw_events __percpu *hw_events = armpmu->hw_events;
  148. for_each_cpu(cpu, &armpmu->supported_cpus) {
  149. int irq = per_cpu(hw_events->irq, cpu);
  150. armpmu_free_irq(irq, cpu);
  151. }
  152. }
  153. int arm_pmu_device_probe(struct platform_device *pdev,
  154. const struct of_device_id *of_table,
  155. const struct pmu_probe_info *probe_table)
  156. {
  157. const struct of_device_id *of_id;
  158. armpmu_init_fn init_fn;
  159. struct device_node *node = pdev->dev.of_node;
  160. struct arm_pmu *pmu;
  161. int ret = -ENODEV;
  162. pmu = armpmu_alloc();
  163. if (!pmu)
  164. return -ENOMEM;
  165. pmu->plat_device = pdev;
  166. ret = pmu_parse_irqs(pmu);
  167. if (ret)
  168. goto out_free;
  169. if (node && (of_id = of_match_node(of_table, pdev->dev.of_node))) {
  170. init_fn = of_id->data;
  171. pmu->secure_access = of_property_read_bool(pdev->dev.of_node,
  172. "secure-reg-access");
  173. /* arm64 systems boot only as non-secure */
  174. if (IS_ENABLED(CONFIG_ARM64) && pmu->secure_access) {
  175. pr_warn("ignoring \"secure-reg-access\" property for arm64\n");
  176. pmu->secure_access = false;
  177. }
  178. ret = init_fn(pmu);
  179. } else if (probe_table) {
  180. cpumask_setall(&pmu->supported_cpus);
  181. ret = probe_current_pmu(pmu, probe_table);
  182. }
  183. if (ret) {
  184. pr_info("%pOF: failed to probe PMU!\n", node);
  185. goto out_free;
  186. }
  187. ret = armpmu_request_irqs(pmu);
  188. if (ret)
  189. goto out_free_irqs;
  190. ret = armpmu_register(pmu);
  191. if (ret)
  192. goto out_free_irqs;
  193. return 0;
  194. out_free_irqs:
  195. armpmu_free_irqs(pmu);
  196. out_free:
  197. pr_info("%pOF: failed to register PMU devices!\n", node);
  198. armpmu_free(pmu);
  199. return ret;
  200. }