1f263522aSJoseph Koshy /*- 2b2fb58a1SJustin Hibbits * Copyright (c) 2011,2013 Justin Hibbits 3f263522aSJoseph Koshy * Copyright (c) 2005, Joseph Koshy 4f263522aSJoseph Koshy * All rights reserved. 5f263522aSJoseph Koshy * 6f263522aSJoseph Koshy * Redistribution and use in source and binary forms, with or without 7f263522aSJoseph Koshy * modification, are permitted provided that the following conditions 8f263522aSJoseph Koshy * are met: 9f263522aSJoseph Koshy * 1. Redistributions of source code must retain the above copyright 10f263522aSJoseph Koshy * notice, this list of conditions and the following disclaimer. 11f263522aSJoseph Koshy * 2. Redistributions in binary form must reproduce the above copyright 12f263522aSJoseph Koshy * notice, this list of conditions and the following disclaimer in the 13f263522aSJoseph Koshy * documentation and/or other materials provided with the distribution. 14f263522aSJoseph Koshy * 15f263522aSJoseph Koshy * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 16f263522aSJoseph Koshy * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 17f263522aSJoseph Koshy * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 18f263522aSJoseph Koshy * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 19f263522aSJoseph Koshy * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 20f263522aSJoseph Koshy * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 21f263522aSJoseph Koshy * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 22f263522aSJoseph Koshy * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 23f263522aSJoseph Koshy * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 24f263522aSJoseph Koshy * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 25f263522aSJoseph Koshy * SUCH DAMAGE. 26f263522aSJoseph Koshy * 27f263522aSJoseph Koshy */ 28f263522aSJoseph Koshy 29f263522aSJoseph Koshy #include <sys/cdefs.h> 30f263522aSJoseph Koshy __FBSDID("$FreeBSD$"); 31f263522aSJoseph Koshy 32f263522aSJoseph Koshy #include <sys/param.h> 33f263522aSJoseph Koshy #include <sys/pmc.h> 347b25dccaSJustin Hibbits #include <sys/pmckern.h> 355c0761baSJustin Hibbits #include <sys/sysent.h> 367b25dccaSJustin Hibbits #include <sys/systm.h> 37f263522aSJoseph Koshy 38f263522aSJoseph Koshy #include <machine/pmc_mdep.h> 397b25dccaSJustin Hibbits #include <machine/spr.h> 40177f0102SJustin Hibbits #include <machine/pte.h> 41177f0102SJustin Hibbits #include <machine/sr.h> 427b25dccaSJustin Hibbits #include <machine/cpu.h> 43cddd0f61SJustin Hibbits #include <machine/stack.h> 44f263522aSJoseph Koshy 45b2fb58a1SJustin Hibbits #include "hwpmc_powerpc.h" 469596916cSJoseph Koshy 47cddd0f61SJustin Hibbits #ifdef __powerpc64__ 48cddd0f61SJustin Hibbits #define OFFSET 4 /* Account for the TOC reload slot */ 49cddd0f61SJustin Hibbits #else 50cddd0f61SJustin Hibbits #define OFFSET 0 51cddd0f61SJustin Hibbits #endif 527b25dccaSJustin Hibbits 53995df27cSJustin Hibbits struct powerpc_cpu **powerpc_pcpu; 54995df27cSJustin Hibbits 559596916cSJoseph Koshy int 569596916cSJoseph Koshy pmc_save_kernel_callchain(uintptr_t *cc, int maxsamples, 579596916cSJoseph Koshy struct trapframe *tf) 589596916cSJoseph Koshy { 59e42edd4dSJustin Hibbits uintptr_t *osp, *sp; 60cddd0f61SJustin Hibbits uintptr_t pc; 61b2fb58a1SJustin Hibbits int frames = 0; 62b2fb58a1SJustin Hibbits 630587a072SJustin Hibbits cc[frames++] = PMC_TRAPFRAME_TO_PC(tf); 640587a072SJustin Hibbits sp = (uintptr_t *)PMC_TRAPFRAME_TO_FP(tf); 65cddd0f61SJustin Hibbits osp = (uintptr_t *)PAGE_SIZE; 66b2fb58a1SJustin Hibbits 675c0761baSJustin Hibbits for (; frames < maxsamples; frames++) { 68cddd0f61SJustin Hibbits if (sp <= osp) 69b2fb58a1SJustin Hibbits break; 705c0761baSJustin Hibbits #ifdef __powerpc64__ 71cddd0f61SJustin Hibbits pc = sp[2]; 725c0761baSJustin Hibbits #else 73cddd0f61SJustin Hibbits pc = sp[1]; 745c0761baSJustin Hibbits #endif 75cddd0f61SJustin Hibbits if ((pc & 3) || (pc < 0x100)) 76cddd0f61SJustin Hibbits break; 77cddd0f61SJustin Hibbits 78cddd0f61SJustin Hibbits /* 79cddd0f61SJustin Hibbits * trapexit() and asttrapexit() are sentinels 80cddd0f61SJustin Hibbits * for kernel stack tracing. 81cddd0f61SJustin Hibbits * */ 82cddd0f61SJustin Hibbits if (pc + OFFSET == (uintptr_t) &trapexit || 83cddd0f61SJustin Hibbits pc + OFFSET == (uintptr_t) &asttrapexit) 84cddd0f61SJustin Hibbits break; 85cddd0f61SJustin Hibbits 86cddd0f61SJustin Hibbits cc[frames] = pc; 87e42edd4dSJustin Hibbits osp = sp; 88b2fb58a1SJustin Hibbits sp = (uintptr_t *)*sp; 899596916cSJoseph Koshy } 90b2fb58a1SJustin Hibbits return (frames); 917b25dccaSJustin Hibbits } 927b25dccaSJustin Hibbits 937b25dccaSJustin Hibbits static int 947b25dccaSJustin Hibbits powerpc_switch_in(struct pmc_cpu *pc, struct pmc_process *pp) 957b25dccaSJustin Hibbits { 965c0761baSJustin Hibbits 97b2fb58a1SJustin Hibbits return (0); 987b25dccaSJustin Hibbits } 997b25dccaSJustin Hibbits 1007b25dccaSJustin Hibbits static int 1017b25dccaSJustin Hibbits powerpc_switch_out(struct pmc_cpu *pc, struct pmc_process *pp) 1027b25dccaSJustin Hibbits { 1035c0761baSJustin Hibbits 104b2fb58a1SJustin Hibbits return (0); 1057b25dccaSJustin Hibbits } 1067b25dccaSJustin Hibbits 107b2fb58a1SJustin Hibbits int 1087b25dccaSJustin Hibbits powerpc_describe(int cpu, int ri, struct pmc_info *pi, struct pmc **ppmc) 1097b25dccaSJustin Hibbits { 1107b25dccaSJustin Hibbits int error; 1117b25dccaSJustin Hibbits struct pmc_hw *phw; 1127b25dccaSJustin Hibbits char powerpc_name[PMC_NAME_MAX]; 1137b25dccaSJustin Hibbits 1147b25dccaSJustin Hibbits KASSERT(cpu >= 0 && cpu < pmc_cpu_max(), 1157b25dccaSJustin Hibbits ("[powerpc,%d], illegal CPU %d", __LINE__, cpu)); 1167b25dccaSJustin Hibbits 1177b25dccaSJustin Hibbits phw = &powerpc_pcpu[cpu]->pc_ppcpmcs[ri]; 1187b25dccaSJustin Hibbits snprintf(powerpc_name, sizeof(powerpc_name), "POWERPC-%d", ri); 1197b25dccaSJustin Hibbits if ((error = copystr(powerpc_name, pi->pm_name, PMC_NAME_MAX, 1207b25dccaSJustin Hibbits NULL)) != 0) 1217b25dccaSJustin Hibbits return error; 122169dd953SJustin Hibbits pi->pm_class = powerpc_pcpu[cpu]->pc_class; 1237b25dccaSJustin Hibbits if (phw->phw_state & PMC_PHW_FLAG_IS_ENABLED) { 1247b25dccaSJustin Hibbits pi->pm_enabled = TRUE; 1257b25dccaSJustin Hibbits *ppmc = phw->phw_pmc; 1267b25dccaSJustin Hibbits } else { 1277b25dccaSJustin Hibbits pi->pm_enabled = FALSE; 1287b25dccaSJustin Hibbits *ppmc = NULL; 1297b25dccaSJustin Hibbits } 1307b25dccaSJustin Hibbits 1317b25dccaSJustin Hibbits return (0); 1327b25dccaSJustin Hibbits } 1337b25dccaSJustin Hibbits 134b2fb58a1SJustin Hibbits int 1357b25dccaSJustin Hibbits powerpc_get_config(int cpu, int ri, struct pmc **ppm) 1367b25dccaSJustin Hibbits { 1375c0761baSJustin Hibbits 1387b25dccaSJustin Hibbits *ppm = powerpc_pcpu[cpu]->pc_ppcpmcs[ri].phw_pmc; 1397b25dccaSJustin Hibbits 140b2fb58a1SJustin Hibbits return (0); 1417b25dccaSJustin Hibbits } 1427b25dccaSJustin Hibbits 1437b25dccaSJustin Hibbits struct pmc_mdep * 1447b25dccaSJustin Hibbits pmc_md_initialize() 1457b25dccaSJustin Hibbits { 1467b25dccaSJustin Hibbits struct pmc_mdep *pmc_mdep; 147b2fb58a1SJustin Hibbits int error; 148b2fb58a1SJustin Hibbits uint16_t vers; 1497b25dccaSJustin Hibbits 1507b25dccaSJustin Hibbits /* 1517b25dccaSJustin Hibbits * Allocate space for pointers to PMC HW descriptors and for 1527b25dccaSJustin Hibbits * the MDEP structure used by MI code. 1537b25dccaSJustin Hibbits */ 1547b25dccaSJustin Hibbits powerpc_pcpu = malloc(sizeof(struct powerpc_cpu *) * pmc_cpu_max(), M_PMC, 1557b25dccaSJustin Hibbits M_WAITOK|M_ZERO); 1567b25dccaSJustin Hibbits 1577b25dccaSJustin Hibbits /* Just one class */ 1585df02332SFabien Thomas pmc_mdep = pmc_mdep_alloc(1); 1597b25dccaSJustin Hibbits 160b2fb58a1SJustin Hibbits vers = mfpvr() >> 16; 1617b25dccaSJustin Hibbits 1627b25dccaSJustin Hibbits pmc_mdep->pmd_switch_in = powerpc_switch_in; 1637b25dccaSJustin Hibbits pmc_mdep->pmd_switch_out = powerpc_switch_out; 1647b25dccaSJustin Hibbits 165b2fb58a1SJustin Hibbits switch (vers) { 166b2fb58a1SJustin Hibbits case MPC7447A: 167b2fb58a1SJustin Hibbits case MPC7448: 168b2fb58a1SJustin Hibbits case MPC7450: 169b2fb58a1SJustin Hibbits case MPC7455: 170b2fb58a1SJustin Hibbits case MPC7457: 171b2fb58a1SJustin Hibbits error = pmc_mpc7xxx_initialize(pmc_mdep); 172e35effceSJustin Hibbits break; 173b2fb58a1SJustin Hibbits case IBM970: 174b2fb58a1SJustin Hibbits case IBM970FX: 175b2fb58a1SJustin Hibbits case IBM970MP: 176169dd953SJustin Hibbits error = pmc_ppc970_initialize(pmc_mdep); 177169dd953SJustin Hibbits break; 178*a7452468SJustin Hibbits case FSL_E500v1: 179*a7452468SJustin Hibbits case FSL_E500v2: 180*a7452468SJustin Hibbits case FSL_E500mc: 181*a7452468SJustin Hibbits error = pmc_e500_initialize(pmc_mdep); 182*a7452468SJustin Hibbits break; 183b2fb58a1SJustin Hibbits default: 184b2fb58a1SJustin Hibbits error = -1; 185b2fb58a1SJustin Hibbits break; 186b2fb58a1SJustin Hibbits } 187b2fb58a1SJustin Hibbits 188b2fb58a1SJustin Hibbits if (error != 0) { 189b2fb58a1SJustin Hibbits pmc_mdep_free(pmc_mdep); 190b2fb58a1SJustin Hibbits pmc_mdep = NULL; 191b2fb58a1SJustin Hibbits } 1927b25dccaSJustin Hibbits 1937b25dccaSJustin Hibbits return (pmc_mdep); 1947b25dccaSJustin Hibbits } 1957b25dccaSJustin Hibbits 1967b25dccaSJustin Hibbits void 1977b25dccaSJustin Hibbits pmc_md_finalize(struct pmc_mdep *md) 1987b25dccaSJustin Hibbits { 199169dd953SJustin Hibbits 200169dd953SJustin Hibbits free(powerpc_pcpu, M_PMC); 201169dd953SJustin Hibbits powerpc_pcpu = NULL; 2027b25dccaSJustin Hibbits } 2037b25dccaSJustin Hibbits 2049596916cSJoseph Koshy int 2059596916cSJoseph Koshy pmc_save_user_callchain(uintptr_t *cc, int maxsamples, 2069596916cSJoseph Koshy struct trapframe *tf) 2079596916cSJoseph Koshy { 208e42edd4dSJustin Hibbits uintptr_t *osp, *sp; 2090587a072SJustin Hibbits int frames = 0; 2100587a072SJustin Hibbits 2110587a072SJustin Hibbits cc[frames++] = PMC_TRAPFRAME_TO_PC(tf); 2120587a072SJustin Hibbits sp = (uintptr_t *)PMC_TRAPFRAME_TO_FP(tf); 213e42edd4dSJustin Hibbits osp = NULL; 2140587a072SJustin Hibbits 2155c0761baSJustin Hibbits for (; frames < maxsamples; frames++) { 216cddd0f61SJustin Hibbits if (sp <= osp) 2170587a072SJustin Hibbits break; 218e42edd4dSJustin Hibbits osp = sp; 2195c0761baSJustin Hibbits #ifdef __powerpc64__ 2205c0761baSJustin Hibbits /* Check if 32-bit mode. */ 2215c0761baSJustin Hibbits if (!(tf->srr1 & PSL_SF)) { 222e42edd4dSJustin Hibbits cc[frames] = fuword32((uint32_t *)sp + 1); 2235c0761baSJustin Hibbits sp = (uintptr_t *)(uintptr_t)fuword32(sp); 2245c0761baSJustin Hibbits } else { 225e42edd4dSJustin Hibbits cc[frames] = fuword(sp + 2); 2260587a072SJustin Hibbits sp = (uintptr_t *)fuword(sp); 2270587a072SJustin Hibbits } 2285c0761baSJustin Hibbits #else 229e42edd4dSJustin Hibbits cc[frames] = fuword32((uint32_t *)sp + 1); 2305c0761baSJustin Hibbits sp = (uintptr_t *)fuword32(sp); 2315c0761baSJustin Hibbits #endif 2325c0761baSJustin Hibbits } 2335c0761baSJustin Hibbits 2340587a072SJustin Hibbits return (frames); 2359596916cSJoseph Koshy } 236