2 * Copyright (c) 2015 Ruslan Bukin <br@bsdpad.com>
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.
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions
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.
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
31 #include <sys/cdefs.h>
32 __FBSDID("$FreeBSD$");
34 #include <sys/param.h>
35 #include <sys/systm.h>
37 #include <sys/pmckern.h>
39 #include <machine/pmc_mdep.h>
40 #include <machine/cpu.h>
42 static int armv7_npmcs;
44 struct armv7_event_code_map {
49 #define PMC_EV_CPU_CYCLES 0xFF
52 * Per-processor information.
55 struct pmc_hw *pc_armv7pmcs;
58 static struct armv7_cpu **armv7_pcpu;
61 * Interrupt Enable Set Register
64 armv7_interrupt_enable(uint32_t pmc)
69 cp15_pminten_set(reg);
73 * Interrupt Clear Set Register
76 armv7_interrupt_disable(uint32_t pmc)
81 cp15_pminten_clr(reg);
85 * Counter Set Enable Register
88 armv7_counter_enable(unsigned int pmc)
93 cp15_pmcnten_set(reg);
97 * Counter Clear Enable Register
100 armv7_counter_disable(unsigned int pmc)
105 cp15_pmcnten_clr(reg);
109 * Performance Count Register N
112 armv7_pmcn_read(unsigned int pmc)
115 KASSERT(pmc < armv7_npmcs, ("%s: illegal PMC number %d", __func__, pmc));
117 cp15_pmselr_set(pmc);
118 return (cp15_pmxevcntr_get());
122 armv7_pmcn_write(unsigned int pmc, uint32_t reg)
125 KASSERT(pmc < armv7_npmcs, ("%s: illegal PMC number %d", __func__, pmc));
127 cp15_pmselr_set(pmc);
128 cp15_pmxevcntr_set(reg);
134 armv7_allocate_pmc(int cpu, int ri, struct pmc *pm,
135 const struct pmc_op_pmcallocate *a)
137 struct armv7_cpu *pac;
142 KASSERT(cpu >= 0 && cpu < pmc_cpu_max(),
143 ("[armv7,%d] illegal CPU value %d", __LINE__, cpu));
144 KASSERT(ri >= 0 && ri < armv7_npmcs,
145 ("[armv7,%d] illegal row index %d", __LINE__, ri));
147 pac = armv7_pcpu[cpu];
150 if (a->pm_class != PMC_CLASS_ARMV7)
154 config = (pe & EVENT_ID_MASK);
155 pm->pm_md.pm_armv7.pm_armv7_evsel = config;
157 PMCDBG2(MDP, ALL, 2, "armv7-allocate ri=%d -> config=0x%x", ri, config);
164 armv7_read_pmc(int cpu, int ri, pmc_value_t *v)
169 KASSERT(cpu >= 0 && cpu < pmc_cpu_max(),
170 ("[armv7,%d] illegal CPU value %d", __LINE__, cpu));
171 KASSERT(ri >= 0 && ri < armv7_npmcs,
172 ("[armv7,%d] illegal row index %d", __LINE__, ri));
174 pm = armv7_pcpu[cpu]->pc_armv7pmcs[ri].phw_pmc;
176 if (pm->pm_md.pm_armv7.pm_armv7_evsel == PMC_EV_CPU_CYCLES)
177 tmp = (uint32_t)cp15_pmccntr_get();
179 tmp = armv7_pmcn_read(ri);
180 tmp += 0x100000000llu * pm->pm_overflowcnt;
182 PMCDBG2(MDP, REA, 2, "armv7-read id=%d -> %jd", ri, tmp);
183 if (PMC_IS_SAMPLING_MODE(PMC_TO_MODE(pm)))
184 *v = ARMV7_PERFCTR_VALUE_TO_RELOAD_COUNT(tmp);
192 armv7_write_pmc(int cpu, int ri, pmc_value_t v)
196 KASSERT(cpu >= 0 && cpu < pmc_cpu_max(),
197 ("[armv7,%d] illegal CPU value %d", __LINE__, cpu));
198 KASSERT(ri >= 0 && ri < armv7_npmcs,
199 ("[armv7,%d] illegal row-index %d", __LINE__, ri));
201 pm = armv7_pcpu[cpu]->pc_armv7pmcs[ri].phw_pmc;
203 if (PMC_IS_SAMPLING_MODE(PMC_TO_MODE(pm)))
204 v = ARMV7_RELOAD_COUNT_TO_PERFCTR_VALUE(v);
206 PMCDBG3(MDP, WRI, 1, "armv7-write cpu=%d ri=%d v=%jx", cpu, ri, v);
208 if (pm->pm_md.pm_armv7.pm_armv7_evsel == PMC_EV_CPU_CYCLES)
211 armv7_pmcn_write(ri, v);
217 armv7_config_pmc(int cpu, int ri, struct pmc *pm)
221 PMCDBG3(MDP, CFG, 1, "cpu=%d ri=%d pm=%p", cpu, ri, pm);
223 KASSERT(cpu >= 0 && cpu < pmc_cpu_max(),
224 ("[armv7,%d] illegal CPU value %d", __LINE__, cpu));
225 KASSERT(ri >= 0 && ri < armv7_npmcs,
226 ("[armv7,%d] illegal row-index %d", __LINE__, ri));
228 phw = &armv7_pcpu[cpu]->pc_armv7pmcs[ri];
230 KASSERT(pm == NULL || phw->phw_pmc == NULL,
231 ("[armv7,%d] pm=%p phw->pm=%p hwpmc not unconfigured",
232 __LINE__, pm, phw->phw_pmc));
240 armv7_start_pmc(int cpu, int ri)
246 phw = &armv7_pcpu[cpu]->pc_armv7pmcs[ri];
248 config = pm->pm_md.pm_armv7.pm_armv7_evsel;
250 pm->pm_overflowcnt = 0;
253 * Configure the event selection.
255 if (config != PMC_EV_CPU_CYCLES) {
257 cp15_pmxevtyper_set(config);
264 armv7_interrupt_enable(ri);
265 armv7_counter_enable(ri);
271 armv7_stop_pmc(int cpu, int ri)
277 phw = &armv7_pcpu[cpu]->pc_armv7pmcs[ri];
279 config = pm->pm_md.pm_armv7.pm_armv7_evsel;
280 if (config == PMC_EV_CPU_CYCLES)
286 armv7_counter_disable(ri);
287 armv7_interrupt_disable(ri);
293 armv7_release_pmc(int cpu, int ri, struct pmc *pmc)
297 KASSERT(cpu >= 0 && cpu < pmc_cpu_max(),
298 ("[armv7,%d] illegal CPU value %d", __LINE__, cpu));
299 KASSERT(ri >= 0 && ri < armv7_npmcs,
300 ("[armv7,%d] illegal row-index %d", __LINE__, ri));
302 phw = &armv7_pcpu[cpu]->pc_armv7pmcs[ri];
303 KASSERT(phw->phw_pmc == NULL,
304 ("[armv7,%d] PHW pmc %p non-NULL", __LINE__, phw->phw_pmc));
310 armv7_intr(struct trapframe *tf)
312 struct armv7_cpu *pc;
319 KASSERT(cpu >= 0 && cpu < pmc_cpu_max(),
320 ("[armv7,%d] CPU %d out of range", __LINE__, cpu));
323 pc = armv7_pcpu[cpu];
325 for (ri = 0; ri < armv7_npmcs; ri++) {
326 pm = armv7_pcpu[cpu]->pc_armv7pmcs[ri].phw_pmc;
330 /* Check if counter has overflowed */
331 if (pm->pm_md.pm_armv7.pm_armv7_evsel == PMC_EV_CPU_CYCLES)
336 if ((cp15_pmovsr_get() & reg) == 0) {
340 /* Clear Overflow Flag */
341 cp15_pmovsr_set(reg);
343 retval = 1; /* Found an interrupting PMC. */
345 if (!PMC_IS_SAMPLING_MODE(PMC_TO_MODE(pm))) {
346 pm->pm_overflowcnt += 1;
349 if (pm->pm_state != PMC_STATE_RUNNING)
352 error = pmc_process_interrupt(PMC_HR, pm, tf);
354 armv7_stop_pmc(cpu, ri);
356 /* Reload sampling count */
357 armv7_write_pmc(cpu, ri, pm->pm_sc.pm_reloadcount);
364 armv7_describe(int cpu, int ri, struct pmc_info *pi, struct pmc **ppmc)
366 char armv7_name[PMC_NAME_MAX];
370 KASSERT(cpu >= 0 && cpu < pmc_cpu_max(),
371 ("[armv7,%d], illegal CPU %d", __LINE__, cpu));
372 KASSERT(ri >= 0 && ri < armv7_npmcs,
373 ("[armv7,%d] row-index %d out of range", __LINE__, ri));
375 phw = &armv7_pcpu[cpu]->pc_armv7pmcs[ri];
376 snprintf(armv7_name, sizeof(armv7_name), "ARMV7-%d", ri);
377 if ((error = copystr(armv7_name, pi->pm_name, PMC_NAME_MAX,
380 pi->pm_class = PMC_CLASS_ARMV7;
381 if (phw->phw_state & PMC_PHW_FLAG_IS_ENABLED) {
382 pi->pm_enabled = TRUE;
383 *ppmc = phw->phw_pmc;
385 pi->pm_enabled = FALSE;
393 armv7_get_config(int cpu, int ri, struct pmc **ppm)
396 *ppm = armv7_pcpu[cpu]->pc_armv7pmcs[ri].phw_pmc;
402 * XXX don't know what we should do here.
405 armv7_switch_in(struct pmc_cpu *pc, struct pmc_process *pp)
412 armv7_switch_out(struct pmc_cpu *pc, struct pmc_process *pp)
419 armv7_pcpu_init(struct pmc_mdep *md, int cpu)
421 struct armv7_cpu *pac;
428 KASSERT(cpu >= 0 && cpu < pmc_cpu_max(),
429 ("[armv7,%d] wrong cpu number %d", __LINE__, cpu));
430 PMCDBG1(MDP, INI, 1, "armv7-init cpu=%d", cpu);
432 armv7_pcpu[cpu] = pac = malloc(sizeof(struct armv7_cpu), M_PMC,
435 pac->pc_armv7pmcs = malloc(sizeof(struct pmc_hw) * armv7_npmcs,
436 M_PMC, M_WAITOK|M_ZERO);
438 first_ri = md->pmd_classdep[PMC_MDEP_CLASS_INDEX_ARMV7].pcd_ri;
439 KASSERT(pc != NULL, ("[armv7,%d] NULL per-cpu pointer", __LINE__));
441 for (i = 0, phw = pac->pc_armv7pmcs; i < armv7_npmcs; i++, phw++) {
442 phw->phw_state = PMC_PHW_FLAG_IS_ENABLED |
443 PMC_PHW_CPU_TO_STATE(cpu) | PMC_PHW_INDEX_TO_STATE(i);
445 pc->pc_hwpmcs[i + first_ri] = phw;
449 cp15_pmcnten_clr(pmnc);
450 cp15_pminten_clr(pmnc);
451 cp15_pmovsr_set(pmnc);
454 pmnc = cp15_pmcr_get();
455 pmnc |= ARMV7_PMNC_ENABLE;
462 armv7_pcpu_fini(struct pmc_mdep *md, int cpu)
466 pmnc = cp15_pmcr_get();
467 pmnc &= ~ARMV7_PMNC_ENABLE;
471 cp15_pmcnten_clr(pmnc);
472 cp15_pminten_clr(pmnc);
473 cp15_pmovsr_set(pmnc);
479 pmc_armv7_initialize()
481 struct pmc_mdep *pmc_mdep;
482 struct pmc_classdep *pcd;
486 reg = cp15_pmcr_get();
487 armv7_npmcs = (reg >> ARMV7_PMNC_N_SHIFT) & \
489 idcode = (reg & ARMV7_IDCODE_MASK) >> ARMV7_IDCODE_SHIFT;
491 PMCDBG1(MDP, INI, 1, "armv7-init npmcs=%d", armv7_npmcs);
494 * Allocate space for pointers to PMC HW descriptors and for
495 * the MDEP structure used by MI code.
497 armv7_pcpu = malloc(sizeof(struct armv7_cpu *) * pmc_cpu_max(),
498 M_PMC, M_WAITOK | M_ZERO);
501 pmc_mdep = pmc_mdep_alloc(1);
504 case ARMV7_IDCODE_CORTEX_A9:
505 pmc_mdep->pmd_cputype = PMC_CPU_ARMV7_CORTEX_A9;
508 case ARMV7_IDCODE_CORTEX_A8:
510 * On A8 we implemented common events only,
511 * so use it for the rest of machines.
513 pmc_mdep->pmd_cputype = PMC_CPU_ARMV7_CORTEX_A8;
517 pcd = &pmc_mdep->pmd_classdep[PMC_MDEP_CLASS_INDEX_ARMV7];
518 pcd->pcd_caps = ARMV7_PMC_CAPS;
519 pcd->pcd_class = PMC_CLASS_ARMV7;
520 pcd->pcd_num = armv7_npmcs;
521 pcd->pcd_ri = pmc_mdep->pmd_npmc;
524 pcd->pcd_allocate_pmc = armv7_allocate_pmc;
525 pcd->pcd_config_pmc = armv7_config_pmc;
526 pcd->pcd_pcpu_fini = armv7_pcpu_fini;
527 pcd->pcd_pcpu_init = armv7_pcpu_init;
528 pcd->pcd_describe = armv7_describe;
529 pcd->pcd_get_config = armv7_get_config;
530 pcd->pcd_read_pmc = armv7_read_pmc;
531 pcd->pcd_release_pmc = armv7_release_pmc;
532 pcd->pcd_start_pmc = armv7_start_pmc;
533 pcd->pcd_stop_pmc = armv7_stop_pmc;
534 pcd->pcd_write_pmc = armv7_write_pmc;
536 pmc_mdep->pmd_intr = armv7_intr;
537 pmc_mdep->pmd_switch_in = armv7_switch_in;
538 pmc_mdep->pmd_switch_out = armv7_switch_out;
540 pmc_mdep->pmd_npmc += armv7_npmcs;
546 pmc_armv7_finalize(struct pmc_mdep *md)