]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - sys/arm/arm/pmu.c
Finish implementation of ARM PMU interrupts.
[FreeBSD/FreeBSD.git] / sys / arm / arm / pmu.c
1 /*-
2  * Copyright (c) 2015 Ruslan Bukin <br@bsdpad.com>
3  * All rights reserved.
4  *
5  * This software was developed by SRI International and the University of
6  * Cambridge Computer Laboratory under DARPA/AFRL contract (FA8750-10-C-0237)
7  * ("CTSRD"), as part of the DARPA CRASH research programme.
8  *
9  * Redistribution and use in source and binary forms, with or without
10  * modification, are permitted provided that the following conditions
11  * are met:
12  * 1. Redistributions of source code must retain the above copyright
13  *    notice, this list of conditions and the following disclaimer.
14  * 2. Redistributions in binary form must reproduce the above copyright
15  *    notice, this list of conditions and the following disclaimer in the
16  *    documentation and/or other materials provided with the distribution.
17  *
18  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
19  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
22  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
23  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
24  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
25  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
26  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
27  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
28  * SUCH DAMAGE.
29  */
30
31 /*
32  * Performance Monitoring Unit
33  */
34
35 #include <sys/cdefs.h>
36 __FBSDID("$FreeBSD$");
37
38 #include "opt_hwpmc_hooks.h"
39 #include "opt_platform.h"
40
41 #include <sys/param.h>
42 #include <sys/systm.h>
43 #include <sys/bus.h>
44 #include <sys/kernel.h>
45 #include <sys/module.h>
46 #include <sys/malloc.h>
47 #include <sys/rman.h>
48 #include <sys/timeet.h>
49 #include <sys/timetc.h>
50 #include <sys/pmc.h>
51 #include <sys/pmckern.h>
52
53 #ifdef FDT
54 #include <dev/ofw/openfirm.h>
55 #include <dev/ofw/ofw_bus.h>
56 #include <dev/ofw/ofw_bus_subr.h>
57 #endif
58
59 #include <machine/bus.h>
60 #include <machine/cpu.h>
61 #include <machine/intr.h>
62
63 #define MAX_RLEN        8
64
65 struct pmu_intr {
66         struct resource *res;
67         void            *ih;
68         int             cpuid;
69 };
70
71 struct pmu_softc {
72         device_t                dev;
73         struct pmu_intr         irq[MAX_RLEN];
74 };
75
76 /* CCNT */
77 #if __ARM_ARCH > 6
78 int pmu_attched = 0;
79 uint32_t ccnt_hi[MAXCPU];
80 #endif
81
82 #define PMU_OVSR_C              0x80000000      /* Cycle Counter */
83 #define PMU_IESR_C              0x80000000      /* Cycle Counter */
84
85 static int
86 pmu_intr(void *arg)
87 {
88 #ifdef HWPMC_HOOKS
89         struct trapframe *tf;
90 #endif
91         uint32_t r;
92 #if defined(__arm__) && (__ARM_ARCH > 6)
93         u_int cpu;
94
95         cpu = PCPU_GET(cpuid);
96
97         r = cp15_pmovsr_get();
98         if (r & PMU_OVSR_C) {
99                 atomic_add_32(&ccnt_hi[cpu], 1);
100                 /* Clear the event. */
101                 r &= ~PMU_OVSR_C;
102                 cp15_pmovsr_set(PMU_OVSR_C);
103         }
104 #else
105         r = 1;
106 #endif
107
108 #ifdef HWPMC_HOOKS
109         /* Only call into the HWPMC framework if we know there is work. */
110         if (r != 0 && pmc_intr) {
111                 tf = arg;
112                 (*pmc_intr)(tf);
113         }
114 #endif
115
116         return (FILTER_HANDLED);
117 }
118
119 static int
120 pmu_parse_affinity(struct pmu_softc *sc, struct pmu_intr *irq, phandle_t xref,
121     uint32_t mpidr)
122 {
123         struct pcpu *pcpu;
124         int i, err;
125
126
127         if (xref  != 0) {
128                 err = OF_getencprop(OF_node_from_xref(xref), "reg", &mpidr,
129                     sizeof(mpidr));
130                 if (err < 0) {
131                         device_printf(sc->dev, "missing 'reg' property\n");
132                                 return (ENXIO);
133                 }
134         }
135
136         for (i = 0; i < MAXCPU; i++) {
137                 pcpu = pcpu_find(i);
138                 if (pcpu != NULL && pcpu->pc_mpidr == mpidr) {
139                         irq->cpuid = i;
140                         return (0);
141                 }
142         }
143
144         device_printf(sc->dev, "Cannot find CPU with MPIDR: 0x%08X\n", mpidr);
145         return (ENXIO);
146 }
147
148 static int
149 pmu_parse_intr(struct pmu_softc *sc)
150 {
151         bool has_affinity;
152         phandle_t node, *cpus;
153         int rid, err, ncpus, i;
154
155
156         node = ofw_bus_get_node(sc->dev);
157         has_affinity = OF_hasprop(node, "interrupt-affinity");
158
159         for (i = 0; i < MAX_RLEN; i++)
160                 sc->irq[i].cpuid = -1;
161
162         cpus = NULL;
163         if (has_affinity) {
164                 ncpus = OF_getencprop_alloc_multi(node, "interrupt-affinity",
165                     sizeof(*cpus), (void **)&cpus);
166                 if (ncpus < 0) {
167                         device_printf(sc->dev,
168                            "Cannot read interrupt affinity property\n");
169                         return (ENXIO);
170                 }
171         }
172
173         /* Process first interrupt */
174         rid = 0;
175         sc->irq[0].res = bus_alloc_resource_any(sc->dev, SYS_RES_IRQ, &rid,
176             RF_ACTIVE | RF_SHAREABLE);
177
178         if (sc->irq[0].res == NULL) {
179                 device_printf(sc->dev, "Cannot get interrupt\n");
180                 err = ENXIO;
181                 goto done;
182         }
183
184         /* Check if PMU have one per-CPU interrupt */
185         if (intr_is_per_cpu(sc->irq[0].res)) {
186                 if (has_affinity) {
187                         device_printf(sc->dev,
188                             "Per CPU interupt have declared affinity\n");
189                         err = ENXIO;
190                         goto done;
191                 }
192                 return (0);
193         }
194
195         /*
196          * PMU with set of generic interrupts (one per core)
197          * Each one must be binded to exact core.
198          */
199         err = pmu_parse_affinity(sc, sc->irq + 0, has_affinity ? cpus[0]: 0,
200             0);
201         if (err != 0) {
202                 device_printf(sc->dev, "Cannot parse affinity for CPUid: 0\n");
203                 goto done;
204         }
205
206         for (i = 1; i < MAX_RLEN; i++) {
207                 rid = i;
208                 sc->irq[i].res = bus_alloc_resource_any(sc->dev, SYS_RES_IRQ,
209                     &rid, RF_ACTIVE | RF_SHAREABLE);
210                 if (sc->irq[i].res == NULL)
211                         break;
212
213                 if (intr_is_per_cpu(sc->irq[i].res))
214                 {
215                         device_printf(sc->dev, "Unexpected per CPU interupt\n");
216                         err = ENXIO;
217                         goto done;
218                 }
219
220                 if (has_affinity && i >= ncpus) {
221                         device_printf(sc->dev, "Missing value in interrupt "
222                             "affinity property\n");
223                         err = ENXIO;
224                         goto done;
225                 }
226
227                 err = pmu_parse_affinity(sc, sc->irq + i,
228                     has_affinity ? cpus[i]: 0, i);
229                 if (err != 0) {
230                         device_printf(sc->dev,
231                            "Cannot parse affinity for CPUid: %d.\n", i);
232                         goto done;
233                 }
234         }
235         err = 0;
236 done:
237         OF_prop_free(cpus);
238         return (err);
239 }
240
241 static int
242 pmu_attach(device_t dev)
243 {
244         struct pmu_softc *sc;
245 #if defined(__arm__) && (__ARM_ARCH > 6)
246         uint32_t iesr;
247 #endif
248         int err, i;
249
250         sc = device_get_softc(dev);
251         sc->dev = dev;
252
253         err = pmu_parse_intr(sc);
254         if (err != 0)
255                 return (err);
256
257         for (i = 0; i < MAX_RLEN; i++) {
258                 if (sc->irq[i].res == NULL)
259                         break;
260                 err = bus_setup_intr(dev, sc->irq[i].res,
261                     INTR_MPSAFE | INTR_TYPE_MISC, pmu_intr, NULL, NULL,
262                     &sc->irq[i].ih);
263                 if (err != 0) {
264                         device_printf(dev,
265                             "Unable to setup interrupt handler.\n");
266                         goto fail;
267                 }
268                 if (sc->irq[i].cpuid != -1) {
269                         err = bus_bind_intr(dev, sc->irq[i].res,
270                             sc->irq[i].cpuid);
271                         if (err != 0) {
272                                 device_printf(sc->dev,
273                                     "Unable to bind interrupt.\n");
274                                 goto fail;
275                         }
276                 }
277         }
278
279 #if defined(__arm__) && (__ARM_ARCH > 6)
280         /* Initialize to 0. */
281         for (i = 0; i < MAXCPU; i++)
282                 ccnt_hi[i] = 0;
283
284         /* Enable the interrupt to fire on overflow. */
285         iesr = cp15_pminten_get();
286         iesr |= PMU_IESR_C;
287         cp15_pminten_set(iesr);
288
289         /* Need this for getcyclecount() fast path. */
290         pmu_attched |= 1;
291 #endif
292
293         return (0);
294
295 fail:
296         for (i = 1; i < MAX_RLEN; i++) {
297                 if (sc->irq[i].ih != NULL)
298                         bus_teardown_intr(dev, sc->irq[i].res, sc->irq[i].ih);
299                 if (sc->irq[i].res != NULL)
300                         bus_release_resource(dev, SYS_RES_IRQ, i,
301                             sc->irq[i].res);
302         }
303         return(err);
304 }
305
306 #ifdef FDT
307 static struct ofw_compat_data compat_data[] = {
308         {"arm,armv8-pmuv3",     1},
309         {"arm,cortex-a77-pmu",  1},
310         {"arm,cortex-a76-pmu",  1},
311         {"arm,cortex-a75-pmu",  1},
312         {"arm,cortex-a73-pmu",  1},
313         {"arm,cortex-a72-pmu",  1},
314         {"arm,cortex-a65-pmu",  1},
315         {"arm,cortex-a57-pmu",  1},
316         {"arm,cortex-a55-pmu",  1},
317         {"arm,cortex-a53-pmu",  1},
318         {"arm,cortex-a34-pmu",  1},
319
320         {"arm,cortex-a17-pmu",  1},
321         {"arm,cortex-a15-pmu",  1},
322         {"arm,cortex-a12-pmu",  1},
323         {"arm,cortex-a9-pmu",   1},
324         {"arm,cortex-a8-pmu",   1},
325         {"arm,cortex-a7-pmu",   1},
326         {"arm,cortex-a5-pmu",   1},
327         {"arm,arm11mpcore-pmu", 1},
328         {"arm,arm1176-pmu",     1},
329         {"arm,arm1136-pmu",     1},
330         {"qcom,krait-pmu",      1},
331         {NULL,                  0}
332 };
333
334 static int
335 pmu_fdt_probe(device_t dev)
336 {
337
338         if (!ofw_bus_status_okay(dev))
339                 return (ENXIO);
340
341         if (ofw_bus_search_compatible(dev, compat_data)->ocd_data != 0) {
342                 device_set_desc(dev, "Performance Monitoring Unit");
343                 return (BUS_PROBE_DEFAULT);
344         }
345
346         return (ENXIO);
347 }
348
349 static device_method_t pmu_fdt_methods[] = {
350         DEVMETHOD(device_probe,         pmu_fdt_probe),
351         DEVMETHOD(device_attach,        pmu_attach),
352         { 0, 0 }
353 };
354
355 static driver_t pmu_fdt_driver = {
356         "pmu",
357         pmu_fdt_methods,
358         sizeof(struct pmu_softc),
359 };
360
361 static devclass_t pmu_fdt_devclass;
362
363 DRIVER_MODULE(pmu, simplebus, pmu_fdt_driver, pmu_fdt_devclass, 0, 0);
364 #endif