arm_pmu_platform.c 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226
  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. cpu = of_cpu_node_to_id(dn);
  72. if (cpu < 0) {
  73. pr_warn("failed to find logical CPU for %s\n", dn->name);
  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. num_irqs = platform_irq_count(pdev);
  85. if (num_irqs < 0) {
  86. pr_err("unable to count PMU IRQs\n");
  87. return num_irqs;
  88. }
  89. /*
  90. * In this case we have no idea which CPUs are covered by the PMU.
  91. * To match our prior behaviour, we assume all CPUs in this case.
  92. */
  93. if (num_irqs == 0) {
  94. pr_warn("no irqs for PMU, sampling events not supported\n");
  95. pmu->pmu.capabilities |= PERF_PMU_CAP_NO_INTERRUPT;
  96. cpumask_setall(&pmu->supported_cpus);
  97. return 0;
  98. }
  99. if (num_irqs == 1) {
  100. int irq = platform_get_irq(pdev, 0);
  101. if (irq && irq_is_percpu_devid(irq))
  102. return pmu_parse_percpu_irq(pmu, irq);
  103. }
  104. if (!pmu_has_irq_affinity(pdev->dev.of_node)) {
  105. pr_warn("no interrupt-affinity property for %pOF, guessing.\n",
  106. pdev->dev.of_node);
  107. }
  108. /*
  109. * Some platforms have all PMU IRQs OR'd into a single IRQ, with a
  110. * special platdata function that attempts to demux them.
  111. */
  112. if (dev_get_platdata(&pdev->dev))
  113. cpumask_setall(&pmu->supported_cpus);
  114. for (i = 0; i < num_irqs; i++) {
  115. int cpu, irq;
  116. irq = platform_get_irq(pdev, i);
  117. if (WARN_ON(irq <= 0))
  118. continue;
  119. if (irq_is_percpu_devid(irq)) {
  120. pr_warn("multiple PPIs or mismatched SPI/PPI detected\n");
  121. return -EINVAL;
  122. }
  123. cpu = pmu_parse_irq_affinity(pdev->dev.of_node, i);
  124. if (cpu < 0)
  125. return cpu;
  126. if (cpu >= nr_cpu_ids)
  127. continue;
  128. if (per_cpu(hw_events->irq, cpu)) {
  129. pr_warn("multiple PMU IRQs for the same CPU detected\n");
  130. return -EINVAL;
  131. }
  132. per_cpu(hw_events->irq, cpu) = irq;
  133. cpumask_set_cpu(cpu, &pmu->supported_cpus);
  134. }
  135. return 0;
  136. }
  137. int arm_pmu_device_probe(struct platform_device *pdev,
  138. const struct of_device_id *of_table,
  139. const struct pmu_probe_info *probe_table)
  140. {
  141. const struct of_device_id *of_id;
  142. armpmu_init_fn init_fn;
  143. struct device_node *node = pdev->dev.of_node;
  144. struct arm_pmu *pmu;
  145. int ret = -ENODEV;
  146. pmu = armpmu_alloc();
  147. if (!pmu)
  148. return -ENOMEM;
  149. pmu->plat_device = pdev;
  150. ret = pmu_parse_irqs(pmu);
  151. if (ret)
  152. goto out_free;
  153. if (node && (of_id = of_match_node(of_table, pdev->dev.of_node))) {
  154. init_fn = of_id->data;
  155. pmu->secure_access = of_property_read_bool(pdev->dev.of_node,
  156. "secure-reg-access");
  157. /* arm64 systems boot only as non-secure */
  158. if (IS_ENABLED(CONFIG_ARM64) && pmu->secure_access) {
  159. pr_warn("ignoring \"secure-reg-access\" property for arm64\n");
  160. pmu->secure_access = false;
  161. }
  162. ret = init_fn(pmu);
  163. } else if (probe_table) {
  164. cpumask_setall(&pmu->supported_cpus);
  165. ret = probe_current_pmu(pmu, probe_table);
  166. }
  167. if (ret) {
  168. pr_info("%pOF: failed to probe PMU!\n", node);
  169. goto out_free;
  170. }
  171. ret = armpmu_request_irqs(pmu);
  172. if (ret)
  173. goto out_free_irqs;
  174. ret = armpmu_register(pmu);
  175. if (ret)
  176. goto out_free;
  177. return 0;
  178. out_free_irqs:
  179. armpmu_free_irqs(pmu);
  180. out_free:
  181. pr_info("%pOF: failed to register PMU devices!\n", node);
  182. armpmu_free(pmu);
  183. return ret;
  184. }