1 /*- 2 * Copyright (c) 2011,2013 Justin Hibbits 3 * Copyright (c) 2005, Joseph Koshy 4 * All rights reserved. 5 * 6 * Redistribution and use in source and binary forms, with or without 7 * modification, are permitted provided that the following conditions 8 * are met: 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. 14 * 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 25 * SUCH DAMAGE. 26 * 27 */ 28 29 #include <sys/cdefs.h> 30 __FBSDID("$FreeBSD$"); 31 32 #include <sys/param.h> 33 #include <sys/pmc.h> 34 #include <sys/pmckern.h> 35 #include <sys/sysent.h> 36 #include <sys/systm.h> 37 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 */ 44 45 #include "hwpmc_powerpc.h" 46 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) 51 52 struct powerpc_cpu **powerpc_pcpu; 53 54 int 55 pmc_save_kernel_callchain(uintptr_t *cc, int maxsamples, 56 struct trapframe *tf) 57 { 58 uintptr_t *osp, *sp; 59 int frames = 0; 60 61 cc[frames++] = PMC_TRAPFRAME_TO_PC(tf); 62 sp = (uintptr_t *)PMC_TRAPFRAME_TO_FP(tf); 63 osp = NULL; 64 65 for (; frames < maxsamples; frames++) { 66 if (!INKERNEL(sp) || sp <= osp) 67 break; 68 #ifdef __powerpc64__ 69 cc[frames] = sp[2]; 70 #else 71 cc[frames] = sp[1]; 72 #endif 73 osp = sp; 74 sp = (uintptr_t *)*sp; 75 } 76 return (frames); 77 } 78 79 static int 80 powerpc_switch_in(struct pmc_cpu *pc, struct pmc_process *pp) 81 { 82 83 return (0); 84 } 85 86 static int 87 powerpc_switch_out(struct pmc_cpu *pc, struct pmc_process *pp) 88 { 89 90 return (0); 91 } 92 93 int 94 powerpc_describe(int cpu, int ri, struct pmc_info *pi, struct pmc **ppmc) 95 { 96 int error; 97 struct pmc_hw *phw; 98 char powerpc_name[PMC_NAME_MAX]; 99 100 KASSERT(cpu >= 0 && cpu < pmc_cpu_max(), 101 ("[powerpc,%d], illegal CPU %d", __LINE__, cpu)); 102 103 phw = &powerpc_pcpu[cpu]->pc_ppcpmcs[ri]; 104 snprintf(powerpc_name, sizeof(powerpc_name), "POWERPC-%d", ri); 105 if ((error = copystr(powerpc_name, pi->pm_name, PMC_NAME_MAX, 106 NULL)) != 0) 107 return error; 108 pi->pm_class = powerpc_pcpu[cpu]->pc_class; 109 if (phw->phw_state & PMC_PHW_FLAG_IS_ENABLED) { 110 pi->pm_enabled = TRUE; 111 *ppmc = phw->phw_pmc; 112 } else { 113 pi->pm_enabled = FALSE; 114 *ppmc = NULL; 115 } 116 117 return (0); 118 } 119 120 int 121 powerpc_get_config(int cpu, int ri, struct pmc **ppm) 122 { 123 124 *ppm = powerpc_pcpu[cpu]->pc_ppcpmcs[ri].phw_pmc; 125 126 return (0); 127 } 128 129 struct pmc_mdep * 130 pmc_md_initialize() 131 { 132 struct pmc_mdep *pmc_mdep; 133 int error; 134 uint16_t vers; 135 136 /* 137 * Allocate space for pointers to PMC HW descriptors and for 138 * the MDEP structure used by MI code. 139 */ 140 powerpc_pcpu = malloc(sizeof(struct powerpc_cpu *) * pmc_cpu_max(), M_PMC, 141 M_WAITOK|M_ZERO); 142 143 /* Just one class */ 144 pmc_mdep = pmc_mdep_alloc(1); 145 146 vers = mfpvr() >> 16; 147 148 pmc_mdep->pmd_switch_in = powerpc_switch_in; 149 pmc_mdep->pmd_switch_out = powerpc_switch_out; 150 151 switch (vers) { 152 case MPC7447A: 153 case MPC7448: 154 case MPC7450: 155 case MPC7455: 156 case MPC7457: 157 error = pmc_mpc7xxx_initialize(pmc_mdep); 158 break; 159 case IBM970: 160 case IBM970FX: 161 case IBM970MP: 162 error = pmc_ppc970_initialize(pmc_mdep); 163 break; 164 default: 165 error = -1; 166 break; 167 } 168 169 if (error != 0) { 170 pmc_mdep_free(pmc_mdep); 171 pmc_mdep = NULL; 172 } 173 174 return (pmc_mdep); 175 } 176 177 void 178 pmc_md_finalize(struct pmc_mdep *md) 179 { 180 181 free(powerpc_pcpu, M_PMC); 182 powerpc_pcpu = NULL; 183 } 184 185 int 186 pmc_save_user_callchain(uintptr_t *cc, int maxsamples, 187 struct trapframe *tf) 188 { 189 uintptr_t *osp, *sp; 190 int frames = 0; 191 192 cc[frames++] = PMC_TRAPFRAME_TO_PC(tf); 193 sp = (uintptr_t *)PMC_TRAPFRAME_TO_FP(tf); 194 osp = NULL; 195 196 for (; frames < maxsamples; frames++) { 197 if (!INUSER(sp) || sp <= osp) 198 break; 199 osp = sp; 200 #ifdef __powerpc64__ 201 /* Check if 32-bit mode. */ 202 if (!(tf->srr1 & PSL_SF)) { 203 cc[frames] = fuword32((uint32_t *)sp + 1); 204 sp = (uintptr_t *)(uintptr_t)fuword32(sp); 205 } else { 206 cc[frames] = fuword(sp + 2); 207 sp = (uintptr_t *)fuword(sp); 208 } 209 #else 210 cc[frames] = fuword32((uint32_t *)sp + 1); 211 sp = (uintptr_t *)fuword32(sp); 212 #endif 213 } 214 215 return (frames); 216 } 217