2 * SPDX-License-Identifier: BSD-2-Clause
4 * Copyright (c) 2019 Ruslan Bukin <br@bsdpad.com>
6 * This software was developed by SRI International and the University of
7 * Cambridge Computer Laboratory (Department of Computer Science and
8 * Technology) under DARPA contract HR0011-18-C-0016 ("ECATS"), as part of the
9 * DARPA SSITH research programme.
11 * Redistribution and use in source and binary forms, with or without
12 * modification, are permitted provided that the following conditions
14 * 1. Redistributions of source code must retain the above copyright
15 * notice, this list of conditions and the following disclaimer.
16 * 2. Redistributions in binary form must reproduce the above copyright
17 * notice, this list of conditions and the following disclaimer in the
18 * documentation and/or other materials provided with the distribution.
20 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
21 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
24 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33 #include <sys/cdefs.h>
34 __FBSDID("$FreeBSD$");
36 #include "opt_hwpmc_hooks.h"
38 #include <sys/param.h>
39 #include <sys/pmckern.h>
41 #include <dev/hwpmc/hwpmc_beri.h>
43 #define BERI_NCOUNTERS 56
44 #define BERI_PMC_CAPS (PMC_CAP_USER | PMC_CAP_SYSTEM | \
45 PMC_CAP_READ | PMC_CAP_WRITE )
47 struct beri_event_code_map {
48 uint32_t pe_ev; /* enum value */
49 uint64_t (*get_func)(void);
52 const struct beri_event_code_map beri_event_codes[BERI_NCOUNTERS] = {
54 statcounters_get_cycle_count },
56 statcounters_get_inst_count },
57 { PMC_EV_BERI_INST_USER,
58 statcounters_get_inst_user_count },
59 { PMC_EV_BERI_INST_KERNEL,
60 statcounters_get_inst_kernel_count },
61 { PMC_EV_BERI_IMPRECISE_SETBOUNDS,
62 statcounters_get_imprecise_setbounds_count },
63 { PMC_EV_BERI_UNREPRESENTABLE_CAPS,
64 statcounters_get_unrepresentable_caps_count },
65 { PMC_EV_BERI_ITLB_MISS,
66 statcounters_get_itlb_miss_count },
67 { PMC_EV_BERI_DTLB_MISS,
68 statcounters_get_dtlb_miss_count },
69 { PMC_EV_BERI_ICACHE_WRITE_HIT,
70 statcounters_get_icache_write_hit_count },
71 { PMC_EV_BERI_ICACHE_WRITE_MISS,
72 statcounters_get_icache_write_miss_count },
73 { PMC_EV_BERI_ICACHE_READ_HIT,
74 statcounters_get_icache_read_hit_count },
75 { PMC_EV_BERI_ICACHE_READ_MISS,
76 statcounters_get_icache_read_miss_count },
77 { PMC_EV_BERI_ICACHE_EVICT,
78 statcounters_get_icache_evict_count },
79 { PMC_EV_BERI_DCACHE_WRITE_HIT,
80 statcounters_get_dcache_write_hit_count },
81 { PMC_EV_BERI_DCACHE_WRITE_MISS,
82 statcounters_get_dcache_write_miss_count },
83 { PMC_EV_BERI_DCACHE_READ_HIT,
84 statcounters_get_dcache_read_hit_count },
85 { PMC_EV_BERI_DCACHE_READ_MISS,
86 statcounters_get_dcache_read_miss_count },
87 { PMC_EV_BERI_DCACHE_EVICT,
88 statcounters_get_dcache_evict_count },
89 { PMC_EV_BERI_DCACHE_SET_TAG_WRITE,
90 statcounters_get_dcache_set_tag_write_count },
91 { PMC_EV_BERI_DCACHE_SET_TAG_READ,
92 statcounters_get_dcache_set_tag_read_count },
93 { PMC_EV_BERI_L2CACHE_WRITE_HIT,
94 statcounters_get_l2cache_write_hit_count },
95 { PMC_EV_BERI_L2CACHE_WRITE_MISS,
96 statcounters_get_l2cache_write_miss_count },
97 { PMC_EV_BERI_L2CACHE_READ_HIT,
98 statcounters_get_l2cache_read_hit_count },
99 { PMC_EV_BERI_L2CACHE_READ_MISS,
100 statcounters_get_l2cache_read_miss_count },
101 { PMC_EV_BERI_L2CACHE_EVICT,
102 statcounters_get_l2cache_evict_count },
103 { PMC_EV_BERI_L2CACHE_SET_TAG_WRITE,
104 statcounters_get_l2cache_set_tag_write_count },
105 { PMC_EV_BERI_L2CACHE_SET_TAG_READ,
106 statcounters_get_l2cache_set_tag_read_count },
107 { PMC_EV_BERI_MEM_BYTE_READ,
108 statcounters_get_mem_byte_read_count },
109 { PMC_EV_BERI_MEM_BYTE_WRITE,
110 statcounters_get_mem_byte_write_count },
111 { PMC_EV_BERI_MEM_HWORD_READ,
112 statcounters_get_mem_hword_read_count },
113 { PMC_EV_BERI_MEM_HWORD_WRITE,
114 statcounters_get_mem_hword_write_count },
115 { PMC_EV_BERI_MEM_WORD_READ,
116 statcounters_get_mem_word_read_count },
117 { PMC_EV_BERI_MEM_WORD_WRITE,
118 statcounters_get_mem_word_write_count },
119 { PMC_EV_BERI_MEM_DWORD_READ,
120 statcounters_get_mem_dword_read_count },
121 { PMC_EV_BERI_MEM_DWORD_WRITE,
122 statcounters_get_mem_dword_write_count },
123 { PMC_EV_BERI_MEM_CAP_READ,
124 statcounters_get_mem_cap_read_count },
125 { PMC_EV_BERI_MEM_CAP_WRITE,
126 statcounters_get_mem_cap_write_count },
127 { PMC_EV_BERI_MEM_CAP_READ_TAG_SET,
128 statcounters_get_mem_cap_read_tag_set_count },
129 { PMC_EV_BERI_MEM_CAP_WRITE_TAG_SET,
130 statcounters_get_mem_cap_write_tag_set_count },
131 { PMC_EV_BERI_TAGCACHE_WRITE_HIT,
132 statcounters_get_tagcache_write_hit_count },
133 { PMC_EV_BERI_TAGCACHE_WRITE_MISS,
134 statcounters_get_tagcache_write_miss_count },
135 { PMC_EV_BERI_TAGCACHE_READ_HIT,
136 statcounters_get_tagcache_read_hit_count },
137 { PMC_EV_BERI_TAGCACHE_READ_MISS,
138 statcounters_get_tagcache_read_miss_count },
139 { PMC_EV_BERI_TAGCACHE_EVICT,
140 statcounters_get_tagcache_evict_count },
141 { PMC_EV_BERI_L2CACHEMASTER_READ_REQ,
142 statcounters_get_l2cachemaster_read_req_count },
143 { PMC_EV_BERI_L2CACHEMASTER_WRITE_REQ,
144 statcounters_get_l2cachemaster_write_req_count },
145 { PMC_EV_BERI_L2CACHEMASTER_WRITE_REQ_FLIT,
146 statcounters_get_l2cachemaster_write_req_flit_count },
147 { PMC_EV_BERI_L2CACHEMASTER_READ_RSP,
148 statcounters_get_l2cachemaster_read_rsp_count },
149 { PMC_EV_BERI_L2CACHEMASTER_READ_RSP_FLIT,
150 statcounters_get_l2cachemaster_read_rsp_flit_count },
151 { PMC_EV_BERI_L2CACHEMASTER_WRITE_RSP,
152 statcounters_get_l2cachemaster_write_rsp_count },
153 { PMC_EV_BERI_TAGCACHEMASTER_READ_REQ,
154 statcounters_get_tagcachemaster_read_req_count },
155 { PMC_EV_BERI_TAGCACHEMASTER_WRITE_REQ,
156 statcounters_get_tagcachemaster_write_req_count },
157 { PMC_EV_BERI_TAGCACHEMASTER_WRITE_REQ_FLIT,
158 statcounters_get_tagcachemaster_write_req_flit_count },
159 { PMC_EV_BERI_TAGCACHEMASTER_READ_RSP,
160 statcounters_get_tagcachemaster_read_rsp_count },
161 { PMC_EV_BERI_TAGCACHEMASTER_READ_RSP_FLIT,
162 statcounters_get_tagcachemaster_read_rsp_flit_count },
163 { PMC_EV_BERI_TAGCACHEMASTER_WRITE_RSP,
164 statcounters_get_tagcachemaster_write_rsp_count },
167 struct mips_pmc_spec beri_pmc_spec = {
168 .ps_cpuclass = PMC_CLASS_BERI,
169 .ps_cputype = PMC_CPU_MIPS_BERI,
170 .ps_capabilities = BERI_PMC_CAPS,
171 .ps_counter_width = 64
175 * Per-processor information.
178 struct pmc_hw *pc_beripmcs;
179 uint64_t start_values[BERI_NCOUNTERS];
180 uint64_t stop_values[BERI_NCOUNTERS];
181 uint64_t saved_values[BERI_NCOUNTERS];
185 static struct beri_cpu **beri_pcpu;
188 beri_allocate_pmc(int cpu, int ri, struct pmc *pm,
189 const struct pmc_op_pmcallocate *a)
194 KASSERT(cpu >= 0 && cpu < pmc_cpu_max(),
195 ("[beri,%d] illegal CPU value %d", __LINE__, cpu));
196 KASSERT(ri >= 0 && ri < beri_npmcs,
197 ("[beri,%d] illegal row index %d", __LINE__, ri));
199 if (a->pm_class != beri_pmc_spec.ps_cpuclass)
202 for (i = 0; i < BERI_NCOUNTERS; i++) {
203 if (beri_event_codes[i].pe_ev == a->pm_ev) {
209 if (i == BERI_NCOUNTERS)
212 pm->pm_md.pm_mips_evsel = config;
214 PMCDBG2(MDP,ALL,2,"beri-allocate ri=%d -> config=0x%x", ri, config);
220 beri_read_pmc(int cpu, int ri, pmc_value_t *v)
225 pmc_value_t start_val;
226 pmc_value_t stop_val;
227 pmc_value_t saved_val;
230 KASSERT(cpu >= 0 && cpu < pmc_cpu_max(),
231 ("[beri,%d] illegal CPU value %d", __LINE__, cpu));
232 KASSERT(ri >= 0 && ri < beri_npmcs,
233 ("[beri,%d] illegal row index %d", __LINE__, ri));
235 pm = beri_pcpu[cpu]->pc_beripmcs[ri].phw_pmc;
236 config = pm->pm_md.pm_mips_evsel;
238 start_val = beri_pcpu[cpu]->start_values[config];
239 if (PMC_IS_SYSTEM_MODE(PMC_TO_MODE(pm))) {
240 stop_val = beri_event_codes[config].get_func();
242 stop_val = beri_pcpu[cpu]->stop_values[config];
244 if (start_val <= stop_val)
245 result = stop_val - start_val;
247 if (config == 0) /* CYCLE counter is 48 bit */
248 result = 0x00ffffffffffffffUL;
250 result = 0xffffffffffffffffUL;
255 saved_val = beri_pcpu[cpu]->saved_values[config];
257 *v = result + saved_val;
263 beri_write_pmc(int cpu, int ri, pmc_value_t v)
268 KASSERT(cpu >= 0 && cpu < pmc_cpu_max(),
269 ("[beri,%d] illegal CPU value %d", __LINE__, cpu));
270 KASSERT(ri >= 0 && ri < beri_npmcs,
271 ("[beri,%d] illegal row-index %d", __LINE__, ri));
273 pm = beri_pcpu[cpu]->pc_beripmcs[ri].phw_pmc;
274 config = pm->pm_md.pm_mips_evsel;
276 if (PMC_IS_SAMPLING_MODE(PMC_TO_MODE(pm)))
277 v = (1UL << (beri_pmc_spec.ps_counter_width - 1)) - v;
279 PMCDBG3(MDP,WRI,1,"beri-write cpu=%d ri=%d v=%jx", cpu, ri, v);
281 if (PMC_IS_SYSTEM_MODE(PMC_TO_MODE(pm)))
282 beri_pcpu[cpu]->saved_values[config] = 0;
284 beri_pcpu[cpu]->saved_values[config] = v;
290 beri_config_pmc(int cpu, int ri, struct pmc *pm)
294 PMCDBG3(MDP,CFG,1, "cpu=%d ri=%d pm=%p", cpu, ri, pm);
296 KASSERT(cpu >= 0 && cpu < pmc_cpu_max(),
297 ("[beri,%d] illegal CPU value %d", __LINE__, cpu));
298 KASSERT(ri >= 0 && ri < beri_npmcs,
299 ("[beri,%d] illegal row-index %d", __LINE__, ri));
301 phw = &beri_pcpu[cpu]->pc_beripmcs[ri];
303 KASSERT(pm == NULL || phw->phw_pmc == NULL,
304 ("[beri,%d] pm=%p phw->pm=%p hwpmc not unconfigured",
305 __LINE__, pm, phw->phw_pmc));
313 beri_start_pmc(int cpu, int ri)
320 phw = &beri_pcpu[cpu]->pc_beripmcs[ri];
322 config = pm->pm_md.pm_mips_evsel;
324 v = beri_event_codes[config].get_func();
325 beri_pcpu[cpu]->start_values[config] = v;
331 beri_stop_pmc(int cpu, int ri)
338 phw = &beri_pcpu[cpu]->pc_beripmcs[ri];
340 config = pm->pm_md.pm_mips_evsel;
342 v = beri_event_codes[config].get_func();
343 beri_pcpu[cpu]->stop_values[config] = v;
349 beri_release_pmc(int cpu, int ri, struct pmc *pmc)
353 KASSERT(cpu >= 0 && cpu < pmc_cpu_max(),
354 ("[beri,%d] illegal CPU value %d", __LINE__, cpu));
355 KASSERT(ri >= 0 && ri < beri_npmcs,
356 ("[beri,%d] illegal row-index %d", __LINE__, ri));
358 phw = &beri_pcpu[cpu]->pc_beripmcs[ri];
359 KASSERT(phw->phw_pmc == NULL,
360 ("[beri,%d] PHW pmc %p non-NULL", __LINE__, phw->phw_pmc));
366 beri_describe(int cpu, int ri, struct pmc_info *pi, struct pmc **ppmc)
369 char beri_name[PMC_NAME_MAX];
372 KASSERT(cpu >= 0 && cpu < pmc_cpu_max(),
373 ("[beri,%d], illegal CPU %d", __LINE__, cpu));
374 KASSERT(ri >= 0 && ri < beri_npmcs,
375 ("[beri,%d] row-index %d out of range", __LINE__, ri));
377 phw = &beri_pcpu[cpu]->pc_beripmcs[ri];
378 snprintf(beri_name, sizeof(beri_name), "MIPS-%d", ri);
379 if ((error = copystr(beri_name, pi->pm_name, PMC_NAME_MAX,
382 pi->pm_class = beri_pmc_spec.ps_cpuclass;
383 if (phw->phw_state & PMC_PHW_FLAG_IS_ENABLED) {
384 pi->pm_enabled = TRUE;
385 *ppmc = phw->phw_pmc;
387 pi->pm_enabled = FALSE;
395 beri_get_config(int cpu, int ri, struct pmc **ppm)
398 *ppm = beri_pcpu[cpu]->pc_beripmcs[ri].phw_pmc;
404 beri_pmc_switch_in(struct pmc_cpu *pc, struct pmc_process *pp)
411 beri_pmc_switch_out(struct pmc_cpu *pc, struct pmc_process *pp)
418 beri_pcpu_init(struct pmc_mdep *md, int cpu)
422 struct beri_cpu *pac;
425 KASSERT(cpu >= 0 && cpu < pmc_cpu_max(),
426 ("[beri,%d] wrong cpu number %d", __LINE__, cpu));
427 PMCDBG1(MDP,INI,1,"beri-init cpu=%d", cpu);
429 beri_pcpu[cpu] = pac = malloc(sizeof(struct beri_cpu), M_PMC,
431 pac->pc_beripmcs = malloc(sizeof(struct pmc_hw) * beri_npmcs,
432 M_PMC, M_WAITOK|M_ZERO);
434 first_ri = md->pmd_classdep[PMC_MDEP_CLASS_INDEX_MIPS].pcd_ri;
435 KASSERT(pc != NULL, ("[beri,%d] NULL per-cpu pointer", __LINE__));
437 for (i = 0, phw = pac->pc_beripmcs; i < beri_npmcs; i++, phw++) {
438 phw->phw_state = PMC_PHW_FLAG_IS_ENABLED |
439 PMC_PHW_CPU_TO_STATE(cpu) | PMC_PHW_INDEX_TO_STATE(i);
441 pc->pc_hwpmcs[i + first_ri] = phw;
448 beri_pcpu_fini(struct pmc_mdep *md, int cpu)
455 pmc_beri_initialize()
457 struct pmc_mdep *pmc_mdep;
458 struct pmc_classdep *pcd;
460 snprintf(pmc_cpuid, sizeof(pmc_cpuid), "beri");
464 PMCDBG1(MDP,INI,1,"beri-init npmcs=%d", beri_npmcs);
467 * Allocate space for pointers to PMC HW descriptors and for
468 * the MDEP structure used by MI code.
470 beri_pcpu = malloc(sizeof(struct beri_cpu *) * pmc_cpu_max(), M_PMC,
474 pmc_mdep = pmc_mdep_alloc(1);
476 pmc_mdep->pmd_cputype = beri_pmc_spec.ps_cputype;
478 pcd = &pmc_mdep->pmd_classdep[PMC_MDEP_CLASS_INDEX_MIPS];
479 pcd->pcd_caps = beri_pmc_spec.ps_capabilities;
480 pcd->pcd_class = beri_pmc_spec.ps_cpuclass;
481 pcd->pcd_num = beri_npmcs;
482 pcd->pcd_ri = pmc_mdep->pmd_npmc;
483 pcd->pcd_width = beri_pmc_spec.ps_counter_width;
485 pcd->pcd_allocate_pmc = beri_allocate_pmc;
486 pcd->pcd_config_pmc = beri_config_pmc;
487 pcd->pcd_pcpu_fini = beri_pcpu_fini;
488 pcd->pcd_pcpu_init = beri_pcpu_init;
489 pcd->pcd_describe = beri_describe;
490 pcd->pcd_get_config = beri_get_config;
491 pcd->pcd_read_pmc = beri_read_pmc;
492 pcd->pcd_release_pmc = beri_release_pmc;
493 pcd->pcd_start_pmc = beri_start_pmc;
494 pcd->pcd_stop_pmc = beri_stop_pmc;
495 pcd->pcd_write_pmc = beri_write_pmc;
497 pmc_mdep->pmd_intr = NULL;
498 pmc_mdep->pmd_switch_in = beri_pmc_switch_in;
499 pmc_mdep->pmd_switch_out = beri_pmc_switch_out;
501 pmc_mdep->pmd_npmc += beri_npmcs;
507 pmc_beri_finalize(struct pmc_mdep *md)
516 return (pmc_beri_initialize());
520 pmc_md_finalize(struct pmc_mdep *md)
523 return (pmc_beri_finalize(md));
527 pmc_save_kernel_callchain(uintptr_t *cc, int nframes,
528 struct trapframe *tf)
535 pmc_save_user_callchain(uintptr_t *cc, int nframes,
536 struct trapframe *tf)