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> 357b25dccaSJustin Hibbits #include <sys/systm.h> 36f263522aSJoseph Koshy 37f263522aSJoseph Koshy #include <machine/pmc_mdep.h> 387b25dccaSJustin Hibbits #include <machine/spr.h> 39177f0102SJustin Hibbits #include <machine/pte.h> 40177f0102SJustin Hibbits #include <machine/sr.h> 417b25dccaSJustin Hibbits #include <machine/cpu.h> 42b2fb58a1SJustin Hibbits #include <machine/vmparam.h> /* For VM_MIN_KERNEL_ADDRESS/VM_MAX_KERNEL_ADDRESS */ 43f263522aSJoseph Koshy 44b2fb58a1SJustin Hibbits #include "hwpmc_powerpc.h" 459596916cSJoseph Koshy 46b2fb58a1SJustin Hibbits #define INKERNEL(x) (((vm_offset_t)(x)) <= VM_MAX_KERNEL_ADDRESS && \ 47b2fb58a1SJustin Hibbits ((vm_offset_t)(x)) >= VM_MIN_KERNEL_ADDRESS) 480587a072SJustin Hibbits #define INUSER(x) (((vm_offset_t)(x)) <= VM_MAXUSER_ADDRESS && \ 490587a072SJustin Hibbits ((vm_offset_t)(x)) >= VM_MIN_ADDRESS) 507b25dccaSJustin Hibbits 51995df27cSJustin Hibbits struct powerpc_cpu **powerpc_pcpu; 52995df27cSJustin Hibbits 539596916cSJoseph Koshy int 549596916cSJoseph Koshy pmc_save_kernel_callchain(uintptr_t *cc, int maxsamples, 559596916cSJoseph Koshy struct trapframe *tf) 569596916cSJoseph Koshy { 57b2fb58a1SJustin Hibbits int frames = 0; 58b2fb58a1SJustin Hibbits uintptr_t *sp; 59b2fb58a1SJustin Hibbits 600587a072SJustin Hibbits cc[frames++] = PMC_TRAPFRAME_TO_PC(tf); 610587a072SJustin Hibbits sp = (uintptr_t *)PMC_TRAPFRAME_TO_FP(tf); 62b2fb58a1SJustin Hibbits 63b2fb58a1SJustin Hibbits for (frames = 1; frames < maxsamples; frames++) { 64b2fb58a1SJustin Hibbits if (!INKERNEL(sp)) 65b2fb58a1SJustin Hibbits break; 660587a072SJustin Hibbits cc[frames++] = sp[1]; 67b2fb58a1SJustin Hibbits sp = (uintptr_t *)*sp; 689596916cSJoseph Koshy } 69b2fb58a1SJustin Hibbits return (frames); 707b25dccaSJustin Hibbits } 717b25dccaSJustin Hibbits 727b25dccaSJustin Hibbits static int 737b25dccaSJustin Hibbits powerpc_switch_in(struct pmc_cpu *pc, struct pmc_process *pp) 747b25dccaSJustin Hibbits { 75b2fb58a1SJustin Hibbits return (0); 767b25dccaSJustin Hibbits } 777b25dccaSJustin Hibbits 787b25dccaSJustin Hibbits static int 797b25dccaSJustin Hibbits powerpc_switch_out(struct pmc_cpu *pc, struct pmc_process *pp) 807b25dccaSJustin Hibbits { 81b2fb58a1SJustin Hibbits return (0); 827b25dccaSJustin Hibbits } 837b25dccaSJustin Hibbits 84b2fb58a1SJustin Hibbits int 857b25dccaSJustin Hibbits powerpc_describe(int cpu, int ri, struct pmc_info *pi, struct pmc **ppmc) 867b25dccaSJustin Hibbits { 877b25dccaSJustin Hibbits int error; 887b25dccaSJustin Hibbits struct pmc_hw *phw; 897b25dccaSJustin Hibbits char powerpc_name[PMC_NAME_MAX]; 907b25dccaSJustin Hibbits 917b25dccaSJustin Hibbits KASSERT(cpu >= 0 && cpu < pmc_cpu_max(), 927b25dccaSJustin Hibbits ("[powerpc,%d], illegal CPU %d", __LINE__, cpu)); 937b25dccaSJustin Hibbits 947b25dccaSJustin Hibbits phw = &powerpc_pcpu[cpu]->pc_ppcpmcs[ri]; 957b25dccaSJustin Hibbits snprintf(powerpc_name, sizeof(powerpc_name), "POWERPC-%d", ri); 967b25dccaSJustin Hibbits if ((error = copystr(powerpc_name, pi->pm_name, PMC_NAME_MAX, 977b25dccaSJustin Hibbits NULL)) != 0) 987b25dccaSJustin Hibbits return error; 997b25dccaSJustin Hibbits pi->pm_class = PMC_CLASS_PPC7450; 1007b25dccaSJustin Hibbits if (phw->phw_state & PMC_PHW_FLAG_IS_ENABLED) { 1017b25dccaSJustin Hibbits pi->pm_enabled = TRUE; 1027b25dccaSJustin Hibbits *ppmc = phw->phw_pmc; 1037b25dccaSJustin Hibbits } else { 1047b25dccaSJustin Hibbits pi->pm_enabled = FALSE; 1057b25dccaSJustin Hibbits *ppmc = NULL; 1067b25dccaSJustin Hibbits } 1077b25dccaSJustin Hibbits 1087b25dccaSJustin Hibbits return (0); 1097b25dccaSJustin Hibbits } 1107b25dccaSJustin Hibbits 111b2fb58a1SJustin Hibbits int 1127b25dccaSJustin Hibbits powerpc_get_config(int cpu, int ri, struct pmc **ppm) 1137b25dccaSJustin Hibbits { 1147b25dccaSJustin Hibbits *ppm = powerpc_pcpu[cpu]->pc_ppcpmcs[ri].phw_pmc; 1157b25dccaSJustin Hibbits 116b2fb58a1SJustin Hibbits return (0); 1177b25dccaSJustin Hibbits } 1187b25dccaSJustin Hibbits 1197b25dccaSJustin Hibbits struct pmc_mdep * 1207b25dccaSJustin Hibbits pmc_md_initialize() 1217b25dccaSJustin Hibbits { 1227b25dccaSJustin Hibbits struct pmc_mdep *pmc_mdep; 123b2fb58a1SJustin Hibbits int error; 124b2fb58a1SJustin Hibbits uint16_t vers; 1257b25dccaSJustin Hibbits 1267b25dccaSJustin Hibbits /* 1277b25dccaSJustin Hibbits * Allocate space for pointers to PMC HW descriptors and for 1287b25dccaSJustin Hibbits * the MDEP structure used by MI code. 1297b25dccaSJustin Hibbits */ 1307b25dccaSJustin Hibbits powerpc_pcpu = malloc(sizeof(struct powerpc_cpu *) * pmc_cpu_max(), M_PMC, 1317b25dccaSJustin Hibbits M_WAITOK|M_ZERO); 1327b25dccaSJustin Hibbits 1337b25dccaSJustin Hibbits /* Just one class */ 1345df02332SFabien Thomas pmc_mdep = pmc_mdep_alloc(1); 1357b25dccaSJustin Hibbits 1367b25dccaSJustin Hibbits pmc_mdep->pmd_cputype = PMC_CPU_PPC_7450; 1377b25dccaSJustin Hibbits 138b2fb58a1SJustin Hibbits vers = mfpvr() >> 16; 1397b25dccaSJustin Hibbits 1407b25dccaSJustin Hibbits pmc_mdep->pmd_switch_in = powerpc_switch_in; 1417b25dccaSJustin Hibbits pmc_mdep->pmd_switch_out = powerpc_switch_out; 1427b25dccaSJustin Hibbits 143b2fb58a1SJustin Hibbits switch (vers) { 144b2fb58a1SJustin Hibbits case MPC7447A: 145b2fb58a1SJustin Hibbits case MPC7448: 146b2fb58a1SJustin Hibbits case MPC7450: 147b2fb58a1SJustin Hibbits case MPC7455: 148b2fb58a1SJustin Hibbits case MPC7457: 149b2fb58a1SJustin Hibbits error = pmc_mpc7xxx_initialize(pmc_mdep); 150*e35effceSJustin Hibbits break; 151b2fb58a1SJustin Hibbits case IBM970: 152b2fb58a1SJustin Hibbits case IBM970FX: 153b2fb58a1SJustin Hibbits case IBM970MP: 154b2fb58a1SJustin Hibbits default: 155b2fb58a1SJustin Hibbits error = -1; 156b2fb58a1SJustin Hibbits break; 157b2fb58a1SJustin Hibbits } 158b2fb58a1SJustin Hibbits 159b2fb58a1SJustin Hibbits if (error != 0) { 160b2fb58a1SJustin Hibbits pmc_mdep_free(pmc_mdep); 161b2fb58a1SJustin Hibbits pmc_mdep = NULL; 162b2fb58a1SJustin Hibbits return NULL; 163b2fb58a1SJustin Hibbits } 1647b25dccaSJustin Hibbits 1657b25dccaSJustin Hibbits return (pmc_mdep); 1667b25dccaSJustin Hibbits } 1677b25dccaSJustin Hibbits 1687b25dccaSJustin Hibbits void 1697b25dccaSJustin Hibbits pmc_md_finalize(struct pmc_mdep *md) 1707b25dccaSJustin Hibbits { 1717b25dccaSJustin Hibbits free(md, M_PMC); 1727b25dccaSJustin Hibbits } 1737b25dccaSJustin Hibbits 1749596916cSJoseph Koshy int 1759596916cSJoseph Koshy pmc_save_user_callchain(uintptr_t *cc, int maxsamples, 1769596916cSJoseph Koshy struct trapframe *tf) 1779596916cSJoseph Koshy { 1780587a072SJustin Hibbits uintptr_t *sp; 1790587a072SJustin Hibbits int frames = 0; 1800587a072SJustin Hibbits 1810587a072SJustin Hibbits cc[frames++] = PMC_TRAPFRAME_TO_PC(tf); 1820587a072SJustin Hibbits sp = (uintptr_t *)PMC_TRAPFRAME_TO_FP(tf); 1830587a072SJustin Hibbits 1840587a072SJustin Hibbits for (frames = 1; frames < maxsamples; frames++) { 1850587a072SJustin Hibbits if (!INUSER(sp)) 1860587a072SJustin Hibbits break; 1870587a072SJustin Hibbits cc[frames++] = fuword(sp + 1); 1880587a072SJustin Hibbits sp = (uintptr_t *)fuword(sp); 1890587a072SJustin Hibbits } 1900587a072SJustin Hibbits return (frames); 1919596916cSJoseph Koshy } 192