arm_pmu_platform.c 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236
  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. #include <linux/bug.h>
  10. #include <linux/cpumask.h>
  11. #include <linux/device.h>
  12. #include <linux/errno.h>
  13. #include <linux/irq.h>
  14. #include <linux/irqdesc.h>
  15. #include <linux/kconfig.h>
  16. #include <linux/of.h>
  17. #include <linux/of_device.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. {
  41. int cpu, ret;
  42. struct pmu_hw_events __percpu *hw_events = pmu->hw_events;
  43. ret = irq_get_percpu_devid_partition(irq, &pmu->supported_cpus);
  44. if (ret)
  45. return ret;
  46. for_each_cpu(cpu, &pmu->supported_cpus)
  47. per_cpu(hw_events->irq, cpu) = irq;
  48. return 0;
  49. }
  50. static bool pmu_has_irq_affinity(struct device_node *node)
  51. {
  52. return !!of_find_property(node, "interrupt-affinity", NULL);
  53. }
  54. static int pmu_parse_irq_affinity(struct device_node *node, int i)
  55. {
  56. struct device_node *dn;
  57. int cpu;
  58. /*
  59. * If we don't have an interrupt-affinity property, we guess irq
  60. * affinity matches our logical CPU order, as we used to assume.
  61. * This is fragile, so we'll warn in pmu_parse_irqs().
  62. */
  63. if (!pmu_has_irq_affinity(node))
  64. return i;
  65. dn = of_parse_phandle(node, "interrupt-affinity", i);
  66. if (!dn) {
  67. pr_warn("failed to parse interrupt-affinity[%d] for %s\n",
  68. i, node->name);
  69. return -EINVAL;
  70. }
  71. /* Now look up the logical CPU number */
  72. for_each_possible_cpu(cpu) {
  73. struct device_node *cpu_dn;
  74. cpu_dn = of_cpu_device_node_get(cpu);
  75. of_node_put(cpu_dn);
  76. if (dn == cpu_dn)
  77. break;
  78. }
  79. if (cpu >= nr_cpu_ids) {
  80. pr_warn("failed to find logical CPU for %s\n", dn->name);
  81. }
  82. of_node_put(dn);
  83. return cpu;
  84. }
  85. static int pmu_parse_irqs(struct arm_pmu *pmu)
  86. {
  87. int i = 0, num_irqs;
  88. struct platform_device *pdev = pmu->plat_device;
  89. struct pmu_hw_events __percpu *hw_events = pmu->hw_events;
  90. num_irqs = platform_irq_count(pdev);
  91. if (num_irqs < 0) {
  92. pr_err("unable to count PMU IRQs\n");
  93. return num_irqs;
  94. }
  95. /*
  96. * In this case we have no idea which CPUs are covered by the PMU.
  97. * To match our prior behaviour, we assume all CPUs in this case.
  98. */
  99. if (num_irqs == 0) {
  100. pr_warn("no irqs for PMU, sampling events not supported\n");
  101. pmu->pmu.capabilities |= PERF_PMU_CAP_NO_INTERRUPT;
  102. cpumask_setall(&pmu->supported_cpus);
  103. return 0;
  104. }
  105. if (num_irqs == 1) {
  106. int irq = platform_get_irq(pdev, 0);
  107. if (irq && irq_is_percpu(irq))
  108. return pmu_parse_percpu_irq(pmu, irq);
  109. }
  110. if (!pmu_has_irq_affinity(pdev->dev.of_node)) {
  111. pr_warn("no interrupt-affinity property for %pOF, guessing.\n",
  112. pdev->dev.of_node);
  113. }
  114. /*
  115. * Some platforms have all PMU IRQs OR'd into a single IRQ, with a
  116. * special platdata function that attempts to demux them.
  117. */
  118. if (dev_get_platdata(&pdev->dev))
  119. cpumask_setall(&pmu->supported_cpus);
  120. for (i = 0; i < num_irqs; i++) {
  121. int cpu, irq;
  122. irq = platform_get_irq(pdev, i);
  123. if (WARN_ON(irq <= 0))
  124. continue;
  125. if (irq_is_percpu(irq)) {
  126. pr_warn("multiple PPIs or mismatched SPI/PPI detected\n");
  127. return -EINVAL;
  128. }
  129. cpu = pmu_parse_irq_affinity(pdev->dev.of_node, i);
  130. if (cpu < 0)
  131. return cpu;
  132. if (cpu >= nr_cpu_ids)
  133. continue;
  134. if (per_cpu(hw_events->irq, cpu)) {
  135. pr_warn("multiple PMU IRQs for the same CPU detected\n");
  136. return -EINVAL;
  137. }
  138. per_cpu(hw_events->irq, cpu) = irq;
  139. cpumask_set_cpu(cpu, &pmu->supported_cpus);
  140. }
  141. return 0;
  142. }
  143. int arm_pmu_device_probe(struct platform_device *pdev,
  144. const struct of_device_id *of_table,
  145. const struct pmu_probe_info *probe_table)
  146. {
  147. const struct of_device_id *of_id;
  148. armpmu_init_fn init_fn;
  149. struct device_node *node = pdev->dev.of_node;
  150. struct arm_pmu *pmu;
  151. int ret = -ENODEV;
  152. pmu = armpmu_alloc();
  153. if (!pmu)
  154. return -ENOMEM;
  155. pmu->plat_device = pdev;
  156. ret = pmu_parse_irqs(pmu);
  157. if (ret)
  158. goto out_free;
  159. if (node && (of_id = of_match_node(of_table, pdev->dev.of_node))) {
  160. init_fn = of_id->data;
  161. pmu->secure_access = of_property_read_bool(pdev->dev.of_node,
  162. "secure-reg-access");
  163. /* arm64 systems boot only as non-secure */
  164. if (IS_ENABLED(CONFIG_ARM64) && pmu->secure_access) {
  165. pr_warn("ignoring \"secure-reg-access\" property for arm64\n");
  166. pmu->secure_access = false;
  167. }
  168. ret = init_fn(pmu);
  169. } else if (probe_table) {
  170. cpumask_setall(&pmu->supported_cpus);
  171. ret = probe_current_pmu(pmu, probe_table);
  172. }
  173. if (ret) {
  174. pr_info("%pOF: failed to probe PMU!\n", node);
  175. goto out_free;
  176. }
  177. ret = armpmu_request_irqs(pmu);
  178. if (ret)
  179. goto out_free_irqs;
  180. ret = armpmu_register(pmu);
  181. if (ret)
  182. goto out_free_irqs;
  183. return 0;
  184. out_free_irqs:
  185. armpmu_free_irqs(pmu);
  186. out_free:
  187. pr_info("%pOF: failed to register PMU devices!\n", node);
  188. armpmu_free(pmu);
  189. return ret;
  190. }