~ [ source navigation ] ~ [ diff markup ] ~ [ identifier search ] ~

TOMOYO Linux Cross Reference
Linux/arch/arm/kernel/perf_event_cpu.c

Version: ~ [ linux-5.16-rc3 ] ~ [ linux-5.15.5 ] ~ [ linux-5.14.21 ] ~ [ linux-5.13.19 ] ~ [ linux-5.12.19 ] ~ [ linux-5.11.22 ] ~ [ linux-5.10.82 ] ~ [ linux-5.9.16 ] ~ [ linux-5.8.18 ] ~ [ linux-5.7.19 ] ~ [ linux-5.6.19 ] ~ [ linux-5.5.19 ] ~ [ linux-5.4.162 ] ~ [ linux-5.3.18 ] ~ [ linux-5.2.21 ] ~ [ linux-5.1.21 ] ~ [ linux-5.0.21 ] ~ [ linux-4.20.17 ] ~ [ linux-4.19.218 ] ~ [ linux-4.18.20 ] ~ [ linux-4.17.19 ] ~ [ linux-4.16.18 ] ~ [ linux-4.15.18 ] ~ [ linux-4.14.256 ] ~ [ linux-4.13.16 ] ~ [ linux-4.12.14 ] ~ [ linux-4.11.12 ] ~ [ linux-4.10.17 ] ~ [ linux-4.9.291 ] ~ [ linux-4.8.17 ] ~ [ linux-4.7.10 ] ~ [ linux-4.6.7 ] ~ [ linux-4.5.7 ] ~ [ linux-4.4.293 ] ~ [ linux-4.3.6 ] ~ [ linux-4.2.8 ] ~ [ linux-4.1.52 ] ~ [ linux-4.0.9 ] ~ [ linux-3.18.140 ] ~ [ linux-3.16.85 ] ~ [ linux-3.14.79 ] ~ [ linux-3.12.74 ] ~ [ linux-3.10.108 ] ~ [ linux-2.6.32.71 ] ~ [ linux-2.6.0 ] ~ [ linux-2.4.37.11 ] ~ [ unix-v6-master ] ~ [ ccs-tools-1.8.5 ] ~ [ policy-sample ] ~
Architecture: ~ [ i386 ] ~ [ alpha ] ~ [ m68k ] ~ [ mips ] ~ [ ppc ] ~ [ sparc ] ~ [ sparc64 ] ~

  1 /*
  2  * This program is free software; you can redistribute it and/or modify
  3  * it under the terms of the GNU General Public License version 2 as
  4  * published by the Free Software Foundation.
  5  *
  6  * This program is distributed in the hope that it will be useful,
  7  * but WITHOUT ANY WARRANTY; without even the implied warranty of
  8  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  9  * GNU General Public License for more details.
 10  *
 11  * You should have received a copy of the GNU General Public License
 12  * along with this program; if not, write to the Free Software
 13  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
 14  *
 15  * Copyright (C) 2012 ARM Limited
 16  *
 17  * Author: Will Deacon <will.deacon@arm.com>
 18  */
 19 #define pr_fmt(fmt) "CPU PMU: " fmt
 20 
 21 #include <linux/bitmap.h>
 22 #include <linux/export.h>
 23 #include <linux/kernel.h>
 24 #include <linux/of.h>
 25 #include <linux/platform_device.h>
 26 #include <linux/slab.h>
 27 #include <linux/spinlock.h>
 28 #include <linux/irq.h>
 29 #include <linux/irqdesc.h>
 30 
 31 #include <asm/cputype.h>
 32 #include <asm/irq_regs.h>
 33 #include <asm/pmu.h>
 34 
 35 /* Set at runtime when we know what CPU type we are. */
 36 static struct arm_pmu *cpu_pmu;
 37 
 38 /*
 39  * Despite the names, these two functions are CPU-specific and are used
 40  * by the OProfile/perf code.
 41  */
 42 const char *perf_pmu_name(void)
 43 {
 44         if (!cpu_pmu)
 45                 return NULL;
 46 
 47         return cpu_pmu->name;
 48 }
 49 EXPORT_SYMBOL_GPL(perf_pmu_name);
 50 
 51 int perf_num_counters(void)
 52 {
 53         int max_events = 0;
 54 
 55         if (cpu_pmu != NULL)
 56                 max_events = cpu_pmu->num_events;
 57 
 58         return max_events;
 59 }
 60 EXPORT_SYMBOL_GPL(perf_num_counters);
 61 
 62 /* Include the PMU-specific implementations. */
 63 #include "perf_event_xscale.c"
 64 #include "perf_event_v6.c"
 65 #include "perf_event_v7.c"
 66 
 67 static void cpu_pmu_enable_percpu_irq(void *data)
 68 {
 69         int irq = *(int *)data;
 70 
 71         enable_percpu_irq(irq, IRQ_TYPE_NONE);
 72 }
 73 
 74 static void cpu_pmu_disable_percpu_irq(void *data)
 75 {
 76         int irq = *(int *)data;
 77 
 78         disable_percpu_irq(irq);
 79 }
 80 
 81 static void cpu_pmu_free_irq(struct arm_pmu *cpu_pmu)
 82 {
 83         int i, irq, irqs;
 84         struct platform_device *pmu_device = cpu_pmu->plat_device;
 85         struct pmu_hw_events __percpu *hw_events = cpu_pmu->hw_events;
 86 
 87         irqs = min(pmu_device->num_resources, num_possible_cpus());
 88 
 89         irq = platform_get_irq(pmu_device, 0);
 90         if (irq >= 0 && irq_is_percpu(irq)) {
 91                 on_each_cpu(cpu_pmu_disable_percpu_irq, &irq, 1);
 92                 free_percpu_irq(irq, &hw_events->percpu_pmu);
 93         } else {
 94                 for (i = 0; i < irqs; ++i) {
 95                         if (!cpumask_test_and_clear_cpu(i, &cpu_pmu->active_irqs))
 96                                 continue;
 97                         irq = platform_get_irq(pmu_device, i);
 98                         if (irq >= 0)
 99                                 free_irq(irq, per_cpu_ptr(&hw_events->percpu_pmu, i));
100                 }
101         }
102 }
103 
104 static int cpu_pmu_request_irq(struct arm_pmu *cpu_pmu, irq_handler_t handler)
105 {
106         int i, err, irq, irqs;
107         struct platform_device *pmu_device = cpu_pmu->plat_device;
108         struct pmu_hw_events __percpu *hw_events = cpu_pmu->hw_events;
109 
110         if (!pmu_device)
111                 return -ENODEV;
112 
113         irqs = min(pmu_device->num_resources, num_possible_cpus());
114         if (irqs < 1) {
115                 pr_warn_once("perf/ARM: No irqs for PMU defined, sampling events not supported\n");
116                 return 0;
117         }
118 
119         irq = platform_get_irq(pmu_device, 0);
120         if (irq >= 0 && irq_is_percpu(irq)) {
121                 err = request_percpu_irq(irq, handler, "arm-pmu",
122                                          &hw_events->percpu_pmu);
123                 if (err) {
124                         pr_err("unable to request IRQ%d for ARM PMU counters\n",
125                                 irq);
126                         return err;
127                 }
128                 on_each_cpu(cpu_pmu_enable_percpu_irq, &irq, 1);
129         } else {
130                 for (i = 0; i < irqs; ++i) {
131                         err = 0;
132                         irq = platform_get_irq(pmu_device, i);
133                         if (irq < 0)
134                                 continue;
135 
136                         /*
137                          * If we have a single PMU interrupt that we can't shift,
138                          * assume that we're running on a uniprocessor machine and
139                          * continue. Otherwise, continue without this interrupt.
140                          */
141                         if (irq_set_affinity(irq, cpumask_of(i)) && irqs > 1) {
142                                 pr_warn("unable to set irq affinity (irq=%d, cpu=%u)\n",
143                                         irq, i);
144                                 continue;
145                         }
146 
147                         err = request_irq(irq, handler,
148                                           IRQF_NOBALANCING | IRQF_NO_THREAD, "arm-pmu",
149                                           per_cpu_ptr(&hw_events->percpu_pmu, i));
150                         if (err) {
151                                 pr_err("unable to request IRQ%d for ARM PMU counters\n",
152                                         irq);
153                                 return err;
154                         }
155 
156                         cpumask_set_cpu(i, &cpu_pmu->active_irqs);
157                 }
158         }
159 
160         return 0;
161 }
162 
163 /*
164  * PMU hardware loses all context when a CPU goes offline.
165  * When a CPU is hotplugged back in, since some hardware registers are
166  * UNKNOWN at reset, the PMU must be explicitly reset to avoid reading
167  * junk values out of them.
168  */
169 static int cpu_pmu_notify(struct notifier_block *b, unsigned long action,
170                           void *hcpu)
171 {
172         struct arm_pmu *pmu = container_of(b, struct arm_pmu, hotplug_nb);
173 
174         if ((action & ~CPU_TASKS_FROZEN) != CPU_STARTING)
175                 return NOTIFY_DONE;
176 
177         if (pmu->reset)
178                 pmu->reset(pmu);
179         else
180                 return NOTIFY_DONE;
181 
182         return NOTIFY_OK;
183 }
184 
185 static int cpu_pmu_init(struct arm_pmu *cpu_pmu)
186 {
187         int err;
188         int cpu;
189         struct pmu_hw_events __percpu *cpu_hw_events;
190 
191         cpu_hw_events = alloc_percpu(struct pmu_hw_events);
192         if (!cpu_hw_events)
193                 return -ENOMEM;
194 
195         cpu_pmu->hotplug_nb.notifier_call = cpu_pmu_notify;
196         err = register_cpu_notifier(&cpu_pmu->hotplug_nb);
197         if (err)
198                 goto out_hw_events;
199 
200         for_each_possible_cpu(cpu) {
201                 struct pmu_hw_events *events = per_cpu_ptr(cpu_hw_events, cpu);
202                 raw_spin_lock_init(&events->pmu_lock);
203                 events->percpu_pmu = cpu_pmu;
204         }
205 
206         cpu_pmu->hw_events      = cpu_hw_events;
207         cpu_pmu->request_irq    = cpu_pmu_request_irq;
208         cpu_pmu->free_irq       = cpu_pmu_free_irq;
209 
210         /* Ensure the PMU has sane values out of reset. */
211         if (cpu_pmu->reset)
212                 on_each_cpu(cpu_pmu->reset, cpu_pmu, 1);
213 
214         /* If no interrupts available, set the corresponding capability flag */
215         if (!platform_get_irq(cpu_pmu->plat_device, 0))
216                 cpu_pmu->pmu.capabilities |= PERF_PMU_CAP_NO_INTERRUPT;
217 
218         return 0;
219 
220 out_hw_events:
221         free_percpu(cpu_hw_events);
222         return err;
223 }
224 
225 static void cpu_pmu_destroy(struct arm_pmu *cpu_pmu)
226 {
227         unregister_cpu_notifier(&cpu_pmu->hotplug_nb);
228         free_percpu(cpu_pmu->hw_events);
229 }
230 
231 /*
232  * PMU platform driver and devicetree bindings.
233  */
234 static const struct of_device_id cpu_pmu_of_device_ids[] = {
235         {.compatible = "arm,cortex-a17-pmu",    .data = armv7_a17_pmu_init},
236         {.compatible = "arm,cortex-a15-pmu",    .data = armv7_a15_pmu_init},
237         {.compatible = "arm,cortex-a12-pmu",    .data = armv7_a12_pmu_init},
238         {.compatible = "arm,cortex-a9-pmu",     .data = armv7_a9_pmu_init},
239         {.compatible = "arm,cortex-a8-pmu",     .data = armv7_a8_pmu_init},
240         {.compatible = "arm,cortex-a7-pmu",     .data = armv7_a7_pmu_init},
241         {.compatible = "arm,cortex-a5-pmu",     .data = armv7_a5_pmu_init},
242         {.compatible = "arm,arm11mpcore-pmu",   .data = armv6mpcore_pmu_init},
243         {.compatible = "arm,arm1176-pmu",       .data = armv6_1176_pmu_init},
244         {.compatible = "arm,arm1136-pmu",       .data = armv6_1136_pmu_init},
245         {.compatible = "qcom,krait-pmu",        .data = krait_pmu_init},
246         {},
247 };
248 
249 static struct platform_device_id cpu_pmu_plat_device_ids[] = {
250         {.name = "arm-pmu"},
251         {.name = "armv6-pmu"},
252         {.name = "armv7-pmu"},
253         {.name = "xscale-pmu"},
254         {},
255 };
256 
257 static const struct pmu_probe_info pmu_probe_table[] = {
258         ARM_PMU_PROBE(ARM_CPU_PART_ARM1136, armv6_1136_pmu_init),
259         ARM_PMU_PROBE(ARM_CPU_PART_ARM1156, armv6_1156_pmu_init),
260         ARM_PMU_PROBE(ARM_CPU_PART_ARM1176, armv6_1176_pmu_init),
261         ARM_PMU_PROBE(ARM_CPU_PART_ARM11MPCORE, armv6mpcore_pmu_init),
262         ARM_PMU_PROBE(ARM_CPU_PART_CORTEX_A8, armv7_a8_pmu_init),
263         ARM_PMU_PROBE(ARM_CPU_PART_CORTEX_A9, armv7_a9_pmu_init),
264         XSCALE_PMU_PROBE(ARM_CPU_XSCALE_ARCH_V1, xscale1pmu_init),
265         XSCALE_PMU_PROBE(ARM_CPU_XSCALE_ARCH_V2, xscale2pmu_init),
266         { /* sentinel value */ }
267 };
268 
269 /*
270  * CPU PMU identification and probing.
271  */
272 static int probe_current_pmu(struct arm_pmu *pmu)
273 {
274         int cpu = get_cpu();
275         unsigned int cpuid = read_cpuid_id();
276         int ret = -ENODEV;
277         const struct pmu_probe_info *info;
278 
279         pr_info("probing PMU on CPU %d\n", cpu);
280 
281         for (info = pmu_probe_table; info->init != NULL; info++) {
282                 if ((cpuid & info->mask) != info->cpuid)
283                         continue;
284                 ret = info->init(pmu);
285                 break;
286         }
287 
288         put_cpu();
289         return ret;
290 }
291 
292 static int cpu_pmu_device_probe(struct platform_device *pdev)
293 {
294         const struct of_device_id *of_id;
295         const int (*init_fn)(struct arm_pmu *);
296         struct device_node *node = pdev->dev.of_node;
297         struct arm_pmu *pmu;
298         int ret = -ENODEV;
299 
300         if (cpu_pmu) {
301                 pr_info("attempt to register multiple PMU devices!\n");
302                 return -ENOSPC;
303         }
304 
305         pmu = kzalloc(sizeof(struct arm_pmu), GFP_KERNEL);
306         if (!pmu) {
307                 pr_info("failed to allocate PMU device!\n");
308                 return -ENOMEM;
309         }
310 
311         cpu_pmu = pmu;
312         cpu_pmu->plat_device = pdev;
313 
314         if (node && (of_id = of_match_node(cpu_pmu_of_device_ids, pdev->dev.of_node))) {
315                 init_fn = of_id->data;
316                 ret = init_fn(pmu);
317         } else {
318                 ret = probe_current_pmu(pmu);
319         }
320 
321         if (ret) {
322                 pr_info("failed to probe PMU!\n");
323                 goto out_free;
324         }
325 
326         ret = cpu_pmu_init(cpu_pmu);
327         if (ret)
328                 goto out_free;
329 
330         ret = armpmu_register(cpu_pmu, -1);
331         if (ret)
332                 goto out_destroy;
333 
334         return 0;
335 
336 out_destroy:
337         cpu_pmu_destroy(cpu_pmu);
338 out_free:
339         pr_info("failed to register PMU devices!\n");
340         kfree(pmu);
341         return ret;
342 }
343 
344 static struct platform_driver cpu_pmu_driver = {
345         .driver         = {
346                 .name   = "arm-pmu",
347                 .pm     = &armpmu_dev_pm_ops,
348                 .of_match_table = cpu_pmu_of_device_ids,
349         },
350         .probe          = cpu_pmu_device_probe,
351         .id_table       = cpu_pmu_plat_device_ids,
352 };
353 
354 static int __init register_pmu_driver(void)
355 {
356         return platform_driver_register(&cpu_pmu_driver);
357 }
358 device_initcall(register_pmu_driver);
359 

~ [ source navigation ] ~ [ diff markup ] ~ [ identifier search ] ~

kernel.org | git.kernel.org | LWN.net | Project Home | Wiki (Japanese) | Wiki (English) | SVN repository | Mail admin

Linux® is a registered trademark of Linus Torvalds in the United States and other countries.
TOMOYO® is a registered trademark of NTT DATA CORPORATION.

osdn.jp