arm_pmu_platform.c 5.5 KB

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