1 /*-
2 * Copyright (c) 2015 Ruslan Bukin <[email protected]>
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
arm64_interrupt_enable(uint32_t pmc)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
arm64_interrupt_disable(uint32_t pmc)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
arm64_counter_enable(unsigned int pmc)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
arm64_counter_disable(unsigned int pmc)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
arm64_pmcr_read(void)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
arm64_pmcr_write(uint32_t reg)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
arm64_pmcn_read(unsigned int pmc)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
arm64_pmcn_write(unsigned int pmc,uint32_t reg)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
arm64_allocate_pmc(int cpu,int ri,struct pmc * pm,const struct pmc_op_pmcallocate * a)164 arm64_allocate_pmc(int cpu, int ri, struct pmc *pm,
165 const struct pmc_op_pmcallocate *a)
166 {
167 uint32_t caps, 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 caps = a->pm_caps;
179 if (a->pm_class != PMC_CLASS_ARMV8) {
180 return (EINVAL);
181 }
182 pe = a->pm_ev;
183
184 /* Adjust the config value if needed. */
185 config = a->pm_md.pm_md_config;
186 if ((a->pm_md.pm_md_flags & PM_MD_RAW_EVENT) == 0) {
187 config = (uint32_t)pe - PMC_EV_ARMV8_FIRST;
188 if (config > (PMC_EV_ARMV8_LAST - PMC_EV_ARMV8_FIRST))
189 return (EINVAL);
190 }
191 pm->pm_md.pm_arm64.pm_arm64_evsel = config;
192 PMCDBG2(MDP, ALL, 2, "arm64-allocate ri=%d -> config=0x%x", ri, config);
193
194 return (0);
195 }
196
197
198 static int
arm64_read_pmc(int cpu,int ri,pmc_value_t * v)199 arm64_read_pmc(int cpu, int ri, pmc_value_t *v)
200 {
201 pmc_value_t tmp;
202 struct pmc *pm;
203 register_t s;
204 int reg;
205
206 KASSERT(cpu >= 0 && cpu < pmc_cpu_max(),
207 ("[arm64,%d] illegal CPU value %d", __LINE__, cpu));
208 KASSERT(ri >= 0 && ri < arm64_npmcs,
209 ("[arm64,%d] illegal row index %d", __LINE__, ri));
210
211 pm = arm64_pcpu[cpu]->pc_arm64pmcs[ri].phw_pmc;
212
213 /*
214 * Ensure we don't get interrupted while updating the overflow count.
215 */
216 s = intr_disable();
217 tmp = arm64_pmcn_read(ri);
218 reg = (1 << ri);
219 if ((READ_SPECIALREG(pmovsclr_el0) & reg) != 0) {
220 /* Clear Overflow Flag */
221 WRITE_SPECIALREG(pmovsclr_el0, reg);
222 pm->pm_pcpu_state[cpu].pps_overflowcnt++;
223
224 /* Reread counter in case we raced. */
225 tmp = arm64_pmcn_read(ri);
226 }
227 tmp += 0x100000000llu * pm->pm_pcpu_state[cpu].pps_overflowcnt;
228 intr_restore(s);
229
230 PMCDBG2(MDP, REA, 2, "arm64-read id=%d -> %jd", ri, tmp);
231 if (PMC_IS_SAMPLING_MODE(PMC_TO_MODE(pm))) {
232 /*
233 * Clamp value to 0 if the counter just overflowed,
234 * otherwise the returned reload count would wrap to a
235 * huge value.
236 */
237 if ((tmp & (1ull << 63)) == 0)
238 tmp = 0;
239 else
240 tmp = ARMV8_PERFCTR_VALUE_TO_RELOAD_COUNT(tmp);
241 }
242 *v = tmp;
243
244 return (0);
245 }
246
247 static int
arm64_write_pmc(int cpu,int ri,pmc_value_t v)248 arm64_write_pmc(int cpu, int ri, pmc_value_t v)
249 {
250 struct pmc *pm;
251
252 KASSERT(cpu >= 0 && cpu < pmc_cpu_max(),
253 ("[arm64,%d] illegal CPU value %d", __LINE__, cpu));
254 KASSERT(ri >= 0 && ri < arm64_npmcs,
255 ("[arm64,%d] illegal row-index %d", __LINE__, ri));
256
257 pm = arm64_pcpu[cpu]->pc_arm64pmcs[ri].phw_pmc;
258
259 if (PMC_IS_SAMPLING_MODE(PMC_TO_MODE(pm)))
260 v = ARMV8_RELOAD_COUNT_TO_PERFCTR_VALUE(v);
261
262 PMCDBG3(MDP, WRI, 1, "arm64-write cpu=%d ri=%d v=%jx", cpu, ri, v);
263
264 pm->pm_pcpu_state[cpu].pps_overflowcnt = v >> 32;
265 arm64_pmcn_write(ri, v);
266
267 return (0);
268 }
269
270 static int
arm64_config_pmc(int cpu,int ri,struct pmc * pm)271 arm64_config_pmc(int cpu, int ri, struct pmc *pm)
272 {
273 struct pmc_hw *phw;
274
275 PMCDBG3(MDP, CFG, 1, "cpu=%d ri=%d pm=%p", cpu, ri, pm);
276
277 KASSERT(cpu >= 0 && cpu < pmc_cpu_max(),
278 ("[arm64,%d] illegal CPU value %d", __LINE__, cpu));
279 KASSERT(ri >= 0 && ri < arm64_npmcs,
280 ("[arm64,%d] illegal row-index %d", __LINE__, ri));
281
282 phw = &arm64_pcpu[cpu]->pc_arm64pmcs[ri];
283
284 KASSERT(pm == NULL || phw->phw_pmc == NULL,
285 ("[arm64,%d] pm=%p phw->pm=%p hwpmc not unconfigured",
286 __LINE__, pm, phw->phw_pmc));
287
288 phw->phw_pmc = pm;
289
290 return (0);
291 }
292
293 static int
arm64_start_pmc(int cpu,int ri)294 arm64_start_pmc(int cpu, int ri)
295 {
296 struct pmc_hw *phw;
297 uint32_t config;
298 struct pmc *pm;
299
300 phw = &arm64_pcpu[cpu]->pc_arm64pmcs[ri];
301 pm = phw->phw_pmc;
302 config = pm->pm_md.pm_arm64.pm_arm64_evsel;
303
304 /*
305 * Configure the event selection.
306 */
307 WRITE_SPECIALREG(pmselr_el0, ri);
308 WRITE_SPECIALREG(pmxevtyper_el0, config);
309
310 isb();
311
312 /*
313 * Enable the PMC.
314 */
315 arm64_interrupt_enable(ri);
316 arm64_counter_enable(ri);
317
318 return (0);
319 }
320
321 static int
arm64_stop_pmc(int cpu,int ri)322 arm64_stop_pmc(int cpu, int ri)
323 {
324 struct pmc_hw *phw;
325 struct pmc *pm;
326
327 phw = &arm64_pcpu[cpu]->pc_arm64pmcs[ri];
328 pm = phw->phw_pmc;
329
330 /*
331 * Disable the PMCs.
332 */
333 arm64_counter_disable(ri);
334 arm64_interrupt_disable(ri);
335
336 return (0);
337 }
338
339 static int
arm64_release_pmc(int cpu,int ri,struct pmc * pmc)340 arm64_release_pmc(int cpu, int ri, struct pmc *pmc)
341 {
342 struct pmc_hw *phw;
343
344 KASSERT(cpu >= 0 && cpu < pmc_cpu_max(),
345 ("[arm64,%d] illegal CPU value %d", __LINE__, cpu));
346 KASSERT(ri >= 0 && ri < arm64_npmcs,
347 ("[arm64,%d] illegal row-index %d", __LINE__, ri));
348
349 phw = &arm64_pcpu[cpu]->pc_arm64pmcs[ri];
350 KASSERT(phw->phw_pmc == NULL,
351 ("[arm64,%d] PHW pmc %p non-NULL", __LINE__, phw->phw_pmc));
352
353 return (0);
354 }
355
356 static int
arm64_intr(struct trapframe * tf)357 arm64_intr(struct trapframe *tf)
358 {
359 struct arm64_cpu *pc;
360 int retval, ri;
361 struct pmc *pm;
362 int error;
363 int reg, cpu;
364
365 cpu = curcpu;
366 KASSERT(cpu >= 0 && cpu < pmc_cpu_max(),
367 ("[arm64,%d] CPU %d out of range", __LINE__, cpu));
368
369 PMCDBG3(MDP,INT,1, "cpu=%d tf=%p um=%d", cpu, (void *)tf,
370 TRAPF_USERMODE(tf));
371
372 retval = 0;
373 pc = arm64_pcpu[cpu];
374
375 for (ri = 0; ri < arm64_npmcs; ri++) {
376 pm = arm64_pcpu[cpu]->pc_arm64pmcs[ri].phw_pmc;
377 if (pm == NULL)
378 continue;
379 /* Check if counter is overflowed */
380 reg = (1 << ri);
381 if ((READ_SPECIALREG(pmovsclr_el0) & reg) == 0)
382 continue;
383 /* Clear Overflow Flag */
384 WRITE_SPECIALREG(pmovsclr_el0, reg);
385
386 isb();
387
388 retval = 1; /* Found an interrupting PMC. */
389
390 pm->pm_pcpu_state[cpu].pps_overflowcnt += 1;
391
392 if (!PMC_IS_SAMPLING_MODE(PMC_TO_MODE(pm)))
393 continue;
394
395 if (pm->pm_state != PMC_STATE_RUNNING)
396 continue;
397
398 error = pmc_process_interrupt(PMC_HR, pm, tf);
399 if (error)
400 arm64_stop_pmc(cpu, ri);
401
402 /* Reload sampling count */
403 arm64_write_pmc(cpu, ri, pm->pm_sc.pm_reloadcount);
404 }
405
406 return (retval);
407 }
408
409 static int
arm64_describe(int cpu,int ri,struct pmc_info * pi,struct pmc ** ppmc)410 arm64_describe(int cpu, int ri, struct pmc_info *pi, struct pmc **ppmc)
411 {
412 char arm64_name[PMC_NAME_MAX];
413 struct pmc_hw *phw;
414 int error;
415
416 KASSERT(cpu >= 0 && cpu < pmc_cpu_max(),
417 ("[arm64,%d], illegal CPU %d", __LINE__, cpu));
418 KASSERT(ri >= 0 && ri < arm64_npmcs,
419 ("[arm64,%d] row-index %d out of range", __LINE__, ri));
420
421 phw = &arm64_pcpu[cpu]->pc_arm64pmcs[ri];
422 snprintf(arm64_name, sizeof(arm64_name), "ARMV8-%d", ri);
423 if ((error = copystr(arm64_name, pi->pm_name, PMC_NAME_MAX,
424 NULL)) != 0)
425 return (error);
426 pi->pm_class = PMC_CLASS_ARMV8;
427 if (phw->phw_state & PMC_PHW_FLAG_IS_ENABLED) {
428 pi->pm_enabled = TRUE;
429 *ppmc = phw->phw_pmc;
430 } else {
431 pi->pm_enabled = FALSE;
432 *ppmc = NULL;
433 }
434
435 return (0);
436 }
437
438 static int
arm64_get_config(int cpu,int ri,struct pmc ** ppm)439 arm64_get_config(int cpu, int ri, struct pmc **ppm)
440 {
441
442 *ppm = arm64_pcpu[cpu]->pc_arm64pmcs[ri].phw_pmc;
443
444 return (0);
445 }
446
447 /*
448 * XXX don't know what we should do here.
449 */
450 static int
arm64_switch_in(struct pmc_cpu * pc,struct pmc_process * pp)451 arm64_switch_in(struct pmc_cpu *pc, struct pmc_process *pp)
452 {
453
454 return (0);
455 }
456
457 static int
arm64_switch_out(struct pmc_cpu * pc,struct pmc_process * pp)458 arm64_switch_out(struct pmc_cpu *pc, struct pmc_process *pp)
459 {
460
461 return (0);
462 }
463
464 static int
arm64_pcpu_init(struct pmc_mdep * md,int cpu)465 arm64_pcpu_init(struct pmc_mdep *md, int cpu)
466 {
467 struct arm64_cpu *pac;
468 struct pmc_hw *phw;
469 struct pmc_cpu *pc;
470 uint64_t pmcr;
471 int first_ri;
472 int i;
473
474 KASSERT(cpu >= 0 && cpu < pmc_cpu_max(),
475 ("[arm64,%d] wrong cpu number %d", __LINE__, cpu));
476 PMCDBG1(MDP, INI, 1, "arm64-init cpu=%d", cpu);
477
478 arm64_pcpu[cpu] = pac = malloc(sizeof(struct arm64_cpu), M_PMC,
479 M_WAITOK | M_ZERO);
480
481 pac->pc_arm64pmcs = malloc(sizeof(struct pmc_hw) * arm64_npmcs,
482 M_PMC, M_WAITOK | M_ZERO);
483 pc = pmc_pcpu[cpu];
484 first_ri = md->pmd_classdep[PMC_MDEP_CLASS_INDEX_ARMV8].pcd_ri;
485 KASSERT(pc != NULL, ("[arm64,%d] NULL per-cpu pointer", __LINE__));
486
487 for (i = 0, phw = pac->pc_arm64pmcs; i < arm64_npmcs; i++, phw++) {
488 phw->phw_state = PMC_PHW_FLAG_IS_ENABLED |
489 PMC_PHW_CPU_TO_STATE(cpu) | PMC_PHW_INDEX_TO_STATE(i);
490 phw->phw_pmc = NULL;
491 pc->pc_hwpmcs[i + first_ri] = phw;
492 }
493
494 /*
495 * Disable all counters and overflow interrupts. Upon reset they are in
496 * an undefined state.
497 *
498 * Don't issue an isb here, just wait for the one in arm64_pmcr_write()
499 * to make the writes visible.
500 */
501 WRITE_SPECIALREG(pmcntenclr_el0, 0xffffffff);
502 WRITE_SPECIALREG(pmintenclr_el1, 0xffffffff);
503
504 /* Enable unit */
505 pmcr = arm64_pmcr_read();
506 pmcr |= PMCR_E;
507 arm64_pmcr_write(pmcr);
508
509 return (0);
510 }
511
512 static int
arm64_pcpu_fini(struct pmc_mdep * md,int cpu)513 arm64_pcpu_fini(struct pmc_mdep *md, int cpu)
514 {
515 uint32_t pmcr;
516
517 pmcr = arm64_pmcr_read();
518 pmcr &= ~PMCR_E;
519 arm64_pmcr_write(pmcr);
520
521 return (0);
522 }
523
524 struct pmc_mdep *
pmc_arm64_initialize()525 pmc_arm64_initialize()
526 {
527 struct pmc_mdep *pmc_mdep;
528 struct pmc_classdep *pcd;
529 int idcode, impcode;
530 int reg;
531 uint64_t midr;
532
533 reg = arm64_pmcr_read();
534 arm64_npmcs = (reg & PMCR_N_MASK) >> PMCR_N_SHIFT;
535 impcode = (reg & PMCR_IMP_MASK) >> PMCR_IMP_SHIFT;
536 idcode = (reg & PMCR_IDCODE_MASK) >> PMCR_IDCODE_SHIFT;
537
538 PMCDBG1(MDP, INI, 1, "arm64-init npmcs=%d", arm64_npmcs);
539
540 /*
541 * Write the CPU model to kern.hwpmc.cpuid.
542 *
543 * We zero the variant and revision fields.
544 *
545 * TODO: how to handle differences between cores due to big.LITTLE?
546 * For now, just use MIDR from CPU 0.
547 */
548 midr = (uint64_t)(pcpu_find(0)->pc_midr);
549 midr &= ~(CPU_VAR_MASK | CPU_REV_MASK);
550 snprintf(pmc_cpuid, sizeof(pmc_cpuid), "0x%016lx", midr);
551
552 /*
553 * Allocate space for pointers to PMC HW descriptors and for
554 * the MDEP structure used by MI code.
555 */
556 arm64_pcpu = malloc(sizeof(struct arm64_cpu *) * pmc_cpu_max(),
557 M_PMC, M_WAITOK | M_ZERO);
558
559 /* Just one class */
560 pmc_mdep = pmc_mdep_alloc(1);
561
562 switch(impcode) {
563 case PMCR_IMP_ARM:
564 switch (idcode) {
565 case PMCR_IDCODE_CORTEX_A76:
566 case PMCR_IDCODE_NEOVERSE_N1:
567 pmc_mdep->pmd_cputype = PMC_CPU_ARMV8_CORTEX_A76;
568 break;
569 case PMCR_IDCODE_CORTEX_A57:
570 case PMCR_IDCODE_CORTEX_A72:
571 pmc_mdep->pmd_cputype = PMC_CPU_ARMV8_CORTEX_A57;
572 break;
573 default:
574 case PMCR_IDCODE_CORTEX_A53:
575 pmc_mdep->pmd_cputype = PMC_CPU_ARMV8_CORTEX_A53;
576 break;
577 }
578 break;
579 default:
580 pmc_mdep->pmd_cputype = PMC_CPU_ARMV8_CORTEX_A53;
581 break;
582 }
583
584 pcd = &pmc_mdep->pmd_classdep[PMC_MDEP_CLASS_INDEX_ARMV8];
585 pcd->pcd_caps = ARMV8_PMC_CAPS;
586 pcd->pcd_class = PMC_CLASS_ARMV8;
587 pcd->pcd_num = arm64_npmcs;
588 pcd->pcd_ri = pmc_mdep->pmd_npmc;
589 pcd->pcd_width = 32;
590
591 pcd->pcd_allocate_pmc = arm64_allocate_pmc;
592 pcd->pcd_config_pmc = arm64_config_pmc;
593 pcd->pcd_pcpu_fini = arm64_pcpu_fini;
594 pcd->pcd_pcpu_init = arm64_pcpu_init;
595 pcd->pcd_describe = arm64_describe;
596 pcd->pcd_get_config = arm64_get_config;
597 pcd->pcd_read_pmc = arm64_read_pmc;
598 pcd->pcd_release_pmc = arm64_release_pmc;
599 pcd->pcd_start_pmc = arm64_start_pmc;
600 pcd->pcd_stop_pmc = arm64_stop_pmc;
601 pcd->pcd_write_pmc = arm64_write_pmc;
602
603 pmc_mdep->pmd_intr = arm64_intr;
604 pmc_mdep->pmd_switch_in = arm64_switch_in;
605 pmc_mdep->pmd_switch_out = arm64_switch_out;
606
607 pmc_mdep->pmd_npmc += arm64_npmcs;
608
609 return (pmc_mdep);
610 }
611
612 void
pmc_arm64_finalize(struct pmc_mdep * md)613 pmc_arm64_finalize(struct pmc_mdep *md)
614 {
615
616 }
617