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, uint32_t evsel)
115 if (evsel == PMC_EV_CPU_CYCLES) {
116 return ((uint32_t)cp15_pmccntr_get());
119 KASSERT(pmc < armv7_npmcs, ("%s: illegal PMC number %d", __func__, pmc));
121 cp15_pmselr_set(pmc);
122 return (cp15_pmxevcntr_get());
126 armv7_pmcn_write(unsigned int pmc, uint32_t reg)
129 KASSERT(pmc < armv7_npmcs, ("%s: illegal PMC number %d", __func__, pmc));
131 cp15_pmselr_set(pmc);
132 cp15_pmxevcntr_set(reg);
138 armv7_allocate_pmc(int cpu, int ri, struct pmc *pm,
139 const struct pmc_op_pmcallocate *a)
141 struct armv7_cpu *pac;
145 KASSERT(cpu >= 0 && cpu < pmc_cpu_max(),
146 ("[armv7,%d] illegal CPU value %d", __LINE__, cpu));
147 KASSERT(ri >= 0 && ri < armv7_npmcs,
148 ("[armv7,%d] illegal row index %d", __LINE__, ri));
150 pac = armv7_pcpu[cpu];
152 if (a->pm_class != PMC_CLASS_ARMV7)
156 config = (pe & EVENT_ID_MASK);
157 pm->pm_md.pm_armv7.pm_armv7_evsel = config;
159 PMCDBG2(MDP, ALL, 2, "armv7-allocate ri=%d -> config=0x%x", ri, config);
166 armv7_read_pmc(int cpu, int ri, pmc_value_t *v)
173 KASSERT(cpu >= 0 && cpu < pmc_cpu_max(),
174 ("[armv7,%d] illegal CPU value %d", __LINE__, cpu));
175 KASSERT(ri >= 0 && ri < armv7_npmcs,
176 ("[armv7,%d] illegal row index %d", __LINE__, ri));
178 pm = armv7_pcpu[cpu]->pc_armv7pmcs[ri].phw_pmc;
181 tmp = armv7_pmcn_read(ri, pm->pm_md.pm_armv7.pm_armv7_evsel);
183 /* Check if counter has overflowed */
184 if (pm->pm_md.pm_armv7.pm_armv7_evsel == PMC_EV_CPU_CYCLES)
189 if ((cp15_pmovsr_get() & reg) != 0) {
190 /* Clear Overflow Flag */
191 cp15_pmovsr_set(reg);
192 if (!PMC_IS_SAMPLING_MODE(PMC_TO_MODE(pm)))
193 pm->pm_pcpu_state[cpu].pps_overflowcnt++;
195 /* Reread counter in case we raced. */
196 tmp = armv7_pmcn_read(ri, pm->pm_md.pm_armv7.pm_armv7_evsel);
198 tmp += 0x100000000llu * pm->pm_pcpu_state[cpu].pps_overflowcnt;
201 PMCDBG2(MDP, REA, 2, "armv7-read id=%d -> %jd", ri, tmp);
202 if (PMC_IS_SAMPLING_MODE(PMC_TO_MODE(pm)))
203 *v = ARMV7_PERFCTR_VALUE_TO_RELOAD_COUNT(tmp);
211 armv7_write_pmc(int cpu, int ri, pmc_value_t v)
215 KASSERT(cpu >= 0 && cpu < pmc_cpu_max(),
216 ("[armv7,%d] illegal CPU value %d", __LINE__, cpu));
217 KASSERT(ri >= 0 && ri < armv7_npmcs,
218 ("[armv7,%d] illegal row-index %d", __LINE__, ri));
220 pm = armv7_pcpu[cpu]->pc_armv7pmcs[ri].phw_pmc;
222 if (PMC_IS_SAMPLING_MODE(PMC_TO_MODE(pm)))
223 v = ARMV7_RELOAD_COUNT_TO_PERFCTR_VALUE(v);
225 PMCDBG3(MDP, WRI, 1, "armv7-write cpu=%d ri=%d v=%jx", cpu, ri, v);
227 pm->pm_pcpu_state[cpu].pps_overflowcnt = v >> 32;
228 if (pm->pm_md.pm_armv7.pm_armv7_evsel == PMC_EV_CPU_CYCLES)
231 armv7_pmcn_write(ri, v);
237 armv7_config_pmc(int cpu, int ri, struct pmc *pm)
241 PMCDBG3(MDP, CFG, 1, "cpu=%d ri=%d pm=%p", cpu, ri, pm);
243 KASSERT(cpu >= 0 && cpu < pmc_cpu_max(),
244 ("[armv7,%d] illegal CPU value %d", __LINE__, cpu));
245 KASSERT(ri >= 0 && ri < armv7_npmcs,
246 ("[armv7,%d] illegal row-index %d", __LINE__, ri));
248 phw = &armv7_pcpu[cpu]->pc_armv7pmcs[ri];
250 KASSERT(pm == NULL || phw->phw_pmc == NULL,
251 ("[armv7,%d] pm=%p phw->pm=%p hwpmc not unconfigured",
252 __LINE__, pm, phw->phw_pmc));
260 armv7_start_pmc(int cpu, int ri)
266 phw = &armv7_pcpu[cpu]->pc_armv7pmcs[ri];
268 config = pm->pm_md.pm_armv7.pm_armv7_evsel;
271 * Configure the event selection.
273 if (config != PMC_EV_CPU_CYCLES) {
275 cp15_pmxevtyper_set(config);
282 armv7_interrupt_enable(ri);
283 armv7_counter_enable(ri);
289 armv7_stop_pmc(int cpu, int ri)
295 phw = &armv7_pcpu[cpu]->pc_armv7pmcs[ri];
297 config = pm->pm_md.pm_armv7.pm_armv7_evsel;
298 if (config == PMC_EV_CPU_CYCLES)
304 armv7_counter_disable(ri);
305 armv7_interrupt_disable(ri);
311 armv7_release_pmc(int cpu, int ri, struct pmc *pmc)
315 KASSERT(cpu >= 0 && cpu < pmc_cpu_max(),
316 ("[armv7,%d] illegal CPU value %d", __LINE__, cpu));
317 KASSERT(ri >= 0 && ri < armv7_npmcs,
318 ("[armv7,%d] illegal row-index %d", __LINE__, ri));
320 phw = &armv7_pcpu[cpu]->pc_armv7pmcs[ri];
321 KASSERT(phw->phw_pmc == NULL,
322 ("[armv7,%d] PHW pmc %p non-NULL", __LINE__, phw->phw_pmc));
328 armv7_intr(struct trapframe *tf)
330 struct armv7_cpu *pc;
337 KASSERT(cpu >= 0 && cpu < pmc_cpu_max(),
338 ("[armv7,%d] CPU %d out of range", __LINE__, cpu));
341 pc = armv7_pcpu[cpu];
343 for (ri = 0; ri < armv7_npmcs; ri++) {
344 pm = armv7_pcpu[cpu]->pc_armv7pmcs[ri].phw_pmc;
348 /* Check if counter has overflowed */
349 if (pm->pm_md.pm_armv7.pm_armv7_evsel == PMC_EV_CPU_CYCLES)
354 if ((cp15_pmovsr_get() & reg) == 0) {
358 /* Clear Overflow Flag */
359 cp15_pmovsr_set(reg);
361 retval = 1; /* Found an interrupting PMC. */
363 if (!PMC_IS_SAMPLING_MODE(PMC_TO_MODE(pm))) {
364 pm->pm_pcpu_state[cpu].pps_overflowcnt += 1;
367 if (pm->pm_state != PMC_STATE_RUNNING)
370 error = pmc_process_interrupt(PMC_HR, pm, tf);
372 armv7_stop_pmc(cpu, ri);
374 /* Reload sampling count */
375 armv7_write_pmc(cpu, ri, pm->pm_sc.pm_reloadcount);
382 armv7_describe(int cpu, int ri, struct pmc_info *pi, struct pmc **ppmc)
384 char armv7_name[PMC_NAME_MAX];
388 KASSERT(cpu >= 0 && cpu < pmc_cpu_max(),
389 ("[armv7,%d], illegal CPU %d", __LINE__, cpu));
390 KASSERT(ri >= 0 && ri < armv7_npmcs,
391 ("[armv7,%d] row-index %d out of range", __LINE__, ri));
393 phw = &armv7_pcpu[cpu]->pc_armv7pmcs[ri];
394 snprintf(armv7_name, sizeof(armv7_name), "ARMV7-%d", ri);
395 if ((error = copystr(armv7_name, pi->pm_name, PMC_NAME_MAX,
398 pi->pm_class = PMC_CLASS_ARMV7;
399 if (phw->phw_state & PMC_PHW_FLAG_IS_ENABLED) {
400 pi->pm_enabled = TRUE;
401 *ppmc = phw->phw_pmc;
403 pi->pm_enabled = FALSE;
411 armv7_get_config(int cpu, int ri, struct pmc **ppm)
414 *ppm = armv7_pcpu[cpu]->pc_armv7pmcs[ri].phw_pmc;
420 * XXX don't know what we should do here.
423 armv7_switch_in(struct pmc_cpu *pc, struct pmc_process *pp)
430 armv7_switch_out(struct pmc_cpu *pc, struct pmc_process *pp)
437 armv7_pcpu_init(struct pmc_mdep *md, int cpu)
439 struct armv7_cpu *pac;
446 KASSERT(cpu >= 0 && cpu < pmc_cpu_max(),
447 ("[armv7,%d] wrong cpu number %d", __LINE__, cpu));
448 PMCDBG1(MDP, INI, 1, "armv7-init cpu=%d", cpu);
450 armv7_pcpu[cpu] = pac = malloc(sizeof(struct armv7_cpu), M_PMC,
453 pac->pc_armv7pmcs = malloc(sizeof(struct pmc_hw) * armv7_npmcs,
454 M_PMC, M_WAITOK|M_ZERO);
456 first_ri = md->pmd_classdep[PMC_MDEP_CLASS_INDEX_ARMV7].pcd_ri;
457 KASSERT(pc != NULL, ("[armv7,%d] NULL per-cpu pointer", __LINE__));
459 for (i = 0, phw = pac->pc_armv7pmcs; i < armv7_npmcs; i++, phw++) {
460 phw->phw_state = PMC_PHW_FLAG_IS_ENABLED |
461 PMC_PHW_CPU_TO_STATE(cpu) | PMC_PHW_INDEX_TO_STATE(i);
463 pc->pc_hwpmcs[i + first_ri] = phw;
467 cp15_pmcnten_clr(pmnc);
468 cp15_pminten_clr(pmnc);
469 cp15_pmovsr_set(pmnc);
472 pmnc = cp15_pmcr_get();
473 pmnc |= ARMV7_PMNC_ENABLE;
480 armv7_pcpu_fini(struct pmc_mdep *md, int cpu)
484 pmnc = cp15_pmcr_get();
485 pmnc &= ~ARMV7_PMNC_ENABLE;
489 cp15_pmcnten_clr(pmnc);
490 cp15_pminten_clr(pmnc);
491 cp15_pmovsr_set(pmnc);
497 pmc_armv7_initialize()
499 struct pmc_mdep *pmc_mdep;
500 struct pmc_classdep *pcd;
504 reg = cp15_pmcr_get();
505 armv7_npmcs = (reg >> ARMV7_PMNC_N_SHIFT) & \
507 idcode = (reg & ARMV7_IDCODE_MASK) >> ARMV7_IDCODE_SHIFT;
509 PMCDBG1(MDP, INI, 1, "armv7-init npmcs=%d", armv7_npmcs);
512 * Allocate space for pointers to PMC HW descriptors and for
513 * the MDEP structure used by MI code.
515 armv7_pcpu = malloc(sizeof(struct armv7_cpu *) * pmc_cpu_max(),
516 M_PMC, M_WAITOK | M_ZERO);
519 pmc_mdep = pmc_mdep_alloc(1);
522 case ARMV7_IDCODE_CORTEX_A9:
523 pmc_mdep->pmd_cputype = PMC_CPU_ARMV7_CORTEX_A9;
526 case ARMV7_IDCODE_CORTEX_A8:
528 * On A8 we implemented common events only,
529 * so use it for the rest of machines.
531 pmc_mdep->pmd_cputype = PMC_CPU_ARMV7_CORTEX_A8;
535 pcd = &pmc_mdep->pmd_classdep[PMC_MDEP_CLASS_INDEX_ARMV7];
536 pcd->pcd_caps = ARMV7_PMC_CAPS;
537 pcd->pcd_class = PMC_CLASS_ARMV7;
538 pcd->pcd_num = armv7_npmcs;
539 pcd->pcd_ri = pmc_mdep->pmd_npmc;
542 pcd->pcd_allocate_pmc = armv7_allocate_pmc;
543 pcd->pcd_config_pmc = armv7_config_pmc;
544 pcd->pcd_pcpu_fini = armv7_pcpu_fini;
545 pcd->pcd_pcpu_init = armv7_pcpu_init;
546 pcd->pcd_describe = armv7_describe;
547 pcd->pcd_get_config = armv7_get_config;
548 pcd->pcd_read_pmc = armv7_read_pmc;
549 pcd->pcd_release_pmc = armv7_release_pmc;
550 pcd->pcd_start_pmc = armv7_start_pmc;
551 pcd->pcd_stop_pmc = armv7_stop_pmc;
552 pcd->pcd_write_pmc = armv7_write_pmc;
554 pmc_mdep->pmd_intr = armv7_intr;
555 pmc_mdep->pmd_switch_in = armv7_switch_in;
556 pmc_mdep->pmd_switch_out = armv7_switch_out;
558 pmc_mdep->pmd_npmc += armv7_npmcs;
564 pmc_armv7_finalize(struct pmc_mdep *md)