xref: /freebsd/sys/dev/hwpmc/hwpmc_arm64.c (revision 13ec1e3155c7e9bf037b12af186351b7fa9b9450)
1 /*-
2  * Copyright (c) 2015 Ruslan Bukin <br@bsdpad.com>
3  * All rights reserved.
4  *
5  * This software was developed by the University of Cambridge Computer
6  * Laboratory with support from ARM Ltd.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions
10  * are met:
11  * 1. Redistributions of source code must retain the above copyright
12  *    notice, this list of conditions and the following disclaimer.
13  * 2. Redistributions in binary form must reproduce the above copyright
14  *    notice, this list of conditions and the following disclaimer in the
15  *    documentation and/or other materials provided with the distribution.
16  *
17  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
18  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
21  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27  * SUCH DAMAGE.
28  */
29 
30 #include <sys/cdefs.h>
31 __FBSDID("$FreeBSD$");
32 
33 #include <sys/param.h>
34 #include <sys/systm.h>
35 #include <sys/pmc.h>
36 #include <sys/pmckern.h>
37 
38 #include <machine/pmc_mdep.h>
39 #include <machine/cpu.h>
40 
41 static int arm64_npmcs;
42 
43 struct arm64_event_code_map {
44 	enum pmc_event	pe_ev;
45 	uint8_t		pe_code;
46 };
47 
48 /*
49  * Per-processor information.
50  */
51 struct arm64_cpu {
52 	struct pmc_hw   *pc_arm64pmcs;
53 };
54 
55 static struct arm64_cpu **arm64_pcpu;
56 
57 /*
58  * Interrupt Enable Set Register
59  */
60 static __inline void
61 arm64_interrupt_enable(uint32_t pmc)
62 {
63 	uint32_t reg;
64 
65 	reg = (1 << pmc);
66 	WRITE_SPECIALREG(pmintenset_el1, reg);
67 
68 	isb();
69 }
70 
71 /*
72  * Interrupt Clear Set Register
73  */
74 static __inline void
75 arm64_interrupt_disable(uint32_t pmc)
76 {
77 	uint32_t reg;
78 
79 	reg = (1 << pmc);
80 	WRITE_SPECIALREG(pmintenclr_el1, reg);
81 
82 	isb();
83 }
84 
85 /*
86  * Counter Set Enable Register
87  */
88 static __inline void
89 arm64_counter_enable(unsigned int pmc)
90 {
91 	uint32_t reg;
92 
93 	reg = (1 << pmc);
94 	WRITE_SPECIALREG(pmcntenset_el0, reg);
95 
96 	isb();
97 }
98 
99 /*
100  * Counter Clear Enable Register
101  */
102 static __inline void
103 arm64_counter_disable(unsigned int pmc)
104 {
105 	uint32_t reg;
106 
107 	reg = (1 << pmc);
108 	WRITE_SPECIALREG(pmcntenclr_el0, reg);
109 
110 	isb();
111 }
112 
113 /*
114  * Performance Monitors Control Register
115  */
116 static uint32_t
117 arm64_pmcr_read(void)
118 {
119 	uint32_t reg;
120 
121 	reg = READ_SPECIALREG(pmcr_el0);
122 
123 	return (reg);
124 }
125 
126 static void
127 arm64_pmcr_write(uint32_t reg)
128 {
129 
130 	WRITE_SPECIALREG(pmcr_el0, reg);
131 
132 	isb();
133 }
134 
135 /*
136  * Performance Count Register N
137  */
138 static uint32_t
139 arm64_pmcn_read(unsigned int pmc)
140 {
141 
142 	KASSERT(pmc < arm64_npmcs, ("%s: illegal PMC number %d", __func__, pmc));
143 
144 	WRITE_SPECIALREG(pmselr_el0, pmc);
145 
146 	isb();
147 
148 	return (READ_SPECIALREG(pmxevcntr_el0));
149 }
150 
151 static void
152 arm64_pmcn_write(unsigned int pmc, uint32_t reg)
153 {
154 
155 	KASSERT(pmc < arm64_npmcs, ("%s: illegal PMC number %d", __func__, pmc));
156 
157 	WRITE_SPECIALREG(pmselr_el0, pmc);
158 	WRITE_SPECIALREG(pmxevcntr_el0, reg);
159 
160 	isb();
161 }
162 
163 static int
164 arm64_allocate_pmc(int cpu, int ri, struct pmc *pm,
165   const struct pmc_op_pmcallocate *a)
166 {
167 	uint32_t config;
168 	struct arm64_cpu *pac;
169 	enum pmc_event pe;
170 
171 	KASSERT(cpu >= 0 && cpu < pmc_cpu_max(),
172 	    ("[arm64,%d] illegal CPU value %d", __LINE__, cpu));
173 	KASSERT(ri >= 0 && ri < arm64_npmcs,
174 	    ("[arm64,%d] illegal row index %d", __LINE__, ri));
175 
176 	pac = arm64_pcpu[cpu];
177 
178 	if (a->pm_class != PMC_CLASS_ARMV8) {
179 		return (EINVAL);
180 	}
181 	pe = a->pm_ev;
182 
183 	/* Adjust the config value if needed. */
184 	config = a->pm_md.pm_md_config;
185 	if ((a->pm_md.pm_md_flags & PM_MD_RAW_EVENT) == 0) {
186 		config = (uint32_t)pe - PMC_EV_ARMV8_FIRST;
187 		if (config > (PMC_EV_ARMV8_LAST - PMC_EV_ARMV8_FIRST))
188 			return (EINVAL);
189 	}
190 	pm->pm_md.pm_arm64.pm_arm64_evsel = config;
191 	PMCDBG2(MDP, ALL, 2, "arm64-allocate ri=%d -> config=0x%x", ri, config);
192 
193 	return (0);
194 }
195 
196 
197 static int
198 arm64_read_pmc(int cpu, int ri, pmc_value_t *v)
199 {
200 	pmc_value_t tmp;
201 	struct pmc *pm;
202 	register_t s;
203 	int reg;
204 
205 	KASSERT(cpu >= 0 && cpu < pmc_cpu_max(),
206 	    ("[arm64,%d] illegal CPU value %d", __LINE__, cpu));
207 	KASSERT(ri >= 0 && ri < arm64_npmcs,
208 	    ("[arm64,%d] illegal row index %d", __LINE__, ri));
209 
210 	pm  = arm64_pcpu[cpu]->pc_arm64pmcs[ri].phw_pmc;
211 
212 	/*
213 	 * Ensure we don't get interrupted while updating the overflow count.
214 	 */
215 	s = intr_disable();
216 	tmp = arm64_pmcn_read(ri);
217 	reg = (1 << ri);
218 	if ((READ_SPECIALREG(pmovsclr_el0) & reg) != 0) {
219 		/* Clear Overflow Flag */
220 		WRITE_SPECIALREG(pmovsclr_el0, reg);
221 		pm->pm_pcpu_state[cpu].pps_overflowcnt++;
222 
223 		/* Reread counter in case we raced. */
224 		tmp = arm64_pmcn_read(ri);
225 	}
226 	tmp += 0x100000000llu * pm->pm_pcpu_state[cpu].pps_overflowcnt;
227 	intr_restore(s);
228 
229 	PMCDBG2(MDP, REA, 2, "arm64-read id=%d -> %jd", ri, tmp);
230 	if (PMC_IS_SAMPLING_MODE(PMC_TO_MODE(pm))) {
231 		/*
232 		 * Clamp value to 0 if the counter just overflowed,
233 		 * otherwise the returned reload count would wrap to a
234 		 * huge value.
235 		 */
236 		if ((tmp & (1ull << 63)) == 0)
237 			tmp = 0;
238 		else
239 			tmp = ARMV8_PERFCTR_VALUE_TO_RELOAD_COUNT(tmp);
240 	}
241 	*v = tmp;
242 
243 	return (0);
244 }
245 
246 static int
247 arm64_write_pmc(int cpu, int ri, pmc_value_t v)
248 {
249 	struct pmc *pm;
250 
251 	KASSERT(cpu >= 0 && cpu < pmc_cpu_max(),
252 	    ("[arm64,%d] illegal CPU value %d", __LINE__, cpu));
253 	KASSERT(ri >= 0 && ri < arm64_npmcs,
254 	    ("[arm64,%d] illegal row-index %d", __LINE__, ri));
255 
256 	pm  = arm64_pcpu[cpu]->pc_arm64pmcs[ri].phw_pmc;
257 
258 	if (PMC_IS_SAMPLING_MODE(PMC_TO_MODE(pm)))
259 		v = ARMV8_RELOAD_COUNT_TO_PERFCTR_VALUE(v);
260 
261 	PMCDBG3(MDP, WRI, 1, "arm64-write cpu=%d ri=%d v=%jx", cpu, ri, v);
262 
263 	pm->pm_pcpu_state[cpu].pps_overflowcnt = v >> 32;
264 	arm64_pmcn_write(ri, v);
265 
266 	return (0);
267 }
268 
269 static int
270 arm64_config_pmc(int cpu, int ri, struct pmc *pm)
271 {
272 	struct pmc_hw *phw;
273 
274 	PMCDBG3(MDP, CFG, 1, "cpu=%d ri=%d pm=%p", cpu, ri, pm);
275 
276 	KASSERT(cpu >= 0 && cpu < pmc_cpu_max(),
277 	    ("[arm64,%d] illegal CPU value %d", __LINE__, cpu));
278 	KASSERT(ri >= 0 && ri < arm64_npmcs,
279 	    ("[arm64,%d] illegal row-index %d", __LINE__, ri));
280 
281 	phw = &arm64_pcpu[cpu]->pc_arm64pmcs[ri];
282 
283 	KASSERT(pm == NULL || phw->phw_pmc == NULL,
284 	    ("[arm64,%d] pm=%p phw->pm=%p hwpmc not unconfigured",
285 	    __LINE__, pm, phw->phw_pmc));
286 
287 	phw->phw_pmc = pm;
288 
289 	return (0);
290 }
291 
292 static int
293 arm64_start_pmc(int cpu, int ri)
294 {
295 	struct pmc_hw *phw;
296 	uint32_t config;
297 	struct pmc *pm;
298 
299 	phw    = &arm64_pcpu[cpu]->pc_arm64pmcs[ri];
300 	pm     = phw->phw_pmc;
301 	config = pm->pm_md.pm_arm64.pm_arm64_evsel;
302 
303 	/*
304 	 * Configure the event selection.
305 	 */
306 	WRITE_SPECIALREG(pmselr_el0, ri);
307 	WRITE_SPECIALREG(pmxevtyper_el0, config);
308 
309 	isb();
310 
311 	/*
312 	 * Enable the PMC.
313 	 */
314 	arm64_interrupt_enable(ri);
315 	arm64_counter_enable(ri);
316 
317 	return (0);
318 }
319 
320 static int
321 arm64_stop_pmc(int cpu, int ri)
322 {
323 	struct pmc_hw *phw;
324 	struct pmc *pm;
325 
326 	phw    = &arm64_pcpu[cpu]->pc_arm64pmcs[ri];
327 	pm     = phw->phw_pmc;
328 
329 	/*
330 	 * Disable the PMCs.
331 	 */
332 	arm64_counter_disable(ri);
333 	arm64_interrupt_disable(ri);
334 
335 	return (0);
336 }
337 
338 static int
339 arm64_release_pmc(int cpu, int ri, struct pmc *pmc)
340 {
341 	struct pmc_hw *phw;
342 
343 	KASSERT(cpu >= 0 && cpu < pmc_cpu_max(),
344 	    ("[arm64,%d] illegal CPU value %d", __LINE__, cpu));
345 	KASSERT(ri >= 0 && ri < arm64_npmcs,
346 	    ("[arm64,%d] illegal row-index %d", __LINE__, ri));
347 
348 	phw = &arm64_pcpu[cpu]->pc_arm64pmcs[ri];
349 	KASSERT(phw->phw_pmc == NULL,
350 	    ("[arm64,%d] PHW pmc %p non-NULL", __LINE__, phw->phw_pmc));
351 
352 	return (0);
353 }
354 
355 static int
356 arm64_intr(struct trapframe *tf)
357 {
358 	struct arm64_cpu *pc;
359 	int retval, ri;
360 	struct pmc *pm;
361 	int error;
362 	int reg, cpu;
363 
364 	cpu = curcpu;
365 	KASSERT(cpu >= 0 && cpu < pmc_cpu_max(),
366 	    ("[arm64,%d] CPU %d out of range", __LINE__, cpu));
367 
368 	PMCDBG3(MDP,INT,1, "cpu=%d tf=%p um=%d", cpu, (void *)tf,
369 	    TRAPF_USERMODE(tf));
370 
371 	retval = 0;
372 	pc = arm64_pcpu[cpu];
373 
374 	for (ri = 0; ri < arm64_npmcs; ri++) {
375 		pm = arm64_pcpu[cpu]->pc_arm64pmcs[ri].phw_pmc;
376 		if (pm == NULL)
377 			continue;
378 		/* Check if counter is overflowed */
379 		reg = (1 << ri);
380 		if ((READ_SPECIALREG(pmovsclr_el0) & reg) == 0)
381 			continue;
382 		/* Clear Overflow Flag */
383 		WRITE_SPECIALREG(pmovsclr_el0, reg);
384 
385 		isb();
386 
387 		retval = 1; /* Found an interrupting PMC. */
388 
389 		pm->pm_pcpu_state[cpu].pps_overflowcnt += 1;
390 
391 		if (!PMC_IS_SAMPLING_MODE(PMC_TO_MODE(pm)))
392 			continue;
393 
394 		if (pm->pm_state != PMC_STATE_RUNNING)
395 			continue;
396 
397 		error = pmc_process_interrupt(PMC_HR, pm, tf);
398 		if (error)
399 			arm64_stop_pmc(cpu, ri);
400 
401 		/* Reload sampling count */
402 		arm64_write_pmc(cpu, ri, pm->pm_sc.pm_reloadcount);
403 	}
404 
405 	return (retval);
406 }
407 
408 static int
409 arm64_describe(int cpu, int ri, struct pmc_info *pi, struct pmc **ppmc)
410 {
411 	char arm64_name[PMC_NAME_MAX];
412 	struct pmc_hw *phw;
413 	int error;
414 
415 	KASSERT(cpu >= 0 && cpu < pmc_cpu_max(),
416 	    ("[arm64,%d], illegal CPU %d", __LINE__, cpu));
417 	KASSERT(ri >= 0 && ri < arm64_npmcs,
418 	    ("[arm64,%d] row-index %d out of range", __LINE__, ri));
419 
420 	phw = &arm64_pcpu[cpu]->pc_arm64pmcs[ri];
421 	snprintf(arm64_name, sizeof(arm64_name), "ARMV8-%d", ri);
422 	if ((error = copystr(arm64_name, pi->pm_name, PMC_NAME_MAX,
423 	    NULL)) != 0)
424 		return (error);
425 	pi->pm_class = PMC_CLASS_ARMV8;
426 	if (phw->phw_state & PMC_PHW_FLAG_IS_ENABLED) {
427 		pi->pm_enabled = TRUE;
428 		*ppmc = phw->phw_pmc;
429 	} else {
430 		pi->pm_enabled = FALSE;
431 		*ppmc = NULL;
432 	}
433 
434 	return (0);
435 }
436 
437 static int
438 arm64_get_config(int cpu, int ri, struct pmc **ppm)
439 {
440 
441 	*ppm = arm64_pcpu[cpu]->pc_arm64pmcs[ri].phw_pmc;
442 
443 	return (0);
444 }
445 
446 /*
447  * XXX don't know what we should do here.
448  */
449 static int
450 arm64_switch_in(struct pmc_cpu *pc, struct pmc_process *pp)
451 {
452 
453 	return (0);
454 }
455 
456 static int
457 arm64_switch_out(struct pmc_cpu *pc, struct pmc_process *pp)
458 {
459 
460 	return (0);
461 }
462 
463 static int
464 arm64_pcpu_init(struct pmc_mdep *md, int cpu)
465 {
466 	struct arm64_cpu *pac;
467 	struct pmc_hw  *phw;
468 	struct pmc_cpu *pc;
469 	uint64_t pmcr;
470 	int first_ri;
471 	int i;
472 
473 	KASSERT(cpu >= 0 && cpu < pmc_cpu_max(),
474 	    ("[arm64,%d] wrong cpu number %d", __LINE__, cpu));
475 	PMCDBG1(MDP, INI, 1, "arm64-init cpu=%d", cpu);
476 
477 	arm64_pcpu[cpu] = pac = malloc(sizeof(struct arm64_cpu), M_PMC,
478 	    M_WAITOK | M_ZERO);
479 
480 	pac->pc_arm64pmcs = malloc(sizeof(struct pmc_hw) * arm64_npmcs,
481 	    M_PMC, M_WAITOK | M_ZERO);
482 	pc = pmc_pcpu[cpu];
483 	first_ri = md->pmd_classdep[PMC_MDEP_CLASS_INDEX_ARMV8].pcd_ri;
484 	KASSERT(pc != NULL, ("[arm64,%d] NULL per-cpu pointer", __LINE__));
485 
486 	for (i = 0, phw = pac->pc_arm64pmcs; i < arm64_npmcs; i++, phw++) {
487 		phw->phw_state    = PMC_PHW_FLAG_IS_ENABLED |
488 		    PMC_PHW_CPU_TO_STATE(cpu) | PMC_PHW_INDEX_TO_STATE(i);
489 		phw->phw_pmc      = NULL;
490 		pc->pc_hwpmcs[i + first_ri] = phw;
491 	}
492 
493 	/*
494 	 * Disable all counters and overflow interrupts. Upon reset they are in
495 	 * an undefined state.
496 	 *
497 	 * Don't issue an isb here, just wait for the one in arm64_pmcr_write()
498 	 * to make the writes visible.
499 	 */
500 	WRITE_SPECIALREG(pmcntenclr_el0, 0xffffffff);
501 	WRITE_SPECIALREG(pmintenclr_el1, 0xffffffff);
502 
503 	/* Enable unit */
504 	pmcr = arm64_pmcr_read();
505 	pmcr |= PMCR_E;
506 	arm64_pmcr_write(pmcr);
507 
508 	return (0);
509 }
510 
511 static int
512 arm64_pcpu_fini(struct pmc_mdep *md, int cpu)
513 {
514 	uint32_t pmcr;
515 
516 	pmcr = arm64_pmcr_read();
517 	pmcr &= ~PMCR_E;
518 	arm64_pmcr_write(pmcr);
519 
520 	return (0);
521 }
522 
523 struct pmc_mdep *
524 pmc_arm64_initialize()
525 {
526 	struct pmc_mdep *pmc_mdep;
527 	struct pmc_classdep *pcd;
528 	int idcode, impcode;
529 	int reg;
530 	uint64_t midr;
531 
532 	reg = arm64_pmcr_read();
533 	arm64_npmcs = (reg & PMCR_N_MASK) >> PMCR_N_SHIFT;
534 	impcode = (reg & PMCR_IMP_MASK) >> PMCR_IMP_SHIFT;
535 	idcode = (reg & PMCR_IDCODE_MASK) >> PMCR_IDCODE_SHIFT;
536 
537 	PMCDBG1(MDP, INI, 1, "arm64-init npmcs=%d", arm64_npmcs);
538 
539 	/*
540 	 * Write the CPU model to kern.hwpmc.cpuid.
541 	 *
542 	 * We zero the variant and revision fields.
543 	 *
544 	 * TODO: how to handle differences between cores due to big.LITTLE?
545 	 * For now, just use MIDR from CPU 0.
546 	 */
547 	midr = (uint64_t)(pcpu_find(0)->pc_midr);
548 	midr &= ~(CPU_VAR_MASK | CPU_REV_MASK);
549 	snprintf(pmc_cpuid, sizeof(pmc_cpuid), "0x%016lx", midr);
550 
551 	/*
552 	 * Allocate space for pointers to PMC HW descriptors and for
553 	 * the MDEP structure used by MI code.
554 	 */
555 	arm64_pcpu = malloc(sizeof(struct arm64_cpu *) * pmc_cpu_max(),
556 		M_PMC, M_WAITOK | M_ZERO);
557 
558 	/* Just one class */
559 	pmc_mdep = pmc_mdep_alloc(1);
560 
561 	switch(impcode) {
562 	case PMCR_IMP_ARM:
563 		switch (idcode) {
564 		case PMCR_IDCODE_CORTEX_A76:
565 		case PMCR_IDCODE_NEOVERSE_N1:
566 			pmc_mdep->pmd_cputype = PMC_CPU_ARMV8_CORTEX_A76;
567 			break;
568 		case PMCR_IDCODE_CORTEX_A57:
569 		case PMCR_IDCODE_CORTEX_A72:
570 			pmc_mdep->pmd_cputype = PMC_CPU_ARMV8_CORTEX_A57;
571 			break;
572 		default:
573 		case PMCR_IDCODE_CORTEX_A53:
574 			pmc_mdep->pmd_cputype = PMC_CPU_ARMV8_CORTEX_A53;
575 			break;
576 		}
577 		break;
578 	default:
579 		pmc_mdep->pmd_cputype = PMC_CPU_ARMV8_CORTEX_A53;
580 		break;
581 	}
582 
583 	pcd = &pmc_mdep->pmd_classdep[PMC_MDEP_CLASS_INDEX_ARMV8];
584 	pcd->pcd_caps  = ARMV8_PMC_CAPS;
585 	pcd->pcd_class = PMC_CLASS_ARMV8;
586 	pcd->pcd_num   = arm64_npmcs;
587 	pcd->pcd_ri    = pmc_mdep->pmd_npmc;
588 	pcd->pcd_width = 32;
589 
590 	pcd->pcd_allocate_pmc   = arm64_allocate_pmc;
591 	pcd->pcd_config_pmc     = arm64_config_pmc;
592 	pcd->pcd_pcpu_fini      = arm64_pcpu_fini;
593 	pcd->pcd_pcpu_init      = arm64_pcpu_init;
594 	pcd->pcd_describe       = arm64_describe;
595 	pcd->pcd_get_config     = arm64_get_config;
596 	pcd->pcd_read_pmc       = arm64_read_pmc;
597 	pcd->pcd_release_pmc    = arm64_release_pmc;
598 	pcd->pcd_start_pmc      = arm64_start_pmc;
599 	pcd->pcd_stop_pmc       = arm64_stop_pmc;
600 	pcd->pcd_write_pmc      = arm64_write_pmc;
601 
602 	pmc_mdep->pmd_intr       = arm64_intr;
603 	pmc_mdep->pmd_switch_in  = arm64_switch_in;
604 	pmc_mdep->pmd_switch_out = arm64_switch_out;
605 
606 	pmc_mdep->pmd_npmc   += arm64_npmcs;
607 
608 	return (pmc_mdep);
609 }
610 
611 void
612 pmc_arm64_finalize(struct pmc_mdep *md)
613 {
614 
615 }
616