arm_pmu_platform.c 5.2 KB

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