2 * Copyright (c) 2011,2013 Justin Hibbits
3 * Copyright (c) 2005, Joseph Koshy
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * 2. Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
15 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
16 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
19 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
20 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
21 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
22 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
23 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
24 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29 #include <sys/cdefs.h>
30 __FBSDID("$FreeBSD$");
32 #include <sys/param.h>
34 #include <sys/pmckern.h>
35 #include <sys/sysent.h>
36 #include <sys/systm.h>
38 #include <machine/pmc_mdep.h>
39 #include <machine/spr.h>
40 #include <machine/pte.h>
41 #include <machine/sr.h>
42 #include <machine/cpu.h>
43 #include <machine/vmparam.h> /* For VM_MIN_KERNEL_ADDRESS/VM_MAX_KERNEL_ADDRESS */
45 #include "hwpmc_powerpc.h"
47 #define INKERNEL(x) (((vm_offset_t)(x)) <= VM_MAX_KERNEL_ADDRESS && \
48 ((vm_offset_t)(x)) >= VM_MIN_KERNEL_ADDRESS)
49 #define INUSER(x) (((vm_offset_t)(x)) <= VM_MAXUSER_ADDRESS && \
50 ((vm_offset_t)(x)) >= VM_MIN_ADDRESS)
52 struct powerpc_cpu **powerpc_pcpu;
55 pmc_save_kernel_callchain(uintptr_t *cc, int maxsamples,
61 cc[frames++] = PMC_TRAPFRAME_TO_PC(tf);
62 sp = (uintptr_t *)PMC_TRAPFRAME_TO_FP(tf);
64 for (; frames < maxsamples; frames++) {
72 sp = (uintptr_t *)*sp;
78 powerpc_switch_in(struct pmc_cpu *pc, struct pmc_process *pp)
85 powerpc_switch_out(struct pmc_cpu *pc, struct pmc_process *pp)
92 powerpc_describe(int cpu, int ri, struct pmc_info *pi, struct pmc **ppmc)
96 char powerpc_name[PMC_NAME_MAX];
98 KASSERT(cpu >= 0 && cpu < pmc_cpu_max(),
99 ("[powerpc,%d], illegal CPU %d", __LINE__, cpu));
101 phw = &powerpc_pcpu[cpu]->pc_ppcpmcs[ri];
102 snprintf(powerpc_name, sizeof(powerpc_name), "POWERPC-%d", ri);
103 if ((error = copystr(powerpc_name, pi->pm_name, PMC_NAME_MAX,
106 pi->pm_class = powerpc_pcpu[cpu]->pc_class;
107 if (phw->phw_state & PMC_PHW_FLAG_IS_ENABLED) {
108 pi->pm_enabled = TRUE;
109 *ppmc = phw->phw_pmc;
111 pi->pm_enabled = FALSE;
119 powerpc_get_config(int cpu, int ri, struct pmc **ppm)
122 *ppm = powerpc_pcpu[cpu]->pc_ppcpmcs[ri].phw_pmc;
130 struct pmc_mdep *pmc_mdep;
135 * Allocate space for pointers to PMC HW descriptors and for
136 * the MDEP structure used by MI code.
138 powerpc_pcpu = malloc(sizeof(struct powerpc_cpu *) * pmc_cpu_max(), M_PMC,
142 pmc_mdep = pmc_mdep_alloc(1);
144 vers = mfpvr() >> 16;
146 pmc_mdep->pmd_switch_in = powerpc_switch_in;
147 pmc_mdep->pmd_switch_out = powerpc_switch_out;
155 error = pmc_mpc7xxx_initialize(pmc_mdep);
160 error = pmc_ppc970_initialize(pmc_mdep);
168 pmc_mdep_free(pmc_mdep);
176 pmc_md_finalize(struct pmc_mdep *md)
179 free(powerpc_pcpu, M_PMC);
184 pmc_save_user_callchain(uintptr_t *cc, int maxsamples,
185 struct trapframe *tf)
190 cc[frames++] = PMC_TRAPFRAME_TO_PC(tf);
191 sp = (uintptr_t *)PMC_TRAPFRAME_TO_FP(tf);
193 for (; frames < maxsamples; frames++) {
197 /* Check if 32-bit mode. */
198 if (!(tf->srr1 & PSL_SF)) {
199 cc[frames++] = fuword32((uint32_t *)sp + 1);
200 sp = (uintptr_t *)(uintptr_t)fuword32(sp);
202 cc[frames++] = fuword(sp + 2);
203 sp = (uintptr_t *)fuword(sp);
206 cc[frames++] = fuword32((uint32_t *)sp + 1);
207 sp = (uintptr_t *)fuword32(sp);