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