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 #define CPU_ID_CORTEX_VER_MASK 0xff
43 #define CPU_ID_CORTEX_VER_SHIFT 4
45 static int armv7_npmcs;
47 struct armv7_event_code_map {
52 const struct armv7_event_code_map armv7_event_codes[] = {
53 { PMC_EV_ARMV7_PMNC_SW_INCR, 0x00 },
54 { PMC_EV_ARMV7_L1_ICACHE_REFILL, 0x01 },
55 { PMC_EV_ARMV7_ITLB_REFILL, 0x02 },
56 { PMC_EV_ARMV7_L1_DCACHE_REFILL, 0x03 },
57 { PMC_EV_ARMV7_L1_DCACHE_ACCESS, 0x04 },
58 { PMC_EV_ARMV7_DTLB_REFILL, 0x05 },
59 { PMC_EV_ARMV7_MEM_READ, 0x06 },
60 { PMC_EV_ARMV7_MEM_WRITE, 0x07 },
61 { PMC_EV_ARMV7_INSTR_EXECUTED, 0x08 },
62 { PMC_EV_ARMV7_EXC_TAKEN, 0x09 },
63 { PMC_EV_ARMV7_EXC_EXECUTED, 0x0A },
64 { PMC_EV_ARMV7_CID_WRITE, 0x0B },
65 { PMC_EV_ARMV7_PC_WRITE, 0x0C },
66 { PMC_EV_ARMV7_PC_IMM_BRANCH, 0x0D },
67 { PMC_EV_ARMV7_PC_PROC_RETURN, 0x0E },
68 { PMC_EV_ARMV7_MEM_UNALIGNED_ACCESS, 0x0F },
69 { PMC_EV_ARMV7_PC_BRANCH_MIS_PRED, 0x10 },
70 { PMC_EV_ARMV7_CLOCK_CYCLES, 0x11 },
71 { PMC_EV_ARMV7_PC_BRANCH_PRED, 0x12 },
72 { PMC_EV_ARMV7_MEM_ACCESS, 0x13 },
73 { PMC_EV_ARMV7_L1_ICACHE_ACCESS, 0x14 },
74 { PMC_EV_ARMV7_L1_DCACHE_WB, 0x15 },
75 { PMC_EV_ARMV7_L2_CACHE_ACCESS, 0x16 },
76 { PMC_EV_ARMV7_L2_CACHE_REFILL, 0x17 },
77 { PMC_EV_ARMV7_L2_CACHE_WB, 0x18 },
78 { PMC_EV_ARMV7_BUS_ACCESS, 0x19 },
79 { PMC_EV_ARMV7_MEM_ERROR, 0x1A },
80 { PMC_EV_ARMV7_INSTR_SPEC, 0x1B },
81 { PMC_EV_ARMV7_TTBR_WRITE, 0x1C },
82 { PMC_EV_ARMV7_BUS_CYCLES, 0x1D },
83 { PMC_EV_ARMV7_CPU_CYCLES, 0xFF },
86 const int armv7_event_codes_size =
87 sizeof(armv7_event_codes) / sizeof(armv7_event_codes[0]);
90 * Per-processor information.
93 struct pmc_hw *pc_armv7pmcs;
97 static struct armv7_cpu **armv7_pcpu;
100 * Performance Monitor Control Register
102 static __inline uint32_t
103 armv7_pmnc_read(void)
107 __asm __volatile("mrc p15, 0, %0, c9, c12, 0" : "=r" (reg));
113 armv7_pmnc_write(uint32_t reg)
116 __asm __volatile("mcr p15, 0, %0, c9, c12, 0" : : "r" (reg));
120 * Clock Counter Register (PMCCNTR)
121 * Counts processor clock cycles.
123 static __inline uint32_t
124 armv7_ccnt_read(void)
128 __asm __volatile("mrc p15, 0, %0, c9, c13, 0" : "=r" (reg));
134 armv7_ccnt_write(uint32_t reg)
137 __asm __volatile("mcr p15, 0, %0, c9, c13, 0" : : "r" (reg));
141 * Interrupt Enable Set Register
144 armv7_interrupt_enable(uint32_t pmc)
150 __asm __volatile("mcr p15, 0, %0, c9, c14, 1" : : "r" (reg));
154 * Interrupt Clear Set Register
157 armv7_interrupt_disable(uint32_t pmc)
163 __asm __volatile("mcr p15, 0, %0, c9, c14, 2" : : "r" (reg));
167 * Overflow Flag Register
169 static __inline uint32_t
170 armv7_flag_read(void)
174 __asm __volatile("mrc p15, 0, %0, c9, c12, 3" : "=r" (reg));
180 armv7_flag_write(uint32_t reg)
183 __asm __volatile("mcr p15, 0, %0, c9, c12, 3" : : "r" (reg));
187 * Event Selection Register
190 armv7_evtsel_write(uint32_t reg)
193 __asm __volatile("mcr p15, 0, %0, c9, c13, 1" : : "r" (reg));
200 armv7_select_counter(unsigned int pmc)
203 __asm __volatile("mcr p15, 0, %0, c9, c12, 5" : : "r" (pmc));
207 * Counter Set Enable Register
210 armv7_counter_enable(unsigned int pmc)
216 __asm __volatile("mcr p15, 0, %0, c9, c12, 1" : : "r" (reg));
220 * Counter Clear Enable Register
223 armv7_counter_disable(unsigned int pmc)
229 __asm __volatile("mcr p15, 0, %0, c9, c12, 2" : : "r" (reg));
233 * Performance Count Register N
236 armv7_pmcn_read(unsigned int pmc)
240 KASSERT(pmc < 4, ("[armv7,%d] illegal PMC number %d", __LINE__, pmc));
242 armv7_select_counter(pmc);
243 __asm __volatile("mrc p15, 0, %0, c9, c13, 2" : "=r" (reg));
249 armv7_pmcn_write(unsigned int pmc, uint32_t reg)
252 KASSERT(pmc < 4, ("[armv7,%d] illegal PMC number %d", __LINE__, pmc));
254 armv7_select_counter(pmc);
255 __asm __volatile("mcr p15, 0, %0, c9, c13, 2" : : "r" (reg));
261 armv7_allocate_pmc(int cpu, int ri, struct pmc *pm,
262 const struct pmc_op_pmcallocate *a)
264 uint32_t caps, config;
265 struct armv7_cpu *pac;
269 KASSERT(cpu >= 0 && cpu < pmc_cpu_max(),
270 ("[armv7,%d] illegal CPU value %d", __LINE__, cpu));
271 KASSERT(ri >= 0 && ri < armv7_npmcs,
272 ("[armv7,%d] illegal row index %d", __LINE__, ri));
274 pac = armv7_pcpu[cpu];
277 if (a->pm_class != PMC_CLASS_ARMV7)
281 for (i = 0; i < armv7_event_codes_size; i++) {
282 if (armv7_event_codes[i].pe_ev == pe) {
283 config = armv7_event_codes[i].pe_code;
287 if (i == armv7_event_codes_size)
290 pm->pm_md.pm_armv7.pm_armv7_evsel = config;
292 PMCDBG(MDP,ALL,2,"armv7-allocate ri=%d -> config=0x%x", ri, config);
299 armv7_read_pmc(int cpu, int ri, pmc_value_t *v)
304 KASSERT(cpu >= 0 && cpu < pmc_cpu_max(),
305 ("[armv7,%d] illegal CPU value %d", __LINE__, cpu));
306 KASSERT(ri >= 0 && ri < armv7_npmcs,
307 ("[armv7,%d] illegal row index %d", __LINE__, ri));
309 pm = armv7_pcpu[cpu]->pc_armv7pmcs[ri].phw_pmc;
311 if (pm->pm_md.pm_armv7.pm_armv7_evsel == 0xFF)
312 tmp = armv7_ccnt_read();
314 tmp = armv7_pmcn_read(ri);
316 PMCDBG(MDP,REA,2,"armv7-read id=%d -> %jd", ri, tmp);
317 if (PMC_IS_SAMPLING_MODE(PMC_TO_MODE(pm)))
318 *v = ARMV7_PERFCTR_VALUE_TO_RELOAD_COUNT(tmp);
326 armv7_write_pmc(int cpu, int ri, pmc_value_t v)
330 KASSERT(cpu >= 0 && cpu < pmc_cpu_max(),
331 ("[armv7,%d] illegal CPU value %d", __LINE__, cpu));
332 KASSERT(ri >= 0 && ri < armv7_npmcs,
333 ("[armv7,%d] illegal row-index %d", __LINE__, ri));
335 pm = armv7_pcpu[cpu]->pc_armv7pmcs[ri].phw_pmc;
337 if (PMC_IS_SAMPLING_MODE(PMC_TO_MODE(pm)))
338 v = ARMV7_RELOAD_COUNT_TO_PERFCTR_VALUE(v);
340 PMCDBG(MDP,WRI,1,"armv7-write cpu=%d ri=%d v=%jx", cpu, ri, v);
342 if (pm->pm_md.pm_armv7.pm_armv7_evsel == 0xFF)
345 armv7_pmcn_write(ri, v);
351 armv7_config_pmc(int cpu, int ri, struct pmc *pm)
355 PMCDBG(MDP,CFG,1, "cpu=%d ri=%d pm=%p", cpu, ri, pm);
357 KASSERT(cpu >= 0 && cpu < pmc_cpu_max(),
358 ("[armv7,%d] illegal CPU value %d", __LINE__, cpu));
359 KASSERT(ri >= 0 && ri < armv7_npmcs,
360 ("[armv7,%d] illegal row-index %d", __LINE__, ri));
362 phw = &armv7_pcpu[cpu]->pc_armv7pmcs[ri];
364 KASSERT(pm == NULL || phw->phw_pmc == NULL,
365 ("[armv7,%d] pm=%p phw->pm=%p hwpmc not unconfigured",
366 __LINE__, pm, phw->phw_pmc));
374 armv7_start_pmc(int cpu, int ri)
380 phw = &armv7_pcpu[cpu]->pc_armv7pmcs[ri];
382 config = pm->pm_md.pm_armv7.pm_armv7_evsel;
385 * Configure the event selection.
387 armv7_select_counter(ri);
388 armv7_evtsel_write(config);
393 armv7_interrupt_enable(ri);
394 armv7_counter_enable(ri);
400 armv7_stop_pmc(int cpu, int ri)
405 phw = &armv7_pcpu[cpu]->pc_armv7pmcs[ri];
411 armv7_counter_disable(ri);
412 armv7_interrupt_disable(ri);
418 armv7_release_pmc(int cpu, int ri, struct pmc *pmc)
422 KASSERT(cpu >= 0 && cpu < pmc_cpu_max(),
423 ("[armv7,%d] illegal CPU value %d", __LINE__, cpu));
424 KASSERT(ri >= 0 && ri < armv7_npmcs,
425 ("[armv7,%d] illegal row-index %d", __LINE__, ri));
427 phw = &armv7_pcpu[cpu]->pc_armv7pmcs[ri];
428 KASSERT(phw->phw_pmc == NULL,
429 ("[armv7,%d] PHW pmc %p non-NULL", __LINE__, phw->phw_pmc));
435 armv7_intr(int cpu, struct trapframe *tf)
437 struct armv7_cpu *pc;
443 KASSERT(cpu >= 0 && cpu < pmc_cpu_max(),
444 ("[armv7,%d] CPU %d out of range", __LINE__, cpu));
447 pc = armv7_pcpu[cpu];
449 for (ri = 0; ri < armv7_npmcs; ri++) {
450 pm = armv7_pcpu[cpu]->pc_armv7pmcs[ri].phw_pmc;
453 if (!PMC_IS_SAMPLING_MODE(PMC_TO_MODE(pm)))
456 /* Check if counter has overflowed */
457 if (pm->pm_md.pm_armv7.pm_armv7_evsel == 0xFF)
462 if ((armv7_flag_read() & reg) == 0) {
466 /* Clear Overflow Flag */
467 armv7_flag_write(reg);
469 retval = 1; /* Found an interrupting PMC. */
470 if (pm->pm_state != PMC_STATE_RUNNING)
473 error = pmc_process_interrupt(cpu, PMC_HR, pm, tf,
476 armv7_stop_pmc(cpu, ri);
478 /* Reload sampling count */
479 armv7_write_pmc(cpu, ri, pm->pm_sc.pm_reloadcount);
486 armv7_describe(int cpu, int ri, struct pmc_info *pi, struct pmc **ppmc)
488 char armv7_name[PMC_NAME_MAX];
492 KASSERT(cpu >= 0 && cpu < pmc_cpu_max(),
493 ("[armv7,%d], illegal CPU %d", __LINE__, cpu));
494 KASSERT(ri >= 0 && ri < armv7_npmcs,
495 ("[armv7,%d] row-index %d out of range", __LINE__, ri));
497 phw = &armv7_pcpu[cpu]->pc_armv7pmcs[ri];
498 snprintf(armv7_name, sizeof(armv7_name), "ARMV7-%d", ri);
499 if ((error = copystr(armv7_name, pi->pm_name, PMC_NAME_MAX,
502 pi->pm_class = PMC_CLASS_ARMV7;
503 if (phw->phw_state & PMC_PHW_FLAG_IS_ENABLED) {
504 pi->pm_enabled = TRUE;
505 *ppmc = phw->phw_pmc;
507 pi->pm_enabled = FALSE;
515 armv7_get_config(int cpu, int ri, struct pmc **ppm)
518 *ppm = armv7_pcpu[cpu]->pc_armv7pmcs[ri].phw_pmc;
524 * XXX don't know what we should do here.
527 armv7_switch_in(struct pmc_cpu *pc, struct pmc_process *pp)
534 armv7_switch_out(struct pmc_cpu *pc, struct pmc_process *pp)
541 armv7_pcpu_init(struct pmc_mdep *md, int cpu)
543 struct armv7_cpu *pac;
551 KASSERT(cpu >= 0 && cpu < pmc_cpu_max(),
552 ("[armv7,%d] wrong cpu number %d", __LINE__, cpu));
553 PMCDBG(MDP,INI,1,"armv7-init cpu=%d", cpu);
555 armv7_pcpu[cpu] = pac = malloc(sizeof(struct armv7_cpu), M_PMC,
559 pac->cortex_ver = (cpuid >> CPU_ID_CORTEX_VER_SHIFT) & \
560 CPU_ID_CORTEX_VER_MASK;
562 pac->pc_armv7pmcs = malloc(sizeof(struct pmc_hw) * armv7_npmcs,
563 M_PMC, M_WAITOK|M_ZERO);
565 first_ri = md->pmd_classdep[PMC_MDEP_CLASS_INDEX_ARMV7].pcd_ri;
566 KASSERT(pc != NULL, ("[armv7,%d] NULL per-cpu pointer", __LINE__));
568 for (i = 0, phw = pac->pc_armv7pmcs; i < armv7_npmcs; i++, phw++) {
569 phw->phw_state = PMC_PHW_FLAG_IS_ENABLED |
570 PMC_PHW_CPU_TO_STATE(cpu) | PMC_PHW_INDEX_TO_STATE(i);
572 pc->pc_hwpmcs[i + first_ri] = phw;
576 pmnc = armv7_pmnc_read();
577 pmnc |= ARMV7_PMNC_ENABLE;
578 armv7_pmnc_write(pmnc);
584 armv7_pcpu_fini(struct pmc_mdep *md, int cpu)
588 pmnc = armv7_pmnc_read();
589 pmnc &= ~ARMV7_PMNC_ENABLE;
590 armv7_pmnc_write(pmnc);
596 pmc_armv7_initialize()
598 struct pmc_mdep *pmc_mdep;
599 struct pmc_classdep *pcd;
602 reg = armv7_pmnc_read();
604 armv7_npmcs = (reg >> ARMV7_PMNC_N_SHIFT) & \
607 PMCDBG(MDP,INI,1,"armv7-init npmcs=%d", armv7_npmcs);
610 * Allocate space for pointers to PMC HW descriptors and for
611 * the MDEP structure used by MI code.
613 armv7_pcpu = malloc(sizeof(struct armv7_cpu *) * pmc_cpu_max(),
614 M_PMC, M_WAITOK | M_ZERO);
617 pmc_mdep = pmc_mdep_alloc(1);
618 pmc_mdep->pmd_cputype = PMC_CPU_ARMV7;
620 pcd = &pmc_mdep->pmd_classdep[PMC_MDEP_CLASS_INDEX_ARMV7];
621 pcd->pcd_caps = ARMV7_PMC_CAPS;
622 pcd->pcd_class = PMC_CLASS_ARMV7;
623 pcd->pcd_num = armv7_npmcs;
624 pcd->pcd_ri = pmc_mdep->pmd_npmc;
627 pcd->pcd_allocate_pmc = armv7_allocate_pmc;
628 pcd->pcd_config_pmc = armv7_config_pmc;
629 pcd->pcd_pcpu_fini = armv7_pcpu_fini;
630 pcd->pcd_pcpu_init = armv7_pcpu_init;
631 pcd->pcd_describe = armv7_describe;
632 pcd->pcd_get_config = armv7_get_config;
633 pcd->pcd_read_pmc = armv7_read_pmc;
634 pcd->pcd_release_pmc = armv7_release_pmc;
635 pcd->pcd_start_pmc = armv7_start_pmc;
636 pcd->pcd_stop_pmc = armv7_stop_pmc;
637 pcd->pcd_write_pmc = armv7_write_pmc;
639 pmc_mdep->pmd_intr = armv7_intr;
640 pmc_mdep->pmd_switch_in = armv7_switch_in;
641 pmc_mdep->pmd_switch_out = armv7_switch_out;
643 pmc_mdep->pmd_npmc += armv7_npmcs;
649 pmc_armv7_finalize(struct pmc_mdep *md)