2 * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
4 * Copyright (c) 2011,2013 Justin Hibbits
5 * Copyright (c) 2005, Joseph Koshy
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
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.
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
31 #include <sys/cdefs.h>
32 __FBSDID("$FreeBSD$");
34 #include <sys/param.h>
36 #include <sys/pmckern.h>
37 #include <sys/sysent.h>
38 #include <sys/systm.h>
40 #include <machine/pmc_mdep.h>
41 #include <machine/spr.h>
42 #include <machine/pte.h>
43 #include <machine/sr.h>
44 #include <machine/cpu.h>
45 #include <machine/stack.h>
47 #include "hwpmc_powerpc.h"
50 #define OFFSET 4 /* Account for the TOC reload slot */
55 struct powerpc_cpu **powerpc_pcpu;
58 pmc_save_kernel_callchain(uintptr_t *cc, int maxsamples,
65 cc[frames++] = PMC_TRAPFRAME_TO_PC(tf);
66 sp = (uintptr_t *)PMC_TRAPFRAME_TO_FP(tf);
67 osp = (uintptr_t *)PAGE_SIZE;
69 for (; frames < maxsamples; frames++) {
77 if ((pc & 3) || (pc < 0x100))
81 * trapexit() and asttrapexit() are sentinels
82 * for kernel stack tracing.
84 if (pc + OFFSET == (uintptr_t) &trapexit ||
85 pc + OFFSET == (uintptr_t) &asttrapexit)
90 sp = (uintptr_t *)*sp;
96 powerpc_switch_in(struct pmc_cpu *pc, struct pmc_process *pp)
103 powerpc_switch_out(struct pmc_cpu *pc, struct pmc_process *pp)
110 powerpc_describe(int cpu, int ri, struct pmc_info *pi, struct pmc **ppmc)
114 char powerpc_name[PMC_NAME_MAX];
116 KASSERT(cpu >= 0 && cpu < pmc_cpu_max(),
117 ("[powerpc,%d], illegal CPU %d", __LINE__, cpu));
119 phw = &powerpc_pcpu[cpu]->pc_ppcpmcs[ri];
120 snprintf(powerpc_name, sizeof(powerpc_name), "POWERPC-%d", ri);
121 if ((error = copystr(powerpc_name, pi->pm_name, PMC_NAME_MAX,
124 pi->pm_class = powerpc_pcpu[cpu]->pc_class;
125 if (phw->phw_state & PMC_PHW_FLAG_IS_ENABLED) {
126 pi->pm_enabled = TRUE;
127 *ppmc = phw->phw_pmc;
129 pi->pm_enabled = FALSE;
137 powerpc_get_config(int cpu, int ri, struct pmc **ppm)
140 *ppm = powerpc_pcpu[cpu]->pc_ppcpmcs[ri].phw_pmc;
148 struct pmc_mdep *pmc_mdep;
153 * Allocate space for pointers to PMC HW descriptors and for
154 * the MDEP structure used by MI code.
156 powerpc_pcpu = malloc(sizeof(struct powerpc_cpu *) * pmc_cpu_max(), M_PMC,
160 pmc_mdep = pmc_mdep_alloc(1);
162 vers = mfpvr() >> 16;
164 pmc_mdep->pmd_switch_in = powerpc_switch_in;
165 pmc_mdep->pmd_switch_out = powerpc_switch_out;
173 error = pmc_mpc7xxx_initialize(pmc_mdep);
178 error = pmc_ppc970_initialize(pmc_mdep);
184 error = pmc_e500_initialize(pmc_mdep);
192 pmc_mdep_free(pmc_mdep);
200 pmc_md_finalize(struct pmc_mdep *md)
203 free(powerpc_pcpu, M_PMC);
208 pmc_save_user_callchain(uintptr_t *cc, int maxsamples,
209 struct trapframe *tf)
214 cc[frames++] = PMC_TRAPFRAME_TO_PC(tf);
215 sp = (uintptr_t *)PMC_TRAPFRAME_TO_FP(tf);
218 for (; frames < maxsamples; frames++) {
223 /* Check if 32-bit mode. */
224 if (!(tf->srr1 & PSL_SF)) {
225 cc[frames] = fuword32((uint32_t *)sp + 1);
226 sp = (uintptr_t *)(uintptr_t)fuword32(sp);
228 cc[frames] = fuword(sp + 2);
229 sp = (uintptr_t *)fuword(sp);
232 cc[frames] = fuword32((uint32_t *)sp + 1);
233 sp = (uintptr_t *)fuword32(sp);