arm_pmu_platform.c 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245
  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 <[email protected]>
  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 *dev, 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(dev->of_node))
  65. return i;
  66. dn = of_parse_phandle(dev->of_node, "interrupt-affinity", i);
  67. if (!dn) {
  68. dev_warn(dev, "failed to parse interrupt-affinity[%d]\n", i);
  69. return -EINVAL;
  70. }
  71. cpu = of_cpu_node_to_id(dn);
  72. if (cpu < 0) {
  73. dev_warn(dev, "failed to find logical CPU for %pOFn\n", dn);
  74. cpu = nr_cpu_ids;
  75. }
  76. of_node_put(dn);
  77. return cpu;
  78. }
  79. static int pmu_parse_irqs(struct arm_pmu *pmu)
  80. {
  81. int i = 0, num_irqs;
  82. struct platform_device *pdev = pmu->plat_device;
  83. struct pmu_hw_events __percpu *hw_events = pmu->hw_events;
  84. struct device *dev = &pdev->dev;
  85. num_irqs = platform_irq_count(pdev);
  86. if (num_irqs < 0)
  87. return dev_err_probe(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. dev_warn(dev, "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 > 0) && irq_is_percpu_devid(irq))
  101. return pmu_parse_percpu_irq(pmu, irq);
  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(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(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->plat_device = pdev;
  163. ret = pmu_parse_irqs(pmu);
  164. if (ret)
  165. goto out_free;
  166. init_fn = of_device_get_match_data(dev);
  167. if (init_fn) {
  168. pmu->secure_access = of_property_read_bool(dev->of_node,
  169. "secure-reg-access");
  170. /* arm64 systems boot only as non-secure */
  171. if (IS_ENABLED(CONFIG_ARM64) && pmu->secure_access) {
  172. dev_warn(dev, "ignoring \"secure-reg-access\" property for arm64\n");
  173. pmu->secure_access = false;
  174. }
  175. ret = init_fn(pmu);
  176. } else if (probe_table) {
  177. cpumask_setall(&pmu->supported_cpus);
  178. ret = probe_current_pmu(pmu, probe_table);
  179. }
  180. if (ret) {
  181. dev_err(dev, "failed to probe PMU!\n");
  182. goto out_free;
  183. }
  184. ret = armpmu_request_irqs(pmu);
  185. if (ret)
  186. goto out_free_irqs;
  187. ret = armpmu_register(pmu);
  188. if (ret) {
  189. dev_err(dev, "failed to register PMU devices!\n");
  190. goto out_free_irqs;
  191. }
  192. return 0;
  193. out_free_irqs:
  194. armpmu_free_irqs(pmu);
  195. out_free:
  196. armpmu_free(pmu);
  197. return ret;
  198. }