xref: /freebsd/sys/dev/hwpmc/hwpmc_powerpc.c (revision 3a92d97ff0f22d21608e1c19b83104c4937523b6)
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 	int frames = 0;
59 	uintptr_t *sp;
60 
61 	cc[frames++] = PMC_TRAPFRAME_TO_PC(tf);
62 	sp = (uintptr_t *)PMC_TRAPFRAME_TO_FP(tf);
63 
64 	for (; frames < maxsamples; frames++) {
65 		if (!INKERNEL(sp))
66 			break;
67 #ifdef __powerpc64__
68 		cc[frames++] = sp[2];
69 #else
70 		cc[frames++] = sp[1];
71 #endif
72 		sp = (uintptr_t *)*sp;
73 	}
74 	return (frames);
75 }
76 
77 static int
78 powerpc_switch_in(struct pmc_cpu *pc, struct pmc_process *pp)
79 {
80 
81 	return (0);
82 }
83 
84 static int
85 powerpc_switch_out(struct pmc_cpu *pc, struct pmc_process *pp)
86 {
87 
88 	return (0);
89 }
90 
91 int
92 powerpc_describe(int cpu, int ri, struct pmc_info *pi, struct pmc **ppmc)
93 {
94 	int error;
95 	struct pmc_hw *phw;
96 	char powerpc_name[PMC_NAME_MAX];
97 
98 	KASSERT(cpu >= 0 && cpu < pmc_cpu_max(),
99 	    ("[powerpc,%d], illegal CPU %d", __LINE__, cpu));
100 
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,
104 	    NULL)) != 0)
105 		return error;
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;
110 	} else {
111 		pi->pm_enabled = FALSE;
112 		*ppmc	       = NULL;
113 	}
114 
115 	return (0);
116 }
117 
118 int
119 powerpc_get_config(int cpu, int ri, struct pmc **ppm)
120 {
121 
122 	*ppm = powerpc_pcpu[cpu]->pc_ppcpmcs[ri].phw_pmc;
123 
124 	return (0);
125 }
126 
127 struct pmc_mdep *
128 pmc_md_initialize()
129 {
130 	struct pmc_mdep *pmc_mdep;
131 	int error;
132 	uint16_t vers;
133 
134 	/*
135 	 * Allocate space for pointers to PMC HW descriptors and for
136 	 * the MDEP structure used by MI code.
137 	 */
138 	powerpc_pcpu = malloc(sizeof(struct powerpc_cpu *) * pmc_cpu_max(), M_PMC,
139 			   M_WAITOK|M_ZERO);
140 
141 	/* Just one class */
142 	pmc_mdep = pmc_mdep_alloc(1);
143 
144 	vers = mfpvr() >> 16;
145 
146 	pmc_mdep->pmd_switch_in  = powerpc_switch_in;
147 	pmc_mdep->pmd_switch_out = powerpc_switch_out;
148 
149 	switch (vers) {
150 	case MPC7447A:
151 	case MPC7448:
152 	case MPC7450:
153 	case MPC7455:
154 	case MPC7457:
155 		error = pmc_mpc7xxx_initialize(pmc_mdep);
156 		break;
157 	case IBM970:
158 	case IBM970FX:
159 	case IBM970MP:
160 		error = pmc_ppc970_initialize(pmc_mdep);
161 		break;
162 	default:
163 		error = -1;
164 		break;
165 	}
166 
167 	if (error != 0) {
168 		pmc_mdep_free(pmc_mdep);
169 		pmc_mdep = NULL;
170 	}
171 
172 	return (pmc_mdep);
173 }
174 
175 void
176 pmc_md_finalize(struct pmc_mdep *md)
177 {
178 
179 	free(powerpc_pcpu, M_PMC);
180 	powerpc_pcpu = NULL;
181 }
182 
183 int
184 pmc_save_user_callchain(uintptr_t *cc, int maxsamples,
185     struct trapframe *tf)
186 {
187 	uintptr_t *sp;
188 	int frames = 0;
189 
190 	cc[frames++] = PMC_TRAPFRAME_TO_PC(tf);
191 	sp = (uintptr_t *)PMC_TRAPFRAME_TO_FP(tf);
192 
193 	for (; frames < maxsamples; frames++) {
194 		if (!INUSER(sp))
195 			break;
196 #ifdef __powerpc64__
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);
201 		} else {
202 			cc[frames++] = fuword(sp + 2);
203 			sp = (uintptr_t *)fuword(sp);
204 		}
205 #else
206 		cc[frames++] = fuword32((uint32_t *)sp + 1);
207 		sp = (uintptr_t *)fuword32(sp);
208 #endif
209 	}
210 
211 	return (frames);
212 }
213